blob: 8cf519c93dc2b9c4340b03bcb76775c695504666 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000021
Ted Kremeneka758d092007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000026QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
30}
31
Francois Pichet01b7c302010-09-08 12:20:18 +000032QualType CXXUuidofExpr::getTypeOperand() const {
33 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
34 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
35 .getUnqualifiedType();
36}
37
Douglas Gregored8abf12010-07-08 06:14:04 +000038// CXXScalarValueInitExpr
Douglas Gregorab6677e2010-09-08 00:15:04 +000039SourceRange CXXScalarValueInitExpr::getSourceRange() const {
40 SourceLocation Start = RParenLoc;
41 if (TypeInfo)
42 Start = TypeInfo->getTypeLoc().getBeginLoc();
43 return SourceRange(Start, RParenLoc);
44}
45
Sebastian Redl4c5d3202008-11-21 19:14:01 +000046// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000047CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl1548d142012-02-16 11:35:52 +000048 FunctionDecl *operatorDelete,
Sebastian Redl2aed8b82012-02-16 12:22:20 +000049 bool usualArrayDeleteWantsSize,
50 Expr **placementArgs, unsigned numPlaceArgs,
51 SourceRange typeIdParens, Expr *arraySize,
52 InitializationStyle initializationStyle,
53 Expr *initializer, QualType ty,
54 TypeSourceInfo *allocatedTypeInfo,
55 SourceLocation startLoc, SourceRange directInitRange)
John McCallf89e55a2010-11-18 06:31:45 +000056 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000057 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +000058 ty->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000059 ty->containsUnexpandedParameterPack()),
Sebastian Redl2aed8b82012-02-16 12:22:20 +000060 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
61 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerd162cf12012-02-26 20:37:14 +000062 StartLoc(startLoc), DirectInitRange(directInitRange),
63 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +000064 assert((initializer != 0 || initializationStyle == NoInit) &&
65 "Only NoInit can have no initializer.");
66 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
67 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000068 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000069 if (Array) {
Douglas Gregor561f8122011-07-01 01:22:09 +000070 if (arraySize->isInstantiationDependent())
71 ExprBits.InstantiationDependent = true;
72
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000073 if (arraySize->containsUnexpandedParameterPack())
74 ExprBits.ContainsUnexpandedParameterPack = true;
75
Sebastian Redlcee63fb2008-12-02 14:43:59 +000076 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000077 }
78
Sebastian Redl2aed8b82012-02-16 12:22:20 +000079 if (initializer) {
80 if (initializer->isInstantiationDependent())
81 ExprBits.InstantiationDependent = true;
82
83 if (initializer->containsUnexpandedParameterPack())
84 ExprBits.ContainsUnexpandedParameterPack = true;
85
86 SubExprs[i++] = initializer;
87 }
88
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000089 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor561f8122011-07-01 01:22:09 +000090 if (placementArgs[j]->isInstantiationDependent())
91 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000092 if (placementArgs[j]->containsUnexpandedParameterPack())
93 ExprBits.ContainsUnexpandedParameterPack = true;
94
Sebastian Redl4c5d3202008-11-21 19:14:01 +000095 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000096 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +000097}
98
Chris Lattner59218632010-05-10 01:22:27 +000099void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000100 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattner59218632010-05-10 01:22:27 +0000101 assert(SubExprs == 0 && "SubExprs already allocated");
102 Array = isArray;
103 NumPlacementArgs = numPlaceArgs;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000104
105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattner59218632010-05-10 01:22:27 +0000106 SubExprs = new (C) Stmt*[TotalSize];
107}
108
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000109bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000110 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000111 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000112}
Chris Lattner59218632010-05-10 01:22:27 +0000113
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000114SourceLocation CXXNewExpr::getEndLoc() const {
115 switch (getInitializationStyle()) {
116 case NoInit:
117 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
118 case CallInit:
119 return DirectInitRange.getEnd();
120 case ListInit:
121 return getInitializer()->getSourceRange().getEnd();
122 }
Matt Beaumont-Gayebc6af12012-02-16 22:15:50 +0000123 llvm_unreachable("bogus initialization style");
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000124}
125
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000126// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000127QualType CXXDeleteExpr::getDestroyedType() const {
128 const Expr *Arg = getArgument();
129 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
130 if (ICE->getCastKind() != CK_UserDefinedConversion &&
131 ICE->getType()->isVoidPointerType())
132 Arg = ICE->getSubExpr();
133 else
134 break;
135 }
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000136 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000137 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000138
139 if (ArgType->isDependentType() && !ArgType->isPointerType())
140 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000141
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000142 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000143}
144
Douglas Gregora71d8192009-09-04 17:36:40 +0000145// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000146PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
147 : Type(Info)
148{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000150}
151
John McCalle23cf432010-12-14 08:05:40 +0000152CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000153 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
154 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
155 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
156 PseudoDestructorTypeStorage DestroyedType)
John McCalle23cf432010-12-14 08:05:40 +0000157 : Expr(CXXPseudoDestructorExprClass,
158 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
159 FunctionProtoType::ExtProtoInfo())),
160 VK_RValue, OK_Ordinary,
161 /*isTypeDependent=*/(Base->isTypeDependent() ||
162 (DestroyedType.getTypeSourceInfo() &&
163 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000164 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000165 (Base->isInstantiationDependent() ||
166 (QualifierLoc &&
167 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
168 (ScopeType &&
169 ScopeType->getType()->isInstantiationDependentType()) ||
170 (DestroyedType.getTypeSourceInfo() &&
171 DestroyedType.getTypeSourceInfo()->getType()
172 ->isInstantiationDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000173 // ContainsUnexpandedParameterPack
174 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()
177 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000178 (ScopeType &&
179 ScopeType->getType()->containsUnexpandedParameterPack()) ||
180 (DestroyedType.getTypeSourceInfo() &&
181 DestroyedType.getTypeSourceInfo()->getType()
182 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
186 DestroyedType(DestroyedType) { }
187
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000188QualType CXXPseudoDestructorExpr::getDestroyedType() const {
189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
190 return TInfo->getType();
191
192 return QualType();
193}
194
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000200}
201
John McCallba135432009-11-21 08:51:07 +0000202// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000203UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000204UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000205 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000206 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000207 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000208 const DeclarationNameInfo &NameInfo,
209 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000210 const TemplateArgumentListInfo *Args,
211 UnresolvedSetIterator Begin,
212 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000213{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000214 assert(Args || TemplateKWLoc.isValid());
215 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000216 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000217 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000218 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
219 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000220 ADL, /*Overload*/ true, Args,
Richard Smithad762fc2011-04-14 22:09:26 +0000221 Begin, End, /*StdIsAssociated=*/false);
John McCallf7a1a742009-11-24 19:00:30 +0000222}
223
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000224UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000225UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
226 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000227 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000228 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000229 if (HasTemplateKWAndArgsInfo)
230 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000231
Chris Lattner32488542010-10-30 05:14:06 +0000232 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000233 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000234 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000235 return E;
236}
237
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000238OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000239 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000240 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000241 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000242 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000243 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000244 UnresolvedSetIterator End,
245 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000246 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247 bool KnownContainsUnexpandedParameterPack)
248 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
249 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000250 (KnownInstantiationDependent ||
251 NameInfo.isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000254 (KnownContainsUnexpandedParameterPack ||
255 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000256 (QualifierLoc &&
257 QualifierLoc.getNestedNameSpecifier()
258 ->containsUnexpandedParameterPack()))),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000259 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
260 Results(0), NumResults(End - Begin),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000261 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000262{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000263 NumResults = End - Begin;
264 if (NumResults) {
265 // Determine whether this expression is type-dependent.
266 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
267 if ((*I)->getDeclContext()->isDependentContext() ||
268 isa<UnresolvedUsingValueDecl>(*I)) {
269 ExprBits.TypeDependent = true;
270 ExprBits.ValueDependent = true;
271 }
272 }
273
274 Results = static_cast<DeclAccessPair *>(
275 C.Allocate(sizeof(DeclAccessPair) * NumResults,
276 llvm::alignOf<DeclAccessPair>()));
277 memcpy(Results, &*Begin.getIterator(),
278 NumResults * sizeof(DeclAccessPair));
279 }
280
281 // If we have explicit template arguments, check for dependent
282 // template arguments and whether they contain any unexpanded pack
283 // expansions.
284 if (TemplateArgs) {
285 bool Dependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000286 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000287 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000288 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
289 Dependent,
290 InstantiationDependent,
291 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000292
293 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 ExprBits.TypeDependent = true;
295 ExprBits.ValueDependent = true;
296 }
297 if (InstantiationDependent)
298 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000299 if (ContainsUnexpandedParameterPack)
300 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000301 } else if (TemplateKWLoc.isValid()) {
302 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000303 }
304
305 if (isTypeDependent())
306 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000307}
308
309void OverloadExpr::initializeResults(ASTContext &C,
310 UnresolvedSetIterator Begin,
311 UnresolvedSetIterator End) {
312 assert(Results == 0 && "Results already initialized!");
313 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000314 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000315 Results = static_cast<DeclAccessPair *>(
316 C.Allocate(sizeof(DeclAccessPair) * NumResults,
317
318 llvm::alignOf<DeclAccessPair>()));
319 memcpy(Results, &*Begin.getIterator(),
320 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000321 }
322}
323
John McCalle9ee23e2010-04-22 18:44:12 +0000324CXXRecordDecl *OverloadExpr::getNamingClass() const {
325 if (isa<UnresolvedLookupExpr>(this))
326 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
327 else
328 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
329}
330
John McCall865d4472009-11-19 22:55:06 +0000331// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000332DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000333 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000334 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000335 const DeclarationNameInfo &NameInfo,
336 const TemplateArgumentListInfo *Args)
337 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
338 true, true,
Douglas Gregor561f8122011-07-01 01:22:09 +0000339 (NameInfo.isInstantiationDependent() ||
340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000342 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000343 (QualifierLoc &&
344 QualifierLoc.getNestedNameSpecifier()
345 ->containsUnexpandedParameterPack()))),
346 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000347 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000348{
349 if (Args) {
350 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000351 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000352 bool ContainsUnexpandedParameterPack
353 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
355 Dependent,
356 InstantiationDependent,
357 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000358 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000361 }
362}
363
John McCallf7a1a742009-11-24 19:00:30 +0000364DependentScopeDeclRefExpr *
365DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000369 const TemplateArgumentListInfo *Args) {
370 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000371 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000372 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
373 else if (TemplateKWLoc.isValid())
374 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000375 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000376 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
377 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000378}
379
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000380DependentScopeDeclRefExpr *
381DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000382 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000383 unsigned NumTemplateArgs) {
384 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000385 if (HasTemplateKWAndArgsInfo)
386 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000387 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000388 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000389 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000390 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000391 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000392 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000393 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000394}
395
Chandler Carruth428edaf2010-10-25 08:47:36 +0000396SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000397 if (isa<CXXTemporaryObjectExpr>(this))
398 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
399
Douglas Gregor40749ee2010-11-03 00:35:38 +0000400 if (ParenRange.isValid())
401 return SourceRange(Loc, ParenRange.getEnd());
402
403 SourceLocation End = Loc;
404 for (unsigned I = getNumArgs(); I > 0; --I) {
405 const Expr *Arg = getArg(I-1);
406 if (!Arg->isDefaultArgument()) {
407 SourceLocation NewEnd = Arg->getLocEnd();
408 if (NewEnd.isValid()) {
409 End = NewEnd;
410 break;
411 }
412 }
413 }
414
415 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000416}
417
Douglas Gregorb4609802008-11-14 16:09:21 +0000418SourceRange CXXOperatorCallExpr::getSourceRange() const {
419 OverloadedOperatorKind Kind = getOperator();
420 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
421 if (getNumArgs() == 1)
422 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000423 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000424 getArg(0)->getSourceRange().getEnd());
425 else
426 // Postfix operator
Chandler Carruthd7650612011-04-02 09:47:38 +0000427 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000428 getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000429 } else if (Kind == OO_Arrow) {
430 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000431 } else if (Kind == OO_Call) {
432 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
433 } else if (Kind == OO_Subscript) {
434 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
435 } else if (getNumArgs() == 1) {
436 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
437 } else if (getNumArgs() == 2) {
438 return SourceRange(getArg(0)->getSourceRange().getBegin(),
439 getArg(1)->getSourceRange().getEnd());
440 } else {
441 return SourceRange();
442 }
443}
444
Ted Kremenekb2771592011-03-30 17:41:19 +0000445Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
446 if (const MemberExpr *MemExpr =
447 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-12-22 05:46:06 +0000448 return MemExpr->getBase();
449
450 // FIXME: Will eventually need to cope with member pointers.
451 return 0;
452}
453
Ted Kremenekb2771592011-03-30 17:41:19 +0000454CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
455 if (const MemberExpr *MemExpr =
456 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
457 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
458
459 // FIXME: Will eventually need to cope with member pointers.
460 return 0;
461}
462
463
Chandler Carruth007a9b12010-10-27 06:55:41 +0000464CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
465 Expr* ThisArg = getImplicitObjectArgument();
466 if (!ThisArg)
467 return 0;
468
469 if (ThisArg->getType()->isAnyPointerType())
470 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
471
472 return ThisArg->getType()->getAsCXXRecordDecl();
473}
474
Douglas Gregor00b98c22009-11-12 15:31:47 +0000475
Douglas Gregor49badde2008-10-27 19:41:14 +0000476//===----------------------------------------------------------------------===//
477// Named casts
478//===----------------------------------------------------------------------===//
479
480/// getCastName - Get the name of the C++ cast being used, e.g.,
481/// "static_cast", "dynamic_cast", "reinterpret_cast", or
482/// "const_cast". The returned pointer must not be freed.
483const char *CXXNamedCastExpr::getCastName() const {
484 switch (getStmtClass()) {
485 case CXXStaticCastExprClass: return "static_cast";
486 case CXXDynamicCastExprClass: return "dynamic_cast";
487 case CXXReinterpretCastExprClass: return "reinterpret_cast";
488 case CXXConstCastExprClass: return "const_cast";
489 default: return "<invalid cast>";
490 }
491}
Douglas Gregor506ae412009-01-16 18:33:17 +0000492
John McCallf871d0c2010-08-07 06:22:56 +0000493CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000494 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000495 CastKind K, Expr *Op,
496 const CXXCastPath *BasePath,
497 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000498 SourceLocation L,
499 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000500 unsigned PathSize = (BasePath ? BasePath->size() : 0);
501 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
502 + PathSize * sizeof(CXXBaseSpecifier*));
503 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000504 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
505 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000506 if (PathSize) E->setCastPath(*BasePath);
507 return E;
508}
509
510CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
511 unsigned PathSize) {
512 void *Buffer =
513 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
514 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
515}
516
517CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000518 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000519 CastKind K, Expr *Op,
520 const CXXCastPath *BasePath,
521 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000522 SourceLocation L,
523 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000524 unsigned PathSize = (BasePath ? BasePath->size() : 0);
525 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
526 + PathSize * sizeof(CXXBaseSpecifier*));
527 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000528 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
529 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000530 if (PathSize) E->setCastPath(*BasePath);
531 return E;
532}
533
534CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
535 unsigned PathSize) {
536 void *Buffer =
537 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
538 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
539}
540
Anders Carlsson0fee3302011-04-11 01:43:55 +0000541/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
542/// to always be null. For example:
543///
544/// struct A { };
545/// struct B final : A { };
546/// struct C { };
547///
548/// C *f(B* b) { return dynamic_cast<C*>(b); }
549bool CXXDynamicCastExpr::isAlwaysNull() const
550{
551 QualType SrcType = getSubExpr()->getType();
552 QualType DestType = getType();
553
554 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
555 SrcType = SrcPTy->getPointeeType();
556 DestType = DestType->castAs<PointerType>()->getPointeeType();
557 }
558
559 const CXXRecordDecl *SrcRD =
560 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
561
562 if (!SrcRD->hasAttr<FinalAttr>())
563 return false;
564
565 const CXXRecordDecl *DestRD =
566 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
567
568 return !DestRD->isDerivedFrom(SrcRD);
569}
570
John McCallf871d0c2010-08-07 06:22:56 +0000571CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000572CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
573 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000574 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000575 TypeSourceInfo *WrittenTy, SourceLocation L,
576 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000577 unsigned PathSize = (BasePath ? BasePath->size() : 0);
578 void *Buffer =
579 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
580 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000581 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
582 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000583 if (PathSize) E->setCastPath(*BasePath);
584 return E;
585}
586
587CXXReinterpretCastExpr *
588CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
589 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
590 + PathSize * sizeof(CXXBaseSpecifier*));
591 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
592}
593
John McCallf89e55a2010-11-18 06:31:45 +0000594CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
595 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000596 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000597 SourceLocation L,
598 SourceLocation RParenLoc) {
599 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000600}
601
602CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
603 return new (C) CXXConstCastExpr(EmptyShell());
604}
605
606CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000607CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000608 TypeSourceInfo *Written, SourceLocation L,
609 CastKind K, Expr *Op, const CXXCastPath *BasePath,
610 SourceLocation R) {
611 unsigned PathSize = (BasePath ? BasePath->size() : 0);
612 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
613 + PathSize * sizeof(CXXBaseSpecifier*));
614 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000615 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000616 if (PathSize) E->setCastPath(*BasePath);
617 return E;
618}
619
620CXXFunctionalCastExpr *
621CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
622 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
623 + PathSize * sizeof(CXXBaseSpecifier*));
624 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
625}
626
Richard Smith9fcce652012-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 McCallf871d0c2010-08-07 06:22:56 +0000660
Douglas Gregor65222e82009-12-23 18:19:08 +0000661CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000662CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
663 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000664 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000665 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
666 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000667}
668
Mike Stump1eb44332009-09-09 15:08:12 +0000669CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000670 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000671 return new (C) CXXTemporary(Destructor);
672}
673
Mike Stump1eb44332009-09-09 15:08:12 +0000674CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000675 CXXTemporary *Temp,
676 Expr* SubExpr) {
Peter Collingbournebceb7552011-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 Carlssonfceb0a82009-05-30 20:03:25 +0000680
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000681 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000682}
683
Anders Carlsson8e587a12009-05-30 20:56:46 +0000684CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000685 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000686 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000687 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000688 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000689 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000690 bool HadMultipleCandidates,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000691 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000692 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
693 Type->getType().getNonReferenceType(),
694 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000695 Cons, false, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000696 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000697 CXXConstructExpr::CK_Complete, parenRange),
698 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000699}
700
701SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000702 return SourceRange(Type->getTypeLoc().getBeginLoc(),
703 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000704}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000705
Mike Stump1eb44332009-09-09 15:08:12 +0000706CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000707 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000708 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000709 Expr **Args, unsigned NumArgs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000710 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000711 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000712 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000713 ConstructionKind ConstructKind,
714 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000715 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000716 Elidable, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000717 HadMultipleCandidates, ListInitialization,
718 ZeroInitialization, ConstructKind,
719 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000720}
721
Mike Stump1eb44332009-09-09 15:08:12 +0000722CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000723 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000724 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000725 Expr **args, unsigned numargs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000726 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000727 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000728 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000729 ConstructionKind ConstructKind,
730 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000731 : Expr(SC, T, VK_RValue, OK_Ordinary,
732 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000733 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734 T->containsUnexpandedParameterPack()),
Douglas Gregora48e6762011-09-26 14:47:03 +0000735 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000736 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000737 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000738 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000739 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-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 Gregorbebbe0d2010-12-15 01:34:56 +0000746
747 if (args[i]->isValueDependent())
748 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000749 if (args[i]->isInstantiationDependent())
750 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000751 if (args[i]->containsUnexpandedParameterPack())
752 ExprBits.ContainsUnexpandedParameterPack = true;
753
Douglas Gregor16006c92009-12-16 18:50:27 +0000754 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000755 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000756 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000757}
758
Douglas Gregor01d08012012-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 Gregordfca6f52012-02-13 22:00:16 +0000795 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000796 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000797 ArrayRef<VarDecl *> ArrayIndexVars,
798 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000799 SourceLocation ClosingBrace)
Douglas Gregor01d08012012-02-07 10:09:13 +0000800 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
801 T->isDependentType(), T->isDependentType(), T->isDependentType(),
802 /*ContainsUnexpandedParameterPack=*/false),
803 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000804 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000805 CaptureDefault(CaptureDefault),
806 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000807 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000808 ClosingBrace(ClosingBrace)
809{
810 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000811 CXXRecordDecl *Class = getLambdaClass();
812 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000813
814 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000815
816 // Copy captures.
817 ASTContext &Context = Class->getASTContext();
818 Data.NumCaptures = NumCaptures;
819 Data.NumExplicitCaptures = 0;
820 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
821 Capture *ToCapture = Data.Captures;
822 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
823 if (Captures[I].isExplicit())
824 ++Data.NumExplicitCaptures;
825
826 *ToCapture++ = Captures[I];
827 }
828
829 // Copy initialization expressions for the non-static data members.
830 Stmt **Stored = getStoredStmts();
831 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
832 *Stored++ = CaptureInits[I];
833
834 // Copy the body of the lambda.
835 *Stored++ = getCallOperator()->getBody();
836
837 // Copy the array index variables, if any.
838 HasArrayIndexVars = !ArrayIndexVars.empty();
839 if (HasArrayIndexVars) {
840 assert(ArrayIndexStarts.size() == NumCaptures);
841 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
842 sizeof(VarDecl *) * ArrayIndexVars.size());
843 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
844 sizeof(unsigned) * Captures.size());
845 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000846 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000847}
848
849LambdaExpr *LambdaExpr::Create(ASTContext &Context,
850 CXXRecordDecl *Class,
851 SourceRange IntroducerRange,
852 LambdaCaptureDefault CaptureDefault,
853 ArrayRef<Capture> Captures,
854 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000855 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000856 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000857 ArrayRef<VarDecl *> ArrayIndexVars,
858 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000859 SourceLocation ClosingBrace) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000860 // Determine the type of the expression (i.e., the type of the
861 // function object we're creating).
862 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000863
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000864 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
865 if (!ArrayIndexVars.empty())
866 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
867 + sizeof(unsigned) * (Captures.size() + 1);
868 void *Mem = Context.Allocate(Size);
869 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000870 Captures, ExplicitParams, ExplicitResultType,
871 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000872 ClosingBrace);
Douglas Gregor01d08012012-02-07 10:09:13 +0000873}
874
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000875LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
876 unsigned NumArrayIndexVars) {
877 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
878 if (NumArrayIndexVars)
879 Size += sizeof(VarDecl) * NumArrayIndexVars
880 + sizeof(unsigned) * (NumCaptures + 1);
881 void *Mem = C.Allocate(Size);
882 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
883}
884
Douglas Gregorda8962a2012-02-13 15:44:47 +0000885LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000886 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000887}
888
889LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000890 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000891}
892
893LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
894 return capture_begin();
895}
896
897LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
898 struct CXXRecordDecl::LambdaDefinitionData &Data
899 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000900 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000901}
902
903LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
904 return explicit_capture_end();
905}
906
907LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
908 return capture_end();
909}
910
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000911ArrayRef<VarDecl *>
912LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000913 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000914
915 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000916 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
917 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000918 VarDecl **IndexVars = getArrayIndexVars();
919 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000920 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
921 IndexVars + IndexStarts[Index + 1]);
922}
923
Douglas Gregor01d08012012-02-07 10:09:13 +0000924CXXRecordDecl *LambdaExpr::getLambdaClass() const {
925 return getType()->getAsCXXRecordDecl();
926}
927
928CXXMethodDecl *LambdaExpr::getCallOperator() const {
929 CXXRecordDecl *Record = getLambdaClass();
930 DeclarationName Name
931 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
932 DeclContext::lookup_result Calls = Record->lookup(Name);
933 assert(Calls.first != Calls.second && "Missing lambda call operator!");
934 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
935 assert(Calls.first == Calls.second && "More than lambda one call operator?");
936 return Result;
937}
938
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000939CompoundStmt *LambdaExpr::getBody() const {
940 if (!getStoredStmts()[NumCaptures])
941 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
942
943 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
944}
945
Douglas Gregor01d08012012-02-07 10:09:13 +0000946bool LambdaExpr::isMutable() const {
947 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
948}
949
John McCall80ee6e82011-11-10 05:35:25 +0000950ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
951 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000952 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000953 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000954 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000955 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000956 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000957 SubExpr(subexpr) {
958 ExprWithCleanupsBits.NumObjects = objects.size();
959 for (unsigned i = 0, e = objects.size(); i != e; ++i)
960 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000961}
962
John McCall80ee6e82011-11-10 05:35:25 +0000963ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
964 ArrayRef<CleanupObject> objects) {
965 size_t size = sizeof(ExprWithCleanups)
966 + objects.size() * sizeof(CleanupObject);
967 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
968 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000969}
970
John McCall80ee6e82011-11-10 05:35:25 +0000971ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
972 : Expr(ExprWithCleanupsClass, empty) {
973 ExprWithCleanupsBits.NumObjects = numObjects;
974}
Chris Lattnerd2598362010-05-10 00:25:06 +0000975
John McCall80ee6e82011-11-10 05:35:25 +0000976ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
977 unsigned numObjects) {
978 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
979 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
980 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000981}
982
Douglas Gregorab6677e2010-09-08 00:15:04 +0000983CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000984 SourceLocation LParenLoc,
985 Expr **Args,
986 unsigned NumArgs,
987 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000988 : Expr(CXXUnresolvedConstructExprClass,
989 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000990 (Type->getType()->isLValueReferenceType() ? VK_LValue
991 :Type->getType()->isRValueReferenceType()? VK_XValue
992 :VK_RValue),
993 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000994 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000995 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000996 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000997 LParenLoc(LParenLoc),
998 RParenLoc(RParenLoc),
999 NumArgs(NumArgs) {
1000 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001001 for (unsigned I = 0; I != NumArgs; ++I) {
1002 if (Args[I]->containsUnexpandedParameterPack())
1003 ExprBits.ContainsUnexpandedParameterPack = true;
1004
1005 StoredArgs[I] = Args[I];
1006 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001007}
1008
1009CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001010CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +00001011 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001012 SourceLocation LParenLoc,
1013 Expr **Args,
1014 unsigned NumArgs,
1015 SourceLocation RParenLoc) {
1016 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1017 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +00001018 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001019 Args, NumArgs, RParenLoc);
1020}
1021
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001022CXXUnresolvedConstructExpr *
1023CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1024 Stmt::EmptyShell Empty;
1025 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1026 sizeof(Expr *) * NumArgs);
1027 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1028}
1029
Douglas Gregorab6677e2010-09-08 00:15:04 +00001030SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1031 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1032}
1033
John McCall865d4472009-11-19 22:55:06 +00001034CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001035 Expr *Base, QualType BaseType,
1036 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001037 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001038 NestedNameSpecifierLoc QualifierLoc,
1039 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001040 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001041 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001042 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001043 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001044 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001045 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001046 (QualifierLoc &&
1047 QualifierLoc.getNestedNameSpecifier()
1048 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001049 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001050 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001051 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001052 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001053 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001054 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 if (TemplateArgs) {
1056 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001057 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001058 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001059 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1060 Dependent,
1061 InstantiationDependent,
1062 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001063 if (ContainsUnexpandedParameterPack)
1064 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001065 } else if (TemplateKWLoc.isValid()) {
1066 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001067 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001068}
1069
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001070CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1071 Expr *Base, QualType BaseType,
1072 bool IsArrow,
1073 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001074 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001075 NamedDecl *FirstQualifierFoundInScope,
1076 DeclarationNameInfo MemberNameInfo)
1077 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001078 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001079 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001080 (QualifierLoc &&
1081 QualifierLoc.getNestedNameSpecifier()->
1082 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001083 MemberNameInfo.containsUnexpandedParameterPack())),
1084 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001085 HasTemplateKWAndArgsInfo(false),
1086 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001087 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1088 MemberNameInfo(MemberNameInfo) { }
1089
John McCall865d4472009-11-19 22:55:06 +00001090CXXDependentScopeMemberExpr *
1091CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001092 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001093 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001094 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001095 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001096 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001097 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001098 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001099 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001100 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1101 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001102 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001103 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001104 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001106 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1107 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1108 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001109
Chris Lattner32488542010-10-30 05:14:06 +00001110 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001111 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1112 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001113 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001114 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001115 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001116 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001117}
1118
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001119CXXDependentScopeMemberExpr *
1120CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001121 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001122 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001123 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001124 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001125 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001126 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001127 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001128
1129 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001130 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001131 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001132 CXXDependentScopeMemberExpr *E
1133 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001134 0, SourceLocation(),
1135 NestedNameSpecifierLoc(),
1136 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001137 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001138 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001139 return E;
1140}
1141
Douglas Gregor4c9be892011-02-28 20:01:57 +00001142bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1143 if (Base == 0)
1144 return true;
1145
Douglas Gregor75e85042011-03-02 21:06:53 +00001146 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001147}
1148
John McCall864c0412011-04-26 20:42:42 +00001149static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1150 UnresolvedSetIterator end) {
1151 do {
1152 NamedDecl *decl = *begin;
1153 if (isa<UnresolvedUsingValueDecl>(decl))
1154 return false;
1155 if (isa<UsingShadowDecl>(decl))
1156 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1157
1158 // Unresolved member expressions should only contain methods and
1159 // method templates.
1160 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1161
1162 if (isa<FunctionTemplateDecl>(decl))
1163 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1164 if (cast<CXXMethodDecl>(decl)->isStatic())
1165 return false;
1166 } while (++begin != end);
1167
1168 return true;
1169}
1170
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001171UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001172 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001173 Expr *Base, QualType BaseType,
1174 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001175 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001176 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001177 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001178 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001179 const TemplateArgumentListInfo *TemplateArgs,
1180 UnresolvedSetIterator Begin,
1181 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001182 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1183 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001184 // Dependent
1185 ((Base && Base->isTypeDependent()) ||
1186 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001187 ((Base && Base->isInstantiationDependent()) ||
1188 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001189 // Contains unexpanded parameter pack
1190 ((Base && Base->containsUnexpandedParameterPack()) ||
1191 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001192 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1193 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001194
1195 // Check whether all of the members are non-static member functions,
1196 // and if so, mark give this bound-member type instead of overload type.
1197 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1198 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001199}
1200
Douglas Gregor4c9be892011-02-28 20:01:57 +00001201bool UnresolvedMemberExpr::isImplicitAccess() const {
1202 if (Base == 0)
1203 return true;
1204
Douglas Gregor75e85042011-03-02 21:06:53 +00001205 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001206}
1207
John McCall129e2df2009-11-30 22:42:35 +00001208UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001209UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001210 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001211 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001212 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001213 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001214 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001215 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001216 const TemplateArgumentListInfo *TemplateArgs,
1217 UnresolvedSetIterator Begin,
1218 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001219 std::size_t size = sizeof(UnresolvedMemberExpr);
1220 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001221 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1222 else if (TemplateKWLoc.isValid())
1223 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001224
Chris Lattner32488542010-10-30 05:14:06 +00001225 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001226 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001227 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001228 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001229 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001230}
1231
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001232UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001233UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001234 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001235 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001236 if (HasTemplateKWAndArgsInfo)
1237 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001238
Chris Lattner32488542010-10-30 05:14:06 +00001239 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001240 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001241 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001242 return E;
1243}
1244
John McCallc373d482010-01-27 01:50:18 +00001245CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1246 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1247
1248 // If there was a nested name specifier, it names the naming class.
1249 // It can't be dependent: after all, we were actually able to do the
1250 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001251 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001252 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001253 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001254 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001255 Record = T->getAsCXXRecordDecl();
1256 assert(Record && "qualifier in member expression does not name record");
1257 }
John McCallc373d482010-01-27 01:50:18 +00001258 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001259 else {
John McCallc373d482010-01-27 01:50:18 +00001260 QualType BaseType = getBaseType().getNonReferenceType();
1261 if (isArrow()) {
1262 const PointerType *PT = BaseType->getAs<PointerType>();
1263 assert(PT && "base of arrow member access is not pointer");
1264 BaseType = PT->getPointeeType();
1265 }
1266
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001267 Record = BaseType->getAsCXXRecordDecl();
1268 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001269 }
1270
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001271 return Record;
John McCallc373d482010-01-27 01:50:18 +00001272}
1273
Douglas Gregorc7793c72011-01-15 01:15:58 +00001274SubstNonTypeTemplateParmPackExpr::
1275SubstNonTypeTemplateParmPackExpr(QualType T,
1276 NonTypeTemplateParmDecl *Param,
1277 SourceLocation NameLoc,
1278 const TemplateArgument &ArgPack)
1279 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001280 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001281 Param(Param), Arguments(ArgPack.pack_begin()),
1282 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1283
1284TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1285 return TemplateArgument(Arguments, NumArguments);
1286}
1287
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001288TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1289 ArrayRef<TypeSourceInfo *> Args,
1290 SourceLocation RParenLoc,
1291 bool Value)
1292 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1293 /*TypeDependent=*/false,
1294 /*ValueDependent=*/false,
1295 /*InstantiationDependent=*/false,
1296 /*ContainsUnexpandedParameterPack=*/false),
1297 Loc(Loc), RParenLoc(RParenLoc)
1298{
1299 TypeTraitExprBits.Kind = Kind;
1300 TypeTraitExprBits.Value = Value;
1301 TypeTraitExprBits.NumArgs = Args.size();
1302
1303 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1304
1305 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1306 if (Args[I]->getType()->isDependentType())
1307 setValueDependent(true);
1308 if (Args[I]->getType()->isInstantiationDependentType())
1309 setInstantiationDependent(true);
1310 if (Args[I]->getType()->containsUnexpandedParameterPack())
1311 setContainsUnexpandedParameterPack(true);
1312
1313 ToArgs[I] = Args[I];
1314 }
1315}
1316
1317TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1318 SourceLocation Loc,
1319 TypeTrait Kind,
1320 ArrayRef<TypeSourceInfo *> Args,
1321 SourceLocation RParenLoc,
1322 bool Value) {
1323 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1324 void *Mem = C.Allocate(Size);
1325 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1326}
1327
1328TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1329 unsigned NumArgs) {
1330 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1331 void *Mem = C.Allocate(Size);
1332 return new (Mem) TypeTraitExpr(EmptyShell());
1333}
1334
David Blaikie99ba9e32011-12-20 02:48:34 +00001335void ArrayTypeTraitExpr::anchor() { }