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 auto *CC = 52 C.getAllocator().Allocate<SimpleVariableConstructionContext>(); 53 return new (CC) SimpleVariableConstructionContext(DS); 54 } else if (const auto *NE = dyn_cast<CXXNewExpr>(S)) { 55 assert(TopLayer->isLast()); 56 auto *CC = 57 C.getAllocator().Allocate<NewAllocatedObjectConstructionContext>(); 58 return new (CC) NewAllocatedObjectConstructionContext(NE); 59 } else if (const auto *BTE = dyn_cast<CXXBindTemporaryExpr>(S)) { 60 const MaterializeTemporaryExpr *MTE = nullptr; 61 assert(BTE->getType().getCanonicalType() 62 ->getAsCXXRecordDecl()->hasNonTrivialDestructor()); 63 // For temporaries with destructors, there may or may not be 64 // lifetime extension on the parent layer. 65 if (const ConstructionContextLayer *ParentLayer = TopLayer->getParent()) { 66 assert(ParentLayer->isLast()); 67 if ((MTE = dyn_cast<MaterializeTemporaryExpr>( 68 ParentLayer->getTriggerStmt()))) { 69 // A temporary object which has both destruction and 70 // materialization info. 71 auto *CC = 72 C.getAllocator().Allocate<TemporaryObjectConstructionContext>(); 73 return new (CC) TemporaryObjectConstructionContext(BTE, MTE); 74 } 75 // C++17 *requires* elision of the constructor at the return site 76 // and at variable initialization site, while previous standards 77 // were allowing an optional elidable constructor. 78 if (auto *RS = dyn_cast<ReturnStmt>(ParentLayer->getTriggerStmt())) { 79 assert(!RS->getRetValue()->getType().getCanonicalType() 80 ->getAsCXXRecordDecl()->hasTrivialDestructor()); 81 auto *CC = 82 C.getAllocator() 83 .Allocate< 84 CXX17ElidedCopyReturnedValueConstructionContext>(); 85 return new (CC) 86 CXX17ElidedCopyReturnedValueConstructionContext(RS, BTE); 87 } 88 if (auto *DS = dyn_cast<DeclStmt>(ParentLayer->getTriggerStmt())) { 89 assert(!cast<VarDecl>(DS->getSingleDecl())->getType() 90 .getCanonicalType()->getAsCXXRecordDecl() 91 ->hasTrivialDestructor()); 92 auto *CC = 93 C.getAllocator() 94 .Allocate<CXX17ElidedCopyVariableConstructionContext>(); 95 return new (CC) CXX17ElidedCopyVariableConstructionContext(DS, BTE); 96 } 97 llvm_unreachable("Unexpected construction context with destructor!"); 98 } 99 // A temporary object that doesn't require materialization. 100 auto *CC = 101 C.getAllocator().Allocate<TemporaryObjectConstructionContext>(); 102 return new (CC) 103 TemporaryObjectConstructionContext(BTE, /*MTE=*/nullptr); 104 } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(S)) { 105 // If the object requires destruction and is not lifetime-extended, 106 // then it must have a BTE within its MTE. 107 assert(MTE->getType().getCanonicalType() 108 ->getAsCXXRecordDecl()->hasTrivialDestructor() || 109 MTE->getStorageDuration() != SD_FullExpression); 110 assert(TopLayer->isLast()); 111 auto *CC = 112 C.getAllocator().Allocate<TemporaryObjectConstructionContext>(); 113 return new (CC) TemporaryObjectConstructionContext(nullptr, MTE); 114 } else if (const auto *RS = dyn_cast<ReturnStmt>(S)) { 115 assert(TopLayer->isLast()); 116 auto *CC = 117 C.getAllocator().Allocate<SimpleReturnedValueConstructionContext>(); 118 return new (CC) SimpleReturnedValueConstructionContext(RS); 119 } 120 } else if (const CXXCtorInitializer *I = TopLayer->getTriggerInit()) { 121 assert(TopLayer->isLast()); 122 auto *CC = 123 C.getAllocator().Allocate<ConstructorInitializerConstructionContext>(); 124 return new (CC) ConstructorInitializerConstructionContext(I); 125 } 126 llvm_unreachable("Unexpected construction context!"); 127 } 128