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 if ((MTE = dyn_cast<MaterializeTemporaryExpr>( 66 ParentLayer->getTriggerStmt()))) { 67 // A temporary object which has both destruction and 68 // materialization info. 69 return create<TemporaryObjectConstructionContext>(C, BTE, MTE); 70 } 71 // C++17 *requires* elision of the constructor at the return site 72 // and at variable initialization site, while previous standards 73 // were allowing an optional elidable constructor. 74 if (auto *RS = dyn_cast<ReturnStmt>(ParentLayer->getTriggerStmt())) { 75 assert(!RS->getRetValue()->getType().getCanonicalType() 76 ->getAsCXXRecordDecl()->hasTrivialDestructor()); 77 return create<CXX17ElidedCopyReturnedValueConstructionContext>(C, 78 RS, BTE); 79 } 80 if (auto *DS = dyn_cast<DeclStmt>(ParentLayer->getTriggerStmt())) { 81 assert(!cast<VarDecl>(DS->getSingleDecl())->getType() 82 .getCanonicalType()->getAsCXXRecordDecl() 83 ->hasTrivialDestructor()); 84 return create<CXX17ElidedCopyVariableConstructionContext>(C, DS, BTE); 85 } 86 llvm_unreachable("Unexpected construction context with destructor!"); 87 } 88 // A temporary object that doesn't require materialization. 89 return create<TemporaryObjectConstructionContext>(C, BTE, /*MTE=*/nullptr); 90 } 91 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(S)) { 92 // If the object requires destruction and is not lifetime-extended, 93 // then it must have a BTE within its MTE. 94 assert(MTE->getType().getCanonicalType() 95 ->getAsCXXRecordDecl()->hasTrivialDestructor() || 96 MTE->getStorageDuration() != SD_FullExpression); 97 assert(TopLayer->isLast()); 98 return create<TemporaryObjectConstructionContext>(C, nullptr, MTE); 99 } 100 if (const auto *RS = dyn_cast<ReturnStmt>(S)) { 101 assert(TopLayer->isLast()); 102 return create<SimpleReturnedValueConstructionContext>(C, RS); 103 } 104 llvm_unreachable("Unexpected construction context with statement!"); 105 } else if (const CXXCtorInitializer *I = TopLayer->getTriggerInit()) { 106 assert(TopLayer->isLast()); 107 return create<ConstructorInitializerConstructionContext>(C, I); 108 } 109 llvm_unreachable("Unexpected construction context!"); 110 } 111