| author | |
| committer | |
| log | cf0d300ddecf3e1bf0363002c995425dab007b74 |
| tree | 1911b3595996594dffb1390699849c46eba8ad0a |
| parent | 33d9dda5584a6c21df1a4d14ab1a251772f76d4d |
| parent | e57e3602e7802789d56a076c6052092715997c24 |
| signature | Signed by PGP key 4AEE18F83AFDEB23 |
Translate-c-2 enums7 files changed, 482 insertions(+), 252 deletions(-)
lib/std/zig/ast.zig+46-46| ... | ... | @@ -581,7 +581,7 @@ pub const Node = struct { |
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | pub const Root = struct { |
| 584 | base: Node, | |
| 584 | base: Node = Node {.id = .Root}, | |
| 585 | 585 | decls: DeclList, |
| 586 | 586 | eof_token: TokenIndex, |
| 587 | 587 | |
| ... | ... | @@ -604,7 +604,7 @@ pub const Node = struct { |
| 604 | 604 | }; |
| 605 | 605 | |
| 606 | 606 | pub const VarDecl = struct { |
| 607 | base: Node, | |
| 607 | base: Node = Node {.id = .VarDecl}, | |
| 608 | 608 | doc_comments: ?*DocComment, |
| 609 | 609 | visib_token: ?TokenIndex, |
| 610 | 610 | thread_local_token: ?TokenIndex, |
| ... | ... | @@ -661,7 +661,7 @@ pub const Node = struct { |
| 661 | 661 | }; |
| 662 | 662 | |
| 663 | 663 | pub const Use = struct { |
| 664 | base: Node, | |
| 664 | base: Node = Node {.id = .Use}, | |
| 665 | 665 | doc_comments: ?*DocComment, |
| 666 | 666 | visib_token: ?TokenIndex, |
| 667 | 667 | use_token: TokenIndex, |
| ... | ... | @@ -688,7 +688,7 @@ pub const Node = struct { |
| 688 | 688 | }; |
| 689 | 689 | |
| 690 | 690 | pub const ErrorSetDecl = struct { |
| 691 | base: Node, | |
| 691 | base: Node = Node {.id = .ErrorSetDecl}, | |
| 692 | 692 | error_token: TokenIndex, |
| 693 | 693 | decls: DeclList, |
| 694 | 694 | rbrace_token: TokenIndex, |
| ... | ... | @@ -714,7 +714,7 @@ pub const Node = struct { |
| 714 | 714 | }; |
| 715 | 715 | |
| 716 | 716 | pub const ContainerDecl = struct { |
| 717 | base: Node, | |
| 717 | base: Node = Node {.id = .ContainerDecl}, | |
| 718 | 718 | layout_token: ?TokenIndex, |
| 719 | 719 | kind_token: TokenIndex, |
| 720 | 720 | init_arg_expr: InitArg, |
| ... | ... | @@ -801,7 +801,7 @@ pub const Node = struct { |
| 801 | 801 | }; |
| 802 | 802 | |
| 803 | 803 | pub const ErrorTag = struct { |
| 804 | base: Node, | |
| 804 | base: Node = Node {.id = .ErrorTag}, | |
| 805 | 805 | doc_comments: ?*DocComment, |
| 806 | 806 | name_token: TokenIndex, |
| 807 | 807 | |
| ... | ... | @@ -826,7 +826,7 @@ pub const Node = struct { |
| 826 | 826 | }; |
| 827 | 827 | |
| 828 | 828 | pub const Identifier = struct { |
| 829 | base: Node, | |
| 829 | base: Node = Node {.id = .Identifier}, | |
| 830 | 830 | token: TokenIndex, |
| 831 | 831 | |
| 832 | 832 | pub fn iterate(self: *Identifier, index: usize) ?*Node { |
| ... | ... | @@ -843,7 +843,7 @@ pub const Node = struct { |
| 843 | 843 | }; |
| 844 | 844 | |
| 845 | 845 | pub const FnProto = struct { |
| 846 | base: Node, | |
| 846 | base: Node = Node {.id = .FnProto}, | |
| 847 | 847 | doc_comments: ?*DocComment, |
| 848 | 848 | visib_token: ?TokenIndex, |
| 849 | 849 | fn_token: TokenIndex, |
| ... | ... | @@ -925,7 +925,7 @@ pub const Node = struct { |
| 925 | 925 | }; |
| 926 | 926 | |
| 927 | 927 | pub const AnyFrameType = struct { |
| 928 | base: Node, | |
| 928 | base: Node = Node {.id = .AnyFrameType}, | |
| 929 | 929 | anyframe_token: TokenIndex, |
| 930 | 930 | result: ?Result, |
| 931 | 931 | |
| ... | ... | @@ -956,7 +956,7 @@ pub const Node = struct { |
| 956 | 956 | }; |
| 957 | 957 | |
| 958 | 958 | pub const ParamDecl = struct { |
| 959 | base: Node, | |
| 959 | base: Node = Node {.id = .ParamDecl}, | |
| 960 | 960 | doc_comments: ?*DocComment, |
| 961 | 961 | comptime_token: ?TokenIndex, |
| 962 | 962 | noalias_token: ?TokenIndex, |
| ... | ... | @@ -989,7 +989,7 @@ pub const Node = struct { |
| 989 | 989 | }; |
| 990 | 990 | |
| 991 | 991 | pub const Block = struct { |
| 992 | base: Node, | |
| 992 | base: Node = Node {.id = .Block}, | |
| 993 | 993 | label: ?TokenIndex, |
| 994 | 994 | lbrace: TokenIndex, |
| 995 | 995 | statements: StatementList, |
| ... | ... | @@ -1020,7 +1020,7 @@ pub const Node = struct { |
| 1020 | 1020 | }; |
| 1021 | 1021 | |
| 1022 | 1022 | pub const Defer = struct { |
| 1023 | base: Node, | |
| 1023 | base: Node = Node {.id = .Defer}, | |
| 1024 | 1024 | defer_token: TokenIndex, |
| 1025 | 1025 | expr: *Node, |
| 1026 | 1026 | |
| ... | ... | @@ -1043,7 +1043,7 @@ pub const Node = struct { |
| 1043 | 1043 | }; |
| 1044 | 1044 | |
| 1045 | 1045 | pub const Comptime = struct { |
| 1046 | base: Node, | |
| 1046 | base: Node = Node {.id = .Comptime}, | |
| 1047 | 1047 | doc_comments: ?*DocComment, |
| 1048 | 1048 | comptime_token: TokenIndex, |
| 1049 | 1049 | expr: *Node, |
| ... | ... | @@ -1067,7 +1067,7 @@ pub const Node = struct { |
| 1067 | 1067 | }; |
| 1068 | 1068 | |
| 1069 | 1069 | pub const Payload = struct { |
| 1070 | base: Node, | |
| 1070 | base: Node = Node {.id = .Payload}, | |
| 1071 | 1071 | lpipe: TokenIndex, |
| 1072 | 1072 | error_symbol: *Node, |
| 1073 | 1073 | rpipe: TokenIndex, |
| ... | ... | @@ -1091,7 +1091,7 @@ pub const Node = struct { |
| 1091 | 1091 | }; |
| 1092 | 1092 | |
| 1093 | 1093 | pub const PointerPayload = struct { |
| 1094 | base: Node, | |
| 1094 | base: Node = Node {.id = .PointerPayload}, | |
| 1095 | 1095 | lpipe: TokenIndex, |
| 1096 | 1096 | ptr_token: ?TokenIndex, |
| 1097 | 1097 | value_symbol: *Node, |
| ... | ... | @@ -1116,7 +1116,7 @@ pub const Node = struct { |
| 1116 | 1116 | }; |
| 1117 | 1117 | |
| 1118 | 1118 | pub const PointerIndexPayload = struct { |
| 1119 | base: Node, | |
| 1119 | base: Node = Node {.id = .PointerIndexPayload}, | |
| 1120 | 1120 | lpipe: TokenIndex, |
| 1121 | 1121 | ptr_token: ?TokenIndex, |
| 1122 | 1122 | value_symbol: *Node, |
| ... | ... | @@ -1147,7 +1147,7 @@ pub const Node = struct { |
| 1147 | 1147 | }; |
| 1148 | 1148 | |
| 1149 | 1149 | pub const Else = struct { |
| 1150 | base: Node, | |
| 1150 | base: Node = Node {.id = .Else}, | |
| 1151 | 1151 | else_token: TokenIndex, |
| 1152 | 1152 | payload: ?*Node, |
| 1153 | 1153 | body: *Node, |
| ... | ... | @@ -1176,7 +1176,7 @@ pub const Node = struct { |
| 1176 | 1176 | }; |
| 1177 | 1177 | |
| 1178 | 1178 | pub const Switch = struct { |
| 1179 | base: Node, | |
| 1179 | base: Node = Node {.id = .Switch}, | |
| 1180 | 1180 | switch_token: TokenIndex, |
| 1181 | 1181 | expr: *Node, |
| 1182 | 1182 | |
| ... | ... | @@ -1208,7 +1208,7 @@ pub const Node = struct { |
| 1208 | 1208 | }; |
| 1209 | 1209 | |
| 1210 | 1210 | pub const SwitchCase = struct { |
| 1211 | base: Node, | |
| 1211 | base: Node = Node {.id = .SwitchCase}, | |
| 1212 | 1212 | items: ItemList, |
| 1213 | 1213 | arrow_token: TokenIndex, |
| 1214 | 1214 | payload: ?*Node, |
| ... | ... | @@ -1243,7 +1243,7 @@ pub const Node = struct { |
| 1243 | 1243 | }; |
| 1244 | 1244 | |
| 1245 | 1245 | pub const SwitchElse = struct { |
| 1246 | base: Node, | |
| 1246 | base: Node = Node {.id = .SwitchElse}, | |
| 1247 | 1247 | token: TokenIndex, |
| 1248 | 1248 | |
| 1249 | 1249 | pub fn iterate(self: *SwitchElse, index: usize) ?*Node { |
| ... | ... | @@ -1260,7 +1260,7 @@ pub const Node = struct { |
| 1260 | 1260 | }; |
| 1261 | 1261 | |
| 1262 | 1262 | pub const While = struct { |
| 1263 | base: Node, | |
| 1263 | base: Node = Node {.id = .While}, | |
| 1264 | 1264 | label: ?TokenIndex, |
| 1265 | 1265 | inline_token: ?TokenIndex, |
| 1266 | 1266 | while_token: TokenIndex, |
| ... | ... | @@ -1319,7 +1319,7 @@ pub const Node = struct { |
| 1319 | 1319 | }; |
| 1320 | 1320 | |
| 1321 | 1321 | pub const For = struct { |
| 1322 | base: Node, | |
| 1322 | base: Node = Node {.id = .For}, | |
| 1323 | 1323 | label: ?TokenIndex, |
| 1324 | 1324 | inline_token: ?TokenIndex, |
| 1325 | 1325 | for_token: TokenIndex, |
| ... | ... | @@ -1370,7 +1370,7 @@ pub const Node = struct { |
| 1370 | 1370 | }; |
| 1371 | 1371 | |
| 1372 | 1372 | pub const If = struct { |
| 1373 | base: Node, | |
| 1373 | base: Node = Node {.id = .If}, | |
| 1374 | 1374 | if_token: TokenIndex, |
| 1375 | 1375 | condition: *Node, |
| 1376 | 1376 | payload: ?*Node, |
| ... | ... | @@ -1413,7 +1413,7 @@ pub const Node = struct { |
| 1413 | 1413 | }; |
| 1414 | 1414 | |
| 1415 | 1415 | pub const InfixOp = struct { |
| 1416 | base: Node, | |
| 1416 | base: Node = Node {.id = .InfixOp}, | |
| 1417 | 1417 | op_token: TokenIndex, |
| 1418 | 1418 | lhs: *Node, |
| 1419 | 1419 | op: Op, |
| ... | ... | @@ -1646,7 +1646,7 @@ pub const Node = struct { |
| 1646 | 1646 | }; |
| 1647 | 1647 | |
| 1648 | 1648 | pub const FieldInitializer = struct { |
| 1649 | base: Node, | |
| 1649 | base: Node = Node {.id = .FieldInitializer}, | |
| 1650 | 1650 | period_token: TokenIndex, |
| 1651 | 1651 | name_token: TokenIndex, |
| 1652 | 1652 | expr: *Node, |
| ... | ... | @@ -1670,7 +1670,7 @@ pub const Node = struct { |
| 1670 | 1670 | }; |
| 1671 | 1671 | |
| 1672 | 1672 | pub const SuffixOp = struct { |
| 1673 | base: Node, | |
| 1673 | base: Node = Node {.id = .SuffixOp}, | |
| 1674 | 1674 | lhs: Lhs, |
| 1675 | 1675 | op: Op, |
| 1676 | 1676 | rtoken: TokenIndex, |
| ... | ... | @@ -1766,7 +1766,7 @@ pub const Node = struct { |
| 1766 | 1766 | }; |
| 1767 | 1767 | |
| 1768 | 1768 | pub const GroupedExpression = struct { |
| 1769 | base: Node, | |
| 1769 | base: Node = Node {.id = .GroupedExpression}, | |
| 1770 | 1770 | lparen: TokenIndex, |
| 1771 | 1771 | expr: *Node, |
| 1772 | 1772 | rparen: TokenIndex, |
| ... | ... | @@ -1790,7 +1790,7 @@ pub const Node = struct { |
| 1790 | 1790 | }; |
| 1791 | 1791 | |
| 1792 | 1792 | pub const ControlFlowExpression = struct { |
| 1793 | base: Node, | |
| 1793 | base: Node = Node {.id = .ControlFlowExpression}, | |
| 1794 | 1794 | ltoken: TokenIndex, |
| 1795 | 1795 | kind: Kind, |
| 1796 | 1796 | rhs: ?*Node, |
| ... | ... | @@ -1856,7 +1856,7 @@ pub const Node = struct { |
| 1856 | 1856 | }; |
| 1857 | 1857 | |
| 1858 | 1858 | pub const Suspend = struct { |
| 1859 | base: Node, | |
| 1859 | base: Node = Node {.id = .Suspend}, | |
| 1860 | 1860 | suspend_token: TokenIndex, |
| 1861 | 1861 | body: ?*Node, |
| 1862 | 1862 | |
| ... | ... | @@ -1885,7 +1885,7 @@ pub const Node = struct { |
| 1885 | 1885 | }; |
| 1886 | 1886 | |
| 1887 | 1887 | pub const IntegerLiteral = struct { |
| 1888 | base: Node, | |
| 1888 | base: Node = Node {.id = .IntegerLiteral}, | |
| 1889 | 1889 | token: TokenIndex, |
| 1890 | 1890 | |
| 1891 | 1891 | pub fn iterate(self: *IntegerLiteral, index: usize) ?*Node { |
| ... | ... | @@ -1902,7 +1902,7 @@ pub const Node = struct { |
| 1902 | 1902 | }; |
| 1903 | 1903 | |
| 1904 | 1904 | pub const EnumLiteral = struct { |
| 1905 | base: Node, | |
| 1905 | base: Node = Node {.id = .EnumLiteral}, | |
| 1906 | 1906 | dot: TokenIndex, |
| 1907 | 1907 | name: TokenIndex, |
| 1908 | 1908 | |
| ... | ... | @@ -1920,7 +1920,7 @@ pub const Node = struct { |
| 1920 | 1920 | }; |
| 1921 | 1921 | |
| 1922 | 1922 | pub const FloatLiteral = struct { |
| 1923 | base: Node, | |
| 1923 | base: Node = Node {.id = .FloatLiteral}, | |
| 1924 | 1924 | token: TokenIndex, |
| 1925 | 1925 | |
| 1926 | 1926 | pub fn iterate(self: *FloatLiteral, index: usize) ?*Node { |
| ... | ... | @@ -1937,7 +1937,7 @@ pub const Node = struct { |
| 1937 | 1937 | }; |
| 1938 | 1938 | |
| 1939 | 1939 | pub const BuiltinCall = struct { |
| 1940 | base: Node, | |
| 1940 | base: Node = Node {.id = .BuiltinCall}, | |
| 1941 | 1941 | builtin_token: TokenIndex, |
| 1942 | 1942 | params: ParamList, |
| 1943 | 1943 | rparen_token: TokenIndex, |
| ... | ... | @@ -1963,7 +1963,7 @@ pub const Node = struct { |
| 1963 | 1963 | }; |
| 1964 | 1964 | |
| 1965 | 1965 | pub const StringLiteral = struct { |
| 1966 | base: Node, | |
| 1966 | base: Node = Node {.id = .StringLiteral}, | |
| 1967 | 1967 | token: TokenIndex, |
| 1968 | 1968 | |
| 1969 | 1969 | pub fn iterate(self: *StringLiteral, index: usize) ?*Node { |
| ... | ... | @@ -1980,7 +1980,7 @@ pub const Node = struct { |
| 1980 | 1980 | }; |
| 1981 | 1981 | |
| 1982 | 1982 | pub const MultilineStringLiteral = struct { |
| 1983 | base: Node, | |
| 1983 | base: Node = Node {.id = .MultilineStringLiteral}, | |
| 1984 | 1984 | lines: LineList, |
| 1985 | 1985 | |
| 1986 | 1986 | pub const LineList = SegmentedList(TokenIndex, 4); |
| ... | ... | @@ -1999,7 +1999,7 @@ pub const Node = struct { |
| 1999 | 1999 | }; |
| 2000 | 2000 | |
| 2001 | 2001 | pub const CharLiteral = struct { |
| 2002 | base: Node, | |
| 2002 | base: Node = Node {.id = .CharLiteral}, | |
| 2003 | 2003 | token: TokenIndex, |
| 2004 | 2004 | |
| 2005 | 2005 | pub fn iterate(self: *CharLiteral, index: usize) ?*Node { |
| ... | ... | @@ -2016,7 +2016,7 @@ pub const Node = struct { |
| 2016 | 2016 | }; |
| 2017 | 2017 | |
| 2018 | 2018 | pub const BoolLiteral = struct { |
| 2019 | base: Node, | |
| 2019 | base: Node = Node {.id = .BoolLiteral}, | |
| 2020 | 2020 | token: TokenIndex, |
| 2021 | 2021 | |
| 2022 | 2022 | pub fn iterate(self: *BoolLiteral, index: usize) ?*Node { |
| ... | ... | @@ -2033,7 +2033,7 @@ pub const Node = struct { |
| 2033 | 2033 | }; |
| 2034 | 2034 | |
| 2035 | 2035 | pub const NullLiteral = struct { |
| 2036 | base: Node, | |
| 2036 | base: Node = Node {.id = .NullLiteral}, | |
| 2037 | 2037 | token: TokenIndex, |
| 2038 | 2038 | |
| 2039 | 2039 | pub fn iterate(self: *NullLiteral, index: usize) ?*Node { |
| ... | ... | @@ -2050,7 +2050,7 @@ pub const Node = struct { |
| 2050 | 2050 | }; |
| 2051 | 2051 | |
| 2052 | 2052 | pub const UndefinedLiteral = struct { |
| 2053 | base: Node, | |
| 2053 | base: Node = Node {.id = .UndefinedLiteral}, | |
| 2054 | 2054 | token: TokenIndex, |
| 2055 | 2055 | |
| 2056 | 2056 | pub fn iterate(self: *UndefinedLiteral, index: usize) ?*Node { |
| ... | ... | @@ -2067,7 +2067,7 @@ pub const Node = struct { |
| 2067 | 2067 | }; |
| 2068 | 2068 | |
| 2069 | 2069 | pub const AsmOutput = struct { |
| 2070 | base: Node, | |
| 2070 | base: Node = Node {.id = .AsmOutput}, | |
| 2071 | 2071 | lbracket: TokenIndex, |
| 2072 | 2072 | symbolic_name: *Node, |
| 2073 | 2073 | constraint: *Node, |
| ... | ... | @@ -2112,7 +2112,7 @@ pub const Node = struct { |
| 2112 | 2112 | }; |
| 2113 | 2113 | |
| 2114 | 2114 | pub const AsmInput = struct { |
| 2115 | base: Node, | |
| 2115 | base: Node = Node {.id = .AsmInput}, | |
| 2116 | 2116 | lbracket: TokenIndex, |
| 2117 | 2117 | symbolic_name: *Node, |
| 2118 | 2118 | constraint: *Node, |
| ... | ... | @@ -2144,7 +2144,7 @@ pub const Node = struct { |
| 2144 | 2144 | }; |
| 2145 | 2145 | |
| 2146 | 2146 | pub const Asm = struct { |
| 2147 | base: Node, | |
| 2147 | base: Node = Node {.id = .Asm}, | |
| 2148 | 2148 | asm_token: TokenIndex, |
| 2149 | 2149 | volatile_token: ?TokenIndex, |
| 2150 | 2150 | template: *Node, |
| ... | ... | @@ -2179,7 +2179,7 @@ pub const Node = struct { |
| 2179 | 2179 | }; |
| 2180 | 2180 | |
| 2181 | 2181 | pub const Unreachable = struct { |
| 2182 | base: Node, | |
| 2182 | base: Node = Node {.id = .Unreachable}, | |
| 2183 | 2183 | token: TokenIndex, |
| 2184 | 2184 | |
| 2185 | 2185 | pub fn iterate(self: *Unreachable, index: usize) ?*Node { |
| ... | ... | @@ -2196,7 +2196,7 @@ pub const Node = struct { |
| 2196 | 2196 | }; |
| 2197 | 2197 | |
| 2198 | 2198 | pub const ErrorType = struct { |
| 2199 | base: Node, | |
| 2199 | base: Node = Node {.id = .ErrorType}, | |
| 2200 | 2200 | token: TokenIndex, |
| 2201 | 2201 | |
| 2202 | 2202 | pub fn iterate(self: *ErrorType, index: usize) ?*Node { |
| ... | ... | @@ -2230,7 +2230,7 @@ pub const Node = struct { |
| 2230 | 2230 | }; |
| 2231 | 2231 | |
| 2232 | 2232 | pub const DocComment = struct { |
| 2233 | base: Node, | |
| 2233 | base: Node = Node {.id = .DocComment}, | |
| 2234 | 2234 | lines: LineList, |
| 2235 | 2235 | |
| 2236 | 2236 | pub const LineList = SegmentedList(TokenIndex, 4); |
| ... | ... | @@ -2249,7 +2249,7 @@ pub const Node = struct { |
| 2249 | 2249 | }; |
| 2250 | 2250 | |
| 2251 | 2251 | pub const TestDecl = struct { |
| 2252 | base: Node, | |
| 2252 | base: Node = Node {.id = .TestDecl}, | |
| 2253 | 2253 | doc_comments: ?*DocComment, |
| 2254 | 2254 | test_token: TokenIndex, |
| 2255 | 2255 | name: *Node, |
lib/std/zig/parse.zig-72| ... | ... | @@ -56,7 +56,6 @@ pub fn parse(allocator: *Allocator, source: []const u8) !*Tree { |
| 56 | 56 | fn parseRoot(arena: *Allocator, it: *TokenIterator, tree: *Tree) Allocator.Error!*Node.Root { |
| 57 | 57 | const node = try arena.create(Node.Root); |
| 58 | 58 | node.* = Node.Root{ |
| 59 | .base = Node{ .id = .Root }, | |
| 60 | 59 | .decls = undefined, |
| 61 | 60 | .eof_token = undefined, |
| 62 | 61 | }; |
| ... | ... | @@ -176,7 +175,6 @@ fn parseContainerDocComments(arena: *Allocator, it: *TokenIterator, tree: *Tree) |
| 176 | 175 | |
| 177 | 176 | const node = try arena.create(Node.DocComment); |
| 178 | 177 | node.* = Node.DocComment{ |
| 179 | .base = Node{ .id = .DocComment }, | |
| 180 | 178 | .lines = lines, |
| 181 | 179 | }; |
| 182 | 180 | return &node.base; |
| ... | ... | @@ -194,7 +192,6 @@ fn parseTestDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 194 | 192 | |
| 195 | 193 | const test_node = try arena.create(Node.TestDecl); |
| 196 | 194 | test_node.* = Node.TestDecl{ |
| 197 | .base = Node{ .id = .TestDecl }, | |
| 198 | 195 | .doc_comments = null, |
| 199 | 196 | .test_token = test_token, |
| 200 | 197 | .name = name_node, |
| ... | ... | @@ -217,7 +214,6 @@ fn parseTopLevelComptime(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?* |
| 217 | 214 | |
| 218 | 215 | const comptime_node = try arena.create(Node.Comptime); |
| 219 | 216 | comptime_node.* = Node.Comptime{ |
| 220 | .base = Node{ .id = .Comptime }, | |
| 221 | 217 | .doc_comments = null, |
| 222 | 218 | .comptime_token = tok, |
| 223 | 219 | .expr = block_node, |
| ... | ... | @@ -338,7 +334,6 @@ fn parseFnProto(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 338 | 334 | |
| 339 | 335 | const fn_proto_node = try arena.create(Node.FnProto); |
| 340 | 336 | fn_proto_node.* = Node.FnProto{ |
| 341 | .base = Node{ .id = .FnProto }, | |
| 342 | 337 | .doc_comments = null, |
| 343 | 338 | .visib_token = null, |
| 344 | 339 | .fn_token = fn_token, |
| ... | ... | @@ -389,7 +384,6 @@ fn parseVarDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 389 | 384 | |
| 390 | 385 | const node = try arena.create(Node.VarDecl); |
| 391 | 386 | node.* = Node.VarDecl{ |
| 392 | .base = Node{ .id = .VarDecl }, | |
| 393 | 387 | .doc_comments = null, |
| 394 | 388 | .visib_token = null, |
| 395 | 389 | .thread_local_token = null, |
| ... | ... | @@ -477,7 +471,6 @@ fn parseStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) Error!?*No |
| 477 | 471 | |
| 478 | 472 | const node = try arena.create(Node.Comptime); |
| 479 | 473 | node.* = Node.Comptime{ |
| 480 | .base = Node{ .id = .Comptime }, | |
| 481 | 474 | .doc_comments = null, |
| 482 | 475 | .comptime_token = token, |
| 483 | 476 | .expr = block_expr, |
| ... | ... | @@ -496,7 +489,6 @@ fn parseStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) Error!?*No |
| 496 | 489 | |
| 497 | 490 | const node = try arena.create(Node.Suspend); |
| 498 | 491 | node.* = Node.Suspend{ |
| 499 | .base = Node{ .id = .Suspend }, | |
| 500 | 492 | .suspend_token = suspend_token, |
| 501 | 493 | .body = body_node, |
| 502 | 494 | }; |
| ... | ... | @@ -510,7 +502,6 @@ fn parseStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) Error!?*No |
| 510 | 502 | }); |
| 511 | 503 | const node = try arena.create(Node.Defer); |
| 512 | 504 | node.* = Node.Defer{ |
| 513 | .base = Node{ .id = .Defer }, | |
| 514 | 505 | .defer_token = token, |
| 515 | 506 | .expr = expr_node, |
| 516 | 507 | }; |
| ... | ... | @@ -558,7 +549,6 @@ fn parseIfStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 558 | 549 | |
| 559 | 550 | const node = try arena.create(Node.Else); |
| 560 | 551 | node.* = Node.Else{ |
| 561 | .base = Node{ .id = .Else }, | |
| 562 | 552 | .else_token = else_token, |
| 563 | 553 | .payload = payload, |
| 564 | 554 | .body = else_body, |
| ... | ... | @@ -652,7 +642,6 @@ fn parseForStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 652 | 642 | |
| 653 | 643 | const else_node = try arena.create(Node.Else); |
| 654 | 644 | else_node.* = Node.Else{ |
| 655 | .base = Node{ .id = .Else }, | |
| 656 | 645 | .else_token = else_token, |
| 657 | 646 | .payload = null, |
| 658 | 647 | .body = statement_node, |
| ... | ... | @@ -677,7 +666,6 @@ fn parseForStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 677 | 666 | |
| 678 | 667 | const else_node = try arena.create(Node.Else); |
| 679 | 668 | else_node.* = Node.Else{ |
| 680 | .base = Node{ .id = .Else }, | |
| 681 | 669 | .else_token = else_token, |
| 682 | 670 | .payload = null, |
| 683 | 671 | .body = statement_node, |
| ... | ... | @@ -714,7 +702,6 @@ fn parseWhileStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No |
| 714 | 702 | |
| 715 | 703 | const else_node = try arena.create(Node.Else); |
| 716 | 704 | else_node.* = Node.Else{ |
| 717 | .base = Node{ .id = .Else }, | |
| 718 | 705 | .else_token = else_token, |
| 719 | 706 | .payload = payload, |
| 720 | 707 | .body = statement_node, |
| ... | ... | @@ -741,7 +728,6 @@ fn parseWhileStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No |
| 741 | 728 | |
| 742 | 729 | const else_node = try arena.create(Node.Else); |
| 743 | 730 | else_node.* = Node.Else{ |
| 744 | .base = Node{ .id = .Else }, | |
| 745 | 731 | .else_token = else_token, |
| 746 | 732 | .payload = payload, |
| 747 | 733 | .body = statement_node, |
| ... | ... | @@ -870,7 +856,6 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 870 | 856 | const expr_node = try parseExpr(arena, it, tree); |
| 871 | 857 | const node = try arena.create(Node.ControlFlowExpression); |
| 872 | 858 | node.* = Node.ControlFlowExpression{ |
| 873 | .base = Node{ .id = .ControlFlowExpression }, | |
| 874 | 859 | .ltoken = token, |
| 875 | 860 | .kind = Node.ControlFlowExpression.Kind{ .Break = label }, |
| 876 | 861 | .rhs = expr_node, |
| ... | ... | @@ -884,7 +869,6 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 884 | 869 | }); |
| 885 | 870 | const node = try arena.create(Node.Comptime); |
| 886 | 871 | node.* = Node.Comptime{ |
| 887 | .base = Node{ .id = .Comptime }, | |
| 888 | 872 | .doc_comments = null, |
| 889 | 873 | .comptime_token = token, |
| 890 | 874 | .expr = expr_node, |
| ... | ... | @@ -896,7 +880,6 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 896 | 880 | const label = try parseBreakLabel(arena, it, tree); |
| 897 | 881 | const node = try arena.create(Node.ControlFlowExpression); |
| 898 | 882 | node.* = Node.ControlFlowExpression{ |
| 899 | .base = Node{ .id = .ControlFlowExpression }, | |
| 900 | 883 | .ltoken = token, |
| 901 | 884 | .kind = Node.ControlFlowExpression.Kind{ .Continue = label }, |
| 902 | 885 | .rhs = null, |
| ... | ... | @@ -910,7 +893,6 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 910 | 893 | }); |
| 911 | 894 | const node = try arena.create(Node.PrefixOp); |
| 912 | 895 | node.* = Node.PrefixOp{ |
| 913 | .base = Node{ .id = .PrefixOp }, | |
| 914 | 896 | .op_token = token, |
| 915 | 897 | .op = Node.PrefixOp.Op.Resume, |
| 916 | 898 | .rhs = expr_node, |
| ... | ... | @@ -922,7 +904,6 @@ fn parsePrimaryExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 922 | 904 | const expr_node = try parseExpr(arena, it, tree); |
| 923 | 905 | const node = try arena.create(Node.ControlFlowExpression); |
| 924 | 906 | node.* = Node.ControlFlowExpression{ |
| 925 | .base = Node{ .id = .ControlFlowExpression }, | |
| 926 | 907 | .ltoken = token, |
| 927 | 908 | .kind = Node.ControlFlowExpression.Kind.Return, |
| 928 | 909 | .rhs = expr_node, |
| ... | ... | @@ -970,7 +951,6 @@ fn parseBlock(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 970 | 951 | |
| 971 | 952 | const block_node = try arena.create(Node.Block); |
| 972 | 953 | block_node.* = Node.Block{ |
| 973 | .base = Node{ .id = .Block }, | |
| 974 | 954 | .label = null, |
| 975 | 955 | .lbrace = lbrace, |
| 976 | 956 | .statements = statements, |
| ... | ... | @@ -1020,7 +1000,6 @@ fn parseForExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1020 | 1000 | |
| 1021 | 1001 | const else_node = try arena.create(Node.Else); |
| 1022 | 1002 | else_node.* = Node.Else{ |
| 1023 | .base = Node{ .id = .Else }, | |
| 1024 | 1003 | .else_token = else_token, |
| 1025 | 1004 | .payload = null, |
| 1026 | 1005 | .body = body, |
| ... | ... | @@ -1050,7 +1029,6 @@ fn parseWhileExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1050 | 1029 | |
| 1051 | 1030 | const else_node = try arena.create(Node.Else); |
| 1052 | 1031 | else_node.* = Node.Else{ |
| 1053 | .base = Node{ .id = .Else }, | |
| 1054 | 1032 | .else_token = else_token, |
| 1055 | 1033 | .payload = payload, |
| 1056 | 1034 | .body = body, |
| ... | ... | @@ -1102,7 +1080,6 @@ fn parseInitList(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node.Suf |
| 1102 | 1080 | |
| 1103 | 1081 | const node = try arena.create(Node.SuffixOp); |
| 1104 | 1082 | node.* = Node.SuffixOp{ |
| 1105 | .base = Node{ .id = .SuffixOp }, | |
| 1106 | 1083 | .lhs = .{ .node = undefined }, // set by caller |
| 1107 | 1084 | .op = op, |
| 1108 | 1085 | .rtoken = try expectToken(it, tree, .RBrace), |
| ... | ... | @@ -1171,7 +1148,6 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1171 | 1148 | }; |
| 1172 | 1149 | const node = try arena.create(Node.SuffixOp); |
| 1173 | 1150 | node.* = Node.SuffixOp{ |
| 1174 | .base = Node{ .id = .SuffixOp }, | |
| 1175 | 1151 | .lhs = .{ .node = res }, |
| 1176 | 1152 | .op = Node.SuffixOp.Op{ |
| 1177 | 1153 | .Call = Node.SuffixOp.Op.Call{ |
| ... | ... | @@ -1199,7 +1175,6 @@ fn parseSuffixExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1199 | 1175 | if (try parseFnCallArguments(arena, it, tree)) |params| { |
| 1200 | 1176 | const call = try arena.create(Node.SuffixOp); |
| 1201 | 1177 | call.* = Node.SuffixOp{ |
| 1202 | .base = Node{ .id = .SuffixOp }, | |
| 1203 | 1178 | .lhs = .{ .node = res }, |
| 1204 | 1179 | .op = Node.SuffixOp.Op{ |
| 1205 | 1180 | .Call = Node.SuffixOp.Op.Call{ |
| ... | ... | @@ -1248,7 +1223,6 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N |
| 1248 | 1223 | if (eatToken(it, .CharLiteral)) |token| { |
| 1249 | 1224 | const node = try arena.create(Node.CharLiteral); |
| 1250 | 1225 | node.* = Node.CharLiteral{ |
| 1251 | .base = Node{ .id = .CharLiteral }, | |
| 1252 | 1226 | .token = token, |
| 1253 | 1227 | }; |
| 1254 | 1228 | return &node.base; |
| ... | ... | @@ -1267,7 +1241,6 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N |
| 1267 | 1241 | const expr = (try parseTypeExpr(arena, it, tree)) orelse return null; |
| 1268 | 1242 | const node = try arena.create(Node.Comptime); |
| 1269 | 1243 | node.* = Node.Comptime{ |
| 1270 | .base = Node{ .id = .Comptime }, | |
| 1271 | 1244 | .doc_comments = null, |
| 1272 | 1245 | .comptime_token = token, |
| 1273 | 1246 | .expr = expr, |
| ... | ... | @@ -1282,7 +1255,6 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N |
| 1282 | 1255 | const global_error_set = try createLiteral(arena, Node.ErrorType, token); |
| 1283 | 1256 | const node = try arena.create(Node.InfixOp); |
| 1284 | 1257 | node.* = Node.InfixOp{ |
| 1285 | .base = Node{ .id = .InfixOp }, | |
| 1286 | 1258 | .op_token = period, |
| 1287 | 1259 | .lhs = global_error_set, |
| 1288 | 1260 | .op = Node.InfixOp.Op.Period, |
| ... | ... | @@ -1295,7 +1267,6 @@ fn parsePrimaryTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N |
| 1295 | 1267 | if (eatToken(it, .Keyword_anyframe)) |token| { |
| 1296 | 1268 | const node = try arena.create(Node.AnyFrameType); |
| 1297 | 1269 | node.* = Node.AnyFrameType{ |
| 1298 | .base = Node{ .id = .AnyFrameType }, | |
| 1299 | 1270 | .anyframe_token = token, |
| 1300 | 1271 | .result = null, |
| 1301 | 1272 | }; |
| ... | ... | @@ -1337,7 +1308,6 @@ fn parseErrorSetDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1337 | 1308 | |
| 1338 | 1309 | const node = try arena.create(Node.ErrorSetDecl); |
| 1339 | 1310 | node.* = Node.ErrorSetDecl{ |
| 1340 | .base = Node{ .id = .ErrorSetDecl }, | |
| 1341 | 1311 | .error_token = error_token, |
| 1342 | 1312 | .decls = decls, |
| 1343 | 1313 | .rbrace_token = rbrace, |
| ... | ... | @@ -1355,7 +1325,6 @@ fn parseGroupedExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1355 | 1325 | |
| 1356 | 1326 | const node = try arena.create(Node.GroupedExpression); |
| 1357 | 1327 | node.* = Node.GroupedExpression{ |
| 1358 | .base = Node{ .id = .GroupedExpression }, | |
| 1359 | 1328 | .lparen = lparen, |
| 1360 | 1329 | .expr = expr, |
| 1361 | 1330 | .rparen = rparen, |
| ... | ... | @@ -1438,7 +1407,6 @@ fn parseForTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1438 | 1407 | |
| 1439 | 1408 | const else_node = try arena.create(Node.Else); |
| 1440 | 1409 | else_node.* = Node.Else{ |
| 1441 | .base = Node{ .id = .Else }, | |
| 1442 | 1410 | .else_token = else_token, |
| 1443 | 1411 | .payload = null, |
| 1444 | 1412 | .body = else_expr, |
| ... | ... | @@ -1469,7 +1437,6 @@ fn parseWhileTypeExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Nod |
| 1469 | 1437 | |
| 1470 | 1438 | const else_node = try arena.create(Node.Else); |
| 1471 | 1439 | else_node.* = Node.Else{ |
| 1472 | .base = Node{ .id = .Else }, | |
| 1473 | 1440 | .else_token = else_token, |
| 1474 | 1441 | .payload = null, |
| 1475 | 1442 | .body = else_expr, |
| ... | ... | @@ -1495,7 +1462,6 @@ fn parseSwitchExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1495 | 1462 | |
| 1496 | 1463 | const node = try arena.create(Node.Switch); |
| 1497 | 1464 | node.* = Node.Switch{ |
| 1498 | .base = Node{ .id = .Switch }, | |
| 1499 | 1465 | .switch_token = switch_token, |
| 1500 | 1466 | .expr = expr_node, |
| 1501 | 1467 | .cases = cases, |
| ... | ... | @@ -1515,7 +1481,6 @@ fn parseAsmExpr(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1515 | 1481 | |
| 1516 | 1482 | const node = try arena.create(Node.Asm); |
| 1517 | 1483 | node.* = Node.Asm{ |
| 1518 | .base = Node{ .id = .Asm }, | |
| 1519 | 1484 | .asm_token = asm_token, |
| 1520 | 1485 | .volatile_token = volatile_token, |
| 1521 | 1486 | .template = template, |
| ... | ... | @@ -1538,7 +1503,6 @@ fn parseAnonLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1538 | 1503 | if (eatToken(it, .Identifier)) |name| { |
| 1539 | 1504 | const node = try arena.create(Node.EnumLiteral); |
| 1540 | 1505 | node.* = Node.EnumLiteral{ |
| 1541 | .base = Node{ .id = .EnumLiteral }, | |
| 1542 | 1506 | .dot = dot, |
| 1543 | 1507 | .name = name, |
| 1544 | 1508 | }; |
| ... | ... | @@ -1591,7 +1555,6 @@ fn parseAsmOutputItem(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Nod |
| 1591 | 1555 | |
| 1592 | 1556 | const node = try arena.create(Node.AsmOutput); |
| 1593 | 1557 | node.* = Node.AsmOutput{ |
| 1594 | .base = Node{ .id = .AsmOutput }, | |
| 1595 | 1558 | .lbracket = lbracket, |
| 1596 | 1559 | .symbolic_name = name, |
| 1597 | 1560 | .constraint = constraint, |
| ... | ... | @@ -1628,7 +1591,6 @@ fn parseAsmInputItem(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1628 | 1591 | |
| 1629 | 1592 | const node = try arena.create(Node.AsmInput); |
| 1630 | 1593 | node.* = Node.AsmInput{ |
| 1631 | .base = Node{ .id = .AsmInput }, | |
| 1632 | 1594 | .lbracket = lbracket, |
| 1633 | 1595 | .symbolic_name = name, |
| 1634 | 1596 | .constraint = constraint, |
| ... | ... | @@ -1687,7 +1649,6 @@ fn parseFieldInit(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1687 | 1649 | |
| 1688 | 1650 | const node = try arena.create(Node.FieldInitializer); |
| 1689 | 1651 | node.* = Node.FieldInitializer{ |
| 1690 | .base = Node{ .id = .FieldInitializer }, | |
| 1691 | 1652 | .period_token = period_token, |
| 1692 | 1653 | .name_token = name_token, |
| 1693 | 1654 | .expr = expr_node, |
| ... | ... | @@ -1760,7 +1721,6 @@ fn parseParamDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1760 | 1721 | |
| 1761 | 1722 | const param_decl = try arena.create(Node.ParamDecl); |
| 1762 | 1723 | param_decl.* = Node.ParamDecl{ |
| 1763 | .base = Node{ .id = .ParamDecl }, | |
| 1764 | 1724 | .doc_comments = doc_comments, |
| 1765 | 1725 | .comptime_token = comptime_token, |
| 1766 | 1726 | .noalias_token = noalias_token, |
| ... | ... | @@ -1807,7 +1767,6 @@ fn parseIfPrefix(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1807 | 1767 | |
| 1808 | 1768 | const node = try arena.create(Node.If); |
| 1809 | 1769 | node.* = Node.If{ |
| 1810 | .base = Node{ .id = .If }, | |
| 1811 | 1770 | .if_token = if_token, |
| 1812 | 1771 | .condition = condition, |
| 1813 | 1772 | .payload = payload, |
| ... | ... | @@ -1832,7 +1791,6 @@ fn parseWhilePrefix(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 1832 | 1791 | |
| 1833 | 1792 | const node = try arena.create(Node.While); |
| 1834 | 1793 | node.* = Node.While{ |
| 1835 | .base = Node{ .id = .While }, | |
| 1836 | 1794 | .label = null, |
| 1837 | 1795 | .inline_token = null, |
| 1838 | 1796 | .while_token = while_token, |
| ... | ... | @@ -1861,7 +1819,6 @@ fn parseForPrefix(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1861 | 1819 | |
| 1862 | 1820 | const node = try arena.create(Node.For); |
| 1863 | 1821 | node.* = Node.For{ |
| 1864 | .base = Node{ .id = .For }, | |
| 1865 | 1822 | .label = null, |
| 1866 | 1823 | .inline_token = null, |
| 1867 | 1824 | .for_token = for_token, |
| ... | ... | @@ -1883,7 +1840,6 @@ fn parsePayload(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1883 | 1840 | |
| 1884 | 1841 | const node = try arena.create(Node.Payload); |
| 1885 | 1842 | node.* = Node.Payload{ |
| 1886 | .base = Node{ .id = .Payload }, | |
| 1887 | 1843 | .lpipe = lpipe, |
| 1888 | 1844 | .error_symbol = identifier, |
| 1889 | 1845 | .rpipe = rpipe, |
| ... | ... | @@ -1902,7 +1858,6 @@ fn parsePtrPayload(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1902 | 1858 | |
| 1903 | 1859 | const node = try arena.create(Node.PointerPayload); |
| 1904 | 1860 | node.* = Node.PointerPayload{ |
| 1905 | .base = Node{ .id = .PointerPayload }, | |
| 1906 | 1861 | .lpipe = lpipe, |
| 1907 | 1862 | .ptr_token = asterisk, |
| 1908 | 1863 | .value_symbol = identifier, |
| ... | ... | @@ -1930,7 +1885,6 @@ fn parsePtrIndexPayload(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*N |
| 1930 | 1885 | |
| 1931 | 1886 | const node = try arena.create(Node.PointerIndexPayload); |
| 1932 | 1887 | node.* = Node.PointerIndexPayload{ |
| 1933 | .base = Node{ .id = .PointerIndexPayload }, | |
| 1934 | 1888 | .lpipe = lpipe, |
| 1935 | 1889 | .ptr_token = asterisk, |
| 1936 | 1890 | .value_symbol = identifier, |
| ... | ... | @@ -1972,7 +1926,6 @@ fn parseSwitchCase(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1972 | 1926 | } else if (eatToken(it, .Keyword_else)) |else_token| { |
| 1973 | 1927 | const else_node = try arena.create(Node.SwitchElse); |
| 1974 | 1928 | else_node.* = Node.SwitchElse{ |
| 1975 | .base = Node{ .id = .SwitchElse }, | |
| 1976 | 1929 | .token = else_token, |
| 1977 | 1930 | }; |
| 1978 | 1931 | try list.push(&else_node.base); |
| ... | ... | @@ -1980,7 +1933,6 @@ fn parseSwitchCase(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1980 | 1933 | |
| 1981 | 1934 | const node = try arena.create(Node.SwitchCase); |
| 1982 | 1935 | node.* = Node.SwitchCase{ |
| 1983 | .base = Node{ .id = .SwitchCase }, | |
| 1984 | 1936 | .items = list, |
| 1985 | 1937 | .arrow_token = undefined, // set by caller |
| 1986 | 1938 | .payload = null, |
| ... | ... | @@ -1999,7 +1951,6 @@ fn parseSwitchItem(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 1999 | 1951 | |
| 2000 | 1952 | const node = try arena.create(Node.InfixOp); |
| 2001 | 1953 | node.* = Node.InfixOp{ |
| 2002 | .base = Node{ .id = .InfixOp }, | |
| 2003 | 1954 | .op_token = token, |
| 2004 | 1955 | .lhs = expr, |
| 2005 | 1956 | .op = Node.InfixOp.Op{ .Range = {} }, |
| ... | ... | @@ -2052,7 +2003,6 @@ fn parseAssignOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2052 | 2003 | |
| 2053 | 2004 | const node = try arena.create(Node.InfixOp); |
| 2054 | 2005 | node.* = Node.InfixOp{ |
| 2055 | .base = Node{ .id = .InfixOp }, | |
| 2056 | 2006 | .op_token = token.index, |
| 2057 | 2007 | .lhs = undefined, // set by caller |
| 2058 | 2008 | .op = op, |
| ... | ... | @@ -2212,7 +2162,6 @@ fn parsePrefixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2212 | 2162 | |
| 2213 | 2163 | const node = try arena.create(Node.PrefixOp); |
| 2214 | 2164 | node.* = Node.PrefixOp{ |
| 2215 | .base = Node{ .id = .PrefixOp }, | |
| 2216 | 2165 | .op_token = token.index, |
| 2217 | 2166 | .op = op, |
| 2218 | 2167 | .rhs = undefined, |
| ... | ... | @@ -2236,7 +2185,6 @@ fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 2236 | 2185 | if (eatToken(it, .QuestionMark)) |token| { |
| 2237 | 2186 | const node = try arena.create(Node.PrefixOp); |
| 2238 | 2187 | node.* = Node.PrefixOp{ |
| 2239 | .base = Node{ .id = .PrefixOp }, | |
| 2240 | 2188 | .op_token = token, |
| 2241 | 2189 | .op = Node.PrefixOp.Op.OptionalType, |
| 2242 | 2190 | .rhs = undefined, // set by caller |
| ... | ... | @@ -2255,7 +2203,6 @@ fn parsePrefixTypeOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 2255 | 2203 | }; |
| 2256 | 2204 | const node = try arena.create(Node.AnyFrameType); |
| 2257 | 2205 | node.* = Node.AnyFrameType{ |
| 2258 | .base = Node{ .id = .AnyFrameType }, | |
| 2259 | 2206 | .anyframe_token = token, |
| 2260 | 2207 | .result = Node.AnyFrameType.Result{ |
| 2261 | 2208 | .arrow_token = arrow, |
| ... | ... | @@ -2430,7 +2377,6 @@ fn parseSuffixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2430 | 2377 | // this grammar rule be altered? |
| 2431 | 2378 | const node = try arena.create(Node.InfixOp); |
| 2432 | 2379 | node.* = Node.InfixOp{ |
| 2433 | .base = Node{ .id = .InfixOp }, | |
| 2434 | 2380 | .op_token = period, |
| 2435 | 2381 | .lhs = undefined, // set by caller |
| 2436 | 2382 | .op = Node.InfixOp.Op.Period, |
| ... | ... | @@ -2452,7 +2398,6 @@ fn parseSuffixOp(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2452 | 2398 | |
| 2453 | 2399 | const node = try arena.create(Node.SuffixOp); |
| 2454 | 2400 | node.* = Node.SuffixOp{ |
| 2455 | .base = Node{ .id = .SuffixOp }, | |
| 2456 | 2401 | .lhs = undefined, // set by caller |
| 2457 | 2402 | .op = op_and_token.op, |
| 2458 | 2403 | .rtoken = op_and_token.token, |
| ... | ... | @@ -2506,7 +2451,6 @@ fn parseArrayTypeStart(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No |
| 2506 | 2451 | |
| 2507 | 2452 | const node = try arena.create(Node.PrefixOp); |
| 2508 | 2453 | node.* = Node.PrefixOp{ |
| 2509 | .base = Node{ .id = .PrefixOp }, | |
| 2510 | 2454 | .op_token = lbracket, |
| 2511 | 2455 | .op = op, |
| 2512 | 2456 | .rhs = undefined, // set by caller |
| ... | ... | @@ -2656,7 +2600,6 @@ fn parseContainerDeclType(arena: *Allocator, it: *TokenIterator, tree: *Tree) !? |
| 2656 | 2600 | |
| 2657 | 2601 | const node = try arena.create(Node.ContainerDecl); |
| 2658 | 2602 | node.* = Node.ContainerDecl{ |
| 2659 | .base = Node{ .id = .ContainerDecl }, | |
| 2660 | 2603 | .layout_token = null, |
| 2661 | 2604 | .kind_token = kind_token.index, |
| 2662 | 2605 | .init_arg_expr = init_arg_expr, |
| ... | ... | @@ -2729,7 +2672,6 @@ fn SimpleBinOpParseFn(comptime token: Token.Id, comptime op: Node.InfixOp.Op) No |
| 2729 | 2672 | const op_token = eatToken(it, token) orelse return null; |
| 2730 | 2673 | const node = try arena.create(Node.InfixOp); |
| 2731 | 2674 | node.* = Node.InfixOp{ |
| 2732 | .base = Node{ .id = .InfixOp }, | |
| 2733 | 2675 | .op_token = op_token, |
| 2734 | 2676 | .lhs = undefined, // set by caller |
| 2735 | 2677 | .op = op, |
| ... | ... | @@ -2752,7 +2694,6 @@ fn parseBuiltinCall(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 2752 | 2694 | }; |
| 2753 | 2695 | const node = try arena.create(Node.BuiltinCall); |
| 2754 | 2696 | node.* = Node.BuiltinCall{ |
| 2755 | .base = Node{ .id = .BuiltinCall }, | |
| 2756 | 2697 | .builtin_token = token, |
| 2757 | 2698 | .params = params.list, |
| 2758 | 2699 | .rparen_token = params.rparen, |
| ... | ... | @@ -2766,7 +2707,6 @@ fn parseErrorTag(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2766 | 2707 | |
| 2767 | 2708 | const node = try arena.create(Node.ErrorTag); |
| 2768 | 2709 | node.* = Node.ErrorTag{ |
| 2769 | .base = Node{ .id = .ErrorTag }, | |
| 2770 | 2710 | .doc_comments = doc_comments, |
| 2771 | 2711 | .name_token = token, |
| 2772 | 2712 | }; |
| ... | ... | @@ -2777,7 +2717,6 @@ fn parseIdentifier(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2777 | 2717 | const token = eatToken(it, .Identifier) orelse return null; |
| 2778 | 2718 | const node = try arena.create(Node.Identifier); |
| 2779 | 2719 | node.* = Node.Identifier{ |
| 2780 | .base = Node{ .id = .Identifier }, | |
| 2781 | 2720 | .token = token, |
| 2782 | 2721 | }; |
| 2783 | 2722 | return &node.base; |
| ... | ... | @@ -2787,7 +2726,6 @@ fn parseVarType(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2787 | 2726 | const token = eatToken(it, .Keyword_var) orelse return null; |
| 2788 | 2727 | const node = try arena.create(Node.VarType); |
| 2789 | 2728 | node.* = Node.VarType{ |
| 2790 | .base = Node{ .id = .VarType }, | |
| 2791 | 2729 | .token = token, |
| 2792 | 2730 | }; |
| 2793 | 2731 | return &node.base; |
| ... | ... | @@ -2807,7 +2745,6 @@ fn parseStringLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Nod |
| 2807 | 2745 | if (eatToken(it, .StringLiteral)) |token| { |
| 2808 | 2746 | const node = try arena.create(Node.StringLiteral); |
| 2809 | 2747 | node.* = Node.StringLiteral{ |
| 2810 | .base = Node{ .id = .StringLiteral }, | |
| 2811 | 2748 | .token = token, |
| 2812 | 2749 | }; |
| 2813 | 2750 | return &node.base; |
| ... | ... | @@ -2816,7 +2753,6 @@ fn parseStringLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Nod |
| 2816 | 2753 | if (eatToken(it, .MultilineStringLiteralLine)) |first_line| { |
| 2817 | 2754 | const node = try arena.create(Node.MultilineStringLiteral); |
| 2818 | 2755 | node.* = Node.MultilineStringLiteral{ |
| 2819 | .base = Node{ .id = .MultilineStringLiteral }, | |
| 2820 | 2756 | .lines = Node.MultilineStringLiteral.LineList.init(arena), |
| 2821 | 2757 | }; |
| 2822 | 2758 | try node.lines.push(first_line); |
| ... | ... | @@ -2833,7 +2769,6 @@ fn parseIntegerLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No |
| 2833 | 2769 | const token = eatToken(it, .IntegerLiteral) orelse return null; |
| 2834 | 2770 | const node = try arena.create(Node.IntegerLiteral); |
| 2835 | 2771 | node.* = Node.IntegerLiteral{ |
| 2836 | .base = Node{ .id = .IntegerLiteral }, | |
| 2837 | 2772 | .token = token, |
| 2838 | 2773 | }; |
| 2839 | 2774 | return &node.base; |
| ... | ... | @@ -2843,7 +2778,6 @@ fn parseFloatLiteral(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node |
| 2843 | 2778 | const token = eatToken(it, .FloatLiteral) orelse return null; |
| 2844 | 2779 | const node = try arena.create(Node.FloatLiteral); |
| 2845 | 2780 | node.* = Node.FloatLiteral{ |
| 2846 | .base = Node{ .id = .FloatLiteral }, | |
| 2847 | 2781 | .token = token, |
| 2848 | 2782 | }; |
| 2849 | 2783 | return &node.base; |
| ... | ... | @@ -2853,7 +2787,6 @@ fn parseTry(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2853 | 2787 | const token = eatToken(it, .Keyword_try) orelse return null; |
| 2854 | 2788 | const node = try arena.create(Node.PrefixOp); |
| 2855 | 2789 | node.* = Node.PrefixOp{ |
| 2856 | .base = Node{ .id = .PrefixOp }, | |
| 2857 | 2790 | .op_token = token, |
| 2858 | 2791 | .op = Node.PrefixOp.Op.Try, |
| 2859 | 2792 | .rhs = undefined, // set by caller |
| ... | ... | @@ -2865,7 +2798,6 @@ fn parseUse(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { |
| 2865 | 2798 | const token = eatToken(it, .Keyword_usingnamespace) orelse return null; |
| 2866 | 2799 | const node = try arena.create(Node.Use); |
| 2867 | 2800 | node.* = Node.Use{ |
| 2868 | .base = Node{ .id = .Use }, | |
| 2869 | 2801 | .doc_comments = null, |
| 2870 | 2802 | .visib_token = null, |
| 2871 | 2803 | .use_token = token, |
| ... | ... | @@ -2891,7 +2823,6 @@ fn parseIf(arena: *Allocator, it: *TokenIterator, tree: *Tree, bodyParseFn: Node |
| 2891 | 2823 | }); |
| 2892 | 2824 | const else_node = try arena.create(Node.Else); |
| 2893 | 2825 | else_node.* = Node.Else{ |
| 2894 | .base = Node{ .id = .Else }, | |
| 2895 | 2826 | .else_token = else_token, |
| 2896 | 2827 | .payload = payload, |
| 2897 | 2828 | .body = else_expr, |
| ... | ... | @@ -2912,7 +2843,6 @@ fn parseDocComment(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node.D |
| 2912 | 2843 | |
| 2913 | 2844 | const node = try arena.create(Node.DocComment); |
| 2914 | 2845 | node.* = Node.DocComment{ |
| 2915 | .base = Node{ .id = .DocComment }, | |
| 2916 | 2846 | .lines = lines, |
| 2917 | 2847 | }; |
| 2918 | 2848 | return node; |
| ... | ... | @@ -2924,7 +2854,6 @@ fn parseAppendedDocComment(arena: *Allocator, it: *TokenIterator, tree: *Tree, a |
| 2924 | 2854 | if (tree.tokensOnSameLine(after_token, comment_token)) { |
| 2925 | 2855 | const node = try arena.create(Node.DocComment); |
| 2926 | 2856 | node.* = Node.DocComment{ |
| 2927 | .base = Node{ .id = .DocComment }, | |
| 2928 | 2857 | .lines = Node.DocComment.LineList.init(arena), |
| 2929 | 2858 | }; |
| 2930 | 2859 | try node.lines.push(comment_token); |
| ... | ... | @@ -3031,7 +2960,6 @@ fn parseBinOpExpr( |
| 3031 | 2960 | fn createInfixOp(arena: *Allocator, index: TokenIndex, op: Node.InfixOp.Op) !*Node { |
| 3032 | 2961 | const node = try arena.create(Node.InfixOp); |
| 3033 | 2962 | node.* = Node.InfixOp{ |
| 3034 | .base = Node{ .id = .InfixOp }, | |
| 3035 | 2963 | .op_token = index, |
| 3036 | 2964 | .lhs = undefined, |
| 3037 | 2965 | .op = op, |
lib/std/zig/render.zig+1| ... | ... | @@ -193,6 +193,7 @@ fn renderRoot( |
| 193 | 193 | fn renderExtraNewline(tree: *ast.Tree, stream: var, start_col: *usize, node: *ast.Node) @TypeOf(stream).Child.Error!void { |
| 194 | 194 | const first_token = node.firstToken(); |
| 195 | 195 | var prev_token = first_token; |
| 196 | if (prev_token == 0) return; | |
| 196 | 197 | while (tree.tokens.at(prev_token - 1).id == .DocComment) { |
| 197 | 198 | prev_token -= 1; |
| 198 | 199 | } |
lib/std/zig/tokenizer.zig+64-53| ... | ... | @@ -9,66 +9,77 @@ pub const Token = struct { |
| 9 | 9 | pub const Keyword = struct { |
| 10 | 10 | bytes: []const u8, |
| 11 | 11 | id: Id, |
| 12 | hash: u32, | |
| 13 | ||
| 14 | fn init(bytes: []const u8, id: Id) Keyword { | |
| 15 | @setEvalBranchQuota(2000); | |
| 16 | return .{ | |
| 17 | .bytes = bytes, | |
| 18 | .id = id, | |
| 19 | .hash = std.hash_map.hashString(bytes), | |
| 20 | }; | |
| 21 | } | |
| 12 | 22 | }; |
| 13 | 23 | |
| 14 | 24 | pub const keywords = [_]Keyword{ |
| 15 | Keyword{ .bytes = "align", .id = Id.Keyword_align }, | |
| 16 | Keyword{ .bytes = "allowzero", .id = Id.Keyword_allowzero }, | |
| 17 | Keyword{ .bytes = "and", .id = Id.Keyword_and }, | |
| 18 | Keyword{ .bytes = "anyframe", .id = Id.Keyword_anyframe }, | |
| 19 | Keyword{ .bytes = "asm", .id = Id.Keyword_asm }, | |
| 20 | Keyword{ .bytes = "async", .id = Id.Keyword_async }, | |
| 21 | Keyword{ .bytes = "await", .id = Id.Keyword_await }, | |
| 22 | Keyword{ .bytes = "break", .id = Id.Keyword_break }, | |
| 23 | Keyword{ .bytes = "catch", .id = Id.Keyword_catch }, | |
| 24 | Keyword{ .bytes = "comptime", .id = Id.Keyword_comptime }, | |
| 25 | Keyword{ .bytes = "const", .id = Id.Keyword_const }, | |
| 26 | Keyword{ .bytes = "continue", .id = Id.Keyword_continue }, | |
| 27 | Keyword{ .bytes = "defer", .id = Id.Keyword_defer }, | |
| 28 | Keyword{ .bytes = "else", .id = Id.Keyword_else }, | |
| 29 | Keyword{ .bytes = "enum", .id = Id.Keyword_enum }, | |
| 30 | Keyword{ .bytes = "errdefer", .id = Id.Keyword_errdefer }, | |
| 31 | Keyword{ .bytes = "error", .id = Id.Keyword_error }, | |
| 32 | Keyword{ .bytes = "export", .id = Id.Keyword_export }, | |
| 33 | Keyword{ .bytes = "extern", .id = Id.Keyword_extern }, | |
| 34 | Keyword{ .bytes = "false", .id = Id.Keyword_false }, | |
| 35 | Keyword{ .bytes = "fn", .id = Id.Keyword_fn }, | |
| 36 | Keyword{ .bytes = "for", .id = Id.Keyword_for }, | |
| 37 | Keyword{ .bytes = "if", .id = Id.Keyword_if }, | |
| 38 | Keyword{ .bytes = "inline", .id = Id.Keyword_inline }, | |
| 39 | Keyword{ .bytes = "nakedcc", .id = Id.Keyword_nakedcc }, | |
| 40 | Keyword{ .bytes = "noalias", .id = Id.Keyword_noalias }, | |
| 41 | Keyword{ .bytes = "noasync", .id = Id.Keyword_noasync }, | |
| 42 | Keyword{ .bytes = "noinline", .id = Id.Keyword_noinline }, | |
| 43 | Keyword{ .bytes = "null", .id = Id.Keyword_null }, | |
| 44 | Keyword{ .bytes = "or", .id = Id.Keyword_or }, | |
| 45 | Keyword{ .bytes = "orelse", .id = Id.Keyword_orelse }, | |
| 46 | Keyword{ .bytes = "packed", .id = Id.Keyword_packed }, | |
| 47 | Keyword{ .bytes = "pub", .id = Id.Keyword_pub }, | |
| 48 | Keyword{ .bytes = "resume", .id = Id.Keyword_resume }, | |
| 49 | Keyword{ .bytes = "return", .id = Id.Keyword_return }, | |
| 50 | Keyword{ .bytes = "linksection", .id = Id.Keyword_linksection }, | |
| 51 | Keyword{ .bytes = "stdcallcc", .id = Id.Keyword_stdcallcc }, | |
| 52 | Keyword{ .bytes = "struct", .id = Id.Keyword_struct }, | |
| 53 | Keyword{ .bytes = "suspend", .id = Id.Keyword_suspend }, | |
| 54 | Keyword{ .bytes = "switch", .id = Id.Keyword_switch }, | |
| 55 | Keyword{ .bytes = "test", .id = Id.Keyword_test }, | |
| 56 | Keyword{ .bytes = "threadlocal", .id = Id.Keyword_threadlocal }, | |
| 57 | Keyword{ .bytes = "true", .id = Id.Keyword_true }, | |
| 58 | Keyword{ .bytes = "try", .id = Id.Keyword_try }, | |
| 59 | Keyword{ .bytes = "undefined", .id = Id.Keyword_undefined }, | |
| 60 | Keyword{ .bytes = "union", .id = Id.Keyword_union }, | |
| 61 | Keyword{ .bytes = "unreachable", .id = Id.Keyword_unreachable }, | |
| 62 | Keyword{ .bytes = "usingnamespace", .id = Id.Keyword_usingnamespace }, | |
| 63 | Keyword{ .bytes = "var", .id = Id.Keyword_var }, | |
| 64 | Keyword{ .bytes = "volatile", .id = Id.Keyword_volatile }, | |
| 65 | Keyword{ .bytes = "while", .id = Id.Keyword_while }, | |
| 25 | Keyword.init("align", .Keyword_align), | |
| 26 | Keyword.init("allowzero", .Keyword_allowzero), | |
| 27 | Keyword.init("and", .Keyword_and), | |
| 28 | Keyword.init("anyframe", .Keyword_anyframe), | |
| 29 | Keyword.init("asm", .Keyword_asm), | |
| 30 | Keyword.init("async", .Keyword_async), | |
| 31 | Keyword.init("await", .Keyword_await), | |
| 32 | Keyword.init("break", .Keyword_break), | |
| 33 | Keyword.init("catch", .Keyword_catch), | |
| 34 | Keyword.init("comptime", .Keyword_comptime), | |
| 35 | Keyword.init("const", .Keyword_const), | |
| 36 | Keyword.init("continue", .Keyword_continue), | |
| 37 | Keyword.init("defer", .Keyword_defer), | |
| 38 | Keyword.init("else", .Keyword_else), | |
| 39 | Keyword.init("enum", .Keyword_enum), | |
| 40 | Keyword.init("errdefer", .Keyword_errdefer), | |
| 41 | Keyword.init("error", .Keyword_error), | |
| 42 | Keyword.init("export", .Keyword_export), | |
| 43 | Keyword.init("extern", .Keyword_extern), | |
| 44 | Keyword.init("false", .Keyword_false), | |
| 45 | Keyword.init("fn", .Keyword_fn), | |
| 46 | Keyword.init("for", .Keyword_for), | |
| 47 | Keyword.init("if", .Keyword_if), | |
| 48 | Keyword.init("inline", .Keyword_inline), | |
| 49 | Keyword.init("nakedcc", .Keyword_nakedcc), | |
| 50 | Keyword.init("noalias", .Keyword_noalias), | |
| 51 | Keyword.init("noasync", .Keyword_noasync), | |
| 52 | Keyword.init("noinline", .Keyword_noinline), | |
| 53 | Keyword.init("null", .Keyword_null), | |
| 54 | Keyword.init("or", .Keyword_or), | |
| 55 | Keyword.init("orelse", .Keyword_orelse), | |
| 56 | Keyword.init("packed", .Keyword_packed), | |
| 57 | Keyword.init("pub", .Keyword_pub), | |
| 58 | Keyword.init("resume", .Keyword_resume), | |
| 59 | Keyword.init("return", .Keyword_return), | |
| 60 | Keyword.init("linksection", .Keyword_linksection), | |
| 61 | Keyword.init("stdcallcc", .Keyword_stdcallcc), | |
| 62 | Keyword.init("struct", .Keyword_struct), | |
| 63 | Keyword.init("suspend", .Keyword_suspend), | |
| 64 | Keyword.init("switch", .Keyword_switch), | |
| 65 | Keyword.init("test", .Keyword_test), | |
| 66 | Keyword.init("threadlocal", .Keyword_threadlocal), | |
| 67 | Keyword.init("true", .Keyword_true), | |
| 68 | Keyword.init("try", .Keyword_try), | |
| 69 | Keyword.init("undefined", .Keyword_undefined), | |
| 70 | Keyword.init("union", .Keyword_union), | |
| 71 | Keyword.init("unreachable", .Keyword_unreachable), | |
| 72 | Keyword.init("usingnamespace", .Keyword_usingnamespace), | |
| 73 | Keyword.init("var", .Keyword_var), | |
| 74 | Keyword.init("volatile", .Keyword_volatile), | |
| 75 | Keyword.init("while", .Keyword_while), | |
| 66 | 76 | }; |
| 67 | 77 | |
| 68 | 78 | // TODO perfect hash at comptime |
| 69 | fn getKeyword(bytes: []const u8) ?Id { | |
| 79 | pub fn getKeyword(bytes: []const u8) ?Id { | |
| 80 | var hash = std.hash_map.hashString(bytes); | |
| 70 | 81 | for (keywords) |kw| { |
| 71 | if (mem.eql(u8, kw.bytes, bytes)) { | |
| 82 | if (kw.hash == hash and mem.eql(u8, kw.bytes, bytes)) { | |
| 72 | 83 | return kw.id; |
| 73 | 84 | } |
| 74 | 85 | } |
src-self-hosted/clang.zig+15-1| ... | ... | @@ -713,6 +713,10 @@ pub const ZigClangRecordDecl_field_iterator = extern struct { |
| 713 | 713 | opaque: *c_void, |
| 714 | 714 | }; |
| 715 | 715 | |
| 716 | pub const ZigClangEnumDecl_enumerator_iterator = extern struct { | |
| 717 | opaque: *c_void, | |
| 718 | }; | |
| 719 | ||
| 716 | 720 | pub extern fn ZigClangSourceManager_getSpellingLoc(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation; |
| 717 | 721 | pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*:0]const u8; |
| 718 | 722 | pub extern fn ZigClangSourceManager_getSpellingLineNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint; |
| ... | ... | @@ -723,7 +727,7 @@ pub extern fn ZigClangASTUnit_getASTContext(self: ?*struct_ZigClangASTUnit) ?*st |
| 723 | 727 | pub extern fn ZigClangASTUnit_getSourceManager(self: *struct_ZigClangASTUnit) *struct_ZigClangSourceManager; |
| 724 | 728 | pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?extern fn (?*c_void, *const struct_ZigClangDecl) bool) bool; |
| 725 | 729 | pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) *const struct_ZigClangRecordDecl; |
| 726 | pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) ?*const struct_ZigClangEnumDecl; | |
| 730 | pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl; | |
| 727 | 731 | pub extern fn ZigClangRecordDecl_getCanonicalDecl(record_decl: ?*const struct_ZigClangRecordDecl) ?*const struct_ZigClangTagDecl; |
| 728 | 732 | pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl; |
| 729 | 733 | pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl; |
| ... | ... | @@ -742,6 +746,11 @@ pub extern fn ZigClangRecordDecl_field_iterator_next(ZigClangRecordDecl_field_it |
| 742 | 746 | pub extern fn ZigClangRecordDecl_field_iterator_deref(ZigClangRecordDecl_field_iterator) *const struct_ZigClangFieldDecl; |
| 743 | 747 | pub extern fn ZigClangRecordDecl_field_iterator_neq(ZigClangRecordDecl_field_iterator, ZigClangRecordDecl_field_iterator) bool; |
| 744 | 748 | pub extern fn ZigClangEnumDecl_getIntegerType(self: ?*const struct_ZigClangEnumDecl) struct_ZigClangQualType; |
| 749 | pub extern fn ZigClangEnumDecl_enumerator_begin(*const ZigClangEnumDecl) ZigClangEnumDecl_enumerator_iterator; | |
| 750 | pub extern fn ZigClangEnumDecl_enumerator_end(*const ZigClangEnumDecl) ZigClangEnumDecl_enumerator_iterator; | |
| 751 | pub extern fn ZigClangEnumDecl_enumerator_iterator_next(ZigClangEnumDecl_enumerator_iterator) ZigClangEnumDecl_enumerator_iterator; | |
| 752 | pub extern fn ZigClangEnumDecl_enumerator_iterator_deref(ZigClangEnumDecl_enumerator_iterator) *const ZigClangEnumConstantDecl; | |
| 753 | pub extern fn ZigClangEnumDecl_enumerator_iterator_neq(ZigClangEnumDecl_enumerator_iterator, ZigClangEnumDecl_enumerator_iterator) bool; | |
| 745 | 754 | pub extern fn ZigClangDecl_getName_bytes_begin(decl: ?*const struct_ZigClangDecl) [*c]const u8; |
| 746 | 755 | pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: struct_ZigClangSourceLocation) bool; |
| 747 | 756 | pub extern fn ZigClangTypedefType_getDecl(self: ?*const struct_ZigClangTypedefType) *const struct_ZigClangTypedefNameDecl; |
| ... | ... | @@ -992,6 +1001,8 @@ pub extern fn ZigClangBinaryOperator_getLHS(*const ZigClangBinaryOperator) *cons |
| 992 | 1001 | pub extern fn ZigClangBinaryOperator_getRHS(*const ZigClangBinaryOperator) *const ZigClangExpr; |
| 993 | 1002 | pub extern fn ZigClangBinaryOperator_getType(*const ZigClangBinaryOperator) ZigClangQualType; |
| 994 | 1003 | |
| 1004 | pub extern fn ZigClangDecayedType_getDecayedType(*const ZigClangDecayedType) ZigClangQualType; | |
| 1005 | ||
| 995 | 1006 | pub extern fn ZigClangStringLiteral_getKind(*const ZigClangStringLiteral) ZigClangStringLiteral_StringKind; |
| 996 | 1007 | pub extern fn ZigClangStringLiteral_getString_bytes_begin_size(*const ZigClangStringLiteral, *usize) [*c]const u8; |
| 997 | 1008 | |
| ... | ... | @@ -1000,3 +1011,6 @@ pub extern fn ZigClangParenExpr_getSubExpr(*const ZigClangParenExpr) *const ZigC |
| 1000 | 1011 | pub extern fn ZigClangFieldDecl_isBitField(*const struct_ZigClangFieldDecl) bool; |
| 1001 | 1012 | pub extern fn ZigClangFieldDecl_getType(*const struct_ZigClangFieldDecl) struct_ZigClangQualType; |
| 1002 | 1013 | pub extern fn ZigClangFieldDecl_getLocation(*const struct_ZigClangFieldDecl) struct_ZigClangSourceLocation; |
| 1014 | ||
| 1015 | pub extern fn ZigClangEnumConstantDecl_getInitExpr(*const ZigClangEnumConstantDecl) ?*const ZigClangExpr; | |
| 1016 | pub extern fn ZigClangEnumConstantDecl_getInitVal(*const ZigClangEnumConstantDecl) *const ZigClangAPSInt; |
src-self-hosted/translate_c.zig+287-77| ... | ... | @@ -15,7 +15,7 @@ pub const Error = error{OutOfMemory}; |
| 15 | 15 | const TypeError = Error || error{UnsupportedType}; |
| 16 | 16 | const TransError = TypeError || error{UnsupportedTranslation}; |
| 17 | 17 | |
| 18 | const DeclTable = std.HashMap(usize, void, addrHash, addrEql); | |
| 18 | const DeclTable = std.HashMap(usize, []const u8, addrHash, addrEql); | |
| 19 | 19 | |
| 20 | 20 | fn addrHash(x: usize) u32 { |
| 21 | 21 | switch (@typeInfo(usize).Int.bits) { |
| ... | ... | @@ -109,6 +109,12 @@ const Context = struct { |
| 109 | 109 | global_scope: *Scope.Root, |
| 110 | 110 | ptr_params: std.BufSet, |
| 111 | 111 | clang_context: *ZigClangASTContext, |
| 112 | mangle_count: u64 = 0, | |
| 113 | ||
| 114 | fn getMangle(c: *Context) u64 { | |
| 115 | c.mangle_count += 1; | |
| 116 | return c.mangle_count; | |
| 117 | } | |
| 112 | 118 | |
| 113 | 119 | fn a(c: *Context) *std.mem.Allocator { |
| 114 | 120 | return &c.tree.arena_allocator.allocator; |
| ... | ... | @@ -239,7 +245,7 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 239 | 245 | return resolveTypeDef(c, @ptrCast(*const ZigClangTypedefNameDecl, decl)); |
| 240 | 246 | }, |
| 241 | 247 | .Enum => { |
| 242 | try emitWarning(c, ZigClangDecl_getLocation(decl), "TODO implement translate-c for enums", .{}); | |
| 248 | _ = try transEnumDecl(c, @ptrCast(*const ZigClangEnumDecl, decl)); | |
| 243 | 249 | }, |
| 244 | 250 | .Record => { |
| 245 | 251 | return resolveRecordDecl(c, @ptrCast(*const ZigClangRecordDecl, decl)); |
| ... | ... | @@ -255,9 +261,10 @@ fn declVisitor(c: *Context, decl: *const ZigClangDecl) Error!void { |
| 255 | 261 | } |
| 256 | 262 | |
| 257 | 263 | fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 258 | if (try c.decl_table.put(@ptrToInt(fn_decl), {})) |_| return; // Avoid processing this decl twice | |
| 264 | if (c.decl_table.contains(@ptrToInt(fn_decl))) return; // Avoid processing this decl twice | |
| 259 | 265 | const rp = makeRestorePoint(c); |
| 260 | 266 | const fn_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, fn_decl))); |
| 267 | _ = try c.decl_table.put(@ptrToInt(fn_decl), fn_name); | |
| 261 | 268 | const fn_decl_loc = ZigClangFunctionDecl_getLocation(fn_decl); |
| 262 | 269 | const fn_qt = ZigClangFunctionDecl_getType(fn_decl); |
| 263 | 270 | const fn_type = ZigClangQualType_getTypePtr(fn_qt); |
| ... | ... | @@ -319,7 +326,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 319 | 326 | } |
| 320 | 327 | |
| 321 | 328 | fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 322 | if (try c.decl_table.put(@ptrToInt(var_decl), {})) |_| return; // Avoid processing this decl twice | |
| 329 | if (c.decl_table.contains(@ptrToInt(var_decl))) return; // Avoid processing this decl twice | |
| 323 | 330 | const rp = makeRestorePoint(c); |
| 324 | 331 | const visib_tok = try appendToken(c, .Keyword_pub, "pub"); |
| 325 | 332 | |
| ... | ... | @@ -330,6 +337,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 330 | 337 | |
| 331 | 338 | var scope = &c.global_scope.base; |
| 332 | 339 | const var_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, var_decl))); |
| 340 | _ = try c.decl_table.put(@ptrToInt(var_decl), var_name); | |
| 333 | 341 | const var_decl_loc = ZigClangVarDecl_getLocation(var_decl); |
| 334 | 342 | |
| 335 | 343 | const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); |
| ... | ... | @@ -348,7 +356,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 348 | 356 | else |
| 349 | 357 | try appendToken(c, .Keyword_var, "var"); |
| 350 | 358 | |
| 351 | const name_tok = try appendToken(c, .Identifier, var_name); | |
| 359 | const name_tok = try appendIdentifier(c, var_name); | |
| 352 | 360 | |
| 353 | 361 | _ = try appendToken(c, .Colon, ":"); |
| 354 | 362 | const type_node = transQualType(rp, qual_type, var_decl_loc) catch |err| switch (err) { |
| ... | ... | @@ -381,7 +389,6 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 381 | 389 | |
| 382 | 390 | const node = try c.a().create(ast.Node.VarDecl); |
| 383 | 391 | node.* = ast.Node.VarDecl{ |
| 384 | .base = ast.Node{ .id = .VarDecl }, | |
| 385 | 392 | .doc_comments = null, |
| 386 | 393 | .visib_token = visib_tok, |
| 387 | 394 | .thread_local_token = thread_local_token, |
| ... | ... | @@ -401,13 +408,16 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void { |
| 401 | 408 | } |
| 402 | 409 | |
| 403 | 410 | fn resolveTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl) Error!void { |
| 404 | if (try c.decl_table.put(@ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)), {})) |_| return; // Avoid processing this decl twice | |
| 411 | if (c.decl_table.contains( | |
| 412 | @ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)), | |
| 413 | )) return; // Avoid processing this decl twice | |
| 405 | 414 | const rp = makeRestorePoint(c); |
| 406 | 415 | const visib_tok = try appendToken(c, .Keyword_pub, "pub"); |
| 407 | 416 | const const_tok = try appendToken(c, .Keyword_const, "const"); |
| 408 | 417 | |
| 409 | 418 | const typedef_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, typedef_decl))); |
| 410 | const name_tok = try appendToken(c, .Identifier, typedef_name); | |
| 419 | _ = try c.decl_table.put(@ptrToInt(ZigClangTypedefNameDecl_getCanonicalDecl(typedef_decl)), typedef_name); | |
| 420 | const name_tok = try appendIdentifier(c, typedef_name); | |
| 411 | 421 | const eq_tok = try appendToken(c, .Equal, "="); |
| 412 | 422 | |
| 413 | 423 | const child_qt = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); |
| ... | ... | @@ -421,7 +431,6 @@ fn resolveTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl) Err |
| 421 | 431 | |
| 422 | 432 | const node = try c.a().create(ast.Node.VarDecl); |
| 423 | 433 | node.* = ast.Node.VarDecl{ |
| 424 | .base = ast.Node{ .id = .VarDecl }, | |
| 425 | 434 | .doc_comments = null, |
| 426 | 435 | .visib_token = visib_tok, |
| 427 | 436 | .thread_local_token = null, |
| ... | ... | @@ -441,7 +450,7 @@ fn resolveTypeDef(c: *Context, typedef_decl: *const ZigClangTypedefNameDecl) Err |
| 441 | 450 | } |
| 442 | 451 | |
| 443 | 452 | fn resolveRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error!void { |
| 444 | if (try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), {})) |_| return; // Avoid processing this decl twice | |
| 453 | if (c.decl_table.contains(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)))) return; // Avoid processing this decl twice | |
| 445 | 454 | const rp = makeRestorePoint(c); |
| 446 | 455 | |
| 447 | 456 | const bare_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, record_decl))); |
| ... | ... | @@ -451,7 +460,7 @@ fn resolveRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error! |
| 451 | 460 | else if (ZigClangRecordDecl_isStruct(record_decl)) |
| 452 | 461 | "struct" |
| 453 | 462 | else |
| 454 | return failDecl(c, ZigClangRecordDecl_getLocation(record_decl), bare_name, "record {} is not a struct or union", .{bare_name}); | |
| 463 | return emitWarning(c, ZigClangRecordDecl_getLocation(record_decl), "record {} is not a struct or union", .{bare_name}); | |
| 455 | 464 | |
| 456 | 465 | if (ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl) or bare_name.len == 0) |
| 457 | 466 | return; |
| ... | ... | @@ -460,7 +469,8 @@ fn resolveRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error! |
| 460 | 469 | const const_tok = try appendToken(c, .Keyword_const, "const"); |
| 461 | 470 | |
| 462 | 471 | const name = try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name }); |
| 463 | const name_tok = try appendToken(c, .Identifier, name); | |
| 472 | _ = try c.decl_table.put(@ptrToInt(ZigClangRecordDecl_getCanonicalDecl(record_decl)), name); | |
| 473 | const name_tok = try appendIdentifier(c, name); | |
| 464 | 474 | |
| 465 | 475 | const eq_tok = try appendToken(c, .Equal, "="); |
| 466 | 476 | const init_node = transRecordDecl(c, record_decl) catch |err| switch (err) { |
| ... | ... | @@ -473,7 +483,6 @@ fn resolveRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error! |
| 473 | 483 | |
| 474 | 484 | const node = try c.a().create(ast.Node.VarDecl); |
| 475 | 485 | node.* = ast.Node.VarDecl{ |
| 476 | .base = ast.Node{ .id = .VarDecl }, | |
| 477 | 486 | .doc_comments = null, |
| 478 | 487 | .visib_token = visib_tok, |
| 479 | 488 | .thread_local_token = null, |
| ... | ... | @@ -497,14 +506,13 @@ fn resolveRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) Error! |
| 497 | 506 | fn createAlias(c: *Context, alias: var) !void { |
| 498 | 507 | const visib_tok = try appendToken(c, .Keyword_pub, "pub"); |
| 499 | 508 | const mut_tok = try appendToken(c, .Keyword_const, "const"); |
| 500 | const name_tok = try appendToken(c, .Identifier, alias.alias); | |
| 509 | const name_tok = try appendIdentifier(c, alias.alias); | |
| 501 | 510 | |
| 502 | 511 | const eq_tok = try appendToken(c, .Equal, "="); |
| 503 | const init_node = try appendIdentifier(c, alias.name); | |
| 512 | const init_node = try transCreateNodeIdentifier(c, alias.name); | |
| 504 | 513 | |
| 505 | 514 | const node = try c.a().create(ast.Node.VarDecl); |
| 506 | 515 | node.* = ast.Node.VarDecl{ |
| 507 | .base = ast.Node{ .id = .VarDecl }, | |
| 508 | 516 | .doc_comments = null, |
| 509 | 517 | .visib_token = visib_tok, |
| 510 | 518 | .thread_local_token = null, |
| ... | ... | @@ -787,7 +795,7 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe |
| 787 | 795 | const c_name = try c.str(ZigClangDecl_getName_bytes_begin( |
| 788 | 796 | @ptrCast(*const ZigClangDecl, var_decl), |
| 789 | 797 | )); |
| 790 | const name_token = try appendToken(c, .Identifier, c_name); | |
| 798 | const name_token = try appendIdentifier(c, c_name); | |
| 791 | 799 | |
| 792 | 800 | const var_scope = try c.a().create(Scope.Var); |
| 793 | 801 | var_scope.* = Scope.Var{ |
| ... | ... | @@ -810,7 +818,6 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe |
| 810 | 818 | |
| 811 | 819 | const node = try c.a().create(ast.Node.VarDecl); |
| 812 | 820 | node.* = ast.Node.VarDecl{ |
| 813 | .base = ast.Node{ .id = .VarDecl }, | |
| 814 | 821 | .doc_comments = null, |
| 815 | 822 | .visib_token = null, |
| 816 | 823 | .thread_local_token = thread_local_token, |
| ... | ... | @@ -856,7 +863,7 @@ fn transDeclRefExpr( |
| 856 | 863 | const c_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, value_decl))); |
| 857 | 864 | const zig_name = transLookupZigIdentifier(scope, c_name); |
| 858 | 865 | if (lrvalue == .l_value) try rp.c.ptr_params.put(zig_name); |
| 859 | const node = try appendIdentifier(rp.c, zig_name); | |
| 866 | const node = try transCreateNodeIdentifier(rp.c, zig_name); | |
| 860 | 867 | return TransResult{ |
| 861 | 868 | .node = node, |
| 862 | 869 | .node_scope = scope, |
| ... | ... | @@ -978,7 +985,6 @@ fn transStringLiteral( |
| 978 | 985 | const token = try appendToken(rp.c, .StringLiteral, buf); |
| 979 | 986 | const node = try rp.c.a().create(ast.Node.StringLiteral); |
| 980 | 987 | node.* = ast.Node.StringLiteral{ |
| 981 | .base = ast.Node{ .id = .StringLiteral }, | |
| 982 | 988 | .token = token, |
| 983 | 989 | }; |
| 984 | 990 | const res = TransResult{ |
| ... | ... | @@ -1145,7 +1151,6 @@ fn transInitListExpr( |
| 1145 | 1151 | const mul_tok = try appendToken(rp.c, .AsteriskAsterisk, "**"); |
| 1146 | 1152 | const mul_node = try rp.c.a().create(ast.Node.InfixOp); |
| 1147 | 1153 | mul_node.* = .{ |
| 1148 | .base = .{ .id = .InfixOp }, | |
| 1149 | 1154 | .op_token = mul_tok, |
| 1150 | 1155 | .lhs = &filler_init_node.base, |
| 1151 | 1156 | .op = .ArrayMult, |
| ... | ... | @@ -1164,7 +1169,6 @@ fn transInitListExpr( |
| 1164 | 1169 | |
| 1165 | 1170 | const cat_node = try rp.c.a().create(ast.Node.InfixOp); |
| 1166 | 1171 | cat_node.* = .{ |
| 1167 | .base = .{ .id = .InfixOp }, | |
| 1168 | 1172 | .op_token = cat_tok, |
| 1169 | 1173 | .lhs = &init_node.base, |
| 1170 | 1174 | .op = .ArrayCat, |
| ... | ... | @@ -1292,11 +1296,10 @@ fn maybeSuppressResult( |
| 1292 | 1296 | if (used == .used) return result; |
| 1293 | 1297 | // NOTE: This is backwards, but the semicolon must immediately follow the node. |
| 1294 | 1298 | _ = try appendToken(rp.c, .Semicolon, ";"); |
| 1295 | const lhs = try appendIdentifier(rp.c, "_"); | |
| 1299 | const lhs = try transCreateNodeIdentifier(rp.c, "_"); | |
| 1296 | 1300 | const op_token = try appendToken(rp.c, .Equal, "="); |
| 1297 | 1301 | const op_node = try rp.c.a().create(ast.Node.InfixOp); |
| 1298 | 1302 | op_node.* = ast.Node.InfixOp{ |
| 1299 | .base = ast.Node{ .id = .InfixOp }, | |
| 1300 | 1303 | .op_token = op_token, |
| 1301 | 1304 | .lhs = lhs, |
| 1302 | 1305 | .op = .Assign, |
| ... | ... | @@ -1352,7 +1355,6 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) TypeErro |
| 1352 | 1355 | |
| 1353 | 1356 | const container_node = try c.a().create(ast.Node.ContainerDecl); |
| 1354 | 1357 | container_node.* = .{ |
| 1355 | .base = ast.Node{ .id = .ContainerDecl }, | |
| 1356 | 1358 | .layout_token = extern_tok, |
| 1357 | 1359 | .kind_token = container_tok, |
| 1358 | 1360 | .init_arg_expr = .None, |
| ... | ... | @@ -1374,7 +1376,7 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) TypeErro |
| 1374 | 1376 | return node; |
| 1375 | 1377 | } |
| 1376 | 1378 | |
| 1377 | const field_name = try appendToken(c, .Identifier, try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, field_decl)))); | |
| 1379 | const field_name = try appendIdentifier(c, try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, field_decl)))); | |
| 1378 | 1380 | _ = try appendToken(c, .Colon, ":"); |
| 1379 | 1381 | const field_type = try transQualType(rp, ZigClangFieldDecl_getType(field_decl), field_loc); |
| 1380 | 1382 | |
| ... | ... | @@ -1396,6 +1398,195 @@ fn transRecordDecl(c: *Context, record_decl: *const ZigClangRecordDecl) TypeErro |
| 1396 | 1398 | return &container_node.base; |
| 1397 | 1399 | } |
| 1398 | 1400 | |
| 1401 | fn transEnumDecl(c: *Context, enum_decl: *const ZigClangEnumDecl) Error!?*ast.Node { | |
| 1402 | if (c.decl_table.get(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)))) |name| | |
| 1403 | return try transCreateNodeIdentifier(c, name.value); // Avoid processing this decl twice | |
| 1404 | const rp = makeRestorePoint(c); | |
| 1405 | const enum_loc = ZigClangEnumDecl_getLocation(enum_decl); | |
| 1406 | ||
| 1407 | const visib_tok = try appendToken(c, .Keyword_pub, "pub"); | |
| 1408 | const const_tok = try appendToken(c, .Keyword_const, "const"); | |
| 1409 | ||
| 1410 | var bare_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, enum_decl))); | |
| 1411 | var is_unnamed = false; | |
| 1412 | if (bare_name.len == 0) { | |
| 1413 | bare_name = try std.fmt.allocPrint(c.a(), "unnamed_{}", .{c.getMangle()}); | |
| 1414 | is_unnamed = true; | |
| 1415 | } | |
| 1416 | ||
| 1417 | const name = try std.fmt.allocPrint(c.a(), "enum_{}", .{bare_name}); | |
| 1418 | _ = try c.decl_table.put(@ptrToInt(ZigClangEnumDecl_getCanonicalDecl(enum_decl)), name); | |
| 1419 | const name_tok = try appendIdentifier(c, name); | |
| 1420 | const eq_tok = try appendToken(c, .Equal, "="); | |
| 1421 | ||
| 1422 | const init_node = if (ZigClangEnumDecl_getDefinition(enum_decl)) |enum_def| blk: { | |
| 1423 | var pure_enum = true; | |
| 1424 | var it = ZigClangEnumDecl_enumerator_begin(enum_def); | |
| 1425 | var end_it = ZigClangEnumDecl_enumerator_end(enum_def); | |
| 1426 | while (ZigClangEnumDecl_enumerator_iterator_neq(it, end_it)) : (it = ZigClangEnumDecl_enumerator_iterator_next(it)) { | |
| 1427 | const enum_const = ZigClangEnumDecl_enumerator_iterator_deref(it); | |
| 1428 | if (ZigClangEnumConstantDecl_getInitExpr(enum_const)) |_| { | |
| 1429 | pure_enum = false; | |
| 1430 | break; | |
| 1431 | } | |
| 1432 | } | |
| 1433 | ||
| 1434 | const extern_tok = try appendToken(c, .Keyword_extern, "extern"); | |
| 1435 | const container_tok = try appendToken(c, .Keyword_enum, "enum"); | |
| 1436 | ||
| 1437 | const container_node = try c.a().create(ast.Node.ContainerDecl); | |
| 1438 | container_node.* = .{ | |
| 1439 | .layout_token = extern_tok, | |
| 1440 | .kind_token = container_tok, | |
| 1441 | .init_arg_expr = .None, | |
| 1442 | .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(c.a()), | |
| 1443 | .lbrace_token = undefined, | |
| 1444 | .rbrace_token = undefined, | |
| 1445 | }; | |
| 1446 | ||
| 1447 | const int_type = ZigClangEnumDecl_getIntegerType(enum_decl); | |
| 1448 | ||
| 1449 | // TODO only emit this tag type if the enum tag type is not the default. | |
| 1450 | // I don't know what the default is, need to figure out how clang is deciding. | |
| 1451 | // it appears to at least be different across gcc/msvc | |
| 1452 | if (!isCBuiltinType(int_type, .UInt) and | |
| 1453 | !isCBuiltinType(int_type, .Int)) | |
| 1454 | { | |
| 1455 | _ = try appendToken(c, .LParen, "("); | |
| 1456 | container_node.init_arg_expr = .{ | |
| 1457 | .Type = transQualType(rp, int_type, enum_loc) catch |err| switch (err) { | |
| 1458 | error.UnsupportedType => { | |
| 1459 | if (is_unnamed) { | |
| 1460 | try emitWarning(c, enum_loc, "unable to translate enum tag type", .{}); | |
| 1461 | } else { | |
| 1462 | try failDecl(c, enum_loc, name, "unable to translate enum tag type", .{}); | |
| 1463 | } | |
| 1464 | return null; | |
| 1465 | }, | |
| 1466 | else => |e| return e, | |
| 1467 | }, | |
| 1468 | }; | |
| 1469 | _ = try appendToken(c, .RParen, ")"); | |
| 1470 | } | |
| 1471 | ||
| 1472 | container_node.lbrace_token = try appendToken(c, .LBrace, "{"); | |
| 1473 | ||
| 1474 | it = ZigClangEnumDecl_enumerator_begin(enum_def); | |
| 1475 | end_it = ZigClangEnumDecl_enumerator_end(enum_def); | |
| 1476 | while (ZigClangEnumDecl_enumerator_iterator_neq(it, end_it)) : (it = ZigClangEnumDecl_enumerator_iterator_next(it)) { | |
| 1477 | const enum_const = ZigClangEnumDecl_enumerator_iterator_deref(it); | |
| 1478 | ||
| 1479 | const enum_val_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, enum_const))); | |
| 1480 | ||
| 1481 | const field_name = if (!is_unnamed and std.mem.startsWith(u8, enum_val_name, bare_name)) | |
| 1482 | enum_val_name[bare_name.len..] | |
| 1483 | else | |
| 1484 | enum_val_name; | |
| 1485 | ||
| 1486 | const field_name_tok = try appendIdentifier(c, field_name); | |
| 1487 | ||
| 1488 | const int_node = if (!pure_enum) blk: { | |
| 1489 | _ = try appendToken(c, .Colon, "="); | |
| 1490 | break :blk try transCreateNodeAPInt(c, ZigClangEnumConstantDecl_getInitVal(enum_const)); | |
| 1491 | } else | |
| 1492 | null; | |
| 1493 | ||
| 1494 | const field_node = try c.a().create(ast.Node.ContainerField); | |
| 1495 | field_node.* = .{ | |
| 1496 | .doc_comments = null, | |
| 1497 | .comptime_token = null, | |
| 1498 | .name_token = field_name_tok, | |
| 1499 | .type_expr = null, | |
| 1500 | .value_expr = int_node, | |
| 1501 | .align_expr = null, | |
| 1502 | }; | |
| 1503 | ||
| 1504 | try container_node.fields_and_decls.push(&field_node.base); | |
| 1505 | _ = try appendToken(c, .Comma, ","); | |
| 1506 | // In C each enum value is in the global namespace. So we put them there too. | |
| 1507 | // At this point we can rely on the enum emitting successfully. | |
| 1508 | try addEnumTopLevel(c, name, field_name, enum_val_name); | |
| 1509 | } | |
| 1510 | container_node.rbrace_token = try appendToken(c, .RBrace, "}"); | |
| 1511 | ||
| 1512 | break :blk &container_node.base; | |
| 1513 | } else | |
| 1514 | try transCreateNodeOpaqueType(c); | |
| 1515 | ||
| 1516 | const semicolon_token = try appendToken(c, .Semicolon, ";"); | |
| 1517 | ||
| 1518 | const node = try c.a().create(ast.Node.VarDecl); | |
| 1519 | node.* = ast.Node.VarDecl{ | |
| 1520 | .visib_token = visib_tok, | |
| 1521 | .mut_token = const_tok, | |
| 1522 | .name_token = name_tok, | |
| 1523 | .eq_token = eq_tok, | |
| 1524 | .init_node = init_node, | |
| 1525 | .semicolon_token = semicolon_token, | |
| 1526 | .doc_comments = null, | |
| 1527 | .comptime_token = null, | |
| 1528 | .extern_export_token = null, | |
| 1529 | .thread_local_token = null, | |
| 1530 | .lib_name = null, | |
| 1531 | .type_node = null, | |
| 1532 | .align_node = null, | |
| 1533 | .section_node = null, | |
| 1534 | }; | |
| 1535 | ||
| 1536 | try addTopLevelDecl(c, name, &node.base); | |
| 1537 | if (!is_unnamed) | |
| 1538 | try c.alias_list.push(.{ .alias = bare_name, .name = name }); | |
| 1539 | return transCreateNodeIdentifier(c, name); | |
| 1540 | } | |
| 1541 | ||
| 1542 | fn addEnumTopLevel(c: *Context, enum_name: []const u8, field_name: []const u8, enum_val_name: []const u8) !void { | |
| 1543 | const visib_tok = try appendToken(c, .Keyword_pub, "pub"); | |
| 1544 | const const_tok = try appendToken(c, .Keyword_const, "const"); | |
| 1545 | const name_tok = try appendIdentifier(c, enum_val_name); | |
| 1546 | const eq_tok = try appendToken(c, .Equal, "="); | |
| 1547 | ||
| 1548 | const enum_ident = try transCreateNodeIdentifier(c, enum_name); | |
| 1549 | const period_tok = try appendToken(c, .Period, "."); | |
| 1550 | const field_ident = try transCreateNodeIdentifier(c, field_name); | |
| 1551 | ||
| 1552 | const field_access_node = try c.a().create(ast.Node.InfixOp); | |
| 1553 | field_access_node.* = .{ | |
| 1554 | .op_token = period_tok, | |
| 1555 | .lhs = enum_ident, | |
| 1556 | .op = .Period, | |
| 1557 | .rhs = field_ident, | |
| 1558 | }; | |
| 1559 | const semicolon_token = try appendToken(c, .Semicolon, ";"); | |
| 1560 | ||
| 1561 | const node = try c.a().create(ast.Node.VarDecl); | |
| 1562 | node.* = ast.Node.VarDecl{ | |
| 1563 | .visib_token = visib_tok, | |
| 1564 | .mut_token = const_tok, | |
| 1565 | .name_token = name_tok, | |
| 1566 | .eq_token = eq_tok, | |
| 1567 | .init_node = &field_access_node.base, | |
| 1568 | .semicolon_token = semicolon_token, | |
| 1569 | .thread_local_token = null, | |
| 1570 | .doc_comments = null, | |
| 1571 | .comptime_token = null, | |
| 1572 | .extern_export_token = null, | |
| 1573 | .lib_name = null, | |
| 1574 | .type_node = null, | |
| 1575 | .align_node = null, | |
| 1576 | .section_node = null, | |
| 1577 | }; | |
| 1578 | ||
| 1579 | try addTopLevelDecl(c, field_name, &node.base); | |
| 1580 | } | |
| 1581 | ||
| 1582 | fn isCBuiltinType(qt: ZigClangQualType, kind: ZigClangBuiltinTypeKind) bool { | |
| 1583 | const c_type = qualTypeCanon(qt); | |
| 1584 | if (ZigClangType_getTypeClass(c_type) != .Builtin) | |
| 1585 | return false; | |
| 1586 | const builtin_ty = @ptrCast(*const ZigClangBuiltinType, c_type); | |
| 1587 | return ZigClangBuiltinType_getKind(builtin_ty) == kind; | |
| 1588 | } | |
| 1589 | ||
| 1399 | 1590 | fn qualTypeIsPtr(qt: ZigClangQualType) bool { |
| 1400 | 1591 | return ZigClangType_getTypeClass(qualTypeCanon(qt)) == .Pointer; |
| 1401 | 1592 | } |
| ... | ... | @@ -1523,7 +1714,6 @@ fn transCreateNodeAssign( |
| 1523 | 1714 | |
| 1524 | 1715 | const node = try rp.c.a().create(ast.Node.InfixOp); |
| 1525 | 1716 | node.* = ast.Node.InfixOp{ |
| 1526 | .base = ast.Node{ .id = .InfixOp }, | |
| 1527 | 1717 | .op_token = eq_token, |
| 1528 | 1718 | .lhs = lhs_node.node, |
| 1529 | 1719 | .op = .Assign, |
| ... | ... | @@ -1553,7 +1743,6 @@ fn transCreateNodeBuiltinFnCall(c: *Context, name: []const u8) !*ast.Node.Builti |
| 1553 | 1743 | _ = try appendToken(c, .LParen, "("); |
| 1554 | 1744 | const node = try c.a().create(ast.Node.BuiltinCall); |
| 1555 | 1745 | node.* = ast.Node.BuiltinCall{ |
| 1556 | .base = ast.Node{ .id = .BuiltinCall }, | |
| 1557 | 1746 | .builtin_token = builtin_token, |
| 1558 | 1747 | .params = ast.Node.BuiltinCall.ParamList.init(c.a()), |
| 1559 | 1748 | .rparen_token = undefined, // set after appending args |
| ... | ... | @@ -1565,7 +1754,6 @@ fn transCreateNodeFnCall(c: *Context, fn_expr: *ast.Node) !*ast.Node.SuffixOp { |
| 1565 | 1754 | _ = try appendToken(c, .LParen, "("); |
| 1566 | 1755 | const node = try c.a().create(ast.Node.SuffixOp); |
| 1567 | 1756 | node.* = ast.Node.SuffixOp{ |
| 1568 | .base = ast.Node{ .id = .SuffixOp }, | |
| 1569 | 1757 | .lhs = fn_expr, |
| 1570 | 1758 | .op = ast.Node.SuffixOp.Op{ |
| 1571 | 1759 | .Call = ast.Node.SuffixOp.Op.Call{ |
| ... | ... | @@ -1586,7 +1774,6 @@ fn transCreateNodePrefixOp( |
| 1586 | 1774 | ) !*ast.Node.PrefixOp { |
| 1587 | 1775 | const node = try c.a().create(ast.Node.PrefixOp); |
| 1588 | 1776 | node.* = ast.Node.PrefixOp{ |
| 1589 | .base = ast.Node{ .id = .PrefixOp }, | |
| 1590 | 1777 | .op_token = try appendToken(c, op_tok_id, bytes), |
| 1591 | 1778 | .op = op, |
| 1592 | 1779 | .rhs = undefined, // translate and set afterward |
| ... | ... | @@ -1609,7 +1796,6 @@ fn transCreateNodeInfixOp( |
| 1609 | 1796 | const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value); |
| 1610 | 1797 | const node = try rp.c.a().create(ast.Node.InfixOp); |
| 1611 | 1798 | node.* = ast.Node.InfixOp{ |
| 1612 | .base = ast.Node{ .id = .InfixOp }, | |
| 1613 | 1799 | .op_token = op_token, |
| 1614 | 1800 | .lhs = lhs.node, |
| 1615 | 1801 | .op = op, |
| ... | ... | @@ -1619,7 +1805,6 @@ fn transCreateNodeInfixOp( |
| 1619 | 1805 | const rparen = try appendToken(rp.c, .RParen, ")"); |
| 1620 | 1806 | const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression); |
| 1621 | 1807 | grouped_expr.* = ast.Node.GroupedExpression{ |
| 1622 | .base = ast.Node{ .id = .GroupedExpression }, | |
| 1623 | 1808 | .lparen = lparen, |
| 1624 | 1809 | .expr = &node.base, |
| 1625 | 1810 | .rparen = rparen, |
| ... | ... | @@ -1644,7 +1829,7 @@ fn transCreateNodePtrType( |
| 1644 | 1829 | .Identifier => blk: { |
| 1645 | 1830 | const lbracket = try appendToken(c, .LBracket, "["); // Rendering checks if this token + 2 == .Identifier, so needs to return this token |
| 1646 | 1831 | _ = try appendToken(c, .Asterisk, "*"); |
| 1647 | _ = try appendToken(c, .Identifier, "c"); | |
| 1832 | _ = try appendIdentifier(c, "c"); | |
| 1648 | 1833 | _ = try appendToken(c, .RBracket, "]"); |
| 1649 | 1834 | break :blk lbracket; |
| 1650 | 1835 | }, |
| ... | ... | @@ -1652,7 +1837,6 @@ fn transCreateNodePtrType( |
| 1652 | 1837 | else => unreachable, |
| 1653 | 1838 | }; |
| 1654 | 1839 | node.* = ast.Node.PrefixOp{ |
| 1655 | .base = ast.Node{ .id = .PrefixOp }, | |
| 1656 | 1840 | .op_token = op_token, |
| 1657 | 1841 | .op = ast.Node.PrefixOp.Op{ |
| 1658 | 1842 | .PtrType = .{ |
| ... | ... | @@ -1679,7 +1863,6 @@ fn transCreateNodeAPInt(c: *Context, int: ?*const ZigClangAPSInt) !*ast.Node { |
| 1679 | 1863 | const token = try appendToken(c, .IntegerLiteral, str); |
| 1680 | 1864 | const node = try c.a().create(ast.Node.IntegerLiteral); |
| 1681 | 1865 | node.* = ast.Node.IntegerLiteral{ |
| 1682 | .base = ast.Node{ .id = .IntegerLiteral }, | |
| 1683 | 1866 | .token = token, |
| 1684 | 1867 | }; |
| 1685 | 1868 | return &node.base; |
| ... | ... | @@ -1689,7 +1872,6 @@ fn transCreateNodeReturnExpr(c: *Context) !*ast.Node { |
| 1689 | 1872 | const ltoken = try appendToken(c, .Keyword_return, "return"); |
| 1690 | 1873 | const node = try c.a().create(ast.Node.ControlFlowExpression); |
| 1691 | 1874 | node.* = ast.Node.ControlFlowExpression{ |
| 1692 | .base = ast.Node{ .id = .ControlFlowExpression }, | |
| 1693 | 1875 | .ltoken = ltoken, |
| 1694 | 1876 | .kind = .Return, |
| 1695 | 1877 | .rhs = null, |
| ... | ... | @@ -1701,7 +1883,6 @@ fn transCreateNodeUndefinedLiteral(c: *Context) !*ast.Node { |
| 1701 | 1883 | const token = try appendToken(c, .Keyword_undefined, "undefined"); |
| 1702 | 1884 | const node = try c.a().create(ast.Node.UndefinedLiteral); |
| 1703 | 1885 | node.* = ast.Node.UndefinedLiteral{ |
| 1704 | .base = ast.Node{ .id = .UndefinedLiteral }, | |
| 1705 | 1886 | .token = token, |
| 1706 | 1887 | }; |
| 1707 | 1888 | return &node.base; |
| ... | ... | @@ -1711,7 +1892,6 @@ fn transCreateNodeNullLiteral(c: *Context) !*ast.Node { |
| 1711 | 1892 | const token = try appendToken(c, .Keyword_null, "null"); |
| 1712 | 1893 | const node = try c.a().create(ast.Node.NullLiteral); |
| 1713 | 1894 | node.* = ast.Node.NullLiteral{ |
| 1714 | .base = ast.Node{ .id = .NullLiteral }, | |
| 1715 | 1895 | .token = token, |
| 1716 | 1896 | }; |
| 1717 | 1897 | return &node.base; |
| ... | ... | @@ -1724,7 +1904,6 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node { |
| 1724 | 1904 | try appendToken(c, .Keyword_false, "false"); |
| 1725 | 1905 | const node = try c.a().create(ast.Node.BoolLiteral); |
| 1726 | 1906 | node.* = ast.Node.BoolLiteral{ |
| 1727 | .base = ast.Node{ .id = .BoolLiteral }, | |
| 1728 | 1907 | .token = token, |
| 1729 | 1908 | }; |
| 1730 | 1909 | return &node.base; |
| ... | ... | @@ -1734,7 +1913,6 @@ fn transCreateNodeArrayInitializer(c: *Context, dot_tok: ast.TokenIndex) !*ast.N |
| 1734 | 1913 | _ = try appendToken(c, .LBrace, "{"); |
| 1735 | 1914 | const node = try c.a().create(ast.Node.SuffixOp); |
| 1736 | 1915 | node.* = ast.Node.SuffixOp{ |
| 1737 | .base = ast.Node{ .id = .SuffixOp }, | |
| 1738 | 1916 | .lhs = .{ .dot = dot_tok }, |
| 1739 | 1917 | .op = .{ |
| 1740 | 1918 | .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()), |
| ... | ... | @@ -1748,7 +1926,6 @@ fn transCreateNodeInt(c: *Context, int: var) !*ast.Node { |
| 1748 | 1926 | const token = try appendTokenFmt(c, .IntegerLiteral, "{}", .{int}); |
| 1749 | 1927 | const node = try c.a().create(ast.Node.IntegerLiteral); |
| 1750 | 1928 | node.* = ast.Node.IntegerLiteral{ |
| 1751 | .base = ast.Node{ .id = .IntegerLiteral }, | |
| 1752 | 1929 | .token = token, |
| 1753 | 1930 | }; |
| 1754 | 1931 | return &node.base; |
| ... | ... | @@ -1793,25 +1970,25 @@ fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSour |
| 1793 | 1970 | .Builtin => { |
| 1794 | 1971 | const builtin_ty = @ptrCast(*const ZigClangBuiltinType, ty); |
| 1795 | 1972 | switch (ZigClangBuiltinType_getKind(builtin_ty)) { |
| 1796 | .Void => return appendIdentifier(rp.c, "c_void"), | |
| 1797 | .Bool => return appendIdentifier(rp.c, "bool"), | |
| 1798 | .Char_U, .UChar, .Char_S, .Char8 => return appendIdentifier(rp.c, "u8"), | |
| 1799 | .SChar => return appendIdentifier(rp.c, "i8"), | |
| 1800 | .UShort => return appendIdentifier(rp.c, "c_ushort"), | |
| 1801 | .UInt => return appendIdentifier(rp.c, "c_uint"), | |
| 1802 | .ULong => return appendIdentifier(rp.c, "c_ulong"), | |
| 1803 | .ULongLong => return appendIdentifier(rp.c, "c_ulonglong"), | |
| 1804 | .Short => return appendIdentifier(rp.c, "c_short"), | |
| 1805 | .Int => return appendIdentifier(rp.c, "c_int"), | |
| 1806 | .Long => return appendIdentifier(rp.c, "c_long"), | |
| 1807 | .LongLong => return appendIdentifier(rp.c, "c_longlong"), | |
| 1808 | .UInt128 => return appendIdentifier(rp.c, "u128"), | |
| 1809 | .Int128 => return appendIdentifier(rp.c, "i128"), | |
| 1810 | .Float => return appendIdentifier(rp.c, "f32"), | |
| 1811 | .Double => return appendIdentifier(rp.c, "f64"), | |
| 1812 | .Float128 => return appendIdentifier(rp.c, "f128"), | |
| 1813 | .Float16 => return appendIdentifier(rp.c, "f16"), | |
| 1814 | .LongDouble => return appendIdentifier(rp.c, "c_longdouble"), | |
| 1973 | .Void => return transCreateNodeIdentifier(rp.c, "c_void"), | |
| 1974 | .Bool => return transCreateNodeIdentifier(rp.c, "bool"), | |
| 1975 | .Char_U, .UChar, .Char_S, .Char8 => return transCreateNodeIdentifier(rp.c, "u8"), | |
| 1976 | .SChar => return transCreateNodeIdentifier(rp.c, "i8"), | |
| 1977 | .UShort => return transCreateNodeIdentifier(rp.c, "c_ushort"), | |
| 1978 | .UInt => return transCreateNodeIdentifier(rp.c, "c_uint"), | |
| 1979 | .ULong => return transCreateNodeIdentifier(rp.c, "c_ulong"), | |
| 1980 | .ULongLong => return transCreateNodeIdentifier(rp.c, "c_ulonglong"), | |
| 1981 | .Short => return transCreateNodeIdentifier(rp.c, "c_short"), | |
| 1982 | .Int => return transCreateNodeIdentifier(rp.c, "c_int"), | |
| 1983 | .Long => return transCreateNodeIdentifier(rp.c, "c_long"), | |
| 1984 | .LongLong => return transCreateNodeIdentifier(rp.c, "c_longlong"), | |
| 1985 | .UInt128 => return transCreateNodeIdentifier(rp.c, "u128"), | |
| 1986 | .Int128 => return transCreateNodeIdentifier(rp.c, "i128"), | |
| 1987 | .Float => return transCreateNodeIdentifier(rp.c, "f32"), | |
| 1988 | .Double => return transCreateNodeIdentifier(rp.c, "f64"), | |
| 1989 | .Float128 => return transCreateNodeIdentifier(rp.c, "f128"), | |
| 1990 | .Float16 => return transCreateNodeIdentifier(rp.c, "f16"), | |
| 1991 | .LongDouble => return transCreateNodeIdentifier(rp.c, "c_longdouble"), | |
| 1815 | 1992 | else => return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported builtin type", .{}), |
| 1816 | 1993 | } |
| 1817 | 1994 | }, |
| ... | ... | @@ -1891,21 +2068,36 @@ fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSour |
| 1891 | 2068 | |
| 1892 | 2069 | const typedef_decl = ZigClangTypedefType_getDecl(typedef_ty); |
| 1893 | 2070 | const typedef_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, typedef_decl))); |
| 1894 | return appendIdentifier(rp.c, typedef_name); | |
| 2071 | return transCreateNodeIdentifier(rp.c, typedef_name); | |
| 1895 | 2072 | }, |
| 1896 | 2073 | .Record => { |
| 1897 | 2074 | const record_ty = @ptrCast(*const ZigClangRecordType, ty); |
| 1898 | 2075 | |
| 1899 | 2076 | const record_decl = ZigClangRecordType_getDecl(record_ty); |
| 1900 | if (try getContainerName(rp.c, record_decl)) |name| | |
| 1901 | return appendIdentifier(rp.c, name) | |
| 2077 | if (try getContainerName(rp, record_decl)) |name| | |
| 2078 | return transCreateNodeIdentifier(rp.c, name) | |
| 1902 | 2079 | else |
| 1903 | 2080 | return transRecordDecl(rp.c, record_decl); |
| 1904 | 2081 | }, |
| 2082 | .Enum => { | |
| 2083 | const enum_ty = @ptrCast(*const ZigClangEnumType, ty); | |
| 2084 | ||
| 2085 | const enum_decl = ZigClangEnumType_getDecl(enum_ty); | |
| 2086 | return (try transEnumDecl(rp.c, enum_decl)) orelse | |
| 2087 | revertAndWarn(rp, error.UnsupportedType, source_loc, "unable to translate enum declaration", .{}); | |
| 2088 | }, | |
| 1905 | 2089 | .Elaborated => { |
| 1906 | 2090 | const elaborated_ty = @ptrCast(*const ZigClangElaboratedType, ty); |
| 1907 | 2091 | return transQualType(rp, ZigClangElaboratedType_getNamedType(elaborated_ty), source_loc); |
| 1908 | 2092 | }, |
| 2093 | .Decayed => { | |
| 2094 | const decayed_ty = @ptrCast(*const ZigClangDecayedType, ty); | |
| 2095 | return transQualType(rp, ZigClangDecayedType_getDecayedType(decayed_ty), source_loc); | |
| 2096 | }, | |
| 2097 | .Attributed => { | |
| 2098 | const attributed_ty = @ptrCast(*const ZigClangAttributedType, ty); | |
| 2099 | return transQualType(rp, ZigClangAttributedType_getEquivalentType(attributed_ty), source_loc); | |
| 2100 | }, | |
| 1909 | 2101 | else => { |
| 1910 | 2102 | const type_name = rp.c.str(ZigClangType_getTypeClassName(ty)); |
| 1911 | 2103 | return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported type: '{}'", .{type_name}); |
| ... | ... | @@ -1913,22 +2105,20 @@ fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSour |
| 1913 | 2105 | } |
| 1914 | 2106 | } |
| 1915 | 2107 | |
| 1916 | fn getContainerName(c: *Context, record_decl: *const ZigClangRecordDecl) !?[]const u8 { | |
| 1917 | const bare_name = try c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, record_decl))); | |
| 2108 | fn getContainerName(rp: RestorePoint, record_decl: *const ZigClangRecordDecl) !?[]const u8 { | |
| 2109 | const bare_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, record_decl))); | |
| 1918 | 2110 | |
| 1919 | 2111 | const container_kind_name = if (ZigClangRecordDecl_isUnion(record_decl)) |
| 1920 | 2112 | "union" |
| 1921 | 2113 | else if (ZigClangRecordDecl_isStruct(record_decl)) |
| 1922 | 2114 | "struct" |
| 1923 | else { | |
| 1924 | try emitWarning(c, ZigClangRecordDecl_getLocation(record_decl), "record {} is not a struct or union", .{bare_name}); | |
| 1925 | return null; | |
| 1926 | }; | |
| 2115 | else | |
| 2116 | return revertAndWarn(rp, error.UnsupportedType, ZigClangRecordDecl_getLocation(record_decl), "record {} is not a struct or union", .{bare_name}); | |
| 1927 | 2117 | |
| 1928 | 2118 | if (ZigClangRecordDecl_isAnonymousStructOrUnion(record_decl) or bare_name.len == 0) |
| 1929 | 2119 | return null; |
| 1930 | 2120 | |
| 1931 | return try std.fmt.allocPrint(c.a(), "{}_{}", .{ container_kind_name, bare_name }); | |
| 2121 | return try std.fmt.allocPrint(rp.c.a(), "{}_{}", .{ container_kind_name, bare_name }); | |
| 1932 | 2122 | } |
| 1933 | 2123 | |
| 1934 | 2124 | fn isCVoid(qt: ZigClangQualType) bool { |
| ... | ... | @@ -2020,7 +2210,7 @@ fn finishTransFnProto( |
| 2020 | 2210 | else |
| 2021 | 2211 | null; |
| 2022 | 2212 | const fn_tok = try appendToken(rp.c, .Keyword_fn, "fn"); |
| 2023 | const name_tok = if (fn_decl_context) |ctx| try appendToken(rp.c, .Identifier, ctx.fn_name) else null; | |
| 2213 | const name_tok = if (fn_decl_context) |ctx| try appendIdentifier(rp.c, ctx.fn_name) else null; | |
| 2024 | 2214 | const lparen_tok = try appendToken(rp.c, .LParen, "("); |
| 2025 | 2215 | |
| 2026 | 2216 | var fn_params = ast.Node.FnProto.ParamList.init(rp.c.a()); |
| ... | ... | @@ -2038,7 +2228,7 @@ fn finishTransFnProto( |
| 2038 | 2228 | const param_name = try rp.c.str(ZigClangDecl_getName_bytes_begin(@ptrCast(*const ZigClangDecl, param))); |
| 2039 | 2229 | if (param_name.len > 0) { |
| 2040 | 2230 | // TODO: If len == 0, auto-generate arg1, arg2, etc? Or leave the name blank? |
| 2041 | const result = try appendToken(rp.c, .Identifier, param_name); | |
| 2231 | const result = try appendIdentifier(rp.c, param_name); | |
| 2042 | 2232 | _ = try appendToken(rp.c, .Colon, ":"); |
| 2043 | 2233 | break :blk result; |
| 2044 | 2234 | } |
| ... | ... | @@ -2087,12 +2277,12 @@ fn finishTransFnProto( |
| 2087 | 2277 | |
| 2088 | 2278 | const return_type_node = blk: { |
| 2089 | 2279 | if (ZigClangFunctionType_getNoReturnAttr(fn_ty)) { |
| 2090 | break :blk try appendIdentifier(rp.c, "noreturn"); | |
| 2280 | break :blk try transCreateNodeIdentifier(rp.c, "noreturn"); | |
| 2091 | 2281 | } else { |
| 2092 | 2282 | const return_qt = ZigClangFunctionType_getReturnType(fn_ty); |
| 2093 | 2283 | if (isCVoid(return_qt)) { |
| 2094 | 2284 | // convert primitive c_void to actual void (only for return type) |
| 2095 | break :blk try appendIdentifier(rp.c, "void"); | |
| 2285 | break :blk try transCreateNodeIdentifier(rp.c, "void"); | |
| 2096 | 2286 | } else { |
| 2097 | 2287 | break :blk transQualType(rp, return_qt, source_loc) catch |err| switch (err) { |
| 2098 | 2288 | error.UnsupportedType => { |
| ... | ... | @@ -2145,7 +2335,7 @@ fn emitWarning(c: *Context, loc: ZigClangSourceLocation, comptime format: []cons |
| 2145 | 2335 | fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comptime format: []const u8, args: var) !void { |
| 2146 | 2336 | // const name = @compileError(msg); |
| 2147 | 2337 | const const_tok = try appendToken(c, .Keyword_const, "const"); |
| 2148 | const name_tok = try appendToken(c, .Identifier, name); | |
| 2338 | const name_tok = try appendIdentifier(c, name); | |
| 2149 | 2339 | const eq_tok = try appendToken(c, .Equal, "="); |
| 2150 | 2340 | const builtin_tok = try appendToken(c, .Builtin, "@compileError"); |
| 2151 | 2341 | const lparen_tok = try appendToken(c, .LParen, "("); |
| ... | ... | @@ -2190,6 +2380,7 @@ fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comptime |
| 2190 | 2380 | } |
| 2191 | 2381 | |
| 2192 | 2382 | fn appendToken(c: *Context, token_id: Token.Id, bytes: []const u8) !ast.TokenIndex { |
| 2383 | std.debug.assert(token_id != .Identifier); // use appendIdentifier | |
| 2193 | 2384 | return appendTokenFmt(c, token_id, "{}", .{bytes}); |
| 2194 | 2385 | } |
| 2195 | 2386 | |
| ... | ... | @@ -2218,8 +2409,27 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8, |
| 2218 | 2409 | return token_index; |
| 2219 | 2410 | } |
| 2220 | 2411 | |
| 2221 | fn appendIdentifier(c: *Context, name: []const u8) !*ast.Node { | |
| 2222 | const token_index = try appendToken(c, .Identifier, name); | |
| 2412 | fn isValidZigIdentifier(name: []const u8) bool { | |
| 2413 | for (name) |c, i| { | |
| 2414 | switch (c) { | |
| 2415 | '_', 'a'...'z', 'A'...'Z' => {}, | |
| 2416 | '0'...'9' => if (i == 0) return false, | |
| 2417 | else => return false, | |
| 2418 | } | |
| 2419 | } | |
| 2420 | return true; | |
| 2421 | } | |
| 2422 | ||
| 2423 | fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { | |
| 2424 | if (!isValidZigIdentifier(name) or std.zig.Token.getKeyword(name) != null) { | |
| 2425 | return appendTokenFmt(c, .Identifier, "@\"{}\"", .{name}); | |
| 2426 | } else { | |
| 2427 | return appendTokenFmt(c, .Identifier, "{}", .{name}); | |
| 2428 | } | |
| 2429 | } | |
| 2430 | ||
| 2431 | fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node { | |
| 2432 | const token_index = try appendIdentifier(c, name); | |
| 2223 | 2433 | const identifier = try c.a().create(ast.Node.Identifier); |
| 2224 | 2434 | identifier.* = ast.Node.Identifier{ |
| 2225 | 2435 | .base = ast.Node{ .id = ast.Node.Id.Identifier }, |
test/translate_c.zig+69-3| ... | ... | @@ -217,6 +217,72 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 217 | 217 | \\pub const OpenGLProcs = union_OpenGLProcs; |
| 218 | 218 | }); |
| 219 | 219 | |
| 220 | cases.add_2("enums", | |
| 221 | \\typedef enum { | |
| 222 | \\ a, | |
| 223 | \\ b, | |
| 224 | \\ c, | |
| 225 | \\} d; | |
| 226 | \\enum { | |
| 227 | \\ e, | |
| 228 | \\ f = 4, | |
| 229 | \\ g, | |
| 230 | \\} h = e; | |
| 231 | \\struct Baz { | |
| 232 | \\ enum { | |
| 233 | \\ i, | |
| 234 | \\ j, | |
| 235 | \\ k, | |
| 236 | \\ } l; | |
| 237 | \\ d m; | |
| 238 | \\}; | |
| 239 | \\enum i { | |
| 240 | \\ n, | |
| 241 | \\ o, | |
| 242 | \\ p, | |
| 243 | \\}; | |
| 244 | , &[_][]const u8{ | |
| 245 | \\pub const a = enum_unnamed_1.a; | |
| 246 | \\pub const b = enum_unnamed_1.b; | |
| 247 | \\pub const c = enum_unnamed_1.c; | |
| 248 | \\pub const enum_unnamed_1 = extern enum { | |
| 249 | \\ a, | |
| 250 | \\ b, | |
| 251 | \\ c, | |
| 252 | \\}; | |
| 253 | \\pub const d = enum_unnamed_1; | |
| 254 | \\pub const e = enum_unnamed_2.e; | |
| 255 | \\pub const f = enum_unnamed_2.f; | |
| 256 | \\pub const g = enum_unnamed_2.g; | |
| 257 | \\pub const enum_unnamed_2 = extern enum { | |
| 258 | \\ e = 0, | |
| 259 | \\ f = 4, | |
| 260 | \\ g = 5, | |
| 261 | \\}; | |
| 262 | \\pub export var h: enum_unnamed_2 = @as(enum_unnamed_2, e); | |
| 263 | \\pub const i = enum_unnamed_3.i; | |
| 264 | \\pub const j = enum_unnamed_3.j; | |
| 265 | \\pub const k = enum_unnamed_3.k; | |
| 266 | \\pub const enum_unnamed_3 = extern enum { | |
| 267 | \\ i, | |
| 268 | \\ j, | |
| 269 | \\ k, | |
| 270 | \\}; | |
| 271 | \\pub const struct_Baz = extern struct { | |
| 272 | \\ l: enum_unnamed_3, | |
| 273 | \\ m: d, | |
| 274 | \\}; | |
| 275 | \\pub const n = enum_i.n; | |
| 276 | \\pub const o = enum_i.o; | |
| 277 | \\pub const p = enum_i.p; | |
| 278 | \\pub const enum_i = extern enum { | |
| 279 | \\ n, | |
| 280 | \\ o, | |
| 281 | \\ p, | |
| 282 | \\}; | |
| 283 | \\pub const Baz = struct_Baz; | |
| 284 | }); | |
| 285 | ||
| 220 | 286 | /////////////// Cases for only stage1 which are TODO items for stage2 //////////////// |
| 221 | 287 | |
| 222 | 288 | cases.add_both("typedef of function in struct field", |
| ... | ... | @@ -440,7 +506,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 440 | 506 | \\} |
| 441 | 507 | }); |
| 442 | 508 | |
| 443 | cases.add("enums", | |
| 509 | cases.add_both("enums", | |
| 444 | 510 | \\enum Foo { |
| 445 | 511 | \\ FooA, |
| 446 | 512 | \\ FooB, |
| ... | ... | @@ -462,7 +528,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 462 | 528 | \\pub const Foo = enum_Foo; |
| 463 | 529 | }); |
| 464 | 530 | |
| 465 | cases.add("enums", | |
| 531 | cases.add_both("enums", | |
| 466 | 532 | \\enum Foo { |
| 467 | 533 | \\ FooA = 2, |
| 468 | 534 | \\ FooB = 5, |
| ... | ... | @@ -686,7 +752,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 686 | 752 | \\pub const SDL_INIT_VIDEO = @as(c_ulonglong, 32); |
| 687 | 753 | }); |
| 688 | 754 | |
| 689 | cases.add("zig keywords in C code", | |
| 755 | cases.add_both("zig keywords in C code", | |
| 690 | 756 | \\struct comptime { |
| 691 | 757 | \\ int defer; |
| 692 | 758 | \\}; |