blob: 3d52e4a51149d5ba057d5eb05988beef4930bcb1 [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();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000130 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000131 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000132
133 if (ArgType->isDependentType() && !ArgType->isPointerType())
134 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000135
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000136 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000137}
138
Douglas Gregorad8a3362009-09-04 17:36:40 +0000139// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000140PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
141 : Type(Info)
142{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000143 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000144}
145
John McCalldb40c7f2010-12-14 08:05:40 +0000146CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000147 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
148 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
149 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
150 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000151 : Expr(CXXPseudoDestructorExprClass,
152 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
153 FunctionProtoType::ExtProtoInfo())),
154 VK_RValue, OK_Ordinary,
155 /*isTypeDependent=*/(Base->isTypeDependent() ||
156 (DestroyedType.getTypeSourceInfo() &&
157 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000158 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000159 (Base->isInstantiationDependent() ||
160 (QualifierLoc &&
161 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
162 (ScopeType &&
163 ScopeType->getType()->isInstantiationDependentType()) ||
164 (DestroyedType.getTypeSourceInfo() &&
165 DestroyedType.getTypeSourceInfo()->getType()
166 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000167 // ContainsUnexpandedParameterPack
168 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000169 (QualifierLoc &&
170 QualifierLoc.getNestedNameSpecifier()
171 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000172 (ScopeType &&
173 ScopeType->getType()->containsUnexpandedParameterPack()) ||
174 (DestroyedType.getTypeSourceInfo() &&
175 DestroyedType.getTypeSourceInfo()->getType()
176 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000177 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000178 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000179 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
180 DestroyedType(DestroyedType) { }
181
Douglas Gregor678f90d2010-02-25 01:56:36 +0000182QualType CXXPseudoDestructorExpr::getDestroyedType() const {
183 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
184 return TInfo->getType();
185
186 return QualType();
187}
188
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000189SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000190 SourceLocation End = DestroyedType.getLocation();
191 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000192 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000193 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000194}
195
John McCalld14a8642009-11-21 08:51:07 +0000196// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000197UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000198UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000199 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000200 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000201 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000202 const DeclarationNameInfo &NameInfo,
203 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000204 const TemplateArgumentListInfo *Args,
205 UnresolvedSetIterator Begin,
206 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000207{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000208 assert(Args || TemplateKWLoc.isValid());
209 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000210 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000211 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000212 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
213 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000214 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000215 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000216}
217
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000218UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000219UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
220 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000221 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000222 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000223 if (HasTemplateKWAndArgsInfo)
224 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000225
Chris Lattner5c0b4052010-10-30 05:14:06 +0000226 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000227 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000228 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000229 return E;
230}
231
Douglas Gregora6e053e2010-12-15 01:34:56 +0000232OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000233 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000234 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000235 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000236 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000237 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000238 UnresolvedSetIterator End,
239 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000240 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000241 bool KnownContainsUnexpandedParameterPack)
242 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
243 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 (KnownInstantiationDependent ||
245 NameInfo.isInstantiationDependent() ||
246 (QualifierLoc &&
247 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248 (KnownContainsUnexpandedParameterPack ||
249 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000250 (QualifierLoc &&
251 QualifierLoc.getNestedNameSpecifier()
252 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000253 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
254 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000255 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000256{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000257 NumResults = End - Begin;
258 if (NumResults) {
259 // Determine whether this expression is type-dependent.
260 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
261 if ((*I)->getDeclContext()->isDependentContext() ||
262 isa<UnresolvedUsingValueDecl>(*I)) {
263 ExprBits.TypeDependent = true;
264 ExprBits.ValueDependent = true;
265 }
266 }
267
268 Results = static_cast<DeclAccessPair *>(
269 C.Allocate(sizeof(DeclAccessPair) * NumResults,
270 llvm::alignOf<DeclAccessPair>()));
271 memcpy(Results, &*Begin.getIterator(),
272 NumResults * sizeof(DeclAccessPair));
273 }
274
275 // If we have explicit template arguments, check for dependent
276 // template arguments and whether they contain any unexpanded pack
277 // expansions.
278 if (TemplateArgs) {
279 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000280 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000281 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000282 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
283 Dependent,
284 InstantiationDependent,
285 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000286
287 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 ExprBits.TypeDependent = true;
289 ExprBits.ValueDependent = true;
290 }
291 if (InstantiationDependent)
292 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000293 if (ContainsUnexpandedParameterPack)
294 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000295 } else if (TemplateKWLoc.isValid()) {
296 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000297 }
298
299 if (isTypeDependent())
300 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000301}
302
303void OverloadExpr::initializeResults(ASTContext &C,
304 UnresolvedSetIterator Begin,
305 UnresolvedSetIterator End) {
306 assert(Results == 0 && "Results already initialized!");
307 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000308 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000309 Results = static_cast<DeclAccessPair *>(
310 C.Allocate(sizeof(DeclAccessPair) * NumResults,
311
312 llvm::alignOf<DeclAccessPair>()));
313 memcpy(Results, &*Begin.getIterator(),
314 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000315 }
316}
317
John McCall8c12dc42010-04-22 18:44:12 +0000318CXXRecordDecl *OverloadExpr::getNamingClass() const {
319 if (isa<UnresolvedLookupExpr>(this))
320 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
321 else
322 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
323}
324
John McCall8cd78132009-11-19 22:55:06 +0000325// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000327 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000328 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000329 const DeclarationNameInfo &NameInfo,
330 const TemplateArgumentListInfo *Args)
331 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
332 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000333 (NameInfo.isInstantiationDependent() ||
334 (QualifierLoc &&
335 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000336 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000337 (QualifierLoc &&
338 QualifierLoc.getNestedNameSpecifier()
339 ->containsUnexpandedParameterPack()))),
340 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000341 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000342{
343 if (Args) {
344 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000345 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000346 bool ContainsUnexpandedParameterPack
347 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000348 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
349 Dependent,
350 InstantiationDependent,
351 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000352 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000353 } else if (TemplateKWLoc.isValid()) {
354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000355 }
356}
357
John McCalle66edc12009-11-24 19:00:30 +0000358DependentScopeDeclRefExpr *
359DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000360 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000361 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000362 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000363 const TemplateArgumentListInfo *Args) {
364 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000365 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000366 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
367 else if (TemplateKWLoc.isValid())
368 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000369 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000370 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
371 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000372}
373
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000374DependentScopeDeclRefExpr *
375DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000376 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000377 unsigned NumTemplateArgs) {
378 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000379 if (HasTemplateKWAndArgsInfo)
380 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000381 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000382 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000383 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000384 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000385 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000387 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000388}
389
Chandler Carruth01718152010-10-25 08:47:36 +0000390SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000391 if (isa<CXXTemporaryObjectExpr>(this))
392 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
393
Douglas Gregor15417cf2010-11-03 00:35:38 +0000394 if (ParenRange.isValid())
395 return SourceRange(Loc, ParenRange.getEnd());
396
397 SourceLocation End = Loc;
398 for (unsigned I = getNumArgs(); I > 0; --I) {
399 const Expr *Arg = getArg(I-1);
400 if (!Arg->isDefaultArgument()) {
401 SourceLocation NewEnd = Arg->getLocEnd();
402 if (NewEnd.isValid()) {
403 End = NewEnd;
404 break;
405 }
406 }
407 }
408
409 return SourceRange(Loc, End);
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000410}
411
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000412SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000413 OverloadedOperatorKind Kind = getOperator();
414 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
415 if (getNumArgs() == 1)
416 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000417 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000418 else
419 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000420 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000421 } else if (Kind == OO_Arrow) {
422 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000423 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000424 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000425 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000426 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000427 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000428 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000429 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000430 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000431 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000432 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000433 }
434}
435
Ted Kremenek98a24e32011-03-30 17:41:19 +0000436Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000437 const Expr *Callee = getCallee()->IgnoreParens();
438 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000439 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000440 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
441 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
442 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000443
444 // FIXME: Will eventually need to cope with member pointers.
445 return 0;
446}
447
Ted Kremenek98a24e32011-03-30 17:41:19 +0000448CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
449 if (const MemberExpr *MemExpr =
450 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
451 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
452
453 // FIXME: Will eventually need to cope with member pointers.
454 return 0;
455}
456
457
David Blaikiec0f58662012-05-03 16:25:49 +0000458CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000459 Expr* ThisArg = getImplicitObjectArgument();
460 if (!ThisArg)
461 return 0;
462
463 if (ThisArg->getType()->isAnyPointerType())
464 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
465
466 return ThisArg->getType()->getAsCXXRecordDecl();
467}
468
Douglas Gregoref986e82009-11-12 15:31:47 +0000469
Douglas Gregore200adc2008-10-27 19:41:14 +0000470//===----------------------------------------------------------------------===//
471// Named casts
472//===----------------------------------------------------------------------===//
473
474/// getCastName - Get the name of the C++ cast being used, e.g.,
475/// "static_cast", "dynamic_cast", "reinterpret_cast", or
476/// "const_cast". The returned pointer must not be freed.
477const char *CXXNamedCastExpr::getCastName() const {
478 switch (getStmtClass()) {
479 case CXXStaticCastExprClass: return "static_cast";
480 case CXXDynamicCastExprClass: return "dynamic_cast";
481 case CXXReinterpretCastExprClass: return "reinterpret_cast";
482 case CXXConstCastExprClass: return "const_cast";
483 default: return "<invalid cast>";
484 }
485}
Douglas Gregordd04d332009-01-16 18:33:17 +0000486
John McCallcf142162010-08-07 06:22:56 +0000487CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000488 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000489 CastKind K, Expr *Op,
490 const CXXCastPath *BasePath,
491 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000492 SourceLocation L,
493 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000494 unsigned PathSize = (BasePath ? BasePath->size() : 0);
495 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
496 + PathSize * sizeof(CXXBaseSpecifier*));
497 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000498 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
499 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000500 if (PathSize) E->setCastPath(*BasePath);
501 return E;
502}
503
504CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
505 unsigned PathSize) {
506 void *Buffer =
507 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
508 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
509}
510
511CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000512 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000513 CastKind K, Expr *Op,
514 const CXXCastPath *BasePath,
515 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000516 SourceLocation L,
517 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000518 unsigned PathSize = (BasePath ? BasePath->size() : 0);
519 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
520 + PathSize * sizeof(CXXBaseSpecifier*));
521 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000522 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
523 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000524 if (PathSize) E->setCastPath(*BasePath);
525 return E;
526}
527
528CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
529 unsigned PathSize) {
530 void *Buffer =
531 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
532 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
533}
534
Anders Carlsson267c0c92011-04-11 01:43:55 +0000535/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
536/// to always be null. For example:
537///
538/// struct A { };
539/// struct B final : A { };
540/// struct C { };
541///
542/// C *f(B* b) { return dynamic_cast<C*>(b); }
543bool CXXDynamicCastExpr::isAlwaysNull() const
544{
545 QualType SrcType = getSubExpr()->getType();
546 QualType DestType = getType();
547
548 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
549 SrcType = SrcPTy->getPointeeType();
550 DestType = DestType->castAs<PointerType>()->getPointeeType();
551 }
552
Alexis Hunt78e2b912012-06-19 23:44:55 +0000553 if (DestType->isVoidType())
554 return false;
555
Anders Carlsson267c0c92011-04-11 01:43:55 +0000556 const CXXRecordDecl *SrcRD =
557 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
558
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000559 if (!SrcRD->hasAttr<FinalAttr>())
560 return false;
561
Anders Carlsson267c0c92011-04-11 01:43:55 +0000562 const CXXRecordDecl *DestRD =
563 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
564
565 return !DestRD->isDerivedFrom(SrcRD);
566}
567
John McCallcf142162010-08-07 06:22:56 +0000568CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000569CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
570 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000571 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000572 TypeSourceInfo *WrittenTy, SourceLocation L,
573 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000574 unsigned PathSize = (BasePath ? BasePath->size() : 0);
575 void *Buffer =
576 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
577 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000578 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
579 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000580 if (PathSize) E->setCastPath(*BasePath);
581 return E;
582}
583
584CXXReinterpretCastExpr *
585CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
586 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
587 + PathSize * sizeof(CXXBaseSpecifier*));
588 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
589}
590
John McCall7decc9e2010-11-18 06:31:45 +0000591CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
592 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000593 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000594 SourceLocation L,
595 SourceLocation RParenLoc) {
596 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000597}
598
599CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
600 return new (C) CXXConstCastExpr(EmptyShell());
601}
602
603CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000604CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000605 TypeSourceInfo *Written, SourceLocation L,
606 CastKind K, Expr *Op, const CXXCastPath *BasePath,
607 SourceLocation R) {
608 unsigned PathSize = (BasePath ? BasePath->size() : 0);
609 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
610 + PathSize * sizeof(CXXBaseSpecifier*));
611 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000612 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000613 if (PathSize) E->setCastPath(*BasePath);
614 return E;
615}
616
617CXXFunctionalCastExpr *
618CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
619 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
620 + PathSize * sizeof(CXXBaseSpecifier*));
621 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
622}
623
Richard Smithc67fdd42012-03-07 08:35:16 +0000624UserDefinedLiteral::LiteralOperatorKind
625UserDefinedLiteral::getLiteralOperatorKind() const {
626 if (getNumArgs() == 0)
627 return LOK_Template;
628 if (getNumArgs() == 2)
629 return LOK_String;
630
631 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
632 QualType ParamTy =
633 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
634 if (ParamTy->isPointerType())
635 return LOK_Raw;
636 if (ParamTy->isAnyCharacterType())
637 return LOK_Character;
638 if (ParamTy->isIntegerType())
639 return LOK_Integer;
640 if (ParamTy->isFloatingType())
641 return LOK_Floating;
642
643 llvm_unreachable("unknown kind of literal operator");
644}
645
646Expr *UserDefinedLiteral::getCookedLiteral() {
647#ifndef NDEBUG
648 LiteralOperatorKind LOK = getLiteralOperatorKind();
649 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
650#endif
651 return getArg(0);
652}
653
654const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
655 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
656}
John McCallcf142162010-08-07 06:22:56 +0000657
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000658CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000659CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
660 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000661 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000662 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
663 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000664}
665
Mike Stump11289f42009-09-09 15:08:12 +0000666CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000667 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000668 return new (C) CXXTemporary(Destructor);
669}
670
Mike Stump11289f42009-09-09 15:08:12 +0000671CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000672 CXXTemporary *Temp,
673 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000674 assert((SubExpr->getType()->isRecordType() ||
675 SubExpr->getType()->isArrayType()) &&
676 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000677
Douglas Gregora6e053e2010-12-15 01:34:56 +0000678 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000679}
680
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000681CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000682 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000683 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000684 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000685 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000686 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000687 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000688 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000689 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
690 Type->getType().getNonReferenceType(),
691 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000692 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000693 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000694 CXXConstructExpr::CK_Complete, parenRange),
695 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000696}
697
698SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000699 return SourceRange(Type->getTypeLoc().getBeginLoc(),
700 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000701}
Anders Carlsson6f287832009-04-21 02:22:11 +0000702
Mike Stump11289f42009-09-09 15:08:12 +0000703CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000704 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000705 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000706 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000707 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000708 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000709 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000710 ConstructionKind ConstructKind,
711 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000712 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000713 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000714 HadMultipleCandidates, ListInitialization,
715 ZeroInitialization, ConstructKind,
716 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000717}
718
Mike Stump11289f42009-09-09 15:08:12 +0000719CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000720 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000721 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000722 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000723 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000724 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000725 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000726 ConstructionKind ConstructKind,
727 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000728 : Expr(SC, T, VK_RValue, OK_Ordinary,
729 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000730 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000731 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000732 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000733 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000734 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000735 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000736 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000737{
738 if (NumArgs) {
739 Args = new (C) Stmt*[NumArgs];
740
741 for (unsigned i = 0; i != NumArgs; ++i) {
742 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000743
744 if (args[i]->isValueDependent())
745 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000746 if (args[i]->isInstantiationDependent())
747 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000748 if (args[i]->containsUnexpandedParameterPack())
749 ExprBits.ContainsUnexpandedParameterPack = true;
750
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000751 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000752 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000753 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000754}
755
Douglas Gregore31e6062012-02-07 10:09:13 +0000756LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
757 LambdaCaptureKind Kind, VarDecl *Var,
758 SourceLocation EllipsisLoc)
759 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
760{
761 unsigned Bits = 0;
762 if (Implicit)
763 Bits |= Capture_Implicit;
764
765 switch (Kind) {
766 case LCK_This:
767 assert(Var == 0 && "'this' capture cannot have a variable!");
768 break;
769
770 case LCK_ByCopy:
771 Bits |= Capture_ByCopy;
772 // Fall through
773 case LCK_ByRef:
774 assert(Var && "capture must have a variable!");
775 break;
776 }
777 VarAndBits.setInt(Bits);
778}
779
780LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
781 if (capturesThis())
782 return LCK_This;
783
784 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
785}
786
787LambdaExpr::LambdaExpr(QualType T,
788 SourceRange IntroducerRange,
789 LambdaCaptureDefault CaptureDefault,
790 ArrayRef<Capture> Captures,
791 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000792 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000793 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000794 ArrayRef<VarDecl *> ArrayIndexVars,
795 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000796 SourceLocation ClosingBrace,
797 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000798 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
799 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000800 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000801 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000802 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000803 CaptureDefault(CaptureDefault),
804 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000805 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000806 ClosingBrace(ClosingBrace)
807{
808 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000809 CXXRecordDecl *Class = getLambdaClass();
810 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000811
812 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000813
814 // Copy captures.
815 ASTContext &Context = Class->getASTContext();
816 Data.NumCaptures = NumCaptures;
817 Data.NumExplicitCaptures = 0;
818 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
819 Capture *ToCapture = Data.Captures;
820 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
821 if (Captures[I].isExplicit())
822 ++Data.NumExplicitCaptures;
823
824 *ToCapture++ = Captures[I];
825 }
826
827 // Copy initialization expressions for the non-static data members.
828 Stmt **Stored = getStoredStmts();
829 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
830 *Stored++ = CaptureInits[I];
831
832 // Copy the body of the lambda.
833 *Stored++ = getCallOperator()->getBody();
834
835 // Copy the array index variables, if any.
836 HasArrayIndexVars = !ArrayIndexVars.empty();
837 if (HasArrayIndexVars) {
838 assert(ArrayIndexStarts.size() == NumCaptures);
839 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
840 sizeof(VarDecl *) * ArrayIndexVars.size());
841 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
842 sizeof(unsigned) * Captures.size());
843 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000844 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000845}
846
847LambdaExpr *LambdaExpr::Create(ASTContext &Context,
848 CXXRecordDecl *Class,
849 SourceRange IntroducerRange,
850 LambdaCaptureDefault CaptureDefault,
851 ArrayRef<Capture> Captures,
852 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000853 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000854 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000855 ArrayRef<VarDecl *> ArrayIndexVars,
856 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000857 SourceLocation ClosingBrace,
858 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000859 // Determine the type of the expression (i.e., the type of the
860 // function object we're creating).
861 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000862
Douglas Gregore5561632012-02-13 17:20:40 +0000863 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
864 if (!ArrayIndexVars.empty())
865 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
866 + sizeof(unsigned) * (Captures.size() + 1);
867 void *Mem = Context.Allocate(Size);
868 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000869 Captures, ExplicitParams, ExplicitResultType,
870 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000871 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000872}
873
Douglas Gregor99ae8062012-02-14 17:54:36 +0000874LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
875 unsigned NumArrayIndexVars) {
876 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
877 if (NumArrayIndexVars)
878 Size += sizeof(VarDecl) * NumArrayIndexVars
879 + sizeof(unsigned) * (NumCaptures + 1);
880 void *Mem = C.Allocate(Size);
881 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
882}
883
Douglas Gregorc8a73492012-02-13 15:44:47 +0000884LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000885 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000886}
887
888LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000889 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000890}
891
892LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
893 return capture_begin();
894}
895
896LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
897 struct CXXRecordDecl::LambdaDefinitionData &Data
898 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000899 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000900}
901
902LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
903 return explicit_capture_end();
904}
905
906LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
907 return capture_end();
908}
909
Douglas Gregor54fcea62012-02-13 16:35:30 +0000910ArrayRef<VarDecl *>
911LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000912 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000913
914 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000915 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
916 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000917 VarDecl **IndexVars = getArrayIndexVars();
918 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000919 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
920 IndexVars + IndexStarts[Index + 1]);
921}
922
Douglas Gregore31e6062012-02-07 10:09:13 +0000923CXXRecordDecl *LambdaExpr::getLambdaClass() const {
924 return getType()->getAsCXXRecordDecl();
925}
926
927CXXMethodDecl *LambdaExpr::getCallOperator() const {
928 CXXRecordDecl *Record = getLambdaClass();
929 DeclarationName Name
930 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
931 DeclContext::lookup_result Calls = Record->lookup(Name);
932 assert(Calls.first != Calls.second && "Missing lambda call operator!");
933 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
934 assert(Calls.first == Calls.second && "More than lambda one call operator?");
935 return Result;
936}
937
Douglas Gregor99ae8062012-02-14 17:54:36 +0000938CompoundStmt *LambdaExpr::getBody() const {
939 if (!getStoredStmts()[NumCaptures])
940 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
941
942 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
943}
944
Douglas Gregore31e6062012-02-07 10:09:13 +0000945bool LambdaExpr::isMutable() const {
946 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
947}
948
John McCall28fc7092011-11-10 05:35:25 +0000949ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
950 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000951 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000952 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000953 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000954 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000955 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000956 SubExpr(subexpr) {
957 ExprWithCleanupsBits.NumObjects = objects.size();
958 for (unsigned i = 0, e = objects.size(); i != e; ++i)
959 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000960}
961
John McCall28fc7092011-11-10 05:35:25 +0000962ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
963 ArrayRef<CleanupObject> objects) {
964 size_t size = sizeof(ExprWithCleanups)
965 + objects.size() * sizeof(CleanupObject);
966 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
967 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000968}
969
John McCall28fc7092011-11-10 05:35:25 +0000970ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
971 : Expr(ExprWithCleanupsClass, empty) {
972 ExprWithCleanupsBits.NumObjects = numObjects;
973}
Chris Lattnercba86142010-05-10 00:25:06 +0000974
John McCall28fc7092011-11-10 05:35:25 +0000975ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
976 unsigned numObjects) {
977 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
978 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
979 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000980}
981
Douglas Gregor2b88c112010-09-08 00:15:04 +0000982CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000983 SourceLocation LParenLoc,
984 Expr **Args,
985 unsigned NumArgs,
986 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000987 : Expr(CXXUnresolvedConstructExprClass,
988 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000989 (Type->getType()->isLValueReferenceType() ? VK_LValue
990 :Type->getType()->isRValueReferenceType()? VK_XValue
991 :VK_RValue),
992 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000993 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000994 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +0000995 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +0000996 LParenLoc(LParenLoc),
997 RParenLoc(RParenLoc),
998 NumArgs(NumArgs) {
999 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001000 for (unsigned I = 0; I != NumArgs; ++I) {
1001 if (Args[I]->containsUnexpandedParameterPack())
1002 ExprBits.ContainsUnexpandedParameterPack = true;
1003
1004 StoredArgs[I] = Args[I];
1005 }
Douglas Gregorce934142009-05-20 18:46:25 +00001006}
1007
1008CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001009CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001010 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001011 SourceLocation LParenLoc,
1012 Expr **Args,
1013 unsigned NumArgs,
1014 SourceLocation RParenLoc) {
1015 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1016 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001017 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +00001018 Args, NumArgs, RParenLoc);
1019}
1020
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001021CXXUnresolvedConstructExpr *
1022CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1023 Stmt::EmptyShell Empty;
1024 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1025 sizeof(Expr *) * NumArgs);
1026 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1027}
1028
Douglas Gregor2b88c112010-09-08 00:15:04 +00001029SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1030 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1031}
1032
John McCall8cd78132009-11-19 22:55:06 +00001033CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001034 Expr *Base, QualType BaseType,
1035 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001036 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001037 NestedNameSpecifierLoc QualifierLoc,
1038 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001039 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001040 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001041 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001042 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001043 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001044 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001045 (QualifierLoc &&
1046 QualifierLoc.getNestedNameSpecifier()
1047 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001048 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001049 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001050 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001051 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001052 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001053 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001054 if (TemplateArgs) {
1055 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001056 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001057 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001058 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1059 Dependent,
1060 InstantiationDependent,
1061 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001062 if (ContainsUnexpandedParameterPack)
1063 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001064 } else if (TemplateKWLoc.isValid()) {
1065 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001066 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001067}
1068
Douglas Gregora6e053e2010-12-15 01:34:56 +00001069CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1070 Expr *Base, QualType BaseType,
1071 bool IsArrow,
1072 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001073 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001074 NamedDecl *FirstQualifierFoundInScope,
1075 DeclarationNameInfo MemberNameInfo)
1076 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001077 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001078 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001079 (QualifierLoc &&
1080 QualifierLoc.getNestedNameSpecifier()->
1081 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001082 MemberNameInfo.containsUnexpandedParameterPack())),
1083 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001084 HasTemplateKWAndArgsInfo(false),
1085 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001086 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1087 MemberNameInfo(MemberNameInfo) { }
1088
John McCall8cd78132009-11-19 22:55:06 +00001089CXXDependentScopeMemberExpr *
1090CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001091 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001092 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001093 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001094 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001095 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001096 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001097 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001098 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001099 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1100 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001101 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001102 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001103 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001104
Abramo Bagnara7945c982012-01-27 09:46:47 +00001105 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1106 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1107 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001108
Chris Lattner5c0b4052010-10-30 05:14:06 +00001109 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001110 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1111 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001112 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001113 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001114 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001115 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001116}
1117
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001118CXXDependentScopeMemberExpr *
1119CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001120 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001121 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001122 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001123 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001124 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001125 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001126 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001127
1128 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001129 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001130 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001131 CXXDependentScopeMemberExpr *E
1132 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001133 0, SourceLocation(),
1134 NestedNameSpecifierLoc(),
1135 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001136 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001137 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001138 return E;
1139}
1140
Douglas Gregor0da1d432011-02-28 20:01:57 +00001141bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1142 if (Base == 0)
1143 return true;
1144
Douglas Gregor25b7e052011-03-02 21:06:53 +00001145 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001146}
1147
John McCall0009fcc2011-04-26 20:42:42 +00001148static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1149 UnresolvedSetIterator end) {
1150 do {
1151 NamedDecl *decl = *begin;
1152 if (isa<UnresolvedUsingValueDecl>(decl))
1153 return false;
1154 if (isa<UsingShadowDecl>(decl))
1155 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1156
1157 // Unresolved member expressions should only contain methods and
1158 // method templates.
1159 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1160
1161 if (isa<FunctionTemplateDecl>(decl))
1162 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1163 if (cast<CXXMethodDecl>(decl)->isStatic())
1164 return false;
1165 } while (++begin != end);
1166
1167 return true;
1168}
1169
Douglas Gregora6e053e2010-12-15 01:34:56 +00001170UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001171 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001172 Expr *Base, QualType BaseType,
1173 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001174 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001175 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001176 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001177 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001178 const TemplateArgumentListInfo *TemplateArgs,
1179 UnresolvedSetIterator Begin,
1180 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001181 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1182 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001183 // Dependent
1184 ((Base && Base->isTypeDependent()) ||
1185 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001186 ((Base && Base->isInstantiationDependent()) ||
1187 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001188 // Contains unexpanded parameter pack
1189 ((Base && Base->containsUnexpandedParameterPack()) ||
1190 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001191 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1192 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001193
1194 // Check whether all of the members are non-static member functions,
1195 // and if so, mark give this bound-member type instead of overload type.
1196 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1197 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001198}
1199
Douglas Gregor0da1d432011-02-28 20:01:57 +00001200bool UnresolvedMemberExpr::isImplicitAccess() const {
1201 if (Base == 0)
1202 return true;
1203
Douglas Gregor25b7e052011-03-02 21:06:53 +00001204 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001205}
1206
John McCall10eae182009-11-30 22:42:35 +00001207UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001208UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001209 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001210 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001211 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001212 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001213 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001214 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001215 const TemplateArgumentListInfo *TemplateArgs,
1216 UnresolvedSetIterator Begin,
1217 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001218 std::size_t size = sizeof(UnresolvedMemberExpr);
1219 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001220 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1221 else if (TemplateKWLoc.isValid())
1222 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001223
Chris Lattner5c0b4052010-10-30 05:14:06 +00001224 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001225 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001226 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001227 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001228 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001229}
1230
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001231UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001232UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001233 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001234 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001235 if (HasTemplateKWAndArgsInfo)
1236 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001237
Chris Lattner5c0b4052010-10-30 05:14:06 +00001238 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001239 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001240 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001241 return E;
1242}
1243
John McCall58cc69d2010-01-27 01:50:18 +00001244CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1245 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1246
1247 // If there was a nested name specifier, it names the naming class.
1248 // It can't be dependent: after all, we were actually able to do the
1249 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001250 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001251 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001252 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001253 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001254 Record = T->getAsCXXRecordDecl();
1255 assert(Record && "qualifier in member expression does not name record");
1256 }
John McCall58cc69d2010-01-27 01:50:18 +00001257 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001258 else {
John McCall58cc69d2010-01-27 01:50:18 +00001259 QualType BaseType = getBaseType().getNonReferenceType();
1260 if (isArrow()) {
1261 const PointerType *PT = BaseType->getAs<PointerType>();
1262 assert(PT && "base of arrow member access is not pointer");
1263 BaseType = PT->getPointeeType();
1264 }
1265
Douglas Gregor9262f472010-04-27 18:19:34 +00001266 Record = BaseType->getAsCXXRecordDecl();
1267 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001268 }
1269
Douglas Gregor9262f472010-04-27 18:19:34 +00001270 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001271}
1272
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001273SubstNonTypeTemplateParmPackExpr::
1274SubstNonTypeTemplateParmPackExpr(QualType T,
1275 NonTypeTemplateParmDecl *Param,
1276 SourceLocation NameLoc,
1277 const TemplateArgument &ArgPack)
1278 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001279 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001280 Param(Param), Arguments(ArgPack.pack_begin()),
1281 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1282
1283TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1284 return TemplateArgument(Arguments, NumArguments);
1285}
1286
Douglas Gregor29c42f22012-02-24 07:38:34 +00001287TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1288 ArrayRef<TypeSourceInfo *> Args,
1289 SourceLocation RParenLoc,
1290 bool Value)
1291 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1292 /*TypeDependent=*/false,
1293 /*ValueDependent=*/false,
1294 /*InstantiationDependent=*/false,
1295 /*ContainsUnexpandedParameterPack=*/false),
1296 Loc(Loc), RParenLoc(RParenLoc)
1297{
1298 TypeTraitExprBits.Kind = Kind;
1299 TypeTraitExprBits.Value = Value;
1300 TypeTraitExprBits.NumArgs = Args.size();
1301
1302 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1303
1304 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1305 if (Args[I]->getType()->isDependentType())
1306 setValueDependent(true);
1307 if (Args[I]->getType()->isInstantiationDependentType())
1308 setInstantiationDependent(true);
1309 if (Args[I]->getType()->containsUnexpandedParameterPack())
1310 setContainsUnexpandedParameterPack(true);
1311
1312 ToArgs[I] = Args[I];
1313 }
1314}
1315
1316TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1317 SourceLocation Loc,
1318 TypeTrait Kind,
1319 ArrayRef<TypeSourceInfo *> Args,
1320 SourceLocation RParenLoc,
1321 bool Value) {
1322 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1323 void *Mem = C.Allocate(Size);
1324 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1325}
1326
1327TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1328 unsigned NumArgs) {
1329 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1330 void *Mem = C.Allocate(Size);
1331 return new (Mem) TypeTraitExpr(EmptyShell());
1332}
1333
David Blaikie68e081d2011-12-20 02:48:34 +00001334void ArrayTypeTraitExpr::anchor() { }