mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
Add #any_int directive to replace auto_cast uses on parameters.
This commit is contained in:
+5
-5
@@ -222,10 +222,10 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
make_slice :: proc($T: typeid/[]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
return make_aligned(T, len, align_of(E), allocator, loc);
|
return make_aligned(T, len, align_of(E), allocator, loc);
|
||||||
}
|
}
|
||||||
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
runtime.make_slice_error_loc(loc, len);
|
runtime.make_slice_error_loc(loc, len);
|
||||||
data := alloc(size_of(E)*len, alignment, allocator, loc);
|
data := alloc(size_of(E)*len, alignment, allocator, loc);
|
||||||
if data == nil && size_of(E) != 0 {
|
if data == nil && size_of(E) != 0 {
|
||||||
@@ -238,10 +238,10 @@ make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, alloca
|
|||||||
make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> T {
|
make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
return make_dynamic_array_len_cap(T, 0, 16, allocator, loc);
|
return make_dynamic_array_len_cap(T, 0, 16, allocator, loc);
|
||||||
}
|
}
|
||||||
make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
return make_dynamic_array_len_cap(T, len, len, allocator, loc);
|
return make_dynamic_array_len_cap(T, len, len, allocator, loc);
|
||||||
}
|
}
|
||||||
make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, auto_cast cap: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
runtime.make_dynamic_array_error_loc(loc, len, cap);
|
runtime.make_dynamic_array_error_loc(loc, len, cap);
|
||||||
data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
||||||
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
||||||
@@ -251,7 +251,7 @@ make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, a
|
|||||||
zero(data, size_of(E)*len);
|
zero(data, size_of(E)*len);
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T {
|
make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
runtime.make_map_expr_error_loc(loc, cap);
|
runtime.make_map_expr_error_loc(loc, cap);
|
||||||
context.allocator = allocator;
|
context.allocator = allocator;
|
||||||
|
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
|
|||||||
|
|
||||||
DEFAULT_RESERVE_CAPACITY :: 16;
|
DEFAULT_RESERVE_CAPACITY :: 16;
|
||||||
|
|
||||||
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
make_aligned :: proc($T: typeid/[]$E, #any_int len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
||||||
make_slice_error_loc(loc, len);
|
make_slice_error_loc(loc, len);
|
||||||
data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc);
|
data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc);
|
||||||
if data == nil && size_of(E) != 0 {
|
if data == nil && size_of(E) != 0 {
|
||||||
@@ -197,7 +197,7 @@ make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, alloca
|
|||||||
}
|
}
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
make_slice :: proc($T: typeid/[]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
make_slice :: proc($T: typeid/[]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
||||||
return make_aligned(T, len, align_of(E), allocator, loc);
|
return make_aligned(T, len, align_of(E), allocator, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,12 +207,12 @@ make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocato
|
|||||||
}
|
}
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, auto_cast len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, #any_int len: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
||||||
return make_dynamic_array_len_cap(T, len, len, allocator, loc);
|
return make_dynamic_array_len_cap(T, len, len, allocator, loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, auto_cast cap: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, #any_int len: int, #any_int cap: int, allocator := context.allocator, loc := #caller_location) -> (T, Allocator_Error) #optional_second {
|
||||||
make_dynamic_array_error_loc(loc, len, cap);
|
make_dynamic_array_error_loc(loc, len, cap);
|
||||||
data, err := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
data, err := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
||||||
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
||||||
@@ -223,7 +223,7 @@ make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, a
|
|||||||
}
|
}
|
||||||
|
|
||||||
@builtin
|
@builtin
|
||||||
make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
|
make_map :: proc($T: typeid/map[$K]$E, #any_int cap: int = DEFAULT_RESERVE_CAPACITY, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
make_map_expr_error_loc(loc, cap);
|
make_map_expr_error_loc(loc, cap);
|
||||||
context.allocator = allocator;
|
context.allocator = allocator;
|
||||||
|
|
||||||
|
|||||||
@@ -4485,6 +4485,10 @@ CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
|
|||||||
bool ok = false;
|
bool ok = false;
|
||||||
if (e->flags & EntityFlag_AutoCast) {
|
if (e->flags & EntityFlag_AutoCast) {
|
||||||
ok = check_is_castable_to(c, &o, t);
|
ok = check_is_castable_to(c, &o, t);
|
||||||
|
} else if (e->flags & EntityFlag_AnyInt) {
|
||||||
|
if (is_type_integer(t)) {
|
||||||
|
ok = check_is_castable_to(c, &o, t);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (ok) {
|
if (ok) {
|
||||||
s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
|
s = assign_score_function(MAXIMUM_TYPE_DISTANCE);
|
||||||
@@ -8730,6 +8734,12 @@ gbString write_expr_to_string(gbString str, Ast *node, bool shorthand) {
|
|||||||
if (f->flags&FieldFlag_auto_cast) {
|
if (f->flags&FieldFlag_auto_cast) {
|
||||||
str = gb_string_appendc(str, "auto_cast ");
|
str = gb_string_appendc(str, "auto_cast ");
|
||||||
}
|
}
|
||||||
|
if (f->flags&FieldFlag_any_int) {
|
||||||
|
str = gb_string_appendc(str, "#any_int ");
|
||||||
|
}
|
||||||
|
if (f->flags&FieldFlag_const) {
|
||||||
|
str = gb_string_appendc(str, "#const ");
|
||||||
|
}
|
||||||
|
|
||||||
for_array(i, f->names) {
|
for_array(i, f->names) {
|
||||||
Ast *name = f->names[i];
|
Ast *name = f->names[i];
|
||||||
|
|||||||
@@ -1526,6 +1526,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
|
|||||||
error(name, "'#const' can only be applied to variable fields");
|
error(name, "'#const' can only be applied to variable fields");
|
||||||
p->flags &= ~FieldFlag_const;
|
p->flags &= ~FieldFlag_const;
|
||||||
}
|
}
|
||||||
|
if (p->flags&FieldFlag_any_int) {
|
||||||
|
error(name, "'#const' can only be applied to variable fields");
|
||||||
|
p->flags &= ~FieldFlag_any_int;
|
||||||
|
}
|
||||||
|
|
||||||
param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
|
param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
|
||||||
param->TypeName.is_type_alias = true;
|
param->TypeName.is_type_alias = true;
|
||||||
@@ -1572,6 +1576,12 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
|
|||||||
if (!check_is_castable_to(ctx, &op, type)) {
|
if (!check_is_castable_to(ctx, &op, type)) {
|
||||||
ok = false;
|
ok = false;
|
||||||
}
|
}
|
||||||
|
} else if (p->flags&FieldFlag_any_int) {
|
||||||
|
if (!is_type_integer(op.type) || !is_type_integer(type)) {
|
||||||
|
ok = false;
|
||||||
|
} else if (!check_is_castable_to(ctx, &op, type)) {
|
||||||
|
ok = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
success = false;
|
success = false;
|
||||||
@@ -1609,6 +1619,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
|
|||||||
error(name, "'auto_cast' can only be applied to variable fields");
|
error(name, "'auto_cast' can only be applied to variable fields");
|
||||||
p->flags &= ~FieldFlag_auto_cast;
|
p->flags &= ~FieldFlag_auto_cast;
|
||||||
}
|
}
|
||||||
|
if (p->flags&FieldFlag_any_int) {
|
||||||
|
error(name, "'#any_int' can only be applied to variable fields");
|
||||||
|
p->flags &= ~FieldFlag_any_int;
|
||||||
|
}
|
||||||
if (p->flags&FieldFlag_const) {
|
if (p->flags&FieldFlag_const) {
|
||||||
error(name, "'#const' can only be applied to variable fields");
|
error(name, "'#const' can only be applied to variable fields");
|
||||||
p->flags &= ~FieldFlag_const;
|
p->flags &= ~FieldFlag_const;
|
||||||
@@ -1632,6 +1646,14 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
|
|||||||
if (p->flags&FieldFlag_auto_cast) {
|
if (p->flags&FieldFlag_auto_cast) {
|
||||||
param->flags |= EntityFlag_AutoCast;
|
param->flags |= EntityFlag_AutoCast;
|
||||||
}
|
}
|
||||||
|
if (p->flags&FieldFlag_any_int) {
|
||||||
|
if (!is_type_integer(param->type)) {
|
||||||
|
gbString str = type_to_string(param->type);
|
||||||
|
error(name, "A parameter with '#any_int' must be an integer, got %s", str);
|
||||||
|
gb_string_free(str);
|
||||||
|
}
|
||||||
|
param->flags |= EntityFlag_AnyInt;
|
||||||
|
}
|
||||||
if (p->flags&FieldFlag_const) {
|
if (p->flags&FieldFlag_const) {
|
||||||
param->flags |= EntityFlag_ConstInput;
|
param->flags |= EntityFlag_ConstInput;
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -62,11 +62,12 @@ enum EntityFlag : u64 {
|
|||||||
|
|
||||||
EntityFlag_CVarArg = 1ull<<22,
|
EntityFlag_CVarArg = 1ull<<22,
|
||||||
EntityFlag_AutoCast = 1ull<<23,
|
EntityFlag_AutoCast = 1ull<<23,
|
||||||
|
EntityFlag_AnyInt = 1ull<<24,
|
||||||
|
|
||||||
EntityFlag_Disabled = 1ull<<24,
|
EntityFlag_Disabled = 1ull<<25,
|
||||||
EntityFlag_Cold = 1ull<<25, // procedure is rarely called
|
EntityFlag_Cold = 1ull<<26, // procedure is rarely called
|
||||||
|
|
||||||
EntityFlag_Lazy = 1ull<<26, // Lazily type checked
|
EntityFlag_Lazy = 1ull<<27, // Lazily type checked
|
||||||
|
|
||||||
EntityFlag_Test = 1ull<<30,
|
EntityFlag_Test = 1ull<<30,
|
||||||
|
|
||||||
|
|||||||
+9
-2
@@ -3402,6 +3402,7 @@ enum FieldPrefixKind {
|
|||||||
FieldPrefix_no_alias,
|
FieldPrefix_no_alias,
|
||||||
FieldPrefix_c_var_arg,
|
FieldPrefix_c_var_arg,
|
||||||
FieldPrefix_auto_cast,
|
FieldPrefix_auto_cast,
|
||||||
|
FieldPrefix_any_int,
|
||||||
};
|
};
|
||||||
|
|
||||||
FieldPrefixKind is_token_field_prefix(AstFile *f) {
|
FieldPrefixKind is_token_field_prefix(AstFile *f) {
|
||||||
@@ -3425,6 +3426,8 @@ FieldPrefixKind is_token_field_prefix(AstFile *f) {
|
|||||||
return FieldPrefix_c_var_arg;
|
return FieldPrefix_c_var_arg;
|
||||||
} else if (f->curr_token.string == "const") {
|
} else if (f->curr_token.string == "const") {
|
||||||
return FieldPrefix_const;
|
return FieldPrefix_const;
|
||||||
|
} else if (f->curr_token.string == "any_int") {
|
||||||
|
return FieldPrefix_any_int;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -3440,6 +3443,7 @@ u32 parse_field_prefixes(AstFile *f) {
|
|||||||
i32 c_vararg_count = 0;
|
i32 c_vararg_count = 0;
|
||||||
i32 auto_cast_count = 0;
|
i32 auto_cast_count = 0;
|
||||||
i32 const_count = 0;
|
i32 const_count = 0;
|
||||||
|
i32 any_int_count = 0;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
FieldPrefixKind kind = is_token_field_prefix(f);
|
FieldPrefixKind kind = is_token_field_prefix(f);
|
||||||
@@ -3457,7 +3461,8 @@ u32 parse_field_prefixes(AstFile *f) {
|
|||||||
case FieldPrefix_no_alias: no_alias_count += 1; advance_token(f); break;
|
case FieldPrefix_no_alias: no_alias_count += 1; advance_token(f); break;
|
||||||
case FieldPrefix_c_var_arg: c_vararg_count += 1; advance_token(f); break;
|
case FieldPrefix_c_var_arg: c_vararg_count += 1; advance_token(f); break;
|
||||||
case FieldPrefix_auto_cast: auto_cast_count += 1; advance_token(f); break;
|
case FieldPrefix_auto_cast: auto_cast_count += 1; advance_token(f); break;
|
||||||
case FieldPrefix_const: const_count += 1; advance_token(f); break;
|
case FieldPrefix_const: const_count += 1; advance_token(f); break;
|
||||||
|
case FieldPrefix_any_int: any_int_count += 1; advance_token(f); break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (using_count > 1) syntax_error(f->curr_token, "Multiple 'using' in this field list");
|
if (using_count > 1) syntax_error(f->curr_token, "Multiple 'using' in this field list");
|
||||||
@@ -3465,6 +3470,7 @@ u32 parse_field_prefixes(AstFile *f) {
|
|||||||
if (c_vararg_count > 1) syntax_error(f->curr_token, "Multiple '#c_vararg' in this field list");
|
if (c_vararg_count > 1) syntax_error(f->curr_token, "Multiple '#c_vararg' in this field list");
|
||||||
if (auto_cast_count > 1) syntax_error(f->curr_token, "Multiple 'auto_cast' in this field list");
|
if (auto_cast_count > 1) syntax_error(f->curr_token, "Multiple 'auto_cast' in this field list");
|
||||||
if (const_count > 1) syntax_error(f->curr_token, "Multiple '#const' in this field list");
|
if (const_count > 1) syntax_error(f->curr_token, "Multiple '#const' in this field list");
|
||||||
|
if (any_int_count > 1) syntax_error(f->curr_token, "Multiple '#any_int' in this field list");
|
||||||
|
|
||||||
|
|
||||||
u32 field_flags = 0;
|
u32 field_flags = 0;
|
||||||
@@ -3472,7 +3478,8 @@ u32 parse_field_prefixes(AstFile *f) {
|
|||||||
if (no_alias_count > 0) field_flags |= FieldFlag_no_alias;
|
if (no_alias_count > 0) field_flags |= FieldFlag_no_alias;
|
||||||
if (c_vararg_count > 0) field_flags |= FieldFlag_c_vararg;
|
if (c_vararg_count > 0) field_flags |= FieldFlag_c_vararg;
|
||||||
if (auto_cast_count > 0) field_flags |= FieldFlag_auto_cast;
|
if (auto_cast_count > 0) field_flags |= FieldFlag_auto_cast;
|
||||||
if (const_count > 0) field_flags |= FieldFlag_const;
|
if (const_count > 0) field_flags |= FieldFlag_const;
|
||||||
|
if (any_int_count > 0) field_flags |= FieldFlag_any_int;
|
||||||
return field_flags;
|
return field_flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -275,6 +275,7 @@ enum FieldFlag {
|
|||||||
FieldFlag_c_vararg = 1<<3,
|
FieldFlag_c_vararg = 1<<3,
|
||||||
FieldFlag_auto_cast = 1<<4,
|
FieldFlag_auto_cast = 1<<4,
|
||||||
FieldFlag_const = 1<<5,
|
FieldFlag_const = 1<<5,
|
||||||
|
FieldFlag_any_int = 1<<6,
|
||||||
|
|
||||||
FieldFlag_Tags = 1<<10,
|
FieldFlag_Tags = 1<<10,
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user