Commit graph

459 commits

Author SHA1 Message Date
Andrew Kelley
d979dd9b58 stage2: improve AstGen FileNotFound error message
Partially addresses #9203. It fixes the first case, but not the second
one mentioned in the issue.
2021-07-02 15:27:00 -07:00
Andrew Kelley
c5c23db627 tokenizer: clean up invalid token error
It now displays the byte with proper printability handling. This makes
the relevant compile error test case no longer a regression in quality
from stage1 to stage2.
2021-07-02 13:28:31 -07:00
Andrew Kelley
24c432608f stage2: improve compile errors from tokenizer
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-02 13:27:35 -07:00
J.C. Moyer
d84b386f60 stage2: print valid filename in error messages 2021-07-02 14:08:52 -04:00
Andrew Kelley
6d47b4f39e Revert "Include package root dir in stage2 error messages"
This reverts commit 15a030ef3d.

ast-check started crashing after this commit. Needs more QA before
merging.
2021-06-27 17:30:29 -07:00
J.C. Moyer
15a030ef3d Include package root dir in stage2 error messages 2021-06-27 14:53:46 +03:00
Jacob G-W
2d2a6ed1a4 stage2: implement @setRuntimeSafety 2021-06-25 08:57:03 +03:00
Andrew Kelley
150515f44d stage2: slightly improve error reporting for missing imports
There is now a distinction between `@import` with a .zig extension and
without. Without a .zig extension it assumes it is a package name, and
returns error.PackageNotFound if not mapped into the package table.
2021-06-23 10:44:46 -07:00
Andrew Kelley
fee5aad699 stage2: remove unused parameter from importPkg 2021-06-21 17:03:04 -07:00
Andrew Kelley
f203334878 stage2: wire up AstGen to the progress bar 2021-06-21 17:03:03 -07:00
Jacob G-W
9fffffb07b fix code broken from previous commit 2021-06-21 17:03:03 -07:00
Jacob G-W
641ecc260f std, src, doc, test: remove unused variables 2021-06-21 17:03:03 -07:00
Veikka Tuominen
7efd7bc3b8 stage2: implement comptime variables 2021-06-07 22:15:56 +03:00
Martin Wickham
fc9430f567 Breaking hash map changes for 0.8.0
- 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-06-03 17:02:16 -05:00
joachimschmidt557
4c75f834e7 Re-enable building the self-hosted compiler for 32-bit targets 2021-05-25 15:38:32 -04:00
Andrew Kelley
79dee75b1c stage2: rename ir.zig to air.zig
We've settled on the nomenclature for the artifacts the compiler
pipeline produces:

1. Tokens
2. AST (Abstract Syntax Tree)
3. ZIR (Zig Intermediate Representation)
4. AIR (Analyzed Intermediate Representation)
5. Machine Code

Renaming `ir` identifiers to `air` will come with the inevitable
air-memory-layout branch that I plan to start after the 0.8.0 release.
2021-05-22 14:29:16 -07:00
Andrew Kelley
ab8f8465a3 stage2: fix deletion of Decls that get re-referenced
When scanDecls happens, we create stub Decl objects that
have not been semantically analyzed. When they get referenced,
they get semantically analyzed.

Before this commit, when they got unreferenced, they were completely
deleted, including deleted from the containing Namespace.

However, if the update did not cause the containing Namespace to get
deleted, for example, if `std.builtin.ExportOptions` is no longer
referenced, but `std.builtin` is still referenced, and then `ExportOptions`
gets referenced again, the Namespace would be incorrectly missing the
Decl, so we get an incorrect "no such member" error.

The solution is to, when dealing with a no longer referenced Decl
objects during an update, clear them to the state they would be in
on a fresh scanDecl, rather than completely deleting them.
2021-05-18 12:35:36 -07:00
Andrew Kelley
615d45da77 Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
Conflicts:
 * src/codegen/spirv.zig
 * src/link/SpirV.zig

We're going to want to improve the stage2 test harness to print
the source file name when a compile error occurs otherwise std lib
contributors are going to see some confusing CI failures when they cause
stage2 AstGen compile errors.
2021-05-17 19:30:38 -07:00
Andrew Kelley
1d808d0dd2 stage2: fix crash in switch compile error
when the AST for the switch has never been loaded
2021-05-17 19:11:11 -07:00
Andrew Kelley
67f5a28257 Sema: use a hash map for ZIR->AIR mapping
Previously, ZIR was per-function so we could simply allocate a slice for
all ZIR instructions. However now ZIR is whole-file, so we need a sparse
mapping of ZIR to AIR instructions in order to not waste memory.
2021-05-17 17:39:52 -07:00
Andrew Kelley
731c35f15a stage2: get rid of NameHash
Previously, stage2 used a global decl_table for all Decl objects, keyed
by a 16-byte name hash that was hopefully unique. Now, there is a tree
of Namespace objects that own their named Decl objects.
2021-05-17 16:09:20 -07:00
Andrew Kelley
3d99fb3352 stage2: get rid of DeclRef 2021-05-17 16:09:20 -07:00
Andrew Kelley
d5e894a9a7 stage2: get rid of failed_root_src_file 2021-05-17 16:09:20 -07:00
Andrew Kelley
3697022a41 stage2: avoid calling freeDecl when hasCodeGenBits == false
Previously the frontend incorrectly called freeDecl for structs, which
never got allocateDecl called for them. There was simply a missing check
for hasCodeGenBits().
2021-05-17 13:44:20 -07:00
Andrew Kelley
7cd94d2123 stage2: omit Decl compile errors from failed AstGen files
Just like when new parse errors occur during an update, when new AstGen
errors occur during an update, we do not reveal compile errors for Decl
objects which are inside of a newly failed File. Once the File passes
AstGen successfully, it will be compared with the previously succeeded
ZIR and the saved Decl compile errors will be handled properly.
2021-05-15 21:20:06 -07:00
Andrew Kelley
5769ed2d44 stage2: compile log stores node offset
Previously, compile log stored full SrcLoc info, which included absolute
AST node index. This becomes invalid after an incremental compilation.
To make it survive incremental compilation, store an offset from parent
Decl instead.
2021-05-14 23:10:38 -07:00
Andrew Kelley
b4692c9a78 stage2: improve Decl dependency management
* Do not report export collision errors until the very end, because it
   is possible, during an update, for a new export to be added before an
   old one is semantically analyzed to be deleted. In such a case there
   should be no compile error.
   - Likewise we defer emitting exports until the end when we know for
     sure what will happen.
 * Sema: Fix not adding a Decl dependency on imported files.
 * Sema: Properly add Decl dependencies for all identifier and namespace
   lookups.
 * After semantic analysis for a Decl, if it is still marked as
   `in_progress`, change it to `dependency_failure` because if the Decl
   itself failed, it would have already been changed during the call to
   add the compile error.
2021-05-14 17:41:22 -07:00
Andrew Kelley
c102e13710 stage2: fix source location of Decl compile errors
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-12 22:50:47 -07:00
Andrew Kelley
344dc0cc0f stage2: fix handling of "prev successful ZIR"
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-12 22:02:44 -07:00
Andrew Kelley
c9cc09a3bf Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen
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-12 16:41:20 -07:00
Andrew Kelley
71afc30880 stage2: more Decl lifetime fixes
* 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-11 23:20:22 -07:00
Andrew Kelley
1ab1a96f87 stage2: improve Decl lifetime management
* 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-11 22:12:36 -07:00
Andrew Kelley
7873e4f588 stage2: lookupIdentifier can return error.AnalysisFailed
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-11 17:44:19 -07:00
Andrew Kelley
cbbc7cc8b1 stage2: better handling of file-level compile errors across updates
* `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-11 17:34:13 -07:00
Andrew Kelley
bcf15e39e2 stage2: add owns_tv flag to Module.Decl
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-11 14:17:52 -07:00
Andrew Kelley
dae22a0a1f stage2: struct, union, enum, opaque, error sets get better names
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-10 22:50:00 -07:00
Andrew Kelley
b9a099e83c stage2: type declarations ZIR encode AnonNameStrategy
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-10 21:34:43 -07:00
Andrew Kelley
5cd9afc6b6 stage2: fully qualified names and better anonymous names
* 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-08 14:34:30 -07:00
Andrew Kelley
28353b3159 stage2: fix struct inits not getting fields resolved 2021-05-07 22:16:15 -07:00
Andrew Kelley
d577654e66 stage2: fix stack overflow in @setEvalBranchQuota test case
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-07 20:03:27 -07:00
Andrew Kelley
81d5104e22 stage2: implement global variables
* 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-07 18:52:11 -07:00
Andrew Kelley
e7c4d545cd stage2: no '$' in anonymous decl names
This way it will not cause a compile error for the C backend.
2021-05-07 16:18:49 -07:00
Andrew Kelley
6ac2047142 stage2: implement extern functions 2021-05-07 16:06:25 -07:00
Andrew Kelley
47531b7d93 Sema: support enough to check main calling convention via @typeInfo
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-07 14:18:14 -07:00
Andrew Kelley
cdea22f5d7 stage2: wire up outdated/deleted decl detection
we're back to incremental compilation working smoothly
2021-05-06 17:20:45 -07:00
Andrew Kelley
17067e0e6b stage2: fix contents hash computation
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-05 17:00:10 -07:00
Andrew Kelley
9b1aac8a65 stage2: mapping old to new ZIR recursively
now it walks into functions and blocks to find decls.
2021-05-05 16:56:24 -07:00
Andrew Kelley
5d7f2697de stage2: add zig changelist debug command
and implement the first pass at mechanism to map old ZIR to new ZIR.
2021-05-05 13:16:14 -07:00
Andrew Kelley
7a27f0d80c Sema: restore the extern lib name functionality 2021-05-04 14:40:59 -07:00
Andrew Kelley
edfbf85ecd Sema: implement error sets 2021-05-04 13:58:08 -07:00