blob: dc559f0940fcf752399356edefa92db36619a15a [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 Redl1548d142012-02-16 11:35:52 +000048 FunctionDecl *operatorDelete,
Sebastian Redl2aed8b82012-02-16 12:22:20 +000049 bool usualArrayDeleteWantsSize,
50 Expr **placementArgs, unsigned numPlaceArgs,
51 SourceRange typeIdParens, Expr *arraySize,
52 InitializationStyle initializationStyle,
53 Expr *initializer, QualType ty,
54 TypeSourceInfo *allocatedTypeInfo,
55 SourceLocation startLoc, SourceRange directInitRange)
John McCallf89e55a2010-11-18 06:31:45 +000056 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000057 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +000058 ty->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000059 ty->containsUnexpandedParameterPack()),
Sebastian Redl2aed8b82012-02-16 12:22:20 +000060 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
61 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerd162cf12012-02-26 20:37:14 +000062 StartLoc(startLoc), DirectInitRange(directInitRange),
63 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +000064 assert((initializer != 0 || initializationStyle == NoInit) &&
65 "Only NoInit can have no initializer.");
66 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
67 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000068 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000069 if (Array) {
Douglas Gregor561f8122011-07-01 01:22:09 +000070 if (arraySize->isInstantiationDependent())
71 ExprBits.InstantiationDependent = true;
72
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000073 if (arraySize->containsUnexpandedParameterPack())
74 ExprBits.ContainsUnexpandedParameterPack = true;
75
Sebastian Redlcee63fb2008-12-02 14:43:59 +000076 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000077 }
78
Sebastian Redl2aed8b82012-02-16 12:22:20 +000079 if (initializer) {
80 if (initializer->isInstantiationDependent())
81 ExprBits.InstantiationDependent = true;
82
83 if (initializer->containsUnexpandedParameterPack())
84 ExprBits.ContainsUnexpandedParameterPack = true;
85
86 SubExprs[i++] = initializer;
87 }
88
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000089 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor561f8122011-07-01 01:22:09 +000090 if (placementArgs[j]->isInstantiationDependent())
91 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000092 if (placementArgs[j]->containsUnexpandedParameterPack())
93 ExprBits.ContainsUnexpandedParameterPack = true;
94
Sebastian Redl4c5d3202008-11-21 19:14:01 +000095 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000096 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +000097}
98
Chris Lattner59218632010-05-10 01:22:27 +000099void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000100 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattner59218632010-05-10 01:22:27 +0000101 assert(SubExprs == 0 && "SubExprs already allocated");
102 Array = isArray;
103 NumPlacementArgs = numPlaceArgs;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000104
105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattner59218632010-05-10 01:22:27 +0000106 SubExprs = new (C) Stmt*[TotalSize];
107}
108
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000109bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000110 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000111 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000112}
Chris Lattner59218632010-05-10 01:22:27 +0000113
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000114SourceLocation CXXNewExpr::getEndLoc() const {
115 switch (getInitializationStyle()) {
116 case NoInit:
117 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
118 case CallInit:
119 return DirectInitRange.getEnd();
120 case ListInit:
121 return getInitializer()->getSourceRange().getEnd();
122 }
Matt Beaumont-Gayebc6af12012-02-16 22:15:50 +0000123 llvm_unreachable("bogus initialization style");
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000124}
125
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000126// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000127QualType CXXDeleteExpr::getDestroyedType() const {
128 const Expr *Arg = getArgument();
129 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
130 if (ICE->getCastKind() != CK_UserDefinedConversion &&
131 ICE->getType()->isVoidPointerType())
132 Arg = ICE->getSubExpr();
133 else
134 break;
135 }
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000136 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000137 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000138
139 if (ArgType->isDependentType() && !ArgType->isPointerType())
140 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000141
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000142 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000143}
144
Douglas Gregora71d8192009-09-04 17:36:40 +0000145// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000146PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
147 : Type(Info)
148{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000150}
151
John McCalle23cf432010-12-14 08:05:40 +0000152CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000153 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
154 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
155 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
156 PseudoDestructorTypeStorage DestroyedType)
John McCalle23cf432010-12-14 08:05:40 +0000157 : Expr(CXXPseudoDestructorExprClass,
158 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
159 FunctionProtoType::ExtProtoInfo())),
160 VK_RValue, OK_Ordinary,
161 /*isTypeDependent=*/(Base->isTypeDependent() ||
162 (DestroyedType.getTypeSourceInfo() &&
163 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000164 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000165 (Base->isInstantiationDependent() ||
166 (QualifierLoc &&
167 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
168 (ScopeType &&
169 ScopeType->getType()->isInstantiationDependentType()) ||
170 (DestroyedType.getTypeSourceInfo() &&
171 DestroyedType.getTypeSourceInfo()->getType()
172 ->isInstantiationDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000173 // ContainsUnexpandedParameterPack
174 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()
177 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000178 (ScopeType &&
179 ScopeType->getType()->containsUnexpandedParameterPack()) ||
180 (DestroyedType.getTypeSourceInfo() &&
181 DestroyedType.getTypeSourceInfo()->getType()
182 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
186 DestroyedType(DestroyedType) { }
187
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000188QualType CXXPseudoDestructorExpr::getDestroyedType() const {
189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
190 return TInfo->getType();
191
192 return QualType();
193}
194
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000200}
201
John McCallba135432009-11-21 08:51:07 +0000202// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000203UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000204UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000205 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000206 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000207 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000208 const DeclarationNameInfo &NameInfo,
209 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000210 const TemplateArgumentListInfo *Args,
211 UnresolvedSetIterator Begin,
212 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000213{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000214 assert(Args || TemplateKWLoc.isValid());
215 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000216 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000217 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000218 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
219 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000220 ADL, /*Overload*/ true, Args,
Richard Smithad762fc2011-04-14 22:09:26 +0000221 Begin, End, /*StdIsAssociated=*/false);
John McCallf7a1a742009-11-24 19:00:30 +0000222}
223
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000224UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000225UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
226 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000227 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000228 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000229 if (HasTemplateKWAndArgsInfo)
230 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000231
Chris Lattner32488542010-10-30 05:14:06 +0000232 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000233 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000234 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000235 return E;
236}
237
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000238OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000239 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000240 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000241 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000242 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000243 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000244 UnresolvedSetIterator End,
245 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000246 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247 bool KnownContainsUnexpandedParameterPack)
248 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
249 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000250 (KnownInstantiationDependent ||
251 NameInfo.isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000254 (KnownContainsUnexpandedParameterPack ||
255 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000256 (QualifierLoc &&
257 QualifierLoc.getNestedNameSpecifier()
258 ->containsUnexpandedParameterPack()))),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000259 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
260 Results(0), NumResults(End - Begin),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000261 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000262{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000263 NumResults = End - Begin;
264 if (NumResults) {
265 // Determine whether this expression is type-dependent.
266 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
267 if ((*I)->getDeclContext()->isDependentContext() ||
268 isa<UnresolvedUsingValueDecl>(*I)) {
269 ExprBits.TypeDependent = true;
270 ExprBits.ValueDependent = true;
271 }
272 }
273
274 Results = static_cast<DeclAccessPair *>(
275 C.Allocate(sizeof(DeclAccessPair) * NumResults,
276 llvm::alignOf<DeclAccessPair>()));
277 memcpy(Results, &*Begin.getIterator(),
278 NumResults * sizeof(DeclAccessPair));
279 }
280
281 // If we have explicit template arguments, check for dependent
282 // template arguments and whether they contain any unexpanded pack
283 // expansions.
284 if (TemplateArgs) {
285 bool Dependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000286 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000287 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000288 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
289 Dependent,
290 InstantiationDependent,
291 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000292
293 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 ExprBits.TypeDependent = true;
295 ExprBits.ValueDependent = true;
296 }
297 if (InstantiationDependent)
298 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000299 if (ContainsUnexpandedParameterPack)
300 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000301 } else if (TemplateKWLoc.isValid()) {
302 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000303 }
304
305 if (isTypeDependent())
306 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000307}
308
309void OverloadExpr::initializeResults(ASTContext &C,
310 UnresolvedSetIterator Begin,
311 UnresolvedSetIterator End) {
312 assert(Results == 0 && "Results already initialized!");
313 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000314 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000315 Results = static_cast<DeclAccessPair *>(
316 C.Allocate(sizeof(DeclAccessPair) * NumResults,
317
318 llvm::alignOf<DeclAccessPair>()));
319 memcpy(Results, &*Begin.getIterator(),
320 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000321 }
322}
323
John McCalle9ee23e2010-04-22 18:44:12 +0000324CXXRecordDecl *OverloadExpr::getNamingClass() const {
325 if (isa<UnresolvedLookupExpr>(this))
326 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
327 else
328 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
329}
330
John McCall865d4472009-11-19 22:55:06 +0000331// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000332DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000333 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000334 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000335 const DeclarationNameInfo &NameInfo,
336 const TemplateArgumentListInfo *Args)
337 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
338 true, true,
Douglas Gregor561f8122011-07-01 01:22:09 +0000339 (NameInfo.isInstantiationDependent() ||
340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000342 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000343 (QualifierLoc &&
344 QualifierLoc.getNestedNameSpecifier()
345 ->containsUnexpandedParameterPack()))),
346 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000347 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000348{
349 if (Args) {
350 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000351 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000352 bool ContainsUnexpandedParameterPack
353 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
355 Dependent,
356 InstantiationDependent,
357 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000358 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000361 }
362}
363
John McCallf7a1a742009-11-24 19:00:30 +0000364DependentScopeDeclRefExpr *
365DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000369 const TemplateArgumentListInfo *Args) {
370 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000371 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000372 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
373 else if (TemplateKWLoc.isValid())
374 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000375 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000376 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
377 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000378}
379
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000380DependentScopeDeclRefExpr *
381DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000382 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000383 unsigned NumTemplateArgs) {
384 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000385 if (HasTemplateKWAndArgsInfo)
386 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000387 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000388 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000389 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000390 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000391 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000392 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000393 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000394}
395
Chandler Carruth428edaf2010-10-25 08:47:36 +0000396SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000397 if (isa<CXXTemporaryObjectExpr>(this))
398 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
399
Douglas Gregor40749ee2010-11-03 00:35:38 +0000400 if (ParenRange.isValid())
401 return SourceRange(Loc, ParenRange.getEnd());
402
403 SourceLocation End = Loc;
404 for (unsigned I = getNumArgs(); I > 0; --I) {
405 const Expr *Arg = getArg(I-1);
406 if (!Arg->isDefaultArgument()) {
407 SourceLocation NewEnd = Arg->getLocEnd();
408 if (NewEnd.isValid()) {
409 End = NewEnd;
410 break;
411 }
412 }
413 }
414
415 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000416}
417
Douglas Gregorb4609802008-11-14 16:09:21 +0000418SourceRange CXXOperatorCallExpr::getSourceRange() const {
419 OverloadedOperatorKind Kind = getOperator();
420 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
421 if (getNumArgs() == 1)
422 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000423 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000424 getArg(0)->getSourceRange().getEnd());
425 else
426 // Postfix operator
Chandler Carruthd7650612011-04-02 09:47:38 +0000427 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000428 getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000429 } else if (Kind == OO_Arrow) {
430 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000431 } else if (Kind == OO_Call) {
432 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
433 } else if (Kind == OO_Subscript) {
434 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
435 } else if (getNumArgs() == 1) {
436 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
437 } else if (getNumArgs() == 2) {
438 return SourceRange(getArg(0)->getSourceRange().getBegin(),
439 getArg(1)->getSourceRange().getEnd());
440 } else {
441 return SourceRange();
442 }
443}
444
Ted Kremenekb2771592011-03-30 17:41:19 +0000445Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
446 if (const MemberExpr *MemExpr =
447 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-12-22 05:46:06 +0000448 return MemExpr->getBase();
449
450 // FIXME: Will eventually need to cope with member pointers.
451 return 0;
452}
453
Ted Kremenekb2771592011-03-30 17:41:19 +0000454CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
455 if (const MemberExpr *MemExpr =
456 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
457 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
458
459 // FIXME: Will eventually need to cope with member pointers.
460 return 0;
461}
462
463
Chandler Carruth007a9b12010-10-27 06:55:41 +0000464CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
465 Expr* ThisArg = getImplicitObjectArgument();
466 if (!ThisArg)
467 return 0;
468
469 if (ThisArg->getType()->isAnyPointerType())
470 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
471
472 return ThisArg->getType()->getAsCXXRecordDecl();
473}
474
Douglas Gregor00b98c22009-11-12 15:31:47 +0000475
Douglas Gregor49badde2008-10-27 19:41:14 +0000476//===----------------------------------------------------------------------===//
477// Named casts
478//===----------------------------------------------------------------------===//
479
480/// getCastName - Get the name of the C++ cast being used, e.g.,
481/// "static_cast", "dynamic_cast", "reinterpret_cast", or
482/// "const_cast". The returned pointer must not be freed.
483const char *CXXNamedCastExpr::getCastName() const {
484 switch (getStmtClass()) {
485 case CXXStaticCastExprClass: return "static_cast";
486 case CXXDynamicCastExprClass: return "dynamic_cast";
487 case CXXReinterpretCastExprClass: return "reinterpret_cast";
488 case CXXConstCastExprClass: return "const_cast";
489 default: return "<invalid cast>";
490 }
491}
Douglas Gregor506ae412009-01-16 18:33:17 +0000492
John McCallf871d0c2010-08-07 06:22:56 +0000493CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000494 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000495 CastKind K, Expr *Op,
496 const CXXCastPath *BasePath,
497 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000498 SourceLocation L,
499 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000500 unsigned PathSize = (BasePath ? BasePath->size() : 0);
501 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
502 + PathSize * sizeof(CXXBaseSpecifier*));
503 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000504 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
505 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000506 if (PathSize) E->setCastPath(*BasePath);
507 return E;
508}
509
510CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
511 unsigned PathSize) {
512 void *Buffer =
513 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
514 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
515}
516
517CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000518 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000519 CastKind K, Expr *Op,
520 const CXXCastPath *BasePath,
521 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000522 SourceLocation L,
523 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000524 unsigned PathSize = (BasePath ? BasePath->size() : 0);
525 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
526 + PathSize * sizeof(CXXBaseSpecifier*));
527 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000528 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
529 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000530 if (PathSize) E->setCastPath(*BasePath);
531 return E;
532}
533
534CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
535 unsigned PathSize) {
536 void *Buffer =
537 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
538 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
539}
540
Anders Carlsson0fee3302011-04-11 01:43:55 +0000541/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
542/// to always be null. For example:
543///
544/// struct A { };
545/// struct B final : A { };
546/// struct C { };
547///
548/// C *f(B* b) { return dynamic_cast<C*>(b); }
549bool CXXDynamicCastExpr::isAlwaysNull() const
550{
551 QualType SrcType = getSubExpr()->getType();
552 QualType DestType = getType();
553
554 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
555 SrcType = SrcPTy->getPointeeType();
556 DestType = DestType->castAs<PointerType>()->getPointeeType();
557 }
558
559 const CXXRecordDecl *SrcRD =
560 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
561
562 if (!SrcRD->hasAttr<FinalAttr>())
563 return false;
564
565 const CXXRecordDecl *DestRD =
566 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
567
568 return !DestRD->isDerivedFrom(SrcRD);
569}
570
John McCallf871d0c2010-08-07 06:22:56 +0000571CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000572CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
573 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000574 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000575 TypeSourceInfo *WrittenTy, SourceLocation L,
576 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000577 unsigned PathSize = (BasePath ? BasePath->size() : 0);
578 void *Buffer =
579 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
580 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000581 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
582 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000583 if (PathSize) E->setCastPath(*BasePath);
584 return E;
585}
586
587CXXReinterpretCastExpr *
588CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
589 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
590 + PathSize * sizeof(CXXBaseSpecifier*));
591 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
592}
593
John McCallf89e55a2010-11-18 06:31:45 +0000594CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
595 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000596 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000597 SourceLocation L,
598 SourceLocation RParenLoc) {
599 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000600}
601
602CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
603 return new (C) CXXConstCastExpr(EmptyShell());
604}
605
606CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000607CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000608 TypeSourceInfo *Written, SourceLocation L,
609 CastKind K, Expr *Op, const CXXCastPath *BasePath,
610 SourceLocation R) {
611 unsigned PathSize = (BasePath ? BasePath->size() : 0);
612 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
613 + PathSize * sizeof(CXXBaseSpecifier*));
614 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000615 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000616 if (PathSize) E->setCastPath(*BasePath);
617 return E;
618}
619
620CXXFunctionalCastExpr *
621CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
622 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
623 + PathSize * sizeof(CXXBaseSpecifier*));
624 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
625}
626
627
Douglas Gregor65222e82009-12-23 18:19:08 +0000628CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000629CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
630 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000631 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000632 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
633 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000634}
635
Mike Stump1eb44332009-09-09 15:08:12 +0000636CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000637 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000638 return new (C) CXXTemporary(Destructor);
639}
640
Mike Stump1eb44332009-09-09 15:08:12 +0000641CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000642 CXXTemporary *Temp,
643 Expr* SubExpr) {
Peter Collingbournebceb7552011-11-27 22:09:28 +0000644 assert((SubExpr->getType()->isRecordType() ||
645 SubExpr->getType()->isArrayType()) &&
646 "Expression bound to a temporary must have record or array type!");
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000647
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000648 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000649}
650
Anders Carlsson8e587a12009-05-30 20:56:46 +0000651CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000652 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000653 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000654 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000655 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000656 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000657 bool HadMultipleCandidates,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000658 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000659 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
660 Type->getType().getNonReferenceType(),
661 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000662 Cons, false, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000663 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000664 CXXConstructExpr::CK_Complete, parenRange),
665 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000666}
667
668SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000669 return SourceRange(Type->getTypeLoc().getBeginLoc(),
670 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000671}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000672
Mike Stump1eb44332009-09-09 15:08:12 +0000673CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000674 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000675 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000676 Expr **Args, unsigned NumArgs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000677 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000678 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000679 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000680 ConstructionKind ConstructKind,
681 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000682 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000683 Elidable, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000684 HadMultipleCandidates, ListInitialization,
685 ZeroInitialization, ConstructKind,
686 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000687}
688
Mike Stump1eb44332009-09-09 15:08:12 +0000689CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000690 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000691 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000692 Expr **args, unsigned numargs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000693 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000694 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000695 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000696 ConstructionKind ConstructKind,
697 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000698 : Expr(SC, T, VK_RValue, OK_Ordinary,
699 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000700 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000701 T->containsUnexpandedParameterPack()),
Douglas Gregora48e6762011-09-26 14:47:03 +0000702 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000703 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000704 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000705 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000706 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000707{
708 if (NumArgs) {
709 Args = new (C) Stmt*[NumArgs];
710
711 for (unsigned i = 0; i != NumArgs; ++i) {
712 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713
714 if (args[i]->isValueDependent())
715 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000716 if (args[i]->isInstantiationDependent())
717 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 if (args[i]->containsUnexpandedParameterPack())
719 ExprBits.ContainsUnexpandedParameterPack = true;
720
Douglas Gregor16006c92009-12-16 18:50:27 +0000721 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000722 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000723 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000724}
725
Douglas Gregor01d08012012-02-07 10:09:13 +0000726LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
727 LambdaCaptureKind Kind, VarDecl *Var,
728 SourceLocation EllipsisLoc)
729 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
730{
731 unsigned Bits = 0;
732 if (Implicit)
733 Bits |= Capture_Implicit;
734
735 switch (Kind) {
736 case LCK_This:
737 assert(Var == 0 && "'this' capture cannot have a variable!");
738 break;
739
740 case LCK_ByCopy:
741 Bits |= Capture_ByCopy;
742 // Fall through
743 case LCK_ByRef:
744 assert(Var && "capture must have a variable!");
745 break;
746 }
747 VarAndBits.setInt(Bits);
748}
749
750LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
751 if (capturesThis())
752 return LCK_This;
753
754 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
755}
756
757LambdaExpr::LambdaExpr(QualType T,
758 SourceRange IntroducerRange,
759 LambdaCaptureDefault CaptureDefault,
760 ArrayRef<Capture> Captures,
761 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000762 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000763 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000764 ArrayRef<VarDecl *> ArrayIndexVars,
765 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000766 SourceLocation ClosingBrace,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000767 unsigned ManglingNumber,
768 Decl *ContextDecl)
Douglas Gregor01d08012012-02-07 10:09:13 +0000769 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
770 T->isDependentType(), T->isDependentType(), T->isDependentType(),
771 /*ContainsUnexpandedParameterPack=*/false),
772 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000773 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000774 CaptureDefault(CaptureDefault),
775 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000776 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000777 ClosingBrace(ClosingBrace)
778{
779 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000780 CXXRecordDecl *Class = getLambdaClass();
781 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000782
783 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000784
785 // Copy captures.
786 ASTContext &Context = Class->getASTContext();
787 Data.NumCaptures = NumCaptures;
788 Data.NumExplicitCaptures = 0;
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000789 Data.ManglingNumber = ManglingNumber;
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000790 Data.ContextDecl = ContextDecl;
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000791 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
792 Capture *ToCapture = Data.Captures;
793 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
794 if (Captures[I].isExplicit())
795 ++Data.NumExplicitCaptures;
796
797 *ToCapture++ = Captures[I];
798 }
799
800 // Copy initialization expressions for the non-static data members.
801 Stmt **Stored = getStoredStmts();
802 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
803 *Stored++ = CaptureInits[I];
804
805 // Copy the body of the lambda.
806 *Stored++ = getCallOperator()->getBody();
807
808 // Copy the array index variables, if any.
809 HasArrayIndexVars = !ArrayIndexVars.empty();
810 if (HasArrayIndexVars) {
811 assert(ArrayIndexStarts.size() == NumCaptures);
812 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
813 sizeof(VarDecl *) * ArrayIndexVars.size());
814 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
815 sizeof(unsigned) * Captures.size());
816 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000817 }
818
819 if (ManglingNumber)
820 Class->ClearLinkageCache();
Douglas Gregor01d08012012-02-07 10:09:13 +0000821}
822
823LambdaExpr *LambdaExpr::Create(ASTContext &Context,
824 CXXRecordDecl *Class,
825 SourceRange IntroducerRange,
826 LambdaCaptureDefault CaptureDefault,
827 ArrayRef<Capture> Captures,
828 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000829 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000830 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000831 ArrayRef<VarDecl *> ArrayIndexVars,
832 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000833 SourceLocation ClosingBrace,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000834 unsigned ManglingNumber,
835 Decl *ContextDecl) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000836 // Determine the type of the expression (i.e., the type of the
837 // function object we're creating).
838 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000839
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000840 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
841 if (!ArrayIndexVars.empty())
842 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
843 + sizeof(unsigned) * (Captures.size() + 1);
844 void *Mem = Context.Allocate(Size);
845 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000846 Captures, ExplicitParams, ExplicitResultType,
847 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +0000848 ClosingBrace, ManglingNumber, ContextDecl);
Douglas Gregor01d08012012-02-07 10:09:13 +0000849}
850
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000851LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
852 unsigned NumArrayIndexVars) {
853 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
854 if (NumArrayIndexVars)
855 Size += sizeof(VarDecl) * NumArrayIndexVars
856 + sizeof(unsigned) * (NumCaptures + 1);
857 void *Mem = C.Allocate(Size);
858 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
859}
860
Douglas Gregorda8962a2012-02-13 15:44:47 +0000861LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000862 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000863}
864
865LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000866 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000867}
868
869LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
870 return capture_begin();
871}
872
873LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
874 struct CXXRecordDecl::LambdaDefinitionData &Data
875 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000876 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000877}
878
879LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
880 return explicit_capture_end();
881}
882
883LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
884 return capture_end();
885}
886
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000887ArrayRef<VarDecl *>
888LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000889 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000890
891 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000892 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
893 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000894 VarDecl **IndexVars = getArrayIndexVars();
895 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000896 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
897 IndexVars + IndexStarts[Index + 1]);
898}
899
Douglas Gregor01d08012012-02-07 10:09:13 +0000900CXXRecordDecl *LambdaExpr::getLambdaClass() const {
901 return getType()->getAsCXXRecordDecl();
902}
903
904CXXMethodDecl *LambdaExpr::getCallOperator() const {
905 CXXRecordDecl *Record = getLambdaClass();
906 DeclarationName Name
907 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
908 DeclContext::lookup_result Calls = Record->lookup(Name);
909 assert(Calls.first != Calls.second && "Missing lambda call operator!");
910 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
911 assert(Calls.first == Calls.second && "More than lambda one call operator?");
912 return Result;
913}
914
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000915CompoundStmt *LambdaExpr::getBody() const {
916 if (!getStoredStmts()[NumCaptures])
917 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
918
919 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
920}
921
Douglas Gregor01d08012012-02-07 10:09:13 +0000922bool LambdaExpr::isMutable() const {
923 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
924}
925
John McCall80ee6e82011-11-10 05:35:25 +0000926ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
927 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000928 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000929 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000930 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000931 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000932 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000933 SubExpr(subexpr) {
934 ExprWithCleanupsBits.NumObjects = objects.size();
935 for (unsigned i = 0, e = objects.size(); i != e; ++i)
936 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000937}
938
John McCall80ee6e82011-11-10 05:35:25 +0000939ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
940 ArrayRef<CleanupObject> objects) {
941 size_t size = sizeof(ExprWithCleanups)
942 + objects.size() * sizeof(CleanupObject);
943 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
944 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000945}
946
John McCall80ee6e82011-11-10 05:35:25 +0000947ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
948 : Expr(ExprWithCleanupsClass, empty) {
949 ExprWithCleanupsBits.NumObjects = numObjects;
950}
Chris Lattnerd2598362010-05-10 00:25:06 +0000951
John McCall80ee6e82011-11-10 05:35:25 +0000952ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
953 unsigned numObjects) {
954 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
955 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
956 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000957}
958
Douglas Gregorab6677e2010-09-08 00:15:04 +0000959CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000960 SourceLocation LParenLoc,
961 Expr **Args,
962 unsigned NumArgs,
963 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000964 : Expr(CXXUnresolvedConstructExprClass,
965 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000966 (Type->getType()->isLValueReferenceType() ? VK_LValue
967 :Type->getType()->isRValueReferenceType()? VK_XValue
968 :VK_RValue),
969 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000970 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000971 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000972 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000973 LParenLoc(LParenLoc),
974 RParenLoc(RParenLoc),
975 NumArgs(NumArgs) {
976 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000977 for (unsigned I = 0; I != NumArgs; ++I) {
978 if (Args[I]->containsUnexpandedParameterPack())
979 ExprBits.ContainsUnexpandedParameterPack = true;
980
981 StoredArgs[I] = Args[I];
982 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000983}
984
985CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000986CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000987 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000988 SourceLocation LParenLoc,
989 Expr **Args,
990 unsigned NumArgs,
991 SourceLocation RParenLoc) {
992 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
993 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000994 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000995 Args, NumArgs, RParenLoc);
996}
997
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000998CXXUnresolvedConstructExpr *
999CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1000 Stmt::EmptyShell Empty;
1001 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1002 sizeof(Expr *) * NumArgs);
1003 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1004}
1005
Douglas Gregorab6677e2010-09-08 00:15:04 +00001006SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1007 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1008}
1009
John McCall865d4472009-11-19 22:55:06 +00001010CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001011 Expr *Base, QualType BaseType,
1012 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001013 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001014 NestedNameSpecifierLoc QualifierLoc,
1015 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001016 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001017 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001018 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001019 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001020 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001021 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001022 (QualifierLoc &&
1023 QualifierLoc.getNestedNameSpecifier()
1024 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001025 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001026 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001027 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001028 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001029 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001030 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001031 if (TemplateArgs) {
1032 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001033 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001034 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001035 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1036 Dependent,
1037 InstantiationDependent,
1038 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001039 if (ContainsUnexpandedParameterPack)
1040 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001041 } else if (TemplateKWLoc.isValid()) {
1042 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001043 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001044}
1045
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001046CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1047 Expr *Base, QualType BaseType,
1048 bool IsArrow,
1049 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001050 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001051 NamedDecl *FirstQualifierFoundInScope,
1052 DeclarationNameInfo MemberNameInfo)
1053 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001054 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001056 (QualifierLoc &&
1057 QualifierLoc.getNestedNameSpecifier()->
1058 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001059 MemberNameInfo.containsUnexpandedParameterPack())),
1060 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001061 HasTemplateKWAndArgsInfo(false),
1062 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001063 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1064 MemberNameInfo(MemberNameInfo) { }
1065
John McCall865d4472009-11-19 22:55:06 +00001066CXXDependentScopeMemberExpr *
1067CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001068 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001069 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001070 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001071 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001072 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001073 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001074 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001075 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001076 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1077 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001078 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001079 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001080 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001082 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1083 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1084 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001085
Chris Lattner32488542010-10-30 05:14:06 +00001086 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001087 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1088 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001089 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001090 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001091 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001092 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001093}
1094
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001095CXXDependentScopeMemberExpr *
1096CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001097 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001098 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001099 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001100 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001101 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001102 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001103 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001104
1105 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001106 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001107 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001108 CXXDependentScopeMemberExpr *E
1109 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001110 0, SourceLocation(),
1111 NestedNameSpecifierLoc(),
1112 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001113 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001114 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001115 return E;
1116}
1117
Douglas Gregor4c9be892011-02-28 20:01:57 +00001118bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1119 if (Base == 0)
1120 return true;
1121
Douglas Gregor75e85042011-03-02 21:06:53 +00001122 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001123}
1124
John McCall864c0412011-04-26 20:42:42 +00001125static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1126 UnresolvedSetIterator end) {
1127 do {
1128 NamedDecl *decl = *begin;
1129 if (isa<UnresolvedUsingValueDecl>(decl))
1130 return false;
1131 if (isa<UsingShadowDecl>(decl))
1132 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1133
1134 // Unresolved member expressions should only contain methods and
1135 // method templates.
1136 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1137
1138 if (isa<FunctionTemplateDecl>(decl))
1139 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1140 if (cast<CXXMethodDecl>(decl)->isStatic())
1141 return false;
1142 } while (++begin != end);
1143
1144 return true;
1145}
1146
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001147UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001148 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001149 Expr *Base, QualType BaseType,
1150 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001151 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001152 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001153 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001154 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001155 const TemplateArgumentListInfo *TemplateArgs,
1156 UnresolvedSetIterator Begin,
1157 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001158 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1159 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001160 // Dependent
1161 ((Base && Base->isTypeDependent()) ||
1162 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001163 ((Base && Base->isInstantiationDependent()) ||
1164 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001165 // Contains unexpanded parameter pack
1166 ((Base && Base->containsUnexpandedParameterPack()) ||
1167 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001168 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1169 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001170
1171 // Check whether all of the members are non-static member functions,
1172 // and if so, mark give this bound-member type instead of overload type.
1173 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1174 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001175}
1176
Douglas Gregor4c9be892011-02-28 20:01:57 +00001177bool UnresolvedMemberExpr::isImplicitAccess() const {
1178 if (Base == 0)
1179 return true;
1180
Douglas Gregor75e85042011-03-02 21:06:53 +00001181 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001182}
1183
John McCall129e2df2009-11-30 22:42:35 +00001184UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001185UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001186 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001187 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001188 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001189 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001190 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001191 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001192 const TemplateArgumentListInfo *TemplateArgs,
1193 UnresolvedSetIterator Begin,
1194 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001195 std::size_t size = sizeof(UnresolvedMemberExpr);
1196 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001197 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1198 else if (TemplateKWLoc.isValid())
1199 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001200
Chris Lattner32488542010-10-30 05:14:06 +00001201 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001202 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001203 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001204 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001205 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001206}
1207
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001208UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001209UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001210 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001211 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001212 if (HasTemplateKWAndArgsInfo)
1213 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001214
Chris Lattner32488542010-10-30 05:14:06 +00001215 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001216 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001217 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001218 return E;
1219}
1220
John McCallc373d482010-01-27 01:50:18 +00001221CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1222 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1223
1224 // If there was a nested name specifier, it names the naming class.
1225 // It can't be dependent: after all, we were actually able to do the
1226 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001227 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001228 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001229 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001230 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001231 Record = T->getAsCXXRecordDecl();
1232 assert(Record && "qualifier in member expression does not name record");
1233 }
John McCallc373d482010-01-27 01:50:18 +00001234 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001235 else {
John McCallc373d482010-01-27 01:50:18 +00001236 QualType BaseType = getBaseType().getNonReferenceType();
1237 if (isArrow()) {
1238 const PointerType *PT = BaseType->getAs<PointerType>();
1239 assert(PT && "base of arrow member access is not pointer");
1240 BaseType = PT->getPointeeType();
1241 }
1242
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001243 Record = BaseType->getAsCXXRecordDecl();
1244 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001245 }
1246
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001247 return Record;
John McCallc373d482010-01-27 01:50:18 +00001248}
1249
Douglas Gregorc7793c72011-01-15 01:15:58 +00001250SubstNonTypeTemplateParmPackExpr::
1251SubstNonTypeTemplateParmPackExpr(QualType T,
1252 NonTypeTemplateParmDecl *Param,
1253 SourceLocation NameLoc,
1254 const TemplateArgument &ArgPack)
1255 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001256 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001257 Param(Param), Arguments(ArgPack.pack_begin()),
1258 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1259
1260TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1261 return TemplateArgument(Arguments, NumArguments);
1262}
1263
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001264TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1265 ArrayRef<TypeSourceInfo *> Args,
1266 SourceLocation RParenLoc,
1267 bool Value)
1268 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1269 /*TypeDependent=*/false,
1270 /*ValueDependent=*/false,
1271 /*InstantiationDependent=*/false,
1272 /*ContainsUnexpandedParameterPack=*/false),
1273 Loc(Loc), RParenLoc(RParenLoc)
1274{
1275 TypeTraitExprBits.Kind = Kind;
1276 TypeTraitExprBits.Value = Value;
1277 TypeTraitExprBits.NumArgs = Args.size();
1278
1279 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1280
1281 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1282 if (Args[I]->getType()->isDependentType())
1283 setValueDependent(true);
1284 if (Args[I]->getType()->isInstantiationDependentType())
1285 setInstantiationDependent(true);
1286 if (Args[I]->getType()->containsUnexpandedParameterPack())
1287 setContainsUnexpandedParameterPack(true);
1288
1289 ToArgs[I] = Args[I];
1290 }
1291}
1292
1293TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1294 SourceLocation Loc,
1295 TypeTrait Kind,
1296 ArrayRef<TypeSourceInfo *> Args,
1297 SourceLocation RParenLoc,
1298 bool Value) {
1299 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1300 void *Mem = C.Allocate(Size);
1301 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1302}
1303
1304TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1305 unsigned NumArgs) {
1306 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1307 void *Mem = C.Allocate(Size);
1308 return new (Mem) TypeTraitExpr(EmptyShell());
1309}
1310
David Blaikie99ba9e32011-12-20 02:48:34 +00001311void ArrayTypeTraitExpr::anchor() { }