Commit 8672f2696f

Robin Voetter <robin@voetter.nl>
2021-09-02 14:50:40
Address Spaces: zig fmt + tests
1 parent 538f1bb
Changed files (2)
lib/std/zig/parser_test.zig
@@ -404,6 +404,10 @@ test "zig fmt: trailing comma in fn parameter list" {
         \\pub fn f(
         \\    a: i32,
         \\    b: i32,
+        \\) addrspace(.generic) i32 {}
+        \\pub fn f(
+        \\    a: i32,
+        \\    b: i32,
         \\) linksection(".text") i32 {}
         \\pub fn f(
         \\    a: i32,
@@ -553,8 +557,8 @@ test "zig fmt: sentinel-terminated slice type" {
 test "zig fmt: pointer-to-one with modifiers" {
     try testCanonical(
         \\const x: *u32 = undefined;
-        \\const y: *allowzero align(8) const volatile u32 = undefined;
-        \\const z: *allowzero align(8:4:2) const volatile u32 = undefined;
+        \\const y: *allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+        \\const z: *allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -562,8 +566,8 @@ test "zig fmt: pointer-to-one with modifiers" {
 test "zig fmt: pointer-to-many with modifiers" {
     try testCanonical(
         \\const x: [*]u32 = undefined;
-        \\const y: [*]allowzero align(8) const volatile u32 = undefined;
-        \\const z: [*]allowzero align(8:4:2) const volatile u32 = undefined;
+        \\const y: [*]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+        \\const z: [*]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -571,8 +575,8 @@ test "zig fmt: pointer-to-many with modifiers" {
 test "zig fmt: sentinel pointer with modifiers" {
     try testCanonical(
         \\const x: [*:42]u32 = undefined;
-        \\const y: [*:42]allowzero align(8) const volatile u32 = undefined;
-        \\const y: [*:42]allowzero align(8:4:2) const volatile u32 = undefined;
+        \\const y: [*:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+        \\const y: [*:42]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -580,8 +584,8 @@ test "zig fmt: sentinel pointer with modifiers" {
 test "zig fmt: c pointer with modifiers" {
     try testCanonical(
         \\const x: [*c]u32 = undefined;
-        \\const y: [*c]allowzero align(8) const volatile u32 = undefined;
-        \\const z: [*c]allowzero align(8:4:2) const volatile u32 = undefined;
+        \\const y: [*c]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
+        \\const z: [*c]allowzero align(8:4:2) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -589,7 +593,7 @@ test "zig fmt: c pointer with modifiers" {
 test "zig fmt: slice with modifiers" {
     try testCanonical(
         \\const x: []u32 = undefined;
-        \\const y: []allowzero align(8) const volatile u32 = undefined;
+        \\const y: []allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -597,7 +601,7 @@ test "zig fmt: slice with modifiers" {
 test "zig fmt: sentinel slice with modifiers" {
     try testCanonical(
         \\const x: [:42]u32 = undefined;
-        \\const y: [:42]allowzero align(8) const volatile u32 = undefined;
+        \\const y: [:42]allowzero align(8) addrspace(.generic) const volatile u32 = undefined;
         \\
     );
 }
@@ -1129,6 +1133,16 @@ test "zig fmt: linksection" {
     );
 }
 
+test "zig fmt: addrspace" {
+    try testCanonical(
+        \\export var python_length: u64 align(1) addrspace(.generic);
+        \\export var python_color: Color addrspace(.generic) = .green;
+        \\export var python_legs: u0 align(8) addrspace(.generic) linksection(".python") = 0;
+        \\export fn python_hiss() align(8) addrspace(.generic) linksection(".python") void;
+        \\
+    );
+}
+
 test "zig fmt: correctly space struct fields with doc comments" {
     try testTransform(
         \\pub const S = struct {
lib/std/zig/render.zig
@@ -797,6 +797,14 @@ fn renderPtrType(
         }
     }
 
+    if (ptr_type.ast.addrspace_node != 0) {
+        const addrspace_first = tree.firstToken(ptr_type.ast.addrspace_node);
+        try renderToken(ais, tree, addrspace_first - 2, .none); // addrspace
+        try renderToken(ais, tree, addrspace_first - 1, .none); // lparen
+        try renderExpression(gpa, ais, tree, ptr_type.ast.addrspace_node, .none);
+        try renderToken(ais, tree, tree.lastToken(ptr_type.ast.addrspace_node) + 1, .space); // rparen
+    }
+
     if (ptr_type.const_token) |const_token| {
         try renderToken(ais, tree, const_token, .space);
     }
@@ -921,6 +929,7 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
 
     const name_space = if (var_decl.ast.type_node == 0 and
         (var_decl.ast.align_node != 0 or
+        var_decl.ast.addrspace_node != 0 or
         var_decl.ast.section_node != 0 or
         var_decl.ast.init_node != 0))
         Space.space
@@ -930,8 +939,8 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
 
     if (var_decl.ast.type_node != 0) {
         try renderToken(ais, tree, var_decl.ast.mut_token + 2, Space.space); // :
-        if (var_decl.ast.align_node != 0 or var_decl.ast.section_node != 0 or
-            var_decl.ast.init_node != 0)
+        if (var_decl.ast.align_node != 0 or var_decl.ast.addrspace_node != 0 or
+            var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0)
         {
             try renderExpression(gpa, ais, tree, var_decl.ast.type_node, .space);
         } else {
@@ -948,6 +957,22 @@ fn renderVarDecl(gpa: *Allocator, ais: *Ais, tree: Ast, var_decl: Ast.full.VarDe
         try renderToken(ais, tree, align_kw, Space.none); // align
         try renderToken(ais, tree, lparen, Space.none); // (
         try renderExpression(gpa, ais, tree, var_decl.ast.align_node, Space.none);
+        if (var_decl.ast.addrspace_node != 0 or var_decl.ast.section_node != 0 or
+            var_decl.ast.init_node != 0) {
+            try renderToken(ais, tree, rparen, .space); // )
+        } else {
+            try renderToken(ais, tree, rparen, .none); // )
+            return renderToken(ais, tree, rparen + 1, Space.newline); // ;
+        }
+    }
+
+    if (var_decl.ast.addrspace_node != 0) {
+        const lparen = tree.firstToken(var_decl.ast.addrspace_node) - 1;
+        const addrspace_kw = lparen - 1;
+        const rparen = tree.lastToken(var_decl.ast.addrspace_node) + 1;
+        try renderToken(ais, tree, addrspace_kw, Space.none); // addrspace
+        try renderToken(ais, tree, lparen, Space.none); // (
+        try renderExpression(gpa, ais, tree, var_decl.ast.addrspace_node, Space.none);
         if (var_decl.ast.section_node != 0 or var_decl.ast.init_node != 0) {
             try renderToken(ais, tree, rparen, .space); // )
         } else {
@@ -1267,6 +1292,14 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
                 smallest_start = start;
             }
         }
+        if (fn_proto.ast.addrspace_expr != 0) {
+            const tok = tree.firstToken(fn_proto.ast.addrspace_expr) - 3;
+            const start = token_starts[tok];
+            if (start < smallest_start) {
+                rparen = tok;
+                smallest_start = start;
+            }
+        }
         if (fn_proto.ast.section_expr != 0) {
             const tok = tree.firstToken(fn_proto.ast.section_expr) - 3;
             const start = token_starts[tok];
@@ -1407,6 +1440,16 @@ fn renderFnProto(gpa: *Allocator, ais: *Ais, tree: Ast, fn_proto: Ast.full.FnPro
         try renderToken(ais, tree, align_rparen, .space); // )
     }
 
+    if (fn_proto.ast.addrspace_expr != 0) {
+        const align_lparen = tree.firstToken(fn_proto.ast.addrspace_expr) - 1;
+        const align_rparen = tree.lastToken(fn_proto.ast.addrspace_expr) + 1;
+
+        try renderToken(ais, tree, align_lparen - 1, .none); // addrspace
+        try renderToken(ais, tree, align_lparen, .none); // (
+        try renderExpression(gpa, ais, tree, fn_proto.ast.addrspace_expr, .none);
+        try renderToken(ais, tree, align_rparen, .space); // )
+    }
+
     if (fn_proto.ast.section_expr != 0) {
         const section_lparen = tree.firstToken(fn_proto.ast.section_expr) - 1;
         const section_rparen = tree.lastToken(fn_proto.ast.section_expr) + 1;