aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
AgeCommit message (Collapse)Author
2022-05-24stage2: treat `error{}!void` as a zero-bit typeAndrew Kelley
2022-05-24stage2: fixes for error unions, optionals, errorsAndrew Kelley
* `?E` where E is an error set with only one field now lowers the same as `bool`. * Fix implementation of errUnionErrOffset and errUnionPayloadOffset to properly compute the offset of each field. Also name them the same as the corresponding LLVM functions and have the same function signature, to avoid confusion. This fixes a bug where wasm was passing the error union type instead of the payload type. * Fix C backend handling of optionals with zero-bit payload types. * C backend: separate out airOptionalPayload and airOptionalPayloadPtr which reduces branching and cleans up control flow. * Make Type.isNoReturn return true for error sets with no fields. * Make `?error{}` have only one possible value (null).
2022-05-24stage2: make `?anyerror` represented the same as `anyerror`Andrew Kelley
I was able to get the backend implementation working on LLVM and the C backend, but I'm going to ask for some help on the other backends.
2022-05-24stage2: fixes for error union semanticsAndrew Kelley
* Sema: avoid unnecessary safety checks when an error set is empty. * Sema: make zirErrorToInt handle comptime errors that are represented as integers. * Sema: make empty error sets properly integrate with typeHasOnePossibleValue. * Type: correct the ABI alignment and size of error unions which have both zero-bit error set and zero-bit payload. The previous code did not account for the fact that we still need to store a bit for whether there is an error. * LLVM: lower error unions possibly with the payload first or with the error code first, depending on alignment. Previously it always put the error code first and used a padding array. * LLVM: lower functions which have an empty error set as the return type the same as anyerror, so that they can be used where fn()anyerror function pointers are expected. In such functions, Zig will lower ret to returning zero instead of void. As a result, one more behavior test is passing.
2022-05-20Sema: introduce laziness to `@sizeOf`Andrew Kelley
Motivation: the behavior test that is now passing. The main change in this commit is introducing `Type.abiSizeAdvanced`, `Value.Tag.lazy_size`, and adjusting `Sema.zirSizeOf` to take advantage of these. However, the bulk of lines changed in this commit ended up being moving logic from value.zig and type.zig into Sema.zig. This logic had no business being in Type/Value as it was only called from a Sema context, and we need access to the Sema context for error reporting when a lazy Value is resolved. Also worth mentioning is that I bumped up the comptime `@floatToInt` implementation from using f64 to f128.
2022-05-16stage2: implement error return tracesVeikka Tuominen
2022-05-16stage2: add global `Type` constant for `u1`William Sengir
2022-05-13target: Rename sparcv9 -> sparc64Koakuma
Rename all references of sparcv9 to sparc64, to make Zig align more with other projects. Also, added new function to convert glibc arch name to Zig arch name, since it refers to the architecture as sparcv9. This is based on the suggestion by @kubkon in PR 11847. (https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-05-07Merge pull request #11605 from Luukdegram/wasm-mul-overflowJakub Konka
stage2: wasm - Improve `@mulWithOverflow` implementation
2022-05-07wasm: `@addWithOverflow` for bitsize 32Luuk de Gram
2022-05-06Sema: solve a false positive "depends on itself"Andrew Kelley
This improves the ABI alignment resolution code. This commit fully enables the MachO linker code in stage3. Note, however, that there are still miscompilations in stage3.
2022-05-04stage2: fix `@sizeOf` for structs with comptime fieldsAndrew Kelley
2022-05-04stage2: improve `@sizeOf` and `@alignOf` integersAndrew Kelley
Prior to this commit, the logic for ABI size and ABI alignment for integers was naive and incorrect. This results in wasted hardware as well as undefined behavior in the LLVM backend when we memset an incorrect number of bytes to 0xaa due to disagreeing with LLVM about the ABI size of integers. This commit introduces a "max int align" value which is different per Target. This value is used to derive the ABI size and alignment of all integers. This commit makes an interesting change from stage1, which treats 128-bit integers as 16-bytes aligned for x86_64-linux. stage1 is incorrect. The maximum integer alignment on this system is only 8 bytes. This change breaks the behavior test called "128-bit cmpxchg" because on that target, 128-bit cmpxchg does require a 16-bytes aligned pointer to a 128 bit integer. However, this alignment property does not belong on *all* 128 bit integers - only on the pointer type in the `@cmpxchg` builtin function prototype. The user can then use an alignment override annotation on a 128-bit integer variable or struct field to obtain such a pointer.
2022-04-20stage2: use indexes for Decl objectsAndrew Kelley
Rather than allocating Decl objects with an Allocator, we instead allocate them with a SegmentedList. This provides four advantages: * Stable memory so that one thread can access a Decl object while another thread allocates additional Decl objects from this list. * It allows us to use u32 indexes to reference Decl objects rather than pointers, saving memory in Type, Value, and dependency sets. * Using integers to reference Decl objects rather than pointers makes serialization trivial. * It provides a unique integer to be used for anonymous symbol names, avoiding multi-threaded contention on an atomic counter.
2022-04-16stage2: fix @mulAdd on aarch64 DarwinJakub Konka
According to Apple docs, the long double type is a double precision IEEE754 binary floating-point type, which makes it identical to the double type. This behavior contrasts to the standard specification, in which a long double is a quad-precision, IEEE754 binary, floating-point type. Thus, we need to take this into account when using the compiler intrinsics so that we select the correct function version for FloatMulAdd.
2022-04-15Merge pull request #11242 from schmee/sema-handle-more-union-errorsVeikka Tuominen
stage2: add more union compile errors / improve error messages
2022-04-14stage2: progress towards stage3Andrew Kelley
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly doing element casting for slice element types. This required an enhancement both to stage1 and stage2. * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that abused this is fixed. * Make some parameters comptime to support functions in switch expressions (as opposed to making them function pointers). * Avoid relying on local temporaries being mutable. * Workarounds for when stage1 and stage2 disagree on function pointer types. * Workaround recursive formatting bug with a `@panic("TODO")`. * Remove unreachable `else` prongs for some inferred error sets. All in effort towards #89.
2022-04-14Sema: workaround for generic instantiation recurison bugAndrew Kelley
2022-04-03sema: add compile error for missing/extra enum fields in union declJohn Schmidt
2022-03-27Merge pull request #10871 from schmee/stage2-bitcast-safetyVeikka Tuominen
stage2: add type checking for @bitCast
2022-03-26stage2: resolve types more lazilyAndrew Kelley
This avoids unwanted "foo depends on itself" compilation errors.
2022-03-26stage2: result location types for function call argumentsAndrew Kelley
* AstGen: restore the param_type ZIR instruction and pass it to the expression for function call arguments. This does not solve the problem for generic function parameters, but it catches stage2 up to stage1 which also does not solve the problem for generic function parameters. - Most of the enhancements in this commit will still be needed for a more sophisticated further improvement to handle generic function types. - In Sema, handling of `as` coercion recognizes the `var_args_param` Type Tag and passes the operand through doing no coercion. - That was the last ZIR tag and we are now using all 256 ZIR tags. * AstGen: array init and struct init expressions use the anon form even when the result location has a type. Prevents the type system incorrectly believing, for example, that a tuple is actually an array when the result location is a param_type of a function with `anytype` parameter. * Sema: add missing coercion in `unionInit` to coerce the init to the corresponding union field type. * `Value.fieldValue` now takes a type and does not take an allocator. closes #11293 After this commit, stage2 passes all the parser tests.
2022-03-25Implement `type.bitSize` for unionsJohn Schmidt
2022-03-25type: fix onePossibleValue for auto numbered enumsVeikka Tuominen
2022-03-22Sema: rename isArrayLike to isArrayOrVectorAndrew Kelley
2022-03-22Sema: introduce a mechanism in Value to resolve typesAndrew Kelley
This commit adds a new optional argument to several Value methods which provides the ability to resolve types if it comes to it. This prevents having duplicated logic inside both Sema and Value. With this commit, the "struct contains slice of itself" test is passing by exploiting the new lazy_align Value Tag.
2022-03-22stage2: lazy `@alignOf`Andrew Kelley
Add a `target` parameter to every function that deals with Type and Value.
2022-03-21stage2: add way to print values with typesVeikka Tuominen
2022-03-18stage2: formatting an error_set value should print members, not declMitchell Hashimoto
2022-03-18stage2: improve `@typeName`Andrew Kelley
* make it always return a fully qualified name. stage1 is inconsistent about this. * AstGen: fix anon_name_strategy to correctly be `func` when anon type creation happens in the operand of the return expression. * Sema: implement type names for the "function" naming strategy. * Put "enum", "union", "opaque", or "struct" in place of "anon" when creating respective anonymous Decl names. * std.testing: add `expectStringStartsWith`. Didn't end up using it after all. Also this enables the real test runner for stage2 LLVM backend (sans wasm32) since it works now.
2022-03-16Merge pull request #11177 from Vexu/dbg_funcVeikka Tuominen
Add debug info for inlined calls
2022-03-16Merge pull request #11191 from Snektron/zig-gdb-improvementsAndrew Kelley
Zig gdb improvements
2022-03-16gdb: add printer for selfhosted TypeRobin Voetter
2022-03-16Type: implement ptrInfo for optional pointersVeikka Tuominen
2022-03-15stage2: comptime fields should not affect opv/comptime-onlyCody Tapscott
2022-03-14stage2: fixups for topolarity-comptime-memory-reinterp branchAndrew Kelley
* don't store `has_well_defined_layout` in memory. * remove struct `hasWellDefinedLayout` logic. it's just `layout != .Auto`. This means we only need one implementation, in Type. * fix some of the cases being wrong in `hasWellDefinedLayout`, such as optional pointers. * move `tag_ty_inferred` field into a position that makes it more obvious how the struct layout will be done. Also we don't have a compiler that intelligently moves fields around so this layout is better. * Sema: don't `resolveTypeLayout` in `zirCoerceResultPtr` unless necessary. * Rename `ComptimePtrLoadKit` `target` field to `pointee` to avoid confusion with `target`.
2022-03-14stage2 sema: Respect container_ty of parent ptrsCody Tapscott
The core change here is that we no longer blindly trust that parent pointers (.elem_ptr, .field_ptr, .eu_payload_ptr, .union_payload_ptr) were derived from the "true" type of the underlying decl. When types diverge, direct dereference fails and we are forced to bitcast, as usual. In order to maximize our chances to have a successful bitcast, this includes several changes to the dereference procedure: - `root` is now `parent` and is the largest Value containing the dereference target, with the condition that its layout and the byte offset of the target within are both well-defined. - If the target cannot be dereferenced directly, because the pointers were not derived from the true type of the underlying decl, then it is returned as null. - `beginComptimePtrDeref` now accepts an optional array_ty param, which is used to directly dereference an array from an elem_ptr, if necessary. This allows us to dereference array types without well-defined layouts (e.g. `[N]?u8`) at an offset The load_ty also allows us to correctly "over-read" an .elem_ptr to an array of [N]T, if necessary. This makes direct dereference work for array types even in the presence of an offset, which is necessary if the array has no well-defined layout (e.g. loading from `[6]?u8`)
2022-03-14stage2: Fix assertion in struct field offset when all fields are 0-sizeCody Tapscott
2022-03-14stage2: Add hasWellDefinedLayout() to type.zig and Sema.zigCody Tapscott
This follows the same strategy as sema.typeRequiresComptime() and type.comptimeOnly(): Two versions of the function, one which performs resolution just-in-time and another which asserts that resolution is complete. Thankfully, this doesn't cause very viral type resolution, since auto-layout structs and unions are very common and are known to not have a well-defined layout without resolving their fields.
2022-03-14stage2: rework Value storage of structs and arraysAndrew Kelley
Now they both use `Value.Tag.aggregate`. Additionally the LLVM backend now has implemented lowering of tuple values.
2022-03-14Sema: Type.abiSize asserts instead of using max with alignmentAndrew Kelley
ABI size is guaranteed to always be >= alignment.
2022-03-11Type.eql: check fn attributes before paramsAndrew Kelley
slightly better for cache locality
2022-03-11LLVM: fix debug info for pointers to voidAndrew Kelley
2022-03-11Sema: fix resolution of inferred error setsAndrew Kelley
Introduce `Module.ensureFuncBodyAnalyzed` and corresponding `Sema` function. This mirrors `ensureDeclAnalyzed` except also waits until the function body has been semantically analyzed, meaning that inferred error sets will have been populated. Resolving error sets can now emit a "unable to resolve inferred error set" error instead of producing an incorrect error set type. Resolving error sets now calls `ensureFuncBodyAnalyzed`. Closes #11046. `coerceInMemoryAllowedErrorSets` now does a lot more work to avoid resolving an inferred error set if possible. Same with `wrapErrorUnionSet`. Inferred error set types no longer check the `func` field to determine if they are equal. That was incorrect because an inline or comptime function call produces a unique error set which has the same `*Module.Fn` value for this field. Instead we use the `*Module.Fn.InferredErrorSet` pointers to test equality of inferred error sets.
2022-03-11stage2: passing threadlocal tests for x86_64-linuxAndrew Kelley
* use the real start code for LLVM backend with x86_64-linux - there is still a check for zig_backend after initializing the TLS area to skip some stuff. * introduce new AIR instructions and implement them for the LLVM backend. They are the same as `call` except with a modifier. - call_always_tail - call_never_tail - call_never_inline * LLVM backend calls hasRuntimeBitsIgnoringComptime in more places to avoid unnecessarily depending on comptimeOnly being resolved for some types. * LLVM backend: remove duplicate code for setting linkage and value name. The canonical place for this is in `updateDeclExports`. * LLVM backend: do some assembly template massaging to make `%%` rendered as `%`. More hacks will be needed to make inline assembly catch up with stage1.
2022-03-10stage2: improve Type.eql and Type.hash for error setsAndrew Kelley
* Reduce branching in Type.eql and Type.hash for error sets. * `Type.eql` uses element-wise bytes comparison since it can rely on the error sets being pre-sorted. * Avoid unnecessarily skipping tests that are passing.
2022-03-10stage2: error_set_merged type equalityMitchell Hashimoto
This implements type equality for error sets. This is done through element-wise error set comparison. Inferred error sets are always distinct types and other error sets are always sorted. See #11022.
2022-03-09Sema: implement pointer-to-tuple coercion to slice and structAndrew Kelley
2022-03-09Sema: fix crash with `@sizeOf` on unionsAndrew Kelley
2022-03-08Sema: implement coercion of tuples to structsAndrew Kelley