blob: e4545c152f18f6b42ec23324a26c5161756f78e7 [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
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000418SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000419 OverloadedOperatorKind Kind = getOperator();
420 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
421 if (getNumArgs() == 1)
422 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000423 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000424 else
425 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000426 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000427 } else if (Kind == OO_Arrow) {
428 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000429 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000430 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000431 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000432 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000433 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000434 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000435 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000436 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000437 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000438 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000439 }
440}
441
Ted Kremenek98a24e32011-03-30 17:41:19 +0000442Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
443 if (const MemberExpr *MemExpr =
444 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000445 return MemExpr->getBase();
446
447 // FIXME: Will eventually need to cope with member pointers.
448 return 0;
449}
450
Ted Kremenek98a24e32011-03-30 17:41:19 +0000451CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
452 if (const MemberExpr *MemExpr =
453 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
454 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
455
456 // FIXME: Will eventually need to cope with member pointers.
457 return 0;
458}
459
460
David Blaikiec0f58662012-05-03 16:25:49 +0000461CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000462 Expr* ThisArg = getImplicitObjectArgument();
463 if (!ThisArg)
464 return 0;
465
466 if (ThisArg->getType()->isAnyPointerType())
467 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
468
469 return ThisArg->getType()->getAsCXXRecordDecl();
470}
471
Douglas Gregoref986e82009-11-12 15:31:47 +0000472
Douglas Gregore200adc2008-10-27 19:41:14 +0000473//===----------------------------------------------------------------------===//
474// Named casts
475//===----------------------------------------------------------------------===//
476
477/// getCastName - Get the name of the C++ cast being used, e.g.,
478/// "static_cast", "dynamic_cast", "reinterpret_cast", or
479/// "const_cast". The returned pointer must not be freed.
480const char *CXXNamedCastExpr::getCastName() const {
481 switch (getStmtClass()) {
482 case CXXStaticCastExprClass: return "static_cast";
483 case CXXDynamicCastExprClass: return "dynamic_cast";
484 case CXXReinterpretCastExprClass: return "reinterpret_cast";
485 case CXXConstCastExprClass: return "const_cast";
486 default: return "<invalid cast>";
487 }
488}
Douglas Gregordd04d332009-01-16 18:33:17 +0000489
John McCallcf142162010-08-07 06:22:56 +0000490CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000491 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000492 CastKind K, Expr *Op,
493 const CXXCastPath *BasePath,
494 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000495 SourceLocation L,
496 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000497 unsigned PathSize = (BasePath ? BasePath->size() : 0);
498 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
499 + PathSize * sizeof(CXXBaseSpecifier*));
500 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000501 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
502 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000503 if (PathSize) E->setCastPath(*BasePath);
504 return E;
505}
506
507CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
508 unsigned PathSize) {
509 void *Buffer =
510 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
511 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
512}
513
514CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000515 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000516 CastKind K, Expr *Op,
517 const CXXCastPath *BasePath,
518 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000519 SourceLocation L,
520 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000521 unsigned PathSize = (BasePath ? BasePath->size() : 0);
522 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
523 + PathSize * sizeof(CXXBaseSpecifier*));
524 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000525 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
526 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000527 if (PathSize) E->setCastPath(*BasePath);
528 return E;
529}
530
531CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
532 unsigned PathSize) {
533 void *Buffer =
534 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
535 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
536}
537
Anders Carlsson267c0c92011-04-11 01:43:55 +0000538/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
539/// to always be null. For example:
540///
541/// struct A { };
542/// struct B final : A { };
543/// struct C { };
544///
545/// C *f(B* b) { return dynamic_cast<C*>(b); }
546bool CXXDynamicCastExpr::isAlwaysNull() const
547{
548 QualType SrcType = getSubExpr()->getType();
549 QualType DestType = getType();
550
551 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
552 SrcType = SrcPTy->getPointeeType();
553 DestType = DestType->castAs<PointerType>()->getPointeeType();
554 }
555
556 const CXXRecordDecl *SrcRD =
557 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
558
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000559 if (!SrcRD->hasAttr<FinalAttr>())
560 return false;
561
Anders Carlsson267c0c92011-04-11 01:43:55 +0000562 const CXXRecordDecl *DestRD =
563 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
564
565 return !DestRD->isDerivedFrom(SrcRD);
566}
567
John McCallcf142162010-08-07 06:22:56 +0000568CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000569CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
570 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000571 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000572 TypeSourceInfo *WrittenTy, SourceLocation L,
573 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000574 unsigned PathSize = (BasePath ? BasePath->size() : 0);
575 void *Buffer =
576 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
577 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000578 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
579 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000580 if (PathSize) E->setCastPath(*BasePath);
581 return E;
582}
583
584CXXReinterpretCastExpr *
585CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
586 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
587 + PathSize * sizeof(CXXBaseSpecifier*));
588 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
589}
590
John McCall7decc9e2010-11-18 06:31:45 +0000591CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
592 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000593 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000594 SourceLocation L,
595 SourceLocation RParenLoc) {
596 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000597}
598
599CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
600 return new (C) CXXConstCastExpr(EmptyShell());
601}
602
603CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000604CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000605 TypeSourceInfo *Written, SourceLocation L,
606 CastKind K, Expr *Op, const CXXCastPath *BasePath,
607 SourceLocation R) {
608 unsigned PathSize = (BasePath ? BasePath->size() : 0);
609 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
610 + PathSize * sizeof(CXXBaseSpecifier*));
611 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000612 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000613 if (PathSize) E->setCastPath(*BasePath);
614 return E;
615}
616
617CXXFunctionalCastExpr *
618CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
619 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
620 + PathSize * sizeof(CXXBaseSpecifier*));
621 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
622}
623
Richard Smithc67fdd42012-03-07 08:35:16 +0000624UserDefinedLiteral::LiteralOperatorKind
625UserDefinedLiteral::getLiteralOperatorKind() const {
626 if (getNumArgs() == 0)
627 return LOK_Template;
628 if (getNumArgs() == 2)
629 return LOK_String;
630
631 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
632 QualType ParamTy =
633 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
634 if (ParamTy->isPointerType())
635 return LOK_Raw;
636 if (ParamTy->isAnyCharacterType())
637 return LOK_Character;
638 if (ParamTy->isIntegerType())
639 return LOK_Integer;
640 if (ParamTy->isFloatingType())
641 return LOK_Floating;
642
643 llvm_unreachable("unknown kind of literal operator");
644}
645
646Expr *UserDefinedLiteral::getCookedLiteral() {
647#ifndef NDEBUG
648 LiteralOperatorKind LOK = getLiteralOperatorKind();
649 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
650#endif
651 return getArg(0);
652}
653
654const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
655 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
656}
John McCallcf142162010-08-07 06:22:56 +0000657
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000658CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000659CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
660 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000661 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000662 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
663 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000664}
665
Mike Stump11289f42009-09-09 15:08:12 +0000666CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000667 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000668 return new (C) CXXTemporary(Destructor);
669}
670
Mike Stump11289f42009-09-09 15:08:12 +0000671CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000672 CXXTemporary *Temp,
673 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000674 assert((SubExpr->getType()->isRecordType() ||
675 SubExpr->getType()->isArrayType()) &&
676 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000677
Douglas Gregora6e053e2010-12-15 01:34:56 +0000678 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000679}
680
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000681CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000682 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000683 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000684 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000685 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000686 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000687 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000688 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000689 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
690 Type->getType().getNonReferenceType(),
691 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000692 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000693 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000694 CXXConstructExpr::CK_Complete, parenRange),
695 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000696}
697
698SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000699 return SourceRange(Type->getTypeLoc().getBeginLoc(),
700 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000701}
Anders Carlsson6f287832009-04-21 02:22:11 +0000702
Mike Stump11289f42009-09-09 15:08:12 +0000703CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000704 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000705 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000706 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000707 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000708 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000709 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000710 ConstructionKind ConstructKind,
711 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000712 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000713 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000714 HadMultipleCandidates, ListInitialization,
715 ZeroInitialization, ConstructKind,
716 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000717}
718
Mike Stump11289f42009-09-09 15:08:12 +0000719CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000720 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000721 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000722 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000723 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000724 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000725 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000726 ConstructionKind ConstructKind,
727 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000728 : Expr(SC, T, VK_RValue, OK_Ordinary,
729 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000730 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000731 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000732 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000733 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000734 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000735 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000736 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000737{
738 if (NumArgs) {
739 Args = new (C) Stmt*[NumArgs];
740
741 for (unsigned i = 0; i != NumArgs; ++i) {
742 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000743
744 if (args[i]->isValueDependent())
745 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000746 if (args[i]->isInstantiationDependent())
747 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000748 if (args[i]->containsUnexpandedParameterPack())
749 ExprBits.ContainsUnexpandedParameterPack = true;
750
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000751 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000752 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000753 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000754}
755
Douglas Gregore31e6062012-02-07 10:09:13 +0000756LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
757 LambdaCaptureKind Kind, VarDecl *Var,
758 SourceLocation EllipsisLoc)
759 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
760{
761 unsigned Bits = 0;
762 if (Implicit)
763 Bits |= Capture_Implicit;
764
765 switch (Kind) {
766 case LCK_This:
767 assert(Var == 0 && "'this' capture cannot have a variable!");
768 break;
769
770 case LCK_ByCopy:
771 Bits |= Capture_ByCopy;
772 // Fall through
773 case LCK_ByRef:
774 assert(Var && "capture must have a variable!");
775 break;
776 }
777 VarAndBits.setInt(Bits);
778}
779
780LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
781 if (capturesThis())
782 return LCK_This;
783
784 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
785}
786
787LambdaExpr::LambdaExpr(QualType T,
788 SourceRange IntroducerRange,
789 LambdaCaptureDefault CaptureDefault,
790 ArrayRef<Capture> Captures,
791 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000792 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000793 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000794 ArrayRef<VarDecl *> ArrayIndexVars,
795 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000796 SourceLocation ClosingBrace)
Douglas Gregore31e6062012-02-07 10:09:13 +0000797 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
798 T->isDependentType(), T->isDependentType(), T->isDependentType(),
799 /*ContainsUnexpandedParameterPack=*/false),
800 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000801 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000802 CaptureDefault(CaptureDefault),
803 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000804 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000805 ClosingBrace(ClosingBrace)
806{
807 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000808 CXXRecordDecl *Class = getLambdaClass();
809 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000810
811 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000812
813 // Copy captures.
814 ASTContext &Context = Class->getASTContext();
815 Data.NumCaptures = NumCaptures;
816 Data.NumExplicitCaptures = 0;
817 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
818 Capture *ToCapture = Data.Captures;
819 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
820 if (Captures[I].isExplicit())
821 ++Data.NumExplicitCaptures;
822
823 *ToCapture++ = Captures[I];
824 }
825
826 // Copy initialization expressions for the non-static data members.
827 Stmt **Stored = getStoredStmts();
828 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
829 *Stored++ = CaptureInits[I];
830
831 // Copy the body of the lambda.
832 *Stored++ = getCallOperator()->getBody();
833
834 // Copy the array index variables, if any.
835 HasArrayIndexVars = !ArrayIndexVars.empty();
836 if (HasArrayIndexVars) {
837 assert(ArrayIndexStarts.size() == NumCaptures);
838 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
839 sizeof(VarDecl *) * ArrayIndexVars.size());
840 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
841 sizeof(unsigned) * Captures.size());
842 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000843 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000844}
845
846LambdaExpr *LambdaExpr::Create(ASTContext &Context,
847 CXXRecordDecl *Class,
848 SourceRange IntroducerRange,
849 LambdaCaptureDefault CaptureDefault,
850 ArrayRef<Capture> Captures,
851 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000852 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000853 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000854 ArrayRef<VarDecl *> ArrayIndexVars,
855 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000856 SourceLocation ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000857 // Determine the type of the expression (i.e., the type of the
858 // function object we're creating).
859 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000860
Douglas Gregore5561632012-02-13 17:20:40 +0000861 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
862 if (!ArrayIndexVars.empty())
863 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
864 + sizeof(unsigned) * (Captures.size() + 1);
865 void *Mem = Context.Allocate(Size);
866 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000867 Captures, ExplicitParams, ExplicitResultType,
868 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000869 ClosingBrace);
Douglas Gregore31e6062012-02-07 10:09:13 +0000870}
871
Douglas Gregor99ae8062012-02-14 17:54:36 +0000872LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
873 unsigned NumArrayIndexVars) {
874 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
875 if (NumArrayIndexVars)
876 Size += sizeof(VarDecl) * NumArrayIndexVars
877 + sizeof(unsigned) * (NumCaptures + 1);
878 void *Mem = C.Allocate(Size);
879 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
880}
881
Douglas Gregorc8a73492012-02-13 15:44:47 +0000882LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000883 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000884}
885
886LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000887 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000888}
889
890LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
891 return capture_begin();
892}
893
894LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
895 struct CXXRecordDecl::LambdaDefinitionData &Data
896 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000897 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000898}
899
900LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
901 return explicit_capture_end();
902}
903
904LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
905 return capture_end();
906}
907
Douglas Gregor54fcea62012-02-13 16:35:30 +0000908ArrayRef<VarDecl *>
909LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000910 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000911
912 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000913 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
914 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000915 VarDecl **IndexVars = getArrayIndexVars();
916 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000917 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
918 IndexVars + IndexStarts[Index + 1]);
919}
920
Douglas Gregore31e6062012-02-07 10:09:13 +0000921CXXRecordDecl *LambdaExpr::getLambdaClass() const {
922 return getType()->getAsCXXRecordDecl();
923}
924
925CXXMethodDecl *LambdaExpr::getCallOperator() const {
926 CXXRecordDecl *Record = getLambdaClass();
927 DeclarationName Name
928 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
929 DeclContext::lookup_result Calls = Record->lookup(Name);
930 assert(Calls.first != Calls.second && "Missing lambda call operator!");
931 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
932 assert(Calls.first == Calls.second && "More than lambda one call operator?");
933 return Result;
934}
935
Douglas Gregor99ae8062012-02-14 17:54:36 +0000936CompoundStmt *LambdaExpr::getBody() const {
937 if (!getStoredStmts()[NumCaptures])
938 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
939
940 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
941}
942
Douglas Gregore31e6062012-02-07 10:09:13 +0000943bool LambdaExpr::isMutable() const {
944 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
945}
946
John McCall28fc7092011-11-10 05:35:25 +0000947ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
948 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000949 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000950 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000951 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000952 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000953 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000954 SubExpr(subexpr) {
955 ExprWithCleanupsBits.NumObjects = objects.size();
956 for (unsigned i = 0, e = objects.size(); i != e; ++i)
957 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000958}
959
John McCall28fc7092011-11-10 05:35:25 +0000960ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
961 ArrayRef<CleanupObject> objects) {
962 size_t size = sizeof(ExprWithCleanups)
963 + objects.size() * sizeof(CleanupObject);
964 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
965 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000966}
967
John McCall28fc7092011-11-10 05:35:25 +0000968ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
969 : Expr(ExprWithCleanupsClass, empty) {
970 ExprWithCleanupsBits.NumObjects = numObjects;
971}
Chris Lattnercba86142010-05-10 00:25:06 +0000972
John McCall28fc7092011-11-10 05:35:25 +0000973ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
974 unsigned numObjects) {
975 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
976 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
977 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000978}
979
Douglas Gregor2b88c112010-09-08 00:15:04 +0000980CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000981 SourceLocation LParenLoc,
982 Expr **Args,
983 unsigned NumArgs,
984 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000985 : Expr(CXXUnresolvedConstructExprClass,
986 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000987 (Type->getType()->isLValueReferenceType() ? VK_LValue
988 :Type->getType()->isRValueReferenceType()? VK_XValue
989 :VK_RValue),
990 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000991 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000992 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +0000993 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +0000994 LParenLoc(LParenLoc),
995 RParenLoc(RParenLoc),
996 NumArgs(NumArgs) {
997 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000998 for (unsigned I = 0; I != NumArgs; ++I) {
999 if (Args[I]->containsUnexpandedParameterPack())
1000 ExprBits.ContainsUnexpandedParameterPack = true;
1001
1002 StoredArgs[I] = Args[I];
1003 }
Douglas Gregorce934142009-05-20 18:46:25 +00001004}
1005
1006CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001007CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001008 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001009 SourceLocation LParenLoc,
1010 Expr **Args,
1011 unsigned NumArgs,
1012 SourceLocation RParenLoc) {
1013 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1014 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001015 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +00001016 Args, NumArgs, RParenLoc);
1017}
1018
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001019CXXUnresolvedConstructExpr *
1020CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1021 Stmt::EmptyShell Empty;
1022 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1023 sizeof(Expr *) * NumArgs);
1024 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1025}
1026
Douglas Gregor2b88c112010-09-08 00:15:04 +00001027SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1028 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1029}
1030
John McCall8cd78132009-11-19 22:55:06 +00001031CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001032 Expr *Base, QualType BaseType,
1033 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001034 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001035 NestedNameSpecifierLoc QualifierLoc,
1036 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001037 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001038 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001039 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001040 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001041 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001042 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001043 (QualifierLoc &&
1044 QualifierLoc.getNestedNameSpecifier()
1045 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001046 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001047 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001048 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001049 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001050 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001051 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001052 if (TemplateArgs) {
1053 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001054 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001055 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001056 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1057 Dependent,
1058 InstantiationDependent,
1059 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001060 if (ContainsUnexpandedParameterPack)
1061 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001062 } else if (TemplateKWLoc.isValid()) {
1063 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001064 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001065}
1066
Douglas Gregora6e053e2010-12-15 01:34:56 +00001067CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1068 Expr *Base, QualType BaseType,
1069 bool IsArrow,
1070 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001071 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001072 NamedDecl *FirstQualifierFoundInScope,
1073 DeclarationNameInfo MemberNameInfo)
1074 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001075 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001076 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001077 (QualifierLoc &&
1078 QualifierLoc.getNestedNameSpecifier()->
1079 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001080 MemberNameInfo.containsUnexpandedParameterPack())),
1081 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001082 HasTemplateKWAndArgsInfo(false),
1083 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001084 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1085 MemberNameInfo(MemberNameInfo) { }
1086
John McCall8cd78132009-11-19 22:55:06 +00001087CXXDependentScopeMemberExpr *
1088CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001089 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001090 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001091 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001092 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001093 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001094 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001095 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001096 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001097 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1098 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001099 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001100 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001101 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001102
Abramo Bagnara7945c982012-01-27 09:46:47 +00001103 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1104 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1105 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001106
Chris Lattner5c0b4052010-10-30 05:14:06 +00001107 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001108 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1109 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001110 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001111 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001112 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001113 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001114}
1115
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001116CXXDependentScopeMemberExpr *
1117CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001118 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001119 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001120 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001121 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001122 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001123 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001124 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001125
1126 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001127 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001128 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001129 CXXDependentScopeMemberExpr *E
1130 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001131 0, SourceLocation(),
1132 NestedNameSpecifierLoc(),
1133 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001134 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001135 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001136 return E;
1137}
1138
Douglas Gregor0da1d432011-02-28 20:01:57 +00001139bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1140 if (Base == 0)
1141 return true;
1142
Douglas Gregor25b7e052011-03-02 21:06:53 +00001143 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001144}
1145
John McCall0009fcc2011-04-26 20:42:42 +00001146static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1147 UnresolvedSetIterator end) {
1148 do {
1149 NamedDecl *decl = *begin;
1150 if (isa<UnresolvedUsingValueDecl>(decl))
1151 return false;
1152 if (isa<UsingShadowDecl>(decl))
1153 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1154
1155 // Unresolved member expressions should only contain methods and
1156 // method templates.
1157 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1158
1159 if (isa<FunctionTemplateDecl>(decl))
1160 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1161 if (cast<CXXMethodDecl>(decl)->isStatic())
1162 return false;
1163 } while (++begin != end);
1164
1165 return true;
1166}
1167
Douglas Gregora6e053e2010-12-15 01:34:56 +00001168UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001169 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001170 Expr *Base, QualType BaseType,
1171 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001172 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001173 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001174 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001175 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001176 const TemplateArgumentListInfo *TemplateArgs,
1177 UnresolvedSetIterator Begin,
1178 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001179 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1180 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001181 // Dependent
1182 ((Base && Base->isTypeDependent()) ||
1183 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001184 ((Base && Base->isInstantiationDependent()) ||
1185 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001186 // Contains unexpanded parameter pack
1187 ((Base && Base->containsUnexpandedParameterPack()) ||
1188 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001189 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1190 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001191
1192 // Check whether all of the members are non-static member functions,
1193 // and if so, mark give this bound-member type instead of overload type.
1194 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1195 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001196}
1197
Douglas Gregor0da1d432011-02-28 20:01:57 +00001198bool UnresolvedMemberExpr::isImplicitAccess() const {
1199 if (Base == 0)
1200 return true;
1201
Douglas Gregor25b7e052011-03-02 21:06:53 +00001202 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001203}
1204
John McCall10eae182009-11-30 22:42:35 +00001205UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001206UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001207 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001208 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001209 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001210 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001211 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001212 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001213 const TemplateArgumentListInfo *TemplateArgs,
1214 UnresolvedSetIterator Begin,
1215 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001216 std::size_t size = sizeof(UnresolvedMemberExpr);
1217 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001218 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1219 else if (TemplateKWLoc.isValid())
1220 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001221
Chris Lattner5c0b4052010-10-30 05:14:06 +00001222 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001223 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001224 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001225 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001226 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001227}
1228
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001229UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001230UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001231 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001232 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001233 if (HasTemplateKWAndArgsInfo)
1234 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001235
Chris Lattner5c0b4052010-10-30 05:14:06 +00001236 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001237 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001238 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001239 return E;
1240}
1241
John McCall58cc69d2010-01-27 01:50:18 +00001242CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1243 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1244
1245 // If there was a nested name specifier, it names the naming class.
1246 // It can't be dependent: after all, we were actually able to do the
1247 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001248 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001249 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001250 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001251 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001252 Record = T->getAsCXXRecordDecl();
1253 assert(Record && "qualifier in member expression does not name record");
1254 }
John McCall58cc69d2010-01-27 01:50:18 +00001255 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001256 else {
John McCall58cc69d2010-01-27 01:50:18 +00001257 QualType BaseType = getBaseType().getNonReferenceType();
1258 if (isArrow()) {
1259 const PointerType *PT = BaseType->getAs<PointerType>();
1260 assert(PT && "base of arrow member access is not pointer");
1261 BaseType = PT->getPointeeType();
1262 }
1263
Douglas Gregor9262f472010-04-27 18:19:34 +00001264 Record = BaseType->getAsCXXRecordDecl();
1265 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001266 }
1267
Douglas Gregor9262f472010-04-27 18:19:34 +00001268 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001269}
1270
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001271SubstNonTypeTemplateParmPackExpr::
1272SubstNonTypeTemplateParmPackExpr(QualType T,
1273 NonTypeTemplateParmDecl *Param,
1274 SourceLocation NameLoc,
1275 const TemplateArgument &ArgPack)
1276 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001277 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001278 Param(Param), Arguments(ArgPack.pack_begin()),
1279 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1280
1281TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1282 return TemplateArgument(Arguments, NumArguments);
1283}
1284
Douglas Gregor29c42f22012-02-24 07:38:34 +00001285TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1286 ArrayRef<TypeSourceInfo *> Args,
1287 SourceLocation RParenLoc,
1288 bool Value)
1289 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1290 /*TypeDependent=*/false,
1291 /*ValueDependent=*/false,
1292 /*InstantiationDependent=*/false,
1293 /*ContainsUnexpandedParameterPack=*/false),
1294 Loc(Loc), RParenLoc(RParenLoc)
1295{
1296 TypeTraitExprBits.Kind = Kind;
1297 TypeTraitExprBits.Value = Value;
1298 TypeTraitExprBits.NumArgs = Args.size();
1299
1300 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1301
1302 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1303 if (Args[I]->getType()->isDependentType())
1304 setValueDependent(true);
1305 if (Args[I]->getType()->isInstantiationDependentType())
1306 setInstantiationDependent(true);
1307 if (Args[I]->getType()->containsUnexpandedParameterPack())
1308 setContainsUnexpandedParameterPack(true);
1309
1310 ToArgs[I] = Args[I];
1311 }
1312}
1313
1314TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1315 SourceLocation Loc,
1316 TypeTrait Kind,
1317 ArrayRef<TypeSourceInfo *> Args,
1318 SourceLocation RParenLoc,
1319 bool Value) {
1320 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1321 void *Mem = C.Allocate(Size);
1322 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1323}
1324
1325TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1326 unsigned NumArgs) {
1327 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1328 void *Mem = C.Allocate(Size);
1329 return new (Mem) TypeTraitExpr(EmptyShell());
1330}
1331
David Blaikie68e081d2011-12-20 02:48:34 +00001332void ArrayTypeTraitExpr::anchor() { }