authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-30 23:06:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-30 23:06:29-07:00
log31cf43de54c012c3b6f4fa7a516e2aac0ae18b56
treee68740ae147e571c4e0847332f4415f524f8900c
parentcd68969115f19b09aeb72923426e722725ba9451

analyze looks for root export decl only in the root source file


3 files changed, 49 insertions(+), 41 deletions(-)

src/analyze.cpp+44-38
......@@ -175,41 +175,46 @@ static void preview_function_declarations(CodeGen *g, ImportTableEntry *import,
175175 }
176176 break;
177177 case NodeTypeRootExportDecl:
178 for (int i = 0; i < node->data.root_export_decl.directives->length; i += 1) {
179 AstNode *directive_node = node->data.root_export_decl.directives->at(i);
180 Buf *name = &directive_node->data.directive.name;
181 Buf *param = &directive_node->data.directive.param;
182 if (buf_eql_str(name, "version")) {
183 set_root_export_version(g, param, directive_node);
184 } else {
185 add_node_error(g, directive_node,
186 buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
178 if (import == g->root_import) {
179 for (int i = 0; i < node->data.root_export_decl.directives->length; i += 1) {
180 AstNode *directive_node = node->data.root_export_decl.directives->at(i);
181 Buf *name = &directive_node->data.directive.name;
182 Buf *param = &directive_node->data.directive.param;
183 if (buf_eql_str(name, "version")) {
184 set_root_export_version(g, param, directive_node);
185 } else {
186 add_node_error(g, directive_node,
187 buf_sprintf("invalid directive: '%s'", buf_ptr(name)));
188 }
187189 }
188 }
189190
190 if (g->root_export_decl) {
191 add_node_error(g, node,
192 buf_sprintf("only one root export declaration allowed"));
193 } else {
194 g->root_export_decl = node;
195
196 if (!g->root_out_name)
197 g->root_out_name = &node->data.root_export_decl.name;
198
199 Buf *out_type = &node->data.root_export_decl.type;
200 OutType export_out_type;
201 if (buf_eql_str(out_type, "executable")) {
202 export_out_type = OutTypeExe;
203 } else if (buf_eql_str(out_type, "library")) {
204 export_out_type = OutTypeLib;
205 } else if (buf_eql_str(out_type, "object")) {
206 export_out_type = OutTypeObj;
207 } else {
191 if (g->root_export_decl) {
208192 add_node_error(g, node,
209 buf_sprintf("invalid export type: '%s'", buf_ptr(out_type)));
193 buf_sprintf("only one root export declaration allowed"));
194 } else {
195 g->root_export_decl = node;
196
197 if (!g->root_out_name)
198 g->root_out_name = &node->data.root_export_decl.name;
199
200 Buf *out_type = &node->data.root_export_decl.type;
201 OutType export_out_type;
202 if (buf_eql_str(out_type, "executable")) {
203 export_out_type = OutTypeExe;
204 } else if (buf_eql_str(out_type, "library")) {
205 export_out_type = OutTypeLib;
206 } else if (buf_eql_str(out_type, "object")) {
207 export_out_type = OutTypeObj;
208 } else {
209 add_node_error(g, node,
210 buf_sprintf("invalid export type: '%s'", buf_ptr(out_type)));
211 }
212 if (g->out_type == OutTypeUnknown)
213 g->out_type = export_out_type;
210214 }
211 if (g->out_type == OutTypeUnknown)
212 g->out_type = export_out_type;
215 } else {
216 add_node_error(g, node,
217 buf_sprintf("root export declaration only valid in root source file"));
213218 }
214219 break;
215220 case NodeTypeUse:
......@@ -428,13 +433,6 @@ static void analyze_root(CodeGen *g, ImportTableEntry *import, AstNode *node) {
428433 analyze_top_level_declaration(g, child);
429434 }
430435
431 if (!g->root_out_name) {
432 add_node_error(g, node,
433 buf_sprintf("missing export declaration and output name not provided"));
434 } else if (g->out_type == OutTypeUnknown) {
435 add_node_error(g, node,
436 buf_sprintf("missing export declaration and export type not provided"));
437 }
438436}
439437
440438void semantic_analyze(CodeGen *g) {
......@@ -447,4 +445,12 @@ void semantic_analyze(CodeGen *g) {
447445 ImportTableEntry *import = entry->value;
448446 analyze_root(g, import, import->root);
449447 }
448
449 if (!g->root_out_name) {
450 add_node_error(g, g->root_import->root,
451 buf_sprintf("missing export declaration and output name not provided"));
452 } else if (g->out_type == OutTypeUnknown) {
453 add_node_error(g, g->root_import->root,
454 buf_sprintf("missing export declaration and export type not provided"));
455 }
450456}
src/codegen.cpp+4-3
......@@ -661,7 +661,7 @@ static void init(CodeGen *g, Buf *source_path) {
661661
662662}
663663
664static void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
664static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
665665 Buf full_path = BUF_INIT;
666666 os_path_join(g->root_source_dir, source_path, &full_path);
667667
......@@ -715,13 +715,14 @@ static void codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) {
715715 codegen_add_code(g, &top_level_decl->data.use.path, &import_code);
716716 }
717717 }
718
719 return import_entry;
718720}
719721
720722void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
721723 init(g, source_path);
722724
723 codegen_add_code(g, source_path, source_code);
724
725 g->root_import = codegen_add_code(g, source_path, source_code);
725726
726727 if (g->verbose) {
727728 fprintf(stderr, "\nSemantic Analysis:\n");
src/semantic_info.hpp+1
......@@ -86,6 +86,7 @@ struct CodeGen {
8686 int version_minor;
8787 int version_patch;
8888 bool verbose;
89 ImportTableEntry *root_import;
8990};
9091
9192struct TypeNode {