aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
AgeCommit message (Collapse)Author
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
2023-10-03codegen: fix field offsets in packed structsXavier Bouchoux
* add nested packed struct/union behavior tests * use ptr_info.packed_offset rather than trying to duplicate the logic from Sema.structFieldPtrByIndex() * use the container_ptr_info.packed_offset to account for non-aligned nested structs. * dedup type.packedStructFieldBitOffset() and module.structPackedFieldBitOffset()
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-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-25type: handle the 0-length array case in abiSizeAdvancedkcbanner
This fixes a panic in `unionAbiSize` when a 0-length array of a union is used as a struct field. Because `resolveTypeLayout` does not resolve the `elem_ty` if `arrayLenIncludingSentinel` returns 0 for the array, the child union type is not guaranteed to have a resolved layout at this point. Fixed this case by just returning 0 here.
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
2023-09-23type: resolve packed union type layouts in bitSizeAdvancedkcbanner
Before this change, packed structs containing packed unions could make it to codegen without having their layout resolved.
2023-09-23sema: rework the comptime representation of comptime unionskcbanner
When the tag is not known, it's set to `.none`. In this case, the value is either an array of bytes (for extern unions) or an integer (for packed unions).
2023-09-23sema: add support for unions in readFromMemory and writeToMemorykcbanner
2023-09-21compiler: fix structFieldName crash for tuplesAndrew Kelley
When struct types have no field names, the names are implicitly understood to be strings corresponding to the field indexes in declaration order. It used to be the case that a NullTerminatedString would be stored for each field in this case, however, now, callers must handle the possibility that there are no names stored at all. This commit introduces `legacyStructFieldName`, a function to fake the previous behavior. Probably something better could be done by reworking all the callsites of this function.
2023-09-21type: give empty unions 1-byte alignmentmlugg
Zero-byte alignment is no longer valid for runtime types. I made most of these changes in an earlier commit, but missed this case.
2023-09-21InternPool,Sema,type,llvm: alignment fixesmlugg
This changeset fixes the handling of alignment in several places. The new rules are: * `@alignOf(T)` where `T` is a runtime zero-bit type is at least 1, maybe greater. * Zero-bit fields in `extern` structs *do* force alignment, potentially offsetting following fields. * Zero-bit fields *do* have addresses within structs which can be observed and are consistent with `@offsetOf`. These are not necessarily all implemented correctly yet (see disabled test), but this commit fixes all regressions compared to master, and makes one new test pass.
2023-09-21Sema: fix dependency loop regression on struct field alignmentAndrew Kelley
2023-09-21fix regressions from this branchAndrew Kelley
2023-09-21compiler: move struct types into InternPool properAndrew Kelley
Structs were previously using `SegmentedList` to be given indexes, but were not actually backed by the InternPool arrays. After this, the only remaining uses of `SegmentedList` in the compiler are `Module.Decl` and `Module.Namespace`. Once those last two are migrated to become backed by InternPool arrays as well, we can introduce state serialization via writing these arrays to disk all at once. Unfortunately there are a lot of source code locations that touch the struct type API, so this commit is still work-in-progress. Once I get it compiling and passing the test suite, I can provide some interesting data points such as how it affected the InternPool memory size and performance comparison against master branch. I also couldn't resist migrating over a bunch of alignment API over to use the log2 Alignment type rather than a mismash of u32 and u64 byte units with 0 meaning something implicitly different and special at every location. Turns out you can do all the math you need directly on the log2 representation of alignments.
2023-09-19write function types consistently with a space before `fn` keywordr00ster91
Currently, the compiler (like @typeName) writes it `fn(...) Type` but zig fmt writes it `fn (...) Type` (notice the space after `fn`). This inconsistency is now resolved and function types are consistently written the zig fmt way. Before this there were more `fn (...) Type` occurrences than `fn(...) Type` already.
2023-09-15Sema: mark pointers to inline functions as comptime-onlymlugg
This is supposed to be the case, similar to how pointers to generic functions are comptime-only (several pieces of logic already assumed this). These types being considered runtime was causing `dbg_var_val` AIR instructions to be wrongly emitted for such values, causing codegen backends to create a runtime reference to the inline function, which (at least on the LLVM backend) triggers an error. Resolves: #38