blob: 1a1a0a36a65b43664398cda90b9512c92fec58e4 [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,
Richard Smithad762fc2011-04-14 22:09:26 +0000192 Begin, End, /*StdIsAssociated=*/false);
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
Anders Carlsson0fee3302011-04-11 01:43:55 +0000485/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
486/// to always be null. For example:
487///
488/// struct A { };
489/// struct B final : A { };
490/// struct C { };
491///
492/// C *f(B* b) { return dynamic_cast<C*>(b); }
493bool CXXDynamicCastExpr::isAlwaysNull() const
494{
495 QualType SrcType = getSubExpr()->getType();
496 QualType DestType = getType();
497
498 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
499 SrcType = SrcPTy->getPointeeType();
500 DestType = DestType->castAs<PointerType>()->getPointeeType();
501 }
502
503 const CXXRecordDecl *SrcRD =
504 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
505
506 if (!SrcRD->hasAttr<FinalAttr>())
507 return false;
508
509 const CXXRecordDecl *DestRD =
510 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
511
512 return !DestRD->isDerivedFrom(SrcRD);
513}
514
John McCallf871d0c2010-08-07 06:22:56 +0000515CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000516CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
517 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000518 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000519 TypeSourceInfo *WrittenTy, SourceLocation L,
520 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000521 unsigned PathSize = (BasePath ? BasePath->size() : 0);
522 void *Buffer =
523 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
524 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000525 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
526 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000527 if (PathSize) E->setCastPath(*BasePath);
528 return E;
529}
530
531CXXReinterpretCastExpr *
532CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
533 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
534 + PathSize * sizeof(CXXBaseSpecifier*));
535 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
536}
537
John McCallf89e55a2010-11-18 06:31:45 +0000538CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
539 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000540 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000541 SourceLocation L,
542 SourceLocation RParenLoc) {
543 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000544}
545
546CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
547 return new (C) CXXConstCastExpr(EmptyShell());
548}
549
550CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000551CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000552 TypeSourceInfo *Written, SourceLocation L,
553 CastKind K, Expr *Op, const CXXCastPath *BasePath,
554 SourceLocation R) {
555 unsigned PathSize = (BasePath ? BasePath->size() : 0);
556 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
557 + PathSize * sizeof(CXXBaseSpecifier*));
558 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000559 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000560 if (PathSize) E->setCastPath(*BasePath);
561 return E;
562}
563
564CXXFunctionalCastExpr *
565CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
566 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
567 + PathSize * sizeof(CXXBaseSpecifier*));
568 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
569}
570
571
Douglas Gregor65222e82009-12-23 18:19:08 +0000572CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000573CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
574 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000575 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000576 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
577 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000578}
579
Mike Stump1eb44332009-09-09 15:08:12 +0000580CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000581 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000582 return new (C) CXXTemporary(Destructor);
583}
584
Mike Stump1eb44332009-09-09 15:08:12 +0000585CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000586 CXXTemporary *Temp,
587 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000588 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000589 "Expression bound to a temporary must have record type!");
590
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000591 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000592}
593
Anders Carlsson8e587a12009-05-30 20:56:46 +0000594CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000595 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000596 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000597 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000598 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000599 SourceRange parenRange,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000600 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000601 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
602 Type->getType().getNonReferenceType(),
603 Type->getTypeLoc().getBeginLoc(),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000604 Cons, false, Args, NumArgs, ZeroInitialization,
605 CXXConstructExpr::CK_Complete, parenRange),
606 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000607}
608
609SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000610 return SourceRange(Type->getTypeLoc().getBeginLoc(),
611 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000612}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000613
Mike Stump1eb44332009-09-09 15:08:12 +0000614CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000615 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000616 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000617 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000618 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000619 ConstructionKind ConstructKind,
620 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000621 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000622 Elidable, Args, NumArgs, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000623 ConstructKind, ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000624}
625
Mike Stump1eb44332009-09-09 15:08:12 +0000626CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000627 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000628 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000629 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000630 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000631 ConstructionKind ConstructKind,
632 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000633 : Expr(SC, T, VK_RValue, OK_Ordinary,
634 T->isDependentType(), T->isDependentType(),
635 T->containsUnexpandedParameterPack()),
636 Constructor(D), Loc(Loc), ParenRange(ParenRange), Elidable(elidable),
637 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
638 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000639{
640 if (NumArgs) {
641 Args = new (C) Stmt*[NumArgs];
642
643 for (unsigned i = 0; i != NumArgs; ++i) {
644 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000645
646 if (args[i]->isValueDependent())
647 ExprBits.ValueDependent = true;
648 if (args[i]->containsUnexpandedParameterPack())
649 ExprBits.ContainsUnexpandedParameterPack = true;
650
Douglas Gregor16006c92009-12-16 18:50:27 +0000651 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000652 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000653 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000654}
655
John McCall4765fa02010-12-06 08:20:24 +0000656ExprWithCleanups::ExprWithCleanups(ASTContext &C,
657 Expr *subexpr,
658 CXXTemporary **temps,
659 unsigned numtemps)
660 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000661 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000662 subexpr->isTypeDependent(), subexpr->isValueDependent(),
663 subexpr->containsUnexpandedParameterPack()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000664 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000665 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000666 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000667 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000668 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000669 }
670}
671
John McCall4765fa02010-12-06 08:20:24 +0000672void ExprWithCleanups::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000673 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000674 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000675 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000676}
677
678
John McCall4765fa02010-12-06 08:20:24 +0000679ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C,
680 Expr *SubExpr,
681 CXXTemporary **Temps,
682 unsigned NumTemps) {
683 return new (C) ExprWithCleanups(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000684}
685
Douglas Gregorab6677e2010-09-08 00:15:04 +0000686CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000687 SourceLocation LParenLoc,
688 Expr **Args,
689 unsigned NumArgs,
690 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000691 : Expr(CXXUnresolvedConstructExprClass,
692 Type->getType().getNonReferenceType(),
John McCall09431682010-11-18 19:01:18 +0000693 VK_LValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000694 Type->getType()->isDependentType(), true,
695 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000696 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000697 LParenLoc(LParenLoc),
698 RParenLoc(RParenLoc),
699 NumArgs(NumArgs) {
700 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000701 for (unsigned I = 0; I != NumArgs; ++I) {
702 if (Args[I]->containsUnexpandedParameterPack())
703 ExprBits.ContainsUnexpandedParameterPack = true;
704
705 StoredArgs[I] = Args[I];
706 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000707}
708
709CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000710CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000711 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000712 SourceLocation LParenLoc,
713 Expr **Args,
714 unsigned NumArgs,
715 SourceLocation RParenLoc) {
716 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
717 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000718 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000719 Args, NumArgs, RParenLoc);
720}
721
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000722CXXUnresolvedConstructExpr *
723CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
724 Stmt::EmptyShell Empty;
725 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
726 sizeof(Expr *) * NumArgs);
727 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
728}
729
Douglas Gregorab6677e2010-09-08 00:15:04 +0000730SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
731 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
732}
733
John McCall865d4472009-11-19 22:55:06 +0000734CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000735 Expr *Base, QualType BaseType,
736 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000737 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000738 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000739 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000740 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000741 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +0000742 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000743 VK_LValue, OK_Ordinary, true, true,
744 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000745 (QualifierLoc &&
746 QualifierLoc.getNestedNameSpecifier()
747 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000748 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +0000749 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
750 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000751 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000752 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000753 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000754 if (TemplateArgs) {
755 bool Dependent = true;
756 bool ContainsUnexpandedParameterPack = false;
757 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
758 ContainsUnexpandedParameterPack);
759 if (ContainsUnexpandedParameterPack)
760 ExprBits.ContainsUnexpandedParameterPack = true;
761 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000762}
763
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000764CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
765 Expr *Base, QualType BaseType,
766 bool IsArrow,
767 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000768 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000769 NamedDecl *FirstQualifierFoundInScope,
770 DeclarationNameInfo MemberNameInfo)
771 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
772 VK_LValue, OK_Ordinary, true, true,
773 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000774 (QualifierLoc &&
775 QualifierLoc.getNestedNameSpecifier()->
776 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000777 MemberNameInfo.containsUnexpandedParameterPack())),
778 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
779 HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000780 QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000781 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
782 MemberNameInfo(MemberNameInfo) { }
783
John McCall865d4472009-11-19 22:55:06 +0000784CXXDependentScopeMemberExpr *
785CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000786 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000787 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000788 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000789 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000790 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000791 const TemplateArgumentListInfo *TemplateArgs) {
792 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000793 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
794 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000795 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000796 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000797 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
John McCalld5532b62009-11-23 01:53:49 +0000799 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
800 if (TemplateArgs)
801 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
802
Chris Lattner32488542010-10-30 05:14:06 +0000803 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000804 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
805 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000806 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +0000807 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000808 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000809}
810
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000811CXXDependentScopeMemberExpr *
812CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Douglas Gregordef03542011-02-04 12:01:24 +0000813 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000814 unsigned NumTemplateArgs) {
Douglas Gregordef03542011-02-04 12:01:24 +0000815 if (!HasExplicitTemplateArgs)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000816 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000817 0, SourceLocation(),
818 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000819 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000820
821 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
822 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +0000823 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000824 CXXDependentScopeMemberExpr *E
825 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000826 0, SourceLocation(),
827 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000828 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000829 E->HasExplicitTemplateArgs = true;
830 return E;
831}
832
Douglas Gregor4c9be892011-02-28 20:01:57 +0000833bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
834 if (Base == 0)
835 return true;
836
Douglas Gregor75e85042011-03-02 21:06:53 +0000837 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000838}
839
John McCall864c0412011-04-26 20:42:42 +0000840static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
841 UnresolvedSetIterator end) {
842 do {
843 NamedDecl *decl = *begin;
844 if (isa<UnresolvedUsingValueDecl>(decl))
845 return false;
846 if (isa<UsingShadowDecl>(decl))
847 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
848
849 // Unresolved member expressions should only contain methods and
850 // method templates.
851 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
852
853 if (isa<FunctionTemplateDecl>(decl))
854 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
855 if (cast<CXXMethodDecl>(decl)->isStatic())
856 return false;
857 } while (++begin != end);
858
859 return true;
860}
861
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000862UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000863 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000864 Expr *Base, QualType BaseType,
865 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000866 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000867 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000868 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000869 const TemplateArgumentListInfo *TemplateArgs,
870 UnresolvedSetIterator Begin,
871 UnresolvedSetIterator End)
Douglas Gregor4c9be892011-02-28 20:01:57 +0000872 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, MemberNameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000873 TemplateArgs, Begin, End,
874 // Dependent
875 ((Base && Base->isTypeDependent()) ||
876 BaseType->isDependentType()),
877 // Contains unexpanded parameter pack
878 ((Base && Base->containsUnexpandedParameterPack()) ||
879 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +0000880 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
881 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +0000882
883 // Check whether all of the members are non-static member functions,
884 // and if so, mark give this bound-member type instead of overload type.
885 if (hasOnlyNonStaticMemberFunctions(Begin, End))
886 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +0000887}
888
Douglas Gregor4c9be892011-02-28 20:01:57 +0000889bool UnresolvedMemberExpr::isImplicitAccess() const {
890 if (Base == 0)
891 return true;
892
Douglas Gregor75e85042011-03-02 21:06:53 +0000893 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +0000894}
895
John McCall129e2df2009-11-30 22:42:35 +0000896UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000897UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +0000898 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000899 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000900 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000901 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000902 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000903 const TemplateArgumentListInfo *TemplateArgs,
904 UnresolvedSetIterator Begin,
905 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000906 std::size_t size = sizeof(UnresolvedMemberExpr);
907 if (TemplateArgs)
908 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
909
Chris Lattner32488542010-10-30 05:14:06 +0000910 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000911 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000912 HasUnresolvedUsing, Base, BaseType,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000913 IsArrow, OperatorLoc, QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000914 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000915}
916
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000917UnresolvedMemberExpr *
Douglas Gregordef03542011-02-04 12:01:24 +0000918UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs,
919 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000920 std::size_t size = sizeof(UnresolvedMemberExpr);
Douglas Gregordef03542011-02-04 12:01:24 +0000921 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000922 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
923
Chris Lattner32488542010-10-30 05:14:06 +0000924 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000925 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Douglas Gregordef03542011-02-04 12:01:24 +0000926 E->HasExplicitTemplateArgs = HasExplicitTemplateArgs;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000927 return E;
928}
929
John McCallc373d482010-01-27 01:50:18 +0000930CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
931 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
932
933 // If there was a nested name specifier, it names the naming class.
934 // It can't be dependent: after all, we were actually able to do the
935 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000936 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000937 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +0000938 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000939 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000940 Record = T->getAsCXXRecordDecl();
941 assert(Record && "qualifier in member expression does not name record");
942 }
John McCallc373d482010-01-27 01:50:18 +0000943 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000944 else {
John McCallc373d482010-01-27 01:50:18 +0000945 QualType BaseType = getBaseType().getNonReferenceType();
946 if (isArrow()) {
947 const PointerType *PT = BaseType->getAs<PointerType>();
948 assert(PT && "base of arrow member access is not pointer");
949 BaseType = PT->getPointeeType();
950 }
951
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000952 Record = BaseType->getAsCXXRecordDecl();
953 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000954 }
955
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000956 return Record;
John McCallc373d482010-01-27 01:50:18 +0000957}
958
Douglas Gregorc7793c72011-01-15 01:15:58 +0000959SubstNonTypeTemplateParmPackExpr::
960SubstNonTypeTemplateParmPackExpr(QualType T,
961 NonTypeTemplateParmDecl *Param,
962 SourceLocation NameLoc,
963 const TemplateArgument &ArgPack)
964 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
965 true, false, true),
966 Param(Param), Arguments(ArgPack.pack_begin()),
967 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
968
969TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
970 return TemplateArgument(Arguments, NumArguments);
971}
972
Douglas Gregorc7793c72011-01-15 01:15:58 +0000973