Skip to content

Commit

Permalink
var is no longer a pseudo-type, it is syntax
Browse files Browse the repository at this point in the history
closes #779
  • Loading branch information
andrewrk committed Mar 6, 2018
1 parent 44ae891 commit eff3530
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 154 deletions.
6 changes: 3 additions & 3 deletions doc/langref.html.in
Expand Up @@ -5733,19 +5733,19 @@ UseDecl = "use" Expression ";"

ExternDecl = "extern" option(String) (FnProto | VariableDeclaration) ";"

FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") TypeExpr
FnProto = option("nakedcc" | "stdcallcc" | "extern" | ("async" option("(" Expression ")"))) "fn" option(Symbol) ParamDeclList option("align" "(" Expression ")") option("section" "(" Expression ")") option("!") (TypeExpr | "var")

FnDef = option("inline" | "export") FnProto Block

ParamDeclList = "(" list(ParamDecl, ",") ")"

ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...")
ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "var" | "...")

Block = option(Symbol ":") "{" many(Statement) "}"

Statement = LocalVarDecl ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";"

TypeExpr = ErrorSetExpr | "var"
TypeExpr = ErrorSetExpr

ErrorSetExpr = (PrefixOpExpression "!" PrefixOpExpression) | PrefixOpExpression

Expand Down
9 changes: 3 additions & 6 deletions src/all_types.hpp
Expand Up @@ -16,6 +16,7 @@
#include "bigint.hpp"
#include "bigfloat.hpp"
#include "target.hpp"
#include "tokenizer.hpp"

struct AstNode;
struct ImportTableEntry;
Expand Down Expand Up @@ -399,7 +400,6 @@ enum NodeType {
NodeTypeStructValueField,
NodeTypeArrayType,
NodeTypeErrorType,
NodeTypeVarLiteral,
NodeTypeIfErrorExpr,
NodeTypeTestExpr,
NodeTypeErrorSetDecl,
Expand Down Expand Up @@ -427,6 +427,7 @@ struct AstNodeFnProto {
Buf *name;
ZigList<AstNode *> params;
AstNode *return_type;
Token *return_var_token;
bool is_var_args;
bool is_extern;
bool is_export;
Expand Down Expand Up @@ -456,6 +457,7 @@ struct AstNodeFnDecl {
struct AstNodeParamDecl {
Buf *name;
AstNode *type;
Token *var_token;
bool is_noalias;
bool is_inline;
bool is_var_args;
Expand Down Expand Up @@ -866,9 +868,6 @@ struct AstNodeUnreachableExpr {
struct AstNodeErrorType {
};

struct AstNodeVarLiteral {
};

struct AstNodeAwaitExpr {
AstNode *expr;
};
Expand Down Expand Up @@ -933,7 +932,6 @@ struct AstNode {
AstNodeUnreachableExpr unreachable_expr;
AstNodeArrayType array_type;
AstNodeErrorType error_type;
AstNodeVarLiteral var_literal;
AstNodeErrorSetDecl err_set_decl;
AstNodeCancelExpr cancel_expr;
AstNodeResumeExpr resume_expr;
Expand Down Expand Up @@ -1134,7 +1132,6 @@ struct TypeTableEntryPromise {

enum TypeTableEntryId {
TypeTableEntryIdInvalid,
TypeTableEntryIdVar,
TypeTableEntryIdMetaType,
TypeTableEntryIdVoid,
TypeTableEntryIdBool,
Expand Down
46 changes: 18 additions & 28 deletions src/analyze.cpp
Expand Up @@ -200,7 +200,6 @@ static uint8_t bits_needed_for_unsigned(uint64_t x) {
bool type_is_complete(TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdStruct:
return type_entry->data.structure.complete;
Expand Down Expand Up @@ -239,7 +238,6 @@ bool type_is_complete(TypeTableEntry *type_entry) {
bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdStruct:
return type_entry->data.structure.zero_bits_known;
Expand Down Expand Up @@ -1281,7 +1279,6 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdMetaType:
case TypeTableEntryIdUnreachable:
Expand Down Expand Up @@ -1324,7 +1321,6 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
static bool type_allowed_in_extern(CodeGen *g, TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdMetaType:
case TypeTableEntryIdNumLitFloat:
Expand Down Expand Up @@ -1428,6 +1424,14 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
calling_convention_name(fn_type_id.cc)));
return g->builtin_types.entry_invalid;
}
} else if (param_node->data.param_decl.var_token != nullptr) {
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("parameter of type 'var' not allowed in function with calling convention '%s'",
calling_convention_name(fn_type_id.cc)));
return g->builtin_types.entry_invalid;
}
return get_generic_fn_type(g, &fn_type_id);
}

TypeTableEntry *type_entry = analyze_type_expr(g, child_scope, param_node->data.param_decl.type);
Expand Down Expand Up @@ -1463,14 +1467,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
return g->builtin_types.entry_invalid;
case TypeTableEntryIdVar:
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("parameter of type 'var' not allowed in function with calling convention '%s'",
calling_convention_name(fn_type_id.cc)));
return g->builtin_types.entry_invalid;
}
return get_generic_fn_type(g, &fn_type_id);
case TypeTableEntryIdNumLitFloat:
case TypeTableEntryIdNumLitInt:
case TypeTableEntryIdNamespace:
Expand Down Expand Up @@ -1527,6 +1523,16 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
fn_type_id.return_type = specified_return_type;
}

if (fn_proto->return_var_token != nullptr) {
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, fn_proto->return_type,
buf_sprintf("return type 'var' not allowed in function with calling convention '%s'",
calling_convention_name(fn_type_id.cc)));
return g->builtin_types.entry_invalid;
}
return get_generic_fn_type(g, &fn_type_id);
}

if (!calling_convention_allows_zig_types(fn_type_id.cc) && !type_allowed_in_extern(g, fn_type_id.return_type)) {
add_node_error(g, fn_proto->return_type,
buf_sprintf("return type '%s' not allowed in function with calling convention '%s'",
Expand All @@ -1552,7 +1558,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, fn_proto->return_type,
Expand Down Expand Up @@ -3226,7 +3231,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
case NodeTypeStructValueField:
case NodeTypeArrayType:
case NodeTypeErrorType:
case NodeTypeVarLiteral:
case NodeTypeIfErrorExpr:
case NodeTypeTestExpr:
case NodeTypeErrorSetDecl:
Expand Down Expand Up @@ -3262,7 +3266,6 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
case TypeTableEntryIdInvalid:
return g->builtin_types.entry_invalid;
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdVar:
case TypeTableEntryIdNumLitFloat:
case TypeTableEntryIdNumLitInt:
case TypeTableEntryIdUndefLit:
Expand Down Expand Up @@ -3641,7 +3644,6 @@ TypeEnumField *find_enum_field_by_tag(TypeTableEntry *enum_type, const BigInt *t
static bool is_container(TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdStruct:
case TypeTableEntryIdEnum:
Expand Down Expand Up @@ -3716,7 +3718,6 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdArgTuple:
case TypeTableEntryIdOpaque:
case TypeTableEntryIdPromise:
Expand Down Expand Up @@ -4216,7 +4217,6 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdVar:
case TypeTableEntryIdArgTuple:
case TypeTableEntryIdOpaque:
zig_unreachable();
Expand Down Expand Up @@ -4515,7 +4515,6 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdInvalid:
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdVar:
zig_unreachable();
}
zig_unreachable();
Expand Down Expand Up @@ -4613,7 +4612,6 @@ bool type_has_bits(TypeTableEntry *type_entry) {
bool type_requires_comptime(TypeTableEntry *type_entry) {
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdOpaque:
zig_unreachable();
case TypeTableEntryIdNumLitFloat:
Expand Down Expand Up @@ -5109,7 +5107,6 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdInvalid:
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdVar:
case TypeTableEntryIdPromise:
zig_unreachable();
}
Expand Down Expand Up @@ -5189,9 +5186,6 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
case TypeTableEntryIdInvalid:
buf_appendf(buf, "(invalid)");
return;
case TypeTableEntryIdVar:
buf_appendf(buf, "(var)");
return;
case TypeTableEntryIdVoid:
buf_appendf(buf, "{}");
return;
Expand Down Expand Up @@ -5427,7 +5421,6 @@ TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits)
uint32_t type_id_hash(TypeId x) {
switch (x.id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdOpaque:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdVoid:
Expand Down Expand Up @@ -5474,7 +5467,6 @@ bool type_id_eql(TypeId a, TypeId b) {
return false;
switch (a.id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdVoid:
case TypeTableEntryIdBool:
Expand Down Expand Up @@ -5629,7 +5621,6 @@ size_t type_id_len() {
size_t type_id_index(TypeTableEntryId id) {
switch (id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdMetaType:
return 0;
Expand Down Expand Up @@ -5688,7 +5679,6 @@ size_t type_id_index(TypeTableEntryId id) {
const char *type_id_name(TypeTableEntryId id) {
switch (id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
zig_unreachable();
case TypeTableEntryIdMetaType:
return "Type";
Expand Down
23 changes: 12 additions & 11 deletions src/ast_render.cpp
Expand Up @@ -236,8 +236,6 @@ static const char *node_type_str(NodeType node_type) {
return "ArrayType";
case NodeTypeErrorType:
return "ErrorType";
case NodeTypeVarLiteral:
return "VarLiteral";
case NodeTypeIfErrorExpr:
return "IfErrorExpr";
case NodeTypeTestExpr:
Expand Down Expand Up @@ -436,6 +434,8 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
if (param_decl->data.param_decl.is_var_args) {
fprintf(ar->f, "...");
} else if (param_decl->data.param_decl.var_token != nullptr) {
fprintf(ar->f, "var");
} else {
render_node_grouped(ar, param_decl->data.param_decl.type);
}
Expand All @@ -456,13 +456,17 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, ")");
}

AstNode *return_type_node = node->data.fn_proto.return_type;
assert(return_type_node != nullptr);
fprintf(ar->f, " ");
if (node->data.fn_proto.auto_err_set) {
fprintf(ar->f, "!");
if (node->data.fn_proto.return_var_token != nullptr) {
fprintf(ar->f, "var");
} else {
AstNode *return_type_node = node->data.fn_proto.return_type;
assert(return_type_node != nullptr);
fprintf(ar->f, " ");
if (node->data.fn_proto.auto_err_set) {
fprintf(ar->f, "!");
}
render_node_grouped(ar, return_type_node);
}
render_node_grouped(ar, return_type_node);
break;
}
case NodeTypeFnDef:
Expand Down Expand Up @@ -768,9 +772,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
case NodeTypeErrorType:
fprintf(ar->f, "error");
break;
case NodeTypeVarLiteral:
fprintf(ar->f, "var");
break;
case NodeTypeAsmExpr:
{
AstNodeAsmExpr *asm_expr = &node->data.asm_expr;
Expand Down
10 changes: 0 additions & 10 deletions src/codegen.cpp
Expand Up @@ -4508,7 +4508,6 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
assert(!type_entry->zero_bits);
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdUnreachable:
case TypeTableEntryIdNumLitFloat:
Expand Down Expand Up @@ -4960,7 +4959,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
case TypeTableEntryIdNamespace:
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdVar:
case TypeTableEntryIdArgTuple:
case TypeTableEntryIdOpaque:
case TypeTableEntryIdPromise:
Expand Down Expand Up @@ -5611,11 +5609,6 @@ static void define_builtin_types(CodeGen *g) {
entry->zero_bits = true;
g->builtin_types.entry_null = entry;
}
{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdVar);
buf_init_from_str(&entry->name, "(var)");
g->builtin_types.entry_var = entry;
}
{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdArgTuple);
buf_init_from_str(&entry->name, "(args)");
Expand Down Expand Up @@ -6444,7 +6437,6 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, TypeTableEntry

switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdNumLitFloat:
case TypeTableEntryIdNumLitInt:
Expand Down Expand Up @@ -6639,7 +6631,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
case TypeTableEntryIdNumLitInt:
case TypeTableEntryIdUndefLit:
case TypeTableEntryIdNullLit:
case TypeTableEntryIdVar:
case TypeTableEntryIdArgTuple:
case TypeTableEntryIdPromise:
zig_unreachable();
Expand Down Expand Up @@ -6781,7 +6772,6 @@ static void gen_h_file(CodeGen *g) {
TypeTableEntry *type_entry = gen_h->types_to_declare.at(type_i);
switch (type_entry->id) {
case TypeTableEntryIdInvalid:
case TypeTableEntryIdVar:
case TypeTableEntryIdMetaType:
case TypeTableEntryIdVoid:
case TypeTableEntryIdBool:
Expand Down

0 comments on commit eff3530

Please sign in to comment.