aboutsummaryrefslogtreecommitdiff
path: root/src/Module.zig
AgeCommit message (Collapse)Author
2021-05-12stage2: fix source location of Decl compile errorsAndrew Kelley
In `Module.semaDecl`, the source location being used was double-relative to the `Decl`, causing a crash when trying to compute byte offset for the compile error. Change the source location to node_offset = 0 since the scope being used makes the source location relative to the Decl, which already has the source node index populated.
2021-05-12stage2: fix handling of "prev successful ZIR"Andrew Kelley
Before this change, the attempt to save the most recent successful ZIR code only worked in some cases; this reworks the code to be more robust, thereby fixing a crash when running the stage2 "enums" CBE test cases.
2021-05-12Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgenAndrew Kelley
Conflicts: * lib/std/os/linux.zig * lib/std/os/windows/bits.zig * src/Module.zig * src/Sema.zig * test/stage2/test.zig Mainly I wanted Jakub's new macOS code for respecting stack size, since we now depend on it for debug builds able to pass one of the test cases for recursive comptime function calls with `@setEvalBranchQuota`. The conflicts were all trivial.
2021-05-11stage2: more Decl lifetime fixesAndrew Kelley
* File stores `root_decl: Decl` instead of `namespace: *Namespace`. This maps more cleanly to the actual ownership, since the `File` does own the root decl, but it does not directly own the `Namespace`. * `semaFile` completes the creation of the `Decl` even when semantic analysis fails. The `analysis` field of the `Decl` will contain the results of semantic analysis. This prevents cleaning up of memory still referenced by other Decl objects. * `semaDecl` sets `Struct.zir_index` of the root struct decl, which fixes use of undefined value in case the first update contained a ZIR compile error.
2021-05-11stage2: improve Decl lifetime managementAndrew Kelley
* Compilation: iteration over the deletion_set only tries to delete the first one, relying on Decl destroy to remove itself from the deletion set. * link: `freeDecl` now has to handle the possibility of freeing a Decl that was never called with `allocateDeclIndexes`. * `deleteDecl` recursively iterates over a Decl's Namespace sub-Decl objects and calls `deleteDecl` on them. - Prevents Decl objects from being destroyed when they are still in `deletion_set`. * Sema: fix cleanup of anonymous Decl objects when an error occurs during semantic analysis. * tests: update test cases for fully qualified names
2021-05-11stage2: lookupIdentifier can return error.AnalysisFailedAndrew Kelley
This avoids causing false positive compile errors when, for example, a file had ZIR errors, and then code tried to look up a public decl from the failed file.
2021-05-11stage2: better handling of file-level compile errors across updatesAndrew Kelley
* `Module.File` now retains the most recent successful ZIR in the event that an update causes a ZIR compile error. This way Zig does not throw out useful semantic analysis results when an update temporarily introduces a ZIR compile error. * Semantic analysis of a File now unconditionally creates a Decl object for the File. The Decl object is marked as `file_failed` in case of File-level compile errors. This allows detecting of the File being outdated, and dependency tracking just like any other Decl.
2021-05-11stage2: add `owns_tv` flag to `Module.Decl`Andrew Kelley
Decl objects need to know whether they are the owner of the Type/Value associated with them, in order to decide whether to destroy the associated Namespace, Fn, or Var when cleaning up.
2021-05-10stage2: struct, union, enum, opaque, error sets get better namesAndrew Kelley
This commit takes advantage of the new "NameStrategy" that is exposed in the ZIR in order to name Decls after their parent when asked to. This makes the next failing test case pass.
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-08stage2: fully qualified names and better anonymous namesAndrew Kelley
* no more global atomic variable for anonymous decl indexes. Instead the Namespace decl table is used to figure out anonymous decl names. - This prepares for better multi-threaded semantic analysis in the future, with no contention on this global atomic integer. * implement fully qualified names for namespaces.
2021-05-07stage2: fix struct inits not getting fields resolvedAndrew Kelley
2021-05-07stage2: fix stack overflow in `@setEvalBranchQuota` test caseAndrew Kelley
Some of the reworkings in this branch put us over the limit, on Linux, where the kernel disregards the fact that we ask for 16 MiB in the ELF file. So we ask for more stack space in `main`.
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: no '$' in anonymous decl namesAndrew Kelley
This way it will not cause a compile error for the C backend.
2021-05-07stage2: implement extern functionsAndrew Kelley
2021-05-07Sema: support enough to check main calling convention via `@typeInfo`Andrew Kelley
After this commit, `pub export fn main() c_int { ... }` will be correctly detected as the intended entry point, and therefore start code will not try to export its own conflicting `main` function. * Implement basic union support - lots of stuff is still TODO, including runtime field access - also TODO: resolving the union tag type - comptime field access is implemented * DRY up some code by using the `Zir.DeclIterator` for skipping over decls in structs and unions. * Start to clean up Sema with regards to calling `.value()` to find out a const value. Instead, Sema code should call one of these two: - `resolvePossiblyUndefinedValue` (followed by logic dealing with undefined values) - `resolveDefinedValue` (a compile error will be emitted if the value is undefined) * An exported function with an unspecified calling convention gets the C calling convention. * Implement comptime field access for structs. * Add another implementation of "type has one possible value" in Sema. This is a bit unfortunate since the logic is duplicated, but the one in Type asserts that the types are resolved already, and is appropriate to call from codegen, while the one in Sema performs type resolution if necessary, reporting any compile errors that occur in the process.
2021-05-06stage2: wire up outdated/deleted decl detectionAndrew Kelley
we're back to incremental compilation working smoothly
2021-05-05stage2: fix contents hash computationAndrew Kelley
during an incremental update change detection, the function call to get the old contents hash took place after mangling the old ZIR index, making it access the wrong array index.
2021-05-05stage2: mapping old to new ZIR recursivelyAndrew Kelley
now it walks into functions and blocks to find decls.
2021-05-05stage2: add `zig changelist` debug commandAndrew Kelley
and implement the first pass at mechanism to map old ZIR to new ZIR.
2021-05-04Sema: restore the extern lib name functionalityAndrew Kelley
2021-05-04Sema: implement error setsAndrew Kelley
2021-05-04stage2: fix "other symbol here" error noteAndrew Kelley
It wasn't setting the source location properly.
2021-05-04stage2: fix structs and enums setting wrong owner_declAndrew Kelley
No more memory leaks.
2021-05-03Sema: implement ExportOptions support in `@export`Andrew Kelley
Also fix switch blocks not emitting their AIR code.
2021-05-03Sema: implement struct_decl instructionAndrew Kelley
2021-05-03stage2: hook up semantic analysis of struct fieldsAndrew Kelley
Now that they are lazy, they need to get analyzed in the correct context, when requested. This commit also hooks up std.builtin type values being resolved properly. This is needed, for example, with the `@export` builtin function, which occurs in start.zig, for `std.builtin.ExportOptions`. The ZIR code uses the special `Ref.export_options` value, and semantic analysis has to map this to the corresponding type from `std.builtin`.
2021-05-02stage2: make struct field analysis lazyAndrew Kelley
This commit breaks struct field analysis; will be fixed in a future commit.
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: fix error reporting not loading ASTAndrew Kelley
In the byteOffset function, compile errors may need to compute the AST from source bytes in order to resolve source locations. Previously there were a few lines trying to access the AST before it was loaded. Trivial fix, just move load the tree at the beginning.
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.
2021-05-01stage2: dbg_stmt ZIR instructions have line/colAndrew Kelley
instead of node indexes. * AstGen: dbg_stmt instructions now have line and column indexes, relative to the parent declaration. This allows codegen to emit debug info without having the source bytes, tokens, or AST nodes loaded in memory. * ZIR: each decl has the absolute line number. This allows computing line numbers from offsets without consulting source code bytes. Memory management: creating a function definition does not prematurely set the Decl arena. Instead the function is allocated with the general purpose allocator. Codegen no longer looks at source code bytes for any reason. They can remain unloaded from disk.
2021-04-30stage2: implement function body analysisAndrew Kelley
now with whole-file-astgen
2021-04-30stage2: introduce new ZIR instruction: argAndrew Kelley
* AstGen: LocalVal and LocalPtr use string table indexes for their names. This is more efficient because local variable declarations do need to include the variable names so that semantic analysis can emit a compile error if a declaration is shadowed. So we take advantage of this fact by comparing string table indexes when resolving names. * The arg ZIR instructions are needed for the above reasoning, as well as to emit equivalent AIR instructions for debug info. Now that we have these arg instructions, get rid of the special `Zir.Inst.Ref` range for parameters. ZIR instructions now refer to the arg instructions for parameters. * Move identAsString and strLitAsString from Module.GenZir to AstGen where they belong.
2021-04-30Sema: implement function declarationsAndrew Kelley
2021-04-30stage2: un-tangle memory management of Decl and NamespaceAndrew Kelley
Before there was this "top_decl" and "tmp_namespace" stack values that were kludgy and buggy. Now Sema is slightly reworked so that files which are structs are analyzed with their own Decl and Namespace already set up. After this commit there are no memory leaks for a successful build-obj.
2021-04-29stage2: fix File incorrectly freeing its NamespaceAndrew Kelley
2021-04-29stage2: properly free Decl nameAndrew Kelley
Two problems solved: * The Decl name may be allocated with gpa or it may be a reference to the ZIR string table. * The main update() function was freeing the ZIR when we still had Decl objects referencing it.
2021-04-29AstGen: represent global variables directlyAndrew Kelley
Rather than with `block_inline_var`. This matches how function declarations work and how extern variables work.
2021-04-29AstGen: implement extern variablesAndrew Kelley
2021-04-29AstGen: implement function prototypes with alignment exprsAndrew Kelley
2021-04-29AstGen: implement anytype struct fieldsAndrew Kelley
2021-04-29AstGen: implement comptime struct fieldsAndrew Kelley
2021-04-28stage2: fix scanDecls not advancing the field bitsAndrew Kelley
Also fix `semaFile` not handling compile errors properly.
2021-04-28stage2: implement semantic analysis for functions and global varsAndrew Kelley
* AstGen: add missing `break_inline` for comptime blocks. * Module: call getTree() in byteOffset(). This generates the AST when using cached ZIR and compile errors need to be reported. * Scope.File: distinguish between successful ZIR generation and AIR generation (when Decls in scope have been scanned). - `semaFile` correctly avoids doing work twice. * Implement first pass at `lookupInNamespace`. It has various TODOs left, such as `usingnamespace`, and setting up Decl dependencies.
2021-04-28stage2: prepare for mainining Decl references to ZIR indexesAndrew Kelley
2021-04-28stage2: semaDecl properly analyzes the decl blockAndrew Kelley
Also flattened out Decl TypedValue fields into ty, val, has_tv and add relevant fields to Decl for alignment and link section.
2021-04-28Sema: do not analyze test decls when not in test modeAndrew Kelley
We do this by reserving string table indexes 0 and 1 in ZIR to be special. Decls now have 0 to mean comptime or usingnamespace, and 1 to mean an unnamed test decl.
2021-04-26stage2: rewire the frontend driver to whole-file-zirAndrew Kelley
* Remove some unused imports in AstGen.zig. I think it would make sense to start decoupling AstGen from the rest of the compiler code, similar to how the tokenizer and parser are decoupled. * AstGen: For decls, move the block_inline instructions to the top of the function so that they get lower ZIR instruction indexes. With this, the block_inline instruction index combined with its corresponding break_inline instruction index can be used to form a ZIR instruction range. This is useful for allocating an array to map ZIR instructions to semantically analyzed instructions. * Module: extract emit-h functionality into a struct, and only allocate it when emit-h is activated. * Module: remove the `decl_table` field. This previously was a table of all Decls in the entire Module. A "name hash" strategy was used to find decls within a given namespace, using this global table. Now, each Namespace has its own map of name to children Decls. - Additionally, there were 3 places that relied on iterating over decl_table in order to function: - C backend and SPIR-V backend. These now have their own decl_table that they keep populated when `updateDecl` and `removeDecl` are called. - emit-h. A `decl_table` field has been added to the new GlobalEmitH struct which is only allocated when emit-h is activated. * Module: fix ZIR serialization/deserialization bug in debug mode having to do with the secret safety tag for untagged unions. There is still an open TODO to investigate a friendlier solution to this problem with the language. * Module: improve deserialization of ZIR to allocate only exactly as much capacity as length in the instructions array so as to not waste space. * Module: move `srcHashEql` to `std.zig` to live next to the definition of `SrcHash` itself. * Module: re-introduce the logic for scanning top level declarations within a namespace. * Compilation: add an `analyze_pkg` Job which is used to kick off the start of semantic analysis by doing the equivalent of `_ = @import("std");`. The `analyze_pkg` job is unconditionally added to the work queue on every update(), with pkg set to the std lib pkg. * Rename TZIR to AIR in a few places. A more comprehensive rename will come later.