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 | 293177 | if name == BUILTIN_FCT_NAME_PRINTF { | |
| 65 | 13748 | info = BuiltinFunctionInfo(0u, 0u, 1u, unlimited); | |
| 66 | 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 | 279429 | } else if name == BUILTIN_FCT_NAME_OFFSETOF { | |
| 69 | 201 | info = BuiltinFunctionInfo(0u, 0u, 2u, 2u); | |
| 70 | 274700 | } else if name == BUILTIN_FCT_NAME_LEN { | |
| 71 | 988 | info = BuiltinFunctionInfo(0u, 0u, 1u, 1u); | |
| 72 | 274499 | } else if name == BUILTIN_FCT_NAME_PANIC { | |
| 73 | 19884 | info = BuiltinFunctionInfo(0u, 0u, 1u, 1u, false, true); | |
| 74 | 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 | 253627 | } else if name == BUILTIN_FCT_NAME_IS_SAME { | |
| 78 | 96 | info = BuiltinFunctionInfo(2u, unlimited, 0u, 0u); | |
| 79 | 253587 | } else if name == BUILTIN_FCT_NAME_IMPLEMENTS_INTERFACE { | |
| 80 | 24 | info = BuiltinFunctionInfo(2u, 2u, 0u, 0u); | |
| 81 | 253491 | } else if name == BUILTIN_FCT_NAME_GET_BUILD_VAR { | |
| 82 | 126 | info = BuiltinFunctionInfo(1u, 1u, 1u, 2u); | |
| 83 | 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 | 253341 | } else if name == BUILTIN_FCT_NAME_NEW { | |
| 86 | 1203 | info = BuiltinFunctionInfo(1u, 1u, 0u, unlimited); | |
| 87 | 251918 | } else if name == BUILTIN_FCT_NAME_PLACEMENT_NEW { | |
| 88 | 3041 | info = BuiltinFunctionInfo(1u, 1u, 1u, unlimited); | |
| 89 | 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 |