mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-22 05:34:59 -07:00
Update procedure names and extend demo.odin
This commit is contained in:
+6
-61
@@ -370,70 +370,16 @@ explicit_parametric_polymorphic_procedures :: proc() {
|
||||
|
||||
main :: proc() {
|
||||
general_stuff();
|
||||
// foreign_blocks();
|
||||
// default_arguments();
|
||||
// named_arguments();
|
||||
// default_return_values();
|
||||
// call_location();
|
||||
// explicit_parametric_polymorphic_procedures();
|
||||
foreign_blocks();
|
||||
default_arguments();
|
||||
named_arguments();
|
||||
default_return_values();
|
||||
call_location();
|
||||
explicit_parametric_polymorphic_procedures();
|
||||
|
||||
// Command line argument(s)!
|
||||
// -opt=0,1,2,3
|
||||
|
||||
|
||||
/*************/
|
||||
/* Questions */
|
||||
/*************/
|
||||
|
||||
/*
|
||||
I'm questioning if I should change the declaration syntax back to Jai-like
|
||||
as I've found solutions to the problems I had with it before.
|
||||
|
||||
Should I change back to Jai-like declarations or keep with the Pascal-like?
|
||||
|
||||
Jai-like
|
||||
|
||||
x: int;
|
||||
x: int = 123;
|
||||
x := 123;
|
||||
|
||||
foo : int : 123;
|
||||
foo :: 123;
|
||||
|
||||
MyInt :: int;
|
||||
BarType :: proc();
|
||||
|
||||
bar :: proc() {
|
||||
}
|
||||
|
||||
foreign lib {
|
||||
foreign_bar :: proc() ---;
|
||||
}
|
||||
|
||||
Pascal-like
|
||||
|
||||
var x: int;
|
||||
var x: int = 123;
|
||||
var x = 123;
|
||||
|
||||
const foo: int = 123;
|
||||
const foo = 123;
|
||||
|
||||
type MyInt int;
|
||||
type BarType proc();
|
||||
|
||||
bar :: proc() {
|
||||
}
|
||||
|
||||
foreign lib {
|
||||
foreign_bar :: proc();
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
main :: proc() {
|
||||
program := "+ + * - /";
|
||||
accumulator := 0;
|
||||
|
||||
@@ -450,5 +396,4 @@ main :: proc() {
|
||||
fmt.printf("The program \"%s\" calculates the value %d\n",
|
||||
program, accumulator);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
@@ -271,7 +271,7 @@ String get_fullpath_core(gbAllocator a, String path) {
|
||||
void init_build_context(void) {
|
||||
BuildContext *bc = &build_context;
|
||||
bc->ODIN_VENDOR = str_lit("odin");
|
||||
bc->ODIN_VERSION = str_lit("0.5.0");
|
||||
bc->ODIN_VERSION = str_lit("0.6.0-dev");
|
||||
bc->ODIN_ROOT = odin_root_dir();
|
||||
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
|
||||
+3
-3
@@ -45,8 +45,8 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
|
||||
}
|
||||
t = default_type(t);
|
||||
}
|
||||
if (is_type_gen_proc(t)) {
|
||||
error(e->token, "Invalid use of a generic procedure in %.*s", LIT(context_name));
|
||||
if (is_type_polymorphic(t)) {
|
||||
error(e->token, "Invalid use of a polymorphic type in %.*s", LIT(context_name));
|
||||
e->type = t_invalid;
|
||||
return NULL;
|
||||
}
|
||||
@@ -364,7 +364,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
|
||||
}
|
||||
|
||||
|
||||
if (pt->is_generic) {
|
||||
if (pt->is_polymorphic) {
|
||||
if (pl->body == NULL) {
|
||||
error(e->token, "Polymorphic procedures must have a body");
|
||||
}
|
||||
|
||||
+18
-15
@@ -284,7 +284,7 @@ i64 check_distance_between_types(Checker *c, Operand *operand, Type *type) {
|
||||
|
||||
|
||||
if (is_type_any(dst)) {
|
||||
if (!is_type_gen_proc(src)) {
|
||||
if (!is_type_polymorphic(src)) {
|
||||
// NOTE(bill): Anything can cast to `Any`
|
||||
add_type_info_type(c, s);
|
||||
return 10;
|
||||
@@ -1601,7 +1601,7 @@ bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array
|
||||
type->Proc.result_count = result_count;
|
||||
type->Proc.variadic = variadic;
|
||||
type->Proc.calling_convention = pt->calling_convention;
|
||||
type->Proc.is_generic = pt->generic;
|
||||
type->Proc.is_polymorphic = pt->generic;
|
||||
|
||||
if (param_count > 0) {
|
||||
Entity *end = params->Tuple.variables[param_count-1];
|
||||
@@ -1619,15 +1619,15 @@ bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array
|
||||
}
|
||||
|
||||
|
||||
bool is_generic = false;
|
||||
bool is_polymorphic = false;
|
||||
for (isize i = 0; i < param_count; i++) {
|
||||
Entity *e = params->Tuple.variables[i];
|
||||
if (e->type->kind == Type_Generic) {
|
||||
is_generic = true;
|
||||
is_polymorphic = true;
|
||||
}
|
||||
}
|
||||
if (operands == NULL) {
|
||||
GB_ASSERT(type->Proc.is_generic == is_generic);
|
||||
GB_ASSERT(type->Proc.is_polymorphic == is_polymorphic);
|
||||
}
|
||||
|
||||
|
||||
@@ -2219,8 +2219,11 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
||||
}
|
||||
}
|
||||
|
||||
if (is_type_gen_proc(type)) {
|
||||
error(e, "Invalid use of polymorphic procedure type");
|
||||
// if (is_type_polymorphic(type)) {
|
||||
if (is_type_poly_proc(type)) {
|
||||
gbString str = type_to_string(type);
|
||||
error(e, "Invalid use of a polymorphic type `%s`", str);
|
||||
gb_string_free(str);
|
||||
type = t_invalid;
|
||||
}
|
||||
|
||||
@@ -4265,8 +4268,8 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
||||
error(operand->expr, "Invalid argument to `type_of`");
|
||||
return false;
|
||||
}
|
||||
if (is_type_gen_proc(operand->type)) {
|
||||
error(operand->expr, "`type_of` of generic procedure cannot be determined");
|
||||
if (is_type_polymorphic(operand->type)) {
|
||||
error(operand->expr, "`type_of` of polymorphic type cannot be determined");
|
||||
return false;
|
||||
}
|
||||
operand->mode = Addressing_Type;
|
||||
@@ -4288,7 +4291,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
|
||||
return false;
|
||||
}
|
||||
Type *t = o.type;
|
||||
if (t == NULL || t == t_invalid || is_type_gen_proc(operand->type)) {
|
||||
if (t == NULL || t == t_invalid || is_type_polymorphic(operand->type)) {
|
||||
error(ce->args[0], "Invalid argument for `type_info`");
|
||||
return false;
|
||||
}
|
||||
@@ -4997,7 +5000,7 @@ Entity *find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity,
|
||||
}
|
||||
|
||||
TypeProc *pt = &base_type(base_entity->type)->Proc;
|
||||
if (!pt->is_generic || pt->is_generic_specialized) {
|
||||
if (!pt->is_polymorphic || pt->is_poly_specialized) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -5038,7 +5041,7 @@ Entity *find_or_generate_polymorphic_procedure(Checker *c, Entity *base_entity,
|
||||
ast_node(pl, ProcLit, proc_lit);
|
||||
// NOTE(bill): Associate the scope declared above with this procedure declaration's type
|
||||
add_scope(c, pl->type, final_proc_type->Proc.scope);
|
||||
final_proc_type->Proc.is_generic_specialized = true;
|
||||
final_proc_type->Proc.is_poly_specialized = true;
|
||||
|
||||
u64 tags = base_entity->Procedure.tags;
|
||||
AstNode *ident = clone_ast_node(a, base_entity->identifier);
|
||||
@@ -5169,7 +5172,7 @@ CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
|
||||
// NOTE(bill): Generate the procedure type for this generic instance
|
||||
ProcedureInfo proc_info = {};
|
||||
|
||||
if (pt->is_generic && !pt->is_generic_specialized) {
|
||||
if (pt->is_polymorphic && !pt->is_poly_specialized) {
|
||||
gen_entity = find_or_generate_polymorphic_procedure(c, entity, &operands, &proc_info);
|
||||
if (gen_entity != NULL) {
|
||||
GB_ASSERT(is_type_proc(gen_entity->type));
|
||||
@@ -5396,7 +5399,7 @@ CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
|
||||
}
|
||||
|
||||
Entity *gen_entity = NULL;
|
||||
if (pt->is_generic && err == CallArgumentError_None) {
|
||||
if (pt->is_polymorphic && err == CallArgumentError_None) {
|
||||
ProcedureInfo proc_info = {};
|
||||
gen_entity = find_or_generate_polymorphic_procedure(c, entity, &ordered_operands, &proc_info);
|
||||
if (gen_entity != NULL) {
|
||||
@@ -5418,7 +5421,7 @@ CALL_ARGUMENT_CHECKER(check_named_call_arguments) {
|
||||
Entity *e = pt->params->Tuple.variables[i];
|
||||
|
||||
if (e->kind == Entity_TypeName) {
|
||||
GB_ASSERT(pt->is_generic);
|
||||
GB_ASSERT(pt->is_polymorphic);
|
||||
if (o->mode != Addressing_Type) {
|
||||
if (show_error) {
|
||||
error(o->expr, "Expected a type for the argument `%.*s`", LIT(e->token.string));
|
||||
|
||||
+5
-7
@@ -1218,7 +1218,7 @@ void add_dependency_to_map(Map<Entity *> *map, CheckerInfo *info, Entity *entity
|
||||
return;
|
||||
}
|
||||
if (entity->type != NULL &&
|
||||
is_type_gen_proc(entity->type)) {
|
||||
is_type_polymorphic(entity->type)) {
|
||||
DeclInfo *decl = decl_info_of_entity(info, entity);
|
||||
if (decl->gen_proc_type == NULL) {
|
||||
return;
|
||||
@@ -1248,11 +1248,9 @@ Map<Entity *> generate_minimum_dependency_map(CheckerInfo *info, Entity *start)
|
||||
|
||||
for_array(i, info->definitions.entries) {
|
||||
Entity *e = info->definitions.entries[i].value;
|
||||
if (e->scope->is_global) {
|
||||
if (!is_type_gen_proc(e->type)) {
|
||||
// NOTE(bill): Require runtime stuff
|
||||
add_dependency_to_map(&map, info, e);
|
||||
}
|
||||
if (e->scope->is_global && !is_type_polymorphic(e->type)) { // TODO(bill): is the check enough?
|
||||
// NOTE(bill): Require runtime stuff
|
||||
add_dependency_to_map(&map, info, e);
|
||||
} else if (e->kind == Entity_Procedure) {
|
||||
if ((e->Procedure.tags & ProcTag_export) != 0) {
|
||||
add_dependency_to_map(&map, info, e);
|
||||
@@ -2252,7 +2250,7 @@ void check_parsed_files(Checker *c) {
|
||||
defer (c->context = prev_context);
|
||||
|
||||
TypeProc *pt = &pi->type->Proc;
|
||||
if (pt->is_generic) {
|
||||
if (pt->is_polymorphic) {
|
||||
if (pi->decl->gen_proc_type == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
+6
-6
@@ -2739,7 +2739,7 @@ irValue *ir_find_or_add_entity_string(irModule *m, String str) {
|
||||
|
||||
|
||||
|
||||
String ir_lookup_polymorphic_field(CheckerInfo *info, Type *dst, Type *src) {
|
||||
String ir_lookup_subtype_polymorphic_field(CheckerInfo *info, Type *dst, Type *src) {
|
||||
Type *prev_src = src;
|
||||
// Type *prev_dst = dst;
|
||||
src = base_type(type_deref(src));
|
||||
@@ -2760,7 +2760,7 @@ String ir_lookup_polymorphic_field(CheckerInfo *info, Type *dst, Type *src) {
|
||||
}
|
||||
}
|
||||
if (is_type_struct(f->type)) {
|
||||
String name = ir_lookup_polymorphic_field(info, dst, f->type);
|
||||
String name = ir_lookup_subtype_polymorphic_field(info, dst, f->type);
|
||||
if (name.len > 0) {
|
||||
return name;
|
||||
}
|
||||
@@ -2963,7 +2963,7 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) {
|
||||
bool dt_is_ptr = is_type_pointer(dt);
|
||||
|
||||
GB_ASSERT(is_type_struct(st) || is_type_union(st));
|
||||
String field_name = ir_lookup_polymorphic_field(proc->module->info, t, st);
|
||||
String field_name = ir_lookup_subtype_polymorphic_field(proc->module->info, t, st);
|
||||
// gb_printf("field_name: %.*s\n", LIT(field_name));
|
||||
if (field_name.len > 0) {
|
||||
// NOTE(bill): It can be casted
|
||||
@@ -3515,7 +3515,7 @@ String ir_mangle_name(irGen *s, String path, Entity *e) {
|
||||
isize base_len = ext-1-base;
|
||||
|
||||
isize max_len = base_len + 1 + 1 + 10 + 1 + name.len;
|
||||
bool require_suffix_id = check_is_entity_overloaded(e) || is_type_gen_proc(e->type);
|
||||
bool require_suffix_id = check_is_entity_overloaded(e) || is_type_poly_proc(e->type);
|
||||
if (require_suffix_id) {
|
||||
max_len += 21;
|
||||
}
|
||||
@@ -6024,7 +6024,7 @@ void ir_build_stmt_internal(irProcedure *proc, AstNode *node) {
|
||||
DeclInfo *decl = decl_info_of_entity(info, e);
|
||||
ast_node(pl, ProcLit, decl->proc_lit);
|
||||
if (pl->body != NULL) {
|
||||
if (is_type_gen_proc(e->type)) {
|
||||
if (is_type_poly_proc(e->type)) {
|
||||
auto found = *map_get(&info->gen_procs, hash_pointer(ident));
|
||||
for_array(i, found) {
|
||||
Entity *e = found[i];
|
||||
@@ -7373,7 +7373,7 @@ void ir_gen_tree(irGen *s) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!scope->is_global || is_type_gen_proc(e->type)) {
|
||||
if (!scope->is_global || is_type_poly_proc(e->type)) {
|
||||
if (e->kind == Entity_Procedure && (e->Procedure.tags & ProcTag_export) != 0) {
|
||||
} else if (e->kind == Entity_Procedure && e->Procedure.link_name.len > 0) {
|
||||
// Handle later
|
||||
|
||||
+1
-1
@@ -83,12 +83,12 @@ TOKEN_KIND(Token__ComparisonEnd, "_ComparisonEnd"), \
|
||||
TOKEN_KIND(Token__OperatorEnd, "_OperatorEnd"), \
|
||||
\
|
||||
TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
|
||||
TOKEN_KIND(Token_type, "type"), \
|
||||
TOKEN_KIND(Token_import, "import"), \
|
||||
TOKEN_KIND(Token_import_load, "import_load"), \
|
||||
TOKEN_KIND(Token_foreign, "foreign"), \
|
||||
TOKEN_KIND(Token_foreign_library, "foreign_library"), \
|
||||
TOKEN_KIND(Token_foreign_system_library, "foreign_system_library"), \
|
||||
TOKEN_KIND(Token_type, "type"), \
|
||||
TOKEN_KIND(Token_when, "when"), \
|
||||
TOKEN_KIND(Token_if, "if"), \
|
||||
TOKEN_KIND(Token_else, "else"), \
|
||||
|
||||
+26
-23
@@ -149,8 +149,8 @@ struct TypeRecord {
|
||||
bool variadic; \
|
||||
bool require_results; \
|
||||
bool c_vararg; \
|
||||
bool is_generic; \
|
||||
bool is_generic_specialized; \
|
||||
bool is_polymorphic; \
|
||||
bool is_poly_specialized; \
|
||||
ProcCallingConvention calling_convention; \
|
||||
}) \
|
||||
TYPE_KIND(Map, struct { \
|
||||
@@ -827,9 +827,9 @@ bool is_type_proc(Type *t) {
|
||||
t = base_type(t);
|
||||
return t->kind == Type_Proc;
|
||||
}
|
||||
bool is_type_gen_proc(Type *t) {
|
||||
bool is_type_poly_proc(Type *t) {
|
||||
t = base_type(t);
|
||||
return t->kind == Type_Proc && t->Proc.is_generic;
|
||||
return t->kind == Type_Proc && t->Proc.is_polymorphic;
|
||||
}
|
||||
Type *base_vector_type(Type *t) {
|
||||
if (is_type_vector(t)) {
|
||||
@@ -942,63 +942,66 @@ bool is_type_indexable(Type *t) {
|
||||
return is_type_array(t) || is_type_slice(t) || is_type_vector(t) || is_type_string(t);
|
||||
}
|
||||
|
||||
bool is_type_generic(Type *t) {
|
||||
bool is_type_polymorphic(Type *t) {
|
||||
t = core_type(t);
|
||||
switch (t->kind) {
|
||||
case Type_Generic:
|
||||
return true;
|
||||
|
||||
case Type_Pointer:
|
||||
return is_type_generic(t->Pointer.elem);
|
||||
return is_type_polymorphic(t->Pointer.elem);
|
||||
case Type_Atomic:
|
||||
return is_type_generic(t->Atomic.elem);
|
||||
return is_type_polymorphic(t->Atomic.elem);
|
||||
case Type_Array:
|
||||
return is_type_generic(t->Array.elem);
|
||||
return is_type_polymorphic(t->Array.elem);
|
||||
case Type_DynamicArray:
|
||||
return is_type_generic(t->DynamicArray.elem);
|
||||
return is_type_polymorphic(t->DynamicArray.elem);
|
||||
case Type_Vector:
|
||||
return is_type_generic(t->Vector.elem);
|
||||
return is_type_polymorphic(t->Vector.elem);
|
||||
case Type_Slice:
|
||||
return is_type_generic(t->Slice.elem);
|
||||
return is_type_polymorphic(t->Slice.elem);
|
||||
|
||||
case Type_Tuple:
|
||||
for (isize i = 0; i < t->Tuple.variable_count; i++) {
|
||||
if (is_type_generic(t->Tuple.variables[i]->type)) {
|
||||
if (is_type_polymorphic(t->Tuple.variables[i]->type)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Type_Proc:
|
||||
if (t->Proc.param_count > 0 &&
|
||||
is_type_generic(t->Proc.params)) {
|
||||
return true;
|
||||
}
|
||||
if (t->Proc.result_count > 0 &&
|
||||
is_type_generic(t->Proc.results)) {
|
||||
if (t->Proc.is_polymorphic) {
|
||||
return true;
|
||||
}
|
||||
// if (t->Proc.param_count > 0 &&
|
||||
// is_type_polymorphic(t->Proc.params)) {
|
||||
// return true;
|
||||
// }
|
||||
// if (t->Proc.result_count > 0 &&
|
||||
// is_type_polymorphic(t->Proc.results)) {
|
||||
// return true;
|
||||
// }
|
||||
break;
|
||||
|
||||
// case Type_Record:
|
||||
// GB_ASSERT(t->Record.kind != TypeRecord_Enum);
|
||||
// for (isize i = 0; i < t->Record.field_count; i++) {
|
||||
// if (is_type_generic(t->Record.fields[i]->type)) {
|
||||
// if (is_type_polymorphic(t->Record.fields[i]->type)) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// for (isize i = 1; i < t->Record.variant_count; i++) {
|
||||
// if (is_type_generic(t->Record.variants[i]->type)) {
|
||||
// if (is_type_polymorphic(t->Record.variants[i]->type)) {
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
|
||||
case Type_Map:
|
||||
if (is_type_generic(t->Map.key)) {
|
||||
if (is_type_polymorphic(t->Map.key)) {
|
||||
return true;
|
||||
}
|
||||
if (is_type_generic(t->Map.value)) {
|
||||
if (is_type_polymorphic(t->Map.value)) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
@@ -1347,7 +1350,7 @@ ProcTypeOverloadKind are_proc_types_overload_safe(Type *x, Type *y) {
|
||||
// return ProcOverload_CallingConvention;
|
||||
// }
|
||||
|
||||
if (px.is_generic != py.is_generic) {
|
||||
if (px.is_polymorphic != py.is_polymorphic) {
|
||||
return ProcOverload_Polymorphic;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user