/src/llvm-project/clang/lib/AST/ExprCXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the subclesses of Expr class declared in ExprCXX.h |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ExprCXX.h" |
14 | | #include "clang/AST/ASTContext.h" |
15 | | #include "clang/AST/Attr.h" |
16 | | #include "clang/AST/ComputeDependence.h" |
17 | | #include "clang/AST/Decl.h" |
18 | | #include "clang/AST/DeclAccessPair.h" |
19 | | #include "clang/AST/DeclBase.h" |
20 | | #include "clang/AST/DeclCXX.h" |
21 | | #include "clang/AST/DeclTemplate.h" |
22 | | #include "clang/AST/DeclarationName.h" |
23 | | #include "clang/AST/DependenceFlags.h" |
24 | | #include "clang/AST/Expr.h" |
25 | | #include "clang/AST/LambdaCapture.h" |
26 | | #include "clang/AST/NestedNameSpecifier.h" |
27 | | #include "clang/AST/TemplateBase.h" |
28 | | #include "clang/AST/Type.h" |
29 | | #include "clang/AST/TypeLoc.h" |
30 | | #include "clang/Basic/LLVM.h" |
31 | | #include "clang/Basic/OperatorKinds.h" |
32 | | #include "clang/Basic/SourceLocation.h" |
33 | | #include "clang/Basic/Specifiers.h" |
34 | | #include "llvm/ADT/ArrayRef.h" |
35 | | #include "llvm/Support/Casting.h" |
36 | | #include "llvm/Support/ErrorHandling.h" |
37 | | #include <cassert> |
38 | | #include <cstddef> |
39 | | #include <cstring> |
40 | | #include <memory> |
41 | | #include <optional> |
42 | | |
43 | | using namespace clang; |
44 | | |
45 | | //===----------------------------------------------------------------------===// |
46 | | // Child Iterators for iterating over subexpressions/substatements |
47 | | //===----------------------------------------------------------------------===// |
48 | | |
49 | 0 | bool CXXOperatorCallExpr::isInfixBinaryOp() const { |
50 | | // An infix binary operator is any operator with two arguments other than |
51 | | // operator() and operator[]. Note that none of these operators can have |
52 | | // default arguments, so it suffices to check the number of argument |
53 | | // expressions. |
54 | 0 | if (getNumArgs() != 2) |
55 | 0 | return false; |
56 | | |
57 | 0 | switch (getOperator()) { |
58 | 0 | case OO_Call: case OO_Subscript: |
59 | 0 | return false; |
60 | 0 | default: |
61 | 0 | return true; |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | CXXRewrittenBinaryOperator::DecomposedForm |
66 | 0 | CXXRewrittenBinaryOperator::getDecomposedForm() const { |
67 | 0 | DecomposedForm Result = {}; |
68 | 0 | const Expr *E = getSemanticForm()->IgnoreImplicit(); |
69 | | |
70 | | // Remove an outer '!' if it exists (only happens for a '!=' rewrite). |
71 | 0 | bool SkippedNot = false; |
72 | 0 | if (auto *NotEq = dyn_cast<UnaryOperator>(E)) { |
73 | 0 | assert(NotEq->getOpcode() == UO_LNot); |
74 | 0 | E = NotEq->getSubExpr()->IgnoreImplicit(); |
75 | 0 | SkippedNot = true; |
76 | 0 | } |
77 | | |
78 | | // Decompose the outer binary operator. |
79 | 0 | if (auto *BO = dyn_cast<BinaryOperator>(E)) { |
80 | 0 | assert(!SkippedNot || BO->getOpcode() == BO_EQ); |
81 | 0 | Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode(); |
82 | 0 | Result.LHS = BO->getLHS(); |
83 | 0 | Result.RHS = BO->getRHS(); |
84 | 0 | Result.InnerBinOp = BO; |
85 | 0 | } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) { |
86 | 0 | assert(!SkippedNot || BO->getOperator() == OO_EqualEqual); |
87 | 0 | assert(BO->isInfixBinaryOp()); |
88 | 0 | switch (BO->getOperator()) { |
89 | 0 | case OO_Less: Result.Opcode = BO_LT; break; |
90 | 0 | case OO_LessEqual: Result.Opcode = BO_LE; break; |
91 | 0 | case OO_Greater: Result.Opcode = BO_GT; break; |
92 | 0 | case OO_GreaterEqual: Result.Opcode = BO_GE; break; |
93 | 0 | case OO_Spaceship: Result.Opcode = BO_Cmp; break; |
94 | 0 | case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break; |
95 | 0 | default: llvm_unreachable("unexpected binop in rewritten operator expr"); |
96 | 0 | } |
97 | 0 | Result.LHS = BO->getArg(0); |
98 | 0 | Result.RHS = BO->getArg(1); |
99 | 0 | Result.InnerBinOp = BO; |
100 | 0 | } else { |
101 | 0 | llvm_unreachable("unexpected rewritten operator form"); |
102 | 0 | } |
103 | | |
104 | | // Put the operands in the right order for == and !=, and canonicalize the |
105 | | // <=> subexpression onto the LHS for all other forms. |
106 | 0 | if (isReversed()) |
107 | 0 | std::swap(Result.LHS, Result.RHS); |
108 | | |
109 | | // If this isn't a spaceship rewrite, we're done. |
110 | 0 | if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE) |
111 | 0 | return Result; |
112 | | |
113 | | // Otherwise, we expect a <=> to now be on the LHS. |
114 | 0 | E = Result.LHS->IgnoreUnlessSpelledInSource(); |
115 | 0 | if (auto *BO = dyn_cast<BinaryOperator>(E)) { |
116 | 0 | assert(BO->getOpcode() == BO_Cmp); |
117 | 0 | Result.LHS = BO->getLHS(); |
118 | 0 | Result.RHS = BO->getRHS(); |
119 | 0 | Result.InnerBinOp = BO; |
120 | 0 | } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) { |
121 | 0 | assert(BO->getOperator() == OO_Spaceship); |
122 | 0 | Result.LHS = BO->getArg(0); |
123 | 0 | Result.RHS = BO->getArg(1); |
124 | 0 | Result.InnerBinOp = BO; |
125 | 0 | } else { |
126 | 0 | llvm_unreachable("unexpected rewritten operator form"); |
127 | 0 | } |
128 | | |
129 | | // Put the comparison operands in the right order. |
130 | 0 | if (isReversed()) |
131 | 0 | std::swap(Result.LHS, Result.RHS); |
132 | 0 | return Result; |
133 | 0 | } |
134 | | |
135 | 0 | bool CXXTypeidExpr::isPotentiallyEvaluated() const { |
136 | 0 | if (isTypeOperand()) |
137 | 0 | return false; |
138 | | |
139 | | // C++11 [expr.typeid]p3: |
140 | | // When typeid is applied to an expression other than a glvalue of |
141 | | // polymorphic class type, [...] the expression is an unevaluated operand. |
142 | 0 | const Expr *E = getExprOperand(); |
143 | 0 | if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl()) |
144 | 0 | if (RD->isPolymorphic() && E->isGLValue()) |
145 | 0 | return true; |
146 | | |
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | 0 | bool CXXTypeidExpr::isMostDerived(ASTContext &Context) const { |
151 | 0 | assert(!isTypeOperand() && "Cannot call isMostDerived for typeid(type)"); |
152 | 0 | const Expr *E = getExprOperand()->IgnoreParenNoopCasts(Context); |
153 | 0 | if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) { |
154 | 0 | QualType Ty = DRE->getDecl()->getType(); |
155 | 0 | if (!Ty->isPointerType() && !Ty->isReferenceType()) |
156 | 0 | return true; |
157 | 0 | } |
158 | | |
159 | 0 | return false; |
160 | 0 | } |
161 | | |
162 | 0 | QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const { |
163 | 0 | assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); |
164 | 0 | Qualifiers Quals; |
165 | 0 | return Context.getUnqualifiedArrayType( |
166 | 0 | Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); |
167 | 0 | } |
168 | | |
169 | 0 | QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const { |
170 | 0 | assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); |
171 | 0 | Qualifiers Quals; |
172 | 0 | return Context.getUnqualifiedArrayType( |
173 | 0 | Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals); |
174 | 0 | } |
175 | | |
176 | | // CXXScalarValueInitExpr |
177 | 0 | SourceLocation CXXScalarValueInitExpr::getBeginLoc() const { |
178 | 0 | return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc(); |
179 | 0 | } |
180 | | |
181 | | // CXXNewExpr |
182 | | CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew, |
183 | | FunctionDecl *OperatorDelete, bool ShouldPassAlignment, |
184 | | bool UsualArrayDeleteWantsSize, |
185 | | ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens, |
186 | | std::optional<Expr *> ArraySize, |
187 | | CXXNewInitializationStyle InitializationStyle, |
188 | | Expr *Initializer, QualType Ty, |
189 | | TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, |
190 | | SourceRange DirectInitRange) |
191 | | : Expr(CXXNewExprClass, Ty, VK_PRValue, OK_Ordinary), |
192 | | OperatorNew(OperatorNew), OperatorDelete(OperatorDelete), |
193 | | AllocatedTypeInfo(AllocatedTypeInfo), Range(Range), |
194 | 0 | DirectInitRange(DirectInitRange) { |
195 | |
|
196 | 0 | assert((Initializer != nullptr || |
197 | 0 | InitializationStyle == CXXNewInitializationStyle::None || |
198 | 0 | InitializationStyle == CXXNewInitializationStyle::Implicit) && |
199 | 0 | "Only NoInit can have no initializer!"); |
200 | | |
201 | 0 | CXXNewExprBits.IsGlobalNew = IsGlobalNew; |
202 | 0 | CXXNewExprBits.IsArray = ArraySize.has_value(); |
203 | 0 | CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment; |
204 | 0 | CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize; |
205 | 0 | CXXNewExprBits.StoredInitializationStyle = |
206 | 0 | llvm::to_underlying(InitializationStyle); |
207 | 0 | bool IsParenTypeId = TypeIdParens.isValid(); |
208 | 0 | CXXNewExprBits.IsParenTypeId = IsParenTypeId; |
209 | 0 | CXXNewExprBits.NumPlacementArgs = PlacementArgs.size(); |
210 | |
|
211 | 0 | if (ArraySize) |
212 | 0 | getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize; |
213 | 0 | if (Initializer) |
214 | 0 | getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer; |
215 | 0 | for (unsigned I = 0; I != PlacementArgs.size(); ++I) |
216 | 0 | getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] = |
217 | 0 | PlacementArgs[I]; |
218 | 0 | if (IsParenTypeId) |
219 | 0 | getTrailingObjects<SourceRange>()[0] = TypeIdParens; |
220 | |
|
221 | 0 | switch (getInitializationStyle()) { |
222 | 0 | case CXXNewInitializationStyle::Call: |
223 | 0 | this->Range.setEnd(DirectInitRange.getEnd()); |
224 | 0 | break; |
225 | 0 | case CXXNewInitializationStyle::List: |
226 | 0 | this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); |
227 | 0 | break; |
228 | 0 | default: |
229 | 0 | if (IsParenTypeId) |
230 | 0 | this->Range.setEnd(TypeIdParens.getEnd()); |
231 | 0 | break; |
232 | 0 | } |
233 | | |
234 | 0 | setDependence(computeDependence(this)); |
235 | 0 | } |
236 | | |
237 | | CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray, |
238 | | unsigned NumPlacementArgs, bool IsParenTypeId) |
239 | 0 | : Expr(CXXNewExprClass, Empty) { |
240 | 0 | CXXNewExprBits.IsArray = IsArray; |
241 | 0 | CXXNewExprBits.NumPlacementArgs = NumPlacementArgs; |
242 | 0 | CXXNewExprBits.IsParenTypeId = IsParenTypeId; |
243 | 0 | } |
244 | | |
245 | | CXXNewExpr *CXXNewExpr::Create( |
246 | | const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew, |
247 | | FunctionDecl *OperatorDelete, bool ShouldPassAlignment, |
248 | | bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, |
249 | | SourceRange TypeIdParens, std::optional<Expr *> ArraySize, |
250 | | CXXNewInitializationStyle InitializationStyle, Expr *Initializer, |
251 | | QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, |
252 | 0 | SourceRange DirectInitRange) { |
253 | 0 | bool IsArray = ArraySize.has_value(); |
254 | 0 | bool HasInit = Initializer != nullptr; |
255 | 0 | unsigned NumPlacementArgs = PlacementArgs.size(); |
256 | 0 | bool IsParenTypeId = TypeIdParens.isValid(); |
257 | 0 | void *Mem = |
258 | 0 | Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>( |
259 | 0 | IsArray + HasInit + NumPlacementArgs, IsParenTypeId), |
260 | 0 | alignof(CXXNewExpr)); |
261 | 0 | return new (Mem) |
262 | 0 | CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment, |
263 | 0 | UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens, |
264 | 0 | ArraySize, InitializationStyle, Initializer, Ty, |
265 | 0 | AllocatedTypeInfo, Range, DirectInitRange); |
266 | 0 | } |
267 | | |
268 | | CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray, |
269 | | bool HasInit, unsigned NumPlacementArgs, |
270 | 0 | bool IsParenTypeId) { |
271 | 0 | void *Mem = |
272 | 0 | Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>( |
273 | 0 | IsArray + HasInit + NumPlacementArgs, IsParenTypeId), |
274 | 0 | alignof(CXXNewExpr)); |
275 | 0 | return new (Mem) |
276 | 0 | CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId); |
277 | 0 | } |
278 | | |
279 | 0 | bool CXXNewExpr::shouldNullCheckAllocation() const { |
280 | 0 | if (getOperatorNew()->getLangOpts().CheckNew) |
281 | 0 | return true; |
282 | 0 | return !getOperatorNew()->hasAttr<ReturnsNonNullAttr>() && |
283 | 0 | getOperatorNew() |
284 | 0 | ->getType() |
285 | 0 | ->castAs<FunctionProtoType>() |
286 | 0 | ->isNothrow() && |
287 | 0 | !getOperatorNew()->isReservedGlobalPlacementOperator(); |
288 | 0 | } |
289 | | |
290 | | // CXXDeleteExpr |
291 | 0 | QualType CXXDeleteExpr::getDestroyedType() const { |
292 | 0 | const Expr *Arg = getArgument(); |
293 | | |
294 | | // For a destroying operator delete, we may have implicitly converted the |
295 | | // pointer type to the type of the parameter of the 'operator delete' |
296 | | // function. |
297 | 0 | while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { |
298 | 0 | if (ICE->getCastKind() == CK_DerivedToBase || |
299 | 0 | ICE->getCastKind() == CK_UncheckedDerivedToBase || |
300 | 0 | ICE->getCastKind() == CK_NoOp) { |
301 | 0 | assert((ICE->getCastKind() == CK_NoOp || |
302 | 0 | getOperatorDelete()->isDestroyingOperatorDelete()) && |
303 | 0 | "only a destroying operator delete can have a converted arg"); |
304 | 0 | Arg = ICE->getSubExpr(); |
305 | 0 | } else |
306 | 0 | break; |
307 | 0 | } |
308 | | |
309 | | // The type-to-delete may not be a pointer if it's a dependent type. |
310 | 0 | const QualType ArgType = Arg->getType(); |
311 | |
|
312 | 0 | if (ArgType->isDependentType() && !ArgType->isPointerType()) |
313 | 0 | return QualType(); |
314 | | |
315 | 0 | return ArgType->castAs<PointerType>()->getPointeeType(); |
316 | 0 | } |
317 | | |
318 | | // CXXPseudoDestructorExpr |
319 | | PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info) |
320 | 0 | : Type(Info) { |
321 | 0 | Location = Info->getTypeLoc().getBeginLoc(); |
322 | 0 | } |
323 | | |
324 | | CXXPseudoDestructorExpr::CXXPseudoDestructorExpr( |
325 | | const ASTContext &Context, Expr *Base, bool isArrow, |
326 | | SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc, |
327 | | TypeSourceInfo *ScopeType, SourceLocation ColonColonLoc, |
328 | | SourceLocation TildeLoc, PseudoDestructorTypeStorage DestroyedType) |
329 | | : Expr(CXXPseudoDestructorExprClass, Context.BoundMemberTy, VK_PRValue, |
330 | | OK_Ordinary), |
331 | | Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), |
332 | | OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), |
333 | | ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc), |
334 | 0 | DestroyedType(DestroyedType) { |
335 | 0 | setDependence(computeDependence(this)); |
336 | 0 | } |
337 | | |
338 | 0 | QualType CXXPseudoDestructorExpr::getDestroyedType() const { |
339 | 0 | if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) |
340 | 0 | return TInfo->getType(); |
341 | | |
342 | 0 | return QualType(); |
343 | 0 | } |
344 | | |
345 | 0 | SourceLocation CXXPseudoDestructorExpr::getEndLoc() const { |
346 | 0 | SourceLocation End = DestroyedType.getLocation(); |
347 | 0 | if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo()) |
348 | 0 | End = TInfo->getTypeLoc().getSourceRange().getEnd(); |
349 | 0 | return End; |
350 | 0 | } |
351 | | |
352 | | // UnresolvedLookupExpr |
353 | | UnresolvedLookupExpr::UnresolvedLookupExpr( |
354 | | const ASTContext &Context, CXXRecordDecl *NamingClass, |
355 | | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
356 | | const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded, |
357 | | const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin, |
358 | | UnresolvedSetIterator End, bool KnownDependent) |
359 | | : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc, |
360 | | TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, |
361 | | KnownDependent, false, false), |
362 | 1 | NamingClass(NamingClass) { |
363 | 1 | UnresolvedLookupExprBits.RequiresADL = RequiresADL; |
364 | 1 | UnresolvedLookupExprBits.Overloaded = Overloaded; |
365 | 1 | } |
366 | | |
367 | | UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty, |
368 | | unsigned NumResults, |
369 | | bool HasTemplateKWAndArgsInfo) |
370 | | : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults, |
371 | 0 | HasTemplateKWAndArgsInfo) {} |
372 | | |
373 | | UnresolvedLookupExpr *UnresolvedLookupExpr::Create( |
374 | | const ASTContext &Context, CXXRecordDecl *NamingClass, |
375 | | NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo, |
376 | | bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin, |
377 | 1 | UnresolvedSetIterator End) { |
378 | 1 | unsigned NumResults = End - Begin; |
379 | 1 | unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo, |
380 | 1 | TemplateArgumentLoc>(NumResults, 0, 0); |
381 | 1 | void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr)); |
382 | 1 | return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc, |
383 | 1 | SourceLocation(), NameInfo, RequiresADL, |
384 | 1 | Overloaded, nullptr, Begin, End, false); |
385 | 1 | } |
386 | | |
387 | | UnresolvedLookupExpr *UnresolvedLookupExpr::Create( |
388 | | const ASTContext &Context, CXXRecordDecl *NamingClass, |
389 | | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
390 | | const DeclarationNameInfo &NameInfo, bool RequiresADL, |
391 | | const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin, |
392 | 0 | UnresolvedSetIterator End, bool KnownDependent) { |
393 | 0 | assert(Args || TemplateKWLoc.isValid()); |
394 | 0 | unsigned NumResults = End - Begin; |
395 | 0 | unsigned NumTemplateArgs = Args ? Args->size() : 0; |
396 | 0 | unsigned Size = |
397 | 0 | totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo, |
398 | 0 | TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs); |
399 | 0 | void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr)); |
400 | 0 | return new (Mem) UnresolvedLookupExpr( |
401 | 0 | Context, NamingClass, QualifierLoc, TemplateKWLoc, NameInfo, RequiresADL, |
402 | 0 | /*Overloaded=*/true, Args, Begin, End, KnownDependent); |
403 | 0 | } |
404 | | |
405 | | UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty( |
406 | | const ASTContext &Context, unsigned NumResults, |
407 | 0 | bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) { |
408 | 0 | assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); |
409 | 0 | unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo, |
410 | 0 | TemplateArgumentLoc>( |
411 | 0 | NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs); |
412 | 0 | void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr)); |
413 | 0 | return new (Mem) |
414 | 0 | UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo); |
415 | 0 | } |
416 | | |
417 | | OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context, |
418 | | NestedNameSpecifierLoc QualifierLoc, |
419 | | SourceLocation TemplateKWLoc, |
420 | | const DeclarationNameInfo &NameInfo, |
421 | | const TemplateArgumentListInfo *TemplateArgs, |
422 | | UnresolvedSetIterator Begin, |
423 | | UnresolvedSetIterator End, bool KnownDependent, |
424 | | bool KnownInstantiationDependent, |
425 | | bool KnownContainsUnexpandedParameterPack) |
426 | | : Expr(SC, Context.OverloadTy, VK_LValue, OK_Ordinary), NameInfo(NameInfo), |
427 | 1 | QualifierLoc(QualifierLoc) { |
428 | 1 | unsigned NumResults = End - Begin; |
429 | 1 | OverloadExprBits.NumResults = NumResults; |
430 | 1 | OverloadExprBits.HasTemplateKWAndArgsInfo = |
431 | 1 | (TemplateArgs != nullptr ) || TemplateKWLoc.isValid(); |
432 | | |
433 | 1 | if (NumResults) { |
434 | | // Copy the results to the trailing array past UnresolvedLookupExpr |
435 | | // or UnresolvedMemberExpr. |
436 | 0 | DeclAccessPair *Results = getTrailingResults(); |
437 | 0 | memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair)); |
438 | 0 | } |
439 | | |
440 | 1 | if (TemplateArgs) { |
441 | 0 | auto Deps = TemplateArgumentDependence::None; |
442 | 0 | getTrailingASTTemplateKWAndArgsInfo()->initializeFrom( |
443 | 0 | TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), Deps); |
444 | 1 | } else if (TemplateKWLoc.isValid()) { |
445 | 0 | getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); |
446 | 0 | } |
447 | | |
448 | 1 | setDependence(computeDependence(this, KnownDependent, |
449 | 1 | KnownInstantiationDependent, |
450 | 1 | KnownContainsUnexpandedParameterPack)); |
451 | 1 | if (isTypeDependent()) |
452 | 0 | setType(Context.DependentTy); |
453 | 1 | } |
454 | | |
455 | | OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults, |
456 | | bool HasTemplateKWAndArgsInfo) |
457 | 0 | : Expr(SC, Empty) { |
458 | 0 | OverloadExprBits.NumResults = NumResults; |
459 | 0 | OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; |
460 | 0 | } |
461 | | |
462 | | // DependentScopeDeclRefExpr |
463 | | DependentScopeDeclRefExpr::DependentScopeDeclRefExpr( |
464 | | QualType Ty, NestedNameSpecifierLoc QualifierLoc, |
465 | | SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, |
466 | | const TemplateArgumentListInfo *Args) |
467 | | : Expr(DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary), |
468 | 0 | QualifierLoc(QualifierLoc), NameInfo(NameInfo) { |
469 | 0 | DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo = |
470 | 0 | (Args != nullptr) || TemplateKWLoc.isValid(); |
471 | 0 | if (Args) { |
472 | 0 | auto Deps = TemplateArgumentDependence::None; |
473 | 0 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
474 | 0 | TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(), Deps); |
475 | 0 | } else if (TemplateKWLoc.isValid()) { |
476 | 0 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
477 | 0 | TemplateKWLoc); |
478 | 0 | } |
479 | 0 | setDependence(computeDependence(this)); |
480 | 0 | } |
481 | | |
482 | | DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create( |
483 | | const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, |
484 | | SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, |
485 | 0 | const TemplateArgumentListInfo *Args) { |
486 | 0 | assert(QualifierLoc && "should be created for dependent qualifiers"); |
487 | 0 | bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid(); |
488 | 0 | std::size_t Size = |
489 | 0 | totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>( |
490 | 0 | HasTemplateKWAndArgsInfo, Args ? Args->size() : 0); |
491 | 0 | void *Mem = Context.Allocate(Size); |
492 | 0 | return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc, |
493 | 0 | TemplateKWLoc, NameInfo, Args); |
494 | 0 | } |
495 | | |
496 | | DependentScopeDeclRefExpr * |
497 | | DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context, |
498 | | bool HasTemplateKWAndArgsInfo, |
499 | 0 | unsigned NumTemplateArgs) { |
500 | 0 | assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); |
501 | 0 | std::size_t Size = |
502 | 0 | totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>( |
503 | 0 | HasTemplateKWAndArgsInfo, NumTemplateArgs); |
504 | 0 | void *Mem = Context.Allocate(Size); |
505 | 0 | auto *E = new (Mem) DependentScopeDeclRefExpr( |
506 | 0 | QualType(), NestedNameSpecifierLoc(), SourceLocation(), |
507 | 0 | DeclarationNameInfo(), nullptr); |
508 | 0 | E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo = |
509 | 0 | HasTemplateKWAndArgsInfo; |
510 | 0 | return E; |
511 | 0 | } |
512 | | |
513 | 0 | SourceLocation CXXConstructExpr::getBeginLoc() const { |
514 | 0 | if (isa<CXXTemporaryObjectExpr>(this)) |
515 | 0 | return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc(); |
516 | 0 | return getLocation(); |
517 | 0 | } |
518 | | |
519 | 0 | SourceLocation CXXConstructExpr::getEndLoc() const { |
520 | 0 | if (isa<CXXTemporaryObjectExpr>(this)) |
521 | 0 | return cast<CXXTemporaryObjectExpr>(this)->getEndLoc(); |
522 | | |
523 | 0 | if (ParenOrBraceRange.isValid()) |
524 | 0 | return ParenOrBraceRange.getEnd(); |
525 | | |
526 | 0 | SourceLocation End = getLocation(); |
527 | 0 | for (unsigned I = getNumArgs(); I > 0; --I) { |
528 | 0 | const Expr *Arg = getArg(I-1); |
529 | 0 | if (!Arg->isDefaultArgument()) { |
530 | 0 | SourceLocation NewEnd = Arg->getEndLoc(); |
531 | 0 | if (NewEnd.isValid()) { |
532 | 0 | End = NewEnd; |
533 | 0 | break; |
534 | 0 | } |
535 | 0 | } |
536 | 0 | } |
537 | |
|
538 | 0 | return End; |
539 | 0 | } |
540 | | |
541 | | CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind, |
542 | | Expr *Fn, ArrayRef<Expr *> Args, |
543 | | QualType Ty, ExprValueKind VK, |
544 | | SourceLocation OperatorLoc, |
545 | | FPOptionsOverride FPFeatures, |
546 | | ADLCallKind UsesADL) |
547 | | : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, |
548 | 0 | OperatorLoc, FPFeatures, /*MinNumArgs=*/0, UsesADL) { |
549 | 0 | CXXOperatorCallExprBits.OperatorKind = OpKind; |
550 | 0 | assert( |
551 | 0 | (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) && |
552 | 0 | "OperatorKind overflow!"); |
553 | 0 | Range = getSourceRangeImpl(); |
554 | 0 | } |
555 | | |
556 | | CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, bool HasFPFeatures, |
557 | | EmptyShell Empty) |
558 | | : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, |
559 | 0 | HasFPFeatures, Empty) {} |
560 | | |
561 | | CXXOperatorCallExpr * |
562 | | CXXOperatorCallExpr::Create(const ASTContext &Ctx, |
563 | | OverloadedOperatorKind OpKind, Expr *Fn, |
564 | | ArrayRef<Expr *> Args, QualType Ty, |
565 | | ExprValueKind VK, SourceLocation OperatorLoc, |
566 | 0 | FPOptionsOverride FPFeatures, ADLCallKind UsesADL) { |
567 | | // Allocate storage for the trailing objects of CallExpr. |
568 | 0 | unsigned NumArgs = Args.size(); |
569 | 0 | unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects( |
570 | 0 | /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage()); |
571 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects, |
572 | 0 | alignof(CXXOperatorCallExpr)); |
573 | 0 | return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc, |
574 | 0 | FPFeatures, UsesADL); |
575 | 0 | } |
576 | | |
577 | | CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx, |
578 | | unsigned NumArgs, |
579 | | bool HasFPFeatures, |
580 | 0 | EmptyShell Empty) { |
581 | | // Allocate storage for the trailing objects of CallExpr. |
582 | 0 | unsigned SizeOfTrailingObjects = |
583 | 0 | CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures); |
584 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects, |
585 | 0 | alignof(CXXOperatorCallExpr)); |
586 | 0 | return new (Mem) CXXOperatorCallExpr(NumArgs, HasFPFeatures, Empty); |
587 | 0 | } |
588 | | |
589 | 0 | SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const { |
590 | 0 | OverloadedOperatorKind Kind = getOperator(); |
591 | 0 | if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) { |
592 | 0 | if (getNumArgs() == 1) |
593 | | // Prefix operator |
594 | 0 | return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc()); |
595 | 0 | else |
596 | | // Postfix operator |
597 | 0 | return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc()); |
598 | 0 | } else if (Kind == OO_Arrow) { |
599 | 0 | return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc()); |
600 | 0 | } else if (Kind == OO_Call) { |
601 | 0 | return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc()); |
602 | 0 | } else if (Kind == OO_Subscript) { |
603 | 0 | return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc()); |
604 | 0 | } else if (getNumArgs() == 1) { |
605 | 0 | return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc()); |
606 | 0 | } else if (getNumArgs() == 2) { |
607 | 0 | return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc()); |
608 | 0 | } else { |
609 | 0 | return getOperatorLoc(); |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | | CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args, |
614 | | QualType Ty, ExprValueKind VK, |
615 | | SourceLocation RP, |
616 | | FPOptionsOverride FPOptions, |
617 | | unsigned MinNumArgs) |
618 | | : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP, |
619 | 0 | FPOptions, MinNumArgs, NotADL) {} |
620 | | |
621 | | CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, bool HasFPFeatures, |
622 | | EmptyShell Empty) |
623 | | : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, HasFPFeatures, |
624 | 0 | Empty) {} |
625 | | |
626 | | CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn, |
627 | | ArrayRef<Expr *> Args, QualType Ty, |
628 | | ExprValueKind VK, |
629 | | SourceLocation RP, |
630 | | FPOptionsOverride FPFeatures, |
631 | 0 | unsigned MinNumArgs) { |
632 | | // Allocate storage for the trailing objects of CallExpr. |
633 | 0 | unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs); |
634 | 0 | unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects( |
635 | 0 | /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage()); |
636 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects, |
637 | 0 | alignof(CXXMemberCallExpr)); |
638 | 0 | return new (Mem) |
639 | 0 | CXXMemberCallExpr(Fn, Args, Ty, VK, RP, FPFeatures, MinNumArgs); |
640 | 0 | } |
641 | | |
642 | | CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx, |
643 | | unsigned NumArgs, |
644 | | bool HasFPFeatures, |
645 | 0 | EmptyShell Empty) { |
646 | | // Allocate storage for the trailing objects of CallExpr. |
647 | 0 | unsigned SizeOfTrailingObjects = |
648 | 0 | CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPFeatures); |
649 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects, |
650 | 0 | alignof(CXXMemberCallExpr)); |
651 | 0 | return new (Mem) CXXMemberCallExpr(NumArgs, HasFPFeatures, Empty); |
652 | 0 | } |
653 | | |
654 | 0 | Expr *CXXMemberCallExpr::getImplicitObjectArgument() const { |
655 | 0 | const Expr *Callee = getCallee()->IgnoreParens(); |
656 | 0 | if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee)) |
657 | 0 | return MemExpr->getBase(); |
658 | 0 | if (const auto *BO = dyn_cast<BinaryOperator>(Callee)) |
659 | 0 | if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI) |
660 | 0 | return BO->getLHS(); |
661 | | |
662 | | // FIXME: Will eventually need to cope with member pointers. |
663 | 0 | return nullptr; |
664 | 0 | } |
665 | | |
666 | 0 | QualType CXXMemberCallExpr::getObjectType() const { |
667 | 0 | QualType Ty = getImplicitObjectArgument()->getType(); |
668 | 0 | if (Ty->isPointerType()) |
669 | 0 | Ty = Ty->getPointeeType(); |
670 | 0 | return Ty; |
671 | 0 | } |
672 | | |
673 | 0 | CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const { |
674 | 0 | if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens())) |
675 | 0 | return cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
676 | | |
677 | | // FIXME: Will eventually need to cope with member pointers. |
678 | | // NOTE: Update makeTailCallIfSwiftAsync on fixing this. |
679 | 0 | return nullptr; |
680 | 0 | } |
681 | | |
682 | 0 | CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const { |
683 | 0 | Expr* ThisArg = getImplicitObjectArgument(); |
684 | 0 | if (!ThisArg) |
685 | 0 | return nullptr; |
686 | | |
687 | 0 | if (ThisArg->getType()->isAnyPointerType()) |
688 | 0 | return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl(); |
689 | | |
690 | 0 | return ThisArg->getType()->getAsCXXRecordDecl(); |
691 | 0 | } |
692 | | |
693 | | //===----------------------------------------------------------------------===// |
694 | | // Named casts |
695 | | //===----------------------------------------------------------------------===// |
696 | | |
697 | | /// getCastName - Get the name of the C++ cast being used, e.g., |
698 | | /// "static_cast", "dynamic_cast", "reinterpret_cast", or |
699 | | /// "const_cast". The returned pointer must not be freed. |
700 | 0 | const char *CXXNamedCastExpr::getCastName() const { |
701 | 0 | switch (getStmtClass()) { |
702 | 0 | case CXXStaticCastExprClass: return "static_cast"; |
703 | 0 | case CXXDynamicCastExprClass: return "dynamic_cast"; |
704 | 0 | case CXXReinterpretCastExprClass: return "reinterpret_cast"; |
705 | 0 | case CXXConstCastExprClass: return "const_cast"; |
706 | 0 | case CXXAddrspaceCastExprClass: return "addrspace_cast"; |
707 | 0 | default: return "<invalid cast>"; |
708 | 0 | } |
709 | 0 | } |
710 | | |
711 | | CXXStaticCastExpr * |
712 | | CXXStaticCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK, |
713 | | CastKind K, Expr *Op, const CXXCastPath *BasePath, |
714 | | TypeSourceInfo *WrittenTy, FPOptionsOverride FPO, |
715 | | SourceLocation L, SourceLocation RParenLoc, |
716 | 0 | SourceRange AngleBrackets) { |
717 | 0 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
718 | 0 | void *Buffer = |
719 | 0 | C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>( |
720 | 0 | PathSize, FPO.requiresTrailingStorage())); |
721 | 0 | auto *E = new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, |
722 | 0 | FPO, L, RParenLoc, AngleBrackets); |
723 | 0 | if (PathSize) |
724 | 0 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
725 | 0 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
726 | 0 | return E; |
727 | 0 | } |
728 | | |
729 | | CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C, |
730 | | unsigned PathSize, |
731 | 0 | bool HasFPFeatures) { |
732 | 0 | void *Buffer = |
733 | 0 | C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>( |
734 | 0 | PathSize, HasFPFeatures)); |
735 | 0 | return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize, HasFPFeatures); |
736 | 0 | } |
737 | | |
738 | | CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T, |
739 | | ExprValueKind VK, |
740 | | CastKind K, Expr *Op, |
741 | | const CXXCastPath *BasePath, |
742 | | TypeSourceInfo *WrittenTy, |
743 | | SourceLocation L, |
744 | | SourceLocation RParenLoc, |
745 | 0 | SourceRange AngleBrackets) { |
746 | 0 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
747 | 0 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
748 | 0 | auto *E = |
749 | 0 | new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, |
750 | 0 | RParenLoc, AngleBrackets); |
751 | 0 | if (PathSize) |
752 | 0 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
753 | 0 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
754 | 0 | return E; |
755 | 0 | } |
756 | | |
757 | | CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C, |
758 | 0 | unsigned PathSize) { |
759 | 0 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
760 | 0 | return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize); |
761 | 0 | } |
762 | | |
763 | | /// isAlwaysNull - Return whether the result of the dynamic_cast is proven |
764 | | /// to always be null. For example: |
765 | | /// |
766 | | /// struct A { }; |
767 | | /// struct B final : A { }; |
768 | | /// struct C { }; |
769 | | /// |
770 | | /// C *f(B* b) { return dynamic_cast<C*>(b); } |
771 | 0 | bool CXXDynamicCastExpr::isAlwaysNull() const { |
772 | 0 | if (isValueDependent() || getCastKind() != CK_Dynamic) |
773 | 0 | return false; |
774 | | |
775 | 0 | QualType SrcType = getSubExpr()->getType(); |
776 | 0 | QualType DestType = getType(); |
777 | |
|
778 | 0 | if (DestType->isVoidPointerType()) |
779 | 0 | return false; |
780 | | |
781 | 0 | if (DestType->isPointerType()) { |
782 | 0 | SrcType = SrcType->getPointeeType(); |
783 | 0 | DestType = DestType->getPointeeType(); |
784 | 0 | } |
785 | |
|
786 | 0 | const auto *SrcRD = SrcType->getAsCXXRecordDecl(); |
787 | 0 | const auto *DestRD = DestType->getAsCXXRecordDecl(); |
788 | 0 | assert(SrcRD && DestRD); |
789 | | |
790 | 0 | if (SrcRD->isEffectivelyFinal()) { |
791 | 0 | assert(!SrcRD->isDerivedFrom(DestRD) && |
792 | 0 | "upcasts should not use CK_Dynamic"); |
793 | 0 | return true; |
794 | 0 | } |
795 | | |
796 | 0 | if (DestRD->isEffectivelyFinal() && !DestRD->isDerivedFrom(SrcRD)) |
797 | 0 | return true; |
798 | | |
799 | 0 | return false; |
800 | 0 | } |
801 | | |
802 | | CXXReinterpretCastExpr * |
803 | | CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T, |
804 | | ExprValueKind VK, CastKind K, Expr *Op, |
805 | | const CXXCastPath *BasePath, |
806 | | TypeSourceInfo *WrittenTy, SourceLocation L, |
807 | | SourceLocation RParenLoc, |
808 | 0 | SourceRange AngleBrackets) { |
809 | 0 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
810 | 0 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
811 | 0 | auto *E = |
812 | 0 | new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, |
813 | 0 | RParenLoc, AngleBrackets); |
814 | 0 | if (PathSize) |
815 | 0 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
816 | 0 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
817 | 0 | return E; |
818 | 0 | } |
819 | | |
820 | | CXXReinterpretCastExpr * |
821 | 0 | CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) { |
822 | 0 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
823 | 0 | return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize); |
824 | 0 | } |
825 | | |
826 | | CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T, |
827 | | ExprValueKind VK, Expr *Op, |
828 | | TypeSourceInfo *WrittenTy, |
829 | | SourceLocation L, |
830 | | SourceLocation RParenLoc, |
831 | 0 | SourceRange AngleBrackets) { |
832 | 0 | return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets); |
833 | 0 | } |
834 | | |
835 | 0 | CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) { |
836 | 0 | return new (C) CXXConstCastExpr(EmptyShell()); |
837 | 0 | } |
838 | | |
839 | | CXXAddrspaceCastExpr * |
840 | | CXXAddrspaceCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK, |
841 | | CastKind K, Expr *Op, TypeSourceInfo *WrittenTy, |
842 | | SourceLocation L, SourceLocation RParenLoc, |
843 | 0 | SourceRange AngleBrackets) { |
844 | 0 | return new (C) CXXAddrspaceCastExpr(T, VK, K, Op, WrittenTy, L, RParenLoc, |
845 | 0 | AngleBrackets); |
846 | 0 | } |
847 | | |
848 | 0 | CXXAddrspaceCastExpr *CXXAddrspaceCastExpr::CreateEmpty(const ASTContext &C) { |
849 | 0 | return new (C) CXXAddrspaceCastExpr(EmptyShell()); |
850 | 0 | } |
851 | | |
852 | | CXXFunctionalCastExpr *CXXFunctionalCastExpr::Create( |
853 | | const ASTContext &C, QualType T, ExprValueKind VK, TypeSourceInfo *Written, |
854 | | CastKind K, Expr *Op, const CXXCastPath *BasePath, FPOptionsOverride FPO, |
855 | 0 | SourceLocation L, SourceLocation R) { |
856 | 0 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
857 | 0 | void *Buffer = |
858 | 0 | C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>( |
859 | 0 | PathSize, FPO.requiresTrailingStorage())); |
860 | 0 | auto *E = new (Buffer) |
861 | 0 | CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, FPO, L, R); |
862 | 0 | if (PathSize) |
863 | 0 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
864 | 0 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
865 | 0 | return E; |
866 | 0 | } |
867 | | |
868 | | CXXFunctionalCastExpr *CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, |
869 | | unsigned PathSize, |
870 | 0 | bool HasFPFeatures) { |
871 | 0 | void *Buffer = |
872 | 0 | C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *, FPOptionsOverride>( |
873 | 0 | PathSize, HasFPFeatures)); |
874 | 0 | return new (Buffer) |
875 | 0 | CXXFunctionalCastExpr(EmptyShell(), PathSize, HasFPFeatures); |
876 | 0 | } |
877 | | |
878 | 0 | SourceLocation CXXFunctionalCastExpr::getBeginLoc() const { |
879 | 0 | return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc(); |
880 | 0 | } |
881 | | |
882 | 0 | SourceLocation CXXFunctionalCastExpr::getEndLoc() const { |
883 | 0 | return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc(); |
884 | 0 | } |
885 | | |
886 | | UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args, |
887 | | QualType Ty, ExprValueKind VK, |
888 | | SourceLocation LitEndLoc, |
889 | | SourceLocation SuffixLoc, |
890 | | FPOptionsOverride FPFeatures) |
891 | | : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, |
892 | | LitEndLoc, FPFeatures, /*MinNumArgs=*/0, NotADL), |
893 | 0 | UDSuffixLoc(SuffixLoc) {} |
894 | | |
895 | | UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, bool HasFPFeatures, |
896 | | EmptyShell Empty) |
897 | | : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, |
898 | 0 | HasFPFeatures, Empty) {} |
899 | | |
900 | | UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn, |
901 | | ArrayRef<Expr *> Args, |
902 | | QualType Ty, ExprValueKind VK, |
903 | | SourceLocation LitEndLoc, |
904 | | SourceLocation SuffixLoc, |
905 | 0 | FPOptionsOverride FPFeatures) { |
906 | | // Allocate storage for the trailing objects of CallExpr. |
907 | 0 | unsigned NumArgs = Args.size(); |
908 | 0 | unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects( |
909 | 0 | /*NumPreArgs=*/0, NumArgs, FPFeatures.requiresTrailingStorage()); |
910 | 0 | void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects, |
911 | 0 | alignof(UserDefinedLiteral)); |
912 | 0 | return new (Mem) |
913 | 0 | UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc, FPFeatures); |
914 | 0 | } |
915 | | |
916 | | UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx, |
917 | | unsigned NumArgs, |
918 | | bool HasFPOptions, |
919 | 0 | EmptyShell Empty) { |
920 | | // Allocate storage for the trailing objects of CallExpr. |
921 | 0 | unsigned SizeOfTrailingObjects = |
922 | 0 | CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs, HasFPOptions); |
923 | 0 | void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects, |
924 | 0 | alignof(UserDefinedLiteral)); |
925 | 0 | return new (Mem) UserDefinedLiteral(NumArgs, HasFPOptions, Empty); |
926 | 0 | } |
927 | | |
928 | | UserDefinedLiteral::LiteralOperatorKind |
929 | 0 | UserDefinedLiteral::getLiteralOperatorKind() const { |
930 | 0 | if (getNumArgs() == 0) |
931 | 0 | return LOK_Template; |
932 | 0 | if (getNumArgs() == 2) |
933 | 0 | return LOK_String; |
934 | | |
935 | 0 | assert(getNumArgs() == 1 && "unexpected #args in literal operator call"); |
936 | 0 | QualType ParamTy = |
937 | 0 | cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType(); |
938 | 0 | if (ParamTy->isPointerType()) |
939 | 0 | return LOK_Raw; |
940 | 0 | if (ParamTy->isAnyCharacterType()) |
941 | 0 | return LOK_Character; |
942 | 0 | if (ParamTy->isIntegerType()) |
943 | 0 | return LOK_Integer; |
944 | 0 | if (ParamTy->isFloatingType()) |
945 | 0 | return LOK_Floating; |
946 | | |
947 | 0 | llvm_unreachable("unknown kind of literal operator"); |
948 | 0 | } |
949 | | |
950 | 0 | Expr *UserDefinedLiteral::getCookedLiteral() { |
951 | 0 | #ifndef NDEBUG |
952 | 0 | LiteralOperatorKind LOK = getLiteralOperatorKind(); |
953 | 0 | assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal"); |
954 | 0 | #endif |
955 | 0 | return getArg(0); |
956 | 0 | } |
957 | | |
958 | 0 | const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const { |
959 | 0 | return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier(); |
960 | 0 | } |
961 | | |
962 | | CXXDefaultArgExpr *CXXDefaultArgExpr::CreateEmpty(const ASTContext &C, |
963 | 0 | bool HasRewrittenInit) { |
964 | 0 | size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit); |
965 | 0 | auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr)); |
966 | 0 | return new (Mem) CXXDefaultArgExpr(EmptyShell(), HasRewrittenInit); |
967 | 0 | } |
968 | | |
969 | | CXXDefaultArgExpr *CXXDefaultArgExpr::Create(const ASTContext &C, |
970 | | SourceLocation Loc, |
971 | | ParmVarDecl *Param, |
972 | | Expr *RewrittenExpr, |
973 | 0 | DeclContext *UsedContext) { |
974 | 0 | size_t Size = totalSizeToAlloc<Expr *>(RewrittenExpr != nullptr); |
975 | 0 | auto *Mem = C.Allocate(Size, alignof(CXXDefaultArgExpr)); |
976 | 0 | return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, |
977 | 0 | RewrittenExpr, UsedContext); |
978 | 0 | } |
979 | | |
980 | 0 | Expr *CXXDefaultArgExpr::getExpr() { |
981 | 0 | return CXXDefaultArgExprBits.HasRewrittenInit ? getAdjustedRewrittenExpr() |
982 | 0 | : getParam()->getDefaultArg(); |
983 | 0 | } |
984 | | |
985 | 0 | Expr *CXXDefaultArgExpr::getAdjustedRewrittenExpr() { |
986 | 0 | assert(hasRewrittenInit() && |
987 | 0 | "expected this CXXDefaultArgExpr to have a rewritten init."); |
988 | 0 | Expr *Init = getRewrittenExpr(); |
989 | 0 | if (auto *E = dyn_cast_if_present<FullExpr>(Init)) |
990 | 0 | if (!isa<ConstantExpr>(E)) |
991 | 0 | return E->getSubExpr(); |
992 | 0 | return Init; |
993 | 0 | } |
994 | | |
995 | | CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, |
996 | | SourceLocation Loc, FieldDecl *Field, |
997 | | QualType Ty, DeclContext *UsedContext, |
998 | | Expr *RewrittenInitExpr) |
999 | | : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx), |
1000 | | Ty->isLValueReferenceType() ? VK_LValue |
1001 | | : Ty->isRValueReferenceType() ? VK_XValue |
1002 | | : VK_PRValue, |
1003 | | /*FIXME*/ OK_Ordinary), |
1004 | 0 | Field(Field), UsedContext(UsedContext) { |
1005 | 0 | CXXDefaultInitExprBits.Loc = Loc; |
1006 | 0 | CXXDefaultInitExprBits.HasRewrittenInit = RewrittenInitExpr != nullptr; |
1007 | |
|
1008 | 0 | if (CXXDefaultInitExprBits.HasRewrittenInit) |
1009 | 0 | *getTrailingObjects<Expr *>() = RewrittenInitExpr; |
1010 | |
|
1011 | 0 | assert(Field->hasInClassInitializer()); |
1012 | | |
1013 | 0 | setDependence(computeDependence(this)); |
1014 | 0 | } |
1015 | | |
1016 | | CXXDefaultInitExpr *CXXDefaultInitExpr::CreateEmpty(const ASTContext &C, |
1017 | 0 | bool HasRewrittenInit) { |
1018 | 0 | size_t Size = totalSizeToAlloc<Expr *>(HasRewrittenInit); |
1019 | 0 | auto *Mem = C.Allocate(Size, alignof(CXXDefaultInitExpr)); |
1020 | 0 | return new (Mem) CXXDefaultInitExpr(EmptyShell(), HasRewrittenInit); |
1021 | 0 | } |
1022 | | |
1023 | | CXXDefaultInitExpr *CXXDefaultInitExpr::Create(const ASTContext &Ctx, |
1024 | | SourceLocation Loc, |
1025 | | FieldDecl *Field, |
1026 | | DeclContext *UsedContext, |
1027 | 0 | Expr *RewrittenInitExpr) { |
1028 | |
|
1029 | 0 | size_t Size = totalSizeToAlloc<Expr *>(RewrittenInitExpr != nullptr); |
1030 | 0 | auto *Mem = Ctx.Allocate(Size, alignof(CXXDefaultInitExpr)); |
1031 | 0 | return new (Mem) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(), |
1032 | 0 | UsedContext, RewrittenInitExpr); |
1033 | 0 | } |
1034 | | |
1035 | 0 | Expr *CXXDefaultInitExpr::getExpr() { |
1036 | 0 | assert(Field->getInClassInitializer() && "initializer hasn't been parsed"); |
1037 | 0 | if (hasRewrittenInit()) |
1038 | 0 | return getRewrittenExpr(); |
1039 | | |
1040 | 0 | return Field->getInClassInitializer(); |
1041 | 0 | } |
1042 | | |
1043 | | CXXTemporary *CXXTemporary::Create(const ASTContext &C, |
1044 | 0 | const CXXDestructorDecl *Destructor) { |
1045 | 0 | return new (C) CXXTemporary(Destructor); |
1046 | 0 | } |
1047 | | |
1048 | | CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C, |
1049 | | CXXTemporary *Temp, |
1050 | 0 | Expr* SubExpr) { |
1051 | 0 | assert((SubExpr->getType()->isRecordType() || |
1052 | 0 | SubExpr->getType()->isArrayType()) && |
1053 | 0 | "Expression bound to a temporary must have record or array type!"); |
1054 | | |
1055 | 0 | return new (C) CXXBindTemporaryExpr(Temp, SubExpr); |
1056 | 0 | } |
1057 | | |
1058 | | CXXTemporaryObjectExpr::CXXTemporaryObjectExpr( |
1059 | | CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI, |
1060 | | ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange, |
1061 | | bool HadMultipleCandidates, bool ListInitialization, |
1062 | | bool StdInitListInitialization, bool ZeroInitialization) |
1063 | | : CXXConstructExpr( |
1064 | | CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(), |
1065 | | Cons, /* Elidable=*/false, Args, HadMultipleCandidates, |
1066 | | ListInitialization, StdInitListInitialization, ZeroInitialization, |
1067 | | CXXConstructionKind::Complete, ParenOrBraceRange), |
1068 | 0 | TSI(TSI) { |
1069 | 0 | setDependence(computeDependence(this)); |
1070 | 0 | } |
1071 | | |
1072 | | CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty, |
1073 | | unsigned NumArgs) |
1074 | 0 | : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {} |
1075 | | |
1076 | | CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create( |
1077 | | const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty, |
1078 | | TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange, |
1079 | | bool HadMultipleCandidates, bool ListInitialization, |
1080 | 0 | bool StdInitListInitialization, bool ZeroInitialization) { |
1081 | 0 | unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size()); |
1082 | 0 | void *Mem = |
1083 | 0 | Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects, |
1084 | 0 | alignof(CXXTemporaryObjectExpr)); |
1085 | 0 | return new (Mem) CXXTemporaryObjectExpr( |
1086 | 0 | Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates, |
1087 | 0 | ListInitialization, StdInitListInitialization, ZeroInitialization); |
1088 | 0 | } |
1089 | | |
1090 | | CXXTemporaryObjectExpr * |
1091 | 0 | CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) { |
1092 | 0 | unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs); |
1093 | 0 | void *Mem = |
1094 | 0 | Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects, |
1095 | 0 | alignof(CXXTemporaryObjectExpr)); |
1096 | 0 | return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs); |
1097 | 0 | } |
1098 | | |
1099 | 0 | SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const { |
1100 | 0 | return getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
1101 | 0 | } |
1102 | | |
1103 | 0 | SourceLocation CXXTemporaryObjectExpr::getEndLoc() const { |
1104 | 0 | SourceLocation Loc = getParenOrBraceRange().getEnd(); |
1105 | 0 | if (Loc.isInvalid() && getNumArgs()) |
1106 | 0 | Loc = getArg(getNumArgs() - 1)->getEndLoc(); |
1107 | 0 | return Loc; |
1108 | 0 | } |
1109 | | |
1110 | | CXXConstructExpr *CXXConstructExpr::Create( |
1111 | | const ASTContext &Ctx, QualType Ty, SourceLocation Loc, |
1112 | | CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args, |
1113 | | bool HadMultipleCandidates, bool ListInitialization, |
1114 | | bool StdInitListInitialization, bool ZeroInitialization, |
1115 | 0 | CXXConstructionKind ConstructKind, SourceRange ParenOrBraceRange) { |
1116 | 0 | unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size()); |
1117 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects, |
1118 | 0 | alignof(CXXConstructExpr)); |
1119 | 0 | return new (Mem) CXXConstructExpr( |
1120 | 0 | CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args, |
1121 | 0 | HadMultipleCandidates, ListInitialization, StdInitListInitialization, |
1122 | 0 | ZeroInitialization, ConstructKind, ParenOrBraceRange); |
1123 | 0 | } |
1124 | | |
1125 | | CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx, |
1126 | 0 | unsigned NumArgs) { |
1127 | 0 | unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs); |
1128 | 0 | void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects, |
1129 | 0 | alignof(CXXConstructExpr)); |
1130 | 0 | return new (Mem) |
1131 | 0 | CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs); |
1132 | 0 | } |
1133 | | |
1134 | | CXXConstructExpr::CXXConstructExpr( |
1135 | | StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor, |
1136 | | bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates, |
1137 | | bool ListInitialization, bool StdInitListInitialization, |
1138 | | bool ZeroInitialization, CXXConstructionKind ConstructKind, |
1139 | | SourceRange ParenOrBraceRange) |
1140 | | : Expr(SC, Ty, VK_PRValue, OK_Ordinary), Constructor(Ctor), |
1141 | 0 | ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) { |
1142 | 0 | CXXConstructExprBits.Elidable = Elidable; |
1143 | 0 | CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates; |
1144 | 0 | CXXConstructExprBits.ListInitialization = ListInitialization; |
1145 | 0 | CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization; |
1146 | 0 | CXXConstructExprBits.ZeroInitialization = ZeroInitialization; |
1147 | 0 | CXXConstructExprBits.ConstructionKind = llvm::to_underlying(ConstructKind); |
1148 | 0 | CXXConstructExprBits.IsImmediateEscalating = false; |
1149 | 0 | CXXConstructExprBits.Loc = Loc; |
1150 | |
|
1151 | 0 | Stmt **TrailingArgs = getTrailingArgs(); |
1152 | 0 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
1153 | 0 | assert(Args[I] && "NULL argument in CXXConstructExpr!"); |
1154 | 0 | TrailingArgs[I] = Args[I]; |
1155 | 0 | } |
1156 | | |
1157 | | // CXXTemporaryObjectExpr does this itself after setting its TypeSourceInfo. |
1158 | 0 | if (SC == CXXConstructExprClass) |
1159 | 0 | setDependence(computeDependence(this)); |
1160 | 0 | } |
1161 | | |
1162 | | CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty, |
1163 | | unsigned NumArgs) |
1164 | 0 | : Expr(SC, Empty), NumArgs(NumArgs) {} |
1165 | | |
1166 | | LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit, |
1167 | | LambdaCaptureKind Kind, ValueDecl *Var, |
1168 | | SourceLocation EllipsisLoc) |
1169 | 0 | : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) { |
1170 | 0 | unsigned Bits = 0; |
1171 | 0 | if (Implicit) |
1172 | 0 | Bits |= Capture_Implicit; |
1173 | |
|
1174 | 0 | switch (Kind) { |
1175 | 0 | case LCK_StarThis: |
1176 | 0 | Bits |= Capture_ByCopy; |
1177 | 0 | [[fallthrough]]; |
1178 | 0 | case LCK_This: |
1179 | 0 | assert(!Var && "'this' capture cannot have a variable!"); |
1180 | 0 | Bits |= Capture_This; |
1181 | 0 | break; |
1182 | | |
1183 | 0 | case LCK_ByCopy: |
1184 | 0 | Bits |= Capture_ByCopy; |
1185 | 0 | [[fallthrough]]; |
1186 | 0 | case LCK_ByRef: |
1187 | 0 | assert(Var && "capture must have a variable!"); |
1188 | 0 | break; |
1189 | 0 | case LCK_VLAType: |
1190 | 0 | assert(!Var && "VLA type capture cannot have a variable!"); |
1191 | 0 | break; |
1192 | 0 | } |
1193 | 0 | DeclAndBits.setInt(Bits); |
1194 | 0 | } |
1195 | | |
1196 | 0 | LambdaCaptureKind LambdaCapture::getCaptureKind() const { |
1197 | 0 | if (capturesVLAType()) |
1198 | 0 | return LCK_VLAType; |
1199 | 0 | bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy; |
1200 | 0 | if (capturesThis()) |
1201 | 0 | return CapByCopy ? LCK_StarThis : LCK_This; |
1202 | 0 | return CapByCopy ? LCK_ByCopy : LCK_ByRef; |
1203 | 0 | } |
1204 | | |
1205 | | LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange, |
1206 | | LambdaCaptureDefault CaptureDefault, |
1207 | | SourceLocation CaptureDefaultLoc, bool ExplicitParams, |
1208 | | bool ExplicitResultType, ArrayRef<Expr *> CaptureInits, |
1209 | | SourceLocation ClosingBrace, |
1210 | | bool ContainsUnexpandedParameterPack) |
1211 | | : Expr(LambdaExprClass, T, VK_PRValue, OK_Ordinary), |
1212 | | IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc), |
1213 | 0 | ClosingBrace(ClosingBrace) { |
1214 | 0 | LambdaExprBits.NumCaptures = CaptureInits.size(); |
1215 | 0 | LambdaExprBits.CaptureDefault = CaptureDefault; |
1216 | 0 | LambdaExprBits.ExplicitParams = ExplicitParams; |
1217 | 0 | LambdaExprBits.ExplicitResultType = ExplicitResultType; |
1218 | |
|
1219 | 0 | CXXRecordDecl *Class = getLambdaClass(); |
1220 | 0 | (void)Class; |
1221 | 0 | assert(capture_size() == Class->capture_size() && "Wrong number of captures"); |
1222 | 0 | assert(getCaptureDefault() == Class->getLambdaCaptureDefault()); |
1223 | | |
1224 | | // Copy initialization expressions for the non-static data members. |
1225 | 0 | Stmt **Stored = getStoredStmts(); |
1226 | 0 | for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I) |
1227 | 0 | *Stored++ = CaptureInits[I]; |
1228 | | |
1229 | | // Copy the body of the lambda. |
1230 | 0 | *Stored++ = getCallOperator()->getBody(); |
1231 | |
|
1232 | 0 | setDependence(computeDependence(this, ContainsUnexpandedParameterPack)); |
1233 | 0 | } |
1234 | | |
1235 | | LambdaExpr::LambdaExpr(EmptyShell Empty, unsigned NumCaptures) |
1236 | 0 | : Expr(LambdaExprClass, Empty) { |
1237 | 0 | LambdaExprBits.NumCaptures = NumCaptures; |
1238 | | |
1239 | | // Initially don't initialize the body of the LambdaExpr. The body will |
1240 | | // be lazily deserialized when needed. |
1241 | 0 | getStoredStmts()[NumCaptures] = nullptr; // Not one past the end. |
1242 | 0 | } |
1243 | | |
1244 | | LambdaExpr *LambdaExpr::Create(const ASTContext &Context, CXXRecordDecl *Class, |
1245 | | SourceRange IntroducerRange, |
1246 | | LambdaCaptureDefault CaptureDefault, |
1247 | | SourceLocation CaptureDefaultLoc, |
1248 | | bool ExplicitParams, bool ExplicitResultType, |
1249 | | ArrayRef<Expr *> CaptureInits, |
1250 | | SourceLocation ClosingBrace, |
1251 | 0 | bool ContainsUnexpandedParameterPack) { |
1252 | | // Determine the type of the expression (i.e., the type of the |
1253 | | // function object we're creating). |
1254 | 0 | QualType T = Context.getTypeDeclType(Class); |
1255 | |
|
1256 | 0 | unsigned Size = totalSizeToAlloc<Stmt *>(CaptureInits.size() + 1); |
1257 | 0 | void *Mem = Context.Allocate(Size); |
1258 | 0 | return new (Mem) |
1259 | 0 | LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc, |
1260 | 0 | ExplicitParams, ExplicitResultType, CaptureInits, ClosingBrace, |
1261 | 0 | ContainsUnexpandedParameterPack); |
1262 | 0 | } |
1263 | | |
1264 | | LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C, |
1265 | 0 | unsigned NumCaptures) { |
1266 | 0 | unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1); |
1267 | 0 | void *Mem = C.Allocate(Size); |
1268 | 0 | return new (Mem) LambdaExpr(EmptyShell(), NumCaptures); |
1269 | 0 | } |
1270 | | |
1271 | 0 | void LambdaExpr::initBodyIfNeeded() const { |
1272 | 0 | if (!getStoredStmts()[capture_size()]) { |
1273 | 0 | auto *This = const_cast<LambdaExpr *>(this); |
1274 | 0 | This->getStoredStmts()[capture_size()] = getCallOperator()->getBody(); |
1275 | 0 | } |
1276 | 0 | } |
1277 | | |
1278 | 0 | Stmt *LambdaExpr::getBody() const { |
1279 | 0 | initBodyIfNeeded(); |
1280 | 0 | return getStoredStmts()[capture_size()]; |
1281 | 0 | } |
1282 | | |
1283 | 0 | const CompoundStmt *LambdaExpr::getCompoundStmtBody() const { |
1284 | 0 | Stmt *Body = getBody(); |
1285 | 0 | if (const auto *CoroBody = dyn_cast<CoroutineBodyStmt>(Body)) |
1286 | 0 | return cast<CompoundStmt>(CoroBody->getBody()); |
1287 | 0 | return cast<CompoundStmt>(Body); |
1288 | 0 | } |
1289 | | |
1290 | 0 | bool LambdaExpr::isInitCapture(const LambdaCapture *C) const { |
1291 | 0 | return C->capturesVariable() && C->getCapturedVar()->isInitCapture() && |
1292 | 0 | getCallOperator() == C->getCapturedVar()->getDeclContext(); |
1293 | 0 | } |
1294 | | |
1295 | 0 | LambdaExpr::capture_iterator LambdaExpr::capture_begin() const { |
1296 | 0 | return getLambdaClass()->captures_begin(); |
1297 | 0 | } |
1298 | | |
1299 | 0 | LambdaExpr::capture_iterator LambdaExpr::capture_end() const { |
1300 | 0 | return getLambdaClass()->captures_end(); |
1301 | 0 | } |
1302 | | |
1303 | 0 | LambdaExpr::capture_range LambdaExpr::captures() const { |
1304 | 0 | return capture_range(capture_begin(), capture_end()); |
1305 | 0 | } |
1306 | | |
1307 | 0 | LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const { |
1308 | 0 | return capture_begin(); |
1309 | 0 | } |
1310 | | |
1311 | 0 | LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const { |
1312 | 0 | return capture_begin() + |
1313 | 0 | getLambdaClass()->getLambdaData().NumExplicitCaptures; |
1314 | 0 | } |
1315 | | |
1316 | 0 | LambdaExpr::capture_range LambdaExpr::explicit_captures() const { |
1317 | 0 | return capture_range(explicit_capture_begin(), explicit_capture_end()); |
1318 | 0 | } |
1319 | | |
1320 | 0 | LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const { |
1321 | 0 | return explicit_capture_end(); |
1322 | 0 | } |
1323 | | |
1324 | 0 | LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const { |
1325 | 0 | return capture_end(); |
1326 | 0 | } |
1327 | | |
1328 | 0 | LambdaExpr::capture_range LambdaExpr::implicit_captures() const { |
1329 | 0 | return capture_range(implicit_capture_begin(), implicit_capture_end()); |
1330 | 0 | } |
1331 | | |
1332 | 0 | CXXRecordDecl *LambdaExpr::getLambdaClass() const { |
1333 | 0 | return getType()->getAsCXXRecordDecl(); |
1334 | 0 | } |
1335 | | |
1336 | 0 | CXXMethodDecl *LambdaExpr::getCallOperator() const { |
1337 | 0 | CXXRecordDecl *Record = getLambdaClass(); |
1338 | 0 | return Record->getLambdaCallOperator(); |
1339 | 0 | } |
1340 | | |
1341 | 0 | FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const { |
1342 | 0 | CXXRecordDecl *Record = getLambdaClass(); |
1343 | 0 | return Record->getDependentLambdaCallOperator(); |
1344 | 0 | } |
1345 | | |
1346 | 0 | TemplateParameterList *LambdaExpr::getTemplateParameterList() const { |
1347 | 0 | CXXRecordDecl *Record = getLambdaClass(); |
1348 | 0 | return Record->getGenericLambdaTemplateParameterList(); |
1349 | 0 | } |
1350 | | |
1351 | 0 | ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const { |
1352 | 0 | const CXXRecordDecl *Record = getLambdaClass(); |
1353 | 0 | return Record->getLambdaExplicitTemplateParameters(); |
1354 | 0 | } |
1355 | | |
1356 | 0 | Expr *LambdaExpr::getTrailingRequiresClause() const { |
1357 | 0 | return getCallOperator()->getTrailingRequiresClause(); |
1358 | 0 | } |
1359 | | |
1360 | 0 | bool LambdaExpr::isMutable() const { return !getCallOperator()->isConst(); } |
1361 | | |
1362 | 0 | LambdaExpr::child_range LambdaExpr::children() { |
1363 | 0 | initBodyIfNeeded(); |
1364 | 0 | return child_range(getStoredStmts(), getStoredStmts() + capture_size() + 1); |
1365 | 0 | } |
1366 | | |
1367 | 0 | LambdaExpr::const_child_range LambdaExpr::children() const { |
1368 | 0 | initBodyIfNeeded(); |
1369 | 0 | return const_child_range(getStoredStmts(), |
1370 | 0 | getStoredStmts() + capture_size() + 1); |
1371 | 0 | } |
1372 | | |
1373 | | ExprWithCleanups::ExprWithCleanups(Expr *subexpr, |
1374 | | bool CleanupsHaveSideEffects, |
1375 | | ArrayRef<CleanupObject> objects) |
1376 | 0 | : FullExpr(ExprWithCleanupsClass, subexpr) { |
1377 | 0 | ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects; |
1378 | 0 | ExprWithCleanupsBits.NumObjects = objects.size(); |
1379 | 0 | for (unsigned i = 0, e = objects.size(); i != e; ++i) |
1380 | 0 | getTrailingObjects<CleanupObject>()[i] = objects[i]; |
1381 | 0 | } |
1382 | | |
1383 | | ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr, |
1384 | | bool CleanupsHaveSideEffects, |
1385 | 0 | ArrayRef<CleanupObject> objects) { |
1386 | 0 | void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()), |
1387 | 0 | alignof(ExprWithCleanups)); |
1388 | 0 | return new (buffer) |
1389 | 0 | ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects); |
1390 | 0 | } |
1391 | | |
1392 | | ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects) |
1393 | 0 | : FullExpr(ExprWithCleanupsClass, empty) { |
1394 | 0 | ExprWithCleanupsBits.NumObjects = numObjects; |
1395 | 0 | } |
1396 | | |
1397 | | ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, |
1398 | | EmptyShell empty, |
1399 | 0 | unsigned numObjects) { |
1400 | 0 | void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects), |
1401 | 0 | alignof(ExprWithCleanups)); |
1402 | 0 | return new (buffer) ExprWithCleanups(empty, numObjects); |
1403 | 0 | } |
1404 | | |
1405 | | CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr( |
1406 | | QualType T, TypeSourceInfo *TSI, SourceLocation LParenLoc, |
1407 | | ArrayRef<Expr *> Args, SourceLocation RParenLoc, bool IsListInit) |
1408 | | : Expr(CXXUnresolvedConstructExprClass, T, |
1409 | | (TSI->getType()->isLValueReferenceType() ? VK_LValue |
1410 | | : TSI->getType()->isRValueReferenceType() ? VK_XValue |
1411 | | : VK_PRValue), |
1412 | | OK_Ordinary), |
1413 | | TypeAndInitForm(TSI, IsListInit), LParenLoc(LParenLoc), |
1414 | 0 | RParenLoc(RParenLoc) { |
1415 | 0 | CXXUnresolvedConstructExprBits.NumArgs = Args.size(); |
1416 | 0 | auto **StoredArgs = getTrailingObjects<Expr *>(); |
1417 | 0 | for (unsigned I = 0; I != Args.size(); ++I) |
1418 | 0 | StoredArgs[I] = Args[I]; |
1419 | 0 | setDependence(computeDependence(this)); |
1420 | 0 | } |
1421 | | |
1422 | | CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create( |
1423 | | const ASTContext &Context, QualType T, TypeSourceInfo *TSI, |
1424 | | SourceLocation LParenLoc, ArrayRef<Expr *> Args, SourceLocation RParenLoc, |
1425 | 0 | bool IsListInit) { |
1426 | 0 | void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size())); |
1427 | 0 | return new (Mem) CXXUnresolvedConstructExpr(T, TSI, LParenLoc, Args, |
1428 | 0 | RParenLoc, IsListInit); |
1429 | 0 | } |
1430 | | |
1431 | | CXXUnresolvedConstructExpr * |
1432 | | CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context, |
1433 | 0 | unsigned NumArgs) { |
1434 | 0 | void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs)); |
1435 | 0 | return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs); |
1436 | 0 | } |
1437 | | |
1438 | 0 | SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const { |
1439 | 0 | return TypeAndInitForm.getPointer()->getTypeLoc().getBeginLoc(); |
1440 | 0 | } |
1441 | | |
1442 | | CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr( |
1443 | | const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow, |
1444 | | SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc, |
1445 | | SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope, |
1446 | | DeclarationNameInfo MemberNameInfo, |
1447 | | const TemplateArgumentListInfo *TemplateArgs) |
1448 | | : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue, |
1449 | | OK_Ordinary), |
1450 | | Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc), |
1451 | 2 | MemberNameInfo(MemberNameInfo) { |
1452 | 2 | CXXDependentScopeMemberExprBits.IsArrow = IsArrow; |
1453 | 2 | CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo = |
1454 | 2 | (TemplateArgs != nullptr) || TemplateKWLoc.isValid(); |
1455 | 2 | CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope = |
1456 | 2 | FirstQualifierFoundInScope != nullptr; |
1457 | 2 | CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc; |
1458 | | |
1459 | 2 | if (TemplateArgs) { |
1460 | 0 | auto Deps = TemplateArgumentDependence::None; |
1461 | 0 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
1462 | 0 | TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(), |
1463 | 0 | Deps); |
1464 | 2 | } else if (TemplateKWLoc.isValid()) { |
1465 | 0 | getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
1466 | 0 | TemplateKWLoc); |
1467 | 0 | } |
1468 | | |
1469 | 2 | if (hasFirstQualifierFoundInScope()) |
1470 | 0 | *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope; |
1471 | 2 | setDependence(computeDependence(this)); |
1472 | 2 | } |
1473 | | |
1474 | | CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr( |
1475 | | EmptyShell Empty, bool HasTemplateKWAndArgsInfo, |
1476 | | bool HasFirstQualifierFoundInScope) |
1477 | 0 | : Expr(CXXDependentScopeMemberExprClass, Empty) { |
1478 | 0 | CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo = |
1479 | 0 | HasTemplateKWAndArgsInfo; |
1480 | 0 | CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope = |
1481 | 0 | HasFirstQualifierFoundInScope; |
1482 | 0 | } |
1483 | | |
1484 | | CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create( |
1485 | | const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow, |
1486 | | SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc, |
1487 | | SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope, |
1488 | | DeclarationNameInfo MemberNameInfo, |
1489 | 2 | const TemplateArgumentListInfo *TemplateArgs) { |
1490 | 2 | bool HasTemplateKWAndArgsInfo = |
1491 | 2 | (TemplateArgs != nullptr) || TemplateKWLoc.isValid(); |
1492 | 2 | unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0; |
1493 | 2 | bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr; |
1494 | | |
1495 | 2 | unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo, |
1496 | 2 | TemplateArgumentLoc, NamedDecl *>( |
1497 | 2 | HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope); |
1498 | | |
1499 | 2 | void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr)); |
1500 | 2 | return new (Mem) CXXDependentScopeMemberExpr( |
1501 | 2 | Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc, |
1502 | 2 | FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs); |
1503 | 2 | } |
1504 | | |
1505 | | CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty( |
1506 | | const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo, |
1507 | 0 | unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) { |
1508 | 0 | assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); |
1509 | | |
1510 | 0 | unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo, |
1511 | 0 | TemplateArgumentLoc, NamedDecl *>( |
1512 | 0 | HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope); |
1513 | |
|
1514 | 0 | void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr)); |
1515 | 0 | return new (Mem) CXXDependentScopeMemberExpr( |
1516 | 0 | EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope); |
1517 | 0 | } |
1518 | | |
1519 | | CXXThisExpr *CXXThisExpr::Create(const ASTContext &Ctx, SourceLocation L, |
1520 | 0 | QualType Ty, bool IsImplicit) { |
1521 | 0 | return new (Ctx) CXXThisExpr(L, Ty, IsImplicit, |
1522 | 0 | Ctx.getLangOpts().HLSL ? VK_LValue : VK_PRValue); |
1523 | 0 | } |
1524 | | |
1525 | 0 | CXXThisExpr *CXXThisExpr::CreateEmpty(const ASTContext &Ctx) { |
1526 | 0 | return new (Ctx) CXXThisExpr(EmptyShell()); |
1527 | 0 | } |
1528 | | |
1529 | | static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin, |
1530 | 0 | UnresolvedSetIterator end) { |
1531 | 0 | do { |
1532 | 0 | NamedDecl *decl = *begin; |
1533 | 0 | if (isa<UnresolvedUsingValueDecl>(decl)) |
1534 | 0 | return false; |
1535 | | |
1536 | | // Unresolved member expressions should only contain methods and |
1537 | | // method templates. |
1538 | 0 | if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction()) |
1539 | 0 | ->isStatic()) |
1540 | 0 | return false; |
1541 | 0 | } while (++begin != end); |
1542 | | |
1543 | 0 | return true; |
1544 | 0 | } |
1545 | | |
1546 | | UnresolvedMemberExpr::UnresolvedMemberExpr( |
1547 | | const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base, |
1548 | | QualType BaseType, bool IsArrow, SourceLocation OperatorLoc, |
1549 | | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
1550 | | const DeclarationNameInfo &MemberNameInfo, |
1551 | | const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin, |
1552 | | UnresolvedSetIterator End) |
1553 | | : OverloadExpr( |
1554 | | UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc, |
1555 | | MemberNameInfo, TemplateArgs, Begin, End, |
1556 | | // Dependent |
1557 | | ((Base && Base->isTypeDependent()) || BaseType->isDependentType()), |
1558 | | ((Base && Base->isInstantiationDependent()) || |
1559 | | BaseType->isInstantiationDependentType()), |
1560 | | // Contains unexpanded parameter pack |
1561 | | ((Base && Base->containsUnexpandedParameterPack()) || |
1562 | | BaseType->containsUnexpandedParameterPack())), |
1563 | 0 | Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) { |
1564 | 0 | UnresolvedMemberExprBits.IsArrow = IsArrow; |
1565 | 0 | UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing; |
1566 | | |
1567 | | // Check whether all of the members are non-static member functions, |
1568 | | // and if so, mark give this bound-member type instead of overload type. |
1569 | 0 | if (hasOnlyNonStaticMemberFunctions(Begin, End)) |
1570 | 0 | setType(Context.BoundMemberTy); |
1571 | 0 | } |
1572 | | |
1573 | | UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty, |
1574 | | unsigned NumResults, |
1575 | | bool HasTemplateKWAndArgsInfo) |
1576 | | : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults, |
1577 | 0 | HasTemplateKWAndArgsInfo) {} |
1578 | | |
1579 | 0 | bool UnresolvedMemberExpr::isImplicitAccess() const { |
1580 | 0 | if (!Base) |
1581 | 0 | return true; |
1582 | | |
1583 | 0 | return cast<Expr>(Base)->isImplicitCXXThis(); |
1584 | 0 | } |
1585 | | |
1586 | | UnresolvedMemberExpr *UnresolvedMemberExpr::Create( |
1587 | | const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base, |
1588 | | QualType BaseType, bool IsArrow, SourceLocation OperatorLoc, |
1589 | | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
1590 | | const DeclarationNameInfo &MemberNameInfo, |
1591 | | const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin, |
1592 | 0 | UnresolvedSetIterator End) { |
1593 | 0 | unsigned NumResults = End - Begin; |
1594 | 0 | bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid(); |
1595 | 0 | unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0; |
1596 | 0 | unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo, |
1597 | 0 | TemplateArgumentLoc>( |
1598 | 0 | NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs); |
1599 | 0 | void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr)); |
1600 | 0 | return new (Mem) UnresolvedMemberExpr( |
1601 | 0 | Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, |
1602 | 0 | QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End); |
1603 | 0 | } |
1604 | | |
1605 | | UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty( |
1606 | | const ASTContext &Context, unsigned NumResults, |
1607 | 0 | bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) { |
1608 | 0 | assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); |
1609 | 0 | unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo, |
1610 | 0 | TemplateArgumentLoc>( |
1611 | 0 | NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs); |
1612 | 0 | void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr)); |
1613 | 0 | return new (Mem) |
1614 | 0 | UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo); |
1615 | 0 | } |
1616 | | |
1617 | 0 | CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() { |
1618 | | // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this. |
1619 | | |
1620 | | // If there was a nested name specifier, it names the naming class. |
1621 | | // It can't be dependent: after all, we were actually able to do the |
1622 | | // lookup. |
1623 | 0 | CXXRecordDecl *Record = nullptr; |
1624 | 0 | auto *NNS = getQualifier(); |
1625 | 0 | if (NNS && NNS->getKind() != NestedNameSpecifier::Super) { |
1626 | 0 | const Type *T = getQualifier()->getAsType(); |
1627 | 0 | assert(T && "qualifier in member expression does not name type"); |
1628 | 0 | Record = T->getAsCXXRecordDecl(); |
1629 | 0 | assert(Record && "qualifier in member expression does not name record"); |
1630 | 0 | } |
1631 | | // Otherwise the naming class must have been the base class. |
1632 | 0 | else { |
1633 | 0 | QualType BaseType = getBaseType().getNonReferenceType(); |
1634 | 0 | if (isArrow()) |
1635 | 0 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
1636 | |
|
1637 | 0 | Record = BaseType->getAsCXXRecordDecl(); |
1638 | 0 | assert(Record && "base of member expression does not name record"); |
1639 | 0 | } |
1640 | | |
1641 | 0 | return Record; |
1642 | 0 | } |
1643 | | |
1644 | | SizeOfPackExpr *SizeOfPackExpr::Create(ASTContext &Context, |
1645 | | SourceLocation OperatorLoc, |
1646 | | NamedDecl *Pack, SourceLocation PackLoc, |
1647 | | SourceLocation RParenLoc, |
1648 | | std::optional<unsigned> Length, |
1649 | 0 | ArrayRef<TemplateArgument> PartialArgs) { |
1650 | 0 | void *Storage = |
1651 | 0 | Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size())); |
1652 | 0 | return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack, |
1653 | 0 | PackLoc, RParenLoc, Length, PartialArgs); |
1654 | 0 | } |
1655 | | |
1656 | | SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context, |
1657 | 0 | unsigned NumPartialArgs) { |
1658 | 0 | void *Storage = |
1659 | 0 | Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs)); |
1660 | 0 | return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs); |
1661 | 0 | } |
1662 | | |
1663 | 0 | NonTypeTemplateParmDecl *SubstNonTypeTemplateParmExpr::getParameter() const { |
1664 | 0 | return cast<NonTypeTemplateParmDecl>( |
1665 | 0 | getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]); |
1666 | 0 | } |
1667 | | |
1668 | | QualType SubstNonTypeTemplateParmExpr::getParameterType( |
1669 | 0 | const ASTContext &Context) const { |
1670 | | // Note that, for a class type NTTP, we will have an lvalue of type 'const |
1671 | | // T', so we can't just compute this from the type and value category. |
1672 | 0 | if (isReferenceParameter()) |
1673 | 0 | return Context.getLValueReferenceType(getType()); |
1674 | 0 | return getType().getUnqualifiedType(); |
1675 | 0 | } |
1676 | | |
1677 | | SubstNonTypeTemplateParmPackExpr::SubstNonTypeTemplateParmPackExpr( |
1678 | | QualType T, ExprValueKind ValueKind, SourceLocation NameLoc, |
1679 | | const TemplateArgument &ArgPack, Decl *AssociatedDecl, unsigned Index) |
1680 | | : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary), |
1681 | | AssociatedDecl(AssociatedDecl), Arguments(ArgPack.pack_begin()), |
1682 | 0 | NumArguments(ArgPack.pack_size()), Index(Index), NameLoc(NameLoc) { |
1683 | 0 | assert(AssociatedDecl != nullptr); |
1684 | 0 | setDependence(ExprDependence::TypeValueInstantiation | |
1685 | 0 | ExprDependence::UnexpandedPack); |
1686 | 0 | } |
1687 | | |
1688 | | NonTypeTemplateParmDecl * |
1689 | 0 | SubstNonTypeTemplateParmPackExpr::getParameterPack() const { |
1690 | 0 | return cast<NonTypeTemplateParmDecl>( |
1691 | 0 | getReplacedTemplateParameterList(getAssociatedDecl())->asArray()[Index]); |
1692 | 0 | } |
1693 | | |
1694 | 0 | TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const { |
1695 | 0 | return TemplateArgument(llvm::ArrayRef(Arguments, NumArguments)); |
1696 | 0 | } |
1697 | | |
1698 | | FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack, |
1699 | | SourceLocation NameLoc, |
1700 | | unsigned NumParams, |
1701 | | VarDecl *const *Params) |
1702 | | : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary), |
1703 | 0 | ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) { |
1704 | 0 | if (Params) |
1705 | 0 | std::uninitialized_copy(Params, Params + NumParams, |
1706 | 0 | getTrailingObjects<VarDecl *>()); |
1707 | 0 | setDependence(ExprDependence::TypeValueInstantiation | |
1708 | 0 | ExprDependence::UnexpandedPack); |
1709 | 0 | } |
1710 | | |
1711 | | FunctionParmPackExpr * |
1712 | | FunctionParmPackExpr::Create(const ASTContext &Context, QualType T, |
1713 | | VarDecl *ParamPack, SourceLocation NameLoc, |
1714 | 0 | ArrayRef<VarDecl *> Params) { |
1715 | 0 | return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size()))) |
1716 | 0 | FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data()); |
1717 | 0 | } |
1718 | | |
1719 | | FunctionParmPackExpr * |
1720 | | FunctionParmPackExpr::CreateEmpty(const ASTContext &Context, |
1721 | 0 | unsigned NumParams) { |
1722 | 0 | return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams))) |
1723 | 0 | FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr); |
1724 | 0 | } |
1725 | | |
1726 | | MaterializeTemporaryExpr::MaterializeTemporaryExpr( |
1727 | | QualType T, Expr *Temporary, bool BoundToLvalueReference, |
1728 | | LifetimeExtendedTemporaryDecl *MTD) |
1729 | | : Expr(MaterializeTemporaryExprClass, T, |
1730 | 0 | BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary) { |
1731 | 0 | if (MTD) { |
1732 | 0 | State = MTD; |
1733 | 0 | MTD->ExprWithTemporary = Temporary; |
1734 | 0 | return; |
1735 | 0 | } |
1736 | 0 | State = Temporary; |
1737 | 0 | setDependence(computeDependence(this)); |
1738 | 0 | } |
1739 | | |
1740 | | void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy, |
1741 | 0 | unsigned ManglingNumber) { |
1742 | | // We only need extra state if we have to remember more than just the Stmt. |
1743 | 0 | if (!ExtendedBy) |
1744 | 0 | return; |
1745 | | |
1746 | | // We may need to allocate extra storage for the mangling number and the |
1747 | | // extended-by ValueDecl. |
1748 | 0 | if (!State.is<LifetimeExtendedTemporaryDecl *>()) |
1749 | 0 | State = LifetimeExtendedTemporaryDecl::Create( |
1750 | 0 | cast<Expr>(State.get<Stmt *>()), ExtendedBy, ManglingNumber); |
1751 | |
|
1752 | 0 | auto ES = State.get<LifetimeExtendedTemporaryDecl *>(); |
1753 | 0 | ES->ExtendingDecl = ExtendedBy; |
1754 | 0 | ES->ManglingNumber = ManglingNumber; |
1755 | 0 | } |
1756 | | |
1757 | | bool MaterializeTemporaryExpr::isUsableInConstantExpressions( |
1758 | 0 | const ASTContext &Context) const { |
1759 | | // C++20 [expr.const]p4: |
1760 | | // An object or reference is usable in constant expressions if it is [...] |
1761 | | // a temporary object of non-volatile const-qualified literal type |
1762 | | // whose lifetime is extended to that of a variable that is usable |
1763 | | // in constant expressions |
1764 | 0 | auto *VD = dyn_cast_or_null<VarDecl>(getExtendingDecl()); |
1765 | 0 | return VD && getType().isConstant(Context) && |
1766 | 0 | !getType().isVolatileQualified() && |
1767 | 0 | getType()->isLiteralType(Context) && |
1768 | 0 | VD->isUsableInConstantExpressions(Context); |
1769 | 0 | } |
1770 | | |
1771 | | TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, |
1772 | | ArrayRef<TypeSourceInfo *> Args, |
1773 | | SourceLocation RParenLoc, bool Value) |
1774 | | : Expr(TypeTraitExprClass, T, VK_PRValue, OK_Ordinary), Loc(Loc), |
1775 | 0 | RParenLoc(RParenLoc) { |
1776 | 0 | assert(Kind <= TT_Last && "invalid enum value!"); |
1777 | 0 | TypeTraitExprBits.Kind = Kind; |
1778 | 0 | assert(static_cast<unsigned>(Kind) == TypeTraitExprBits.Kind && |
1779 | 0 | "TypeTraitExprBits.Kind overflow!"); |
1780 | 0 | TypeTraitExprBits.Value = Value; |
1781 | 0 | TypeTraitExprBits.NumArgs = Args.size(); |
1782 | 0 | assert(Args.size() == TypeTraitExprBits.NumArgs && |
1783 | 0 | "TypeTraitExprBits.NumArgs overflow!"); |
1784 | | |
1785 | 0 | auto **ToArgs = getTrailingObjects<TypeSourceInfo *>(); |
1786 | 0 | for (unsigned I = 0, N = Args.size(); I != N; ++I) |
1787 | 0 | ToArgs[I] = Args[I]; |
1788 | |
|
1789 | 0 | setDependence(computeDependence(this)); |
1790 | 0 | } |
1791 | | |
1792 | | TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T, |
1793 | | SourceLocation Loc, |
1794 | | TypeTrait Kind, |
1795 | | ArrayRef<TypeSourceInfo *> Args, |
1796 | | SourceLocation RParenLoc, |
1797 | 0 | bool Value) { |
1798 | 0 | void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size())); |
1799 | 0 | return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value); |
1800 | 0 | } |
1801 | | |
1802 | | TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C, |
1803 | 0 | unsigned NumArgs) { |
1804 | 0 | void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs)); |
1805 | 0 | return new (Mem) TypeTraitExpr(EmptyShell()); |
1806 | 0 | } |
1807 | | |
1808 | | CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config, |
1809 | | ArrayRef<Expr *> Args, QualType Ty, |
1810 | | ExprValueKind VK, SourceLocation RP, |
1811 | | FPOptionsOverride FPFeatures, |
1812 | | unsigned MinNumArgs) |
1813 | | : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK, |
1814 | 0 | RP, FPFeatures, MinNumArgs, NotADL) {} |
1815 | | |
1816 | | CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, bool HasFPFeatures, |
1817 | | EmptyShell Empty) |
1818 | | : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs, |
1819 | 0 | HasFPFeatures, Empty) {} |
1820 | | |
1821 | | CUDAKernelCallExpr * |
1822 | | CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config, |
1823 | | ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, |
1824 | | SourceLocation RP, FPOptionsOverride FPFeatures, |
1825 | 0 | unsigned MinNumArgs) { |
1826 | | // Allocate storage for the trailing objects of CallExpr. |
1827 | 0 | unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs); |
1828 | 0 | unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects( |
1829 | 0 | /*NumPreArgs=*/END_PREARG, NumArgs, FPFeatures.requiresTrailingStorage()); |
1830 | 0 | void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects, |
1831 | 0 | alignof(CUDAKernelCallExpr)); |
1832 | 0 | return new (Mem) |
1833 | 0 | CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, FPFeatures, MinNumArgs); |
1834 | 0 | } |
1835 | | |
1836 | | CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx, |
1837 | | unsigned NumArgs, |
1838 | | bool HasFPFeatures, |
1839 | 0 | EmptyShell Empty) { |
1840 | | // Allocate storage for the trailing objects of CallExpr. |
1841 | 0 | unsigned SizeOfTrailingObjects = CallExpr::sizeOfTrailingObjects( |
1842 | 0 | /*NumPreArgs=*/END_PREARG, NumArgs, HasFPFeatures); |
1843 | 0 | void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects, |
1844 | 0 | alignof(CUDAKernelCallExpr)); |
1845 | 0 | return new (Mem) CUDAKernelCallExpr(NumArgs, HasFPFeatures, Empty); |
1846 | 0 | } |
1847 | | |
1848 | | CXXParenListInitExpr * |
1849 | | CXXParenListInitExpr::Create(ASTContext &C, ArrayRef<Expr *> Args, QualType T, |
1850 | | unsigned NumUserSpecifiedExprs, |
1851 | | SourceLocation InitLoc, SourceLocation LParenLoc, |
1852 | 0 | SourceLocation RParenLoc) { |
1853 | 0 | void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size())); |
1854 | 0 | return new (Mem) CXXParenListInitExpr(Args, T, NumUserSpecifiedExprs, InitLoc, |
1855 | 0 | LParenLoc, RParenLoc); |
1856 | 0 | } |
1857 | | |
1858 | | CXXParenListInitExpr *CXXParenListInitExpr::CreateEmpty(ASTContext &C, |
1859 | | unsigned NumExprs, |
1860 | 0 | EmptyShell Empty) { |
1861 | 0 | void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumExprs), |
1862 | 0 | alignof(CXXParenListInitExpr)); |
1863 | 0 | return new (Mem) CXXParenListInitExpr(Empty, NumExprs); |
1864 | 0 | } |