Skip to content

Commit 57d3d07

Browse files
authored
[libc] added newhdrgen class implementation (#96710)
Added a class representation of a libc header file, allowing for easier conversion from YAML to .h file output. Classes include: - Function (representing function headers) - Include (representing various include statements found on a header file) - Macro (representing macro definitions) - Enumeration (representing enum definitions) - Type (representing include statements for NamedTypes) - Object (representing ObjectSpec defintitions)
1 parent 133492f commit 57d3d07

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Enumeration class for libc function headers ----------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Enumeration:
13+
def __init__(self, name, value=None):
14+
self.name = name
15+
self.value = value
16+
17+
def __str__(self):
18+
if self.value != None:
19+
return f"{self.name} = {self.value}"
20+
else:
21+
return f"{self.name}"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Function class for libc function headers -------------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Function:
13+
def __init__(
14+
self, standards, return_type, name, arguments, guard=None, attributes=[]
15+
):
16+
self.standards = standards
17+
self.return_type = return_type
18+
self.name = name
19+
self.arguments = [arg["type"] for arg in arguments]
20+
self.guard = guard
21+
self.attributes = attributes
22+
23+
def __str__(self):
24+
args_str = ", ".join(self.arguments)
25+
attributes_str = " ".join(self.attributes)
26+
result = f"{self.return_type} {self.name}({args_str}){attributes_str};"
27+
if self.guard:
28+
result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
29+
return result
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Include class for libc function headers --------------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Include:
13+
def __init__(self, name):
14+
self.name = name
15+
16+
def __str__(self):
17+
return f'#include "{self.name}"'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Macro class for libc function headers ----------------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Macro:
13+
def __init__(self, name, value=None):
14+
self.name = name
15+
self.value = value
16+
17+
def __str__(self):
18+
if self.value != None:
19+
return f"#define {self.name} {self.value}"
20+
else:
21+
return f"#define {self.name}"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Object class for libc function headers ---------------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Object:
13+
def __init__(self, name, type):
14+
self.name = name
15+
self.type = type
16+
17+
def __str__(self):
18+
return f"extern {self.type} {self.name}"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
#
3+
# ====-- Type class for libc function headers -----------------*- python -*--==#
4+
#
5+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6+
# See https://llvm.org/LICENSE.txt for license information.
7+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
#
9+
# ==-------------------------------------------------------------------------==#
10+
11+
12+
class Type:
13+
def __init__(self, type_name):
14+
self.type_name = type_name
15+
16+
def __str__(self):
17+
return f"#include <llvm-libc-types/{self.type_name}.h>"

0 commit comments

Comments
 (0)