blob: a7240e6978ada5a0a3b2c49eea4a27020385098c [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"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000020using namespace clang;
21
Douglas Gregor9da64192010-04-26 22:37:10 +000022
Ted Kremeneke3a0c142007-08-24 20:21:10 +000023//===----------------------------------------------------------------------===//
24// Child Iterators for iterating over subexpressions/substatements
25//===----------------------------------------------------------------------===//
26
Douglas Gregor9da64192010-04-26 22:37:10 +000027QualType CXXTypeidExpr::getTypeOperand() const {
28 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
29 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
30 .getUnqualifiedType();
31}
32
Francois Pichet9f4f2072010-09-08 12:20:18 +000033QualType CXXUuidofExpr::getTypeOperand() const {
34 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
35 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
36 .getUnqualifiedType();
37}
38
Douglas Gregor747eb782010-07-08 06:14:04 +000039// CXXScalarValueInitExpr
Douglas Gregor2b88c112010-09-08 00:15:04 +000040SourceRange CXXScalarValueInitExpr::getSourceRange() const {
41 SourceLocation Start = RParenLoc;
42 if (TypeInfo)
43 Start = TypeInfo->getTypeLoc().getBeginLoc();
44 return SourceRange(Start, RParenLoc);
45}
46
Sebastian Redlbd150f42008-11-21 19:14:01 +000047// CXXNewExpr
Ted Kremenek9d6eb402010-02-11 22:51:03 +000048CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redlc3a3c602012-02-16 11:35:52 +000049 FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +000050 bool usualArrayDeleteWantsSize,
51 Expr **placementArgs, unsigned numPlaceArgs,
52 SourceRange typeIdParens, Expr *arraySize,
53 InitializationStyle initializationStyle,
54 Expr *initializer, QualType ty,
55 TypeSourceInfo *allocatedTypeInfo,
56 SourceLocation startLoc, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +000057 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000058 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000059 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000060 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +000061 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
62 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +000063 StartLoc(startLoc), DirectInitRange(directInitRange),
64 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +000065 assert((initializer != 0 || initializationStyle == NoInit) &&
66 "Only NoInit can have no initializer.");
67 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
68 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +000069 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000070 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000071 if (arraySize->isInstantiationDependent())
72 ExprBits.InstantiationDependent = true;
73
Douglas Gregora6e053e2010-12-15 01:34:56 +000074 if (arraySize->containsUnexpandedParameterPack())
75 ExprBits.ContainsUnexpandedParameterPack = true;
76
Sebastian Redl351bb782008-12-02 14:43:59 +000077 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000078 }
79
Sebastian Redl6047f072012-02-16 12:22:20 +000080 if (initializer) {
81 if (initializer->isInstantiationDependent())
82 ExprBits.InstantiationDependent = true;
83
84 if (initializer->containsUnexpandedParameterPack())
85 ExprBits.ContainsUnexpandedParameterPack = true;
86
87 SubExprs[i++] = initializer;
88 }
89
Douglas Gregora6e053e2010-12-15 01:34:56 +000090 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000091 if (placementArgs[j]->isInstantiationDependent())
92 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +000093 if (placementArgs[j]->containsUnexpandedParameterPack())
94 ExprBits.ContainsUnexpandedParameterPack = true;
95
Sebastian Redlbd150f42008-11-21 19:14:01 +000096 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +000097 }
Sebastian Redlbd150f42008-11-21 19:14:01 +000098}
99
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000100void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000101 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000102 assert(SubExprs == 0 && "SubExprs already allocated");
103 Array = isArray;
104 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000105
106 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000107 SubExprs = new (C) Stmt*[TotalSize];
108}
109
Sebastian Redl31ad7542011-03-13 17:09:40 +0000110bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000111 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000112 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000113}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000114
Sebastian Redl6047f072012-02-16 12:22:20 +0000115SourceLocation CXXNewExpr::getEndLoc() const {
116 switch (getInitializationStyle()) {
117 case NoInit:
118 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
119 case CallInit:
120 return DirectInitRange.getEnd();
121 case ListInit:
122 return getInitializer()->getSourceRange().getEnd();
123 }
Matt Beaumont-Gay2150d382012-02-16 22:15:50 +0000124 llvm_unreachable("bogus initialization style");
Sebastian Redl6047f072012-02-16 12:22:20 +0000125}
126
Sebastian Redlbd150f42008-11-21 19:14:01 +0000127// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000128QualType CXXDeleteExpr::getDestroyedType() const {
129 const Expr *Arg = getArgument();
130 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
131 if (ICE->getCastKind() != CK_UserDefinedConversion &&
132 ICE->getType()->isVoidPointerType())
133 Arg = ICE->getSubExpr();
134 else
135 break;
136 }
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000137 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000138 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000139
140 if (ArgType->isDependentType() && !ArgType->isPointerType())
141 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000142
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000143 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000144}
145
Douglas Gregorad8a3362009-09-04 17:36:40 +0000146// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000147PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
148 : Type(Info)
149{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000150 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000151}
152
John McCalldb40c7f2010-12-14 08:05:40 +0000153CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000154 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
155 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
156 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
157 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000158 : Expr(CXXPseudoDestructorExprClass,
159 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
160 FunctionProtoType::ExtProtoInfo())),
161 VK_RValue, OK_Ordinary,
162 /*isTypeDependent=*/(Base->isTypeDependent() ||
163 (DestroyedType.getTypeSourceInfo() &&
164 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000165 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000166 (Base->isInstantiationDependent() ||
167 (QualifierLoc &&
168 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
169 (ScopeType &&
170 ScopeType->getType()->isInstantiationDependentType()) ||
171 (DestroyedType.getTypeSourceInfo() &&
172 DestroyedType.getTypeSourceInfo()->getType()
173 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000174 // ContainsUnexpandedParameterPack
175 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000176 (QualifierLoc &&
177 QualifierLoc.getNestedNameSpecifier()
178 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000179 (ScopeType &&
180 ScopeType->getType()->containsUnexpandedParameterPack()) ||
181 (DestroyedType.getTypeSourceInfo() &&
182 DestroyedType.getTypeSourceInfo()->getType()
183 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000184 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000185 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000186 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
187 DestroyedType(DestroyedType) { }
188
Douglas Gregor678f90d2010-02-25 01:56:36 +0000189QualType CXXPseudoDestructorExpr::getDestroyedType() const {
190 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
191 return TInfo->getType();
192
193 return QualType();
194}
195
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000196SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000197 SourceLocation End = DestroyedType.getLocation();
198 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000199 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000200 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000201}
202
John McCalld14a8642009-11-21 08:51:07 +0000203// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000204UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000205UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000206 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000207 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000208 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000209 const DeclarationNameInfo &NameInfo,
210 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000211 const TemplateArgumentListInfo *Args,
212 UnresolvedSetIterator Begin,
213 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000214{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000215 assert(Args || TemplateKWLoc.isValid());
216 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000217 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000218 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000219 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
220 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000221 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000222 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000223}
224
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000225UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000226UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
227 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000228 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000229 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000230 if (HasTemplateKWAndArgsInfo)
231 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000232
Chris Lattner5c0b4052010-10-30 05:14:06 +0000233 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000234 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000235 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000236 return E;
237}
238
Douglas Gregora6e053e2010-12-15 01:34:56 +0000239OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000240 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000241 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000242 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000243 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000244 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000245 UnresolvedSetIterator End,
246 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000247 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248 bool KnownContainsUnexpandedParameterPack)
249 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
250 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000251 (KnownInstantiationDependent ||
252 NameInfo.isInstantiationDependent() ||
253 (QualifierLoc &&
254 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000255 (KnownContainsUnexpandedParameterPack ||
256 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000257 (QualifierLoc &&
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000260 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
261 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000262 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000263{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000264 NumResults = End - Begin;
265 if (NumResults) {
266 // Determine whether this expression is type-dependent.
267 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
268 if ((*I)->getDeclContext()->isDependentContext() ||
269 isa<UnresolvedUsingValueDecl>(*I)) {
270 ExprBits.TypeDependent = true;
271 ExprBits.ValueDependent = true;
272 }
273 }
274
275 Results = static_cast<DeclAccessPair *>(
276 C.Allocate(sizeof(DeclAccessPair) * NumResults,
277 llvm::alignOf<DeclAccessPair>()));
278 memcpy(Results, &*Begin.getIterator(),
279 NumResults * sizeof(DeclAccessPair));
280 }
281
282 // If we have explicit template arguments, check for dependent
283 // template arguments and whether they contain any unexpanded pack
284 // expansions.
285 if (TemplateArgs) {
286 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000287 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000288 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000289 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
290 Dependent,
291 InstantiationDependent,
292 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000293
294 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000295 ExprBits.TypeDependent = true;
296 ExprBits.ValueDependent = true;
297 }
298 if (InstantiationDependent)
299 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000300 if (ContainsUnexpandedParameterPack)
301 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 } else if (TemplateKWLoc.isValid()) {
303 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000304 }
305
306 if (isTypeDependent())
307 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000308}
309
310void OverloadExpr::initializeResults(ASTContext &C,
311 UnresolvedSetIterator Begin,
312 UnresolvedSetIterator End) {
313 assert(Results == 0 && "Results already initialized!");
314 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000315 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000316 Results = static_cast<DeclAccessPair *>(
317 C.Allocate(sizeof(DeclAccessPair) * NumResults,
318
319 llvm::alignOf<DeclAccessPair>()));
320 memcpy(Results, &*Begin.getIterator(),
321 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000322 }
323}
324
John McCall8c12dc42010-04-22 18:44:12 +0000325CXXRecordDecl *OverloadExpr::getNamingClass() const {
326 if (isa<UnresolvedLookupExpr>(this))
327 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
328 else
329 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
330}
331
John McCall8cd78132009-11-19 22:55:06 +0000332// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000333DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000334 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000335 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000336 const DeclarationNameInfo &NameInfo,
337 const TemplateArgumentListInfo *Args)
338 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
339 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000340 (NameInfo.isInstantiationDependent() ||
341 (QualifierLoc &&
342 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000343 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000344 (QualifierLoc &&
345 QualifierLoc.getNestedNameSpecifier()
346 ->containsUnexpandedParameterPack()))),
347 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000348 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349{
350 if (Args) {
351 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000352 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000353 bool ContainsUnexpandedParameterPack
354 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000355 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
356 Dependent,
357 InstantiationDependent,
358 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000360 } else if (TemplateKWLoc.isValid()) {
361 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000362 }
363}
364
John McCalle66edc12009-11-24 19:00:30 +0000365DependentScopeDeclRefExpr *
366DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000368 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000369 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *Args) {
371 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000372 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000373 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
374 else if (TemplateKWLoc.isValid())
375 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000376 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
378 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000379}
380
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000381DependentScopeDeclRefExpr *
382DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000383 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000384 unsigned NumTemplateArgs) {
385 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 if (HasTemplateKWAndArgsInfo)
387 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000388 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000389 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000390 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000391 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000392 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000393 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000394 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000395}
396
Chandler Carruth01718152010-10-25 08:47:36 +0000397SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000398 if (isa<CXXTemporaryObjectExpr>(this))
399 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
400
Douglas Gregor15417cf2010-11-03 00:35:38 +0000401 if (ParenRange.isValid())
402 return SourceRange(Loc, ParenRange.getEnd());
403
404 SourceLocation End = Loc;
405 for (unsigned I = getNumArgs(); I > 0; --I) {
406 const Expr *Arg = getArg(I-1);
407 if (!Arg->isDefaultArgument()) {
408 SourceLocation NewEnd = Arg->getLocEnd();
409 if (NewEnd.isValid()) {
410 End = NewEnd;
411 break;
412 }
413 }
414 }
415
416 return SourceRange(Loc, End);
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000417}
418
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000419SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000420 OverloadedOperatorKind Kind = getOperator();
421 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
422 if (getNumArgs() == 1)
423 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000424 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000425 else
426 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000427 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000428 } else if (Kind == OO_Arrow) {
429 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000430 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000431 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000432 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000433 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000434 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000435 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000436 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000437 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000438 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000439 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000440 }
441}
442
Ted Kremenek98a24e32011-03-30 17:41:19 +0000443Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
444 if (const MemberExpr *MemExpr =
445 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000446 return MemExpr->getBase();
447
448 // FIXME: Will eventually need to cope with member pointers.
449 return 0;
450}
451
Ted Kremenek98a24e32011-03-30 17:41:19 +0000452CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
453 if (const MemberExpr *MemExpr =
454 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
455 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
456
457 // FIXME: Will eventually need to cope with member pointers.
458 return 0;
459}
460
461
David Blaikiec0f58662012-05-03 16:25:49 +0000462CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000463 Expr* ThisArg = getImplicitObjectArgument();
464 if (!ThisArg)
465 return 0;
466
467 if (ThisArg->getType()->isAnyPointerType())
468 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
469
470 return ThisArg->getType()->getAsCXXRecordDecl();
471}
472
Douglas Gregoref986e82009-11-12 15:31:47 +0000473
Douglas Gregore200adc2008-10-27 19:41:14 +0000474//===----------------------------------------------------------------------===//
475// Named casts
476//===----------------------------------------------------------------------===//
477
478/// getCastName - Get the name of the C++ cast being used, e.g.,
479/// "static_cast", "dynamic_cast", "reinterpret_cast", or
480/// "const_cast". The returned pointer must not be freed.
481const char *CXXNamedCastExpr::getCastName() const {
482 switch (getStmtClass()) {
483 case CXXStaticCastExprClass: return "static_cast";
484 case CXXDynamicCastExprClass: return "dynamic_cast";
485 case CXXReinterpretCastExprClass: return "reinterpret_cast";
486 case CXXConstCastExprClass: return "const_cast";
487 default: return "<invalid cast>";
488 }
489}
Douglas Gregordd04d332009-01-16 18:33:17 +0000490
John McCallcf142162010-08-07 06:22:56 +0000491CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000492 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000493 CastKind K, Expr *Op,
494 const CXXCastPath *BasePath,
495 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000496 SourceLocation L,
497 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000498 unsigned PathSize = (BasePath ? BasePath->size() : 0);
499 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
500 + PathSize * sizeof(CXXBaseSpecifier*));
501 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000502 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
503 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000504 if (PathSize) E->setCastPath(*BasePath);
505 return E;
506}
507
508CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
509 unsigned PathSize) {
510 void *Buffer =
511 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
512 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
513}
514
515CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000516 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000517 CastKind K, Expr *Op,
518 const CXXCastPath *BasePath,
519 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000520 SourceLocation L,
521 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000522 unsigned PathSize = (BasePath ? BasePath->size() : 0);
523 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
524 + PathSize * sizeof(CXXBaseSpecifier*));
525 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000526 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
527 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000528 if (PathSize) E->setCastPath(*BasePath);
529 return E;
530}
531
532CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
533 unsigned PathSize) {
534 void *Buffer =
535 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
536 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
537}
538
Anders Carlsson267c0c92011-04-11 01:43:55 +0000539/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
540/// to always be null. For example:
541///
542/// struct A { };
543/// struct B final : A { };
544/// struct C { };
545///
546/// C *f(B* b) { return dynamic_cast<C*>(b); }
547bool CXXDynamicCastExpr::isAlwaysNull() const
548{
549 QualType SrcType = getSubExpr()->getType();
550 QualType DestType = getType();
551
552 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
553 SrcType = SrcPTy->getPointeeType();
554 DestType = DestType->castAs<PointerType>()->getPointeeType();
555 }
556
Alexis Hunt78e2b912012-06-19 23:44:55 +0000557 if (DestType->isVoidType())
558 return false;
559
Anders Carlsson267c0c92011-04-11 01:43:55 +0000560 const CXXRecordDecl *SrcRD =
561 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
562
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000563 if (!SrcRD->hasAttr<FinalAttr>())
564 return false;
565
Anders Carlsson267c0c92011-04-11 01:43:55 +0000566 const CXXRecordDecl *DestRD =
567 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
568
569 return !DestRD->isDerivedFrom(SrcRD);
570}
571
John McCallcf142162010-08-07 06:22:56 +0000572CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000573CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
574 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000575 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000576 TypeSourceInfo *WrittenTy, SourceLocation L,
577 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000578 unsigned PathSize = (BasePath ? BasePath->size() : 0);
579 void *Buffer =
580 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
581 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000582 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
583 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000584 if (PathSize) E->setCastPath(*BasePath);
585 return E;
586}
587
588CXXReinterpretCastExpr *
589CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
590 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
591 + PathSize * sizeof(CXXBaseSpecifier*));
592 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
593}
594
John McCall7decc9e2010-11-18 06:31:45 +0000595CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
596 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000597 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000598 SourceLocation L,
599 SourceLocation RParenLoc) {
600 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000601}
602
603CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
604 return new (C) CXXConstCastExpr(EmptyShell());
605}
606
607CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000608CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000609 TypeSourceInfo *Written, SourceLocation L,
610 CastKind K, Expr *Op, const CXXCastPath *BasePath,
611 SourceLocation R) {
612 unsigned PathSize = (BasePath ? BasePath->size() : 0);
613 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
614 + PathSize * sizeof(CXXBaseSpecifier*));
615 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000616 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000617 if (PathSize) E->setCastPath(*BasePath);
618 return E;
619}
620
621CXXFunctionalCastExpr *
622CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
623 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
624 + PathSize * sizeof(CXXBaseSpecifier*));
625 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
626}
627
Richard Smithc67fdd42012-03-07 08:35:16 +0000628UserDefinedLiteral::LiteralOperatorKind
629UserDefinedLiteral::getLiteralOperatorKind() const {
630 if (getNumArgs() == 0)
631 return LOK_Template;
632 if (getNumArgs() == 2)
633 return LOK_String;
634
635 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
636 QualType ParamTy =
637 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
638 if (ParamTy->isPointerType())
639 return LOK_Raw;
640 if (ParamTy->isAnyCharacterType())
641 return LOK_Character;
642 if (ParamTy->isIntegerType())
643 return LOK_Integer;
644 if (ParamTy->isFloatingType())
645 return LOK_Floating;
646
647 llvm_unreachable("unknown kind of literal operator");
648}
649
650Expr *UserDefinedLiteral::getCookedLiteral() {
651#ifndef NDEBUG
652 LiteralOperatorKind LOK = getLiteralOperatorKind();
653 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
654#endif
655 return getArg(0);
656}
657
658const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
659 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
660}
John McCallcf142162010-08-07 06:22:56 +0000661
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000662CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000663CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
664 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000665 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000666 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
667 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000668}
669
Mike Stump11289f42009-09-09 15:08:12 +0000670CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000671 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000672 return new (C) CXXTemporary(Destructor);
673}
674
Mike Stump11289f42009-09-09 15:08:12 +0000675CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000676 CXXTemporary *Temp,
677 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000678 assert((SubExpr->getType()->isRecordType() ||
679 SubExpr->getType()->isArrayType()) &&
680 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000681
Douglas Gregora6e053e2010-12-15 01:34:56 +0000682 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000683}
684
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000685CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000686 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000687 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000688 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000689 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000690 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000691 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000692 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000693 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
694 Type->getType().getNonReferenceType(),
695 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000696 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000697 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000698 CXXConstructExpr::CK_Complete, parenRange),
699 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000700}
701
702SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000703 return SourceRange(Type->getTypeLoc().getBeginLoc(),
704 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000705}
Anders Carlsson6f287832009-04-21 02:22:11 +0000706
Mike Stump11289f42009-09-09 15:08:12 +0000707CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000708 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000709 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000710 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000711 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000712 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000713 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000714 ConstructionKind ConstructKind,
715 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000716 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000717 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000718 HadMultipleCandidates, ListInitialization,
719 ZeroInitialization, ConstructKind,
720 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000721}
722
Mike Stump11289f42009-09-09 15:08:12 +0000723CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000724 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000725 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000726 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000727 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000728 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000729 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000730 ConstructionKind ConstructKind,
731 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000732 : Expr(SC, T, VK_RValue, OK_Ordinary,
733 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000734 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000735 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000736 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000737 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000738 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000739 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000740 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000741{
742 if (NumArgs) {
743 Args = new (C) Stmt*[NumArgs];
744
745 for (unsigned i = 0; i != NumArgs; ++i) {
746 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000747
748 if (args[i]->isValueDependent())
749 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000750 if (args[i]->isInstantiationDependent())
751 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000752 if (args[i]->containsUnexpandedParameterPack())
753 ExprBits.ContainsUnexpandedParameterPack = true;
754
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000755 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000756 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000757 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000758}
759
Douglas Gregore31e6062012-02-07 10:09:13 +0000760LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
761 LambdaCaptureKind Kind, VarDecl *Var,
762 SourceLocation EllipsisLoc)
763 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
764{
765 unsigned Bits = 0;
766 if (Implicit)
767 Bits |= Capture_Implicit;
768
769 switch (Kind) {
770 case LCK_This:
771 assert(Var == 0 && "'this' capture cannot have a variable!");
772 break;
773
774 case LCK_ByCopy:
775 Bits |= Capture_ByCopy;
776 // Fall through
777 case LCK_ByRef:
778 assert(Var && "capture must have a variable!");
779 break;
780 }
781 VarAndBits.setInt(Bits);
782}
783
784LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
785 if (capturesThis())
786 return LCK_This;
787
788 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
789}
790
791LambdaExpr::LambdaExpr(QualType T,
792 SourceRange IntroducerRange,
793 LambdaCaptureDefault CaptureDefault,
794 ArrayRef<Capture> Captures,
795 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000796 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000797 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000798 ArrayRef<VarDecl *> ArrayIndexVars,
799 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000800 SourceLocation ClosingBrace)
Douglas Gregore31e6062012-02-07 10:09:13 +0000801 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
802 T->isDependentType(), T->isDependentType(), T->isDependentType(),
803 /*ContainsUnexpandedParameterPack=*/false),
804 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000805 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000806 CaptureDefault(CaptureDefault),
807 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000808 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000809 ClosingBrace(ClosingBrace)
810{
811 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000812 CXXRecordDecl *Class = getLambdaClass();
813 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000814
815 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000816
817 // Copy captures.
818 ASTContext &Context = Class->getASTContext();
819 Data.NumCaptures = NumCaptures;
820 Data.NumExplicitCaptures = 0;
821 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
822 Capture *ToCapture = Data.Captures;
823 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
824 if (Captures[I].isExplicit())
825 ++Data.NumExplicitCaptures;
826
827 *ToCapture++ = Captures[I];
828 }
829
830 // Copy initialization expressions for the non-static data members.
831 Stmt **Stored = getStoredStmts();
832 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
833 *Stored++ = CaptureInits[I];
834
835 // Copy the body of the lambda.
836 *Stored++ = getCallOperator()->getBody();
837
838 // Copy the array index variables, if any.
839 HasArrayIndexVars = !ArrayIndexVars.empty();
840 if (HasArrayIndexVars) {
841 assert(ArrayIndexStarts.size() == NumCaptures);
842 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
843 sizeof(VarDecl *) * ArrayIndexVars.size());
844 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
845 sizeof(unsigned) * Captures.size());
846 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000847 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000848}
849
850LambdaExpr *LambdaExpr::Create(ASTContext &Context,
851 CXXRecordDecl *Class,
852 SourceRange IntroducerRange,
853 LambdaCaptureDefault CaptureDefault,
854 ArrayRef<Capture> Captures,
855 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000856 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000857 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000858 ArrayRef<VarDecl *> ArrayIndexVars,
859 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000860 SourceLocation ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000861 // Determine the type of the expression (i.e., the type of the
862 // function object we're creating).
863 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000864
Douglas Gregore5561632012-02-13 17:20:40 +0000865 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
866 if (!ArrayIndexVars.empty())
867 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
868 + sizeof(unsigned) * (Captures.size() + 1);
869 void *Mem = Context.Allocate(Size);
870 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000871 Captures, ExplicitParams, ExplicitResultType,
872 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregorb61e8092012-04-04 17:40:10 +0000873 ClosingBrace);
Douglas Gregore31e6062012-02-07 10:09:13 +0000874}
875
Douglas Gregor99ae8062012-02-14 17:54:36 +0000876LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
877 unsigned NumArrayIndexVars) {
878 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
879 if (NumArrayIndexVars)
880 Size += sizeof(VarDecl) * NumArrayIndexVars
881 + sizeof(unsigned) * (NumCaptures + 1);
882 void *Mem = C.Allocate(Size);
883 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
884}
885
Douglas Gregorc8a73492012-02-13 15:44:47 +0000886LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000887 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000888}
889
890LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000891 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000892}
893
894LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
895 return capture_begin();
896}
897
898LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
899 struct CXXRecordDecl::LambdaDefinitionData &Data
900 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000901 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000902}
903
904LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
905 return explicit_capture_end();
906}
907
908LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
909 return capture_end();
910}
911
Douglas Gregor54fcea62012-02-13 16:35:30 +0000912ArrayRef<VarDecl *>
913LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000914 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000915
916 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000917 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
918 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000919 VarDecl **IndexVars = getArrayIndexVars();
920 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000921 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
922 IndexVars + IndexStarts[Index + 1]);
923}
924
Douglas Gregore31e6062012-02-07 10:09:13 +0000925CXXRecordDecl *LambdaExpr::getLambdaClass() const {
926 return getType()->getAsCXXRecordDecl();
927}
928
929CXXMethodDecl *LambdaExpr::getCallOperator() const {
930 CXXRecordDecl *Record = getLambdaClass();
931 DeclarationName Name
932 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
933 DeclContext::lookup_result Calls = Record->lookup(Name);
934 assert(Calls.first != Calls.second && "Missing lambda call operator!");
935 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
936 assert(Calls.first == Calls.second && "More than lambda one call operator?");
937 return Result;
938}
939
Douglas Gregor99ae8062012-02-14 17:54:36 +0000940CompoundStmt *LambdaExpr::getBody() const {
941 if (!getStoredStmts()[NumCaptures])
942 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
943
944 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
945}
946
Douglas Gregore31e6062012-02-07 10:09:13 +0000947bool LambdaExpr::isMutable() const {
948 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
949}
950
John McCall28fc7092011-11-10 05:35:25 +0000951ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
952 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000953 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000954 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000955 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000956 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000957 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000958 SubExpr(subexpr) {
959 ExprWithCleanupsBits.NumObjects = objects.size();
960 for (unsigned i = 0, e = objects.size(); i != e; ++i)
961 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000962}
963
John McCall28fc7092011-11-10 05:35:25 +0000964ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
965 ArrayRef<CleanupObject> objects) {
966 size_t size = sizeof(ExprWithCleanups)
967 + objects.size() * sizeof(CleanupObject);
968 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
969 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000970}
971
John McCall28fc7092011-11-10 05:35:25 +0000972ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
973 : Expr(ExprWithCleanupsClass, empty) {
974 ExprWithCleanupsBits.NumObjects = numObjects;
975}
Chris Lattnercba86142010-05-10 00:25:06 +0000976
John McCall28fc7092011-11-10 05:35:25 +0000977ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
978 unsigned numObjects) {
979 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
980 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
981 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000982}
983
Douglas Gregor2b88c112010-09-08 00:15:04 +0000984CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000985 SourceLocation LParenLoc,
986 Expr **Args,
987 unsigned NumArgs,
988 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000989 : Expr(CXXUnresolvedConstructExprClass,
990 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000991 (Type->getType()->isLValueReferenceType() ? VK_LValue
992 :Type->getType()->isRValueReferenceType()? VK_XValue
993 :VK_RValue),
994 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000995 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000996 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +0000997 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +0000998 LParenLoc(LParenLoc),
999 RParenLoc(RParenLoc),
1000 NumArgs(NumArgs) {
1001 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001002 for (unsigned I = 0; I != NumArgs; ++I) {
1003 if (Args[I]->containsUnexpandedParameterPack())
1004 ExprBits.ContainsUnexpandedParameterPack = true;
1005
1006 StoredArgs[I] = Args[I];
1007 }
Douglas Gregorce934142009-05-20 18:46:25 +00001008}
1009
1010CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001011CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001012 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001013 SourceLocation LParenLoc,
1014 Expr **Args,
1015 unsigned NumArgs,
1016 SourceLocation RParenLoc) {
1017 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1018 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001019 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +00001020 Args, NumArgs, RParenLoc);
1021}
1022
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001023CXXUnresolvedConstructExpr *
1024CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1025 Stmt::EmptyShell Empty;
1026 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1027 sizeof(Expr *) * NumArgs);
1028 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1029}
1030
Douglas Gregor2b88c112010-09-08 00:15:04 +00001031SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1032 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1033}
1034
John McCall8cd78132009-11-19 22:55:06 +00001035CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001036 Expr *Base, QualType BaseType,
1037 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001038 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001039 NestedNameSpecifierLoc QualifierLoc,
1040 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001041 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001042 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001043 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001044 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001045 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001046 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001047 (QualifierLoc &&
1048 QualifierLoc.getNestedNameSpecifier()
1049 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001050 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001051 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001052 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001053 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001054 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001055 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001056 if (TemplateArgs) {
1057 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001058 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001059 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001060 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1061 Dependent,
1062 InstantiationDependent,
1063 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001064 if (ContainsUnexpandedParameterPack)
1065 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001066 } else if (TemplateKWLoc.isValid()) {
1067 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001068 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001069}
1070
Douglas Gregora6e053e2010-12-15 01:34:56 +00001071CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1072 Expr *Base, QualType BaseType,
1073 bool IsArrow,
1074 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001075 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001076 NamedDecl *FirstQualifierFoundInScope,
1077 DeclarationNameInfo MemberNameInfo)
1078 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001079 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001080 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001081 (QualifierLoc &&
1082 QualifierLoc.getNestedNameSpecifier()->
1083 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001084 MemberNameInfo.containsUnexpandedParameterPack())),
1085 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001086 HasTemplateKWAndArgsInfo(false),
1087 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001088 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1089 MemberNameInfo(MemberNameInfo) { }
1090
John McCall8cd78132009-11-19 22:55:06 +00001091CXXDependentScopeMemberExpr *
1092CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001093 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001094 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001095 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001096 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001097 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001098 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001099 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001100 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001101 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1102 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001103 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001104 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001105 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001106
Abramo Bagnara7945c982012-01-27 09:46:47 +00001107 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1108 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1109 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001110
Chris Lattner5c0b4052010-10-30 05:14:06 +00001111 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001112 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1113 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001114 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001115 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001116 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001117 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001118}
1119
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001120CXXDependentScopeMemberExpr *
1121CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001122 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001123 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001124 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001125 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001126 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001127 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001128 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001129
1130 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001131 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001132 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001133 CXXDependentScopeMemberExpr *E
1134 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001135 0, SourceLocation(),
1136 NestedNameSpecifierLoc(),
1137 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001138 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001139 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001140 return E;
1141}
1142
Douglas Gregor0da1d432011-02-28 20:01:57 +00001143bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1144 if (Base == 0)
1145 return true;
1146
Douglas Gregor25b7e052011-03-02 21:06:53 +00001147 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001148}
1149
John McCall0009fcc2011-04-26 20:42:42 +00001150static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1151 UnresolvedSetIterator end) {
1152 do {
1153 NamedDecl *decl = *begin;
1154 if (isa<UnresolvedUsingValueDecl>(decl))
1155 return false;
1156 if (isa<UsingShadowDecl>(decl))
1157 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1158
1159 // Unresolved member expressions should only contain methods and
1160 // method templates.
1161 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1162
1163 if (isa<FunctionTemplateDecl>(decl))
1164 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1165 if (cast<CXXMethodDecl>(decl)->isStatic())
1166 return false;
1167 } while (++begin != end);
1168
1169 return true;
1170}
1171
Douglas Gregora6e053e2010-12-15 01:34:56 +00001172UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001173 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001174 Expr *Base, QualType BaseType,
1175 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001176 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001177 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001178 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001179 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001180 const TemplateArgumentListInfo *TemplateArgs,
1181 UnresolvedSetIterator Begin,
1182 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001183 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1184 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001185 // Dependent
1186 ((Base && Base->isTypeDependent()) ||
1187 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001188 ((Base && Base->isInstantiationDependent()) ||
1189 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001190 // Contains unexpanded parameter pack
1191 ((Base && Base->containsUnexpandedParameterPack()) ||
1192 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001193 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1194 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001195
1196 // Check whether all of the members are non-static member functions,
1197 // and if so, mark give this bound-member type instead of overload type.
1198 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1199 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001200}
1201
Douglas Gregor0da1d432011-02-28 20:01:57 +00001202bool UnresolvedMemberExpr::isImplicitAccess() const {
1203 if (Base == 0)
1204 return true;
1205
Douglas Gregor25b7e052011-03-02 21:06:53 +00001206 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001207}
1208
John McCall10eae182009-11-30 22:42:35 +00001209UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001210UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001211 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001212 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001213 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001214 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001215 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001216 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001217 const TemplateArgumentListInfo *TemplateArgs,
1218 UnresolvedSetIterator Begin,
1219 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001220 std::size_t size = sizeof(UnresolvedMemberExpr);
1221 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001222 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1223 else if (TemplateKWLoc.isValid())
1224 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001225
Chris Lattner5c0b4052010-10-30 05:14:06 +00001226 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001227 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001228 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001229 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001230 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001231}
1232
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001233UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001234UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001235 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001236 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001237 if (HasTemplateKWAndArgsInfo)
1238 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001239
Chris Lattner5c0b4052010-10-30 05:14:06 +00001240 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001241 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001242 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001243 return E;
1244}
1245
John McCall58cc69d2010-01-27 01:50:18 +00001246CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1247 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1248
1249 // If there was a nested name specifier, it names the naming class.
1250 // It can't be dependent: after all, we were actually able to do the
1251 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001252 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001253 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001254 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001255 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001256 Record = T->getAsCXXRecordDecl();
1257 assert(Record && "qualifier in member expression does not name record");
1258 }
John McCall58cc69d2010-01-27 01:50:18 +00001259 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001260 else {
John McCall58cc69d2010-01-27 01:50:18 +00001261 QualType BaseType = getBaseType().getNonReferenceType();
1262 if (isArrow()) {
1263 const PointerType *PT = BaseType->getAs<PointerType>();
1264 assert(PT && "base of arrow member access is not pointer");
1265 BaseType = PT->getPointeeType();
1266 }
1267
Douglas Gregor9262f472010-04-27 18:19:34 +00001268 Record = BaseType->getAsCXXRecordDecl();
1269 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001270 }
1271
Douglas Gregor9262f472010-04-27 18:19:34 +00001272 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001273}
1274
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001275SubstNonTypeTemplateParmPackExpr::
1276SubstNonTypeTemplateParmPackExpr(QualType T,
1277 NonTypeTemplateParmDecl *Param,
1278 SourceLocation NameLoc,
1279 const TemplateArgument &ArgPack)
1280 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001281 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001282 Param(Param), Arguments(ArgPack.pack_begin()),
1283 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1284
1285TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1286 return TemplateArgument(Arguments, NumArguments);
1287}
1288
Douglas Gregor29c42f22012-02-24 07:38:34 +00001289TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1290 ArrayRef<TypeSourceInfo *> Args,
1291 SourceLocation RParenLoc,
1292 bool Value)
1293 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1294 /*TypeDependent=*/false,
1295 /*ValueDependent=*/false,
1296 /*InstantiationDependent=*/false,
1297 /*ContainsUnexpandedParameterPack=*/false),
1298 Loc(Loc), RParenLoc(RParenLoc)
1299{
1300 TypeTraitExprBits.Kind = Kind;
1301 TypeTraitExprBits.Value = Value;
1302 TypeTraitExprBits.NumArgs = Args.size();
1303
1304 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1305
1306 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1307 if (Args[I]->getType()->isDependentType())
1308 setValueDependent(true);
1309 if (Args[I]->getType()->isInstantiationDependentType())
1310 setInstantiationDependent(true);
1311 if (Args[I]->getType()->containsUnexpandedParameterPack())
1312 setContainsUnexpandedParameterPack(true);
1313
1314 ToArgs[I] = Args[I];
1315 }
1316}
1317
1318TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1319 SourceLocation Loc,
1320 TypeTrait Kind,
1321 ArrayRef<TypeSourceInfo *> Args,
1322 SourceLocation RParenLoc,
1323 bool Value) {
1324 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1325 void *Mem = C.Allocate(Size);
1326 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1327}
1328
1329TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1330 unsigned NumArgs) {
1331 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1332 void *Mem = C.Allocate(Size);
1333 return new (Mem) TypeTraitExpr(EmptyShell());
1334}
1335
David Blaikie68e081d2011-12-20 02:48:34 +00001336void ArrayTypeTraitExpr::anchor() { }