GCC Code Coverage Report


src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
50 / 50
100.0%
Branches:
48 / 48
100.0%
Functions:
6 / 6, 1 excl
100.0%

typechecker/builtin-functions.spice
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 import "std/type/types";
4
5 // Documented builtins
6 public const string BUILTIN_FCT_NAME_PRINTF = "printf";
7 public const string BUILTIN_FCT_NAME_SIZEOF = "sizeof";
8 public const string BUILTIN_FCT_NAME_ALIGNOF = "alignof";
9 public const string BUILTIN_FCT_NAME_TYPEID = "typeid";
10 public const string BUILTIN_FCT_NAME_TYPENAME = "typename";
11 public const string BUILTIN_FCT_NAME_LEN = "len";
12 public const string BUILTIN_FCT_NAME_PANIC = "panic";
13 public const string BUILTIN_FCT_NAME_SYSCALL = "syscall";
14 // Undocumented builtins (intended to be primarily used via std wrapper functions)
15 public const string BUILTIN_FCT_NAME_OFFSETOF = "__offsetof";
16 public const string BUILTIN_FCT_NAME_IS_SAME = "__is_same";
17 public const string BUILTIN_FCT_NAME_IMPLEMENTS_INTERFACE = "__implements_interface";
18 public const string BUILTIN_FCT_NAME_GET_BUILD_VAR = "__get_build_var";
19 public const string BUILTIN_FCT_NAME_IS_TRIVIALLY_CONSTRUCTIBLE = "__is_trivially_constructible";
20 public const string BUILTIN_FCT_NAME_IS_TRIVIALLY_COPYABLE = "__is_trivially_copyable";
21 public const string BUILTIN_FCT_NAME_IS_TRIVIALLY_DESTRUCTIBLE = "__is_trivially_destructible";
22 public const string BUILTIN_FCT_NAME_IS_HEAP = "__is_heap";
23 public const string BUILTIN_FCT_NAME_NEW = "__new";
24 public const string BUILTIN_FCT_NAME_PLACEMENT_NEW = "__placement_new";
25 public const string BUILTIN_FCT_NAME_SOURCE_FILE = "__source_file";
26 public const string BUILTIN_FCT_NAME_SOURCE_LINE = "__source_line";
27 public const string BUILTIN_FCT_NAME_SOURCE_COLUMN = "__source_column";
28 public const string BUILTIN_FCT_NAME_ERR_TRACE_BUFFER = "__err_trace_buffer";
29 public const string BUILTIN_FCT_NAME_STDERR = "__stderr";
30 public const string BUILTIN_FCT_NAME_FRAME_ADDRESS = "__frame_address";
31
32 /**
33 * Metadata about a builtin function (argument/template counts and flags only, no method pointers)
34 */
35 public type BuiltinFunctionInfo struct {
36 348432 public unsigned int minTemplateTypes = 0u
37 348432 public unsigned int maxTemplateTypes = 0u
38 348432 public unsigned int minArgTypes = 0u
39 348432 public unsigned int maxArgTypes = 0u
40 348432 public bool allTemplateTypesOrAllArgTypes = false
41 348432 public bool isFunctionTerminator = false
42 }
43
44 586354 public p BuiltinFunctionInfo.ctor() {}
45
46 55255 public p BuiltinFunctionInfo.ctor(unsigned int minTemplateTypes, unsigned int maxTemplateTypes, unsigned int minArgTypes, unsigned int maxArgTypes, bool allTemplateTypesOrAllArgTypes = false, bool isFunctionTerminator = false) {
47 55255 this.minTemplateTypes = minTemplateTypes;
48 55255 this.maxTemplateTypes = maxTemplateTypes;
49 55255 this.minArgTypes = minArgTypes;
50 55255 this.maxArgTypes = maxArgTypes;
51 55255 this.allTemplateTypesOrAllArgTypes = allTemplateTypesOrAllArgTypes;
52 55255 this.isFunctionTerminator = isFunctionTerminator;
53 55255 }
54
55 /**
56 * Look up the metadata of a builtin function.
57 *
58 * @param name Function name
59 * @param info Receives the metadata of the builtin function, if found
60 * @return True if the given name is the name of a builtin function
61 */
62 293177 public f<bool> lookupBuiltinFunction(const String& name, BuiltinFunctionInfo& info) {
63 293177 const unsigned int unlimited = UINT_MAX_VALUE;
64
2/2
✓ Branch 0 taken 13748 times.
✓ Branch 1 taken 279429 times.
293177 if name == BUILTIN_FCT_NAME_PRINTF {
65 13748 info = BuiltinFunctionInfo(0u, 0u, 1u, unlimited);
66
8/8
✓ Branch 0 taken 275179 times.
✓ Branch 1 taken 4250 times.
✓ Branch 2 taken 274996 times.
✓ Branch 3 taken 183 times.
✓ Branch 4 taken 169 times.
✓ Branch 5 taken 274827 times.
✓ Branch 6 taken 4729 times.
✓ Branch 7 taken 274700 times.
293177 } else if name == BUILTIN_FCT_NAME_SIZEOF || name == BUILTIN_FCT_NAME_ALIGNOF || name == BUILTIN_FCT_NAME_TYPEID || name == BUILTIN_FCT_NAME_TYPENAME {
67 4729 info = BuiltinFunctionInfo(0u, 1u, 0u, 1u, true);
68
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 274499 times.
279429 } else if name == BUILTIN_FCT_NAME_OFFSETOF {
69 201 info = BuiltinFunctionInfo(0u, 0u, 2u, 2u);
70
2/2
✓ Branch 0 taken 988 times.
✓ Branch 1 taken 273511 times.
274700 } else if name == BUILTIN_FCT_NAME_LEN {
71 988 info = BuiltinFunctionInfo(0u, 0u, 1u, 1u);
72
2/2
✓ Branch 0 taken 19884 times.
✓ Branch 1 taken 253627 times.
274499 } else if name == BUILTIN_FCT_NAME_PANIC {
73 19884 info = BuiltinFunctionInfo(0u, 0u, 1u, 1u, false, true);
74
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 253587 times.
273511 } else if name == BUILTIN_FCT_NAME_SYSCALL {
75 // The syscall number plus up to 6 args
76 40 info = BuiltinFunctionInfo(0u, 0u, 1u, 7u);
77
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 253491 times.
253627 } else if name == BUILTIN_FCT_NAME_IS_SAME {
78 96 info = BuiltinFunctionInfo(2u, unlimited, 0u, 0u);
79
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 253467 times.
253587 } else if name == BUILTIN_FCT_NAME_IMPLEMENTS_INTERFACE {
80 24 info = BuiltinFunctionInfo(2u, 2u, 0u, 0u);
81
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 253341 times.
253491 } else if name == BUILTIN_FCT_NAME_GET_BUILD_VAR {
82 126 info = BuiltinFunctionInfo(1u, 1u, 1u, 2u);
83
8/8
✓ Branch 0 taken 253301 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 253241 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 1274 times.
✓ Branch 5 taken 251967 times.
✓ Branch 6 taken 1423 times.
✓ Branch 7 taken 251918 times.
253467 } else if name == BUILTIN_FCT_NAME_IS_TRIVIALLY_CONSTRUCTIBLE || name == BUILTIN_FCT_NAME_IS_TRIVIALLY_COPYABLE || name == BUILTIN_FCT_NAME_IS_TRIVIALLY_DESTRUCTIBLE || name == BUILTIN_FCT_NAME_IS_HEAP {
84 1423 info = BuiltinFunctionInfo(1u, 1u, 0u, 0u);
85
2/2
✓ Branch 0 taken 1203 times.
✓ Branch 1 taken 250715 times.
253341 } else if name == BUILTIN_FCT_NAME_NEW {
86 1203 info = BuiltinFunctionInfo(1u, 1u, 0u, unlimited);
87
2/2
✓ Branch 0 taken 3041 times.
✓ Branch 1 taken 247674 times.
251918 } else if name == BUILTIN_FCT_NAME_PLACEMENT_NEW {
88 3041 info = BuiltinFunctionInfo(1u, 1u, 1u, unlimited);
89
12/12
✓ Branch 0 taken 247659 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 247629 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 247599 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 242829 times.
✓ Branch 7 taken 4770 times.
✓ Branch 8 taken 4907 times.
✓ Branch 9 taken 237922 times.
✓ Branch 10 taken 9752 times.
✓ Branch 11 taken 237922 times.
250715 } else if name == BUILTIN_FCT_NAME_SOURCE_FILE || name == BUILTIN_FCT_NAME_SOURCE_LINE || name == BUILTIN_FCT_NAME_SOURCE_COLUMN || name == BUILTIN_FCT_NAME_ERR_TRACE_BUFFER || name == BUILTIN_FCT_NAME_STDERR || name == BUILTIN_FCT_NAME_FRAME_ADDRESS {
90 9752 info = BuiltinFunctionInfo(0u, 0u, 0u, 0u);
91 9752 } else {
92 237922 return false;
93 41507 }
94 55255 return true;
95 }
96
97 /**
98 * Check if the given name is the name of a builtin function
99 *
100 * @param name Function name
101 * @return Builtin function or not
102 */
103 159108 public f<bool> isBuiltinFunction(const String& name) {
104 159108 BuiltinFunctionInfo info;
105 159108 return lookupBuiltinFunction(name, info);
106 }
107