-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Metaprogramming - @typeInfo [DONE] #951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
7eab623
One step towards @typeInfo
alexnask fb88f5a
@typeInfo with void payloads now works!
alexnask ec2a3ed
Attempt at adding comptime union field access
alexnask e9309d3
Fixed IntInfo generation.
alexnask 0e5fb03
Added (broken) pointer info, float info
alexnask 189e8e9
PointerInfo child is a pointer to a TypeInfo union, still not working…
alexnask 2d8553c
Fixed PointerInfo generation
alexnask 09d7033
PointerInfo child is known at comptime
alexnask 182a9fa
Added ArrayInfo, NullableInfo, PromiseInfo generation
alexnask 778b931
Fixed comptime union void field access
alexnask d68aea4
Added checks for field name/index mapping in TypeInfo generation. Abs…
alexnask 2606993
Fixed ir_type_info_struct_set_parent for struct parents.
alexnask bc16082
Changed TypeInfo layout.
alexnask dd88d7d
Cleanup
alexnask bb56360
Added TypeInfo cache
alexnask 7a91e47
Reset parent on cached TypeInfo values if we need to.
alexnask f5977f6
Added Enum TypeInfo except for methods
alexnask 4aa5d87
Added ErrorSet TypeInfo generation.
alexnask fbbbee6
Switched to shallow TypeInfo.
alexnask 884e32d
Added ErrorUnion, Union TypeInfo generation
alexnask 9041d0d
Fixed enum tag type detection in TypeInfo generation.
alexnask a2dadbc
Added struct TypeInfo generation.
alexnask 8f703f9
Added Fn TypeInfo generation.
alexnask ea25962
Added BoundFn TypeInfo generation.
alexnask 61b0180
Added definition TypeInfo generation, except for function definitions.
alexnask 9ba4006
Generating TypeInfo's now forces definitions to be resolved.
alexnask af73462
Started work on function definition TypeInfo generation.
alexnask 66aa760
More FnDef TypeInfo generation.
alexnask 013f548
Finished FnDef TypeInfo generation (warning: may be buggy).
alexnask ff1c4e1
Added tests.
alexnask e1535ee
Added typeInfo tests
alexnask 255c0ef
Resolved merge conflict.
alexnask 1b6e973
Added type info tests to behavior test listing
alexnask 7d23941
Fixed type info test, added documentation.
alexnask 5794083
Added typeInfo to langref built_ins
alexnask 849ea61
Small fix.
alexnask File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -6344,6 +6347,190 @@ static void define_builtin_compile_vars(CodeGen *g) { | |
} | ||
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" | ||
" 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 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these asserts are appreciated, and will for sure prevent bugs in the future 👍 |
||
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" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good as is, but just a note, this is going to become a top level export of builtin.zig after #661