aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
AgeCommit message (Collapse)Author
2024-07-04compiler: type.zig -> Type.zigmlugg
2024-06-26Zir: make `src_line` absolute for `declaration` instructionsmlugg
We need special logic for updating line numbers anyway, so it's fine to just use absolute numbers here. This eliminates a field from `Decl`.
2024-06-22rename src/Module.zig to src/Zcu.zigAndrew Kelley
This patch is a pure rename plus only changing the file path in `@import` sites, so it is expected to not create version control conflicts, even when rebasing.
2024-06-18Zir: provide absolute node for `reify`mlugg
Since we track `reify` instructions across incremental updates, it is acceptable to treat it as the baseline for a relative source location. This turns out to be a good idea, since it makes it easy to define the source location for a reified type.
2024-06-15Zcu: rework source locationsmlugg
`LazySrcLoc` now stores a reference to the "base AST node" to which it is relative. The previous tagged union is `LazySrcLoc.Offset`. To make working with this structure convenient, `Sema.Block` contains a convenience `src` method which takes an `Offset` and returns a `LazySrcLoc`. The "base node" of a source location is no longer given by a `Decl`, but rather a `TrackedInst` representing either a `declaration`, `struct_decl`, `union_decl`, `enum_decl`, or `opaque_decl`. This is a more appropriate model, and removes an unnecessary responsibility from `Decl` in preparation for the upcoming refactor which will split it into `Nav` and `Cau`. As a part of these `Decl` reworks, the `src_node` field is eliminated. This change aids incremental compilation, and simplifies `Decl`. In some cases -- particularly in backends -- the source location of a declaration is desired. This was previously `Decl.srcLoc` and worked for any `Decl`. Now, it is `Decl.navSrcLoc` in reference to the upcoming refactor, since the set of `Decl`s this works for precisely corresponds to what will in future become a `Nav` -- that is, source-level declarations and generic function instantiations, but *not* type owner Decls. This commit introduces more tags to `LazySrcLoc.Offset` so as to eliminate the concept of `error.NeededSourceLocation`. Now, `.unneeded` should only be used to assert that an error path is unreachable. In the future, uses of `.unneeded` can probably be replaced with `undefined`. The `src_decl` field of `Sema.Block` no longer has a role in type resolution. Its main remaining purpose is to handle namespacing of type names. It will be eliminated entirely in a future commit to remove another undue responsibility from `Decl`. It is worth noting that in future, the `Zcu.SrcLoc` type should probably be eliminated entirely in favour of storing `Zcu.LazySrcLoc` values. This is because `Zcu.SrcLoc` is not valid across incremental updates, and we want to be able to reuse error messages from previous updates even if the source file in question changed. The error reporting logic should instead simply resolve the location from the `LazySrcLoc` on the fly.
2024-05-08fix wrong int alignment for i65..i127 on x86 archAndrew Kelley
2024-05-08Type.intAbiAlignment: update for LLVM 18 changesAndrew Kelley
This function is intended to match what the backend desires. No kink shaming.
2024-05-08update maxIntAlignment for x86 to 16Andrew Kelley
LLVMABIAlignmentOfType(i128) reports 16 on this target, however the C ABI uses align(4). Clang in LLVM 17 does this: %struct.foo = type { i32, i128 } Clang in LLVM 18 does this: %struct.foo = type <{ i32, i128 }> Clang is working around the 16-byte alignment to use align(4) for the C ABI by making the LLVM struct packed.
2024-05-08std.Target.maxIntAlignment: move to compiler implementationAndrew Kelley
This should not be a public API, and the x86 backend does not support the value 16.
2024-05-08LLVM 18 update: avoid passing vectors sometimesAndrew Kelley
LLVM now refuses to lower arguments and return values on x86 targets when the total vector bit size is >= 512. This code detects such a situation and uses byref instead of byval.
2024-04-17compiler: rework comptime pointer representation and accessmlugg
We've got a big one here! This commit reworks how we represent pointers in the InternPool, and rewrites the logic for loading and storing from them at comptime. Firstly, the pointer representation. Previously, pointers were represented in a highly structured manner: pointers to fields, array elements, etc, were explicitly represented. This works well for simple cases, but is quite difficult to handle in the cases of unusual reinterpretations, pointer casts, offsets, etc. Therefore, pointers are now represented in a more "flat" manner. For types without well-defined layouts -- such as comptime-only types, automatic-layout aggregates, and so on -- we still use this "hierarchical" structure. However, for types with well-defined layouts, we use a byte offset associated with the pointer. This allows the comptime pointer access logic to deal with reinterpreted pointers far more gracefully, because the "base address" of a pointer -- for instance a `field` -- is a single value which pointer accesses cannot exceed since the parent has undefined layout. This strategy is also more useful to most backends -- see the updated logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do prefer a chain of field and elements accesses for lowering pointer values, such as SPIR-V, there is a helpful function in `Value` which creates a strategy to derive a pointer value using ideally only field and element accesses. This is actually more correct than the previous logic, since it correctly handles pointer casts which, after the dust has settled, end up referring exactly to an aggregate field or array element. In terms of the pointer access code, it has been rewritten from the ground up. The old logic had become rather a mess of special cases being added whenever bugs were hit, and was still riddled with bugs. The new logic was written to handle the "difficult" cases correctly, the most notable of which is restructuring of a comptime-only array (for instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`. Currently, the logic for loading and storing work somewhat differently, but a future change will likely improve the loading logic to bring it more in line with the store strategy. As far as I can tell, the rewrite has fixed all bugs exposed by #19414. As a part of this, the comptime bitcast logic has also been rewritten. Previously, bitcasts simply worked by serializing the entire value into an in-memory buffer, then deserializing it. This strategy has two key weaknesses: pointers, and undefined values. Representations of these values at comptime cannot be easily serialized/deserialized whilst preserving data, which means many bitcasts would become runtime-known if pointers were involved, or would turn `undefined` values into `0xAA`. The new logic works by "flattening" the datastructure to be cast into a sequence of bit-packed atomic values, and then "unflattening" it; using serialization when necessary, but with special handling for `undefined` values and for pointers which align in virtual memory. The resulting code is definitely slower -- more on this later -- but it is correct. The pointer access and bitcast logic required some helper functions and types which are not generally useful elsewhere, so I opted to split them into separate files `Sema/comptime_ptr_access.zig` and `Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small public APIs. Whilst working on this branch, I caught various unrelated bugs with transitive Sema errors, and with the handling of `undefined` values. These bugs have been fixed, and corresponding behavior test added. In terms of performance, I do anticipate that this commit will regress performance somewhat, because the new pointer access and bitcast logic is necessarily more complex. I have not yet taken performance measurements, but will do shortly, and post the results in this PR. If the performance regression is severe, I will do work to to optimize the new logic before merge. Resolves: #19452 Resolves: #19460
2024-04-08InternPool: remove slice from byte aggregate keysJacob Young
This deletes a ton of lookups and avoids many UAF bugs. Closes #19485
2024-03-30cbe: rewrite `CType`Jacob Young
Closes #14904
2024-03-26compiler: eliminate TypedValuemlugg
The only logic which remained in this file was the Value printing logic. This has been moved into a new `print_value.zig`.
2024-03-26compiler: eliminate most usages of TypedValuemlugg
2024-03-26compiler: eliminate legacy Value representationmlugg
Good riddance! Most of these changes are trivial. There's a fix for a minor bug this exposed in `Value.readFromPackedMemory`, but aside from that, it's all just things like changing `intern` calls to `toIntern`.
2024-03-17Merge pull request #19333 from Vexu/fixesAndrew Kelley
Miscellaneous error fixes
2024-03-17Sema: fix spurious type has no namespace errorVeikka Tuominen
Closes #19232
2024-03-17Sema: fix printing of inferred error set of generic fnVeikka Tuominen
Closes #19332
2024-03-17AstGen: disallow alignment on function typesJacob Young
A pointer type already has an alignment, so this information does not need to be duplicated on the function type. This already has precedence with addrspace which is already disallowed on function types for this reason. Also fixes `@TypeOf(&func)` to have the correct addrspace and alignment.
2024-03-11std.builtin: make container layout fields lowercaseTristan Ross
2024-03-06compiler: namespace type equivalence based on AST node + capturesmlugg
This implements the accepted proposal #18816. Namespace-owning types (struct, enum, union, opaque) are no longer unique whenever analysed; instead, their identity is determined based on their AST node and the set of values they capture. Reified types (`@Type`) are deduplicated based on the structure of the type created. For instance, if two structs are created by the same reification with identical fields, layout, etc, they will be the same type. This commit does not produce a working compiler; the next commit, adding captures for decl references, is necessary. It felt appropriate to split this up. Resolves: #18816
2024-03-06compiler: represent captures directly in InternPoolmlugg
These were previously associated with the type's namespace, but we need to store them directly in the InternPool for #18816.
2024-03-06InternPool: create specialized functions for loading namespace typesmlugg
Namespace types (`struct`, `enum`, `union`, `opaque`) do not use structural equality - equivalence is based on their Decl index (and soon will change to AST node + captures). However, we previously stored all other information in the corresponding `InternPool.Key` anyway. For logical consistency, it makes sense to have the key only be the true key (that is, the Decl index) and to load all other data through another function. This introduces those functions, by the name of `loadStructType` etc. It's a big diff, but most of it is no-brainer changes. In future, it might be nice to eliminate a bunch of the loaded state in favour of accessor functions on the `LoadedXyzType` types (like how we have `LoadedUnionType.size()`), but that can be explored at a later date.
2024-02-26Fix tuple default valuesJohn Schmidt
- Add default values to the list of comptime-known elements in `zirValidatePtrArrayInit` - In `structFieldValueComptime`, only assert `haveFieldInits` if we enter the`fieldIsComptime` branch (otherwise they are not needed).
2024-02-25x86_64: fix alignment of bool vectorsJacob Young
2024-02-25Sema: implement vector coercionsJacob Young
These used to be lowered elementwise in air, and now are a single air instruction that can be lowered elementwise in the backend if necessary.
2024-02-25x86_64: implement `@shuffle`Jacob Young
2024-02-16InternPool: make more use of `NullTerminatedString.Slice`Jacob Young
This should avoid the random pointer invalidation crashes. Closes #18954
2024-02-12Sema: add declared here notes in `fail`Veikka Tuominen
This ensures that the note is added in more places and that `errMsg` needs to be used in fewer places.
2024-02-06Merge pull request #18814 from mlugg/incremental-dependenciesMatthew Lugg
Begin re-implementing incremental compilation
2024-02-05compiler: rename value.zig to Value.zigAndrew Kelley
This commit only does the file rename to be friendlier to version control conflicts.
2024-02-04compiler: re-introduce dependencies for incremental compilationmlugg
Sema now tracks dependencies appropriately. Early logic in Zcu for resolving outdated decls/functions is in place. The setup used does not support `usingnamespace`; compilations using this construct are not yet supported by this incremental compilation model.
2024-02-02InternPool: use separate key for slicesmlugg
This change eliminates some problematic recursive logic in InternPool, and provides a safer API.
2024-01-29Fix some comptime packed struct issuesSuperAuguste
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2024-01-20Sema: replace uses of `toUnsignedInt` with `toUnsignedIntAdvanced`Veikka Tuominen
During semantic analysis the value may be an unresolved lazy value which makes using `toUnsignedInt` invalid. Add assertions to detect similar issues in the future. Closes #18624
2023-11-26move Module.Decl.Index and Module.Namespace.Index to InternPoolMeghan Denny
2023-11-25convert `toType` and `toValue` to `Type.fromInterned` and `Value.fromInterned`Techatrix
2023-11-11sema: pass sema through if available for the array_type case in bitSizeAdvancedkcbanner
2023-11-07sema: analyze field init bodies in a second passkcbanner
This change allows struct field inits to use layout information of their own struct without causing a circular dependency. `semaStructFields` caches the ranges of the init bodies in the `StructType` trailing data. The init bodies are then resolved by `resolveStructFieldInits`, which is called before the inits are actually required. Within the init bodies, the struct decl's instruction is repurposed to refer to the field type itself. This is to allow us to easily rebuild the inst_map mapping required for the init body instructions to refer to the field type. Thanks to @mlugg for the guidance on this one!
2023-10-31sema: Add union alignment resolutionkcbanner
- Add resolveUnionAlignment, to resolve a union's alignment only, without triggering layout resolution. - Update resolveUnionLayout to cache size, alignment, and padding. abiSizeAdvanced and abiAlignmentAdvanced now use this information instead of computing it each time.
2023-10-29Fix hasRuntimeBitsAdvanced lazy case for pointers and optionals DilithiumNitrate
As suggested by mlugg, always returns `error.NeedLazy`. If this has a performance impact, it could be replaced by adding lazy handling to `comptimeOnlyAdvanced`.
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-22remove uses of non-configurable `err_int`Veikka Tuominen
2023-10-21make distinct error limit configurableVeikka Tuominen
Closes #786
2023-10-20Revert "make distinct error limit configurable"Andrew Kelley
This reverts commit 78855bd21866b515018259a2194e036e4b3120df. This commit did not replace uses of `Type.err_int` of which there are currently 60 uses. Re-opens #786
2023-10-16make distinct error limit configurableVeikka Tuominen
Closes #786
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-03Merge pull request #17352 from kcbanner/extern_union_comptime_memoryAndrew Kelley
sema: Support reinterpreting extern/packed unions at comptime via field access
2023-10-03codegen: fix byte-aligned field offsets in unaligned nested packed structsXavier Bouchoux