Merge pull request #1944 from odin-lang/load-improvements

Improvements to `#load`
This commit is contained in:
gingerBill
2022-08-15 10:27:53 +01:00
committed by GitHub
9 changed files with 694 additions and 457 deletions
+289 -240
View File
@@ -1074,119 +1074,183 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call
return false; return false;
} }
bool cache_load_file_directive(CheckerContext *c, Ast *call, String const &original_string, bool err_on_not_found, LoadFileCache **cache_) {
bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
ast_node(ce, CallExpr, call); ast_node(ce, CallExpr, call);
if (ce->inlining != ProcInlining_none) { ast_node(bd, BasicDirective, ce->proc);
error(call, "Inlining operators are not allowed on built-in procedures"); String builtin_name = bd->name.string;
}
BuiltinProc *bp = &builtin_procs[id]; String base_dir = dir_from_path(get_file_path_string(call->file_id));
{
char const *err = nullptr;
if (ce->args.count < bp->arg_count) {
err = "Too few";
} else if (ce->args.count > bp->arg_count && !bp->variadic) {
err = "Too many";
}
if (err != nullptr) { BlockingMutex *ignore_mutex = nullptr;
gbString expr = expr_to_string(ce->proc); String path = {};
error(ce->close, "%s arguments for '%s', expected %td, got %td", bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
err, expr, gb_unused(ok);
bp->arg_count, ce->args.count);
gb_string_free(expr);
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; 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;
} }
} }
switch (id) { return true;
case BuiltinProc_size_of: }
case BuiltinProc_align_of:
case BuiltinProc_offset_of:
case BuiltinProc_offset_of_by_string:
case BuiltinProc_type_info_of:
case BuiltinProc_typeid_of:
case BuiltinProc_len:
case BuiltinProc_min:
case BuiltinProc_max:
case BuiltinProc_type_is_subtype_of:
case BuiltinProc_objc_send:
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_selector:
case BuiltinProc_objc_register_class:
case BuiltinProc_atomic_type_is_lock_free:
// NOTE(bill): The first arg may be a Type, this will be checked case by case
break;
case BuiltinProc_atomic_thread_fence:
case BuiltinProc_atomic_signal_fence:
// NOTE(bill): first type will require a type hint
break;
case BuiltinProc_DIRECTIVE: { 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); ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string; String name = bd->name.string;
if (name == "defined") { GB_ASSERT(name == "load");
break;
} if (ce->args.count != 1 && ce->args.count != 2) {
if (name == "config") { if (ce->args.count == 0) {
break; error(ce->close, "'#%.*s' expects 1 or 2 arguments, got 0", LIT(name));
} } else {
/*fallthrough*/ error(ce->args[0], "'#%.*s' expects 1 or 2 arguments, got %td", LIT(name), ce->args.count);
}
default:
if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
check_expr_or_type(c, operand, ce->args[0]);
} else if (ce->args.count > 0) {
check_multi_expr(c, operand, ce->args[0]);
}
break;
} }
String const &builtin_name = builtin_procs[id].name; return LoadDirective_Error;
if (ce->args.count > 0) {
if (ce->args[0]->kind == Ast_FieldValue) {
if (id != BuiltinProc_soa_zip) {
error(call, "'field = value' calling is not allowed on built-in procedures");
return false;
}
}
} }
if (BuiltinProc__simd_begin < id && id < BuiltinProc__simd_end) { Ast *arg = ce->args[0];
bool ok = check_builtin_simd_operation(c, operand, call, id, type_hint); Operand o = {};
if (!ok) { check_expr(c, &o, arg);
operand->type = t_invalid; if (o.mode != Addressing_Constant) {
} error(arg, "'#%.*s' expected a constant string argument", LIT(name));
operand->mode = Addressing_Value; return LoadDirective_Error;
operand->value = {};
operand->expr = call;
return ok;
} }
switch (id) { if (!is_type_string(o.type)) {
default: gbString str = type_to_string(o.type);
GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name)); error(arg, "'#%.*s' expected a constant string, got %s", LIT(name), str);
break; gb_string_free(str);
return LoadDirective_Error;
}
case BuiltinProc_objc_send: GB_ASSERT(o.value.kind == ExactValue_String);
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_selector:
case BuiltinProc_objc_register_class:
return check_builtin_objc_procedure(c, operand, call, id, type_hint);
case BuiltinProc___entry_point: operand->type = t_u8_slice;
operand->mode = Addressing_NoValue; if (ce->args.count == 1) {
operand->type = nullptr; if (type_hint && is_valid_type_for_load(type_hint)) {
mpmc_enqueue(&c->info->intrinsics_entry_point_usage, call); operand->type = type_hint;
break; }
} 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;
case BuiltinProc_DIRECTIVE: { 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); ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string; String name = bd->name.string;
if (name == "location") { if (name == "location") {
@@ -1210,81 +1274,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
operand->type = t_source_code_location; operand->type = t_source_code_location;
operand->mode = Addressing_Value; operand->mode = Addressing_Value;
} else if (name == "load") { } else if (name == "load") {
if (ce->args.count != 1) { return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
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") { } else if (name == "load_hash") {
if (ce->args.count != 2) { if (ce->args.count != 2) {
if (ce->args.count == 0) { if (ce->args.count == 0) {
@@ -1324,14 +1314,11 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
gb_string_free(str); gb_string_free(str);
return false; return false;
} }
gbAllocator a = heap_allocator(); gbAllocator a = heap_allocator();
GB_ASSERT(o.value.kind == ExactValue_String); GB_ASSERT(o.value.kind == ExactValue_String);
GB_ASSERT(o_hash.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 original_string = o.value.value_string;
String hash_kind = o_hash.value.value_string; String hash_kind = o_hash.value.value_string;
@@ -1364,44 +1351,17 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
return false; return false;
} }
LoadFileCache *cache = nullptr;
BlockingMutex *ignore_mutex = nullptr; if (cache_load_file_directive(c, call, original_string, true, &cache)) {
String path = {}; MUTEX_GUARD(&c->info->load_file_mutex);
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 // TODO(bill): make these procedures fast :P
u64 hash_value = 0; u64 hash_value = 0;
String result = {}; u64 *hash_value_ptr = string_map_get(&cache->hashes, hash_kind);
isize file_size = cast(isize)gb_file_size(&f); if (hash_value_ptr) {
if (file_size > 0) { hash_value = *hash_value_ptr;
u8 *data = cast(u8 *)gb_alloc(a, file_size); } else {
gb_file_read_at(&f, data, file_size, 0); u8 *data = cache->data.text;
isize file_size = cache->data.len;
if (hash_kind == "adler32") { if (hash_kind == "adler32") {
hash_value = gb_adler32(data, file_size); hash_value = gb_adler32(data, file_size);
} else if (hash_kind == "crc32") { } else if (hash_kind == "crc32") {
@@ -1423,14 +1383,18 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
} else { } else {
compiler_error("unhandled hash kind: %.*s", LIT(hash_kind)); compiler_error("unhandled hash kind: %.*s", LIT(hash_kind));
} }
gb_free(a, data); string_map_set(&cache->hashes, hash_kind, hash_value);
} }
operand->type = t_untyped_integer; operand->type = t_untyped_integer;
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
operand->value = exact_value_u64(hash_value); operand->value = exact_value_u64(hash_value);
return true;
}
return false;
} else if (name == "load_or") { } 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 != 2) {
if (ce->args.count == 0) { if (ce->args.count == 0) {
error(ce->close, "'#load_or' expects 2 arguments, got 0"); error(ce->close, "'#load_or' expects 2 arguments, got 0");
@@ -1469,45 +1433,17 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
gb_string_free(str); gb_string_free(str);
return false; return false;
} }
gbAllocator a = heap_allocator();
GB_ASSERT(o.value.kind == ExactValue_String); 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; 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->type = t_u8_slice;
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
if (file_err == gbFileError_None) { LoadFileCache *cache = nullptr;
String result = {}; if (cache_load_file_directive(c, call, original_string, false, &cache)) {
isize file_size = cast(isize)gb_file_size(&f); operand->value = exact_value_string(cache->data);
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 { } else {
operand->value = default_op.value; operand->value = default_op.value;
} }
} else if (name == "assert") { } else if (name == "assert") {
if (ce->args.count != 1 && ce->args.count != 2) { if (ce->args.count != 1 && ce->args.count != 2) {
error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count); error(call, "'#assert' expects either 1 or 2 arguments, got %td", ce->args.count);
@@ -1635,9 +1571,122 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
} else { } else {
error(call, "Unknown directive call: #%.*s", LIT(name)); 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);
if (ce->inlining != ProcInlining_none) {
error(call, "Inlining operators are not allowed on built-in procedures");
}
BuiltinProc *bp = &builtin_procs[id];
{
char const *err = nullptr;
if (ce->args.count < bp->arg_count) {
err = "Too few";
} else if (ce->args.count > bp->arg_count && !bp->variadic) {
err = "Too many";
}
if (err != nullptr) {
gbString expr = expr_to_string(ce->proc);
error(ce->close, "%s arguments for '%s', expected %td, got %td",
err, expr,
bp->arg_count, ce->args.count);
gb_string_free(expr);
return false;
}
}
switch (id) {
case BuiltinProc_size_of:
case BuiltinProc_align_of:
case BuiltinProc_offset_of:
case BuiltinProc_offset_of_by_string:
case BuiltinProc_type_info_of:
case BuiltinProc_typeid_of:
case BuiltinProc_len:
case BuiltinProc_min:
case BuiltinProc_max:
case BuiltinProc_type_is_subtype_of:
case BuiltinProc_objc_send:
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_selector:
case BuiltinProc_objc_register_class:
case BuiltinProc_atomic_type_is_lock_free:
// NOTE(bill): The first arg may be a Type, this will be checked case by case
break;
case BuiltinProc_atomic_thread_fence:
case BuiltinProc_atomic_signal_fence:
// NOTE(bill): first type will require a type hint
break;
case BuiltinProc_DIRECTIVE: {
ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string;
if (name == "defined") {
break; break;
} }
if (name == "config") {
break;
}
/*fallthrough*/
}
default:
if (BuiltinProc__type_begin < id && id < BuiltinProc__type_end) {
check_expr_or_type(c, operand, ce->args[0]);
} else if (ce->args.count > 0) {
check_multi_expr(c, operand, ce->args[0]);
}
break;
}
String const &builtin_name = builtin_procs[id].name;
if (ce->args.count > 0) {
if (ce->args[0]->kind == Ast_FieldValue) {
if (id != BuiltinProc_soa_zip) {
error(call, "'field = value' calling is not allowed on built-in procedures");
return false;
}
}
}
if (BuiltinProc__simd_begin < id && id < BuiltinProc__simd_end) {
bool ok = check_builtin_simd_operation(c, operand, call, id, type_hint);
if (!ok) {
operand->type = t_invalid;
}
operand->mode = Addressing_Value;
operand->value = {};
operand->expr = call;
return ok;
}
switch (id) {
default:
GB_PANIC("Implement built-in procedure: %.*s", LIT(builtin_name));
break;
case BuiltinProc_objc_send:
case BuiltinProc_objc_find_selector:
case BuiltinProc_objc_find_class:
case BuiltinProc_objc_register_selector:
case BuiltinProc_objc_register_class:
return check_builtin_objc_procedure(c, operand, call, id, type_hint);
case BuiltinProc___entry_point:
operand->mode = Addressing_NoValue;
operand->type = nullptr;
mpmc_enqueue(&c->info->intrinsics_entry_point_usage, call);
break;
case BuiltinProc_DIRECTIVE:
return check_builtin_procedure_directive(c, operand, call, type_hint);
case BuiltinProc_len: case BuiltinProc_len:
check_expr_or_type(c, operand, ce->args[0]); check_expr_or_type(c, operand, ce->args[0]);
+73 -2
View File
@@ -121,6 +121,28 @@ void check_or_return_split_types(CheckerContext *c, Operand *x, String const &na
bool is_diverging_expr(Ast *expr); bool is_diverging_expr(Ast *expr);
enum LoadDirectiveResult {
LoadDirective_Success = 0,
LoadDirective_Error = 1,
LoadDirective_NotFound = 2,
};
bool is_load_directive_call(Ast *call) {
call = unparen_expr(call);
if (call->kind != Ast_CallExpr) {
return false;
}
ast_node(ce, CallExpr, call);
if (ce->proc->kind != Ast_BasicDirective) {
return false;
}
ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string;
return name == "load";
}
LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found);
void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") { void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") {
auto results = did_you_mean_results(d); auto results = did_you_mean_results(d);
if (results.count != 0) { if (results.count != 0) {
@@ -7407,9 +7429,59 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
String name = oe->token.string; String name = oe->token.string;
Ast *arg = oe->x; Ast *arg = oe->x;
Ast *default_value = oe->y; Ast *default_value = oe->y;
Operand x = {}; Operand x = {};
Operand y = {}; Operand y = {};
// NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default
if (is_load_directive_call(arg)) {
LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false);
// Allow for chaining of '#load(path) or_else #load(path)'
if (!(is_load_directive_call(default_value) && res == LoadDirective_Success)) {
bool y_is_diverging = false;
check_expr_base(c, &y, default_value, x.type);
switch (y.mode) {
case Addressing_NoValue:
if (is_diverging_expr(y.expr)) {
// Allow
y.mode = Addressing_Value;
y_is_diverging = true;
} else {
error_operand_no_value(&y);
y.mode = Addressing_Invalid;
}
break;
case Addressing_Type:
error_operand_not_expression(&y);
y.mode = Addressing_Invalid;
break;
}
if (y.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
o->type = t_invalid;
o->expr = node;
return Expr_Expr;
}
if (!y_is_diverging) {
check_assignment(c, &y, x.type, name);
if (y.mode != Addressing_Constant) {
error(y.expr, "expected a constant expression on the right-hand side of 'or_else' in conjuction with '#load'");
}
}
}
if (res == LoadDirective_Success) {
*o = x;
} else {
*o = y;
}
o->expr = node;
return Expr_Expr;
}
check_multi_expr_with_type_hint(c, &x, arg, type_hint); check_multi_expr_with_type_hint(c, &x, arg, type_hint);
if (x.mode == Addressing_Invalid) { if (x.mode == Addressing_Invalid) {
o->mode = Addressing_Value; o->mode = Addressing_Value;
@@ -7417,7 +7489,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
o->expr = node; o->expr = node;
return Expr_Expr; return Expr_Expr;
} }
bool y_is_diverging = false; bool y_is_diverging = false;
check_expr_base(c, &y, default_value, x.type); check_expr_base(c, &y, default_value, x.type);
switch (y.mode) { switch (y.mode) {
+4
View File
@@ -1170,6 +1170,8 @@ void init_checker_info(CheckerInfo *i) {
mutex_init(&i->objc_types_mutex); mutex_init(&i->objc_types_mutex);
map_init(&i->objc_msgSend_types, a); map_init(&i->objc_msgSend_types, a);
mutex_init(&i->load_file_mutex);
string_map_init(&i->load_file_cache, a);
} }
void destroy_checker_info(CheckerInfo *i) { void destroy_checker_info(CheckerInfo *i) {
@@ -1205,6 +1207,8 @@ void destroy_checker_info(CheckerInfo *i) {
mutex_destroy(&i->objc_types_mutex); mutex_destroy(&i->objc_types_mutex);
map_destroy(&i->objc_msgSend_types); map_destroy(&i->objc_msgSend_types);
mutex_init(&i->load_file_mutex);
string_map_destroy(&i->load_file_cache);
} }
CheckerContext make_checker_context(Checker *c) { CheckerContext make_checker_context(Checker *c) {
+9
View File
@@ -287,6 +287,12 @@ struct ObjcMsgData {
ObjcMsgKind kind; ObjcMsgKind kind;
Type *proc_type; Type *proc_type;
}; };
struct LoadFileCache {
String path;
gbFileError file_error;
String data;
StringMap<u64> hashes;
};
// CheckerInfo stores all the symbol information for a type-checked program // CheckerInfo stores all the symbol information for a type-checked program
struct CheckerInfo { struct CheckerInfo {
@@ -363,6 +369,9 @@ struct CheckerInfo {
BlockingMutex objc_types_mutex; BlockingMutex objc_types_mutex;
PtrMap<Ast *, ObjcMsgData> objc_msgSend_types; PtrMap<Ast *, ObjcMsgData> objc_msgSend_types;
BlockingMutex load_file_mutex;
StringMap<LoadFileCache *> load_file_cache;
}; };
struct CheckerContext { struct CheckerContext {
+2 -2
View File
@@ -391,8 +391,8 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
if (is_type_slice(type)) { if (is_type_slice(type)) {
if (value.kind == ExactValue_String) { if (value.kind == ExactValue_String) {
GB_ASSERT(is_type_u8_slice(type)); GB_ASSERT(is_type_slice(type));
res.value = lb_find_or_add_entity_string_byte_slice(m, value.value_string).value; res.value = lb_find_or_add_entity_string_byte_slice_with_type(m, value.value_string, original_type).value;
return res; return res;
} else { } else {
ast_node(cl, CompoundLit, value.value_compound); ast_node(cl, CompoundLit, value.value_compound);
+48
View File
@@ -2508,8 +2508,56 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str)
res.type = t_u8_slice; res.type = t_u8_slice;
return res; return res;
} }
lbValue lb_find_or_add_entity_string_byte_slice_with_type(lbModule *m, String const &str, Type *slice_type) {
GB_ASSERT(is_type_slice(slice_type));
LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)};
LLVMValueRef data = LLVMConstStringInContext(m->ctx,
cast(char const *)str.text,
cast(unsigned)str.len,
false);
char *name = nullptr;
{
isize max_len = 7+8+1;
name = gb_alloc_array(permanent_allocator(), char, max_len);
u32 id = m->gen->global_array_index.fetch_add(1);
isize len = gb_snprintf(name, max_len, "csbs$%x", id);
len -= 1;
}
LLVMTypeRef type = LLVMTypeOf(data);
LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name);
LLVMSetInitializer(global_data, data);
LLVMSetLinkage(global_data, LLVMPrivateLinkage);
LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr);
LLVMSetAlignment(global_data, 1);
LLVMSetGlobalConstant(global_data, true);
i64 data_len = str.len;
LLVMValueRef ptr = nullptr;
if (data_len != 0) {
ptr = LLVMConstInBoundsGEP2(type, global_data, indices, 2);
} else {
ptr = LLVMConstNull(lb_type(m, t_u8_ptr));
}
if (!is_type_u8_slice(slice_type)) {
Type *bt = base_type(slice_type);
Type *elem = bt->Slice.elem;
i64 sz = type_size_of(elem);
GB_ASSERT(sz > 0);
ptr = LLVMConstPointerCast(ptr, lb_type(m, alloc_type_pointer(elem)));
data_len /= sz;
}
LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), data_len, true);
LLVMValueRef values[2] = {ptr, len};
lbValue res = {};
res.value = llvm_const_named_struct(m, slice_type, values, 2);
res.type = slice_type;
return res;
}
lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) { lbValue lb_find_ident(lbProcedure *p, lbModule *m, Entity *e, Ast *expr) {
+4
View File
@@ -351,6 +351,10 @@ lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) {
lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) { lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) {
if (arg->state_flags & StateFlag_DirectiveWasFalse) {
return lb_build_expr(p, else_expr);
}
lbValue lhs = {}; lbValue lhs = {};
lbValue rhs = {}; lbValue rhs = {};
lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs);
+2 -1
View File
@@ -282,7 +282,8 @@ enum StateFlag : u8 {
StateFlag_type_assert = 1<<2, StateFlag_type_assert = 1<<2,
StateFlag_no_type_assert = 1<<3, StateFlag_no_type_assert = 1<<3,
StateFlag_SelectorCallExpr = 1<<6, StateFlag_SelectorCallExpr = 1<<5,
StateFlag_DirectiveWasFalse = 1<<6,
StateFlag_BeenHandled = 1<<7, StateFlag_BeenHandled = 1<<7,
}; };
+51
View File
@@ -2403,6 +2403,57 @@ bool is_type_simple_compare(Type *t) {
return false; return false;
} }
bool is_type_load_safe(Type *type) {
GB_ASSERT(type != nullptr);
type = core_type(core_array_type(type));
switch (type->kind) {
case Type_Basic:
return (type->Basic.flags & (BasicFlag_Boolean|BasicFlag_Numeric|BasicFlag_Rune)) != 0;
case Type_BitSet:
if (type->BitSet.underlying) {
return is_type_load_safe(type->BitSet.underlying);
}
return true;
case Type_RelativePointer:
case Type_RelativeSlice:
return true;
case Type_Pointer:
case Type_MultiPointer:
case Type_Slice:
case Type_DynamicArray:
case Type_Proc:
case Type_SoaPointer:
return false;
case Type_Enum:
case Type_EnumeratedArray:
case Type_Array:
case Type_SimdVector:
case Type_Matrix:
GB_PANIC("should never be hit");
return false;
case Type_Struct:
for_array(i, type->Struct.fields) {
if (!is_type_load_safe(type->Struct.fields[i]->type)) {
return false;
}
}
return type_size_of(type) > 0;
case Type_Union:
for_array(i, type->Union.variants) {
if (!is_type_load_safe(type->Union.variants[i])) {
return false;
}
}
return type_size_of(type) > 0;
}
return false;
}
String lookup_subtype_polymorphic_field(Type *dst, Type *src) { String lookup_subtype_polymorphic_field(Type *dst, Type *src) {
Type *prev_src = src; Type *prev_src = src;
// Type *prev_dst = dst; // Type *prev_dst = dst;