# stage2: type declarations ZIR encode AnonNameStrategy · gitcafe/zig

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

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

Visibility: public

Requested revision: b9a099e83cf11629554c68c7606836065133f56b

Requested commit: b9a099e83cf11629554c68c7606836065133f56b

Commit: b9a099e83cf11629554c68c7606836065133f56b

Tree: 4673139264605fba8295c97024035d1dc0fb5cee

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

````
stage2: type declarations ZIR encode AnonNameStrategy

which can be either parent, func, or anon. Here's the enum reproduced in
the commit message for convenience:

```zig
pub const NameStrategy = enum(u2) {
    /// Use the same name as the parent declaration name.
    /// e.g. `const Foo = struct {...};`.
    parent,
    /// Use the name of the currently executing comptime function call,
    /// with the current parameters. e.g. `ArrayList(i32)`.
    func,
    /// Create an anonymous name for this declaration.
    /// Like this: "ParentDeclName_struct_69"
    anon,
};
```

With this information in the ZIR, a future commit can improve the
names of structs, unions, enums, and opaques.

In order to accomplish this, the following ZIR instruction forms were
removed and replaced with Extended op codes:

 * struct_decl
 * struct_decl_packed
 * struct_decl_extern
 * union_decl
 * union_decl_packed
 * union_decl_extern
 * enum_decl
 * enum_decl_nonexhaustive

By being extended opcodes, one more u32 is needed, however we more than
make up for it by repurposing the 16 "small" bits to provide shorter
encodings for when decls_len == 0, fields_len == 0, a source node is not
provided, etc. There tends to be no downside, and in fact sometimes
upsides, to using an extended op code when there is a need for flag
bits, which is the case for all three of these. Likewise, the container
layout can be encoded in these bits rather than into the opcode.

The following 4 ZIR instructions were added, netting a total of 4 freed
up ZIR enum tags for future use:

 * opaque_decl_anon
 * opaque_decl_func
 * error_set_decl_anon
 * error_set_decl_func

This is so that opaques and error sets can have the same name hint as
structs, enums, and unions.

`std.builtin.ContainerLayout` gets an explicit integer tag type so that
it can be used inside packed structs.

This commit also makes `Module.Namespace` use a separate set for
anonymous decls, thus allowing anonymous decls to share the same
`Decl.name` as their owner `Decl` objects.

````

## Parents

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

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