authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-10 15:39:52-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-01-10 15:39:52-05:00
log430e33b869b004ca24faee2dfa9e51aa4e94093f
tree8b5b3124c51214ac78151eddf28822f8e9d050c0
parent8d27a027051e93e6f63aa9b969fed1dbfcf2aa73

partially fix parseh command


8 files changed, 102 insertions(+), 21 deletions(-)

src/analyze.cpp+7-7
......@@ -870,26 +870,26 @@ static TypeTableEntryId container_to_type(ContainerKind kind) {
870870 zig_unreachable();
871871}
872872
873TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name) {
873TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name, bool is_extern) {
874874 TypeTableEntryId type_id = container_to_type(kind);
875875 TypeTableEntry *entry = new_container_type_entry(type_id, decl_node, scope);
876876
877877 switch (kind) {
878878 case ContainerKindStruct:
879879 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;
881881 break;
882882 case ContainerKindEnum:
883883 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;
885885 break;
886886 case ContainerKindUnion:
887887 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;
889889 break;
890890 }
891891
892 unsigned line = decl_node->line;
892 unsigned line = decl_node ? decl_node->line : 0;
893893
894894 ImportTableEntry *import = get_scope_import(scope);
895895 entry->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), name);
......@@ -1688,7 +1688,7 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
16881688 tld->name = name;
16891689 tld->visib_mod = visib_mod;
16901690 tld->source_node = source_node;
1691 tld->import = source_node->owner;
1691 tld->import = source_node ? source_node->owner : nullptr;
16921692 tld->parent_scope = parent_scope;
16931693}
16941694
......@@ -1895,7 +1895,7 @@ VariableTableEntry *add_variable(CodeGen *g, AstNode *source_node, Scope *parent
18951895 }
18961896
18971897 Scope *child_scope;
1898 if (source_node->type == NodeTypeParamDecl) {
1898 if (source_node && source_node->type == NodeTypeParamDecl) {
18991899 child_scope = create_var_scope(source_node, parent_scope, variable_entry);
19001900 } else {
19011901 // 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);
2626TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);
2727TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);
2828TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
29TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name);
29TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKind kind, AstNode *decl_node, const char *name, bool is_extern);
3030TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
3131TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type);
3232TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry);
src/ast_render.cpp+81
......@@ -6,6 +6,7 @@
66 */
77
88#include "ast_render.hpp"
9#include "analyze.hpp"
910
1011#include <stdio.h>
1112
......@@ -891,3 +892,83 @@ void ast_render(FILE *f, AstNode *node, int indent_size) {
891892 render_node_grouped(&ar, node);
892893}
893894
895static 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
917static 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
944void 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);
1919
2020const char *container_string(ContainerKind kind);
2121
22void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import);
23
2224#endif
2325
src/codegen.cpp+1-5
......@@ -3713,6 +3713,7 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source
37133713 import->source_code = source_code;
37143714 import->path = full_path;
37153715 g->root_import = import;
3716 import->decls_scope = create_decls_scope(nullptr, nullptr, nullptr, import);
37163717
37173718 init(g, full_path);
37183719
......@@ -3734,11 +3735,6 @@ void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source
37343735 }
37353736}
37363737
3737void codegen_render_ast(CodeGen *g, FILE *f, int indent_size) {
3738 ast_render(stdout, g->root_import->root, 4);
3739}
3740
3741
37423738static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) {
37433739 Buf *std_dir = g->zig_std_dir;
37443740 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,
50315031 TldContainer *tld_container = allocate<TldContainer>(1);
50325032 init_tld(&tld_container->base, TldIdContainer, name, visib_mod, node, parent_scope);
50335033
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);
50355036 ScopeDecls *child_scope = get_container_scope(container_type);
50365037
50375038 tld_container->type_entry = container_type;
src/main.cpp+5-4
......@@ -5,13 +5,14 @@
55 * See http://opensource.org/licenses/MIT
66 */
77
8#include "config.h"
8#include "ast_render.hpp"
99#include "buffer.hpp"
1010#include "codegen.hpp"
11#include "os.hpp"
11#include "config.h"
1212#include "error.hpp"
13#include "target.hpp"
1413#include "link.hpp"
14#include "os.hpp"
15#include "target.hpp"
1516
1617#include <stdio.h>
1718
......@@ -410,7 +411,7 @@ int main(int argc, char **argv) {
410411 return EXIT_SUCCESS;
411412 } else if (cmd == CmdParseH) {
412413 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);
414415 return EXIT_SUCCESS;
415416 } else if (cmd == CmdTest) {
416417 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)
687687 const EnumDecl *enum_def = enum_decl->getDefinition();
688688 if (!enum_def) {
689689 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);
691691 enum_type->data.enumeration.zero_bits_known = true;
692692 c->enum_type_table.put(bare_name, enum_type);
693693 c->decl_table.put(enum_decl, enum_type);
......@@ -712,7 +712,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
712712
713713 if (pure_enum) {
714714 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);
716716 c->enum_type_table.put(bare_name, enum_type);
717717 c->decl_table.put(enum_decl, enum_type);
718718
......@@ -852,7 +852,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
852852
853853
854854 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);
856856 struct_type->data.structure.zero_bits_known = true;
857857
858858 c->struct_type_table.put(bare_name, struct_type);