aboutsummaryrefslogtreecommitdiff
path: root/src/check_type.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/check_type.cpp')
-rw-r--r--src/check_type.cpp78
1 files changed, 67 insertions, 11 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp
index bc89a9be9..da0a9706b 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -695,11 +695,6 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Array<Op
error(ut->align, "A union with #no_nil must have at least 2 variants");
}
break;
- case UnionType_maybe:
- if (variants.count != 1) {
- error(ut->align, "A union with #maybe must have at 1 variant, got %lld", cast(long long)variants.count);
- }
- break;
}
if (ut->align != nullptr) {
@@ -1345,7 +1340,9 @@ ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type *
param_value.kind = ParameterValue_Constant;
param_value.value = o.value;
} else {
- error(expr, "Default parameter must be a constant, %d", o.mode);
+ gbString s = expr_to_string(o.expr);
+ error(expr, "Default parameter must be a constant, got %s", s);
+ gb_string_free(s);
}
}
} else {
@@ -1614,6 +1611,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
error(name, "'#any_int' can only be applied to variable fields");
p->flags &= ~FieldFlag_any_int;
}
+ if (p->flags&FieldFlag_by_ptr) {
+ error(name, "'#by_ptr' can only be applied to variable fields");
+ p->flags &= ~FieldFlag_by_ptr;
+ }
param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
param->TypeName.is_type_alias = true;
@@ -1690,10 +1691,17 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
if (p->flags&FieldFlag_no_alias) {
if (!is_type_pointer(type)) {
- error(name, "'#no_alias' can only be applied to fields of pointer type");
+ error(name, "'#no_alias' can only be applied pointer typed parameters");
p->flags &= ~FieldFlag_no_alias; // Remove the flag
}
}
+ if (p->flags&FieldFlag_by_ptr) {
+ if (is_type_internally_pointer_like(type)) {
+ error(name, "'#by_ptr' can only be applied to non-pointer-like parameters");
+ p->flags &= ~FieldFlag_by_ptr; // Remove the flag
+ }
+ }
+
if (is_poly_name) {
if (p->flags&FieldFlag_no_alias) {
error(name, "'#no_alias' can only be applied to non constant values");
@@ -1711,6 +1719,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_by_ptr) {
+ error(name, "'#by_ptr' can only be applied to variable fields");
+ p->flags &= ~FieldFlag_by_ptr;
+ }
if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
gbString str = type_to_string(type);
@@ -1743,6 +1755,9 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
if (p->flags&FieldFlag_const) {
param->flags |= EntityFlag_ConstInput;
}
+ if (p->flags&FieldFlag_by_ptr) {
+ param->flags |= EntityFlag_ByPtr;
+ }
param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
add_entity(ctx, scope, name, param);
@@ -2678,14 +2693,55 @@ bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_t
case_ast_node(ue, UnaryExpr, e);
switch (ue->op.kind) {
case Token_Pointer:
- *type = alloc_type_pointer(check_type(ctx, ue->expr));
- set_base_type(named_type, *type);
- return true;
+ {
+ Type *elem = check_type(ctx, ue->expr);
+ *type = alloc_type_pointer(elem);
+ set_base_type(named_type, *type);
+ return true;
+ }
}
case_end;
case_ast_node(pt, PointerType, e);
- *type = alloc_type_pointer(check_type(ctx, pt->type));
+ CheckerContext c = *ctx;
+ c.type_path = new_checker_type_path();
+ defer (destroy_checker_type_path(c.type_path));
+
+ Type *elem = t_invalid;
+ Operand o = {};
+ check_expr_or_type(&c, &o, pt->type);
+ if (o.mode != Addressing_Invalid && o.mode != Addressing_Type) {
+ // NOTE(bill): call check_type_expr again to get a consistent error message
+ begin_error_block();
+ elem = check_type_expr(&c, pt->type, nullptr);
+ if (o.mode == Addressing_Variable) {
+ gbString s = expr_to_string(pt->type);
+ error_line("\tSuggestion: ^ is used for pointer types, did you mean '&%s'?\n", s);
+ gb_string_free(s);
+ }
+ end_error_block();
+ } else {
+ elem = o.type;
+ }
+
+ if (pt->tag != nullptr) {
+ GB_ASSERT(pt->tag->kind == Ast_BasicDirective);
+ String name = pt->tag->BasicDirective.name.string;
+ if (name == "soa") {
+ // TODO(bill): generic #soa pointers
+ if (is_type_soa_struct(elem)) {
+ *type = alloc_type_soa_pointer(elem);
+ } else {
+ error(pt->tag, "#soa pointers require an #soa record type as the element");
+ *type = alloc_type_pointer(elem);
+ }
+ } else {
+ error(pt->tag, "Invalid tag applied to pointer, got #%.*s", LIT(name));
+ *type = alloc_type_pointer(elem);
+ }
+ } else {
+ *type = alloc_type_pointer(elem);
+ }
set_base_type(named_type, *type);
return true;
case_end;