authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-01 18:07:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-02 18:31:18-04:00
logd7bc7635c0e3324fa08fc809ec21ba014464ea03
tree3e7961eee42f99f1a43374459bcc4aac7fd037cf
parentd3f2fe2cef7de1173a038365f91e8a574a08f21a
signaturelock-open Commit is signed but in an unrecognized format.

put the hack from master branch back in


1 files changed, 44 insertions(+), 39 deletions(-)

src/analyze.cpp+44-39
......@@ -2038,6 +2038,8 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20382038static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20392039 assert(struct_type->id == ZigTypeIdStruct);
20402040
2041 Error err;
2042
20412043 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
20422044 return ErrorSemanticAnalyzeFail;
20432045 if (struct_type->data.structure.resolve_status >= ResolveStatusZeroBitsKnown)
......@@ -2047,13 +2049,12 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20472049 assert(decl_node->type == NodeTypeContainerDecl);
20482050
20492051 if (struct_type->data.structure.resolve_loop_flag) {
2050 if (struct_type->data.structure.resolve_status != ResolveStatusInvalid) {
2051 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2052 ErrorMsg *msg = add_node_error(g, decl_node,
2053 buf_sprintf("struct '%s' depends on its own size", buf_ptr(&struct_type->name)));
2054 emit_error_notes_for_ref_stack(g, msg);
2055 }
2056 return ErrorSemanticAnalyzeFail;
2052 // TODO This is a problem. I believe it can be solved with lazy values.
2053 struct_type->size_in_bits = SIZE_MAX;;
2054 struct_type->abi_size = SIZE_MAX;;
2055 struct_type->data.structure.resolve_status = ResolveStatusZeroBitsKnown;
2056 struct_type->data.structure.resolve_loop_flag = false;
2057 return ErrorNone;
20572058 }
20582059
20592060 struct_type->data.structure.resolve_loop_flag = true;
......@@ -2097,6 +2098,21 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
20972098 if (struct_type->data.structure.resolve_status == ResolveStatusInvalid)
20982099 return ErrorSemanticAnalyzeFail;
20992100
2101 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2102 !type_allowed_in_extern(g, field_type))
2103 {
2104 add_node_error(g, field_node,
2105 buf_sprintf("extern structs cannot contain fields of type '%s'",
2106 buf_ptr(&field_type->name)));
2107 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2108 return ErrorSemanticAnalyzeFail;
2109 } else if (struct_type->data.structure.layout == ContainerLayoutPacked) {
2110 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_node))) {
2111 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2112 return ErrorSemanticAnalyzeFail;
2113 }
2114 }
2115
21002116 type_struct_field->src_index = i;
21012117 type_struct_field->gen_index = SIZE_MAX;
21022118
......@@ -2171,49 +2187,39 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
21712187 struct_type->data.structure.resolve_loop_flag = true;
21722188 assert(decl_node->type == NodeTypeContainerDecl);
21732189
2174 size_t abi_align = 0;
21752190 size_t field_count = struct_type->data.structure.src_field_count;
21762191 bool packed = struct_type->data.structure.layout == ContainerLayoutPacked;
21772192
21782193 for (size_t i = 0; i < field_count; i += 1) {
2179 AstNode *field_source_node = decl_node->data.container_decl.fields.at(i);
21802194 TypeStructField *field = &struct_type->data.structure.fields[i];
2181 ZigType *field_type = field->type_entry;
2182 assert(field_type != nullptr);
2183
2184 if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
2185 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2186 return ErrorSemanticAnalyzeFail;
2187 }
2188
2189 if (struct_type->data.structure.layout == ContainerLayoutExtern &&
2190 !type_allowed_in_extern(g, field_type))
2191 {
2192 add_node_error(g, field_source_node,
2193 buf_sprintf("extern structs cannot contain fields of type '%s'",
2194 buf_ptr(&field_type->name)));
2195 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2196 return ErrorSemanticAnalyzeFail;
2197 } else if (packed) {
2198 if ((err = emit_error_unless_type_allowed_in_packed_struct(g, field_type, field_source_node))) {
2199 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2200 return ErrorSemanticAnalyzeFail;
2201 }
2202 }
2203
22042195 if (field->gen_index == SIZE_MAX)
22052196 continue;
22062197
2198 size_t this_field_align;
22072199 if (packed) {
22082200 // TODO: https://github.com/ziglang/zig/issues/1512
2209 if (1 > abi_align) {
2210 abi_align = 1;
2211 }
2201 this_field_align = 1;
2202 // TODO If we have no type_entry for the field, we've already failed to
2203 // compile the program correctly. This stage1 compiler needs a deeper
2204 // reworking to make this correct, or we can ignore the problem
2205 // and make sure it is fixed in stage2. This workaround is for when
2206 // there is a false positive of a dependency loop, of alignment depending
2207 // on itself. When this false positive happens we assume a pointer-aligned
2208 // field, which is usually fine but could be incorrectly over-aligned or
2209 // even under-aligned. See https://github.com/ziglang/zig/issues/1512
2210 } else if (field->type_entry == nullptr) {
2211 this_field_align = g->builtin_types.entry_usize->abi_align;
22122212 } else {
2213 // TODO: https://github.com/ziglang/zig/issues/1512
2214 if (field_type->abi_align > abi_align) {
2215 abi_align = field_type->abi_align;
2213 if ((err = type_resolve(g, field->type_entry, ResolveStatusAlignmentKnown))) {
2214 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2215 return ErrorSemanticAnalyzeFail;
22162216 }
2217 this_field_align = field->type_entry->abi_align;
2218 }
2219
2220 // TODO: https://github.com/ziglang/zig/issues/1512
2221 if (this_field_align > struct_type->abi_align) {
2222 struct_type->abi_align = this_field_align;
22172223 }
22182224 }
22192225
......@@ -2224,7 +2230,6 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
22242230 }
22252231
22262232 struct_type->data.structure.resolve_status = ResolveStatusAlignmentKnown;
2227 struct_type->abi_align = abi_align;
22282233 return ErrorNone;
22292234}
22302235