|
@@ -1074,6 +1074,505 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_) {
|
|
|
+ ast_node(ce, CallExpr, call);
|
|
|
+ ast_node(bd, BasicDirective, ce->proc);
|
|
|
+ String builtin_name = bd->name.string;
|
|
|
+
|
|
|
+ String base_dir = dir_from_path(get_file_path_string(call->file_id));
|
|
|
+
|
|
|
+ BlockingMutex *ignore_mutex = nullptr;
|
|
|
+ String path = {};
|
|
|
+ bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
|
|
|
+ gb_unused(ok);
|
|
|
+
|
|
|
+
|
|
|
+ MUTEX_GUARD(&c->info->load_file_mutex);
|
|
|
+
|
|
|
+ gbFileError file_error = gbFileError_None;
|
|
|
+ String data = {};
|
|
|
+
|
|
|
+ LoadFileCache **cache_ptr = string_map_get(&c->info->load_file_cache, path);
|
|
|
+ LoadFileCache *cache = cache_ptr ? *cache_ptr : nullptr;
|
|
|
+ if (cache) {
|
|
|
+ file_error = cache->file_error;
|
|
|
+ data = cache->data;
|
|
|
+ }
|
|
|
+ defer ({
|
|
|
+ if (cache == nullptr) {
|
|
|
+ LoadFileCache *new_cache = gb_alloc_item(permanent_allocator(), LoadFileCache);
|
|
|
+ new_cache->path = path;
|
|
|
+ new_cache->data = data;
|
|
|
+ new_cache->file_error = file_error;
|
|
|
+ string_map_init(&new_cache->hashes, heap_allocator(), 32);
|
|
|
+ string_map_set(&c->info->load_file_cache, path, new_cache);
|
|
|
+ if (cache_) *cache_ = new_cache;
|
|
|
+ } else {
|
|
|
+ cache->data = data;
|
|
|
+ cache->file_error = file_error;
|
|
|
+ if (cache_) *cache_ = cache;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ char *c_str = alloc_cstring(heap_allocator(), path);
|
|
|
+ defer (gb_free(heap_allocator(), c_str));
|
|
|
+
|
|
|
+ gbFile f = {};
|
|
|
+ if (cache == nullptr) {
|
|
|
+ file_error = gb_file_open(&f, c_str);
|
|
|
+ }
|
|
|
+ defer (gb_file_close(&f));
|
|
|
+
|
|
|
+ switch (file_error) {
|
|
|
+ default:
|
|
|
+ case gbFileError_Invalid:
|
|
|
+ if (err_on_not_found) {
|
|
|
+ error(ce->proc, "Failed to `#%.*s` file: %s; invalid file or cannot be found", LIT(builtin_name), c_str);
|
|
|
+ }
|
|
|
+ call->state_flags |= StateFlag_DirectiveWasFalse;
|
|
|
+ return false;
|
|
|
+ case gbFileError_NotExists:
|
|
|
+ if (err_on_not_found) {
|
|
|
+ error(ce->proc, "Failed to `#%.*s` file: %s; file cannot be found", LIT(builtin_name), c_str);
|
|
|
+ }
|
|
|
+ call->state_flags |= StateFlag_DirectiveWasFalse;
|
|
|
+ return false;
|
|
|
+ case gbFileError_Permission:
|
|
|
+ if (err_on_not_found) {
|
|
|
+ error(ce->proc, "Failed to `#%.*s` file: %s; file permissions problem", LIT(builtin_name), c_str);
|
|
|
+ }
|
|
|
+ call->state_flags |= StateFlag_DirectiveWasFalse;
|
|
|
+ return false;
|
|
|
+ case gbFileError_None:
|
|
|
+ // Okay
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (cache == nullptr) {
|
|
|
+ isize file_size = cast(isize)gb_file_size(&f);
|
|
|
+ if (file_size > 0) {
|
|
|
+ u8 *ptr = cast(u8 *)gb_alloc(permanent_allocator(), file_size+1);
|
|
|
+ gb_file_read_at(&f, ptr, file_size, 0);
|
|
|
+ ptr[file_size] = '\0';
|
|
|
+ data.text = ptr;
|
|
|
+ data.len = file_size;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool is_valid_type_for_load(Type *type) {
|
|
|
+ if (type == t_invalid) {
|
|
|
+ return false;
|
|
|
+ } else if (is_type_string(type)) {
|
|
|
+ return true;
|
|
|
+ } else if (is_type_slice(type) /*|| is_type_array(type) || is_type_enumerated_array(type)*/) {
|
|
|
+ Type *elem = nullptr;
|
|
|
+ Type *bt = base_type(type);
|
|
|
+ if (bt->kind == Type_Slice) {
|
|
|
+ elem = bt->Slice.elem;
|
|
|
+ } else if (bt->kind == Type_Array) {
|
|
|
+ elem = bt->Array.elem;
|
|
|
+ } else if (bt->kind == Type_EnumeratedArray) {
|
|
|
+ elem = bt->EnumeratedArray.elem;
|
|
|
+ }
|
|
|
+ GB_ASSERT(elem != nullptr);
|
|
|
+ return is_type_load_safe(elem);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) {
|
|
|
+ ast_node(ce, CallExpr, call);
|
|
|
+ ast_node(bd, BasicDirective, ce->proc);
|
|
|
+ String name = bd->name.string;
|
|
|
+ GB_ASSERT(name == "load");
|
|
|
+
|
|
|
+ if (ce->args.count != 1 && ce->args.count != 2) {
|
|
|
+ if (ce->args.count == 0) {
|
|
|
+ error(ce->close, "'#%.*s' expects 1 or 2 arguments, got 0", LIT(name));
|
|
|
+ } else {
|
|
|
+ error(ce->args[0], "'#%.*s' expects 1 or 2 arguments, got %td", LIT(name), ce->args.count);
|
|
|
+ }
|
|
|
+
|
|
|
+ return LoadDirective_Error;
|
|
|
+ }
|
|
|
+
|
|
|
+ Ast *arg = ce->args[0];
|
|
|
+ Operand o = {};
|
|
|
+ check_expr(c, &o, arg);
|
|
|
+ if (o.mode != Addressing_Constant) {
|
|
|
+ error(arg, "'#%.*s' expected a constant string argument", LIT(name));
|
|
|
+ return LoadDirective_Error;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_type_string(o.type)) {
|
|
|
+ gbString str = type_to_string(o.type);
|
|
|
+ error(arg, "'#%.*s' expected a constant string, got %s", LIT(name), str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return LoadDirective_Error;
|
|
|
+ }
|
|
|
+
|
|
|
+ GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
+
|
|
|
+ operand->type = t_u8_slice;
|
|
|
+ if (ce->args.count == 1) {
|
|
|
+ if (type_hint && is_valid_type_for_load(type_hint)) {
|
|
|
+ operand->type = type_hint;
|
|
|
+ }
|
|
|
+ } else if (ce->args.count == 2) {
|
|
|
+ Ast *arg_type = ce->args[1];
|
|
|
+ Type *type = check_type(c, arg_type);
|
|
|
+ if (type != nullptr) {
|
|
|
+ if (is_valid_type_for_load(type)) {
|
|
|
+ operand->type = type;
|
|
|
+ } else {
|
|
|
+ gbString type_str = type_to_string(type);
|
|
|
+ error(arg_type, "'#%.*s' invalid type, expected a string, or slice of simple types, got %s", LIT(name), type_str);
|
|
|
+ gb_string_free(type_str);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ GB_PANIC("unreachable");
|
|
|
+ }
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+
|
|
|
+ LoadFileCache *cache = nullptr;
|
|
|
+ if (cache_load_file_directive(c, call, o.value.value_string, err_on_not_found, &cache)) {
|
|
|
+ operand->value = exact_value_string(cache->data);
|
|
|
+ return LoadDirective_Success;
|
|
|
+ }
|
|
|
+ return LoadDirective_NotFound;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) {
|
|
|
+ ast_node(ce, CallExpr, call);
|
|
|
+ ast_node(bd, BasicDirective, ce->proc);
|
|
|
+ String name = bd->name.string;
|
|
|
+ if (name == "location") {
|
|
|
+ if (ce->args.count > 1) {
|
|
|
+ error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
|
|
|
+ }
|
|
|
+ if (ce->args.count > 0) {
|
|
|
+ Ast *arg = ce->args[0];
|
|
|
+ Entity *e = nullptr;
|
|
|
+ Operand o = {};
|
|
|
+ if (arg->kind == Ast_Ident) {
|
|
|
+ e = check_ident(c, &o, arg, nullptr, nullptr, true);
|
|
|
+ } else if (arg->kind == Ast_SelectorExpr) {
|
|
|
+ e = check_selector(c, &o, arg, nullptr);
|
|
|
+ }
|
|
|
+ if (e == nullptr) {
|
|
|
+ error(ce->args[0], "'#location' expected a valid entity name");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ operand->type = t_source_code_location;
|
|
|
+ operand->mode = Addressing_Value;
|
|
|
+ } else if (name == "load") {
|
|
|
+ return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
|
|
|
+ } else if (name == "load_hash") {
|
|
|
+ if (ce->args.count != 2) {
|
|
|
+ if (ce->args.count == 0) {
|
|
|
+ error(ce->close, "'#load_hash' expects 2 argument, got 0");
|
|
|
+ } else {
|
|
|
+ error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Ast *arg0 = ce->args[0];
|
|
|
+ Ast *arg1 = ce->args[1];
|
|
|
+ Operand o = {};
|
|
|
+ check_expr(c, &o, arg0);
|
|
|
+ if (o.mode != Addressing_Constant) {
|
|
|
+ error(arg0, "'#load_hash' expected a constant string argument");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_type_string(o.type)) {
|
|
|
+ gbString str = type_to_string(o.type);
|
|
|
+ error(arg0, "'#load_hash' expected a constant string, got %s", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Operand o_hash = {};
|
|
|
+ check_expr(c, &o_hash, arg1);
|
|
|
+ if (o_hash.mode != Addressing_Constant) {
|
|
|
+ error(arg1, "'#load_hash' expected a constant string argument");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_type_string(o_hash.type)) {
|
|
|
+ gbString str = type_to_string(o.type);
|
|
|
+ error(arg1, "'#load_hash' expected a constant string, got %s", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ gbAllocator a = heap_allocator();
|
|
|
+
|
|
|
+ GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
+ GB_ASSERT(o_hash.value.kind == ExactValue_String);
|
|
|
+
|
|
|
+ String original_string = o.value.value_string;
|
|
|
+ String hash_kind = o_hash.value.value_string;
|
|
|
+
|
|
|
+ String supported_hashes[] = {
|
|
|
+ str_lit("adler32"),
|
|
|
+ str_lit("crc32"),
|
|
|
+ str_lit("crc64"),
|
|
|
+ str_lit("fnv32"),
|
|
|
+ str_lit("fnv64"),
|
|
|
+ str_lit("fnv32a"),
|
|
|
+ str_lit("fnv64a"),
|
|
|
+ str_lit("murmur32"),
|
|
|
+ str_lit("murmur64"),
|
|
|
+ };
|
|
|
+
|
|
|
+ bool hash_found = false;
|
|
|
+ for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
|
|
|
+ if (supported_hashes[i] == hash_kind) {
|
|
|
+ hash_found = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!hash_found) {
|
|
|
+ ERROR_BLOCK();
|
|
|
+ error(ce->proc, "Invalid hash kind passed to `#load_hash`, got: %.*s", LIT(hash_kind));
|
|
|
+ error_line("\tAvailable hash kinds:\n");
|
|
|
+ for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
|
|
|
+ error_line("\t%.*s\n", LIT(supported_hashes[i]));
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ LoadFileCache *cache = nullptr;
|
|
|
+ if (cache_load_file_directive(c, call, original_string, true, &cache)) {
|
|
|
+ MUTEX_GUARD(&c->info->load_file_mutex);
|
|
|
+ // TODO(bill): make these procedures fast :P
|
|
|
+ u64 hash_value = 0;
|
|
|
+ u64 *hash_value_ptr = string_map_get(&cache->hashes, hash_kind);
|
|
|
+ if (hash_value_ptr) {
|
|
|
+ hash_value = *hash_value_ptr;
|
|
|
+ } else {
|
|
|
+ u8 *data = cache->data.text;
|
|
|
+ isize file_size = cache->data.len;
|
|
|
+ if (hash_kind == "adler32") {
|
|
|
+ hash_value = gb_adler32(data, file_size);
|
|
|
+ } else if (hash_kind == "crc32") {
|
|
|
+ hash_value = gb_crc32(data, file_size);
|
|
|
+ } else if (hash_kind == "crc64") {
|
|
|
+ hash_value = gb_crc64(data, file_size);
|
|
|
+ } else if (hash_kind == "fnv32") {
|
|
|
+ hash_value = gb_fnv32(data, file_size);
|
|
|
+ } else if (hash_kind == "fnv64") {
|
|
|
+ hash_value = gb_fnv64(data, file_size);
|
|
|
+ } else if (hash_kind == "fnv32a") {
|
|
|
+ hash_value = fnv32a(data, file_size);
|
|
|
+ } else if (hash_kind == "fnv64a") {
|
|
|
+ hash_value = fnv64a(data, file_size);
|
|
|
+ } else if (hash_kind == "murmur32") {
|
|
|
+ hash_value = gb_murmur32(data, file_size);
|
|
|
+ } else if (hash_kind == "murmur64") {
|
|
|
+ hash_value = gb_murmur64(data, file_size);
|
|
|
+ } else {
|
|
|
+ compiler_error("unhandled hash kind: %.*s", LIT(hash_kind));
|
|
|
+ }
|
|
|
+ string_map_set(&cache->hashes, hash_kind, hash_value);
|
|
|
+ }
|
|
|
+
|
|
|
+ operand->type = t_untyped_integer;
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+ operand->value = exact_value_u64(hash_value);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ } else if (name == "load_or") {
|
|
|
+ warning(call, "'#load_or' is deprecated in favour of '#load(path) or_else default'");
|
|
|
+
|
|
|
+ if (ce->args.count != 2) {
|
|
|
+ if (ce->args.count == 0) {
|
|
|
+ error(ce->close, "'#load_or' expects 2 arguments, got 0");
|
|
|
+ } else {
|
|
|
+ error(ce->args[0], "'#load_or' expects 2 arguments, got %td", ce->args.count);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Ast *arg = ce->args[0];
|
|
|
+ Operand o = {};
|
|
|
+ check_expr(c, &o, arg);
|
|
|
+ if (o.mode != Addressing_Constant) {
|
|
|
+ error(arg, "'#load_or' expected a constant string argument");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_type_string(o.type)) {
|
|
|
+ gbString str = type_to_string(o.type);
|
|
|
+ error(arg, "'#load_or' expected a constant string, got %s", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Ast *default_arg = ce->args[1];
|
|
|
+ Operand default_op = {};
|
|
|
+ check_expr_with_type_hint(c, &default_op, default_arg, t_u8_slice);
|
|
|
+ if (default_op.mode != Addressing_Constant) {
|
|
|
+ error(arg, "'#load_or' expected a constant '[]byte' argument");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!are_types_identical(base_type(default_op.type), t_u8_slice)) {
|
|
|
+ gbString str = type_to_string(default_op.type);
|
|
|
+ error(arg, "'#load_or' expected a constant '[]byte', got %s", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
+ String original_string = o.value.value_string;
|
|
|
+
|
|
|
+ operand->type = t_u8_slice;
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+ LoadFileCache *cache = nullptr;
|
|
|
+ if (cache_load_file_directive(c, call, original_string, false, &cache)) {
|
|
|
+ operand->value = exact_value_string(cache->data);
|
|
|
+ } else {
|
|
|
+ operand->value = default_op.value;
|
|
|
+ }
|
|
|
+ } else if (name == "assert") {
|
|
|
+ if (ce->args.count != 1 && ce->args.count != 2) {
|
|
|
+ error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
|
|
|
+ gbString str = expr_to_string(ce->args[0]);
|
|
|
+ error(call, "'%s' is not a constant boolean", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (ce->args.count == 2) {
|
|
|
+ Ast *arg = unparen_expr(ce->args[1]);
|
|
|
+ if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) {
|
|
|
+ gbString str = expr_to_string(arg);
|
|
|
+ error(call, "'%s' is not a constant string", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!operand->value.value_bool) {
|
|
|
+ gbString arg1 = expr_to_string(ce->args[0]);
|
|
|
+ gbString arg2 = {};
|
|
|
+
|
|
|
+ if (ce->args.count == 1) {
|
|
|
+ error(call, "Compile time assertion: %s", arg1);
|
|
|
+ } else {
|
|
|
+ arg2 = expr_to_string(ce->args[1]);
|
|
|
+ error(call, "Compile time assertion: %s (%s)", arg1, arg2);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (c->proc_name != "") {
|
|
|
+ gbString str = type_to_string(c->curr_proc_sig);
|
|
|
+ error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
|
|
|
+ gb_string_free(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ gb_string_free(arg1);
|
|
|
+ if (ce->args.count == 2) {
|
|
|
+ gb_string_free(arg2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ operand->type = t_untyped_bool;
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+ } else if (name == "panic") {
|
|
|
+ if (ce->args.count != 1) {
|
|
|
+ error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
|
|
|
+ gbString str = expr_to_string(ce->args[0]);
|
|
|
+ error(call, "'%s' is not a constant string", str);
|
|
|
+ gb_string_free(str);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
|
|
|
+ if (c->proc_name != "") {
|
|
|
+ gbString str = type_to_string(c->curr_proc_sig);
|
|
|
+ error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
|
|
|
+ gb_string_free(str);
|
|
|
+ }
|
|
|
+ operand->type = t_invalid;
|
|
|
+ operand->mode = Addressing_NoValue;
|
|
|
+ } else if (name == "defined") {
|
|
|
+ if (ce->args.count != 1) {
|
|
|
+ error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Ast *arg = unparen_expr(ce->args[0]);
|
|
|
+ if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
|
|
|
+ error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (c->curr_proc_decl == nullptr) {
|
|
|
+ error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool is_defined = check_identifier_exists(c->scope, arg);
|
|
|
+ gb_unused(is_defined);
|
|
|
+ operand->type = t_untyped_bool;
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+ operand->value = exact_value_bool(false);
|
|
|
+
|
|
|
+ } else if (name == "config") {
|
|
|
+ if (ce->args.count != 2) {
|
|
|
+ error(call, "'#config' expects 2 argument, got %td", ce->args.count);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Ast *arg = unparen_expr(ce->args[0]);
|
|
|
+ if (arg == nullptr || arg->kind != Ast_Ident) {
|
|
|
+ error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ Ast *def_arg = unparen_expr(ce->args[1]);
|
|
|
+
|
|
|
+ Operand def = {};
|
|
|
+ check_expr(c, &def, def_arg);
|
|
|
+ if (def.mode != Addressing_Constant) {
|
|
|
+ error(def_arg, "'#config' default value must be a constant");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String name = arg->Ident.token.string;
|
|
|
+
|
|
|
+
|
|
|
+ operand->type = def.type;
|
|
|
+ operand->mode = def.mode;
|
|
|
+ operand->value = def.value;
|
|
|
+
|
|
|
+ Entity *found = scope_lookup_current(config_pkg->scope, name);
|
|
|
+ if (found != nullptr) {
|
|
|
+ if (found->kind != Entity_Constant) {
|
|
|
+ error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
|
|
|
+ } else {
|
|
|
+ operand->type = found->type;
|
|
|
+ operand->mode = Addressing_Constant;
|
|
|
+ operand->value = found->Constant.value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ error(call, "Unknown directive call: #%.*s", LIT(name));
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
|
|
|
bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
|
|
|
ast_node(ce, CallExpr, call);
|
|
@@ -1186,458 +1685,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
|
|
|
mpmc_enqueue(&c->info->intrinsics_entry_point_usage, call);
|
|
|
break;
|
|
|
|
|
|
- case BuiltinProc_DIRECTIVE: {
|
|
|
- ast_node(bd, BasicDirective, ce->proc);
|
|
|
- String name = bd->name.string;
|
|
|
- if (name == "location") {
|
|
|
- if (ce->args.count > 1) {
|
|
|
- error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
|
|
|
- }
|
|
|
- if (ce->args.count > 0) {
|
|
|
- Ast *arg = ce->args[0];
|
|
|
- Entity *e = nullptr;
|
|
|
- Operand o = {};
|
|
|
- if (arg->kind == Ast_Ident) {
|
|
|
- e = check_ident(c, &o, arg, nullptr, nullptr, true);
|
|
|
- } else if (arg->kind == Ast_SelectorExpr) {
|
|
|
- e = check_selector(c, &o, arg, nullptr);
|
|
|
- }
|
|
|
- if (e == nullptr) {
|
|
|
- error(ce->args[0], "'#location' expected a valid entity name");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- operand->type = t_source_code_location;
|
|
|
- operand->mode = Addressing_Value;
|
|
|
- } else if (name == "load") {
|
|
|
- if (ce->args.count != 1) {
|
|
|
- if (ce->args.count == 0) {
|
|
|
- error(ce->close, "'#load' expects 1 argument, got 0");
|
|
|
- } else {
|
|
|
- error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
|
|
|
- }
|
|
|
-
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Ast *arg = ce->args[0];
|
|
|
- Operand o = {};
|
|
|
- check_expr(c, &o, arg);
|
|
|
- if (o.mode != Addressing_Constant) {
|
|
|
- error(arg, "'#load' expected a constant string argument");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (!is_type_string(o.type)) {
|
|
|
- gbString str = type_to_string(o.type);
|
|
|
- error(arg, "'#load' expected a constant string, got %s", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- gbAllocator a = heap_allocator();
|
|
|
-
|
|
|
- GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
- String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
|
|
|
- String original_string = o.value.value_string;
|
|
|
-
|
|
|
-
|
|
|
- BlockingMutex *ignore_mutex = nullptr;
|
|
|
- String path = {};
|
|
|
- bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
|
|
|
- gb_unused(ok);
|
|
|
-
|
|
|
- char *c_str = alloc_cstring(a, path);
|
|
|
- defer (gb_free(a, c_str));
|
|
|
-
|
|
|
-
|
|
|
- gbFile f = {};
|
|
|
- gbFileError file_err = gb_file_open(&f, c_str);
|
|
|
- defer (gb_file_close(&f));
|
|
|
-
|
|
|
- switch (file_err) {
|
|
|
- default:
|
|
|
- case gbFileError_Invalid:
|
|
|
- error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_NotExists:
|
|
|
- error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_Permission:
|
|
|
- error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_None:
|
|
|
- // Okay
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- String result = {};
|
|
|
- isize file_size = cast(isize)gb_file_size(&f);
|
|
|
- if (file_size > 0) {
|
|
|
- u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
|
|
|
- gb_file_read_at(&f, data, file_size, 0);
|
|
|
- data[file_size] = '\0';
|
|
|
- result.text = data;
|
|
|
- result.len = file_size;
|
|
|
- }
|
|
|
-
|
|
|
- operand->type = t_u8_slice;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- operand->value = exact_value_string(result);
|
|
|
-
|
|
|
- } else if (name == "load_hash") {
|
|
|
- if (ce->args.count != 2) {
|
|
|
- if (ce->args.count == 0) {
|
|
|
- error(ce->close, "'#load_hash' expects 2 argument, got 0");
|
|
|
- } else {
|
|
|
- error(ce->args[0], "'#load_hash' expects 2 argument, got %td", ce->args.count);
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Ast *arg0 = ce->args[0];
|
|
|
- Ast *arg1 = ce->args[1];
|
|
|
- Operand o = {};
|
|
|
- check_expr(c, &o, arg0);
|
|
|
- if (o.mode != Addressing_Constant) {
|
|
|
- error(arg0, "'#load_hash' expected a constant string argument");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (!is_type_string(o.type)) {
|
|
|
- gbString str = type_to_string(o.type);
|
|
|
- error(arg0, "'#load_hash' expected a constant string, got %s", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Operand o_hash = {};
|
|
|
- check_expr(c, &o_hash, arg1);
|
|
|
- if (o_hash.mode != Addressing_Constant) {
|
|
|
- error(arg1, "'#load_hash' expected a constant string argument");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (!is_type_string(o_hash.type)) {
|
|
|
- gbString str = type_to_string(o.type);
|
|
|
- error(arg1, "'#load_hash' expected a constant string, got %s", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- gbAllocator a = heap_allocator();
|
|
|
-
|
|
|
- GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
- GB_ASSERT(o_hash.value.kind == ExactValue_String);
|
|
|
-
|
|
|
- String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
|
|
|
- String original_string = o.value.value_string;
|
|
|
- String hash_kind = o_hash.value.value_string;
|
|
|
-
|
|
|
- String supported_hashes[] = {
|
|
|
- str_lit("adler32"),
|
|
|
- str_lit("crc32"),
|
|
|
- str_lit("crc64"),
|
|
|
- str_lit("fnv32"),
|
|
|
- str_lit("fnv64"),
|
|
|
- str_lit("fnv32a"),
|
|
|
- str_lit("fnv64a"),
|
|
|
- str_lit("murmur32"),
|
|
|
- str_lit("murmur64"),
|
|
|
- };
|
|
|
-
|
|
|
- bool hash_found = false;
|
|
|
- for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
|
|
|
- if (supported_hashes[i] == hash_kind) {
|
|
|
- hash_found = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (!hash_found) {
|
|
|
- ERROR_BLOCK();
|
|
|
- error(ce->proc, "Invalid hash kind passed to `#load_hash`, got: %.*s", LIT(hash_kind));
|
|
|
- error_line("\tAvailable hash kinds:\n");
|
|
|
- for (isize i = 0; i < gb_count_of(supported_hashes); i++) {
|
|
|
- error_line("\t%.*s\n", LIT(supported_hashes[i]));
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- BlockingMutex *ignore_mutex = nullptr;
|
|
|
- String path = {};
|
|
|
- bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
|
|
|
- gb_unused(ok);
|
|
|
-
|
|
|
- char *c_str = alloc_cstring(a, path);
|
|
|
- defer (gb_free(a, c_str));
|
|
|
-
|
|
|
-
|
|
|
- gbFile f = {};
|
|
|
- gbFileError file_err = gb_file_open(&f, c_str);
|
|
|
- defer (gb_file_close(&f));
|
|
|
-
|
|
|
- switch (file_err) {
|
|
|
- default:
|
|
|
- case gbFileError_Invalid:
|
|
|
- error(ce->proc, "Failed to `#load_hash` file: %s; invalid file or cannot be found", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_NotExists:
|
|
|
- error(ce->proc, "Failed to `#load_hash` file: %s; file cannot be found", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_Permission:
|
|
|
- error(ce->proc, "Failed to `#load_hash` file: %s; file permissions problem", c_str);
|
|
|
- return false;
|
|
|
- case gbFileError_None:
|
|
|
- // Okay
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- // TODO(bill): make these procedures fast :P
|
|
|
-
|
|
|
- u64 hash_value = 0;
|
|
|
- String result = {};
|
|
|
- isize file_size = cast(isize)gb_file_size(&f);
|
|
|
- if (file_size > 0) {
|
|
|
- u8 *data = cast(u8 *)gb_alloc(a, file_size);
|
|
|
- gb_file_read_at(&f, data, file_size, 0);
|
|
|
- if (hash_kind == "adler32") {
|
|
|
- hash_value = gb_adler32(data, file_size);
|
|
|
- } else if (hash_kind == "crc32") {
|
|
|
- hash_value = gb_crc32(data, file_size);
|
|
|
- } else if (hash_kind == "crc64") {
|
|
|
- hash_value = gb_crc64(data, file_size);
|
|
|
- } else if (hash_kind == "fnv32") {
|
|
|
- hash_value = gb_fnv32(data, file_size);
|
|
|
- } else if (hash_kind == "fnv64") {
|
|
|
- hash_value = gb_fnv64(data, file_size);
|
|
|
- } else if (hash_kind == "fnv32a") {
|
|
|
- hash_value = fnv32a(data, file_size);
|
|
|
- } else if (hash_kind == "fnv64a") {
|
|
|
- hash_value = fnv64a(data, file_size);
|
|
|
- } else if (hash_kind == "murmur32") {
|
|
|
- hash_value = gb_murmur32(data, file_size);
|
|
|
- } else if (hash_kind == "murmur64") {
|
|
|
- hash_value = gb_murmur64(data, file_size);
|
|
|
- } else {
|
|
|
- compiler_error("unhandled hash kind: %.*s", LIT(hash_kind));
|
|
|
- }
|
|
|
- gb_free(a, data);
|
|
|
- }
|
|
|
-
|
|
|
- operand->type = t_untyped_integer;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- operand->value = exact_value_u64(hash_value);
|
|
|
-
|
|
|
- } else if (name == "load_or") {
|
|
|
- if (ce->args.count != 2) {
|
|
|
- if (ce->args.count == 0) {
|
|
|
- error(ce->close, "'#load_or' expects 2 arguments, got 0");
|
|
|
- } else {
|
|
|
- error(ce->args[0], "'#load_or' expects 2 arguments, got %td", ce->args.count);
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Ast *arg = ce->args[0];
|
|
|
- Operand o = {};
|
|
|
- check_expr(c, &o, arg);
|
|
|
- if (o.mode != Addressing_Constant) {
|
|
|
- error(arg, "'#load_or' expected a constant string argument");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (!is_type_string(o.type)) {
|
|
|
- gbString str = type_to_string(o.type);
|
|
|
- error(arg, "'#load_or' expected a constant string, got %s", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Ast *default_arg = ce->args[1];
|
|
|
- Operand default_op = {};
|
|
|
- check_expr_with_type_hint(c, &default_op, default_arg, t_u8_slice);
|
|
|
- if (default_op.mode != Addressing_Constant) {
|
|
|
- error(arg, "'#load_or' expected a constant '[]byte' argument");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (!are_types_identical(base_type(default_op.type), t_u8_slice)) {
|
|
|
- gbString str = type_to_string(default_op.type);
|
|
|
- error(arg, "'#load_or' expected a constant '[]byte', got %s", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- gbAllocator a = heap_allocator();
|
|
|
-
|
|
|
- GB_ASSERT(o.value.kind == ExactValue_String);
|
|
|
- String base_dir = dir_from_path(get_file_path_string(bd->token.pos.file_id));
|
|
|
- String original_string = o.value.value_string;
|
|
|
-
|
|
|
-
|
|
|
- BlockingMutex *ignore_mutex = nullptr;
|
|
|
- String path = {};
|
|
|
- bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
|
|
|
- gb_unused(ok);
|
|
|
-
|
|
|
- char *c_str = alloc_cstring(a, path);
|
|
|
- defer (gb_free(a, c_str));
|
|
|
-
|
|
|
-
|
|
|
- gbFile f = {};
|
|
|
- gbFileError file_err = gb_file_open(&f, c_str);
|
|
|
- defer (gb_file_close(&f));
|
|
|
-
|
|
|
- operand->type = t_u8_slice;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- if (file_err == gbFileError_None) {
|
|
|
- String result = {};
|
|
|
- isize file_size = cast(isize)gb_file_size(&f);
|
|
|
- if (file_size > 0) {
|
|
|
- u8 *data = cast(u8 *)gb_alloc(a, file_size+1);
|
|
|
- gb_file_read_at(&f, data, file_size, 0);
|
|
|
- data[file_size] = '\0';
|
|
|
- result.text = data;
|
|
|
- result.len = file_size;
|
|
|
- }
|
|
|
-
|
|
|
- operand->value = exact_value_string(result);
|
|
|
- } else {
|
|
|
- operand->value = default_op.value;
|
|
|
- }
|
|
|
-
|
|
|
- } else if (name == "assert") {
|
|
|
- if (ce->args.count != 1 && ce->args.count != 2) {
|
|
|
- error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count);
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!is_type_boolean(operand->type) || operand->mode != Addressing_Constant) {
|
|
|
- gbString str = expr_to_string(ce->args[0]);
|
|
|
- error(call, "'%s' is not a constant boolean", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (ce->args.count == 2) {
|
|
|
- Ast *arg = unparen_expr(ce->args[1]);
|
|
|
- if (arg == nullptr || arg->kind != Ast_BasicLit || arg->BasicLit.token.kind != Token_String) {
|
|
|
- gbString str = expr_to_string(arg);
|
|
|
- error(call, "'%s' is not a constant string", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (!operand->value.value_bool) {
|
|
|
- gbString arg1 = expr_to_string(ce->args[0]);
|
|
|
- gbString arg2 = {};
|
|
|
-
|
|
|
- if (ce->args.count == 1) {
|
|
|
- error(call, "Compile time assertion: %s", arg1);
|
|
|
- } else {
|
|
|
- arg2 = expr_to_string(ce->args[1]);
|
|
|
- error(call, "Compile time assertion: %s (%s)", arg1, arg2);
|
|
|
- }
|
|
|
-
|
|
|
- if (c->proc_name != "") {
|
|
|
- gbString str = type_to_string(c->curr_proc_sig);
|
|
|
- error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
|
|
|
- gb_string_free(str);
|
|
|
- }
|
|
|
-
|
|
|
- gb_string_free(arg1);
|
|
|
- if (ce->args.count == 2) {
|
|
|
- gb_string_free(arg2);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- operand->type = t_untyped_bool;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- } else if (name == "panic") {
|
|
|
- if (ce->args.count != 1) {
|
|
|
- error(call, "'#panic' expects 1 argument, got %td", ce->args.count);
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!is_type_string(operand->type) && operand->mode != Addressing_Constant) {
|
|
|
- gbString str = expr_to_string(ce->args[0]);
|
|
|
- error(call, "'%s' is not a constant string", str);
|
|
|
- gb_string_free(str);
|
|
|
- return false;
|
|
|
- }
|
|
|
- error(call, "Compile time panic: %.*s", LIT(operand->value.value_string));
|
|
|
- if (c->proc_name != "") {
|
|
|
- gbString str = type_to_string(c->curr_proc_sig);
|
|
|
- error_line("\tCalled within '%.*s' :: %s\n", LIT(c->proc_name), str);
|
|
|
- gb_string_free(str);
|
|
|
- }
|
|
|
- operand->type = t_invalid;
|
|
|
- operand->mode = Addressing_NoValue;
|
|
|
- } else if (name == "defined") {
|
|
|
- if (ce->args.count != 1) {
|
|
|
- error(call, "'#defined' expects 1 argument, got %td", ce->args.count);
|
|
|
- return false;
|
|
|
- }
|
|
|
- Ast *arg = unparen_expr(ce->args[0]);
|
|
|
- if (arg == nullptr || (arg->kind != Ast_Ident && arg->kind != Ast_SelectorExpr)) {
|
|
|
- error(call, "'#defined' expects an identifier or selector expression, got %.*s", LIT(ast_strings[arg->kind]));
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (c->curr_proc_decl == nullptr) {
|
|
|
- error(call, "'#defined' is only allowed within a procedure, prefer the replacement '#config(NAME, default_value)'");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- bool is_defined = check_identifier_exists(c->scope, arg);
|
|
|
- gb_unused(is_defined);
|
|
|
- operand->type = t_untyped_bool;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- operand->value = exact_value_bool(false);
|
|
|
-
|
|
|
- } else if (name == "config") {
|
|
|
- if (ce->args.count != 2) {
|
|
|
- error(call, "'#config' expects 2 argument, got %td", ce->args.count);
|
|
|
- return false;
|
|
|
- }
|
|
|
- Ast *arg = unparen_expr(ce->args[0]);
|
|
|
- if (arg == nullptr || arg->kind != Ast_Ident) {
|
|
|
- error(call, "'#config' expects an identifier, got %.*s", LIT(ast_strings[arg->kind]));
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- Ast *def_arg = unparen_expr(ce->args[1]);
|
|
|
-
|
|
|
- Operand def = {};
|
|
|
- check_expr(c, &def, def_arg);
|
|
|
- if (def.mode != Addressing_Constant) {
|
|
|
- error(def_arg, "'#config' default value must be a constant");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- String name = arg->Ident.token.string;
|
|
|
-
|
|
|
-
|
|
|
- operand->type = def.type;
|
|
|
- operand->mode = def.mode;
|
|
|
- operand->value = def.value;
|
|
|
-
|
|
|
- Entity *found = scope_lookup_current(config_pkg->scope, name);
|
|
|
- if (found != nullptr) {
|
|
|
- if (found->kind != Entity_Constant) {
|
|
|
- error(arg, "'#config' entity '%.*s' found but expected a constant", LIT(name));
|
|
|
- } else {
|
|
|
- operand->type = found->type;
|
|
|
- operand->mode = Addressing_Constant;
|
|
|
- operand->value = found->Constant.value;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- error(call, "Unknown directive call: #%.*s", LIT(name));
|
|
|
- }
|
|
|
-
|
|
|
- break;
|
|
|
- }
|
|
|
+ case BuiltinProc_DIRECTIVE:
|
|
|
+ return check_builtin_procedure_directive(c, operand, call, type_hint);
|
|
|
|
|
|
case BuiltinProc_len:
|
|
|
check_expr_or_type(c, operand, ce->args[0]);
|