1 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the subclesses of Expr class declared in ExprCXX.h
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.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 #include "clang/Basic/IdentifierTable.h"
21 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // Child Iterators for iterating over subexpressions/substatements
26 //===----------------------------------------------------------------------===//
28 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
43 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
44 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
50 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
51 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
58 UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
60 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
65 Ty = Ty->getBaseElementTypeUnsafe();
67 // Loop all record redeclaration looking for an uuid attribute.
68 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
72 // __uuidof can grab UUIDs from template arguments.
73 if (ClassTemplateSpecializationDecl *CTSD =
74 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
75 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
76 UuidAttr *UuidForRD = 0;
78 for (unsigned I = 0, N = TAL.size(); I != N; ++I) {
79 const TemplateArgument &TA = TAL[I];
80 bool SeenMultipleGUIDs = false;
82 UuidAttr *UuidForTA = 0;
83 if (TA.getKind() == TemplateArgument::Type)
84 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
85 else if (TA.getKind() == TemplateArgument::Declaration)
87 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
89 // If the template argument has a UUID, there are three cases:
90 // - This is the first UUID seen for this RecordDecl.
91 // - This is a different UUID than previously seen for this RecordDecl.
92 // - This is the same UUID than previously seen for this RecordDecl.
95 UuidForRD = UuidForTA;
96 else if (UuidForRD != UuidForTA)
97 SeenMultipleGUIDs = true;
100 // Seeing multiple UUIDs means that we couldn't find a UUID
101 if (SeenMultipleGUIDs) {
102 if (RDHasMultipleGUIDsPtr)
103 *RDHasMultipleGUIDsPtr = true;
111 for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
112 E = RD->redecls_end();
114 if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
120 StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
125 // Special case: __uuidof(0) means an all-zero GUID.
126 Expr *Op = getExprOperand();
127 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
128 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
130 Uuid = "00000000-0000-0000-0000-000000000000";
135 // CXXScalarValueInitExpr
136 SourceLocation CXXScalarValueInitExpr::getLocStart() const {
137 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
141 CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
142 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
143 bool usualArrayDeleteWantsSize,
144 ArrayRef<Expr*> placementArgs,
145 SourceRange typeIdParens, Expr *arraySize,
146 InitializationStyle initializationStyle,
147 Expr *initializer, QualType ty,
148 TypeSourceInfo *allocatedTypeInfo,
149 SourceRange Range, SourceRange directInitRange)
150 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
151 ty->isDependentType(), ty->isDependentType(),
152 ty->isInstantiationDependentType(),
153 ty->containsUnexpandedParameterPack()),
154 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
155 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
156 Range(Range), DirectInitRange(directInitRange),
157 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
158 assert((initializer != 0 || initializationStyle == NoInit) &&
159 "Only NoInit can have no initializer.");
160 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
161 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
164 if (arraySize->isInstantiationDependent())
165 ExprBits.InstantiationDependent = true;
167 if (arraySize->containsUnexpandedParameterPack())
168 ExprBits.ContainsUnexpandedParameterPack = true;
170 SubExprs[i++] = arraySize;
174 if (initializer->isInstantiationDependent())
175 ExprBits.InstantiationDependent = true;
177 if (initializer->containsUnexpandedParameterPack())
178 ExprBits.ContainsUnexpandedParameterPack = true;
180 SubExprs[i++] = initializer;
183 for (unsigned j = 0; j != placementArgs.size(); ++j) {
184 if (placementArgs[j]->isInstantiationDependent())
185 ExprBits.InstantiationDependent = true;
186 if (placementArgs[j]->containsUnexpandedParameterPack())
187 ExprBits.ContainsUnexpandedParameterPack = true;
189 SubExprs[i++] = placementArgs[j];
192 switch (getInitializationStyle()) {
194 this->Range.setEnd(DirectInitRange.getEnd()); break;
196 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
198 if (TypeIdParens.isValid())
199 this->Range.setEnd(TypeIdParens.getEnd());
204 void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
205 unsigned numPlaceArgs, bool hasInitializer){
206 assert(SubExprs == 0 && "SubExprs already allocated");
208 NumPlacementArgs = numPlaceArgs;
210 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
211 SubExprs = new (C) Stmt*[TotalSize];
214 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
215 return getOperatorNew()->getType()->
216 castAs<FunctionProtoType>()->isNothrow(Ctx);
220 QualType CXXDeleteExpr::getDestroyedType() const {
221 const Expr *Arg = getArgument();
222 // The type-to-delete may not be a pointer if it's a dependent type.
223 const QualType ArgType = Arg->getType();
225 if (ArgType->isDependentType() && !ArgType->isPointerType())
228 return ArgType->getAs<PointerType>()->getPointeeType();
231 // CXXPseudoDestructorExpr
232 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
235 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
238 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
239 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
240 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
241 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
242 PseudoDestructorTypeStorage DestroyedType)
243 : Expr(CXXPseudoDestructorExprClass,
244 Context.getPointerType(Context.getFunctionType(
245 Context.VoidTy, None,
246 FunctionProtoType::ExtProtoInfo(
247 Context.getDefaultCallingConvention(false, true)))),
248 VK_RValue, OK_Ordinary,
249 /*isTypeDependent=*/(Base->isTypeDependent() ||
250 (DestroyedType.getTypeSourceInfo() &&
251 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
252 /*isValueDependent=*/Base->isValueDependent(),
253 (Base->isInstantiationDependent() ||
255 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
257 ScopeType->getType()->isInstantiationDependentType()) ||
258 (DestroyedType.getTypeSourceInfo() &&
259 DestroyedType.getTypeSourceInfo()->getType()
260 ->isInstantiationDependentType())),
261 // ContainsUnexpandedParameterPack
262 (Base->containsUnexpandedParameterPack() ||
264 QualifierLoc.getNestedNameSpecifier()
265 ->containsUnexpandedParameterPack()) ||
267 ScopeType->getType()->containsUnexpandedParameterPack()) ||
268 (DestroyedType.getTypeSourceInfo() &&
269 DestroyedType.getTypeSourceInfo()->getType()
270 ->containsUnexpandedParameterPack()))),
271 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
272 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
273 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
274 DestroyedType(DestroyedType) { }
276 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
277 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
278 return TInfo->getType();
283 SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
284 SourceLocation End = DestroyedType.getLocation();
285 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
286 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
290 // UnresolvedLookupExpr
291 UnresolvedLookupExpr *
292 UnresolvedLookupExpr::Create(const ASTContext &C,
293 CXXRecordDecl *NamingClass,
294 NestedNameSpecifierLoc QualifierLoc,
295 SourceLocation TemplateKWLoc,
296 const DeclarationNameInfo &NameInfo,
298 const TemplateArgumentListInfo *Args,
299 UnresolvedSetIterator Begin,
300 UnresolvedSetIterator End)
302 assert(Args || TemplateKWLoc.isValid());
303 unsigned num_args = Args ? Args->size() : 0;
304 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
305 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
306 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
307 TemplateKWLoc, NameInfo,
308 ADL, /*Overload*/ true, Args,
312 UnresolvedLookupExpr *
313 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
314 bool HasTemplateKWAndArgsInfo,
315 unsigned NumTemplateArgs) {
316 std::size_t size = sizeof(UnresolvedLookupExpr);
317 if (HasTemplateKWAndArgsInfo)
318 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
320 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
321 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
322 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
326 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
327 NestedNameSpecifierLoc QualifierLoc,
328 SourceLocation TemplateKWLoc,
329 const DeclarationNameInfo &NameInfo,
330 const TemplateArgumentListInfo *TemplateArgs,
331 UnresolvedSetIterator Begin,
332 UnresolvedSetIterator End,
334 bool KnownInstantiationDependent,
335 bool KnownContainsUnexpandedParameterPack)
336 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
338 (KnownInstantiationDependent ||
339 NameInfo.isInstantiationDependent() ||
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
342 (KnownContainsUnexpandedParameterPack ||
343 NameInfo.containsUnexpandedParameterPack() ||
345 QualifierLoc.getNestedNameSpecifier()
346 ->containsUnexpandedParameterPack()))),
347 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
348 Results(0), NumResults(End - Begin),
349 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
351 NumResults = End - Begin;
353 // Determine whether this expression is type-dependent.
354 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
355 if ((*I)->getDeclContext()->isDependentContext() ||
356 isa<UnresolvedUsingValueDecl>(*I)) {
357 ExprBits.TypeDependent = true;
358 ExprBits.ValueDependent = true;
359 ExprBits.InstantiationDependent = true;
363 Results = static_cast<DeclAccessPair *>(
364 C.Allocate(sizeof(DeclAccessPair) * NumResults,
365 llvm::alignOf<DeclAccessPair>()));
366 memcpy(Results, &*Begin.getIterator(),
367 NumResults * sizeof(DeclAccessPair));
370 // If we have explicit template arguments, check for dependent
371 // template arguments and whether they contain any unexpanded pack
374 bool Dependent = false;
375 bool InstantiationDependent = false;
376 bool ContainsUnexpandedParameterPack = false;
377 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
379 InstantiationDependent,
380 ContainsUnexpandedParameterPack);
383 ExprBits.TypeDependent = true;
384 ExprBits.ValueDependent = true;
386 if (InstantiationDependent)
387 ExprBits.InstantiationDependent = true;
388 if (ContainsUnexpandedParameterPack)
389 ExprBits.ContainsUnexpandedParameterPack = true;
390 } else if (TemplateKWLoc.isValid()) {
391 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
394 if (isTypeDependent())
395 setType(C.DependentTy);
398 void OverloadExpr::initializeResults(const ASTContext &C,
399 UnresolvedSetIterator Begin,
400 UnresolvedSetIterator End) {
401 assert(Results == 0 && "Results already initialized!");
402 NumResults = End - Begin;
404 Results = static_cast<DeclAccessPair *>(
405 C.Allocate(sizeof(DeclAccessPair) * NumResults,
407 llvm::alignOf<DeclAccessPair>()));
408 memcpy(Results, &*Begin.getIterator(),
409 NumResults * sizeof(DeclAccessPair));
413 CXXRecordDecl *OverloadExpr::getNamingClass() const {
414 if (isa<UnresolvedLookupExpr>(this))
415 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
417 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
420 // DependentScopeDeclRefExpr
421 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
422 NestedNameSpecifierLoc QualifierLoc,
423 SourceLocation TemplateKWLoc,
424 const DeclarationNameInfo &NameInfo,
425 const TemplateArgumentListInfo *Args)
426 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
428 (NameInfo.isInstantiationDependent() ||
430 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
431 (NameInfo.containsUnexpandedParameterPack() ||
433 QualifierLoc.getNestedNameSpecifier()
434 ->containsUnexpandedParameterPack()))),
435 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
436 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
439 bool Dependent = true;
440 bool InstantiationDependent = true;
441 bool ContainsUnexpandedParameterPack
442 = ExprBits.ContainsUnexpandedParameterPack;
443 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
445 InstantiationDependent,
446 ContainsUnexpandedParameterPack);
447 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
448 } else if (TemplateKWLoc.isValid()) {
449 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
453 DependentScopeDeclRefExpr *
454 DependentScopeDeclRefExpr::Create(const ASTContext &C,
455 NestedNameSpecifierLoc QualifierLoc,
456 SourceLocation TemplateKWLoc,
457 const DeclarationNameInfo &NameInfo,
458 const TemplateArgumentListInfo *Args) {
459 assert(QualifierLoc && "should be created for dependent qualifiers");
460 std::size_t size = sizeof(DependentScopeDeclRefExpr);
462 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
463 else if (TemplateKWLoc.isValid())
464 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
465 void *Mem = C.Allocate(size);
466 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
467 TemplateKWLoc, NameInfo, Args);
470 DependentScopeDeclRefExpr *
471 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
472 bool HasTemplateKWAndArgsInfo,
473 unsigned NumTemplateArgs) {
474 std::size_t size = sizeof(DependentScopeDeclRefExpr);
475 if (HasTemplateKWAndArgsInfo)
476 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
477 void *Mem = C.Allocate(size);
478 DependentScopeDeclRefExpr *E
479 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
481 DeclarationNameInfo(), 0);
482 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
486 SourceLocation CXXConstructExpr::getLocStart() const {
487 if (isa<CXXTemporaryObjectExpr>(this))
488 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
492 SourceLocation CXXConstructExpr::getLocEnd() const {
493 if (isa<CXXTemporaryObjectExpr>(this))
494 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
496 if (ParenOrBraceRange.isValid())
497 return ParenOrBraceRange.getEnd();
499 SourceLocation End = Loc;
500 for (unsigned I = getNumArgs(); I > 0; --I) {
501 const Expr *Arg = getArg(I-1);
502 if (!Arg->isDefaultArgument()) {
503 SourceLocation NewEnd = Arg->getLocEnd();
504 if (NewEnd.isValid()) {
514 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
515 OverloadedOperatorKind Kind = getOperator();
516 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
517 if (getNumArgs() == 1)
519 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
522 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
523 } else if (Kind == OO_Arrow) {
524 return getArg(0)->getSourceRange();
525 } else if (Kind == OO_Call) {
526 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
527 } else if (Kind == OO_Subscript) {
528 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
529 } else if (getNumArgs() == 1) {
530 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
531 } else if (getNumArgs() == 2) {
532 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
534 return getOperatorLoc();
538 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
539 const Expr *Callee = getCallee()->IgnoreParens();
540 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
541 return MemExpr->getBase();
542 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
543 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
546 // FIXME: Will eventually need to cope with member pointers.
550 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
551 if (const MemberExpr *MemExpr =
552 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
553 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
555 // FIXME: Will eventually need to cope with member pointers.
560 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
561 Expr* ThisArg = getImplicitObjectArgument();
565 if (ThisArg->getType()->isAnyPointerType())
566 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
568 return ThisArg->getType()->getAsCXXRecordDecl();
572 //===----------------------------------------------------------------------===//
574 //===----------------------------------------------------------------------===//
576 /// getCastName - Get the name of the C++ cast being used, e.g.,
577 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
578 /// "const_cast". The returned pointer must not be freed.
579 const char *CXXNamedCastExpr::getCastName() const {
580 switch (getStmtClass()) {
581 case CXXStaticCastExprClass: return "static_cast";
582 case CXXDynamicCastExprClass: return "dynamic_cast";
583 case CXXReinterpretCastExprClass: return "reinterpret_cast";
584 case CXXConstCastExprClass: return "const_cast";
585 default: return "<invalid cast>";
589 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
591 CastKind K, Expr *Op,
592 const CXXCastPath *BasePath,
593 TypeSourceInfo *WrittenTy,
595 SourceLocation RParenLoc,
596 SourceRange AngleBrackets) {
597 unsigned PathSize = (BasePath ? BasePath->size() : 0);
598 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
599 + PathSize * sizeof(CXXBaseSpecifier*));
600 CXXStaticCastExpr *E =
601 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
602 RParenLoc, AngleBrackets);
603 if (PathSize) E->setCastPath(*BasePath);
607 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
610 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
611 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
614 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
616 CastKind K, Expr *Op,
617 const CXXCastPath *BasePath,
618 TypeSourceInfo *WrittenTy,
620 SourceLocation RParenLoc,
621 SourceRange AngleBrackets) {
622 unsigned PathSize = (BasePath ? BasePath->size() : 0);
623 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
624 + PathSize * sizeof(CXXBaseSpecifier*));
625 CXXDynamicCastExpr *E =
626 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
627 RParenLoc, AngleBrackets);
628 if (PathSize) E->setCastPath(*BasePath);
632 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
635 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
636 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
639 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
640 /// to always be null. For example:
643 /// struct B final : A { };
646 /// C *f(B* b) { return dynamic_cast<C*>(b); }
647 bool CXXDynamicCastExpr::isAlwaysNull() const
649 QualType SrcType = getSubExpr()->getType();
650 QualType DestType = getType();
652 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
653 SrcType = SrcPTy->getPointeeType();
654 DestType = DestType->castAs<PointerType>()->getPointeeType();
657 if (DestType->isVoidType())
660 const CXXRecordDecl *SrcRD =
661 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
663 if (!SrcRD->hasAttr<FinalAttr>())
666 const CXXRecordDecl *DestRD =
667 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
669 return !DestRD->isDerivedFrom(SrcRD);
672 CXXReinterpretCastExpr *
673 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
674 ExprValueKind VK, CastKind K, Expr *Op,
675 const CXXCastPath *BasePath,
676 TypeSourceInfo *WrittenTy, SourceLocation L,
677 SourceLocation RParenLoc,
678 SourceRange AngleBrackets) {
679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
681 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
682 CXXReinterpretCastExpr *E =
683 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
684 RParenLoc, AngleBrackets);
685 if (PathSize) E->setCastPath(*BasePath);
689 CXXReinterpretCastExpr *
690 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
691 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
692 + PathSize * sizeof(CXXBaseSpecifier*));
693 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
696 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
697 ExprValueKind VK, Expr *Op,
698 TypeSourceInfo *WrittenTy,
700 SourceLocation RParenLoc,
701 SourceRange AngleBrackets) {
702 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
705 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
706 return new (C) CXXConstCastExpr(EmptyShell());
709 CXXFunctionalCastExpr *
710 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
711 TypeSourceInfo *Written, CastKind K, Expr *Op,
712 const CXXCastPath *BasePath,
713 SourceLocation L, SourceLocation R) {
714 unsigned PathSize = (BasePath ? BasePath->size() : 0);
715 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
716 + PathSize * sizeof(CXXBaseSpecifier*));
717 CXXFunctionalCastExpr *E =
718 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
719 if (PathSize) E->setCastPath(*BasePath);
723 CXXFunctionalCastExpr *
724 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
725 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
726 + PathSize * sizeof(CXXBaseSpecifier*));
727 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
730 SourceLocation CXXFunctionalCastExpr::getLocStart() const {
731 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
734 SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
735 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
738 UserDefinedLiteral::LiteralOperatorKind
739 UserDefinedLiteral::getLiteralOperatorKind() const {
740 if (getNumArgs() == 0)
742 if (getNumArgs() == 2)
745 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
747 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
748 if (ParamTy->isPointerType())
750 if (ParamTy->isAnyCharacterType())
751 return LOK_Character;
752 if (ParamTy->isIntegerType())
754 if (ParamTy->isFloatingType())
757 llvm_unreachable("unknown kind of literal operator");
760 Expr *UserDefinedLiteral::getCookedLiteral() {
762 LiteralOperatorKind LOK = getLiteralOperatorKind();
763 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
768 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
769 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
773 CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
774 ParmVarDecl *Param, Expr *SubExpr) {
775 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
776 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
780 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
781 FieldDecl *Field, QualType T)
782 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
783 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
786 /*FIXME*/ OK_Ordinary, false, false, false, false),
787 Field(Field), Loc(Loc) {
788 assert(Field->hasInClassInitializer());
791 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
792 const CXXDestructorDecl *Destructor) {
793 return new (C) CXXTemporary(Destructor);
796 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
799 assert((SubExpr->getType()->isRecordType() ||
800 SubExpr->getType()->isArrayType()) &&
801 "Expression bound to a temporary must have record or array type!");
803 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
806 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
807 CXXConstructorDecl *Cons,
808 TypeSourceInfo *Type,
809 ArrayRef<Expr*> Args,
810 SourceRange ParenOrBraceRange,
811 bool HadMultipleCandidates,
812 bool ListInitialization,
813 bool ZeroInitialization)
814 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
815 Type->getType().getNonReferenceType(),
816 Type->getTypeLoc().getBeginLoc(),
818 HadMultipleCandidates,
819 ListInitialization, ZeroInitialization,
820 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
824 SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
825 return Type->getTypeLoc().getBeginLoc();
828 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
829 SourceLocation Loc = getParenOrBraceRange().getEnd();
830 if (Loc.isInvalid() && getNumArgs())
831 Loc = getArg(getNumArgs()-1)->getLocEnd();
835 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
837 CXXConstructorDecl *D, bool Elidable,
838 ArrayRef<Expr*> Args,
839 bool HadMultipleCandidates,
840 bool ListInitialization,
841 bool ZeroInitialization,
842 ConstructionKind ConstructKind,
843 SourceRange ParenOrBraceRange) {
844 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
846 HadMultipleCandidates, ListInitialization,
847 ZeroInitialization, ConstructKind,
851 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
852 QualType T, SourceLocation Loc,
853 CXXConstructorDecl *D, bool elidable,
854 ArrayRef<Expr*> args,
855 bool HadMultipleCandidates,
856 bool ListInitialization,
857 bool ZeroInitialization,
858 ConstructionKind ConstructKind,
859 SourceRange ParenOrBraceRange)
860 : Expr(SC, T, VK_RValue, OK_Ordinary,
861 T->isDependentType(), T->isDependentType(),
862 T->isInstantiationDependentType(),
863 T->containsUnexpandedParameterPack()),
864 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
865 NumArgs(args.size()),
866 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
867 ListInitialization(ListInitialization),
868 ZeroInitialization(ZeroInitialization),
869 ConstructKind(ConstructKind), Args(0)
872 Args = new (C) Stmt*[args.size()];
874 for (unsigned i = 0; i != args.size(); ++i) {
875 assert(args[i] && "NULL argument in CXXConstructExpr");
877 if (args[i]->isValueDependent())
878 ExprBits.ValueDependent = true;
879 if (args[i]->isInstantiationDependent())
880 ExprBits.InstantiationDependent = true;
881 if (args[i]->containsUnexpandedParameterPack())
882 ExprBits.ContainsUnexpandedParameterPack = true;
889 LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
890 LambdaCaptureKind Kind, VarDecl *Var,
891 SourceLocation EllipsisLoc)
892 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
896 Bits |= Capture_Implicit;
900 assert(Var == 0 && "'this' capture cannot have a variable!");
904 Bits |= Capture_ByCopy;
907 assert(Var && "capture must have a variable!");
910 DeclAndBits.setInt(Bits);
913 LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
914 Decl *D = DeclAndBits.getPointer();
918 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
921 LambdaExpr::LambdaExpr(QualType T,
922 SourceRange IntroducerRange,
923 LambdaCaptureDefault CaptureDefault,
924 SourceLocation CaptureDefaultLoc,
925 ArrayRef<Capture> Captures,
927 bool ExplicitResultType,
928 ArrayRef<Expr *> CaptureInits,
929 ArrayRef<VarDecl *> ArrayIndexVars,
930 ArrayRef<unsigned> ArrayIndexStarts,
931 SourceLocation ClosingBrace,
932 bool ContainsUnexpandedParameterPack)
933 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
934 T->isDependentType(), T->isDependentType(), T->isDependentType(),
935 ContainsUnexpandedParameterPack),
936 IntroducerRange(IntroducerRange),
937 CaptureDefaultLoc(CaptureDefaultLoc),
938 NumCaptures(Captures.size()),
939 CaptureDefault(CaptureDefault),
940 ExplicitParams(ExplicitParams),
941 ExplicitResultType(ExplicitResultType),
942 ClosingBrace(ClosingBrace)
944 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
945 CXXRecordDecl *Class = getLambdaClass();
946 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
948 // FIXME: Propagate "has unexpanded parameter pack" bit.
951 const ASTContext &Context = Class->getASTContext();
952 Data.NumCaptures = NumCaptures;
953 Data.NumExplicitCaptures = 0;
954 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
955 Capture *ToCapture = Data.Captures;
956 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
957 if (Captures[I].isExplicit())
958 ++Data.NumExplicitCaptures;
960 *ToCapture++ = Captures[I];
963 // Copy initialization expressions for the non-static data members.
964 Stmt **Stored = getStoredStmts();
965 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
966 *Stored++ = CaptureInits[I];
968 // Copy the body of the lambda.
969 *Stored++ = getCallOperator()->getBody();
971 // Copy the array index variables, if any.
972 HasArrayIndexVars = !ArrayIndexVars.empty();
973 if (HasArrayIndexVars) {
974 assert(ArrayIndexStarts.size() == NumCaptures);
975 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
976 sizeof(VarDecl *) * ArrayIndexVars.size());
977 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
978 sizeof(unsigned) * Captures.size());
979 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
983 LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
984 CXXRecordDecl *Class,
985 SourceRange IntroducerRange,
986 LambdaCaptureDefault CaptureDefault,
987 SourceLocation CaptureDefaultLoc,
988 ArrayRef<Capture> Captures,
990 bool ExplicitResultType,
991 ArrayRef<Expr *> CaptureInits,
992 ArrayRef<VarDecl *> ArrayIndexVars,
993 ArrayRef<unsigned> ArrayIndexStarts,
994 SourceLocation ClosingBrace,
995 bool ContainsUnexpandedParameterPack) {
996 // Determine the type of the expression (i.e., the type of the
997 // function object we're creating).
998 QualType T = Context.getTypeDeclType(Class);
1000 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
1001 if (!ArrayIndexVars.empty()) {
1002 Size += sizeof(unsigned) * (Captures.size() + 1);
1003 // Realign for following VarDecl array.
1004 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
1005 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1007 void *Mem = Context.Allocate(Size);
1008 return new (Mem) LambdaExpr(T, IntroducerRange,
1009 CaptureDefault, CaptureDefaultLoc, Captures,
1010 ExplicitParams, ExplicitResultType,
1011 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
1012 ClosingBrace, ContainsUnexpandedParameterPack);
1015 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1016 unsigned NumCaptures,
1017 unsigned NumArrayIndexVars) {
1018 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1019 if (NumArrayIndexVars)
1020 Size += sizeof(VarDecl) * NumArrayIndexVars
1021 + sizeof(unsigned) * (NumCaptures + 1);
1022 void *Mem = C.Allocate(Size);
1023 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1026 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1027 return getLambdaClass()->getLambdaData().Captures;
1030 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1031 return capture_begin() + NumCaptures;
1034 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1035 return capture_begin();
1038 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1039 struct CXXRecordDecl::LambdaDefinitionData &Data
1040 = getLambdaClass()->getLambdaData();
1041 return Data.Captures + Data.NumExplicitCaptures;
1044 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1045 return explicit_capture_end();
1048 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1049 return capture_end();
1053 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
1054 assert(HasArrayIndexVars && "No array index-var data?");
1056 unsigned Index = Iter - capture_init_begin();
1057 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1058 "Capture index out-of-range");
1059 VarDecl **IndexVars = getArrayIndexVars();
1060 unsigned *IndexStarts = getArrayIndexStarts();
1061 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1062 IndexVars + IndexStarts[Index + 1]);
1065 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1066 return getType()->getAsCXXRecordDecl();
1069 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1070 CXXRecordDecl *Record = getLambdaClass();
1071 return Record->getLambdaCallOperator();
1074 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1075 CXXRecordDecl *Record = getLambdaClass();
1076 return Record->getGenericLambdaTemplateParameterList();
1080 CompoundStmt *LambdaExpr::getBody() const {
1081 if (!getStoredStmts()[NumCaptures])
1082 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1084 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1087 bool LambdaExpr::isMutable() const {
1088 return !getCallOperator()->isConst();
1091 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1092 ArrayRef<CleanupObject> objects)
1093 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1094 subexpr->getValueKind(), subexpr->getObjectKind(),
1095 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1096 subexpr->isInstantiationDependent(),
1097 subexpr->containsUnexpandedParameterPack()),
1099 ExprWithCleanupsBits.NumObjects = objects.size();
1100 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1101 getObjectsBuffer()[i] = objects[i];
1104 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1105 ArrayRef<CleanupObject> objects) {
1106 size_t size = sizeof(ExprWithCleanups)
1107 + objects.size() * sizeof(CleanupObject);
1108 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1109 return new (buffer) ExprWithCleanups(subexpr, objects);
1112 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1113 : Expr(ExprWithCleanupsClass, empty) {
1114 ExprWithCleanupsBits.NumObjects = numObjects;
1117 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1119 unsigned numObjects) {
1120 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1121 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1122 return new (buffer) ExprWithCleanups(empty, numObjects);
1125 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1126 SourceLocation LParenLoc,
1127 ArrayRef<Expr*> Args,
1128 SourceLocation RParenLoc)
1129 : Expr(CXXUnresolvedConstructExprClass,
1130 Type->getType().getNonReferenceType(),
1131 (Type->getType()->isLValueReferenceType() ? VK_LValue
1132 :Type->getType()->isRValueReferenceType()? VK_XValue
1135 Type->getType()->isDependentType(), true, true,
1136 Type->getType()->containsUnexpandedParameterPack()),
1138 LParenLoc(LParenLoc),
1139 RParenLoc(RParenLoc),
1140 NumArgs(Args.size()) {
1141 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
1142 for (unsigned I = 0; I != Args.size(); ++I) {
1143 if (Args[I]->containsUnexpandedParameterPack())
1144 ExprBits.ContainsUnexpandedParameterPack = true;
1146 StoredArgs[I] = Args[I];
1150 CXXUnresolvedConstructExpr *
1151 CXXUnresolvedConstructExpr::Create(const ASTContext &C,
1152 TypeSourceInfo *Type,
1153 SourceLocation LParenLoc,
1154 ArrayRef<Expr*> Args,
1155 SourceLocation RParenLoc) {
1156 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1157 sizeof(Expr *) * Args.size());
1158 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1161 CXXUnresolvedConstructExpr *
1162 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
1163 Stmt::EmptyShell Empty;
1164 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1165 sizeof(Expr *) * NumArgs);
1166 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1169 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1170 return Type->getTypeLoc().getBeginLoc();
1173 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
1174 Expr *Base, QualType BaseType,
1176 SourceLocation OperatorLoc,
1177 NestedNameSpecifierLoc QualifierLoc,
1178 SourceLocation TemplateKWLoc,
1179 NamedDecl *FirstQualifierFoundInScope,
1180 DeclarationNameInfo MemberNameInfo,
1181 const TemplateArgumentListInfo *TemplateArgs)
1182 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1183 VK_LValue, OK_Ordinary, true, true, true,
1184 ((Base && Base->containsUnexpandedParameterPack()) ||
1186 QualifierLoc.getNestedNameSpecifier()
1187 ->containsUnexpandedParameterPack()) ||
1188 MemberNameInfo.containsUnexpandedParameterPack())),
1189 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1190 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
1191 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1192 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1193 MemberNameInfo(MemberNameInfo) {
1195 bool Dependent = true;
1196 bool InstantiationDependent = true;
1197 bool ContainsUnexpandedParameterPack = false;
1198 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1200 InstantiationDependent,
1201 ContainsUnexpandedParameterPack);
1202 if (ContainsUnexpandedParameterPack)
1203 ExprBits.ContainsUnexpandedParameterPack = true;
1204 } else if (TemplateKWLoc.isValid()) {
1205 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
1209 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
1210 Expr *Base, QualType BaseType,
1212 SourceLocation OperatorLoc,
1213 NestedNameSpecifierLoc QualifierLoc,
1214 NamedDecl *FirstQualifierFoundInScope,
1215 DeclarationNameInfo MemberNameInfo)
1216 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1217 VK_LValue, OK_Ordinary, true, true, true,
1218 ((Base && Base->containsUnexpandedParameterPack()) ||
1220 QualifierLoc.getNestedNameSpecifier()->
1221 containsUnexpandedParameterPack()) ||
1222 MemberNameInfo.containsUnexpandedParameterPack())),
1223 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1224 HasTemplateKWAndArgsInfo(false),
1225 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1226 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1227 MemberNameInfo(MemberNameInfo) { }
1229 CXXDependentScopeMemberExpr *
1230 CXXDependentScopeMemberExpr::Create(const ASTContext &C,
1231 Expr *Base, QualType BaseType, bool IsArrow,
1232 SourceLocation OperatorLoc,
1233 NestedNameSpecifierLoc QualifierLoc,
1234 SourceLocation TemplateKWLoc,
1235 NamedDecl *FirstQualifierFoundInScope,
1236 DeclarationNameInfo MemberNameInfo,
1237 const TemplateArgumentListInfo *TemplateArgs) {
1238 if (!TemplateArgs && !TemplateKWLoc.isValid())
1239 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1240 IsArrow, OperatorLoc,
1242 FirstQualifierFoundInScope,
1245 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1246 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1247 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1249 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1250 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1251 IsArrow, OperatorLoc,
1254 FirstQualifierFoundInScope,
1255 MemberNameInfo, TemplateArgs);
1258 CXXDependentScopeMemberExpr *
1259 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
1260 bool HasTemplateKWAndArgsInfo,
1261 unsigned NumTemplateArgs) {
1262 if (!HasTemplateKWAndArgsInfo)
1263 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
1264 0, SourceLocation(),
1265 NestedNameSpecifierLoc(), 0,
1266 DeclarationNameInfo());
1268 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
1269 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1270 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1271 CXXDependentScopeMemberExpr *E
1272 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
1273 0, SourceLocation(),
1274 NestedNameSpecifierLoc(),
1275 SourceLocation(), 0,
1276 DeclarationNameInfo(), 0);
1277 E->HasTemplateKWAndArgsInfo = true;
1281 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1285 return cast<Expr>(Base)->isImplicitCXXThis();
1288 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1289 UnresolvedSetIterator end) {
1291 NamedDecl *decl = *begin;
1292 if (isa<UnresolvedUsingValueDecl>(decl))
1294 if (isa<UsingShadowDecl>(decl))
1295 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1297 // Unresolved member expressions should only contain methods and
1298 // method templates.
1299 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1301 if (isa<FunctionTemplateDecl>(decl))
1302 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1303 if (cast<CXXMethodDecl>(decl)->isStatic())
1305 } while (++begin != end);
1310 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
1311 bool HasUnresolvedUsing,
1312 Expr *Base, QualType BaseType,
1314 SourceLocation OperatorLoc,
1315 NestedNameSpecifierLoc QualifierLoc,
1316 SourceLocation TemplateKWLoc,
1317 const DeclarationNameInfo &MemberNameInfo,
1318 const TemplateArgumentListInfo *TemplateArgs,
1319 UnresolvedSetIterator Begin,
1320 UnresolvedSetIterator End)
1321 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1322 MemberNameInfo, TemplateArgs, Begin, End,
1324 ((Base && Base->isTypeDependent()) ||
1325 BaseType->isDependentType()),
1326 ((Base && Base->isInstantiationDependent()) ||
1327 BaseType->isInstantiationDependentType()),
1328 // Contains unexpanded parameter pack
1329 ((Base && Base->containsUnexpandedParameterPack()) ||
1330 BaseType->containsUnexpandedParameterPack())),
1331 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1332 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1334 // Check whether all of the members are non-static member functions,
1335 // and if so, mark give this bound-member type instead of overload type.
1336 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1337 setType(C.BoundMemberTy);
1340 bool UnresolvedMemberExpr::isImplicitAccess() const {
1344 return cast<Expr>(Base)->isImplicitCXXThis();
1347 UnresolvedMemberExpr *
1348 UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
1349 Expr *Base, QualType BaseType, bool IsArrow,
1350 SourceLocation OperatorLoc,
1351 NestedNameSpecifierLoc QualifierLoc,
1352 SourceLocation TemplateKWLoc,
1353 const DeclarationNameInfo &MemberNameInfo,
1354 const TemplateArgumentListInfo *TemplateArgs,
1355 UnresolvedSetIterator Begin,
1356 UnresolvedSetIterator End) {
1357 std::size_t size = sizeof(UnresolvedMemberExpr);
1359 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1360 else if (TemplateKWLoc.isValid())
1361 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
1363 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1364 return new (Mem) UnresolvedMemberExpr(C,
1365 HasUnresolvedUsing, Base, BaseType,
1366 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1367 MemberNameInfo, TemplateArgs, Begin, End);
1370 UnresolvedMemberExpr *
1371 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1372 bool HasTemplateKWAndArgsInfo,
1373 unsigned NumTemplateArgs) {
1374 std::size_t size = sizeof(UnresolvedMemberExpr);
1375 if (HasTemplateKWAndArgsInfo)
1376 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1378 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1379 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1380 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1384 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1385 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1387 // If there was a nested name specifier, it names the naming class.
1388 // It can't be dependent: after all, we were actually able to do the
1390 CXXRecordDecl *Record = 0;
1391 if (getQualifier()) {
1392 const Type *T = getQualifier()->getAsType();
1393 assert(T && "qualifier in member expression does not name type");
1394 Record = T->getAsCXXRecordDecl();
1395 assert(Record && "qualifier in member expression does not name record");
1397 // Otherwise the naming class must have been the base class.
1399 QualType BaseType = getBaseType().getNonReferenceType();
1401 const PointerType *PT = BaseType->getAs<PointerType>();
1402 assert(PT && "base of arrow member access is not pointer");
1403 BaseType = PT->getPointeeType();
1406 Record = BaseType->getAsCXXRecordDecl();
1407 assert(Record && "base of member expression does not name record");
1413 SubstNonTypeTemplateParmPackExpr::
1414 SubstNonTypeTemplateParmPackExpr(QualType T,
1415 NonTypeTemplateParmDecl *Param,
1416 SourceLocation NameLoc,
1417 const TemplateArgument &ArgPack)
1418 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1419 true, true, true, true),
1420 Param(Param), Arguments(ArgPack.pack_begin()),
1421 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1423 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1424 return TemplateArgument(Arguments, NumArguments);
1427 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1428 SourceLocation NameLoc,
1430 Decl * const *Params)
1431 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1432 true, true, true, true),
1433 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1435 std::uninitialized_copy(Params, Params + NumParams,
1436 reinterpret_cast<Decl**>(this+1));
1439 FunctionParmPackExpr *
1440 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1441 ParmVarDecl *ParamPack, SourceLocation NameLoc,
1442 ArrayRef<Decl *> Params) {
1443 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1444 sizeof(ParmVarDecl*) * Params.size()))
1445 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1448 FunctionParmPackExpr *
1449 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1450 unsigned NumParams) {
1451 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1452 sizeof(ParmVarDecl*) * NumParams))
1453 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1456 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1457 ArrayRef<TypeSourceInfo *> Args,
1458 SourceLocation RParenLoc,
1460 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1461 /*TypeDependent=*/false,
1462 /*ValueDependent=*/false,
1463 /*InstantiationDependent=*/false,
1464 /*ContainsUnexpandedParameterPack=*/false),
1465 Loc(Loc), RParenLoc(RParenLoc)
1467 TypeTraitExprBits.Kind = Kind;
1468 TypeTraitExprBits.Value = Value;
1469 TypeTraitExprBits.NumArgs = Args.size();
1471 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1473 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1474 if (Args[I]->getType()->isDependentType())
1475 setValueDependent(true);
1476 if (Args[I]->getType()->isInstantiationDependentType())
1477 setInstantiationDependent(true);
1478 if (Args[I]->getType()->containsUnexpandedParameterPack())
1479 setContainsUnexpandedParameterPack(true);
1481 ToArgs[I] = Args[I];
1485 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1488 ArrayRef<TypeSourceInfo *> Args,
1489 SourceLocation RParenLoc,
1491 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1492 void *Mem = C.Allocate(Size);
1493 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1496 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1498 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1499 void *Mem = C.Allocate(Size);
1500 return new (Mem) TypeTraitExpr(EmptyShell());
1503 void ArrayTypeTraitExpr::anchor() { }