authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 15:48:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 15:48:28-05:00
log6769183a9d5f5ec69747f46d4d13c0f8709b2f46
tree273a352de8765f10482336d510a995543ee3d259
parent52c03de5c2495b369ae730ff203e5342e4f33a36
signature Commit is signed but in an unrecognized format.

fix implicit cast error unions with non-optional to optional pointer

and update self hosted compiler for C pointers See #1059

13 files changed, 228 insertions(+), 131 deletions(-)

doc/docgen.zig+1
...@@ -916,6 +916,7 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok...@@ -916,6 +916,7 @@ fn tokenizeAndPrintRaw(docgen_tokenizer: *Tokenizer, out: var, source_token: Tok
916 std.zig.Token.Id.AngleBracketAngleBracketRightEqual,916 std.zig.Token.Id.AngleBracketAngleBracketRightEqual,
917 std.zig.Token.Id.Tilde,917 std.zig.Token.Id.Tilde,
918 std.zig.Token.Id.BracketStarBracket,918 std.zig.Token.Id.BracketStarBracket,
919 std.zig.Token.Id.BracketStarCBracket,
919 => try writeEscaped(out, src[token.start..token.end]),920 => try writeEscaped(out, src[token.start..token.end]),
920921
921 std.zig.Token.Id.Invalid => return parseError(922 std.zig.Token.Id.Invalid => return parseError(
src-self-hosted/codegen.zig+21-21
...@@ -137,10 +137,10 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)...@@ -137,10 +137,10 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
137137
138pub const ObjectFile = struct {138pub const ObjectFile = struct {
139 comp: *Compilation,139 comp: *Compilation,
140 module: llvm.ModuleRef,140 module: *llvm.Module,
141 builder: llvm.BuilderRef,141 builder: *llvm.Builder,
142 dibuilder: *llvm.DIBuilder,142 dibuilder: *llvm.DIBuilder,
143 context: llvm.ContextRef,143 context: *llvm.Context,
144 lock: event.Lock,144 lock: event.Lock,
145 arena: *std.mem.Allocator,145 arena: *std.mem.Allocator,
146146
...@@ -323,7 +323,7 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)...@@ -323,7 +323,7 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
323323
324fn addLLVMAttr(324fn addLLVMAttr(
325 ofile: *ObjectFile,325 ofile: *ObjectFile,
326 val: llvm.ValueRef,326 val: *llvm.Value,
327 attr_index: llvm.AttributeIndex,327 attr_index: llvm.AttributeIndex,
328 attr_name: []const u8,328 attr_name: []const u8,
329) !void {329) !void {
...@@ -335,7 +335,7 @@ fn addLLVMAttr(...@@ -335,7 +335,7 @@ fn addLLVMAttr(
335335
336fn addLLVMAttrStr(336fn addLLVMAttrStr(
337 ofile: *ObjectFile,337 ofile: *ObjectFile,
338 val: llvm.ValueRef,338 val: *llvm.Value,
339 attr_index: llvm.AttributeIndex,339 attr_index: llvm.AttributeIndex,
340 attr_name: []const u8,340 attr_name: []const u8,
341 attr_val: []const u8,341 attr_val: []const u8,
...@@ -351,7 +351,7 @@ fn addLLVMAttrStr(...@@ -351,7 +351,7 @@ fn addLLVMAttrStr(
351}351}
352352
353fn addLLVMAttrInt(353fn addLLVMAttrInt(
354 val: llvm.ValueRef,354 val: *llvm.Value,
355 attr_index: llvm.AttributeIndex,355 attr_index: llvm.AttributeIndex,
356 attr_name: []const u8,356 attr_name: []const u8,
357 attr_val: u64,357 attr_val: u64,
...@@ -362,25 +362,25 @@ fn addLLVMAttrInt(...@@ -362,25 +362,25 @@ fn addLLVMAttrInt(
362 llvm.AddAttributeAtIndex(val, attr_index, llvm_attr);362 llvm.AddAttributeAtIndex(val, attr_index, llvm_attr);
363}363}
364364
365fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8) !void {365fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8) !void {
366 return addLLVMAttr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name);366 return addLLVMAttr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name);
367}367}
368368
369fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: []const u8) !void {369fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8, attr_val: []const u8) !void {
370 return addLLVMAttrStr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);370 return addLLVMAttrStr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
371}371}
372372
373fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: u64) !void {373fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8, attr_val: u64) !void {
374 return addLLVMAttrInt(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);374 return addLLVMAttrInt(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
375}375}
376376
377fn renderLoadUntyped(377fn renderLoadUntyped(
378 ofile: *ObjectFile,378 ofile: *ObjectFile,
379 ptr: llvm.ValueRef,379 ptr: *llvm.Value,
380 alignment: Type.Pointer.Align,380 alignment: Type.Pointer.Align,
381 vol: Type.Pointer.Vol,381 vol: Type.Pointer.Vol,
382 name: [*]const u8,382 name: [*]const u8,
383) !llvm.ValueRef {383) !*llvm.Value {
384 const result = llvm.BuildLoad(ofile.builder, ptr, name) orelse return error.OutOfMemory;384 const result = llvm.BuildLoad(ofile.builder, ptr, name) orelse return error.OutOfMemory;
385 switch (vol) {385 switch (vol) {
386 Type.Pointer.Vol.Non => {},386 Type.Pointer.Vol.Non => {},
...@@ -390,11 +390,11 @@ fn renderLoadUntyped(...@@ -390,11 +390,11 @@ fn renderLoadUntyped(
390 return result;390 return result;
391}391}
392392
393fn renderLoad(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Pointer, name: [*]const u8) !llvm.ValueRef {393fn renderLoad(ofile: *ObjectFile, ptr: *llvm.Value, ptr_type: *Type.Pointer, name: [*]const u8) !*llvm.Value {
394 return renderLoadUntyped(ofile, ptr, ptr_type.key.alignment, ptr_type.key.vol, name);394 return renderLoadUntyped(ofile, ptr, ptr_type.key.alignment, ptr_type.key.vol, name);
395}395}
396396
397pub fn getHandleValue(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Pointer) !?llvm.ValueRef {397pub fn getHandleValue(ofile: *ObjectFile, ptr: *llvm.Value, ptr_type: *Type.Pointer) !?*llvm.Value {
398 const child_type = ptr_type.key.child_type;398 const child_type = ptr_type.key.child_type;
399 if (!child_type.hasBits()) {399 if (!child_type.hasBits()) {
400 return null;400 return null;
...@@ -407,11 +407,11 @@ pub fn getHandleValue(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Po...@@ -407,11 +407,11 @@ pub fn getHandleValue(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Po
407407
408pub fn renderStoreUntyped(408pub fn renderStoreUntyped(
409 ofile: *ObjectFile,409 ofile: *ObjectFile,
410 value: llvm.ValueRef,410 value: *llvm.Value,
411 ptr: llvm.ValueRef,411 ptr: *llvm.Value,
412 alignment: Type.Pointer.Align,412 alignment: Type.Pointer.Align,
413 vol: Type.Pointer.Vol,413 vol: Type.Pointer.Vol,
414) !llvm.ValueRef {414) !*llvm.Value {
415 const result = llvm.BuildStore(ofile.builder, value, ptr) orelse return error.OutOfMemory;415 const result = llvm.BuildStore(ofile.builder, value, ptr) orelse return error.OutOfMemory;
416 switch (vol) {416 switch (vol) {
417 Type.Pointer.Vol.Non => {},417 Type.Pointer.Vol.Non => {},
...@@ -423,10 +423,10 @@ pub fn renderStoreUntyped(...@@ -423,10 +423,10 @@ pub fn renderStoreUntyped(
423423
424pub fn renderStore(424pub fn renderStore(
425 ofile: *ObjectFile,425 ofile: *ObjectFile,
426 value: llvm.ValueRef,426 value: *llvm.Value,
427 ptr: llvm.ValueRef,427 ptr: *llvm.Value,
428 ptr_type: *Type.Pointer,428 ptr_type: *Type.Pointer,
429) !llvm.ValueRef {429) !*llvm.Value {
430 return renderStoreUntyped(ofile, value, ptr, ptr_type.key.alignment, ptr_type.key.vol);430 return renderStoreUntyped(ofile, value, ptr, ptr_type.key.alignment, ptr_type.key.vol);
431}431}
432432
...@@ -435,7 +435,7 @@ pub fn renderAlloca(...@@ -435,7 +435,7 @@ pub fn renderAlloca(
435 var_type: *Type,435 var_type: *Type,
436 name: []const u8,436 name: []const u8,
437 alignment: Type.Pointer.Align,437 alignment: Type.Pointer.Align,
438) !llvm.ValueRef {438) !*llvm.Value {
439 const llvm_var_type = try var_type.getLlvmType(ofile.arena, ofile.context);439 const llvm_var_type = try var_type.getLlvmType(ofile.arena, ofile.context);
440 const name_with_null = try std.cstr.addNullByte(ofile.arena, name);440 const name_with_null = try std.cstr.addNullByte(ofile.arena, name);
441 const result = llvm.BuildAlloca(ofile.builder, llvm_var_type, name_with_null.ptr) orelse return error.OutOfMemory;441 const result = llvm.BuildAlloca(ofile.builder, llvm_var_type, name_with_null.ptr) orelse return error.OutOfMemory;
...@@ -443,7 +443,7 @@ pub fn renderAlloca(...@@ -443,7 +443,7 @@ pub fn renderAlloca(
443 return result;443 return result;
444}444}
445445
446pub fn resolveAlign(ofile: *ObjectFile, alignment: Type.Pointer.Align, llvm_type: llvm.TypeRef) u32 {446pub fn resolveAlign(ofile: *ObjectFile, alignment: Type.Pointer.Align, llvm_type: *llvm.Type) u32 {
447 return switch (alignment) {447 return switch (alignment) {
448 Type.Pointer.Align.Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type),448 Type.Pointer.Align.Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type),
449 Type.Pointer.Align.Override => |a| a,449 Type.Pointer.Align.Override => |a| a,
src-self-hosted/compilation.zig+11-11
...@@ -37,7 +37,7 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB...@@ -37,7 +37,7 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
37/// Data that is local to the event loop.37/// Data that is local to the event loop.
38pub const ZigCompiler = struct {38pub const ZigCompiler = struct {
39 loop: *event.Loop,39 loop: *event.Loop,
40 llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),40 llvm_handle_pool: std.atomic.Stack(*llvm.Context),
41 lld_lock: event.Lock,41 lld_lock: event.Lock,
4242
43 /// TODO pool these so that it doesn't have to lock43 /// TODO pool these so that it doesn't have to lock
...@@ -60,7 +60,7 @@ pub const ZigCompiler = struct {...@@ -60,7 +60,7 @@ pub const ZigCompiler = struct {
60 return ZigCompiler{60 return ZigCompiler{
61 .loop = loop,61 .loop = loop,
62 .lld_lock = event.Lock.init(loop),62 .lld_lock = event.Lock.init(loop),
63 .llvm_handle_pool = std.atomic.Stack(llvm.ContextRef).init(),63 .llvm_handle_pool = std.atomic.Stack(*llvm.Context).init(),
64 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),64 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
65 .native_libc = event.Future(LibCInstallation).init(loop),65 .native_libc = event.Future(LibCInstallation).init(loop),
66 };66 };
...@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {...@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {
70 fn deinit(self: *ZigCompiler) void {70 fn deinit(self: *ZigCompiler) void {
71 self.lld_lock.deinit();71 self.lld_lock.deinit();
72 while (self.llvm_handle_pool.pop()) |node| {72 while (self.llvm_handle_pool.pop()) |node| {
73 c.LLVMContextDispose(node.data);73 llvm.ContextDispose(node.data);
74 self.loop.allocator.destroy(node);74 self.loop.allocator.destroy(node);
75 }75 }
76 }76 }
...@@ -80,11 +80,11 @@ pub const ZigCompiler = struct {...@@ -80,11 +80,11 @@ pub const ZigCompiler = struct {
80 pub fn getAnyLlvmContext(self: *ZigCompiler) !LlvmHandle {80 pub fn getAnyLlvmContext(self: *ZigCompiler) !LlvmHandle {
81 if (self.llvm_handle_pool.pop()) |node| return LlvmHandle{ .node = node };81 if (self.llvm_handle_pool.pop()) |node| return LlvmHandle{ .node = node };
8282
83 const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory;83 const context_ref = llvm.ContextCreate() orelse return error.OutOfMemory;
84 errdefer c.LLVMContextDispose(context_ref);84 errdefer llvm.ContextDispose(context_ref);
8585
86 const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node);86 const node = try self.loop.allocator.create(std.atomic.Stack(*llvm.Context).Node);
87 node.* = std.atomic.Stack(llvm.ContextRef).Node{87 node.* = std.atomic.Stack(*llvm.Context).Node{
88 .next = undefined,88 .next = undefined,
89 .data = context_ref,89 .data = context_ref,
90 };90 };
...@@ -114,7 +114,7 @@ pub const ZigCompiler = struct {...@@ -114,7 +114,7 @@ pub const ZigCompiler = struct {
114};114};
115115
116pub const LlvmHandle = struct {116pub const LlvmHandle = struct {
117 node: *std.atomic.Stack(llvm.ContextRef).Node,117 node: *std.atomic.Stack(*llvm.Context).Node,
118118
119 pub fn release(self: LlvmHandle, zig_compiler: *ZigCompiler) void {119 pub fn release(self: LlvmHandle, zig_compiler: *ZigCompiler) void {
120 zig_compiler.llvm_handle_pool.push(self.node);120 zig_compiler.llvm_handle_pool.push(self.node);
...@@ -128,7 +128,7 @@ pub const Compilation = struct {...@@ -128,7 +128,7 @@ pub const Compilation = struct {
128 llvm_triple: Buffer,128 llvm_triple: Buffer,
129 root_src_path: ?[]const u8,129 root_src_path: ?[]const u8,
130 target: Target,130 target: Target,
131 llvm_target: llvm.TargetRef,131 llvm_target: *llvm.Target,
132 build_mode: builtin.Mode,132 build_mode: builtin.Mode,
133 zig_lib_dir: []const u8,133 zig_lib_dir: []const u8,
134 zig_std_dir: []const u8,134 zig_std_dir: []const u8,
...@@ -212,8 +212,8 @@ pub const Compilation = struct {...@@ -212,8 +212,8 @@ pub const Compilation = struct {
212 false_value: *Value.Bool,212 false_value: *Value.Bool,
213 noreturn_value: *Value.NoReturn,213 noreturn_value: *Value.NoReturn,
214214
215 target_machine: llvm.TargetMachineRef,215 target_machine: *llvm.TargetMachine,
216 target_data_ref: llvm.TargetDataRef,216 target_data_ref: *llvm.TargetData,
217 target_layout_str: [*]u8,217 target_layout_str: [*]u8,
218 target_ptr_bits: u32,218 target_ptr_bits: u32,
219219
src-self-hosted/ir.zig+10-10
...@@ -67,7 +67,7 @@ pub const Inst = struct {...@@ -67,7 +67,7 @@ pub const Inst = struct {
67 parent: ?*Inst,67 parent: ?*Inst,
6868
69 /// populated durign codegen69 /// populated durign codegen
70 llvm_value: ?llvm.ValueRef,70 llvm_value: ?*llvm.Value,
7171
72 pub fn cast(base: *Inst, comptime T: type) ?*T {72 pub fn cast(base: *Inst, comptime T: type) ?*T {
73 if (base.id == comptime typeToId(T)) {73 if (base.id == comptime typeToId(T)) {
...@@ -129,7 +129,7 @@ pub const Inst = struct {...@@ -129,7 +129,7 @@ pub const Inst = struct {
129 }129 }
130 }130 }
131131
132 pub fn render(base: *Inst, ofile: *ObjectFile, fn_val: *Value.Fn) (error{OutOfMemory}!?llvm.ValueRef) {132 pub fn render(base: *Inst, ofile: *ObjectFile, fn_val: *Value.Fn) (error{OutOfMemory}!?*llvm.Value) {
133 switch (base.id) {133 switch (base.id) {
134 Id.Return => return @fieldParentPtr(Return, "base", base).render(ofile, fn_val),134 Id.Return => return @fieldParentPtr(Return, "base", base).render(ofile, fn_val),
135 Id.Const => return @fieldParentPtr(Const, "base", base).render(ofile, fn_val),135 Id.Const => return @fieldParentPtr(Const, "base", base).render(ofile, fn_val),
...@@ -313,10 +313,10 @@ pub const Inst = struct {...@@ -313,10 +313,10 @@ pub const Inst = struct {
313 return new_inst;313 return new_inst;
314 }314 }
315315
316 pub fn render(self: *Call, ofile: *ObjectFile, fn_val: *Value.Fn) !?llvm.ValueRef {316 pub fn render(self: *Call, ofile: *ObjectFile, fn_val: *Value.Fn) !?*llvm.Value {
317 const fn_ref = self.params.fn_ref.llvm_value.?;317 const fn_ref = self.params.fn_ref.llvm_value.?;
318318
319 const args = try ofile.arena.alloc(llvm.ValueRef, self.params.args.len);319 const args = try ofile.arena.alloc(*llvm.Value, self.params.args.len);
320 for (self.params.args) |arg, i| {320 for (self.params.args) |arg, i| {
321 args[i] = arg.llvm_value.?;321 args[i] = arg.llvm_value.?;
322 }322 }
...@@ -360,7 +360,7 @@ pub const Inst = struct {...@@ -360,7 +360,7 @@ pub const Inst = struct {
360 return new_inst;360 return new_inst;
361 }361 }
362362
363 pub fn render(self: *Const, ofile: *ObjectFile, fn_val: *Value.Fn) !?llvm.ValueRef {363 pub fn render(self: *Const, ofile: *ObjectFile, fn_val: *Value.Fn) !?*llvm.Value {
364 return self.base.val.KnownValue.getLlvmConst(ofile);364 return self.base.val.KnownValue.getLlvmConst(ofile);
365 }365 }
366 };366 };
...@@ -392,7 +392,7 @@ pub const Inst = struct {...@@ -392,7 +392,7 @@ pub const Inst = struct {
392 return ira.irb.build(Return, self.base.scope, self.base.span, Params{ .return_value = casted_value });392 return ira.irb.build(Return, self.base.scope, self.base.span, Params{ .return_value = casted_value });
393 }393 }
394394
395 pub fn render(self: *Return, ofile: *ObjectFile, fn_val: *Value.Fn) !?llvm.ValueRef {395 pub fn render(self: *Return, ofile: *ObjectFile, fn_val: *Value.Fn) !?*llvm.Value {
396 const value = self.params.return_value.llvm_value;396 const value = self.params.return_value.llvm_value;
397 const return_type = self.params.return_value.getKnownType();397 const return_type = self.params.return_value.getKnownType();
398398
...@@ -540,7 +540,7 @@ pub const Inst = struct {...@@ -540,7 +540,7 @@ pub const Inst = struct {
540 }540 }
541 }541 }
542542
543 pub fn render(self: *VarPtr, ofile: *ObjectFile, fn_val: *Value.Fn) llvm.ValueRef {543 pub fn render(self: *VarPtr, ofile: *ObjectFile, fn_val: *Value.Fn) *llvm.Value {
544 switch (self.params.var_scope.data) {544 switch (self.params.var_scope.data) {
545 Scope.Var.Data.Const => unreachable, // turned into Inst.Const in analyze pass545 Scope.Var.Data.Const => unreachable, // turned into Inst.Const in analyze pass
546 Scope.Var.Data.Param => |param| return param.llvm_value,546 Scope.Var.Data.Param => |param| return param.llvm_value,
...@@ -596,7 +596,7 @@ pub const Inst = struct {...@@ -596,7 +596,7 @@ pub const Inst = struct {
596 return new_inst;596 return new_inst;
597 }597 }
598598
599 pub fn render(self: *LoadPtr, ofile: *ObjectFile, fn_val: *Value.Fn) !?llvm.ValueRef {599 pub fn render(self: *LoadPtr, ofile: *ObjectFile, fn_val: *Value.Fn) !?*llvm.Value {
600 const child_type = self.base.getKnownType();600 const child_type = self.base.getKnownType();
601 if (!child_type.hasBits()) {601 if (!child_type.hasBits()) {
602 return null;602 return null;
...@@ -935,8 +935,8 @@ pub const BasicBlock = struct {...@@ -935,8 +935,8 @@ pub const BasicBlock = struct {
935 ref_instruction: ?*Inst,935 ref_instruction: ?*Inst,
936936
937 /// for codegen937 /// for codegen
938 llvm_block: llvm.BasicBlockRef,938 llvm_block: *llvm.BasicBlock,
939 llvm_exit_block: llvm.BasicBlockRef,939 llvm_exit_block: *llvm.BasicBlock,
940940
941 /// the basic block that is derived from this one in analysis941 /// the basic block that is derived from this one in analysis
942 child: ?*BasicBlock,942 child: ?*BasicBlock,
src-self-hosted/llvm.zig+129-52
...@@ -11,45 +11,31 @@ const assert = @import("std").debug.assert;...@@ -11,45 +11,31 @@ const assert = @import("std").debug.assert;
11pub const AttributeIndex = c_uint;11pub const AttributeIndex = c_uint;
12pub const Bool = c_int;12pub const Bool = c_int;
1313
14pub const BuilderRef = removeNullability(c.LLVMBuilderRef);14pub const Builder = c.LLVMBuilderRef.Child;
15pub const ContextRef = removeNullability(c.LLVMContextRef);15pub const Context = c.LLVMContextRef.Child;
16pub const ModuleRef = removeNullability(c.LLVMModuleRef);16pub const Module = c.LLVMModuleRef.Child;
17pub const ValueRef = removeNullability(c.LLVMValueRef);17pub const Value = c.LLVMValueRef.Child;
18pub const TypeRef = removeNullability(c.LLVMTypeRef);18pub const Type = c.LLVMTypeRef.Child;
19pub const BasicBlockRef = removeNullability(c.LLVMBasicBlockRef);19pub const BasicBlock = c.LLVMBasicBlockRef.Child;
20pub const AttributeRef = removeNullability(c.LLVMAttributeRef);20pub const Attribute = c.LLVMAttributeRef.Child;
21pub const TargetRef = removeNullability(c.LLVMTargetRef);21pub const Target = c.LLVMTargetRef.Child;
22pub const TargetMachineRef = removeNullability(c.LLVMTargetMachineRef);22pub const TargetMachine = c.LLVMTargetMachineRef.Child;
23pub const TargetDataRef = removeNullability(c.LLVMTargetDataRef);23pub const TargetData = c.LLVMTargetDataRef.Child;
24pub const DIBuilder = c.ZigLLVMDIBuilder;24pub const DIBuilder = c.ZigLLVMDIBuilder;
25pub const DIFile = c.ZigLLVMDIFile;
26pub const DICompileUnit = c.ZigLLVMDICompileUnit;
2527
26pub const ABIAlignmentOfType = c.LLVMABIAlignmentOfType;28pub const ABIAlignmentOfType = c.LLVMABIAlignmentOfType;
27pub const AddAttributeAtIndex = c.LLVMAddAttributeAtIndex;29pub const AddAttributeAtIndex = c.LLVMAddAttributeAtIndex;
28pub const AddFunction = c.LLVMAddFunction;
29pub const AddGlobal = c.LLVMAddGlobal;
30pub const AddModuleCodeViewFlag = c.ZigLLVMAddModuleCodeViewFlag;30pub const AddModuleCodeViewFlag = c.ZigLLVMAddModuleCodeViewFlag;
31pub const AddModuleDebugInfoFlag = c.ZigLLVMAddModuleDebugInfoFlag;31pub const AddModuleDebugInfoFlag = c.ZigLLVMAddModuleDebugInfoFlag;
32pub const ArrayType = c.LLVMArrayType;
33pub const BuildLoad = c.LLVMBuildLoad;
34pub const ClearCurrentDebugLocation = c.ZigLLVMClearCurrentDebugLocation;32pub const ClearCurrentDebugLocation = c.ZigLLVMClearCurrentDebugLocation;
35pub const ConstAllOnes = c.LLVMConstAllOnes;33pub const ConstAllOnes = c.LLVMConstAllOnes;
36pub const ConstArray = c.LLVMConstArray;34pub const ConstArray = c.LLVMConstArray;
37pub const ConstBitCast = c.LLVMConstBitCast;35pub const ConstBitCast = c.LLVMConstBitCast;
38pub const ConstInt = c.LLVMConstInt;
39pub const ConstIntOfArbitraryPrecision = c.LLVMConstIntOfArbitraryPrecision;36pub const ConstIntOfArbitraryPrecision = c.LLVMConstIntOfArbitraryPrecision;
40pub const ConstNeg = c.LLVMConstNeg;37pub const ConstNeg = c.LLVMConstNeg;
41pub const ConstNull = c.LLVMConstNull;
42pub const ConstStringInContext = c.LLVMConstStringInContext;
43pub const ConstStructInContext = c.LLVMConstStructInContext;38pub const ConstStructInContext = c.LLVMConstStructInContext;
44pub const CopyStringRepOfTargetData = c.LLVMCopyStringRepOfTargetData;
45pub const CreateBuilderInContext = c.LLVMCreateBuilderInContext;
46pub const CreateCompileUnit = c.ZigLLVMCreateCompileUnit;
47pub const CreateDIBuilder = c.ZigLLVMCreateDIBuilder;
48pub const CreateEnumAttribute = c.LLVMCreateEnumAttribute;
49pub const CreateFile = c.ZigLLVMCreateFile;
50pub const CreateStringAttribute = c.LLVMCreateStringAttribute;
51pub const CreateTargetDataLayout = c.LLVMCreateTargetDataLayout;
52pub const CreateTargetMachine = c.LLVMCreateTargetMachine;
53pub const DIBuilderFinalize = c.ZigLLVMDIBuilderFinalize;39pub const DIBuilderFinalize = c.ZigLLVMDIBuilderFinalize;
54pub const DisposeBuilder = c.LLVMDisposeBuilder;40pub const DisposeBuilder = c.LLVMDisposeBuilder;
55pub const DisposeDIBuilder = c.ZigLLVMDisposeDIBuilder;41pub const DisposeDIBuilder = c.ZigLLVMDisposeDIBuilder;
...@@ -62,9 +48,7 @@ pub const DumpModule = c.LLVMDumpModule;...@@ -62,9 +48,7 @@ pub const DumpModule = c.LLVMDumpModule;
62pub const FP128TypeInContext = c.LLVMFP128TypeInContext;48pub const FP128TypeInContext = c.LLVMFP128TypeInContext;
63pub const FloatTypeInContext = c.LLVMFloatTypeInContext;49pub const FloatTypeInContext = c.LLVMFloatTypeInContext;
64pub const GetEnumAttributeKindForName = c.LLVMGetEnumAttributeKindForName;50pub const GetEnumAttributeKindForName = c.LLVMGetEnumAttributeKindForName;
65pub const GetHostCPUName = c.ZigLLVMGetHostCPUName;
66pub const GetMDKindIDInContext = c.LLVMGetMDKindIDInContext;51pub const GetMDKindIDInContext = c.LLVMGetMDKindIDInContext;
67pub const GetNativeFeatures = c.ZigLLVMGetNativeFeatures;
68pub const GetUndef = c.LLVMGetUndef;52pub const GetUndef = c.LLVMGetUndef;
69pub const HalfTypeInContext = c.LLVMHalfTypeInContext;53pub const HalfTypeInContext = c.LLVMHalfTypeInContext;
70pub const InitializeAllAsmParsers = c.LLVMInitializeAllAsmParsers;54pub const InitializeAllAsmParsers = c.LLVMInitializeAllAsmParsers;
...@@ -81,14 +65,11 @@ pub const Int64TypeInContext = c.LLVMInt64TypeInContext;...@@ -81,14 +65,11 @@ pub const Int64TypeInContext = c.LLVMInt64TypeInContext;
81pub const Int8TypeInContext = c.LLVMInt8TypeInContext;65pub const Int8TypeInContext = c.LLVMInt8TypeInContext;
82pub const IntPtrTypeForASInContext = c.LLVMIntPtrTypeForASInContext;66pub const IntPtrTypeForASInContext = c.LLVMIntPtrTypeForASInContext;
83pub const IntPtrTypeInContext = c.LLVMIntPtrTypeInContext;67pub const IntPtrTypeInContext = c.LLVMIntPtrTypeInContext;
84pub const IntTypeInContext = c.LLVMIntTypeInContext;
85pub const LabelTypeInContext = c.LLVMLabelTypeInContext;68pub const LabelTypeInContext = c.LLVMLabelTypeInContext;
86pub const MDNodeInContext = c.LLVMMDNodeInContext;69pub const MDNodeInContext = c.LLVMMDNodeInContext;
87pub const MDStringInContext = c.LLVMMDStringInContext;70pub const MDStringInContext = c.LLVMMDStringInContext;
88pub const MetadataTypeInContext = c.LLVMMetadataTypeInContext;71pub const MetadataTypeInContext = c.LLVMMetadataTypeInContext;
89pub const ModuleCreateWithNameInContext = c.LLVMModuleCreateWithNameInContext;
90pub const PPCFP128TypeInContext = c.LLVMPPCFP128TypeInContext;72pub const PPCFP128TypeInContext = c.LLVMPPCFP128TypeInContext;
91pub const PointerType = c.LLVMPointerType;
92pub const SetAlignment = c.LLVMSetAlignment;73pub const SetAlignment = c.LLVMSetAlignment;
93pub const SetDataLayout = c.LLVMSetDataLayout;74pub const SetDataLayout = c.LLVMSetDataLayout;
94pub const SetGlobalConstant = c.LLVMSetGlobalConstant;75pub const SetGlobalConstant = c.LLVMSetGlobalConstant;
...@@ -99,50 +80,146 @@ pub const SetUnnamedAddr = c.LLVMSetUnnamedAddr;...@@ -99,50 +80,146 @@ pub const SetUnnamedAddr = c.LLVMSetUnnamedAddr;
99pub const SetVolatile = c.LLVMSetVolatile;80pub const SetVolatile = c.LLVMSetVolatile;
100pub const StructTypeInContext = c.LLVMStructTypeInContext;81pub const StructTypeInContext = c.LLVMStructTypeInContext;
101pub const TokenTypeInContext = c.LLVMTokenTypeInContext;82pub const TokenTypeInContext = c.LLVMTokenTypeInContext;
102pub const VoidTypeInContext = c.LLVMVoidTypeInContext;
103pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;83pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;
104pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext;84pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext;
10585
86pub const AddGlobal = LLVMAddGlobal;
87extern fn LLVMAddGlobal(M: *Module, Ty: *Type, Name: [*]const u8) ?*Value;
88
89pub const ConstStringInContext = LLVMConstStringInContext;
90extern fn LLVMConstStringInContext(C: *Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) ?*Value;
91
92pub const ConstInt = LLVMConstInt;
93extern fn LLVMConstInt(IntTy: *Type, N: c_ulonglong, SignExtend: Bool) ?*Value;
94
95pub const BuildLoad = LLVMBuildLoad;
96extern fn LLVMBuildLoad(arg0: *Builder, PointerVal: *Value, Name: [*]const u8) ?*Value;
97
98pub const ConstNull = LLVMConstNull;
99extern fn LLVMConstNull(Ty: *Type) ?*Value;
100
101pub const CreateStringAttribute = LLVMCreateStringAttribute;
102extern fn LLVMCreateStringAttribute(
103 C: *Context,
104 K: [*]const u8,
105 KLength: c_uint,
106 V: [*]const u8,
107 VLength: c_uint,
108) ?*Attribute;
109
110pub const CreateEnumAttribute = LLVMCreateEnumAttribute;
111extern fn LLVMCreateEnumAttribute(C: *Context, KindID: c_uint, Val: u64) ?*Attribute;
112
113pub const AddFunction = LLVMAddFunction;
114extern fn LLVMAddFunction(M: *Module, Name: [*]const u8, FunctionTy: *Type) ?*Value;
115
116pub const CreateCompileUnit = ZigLLVMCreateCompileUnit;
117extern fn ZigLLVMCreateCompileUnit(
118 dibuilder: *DIBuilder,
119 lang: c_uint,
120 difile: *DIFile,
121 producer: [*]const u8,
122 is_optimized: bool,
123 flags: [*]const u8,
124 runtime_version: c_uint,
125 split_name: [*]const u8,
126 dwo_id: u64,
127 emit_debug_info: bool,
128) ?*DICompileUnit;
129
130pub const CreateFile = ZigLLVMCreateFile;
131extern fn ZigLLVMCreateFile(dibuilder: *DIBuilder, filename: [*]const u8, directory: [*]const u8) ?*DIFile;
132
133pub const ArrayType = LLVMArrayType;
134extern fn LLVMArrayType(ElementType: *Type, ElementCount: c_uint) ?*Type;
135
136pub const CreateDIBuilder = ZigLLVMCreateDIBuilder;
137extern fn ZigLLVMCreateDIBuilder(module: *Module, allow_unresolved: bool) ?*DIBuilder;
138
139pub const PointerType = LLVMPointerType;
140extern fn LLVMPointerType(ElementType: *Type, AddressSpace: c_uint) ?*Type;
141
142pub const CreateBuilderInContext = LLVMCreateBuilderInContext;
143extern fn LLVMCreateBuilderInContext(C: *Context) ?*Builder;
144
145pub const IntTypeInContext = LLVMIntTypeInContext;
146extern fn LLVMIntTypeInContext(C: *Context, NumBits: c_uint) ?*Type;
147
148pub const ModuleCreateWithNameInContext = LLVMModuleCreateWithNameInContext;
149extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*]const u8, C: *Context) ?*Module;
150
151pub const VoidTypeInContext = LLVMVoidTypeInContext;
152extern fn LLVMVoidTypeInContext(C: *Context) ?*Type;
153
154pub const ContextCreate = LLVMContextCreate;
155extern fn LLVMContextCreate() ?*Context;
156
157pub const ContextDispose = LLVMContextDispose;
158extern fn LLVMContextDispose(C: *Context) void;
159
160pub const CopyStringRepOfTargetData = LLVMCopyStringRepOfTargetData;
161extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8;
162
163pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout;
164extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;
165
166pub const CreateTargetMachine = LLVMCreateTargetMachine;
167extern fn LLVMCreateTargetMachine(
168 T: *Target,
169 Triple: [*]const u8,
170 CPU: [*]const u8,
171 Features: [*]const u8,
172 Level: CodeGenOptLevel,
173 Reloc: RelocMode,
174 CodeModel: CodeModel,
175) ?*TargetMachine;
176
177pub const GetHostCPUName = LLVMGetHostCPUName;
178extern fn LLVMGetHostCPUName() ?[*]u8;
179
180pub const GetNativeFeatures = ZigLLVMGetNativeFeatures;
181extern fn ZigLLVMGetNativeFeatures() ?[*]u8;
182
106pub const GetElementType = LLVMGetElementType;183pub const GetElementType = LLVMGetElementType;
107extern fn LLVMGetElementType(Ty: TypeRef) TypeRef;184extern fn LLVMGetElementType(Ty: *Type) *Type;
108185
109pub const TypeOf = LLVMTypeOf;186pub const TypeOf = LLVMTypeOf;
110extern fn LLVMTypeOf(Val: ValueRef) TypeRef;187extern fn LLVMTypeOf(Val: *Value) *Type;
111188
112pub const BuildStore = LLVMBuildStore;189pub const BuildStore = LLVMBuildStore;
113extern fn LLVMBuildStore(arg0: BuilderRef, Val: ValueRef, Ptr: ValueRef) ?ValueRef;190extern fn LLVMBuildStore(arg0: *Builder, Val: *Value, Ptr: *Value) ?*Value;
114191
115pub const BuildAlloca = LLVMBuildAlloca;192pub const BuildAlloca = LLVMBuildAlloca;
116extern fn LLVMBuildAlloca(arg0: BuilderRef, Ty: TypeRef, Name: ?[*]const u8) ?ValueRef;193extern fn LLVMBuildAlloca(arg0: *Builder, Ty: *Type, Name: ?[*]const u8) ?*Value;
117194
118pub const ConstInBoundsGEP = LLVMConstInBoundsGEP;195pub const ConstInBoundsGEP = LLVMConstInBoundsGEP;
119pub extern fn LLVMConstInBoundsGEP(ConstantVal: ValueRef, ConstantIndices: [*]ValueRef, NumIndices: c_uint) ?ValueRef;196pub extern fn LLVMConstInBoundsGEP(ConstantVal: *Value, ConstantIndices: [*]*Value, NumIndices: c_uint) ?*Value;
120197
121pub const GetTargetFromTriple = LLVMGetTargetFromTriple;198pub const GetTargetFromTriple = LLVMGetTargetFromTriple;
122extern fn LLVMGetTargetFromTriple(Triple: [*]const u8, T: *TargetRef, ErrorMessage: ?*[*]u8) Bool;199extern fn LLVMGetTargetFromTriple(Triple: [*]const u8, T: **Target, ErrorMessage: ?*[*]u8) Bool;
123200
124pub const VerifyModule = LLVMVerifyModule;201pub const VerifyModule = LLVMVerifyModule;
125extern fn LLVMVerifyModule(M: ModuleRef, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool;202extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool;
126203
127pub const GetInsertBlock = LLVMGetInsertBlock;204pub const GetInsertBlock = LLVMGetInsertBlock;
128extern fn LLVMGetInsertBlock(Builder: BuilderRef) BasicBlockRef;205extern fn LLVMGetInsertBlock(Builder: *Builder) *BasicBlock;
129206
130pub const FunctionType = LLVMFunctionType;207pub const FunctionType = LLVMFunctionType;
131extern fn LLVMFunctionType(208extern fn LLVMFunctionType(
132 ReturnType: TypeRef,209 ReturnType: *Type,
133 ParamTypes: [*]TypeRef,210 ParamTypes: [*]*Type,
134 ParamCount: c_uint,211 ParamCount: c_uint,
135 IsVarArg: Bool,212 IsVarArg: Bool,
136) ?TypeRef;213) ?*Type;
137214
138pub const GetParam = LLVMGetParam;215pub const GetParam = LLVMGetParam;
139extern fn LLVMGetParam(Fn: ValueRef, Index: c_uint) ValueRef;216extern fn LLVMGetParam(Fn: *Value, Index: c_uint) *Value;
140217
141pub const AppendBasicBlockInContext = LLVMAppendBasicBlockInContext;218pub const AppendBasicBlockInContext = LLVMAppendBasicBlockInContext;
142extern fn LLVMAppendBasicBlockInContext(C: ContextRef, Fn: ValueRef, Name: [*]const u8) ?BasicBlockRef;219extern fn LLVMAppendBasicBlockInContext(C: *Context, Fn: *Value, Name: [*]const u8) ?*BasicBlock;
143220
144pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd;221pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd;
145extern fn LLVMPositionBuilderAtEnd(Builder: BuilderRef, Block: BasicBlockRef) void;222extern fn LLVMPositionBuilderAtEnd(Builder: *Builder, Block: *BasicBlock) void;
146223
147pub const AbortProcessAction = VerifierFailureAction.LLVMAbortProcessAction;224pub const AbortProcessAction = VerifierFailureAction.LLVMAbortProcessAction;
148pub const PrintMessageAction = VerifierFailureAction.LLVMPrintMessageAction;225pub const PrintMessageAction = VerifierFailureAction.LLVMPrintMessageAction;
...@@ -190,17 +267,17 @@ pub const FnInline = extern enum {...@@ -190,17 +267,17 @@ pub const FnInline = extern enum {
190};267};
191268
192fn removeNullability(comptime T: type) type {269fn removeNullability(comptime T: type) type {
193 comptime assert(@typeId(T) == builtin.TypeId.Optional);270 comptime assert(@typeInfo(T).Pointer.size == @import("builtin").TypeInfo.Pointer.Size.C);
194 return T.Child;271 return *T.Child;
195}272}
196273
197pub const BuildRet = LLVMBuildRet;274pub const BuildRet = LLVMBuildRet;
198extern fn LLVMBuildRet(arg0: BuilderRef, V: ?ValueRef) ?ValueRef;275extern fn LLVMBuildRet(arg0: *Builder, V: ?*Value) ?*Value;
199276
200pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile;277pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile;
201extern fn ZigLLVMTargetMachineEmitToFile(278extern fn ZigLLVMTargetMachineEmitToFile(
202 targ_machine_ref: TargetMachineRef,279 targ_machine_ref: *TargetMachine,
203 module_ref: ModuleRef,280 module_ref: *Module,
204 filename: [*]const u8,281 filename: [*]const u8,
205 output_type: EmitOutputType,282 output_type: EmitOutputType,
206 error_message: *[*]u8,283 error_message: *[*]u8,
...@@ -209,6 +286,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(...@@ -209,6 +286,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(
209) bool;286) bool;
210287
211pub const BuildCall = ZigLLVMBuildCall;288pub const BuildCall = ZigLLVMBuildCall;
212extern fn ZigLLVMBuildCall(B: BuilderRef, Fn: ValueRef, Args: [*]ValueRef, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?ValueRef;289extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value;
213290
214pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;291pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;
src-self-hosted/scope.zig+1-1
...@@ -362,7 +362,7 @@ pub const Scope = struct {...@@ -362,7 +362,7 @@ pub const Scope = struct {
362 pub const Param = struct {362 pub const Param = struct {
363 index: usize,363 index: usize,
364 typ: *Type,364 typ: *Type,
365 llvm_value: llvm.ValueRef,365 llvm_value: *llvm.Value,
366 };366 };
367367
368 pub fn createParam(368 pub fn createParam(
src-self-hosted/target.zig+2-2
...@@ -457,8 +457,8 @@ pub const Target = union(enum) {...@@ -457,8 +457,8 @@ pub const Target = union(enum) {
457 }457 }
458 }458 }
459459
460 pub fn llvmTargetFromTriple(triple: std.Buffer) !llvm.TargetRef {460 pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target {
461 var result: llvm.TargetRef = undefined;461 var result: *llvm.Target = undefined;
462 var err_msg: [*]u8 = undefined;462 var err_msg: [*]u8 = undefined;
463 if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {463 if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {
464 std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg);464 std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg);
src-self-hosted/type.zig+21-21
...@@ -51,8 +51,8 @@ pub const Type = struct {...@@ -51,8 +51,8 @@ pub const Type = struct {
51 pub fn getLlvmType(51 pub fn getLlvmType(
52 base: *Type,52 base: *Type,
53 allocator: *Allocator,53 allocator: *Allocator,
54 llvm_context: llvm.ContextRef,54 llvm_context: *llvm.Context,
55 ) (error{OutOfMemory}!llvm.TypeRef) {55 ) (error{OutOfMemory}!*llvm.Type) {
56 switch (base.id) {56 switch (base.id) {
57 Id.Struct => return @fieldParentPtr(Struct, "base", base).getLlvmType(allocator, llvm_context),57 Id.Struct => return @fieldParentPtr(Struct, "base", base).getLlvmType(allocator, llvm_context),
58 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmType(allocator, llvm_context),58 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmType(allocator, llvm_context),
...@@ -196,7 +196,7 @@ pub const Type = struct {...@@ -196,7 +196,7 @@ pub const Type = struct {
196 }196 }
197197
198 /// If you have an llvm conext handy, you can use it here.198 /// If you have an llvm conext handy, you can use it here.
199 pub async fn getAbiAlignmentInContext(base: *Type, comp: *Compilation, llvm_context: llvm.ContextRef) !u32 {199 pub async fn getAbiAlignmentInContext(base: *Type, comp: *Compilation, llvm_context: *llvm.Context) !u32 {
200 if (await (async base.abi_alignment.start() catch unreachable)) |ptr| return ptr.*;200 if (await (async base.abi_alignment.start() catch unreachable)) |ptr| return ptr.*;
201201
202 base.abi_alignment.data = await (async base.resolveAbiAlignment(comp, llvm_context) catch unreachable);202 base.abi_alignment.data = await (async base.resolveAbiAlignment(comp, llvm_context) catch unreachable);
...@@ -205,7 +205,7 @@ pub const Type = struct {...@@ -205,7 +205,7 @@ pub const Type = struct {
205 }205 }
206206
207 /// Lower level function that does the work. See getAbiAlignment.207 /// Lower level function that does the work. See getAbiAlignment.
208 async fn resolveAbiAlignment(base: *Type, comp: *Compilation, llvm_context: llvm.ContextRef) !u32 {208 async fn resolveAbiAlignment(base: *Type, comp: *Compilation, llvm_context: *llvm.Context) !u32 {
209 const llvm_type = try base.getLlvmType(comp.gpa(), llvm_context);209 const llvm_type = try base.getLlvmType(comp.gpa(), llvm_context);
210 return @intCast(u32, llvm.ABIAlignmentOfType(comp.target_data_ref, llvm_type));210 return @intCast(u32, llvm.ABIAlignmentOfType(comp.target_data_ref, llvm_type));
211 }211 }
...@@ -218,7 +218,7 @@ pub const Type = struct {...@@ -218,7 +218,7 @@ pub const Type = struct {
218 comp.gpa().destroy(self);218 comp.gpa().destroy(self);
219 }219 }
220220
221 pub fn getLlvmType(self: *Struct, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {221 pub fn getLlvmType(self: *Struct, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
222 @panic("TODO");222 @panic("TODO");
223 }223 }
224 };224 };
...@@ -496,13 +496,13 @@ pub const Type = struct {...@@ -496,13 +496,13 @@ pub const Type = struct {
496 comp.gpa().destroy(self);496 comp.gpa().destroy(self);
497 }497 }
498498
499 pub fn getLlvmType(self: *Fn, allocator: *Allocator, llvm_context: llvm.ContextRef) !llvm.TypeRef {499 pub fn getLlvmType(self: *Fn, allocator: *Allocator, llvm_context: *llvm.Context) !*llvm.Type {
500 const normal = &self.key.data.Normal;500 const normal = &self.key.data.Normal;
501 const llvm_return_type = switch (normal.return_type.id) {501 const llvm_return_type = switch (normal.return_type.id) {
502 Type.Id.Void => llvm.VoidTypeInContext(llvm_context) orelse return error.OutOfMemory,502 Type.Id.Void => llvm.VoidTypeInContext(llvm_context) orelse return error.OutOfMemory,
503 else => try normal.return_type.getLlvmType(allocator, llvm_context),503 else => try normal.return_type.getLlvmType(allocator, llvm_context),
504 };504 };
505 const llvm_param_types = try allocator.alloc(llvm.TypeRef, normal.params.len);505 const llvm_param_types = try allocator.alloc(*llvm.Type, normal.params.len);
506 defer allocator.free(llvm_param_types);506 defer allocator.free(llvm_param_types);
507 for (llvm_param_types) |*llvm_param_type, i| {507 for (llvm_param_types) |*llvm_param_type, i| {
508 llvm_param_type.* = try normal.params[i].typ.getLlvmType(allocator, llvm_context);508 llvm_param_type.* = try normal.params[i].typ.getLlvmType(allocator, llvm_context);
...@@ -559,7 +559,7 @@ pub const Type = struct {...@@ -559,7 +559,7 @@ pub const Type = struct {
559 comp.gpa().destroy(self);559 comp.gpa().destroy(self);
560 }560 }
561561
562 pub fn getLlvmType(self: *Bool, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {562 pub fn getLlvmType(self: *Bool, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
563 @panic("TODO");563 @panic("TODO");
564 }564 }
565 };565 };
...@@ -658,7 +658,7 @@ pub const Type = struct {...@@ -658,7 +658,7 @@ pub const Type = struct {
658 comp.gpa().destroy(self);658 comp.gpa().destroy(self);
659 }659 }
660660
661 pub fn getLlvmType(self: *Int, allocator: *Allocator, llvm_context: llvm.ContextRef) !llvm.TypeRef {661 pub fn getLlvmType(self: *Int, allocator: *Allocator, llvm_context: *llvm.Context) !*llvm.Type {
662 return llvm.IntTypeInContext(llvm_context, self.key.bit_count) orelse return error.OutOfMemory;662 return llvm.IntTypeInContext(llvm_context, self.key.bit_count) orelse return error.OutOfMemory;
663 }663 }
664 };664 };
...@@ -670,7 +670,7 @@ pub const Type = struct {...@@ -670,7 +670,7 @@ pub const Type = struct {
670 comp.gpa().destroy(self);670 comp.gpa().destroy(self);
671 }671 }
672672
673 pub fn getLlvmType(self: *Float, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {673 pub fn getLlvmType(self: *Float, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
674 @panic("TODO");674 @panic("TODO");
675 }675 }
676 };676 };
...@@ -836,7 +836,7 @@ pub const Type = struct {...@@ -836,7 +836,7 @@ pub const Type = struct {
836 return self;836 return self;
837 }837 }
838838
839 pub fn getLlvmType(self: *Pointer, allocator: *Allocator, llvm_context: llvm.ContextRef) !llvm.TypeRef {839 pub fn getLlvmType(self: *Pointer, allocator: *Allocator, llvm_context: *llvm.Context) !*llvm.Type {
840 const elem_llvm_type = try self.key.child_type.getLlvmType(allocator, llvm_context);840 const elem_llvm_type = try self.key.child_type.getLlvmType(allocator, llvm_context);
841 return llvm.PointerType(elem_llvm_type, 0) orelse return error.OutOfMemory;841 return llvm.PointerType(elem_llvm_type, 0) orelse return error.OutOfMemory;
842 }842 }
...@@ -904,7 +904,7 @@ pub const Type = struct {...@@ -904,7 +904,7 @@ pub const Type = struct {
904 return self;904 return self;
905 }905 }
906906
907 pub fn getLlvmType(self: *Array, allocator: *Allocator, llvm_context: llvm.ContextRef) !llvm.TypeRef {907 pub fn getLlvmType(self: *Array, allocator: *Allocator, llvm_context: *llvm.Context) !*llvm.Type {
908 const elem_llvm_type = try self.key.elem_type.getLlvmType(allocator, llvm_context);908 const elem_llvm_type = try self.key.elem_type.getLlvmType(allocator, llvm_context);
909 return llvm.ArrayType(elem_llvm_type, @intCast(c_uint, self.key.len)) orelse return error.OutOfMemory;909 return llvm.ArrayType(elem_llvm_type, @intCast(c_uint, self.key.len)) orelse return error.OutOfMemory;
910 }910 }
...@@ -917,7 +917,7 @@ pub const Type = struct {...@@ -917,7 +917,7 @@ pub const Type = struct {
917 comp.gpa().destroy(self);917 comp.gpa().destroy(self);
918 }918 }
919919
920 pub fn getLlvmType(self: *Vector, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {920 pub fn getLlvmType(self: *Vector, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
921 @panic("TODO");921 @panic("TODO");
922 }922 }
923 };923 };
...@@ -967,7 +967,7 @@ pub const Type = struct {...@@ -967,7 +967,7 @@ pub const Type = struct {
967 comp.gpa().destroy(self);967 comp.gpa().destroy(self);
968 }968 }
969969
970 pub fn getLlvmType(self: *Optional, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {970 pub fn getLlvmType(self: *Optional, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
971 @panic("TODO");971 @panic("TODO");
972 }972 }
973 };973 };
...@@ -979,7 +979,7 @@ pub const Type = struct {...@@ -979,7 +979,7 @@ pub const Type = struct {
979 comp.gpa().destroy(self);979 comp.gpa().destroy(self);
980 }980 }
981981
982 pub fn getLlvmType(self: *ErrorUnion, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {982 pub fn getLlvmType(self: *ErrorUnion, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
983 @panic("TODO");983 @panic("TODO");
984 }984 }
985 };985 };
...@@ -991,7 +991,7 @@ pub const Type = struct {...@@ -991,7 +991,7 @@ pub const Type = struct {
991 comp.gpa().destroy(self);991 comp.gpa().destroy(self);
992 }992 }
993993
994 pub fn getLlvmType(self: *ErrorSet, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {994 pub fn getLlvmType(self: *ErrorSet, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
995 @panic("TODO");995 @panic("TODO");
996 }996 }
997 };997 };
...@@ -1003,7 +1003,7 @@ pub const Type = struct {...@@ -1003,7 +1003,7 @@ pub const Type = struct {
1003 comp.gpa().destroy(self);1003 comp.gpa().destroy(self);
1004 }1004 }
10051005
1006 pub fn getLlvmType(self: *Enum, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {1006 pub fn getLlvmType(self: *Enum, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
1007 @panic("TODO");1007 @panic("TODO");
1008 }1008 }
1009 };1009 };
...@@ -1015,7 +1015,7 @@ pub const Type = struct {...@@ -1015,7 +1015,7 @@ pub const Type = struct {
1015 comp.gpa().destroy(self);1015 comp.gpa().destroy(self);
1016 }1016 }
10171017
1018 pub fn getLlvmType(self: *Union, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {1018 pub fn getLlvmType(self: *Union, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
1019 @panic("TODO");1019 @panic("TODO");
1020 }1020 }
1021 };1021 };
...@@ -1035,7 +1035,7 @@ pub const Type = struct {...@@ -1035,7 +1035,7 @@ pub const Type = struct {
1035 comp.gpa().destroy(self);1035 comp.gpa().destroy(self);
1036 }1036 }
10371037
1038 pub fn getLlvmType(self: *BoundFn, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {1038 pub fn getLlvmType(self: *BoundFn, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
1039 @panic("TODO");1039 @panic("TODO");
1040 }1040 }
1041 };1041 };
...@@ -1055,7 +1055,7 @@ pub const Type = struct {...@@ -1055,7 +1055,7 @@ pub const Type = struct {
1055 comp.gpa().destroy(self);1055 comp.gpa().destroy(self);
1056 }1056 }
10571057
1058 pub fn getLlvmType(self: *Opaque, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {1058 pub fn getLlvmType(self: *Opaque, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
1059 @panic("TODO");1059 @panic("TODO");
1060 }1060 }
1061 };1061 };
...@@ -1067,7 +1067,7 @@ pub const Type = struct {...@@ -1067,7 +1067,7 @@ pub const Type = struct {
1067 comp.gpa().destroy(self);1067 comp.gpa().destroy(self);
1068 }1068 }
10691069
1070 pub fn getLlvmType(self: *Promise, allocator: *Allocator, llvm_context: llvm.ContextRef) llvm.TypeRef {1070 pub fn getLlvmType(self: *Promise, allocator: *Allocator, llvm_context: *llvm.Context) *llvm.Type {
1071 @panic("TODO");1071 @panic("TODO");
1072 }1072 }
1073 };1073 };
src-self-hosted/value.zig+8-8
...@@ -57,7 +57,7 @@ pub const Value = struct {...@@ -57,7 +57,7 @@ pub const Value = struct {
57 std.debug.warn("{}", @tagName(base.id));57 std.debug.warn("{}", @tagName(base.id));
58 }58 }
5959
60 pub fn getLlvmConst(base: *Value, ofile: *ObjectFile) (error{OutOfMemory}!?llvm.ValueRef) {60 pub fn getLlvmConst(base: *Value, ofile: *ObjectFile) (error{OutOfMemory}!?*llvm.Value) {
61 switch (base.id) {61 switch (base.id) {
62 Id.Type => unreachable,62 Id.Type => unreachable,
63 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmConst(ofile),63 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmConst(ofile),
...@@ -153,7 +153,7 @@ pub const Value = struct {...@@ -153,7 +153,7 @@ pub const Value = struct {
153 comp.gpa().destroy(self);153 comp.gpa().destroy(self);
154 }154 }
155155
156 pub fn getLlvmConst(self: *FnProto, ofile: *ObjectFile) !?llvm.ValueRef {156 pub fn getLlvmConst(self: *FnProto, ofile: *ObjectFile) !?*llvm.Value {
157 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);157 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
158 const llvm_fn = llvm.AddFunction(158 const llvm_fn = llvm.AddFunction(
159 ofile.module,159 ofile.module,
...@@ -238,7 +238,7 @@ pub const Value = struct {...@@ -238,7 +238,7 @@ pub const Value = struct {
238 /// We know that the function definition will end up in an .o file somewhere.238 /// We know that the function definition will end up in an .o file somewhere.
239 /// Here, all we have to do is generate a global prototype.239 /// Here, all we have to do is generate a global prototype.
240 /// TODO cache the prototype per ObjectFile240 /// TODO cache the prototype per ObjectFile
241 pub fn getLlvmConst(self: *Fn, ofile: *ObjectFile) !?llvm.ValueRef {241 pub fn getLlvmConst(self: *Fn, ofile: *ObjectFile) !?*llvm.Value {
242 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);242 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
243 const llvm_fn = llvm.AddFunction(243 const llvm_fn = llvm.AddFunction(
244 ofile.module,244 ofile.module,
...@@ -283,7 +283,7 @@ pub const Value = struct {...@@ -283,7 +283,7 @@ pub const Value = struct {
283 comp.gpa().destroy(self);283 comp.gpa().destroy(self);
284 }284 }
285285
286 pub fn getLlvmConst(self: *Bool, ofile: *ObjectFile) ?llvm.ValueRef {286 pub fn getLlvmConst(self: *Bool, ofile: *ObjectFile) ?*llvm.Value {
287 const llvm_type = llvm.Int1TypeInContext(ofile.context);287 const llvm_type = llvm.Int1TypeInContext(ofile.context);
288 if (self.x) {288 if (self.x) {
289 return llvm.ConstAllOnes(llvm_type);289 return llvm.ConstAllOnes(llvm_type);
...@@ -381,7 +381,7 @@ pub const Value = struct {...@@ -381,7 +381,7 @@ pub const Value = struct {
381 comp.gpa().destroy(self);381 comp.gpa().destroy(self);
382 }382 }
383383
384 pub fn getLlvmConst(self: *Ptr, ofile: *ObjectFile) !?llvm.ValueRef {384 pub fn getLlvmConst(self: *Ptr, ofile: *ObjectFile) !?*llvm.Value {
385 const llvm_type = self.base.typ.getLlvmType(ofile.arena, ofile.context);385 const llvm_type = self.base.typ.getLlvmType(ofile.arena, ofile.context);
386 // TODO carefully port the logic from codegen.cpp:gen_const_val_ptr386 // TODO carefully port the logic from codegen.cpp:gen_const_val_ptr
387 switch (self.special) {387 switch (self.special) {
...@@ -391,7 +391,7 @@ pub const Value = struct {...@@ -391,7 +391,7 @@ pub const Value = struct {
391 const array_llvm_value = (try base_array.val.getLlvmConst(ofile)).?;391 const array_llvm_value = (try base_array.val.getLlvmConst(ofile)).?;
392 const ptr_bit_count = ofile.comp.target_ptr_bits;392 const ptr_bit_count = ofile.comp.target_ptr_bits;
393 const usize_llvm_type = llvm.IntTypeInContext(ofile.context, ptr_bit_count) orelse return error.OutOfMemory;393 const usize_llvm_type = llvm.IntTypeInContext(ofile.context, ptr_bit_count) orelse return error.OutOfMemory;
394 const indices = []llvm.ValueRef{394 const indices = []*llvm.Value{
395 llvm.ConstNull(usize_llvm_type) orelse return error.OutOfMemory,395 llvm.ConstNull(usize_llvm_type) orelse return error.OutOfMemory,
396 llvm.ConstInt(usize_llvm_type, base_array.elem_index, 0) orelse return error.OutOfMemory,396 llvm.ConstInt(usize_llvm_type, base_array.elem_index, 0) orelse return error.OutOfMemory,
397 };397 };
...@@ -459,7 +459,7 @@ pub const Value = struct {...@@ -459,7 +459,7 @@ pub const Value = struct {
459 comp.gpa().destroy(self);459 comp.gpa().destroy(self);
460 }460 }
461461
462 pub fn getLlvmConst(self: *Array, ofile: *ObjectFile) !?llvm.ValueRef {462 pub fn getLlvmConst(self: *Array, ofile: *ObjectFile) !?*llvm.Value {
463 switch (self.special) {463 switch (self.special) {
464 Special.Undefined => {464 Special.Undefined => {
465 const llvm_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);465 const llvm_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
...@@ -534,7 +534,7 @@ pub const Value = struct {...@@ -534,7 +534,7 @@ pub const Value = struct {
534 return self;534 return self;
535 }535 }
536536
537 pub fn getLlvmConst(self: *Int, ofile: *ObjectFile) !?llvm.ValueRef {537 pub fn getLlvmConst(self: *Int, ofile: *ObjectFile) !?*llvm.Value {
538 switch (self.base.typ.id) {538 switch (self.base.typ.id) {
539 Type.Id.Int => {539 Type.Id.Int => {
540 const type_ref = try self.base.typ.getLlvmType(ofile.arena, ofile.context);540 const type_ref = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
src/ir.cpp+1-1
...@@ -8696,7 +8696,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8696,7 +8696,7 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8696 return result;8696 return result;
8697 }8697 }
8698 bool ok_allows_zero = (wanted_allows_zero &&8698 bool ok_allows_zero = (wanted_allows_zero &&
8699 (actual_allows_zero || wanted_ptr_type->data.pointer.is_const)) ||8699 (actual_allows_zero || !wanted_is_mutable)) ||
8700 (!wanted_allows_zero && !actual_allows_zero);8700 (!wanted_allows_zero && !actual_allows_zero);
8701 if (!ok_allows_zero) {8701 if (!ok_allows_zero) {
8702 result.id = ConstCastResultIdBadAllowsZero;8702 result.id = ConstCastResultIdBadAllowsZero;
std/hash_map.zig+2
...@@ -496,6 +496,7 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type...@@ -496,6 +496,7 @@ pub fn autoHash(key: var, comptime rng: *std.rand.Random, comptime HashInt: type
496 builtin.TypeId.Pointer => |info| switch (info.size) {496 builtin.TypeId.Pointer => |info| switch (info.size) {
497 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto hash for single item pointers"),497 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto hash for single item pointers"),
498 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto hash for many item pointers"),498 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto hash for many item pointers"),
499 builtin.TypeInfo.Pointer.Size.C => @compileError("TODO auto hash C pointers"),
499 builtin.TypeInfo.Pointer.Size.Slice => {500 builtin.TypeInfo.Pointer.Size.Slice => {
500 const interval = std.math.max(1, key.len / 256);501 const interval = std.math.max(1, key.len / 256);
501 var i: usize = 0;502 var i: usize = 0;
...@@ -543,6 +544,7 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {...@@ -543,6 +544,7 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {
543 builtin.TypeId.Pointer => |info| switch (info.size) {544 builtin.TypeId.Pointer => |info| switch (info.size) {
544 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto eql for single item pointers"),545 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto eql for single item pointers"),
545 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto eql for many item pointers"),546 builtin.TypeInfo.Pointer.Size.Many => @compileError("TODO auto eql for many item pointers"),
547 builtin.TypeInfo.Pointer.Size.C => @compileError("TODO auto eql for C pointers"),
546 builtin.TypeInfo.Pointer.Size.Slice => {548 builtin.TypeInfo.Pointer.Size.Slice => {
547 if (a.len != b.len) return false;549 if (a.len != b.len) return false;
548 for (a) |a_item, i| {550 for (a) |a_item, i| {
test/compile_errors.zig+4-4
...@@ -92,15 +92,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -92,15 +92,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
92 \\ var slice: []u8 = &buf;92 \\ var slice: []u8 = &buf;
93 \\ var opt_many_ptr: [*]u8 = slice.ptr;93 \\ var opt_many_ptr: [*]u8 = slice.ptr;
94 \\ var ptr_opt_many_ptr = &opt_many_ptr;94 \\ var ptr_opt_many_ptr = &opt_many_ptr;
95 \\ var c_ptr: [*c]const [*c]u8 = ptr_opt_many_ptr;95 \\ var c_ptr: [*c][*c]const u8 = ptr_opt_many_ptr;
96 \\}96 \\}
97 ,97 ,
98 ".tmp_source.zig:6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'",98 ".tmp_source.zig:6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'",
99 ".tmp_source.zig:6:24: note: pointer type child '[*c]const u8' cannot cast into pointer type child '[*]const u8'",99 ".tmp_source.zig:6:24: note: pointer type child '[*c]const u8' cannot cast into pointer type child '[*]const u8'",
100 ".tmp_source.zig:6:24: note: '[*c]const u8' could have null values which are illegal in type '[*]const u8'",100 ".tmp_source.zig:6:24: note: '[*c]const u8' could have null values which are illegal in type '[*]const u8'",
101 ".tmp_source.zig:13:35: error: expected type '[*c]const [*c]u8', found '*[*]u8'",101 ".tmp_source.zig:13:35: error: expected type '[*c][*c]const u8', found '*[*]u8'",
102 ".tmp_source.zig:13:35: note: pointer type child '[*]u8' cannot cast into pointer type child '[*c]u8'",102 ".tmp_source.zig:13:35: note: pointer type child '[*]u8' cannot cast into pointer type child '[*c]const u8'",
103 ".tmp_source.zig:13:35: note: mutable '[*c]u8' allows illegal null values stored to type '[*]u8'",103 ".tmp_source.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'",
104 );104 );
105105
106 cases.addTest(106 cases.addTest(
test/stage1/behavior/pointers.zig+17
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectError = std.testing.expectError;
34
4test "dereference pointer" {5test "dereference pointer" {
5 comptime testDerefPtr();6 comptime testDerefPtr();
...@@ -107,3 +108,19 @@ test "implicit casting between C pointer and optional non-C pointer" {...@@ -107,3 +108,19 @@ test "implicit casting between C pointer and optional non-C pointer" {
107 ptr_opt_many_ptr = c_ptr;108 ptr_opt_many_ptr = c_ptr;
108 expect(ptr_opt_many_ptr.*.?[1] == 'o');109 expect(ptr_opt_many_ptr.*.?[1] == 'o');
109}110}
111
112test "implicit cast error unions with non-optional to optional pointer" {
113 const S = struct {
114 fn doTheTest() void {
115 expectError(error.Fail, foo());
116 }
117 fn foo() anyerror!?*u8 {
118 return bar() orelse error.Fail;
119 }
120 fn bar() ?*u8 {
121 return null;
122 }
123 };
124 S.doTheTest();
125 comptime S.doTheTest();
126}