aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
AgeCommit message (Collapse)Author
2021-07-07stage2: implement `@panic` and beginnigs of inferred error setsAndrew Kelley
* ZIR: add two instructions: - ret_err_value_code - ret_err_value * AstGen: add countDefers and utilize it to emit more efficient ZIR for return expressions in the presence of defers. * AstGen: implement |err| payloads for `errdefer` syntax. - There is not an "unused capture" error for it yet. * AstGen: `return error.Foo` syntax gets a hot path in return expressions, using the new ZIR instructions. This also is part of implementing inferred error sets, since we need to tell Sema to add an error value to the inferred error set before it gets coerced. * Sema: implement `@setCold`. - Implement `@setCold` support for C backend. * `@panic` and regular safety panics such as `unreachable` now properly invoke `std.builtin.panic`. * C backend: improve pointer and function value rendering. * C linker: fix redundant typedefs. * Add Type.error_set_inferred. * Fix Value.format for enum_literal, enum_field_index, bytes. * Remove the C backend test that checks for identical text I measured a 14% reduction in Total ZIR Bytes from master branch for std/os.zig.
2021-07-02stage2: improve AstGen FileNotFound error messageAndrew Kelley
Partially addresses #9203. It fixes the first case, but not the second one mentioned in the issue.
2021-07-02AstGen: cleanups to pass more compile error test casesAndrew Kelley
2021-07-02AstGen: detect redeclaration of function parametersAndrew Kelley
Also improve redeclaration error message to include the category of variable.
2021-07-02AstGen: pass more compile error testsAndrew Kelley
* Implement "initializing array with struct syntax" * Implement "'_' used as an identifier without @\"_\" syntax" * Fix source location of "missing parameter name" * Update test cases where appropriate
2021-07-02stage2: improve compile errors from tokenizerAndrew Kelley
In order to not regress the quality of compile errors, some improvements had to be made. * std.zig.parseCharLiteral is improved to return more detailed parse failure information. * tokenizer is improved to handle null bytes in the middle of strings, character literals, and line comments. * validating how many unicode escape digits in string literals is moved to std.zig.parseStringLiteral rather than handled in the tokenizer. * when a tokenizer error occurs, if the reported token is the 'invalid' tag, an error note is added to point to the invalid byte location. Further improvements would be: - Mention the expected set of allowed bytes at this location. - Display the invalid byte (if printable, print it, otherwise escape-print it).
2021-07-02avoid calling into stage1 backend when AstGen failsAndrew Kelley
The motivation for this commit is that there exists source files which produce ast-check errors, but crash stage1 or otherwise trigger stage1 bugs. Previously to this commit, Zig would run AstGen, collect the compile errors, run stage1, report stage1 compile errors and exit if any, and then report AstGen compile errors. The main change in this commit is to report AstGen errors prior to invoking stage1, and in fact if any AstGen errors occur, do not invoke stage1 at all. This caused most of the compile error tests to fail due to things such as unused local variables and mismatched stage1/stage2 error messages. It was taking a long time to update the test cases one-by-one, so I took this opportunity to unify the stage1 and stage2 testing harness, specifically with regards to compile errors. In this way we can start keeping track of which tests pass for 1, 2, or both. `zig build test-compile-errors` no longer works; it is now integrated into `zig build test-stage2`. This is one step closer to executing compile error tests in parallel; in fact the ThreadPool object is already in scope. There are some cases where the stage1 compile errors were actually better; those are left failing in this commit, to be addressed in a follow-up commit. Other changes in this commit: * build.zig: improve support for -Dstage1 used with the test step. * AstGen: minor cosmetic changes to error messages. * stage2: add -fstage1 and -fno-stage1 flags. This now allows one to download a binary of the zig compiler and use the llvm backend of self-hosted. This was also needed for hooking up the test harness. However, I realized that stage1 calls exit() and also has memory leaks, so had to complicate the test harness by not using this flag after all and instead invoking as a child process. - These CLI flags will disappear once we start shipping the self-hosted compiler as the main compiler. Until then, they can be used to try out the work-in-progress stage2. * stage2: select the LLVM backend by default for release modes, as long as the target architecture is supported by LLVM. * test harness: support setting the optimize mode
2021-07-02AstGen: fix missing compile error for unreachable `@TypeOf` argumentAndrew Kelley
2021-07-02AstGen: introduce 'reachableExpr' functionAndrew Kelley
This function can be swapped out for calls to expr() to report a compile error when the expression results in control flow that does not return.
2021-07-02AstGen: implement compile error for useless localsAndrew Kelley
When a local variable has an initialization expression of type 'noreturn', emit a compile error. This brings this branch closer to parity with master branch.
2021-07-02move "unreachable code" error from stage1 to stage2Andrew Kelley
* AstGen: implement "unreachable code" error for blocks. This works at the statement level. * stage1: remove the "unreachable code" error implementation, which means removing the `is_gen` field from IrInstSrc. This is one small step towards a smaller memory footprint for stage1. The benefits won't be realized until a future commit because this flag took advantage of padding. There may be a regression here with "union has no associated enum" error, and there is a regression with the following code: ```zig const a = noreturn; ``` A future commit will address these regressions.
2021-06-27stage2: fix unreachable in union(enum) with anytype payloadg-w1
2021-06-25AstGen: fix O(N^2) perf for many decls with same parentAndrew Kelley
AstGen was calling findLineColumn() for every sibling Decl, using the parent Decl as the starting point for the search for newlines. This resulted in poor performance for large numbers of Decls with the same parent. The solution is simple: since AstGen progresses monotonically through the AST, keep a single cursor into the source file, and whenever line/column information is needed, advance the cursor. This guarantees O(N) on the number of bytes in the file. Perf: As an example I ran ast-check on zigwin32/win32/everything.zig (a 17 MiB file) in master branch, and after this commit. With master branch, I killed the process after 17 seconds out of boredom. With this commit, it completed in 300 milliseconds. Closes #9234
2021-06-23astgen: error on struct field with no typeJacob G-W
2021-06-21AstGen: remove unused parametersAndrew Kelley
2021-06-21AstGen: remove unused scope parameter from rvalueAndrew Kelley
2021-06-21AstGen: remove unused parameterAndrew Kelley
2021-06-21remove unused parametersAndrew Kelley
2021-06-21AstGen: while loop continue expr captures in scopeAndrew Kelley
Before this, the continue expression of a while loop did not have the capture variable in it, making it incorrectly emit a compile error for not using the capture, even if it was referenced.
2021-06-21AstGen: convert a TODO comment to an issueAndrew Kelley
See #363
2021-06-21fix code broken from previous commitJacob G-W
2021-06-21stage2 AstGen: fix lots of bugs and catch more errorsJacob G-W
Gotta catch 'em all! also simplify identifier( logic
2021-06-21std, src, doc, test: remove unused variablesJacob G-W
2021-06-21stage2: fix TODO in @export to look for runtime-varsJacob G-W
Also rename LocalPtr.is_comptime to LocalPtr.maybe_comptime as it is a better name, as it could be runtime, but is not always runtime.
2021-06-21stage2 astgen: make asm outputs count as referencing varsJacob G-W
This is temporary and putting this as a seperate commit so that it can easily be reverted as andrewrk suggested.
2021-06-21stage2: make loop vars be comptime if they are inlineJacob G-W
thanks @Vexu
2021-06-21stage2 astgen: fix bug in struct init where type not refedJacob G-W
2021-06-21stage2 astgen: find unused varsJacob G-W
2021-06-20stage2: Remove special double ampersand parsing case (#9114)Dmitry Matveyev
* Remove parser error on double ampersand * Add failing test for double ampersand case * Add error when encountering double ampersand in AstGen "Bit and" operator should not make sense when one of its operands is an address. * Check that 2 ampersands are adjacent to each other in source string * Remove cases of unused variables in tests
2021-06-17AstGen: properly generate errdefer expressions when returningAndrew Kelley
`return` statements use a new function `nodeMayEvalToError` which does some basic checks on the AST node to return never, always, or maybe. Depending on this result, AstGen skips the errdefers, always includes the errdefers, or emits a conditional branch to check whether the return value is an error that Sema will have to evaluate. Closes #8821 Unblocks #9047
2021-06-15AstGen: support `@export` with field accessAndrew Kelley
The Zig language specification will support identifiers and field access in order to refer to which declaration to export with `@export`. This commit implements the change in AstGen and updates the language reference.
2021-06-14add ast-check flag to zig fmt, fix found bugsVeikka Tuominen
2021-06-12Renamed @byteOffsetOf to @offsetOfExonorid
2021-06-08stage2: compile error for ambiguous decl refrencesjacob gw
std: fix compile errors from this change. This is a stage1 bug.
2021-06-07stage2: implement comptime variablesVeikka Tuominen
2021-06-03Breaking hash map changes for 0.8.0Martin Wickham
- hash/eql functions moved into a Context object - *Context functions pass an explicit context - *Adapted functions pass specialized keys and contexts - new getPtr() function returns a pointer to value - remove functions renamed to fetchRemove - new remove functions return bool - removeAssertDiscard deleted, use assert(remove(...)) instead - Keys and values are stored in separate arrays - Entry is now {*K, *V}, the new KV is {K, V} - BufSet/BufMap functions renamed to match other set/map types - fixed iterating-while-modifying bug in src/link/C.zig
2021-05-28AstGen: properly restore previous state after temporary changesAndrew Kelley
Before this, if a compile error occurred, it would cause the previous value for e.g. the function scope to not get reset. If the AstGen process continued, it would result in a violation of the data guarantees that it relies on. This commit takes advantage of defer to ensure the previous value is always reset, even in the case of an error. Closes #8920
2021-05-22Merge pull request #8844 from ifreund/inlineAndrew Kelley
Support inline keyword as well as callconv(.Inline)
2021-05-22stage2: astgen error for return or try in defer blockjacob gw
2021-05-20stage2: support inline keyword on function declsIsaac Freund
This is an alternative to callconv(.Inline). Using an inline keyword as well as an explicit callconv() is a compile error.
2021-05-13AstGen: add compile error for decl name conflictsAndrew Kelley
* Remove the ability for GenZir parent Scope to be null. Now there is a Top Scope at the top. * Introduce Scope.Namespace to contain a table of decl names in order to emit a compile error for name conflicts. * Fix use of invalid memory when reporting compile errors by duplicating decl names into a temporary heap allocated buffer. * Fix memory leak in while and for loops, not cleaning up their labeled_breaks and store_to_block_ptr_list arrays. * Fix stage2 test cases because now the source location of redundant comptime keyword compile errors is improved. * Implement compile error for local variable shadowing declaration.
2021-05-13AstGen: fix elision of store_to_block_ptr for condbrAndrew Kelley
2021-05-11AstGen: support emitting multiple compile errorsAndrew Kelley
2021-05-10stage2: type declarations ZIR encode AnonNameStrategyAndrew Kelley
which can be either parent, func, or anon. Here's the enum reproduced in the commit message for convenience: ```zig pub const NameStrategy = enum(u2) { /// Use the same name as the parent declaration name. /// e.g. `const Foo = struct {...};`. parent, /// Use the name of the currently executing comptime function call, /// with the current parameters. e.g. `ArrayList(i32)`. func, /// Create an anonymous name for this declaration. /// Like this: "ParentDeclName_struct_69" anon, }; ``` With this information in the ZIR, a future commit can improve the names of structs, unions, enums, and opaques. In order to accomplish this, the following ZIR instruction forms were removed and replaced with Extended op codes: * struct_decl * struct_decl_packed * struct_decl_extern * union_decl * union_decl_packed * union_decl_extern * enum_decl * enum_decl_nonexhaustive By being extended opcodes, one more u32 is needed, however we more than make up for it by repurposing the 16 "small" bits to provide shorter encodings for when decls_len == 0, fields_len == 0, a source node is not provided, etc. There tends to be no downside, and in fact sometimes upsides, to using an extended op code when there is a need for flag bits, which is the case for all three of these. Likewise, the container layout can be encoded in these bits rather than into the opcode. The following 4 ZIR instructions were added, netting a total of 4 freed up ZIR enum tags for future use: * opaque_decl_anon * opaque_decl_func * error_set_decl_anon * error_set_decl_func This is so that opaques and error sets can have the same name hint as structs, enums, and unions. `std.builtin.ContainerLayout` gets an explicit integer tag type so that it can be used inside packed structs. This commit also makes `Module.Namespace` use a separate set for anonymous decls, thus allowing anonymous decls to share the same `Decl.name` as their owner `Decl` objects.
2021-05-08AstGen: fix incorrect logic for adding implicit return instructionAndrew Kelley
2021-05-07stage2: implement global variablesAndrew Kelley
* Sema: implement global variables - Improved global constants to stop needlessly creating a Var structure; they can just store the value directly. - This required making memory management a bit more sophisticated to detect when a Decl owns the Namespace associated with it, for the purposes of deinitialization. * Decl.name and Namespace decl table keys no longer directly reference ZIR; instead they have heap-duped names, so that deleted decls, which no longer have any ZIR to reference for their names, can be removed from the parent Namespace table. - In the future I would like to explore going a different direction with this, where the strings would still point to the ZIR however they would be removed from their owner Namespace objects during the update detection. The design principle here is that the existence of incremental compilation as a feature should not incur any cost for the use case when it is not used. In this example Decl names could simply point to ZIR string table memory, and it is only because of incremental compilation that we duplicate their names. * AstGen: implement threadlocal variables * CLI: call cleanExit after building a compilation so that in release modes we don't bother freeing memory or closing file descriptors, allowing the OS to do it more efficiently. * Avoid calling `freeDecl` in the linker for unreferenced Decl objects. * Fix CBE test case expecting the compile error to point to the wrong column.
2021-05-07stage2: implement extern functionsAndrew Kelley
2021-05-02AstGen: fix outdated doc commentAndrew Kelley
2021-05-02AstGen: decouple from Module/CompilationAndrew Kelley
AstGen is now completely independent from the rest of the compiler. It ingests an AST tree and produces ZIR code as the output, without depending on any of the glue code of the compiler.
2021-05-02stage2: test decls encode that they are tests in ZIRAndrew Kelley
This allows Sema to namespace them separately from function decls with the same name. Ran into this in std.math.order conflicting with a test with the same name.