blob: 526c484677bc9532fb7f2e83ba406eb124b4ad17 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000021
Ted Kremeneka758d092007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000026QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
30}
31
Francois Pichet01b7c302010-09-08 12:20:18 +000032QualType CXXUuidofExpr::getTypeOperand() const {
33 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
34 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
35 .getUnqualifiedType();
36}
37
Douglas Gregored8abf12010-07-08 06:14:04 +000038// CXXScalarValueInitExpr
Douglas Gregorab6677e2010-09-08 00:15:04 +000039SourceRange CXXScalarValueInitExpr::getSourceRange() const {
40 SourceLocation Start = RParenLoc;
41 if (TypeInfo)
42 Start = TypeInfo->getTypeLoc().getBeginLoc();
43 return SourceRange(Start, RParenLoc);
44}
45
Sebastian Redl4c5d3202008-11-21 19:14:01 +000046// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000047CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000048 Expr **placementArgs, unsigned numPlaceArgs,
Douglas Gregor4bd40312010-07-13 15:54:32 +000049 SourceRange TypeIdParens, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000050 CXXConstructorDecl *constructor, bool initializer,
51 Expr **constructorArgs, unsigned numConsArgs,
John McCall6ec278d2011-01-27 09:37:56 +000052 FunctionDecl *operatorDelete,
53 bool usualArrayDeleteWantsSize, QualType ty,
Douglas Gregor1bb2a932010-09-07 21:49:58 +000054 TypeSourceInfo *AllocatedTypeInfo,
Chandler Carruth428edaf2010-10-25 08:47:36 +000055 SourceLocation startLoc, SourceLocation endLoc,
56 SourceLocation constructorLParen,
57 SourceLocation constructorRParen)
John McCallf89e55a2010-11-18 06:31:45 +000058 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000059 ty->isDependentType(), ty->isDependentType(),
60 ty->containsUnexpandedParameterPack()),
John McCall6ec278d2011-01-27 09:37:56 +000061 GlobalNew(globalNew), Initializer(initializer),
62 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize),
63 SubExprs(0), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000064 OperatorDelete(operatorDelete), Constructor(constructor),
Douglas Gregor1bb2a932010-09-07 21:49:58 +000065 AllocatedTypeInfo(AllocatedTypeInfo), TypeIdParens(TypeIdParens),
Chandler Carruth428edaf2010-10-25 08:47:36 +000066 StartLoc(startLoc), EndLoc(endLoc), ConstructorLParen(constructorLParen),
67 ConstructorRParen(constructorRParen) {
Chris Lattner59218632010-05-10 01:22:27 +000068 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000069 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000070 if (Array) {
71 if (arraySize->containsUnexpandedParameterPack())
72 ExprBits.ContainsUnexpandedParameterPack = true;
73
Sebastian Redlcee63fb2008-12-02 14:43:59 +000074 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000075 }
76
77 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
78 if (placementArgs[j]->containsUnexpandedParameterPack())
79 ExprBits.ContainsUnexpandedParameterPack = true;
80
Sebastian Redl4c5d3202008-11-21 19:14:01 +000081 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000082 }
83
84 for (unsigned j = 0; j < NumConstructorArgs; ++j) {
85 if (constructorArgs[j]->containsUnexpandedParameterPack())
86 ExprBits.ContainsUnexpandedParameterPack = true;
87
Sebastian Redl4c5d3202008-11-21 19:14:01 +000088 SubExprs[i++] = constructorArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000089 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +000090}
91
Chris Lattner59218632010-05-10 01:22:27 +000092void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
93 unsigned numPlaceArgs, unsigned numConsArgs){
94 assert(SubExprs == 0 && "SubExprs already allocated");
95 Array = isArray;
96 NumPlacementArgs = numPlaceArgs;
97 NumConstructorArgs = numConsArgs;
98
99 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
100 SubExprs = new (C) Stmt*[TotalSize];
101}
102
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000103bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000104 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000105 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000106}
Chris Lattner59218632010-05-10 01:22:27 +0000107
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000108// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000109QualType CXXDeleteExpr::getDestroyedType() const {
110 const Expr *Arg = getArgument();
111 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
112 if (ICE->getCastKind() != CK_UserDefinedConversion &&
113 ICE->getType()->isVoidPointerType())
114 Arg = ICE->getSubExpr();
115 else
116 break;
117 }
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000118 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000119 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000120
121 if (ArgType->isDependentType() && !ArgType->isPointerType())
122 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000123
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000124 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000125}
126
Douglas Gregora71d8192009-09-04 17:36:40 +0000127// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000128PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
129 : Type(Info)
130{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000131 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000132}
133
John McCalle23cf432010-12-14 08:05:40 +0000134CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000135 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
136 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
137 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
138 PseudoDestructorTypeStorage DestroyedType)
John McCalle23cf432010-12-14 08:05:40 +0000139 : Expr(CXXPseudoDestructorExprClass,
140 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
141 FunctionProtoType::ExtProtoInfo())),
142 VK_RValue, OK_Ordinary,
143 /*isTypeDependent=*/(Base->isTypeDependent() ||
144 (DestroyedType.getTypeSourceInfo() &&
145 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000146 /*isValueDependent=*/Base->isValueDependent(),
147 // ContainsUnexpandedParameterPack
148 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000149 (QualifierLoc &&
150 QualifierLoc.getNestedNameSpecifier()
151 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000152 (ScopeType &&
153 ScopeType->getType()->containsUnexpandedParameterPack()) ||
154 (DestroyedType.getTypeSourceInfo() &&
155 DestroyedType.getTypeSourceInfo()->getType()
156 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000157 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000158 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000159 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
160 DestroyedType(DestroyedType) { }
161
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000162QualType CXXPseudoDestructorExpr::getDestroyedType() const {
163 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
164 return TInfo->getType();
165
166 return QualType();
167}
168
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000169SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000170 SourceLocation End = DestroyedType.getLocation();
171 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000172 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000173 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000174}
175
176
John McCallba135432009-11-21 08:51:07 +0000177// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000178UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000179UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000180 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000181 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000182 const DeclarationNameInfo &NameInfo,
183 bool ADL,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000184 const TemplateArgumentListInfo &Args,
185 UnresolvedSetIterator Begin,
186 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000187{
188 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
189 ExplicitTemplateArgumentList::sizeFor(Args));
Douglas Gregor4c9be892011-02-28 20:01:57 +0000190 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000191 ADL, /*Overload*/ true, &Args,
192 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000193}
194
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000195UnresolvedLookupExpr *
Douglas Gregordef03542011-02-04 12:01:24 +0000196UnresolvedLookupExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
197 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000198 std::size_t size = sizeof(UnresolvedLookupExpr);
Douglas Gregordef03542011-02-04 12:01:24 +0000199 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000200 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
201
Chris Lattner32488542010-10-30 05:14:06 +0000202 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000203 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Douglas Gregordef03542011-02-04 12:01:24 +0000204 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000205 return E;
206}
207
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000208OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000209 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000210 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000211 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000212 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000213 UnresolvedSetIterator End,
214 bool KnownDependent,
215 bool KnownContainsUnexpandedParameterPack)
216 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
217 KnownDependent,
218 (KnownContainsUnexpandedParameterPack ||
219 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000220 (QualifierLoc &&
221 QualifierLoc.getNestedNameSpecifier()
222 ->containsUnexpandedParameterPack()))),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000223 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
Douglas Gregor4c9be892011-02-28 20:01:57 +0000224 QualifierLoc(QualifierLoc), HasExplicitTemplateArgs(TemplateArgs != 0)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000225{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000226 NumResults = End - Begin;
227 if (NumResults) {
228 // Determine whether this expression is type-dependent.
229 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
230 if ((*I)->getDeclContext()->isDependentContext() ||
231 isa<UnresolvedUsingValueDecl>(*I)) {
232 ExprBits.TypeDependent = true;
233 ExprBits.ValueDependent = true;
234 }
235 }
236
237 Results = static_cast<DeclAccessPair *>(
238 C.Allocate(sizeof(DeclAccessPair) * NumResults,
239 llvm::alignOf<DeclAccessPair>()));
240 memcpy(Results, &*Begin.getIterator(),
241 NumResults * sizeof(DeclAccessPair));
242 }
243
244 // If we have explicit template arguments, check for dependent
245 // template arguments and whether they contain any unexpanded pack
246 // expansions.
247 if (TemplateArgs) {
248 bool Dependent = false;
249 bool ContainsUnexpandedParameterPack = false;
250 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
251 ContainsUnexpandedParameterPack);
252
253 if (Dependent) {
254 ExprBits.TypeDependent = true;
255 ExprBits.ValueDependent = true;
256 }
257 if (ContainsUnexpandedParameterPack)
258 ExprBits.ContainsUnexpandedParameterPack = true;
259 }
260
261 if (isTypeDependent())
262 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000263}
264
265void OverloadExpr::initializeResults(ASTContext &C,
266 UnresolvedSetIterator Begin,
267 UnresolvedSetIterator End) {
268 assert(Results == 0 && "Results already initialized!");
269 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000270 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000271 Results = static_cast<DeclAccessPair *>(
272 C.Allocate(sizeof(DeclAccessPair) * NumResults,
273
274 llvm::alignOf<DeclAccessPair>()));
275 memcpy(Results, &*Begin.getIterator(),
276 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000277 }
278}
279
John McCalle9ee23e2010-04-22 18:44:12 +0000280CXXRecordDecl *OverloadExpr::getNamingClass() const {
281 if (isa<UnresolvedLookupExpr>(this))
282 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
283 else
284 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
285}
286
John McCall865d4472009-11-19 22:55:06 +0000287// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000288DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000289 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000290 const DeclarationNameInfo &NameInfo,
291 const TemplateArgumentListInfo *Args)
292 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
293 true, true,
294 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000295 (QualifierLoc &&
296 QualifierLoc.getNestedNameSpecifier()
297 ->containsUnexpandedParameterPack()))),
298 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000299 HasExplicitTemplateArgs(Args != 0)
300{
301 if (Args) {
302 bool Dependent = true;
303 bool ContainsUnexpandedParameterPack
304 = ExprBits.ContainsUnexpandedParameterPack;
305
306 reinterpret_cast<ExplicitTemplateArgumentList*>(this+1)
307 ->initializeFrom(*Args, Dependent, ContainsUnexpandedParameterPack);
308 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
309 }
310}
311
John McCallf7a1a742009-11-24 19:00:30 +0000312DependentScopeDeclRefExpr *
313DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000314 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000315 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000316 const TemplateArgumentListInfo *Args) {
317 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000318 if (Args)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000319 size += ExplicitTemplateArgumentList::sizeFor(*Args);
320 void *Mem = C.Allocate(size);
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000321 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000322 NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000323}
324
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000325DependentScopeDeclRefExpr *
326DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Douglas Gregordef03542011-02-04 12:01:24 +0000327 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000328 unsigned NumTemplateArgs) {
329 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Douglas Gregordef03542011-02-04 12:01:24 +0000330 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000331 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
332 void *Mem = C.Allocate(size);
Douglas Gregordef03542011-02-04 12:01:24 +0000333 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000334 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Douglas Gregordef03542011-02-04 12:01:24 +0000335 DeclarationNameInfo(), 0);
336 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
337 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000338}
339
Chandler Carruth428edaf2010-10-25 08:47:36 +0000340SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000341 if (isa<CXXTemporaryObjectExpr>(this))
342 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
343
Douglas Gregor40749ee2010-11-03 00:35:38 +0000344 if (ParenRange.isValid())
345 return SourceRange(Loc, ParenRange.getEnd());
346
347 SourceLocation End = Loc;
348 for (unsigned I = getNumArgs(); I > 0; --I) {
349 const Expr *Arg = getArg(I-1);
350 if (!Arg->isDefaultArgument()) {
351 SourceLocation NewEnd = Arg->getLocEnd();
352 if (NewEnd.isValid()) {
353 End = NewEnd;
354 break;
355 }
356 }
357 }
358
359 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000360}
361
Douglas Gregorb4609802008-11-14 16:09:21 +0000362SourceRange CXXOperatorCallExpr::getSourceRange() const {
363 OverloadedOperatorKind Kind = getOperator();
364 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
365 if (getNumArgs() == 1)
366 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000367 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000368 getArg(0)->getSourceRange().getEnd());
369 else
370 // Postfix operator
Chandler Carruthd7650612011-04-02 09:47:38 +0000371 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000372 getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000373 } else if (Kind == OO_Arrow) {
374 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000375 } else if (Kind == OO_Call) {
376 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
377 } else if (Kind == OO_Subscript) {
378 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
379 } else if (getNumArgs() == 1) {
380 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
381 } else if (getNumArgs() == 2) {
382 return SourceRange(getArg(0)->getSourceRange().getBegin(),
383 getArg(1)->getSourceRange().getEnd());
384 } else {
385 return SourceRange();
386 }
387}
388
Ted Kremenekb2771592011-03-30 17:41:19 +0000389Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
390 if (const MemberExpr *MemExpr =
391 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-12-22 05:46:06 +0000392 return MemExpr->getBase();
393
394 // FIXME: Will eventually need to cope with member pointers.
395 return 0;
396}
397
Ted Kremenekb2771592011-03-30 17:41:19 +0000398CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
399 if (const MemberExpr *MemExpr =
400 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
401 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
402
403 // FIXME: Will eventually need to cope with member pointers.
404 return 0;
405}
406
407
Chandler Carruth007a9b12010-10-27 06:55:41 +0000408CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
409 Expr* ThisArg = getImplicitObjectArgument();
410 if (!ThisArg)
411 return 0;
412
413 if (ThisArg->getType()->isAnyPointerType())
414 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
415
416 return ThisArg->getType()->getAsCXXRecordDecl();
417}
418
Douglas Gregor00b98c22009-11-12 15:31:47 +0000419
Douglas Gregor49badde2008-10-27 19:41:14 +0000420//===----------------------------------------------------------------------===//
421// Named casts
422//===----------------------------------------------------------------------===//
423
424/// getCastName - Get the name of the C++ cast being used, e.g.,
425/// "static_cast", "dynamic_cast", "reinterpret_cast", or
426/// "const_cast". The returned pointer must not be freed.
427const char *CXXNamedCastExpr::getCastName() const {
428 switch (getStmtClass()) {
429 case CXXStaticCastExprClass: return "static_cast";
430 case CXXDynamicCastExprClass: return "dynamic_cast";
431 case CXXReinterpretCastExprClass: return "reinterpret_cast";
432 case CXXConstCastExprClass: return "const_cast";
433 default: return "<invalid cast>";
434 }
435}
Douglas Gregor506ae412009-01-16 18:33:17 +0000436
John McCallf871d0c2010-08-07 06:22:56 +0000437CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000438 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000439 CastKind K, Expr *Op,
440 const CXXCastPath *BasePath,
441 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000442 SourceLocation L,
443 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000444 unsigned PathSize = (BasePath ? BasePath->size() : 0);
445 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
446 + PathSize * sizeof(CXXBaseSpecifier*));
447 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000448 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
449 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000450 if (PathSize) E->setCastPath(*BasePath);
451 return E;
452}
453
454CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
455 unsigned PathSize) {
456 void *Buffer =
457 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
458 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
459}
460
461CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000462 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000463 CastKind K, Expr *Op,
464 const CXXCastPath *BasePath,
465 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000466 SourceLocation L,
467 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000468 unsigned PathSize = (BasePath ? BasePath->size() : 0);
469 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
470 + PathSize * sizeof(CXXBaseSpecifier*));
471 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000472 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
473 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000474 if (PathSize) E->setCastPath(*BasePath);
475 return E;
476}
477
478CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
479 unsigned PathSize) {
480 void *Buffer =
481 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
482 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
483}
484
485CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000486CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
487 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000488 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000489 TypeSourceInfo *WrittenTy, SourceLocation L,
490 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000491 unsigned PathSize = (BasePath ? BasePath->size() : 0);
492 void *Buffer =
493 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
494 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000495 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
496 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000497 if (PathSize) E->setCastPath(*BasePath);
498 return E;
499}
500
501CXXReinterpretCastExpr *
502CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
503 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
504 + PathSize * sizeof(CXXBaseSpecifier*));
505 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
506}
507
John McCallf89e55a2010-11-18 06:31:45 +0000508CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
509 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000510 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000511 SourceLocation L,
512 SourceLocation RParenLoc) {
513 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000514}
515
516CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
517 return new (C) CXXConstCastExpr(EmptyShell());
518}
519
520CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000521CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000522 TypeSourceInfo *Written, SourceLocation L,
523 CastKind K, Expr *Op, const CXXCastPath *BasePath,
524 SourceLocation R) {
525 unsigned PathSize = (BasePath ? BasePath->size() : 0);
526 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
527 + PathSize * sizeof(CXXBaseSpecifier*));
528 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000529 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000530 if (PathSize) E->setCastPath(*BasePath);
531 return E;
532}
533
534CXXFunctionalCastExpr *
535CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
536 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
537 + PathSize * sizeof(CXXBaseSpecifier*));
538 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
539}
540
541
Douglas Gregor65222e82009-12-23 18:19:08 +0000542CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000543CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
544 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000545 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000546 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
547 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000548}
549
Mike Stump1eb44332009-09-09 15:08:12 +0000550CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000551 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000552 return new (C) CXXTemporary(Destructor);
553}
554
Mike Stump1eb44332009-09-09 15:08:12 +0000555CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000556 CXXTemporary *Temp,
557 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000558 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000559 "Expression bound to a temporary must have record type!");
560
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000561 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000562}
563
Anders Carlsson8e587a12009-05-30 20:56:46 +0000564CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000565 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000566 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000567 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000568 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000569 SourceRange parenRange,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000570 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000571 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
572 Type->getType().getNonReferenceType(),
573 Type->getTypeLoc().getBeginLoc(),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000574 Cons, false, Args, NumArgs, ZeroInitialization,
575 CXXConstructExpr::CK_Complete, parenRange),
576 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000577}
578
579SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000580 return SourceRange(Type->getTypeLoc().getBeginLoc(),
581 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000582}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000583
Mike Stump1eb44332009-09-09 15:08:12 +0000584CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000585 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000586 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000587 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000588 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000589 ConstructionKind ConstructKind,
590 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000591 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000592 Elidable, Args, NumArgs, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000593 ConstructKind, ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000594}
595
Mike Stump1eb44332009-09-09 15:08:12 +0000596CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000597 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000598 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000599 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000600 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000601 ConstructionKind ConstructKind,
602 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000603 : Expr(SC, T, VK_RValue, OK_Ordinary,
604 T->isDependentType(), T->isDependentType(),
605 T->containsUnexpandedParameterPack()),
606 Constructor(D), Loc(Loc), ParenRange(ParenRange), Elidable(elidable),
607 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
608 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000609{
610 if (NumArgs) {
611 Args = new (C) Stmt*[NumArgs];
612
613 for (unsigned i = 0; i != NumArgs; ++i) {
614 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000615
616 if (args[i]->isValueDependent())
617 ExprBits.ValueDependent = true;
618 if (args[i]->containsUnexpandedParameterPack())
619 ExprBits.ContainsUnexpandedParameterPack = true;
620
Douglas Gregor16006c92009-12-16 18:50:27 +0000621 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000622 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000623 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000624}
625
John McCall4765fa02010-12-06 08:20:24 +0000626ExprWithCleanups::ExprWithCleanups(ASTContext &C,
627 Expr *subexpr,
628 CXXTemporary **temps,
629 unsigned numtemps)
630 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000631 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000632 subexpr->isTypeDependent(), subexpr->isValueDependent(),
633 subexpr->containsUnexpandedParameterPack()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000634 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000635 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000636 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000637 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000638 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000639 }
640}
641
John McCall4765fa02010-12-06 08:20:24 +0000642void ExprWithCleanups::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000643 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000644 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000645 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000646}
647
648
John McCall4765fa02010-12-06 08:20:24 +0000649ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C,
650 Expr *SubExpr,
651 CXXTemporary **Temps,
652 unsigned NumTemps) {
653 return new (C) ExprWithCleanups(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000654}
655
Douglas Gregorab6677e2010-09-08 00:15:04 +0000656CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000657 SourceLocation LParenLoc,
658 Expr **Args,
659 unsigned NumArgs,
660 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000661 : Expr(CXXUnresolvedConstructExprClass,
662 Type->getType().getNonReferenceType(),
John McCall09431682010-11-18 19:01:18 +0000663 VK_LValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000664 Type->getType()->isDependentType(), true,
665 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000666 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000667 LParenLoc(LParenLoc),
668 RParenLoc(RParenLoc),
669 NumArgs(NumArgs) {
670 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000671 for (unsigned I = 0; I != NumArgs; ++I) {
672 if (Args[I]->containsUnexpandedParameterPack())
673 ExprBits.ContainsUnexpandedParameterPack = true;
674
675 StoredArgs[I] = Args[I];
676 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000677}
678
679CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000680CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000681 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000682 SourceLocation LParenLoc,
683 Expr **Args,
684 unsigned NumArgs,
685 SourceLocation RParenLoc) {
686 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
687 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000688 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000689 Args, NumArgs, RParenLoc);
690}
691
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000692CXXUnresolvedConstructExpr *
693CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
694 Stmt::EmptyShell Empty;
695 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
696 sizeof(Expr *) * NumArgs);
697 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
698}
699
Douglas Gregorab6677e2010-09-08 00:15:04 +0000700SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
701 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
702}
703
John McCall865d4472009-11-19 22:55:06 +0000704CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000705 Expr *Base, QualType BaseType,
706 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000707 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000708 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000709 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000710 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000711 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +0000712 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713 VK_LValue, OK_Ordinary, true, true,
714 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000715 (QualifierLoc &&
716 QualifierLoc.getNestedNameSpecifier()
717 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +0000719 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
720 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000721 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000722 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000723 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000724 if (TemplateArgs) {
725 bool Dependent = true;
726 bool ContainsUnexpandedParameterPack = false;
727 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
728 ContainsUnexpandedParameterPack);
729 if (ContainsUnexpandedParameterPack)
730 ExprBits.ContainsUnexpandedParameterPack = true;
731 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000732}
733
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
735 Expr *Base, QualType BaseType,
736 bool IsArrow,
737 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000738 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 NamedDecl *FirstQualifierFoundInScope,
740 DeclarationNameInfo MemberNameInfo)
741 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
742 VK_LValue, OK_Ordinary, true, true,
743 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000744 (QualifierLoc &&
745 QualifierLoc.getNestedNameSpecifier()->
746 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000747 MemberNameInfo.containsUnexpandedParameterPack())),
748 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
749 HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000750 QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000751 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
752 MemberNameInfo(MemberNameInfo) { }
753
John McCall865d4472009-11-19 22:55:06 +0000754CXXDependentScopeMemberExpr *
755CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000756 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000757 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000758 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000759 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000760 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000761 const TemplateArgumentListInfo *TemplateArgs) {
762 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000763 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
764 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000765 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000766 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000767 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
John McCalld5532b62009-11-23 01:53:49 +0000769 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
770 if (TemplateArgs)
771 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
772
Chris Lattner32488542010-10-30 05:14:06 +0000773 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000774 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
775 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000776 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000777 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000778 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000779}
780
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000781CXXDependentScopeMemberExpr *
782CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Douglas Gregordef03542011-02-04 12:01:24 +0000783 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000784 unsigned NumTemplateArgs) {
Douglas Gregordef03542011-02-04 12:01:24 +0000785 if (!HasExplicitTemplateArgs)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000786 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000787 0, SourceLocation(),
788 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000789 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000790
791 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
792 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +0000793 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000794 CXXDependentScopeMemberExpr *E
795 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000796 0, SourceLocation(),
797 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000798 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000799 E->HasExplicitTemplateArgs = true;
800 return E;
801}
802
Douglas Gregor4c9be892011-02-28 20:01:57 +0000803bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
804 if (Base == 0)
805 return true;
806
Douglas Gregor75e85042011-03-02 21:06:53 +0000807 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000808}
809
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000810UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000811 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000812 Expr *Base, QualType BaseType,
813 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000814 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000815 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000816 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000817 const TemplateArgumentListInfo *TemplateArgs,
818 UnresolvedSetIterator Begin,
819 UnresolvedSetIterator End)
Douglas Gregor4c9be892011-02-28 20:01:57 +0000820 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, MemberNameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000821 TemplateArgs, Begin, End,
822 // Dependent
823 ((Base && Base->isTypeDependent()) ||
824 BaseType->isDependentType()),
825 // Contains unexpanded parameter pack
826 ((Base && Base->containsUnexpandedParameterPack()) ||
827 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +0000828 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
829 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000830}
831
Douglas Gregor4c9be892011-02-28 20:01:57 +0000832bool UnresolvedMemberExpr::isImplicitAccess() const {
833 if (Base == 0)
834 return true;
835
Douglas Gregor75e85042011-03-02 21:06:53 +0000836 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000837}
838
John McCall129e2df2009-11-30 22:42:35 +0000839UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000840UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000841 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000842 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000843 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000844 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000845 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000846 const TemplateArgumentListInfo *TemplateArgs,
847 UnresolvedSetIterator Begin,
848 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000849 std::size_t size = sizeof(UnresolvedMemberExpr);
850 if (TemplateArgs)
851 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
852
Chris Lattner32488542010-10-30 05:14:06 +0000853 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000854 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000855 HasUnresolvedUsing, Base, BaseType,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000856 IsArrow, OperatorLoc, QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000857 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000858}
859
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000860UnresolvedMemberExpr *
Douglas Gregordef03542011-02-04 12:01:24 +0000861UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
862 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000863 std::size_t size = sizeof(UnresolvedMemberExpr);
Douglas Gregordef03542011-02-04 12:01:24 +0000864 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000865 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
866
Chris Lattner32488542010-10-30 05:14:06 +0000867 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000868 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Douglas Gregordef03542011-02-04 12:01:24 +0000869 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000870 return E;
871}
872
John McCallc373d482010-01-27 01:50:18 +0000873CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
874 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
875
876 // If there was a nested name specifier, it names the naming class.
877 // It can't be dependent: after all, we were actually able to do the
878 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000879 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000880 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +0000881 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000882 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000883 Record = T->getAsCXXRecordDecl();
884 assert(Record && "qualifier in member expression does not name record");
885 }
John McCallc373d482010-01-27 01:50:18 +0000886 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000887 else {
John McCallc373d482010-01-27 01:50:18 +0000888 QualType BaseType = getBaseType().getNonReferenceType();
889 if (isArrow()) {
890 const PointerType *PT = BaseType->getAs<PointerType>();
891 assert(PT && "base of arrow member access is not pointer");
892 BaseType = PT->getPointeeType();
893 }
894
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000895 Record = BaseType->getAsCXXRecordDecl();
896 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000897 }
898
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000899 return Record;
John McCallc373d482010-01-27 01:50:18 +0000900}
901
Douglas Gregorc7793c72011-01-15 01:15:58 +0000902SubstNonTypeTemplateParmPackExpr::
903SubstNonTypeTemplateParmPackExpr(QualType T,
904 NonTypeTemplateParmDecl *Param,
905 SourceLocation NameLoc,
906 const TemplateArgument &ArgPack)
907 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
908 true, false, true),
909 Param(Param), Arguments(ArgPack.pack_begin()),
910 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
911
912TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
913 return TemplateArgument(Arguments, NumArguments);
914}
915
Douglas Gregorc7793c72011-01-15 01:15:58 +0000916