1 //===- ConstructionContext.cpp - CFG constructor information --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ConstructionContext class and its sub-classes,
11 // which represent various different ways of constructing C++ objects
12 // with the additional information the users may want to know about
13 // the constructor.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "clang/Analysis/ConstructionContext.h"
18 
19 using namespace clang;
20 
21 const ConstructionContextLayer *
22 ConstructionContextLayer::create(BumpVectorContext &C, TriggerTy Trigger,
23                                  const ConstructionContextLayer *Parent) {
24   ConstructionContextLayer *CC =
25       C.getAllocator().Allocate<ConstructionContextLayer>();
26   return new (CC) ConstructionContextLayer(Trigger, Parent);
27 }
28 
29 bool ConstructionContextLayer::isStrictlyMoreSpecificThan(
30     const ConstructionContextLayer *Other) const {
31   const ConstructionContextLayer *Self = this;
32   while (true) {
33     if (!Other)
34       return Self;
35     if (!Self || !Self->isSameLayer(Other))
36       return false;
37     Self = Self->getParent();
38     Other = Other->getParent();
39   }
40   llvm_unreachable("The above loop can only be terminated via return!");
41 }
42 
43 const ConstructionContext *ConstructionContext::createFromLayers(
44     BumpVectorContext &C, const ConstructionContextLayer *TopLayer) {
45   // Before this point all we've had was a stockpile of arbitrary layers.
46   // Now validate that it is shaped as one of the finite amount of expected
47   // patterns.
48   if (const Stmt *S = TopLayer->getTriggerStmt()) {
49     if (const auto *DS = dyn_cast<DeclStmt>(S)) {
50       assert(TopLayer->isLast());
51       return create<SimpleVariableConstructionContext>(C, DS);
52     }
53     if (const auto *NE = dyn_cast<CXXNewExpr>(S)) {
54       assert(TopLayer->isLast());
55       return create<NewAllocatedObjectConstructionContext>(C, NE);
56     }
57     if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(S)) {
58       const MaterializeTemporaryExpr *MTE = nullptr;
59       assert(BTE->getType().getCanonicalType()
60                 ->getAsCXXRecordDecl()->hasNonTrivialDestructor());
61       // For temporaries with destructors, there may or may not be
62       // lifetime extension on the parent layer.
63       if (const ConstructionContextLayer *ParentLayer = TopLayer->getParent()) {
64         // C++17 *requires* elision of the constructor at the return site
65         // and at variable/member initialization site, while previous standards
66         // were allowing an optional elidable constructor.
67         // This is the C++17 copy-elided construction into a ctor initializer.
68         if (const CXXCtorInitializer *I = ParentLayer->getTriggerInit()) {
69           return create<
70               CXX17ElidedCopyConstructorInitializerConstructionContext>(C,
71                                                                         I, BTE);
72         }
73         assert(ParentLayer->getTriggerStmt() &&
74                "Non-statement-based layers have been handled above!");
75         // This is the normal, non-C++17 case: a temporary object which has
76         // both destruction and materialization info attached to it in the AST.
77         if ((MTE = dyn_cast<MaterializeTemporaryExpr>(
78                  ParentLayer->getTriggerStmt()))) {
79           // Handle pre-C++17 copy and move elision.
80           const CXXConstructExpr *ElidedCE = nullptr;
81           const ConstructionContext *ElidedCC = nullptr;
82           if (const ConstructionContextLayer *ElidedLayer =
83                   ParentLayer->getParent()) {
84             ElidedCE = cast<CXXConstructExpr>(ElidedLayer->getTriggerStmt());
85             assert(ElidedCE->isElidable());
86             // We're creating a construction context that might have already
87             // been created elsewhere. Maybe we should unique our construction
88             // contexts. That's what we often do, but in this case it's unlikely
89             // to bring any benefits.
90             ElidedCC = createFromLayers(C, ElidedLayer->getParent());
91             if (!ElidedCC) {
92               // We may fail to create the elided construction context.
93               // In this case, skip copy elision entirely.
94               return create<SimpleTemporaryObjectConstructionContext>(C, BTE,
95                                                                       MTE);
96             } else {
97               return create<ElidedTemporaryObjectConstructionContext>(
98                   C, BTE, MTE, ElidedCE, ElidedCC);
99             }
100           }
101           assert(ParentLayer->isLast());
102           return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE);
103         }
104         assert(ParentLayer->isLast());
105 
106         // This is a constructor into a function argument. Not implemented yet.
107         if (isa<CallExpr>(ParentLayer->getTriggerStmt()))
108           return nullptr;
109         // This is C++17 copy-elided construction into return statement.
110         if (auto *RS = dyn_cast<ReturnStmt>(ParentLayer->getTriggerStmt())) {
111           assert(!RS->getRetValue()->getType().getCanonicalType()
112                     ->getAsCXXRecordDecl()->hasTrivialDestructor());
113           return create<CXX17ElidedCopyReturnedValueConstructionContext>(C,
114                                                                        RS, BTE);
115         }
116         // This is C++17 copy-elided construction into a simple variable.
117         if (auto *DS = dyn_cast<DeclStmt>(ParentLayer->getTriggerStmt())) {
118           assert(!cast<VarDecl>(DS->getSingleDecl())->getType()
119                       .getCanonicalType()->getAsCXXRecordDecl()
120                       ->hasTrivialDestructor());
121           return create<CXX17ElidedCopyVariableConstructionContext>(C, DS, BTE);
122         }
123         llvm_unreachable("Unexpected construction context with destructor!");
124       }
125       // A temporary object that doesn't require materialization.
126       // In particular, it shouldn't require copy elision, because
127       // copy/move constructors take a reference, which requires
128       // materialization to obtain the glvalue.
129       return create<SimpleTemporaryObjectConstructionContext>(C, BTE,
130                                                               /*MTE=*/nullptr);
131     }
132     if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(S)) {
133       // If the object requires destruction and is not lifetime-extended,
134       // then it must have a BTE within its MTE.
135       // FIXME: This should be an assertion.
136       if (!(MTE->getType().getCanonicalType()
137                 ->getAsCXXRecordDecl()->hasTrivialDestructor() ||
138              MTE->getStorageDuration() != SD_FullExpression))
139         return nullptr;
140 
141       // Handle pre-C++17 copy and move elision.
142       const CXXConstructExpr *ElidedCE = nullptr;
143       const ConstructionContext *ElidedCC = nullptr;
144       if (const ConstructionContextLayer *ElidedLayer = TopLayer->getParent()) {
145         ElidedCE = cast<CXXConstructExpr>(ElidedLayer->getTriggerStmt());
146         assert(ElidedCE->isElidable());
147         // We're creating a construction context that might have already
148         // been created elsewhere. Maybe we should unique our construction
149         // contexts. That's what we often do, but in this case it's unlikely
150         // to bring any benefits.
151         ElidedCC = createFromLayers(C, ElidedLayer->getParent());
152         if (!ElidedCC) {
153           // We may fail to create the elided construction context.
154           // In this case, skip copy elision entirely.
155           return create<SimpleTemporaryObjectConstructionContext>(C, nullptr,
156                                                                   MTE);
157         }
158         return create<ElidedTemporaryObjectConstructionContext>(
159             C, nullptr, MTE, ElidedCE, ElidedCC);
160       }
161       assert(TopLayer->isLast());
162       return create<SimpleTemporaryObjectConstructionContext>(C, nullptr, MTE);
163     }
164     if (const auto *RS = dyn_cast<ReturnStmt>(S)) {
165       assert(TopLayer->isLast());
166       return create<SimpleReturnedValueConstructionContext>(C, RS);
167     }
168     // This is a constructor into a function argument. Not implemented yet.
169     if (isa<CallExpr>(TopLayer->getTriggerStmt()))
170       return nullptr;
171     llvm_unreachable("Unexpected construction context with statement!");
172   } else if (const CXXCtorInitializer *I = TopLayer->getTriggerInit()) {
173     assert(TopLayer->isLast());
174     return create<SimpleConstructorInitializerConstructionContext>(C, I);
175   }
176   llvm_unreachable("Unexpected construction context!");
177 }
178