Fixed enum access

This commit is contained in:
Alexandros Naskos 2020-06-18 15:43:03 +03:00
parent 2dc7b609d1
commit fc769e7e83
3 changed files with 33 additions and 8 deletions

View File

@ -832,7 +832,7 @@ pub fn resolveTypeOfNodeInternal(
.type = .{ .data = .{ .other = node }, .is_type_val = false }, .type = .{ .data = .{ .other = node }, .is_type_val = false },
.handle = handle, .handle = handle,
}, },
else => std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}), else => {}, //std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}),
} }
return null; return null;
} }
@ -1514,16 +1514,29 @@ pub fn iterateSymbolsContainer(
orig_handle: *DocumentStore.Handle, orig_handle: *DocumentStore.Handle,
comptime callback: var, comptime callback: var,
context: var, context: var,
include_fields: bool, instance_access: bool,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
const container = container_handle.node; const container = container_handle.node;
const handle = container_handle.handle; const handle = container_handle.handle;
const is_enum = if (container.cast(ast.Node.ContainerDecl)) |cont_decl|
handle.tree.token_ids[cont_decl.kind_token] == .Keyword_enum
else
false;
if (findContainerScope(container_handle)) |container_scope| { if (findContainerScope(container_handle)) |container_scope| {
var decl_it = container_scope.decls.iterator(); var decl_it = container_scope.decls.iterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |entry| {
if (!include_fields and entry.value == .ast_node and entry.value.ast_node.id == .ContainerField) continue; switch (entry.value) {
if (entry.value == .label_decl) continue; .ast_node => |node| {
if (node.id == .ContainerField) {
if (!instance_access and !is_enum) continue;
if (instance_access and is_enum) continue;
}
},
.label_decl => continue,
else => {},
}
const decl = DeclWithHandle{ .decl = &entry.value, .handle = handle }; const decl = DeclWithHandle{ .decl = &entry.value, .handle = handle };
if (handle != orig_handle and !decl.isPublic()) continue; if (handle != orig_handle and !decl.isPublic()) continue;
try callback(context, decl); try callback(context, decl);
@ -1694,16 +1707,26 @@ pub fn lookupSymbolContainer(
arena: *std.heap.ArenaAllocator, arena: *std.heap.ArenaAllocator,
container_handle: NodeWithHandle, container_handle: NodeWithHandle,
symbol: []const u8, symbol: []const u8,
accept_fields: bool, /// If true, we are looking up the symbol like we are accessing through a field access
/// of an instance of the type, otherwise as a field access of the type value itself.
instance_access: bool,
) error{OutOfMemory}!?DeclWithHandle { ) error{OutOfMemory}!?DeclWithHandle {
const container = container_handle.node; const container = container_handle.node;
const handle = container_handle.handle; const handle = container_handle.handle;
const is_enum = if (container.cast(ast.Node.ContainerDecl)) |cont_decl|
handle.tree.token_ids[cont_decl.kind_token] == .Keyword_enum
else
false;
if (findContainerScope(container_handle)) |container_scope| { if (findContainerScope(container_handle)) |container_scope| {
if (container_scope.decls.get(symbol)) |candidate| { if (container_scope.decls.get(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value) {
.ast_node => |node| { .ast_node => |node| {
if (node.id == .ContainerField and !accept_fields) return null; if (node.id == .ContainerField) {
if (!instance_access and !is_enum) return null;
if (instance_access and is_enum) return null;
}
}, },
.label_decl => unreachable, .label_decl => unreachable,
else => {}, else => {},
@ -1715,7 +1738,7 @@ pub fn lookupSymbolContainer(
return null; return null;
} }
std.debug.warn("Did not find container scope when looking up in container {} (name: {})\n", .{ container, getDeclName(handle.tree, container) }); // std.debug.warn("Did not find container scope when looking up in container {} (name: {})\n", .{ container, getDeclName(handle.tree, container) });
return null; return null;
} }

View File

@ -552,7 +552,7 @@ pub fn resolveImport(self: *DocumentStore, handle: *Handle, import_str: []const
import_str, import_str,
)) orelse return null; )) orelse return null;
std.debug.warn("Import final URI: {}\n", .{final_uri}); // std.debug.warn("Import final URI: {}\n", .{final_uri});
var consumed_final_uri = false; var consumed_final_uri = false;
defer if (!consumed_final_uri) allocator.free(final_uri); defer if (!consumed_final_uri) allocator.free(final_uri);

View File

@ -492,6 +492,8 @@ fn writeNodeTokens(builder: *Builder, arena: *std.heap.ArenaAllocator, store: *D
if (tok_type) |tt| try writeToken(builder, infix_op.rhs.firstToken(), tt); if (tok_type) |tt| try writeToken(builder, infix_op.rhs.firstToken(), tt);
return; return;
} else if (decl_node.id == .ErrorTag) {
try writeToken(builder, infix_op.rhs.firstToken(), .errorTag);
} }
}, },
else => {}, else => {},