authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 15:20:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-14 15:28:52-04:00
log42ea2d0d1c3b8cafdfc9a383cbb1bab274eb0140
treeb4cda1880f432c47c228747c356200b01604bafb
parent9e8db5b7505a6c2e601c8a94b9d8a4282b5df184
signaturelock-open Commit is signed but in an unrecognized format.

fix `@export` for arrays and allow sections on extern variables

previously `@export` for an array would panic with a TODO message. now it will do the export. However, it uses the variable's name rather than the name passed to `@export`. Issue #2679 remains open for that problem.

3 files changed, 11 insertions(+), 31 deletions(-)

src/analyze.cpp+2-10
...@@ -2780,12 +2780,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -2780,12 +2780,7 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
2780 fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry);2780 fn_table_entry->type_entry = analyze_fn_type(g, source_node, child_scope, fn_table_entry);
27812781
2782 if (fn_proto->section_expr != nullptr) {2782 if (fn_proto->section_expr != nullptr) {
2783 if (fn_table_entry->body_node == nullptr) {2783 analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
2784 add_node_error(g, fn_proto->section_expr,
2785 buf_sprintf("cannot set section of external function '%s'", buf_ptr(&fn_table_entry->symbol_name)));
2786 } else {
2787 analyze_const_string(g, child_scope, fn_proto->section_expr, &fn_table_entry->section_name);
2788 }
2789 }2784 }
27902785
2791 if (fn_table_entry->type_entry->id == ZigTypeIdInvalid) {2786 if (fn_table_entry->type_entry->id == ZigTypeIdInvalid) {
...@@ -3258,10 +3253,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -3258,10 +3253,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
3258 }3253 }
32593254
3260 if (var_decl->section_expr != nullptr) {3255 if (var_decl->section_expr != nullptr) {
3261 if (var_decl->is_extern) {3256 if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
3262 add_node_error(g, var_decl->section_expr,
3263 buf_sprintf("cannot set section of external variable '%s'", buf_ptr(var_decl->symbol)));
3264 } else if (!analyze_const_string(g, tld_var->base.parent_scope, var_decl->section_expr, &tld_var->section_name)) {
3265 tld_var->section_name = nullptr;3257 tld_var->section_name = nullptr;
3266 }3258 }
3267 }3259 }
src/ir.cpp+9-1
...@@ -13901,6 +13901,15 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio...@@ -13901,6 +13901,15 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
13901 want_var_export = true;13901 want_var_export = true;
13902 }13902 }
13903 break;13903 break;
13904 case ZigTypeIdArray:
13905 if (!type_allowed_in_extern(ira->codegen, target->value.type->data.array.child_type)) {
13906 ir_add_error(ira, target,
13907 buf_sprintf("array element type '%s' not extern-compatible",
13908 buf_ptr(&target->value.type->data.array.child_type->name)));
13909 } else {
13910 want_var_export = true;
13911 }
13912 break;
13904 case ZigTypeIdMetaType: {13913 case ZigTypeIdMetaType: {
13905 ZigType *type_value = target->value.data.x_type;13914 ZigType *type_value = target->value.data.x_type;
13906 switch (type_value->id) {13915 switch (type_value->id) {
...@@ -13968,7 +13977,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio...@@ -13968,7 +13977,6 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
13968 case ZigTypeIdInt:13977 case ZigTypeIdInt:
13969 case ZigTypeIdFloat:13978 case ZigTypeIdFloat:
13970 case ZigTypeIdPointer:13979 case ZigTypeIdPointer:
13971 case ZigTypeIdArray:
13972 case ZigTypeIdComptimeFloat:13980 case ZigTypeIdComptimeFloat:
13973 case ZigTypeIdComptimeInt:13981 case ZigTypeIdComptimeInt:
13974 case ZigTypeIdUndefined:13982 case ZigTypeIdUndefined:
test/compile_errors.zig-20
...@@ -4645,16 +4645,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4645,16 +4645,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4645 "tmp.zig:1:1: note: declared here",4645 "tmp.zig:1:1: note: declared here",
4646 );4646 );
46474647
4648 cases.add(
4649 "setting a section on an extern variable",
4650 \\extern var foo: i32 linksection(".text2");
4651 \\export fn entry() i32 {
4652 \\ return foo;
4653 \\}
4654 ,
4655 "tmp.zig:1:33: error: cannot set section of external variable 'foo'",
4656 );
4657
4658 cases.add(4648 cases.add(
4659 "setting a section on a local variable",4649 "setting a section on a local variable",
4660 \\export fn entry() i32 {4650 \\export fn entry() i32 {
...@@ -4665,16 +4655,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -4665,16 +4655,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
4665 "tmp.zig:2:30: error: cannot set section of local variable 'foo'",4655 "tmp.zig:2:30: error: cannot set section of local variable 'foo'",
4666 );4656 );
46674657
4668 cases.add(
4669 "setting a section on an extern fn",
4670 \\extern fn foo() linksection(".text2") void;
4671 \\export fn entry() void {
4672 \\ foo();
4673 \\}
4674 ,
4675 "tmp.zig:1:29: error: cannot set section of external function 'foo'",
4676 );
4677
4678 cases.add(4658 cases.add(
4679 "returning address of local variable - simple",4659 "returning address of local variable - simple",
4680 \\export fn foo() *i32 {4660 \\export fn foo() *i32 {