mirror of
https://github.com/ziglang/zig.git
synced 2026-01-25 00:35:20 +00:00
stage2: astgen: fix most of the remaining compile errors
more progress on converting astgen to the new AST memory layout. only a few code paths left to update.
This commit is contained in:
parent
29daf10639
commit
9010bd8aec
@ -1754,25 +1754,20 @@ pub const Tree = struct {
|
||||
const token_tags = tree.tokens.items(.tag);
|
||||
// TODO: looks like stage1 isn't quite smart enough to handle enum
|
||||
// literals in some places here
|
||||
const Kind = full.PtrType.Kind;
|
||||
const kind: Kind = switch (token_tags[info.main_token]) {
|
||||
const Size = std.builtin.TypeInfo.Pointer.Size;
|
||||
const size: Size = switch (token_tags[info.main_token]) {
|
||||
.asterisk,
|
||||
.asterisk_asterisk,
|
||||
=> switch (token_tags[info.main_token + 1]) {
|
||||
.r_bracket => .many,
|
||||
.colon => .sentinel,
|
||||
.identifier => if (token_tags[info.main_token - 1] == .l_bracket) Kind.c else .one,
|
||||
else => .one,
|
||||
},
|
||||
.l_bracket => switch (token_tags[info.main_token + 1]) {
|
||||
.r_bracket => Kind.slice,
|
||||
.colon => .slice_sentinel,
|
||||
else => unreachable,
|
||||
.r_bracket, .colon => .Many,
|
||||
.identifier => if (token_tags[info.main_token - 1] == .l_bracket) Size.C else .One,
|
||||
else => .One,
|
||||
},
|
||||
.l_bracket => Size.Slice,
|
||||
else => unreachable,
|
||||
};
|
||||
var result: full.PtrType = .{
|
||||
.kind = kind,
|
||||
.size = size,
|
||||
.allowzero_token = null,
|
||||
.const_token = null,
|
||||
.volatile_token = null,
|
||||
@ -1782,13 +1777,7 @@ pub const Tree = struct {
|
||||
// here while looking for modifiers as that could result in false
|
||||
// positives. Therefore, start after a sentinel if there is one and
|
||||
// skip over any align node and bit range nodes.
|
||||
var i = if (kind == .sentinel or kind == .slice_sentinel) blk: {
|
||||
assert(info.sentinel != 0);
|
||||
break :blk tree.lastToken(info.sentinel) + 1;
|
||||
} else blk: {
|
||||
assert(info.sentinel == 0);
|
||||
break :blk info.main_token;
|
||||
};
|
||||
var i = if (info.sentinel != 0) tree.lastToken(info.sentinel) + 1 else info.main_token;
|
||||
const end = tree.firstToken(info.child_type);
|
||||
while (i < end) : (i += 1) {
|
||||
switch (token_tags[i]) {
|
||||
@ -2115,7 +2104,7 @@ pub const full = struct {
|
||||
.comptime_noalias = comptime_noalias,
|
||||
.name_token = name_token,
|
||||
.anytype_ellipsis3 = it.tok_i - 1,
|
||||
.type_expr = param_type,
|
||||
.type_expr = 0,
|
||||
};
|
||||
}
|
||||
it.tok_flag = false;
|
||||
@ -2166,21 +2155,12 @@ pub const full = struct {
|
||||
};
|
||||
|
||||
pub const PtrType = struct {
|
||||
kind: Kind,
|
||||
size: std.builtin.TypeInfo.Pointer.Size,
|
||||
allowzero_token: ?TokenIndex,
|
||||
const_token: ?TokenIndex,
|
||||
volatile_token: ?TokenIndex,
|
||||
ast: Ast,
|
||||
|
||||
pub const Kind = enum {
|
||||
one,
|
||||
many,
|
||||
sentinel,
|
||||
c,
|
||||
slice,
|
||||
slice_sentinel,
|
||||
};
|
||||
|
||||
pub const Ast = struct {
|
||||
main_token: TokenIndex,
|
||||
align_node: Node.Index,
|
||||
|
||||
@ -677,8 +677,8 @@ fn renderPtrType(
|
||||
ptr_type: ast.full.PtrType,
|
||||
space: Space,
|
||||
) Error!void {
|
||||
switch (ptr_type.kind) {
|
||||
.one => {
|
||||
switch (ptr_type.size) {
|
||||
.One => {
|
||||
// Since ** tokens exist and the same token is shared by two
|
||||
// nested pointer types, we check to see if we are the parent
|
||||
// in such a relationship. If so, skip rendering anything for
|
||||
@ -691,33 +691,35 @@ fn renderPtrType(
|
||||
}
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
},
|
||||
.many => {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token - 1, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // rbracket
|
||||
.Many => {
|
||||
if (ptr_type.ast.sentinel == 0) {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token - 1, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // rbracket
|
||||
} else {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token - 1, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // colon
|
||||
try renderExpression(ais, tree, ptr_type.ast.sentinel, .none);
|
||||
try renderToken(ais, tree, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
|
||||
}
|
||||
},
|
||||
.sentinel => {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token - 1, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // colon
|
||||
try renderExpression(ais, tree, ptr_type.ast.sentinel, .none);
|
||||
try renderToken(ais, tree, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
|
||||
},
|
||||
.c => {
|
||||
.C => {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token - 1, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // asterisk
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // c
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 2, .none); // rbracket
|
||||
},
|
||||
.slice => {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // rbracket
|
||||
},
|
||||
.slice_sentinel => {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // colon
|
||||
try renderExpression(ais, tree, ptr_type.ast.sentinel, .none);
|
||||
try renderToken(ais, tree, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
|
||||
.Slice => {
|
||||
if (ptr_type.ast.sentinel == 0) {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // rbracket
|
||||
} else {
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token, .none); // lbracket
|
||||
try renderToken(ais, tree, ptr_type.ast.main_token + 1, .none); // colon
|
||||
try renderExpression(ais, tree, ptr_type.ast.sentinel, .none);
|
||||
try renderToken(ais, tree, tree.lastToken(ptr_type.ast.sentinel) + 1, .none); // rbracket
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
1043
src/BuiltinFn.zig
1043
src/BuiltinFn.zig
File diff suppressed because it is too large
Load Diff
@ -430,12 +430,12 @@ pub const Scope = struct {
|
||||
/// Asserts the scope is a child of a File and has an AST tree and returns the tree.
|
||||
pub fn tree(self: *Scope) *const ast.Tree {
|
||||
switch (self.tag) {
|
||||
.file => return self.cast(File).?.tree,
|
||||
.block => return self.cast(Block).?.src_decl.container.file_scope.tree,
|
||||
.gen_zir => return self.cast(GenZIR).?.decl.container.file_scope.tree,
|
||||
.local_val => return self.cast(LocalVal).?.gen_zir.decl.container.file_scope.tree,
|
||||
.local_ptr => return self.cast(LocalPtr).?.gen_zir.decl.container.file_scope.tree,
|
||||
.container => return self.cast(Container).?.file_scope.tree,
|
||||
.file => return &self.cast(File).?.tree,
|
||||
.block => return &self.cast(Block).?.src_decl.container.file_scope.tree,
|
||||
.gen_zir => return &self.cast(GenZIR).?.decl.container.file_scope.tree,
|
||||
.local_val => return &self.cast(LocalVal).?.gen_zir.decl.container.file_scope.tree,
|
||||
.local_ptr => return &self.cast(LocalPtr).?.gen_zir.decl.container.file_scope.tree,
|
||||
.container => return &self.cast(Container).?.file_scope.tree,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1612,6 +1612,7 @@ fn astgenAndSemaVarDecl(
|
||||
.decl = decl,
|
||||
.arena = &type_scope_arena.allocator,
|
||||
.parent = &decl.container.base,
|
||||
.force_comptime = true,
|
||||
};
|
||||
defer type_scope.instructions.deinit(mod.gpa);
|
||||
|
||||
@ -1630,7 +1631,7 @@ fn astgenAndSemaVarDecl(
|
||||
} else {
|
||||
return mod.failTok(
|
||||
&block_scope.base,
|
||||
tree.firstToken(var_decl),
|
||||
var_decl.ast.mut_token,
|
||||
"unable to infer variable type",
|
||||
.{},
|
||||
);
|
||||
@ -1639,7 +1640,7 @@ fn astgenAndSemaVarDecl(
|
||||
if (is_mutable and !var_info.ty.isValidVarType(is_extern)) {
|
||||
return mod.failTok(
|
||||
&block_scope.base,
|
||||
tree.firstToken(var_decl),
|
||||
var_decl.ast.mut_token,
|
||||
"variable of type '{}' must be const",
|
||||
.{var_info.ty},
|
||||
);
|
||||
|
||||
699
src/astgen.zig
699
src/astgen.zig
File diff suppressed because it is too large
Load Diff
16
src/zir.zig
16
src/zir.zig
@ -863,8 +863,8 @@ pub const Inst = struct {
|
||||
kw_args: struct {
|
||||
@"volatile": bool = false,
|
||||
output: ?*Inst = null,
|
||||
inputs: []*Inst = &[0]*Inst{},
|
||||
clobbers: []*Inst = &[0]*Inst{},
|
||||
inputs: []const []const u8 = &.{},
|
||||
clobbers: []const []const u8 = &.{},
|
||||
args: []*Inst = &[0]*Inst{},
|
||||
},
|
||||
};
|
||||
@ -1192,16 +1192,9 @@ pub const Inst = struct {
|
||||
},
|
||||
kw_args: struct {
|
||||
init_inst: ?*Inst = null,
|
||||
init_kind: InitKind = .none,
|
||||
has_enum_token: bool,
|
||||
layout: std.builtin.TypeInfo.ContainerLayout = .Auto,
|
||||
},
|
||||
|
||||
// TODO error: values of type '(enum literal)' must be comptime known
|
||||
pub const InitKind = enum {
|
||||
enum_type,
|
||||
tag_type,
|
||||
none,
|
||||
};
|
||||
};
|
||||
|
||||
pub const SwitchBr = struct {
|
||||
@ -1413,6 +1406,7 @@ const Writer = struct {
|
||||
}
|
||||
switch (@TypeOf(param)) {
|
||||
*Inst => return self.writeInstParamToStream(stream, param),
|
||||
?*Inst => return self.writeInstParamToStream(stream, param.?),
|
||||
[]*Inst => {
|
||||
try stream.writeByte('[');
|
||||
for (param) |inst, i| {
|
||||
@ -1480,7 +1474,7 @@ const Writer = struct {
|
||||
const name = self.loop_table.get(param).?;
|
||||
return stream.print("\"{}\"", .{std.zig.fmtEscapes(name)});
|
||||
},
|
||||
[][]const u8 => {
|
||||
[][]const u8, []const []const u8 => {
|
||||
try stream.writeByte('[');
|
||||
for (param) |str, i| {
|
||||
if (i != 0) {
|
||||
|
||||
@ -2023,19 +2023,21 @@ fn zirDeref(mod: *Module, scope: *Scope, deref: *zir.Inst.UnOp) InnerError!*Inst
|
||||
fn zirAsm(mod: *Module, scope: *Scope, assembly: *zir.Inst.Asm) InnerError!*Inst {
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const return_type = try resolveType(mod, scope, assembly.positionals.return_type);
|
||||
const asm_source = try resolveConstString(mod, scope, assembly.positionals.asm_source);
|
||||
const output = if (assembly.kw_args.output) |o| try resolveConstString(mod, scope, o) else null;
|
||||
|
||||
const inputs = try scope.arena().alloc([]const u8, assembly.kw_args.inputs.len);
|
||||
const clobbers = try scope.arena().alloc([]const u8, assembly.kw_args.clobbers.len);
|
||||
const args = try scope.arena().alloc(*Inst, assembly.kw_args.args.len);
|
||||
const arena = scope.arena();
|
||||
const inputs = try arena.alloc([]const u8, assembly.kw_args.inputs.len);
|
||||
const clobbers = try arena.alloc([]const u8, assembly.kw_args.clobbers.len);
|
||||
const args = try arena.alloc(*Inst, assembly.kw_args.args.len);
|
||||
|
||||
for (inputs) |*elem, i| {
|
||||
elem.* = try resolveConstString(mod, scope, assembly.kw_args.inputs[i]);
|
||||
elem.* = try arena.dupe(u8, assembly.kw_args.inputs[i]);
|
||||
}
|
||||
for (clobbers) |*elem, i| {
|
||||
elem.* = try resolveConstString(mod, scope, assembly.kw_args.clobbers[i]);
|
||||
elem.* = try arena.dupe(u8, assembly.kw_args.clobbers[i]);
|
||||
}
|
||||
for (args) |*elem, i| {
|
||||
const arg = try resolveInst(mod, scope, assembly.kw_args.args[i]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user