blob: 039d70cfc56eb62a8058ce94e375bc9ee8213c56 [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremeneke3a0c142007-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 Gregor993603d2008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor9da64192010-04-26 22:37:10 +000021
Ted Kremeneke3a0c142007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor9da64192010-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 Pichet9f4f2072010-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 Gregor747eb782010-07-08 06:14:04 +000038// CXXScalarValueInitExpr
Douglas Gregor2b88c112010-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 Redlbd150f42008-11-21 19:14:01 +000046// CXXNewExpr
Ted Kremenek9d6eb402010-02-11 22:51:03 +000047CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redlc3a3c602012-02-16 11:35:52 +000048 FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-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 McCall7decc9e2010-11-18 06:31:45 +000056 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000057 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000058 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000059 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +000060 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
61 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +000062 StartLoc(startLoc), DirectInitRange(directInitRange),
63 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-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 Redlbd150f42008-11-21 19:14:01 +000068 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000069 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000070 if (arraySize->isInstantiationDependent())
71 ExprBits.InstantiationDependent = true;
72
Douglas Gregora6e053e2010-12-15 01:34:56 +000073 if (arraySize->containsUnexpandedParameterPack())
74 ExprBits.ContainsUnexpandedParameterPack = true;
75
Sebastian Redl351bb782008-12-02 14:43:59 +000076 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000077 }
78
Sebastian Redl6047f072012-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 Gregora6e053e2010-12-15 01:34:56 +000089 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000090 if (placementArgs[j]->isInstantiationDependent())
91 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +000092 if (placementArgs[j]->containsUnexpandedParameterPack())
93 ExprBits.ContainsUnexpandedParameterPack = true;
94
Sebastian Redlbd150f42008-11-21 19:14:01 +000095 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +000096 }
Sebastian Redlbd150f42008-11-21 19:14:01 +000097}
98
Chris Lattnerabfb58d2010-05-10 01:22:27 +000099void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000100 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000101 assert(SubExprs == 0 && "SubExprs already allocated");
102 Array = isArray;
103 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000104
105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000106 SubExprs = new (C) Stmt*[TotalSize];
107}
108
Sebastian Redl31ad7542011-03-13 17:09:40 +0000109bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000110 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000111 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000112}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000113
Sebastian Redl6047f072012-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-Gay2150d382012-02-16 22:15:50 +0000123 llvm_unreachable("bogus initialization style");
Sebastian Redl6047f072012-02-16 12:22:20 +0000124}
125
Sebastian Redlbd150f42008-11-21 19:14:01 +0000126// CXXDeleteExpr
Douglas Gregor6ed2fee2010-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 Silverstein20f7ab72010-10-20 00:38:15 +0000136 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000137 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000138
139 if (ArgType->isDependentType() && !ArgType->isPointerType())
140 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000141
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000142 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000143}
144
Douglas Gregorad8a3362009-09-04 17:36:40 +0000145// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000146PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
147 : Type(Info)
148{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000150}
151
John McCalldb40c7f2010-12-14 08:05:40 +0000152CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-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 McCalldb40c7f2010-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 Gregora6e053e2010-12-15 01:34:56 +0000164 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-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 Gregora6e053e2010-12-15 01:34:56 +0000173 // ContainsUnexpandedParameterPack
174 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()
177 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000178 (ScopeType &&
179 ScopeType->getType()->containsUnexpandedParameterPack()) ||
180 (DestroyedType.getTypeSourceInfo() &&
181 DestroyedType.getTypeSourceInfo()->getType()
182 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
186 DestroyedType(DestroyedType) { }
187
Douglas Gregor678f90d2010-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 Gregor651fe5e2010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000200}
201
John McCalld14a8642009-11-21 08:51:07 +0000202// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000203UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000204UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000205 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000206 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000207 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000208 const DeclarationNameInfo &NameInfo,
209 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000210 const TemplateArgumentListInfo *Args,
211 UnresolvedSetIterator Begin,
212 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000213{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000214 assert(Args || TemplateKWLoc.isValid());
215 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000216 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000217 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000218 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
219 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000220 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000221 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000222}
223
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000224UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000225UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
226 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000227 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000228 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000229 if (HasTemplateKWAndArgsInfo)
230 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000231
Chris Lattner5c0b4052010-10-30 05:14:06 +0000232 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000233 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000234 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000235 return E;
236}
237
Douglas Gregora6e053e2010-12-15 01:34:56 +0000238OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000239 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000240 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000241 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000242 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000243 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000244 UnresolvedSetIterator End,
245 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000246 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000247 bool KnownContainsUnexpandedParameterPack)
248 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
249 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000250 (KnownInstantiationDependent ||
251 NameInfo.isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000254 (KnownContainsUnexpandedParameterPack ||
255 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000256 (QualifierLoc &&
257 QualifierLoc.getNestedNameSpecifier()
258 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000259 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
260 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000261 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000262{
Douglas Gregora6e053e2010-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 Gregor678d76c2011-07-01 01:22:09 +0000286 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000287 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000288 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
289 Dependent,
290 InstantiationDependent,
291 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000292
293 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000294 ExprBits.TypeDependent = true;
295 ExprBits.ValueDependent = true;
296 }
297 if (InstantiationDependent)
298 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000299 if (ContainsUnexpandedParameterPack)
300 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000301 } else if (TemplateKWLoc.isValid()) {
302 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000303 }
304
305 if (isTypeDependent())
306 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-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 Gregorc69978f2010-05-23 19:36:40 +0000314 if (NumResults) {
Douglas Gregora6e053e2010-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 Gregorc69978f2010-05-23 19:36:40 +0000321 }
322}
323
John McCall8c12dc42010-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 McCall8cd78132009-11-19 22:55:06 +0000331// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000332DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000333 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000334 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-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 Gregor678d76c2011-07-01 01:22:09 +0000339 (NameInfo.isInstantiationDependent() ||
340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000342 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000343 (QualifierLoc &&
344 QualifierLoc.getNestedNameSpecifier()
345 ->containsUnexpandedParameterPack()))),
346 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000347 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000348{
349 if (Args) {
350 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000351 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000352 bool ContainsUnexpandedParameterPack
353 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
355 Dependent,
356 InstantiationDependent,
357 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000361 }
362}
363
John McCalle66edc12009-11-24 19:00:30 +0000364DependentScopeDeclRefExpr *
365DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000369 const TemplateArgumentListInfo *Args) {
370 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000371 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000372 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
373 else if (TemplateKWLoc.isValid())
374 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000375 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000376 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
377 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000378}
379
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000380DependentScopeDeclRefExpr *
381DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000382 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000383 unsigned NumTemplateArgs) {
384 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 if (HasTemplateKWAndArgsInfo)
386 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000387 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000388 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000389 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000390 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000391 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000392 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000393 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000394}
395
Chandler Carruth01718152010-10-25 08:47:36 +0000396SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000397 if (isa<CXXTemporaryObjectExpr>(this))
398 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
399
Douglas Gregor15417cf2010-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 Kremenek49ace5c2009-12-23 04:00:48 +0000416}
417
Douglas Gregor993603d2008-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 Stump11289f42009-09-09 15:08:12 +0000423 return SourceRange(getOperatorLoc(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000424 getArg(0)->getSourceRange().getEnd());
425 else
426 // Postfix operator
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000427 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000428 getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000429 } else if (Kind == OO_Arrow) {
430 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-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 Kremenek98a24e32011-03-30 17:41:19 +0000445Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
446 if (const MemberExpr *MemExpr =
447 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor97fd6e22008-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 Kremenek98a24e32011-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 Carruth00426b42010-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 Gregoref986e82009-11-12 15:31:47 +0000475
Douglas Gregore200adc2008-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 Gregordd04d332009-01-16 18:33:17 +0000492
John McCallcf142162010-08-07 06:22:56 +0000493CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000494 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000495 CastKind K, Expr *Op,
496 const CXXCastPath *BasePath,
497 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000498 SourceLocation L,
499 SourceLocation RParenLoc) {
John McCallcf142162010-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 Gregor4478f852011-01-12 22:41:29 +0000504 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
505 RParenLoc);
John McCallcf142162010-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 McCall7decc9e2010-11-18 06:31:45 +0000518 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000519 CastKind K, Expr *Op,
520 const CXXCastPath *BasePath,
521 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000522 SourceLocation L,
523 SourceLocation RParenLoc) {
John McCallcf142162010-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 Gregor4478f852011-01-12 22:41:29 +0000528 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
529 RParenLoc);
John McCallcf142162010-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 Carlsson267c0c92011-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 McCallcf142162010-08-07 06:22:56 +0000571CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000572CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
573 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000574 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000575 TypeSourceInfo *WrittenTy, SourceLocation L,
576 SourceLocation RParenLoc) {
John McCallcf142162010-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 Gregor4478f852011-01-12 22:41:29 +0000581 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
582 RParenLoc);
John McCallcf142162010-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 McCall7decc9e2010-11-18 06:31:45 +0000594CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
595 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000596 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000597 SourceLocation L,
598 SourceLocation RParenLoc) {
599 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000600}
601
602CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
603 return new (C) CXXConstCastExpr(EmptyShell());
604}
605
606CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000607CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-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 McCall7decc9e2010-11-18 06:31:45 +0000615 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-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
Richard Smithc67fdd42012-03-07 08:35:16 +0000627UserDefinedLiteral::LiteralOperatorKind
628UserDefinedLiteral::getLiteralOperatorKind() const {
629 if (getNumArgs() == 0)
630 return LOK_Template;
631 if (getNumArgs() == 2)
632 return LOK_String;
633
634 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
635 QualType ParamTy =
636 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
637 if (ParamTy->isPointerType())
638 return LOK_Raw;
639 if (ParamTy->isAnyCharacterType())
640 return LOK_Character;
641 if (ParamTy->isIntegerType())
642 return LOK_Integer;
643 if (ParamTy->isFloatingType())
644 return LOK_Floating;
645
646 llvm_unreachable("unknown kind of literal operator");
647}
648
649Expr *UserDefinedLiteral::getCookedLiteral() {
650#ifndef NDEBUG
651 LiteralOperatorKind LOK = getLiteralOperatorKind();
652 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
653#endif
654 return getArg(0);
655}
656
657const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
658 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
659}
John McCallcf142162010-08-07 06:22:56 +0000660
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000661CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000662CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
663 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000664 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000665 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
666 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000667}
668
Mike Stump11289f42009-09-09 15:08:12 +0000669CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000670 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000671 return new (C) CXXTemporary(Destructor);
672}
673
Mike Stump11289f42009-09-09 15:08:12 +0000674CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000675 CXXTemporary *Temp,
676 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000677 assert((SubExpr->getType()->isRecordType() ||
678 SubExpr->getType()->isArrayType()) &&
679 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000680
Douglas Gregora6e053e2010-12-15 01:34:56 +0000681 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000682}
683
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000684CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000685 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000686 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000687 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000688 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000689 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000690 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000691 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000692 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
693 Type->getType().getNonReferenceType(),
694 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000695 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000696 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000697 CXXConstructExpr::CK_Complete, parenRange),
698 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000699}
700
701SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000702 return SourceRange(Type->getTypeLoc().getBeginLoc(),
703 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000704}
Anders Carlsson6f287832009-04-21 02:22:11 +0000705
Mike Stump11289f42009-09-09 15:08:12 +0000706CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000707 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000708 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000709 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000710 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000711 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000712 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000713 ConstructionKind ConstructKind,
714 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000715 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000716 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000717 HadMultipleCandidates, ListInitialization,
718 ZeroInitialization, ConstructKind,
719 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000720}
721
Mike Stump11289f42009-09-09 15:08:12 +0000722CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000723 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000724 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000725 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000726 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000727 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000728 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000729 ConstructionKind ConstructKind,
730 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000731 : Expr(SC, T, VK_RValue, OK_Ordinary,
732 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000733 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000734 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000735 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000736 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000737 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000738 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000739 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000740{
741 if (NumArgs) {
742 Args = new (C) Stmt*[NumArgs];
743
744 for (unsigned i = 0; i != NumArgs; ++i) {
745 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000746
747 if (args[i]->isValueDependent())
748 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000749 if (args[i]->isInstantiationDependent())
750 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000751 if (args[i]->containsUnexpandedParameterPack())
752 ExprBits.ContainsUnexpandedParameterPack = true;
753
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000754 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000755 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000756 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000757}
758
Douglas Gregore31e6062012-02-07 10:09:13 +0000759LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
760 LambdaCaptureKind Kind, VarDecl *Var,
761 SourceLocation EllipsisLoc)
762 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
763{
764 unsigned Bits = 0;
765 if (Implicit)
766 Bits |= Capture_Implicit;
767
768 switch (Kind) {
769 case LCK_This:
770 assert(Var == 0 && "'this' capture cannot have a variable!");
771 break;
772
773 case LCK_ByCopy:
774 Bits |= Capture_ByCopy;
775 // Fall through
776 case LCK_ByRef:
777 assert(Var && "capture must have a variable!");
778 break;
779 }
780 VarAndBits.setInt(Bits);
781}
782
783LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
784 if (capturesThis())
785 return LCK_This;
786
787 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
788}
789
790LambdaExpr::LambdaExpr(QualType T,
791 SourceRange IntroducerRange,
792 LambdaCaptureDefault CaptureDefault,
793 ArrayRef<Capture> Captures,
794 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000795 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000796 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000797 ArrayRef<VarDecl *> ArrayIndexVars,
798 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor63798542012-02-20 19:44:39 +0000799 SourceLocation ClosingBrace,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000800 unsigned ManglingNumber,
801 Decl *ContextDecl)
Douglas Gregore31e6062012-02-07 10:09:13 +0000802 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
803 T->isDependentType(), T->isDependentType(), T->isDependentType(),
804 /*ContainsUnexpandedParameterPack=*/false),
805 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000806 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000807 CaptureDefault(CaptureDefault),
808 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000809 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000810 ClosingBrace(ClosingBrace)
811{
812 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000813 CXXRecordDecl *Class = getLambdaClass();
814 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000815
816 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000817
818 // Copy captures.
819 ASTContext &Context = Class->getASTContext();
820 Data.NumCaptures = NumCaptures;
821 Data.NumExplicitCaptures = 0;
Douglas Gregor63798542012-02-20 19:44:39 +0000822 Data.ManglingNumber = ManglingNumber;
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000823 Data.ContextDecl = ContextDecl;
Douglas Gregore5561632012-02-13 17:20:40 +0000824 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
825 Capture *ToCapture = Data.Captures;
826 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
827 if (Captures[I].isExplicit())
828 ++Data.NumExplicitCaptures;
829
830 *ToCapture++ = Captures[I];
831 }
832
833 // Copy initialization expressions for the non-static data members.
834 Stmt **Stored = getStoredStmts();
835 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
836 *Stored++ = CaptureInits[I];
837
838 // Copy the body of the lambda.
839 *Stored++ = getCallOperator()->getBody();
840
841 // Copy the array index variables, if any.
842 HasArrayIndexVars = !ArrayIndexVars.empty();
843 if (HasArrayIndexVars) {
844 assert(ArrayIndexStarts.size() == NumCaptures);
845 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
846 sizeof(VarDecl *) * ArrayIndexVars.size());
847 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
848 sizeof(unsigned) * Captures.size());
849 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000850 }
851
852 if (ManglingNumber)
853 Class->ClearLinkageCache();
Douglas Gregore31e6062012-02-07 10:09:13 +0000854}
855
856LambdaExpr *LambdaExpr::Create(ASTContext &Context,
857 CXXRecordDecl *Class,
858 SourceRange IntroducerRange,
859 LambdaCaptureDefault CaptureDefault,
860 ArrayRef<Capture> Captures,
861 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000862 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000863 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000864 ArrayRef<VarDecl *> ArrayIndexVars,
865 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor63798542012-02-20 19:44:39 +0000866 SourceLocation ClosingBrace,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000867 unsigned ManglingNumber,
868 Decl *ContextDecl) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000869 // Determine the type of the expression (i.e., the type of the
870 // function object we're creating).
871 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000872
Douglas Gregore5561632012-02-13 17:20:40 +0000873 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
874 if (!ArrayIndexVars.empty())
875 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
876 + sizeof(unsigned) * (Captures.size() + 1);
877 void *Mem = Context.Allocate(Size);
878 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000879 Captures, ExplicitParams, ExplicitResultType,
880 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000881 ClosingBrace, ManglingNumber, ContextDecl);
Douglas Gregore31e6062012-02-07 10:09:13 +0000882}
883
Douglas Gregor99ae8062012-02-14 17:54:36 +0000884LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
885 unsigned NumArrayIndexVars) {
886 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
887 if (NumArrayIndexVars)
888 Size += sizeof(VarDecl) * NumArrayIndexVars
889 + sizeof(unsigned) * (NumCaptures + 1);
890 void *Mem = C.Allocate(Size);
891 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
892}
893
Douglas Gregorc8a73492012-02-13 15:44:47 +0000894LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000895 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000896}
897
898LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000899 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000900}
901
902LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
903 return capture_begin();
904}
905
906LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
907 struct CXXRecordDecl::LambdaDefinitionData &Data
908 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000909 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000910}
911
912LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
913 return explicit_capture_end();
914}
915
916LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
917 return capture_end();
918}
919
Douglas Gregor54fcea62012-02-13 16:35:30 +0000920ArrayRef<VarDecl *>
921LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000922 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000923
924 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000925 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
926 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000927 VarDecl **IndexVars = getArrayIndexVars();
928 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000929 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
930 IndexVars + IndexStarts[Index + 1]);
931}
932
Douglas Gregore31e6062012-02-07 10:09:13 +0000933CXXRecordDecl *LambdaExpr::getLambdaClass() const {
934 return getType()->getAsCXXRecordDecl();
935}
936
937CXXMethodDecl *LambdaExpr::getCallOperator() const {
938 CXXRecordDecl *Record = getLambdaClass();
939 DeclarationName Name
940 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
941 DeclContext::lookup_result Calls = Record->lookup(Name);
942 assert(Calls.first != Calls.second && "Missing lambda call operator!");
943 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
944 assert(Calls.first == Calls.second && "More than lambda one call operator?");
945 return Result;
946}
947
Douglas Gregor99ae8062012-02-14 17:54:36 +0000948CompoundStmt *LambdaExpr::getBody() const {
949 if (!getStoredStmts()[NumCaptures])
950 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
951
952 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
953}
954
Douglas Gregore31e6062012-02-07 10:09:13 +0000955bool LambdaExpr::isMutable() const {
956 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
957}
958
John McCall28fc7092011-11-10 05:35:25 +0000959ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
960 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000961 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000962 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000963 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000964 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000965 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000966 SubExpr(subexpr) {
967 ExprWithCleanupsBits.NumObjects = objects.size();
968 for (unsigned i = 0, e = objects.size(); i != e; ++i)
969 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000970}
971
John McCall28fc7092011-11-10 05:35:25 +0000972ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
973 ArrayRef<CleanupObject> objects) {
974 size_t size = sizeof(ExprWithCleanups)
975 + objects.size() * sizeof(CleanupObject);
976 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
977 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000978}
979
John McCall28fc7092011-11-10 05:35:25 +0000980ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
981 : Expr(ExprWithCleanupsClass, empty) {
982 ExprWithCleanupsBits.NumObjects = numObjects;
983}
Chris Lattnercba86142010-05-10 00:25:06 +0000984
John McCall28fc7092011-11-10 05:35:25 +0000985ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
986 unsigned numObjects) {
987 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
988 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
989 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000990}
991
Douglas Gregor2b88c112010-09-08 00:15:04 +0000992CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000993 SourceLocation LParenLoc,
994 Expr **Args,
995 unsigned NumArgs,
996 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000997 : Expr(CXXUnresolvedConstructExprClass,
998 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000999 (Type->getType()->isLValueReferenceType() ? VK_LValue
1000 :Type->getType()->isRValueReferenceType()? VK_XValue
1001 :VK_RValue),
1002 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001003 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001004 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001005 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001006 LParenLoc(LParenLoc),
1007 RParenLoc(RParenLoc),
1008 NumArgs(NumArgs) {
1009 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001010 for (unsigned I = 0; I != NumArgs; ++I) {
1011 if (Args[I]->containsUnexpandedParameterPack())
1012 ExprBits.ContainsUnexpandedParameterPack = true;
1013
1014 StoredArgs[I] = Args[I];
1015 }
Douglas Gregorce934142009-05-20 18:46:25 +00001016}
1017
1018CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001019CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001020 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001021 SourceLocation LParenLoc,
1022 Expr **Args,
1023 unsigned NumArgs,
1024 SourceLocation RParenLoc) {
1025 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1026 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001027 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +00001028 Args, NumArgs, RParenLoc);
1029}
1030
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001031CXXUnresolvedConstructExpr *
1032CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1033 Stmt::EmptyShell Empty;
1034 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1035 sizeof(Expr *) * NumArgs);
1036 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1037}
1038
Douglas Gregor2b88c112010-09-08 00:15:04 +00001039SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1040 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1041}
1042
John McCall8cd78132009-11-19 22:55:06 +00001043CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001044 Expr *Base, QualType BaseType,
1045 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001046 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001047 NestedNameSpecifierLoc QualifierLoc,
1048 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001049 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001050 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001051 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001052 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001053 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001054 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001055 (QualifierLoc &&
1056 QualifierLoc.getNestedNameSpecifier()
1057 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001058 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001059 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001060 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001061 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001062 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001063 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001064 if (TemplateArgs) {
1065 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001066 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001067 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001068 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1069 Dependent,
1070 InstantiationDependent,
1071 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001072 if (ContainsUnexpandedParameterPack)
1073 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001074 } else if (TemplateKWLoc.isValid()) {
1075 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001076 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001077}
1078
Douglas Gregora6e053e2010-12-15 01:34:56 +00001079CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1080 Expr *Base, QualType BaseType,
1081 bool IsArrow,
1082 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001083 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001084 NamedDecl *FirstQualifierFoundInScope,
1085 DeclarationNameInfo MemberNameInfo)
1086 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001087 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001088 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001089 (QualifierLoc &&
1090 QualifierLoc.getNestedNameSpecifier()->
1091 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001092 MemberNameInfo.containsUnexpandedParameterPack())),
1093 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001094 HasTemplateKWAndArgsInfo(false),
1095 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001096 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1097 MemberNameInfo(MemberNameInfo) { }
1098
John McCall8cd78132009-11-19 22:55:06 +00001099CXXDependentScopeMemberExpr *
1100CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001101 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001102 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001103 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001104 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001105 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001106 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001107 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001108 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001109 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1110 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001111 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001112 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001113 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Abramo Bagnara7945c982012-01-27 09:46:47 +00001115 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1116 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1117 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001118
Chris Lattner5c0b4052010-10-30 05:14:06 +00001119 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001120 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1121 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001122 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001123 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001124 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001125 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001126}
1127
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001128CXXDependentScopeMemberExpr *
1129CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001130 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001131 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001132 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001133 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001134 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001135 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001136 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001137
1138 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001139 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001140 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001141 CXXDependentScopeMemberExpr *E
1142 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001143 0, SourceLocation(),
1144 NestedNameSpecifierLoc(),
1145 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001146 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001147 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001148 return E;
1149}
1150
Douglas Gregor0da1d432011-02-28 20:01:57 +00001151bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1152 if (Base == 0)
1153 return true;
1154
Douglas Gregor25b7e052011-03-02 21:06:53 +00001155 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001156}
1157
John McCall0009fcc2011-04-26 20:42:42 +00001158static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1159 UnresolvedSetIterator end) {
1160 do {
1161 NamedDecl *decl = *begin;
1162 if (isa<UnresolvedUsingValueDecl>(decl))
1163 return false;
1164 if (isa<UsingShadowDecl>(decl))
1165 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1166
1167 // Unresolved member expressions should only contain methods and
1168 // method templates.
1169 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1170
1171 if (isa<FunctionTemplateDecl>(decl))
1172 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1173 if (cast<CXXMethodDecl>(decl)->isStatic())
1174 return false;
1175 } while (++begin != end);
1176
1177 return true;
1178}
1179
Douglas Gregora6e053e2010-12-15 01:34:56 +00001180UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001181 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001182 Expr *Base, QualType BaseType,
1183 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001184 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001185 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001186 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001187 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001188 const TemplateArgumentListInfo *TemplateArgs,
1189 UnresolvedSetIterator Begin,
1190 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001191 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1192 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001193 // Dependent
1194 ((Base && Base->isTypeDependent()) ||
1195 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001196 ((Base && Base->isInstantiationDependent()) ||
1197 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001198 // Contains unexpanded parameter pack
1199 ((Base && Base->containsUnexpandedParameterPack()) ||
1200 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001201 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1202 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001203
1204 // Check whether all of the members are non-static member functions,
1205 // and if so, mark give this bound-member type instead of overload type.
1206 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1207 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001208}
1209
Douglas Gregor0da1d432011-02-28 20:01:57 +00001210bool UnresolvedMemberExpr::isImplicitAccess() const {
1211 if (Base == 0)
1212 return true;
1213
Douglas Gregor25b7e052011-03-02 21:06:53 +00001214 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001215}
1216
John McCall10eae182009-11-30 22:42:35 +00001217UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001219 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001220 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001221 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001222 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001223 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001224 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001225 const TemplateArgumentListInfo *TemplateArgs,
1226 UnresolvedSetIterator Begin,
1227 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001228 std::size_t size = sizeof(UnresolvedMemberExpr);
1229 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001230 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1231 else if (TemplateKWLoc.isValid())
1232 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001233
Chris Lattner5c0b4052010-10-30 05:14:06 +00001234 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001235 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001236 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001237 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001238 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001239}
1240
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001241UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001242UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001243 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001244 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001245 if (HasTemplateKWAndArgsInfo)
1246 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001247
Chris Lattner5c0b4052010-10-30 05:14:06 +00001248 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001249 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001250 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001251 return E;
1252}
1253
John McCall58cc69d2010-01-27 01:50:18 +00001254CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1255 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1256
1257 // If there was a nested name specifier, it names the naming class.
1258 // It can't be dependent: after all, we were actually able to do the
1259 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001260 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001261 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001262 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001263 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001264 Record = T->getAsCXXRecordDecl();
1265 assert(Record && "qualifier in member expression does not name record");
1266 }
John McCall58cc69d2010-01-27 01:50:18 +00001267 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001268 else {
John McCall58cc69d2010-01-27 01:50:18 +00001269 QualType BaseType = getBaseType().getNonReferenceType();
1270 if (isArrow()) {
1271 const PointerType *PT = BaseType->getAs<PointerType>();
1272 assert(PT && "base of arrow member access is not pointer");
1273 BaseType = PT->getPointeeType();
1274 }
1275
Douglas Gregor9262f472010-04-27 18:19:34 +00001276 Record = BaseType->getAsCXXRecordDecl();
1277 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001278 }
1279
Douglas Gregor9262f472010-04-27 18:19:34 +00001280 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001281}
1282
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001283SubstNonTypeTemplateParmPackExpr::
1284SubstNonTypeTemplateParmPackExpr(QualType T,
1285 NonTypeTemplateParmDecl *Param,
1286 SourceLocation NameLoc,
1287 const TemplateArgument &ArgPack)
1288 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001289 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001290 Param(Param), Arguments(ArgPack.pack_begin()),
1291 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1292
1293TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1294 return TemplateArgument(Arguments, NumArguments);
1295}
1296
Douglas Gregor29c42f22012-02-24 07:38:34 +00001297TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1298 ArrayRef<TypeSourceInfo *> Args,
1299 SourceLocation RParenLoc,
1300 bool Value)
1301 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1302 /*TypeDependent=*/false,
1303 /*ValueDependent=*/false,
1304 /*InstantiationDependent=*/false,
1305 /*ContainsUnexpandedParameterPack=*/false),
1306 Loc(Loc), RParenLoc(RParenLoc)
1307{
1308 TypeTraitExprBits.Kind = Kind;
1309 TypeTraitExprBits.Value = Value;
1310 TypeTraitExprBits.NumArgs = Args.size();
1311
1312 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1313
1314 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1315 if (Args[I]->getType()->isDependentType())
1316 setValueDependent(true);
1317 if (Args[I]->getType()->isInstantiationDependentType())
1318 setInstantiationDependent(true);
1319 if (Args[I]->getType()->containsUnexpandedParameterPack())
1320 setContainsUnexpandedParameterPack(true);
1321
1322 ToArgs[I] = Args[I];
1323 }
1324}
1325
1326TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1327 SourceLocation Loc,
1328 TypeTrait Kind,
1329 ArrayRef<TypeSourceInfo *> Args,
1330 SourceLocation RParenLoc,
1331 bool Value) {
1332 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1333 void *Mem = C.Allocate(Size);
1334 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1335}
1336
1337TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1338 unsigned NumArgs) {
1339 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1340 void *Mem = C.Allocate(Size);
1341 return new (Mem) TypeTraitExpr(EmptyShell());
1342}
1343
David Blaikie68e081d2011-12-20 02:48:34 +00001344void ArrayTypeTraitExpr::anchor() { }