diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-08-03 22:34:22 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-08-03 22:34:22 -0700 |
| commit | 382d201781eb57d9e950ad07ce814adc5a68b329 (patch) | |
| tree | fd066174fcce78af51e925362c619d1ffd584eef /src/type.zig | |
| parent | 609b84611dcde382af5d9fbc2345ede468d31a6f (diff) | |
| download | zig-382d201781eb57d9e950ad07ce814adc5a68b329.tar.gz zig-382d201781eb57d9e950ad07ce814adc5a68b329.zip | |
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.
Diffstat (limited to 'src/type.zig')
| -rw-r--r-- | src/type.zig | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/src/type.zig b/src/type.zig index feb16fd47c..237614e372 100644 --- a/src/type.zig +++ b/src/type.zig @@ -1182,7 +1182,6 @@ pub const Type = extern union { .fn_void_no_args, .fn_naked_noreturn_no_args, .fn_ccc_void_no_args, - .function, .single_const_pointer_to_comptime_int, .const_slice_u8, .array_u8_sentinel_0, @@ -1207,6 +1206,8 @@ pub const Type = extern union { .anyframe_T, => true, + .function => !self.castTag(.function).?.data.is_generic, + .@"struct" => { // TODO introduce lazy value mechanism const struct_obj = self.castTag(.@"struct").?.data; |
