mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
#load directive (request from #368) (Basic implementation)
This commit is contained in:
+71
-1
@@ -3125,6 +3125,76 @@ 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") {
|
||||||
|
if (ce->args.count != 1) {
|
||||||
|
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(bd->token.pos.file);
|
||||||
|
String original_string = o.value.value_string;
|
||||||
|
|
||||||
|
|
||||||
|
gbMutex *ignore_mutex = nullptr;
|
||||||
|
String path = {};
|
||||||
|
bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
|
||||||
|
|
||||||
|
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 == "assert") {
|
} else if (name == "assert") {
|
||||||
if (ce->args.count != 1) {
|
if (ce->args.count != 1) {
|
||||||
error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
|
error(call, "'#assert' expects 1 argument, got %td", ce->args.count);
|
||||||
@@ -5318,7 +5388,7 @@ ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call) {
|
|||||||
ce->proc->kind == Ast_BasicDirective) {
|
ce->proc->kind == Ast_BasicDirective) {
|
||||||
ast_node(bd, BasicDirective, ce->proc);
|
ast_node(bd, BasicDirective, ce->proc);
|
||||||
String name = bd->name;
|
String name = bd->name;
|
||||||
if (name == "location" || name == "assert" || name == "defined") {
|
if (name == "location" || name == "assert" || name == "defined" || name == "load") {
|
||||||
operand->mode = Addressing_Builtin;
|
operand->mode = Addressing_Builtin;
|
||||||
operand->builtin_id = BuiltinProc_DIRECTIVE;
|
operand->builtin_id = BuiltinProc_DIRECTIVE;
|
||||||
operand->expr = ce->proc;
|
operand->expr = ce->proc;
|
||||||
|
|||||||
+27
-22
@@ -1525,30 +1525,35 @@ irValue *ir_add_module_constant(irModule *m, Type *type, ExactValue value) {
|
|||||||
gbAllocator a = ir_allocator();
|
gbAllocator a = ir_allocator();
|
||||||
|
|
||||||
if (is_type_slice(type)) {
|
if (is_type_slice(type)) {
|
||||||
ast_node(cl, CompoundLit, value.value_compound);
|
if (value.kind == ExactValue_String) {
|
||||||
|
GB_ASSERT(is_type_u8_slice(type));
|
||||||
|
return ir_value_constant(type, value);
|
||||||
|
} else {
|
||||||
|
ast_node(cl, CompoundLit, value.value_compound);
|
||||||
|
|
||||||
isize count = cl->elems.count;
|
isize count = cl->elems.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return ir_value_nil(type);
|
return ir_value_nil(type);
|
||||||
|
}
|
||||||
|
Type *elem = base_type(type)->Slice.elem;
|
||||||
|
Type *t = alloc_type_array(elem, count);
|
||||||
|
irValue *backing_array = ir_add_module_constant(m, t, value);
|
||||||
|
|
||||||
|
|
||||||
|
isize max_len = 7+8+1;
|
||||||
|
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
|
||||||
|
isize len = gb_snprintf(cast(char *)str, max_len, "csba$%x", m->global_array_index);
|
||||||
|
m->global_array_index++;
|
||||||
|
|
||||||
|
String name = make_string(str, len-1);
|
||||||
|
|
||||||
|
Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
|
||||||
|
irValue *g = ir_value_global(e, backing_array);
|
||||||
|
ir_module_add_value(m, e, g);
|
||||||
|
map_set(&m->members, hash_string(name), g);
|
||||||
|
|
||||||
|
return ir_value_constant_slice(type, g, count);
|
||||||
}
|
}
|
||||||
Type *elem = base_type(type)->Slice.elem;
|
|
||||||
Type *t = alloc_type_array(elem, count);
|
|
||||||
irValue *backing_array = ir_add_module_constant(m, t, value);
|
|
||||||
|
|
||||||
|
|
||||||
isize max_len = 7+8+1;
|
|
||||||
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
|
|
||||||
isize len = gb_snprintf(cast(char *)str, max_len, "csba$%x", m->global_array_index);
|
|
||||||
m->global_array_index++;
|
|
||||||
|
|
||||||
String name = make_string(str, len-1);
|
|
||||||
|
|
||||||
Entity *e = alloc_entity_constant(nullptr, make_token_ident(name), t, value);
|
|
||||||
irValue *g = ir_value_global(e, backing_array);
|
|
||||||
ir_module_add_value(m, e, g);
|
|
||||||
map_set(&m->members, hash_string(name), g);
|
|
||||||
|
|
||||||
return ir_value_constant_slice(type, g, count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ir_value_constant(type, value);
|
return ir_value_constant(type, value);
|
||||||
|
|||||||
+14
-1
@@ -673,7 +673,20 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
|
|||||||
ir_write_str_lit(f, "zeroinitializer");
|
ir_write_str_lit(f, "zeroinitializer");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!is_type_string(type)) {
|
if (is_type_u8_slice(type)) {
|
||||||
|
irValue *str_array = ir_add_global_string_array(m, str);
|
||||||
|
ir_write_str_lit(f, "{i8* getelementptr inbounds (");
|
||||||
|
ir_print_type(f, m, str_array->Global.entity->type);
|
||||||
|
ir_write_str_lit(f, ", ");
|
||||||
|
ir_print_type(f, m, str_array->Global.entity->type);
|
||||||
|
ir_write_str_lit(f, "* ");
|
||||||
|
ir_print_encoded_global(f, str_array->Global.entity->token.string, false);
|
||||||
|
ir_write_str_lit(f, ", ");
|
||||||
|
ir_print_type(f, m, t_i32);
|
||||||
|
ir_write_str_lit(f, " 0, i32 0), ");
|
||||||
|
ir_print_type(f, m, t_int);
|
||||||
|
ir_fprintf(f, " %lld}", cast(i64)str.len);
|
||||||
|
} else if (!is_type_string(type)) {
|
||||||
GB_ASSERT(is_type_array(type));
|
GB_ASSERT(is_type_array(type));
|
||||||
ir_write_str_lit(f, "c\"");
|
ir_write_str_lit(f, "c\"");
|
||||||
ir_print_escape_string(f, str, false, false);
|
ir_print_escape_string(f, str, false, false);
|
||||||
|
|||||||
+21
-14
@@ -1677,6 +1677,9 @@ Ast *parse_operand(AstFile *f, bool lhs) {
|
|||||||
} else if (name.string == "location") {
|
} else if (name.string == "location") {
|
||||||
Ast *tag = ast_basic_directive(f, token, name.string);
|
Ast *tag = ast_basic_directive(f, token, name.string);
|
||||||
return parse_call_expr(f, tag);
|
return parse_call_expr(f, tag);
|
||||||
|
} else if (name.string == "load") {
|
||||||
|
Ast *tag = ast_basic_directive(f, token, name.string);
|
||||||
|
return parse_call_expr(f, tag);
|
||||||
} else if (name.string == "assert") {
|
} else if (name.string == "assert") {
|
||||||
Ast *tag = ast_basic_directive(f, token, name.string);
|
Ast *tag = ast_basic_directive(f, token, name.string);
|
||||||
return parse_call_expr(f, tag);
|
return parse_call_expr(f, tag);
|
||||||
@@ -4191,7 +4194,7 @@ bool is_package_name_reserved(String const &name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool determine_path_from_string(Parser *p, Ast *node, String base_dir, String original_string, String *path) {
|
bool determine_path_from_string(gbMutex *file_mutex, Ast *node, String base_dir, String original_string, String *path) {
|
||||||
GB_ASSERT(path != nullptr);
|
GB_ASSERT(path != nullptr);
|
||||||
|
|
||||||
gbAllocator a = heap_allocator();
|
gbAllocator a = heap_allocator();
|
||||||
@@ -4229,8 +4232,8 @@ bool determine_path_from_string(Parser *p, Ast *node, String base_dir, String or
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
gb_mutex_lock(&p->file_decl_mutex);
|
if (file_mutex) gb_mutex_lock(file_mutex);
|
||||||
defer (gb_mutex_unlock(&p->file_decl_mutex));
|
defer (if (file_mutex) gb_mutex_unlock(file_mutex));
|
||||||
|
|
||||||
|
|
||||||
if (node->kind == Ast_ForeignImportDecl) {
|
if (node->kind == Ast_ForeignImportDecl) {
|
||||||
@@ -4321,7 +4324,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *>
|
|||||||
|
|
||||||
String original_string = string_trim_whitespace(id->relpath.string);
|
String original_string = string_trim_whitespace(id->relpath.string);
|
||||||
String import_path = {};
|
String import_path = {};
|
||||||
bool ok = determine_path_from_string(p, node, base_dir, original_string, &import_path);
|
bool ok = determine_path_from_string(&p->file_decl_mutex, node, base_dir, original_string, &import_path);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
|
decls[i] = ast_bad_decl(f, id->relpath, id->relpath);
|
||||||
continue;
|
continue;
|
||||||
@@ -4344,7 +4347,7 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, Array<Ast *>
|
|||||||
String fullpath = file_str;
|
String fullpath = file_str;
|
||||||
|
|
||||||
String foreign_path = {};
|
String foreign_path = {};
|
||||||
bool ok = determine_path_from_string(p, node, base_dir, file_str, &foreign_path);
|
bool ok = determine_path_from_string(&p->file_decl_mutex, node, base_dir, file_str, &foreign_path);
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
decls[i] = ast_bad_decl(f, fl->filepaths[fp_idx], fl->filepaths[fl->filepaths.count-1]);
|
decls[i] = ast_bad_decl(f, fl->filepaths[fp_idx], fl->filepaths[fl->filepaths.count-1]);
|
||||||
goto end;
|
goto end;
|
||||||
@@ -4444,6 +4447,18 @@ bool parse_build_tag(Token token_for_pos, String s) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String dir_from_path(String path) {
|
||||||
|
String base_dir = path;
|
||||||
|
for (isize i = path.len-1; i >= 0; i--) {
|
||||||
|
if (base_dir[i] == '\\' ||
|
||||||
|
base_dir[i] == '/') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
base_dir.len--;
|
||||||
|
}
|
||||||
|
return base_dir;
|
||||||
|
}
|
||||||
|
|
||||||
bool parse_file(Parser *p, AstFile *f) {
|
bool parse_file(Parser *p, AstFile *f) {
|
||||||
if (f->tokens.count == 0) {
|
if (f->tokens.count == 0) {
|
||||||
return true;
|
return true;
|
||||||
@@ -4453,15 +4468,7 @@ bool parse_file(Parser *p, AstFile *f) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String filepath = f->tokenizer.fullpath;
|
String filepath = f->tokenizer.fullpath;
|
||||||
String base_dir = filepath;
|
String base_dir = dir_from_path(filepath);
|
||||||
for (isize i = filepath.len-1; i >= 0; i--) {
|
|
||||||
if (base_dir[i] == '\\' ||
|
|
||||||
base_dir[i] == '/') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
base_dir.len--;
|
|
||||||
}
|
|
||||||
|
|
||||||
comsume_comment_groups(f, f->prev_token);
|
comsume_comment_groups(f, f->prev_token);
|
||||||
|
|
||||||
CommentGroup *docs = f->lead_comment;
|
CommentGroup *docs = f->lead_comment;
|
||||||
|
|||||||
Reference in New Issue
Block a user