Skip to content

Commit

Permalink
Address spaces: addrspace(A) parsing
Browse files Browse the repository at this point in the history
The grammar for function prototypes, (global) variable declarations, and
pointer types now accepts an optional addrspace(A) modifier.
  • Loading branch information
Snektron committed Sep 20, 2021
1 parent 9fa723e commit ccc7f99
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 30 deletions.
54 changes: 50 additions & 4 deletions lib/std/zig/Ast.zig
Expand Up @@ -262,6 +262,9 @@ pub fn renderError(tree: Tree, parse_error: Error, stream: anytype) !void {
token_tags[parse_error.token].symbol(),
});
},
.extra_addrspace_qualifier => {
return stream.writeAll("extra addrspace qualifier");
},
.extra_align_qualifier => {
return stream.writeAll("extra align qualifier");
},
Expand Down Expand Up @@ -1021,7 +1024,7 @@ pub fn lastToken(tree: Tree, node: Node.Index) TokenIndex {
},
.fn_proto_one => {
const extra = tree.extraData(datas[n].lhs, Node.FnProtoOne);
// linksection, callconv, align can appear in any order, so we
// addrspace, linksection, callconv, align can appear in any order, so we
// find the last one here.
var max_node: Node.Index = datas[n].rhs;
var max_start = token_starts[main_tokens[max_node]];
Expand All @@ -1034,6 +1037,14 @@ pub fn lastToken(tree: Tree, node: Node.Index) TokenIndex {
max_offset = 1; // for the rparen
}
}
if (extra.addrspace_expr != 0) {
const start = token_starts[main_tokens[extra.addrspace_expr]];
if (start > max_start) {
max_node = extra.addrspace_expr;
max_start = start;
max_offset = 1; // for the rparen
}
}
if (extra.section_expr != 0) {
const start = token_starts[main_tokens[extra.section_expr]];
if (start > max_start) {
Expand All @@ -1055,7 +1066,7 @@ pub fn lastToken(tree: Tree, node: Node.Index) TokenIndex {
},
.fn_proto => {
const extra = tree.extraData(datas[n].lhs, Node.FnProto);
// linksection, callconv, align can appear in any order, so we
// addrspace, linksection, callconv, align can appear in any order, so we
// find the last one here.
var max_node: Node.Index = datas[n].rhs;
var max_start = token_starts[main_tokens[max_node]];
Expand All @@ -1068,6 +1079,14 @@ pub fn lastToken(tree: Tree, node: Node.Index) TokenIndex {
max_offset = 1; // for the rparen
}
}
if (extra.addrspace_expr != 0) {
const start = token_starts[main_tokens[extra.addrspace_expr]];
if (start > max_start) {
max_node = extra.addrspace_expr;
max_start = start;
max_offset = 1; // for the rparen
}
}
if (extra.section_expr != 0) {
const start = token_starts[main_tokens[extra.section_expr]];
if (start > max_start) {
Expand Down Expand Up @@ -1138,6 +1157,7 @@ pub fn globalVarDecl(tree: Tree, node: Node.Index) full.VarDecl {
return tree.fullVarDecl(.{
.type_node = extra.type_node,
.align_node = extra.align_node,
.addrspace_node = extra.addrspace_node,
.section_node = extra.section_node,
.init_node = data.rhs,
.mut_token = tree.nodes.items(.main_token)[node],
Expand All @@ -1151,6 +1171,7 @@ pub fn localVarDecl(tree: Tree, node: Node.Index) full.VarDecl {
return tree.fullVarDecl(.{
.type_node = extra.type_node,
.align_node = extra.align_node,
.addrspace_node = 0,
.section_node = 0,
.init_node = data.rhs,
.mut_token = tree.nodes.items(.main_token)[node],
Expand All @@ -1163,6 +1184,7 @@ pub fn simpleVarDecl(tree: Tree, node: Node.Index) full.VarDecl {
return tree.fullVarDecl(.{
.type_node = data.lhs,
.align_node = 0,
.addrspace_node = 0,
.section_node = 0,
.init_node = data.rhs,
.mut_token = tree.nodes.items(.main_token)[node],
Expand All @@ -1175,6 +1197,7 @@ pub fn alignedVarDecl(tree: Tree, node: Node.Index) full.VarDecl {
return tree.fullVarDecl(.{
.type_node = 0,
.align_node = data.lhs,
.addrspace_node = 0,
.section_node = 0,
.init_node = data.rhs,
.mut_token = tree.nodes.items(.main_token)[node],
Expand Down Expand Up @@ -1249,6 +1272,7 @@ pub fn fnProtoSimple(tree: Tree, buffer: *[1]Node.Index, node: Node.Index) full.
.return_type = data.rhs,
.params = params,
.align_expr = 0,
.addrspace_expr = 0,
.section_expr = 0,
.callconv_expr = 0,
});
Expand All @@ -1265,6 +1289,7 @@ pub fn fnProtoMulti(tree: Tree, node: Node.Index) full.FnProto {
.return_type = data.rhs,
.params = params,
.align_expr = 0,
.addrspace_expr = 0,
.section_expr = 0,
.callconv_expr = 0,
});
Expand All @@ -1282,6 +1307,7 @@ pub fn fnProtoOne(tree: Tree, buffer: *[1]Node.Index, node: Node.Index) full.FnP
.return_type = data.rhs,
.params = params,
.align_expr = extra.align_expr,
.addrspace_expr = extra.addrspace_expr,
.section_expr = extra.section_expr,
.callconv_expr = extra.callconv_expr,
});
Expand All @@ -1298,6 +1324,7 @@ pub fn fnProto(tree: Tree, node: Node.Index) full.FnProto {
.return_type = data.rhs,
.params = params,
.align_expr = extra.align_expr,
.addrspace_expr = extra.addrspace_expr,
.section_expr = extra.section_expr,
.callconv_expr = extra.callconv_expr,
});
Expand Down Expand Up @@ -1453,6 +1480,7 @@ pub fn ptrTypeAligned(tree: Tree, node: Node.Index) full.PtrType {
return tree.fullPtrType(.{
.main_token = tree.nodes.items(.main_token)[node],
.align_node = data.lhs,
.addrspace_node = 0,
.sentinel = 0,
.bit_range_start = 0,
.bit_range_end = 0,
Expand All @@ -1466,6 +1494,7 @@ pub fn ptrTypeSentinel(tree: Tree, node: Node.Index) full.PtrType {
return tree.fullPtrType(.{
.main_token = tree.nodes.items(.main_token)[node],
.align_node = 0,
.addrspace_node = 0,
.sentinel = data.lhs,
.bit_range_start = 0,
.bit_range_end = 0,
Expand All @@ -1480,6 +1509,7 @@ pub fn ptrType(tree: Tree, node: Node.Index) full.PtrType {
return tree.fullPtrType(.{
.main_token = tree.nodes.items(.main_token)[node],
.align_node = extra.align_node,
.addrspace_node = extra.addrspace_node,
.sentinel = extra.sentinel,
.bit_range_start = 0,
.bit_range_end = 0,
Expand All @@ -1494,6 +1524,7 @@ pub fn ptrTypeBitRange(tree: Tree, node: Node.Index) full.PtrType {
return tree.fullPtrType(.{
.main_token = tree.nodes.items(.main_token)[node],
.align_node = extra.align_node,
.addrspace_node = extra.addrspace_node,
.sentinel = extra.sentinel,
.bit_range_start = extra.bit_range_start,
.bit_range_end = extra.bit_range_end,
Expand Down Expand Up @@ -2063,6 +2094,7 @@ pub const full = struct {
mut_token: TokenIndex,
type_node: Node.Index,
align_node: Node.Index,
addrspace_node: Node.Index,
section_node: Node.Index,
init_node: Node.Index,
};
Expand Down Expand Up @@ -2130,6 +2162,7 @@ pub const full = struct {
return_type: Node.Index,
params: []const Node.Index,
align_expr: Node.Index,
addrspace_expr: Node.Index,
section_expr: Node.Index,
callconv_expr: Node.Index,
};
Expand Down Expand Up @@ -2288,6 +2321,7 @@ pub const full = struct {
pub const Components = struct {
main_token: TokenIndex,
align_node: Node.Index,
addrspace_node: Node.Index,
sentinel: Node.Index,
bit_range_start: Node.Index,
bit_range_end: Node.Index,
Expand Down Expand Up @@ -2397,6 +2431,7 @@ pub const Error = struct {
expected_var_decl_or_fn,
expected_loop_payload,
expected_container,
extra_addrspace_qualifier,
extra_align_qualifier,
extra_allowzero_qualifier,
extra_const_qualifier,
Expand Down Expand Up @@ -2723,13 +2758,13 @@ pub const Node = struct {
/// main_token is the `fn` keyword.
/// extern function declarations use this tag.
fn_proto_multi,
/// `fn(a: b) rhs linksection(e) callconv(f)`. `FnProtoOne[lhs]`.
/// `fn(a: b) rhs addrspace(e) linksection(f) callconv(g)`. `FnProtoOne[lhs]`.
/// zero or one parameters.
/// anytype and ... parameters are omitted from the AST tree.
/// main_token is the `fn` keyword.
/// extern function declarations use this tag.
fn_proto_one,
/// `fn(a: b, c: d) rhs linksection(e) callconv(f)`. `FnProto[lhs]`.
/// `fn(a: b, c: d) rhs addrspace(e) linksection(f) callconv(g)`. `FnProto[lhs]`.
/// anytype and ... parameters are omitted from the AST tree.
/// main_token is the `fn` keyword.
/// extern function declarations use this tag.
Expand Down Expand Up @@ -2893,11 +2928,13 @@ pub const Node = struct {
pub const PtrType = struct {
sentinel: Index,
align_node: Index,
addrspace_node: Index,
};

pub const PtrTypeBitRange = struct {
sentinel: Index,
align_node: Index,
addrspace_node: Index,
bit_range_start: Index,
bit_range_end: Index,
};
Expand All @@ -2920,8 +2957,13 @@ pub const Node = struct {
};

pub const GlobalVarDecl = struct {
/// Populated if there is an explicit type ascription.
type_node: Index,
/// Populated if align(A) is present.
align_node: Index,
/// Populated if linksection(A) is present.
addrspace_node: Index,
/// Populated if addrspace(A) is present.
section_node: Index,
};

Expand Down Expand Up @@ -2954,6 +2996,8 @@ pub const Node = struct {
/// Populated if align(A) is present.
align_expr: Index,
/// Populated if linksection(A) is present.
addrspace_expr: Index,
/// Populated if addrspace(A) is present.
section_expr: Index,
/// Populated if callconv(A) is present.
callconv_expr: Index,
Expand All @@ -2964,6 +3008,8 @@ pub const Node = struct {
params_end: Index,
/// Populated if align(A) is present.
align_expr: Index,
/// Populated if addrspace(A) is present.
addrspace_expr: Index,
/// Populated if linksection(A) is present.
section_expr: Index,
/// Populated if callconv(A) is present.
Expand Down

0 comments on commit ccc7f99

Please sign in to comment.