Skip to content

Commit 26446bf

Browse files
authored
resolve more result location types (#2558)
- sentinel and address space in pointers - sentinel in arrays - address space, calling convention and section in functions - clobbers in assembly
1 parent 1f4560a commit 26446bf

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/analysis.zig

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,11 @@ const FindBreaks = struct {
18521852
}
18531853
};
18541854

1855+
pub fn resolveInstanceOfNode(analyser: *Analyser, options: ResolveOptions) error{OutOfMemory}!?Type {
1856+
const ty = try analyser.resolveTypeOfNode(options) orelse return null;
1857+
return ty.instanceTypeVal(analyser);
1858+
}
1859+
18551860
/// Resolves the type of an Ast Node.
18561861
/// Returns `null` if the type could not be resolved.
18571862
pub fn resolveTypeOfNode(analyser: *Analyser, options: ResolveOptions) error{OutOfMemory}!?Type {
@@ -6004,6 +6009,22 @@ pub fn resolveExpressionTypeFromAncestors(
60046009
if (node.toOptional() == var_decl.ast.init_node) {
60056010
return try analyser.resolveTypeOfNode(.of(ancestors[0], handle));
60066011
}
6012+
if (node.toOptional() == var_decl.ast.addrspace_node) {
6013+
return analyser.instanceStdBuiltinType("AddressSpace");
6014+
}
6015+
if (node.toOptional() == var_decl.ast.section_node) {
6016+
return .{
6017+
.data = .{
6018+
.pointer = .{
6019+
.size = .slice,
6020+
.sentinel = .none,
6021+
.is_const = true,
6022+
.elem_ty = try analyser.allocType(.fromIP(analyser, .type_type, .u8_type)),
6023+
},
6024+
},
6025+
.is_type_val = false,
6026+
};
6027+
}
60076028
},
60086029
.if_simple,
60096030
.@"if",
@@ -6108,6 +6129,25 @@ pub fn resolveExpressionTypeFromAncestors(
61086129
return try analyser.resolveTypeOfNode(.of(lhs, handle));
61096130
}
61106131
},
6132+
.ptr_type_aligned,
6133+
.ptr_type_sentinel,
6134+
.ptr_type,
6135+
.ptr_type_bit_range,
6136+
=> {
6137+
const ptr = tree.fullPtrType(ancestors[0]).?;
6138+
if (node.toOptional() == ptr.ast.sentinel) {
6139+
return analyser.resolveInstanceOfNode(.of(ptr.ast.child_type, handle));
6140+
}
6141+
if (node.toOptional() == ptr.ast.addrspace_node) {
6142+
return analyser.instanceStdBuiltinType("AddressSpace");
6143+
}
6144+
},
6145+
.array_type_sentinel => {
6146+
const array_type = tree.fullArrayType(ancestors[0]).?;
6147+
if (node.toOptional() == array_type.ast.sentinel) {
6148+
return analyser.resolveInstanceOfNode(.of(array_type.ast.elem_type, handle));
6149+
}
6150+
},
61116151

61126152
.equal_equal, .bang_equal => {
61136153
const lhs, const rhs = tree.nodeData(ancestors[0]).node_and_node;
@@ -6262,6 +6302,41 @@ pub fn resolveExpressionTypeFromAncestors(
62626302

62636303
return null;
62646304
},
6305+
.fn_proto_simple,
6306+
.fn_proto_multi,
6307+
.fn_proto_one,
6308+
.fn_proto,
6309+
=> {
6310+
var buf: [1]Ast.Node.Index = undefined;
6311+
const proto = tree.fullFnProto(&buf, ancestors[0]).?;
6312+
if (node.toOptional() == proto.ast.addrspace_expr) {
6313+
return analyser.instanceStdBuiltinType("AddressSpace");
6314+
}
6315+
if (node.toOptional() == proto.ast.callconv_expr) {
6316+
return analyser.instanceStdBuiltinType("CallingConvention");
6317+
}
6318+
if (node.toOptional() == proto.ast.section_expr) {
6319+
return .{
6320+
.data = .{
6321+
.pointer = .{
6322+
.size = .slice,
6323+
.sentinel = .none,
6324+
.is_const = true,
6325+
.elem_ty = try analyser.allocType(.fromIP(analyser, .type_type, .u8_type)),
6326+
},
6327+
},
6328+
.is_type_val = false,
6329+
};
6330+
}
6331+
},
6332+
.asm_simple,
6333+
.@"asm",
6334+
=> {
6335+
const full = tree.fullAsm(ancestors[0]).?;
6336+
if (node.toOptional() == full.ast.clobbers) {
6337+
return analyser.instanceStdBuiltinType("assembly.Clobbers");
6338+
}
6339+
},
62656340

62666341
else => {}, // TODO: Implement more expressions; better safe than sorry
62676342
}

src/ast.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,8 @@ fn iterateChildrenTypeErased(
14561456
for (asm_node.inputs) |input_node| {
14571457
try callback(context, tree, tree.nodeData(input_node).node_and_token[0]);
14581458
}
1459+
1460+
if (asm_node.ast.clobbers.unwrap()) |clobbers| try callback(context, tree, clobbers);
14591461
},
14601462

14611463
.asm_output,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn func() addrspace(.generic) linksection(.{}) callconv(.auto) void {}
2+
// ^^^^^^^^ (AddressSpace)()
3+
// ^ ([]const u8)()
4+
// ^^^^^ (void)()
5+
const variable addrspace(.generic) linksection(.{}) = 0;
6+
// ^^^^^^^^ (AddressSpace)()
7+
// ^ ([]const u8)()
8+
9+
const pointer = [*:.{}]addrspace(.generic) const struct {};
10+
// ^ (struct {})()
11+
// ^^^^^^^^ (AddressSpace)()
12+
const array = [0:.{}]struct {};
13+
// ^ (struct {})()
14+
15+
const assembly = asm ("" ::: .{ .memory = true });
16+
// ^ (either type)()
17+
// ^^^^^^^ (bool)()

0 commit comments

Comments
 (0)