aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorMartin Wickham <spexguy070@gmail.com>2021-09-28 12:00:35 -0500
committerGitHub <noreply@github.com>2021-09-28 12:00:35 -0500
commit1cc5d4e758a95be373756e7c32f9bb46d21633c9 (patch)
tree9c142d3d009e1622f27a22c9228a6ff10b878721 /src/type.zig
parent60b6e74468570a124f602a62b6bd2da95ba8c17c (diff)
downloadzig-1cc5d4e758a95be373756e7c32f9bb46d21633c9.tar.gz
zig-1cc5d4e758a95be373756e7c32f9bb46d21633c9.zip
Stage 2: Support inst.func() syntax (#9827)
* Merge call zir instructions to make space for field_call * Fix bug with comptime known anytype args * Delete the param_type zir instruction * Move some passing tests to stage 2 * Implement a.b() function calls * Add field_call_bind support for call and field builtins
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig62
1 files changed, 60 insertions, 2 deletions
diff --git a/src/type.zig b/src/type.zig
index 781fe74d45..e13ede852a 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -138,6 +138,7 @@ pub const Type = extern union {
.type_info,
=> return .Union,
+ .bound_fn => unreachable,
.var_args_param => unreachable, // can be any type
}
}
@@ -771,6 +772,7 @@ pub const Type = extern union {
.type_info,
.@"anyframe",
.generic_poison,
+ .bound_fn,
=> unreachable,
.array_u8,
@@ -936,6 +938,7 @@ pub const Type = extern union {
.comptime_float,
.noreturn,
.var_args_param,
+ .bound_fn,
=> return writer.writeAll(@tagName(t)),
.enum_literal => return writer.writeAll("@Type(.EnumLiteral)"),
@@ -1248,6 +1251,7 @@ pub const Type = extern union {
.var_args_param => unreachable,
.inferred_alloc_mut => unreachable,
.inferred_alloc_const => unreachable,
+ .bound_fn => unreachable,
.array_u8,
.array_u8_sentinel_0,
@@ -1479,6 +1483,7 @@ pub const Type = extern union {
.empty_struct_literal,
.@"opaque",
.type_info,
+ .bound_fn,
=> false,
.inferred_alloc_const => unreachable,
@@ -1489,7 +1494,9 @@ pub const Type = extern union {
}
pub fn isNoReturn(self: Type) bool {
- const definitely_correct_result = self.zigTypeTag() == .NoReturn;
+ const definitely_correct_result =
+ self.tag_if_small_enough != .bound_fn and
+ self.zigTypeTag() == .NoReturn;
const fast_result = self.tag_if_small_enough == Tag.noreturn;
assert(fast_result == definitely_correct_result);
return fast_result;
@@ -1736,6 +1743,7 @@ pub const Type = extern union {
.@"opaque",
.var_args_param,
.type_info,
+ .bound_fn,
=> unreachable,
.generic_poison => unreachable,
@@ -1768,6 +1776,7 @@ pub const Type = extern union {
.var_args_param => unreachable,
.generic_poison => unreachable,
.type_info => unreachable,
+ .bound_fn => unreachable,
.@"struct" => {
const s = self.castTag(.@"struct").?.data;
@@ -1951,6 +1960,7 @@ pub const Type = extern union {
.@"opaque" => unreachable,
.var_args_param => unreachable,
.generic_poison => unreachable,
+ .bound_fn => unreachable,
.@"struct" => {
@panic("TODO bitSize struct");
@@ -2353,6 +2363,51 @@ pub const Type = extern union {
}
}
+ /// Returns if type can be used for a runtime variable
+ pub fn isValidVarType(self: Type, is_extern: bool) bool {
+ var ty = self;
+ while (true) switch (ty.zigTypeTag()) {
+ .Bool,
+ .Int,
+ .Float,
+ .ErrorSet,
+ .Enum,
+ .Frame,
+ .AnyFrame,
+ => return true,
+
+ .Opaque => return is_extern,
+ .BoundFn,
+ .ComptimeFloat,
+ .ComptimeInt,
+ .EnumLiteral,
+ .NoReturn,
+ .Type,
+ .Void,
+ .Undefined,
+ .Null,
+ => return false,
+
+ .Optional => {
+ var buf: Payload.ElemType = undefined;
+ return ty.optionalChild(&buf).isValidVarType(is_extern);
+ },
+ .Pointer, .Array, .Vector => ty = ty.elemType(),
+ .ErrorUnion => ty = ty.errorUnionPayload(),
+
+ .Fn => @panic("TODO fn isValidVarType"),
+ .Struct => {
+ // TODO this is not always correct; introduce lazy value mechanism
+ // and here we need to force a resolve of "type requires comptime".
+ return true;
+ },
+ .Union => @panic("TODO union isValidVarType"),
+ };
+ }
+
+ /// For *[N]T, returns [N]T.
+ /// For *T, returns T.
+ /// For [*]T, returns T.
pub fn childType(ty: Type) Type {
return switch (ty.tag()) {
.vector => ty.castTag(.vector).?.data.elem_type,
@@ -2934,6 +2989,7 @@ pub const Type = extern union {
.single_const_pointer,
.single_mut_pointer,
.pointer,
+ .bound_fn,
=> return null,
.@"struct" => {
@@ -3480,6 +3536,7 @@ pub const Type = extern union {
inferred_alloc_mut,
/// Same as `inferred_alloc_mut` but the local is `var` not `const`.
inferred_alloc_const, // See last_no_payload_tag below.
+ bound_fn,
// After this, the tag requires a payload.
array_u8,
@@ -3518,7 +3575,7 @@ pub const Type = extern union {
enum_full,
enum_nonexhaustive,
- pub const last_no_payload_tag = Tag.inferred_alloc_const;
+ pub const last_no_payload_tag = Tag.bound_fn;
pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1;
pub fn Type(comptime t: Tag) type {
@@ -3585,6 +3642,7 @@ pub const Type = extern union {
.extern_options,
.type_info,
.@"anyframe",
+ .bound_fn,
=> @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
.array_u8,