# stage2: support nested structs and arrays and sret · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/6d6cf598475ab8d2c3259002655ba04f1d056b2e)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 6d6cf598475ab8d2c3259002655ba04f1d056b2e

Requested commit: 6d6cf598475ab8d2c3259002655ba04f1d056b2e

Commit: 6d6cf598475ab8d2c3259002655ba04f1d056b2e

Tree: daf38d2c3c68de0411d9106668bde92cc7e0651b

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: support nested structs and arrays and sret

 * Add AIR instructions: ret_ptr, ret_load
   - This allows Sema to be blissfully unaware of the backend's decision
     to implement by-val/by-ref semantics for struct/union/array types.
     Backends can lower these simply as alloc, load, ret instructions,
     or they can take advantage of them to use a result pointer.
 * Add AIR instruction: array_elem_val
   - Allows for better codegen for `Sema.elemVal`.
 * Implement calculation of ABI alignment and ABI size for unions.
 * Before appending the following AIR instructions to a block,
   resolveTypeLayout is called on the type:
   - call - return type
   - ret - return type
   - store_ptr - elem type
 * Sema: fix memory leak in `zirArrayInit` and other cleanups to this
   function.
 * x86_64: implement the full x86_64 C ABI according to the spec
 * Type: implement `intInfo` for error sets.
 * Type: implement `intTagType` for tagged unions.

The Zig type tag `Fn` is now used exclusively for function bodies.
Function pointers are modeled as `*const T` where `T` is a `Fn` type.
 * The `call` AIR instruction now allows a function pointer operand as
   well as a function operand.
 * Sema now has a coercion from function body to function pointer.
 * Function type syntax, e.g. `fn()void`, now returns zig tag type of
   Pointer with child Fn, rather than Fn directly.
   - I think this should probably be reverted. Will discuss the lang
     specs before doing this. Idea being that function pointers would
     need to be specified as `*const fn()void` rather than `fn() void`.

LLVM backend:
 * Enable calling the panic handler (previously this just
   emitted `@breakpoint()` since the backend could not handle the panic
   function).
 * Implement sret
 * Introduce `isByRef` and implement it for structs and arrays. Types
   that are `isByRef` are now passed as pointers to functions, and e.g.
   `elem_val` will return a pointer instead of doing a load.
 * Move the function type creating code from `resolveLlvmFunction` to
   `llvmType` where it belongs; now there is only 1 instance of this
   logic instead of two.
 * Add the `nonnull` attribute to non-optional pointer parameters.
 * Fix `resolveGlobalDecl` not using fully-qualified names and not using
   the `decl_map`.
 * Implement `genTypedValue` for pointer-like optionals.
 * Fix memory leak when lowering `block` instruction and OOM occurs.
 * Implement volatile checks where relevant.

```

## Parents

- [f42725c39bbbe5db13c1a1706db3f31aa0549307](https://git.cafe/gitcafe/zig/commit/f42725c39bbbe5db13c1a1706db3f31aa0549307?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/6d6cf598475ab8d2c3259002655ba04f1d056b2e?format=markdown)
