From 64bd884d94fc45600eaed9566585d455b875a87a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 23 Feb 2019 14:42:44 +0000 Subject: Add "none" calling convention --- src/parser.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index a62bac233..e38c34a0a 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -2700,6 +2700,7 @@ ProcCallingConvention string_to_calling_convention(String s) { if (s == "std") return ProcCC_StdCall; if (s == "fastcall") return ProcCC_FastCall; if (s == "fast") return ProcCC_FastCall; + if (s == "none") return ProcCC_None; return ProcCC_Invalid; } -- cgit v1.2.3 From e551d2b25ea39afb95f7b8ee4309ef0cc8b502b8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 23 Feb 2019 21:39:47 +0000 Subject: Replace `foreign export {}` with `@export` --- src/check_decl.cpp | 11 ++++++----- src/check_expr.cpp | 16 ++++++++++++++++ src/check_type.cpp | 6 +++++- src/checker.cpp | 54 ++++++++++++++++++++++++++++++++++++++++-------------- src/checker.hpp | 2 +- src/parser.cpp | 10 ++-------- src/tokenizer.cpp | 1 - 7 files changed, 70 insertions(+), 30 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 91fd0ff01..e9d6d5860 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -551,20 +551,20 @@ void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) { check_procedure_type(&tmp_ctx, proc_type, pl->type); TypeProc *pt = &proc_type->Proc; - - bool is_foreign = e->Procedure.is_foreign; - bool is_export = e->Procedure.is_export; - bool is_require_results = (pl->tags & ProcTag_require_results) != 0; - AttributeContext ac = make_attribute_context(e->Procedure.link_prefix); if (d != nullptr) { check_decl_attributes(ctx, d->attributes, proc_decl_attribute, &ac); } + e->Procedure.is_export = ac.is_export; e->deprecated_message = ac.deprecated_message; ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix); + bool is_foreign = e->Procedure.is_foreign; + bool is_export = e->Procedure.is_export; + bool is_require_results = (pl->tags & ProcTag_require_results) != 0; + if (e->pkg != nullptr && e->token.string == "main") { if (pt->param_count != 0 || pt->result_count != 0) { @@ -718,6 +718,7 @@ void check_var_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init_ex check_decl_attributes(ctx, decl->attributes, var_decl_attribute, &ac); } + e->Variable.is_export = ac.is_export; ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix); e->Variable.thread_local_model = ac.thread_local_model; diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 75c16b705..ea125a5eb 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -1241,6 +1241,14 @@ bool check_binary_op(CheckerContext *c, Operand *o, Token op) { error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string)); return false; } + if (is_type_simd_vector(o->type)) { + switch (op.kind) { + case Token_ModMod: + case Token_ModModEq: + error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string)); + return false; + } + } break; case Token_AndNot: @@ -1249,6 +1257,14 @@ bool check_binary_op(CheckerContext *c, Operand *o, Token op) { error(op, "Operator '%.*s' is only allowed with integers and bit sets", LIT(op.string)); return false; } + if (is_type_simd_vector(o->type)) { + switch (op.kind) { + case Token_AndNot: + case Token_AndNotEq: + error(op, "Operator '%.*s' is only allowed with integers", LIT(op.string)); + return false; + } + } break; case Token_CmpAnd: diff --git a/src/check_type.cpp b/src/check_type.cpp index 8b6b67e65..1c5d5ac85 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -1783,7 +1783,11 @@ Type *type_to_abi_compat_param_type(gbAllocator a, Type *original_type) { Type *new_type = original_type; if (is_type_boolean(original_type)) { - return t_llvm_bool; + Type *t = core_type(base_type(new_type)); + if (t == t_bool) { + return t_llvm_bool; + } + return new_type; } if (build_context.ODIN_ARCH == "386") { diff --git a/src/checker.cpp b/src/checker.cpp index 7900555a5..9bbe64839 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1952,7 +1952,18 @@ DECL_ATTRIBUTE_PROC(foreign_block_decl_attribute) { } DECL_ATTRIBUTE_PROC(proc_decl_attribute) { - if (name == "deferred") { + if (name == "export") { + ExactValue ev = check_decl_attribute_value(c, value); + if (ev.kind == ExactValue_Invalid) { + ac->is_export = true; + } else if (ev.kind == ExactValue_Bool) { + ac->is_export = ev.value_bool; + } else { + error(value, "Expected either a boolean or no parameter for 'export'"); + return false; + } + return true; + } else if (name == "deferred") { if (value != nullptr) { Operand o = {}; check_expr(c, &o, value); @@ -2064,7 +2075,20 @@ DECL_ATTRIBUTE_PROC(var_decl_attribute) { return true; } - if (name == "link_name") { + if (name == "export") { + ExactValue ev = check_decl_attribute_value(c, value); + if (ev.kind == ExactValue_Invalid) { + ac->is_export = true; + } else if (ev.kind == ExactValue_Bool) { + ac->is_export = ev.value_bool; + } else { + error(value, "Expected either a boolean or no parameter for 'export'"); + return false; + } + if (ac->thread_local_model != "") { + error(elem, "An exported variable cannot be thread local"); + } + } else if (name == "link_name") { if (ev.kind == ExactValue_String) { ac->link_name = ev.value_string; if (!is_foreign_name_valid(ac->link_name)) { @@ -2087,8 +2111,10 @@ DECL_ATTRIBUTE_PROC(var_decl_attribute) { } else if (name == "thread_local") { if (ac->init_expr_list_count > 0) { error(elem, "A thread local variable declaration cannot have initialization values"); - } else if (c->foreign_context.curr_library || c->foreign_context.in_export) { + } else if (c->foreign_context.curr_library) { error(elem, "A foreign block variable cannot be thread local"); + } else if (ac->is_export) { + error(elem, "An exported variable cannot be thread local"); } else if (ev.kind == ExactValue_Invalid) { ac->thread_local_model = str_lit("default"); } else if (ev.kind == ExactValue_String) { @@ -2151,9 +2177,17 @@ void check_decl_attributes(CheckerContext *c, Array const &attributes, De case_ast_node(i, Ident, elem); name = i->token.string; case_end; + case_ast_node(i, Implicit, elem); + name = i->string; + case_end; case_ast_node(fv, FieldValue, elem); - GB_ASSERT(fv->field->kind == Ast_Ident); - name = fv->field->Ident.token.string; + if (fv->field->kind == Ast_Ident) { + name = fv->field->Ident.token.string; + } else if (fv->field->kind == Ast_Implicit) { + name = fv->field->Implicit.string; + } else { + GB_PANIC("Unknown Field Value name"); + } value = fv->value; case_end; default: @@ -2407,9 +2441,6 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { e->Variable.foreign_library_ident = fl; e->Variable.link_prefix = c->foreign_context.link_prefix; - - } else if (c->foreign_context.in_export) { - e->Variable.is_export = true; } Ast *init_expr = value; @@ -2475,9 +2506,6 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { GB_ASSERT(cc != ProcCC_Invalid); pl->type->ProcType.calling_convention = cc; - - } else if (c->foreign_context.in_export) { - e->Procedure.is_export = true; } d->proc_lit = init; d->type_expr = pl->type; @@ -2516,7 +2544,7 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { } if (e->kind != Entity_Procedure) { - if (fl != nullptr || c->foreign_context.in_export) { + if (fl != nullptr) { AstKind kind = init->kind; error(name, "Only procedures and variables are allowed to be in a foreign block, got %.*s", LIT(ast_strings[kind])); if (kind == Ast_ProcType) { @@ -2544,8 +2572,6 @@ void check_add_foreign_block_decl(CheckerContext *ctx, Ast *decl) { CheckerContext c = *ctx; if (foreign_library->kind == Ast_Ident) { c.foreign_context.curr_library = foreign_library; - } else if (foreign_library->kind == Ast_Implicit && foreign_library->Implicit.kind == Token_export) { - c.foreign_context.in_export = true; } else { error(foreign_library, "Foreign block name must be an identifier or 'export'"); c.foreign_context.curr_library = nullptr; diff --git a/src/checker.hpp b/src/checker.hpp index de491671e..759e343bf 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -305,6 +305,7 @@ struct DeferredProcedure { struct AttributeContext { + bool is_export; String link_name; String link_prefix; isize init_expr_list_count; @@ -423,7 +424,6 @@ struct ForeignContext { Ast * curr_library; ProcCallingConvention default_cc; String link_prefix; - bool in_export; }; typedef Array CheckerTypePath; diff --git a/src/parser.cpp b/src/parser.cpp index e38c34a0a..9d42b3828 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1199,7 +1199,6 @@ void fix_advance_to_next_stmt(AstFile *f) { case Token_package: case Token_foreign: case Token_import: - case Token_export: case Token_if: case Token_for: @@ -2452,9 +2451,7 @@ void parse_foreign_block_decl(AstFile *f, Array *decls) { Ast *parse_foreign_block(AstFile *f, Token token) { CommentGroup *docs = f->lead_comment; Ast *foreign_library = nullptr; - if (f->curr_token.kind == Token_export) { - foreign_library = ast_implicit(f, expect_token(f, Token_export)); - } else if (f->curr_token.kind == Token_OpenBrace) { + if (f->curr_token.kind == Token_OpenBrace) { foreign_library = ast_ident(f, blank_token); } else { foreign_library = parse_ident(f); @@ -3590,7 +3587,6 @@ Ast *parse_foreign_decl(AstFile *f) { Token token = expect_token(f, Token_foreign); switch (f->curr_token.kind) { - case Token_export: case Token_Ident: case Token_OpenBrace: return parse_foreign_block(f, token); @@ -3667,6 +3663,7 @@ Ast *parse_attribute(AstFile *f, Token token, TokenKind open_kind, TokenKind clo f->curr_token.kind != Token_EOF) { Ast *elem = nullptr; elem = parse_ident(f); + if (f->curr_token.kind == Token_Eq) { Token eq = expect_token(f, Token_Eq); Ast *value = parse_value(f); @@ -3732,9 +3729,6 @@ Ast *parse_stmt(AstFile *f) { case Token_import: return parse_import_decl(f, ImportDecl_Standard); - // case Token_export: - // return parse_export_decl(f); - case Token_if: return parse_if_stmt(f); case Token_when: return parse_when_stmt(f); diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 02770e371..31afe3d2e 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -82,7 +82,6 @@ TOKEN_KIND(Token__OperatorEnd, ""), \ \ TOKEN_KIND(Token__KeywordBegin, ""), \ TOKEN_KIND(Token_import, "import"), \ - TOKEN_KIND(Token_export, "export"), \ TOKEN_KIND(Token_foreign, "foreign"), \ TOKEN_KIND(Token_package, "package"), \ TOKEN_KIND(Token_typeid, "typeid"), \ -- cgit v1.2.3 From a9ab90bd2488783c3523fa30315f9754937fd52e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 23 Feb 2019 22:17:27 +0000 Subject: Make `static` an attribute rather than a keyword prefix --- core/strings/builder.odin | 4 ++-- src/check_decl.cpp | 7 +++++- src/check_stmt.cpp | 17 ++++++++++----- src/checker.cpp | 20 +++++++---------- src/checker.hpp | 1 + src/ir.cpp | 11 +++++++++- src/parser.cpp | 55 +++++++++++++++++++++++------------------------ src/parser.hpp | 1 - src/tokenizer.cpp | 1 - 9 files changed, 66 insertions(+), 51 deletions(-) (limited to 'src/parser.cpp') diff --git a/core/strings/builder.odin b/core/strings/builder.odin index e86d748cc..547c456ba 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -61,8 +61,8 @@ write_bytes :: proc(b: ^Builder, x: []byte) { append(&b.buf, ..x); } -@(private) -static DIGITS_LOWER := "0123456789abcdefx"; +@(private, static) +DIGITS_LOWER := "0123456789abcdefx"; write_quoted_string :: proc(b: ^Builder, s: string, quote: byte = '"') { write_byte(b, quote); diff --git a/src/check_decl.cpp b/src/check_decl.cpp index e9d6d5860..c2796e1da 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -718,9 +718,14 @@ void check_var_decl(CheckerContext *ctx, Entity *e, Ast *type_expr, Ast *init_ex check_decl_attributes(ctx, decl->attributes, var_decl_attribute, &ac); } + e->Variable.thread_local_model = ac.thread_local_model; e->Variable.is_export = ac.is_export; + if (ac.is_static) { + e->flags |= EntityFlag_Static; + } else { + e->flags &= ~EntityFlag_Static; + } ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix); - e->Variable.thread_local_model = ac.thread_local_model; String context_name = str_lit("variable declaration"); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 3262aea5d..c861cfdf3 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1665,8 +1665,6 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { if (!is_blank_ident(str)) { found = scope_lookup_current(ctx->scope, str); new_name_count += 1; - } else if (vd->is_static) { - error(name, "'static' is now allowed to be applied to '_'"); } if (found == nullptr) { entity = alloc_entity_variable(ctx->scope, token, nullptr, false); @@ -1678,9 +1676,6 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { entity->Variable.is_foreign = true; entity->Variable.foreign_library_ident = fl; } - if (vd->is_static) { - entity->flags |= EntityFlag_Static; - } } else { TokenPos pos = found->token.pos; error(token, @@ -1744,6 +1739,16 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { if (ac.link_name.len > 0) { e->Variable.link_name = ac.link_name; } + + e->flags &= ~EntityFlag_Static; + if (ac.is_static) { + String name = e->token.string; + if (name == "_") { + error(e->token, "The 'static' attribute is not allowed to be applied to '_'"); + } else { + e->flags |= EntityFlag_Static; + } + } } check_arity_match(ctx, vd); @@ -1751,6 +1756,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { for (isize i = 0; i < entity_count; i++) { Entity *e = entities[i]; + if (e->Variable.is_foreign) { if (vd->values.count > 0) { error(e->token, "A foreign variable declaration cannot have a default value"); @@ -1842,6 +1848,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { } } } + } else { // constant value declaration // NOTE(bill): Check `_` declarations diff --git a/src/checker.cpp b/src/checker.cpp index 9bbe64839..6389e3d30 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2070,6 +2070,14 @@ DECL_ATTRIBUTE_PROC(proc_decl_attribute) { DECL_ATTRIBUTE_PROC(var_decl_attribute) { ExactValue ev = check_decl_attribute_value(c, value); + if (name == "static") { + if (value != nullptr) { + error(elem, "'static' does not have any parameters"); + } + ac->is_static = true; + return true; + } + if (c->curr_proc_decl != nullptr) { error(elem, "Only a variable at file scope can have a '%.*s'", LIT(name)); return true; @@ -2425,10 +2433,6 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { e->flags |= EntityFlag_NotExported; } - if (vd->is_static) { - e->flags |= EntityFlag_Static; - } - if (vd->is_using) { vd->is_using = false; // NOTE(bill): This error will be only caught once error(name, "'using' is not allowed at the file scope"); @@ -2527,14 +2531,6 @@ void check_collect_value_decl(CheckerContext *c, Ast *decl) { e->flags |= EntityFlag_NotExported; } - if (vd->is_static) { - if (e->kind == Entity_Constant) { - e->flags |= EntityFlag_Static; - } else { - error(name, "'static' is not allowed on this constant value declaration"); - } - } - if (vd->is_using) { if (e->kind == Entity_TypeName && init->kind == Ast_EnumType) { d->is_using = true; diff --git a/src/checker.hpp b/src/checker.hpp index 759e343bf..4420d6bb4 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -306,6 +306,7 @@ struct DeferredProcedure { struct AttributeContext { bool is_export; + bool is_static; String link_name; String link_prefix; isize init_expr_list_count; diff --git a/src/ir.cpp b/src/ir.cpp index 4df6665f7..e92fa7f60 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -8166,7 +8166,16 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) { if (vd->is_mutable) { irModule *m = proc->module; - if (vd->is_static) { + bool is_static = false; + if (vd->names.count > 0) { + Entity *e = entity_of_ident(vd->names[0]); + if (e->flags & EntityFlag_Static) { + // NOTE(bill): If one of the entities is static, they all are + is_static = true; + } + } + + if (is_static) { for_array(i, vd->names) { irValue *value = nullptr; if (vd->values.count > 0) { diff --git a/src/parser.cpp b/src/parser.cpp index 9d42b3828..a5526fd73 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1208,7 +1208,6 @@ void fix_advance_to_next_stmt(AstFile *f) { case Token_defer: case Token_asm: case Token_using: - case Token_static: case Token_break: case Token_continue: @@ -3751,33 +3750,33 @@ Ast *parse_stmt(AstFile *f) { return s; } - case Token_static: { - CommentGroup *docs = f->lead_comment; - Token token = expect_token(f, Token_static); - - Ast *decl = nullptr; - Array list = parse_lhs_expr_list(f); - if (list.count == 0) { - syntax_error(token, "Illegal use of 'static' statement"); - expect_semicolon(f, nullptr); - return ast_bad_stmt(f, token, f->curr_token); - } - - expect_token_after(f, Token_Colon, "identifier list"); - decl = parse_value_decl(f, list, docs); - - if (decl != nullptr && decl->kind == Ast_ValueDecl) { - if (decl->ValueDecl.is_mutable) { - decl->ValueDecl.is_static = true; - } else { - error(token, "'static' may only be currently used with variable declaration"); - } - return decl; - } - - syntax_error(token, "Illegal use of 'static' statement"); - return ast_bad_stmt(f, token, f->curr_token); - } break; + // case Token_static: { + // CommentGroup *docs = f->lead_comment; + // Token token = expect_token(f, Token_static); + + // Ast *decl = nullptr; + // Array list = parse_lhs_expr_list(f); + // if (list.count == 0) { + // syntax_error(token, "Illegal use of 'static' statement"); + // expect_semicolon(f, nullptr); + // return ast_bad_stmt(f, token, f->curr_token); + // } + + // expect_token_after(f, Token_Colon, "identifier list"); + // decl = parse_value_decl(f, list, docs); + + // if (decl != nullptr && decl->kind == Ast_ValueDecl) { + // if (decl->ValueDecl.is_mutable) { + // decl->ValueDecl.is_static = true; + // } else { + // error(token, "'static' may only be currently used with variable declaration"); + // } + // return decl; + // } + + // syntax_error(token, "Illegal use of 'static' statement"); + // return ast_bad_stmt(f, token, f->curr_token); + // } break; case Token_using: { CommentGroup *docs = f->lead_comment; diff --git a/src/parser.hpp b/src/parser.hpp index 13dd1258c..d685aa1af 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -376,7 +376,6 @@ AST_KIND(_DeclBegin, "", bool) \ Array attributes; \ CommentGroup *docs; \ CommentGroup *comment; \ - bool is_static; \ bool is_using; \ bool is_mutable; \ }) \ diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 31afe3d2e..11fae7120 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -107,7 +107,6 @@ TOKEN_KIND(Token__KeywordBegin, ""), \ TOKEN_KIND(Token_bit_field, "bit_field"), \ TOKEN_KIND(Token_bit_set, "bit_set"), \ TOKEN_KIND(Token_map, "map"), \ - TOKEN_KIND(Token_static, "static"), \ TOKEN_KIND(Token_dynamic, "dynamic"), \ TOKEN_KIND(Token_auto_cast, "auto_cast"), \ TOKEN_KIND(Token_cast, "cast"), \ -- cgit v1.2.3 From ad3b6ab71854a638f2b769fc12e8776abfff48d6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 6 Mar 2019 16:19:47 +0000 Subject: Implicit Selector Expressions: `.A` --- core/odin/ast/ast.odin | 4 ++++ core/odin/parser/parser.odin | 7 +++++++ src/check_expr.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++-- src/ir.cpp | 7 +++++++ src/parser.cpp | 24 +++++++++++++++++++++- src/parser.hpp | 1 + 6 files changed, 89 insertions(+), 3 deletions(-) (limited to 'src/parser.cpp') diff --git a/core/odin/ast/ast.odin b/core/odin/ast/ast.odin index 8c43ee8e3..59d18a0c0 100644 --- a/core/odin/ast/ast.odin +++ b/core/odin/ast/ast.odin @@ -144,6 +144,10 @@ Selector_Expr :: struct { field: ^Ident, } +Implicit_Selector_Expr :: struct { + using node: Expr, + field: ^Ident, +} Index_Expr :: struct { using node: Expr, diff --git a/core/odin/parser/parser.odin b/core/odin/parser/parser.odin index c10a83e47..90de2ca30 100644 --- a/core/odin/parser/parser.odin +++ b/core/odin/parser/parser.odin @@ -2581,6 +2581,13 @@ parse_unary_expr :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { ue.expr = expr; return ue; + case token.Period: + op := advance_token(p); + field := parse_ident(p); + ise := ast.new(ast.Implicit_Selector_Expr, op.pos, field.end); + ise.field = field; + return ise; + } return parse_atom_expr(p, parse_operand(p, lhs), lhs); } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index ea125a5eb..4c921f484 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -991,7 +991,6 @@ Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *named_type, Typ o->expr = n; String name = n->Ident.token.string; - Entity *e = scope_lookup(c->scope, name); if (e == nullptr) { if (is_blank_ident(name)) { @@ -6177,7 +6176,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type continue; } - check_expr(c, o, elem); + check_expr_with_type_hint(c, o, elem, et); if (is_constant) { is_constant = o->mode == Addressing_Constant; @@ -6408,6 +6407,47 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type case_end; + case_ast_node(ise, ImplicitSelectorExpr, node); + o->type = t_invalid; + o->expr = node; + o->mode = Addressing_Invalid; + + if (type_hint == nullptr) { + gbString str = expr_to_string(node); + error(node, "Cannot determine type for implicit selector expression '%s'", str); + gb_string_free(str); + return Expr_Expr; + } + o->type = type_hint; + if (!is_type_enum(type_hint)) { + gbString typ = type_to_string(type_hint); + gbString str = expr_to_string(node); + error(node, "Invalid type '%s' for implicit selector expression '%s'", typ, str); + gb_string_free(str); + gb_string_free(typ); + return Expr_Expr; + } + GB_ASSERT(ise->selector->kind == Ast_Ident); + String name = ise->selector->Ident.token.string; + + Type *enum_type = base_type(type_hint); + GB_ASSERT(enum_type->kind == Type_Enum); + Entity *e = scope_lookup_current(enum_type->Enum.scope, name); + if (e == nullptr) { + gbString typ = type_to_string(type_hint); + error(node, "Undeclared name %.*s for type '%s'", LIT(name), typ); + gb_string_free(typ); + return Expr_Expr; + } + GB_ASSERT(are_types_identical(base_type(e->type), base_type(type_hint))); + GB_ASSERT(e->kind == Entity_Constant); + o->value = e->Constant.value; + o->mode = Addressing_Constant; + o->type = e->type; + + return Expr_Expr; + case_end; + case_ast_node(ie, IndexExpr, node); check_expr(c, o, ie->expr); if (o->mode == Addressing_Invalid) { @@ -6832,6 +6872,11 @@ gbString write_expr_to_string(gbString str, Ast *node) { str = write_expr_to_string(str, se->selector); case_end; + case_ast_node(se, ImplicitSelectorExpr, node); + str = gb_string_append_rune(str, '.'); + str = write_expr_to_string(str, se->selector); + case_end; + case_ast_node(ta, TypeAssertion, node); str = write_expr_to_string(str, ta->expr); str = gb_string_appendc(str, ".("); diff --git a/src/ir.cpp b/src/ir.cpp index e3fe83c3b..0d5e3fc36 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6268,6 +6268,13 @@ irValue *ir_build_expr_internal(irProcedure *proc, Ast *expr) { return ir_addr_load(proc, ir_build_addr(proc, expr)); case_end; + case_ast_node(ise, ImplicitSelectorExpr, expr); + TypeAndValue tav = type_and_value_of_expr(expr); + GB_ASSERT(tav.mode == Addressing_Constant); + + return ir_add_module_constant(proc->module, tv.type, tv.value); + case_end; + case_ast_node(te, TernaryExpr, expr); ir_emit_comment(proc, str_lit("TernaryExpr")); diff --git a/src/parser.cpp b/src/parser.cpp index a5526fd73..84c03587d 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -25,6 +25,11 @@ Token ast_token(Ast *node) { return ast_token(node->SelectorExpr.selector); } return node->SelectorExpr.token; + case Ast_ImplicitSelectorExpr: + if (node->ImplicitSelectorExpr.selector != nullptr) { + return ast_token(node->ImplicitSelectorExpr.selector); + } + return node->ImplicitSelectorExpr.token; case Ast_IndexExpr: return node->IndexExpr.open; case Ast_SliceExpr: return node->SliceExpr.open; case Ast_Ellipsis: return node->Ellipsis.token; @@ -165,6 +170,9 @@ Ast *clone_ast(Ast *node) { n->SelectorExpr.expr = clone_ast(n->SelectorExpr.expr); n->SelectorExpr.selector = clone_ast(n->SelectorExpr.selector); break; + case Ast_ImplicitSelectorExpr: + n->ImplicitSelectorExpr.selector = clone_ast(n->ImplicitSelectorExpr.selector); + break; case Ast_IndexExpr: n->IndexExpr.expr = clone_ast(n->IndexExpr.expr); n->IndexExpr.index = clone_ast(n->IndexExpr.index); @@ -504,11 +512,20 @@ Ast *ast_call_expr(AstFile *f, Ast *proc, Array args, Token open, Token c Ast *ast_selector_expr(AstFile *f, Token token, Ast *expr, Ast *selector) { Ast *result = alloc_ast_node(f, Ast_SelectorExpr); + result->SelectorExpr.token = token; result->SelectorExpr.expr = expr; result->SelectorExpr.selector = selector; return result; } +Ast *ast_implicit_selector_expr(AstFile *f, Token token, Ast *selector) { + Ast *result = alloc_ast_node(f, Ast_ImplicitSelectorExpr); + result->ImplicitSelectorExpr.token = token; + result->ImplicitSelectorExpr.selector = selector; + return result; +} + + Ast *ast_index_expr(AstFile *f, Ast *expr, Ast *index, Token open, Token close) { Ast *result = alloc_ast_node(f, Ast_IndexExpr); result->IndexExpr.expr = expr; @@ -1612,7 +1629,6 @@ Ast *parse_operand(AstFile *f, bool lhs) { case Token_offset_of: return parse_call_expr(f, ast_implicit(f, advance_token(f))); - case Token_String: return ast_basic_lit(f, advance_token(f)); @@ -2277,6 +2293,12 @@ Ast *parse_unary_expr(AstFile *f, bool lhs) { Ast *expr = parse_unary_expr(f, lhs); return ast_unary_expr(f, token, expr); } + + case Token_Period: { + Token token = expect_token(f, Token_Period); + Ast *ident = parse_ident(f); + return ast_implicit_selector_expr(f, token, ident); + } } return parse_atom_expr(f, parse_operand(f, lhs), lhs); diff --git a/src/parser.hpp b/src/parser.hpp index d685aa1af..816f3f534 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -245,6 +245,7 @@ AST_KIND(_ExprBegin, "", bool) \ AST_KIND(BinaryExpr, "binary expression", struct { Token op; Ast *left, *right; } ) \ AST_KIND(ParenExpr, "parentheses expression", struct { Ast *expr; Token open, close; }) \ AST_KIND(SelectorExpr, "selector expression", struct { Token token; Ast *expr, *selector; }) \ + AST_KIND(ImplicitSelectorExpr, "implicit selector expression", struct { Token token; Ast *selector; }) \ AST_KIND(IndexExpr, "index expression", struct { Ast *expr, *index; Token open, close; }) \ AST_KIND(DerefExpr, "dereference expression", struct { Token op; Ast *expr; }) \ AST_KIND(SliceExpr, "slice expression", struct { \ -- cgit v1.2.3