• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.name = name
13        self.macros = []
14        self.types = []
15        self.enumerations = []
16        self.objects = []
17        self.functions = []
18
19    def add_macro(self, macro):
20        self.macros.append(macro)
21
22    def add_type(self, type_):
23        self.types.append(type_)
24
25    def add_enumeration(self, enumeration):
26        self.enumerations.append(enumeration)
27
28    def add_object(self, object):
29        self.objects.append(object)
30
31    def add_function(self, function):
32        self.functions.append(function)
33
34    def __str__(self):
35        content = [""]
36
37        for macro in self.macros:
38            content.append(f"{macro}\n")
39
40        for type_ in self.types:
41            content.append(f"{type_}")
42
43        if self.enumerations:
44            combined_enum_content = ",\n  ".join(
45                str(enum) for enum in self.enumerations
46            )
47            content.append(f"\nenum {{\n  {combined_enum_content},\n}};")
48
49        content.append("\n__BEGIN_C_DECLS\n")
50
51        current_guard = None
52        for function in self.functions:
53            if function.guard == None:
54                content.append(str(function) + " __NOEXCEPT;")
55                content.append("")
56            else:
57                if current_guard == None:
58                    current_guard = function.guard
59                    content.append(f"#ifdef {current_guard}")
60                    content.append(str(function) + " __NOEXCEPT;")
61                    content.append("")
62                elif current_guard == function.guard:
63                    content.append(str(function) + " __NOEXCEPT;")
64                    content.append("")
65                else:
66                    content.pop()
67                    content.append(f"#endif // {current_guard}")
68                    content.append("")
69                    current_guard = function.guard
70                    content.append(f"#ifdef {current_guard}")
71                    content.append(str(function) + " __NOEXCEPT;")
72                    content.append("")
73        if current_guard != None:
74            content.pop()
75            content.append(f"#endif // {current_guard}")
76            content.append("")
77
78        for object in self.objects:
79            content.append(str(object))
80        if self.objects:
81            content.append("\n__END_C_DECLS")
82        else:
83            content.append("__END_C_DECLS")
84
85        return "\n".join(content)
86