Skip to content

[libc] added newhdrgen class implementation #96710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 26, 2024

Conversation

RoseZhang03
Copy link
Contributor

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 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)
@llvmbot llvmbot added the libc label Jun 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 25, 2024

@llvm/pr-subscribers-libc

Author: None (RoseZhang03)

Changes

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)

Full diff: https://github.com/llvm/llvm-project/pull/96710.diff

6 Files Affected:

  • (added) libc/newhdrgen/class_implementation/classes/enumeration.py (+20)
  • (added) libc/newhdrgen/class_implementation/classes/function.py (+26)
  • (added) libc/newhdrgen/class_implementation/classes/include.py (+17)
  • (added) libc/newhdrgen/class_implementation/classes/macro.py (+21)
  • (added) libc/newhdrgen/class_implementation/classes/object.py (+18)
  • (added) libc/newhdrgen/class_implementation/classes/type.py (+17)
diff --git a/libc/newhdrgen/class_implementation/classes/enumeration.py b/libc/newhdrgen/class_implementation/classes/enumeration.py
new file mode 100644
index 0000000000000..c20f0a33ec174
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/enumeration.py
@@ -0,0 +1,20 @@
+#!/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}"      
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/function.py b/libc/newhdrgen/class_implementation/classes/function.py
new file mode 100644
index 0000000000000..b9c089ca5bfc8
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/function.py
@@ -0,0 +1,26 @@
+#!/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, return_type, name, arguments, guard=None, attributes=None):
+        self.return_type = return_type
+        self.name = name
+        self.arguments = [arg if isinstance(arg, str) else arg['type'] for arg in arguments]
+        self.guard = guard
+        self.attributes = attributes or []
+
+    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
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/include.py b/libc/newhdrgen/class_implementation/classes/include.py
new file mode 100644
index 0000000000000..b4583c66c88fc
--- /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}"'
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/macro.py b/libc/newhdrgen/class_implementation/classes/macro.py
new file mode 100644
index 0000000000000..9c562708ee284
--- /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}"
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/object.py b/libc/newhdrgen/class_implementation/classes/object.py
new file mode 100644
index 0000000000000..ec187a5cc8a95
--- /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}'
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/type.py b/libc/newhdrgen/class_implementation/classes/type.py
new file mode 100644
index 0000000000000..0882e3a1e0d0b
--- /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>"
\ No newline at end of file

Copy link

github-actions bot commented Jun 25, 2024

✅ With the latest revision this PR passed the Python code formatter.

@nickdesaulniers
Copy link
Member

See the presubmit bot warning; you'll want to format this PR.

@SchrodingerZhu
Copy link
Contributor

if the input argument is standards, shouldn't the field also be of the same plurality?

self.standard = standards
self.return_type = return_type
self.name = name
self.arguments = [arg["type"] for arg in arguments]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we extract just the type for the argument? Don't we need the identifier? Parameter attributes?

What fields are you expecting an arg to have?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we use arguments in the Yaml file is carrying the parameter type so we can identify it as type in arguments.

- name: call_once
    standard: stdc
    return_type: void
    arguments:
      - type: once_flag *
      - type: __call_once_func_

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have this in case we end up having to separate the argument type into prefix, name, suffix. If it is confirmed that we don't need to do so, we can easily remove "type:" so that it is a regular list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up adding more fields to an argument, then we might want just to keep a Function's arguments as a list of dict's rather than list of strings of the parameter types. But I'll leave that up to later patches that make use of these objects.

@RoseZhang03
Copy link
Contributor Author

if the input argument is standards, shouldn't the field also be of the same plurality?

Thank you for catching that, will fix now.

self.standard = standards
self.return_type = return_type
self.name = name
self.arguments = [arg["type"] for arg in arguments]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up adding more fields to an argument, then we might want just to keep a Function's arguments as a list of dict's rather than list of strings of the parameter types. But I'll leave that up to later patches that make use of these objects.

@RoseZhang03 RoseZhang03 merged commit 57d3d07 into llvm:main Jun 26, 2024
4 of 5 checks passed
@RoseZhang03 RoseZhang03 deleted the rosezhang7 branch June 27, 2024 20:44
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
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)
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants