319 Commits

Author SHA1 Message Date
Techatrix
ca6fb30e99
std.zig.Ast: improve type safety
This commits adds the following distinct integer types to std.zig.Ast:
- OptionalTokenIndex
- TokenOffset
- OptionalTokenOffset
- Node.OptionalIndex
- Node.Offset
- Node.OptionalOffset

The `Node.Index` type has also been converted to a distinct type while
`TokenIndex` remains unchanged.

`Ast.Node.Data` has also been changed to a (untagged) union to provide
safety checks.
2025-03-07 22:22:01 +01:00
Techatrix
6dcd8f4f75
std.zig.Ast: add blockStatements and builtinCallParams 2025-03-07 22:20:35 +01:00
Andrew Kelley
eb3c7f5706 zig build fmt 2025-02-22 17:09:20 -08:00
Andrew Kelley
ed55b2ef17 update to new pop API 2025-02-22 17:09:20 -08:00
87flowers
87172ee4e6 std/zig/render: Add doc comments to AutoIndentingStream 2025-02-22 17:09:20 -08:00
87flowers
c724e157d6 std/zig/render: implement fixes for unit tests 2025-02-22 17:09:20 -08:00
87flowers
a702dc31cc std/zig/render: assigns are .after_equals 2025-02-22 17:09:20 -08:00
87flowers
6013b53702 std/zig/render: Collapse one level of indentation in binop after equals sign 2025-02-22 17:09:20 -08:00
87flowers
9b2677c639 std/zig/render: Implement space mode to fix comment indentation 2025-02-22 17:09:20 -08:00
87flowers
ec3e4cc14b std/zig/render: Initial implementation of indentation 2025-02-22 17:09:20 -08:00
87flowers
1b0584fd02 std/zig/render: Implement AutoIndentingStream.init 2025-02-22 17:09:19 -08:00
mlugg
d00e05f186
all: update to std.builtin.Type.Pointer.Size field renames
This was done by regex substitution with `sed`. I then manually went
over the entire diff and fixed any incorrect changes.

This diff also changes a lot of `callconv(.C)` to `callconv(.c)`, since
my regex happened to also trigger here. I opted to leave these changes
in, since they *are* a correct migration, even if they're not the one I
was trying to do!
2025-01-16 12:46:29 +00:00
mlugg
cbfe00b17d
std.zig.render: fix callconv(.Inline) -> inline fn promotion 2024-10-19 19:15:23 +01:00
Linus Groh
8588964972 Replace deprecated default initializations with decl literals 2024-09-12 16:01:23 +01:00
Ian Johnson
9007534551 std.zig.tokenizer: simplify line-based tokens
Closes #21358
Closes #21360

This commit modifies the `multiline_string_literal_line`, `doc_comment`,
and `container_doc_comment` tokens to no longer include the line ending
as part of the token. This makes it easier to handle line endings (which
may be LF, CRLF, or in edge cases possibly nonexistent) consistently.

In the two issues linked above, Autodoc was already assuming this for
doc comments, and yielding incorrect results when handling files with
CRLF line endings (both in Markdown parsing and source rendering).

Applying the same simplification for multiline string literals also
brings `zig fmt` into conformance with
https://github.com/ziglang/zig-spec/issues/38 regarding formatting of
multiline strings with CRLF line endings: the spec says that `zig fmt`
should remove the CR from such line endings, but this was not previously
the case.
2024-09-10 13:34:33 +03:00
Des-Nerger
6b1f509422 std.zig.render: replace magic number 4 with indent_delta. 2024-09-10 01:27:49 +03:00
mlugg
3b52e5a221
std.zig.render: fix switch rendering 2024-09-01 18:30:31 +01:00
Will Lillis
7e76818132
fix(fmt): pointer type syntax to index (take 2) (#20336)
* Change main token for many-item and c style pointers from asterisk to l brace, update main token in c translation
2024-07-21 01:55:52 -07:00
Travis Staloch
8af59d1f98 ComptimeStringMap: return a regular struct and optimize
this patch renames ComptimeStringMap to StaticStringMap, makes it
accept only a single type parameter, and return a known struct type
instead of an anonymous struct.  initial motivation for these changes
was to reduce the 'very long type names' issue described here
https://github.com/ziglang/zig/pull/19682.

this breaks the previous API.  users will now need to write:
`const map = std.StaticStringMap(T).initComptime(kvs_list);`

* move `kvs_list` param from type param to an `initComptime()` param
* new public methods
  * `keys()`, `values()` helpers
  * `init(allocator)`, `deinit(allocator)` for runtime data
  * `getLongestPrefix(str)`, `getLongestPrefixIndex(str)` - i'm not sure
     these belong but have left in for now incase they are deemed useful
* performance notes:
  * i posted some benchmarking results here:
    https://github.com/travisstaloch/comptime-string-map-revised/issues/1
  * i noticed a speedup reducing the size of the struct from 48 to 32
    bytes and thus use u32s instead of usize for all length fields
  * i noticed speedup storing KVs as a struct of arrays
  * latest benchmark shows these wall_time improvements for
    debug/safe/small/fast builds: -6.6% / -10.2% / -19.1% / -8.9%. full
    output in link above.
2024-04-22 15:31:41 -07:00
Carl Ã…stholm
4c393c7468 Update usages of fmtId/isValidId
`{}` for decls
`{p}` for enum fields
`{p_}` for struct fields and in contexts following a `.`

Elsewhere, `{p}` was used since it's equivalent to the old behavior.
2024-04-07 14:47:11 +02:00
Jacob Young
c11b6adf13 Ast: fix comptime destructure
A preceding `comptime` keyword was being ignored if the first
destructure variable was an expression.
2024-03-17 15:23:16 -07:00
Carl Ã…stholm
3cafb9655a zig fmt: Preserve trailing comma after single-item switch case 2024-02-18 12:36:04 +02:00
Andrew Kelley
195eeed2d8 std: remove deprecated API 2024-01-16 04:20:05 -08:00
yunsh1
7e4177a4b1 fmt: Skip extra newline if doc_comment exists 2023-12-08 16:05:46 +02:00
Andrew Kelley
70d51be3a2 std.zig.render: add ability to append strings after nodes 2023-11-26 19:41:00 -07:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Adam Goertz
91570cc42d zig-reduce: Reduce if expressions
Perform these transformations in this priority order:
1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already.
2. If the `then` block is empty, replace the condition with `if (false)` if it is not already.
3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression.
4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression.
2023-11-06 04:31:55 +00:00
Andrew Kelley
c4dddcbadb std.zig.render: ability to omit variable declarations 2023-11-04 20:26:39 -07:00
Andrew Kelley
88acdb9aa6 zig reduce: delete statements from blocks 2023-11-04 18:06:11 -07:00
Andrew Kelley
31ad3af956 std.zig.render: support fixing unused parameter 2023-11-04 17:37:34 -07:00
Andrew Kelley
fc1e7a5644 zig reduce: rename identifiers when inlining an @import 2023-11-04 15:48:28 -07:00
Andrew Kelley
8bd01d2d9b zig reduce: add transformation for inlining file-based @import
One thing is missing for it to be useful, however, which is dealing with
ambiguous reference errors introduced by the inlining process.
2023-11-04 13:57:29 -07:00
Andrew Kelley
31529f912b zig reduce: add transformation of replacing var init with undefined 2023-11-03 22:08:01 -07:00
Andrew Kelley
a2f4adbd19 zig reduce: add "delete unused globals" transform
While walking the AST looking for reductions, notice if any globals are
unreferenced, and if they are, add a transformation for removing the
global.
2023-11-03 21:31:32 -07:00
Andrew Kelley
1396479656 zig reduce: redesign
Now it works like this:
1. Walk the AST of the source file looking for independent
   reductions and collecting them all into an array list.
2. Randomize the list of transformations. A future enhancement will add
   priority weights to the sorting but for now they are completely
   shuffled.
3. Apply a subset consisting of 1/2 of the transformations and check for
   interestingness.
4. If not interesting, half the subset size again and check again.
5. Repeat until the subset size is 1, then march the transformation
   index forward by 1 with each non-interesting attempt.

At any point if a subset of transformations succeeds in producing an interesting
result, restart the whole process, reparsing the AST and re-generating the list
of all possible transformations and shuffling it again.

As for std.zig.render, the fixups operate based on AST Node Index rather
than Nth index of the function occurence. This allows precise control
over how to mutate the input.
2023-11-03 20:05:32 -07:00
Andrew Kelley
b1aaf42134 std.zig.render: fix iteration over parameters
use the handy iteration API to be correct
2023-11-03 20:05:32 -07:00
Andrew Kelley
5f1f145199 add a transformation for gutting the body of a function 2023-11-03 20:05:32 -07:00
Andrew Kelley
3263d6e6ab std.zig: move render state to struct; add fixups 2023-11-03 20:05:32 -07:00
Tobias Simetsreiter
7a9500fd80
Fix rendering ast in zon mode (#17547)
Co-authored-by: Tobias Simetsreiter <tobias.simetsreiter@wabtec.com>
2023-10-17 19:04:01 -04:00
Andrew Kelley
027aabf497 drop for loop syntax upgrade mechanisms 2023-10-13 03:43:54 -07:00
Veikka Tuominen
63bd2bff12 Sema: add @errorCast which works for both error sets and error unions
Closes #17343
2023-10-01 17:00:01 +03:00
antlilja
8191199951 fmt: add rewrite from @fabs to @abs 2023-09-27 11:24:45 -07:00
mlugg
88f5315ddf compiler: implement destructuring syntax
This change implements the following syntax into the compiler:

```zig
const x: u32, var y, foo.bar = .{ 1, 2, 3 };
```

A destructure expression may only appear within a block (i.e. not at
comtainer scope). The LHS consists of a sequence of comma-separated var
decls and/or lvalue expressions. The RHS is a normal expression.

A new result location type, `destructure`, is used, which contains
result pointers for each component of the destructure. This means that
when the RHS is a more complicated expression, peer type resolution is
not used: each result value is individually destructured and written to
the result pointers. RLS is always used for destructure expressions,
meaning every `const` on the LHS of such an expression creates a true
stack allocation.

Aside from anonymous array literals, Sema is capable of destructuring
the following types:
* Tuples
* Arrays
* Vectors

A destructure may be prefixed with the `comptime` keyword, in which case
the entire destructure is evaluated at comptime: this means all `var`s
in the LHS are `comptime var`s, every lvalue expression is evaluated at
comptime, and the RHS is evaluated at comptime. If every LHS is a
`const`, this is not allowed: as with single declarations, the user
should instead mark the RHS as `comptime`.

There are a few subtleties in the grammar changes here. For one thing,
if every LHS is an lvalue expression (rather than a var decl), a
destructure is considered an expression. This makes, for instance,
`if (cond) x, y = .{ 1, 2 };` valid Zig code. A destructure is allowed
in almost every context where a standard assignment expression is
permitted. The exception is `switch` prongs, which cannot be
destructures as the comma is ambiguous with the end of the prong.

A follow-up commit will begin utilizing this syntax in the Zig compiler.

Resolves: #498
2023-09-15 11:33:53 -07:00
Andrew Kelley
23a806102a Revert "zig fmt: additionally auto-fix @byteSwap (#16462)"
This reverts commit f31e114f633f5d57f6d09a616f5997d83949f641.

This commit was incorrect; `@byteSwap` was not modified during the
0.11.0 release cycle.
2023-07-23 20:30:34 -07:00
iacore
f31e114f63
zig fmt: additionally auto-fix @byteSwap (#16462) 2023-07-22 01:42:39 -04:00
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
mlugg
283d650973 fmt: add rewrite for cast builtin type parameters 2023-06-24 16:47:49 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Eric Joldasov
a6c8ee5231 compiler: rename "@XToY" to "@YFromX", zig fmt: rewrite them
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:24 -07:00
r00ster91
2593156068 migration: std.math.{min, min3, max, max3} -> @min & @max 2023-06-16 13:44:09 -07:00