Skip to content

Commit 6769183

Browse files
committed
fix implicit cast error unions with non-optional to optional pointer
and update self hosted compiler for C pointers See #1059
1 parent 52c03de commit 6769183

File tree

13 files changed

+228
-131
lines changed

13 files changed

+228
-131
lines changed

doc/docgen.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
137137

138138
pub 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

324324
fn 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

336336
fn 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

353353
fn 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

365-
fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8) !void {
365+
fn 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

369-
fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: []const u8) !void {
369+
fn 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

373-
fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: llvm.ValueRef, attr_name: []const u8, attr_val: u64) !void {
373+
fn 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

377377
fn 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

393-
fn renderLoad(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Pointer, name: [*]const u8) !llvm.ValueRef {
393+
fn 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

397-
pub fn getHandleValue(ofile: *ObjectFile, ptr: llvm.ValueRef, ptr_type: *Type.Pointer) !?llvm.ValueRef {
397+
pub 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

408408
pub 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

424424
pub 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,15 +435,15 @@ 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;
442442
llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm_var_type));
443443
return result;
444444
}
445445

446-
pub fn resolveAlign(ofile: *ObjectFile, alignment: Type.Pointer.Align, llvm_type: llvm.TypeRef) u32 {
446+
pub 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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
3737
/// Data that is local to the event loop.
3838
pub 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

116116
pub 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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

0 commit comments

Comments
 (0)