aboutsummaryrefslogtreecommitdiff
path: root/src/Sema.zig
AgeCommit message (Collapse)Author
2023-10-26link: support exporting constant values without a DeclAndrew Kelley
The main motivating change here is to prevent the creation of a fake Decl object by the frontend in order to `@export()` a value. Instead, `link.updateDeclExports` is renamed to `link.updateExports` and accepts a tagged union which can be either a Decl.Index or a InternPool.Index.
2023-10-26sema: remove source location logic in zirExportValueAndrew Kelley
`.unneeded` source location should never be passed when the source location is in fact available.
2023-10-25Sema: replace refValue with simpler implementationAndrew Kelley
Move some more anon decls to the new mechanism
2023-10-25Sema: rework addConstantMaybeRefAndrew Kelley
and add anonDeclRef and migrate some anon decls to it
2023-10-25Sema: move `@typeInfo` anon decls to new mechanismAndrew Kelley
2023-10-25Sema: move some TODO comments to an issueAndrew Kelley
Tracking issue: #17689
2023-10-25Sema: migrate `@src` to new anon decl mechanismAndrew Kelley
2023-10-24Sema: don't allow undef values through resolveDefinedValue in typeof blockmlugg
This logic is not correct in most cases. If any instruction needs to operate with different semantics within `@TypeOf`, it should be made to do so explicitly. This broke a line in `std.mem`: I have opted to fix this in std for now, since as far as I know it's not yet been discussed which operations (if any) should be special-cased like this within `@TypeOf`.
2023-10-24Sema: rename value resolution functionsmlugg
Having simplified these functions in a previous commit, I felt inclined to refactor their names, which were previously quite inconsistent. There are now 4 "core" functions: * `resolveValue` (previously `resolveMaybeUndefVal`) allows runtime-known and undef values. * `resolveConstValue` (previously `resolveConstMaybeUndefVal`) allows undef but not runtime-known values. * `resolveDefinedValue` (name unchanged) allows runtime-known values but not comptime-known undef. * `resolveConstDefinedValue` (previously `resolveConstValue`) does not allow runtime-known or undef values. You can see the inconsistencies in the old names here - sometimes we specified "maybe undef", and sometimes we went the other way by specifying "defined". With the new names, the most common function, `resolveValue`, has the shortest name and does the most general thing, and is the baseline that the other functions are adding logic to. Some other functions were also renamed: * `resolveMaybeUndefLazyVal` -> `resolveValueResolveLazy` * `resolveMaybeUndefValIntable` -> `resolveValueIntable` * `resolveMaybeUndefValAllowVariables` -> `resolveValueAllowVariables`
2023-10-24InternPool: remove runtime_value representationmlugg
The main goal of this commit is to remove the `runtime_value` field from `InternPool.Key` (and its associated representation), but there are a few dominos. Specifically, this mostly eliminates the "maybe runtime" concept from value resolution in Sema: so some resolution functions like `resolveMaybeUndefValAllowVariablesMaybeRuntime` are gone. This required a small change to struct/union/array initializers, to no longer use `runtime_value` if a field was a `variable` - I'm not convinced this case was even reachable, as `variable` should only ever exist as the trivial value of a global runtime `var` decl. Now, the only case in which a `Sema.resolveMaybeUndefVal`-esque function can return the `variable` key is `resolveMaybeUndefValAllowVariables`, which is directly called from `Sema.resolveInstValueAllowVariables` (previously `Sema.resolveInstValue`), which is only used for resolving the value of a Decl from `Module.semaDecl`. While changing these functions, I also slightly reordered and restructured some of them, and updated their doc comments.
2023-10-24Merge pull request #17688 from ziglang/comptime-srcAndrew Kelley
Sema: make `@src().line` comptime-known
2023-10-23Sema: make `@src().line` comptime-knownAndrew Kelley
Reverts 89cef9f5f731f8f33dc935aac3c21bd57c92900d. Closes #13315
2023-10-23frontend: rework `@embedFile` for incremental compilationAndrew Kelley
This feature was made to work with the legacy incremental compilation mechanism which is being reworked. This commit regresses the ability to update files used with `@embedFile` while the compiler is running. In exchange, we get these benefits: * The embedded file contents are copied directly into InternPool rather than there being an extra allocation and memcpy. * The EmbedFile struct, which represents a long-lived object, is made more serialization friendly. * Eliminate the creation and use of a Decl as an anonymous decl. When implementing the new incremental compilation mechanism, functionality will need to be added back for handling `@embedFile`.
2023-10-23Merge pull request #17651 from Vexu/error-limitAndrew Kelley
Make distinct error limit configurable (attempt #2)
2023-10-22remove uses of non-configurable `err_int`Veikka Tuominen
2023-10-21Sema: migrate zirResolveInferredAlloc to new anon decl mechanismAndrew Kelley
This required a bug fix in zirMakePtrConst.
2023-10-21InternPool: store alignment of anon declsAndrew Kelley
Commit 5393e56500d499753dbc39704c0161b47d1e4d5c has a flaw pointed out by @mlugg: the `ty` field of pointer values changes when comptime values are pointer-casted. This commit introduces a new encoding which additionally stores the "original pointer type" which is used to store the alignment of the anonymous decl, and potentially other information in the future such as section and pointer address space. However, this new encoding is only used when the original pointer type differs from the casted pointer type in a meaningful way. I was able to make the LLVM backend and the C backend lower anonymous decls with the appropriate alignment, however I will need some help figuring out how to do this for the backends that lower anonymous decls via src/codegen.zig and the wasm backend.
2023-10-21migrate make_ptr_const to new anonymous decl mechanismAndrew Kelley
Instead of creating Module.Decl objects, directly create InternPool pointer values using the anon_decl Addr encoding. The LLVM backend needed code to notice the alignment of the pointer and lower accordingly. The other backends likely need a similar change.
2023-10-18Merge pull request #17561 from alichraghi/spirv-0Robin Voetter
spirv: memcpy
2023-10-17Sema: disallow `@intFromPtr` for comptime-only typesBogdan Romanyuk
2023-10-17sema: emit logical and/or for safety checksAli Chraghi
this has no performance impact as there is no branching either way
2023-10-16Sema: fix missing check for tuple default initializersVeikka Tuominen
Closes #17525
2023-10-16Sema: fix `@extern` error on function pointerBogdan Romanyuk
2023-10-13Merge pull request #17510 from Vexu/vectorAndrew Kelley
Fix `@Vector` source locations being swapped
2023-10-13Sema: fix `@Vector` source locations being swappedVeikka Tuominen
2023-10-13Sema: fix crash when ref coercion dest is var argsAndrew Kelley
When analyzing the `validate_ref_ty` ZIR instruction, an assertion would trip if the result type was a var args function argument. The fix is the same as e6b73be870a39f4da7a08a40da23e38b5e9613da - inline the logic of `resolveType` and handle the case of var args. Closes #17494
2023-10-12Sema: fix crash when coercion dest is var argsAndrew Kelley
When analyzing the `as` ZIR instruction, an assertion would trip if the result type was a var args function argument. The fix is simple: inline a little bit of the `resolveType` logic into `analyzeAs` to make it detect this situation - which it was already attempting to do. Closes #16197
2023-10-10Sema,type: unify type query functionsmlugg
The following pairs of functions have been combined using the "advanced" pattern used for other type queries: * `Sema.fnHasRuntimeBits`, `Type.isFnOrHasRuntimeBits` * `Sema.typeRequiresComptime`, `Type.comptimeOnly`
2023-10-09Sema: fix `dbg_inline` instructions not being emittedVeikka Tuominen
This broke with #16604 but went unnoticed due to lack of tests. Closes #17444
2023-10-08give modules friendly names for error reportingAndrew Kelley
2023-10-08use long-lived arena for `@cImport`-generated ModuleAndrew Kelley
2023-10-08finish hooking up new dependency tree logicAndrew Kelley
* add Module instances for each package's build.zig and attach it to the dependencies.zig module with the hash digest hex string as the name. * fix incorrectly skipping the wrong packages for creating dependencies.zig * a couple more renaming of "package" to "module"
2023-10-08more fixes related to previous commits Package/Module APIAndrew Kelley
2023-10-08CLI: finish updating module API usageAndrew Kelley
Finish the work started in 4c4fb839972f66f55aa44fc0aca5f80b0608c731. Now the compiler compiles again. Wire up dependency tree fetching code in the CLI for `zig build`. Everything is hooked up except for `createDependenciesModule` is not yet implemented.
2023-10-03compiler: start handling anonymous decls differentlyAndrew Kelley
Instead of explicitly creating a `Module.Decl` object for each anonymous declaration, each `InternPool.Index` value is implicitly understood to be an anonymous declaration when encountered by backend codegen. The memory management strategy for these anonymous decls then becomes to garbage collect them along with standard InternPool garbage. In the interest of a smooth transition, this commit only implements this new scheme for string literals and leaves all the previous mechanisms in place.
2023-10-03Merge pull request #17352 from kcbanner/extern_union_comptime_memoryAndrew Kelley
sema: Support reinterpreting extern/packed unions at comptime via field access
2023-10-03Sema: fix issues in `@errorCast` with error unionsVeikka Tuominen
2023-10-02sema: handle big-endian when bitcasting between different-sized union fieldskcbanner
Updated the tests to also run at runtime, and moved them to union.zig
2023-10-02sema: support reinterpreting extern/packed unions at comptime via field accesskcbanner
My previous change for reading / writing to unions at comptime did not handle union field read/writes correctly in all cases. Previously, if a field was written to a union, it would overwrite the entire value. This is problematic when a field of a larger size is subsequently read, because the value would not be long enough, causing a panic. Additionally, the writing behaviour itself was incorrect. Writing to a field of a packed or extern union should only overwrite the bits corresponding to that field, allowing for memory reintepretation via field writes / reads. I addressed these problems as follows: Add the concept of a "backing type" for extern / packed unions (`Type.unionBackingType`). For extern unions, this is a `u8` array, for packed unions it's an integer matching the `bitSize` of the union. Whenever union memory is read at comptime, it's read as this type. When union memory is written at comptime, the tag may still be known. If so, the memory is written using the tagged type. If the tag is unknown (because this union had previously been read from memory), it's simply written back out as the backing type. I added `write_packed` to the `reinterpret` field of `ComptimePtrMutationKit`. This causes writes of the operand to be packed - which is necessary when writing to a field of a packed union. Without this, writing a value to a `u1` field would overwrite the entire byte it occupied. The final case to address was reading a different (potentially larger) field from a union when it was written with a known tag. To handle this, a new kind of bitcast was introduced (`bitCastUnionFieldVal`) which supports reading a larger field by using a backing buffer that has the unwritten bits set to undefined. The reason to support this (vs always just writing the union as it's backing type), is that no reads to larger fields ever occur at comptime, it would be strictly worse to have spent time writing the full backing type.
2023-10-01x86_64: implement and test unary float builtinsJacob Young
2023-10-01Sema: add `@errorCast` which works for both error sets and error unionsVeikka Tuominen
Closes #17343
2023-09-27Revert "compiler: don't use `@abs` builtin yet"Andrew Kelley
This reverts commit 21780899eb17a0cb795ff40e5fae6556c38ea13e. After this commit, a version of the compiler which supports the new `@abs` builtin is required.
2023-09-27compiler: don't use `@abs` builtin yetAndrew Kelley
This commit can be used to rebuild zig1.wasm
2023-09-27Rename `@fabs` to `@abs` and accept integersantlilja
Replaces the @fabs builtin with a new @abs builtins which accepts floats, signed integers and vectors of said types.
2023-09-26Merge pull request #17215 from kcbanner/read_from_memory_unionVeikka Tuominen
sema: add support for unions in readFromMemory and writeToMemory
2023-09-25translate-c: convert clang errors messages into `std.zig.ErrorBundle`Techatrix
2023-09-24Merge pull request #17256 from ziglang/packed-bit-offsetsAndrew Kelley
compiler: packed structs cache bit offsets
2023-09-24revert "compiler: packed structs cache bit offsets"Andrew Kelley
This is mostly a revert of a7088fd9a3edb037f0f51bb402a3c557334634f3. Measurement revealed the commit actually regressed performance.
2023-09-23compiler: packed structs cache bit offsetsAndrew Kelley
Instead of linear search every time a packed struct field's bit or byte offset is wanted, they are computed once during resolution of the packed struct's backing int type, and stored in InternPool for O(1) lookup. Closes #17178
2023-09-23compiler: preserve result type information through address-of operatormlugg
This commit introduces the new `ref_coerced_ty` result type into AstGen. This represents a expression which we want to treat as an lvalue, and the pointer will be coerced to a given type. This change gives known result types to many expressions, in particular struct and array initializations. This allows certain casts to work which previously required explicitly specifying types via `@as`. It also eliminates our dependence on anonymous struct types for expressions of the form `&.{ ... }` - this paves the way for #16865, and also results in less Sema magic happening for such initializations, also leading to potentially better runtime code. As part of these changes, this commit also implements #17194 by disallowing RLS on explicitly-typed struct and array initializations. Apologies for linking these changes - it seemed rather pointless to try and separate them, since they both make big changes to struct and array initializations in AstGen. The rationale for this change can be found in the proposal - in essence, performing RLS whilst maintaining the semantics of the intermediary type is a very difficult problem to solve. This allowed the problematic `coerce_result_ptr` ZIR instruction to be completely eliminated, which in turn also simplified the logic for inferred allocations in Sema - thanks to this, we almost break even on line count! In doing this, the ZIR instructions surrounding these initializations have been restructured - some have been added and removed, and others renamed for clarity (and their semantics changed slightly). In order to optimize ZIR tag count, the `struct_init_anon_ref` and `array_init_anon_ref` instructions have been removed in favour of using `ref` on a standard anonymous value initialization, since these instructions are now virtually never used. Lastly, it's worth noting that this commit introduces a slightly strange source of generic poison types: in the expression `@as(*anyopaque, &x)`, the sub-expression `x` has a generic poison result type, despite no generic code being involved. This turns out to be a logical choice, because we don't know the result type for `x`, and the generic poison type represents precisely this case, providing the semantics we need. Resolves: #16512 Resolves: #17194