GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 5 / 1 / 6
Functions: 100.0% 1 / 0 / 1
Branches: 75.0% 3 / 0 / 4

src/typechecker/BuiltinFunctions.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <algorithm>
6 #include <array>
7 #include <limits>
8 #include <string_view>
9 #include <unordered_map>
10
11 namespace spice::compiler {
12
13 // Metadata about a builtin function (argument/template counts and flags only, no method pointers)
14 struct BuiltinFunctionInfo {
15 unsigned int minTemplateTypes = 0;
16 unsigned int maxTemplateTypes = 0;
17 unsigned int minArgTypes = 0;
18 unsigned int maxArgTypes = 0;
19 bool allTemplateTypesOrAllArgTypes = false;
20 bool isFunctionTerminator = false;
21 };
22
23 struct BuiltinFunctionEntry {
24 std::string_view name;
25 BuiltinFunctionInfo info;
26 };
27
28 // Constants
29 // Documented builtins
30 static constexpr std::string_view BUILTIN_FCT_NAME_PRINTF = "printf";
31 static constexpr std::string_view BUILTIN_FCT_NAME_SIZEOF = "sizeof";
32 static constexpr std::string_view BUILTIN_FCT_NAME_ALIGNOF = "alignof";
33 static constexpr std::string_view BUILTIN_FCT_NAME_TYPEID = "typeid";
34 static constexpr std::string_view BUILTIN_FCT_NAME_TYPENAME = "typename";
35 static constexpr std::string_view BUILTIN_FCT_NAME_LEN = "len";
36 static constexpr std::string_view BUILTIN_FCT_NAME_PANIC = "panic";
37 static constexpr std::string_view BUILTIN_FCT_NAME_SYSCALL = "syscall";
38 // Undocumented builtins (intended to be primarily used via std wrapper functions)
39 static constexpr std::string_view BUILTIN_FCT_NAME_OFFSETOF = "__offsetof";
40 static constexpr std::string_view BUILTIN_FCT_NAME_IS_SAME = "__is_same";
41 static constexpr std::string_view BUILTIN_FCT_NAME_IMPLEMENTS_INTERFACE = "__implements_interface";
42 static constexpr std::string_view BUILTIN_FCT_NAME_GET_BUILD_VAR = "__get_build_var";
43 static constexpr std::string_view BUILTIN_FCT_NAME_IS_TRIVIALLY_CONSTRUCTIBLE = "__is_trivially_constructible";
44 static constexpr std::string_view BUILTIN_FCT_NAME_IS_TRIVIALLY_COPYABLE = "__is_trivially_copyable";
45 static constexpr std::string_view BUILTIN_FCT_NAME_IS_TRIVIALLY_DESTRUCTIBLE = "__is_trivially_destructible";
46 static constexpr std::string_view BUILTIN_FCT_NAME_IS_HEAP = "__is_heap";
47 static constexpr std::string_view BUILTIN_FCT_NAME_NEW = "__new";
48 static constexpr std::string_view BUILTIN_FCT_NAME_PLACEMENT_NEW = "__placement_new";
49
50 static constexpr std::array BUILTIN_FUNCTIONS = {
51 BuiltinFunctionEntry{
52 BUILTIN_FCT_NAME_PRINTF,
53 BuiltinFunctionInfo{
54 .minArgTypes = 1,
55 .maxArgTypes = std::numeric_limits<unsigned int>::max(),
56 },
57 },
58 BuiltinFunctionEntry{
59 BUILTIN_FCT_NAME_SIZEOF,
60 BuiltinFunctionInfo{
61 .maxTemplateTypes = 1,
62 .maxArgTypes = 1,
63 .allTemplateTypesOrAllArgTypes = true,
64 },
65 },
66 BuiltinFunctionEntry{
67 BUILTIN_FCT_NAME_ALIGNOF,
68 BuiltinFunctionInfo{
69 .maxTemplateTypes = 1,
70 .maxArgTypes = 1,
71 .allTemplateTypesOrAllArgTypes = true,
72 },
73 },
74 BuiltinFunctionEntry{
75 BUILTIN_FCT_NAME_OFFSETOF,
76 BuiltinFunctionInfo{
77 .minArgTypes = 2,
78 .maxArgTypes = 2,
79 },
80 },
81 BuiltinFunctionEntry{
82 BUILTIN_FCT_NAME_TYPEID,
83 BuiltinFunctionInfo{
84 .maxTemplateTypes = 1,
85 .maxArgTypes = 1,
86 .allTemplateTypesOrAllArgTypes = true,
87 },
88 },
89 BuiltinFunctionEntry{
90 BUILTIN_FCT_NAME_TYPENAME,
91 BuiltinFunctionInfo{
92 .maxTemplateTypes = 1,
93 .maxArgTypes = 1,
94 .allTemplateTypesOrAllArgTypes = true,
95 },
96 },
97 BuiltinFunctionEntry{
98 BUILTIN_FCT_NAME_LEN,
99 BuiltinFunctionInfo{
100 .minArgTypes = 1,
101 .maxArgTypes = 1,
102 },
103 },
104 BuiltinFunctionEntry{
105 BUILTIN_FCT_NAME_PANIC,
106 BuiltinFunctionInfo{
107 .minArgTypes = 1,
108 .maxArgTypes = 1,
109 .isFunctionTerminator = true,
110 },
111 },
112 BuiltinFunctionEntry{
113 BUILTIN_FCT_NAME_SYSCALL,
114 BuiltinFunctionInfo{
115 .minArgTypes = 1,
116 // According to https://www.chromium.org/chromium-os/developer-library/reference/linux-constants/syscalls/
117 .maxArgTypes = 6,
118 },
119 },
120 BuiltinFunctionEntry{
121 BUILTIN_FCT_NAME_IS_SAME,
122 BuiltinFunctionInfo{
123 .minTemplateTypes = 2,
124 .maxTemplateTypes = std::numeric_limits<unsigned int>::max(),
125 },
126 },
127 BuiltinFunctionEntry{
128 BUILTIN_FCT_NAME_IMPLEMENTS_INTERFACE,
129 BuiltinFunctionInfo{
130 .minTemplateTypes = 2,
131 .maxTemplateTypes = 2,
132 },
133 },
134 BuiltinFunctionEntry{
135 BUILTIN_FCT_NAME_GET_BUILD_VAR,
136 BuiltinFunctionInfo{
137 .minTemplateTypes = 1,
138 .maxTemplateTypes = 1,
139 .minArgTypes = 1,
140 .maxArgTypes = 2,
141 },
142 },
143 BuiltinFunctionEntry{
144 BUILTIN_FCT_NAME_IS_TRIVIALLY_CONSTRUCTIBLE,
145 BuiltinFunctionInfo{
146 .minTemplateTypes = 1,
147 .maxTemplateTypes = 1,
148 },
149 },
150 BuiltinFunctionEntry{
151 BUILTIN_FCT_NAME_IS_TRIVIALLY_COPYABLE,
152 BuiltinFunctionInfo{
153 .minTemplateTypes = 1,
154 .maxTemplateTypes = 1,
155 },
156 },
157 BuiltinFunctionEntry{
158 BUILTIN_FCT_NAME_IS_TRIVIALLY_DESTRUCTIBLE,
159 BuiltinFunctionInfo{
160 .minTemplateTypes = 1,
161 .maxTemplateTypes = 1,
162 },
163 },
164 BuiltinFunctionEntry{
165 BUILTIN_FCT_NAME_IS_HEAP,
166 BuiltinFunctionInfo{
167 .minTemplateTypes = 1,
168 .maxTemplateTypes = 1,
169 },
170 },
171 BuiltinFunctionEntry{
172 BUILTIN_FCT_NAME_NEW,
173 BuiltinFunctionInfo{
174 .minTemplateTypes = 1,
175 .maxTemplateTypes = 1,
176 .maxArgTypes = std::numeric_limits<unsigned int>::max(),
177 },
178 },
179 BuiltinFunctionEntry{
180 BUILTIN_FCT_NAME_PLACEMENT_NEW,
181 BuiltinFunctionInfo{
182 .minTemplateTypes = 1,
183 .maxTemplateTypes = 1,
184 .minArgTypes = 1,
185 .maxArgTypes = std::numeric_limits<unsigned int>::max(),
186 },
187 },
188 };
189
190 6 static const std::unordered_map<std::string_view, BuiltinFunctionInfo> BUILTIN_FUNCTIONS_MAP = [] {
191 6 std::unordered_map<std::string_view, BuiltinFunctionInfo> map;
192
2/2
✓ Branch 6 → 4 taken 108 times.
✓ Branch 6 → 7 taken 6 times.
114 for (const auto &[name, info] : BUILTIN_FUNCTIONS)
193
1/2
✓ Branch 4 → 5 taken 108 times.
✗ Branch 4 → 9 not taken.
108 map.emplace(name, info);
194 6 return map;
195 }(); // LCOV_EXCL_LINE - Coverage tool false positive
196
197 // Validate builtins at compile time
198 static consteval bool validateBuiltins() {
199 return std::ranges::all_of(BUILTIN_FUNCTIONS, [](const BuiltinFunctionEntry &entry) {
200 const auto &[name, info] = entry;
201 return !name.empty() && info.minTemplateTypes <= info.maxTemplateTypes && info.minArgTypes <= info.maxArgTypes;
202 });
203 }
204 static_assert(validateBuiltins(), "Invalid builtin function definitions");
205
206 } // namespace spice::compiler
207