From: Brendan Hansen Date: Tue, 13 Apr 2021 13:58:53 +0000 (-0500) Subject: documented overloaded procs; bugfixes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9e232a256580b0fa668b2931686377a8cdeeba34;p=onyx.git documented overloaded procs; bugfixes --- diff --git a/bin/onyx b/bin/onyx index 303d9fdd..903d5271 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/include/onyxtypes.h b/include/onyxtypes.h index daa6d7b0..b0d22e27 100644 --- a/include/onyxtypes.h +++ b/include/onyxtypes.h @@ -201,5 +201,6 @@ b32 type_is_structlike(Type* type); b32 type_is_structlike_strict(Type* type); u32 type_structlike_mem_count(Type* type); u32 type_structlike_is_simple(Type* type); +b32 type_is_sl_constructable(Type* type); #endif // #ifndef ONYX_TYPES diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index 95a71aca..6811e98c 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -430,6 +430,8 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) { if (node->kind == Ast_Kind_Struct_Literal && node->type_node == NULL) { if (type->kind == Type_Kind_VarArgs) type = type->VarArgs.ptr_to_data->Pointer.elem; + if (!type_is_sl_constructable(type)) return 0; + node->type = type; add_entities_for_node(NULL, (AstNode *) node, NULL, NULL); diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 7e2eae1a..83602da1 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -938,11 +938,13 @@ CheckStatus check_struct_literal(AstStructLiteral* sl) { bh_arr_each(AstTyped *, value, sl->args.values) { if (*value == NULL) { i32 member_idx = value - sl->args.values; // Pointer subtraction hack + StructMember smem; + type_lookup_member_by_idx(sl->type, member_idx, &smem); onyx_report_error(sl->token->pos, - "Value not given for %d%s member, '%s'.", + "Value not given for %d%s member, '%s', for type '%s'.", member_idx + 1, bh_num_suffix(member_idx + 1), - sl->type->Struct.memarr[member_idx]->name); + smem.name, type_get_name(sl->type)); } } diff --git a/src/onyxtypes.c b/src/onyxtypes.c index 778b3897..18251cf6 100644 --- a/src/onyxtypes.c +++ b/src/onyxtypes.c @@ -1043,3 +1043,12 @@ u32 type_structlike_is_simple(Type* type) { default: return 0; } } + +b32 type_is_sl_constructable(Type* type) { + switch (type->kind) { + case Type_Kind_Struct: return 1; + case Type_Kind_Slice: return 1; + case Type_Kind_DynArray: return 1; + default: return 0; + } +} diff --git a/src/onyxutils.c b/src/onyxutils.c index 6d7e3aee..e1557180 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -943,6 +943,24 @@ AstFunction* polymorphic_proc_build_only_header(AstPolyProc* pp, PolyProcLookupM // * Resolving an overload from a TypeFunction (so an overloaded procedure can be passed as a parameter) // +// NOTE: The job of this function is to take a set of overloads, and traverse it to add all possible +// overloads that are reachable. This is slightly more difficult than it may seem. In this language, +// overloaded procedures have a strict ordering to their overloads, which determines how the correct +// match will be found. This was not very complicated until overloaded procedures could be used as +// overload options. This means that you could create an "infinite loop" of overloads like so: +// +// o1 :: { o2 :: { +// (...) { ... }, (...) { ... }, +// o2 o1 +// } } +// +// Obviously, this is not really an infinite loop. It just means that all options are available if +// o1 or o2 are called. The difference between o1 and o2 is the order that the overloads will be +// searched. To build the the list of overloads, a hashmap is used to prevent the problem from being +// O(n^2), even though n would (probably) be small. bh_imap has the useful property that it maintains +// an "entries" array that, so long as nothing is ever removed from it, will maintain the order in +// which entries were put into the map. This is useful because a simple recursive algorithm can +// collect all the overloads into the map, and also use the map to provide a base case. static void build_all_overload_options(bh_arr(AstTyped *) overloads, bh_imap* all_overloads) { bh_arr_each(AstTyped *, node, overloads) { if (bh_imap_has(all_overloads, (u64) *node)) continue;