xref: /llvm-project/libc/utils/hdrgen/header.py (revision 6ad0dcf67f5dccdf8506ce5f51d793062a1c6879)
1# ====- HeaderFile Class for libc function headers  -----------*- python -*--==#
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7# ==-------------------------------------------------------------------------==#
8
9
10class HeaderFile:
11    def __init__(self, name):
12        self.template_file = None
13        self.name = name
14        self.macros = []
15        self.types = []
16        self.enumerations = []
17        self.objects = []
18        self.functions = []
19
20    def add_macro(self, macro):
21        self.macros.append(macro)
22
23    def add_type(self, type_):
24        self.types.append(type_)
25
26    def add_enumeration(self, enumeration):
27        self.enumerations.append(enumeration)
28
29    def add_object(self, object):
30        self.objects.append(object)
31
32    def add_function(self, function):
33        self.functions.append(function)
34
35    def public_api(self):
36        content = [""]
37
38        for macro in self.macros:
39            content.append(f"{macro}\n")
40
41        for type_ in self.types:
42            content.append(f"{type_}")
43
44        if self.enumerations:
45            combined_enum_content = ",\n  ".join(
46                str(enum) for enum in self.enumerations
47            )
48            content.append(f"\nenum {{\n  {combined_enum_content},\n}};")
49
50        content.append("\n__BEGIN_C_DECLS\n")
51
52        current_guard = None
53        for function in self.functions:
54            if function.guard == None:
55                content.append(str(function) + " __NOEXCEPT;")
56                content.append("")
57            else:
58                if current_guard == None:
59                    current_guard = function.guard
60                    content.append(f"#ifdef {current_guard}")
61                    content.append(str(function) + " __NOEXCEPT;")
62                    content.append("")
63                elif current_guard == function.guard:
64                    content.append(str(function) + " __NOEXCEPT;")
65                    content.append("")
66                else:
67                    content.pop()
68                    content.append(f"#endif // {current_guard}")
69                    content.append("")
70                    current_guard = function.guard
71                    content.append(f"#ifdef {current_guard}")
72                    content.append(str(function) + " __NOEXCEPT;")
73                    content.append("")
74        if current_guard != None:
75            content.pop()
76            content.append(f"#endif // {current_guard}")
77            content.append("")
78
79        for object in self.objects:
80            content.append(str(object))
81        if self.objects:
82            content.append("\n__END_C_DECLS")
83        else:
84            content.append("__END_C_DECLS")
85
86        return "\n".join(content)
87