aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-10-27 19:54:48 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-10-27 20:35:12 -0400
commit41c0ea851a542bcd7d54b8c5c06d70e1e00095e1 (patch)
tree6880cd0c58c10545a9723115f712d86e12224347
parentdf258c1773adef59994da2e010f74610eb81a1b2 (diff)
Deprecate "&" for stack references
-rw-r--r--ast.c5
-rw-r--r--ast.h6
-rw-r--r--compile.c37
-rw-r--r--parse.c41
-rw-r--r--repl.c2
-rw-r--r--typecheck.c59
-rw-r--r--types.c12
-rw-r--r--types.h4
8 files changed, 46 insertions, 120 deletions
diff --git a/ast.c b/ast.c
index af0aea5a..0593ccbf 100644
--- a/ast.c
+++ b/ast.c
@@ -113,7 +113,6 @@ CORD ast_to_xml(ast_t *ast)
T(Negative, "<Negative>%r</Negative>", ast_to_xml(data.value))
T(Not, "<Not>%r</Not>", ast_to_xml(data.value))
T(HeapAllocate, "<HeapAllocate>%r</HeapAllocate>", ast_to_xml(data.value))
- T(StackReference, "<StackReference>%r</StackReference>", ast_to_xml(data.value))
T(Min, "<Min>%r%r%r</Min>", ast_to_xml(data.lhs), ast_to_xml(data.rhs), optional_tagged("key", data.key))
T(Max, "<Max>%r%r%r</Max>", ast_to_xml(data.lhs), ast_to_xml(data.rhs), optional_tagged("key", data.key))
T(Array, "<Array>%r%r</Array>", optional_tagged_type("item-type", data.item_type), ast_list_to_xml(data.items))
@@ -172,8 +171,8 @@ CORD type_ast_to_xml(type_ast_t *t)
#define T(type, ...) case type: { auto data = t->__data.type; (void)data; return CORD_asprintf(__VA_ARGS__); }
T(UnknownTypeAST, "<UnknownType/>")
T(VarTypeAST, "%s", data.name)
- T(PointerTypeAST, "<PointerType is_stack=\"%s\">%r</PointerType>",
- data.is_stack ? "yes" : "no", type_ast_to_xml(data.pointed))
+ T(PointerTypeAST, "<PointerType is_view=\"%s\">%r</PointerType>",
+ data.is_view ? "yes" : "no", type_ast_to_xml(data.pointed))
T(ArrayTypeAST, "<ArrayType>%r</ArrayType>", type_ast_to_xml(data.item))
T(SetTypeAST, "<TableType>%r</TableType>", type_ast_to_xml(data.item))
T(ChannelTypeAST, "<ChannelType>%r</ChannelType>", type_ast_to_xml(data.item))
diff --git a/ast.h b/ast.h
index 4b8e03dc..fada01d4 100644
--- a/ast.h
+++ b/ast.h
@@ -100,7 +100,7 @@ struct type_ast_s {
} VarTypeAST;
struct {
type_ast_t *pointed;
- bool is_stack:1;
+ bool is_view:1;
} PointerTypeAST;
struct {
type_ast_t *item;
@@ -128,7 +128,7 @@ typedef enum {
TextLiteral, TextJoin, PrintStatement,
Declare, Assign,
BinaryOp, UpdateAssign,
- Not, Negative, HeapAllocate, StackReference,
+ Not, Negative, HeapAllocate,
Min, Max,
Array, Channel, Set, Table, TableEntry, Comprehension,
FunctionDef, Lambda,
@@ -195,7 +195,7 @@ struct ast_s {
} BinaryOp, UpdateAssign;
struct {
ast_t *value;
- } Not, Negative, HeapAllocate, StackReference;
+ } Not, Negative, HeapAllocate;
struct {
ast_t *lhs, *rhs, *key;
} Min, Max;
diff --git a/compile.c b/compile.c
index c88e2a7f..a0069b34 100644
--- a/compile.c
+++ b/compile.c
@@ -506,8 +506,6 @@ CORD compile_statement(env_t *env, ast_t *ast)
if (!assign->targets->next && assign->targets->ast->tag == Var && is_idempotent(assign->targets->ast)) {
// Common case: assigning to one variable:
type_t *lhs_t = get_type(env, assign->targets->ast);
- if (lhs_t->tag == PointerType && Match(lhs_t, PointerType)->is_stack)
- code_err(test->expr, "Stack references cannot be assigned to local variables because the variable may outlive the stack memory.");
env_t *val_scope = with_enum_scope(env, lhs_t);
type_t *rhs_t = get_type(val_scope, assign->values->ast);
CORD value;
@@ -535,8 +533,6 @@ CORD compile_statement(env_t *env, ast_t *ast)
int64_t i = 1;
for (ast_list_t *target = assign->targets, *value = assign->values; target && value; target = target->next, value = value->next) {
type_t *target_type = get_type(env, target->ast);
- if (target_type->tag == PointerType && Match(target_type, PointerType)->is_stack)
- code_err(ast, "Stack references cannot be assigned to local variables because the variable may outlive the stack memory.");
env_t *val_scope = with_enum_scope(env, target_type);
type_t *value_type = get_type(val_scope, value->ast);
CORD val_code;
@@ -609,8 +605,6 @@ CORD compile_statement(env_t *env, ast_t *ast)
// Single assignment, no temp vars needed:
if (assign->targets && !assign->targets->next && is_idempotent(assign->targets->ast)) {
type_t *lhs_t = get_type(env, assign->targets->ast);
- if (lhs_t->tag == PointerType && Match(lhs_t, PointerType)->is_stack)
- code_err(ast, "Stack references cannot be assigned to local variables because the variable may outlive the stack memory.");
env_t *val_env = with_enum_scope(env, lhs_t);
type_t *rhs_t = get_type(val_env, assign->values->ast);
CORD val;
@@ -628,8 +622,6 @@ CORD compile_statement(env_t *env, ast_t *ast)
int64_t i = 1;
for (ast_list_t *value = assign->values, *target = assign->targets; value && target; value = value->next, target = target->next) {
type_t *lhs_t = get_type(env, target->ast);
- if (lhs_t->tag == PointerType && Match(lhs_t, PointerType)->is_stack)
- code_err(ast, "Stack references cannot be assigned to local variables because the variable may outlive the stack memory.");
env_t *val_env = with_enum_scope(env, lhs_t);
type_t *rhs_t = get_type(val_env, value->ast);
CORD val;
@@ -1518,7 +1510,7 @@ CORD compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool
val = CORD_all("(&", val, ")");
else
code_err(ast, "This should be a pointer, not %T", get_type(env, ast));
- t = Type(PointerType, .pointed=t, .is_stack=true);
+ t = Type(PointerType, .pointed=t, .is_view=true);
++depth;
} else {
auto ptr = Match(t, PointerType);
@@ -1978,13 +1970,6 @@ CORD compile(env_t *env, ast_t *ast)
}
// TODO: for constructors, do new(T, ...) instead of heap((T){...})
case HeapAllocate: return CORD_asprintf("heap(%r)", compile(env, Match(ast, HeapAllocate)->value));
- case StackReference: {
- ast_t *subject = Match(ast, StackReference)->value;
- if (can_be_mutated(env, subject))
- return CORD_all("(&", compile_lvalue(env, subject), ")");
- else
- code_err(subject, "This subject can't be mutated!");
- }
case Optional: {
ast_t *value = Match(ast, Optional)->value;
CORD value_code = compile(env, value);
@@ -2352,7 +2337,7 @@ CORD compile(env_t *env, ast_t *ast)
static int64_t comp_num = 1;
const char *comprehension_name = heap_strf("arr$%ld", comp_num++);
ast_t *comprehension_var = FakeAST(InlineCCode, .code=CORD_all("&", comprehension_name),
- .type=Type(PointerType, .pointed=array_type, .is_stack=true));
+ .type=Type(PointerType, .pointed=array_type, .is_view=true));
Closure_t comp_action = {.fn=add_to_array_comprehension, .userdata=comprehension_var};
scope->comprehension_action = &comp_action;
CORD code = CORD_all("({ Array_t ", comprehension_name, " = {};");
@@ -2431,7 +2416,7 @@ CORD compile(env_t *env, ast_t *ast)
env_t *scope = fresh_scope(env);
const char *comprehension_name = heap_strf("table$%ld", comp_num++);
ast_t *comprehension_var = FakeAST(InlineCCode, .code=CORD_all("&", comprehension_name),
- .type=Type(PointerType, .pointed=table_type, .is_stack=true));
+ .type=Type(PointerType, .pointed=table_type, .is_view=true));
CORD code = CORD_all("({ Table_t ", comprehension_name, " = {");
if (table->fallback)
@@ -2485,7 +2470,7 @@ CORD compile(env_t *env, ast_t *ast)
env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : fresh_scope(env);
const char *comprehension_name = heap_strf("set$%ld", comp_num++);
ast_t *comprehension_var = FakeAST(InlineCCode, .code=CORD_all("&", comprehension_name),
- .type=Type(PointerType, .pointed=set_type, .is_stack=true));
+ .type=Type(PointerType, .pointed=set_type, .is_view=true));
CORD code = CORD_all("({ Table_t ", comprehension_name, " = {};");
Closure_t comp_action = {.fn=add_to_set_comprehension, .userdata=comprehension_var};
scope->comprehension_action = &comp_action;
@@ -2675,7 +2660,7 @@ CORD compile(env_t *env, ast_t *ast)
: compile_to_pointer_depth(env, call->self, 0, false);
CORD comparison;
if (call->args) {
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
arg_t *arg_spec = new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t));
@@ -2688,7 +2673,7 @@ CORD compile(env_t *env, ast_t *ast)
CORD self = compile_to_pointer_depth(env, call->self, 1, false);
CORD comparison;
if (call->args) {
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
arg_t *arg_spec = new(arg_t, .name="by", .type=Type(ClosureType, .fn=fn_t));
@@ -2699,7 +2684,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all("Array$heapify(", self, ", ", comparison, ", ", padded_item_size, ")");
} else if (streq(call->name, "heap_push")) {
CORD self = compile_to_pointer_depth(env, call->self, 1, false);
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
ast_t *default_cmp = FakeAST(InlineCCode,
@@ -2712,7 +2697,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all("Array$heap_push_value(", self, ", ", arg_code, ", ", padded_item_size, ")");
} else if (streq(call->name, "heap_pop")) {
CORD self = compile_to_pointer_depth(env, call->self, 1, false);
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
ast_t *default_cmp = FakeAST(InlineCCode,
@@ -2724,7 +2709,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_all("Array$heap_pop_value(", self, ", ", arg_code, ", ", padded_item_size, ", ", compile_type(item_t), ")");
} else if (streq(call->name, "binary_search")) {
CORD self = compile_to_pointer_depth(env, call->self, 0, call->args != NULL);
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *fn_t = Type(FunctionType, .args=new(arg_t, .name="x", .type=item_ptr, .next=new(arg_t, .name="y", .type=item_ptr)),
.ret=Type(IntType, .bits=TYPE_IBITS32));
ast_t *default_cmp = FakeAST(InlineCCode,
@@ -2746,7 +2731,7 @@ CORD compile(env_t *env, ast_t *ast)
", ", compile_type_info(env, self_value_t), ")");
} else if (streq(call->name, "first")) {
CORD self = compile_to_pointer_depth(env, call->self, 0, call->args != NULL);
- type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_stack=true);
+ type_t *item_ptr = Type(PointerType, .pointed=item_t, .is_view=true);
type_t *predicate_type = Type(
ClosureType, .fn=Type(FunctionType, .args=new(arg_t, .name="item", .type=item_ptr), .ret=Type(BoolType)));
arg_t *arg_spec = new(arg_t, .name="predicate", .type=predicate_type);
@@ -3489,7 +3474,7 @@ CORD compile_type_info(env_t *env, type_t *t)
}
case PointerType: {
auto ptr = Match(t, PointerType);
- CORD sigil = ptr->is_stack ? "&" : "@";
+ CORD sigil = ptr->is_view ? "&" : "@";
return CORD_asprintf("Pointer$info(%r, %r)",
CORD_quoted(sigil),
compile_type_info(env, ptr->pointed));
diff --git a/parse.c b/parse.c
index 5d2cc6cc..73c5dc1d 100644
--- a/parse.c
+++ b/parse.c
@@ -126,7 +126,6 @@ static PARSER(parse_return);
static PARSER(parse_say);
static PARSER(parse_set);
static PARSER(parse_skip);
-static PARSER(parse_stack_reference);
static PARSER(parse_statement);
static PARSER(parse_stop);
static PARSER(parse_struct_def);
@@ -628,18 +627,18 @@ type_ast_t *parse_channel_type(parse_ctx_t *ctx, const char *pos) {
type_ast_t *parse_pointer_type(parse_ctx_t *ctx, const char *pos) {
const char *start = pos;
- bool is_stack;
+ bool is_view;
if (match(&pos, "@"))
- is_stack = false;
+ is_view = false;
else if (match(&pos, "&"))
- is_stack = true;
+ is_view = true;
else
return NULL;
spaces(&pos);
type_ast_t *type = expect(ctx, start, &pos, parse_non_optional_type,
"I couldn't parse a pointer type after this point");
- type_ast_t *ptr_type = NewTypeAST(ctx->file, start, pos, PointerTypeAST, .pointed=type, .is_stack=is_stack);
+ type_ast_t *ptr_type = NewTypeAST(ctx->file, start, pos, PointerTypeAST, .pointed=type, .is_view=is_view);
spaces(&pos);
if (match(&pos, "?"))
return NewTypeAST(ctx->file, start, pos, OptionalTypeAST, .type=ptr_type);
@@ -1250,32 +1249,6 @@ PARSER(parse_heap_alloc) {
return ast;
}
-PARSER(parse_stack_reference) {
- const char *start = pos;
- if (!match(&pos, "&")) return NULL;
- spaces(&pos);
- ast_t *val = expect(ctx, start, &pos, parse_term_no_suffix, "I expected an expression for this '&'");
-
- for (;;) {
- ast_t *new_term;
- if ((new_term=parse_index_suffix(ctx, val))
- || (new_term=parse_fncall_suffix(ctx, val))
- || (new_term=parse_field_suffix(ctx, val))) {
- val = new_term;
- } else break;
- }
- pos = val->end;
-
- ast_t *ast = NewAST(ctx->file, start, pos, StackReference, .value=val);
- for (;;) {
- ast_t *next = parse_optional_suffix(ctx, ast);
- if (!next) next = parse_non_optional_suffix(ctx, ast);
- if (!next) break;
- ast = next;
- }
- return ast;
-}
-
PARSER(parse_not) {
const char *start = pos;
if (!match_word(&pos, "not")) return NULL;
@@ -1583,7 +1556,6 @@ PARSER(parse_term_no_suffix) {
|| (term=parse_int(ctx, pos))
|| (term=parse_negative(ctx, pos)) // Must come after num/int
|| (term=parse_heap_alloc(ctx, pos))
- || (term=parse_stack_reference(ctx, pos))
|| (term=parse_bool(ctx, pos))
|| (term=parse_text(ctx, pos))
|| (term=parse_path(ctx, pos))
@@ -2320,6 +2292,11 @@ PARSER(parse_doctest) {
if (!match(&pos, ">>")) return NULL;
spaces(&pos);
ast_t *expr = expect(ctx, start, &pos, parse_statement, "I couldn't parse the expression for this doctest");
+ spaces(&pos);
+ comment(&pos);
+ // We need at least one newline before looking for "= <expected value>"
+ if (strspn(pos, "\r\n") < 1 && strcspn(pos, "\r\n") > 0)
+ parser_err(ctx, pos, pos + strcspn(pos, "\r\n"), "I couldn't parse this code as part of the doctest expression");
whitespace(&pos);
const char* output = NULL;
if (match(&pos, "=")) {
diff --git a/repl.c b/repl.c
index 601231fe..c9d518d3 100644
--- a/repl.c
+++ b/repl.c
@@ -135,7 +135,7 @@ const TypeInfo_t *type_to_type_info(type_t *t)
}
case PointerType: {
auto ptr = Match(t, PointerType);
- CORD sigil = ptr->is_stack ? "&" : "@";
+ CORD sigil = ptr->is_view ? "&" : "@";
const TypeInfo_t *pointed_info = type_to_type_info(ptr->pointed);
const TypeInfo_t pointer_info = {.size=sizeof(void*), .align=__alignof__(void*),
.tag=PointerInfo, .PointerInfo={.sigil=sigil, .pointed=pointed_info}};
diff --git a/typecheck.c b/typecheck.c
index aefd94cf..ceefda19 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -46,13 +46,13 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
type_t *pointed_t = parse_type_ast(env, ptr->pointed);
if (pointed_t->tag == VoidType)
code_err(ast, "Void pointers are not supported. You probably meant 'Memory' instead of 'Void'");
- return Type(PointerType, .pointed=pointed_t, .is_stack=ptr->is_stack);
+ return Type(PointerType, .pointed=pointed_t, .is_view=ptr->is_view);
}
case ArrayTypeAST: {
type_ast_t *item_type = Match(ast, ArrayTypeAST)->item;
type_t *item_t = parse_type_ast(env, item_type);
if (!item_t) code_err(item_type, "I can't figure out what this type is.");
- if (has_stack_memory(item_t))
+ if (has_view_memory(item_t))
code_err(item_type, "Arrays can't have stack references because the array may outlive the stack frame.");
if (type_size(item_t) > ARRAY_MAX_STRIDE)
code_err(ast, "This array holds items that take up %ld bytes, but the maximum supported size is %ld bytes. Consider using an array of pointers instead.",
@@ -63,7 +63,7 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
type_ast_t *item_type = Match(ast, SetTypeAST)->item;
type_t *item_t = parse_type_ast(env, item_type);
if (!item_t) code_err(item_type, "I can't figure out what this type is.");
- if (has_stack_memory(item_t))
+ if (has_view_memory(item_t))
code_err(item_type, "Sets can't have stack references because the array may outlive the stack frame.");
if (type_size(item_t) > ARRAY_MAX_STRIDE)
code_err(ast, "This set holds items that take up %ld bytes, but the maximum supported size is %ld bytes. Consider using an set of pointers instead.",
@@ -85,19 +85,19 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
type_ast_t *key_type_ast = Match(ast, TableTypeAST)->key;
type_t *key_type = parse_type_ast(env, key_type_ast);
if (!key_type) code_err(key_type_ast, "I can't figure out what type this is.");
- if (has_stack_memory(key_type))
+ if (has_view_memory(key_type))
code_err(key_type_ast, "Tables can't have stack references because the array may outlive the stack frame.");
type_ast_t *val_type_ast = Match(ast, TableTypeAST)->value;
type_t *val_type = parse_type_ast(env, val_type_ast);
if (!val_type) code_err(val_type_ast, "I can't figure out what type this is.");
- if (has_stack_memory(val_type))
+ if (has_view_memory(val_type))
code_err(val_type_ast, "Tables can't have stack references because the array may outlive the stack frame.");
return Type(TableType, .key_type=key_type, .value_type=val_type);
}
case FunctionTypeAST: {
auto fn = Match(ast, FunctionTypeAST);
type_t *ret_t = fn->ret ? parse_type_ast(env, fn->ret) : Type(VoidType);
- if (has_stack_memory(ret_t))
+ if (has_view_memory(ret_t))
code_err(fn->ret, "Functions are not allowed to return stack references, because the reference may no longer exist on the stack.");
arg_t *type_args = NULL;
for (arg_ast_t *arg = fn->args; arg; arg = arg->next) {
@@ -421,7 +421,7 @@ type_t *get_function_def_type(env_t *env, ast_t *ast)
REVERSE_LIST(args);
type_t *ret = fn->ret_type ? parse_type_ast(scope, fn->ret_type) : Type(VoidType);
- if (has_stack_memory(ret))
+ if (has_view_memory(ret))
code_err(ast, "Functions can't return stack references because the reference may outlive its stack frame.");
return Type(FunctionType, .args=args, .ret=ret);
}
@@ -510,45 +510,10 @@ type_t *get_type(env_t *env, ast_t *ast)
}
case HeapAllocate: {
type_t *pointed = get_type(env, Match(ast, HeapAllocate)->value);
- if (has_stack_memory(pointed))
+ if (has_view_memory(pointed))
code_err(ast, "Stack references cannot be moved to the heap because they may outlive the stack frame they were created in.");
return Type(PointerType, .pointed=pointed);
}
- case StackReference: {
- // Supported:
- // &variable
- // &struct_variable.field.(...)
- // &struct_ptr.field.(...)
- // Not supported:
- // &ptr[]
- // &list[index]
- // &table[key]
- // &(expression).field
- // &(expression)
- // &optional_struct_ptr.field
- ast_t *value = Match(ast, StackReference)->value;
- if (value->tag == Var)
- return Type(PointerType, .pointed=get_type(env, value), .is_stack=true);
-
- if (value->tag == FieldAccess) {
- ast_t *base = value;
- while (base->tag == FieldAccess)
- base = Match(base, FieldAccess)->fielded;
-
- type_t *ref_type = get_type(env, value);
- type_t *base_type = get_type(env, base);
- if (base_type->tag == OptionalType) {
- code_err(base, "This value might be null, so it can't be safely dereferenced");
- } else if (base_type->tag == PointerType) {
- auto ptr = Match(base_type, PointerType);
- return Type(PointerType, .pointed=ref_type, .is_stack=ptr->is_stack);
- } else if (base->tag == Var) {
- return Type(PointerType, .pointed=ref_type, .is_stack=true);
- }
- }
-
- code_err(ast, "'&' stack references can only be used on variables or fields of variables");
- }
case Optional: {
ast_t *value = Match(ast, Optional)->value;
type_t *t = get_type(env, value);
@@ -607,7 +572,7 @@ type_t *get_type(env_t *env, ast_t *ast)
} else {
code_err(ast, "I can't figure out what type this array has because it has no members or explicit type");
}
- if (has_stack_memory(item_type))
+ if (has_view_memory(item_type))
code_err(ast, "Arrays cannot hold stack references, because the array may outlive the stack frame the reference was created in.");
return Type(ArrayType, .item_type=item_type);
}
@@ -636,7 +601,7 @@ type_t *get_type(env_t *env, ast_t *ast)
item_type = item_merged;
}
}
- if (has_stack_memory(item_type))
+ if (has_view_memory(item_type))
code_err(ast, "Sets cannot hold stack references because the set may outlive the reference's stack frame.");
return Type(SetType, .item_type=item_type);
}
@@ -681,7 +646,7 @@ type_t *get_type(env_t *env, ast_t *ast)
value_type = val_merged;
}
}
- if (has_stack_memory(key_type) || has_stack_memory(value_type))
+ if (has_view_memory(key_type) || has_view_memory(value_type))
code_err(ast, "Tables cannot hold stack references because the table may outlive the reference's stack frame.");
return Type(TableType, .key_type=key_type, .value_type=value_type);
}
@@ -1143,7 +1108,7 @@ type_t *get_type(env_t *env, ast_t *ast)
declared, ret);
}
- if (has_stack_memory(ret))
+ if (has_view_memory(ret))
code_err(ast, "Functions can't return stack references because the reference may outlive its stack frame.");
return Type(ClosureType, Type(FunctionType, .args=args, .ret=ret));
}
diff --git a/types.c b/types.c
index ec1b66c7..1a140a32 100644
--- a/types.c
+++ b/types.c
@@ -67,7 +67,7 @@ CORD type_to_cord(type_t *t) {
}
case PointerType: {
auto ptr = Match(t, PointerType);
- CORD sigil = ptr->is_stack ? "&" : "@";
+ CORD sigil = ptr->is_view ? "&" : "@";
return CORD_all(sigil, type_to_cord(ptr->pointed));
}
case EnumType: {
@@ -123,7 +123,7 @@ bool type_is_a(type_t *t, type_t *req)
auto t_ptr = Match(t, PointerType);
auto req_ptr = Match(req, PointerType);
if (type_eq(t_ptr->pointed, req_ptr->pointed))
- return (!t_ptr->is_stack && req_ptr->is_stack) || (!t_ptr->is_stack);
+ return (!t_ptr->is_view && req_ptr->is_view) || (!t_ptr->is_view);
}
return false;
}
@@ -275,11 +275,11 @@ PUREFUNC bool can_send_over_channel(type_t *t)
}
}
-PUREFUNC bool has_stack_memory(type_t *t)
+PUREFUNC bool has_view_memory(type_t *t)
{
switch (t->tag) {
- case PointerType: return Match(t, PointerType)->is_stack;
- case OptionalType: return has_stack_memory(Match(t, OptionalType)->type);
+ case PointerType: return Match(t, PointerType)->is_view;
+ case OptionalType: return has_view_memory(Match(t, OptionalType)->type);
default: return false;
}
}
@@ -345,7 +345,7 @@ PUREFUNC bool can_promote(type_t *actual, type_t *needed)
// Can't use @Foo for a function that wants @Baz
// But you *can* use @Foo for a function that wants @Memory
return false;
- else if (actual_ptr->is_stack && !needed_ptr->is_stack)
+ else if (actual_ptr->is_view && !needed_ptr->is_view)
// Can't use &x for a function that wants a @Foo or ?Foo
return false;
else
diff --git a/types.h b/types.h
index 029692ad..96e0b6c5 100644
--- a/types.h
+++ b/types.h
@@ -101,7 +101,7 @@ struct type_s {
} ClosureType;
struct {
type_t *pointed;
- bool is_stack:1;
+ bool is_view:1;
} PointerType;
struct {
const char *name;
@@ -143,7 +143,7 @@ type_t *value_type(type_t *a);
typedef enum {NUM_PRECISION_EQUAL, NUM_PRECISION_LESS, NUM_PRECISION_MORE, NUM_PRECISION_INCOMPARABLE} precision_cmp_e;
PUREFUNC precision_cmp_e compare_precision(type_t *a, type_t *b);
PUREFUNC bool has_heap_memory(type_t *t);
-PUREFUNC bool has_stack_memory(type_t *t);
+PUREFUNC bool has_view_memory(type_t *t);
PUREFUNC bool can_send_over_channel(type_t *t);
PUREFUNC bool can_promote(type_t *actual, type_t *needed);
PUREFUNC const char *enum_single_value_tag(type_t *enum_type, type_t *t);