| ... | ... | @@ -40,9 +40,6 @@ struct Context { |
| 40 | 40 | bool warnings_on; |
| 41 | 41 | VisibMod visib_mod; |
| 42 | 42 | AstNode *root; |
| 43 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> global_type_table; |
| 44 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> struct_type_table; |
| 45 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> enum_type_table; |
| 46 | 43 | HashMap<const void *, AstNode *, ptr_hash, ptr_eq> decl_table; |
| 47 | 44 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> macro_table; |
| 48 | 45 | SourceManager *source_manager; |
| ... | ... | @@ -56,9 +53,9 @@ struct Context { |
| 56 | 53 | |
| 57 | 54 | static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl); |
| 58 | 55 | static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl); |
| 59 | | static AstNode * trans_qual_type_with_table(Context *c, QualType qt, const SourceLocation &source_loc, |
| 60 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> *type_table); |
| 61 | | static AstNode * trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc); |
| 56 | static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl); |
| 57 | static AstNode *trans_qual_type_with_table(Context *c, QualType qt, const SourceLocation &source_loc); |
| 58 | static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc); |
| 62 | 59 | |
| 63 | 60 | |
| 64 | 61 | __attribute__ ((format (printf, 3, 4))) |
| ... | ... | @@ -372,9 +369,7 @@ static AstNode * trans_expr(Context *c, AstNode *block, Expr *expr) { |
| 372 | 369 | return trans_stmt(c, block, expr); |
| 373 | 370 | } |
| 374 | 371 | |
| 375 | | static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLocation &source_loc, |
| 376 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> *type_table) |
| 377 | | { |
| 372 | static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLocation &source_loc) { |
| 378 | 373 | switch (ty->getTypeClass()) { |
| 379 | 374 | case Type::Builtin: |
| 380 | 375 | { |
| ... | ... | @@ -504,46 +499,16 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo |
| 504 | 499 | { |
| 505 | 500 | const TypedefType *typedef_ty = static_cast<const TypedefType*>(ty); |
| 506 | 501 | const TypedefNameDecl *typedef_decl = typedef_ty->getDecl(); |
| 507 | | Buf *type_name = buf_create_from_str(decl_name(typedef_decl)); |
| 508 | | if (buf_eql_str(type_name, "uint8_t")) { |
| 509 | | return trans_create_node_symbol_str(c, "u8"); |
| 510 | | } else if (buf_eql_str(type_name, "int8_t")) { |
| 511 | | return trans_create_node_symbol_str(c, "i8"); |
| 512 | | } else if (buf_eql_str(type_name, "uint16_t")) { |
| 513 | | return trans_create_node_symbol_str(c, "u16"); |
| 514 | | } else if (buf_eql_str(type_name, "int16_t")) { |
| 515 | | return trans_create_node_symbol_str(c, "i16"); |
| 516 | | } else if (buf_eql_str(type_name, "uint32_t")) { |
| 517 | | return trans_create_node_symbol_str(c, "u32"); |
| 518 | | } else if (buf_eql_str(type_name, "int32_t")) { |
| 519 | | return trans_create_node_symbol_str(c, "i32"); |
| 520 | | } else if (buf_eql_str(type_name, "uint64_t")) { |
| 521 | | return trans_create_node_symbol_str(c, "u64"); |
| 522 | | } else if (buf_eql_str(type_name, "int64_t")) { |
| 523 | | return trans_create_node_symbol_str(c, "i64"); |
| 524 | | } else if (buf_eql_str(type_name, "intptr_t")) { |
| 525 | | return trans_create_node_symbol_str(c, "isize"); |
| 526 | | } else if (buf_eql_str(type_name, "uintptr_t")) { |
| 527 | | return trans_create_node_symbol_str(c, "usize"); |
| 528 | | } else { |
| 529 | | auto entry = type_table->maybe_get(type_name); |
| 530 | | if (entry == nullptr || entry->value == nullptr) { |
| 531 | | return nullptr; |
| 532 | | } else { |
| 533 | | return entry->value; |
| 534 | | } |
| 535 | | } |
| 502 | return resolve_typedef_decl(c, typedef_decl); |
| 536 | 503 | } |
| 537 | 504 | case Type::Elaborated: |
| 538 | 505 | { |
| 539 | 506 | const ElaboratedType *elaborated_ty = static_cast<const ElaboratedType*>(ty); |
| 540 | 507 | switch (elaborated_ty->getKeyword()) { |
| 541 | 508 | case ETK_Struct: |
| 542 | | return trans_qual_type_with_table(c, elaborated_ty->getNamedType(), |
| 543 | | source_loc, &c->struct_type_table); |
| 509 | return trans_qual_type_with_table(c, elaborated_ty->getNamedType(), source_loc); |
| 544 | 510 | case ETK_Enum: |
| 545 | | return trans_qual_type_with_table(c, elaborated_ty->getNamedType(), |
| 546 | | source_loc, &c->enum_type_table); |
| 511 | return trans_qual_type_with_table(c, elaborated_ty->getNamedType(), source_loc); |
| 547 | 512 | case ETK_Interface: |
| 548 | 513 | case ETK_Union: |
| 549 | 514 | case ETK_Class: |
| ... | ... | @@ -738,14 +703,12 @@ static AstNode *trans_type_with_table(Context *c, const Type *ty, const SourceLo |
| 738 | 703 | zig_unreachable(); |
| 739 | 704 | } |
| 740 | 705 | |
| 741 | | static AstNode * trans_qual_type_with_table(Context *c, QualType qt, const SourceLocation &source_loc, |
| 742 | | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> *type_table) |
| 743 | | { |
| 744 | | return trans_type_with_table(c, qt.getTypePtr(), source_loc, type_table); |
| 706 | static AstNode * trans_qual_type_with_table(Context *c, QualType qt, const SourceLocation &source_loc) { |
| 707 | return trans_type_with_table(c, qt.getTypePtr(), source_loc); |
| 745 | 708 | } |
| 746 | 709 | |
| 747 | 710 | static AstNode * trans_qual_type(Context *c, QualType qt, const SourceLocation &source_loc) { |
| 748 | | return trans_qual_type_with_table(c, qt, source_loc, &c->global_type_table); |
| 711 | return trans_qual_type_with_table(c, qt, source_loc); |
| 749 | 712 | } |
| 750 | 713 | |
| 751 | 714 | static AstNode * trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) { |
| ... | ... | @@ -1690,23 +1653,45 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) { |
| 1690 | 1653 | c->root->data.root.top_level_decls.append(proto_node); |
| 1691 | 1654 | } |
| 1692 | 1655 | |
| 1693 | | static void visit_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl) { |
| 1656 | static AstNode *resolve_typdef_as_builtin(Context *c, const TypedefNameDecl *typedef_decl, const char *primitive_name) { |
| 1657 | AstNode *node = trans_create_node_symbol_str(c, primitive_name); |
| 1658 | c->decl_table.put(typedef_decl, node); |
| 1659 | return node; |
| 1660 | } |
| 1661 | |
| 1662 | static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl) { |
| 1663 | auto existing_entry = c->decl_table.maybe_get((void*)typedef_decl); |
| 1664 | if (existing_entry) { |
| 1665 | return existing_entry->value; |
| 1666 | } |
| 1667 | |
| 1694 | 1668 | QualType child_qt = typedef_decl->getUnderlyingType(); |
| 1695 | 1669 | Buf *type_name = buf_create_from_str(decl_name(typedef_decl)); |
| 1696 | 1670 | |
| 1697 | | if (buf_eql_str(type_name, "uint8_t") || |
| 1698 | | buf_eql_str(type_name, "int8_t") || |
| 1699 | | buf_eql_str(type_name, "uint16_t") || |
| 1700 | | buf_eql_str(type_name, "int16_t") || |
| 1701 | | buf_eql_str(type_name, "uint32_t") || |
| 1702 | | buf_eql_str(type_name, "int32_t") || |
| 1703 | | buf_eql_str(type_name, "uint64_t") || |
| 1704 | | buf_eql_str(type_name, "int64_t") || |
| 1705 | | buf_eql_str(type_name, "intptr_t") || |
| 1706 | | buf_eql_str(type_name, "uintptr_t")) |
| 1707 | | { |
| 1708 | | // special case we can just use the builtin types |
| 1709 | | return; |
| 1671 | if (buf_eql_str(type_name, "uint8_t")) { |
| 1672 | return resolve_typdef_as_builtin(c, typedef_decl, "u8"); |
| 1673 | } else if (buf_eql_str(type_name, "int8_t")) { |
| 1674 | return resolve_typdef_as_builtin(c, typedef_decl, "i8"); |
| 1675 | } else if (buf_eql_str(type_name, "uint16_t")) { |
| 1676 | return resolve_typdef_as_builtin(c, typedef_decl, "u16"); |
| 1677 | } else if (buf_eql_str(type_name, "int16_t")) { |
| 1678 | return resolve_typdef_as_builtin(c, typedef_decl, "i16"); |
| 1679 | } else if (buf_eql_str(type_name, "uint32_t")) { |
| 1680 | return resolve_typdef_as_builtin(c, typedef_decl, "u32"); |
| 1681 | } else if (buf_eql_str(type_name, "int32_t")) { |
| 1682 | return resolve_typdef_as_builtin(c, typedef_decl, "i32"); |
| 1683 | } else if (buf_eql_str(type_name, "uint64_t")) { |
| 1684 | return resolve_typdef_as_builtin(c, typedef_decl, "u64"); |
| 1685 | } else if (buf_eql_str(type_name, "int64_t")) { |
| 1686 | return resolve_typdef_as_builtin(c, typedef_decl, "i64"); |
| 1687 | } else if (buf_eql_str(type_name, "intptr_t")) { |
| 1688 | return resolve_typdef_as_builtin(c, typedef_decl, "isize"); |
| 1689 | } else if (buf_eql_str(type_name, "uintptr_t")) { |
| 1690 | return resolve_typdef_as_builtin(c, typedef_decl, "usize"); |
| 1691 | } else if (buf_eql_str(type_name, "ssize_t")) { |
| 1692 | return resolve_typdef_as_builtin(c, typedef_decl, "isize"); |
| 1693 | } else if (buf_eql_str(type_name, "size_t")) { |
| 1694 | return resolve_typdef_as_builtin(c, typedef_decl, "usize"); |
| 1710 | 1695 | } |
| 1711 | 1696 | |
| 1712 | 1697 | // if the underlying type is anonymous, we can special case it to just |
| ... | ... | @@ -1716,10 +1701,14 @@ static void visit_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl) |
| 1716 | 1701 | AstNode *type_node = trans_qual_type(c, child_qt, typedef_decl->getLocation()); |
| 1717 | 1702 | if (type_node == nullptr) { |
| 1718 | 1703 | emit_warning(c, typedef_decl->getLocation(), "typedef %s - unresolved child type", buf_ptr(type_name)); |
| 1719 | | return; |
| 1704 | c->decl_table.put(typedef_decl, nullptr); |
| 1705 | return nullptr; |
| 1720 | 1706 | } |
| 1721 | 1707 | add_global_var(c, type_name, type_node); |
| 1722 | | c->global_type_table.put(type_name, type_node); |
| 1708 | |
| 1709 | AstNode *symbol_node = trans_create_node_symbol(c, type_name); |
| 1710 | c->decl_table.put(typedef_decl, symbol_node); |
| 1711 | return symbol_node; |
| 1723 | 1712 | } |
| 1724 | 1713 | |
| 1725 | 1714 | struct AstNode *demote_enum_to_opaque(Context *c, const EnumDecl *enum_decl, |
| ... | ... | @@ -1731,7 +1720,6 @@ struct AstNode *demote_enum_to_opaque(Context *c, const EnumDecl *enum_decl, |
| 1731 | 1720 | return opaque_node; |
| 1732 | 1721 | } |
| 1733 | 1722 | AstNode *symbol_node = trans_create_node_symbol(c, full_type_name); |
| 1734 | | c->enum_type_table.put(bare_name, symbol_node); |
| 1735 | 1723 | add_global_weak_alias(c, bare_name, full_type_name); |
| 1736 | 1724 | add_global_var(c, full_type_name, opaque_node); |
| 1737 | 1725 | c->decl_table.put(enum_decl, symbol_node); |
| ... | ... | @@ -1808,7 +1796,6 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) { |
| 1808 | 1796 | return enum_node; |
| 1809 | 1797 | } else { |
| 1810 | 1798 | AstNode *symbol_node = trans_create_node_symbol(c, full_type_name); |
| 1811 | | c->enum_type_table.put(bare_name, symbol_node); |
| 1812 | 1799 | add_global_weak_alias(c, bare_name, full_type_name); |
| 1813 | 1800 | add_global_var(c, full_type_name, enum_node); |
| 1814 | 1801 | c->decl_table.put(enum_decl, symbol_node); |
| ... | ... | @@ -1839,7 +1826,6 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) { |
| 1839 | 1826 | return enum_node; |
| 1840 | 1827 | } else { |
| 1841 | 1828 | AstNode *symbol_node = trans_create_node_symbol(c, full_type_name); |
| 1842 | | c->enum_type_table.put(bare_name, symbol_node); |
| 1843 | 1829 | add_global_weak_alias(c, bare_name, full_type_name); |
| 1844 | 1830 | add_global_var(c, full_type_name, enum_node); |
| 1845 | 1831 | return symbol_node; |
| ... | ... | @@ -1855,7 +1841,6 @@ static AstNode *demote_struct_to_opaque(Context *c, const RecordDecl *record_dec |
| 1855 | 1841 | return opaque_node; |
| 1856 | 1842 | } |
| 1857 | 1843 | AstNode *symbol_node = trans_create_node_symbol(c, full_type_name); |
| 1858 | | c->struct_type_table.put(bare_name, symbol_node); |
| 1859 | 1844 | add_global_weak_alias(c, bare_name, full_type_name); |
| 1860 | 1845 | add_global_var(c, full_type_name, opaque_node); |
| 1861 | 1846 | c->decl_table.put(record_decl, symbol_node); |
| ... | ... | @@ -1872,6 +1857,7 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) { |
| 1872 | 1857 | |
| 1873 | 1858 | if (!record_decl->isStruct()) { |
| 1874 | 1859 | emit_warning(c, record_decl->getLocation(), "skipping record %s, not a struct", raw_name); |
| 1860 | c->decl_table.put(record_decl, nullptr); |
| 1875 | 1861 | return nullptr; |
| 1876 | 1862 | } |
| 1877 | 1863 | |
| ... | ... | @@ -1911,9 +1897,6 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) { |
| 1911 | 1897 | if (is_anonymous) { |
| 1912 | 1898 | c->decl_table.put(record_decl, struct_node); |
| 1913 | 1899 | } else { |
| 1914 | | c->struct_type_table.put(bare_name, struct_node); |
| 1915 | | add_global_weak_alias(c, bare_name, full_type_name); |
| 1916 | | add_global_var(c, full_type_name, struct_node); |
| 1917 | 1900 | c->decl_table.put(record_decl, trans_create_node_symbol(c, full_type_name)); |
| 1918 | 1901 | } |
| 1919 | 1902 | |
| ... | ... | @@ -1942,6 +1925,8 @@ static AstNode *resolve_record_decl(Context *c, const RecordDecl *record_decl) { |
| 1942 | 1925 | if (is_anonymous) { |
| 1943 | 1926 | return struct_node; |
| 1944 | 1927 | } else { |
| 1928 | add_global_weak_alias(c, bare_name, full_type_name); |
| 1929 | add_global_var(c, full_type_name, struct_node); |
| 1945 | 1930 | return trans_create_node_symbol(c, full_type_name); |
| 1946 | 1931 | } |
| 1947 | 1932 | } |
| ... | ... | @@ -2032,7 +2017,7 @@ static bool decl_visitor(void *context, const Decl *decl) { |
| 2032 | 2017 | visit_fn_decl(c, static_cast<const FunctionDecl*>(decl)); |
| 2033 | 2018 | break; |
| 2034 | 2019 | case Decl::Typedef: |
| 2035 | | visit_typedef_decl(c, static_cast<const TypedefNameDecl *>(decl)); |
| 2020 | resolve_typedef_decl(c, static_cast<const TypedefNameDecl *>(decl)); |
| 2036 | 2021 | break; |
| 2037 | 2022 | case Decl::Enum: |
| 2038 | 2023 | resolve_enum_decl(c, static_cast<const EnumDecl *>(decl)); |
| ... | ... | @@ -2260,9 +2245,6 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch |
| 2260 | 2245 | c->import = import; |
| 2261 | 2246 | c->errors = errors; |
| 2262 | 2247 | c->visib_mod = VisibModPub; |
| 2263 | | c->global_type_table.init(8); |
| 2264 | | c->enum_type_table.init(8); |
| 2265 | | c->struct_type_table.init(8); |
| 2266 | 2248 | c->decl_table.init(8); |
| 2267 | 2249 | c->macro_table.init(8); |
| 2268 | 2250 | c->codegen = codegen; |