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