aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2024-02-09 15:18:29 +0000
committergingerBill <bill@gingerbill.org>2024-02-09 15:18:29 +0000
commit5c4485f65767366c14dfd9a98945a5479ae0e449 (patch)
tree94ee4b5181c381f45332b27fb37d78b5464c6bde /src
parentbae2a6fc1e0d2207e51a66b5ea6e8ae511a3da6a (diff)
Add `#load_directory(path: string) > []runtime.Load_Directory_File`
Diffstat (limited to 'src')
-rw-r--r--src/check_builtin.cpp187
-rw-r--r--src/check_expr.cpp5
-rw-r--r--src/checker.cpp15
-rw-r--r--src/checker.hpp18
-rw-r--r--src/llvm_backend_proc.cpp67
-rw-r--r--src/string.cpp12
-rw-r--r--src/types.cpp4
7 files changed, 239 insertions, 69 deletions
diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp
index 4e374add6..d39be37a9 100644
--- a/src/check_builtin.cpp
+++ b/src/check_builtin.cpp
@@ -1264,6 +1264,139 @@ gb_internal LoadDirectiveResult check_load_directive(CheckerContext *c, Operand
}
+gb_internal int file_cache_sort_cmp(void const *x, void const *y) {
+ LoadFileCache const *a = *(LoadFileCache const **)(x);
+ LoadFileCache const *b = *(LoadFileCache const **)(y);
+ return string_compare(a->path, b->path);
+}
+
+gb_internal LoadDirectiveResult check_load_directory_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_directory");
+
+ if (ce->args.count != 1) {
+ error(ce->args[0], "'#%.*s' expects 1 argument, 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);
+
+ init_core_load_directory_file(c->checker);
+
+ operand->type = t_load_directory_file_slice;
+ operand->mode = Addressing_Value;
+
+
+ String original_string = o.value.value_string;
+ String path;
+ if (gb_path_is_absolute((char*)original_string.text)) {
+ path = original_string;
+ } else {
+ String base_dir = dir_from_path(get_file_path_string(call->file_id));
+
+ BlockingMutex *ignore_mutex = nullptr;
+ bool ok = determine_path_from_string(ignore_mutex, call, base_dir, original_string, &path);
+ gb_unused(ok);
+ }
+ MUTEX_GUARD(&c->info->load_directory_mutex);
+
+
+ gbFileError file_error = gbFileError_None;
+
+ Array<LoadFileCache *> file_caches = {};
+
+ LoadDirectoryCache **cache_ptr = string_map_get(&c->info->load_directory_cache, path);
+ LoadDirectoryCache *cache = cache_ptr ? *cache_ptr : nullptr;
+ if (cache) {
+ file_error = cache->file_error;
+ }
+ defer ({
+ if (cache == nullptr) {
+ LoadDirectoryCache *new_cache = gb_alloc_item(permanent_allocator(), LoadDirectoryCache);
+ new_cache->path = path;
+ new_cache->files = file_caches;
+ new_cache->file_error = file_error;
+ string_map_set(&c->info->load_directory_cache, path, new_cache);
+
+ map_set(&c->info->load_directory_map, call, new_cache);
+ } else {
+ cache->file_error = file_error;
+ }
+ });
+
+
+ LoadDirectiveResult result = LoadDirective_Success;
+
+
+ if (cache == nullptr) {
+ Array<FileInfo> list = {};
+ ReadDirectoryError rd_err = read_directory(path, &list);
+ defer (array_free(&list));
+
+ if (list.count == 1) {
+ GB_ASSERT(path != list[0].fullpath);
+ }
+
+
+ switch (rd_err) {
+ case ReadDirectory_InvalidPath:
+ error(call, "%.*s error - invalid path: %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_NotFound;
+ case ReadDirectory_NotExists:
+ error(call, "%.*s error - path does not exist: %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_NotFound;
+ case ReadDirectory_Permission:
+ error(call, "%.*s error - unknown error whilst reading path, %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_Error;
+ case ReadDirectory_NotDir:
+ error(call, "%.*s error - expected a directory, got a file: %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_Error;
+ case ReadDirectory_Empty:
+ error(call, "%.*s error - empty directory: %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_NotFound;
+ case ReadDirectory_Unknown:
+ error(call, "%.*s error - unknown error whilst reading path %.*s", LIT(name), LIT(original_string));
+ return LoadDirective_Error;
+ }
+
+ isize files_to_reserve = list.count+1; // always reserve 1
+
+ file_caches = array_make<LoadFileCache *>(heap_allocator(), 0, files_to_reserve);
+
+ for (FileInfo fi : list) {
+ LoadFileCache *cache = nullptr;
+ if (cache_load_file_directive(c, call, fi.fullpath, err_on_not_found, &cache)) {
+ array_add(&file_caches, cache);
+ } else {
+ result = LoadDirective_Error;
+ }
+ }
+
+ gb_sort_array(file_caches.data, file_caches.count, file_cache_sort_cmp);
+
+ }
+
+ return result;
+}
+
+
gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) {
ast_node(ce, CallExpr, call);
@@ -1291,6 +1424,8 @@ gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *o
operand->mode = Addressing_Value;
} else if (name == "load") {
return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
+ } else if (name == "load_directory") {
+ return check_load_directory_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
} else if (name == "load_hash") {
if (ce->args.count != 2) {
if (ce->args.count == 0) {
@@ -1408,58 +1543,6 @@ gb_internal bool check_builtin_procedure_directive(CheckerContext *c, Operand *o
return true;
}
return false;
- } else if (name == "load_or") {
- error(call, "'#load_or' has now been removed 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);
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 9b71208cd..11eb4b533 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -7107,8 +7107,8 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
name == "defined" ||
name == "config" ||
name == "load" ||
- name == "load_hash" ||
- name == "load_or"
+ name == "load_directory" ||
+ name == "load_hash"
) {
operand->mode = Addressing_Builtin;
operand->builtin_id = BuiltinProc_DIRECTIVE;
@@ -7958,6 +7958,7 @@ gb_internal ExprKind check_basic_directive_expr(CheckerContext *c, Operand *o, A
name == "config" ||
name == "load" ||
name == "load_hash" ||
+ name == "load_directory" ||
name == "load_or"
) {
error(node, "'#%.*s' must be used as a call", LIT(name));
diff --git a/src/checker.cpp b/src/checker.cpp
index 457ee6146..569a3c76f 100644
--- a/src/checker.cpp
+++ b/src/checker.cpp
@@ -1257,6 +1257,9 @@ gb_internal void init_checker_info(CheckerInfo *i) {
mpsc_init(&i->required_global_variable_queue, a); // 1<<10);
mpsc_init(&i->required_foreign_imports_through_force_queue, a); // 1<<10);
mpsc_init(&i->intrinsics_entry_point_usage, a); // 1<<10); // just waste some memory here, even if it probably never used
+
+ string_map_init(&i->load_directory_cache);
+ map_init(&i->load_directory_map);
}
gb_internal void destroy_checker_info(CheckerInfo *i) {
@@ -1280,6 +1283,8 @@ gb_internal void destroy_checker_info(CheckerInfo *i) {
map_destroy(&i->objc_msgSend_types);
string_map_destroy(&i->load_file_cache);
+ string_map_destroy(&i->load_directory_cache);
+ map_destroy(&i->load_directory_map);
}
gb_internal CheckerContext make_checker_context(Checker *c) {
@@ -2958,6 +2963,16 @@ gb_internal void init_core_source_code_location(Checker *c) {
t_source_code_location_ptr = alloc_type_pointer(t_source_code_location);
}
+gb_internal void init_core_load_directory_file(Checker *c) {
+ if (t_load_directory_file != nullptr) {
+ return;
+ }
+ t_load_directory_file = find_core_type(c, str_lit("Load_Directory_File"));
+ t_load_directory_file_ptr = alloc_type_pointer(t_load_directory_file);
+ t_load_directory_file_slice = alloc_type_slice(t_load_directory_file);
+}
+
+
gb_internal void init_core_map_type(Checker *c) {
if (t_map_info != nullptr) {
return;
diff --git a/src/checker.hpp b/src/checker.hpp
index 9da0f2950..9aee82257 100644
--- a/src/checker.hpp
+++ b/src/checker.hpp
@@ -340,6 +340,19 @@ struct LoadFileCache {
StringMap<u64> hashes;
};
+
+struct LoadDirectoryFile {
+ String file_name;
+ String data;
+};
+
+struct LoadDirectoryCache {
+ String path;
+ gbFileError file_error;
+ Array<LoadFileCache *> files;
+};
+
+
struct GenProcsData {
Array<Entity *> procs;
RwMutex mutex;
@@ -416,6 +429,11 @@ struct CheckerInfo {
BlockingMutex instrumentation_mutex;
Entity *instrumentation_enter_entity;
Entity *instrumentation_exit_entity;
+
+
+ BlockingMutex load_directory_mutex;
+ StringMap<LoadDirectoryCache *> load_directory_cache;
+ PtrMap<Ast *, LoadDirectoryCache *> load_directory_map; // Key: Ast_CallExpr *
};
struct CheckerContext {
diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp
index e0aca2c10..9419f9a3c 100644
--- a/src/llvm_backend_proc.cpp
+++ b/src/llvm_backend_proc.cpp
@@ -1693,24 +1693,61 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu
case BuiltinProc_DIRECTIVE: {
ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string;
- GB_ASSERT(name == "location");
- String procedure = p->entity->token.string;
- TokenPos pos = ast_token(ce->proc).pos;
- if (ce->args.count > 0) {
- Ast *ident = unselector_expr(ce->args[0]);
- GB_ASSERT(ident->kind == Ast_Ident);
- Entity *e = entity_of_node(ident);
- GB_ASSERT(e != nullptr);
-
- if (e->parent_proc_decl != nullptr && e->parent_proc_decl->entity != nullptr) {
- procedure = e->parent_proc_decl->entity->token.string;
- } else {
- procedure = str_lit("");
+ if (name == "location") {
+ String procedure = p->entity->token.string;
+ TokenPos pos = ast_token(ce->proc).pos;
+ if (ce->args.count > 0) {
+ Ast *ident = unselector_expr(ce->args[0]);
+ GB_ASSERT(ident->kind == Ast_Ident);
+ Entity *e = entity_of_node(ident);
+ GB_ASSERT(e != nullptr);
+
+ if (e->parent_proc_decl != nullptr && e->parent_proc_decl->entity != nullptr) {
+ procedure = e->parent_proc_decl->entity->token.string;
+ } else {
+ procedure = str_lit("");
+ }
+ pos = e->token.pos;
+
}
- pos = e->token.pos;
+ return lb_emit_source_code_location_as_global(p, procedure, pos);
+ } else if (name == "load_directory") {
+ lbModule *m = p->module;
+ TEMPORARY_ALLOCATOR_GUARD();
+ LoadDirectoryCache *cache = map_must_get(&m->info->load_directory_map, expr);
+ isize count = cache->files.count;
+
+ LLVMValueRef *elements = gb_alloc_array(temporary_allocator(), LLVMValueRef, count);
+ for_array(i, cache->files) {
+ LoadFileCache *file = cache->files[i];
+ String file_name = filename_without_directory(file->path);
+
+ LLVMValueRef values[2] = {};
+ values[0] = lb_const_string(m, file_name).value;
+ values[1] = lb_const_string(m, file->data).value;
+ LLVMValueRef element = llvm_const_named_struct(m, t_load_directory_file, values, gb_count_of(values));
+ elements[i] = element;
+ }
+
+ LLVMValueRef backing_array = llvm_const_array(lb_type(m, t_load_directory_file), elements, count);
+
+ Type *array_type = alloc_type_array(t_load_directory_file, count);
+ lbAddr backing_array_addr = lb_add_global_generated(m, array_type, {backing_array, array_type}, nullptr);
+ lb_make_global_private_const(backing_array_addr);
+
+ LLVMValueRef backing_array_ptr = backing_array_addr.addr.value;
+ backing_array_ptr = LLVMConstPointerCast(backing_array_ptr, lb_type(m, t_load_directory_file_ptr));
+
+ LLVMValueRef const_slice = llvm_const_slice_internal(m, backing_array_ptr, LLVMConstInt(lb_type(m, t_int), count, false));
+
+ lbAddr addr = lb_add_global_generated(p->module, tv.type, {const_slice, t_load_directory_file_slice}, nullptr);
+ lb_make_global_private_const(addr);
+
+ return lb_addr_load(p, addr);
+ } else {
+ GB_PANIC("UNKNOWN DIRECTIVE: %.*s", LIT(name));
}
- return lb_emit_source_code_location_as_global(p, procedure, pos);
}
case BuiltinProc_type_info_of: {
diff --git a/src/string.cpp b/src/string.cpp
index 9fb933b1b..bd703b2a6 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -293,6 +293,18 @@ gb_internal String filename_from_path(String s) {
return make_string(nullptr, 0);
}
+
+gb_internal String filename_without_directory(String s) {
+ isize j = 0;
+ for (j = s.len-1; j >= 0; j--) {
+ if (s[j] == '/' ||
+ s[j] == '\\') {
+ break;
+ }
+ }
+ return substring(s, gb_max(j+1, 0), s.len);
+}
+
gb_internal String concatenate_strings(gbAllocator a, String const &x, String const &y) {
isize len = x.len+y.len;
u8 *data = gb_alloc_array(a, u8, len+1);
diff --git a/src/types.cpp b/src/types.cpp
index c4b03c967..8275b87ba 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -679,6 +679,10 @@ gb_global Type *t_allocator_error = nullptr;
gb_global Type *t_source_code_location = nullptr;
gb_global Type *t_source_code_location_ptr = nullptr;
+gb_global Type *t_load_directory_file = nullptr;
+gb_global Type *t_load_directory_file_ptr = nullptr;
+gb_global Type *t_load_directory_file_slice = nullptr;
+
gb_global Type *t_map_info = nullptr;
gb_global Type *t_map_cell_info = nullptr;
gb_global Type *t_raw_map = nullptr;