mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-04-27 19:09:47 +03:00
Merge branch 'master' into pointer-reform
This commit is contained in:
+30
-28
@@ -416,8 +416,8 @@ set(ZIG_CPP_SOURCES
|
||||
set(ZIG_STD_FILES
|
||||
"array_list.zig"
|
||||
"atomic/index.zig"
|
||||
"atomic/stack.zig"
|
||||
"atomic/queue.zig"
|
||||
"atomic/stack.zig"
|
||||
"base64.zig"
|
||||
"buf_map.zig"
|
||||
"buf_set.zig"
|
||||
@@ -427,13 +427,13 @@ set(ZIG_STD_FILES
|
||||
"c/index.zig"
|
||||
"c/linux.zig"
|
||||
"c/windows.zig"
|
||||
"crypto/blake2.zig"
|
||||
"crypto/hmac.zig"
|
||||
"crypto/index.zig"
|
||||
"crypto/md5.zig"
|
||||
"crypto/sha1.zig"
|
||||
"crypto/sha2.zig"
|
||||
"crypto/sha3.zig"
|
||||
"crypto/blake2.zig"
|
||||
"crypto/hmac.zig"
|
||||
"cstr.zig"
|
||||
"debug/failing_allocator.zig"
|
||||
"debug/index.zig"
|
||||
@@ -445,15 +445,16 @@ set(ZIG_STD_FILES
|
||||
"fmt/errol/index.zig"
|
||||
"fmt/errol/lookup.zig"
|
||||
"fmt/index.zig"
|
||||
"hash_map.zig"
|
||||
"hash/index.zig"
|
||||
"hash/adler.zig"
|
||||
"hash/crc.zig"
|
||||
"hash/fnv.zig"
|
||||
"hash/index.zig"
|
||||
"hash/siphash.zig"
|
||||
"hash_map.zig"
|
||||
"heap.zig"
|
||||
"index.zig"
|
||||
"io.zig"
|
||||
"json.zig"
|
||||
"linked_list.zig"
|
||||
"macho.zig"
|
||||
"math/acos.zig"
|
||||
@@ -465,6 +466,28 @@ set(ZIG_STD_FILES
|
||||
"math/atanh.zig"
|
||||
"math/cbrt.zig"
|
||||
"math/ceil.zig"
|
||||
"math/complex/abs.zig"
|
||||
"math/complex/acos.zig"
|
||||
"math/complex/acosh.zig"
|
||||
"math/complex/arg.zig"
|
||||
"math/complex/asin.zig"
|
||||
"math/complex/asinh.zig"
|
||||
"math/complex/atan.zig"
|
||||
"math/complex/atanh.zig"
|
||||
"math/complex/conj.zig"
|
||||
"math/complex/cos.zig"
|
||||
"math/complex/cosh.zig"
|
||||
"math/complex/exp.zig"
|
||||
"math/complex/index.zig"
|
||||
"math/complex/ldexp.zig"
|
||||
"math/complex/log.zig"
|
||||
"math/complex/pow.zig"
|
||||
"math/complex/proj.zig"
|
||||
"math/complex/sin.zig"
|
||||
"math/complex/sinh.zig"
|
||||
"math/complex/sqrt.zig"
|
||||
"math/complex/tan.zig"
|
||||
"math/complex/tanh.zig"
|
||||
"math/copysign.zig"
|
||||
"math/cos.zig"
|
||||
"math/cosh.zig"
|
||||
@@ -501,33 +524,12 @@ set(ZIG_STD_FILES
|
||||
"math/tan.zig"
|
||||
"math/tanh.zig"
|
||||
"math/trunc.zig"
|
||||
"math/complex/abs.zig"
|
||||
"math/complex/acosh.zig"
|
||||
"math/complex/acos.zig"
|
||||
"math/complex/arg.zig"
|
||||
"math/complex/asinh.zig"
|
||||
"math/complex/asin.zig"
|
||||
"math/complex/atanh.zig"
|
||||
"math/complex/atan.zig"
|
||||
"math/complex/conj.zig"
|
||||
"math/complex/cosh.zig"
|
||||
"math/complex/cos.zig"
|
||||
"math/complex/exp.zig"
|
||||
"math/complex/index.zig"
|
||||
"math/complex/ldexp.zig"
|
||||
"math/complex/log.zig"
|
||||
"math/complex/pow.zig"
|
||||
"math/complex/proj.zig"
|
||||
"math/complex/sinh.zig"
|
||||
"math/complex/sin.zig"
|
||||
"math/complex/sqrt.zig"
|
||||
"math/complex/tanh.zig"
|
||||
"math/complex/tan.zig"
|
||||
"mem.zig"
|
||||
"net.zig"
|
||||
"os/child_process.zig"
|
||||
"os/darwin.zig"
|
||||
"os/darwin_errno.zig"
|
||||
"os/epoch.zig"
|
||||
"os/file.zig"
|
||||
"os/get_user_id.zig"
|
||||
"os/index.zig"
|
||||
@@ -537,13 +539,13 @@ set(ZIG_STD_FILES
|
||||
"os/linux/x86_64.zig"
|
||||
"os/path.zig"
|
||||
"os/time.zig"
|
||||
"os/epoch.zig"
|
||||
"os/windows/error.zig"
|
||||
"os/windows/index.zig"
|
||||
"os/windows/util.zig"
|
||||
"os/zen.zig"
|
||||
"rand/index.zig"
|
||||
"rand/ziggurat.zig"
|
||||
"segmented_list.zig"
|
||||
"sort.zig"
|
||||
"special/bootstrap.zig"
|
||||
"special/bootstrap_lib.zig"
|
||||
|
||||
+388
-5
@@ -4809,6 +4809,182 @@ pub const TypeId = enum {
|
||||
BoundFn,
|
||||
ArgTuple,
|
||||
Opaque,
|
||||
};
|
||||
{#code_end#}
|
||||
{#header_close#}
|
||||
{#header_open|@typeInfo#}
|
||||
<pre><code class="zig">@typeInfo(comptime T: type) -> @import("builtin").TypeInfo</code></pre>
|
||||
<p>
|
||||
Returns information on the type. Returns a value of the following union:
|
||||
</p>
|
||||
{#code_begin|syntax#}
|
||||
pub const TypeInfo = union(TypeId) {
|
||||
Type: void,
|
||||
Void: void,
|
||||
Bool: void,
|
||||
NoReturn: void,
|
||||
Int: Int,
|
||||
Float: Float,
|
||||
Pointer: Pointer,
|
||||
Array: Array,
|
||||
Struct: Struct,
|
||||
FloatLiteral: void,
|
||||
IntLiteral: void,
|
||||
UndefinedLiteral: void,
|
||||
NullLiteral: void,
|
||||
Nullable: Nullable,
|
||||
ErrorUnion: ErrorUnion,
|
||||
ErrorSet: ErrorSet,
|
||||
Enum: Enum,
|
||||
Union: Union,
|
||||
Fn: Fn,
|
||||
Namespace: void,
|
||||
Block: void,
|
||||
BoundFn: Fn,
|
||||
ArgTuple: void,
|
||||
Opaque: void,
|
||||
Promise: Promise,
|
||||
|
||||
|
||||
pub const Int = struct {
|
||||
is_signed: bool,
|
||||
bits: u8,
|
||||
};
|
||||
|
||||
pub const Float = struct {
|
||||
bits: u8,
|
||||
};
|
||||
|
||||
pub const Pointer = struct {
|
||||
is_const: bool,
|
||||
is_volatile: bool,
|
||||
alignment: u32,
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const Array = struct {
|
||||
len: usize,
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const ContainerLayout = enum {
|
||||
Auto,
|
||||
Extern,
|
||||
Packed,
|
||||
};
|
||||
|
||||
pub const StructField = struct {
|
||||
name: []const u8,
|
||||
offset: ?usize,
|
||||
field_type: type,
|
||||
};
|
||||
|
||||
pub const Struct = struct {
|
||||
layout: ContainerLayout,
|
||||
fields: []StructField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const Nullable = struct {
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const ErrorUnion = struct {
|
||||
error_set: type,
|
||||
payload: type,
|
||||
};
|
||||
|
||||
pub const Error = struct {
|
||||
name: []const u8,
|
||||
value: usize,
|
||||
};
|
||||
|
||||
pub const ErrorSet = struct {
|
||||
errors: []Error,
|
||||
};
|
||||
|
||||
pub const EnumField = struct {
|
||||
name: []const u8,
|
||||
value: usize,
|
||||
};
|
||||
|
||||
pub const Enum = struct {
|
||||
layout: ContainerLayout,
|
||||
tag_type: type,
|
||||
fields: []EnumField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const UnionField = struct {
|
||||
name: []const u8,
|
||||
enum_field: ?EnumField,
|
||||
field_type: type,
|
||||
};
|
||||
|
||||
pub const Union = struct {
|
||||
layout: ContainerLayout,
|
||||
tag_type: type,
|
||||
fields: []UnionField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const CallingConvention = enum {
|
||||
Unspecified,
|
||||
C,
|
||||
Cold,
|
||||
Naked,
|
||||
Stdcall,
|
||||
Async,
|
||||
};
|
||||
|
||||
pub const FnArg = struct {
|
||||
is_generic: bool,
|
||||
is_noalias: bool,
|
||||
arg_type: type,
|
||||
};
|
||||
|
||||
pub const Fn = struct {
|
||||
calling_convention: CallingConvention,
|
||||
is_generic: bool,
|
||||
is_var_args: bool,
|
||||
return_type: type,
|
||||
async_allocator_type: type,
|
||||
args: []FnArg,
|
||||
};
|
||||
|
||||
pub const Promise = struct {
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const Definition = struct {
|
||||
name: []const u8,
|
||||
is_pub: bool,
|
||||
data: Data,
|
||||
|
||||
pub const Data = union(enum) {
|
||||
Type: type,
|
||||
Var: type,
|
||||
Fn: FnDef,
|
||||
|
||||
pub const FnDef = struct {
|
||||
fn_type: type,
|
||||
inline_type: Inline,
|
||||
calling_convention: CallingConvention,
|
||||
is_var_args: bool,
|
||||
is_extern: bool,
|
||||
is_export: bool,
|
||||
lib_name: ?[]const u8,
|
||||
return_type: type,
|
||||
arg_names: [][] const u8,
|
||||
|
||||
pub const Inline = enum {
|
||||
Auto,
|
||||
Always,
|
||||
Never,
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
{#code_end#}
|
||||
{#header_close#}
|
||||
@@ -5226,7 +5402,6 @@ pub const Os = enum {
|
||||
rtems,
|
||||
nacl,
|
||||
cnk,
|
||||
bitrig,
|
||||
aix,
|
||||
cuda,
|
||||
nvcl,
|
||||
@@ -5237,10 +5412,12 @@ pub const Os = enum {
|
||||
watchos,
|
||||
mesa3d,
|
||||
contiki,
|
||||
amdpal,
|
||||
zen,
|
||||
};
|
||||
|
||||
pub const Arch = enum {
|
||||
armv8_3a,
|
||||
armv8_2a,
|
||||
armv8_1a,
|
||||
armv8,
|
||||
@@ -5260,9 +5437,29 @@ pub const Arch = enum {
|
||||
armv5,
|
||||
armv5te,
|
||||
armv4t,
|
||||
armeb,
|
||||
armebv8_3a,
|
||||
armebv8_2a,
|
||||
armebv8_1a,
|
||||
armebv8,
|
||||
armebv8r,
|
||||
armebv8m_baseline,
|
||||
armebv8m_mainline,
|
||||
armebv7,
|
||||
armebv7em,
|
||||
armebv7m,
|
||||
armebv7s,
|
||||
armebv7k,
|
||||
armebv7ve,
|
||||
armebv6,
|
||||
armebv6m,
|
||||
armebv6k,
|
||||
armebv6t2,
|
||||
armebv5,
|
||||
armebv5te,
|
||||
armebv4t,
|
||||
aarch64,
|
||||
aarch64_be,
|
||||
arc,
|
||||
avr,
|
||||
bpfel,
|
||||
bpfeb,
|
||||
@@ -5315,6 +5512,7 @@ pub const Arch = enum {
|
||||
pub const Environ = enum {
|
||||
unknown,
|
||||
gnu,
|
||||
gnuabin32,
|
||||
gnuabi64,
|
||||
gnueabi,
|
||||
gnueabihf,
|
||||
@@ -5332,6 +5530,7 @@ pub const Environ = enum {
|
||||
amdopencl,
|
||||
coreclr,
|
||||
opencl,
|
||||
simulator,
|
||||
};
|
||||
|
||||
pub const ObjectFormat = enum {
|
||||
@@ -5358,10 +5557,23 @@ pub const AtomicOrder = enum {
|
||||
SeqCst,
|
||||
};
|
||||
|
||||
pub const AtomicRmwOp = enum {
|
||||
Xchg,
|
||||
Add,
|
||||
Sub,
|
||||
And,
|
||||
Nand,
|
||||
Or,
|
||||
Xor,
|
||||
Max,
|
||||
Min,
|
||||
};
|
||||
|
||||
pub const Mode = enum {
|
||||
Debug,
|
||||
ReleaseSafe,
|
||||
ReleaseFast,
|
||||
ReleaseSmall,
|
||||
};
|
||||
|
||||
pub const TypeId = enum {
|
||||
@@ -5380,7 +5592,7 @@ pub const TypeId = enum {
|
||||
NullLiteral,
|
||||
Nullable,
|
||||
ErrorUnion,
|
||||
Error,
|
||||
ErrorSet,
|
||||
Enum,
|
||||
Union,
|
||||
Fn,
|
||||
@@ -5389,6 +5601,176 @@ pub const TypeId = enum {
|
||||
BoundFn,
|
||||
ArgTuple,
|
||||
Opaque,
|
||||
Promise,
|
||||
};
|
||||
|
||||
pub const TypeInfo = union(TypeId) {
|
||||
Type: void,
|
||||
Void: void,
|
||||
Bool: void,
|
||||
NoReturn: void,
|
||||
Int: Int,
|
||||
Float: Float,
|
||||
Pointer: Pointer,
|
||||
Array: Array,
|
||||
Struct: Struct,
|
||||
FloatLiteral: void,
|
||||
IntLiteral: void,
|
||||
UndefinedLiteral: void,
|
||||
NullLiteral: void,
|
||||
Nullable: Nullable,
|
||||
ErrorUnion: ErrorUnion,
|
||||
ErrorSet: ErrorSet,
|
||||
Enum: Enum,
|
||||
Union: Union,
|
||||
Fn: Fn,
|
||||
Namespace: void,
|
||||
Block: void,
|
||||
BoundFn: Fn,
|
||||
ArgTuple: void,
|
||||
Opaque: void,
|
||||
Promise: Promise,
|
||||
|
||||
|
||||
pub const Int = struct {
|
||||
is_signed: bool,
|
||||
bits: u8,
|
||||
};
|
||||
|
||||
pub const Float = struct {
|
||||
bits: u8,
|
||||
};
|
||||
|
||||
pub const Pointer = struct {
|
||||
is_const: bool,
|
||||
is_volatile: bool,
|
||||
alignment: u32,
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const Array = struct {
|
||||
len: usize,
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const ContainerLayout = enum {
|
||||
Auto,
|
||||
Extern,
|
||||
Packed,
|
||||
};
|
||||
|
||||
pub const StructField = struct {
|
||||
name: []const u8,
|
||||
offset: ?usize,
|
||||
field_type: type,
|
||||
};
|
||||
|
||||
pub const Struct = struct {
|
||||
layout: ContainerLayout,
|
||||
fields: []StructField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const Nullable = struct {
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const ErrorUnion = struct {
|
||||
error_set: type,
|
||||
payload: type,
|
||||
};
|
||||
|
||||
pub const Error = struct {
|
||||
name: []const u8,
|
||||
value: usize,
|
||||
};
|
||||
|
||||
pub const ErrorSet = struct {
|
||||
errors: []Error,
|
||||
};
|
||||
|
||||
pub const EnumField = struct {
|
||||
name: []const u8,
|
||||
value: usize,
|
||||
};
|
||||
|
||||
pub const Enum = struct {
|
||||
layout: ContainerLayout,
|
||||
tag_type: type,
|
||||
fields: []EnumField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const UnionField = struct {
|
||||
name: []const u8,
|
||||
enum_field: ?EnumField,
|
||||
field_type: type,
|
||||
};
|
||||
|
||||
pub const Union = struct {
|
||||
layout: ContainerLayout,
|
||||
tag_type: type,
|
||||
fields: []UnionField,
|
||||
defs: []Definition,
|
||||
};
|
||||
|
||||
pub const CallingConvention = enum {
|
||||
Unspecified,
|
||||
C,
|
||||
Cold,
|
||||
Naked,
|
||||
Stdcall,
|
||||
Async,
|
||||
};
|
||||
|
||||
pub const FnArg = struct {
|
||||
is_generic: bool,
|
||||
is_noalias: bool,
|
||||
arg_type: type,
|
||||
};
|
||||
|
||||
pub const Fn = struct {
|
||||
calling_convention: CallingConvention,
|
||||
is_generic: bool,
|
||||
is_var_args: bool,
|
||||
return_type: type,
|
||||
async_allocator_type: type,
|
||||
args: []FnArg,
|
||||
};
|
||||
|
||||
pub const Promise = struct {
|
||||
child: type,
|
||||
};
|
||||
|
||||
pub const Definition = struct {
|
||||
name: []const u8,
|
||||
is_pub: bool,
|
||||
data: Data,
|
||||
|
||||
pub const Data = union(enum) {
|
||||
Type: type,
|
||||
Var: type,
|
||||
Fn: FnDef,
|
||||
|
||||
pub const FnDef = struct {
|
||||
fn_type: type,
|
||||
inline_type: Inline,
|
||||
calling_convention: CallingConvention,
|
||||
is_var_args: bool,
|
||||
is_extern: bool,
|
||||
is_export: bool,
|
||||
lib_name: ?[]const u8,
|
||||
return_type: type,
|
||||
arg_names: [][] const u8,
|
||||
|
||||
pub const Inline = enum {
|
||||
Auto,
|
||||
Always,
|
||||
Never,
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
pub const FloatMode = enum {
|
||||
@@ -5402,7 +5784,7 @@ pub const Endian = enum {
|
||||
};
|
||||
|
||||
pub const endian = Endian.Little;
|
||||
pub const is_test = false;
|
||||
pub const is_test = true;
|
||||
pub const os = Os.linux;
|
||||
pub const arch = Arch.x86_64;
|
||||
pub const environ = Environ.gnu;
|
||||
@@ -5410,6 +5792,7 @@ pub const object_format = ObjectFormat.elf;
|
||||
pub const mode = Mode.Debug;
|
||||
pub const link_libc = false;
|
||||
pub const have_error_return_tracing = true;
|
||||
pub const __zig_test_fn_slice = {}; // overwritten later
|
||||
{#code_end#}
|
||||
{#see_also|Build Mode#}
|
||||
{#header_close#}
|
||||
@@ -6070,7 +6453,7 @@ hljs.registerLanguage("zig", function(t) {
|
||||
a = t.IR + "\\s*\\(",
|
||||
c = {
|
||||
keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong",
|
||||
built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field",
|
||||
built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo",
|
||||
literal: "true false null undefined"
|
||||
},
|
||||
n = [e, t.CLCM, t.CBCM, s, r];
|
||||
|
||||
@@ -1298,6 +1298,7 @@ enum BuiltinFnId {
|
||||
BuiltinFnIdMemberType,
|
||||
BuiltinFnIdMemberName,
|
||||
BuiltinFnIdField,
|
||||
BuiltinFnIdTypeInfo,
|
||||
BuiltinFnIdTypeof,
|
||||
BuiltinFnIdAddWithOverflow,
|
||||
BuiltinFnIdSubWithOverflow,
|
||||
@@ -1511,6 +1512,7 @@ struct CodeGen {
|
||||
HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> exported_symbol_names;
|
||||
HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
|
||||
HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> string_literals_table;
|
||||
HashMap<const TypeTableEntry *, ConstExprValue *, type_ptr_hash, type_ptr_eql> type_info_cache;
|
||||
|
||||
|
||||
ZigList<ImportTableEntry *> import_queue;
|
||||
@@ -2042,6 +2044,7 @@ enum IrInstructionId {
|
||||
IrInstructionIdTagType,
|
||||
IrInstructionIdFieldParentPtr,
|
||||
IrInstructionIdOffsetOf,
|
||||
IrInstructionIdTypeInfo,
|
||||
IrInstructionIdTypeId,
|
||||
IrInstructionIdSetEvalBranchQuota,
|
||||
IrInstructionIdPtrTypeOf,
|
||||
@@ -2863,6 +2866,12 @@ struct IrInstructionOffsetOf {
|
||||
IrInstruction *field_name;
|
||||
};
|
||||
|
||||
struct IrInstructionTypeInfo {
|
||||
IrInstruction base;
|
||||
|
||||
IrInstruction *type_value;
|
||||
};
|
||||
|
||||
struct IrInstructionTypeId {
|
||||
IrInstruction base;
|
||||
|
||||
|
||||
+11
-3
@@ -2325,8 +2325,14 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) {
|
||||
HashMap<BigInt, AstNode *, bigint_hash, bigint_eql> occupied_tag_values = {};
|
||||
occupied_tag_values.init(field_count);
|
||||
|
||||
TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
|
||||
TypeTableEntry *tag_int_type;
|
||||
if (enum_type->data.enumeration.layout == ContainerLayoutExtern) {
|
||||
tag_int_type = get_c_int_type(g, CIntTypeInt);
|
||||
} else {
|
||||
tag_int_type = get_smallest_unsigned_int_type(g, field_count - 1);
|
||||
}
|
||||
|
||||
// TODO: Are extern enums allowed to have an init_arg_expr?
|
||||
if (decl_node->data.container_decl.init_arg_expr != nullptr) {
|
||||
TypeTableEntry *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
|
||||
if (type_is_invalid(wanted_tag_int_type)) {
|
||||
@@ -5926,8 +5932,8 @@ size_t type_id_len() {
|
||||
return array_length(all_type_ids);
|
||||
}
|
||||
|
||||
size_t type_id_index(TypeTableEntryId id) {
|
||||
switch (id) {
|
||||
size_t type_id_index(TypeTableEntry *entry) {
|
||||
switch (entry->id) {
|
||||
case TypeTableEntryIdInvalid:
|
||||
zig_unreachable();
|
||||
case TypeTableEntryIdMetaType:
|
||||
@@ -5947,6 +5953,8 @@ size_t type_id_index(TypeTableEntryId id) {
|
||||
case TypeTableEntryIdArray:
|
||||
return 7;
|
||||
case TypeTableEntryIdStruct:
|
||||
if (entry->data.structure.is_slice)
|
||||
return 25;
|
||||
return 8;
|
||||
case TypeTableEntryIdNumLitFloat:
|
||||
return 9;
|
||||
|
||||
+1
-1
@@ -174,7 +174,7 @@ void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
|
||||
const char *type_id_name(TypeTableEntryId id);
|
||||
TypeTableEntryId type_id_at_index(size_t index);
|
||||
size_t type_id_len();
|
||||
size_t type_id_index(TypeTableEntryId id);
|
||||
size_t type_id_index(TypeTableEntry *entry);
|
||||
TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
|
||||
bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
|
||||
LinkLib *create_link_lib(Buf *name);
|
||||
|
||||
+1
-1
@@ -736,7 +736,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
|
||||
render_node_grouped(ar, field_node->data.struct_field.type);
|
||||
}
|
||||
if (field_node->data.struct_field.value != nullptr) {
|
||||
fprintf(ar->f, "= ");
|
||||
fprintf(ar->f, " = ");
|
||||
render_node_grouped(ar, field_node->data.struct_field.value);
|
||||
}
|
||||
fprintf(ar->f, ",\n");
|
||||
|
||||
+4
-6
@@ -1259,12 +1259,11 @@ void bigint_and(BigInt *dest, const BigInt *op1, const BigInt *op2) {
|
||||
bigint_normalize(dest);
|
||||
return;
|
||||
}
|
||||
// TODO this code path is untested
|
||||
uint64_t first_digit = dest->data.digit;
|
||||
|
||||
dest->digit_count = max(op1->digit_count, op2->digit_count);
|
||||
dest->data.digits = allocate_nonzero<uint64_t>(dest->digit_count);
|
||||
dest->data.digits[0] = first_digit;
|
||||
size_t i = 1;
|
||||
|
||||
size_t i = 0;
|
||||
for (; i < op1->digit_count && i < op2->digit_count; i += 1) {
|
||||
dest->data.digits[i] = op1_digits[i] & op2_digits[i];
|
||||
}
|
||||
@@ -1412,7 +1411,6 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO this code path is untested
|
||||
size_t digit_shift_count = shift_amt / 64;
|
||||
size_t leftover_shift_count = shift_amt % 64;
|
||||
|
||||
@@ -1427,7 +1425,7 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {
|
||||
uint64_t digit = op1_digits[op_digit_index];
|
||||
size_t dest_digit_index = op_digit_index - digit_shift_count;
|
||||
dest->data.digits[dest_digit_index] = carry | (digit >> leftover_shift_count);
|
||||
carry = (0xffffffffffffffffULL << leftover_shift_count) & digit;
|
||||
carry = digit << leftover_shift_count;
|
||||
|
||||
if (dest_digit_index == 0) { break; }
|
||||
op_digit_index -= 1;
|
||||
|
||||
+191
@@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
|
||||
g->exported_symbol_names.init(8);
|
||||
g->external_prototypes.init(8);
|
||||
g->string_literals_table.init(16);
|
||||
g->type_info_cache.init(32);
|
||||
g->is_test_build = false;
|
||||
g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
|
||||
buf_resize(&g->global_asm, 0);
|
||||
@@ -4502,6 +4503,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
|
||||
case IrInstructionIdDeclRef:
|
||||
case IrInstructionIdSwitchVar:
|
||||
case IrInstructionIdOffsetOf:
|
||||
case IrInstructionIdTypeInfo:
|
||||
case IrInstructionIdTypeId:
|
||||
case IrInstructionIdSetEvalBranchQuota:
|
||||
case IrInstructionIdPtrTypeOf:
|
||||
@@ -6125,6 +6127,7 @@ static void define_builtin_fns(CodeGen *g) {
|
||||
create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
|
||||
create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
|
||||
create_builtin_fn(g, BuiltinFnIdField, "field", 2);
|
||||
create_builtin_fn(g, BuiltinFnIdTypeInfo, "typeInfo", 1);
|
||||
create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf
|
||||
create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
|
||||
create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);
|
||||
@@ -6342,8 +6345,196 @@ static void define_builtin_compile_vars(CodeGen *g) {
|
||||
const TypeTableEntryId id = type_id_at_index(i);
|
||||
buf_appendf(contents, " %s,\n", type_id_name(id));
|
||||
}
|
||||
buf_appendf(contents, " Slice,\n");
|
||||
buf_appendf(contents, "};\n\n");
|
||||
}
|
||||
{
|
||||
buf_appendf(contents,
|
||||
"pub const TypeInfo = union(TypeId) {\n"
|
||||
" Type: void,\n"
|
||||
" Void: void,\n"
|
||||
" Bool: void,\n"
|
||||
" NoReturn: void,\n"
|
||||
" Int: Int,\n"
|
||||
" Float: Float,\n"
|
||||
" Pointer: Pointer,\n"
|
||||
" Slice: Slice,\n"
|
||||
" Array: Array,\n"
|
||||
" Struct: Struct,\n"
|
||||
" FloatLiteral: void,\n"
|
||||
" IntLiteral: void,\n"
|
||||
" UndefinedLiteral: void,\n"
|
||||
" NullLiteral: void,\n"
|
||||
" Nullable: Nullable,\n"
|
||||
" ErrorUnion: ErrorUnion,\n"
|
||||
" ErrorSet: ErrorSet,\n"
|
||||
" Enum: Enum,\n"
|
||||
" Union: Union,\n"
|
||||
" Fn: Fn,\n"
|
||||
" Namespace: void,\n"
|
||||
" Block: void,\n"
|
||||
" BoundFn: Fn,\n"
|
||||
" ArgTuple: void,\n"
|
||||
" Opaque: void,\n"
|
||||
" Promise: Promise,\n"
|
||||
"\n\n"
|
||||
" pub const Int = struct {\n"
|
||||
" is_signed: bool,\n"
|
||||
" bits: u8,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Float = struct {\n"
|
||||
" bits: u8,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Pointer = struct {\n"
|
||||
" is_const: bool,\n"
|
||||
" is_volatile: bool,\n"
|
||||
" alignment: u32,\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Slice = Pointer;\n"
|
||||
"\n"
|
||||
" pub const Array = struct {\n"
|
||||
" len: usize,\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const ContainerLayout = enum {\n"
|
||||
" Auto,\n"
|
||||
" Extern,\n"
|
||||
" Packed,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const StructField = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" offset: ?usize,\n"
|
||||
" field_type: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Struct = struct {\n"
|
||||
" layout: ContainerLayout,\n"
|
||||
" fields: []StructField,\n"
|
||||
" defs: []Definition,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Nullable = struct {\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const ErrorUnion = struct {\n"
|
||||
" error_set: type,\n"
|
||||
" payload: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Error = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" value: usize,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const ErrorSet = struct {\n"
|
||||
" errors: []Error,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const EnumField = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" value: usize,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Enum = struct {\n"
|
||||
" layout: ContainerLayout,\n"
|
||||
" tag_type: type,\n"
|
||||
" fields: []EnumField,\n"
|
||||
" defs: []Definition,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const UnionField = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" enum_field: ?EnumField,\n"
|
||||
" field_type: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Union = struct {\n"
|
||||
" layout: ContainerLayout,\n"
|
||||
" tag_type: type,\n"
|
||||
" fields: []UnionField,\n"
|
||||
" defs: []Definition,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const CallingConvention = enum {\n"
|
||||
" Unspecified,\n"
|
||||
" C,\n"
|
||||
" Cold,\n"
|
||||
" Naked,\n"
|
||||
" Stdcall,\n"
|
||||
" Async,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const FnArg = struct {\n"
|
||||
" is_generic: bool,\n"
|
||||
" is_noalias: bool,\n"
|
||||
" arg_type: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Fn = struct {\n"
|
||||
" calling_convention: CallingConvention,\n"
|
||||
" is_generic: bool,\n"
|
||||
" is_var_args: bool,\n"
|
||||
" return_type: type,\n"
|
||||
" async_allocator_type: type,\n"
|
||||
" args: []FnArg,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Promise = struct {\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Definition = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" is_pub: bool,\n"
|
||||
" data: Data,\n"
|
||||
"\n"
|
||||
" pub const Data = union(enum) {\n"
|
||||
" Type: type,\n"
|
||||
" Var: type,\n"
|
||||
" Fn: FnDef,\n"
|
||||
"\n"
|
||||
" pub const FnDef = struct {\n"
|
||||
" fn_type: type,\n"
|
||||
" inline_type: Inline,\n"
|
||||
" calling_convention: CallingConvention,\n"
|
||||
" is_var_args: bool,\n"
|
||||
" is_extern: bool,\n"
|
||||
" is_export: bool,\n"
|
||||
" lib_name: ?[]const u8,\n"
|
||||
" return_type: type,\n"
|
||||
" arg_names: [][] const u8,\n"
|
||||
"\n"
|
||||
" pub const Inline = enum {\n"
|
||||
" Auto,\n"
|
||||
" Always,\n"
|
||||
" Never,\n"
|
||||
" };\n"
|
||||
" };\n"
|
||||
" };\n"
|
||||
" };\n"
|
||||
"};\n\n");
|
||||
assert(ContainerLayoutAuto == 0);
|
||||
assert(ContainerLayoutExtern == 1);
|
||||
assert(ContainerLayoutPacked == 2);
|
||||
|
||||
assert(CallingConventionUnspecified == 0);
|
||||
assert(CallingConventionC == 1);
|
||||
assert(CallingConventionCold == 2);
|
||||
assert(CallingConventionNaked == 3);
|
||||
assert(CallingConventionStdcall == 4);
|
||||
assert(CallingConventionAsync == 5);
|
||||
|
||||
assert(FnInlineAuto == 0);
|
||||
assert(FnInlineAlways == 1);
|
||||
assert(FnInlineNever == 2);
|
||||
}
|
||||
{
|
||||
buf_appendf(contents,
|
||||
"pub const FloatMode = enum {\n"
|
||||
|
||||
+989
-11
@@ -145,6 +145,8 @@ static bool ir_should_inline(IrExecutable *exec, Scope *scope) {
|
||||
while (scope != nullptr) {
|
||||
if (scope->id == ScopeIdCompTime)
|
||||
return true;
|
||||
if (scope->id == ScopeIdFnDef)
|
||||
break;
|
||||
scope = scope->parent;
|
||||
}
|
||||
return false;
|
||||
@@ -615,6 +617,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {
|
||||
return IrInstructionIdOffsetOf;
|
||||
}
|
||||
|
||||
static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
|
||||
return IrInstructionIdTypeInfo;
|
||||
}
|
||||
|
||||
static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) {
|
||||
return IrInstructionIdTypeId;
|
||||
}
|
||||
@@ -2440,6 +2446,16 @@ static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *
|
||||
return &instruction->base;
|
||||
}
|
||||
|
||||
static IrInstruction *ir_build_type_info(IrBuilder *irb, Scope *scope, AstNode *source_node,
|
||||
IrInstruction *type_value) {
|
||||
IrInstructionTypeInfo *instruction = ir_build_instruction<IrInstructionTypeInfo>(irb, scope, source_node);
|
||||
instruction->type_value = type_value;
|
||||
|
||||
ir_ref_instruction(type_value, irb->current_basic_block);
|
||||
|
||||
return &instruction->base;
|
||||
}
|
||||
|
||||
static IrInstruction *ir_build_type_id(IrBuilder *irb, Scope *scope, AstNode *source_node,
|
||||
IrInstruction *type_value)
|
||||
{
|
||||
@@ -4083,6 +4099,16 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
|
||||
|
||||
return ir_build_load_ptr(irb, scope, node, ptr_instruction);
|
||||
}
|
||||
case BuiltinFnIdTypeInfo:
|
||||
{
|
||||
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
|
||||
IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
|
||||
if (arg0_value == irb->codegen->invalid_instruction)
|
||||
return arg0_value;
|
||||
|
||||
IrInstruction *type_info = ir_build_type_info(irb, scope, node, arg0_value);
|
||||
return ir_lval_wrap(irb, scope, type_info, lval);
|
||||
}
|
||||
case BuiltinFnIdBreakpoint:
|
||||
return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval);
|
||||
case BuiltinFnIdReturnAddress:
|
||||
@@ -13392,7 +13418,6 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
|
||||
return ira->codegen->invalid_instruction;
|
||||
}
|
||||
|
||||
|
||||
static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
|
||||
IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type)
|
||||
{
|
||||
@@ -13454,6 +13479,51 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
|
||||
} else if (bare_type->id == TypeTableEntryIdUnion) {
|
||||
TypeUnionField *field = find_union_type_field(bare_type, field_name);
|
||||
if (field) {
|
||||
if (instr_is_comptime(container_ptr)) {
|
||||
ConstExprValue *ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
|
||||
if (!ptr_val)
|
||||
return ira->codegen->invalid_instruction;
|
||||
|
||||
if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
|
||||
ConstExprValue *union_val = const_ptr_pointee(ira->codegen, ptr_val);
|
||||
if (type_is_invalid(union_val->type))
|
||||
return ira->codegen->invalid_instruction;
|
||||
|
||||
TypeUnionField *actual_field = find_union_field_by_tag(bare_type, &union_val->data.x_union.tag);
|
||||
if (actual_field == nullptr)
|
||||
zig_unreachable();
|
||||
|
||||
if (field != actual_field) {
|
||||
ir_add_error_node(ira, source_instr->source_node,
|
||||
buf_sprintf("accessing union field '%s' while field '%s' is set", buf_ptr(field_name),
|
||||
buf_ptr(actual_field->name)));
|
||||
return ira->codegen->invalid_instruction;
|
||||
}
|
||||
|
||||
ConstExprValue *payload_val = union_val->data.x_union.payload;
|
||||
|
||||
TypeTableEntry *field_type = field->type_entry;
|
||||
if (field_type->id == TypeTableEntryIdVoid)
|
||||
{
|
||||
assert(payload_val == nullptr);
|
||||
payload_val = create_const_vals(1);
|
||||
payload_val->special = ConstValSpecialStatic;
|
||||
payload_val->type = field_type;
|
||||
}
|
||||
|
||||
TypeTableEntry *ptr_type = get_pointer_to_type_extra(ira->codegen, field_type, is_const, is_volatile,
|
||||
get_abi_alignment(ira->codegen, field_type), 0, 0);
|
||||
|
||||
IrInstruction *result = ir_get_const(ira, source_instr);
|
||||
ConstExprValue *const_val = &result->value;
|
||||
const_val->data.x_ptr.special = ConstPtrSpecialRef;
|
||||
const_val->data.x_ptr.mut = container_ptr->value.data.x_ptr.mut;
|
||||
const_val->data.x_ptr.data.ref.pointee = payload_val;
|
||||
const_val->type = ptr_type;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
IrInstruction *result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, source_instr->source_node, container_ptr, field);
|
||||
result->value.type = get_pointer_to_type_extra(ira->codegen, field->type_entry, is_const, is_volatile,
|
||||
get_abi_alignment(ira->codegen, field->type_entry), 0, 0);
|
||||
@@ -13672,7 +13742,16 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
|
||||
create_const_enum(child_type, &field->value), child_type,
|
||||
ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
|
||||
}
|
||||
} else if (child_type->id == TypeTableEntryIdUnion &&
|
||||
}
|
||||
ScopeDecls *container_scope = get_container_scope(child_type);
|
||||
if (container_scope != nullptr) {
|
||||
auto entry = container_scope->decl_table.maybe_get(field_name);
|
||||
Tld *tld = entry ? entry->value : nullptr;
|
||||
if (tld) {
|
||||
return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
|
||||
}
|
||||
}
|
||||
if (child_type->id == TypeTableEntryIdUnion &&
|
||||
(child_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr ||
|
||||
child_type->data.unionation.decl_node->data.container_decl.auto_enum))
|
||||
{
|
||||
@@ -13689,14 +13768,6 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
|
||||
ConstPtrMutComptimeConst, ptr_is_const, ptr_is_volatile);
|
||||
}
|
||||
}
|
||||
ScopeDecls *container_scope = get_container_scope(child_type);
|
||||
if (container_scope != nullptr) {
|
||||
auto entry = container_scope->decl_table.maybe_get(field_name);
|
||||
Tld *tld = entry ? entry->value : nullptr;
|
||||
if (tld) {
|
||||
return ir_analyze_decl_ref(ira, &field_ptr_instruction->base, tld);
|
||||
}
|
||||
}
|
||||
ir_add_error(ira, &field_ptr_instruction->base,
|
||||
buf_sprintf("container '%s' has no member called '%s'",
|
||||
buf_ptr(&child_type->name), buf_ptr(field_name)));
|
||||
@@ -15683,6 +15754,910 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
|
||||
return ira->codegen->builtin_types.entry_num_lit_int;
|
||||
}
|
||||
|
||||
static void ensure_field_index(TypeTableEntry *type, const char *field_name, size_t index)
|
||||
{
|
||||
Buf *field_name_buf;
|
||||
|
||||
assert(type != nullptr && !type_is_invalid(type));
|
||||
// Check for our field by creating a buffer in place then using the comma operator to free it so that we don't
|
||||
// leak memory in debug mode.
|
||||
assert(find_struct_type_field(type, field_name_buf = buf_create_from_str(field_name))->src_index == index &&
|
||||
(buf_deinit(field_name_buf), true));
|
||||
}
|
||||
|
||||
static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, TypeTableEntry *root = nullptr)
|
||||
{
|
||||
static ConstExprValue *type_info_var = nullptr;
|
||||
static TypeTableEntry *type_info_type = nullptr;
|
||||
if (type_info_var == nullptr)
|
||||
{
|
||||
type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
|
||||
assert(type_info_var->type->id == TypeTableEntryIdMetaType);
|
||||
|
||||
ensure_complete_type(ira->codegen, type_info_var->data.x_type);
|
||||
type_info_type = type_info_var->data.x_type;
|
||||
assert(type_info_type->id == TypeTableEntryIdUnion);
|
||||
}
|
||||
|
||||
if (type_name == nullptr && root == nullptr)
|
||||
return type_info_type;
|
||||
else if (type_name == nullptr)
|
||||
return root;
|
||||
|
||||
TypeTableEntry *root_type = (root == nullptr) ? type_info_type : root;
|
||||
|
||||
ScopeDecls *type_info_scope = get_container_scope(root_type);
|
||||
assert(type_info_scope != nullptr);
|
||||
|
||||
Buf field_name = BUF_INIT;
|
||||
buf_init_from_str(&field_name, type_name);
|
||||
auto entry = type_info_scope->decl_table.get(&field_name);
|
||||
buf_deinit(&field_name);
|
||||
|
||||
TldVar *tld = (TldVar *)entry;
|
||||
assert(tld->base.id == TldIdVar);
|
||||
|
||||
VariableTableEntry *var = tld->var;
|
||||
|
||||
ensure_complete_type(ira->codegen, var->value->type);
|
||||
assert(var->value->type->id == TypeTableEntryIdMetaType);
|
||||
return var->value->data.x_type;
|
||||
}
|
||||
|
||||
static void ir_make_type_info_defs(IrAnalyze *ira, ConstExprValue *out_val, ScopeDecls *decls_scope)
|
||||
{
|
||||
TypeTableEntry *type_info_definition_type = ir_type_info_get_type(ira, "Definition");
|
||||
ensure_complete_type(ira->codegen, type_info_definition_type);
|
||||
ensure_field_index(type_info_definition_type, "name", 0);
|
||||
ensure_field_index(type_info_definition_type, "is_pub", 1);
|
||||
ensure_field_index(type_info_definition_type, "data", 2);
|
||||
|
||||
TypeTableEntry *type_info_definition_data_type = ir_type_info_get_type(ira, "Data", type_info_definition_type);
|
||||
ensure_complete_type(ira->codegen, type_info_definition_data_type);
|
||||
|
||||
TypeTableEntry *type_info_fn_def_type = ir_type_info_get_type(ira, "FnDef", type_info_definition_data_type);
|
||||
ensure_complete_type(ira->codegen, type_info_fn_def_type);
|
||||
|
||||
TypeTableEntry *type_info_fn_def_inline_type = ir_type_info_get_type(ira, "Inline", type_info_fn_def_type);
|
||||
ensure_complete_type(ira->codegen, type_info_fn_def_inline_type);
|
||||
|
||||
// Loop through our definitions once to figure out how many definitions we will generate info for.
|
||||
auto decl_it = decls_scope->decl_table.entry_iterator();
|
||||
decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
|
||||
int definition_count = 0;
|
||||
|
||||
while ((curr_entry = decl_it.next()) != nullptr)
|
||||
{
|
||||
// If the definition is unresolved, force it to be resolved again.
|
||||
if (curr_entry->value->resolution == TldResolutionUnresolved)
|
||||
{
|
||||
resolve_top_level_decl(ira->codegen, curr_entry->value, false, curr_entry->value->source_node);
|
||||
if (curr_entry->value->resolution != TldResolutionOk)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip comptime blocks and test functions.
|
||||
if (curr_entry->value->id != TldIdCompTime)
|
||||
{
|
||||
if (curr_entry->value->id == TldIdFn)
|
||||
{
|
||||
FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
|
||||
if (fn_entry->is_test)
|
||||
continue;
|
||||
}
|
||||
|
||||
definition_count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
ConstExprValue *definition_array = create_const_vals(1);
|
||||
definition_array->special = ConstValSpecialStatic;
|
||||
definition_array->type = get_array_type(ira->codegen, type_info_definition_type, definition_count);
|
||||
definition_array->data.x_array.special = ConstArraySpecialNone;
|
||||
definition_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
definition_array->data.x_array.s_none.elements = create_const_vals(definition_count);
|
||||
init_const_slice(ira->codegen, out_val, definition_array, 0, definition_count, false);
|
||||
|
||||
// Loop through the definitions and generate info.
|
||||
decl_it = decls_scope->decl_table.entry_iterator();
|
||||
curr_entry = nullptr;
|
||||
int definition_index = 0;
|
||||
while ((curr_entry = decl_it.next()) != nullptr)
|
||||
{
|
||||
// Skip comptime blocks and test functions.
|
||||
if (curr_entry->value->id == TldIdCompTime)
|
||||
continue;
|
||||
else if (curr_entry->value->id == TldIdFn)
|
||||
{
|
||||
FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
|
||||
if (fn_entry->is_test)
|
||||
continue;
|
||||
}
|
||||
|
||||
ConstExprValue *definition_val = &definition_array->data.x_array.s_none.elements[definition_index];
|
||||
|
||||
definition_val->special = ConstValSpecialStatic;
|
||||
definition_val->type = type_info_definition_type;
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(3);
|
||||
ConstExprValue *name = create_const_str_lit(ira->codegen, curr_entry->key);
|
||||
init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(curr_entry->key), true);
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
|
||||
inner_fields[1].data.x_bool = curr_entry->value->visib_mod == VisibModPub;
|
||||
inner_fields[2].special = ConstValSpecialStatic;
|
||||
inner_fields[2].type = type_info_definition_data_type;
|
||||
inner_fields[2].data.x_union.parent.id = ConstParentIdStruct;
|
||||
inner_fields[2].data.x_union.parent.data.p_struct.struct_val = definition_val;
|
||||
inner_fields[2].data.x_union.parent.data.p_struct.field_index = 1;
|
||||
|
||||
switch (curr_entry->value->id)
|
||||
{
|
||||
case TldIdVar:
|
||||
{
|
||||
VariableTableEntry *var = ((TldVar *)curr_entry->value)->var;
|
||||
ensure_complete_type(ira->codegen, var->value->type);
|
||||
if (var->value->type->id == TypeTableEntryIdMetaType)
|
||||
{
|
||||
// We have a variable of type 'type', so it's actually a type definition.
|
||||
// 0: Data.Type: type
|
||||
bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);
|
||||
inner_fields[2].data.x_union.payload = var->value;
|
||||
}
|
||||
else
|
||||
{
|
||||
// We have a variable of another type, so we store the type of the variable.
|
||||
// 1: Data.Var: type
|
||||
bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 1);
|
||||
|
||||
ConstExprValue *payload = create_const_vals(1);
|
||||
payload->type = ira->codegen->builtin_types.entry_type;
|
||||
payload->data.x_type = var->value->type;
|
||||
|
||||
inner_fields[2].data.x_union.payload = payload;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TldIdFn:
|
||||
{
|
||||
// 2: Data.Fn: Data.FnDef
|
||||
bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 2);
|
||||
|
||||
FnTableEntry *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
|
||||
assert(!fn_entry->is_test);
|
||||
|
||||
analyze_fn_body(ira->codegen, fn_entry);
|
||||
if (fn_entry->anal_state == FnAnalStateInvalid)
|
||||
return;
|
||||
|
||||
AstNodeFnProto *fn_node = (AstNodeFnProto *)(fn_entry->proto_node);
|
||||
|
||||
ConstExprValue *fn_def_val = create_const_vals(1);
|
||||
fn_def_val->special = ConstValSpecialStatic;
|
||||
fn_def_val->type = type_info_fn_def_type;
|
||||
fn_def_val->data.x_struct.parent.id = ConstParentIdUnion;
|
||||
fn_def_val->data.x_struct.parent.data.p_union.union_val = &inner_fields[2];
|
||||
|
||||
ConstExprValue *fn_def_fields = create_const_vals(9);
|
||||
fn_def_val->data.x_struct.fields = fn_def_fields;
|
||||
|
||||
// fn_type: type
|
||||
ensure_field_index(fn_def_val->type, "fn_type", 0);
|
||||
fn_def_fields[0].special = ConstValSpecialStatic;
|
||||
fn_def_fields[0].type = ira->codegen->builtin_types.entry_type;
|
||||
fn_def_fields[0].data.x_type = fn_entry->type_entry;
|
||||
// inline_type: Data.FnDef.Inline
|
||||
ensure_field_index(fn_def_val->type, "inline_type", 1);
|
||||
fn_def_fields[1].special = ConstValSpecialStatic;
|
||||
fn_def_fields[1].type = type_info_fn_def_inline_type;
|
||||
bigint_init_unsigned(&fn_def_fields[1].data.x_enum_tag, fn_entry->fn_inline);
|
||||
// calling_convention: TypeInfo.CallingConvention
|
||||
ensure_field_index(fn_def_val->type, "calling_convention", 2);
|
||||
fn_def_fields[2].special = ConstValSpecialStatic;
|
||||
fn_def_fields[2].type = ir_type_info_get_type(ira, "CallingConvention");
|
||||
bigint_init_unsigned(&fn_def_fields[2].data.x_enum_tag, fn_node->cc);
|
||||
// is_var_args: bool
|
||||
ensure_field_index(fn_def_val->type, "is_var_args", 3);
|
||||
bool is_varargs = fn_node->is_var_args;
|
||||
fn_def_fields[3].special = ConstValSpecialStatic;
|
||||
fn_def_fields[3].type = ira->codegen->builtin_types.entry_bool;
|
||||
fn_def_fields[3].data.x_bool = is_varargs;
|
||||
// is_extern: bool
|
||||
ensure_field_index(fn_def_val->type, "is_extern", 4);
|
||||
fn_def_fields[4].special = ConstValSpecialStatic;
|
||||
fn_def_fields[4].type = ira->codegen->builtin_types.entry_bool;
|
||||
fn_def_fields[4].data.x_bool = fn_node->is_extern;
|
||||
// is_export: bool
|
||||
ensure_field_index(fn_def_val->type, "is_export", 5);
|
||||
fn_def_fields[5].special = ConstValSpecialStatic;
|
||||
fn_def_fields[5].type = ira->codegen->builtin_types.entry_bool;
|
||||
fn_def_fields[5].data.x_bool = fn_node->is_export;
|
||||
// lib_name: ?[]const u8
|
||||
ensure_field_index(fn_def_val->type, "lib_name", 6);
|
||||
fn_def_fields[6].special = ConstValSpecialStatic;
|
||||
fn_def_fields[6].type = get_maybe_type(ira->codegen,
|
||||
get_slice_type(ira->codegen, get_pointer_to_type(ira->codegen,
|
||||
ira->codegen->builtin_types.entry_u8, true)));
|
||||
if (fn_node->is_extern && buf_len(fn_node->lib_name) > 0)
|
||||
{
|
||||
fn_def_fields[6].data.x_maybe = create_const_vals(1);
|
||||
ConstExprValue *lib_name = create_const_str_lit(ira->codegen, fn_node->lib_name);
|
||||
init_const_slice(ira->codegen, fn_def_fields[6].data.x_maybe, lib_name, 0, buf_len(fn_node->lib_name), true);
|
||||
}
|
||||
else
|
||||
fn_def_fields[6].data.x_maybe = nullptr;
|
||||
// return_type: type
|
||||
ensure_field_index(fn_def_val->type, "return_type", 7);
|
||||
fn_def_fields[7].special = ConstValSpecialStatic;
|
||||
fn_def_fields[7].type = ira->codegen->builtin_types.entry_type;
|
||||
if (fn_entry->src_implicit_return_type != nullptr)
|
||||
fn_def_fields[7].data.x_type = fn_entry->src_implicit_return_type;
|
||||
else if (fn_entry->type_entry->data.fn.gen_return_type != nullptr)
|
||||
fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.gen_return_type;
|
||||
else
|
||||
fn_def_fields[7].data.x_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
|
||||
// arg_names: [][] const u8
|
||||
ensure_field_index(fn_def_val->type, "arg_names", 8);
|
||||
size_t fn_arg_count = fn_entry->variable_list.length;
|
||||
ConstExprValue *fn_arg_name_array = create_const_vals(1);
|
||||
fn_arg_name_array->special = ConstValSpecialStatic;
|
||||
fn_arg_name_array->type = get_array_type(ira->codegen, get_slice_type(ira->codegen,
|
||||
get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true)), fn_arg_count);
|
||||
fn_arg_name_array->data.x_array.special = ConstArraySpecialNone;
|
||||
fn_arg_name_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
fn_arg_name_array->data.x_array.s_none.elements = create_const_vals(fn_arg_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fn_def_fields[8], fn_arg_name_array, 0, fn_arg_count, false);
|
||||
|
||||
for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++)
|
||||
{
|
||||
VariableTableEntry *arg_var = fn_entry->variable_list.at(fn_arg_index);
|
||||
ConstExprValue *fn_arg_name_val = &fn_arg_name_array->data.x_array.s_none.elements[fn_arg_index];
|
||||
ConstExprValue *arg_name = create_const_str_lit(ira->codegen, &arg_var->name);
|
||||
init_const_slice(ira->codegen, fn_arg_name_val, arg_name, 0, buf_len(&arg_var->name), true);
|
||||
fn_arg_name_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
fn_arg_name_val->data.x_struct.parent.data.p_array.array_val = fn_arg_name_array;
|
||||
fn_arg_name_val->data.x_struct.parent.data.p_array.elem_index = fn_arg_index;
|
||||
}
|
||||
|
||||
inner_fields[2].data.x_union.payload = fn_def_val;
|
||||
break;
|
||||
}
|
||||
case TldIdContainer:
|
||||
{
|
||||
TypeTableEntry *type_entry = ((TldContainer *)curr_entry->value)->type_entry;
|
||||
ensure_complete_type(ira->codegen, type_entry);
|
||||
// This is a type.
|
||||
bigint_init_unsigned(&inner_fields[2].data.x_union.tag, 0);
|
||||
|
||||
ConstExprValue *payload = create_const_vals(1);
|
||||
payload->type = ira->codegen->builtin_types.entry_type;
|
||||
payload->data.x_type = type_entry;
|
||||
|
||||
inner_fields[2].data.x_union.payload = payload;
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
zig_unreachable();
|
||||
}
|
||||
|
||||
definition_val->data.x_struct.fields = inner_fields;
|
||||
definition_index++;
|
||||
}
|
||||
|
||||
assert(definition_index == definition_count);
|
||||
}
|
||||
|
||||
static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, TypeTableEntry *type_entry)
|
||||
{
|
||||
assert(type_entry != nullptr);
|
||||
assert(!type_is_invalid(type_entry));
|
||||
|
||||
ensure_complete_type(ira->codegen, type_entry);
|
||||
|
||||
const auto make_enum_field_val = [ira](ConstExprValue *enum_field_val, TypeEnumField *enum_field,
|
||||
TypeTableEntry *type_info_enum_field_type) {
|
||||
enum_field_val->special = ConstValSpecialStatic;
|
||||
enum_field_val->type = type_info_enum_field_type;
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(2);
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
|
||||
|
||||
ConstExprValue *name = create_const_str_lit(ira->codegen, enum_field->name);
|
||||
init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(enum_field->name), true);
|
||||
|
||||
bigint_init_bigint(&inner_fields[1].data.x_bigint, &enum_field->value);
|
||||
|
||||
enum_field_val->data.x_struct.fields = inner_fields;
|
||||
};
|
||||
|
||||
const auto create_ptr_like_type_info = [ira](const char *name, TypeTableEntry *ptr_type_entry) {
|
||||
ConstExprValue *result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, name);
|
||||
|
||||
ConstExprValue *fields = create_const_vals(4);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// is_const: bool
|
||||
ensure_field_index(result->type, "is_const", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_bool;
|
||||
fields[0].data.x_bool = ptr_type_entry->data.pointer.is_const;
|
||||
// is_volatile: bool
|
||||
ensure_field_index(result->type, "is_volatile", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_bool;
|
||||
fields[1].data.x_bool = ptr_type_entry->data.pointer.is_volatile;
|
||||
// alignment: u32
|
||||
ensure_field_index(result->type, "alignment", 2);
|
||||
fields[2].special = ConstValSpecialStatic;
|
||||
fields[2].type = ira->codegen->builtin_types.entry_u32;
|
||||
bigint_init_unsigned(&fields[2].data.x_bigint, ptr_type_entry->data.pointer.alignment);
|
||||
// child: type
|
||||
ensure_field_index(result->type, "child", 3);
|
||||
fields[3].special = ConstValSpecialStatic;
|
||||
fields[3].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[3].data.x_type = ptr_type_entry->data.pointer.child_type;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
ConstExprValue *result = nullptr;
|
||||
switch (type_entry->id)
|
||||
{
|
||||
case TypeTableEntryIdInvalid:
|
||||
zig_unreachable();
|
||||
case TypeTableEntryIdMetaType:
|
||||
case TypeTableEntryIdVoid:
|
||||
case TypeTableEntryIdBool:
|
||||
case TypeTableEntryIdUnreachable:
|
||||
case TypeTableEntryIdNumLitFloat:
|
||||
case TypeTableEntryIdNumLitInt:
|
||||
case TypeTableEntryIdUndefLit:
|
||||
case TypeTableEntryIdNullLit:
|
||||
case TypeTableEntryIdNamespace:
|
||||
case TypeTableEntryIdBlock:
|
||||
case TypeTableEntryIdArgTuple:
|
||||
case TypeTableEntryIdOpaque:
|
||||
return nullptr;
|
||||
default:
|
||||
{
|
||||
// Lookup an available value in our cache.
|
||||
auto entry = ira->codegen->type_info_cache.maybe_get(type_entry);
|
||||
if (entry != nullptr)
|
||||
return entry->value;
|
||||
|
||||
// Fallthrough if we don't find one.
|
||||
}
|
||||
case TypeTableEntryIdInt:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Int");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(2);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// is_signed: bool
|
||||
ensure_field_index(result->type, "is_signed", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_bool;
|
||||
fields[0].data.x_bool = type_entry->data.integral.is_signed;
|
||||
// bits: u8
|
||||
ensure_field_index(result->type, "bits", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_u8;
|
||||
bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdFloat:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Float");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(1);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// bits: u8
|
||||
ensure_field_index(result->type, "bits", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_u8;
|
||||
bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdPointer:
|
||||
{
|
||||
result = create_ptr_like_type_info("Pointer", type_entry);
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdArray:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Array");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(2);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// len: usize
|
||||
ensure_field_index(result->type, "len", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_usize;
|
||||
bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
|
||||
// child: type
|
||||
ensure_field_index(result->type, "child", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[1].data.x_type = type_entry->data.array.child_type;
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdMaybe:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Nullable");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(1);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// child: type
|
||||
ensure_field_index(result->type, "child", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[0].data.x_type = type_entry->data.maybe.child_type;
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdPromise:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Promise");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(1);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// @TODO ?type instead of using @typeOf(undefined) when we have no type.
|
||||
// child: type
|
||||
ensure_field_index(result->type, "child", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_type;
|
||||
|
||||
if (type_entry->data.promise.result_type == nullptr)
|
||||
fields[0].data.x_type = ira->codegen->builtin_types.entry_undef;
|
||||
else
|
||||
fields[0].data.x_type = type_entry->data.promise.result_type;
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdEnum:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Enum");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(4);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// layout: ContainerLayout
|
||||
ensure_field_index(result->type, "layout", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
|
||||
bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.enumeration.layout);
|
||||
// tag_type: type
|
||||
ensure_field_index(result->type, "tag_type", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[1].data.x_type = type_entry->data.enumeration.tag_int_type;
|
||||
// fields: []TypeInfo.EnumField
|
||||
ensure_field_index(result->type, "fields", 2);
|
||||
|
||||
TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
|
||||
uint32_t enum_field_count = type_entry->data.enumeration.src_field_count;
|
||||
|
||||
ConstExprValue *enum_field_array = create_const_vals(1);
|
||||
enum_field_array->special = ConstValSpecialStatic;
|
||||
enum_field_array->type = get_array_type(ira->codegen, type_info_enum_field_type, enum_field_count);
|
||||
enum_field_array->data.x_array.special = ConstArraySpecialNone;
|
||||
enum_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
enum_field_array->data.x_array.s_none.elements = create_const_vals(enum_field_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fields[2], enum_field_array, 0, enum_field_count, false);
|
||||
|
||||
for (uint32_t enum_field_index = 0; enum_field_index < enum_field_count; enum_field_index++)
|
||||
{
|
||||
TypeEnumField *enum_field = &type_entry->data.enumeration.fields[enum_field_index];
|
||||
ConstExprValue *enum_field_val = &enum_field_array->data.x_array.s_none.elements[enum_field_index];
|
||||
make_enum_field_val(enum_field_val, enum_field, type_info_enum_field_type);
|
||||
enum_field_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
enum_field_val->data.x_struct.parent.data.p_array.array_val = enum_field_array;
|
||||
enum_field_val->data.x_struct.parent.data.p_array.elem_index = enum_field_index;
|
||||
}
|
||||
// defs: []TypeInfo.Definition
|
||||
ensure_field_index(result->type, "defs", 3);
|
||||
ir_make_type_info_defs(ira, &fields[3], type_entry->data.enumeration.decls_scope);
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdErrorSet:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "ErrorSet");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(1);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// errors: []TypeInfo.Error
|
||||
ensure_field_index(result->type, "errors", 0);
|
||||
|
||||
TypeTableEntry *type_info_error_type = ir_type_info_get_type(ira, "Error");
|
||||
uint32_t error_count = type_entry->data.error_set.err_count;
|
||||
ConstExprValue *error_array = create_const_vals(1);
|
||||
error_array->special = ConstValSpecialStatic;
|
||||
error_array->type = get_array_type(ira->codegen, type_info_error_type, error_count);
|
||||
error_array->data.x_array.special = ConstArraySpecialNone;
|
||||
error_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
error_array->data.x_array.s_none.elements = create_const_vals(error_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fields[0], error_array, 0, error_count, false);
|
||||
for (uint32_t error_index = 0; error_index < error_count; error_index++)
|
||||
{
|
||||
ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];
|
||||
ConstExprValue *error_val = &error_array->data.x_array.s_none.elements[error_index];
|
||||
|
||||
error_val->special = ConstValSpecialStatic;
|
||||
error_val->type = type_info_error_type;
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(2);
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = ira->codegen->builtin_types.entry_usize;
|
||||
|
||||
ConstExprValue *name = nullptr;
|
||||
if (error->cached_error_name_val != nullptr)
|
||||
name = error->cached_error_name_val;
|
||||
if (name == nullptr)
|
||||
name = create_const_str_lit(ira->codegen, &error->name);
|
||||
init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(&error->name), true);
|
||||
bigint_init_unsigned(&inner_fields[1].data.x_bigint, error->value);
|
||||
|
||||
error_val->data.x_struct.fields = inner_fields;
|
||||
error_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
error_val->data.x_struct.parent.data.p_array.array_val = error_array;
|
||||
error_val->data.x_struct.parent.data.p_array.elem_index = error_index;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdErrorUnion:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "ErrorUnion");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(2);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// error_set: type
|
||||
ensure_field_index(result->type, "error_set", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[0].data.x_type = type_entry->data.error_union.err_set_type;
|
||||
|
||||
// payload: type
|
||||
ensure_field_index(result->type, "payload", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_type;
|
||||
fields[1].data.x_type = type_entry->data.error_union.payload_type;
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdUnion:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Union");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(4);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// layout: ContainerLayout
|
||||
ensure_field_index(result->type, "layout", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
|
||||
bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.unionation.layout);
|
||||
// tag_type: type
|
||||
ensure_field_index(result->type, "tag_type", 1);
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_type;
|
||||
// @TODO ?type instead of using @typeOf(undefined) when we have no type.
|
||||
AstNode *union_decl_node = type_entry->data.unionation.decl_node;
|
||||
if (union_decl_node->data.container_decl.auto_enum ||
|
||||
union_decl_node->data.container_decl.init_arg_expr != nullptr)
|
||||
{
|
||||
fields[1].data.x_type = type_entry->data.unionation.tag_type;
|
||||
}
|
||||
else
|
||||
fields[1].data.x_type = ira->codegen->builtin_types.entry_undef;
|
||||
// fields: []TypeInfo.UnionField
|
||||
ensure_field_index(result->type, "fields", 2);
|
||||
|
||||
TypeTableEntry *type_info_union_field_type = ir_type_info_get_type(ira, "UnionField");
|
||||
uint32_t union_field_count = type_entry->data.unionation.src_field_count;
|
||||
|
||||
ConstExprValue *union_field_array = create_const_vals(1);
|
||||
union_field_array->special = ConstValSpecialStatic;
|
||||
union_field_array->type = get_array_type(ira->codegen, type_info_union_field_type, union_field_count);
|
||||
union_field_array->data.x_array.special = ConstArraySpecialNone;
|
||||
union_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
union_field_array->data.x_array.s_none.elements = create_const_vals(union_field_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fields[2], union_field_array, 0, union_field_count, false);
|
||||
|
||||
TypeTableEntry *type_info_enum_field_type = ir_type_info_get_type(ira, "EnumField");
|
||||
|
||||
for (uint32_t union_field_index = 0; union_field_index < union_field_count; union_field_index++)
|
||||
{
|
||||
TypeUnionField *union_field = &type_entry->data.unionation.fields[union_field_index];
|
||||
ConstExprValue *union_field_val = &union_field_array->data.x_array.s_none.elements[union_field_index];
|
||||
|
||||
union_field_val->special = ConstValSpecialStatic;
|
||||
union_field_val->type = type_info_union_field_type;
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(3);
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = get_maybe_type(ira->codegen, type_info_enum_field_type);
|
||||
|
||||
if (fields[1].data.x_type == ira->codegen->builtin_types.entry_undef)
|
||||
inner_fields[1].data.x_maybe = nullptr;
|
||||
else
|
||||
{
|
||||
inner_fields[1].data.x_maybe = create_const_vals(1);
|
||||
make_enum_field_val(inner_fields[1].data.x_maybe, union_field->enum_field, type_info_enum_field_type);
|
||||
}
|
||||
|
||||
inner_fields[2].special = ConstValSpecialStatic;
|
||||
inner_fields[2].type = ira->codegen->builtin_types.entry_type;
|
||||
inner_fields[2].data.x_type = union_field->type_entry;
|
||||
|
||||
ConstExprValue *name = create_const_str_lit(ira->codegen, union_field->name);
|
||||
init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(union_field->name), true);
|
||||
|
||||
union_field_val->data.x_struct.fields = inner_fields;
|
||||
union_field_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
union_field_val->data.x_struct.parent.data.p_array.array_val = union_field_array;
|
||||
union_field_val->data.x_struct.parent.data.p_array.elem_index = union_field_index;
|
||||
}
|
||||
// defs: []TypeInfo.Definition
|
||||
ensure_field_index(result->type, "defs", 3);
|
||||
ir_make_type_info_defs(ira, &fields[3], type_entry->data.unionation.decls_scope);
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdStruct:
|
||||
{
|
||||
if (type_entry->data.structure.is_slice) {
|
||||
Buf ptr_field_name = BUF_INIT;
|
||||
buf_init_from_str(&ptr_field_name, "ptr");
|
||||
TypeTableEntry *ptr_type = type_entry->data.structure.fields_by_name.get(&ptr_field_name)->type_entry;
|
||||
ensure_complete_type(ira->codegen, ptr_type);
|
||||
buf_deinit(&ptr_field_name);
|
||||
|
||||
result = create_ptr_like_type_info("Slice", ptr_type);
|
||||
break;
|
||||
}
|
||||
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Struct");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(3);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// layout: ContainerLayout
|
||||
ensure_field_index(result->type, "layout", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ir_type_info_get_type(ira, "ContainerLayout");
|
||||
bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.structure.layout);
|
||||
// fields: []TypeInfo.StructField
|
||||
ensure_field_index(result->type, "fields", 1);
|
||||
|
||||
TypeTableEntry *type_info_struct_field_type = ir_type_info_get_type(ira, "StructField");
|
||||
uint32_t struct_field_count = type_entry->data.structure.src_field_count;
|
||||
|
||||
ConstExprValue *struct_field_array = create_const_vals(1);
|
||||
struct_field_array->special = ConstValSpecialStatic;
|
||||
struct_field_array->type = get_array_type(ira->codegen, type_info_struct_field_type, struct_field_count);
|
||||
struct_field_array->data.x_array.special = ConstArraySpecialNone;
|
||||
struct_field_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
struct_field_array->data.x_array.s_none.elements = create_const_vals(struct_field_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fields[1], struct_field_array, 0, struct_field_count, false);
|
||||
|
||||
for (uint32_t struct_field_index = 0; struct_field_index < struct_field_count; struct_field_index++)
|
||||
{
|
||||
TypeStructField *struct_field = &type_entry->data.structure.fields[struct_field_index];
|
||||
ConstExprValue *struct_field_val = &struct_field_array->data.x_array.s_none.elements[struct_field_index];
|
||||
|
||||
struct_field_val->special = ConstValSpecialStatic;
|
||||
struct_field_val->type = type_info_struct_field_type;
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(3);
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = get_maybe_type(ira->codegen, ira->codegen->builtin_types.entry_usize);
|
||||
|
||||
if (!type_has_bits(struct_field->type_entry))
|
||||
inner_fields[1].data.x_maybe = nullptr;
|
||||
else
|
||||
{
|
||||
size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, type_entry->type_ref, struct_field->gen_index);
|
||||
inner_fields[1].data.x_maybe = create_const_vals(1);
|
||||
inner_fields[1].data.x_maybe->type = ira->codegen->builtin_types.entry_usize;
|
||||
bigint_init_unsigned(&inner_fields[1].data.x_maybe->data.x_bigint, byte_offset);
|
||||
}
|
||||
|
||||
inner_fields[2].special = ConstValSpecialStatic;
|
||||
inner_fields[2].type = ira->codegen->builtin_types.entry_type;
|
||||
inner_fields[2].data.x_type = struct_field->type_entry;
|
||||
|
||||
ConstExprValue *name = create_const_str_lit(ira->codegen, struct_field->name);
|
||||
init_const_slice(ira->codegen, &inner_fields[0], name, 0, buf_len(struct_field->name), true);
|
||||
|
||||
struct_field_val->data.x_struct.fields = inner_fields;
|
||||
struct_field_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
struct_field_val->data.x_struct.parent.data.p_array.array_val = struct_field_array;
|
||||
struct_field_val->data.x_struct.parent.data.p_array.elem_index = struct_field_index;
|
||||
}
|
||||
// defs: []TypeInfo.Definition
|
||||
ensure_field_index(result->type, "defs", 2);
|
||||
ir_make_type_info_defs(ira, &fields[2], type_entry->data.structure.decls_scope);
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdFn:
|
||||
{
|
||||
result = create_const_vals(1);
|
||||
result->special = ConstValSpecialStatic;
|
||||
result->type = ir_type_info_get_type(ira, "Fn");
|
||||
|
||||
ConstExprValue *fields = create_const_vals(6);
|
||||
result->data.x_struct.fields = fields;
|
||||
|
||||
// @TODO Fix type = undefined with ?type
|
||||
|
||||
// calling_convention: TypeInfo.CallingConvention
|
||||
ensure_field_index(result->type, "calling_convention", 0);
|
||||
fields[0].special = ConstValSpecialStatic;
|
||||
fields[0].type = ir_type_info_get_type(ira, "CallingConvention");
|
||||
bigint_init_unsigned(&fields[0].data.x_enum_tag, type_entry->data.fn.fn_type_id.cc);
|
||||
// is_generic: bool
|
||||
ensure_field_index(result->type, "is_generic", 1);
|
||||
bool is_generic = type_entry->data.fn.is_generic;
|
||||
fields[1].special = ConstValSpecialStatic;
|
||||
fields[1].type = ira->codegen->builtin_types.entry_bool;
|
||||
fields[1].data.x_bool = is_generic;
|
||||
// is_varargs: bool
|
||||
ensure_field_index(result->type, "is_var_args", 2);
|
||||
bool is_varargs = type_entry->data.fn.fn_type_id.is_var_args;
|
||||
fields[2].special = ConstValSpecialStatic;
|
||||
fields[2].type = ira->codegen->builtin_types.entry_bool;
|
||||
fields[2].data.x_bool = type_entry->data.fn.fn_type_id.is_var_args;
|
||||
// return_type: type
|
||||
ensure_field_index(result->type, "return_type", 3);
|
||||
fields[3].special = ConstValSpecialStatic;
|
||||
fields[3].type = ira->codegen->builtin_types.entry_type;
|
||||
if (type_entry->data.fn.fn_type_id.return_type == nullptr)
|
||||
fields[3].data.x_type = ira->codegen->builtin_types.entry_undef;
|
||||
else
|
||||
fields[3].data.x_type = type_entry->data.fn.fn_type_id.return_type;
|
||||
// async_allocator_type: type
|
||||
ensure_field_index(result->type, "async_allocator_type", 4);
|
||||
fields[4].special = ConstValSpecialStatic;
|
||||
fields[4].type = ira->codegen->builtin_types.entry_type;
|
||||
if (type_entry->data.fn.fn_type_id.async_allocator_type == nullptr)
|
||||
fields[4].data.x_type = ira->codegen->builtin_types.entry_undef;
|
||||
else
|
||||
fields[4].data.x_type = type_entry->data.fn.fn_type_id.async_allocator_type;
|
||||
// args: []TypeInfo.FnArg
|
||||
TypeTableEntry *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg");
|
||||
size_t fn_arg_count = type_entry->data.fn.fn_type_id.param_count -
|
||||
(is_varargs && type_entry->data.fn.fn_type_id.cc != CallingConventionC);
|
||||
|
||||
ConstExprValue *fn_arg_array = create_const_vals(1);
|
||||
fn_arg_array->special = ConstValSpecialStatic;
|
||||
fn_arg_array->type = get_array_type(ira->codegen, type_info_fn_arg_type, fn_arg_count);
|
||||
fn_arg_array->data.x_array.special = ConstArraySpecialNone;
|
||||
fn_arg_array->data.x_array.s_none.parent.id = ConstParentIdNone;
|
||||
fn_arg_array->data.x_array.s_none.elements = create_const_vals(fn_arg_count);
|
||||
|
||||
init_const_slice(ira->codegen, &fields[5], fn_arg_array, 0, fn_arg_count, false);
|
||||
|
||||
for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++)
|
||||
{
|
||||
FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];
|
||||
ConstExprValue *fn_arg_val = &fn_arg_array->data.x_array.s_none.elements[fn_arg_index];
|
||||
|
||||
fn_arg_val->special = ConstValSpecialStatic;
|
||||
fn_arg_val->type = type_info_fn_arg_type;
|
||||
|
||||
bool arg_is_generic = fn_param_info->type == nullptr;
|
||||
if (arg_is_generic) assert(is_generic);
|
||||
|
||||
ConstExprValue *inner_fields = create_const_vals(3);
|
||||
inner_fields[0].special = ConstValSpecialStatic;
|
||||
inner_fields[0].type = ira->codegen->builtin_types.entry_bool;
|
||||
inner_fields[0].data.x_bool = arg_is_generic;
|
||||
inner_fields[1].special = ConstValSpecialStatic;
|
||||
inner_fields[1].type = ira->codegen->builtin_types.entry_bool;
|
||||
inner_fields[1].data.x_bool = fn_param_info->is_noalias;
|
||||
inner_fields[2].special = ConstValSpecialStatic;
|
||||
inner_fields[2].type = ira->codegen->builtin_types.entry_type;
|
||||
|
||||
if (arg_is_generic)
|
||||
inner_fields[2].data.x_type = ira->codegen->builtin_types.entry_undef;
|
||||
else
|
||||
inner_fields[2].data.x_type = fn_param_info->type;
|
||||
|
||||
fn_arg_val->data.x_struct.fields = inner_fields;
|
||||
fn_arg_val->data.x_struct.parent.id = ConstParentIdArray;
|
||||
fn_arg_val->data.x_struct.parent.data.p_array.array_val = fn_arg_array;
|
||||
fn_arg_val->data.x_struct.parent.data.p_array.elem_index = fn_arg_index;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case TypeTableEntryIdBoundFn:
|
||||
{
|
||||
TypeTableEntry *fn_type = type_entry->data.bound_fn.fn_type;
|
||||
assert(fn_type->id == TypeTableEntryIdFn);
|
||||
result = ir_make_type_info_value(ira, fn_type);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert(result != nullptr);
|
||||
ira->codegen->type_info_cache.put(type_entry, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
|
||||
IrInstructionTypeInfo *instruction)
|
||||
{
|
||||
IrInstruction *type_value = instruction->type_value->other;
|
||||
TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
|
||||
if (type_is_invalid(type_entry))
|
||||
return ira->codegen->builtin_types.entry_invalid;
|
||||
|
||||
TypeTableEntry *result_type = ir_type_info_get_type(ira, nullptr);
|
||||
|
||||
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
|
||||
out_val->type = result_type;
|
||||
bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry));
|
||||
|
||||
ConstExprValue *payload = ir_make_type_info_value(ira, type_entry);
|
||||
out_val->data.x_union.payload = payload;
|
||||
|
||||
if (payload != nullptr)
|
||||
{
|
||||
assert(payload->type->id == TypeTableEntryIdStruct);
|
||||
payload->data.x_struct.parent.id = ConstParentIdUnion;
|
||||
payload->data.x_struct.parent.data.p_union.union_val = out_val;
|
||||
}
|
||||
|
||||
return result_type;
|
||||
}
|
||||
|
||||
static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
|
||||
IrInstructionTypeId *instruction)
|
||||
{
|
||||
@@ -15696,7 +16671,7 @@ static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
|
||||
TypeTableEntry *result_type = var_value->data.x_type;
|
||||
|
||||
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
|
||||
bigint_init_unsigned(&out_val->data.x_enum_tag, type_id_index(type_entry->id));
|
||||
bigint_init_unsigned(&out_val->data.x_enum_tag, type_id_index(type_entry));
|
||||
return result_type;
|
||||
}
|
||||
|
||||
@@ -18584,6 +19559,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
|
||||
return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);
|
||||
case IrInstructionIdOffsetOf:
|
||||
return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);
|
||||
case IrInstructionIdTypeInfo:
|
||||
return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
|
||||
case IrInstructionIdTypeId:
|
||||
return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);
|
||||
case IrInstructionIdSetEvalBranchQuota:
|
||||
@@ -18850,6 +19827,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
|
||||
case IrInstructionIdTagName:
|
||||
case IrInstructionIdFieldParentPtr:
|
||||
case IrInstructionIdOffsetOf:
|
||||
case IrInstructionIdTypeInfo:
|
||||
case IrInstructionIdTypeId:
|
||||
case IrInstructionIdAlignCast:
|
||||
case IrInstructionIdOpaqueType:
|
||||
|
||||
@@ -966,6 +966,12 @@ static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction)
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_type_info(IrPrint *irp, IrInstructionTypeInfo *instruction) {
|
||||
fprintf(irp->f, "@typeInfo(");
|
||||
ir_print_other_instruction(irp, instruction->type_value);
|
||||
fprintf(irp->f, ")");
|
||||
}
|
||||
|
||||
static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {
|
||||
fprintf(irp->f, "@typeId(");
|
||||
ir_print_other_instruction(irp, instruction->type_value);
|
||||
@@ -1536,6 +1542,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
|
||||
case IrInstructionIdOffsetOf:
|
||||
ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);
|
||||
break;
|
||||
case IrInstructionIdTypeInfo:
|
||||
ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
|
||||
break;
|
||||
case IrInstructionIdTypeId:
|
||||
ir_print_type_id(irp, (IrInstructionTypeId *)instruction);
|
||||
break;
|
||||
|
||||
+45
-71
@@ -3672,6 +3672,7 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_
|
||||
if (existing_entry) {
|
||||
return existing_entry->value;
|
||||
}
|
||||
|
||||
QualType child_qt = typedef_decl->getUnderlyingType();
|
||||
Buf *type_name = buf_create_from_str(decl_name(typedef_decl));
|
||||
|
||||
@@ -3705,16 +3706,19 @@ static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_
|
||||
// use the name of this typedef
|
||||
// TODO
|
||||
|
||||
// trans_qual_type here might cause us to look at this typedef again so we put the item in the map first
|
||||
AstNode *symbol_node = trans_create_node_symbol(c, type_name);
|
||||
c->decl_table.put(typedef_decl->getCanonicalDecl(), symbol_node);
|
||||
|
||||
AstNode *type_node = trans_qual_type(c, child_qt, typedef_decl->getLocation());
|
||||
if (type_node == nullptr) {
|
||||
emit_warning(c, typedef_decl->getLocation(), "typedef %s - unresolved child type", buf_ptr(type_name));
|
||||
c->decl_table.put(typedef_decl, nullptr);
|
||||
// TODO add global var with type_name equal to @compileError("unable to resolve C type")
|
||||
return nullptr;
|
||||
}
|
||||
add_global_var(c, type_name, type_node);
|
||||
|
||||
AstNode *symbol_node = trans_create_node_symbol(c, type_name);
|
||||
c->decl_table.put(typedef_decl->getCanonicalDecl(), symbol_node);
|
||||
return symbol_node;
|
||||
}
|
||||
|
||||
@@ -3749,6 +3753,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) {
|
||||
return demote_enum_to_opaque(c, enum_decl, full_type_name, bare_name);
|
||||
}
|
||||
|
||||
|
||||
bool pure_enum = true;
|
||||
uint32_t field_count = 0;
|
||||
for (auto it = enum_def->enumerator_begin(),
|
||||
@@ -3760,84 +3765,53 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) {
|
||||
pure_enum = false;
|
||||
}
|
||||
}
|
||||
|
||||
AstNode *tag_int_type = trans_qual_type(c, enum_decl->getIntegerType(), enum_decl->getLocation());
|
||||
assert(tag_int_type);
|
||||
|
||||
if (pure_enum) {
|
||||
AstNode *enum_node = trans_create_node(c, NodeTypeContainerDecl);
|
||||
enum_node->data.container_decl.kind = ContainerKindEnum;
|
||||
enum_node->data.container_decl.layout = ContainerLayoutExtern;
|
||||
// TODO only emit this tag type if the enum tag type is not the default.
|
||||
// I don't know what the default is, need to figure out how clang is deciding.
|
||||
// it appears to at least be different across gcc/msvc
|
||||
if (!c_is_builtin_type(c, enum_decl->getIntegerType(), BuiltinType::UInt) &&
|
||||
!c_is_builtin_type(c, enum_decl->getIntegerType(), BuiltinType::Int))
|
||||
{
|
||||
enum_node->data.container_decl.init_arg_expr = tag_int_type;
|
||||
}
|
||||
|
||||
enum_node->data.container_decl.fields.resize(field_count);
|
||||
uint32_t i = 0;
|
||||
for (auto it = enum_def->enumerator_begin(),
|
||||
it_end = enum_def->enumerator_end();
|
||||
it != it_end; ++it, i += 1)
|
||||
{
|
||||
const EnumConstantDecl *enum_const = *it;
|
||||
|
||||
Buf *enum_val_name = buf_create_from_str(decl_name(enum_const));
|
||||
Buf *field_name;
|
||||
if (bare_name != nullptr && buf_starts_with_buf(enum_val_name, bare_name)) {
|
||||
field_name = buf_slice(enum_val_name, buf_len(bare_name), buf_len(enum_val_name));
|
||||
} else {
|
||||
field_name = enum_val_name;
|
||||
}
|
||||
|
||||
AstNode *field_node = trans_create_node(c, NodeTypeStructField);
|
||||
field_node->data.struct_field.name = field_name;
|
||||
field_node->data.struct_field.type = nullptr;
|
||||
enum_node->data.container_decl.fields.items[i] = field_node;
|
||||
|
||||
// in C each enum value is in the global namespace. so we put them there too.
|
||||
// at this point we can rely on the enum emitting successfully
|
||||
if (is_anonymous) {
|
||||
AstNode *lit_node = trans_create_node_unsigned(c, i);
|
||||
add_global_var(c, enum_val_name, lit_node);
|
||||
} else {
|
||||
AstNode *field_access_node = trans_create_node_field_access(c,
|
||||
trans_create_node_symbol(c, full_type_name), field_name);
|
||||
add_global_var(c, enum_val_name, field_access_node);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_anonymous) {
|
||||
c->decl_table.put(enum_decl->getCanonicalDecl(), enum_node);
|
||||
return enum_node;
|
||||
} else {
|
||||
AstNode *symbol_node = trans_create_node_symbol(c, full_type_name);
|
||||
add_global_weak_alias(c, bare_name, full_type_name);
|
||||
add_global_var(c, full_type_name, enum_node);
|
||||
c->decl_table.put(enum_decl->getCanonicalDecl(), symbol_node);
|
||||
return enum_node;
|
||||
}
|
||||
AstNode *enum_node = trans_create_node(c, NodeTypeContainerDecl);
|
||||
enum_node->data.container_decl.kind = ContainerKindEnum;
|
||||
enum_node->data.container_decl.layout = ContainerLayoutExtern;
|
||||
// TODO only emit this tag type if the enum tag type is not the default.
|
||||
// I don't know what the default is, need to figure out how clang is deciding.
|
||||
// it appears to at least be different across gcc/msvc
|
||||
if (!c_is_builtin_type(c, enum_decl->getIntegerType(), BuiltinType::UInt) &&
|
||||
!c_is_builtin_type(c, enum_decl->getIntegerType(), BuiltinType::Int))
|
||||
{
|
||||
enum_node->data.container_decl.init_arg_expr = tag_int_type;
|
||||
}
|
||||
|
||||
// TODO after issue #305 is solved, make this be an enum with tag_int_type
|
||||
// as the integer type and set the custom enum values
|
||||
AstNode *enum_node = tag_int_type;
|
||||
|
||||
|
||||
// add variables for all the values with enum_node
|
||||
enum_node->data.container_decl.fields.resize(field_count);
|
||||
uint32_t i = 0;
|
||||
for (auto it = enum_def->enumerator_begin(),
|
||||
it_end = enum_def->enumerator_end();
|
||||
it != it_end; ++it)
|
||||
it != it_end; ++it, i += 1)
|
||||
{
|
||||
const EnumConstantDecl *enum_const = *it;
|
||||
|
||||
Buf *enum_val_name = buf_create_from_str(decl_name(enum_const));
|
||||
AstNode *int_node = trans_create_node_apint(c, enum_const->getInitVal());
|
||||
AstNode *var_node = add_global_var(c, enum_val_name, int_node);
|
||||
var_node->data.variable_declaration.type = tag_int_type;
|
||||
Buf *field_name;
|
||||
if (bare_name != nullptr && buf_starts_with_buf(enum_val_name, bare_name)) {
|
||||
field_name = buf_slice(enum_val_name, buf_len(bare_name), buf_len(enum_val_name));
|
||||
} else {
|
||||
field_name = enum_val_name;
|
||||
}
|
||||
|
||||
AstNode *int_node = pure_enum && !is_anonymous ? nullptr : trans_create_node_apint(c, enum_const->getInitVal());
|
||||
AstNode *field_node = trans_create_node(c, NodeTypeStructField);
|
||||
field_node->data.struct_field.name = field_name;
|
||||
field_node->data.struct_field.type = nullptr;
|
||||
field_node->data.struct_field.value = int_node;
|
||||
enum_node->data.container_decl.fields.items[i] = field_node;
|
||||
|
||||
// in C each enum value is in the global namespace. so we put them there too.
|
||||
// at this point we can rely on the enum emitting successfully
|
||||
if (is_anonymous) {
|
||||
Buf *enum_val_name = buf_create_from_str(decl_name(enum_const));
|
||||
add_global_var(c, enum_val_name, int_node);
|
||||
} else {
|
||||
AstNode *field_access_node = trans_create_node_field_access(c,
|
||||
trans_create_node_symbol(c, full_type_name), field_name);
|
||||
add_global_var(c, enum_val_name, field_access_node);
|
||||
}
|
||||
}
|
||||
|
||||
if (is_anonymous) {
|
||||
@@ -3848,7 +3822,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl) {
|
||||
add_global_weak_alias(c, bare_name, full_type_name);
|
||||
add_global_var(c, full_type_name, enum_node);
|
||||
c->decl_table.put(enum_decl->getCanonicalDecl(), symbol_node);
|
||||
return symbol_node;
|
||||
return enum_node;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+64
-2
@@ -28,11 +28,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(l: &Self) void {
|
||||
pub fn deinit(l: &const Self) void {
|
||||
l.allocator.free(l.items);
|
||||
}
|
||||
|
||||
pub fn toSlice(l: &Self) []align(A) T {
|
||||
pub fn toSlice(l: &const Self) []align(A) T {
|
||||
return l.items[0..l.len];
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
|
||||
return l.toSliceConst()[n];
|
||||
}
|
||||
|
||||
pub fn count(self: &const Self) usize {
|
||||
return self.len;
|
||||
}
|
||||
|
||||
/// ArrayList takes ownership of the passed in slice. The slice must have been
|
||||
/// allocated with `allocator`.
|
||||
/// Deinitialize with `deinit` or use `toOwnedSlice`.
|
||||
@@ -127,6 +131,27 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
|
||||
if (self.len == 0) return null;
|
||||
return self.pop();
|
||||
}
|
||||
|
||||
pub const Iterator = struct {
|
||||
list: &const Self,
|
||||
// how many items have we returned
|
||||
count: usize,
|
||||
|
||||
pub fn next(it: &Iterator) ?T {
|
||||
if (it.count >= it.list.len) return null;
|
||||
const val = it.list.at(it.count);
|
||||
it.count += 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
pub fn reset(it: &Iterator) void {
|
||||
it.count = 0;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn iterator(self: &const Self) Iterator {
|
||||
return Iterator { .list = self, .count = 0 };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -148,6 +173,14 @@ test "basic ArrayList test" {
|
||||
}
|
||||
}
|
||||
|
||||
for (list.toSlice()) |v, i| {
|
||||
assert(v == i32(i + 1));
|
||||
}
|
||||
|
||||
for (list.toSliceConst()) |v, i| {
|
||||
assert(v == i32(i + 1));
|
||||
}
|
||||
|
||||
assert(list.pop() == 10);
|
||||
assert(list.len == 9);
|
||||
|
||||
@@ -166,6 +199,35 @@ test "basic ArrayList test" {
|
||||
assert(list.len == 9);
|
||||
}
|
||||
|
||||
test "iterator ArrayList test" {
|
||||
var list = ArrayList(i32).init(debug.global_allocator);
|
||||
defer list.deinit();
|
||||
|
||||
try list.append(1);
|
||||
try list.append(2);
|
||||
try list.append(3);
|
||||
|
||||
var count : i32 = 0;
|
||||
var it = list.iterator();
|
||||
while (it.next()) |next| {
|
||||
assert(next == count + 1);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
assert(count == 3);
|
||||
assert(it.next() == null);
|
||||
it.reset();
|
||||
count = 0;
|
||||
while (it.next()) |next| {
|
||||
assert(next == count + 1);
|
||||
count += 1;
|
||||
if (count == 2) break;
|
||||
}
|
||||
|
||||
it.reset();
|
||||
assert(?? it.next() == 1);
|
||||
}
|
||||
|
||||
test "insert ArrayList test" {
|
||||
var list = ArrayList(i32).init(debug.global_allocator);
|
||||
defer list.deinit();
|
||||
|
||||
+10
-4
@@ -31,10 +31,10 @@ pub fn Queue(comptime T: type) type {
|
||||
}
|
||||
|
||||
pub fn get(self: &Self) ?&Node {
|
||||
var head = @atomicLoad(&Node, &self.head, AtomicOrder.Acquire);
|
||||
var head = @atomicLoad(&Node, &self.head, AtomicOrder.SeqCst);
|
||||
while (true) {
|
||||
const node = head.next ?? return null;
|
||||
head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.Release, AtomicOrder.Acquire) ?? return node;
|
||||
head = @cmpxchgWeak(&Node, &self.head, head, node, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return node;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -49,14 +49,20 @@ const Context = struct {
|
||||
get_count: usize,
|
||||
puts_done: u8, // TODO make this a bool
|
||||
};
|
||||
const puts_per_thread = 10000;
|
||||
|
||||
// TODO add lazy evaluated build options and then put puts_per_thread behind
|
||||
// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
|
||||
// CI we would use a less aggressive setting since at 1 core, while we still
|
||||
// want this test to pass, we need a smaller value since there is so much thrashing
|
||||
// we would also use a less aggressive setting when running in valgrind
|
||||
const puts_per_thread = 500;
|
||||
const put_thread_count = 3;
|
||||
|
||||
test "std.atomic.queue" {
|
||||
var direct_allocator = std.heap.DirectAllocator.init();
|
||||
defer direct_allocator.deinit();
|
||||
|
||||
var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
|
||||
var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
|
||||
defer direct_allocator.allocator.free(plenty_of_memory);
|
||||
|
||||
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
|
||||
|
||||
@@ -35,7 +35,7 @@ pub fn Stack(comptime T: type) type {
|
||||
}
|
||||
|
||||
pub fn pop(self: &Self) ?&Node {
|
||||
var root = @atomicLoad(?&Node, &self.root, AtomicOrder.Acquire);
|
||||
var root = @atomicLoad(?&Node, &self.root, AtomicOrder.SeqCst);
|
||||
while (true) {
|
||||
root = @cmpxchgWeak(?&Node, &self.root, root, (root ?? return null).next, AtomicOrder.SeqCst, AtomicOrder.SeqCst) ?? return root;
|
||||
}
|
||||
@@ -56,14 +56,19 @@ const Context = struct {
|
||||
get_count: usize,
|
||||
puts_done: u8, // TODO make this a bool
|
||||
};
|
||||
const puts_per_thread = 1000;
|
||||
// TODO add lazy evaluated build options and then put puts_per_thread behind
|
||||
// some option such as: "AggressiveMultithreadedFuzzTest". In the AppVeyor
|
||||
// CI we would use a less aggressive setting since at 1 core, while we still
|
||||
// want this test to pass, we need a smaller value since there is so much thrashing
|
||||
// we would also use a less aggressive setting when running in valgrind
|
||||
const puts_per_thread = 500;
|
||||
const put_thread_count = 3;
|
||||
|
||||
test "std.atomic.stack" {
|
||||
var direct_allocator = std.heap.DirectAllocator.init();
|
||||
defer direct_allocator.deinit();
|
||||
|
||||
var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 64 * 1024 * 1024);
|
||||
var plenty_of_memory = try direct_allocator.allocator.alloc(u8, 300 * 1024);
|
||||
defer direct_allocator.allocator.free(plenty_of_memory);
|
||||
|
||||
var fixed_buffer_allocator = std.heap.ThreadSafeFixedBufferAllocator.init(plenty_of_memory);
|
||||
|
||||
+6
-6
@@ -18,10 +18,10 @@ pub const BufMap = struct {
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn deinit(self: &BufMap) void {
|
||||
pub fn deinit(self: &const BufMap) void {
|
||||
var it = self.hash_map.iterator();
|
||||
while (true) {
|
||||
const entry = it.next() ?? break;
|
||||
const entry = it.next() ?? break;
|
||||
self.free(entry.key);
|
||||
self.free(entry.value);
|
||||
}
|
||||
@@ -38,7 +38,7 @@ pub const BufMap = struct {
|
||||
_ = try self.hash_map.put(key_copy, value_copy);
|
||||
}
|
||||
|
||||
pub fn get(self: &BufMap, key: []const u8) ?[]const u8 {
|
||||
pub fn get(self: &const BufMap, key: []const u8) ?[]const u8 {
|
||||
const entry = self.hash_map.get(key) ?? return null;
|
||||
return entry.value;
|
||||
}
|
||||
@@ -50,18 +50,18 @@ pub const BufMap = struct {
|
||||
}
|
||||
|
||||
pub fn count(self: &const BufMap) usize {
|
||||
return self.hash_map.size;
|
||||
return self.hash_map.count();
|
||||
}
|
||||
|
||||
pub fn iterator(self: &const BufMap) BufMapHashMap.Iterator {
|
||||
return self.hash_map.iterator();
|
||||
}
|
||||
|
||||
fn free(self: &BufMap, value: []const u8) void {
|
||||
fn free(self: &const BufMap, value: []const u8) void {
|
||||
self.hash_map.allocator.free(value);
|
||||
}
|
||||
|
||||
fn copy(self: &BufMap, value: []const u8) ![]const u8 {
|
||||
fn copy(self: &const BufMap, value: []const u8) ![]const u8 {
|
||||
return mem.dupe(self.hash_map.allocator, u8, value);
|
||||
}
|
||||
};
|
||||
|
||||
+23
-5
@@ -1,6 +1,8 @@
|
||||
const std = @import("index.zig");
|
||||
const HashMap = @import("hash_map.zig").HashMap;
|
||||
const mem = @import("mem.zig");
|
||||
const Allocator = mem.Allocator;
|
||||
const assert = std.debug.assert;
|
||||
|
||||
pub const BufSet = struct {
|
||||
hash_map: BufSetHashMap,
|
||||
@@ -14,10 +16,10 @@ pub const BufSet = struct {
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn deinit(self: &BufSet) void {
|
||||
pub fn deinit(self: &const BufSet) void {
|
||||
var it = self.hash_map.iterator();
|
||||
while (true) {
|
||||
const entry = it.next() ?? break;
|
||||
const entry = it.next() ?? break;
|
||||
self.free(entry.key);
|
||||
}
|
||||
|
||||
@@ -38,7 +40,7 @@ pub const BufSet = struct {
|
||||
}
|
||||
|
||||
pub fn count(self: &const BufSet) usize {
|
||||
return self.hash_map.size;
|
||||
return self.hash_map.count();
|
||||
}
|
||||
|
||||
pub fn iterator(self: &const BufSet) BufSetHashMap.Iterator {
|
||||
@@ -49,14 +51,30 @@ pub const BufSet = struct {
|
||||
return self.hash_map.allocator;
|
||||
}
|
||||
|
||||
fn free(self: &BufSet, value: []const u8) void {
|
||||
fn free(self: &const BufSet, value: []const u8) void {
|
||||
self.hash_map.allocator.free(value);
|
||||
}
|
||||
|
||||
fn copy(self: &BufSet, value: []const u8) ![]const u8 {
|
||||
fn copy(self: &const BufSet, value: []const u8) ![]const u8 {
|
||||
const result = try self.hash_map.allocator.alloc(u8, value.len);
|
||||
mem.copy(u8, result, value);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
test "BufSet" {
|
||||
var direct_allocator = std.heap.DirectAllocator.init();
|
||||
defer direct_allocator.deinit();
|
||||
|
||||
var bufset = BufSet.init(&direct_allocator.allocator);
|
||||
defer bufset.deinit();
|
||||
|
||||
try bufset.put("x");
|
||||
assert(bufset.count() == 1);
|
||||
bufset.delete("x");
|
||||
assert(bufset.count() == 0);
|
||||
|
||||
try bufset.put("x");
|
||||
try bufset.put("y");
|
||||
try bufset.put("z");
|
||||
}
|
||||
|
||||
+2
-2
@@ -66,7 +66,7 @@ pub const Buffer = struct {
|
||||
self.list.deinit();
|
||||
}
|
||||
|
||||
pub fn toSlice(self: &Buffer) []u8 {
|
||||
pub fn toSlice(self: &const Buffer) []u8 {
|
||||
return self.list.toSlice()[0..self.len()];
|
||||
}
|
||||
|
||||
@@ -166,5 +166,5 @@ test "simple Buffer" {
|
||||
assert(buf.endsWith("orld"));
|
||||
|
||||
try buf2.resize(4);
|
||||
assert(buf.startsWith(buf2.toSliceConst()));
|
||||
assert(buf.startsWith(buf2.toSlice()));
|
||||
}
|
||||
|
||||
+57
-5
@@ -54,6 +54,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
}
|
||||
unreachable; // no next item
|
||||
}
|
||||
|
||||
// Reset the iterator to the initial index
|
||||
pub fn reset(it: &Iterator) void {
|
||||
it.count = 0;
|
||||
it.index = 0;
|
||||
// Resetting the modification count too
|
||||
it.initial_modification_count = it.hm.modification_count;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn init(allocator: &Allocator) Self {
|
||||
@@ -66,7 +74,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(hm: &Self) void {
|
||||
pub fn deinit(hm: &const Self) void {
|
||||
hm.allocator.free(hm.entries);
|
||||
}
|
||||
|
||||
@@ -79,6 +87,10 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
hm.incrementModificationCount();
|
||||
}
|
||||
|
||||
pub fn count(hm: &const Self) usize {
|
||||
return hm.size;
|
||||
}
|
||||
|
||||
/// Returns the value that was already there.
|
||||
pub fn put(hm: &Self, key: K, value: &const V) !?V {
|
||||
if (hm.entries.len == 0) {
|
||||
@@ -102,14 +114,14 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
return hm.internalPut(key, value);
|
||||
}
|
||||
|
||||
pub fn get(hm: &Self, key: K) ?&Entry {
|
||||
pub fn get(hm: &const Self, key: K) ?&Entry {
|
||||
if (hm.entries.len == 0) {
|
||||
return null;
|
||||
}
|
||||
return hm.internalGet(key);
|
||||
}
|
||||
|
||||
pub fn contains(hm: &Self, key: K) bool {
|
||||
pub fn contains(hm: &const Self, key: K) bool {
|
||||
return hm.get(key) != null;
|
||||
}
|
||||
|
||||
@@ -218,7 +230,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
unreachable; // put into a full map
|
||||
}
|
||||
|
||||
fn internalGet(hm: &Self, key: K) ?&Entry {
|
||||
fn internalGet(hm: &const Self, key: K) ?&Entry {
|
||||
const start_index = hm.keyToIndex(key);
|
||||
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
|
||||
const index = (start_index + roll_over) % hm.entries.len;
|
||||
@@ -230,7 +242,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
|
||||
return null;
|
||||
}
|
||||
|
||||
fn keyToIndex(hm: &Self, key: K) usize {
|
||||
fn keyToIndex(hm: &const Self, key: K) usize {
|
||||
return usize(hash(key)) % hm.entries.len;
|
||||
}
|
||||
};
|
||||
@@ -252,12 +264,52 @@ test "basic hash map usage" {
|
||||
assert(??(map.put(5, 66) catch unreachable) == 55);
|
||||
assert(??(map.put(5, 55) catch unreachable) == 66);
|
||||
|
||||
assert(map.contains(2));
|
||||
assert((??map.get(2)).value == 22);
|
||||
_ = map.remove(2);
|
||||
assert(map.remove(2) == null);
|
||||
assert(map.get(2) == null);
|
||||
}
|
||||
|
||||
test "iterator hash map" {
|
||||
var direct_allocator = std.heap.DirectAllocator.init();
|
||||
defer direct_allocator.deinit();
|
||||
|
||||
var reset_map = HashMap(i32, i32, hash_i32, eql_i32).init(&direct_allocator.allocator);
|
||||
defer reset_map.deinit();
|
||||
|
||||
assert((reset_map.put(1, 11) catch unreachable) == null);
|
||||
assert((reset_map.put(2, 22) catch unreachable) == null);
|
||||
assert((reset_map.put(3, 33) catch unreachable) == null);
|
||||
|
||||
var keys = []i32 { 1, 2, 3 };
|
||||
var values = []i32 { 11, 22, 33 };
|
||||
|
||||
var it = reset_map.iterator();
|
||||
var count : usize = 0;
|
||||
while (it.next()) |next| {
|
||||
assert(next.key == keys[count]);
|
||||
assert(next.value == values[count]);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
assert(count == 3);
|
||||
assert(it.next() == null);
|
||||
it.reset();
|
||||
count = 0;
|
||||
while (it.next()) |next| {
|
||||
assert(next.key == keys[count]);
|
||||
assert(next.value == values[count]);
|
||||
count += 1;
|
||||
if (count == 2) break;
|
||||
}
|
||||
|
||||
it.reset();
|
||||
var entry = ?? it.next();
|
||||
assert(entry.key == keys[0]);
|
||||
assert(entry.value == values[0]);
|
||||
}
|
||||
|
||||
fn hash_i32(x: i32) u32 {
|
||||
return @bitCast(u32, x);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ pub const BufferOutStream = @import("buffer.zig").BufferOutStream;
|
||||
pub const HashMap = @import("hash_map.zig").HashMap;
|
||||
pub const LinkedList = @import("linked_list.zig").LinkedList;
|
||||
pub const IntrusiveLinkedList = @import("linked_list.zig").IntrusiveLinkedList;
|
||||
pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
|
||||
|
||||
pub const atomic = @import("atomic/index.zig");
|
||||
pub const base64 = @import("base64.zig");
|
||||
@@ -23,6 +24,7 @@ pub const fmt = @import("fmt/index.zig");
|
||||
pub const hash = @import("hash/index.zig");
|
||||
pub const heap = @import("heap.zig");
|
||||
pub const io = @import("io.zig");
|
||||
pub const json = @import("json.zig");
|
||||
pub const macho = @import("macho.zig");
|
||||
pub const math = @import("math/index.zig");
|
||||
pub const mem = @import("mem.zig");
|
||||
@@ -42,6 +44,7 @@ test "std" {
|
||||
_ = @import("buffer.zig");
|
||||
_ = @import("hash_map.zig");
|
||||
_ = @import("linked_list.zig");
|
||||
_ = @import("segmented_list.zig");
|
||||
|
||||
_ = @import("base64.zig");
|
||||
_ = @import("build.zig");
|
||||
@@ -56,6 +59,7 @@ test "std" {
|
||||
_ = @import("fmt/index.zig");
|
||||
_ = @import("hash/index.zig");
|
||||
_ = @import("io.zig");
|
||||
_ = @import("json.zig");
|
||||
_ = @import("macho.zig");
|
||||
_ = @import("math/index.zig");
|
||||
_ = @import("mem.zig");
|
||||
|
||||
+1304
@@ -0,0 +1,1304 @@
|
||||
// JSON parser conforming to RFC8259.
|
||||
//
|
||||
// https://tools.ietf.org/html/rfc8259
|
||||
|
||||
const std = @import("index.zig");
|
||||
const mem = std.mem;
|
||||
|
||||
const u1 = @IntType(false, 1);
|
||||
const u256 = @IntType(false, 256);
|
||||
|
||||
// A single token slice into the parent string.
|
||||
//
|
||||
// Use `token.slice()` on the inptu at the current position to get the current slice.
|
||||
pub const Token = struct {
|
||||
id: Id,
|
||||
// How many bytes do we skip before counting
|
||||
offset: u1,
|
||||
// Whether string contains a \uXXXX sequence and cannot be zero-copied
|
||||
string_has_escape: bool,
|
||||
// Whether number is simple and can be represented by an integer (i.e. no `.` or `e`)
|
||||
number_is_integer: bool,
|
||||
// How many bytes from the current position behind the start of this token is.
|
||||
count: usize,
|
||||
|
||||
pub const Id = enum {
|
||||
ObjectBegin,
|
||||
ObjectEnd,
|
||||
ArrayBegin,
|
||||
ArrayEnd,
|
||||
String,
|
||||
Number,
|
||||
True,
|
||||
False,
|
||||
Null,
|
||||
};
|
||||
|
||||
pub fn init(id: Id, count: usize, offset: u1) Token {
|
||||
return Token {
|
||||
.id = id,
|
||||
.offset = offset,
|
||||
.string_has_escape = false,
|
||||
.number_is_integer = true,
|
||||
.count = count,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initString(count: usize, has_unicode_escape: bool) Token {
|
||||
return Token {
|
||||
.id = Id.String,
|
||||
.offset = 0,
|
||||
.string_has_escape = has_unicode_escape,
|
||||
.number_is_integer = true,
|
||||
.count = count,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn initNumber(count: usize, number_is_integer: bool) Token {
|
||||
return Token {
|
||||
.id = Id.Number,
|
||||
.offset = 0,
|
||||
.string_has_escape = false,
|
||||
.number_is_integer = number_is_integer,
|
||||
.count = count,
|
||||
};
|
||||
}
|
||||
|
||||
// A marker token is a zero-length
|
||||
pub fn initMarker(id: Id) Token {
|
||||
return Token {
|
||||
.id = id,
|
||||
.offset = 0,
|
||||
.string_has_escape = false,
|
||||
.number_is_integer = true,
|
||||
.count = 0,
|
||||
};
|
||||
}
|
||||
|
||||
// Slice into the underlying input string.
|
||||
pub fn slice(self: &const Token, input: []const u8, i: usize) []const u8 {
|
||||
return input[i + self.offset - self.count .. i + self.offset];
|
||||
}
|
||||
};
|
||||
|
||||
// A small streaming JSON parser. This accepts input one byte at a time and returns tokens as
|
||||
// they are encountered. No copies or allocations are performed during parsing and the entire
|
||||
// parsing state requires ~40-50 bytes of stack space.
|
||||
//
|
||||
// Conforms strictly to RFC8529.
|
||||
const StreamingJsonParser = struct {
|
||||
// Current state
|
||||
state: State,
|
||||
// How many bytes we have counted for the current token
|
||||
count: usize,
|
||||
// What state to follow after parsing a string (either property or value string)
|
||||
after_string_state: State,
|
||||
// What state to follow after parsing a value (either top-level or value end)
|
||||
after_value_state: State,
|
||||
// If we stopped now, would the complete parsed string to now be a valid json string
|
||||
complete: bool,
|
||||
// Current token flags to pass through to the next generated, see Token.
|
||||
string_has_escape: bool,
|
||||
number_is_integer: bool,
|
||||
|
||||
// Bit-stack for nested object/map literals (max 255 nestings).
|
||||
stack: u256,
|
||||
stack_used: u8,
|
||||
|
||||
const object_bit = 0;
|
||||
const array_bit = 1;
|
||||
const max_stack_size = @maxValue(u8);
|
||||
|
||||
pub fn init() StreamingJsonParser {
|
||||
var p: StreamingJsonParser = undefined;
|
||||
p.reset();
|
||||
return p;
|
||||
}
|
||||
|
||||
pub fn reset(p: &StreamingJsonParser) void {
|
||||
p.state = State.TopLevelBegin;
|
||||
p.count = 0;
|
||||
// Set before ever read in main transition function
|
||||
p.after_string_state = undefined;
|
||||
p.after_value_state = State.ValueEnd; // handle end of values normally
|
||||
p.stack = 0;
|
||||
p.stack_used = 0;
|
||||
p.complete = false;
|
||||
p.string_has_escape = false;
|
||||
p.number_is_integer = true;
|
||||
}
|
||||
|
||||
pub const State = enum {
|
||||
// These must be first with these explicit values as we rely on them for indexing the
|
||||
// bit-stack directly and avoiding a branch.
|
||||
ObjectSeparator = 0,
|
||||
ValueEnd = 1,
|
||||
|
||||
TopLevelBegin,
|
||||
TopLevelEnd,
|
||||
|
||||
ValueBegin,
|
||||
ValueBeginNoClosing,
|
||||
|
||||
String,
|
||||
StringUtf8Byte3,
|
||||
StringUtf8Byte2,
|
||||
StringUtf8Byte1,
|
||||
StringEscapeCharacter,
|
||||
StringEscapeHexUnicode4,
|
||||
StringEscapeHexUnicode3,
|
||||
StringEscapeHexUnicode2,
|
||||
StringEscapeHexUnicode1,
|
||||
|
||||
Number,
|
||||
NumberMaybeDotOrExponent,
|
||||
NumberMaybeDigitOrDotOrExponent,
|
||||
NumberFractionalRequired,
|
||||
NumberFractional,
|
||||
NumberMaybeExponent,
|
||||
NumberExponent,
|
||||
NumberExponentDigitsRequired,
|
||||
NumberExponentDigits,
|
||||
|
||||
TrueLiteral1,
|
||||
TrueLiteral2,
|
||||
TrueLiteral3,
|
||||
|
||||
FalseLiteral1,
|
||||
FalseLiteral2,
|
||||
FalseLiteral3,
|
||||
FalseLiteral4,
|
||||
|
||||
NullLiteral1,
|
||||
NullLiteral2,
|
||||
NullLiteral3,
|
||||
|
||||
// Only call this function to generate array/object final state.
|
||||
pub fn fromInt(x: var) State {
|
||||
std.debug.assert(x == 0 or x == 1);
|
||||
const T = @TagType(State);
|
||||
return State(T(x));
|
||||
}
|
||||
};
|
||||
|
||||
pub const Error = error {
|
||||
InvalidTopLevel,
|
||||
TooManyNestedItems,
|
||||
TooManyClosingItems,
|
||||
InvalidValueBegin,
|
||||
InvalidValueEnd,
|
||||
UnbalancedBrackets,
|
||||
UnbalancedBraces,
|
||||
UnexpectedClosingBracket,
|
||||
UnexpectedClosingBrace,
|
||||
InvalidNumber,
|
||||
InvalidSeparator,
|
||||
InvalidLiteral,
|
||||
InvalidEscapeCharacter,
|
||||
InvalidUnicodeHexSymbol,
|
||||
InvalidUtf8Byte,
|
||||
InvalidTopLevelTrailing,
|
||||
InvalidControlCharacter,
|
||||
};
|
||||
|
||||
// Give another byte to the parser and obtain any new tokens. This may (rarely) return two
|
||||
// tokens. token2 is always null if token1 is null.
|
||||
//
|
||||
// There is currently no error recovery on a bad stream.
|
||||
pub fn feed(p: &StreamingJsonParser, c: u8, token1: &?Token, token2: &?Token) Error!void {
|
||||
*token1 = null;
|
||||
*token2 = null;
|
||||
p.count += 1;
|
||||
|
||||
// unlikely
|
||||
if (try p.transition(c, token1)) {
|
||||
_ = try p.transition(c, token2);
|
||||
}
|
||||
}
|
||||
|
||||
// Perform a single transition on the state machine and return any possible token.
|
||||
fn transition(p: &StreamingJsonParser, c: u8, token: &?Token) Error!bool {
|
||||
switch (p.state) {
|
||||
State.TopLevelBegin => switch (c) {
|
||||
'{' => {
|
||||
p.stack <<= 1;
|
||||
p.stack |= object_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ObjectSeparator;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ObjectBegin);
|
||||
},
|
||||
'[' => {
|
||||
p.stack <<= 1;
|
||||
p.stack |= array_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ValueEnd;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ArrayBegin);
|
||||
},
|
||||
'-' => {
|
||||
p.number_is_integer = true;
|
||||
p.state = State.Number;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
'0' => {
|
||||
p.number_is_integer = true;
|
||||
p.state = State.NumberMaybeDotOrExponent;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
'1' ... '9' => {
|
||||
p.number_is_integer = true;
|
||||
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
'"' => {
|
||||
p.state = State.String;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
// We don't actually need the following since after_value_state should override.
|
||||
p.after_string_state = State.ValueEnd;
|
||||
p.string_has_escape = false;
|
||||
p.count = 0;
|
||||
},
|
||||
't' => {
|
||||
p.state = State.TrueLiteral1;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
'f' => {
|
||||
p.state = State.FalseLiteral1;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
'n' => {
|
||||
p.state = State.NullLiteral1;
|
||||
p.after_value_state = State.TopLevelEnd;
|
||||
p.count = 0;
|
||||
},
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidTopLevel;
|
||||
},
|
||||
},
|
||||
|
||||
State.TopLevelEnd => switch (c) {
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidTopLevelTrailing;
|
||||
},
|
||||
},
|
||||
|
||||
State.ValueBegin => switch (c) {
|
||||
// NOTE: These are shared in ValueEnd as well, think we can reorder states to
|
||||
// be a bit clearer and avoid this duplication.
|
||||
'}' => {
|
||||
// unlikely
|
||||
if (p.stack & 1 != object_bit) {
|
||||
return error.UnexpectedClosingBracket;
|
||||
}
|
||||
if (p.stack_used == 0) {
|
||||
return error.TooManyClosingItems;
|
||||
}
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.fromInt(p.stack & 1);
|
||||
|
||||
p.stack >>= 1;
|
||||
p.stack_used -= 1;
|
||||
|
||||
switch (p.stack_used) {
|
||||
0 => {
|
||||
p.complete = true;
|
||||
p.state = State.TopLevelEnd;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
|
||||
*token = Token.initMarker(Token.Id.ObjectEnd);
|
||||
},
|
||||
']' => {
|
||||
if (p.stack & 1 != array_bit) {
|
||||
return error.UnexpectedClosingBrace;
|
||||
}
|
||||
if (p.stack_used == 0) {
|
||||
return error.TooManyClosingItems;
|
||||
}
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.fromInt(p.stack & 1);
|
||||
|
||||
p.stack >>= 1;
|
||||
p.stack_used -= 1;
|
||||
|
||||
switch (p.stack_used) {
|
||||
0 => {
|
||||
p.complete = true;
|
||||
p.state = State.TopLevelEnd;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
|
||||
*token = Token.initMarker(Token.Id.ArrayEnd);
|
||||
},
|
||||
'{' => {
|
||||
if (p.stack_used == max_stack_size) {
|
||||
return error.TooManyNestedItems;
|
||||
}
|
||||
|
||||
p.stack <<= 1;
|
||||
p.stack |= object_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ObjectSeparator;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ObjectBegin);
|
||||
},
|
||||
'[' => {
|
||||
if (p.stack_used == max_stack_size) {
|
||||
return error.TooManyNestedItems;
|
||||
}
|
||||
|
||||
p.stack <<= 1;
|
||||
p.stack |= array_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ValueEnd;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ArrayBegin);
|
||||
},
|
||||
'-' => {
|
||||
p.state = State.Number;
|
||||
p.count = 0;
|
||||
},
|
||||
'0' => {
|
||||
p.state = State.NumberMaybeDotOrExponent;
|
||||
p.count = 0;
|
||||
},
|
||||
'1' ... '9' => {
|
||||
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
||||
p.count = 0;
|
||||
},
|
||||
'"' => {
|
||||
p.state = State.String;
|
||||
p.count = 0;
|
||||
},
|
||||
't' => {
|
||||
p.state = State.TrueLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
'f' => {
|
||||
p.state = State.FalseLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
'n' => {
|
||||
p.state = State.NullLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidValueBegin;
|
||||
},
|
||||
},
|
||||
|
||||
// TODO: A bit of duplication here and in the following state, redo.
|
||||
State.ValueBeginNoClosing => switch (c) {
|
||||
'{' => {
|
||||
if (p.stack_used == max_stack_size) {
|
||||
return error.TooManyNestedItems;
|
||||
}
|
||||
|
||||
p.stack <<= 1;
|
||||
p.stack |= object_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ObjectSeparator;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ObjectBegin);
|
||||
},
|
||||
'[' => {
|
||||
if (p.stack_used == max_stack_size) {
|
||||
return error.TooManyNestedItems;
|
||||
}
|
||||
|
||||
p.stack <<= 1;
|
||||
p.stack |= array_bit;
|
||||
p.stack_used += 1;
|
||||
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ValueEnd;
|
||||
|
||||
*token = Token.initMarker(Token.Id.ArrayBegin);
|
||||
},
|
||||
'-' => {
|
||||
p.state = State.Number;
|
||||
p.count = 0;
|
||||
},
|
||||
'0' => {
|
||||
p.state = State.NumberMaybeDotOrExponent;
|
||||
p.count = 0;
|
||||
},
|
||||
'1' ... '9' => {
|
||||
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
||||
p.count = 0;
|
||||
},
|
||||
'"' => {
|
||||
p.state = State.String;
|
||||
p.count = 0;
|
||||
},
|
||||
't' => {
|
||||
p.state = State.TrueLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
'f' => {
|
||||
p.state = State.FalseLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
'n' => {
|
||||
p.state = State.NullLiteral1;
|
||||
p.count = 0;
|
||||
},
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidValueBegin;
|
||||
},
|
||||
},
|
||||
|
||||
State.ValueEnd => switch (c) {
|
||||
',' => {
|
||||
p.after_string_state = State.fromInt(p.stack & 1);
|
||||
p.state = State.ValueBeginNoClosing;
|
||||
},
|
||||
']' => {
|
||||
if (p.stack_used == 0) {
|
||||
return error.UnbalancedBrackets;
|
||||
}
|
||||
|
||||
p.state = State.ValueEnd;
|
||||
p.after_string_state = State.fromInt(p.stack & 1);
|
||||
|
||||
p.stack >>= 1;
|
||||
p.stack_used -= 1;
|
||||
|
||||
if (p.stack_used == 0) {
|
||||
p.complete = true;
|
||||
p.state = State.TopLevelEnd;
|
||||
}
|
||||
|
||||
*token = Token.initMarker(Token.Id.ArrayEnd);
|
||||
},
|
||||
'}' => {
|
||||
if (p.stack_used == 0) {
|
||||
return error.UnbalancedBraces;
|
||||
}
|
||||
|
||||
p.state = State.ValueEnd;
|
||||
p.after_string_state = State.fromInt(p.stack & 1);
|
||||
|
||||
p.stack >>= 1;
|
||||
p.stack_used -= 1;
|
||||
|
||||
if (p.stack_used == 0) {
|
||||
p.complete = true;
|
||||
p.state = State.TopLevelEnd;
|
||||
}
|
||||
|
||||
*token = Token.initMarker(Token.Id.ObjectEnd);
|
||||
},
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidValueEnd;
|
||||
},
|
||||
},
|
||||
|
||||
State.ObjectSeparator => switch (c) {
|
||||
':' => {
|
||||
p.state = State.ValueBegin;
|
||||
p.after_string_state = State.ValueEnd;
|
||||
},
|
||||
0x09, 0x0A, 0x0D, 0x20 => {
|
||||
// whitespace
|
||||
},
|
||||
else => {
|
||||
return error.InvalidSeparator;
|
||||
},
|
||||
},
|
||||
|
||||
State.String => switch (c) {
|
||||
0x00 ... 0x1F => {
|
||||
return error.InvalidControlCharacter;
|
||||
},
|
||||
'"' => {
|
||||
p.state = p.after_string_state;
|
||||
if (p.after_value_state == State.TopLevelEnd) {
|
||||
p.state = State.TopLevelEnd;
|
||||
p.complete = true;
|
||||
}
|
||||
|
||||
*token = Token.initString(p.count - 1, p.string_has_escape);
|
||||
},
|
||||
'\\' => {
|
||||
p.state = State.StringEscapeCharacter;
|
||||
},
|
||||
0x20, 0x21, 0x23 ... 0x5B, 0x5D ... 0x7F => {
|
||||
// non-control ascii
|
||||
},
|
||||
0xC0 ... 0xDF => {
|
||||
p.state = State.StringUtf8Byte1;
|
||||
},
|
||||
0xE0 ... 0xEF => {
|
||||
p.state = State.StringUtf8Byte2;
|
||||
},
|
||||
0xF0 ... 0xFF => {
|
||||
p.state = State.StringUtf8Byte3;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidUtf8Byte;
|
||||
},
|
||||
},
|
||||
|
||||
State.StringUtf8Byte3 => switch (c >> 6) {
|
||||
0b10 => p.state = State.StringUtf8Byte2,
|
||||
else => return error.InvalidUtf8Byte,
|
||||
},
|
||||
|
||||
State.StringUtf8Byte2 => switch (c >> 6) {
|
||||
0b10 => p.state = State.StringUtf8Byte1,
|
||||
else => return error.InvalidUtf8Byte,
|
||||
},
|
||||
|
||||
State.StringUtf8Byte1 => switch (c >> 6) {
|
||||
0b10 => p.state = State.String,
|
||||
else => return error.InvalidUtf8Byte,
|
||||
},
|
||||
|
||||
State.StringEscapeCharacter => switch (c) {
|
||||
// NOTE: '/' is allowed as an escaped character but it also is allowed
|
||||
// as unescaped according to the RFC. There is a reported errata which suggests
|
||||
// removing the non-escaped variant but it makes more sense to simply disallow
|
||||
// it as an escape code here.
|
||||
//
|
||||
// The current JSONTestSuite tests rely on both of this behaviour being present
|
||||
// however, so we default to the status quo where both are accepted until this
|
||||
// is further clarified.
|
||||
'"', '\\', '/', 'b', 'f', 'n', 'r', 't' => {
|
||||
p.string_has_escape = true;
|
||||
p.state = State.String;
|
||||
},
|
||||
'u' => {
|
||||
p.string_has_escape = true;
|
||||
p.state = State.StringEscapeHexUnicode4;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidEscapeCharacter;
|
||||
},
|
||||
},
|
||||
|
||||
State.StringEscapeHexUnicode4 => switch (c) {
|
||||
'0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
|
||||
p.state = State.StringEscapeHexUnicode3;
|
||||
},
|
||||
else => return error.InvalidUnicodeHexSymbol,
|
||||
},
|
||||
|
||||
State.StringEscapeHexUnicode3 => switch (c) {
|
||||
'0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
|
||||
p.state = State.StringEscapeHexUnicode2;
|
||||
},
|
||||
else => return error.InvalidUnicodeHexSymbol,
|
||||
},
|
||||
|
||||
State.StringEscapeHexUnicode2 => switch (c) {
|
||||
'0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
|
||||
p.state = State.StringEscapeHexUnicode1;
|
||||
},
|
||||
else => return error.InvalidUnicodeHexSymbol,
|
||||
},
|
||||
|
||||
State.StringEscapeHexUnicode1 => switch (c) {
|
||||
'0' ... '9', 'A' ... 'F', 'a' ... 'f' => {
|
||||
p.state = State.String;
|
||||
},
|
||||
else => return error.InvalidUnicodeHexSymbol,
|
||||
},
|
||||
|
||||
State.Number => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'0' => {
|
||||
p.state = State.NumberMaybeDotOrExponent;
|
||||
},
|
||||
'1' ... '9' => {
|
||||
p.state = State.NumberMaybeDigitOrDotOrExponent;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidNumber;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberMaybeDotOrExponent => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'.' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberFractionalRequired;
|
||||
},
|
||||
'e', 'E' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberExponent;
|
||||
},
|
||||
else => {
|
||||
p.state = p.after_value_state;
|
||||
*token = Token.initNumber(p.count, p.number_is_integer);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberMaybeDigitOrDotOrExponent => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'.' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberFractionalRequired;
|
||||
},
|
||||
'e', 'E' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberExponent;
|
||||
},
|
||||
'0' ... '9' => {
|
||||
// another digit
|
||||
},
|
||||
else => {
|
||||
p.state = p.after_value_state;
|
||||
*token = Token.initNumber(p.count, p.number_is_integer);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberFractionalRequired => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'0' ... '9' => {
|
||||
p.state = State.NumberFractional;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidNumber;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberFractional => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'0' ... '9' => {
|
||||
// another digit
|
||||
},
|
||||
'e', 'E' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberExponent;
|
||||
},
|
||||
else => {
|
||||
p.state = p.after_value_state;
|
||||
*token = Token.initNumber(p.count, p.number_is_integer);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberMaybeExponent => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'e', 'E' => {
|
||||
p.number_is_integer = false;
|
||||
p.state = State.NumberExponent;
|
||||
},
|
||||
else => {
|
||||
p.state = p.after_value_state;
|
||||
*token = Token.initNumber(p.count, p.number_is_integer);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.NumberExponent => switch (c) {
|
||||
'-', '+', => {
|
||||
p.complete = false;
|
||||
p.state = State.NumberExponentDigitsRequired;
|
||||
},
|
||||
'0' ... '9' => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
p.state = State.NumberExponentDigits;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidNumber;
|
||||
},
|
||||
},
|
||||
|
||||
State.NumberExponentDigitsRequired => switch (c) {
|
||||
'0' ... '9' => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
p.state = State.NumberExponentDigits;
|
||||
},
|
||||
else => {
|
||||
return error.InvalidNumber;
|
||||
},
|
||||
},
|
||||
|
||||
State.NumberExponentDigits => {
|
||||
p.complete = p.after_value_state == State.TopLevelEnd;
|
||||
switch (c) {
|
||||
'0' ... '9' => {
|
||||
// another digit
|
||||
},
|
||||
else => {
|
||||
p.state = p.after_value_state;
|
||||
*token = Token.initNumber(p.count, p.number_is_integer);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
State.TrueLiteral1 => switch (c) {
|
||||
'r' => p.state = State.TrueLiteral2,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.TrueLiteral2 => switch (c) {
|
||||
'u' => p.state = State.TrueLiteral3,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.TrueLiteral3 => switch (c) {
|
||||
'e' => {
|
||||
p.state = p.after_value_state;
|
||||
p.complete = p.state == State.TopLevelEnd;
|
||||
*token = Token.init(Token.Id.True, p.count + 1, 1);
|
||||
},
|
||||
else => {
|
||||
return error.InvalidLiteral;
|
||||
},
|
||||
},
|
||||
|
||||
State.FalseLiteral1 => switch (c) {
|
||||
'a' => p.state = State.FalseLiteral2,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.FalseLiteral2 => switch (c) {
|
||||
'l' => p.state = State.FalseLiteral3,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.FalseLiteral3 => switch (c) {
|
||||
's' => p.state = State.FalseLiteral4,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.FalseLiteral4 => switch (c) {
|
||||
'e' => {
|
||||
p.state = p.after_value_state;
|
||||
p.complete = p.state == State.TopLevelEnd;
|
||||
*token = Token.init(Token.Id.False, p.count + 1, 1);
|
||||
},
|
||||
else => {
|
||||
return error.InvalidLiteral;
|
||||
},
|
||||
},
|
||||
|
||||
State.NullLiteral1 => switch (c) {
|
||||
'u' => p.state = State.NullLiteral2,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.NullLiteral2 => switch (c) {
|
||||
'l' => p.state = State.NullLiteral3,
|
||||
else => return error.InvalidLiteral,
|
||||
},
|
||||
|
||||
State.NullLiteral3 => switch (c) {
|
||||
'l' => {
|
||||
p.state = p.after_value_state;
|
||||
p.complete = p.state == State.TopLevelEnd;
|
||||
*token = Token.init(Token.Id.Null, p.count + 1, 1);
|
||||
},
|
||||
else => {
|
||||
return error.InvalidLiteral;
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// Validate a JSON string. This does not limit number precision so a decoder may not necessarily
|
||||
// be able to decode the string even if this returns true.
|
||||
pub fn validate(s: []const u8) bool {
|
||||
var p = StreamingJsonParser.init();
|
||||
|
||||
for (s) |c, i| {
|
||||
var token1: ?Token = undefined;
|
||||
var token2: ?Token = undefined;
|
||||
|
||||
p.feed(c, &token1, &token2) catch |err| {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
return p.complete;
|
||||
}
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ArenaAllocator = std.heap.ArenaAllocator;
|
||||
const ArrayList = std.ArrayList;
|
||||
const HashMap = std.HashMap;
|
||||
|
||||
pub const ValueTree = struct {
|
||||
arena: ArenaAllocator,
|
||||
root: Value,
|
||||
|
||||
pub fn deinit(self: &ValueTree) void {
|
||||
self.arena.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
pub const ObjectMap = HashMap([]const u8, Value, mem.hash_slice_u8, mem.eql_slice_u8);
|
||||
|
||||
pub const Value = union(enum) {
|
||||
Null,
|
||||
Bool: bool,
|
||||
Integer: i64,
|
||||
Float: f64,
|
||||
String: []const u8,
|
||||
Array: ArrayList(Value),
|
||||
Object: ObjectMap,
|
||||
|
||||
pub fn dump(self: &const Value) void {
|
||||
switch (*self) {
|
||||
Value.Null => {
|
||||
std.debug.warn("null");
|
||||
},
|
||||
Value.Bool => |inner| {
|
||||
std.debug.warn("{}", inner);
|
||||
},
|
||||
Value.Integer => |inner| {
|
||||
std.debug.warn("{}", inner);
|
||||
},
|
||||
Value.Float => |inner| {
|
||||
std.debug.warn("{.5}", inner);
|
||||
},
|
||||
Value.String => |inner| {
|
||||
std.debug.warn("\"{}\"", inner);
|
||||
},
|
||||
Value.Array => |inner| {
|
||||
var not_first = false;
|
||||
std.debug.warn("[");
|
||||
for (inner.toSliceConst()) |value| {
|
||||
if (not_first) {
|
||||
std.debug.warn(",");
|
||||
}
|
||||
not_first = true;
|
||||
value.dump();
|
||||
}
|
||||
std.debug.warn("]");
|
||||
},
|
||||
Value.Object => |inner| {
|
||||
var not_first = false;
|
||||
std.debug.warn("{{");
|
||||
var it = inner.iterator();
|
||||
|
||||
while (it.next()) |entry| {
|
||||
if (not_first) {
|
||||
std.debug.warn(",");
|
||||
}
|
||||
not_first = true;
|
||||
std.debug.warn("\"{}\":", entry.key);
|
||||
entry.value.dump();
|
||||
}
|
||||
std.debug.warn("}}");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dumpIndent(self: &const Value, indent: usize) void {
|
||||
if (indent == 0) {
|
||||
self.dump();
|
||||
} else {
|
||||
self.dumpIndentLevel(indent, 0);
|
||||
}
|
||||
}
|
||||
|
||||
fn dumpIndentLevel(self: &const Value, indent: usize, level: usize) void {
|
||||
switch (*self) {
|
||||
Value.Null => {
|
||||
std.debug.warn("null");
|
||||
},
|
||||
Value.Bool => |inner| {
|
||||
std.debug.warn("{}", inner);
|
||||
},
|
||||
Value.Integer => |inner| {
|
||||
std.debug.warn("{}", inner);
|
||||
},
|
||||
Value.Float => |inner| {
|
||||
std.debug.warn("{.5}", inner);
|
||||
},
|
||||
Value.String => |inner| {
|
||||
std.debug.warn("\"{}\"", inner);
|
||||
},
|
||||
Value.Array => |inner| {
|
||||
var not_first = false;
|
||||
std.debug.warn("[\n");
|
||||
|
||||
for (inner.toSliceConst()) |value| {
|
||||
if (not_first) {
|
||||
std.debug.warn(",\n");
|
||||
}
|
||||
not_first = true;
|
||||
padSpace(level + indent);
|
||||
value.dumpIndentLevel(indent, level + indent);
|
||||
}
|
||||
std.debug.warn("\n");
|
||||
padSpace(level);
|
||||
std.debug.warn("]");
|
||||
},
|
||||
Value.Object => |inner| {
|
||||
var not_first = false;
|
||||
std.debug.warn("{{\n");
|
||||
var it = inner.iterator();
|
||||
|
||||
while (it.next()) |entry| {
|
||||
if (not_first) {
|
||||
std.debug.warn(",\n");
|
||||
}
|
||||
not_first = true;
|
||||
padSpace(level + indent);
|
||||
std.debug.warn("\"{}\": ", entry.key);
|
||||
entry.value.dumpIndentLevel(indent, level + indent);
|
||||
}
|
||||
std.debug.warn("\n");
|
||||
padSpace(level);
|
||||
std.debug.warn("}}");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn padSpace(indent: usize) void {
|
||||
var i: usize = 0;
|
||||
while (i < indent) : (i += 1) {
|
||||
std.debug.warn(" ");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// A non-stream JSON parser which constructs a tree of Value's.
|
||||
const JsonParser = struct {
|
||||
allocator: &Allocator,
|
||||
state: State,
|
||||
copy_strings: bool,
|
||||
// Stores parent nodes and un-combined Values.
|
||||
stack: ArrayList(Value),
|
||||
|
||||
const State = enum {
|
||||
ObjectKey,
|
||||
ObjectValue,
|
||||
ArrayValue,
|
||||
Simple,
|
||||
};
|
||||
|
||||
pub fn init(allocator: &Allocator, copy_strings: bool) JsonParser {
|
||||
return JsonParser {
|
||||
.allocator = allocator,
|
||||
.state = State.Simple,
|
||||
.copy_strings = copy_strings,
|
||||
.stack = ArrayList(Value).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(p: &JsonParser) void {
|
||||
p.stack.deinit();
|
||||
}
|
||||
|
||||
pub fn reset(p: &JsonParser) void {
|
||||
p.state = State.Simple;
|
||||
p.stack.shrink(0);
|
||||
}
|
||||
|
||||
pub fn parse(p: &JsonParser, input: []const u8) !ValueTree {
|
||||
var mp = StreamingJsonParser.init();
|
||||
|
||||
var arena = ArenaAllocator.init(p.allocator);
|
||||
errdefer arena.deinit();
|
||||
|
||||
for (input) |c, i| {
|
||||
var mt1: ?Token = undefined;
|
||||
var mt2: ?Token = undefined;
|
||||
|
||||
try mp.feed(c, &mt1, &mt2);
|
||||
if (mt1) |t1| {
|
||||
try p.transition(&arena.allocator, input, i, t1);
|
||||
|
||||
if (mt2) |t2| {
|
||||
try p.transition(&arena.allocator, input, i, t2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle top-level lonely number values.
|
||||
{
|
||||
const i = input.len;
|
||||
var mt1: ?Token = undefined;
|
||||
var mt2: ?Token = undefined;
|
||||
|
||||
try mp.feed(' ', &mt1, &mt2);
|
||||
if (mt1) |t1| {
|
||||
try p.transition(&arena.allocator, input, i, t1);
|
||||
}
|
||||
}
|
||||
|
||||
if (!mp.complete) {
|
||||
return error.IncompleteJsonInput;
|
||||
}
|
||||
|
||||
std.debug.assert(p.stack.len == 1);
|
||||
|
||||
return ValueTree {
|
||||
.arena = arena,
|
||||
.root = p.stack.at(0),
|
||||
};
|
||||
}
|
||||
|
||||
// Even though p.allocator exists, we take an explicit allocator so that allocation state
|
||||
// can be cleaned up on error correctly during a `parse` on call.
|
||||
fn transition(p: &JsonParser, allocator: &Allocator, input: []const u8, i: usize, token: &const Token) !void {
|
||||
switch (p.state) {
|
||||
State.ObjectKey => switch (token.id) {
|
||||
Token.Id.ObjectEnd => {
|
||||
if (p.stack.len == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var value = p.stack.pop();
|
||||
try p.pushToParent(value);
|
||||
},
|
||||
Token.Id.String => {
|
||||
try p.stack.append(try p.parseString(allocator, token, input, i));
|
||||
p.state = State.ObjectValue;
|
||||
},
|
||||
else => {
|
||||
unreachable;
|
||||
},
|
||||
},
|
||||
State.ObjectValue => {
|
||||
var object = &p.stack.items[p.stack.len - 2].Object;
|
||||
var key = p.stack.items[p.stack.len - 1].String;
|
||||
|
||||
switch (token.id) {
|
||||
Token.Id.ObjectBegin => {
|
||||
try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.ArrayBegin => {
|
||||
try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
|
||||
p.state = State.ArrayValue;
|
||||
},
|
||||
Token.Id.String => {
|
||||
_ = try object.put(key, try p.parseString(allocator, token, input, i));
|
||||
_ = p.stack.pop();
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.Number => {
|
||||
_ = try object.put(key, try p.parseNumber(token, input, i));
|
||||
_ = p.stack.pop();
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.True => {
|
||||
_ = try object.put(key, Value { .Bool = true });
|
||||
_ = p.stack.pop();
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.False => {
|
||||
_ = try object.put(key, Value { .Bool = false });
|
||||
_ = p.stack.pop();
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.Null => {
|
||||
_ = try object.put(key, Value.Null);
|
||||
_ = p.stack.pop();
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
else => {
|
||||
unreachable;
|
||||
},
|
||||
}
|
||||
},
|
||||
State.ArrayValue => {
|
||||
var array = &p.stack.items[p.stack.len - 1].Array;
|
||||
|
||||
switch (token.id) {
|
||||
Token.Id.ArrayEnd => {
|
||||
if (p.stack.len == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
var value = p.stack.pop();
|
||||
try p.pushToParent(value);
|
||||
},
|
||||
Token.Id.ObjectBegin => {
|
||||
try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.ArrayBegin => {
|
||||
try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
|
||||
p.state = State.ArrayValue;
|
||||
},
|
||||
Token.Id.String => {
|
||||
try array.append(try p.parseString(allocator, token, input, i));
|
||||
},
|
||||
Token.Id.Number => {
|
||||
try array.append(try p.parseNumber(token, input, i));
|
||||
},
|
||||
Token.Id.True => {
|
||||
try array.append(Value { .Bool = true });
|
||||
},
|
||||
Token.Id.False => {
|
||||
try array.append(Value { .Bool = false });
|
||||
},
|
||||
Token.Id.Null => {
|
||||
try array.append(Value.Null);
|
||||
},
|
||||
else => {
|
||||
unreachable;
|
||||
},
|
||||
}
|
||||
},
|
||||
State.Simple => switch (token.id) {
|
||||
Token.Id.ObjectBegin => {
|
||||
try p.stack.append(Value { .Object = ObjectMap.init(allocator) });
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
Token.Id.ArrayBegin => {
|
||||
try p.stack.append(Value { .Array = ArrayList(Value).init(allocator) });
|
||||
p.state = State.ArrayValue;
|
||||
},
|
||||
Token.Id.String => {
|
||||
try p.stack.append(try p.parseString(allocator, token, input, i));
|
||||
},
|
||||
Token.Id.Number => {
|
||||
try p.stack.append(try p.parseNumber(token, input, i));
|
||||
},
|
||||
Token.Id.True => {
|
||||
try p.stack.append(Value { .Bool = true });
|
||||
},
|
||||
Token.Id.False => {
|
||||
try p.stack.append(Value { .Bool = false });
|
||||
},
|
||||
Token.Id.Null => {
|
||||
try p.stack.append(Value.Null);
|
||||
},
|
||||
Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
|
||||
unreachable;
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn pushToParent(p: &JsonParser, value: &const Value) !void {
|
||||
switch (p.stack.at(p.stack.len - 1)) {
|
||||
// Object Parent -> [ ..., object, <key>, value ]
|
||||
Value.String => |key| {
|
||||
_ = p.stack.pop();
|
||||
|
||||
var object = &p.stack.items[p.stack.len - 1].Object;
|
||||
_ = try object.put(key, value);
|
||||
p.state = State.ObjectKey;
|
||||
},
|
||||
// Array Parent -> [ ..., <array>, value ]
|
||||
Value.Array => |*array| {
|
||||
try array.append(value);
|
||||
p.state = State.ArrayValue;
|
||||
},
|
||||
else => {
|
||||
unreachable;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn parseString(p: &JsonParser, allocator: &Allocator, token: &const Token, input: []const u8, i: usize) !Value {
|
||||
// TODO: We don't strictly have to copy values which do not contain any escape
|
||||
// characters if flagged with the option.
|
||||
const slice = token.slice(input, i);
|
||||
return Value { .String = try mem.dupe(p.allocator, u8, slice) };
|
||||
}
|
||||
|
||||
fn parseNumber(p: &JsonParser, token: &const Token, input: []const u8, i: usize) !Value {
|
||||
return if (token.number_is_integer)
|
||||
Value { .Integer = try std.fmt.parseInt(i64, token.slice(input, i), 10) }
|
||||
else
|
||||
@panic("TODO: fmt.parseFloat not yet implemented")
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
const debug = std.debug;
|
||||
|
||||
test "json parser dynamic" {
|
||||
var p = JsonParser.init(std.debug.global_allocator, false);
|
||||
defer p.deinit();
|
||||
|
||||
const s =
|
||||
\\{
|
||||
\\ "Image": {
|
||||
\\ "Width": 800,
|
||||
\\ "Height": 600,
|
||||
\\ "Title": "View from 15th Floor",
|
||||
\\ "Thumbnail": {
|
||||
\\ "Url": "http://www.example.com/image/481989943",
|
||||
\\ "Height": 125,
|
||||
\\ "Width": 100
|
||||
\\ },
|
||||
\\ "Animated" : false,
|
||||
\\ "IDs": [116, 943, 234, 38793]
|
||||
\\ }
|
||||
\\}
|
||||
;
|
||||
|
||||
var tree = try p.parse(s);
|
||||
defer tree.deinit();
|
||||
|
||||
var root = tree.root;
|
||||
|
||||
var image = (??root.Object.get("Image")).value;
|
||||
|
||||
const width = (??image.Object.get("Width")).value;
|
||||
debug.assert(width.Integer == 800);
|
||||
|
||||
const height = (??image.Object.get("Height")).value;
|
||||
debug.assert(height.Integer == 600);
|
||||
|
||||
const title = (??image.Object.get("Title")).value;
|
||||
debug.assert(mem.eql(u8, title.String, "View from 15th Floor"));
|
||||
|
||||
const animated = (??image.Object.get("Animated")).value;
|
||||
debug.assert(animated.Bool == false);
|
||||
}
|
||||
+1942
@@ -0,0 +1,1942 @@
|
||||
// RFC 8529 conformance tests.
|
||||
//
|
||||
// Tests are taken from https://github.com/nst/JSONTestSuite
|
||||
// Read also http://seriot.ch/parsing_json.php for a good overview.
|
||||
|
||||
const std = @import("index.zig");
|
||||
|
||||
fn ok(comptime s: []const u8) void {
|
||||
std.debug.assert(std.json.validate(s));
|
||||
}
|
||||
|
||||
fn err(comptime s: []const u8) void {
|
||||
std.debug.assert(!std.json.validate(s));
|
||||
}
|
||||
|
||||
fn any(comptime s: []const u8) void {
|
||||
std.debug.assert(true);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
test "y_array_arraysWithSpaces" {
|
||||
ok(
|
||||
\\[[] ]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_empty" {
|
||||
ok(
|
||||
\\[]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_empty-string" {
|
||||
ok(
|
||||
\\[""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_ending_with_newline" {
|
||||
ok(
|
||||
\\["a"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_false" {
|
||||
ok(
|
||||
\\[false]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_heterogeneous" {
|
||||
ok(
|
||||
\\[null, 1, "1", {}]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_null" {
|
||||
ok(
|
||||
\\[null]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_with_1_and_newline" {
|
||||
ok(
|
||||
\\[1
|
||||
\\]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_with_leading_space" {
|
||||
ok(
|
||||
\\ [1]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_with_several_null" {
|
||||
ok(
|
||||
\\[1,null,null,null,2]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_array_with_trailing_space" {
|
||||
ok(
|
||||
"[2] "
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_0e+1" {
|
||||
ok(
|
||||
\\[0e+1]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_0e1" {
|
||||
ok(
|
||||
\\[0e1]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_after_space" {
|
||||
ok(
|
||||
\\[ 4]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_double_close_to_zero" {
|
||||
ok(
|
||||
\\[-0.000000000000000000000000000000000000000000000000000000000000000000000000000001]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_int_with_exp" {
|
||||
ok(
|
||||
\\[20e1]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number" {
|
||||
ok(
|
||||
\\[123e65]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_minus_zero" {
|
||||
ok(
|
||||
\\[-0]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_negative_int" {
|
||||
ok(
|
||||
\\[-123]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_negative_one" {
|
||||
ok(
|
||||
\\[-1]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_negative_zero" {
|
||||
ok(
|
||||
\\[-0]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_capital_e" {
|
||||
ok(
|
||||
\\[1E22]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_capital_e_neg_exp" {
|
||||
ok(
|
||||
\\[1E-2]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_capital_e_pos_exp" {
|
||||
ok(
|
||||
\\[1E+2]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_exponent" {
|
||||
ok(
|
||||
\\[123e45]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_fraction_exponent" {
|
||||
ok(
|
||||
\\[123.456e78]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_neg_exp" {
|
||||
ok(
|
||||
\\[1e-2]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_real_pos_exponent" {
|
||||
ok(
|
||||
\\[1e+2]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_simple_int" {
|
||||
ok(
|
||||
\\[123]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_number_simple_real" {
|
||||
ok(
|
||||
\\[123.456789]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_basic" {
|
||||
ok(
|
||||
\\{"asd":"sdf"}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_duplicated_key_and_value" {
|
||||
ok(
|
||||
\\{"a":"b","a":"b"}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_duplicated_key" {
|
||||
ok(
|
||||
\\{"a":"b","a":"c"}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_empty" {
|
||||
ok(
|
||||
\\{}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_empty_key" {
|
||||
ok(
|
||||
\\{"":0}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_escaped_null_in_key" {
|
||||
ok(
|
||||
\\{"foo\u0000bar": 42}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_extreme_numbers" {
|
||||
ok(
|
||||
\\{ "min": -1.0e+28, "max": 1.0e+28 }
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object" {
|
||||
ok(
|
||||
\\{"asd":"sdf", "dfg":"fgh"}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_long_strings" {
|
||||
ok(
|
||||
\\{"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_simple" {
|
||||
ok(
|
||||
\\{"a":[]}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_string_unicode" {
|
||||
ok(
|
||||
\\{"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" }
|
||||
);
|
||||
}
|
||||
|
||||
test "y_object_with_newlines" {
|
||||
ok(
|
||||
\\{
|
||||
\\"a": "b"
|
||||
\\}
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_1_2_3_bytes_UTF-8_sequences" {
|
||||
ok(
|
||||
\\["\u0060\u012a\u12AB"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_accepted_surrogate_pair" {
|
||||
ok(
|
||||
\\["\uD801\udc37"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_accepted_surrogate_pairs" {
|
||||
ok(
|
||||
\\["\ud83d\ude39\ud83d\udc8d"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_allowed_escapes" {
|
||||
ok(
|
||||
\\["\"\\\/\b\f\n\r\t"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_backslash_and_u_escaped_zero" {
|
||||
ok(
|
||||
\\["\\u0000"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_backslash_doublequotes" {
|
||||
ok(
|
||||
\\["\""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_comments" {
|
||||
ok(
|
||||
\\["a/*b*/c/*d//e"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_double_escape_a" {
|
||||
ok(
|
||||
\\["\\a"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_double_escape_n" {
|
||||
ok(
|
||||
\\["\\n"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_escaped_control_character" {
|
||||
ok(
|
||||
\\["\u0012"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_escaped_noncharacter" {
|
||||
ok(
|
||||
\\["\uFFFF"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_in_array" {
|
||||
ok(
|
||||
\\["asd"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_in_array_with_leading_space" {
|
||||
ok(
|
||||
\\[ "asd"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_last_surrogates_1_and_2" {
|
||||
ok(
|
||||
\\["\uDBFF\uDFFF"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_nbsp_uescaped" {
|
||||
ok(
|
||||
\\["new\u00A0line"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_nonCharacterInUTF-8_U+10FFFF" {
|
||||
ok(
|
||||
\\[""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_nonCharacterInUTF-8_U+FFFF" {
|
||||
ok(
|
||||
\\[""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_null_escape" {
|
||||
ok(
|
||||
\\["\u0000"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_one-byte-utf-8" {
|
||||
ok(
|
||||
\\["\u002c"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_pi" {
|
||||
ok(
|
||||
\\["π"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_reservedCharacterInUTF-8_U+1BFFF" {
|
||||
ok(
|
||||
\\[""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_simple_ascii" {
|
||||
ok(
|
||||
\\["asd "]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_space" {
|
||||
ok(
|
||||
\\" "
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF" {
|
||||
ok(
|
||||
\\["\uD834\uDd1e"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_three-byte-utf-8" {
|
||||
ok(
|
||||
\\["\u0821"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_two-byte-utf-8" {
|
||||
ok(
|
||||
\\["\u0123"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_u+2028_line_sep" {
|
||||
ok(
|
||||
\\["
"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_u+2029_par_sep" {
|
||||
ok(
|
||||
\\["
"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_uescaped_newline" {
|
||||
ok(
|
||||
\\["new\u000Aline"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_uEscape" {
|
||||
ok(
|
||||
\\["\u0061\u30af\u30EA\u30b9"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unescaped_char_delete" {
|
||||
ok(
|
||||
\\[""]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_2" {
|
||||
ok(
|
||||
\\["⍂㈴⍂"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicodeEscapedBackslash" {
|
||||
ok(
|
||||
\\["\u005C"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_escaped_double_quote" {
|
||||
ok(
|
||||
\\["\u0022"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode" {
|
||||
ok(
|
||||
\\["\uA66D"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+10FFFE_nonchar" {
|
||||
ok(
|
||||
\\["\uDBFF\uDFFE"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+1FFFE_nonchar" {
|
||||
ok(
|
||||
\\["\uD83F\uDFFE"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+200B_ZERO_WIDTH_SPACE" {
|
||||
ok(
|
||||
\\["\u200B"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+2064_invisible_plus" {
|
||||
ok(
|
||||
\\["\u2064"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+FDD0_nonchar" {
|
||||
ok(
|
||||
\\["\uFDD0"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_unicode_U+FFFE_nonchar" {
|
||||
ok(
|
||||
\\["\uFFFE"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_utf8" {
|
||||
ok(
|
||||
\\["€𝄞"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_string_with_del_character" {
|
||||
ok(
|
||||
\\["aa"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_false" {
|
||||
ok(
|
||||
\\false
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_int" {
|
||||
ok(
|
||||
\\42
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_negative_real" {
|
||||
ok(
|
||||
\\-0.1
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_null" {
|
||||
ok(
|
||||
\\null
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_string" {
|
||||
ok(
|
||||
\\"asd"
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_lonely_true" {
|
||||
ok(
|
||||
\\true
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_string_empty" {
|
||||
ok(
|
||||
\\""
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_trailing_newline" {
|
||||
ok(
|
||||
\\["a"]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_true_in_array" {
|
||||
ok(
|
||||
\\[true]
|
||||
);
|
||||
}
|
||||
|
||||
test "y_structure_whitespace_array" {
|
||||
ok(
|
||||
" [] "
|
||||
);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
test "n_array_1_true_without_comma" {
|
||||
err(
|
||||
\\[1 true]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_a_invalid_utf8" {
|
||||
err(
|
||||
\\[aå]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_colon_instead_of_comma" {
|
||||
err(
|
||||
\\["": 1]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_comma_after_close" {
|
||||
//err(
|
||||
// \\[""],
|
||||
//);
|
||||
}
|
||||
|
||||
test "n_array_comma_and_number" {
|
||||
err(
|
||||
\\[,1]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_double_comma" {
|
||||
err(
|
||||
\\[1,,2]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_double_extra_comma" {
|
||||
err(
|
||||
\\["x",,]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_extra_close" {
|
||||
err(
|
||||
\\["x"]]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_extra_comma" {
|
||||
//err(
|
||||
// \\["",]
|
||||
//);
|
||||
}
|
||||
|
||||
test "n_array_incomplete_invalid_value" {
|
||||
err(
|
||||
\\[x
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_incomplete" {
|
||||
err(
|
||||
\\["x"
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_inner_array_no_comma" {
|
||||
err(
|
||||
\\[3[4]]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_invalid_utf8" {
|
||||
err(
|
||||
\\[ÿ]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_items_separated_by_semicolon" {
|
||||
err(
|
||||
\\[1:2]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_just_comma" {
|
||||
err(
|
||||
\\[,]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_just_minus" {
|
||||
err(
|
||||
\\[-]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_missing_value" {
|
||||
err(
|
||||
\\[ , ""]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_newlines_unclosed" {
|
||||
err(
|
||||
\\["a",
|
||||
\\4
|
||||
\\,1,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
test "n_array_number_and_comma" {
|
||||
err(
|
||||
\\[1,]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_number_and_several_commas" {
|
||||
err(
|
||||
\\[1,,]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_spaces_vertical_tab_formfeed" {
|
||||
err(
|
||||
\\["a"\f]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_star_inside" {
|
||||
err(
|
||||
\\[*]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_unclosed" {
|
||||
err(
|
||||
\\[""
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_unclosed_trailing_comma" {
|
||||
err(
|
||||
\\[1,
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_unclosed_with_new_lines" {
|
||||
err(
|
||||
\\[1,
|
||||
\\1
|
||||
\\,1
|
||||
);
|
||||
}
|
||||
|
||||
test "n_array_unclosed_with_object_inside" {
|
||||
err(
|
||||
\\[{}
|
||||
);
|
||||
}
|
||||
|
||||
test "n_incomplete_false" {
|
||||
err(
|
||||
\\[fals]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_incomplete_null" {
|
||||
err(
|
||||
\\[nul]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_incomplete_true" {
|
||||
err(
|
||||
\\[tru]
|
||||
);
|
||||
}
|
||||
|
||||
test "n_multidigit_number_then_00" {
|
||||
err(
|
||||
\\123 | ||||