Bladeren bron

Add `#any_int` directive to replace `auto_cast` uses on parameters.

gingerBill 4 jaren geleden
bovenliggende
commit
7bbc9a4634
7 gewijzigde bestanden met toevoegingen van 56 en 15 verwijderingen
  1. 5 5
      core/mem/alloc.odin
  2. 5 5
      core/runtime/core_builtin.odin
  3. 10 0
      src/check_expr.cpp
  4. 22 0
      src/check_type.cpp
  5. 4 3
      src/entity.cpp
  6. 9 2
      src/parser.cpp
  7. 1 0
      src/parser.hpp

+ 5 - 5
core/mem/alloc.odin

@@ -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);
 }
-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);
 	data := alloc(size_of(E)*len, alignment, allocator, loc);
 	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 {
 	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);
 }
-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);
 	data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
 	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);
 	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);
 	context.allocator = allocator;
 

+ 5 - 5
core/runtime/core_builtin.odin

@@ -186,7 +186,7 @@ new_clone :: proc(data: $T, allocator := context.allocator, loc := #caller_locat
 
 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);
 	data, err := mem_alloc_bytes(size_of(E)*len, alignment, allocator, loc);
 	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
-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);
 }
 
@@ -207,12 +207,12 @@ make_dynamic_array :: proc($T: typeid/[dynamic]$E, allocator := context.allocato
 }
 
 @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);
 }
 
 @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);
 	data, err := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
 	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
-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);
 	context.allocator = allocator;
 

+ 10 - 0
src/check_expr.cpp

@@ -4485,6 +4485,10 @@ CALL_ARGUMENT_CHECKER(check_call_arguments_internal) {
 					bool ok = false;
 					if (e->flags & EntityFlag_AutoCast) {
 						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) {
 						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) {
 			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) {
 			Ast *name = f->names[i];

+ 22 - 0
src/check_type.cpp

@@ -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");
 					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->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)) {
 								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) {
 							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");
 						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) {
 						error(name, "'#const' can only be applied to variable fields");
 						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) {
 				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) {
 				param->flags |= EntityFlag_ConstInput;
 			}

+ 4 - 3
src/entity.cpp

@@ -62,11 +62,12 @@ enum EntityFlag : u64 {
 
 	EntityFlag_CVarArg       = 1ull<<22,
 	EntityFlag_AutoCast      = 1ull<<23,
+	EntityFlag_AnyInt        = 1ull<<24,
 
-	EntityFlag_Disabled      = 1ull<<24,
-	EntityFlag_Cold          = 1ull<<25, // procedure is rarely called
+	EntityFlag_Disabled      = 1ull<<25,
+	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,
 

+ 9 - 2
src/parser.cpp

@@ -3402,6 +3402,7 @@ enum FieldPrefixKind {
 	FieldPrefix_no_alias,
 	FieldPrefix_c_var_arg,
 	FieldPrefix_auto_cast,
+	FieldPrefix_any_int,
 };
 
 FieldPrefixKind is_token_field_prefix(AstFile *f) {
@@ -3425,6 +3426,8 @@ FieldPrefixKind is_token_field_prefix(AstFile *f) {
 				return FieldPrefix_c_var_arg;
 			} else if (f->curr_token.string == "const") {
 				return FieldPrefix_const;
+			} else if (f->curr_token.string == "any_int") {
+				return FieldPrefix_any_int;
 			}
 			break;
 		}
@@ -3440,6 +3443,7 @@ u32 parse_field_prefixes(AstFile *f) {
 	i32 c_vararg_count  = 0;
 	i32 auto_cast_count = 0;
 	i32 const_count     = 0;
+	i32 any_int_count   = 0;
 
 	for (;;) {
 		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_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_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");
@@ -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 (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 (any_int_count > 1)   syntax_error(f->curr_token, "Multiple '#any_int' in this field list");
 
 
 	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 (c_vararg_count  > 0) field_flags |= FieldFlag_c_vararg;
 	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;
 }
 

+ 1 - 0
src/parser.hpp

@@ -275,6 +275,7 @@ enum FieldFlag {
 	FieldFlag_c_vararg  = 1<<3,
 	FieldFlag_auto_cast = 1<<4,
 	FieldFlag_const     = 1<<5,
+	FieldFlag_any_int   = 1<<6,
 
 	FieldFlag_Tags = 1<<10,