[libc-commits] [libc] 57d3d07 - [libc] added newhdrgen class implementation (#96710)
via libc-commits
libc-commits at lists.llvm.org
Wed Jun 26 13:06:24 PDT 2024
Author: RoseZhang03
Date: 2024-06-26T20:06:21Z
New Revision: 57d3d070502f54c63c5fca588cf74b78d607e272
URL: https://github.com/llvm/llvm-project/commit/57d3d070502f54c63c5fca588cf74b78d607e272
DIFF: https://github.com/llvm/llvm-project/commit/57d3d070502f54c63c5fca588cf74b78d607e272.diff
LOG: [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)
Added:
libc/newhdrgen/class_implementation/classes/enumeration.py
libc/newhdrgen/class_implementation/classes/function.py
libc/newhdrgen/class_implementation/classes/include.py
libc/newhdrgen/class_implementation/classes/macro.py
libc/newhdrgen/class_implementation/classes/object.py
libc/newhdrgen/class_implementation/classes/type.py
Modified:
Removed:
################################################################################
diff --git a/libc/newhdrgen/class_implementation/classes/enumeration.py b/libc/newhdrgen/class_implementation/classes/enumeration.py
new file mode 100644
index 0000000000000..be03dbf603c2b
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/enumeration.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+#
+# ====-- Enumeration class for libc function headers ----------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Enumeration:
+ def __init__(self, name, value=None):
+ self.name = name
+ self.value = value
+
+ def __str__(self):
+ if self.value != None:
+ return f"{self.name} = {self.value}"
+ else:
+ return f"{self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/function.py b/libc/newhdrgen/class_implementation/classes/function.py
new file mode 100644
index 0000000000000..dc73e0e1ea5a9
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/function.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+#
+# ====-- Function class for libc function headers -------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Function:
+ def __init__(
+ self, standards, return_type, name, arguments, guard=None, attributes=[]
+ ):
+ self.standards = standards
+ self.return_type = return_type
+ self.name = name
+ self.arguments = [arg["type"] for arg in arguments]
+ self.guard = guard
+ self.attributes = attributes
+
+ def __str__(self):
+ args_str = ", ".join(self.arguments)
+ attributes_str = " ".join(self.attributes)
+ result = f"{self.return_type} {self.name}({args_str}){attributes_str};"
+ if self.guard:
+ result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
+ return result
diff --git a/libc/newhdrgen/class_implementation/classes/include.py b/libc/newhdrgen/class_implementation/classes/include.py
new file mode 100644
index 0000000000000..2657f2e7c931c
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/include.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+#
+# ====-- Include class for libc function headers --------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Include:
+ def __init__(self, name):
+ self.name = name
+
+ def __str__(self):
+ return f'#include "{self.name}"'
diff --git a/libc/newhdrgen/class_implementation/classes/macro.py b/libc/newhdrgen/class_implementation/classes/macro.py
new file mode 100644
index 0000000000000..bf17ae6b6c5ab
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/macro.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+#
+# ====-- Macro class for libc function headers ----------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Macro:
+ def __init__(self, name, value=None):
+ self.name = name
+ self.value = value
+
+ def __str__(self):
+ if self.value != None:
+ return f"#define {self.name} {self.value}"
+ else:
+ return f"#define {self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/object.py b/libc/newhdrgen/class_implementation/classes/object.py
new file mode 100644
index 0000000000000..c65a82e1a660d
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/object.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+#
+# ====-- Object class for libc function headers ---------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Object:
+ def __init__(self, name, type):
+ self.name = name
+ self.type = type
+
+ def __str__(self):
+ return f"extern {self.type} {self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/type.py b/libc/newhdrgen/class_implementation/classes/type.py
new file mode 100644
index 0000000000000..8e4a8f3cc9c58
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/type.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+#
+# ====-- Type class for libc function headers -----------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Type:
+ def __init__(self, type_name):
+ self.type_name = type_name
+
+ def __str__(self):
+ return f"#include <llvm-libc-types/{self.type_name}.h>"
More information about the libc-commits
mailing list