From f19bb0f4d45ba4352c4392807ca2b1efe5918ea7 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 8 Jun 2021 14:33:49 +0100 Subject: [PATCH] Make default calling convention code more correct to read --- src/check_decl.cpp | 6 +++--- src/docs_writer.cpp | 10 ++++++++-- src/parser.cpp | 2 +- src/parser.hpp | 22 +++++++++++++--------- src/types.cpp | 7 ++++++- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 323de6d43..1a87b57b8 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -648,7 +648,7 @@ void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) { if (d->gen_proc_type != nullptr) { proc_type = d->gen_proc_type; } else { - proc_type = alloc_type_proc(e->scope, nullptr, 0, nullptr, 0, false, ProcCC_Odin); + proc_type = alloc_type_proc(e->scope, nullptr, 0, nullptr, 0, false, default_calling_convention()); } e->type = proc_type; ast_node(pl, ProcLit, d->proc_lit); @@ -746,10 +746,10 @@ void check_proc_decl(CheckerContext *ctx, Entity *e, DeclInfo *d) { error(e->token, "Procedure type of 'main' was expected to be 'proc()', got %s", str); gb_string_free(str); } - if (pt->calling_convention != ProcCC_Odin) { + if (pt->calling_convention != default_calling_convention()) { error(e->token, "Procedure 'main' cannot have a custom calling convention"); } - pt->calling_convention = ProcCC_Odin; + pt->calling_convention = default_calling_convention(); if (e->pkg->kind == Package_Init) { if (ctx->info->entry_point != nullptr) { error(e->token, "Redeclaration of the entry pointer procedure 'main'"); diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index fb4573b9f..19f5ae156 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -676,11 +676,17 @@ OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { String calling_convention = {}; switch (type->Proc.calling_convention) { case ProcCC_Invalid: - case ProcCC_Odin: // no need break; + case ProcCC_Odin: + if (default_calling_convention() != ProcCC_Odin) { + calling_convention = str_lit("odin"); + } + break; case ProcCC_Contextless: - calling_convention = str_lit("contextless"); + if (default_calling_convention() != ProcCC_Contextless) { + calling_convention = str_lit("contextless"); + } break; case ProcCC_CDecl: calling_convention = str_lit("cdecl"); diff --git a/src/parser.cpp b/src/parser.cpp index 404f82c07..50584eb51 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -3232,7 +3232,7 @@ Ast *parse_proc_type(AstFile *f, Token proc_token) { if (f->in_foreign_block) { cc = ProcCC_ForeignBlockDefault; } else { - cc = ProcCC_Odin; + cc = default_calling_convention(); } } diff --git a/src/parser.hpp b/src/parser.hpp index 060fd56d3..7c278b304 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -208,18 +208,18 @@ enum ProcTag { ProcTag_optional_second = 1<<6, }; -enum ProcCallingConvention { - ProcCC_Invalid = 0, - ProcCC_Odin = 1, +enum ProcCallingConvention : i32 { + ProcCC_Invalid = 0, + ProcCC_Odin = 1, ProcCC_Contextless = 2, - ProcCC_CDecl = 3, - ProcCC_StdCall = 4, - ProcCC_FastCall = 5, + ProcCC_CDecl = 3, + ProcCC_StdCall = 4, + ProcCC_FastCall = 5, - ProcCC_None = 6, - ProcCC_Naked = 7, + ProcCC_None = 6, + ProcCC_Naked = 7, - ProcCC_InlineAsm = 8, + ProcCC_InlineAsm = 8, ProcCC_MAX, @@ -227,6 +227,10 @@ enum ProcCallingConvention { ProcCC_ForeignBlockDefault = -1, }; +ProcCallingConvention default_calling_convention(void) { + return ProcCC_Odin; +} + enum StateFlag : u16 { StateFlag_bounds_check = 1<<0, StateFlag_no_bounds_check = 1<<1, diff --git a/src/types.cpp b/src/types.cpp index 04ac31a01..f36765641 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -3618,9 +3618,14 @@ gbString write_type_to_string(gbString str, Type *type) { switch (type->Proc.calling_convention) { case ProcCC_Odin: + if (default_calling_convention() != ProcCC_Odin) { + str = gb_string_appendc(str, " \"odin\" "); + } break; case ProcCC_Contextless: - str = gb_string_appendc(str, " \"contextless\" "); + if (default_calling_convention() != ProcCC_Contextless) { + str = gb_string_appendc(str, " \"contextless\" "); + } break; case ProcCC_CDecl: str = gb_string_appendc(str, " \"cdecl\" ");