Allow all set entry types to be implicitly cast to their key/value type to allow for easier iteration

This commit is contained in:
gingerBill
2023-01-03 12:18:35 +00:00
parent 252be0fb41
commit 747a11a954
8 changed files with 50 additions and 50 deletions
+2 -4
View File
@@ -1332,11 +1332,10 @@ gb_internal void enable_target_feature(TokenPos pos, String const &target_featur
gb_internal char const *target_features_set_to_cstring(gbAllocator allocator, bool with_quotes) { gb_internal char const *target_features_set_to_cstring(gbAllocator allocator, bool with_quotes) {
isize len = 0; isize len = 0;
isize i = 0; isize i = 0;
for (auto const &entry : build_context.target_features_set) { for (String const &feature : build_context.target_features_set) {
if (i != 0) { if (i != 0) {
len += 1; len += 1;
} }
String feature = entry.value;
len += feature.len; len += feature.len;
if (with_quotes) len += 2; if (with_quotes) len += 2;
i += 1; i += 1;
@@ -1344,13 +1343,12 @@ gb_internal char const *target_features_set_to_cstring(gbAllocator allocator, bo
char *features = gb_alloc_array(allocator, char, len+1); char *features = gb_alloc_array(allocator, char, len+1);
len = 0; len = 0;
i = 0; i = 0;
for (auto const &entry : build_context.target_features_set) { for (String const &feature : build_context.target_features_set) {
if (i != 0) { if (i != 0) {
features[len++] = ','; features[len++] = ',';
} }
if (with_quotes) features[len++] = '"'; if (with_quotes) features[len++] = '"';
String feature = entry.value;
gb_memmove(features + len, feature.text, feature.len); gb_memmove(features + len, feature.text, feature.len);
len += feature.len; len += feature.len;
if (with_quotes) features[len++] = '"'; if (with_quotes) features[len++] = '"';
+2 -4
View File
@@ -1587,16 +1587,14 @@ gb_internal bool check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *de
MUTEX_GUARD_BLOCK(decl->deps_mutex) MUTEX_GUARD_BLOCK(decl->deps_mutex)
MUTEX_GUARD_BLOCK(decl->parent->deps_mutex) { MUTEX_GUARD_BLOCK(decl->parent->deps_mutex) {
for (auto const &entry : decl->deps) { for (Entity *e : decl->deps) {
Entity *e = entry.ptr;
ptr_set_add(&decl->parent->deps, e); ptr_set_add(&decl->parent->deps, e);
} }
} }
MUTEX_GUARD_BLOCK(decl->type_info_deps_mutex) MUTEX_GUARD_BLOCK(decl->type_info_deps_mutex)
MUTEX_GUARD_BLOCK(decl->parent->type_info_deps_mutex) { MUTEX_GUARD_BLOCK(decl->parent->type_info_deps_mutex) {
for (auto const &entry : decl->type_info_deps) { for (Type *t : decl->type_info_deps) {
Type *t = entry.ptr;
ptr_set_add(&decl->parent->type_info_deps, t); ptr_set_add(&decl->parent->type_info_deps, t);
} }
} }
+3 -4
View File
@@ -202,8 +202,8 @@ gb_internal void check_did_you_mean_objc_entity(String const &name, Entity *e, b
DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), set.entries.count, name); DidYouMeanAnswers d = did_you_mean_make(heap_allocator(), set.entries.count, name);
defer (did_you_mean_destroy(&d)); defer (did_you_mean_destroy(&d));
for (auto const &entry : set) { for (String const &target : set) {
did_you_mean_append(&d, entry.value); did_you_mean_append(&d, target);
} }
check_did_you_mean_print(&d, prefix); check_did_you_mean_print(&d, prefix);
} }
@@ -4942,8 +4942,7 @@ gb_internal isize add_dependencies_from_unpacking(CheckerContext *c, Entity **lh
if (e != nullptr) { if (e != nullptr) {
DeclInfo *decl = decl_info_of_entity(e); DeclInfo *decl = decl_info_of_entity(e);
if (decl != nullptr) { if (decl != nullptr) {
for (auto const &entry : decl->deps) { for (Entity *dep : decl->deps) {
Entity *dep = entry.ptr;
ptr_set_add(&c->decl->deps, dep); ptr_set_add(&c->decl->deps, dep);
} }
} }
+13 -25
View File
@@ -2222,12 +2222,11 @@ gb_internal void add_dependency_to_set(Checker *c, Entity *entity) {
return; return;
} }
for (auto const &entry : decl->type_info_deps) { for (Type *t : decl->type_info_deps) {
add_min_dep_type_info(c, entry.ptr); add_min_dep_type_info(c, t);
} }
for (auto const &entry : decl->deps) { for (Entity *e : decl->deps) {
Entity *e = entry.ptr;
add_dependency_to_set(c, e); add_dependency_to_set(c, e);
if (e->kind == Entity_Procedure && e->Procedure.is_foreign) { if (e->kind == Entity_Procedure && e->Procedure.is_foreign) {
Entity *fl = e->Procedure.foreign_library; Entity *fl = e->Procedure.foreign_library;
@@ -2510,8 +2509,7 @@ gb_internal Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInf
DeclInfo *decl = decl_info_of_entity(e); DeclInfo *decl = decl_info_of_entity(e);
GB_ASSERT(decl != nullptr); GB_ASSERT(decl != nullptr);
for (auto const &entry : decl->deps) { for (Entity *dep : decl->deps) {
Entity *dep = entry.ptr;
if (dep->flags & EntityFlag_Field) { if (dep->flags & EntityFlag_Field) {
continue; continue;
} }
@@ -2537,15 +2535,12 @@ gb_internal Array<EntityGraphNode *> generate_entity_dependency_graph(CheckerInf
if (e->kind == Entity_Procedure) { if (e->kind == Entity_Procedure) {
// Connect each pred 'p' of 'n' with each succ 's' and from // Connect each pred 'p' of 'n' with each succ 's' and from
// the procedure node // the procedure node
for (auto const &p_entry : n->pred) { for (EntityGraphNode *p : n->pred) {
EntityGraphNode *p = p_entry.ptr;
// Ignore self-cycles // Ignore self-cycles
if (p != n) { if (p != n) {
// Each succ 's' of 'n' becomes a succ of 'p', and // Each succ 's' of 'n' becomes a succ of 'p', and
// each pred 'p' of 'n' becomes a pred of 's' // each pred 'p' of 'n' becomes a pred of 's'
for (auto const &s_entry : n->succ) { for (EntityGraphNode *s : n->succ) {
EntityGraphNode *s = s_entry.ptr;
// Ignore self-cycles // Ignore self-cycles
if (s != n) { if (s != n) {
if (p->entity->kind == Entity_Procedure && if (p->entity->kind == Entity_Procedure &&
@@ -4784,8 +4779,7 @@ gb_internal void check_import_entities(Checker *c) {
} }
} }
for (auto const &entry : n->pred) { for (ImportGraphNode *p : n->pred) {
ImportGraphNode *p = entry.ptr;
p->dep_count = gb_max(p->dep_count-1, 0); p->dep_count = gb_max(p->dep_count-1, 0);
priority_queue_fix(&pq, p->index); priority_queue_fix(&pq, p->index);
} }
@@ -4893,8 +4887,7 @@ gb_internal bool find_entity_path_tuple(Type *tuple, Entity *end, PtrSet<Entity
if (var_decl == nullptr) { if (var_decl == nullptr) {
continue; continue;
} }
for (auto const &entry : var_decl->deps) { for (Entity *dep : var_decl->deps) {
Entity *dep = entry.ptr;
if (dep == end) { if (dep == end) {
auto path = array_make<Entity *>(heap_allocator()); auto path = array_make<Entity *>(heap_allocator());
array_add(&path, dep); array_add(&path, dep);
@@ -4944,8 +4937,7 @@ gb_internal Array<Entity *> find_entity_path(Entity *start, Entity *end, PtrSet<
return path; return path;
} }
} else { } else {
for (auto const &entry : decl->deps) { for (Entity *dep : decl->deps) {
Entity *dep = entry.ptr;
if (dep == end) { if (dep == end) {
auto path = array_make<Entity *>(heap_allocator()); auto path = array_make<Entity *>(heap_allocator());
array_add(&path, dep); array_add(&path, dep);
@@ -5002,8 +4994,7 @@ gb_internal void calculate_global_init_order(Checker *c) {
} }
} }
for (auto const &entry : n->pred) { for (EntityGraphNode *p : n->pred) {
EntityGraphNode *p = entry.ptr;
p->dep_count -= 1; p->dep_count -= 1;
p->dep_count = gb_max(p->dep_count, 0); p->dep_count = gb_max(p->dep_count, 0);
priority_queue_fix(&pq, p->index); priority_queue_fix(&pq, p->index);
@@ -5163,8 +5154,7 @@ gb_internal void check_unchecked_bodies(Checker *c) {
// use the `procs_to_check` array // use the `procs_to_check` array
global_procedure_body_in_worker_queue = false; global_procedure_body_in_worker_queue = false;
for (auto const &entry : c->info.minimum_dependency_set) { for (Entity *e : c->info.minimum_dependency_set) {
Entity *e = entry.ptr;
if (e == nullptr || e->kind != Entity_Procedure) { if (e == nullptr || e->kind != Entity_Procedure) {
continue; continue;
} }
@@ -5239,8 +5229,7 @@ gb_internal void check_test_procedures(Checker *c) {
AstPackage *pkg = c->info.init_package; AstPackage *pkg = c->info.init_package;
Scope *s = pkg->scope; Scope *s = pkg->scope;
for (auto const &entry : build_context.test_names) { for (String const &name : build_context.test_names) {
String name = entry.value;
Entity *e = scope_lookup(s, name); Entity *e = scope_lookup(s, name);
if (e == nullptr) { if (e == nullptr) {
Token tok = {}; Token tok = {};
@@ -5744,8 +5733,7 @@ gb_internal void check_parsed_files(Checker *c) {
DeclInfo *decl = e->decl_info; DeclInfo *decl = e->decl_info;
ast_node(pl, ProcLit, decl->proc_lit); ast_node(pl, ProcLit, decl->proc_lit);
if (pl->inlining == ProcInlining_inline) { if (pl->inlining == ProcInlining_inline) {
for (auto const &entry : decl->deps) { for (Entity *dep : decl->deps) {
Entity *dep = entry.ptr;
if (dep == e) { if (dep == e) {
error(e->token, "Cannot inline recursive procedure '%.*s'", LIT(e->token.string)); error(e->token, "Cannot inline recursive procedure '%.*s'", LIT(e->token.string));
break; break;
+8 -4
View File
@@ -2,6 +2,10 @@ template <typename T>
struct PtrSetEntry { struct PtrSetEntry {
T ptr; T ptr;
MapIndex next; MapIndex next;
operator T() const noexcept {
return this->ptr;
}
}; };
template <typename T> template <typename T>
@@ -245,21 +249,21 @@ gb_internal gb_inline void ptr_set_clear(PtrSet<T> *s) {
template <typename T> template <typename T>
gb_internal PtrSetEntry<T> *begin(PtrSet<T> &m) { gb_internal PtrSetEntry<T> *begin(PtrSet<T> &m) noexcept {
return m.entries.data; return m.entries.data;
} }
template <typename T> template <typename T>
gb_internal PtrSetEntry<T> const *begin(PtrSet<T> const &m) { gb_internal PtrSetEntry<T> const *begin(PtrSet<T> const &m) noexcept {
return m.entries.data; return m.entries.data;
} }
template <typename T> template <typename T>
gb_internal PtrSetEntry<T> *end(PtrSet<T> &m) { gb_internal PtrSetEntry<T> *end(PtrSet<T> &m) noexcept {
return m.entries.data + m.entries.count; return m.entries.data + m.entries.count;
} }
template <typename T> template <typename T>
gb_internal PtrSetEntry<T> const *end(PtrSet<T> const &m) { gb_internal PtrSetEntry<T> const *end(PtrSet<T> const &m) noexcept {
return m.entries.data + m.entries.count; return m.entries.data + m.entries.count;
} }
+10 -3
View File
@@ -1,6 +1,13 @@
struct StringHashKey { struct StringHashKey {
u32 hash; u32 hash;
String string; String string;
operator String() const noexcept {
return this->string;
}
operator String const &() const noexcept {
return this->string;
}
}; };
gb_internal gb_inline StringHashKey string_hash_string(String const &s) { gb_internal gb_inline StringHashKey string_hash_string(String const &s) {
@@ -283,11 +290,11 @@ gb_internal gb_inline void string_map_clear(StringMap<T> *h) {
template <typename T> template <typename T>
gb_internal StringMapEntry<T> *begin(StringMap<T> &m) { gb_internal StringMapEntry<T> *begin(StringMap<T> &m) noexcept {
return m.entries.data; return m.entries.data;
} }
template <typename T> template <typename T>
gb_internal StringMapEntry<T> const *begin(StringMap<T> const &m) { gb_internal StringMapEntry<T> const *begin(StringMap<T> const &m) noexcept {
return m.entries.data; return m.entries.data;
} }
@@ -298,6 +305,6 @@ gb_internal StringMapEntry<T> *end(StringMap<T> &m) {
} }
template <typename T> template <typename T>
gb_internal StringMapEntry<T> const *end(StringMap<T> const &m) { gb_internal StringMapEntry<T> const *end(StringMap<T> const &m) noexcept {
return m.entries.data + m.entries.count; return m.entries.data + m.entries.count;
} }
+11 -4
View File
@@ -2,6 +2,13 @@ struct StringSetEntry {
u32 hash; u32 hash;
MapIndex next; MapIndex next;
String value; String value;
operator String const() const noexcept {
return this->value;
}
operator String const &() const noexcept {
return this->value;
}
}; };
struct StringSet { struct StringSet {
@@ -226,18 +233,18 @@ gb_internal gb_inline void string_set_clear(StringSet *s) {
} }
gb_internal StringSetEntry *begin(StringSet &m) { gb_internal StringSetEntry *begin(StringSet &m) noexcept {
return m.entries.data; return m.entries.data;
} }
gb_internal StringSetEntry const *begin(StringSet const &m) { gb_internal StringSetEntry const *begin(StringSet const &m) noexcept {
return m.entries.data; return m.entries.data;
} }
gb_internal StringSetEntry *end(StringSet &m) { gb_internal StringSetEntry *end(StringSet &m) noexcept {
return m.entries.data + m.entries.count; return m.entries.data + m.entries.count;
} }
gb_internal StringSetEntry const *end(StringSet const &m) { gb_internal StringSetEntry const *end(StringSet const &m) noexcept {
return m.entries.data + m.entries.count; return m.entries.data + m.entries.count;
} }
+1 -2
View File
@@ -823,8 +823,7 @@ gb_internal bool type_ptr_set_exists(PtrSet<Type *> *s, Type *t) {
// TODO(bill, 2019-10-05): This is very slow and it's probably a lot // TODO(bill, 2019-10-05): This is very slow and it's probably a lot
// faster to cache types correctly // faster to cache types correctly
for (auto const &entry : *s) { for (Type *f : *s) {
Type *f = entry.ptr;
if (are_types_identical(t, f)) { if (are_types_identical(t, f)) {
ptr_set_add(s, t); ptr_set_add(s, t);
return true; return true;