Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,11 @@ const FindBreaks = struct {
}
};

pub fn resolveInstanceOfNode(analyser: *Analyser, options: ResolveOptions) error{OutOfMemory}!?Type {
const ty = try analyser.resolveTypeOfNode(options) orelse return null;
return ty.instanceTypeVal(analyser);
}

/// Resolves the type of an Ast Node.
/// Returns `null` if the type could not be resolved.
pub fn resolveTypeOfNode(analyser: *Analyser, options: ResolveOptions) error{OutOfMemory}!?Type {
Expand Down Expand Up @@ -5995,6 +6000,22 @@ pub fn resolveExpressionTypeFromAncestors(
if (node.toOptional() == var_decl.ast.init_node) {
return try analyser.resolveTypeOfNode(.of(ancestors[0], handle));
}
if (node.toOptional() == var_decl.ast.addrspace_node) {
return analyser.instanceStdBuiltinType("AddressSpace");
}
if (node.toOptional() == var_decl.ast.section_node) {
return .{
.data = .{
.pointer = .{
.size = .slice,
.sentinel = .none,
.is_const = true,
.elem_ty = try analyser.allocType(.fromIP(analyser, .type_type, .u8_type)),
},
},
.is_type_val = false,
};
}
},
.if_simple,
.@"if",
Expand Down Expand Up @@ -6099,6 +6120,25 @@ pub fn resolveExpressionTypeFromAncestors(
return try analyser.resolveTypeOfNode(.of(lhs, handle));
}
},
.ptr_type_aligned,
.ptr_type_sentinel,
.ptr_type,
.ptr_type_bit_range,
=> {
const ptr = tree.fullPtrType(ancestors[0]).?;
if (node.toOptional() == ptr.ast.sentinel) {
return analyser.resolveInstanceOfNode(.of(ptr.ast.child_type, handle));
}
if (node.toOptional() == ptr.ast.addrspace_node) {
return analyser.instanceStdBuiltinType("AddressSpace");
}
},
.array_type_sentinel => {
const array_type = tree.fullArrayType(ancestors[0]).?;
if (node.toOptional() == array_type.ast.sentinel) {
return analyser.resolveInstanceOfNode(.of(array_type.ast.elem_type, handle));
}
},

.equal_equal, .bang_equal => {
const lhs, const rhs = tree.nodeData(ancestors[0]).node_and_node;
Expand Down Expand Up @@ -6253,6 +6293,41 @@ pub fn resolveExpressionTypeFromAncestors(

return null;
},
.fn_proto_simple,
.fn_proto_multi,
.fn_proto_one,
.fn_proto,
=> {
var buf: [1]Ast.Node.Index = undefined;
const proto = tree.fullFnProto(&buf, ancestors[0]).?;
if (node.toOptional() == proto.ast.addrspace_expr) {
return analyser.instanceStdBuiltinType("AddressSpace");
}
if (node.toOptional() == proto.ast.callconv_expr) {
return analyser.instanceStdBuiltinType("CallingConvention");
}
if (node.toOptional() == proto.ast.section_expr) {
return .{
.data = .{
.pointer = .{
.size = .slice,
.sentinel = .none,
.is_const = true,
.elem_ty = try analyser.allocType(.fromIP(analyser, .type_type, .u8_type)),
},
},
.is_type_val = false,
};
}
},
.asm_simple,
.@"asm",
=> {
const full = tree.fullAsm(ancestors[0]).?;
if (node.toOptional() == full.ast.clobbers) {
return analyser.instanceStdBuiltinType("assembly.Clobbers");
}
},

else => {}, // TODO: Implement more expressions; better safe than sorry
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,8 @@ fn iterateChildrenTypeErased(
for (asm_node.inputs) |input_node| {
try callback(context, tree, tree.nodeData(input_node).node_and_token[0]);
}

if (asm_node.ast.clobbers.unwrap()) |clobbers| try callback(context, tree, clobbers);
},

.asm_output,
Expand Down
17 changes: 17 additions & 0 deletions tests/analysis/language_constructs.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn func() addrspace(.generic) linksection(.{}) callconv(.auto) void {}
// ^^^^^^^^ (AddressSpace)()
// ^ ([]const u8)()
// ^^^^^ (void)()
const variable addrspace(.generic) linksection(.{}) = 0;
// ^^^^^^^^ (AddressSpace)()
// ^ ([]const u8)()

const pointer = [*:.{}]addrspace(.generic) const struct {};
// ^ (struct {})()
// ^^^^^^^^ (AddressSpace)()
const array = [0:.{}]struct {};
// ^ (struct {})()

const assembly = asm ("" ::: .{ .memory = true });
// ^ (either type)()
// ^^^^^^^ (bool)()