Commit c3d10dbda1

Andrew Kelley <andrew@ziglang.org>
2021-07-26 04:06:48
stage2 llvm backend: implement llvmType for error union and slices
1 parent 5b4885f
Changed files (2)
src
src/codegen/llvm/bindings.zig
@@ -35,7 +35,12 @@ pub const Context = opaque {
     extern fn LLVMVoidTypeInContext(C: *const Context) *const Type;
 
     pub const structType = LLVMStructTypeInContext;
-    extern fn LLVMStructTypeInContext(C: *const Context, ElementTypes: [*]*const Type, ElementCount: c_uint, Packed: Bool) *const Type;
+    extern fn LLVMStructTypeInContext(
+        C: *const Context,
+        ElementTypes: [*]const *const Type,
+        ElementCount: c_uint,
+        Packed: Bool,
+    ) *const Type;
 
     pub const constString = LLVMConstStringInContext;
     extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value;
src/codegen/llvm.zig
@@ -534,7 +534,14 @@ pub const DeclGen = struct {
             .Bool => return self.context.intType(1),
             .Pointer => {
                 if (t.isSlice()) {
-                    return self.todo("implement slices", .{});
+                    var buf: Type.Payload.ElemType = undefined;
+                    const ptr_type = t.slicePtrFieldType(&buf);
+
+                    const fields: [2]*const llvm.Type = .{
+                        try self.llvmType(ptr_type),
+                        try self.llvmType(Type.initTag(.usize)),
+                    };
+                    return self.context.structType(&fields, 2, .False);
                 } else {
                     const elem_type = try self.llvmType(t.elemType());
                     return elem_type.pointerType(0);
@@ -549,7 +556,7 @@ pub const DeclGen = struct {
                     var buf: Type.Payload.ElemType = undefined;
                     const child_type = t.optionalChild(&buf);
 
-                    var optional_types: [2]*const llvm.Type = .{
+                    const optional_types: [2]*const llvm.Type = .{
                         try self.llvmType(child_type),
                         self.context.intType(1),
                     };
@@ -559,8 +566,16 @@ pub const DeclGen = struct {
                 }
             },
             .ErrorUnion => {
+                const error_type = t.errorUnionSet();
+                const payload_type = t.errorUnionPayload();
+                if (!payload_type.hasCodeGenBits()) {
+                    return self.llvmType(error_type);
+                }
                 return self.todo("implement llvmType for error unions", .{});
             },
+            .ErrorSet => {
+                return self.context.intType(16);
+            },
             .ComptimeInt => unreachable,
             .ComptimeFloat => unreachable,
             .Type => unreachable,
@@ -572,7 +587,6 @@ pub const DeclGen = struct {
 
             .Float,
             .Struct,
-            .ErrorSet,
             .Enum,
             .Union,
             .Fn,