authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 03:07:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 03:07:58-04:00
logbf57d8a7e3beb8d69dec38e131a3717f008f6c5e
tree1559356eda32fa631d1bac1de5efb5dcf7be0d48
parentbf67427c67dac00ca10ed7423ae8d99e2901262f

typedefpocalypse

closes #314

14 files changed, 334 insertions(+), 620 deletions(-)

doc/langref.md+2-4
...@@ -9,9 +9,7 @@ TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestD...@@ -9,9 +9,7 @@ TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestD
99
10TestDecl = "test" String Block10TestDecl = "test" String Block
1111
12TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)12TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | UseDecl)
13
14TypeDecl = "type" Symbol "=" TypeExpr ";"
1513
16ErrorValueDecl = "error" Symbol ";"14ErrorValueDecl = "error" Symbol ";"
1715
...@@ -155,7 +153,7 @@ GotoExpression = "goto" Symbol...@@ -155,7 +153,7 @@ GotoExpression = "goto" Symbol
155153
156GroupedExpression = "(" Expression ")"154GroupedExpression = "(" Expression ")"
157155
158KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "type" | "this" | "unreachable"156KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "this" | "unreachable"
159157
160ContainerDecl = option("extern" | "packed") ("struct" | "enum" | "union") "{" many(ContainerMember) "}"158ContainerDecl = option("extern" | "packed") ("struct" | "enum" | "union") "{" many(ContainerMember) "}"
161```159```
src/all_types.hpp+1-27
...@@ -254,7 +254,6 @@ enum TldId {...@@ -254,7 +254,6 @@ enum TldId {
254 TldIdVar,254 TldIdVar,
255 TldIdFn,255 TldIdFn,
256 TldIdContainer,256 TldIdContainer,
257 TldIdTypeDef,
258 TldIdCompTime,257 TldIdCompTime,
259};258};
260259
...@@ -303,12 +302,6 @@ struct TldContainer {...@@ -303,12 +302,6 @@ struct TldContainer {
303 TypeTableEntry *type_entry;302 TypeTableEntry *type_entry;
304};303};
305304
306struct TldTypeDef {
307 Tld base;
308
309 TypeTableEntry *type_entry;
310};
311
312struct TldCompTime {305struct TldCompTime {
313 Tld base;306 Tld base;
314};307};
...@@ -330,7 +323,6 @@ enum NodeType {...@@ -330,7 +323,6 @@ enum NodeType {
330 NodeTypeReturnExpr,323 NodeTypeReturnExpr,
331 NodeTypeDefer,324 NodeTypeDefer,
332 NodeTypeVariableDeclaration,325 NodeTypeVariableDeclaration,
333 NodeTypeTypeDecl,
334 NodeTypeErrorValueDecl,326 NodeTypeErrorValueDecl,
335 NodeTypeTestDecl,327 NodeTypeTestDecl,
336 NodeTypeBinOpExpr,328 NodeTypeBinOpExpr,
...@@ -369,7 +361,6 @@ enum NodeType {...@@ -369,7 +361,6 @@ enum NodeType {
369 NodeTypeStructValueField,361 NodeTypeStructValueField,
370 NodeTypeArrayType,362 NodeTypeArrayType,
371 NodeTypeErrorType,363 NodeTypeErrorType,
372 NodeTypeTypeLiteral,
373 NodeTypeVarLiteral,364 NodeTypeVarLiteral,
374 NodeTypeTryExpr,365 NodeTypeTryExpr,
375 NodeTypeInlineExpr,366 NodeTypeInlineExpr,
...@@ -448,12 +439,6 @@ struct AstNodeVariableDeclaration {...@@ -448,12 +439,6 @@ struct AstNodeVariableDeclaration {
448 AstNode *expr;439 AstNode *expr;
449};440};
450441
451struct AstNodeTypeDecl {
452 VisibMod visib_mod;
453 Buf *symbol;
454 AstNode *child_type;
455};
456
457struct AstNodeErrorValueDecl {442struct AstNodeErrorValueDecl {
458 Buf *name;443 Buf *name;
459444
...@@ -790,9 +775,6 @@ struct AstNodeArrayType {...@@ -790,9 +775,6 @@ struct AstNodeArrayType {
790struct AstNodeErrorType {775struct AstNodeErrorType {
791};776};
792777
793struct AstNodeTypeLiteral {
794};
795
796struct AstNodeVarLiteral {778struct AstNodeVarLiteral {
797};779};
798780
...@@ -816,7 +798,6 @@ struct AstNode {...@@ -816,7 +798,6 @@ struct AstNode {
816 AstNodeReturnExpr return_expr;798 AstNodeReturnExpr return_expr;
817 AstNodeDefer defer;799 AstNodeDefer defer;
818 AstNodeVariableDeclaration variable_declaration;800 AstNodeVariableDeclaration variable_declaration;
819 AstNodeTypeDecl type_decl;
820 AstNodeErrorValueDecl error_value_decl;801 AstNodeErrorValueDecl error_value_decl;
821 AstNodeTestDecl test_decl;802 AstNodeTestDecl test_decl;
822 AstNodeBinOpExpr bin_op_expr;803 AstNodeBinOpExpr bin_op_expr;
...@@ -856,7 +837,6 @@ struct AstNode {...@@ -856,7 +837,6 @@ struct AstNode {
856 AstNodeUnreachableExpr unreachable_expr;837 AstNodeUnreachableExpr unreachable_expr;
857 AstNodeArrayType array_type;838 AstNodeArrayType array_type;
858 AstNodeErrorType error_type;839 AstNodeErrorType error_type;
859 AstNodeTypeLiteral type_literal;
860 AstNodeVarLiteral var_literal;840 AstNodeVarLiteral var_literal;
861 AstNodeInlineExpr inline_expr;841 AstNodeInlineExpr inline_expr;
862 } data;842 } data;
...@@ -1026,11 +1006,6 @@ struct TypeTableEntryBoundFn {...@@ -1026,11 +1006,6 @@ struct TypeTableEntryBoundFn {
1026 TypeTableEntry *fn_type;1006 TypeTableEntry *fn_type;
1027};1007};
10281008
1029struct TypeTableEntryTypeDecl {
1030 TypeTableEntry *child_type;
1031 TypeTableEntry *canonical_type;
1032};
1033
1034enum TypeTableEntryId {1009enum TypeTableEntryId {
1035 TypeTableEntryIdInvalid,1010 TypeTableEntryIdInvalid,
1036 TypeTableEntryIdVar,1011 TypeTableEntryIdVar,
...@@ -1054,11 +1029,11 @@ enum TypeTableEntryId {...@@ -1054,11 +1029,11 @@ enum TypeTableEntryId {
1054 TypeTableEntryIdEnumTag,1029 TypeTableEntryIdEnumTag,
1055 TypeTableEntryIdUnion,1030 TypeTableEntryIdUnion,
1056 TypeTableEntryIdFn,1031 TypeTableEntryIdFn,
1057 TypeTableEntryIdTypeDecl,
1058 TypeTableEntryIdNamespace,1032 TypeTableEntryIdNamespace,
1059 TypeTableEntryIdBlock,1033 TypeTableEntryIdBlock,
1060 TypeTableEntryIdBoundFn,1034 TypeTableEntryIdBoundFn,
1061 TypeTableEntryIdArgTuple,1035 TypeTableEntryIdArgTuple,
1036 TypeTableEntryIdOpaque,
1062};1037};
10631038
1064struct TypeTableEntry {1039struct TypeTableEntry {
...@@ -1083,7 +1058,6 @@ struct TypeTableEntry {...@@ -1083,7 +1058,6 @@ struct TypeTableEntry {
1083 TypeTableEntryEnumTag enum_tag;1058 TypeTableEntryEnumTag enum_tag;
1084 TypeTableEntryUnion unionation;1059 TypeTableEntryUnion unionation;
1085 TypeTableEntryFn fn;1060 TypeTableEntryFn fn;
1086 TypeTableEntryTypeDecl type_decl;
1087 TypeTableEntryBoundFn bound_fn;1061 TypeTableEntryBoundFn bound_fn;
1088 } data;1062 } data;
10891063
src/analyze.cpp+76-114
...@@ -186,6 +186,8 @@ bool type_is_complete(TypeTableEntry *type_entry) {...@@ -186,6 +186,8 @@ bool type_is_complete(TypeTableEntry *type_entry) {
186 return type_entry->data.enumeration.complete;186 return type_entry->data.enumeration.complete;
187 case TypeTableEntryIdUnion:187 case TypeTableEntryIdUnion:
188 return type_entry->data.unionation.complete;188 return type_entry->data.unionation.complete;
189 case TypeTableEntryIdOpaque:
190 return false;
189 case TypeTableEntryIdMetaType:191 case TypeTableEntryIdMetaType:
190 case TypeTableEntryIdVoid:192 case TypeTableEntryIdVoid:
191 case TypeTableEntryIdBool:193 case TypeTableEntryIdBool:
...@@ -202,7 +204,6 @@ bool type_is_complete(TypeTableEntry *type_entry) {...@@ -202,7 +204,6 @@ bool type_is_complete(TypeTableEntry *type_entry) {
202 case TypeTableEntryIdErrorUnion:204 case TypeTableEntryIdErrorUnion:
203 case TypeTableEntryIdPureError:205 case TypeTableEntryIdPureError:
204 case TypeTableEntryIdFn:206 case TypeTableEntryIdFn:
205 case TypeTableEntryIdTypeDecl:
206 case TypeTableEntryIdNamespace:207 case TypeTableEntryIdNamespace:
207 case TypeTableEntryIdBlock:208 case TypeTableEntryIdBlock:
208 case TypeTableEntryIdBoundFn:209 case TypeTableEntryIdBoundFn:
...@@ -240,12 +241,12 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {...@@ -240,12 +241,12 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
240 case TypeTableEntryIdErrorUnion:241 case TypeTableEntryIdErrorUnion:
241 case TypeTableEntryIdPureError:242 case TypeTableEntryIdPureError:
242 case TypeTableEntryIdFn:243 case TypeTableEntryIdFn:
243 case TypeTableEntryIdTypeDecl:
244 case TypeTableEntryIdNamespace:244 case TypeTableEntryIdNamespace:
245 case TypeTableEntryIdBlock:245 case TypeTableEntryIdBlock:
246 case TypeTableEntryIdBoundFn:246 case TypeTableEntryIdBoundFn:
247 case TypeTableEntryIdEnumTag:247 case TypeTableEntryIdEnumTag:
248 case TypeTableEntryIdArgTuple:248 case TypeTableEntryIdArgTuple:
249 case TypeTableEntryIdOpaque:
249 return true;250 return true;
250 }251 }
251 zig_unreachable();252 zig_unreachable();
...@@ -254,18 +255,17 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {...@@ -254,18 +255,17 @@ bool type_has_zero_bits_known(TypeTableEntry *type_entry) {
254255
255uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry) {256uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry) {
256 assert(type_is_complete(type_entry));257 assert(type_is_complete(type_entry));
257 TypeTableEntry *canon_type = get_underlying_type(type_entry);
258258
259 if (!type_has_bits(type_entry))259 if (!type_has_bits(type_entry))
260 return 0;260 return 0;
261261
262 if (canon_type->id == TypeTableEntryIdStruct && canon_type->data.structure.layout == ContainerLayoutPacked) {262 if (type_entry->id == TypeTableEntryIdStruct && type_entry->data.structure.layout == ContainerLayoutPacked) {
263 uint64_t size_in_bits = type_size_bits(g, type_entry);263 uint64_t size_in_bits = type_size_bits(g, type_entry);
264 return (size_in_bits + 7) / 8;264 return (size_in_bits + 7) / 8;
265 } else if (canon_type->id == TypeTableEntryIdArray) {265 } else if (type_entry->id == TypeTableEntryIdArray) {
266 TypeTableEntry *canon_child_type = get_underlying_type(canon_type->data.array.child_type);266 TypeTableEntry *child_type = type_entry->data.array.child_type;
267 if (canon_child_type->id == TypeTableEntryIdStruct &&267 if (child_type->id == TypeTableEntryIdStruct &&
268 canon_child_type->data.structure.layout == ContainerLayoutPacked)268 child_type->data.structure.layout == ContainerLayoutPacked)
269 {269 {
270 uint64_t size_in_bits = type_size_bits(g, type_entry);270 uint64_t size_in_bits = type_size_bits(g, type_entry);
271 return (size_in_bits + 7) / 8;271 return (size_in_bits + 7) / 8;
...@@ -277,27 +277,26 @@ uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry) {...@@ -277,27 +277,26 @@ uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry) {
277277
278uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry) {278uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry) {
279 assert(type_is_complete(type_entry));279 assert(type_is_complete(type_entry));
280 TypeTableEntry *canon_type = get_underlying_type(type_entry);
281280
282 if (!type_has_bits(type_entry))281 if (!type_has_bits(type_entry))
283 return 0;282 return 0;
284283
285 if (canon_type->id == TypeTableEntryIdStruct && canon_type->data.structure.layout == ContainerLayoutPacked) {284 if (type_entry->id == TypeTableEntryIdStruct && type_entry->data.structure.layout == ContainerLayoutPacked) {
286 uint64_t result = 0;285 uint64_t result = 0;
287 for (size_t i = 0; i < canon_type->data.structure.src_field_count; i += 1) {286 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
288 result += type_size_bits(g, canon_type->data.structure.fields[i].type_entry);287 result += type_size_bits(g, type_entry->data.structure.fields[i].type_entry);
289 }288 }
290 return result;289 return result;
291 } else if (canon_type->id == TypeTableEntryIdArray) {290 } else if (type_entry->id == TypeTableEntryIdArray) {
292 TypeTableEntry *canon_child_type = get_underlying_type(canon_type->data.array.child_type);291 TypeTableEntry *child_type = type_entry->data.array.child_type;
293 if (canon_child_type->id == TypeTableEntryIdStruct &&292 if (child_type->id == TypeTableEntryIdStruct &&
294 canon_child_type->data.structure.layout == ContainerLayoutPacked)293 child_type->data.structure.layout == ContainerLayoutPacked)
295 {294 {
296 return canon_type->data.array.len * type_size_bits(g, canon_child_type);295 return type_entry->data.array.len * type_size_bits(g, child_type);
297 }296 }
298 }297 }
299298
300 return LLVMSizeOfTypeInBits(g->target_data_ref, canon_type->type_ref);299 return LLVMSizeOfTypeInBits(g->target_data_ref, type_entry->type_ref);
301}300}
302301
303static bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry) {302static bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry) {
...@@ -360,10 +359,9 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type...@@ -360,10 +359,9 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
360 bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));359 bit_offset + unaligned_bit_count, const_str, volatile_str, buf_ptr(&child_type->name));
361 }360 }
362361
363 TypeTableEntry *canon_child_type = get_underlying_type(child_type);362 assert(child_type->id != TypeTableEntryIdInvalid);
364 assert(canon_child_type->id != TypeTableEntryIdInvalid);
365363
366 entry->zero_bits = !type_has_bits(canon_child_type);364 entry->zero_bits = !type_has_bits(child_type);
367365
368 if (!entry->zero_bits) {366 if (!entry->zero_bits) {
369 entry->type_ref = LLVMPointerType(child_type->type_ref, 0);367 entry->type_ref = LLVMPointerType(child_type->type_ref, 0);
...@@ -766,17 +764,22 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c...@@ -766,17 +764,22 @@ TypeTableEntry *get_slice_type(CodeGen *g, TypeTableEntry *child_type, bool is_c
766 }764 }
767}765}
768766
769TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *child_type) {767TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name) {
770 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdTypeDecl);768 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdOpaque);
771769
772 buf_init_from_str(&entry->name, name);770 buf_init_from_str(&entry->name, name);
773771
774 entry->is_copyable = type_is_copyable(g, child_type);772 ImportTableEntry *import = scope ? get_scope_import(scope) : nullptr;
775 entry->type_ref = child_type->type_ref;773 unsigned line = source_node ? (unsigned)(source_node->line + 1) : 0;
776 entry->di_type = child_type->di_type;774
777 entry->zero_bits = child_type->zero_bits;775 entry->is_copyable = false;
778 entry->data.type_decl.child_type = child_type;776 entry->type_ref = LLVMInt8Type();
779 entry->data.type_decl.canonical_type = get_underlying_type(child_type);777 entry->di_type = ZigLLVMCreateDebugForwardDeclType(g->dbuilder,
778 ZigLLVMTag_DW_structure_type(), buf_ptr(&entry->name),
779 import ? ZigLLVMFileToScope(import->di_file) : nullptr,
780 import ? import->di_file : nullptr,
781 line);
782 entry->zero_bits = false;
780783
781 return entry;784 return entry;
782}785}
...@@ -968,14 +971,6 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKi...@@ -968,14 +971,6 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKi
968 return entry;971 return entry;
969}972}
970973
971TypeTableEntry *get_underlying_type(TypeTableEntry *type_entry) {
972 if (type_entry->id == TypeTableEntryIdTypeDecl) {
973 return type_entry->data.type_decl.canonical_type;
974 } else {
975 return type_entry;
976 }
977}
978
979static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, TypeTableEntry *type_entry, Buf *type_name) {974static IrInstruction *analyze_const_value(CodeGen *g, Scope *scope, AstNode *node, TypeTableEntry *type_entry, Buf *type_name) {
980 size_t backward_branch_count = 0;975 size_t backward_branch_count = 0;
981 return ir_eval_const_value(g, scope, node, type_entry,976 return ir_eval_const_value(g, scope, node, type_entry,
...@@ -1066,6 +1061,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c...@@ -1066,6 +1061,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
1066 case TypeTableEntryIdUndefLit:1061 case TypeTableEntryIdUndefLit:
1067 case TypeTableEntryIdNullLit:1062 case TypeTableEntryIdNullLit:
1068 case TypeTableEntryIdArgTuple:1063 case TypeTableEntryIdArgTuple:
1064 case TypeTableEntryIdOpaque:
1069 add_node_error(g, param_node->data.param_decl.type,1065 add_node_error(g, param_node->data.param_decl.type,
1070 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));1066 buf_sprintf("parameter of type '%s' not allowed", buf_ptr(&type_entry->name)));
1071 return g->builtin_types.entry_invalid;1067 return g->builtin_types.entry_invalid;
...@@ -1099,7 +1095,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c...@@ -1099,7 +1095,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
1099 case TypeTableEntryIdEnum:1095 case TypeTableEntryIdEnum:
1100 case TypeTableEntryIdUnion:1096 case TypeTableEntryIdUnion:
1101 case TypeTableEntryIdFn:1097 case TypeTableEntryIdFn:
1102 case TypeTableEntryIdTypeDecl:
1103 case TypeTableEntryIdEnumTag:1098 case TypeTableEntryIdEnumTag:
1104 ensure_complete_type(g, type_entry);1099 ensure_complete_type(g, type_entry);
1105 if (!fn_type_id.is_extern && !type_is_copyable(g, type_entry)) {1100 if (!fn_type_id.is_extern && !type_is_copyable(g, type_entry)) {
...@@ -1123,6 +1118,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c...@@ -1123,6 +1118,7 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
1123 case TypeTableEntryIdUndefLit:1118 case TypeTableEntryIdUndefLit:
1124 case TypeTableEntryIdNullLit:1119 case TypeTableEntryIdNullLit:
1125 case TypeTableEntryIdArgTuple:1120 case TypeTableEntryIdArgTuple:
1121 case TypeTableEntryIdOpaque:
1126 add_node_error(g, fn_proto->return_type,1122 add_node_error(g, fn_proto->return_type,
1127 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));1123 buf_sprintf("return type '%s' not allowed", buf_ptr(&fn_type_id.return_type->name)));
1128 return g->builtin_types.entry_invalid;1124 return g->builtin_types.entry_invalid;
...@@ -1155,7 +1151,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c...@@ -1155,7 +1151,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
1155 case TypeTableEntryIdEnum:1151 case TypeTableEntryIdEnum:
1156 case TypeTableEntryIdUnion:1152 case TypeTableEntryIdUnion:
1157 case TypeTableEntryIdFn:1153 case TypeTableEntryIdFn:
1158 case TypeTableEntryIdTypeDecl:
1159 case TypeTableEntryIdEnumTag:1154 case TypeTableEntryIdEnumTag:
1160 break;1155 break;
1161 }1156 }
...@@ -1173,8 +1168,6 @@ bool type_is_invalid(TypeTableEntry *type_entry) {...@@ -1173,8 +1168,6 @@ bool type_is_invalid(TypeTableEntry *type_entry) {
1173 return type_entry->data.enumeration.is_invalid;1168 return type_entry->data.enumeration.is_invalid;
1174 case TypeTableEntryIdUnion:1169 case TypeTableEntryIdUnion:
1175 return type_entry->data.unionation.is_invalid;1170 return type_entry->data.unionation.is_invalid;
1176 case TypeTableEntryIdTypeDecl:
1177 return type_is_invalid(type_entry->data.type_decl.canonical_type);
1178 default:1171 default:
1179 return false;1172 return false;
1180 }1173 }
...@@ -1380,8 +1373,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {...@@ -1380,8 +1373,7 @@ static void resolve_enum_type(CodeGen *g, TypeTableEntry *enum_type) {
1380}1373}
13811374
1382static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {1375static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
1383 TypeTableEntry *canon_type = get_underlying_type(type_entry);1376 switch (type_entry->id) {
1384 switch (canon_type->id) {
1385 case TypeTableEntryIdInvalid:1377 case TypeTableEntryIdInvalid:
1386 case TypeTableEntryIdVar:1378 case TypeTableEntryIdVar:
1387 zig_unreachable();1379 zig_unreachable();
...@@ -1395,11 +1387,11 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {...@@ -1395,11 +1387,11 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
1395 case TypeTableEntryIdPureError:1387 case TypeTableEntryIdPureError:
1396 case TypeTableEntryIdEnum:1388 case TypeTableEntryIdEnum:
1397 case TypeTableEntryIdEnumTag:1389 case TypeTableEntryIdEnumTag:
1398 case TypeTableEntryIdTypeDecl:
1399 case TypeTableEntryIdNamespace:1390 case TypeTableEntryIdNamespace:
1400 case TypeTableEntryIdBlock:1391 case TypeTableEntryIdBlock:
1401 case TypeTableEntryIdBoundFn:1392 case TypeTableEntryIdBoundFn:
1402 case TypeTableEntryIdArgTuple:1393 case TypeTableEntryIdArgTuple:
1394 case TypeTableEntryIdOpaque:
1403 return false;1395 return false;
1404 case TypeTableEntryIdVoid:1396 case TypeTableEntryIdVoid:
1405 case TypeTableEntryIdBool:1397 case TypeTableEntryIdBool:
...@@ -1411,11 +1403,11 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {...@@ -1411,11 +1403,11 @@ static bool type_allowed_in_packed_struct(TypeTableEntry *type_entry) {
1411 case TypeTableEntryIdFn:1403 case TypeTableEntryIdFn:
1412 return true;1404 return true;
1413 case TypeTableEntryIdStruct:1405 case TypeTableEntryIdStruct:
1414 return canon_type->data.structure.layout == ContainerLayoutPacked;1406 return type_entry->data.structure.layout == ContainerLayoutPacked;
1415 case TypeTableEntryIdMaybe:1407 case TypeTableEntryIdMaybe:
1416 {1408 {
1417 TypeTableEntry *canon_child_type = get_underlying_type(canon_type->data.maybe.child_type);1409 TypeTableEntry *child_type = type_entry->data.maybe.child_type;
1418 return canon_child_type->id == TypeTableEntryIdPointer || canon_child_type->id == TypeTableEntryIdFn;1410 return child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn;
1419 }1411 }
1420 }1412 }
1421 zig_unreachable();1413 zig_unreachable();
...@@ -2037,15 +2029,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -2037,15 +2029,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
2037 add_top_level_decl(g, decls_scope, &tld_var->base);2029 add_top_level_decl(g, decls_scope, &tld_var->base);
2038 break;2030 break;
2039 }2031 }
2040 case NodeTypeTypeDecl:
2041 {
2042 Buf *name = node->data.type_decl.symbol;
2043 VisibMod visib_mod = node->data.type_decl.visib_mod;
2044 TldTypeDef *tld_typedef = allocate<TldTypeDef>(1);
2045 init_tld(&tld_typedef->base, TldIdTypeDef, name, visib_mod, node, &decls_scope->base);
2046 add_top_level_decl(g, decls_scope, &tld_typedef->base);
2047 break;
2048 }
2049 case NodeTypeFnProto:2032 case NodeTypeFnProto:
2050 {2033 {
2051 // if the name is missing, we immediately announce an error2034 // if the name is missing, we immediately announce an error
...@@ -2126,7 +2109,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -2126,7 +2109,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
2126 case NodeTypeStructValueField:2109 case NodeTypeStructValueField:
2127 case NodeTypeArrayType:2110 case NodeTypeArrayType:
2128 case NodeTypeErrorType:2111 case NodeTypeErrorType:
2129 case NodeTypeTypeLiteral:
2130 case NodeTypeVarLiteral:2112 case NodeTypeVarLiteral:
2131 case NodeTypeTryExpr:2113 case NodeTypeTryExpr:
2132 case NodeTypeInlineExpr:2114 case NodeTypeInlineExpr:
...@@ -2154,10 +2136,7 @@ static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {...@@ -2154,10 +2136,7 @@ static void resolve_decl_container(CodeGen *g, TldContainer *tld_container) {
2154}2136}
21552137
2156TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry) {2138TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEntry *type_entry) {
2157 TypeTableEntry *underlying_type = get_underlying_type(type_entry);2139 switch (type_entry->id) {
2158 switch (underlying_type->id) {
2159 case TypeTableEntryIdTypeDecl:
2160 zig_unreachable();
2161 case TypeTableEntryIdInvalid:2140 case TypeTableEntryIdInvalid:
2162 return g->builtin_types.entry_invalid;2141 return g->builtin_types.entry_invalid;
2163 case TypeTableEntryIdUnreachable:2142 case TypeTableEntryIdUnreachable:
...@@ -2168,8 +2147,9 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt...@@ -2168,8 +2147,9 @@ TypeTableEntry *validate_var_type(CodeGen *g, AstNode *source_node, TypeTableEnt
2168 case TypeTableEntryIdNullLit:2147 case TypeTableEntryIdNullLit:
2169 case TypeTableEntryIdBlock:2148 case TypeTableEntryIdBlock:
2170 case TypeTableEntryIdArgTuple:2149 case TypeTableEntryIdArgTuple:
2150 case TypeTableEntryIdOpaque:
2171 add_node_error(g, source_node, buf_sprintf("variable of type '%s' not allowed",2151 add_node_error(g, source_node, buf_sprintf("variable of type '%s' not allowed",
2172 buf_ptr(&underlying_type->name)));2152 buf_ptr(&type_entry->name)));
2173 return g->builtin_types.entry_invalid;2153 return g->builtin_types.entry_invalid;
2174 case TypeTableEntryIdNamespace:2154 case TypeTableEntryIdNamespace:
2175 case TypeTableEntryIdMetaType:2155 case TypeTableEntryIdMetaType:
...@@ -2328,17 +2308,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {...@@ -2328,17 +2308,6 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) {
2328 g->global_vars.append(tld_var);2308 g->global_vars.append(tld_var);
2329}2309}
23302310
2331static void resolve_decl_typedef(CodeGen *g, TldTypeDef *tld_typedef) {
2332 AstNode *typedef_node = tld_typedef->base.source_node;
2333 assert(typedef_node->type == NodeTypeTypeDecl);
2334 AstNode *type_node = typedef_node->data.type_decl.child_type;
2335 Buf *decl_name = typedef_node->data.type_decl.symbol;
2336
2337 TypeTableEntry *child_type = analyze_type_expr(g, tld_typedef->base.parent_scope, type_node);
2338 tld_typedef->type_entry = (child_type->id == TypeTableEntryIdInvalid) ?
2339 child_type : get_typedecl_type(g, buf_ptr(decl_name), child_type);
2340}
2341
2342void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {2311void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
2343 if (tld->resolution != TldResolutionUnresolved)2312 if (tld->resolution != TldResolutionUnresolved)
2344 return;2313 return;
...@@ -2370,12 +2339,6 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {...@@ -2370,12 +2339,6 @@ void resolve_top_level_decl(CodeGen *g, Tld *tld, bool pointer_only) {
2370 resolve_decl_container(g, tld_container);2339 resolve_decl_container(g, tld_container);
2371 break;2340 break;
2372 }2341 }
2373 case TldIdTypeDef:
2374 {
2375 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
2376 resolve_decl_typedef(g, tld_typedef);
2377 break;
2378 }
2379 case TldIdCompTime:2342 case TldIdCompTime:
2380 {2343 {
2381 TldCompTime *tld_comptime = (TldCompTime *)tld;2344 TldCompTime *tld_comptime = (TldCompTime *)tld;
...@@ -2589,6 +2552,7 @@ static bool is_container(TypeTableEntry *type_entry) {...@@ -2589,6 +2552,7 @@ static bool is_container(TypeTableEntry *type_entry) {
2589 switch (type_entry->id) {2552 switch (type_entry->id) {
2590 case TypeTableEntryIdInvalid:2553 case TypeTableEntryIdInvalid:
2591 case TypeTableEntryIdVar:2554 case TypeTableEntryIdVar:
2555 case TypeTableEntryIdOpaque:
2592 zig_unreachable();2556 zig_unreachable();
2593 case TypeTableEntryIdStruct:2557 case TypeTableEntryIdStruct:
2594 case TypeTableEntryIdEnum:2558 case TypeTableEntryIdEnum:
...@@ -2610,7 +2574,6 @@ static bool is_container(TypeTableEntry *type_entry) {...@@ -2610,7 +2574,6 @@ static bool is_container(TypeTableEntry *type_entry) {
2610 case TypeTableEntryIdErrorUnion:2574 case TypeTableEntryIdErrorUnion:
2611 case TypeTableEntryIdPureError:2575 case TypeTableEntryIdPureError:
2612 case TypeTableEntryIdFn:2576 case TypeTableEntryIdFn:
2613 case TypeTableEntryIdTypeDecl:
2614 case TypeTableEntryIdNamespace:2577 case TypeTableEntryIdNamespace:
2615 case TypeTableEntryIdBlock:2578 case TypeTableEntryIdBlock:
2616 case TypeTableEntryIdBoundFn:2579 case TypeTableEntryIdBoundFn:
...@@ -2659,7 +2622,6 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {...@@ -2659,7 +2622,6 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
2659 case TypeTableEntryIdErrorUnion:2622 case TypeTableEntryIdErrorUnion:
2660 case TypeTableEntryIdPureError:2623 case TypeTableEntryIdPureError:
2661 case TypeTableEntryIdFn:2624 case TypeTableEntryIdFn:
2662 case TypeTableEntryIdTypeDecl:
2663 case TypeTableEntryIdNamespace:2625 case TypeTableEntryIdNamespace:
2664 case TypeTableEntryIdBlock:2626 case TypeTableEntryIdBlock:
2665 case TypeTableEntryIdBoundFn:2627 case TypeTableEntryIdBoundFn:
...@@ -2667,6 +2629,7 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {...@@ -2667,6 +2629,7 @@ void resolve_container_type(CodeGen *g, TypeTableEntry *type_entry) {
2667 case TypeTableEntryIdVar:2629 case TypeTableEntryIdVar:
2668 case TypeTableEntryIdEnumTag:2630 case TypeTableEntryIdEnumTag:
2669 case TypeTableEntryIdArgTuple:2631 case TypeTableEntryIdArgTuple:
2632 case TypeTableEntryIdOpaque:
2670 zig_unreachable();2633 zig_unreachable();
2671 }2634 }
2672}2635}
...@@ -3062,6 +3025,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {...@@ -3062,6 +3025,7 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
3062 case TypeTableEntryIdBoundFn:3025 case TypeTableEntryIdBoundFn:
3063 case TypeTableEntryIdVar:3026 case TypeTableEntryIdVar:
3064 case TypeTableEntryIdArgTuple:3027 case TypeTableEntryIdArgTuple:
3028 case TypeTableEntryIdOpaque:
3065 zig_unreachable();3029 zig_unreachable();
3066 case TypeTableEntryIdUnreachable:3030 case TypeTableEntryIdUnreachable:
3067 case TypeTableEntryIdVoid:3031 case TypeTableEntryIdVoid:
...@@ -3086,8 +3050,6 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {...@@ -3086,8 +3050,6 @@ bool handle_is_ptr(TypeTableEntry *type_entry) {
3086 return type_has_bits(type_entry->data.maybe.child_type) &&3050 return type_has_bits(type_entry->data.maybe.child_type) &&
3087 type_entry->data.maybe.child_type->id != TypeTableEntryIdPointer &&3051 type_entry->data.maybe.child_type->id != TypeTableEntryIdPointer &&
3088 type_entry->data.maybe.child_type->id != TypeTableEntryIdFn;3052 type_entry->data.maybe.child_type->id != TypeTableEntryIdFn;
3089 case TypeTableEntryIdTypeDecl:
3090 return handle_is_ptr(type_entry->data.type_decl.canonical_type);
3091 }3053 }
3092 zig_unreachable();3054 zig_unreachable();
3093}3055}
...@@ -3165,6 +3127,8 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {...@@ -3165,6 +3127,8 @@ bool fn_type_id_eql(FnTypeId *a, FnTypeId *b) {
3165static uint32_t hash_const_val(ConstExprValue *const_val) {3127static uint32_t hash_const_val(ConstExprValue *const_val) {
3166 assert(const_val->special == ConstValSpecialStatic);3128 assert(const_val->special == ConstValSpecialStatic);
3167 switch (const_val->type->id) {3129 switch (const_val->type->id) {
3130 case TypeTableEntryIdOpaque:
3131 zig_unreachable();
3168 case TypeTableEntryIdBool:3132 case TypeTableEntryIdBool:
3169 return const_val->data.x_bool ? (uint32_t)127863866 : (uint32_t)215080464;3133 return const_val->data.x_bool ? (uint32_t)127863866 : (uint32_t)215080464;
3170 case TypeTableEntryIdMetaType:3134 case TypeTableEntryIdMetaType:
...@@ -3254,8 +3218,6 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {...@@ -3254,8 +3218,6 @@ static uint32_t hash_const_val(ConstExprValue *const_val) {
3254 case TypeTableEntryIdFn:3218 case TypeTableEntryIdFn:
3255 return hash_ptr(const_val->data.x_fn.fn_entry) +3219 return hash_ptr(const_val->data.x_fn.fn_entry) +
3256 (const_val->data.x_fn.is_inline ? 4133894920 : 3983484790);3220 (const_val->data.x_fn.is_inline ? 4133894920 : 3983484790);
3257 case TypeTableEntryIdTypeDecl:
3258 return hash_ptr(const_val->data.x_type);
3259 case TypeTableEntryIdNamespace:3221 case TypeTableEntryIdNamespace:
3260 return hash_ptr(const_val->data.x_import);3222 return hash_ptr(const_val->data.x_import);
3261 case TypeTableEntryIdBlock:3223 case TypeTableEntryIdBlock:
...@@ -3359,10 +3321,10 @@ bool type_has_bits(TypeTableEntry *type_entry) {...@@ -3359,10 +3321,10 @@ bool type_has_bits(TypeTableEntry *type_entry) {
3359}3321}
33603322
3361bool type_requires_comptime(TypeTableEntry *type_entry) {3323bool type_requires_comptime(TypeTableEntry *type_entry) {
3362 switch (get_underlying_type(type_entry)->id) {3324 switch (type_entry->id) {
3363 case TypeTableEntryIdInvalid:3325 case TypeTableEntryIdInvalid:
3364 case TypeTableEntryIdVar:3326 case TypeTableEntryIdVar:
3365 case TypeTableEntryIdTypeDecl:3327 case TypeTableEntryIdOpaque:
3366 zig_unreachable();3328 zig_unreachable();
3367 case TypeTableEntryIdNumLitFloat:3329 case TypeTableEntryIdNumLitFloat:
3368 case TypeTableEntryIdNumLitInt:3330 case TypeTableEntryIdNumLitInt:
...@@ -3603,14 +3565,14 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_...@@ -3603,14 +3565,14 @@ ConstExprValue *create_const_arg_tuple(CodeGen *g, size_t arg_index_start, size_
36033565
36043566
3605void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {3567void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
3606 TypeTableEntry *canon_wanted_type = get_underlying_type(const_val->type);3568 TypeTableEntry *wanted_type = const_val->type;
3607 if (canon_wanted_type->id == TypeTableEntryIdArray) {3569 if (wanted_type->id == TypeTableEntryIdArray) {
3608 const_val->special = ConstValSpecialStatic;3570 const_val->special = ConstValSpecialStatic;
3609 size_t elem_count = canon_wanted_type->data.array.len;3571 size_t elem_count = wanted_type->data.array.len;
3610 const_val->data.x_array.elements = allocate<ConstExprValue>(elem_count);3572 const_val->data.x_array.elements = allocate<ConstExprValue>(elem_count);
3611 for (size_t i = 0; i < elem_count; i += 1) {3573 for (size_t i = 0; i < elem_count; i += 1) {
3612 ConstExprValue *element_val = &const_val->data.x_array.elements[i];3574 ConstExprValue *element_val = &const_val->data.x_array.elements[i];
3613 element_val->type = canon_wanted_type->data.array.child_type;3575 element_val->type = wanted_type->data.array.child_type;
3614 init_const_undefined(g, element_val);3576 init_const_undefined(g, element_val);
3615 ConstParent *parent = get_const_val_parent(element_val);3577 ConstParent *parent = get_const_val_parent(element_val);
3616 if (parent != nullptr) {3578 if (parent != nullptr) {
...@@ -3619,15 +3581,15 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {...@@ -3619,15 +3581,15 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
3619 parent->data.p_array.elem_index = i;3581 parent->data.p_array.elem_index = i;
3620 }3582 }
3621 }3583 }
3622 } else if (canon_wanted_type->id == TypeTableEntryIdStruct) {3584 } else if (wanted_type->id == TypeTableEntryIdStruct) {
3623 ensure_complete_type(g, canon_wanted_type);3585 ensure_complete_type(g, wanted_type);
36243586
3625 const_val->special = ConstValSpecialStatic;3587 const_val->special = ConstValSpecialStatic;
3626 size_t field_count = canon_wanted_type->data.structure.src_field_count;3588 size_t field_count = wanted_type->data.structure.src_field_count;
3627 const_val->data.x_struct.fields = allocate<ConstExprValue>(field_count);3589 const_val->data.x_struct.fields = allocate<ConstExprValue>(field_count);
3628 for (size_t i = 0; i < field_count; i += 1) {3590 for (size_t i = 0; i < field_count; i += 1) {
3629 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];3591 ConstExprValue *field_val = &const_val->data.x_struct.fields[i];
3630 field_val->type = canon_wanted_type->data.structure.fields[i].type_entry;3592 field_val->type = wanted_type->data.structure.fields[i].type_entry;
3631 assert(field_val->type);3593 assert(field_val->type);
3632 init_const_undefined(g, field_val);3594 init_const_undefined(g, field_val);
3633 ConstParent *parent = get_const_val_parent(field_val);3595 ConstParent *parent = get_const_val_parent(field_val);
...@@ -3678,6 +3640,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {...@@ -3678,6 +3640,8 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
3678 assert(a->special == ConstValSpecialStatic);3640 assert(a->special == ConstValSpecialStatic);
3679 assert(b->special == ConstValSpecialStatic);3641 assert(b->special == ConstValSpecialStatic);
3680 switch (a->type->id) {3642 switch (a->type->id) {
3643 case TypeTableEntryIdOpaque:
3644 zig_unreachable();
3681 case TypeTableEntryIdEnum:3645 case TypeTableEntryIdEnum:
3682 {3646 {
3683 ConstEnumValue *enum1 = &a->data.x_enum;3647 ConstEnumValue *enum1 = &a->data.x_enum;
...@@ -3767,8 +3731,6 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {...@@ -3767,8 +3731,6 @@ bool const_values_equal(ConstExprValue *a, ConstExprValue *b) {
3767 }3731 }
3768 case TypeTableEntryIdErrorUnion:3732 case TypeTableEntryIdErrorUnion:
3769 zig_panic("TODO");3733 zig_panic("TODO");
3770 case TypeTableEntryIdTypeDecl:
3771 zig_panic("TODO");
3772 case TypeTableEntryIdNamespace:3734 case TypeTableEntryIdNamespace:
3773 return a->data.x_import == b->data.x_import;3735 return a->data.x_import == b->data.x_import;
3774 case TypeTableEntryIdBlock:3736 case TypeTableEntryIdBlock:
...@@ -3857,9 +3819,9 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -3857,9 +3819,9 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
3857 }3819 }
3858 assert(const_val->type);3820 assert(const_val->type);
38593821
3860 TypeTableEntry *canon_type = get_underlying_type(const_val->type);3822 TypeTableEntry *type_entry = const_val->type;
3861 switch (canon_type->id) {3823 switch (type_entry->id) {
3862 case TypeTableEntryIdTypeDecl:3824 case TypeTableEntryIdOpaque:
3863 zig_unreachable();3825 zig_unreachable();
3864 case TypeTableEntryIdInvalid:3826 case TypeTableEntryIdInvalid:
3865 buf_appendf(buf, "(invalid)");3827 buf_appendf(buf, "(invalid)");
...@@ -3926,7 +3888,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -3926,7 +3888,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
3926 return;3888 return;
3927 }3889 }
3928 case ConstPtrSpecialHardCodedAddr:3890 case ConstPtrSpecialHardCodedAddr:
3929 buf_appendf(buf, "(&%s)(%" PRIx64 ")", buf_ptr(&canon_type->data.pointer.child_type->name),3891 buf_appendf(buf, "(&%s)(%" PRIx64 ")", buf_ptr(&type_entry->data.pointer.child_type->name),
3930 const_val->data.x_ptr.data.hard_coded_addr.addr);3892 const_val->data.x_ptr.data.hard_coded_addr.addr);
3931 return;3893 return;
3932 case ConstPtrSpecialDiscard:3894 case ConstPtrSpecialDiscard:
...@@ -3949,8 +3911,8 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -3949,8 +3911,8 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
3949 }3911 }
3950 case TypeTableEntryIdArray:3912 case TypeTableEntryIdArray:
3951 {3913 {
3952 TypeTableEntry *child_type = canon_type->data.array.child_type;3914 TypeTableEntry *child_type = type_entry->data.array.child_type;
3953 uint64_t len = canon_type->data.array.len;3915 uint64_t len = type_entry->data.array.len;
39543916
3955 // if it's []u8, assume UTF-8 and output a string3917 // if it's []u8, assume UTF-8 and output a string
3956 if (child_type->id == TypeTableEntryIdInt &&3918 if (child_type->id == TypeTableEntryIdInt &&
...@@ -3973,7 +3935,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -3973,7 +3935,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
3973 return;3935 return;
3974 }3936 }
39753937
3976 buf_appendf(buf, "%s{", buf_ptr(&canon_type->name));3938 buf_appendf(buf, "%s{", buf_ptr(&type_entry->name));
3977 for (uint64_t i = 0; i < len; i += 1) {3939 for (uint64_t i = 0; i < len; i += 1) {
3978 if (i != 0)3940 if (i != 0)
3979 buf_appendf(buf, ",");3941 buf_appendf(buf, ",");
...@@ -4021,22 +3983,22 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -4021,22 +3983,22 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
4021 }3983 }
4022 case TypeTableEntryIdStruct:3984 case TypeTableEntryIdStruct:
4023 {3985 {
4024 buf_appendf(buf, "(struct %s constant)", buf_ptr(&canon_type->name));3986 buf_appendf(buf, "(struct %s constant)", buf_ptr(&type_entry->name));
4025 return;3987 return;
4026 }3988 }
4027 case TypeTableEntryIdEnum:3989 case TypeTableEntryIdEnum:
4028 {3990 {
4029 buf_appendf(buf, "(enum %s constant)", buf_ptr(&canon_type->name));3991 buf_appendf(buf, "(enum %s constant)", buf_ptr(&type_entry->name));
4030 return;3992 return;
4031 }3993 }
4032 case TypeTableEntryIdErrorUnion:3994 case TypeTableEntryIdErrorUnion:
4033 {3995 {
4034 buf_appendf(buf, "(error union %s constant)", buf_ptr(&canon_type->name));3996 buf_appendf(buf, "(error union %s constant)", buf_ptr(&type_entry->name));
4035 return;3997 return;
4036 }3998 }
4037 case TypeTableEntryIdUnion:3999 case TypeTableEntryIdUnion:
4038 {4000 {
4039 buf_appendf(buf, "(union %s constant)", buf_ptr(&canon_type->name));4001 buf_appendf(buf, "(union %s constant)", buf_ptr(&type_entry->name));
4040 return;4002 return;
4041 }4003 }
4042 case TypeTableEntryIdPureError:4004 case TypeTableEntryIdPureError:
...@@ -4046,7 +4008,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {...@@ -4046,7 +4008,7 @@ void render_const_value(Buf *buf, ConstExprValue *const_val) {
4046 }4008 }
4047 case TypeTableEntryIdEnumTag:4009 case TypeTableEntryIdEnumTag:
4048 {4010 {
4049 TypeTableEntry *enum_type = canon_type->data.enum_tag.enum_type;4011 TypeTableEntry *enum_type = type_entry->data.enum_tag.enum_type;
4050 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];4012 TypeEnumField *field = &enum_type->data.enumeration.fields[const_val->data.x_bignum.data.x_uint];
4051 buf_appendf(buf, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));4013 buf_appendf(buf, "%s.%s", buf_ptr(&enum_type->name), buf_ptr(field->name));
4052 return;4014 return;
...@@ -4095,6 +4057,7 @@ uint32_t type_id_hash(TypeId x) {...@@ -4095,6 +4057,7 @@ uint32_t type_id_hash(TypeId x) {
4095 switch (x.id) {4057 switch (x.id) {
4096 case TypeTableEntryIdInvalid:4058 case TypeTableEntryIdInvalid:
4097 case TypeTableEntryIdVar:4059 case TypeTableEntryIdVar:
4060 case TypeTableEntryIdOpaque:
4098 case TypeTableEntryIdMetaType:4061 case TypeTableEntryIdMetaType:
4099 case TypeTableEntryIdVoid:4062 case TypeTableEntryIdVoid:
4100 case TypeTableEntryIdBool:4063 case TypeTableEntryIdBool:
...@@ -4112,7 +4075,6 @@ uint32_t type_id_hash(TypeId x) {...@@ -4112,7 +4075,6 @@ uint32_t type_id_hash(TypeId x) {
4112 case TypeTableEntryIdEnumTag:4075 case TypeTableEntryIdEnumTag:
4113 case TypeTableEntryIdUnion:4076 case TypeTableEntryIdUnion:
4114 case TypeTableEntryIdFn:4077 case TypeTableEntryIdFn:
4115 case TypeTableEntryIdTypeDecl:
4116 case TypeTableEntryIdNamespace:4078 case TypeTableEntryIdNamespace:
4117 case TypeTableEntryIdBlock:4079 case TypeTableEntryIdBlock:
4118 case TypeTableEntryIdBoundFn:4080 case TypeTableEntryIdBoundFn:
...@@ -4157,11 +4119,11 @@ bool type_id_eql(TypeId a, TypeId b) {...@@ -4157,11 +4119,11 @@ bool type_id_eql(TypeId a, TypeId b) {
4157 case TypeTableEntryIdEnumTag:4119 case TypeTableEntryIdEnumTag:
4158 case TypeTableEntryIdUnion:4120 case TypeTableEntryIdUnion:
4159 case TypeTableEntryIdFn:4121 case TypeTableEntryIdFn:
4160 case TypeTableEntryIdTypeDecl:
4161 case TypeTableEntryIdNamespace:4122 case TypeTableEntryIdNamespace:
4162 case TypeTableEntryIdBlock:4123 case TypeTableEntryIdBlock:
4163 case TypeTableEntryIdBoundFn:4124 case TypeTableEntryIdBoundFn:
4164 case TypeTableEntryIdArgTuple:4125 case TypeTableEntryIdArgTuple:
4126 case TypeTableEntryIdOpaque:
4165 zig_unreachable();4127 zig_unreachable();
4166 case TypeTableEntryIdPointer:4128 case TypeTableEntryIdPointer:
4167 return a.data.pointer.child_type == b.data.pointer.child_type &&4129 return a.data.pointer.child_type == b.data.pointer.child_type &&
...@@ -4211,10 +4173,10 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {...@@ -4211,10 +4173,10 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) {
42114173
4212ConstParent *get_const_val_parent(ConstExprValue *value) {4174ConstParent *get_const_val_parent(ConstExprValue *value) {
4213 assert(value->type);4175 assert(value->type);
4214 TypeTableEntry *canon_type = get_underlying_type(value->type);4176 TypeTableEntry *type_entry = value->type;
4215 if (canon_type->id == TypeTableEntryIdArray) {4177 if (type_entry->id == TypeTableEntryIdArray) {
4216 return &value->data.x_array.parent;4178 return &value->data.x_array.parent;
4217 } else if (canon_type->id == TypeTableEntryIdStruct) {4179 } else if (type_entry->id == TypeTableEntryIdStruct) {
4218 return &value->data.x_struct.parent;4180 return &value->data.x_struct.parent;
4219 }4181 }
4220 return nullptr;4182 return nullptr;
src/analyze.hpp+1-2
...@@ -24,7 +24,6 @@ TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_b...@@ -24,7 +24,6 @@ TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_b
24TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);24TypeTableEntry *get_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
25TypeTableEntry **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);25TypeTableEntry **get_c_int_type_ptr(CodeGen *g, CIntType c_int_type);
26TypeTableEntry *get_c_int_type(CodeGen *g, CIntType c_int_type);26TypeTableEntry *get_c_int_type(CodeGen *g, CIntType c_int_type);
27TypeTableEntry *get_typedecl_type(CodeGen *g, const char *name, TypeTableEntry *child_type);
28TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);27TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id);
29TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);28TypeTableEntry *get_maybe_type(CodeGen *g, TypeTableEntry *child_type);
30TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);29TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t array_size);
...@@ -34,11 +33,11 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKi...@@ -34,11 +33,11 @@ TypeTableEntry *get_partial_container_type(CodeGen *g, Scope *scope, ContainerKi
34TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);33TypeTableEntry *get_smallest_unsigned_int_type(CodeGen *g, uint64_t x);
35TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type);34TypeTableEntry *get_error_type(CodeGen *g, TypeTableEntry *child_type);
36TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry);35TypeTableEntry *get_bound_fn_type(CodeGen *g, FnTableEntry *fn_entry);
36TypeTableEntry *get_opaque_type(CodeGen *g, Scope *scope, AstNode *source_node, const char *name);
37bool handle_is_ptr(TypeTableEntry *type_entry);37bool handle_is_ptr(TypeTableEntry *type_entry);
38void find_libc_include_path(CodeGen *g);38void find_libc_include_path(CodeGen *g);
39void find_libc_lib_path(CodeGen *g);39void find_libc_lib_path(CodeGen *g);
4040
41TypeTableEntry *get_underlying_type(TypeTableEntry *type_entry);
42bool type_has_bits(TypeTableEntry *type_entry);41bool type_has_bits(TypeTableEntry *type_entry);
4342
4443
src/ast_render.cpp+6-28
...@@ -166,8 +166,6 @@ static const char *node_type_str(NodeType node_type) {...@@ -166,8 +166,6 @@ static const char *node_type_str(NodeType node_type) {
166 return "Defer";166 return "Defer";
167 case NodeTypeVariableDeclaration:167 case NodeTypeVariableDeclaration:
168 return "VariableDeclaration";168 return "VariableDeclaration";
169 case NodeTypeTypeDecl:
170 return "TypeDecl";
171 case NodeTypeErrorValueDecl:169 case NodeTypeErrorValueDecl:
172 return "ErrorValueDecl";170 return "ErrorValueDecl";
173 case NodeTypeTestDecl:171 case NodeTypeTestDecl:
...@@ -234,8 +232,6 @@ static const char *node_type_str(NodeType node_type) {...@@ -234,8 +232,6 @@ static const char *node_type_str(NodeType node_type) {
234 return "ArrayType";232 return "ArrayType";
235 case NodeTypeErrorType:233 case NodeTypeErrorType:
236 return "ErrorType";234 return "ErrorType";
237 case NodeTypeTypeLiteral:
238 return "TypeLiteral";
239 case NodeTypeVarLiteral:235 case NodeTypeVarLiteral:
240 return "VarLiteral";236 return "VarLiteral";
241 case NodeTypeTryExpr:237 case NodeTypeTryExpr:
...@@ -394,7 +390,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -394,7 +390,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
394390
395 if (child->type == NodeTypeUse ||391 if (child->type == NodeTypeUse ||
396 child->type == NodeTypeVariableDeclaration ||392 child->type == NodeTypeVariableDeclaration ||
397 child->type == NodeTypeTypeDecl ||
398 child->type == NodeTypeErrorValueDecl ||393 child->type == NodeTypeErrorValueDecl ||
399 child->type == NodeTypeFnProto)394 child->type == NodeTypeFnProto)
400 {395 {
...@@ -507,14 +502,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -507,14 +502,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
507 }502 }
508 break;503 break;
509 }504 }
510 case NodeTypeTypeDecl:
511 {
512 const char *pub_str = visib_mod_string(node->data.type_decl.visib_mod);
513 const char *var_name = buf_ptr(node->data.type_decl.symbol);
514 fprintf(ar->f, "%stype %s = ", pub_str, var_name);
515 render_node_grouped(ar, node->data.type_decl.child_type);
516 break;
517 }
518 case NodeTypeBinOpExpr:505 case NodeTypeBinOpExpr:
519 if (!grouped) fprintf(ar->f, "(");506 if (!grouped) fprintf(ar->f, "(");
520 render_node_ungrouped(ar, node->data.bin_op_expr.op1);507 render_node_ungrouped(ar, node->data.bin_op_expr.op1);
...@@ -668,9 +655,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -668,9 +655,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
668 case NodeTypeErrorType:655 case NodeTypeErrorType:
669 fprintf(ar->f, "error");656 fprintf(ar->f, "error");
670 break;657 break;
671 case NodeTypeTypeLiteral:
672 fprintf(ar->f, "type");
673 break;
674 case NodeTypeVarLiteral:658 case NodeTypeVarLiteral:
675 fprintf(ar->f, "var");659 fprintf(ar->f, "var");
676 break;660 break;
...@@ -1034,6 +1018,12 @@ static void ast_render_tld_var(AstRender *ar, Buf *name, TldVar *tld_var) {...@@ -1034,6 +1018,12 @@ static void ast_render_tld_var(AstRender *ar, Buf *name, TldVar *tld_var) {
1034 fprintf(ar->f, "union {");1018 fprintf(ar->f, "union {");
1035 fprintf(ar->f, "TODO");1019 fprintf(ar->f, "TODO");
1036 fprintf(ar->f, "}");1020 fprintf(ar->f, "}");
1021 } else if (type_entry->id == TypeTableEntryIdOpaque) {
1022 if (buf_eql_buf(&type_entry->name, name)) {
1023 fprintf(ar->f, "@OpaqueType()");
1024 } else {
1025 fprintf(ar->f, "%s", buf_ptr(&type_entry->name));
1026 }
1037 } else {1027 } else {
1038 fprintf(ar->f, "%s", buf_ptr(&type_entry->name));1028 fprintf(ar->f, "%s", buf_ptr(&type_entry->name));
1039 }1029 }
...@@ -1047,15 +1037,6 @@ static void ast_render_tld_var(AstRender *ar, Buf *name, TldVar *tld_var) {...@@ -1047,15 +1037,6 @@ static void ast_render_tld_var(AstRender *ar, Buf *name, TldVar *tld_var) {
1047 fprintf(ar->f, ";\n");1037 fprintf(ar->f, ";\n");
1048}1038}
10491039
1050static void ast_render_tld_typedef(AstRender *ar, Buf *name, TldTypeDef *tld_typedef) {
1051 TypeTableEntry *type_entry = tld_typedef->type_entry;
1052 TypeTableEntry *canon_type = get_underlying_type(type_entry);
1053
1054 fprintf(ar->f, "pub type ");
1055 print_symbol(ar, name);
1056 fprintf(ar->f, " = %s;\n", buf_ptr(&canon_type->name));
1057}
1058
1059void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) {1040void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) {
1060 AstRender ar = {0};1041 AstRender ar = {0};
1061 ar.f = f;1042 ar.f = f;
...@@ -1087,9 +1068,6 @@ void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) {...@@ -1087,9 +1068,6 @@ void ast_render_decls(FILE *f, int indent_size, ImportTableEntry *import) {
1087 case TldIdContainer:1068 case TldIdContainer:
1088 fprintf(stdout, "container\n");1069 fprintf(stdout, "container\n");
1089 break;1070 break;
1090 case TldIdTypeDef:
1091 ast_render_tld_typedef(&ar, entry->key, (TldTypeDef *)tld);
1092 break;
1093 case TldIdCompTime:1071 case TldIdCompTime:
1094 fprintf(stdout, "comptime\n");1072 fprintf(stdout, "comptime\n");
1095 break;1073 break;
src/codegen.cpp+85-89
...@@ -649,12 +649,9 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,...@@ -649,12 +649,9 @@ static void add_bounds_check(CodeGen *g, LLVMValueRef target_val,
649 LLVMPositionBuilderAtEnd(g->builder, ok_block);649 LLVMPositionBuilderAtEnd(g->builder, ok_block);
650}650}
651651
652static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_debug_safety, TypeTableEntry *actual_type_non_canon,652static LLVMValueRef gen_widen_or_shorten(CodeGen *g, bool want_debug_safety, TypeTableEntry *actual_type,
653 TypeTableEntry *wanted_type_non_canon, LLVMValueRef expr_val)653 TypeTableEntry *wanted_type, LLVMValueRef expr_val)
654{654{
655 TypeTableEntry *actual_type = get_underlying_type(actual_type_non_canon);
656 TypeTableEntry *wanted_type = get_underlying_type(wanted_type_non_canon);
657
658 assert(actual_type->id == wanted_type->id);655 assert(actual_type->id == wanted_type->id);
659656
660 uint64_t actual_bits;657 uint64_t actual_bits;
...@@ -852,7 +849,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef...@@ -852,7 +849,7 @@ static LLVMValueRef gen_struct_memcpy(CodeGen *g, LLVMValueRef src, LLVMValueRef
852static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, TypeTableEntry *ptr_type,849static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, TypeTableEntry *ptr_type,
853 LLVMValueRef value)850 LLVMValueRef value)
854{851{
855 TypeTableEntry *child_type = get_underlying_type(ptr_type->data.pointer.child_type);852 TypeTableEntry *child_type = ptr_type->data.pointer.child_type;
856853
857 if (!type_has_bits(child_type))854 if (!type_has_bits(child_type))
858 return nullptr;855 return nullptr;
...@@ -1111,7 +1108,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1111,7 +1108,7 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1111 IrInstruction *op2 = bin_op_instruction->op2;1108 IrInstruction *op2 = bin_op_instruction->op2;
11121109
1113 assert(op1->value.type == op2->value.type);1110 assert(op1->value.type == op2->value.type);
1114 TypeTableEntry *canon_type = get_underlying_type(op1->value.type);1111 TypeTableEntry *type_entry = op1->value.type;
11151112
1116 bool want_debug_safety = bin_op_instruction->safety_check_on &&1113 bool want_debug_safety = bin_op_instruction->safety_check_on &&
1117 ir_want_debug_safety(g, &bin_op_instruction->base);1114 ir_want_debug_safety(g, &bin_op_instruction->base);
...@@ -1133,22 +1130,22 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1133,22 +1130,22 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1133 case IrBinOpCmpGreaterThan:1130 case IrBinOpCmpGreaterThan:
1134 case IrBinOpCmpLessOrEq:1131 case IrBinOpCmpLessOrEq:
1135 case IrBinOpCmpGreaterOrEq:1132 case IrBinOpCmpGreaterOrEq:
1136 if (canon_type->id == TypeTableEntryIdFloat) {1133 if (type_entry->id == TypeTableEntryIdFloat) {
1137 LLVMRealPredicate pred = cmp_op_to_real_predicate(op_id);1134 LLVMRealPredicate pred = cmp_op_to_real_predicate(op_id);
1138 return LLVMBuildFCmp(g->builder, pred, op1_value, op2_value, "");1135 return LLVMBuildFCmp(g->builder, pred, op1_value, op2_value, "");
1139 } else if (canon_type->id == TypeTableEntryIdInt) {1136 } else if (type_entry->id == TypeTableEntryIdInt) {
1140 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, canon_type->data.integral.is_signed);1137 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, type_entry->data.integral.is_signed);
1141 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");1138 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
1142 } else if (canon_type->id == TypeTableEntryIdEnum) {1139 } else if (type_entry->id == TypeTableEntryIdEnum) {
1143 if (canon_type->data.enumeration.gen_field_count == 0) {1140 if (type_entry->data.enumeration.gen_field_count == 0) {
1144 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);1141 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);
1145 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");1142 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
1146 } else {1143 } else {
1147 zig_unreachable();1144 zig_unreachable();
1148 }1145 }
1149 } else if (canon_type->id == TypeTableEntryIdPureError ||1146 } else if (type_entry->id == TypeTableEntryIdPureError ||
1150 canon_type->id == TypeTableEntryIdPointer ||1147 type_entry->id == TypeTableEntryIdPointer ||
1151 canon_type->id == TypeTableEntryIdBool)1148 type_entry->id == TypeTableEntryIdBool)
1152 {1149 {
1153 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);1150 LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false);
1154 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");1151 return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, "");
...@@ -1157,15 +1154,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1157,15 +1154,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1157 }1154 }
1158 case IrBinOpAdd:1155 case IrBinOpAdd:
1159 case IrBinOpAddWrap:1156 case IrBinOpAddWrap:
1160 if (canon_type->id == TypeTableEntryIdFloat) {1157 if (type_entry->id == TypeTableEntryIdFloat) {
1161 return LLVMBuildFAdd(g->builder, op1_value, op2_value, "");1158 return LLVMBuildFAdd(g->builder, op1_value, op2_value, "");
1162 } else if (canon_type->id == TypeTableEntryIdInt) {1159 } else if (type_entry->id == TypeTableEntryIdInt) {
1163 bool is_wrapping = (op_id == IrBinOpAddWrap);1160 bool is_wrapping = (op_id == IrBinOpAddWrap);
1164 if (is_wrapping) {1161 if (is_wrapping) {
1165 return LLVMBuildAdd(g->builder, op1_value, op2_value, "");1162 return LLVMBuildAdd(g->builder, op1_value, op2_value, "");
1166 } else if (want_debug_safety) {1163 } else if (want_debug_safety) {
1167 return gen_overflow_op(g, canon_type, AddSubMulAdd, op1_value, op2_value);1164 return gen_overflow_op(g, type_entry, AddSubMulAdd, op1_value, op2_value);
1168 } else if (canon_type->data.integral.is_signed) {1165 } else if (type_entry->data.integral.is_signed) {
1169 return LLVMBuildNSWAdd(g->builder, op1_value, op2_value, "");1166 return LLVMBuildNSWAdd(g->builder, op1_value, op2_value, "");
1170 } else {1167 } else {
1171 return LLVMBuildNUWAdd(g->builder, op1_value, op2_value, "");1168 return LLVMBuildNUWAdd(g->builder, op1_value, op2_value, "");
...@@ -1182,36 +1179,36 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1182,36 +1179,36 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1182 case IrBinOpBitShiftLeft:1179 case IrBinOpBitShiftLeft:
1183 case IrBinOpBitShiftLeftWrap:1180 case IrBinOpBitShiftLeftWrap:
1184 {1181 {
1185 assert(canon_type->id == TypeTableEntryIdInt);1182 assert(type_entry->id == TypeTableEntryIdInt);
1186 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);1183 bool is_wrapping = (op_id == IrBinOpBitShiftLeftWrap);
1187 if (is_wrapping) {1184 if (is_wrapping) {
1188 return LLVMBuildShl(g->builder, op1_value, op2_value, "");1185 return LLVMBuildShl(g->builder, op1_value, op2_value, "");
1189 } else if (want_debug_safety) {1186 } else if (want_debug_safety) {
1190 return gen_overflow_shl_op(g, canon_type, op1_value, op2_value);1187 return gen_overflow_shl_op(g, type_entry, op1_value, op2_value);
1191 } else if (canon_type->data.integral.is_signed) {1188 } else if (type_entry->data.integral.is_signed) {
1192 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");1189 return ZigLLVMBuildNSWShl(g->builder, op1_value, op2_value, "");
1193 } else {1190 } else {
1194 return ZigLLVMBuildNUWShl(g->builder, op1_value, op2_value, "");1191 return ZigLLVMBuildNUWShl(g->builder, op1_value, op2_value, "");
1195 }1192 }
1196 }1193 }
1197 case IrBinOpBitShiftRight:1194 case IrBinOpBitShiftRight:
1198 assert(canon_type->id == TypeTableEntryIdInt);1195 assert(type_entry->id == TypeTableEntryIdInt);
1199 if (canon_type->data.integral.is_signed) {1196 if (type_entry->data.integral.is_signed) {
1200 return LLVMBuildAShr(g->builder, op1_value, op2_value, "");1197 return LLVMBuildAShr(g->builder, op1_value, op2_value, "");
1201 } else {1198 } else {
1202 return LLVMBuildLShr(g->builder, op1_value, op2_value, "");1199 return LLVMBuildLShr(g->builder, op1_value, op2_value, "");
1203 }1200 }
1204 case IrBinOpSub:1201 case IrBinOpSub:
1205 case IrBinOpSubWrap:1202 case IrBinOpSubWrap:
1206 if (canon_type->id == TypeTableEntryIdFloat) {1203 if (type_entry->id == TypeTableEntryIdFloat) {
1207 return LLVMBuildFSub(g->builder, op1_value, op2_value, "");1204 return LLVMBuildFSub(g->builder, op1_value, op2_value, "");
1208 } else if (canon_type->id == TypeTableEntryIdInt) {1205 } else if (type_entry->id == TypeTableEntryIdInt) {
1209 bool is_wrapping = (op_id == IrBinOpSubWrap);1206 bool is_wrapping = (op_id == IrBinOpSubWrap);
1210 if (is_wrapping) {1207 if (is_wrapping) {
1211 return LLVMBuildSub(g->builder, op1_value, op2_value, "");1208 return LLVMBuildSub(g->builder, op1_value, op2_value, "");
1212 } else if (want_debug_safety) {1209 } else if (want_debug_safety) {
1213 return gen_overflow_op(g, canon_type, AddSubMulSub, op1_value, op2_value);1210 return gen_overflow_op(g, type_entry, AddSubMulSub, op1_value, op2_value);
1214 } else if (canon_type->data.integral.is_signed) {1211 } else if (type_entry->data.integral.is_signed) {
1215 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");1212 return LLVMBuildNSWSub(g->builder, op1_value, op2_value, "");
1216 } else {1213 } else {
1217 return LLVMBuildNUWSub(g->builder, op1_value, op2_value, "");1214 return LLVMBuildNUWSub(g->builder, op1_value, op2_value, "");
...@@ -1221,15 +1218,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1221,15 +1218,15 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1221 }1218 }
1222 case IrBinOpMult:1219 case IrBinOpMult:
1223 case IrBinOpMultWrap:1220 case IrBinOpMultWrap:
1224 if (canon_type->id == TypeTableEntryIdFloat) {1221 if (type_entry->id == TypeTableEntryIdFloat) {
1225 return LLVMBuildFMul(g->builder, op1_value, op2_value, "");1222 return LLVMBuildFMul(g->builder, op1_value, op2_value, "");
1226 } else if (canon_type->id == TypeTableEntryIdInt) {1223 } else if (type_entry->id == TypeTableEntryIdInt) {
1227 bool is_wrapping = (op_id == IrBinOpMultWrap);1224 bool is_wrapping = (op_id == IrBinOpMultWrap);
1228 if (is_wrapping) {1225 if (is_wrapping) {
1229 return LLVMBuildMul(g->builder, op1_value, op2_value, "");1226 return LLVMBuildMul(g->builder, op1_value, op2_value, "");
1230 } else if (want_debug_safety) {1227 } else if (want_debug_safety) {
1231 return gen_overflow_op(g, canon_type, AddSubMulMul, op1_value, op2_value);1228 return gen_overflow_op(g, type_entry, AddSubMulMul, op1_value, op2_value);
1232 } else if (canon_type->data.integral.is_signed) {1229 } else if (type_entry->data.integral.is_signed) {
1233 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");1230 return LLVMBuildNSWMul(g->builder, op1_value, op2_value, "");
1234 } else {1231 } else {
1235 return LLVMBuildNUWMul(g->builder, op1_value, op2_value, "");1232 return LLVMBuildNUWMul(g->builder, op1_value, op2_value, "");
...@@ -1238,9 +1235,9 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,...@@ -1238,9 +1235,9 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable,
1238 zig_unreachable();1235 zig_unreachable();
1239 }1236 }
1240 case IrBinOpDiv:1237 case IrBinOpDiv:
1241 return gen_div(g, want_debug_safety, op1_value, op2_value, canon_type, false);1238 return gen_div(g, want_debug_safety, op1_value, op2_value, type_entry, false);
1242 case IrBinOpRem:1239 case IrBinOpRem:
1243 return gen_rem(g, want_debug_safety, op1_value, op2_value, canon_type);1240 return gen_rem(g, want_debug_safety, op1_value, op2_value, type_entry);
1244 }1241 }
1245 zig_unreachable();1242 zig_unreachable();
1246}1243}
...@@ -1644,7 +1641,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir...@@ -1644,7 +1641,7 @@ static LLVMValueRef ir_render_store_ptr(CodeGen *g, IrExecutable *executable, Ir
1644 LLVMValueRef value = ir_llvm_value(g, instruction->value);1641 LLVMValueRef value = ir_llvm_value(g, instruction->value);
16451642
1646 assert(instruction->ptr->value.type->id == TypeTableEntryIdPointer);1643 assert(instruction->ptr->value.type->id == TypeTableEntryIdPointer);
1647 TypeTableEntry *ptr_type = get_underlying_type(instruction->ptr->value.type);1644 TypeTableEntry *ptr_type = instruction->ptr->value.type;
16481645
1649 gen_assign_raw(g, ptr, ptr_type, value);1646 gen_assign_raw(g, ptr, ptr_type, value);
16501647
...@@ -1685,9 +1682,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -1685,9 +1682,9 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
1685 if (array_ptr_type->data.pointer.unaligned_bit_count != 0) {1682 if (array_ptr_type->data.pointer.unaligned_bit_count != 0) {
1686 return array_ptr_ptr;1683 return array_ptr_ptr;
1687 }1684 }
1688 TypeTableEntry *canon_child_type = get_underlying_type(array_type->data.array.child_type);1685 TypeTableEntry *child_type = array_type->data.array.child_type;
1689 if (canon_child_type->id == TypeTableEntryIdStruct &&1686 if (child_type->id == TypeTableEntryIdStruct &&
1690 canon_child_type->data.structure.layout == ContainerLayoutPacked)1687 child_type->data.structure.layout == ContainerLayoutPacked)
1691 {1688 {
1692 size_t unaligned_bit_count = instruction->base.value.type->data.pointer.unaligned_bit_count;1689 size_t unaligned_bit_count = instruction->base.value.type->data.pointer.unaligned_bit_count;
1693 if (unaligned_bit_count != 0) {1690 if (unaligned_bit_count != 0) {
...@@ -1701,7 +1698,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI...@@ -1701,7 +1698,7 @@ static LLVMValueRef ir_render_elem_ptr(CodeGen *g, IrExecutable *executable, IrI
1701 byte_offset1698 byte_offset
1702 };1699 };
1703 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");1700 LLVMValueRef elem_byte_ptr = LLVMBuildInBoundsGEP(g->builder, u8_array_ptr, indices, 1, "");
1704 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(canon_child_type->type_ref, 0), "");1701 return LLVMBuildBitCast(g->builder, elem_byte_ptr, LLVMPointerType(child_type->type_ref, 0), "");
1705 }1702 }
1706 }1703 }
1707 LLVMValueRef indices[] = {1704 LLVMValueRef indices[] = {
...@@ -2206,8 +2203,8 @@ static LLVMValueRef ir_render_div_exact(CodeGen *g, IrExecutable *executable, Ir...@@ -2206,8 +2203,8 @@ static LLVMValueRef ir_render_div_exact(CodeGen *g, IrExecutable *executable, Ir
22062203
2207static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrInstructionTruncate *instruction) {2204static LLVMValueRef ir_render_truncate(CodeGen *g, IrExecutable *executable, IrInstructionTruncate *instruction) {
2208 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);2205 LLVMValueRef target_val = ir_llvm_value(g, instruction->target);
2209 TypeTableEntry *dest_type = get_underlying_type(instruction->base.value.type);2206 TypeTableEntry *dest_type = instruction->base.value.type;
2210 TypeTableEntry *src_type = get_underlying_type(instruction->target->value.type);2207 TypeTableEntry *src_type = instruction->target->value.type;
2211 if (dest_type == src_type) {2208 if (dest_type == src_type) {
2212 // no-op2209 // no-op
2213 return target_val;2210 return target_val;
...@@ -2228,7 +2225,7 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutable *executable, IrIns...@@ -2228,7 +2225,7 @@ static LLVMValueRef ir_render_memset(CodeGen *g, IrExecutable *executable, IrIns
22282225
2229 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, dest_ptr, ptr_u8, "");2226 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, dest_ptr, ptr_u8, "");
22302227
2231 TypeTableEntry *ptr_type = get_underlying_type(instruction->dest_ptr->value.type);2228 TypeTableEntry *ptr_type = instruction->dest_ptr->value.type;
2232 assert(ptr_type->id == TypeTableEntryIdPointer);2229 assert(ptr_type->id == TypeTableEntryIdPointer);
22332230
2234 LLVMValueRef is_volatile = ptr_type->data.pointer.is_volatile ?2231 LLVMValueRef is_volatile = ptr_type->data.pointer.is_volatile ?
...@@ -2256,8 +2253,8 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns...@@ -2256,8 +2253,8 @@ static LLVMValueRef ir_render_memcpy(CodeGen *g, IrExecutable *executable, IrIns
2256 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, dest_ptr, ptr_u8, "");2253 LLVMValueRef dest_ptr_casted = LLVMBuildBitCast(g->builder, dest_ptr, ptr_u8, "");
2257 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr, ptr_u8, "");2254 LLVMValueRef src_ptr_casted = LLVMBuildBitCast(g->builder, src_ptr, ptr_u8, "");
22582255
2259 TypeTableEntry *dest_ptr_type = get_underlying_type(instruction->dest_ptr->value.type);2256 TypeTableEntry *dest_ptr_type = instruction->dest_ptr->value.type;
2260 TypeTableEntry *src_ptr_type = get_underlying_type(instruction->src_ptr->value.type);2257 TypeTableEntry *src_ptr_type = instruction->src_ptr->value.type;
22612258
2262 assert(dest_ptr_type->id == TypeTableEntryIdPointer);2259 assert(dest_ptr_type->id == TypeTableEntryIdPointer);
2263 assert(src_ptr_type->id == TypeTableEntryIdPointer);2260 assert(src_ptr_type->id == TypeTableEntryIdPointer);
...@@ -2407,7 +2404,7 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable...@@ -2407,7 +2404,7 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable
2407}2404}
24082405
2409static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) {2406static LLVMValueRef render_shl_with_overflow(CodeGen *g, IrInstructionOverflowOp *instruction) {
2410 TypeTableEntry *int_type = get_underlying_type(instruction->result_ptr_type);2407 TypeTableEntry *int_type = instruction->result_ptr_type;
2411 assert(int_type->id == TypeTableEntryIdInt);2408 assert(int_type->id == TypeTableEntryIdInt);
24122409
2413 LLVMValueRef op1 = ir_llvm_value(g, instruction->op1);2410 LLVMValueRef op1 = ir_llvm_value(g, instruction->op1);
...@@ -2444,7 +2441,7 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,...@@ -2444,7 +2441,7 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,
2444 return render_shl_with_overflow(g, instruction);2441 return render_shl_with_overflow(g, instruction);
2445 }2442 }
24462443
2447 TypeTableEntry *int_type = get_underlying_type(instruction->result_ptr_type);2444 TypeTableEntry *int_type = instruction->result_ptr_type;
2448 assert(int_type->id == TypeTableEntryIdInt);2445 assert(int_type->id == TypeTableEntryIdInt);
24492446
2450 LLVMValueRef fn_val = get_int_overflow_fn(g, int_type, add_sub_mul);2447 LLVMValueRef fn_val = get_int_overflow_fn(g, int_type, add_sub_mul);
...@@ -2467,8 +2464,8 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,...@@ -2467,8 +2464,8 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,
2467}2464}
24682465
2469static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {2466static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {
2470 TypeTableEntry *err_union_type = get_underlying_type(instruction->value->value.type);2467 TypeTableEntry *err_union_type = instruction->value->value.type;
2471 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);2468 TypeTableEntry *child_type = err_union_type->data.error.child_type;
2472 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);2469 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);
24732470
2474 LLVMValueRef err_val;2471 LLVMValueRef err_val;
...@@ -2484,11 +2481,11 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI...@@ -2484,11 +2481,11 @@ static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrI
2484}2481}
24852482
2486static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrCode *instruction) {2483static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrCode *instruction) {
2487 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->value.type);2484 TypeTableEntry *ptr_type = instruction->value->value.type;
2488 assert(ptr_type->id == TypeTableEntryIdPointer);2485 assert(ptr_type->id == TypeTableEntryIdPointer);
2489 bool is_volatile = ptr_type->data.pointer.is_volatile;2486 bool is_volatile = ptr_type->data.pointer.is_volatile;
2490 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);2487 TypeTableEntry *err_union_type = ptr_type->data.pointer.child_type;
2491 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);2488 TypeTableEntry *child_type = err_union_type->data.error.child_type;
2492 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);2489 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);
2493 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, is_volatile);2490 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, is_volatile);
24942491
...@@ -2501,11 +2498,11 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab...@@ -2501,11 +2498,11 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab
2501}2498}
25022499
2503static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrPayload *instruction) {2500static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable, IrInstructionUnwrapErrPayload *instruction) {
2504 TypeTableEntry *ptr_type = get_underlying_type(instruction->value->value.type);2501 TypeTableEntry *ptr_type = instruction->value->value.type;
2505 assert(ptr_type->id == TypeTableEntryIdPointer);2502 assert(ptr_type->id == TypeTableEntryIdPointer);
2506 bool is_volatile = ptr_type->data.pointer.is_volatile;2503 bool is_volatile = ptr_type->data.pointer.is_volatile;
2507 TypeTableEntry *err_union_type = get_underlying_type(ptr_type->data.pointer.child_type);2504 TypeTableEntry *err_union_type = ptr_type->data.pointer.child_type;
2508 TypeTableEntry *child_type = get_underlying_type(err_union_type->data.error.child_type);2505 TypeTableEntry *child_type = err_union_type->data.error.child_type;
2509 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);2506 LLVMValueRef err_union_ptr = ir_llvm_value(g, instruction->value);
2510 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, is_volatile);2507 LLVMValueRef err_union_handle = get_handle_value(g, err_union_ptr, err_union_type, is_volatile);
25112508
...@@ -2941,9 +2938,9 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -2941,9 +2938,9 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
2941 break;2938 break;
2942 }2939 }
29432940
2944 TypeTableEntry *canon_type = get_underlying_type(const_val->type);2941 TypeTableEntry *type_entry = const_val->type;
2945 assert(!canon_type->zero_bits);2942 assert(!type_entry->zero_bits);
2946 switch (canon_type->id) {2943 switch (type_entry->id) {
2947 case TypeTableEntryIdInvalid:2944 case TypeTableEntryIdInvalid:
2948 case TypeTableEntryIdVar:2945 case TypeTableEntryIdVar:
2949 case TypeTableEntryIdMetaType:2946 case TypeTableEntryIdMetaType:
...@@ -2956,12 +2953,12 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -2956,12 +2953,12 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
2956 case TypeTableEntryIdPureError:2953 case TypeTableEntryIdPureError:
2957 case TypeTableEntryIdEnum:2954 case TypeTableEntryIdEnum:
2958 case TypeTableEntryIdEnumTag:2955 case TypeTableEntryIdEnumTag:
2959 case TypeTableEntryIdTypeDecl:
2960 case TypeTableEntryIdNamespace:2956 case TypeTableEntryIdNamespace:
2961 case TypeTableEntryIdBlock:2957 case TypeTableEntryIdBlock:
2962 case TypeTableEntryIdBoundFn:2958 case TypeTableEntryIdBoundFn:
2963 case TypeTableEntryIdArgTuple:2959 case TypeTableEntryIdArgTuple:
2964 case TypeTableEntryIdVoid:2960 case TypeTableEntryIdVoid:
2961 case TypeTableEntryIdOpaque:
2965 zig_unreachable();2962 zig_unreachable();
2966 case TypeTableEntryIdBool:2963 case TypeTableEntryIdBool:
2967 return LLVMConstInt(big_int_type_ref, const_val->data.x_bool ? 1 : 0, false);2964 return LLVMConstInt(big_int_type_ref, const_val->data.x_bool ? 1 : 0, false);
...@@ -2975,7 +2972,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -2975,7 +2972,7 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
2975 {2972 {
2976 LLVMValueRef float_val = gen_const_val(g, const_val);2973 LLVMValueRef float_val = gen_const_val(g, const_val);
2977 LLVMValueRef int_val = LLVMConstFPToUI(float_val,2974 LLVMValueRef int_val = LLVMConstFPToUI(float_val,
2978 LLVMIntType((unsigned)canon_type->data.floating.bit_count));2975 LLVMIntType((unsigned)type_entry->data.floating.bit_count));
2979 return LLVMConstZExt(int_val, big_int_type_ref);2976 return LLVMConstZExt(int_val, big_int_type_ref);
2980 }2977 }
2981 case TypeTableEntryIdPointer:2978 case TypeTableEntryIdPointer:
...@@ -2992,11 +2989,11 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -2992,11 +2989,11 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
2992 zig_panic("TODO bit pack a union");2989 zig_panic("TODO bit pack a union");
2993 case TypeTableEntryIdStruct:2990 case TypeTableEntryIdStruct:
2994 {2991 {
2995 assert(canon_type->data.structure.layout == ContainerLayoutPacked);2992 assert(type_entry->data.structure.layout == ContainerLayoutPacked);
29962993
2997 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);2994 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
2998 for (size_t i = 0; i < canon_type->data.structure.src_field_count; i += 1) {2995 for (size_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) {
2999 TypeStructField *field = &canon_type->data.structure.fields[i];2996 TypeStructField *field = &type_entry->data.structure.fields[i];
3000 if (field->gen_index == SIZE_MAX) {2997 if (field->gen_index == SIZE_MAX) {
3001 continue;2998 continue;
3002 }2999 }
...@@ -3012,37 +3009,35 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con...@@ -3012,37 +3009,35 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
3012}3009}
30133010
3014static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {3011static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3015 TypeTableEntry *canon_type = get_underlying_type(const_val->type);3012 TypeTableEntry *type_entry = const_val->type;
3016 assert(!canon_type->zero_bits);3013 assert(!type_entry->zero_bits);
30173014
3018 switch (const_val->special) {3015 switch (const_val->special) {
3019 case ConstValSpecialRuntime:3016 case ConstValSpecialRuntime:
3020 zig_unreachable();3017 zig_unreachable();
3021 case ConstValSpecialUndef:3018 case ConstValSpecialUndef:
3022 return LLVMGetUndef(canon_type->type_ref);3019 return LLVMGetUndef(type_entry->type_ref);
3023 case ConstValSpecialStatic:3020 case ConstValSpecialStatic:
3024 break;3021 break;
3025 }3022 }
30263023
3027 switch (canon_type->id) {3024 switch (type_entry->id) {
3028 case TypeTableEntryIdTypeDecl:
3029 zig_unreachable();
3030 case TypeTableEntryIdInt:3025 case TypeTableEntryIdInt:
3031 case TypeTableEntryIdEnumTag:3026 case TypeTableEntryIdEnumTag:
3032 return LLVMConstInt(canon_type->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);3027 return LLVMConstInt(type_entry->type_ref, bignum_to_twos_complement(&const_val->data.x_bignum), false);
3033 case TypeTableEntryIdPureError:3028 case TypeTableEntryIdPureError:
3034 assert(const_val->data.x_pure_err);3029 assert(const_val->data.x_pure_err);
3035 return LLVMConstInt(g->builtin_types.entry_pure_error->type_ref,3030 return LLVMConstInt(g->builtin_types.entry_pure_error->type_ref,
3036 const_val->data.x_pure_err->value, false);3031 const_val->data.x_pure_err->value, false);
3037 case TypeTableEntryIdFloat:3032 case TypeTableEntryIdFloat:
3038 if (const_val->data.x_bignum.kind == BigNumKindFloat) {3033 if (const_val->data.x_bignum.kind == BigNumKindFloat) {
3039 return LLVMConstReal(canon_type->type_ref, const_val->data.x_bignum.data.x_float);3034 return LLVMConstReal(type_entry->type_ref, const_val->data.x_bignum.data.x_float);
3040 } else {3035 } else {
3041 double x = (double)const_val->data.x_bignum.data.x_uint;3036 double x = (double)const_val->data.x_bignum.data.x_uint;
3042 if (const_val->data.x_bignum.is_negative) {3037 if (const_val->data.x_bignum.is_negative) {
3043 x = -x;3038 x = -x;
3044 }3039 }
3045 return LLVMConstReal(canon_type->type_ref, x);3040 return LLVMConstReal(type_entry->type_ref, x);
3046 }3041 }
3047 case TypeTableEntryIdBool:3042 case TypeTableEntryIdBool:
3048 if (const_val->data.x_bool) {3043 if (const_val->data.x_bool) {
...@@ -3052,7 +3047,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3052,7 +3047,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3052 }3047 }
3053 case TypeTableEntryIdMaybe:3048 case TypeTableEntryIdMaybe:
3054 {3049 {
3055 TypeTableEntry *child_type = canon_type->data.maybe.child_type;3050 TypeTableEntry *child_type = type_entry->data.maybe.child_type;
3056 if (child_type->zero_bits) {3051 if (child_type->zero_bits) {
3057 return LLVMConstInt(LLVMInt1Type(), const_val->data.x_maybe ? 1 : 0, false);3052 return LLVMConstInt(LLVMInt1Type(), const_val->data.x_maybe ? 1 : 0, false);
3058 } else if (child_type->id == TypeTableEntryIdPointer ||3053 } else if (child_type->id == TypeTableEntryIdPointer ||
...@@ -3082,12 +3077,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3082,12 +3077,12 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3082 }3077 }
3083 case TypeTableEntryIdStruct:3078 case TypeTableEntryIdStruct:
3084 {3079 {
3085 LLVMValueRef *fields = allocate<LLVMValueRef>(canon_type->data.structure.gen_field_count);3080 LLVMValueRef *fields = allocate<LLVMValueRef>(type_entry->data.structure.gen_field_count);
3086 size_t src_field_count = canon_type->data.structure.src_field_count;3081 size_t src_field_count = type_entry->data.structure.src_field_count;
3087 if (canon_type->data.structure.layout == ContainerLayoutPacked) {3082 if (type_entry->data.structure.layout == ContainerLayoutPacked) {
3088 size_t src_field_index = 0;3083 size_t src_field_index = 0;
3089 while (src_field_index < src_field_count) {3084 while (src_field_index < src_field_count) {
3090 TypeStructField *type_struct_field = &canon_type->data.structure.fields[src_field_index];3085 TypeStructField *type_struct_field = &type_entry->data.structure.fields[src_field_index];
3091 if (type_struct_field->gen_index == SIZE_MAX) {3086 if (type_struct_field->gen_index == SIZE_MAX) {
3092 src_field_index += 1;3087 src_field_index += 1;
3093 continue;3088 continue;
...@@ -3095,7 +3090,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3095,7 +3090,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
30953090
3096 size_t src_field_index_end = src_field_index + 1;3091 size_t src_field_index_end = src_field_index + 1;
3097 for (; src_field_index_end < src_field_count; src_field_index_end += 1) {3092 for (; src_field_index_end < src_field_count; src_field_index_end += 1) {
3098 TypeStructField *it_field = &canon_type->data.structure.fields[src_field_index_end];3093 TypeStructField *it_field = &type_entry->data.structure.fields[src_field_index_end];
3099 if (it_field->gen_index != type_struct_field->gen_index)3094 if (it_field->gen_index != type_struct_field->gen_index)
3100 break;3095 break;
3101 }3096 }
...@@ -3104,11 +3099,11 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3104,11 +3099,11 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3104 fields[type_struct_field->gen_index] =3099 fields[type_struct_field->gen_index] =
3105 gen_const_val(g, &const_val->data.x_struct.fields[src_field_index]);3100 gen_const_val(g, &const_val->data.x_struct.fields[src_field_index]);
3106 } else {3101 } else {
3107 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(canon_type->type_ref,3102 LLVMTypeRef big_int_type_ref = LLVMStructGetTypeAtIndex(type_entry->type_ref,
3108 (unsigned)type_struct_field->gen_index);3103 (unsigned)type_struct_field->gen_index);
3109 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);3104 LLVMValueRef val = LLVMConstInt(big_int_type_ref, 0, false);
3110 for (size_t i = src_field_index; i < src_field_index_end; i += 1) {3105 for (size_t i = src_field_index; i < src_field_index_end; i += 1) {
3111 TypeStructField *it_field = &canon_type->data.structure.fields[i];3106 TypeStructField *it_field = &type_entry->data.structure.fields[i];
3112 if (it_field->gen_index == SIZE_MAX) {3107 if (it_field->gen_index == SIZE_MAX) {
3113 continue;3108 continue;
3114 }3109 }
...@@ -3126,14 +3121,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3126,14 +3121,14 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3126 }3121 }
3127 } else {3122 } else {
3128 for (uint32_t i = 0; i < src_field_count; i += 1) {3123 for (uint32_t i = 0; i < src_field_count; i += 1) {
3129 TypeStructField *type_struct_field = &canon_type->data.structure.fields[i];3124 TypeStructField *type_struct_field = &type_entry->data.structure.fields[i];
3130 if (type_struct_field->gen_index == SIZE_MAX) {3125 if (type_struct_field->gen_index == SIZE_MAX) {
3131 continue;3126 continue;
3132 }3127 }
3133 fields[type_struct_field->gen_index] = gen_const_val(g, &const_val->data.x_struct.fields[i]);3128 fields[type_struct_field->gen_index] = gen_const_val(g, &const_val->data.x_struct.fields[i]);
3134 }3129 }
3135 }3130 }
3136 return LLVMConstNamedStruct(canon_type->type_ref, fields, canon_type->data.structure.gen_field_count);3131 return LLVMConstNamedStruct(type_entry->type_ref, fields, type_entry->data.structure.gen_field_count);
3137 }3132 }
3138 case TypeTableEntryIdUnion:3133 case TypeTableEntryIdUnion:
3139 {3134 {
...@@ -3141,7 +3136,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3141,7 +3136,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3141 }3136 }
3142 case TypeTableEntryIdArray:3137 case TypeTableEntryIdArray:
3143 {3138 {
3144 uint64_t len = canon_type->data.array.len;3139 uint64_t len = type_entry->data.array.len;
3145 LLVMValueRef *values = allocate<LLVMValueRef>(len);3140 LLVMValueRef *values = allocate<LLVMValueRef>(len);
3146 for (uint64_t i = 0; i < len; i += 1) {3141 for (uint64_t i = 0; i < len; i += 1) {
3147 ConstExprValue *elem_value = &const_val->data.x_array.elements[i];3142 ConstExprValue *elem_value = &const_val->data.x_array.elements[i];
...@@ -3151,13 +3146,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3151,13 +3146,13 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3151 }3146 }
3152 case TypeTableEntryIdEnum:3147 case TypeTableEntryIdEnum:
3153 {3148 {
3154 LLVMTypeRef tag_type_ref = canon_type->data.enumeration.tag_type->type_ref;3149 LLVMTypeRef tag_type_ref = type_entry->data.enumeration.tag_type->type_ref;
3155 LLVMValueRef tag_value = LLVMConstInt(tag_type_ref, const_val->data.x_enum.tag, false);3150 LLVMValueRef tag_value = LLVMConstInt(tag_type_ref, const_val->data.x_enum.tag, false);
3156 if (canon_type->data.enumeration.gen_field_count == 0) {3151 if (type_entry->data.enumeration.gen_field_count == 0) {
3157 return tag_value;3152 return tag_value;
3158 } else {3153 } else {
3159 TypeTableEntry *union_type = canon_type->data.enumeration.union_type;3154 TypeTableEntry *union_type = type_entry->data.enumeration.union_type;
3160 TypeEnumField *enum_field = &canon_type->data.enumeration.fields[const_val->data.x_enum.tag];3155 TypeEnumField *enum_field = &type_entry->data.enumeration.fields[const_val->data.x_enum.tag];
3161 assert(enum_field->value == const_val->data.x_enum.tag);3156 assert(enum_field->value == const_val->data.x_enum.tag);
3162 LLVMValueRef union_value;3157 LLVMValueRef union_value;
3163 if (type_has_bits(enum_field->type_entry)) {3158 if (type_has_bits(enum_field->type_entry)) {
...@@ -3261,7 +3256,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3261,7 +3256,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3261 }3256 }
3262 case TypeTableEntryIdErrorUnion:3257 case TypeTableEntryIdErrorUnion:
3263 {3258 {
3264 TypeTableEntry *child_type = canon_type->data.error.child_type;3259 TypeTableEntry *child_type = type_entry->data.error.child_type;
3265 if (!type_has_bits(child_type)) {3260 if (!type_has_bits(child_type)) {
3266 uint64_t value = const_val->data.x_err_union.err ? const_val->data.x_err_union.err->value : 0;3261 uint64_t value = const_val->data.x_err_union.err ? const_val->data.x_err_union.err->value : 0;
3267 return LLVMConstInt(g->err_tag_type->type_ref, value, false);3262 return LLVMConstInt(g->err_tag_type->type_ref, value, false);
...@@ -3296,6 +3291,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3296,6 +3291,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3296 case TypeTableEntryIdBoundFn:3291 case TypeTableEntryIdBoundFn:
3297 case TypeTableEntryIdVar:3292 case TypeTableEntryIdVar:
3298 case TypeTableEntryIdArgTuple:3293 case TypeTableEntryIdArgTuple:
3294 case TypeTableEntryIdOpaque:
3299 zig_unreachable();3295 zig_unreachable();
33003296
3301 }3297 }
...@@ -4107,7 +4103,7 @@ static void define_builtin_types(CodeGen *g) {...@@ -4107,7 +4103,7 @@ static void define_builtin_types(CodeGen *g) {
4107 g->builtin_types.entry_i64 = get_int_type(g, true, 64);4103 g->builtin_types.entry_i64 = get_int_type(g, true, 64);
41084104
4109 {4105 {
4110 g->builtin_types.entry_c_void = get_typedecl_type(g, "c_void", g->builtin_types.entry_u8);4106 g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void");
4111 g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);4107 g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
4112 }4108 }
41134109
...@@ -4747,6 +4743,7 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {...@@ -4747,6 +4743,7 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
4747 zig_unreachable();4743 zig_unreachable();
4748 }4744 }
4749 }4745 }
4746 case TypeTableEntryIdOpaque:
4750 case TypeTableEntryIdArray:4747 case TypeTableEntryIdArray:
4751 case TypeTableEntryIdStruct:4748 case TypeTableEntryIdStruct:
4752 case TypeTableEntryIdErrorUnion:4749 case TypeTableEntryIdErrorUnion:
...@@ -4754,7 +4751,6 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {...@@ -4754,7 +4751,6 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
4754 case TypeTableEntryIdEnum:4751 case TypeTableEntryIdEnum:
4755 case TypeTableEntryIdUnion:4752 case TypeTableEntryIdUnion:
4756 case TypeTableEntryIdFn:4753 case TypeTableEntryIdFn:
4757 case TypeTableEntryIdTypeDecl:
4758 case TypeTableEntryIdEnumTag:4754 case TypeTableEntryIdEnumTag:
4759 zig_panic("TODO implement get_c_type for more types");4755 zig_panic("TODO implement get_c_type for more types");
4760 case TypeTableEntryIdInvalid:4756 case TypeTableEntryIdInvalid:
src/ir.cpp+141-238
...@@ -5323,11 +5323,6 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *nod...@@ -5323,11 +5323,6 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *nod
5323 return ir_build_br(irb, scope, node, dest_block, is_comptime);5323 return ir_build_br(irb, scope, node, dest_block, is_comptime);
5324}5324}
53255325
5326static IrInstruction *ir_gen_type_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
5327 assert(node->type == NodeTypeTypeLiteral);
5328 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_type);
5329}
5330
5331static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *node) {5326static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *node) {
5332 assert(node->type == NodeTypeErrorType);5327 assert(node->type == NodeTypeErrorType);
5333 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_pure_error);5328 return ir_build_const_type(irb, scope, node, irb->codegen->builtin_types.entry_pure_error);
...@@ -5600,8 +5595,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop...@@ -5600,8 +5595,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
5600 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);5595 return ir_lval_wrap(irb, scope, ir_gen_goto(irb, scope, node), lval);
5601 case NodeTypeCompTime:5596 case NodeTypeCompTime:
5602 return ir_gen_comptime(irb, scope, node, lval);5597 return ir_gen_comptime(irb, scope, node, lval);
5603 case NodeTypeTypeLiteral:
5604 return ir_lval_wrap(irb, scope, ir_gen_type_literal(irb, scope, node), lval);
5605 case NodeTypeErrorType:5598 case NodeTypeErrorType:
5606 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval);5599 return ir_lval_wrap(irb, scope, ir_gen_error_type(irb, scope, node), lval);
5607 case NodeTypeBreak:5600 case NodeTypeBreak:
...@@ -5628,8 +5621,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop...@@ -5628,8 +5621,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
5628 zig_panic("TODO IR gen NodeTypeFnDecl");5621 zig_panic("TODO IR gen NodeTypeFnDecl");
5629 case NodeTypeErrorValueDecl:5622 case NodeTypeErrorValueDecl:
5630 zig_panic("TODO IR gen NodeTypeErrorValueDecl");5623 zig_panic("TODO IR gen NodeTypeErrorValueDecl");
5631 case NodeTypeTypeDecl:
5632 zig_panic("TODO IR gen NodeTypeTypeDecl");
5633 case NodeTypeTestDecl:5624 case NodeTypeTestDecl:
5634 zig_panic("TODO IR gen NodeTypeTestDecl");5625 zig_panic("TODO IR gen NodeTypeTestDecl");
5635 }5626 }
...@@ -5753,13 +5744,6 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction,...@@ -5753,13 +5744,6 @@ static ErrorMsg *ir_add_error(IrAnalyze *ira, IrInstruction *source_instruction,
5753 return ir_add_error_node(ira, source_instruction->source_node, msg);5744 return ir_add_error_node(ira, source_instruction->source_node, msg);
5754}5745}
57555746
5756static void ir_add_typedef_err_note(IrAnalyze *ira, ErrorMsg *msg, TypeTableEntry *type_entry) {
5757 if (type_entry->id == TypeTableEntryIdTypeDecl) {
5758 // requires tracking source_node in the typedecl type
5759 zig_panic("TODO add error note about typedecls");
5760 }
5761}
5762
5763static IrInstruction *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec) {5747static IrInstruction *ir_exec_const_result(CodeGen *codegen, IrExecutable *exec) {
5764 IrBasicBlock *bb = exec->basic_block_list.at(0);5748 IrBasicBlock *bb = exec->basic_block_list.at(0);
5765 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {5749 for (size_t i = 0; i < bb->instruction_list.length; i += 1) {
...@@ -5791,27 +5775,25 @@ static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *so...@@ -5791,27 +5775,25 @@ static bool ir_emit_global_runtime_side_effect(IrAnalyze *ira, IrInstruction *so
5791}5775}
57925776
5793static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruction, TypeTableEntry *other_type) {5777static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruction, TypeTableEntry *other_type) {
5794 TypeTableEntry *other_type_underlying = get_underlying_type(other_type);5778 if (type_is_invalid(other_type)) {
5795
5796 if (type_is_invalid(other_type_underlying)) {
5797 return false;5779 return false;
5798 }5780 }
57995781
5800 ConstExprValue *const_val = &instruction->value;5782 ConstExprValue *const_val = &instruction->value;
5801 assert(const_val->special != ConstValSpecialRuntime);5783 assert(const_val->special != ConstValSpecialRuntime);
5802 if (other_type_underlying->id == TypeTableEntryIdFloat) {5784 if (other_type->id == TypeTableEntryIdFloat) {
5803 return true;5785 return true;
5804 } else if (other_type_underlying->id == TypeTableEntryIdInt &&5786 } else if (other_type->id == TypeTableEntryIdInt &&
5805 const_val->data.x_bignum.kind == BigNumKindInt)5787 const_val->data.x_bignum.kind == BigNumKindInt)
5806 {5788 {
5807 if (bignum_fits_in_bits(&const_val->data.x_bignum, other_type_underlying->data.integral.bit_count,5789 if (bignum_fits_in_bits(&const_val->data.x_bignum, other_type->data.integral.bit_count,
5808 other_type_underlying->data.integral.is_signed))5790 other_type->data.integral.is_signed))
5809 {5791 {
5810 return true;5792 return true;
5811 }5793 }
5812 } else if ((other_type_underlying->id == TypeTableEntryIdNumLitFloat &&5794 } else if ((other_type->id == TypeTableEntryIdNumLitFloat &&
5813 const_val->data.x_bignum.kind == BigNumKindFloat) ||5795 const_val->data.x_bignum.kind == BigNumKindFloat) ||
5814 (other_type_underlying->id == TypeTableEntryIdNumLitInt &&5796 (other_type->id == TypeTableEntryIdNumLitInt &&
5815 const_val->data.x_bignum.kind == BigNumKindInt))5797 const_val->data.x_bignum.kind == BigNumKindInt))
5816 {5798 {
5817 return true;5799 return true;
...@@ -6890,12 +6872,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -6890,12 +6872,9 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
6890 TypeTableEntry *wanted_type, IrInstruction *value)6872 TypeTableEntry *wanted_type, IrInstruction *value)
6891{6873{
6892 TypeTableEntry *actual_type = value->value.type;6874 TypeTableEntry *actual_type = value->value.type;
6893 TypeTableEntry *wanted_type_canon = get_underlying_type(wanted_type);
6894 TypeTableEntry *actual_type_canon = get_underlying_type(actual_type);
6895
6896 TypeTableEntry *usize_type = ira->codegen->builtin_types.entry_usize;6875 TypeTableEntry *usize_type = ira->codegen->builtin_types.entry_usize;
68976876
6898 if (type_is_invalid(wanted_type_canon) || type_is_invalid(actual_type_canon)) {6877 if (type_is_invalid(wanted_type) || type_is_invalid(actual_type)) {
6899 return ira->codegen->invalid_instruction;6878 return ira->codegen->invalid_instruction;
6900 }6879 }
69016880
...@@ -6908,36 +6887,36 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -6908,36 +6887,36 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
6908 }6887 }
69096888
6910 // explicit cast from bool to int6889 // explicit cast from bool to int
6911 if (wanted_type_canon->id == TypeTableEntryIdInt &&6890 if (wanted_type->id == TypeTableEntryIdInt &&
6912 actual_type_canon->id == TypeTableEntryIdBool)6891 actual_type->id == TypeTableEntryIdBool)
6913 {6892 {
6914 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);6893 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpBoolToInt, false);
6915 }6894 }
69166895
6917 // explicit cast from pointer to usize6896 // explicit cast from pointer to usize
6918 if (wanted_type_canon == usize_type && type_is_codegen_pointer(actual_type_canon)) {6897 if (wanted_type == usize_type && type_is_codegen_pointer(actual_type)) {
6919 return ir_analyze_ptr_to_int(ira, source_instr, value, wanted_type);6898 return ir_analyze_ptr_to_int(ira, source_instr, value, wanted_type);
6920 }6899 }
69216900
6922 // explicit widening or shortening cast6901 // explicit widening or shortening cast
6923 if ((wanted_type_canon->id == TypeTableEntryIdInt &&6902 if ((wanted_type->id == TypeTableEntryIdInt &&
6924 actual_type_canon->id == TypeTableEntryIdInt) ||6903 actual_type->id == TypeTableEntryIdInt) ||
6925 (wanted_type_canon->id == TypeTableEntryIdFloat &&6904 (wanted_type->id == TypeTableEntryIdFloat &&
6926 actual_type_canon->id == TypeTableEntryIdFloat))6905 actual_type->id == TypeTableEntryIdFloat))
6927 {6906 {
6928 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);6907 return ir_analyze_widen_or_shorten(ira, source_instr, value, wanted_type);
6929 }6908 }
69306909
6931 // explicit cast from int to float6910 // explicit cast from int to float
6932 if (wanted_type_canon->id == TypeTableEntryIdFloat &&6911 if (wanted_type->id == TypeTableEntryIdFloat &&
6933 actual_type_canon->id == TypeTableEntryIdInt)6912 actual_type->id == TypeTableEntryIdInt)
6934 {6913 {
6935 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpIntToFloat, false);6914 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpIntToFloat, false);
6936 }6915 }
69376916
6938 // explicit cast from float to int6917 // explicit cast from float to int
6939 if (wanted_type_canon->id == TypeTableEntryIdInt &&6918 if (wanted_type->id == TypeTableEntryIdInt &&
6940 actual_type_canon->id == TypeTableEntryIdFloat)6919 actual_type->id == TypeTableEntryIdFloat)
6941 {6920 {
6942 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpFloatToInt, false);6921 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpFloatToInt, false);
6943 }6922 }
...@@ -7070,17 +7049,17 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst...@@ -7070,17 +7049,17 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
7070 return ira->codegen->invalid_instruction;7049 return ira->codegen->invalid_instruction;
70717050
7072 return cast2;7051 return cast2;
7073 } else if (ir_num_lit_fits_in_other_type(ira, value, wanted_type_canon)) {7052 } else if (ir_num_lit_fits_in_other_type(ira, value, wanted_type)) {
7074 CastOp op;7053 CastOp op;
7075 if ((actual_type->id == TypeTableEntryIdNumLitFloat &&7054 if ((actual_type->id == TypeTableEntryIdNumLitFloat &&
7076 wanted_type_canon->id == TypeTableEntryIdFloat) ||7055 wanted_type->id == TypeTableEntryIdFloat) ||
7077 (actual_type->id == TypeTableEntryIdNumLitInt &&7056 (actual_type->id == TypeTableEntryIdNumLitInt &&
7078 wanted_type_canon->id == TypeTableEntryIdInt))7057 wanted_type->id == TypeTableEntryIdInt))
7079 {7058 {
7080 op = CastOpNoop;7059 op = CastOpNoop;
7081 } else if (wanted_type_canon->id == TypeTableEntryIdInt) {7060 } else if (wanted_type->id == TypeTableEntryIdInt) {
7082 op = CastOpFloatToInt;7061 op = CastOpFloatToInt;
7083 } else if (wanted_type_canon->id == TypeTableEntryIdFloat) {7062 } else if (wanted_type->id == TypeTableEntryIdFloat) {
7084 op = CastOpIntToFloat;7063 op = CastOpIntToFloat;
7085 } else {7064 } else {
7086 zig_unreachable();7065 zig_unreachable();
...@@ -7475,7 +7454,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp...@@ -7475,7 +7454,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
7475 case TypeTableEntryIdPointer:7454 case TypeTableEntryIdPointer:
7476 case TypeTableEntryIdPureError:7455 case TypeTableEntryIdPureError:
7477 case TypeTableEntryIdFn:7456 case TypeTableEntryIdFn:
7478 case TypeTableEntryIdTypeDecl:7457 case TypeTableEntryIdOpaque:
7479 case TypeTableEntryIdNamespace:7458 case TypeTableEntryIdNamespace:
7480 case TypeTableEntryIdBlock:7459 case TypeTableEntryIdBlock:
7481 case TypeTableEntryIdBoundFn:7460 case TypeTableEntryIdBoundFn:
...@@ -7709,15 +7688,14 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -7709,15 +7688,14 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
7709 TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, bin_op_instruction->base.source_node, instructions, 2);7688 TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, bin_op_instruction->base.source_node, instructions, 2);
7710 if (type_is_invalid(resolved_type))7689 if (type_is_invalid(resolved_type))
7711 return resolved_type;7690 return resolved_type;
7712 TypeTableEntry *canon_resolved_type = get_underlying_type(resolved_type);
7713 IrBinOp op_id = bin_op_instruction->op_id;7691 IrBinOp op_id = bin_op_instruction->op_id;
77147692
7715 if (canon_resolved_type->id == TypeTableEntryIdInt ||7693 if (resolved_type->id == TypeTableEntryIdInt ||
7716 canon_resolved_type->id == TypeTableEntryIdNumLitInt)7694 resolved_type->id == TypeTableEntryIdNumLitInt)
7717 {7695 {
7718 // int7696 // int
7719 } else if ((canon_resolved_type->id == TypeTableEntryIdFloat ||7697 } else if ((resolved_type->id == TypeTableEntryIdFloat ||
7720 canon_resolved_type->id == TypeTableEntryIdNumLitFloat) &&7698 resolved_type->id == TypeTableEntryIdNumLitFloat) &&
7721 (op_id == IrBinOpAdd ||7699 (op_id == IrBinOpAdd ||
7722 op_id == IrBinOpSub ||7700 op_id == IrBinOpSub ||
7723 op_id == IrBinOpMult ||7701 op_id == IrBinOpMult ||
...@@ -7751,7 +7729,7 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -7751,7 +7729,7 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
7751 bin_op_instruction->base.other = &bin_op_instruction->base;7729 bin_op_instruction->base.other = &bin_op_instruction->base;
77527730
7753 int err;7731 int err;
7754 if ((err = ir_eval_math_op(canon_resolved_type, op1_val, op_id, op2_val, out_val))) {7732 if ((err = ir_eval_math_op(resolved_type, op1_val, op_id, op2_val, out_val))) {
7755 if (err == ErrorDivByZero) {7733 if (err == ErrorDivByZero) {
7756 ir_add_error_node(ira, bin_op_instruction->base.source_node,7734 ir_add_error_node(ira, bin_op_instruction->base.source_node,
7757 buf_sprintf("division by zero is undefined"));7735 buf_sprintf("division by zero is undefined"));
...@@ -7776,13 +7754,13 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp...@@ -7776,13 +7754,13 @@ static TypeTableEntry *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
77767754
7777static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruction) {7755static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *instruction) {
7778 IrInstruction *op1 = instruction->op1->other;7756 IrInstruction *op1 = instruction->op1->other;
7779 TypeTableEntry *op1_canon_type = get_underlying_type(op1->value.type);7757 TypeTableEntry *op1_type = op1->value.type;
7780 if (type_is_invalid(op1_canon_type))7758 if (type_is_invalid(op1_type))
7781 return ira->codegen->builtin_types.entry_invalid;7759 return ira->codegen->builtin_types.entry_invalid;
77827760
7783 IrInstruction *op2 = instruction->op2->other;7761 IrInstruction *op2 = instruction->op2->other;
7784 TypeTableEntry *op2_canon_type = get_underlying_type(op2->value.type);7762 TypeTableEntry *op2_type = op2->value.type;
7785 if (type_is_invalid(op2_canon_type))7763 if (type_is_invalid(op2_type))
7786 return ira->codegen->builtin_types.entry_invalid;7764 return ira->codegen->builtin_types.entry_invalid;
77877765
7788 ConstExprValue *op1_val = ir_resolve_const(ira, op1, UndefBad);7766 ConstExprValue *op1_val = ir_resolve_const(ira, op1, UndefBad);
...@@ -7797,22 +7775,22 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7797,22 +7775,22 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7797 size_t op1_array_index;7775 size_t op1_array_index;
7798 size_t op1_array_end;7776 size_t op1_array_end;
7799 TypeTableEntry *child_type;7777 TypeTableEntry *child_type;
7800 if (op1_canon_type->id == TypeTableEntryIdArray) {7778 if (op1_type->id == TypeTableEntryIdArray) {
7801 child_type = op1_canon_type->data.array.child_type;7779 child_type = op1_type->data.array.child_type;
7802 op1_array_val = op1_val;7780 op1_array_val = op1_val;
7803 op1_array_index = 0;7781 op1_array_index = 0;
7804 op1_array_end = op1_canon_type->data.array.len;7782 op1_array_end = op1_type->data.array.len;
7805 } else if (op1_canon_type->id == TypeTableEntryIdPointer &&7783 } else if (op1_type->id == TypeTableEntryIdPointer &&
7806 op1_canon_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&7784 op1_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&
7807 op1_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&7785 op1_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&
7808 op1_val->data.x_ptr.data.base_array.is_cstr)7786 op1_val->data.x_ptr.data.base_array.is_cstr)
7809 {7787 {
7810 child_type = op1_canon_type->data.pointer.child_type;7788 child_type = op1_type->data.pointer.child_type;
7811 op1_array_val = op1_val->data.x_ptr.data.base_array.array_val;7789 op1_array_val = op1_val->data.x_ptr.data.base_array.array_val;
7812 op1_array_index = op1_val->data.x_ptr.data.base_array.elem_index;7790 op1_array_index = op1_val->data.x_ptr.data.base_array.elem_index;
7813 op1_array_end = op1_array_val->type->data.array.len - 1;7791 op1_array_end = op1_array_val->type->data.array.len - 1;
7814 } else if (is_slice(op1_canon_type)) {7792 } else if (is_slice(op1_type)) {
7815 TypeTableEntry *ptr_type = op1_canon_type->data.structure.fields[slice_ptr_index].type_entry;7793 TypeTableEntry *ptr_type = op1_type->data.structure.fields[slice_ptr_index].type_entry;
7816 child_type = ptr_type->data.pointer.child_type;7794 child_type = ptr_type->data.pointer.child_type;
7817 ConstExprValue *ptr_val = &op1_val->data.x_struct.fields[slice_ptr_index];7795 ConstExprValue *ptr_val = &op1_val->data.x_struct.fields[slice_ptr_index];
7818 assert(ptr_val->data.x_ptr.special == ConstPtrSpecialBaseArray);7796 assert(ptr_val->data.x_ptr.special == ConstPtrSpecialBaseArray);
...@@ -7822,15 +7800,14 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7822,15 +7800,14 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7822 } else {7800 } else {
7823 ir_add_error(ira, op1,7801 ir_add_error(ira, op1,
7824 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));7802 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
7825 // TODO if meta_type is type decl, add note pointing to type decl declaration
7826 return ira->codegen->builtin_types.entry_invalid;7803 return ira->codegen->builtin_types.entry_invalid;
7827 }7804 }
78287805
7829 ConstExprValue *op2_array_val;7806 ConstExprValue *op2_array_val;
7830 size_t op2_array_index;7807 size_t op2_array_index;
7831 size_t op2_array_end;7808 size_t op2_array_end;
7832 if (op2_canon_type->id == TypeTableEntryIdArray) {7809 if (op2_type->id == TypeTableEntryIdArray) {
7833 if (op2_canon_type->data.array.child_type != child_type) {7810 if (op2_type->data.array.child_type != child_type) {
7834 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",7811 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",
7835 buf_ptr(&child_type->name),7812 buf_ptr(&child_type->name),
7836 buf_ptr(&op2->value.type->name)));7813 buf_ptr(&op2->value.type->name)));
...@@ -7839,8 +7816,8 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7839,8 +7816,8 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7839 op2_array_val = op2_val;7816 op2_array_val = op2_val;
7840 op2_array_index = 0;7817 op2_array_index = 0;
7841 op2_array_end = op2_array_val->type->data.array.len;7818 op2_array_end = op2_array_val->type->data.array.len;
7842 } else if (op2_canon_type->id == TypeTableEntryIdPointer &&7819 } else if (op2_type->id == TypeTableEntryIdPointer &&
7843 op2_canon_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&7820 op2_type->data.pointer.child_type == ira->codegen->builtin_types.entry_u8 &&
7844 op2_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&7821 op2_val->data.x_ptr.special == ConstPtrSpecialBaseArray &&
7845 op2_val->data.x_ptr.data.base_array.is_cstr)7822 op2_val->data.x_ptr.data.base_array.is_cstr)
7846 {7823 {
...@@ -7853,8 +7830,8 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7853,8 +7830,8 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7853 op2_array_val = op2_val->data.x_ptr.data.base_array.array_val;7830 op2_array_val = op2_val->data.x_ptr.data.base_array.array_val;
7854 op2_array_index = op2_val->data.x_ptr.data.base_array.elem_index;7831 op2_array_index = op2_val->data.x_ptr.data.base_array.elem_index;
7855 op2_array_end = op2_array_val->type->data.array.len - 1;7832 op2_array_end = op2_array_val->type->data.array.len - 1;
7856 } else if (is_slice(op2_canon_type)) {7833 } else if (is_slice(op2_type)) {
7857 TypeTableEntry *ptr_type = op2_canon_type->data.structure.fields[slice_ptr_index].type_entry;7834 TypeTableEntry *ptr_type = op2_type->data.structure.fields[slice_ptr_index].type_entry;
7858 if (ptr_type->data.pointer.child_type != child_type) {7835 if (ptr_type->data.pointer.child_type != child_type) {
7859 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",7836 ir_add_error(ira, op2, buf_sprintf("expected array of type '%s', found '%s'",
7860 buf_ptr(&child_type->name),7837 buf_ptr(&child_type->name),
...@@ -7869,7 +7846,6 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7869,7 +7846,6 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7869 } else {7846 } else {
7870 ir_add_error(ira, op2,7847 ir_add_error(ira, op2,
7871 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));7848 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));
7872 // TODO if meta_type is type decl, add note pointing to type decl declaration
7873 return ira->codegen->builtin_types.entry_invalid;7849 return ira->codegen->builtin_types.entry_invalid;
7874 }7850 }
78757851
...@@ -7878,7 +7854,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *...@@ -7878,7 +7854,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
7878 TypeTableEntry *result_type;7854 TypeTableEntry *result_type;
7879 ConstExprValue *out_array_val;7855 ConstExprValue *out_array_val;
7880 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);7856 size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index);
7881 if (op1_canon_type->id == TypeTableEntryIdArray || op2_canon_type->id == TypeTableEntryIdArray) {7857 if (op1_type->id == TypeTableEntryIdArray || op2_type->id == TypeTableEntryIdArray) {
7882 result_type = get_array_type(ira->codegen, child_type, new_len);7858 result_type = get_array_type(ira->codegen, child_type, new_len);
78837859
7884 out_array_val = out_val;7860 out_array_val = out_val;
...@@ -7931,14 +7907,13 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp...@@ -7931,14 +7907,13 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
7931 if (!ir_resolve_usize(ira, op2, &mult_amt))7907 if (!ir_resolve_usize(ira, op2, &mult_amt))
7932 return ira->codegen->builtin_types.entry_invalid;7908 return ira->codegen->builtin_types.entry_invalid;
79337909
7934 TypeTableEntry *array_canon_type = get_underlying_type(op1->value.type);7910 TypeTableEntry *array_type = op1->value.type;
7935 if (array_canon_type->id != TypeTableEntryIdArray) {7911 if (array_type->id != TypeTableEntryIdArray) {
7936 ir_add_error(ira, op1, buf_sprintf("expected array type, found '%s'", buf_ptr(&op1->value.type->name)));7912 ir_add_error(ira, op1, buf_sprintf("expected array type, found '%s'", buf_ptr(&op1->value.type->name)));
7937 // TODO if meta_type is type decl, add note pointing to type decl declaration
7938 return ira->codegen->builtin_types.entry_invalid;7913 return ira->codegen->builtin_types.entry_invalid;
7939 }7914 }
79407915
7941 uint64_t old_array_len = array_canon_type->data.array.len;7916 uint64_t old_array_len = array_type->data.array.len;
79427917
7943 BigNum array_len;7918 BigNum array_len;
7944 bignum_init_unsigned(&array_len, old_array_len);7919 bignum_init_unsigned(&array_len, old_array_len);
...@@ -7961,7 +7936,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp...@@ -7961,7 +7936,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
7961 }7936 }
7962 assert(i == new_array_len);7937 assert(i == new_array_len);
79637938
7964 TypeTableEntry *child_type = array_canon_type->data.array.child_type;7939 TypeTableEntry *child_type = array_type->data.array.child_type;
7965 return get_array_type(ira->codegen, child_type, new_array_len);7940 return get_array_type(ira->codegen, child_type, new_array_len);
7966}7941}
79677942
...@@ -8033,7 +8008,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -8033,7 +8008,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
8033 AstNode *source_node = decl_var_instruction->base.source_node;8008 AstNode *source_node = decl_var_instruction->base.source_node;
80348009
8035 IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type);8010 IrInstruction *casted_init_value = ir_implicit_cast(ira, init_value, explicit_type);
8036 TypeTableEntry *result_type = get_underlying_type(casted_init_value->value.type);8011 TypeTableEntry *result_type = casted_init_value->value.type;
8037 if (type_is_invalid(result_type)) {8012 if (type_is_invalid(result_type)) {
8038 result_type = ira->codegen->builtin_types.entry_invalid;8013 result_type = ira->codegen->builtin_types.entry_invalid;
8039 }8014 }
...@@ -8041,8 +8016,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -8041,8 +8016,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
8041 bool is_comptime_var = ir_get_var_is_comptime(var);8016 bool is_comptime_var = ir_get_var_is_comptime(var);
80428017
8043 switch (result_type->id) {8018 switch (result_type->id) {
8044 case TypeTableEntryIdTypeDecl:
8045 zig_unreachable();
8046 case TypeTableEntryIdInvalid:8019 case TypeTableEntryIdInvalid:
8047 break; // handled above8020 break; // handled above
8048 case TypeTableEntryIdNumLitFloat:8021 case TypeTableEntryIdNumLitFloat:
...@@ -8057,6 +8030,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc...@@ -8057,6 +8030,7 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc
8057 case TypeTableEntryIdVar:8030 case TypeTableEntryIdVar:
8058 case TypeTableEntryIdBlock:8031 case TypeTableEntryIdBlock:
8059 case TypeTableEntryIdNullLit:8032 case TypeTableEntryIdNullLit:
8033 case TypeTableEntryIdOpaque:
8060 ir_add_error_node(ira, source_node,8034 ir_add_error_node(ira, source_node,
8061 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));8035 buf_sprintf("variable of type '%s' not allowed", buf_ptr(&result_type->name)));
8062 result_type = ira->codegen->builtin_types.entry_invalid;8036 result_type = ira->codegen->builtin_types.entry_invalid;
...@@ -8670,14 +8644,11 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct...@@ -8670,14 +8644,11 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
8670 IrInstruction *value = un_op_instruction->value->other;8644 IrInstruction *value = un_op_instruction->value->other;
86718645
8672 TypeTableEntry *meta_type = ir_resolve_type(ira, value);8646 TypeTableEntry *meta_type = ir_resolve_type(ira, value);
8673 TypeTableEntry *underlying_meta_type = get_underlying_type(meta_type);8647 if (type_is_invalid(meta_type))
8674
8675 if (type_is_invalid(underlying_meta_type))
8676 return ira->codegen->builtin_types.entry_invalid;8648 return ira->codegen->builtin_types.entry_invalid;
86778649
86788650
8679 switch (underlying_meta_type->id) {8651 switch (meta_type->id) {
8680 case TypeTableEntryIdTypeDecl:
8681 case TypeTableEntryIdInvalid: // handled above8652 case TypeTableEntryIdInvalid: // handled above
8682 zig_unreachable();8653 zig_unreachable();
86838654
...@@ -8712,9 +8683,9 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct...@@ -8712,9 +8683,9 @@ static TypeTableEntry *ir_analyze_unary_prefix_op_err(IrAnalyze *ira, IrInstruct
8712 case TypeTableEntryIdUnreachable:8683 case TypeTableEntryIdUnreachable:
8713 case TypeTableEntryIdVar:8684 case TypeTableEntryIdVar:
8714 case TypeTableEntryIdArgTuple:8685 case TypeTableEntryIdArgTuple:
8686 case TypeTableEntryIdOpaque:
8715 ir_add_error_node(ira, un_op_instruction->base.source_node,8687 ir_add_error_node(ira, un_op_instruction->base.source_node,
8716 buf_sprintf("unable to wrap type '%s' in error type", buf_ptr(&meta_type->name)));8688 buf_sprintf("unable to wrap type '%s' in error type", buf_ptr(&meta_type->name)));
8717 // TODO if meta_type is type decl, add note pointing to type decl declaration
8718 return ira->codegen->builtin_types.entry_invalid;8689 return ira->codegen->builtin_types.entry_invalid;
8719 }8690 }
8720 zig_unreachable();8691 zig_unreachable();
...@@ -8756,13 +8727,11 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp...@@ -8756,13 +8727,11 @@ static TypeTableEntry *ir_analyze_dereference(IrAnalyze *ira, IrInstructionUnOp
8756static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {8727static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op_instruction) {
8757 IrInstruction *value = un_op_instruction->value->other;8728 IrInstruction *value = un_op_instruction->value->other;
8758 TypeTableEntry *type_entry = ir_resolve_type(ira, value);8729 TypeTableEntry *type_entry = ir_resolve_type(ira, value);
8759 TypeTableEntry *canon_type = get_underlying_type(type_entry);8730 if (type_is_invalid(type_entry))
8760 if (type_is_invalid(canon_type))
8761 return ira->codegen->builtin_types.entry_invalid;8731 return ira->codegen->builtin_types.entry_invalid;
8762 switch (canon_type->id) {8732 switch (type_entry->id) {
8763 case TypeTableEntryIdInvalid:8733 case TypeTableEntryIdInvalid:
8764 case TypeTableEntryIdVar:8734 case TypeTableEntryIdVar:
8765 case TypeTableEntryIdTypeDecl:
8766 zig_unreachable();8735 zig_unreachable();
8767 case TypeTableEntryIdMetaType:8736 case TypeTableEntryIdMetaType:
8768 case TypeTableEntryIdVoid:8737 case TypeTableEntryIdVoid:
...@@ -8793,9 +8762,9 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op...@@ -8793,9 +8762,9 @@ static TypeTableEntry *ir_analyze_maybe(IrAnalyze *ira, IrInstructionUnOp *un_op
8793 return ira->codegen->builtin_types.entry_type;8762 return ira->codegen->builtin_types.entry_type;
8794 }8763 }
8795 case TypeTableEntryIdUnreachable:8764 case TypeTableEntryIdUnreachable:
8765 case TypeTableEntryIdOpaque:
8796 ir_add_error_node(ira, un_op_instruction->base.source_node,8766 ir_add_error_node(ira, un_op_instruction->base.source_node,
8797 buf_sprintf("type '%s' not nullable", buf_ptr(&type_entry->name)));8767 buf_sprintf("type '%s' not nullable", buf_ptr(&type_entry->name)));
8798 // TODO if it's a type decl, put an error note here pointing to the decl
8799 return ira->codegen->builtin_types.entry_invalid;8768 return ira->codegen->builtin_types.entry_invalid;
8800 }8769 }
8801 zig_unreachable();8770 zig_unreachable();
...@@ -9406,23 +9375,6 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source...@@ -9406,23 +9375,6 @@ static TypeTableEntry *ir_analyze_decl_ref(IrAnalyze *ira, IrInstruction *source
9406 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,9375 return ir_analyze_const_ptr(ira, source_instruction, const_val, fn_entry->type_entry,
9407 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);9376 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
9408 }9377 }
9409 case TldIdTypeDef:
9410 {
9411 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
9412 assert(tld_typedef->type_entry);
9413
9414 // TODO instead of allocating this every time, put it in the tld value and we can reference
9415 // the same one every time
9416 ConstExprValue *const_val = allocate<ConstExprValue>(1);
9417 const_val->special = ConstValSpecialStatic;
9418 const_val->type = ira->codegen->builtin_types.entry_type;
9419 const_val->data.x_type = tld_typedef->type_entry;
9420
9421 bool ptr_is_const = true;
9422 bool ptr_is_volatile = false;
9423 return ir_analyze_const_ptr(ira, source_instruction, const_val, ira->codegen->builtin_types.entry_type,
9424 ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
9425 }
9426 }9378 }
9427 zig_unreachable();9379 zig_unreachable();
9428}9380}
...@@ -9726,9 +9678,9 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi...@@ -9726,9 +9678,9 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
9726 case TypeTableEntryIdEnum:9678 case TypeTableEntryIdEnum:
9727 case TypeTableEntryIdUnion:9679 case TypeTableEntryIdUnion:
9728 case TypeTableEntryIdFn:9680 case TypeTableEntryIdFn:
9729 case TypeTableEntryIdTypeDecl:
9730 case TypeTableEntryIdEnumTag:9681 case TypeTableEntryIdEnumTag:
9731 case TypeTableEntryIdArgTuple:9682 case TypeTableEntryIdArgTuple:
9683 case TypeTableEntryIdOpaque:
9732 {9684 {
9733 ConstExprValue *out_val = ir_build_const_from(ira, &typeof_instruction->base);9685 ConstExprValue *out_val = ir_build_const_from(ira, &typeof_instruction->base);
9734 out_val->data.x_type = type_entry;9686 out_val->data.x_type = type_entry;
...@@ -9990,12 +9942,10 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -9990,12 +9942,10 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
9990 bool is_const = slice_type_instruction->is_const;9942 bool is_const = slice_type_instruction->is_const;
99919943
9992 TypeTableEntry *resolved_child_type = ir_resolve_type(ira, child_type);9944 TypeTableEntry *resolved_child_type = ir_resolve_type(ira, child_type);
9993 TypeTableEntry *canon_child_type = get_underlying_type(resolved_child_type);9945 if (type_is_invalid(resolved_child_type))
9994 if (type_is_invalid(canon_child_type))
9995 return ira->codegen->builtin_types.entry_invalid;9946 return ira->codegen->builtin_types.entry_invalid;
99969947
9997 switch (canon_child_type->id) {9948 switch (resolved_child_type->id) {
9998 case TypeTableEntryIdTypeDecl:
9999 case TypeTableEntryIdInvalid: // handled above9949 case TypeTableEntryIdInvalid: // handled above
10000 zig_unreachable();9950 zig_unreachable();
10001 case TypeTableEntryIdVar:9951 case TypeTableEntryIdVar:
...@@ -10004,9 +9954,9 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,...@@ -10004,9 +9954,9 @@ static TypeTableEntry *ir_analyze_instruction_slice_type(IrAnalyze *ira,
10004 case TypeTableEntryIdNullLit:9954 case TypeTableEntryIdNullLit:
10005 case TypeTableEntryIdBlock:9955 case TypeTableEntryIdBlock:
10006 case TypeTableEntryIdArgTuple:9956 case TypeTableEntryIdArgTuple:
9957 case TypeTableEntryIdOpaque:
10007 ir_add_error_node(ira, slice_type_instruction->base.source_node,9958 ir_add_error_node(ira, slice_type_instruction->base.source_node,
10008 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&resolved_child_type->name)));9959 buf_sprintf("slice of type '%s' not allowed", buf_ptr(&resolved_child_type->name)));
10009 // TODO if this is a typedecl, add error note showing the declaration of the type decl
10010 return ira->codegen->builtin_types.entry_invalid;9960 return ira->codegen->builtin_types.entry_invalid;
10011 case TypeTableEntryIdMetaType:9961 case TypeTableEntryIdMetaType:
10012 case TypeTableEntryIdVoid:9962 case TypeTableEntryIdVoid:
...@@ -10100,11 +10050,9 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -10100,11 +10050,9 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
1010010050
10101 IrInstruction *child_type_value = array_type_instruction->child_type->other;10051 IrInstruction *child_type_value = array_type_instruction->child_type->other;
10102 TypeTableEntry *child_type = ir_resolve_type(ira, child_type_value);10052 TypeTableEntry *child_type = ir_resolve_type(ira, child_type_value);
10103 TypeTableEntry *canon_child_type = get_underlying_type(child_type);10053 if (type_is_invalid(child_type))
10104 if (type_is_invalid(canon_child_type))
10105 return ira->codegen->builtin_types.entry_invalid;10054 return ira->codegen->builtin_types.entry_invalid;
10106 switch (canon_child_type->id) {10055 switch (child_type->id) {
10107 case TypeTableEntryIdTypeDecl:
10108 case TypeTableEntryIdInvalid: // handled above10056 case TypeTableEntryIdInvalid: // handled above
10109 zig_unreachable();10057 zig_unreachable();
10110 case TypeTableEntryIdVar:10058 case TypeTableEntryIdVar:
...@@ -10113,9 +10061,9 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -10113,9 +10061,9 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
10113 case TypeTableEntryIdNullLit:10061 case TypeTableEntryIdNullLit:
10114 case TypeTableEntryIdBlock:10062 case TypeTableEntryIdBlock:
10115 case TypeTableEntryIdArgTuple:10063 case TypeTableEntryIdArgTuple:
10064 case TypeTableEntryIdOpaque:
10116 ir_add_error_node(ira, array_type_instruction->base.source_node,10065 ir_add_error_node(ira, array_type_instruction->base.source_node,
10117 buf_sprintf("array of type '%s' not allowed", buf_ptr(&child_type->name)));10066 buf_sprintf("array of type '%s' not allowed", buf_ptr(&child_type->name)));
10118 // TODO if this is a typedecl, add error note showing the declaration of the type decl
10119 return ira->codegen->builtin_types.entry_invalid;10067 return ira->codegen->builtin_types.entry_invalid;
10120 case TypeTableEntryIdMetaType:10068 case TypeTableEntryIdMetaType:
10121 case TypeTableEntryIdVoid:10069 case TypeTableEntryIdVoid:
...@@ -10172,15 +10120,13 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,...@@ -10172,15 +10120,13 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
10172{10120{
10173 IrInstruction *type_value = size_of_instruction->type_value->other;10121 IrInstruction *type_value = size_of_instruction->type_value->other;
10174 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);10122 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
10175 TypeTableEntry *canon_type_entry = get_underlying_type(type_entry);
1017610123
10177 ensure_complete_type(ira->codegen, type_entry);10124 ensure_complete_type(ira->codegen, type_entry);
10178 if (type_is_invalid(canon_type_entry))10125 if (type_is_invalid(type_entry))
10179 return ira->codegen->builtin_types.entry_invalid;10126 return ira->codegen->builtin_types.entry_invalid;
1018010127
10181 switch (canon_type_entry->id) {10128 switch (type_entry->id) {
10182 case TypeTableEntryIdInvalid: // handled above10129 case TypeTableEntryIdInvalid: // handled above
10183 case TypeTableEntryIdTypeDecl:
10184 zig_unreachable();10130 zig_unreachable();
10185 case TypeTableEntryIdVar:10131 case TypeTableEntryIdVar:
10186 case TypeTableEntryIdUnreachable:10132 case TypeTableEntryIdUnreachable:
...@@ -10193,9 +10139,9 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,...@@ -10193,9 +10139,9 @@ static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
10193 case TypeTableEntryIdMetaType:10139 case TypeTableEntryIdMetaType:
10194 case TypeTableEntryIdNamespace:10140 case TypeTableEntryIdNamespace:
10195 case TypeTableEntryIdArgTuple:10141 case TypeTableEntryIdArgTuple:
10142 case TypeTableEntryIdOpaque:
10196 ir_add_error_node(ira, size_of_instruction->base.source_node,10143 ir_add_error_node(ira, size_of_instruction->base.source_node,
10197 buf_sprintf("no size available for type '%s'", buf_ptr(&type_entry->name)));10144 buf_sprintf("no size available for type '%s'", buf_ptr(&type_entry->name)));
10198 // TODO if this is a typedecl, add error note showing the declaration of the type decl
10199 return ira->codegen->builtin_types.entry_invalid;10145 return ira->codegen->builtin_types.entry_invalid;
10200 case TypeTableEntryIdVoid:10146 case TypeTableEntryIdVoid:
10201 case TypeTableEntryIdBool:10147 case TypeTableEntryIdBool:
...@@ -10516,15 +10462,13 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -10516,15 +10462,13 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
10516 if (pointee_val->special == ConstValSpecialRuntime)10462 if (pointee_val->special == ConstValSpecialRuntime)
10517 pointee_val = nullptr;10463 pointee_val = nullptr;
10518 }10464 }
10519 TypeTableEntry *canon_target_type = get_underlying_type(target_type);
10520 ensure_complete_type(ira->codegen, target_type);10465 ensure_complete_type(ira->codegen, target_type);
10521 if (type_is_invalid(canon_target_type))10466 if (type_is_invalid(target_type))
10522 return ira->codegen->builtin_types.entry_invalid;10467 return ira->codegen->builtin_types.entry_invalid;
1052310468
10524 switch (canon_target_type->id) {10469 switch (target_type->id) {
10525 case TypeTableEntryIdInvalid:10470 case TypeTableEntryIdInvalid:
10526 case TypeTableEntryIdVar:10471 case TypeTableEntryIdVar:
10527 case TypeTableEntryIdTypeDecl:
10528 zig_unreachable();10472 zig_unreachable();
10529 case TypeTableEntryIdMetaType:10473 case TypeTableEntryIdMetaType:
10530 case TypeTableEntryIdVoid:10474 case TypeTableEntryIdVoid:
...@@ -10576,9 +10520,9 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,...@@ -10576,9 +10520,9 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
10576 case TypeTableEntryIdBlock:10520 case TypeTableEntryIdBlock:
10577 case TypeTableEntryIdBoundFn:10521 case TypeTableEntryIdBoundFn:
10578 case TypeTableEntryIdArgTuple:10522 case TypeTableEntryIdArgTuple:
10523 case TypeTableEntryIdOpaque:
10579 ir_add_error(ira, &switch_target_instruction->base,10524 ir_add_error(ira, &switch_target_instruction->base,
10580 buf_sprintf("invalid switch target type '%s'", buf_ptr(&target_type->name)));10525 buf_sprintf("invalid switch target type '%s'", buf_ptr(&target_type->name)));
10581 // TODO if this is a typedecl, add error note showing the declaration of the type decl
10582 return ira->codegen->builtin_types.entry_invalid;10526 return ira->codegen->builtin_types.entry_invalid;
10583 }10527 }
10584 zig_unreachable();10528 zig_unreachable();
...@@ -10630,9 +10574,8 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr...@@ -10630,9 +10574,8 @@ static TypeTableEntry *ir_analyze_instruction_switch_var(IrAnalyze *ira, IrInstr
10630 return get_pointer_to_type(ira->codegen, field->type_entry,10574 return get_pointer_to_type(ira->codegen, field->type_entry,
10631 target_value_ptr->value.type->data.pointer.is_const);10575 target_value_ptr->value.type->data.pointer.is_const);
10632 } else {10576 } else {
10633 ErrorMsg *msg = ir_add_error(ira, &instruction->base,10577 ir_add_error(ira, &instruction->base,
10634 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));10578 buf_sprintf("switch on type '%s' provides no expression parameter", buf_ptr(&target_type->name)));
10635 ir_add_typedef_err_note(ira, msg, target_type);
10636 return ira->codegen->builtin_types.entry_invalid;10579 return ira->codegen->builtin_types.entry_invalid;
10637 }10580 }
10638}10581}
...@@ -10743,13 +10686,13 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,...@@ -10743,13 +10686,13 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
10743 IrInstructionArrayLen *array_len_instruction)10686 IrInstructionArrayLen *array_len_instruction)
10744{10687{
10745 IrInstruction *array_value = array_len_instruction->array_value->other;10688 IrInstruction *array_value = array_len_instruction->array_value->other;
10746 TypeTableEntry *canon_type = get_underlying_type(array_value->value.type);10689 TypeTableEntry *type_entry = array_value->value.type;
10747 if (type_is_invalid(canon_type)) {10690 if (type_is_invalid(type_entry)) {
10748 return ira->codegen->builtin_types.entry_invalid;10691 return ira->codegen->builtin_types.entry_invalid;
10749 } else if (canon_type->id == TypeTableEntryIdArray) {10692 } else if (type_entry->id == TypeTableEntryIdArray) {
10750 return ir_analyze_const_usize(ira, &array_len_instruction->base,10693 return ir_analyze_const_usize(ira, &array_len_instruction->base,
10751 canon_type->data.array.len);10694 type_entry->data.array.len);
10752 } else if (is_slice(canon_type)) {10695 } else if (is_slice(type_entry)) {
10753 if (array_value->value.special != ConstValSpecialRuntime) {10696 if (array_value->value.special != ConstValSpecialRuntime) {
10754 ConstExprValue *len_val = &array_value->value.data.x_struct.fields[slice_len_index];10697 ConstExprValue *len_val = &array_value->value.data.x_struct.fields[slice_len_index];
10755 if (len_val->special != ConstValSpecialRuntime) {10698 if (len_val->special != ConstValSpecialRuntime) {
...@@ -10757,7 +10700,7 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,...@@ -10757,7 +10700,7 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
10757 len_val->data.x_bignum.data.x_uint);10700 len_val->data.x_bignum.data.x_uint);
10758 }10701 }
10759 }10702 }
10760 TypeStructField *field = &canon_type->data.structure.fields[slice_len_index];10703 TypeStructField *field = &type_entry->data.structure.fields[slice_len_index];
10761 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,10704 IrInstruction *len_ptr = ir_build_struct_field_ptr(&ira->new_irb, array_len_instruction->base.scope,
10762 array_len_instruction->base.source_node, array_value, field);10705 array_len_instruction->base.source_node, array_value, field);
10763 len_ptr->value.type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);10706 len_ptr->value.type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_usize, true);
...@@ -10766,7 +10709,6 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,...@@ -10766,7 +10709,6 @@ static TypeTableEntry *ir_analyze_instruction_array_len(IrAnalyze *ira,
10766 } else {10709 } else {
10767 ir_add_error_node(ira, array_len_instruction->base.source_node,10710 ir_add_error_node(ira, array_len_instruction->base.source_node,
10768 buf_sprintf("type '%s' has no field 'len'", buf_ptr(&array_value->value.type->name)));10711 buf_sprintf("type '%s' has no field 'len'", buf_ptr(&array_value->value.type->name)));
10769 // TODO if this is a typedecl, add error note showing the declaration of the type decl
10770 return ira->codegen->builtin_types.entry_invalid;10712 return ira->codegen->builtin_types.entry_invalid;
10771 }10713 }
10772}10714}
...@@ -11049,29 +10991,28 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_...@@ -11049,29 +10991,28 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
11049 IrInstruction *target_type_value, bool is_max)10991 IrInstruction *target_type_value, bool is_max)
11050{10992{
11051 TypeTableEntry *target_type = ir_resolve_type(ira, target_type_value);10993 TypeTableEntry *target_type = ir_resolve_type(ira, target_type_value);
11052 TypeTableEntry *canon_type = get_underlying_type(target_type);10994 if (type_is_invalid(target_type))
11053 if (type_is_invalid(canon_type))
11054 return ira->codegen->builtin_types.entry_invalid;10995 return ira->codegen->builtin_types.entry_invalid;
11055 switch (canon_type->id) {10996 switch (target_type->id) {
11056 case TypeTableEntryIdInvalid:10997 case TypeTableEntryIdInvalid:
11057 zig_unreachable();10998 zig_unreachable();
11058 case TypeTableEntryIdInt:10999 case TypeTableEntryIdInt:
11059 {11000 {
11060 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);11001 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);
11061 eval_min_max_value(ira->codegen, canon_type, out_val, is_max);11002 eval_min_max_value(ira->codegen, target_type, out_val, is_max);
11062 return ira->codegen->builtin_types.entry_num_lit_int;11003 return ira->codegen->builtin_types.entry_num_lit_int;
11063 }11004 }
11064 case TypeTableEntryIdFloat:11005 case TypeTableEntryIdFloat:
11065 {11006 {
11066 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);11007 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);
11067 eval_min_max_value(ira->codegen, canon_type, out_val, is_max);11008 eval_min_max_value(ira->codegen, target_type, out_val, is_max);
11068 return ira->codegen->builtin_types.entry_num_lit_float;11009 return ira->codegen->builtin_types.entry_num_lit_float;
11069 }11010 }
11070 case TypeTableEntryIdBool:11011 case TypeTableEntryIdBool:
11071 case TypeTableEntryIdVoid:11012 case TypeTableEntryIdVoid:
11072 {11013 {
11073 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);11014 ConstExprValue *out_val = ir_build_const_from(ira, source_instruction);
11074 eval_min_max_value(ira->codegen, canon_type, out_val, is_max);11015 eval_min_max_value(ira->codegen, target_type, out_val, is_max);
11075 return target_type;11016 return target_type;
11076 }11017 }
11077 case TypeTableEntryIdEnumTag:11018 case TypeTableEntryIdEnumTag:
...@@ -11092,18 +11033,17 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_...@@ -11092,18 +11033,17 @@ static TypeTableEntry *ir_analyze_min_max(IrAnalyze *ira, IrInstruction *source_
11092 case TypeTableEntryIdEnum:11033 case TypeTableEntryIdEnum:
11093 case TypeTableEntryIdUnion:11034 case TypeTableEntryIdUnion:
11094 case TypeTableEntryIdFn:11035 case TypeTableEntryIdFn:
11095 case TypeTableEntryIdTypeDecl:
11096 case TypeTableEntryIdNamespace:11036 case TypeTableEntryIdNamespace:
11097 case TypeTableEntryIdBlock:11037 case TypeTableEntryIdBlock:
11098 case TypeTableEntryIdBoundFn:11038 case TypeTableEntryIdBoundFn:
11099 case TypeTableEntryIdArgTuple:11039 case TypeTableEntryIdArgTuple:
11040 case TypeTableEntryIdOpaque:
11100 {11041 {
11101 const char *err_format = is_max ?11042 const char *err_format = is_max ?
11102 "no max value available for type '%s'" :11043 "no max value available for type '%s'" :
11103 "no min value available for type '%s'";11044 "no min value available for type '%s'";
11104 ir_add_error(ira, source_instruction,11045 ir_add_error(ira, source_instruction,
11105 buf_sprintf(err_format, buf_ptr(&target_type->name)));11046 buf_sprintf(err_format, buf_ptr(&target_type->name)));
11106 // TODO if this is a typedecl, add error note showing the declaration of the type decl
11107 return ira->codegen->builtin_types.entry_invalid;11047 return ira->codegen->builtin_types.entry_invalid;
11108 }11048 }
11109 }11049 }
...@@ -11508,14 +11448,11 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru...@@ -11508,14 +11448,11 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru
11508 if (type_is_invalid(result_type))11448 if (type_is_invalid(result_type))
11509 return ira->codegen->builtin_types.entry_invalid;11449 return ira->codegen->builtin_types.entry_invalid;
1151011450
11511 TypeTableEntry *canon_type = get_underlying_type(result_type);11451 if (result_type->id != TypeTableEntryIdInt &&
1151211452 result_type->id != TypeTableEntryIdNumLitInt)
11513 if (canon_type->id != TypeTableEntryIdInt &&
11514 canon_type->id != TypeTableEntryIdNumLitInt)
11515 {11453 {
11516 ir_add_error(ira, &instruction->base,11454 ir_add_error(ira, &instruction->base,
11517 buf_sprintf("expected integer type, found '%s'", buf_ptr(&result_type->name)));11455 buf_sprintf("expected integer type, found '%s'", buf_ptr(&result_type->name)));
11518 // TODO if meta_type is type decl, add note pointing to type decl declaration
11519 return ira->codegen->builtin_types.entry_invalid;11456 return ira->codegen->builtin_types.entry_invalid;
11520 }11457 }
1152111458
...@@ -11563,49 +11500,42 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru...@@ -11563,49 +11500,42 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru
11563static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstructionTruncate *instruction) {11500static TypeTableEntry *ir_analyze_instruction_truncate(IrAnalyze *ira, IrInstructionTruncate *instruction) {
11564 IrInstruction *dest_type_value = instruction->dest_type->other;11501 IrInstruction *dest_type_value = instruction->dest_type->other;
11565 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);11502 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
11566 TypeTableEntry *canon_dest_type = get_underlying_type(dest_type);11503 if (type_is_invalid(dest_type))
11567
11568 if (type_is_invalid(canon_dest_type))
11569 return ira->codegen->builtin_types.entry_invalid;11504 return ira->codegen->builtin_types.entry_invalid;
1157011505
11571 if (canon_dest_type->id != TypeTableEntryIdInt &&11506 if (dest_type->id != TypeTableEntryIdInt &&
11572 canon_dest_type->id != TypeTableEntryIdNumLitInt)11507 dest_type->id != TypeTableEntryIdNumLitInt)
11573 {11508 {
11574 ir_add_error(ira, dest_type_value, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));11509 ir_add_error(ira, dest_type_value, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
11575 // TODO if meta_type is type decl, add note pointing to type decl declaration
11576 return ira->codegen->builtin_types.entry_invalid;11510 return ira->codegen->builtin_types.entry_invalid;
11577 }11511 }
1157811512
11579 IrInstruction *target = instruction->target->other;11513 IrInstruction *target = instruction->target->other;
11580 TypeTableEntry *src_type = target->value.type;11514 TypeTableEntry *src_type = target->value.type;
11581 TypeTableEntry *canon_src_type = get_underlying_type(src_type);11515 if (type_is_invalid(src_type))
11582 if (type_is_invalid(canon_src_type))
11583 return ira->codegen->builtin_types.entry_invalid;11516 return ira->codegen->builtin_types.entry_invalid;
1158411517
11585 if (canon_src_type->id != TypeTableEntryIdInt &&11518 if (src_type->id != TypeTableEntryIdInt &&
11586 canon_src_type->id != TypeTableEntryIdNumLitInt)11519 src_type->id != TypeTableEntryIdNumLitInt)
11587 {11520 {
11588 ir_add_error(ira, target, buf_sprintf("expected integer type, found '%s'", buf_ptr(&src_type->name)));11521 ir_add_error(ira, target, buf_sprintf("expected integer type, found '%s'", buf_ptr(&src_type->name)));
11589 // TODO if meta_type is type decl, add note pointing to type decl declaration
11590 return ira->codegen->builtin_types.entry_invalid;11522 return ira->codegen->builtin_types.entry_invalid;
11591 }11523 }
1159211524
11593 if (canon_src_type->data.integral.is_signed != canon_dest_type->data.integral.is_signed) {11525 if (src_type->data.integral.is_signed != dest_type->data.integral.is_signed) {
11594 const char *sign_str = canon_dest_type->data.integral.is_signed ? "signed" : "unsigned";11526 const char *sign_str = dest_type->data.integral.is_signed ? "signed" : "unsigned";
11595 ir_add_error(ira, target, buf_sprintf("expected %s integer type, found '%s'", sign_str, buf_ptr(&src_type->name)));11527 ir_add_error(ira, target, buf_sprintf("expected %s integer type, found '%s'", sign_str, buf_ptr(&src_type->name)));
11596 // TODO if meta_type is type decl, add note pointing to type decl declaration
11597 return ira->codegen->builtin_types.entry_invalid;11528 return ira->codegen->builtin_types.entry_invalid;
11598 } else if (canon_src_type->data.integral.bit_count < canon_dest_type->data.integral.bit_count) {11529 } else if (src_type->data.integral.bit_count < dest_type->data.integral.bit_count) {
11599 ir_add_error(ira, target, buf_sprintf("type '%s' has fewer bits than destination type '%s'",11530 ir_add_error(ira, target, buf_sprintf("type '%s' has fewer bits than destination type '%s'",
11600 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));11531 buf_ptr(&src_type->name), buf_ptr(&dest_type->name)));
11601 // TODO if meta_type is type decl, add note pointing to type decl declaration
11602 return ira->codegen->builtin_types.entry_invalid;11532 return ira->codegen->builtin_types.entry_invalid;
11603 }11533 }
1160411534
11605 if (target->value.special == ConstValSpecialStatic) {11535 if (target->value.special == ConstValSpecialStatic) {
11606 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);11536 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
11607 bignum_init_bignum(&out_val->data.x_bignum, &target->value.data.x_bignum);11537 bignum_init_bignum(&out_val->data.x_bignum, &target->value.data.x_bignum);
11608 bignum_truncate(&out_val->data.x_bignum, canon_dest_type->data.integral.bit_count);11538 bignum_truncate(&out_val->data.x_bignum, dest_type->data.integral.bit_count);
11609 return dest_type;11539 return dest_type;
11610 }11540 }
1161111541
...@@ -11663,7 +11593,7 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi...@@ -11663,7 +11593,7 @@ static TypeTableEntry *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructi
11663 if (type_is_invalid(count_value->value.type))11593 if (type_is_invalid(count_value->value.type))
11664 return ira->codegen->builtin_types.entry_invalid;11594 return ira->codegen->builtin_types.entry_invalid;
1166511595
11666 TypeTableEntry *dest_uncasted_type = get_underlying_type(dest_ptr->value.type);11596 TypeTableEntry *dest_uncasted_type = dest_ptr->value.type;
11667 bool dest_is_volatile = (dest_uncasted_type->id == TypeTableEntryIdPointer) &&11597 bool dest_is_volatile = (dest_uncasted_type->id == TypeTableEntryIdPointer) &&
11668 dest_uncasted_type->data.pointer.is_volatile;11598 dest_uncasted_type->data.pointer.is_volatile;
1166911599
...@@ -11749,8 +11679,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi...@@ -11749,8 +11679,8 @@ static TypeTableEntry *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructi
11749 if (type_is_invalid(count_value->value.type))11679 if (type_is_invalid(count_value->value.type))
11750 return ira->codegen->builtin_types.entry_invalid;11680 return ira->codegen->builtin_types.entry_invalid;
1175111681
11752 TypeTableEntry *dest_uncasted_type = get_underlying_type(dest_ptr->value.type);11682 TypeTableEntry *dest_uncasted_type = dest_ptr->value.type;
11753 TypeTableEntry *src_uncasted_type = get_underlying_type(src_ptr->value.type);11683 TypeTableEntry *src_uncasted_type = src_ptr->value.type;
11754 bool dest_is_volatile = (dest_uncasted_type->id == TypeTableEntryIdPointer) &&11684 bool dest_is_volatile = (dest_uncasted_type->id == TypeTableEntryIdPointer) &&
11755 dest_uncasted_type->data.pointer.is_volatile;11685 dest_uncasted_type->data.pointer.is_volatile;
11756 bool src_is_volatile = (src_uncasted_type->id == TypeTableEntryIdPointer) &&11686 bool src_is_volatile = (src_uncasted_type->id == TypeTableEntryIdPointer) &&
...@@ -11866,8 +11796,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -11866,8 +11796,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1186611796
11867 TypeTableEntry *ptr_type = ptr_ptr->value.type;11797 TypeTableEntry *ptr_type = ptr_ptr->value.type;
11868 assert(ptr_type->id == TypeTableEntryIdPointer);11798 assert(ptr_type->id == TypeTableEntryIdPointer);
11869 TypeTableEntry *non_canon_array_type = ptr_type->data.pointer.child_type;11799 TypeTableEntry *array_type = ptr_type->data.pointer.child_type;
11870 TypeTableEntry *canon_array_type = get_underlying_type(non_canon_array_type);
1187111800
11872 IrInstruction *start = instruction->start->other;11801 IrInstruction *start = instruction->start->other;
11873 if (type_is_invalid(start->value.type))11802 if (type_is_invalid(start->value.type))
...@@ -11892,22 +11821,21 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -11892,22 +11821,21 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
1189211821
11893 TypeTableEntry *return_type;11822 TypeTableEntry *return_type;
1189411823
11895 if (canon_array_type->id == TypeTableEntryIdArray) {11824 if (array_type->id == TypeTableEntryIdArray) {
11896 return_type = get_slice_type(ira->codegen, canon_array_type->data.array.child_type, instruction->is_const);11825 return_type = get_slice_type(ira->codegen, array_type->data.array.child_type, instruction->is_const);
11897 } else if (canon_array_type->id == TypeTableEntryIdPointer) {11826 } else if (array_type->id == TypeTableEntryIdPointer) {
11898 return_type = get_slice_type(ira->codegen, canon_array_type->data.pointer.child_type, instruction->is_const);11827 return_type = get_slice_type(ira->codegen, array_type->data.pointer.child_type, instruction->is_const);
11899 if (!end) {11828 if (!end) {
11900 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));11829 ir_add_error(ira, &instruction->base, buf_sprintf("slice of pointer must include end value"));
11901 return ira->codegen->builtin_types.entry_invalid;11830 return ira->codegen->builtin_types.entry_invalid;
11902 }11831 }
11903 } else if (is_slice(canon_array_type)) {11832 } else if (is_slice(array_type)) {
11904 return_type = get_slice_type(ira->codegen,11833 return_type = get_slice_type(ira->codegen,
11905 canon_array_type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.child_type,11834 array_type->data.structure.fields[slice_ptr_index].type_entry->data.pointer.child_type,
11906 instruction->is_const);11835 instruction->is_const);
11907 } else {11836 } else {
11908 ir_add_error(ira, &instruction->base,11837 ir_add_error(ira, &instruction->base,
11909 buf_sprintf("slice of non-array type '%s'", buf_ptr(&non_canon_array_type->name)));11838 buf_sprintf("slice of non-array type '%s'", buf_ptr(&array_type->name)));
11910 // TODO if this is a typedecl, add error note showing the declaration of the type decl
11911 return ira->codegen->builtin_types.entry_invalid;11839 return ira->codegen->builtin_types.entry_invalid;
11912 }11840 }
1191311841
...@@ -11919,12 +11847,12 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -11919,12 +11847,12 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
11919 ConstExprValue *parent_ptr;11847 ConstExprValue *parent_ptr;
11920 size_t abs_offset;11848 size_t abs_offset;
11921 size_t rel_end;11849 size_t rel_end;
11922 if (canon_array_type->id == TypeTableEntryIdArray) {11850 if (array_type->id == TypeTableEntryIdArray) {
11923 array_val = const_ptr_pointee(&ptr_ptr->value);11851 array_val = const_ptr_pointee(&ptr_ptr->value);
11924 abs_offset = 0;11852 abs_offset = 0;
11925 rel_end = canon_array_type->data.array.len;11853 rel_end = array_type->data.array.len;
11926 parent_ptr = nullptr;11854 parent_ptr = nullptr;
11927 } else if (canon_array_type->id == TypeTableEntryIdPointer) {11855 } else if (array_type->id == TypeTableEntryIdPointer) {
11928 parent_ptr = const_ptr_pointee(&ptr_ptr->value);11856 parent_ptr = const_ptr_pointee(&ptr_ptr->value);
11929 switch (parent_ptr->data.x_ptr.special) {11857 switch (parent_ptr->data.x_ptr.special) {
11930 case ConstPtrSpecialInvalid:11858 case ConstPtrSpecialInvalid:
...@@ -11946,7 +11874,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -11946,7 +11874,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
11946 array_val = nullptr;11874 array_val = nullptr;
11947 break;11875 break;
11948 }11876 }
11949 } else if (is_slice(canon_array_type)) {11877 } else if (is_slice(array_type)) {
11950 ConstExprValue *slice_ptr = const_ptr_pointee(&ptr_ptr->value);11878 ConstExprValue *slice_ptr = const_ptr_pointee(&ptr_ptr->value);
11951 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];11879 parent_ptr = &slice_ptr->data.x_struct.fields[slice_ptr_index];
11952 ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];11880 ConstExprValue *len_val = &slice_ptr->data.x_struct.fields[slice_len_index];
...@@ -12005,7 +11933,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio...@@ -12005,7 +11933,7 @@ static TypeTableEntry *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstructio
12005 if (array_val) {11933 if (array_val) {
12006 size_t index = abs_offset + start_scalar;11934 size_t index = abs_offset + start_scalar;
12007 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, instruction->is_const);11935 init_const_ptr_array(ira->codegen, ptr_val, array_val, index, instruction->is_const);
12008 if (canon_array_type->id == TypeTableEntryIdArray) {11936 if (array_type->id == TypeTableEntryIdArray) {
12009 ptr_val->data.x_ptr.mut = ptr_ptr->value.data.x_ptr.mut;11937 ptr_val->data.x_ptr.mut = ptr_ptr->value.data.x_ptr.mut;
12010 }11938 }
12011 } else {11939 } else {
...@@ -12045,17 +11973,16 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns...@@ -12045,17 +11973,16 @@ static TypeTableEntry *ir_analyze_instruction_member_count(IrAnalyze *ira, IrIns
12045 if (type_is_invalid(container->value.type))11973 if (type_is_invalid(container->value.type))
12046 return ira->codegen->builtin_types.entry_invalid;11974 return ira->codegen->builtin_types.entry_invalid;
12047 TypeTableEntry *container_type = ir_resolve_type(ira, container);11975 TypeTableEntry *container_type = ir_resolve_type(ira, container);
12048 TypeTableEntry *canon_type = get_underlying_type(container_type);
1204911976
12050 uint64_t result;11977 uint64_t result;
12051 if (type_is_invalid(canon_type)) {11978 if (type_is_invalid(container_type)) {
12052 return ira->codegen->builtin_types.entry_invalid;11979 return ira->codegen->builtin_types.entry_invalid;
12053 } else if (canon_type->id == TypeTableEntryIdEnum) {11980 } else if (container_type->id == TypeTableEntryIdEnum) {
12054 result = canon_type->data.enumeration.src_field_count;11981 result = container_type->data.enumeration.src_field_count;
12055 } else if (canon_type->id == TypeTableEntryIdStruct) {11982 } else if (container_type->id == TypeTableEntryIdStruct) {
12056 result = canon_type->data.structure.src_field_count;11983 result = container_type->data.structure.src_field_count;
12057 } else if (canon_type->id == TypeTableEntryIdUnion) {11984 } else if (container_type->id == TypeTableEntryIdUnion) {
12058 result = canon_type->data.unionation.src_field_count;11985 result = container_type->data.unionation.src_field_count;
12059 } else {11986 } else {
12060 ir_add_error(ira, &instruction->base, buf_sprintf("no value count available for type '%s'", buf_ptr(&container_type->name)));11987 ir_add_error(ira, &instruction->base, buf_sprintf("no value count available for type '%s'", buf_ptr(&container_type->name)));
12061 return ira->codegen->builtin_types.entry_invalid;11988 return ira->codegen->builtin_types.entry_invalid;
...@@ -12112,14 +12039,12 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst...@@ -12112,14 +12039,12 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst
12112 if (type_is_invalid(type_value->value.type))12039 if (type_is_invalid(type_value->value.type))
12113 return ira->codegen->builtin_types.entry_invalid;12040 return ira->codegen->builtin_types.entry_invalid;
12114 TypeTableEntry *dest_type = ir_resolve_type(ira, type_value);12041 TypeTableEntry *dest_type = ir_resolve_type(ira, type_value);
12115 TypeTableEntry *canon_type = get_underlying_type(dest_type);12042 if (type_is_invalid(dest_type))
12116 if (type_is_invalid(canon_type))
12117 return ira->codegen->builtin_types.entry_invalid;12043 return ira->codegen->builtin_types.entry_invalid;
1211812044
12119 if (canon_type->id != TypeTableEntryIdInt) {12045 if (dest_type->id != TypeTableEntryIdInt) {
12120 ir_add_error(ira, type_value,12046 ir_add_error(ira, type_value,
12121 buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));12047 buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
12122 // TODO if this is a typedecl, add error note showing the declaration of the type decl
12123 return ira->codegen->builtin_types.entry_invalid;12048 return ira->codegen->builtin_types.entry_invalid;
12124 }12049 }
1212512050
...@@ -12171,11 +12096,11 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst...@@ -12171,11 +12096,11 @@ static TypeTableEntry *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInst
12171 out_val->data.x_bool = bignum_shl(dest_bignum, op1_bignum, op2_bignum);12096 out_val->data.x_bool = bignum_shl(dest_bignum, op1_bignum, op2_bignum);
12172 break;12097 break;
12173 }12098 }
12174 if (!bignum_fits_in_bits(dest_bignum, canon_type->data.integral.bit_count,12099 if (!bignum_fits_in_bits(dest_bignum, dest_type->data.integral.bit_count,
12175 canon_type->data.integral.is_signed))12100 dest_type->data.integral.is_signed))
12176 {12101 {
12177 out_val->data.x_bool = true;12102 out_val->data.x_bool = true;
12178 bignum_truncate(dest_bignum, canon_type->data.integral.bit_count);12103 bignum_truncate(dest_bignum, dest_type->data.integral.bit_count);
12179 }12104 }
12180 pointee_val->special = ConstValSpecialStatic;12105 pointee_val->special = ConstValSpecialStatic;
12181 return ira->codegen->builtin_types.entry_bool;12106 return ira->codegen->builtin_types.entry_bool;
...@@ -12191,12 +12116,10 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc...@@ -12191,12 +12116,10 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc
12191 if (type_is_invalid(value->value.type))12116 if (type_is_invalid(value->value.type))
12192 return ira->codegen->builtin_types.entry_invalid;12117 return ira->codegen->builtin_types.entry_invalid;
1219312118
12194 TypeTableEntry *non_canon_type = value->value.type;12119 TypeTableEntry *type_entry = value->value.type;
1219512120 if (type_is_invalid(type_entry)) {
12196 TypeTableEntry *canon_type = get_underlying_type(non_canon_type);
12197 if (type_is_invalid(canon_type)) {
12198 return ira->codegen->builtin_types.entry_invalid;12121 return ira->codegen->builtin_types.entry_invalid;
12199 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {12122 } else if (type_entry->id == TypeTableEntryIdErrorUnion) {
12200 if (instr_is_comptime(value)) {12123 if (instr_is_comptime(value)) {
12201 ConstExprValue *err_union_val = ir_resolve_const(ira, value, UndefBad);12124 ConstExprValue *err_union_val = ir_resolve_const(ira, value, UndefBad);
12202 if (!err_union_val)12125 if (!err_union_val)
...@@ -12211,7 +12134,7 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc...@@ -12211,7 +12134,7 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc
1221112134
12212 ir_build_test_err_from(&ira->new_irb, &instruction->base, value);12135 ir_build_test_err_from(&ira->new_irb, &instruction->base, value);
12213 return ira->codegen->builtin_types.entry_bool;12136 return ira->codegen->builtin_types.entry_bool;
12214 } else if (canon_type->id == TypeTableEntryIdPureError) {12137 } else if (type_entry->id == TypeTableEntryIdPureError) {
12215 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);12138 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
12216 out_val->data.x_bool = true;12139 out_val->data.x_bool = true;
12217 return ira->codegen->builtin_types.entry_bool;12140 return ira->codegen->builtin_types.entry_bool;
...@@ -12233,11 +12156,10 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,...@@ -12233,11 +12156,10 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
12233 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.12156 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
12234 assert(ptr_type->id == TypeTableEntryIdPointer);12157 assert(ptr_type->id == TypeTableEntryIdPointer);
1223512158
12236 TypeTableEntry *non_canon_type = ptr_type->data.pointer.child_type;12159 TypeTableEntry *type_entry = ptr_type->data.pointer.child_type;
12237 TypeTableEntry *canon_type = get_underlying_type(non_canon_type);12160 if (type_is_invalid(type_entry)) {
12238 if (type_is_invalid(canon_type)) {
12239 return ira->codegen->builtin_types.entry_invalid;12161 return ira->codegen->builtin_types.entry_invalid;
12240 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {12162 } else if (type_entry->id == TypeTableEntryIdErrorUnion) {
12241 if (instr_is_comptime(value)) {12163 if (instr_is_comptime(value)) {
12242 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);12164 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
12243 if (!ptr_val)12165 if (!ptr_val)
...@@ -12257,8 +12179,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,...@@ -12257,8 +12179,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
12257 return ira->codegen->builtin_types.entry_pure_error;12179 return ira->codegen->builtin_types.entry_pure_error;
12258 } else {12180 } else {
12259 ir_add_error(ira, value,12181 ir_add_error(ira, value,
12260 buf_sprintf("expected error union type, found '%s'", buf_ptr(&non_canon_type->name)));12182 buf_sprintf("expected error union type, found '%s'", buf_ptr(&type_entry->name)));
12261 // TODO if this is a typedecl, add error note showing the declaration of the type decl
12262 return ira->codegen->builtin_types.entry_invalid;12183 return ira->codegen->builtin_types.entry_invalid;
12263 }12184 }
12264}12185}
...@@ -12275,12 +12196,11 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,...@@ -12275,12 +12196,11 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
12275 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.12196 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
12276 assert(ptr_type->id == TypeTableEntryIdPointer);12197 assert(ptr_type->id == TypeTableEntryIdPointer);
1227712198
12278 TypeTableEntry *non_canon_type = ptr_type->data.pointer.child_type;12199 TypeTableEntry *type_entry = ptr_type->data.pointer.child_type;
12279 TypeTableEntry *canon_type = get_underlying_type(non_canon_type);12200 if (type_is_invalid(type_entry)) {
12280 if (type_is_invalid(canon_type)) {
12281 return ira->codegen->builtin_types.entry_invalid;12201 return ira->codegen->builtin_types.entry_invalid;
12282 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {12202 } else if (type_entry->id == TypeTableEntryIdErrorUnion) {
12283 TypeTableEntry *child_type = canon_type->data.error.child_type;12203 TypeTableEntry *child_type = type_entry->data.error.child_type;
12284 TypeTableEntry *result_type = get_pointer_to_type_extra(ira->codegen, child_type,12204 TypeTableEntry *result_type = get_pointer_to_type_extra(ira->codegen, child_type,
12285 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, 0, 0);12205 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile, 0, 0);
12286 if (instr_is_comptime(value)) {12206 if (instr_is_comptime(value)) {
...@@ -12307,8 +12227,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,...@@ -12307,8 +12227,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
12307 return result_type;12227 return result_type;
12308 } else {12228 } else {
12309 ir_add_error(ira, value,12229 ir_add_error(ira, value,
12310 buf_sprintf("expected error union type, found '%s'", buf_ptr(&non_canon_type->name)));12230 buf_sprintf("expected error union type, found '%s'", buf_ptr(&type_entry->name)));
12311 // TODO if this is a typedecl, add error note showing the declaration of the type decl
12312 return ira->codegen->builtin_types.entry_invalid;12231 return ira->codegen->builtin_types.entry_invalid;
12313 }12232 }
1231412233
...@@ -12594,22 +12513,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_ref(IrAnalyze *ira,...@@ -12594,22 +12513,6 @@ static TypeTableEntry *ir_analyze_instruction_decl_ref(IrAnalyze *ira,
12594 return ref_instruction->value.type;12513 return ref_instruction->value.type;
12595 }12514 }
12596 }12515 }
12597 case TldIdTypeDef:
12598 {
12599 TldTypeDef *tld_typedef = (TldTypeDef *)tld;
12600 TypeTableEntry *typedef_type = tld_typedef->type_entry;
12601
12602 IrInstruction *ref_instruction = ir_create_const_type(&ira->new_irb, instruction->base.scope,
12603 instruction->base.source_node, typedef_type);
12604 if (lval.is_ptr) {
12605 IrInstruction *ptr_inst = ir_get_ref(ira, &instruction->base, ref_instruction, true, false);
12606 ir_link_new_instruction(ptr_inst, &instruction->base);
12607 return ptr_inst->value.type;
12608 } else {
12609 ir_link_new_instruction(ref_instruction, &instruction->base);
12610 return ref_instruction->value.type;
12611 }
12612 }
12613 }12516 }
12614 zig_unreachable();12517 zig_unreachable();
12615}12518}
src/parseh.cpp+16-57
...@@ -219,28 +219,8 @@ static Tld *add_container_tld(Context *c, TypeTableEntry *type_entry) {...@@ -219,28 +219,8 @@ static Tld *add_container_tld(Context *c, TypeTableEntry *type_entry) {
219 return add_const_type(c, &type_entry->name, type_entry);219 return add_const_type(c, &type_entry->name, type_entry);
220}220}
221221
222static Tld *add_typedef_tld(Context *c, TypeTableEntry *type_decl) {
223 assert(type_decl);
224 assert(type_decl->id == TypeTableEntryIdTypeDecl);
225
226 TldTypeDef *tld_typedef = allocate<TldTypeDef>(1);
227 parseh_init_tld(c, &tld_typedef->base, TldIdTypeDef, &type_decl->name);
228 tld_typedef->type_entry = type_decl;
229
230 add_global(c, &tld_typedef->base);
231 c->global_type_table.put(&type_decl->name, type_decl);
232
233 return &tld_typedef->base;
234}
235
236static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {222static bool is_c_void_type(Context *c, TypeTableEntry *type_entry) {
237 while (type_entry->id == TypeTableEntryIdTypeDecl) {223 return (type_entry == c->codegen->builtin_types.entry_c_void);
238 if (type_entry == c->codegen->builtin_types.entry_c_void) {
239 return true;
240 }
241 type_entry = type_entry->data.type_decl.child_type;
242 }
243 return false;
244}224}
245225
246static bool qual_type_child_is_fn_proto(const QualType &qt) {226static bool qual_type_child_is_fn_proto(const QualType &qt) {
...@@ -369,7 +349,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const...@@ -369,7 +349,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
369 const PointerType *pointer_ty = static_cast<const PointerType*>(ty);349 const PointerType *pointer_ty = static_cast<const PointerType*>(ty);
370 QualType child_qt = pointer_ty->getPointeeType();350 QualType child_qt = pointer_ty->getPointeeType();
371 TypeTableEntry *child_type = resolve_qual_type(c, child_qt, decl);351 TypeTableEntry *child_type = resolve_qual_type(c, child_qt, decl);
372 if (get_underlying_type(child_type)->id == TypeTableEntryIdInvalid) {352 if (type_is_invalid(child_type)) {
373 emit_warning(c, decl, "pointer to unresolved type");353 emit_warning(c, decl, "pointer to unresolved type");
374 return c->codegen->builtin_types.entry_invalid;354 return c->codegen->builtin_types.entry_invalid;
375 }355 }
...@@ -410,7 +390,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const...@@ -410,7 +390,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
410 } else {390 } else {
411 auto entry = type_table->maybe_get(type_name);391 auto entry = type_table->maybe_get(type_name);
412 if (entry) {392 if (entry) {
413 if (get_underlying_type(entry->value)->id == TypeTableEntryIdInvalid) {393 if (type_is_invalid(entry->value)) {
414 return c->codegen->builtin_types.entry_invalid;394 return c->codegen->builtin_types.entry_invalid;
415 } else {395 } else {
416 return entry->value;396 return entry->value;
...@@ -507,7 +487,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const...@@ -507,7 +487,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
507 fn_type_id.return_type = c->codegen->builtin_types.entry_unreachable;487 fn_type_id.return_type = c->codegen->builtin_types.entry_unreachable;
508 } else {488 } else {
509 fn_type_id.return_type = resolve_qual_type(c, fn_proto_ty->getReturnType(), decl);489 fn_type_id.return_type = resolve_qual_type(c, fn_proto_ty->getReturnType(), decl);
510 if (get_underlying_type(fn_type_id.return_type)->id == TypeTableEntryIdInvalid) {490 if (type_is_invalid(fn_type_id.return_type)) {
511 emit_warning(c, decl, "unresolved function proto return type");491 emit_warning(c, decl, "unresolved function proto return type");
512 return c->codegen->builtin_types.entry_invalid;492 return c->codegen->builtin_types.entry_invalid;
513 }493 }
...@@ -522,7 +502,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const...@@ -522,7 +502,7 @@ static TypeTableEntry *resolve_type_with_table(Context *c, const Type *ty, const
522 QualType qt = fn_proto_ty->getParamType(i);502 QualType qt = fn_proto_ty->getParamType(i);
523 TypeTableEntry *param_type = resolve_qual_type(c, qt, decl);503 TypeTableEntry *param_type = resolve_qual_type(c, qt, decl);
524504
525 if (get_underlying_type(param_type)->id == TypeTableEntryIdInvalid) {505 if (type_is_invalid(param_type)) {
526 emit_warning(c, decl, "unresolved function proto parameter type");506 emit_warning(c, decl, "unresolved function proto parameter type");
527 return c->codegen->builtin_types.entry_invalid;507 return c->codegen->builtin_types.entry_invalid;
528 }508 }
...@@ -702,6 +682,7 @@ static void replace_with_fwd_decl(Context *c, TypeTableEntry *struct_type, Buf *...@@ -702,6 +682,7 @@ static void replace_with_fwd_decl(Context *c, TypeTableEntry *struct_type, Buf *
702682
703 ZigLLVMReplaceTemporary(c->codegen->dbuilder, struct_type->di_type, replacement_di_type);683 ZigLLVMReplaceTemporary(c->codegen->dbuilder, struct_type->di_type, replacement_di_type);
704 struct_type->di_type = replacement_di_type;684 struct_type->di_type = replacement_di_type;
685 struct_type->id = TypeTableEntryIdOpaque;
705}686}
706687
707static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) {688static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) {
...@@ -809,7 +790,9 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)...@@ -809,7 +790,9 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
809790
810 return enum_type;791 return enum_type;
811 } else {792 } else {
812 TypeTableEntry *enum_type = get_typedecl_type(c->codegen, buf_ptr(full_type_name), tag_type_entry);793 // TODO after issue #305 is solved, make this be an enum with tag_type_entry
794 // as the integer type and set the custom enum values
795 TypeTableEntry *enum_type = tag_type_entry;
813 c->enum_type_table.put(bare_name, enum_type);796 c->enum_type_table.put(bare_name, enum_type);
814 c->decl_table.put(enum_decl, enum_type);797 c->decl_table.put(enum_decl, enum_type);
815798
...@@ -847,22 +830,8 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {...@@ -847,22 +830,8 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
847830
848 Buf *bare_name = buf_create_from_str(decl_name(enum_decl));831 Buf *bare_name = buf_create_from_str(decl_name(enum_decl));
849832
850 if (enum_type->id == TypeTableEntryIdEnum) {833 Tld *tld = add_container_tld(c, enum_type);
851 if (enum_type->data.enumeration.complete) {834 add_global_weak_alias(c, bare_name, tld);
852 Tld *tld = add_container_tld(c, enum_type);
853 add_global_weak_alias(c, bare_name, tld);
854 } else {
855 TypeTableEntry *typedecl_type = get_typedecl_type(c->codegen, buf_ptr(&enum_type->name),
856 c->codegen->builtin_types.entry_u8);
857 Tld *tld = add_typedef_tld(c, typedecl_type);
858 add_global_weak_alias(c, bare_name, tld);
859 }
860 } else if (enum_type->id == TypeTableEntryIdTypeDecl) {
861 Tld *tld = add_typedef_tld(c, enum_type);
862 add_global_weak_alias(c, bare_name, tld);
863 } else {
864 zig_unreachable();
865 }
866}835}
867836
868static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_decl) {837static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_decl) {
...@@ -912,7 +881,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_...@@ -912,7 +881,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
912 const FieldDecl *field_decl = *it;881 const FieldDecl *field_decl = *it;
913882
914 if (field_decl->isBitField()) {883 if (field_decl->isBitField()) {
915 emit_warning(c, field_decl, "struct %s demoted to typedef - has bitfield\n", buf_ptr(bare_name));884 emit_warning(c, field_decl, "struct %s demoted to opaque type - has bitfield\n", buf_ptr(bare_name));
916 replace_with_fwd_decl(c, struct_type, full_type_name);885 replace_with_fwd_decl(c, struct_type, full_type_name);
917 return struct_type;886 return struct_type;
918 }887 }
...@@ -939,7 +908,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_...@@ -939,7 +908,7 @@ static TypeTableEntry *resolve_record_decl(Context *c, const RecordDecl *record_
939 type_struct_field->type_entry = field_type;908 type_struct_field->type_entry = field_type;
940909
941 if (type_is_invalid(field_type) || !type_is_complete(field_type)) {910 if (type_is_invalid(field_type) || !type_is_complete(field_type)) {
942 emit_warning(c, field_decl, "struct %s demoted to typedef - unresolved type\n", buf_ptr(bare_name));911 emit_warning(c, field_decl, "struct %s demoted to opaque type - unresolved type\n", buf_ptr(bare_name));
943 replace_with_fwd_decl(c, struct_type, full_type_name);912 replace_with_fwd_decl(c, struct_type, full_type_name);
944 return struct_type;913 return struct_type;
945 }914 }
...@@ -1001,23 +970,14 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {...@@ -1001,23 +970,14 @@ static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
1001 return;970 return;
1002 }971 }
1003972
1004 assert(struct_type->id == TypeTableEntryIdStruct);
1005
1006 bool is_anonymous = (record_decl->isAnonymousStructOrUnion() || decl_name(record_decl)[0] == 0);973 bool is_anonymous = (record_decl->isAnonymousStructOrUnion() || decl_name(record_decl)[0] == 0);
1007 if (is_anonymous)974 if (is_anonymous)
1008 return;975 return;
1009976
1010 Buf *bare_name = buf_create_from_str(decl_name(record_decl));977 Buf *bare_name = buf_create_from_str(decl_name(record_decl));
1011978
1012 if (struct_type->data.structure.complete) {979 Tld *tld = add_container_tld(c, struct_type);
1013 Tld *tld = add_container_tld(c, struct_type);980 add_global_weak_alias(c, bare_name, tld);
1014 add_global_weak_alias(c, bare_name, tld);
1015 } else {
1016 TypeTableEntry *typedecl_type = get_typedecl_type(c->codegen, buf_ptr(&struct_type->name),
1017 c->codegen->builtin_types.entry_u8);
1018 Tld *tld = add_typedef_tld(c, typedecl_type);
1019 add_global_weak_alias(c, bare_name, tld);
1020 }
1021}981}
1022982
1023static void visit_var_decl(Context *c, const VarDecl *var_decl) {983static void visit_var_decl(Context *c, const VarDecl *var_decl) {
...@@ -1059,8 +1019,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {...@@ -1059,8 +1019,7 @@ static void visit_var_decl(Context *c, const VarDecl *var_decl) {
1059 switch (ap_value->getKind()) {1019 switch (ap_value->getKind()) {
1060 case APValue::Int:1020 case APValue::Int:
1061 {1021 {
1062 TypeTableEntry *canon_type = get_underlying_type(var_type);1022 if (var_type->id != TypeTableEntryIdInt) {
1063 if (canon_type->id != TypeTableEntryIdInt) {
1064 emit_warning(c, var_decl,1023 emit_warning(c, var_decl,
1065 "ignoring variable '%s' - int initializer for non int type\n", buf_ptr(name));1024 "ignoring variable '%s' - int initializer for non int type\n", buf_ptr(name));
1066 return;1025 return;
src/parser.cpp+2-43
...@@ -710,7 +710,7 @@ static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool m...@@ -710,7 +710,7 @@ static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool m
710710
711/*711/*
712PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl712PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl
713KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "type" | "this" | "unreachable"713KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "this" | "unreachable"
714*/714*/
715static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory) {715static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
716 Token *token = &pc->tokens->at(*token_index);716 Token *token = &pc->tokens->at(*token_index);
...@@ -766,10 +766,6 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo...@@ -766,10 +766,6 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
766 AstNode *node = ast_create_node(pc, NodeTypeUnreachable, token);766 AstNode *node = ast_create_node(pc, NodeTypeUnreachable, token);
767 *token_index += 1;767 *token_index += 1;
768 return node;768 return node;
769 } else if (token->id == TokenIdKeywordType) {
770 AstNode *node = ast_create_node(pc, NodeTypeTypeLiteral, token);
771 *token_index += 1;
772 return node;
773 } else if (token->id == TokenIdKeywordError) {769 } else if (token->id == TokenIdKeywordError) {
774 AstNode *node = ast_create_node(pc, NodeTypeErrorType, token);770 AstNode *node = ast_create_node(pc, NodeTypeErrorType, token);
775 *token_index += 1;771 *token_index += 1;
...@@ -2500,34 +2496,9 @@ static AstNode *ast_parse_test_decl_node(ParseContext *pc, size_t *token_index)...@@ -2500,34 +2496,9 @@ static AstNode *ast_parse_test_decl_node(ParseContext *pc, size_t *token_index)
2500 return node;2496 return node;
2501}2497}
25022498
2503/*
2504TypeDecl = "type" "Symbol" "=" TypeExpr ";"
2505*/
2506static AstNode *ast_parse_type_decl(ParseContext *pc, size_t *token_index, VisibMod visib_mod) {
2507 Token *first_token = &pc->tokens->at(*token_index);
2508
2509 if (first_token->id != TokenIdKeywordType) {
2510 return nullptr;
2511 }
2512 *token_index += 1;
2513
2514 Token *name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
2515 ast_eat_token(pc, token_index, TokenIdEq);
2516
2517 AstNode *node = ast_create_node(pc, NodeTypeTypeDecl, first_token);
2518 node->data.type_decl.symbol = token_buf(name_tok);
2519 node->data.type_decl.child_type = ast_parse_type_expr(pc, token_index, true);
2520
2521 ast_eat_token(pc, token_index, TokenIdSemicolon);
2522
2523 node->data.type_decl.visib_mod = visib_mod;
2524
2525 return node;
2526}
2527
2528/*2499/*
2529TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl2500TopLevelItem = ErrorValueDecl | CompTimeExpression(Block) | TopLevelDecl | TestDecl
2530TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | TypeDecl | UseDecl)2501TopLevelDecl = option(VisibleMod) (FnDef | ExternDecl | GlobalVarDecl | UseDecl)
2531*/2502*/
2532static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {2503static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, ZigList<AstNode *> *top_level_decls) {
2533 for (;;) {2504 for (;;) {
...@@ -2586,12 +2557,6 @@ static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, Zig...@@ -2586,12 +2557,6 @@ static void ast_parse_top_level_decls(ParseContext *pc, size_t *token_index, Zig
2586 continue;2557 continue;
2587 }2558 }
25882559
2589 AstNode *type_decl_node = ast_parse_type_decl(pc, token_index, visib_mod);
2590 if (type_decl_node) {
2591 top_level_decls->append(type_decl_node);
2592 continue;
2593 }
2594
2595 return;2560 return;
2596 }2561 }
2597 zig_unreachable();2562 zig_unreachable();
...@@ -2674,9 +2639,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont...@@ -2674,9 +2639,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
2674 visit_field(&node->data.variable_declaration.type, visit, context);2639 visit_field(&node->data.variable_declaration.type, visit, context);
2675 visit_field(&node->data.variable_declaration.expr, visit, context);2640 visit_field(&node->data.variable_declaration.expr, visit, context);
2676 break;2641 break;
2677 case NodeTypeTypeDecl:
2678 visit_field(&node->data.type_decl.child_type, visit, context);
2679 break;
2680 case NodeTypeErrorValueDecl:2642 case NodeTypeErrorValueDecl:
2681 // none2643 // none
2682 break;2644 break;
...@@ -2826,9 +2788,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont...@@ -2826,9 +2788,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
2826 case NodeTypeErrorType:2788 case NodeTypeErrorType:
2827 // none2789 // none
2828 break;2790 break;
2829 case NodeTypeTypeLiteral:
2830 // none
2831 break;
2832 case NodeTypeVarLiteral:2791 case NodeTypeVarLiteral:
2833 // none2792 // none
2834 break;2793 break;
src/tokenizer.cpp-2
...@@ -139,7 +139,6 @@ static const struct ZigKeyword zig_keywords[] = {...@@ -139,7 +139,6 @@ static const struct ZigKeyword zig_keywords[] = {
139 {"this", TokenIdKeywordThis},139 {"this", TokenIdKeywordThis},
140 {"true", TokenIdKeywordTrue},140 {"true", TokenIdKeywordTrue},
141 {"try", TokenIdKeywordTry},141 {"try", TokenIdKeywordTry},
142 {"type", TokenIdKeywordType},
143 {"undefined", TokenIdKeywordUndefined},142 {"undefined", TokenIdKeywordUndefined},
144 {"union", TokenIdKeywordUnion},143 {"union", TokenIdKeywordUnion},
145 {"unreachable", TokenIdKeywordUnreachable},144 {"unreachable", TokenIdKeywordUnreachable},
...@@ -1474,7 +1473,6 @@ const char * token_name(TokenId id) {...@@ -1474,7 +1473,6 @@ const char * token_name(TokenId id) {
1474 case TokenIdKeywordThis: return "this";1473 case TokenIdKeywordThis: return "this";
1475 case TokenIdKeywordTrue: return "true";1474 case TokenIdKeywordTrue: return "true";
1476 case TokenIdKeywordTry: return "try";1475 case TokenIdKeywordTry: return "try";
1477 case TokenIdKeywordType: return "type";
1478 case TokenIdKeywordUndefined: return "undefined";1476 case TokenIdKeywordUndefined: return "undefined";
1479 case TokenIdKeywordUnion: return "union";1477 case TokenIdKeywordUnion: return "union";
1480 case TokenIdKeywordUnreachable: return "unreachable";1478 case TokenIdKeywordUnreachable: return "unreachable";
src/tokenizer.hpp-1
...@@ -76,7 +76,6 @@ enum TokenId {...@@ -76,7 +76,6 @@ enum TokenId {
76 TokenIdKeywordThis,76 TokenIdKeywordThis,
77 TokenIdKeywordTrue,77 TokenIdKeywordTrue,
78 TokenIdKeywordTry,78 TokenIdKeywordTry,
79 TokenIdKeywordType,
80 TokenIdKeywordUndefined,79 TokenIdKeywordUndefined,
81 TokenIdKeywordUnion,80 TokenIdKeywordUnion,
82 TokenIdKeywordUnreachable,81 TokenIdKeywordUnreachable,
test/cases/typedef.zig deleted-10
...@@ -1,10 +0,0 @@
1const assert = @import("std").debug.assert;
2
3type int = u8;
4
5fn add(a: int, b: int) -> int {
6 a + b
7}
8test "typedef" {
9 assert(add(12, 34) == 46);
10}
test/run_tests.cpp+4-4
...@@ -2191,7 +2191,7 @@ struct Foo {...@@ -2191,7 +2191,7 @@ struct Foo {
2191 add_parseh_case("struct prototype used in func", AllowWarningsNo, R"SOURCE(2191 add_parseh_case("struct prototype used in func", AllowWarningsNo, R"SOURCE(
2192struct Foo;2192struct Foo;
2193struct Foo *some_func(struct Foo *foo, int x);2193struct Foo *some_func(struct Foo *foo, int x);
2194 )SOURCE", 3, R"OUTPUT(pub type struct_Foo = u8;)OUTPUT",2194 )SOURCE", 3, R"OUTPUT(pub const struct_Foo = @OpaqueType();)OUTPUT",
2195 R"OUTPUT(pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",2195 R"OUTPUT(pub extern fn some_func(foo: ?&struct_Foo, x: c_int) -> ?&struct_Foo;)OUTPUT",
2196 R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");2196 R"OUTPUT(pub const Foo = struct_Foo;)OUTPUT");
21972197
...@@ -2277,12 +2277,12 @@ void foo(void (__cdecl *fn_ptr)(void));...@@ -2277,12 +2277,12 @@ void foo(void (__cdecl *fn_ptr)(void));
2277 )SOURCE", 1, "pub const SDL_INIT_VIDEO = 32;");2277 )SOURCE", 1, "pub const SDL_INIT_VIDEO = 32;");
22782278
2279 add_parseh_case("zig keywords in C code", AllowWarningsNo, R"SOURCE(2279 add_parseh_case("zig keywords in C code", AllowWarningsNo, R"SOURCE(
2280struct type {2280struct comptime {
2281 int defer;2281 int defer;
2282};2282};
2283 )SOURCE", 2, R"(pub const struct_type = extern struct {2283 )SOURCE", 2, R"(pub const struct_comptime = extern struct {
2284 @"defer": c_int,2284 @"defer": c_int,
2285};)", R"(pub const @"type" = struct_type;)");2285};)", R"(pub const @"comptime" = struct_comptime;)");
22862286
2287 add_parseh_case("macro defines string literal with octal", AllowWarningsNo, R"SOURCE(2287 add_parseh_case("macro defines string literal with octal", AllowWarningsNo, R"SOURCE(
2288#define FOO "aoeu\023 derp"2288#define FOO "aoeu\023 derp"
test/self_hosted.zig-1
...@@ -31,7 +31,6 @@ comptime {...@@ -31,7 +31,6 @@ comptime {
31 _ = @import("cases/switch_prong_implicit_cast.zig");31 _ = @import("cases/switch_prong_implicit_cast.zig");
32 _ = @import("cases/this.zig");32 _ = @import("cases/this.zig");
33 _ = @import("cases/try.zig");33 _ = @import("cases/try.zig");
34 _ = @import("cases/typedef.zig");
35 _ = @import("cases/undefined.zig");34 _ = @import("cases/undefined.zig");
36 _ = @import("cases/var_args.zig");35 _ = @import("cases/var_args.zig");
37 _ = @import("cases/void.zig");36 _ = @import("cases/void.zig");