Support taking extern pointers at comptime

This commit makes it possible to obtain pointers to `extern` variables
at comptime.

 - `ir_get_var_ptr` employs several checks to determine if the given
   variable is eligible for obtaining its pointer at comptime. This
   commit alters these checks to consider `extern` variables, which have
   runtime values, as eligible.

 - After this change, it's now possible for `render_const_val` to be
   called for `extern` variables. This commit modifies
   `render_const_val` to suppress the value generation for `extern`
   variables.

 - `do_code_gen` now creates `ZigValue::llvm_global` of `extern`
   variables before iterating through module-level variables so that
   other module-level variables can refer to them.

This solution is incomplete since there are several cases still
failing:

 - `global_var.array[n..m]`
 - `&global_var.array[i]`
 - `&global_var.inner_struct.value`
 - `&global_array[i]`

Closes #5349
This commit is contained in:
yvt
2020-05-15 18:28:58 +09:00
committed by Andrew Kelley
parent 3b26e50863
commit d3ebd42865
8 changed files with 284 additions and 47 deletions

View File

@@ -7637,4 +7637,26 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
, &[_][]const u8{
"tmp.zig:4:9: error: expected type '*c_void', found '?*c_void'",
});
cases.add("pointer to a local runtime `var` is not constant",
\\export fn get_ptr() *const u32 {
\\ var local_var: u32 = 42;
\\ return struct {
\\ const ptr = &local_var;
\\ }.ptr;
\\}
, &[_][]const u8{
":4:21: error: cannot store runtime value in compile time variable",
});
cases.add("pointer to a local runtime `const` is not constant",
\\export fn get_ptr(x: u32) *const u32 {
\\ const local_var: u32 = x;
\\ return struct {
\\ const ptr = &local_var;
\\ }.ptr;
\\}
, &[_][]const u8{
":4:21: error: cannot store runtime value in compile time variable",
});
}