xref: /llvm-project/libc/utils/hdrgen/header.py (revision 6ad0dcf67f5dccdf8506ce5f51d793062a1c6879)
19abcca5eSRoland McGrath# ====- HeaderFile Class for libc function headers  -----------*- python -*--==#
29abcca5eSRoland McGrath#
39abcca5eSRoland McGrath# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
49abcca5eSRoland McGrath# See https://llvm.org/LICENSE.txt for license information.
59abcca5eSRoland McGrath# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
69abcca5eSRoland McGrath#
79abcca5eSRoland McGrath# ==-------------------------------------------------------------------------==#
89abcca5eSRoland McGrath
99abcca5eSRoland McGrath
109abcca5eSRoland McGrathclass HeaderFile:
119abcca5eSRoland McGrath    def __init__(self, name):
12*6ad0dcf6SRoland McGrath        self.template_file = None
139abcca5eSRoland McGrath        self.name = name
149abcca5eSRoland McGrath        self.macros = []
159abcca5eSRoland McGrath        self.types = []
169abcca5eSRoland McGrath        self.enumerations = []
179abcca5eSRoland McGrath        self.objects = []
189abcca5eSRoland McGrath        self.functions = []
199abcca5eSRoland McGrath
209abcca5eSRoland McGrath    def add_macro(self, macro):
219abcca5eSRoland McGrath        self.macros.append(macro)
229abcca5eSRoland McGrath
239abcca5eSRoland McGrath    def add_type(self, type_):
249abcca5eSRoland McGrath        self.types.append(type_)
259abcca5eSRoland McGrath
269abcca5eSRoland McGrath    def add_enumeration(self, enumeration):
279abcca5eSRoland McGrath        self.enumerations.append(enumeration)
289abcca5eSRoland McGrath
299abcca5eSRoland McGrath    def add_object(self, object):
309abcca5eSRoland McGrath        self.objects.append(object)
319abcca5eSRoland McGrath
329abcca5eSRoland McGrath    def add_function(self, function):
339abcca5eSRoland McGrath        self.functions.append(function)
349abcca5eSRoland McGrath
35*6ad0dcf6SRoland McGrath    def public_api(self):
369abcca5eSRoland McGrath        content = [""]
379abcca5eSRoland McGrath
389abcca5eSRoland McGrath        for macro in self.macros:
399abcca5eSRoland McGrath            content.append(f"{macro}\n")
409abcca5eSRoland McGrath
419abcca5eSRoland McGrath        for type_ in self.types:
429abcca5eSRoland McGrath            content.append(f"{type_}")
439abcca5eSRoland McGrath
449abcca5eSRoland McGrath        if self.enumerations:
459abcca5eSRoland McGrath            combined_enum_content = ",\n  ".join(
469abcca5eSRoland McGrath                str(enum) for enum in self.enumerations
479abcca5eSRoland McGrath            )
489abcca5eSRoland McGrath            content.append(f"\nenum {{\n  {combined_enum_content},\n}};")
499abcca5eSRoland McGrath
509abcca5eSRoland McGrath        content.append("\n__BEGIN_C_DECLS\n")
519abcca5eSRoland McGrath
529abcca5eSRoland McGrath        current_guard = None
539abcca5eSRoland McGrath        for function in self.functions:
549abcca5eSRoland McGrath            if function.guard == None:
559abcca5eSRoland McGrath                content.append(str(function) + " __NOEXCEPT;")
569abcca5eSRoland McGrath                content.append("")
579abcca5eSRoland McGrath            else:
589abcca5eSRoland McGrath                if current_guard == None:
599abcca5eSRoland McGrath                    current_guard = function.guard
609abcca5eSRoland McGrath                    content.append(f"#ifdef {current_guard}")
619abcca5eSRoland McGrath                    content.append(str(function) + " __NOEXCEPT;")
629abcca5eSRoland McGrath                    content.append("")
639abcca5eSRoland McGrath                elif current_guard == function.guard:
649abcca5eSRoland McGrath                    content.append(str(function) + " __NOEXCEPT;")
659abcca5eSRoland McGrath                    content.append("")
669abcca5eSRoland McGrath                else:
679abcca5eSRoland McGrath                    content.pop()
689abcca5eSRoland McGrath                    content.append(f"#endif // {current_guard}")
699abcca5eSRoland McGrath                    content.append("")
709abcca5eSRoland McGrath                    current_guard = function.guard
719abcca5eSRoland McGrath                    content.append(f"#ifdef {current_guard}")
729abcca5eSRoland McGrath                    content.append(str(function) + " __NOEXCEPT;")
739abcca5eSRoland McGrath                    content.append("")
749abcca5eSRoland McGrath        if current_guard != None:
759abcca5eSRoland McGrath            content.pop()
769abcca5eSRoland McGrath            content.append(f"#endif // {current_guard}")
779abcca5eSRoland McGrath            content.append("")
789abcca5eSRoland McGrath
799abcca5eSRoland McGrath        for object in self.objects:
809abcca5eSRoland McGrath            content.append(str(object))
819abcca5eSRoland McGrath        if self.objects:
829abcca5eSRoland McGrath            content.append("\n__END_C_DECLS")
839abcca5eSRoland McGrath        else:
849abcca5eSRoland McGrath            content.append("__END_C_DECLS")
859abcca5eSRoland McGrath
869abcca5eSRoland McGrath        return "\n".join(content)
87