zig/lib/std/c/ast.zig

682 lines
20 KiB
Zig
Raw Normal View History

2020-01-05 03:24:10 -08:00
const std = @import("std");
2020-01-04 04:34:00 -08:00
const SegmentedList = std.SegmentedList;
const Token = std.c.Token;
const Source = std.c.tokenizer.Source;
pub const TokenIndex = usize;
pub const Tree = struct {
tokens: TokenList,
sources: SourceList,
root_node: *Node.Root,
arena_allocator: std.heap.ArenaAllocator,
msgs: MsgList,
2020-01-04 04:34:00 -08:00
pub const SourceList = SegmentedList(Source, 4);
pub const TokenList = Source.TokenList;
pub const MsgList = SegmentedList(Msg, 0);
2020-01-04 04:34:00 -08:00
pub fn deinit(self: *Tree) void {
// Here we copy the arena allocator into stack memory, because
// otherwise it would destroy itself while it was still working.
var arena_allocator = self.arena_allocator;
arena_allocator.deinit();
// self is destroyed
}
2020-01-07 06:05:13 -08:00
2020-01-07 12:43:44 -08:00
pub fn tokenSlice(tree: *Tree, token: TokenIndex) []const u8 {
return tree.tokens.at(token).slice();
}
pub fn tokenEql(tree: *Tree, a: TokenIndex, b: TokenIndex) bool {
const atok = tree.tokens.at(a);
const btok = tree.tokens.at(b);
return atok.eql(btok.*);
2020-01-07 06:05:13 -08:00
}
2020-01-04 04:34:00 -08:00
};
pub const Msg = struct {
kind: enum {
Error,
Warning,
Note,
},
inner: Error,
};
2020-01-04 04:34:00 -08:00
pub const Error = union(enum) {
2020-01-04 14:33:34 -08:00
InvalidToken: SingleTokenError("invalid token '{}'"),
2020-01-04 12:27:05 -08:00
ExpectedToken: ExpectedToken,
2020-01-04 14:33:34 -08:00
ExpectedExpr: SingleTokenError("expected expression, found '{}'"),
2020-01-05 03:24:10 -08:00
ExpectedTypeName: SingleTokenError("expected type name, found '{}'"),
2020-01-05 05:15:55 -08:00
ExpectedFnBody: SingleTokenError("expected function body, found '{}'"),
2020-01-06 04:41:53 -08:00
ExpectedDeclarator: SingleTokenError("expected declarator, found '{}'"),
2020-01-05 05:15:55 -08:00
ExpectedInitializer: SingleTokenError("expected initializer, found '{}'"),
ExpectedEnumField: SingleTokenError("expected enum field, found '{}'"),
ExpectedType: SingleTokenError("expected enum field, found '{}'"),
2020-01-04 14:33:34 -08:00
InvalidTypeSpecifier: InvalidTypeSpecifier,
2020-01-07 10:15:57 -08:00
InvalidStorageClass: SingleTokenError("invalid storage class, found '{}'"),
InvalidDeclarator: SimpleError("invalid declarator"),
2020-01-04 14:33:34 -08:00
DuplicateQualifier: SingleTokenError("duplicate type qualifier '{}'"),
2020-01-05 03:24:10 -08:00
DuplicateSpecifier: SingleTokenError("duplicate declaration specifier '{}'"),
MustUseKwToRefer: MustUseKwToRefer,
2020-01-07 10:15:57 -08:00
FnSpecOnNonFn: SingleTokenError("function specifier '{}' on non function"),
NothingDeclared: SimpleError("declaration doesn't declare anything"),
QualifierIgnored: SingleTokenError("qualifier '{}' ignored"),
2020-01-04 04:34:00 -08:00
2020-01-07 06:05:13 -08:00
pub fn render(self: *const Error, tree: *Tree, stream: var) !void {
2020-01-04 04:34:00 -08:00
switch (self.*) {
2020-01-07 06:05:13 -08:00
.InvalidToken => |*x| return x.render(tree, stream),
.ExpectedToken => |*x| return x.render(tree, stream),
.ExpectedExpr => |*x| return x.render(tree, stream),
.ExpectedTypeName => |*x| return x.render(tree, stream),
.ExpectedDeclarator => |*x| return x.render(tree, stream),
.ExpectedFnBody => |*x| return x.render(tree, stream),
.ExpectedInitializer => |*x| return x.render(tree, stream),
.ExpectedEnumField => |*x| return x.render(tree, stream),
.ExpectedType => |*x| return x.render(tree, stream),
2020-01-07 06:05:13 -08:00
.InvalidTypeSpecifier => |*x| return x.render(tree, stream),
2020-01-07 10:15:57 -08:00
.InvalidStorageClass => |*x| return x.render(tree, stream),
.InvalidDeclarator => |*x| return x.render(tree, stream),
2020-01-07 06:05:13 -08:00
.DuplicateQualifier => |*x| return x.render(tree, stream),
.DuplicateSpecifier => |*x| return x.render(tree, stream),
.MustUseKwToRefer => |*x| return x.render(tree, stream),
2020-01-07 10:15:57 -08:00
.FnSpecOnNonFn => |*x| return x.render(tree, stream),
.NothingDeclared => |*x| return x.render(tree, stream),
.QualifierIgnored => |*x| return x.render(tree, stream),
2020-01-04 04:34:00 -08:00
}
}
pub fn loc(self: *const Error) TokenIndex {
switch (self.*) {
.InvalidToken => |x| return x.token,
2020-01-04 12:27:05 -08:00
.ExpectedToken => |x| return x.token,
.ExpectedExpr => |x| return x.token,
2020-01-05 03:24:10 -08:00
.ExpectedTypeName => |x| return x.token,
2020-01-05 05:15:55 -08:00
.ExpectedDeclarator => |x| return x.token,
.ExpectedFnBody => |x| return x.token,
2020-01-06 04:41:53 -08:00
.ExpectedInitializer => |x| return x.token,
.ExpectedEnumField => |x| return x.token,
.ExpectedType => |*x| return x.token,
2020-01-04 14:33:34 -08:00
.InvalidTypeSpecifier => |x| return x.token,
2020-01-07 10:15:57 -08:00
.InvalidStorageClass => |x| return x.token,
.InvalidDeclarator => |x| return x.token,
2020-01-04 14:33:34 -08:00
.DuplicateQualifier => |x| return x.token,
2020-01-05 03:24:10 -08:00
.DuplicateSpecifier => |x| return x.token,
.MustUseKwToRefer => |*x| return x.name,
2020-01-07 10:15:57 -08:00
.FnSpecOnNonFn => |*x| return x.name,
.NothingDeclared => |*x| return x.name,
.QualifierIgnored => |*x| return x.name,
2020-01-04 04:34:00 -08:00
}
}
2020-01-04 12:27:05 -08:00
pub const ExpectedToken = struct {
token: TokenIndex,
expected_id: @TagType(Token.Id),
2020-01-07 06:05:13 -08:00
pub fn render(self: *const ExpectedToken, tree: *Tree, stream: var) !void {
const found_token = tree.tokens.at(self.token);
2020-01-04 12:27:05 -08:00
if (found_token.id == .Invalid) {
return stream.print("expected '{}', found invalid bytes", .{self.expected_id.symbol()});
} else {
const token_name = found_token.id.symbol();
return stream.print("expected '{}', found '{}'", .{ self.expected_id.symbol(), token_name });
}
}
};
2020-01-04 04:34:00 -08:00
2020-01-04 14:33:34 -08:00
pub const InvalidTypeSpecifier = struct {
token: TokenIndex,
2020-01-05 03:24:10 -08:00
type_spec: *Node.TypeSpec,
2020-01-04 14:33:34 -08:00
2020-01-07 06:05:13 -08:00
pub fn render(self: *const ExpectedToken, tree: *Tree, stream: var) !void {
2020-01-04 14:33:34 -08:00
try stream.write("invalid type specifier '");
2020-01-07 06:05:13 -08:00
try type_spec.spec.print(tree, stream);
const token_name = tree.tokens.at(self.token).id.symbol();
2020-01-05 05:15:55 -08:00
return stream.print("{}'", .{token_name});
2020-01-04 14:33:34 -08:00
}
};
pub const MustUseKwToRefer = struct {
kw: TokenIndex,
name: TokenIndex,
pub fn render(self: *const ExpectedToken, tree: *Tree, stream: var) !void {
2020-01-07 10:15:57 -08:00
return stream.print("must use '{}' tag to refer to type '{}'", .{ tree.slice(kw), tree.slice(name) });
}
};
2020-01-04 04:34:00 -08:00
fn SingleTokenError(comptime msg: []const u8) type {
return struct {
token: TokenIndex,
2020-01-07 06:05:13 -08:00
pub fn render(self: *const @This(), tree: *Tree, stream: var) !void {
const actual_token = tree.tokens.at(self.token);
2020-01-04 04:34:00 -08:00
return stream.print(msg, .{actual_token.id.symbol()});
}
};
}
2020-01-07 10:15:57 -08:00
fn SimpleError(comptime msg: []const u8) type {
return struct {
const ThisError = @This();
token: TokenIndex,
pub fn render(self: *const ThisError, tokens: *Tree.TokenList, stream: var) !void {
return stream.write(msg);
}
};
}
2020-01-04 04:34:00 -08:00
};
2020-01-07 06:05:13 -08:00
pub const Type = struct {
pub const TypeList = std.SegmentedList(*Type, 4);
@"const": bool = false,
atomic: bool = false,
@"volatile": bool = false,
restrict: bool = false,
2020-01-07 06:05:13 -08:00
id: union(enum) {
Int: struct {
id: Id,
is_signed: bool,
pub const Id = enum {
Char,
Short,
Int,
Long,
LongLong,
};
},
Float: struct {
id: Id,
pub const Id = enum {
Float,
Double,
LongDouble,
};
},
Pointer: *Type,
2020-01-07 06:05:13 -08:00
Function: struct {
return_type: *Type,
param_types: TypeList,
},
Typedef: *Type,
Record: *Node.RecordType,
Enum: *Node.EnumType,
2020-01-07 12:43:44 -08:00
/// Special case for macro parameters that can be any type.
/// Only present if `retain_macros == true`.
Macro,
2020-01-07 06:05:13 -08:00
},
};
2020-01-04 11:26:43 -08:00
pub const Node = struct {
id: Id,
2020-01-04 04:34:00 -08:00
2020-01-04 11:26:43 -08:00
pub const Id = enum {
Root,
EnumField,
RecordField,
2020-01-14 06:18:32 -08:00
RecordDeclarator,
2020-01-04 11:26:43 -08:00
JumpStmt,
ExprStmt,
2020-01-11 11:46:36 -08:00
LabeledStmt,
2020-01-04 12:27:05 -08:00
CompoundStmt,
IfStmt,
2020-01-11 11:46:36 -08:00
SwitchStmt,
2020-01-07 14:00:14 -08:00
WhileStmt,
DoStmt,
ForStmt,
2020-01-05 05:15:55 -08:00
StaticAssert,
2020-01-07 10:15:57 -08:00
Declarator,
Pointer,
FnDecl,
2020-01-05 14:06:33 -08:00
Typedef,
2020-01-07 10:15:57 -08:00
VarDecl,
2020-01-04 11:26:43 -08:00
};
pub const Root = struct {
2020-01-05 05:15:55 -08:00
base: Node = Node{ .id = .Root },
2020-01-04 11:26:43 -08:00
decls: DeclList,
eof: TokenIndex,
2020-01-04 04:34:00 -08:00
2020-01-04 11:26:43 -08:00
pub const DeclList = SegmentedList(*Node, 4);
};
2020-01-04 04:34:00 -08:00
2020-01-05 03:24:10 -08:00
pub const DeclSpec = struct {
storage_class: union(enum) {
Auto: TokenIndex,
Extern: TokenIndex,
Register: TokenIndex,
Static: TokenIndex,
Typedef: TokenIndex,
None,
} = .None,
thread_local: ?TokenIndex = null,
type_spec: TypeSpec = TypeSpec{},
fn_spec: union(enum) {
Inline: TokenIndex,
Noreturn: TokenIndex,
None,
} = .None,
align_spec: ?struct {
alignas: TokenIndex,
expr: *Node,
rparen: TokenIndex,
} = null,
};
pub const TypeSpec = struct {
qual: TypeQual = TypeQual{},
spec: union(enum) {
2020-01-04 14:33:34 -08:00
/// error or default to int
None,
Void: TokenIndex,
Char: struct {
sign: ?TokenIndex = null,
char: TokenIndex,
},
Short: struct {
sign: ?TokenIndex = null,
short: TokenIndex = null,
int: ?TokenIndex = null,
},
Int: struct {
sign: ?TokenIndex = null,
int: ?TokenIndex = null,
},
Long: struct {
sign: ?TokenIndex = null,
long: TokenIndex,
longlong: ?TokenIndex = null,
int: ?TokenIndex = null,
},
Float: struct {
float: TokenIndex,
complex: ?TokenIndex = null,
},
Double: struct {
long: ?TokenIndex = null,
double: ?TokenIndex,
complex: ?TokenIndex = null,
},
Bool: TokenIndex,
Atomic: struct {
atomic: TokenIndex,
typename: *Node,
rparen: TokenIndex,
},
2020-01-07 06:05:13 -08:00
Enum: *EnumType,
Record: *RecordType,
Typedef: struct {
sym: TokenIndex,
sym_type: *Type,
},
2020-01-04 14:33:34 -08:00
2020-01-07 06:05:13 -08:00
pub fn print(self: *@This(), self: *const @This(), tree: *Tree, stream: var) !void {
switch (self.spec) {
2020-01-04 14:33:34 -08:00
.None => unreachable,
2020-01-07 06:05:13 -08:00
.Void => |index| try stream.write(tree.slice(index)),
.Char => |char| {
if (char.sign) |s| {
try stream.write(tree.slice(s));
try stream.writeByte(' ');
}
try stream.write(tree.slice(char.char));
},
.Short => |short| {
if (short.sign) |s| {
try stream.write(tree.slice(s));
try stream.writeByte(' ');
}
try stream.write(tree.slice(short.short));
if (short.int) |i| {
try stream.writeByte(' ');
try stream.write(tree.slice(i));
}
},
.Int => |int| {
if (int.sign) |s| {
try stream.write(tree.slice(s));
try stream.writeByte(' ');
}
if (int.int) |i| {
try stream.writeByte(' ');
try stream.write(tree.slice(i));
}
},
.Long => |long| {
if (long.sign) |s| {
try stream.write(tree.slice(s));
try stream.writeByte(' ');
}
try stream.write(tree.slice(long.long));
if (long.longlong) |l| {
try stream.writeByte(' ');
try stream.write(tree.slice(l));
}
if (long.int) |i| {
try stream.writeByte(' ');
try stream.write(tree.slice(i));
}
},
.Float => |float| {
try stream.write(tree.slice(float.float));
if (float.complex) |c| {
try stream.writeByte(' ');
try stream.write(tree.slice(c));
}
},
.Double => |double| {
if (double.long) |l| {
try stream.write(tree.slice(l));
try stream.writeByte(' ');
}
try stream.write(tree.slice(double.double));
if (double.complex) |c| {
try stream.writeByte(' ');
try stream.write(tree.slice(c));
}
},
.Bool => |index| try stream.write(tree.slice(index)),
.Typedef => |typedef| try stream.write(tree.slice(typedef.sym)),
else => try stream.print("TODO print {}", self.spec),
2020-01-04 14:33:34 -08:00
}
}
2020-01-05 03:24:10 -08:00
} = .None,
2020-01-04 14:33:34 -08:00
};
2020-01-07 06:05:13 -08:00
pub const EnumType = struct {
tok: TokenIndex,
name: ?TokenIndex,
body: ?struct {
lbrace: TokenIndex,
/// always EnumField
fields: FieldList,
rbrace: TokenIndex,
},
pub const FieldList = Root.DeclList;
};
pub const EnumField = struct {
base: Node = Node{ .id = .EnumField },
2020-01-07 06:05:13 -08:00
name: TokenIndex,
value: ?*Node,
};
pub const RecordType = struct {
tok: TokenIndex,
kind: enum {
Struct,
Union,
2020-01-07 06:05:13 -08:00
},
name: ?TokenIndex,
body: ?struct {
lbrace: TokenIndex,
/// RecordField or StaticAssert
fields: FieldList,
rbrace: TokenIndex,
},
pub const FieldList = Root.DeclList;
};
pub const RecordField = struct {
base: Node = Node{ .id = .RecordField },
type_spec: TypeSpec,
declarators: DeclaratorList,
semicolon: TokenIndex,
pub const DeclaratorList = Root.DeclList;
2020-01-07 06:05:13 -08:00
};
pub const RecordDeclarator = struct {
2020-01-14 06:18:32 -08:00
base: Node = Node{ .id = .RecordDeclarator },
declarator: ?*Declarator,
2020-01-07 14:00:14 -08:00
bit_field_expr: ?*Expr,
};
2020-01-05 03:24:10 -08:00
pub const TypeQual = struct {
2020-01-04 14:33:34 -08:00
@"const": ?TokenIndex = null,
atomic: ?TokenIndex = null,
@"volatile": ?TokenIndex = null,
restrict: ?TokenIndex = null,
};
2020-01-04 11:26:43 -08:00
pub const JumpStmt = struct {
2020-01-04 12:27:05 -08:00
base: Node = Node{ .id = .JumpStmt },
2020-01-04 11:26:43 -08:00
ltoken: TokenIndex,
2020-01-11 11:46:36 -08:00
kind: union(enum) {
2020-01-04 11:26:43 -08:00
Break,
Continue,
Return: ?*Node,
Goto: TokenIndex,
2020-01-11 11:46:36 -08:00
},
semicolon: TokenIndex,
2020-01-04 11:26:43 -08:00
};
pub const ExprStmt = struct {
2020-01-04 12:27:05 -08:00
base: Node = Node{ .id = .ExprStmt },
2020-01-11 11:46:36 -08:00
expr: ?*Expr,
2020-01-04 11:26:43 -08:00
semicolon: TokenIndex,
};
2020-01-11 11:46:36 -08:00
pub const LabeledStmt = struct {
base: Node = Node{ .id = .LabeledStmt },
kind: union(enum) {
Label: TokenIndex,
Case: TokenIndex,
Default: TokenIndex,
},
stmt: *Node,
2020-01-04 11:26:43 -08:00
};
2020-01-04 12:27:05 -08:00
pub const CompoundStmt = struct {
base: Node = Node{ .id = .CompoundStmt },
lbrace: TokenIndex,
statements: StmtList,
rbrace: TokenIndex,
pub const StmtList = Root.DeclList;
};
pub const IfStmt = struct {
base: Node = Node{ .id = .IfStmt },
@"if": TokenIndex,
cond: *Node,
2020-01-07 14:00:14 -08:00
body: *Node,
2020-01-04 12:27:05 -08:00
@"else": ?struct {
tok: TokenIndex,
2020-01-07 14:00:14 -08:00
body: *Node,
2020-01-04 12:27:05 -08:00
},
};
2020-01-05 05:15:55 -08:00
2020-01-11 11:46:36 -08:00
pub const SwitchStmt = struct {
base: Node = Node{ .id = .SwitchStmt },
@"switch": TokenIndex,
expr: *Expr,
rparen: TokenIndex,
stmt: *Node,
};
2020-01-07 14:00:14 -08:00
pub const WhileStmt = struct {
base: Node = Node{ .id = .WhileStmt },
@"while": TokenIndex,
cond: *Expr,
rparen: TokenIndex,
body: *Node,
};
pub const DoStmt = struct {
base: Node = Node{ .id = .DoStmt },
do: TokenIndex,
body: *Node,
@"while": TokenIndex,
cond: *Expr,
semicolon: TokenIndex,
};
pub const ForStmt = struct {
base: Node = Node{ .id = .ForStmt },
@"for": TokenIndex,
init: ?*Node,
cond: ?*Expr,
semicolon: TokenIndex,
incr: ?*Expr,
rparen: TokenIndex,
body: *Node,
};
2020-01-05 05:15:55 -08:00
pub const StaticAssert = struct {
base: Node = Node{ .id = .StaticAssert },
assert: TokenIndex,
expr: *Node,
semicolon: TokenIndex,
};
2020-01-05 14:06:33 -08:00
pub const Declarator = struct {
2020-01-06 04:41:53 -08:00
base: Node = Node{ .id = .Declarator },
2020-01-07 10:15:57 -08:00
pointer: ?*Pointer,
2020-01-06 04:41:53 -08:00
prefix: union(enum) {
None,
Identifer: TokenIndex,
2020-01-05 14:06:33 -08:00
Complex: struct {
lparen: TokenIndex,
2020-01-06 04:41:53 -08:00
inner: *Node,
2020-01-05 14:06:33 -08:00
rparen: TokenIndex,
},
2020-01-06 04:41:53 -08:00
},
suffix: union(enum) {
None,
Fn: struct {
lparen: TokenIndex,
params: Params,
rparen: TokenIndex,
},
Array: Arrays,
2020-01-05 14:06:33 -08:00
},
2020-01-06 04:41:53 -08:00
pub const Arrays = std.SegmentedList(*Array, 2);
pub const Params = std.SegmentedList(*Param, 4);
2020-01-05 14:06:33 -08:00
};
2020-01-06 04:41:53 -08:00
pub const Array = struct {
2020-01-07 10:15:57 -08:00
lbracket: TokenIndex,
2020-01-06 04:41:53 -08:00
inner: union(enum) {
Inferred,
Unspecified: TokenIndex,
Variable: struct {
asterisk: ?TokenIndex,
static: ?TokenIndex,
qual: TypeQual,
expr: *Expr,
2020-01-06 04:41:53 -08:00
},
},
rbracket: TokenIndex,
2020-01-05 14:06:33 -08:00
};
pub const Pointer = struct {
2020-01-06 04:41:53 -08:00
base: Node = Node{ .id = .Pointer },
2020-01-05 14:06:33 -08:00
asterisk: TokenIndex,
qual: TypeQual,
pointer: ?*Pointer,
};
pub const Param = struct {
kind: union(enum) {
Variable,
Old: TokenIndex,
Normal: struct {
decl_spec: *DeclSpec,
2020-01-06 04:41:53 -08:00
declarator: *Node,
2020-01-05 14:06:33 -08:00
},
},
};
2020-01-07 10:15:57 -08:00
pub const FnDecl = struct {
base: Node = Node{ .id = .FnDecl },
decl_spec: DeclSpec,
2020-01-07 10:15:57 -08:00
declarator: *Declarator,
2020-01-05 05:15:55 -08:00
old_decls: OldDeclList,
2020-01-05 14:06:33 -08:00
body: ?*CompoundStmt,
2020-01-05 05:15:55 -08:00
pub const OldDeclList = SegmentedList(*Node, 0);
};
2020-01-05 14:06:33 -08:00
pub const Typedef = struct {
base: Node = Node{ .id = .Typedef },
decl_spec: DeclSpec,
declarators: DeclaratorList,
2020-01-07 10:15:57 -08:00
semicolon: TokenIndex,
2020-01-05 14:06:33 -08:00
2020-01-06 04:41:53 -08:00
pub const DeclaratorList = Root.DeclList;
2020-01-05 14:06:33 -08:00
};
2020-01-07 10:15:57 -08:00
pub const VarDecl = struct {
base: Node = Node{ .id = .VarDecl },
2020-01-05 14:06:33 -08:00
decl_spec: DeclSpec,
initializers: Initializers,
2020-01-07 10:15:57 -08:00
semicolon: TokenIndex,
2020-01-05 14:06:33 -08:00
2020-01-07 10:15:57 -08:00
pub const Initializers = Root.DeclList;
2020-01-05 14:06:33 -08:00
};
pub const Initialized = struct {
2020-01-07 10:15:57 -08:00
base: Node = Node{ .id = Initialized },
declarator: *Declarator,
2020-01-05 14:06:33 -08:00
eq: TokenIndex,
init: Initializer,
};
pub const Initializer = union(enum) {
list: struct {
initializers: InitializerList,
rbrace: TokenIndex,
},
expr: *Expr,
pub const InitializerList = std.SegmentedList(*Initializer, 4);
};
2020-01-07 12:43:44 -08:00
pub const Macro = struct {
base: Node = Node{ .id = Macro },
kind: union(enum) {
Undef: []const u8,
Fn: struct {
params: []const []const u8,
expr: *Expr,
},
Expr: *Expr,
},
};
2020-01-04 11:26:43 -08:00
};
pub const Expr = struct {
id: Id,
ty: *Type,
value: union(enum) {
None,
},
pub const Id = enum {
Infix,
Literal,
};
pub const Infix = struct {
base: Expr = Expr{ .id = .Infix },
lhs: *Expr,
op_token: TokenIndex,
op: Op,
rhs: *Expr,
pub const Op = enum {};
};
};