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/Basic/IdentifierTable.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "clang/AST/TypeLoc.h"
19 using namespace clang;
22 //===----------------------------------------------------------------------===//
23 // Child Iterators for iterating over subexpressions/substatements
24 //===----------------------------------------------------------------------===//
26 QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
32 QualType CXXUuidofExpr::getTypeOperand() const {
33 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
34 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
35 .getUnqualifiedType();
38 // CXXScalarValueInitExpr
39 SourceRange CXXScalarValueInitExpr::getSourceRange() const {
40 SourceLocation Start = RParenLoc;
42 Start = TypeInfo->getTypeLoc().getBeginLoc();
43 return SourceRange(Start, RParenLoc);
47 CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
48 Expr **placementArgs, unsigned numPlaceArgs,
49 SourceRange TypeIdParens, Expr *arraySize,
50 CXXConstructorDecl *constructor, bool initializer,
51 Expr **constructorArgs, unsigned numConsArgs,
52 FunctionDecl *operatorDelete,
53 bool usualArrayDeleteWantsSize, QualType ty,
54 TypeSourceInfo *AllocatedTypeInfo,
55 SourceLocation startLoc, SourceLocation endLoc,
56 SourceLocation constructorLParen,
57 SourceLocation constructorRParen)
58 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
59 ty->isDependentType(), ty->isDependentType(),
60 ty->containsUnexpandedParameterPack()),
61 GlobalNew(globalNew), Initializer(initializer),
62 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize),
63 SubExprs(0), OperatorNew(operatorNew),
64 OperatorDelete(operatorDelete), Constructor(constructor),
65 AllocatedTypeInfo(AllocatedTypeInfo), TypeIdParens(TypeIdParens),
66 StartLoc(startLoc), EndLoc(endLoc), ConstructorLParen(constructorLParen),
67 ConstructorRParen(constructorRParen) {
68 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs);
71 if (arraySize->containsUnexpandedParameterPack())
72 ExprBits.ContainsUnexpandedParameterPack = true;
74 SubExprs[i++] = arraySize;
77 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
78 if (placementArgs[j]->containsUnexpandedParameterPack())
79 ExprBits.ContainsUnexpandedParameterPack = true;
81 SubExprs[i++] = placementArgs[j];
84 for (unsigned j = 0; j < NumConstructorArgs; ++j) {
85 if (constructorArgs[j]->containsUnexpandedParameterPack())
86 ExprBits.ContainsUnexpandedParameterPack = true;
88 SubExprs[i++] = constructorArgs[j];
92 void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
93 unsigned numPlaceArgs, unsigned numConsArgs){
94 assert(SubExprs == 0 && "SubExprs already allocated");
96 NumPlacementArgs = numPlaceArgs;
97 NumConstructorArgs = numConsArgs;
99 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
100 SubExprs = new (C) Stmt*[TotalSize];
105 QualType CXXDeleteExpr::getDestroyedType() const {
106 const Expr *Arg = getArgument();
107 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
108 if (ICE->getCastKind() != CK_UserDefinedConversion &&
109 ICE->getType()->isVoidPointerType())
110 Arg = ICE->getSubExpr();
114 // The type-to-delete may not be a pointer if it's a dependent type.
115 const QualType ArgType = Arg->getType();
117 if (ArgType->isDependentType() && !ArgType->isPointerType())
120 return ArgType->getAs<PointerType>()->getPointeeType();
123 // CXXPseudoDestructorExpr
124 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
127 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
130 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
131 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
132 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
133 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
134 PseudoDestructorTypeStorage DestroyedType)
135 : Expr(CXXPseudoDestructorExprClass,
136 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
137 FunctionProtoType::ExtProtoInfo())),
138 VK_RValue, OK_Ordinary,
139 /*isTypeDependent=*/(Base->isTypeDependent() ||
140 (DestroyedType.getTypeSourceInfo() &&
141 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
142 /*isValueDependent=*/Base->isValueDependent(),
143 // ContainsUnexpandedParameterPack
144 (Base->containsUnexpandedParameterPack() ||
146 QualifierLoc.getNestedNameSpecifier()
147 ->containsUnexpandedParameterPack()) ||
149 ScopeType->getType()->containsUnexpandedParameterPack()) ||
150 (DestroyedType.getTypeSourceInfo() &&
151 DestroyedType.getTypeSourceInfo()->getType()
152 ->containsUnexpandedParameterPack()))),
153 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
154 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
155 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
156 DestroyedType(DestroyedType) { }
158 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
159 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
160 return TInfo->getType();
165 SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
166 SourceLocation End = DestroyedType.getLocation();
167 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
168 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
169 return SourceRange(Base->getLocStart(), End);
173 // UnresolvedLookupExpr
174 UnresolvedLookupExpr *
175 UnresolvedLookupExpr::Create(ASTContext &C,
176 CXXRecordDecl *NamingClass,
177 NestedNameSpecifier *Qualifier,
178 SourceRange QualifierRange,
179 const DeclarationNameInfo &NameInfo,
181 const TemplateArgumentListInfo &Args,
182 UnresolvedSetIterator Begin,
183 UnresolvedSetIterator End)
185 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
186 ExplicitTemplateArgumentList::sizeFor(Args));
187 return new (Mem) UnresolvedLookupExpr(C, NamingClass,
188 Qualifier, QualifierRange, NameInfo,
189 ADL, /*Overload*/ true, &Args,
193 UnresolvedLookupExpr *
194 UnresolvedLookupExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
195 unsigned NumTemplateArgs) {
196 std::size_t size = sizeof(UnresolvedLookupExpr);
197 if (HasExplicitTemplateArgs)
198 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
200 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
201 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
202 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
206 OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
207 NestedNameSpecifier *Qualifier, SourceRange QRange,
208 const DeclarationNameInfo &NameInfo,
209 const TemplateArgumentListInfo *TemplateArgs,
210 UnresolvedSetIterator Begin,
211 UnresolvedSetIterator End,
213 bool KnownContainsUnexpandedParameterPack)
214 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
216 (KnownContainsUnexpandedParameterPack ||
217 NameInfo.containsUnexpandedParameterPack() ||
218 (Qualifier && Qualifier->containsUnexpandedParameterPack()))),
219 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
220 Qualifier(Qualifier), QualifierRange(QRange),
221 HasExplicitTemplateArgs(TemplateArgs != 0)
223 NumResults = End - Begin;
225 // Determine whether this expression is type-dependent.
226 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
227 if ((*I)->getDeclContext()->isDependentContext() ||
228 isa<UnresolvedUsingValueDecl>(*I)) {
229 ExprBits.TypeDependent = true;
230 ExprBits.ValueDependent = true;
234 Results = static_cast<DeclAccessPair *>(
235 C.Allocate(sizeof(DeclAccessPair) * NumResults,
236 llvm::alignOf<DeclAccessPair>()));
237 memcpy(Results, &*Begin.getIterator(),
238 NumResults * sizeof(DeclAccessPair));
241 // If we have explicit template arguments, check for dependent
242 // template arguments and whether they contain any unexpanded pack
245 bool Dependent = false;
246 bool ContainsUnexpandedParameterPack = false;
247 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
248 ContainsUnexpandedParameterPack);
251 ExprBits.TypeDependent = true;
252 ExprBits.ValueDependent = true;
254 if (ContainsUnexpandedParameterPack)
255 ExprBits.ContainsUnexpandedParameterPack = true;
258 if (isTypeDependent())
259 setType(C.DependentTy);
262 void OverloadExpr::initializeResults(ASTContext &C,
263 UnresolvedSetIterator Begin,
264 UnresolvedSetIterator End) {
265 assert(Results == 0 && "Results already initialized!");
266 NumResults = End - Begin;
268 Results = static_cast<DeclAccessPair *>(
269 C.Allocate(sizeof(DeclAccessPair) * NumResults,
271 llvm::alignOf<DeclAccessPair>()));
272 memcpy(Results, &*Begin.getIterator(),
273 NumResults * sizeof(DeclAccessPair));
277 CXXRecordDecl *OverloadExpr::getNamingClass() const {
278 if (isa<UnresolvedLookupExpr>(this))
279 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
281 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
284 // DependentScopeDeclRefExpr
285 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
286 NestedNameSpecifierLoc QualifierLoc,
287 const DeclarationNameInfo &NameInfo,
288 const TemplateArgumentListInfo *Args)
289 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
291 (NameInfo.containsUnexpandedParameterPack() ||
293 QualifierLoc.getNestedNameSpecifier()
294 ->containsUnexpandedParameterPack()))),
295 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
296 HasExplicitTemplateArgs(Args != 0)
299 bool Dependent = true;
300 bool ContainsUnexpandedParameterPack
301 = ExprBits.ContainsUnexpandedParameterPack;
303 reinterpret_cast<ExplicitTemplateArgumentList*>(this+1)
304 ->initializeFrom(*Args, Dependent, ContainsUnexpandedParameterPack);
305 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
309 DependentScopeDeclRefExpr *
310 DependentScopeDeclRefExpr::Create(ASTContext &C,
311 NestedNameSpecifierLoc QualifierLoc,
312 const DeclarationNameInfo &NameInfo,
313 const TemplateArgumentListInfo *Args) {
314 std::size_t size = sizeof(DependentScopeDeclRefExpr);
316 size += ExplicitTemplateArgumentList::sizeFor(*Args);
317 void *Mem = C.Allocate(size);
318 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
322 DependentScopeDeclRefExpr *
323 DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
324 bool HasExplicitTemplateArgs,
325 unsigned NumTemplateArgs) {
326 std::size_t size = sizeof(DependentScopeDeclRefExpr);
327 if (HasExplicitTemplateArgs)
328 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
329 void *Mem = C.Allocate(size);
330 DependentScopeDeclRefExpr *E
331 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
332 DeclarationNameInfo(), 0);
333 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
337 SourceRange CXXConstructExpr::getSourceRange() const {
338 if (isa<CXXTemporaryObjectExpr>(this))
339 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
341 if (ParenRange.isValid())
342 return SourceRange(Loc, ParenRange.getEnd());
344 SourceLocation End = Loc;
345 for (unsigned I = getNumArgs(); I > 0; --I) {
346 const Expr *Arg = getArg(I-1);
347 if (!Arg->isDefaultArgument()) {
348 SourceLocation NewEnd = Arg->getLocEnd();
349 if (NewEnd.isValid()) {
356 return SourceRange(Loc, End);
359 SourceRange CXXOperatorCallExpr::getSourceRange() const {
360 OverloadedOperatorKind Kind = getOperator();
361 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
362 if (getNumArgs() == 1)
364 return SourceRange(getOperatorLoc(),
365 getArg(0)->getSourceRange().getEnd());
368 return SourceRange(getArg(0)->getSourceRange().getEnd(),
370 } else if (Kind == OO_Call) {
371 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
372 } else if (Kind == OO_Subscript) {
373 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
374 } else if (getNumArgs() == 1) {
375 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
376 } else if (getNumArgs() == 2) {
377 return SourceRange(getArg(0)->getSourceRange().getBegin(),
378 getArg(1)->getSourceRange().getEnd());
380 return SourceRange();
384 Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
385 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
386 return MemExpr->getBase();
388 // FIXME: Will eventually need to cope with member pointers.
392 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
393 Expr* ThisArg = getImplicitObjectArgument();
397 if (ThisArg->getType()->isAnyPointerType())
398 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
400 return ThisArg->getType()->getAsCXXRecordDecl();
404 //===----------------------------------------------------------------------===//
406 //===----------------------------------------------------------------------===//
408 /// getCastName - Get the name of the C++ cast being used, e.g.,
409 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
410 /// "const_cast". The returned pointer must not be freed.
411 const char *CXXNamedCastExpr::getCastName() const {
412 switch (getStmtClass()) {
413 case CXXStaticCastExprClass: return "static_cast";
414 case CXXDynamicCastExprClass: return "dynamic_cast";
415 case CXXReinterpretCastExprClass: return "reinterpret_cast";
416 case CXXConstCastExprClass: return "const_cast";
417 default: return "<invalid cast>";
421 CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
423 CastKind K, Expr *Op,
424 const CXXCastPath *BasePath,
425 TypeSourceInfo *WrittenTy,
427 SourceLocation RParenLoc) {
428 unsigned PathSize = (BasePath ? BasePath->size() : 0);
429 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
430 + PathSize * sizeof(CXXBaseSpecifier*));
431 CXXStaticCastExpr *E =
432 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
434 if (PathSize) E->setCastPath(*BasePath);
438 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
441 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
442 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
445 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
447 CastKind K, Expr *Op,
448 const CXXCastPath *BasePath,
449 TypeSourceInfo *WrittenTy,
451 SourceLocation RParenLoc) {
452 unsigned PathSize = (BasePath ? BasePath->size() : 0);
453 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
454 + PathSize * sizeof(CXXBaseSpecifier*));
455 CXXDynamicCastExpr *E =
456 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
458 if (PathSize) E->setCastPath(*BasePath);
462 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
465 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
466 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
469 CXXReinterpretCastExpr *
470 CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
471 CastKind K, Expr *Op,
472 const CXXCastPath *BasePath,
473 TypeSourceInfo *WrittenTy, SourceLocation L,
474 SourceLocation RParenLoc) {
475 unsigned PathSize = (BasePath ? BasePath->size() : 0);
477 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
478 CXXReinterpretCastExpr *E =
479 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
481 if (PathSize) E->setCastPath(*BasePath);
485 CXXReinterpretCastExpr *
486 CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
487 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
488 + PathSize * sizeof(CXXBaseSpecifier*));
489 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
492 CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
493 ExprValueKind VK, Expr *Op,
494 TypeSourceInfo *WrittenTy,
496 SourceLocation RParenLoc) {
497 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
500 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
501 return new (C) CXXConstCastExpr(EmptyShell());
504 CXXFunctionalCastExpr *
505 CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
506 TypeSourceInfo *Written, SourceLocation L,
507 CastKind K, Expr *Op, const CXXCastPath *BasePath,
509 unsigned PathSize = (BasePath ? BasePath->size() : 0);
510 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
511 + PathSize * sizeof(CXXBaseSpecifier*));
512 CXXFunctionalCastExpr *E =
513 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
514 if (PathSize) E->setCastPath(*BasePath);
518 CXXFunctionalCastExpr *
519 CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
520 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
521 + PathSize * sizeof(CXXBaseSpecifier*));
522 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
527 CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
528 ParmVarDecl *Param, Expr *SubExpr) {
529 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
530 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
534 CXXTemporary *CXXTemporary::Create(ASTContext &C,
535 const CXXDestructorDecl *Destructor) {
536 return new (C) CXXTemporary(Destructor);
539 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
542 assert(SubExpr->getType()->isRecordType() &&
543 "Expression bound to a temporary must have record type!");
545 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
548 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
549 CXXConstructorDecl *Cons,
550 TypeSourceInfo *Type,
553 SourceRange parenRange,
554 bool ZeroInitialization)
555 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
556 Type->getType().getNonReferenceType(),
557 Type->getTypeLoc().getBeginLoc(),
558 Cons, false, Args, NumArgs, ZeroInitialization,
559 CXXConstructExpr::CK_Complete, parenRange),
563 SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
564 return SourceRange(Type->getTypeLoc().getBeginLoc(),
565 getParenRange().getEnd());
568 CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
570 CXXConstructorDecl *D, bool Elidable,
571 Expr **Args, unsigned NumArgs,
572 bool ZeroInitialization,
573 ConstructionKind ConstructKind,
574 SourceRange ParenRange) {
575 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
576 Elidable, Args, NumArgs, ZeroInitialization,
577 ConstructKind, ParenRange);
580 CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
582 CXXConstructorDecl *D, bool elidable,
583 Expr **args, unsigned numargs,
584 bool ZeroInitialization,
585 ConstructionKind ConstructKind,
586 SourceRange ParenRange)
587 : Expr(SC, T, VK_RValue, OK_Ordinary,
588 T->isDependentType(), T->isDependentType(),
589 T->containsUnexpandedParameterPack()),
590 Constructor(D), Loc(Loc), ParenRange(ParenRange), Elidable(elidable),
591 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
592 Args(0), NumArgs(numargs)
595 Args = new (C) Stmt*[NumArgs];
597 for (unsigned i = 0; i != NumArgs; ++i) {
598 assert(args[i] && "NULL argument in CXXConstructExpr");
600 if (args[i]->isValueDependent())
601 ExprBits.ValueDependent = true;
602 if (args[i]->containsUnexpandedParameterPack())
603 ExprBits.ContainsUnexpandedParameterPack = true;
610 ExprWithCleanups::ExprWithCleanups(ASTContext &C,
612 CXXTemporary **temps,
614 : Expr(ExprWithCleanupsClass, subexpr->getType(),
615 subexpr->getValueKind(), subexpr->getObjectKind(),
616 subexpr->isTypeDependent(), subexpr->isValueDependent(),
617 subexpr->containsUnexpandedParameterPack()),
618 SubExpr(subexpr), Temps(0), NumTemps(0) {
620 setNumTemporaries(C, numtemps);
621 for (unsigned i = 0; i != numtemps; ++i)
626 void ExprWithCleanups::setNumTemporaries(ASTContext &C, unsigned N) {
627 assert(Temps == 0 && "Cannot resize with this");
629 Temps = new (C) CXXTemporary*[NumTemps];
633 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C,
635 CXXTemporary **Temps,
637 return new (C) ExprWithCleanups(C, SubExpr, Temps, NumTemps);
640 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
641 SourceLocation LParenLoc,
644 SourceLocation RParenLoc)
645 : Expr(CXXUnresolvedConstructExprClass,
646 Type->getType().getNonReferenceType(),
647 VK_LValue, OK_Ordinary,
648 Type->getType()->isDependentType(), true,
649 Type->getType()->containsUnexpandedParameterPack()),
651 LParenLoc(LParenLoc),
652 RParenLoc(RParenLoc),
654 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
655 for (unsigned I = 0; I != NumArgs; ++I) {
656 if (Args[I]->containsUnexpandedParameterPack())
657 ExprBits.ContainsUnexpandedParameterPack = true;
659 StoredArgs[I] = Args[I];
663 CXXUnresolvedConstructExpr *
664 CXXUnresolvedConstructExpr::Create(ASTContext &C,
665 TypeSourceInfo *Type,
666 SourceLocation LParenLoc,
669 SourceLocation RParenLoc) {
670 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
671 sizeof(Expr *) * NumArgs);
672 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
673 Args, NumArgs, RParenLoc);
676 CXXUnresolvedConstructExpr *
677 CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
678 Stmt::EmptyShell Empty;
679 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
680 sizeof(Expr *) * NumArgs);
681 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
684 SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
685 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
688 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
689 Expr *Base, QualType BaseType,
691 SourceLocation OperatorLoc,
692 NestedNameSpecifier *Qualifier,
693 SourceRange QualifierRange,
694 NamedDecl *FirstQualifierFoundInScope,
695 DeclarationNameInfo MemberNameInfo,
696 const TemplateArgumentListInfo *TemplateArgs)
697 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
698 VK_LValue, OK_Ordinary, true, true,
699 ((Base && Base->containsUnexpandedParameterPack()) ||
700 (Qualifier && Qualifier->containsUnexpandedParameterPack()) ||
701 MemberNameInfo.containsUnexpandedParameterPack())),
702 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
703 HasExplicitTemplateArgs(TemplateArgs != 0),
704 OperatorLoc(OperatorLoc),
705 Qualifier(Qualifier), QualifierRange(QualifierRange),
706 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
707 MemberNameInfo(MemberNameInfo) {
709 bool Dependent = true;
710 bool ContainsUnexpandedParameterPack = false;
711 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
712 ContainsUnexpandedParameterPack);
713 if (ContainsUnexpandedParameterPack)
714 ExprBits.ContainsUnexpandedParameterPack = true;
718 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
719 Expr *Base, QualType BaseType,
721 SourceLocation OperatorLoc,
722 NestedNameSpecifier *Qualifier,
723 SourceRange QualifierRange,
724 NamedDecl *FirstQualifierFoundInScope,
725 DeclarationNameInfo MemberNameInfo)
726 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
727 VK_LValue, OK_Ordinary, true, true,
728 ((Base && Base->containsUnexpandedParameterPack()) ||
729 (Qualifier && Qualifier->containsUnexpandedParameterPack()) ||
730 MemberNameInfo.containsUnexpandedParameterPack())),
731 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
732 HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc),
733 Qualifier(Qualifier), QualifierRange(QualifierRange),
734 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
735 MemberNameInfo(MemberNameInfo) { }
737 CXXDependentScopeMemberExpr *
738 CXXDependentScopeMemberExpr::Create(ASTContext &C,
739 Expr *Base, QualType BaseType, bool IsArrow,
740 SourceLocation OperatorLoc,
741 NestedNameSpecifier *Qualifier,
742 SourceRange QualifierRange,
743 NamedDecl *FirstQualifierFoundInScope,
744 DeclarationNameInfo MemberNameInfo,
745 const TemplateArgumentListInfo *TemplateArgs) {
747 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
748 IsArrow, OperatorLoc,
749 Qualifier, QualifierRange,
750 FirstQualifierFoundInScope,
753 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
755 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
757 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
758 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
759 IsArrow, OperatorLoc,
760 Qualifier, QualifierRange,
761 FirstQualifierFoundInScope,
762 MemberNameInfo, TemplateArgs);
765 CXXDependentScopeMemberExpr *
766 CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
767 bool HasExplicitTemplateArgs,
768 unsigned NumTemplateArgs) {
769 if (!HasExplicitTemplateArgs)
770 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
771 0, SourceLocation(), 0,
773 DeclarationNameInfo());
775 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
776 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
777 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
778 CXXDependentScopeMemberExpr *E
779 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
780 0, SourceLocation(), 0,
782 DeclarationNameInfo(), 0);
783 E->HasExplicitTemplateArgs = true;
787 UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
788 bool HasUnresolvedUsing,
789 Expr *Base, QualType BaseType,
791 SourceLocation OperatorLoc,
792 NestedNameSpecifier *Qualifier,
793 SourceRange QualifierRange,
794 const DeclarationNameInfo &MemberNameInfo,
795 const TemplateArgumentListInfo *TemplateArgs,
796 UnresolvedSetIterator Begin,
797 UnresolvedSetIterator End)
798 : OverloadExpr(UnresolvedMemberExprClass, C,
799 Qualifier, QualifierRange, MemberNameInfo,
800 TemplateArgs, Begin, End,
802 ((Base && Base->isTypeDependent()) ||
803 BaseType->isDependentType()),
804 // Contains unexpanded parameter pack
805 ((Base && Base->containsUnexpandedParameterPack()) ||
806 BaseType->containsUnexpandedParameterPack())),
807 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
808 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
811 UnresolvedMemberExpr *
812 UnresolvedMemberExpr::Create(ASTContext &C,
813 bool HasUnresolvedUsing,
814 Expr *Base, QualType BaseType, bool IsArrow,
815 SourceLocation OperatorLoc,
816 NestedNameSpecifier *Qualifier,
817 SourceRange QualifierRange,
818 const DeclarationNameInfo &MemberNameInfo,
819 const TemplateArgumentListInfo *TemplateArgs,
820 UnresolvedSetIterator Begin,
821 UnresolvedSetIterator End) {
822 std::size_t size = sizeof(UnresolvedMemberExpr);
824 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
826 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
827 return new (Mem) UnresolvedMemberExpr(C,
828 HasUnresolvedUsing, Base, BaseType,
829 IsArrow, OperatorLoc, Qualifier, QualifierRange,
830 MemberNameInfo, TemplateArgs, Begin, End);
833 UnresolvedMemberExpr *
834 UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
835 unsigned NumTemplateArgs) {
836 std::size_t size = sizeof(UnresolvedMemberExpr);
837 if (HasExplicitTemplateArgs)
838 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
840 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
841 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
842 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
846 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
847 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
849 // If there was a nested name specifier, it names the naming class.
850 // It can't be dependent: after all, we were actually able to do the
852 CXXRecordDecl *Record = 0;
853 if (getQualifier()) {
854 const Type *T = getQualifier()->getAsType();
855 assert(T && "qualifier in member expression does not name type");
856 Record = T->getAsCXXRecordDecl();
857 assert(Record && "qualifier in member expression does not name record");
859 // Otherwise the naming class must have been the base class.
861 QualType BaseType = getBaseType().getNonReferenceType();
863 const PointerType *PT = BaseType->getAs<PointerType>();
864 assert(PT && "base of arrow member access is not pointer");
865 BaseType = PT->getPointeeType();
868 Record = BaseType->getAsCXXRecordDecl();
869 assert(Record && "base of member expression does not name record");
875 SubstNonTypeTemplateParmPackExpr::
876 SubstNonTypeTemplateParmPackExpr(QualType T,
877 NonTypeTemplateParmDecl *Param,
878 SourceLocation NameLoc,
879 const TemplateArgument &ArgPack)
880 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
882 Param(Param), Arguments(ArgPack.pack_begin()),
883 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
885 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
886 return TemplateArgument(Arguments, NumArguments);