blob: 35c2d4100823f13a497896bf67853b09fbd1da58 [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
371 return SourceRange(getArg(0)->getSourceRange().getEnd(),
372 getOperatorLoc());
373 } else if (Kind == OO_Call) {
374 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
375 } else if (Kind == OO_Subscript) {
376 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
377 } else if (getNumArgs() == 1) {
378 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
379 } else if (getNumArgs() == 2) {
380 return SourceRange(getArg(0)->getSourceRange().getBegin(),
381 getArg(1)->getSourceRange().getEnd());
382 } else {
383 return SourceRange();
384 }
385}
386
Douglas Gregor88a35142008-12-22 05:46:06 +0000387Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
388 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
389 return MemExpr->getBase();
390
391 // FIXME: Will eventually need to cope with member pointers.
392 return 0;
393}
394
Chandler Carruth007a9b12010-10-27 06:55:41 +0000395CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
396 Expr* ThisArg = getImplicitObjectArgument();
397 if (!ThisArg)
398 return 0;
399
400 if (ThisArg->getType()->isAnyPointerType())
401 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
402
403 return ThisArg->getType()->getAsCXXRecordDecl();
404}
405
Douglas Gregor00b98c22009-11-12 15:31:47 +0000406
Douglas Gregor49badde2008-10-27 19:41:14 +0000407//===----------------------------------------------------------------------===//
408// Named casts
409//===----------------------------------------------------------------------===//
410
411/// getCastName - Get the name of the C++ cast being used, e.g.,
412/// "static_cast", "dynamic_cast", "reinterpret_cast", or
413/// "const_cast". The returned pointer must not be freed.
414const char *CXXNamedCastExpr::getCastName() const {
415 switch (getStmtClass()) {
416 case CXXStaticCastExprClass: return "static_cast";
417 case CXXDynamicCastExprClass: return "dynamic_cast";
418 case CXXReinterpretCastExprClass: return "reinterpret_cast";
419 case CXXConstCastExprClass: return "const_cast";
420 default: return "<invalid cast>";
421 }
422}
Douglas Gregor506ae412009-01-16 18:33:17 +0000423
John McCallf871d0c2010-08-07 06:22:56 +0000424CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000425 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000426 CastKind K, Expr *Op,
427 const CXXCastPath *BasePath,
428 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000429 SourceLocation L,
430 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000431 unsigned PathSize = (BasePath ? BasePath->size() : 0);
432 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
433 + PathSize * sizeof(CXXBaseSpecifier*));
434 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000435 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
436 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000437 if (PathSize) E->setCastPath(*BasePath);
438 return E;
439}
440
441CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
442 unsigned PathSize) {
443 void *Buffer =
444 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
445 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
446}
447
448CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000449 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000450 CastKind K, Expr *Op,
451 const CXXCastPath *BasePath,
452 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000453 SourceLocation L,
454 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000455 unsigned PathSize = (BasePath ? BasePath->size() : 0);
456 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
457 + PathSize * sizeof(CXXBaseSpecifier*));
458 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000459 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
460 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000461 if (PathSize) E->setCastPath(*BasePath);
462 return E;
463}
464
465CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
466 unsigned PathSize) {
467 void *Buffer =
468 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
469 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
470}
471
472CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000473CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
474 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000475 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000476 TypeSourceInfo *WrittenTy, SourceLocation L,
477 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000478 unsigned PathSize = (BasePath ? BasePath->size() : 0);
479 void *Buffer =
480 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
481 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000482 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
483 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000484 if (PathSize) E->setCastPath(*BasePath);
485 return E;
486}
487
488CXXReinterpretCastExpr *
489CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
490 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
491 + PathSize * sizeof(CXXBaseSpecifier*));
492 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
493}
494
John McCallf89e55a2010-11-18 06:31:45 +0000495CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
496 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000497 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000498 SourceLocation L,
499 SourceLocation RParenLoc) {
500 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000501}
502
503CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
504 return new (C) CXXConstCastExpr(EmptyShell());
505}
506
507CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000508CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000509 TypeSourceInfo *Written, SourceLocation L,
510 CastKind K, Expr *Op, const CXXCastPath *BasePath,
511 SourceLocation R) {
512 unsigned PathSize = (BasePath ? BasePath->size() : 0);
513 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
514 + PathSize * sizeof(CXXBaseSpecifier*));
515 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000516 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000517 if (PathSize) E->setCastPath(*BasePath);
518 return E;
519}
520
521CXXFunctionalCastExpr *
522CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
523 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
524 + PathSize * sizeof(CXXBaseSpecifier*));
525 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
526}
527
528
Douglas Gregor65222e82009-12-23 18:19:08 +0000529CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000530CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
531 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000532 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000533 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
534 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000535}
536
Mike Stump1eb44332009-09-09 15:08:12 +0000537CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000538 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000539 return new (C) CXXTemporary(Destructor);
540}
541
Mike Stump1eb44332009-09-09 15:08:12 +0000542CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000543 CXXTemporary *Temp,
544 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000545 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000546 "Expression bound to a temporary must have record type!");
547
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000548 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000549}
550
Anders Carlsson8e587a12009-05-30 20:56:46 +0000551CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000552 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000553 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000554 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000555 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000556 SourceRange parenRange,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000557 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000558 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
559 Type->getType().getNonReferenceType(),
560 Type->getTypeLoc().getBeginLoc(),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000561 Cons, false, Args, NumArgs, ZeroInitialization,
562 CXXConstructExpr::CK_Complete, parenRange),
563 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000564}
565
566SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000567 return SourceRange(Type->getTypeLoc().getBeginLoc(),
568 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000569}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000570
Mike Stump1eb44332009-09-09 15:08:12 +0000571CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000572 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000573 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000574 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000575 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000576 ConstructionKind ConstructKind,
577 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000578 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000579 Elidable, Args, NumArgs, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000580 ConstructKind, ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000581}
582
Mike Stump1eb44332009-09-09 15:08:12 +0000583CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000584 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000585 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000586 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000587 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000588 ConstructionKind ConstructKind,
589 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000590 : Expr(SC, T, VK_RValue, OK_Ordinary,
591 T->isDependentType(), T->isDependentType(),
592 T->containsUnexpandedParameterPack()),
593 Constructor(D), Loc(Loc), ParenRange(ParenRange), Elidable(elidable),
594 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
595 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000596{
597 if (NumArgs) {
598 Args = new (C) Stmt*[NumArgs];
599
600 for (unsigned i = 0; i != NumArgs; ++i) {
601 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000602
603 if (args[i]->isValueDependent())
604 ExprBits.ValueDependent = true;
605 if (args[i]->containsUnexpandedParameterPack())
606 ExprBits.ContainsUnexpandedParameterPack = true;
607
Douglas Gregor16006c92009-12-16 18:50:27 +0000608 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000609 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000610 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000611}
612
John McCall4765fa02010-12-06 08:20:24 +0000613ExprWithCleanups::ExprWithCleanups(ASTContext &C,
614 Expr *subexpr,
615 CXXTemporary **temps,
616 unsigned numtemps)
617 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000618 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000619 subexpr->isTypeDependent(), subexpr->isValueDependent(),
620 subexpr->containsUnexpandedParameterPack()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000621 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000622 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000623 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000624 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000625 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000626 }
627}
628
John McCall4765fa02010-12-06 08:20:24 +0000629void ExprWithCleanups::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000630 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000631 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000632 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000633}
634
635
John McCall4765fa02010-12-06 08:20:24 +0000636ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C,
637 Expr *SubExpr,
638 CXXTemporary **Temps,
639 unsigned NumTemps) {
640 return new (C) ExprWithCleanups(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000641}
642
Douglas Gregorab6677e2010-09-08 00:15:04 +0000643CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000644 SourceLocation LParenLoc,
645 Expr **Args,
646 unsigned NumArgs,
647 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000648 : Expr(CXXUnresolvedConstructExprClass,
649 Type->getType().getNonReferenceType(),
John McCall09431682010-11-18 19:01:18 +0000650 VK_LValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000651 Type->getType()->isDependentType(), true,
652 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000653 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000654 LParenLoc(LParenLoc),
655 RParenLoc(RParenLoc),
656 NumArgs(NumArgs) {
657 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000658 for (unsigned I = 0; I != NumArgs; ++I) {
659 if (Args[I]->containsUnexpandedParameterPack())
660 ExprBits.ContainsUnexpandedParameterPack = true;
661
662 StoredArgs[I] = Args[I];
663 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000664}
665
666CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000667CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000668 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000669 SourceLocation LParenLoc,
670 Expr **Args,
671 unsigned NumArgs,
672 SourceLocation RParenLoc) {
673 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
674 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000675 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000676 Args, NumArgs, RParenLoc);
677}
678
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000679CXXUnresolvedConstructExpr *
680CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
681 Stmt::EmptyShell Empty;
682 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
683 sizeof(Expr *) * NumArgs);
684 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
685}
686
Douglas Gregorab6677e2010-09-08 00:15:04 +0000687SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
688 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
689}
690
John McCall865d4472009-11-19 22:55:06 +0000691CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000692 Expr *Base, QualType BaseType,
693 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000694 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000695 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000696 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000697 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000698 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +0000699 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000700 VK_LValue, OK_Ordinary, true, true,
701 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000702 (QualifierLoc &&
703 QualifierLoc.getNestedNameSpecifier()
704 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000705 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +0000706 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
707 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000708 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000709 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000710 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000711 if (TemplateArgs) {
712 bool Dependent = true;
713 bool ContainsUnexpandedParameterPack = false;
714 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
715 ContainsUnexpandedParameterPack);
716 if (ContainsUnexpandedParameterPack)
717 ExprBits.ContainsUnexpandedParameterPack = true;
718 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000719}
720
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000721CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
722 Expr *Base, QualType BaseType,
723 bool IsArrow,
724 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000725 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000726 NamedDecl *FirstQualifierFoundInScope,
727 DeclarationNameInfo MemberNameInfo)
728 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
729 VK_LValue, OK_Ordinary, true, true,
730 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000731 (QualifierLoc &&
732 QualifierLoc.getNestedNameSpecifier()->
733 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734 MemberNameInfo.containsUnexpandedParameterPack())),
735 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
736 HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000737 QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000738 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
739 MemberNameInfo(MemberNameInfo) { }
740
John McCall865d4472009-11-19 22:55:06 +0000741CXXDependentScopeMemberExpr *
742CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000743 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000744 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000745 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000746 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000747 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000748 const TemplateArgumentListInfo *TemplateArgs) {
749 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000750 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
751 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000752 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000753 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000754 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
John McCalld5532b62009-11-23 01:53:49 +0000756 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
757 if (TemplateArgs)
758 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
759
Chris Lattner32488542010-10-30 05:14:06 +0000760 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000761 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
762 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000763 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000764 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000765 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000766}
767
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000768CXXDependentScopeMemberExpr *
769CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Douglas Gregordef03542011-02-04 12:01:24 +0000770 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000771 unsigned NumTemplateArgs) {
Douglas Gregordef03542011-02-04 12:01:24 +0000772 if (!HasExplicitTemplateArgs)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000773 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000774 0, SourceLocation(),
775 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000776 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000777
778 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
779 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +0000780 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000781 CXXDependentScopeMemberExpr *E
782 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000783 0, SourceLocation(),
784 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000785 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000786 E->HasExplicitTemplateArgs = true;
787 return E;
788}
789
Douglas Gregor4c9be892011-02-28 20:01:57 +0000790bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
791 if (Base == 0)
792 return true;
793
Douglas Gregor75e85042011-03-02 21:06:53 +0000794 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000795}
796
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000797UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000798 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000799 Expr *Base, QualType BaseType,
800 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000801 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000802 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000803 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000804 const TemplateArgumentListInfo *TemplateArgs,
805 UnresolvedSetIterator Begin,
806 UnresolvedSetIterator End)
Douglas Gregor4c9be892011-02-28 20:01:57 +0000807 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, MemberNameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000808 TemplateArgs, Begin, End,
809 // Dependent
810 ((Base && Base->isTypeDependent()) ||
811 BaseType->isDependentType()),
812 // Contains unexpanded parameter pack
813 ((Base && Base->containsUnexpandedParameterPack()) ||
814 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +0000815 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
816 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000817}
818
Douglas Gregor4c9be892011-02-28 20:01:57 +0000819bool UnresolvedMemberExpr::isImplicitAccess() const {
820 if (Base == 0)
821 return true;
822
Douglas Gregor75e85042011-03-02 21:06:53 +0000823 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000824}
825
John McCall129e2df2009-11-30 22:42:35 +0000826UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000827UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000828 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000829 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000830 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000831 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000832 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000833 const TemplateArgumentListInfo *TemplateArgs,
834 UnresolvedSetIterator Begin,
835 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000836 std::size_t size = sizeof(UnresolvedMemberExpr);
837 if (TemplateArgs)
838 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
839
Chris Lattner32488542010-10-30 05:14:06 +0000840 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000841 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000842 HasUnresolvedUsing, Base, BaseType,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000843 IsArrow, OperatorLoc, QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000844 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000845}
846
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000847UnresolvedMemberExpr *
Douglas Gregordef03542011-02-04 12:01:24 +0000848UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
849 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000850 std::size_t size = sizeof(UnresolvedMemberExpr);
Douglas Gregordef03542011-02-04 12:01:24 +0000851 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000852 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
853
Chris Lattner32488542010-10-30 05:14:06 +0000854 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000855 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Douglas Gregordef03542011-02-04 12:01:24 +0000856 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000857 return E;
858}
859
John McCallc373d482010-01-27 01:50:18 +0000860CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
861 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
862
863 // If there was a nested name specifier, it names the naming class.
864 // It can't be dependent: after all, we were actually able to do the
865 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000866 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000867 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +0000868 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000869 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000870 Record = T->getAsCXXRecordDecl();
871 assert(Record && "qualifier in member expression does not name record");
872 }
John McCallc373d482010-01-27 01:50:18 +0000873 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000874 else {
John McCallc373d482010-01-27 01:50:18 +0000875 QualType BaseType = getBaseType().getNonReferenceType();
876 if (isArrow()) {
877 const PointerType *PT = BaseType->getAs<PointerType>();
878 assert(PT && "base of arrow member access is not pointer");
879 BaseType = PT->getPointeeType();
880 }
881
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000882 Record = BaseType->getAsCXXRecordDecl();
883 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000884 }
885
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000886 return Record;
John McCallc373d482010-01-27 01:50:18 +0000887}
888
Douglas Gregorc7793c72011-01-15 01:15:58 +0000889SubstNonTypeTemplateParmPackExpr::
890SubstNonTypeTemplateParmPackExpr(QualType T,
891 NonTypeTemplateParmDecl *Param,
892 SourceLocation NameLoc,
893 const TemplateArgument &ArgPack)
894 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
895 true, false, true),
896 Param(Param), Arguments(ArgPack.pack_begin()),
897 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
898
899TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
900 return TemplateArgument(Arguments, NumArguments);
901}
902
Douglas Gregorc7793c72011-01-15 01:15:58 +0000903