# stage2: basic generic functions are working · gitcafe/zig

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

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

Visibility: public

Requested revision: 382d201781eb57d9e950ad07ce814adc5a68b329

Requested commit: 382d201781eb57d9e950ad07ce814adc5a68b329

Commit: 382d201781eb57d9e950ad07ce814adc5a68b329

Tree: fd066174fcce78af51e925362c619d1ffd584eef

Author: Andrew Kelley

Committer: Andrew Kelley

## Message

```
stage2: basic generic functions are working

The general strategy is that Sema will pre-map comptime arguments into
the inst_map, and then re-run the block body that contains the `param`
and `func` instructions. This re-runs all the parameter type expressions
except with comptime values populated.

In Sema, param instructions are now handled specially: they detect
whether they are comptime-elided or not. If so, they skip putting a
value in the inst_map, since it is already pre-populated. If not, then
they append to the `fields` field of `Sema` for use with the `func`
instruction.

So when the block body is re-run, a new function is generated with
all the comptime arguments elided, and the new function type has only
runtime parameters in it. TODO: give the generated Decls better names
than "foo__anon_x".

The new function is then added to the work queue to have its body
analyzed and a runtime call AIR instruction to the new function is
emitted.

When the new function gets semantically analyzed, comptime parameters are
pre-mapped to the corresponding `comptime_args` values rather than
mapped to an `arg` AIR instruction. `comptime_args` is a new field that
`Fn` has which is a `TypedValue` for each parameter. This field is non-null
for generic function instantiations only. The values are the comptime
arguments. For non-comptime parameters, a sentinel value is used. This is
because we need to know the information of which parameters are
comptime-known.

Additionally:
 * AstGen: align and section expressions are evaluated in the scope that
   has comptime parameters in it.

There are still some TODO items left; see the BRANCH_TODO file.

```

## Parents

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

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