| author | |
| committer | |
| log | 430e33b869b004ca24faee2dfa9e51aa4e94093f |
| tree | 8b5b3124c51214ac78151eddf28822f8e9d050c0 |
| parent | 8d27a027051e93e6f63aa9b969fed1dbfcf2aa73 |
8 files changed, 102 insertions(+), 21 deletions(-)
src/analyze.cpp+7-7| ... | @@ -870,26 +870,26 @@ static TypeTableEntryId container_to_type(ContainerKind kind) { | ... | @@ -870,26 +870,26 @@ static TypeTableEntryId container_to_type(ContainerKind kind) { |
| 870 | zig_unreachable(); | 870 | zig_unreachable(); |
| 871 | } | 871 | } |
| 872 | 872 | ||
| 873 | TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name) { | 873 | TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name, bool is_extern) { |
| 874 | TypeTableEntryId type_id = container_to_type(kind); | 874 | TypeTableEntryId type_id = container_to_type(kind); |
| 875 | TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope); | 875 | TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope); |
| 876 | 876 | ||
| 877 | switch (kind) { | 877 | switch (kind) { |
| 878 | case ContainerKindStruct: | 878 | case ContainerKindStruct: |
| 879 | entry->data.structure.decl_node = decl_node; | 879 | entry->data.structure.decl_node = decl_node; |
| 880 | entry->data.structure.is_extern = decl_node->data.container_decl.is_extern; | 880 | entry->data.structure.is_extern = is_extern; |
| 881 | break; | 881 | break; |
| 882 | case ContainerKindEnum: | 882 | case ContainerKindEnum: |
| 883 | entry->data.enumeration.decl_node = decl_node; | 883 | entry->data.enumeration.decl_node = decl_node; |
| 884 | entry->data.enumeration.is_extern = decl_node->data.container_decl.is_extern; | 884 | entry->data.enumeration.is_extern = is_extern; |
| 885 | break; | 885 | break; |
| 886 | case ContainerKindUnion: | 886 | case ContainerKindUnion: |
| 887 | entry->data.unionation.decl_node = decl_node; | 887 | entry->data.unionation.decl_node = decl_node; |
| 888 | entry->data.unionation.is_extern = decl_node->data.container_decl.is_extern; | 888 | entry->data.unionation.is_extern = is_extern; |
| 889 | break; | 889 | break; |
| 890 | } | 890 | } |
| 891 | 891 | ||
| 892 | unsigned line = decl_node->line; | 892 | unsigned line = decl_node ? decl_node->line : 0; |
| 893 | 893 | ||
| 894 | ImportTableEntry *import = get_scope_import(scope); | 894 | ImportTableEntry *import = get_scope_import(scope); |
| 895 | entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name); | 895 | entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name); |
| ... | @@ -1688,7 +1688,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source | ... | @@ -1688,7 +1688,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source |
| 1688 | tld->name = name; | 1688 | tld->name = name; |
| 1689 | tld->visib_mod = visib_mod; | 1689 | tld->visib_mod = visib_mod; |
| 1690 | tld->source_node = source_node; | 1690 | tld->source_node = source_node; |
| 1691 | tld->import = source_node->owner; | 1691 | tld->import = source_node ? source_node->owner : nullptr; |
| 1692 | tld->parent_scope = parent_scope; | 1692 | tld->parent_scope = parent_scope; |
| 1693 | } | 1693 | } |
| 1694 | 1694 | ||
| ... | @@ -1895,7 +1895,7 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent | ... | @@ -1895,7 +1895,7 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent |
| 1895 | } | 1895 | } |
| 1896 | 1896 | ||
| 1897 | Scope *child_scope; | 1897 | Scope *child_scope; |
| 1898 | if (source_node->type == NodeTypeParamDecl) { | 1898 | if (source_node && source_node->type == NodeTypeParamDecl) { |
| 1899 | child_scope = create_var_scope(source_node, parent_scope, variable_entry); | 1899 | child_scope = create_var_scope(source_node, parent_scope, variable_entry); |
| 1900 | } else { | 1900 | } else { |
| 1901 | // it's already in the decls table | 1901 | // it's already in the decls table |
src/analyze.hpp+1-1| ... | @@ -26,7 +26,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id); | ... | @@ -26,7 +26,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id); |
| 26 | TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type); | 26 | TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type); |
| 27 | TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size); | 27 | TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size); |
| 28 | TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_const); | 28 | TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_const); |
| 29 | TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name); | 29 | TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name, bool is_extern); |
| 30 | TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x); | 30 | TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x); |
| 31 | TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type); | 31 | TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type); |
| 32 | TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry); | 32 | TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry); |
src/ast_render.cpp+81| ... | @@ -6,6 +6,7 @@ | ... | @@ -6,6 +6,7 @@ |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include "ast_render.hpp" | 8 | #include "ast_render.hpp" |
| 9 | #include "analyze.hpp" | ||
| 9 | 10 | ||
| 10 | #include <stdio.h> | 11 | #include <stdio.h> |
| 11 | 12 | ||
| ... | @@ -891,3 +892,83 @@ void ast_render(FILE *f, AstNode *node, int indent_size) { | ... | @@ -891,3 +892,83 @@ void ast_render(FILE *f, AstNode *node, int indent_size) { |
| 891 | render_node_grouped(&ar, node); | 892 | render_node_grouped(&ar, node); |
| 892 | } | 893 | } |
| 893 | 894 | ||
| 895 | static void ast_render_tld_fn(AstRender *ar, TldFn *tld_fn) { | ||
| 896 | FnTableEntry *fn_entry = tld_fn->fn_entry; | ||
| 897 | FnTypeId *fn_type_id = &fn_entry->type_entry->data.fn.fn_type_id; | ||
| 898 | const char *visib_mod_str = visib_mod_string(tld_fn->base.visib_mod); | ||
| 899 | const char *extern_str = extern_string(fn_type_id->is_extern); | ||
| 900 | const char *coldcc_str = fn_type_id->is_cold ? "coldcc " : ""; | ||
| 901 | const char *nakedcc_str = fn_type_id->is_naked ? "nakedcc " : ""; | ||
| 902 | fprintf(ar->f, "%s%s%s%sfn %s(", visib_mod_str, extern_str, coldcc_str, nakedcc_str, buf_ptr(&fn_entry->symbol_name)); | ||
| 903 | for (size_t i = 0; i < fn_type_id->param_count; i += 1) { | ||
| 904 | FnTypeParamInfo *param_info = &fn_type_id->param_info[i]; | ||
| 905 | if (param_info->is_noalias) { | ||
| 906 | fprintf(ar->f, "noalias "); | ||
| 907 | } | ||
| 908 | fprintf(ar->f, "arg_%zu: %s", i, buf_ptr(&param_info->type->name)); | ||
| 909 | } | ||
| 910 | if (fn_type_id->return_type->id == TypeTableEntryIdVoid) { | ||
| 911 | fprintf(ar->f, ");\n"); | ||
| 912 | } else { | ||
| 913 | fprintf(ar->f, ") -> %s;\n", buf_ptr(&fn_type_id->return_type->name)); | ||
| 914 | } | ||
| 915 | } | ||
| 916 | |||
| 917 | static void ast_render_tld_var(AstRender *ar, TldVar *tld_var) { | ||
| 918 | VariableTableEntry *var = tld_var->var; | ||
| 919 | const char *visib_mod_str = visib_mod_string(tld_var->base.visib_mod); | ||
| 920 | const char *const_or_var = const_or_var_string(var->src_is_const); | ||
| 921 | const char *extern_str = extern_string(var->is_extern); | ||
| 922 | fprintf(ar->f, "%s%s%s %s", visib_mod_str, extern_str, const_or_var, buf_ptr(&var->name)); | ||
| 923 | |||
| 924 | if (var->value.type->id == TypeTableEntryIdNumLitFloat || | ||
| 925 | var->value.type->id == TypeTableEntryIdNumLitInt) | ||
| 926 | { | ||
| 927 | // skip type | ||
| 928 | } else { | ||
| 929 | fprintf(ar->f, ": %s", buf_ptr(&var->value.type->name)); | ||
| 930 | } | ||
| 931 | |||
| 932 | if (var->value.special == ConstValSpecialRuntime) { | ||
| 933 | fprintf(ar->f, ";\n"); | ||
| 934 | return; | ||
| 935 | } | ||
| 936 | |||
| 937 | Buf buf = BUF_INIT; | ||
| 938 | buf_resize(&buf, 0); | ||
| 939 | render_const_value(&buf, &var->value); | ||
| 940 | |||
| 941 | fprintf(ar->f, " = %s;\n", buf_ptr(&buf)); | ||
| 942 | } | ||
| 943 | |||
| 944 | void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) { | ||
| 945 | AstRender ar = {0}; | ||
| 946 | ar.f = f; | ||
| 947 | ar.indent_size = indent_size; | ||
| 948 | ar.indent = 0; | ||
| 949 | |||
| 950 | auto it = import->decls_scope->decl_table.entry_iterator(); | ||
| 951 | for (;;) { | ||
| 952 | auto *entry = it.next(); | ||
| 953 | if (!entry) | ||
| 954 | break; | ||
| 955 | |||
| 956 | Tld *tld = entry->value; | ||
| 957 | switch (tld->id) { | ||
| 958 | case TldIdVar: | ||
| 959 | ast_render_tld_var(&ar, (TldVar *)tld); | ||
| 960 | break; | ||
| 961 | case TldIdFn: | ||
| 962 | ast_render_tld_fn(&ar, (TldFn *)tld); | ||
| 963 | break; | ||
| 964 | case TldIdContainer: | ||
| 965 | fprintf(stdout, "container\n"); | ||
| 966 | break; | ||
| 967 | case TldIdTypeDef: | ||
| 968 | fprintf(stdout, "typedef\n"); | ||
| 969 | break; | ||
| 970 | } | ||
| 971 | } | ||
| 972 | } | ||
| 973 | |||
| 974 |
src/ast_render.hpp+2| ... | @@ -19,5 +19,7 @@ void ast_render(FILE *f, AstNode *node, int indent_size); | ... | @@ -19,5 +19,7 @@ void ast_render(FILE *f, AstNode *node, int indent_size); |
| 19 | 19 | ||
| 20 | const char *container_string(ContainerKind kind); | 20 | const char *container_string(ContainerKind kind); |
| 21 | 21 | ||
| 22 | void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import); | ||
| 23 | |||
| 22 | #endif | 24 | #endif |
| 23 | 25 |
src/codegen.cpp+1-5| ... | @@ -3713,6 +3713,7 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source | ... | @@ -3713,6 +3713,7 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source |
| 3713 | import->source_code = source_code; | 3713 | import->source_code = source_code; |
| 3714 | import->path = full_path; | 3714 | import->path = full_path; |
| 3715 | g->root_import = import; | 3715 | g->root_import = import; |
| 3716 | import->decls_scope = create_decls_scope(nullptr, nullptr, nullptr, import); | ||
| 3716 | 3717 | ||
| 3717 | init(g, full_path); | 3718 | init(g, full_path); |
| 3718 | 3719 | ||
| ... | @@ -3734,11 +3735,6 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source | ... | @@ -3734,11 +3735,6 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source |
| 3734 | } | 3735 | } |
| 3735 | } | 3736 | } |
| 3736 | 3737 | ||
| 3737 | void codegen_render_ast(CodeGen *g, FILE *f, int indent_size) { | ||
| 3738 | ast_render(stdout, g->root_import->root, 4); | ||
| 3739 | } | ||
| 3740 | |||
| 3741 | |||
| 3742 | static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) { | 3738 | static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) { |
| 3743 | Buf *std_dir = g->zig_std_dir; | 3739 | Buf *std_dir = g->zig_std_dir; |
| 3744 | Buf *code_basename = buf_create_from_str(basename); | 3740 | Buf *code_basename = buf_create_from_str(basename); |
src/ir.cpp+2-1| ... | @@ -5031,7 +5031,8 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, | ... | @@ -5031,7 +5031,8 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, |
| 5031 | TldContainer *tld_container = allocate<TldContainer>(1); | 5031 | TldContainer *tld_container = allocate<TldContainer>(1); |
| 5032 | init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope); | 5032 | init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope); |
| 5033 | 5033 | ||
| 5034 | TypeTableEntry *container_type = get_partial_container_type(irb->codegen, parent_scope, kind, node, buf_ptr(name)); | 5034 | TypeTableEntry *container_type = get_partial_container_type(irb->codegen, parent_scope, kind, node, buf_ptr(name), |
| 5035 | node->data.container_decl.is_extern); | ||
| 5035 | ScopeDecls *child_scope = get_container_scope(container_type); | 5036 | ScopeDecls *child_scope = get_container_scope(container_type); |
| 5036 | 5037 | ||
| 5037 | tld_container->type_entry = container_type; | 5038 | tld_container->type_entry = container_type; |
src/main.cpp+5-4| ... | @@ -5,13 +5,14 @@ | ... | @@ -5,13 +5,14 @@ |
| 5 | * See http://opensource.org/licenses/MIT | 5 | * See http://opensource.org/licenses/MIT |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | #include "config.h" | 8 | #include "ast_render.hpp" |
| 9 | #include "buffer.hpp" | 9 | #include "buffer.hpp" |
| 10 | #include "codegen.hpp" | 10 | #include "codegen.hpp" |
| 11 | #include "os.hpp" | 11 | #include "config.h" |
| 12 | #include "error.hpp" | 12 | #include "error.hpp" |
| 13 | #include "target.hpp" | ||
| 14 | #include "link.hpp" | 13 | #include "link.hpp" |
| 14 | #include "os.hpp" | ||
| 15 | #include "target.hpp" | ||
| 15 | 16 | ||
| 16 | #include <stdio.h> | 17 | #include <stdio.h> |
| 17 | 18 | ||
| ... | @@ -410,7 +411,7 @@ int main(int argc, char **argv) { | ... | @@ -410,7 +411,7 @@ int main(int argc, char **argv) { |
| 410 | return EXIT_SUCCESS; | 411 | return EXIT_SUCCESS; |
| 411 | } else if (cmd == CmdParseH) { | 412 | } else if (cmd == CmdParseH) { |
| 412 | codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code); | 413 | codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code); |
| 413 | codegen_render_ast(g, stdout, 4); | 414 | ast_render_decls(stdout, 4, g->root_import); |
| 414 | return EXIT_SUCCESS; | 415 | return EXIT_SUCCESS; |
| 415 | } else if (cmd == CmdTest) { | 416 | } else if (cmd == CmdTest) { |
| 416 | codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code); | 417 | codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code); |
src/parseh.cpp+3-3| ... | @@ -687,7 +687,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) | ... | @@ -687,7 +687,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) |
| 687 | const EnumDecl *enum_def = enum_decl->getDefinition(); | 687 | const EnumDecl *enum_def = enum_decl->getDefinition(); |
| 688 | if (!enum_def) { | 688 | if (!enum_def) { |
| 689 | TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, | 689 | TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, |
| 690 | ContainerKindEnum, c->source_node, buf_ptr(full_type_name)); | 690 | ContainerKindEnum, c->source_node, buf_ptr(full_type_name), true); |
| 691 | enum_type->data.enumeration.zero_bits_known = true; | 691 | enum_type->data.enumeration.zero_bits_known = true; |
| 692 | c->enum_type_table.put(bare_name, enum_type); | 692 | c->enum_type_table.put(bare_name, enum_type); |
| 693 | c->decl_table.put(enum_decl, enum_type); | 693 | c->decl_table.put(enum_decl, enum_type); |
| ... | @@ -712,7 +712,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) | ... | @@ -712,7 +712,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) |
| 712 | 712 | ||
| 713 | if (pure_enum) { | 713 | if (pure_enum) { |
| 714 | TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, | 714 | TypeTableEntry *enum_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, |
| 715 | ContainerKindEnum, c->source_node, buf_ptr(full_type_name)); | 715 | ContainerKindEnum, c->source_node, buf_ptr(full_type_name), true); |
| 716 | c->enum_type_table.put(bare_name, enum_type); | 716 | c->enum_type_table.put(bare_name, enum_type); |
| 717 | c->decl_table.put(enum_decl, enum_type); | 717 | c->decl_table.put(enum_decl, enum_type); |
| 718 | 718 | ||
| ... | @@ -852,7 +852,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_ | ... | @@ -852,7 +852,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_ |
| 852 | 852 | ||
| 853 | 853 | ||
| 854 | TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, | 854 | TypeTableEntry *struct_type = get_partial_container_type(c->codegen, &c->import->decls_scope->base, |
| 855 | ContainerKindStruct, c->source_node, buf_ptr(full_type_name)); | 855 | ContainerKindStruct, c->source_node, buf_ptr(full_type_name), true); |
| 856 | struct_type->data.structure.zero_bits_known = true; | 856 | struct_type->data.structure.zero_bits_known = true; |
| 857 | 857 | ||
| 858 | c->struct_type_table.put(bare_name, struct_type); | 858 | c->struct_type_table.put(bare_name, struct_type); |