ConstructionContext.cpp
9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//===- ConstructionContext.cpp - CFG constructor information --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the ConstructionContext class and its sub-classes,
// which represent various different ways of constructing C++ objects
// with the additional information the users may want to know about
// the constructor.
//
//===----------------------------------------------------------------------===//
#include "clang/Analysis/ConstructionContext.h"
#include "clang/AST/ExprObjC.h"
using namespace clang;
const ConstructionContextLayer *
ConstructionContextLayer::create(BumpVectorContext &C,
const ConstructionContextItem &Item,
const ConstructionContextLayer *Parent) {
ConstructionContextLayer *CC =
C.getAllocator().Allocate<ConstructionContextLayer>();
return new (CC) ConstructionContextLayer(Item, Parent);
}
bool ConstructionContextLayer::isStrictlyMoreSpecificThan(
const ConstructionContextLayer *Other) const {
const ConstructionContextLayer *Self = this;
while (true) {
if (!Other)
return Self;
if (!Self || !(Self->Item == Other->Item))
return false;
Self = Self->getParent();
Other = Other->getParent();
}
llvm_unreachable("The above loop can only be terminated via return!");
}
const ConstructionContext *
ConstructionContext::createMaterializedTemporaryFromLayers(
BumpVectorContext &C, const MaterializeTemporaryExpr *MTE,
const CXXBindTemporaryExpr *BTE,
const ConstructionContextLayer *ParentLayer) {
assert(MTE);
// If the object requires destruction and is not lifetime-extended,
// then it must have a BTE within its MTE, otherwise it shouldn't.
// FIXME: This should be an assertion.
if (!BTE && !(MTE->getType().getCanonicalType()->getAsCXXRecordDecl()
->hasTrivialDestructor() ||
MTE->getStorageDuration() != SD_FullExpression)) {
return nullptr;
}
// If the temporary is lifetime-extended, don't save the BTE,
// because we don't need a temporary destructor, but an automatic
// destructor.
if (MTE->getStorageDuration() != SD_FullExpression) {
BTE = nullptr;
}
// Handle pre-C++17 copy and move elision.
const CXXConstructExpr *ElidedCE = nullptr;
const ConstructionContext *ElidedCC = nullptr;
if (ParentLayer) {
const ConstructionContextItem &ElidedItem = ParentLayer->getItem();
assert(ElidedItem.getKind() ==
ConstructionContextItem::ElidableConstructorKind);
ElidedCE = cast<CXXConstructExpr>(ElidedItem.getStmt());
assert(ElidedCE->isElidable());
// We're creating a construction context that might have already
// been created elsewhere. Maybe we should unique our construction
// contexts. That's what we often do, but in this case it's unlikely
// to bring any benefits.
ElidedCC = createFromLayers(C, ParentLayer->getParent());
if (!ElidedCC) {
// We may fail to create the elided construction context.
// In this case, skip copy elision entirely.
return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE);
}
return create<ElidedTemporaryObjectConstructionContext>(
C, BTE, MTE, ElidedCE, ElidedCC);
}
// This is a normal temporary.
assert(!ParentLayer);
return create<SimpleTemporaryObjectConstructionContext>(C, BTE, MTE);
}
const ConstructionContext *ConstructionContext::createBoundTemporaryFromLayers(
BumpVectorContext &C, const CXXBindTemporaryExpr *BTE,
const ConstructionContextLayer *ParentLayer) {
if (!ParentLayer) {
// A temporary object that doesn't require materialization.
// In particular, it shouldn't require copy elision, because
// copy/move constructors take a reference, which requires
// materialization to obtain the glvalue.
return create<SimpleTemporaryObjectConstructionContext>(C, BTE,
/*MTE=*/nullptr);
}
const ConstructionContextItem &ParentItem = ParentLayer->getItem();
switch (ParentItem.getKind()) {
case ConstructionContextItem::VariableKind: {
const auto *DS = cast<DeclStmt>(ParentItem.getStmt());
assert(!cast<VarDecl>(DS->getSingleDecl())->getType().getCanonicalType()
->getAsCXXRecordDecl()->hasTrivialDestructor());
return create<CXX17ElidedCopyVariableConstructionContext>(C, DS, BTE);
}
case ConstructionContextItem::NewAllocatorKind: {
llvm_unreachable("This context does not accept a bound temporary!");
}
case ConstructionContextItem::ReturnKind: {
assert(ParentLayer->isLast());
const auto *RS = cast<ReturnStmt>(ParentItem.getStmt());
assert(!RS->getRetValue()->getType().getCanonicalType()
->getAsCXXRecordDecl()->hasTrivialDestructor());
return create<CXX17ElidedCopyReturnedValueConstructionContext>(C, RS,
BTE);
}
case ConstructionContextItem::MaterializationKind: {
// No assert. We may have an elidable copy on the grandparent layer.
const auto *MTE = cast<MaterializeTemporaryExpr>(ParentItem.getStmt());
return createMaterializedTemporaryFromLayers(C, MTE, BTE,
ParentLayer->getParent());
}
case ConstructionContextItem::TemporaryDestructorKind: {
llvm_unreachable("Duplicate CXXBindTemporaryExpr in the AST!");
}
case ConstructionContextItem::ElidedDestructorKind: {
llvm_unreachable("Elided destructor items are not produced by the CFG!");
}
case ConstructionContextItem::ElidableConstructorKind: {
llvm_unreachable("Materialization is necessary to put temporary into a "
"copy or move constructor!");
}
case ConstructionContextItem::ArgumentKind: {
assert(ParentLayer->isLast());
const auto *E = cast<Expr>(ParentItem.getStmt());
assert(isa<CallExpr>(E) || isa<CXXConstructExpr>(E) ||
isa<ObjCMessageExpr>(E));
return create<ArgumentConstructionContext>(C, E, ParentItem.getIndex(),
BTE);
}
case ConstructionContextItem::InitializerKind: {
assert(ParentLayer->isLast());
const auto *I = ParentItem.getCXXCtorInitializer();
assert(!I->getAnyMember()->getType().getCanonicalType()
->getAsCXXRecordDecl()->hasTrivialDestructor());
return create<CXX17ElidedCopyConstructorInitializerConstructionContext>(
C, I, BTE);
}
} // switch (ParentItem.getKind())
llvm_unreachable("Unexpected construction context with destructor!");
}
const ConstructionContext *ConstructionContext::createFromLayers(
BumpVectorContext &C, const ConstructionContextLayer *TopLayer) {
// Before this point all we've had was a stockpile of arbitrary layers.
// Now validate that it is shaped as one of the finite amount of expected
// patterns.
const ConstructionContextItem &TopItem = TopLayer->getItem();
switch (TopItem.getKind()) {
case ConstructionContextItem::VariableKind: {
assert(TopLayer->isLast());
const auto *DS = cast<DeclStmt>(TopItem.getStmt());
return create<SimpleVariableConstructionContext>(C, DS);
}
case ConstructionContextItem::NewAllocatorKind: {
assert(TopLayer->isLast());
const auto *NE = cast<CXXNewExpr>(TopItem.getStmt());
return create<NewAllocatedObjectConstructionContext>(C, NE);
}
case ConstructionContextItem::ReturnKind: {
assert(TopLayer->isLast());
const auto *RS = cast<ReturnStmt>(TopItem.getStmt());
return create<SimpleReturnedValueConstructionContext>(C, RS);
}
case ConstructionContextItem::MaterializationKind: {
const auto *MTE = cast<MaterializeTemporaryExpr>(TopItem.getStmt());
return createMaterializedTemporaryFromLayers(C, MTE, /*BTE=*/nullptr,
TopLayer->getParent());
}
case ConstructionContextItem::TemporaryDestructorKind: {
const auto *BTE = cast<CXXBindTemporaryExpr>(TopItem.getStmt());
assert(BTE->getType().getCanonicalType()->getAsCXXRecordDecl()
->hasNonTrivialDestructor());
return createBoundTemporaryFromLayers(C, BTE, TopLayer->getParent());
}
case ConstructionContextItem::ElidedDestructorKind: {
llvm_unreachable("Elided destructor items are not produced by the CFG!");
}
case ConstructionContextItem::ElidableConstructorKind: {
llvm_unreachable("The argument needs to be materialized first!");
}
case ConstructionContextItem::InitializerKind: {
assert(TopLayer->isLast());
const CXXCtorInitializer *I = TopItem.getCXXCtorInitializer();
return create<SimpleConstructorInitializerConstructionContext>(C, I);
}
case ConstructionContextItem::ArgumentKind: {
assert(TopLayer->isLast());
const auto *E = cast<Expr>(TopItem.getStmt());
return create<ArgumentConstructionContext>(C, E, TopItem.getIndex(),
/*BTE=*/nullptr);
}
} // switch (TopItem.getKind())
llvm_unreachable("Unexpected construction context!");
}