1 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the subclesses of Expr class declared in ExprCXX.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Basic/IdentifierTable.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/TypeLoc.h"
20 using namespace clang;
21
22
23 //===----------------------------------------------------------------------===//
24 // Child Iterators for iterating over subexpressions/substatements
25 //===----------------------------------------------------------------------===//
26
isPotentiallyEvaluated() const27 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
28 if (isTypeOperand())
29 return false;
30
31 // C++11 [expr.typeid]p3:
32 // When typeid is applied to an expression other than a glvalue of
33 // polymorphic class type, [...] the expression is an unevaluated operand.
34 const Expr *E = getExprOperand();
35 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
36 if (RD->isPolymorphic() && E->isGLValue())
37 return true;
38
39 return false;
40 }
41
getTypeOperand() const42 QualType CXXTypeidExpr::getTypeOperand() const {
43 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
44 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
45 .getUnqualifiedType();
46 }
47
getTypeOperand() const48 QualType CXXUuidofExpr::getTypeOperand() const {
49 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
50 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
51 .getUnqualifiedType();
52 }
53
54 // CXXScalarValueInitExpr
getSourceRange() const55 SourceRange CXXScalarValueInitExpr::getSourceRange() const {
56 SourceLocation Start = RParenLoc;
57 if (TypeInfo)
58 Start = TypeInfo->getTypeLoc().getBeginLoc();
59 return SourceRange(Start, RParenLoc);
60 }
61
62 // CXXNewExpr
CXXNewExpr(ASTContext & C,bool globalNew,FunctionDecl * operatorNew,FunctionDecl * operatorDelete,bool usualArrayDeleteWantsSize,ArrayRef<Expr * > placementArgs,SourceRange typeIdParens,Expr * arraySize,InitializationStyle initializationStyle,Expr * initializer,QualType ty,TypeSourceInfo * allocatedTypeInfo,SourceLocation startLoc,SourceRange directInitRange)63 CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
64 FunctionDecl *operatorDelete,
65 bool usualArrayDeleteWantsSize,
66 ArrayRef<Expr*> placementArgs,
67 SourceRange typeIdParens, Expr *arraySize,
68 InitializationStyle initializationStyle,
69 Expr *initializer, QualType ty,
70 TypeSourceInfo *allocatedTypeInfo,
71 SourceLocation startLoc, SourceRange directInitRange)
72 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
73 ty->isDependentType(), ty->isDependentType(),
74 ty->isInstantiationDependentType(),
75 ty->containsUnexpandedParameterPack()),
76 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
77 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
78 StartLoc(startLoc), DirectInitRange(directInitRange),
79 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
80 assert((initializer != 0 || initializationStyle == NoInit) &&
81 "Only NoInit can have no initializer.");
82 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
83 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
84 unsigned i = 0;
85 if (Array) {
86 if (arraySize->isInstantiationDependent())
87 ExprBits.InstantiationDependent = true;
88
89 if (arraySize->containsUnexpandedParameterPack())
90 ExprBits.ContainsUnexpandedParameterPack = true;
91
92 SubExprs[i++] = arraySize;
93 }
94
95 if (initializer) {
96 if (initializer->isInstantiationDependent())
97 ExprBits.InstantiationDependent = true;
98
99 if (initializer->containsUnexpandedParameterPack())
100 ExprBits.ContainsUnexpandedParameterPack = true;
101
102 SubExprs[i++] = initializer;
103 }
104
105 for (unsigned j = 0; j != placementArgs.size(); ++j) {
106 if (placementArgs[j]->isInstantiationDependent())
107 ExprBits.InstantiationDependent = true;
108 if (placementArgs[j]->containsUnexpandedParameterPack())
109 ExprBits.ContainsUnexpandedParameterPack = true;
110
111 SubExprs[i++] = placementArgs[j];
112 }
113 }
114
AllocateArgsArray(ASTContext & C,bool isArray,unsigned numPlaceArgs,bool hasInitializer)115 void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
116 unsigned numPlaceArgs, bool hasInitializer){
117 assert(SubExprs == 0 && "SubExprs already allocated");
118 Array = isArray;
119 NumPlacementArgs = numPlaceArgs;
120
121 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
122 SubExprs = new (C) Stmt*[TotalSize];
123 }
124
shouldNullCheckAllocation(ASTContext & Ctx) const125 bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
126 return getOperatorNew()->getType()->
127 castAs<FunctionProtoType>()->isNothrow(Ctx);
128 }
129
getEndLoc() const130 SourceLocation CXXNewExpr::getEndLoc() const {
131 switch (getInitializationStyle()) {
132 case NoInit:
133 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
134 case CallInit:
135 return DirectInitRange.getEnd();
136 case ListInit:
137 return getInitializer()->getSourceRange().getEnd();
138 }
139 llvm_unreachable("bogus initialization style");
140 }
141
142 // CXXDeleteExpr
getDestroyedType() const143 QualType CXXDeleteExpr::getDestroyedType() const {
144 const Expr *Arg = getArgument();
145 // The type-to-delete may not be a pointer if it's a dependent type.
146 const QualType ArgType = Arg->getType();
147
148 if (ArgType->isDependentType() && !ArgType->isPointerType())
149 return QualType();
150
151 return ArgType->getAs<PointerType>()->getPointeeType();
152 }
153
154 // CXXPseudoDestructorExpr
PseudoDestructorTypeStorage(TypeSourceInfo * Info)155 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
156 : Type(Info)
157 {
158 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
159 }
160
CXXPseudoDestructorExpr(ASTContext & Context,Expr * Base,bool isArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,TypeSourceInfo * ScopeType,SourceLocation ColonColonLoc,SourceLocation TildeLoc,PseudoDestructorTypeStorage DestroyedType)161 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
162 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
163 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
164 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
165 PseudoDestructorTypeStorage DestroyedType)
166 : Expr(CXXPseudoDestructorExprClass,
167 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
168 FunctionProtoType::ExtProtoInfo())),
169 VK_RValue, OK_Ordinary,
170 /*isTypeDependent=*/(Base->isTypeDependent() ||
171 (DestroyedType.getTypeSourceInfo() &&
172 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
173 /*isValueDependent=*/Base->isValueDependent(),
174 (Base->isInstantiationDependent() ||
175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
177 (ScopeType &&
178 ScopeType->getType()->isInstantiationDependentType()) ||
179 (DestroyedType.getTypeSourceInfo() &&
180 DestroyedType.getTypeSourceInfo()->getType()
181 ->isInstantiationDependentType())),
182 // ContainsUnexpandedParameterPack
183 (Base->containsUnexpandedParameterPack() ||
184 (QualifierLoc &&
185 QualifierLoc.getNestedNameSpecifier()
186 ->containsUnexpandedParameterPack()) ||
187 (ScopeType &&
188 ScopeType->getType()->containsUnexpandedParameterPack()) ||
189 (DestroyedType.getTypeSourceInfo() &&
190 DestroyedType.getTypeSourceInfo()->getType()
191 ->containsUnexpandedParameterPack()))),
192 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
193 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
194 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
195 DestroyedType(DestroyedType) { }
196
getDestroyedType() const197 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
198 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
199 return TInfo->getType();
200
201 return QualType();
202 }
203
getSourceRange() const204 SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
205 SourceLocation End = DestroyedType.getLocation();
206 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
207 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
208 return SourceRange(Base->getLocStart(), End);
209 }
210
211 // UnresolvedLookupExpr
212 UnresolvedLookupExpr *
Create(ASTContext & C,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool ADL,const TemplateArgumentListInfo * Args,UnresolvedSetIterator Begin,UnresolvedSetIterator End)213 UnresolvedLookupExpr::Create(ASTContext &C,
214 CXXRecordDecl *NamingClass,
215 NestedNameSpecifierLoc QualifierLoc,
216 SourceLocation TemplateKWLoc,
217 const DeclarationNameInfo &NameInfo,
218 bool ADL,
219 const TemplateArgumentListInfo *Args,
220 UnresolvedSetIterator Begin,
221 UnresolvedSetIterator End)
222 {
223 assert(Args || TemplateKWLoc.isValid());
224 unsigned num_args = Args ? Args->size() : 0;
225 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
226 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
227 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
228 TemplateKWLoc, NameInfo,
229 ADL, /*Overload*/ true, Args,
230 Begin, End, /*StdIsAssociated=*/false);
231 }
232
233 UnresolvedLookupExpr *
CreateEmpty(ASTContext & C,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)234 UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
235 bool HasTemplateKWAndArgsInfo,
236 unsigned NumTemplateArgs) {
237 std::size_t size = sizeof(UnresolvedLookupExpr);
238 if (HasTemplateKWAndArgsInfo)
239 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
240
241 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
242 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
243 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
244 return E;
245 }
246
OverloadExpr(StmtClass K,ASTContext & C,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent,bool KnownContainsUnexpandedParameterPack)247 OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
248 NestedNameSpecifierLoc QualifierLoc,
249 SourceLocation TemplateKWLoc,
250 const DeclarationNameInfo &NameInfo,
251 const TemplateArgumentListInfo *TemplateArgs,
252 UnresolvedSetIterator Begin,
253 UnresolvedSetIterator End,
254 bool KnownDependent,
255 bool KnownInstantiationDependent,
256 bool KnownContainsUnexpandedParameterPack)
257 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
258 KnownDependent,
259 (KnownInstantiationDependent ||
260 NameInfo.isInstantiationDependent() ||
261 (QualifierLoc &&
262 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
263 (KnownContainsUnexpandedParameterPack ||
264 NameInfo.containsUnexpandedParameterPack() ||
265 (QualifierLoc &&
266 QualifierLoc.getNestedNameSpecifier()
267 ->containsUnexpandedParameterPack()))),
268 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
269 Results(0), NumResults(End - Begin),
270 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
271 {
272 NumResults = End - Begin;
273 if (NumResults) {
274 // Determine whether this expression is type-dependent.
275 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
276 if ((*I)->getDeclContext()->isDependentContext() ||
277 isa<UnresolvedUsingValueDecl>(*I)) {
278 ExprBits.TypeDependent = true;
279 ExprBits.ValueDependent = true;
280 ExprBits.InstantiationDependent = true;
281 }
282 }
283
284 Results = static_cast<DeclAccessPair *>(
285 C.Allocate(sizeof(DeclAccessPair) * NumResults,
286 llvm::alignOf<DeclAccessPair>()));
287 memcpy(Results, &*Begin.getIterator(),
288 NumResults * sizeof(DeclAccessPair));
289 }
290
291 // If we have explicit template arguments, check for dependent
292 // template arguments and whether they contain any unexpanded pack
293 // expansions.
294 if (TemplateArgs) {
295 bool Dependent = false;
296 bool InstantiationDependent = false;
297 bool ContainsUnexpandedParameterPack = false;
298 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
299 Dependent,
300 InstantiationDependent,
301 ContainsUnexpandedParameterPack);
302
303 if (Dependent) {
304 ExprBits.TypeDependent = true;
305 ExprBits.ValueDependent = true;
306 }
307 if (InstantiationDependent)
308 ExprBits.InstantiationDependent = true;
309 if (ContainsUnexpandedParameterPack)
310 ExprBits.ContainsUnexpandedParameterPack = true;
311 } else if (TemplateKWLoc.isValid()) {
312 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
313 }
314
315 if (isTypeDependent())
316 setType(C.DependentTy);
317 }
318
initializeResults(ASTContext & C,UnresolvedSetIterator Begin,UnresolvedSetIterator End)319 void OverloadExpr::initializeResults(ASTContext &C,
320 UnresolvedSetIterator Begin,
321 UnresolvedSetIterator End) {
322 assert(Results == 0 && "Results already initialized!");
323 NumResults = End - Begin;
324 if (NumResults) {
325 Results = static_cast<DeclAccessPair *>(
326 C.Allocate(sizeof(DeclAccessPair) * NumResults,
327
328 llvm::alignOf<DeclAccessPair>()));
329 memcpy(Results, &*Begin.getIterator(),
330 NumResults * sizeof(DeclAccessPair));
331 }
332 }
333
getNamingClass() const334 CXXRecordDecl *OverloadExpr::getNamingClass() const {
335 if (isa<UnresolvedLookupExpr>(this))
336 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
337 else
338 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
339 }
340
341 // DependentScopeDeclRefExpr
DependentScopeDeclRefExpr(QualType T,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)342 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
343 NestedNameSpecifierLoc QualifierLoc,
344 SourceLocation TemplateKWLoc,
345 const DeclarationNameInfo &NameInfo,
346 const TemplateArgumentListInfo *Args)
347 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
348 true, true,
349 (NameInfo.isInstantiationDependent() ||
350 (QualifierLoc &&
351 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
352 (NameInfo.containsUnexpandedParameterPack() ||
353 (QualifierLoc &&
354 QualifierLoc.getNestedNameSpecifier()
355 ->containsUnexpandedParameterPack()))),
356 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
357 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
358 {
359 if (Args) {
360 bool Dependent = true;
361 bool InstantiationDependent = true;
362 bool ContainsUnexpandedParameterPack
363 = ExprBits.ContainsUnexpandedParameterPack;
364 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
365 Dependent,
366 InstantiationDependent,
367 ContainsUnexpandedParameterPack);
368 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
369 } else if (TemplateKWLoc.isValid()) {
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
371 }
372 }
373
374 DependentScopeDeclRefExpr *
Create(ASTContext & C,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)375 DependentScopeDeclRefExpr::Create(ASTContext &C,
376 NestedNameSpecifierLoc QualifierLoc,
377 SourceLocation TemplateKWLoc,
378 const DeclarationNameInfo &NameInfo,
379 const TemplateArgumentListInfo *Args) {
380 std::size_t size = sizeof(DependentScopeDeclRefExpr);
381 if (Args)
382 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
383 else if (TemplateKWLoc.isValid())
384 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
385 void *Mem = C.Allocate(size);
386 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
387 TemplateKWLoc, NameInfo, Args);
388 }
389
390 DependentScopeDeclRefExpr *
CreateEmpty(ASTContext & C,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)391 DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
392 bool HasTemplateKWAndArgsInfo,
393 unsigned NumTemplateArgs) {
394 std::size_t size = sizeof(DependentScopeDeclRefExpr);
395 if (HasTemplateKWAndArgsInfo)
396 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
397 void *Mem = C.Allocate(size);
398 DependentScopeDeclRefExpr *E
399 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
400 SourceLocation(),
401 DeclarationNameInfo(), 0);
402 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
403 return E;
404 }
405
getSourceRange() const406 SourceRange CXXConstructExpr::getSourceRange() const {
407 if (isa<CXXTemporaryObjectExpr>(this))
408 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
409
410 if (ParenRange.isValid())
411 return SourceRange(Loc, ParenRange.getEnd());
412
413 SourceLocation End = Loc;
414 for (unsigned I = getNumArgs(); I > 0; --I) {
415 const Expr *Arg = getArg(I-1);
416 if (!Arg->isDefaultArgument()) {
417 SourceLocation NewEnd = Arg->getLocEnd();
418 if (NewEnd.isValid()) {
419 End = NewEnd;
420 break;
421 }
422 }
423 }
424
425 return SourceRange(Loc, End);
426 }
427
getSourceRangeImpl() const428 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
429 OverloadedOperatorKind Kind = getOperator();
430 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
431 if (getNumArgs() == 1)
432 // Prefix operator
433 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
434 else
435 // Postfix operator
436 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
437 } else if (Kind == OO_Arrow) {
438 return getArg(0)->getSourceRange();
439 } else if (Kind == OO_Call) {
440 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
441 } else if (Kind == OO_Subscript) {
442 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
443 } else if (getNumArgs() == 1) {
444 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
445 } else if (getNumArgs() == 2) {
446 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
447 } else {
448 return getOperatorLoc();
449 }
450 }
451
getImplicitObjectArgument() const452 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
453 const Expr *Callee = getCallee()->IgnoreParens();
454 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
455 return MemExpr->getBase();
456 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
457 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
458 return BO->getLHS();
459
460 // FIXME: Will eventually need to cope with member pointers.
461 return 0;
462 }
463
getMethodDecl() const464 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
465 if (const MemberExpr *MemExpr =
466 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
467 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
468
469 // FIXME: Will eventually need to cope with member pointers.
470 return 0;
471 }
472
473
getRecordDecl() const474 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
475 Expr* ThisArg = getImplicitObjectArgument();
476 if (!ThisArg)
477 return 0;
478
479 if (ThisArg->getType()->isAnyPointerType())
480 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
481
482 return ThisArg->getType()->getAsCXXRecordDecl();
483 }
484
485
486 //===----------------------------------------------------------------------===//
487 // Named casts
488 //===----------------------------------------------------------------------===//
489
490 /// getCastName - Get the name of the C++ cast being used, e.g.,
491 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
492 /// "const_cast". The returned pointer must not be freed.
getCastName() const493 const char *CXXNamedCastExpr::getCastName() const {
494 switch (getStmtClass()) {
495 case CXXStaticCastExprClass: return "static_cast";
496 case CXXDynamicCastExprClass: return "dynamic_cast";
497 case CXXReinterpretCastExprClass: return "reinterpret_cast";
498 case CXXConstCastExprClass: return "const_cast";
499 default: return "<invalid cast>";
500 }
501 }
502
Create(ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc)503 CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
504 ExprValueKind VK,
505 CastKind K, Expr *Op,
506 const CXXCastPath *BasePath,
507 TypeSourceInfo *WrittenTy,
508 SourceLocation L,
509 SourceLocation RParenLoc) {
510 unsigned PathSize = (BasePath ? BasePath->size() : 0);
511 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
512 + PathSize * sizeof(CXXBaseSpecifier*));
513 CXXStaticCastExpr *E =
514 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
515 RParenLoc);
516 if (PathSize) E->setCastPath(*BasePath);
517 return E;
518 }
519
CreateEmpty(ASTContext & C,unsigned PathSize)520 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
521 unsigned PathSize) {
522 void *Buffer =
523 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
524 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
525 }
526
Create(ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc)527 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
528 ExprValueKind VK,
529 CastKind K, Expr *Op,
530 const CXXCastPath *BasePath,
531 TypeSourceInfo *WrittenTy,
532 SourceLocation L,
533 SourceLocation RParenLoc) {
534 unsigned PathSize = (BasePath ? BasePath->size() : 0);
535 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
536 + PathSize * sizeof(CXXBaseSpecifier*));
537 CXXDynamicCastExpr *E =
538 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
539 RParenLoc);
540 if (PathSize) E->setCastPath(*BasePath);
541 return E;
542 }
543
CreateEmpty(ASTContext & C,unsigned PathSize)544 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
545 unsigned PathSize) {
546 void *Buffer =
547 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
548 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
549 }
550
551 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
552 /// to always be null. For example:
553 ///
554 /// struct A { };
555 /// struct B final : A { };
556 /// struct C { };
557 ///
558 /// C *f(B* b) { return dynamic_cast<C*>(b); }
isAlwaysNull() const559 bool CXXDynamicCastExpr::isAlwaysNull() const
560 {
561 QualType SrcType = getSubExpr()->getType();
562 QualType DestType = getType();
563
564 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
565 SrcType = SrcPTy->getPointeeType();
566 DestType = DestType->castAs<PointerType>()->getPointeeType();
567 }
568
569 if (DestType->isVoidType())
570 return false;
571
572 const CXXRecordDecl *SrcRD =
573 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
574
575 if (!SrcRD->hasAttr<FinalAttr>())
576 return false;
577
578 const CXXRecordDecl *DestRD =
579 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
580
581 return !DestRD->isDerivedFrom(SrcRD);
582 }
583
584 CXXReinterpretCastExpr *
Create(ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc)585 CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
586 CastKind K, Expr *Op,
587 const CXXCastPath *BasePath,
588 TypeSourceInfo *WrittenTy, SourceLocation L,
589 SourceLocation RParenLoc) {
590 unsigned PathSize = (BasePath ? BasePath->size() : 0);
591 void *Buffer =
592 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
593 CXXReinterpretCastExpr *E =
594 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
595 RParenLoc);
596 if (PathSize) E->setCastPath(*BasePath);
597 return E;
598 }
599
600 CXXReinterpretCastExpr *
CreateEmpty(ASTContext & C,unsigned PathSize)601 CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
602 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
603 + PathSize * sizeof(CXXBaseSpecifier*));
604 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
605 }
606
Create(ASTContext & C,QualType T,ExprValueKind VK,Expr * Op,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc)607 CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
608 ExprValueKind VK, Expr *Op,
609 TypeSourceInfo *WrittenTy,
610 SourceLocation L,
611 SourceLocation RParenLoc) {
612 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
613 }
614
CreateEmpty(ASTContext & C)615 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
616 return new (C) CXXConstCastExpr(EmptyShell());
617 }
618
619 CXXFunctionalCastExpr *
Create(ASTContext & C,QualType T,ExprValueKind VK,TypeSourceInfo * Written,SourceLocation L,CastKind K,Expr * Op,const CXXCastPath * BasePath,SourceLocation R)620 CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
621 TypeSourceInfo *Written, SourceLocation L,
622 CastKind K, Expr *Op, const CXXCastPath *BasePath,
623 SourceLocation R) {
624 unsigned PathSize = (BasePath ? BasePath->size() : 0);
625 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
626 + PathSize * sizeof(CXXBaseSpecifier*));
627 CXXFunctionalCastExpr *E =
628 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
629 if (PathSize) E->setCastPath(*BasePath);
630 return E;
631 }
632
633 CXXFunctionalCastExpr *
CreateEmpty(ASTContext & C,unsigned PathSize)634 CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
635 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
636 + PathSize * sizeof(CXXBaseSpecifier*));
637 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
638 }
639
640 UserDefinedLiteral::LiteralOperatorKind
getLiteralOperatorKind() const641 UserDefinedLiteral::getLiteralOperatorKind() const {
642 if (getNumArgs() == 0)
643 return LOK_Template;
644 if (getNumArgs() == 2)
645 return LOK_String;
646
647 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
648 QualType ParamTy =
649 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
650 if (ParamTy->isPointerType())
651 return LOK_Raw;
652 if (ParamTy->isAnyCharacterType())
653 return LOK_Character;
654 if (ParamTy->isIntegerType())
655 return LOK_Integer;
656 if (ParamTy->isFloatingType())
657 return LOK_Floating;
658
659 llvm_unreachable("unknown kind of literal operator");
660 }
661
getCookedLiteral()662 Expr *UserDefinedLiteral::getCookedLiteral() {
663 #ifndef NDEBUG
664 LiteralOperatorKind LOK = getLiteralOperatorKind();
665 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
666 #endif
667 return getArg(0);
668 }
669
getUDSuffix() const670 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
671 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
672 }
673
674 CXXDefaultArgExpr *
Create(ASTContext & C,SourceLocation Loc,ParmVarDecl * Param,Expr * SubExpr)675 CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
676 ParmVarDecl *Param, Expr *SubExpr) {
677 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
678 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
679 SubExpr);
680 }
681
Create(ASTContext & C,const CXXDestructorDecl * Destructor)682 CXXTemporary *CXXTemporary::Create(ASTContext &C,
683 const CXXDestructorDecl *Destructor) {
684 return new (C) CXXTemporary(Destructor);
685 }
686
Create(ASTContext & C,CXXTemporary * Temp,Expr * SubExpr)687 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
688 CXXTemporary *Temp,
689 Expr* SubExpr) {
690 assert((SubExpr->getType()->isRecordType() ||
691 SubExpr->getType()->isArrayType()) &&
692 "Expression bound to a temporary must have record or array type!");
693
694 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
695 }
696
CXXTemporaryObjectExpr(ASTContext & C,CXXConstructorDecl * Cons,TypeSourceInfo * Type,ArrayRef<Expr * > Args,SourceRange parenRange,bool HadMultipleCandidates,bool ZeroInitialization)697 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
698 CXXConstructorDecl *Cons,
699 TypeSourceInfo *Type,
700 ArrayRef<Expr*> Args,
701 SourceRange parenRange,
702 bool HadMultipleCandidates,
703 bool ZeroInitialization)
704 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
705 Type->getType().getNonReferenceType(),
706 Type->getTypeLoc().getBeginLoc(),
707 Cons, false, Args,
708 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
709 CXXConstructExpr::CK_Complete, parenRange),
710 Type(Type) {
711 }
712
getSourceRange() const713 SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
714 return SourceRange(Type->getTypeLoc().getBeginLoc(),
715 getParenRange().getEnd());
716 }
717
Create(ASTContext & C,QualType T,SourceLocation Loc,CXXConstructorDecl * D,bool Elidable,ArrayRef<Expr * > Args,bool HadMultipleCandidates,bool ListInitialization,bool ZeroInitialization,ConstructionKind ConstructKind,SourceRange ParenRange)718 CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
719 SourceLocation Loc,
720 CXXConstructorDecl *D, bool Elidable,
721 ArrayRef<Expr*> Args,
722 bool HadMultipleCandidates,
723 bool ListInitialization,
724 bool ZeroInitialization,
725 ConstructionKind ConstructKind,
726 SourceRange ParenRange) {
727 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
728 Elidable, Args,
729 HadMultipleCandidates, ListInitialization,
730 ZeroInitialization, ConstructKind,
731 ParenRange);
732 }
733
CXXConstructExpr(ASTContext & C,StmtClass SC,QualType T,SourceLocation Loc,CXXConstructorDecl * D,bool elidable,ArrayRef<Expr * > args,bool HadMultipleCandidates,bool ListInitialization,bool ZeroInitialization,ConstructionKind ConstructKind,SourceRange ParenRange)734 CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
735 SourceLocation Loc,
736 CXXConstructorDecl *D, bool elidable,
737 ArrayRef<Expr*> args,
738 bool HadMultipleCandidates,
739 bool ListInitialization,
740 bool ZeroInitialization,
741 ConstructionKind ConstructKind,
742 SourceRange ParenRange)
743 : Expr(SC, T, VK_RValue, OK_Ordinary,
744 T->isDependentType(), T->isDependentType(),
745 T->isInstantiationDependentType(),
746 T->containsUnexpandedParameterPack()),
747 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(args.size()),
748 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
749 ListInitialization(ListInitialization),
750 ZeroInitialization(ZeroInitialization),
751 ConstructKind(ConstructKind), Args(0)
752 {
753 if (NumArgs) {
754 Args = new (C) Stmt*[args.size()];
755
756 for (unsigned i = 0; i != args.size(); ++i) {
757 assert(args[i] && "NULL argument in CXXConstructExpr");
758
759 if (args[i]->isValueDependent())
760 ExprBits.ValueDependent = true;
761 if (args[i]->isInstantiationDependent())
762 ExprBits.InstantiationDependent = true;
763 if (args[i]->containsUnexpandedParameterPack())
764 ExprBits.ContainsUnexpandedParameterPack = true;
765
766 Args[i] = args[i];
767 }
768 }
769 }
770
Capture(SourceLocation Loc,bool Implicit,LambdaCaptureKind Kind,VarDecl * Var,SourceLocation EllipsisLoc)771 LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
772 LambdaCaptureKind Kind, VarDecl *Var,
773 SourceLocation EllipsisLoc)
774 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
775 {
776 unsigned Bits = 0;
777 if (Implicit)
778 Bits |= Capture_Implicit;
779
780 switch (Kind) {
781 case LCK_This:
782 assert(Var == 0 && "'this' capture cannot have a variable!");
783 break;
784
785 case LCK_ByCopy:
786 Bits |= Capture_ByCopy;
787 // Fall through
788 case LCK_ByRef:
789 assert(Var && "capture must have a variable!");
790 break;
791 }
792 VarAndBits.setInt(Bits);
793 }
794
getCaptureKind() const795 LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
796 if (capturesThis())
797 return LCK_This;
798
799 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
800 }
801
LambdaExpr(QualType T,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,ArrayRef<Capture> Captures,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,ArrayRef<VarDecl * > ArrayIndexVars,ArrayRef<unsigned> ArrayIndexStarts,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)802 LambdaExpr::LambdaExpr(QualType T,
803 SourceRange IntroducerRange,
804 LambdaCaptureDefault CaptureDefault,
805 ArrayRef<Capture> Captures,
806 bool ExplicitParams,
807 bool ExplicitResultType,
808 ArrayRef<Expr *> CaptureInits,
809 ArrayRef<VarDecl *> ArrayIndexVars,
810 ArrayRef<unsigned> ArrayIndexStarts,
811 SourceLocation ClosingBrace,
812 bool ContainsUnexpandedParameterPack)
813 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
814 T->isDependentType(), T->isDependentType(), T->isDependentType(),
815 ContainsUnexpandedParameterPack),
816 IntroducerRange(IntroducerRange),
817 NumCaptures(Captures.size()),
818 CaptureDefault(CaptureDefault),
819 ExplicitParams(ExplicitParams),
820 ExplicitResultType(ExplicitResultType),
821 ClosingBrace(ClosingBrace)
822 {
823 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
824 CXXRecordDecl *Class = getLambdaClass();
825 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
826
827 // FIXME: Propagate "has unexpanded parameter pack" bit.
828
829 // Copy captures.
830 ASTContext &Context = Class->getASTContext();
831 Data.NumCaptures = NumCaptures;
832 Data.NumExplicitCaptures = 0;
833 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
834 Capture *ToCapture = Data.Captures;
835 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
836 if (Captures[I].isExplicit())
837 ++Data.NumExplicitCaptures;
838
839 *ToCapture++ = Captures[I];
840 }
841
842 // Copy initialization expressions for the non-static data members.
843 Stmt **Stored = getStoredStmts();
844 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
845 *Stored++ = CaptureInits[I];
846
847 // Copy the body of the lambda.
848 *Stored++ = getCallOperator()->getBody();
849
850 // Copy the array index variables, if any.
851 HasArrayIndexVars = !ArrayIndexVars.empty();
852 if (HasArrayIndexVars) {
853 assert(ArrayIndexStarts.size() == NumCaptures);
854 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
855 sizeof(VarDecl *) * ArrayIndexVars.size());
856 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
857 sizeof(unsigned) * Captures.size());
858 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
859 }
860 }
861
Create(ASTContext & Context,CXXRecordDecl * Class,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,ArrayRef<Capture> Captures,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,ArrayRef<VarDecl * > ArrayIndexVars,ArrayRef<unsigned> ArrayIndexStarts,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)862 LambdaExpr *LambdaExpr::Create(ASTContext &Context,
863 CXXRecordDecl *Class,
864 SourceRange IntroducerRange,
865 LambdaCaptureDefault CaptureDefault,
866 ArrayRef<Capture> Captures,
867 bool ExplicitParams,
868 bool ExplicitResultType,
869 ArrayRef<Expr *> CaptureInits,
870 ArrayRef<VarDecl *> ArrayIndexVars,
871 ArrayRef<unsigned> ArrayIndexStarts,
872 SourceLocation ClosingBrace,
873 bool ContainsUnexpandedParameterPack) {
874 // Determine the type of the expression (i.e., the type of the
875 // function object we're creating).
876 QualType T = Context.getTypeDeclType(Class);
877
878 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
879 if (!ArrayIndexVars.empty()) {
880 Size += sizeof(unsigned) * (Captures.size() + 1);
881 // Realign for following VarDecl array.
882 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
883 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
884 }
885 void *Mem = Context.Allocate(Size);
886 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
887 Captures, ExplicitParams, ExplicitResultType,
888 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
889 ClosingBrace, ContainsUnexpandedParameterPack);
890 }
891
CreateDeserialized(ASTContext & C,unsigned NumCaptures,unsigned NumArrayIndexVars)892 LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
893 unsigned NumArrayIndexVars) {
894 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
895 if (NumArrayIndexVars)
896 Size += sizeof(VarDecl) * NumArrayIndexVars
897 + sizeof(unsigned) * (NumCaptures + 1);
898 void *Mem = C.Allocate(Size);
899 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
900 }
901
capture_begin() const902 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
903 return getLambdaClass()->getLambdaData().Captures;
904 }
905
capture_end() const906 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
907 return capture_begin() + NumCaptures;
908 }
909
explicit_capture_begin() const910 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
911 return capture_begin();
912 }
913
explicit_capture_end() const914 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
915 struct CXXRecordDecl::LambdaDefinitionData &Data
916 = getLambdaClass()->getLambdaData();
917 return Data.Captures + Data.NumExplicitCaptures;
918 }
919
implicit_capture_begin() const920 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
921 return explicit_capture_end();
922 }
923
implicit_capture_end() const924 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
925 return capture_end();
926 }
927
928 ArrayRef<VarDecl *>
getCaptureInitIndexVars(capture_init_iterator Iter) const929 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
930 assert(HasArrayIndexVars && "No array index-var data?");
931
932 unsigned Index = Iter - capture_init_begin();
933 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
934 "Capture index out-of-range");
935 VarDecl **IndexVars = getArrayIndexVars();
936 unsigned *IndexStarts = getArrayIndexStarts();
937 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
938 IndexVars + IndexStarts[Index + 1]);
939 }
940
getLambdaClass() const941 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
942 return getType()->getAsCXXRecordDecl();
943 }
944
getCallOperator() const945 CXXMethodDecl *LambdaExpr::getCallOperator() const {
946 CXXRecordDecl *Record = getLambdaClass();
947 DeclarationName Name
948 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
949 DeclContext::lookup_result Calls = Record->lookup(Name);
950 assert(Calls.first != Calls.second && "Missing lambda call operator!");
951 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
952 assert(Calls.first == Calls.second && "More than lambda one call operator?");
953 return Result;
954 }
955
getBody() const956 CompoundStmt *LambdaExpr::getBody() const {
957 if (!getStoredStmts()[NumCaptures])
958 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
959
960 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
961 }
962
isMutable() const963 bool LambdaExpr::isMutable() const {
964 return !getCallOperator()->isConst();
965 }
966
ExprWithCleanups(Expr * subexpr,ArrayRef<CleanupObject> objects)967 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
968 ArrayRef<CleanupObject> objects)
969 : Expr(ExprWithCleanupsClass, subexpr->getType(),
970 subexpr->getValueKind(), subexpr->getObjectKind(),
971 subexpr->isTypeDependent(), subexpr->isValueDependent(),
972 subexpr->isInstantiationDependent(),
973 subexpr->containsUnexpandedParameterPack()),
974 SubExpr(subexpr) {
975 ExprWithCleanupsBits.NumObjects = objects.size();
976 for (unsigned i = 0, e = objects.size(); i != e; ++i)
977 getObjectsBuffer()[i] = objects[i];
978 }
979
Create(ASTContext & C,Expr * subexpr,ArrayRef<CleanupObject> objects)980 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
981 ArrayRef<CleanupObject> objects) {
982 size_t size = sizeof(ExprWithCleanups)
983 + objects.size() * sizeof(CleanupObject);
984 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
985 return new (buffer) ExprWithCleanups(subexpr, objects);
986 }
987
ExprWithCleanups(EmptyShell empty,unsigned numObjects)988 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
989 : Expr(ExprWithCleanupsClass, empty) {
990 ExprWithCleanupsBits.NumObjects = numObjects;
991 }
992
Create(ASTContext & C,EmptyShell empty,unsigned numObjects)993 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
994 unsigned numObjects) {
995 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
996 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
997 return new (buffer) ExprWithCleanups(empty, numObjects);
998 }
999
CXXUnresolvedConstructExpr(TypeSourceInfo * Type,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc)1000 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1001 SourceLocation LParenLoc,
1002 ArrayRef<Expr*> Args,
1003 SourceLocation RParenLoc)
1004 : Expr(CXXUnresolvedConstructExprClass,
1005 Type->getType().getNonReferenceType(),
1006 (Type->getType()->isLValueReferenceType() ? VK_LValue
1007 :Type->getType()->isRValueReferenceType()? VK_XValue
1008 :VK_RValue),
1009 OK_Ordinary,
1010 Type->getType()->isDependentType(), true, true,
1011 Type->getType()->containsUnexpandedParameterPack()),
1012 Type(Type),
1013 LParenLoc(LParenLoc),
1014 RParenLoc(RParenLoc),
1015 NumArgs(Args.size()) {
1016 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
1017 for (unsigned I = 0; I != Args.size(); ++I) {
1018 if (Args[I]->containsUnexpandedParameterPack())
1019 ExprBits.ContainsUnexpandedParameterPack = true;
1020
1021 StoredArgs[I] = Args[I];
1022 }
1023 }
1024
1025 CXXUnresolvedConstructExpr *
Create(ASTContext & C,TypeSourceInfo * Type,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc)1026 CXXUnresolvedConstructExpr::Create(ASTContext &C,
1027 TypeSourceInfo *Type,
1028 SourceLocation LParenLoc,
1029 ArrayRef<Expr*> Args,
1030 SourceLocation RParenLoc) {
1031 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1032 sizeof(Expr *) * Args.size());
1033 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1034 }
1035
1036 CXXUnresolvedConstructExpr *
CreateEmpty(ASTContext & C,unsigned NumArgs)1037 CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1038 Stmt::EmptyShell Empty;
1039 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1040 sizeof(Expr *) * NumArgs);
1041 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1042 }
1043
getSourceRange() const1044 SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1045 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1046 }
1047
CXXDependentScopeMemberExpr(ASTContext & C,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)1048 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1049 Expr *Base, QualType BaseType,
1050 bool IsArrow,
1051 SourceLocation OperatorLoc,
1052 NestedNameSpecifierLoc QualifierLoc,
1053 SourceLocation TemplateKWLoc,
1054 NamedDecl *FirstQualifierFoundInScope,
1055 DeclarationNameInfo MemberNameInfo,
1056 const TemplateArgumentListInfo *TemplateArgs)
1057 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1058 VK_LValue, OK_Ordinary, true, true, true,
1059 ((Base && Base->containsUnexpandedParameterPack()) ||
1060 (QualifierLoc &&
1061 QualifierLoc.getNestedNameSpecifier()
1062 ->containsUnexpandedParameterPack()) ||
1063 MemberNameInfo.containsUnexpandedParameterPack())),
1064 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1065 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
1066 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1067 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1068 MemberNameInfo(MemberNameInfo) {
1069 if (TemplateArgs) {
1070 bool Dependent = true;
1071 bool InstantiationDependent = true;
1072 bool ContainsUnexpandedParameterPack = false;
1073 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1074 Dependent,
1075 InstantiationDependent,
1076 ContainsUnexpandedParameterPack);
1077 if (ContainsUnexpandedParameterPack)
1078 ExprBits.ContainsUnexpandedParameterPack = true;
1079 } else if (TemplateKWLoc.isValid()) {
1080 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
1081 }
1082 }
1083
CXXDependentScopeMemberExpr(ASTContext & C,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo)1084 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1085 Expr *Base, QualType BaseType,
1086 bool IsArrow,
1087 SourceLocation OperatorLoc,
1088 NestedNameSpecifierLoc QualifierLoc,
1089 NamedDecl *FirstQualifierFoundInScope,
1090 DeclarationNameInfo MemberNameInfo)
1091 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1092 VK_LValue, OK_Ordinary, true, true, true,
1093 ((Base && Base->containsUnexpandedParameterPack()) ||
1094 (QualifierLoc &&
1095 QualifierLoc.getNestedNameSpecifier()->
1096 containsUnexpandedParameterPack()) ||
1097 MemberNameInfo.containsUnexpandedParameterPack())),
1098 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1099 HasTemplateKWAndArgsInfo(false),
1100 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1101 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1102 MemberNameInfo(MemberNameInfo) { }
1103
1104 CXXDependentScopeMemberExpr *
Create(ASTContext & C,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)1105 CXXDependentScopeMemberExpr::Create(ASTContext &C,
1106 Expr *Base, QualType BaseType, bool IsArrow,
1107 SourceLocation OperatorLoc,
1108 NestedNameSpecifierLoc QualifierLoc,
1109 SourceLocation TemplateKWLoc,
1110 NamedDecl *FirstQualifierFoundInScope,
1111 DeclarationNameInfo MemberNameInfo,
1112 const TemplateArgumentListInfo *TemplateArgs) {
1113 if (!TemplateArgs && !TemplateKWLoc.isValid())
1114 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1115 IsArrow, OperatorLoc,
1116 QualifierLoc,
1117 FirstQualifierFoundInScope,
1118 MemberNameInfo);
1119
1120 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1121 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1122 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1123
1124 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1125 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1126 IsArrow, OperatorLoc,
1127 QualifierLoc,
1128 TemplateKWLoc,
1129 FirstQualifierFoundInScope,
1130 MemberNameInfo, TemplateArgs);
1131 }
1132
1133 CXXDependentScopeMemberExpr *
CreateEmpty(ASTContext & C,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)1134 CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
1135 bool HasTemplateKWAndArgsInfo,
1136 unsigned NumTemplateArgs) {
1137 if (!HasTemplateKWAndArgsInfo)
1138 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
1139 0, SourceLocation(),
1140 NestedNameSpecifierLoc(), 0,
1141 DeclarationNameInfo());
1142
1143 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
1144 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1145 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1146 CXXDependentScopeMemberExpr *E
1147 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
1148 0, SourceLocation(),
1149 NestedNameSpecifierLoc(),
1150 SourceLocation(), 0,
1151 DeclarationNameInfo(), 0);
1152 E->HasTemplateKWAndArgsInfo = true;
1153 return E;
1154 }
1155
isImplicitAccess() const1156 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1157 if (Base == 0)
1158 return true;
1159
1160 return cast<Expr>(Base)->isImplicitCXXThis();
1161 }
1162
hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,UnresolvedSetIterator end)1163 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1164 UnresolvedSetIterator end) {
1165 do {
1166 NamedDecl *decl = *begin;
1167 if (isa<UnresolvedUsingValueDecl>(decl))
1168 return false;
1169 if (isa<UsingShadowDecl>(decl))
1170 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1171
1172 // Unresolved member expressions should only contain methods and
1173 // method templates.
1174 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1175
1176 if (isa<FunctionTemplateDecl>(decl))
1177 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1178 if (cast<CXXMethodDecl>(decl)->isStatic())
1179 return false;
1180 } while (++begin != end);
1181
1182 return true;
1183 }
1184
UnresolvedMemberExpr(ASTContext & C,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)1185 UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
1186 bool HasUnresolvedUsing,
1187 Expr *Base, QualType BaseType,
1188 bool IsArrow,
1189 SourceLocation OperatorLoc,
1190 NestedNameSpecifierLoc QualifierLoc,
1191 SourceLocation TemplateKWLoc,
1192 const DeclarationNameInfo &MemberNameInfo,
1193 const TemplateArgumentListInfo *TemplateArgs,
1194 UnresolvedSetIterator Begin,
1195 UnresolvedSetIterator End)
1196 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1197 MemberNameInfo, TemplateArgs, Begin, End,
1198 // Dependent
1199 ((Base && Base->isTypeDependent()) ||
1200 BaseType->isDependentType()),
1201 ((Base && Base->isInstantiationDependent()) ||
1202 BaseType->isInstantiationDependentType()),
1203 // Contains unexpanded parameter pack
1204 ((Base && Base->containsUnexpandedParameterPack()) ||
1205 BaseType->containsUnexpandedParameterPack())),
1206 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1207 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1208
1209 // Check whether all of the members are non-static member functions,
1210 // and if so, mark give this bound-member type instead of overload type.
1211 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1212 setType(C.BoundMemberTy);
1213 }
1214
isImplicitAccess() const1215 bool UnresolvedMemberExpr::isImplicitAccess() const {
1216 if (Base == 0)
1217 return true;
1218
1219 return cast<Expr>(Base)->isImplicitCXXThis();
1220 }
1221
1222 UnresolvedMemberExpr *
Create(ASTContext & C,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)1223 UnresolvedMemberExpr::Create(ASTContext &C,
1224 bool HasUnresolvedUsing,
1225 Expr *Base, QualType BaseType, bool IsArrow,
1226 SourceLocation OperatorLoc,
1227 NestedNameSpecifierLoc QualifierLoc,
1228 SourceLocation TemplateKWLoc,
1229 const DeclarationNameInfo &MemberNameInfo,
1230 const TemplateArgumentListInfo *TemplateArgs,
1231 UnresolvedSetIterator Begin,
1232 UnresolvedSetIterator End) {
1233 std::size_t size = sizeof(UnresolvedMemberExpr);
1234 if (TemplateArgs)
1235 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1236 else if (TemplateKWLoc.isValid())
1237 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
1238
1239 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1240 return new (Mem) UnresolvedMemberExpr(C,
1241 HasUnresolvedUsing, Base, BaseType,
1242 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1243 MemberNameInfo, TemplateArgs, Begin, End);
1244 }
1245
1246 UnresolvedMemberExpr *
CreateEmpty(ASTContext & C,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)1247 UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
1248 unsigned NumTemplateArgs) {
1249 std::size_t size = sizeof(UnresolvedMemberExpr);
1250 if (HasTemplateKWAndArgsInfo)
1251 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1252
1253 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1254 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1255 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1256 return E;
1257 }
1258
getNamingClass() const1259 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1260 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1261
1262 // If there was a nested name specifier, it names the naming class.
1263 // It can't be dependent: after all, we were actually able to do the
1264 // lookup.
1265 CXXRecordDecl *Record = 0;
1266 if (getQualifier()) {
1267 const Type *T = getQualifier()->getAsType();
1268 assert(T && "qualifier in member expression does not name type");
1269 Record = T->getAsCXXRecordDecl();
1270 assert(Record && "qualifier in member expression does not name record");
1271 }
1272 // Otherwise the naming class must have been the base class.
1273 else {
1274 QualType BaseType = getBaseType().getNonReferenceType();
1275 if (isArrow()) {
1276 const PointerType *PT = BaseType->getAs<PointerType>();
1277 assert(PT && "base of arrow member access is not pointer");
1278 BaseType = PT->getPointeeType();
1279 }
1280
1281 Record = BaseType->getAsCXXRecordDecl();
1282 assert(Record && "base of member expression does not name record");
1283 }
1284
1285 return Record;
1286 }
1287
1288 SubstNonTypeTemplateParmPackExpr::
SubstNonTypeTemplateParmPackExpr(QualType T,NonTypeTemplateParmDecl * Param,SourceLocation NameLoc,const TemplateArgument & ArgPack)1289 SubstNonTypeTemplateParmPackExpr(QualType T,
1290 NonTypeTemplateParmDecl *Param,
1291 SourceLocation NameLoc,
1292 const TemplateArgument &ArgPack)
1293 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1294 true, true, true, true),
1295 Param(Param), Arguments(ArgPack.pack_begin()),
1296 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1297
getArgumentPack() const1298 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1299 return TemplateArgument(Arguments, NumArguments);
1300 }
1301
TypeTraitExpr(QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)1302 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1303 ArrayRef<TypeSourceInfo *> Args,
1304 SourceLocation RParenLoc,
1305 bool Value)
1306 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1307 /*TypeDependent=*/false,
1308 /*ValueDependent=*/false,
1309 /*InstantiationDependent=*/false,
1310 /*ContainsUnexpandedParameterPack=*/false),
1311 Loc(Loc), RParenLoc(RParenLoc)
1312 {
1313 TypeTraitExprBits.Kind = Kind;
1314 TypeTraitExprBits.Value = Value;
1315 TypeTraitExprBits.NumArgs = Args.size();
1316
1317 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1318
1319 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1320 if (Args[I]->getType()->isDependentType())
1321 setValueDependent(true);
1322 if (Args[I]->getType()->isInstantiationDependentType())
1323 setInstantiationDependent(true);
1324 if (Args[I]->getType()->containsUnexpandedParameterPack())
1325 setContainsUnexpandedParameterPack(true);
1326
1327 ToArgs[I] = Args[I];
1328 }
1329 }
1330
Create(ASTContext & C,QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)1331 TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1332 SourceLocation Loc,
1333 TypeTrait Kind,
1334 ArrayRef<TypeSourceInfo *> Args,
1335 SourceLocation RParenLoc,
1336 bool Value) {
1337 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1338 void *Mem = C.Allocate(Size);
1339 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1340 }
1341
CreateDeserialized(ASTContext & C,unsigned NumArgs)1342 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1343 unsigned NumArgs) {
1344 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1345 void *Mem = C.Allocate(Size);
1346 return new (Mem) TypeTraitExpr(EmptyShell());
1347 }
1348
anchor()1349 void ArrayTypeTraitExpr::anchor() { }
1350