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
signaturelock-open 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
916916 std.zig.Token.Id.AngleBracketAngleBracketRightEqual,
917917 std.zig.Token.Id.Tilde,
918918 std.zig.Token.Id.BracketStarBracket,
919 std.zig.Token.Id.BracketStarCBracket,
919920 => try writeEscaped(out, src[token.start..token.end]),
920921
921922 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)
137137
138138pub const ObjectFile = struct {
139139 comp: *Compilation,
140 module: llvm.ModuleRef,
141 builder: llvm.BuilderRef,
140 module: *llvm.Module,
141 builder: *llvm.Builder,
142142 dibuilder: *llvm.DIBuilder,
143 context: llvm.ContextRef,
143 context: *llvm.Context,
144144 lock: event.Lock,
145145 arena: *std.mem.Allocator,
146146
......@@ -323,7 +323,7 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
323323
324324fn addLLVMAttr(
325325 ofile: *ObjectFile,
326 val: llvm.ValueRef,
326 val: *llvm.Value,
327327 attr_index: llvm.AttributeIndex,
328328 attr_name: []const u8,
329329) !void {
......@@ -335,7 +335,7 @@ fn addLLVMAttr(
335335
336336fn addLLVMAttrStr(
337337 ofile: *ObjectFile,
338 val: llvm.ValueRef,
338 val: *llvm.Value,
339339 attr_index: llvm.AttributeIndex,
340340 attr_name: []const u8,
341341 attr_val: []const u8,
......@@ -351,7 +351,7 @@ fn addLLVMAttrStr(
351351}
352352
353353fn addLLVMAttrInt(
354 val: llvm.ValueRef,
354 val: *llvm.Value,
355355 attr_index: llvm.AttributeIndex,
356356 attr_name: []const u8,
357357 attr_val: u64,
......@@ -362,25 +362,25 @@ fn addLLVMAttrInt(
362362 llvm.AddAttributeAtIndex(val, attr_index, llvm_attr);
363363}
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 {
366366 return addLLVMAttr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name);
367367}
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 {
370370 return addLLVMAttrStr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
371371}
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 {
374374 return addLLVMAttrInt(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val);
375375}
376376
377377fn renderLoadUntyped(
378378 ofile: *ObjectFile,
379 ptr: llvm.ValueRef,
379 ptr: *llvm.Value,
380380 alignment: Type.Pointer.Align,
381381 vol: Type.Pointer.Vol,
382382 name: [*]const u8,
383) !llvm.ValueRef {
383) !*llvm.Value {
384384 const result = llvm.BuildLoad(ofile.builder, ptr, name) orelse return error.OutOfMemory;
385385 switch (vol) {
386386 Type.Pointer.Vol.Non => {},
......@@ -390,11 +390,11 @@ fn renderLoadUntyped(
390390 return result;
391391}
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 {
394394 return renderLoadUntyped(ofile, ptr, ptr_type.key.alignment, ptr_type.key.vol, name);
395395}
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 {
398398 const child_type = ptr_type.key.child_type;
399399 if (!child_type.hasBits()) {
400400 return null;
......@@ -407,11 +407,11 @@ pub fn getHandleValue(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Po
407407
408408pub fn renderStoreUntyped(
409409 ofile: *ObjectFile,
410 value: llvm.ValueRef,
411 ptr: llvm.ValueRef,
410 value: *llvm.Value,
411 ptr: *llvm.Value,
412412 alignment: Type.Pointer.Align,
413413 vol: Type.Pointer.Vol,
414) !llvm.ValueRef {
414) !*llvm.Value {
415415 const result = llvm.BuildStore(ofile.builder, value, ptr) orelse return error.OutOfMemory;
416416 switch (vol) {
417417 Type.Pointer.Vol.Non => {},
......@@ -423,10 +423,10 @@ pub fn renderStoreUntyped(
423423
424424pub fn renderStore(
425425 ofile: *ObjectFile,
426 value: llvm.ValueRef,
427 ptr: llvm.ValueRef,
426 value: *llvm.Value,
427 ptr: *llvm.Value,
428428 ptr_type: *Type.Pointer,
429) !llvm.ValueRef {
429) !*llvm.Value {
430430 return renderStoreUntyped(ofile, value, ptr, ptr_type.key.alignment, ptr_type.key.vol);
431431}
432432
......@@ -435,7 +435,7 @@ pub fn renderAlloca(
435435 var_type: *Type,
436436 name: []const u8,
437437 alignment: Type.Pointer.Align,
438) !llvm.ValueRef {
438) !*llvm.Value {
439439 const llvm_var_type = try var_type.getLlvmType(ofile.arena, ofile.context);
440440 const name_with_null = try std.cstr.addNullByte(ofile.arena, name);
441441 const result = llvm.BuildAlloca(ofile.builder, llvm_var_type, name_with_null.ptr) orelse return error.OutOfMemory;
......@@ -443,7 +443,7 @@ pub fn renderAlloca(
443443 return result;
444444}
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 {
447447 return switch (alignment) {
448448 Type.Pointer.Align.Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type),
449449 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
3737/// Data that is local to the event loop.
3838pub const ZigCompiler = struct {
3939 loop: *event.Loop,
40 llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),
40 llvm_handle_pool: std.atomic.Stack(*llvm.Context),
4141 lld_lock: event.Lock,
4242
4343 /// TODO pool these so that it doesn't have to lock
......@@ -60,7 +60,7 @@ pub const ZigCompiler = struct {
6060 return ZigCompiler{
6161 .loop = loop,
6262 .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(),
6464 .prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
6565 .native_libc = event.Future(LibCInstallation).init(loop),
6666 };
......@@ -70,7 +70,7 @@ pub const ZigCompiler = struct {
7070 fn deinit(self: *ZigCompiler) void {
7171 self.lld_lock.deinit();
7272 while (self.llvm_handle_pool.pop()) |node| {
73 c.LLVMContextDispose(node.data);
73 llvm.ContextDispose(node.data);
7474 self.loop.allocator.destroy(node);
7575 }
7676 }
......@@ -80,11 +80,11 @@ pub const ZigCompiler = struct {
8080 pub fn getAnyLlvmContext(self: *ZigCompiler) !LlvmHandle {
8181 if (self.llvm_handle_pool.pop()) |node| return LlvmHandle{ .node = node };
8282
83 const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory;
84 errdefer c.LLVMContextDispose(context_ref);
83 const context_ref = llvm.ContextCreate() orelse return error.OutOfMemory;
84 errdefer llvm.ContextDispose(context_ref);
8585
86 const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node);
87 node.* = 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.Context).Node{
8888 .next = undefined,
8989 .data = context_ref,
9090 };
......@@ -114,7 +114,7 @@ pub const ZigCompiler = struct {
114114};
115115
116116pub const LlvmHandle = struct {
117 node: *std.atomic.Stack(llvm.ContextRef).Node,
117 node: *std.atomic.Stack(*llvm.Context).Node,
118118
119119 pub fn release(self: LlvmHandle, zig_compiler: *ZigCompiler) void {
120120 zig_compiler.llvm_handle_pool.push(self.node);
......@@ -128,7 +128,7 @@ pub const Compilation = struct {
128128 llvm_triple: Buffer,
129129 root_src_path: ?[]const u8,
130130 target: Target,
131 llvm_target: llvm.TargetRef,
131 llvm_target: *llvm.Target,
132132 build_mode: builtin.Mode,
133133 zig_lib_dir: []const u8,
134134 zig_std_dir: []const u8,
......@@ -212,8 +212,8 @@ pub const Compilation = struct {
212212 false_value: *Value.Bool,
213213 noreturn_value: *Value.NoReturn,
214214
215 target_machine: llvm.TargetMachineRef,
216 target_data_ref: llvm.TargetDataRef,
215 target_machine: *llvm.TargetMachine,
216 target_data_ref: *llvm.TargetData,
217217 target_layout_str: [*]u8,
218218 target_ptr_bits: u32,
219219
src-self-hosted/ir.zig+10-10
......@@ -67,7 +67,7 @@ pub const Inst = struct {
6767 parent: ?*Inst,
6868
6969 /// populated durign codegen
70 llvm_value: ?llvm.ValueRef,
70 llvm_value: ?*llvm.Value,
7171
7272 pub fn cast(base: *Inst, comptime T: type) ?*T {
7373 if (base.id == comptime typeToId(T)) {
......@@ -129,7 +129,7 @@ pub const Inst = struct {
129129 }
130130 }
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) {
133133 switch (base.id) {
134134 Id.Return => return @fieldParentPtr(Return, "base", base).render(ofile, fn_val),
135135 Id.Const => return @fieldParentPtr(Const, "base", base).render(ofile, fn_val),
......@@ -313,10 +313,10 @@ pub const Inst = struct {
313313 return new_inst;
314314 }
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 {
317317 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);
320320 for (self.params.args) |arg, i| {
321321 args[i] = arg.llvm_value.?;
322322 }
......@@ -360,7 +360,7 @@ pub const Inst = struct {
360360 return new_inst;
361361 }
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 {
364364 return self.base.val.KnownValue.getLlvmConst(ofile);
365365 }
366366 };
......@@ -392,7 +392,7 @@ pub const Inst = struct {
392392 return ira.irb.build(Return, self.base.scope, self.base.span, Params{ .return_value = casted_value });
393393 }
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 {
396396 const value = self.params.return_value.llvm_value;
397397 const return_type = self.params.return_value.getKnownType();
398398
......@@ -540,7 +540,7 @@ pub const Inst = struct {
540540 }
541541 }
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 {
544544 switch (self.params.var_scope.data) {
545545 Scope.Var.Data.Const => unreachable, // turned into Inst.Const in analyze pass
546546 Scope.Var.Data.Param => |param| return param.llvm_value,
......@@ -596,7 +596,7 @@ pub const Inst = struct {
596596 return new_inst;
597597 }
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 {
600600 const child_type = self.base.getKnownType();
601601 if (!child_type.hasBits()) {
602602 return null;
......@@ -935,8 +935,8 @@ pub const BasicBlock = struct {
935935 ref_instruction: ?*Inst,
936936
937937 /// for codegen
938 llvm_block: llvm.BasicBlockRef,
939 llvm_exit_block: llvm.BasicBlockRef,
938 llvm_block: *llvm.BasicBlock,
939 llvm_exit_block: *llvm.BasicBlock,
940940
941941 /// the basic block that is derived from this one in analysis
942942 child: ?*BasicBlock,
src-self-hosted/llvm.zig+129-52
......@@ -11,45 +11,31 @@ const assert = @import("std").debug.assert;
1111pub const AttributeIndex = c_uint;
1212pub const Bool = c_int;
1313
14pub const BuilderRef = removeNullability(c.LLVMBuilderRef);
15pub const ContextRef = removeNullability(c.LLVMContextRef);
16pub const ModuleRef = removeNullability(c.LLVMModuleRef);
17pub const ValueRef = removeNullability(c.LLVMValueRef);
18pub const TypeRef = removeNullability(c.LLVMTypeRef);
19pub const BasicBlockRef = removeNullability(c.LLVMBasicBlockRef);
20pub const AttributeRef = removeNullability(c.LLVMAttributeRef);
21pub const TargetRef = removeNullability(c.LLVMTargetRef);
22pub const TargetMachineRef = removeNullability(c.LLVMTargetMachineRef);
23pub const TargetDataRef = removeNullability(c.LLVMTargetDataRef);
14pub const Builder = c.LLVMBuilderRef.Child;
15pub const Context = c.LLVMContextRef.Child;
16pub const Module = c.LLVMModuleRef.Child;
17pub const Value = c.LLVMValueRef.Child;
18pub const Type = c.LLVMTypeRef.Child;
19pub const BasicBlock = c.LLVMBasicBlockRef.Child;
20pub const Attribute = c.LLVMAttributeRef.Child;
21pub const Target = c.LLVMTargetRef.Child;
22pub const TargetMachine = c.LLVMTargetMachineRef.Child;
23pub const TargetData = c.LLVMTargetDataRef.Child;
2424pub const DIBuilder = c.ZigLLVMDIBuilder;
25pub const DIFile = c.ZigLLVMDIFile;
26pub const DICompileUnit = c.ZigLLVMDICompileUnit;
2527
2628pub const ABIAlignmentOfType = c.LLVMABIAlignmentOfType;
2729pub const AddAttributeAtIndex = c.LLVMAddAttributeAtIndex;
28pub const AddFunction = c.LLVMAddFunction;
29pub const AddGlobal = c.LLVMAddGlobal;
3030pub const AddModuleCodeViewFlag = c.ZigLLVMAddModuleCodeViewFlag;
3131pub const AddModuleDebugInfoFlag = c.ZigLLVMAddModuleDebugInfoFlag;
32pub const ArrayType = c.LLVMArrayType;
33pub const BuildLoad = c.LLVMBuildLoad;
3432pub const ClearCurrentDebugLocation = c.ZigLLVMClearCurrentDebugLocation;
3533pub const ConstAllOnes = c.LLVMConstAllOnes;
3634pub const ConstArray = c.LLVMConstArray;
3735pub const ConstBitCast = c.LLVMConstBitCast;
38pub const ConstInt = c.LLVMConstInt;
3936pub const ConstIntOfArbitraryPrecision = c.LLVMConstIntOfArbitraryPrecision;
4037pub const ConstNeg = c.LLVMConstNeg;
41pub const ConstNull = c.LLVMConstNull;
42pub const ConstStringInContext = c.LLVMConstStringInContext;
4338pub 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;
5339pub const DIBuilderFinalize = c.ZigLLVMDIBuilderFinalize;
5440pub const DisposeBuilder = c.LLVMDisposeBuilder;
5541pub const DisposeDIBuilder = c.ZigLLVMDisposeDIBuilder;
......@@ -62,9 +48,7 @@ pub const DumpModule = c.LLVMDumpModule;
6248pub const FP128TypeInContext = c.LLVMFP128TypeInContext;
6349pub const FloatTypeInContext = c.LLVMFloatTypeInContext;
6450pub const GetEnumAttributeKindForName = c.LLVMGetEnumAttributeKindForName;
65pub const GetHostCPUName = c.ZigLLVMGetHostCPUName;
6651pub const GetMDKindIDInContext = c.LLVMGetMDKindIDInContext;
67pub const GetNativeFeatures = c.ZigLLVMGetNativeFeatures;
6852pub const GetUndef = c.LLVMGetUndef;
6953pub const HalfTypeInContext = c.LLVMHalfTypeInContext;
7054pub const InitializeAllAsmParsers = c.LLVMInitializeAllAsmParsers;
......@@ -81,14 +65,11 @@ pub const Int64TypeInContext = c.LLVMInt64TypeInContext;
8165pub const Int8TypeInContext = c.LLVMInt8TypeInContext;
8266pub const IntPtrTypeForASInContext = c.LLVMIntPtrTypeForASInContext;
8367pub const IntPtrTypeInContext = c.LLVMIntPtrTypeInContext;
84pub const IntTypeInContext = c.LLVMIntTypeInContext;
8568pub const LabelTypeInContext = c.LLVMLabelTypeInContext;
8669pub const MDNodeInContext = c.LLVMMDNodeInContext;
8770pub const MDStringInContext = c.LLVMMDStringInContext;
8871pub const MetadataTypeInContext = c.LLVMMetadataTypeInContext;
89pub const ModuleCreateWithNameInContext = c.LLVMModuleCreateWithNameInContext;
9072pub const PPCFP128TypeInContext = c.LLVMPPCFP128TypeInContext;
91pub const PointerType = c.LLVMPointerType;
9273pub const SetAlignment = c.LLVMSetAlignment;
9374pub const SetDataLayout = c.LLVMSetDataLayout;
9475pub const SetGlobalConstant = c.LLVMSetGlobalConstant;
......@@ -99,50 +80,146 @@ pub const SetUnnamedAddr = c.LLVMSetUnnamedAddr;
9980pub const SetVolatile = c.LLVMSetVolatile;
10081pub const StructTypeInContext = c.LLVMStructTypeInContext;
10182pub const TokenTypeInContext = c.LLVMTokenTypeInContext;
102pub const VoidTypeInContext = c.LLVMVoidTypeInContext;
10383pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;
10484pub 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
106183pub const GetElementType = LLVMGetElementType;
107extern fn LLVMGetElementType(Ty: TypeRef) TypeRef;
184extern fn LLVMGetElementType(Ty: *Type) *Type;
108185
109186pub const TypeOf = LLVMTypeOf;
110extern fn LLVMTypeOf(Val: ValueRef) TypeRef;
187extern fn LLVMTypeOf(Val: *Value) *Type;
111188
112189pub const BuildStore = LLVMBuildStore;
113extern fn LLVMBuildStore(arg0: BuilderRef, Val: ValueRef, Ptr: ValueRef) ?ValueRef;
190extern fn LLVMBuildStore(arg0: *Builder, Val: *Value, Ptr: *Value) ?*Value;
114191
115192pub 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
118195pub 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
121198pub 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
124201pub const VerifyModule = LLVMVerifyModule;
125extern fn LLVMVerifyModule(M: ModuleRef, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool;
202extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool;
126203
127204pub const GetInsertBlock = LLVMGetInsertBlock;
128extern fn LLVMGetInsertBlock(Builder: BuilderRef) BasicBlockRef;
205extern fn LLVMGetInsertBlock(Builder: *Builder) *BasicBlock;
129206
130207pub const FunctionType = LLVMFunctionType;
131208extern fn LLVMFunctionType(
132 ReturnType: TypeRef,
133 ParamTypes: [*]TypeRef,
209 ReturnType: *Type,
210 ParamTypes: [*]*Type,
134211 ParamCount: c_uint,
135212 IsVarArg: Bool,
136) ?TypeRef;
213) ?*Type;
137214
138215pub const GetParam = LLVMGetParam;
139extern fn LLVMGetParam(Fn: ValueRef, Index: c_uint) ValueRef;
216extern fn LLVMGetParam(Fn: *Value, Index: c_uint) *Value;
140217
141218pub 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
144221pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd;
145extern fn LLVMPositionBuilderAtEnd(Builder: BuilderRef, Block: BasicBlockRef) void;
222extern fn LLVMPositionBuilderAtEnd(Builder: *Builder, Block: *BasicBlock) void;
146223
147224pub const AbortProcessAction = VerifierFailureAction.LLVMAbortProcessAction;
148225pub const PrintMessageAction = VerifierFailureAction.LLVMPrintMessageAction;
......@@ -190,17 +267,17 @@ pub const FnInline = extern enum {
190267};
191268
192269fn removeNullability(comptime T: type) type {
193 comptime assert(@typeId(T) == builtin.TypeId.Optional);
194 return T.Child;
270 comptime assert(@typeInfo(T).Pointer.size == @import("builtin").TypeInfo.Pointer.Size.C);
271 return *T.Child;
195272}
196273
197274pub const BuildRet = LLVMBuildRet;
198extern fn LLVMBuildRet(arg0: BuilderRef, V: ?ValueRef) ?ValueRef;
275extern fn LLVMBuildRet(arg0: *Builder, V: ?*Value) ?*Value;
199276
200277pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile;
201278extern fn ZigLLVMTargetMachineEmitToFile(
202 targ_machine_ref: TargetMachineRef,
203 module_ref: ModuleRef,
279 targ_machine_ref: *TargetMachine,
280 module_ref: *Module,
204281 filename: [*]const u8,
205282 output_type: EmitOutputType,
206283 error_message: *[*]u8,
......@@ -209,6 +286,6 @@ extern fn ZigLLVMTargetMachineEmitToFile(
209286) bool;
210287
211288pub 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
214291pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;
src-self-hosted/scope.zig+1-1
......@@ -362,7 +362,7 @@ pub const Scope = struct {
362362 pub const Param = struct {
363363 index: usize,
364364 typ: *Type,
365 llvm_value: llvm.ValueRef,
365 llvm_value: *llvm.Value,
366366 };
367367
368368 pub fn createParam(
src-self-hosted/target.zig+2-2
......@@ -457,8 +457,8 @@ pub const Target = union(enum) {
457457 }
458458 }
459459
460 pub fn llvmTargetFromTriple(triple: std.Buffer) !llvm.TargetRef {
461 var result: llvm.TargetRef = undefined;
460 pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target {
461 var result: *llvm.Target = undefined;
462462 var err_msg: [*]u8 = undefined;
463463 if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) {
464464 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 {
5151 pub fn getLlvmType(
5252 base: *Type,
5353 allocator: *Allocator,
54 llvm_context: llvm.ContextRef,
55 ) (error{OutOfMemory}!llvm.TypeRef) {
54 llvm_context: *llvm.Context,
55 ) (error{OutOfMemory}!*llvm.Type) {
5656 switch (base.id) {
5757 Id.Struct => return @fieldParentPtr(Struct, "base", base).getLlvmType(allocator, llvm_context),
5858 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmType(allocator, llvm_context),
......@@ -196,7 +196,7 @@ pub const Type = struct {
196196 }
197197
198198 /// 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 {
200200 if (await (async base.abi_alignment.start() catch unreachable)) |ptr| return ptr.*;
201201
202202 base.abi_alignment.data = await (async base.resolveAbiAlignment(comp, llvm_context) catch unreachable);
......@@ -205,7 +205,7 @@ pub const Type = struct {
205205 }
206206
207207 /// 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 {
209209 const llvm_type = try base.getLlvmType(comp.gpa(), llvm_context);
210210 return @intCast(u32, llvm.ABIAlignmentOfType(comp.target_data_ref, llvm_type));
211211 }
......@@ -218,7 +218,7 @@ pub const Type = struct {
218218 comp.gpa().destroy(self);
219219 }
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 {
222222 @panic("TODO");
223223 }
224224 };
......@@ -496,13 +496,13 @@ pub const Type = struct {
496496 comp.gpa().destroy(self);
497497 }
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 {
500500 const normal = &self.key.data.Normal;
501501 const llvm_return_type = switch (normal.return_type.id) {
502502 Type.Id.Void => llvm.VoidTypeInContext(llvm_context) orelse return error.OutOfMemory,
503503 else => try normal.return_type.getLlvmType(allocator, llvm_context),
504504 };
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);
506506 defer allocator.free(llvm_param_types);
507507 for (llvm_param_types) |*llvm_param_type, i| {
508508 llvm_param_type.* = try normal.params[i].typ.getLlvmType(allocator, llvm_context);
......@@ -559,7 +559,7 @@ pub const Type = struct {
559559 comp.gpa().destroy(self);
560560 }
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 {
563563 @panic("TODO");
564564 }
565565 };
......@@ -658,7 +658,7 @@ pub const Type = struct {
658658 comp.gpa().destroy(self);
659659 }
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 {
662662 return llvm.IntTypeInContext(llvm_context, self.key.bit_count) orelse return error.OutOfMemory;
663663 }
664664 };
......@@ -670,7 +670,7 @@ pub const Type = struct {
670670 comp.gpa().destroy(self);
671671 }
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 {
674674 @panic("TODO");
675675 }
676676 };
......@@ -836,7 +836,7 @@ pub const Type = struct {
836836 return self;
837837 }
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 {
840840 const elem_llvm_type = try self.key.child_type.getLlvmType(allocator, llvm_context);
841841 return llvm.PointerType(elem_llvm_type, 0) orelse return error.OutOfMemory;
842842 }
......@@ -904,7 +904,7 @@ pub const Type = struct {
904904 return self;
905905 }
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 {
908908 const elem_llvm_type = try self.key.elem_type.getLlvmType(allocator, llvm_context);
909909 return llvm.ArrayType(elem_llvm_type, @intCast(c_uint, self.key.len)) orelse return error.OutOfMemory;
910910 }
......@@ -917,7 +917,7 @@ pub const Type = struct {
917917 comp.gpa().destroy(self);
918918 }
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 {
921921 @panic("TODO");
922922 }
923923 };
......@@ -967,7 +967,7 @@ pub const Type = struct {
967967 comp.gpa().destroy(self);
968968 }
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 {
971971 @panic("TODO");
972972 }
973973 };
......@@ -979,7 +979,7 @@ pub const Type = struct {
979979 comp.gpa().destroy(self);
980980 }
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 {
983983 @panic("TODO");
984984 }
985985 };
......@@ -991,7 +991,7 @@ pub const Type = struct {
991991 comp.gpa().destroy(self);
992992 }
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 {
995995 @panic("TODO");
996996 }
997997 };
......@@ -1003,7 +1003,7 @@ pub const Type = struct {
10031003 comp.gpa().destroy(self);
10041004 }
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 {
10071007 @panic("TODO");
10081008 }
10091009 };
......@@ -1015,7 +1015,7 @@ pub const Type = struct {
10151015 comp.gpa().destroy(self);
10161016 }
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 {
10191019 @panic("TODO");
10201020 }
10211021 };
......@@ -1035,7 +1035,7 @@ pub const Type = struct {
10351035 comp.gpa().destroy(self);
10361036 }
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 {
10391039 @panic("TODO");
10401040 }
10411041 };
......@@ -1055,7 +1055,7 @@ pub const Type = struct {
10551055 comp.gpa().destroy(self);
10561056 }
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 {
10591059 @panic("TODO");
10601060 }
10611061 };
......@@ -1067,7 +1067,7 @@ pub const Type = struct {
10671067 comp.gpa().destroy(self);
10681068 }
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 {
10711071 @panic("TODO");
10721072 }
10731073 };
src-self-hosted/value.zig+8-8
......@@ -57,7 +57,7 @@ pub const Value = struct {
5757 std.debug.warn("{}", @tagName(base.id));
5858 }
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) {
6161 switch (base.id) {
6262 Id.Type => unreachable,
6363 Id.Fn => return @fieldParentPtr(Fn, "base", base).getLlvmConst(ofile),
......@@ -153,7 +153,7 @@ pub const Value = struct {
153153 comp.gpa().destroy(self);
154154 }
155155
156 pub fn getLlvmConst(self: *FnProto, ofile: *ObjectFile) !?llvm.ValueRef {
156 pub fn getLlvmConst(self: *FnProto, ofile: *ObjectFile) !?*llvm.Value {
157157 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
158158 const llvm_fn = llvm.AddFunction(
159159 ofile.module,
......@@ -238,7 +238,7 @@ pub const Value = struct {
238238 /// We know that the function definition will end up in an .o file somewhere.
239239 /// Here, all we have to do is generate a global prototype.
240240 /// 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 {
242242 const llvm_fn_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
243243 const llvm_fn = llvm.AddFunction(
244244 ofile.module,
......@@ -283,7 +283,7 @@ pub const Value = struct {
283283 comp.gpa().destroy(self);
284284 }
285285
286 pub fn getLlvmConst(self: *Bool, ofile: *ObjectFile) ?llvm.ValueRef {
286 pub fn getLlvmConst(self: *Bool, ofile: *ObjectFile) ?*llvm.Value {
287287 const llvm_type = llvm.Int1TypeInContext(ofile.context);
288288 if (self.x) {
289289 return llvm.ConstAllOnes(llvm_type);
......@@ -381,7 +381,7 @@ pub const Value = struct {
381381 comp.gpa().destroy(self);
382382 }
383383
384 pub fn getLlvmConst(self: *Ptr, ofile: *ObjectFile) !?llvm.ValueRef {
384 pub fn getLlvmConst(self: *Ptr, ofile: *ObjectFile) !?*llvm.Value {
385385 const llvm_type = self.base.typ.getLlvmType(ofile.arena, ofile.context);
386386 // TODO carefully port the logic from codegen.cpp:gen_const_val_ptr
387387 switch (self.special) {
......@@ -391,7 +391,7 @@ pub const Value = struct {
391391 const array_llvm_value = (try base_array.val.getLlvmConst(ofile)).?;
392392 const ptr_bit_count = ofile.comp.target_ptr_bits;
393393 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{
395395 llvm.ConstNull(usize_llvm_type) orelse return error.OutOfMemory,
396396 llvm.ConstInt(usize_llvm_type, base_array.elem_index, 0) orelse return error.OutOfMemory,
397397 };
......@@ -459,7 +459,7 @@ pub const Value = struct {
459459 comp.gpa().destroy(self);
460460 }
461461
462 pub fn getLlvmConst(self: *Array, ofile: *ObjectFile) !?llvm.ValueRef {
462 pub fn getLlvmConst(self: *Array, ofile: *ObjectFile) !?*llvm.Value {
463463 switch (self.special) {
464464 Special.Undefined => {
465465 const llvm_type = try self.base.typ.getLlvmType(ofile.arena, ofile.context);
......@@ -534,7 +534,7 @@ pub const Value = struct {
534534 return self;
535535 }
536536
537 pub fn getLlvmConst(self: *Int, ofile: *ObjectFile) !?llvm.ValueRef {
537 pub fn getLlvmConst(self: *Int, ofile: *ObjectFile) !?*llvm.Value {
538538 switch (self.base.typ.id) {
539539 Type.Id.Int => {
540540 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
86968696 return result;
86978697 }
86988698 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)) ||
87008700 (!wanted_allows_zero && !actual_allows_zero);
87018701 if (!ok_allows_zero) {
87028702 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
496496 builtin.TypeId.Pointer => |info| switch (info.size) {
497497 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto hash for single item pointers"),
498498 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"),
499500 builtin.TypeInfo.Pointer.Size.Slice => {
500501 const interval = std.math.max(1, key.len / 256);
501502 var i: usize = 0;
......@@ -543,6 +544,7 @@ pub fn autoEql(a: var, b: @typeOf(a)) bool {
543544 builtin.TypeId.Pointer => |info| switch (info.size) {
544545 builtin.TypeInfo.Pointer.Size.One => @compileError("TODO auto eql for single item pointers"),
545546 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"),
546548 builtin.TypeInfo.Pointer.Size.Slice => {
547549 if (a.len != b.len) return false;
548550 for (a) |a_item, i| {
test/compile_errors.zig+4-4
......@@ -92,15 +92,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
9292 \\ var slice: []u8 = &buf;
9393 \\ var opt_many_ptr: [*]u8 = slice.ptr;
9494 \\ 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;
9696 \\}
9797 ,
9898 ".tmp_source.zig:6:24: error: expected type '*const [*]const u8', found '[*c]const [*c]const u8'",
9999 ".tmp_source.zig:6:24: note: pointer type child '[*c]const u8' cannot cast into pointer type child '[*]const u8'",
100100 ".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'",
102 ".tmp_source.zig:13:35: note: pointer type child '[*]u8' cannot cast into pointer type child '[*c]u8'",
103 ".tmp_source.zig:13:35: note: mutable '[*c]u8' allows illegal null values stored to type '[*]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]const u8'",
103 ".tmp_source.zig:13:35: note: mutable '[*c]const u8' allows illegal null values stored to type '[*]u8'",
104104 );
105105
106106 cases.addTest(
test/stage1/behavior/pointers.zig+17
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectError = std.testing.expectError;
34
45test "dereference pointer" {
56 comptime testDerefPtr();
......@@ -107,3 +108,19 @@ test "implicit casting between C pointer and optional non-C pointer" {
107108 ptr_opt_many_ptr = c_ptr;
108109 expect(ptr_opt_many_ptr.*.?[1] == 'o');
109110}
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}