• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // dynamic_cast
2 // Ensure that dynamic casting works normally
3 
4 // RUN: %clang_cc1 %s -triple=aarch64-unknown-fuchsia -O3 -S -o - -emit-llvm -fexperimental-relative-c++-abi-vtables | FileCheck %s
5 
6 // CHECK:      define %class.A* @_Z6upcastP1B(%class.B* readnone %b) local_unnamed_addr
7 // CHECK-NEXT: entry:
8 // CHECK-NEXT:   [[a:%[0-9]+]] = getelementptr %class.B, %class.B* %b, i64 0, i32 0
9 // CHECK-NEXT:   ret %class.A* [[a]]
10 // CHECK-NEXT: }
11 
12 // CHECK:      define %class.B* @_Z8downcastP1A(%class.A* readonly %a) local_unnamed_addr
13 // CHECK-NEXT: entry:
14 // CHECK-NEXT:   [[isnull:%[0-9]+]] = icmp eq %class.A* %a, null
15 // CHECK-NEXT:   br i1 [[isnull]], label %[[dynamic_cast_end:[a-z0-9._]+]], label %[[dynamic_cast_notnull:[a-z0-9._]+]]
16 // CHECK:      [[dynamic_cast_notnull]]:
17 // CHECK-NEXT:   [[a:%[0-9]+]] = bitcast %class.A* %a to i8*
18 // CHECK-NEXT:   [[as_b:%[0-9]+]] = tail call i8* @__dynamic_cast(i8* nonnull [[a]], i8* bitcast ({ i8*, i8* }* @_ZTI1A to i8*), i8* bitcast ({ i8*, i8*, i8* }* @_ZTI1B to i8*), i64 0)
19 // CHECK-NEXT:   [[b:%[0-9]+]] = bitcast i8* [[as_b]] to %class.B*
20 // CHECK-NEXT:   br label %[[dynamic_cast_end]]
21 // CHECK:      [[dynamic_cast_end]]:
22 // CHECK-NEXT:   [[res:%[0-9]+]] = phi %class.B* [ [[b]], %[[dynamic_cast_notnull]] ], [ null, %entry ]
23 // CHECK-NEXT:   ret %class.B* [[res]]
24 // CHECK-NEXT: }
25 
26 // CHECK: declare i8* @__dynamic_cast(i8*, i8*, i8*, i64) local_unnamed_addr
27 
28 // CHECK:      define %class.B* @_Z8selfcastP1B(%class.B* readnone returned %b) local_unnamed_addr
29 // CHECK-NEXT: entry
30 // CHECK-NEXT:   ret %class.B* %b
31 // CHECK-NEXT: }
32 
33 // CHECK: define i8* @_Z9void_castP1B(%class.B* readonly %b) local_unnamed_addr
34 // CHECK-NEXT: entry:
35 // CHECK-NEXT:   [[isnull:%[0-9]+]] = icmp eq %class.B* %b, null
36 // CHECK-NEXT:   br i1 [[isnull]], label %[[dynamic_cast_end:[a-z0-9._]+]], label %[[dynamic_cast_notnull:[a-z0-9._]+]]
37 // CHECK:      [[dynamic_cast_notnull]]:
38 // CHECK-DAG:    [[b2:%[0-9]+]] = bitcast %class.B* %b to i32**
39 // CHECK-DAG:    [[vtable:%[a-z0-9]+]] = load i32*, i32** [[b2]], align 8
40 // CHECK-DAG:    [[offset_ptr:%.+]] = getelementptr inbounds i32, i32* [[vtable]], i64 -2
41 // CHECK-DAG:    [[offset_to_top:%.+]] = load i32, i32* [[offset_ptr]], align 4
42 // CHECK-DAG:    [[b:%[0-9]+]] = bitcast %class.B* %b to i8*
43 // CHECK-DAG:    [[offset_to_top2:%.+]] = sext i32 [[offset_to_top]] to i64
44 // CHECK-DAG:    [[casted:%.+]] = getelementptr inbounds i8, i8* [[b]], i64 [[offset_to_top2]]
45 // CHECK-NEXT:   br label %[[dynamic_cast_end]]
46 // CHECK:      [[dynamic_cast_end]]:
47 // CHECK-NEXT:   [[res:%[0-9]+]] = phi i8* [ [[casted]], %[[dynamic_cast_notnull]] ], [ null, %entry ]
48 // CHECK-NEXT:   ret i8* [[res]]
49 // CHECK-NEXT: }
50 
51 class A {
52 public:
53   virtual void foo();
54 };
55 
56 class B : public A {
57 public:
58   void foo() override;
59 };
60 
foo()61 void A::foo() {}
foo()62 void B::foo() {}
63 
upcast(B * b)64 A *upcast(B *b) {
65   return dynamic_cast<A *>(b);
66 }
67 
downcast(A * a)68 B *downcast(A *a) {
69   return dynamic_cast<B *>(a);
70 }
71 
selfcast(B * b)72 B *selfcast(B *b) {
73   return dynamic_cast<B *>(b);
74 }
75 
void_cast(B * b)76 void *void_cast(B *b) {
77   return dynamic_cast<void *>(b);
78 }
79