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         assert(ParentLayer->isLast());
65         // C++17 *requires* elision of the constructor at the return site
66         // and at variable/member initialization site, while previous standards
67         // were allowing an optional elidable constructor.
68         // This is the C++17 copy-elided construction into a ctor initializer.
69         if (const CXXCtorInitializer *I = ParentLayer->getTriggerInit()) {
70           return create<
71               CXX17ElidedCopyConstructorInitializerConstructionContext>(C,
72                                                                         I, BTE);
73         }
74         assert(ParentLayer->getTriggerStmt() &&
75                "Non-statement-based layers have been handled above!");
76         // This is the normal, non-C++17 case: a temporary object which has
77         // both destruction and materialization info attached to it in the AST.
78         if ((MTE = dyn_cast<MaterializeTemporaryExpr>(
79                  ParentLayer->getTriggerStmt()))) {
80           return create<TemporaryObjectConstructionContext>(C, BTE, MTE);
81         }
82         // This is C++17 copy-elided construction into return statement.
83         if (auto *RS = dyn_cast<ReturnStmt>(ParentLayer->getTriggerStmt())) {
84           assert(!RS->getRetValue()->getType().getCanonicalType()
85                     ->getAsCXXRecordDecl()->hasTrivialDestructor());
86           return create<CXX17ElidedCopyReturnedValueConstructionContext>(C,
87                                                                        RS, BTE);
88         }
89         // This is C++17 copy-elided construction into a simple variable.
90         if (auto *DS = dyn_cast<DeclStmt>(ParentLayer->getTriggerStmt())) {
91           assert(!cast<VarDecl>(DS->getSingleDecl())->getType()
92                       .getCanonicalType()->getAsCXXRecordDecl()
93                       ->hasTrivialDestructor());
94           return create<CXX17ElidedCopyVariableConstructionContext>(C, DS, BTE);
95         }
96         llvm_unreachable("Unexpected construction context with destructor!");
97       }
98       // A temporary object that doesn't require materialization.
99       return create<TemporaryObjectConstructionContext>(C, BTE, /*MTE=*/nullptr);
100     }
101     if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(S)) {
102       // If the object requires destruction and is not lifetime-extended,
103       // then it must have a BTE within its MTE.
104       // FIXME: This should be an assertion.
105       if (!(MTE->getType().getCanonicalType()
106                 ->getAsCXXRecordDecl()->hasTrivialDestructor() ||
107              MTE->getStorageDuration() != SD_FullExpression))
108         return nullptr;
109 
110       assert(TopLayer->isLast());
111       return create<TemporaryObjectConstructionContext>(C, nullptr, MTE);
112     }
113     if (const auto *RS = dyn_cast<ReturnStmt>(S)) {
114       assert(TopLayer->isLast());
115       return create<SimpleReturnedValueConstructionContext>(C, RS);
116     }
117     llvm_unreachable("Unexpected construction context with statement!");
118   } else if (const CXXCtorInitializer *I = TopLayer->getTriggerInit()) {
119     assert(TopLayer->isLast());
120     return create<SimpleConstructorInitializerConstructionContext>(C, I);
121   }
122   llvm_unreachable("Unexpected construction context!");
123 }
124