Support derived to base conversions. (#3487)

This is enough to support calling methods that take a `Base` or `Base*`
as their `self`. But name lookup doesn't look in the base class yet, so
base class methods aren't actually found.
This commit is contained in:
Richard Smith
2023-12-11 23:09:01 +00:00
committed by GitHub
parent 6037b11e90
commit 87d341c61b
6 changed files with 603 additions and 26 deletions
+96
View File
@@ -571,6 +571,80 @@ static auto ConvertStructToClass(Context& context, SemIR::StructType src_type,
return result_id;
}
// An inheritance path is a sequence of `BaseDecl`s in order from derived to
// base.
using InheritancePath = llvm::SmallVector<SemIR::InstId>;
// Computes the inheritance path from class `derived_id` to class `base_id`.
// Returns nullopt if `derived_id` is not a class derived from `base_id`.
static auto ComputeInheritancePath(Context& context, SemIR::TypeId derived_id,
SemIR::TypeId base_id)
-> std::optional<InheritancePath> {
// We intend for NRVO to be applied to `result`. All `return` statements in
// this function should `return result;`.
std::optional<InheritancePath> result(std::in_place);
if (!context.TryToCompleteType(derived_id)) {
// TODO: Should we give an error here? If we don't, and there is an
// inheritance path when the class is defined, we may have a coherence
// problem.
result = std::nullopt;
return result;
}
while (derived_id != base_id) {
auto derived_class_type =
context.types().TryGetAs<SemIR::ClassType>(derived_id);
if (!derived_class_type) {
result = std::nullopt;
break;
}
auto& derived_class = context.classes().Get(derived_class_type->class_id);
if (!derived_class.base_id.is_valid()) {
result = std::nullopt;
break;
}
result->push_back(derived_class.base_id);
derived_id = context.insts()
.GetAs<SemIR::BaseDecl>(derived_class.base_id)
.base_type_id;
}
return result;
}
// Performs a conversion from a derived class value or reference to a base class
// value or reference.
static auto ConvertDerivedToBase(Context& context, Parse::NodeId parse_node,
SemIR::InstId value_id,
const InheritancePath& path) -> SemIR::InstId {
// Materialize a temporary if necessary.
value_id = ConvertToValueOrRefExpr(context, value_id);
// Add a series of `.base` accesses.
for (auto base_id : path) {
auto base_decl = context.insts().GetAs<SemIR::BaseDecl>(base_id);
value_id = context.AddInst(SemIR::ClassElementAccess{
parse_node, base_decl.base_type_id, value_id, base_decl.index});
}
return value_id;
}
// Performs a conversion from a derived class pointer to a base class pointer.
static auto ConvertDerivedPointerToBasePointer(
Context& context, Parse::NodeId parse_node, SemIR::PointerType src_ptr_type,
SemIR::TypeId dest_ptr_type_id, SemIR::InstId ptr_id,
const InheritancePath& path) -> SemIR::InstId {
// Form `*p`.
ptr_id = ConvertToValueExpr(context, ptr_id);
auto ref_id = context.AddInst(
SemIR::Deref{parse_node, src_ptr_type.pointee_id, ptr_id});
// Convert as a reference expression.
ref_id = ConvertDerivedToBase(context, parse_node, ref_id, path);
// Take the address.
return context.AddInst(
SemIR::AddressOf{parse_node, dest_ptr_type_id, ref_id});
}
// Returns whether `category` is a valid expression category to produce as a
// result of a conversion with kind `target_kind`, or at most needs a temporary
// to be materialized.
@@ -697,6 +771,28 @@ static auto PerformBuiltinConversion(Context& context, Parse::NodeId parse_node,
return ConvertStructToClass(context, *src_struct_type, *target_class_type,
value_id, target);
}
// An expression of type T converts to U if T is a class derived from U.
if (auto path =
ComputeInheritancePath(context, value_type_id, target.type_id);
path && !path->empty()) {
return ConvertDerivedToBase(context, parse_node, value_id, *path);
}
}
// A pointer T* converts to U* if T is a class derived from U.
if (auto target_pointer_type = target_type_inst.TryAs<SemIR::PointerType>()) {
if (auto src_pointer_type =
sem_ir.types().TryGetAs<SemIR::PointerType>(value_type_id)) {
if (auto path =
ComputeInheritancePath(context, src_pointer_type->pointee_id,
target_pointer_type->pointee_id);
path && !path->empty()) {
return ConvertDerivedPointerToBasePointer(
context, parse_node, *src_pointer_type, target.type_id, value_id,
*path);
}
}
}
if (target.type_id == SemIR::TypeId::TypeType) {
+193
View File
@@ -0,0 +1,193 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
base class A {
var a: i32;
}
base class B {
extend base: A;
var b: i32;
}
class C {
extend base: B;
var c: i32;
}
fn ConvertCToB(p: C*) -> B* { return p; }
fn ConvertBToA(p: B*) -> A* { return p; }
fn ConvertCToA(p: C*) -> A* { return p; }
fn ConvertValue(c: C) {
let a: A = c;
}
fn ConvertRef(c: C*) -> A* {
return &(*c as A);
}
fn ConvertInit() {
let a: A = {.base = {.base = {.a = 1}, .b = 2}, .c = 3} as C;
}
// CHECK:STDOUT: --- derived_to_base.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.loc9: type = struct_type {.a: i32}
// CHECK:STDOUT: %.loc7: type = ptr_type {.a: i32}
// CHECK:STDOUT: %.loc14_1.1: type = struct_type {.base: A, .b: i32}
// CHECK:STDOUT: %.loc14_1.2: type = struct_type {.base: {.a: i32}*, .b: i32}
// CHECK:STDOUT: %.loc14_1.3: type = ptr_type {.base: {.a: i32}*, .b: i32}
// CHECK:STDOUT: %.loc11: type = ptr_type {.base: A, .b: i32}
// CHECK:STDOUT: %.loc19_1.1: type = struct_type {.base: B, .c: i32}
// CHECK:STDOUT: %.loc19_1.2: type = struct_type {.base: {.base: A, .b: i32}*, .c: i32}
// CHECK:STDOUT: %.loc19_1.3: type = ptr_type {.base: {.base: A, .b: i32}*, .c: i32}
// CHECK:STDOUT: %.loc16: type = ptr_type {.base: B, .c: i32}
// CHECK:STDOUT: %.loc34_48: type = struct_type {.base: {.a: i32}, .b: i32}
// CHECK:STDOUT: %.loc34_57: type = struct_type {.base: {.base: {.a: i32}, .b: i32}, .c: i32}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {.A = %A.decl, .B = %B.decl, .C = %C.decl, .ConvertCToB = %ConvertCToB, .ConvertBToA = %ConvertBToA, .ConvertCToA = %ConvertCToA, .ConvertValue = %ConvertValue, .ConvertRef = %ConvertRef, .ConvertInit = %ConvertInit}
// CHECK:STDOUT: %A.decl = class_decl @A, ()
// CHECK:STDOUT: %A: type = class_type @A
// CHECK:STDOUT: %B.decl = class_decl @B, ()
// CHECK:STDOUT: %B: type = class_type @B
// CHECK:STDOUT: %C.decl = class_decl @C, ()
// CHECK:STDOUT: %C: type = class_type @C
// CHECK:STDOUT: %ConvertCToB: <function> = fn_decl @ConvertCToB
// CHECK:STDOUT: %ConvertBToA: <function> = fn_decl @ConvertBToA
// CHECK:STDOUT: %ConvertCToA: <function> = fn_decl @ConvertCToA
// CHECK:STDOUT: %ConvertValue: <function> = fn_decl @ConvertValue
// CHECK:STDOUT: %ConvertRef: <function> = fn_decl @ConvertRef
// CHECK:STDOUT: %ConvertInit: <function> = fn_decl @ConvertInit
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A {
// CHECK:STDOUT: %.loc8_8.1: type = unbound_element_type A, i32
// CHECK:STDOUT: %.loc8_8.2: <unbound element of class A> = field_decl a, element0
// CHECK:STDOUT: %a: <unbound element of class A> = bind_name a, %.loc8_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .a = %a
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @B {
// CHECK:STDOUT: %A.ref: type = name_ref A, file.%A
// CHECK:STDOUT: %.loc12_17.1: type = unbound_element_type B, A
// CHECK:STDOUT: %.loc12_17.2: <unbound element of class B> = base_decl A, element0
// CHECK:STDOUT: %.loc13_8.1: type = unbound_element_type B, i32
// CHECK:STDOUT: %.loc13_8.2: <unbound element of class B> = field_decl b, element1
// CHECK:STDOUT: %b: <unbound element of class B> = bind_name b, %.loc13_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc12_17.2
// CHECK:STDOUT: .b = %b
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: %B.ref: type = name_ref B, file.%B
// CHECK:STDOUT: %.loc17_17.1: type = unbound_element_type C, B
// CHECK:STDOUT: %.loc17_17.2: <unbound element of class C> = base_decl B, element0
// CHECK:STDOUT: %.loc18_8.1: type = unbound_element_type C, i32
// CHECK:STDOUT: %.loc18_8.2: <unbound element of class C> = field_decl c, element1
// CHECK:STDOUT: %c: <unbound element of class C> = bind_name c, %.loc18_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc17_17.2
// CHECK:STDOUT: .c = %c
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertCToB(%p: C*) -> B* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: C* = name_ref p, %p
// CHECK:STDOUT: %.loc21_39.1: ref C = deref %p.ref
// CHECK:STDOUT: %.loc21_39.2: ref B = class_element_access %.loc21_39.1, element0
// CHECK:STDOUT: %.loc21_39.3: B* = address_of %.loc21_39.2
// CHECK:STDOUT: %.loc21_39.4: B* = converted %p.ref, %.loc21_39.3
// CHECK:STDOUT: return %.loc21_39.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertBToA(%p: B*) -> A* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: B* = name_ref p, %p
// CHECK:STDOUT: %.loc22_39.1: ref B = deref %p.ref
// CHECK:STDOUT: %.loc22_39.2: ref A = class_element_access %.loc22_39.1, element0
// CHECK:STDOUT: %.loc22_39.3: A* = address_of %.loc22_39.2
// CHECK:STDOUT: %.loc22_39.4: A* = converted %p.ref, %.loc22_39.3
// CHECK:STDOUT: return %.loc22_39.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertCToA(%p: C*) -> A* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: C* = name_ref p, %p
// CHECK:STDOUT: %.loc23_39.1: ref C = deref %p.ref
// CHECK:STDOUT: %.loc23_39.2: ref B = class_element_access %.loc23_39.1, element0
// CHECK:STDOUT: %.loc23_39.3: ref A = class_element_access %.loc23_39.2, element0
// CHECK:STDOUT: %.loc23_39.4: A* = address_of %.loc23_39.3
// CHECK:STDOUT: %.loc23_39.5: A* = converted %p.ref, %.loc23_39.4
// CHECK:STDOUT: return %.loc23_39.5
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertValue(%c: C) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %A.ref: type = name_ref A, file.%A
// CHECK:STDOUT: %c.ref: C = name_ref c, %c
// CHECK:STDOUT: %.loc26_15.1: ref B = class_element_access %c.ref, element0
// CHECK:STDOUT: %.loc26_15.2: ref A = class_element_access %.loc26_15.1, element0
// CHECK:STDOUT: %.loc26_15.3: ref A = converted %c.ref, %.loc26_15.2
// CHECK:STDOUT: %.loc26_15.4: A = bind_value %.loc26_15.3
// CHECK:STDOUT: %a: A = bind_name a, %.loc26_15.4
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertRef(%c: C*) -> A* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %c.ref: C* = name_ref c, %c
// CHECK:STDOUT: %.loc30_12: ref C = deref %c.ref
// CHECK:STDOUT: %A.ref: type = name_ref A, file.%A
// CHECK:STDOUT: %.loc30_15.1: ref B = class_element_access %.loc30_12, element0
// CHECK:STDOUT: %.loc30_15.2: ref A = class_element_access %.loc30_15.1, element0
// CHECK:STDOUT: %.loc30_15.3: ref A = converted %.loc30_12, %.loc30_15.2
// CHECK:STDOUT: %.loc30_10: A* = address_of %.loc30_15.3
// CHECK:STDOUT: return %.loc30_10
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertInit() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %A.ref: type = name_ref A, file.%A
// CHECK:STDOUT: %.loc34_38: i32 = int_literal 1
// CHECK:STDOUT: %.loc34_39.1: {.a: i32} = struct_literal (%.loc34_38)
// CHECK:STDOUT: %.loc34_47: i32 = int_literal 2
// CHECK:STDOUT: %.loc34_48.1: {.base: {.a: i32}, .b: i32} = struct_literal (%.loc34_39.1, %.loc34_47)
// CHECK:STDOUT: %.loc34_56: i32 = int_literal 3
// CHECK:STDOUT: %.loc34_57.1: {.base: {.base: {.a: i32}, .b: i32}, .c: i32} = struct_literal (%.loc34_48.1, %.loc34_56)
// CHECK:STDOUT: %C.ref: type = name_ref C, file.%C
// CHECK:STDOUT: %.loc34_57.2: ref C = temporary_storage
// CHECK:STDOUT: %.loc34_57.3: ref B = class_element_access %.loc34_57.2, element0
// CHECK:STDOUT: %.loc34_48.2: ref A = class_element_access %.loc34_57.3, element0
// CHECK:STDOUT: %.loc34_39.2: ref i32 = class_element_access %.loc34_48.2, element0
// CHECK:STDOUT: %.loc34_39.3: init i32 = initialize_from %.loc34_38 to %.loc34_39.2
// CHECK:STDOUT: %.loc34_39.4: init A = class_init (%.loc34_39.3), %.loc34_48.2
// CHECK:STDOUT: %.loc34_39.5: init A = converted %.loc34_39.1, %.loc34_39.4
// CHECK:STDOUT: %.loc34_48.3: ref i32 = class_element_access %.loc34_57.3, element1
// CHECK:STDOUT: %.loc34_48.4: init i32 = initialize_from %.loc34_47 to %.loc34_48.3
// CHECK:STDOUT: %.loc34_48.5: init B = class_init (%.loc34_39.5, %.loc34_48.4), %.loc34_57.3
// CHECK:STDOUT: %.loc34_48.6: init B = converted %.loc34_48.1, %.loc34_48.5
// CHECK:STDOUT: %.loc34_57.4: ref i32 = class_element_access %.loc34_57.2, element1
// CHECK:STDOUT: %.loc34_57.5: init i32 = initialize_from %.loc34_56 to %.loc34_57.4
// CHECK:STDOUT: %.loc34_57.6: init C = class_init (%.loc34_48.6, %.loc34_57.5), %.loc34_57.2
// CHECK:STDOUT: %.loc34_57.7: ref C = temporary %.loc34_57.2, %.loc34_57.6
// CHECK:STDOUT: %.loc34_57.8: ref C = converted %.loc34_57.1, %.loc34_57.7
// CHECK:STDOUT: %.loc34_63.1: ref B = class_element_access %.loc34_57.8, element0
// CHECK:STDOUT: %.loc34_63.2: ref A = class_element_access %.loc34_63.1, element0
// CHECK:STDOUT: %.loc34_63.3: ref A = converted %.loc34_57.8, %.loc34_63.2
// CHECK:STDOUT: %.loc34_63.4: A = bind_value %.loc34_63.3
// CHECK:STDOUT: %a: A = bind_name a, %.loc34_63.4
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
+97 -23
View File
@@ -21,6 +21,10 @@ class DeriveFromi32 {
extend base: i32;
}
// It's not really important whether this conversion produces an error or not,
// but it shouldn't crash.
fn ConvertToBadBasei32(p: DeriveFromi32*) -> i32* { return p; }
class DeriveFromTuple {
// CHECK:STDERR: fail_base_bad_type.carbon:[[@LINE+3]]:23: ERROR: Deriving from final type `(Base,)`. Base type must be an `abstract` or `base` class.
// CHECK:STDERR: extend base: (Base,);
@@ -28,6 +32,8 @@ class DeriveFromTuple {
extend base: (Base,);
}
fn ConvertToBadBaseTuple(p: DeriveFromTuple*) -> (Base,)* { return p; }
// TODO: Should we allow this?
// We do allow `{.base = {.a: i32, .b: i32}}`.
class DeriveFromStruct {
@@ -37,6 +43,8 @@ class DeriveFromStruct {
extend base: {.a: i32, .b: i32};
}
fn ConvertToBadBaseStruct(p: DeriveFromStruct*) -> {.a: i32, .b: i32}* { return p; }
base class Incomplete;
class DeriveFromIncomplete {
@@ -49,6 +57,11 @@ class DeriveFromIncomplete {
extend base: Incomplete;
}
// CHECK:STDERR: fail_base_bad_type.carbon:[[@LINE+3]]:82: ERROR: Cannot implicitly convert from `DeriveFromIncomplete*` to `Incomplete*`.
// CHECK:STDERR: fn ConvertToBadBaseIncomplete(p: DeriveFromIncomplete*) -> Incomplete* { return p; }
// CHECK:STDERR: ^
fn ConvertToBadBaseIncomplete(p: DeriveFromIncomplete*) -> Incomplete* { return p; }
class DeriveFromFinal {
// CHECK:STDERR: fail_base_bad_type.carbon:[[@LINE+3]]:21: ERROR: Deriving from final type `Final`. Base type must be an `abstract` or `base` class.
// CHECK:STDERR: extend base: Final;
@@ -56,25 +69,35 @@ class DeriveFromFinal {
extend base: Final;
}
fn ConvertToBadBaseFinal(p: DeriveFromFinal*) -> Final* { return p; }
// CHECK:STDOUT: --- fail_base_bad_type.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.loc7_18.1: type = struct_type {}
// CHECK:STDOUT: %.loc15: type = struct_type {.base: <error>}
// CHECK:STDOUT: %.loc22: type = struct_type {.base: i32}
// CHECK:STDOUT: %.loc28_22: type = tuple_type (type)
// CHECK:STDOUT: %.loc28_23.1: type = tuple_type (Base)
// CHECK:STDOUT: %.loc17: type = ptr_type {.base: i32}
// CHECK:STDOUT: %.loc32_22: type = tuple_type (type)
// CHECK:STDOUT: %.loc32_23.1: type = tuple_type (Base)
// CHECK:STDOUT: %.loc7_18.2: type = tuple_type ()
// CHECK:STDOUT: %.loc7_1: type = ptr_type {}
// CHECK:STDOUT: %.loc28_23.2: type = tuple_type ({}*)
// CHECK:STDOUT: %.loc29: type = struct_type {.base: (Base,)}
// CHECK:STDOUT: %.loc37: type = ptr_type {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc38: type = struct_type {.base: {.a: i32, .b: i32}}
// CHECK:STDOUT: %.loc57: type = struct_type {.base: Final}
// CHECK:STDOUT: %.loc32_23.2: type = tuple_type ({}*)
// CHECK:STDOUT: %.loc33_1.1: type = struct_type {.base: (Base,)}
// CHECK:STDOUT: %.loc33_1.2: type = struct_type {.base: ({}*,)}
// CHECK:STDOUT: %.loc28: type = ptr_type {.base: (Base,)}
// CHECK:STDOUT: %.loc43: type = ptr_type {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc44_1.1: type = struct_type {.base: {.a: i32, .b: i32}}
// CHECK:STDOUT: %.loc44_1.2: type = struct_type {.base: {.a: i32, .b: i32}*}
// CHECK:STDOUT: %.loc39: type = ptr_type {.base: {.a: i32, .b: i32}}
// CHECK:STDOUT: %.loc50: type = ptr_type {.base: <error>}
// CHECK:STDOUT: %.loc70_1.1: type = struct_type {.base: Final}
// CHECK:STDOUT: %.loc70_1.2: type = struct_type {.base: {}*}
// CHECK:STDOUT: %.loc65: type = ptr_type {.base: Final}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {.Base = %Base.decl, .Final = %Final.decl, .DeriveFromNonType = %DeriveFromNonType.decl, .DeriveFromi32 = %DeriveFromi32.decl, .DeriveFromTuple = %DeriveFromTuple.decl, .DeriveFromStruct = %DeriveFromStruct.decl, .Incomplete = %Incomplete.decl, .DeriveFromIncomplete = %DeriveFromIncomplete.decl, .DeriveFromFinal = %DeriveFromFinal.decl}
// CHECK:STDOUT: package: <namespace> = namespace {.Base = %Base.decl, .Final = %Final.decl, .DeriveFromNonType = %DeriveFromNonType.decl, .DeriveFromi32 = %DeriveFromi32.decl, .ConvertToBadBasei32 = %ConvertToBadBasei32, .DeriveFromTuple = %DeriveFromTuple.decl, .ConvertToBadBaseTuple = %ConvertToBadBaseTuple, .DeriveFromStruct = %DeriveFromStruct.decl, .ConvertToBadBaseStruct = %ConvertToBadBaseStruct, .Incomplete = %Incomplete.decl, .DeriveFromIncomplete = %DeriveFromIncomplete.decl, .ConvertToBadBaseIncomplete = %ConvertToBadBaseIncomplete, .DeriveFromFinal = %DeriveFromFinal.decl, .ConvertToBadBaseFinal = %ConvertToBadBaseFinal}
// CHECK:STDOUT: %Base.decl = class_decl @Base, ()
// CHECK:STDOUT: %Base: type = class_type @Base
// CHECK:STDOUT: %Final.decl = class_decl @Final, ()
@@ -83,16 +106,21 @@ class DeriveFromFinal {
// CHECK:STDOUT: %DeriveFromNonType: type = class_type @DeriveFromNonType
// CHECK:STDOUT: %DeriveFromi32.decl = class_decl @DeriveFromi32, ()
// CHECK:STDOUT: %DeriveFromi32: type = class_type @DeriveFromi32
// CHECK:STDOUT: %ConvertToBadBasei32: <function> = fn_decl @ConvertToBadBasei32
// CHECK:STDOUT: %DeriveFromTuple.decl = class_decl @DeriveFromTuple, ()
// CHECK:STDOUT: %DeriveFromTuple: type = class_type @DeriveFromTuple
// CHECK:STDOUT: %ConvertToBadBaseTuple: <function> = fn_decl @ConvertToBadBaseTuple
// CHECK:STDOUT: %DeriveFromStruct.decl = class_decl @DeriveFromStruct, ()
// CHECK:STDOUT: %DeriveFromStruct: type = class_type @DeriveFromStruct
// CHECK:STDOUT: %ConvertToBadBaseStruct: <function> = fn_decl @ConvertToBadBaseStruct
// CHECK:STDOUT: %Incomplete.decl = class_decl @Incomplete, ()
// CHECK:STDOUT: %Incomplete: type = class_type @Incomplete
// CHECK:STDOUT: %DeriveFromIncomplete.decl = class_decl @DeriveFromIncomplete, ()
// CHECK:STDOUT: %DeriveFromIncomplete: type = class_type @DeriveFromIncomplete
// CHECK:STDOUT: %ConvertToBadBaseIncomplete: <function> = fn_decl @ConvertToBadBaseIncomplete
// CHECK:STDOUT: %DeriveFromFinal.decl = class_decl @DeriveFromFinal, ()
// CHECK:STDOUT: %DeriveFromFinal: type = class_type @DeriveFromFinal
// CHECK:STDOUT: %ConvertToBadBaseFinal: <function> = fn_decl @ConvertToBadBaseFinal
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Base {
@@ -124,41 +152,87 @@ class DeriveFromFinal {
// CHECK:STDOUT:
// CHECK:STDOUT: class @DeriveFromTuple {
// CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base
// CHECK:STDOUT: %.loc28_22: (type,) = tuple_literal (%Base.ref)
// CHECK:STDOUT: %.loc28_23.1: type = converted %.loc28_22, constants.%.loc28_23.1
// CHECK:STDOUT: %.loc28_23.2: type = unbound_element_type DeriveFromTuple, (Base,)
// CHECK:STDOUT: %.loc28_23.3: <unbound element of class DeriveFromTuple> = base_decl (Base,), element0
// CHECK:STDOUT: %.loc32_22: (type,) = tuple_literal (%Base.ref)
// CHECK:STDOUT: %.loc32_23.1: type = converted %.loc32_22, constants.%.loc32_23.1
// CHECK:STDOUT: %.loc32_23.2: type = unbound_element_type DeriveFromTuple, (Base,)
// CHECK:STDOUT: %.loc32_23.3: <unbound element of class DeriveFromTuple> = base_decl (Base,), element0
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc28_23.3
// CHECK:STDOUT: .base = %.loc32_23.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @DeriveFromStruct {
// CHECK:STDOUT: %.loc37_33: type = struct_type {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc37_34.1: type = unbound_element_type DeriveFromStruct, {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc37_34.2: <unbound element of class DeriveFromStruct> = base_decl {.a: i32, .b: i32}, element0
// CHECK:STDOUT: %.loc43_33: type = struct_type {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc43_34.1: type = unbound_element_type DeriveFromStruct, {.a: i32, .b: i32}
// CHECK:STDOUT: %.loc43_34.2: <unbound element of class DeriveFromStruct> = base_decl {.a: i32, .b: i32}, element0
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc37_34.2
// CHECK:STDOUT: .base = %.loc43_34.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Incomplete;
// CHECK:STDOUT:
// CHECK:STDOUT: class @DeriveFromIncomplete {
// CHECK:STDOUT: %Incomplete.ref: type = name_ref Incomplete, file.%Incomplete
// CHECK:STDOUT: %.loc49_26.1: type = unbound_element_type DeriveFromIncomplete, <error>
// CHECK:STDOUT: %.loc49_26.2: <unbound element of class DeriveFromIncomplete> = base_decl <error>, element0
// CHECK:STDOUT: %.loc57_26.1: type = unbound_element_type DeriveFromIncomplete, <error>
// CHECK:STDOUT: %.loc57_26.2: <unbound element of class DeriveFromIncomplete> = base_decl <error>, element0
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc49_26.2
// CHECK:STDOUT: .base = %.loc57_26.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @DeriveFromFinal {
// CHECK:STDOUT: %Final.ref: type = name_ref Final, file.%Final
// CHECK:STDOUT: %.loc56_21.1: type = unbound_element_type DeriveFromFinal, Final
// CHECK:STDOUT: %.loc56_21.2: <unbound element of class DeriveFromFinal> = base_decl Final, element0
// CHECK:STDOUT: %.loc69_21.1: type = unbound_element_type DeriveFromFinal, Final
// CHECK:STDOUT: %.loc69_21.2: <unbound element of class DeriveFromFinal> = base_decl Final, element0
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc56_21.2
// CHECK:STDOUT: .base = %.loc69_21.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertToBadBasei32(%p: DeriveFromi32*) -> i32* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: DeriveFromi32* = name_ref p, %p
// CHECK:STDOUT: %.loc26_61.1: ref DeriveFromi32 = deref %p.ref
// CHECK:STDOUT: %.loc26_61.2: ref i32 = class_element_access %.loc26_61.1, element0
// CHECK:STDOUT: %.loc26_61.3: i32* = address_of %.loc26_61.2
// CHECK:STDOUT: %.loc26_61.4: i32* = converted %p.ref, %.loc26_61.3
// CHECK:STDOUT: return %.loc26_61.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertToBadBaseTuple(%p: DeriveFromTuple*) -> (Base,)* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: DeriveFromTuple* = name_ref p, %p
// CHECK:STDOUT: %.loc35_69.1: ref DeriveFromTuple = deref %p.ref
// CHECK:STDOUT: %.loc35_69.2: ref (Base,) = class_element_access %.loc35_69.1, element0
// CHECK:STDOUT: %.loc35_69.3: (Base,)* = address_of %.loc35_69.2
// CHECK:STDOUT: %.loc35_69.4: (Base,)* = converted %p.ref, %.loc35_69.3
// CHECK:STDOUT: return %.loc35_69.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertToBadBaseStruct(%p: DeriveFromStruct*) -> {.a: i32, .b: i32}* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: DeriveFromStruct* = name_ref p, %p
// CHECK:STDOUT: %.loc46_82.1: ref DeriveFromStruct = deref %p.ref
// CHECK:STDOUT: %.loc46_82.2: ref {.a: i32, .b: i32} = class_element_access %.loc46_82.1, element0
// CHECK:STDOUT: %.loc46_82.3: {.a: i32, .b: i32}* = address_of %.loc46_82.2
// CHECK:STDOUT: %.loc46_82.4: {.a: i32, .b: i32}* = converted %p.ref, %.loc46_82.3
// CHECK:STDOUT: return %.loc46_82.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertToBadBaseIncomplete(%p: DeriveFromIncomplete*) -> Incomplete* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: DeriveFromIncomplete* = name_ref p, %p
// CHECK:STDOUT: return <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertToBadBaseFinal(%p: DeriveFromFinal*) -> Final* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: DeriveFromFinal* = name_ref p, %p
// CHECK:STDOUT: %.loc72_67.1: ref DeriveFromFinal = deref %p.ref
// CHECK:STDOUT: %.loc72_67.2: ref Final = class_element_access %.loc72_67.1, element0
// CHECK:STDOUT: %.loc72_67.3: Final* = address_of %.loc72_67.2
// CHECK:STDOUT: %.loc72_67.4: Final* = converted %p.ref, %.loc72_67.3
// CHECK:STDOUT: return %.loc72_67.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -0,0 +1,101 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
base class A1 {
var a: i32;
}
base class A2 {
var a: i32;
}
class B2 {
extend base: A2;
var b: i32;
}
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+3]]:46: ERROR: Cannot implicitly convert from `B2*` to `A1*`.
// CHECK:STDERR: fn ConvertUnrelated(p: B2*) -> A1* { return p; }
// CHECK:STDERR: ^
fn ConvertUnrelated(p: B2*) -> A1* { return p; }
class Incomplete;
// CHECK:STDERR: fail_derived_to_base.carbon:[[@LINE+3]]:55: ERROR: Cannot implicitly convert from `Incomplete*` to `A2*`.
// CHECK:STDERR: fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
// CHECK:STDERR: ^
fn ConvertIncomplete(p: Incomplete*) -> A2* { return p; }
// CHECK:STDOUT: --- fail_derived_to_base.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.loc9: type = struct_type {.a: i32}
// CHECK:STDOUT: %.loc11: type = ptr_type {.a: i32}
// CHECK:STDOUT: %.loc18_1.1: type = struct_type {.base: A2, .b: i32}
// CHECK:STDOUT: %.loc18_1.2: type = struct_type {.base: {.a: i32}*, .b: i32}
// CHECK:STDOUT: %.loc18_1.3: type = ptr_type {.base: {.a: i32}*, .b: i32}
// CHECK:STDOUT: %.loc15: type = ptr_type {.base: A2, .b: i32}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {.A1 = %A1.decl, .A2 = %A2.decl, .B2 = %B2.decl, .ConvertUnrelated = %ConvertUnrelated, .Incomplete = %Incomplete.decl, .ConvertIncomplete = %ConvertIncomplete}
// CHECK:STDOUT: %A1.decl = class_decl @A1, ()
// CHECK:STDOUT: %A1: type = class_type @A1
// CHECK:STDOUT: %A2.decl = class_decl @A2, ()
// CHECK:STDOUT: %A2: type = class_type @A2
// CHECK:STDOUT: %B2.decl = class_decl @B2, ()
// CHECK:STDOUT: %B2: type = class_type @B2
// CHECK:STDOUT: %ConvertUnrelated: <function> = fn_decl @ConvertUnrelated
// CHECK:STDOUT: %Incomplete.decl = class_decl @Incomplete, ()
// CHECK:STDOUT: %Incomplete: type = class_type @Incomplete
// CHECK:STDOUT: %ConvertIncomplete: <function> = fn_decl @ConvertIncomplete
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A1 {
// CHECK:STDOUT: %.loc8_8.1: type = unbound_element_type A1, i32
// CHECK:STDOUT: %.loc8_8.2: <unbound element of class A1> = field_decl a, element0
// CHECK:STDOUT: %a: <unbound element of class A1> = bind_name a, %.loc8_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .a = %a
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @A2 {
// CHECK:STDOUT: %.loc12_8.1: type = unbound_element_type A2, i32
// CHECK:STDOUT: %.loc12_8.2: <unbound element of class A2> = field_decl a, element0
// CHECK:STDOUT: %a: <unbound element of class A2> = bind_name a, %.loc12_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .a = %a
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @B2 {
// CHECK:STDOUT: %A2.ref: type = name_ref A2, file.%A2
// CHECK:STDOUT: %.loc16_18.1: type = unbound_element_type B2, A2
// CHECK:STDOUT: %.loc16_18.2: <unbound element of class B2> = base_decl A2, element0
// CHECK:STDOUT: %.loc17_8.1: type = unbound_element_type B2, i32
// CHECK:STDOUT: %.loc17_8.2: <unbound element of class B2> = field_decl b, element1
// CHECK:STDOUT: %b: <unbound element of class B2> = bind_name b, %.loc17_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc16_18.2
// CHECK:STDOUT: .b = %b
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Incomplete;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertUnrelated(%p: B2*) -> A1* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: B2* = name_ref p, %p
// CHECK:STDOUT: return <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConvertIncomplete(%p: Incomplete*) -> A2* {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref: Incomplete* = name_ref p, %p
// CHECK:STDOUT: return <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
+115
View File
@@ -0,0 +1,115 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
base class Base {
var a: i32;
}
class Derived {
extend base: Base;
fn SelfBase[self: Base]() -> i32;
fn AddrSelfBase[addr self: Base*]();
}
fn Derived.SelfBase[self: Base]() -> i32 {
return self.a;
}
fn Derived.AddrSelfBase[addr self: Base*]() {
(*self).a = 1;
}
fn Call(p: Derived*) -> i32 {
(*p).AddrSelfBase();
return (*p).SelfBase();
}
// CHECK:STDOUT: --- self_conversion.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.loc9: type = struct_type {.a: i32}
// CHECK:STDOUT: %.loc7: type = ptr_type {.a: i32}
// CHECK:STDOUT: %.loc16_1.1: type = struct_type {.base: Base}
// CHECK:STDOUT: %.loc16_1.2: type = struct_type {.base: {.a: i32}*}
// CHECK:STDOUT: %.loc11: type = ptr_type {.base: Base}
// CHECK:STDOUT: %.loc27: type = tuple_type ()
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {.Base = %Base.decl, .Derived = %Derived.decl, .Call = %Call}
// CHECK:STDOUT: %Base.decl = class_decl @Base, ()
// CHECK:STDOUT: %Base: type = class_type @Base
// CHECK:STDOUT: %Derived.decl = class_decl @Derived, ()
// CHECK:STDOUT: %Derived: type = class_type @Derived
// CHECK:STDOUT: %SelfBase: <function> = fn_decl @SelfBase
// CHECK:STDOUT: %AddrSelfBase: <function> = fn_decl @AddrSelfBase
// CHECK:STDOUT: %Call: <function> = fn_decl @Call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Base {
// CHECK:STDOUT: %.loc8_8.1: type = unbound_element_type Base, i32
// CHECK:STDOUT: %.loc8_8.2: <unbound element of class Base> = field_decl a, element0
// CHECK:STDOUT: %a: <unbound element of class Base> = bind_name a, %.loc8_8.2
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .a = %a
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @Derived {
// CHECK:STDOUT: %Base.ref: type = name_ref Base, file.%Base
// CHECK:STDOUT: %.loc12_20.1: type = unbound_element_type Derived, Base
// CHECK:STDOUT: %.loc12_20.2: <unbound element of class Derived> = base_decl Base, element0
// CHECK:STDOUT: %SelfBase: <function> = fn_decl @SelfBase
// CHECK:STDOUT: %AddrSelfBase: <function> = fn_decl @AddrSelfBase
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .base = %.loc12_20.2
// CHECK:STDOUT: .SelfBase = %SelfBase
// CHECK:STDOUT: .AddrSelfBase = %AddrSelfBase
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @SelfBase[%self: Base]() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %self.ref: Base = name_ref self, %self
// CHECK:STDOUT: %.loc19_14.1: ref i32 = class_element_access %self.ref, element0
// CHECK:STDOUT: %.loc19_14.2: i32 = bind_value %.loc19_14.1
// CHECK:STDOUT: return %.loc19_14.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddrSelfBase[%self.addr: Base*]() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %self.ref: Base* = name_ref self, %self.addr
// CHECK:STDOUT: %.loc23_4: ref Base = deref %self.ref
// CHECK:STDOUT: %.loc23_10: ref i32 = class_element_access %.loc23_4, element0
// CHECK:STDOUT: %.loc23_15: i32 = int_literal 1
// CHECK:STDOUT: assign %.loc23_10, %.loc23_15
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Call(%p: Derived*) -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %p.ref.loc27: Derived* = name_ref p, %p
// CHECK:STDOUT: %.loc27_4.1: ref Derived = deref %p.ref.loc27
// CHECK:STDOUT: %.loc27_7: <bound method> = bound_method %.loc27_4.1, @Derived.%AddrSelfBase
// CHECK:STDOUT: %.loc27_4.2: Derived* = address_of %.loc27_4.1
// CHECK:STDOUT: %.loc27_20.1: ref Derived = deref %.loc27_4.2
// CHECK:STDOUT: %.loc27_20.2: ref Base = class_element_access %.loc27_20.1, element0
// CHECK:STDOUT: %.loc27_20.3: Base* = address_of %.loc27_20.2
// CHECK:STDOUT: %.loc27_20.4: Base* = converted %.loc27_4.2, %.loc27_20.3
// CHECK:STDOUT: %.loc27_20.5: init () = call %.loc27_7(%.loc27_20.4)
// CHECK:STDOUT: %p.ref.loc28: Derived* = name_ref p, %p
// CHECK:STDOUT: %.loc28_11: ref Derived = deref %p.ref.loc28
// CHECK:STDOUT: %.loc28_14: <bound method> = bound_method %.loc28_11, @Derived.%SelfBase
// CHECK:STDOUT: %.loc28_23.1: ref Base = class_element_access %.loc28_11, element0
// CHECK:STDOUT: %.loc28_23.2: ref Base = converted %.loc28_11, %.loc28_23.1
// CHECK:STDOUT: %.loc28_23.3: Base = bind_value %.loc28_23.2
// CHECK:STDOUT: %.loc28_23.4: init i32 = call %.loc28_14(%.loc28_23.3)
// CHECK:STDOUT: %.loc28_25.1: i32 = value_of_initializer %.loc28_23.4
// CHECK:STDOUT: %.loc28_25.2: i32 = converted %.loc28_23.4, %.loc28_25.1
// CHECK:STDOUT: return %.loc28_25.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
+1 -3
View File
@@ -23,9 +23,7 @@ fn Access(d: Derived) -> (i32, i32) {
}
fn Convert(p: Derived*) -> Base* {
// TODO: This should be allowed as an implicit conversion.
// TODO: We should support `p->base`.
return &(*p).base;
return p;
}
// CHECK:STDOUT: ; ModuleID = 'base.carbon'