| ... | @@ -7,7 +7,10 @@ file: File.Index, | ... | @@ -7,7 +7,10 @@ file: File.Index, |
| 7 | | 7 | |
| 8 | /// keep in sync with "CAT_" constants in main.js | 8 | /// keep in sync with "CAT_" constants in main.js |
| 9 | pub const Category = union(enum(u8)) { | 9 | pub const Category = union(enum(u8)) { |
| | 10 | /// A struct type used only to group declarations. |
| 10 | namespace: Ast.Node.Index, | 11 | namespace: Ast.Node.Index, |
| | 12 | /// A container type (struct, union, enum, opaque). |
| | 13 | container: Ast.Node.Index, |
| 11 | global_variable: Ast.Node.Index, | 14 | global_variable: Ast.Node.Index, |
| 12 | /// A function that has not been detected as returning a type. | 15 | /// A function that has not been detected as returning a type. |
| 13 | function: Ast.Node.Index, | 16 | function: Ast.Node.Index, |
| ... | @@ -45,13 +48,6 @@ pub const File = struct { | ... | @@ -45,13 +48,6 @@ pub const File = struct { |
| 45 | return file.node_decls.get(decl_node) orelse return .none; | 48 | return file.node_decls.get(decl_node) orelse return .none; |
| 46 | } | 49 | } |
| 47 | | 50 | |
| 48 | pub fn field_count(file: *const File, node: Ast.Node.Index) u32 { | | |
| 49 | const scope = file.scopes.get(node) orelse return 0; | | |
| 50 | if (scope.tag != .namespace) return 0; | | |
| 51 | const namespace: *Scope.Namespace = @alignCast(@fieldParentPtr("base", scope)); | | |
| 52 | return namespace.field_count; | | |
| 53 | } | | |
| 54 | | | |
| 55 | pub const Index = enum(u32) { | 51 | pub const Index = enum(u32) { |
| 56 | _, | 52 | _, |
| 57 | | 53 | |
| ... | @@ -87,7 +83,18 @@ pub const File = struct { | ... | @@ -87,7 +83,18 @@ pub const File = struct { |
| 87 | const node_tags = ast.nodes.items(.tag); | 83 | const node_tags = ast.nodes.items(.tag); |
| 88 | const token_tags = ast.tokens.items(.tag); | 84 | const token_tags = ast.tokens.items(.tag); |
| 89 | switch (node_tags[node]) { | 85 | switch (node_tags[node]) { |
| 90 | .root => return .{ .namespace = node }, | 86 | .root => { |
| | 87 | for (ast.rootDecls()) |member| { |
| | 88 | switch (node_tags[member]) { |
| | 89 | .container_field_init, |
| | 90 | .container_field_align, |
| | 91 | .container_field, |
| | 92 | => return .{ .container = node }, |
| | 93 | else => {}, |
| | 94 | } |
| | 95 | } |
| | 96 | return .{ .namespace = node }; |
| | 97 | }, |
| 91 | | 98 | |
| 92 | .global_var_decl, | 99 | .global_var_decl, |
| 93 | .local_var_decl, | 100 | .local_var_decl, |
| ... | @@ -122,7 +129,7 @@ pub const File = struct { | ... | @@ -122,7 +129,7 @@ pub const File = struct { |
| 122 | full: Ast.full.FnProto, | 129 | full: Ast.full.FnProto, |
| 123 | ) Category { | 130 | ) Category { |
| 124 | return switch (categorize_expr(file_index, full.ast.return_type)) { | 131 | return switch (categorize_expr(file_index, full.ast.return_type)) { |
| 125 | .namespace, .error_set, .type_type => .{ .type_function = node }, | 132 | .namespace, .container, .error_set, .type_type => .{ .type_function = node }, |
| 126 | else => .{ .function = node }, | 133 | else => .{ .function = node }, |
| 127 | }; | 134 | }; |
| 128 | } | 135 | } |
| ... | @@ -140,6 +147,7 @@ pub const File = struct { | ... | @@ -140,6 +147,7 @@ pub const File = struct { |
| 140 | const node_tags = ast.nodes.items(.tag); | 147 | const node_tags = ast.nodes.items(.tag); |
| 141 | const node_datas = ast.nodes.items(.data); | 148 | const node_datas = ast.nodes.items(.data); |
| 142 | const main_tokens = ast.nodes.items(.main_token); | 149 | const main_tokens = ast.nodes.items(.main_token); |
| | 150 | const token_tags = ast.tokens.items(.tag); |
| 143 | //log.debug("categorize_expr tag {s}", .{@tagName(node_tags[node])}); | 151 | //log.debug("categorize_expr tag {s}", .{@tagName(node_tags[node])}); |
| 144 | return switch (node_tags[node]) { | 152 | return switch (node_tags[node]) { |
| 145 | .container_decl, | 153 | .container_decl, |
| ... | @@ -154,7 +162,23 @@ pub const File = struct { | ... | @@ -154,7 +162,23 @@ pub const File = struct { |
| 154 | .tagged_union_enum_tag_trailing, | 162 | .tagged_union_enum_tag_trailing, |
| 155 | .tagged_union_two, | 163 | .tagged_union_two, |
| 156 | .tagged_union_two_trailing, | 164 | .tagged_union_two_trailing, |
| 157 | => .{ .namespace = node }, | 165 | => { |
| | 166 | var buf: [2]Ast.Node.Index = undefined; |
| | 167 | const container_decl = ast.fullContainerDecl(&buf, node).?; |
| | 168 | if (token_tags[container_decl.ast.main_token] != .keyword_struct) { |
| | 169 | return .{ .container = node }; |
| | 170 | } |
| | 171 | for (container_decl.ast.members) |member| { |
| | 172 | switch (node_tags[member]) { |
| | 173 | .container_field_init, |
| | 174 | .container_field_align, |
| | 175 | .container_field, |
| | 176 | => return .{ .container = node }, |
| | 177 | else => {}, |
| | 178 | } |
| | 179 | } |
| | 180 | return .{ .namespace = node }; |
| | 181 | }, |
| 158 | | 182 | |
| 159 | .error_set_decl, | 183 | .error_set_decl, |
| 160 | .merge_error_sets, | 184 | .merge_error_sets, |
| ... | @@ -240,6 +264,7 @@ pub const File = struct { | ... | @@ -240,6 +264,7 @@ pub const File = struct { |
| 240 | return .{ .error_set = node }; | 264 | return .{ .error_set = node }; |
| 241 | } else if (then_cat == .type or else_cat == .type or | 265 | } else if (then_cat == .type or else_cat == .type or |
| 242 | then_cat == .namespace or else_cat == .namespace or | 266 | then_cat == .namespace or else_cat == .namespace or |
| | 267 | then_cat == .container or else_cat == .container or |
| 243 | then_cat == .error_set or else_cat == .error_set or | 268 | then_cat == .error_set or else_cat == .error_set or |
| 244 | then_cat == .type_function or else_cat == .type_function) | 269 | then_cat == .type_function or else_cat == .type_function) |
| 245 | { | 270 | { |
| ... | @@ -346,7 +371,7 @@ pub const File = struct { | ... | @@ -346,7 +371,7 @@ pub const File = struct { |
| 346 | any_type = true; | 371 | any_type = true; |
| 347 | all_type_type = false; | 372 | all_type_type = false; |
| 348 | }, | 373 | }, |
| 349 | .type, .namespace, .type_function => { | 374 | .type, .namespace, .container, .type_function => { |
| 350 | any_type = true; | 375 | any_type = true; |
| 351 | all_error_set = false; | 376 | all_error_set = false; |
| 352 | all_type_type = false; | 377 | all_type_type = false; |
| ... | @@ -431,7 +456,6 @@ pub const Scope = struct { | ... | @@ -431,7 +456,6 @@ pub const Scope = struct { |
| 431 | names: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{}, | 456 | names: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{}, |
| 432 | doctests: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{}, | 457 | doctests: std.StringArrayHashMapUnmanaged(Ast.Node.Index) = .{}, |
| 433 | decl_index: Decl.Index, | 458 | decl_index: Decl.Index, |
| 434 | field_count: u32, | | |
| 435 | }; | 459 | }; |
| 436 | | 460 | |
| 437 | fn getNamespaceDecl(start_scope: *Scope) Decl.Index { | 461 | fn getNamespaceDecl(start_scope: *Scope) Decl.Index { |
| ... | @@ -500,7 +524,6 @@ fn struct_decl( | ... | @@ -500,7 +524,6 @@ fn struct_decl( |
| 500 | namespace.* = .{ | 524 | namespace.* = .{ |
| 501 | .parent = scope, | 525 | .parent = scope, |
| 502 | .decl_index = parent_decl, | 526 | .decl_index = parent_decl, |
| 503 | .field_count = 0, | | |
| 504 | }; | 527 | }; |
| 505 | try w.file.get().scopes.putNoClobber(gpa, node, &namespace.base); | 528 | try w.file.get().scopes.putNoClobber(gpa, node, &namespace.base); |
| 506 | try w.scanDecls(namespace, container_decl.ast.members); | 529 | try w.scanDecls(namespace, container_decl.ast.members); |
| ... | @@ -1061,14 +1084,6 @@ fn scanDecls(w: *Walk, namespace: *Scope.Namespace, members: []const Ast.Node.In | ... | @@ -1061,14 +1084,6 @@ fn scanDecls(w: *Walk, namespace: *Scope.Namespace, members: []const Ast.Node.In |
| 1061 | continue; | 1084 | continue; |
| 1062 | }, | 1085 | }, |
| 1063 | | 1086 | |
| 1064 | .container_field_init, | | |
| 1065 | .container_field_align, | | |
| 1066 | .container_field, | | |
| 1067 | => { | | |
| 1068 | namespace.field_count += 1; | | |
| 1069 | continue; | | |
| 1070 | }, | | |
| 1071 | | | |
| 1072 | else => continue, | 1087 | else => continue, |
| 1073 | }; | 1088 | }; |
| 1074 | | 1089 | |