blob: d87c60a733b1c9f321e871736fbcea0772972c3b [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
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.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"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/Basic/IdentifierTable.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000021using namespace clang;
22
Douglas Gregor9da64192010-04-26 22:37:10 +000023
Ted Kremeneke3a0c142007-08-24 20:21:10 +000024//===----------------------------------------------------------------------===//
25// Child Iterators for iterating over subexpressions/substatements
26//===----------------------------------------------------------------------===//
27
Richard Smithef8bf432012-08-13 20:08:14 +000028bool CXXTypeidExpr::isPotentiallyEvaluated() const {
29 if (isTypeOperand())
30 return false;
31
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
38 return true;
39
40 return false;
41}
42
Douglas Gregor9da64192010-04-26 22:37:10 +000043QualType CXXTypeidExpr::getTypeOperand() const {
44 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
45 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
46 .getUnqualifiedType();
47}
48
Francois Pichet9f4f2072010-09-08 12:20:18 +000049QualType CXXUuidofExpr::getTypeOperand() const {
50 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
51 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
52 .getUnqualifiedType();
53}
54
Nico Webercf4ff5862012-10-11 10:13:44 +000055// static
David Majnemer59c0ec22013-09-07 06:59:46 +000056UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
57 bool *RDHasMultipleGUIDsPtr) {
Nico Webercf4ff5862012-10-11 10:13:44 +000058 // Optionally remove one level of pointer, reference or array indirection.
59 const Type *Ty = QT.getTypePtr();
60 if (QT->isPointerType() || QT->isReferenceType())
61 Ty = QT->getPointeeType().getTypePtr();
62 else if (QT->isArrayType())
63 Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
64
65 // Loop all record redeclaration looking for an uuid attribute.
66 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
David Majnemer59c0ec22013-09-07 06:59:46 +000067 if (!RD)
68 return 0;
69
70 if (ClassTemplateSpecializationDecl *CTSD =
71 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
72 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
73 UuidAttr *UuidForRD = 0;
74
75 for (unsigned I = 0, N = TAL.size(); I != N; ++I) {
76 const TemplateArgument &TA = TAL[I];
77 bool SeenMultipleGUIDs = false;
78
79 UuidAttr *UuidForTA = 0;
80 if (TA.getKind() == TemplateArgument::Type)
81 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
82 else if (TA.getKind() == TemplateArgument::Declaration)
83 UuidForTA =
84 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
85
86 // If the template argument has a UUID, there are three cases:
87 // - This is the first UUID seen for this RecordDecl.
88 // - This is a different UUID than previously seed for this RecordDecl.
89 // - This is the same UUID than previously seed for this RecordDecl.
90 if (UuidForTA) {
91 if (!UuidForRD)
92 UuidForRD = UuidForTA;
93 else if (UuidForRD != UuidForTA)
94 SeenMultipleGUIDs = true;
95 }
96
97 // Seeing multiple UUIDs means that we couldn't find a UUID
98 if (SeenMultipleGUIDs) {
99 if (RDHasMultipleGUIDsPtr)
100 *RDHasMultipleGUIDsPtr = true;
101 return 0;
102 }
103 }
104
105 return UuidForRD;
106 } else
107 for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
108 E = RD->redecls_end();
109 I != E; ++I)
110 if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
111 return Uuid;
Nico Webercf4ff5862012-10-11 10:13:44 +0000112
113 return 0;
114}
115
David Majnemer8eaab6f2013-08-13 06:32:20 +0000116StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
117 StringRef Uuid;
118 if (isTypeOperand())
119 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand())->getGuid();
120 else {
121 // Special case: __uuidof(0) means an all-zero GUID.
122 Expr *Op = getExprOperand();
123 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
124 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
125 else
126 Uuid = "00000000-0000-0000-0000-000000000000";
127 }
128 return Uuid;
129}
130
Douglas Gregor747eb782010-07-08 06:14:04 +0000131// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000132SourceLocation CXXScalarValueInitExpr::getLocStart() const {
133 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000134}
135
Sebastian Redlbd150f42008-11-21 19:14:01 +0000136// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000137CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
138 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000139 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000140 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000141 SourceRange typeIdParens, Expr *arraySize,
142 InitializationStyle initializationStyle,
143 Expr *initializer, QualType ty,
144 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000145 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000146 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000147 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000148 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +0000150 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
151 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000152 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000153 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +0000154 assert((initializer != 0 || initializationStyle == NoInit) &&
155 "Only NoInit can have no initializer.");
156 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Benjamin Kramerc215e762012-08-24 11:54:20 +0000157 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000158 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000159 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000160 if (arraySize->isInstantiationDependent())
161 ExprBits.InstantiationDependent = true;
162
Douglas Gregora6e053e2010-12-15 01:34:56 +0000163 if (arraySize->containsUnexpandedParameterPack())
164 ExprBits.ContainsUnexpandedParameterPack = true;
165
Sebastian Redl351bb782008-12-02 14:43:59 +0000166 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000167 }
168
Sebastian Redl6047f072012-02-16 12:22:20 +0000169 if (initializer) {
170 if (initializer->isInstantiationDependent())
171 ExprBits.InstantiationDependent = true;
172
173 if (initializer->containsUnexpandedParameterPack())
174 ExprBits.ContainsUnexpandedParameterPack = true;
175
176 SubExprs[i++] = initializer;
177 }
178
Benjamin Kramerc215e762012-08-24 11:54:20 +0000179 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000180 if (placementArgs[j]->isInstantiationDependent())
181 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000182 if (placementArgs[j]->containsUnexpandedParameterPack())
183 ExprBits.ContainsUnexpandedParameterPack = true;
184
Sebastian Redlbd150f42008-11-21 19:14:01 +0000185 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000186 }
David Blaikie3a0de212012-11-08 22:53:48 +0000187
188 switch (getInitializationStyle()) {
189 case CallInit:
190 this->Range.setEnd(DirectInitRange.getEnd()); break;
191 case ListInit:
192 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000193 default:
194 if (TypeIdParens.isValid())
195 this->Range.setEnd(TypeIdParens.getEnd());
196 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000197 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000198}
199
Craig Toppera31a8822013-08-22 07:09:37 +0000200void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000201 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000202 assert(SubExprs == 0 && "SubExprs already allocated");
203 Array = isArray;
204 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000205
206 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000207 SubExprs = new (C) Stmt*[TotalSize];
208}
209
Craig Toppera31a8822013-08-22 07:09:37 +0000210bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000211 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000212 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000213}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000214
Sebastian Redlbd150f42008-11-21 19:14:01 +0000215// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000216QualType CXXDeleteExpr::getDestroyedType() const {
217 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000218 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000219 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000220
221 if (ArgType->isDependentType() && !ArgType->isPointerType())
222 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000223
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000224 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000225}
226
Douglas Gregorad8a3362009-09-04 17:36:40 +0000227// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000228PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
229 : Type(Info)
230{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000231 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000232}
233
Craig Toppera31a8822013-08-22 07:09:37 +0000234CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000235 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
236 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
237 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
238 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000239 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000240 Context.getPointerType(Context.getFunctionType(
241 Context.VoidTy, None,
242 FunctionProtoType::ExtProtoInfo(
243 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000244 VK_RValue, OK_Ordinary,
245 /*isTypeDependent=*/(Base->isTypeDependent() ||
246 (DestroyedType.getTypeSourceInfo() &&
247 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000249 (Base->isInstantiationDependent() ||
250 (QualifierLoc &&
251 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
252 (ScopeType &&
253 ScopeType->getType()->isInstantiationDependentType()) ||
254 (DestroyedType.getTypeSourceInfo() &&
255 DestroyedType.getTypeSourceInfo()->getType()
256 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000257 // ContainsUnexpandedParameterPack
258 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000259 (QualifierLoc &&
260 QualifierLoc.getNestedNameSpecifier()
261 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000262 (ScopeType &&
263 ScopeType->getType()->containsUnexpandedParameterPack()) ||
264 (DestroyedType.getTypeSourceInfo() &&
265 DestroyedType.getTypeSourceInfo()->getType()
266 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000267 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000268 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000269 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
270 DestroyedType(DestroyedType) { }
271
Douglas Gregor678f90d2010-02-25 01:56:36 +0000272QualType CXXPseudoDestructorExpr::getDestroyedType() const {
273 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
274 return TInfo->getType();
275
276 return QualType();
277}
278
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000279SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000280 SourceLocation End = DestroyedType.getLocation();
281 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000282 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000283 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000284}
285
John McCalld14a8642009-11-21 08:51:07 +0000286// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000287UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000288UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000289 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000290 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000291 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000292 const DeclarationNameInfo &NameInfo,
293 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000294 const TemplateArgumentListInfo *Args,
295 UnresolvedSetIterator Begin,
296 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000297{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000298 assert(Args || TemplateKWLoc.isValid());
299 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000300 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000301 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
303 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000304 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000305 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000306}
307
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000308UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000309UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000310 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000311 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000312 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000313 if (HasTemplateKWAndArgsInfo)
314 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000315
Chris Lattner5c0b4052010-10-30 05:14:06 +0000316 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000317 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000318 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000319 return E;
320}
321
Craig Toppera31a8822013-08-22 07:09:37 +0000322OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000323 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000324 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000325 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000326 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000327 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000328 UnresolvedSetIterator End,
329 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000330 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 bool KnownContainsUnexpandedParameterPack)
332 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
333 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000334 (KnownInstantiationDependent ||
335 NameInfo.isInstantiationDependent() ||
336 (QualifierLoc &&
337 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000338 (KnownContainsUnexpandedParameterPack ||
339 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()
342 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000343 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
344 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000345 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000346{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000347 NumResults = End - Begin;
348 if (NumResults) {
349 // Determine whether this expression is type-dependent.
350 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
351 if ((*I)->getDeclContext()->isDependentContext() ||
352 isa<UnresolvedUsingValueDecl>(*I)) {
353 ExprBits.TypeDependent = true;
354 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000355 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000356 }
357 }
358
359 Results = static_cast<DeclAccessPair *>(
360 C.Allocate(sizeof(DeclAccessPair) * NumResults,
361 llvm::alignOf<DeclAccessPair>()));
362 memcpy(Results, &*Begin.getIterator(),
363 NumResults * sizeof(DeclAccessPair));
364 }
365
366 // If we have explicit template arguments, check for dependent
367 // template arguments and whether they contain any unexpanded pack
368 // expansions.
369 if (TemplateArgs) {
370 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000371 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000372 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000373 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
374 Dependent,
375 InstantiationDependent,
376 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000377
378 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000379 ExprBits.TypeDependent = true;
380 ExprBits.ValueDependent = true;
381 }
382 if (InstantiationDependent)
383 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000384 if (ContainsUnexpandedParameterPack)
385 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 } else if (TemplateKWLoc.isValid()) {
387 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388 }
389
390 if (isTypeDependent())
391 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000392}
393
Craig Toppera31a8822013-08-22 07:09:37 +0000394void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000395 UnresolvedSetIterator Begin,
396 UnresolvedSetIterator End) {
397 assert(Results == 0 && "Results already initialized!");
398 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000399 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000400 Results = static_cast<DeclAccessPair *>(
401 C.Allocate(sizeof(DeclAccessPair) * NumResults,
402
403 llvm::alignOf<DeclAccessPair>()));
404 memcpy(Results, &*Begin.getIterator(),
405 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000406 }
407}
408
John McCall8c12dc42010-04-22 18:44:12 +0000409CXXRecordDecl *OverloadExpr::getNamingClass() const {
410 if (isa<UnresolvedLookupExpr>(this))
411 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
412 else
413 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
414}
415
John McCall8cd78132009-11-19 22:55:06 +0000416// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000417DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000418 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000419 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000420 const DeclarationNameInfo &NameInfo,
421 const TemplateArgumentListInfo *Args)
422 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
423 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000424 (NameInfo.isInstantiationDependent() ||
425 (QualifierLoc &&
426 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000427 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000428 (QualifierLoc &&
429 QualifierLoc.getNestedNameSpecifier()
430 ->containsUnexpandedParameterPack()))),
431 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000433{
434 if (Args) {
435 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000436 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000437 bool ContainsUnexpandedParameterPack
438 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000439 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
440 Dependent,
441 InstantiationDependent,
442 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000443 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000444 } else if (TemplateKWLoc.isValid()) {
445 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000446 }
447}
448
John McCalle66edc12009-11-24 19:00:30 +0000449DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000450DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000451 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000452 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000453 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000454 const TemplateArgumentListInfo *Args) {
455 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000456 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000457 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
458 else if (TemplateKWLoc.isValid())
459 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000460 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000461 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
462 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000463}
464
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000465DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000466DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000467 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000468 unsigned NumTemplateArgs) {
469 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000470 if (HasTemplateKWAndArgsInfo)
471 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000472 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000473 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000474 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000475 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000476 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000477 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000478 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000479}
480
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000481SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000482 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000483 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
484 return Loc;
485}
486
487SourceLocation CXXConstructExpr::getLocEnd() const {
488 if (isa<CXXTemporaryObjectExpr>(this))
489 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000490
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000491 if (ParenOrBraceRange.isValid())
492 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000493
494 SourceLocation End = Loc;
495 for (unsigned I = getNumArgs(); I > 0; --I) {
496 const Expr *Arg = getArg(I-1);
497 if (!Arg->isDefaultArgument()) {
498 SourceLocation NewEnd = Arg->getLocEnd();
499 if (NewEnd.isValid()) {
500 End = NewEnd;
501 break;
502 }
503 }
504 }
505
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000506 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000507}
508
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000509SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000510 OverloadedOperatorKind Kind = getOperator();
511 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
512 if (getNumArgs() == 1)
513 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000514 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000515 else
516 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000517 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000518 } else if (Kind == OO_Arrow) {
519 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000520 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000521 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000522 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000523 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000524 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000525 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000526 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000527 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000528 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000529 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000530 }
531}
532
Ted Kremenek98a24e32011-03-30 17:41:19 +0000533Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000534 const Expr *Callee = getCallee()->IgnoreParens();
535 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000536 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000537 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
538 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
539 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000540
541 // FIXME: Will eventually need to cope with member pointers.
542 return 0;
543}
544
Ted Kremenek98a24e32011-03-30 17:41:19 +0000545CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
546 if (const MemberExpr *MemExpr =
547 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
548 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
549
550 // FIXME: Will eventually need to cope with member pointers.
551 return 0;
552}
553
554
David Blaikiec0f58662012-05-03 16:25:49 +0000555CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000556 Expr* ThisArg = getImplicitObjectArgument();
557 if (!ThisArg)
558 return 0;
559
560 if (ThisArg->getType()->isAnyPointerType())
561 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
562
563 return ThisArg->getType()->getAsCXXRecordDecl();
564}
565
Douglas Gregoref986e82009-11-12 15:31:47 +0000566
Douglas Gregore200adc2008-10-27 19:41:14 +0000567//===----------------------------------------------------------------------===//
568// Named casts
569//===----------------------------------------------------------------------===//
570
571/// getCastName - Get the name of the C++ cast being used, e.g.,
572/// "static_cast", "dynamic_cast", "reinterpret_cast", or
573/// "const_cast". The returned pointer must not be freed.
574const char *CXXNamedCastExpr::getCastName() const {
575 switch (getStmtClass()) {
576 case CXXStaticCastExprClass: return "static_cast";
577 case CXXDynamicCastExprClass: return "dynamic_cast";
578 case CXXReinterpretCastExprClass: return "reinterpret_cast";
579 case CXXConstCastExprClass: return "const_cast";
580 default: return "<invalid cast>";
581 }
582}
Douglas Gregordd04d332009-01-16 18:33:17 +0000583
Craig Toppera31a8822013-08-22 07:09:37 +0000584CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000585 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000586 CastKind K, Expr *Op,
587 const CXXCastPath *BasePath,
588 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000589 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000590 SourceLocation RParenLoc,
591 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000592 unsigned PathSize = (BasePath ? BasePath->size() : 0);
593 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
594 + PathSize * sizeof(CXXBaseSpecifier*));
595 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000596 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000597 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000598 if (PathSize) E->setCastPath(*BasePath);
599 return E;
600}
601
Craig Toppera31a8822013-08-22 07:09:37 +0000602CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000603 unsigned PathSize) {
604 void *Buffer =
605 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
606 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
607}
608
Craig Toppera31a8822013-08-22 07:09:37 +0000609CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000610 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000611 CastKind K, Expr *Op,
612 const CXXCastPath *BasePath,
613 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000614 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000615 SourceLocation RParenLoc,
616 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000617 unsigned PathSize = (BasePath ? BasePath->size() : 0);
618 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
619 + PathSize * sizeof(CXXBaseSpecifier*));
620 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000621 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000622 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000623 if (PathSize) E->setCastPath(*BasePath);
624 return E;
625}
626
Craig Toppera31a8822013-08-22 07:09:37 +0000627CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000628 unsigned PathSize) {
629 void *Buffer =
630 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
631 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
632}
633
Anders Carlsson267c0c92011-04-11 01:43:55 +0000634/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
635/// to always be null. For example:
636///
637/// struct A { };
638/// struct B final : A { };
639/// struct C { };
640///
641/// C *f(B* b) { return dynamic_cast<C*>(b); }
642bool CXXDynamicCastExpr::isAlwaysNull() const
643{
644 QualType SrcType = getSubExpr()->getType();
645 QualType DestType = getType();
646
647 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
648 SrcType = SrcPTy->getPointeeType();
649 DestType = DestType->castAs<PointerType>()->getPointeeType();
650 }
651
Alexis Hunt78e2b912012-06-19 23:44:55 +0000652 if (DestType->isVoidType())
653 return false;
654
Anders Carlsson267c0c92011-04-11 01:43:55 +0000655 const CXXRecordDecl *SrcRD =
656 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
657
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000658 if (!SrcRD->hasAttr<FinalAttr>())
659 return false;
660
Anders Carlsson267c0c92011-04-11 01:43:55 +0000661 const CXXRecordDecl *DestRD =
662 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
663
664 return !DestRD->isDerivedFrom(SrcRD);
665}
666
John McCallcf142162010-08-07 06:22:56 +0000667CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000668CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
669 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000670 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000671 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000672 SourceLocation RParenLoc,
673 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000674 unsigned PathSize = (BasePath ? BasePath->size() : 0);
675 void *Buffer =
676 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
677 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000678 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000679 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000680 if (PathSize) E->setCastPath(*BasePath);
681 return E;
682}
683
684CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000685CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000686 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
687 + PathSize * sizeof(CXXBaseSpecifier*));
688 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
689}
690
Craig Toppera31a8822013-08-22 07:09:37 +0000691CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000692 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000693 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000694 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000695 SourceLocation RParenLoc,
696 SourceRange AngleBrackets) {
697 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000698}
699
Craig Toppera31a8822013-08-22 07:09:37 +0000700CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000701 return new (C) CXXConstCastExpr(EmptyShell());
702}
703
704CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000705CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000706 TypeSourceInfo *Written, CastKind K, Expr *Op,
707 const CXXCastPath *BasePath,
708 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000709 unsigned PathSize = (BasePath ? BasePath->size() : 0);
710 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
711 + PathSize * sizeof(CXXBaseSpecifier*));
712 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000713 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000714 if (PathSize) E->setCastPath(*BasePath);
715 return E;
716}
717
718CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000719CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000720 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
721 + PathSize * sizeof(CXXBaseSpecifier*));
722 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
723}
724
Eli Friedman89fe0d52013-08-15 22:02:56 +0000725SourceLocation CXXFunctionalCastExpr::getLocStart() const {
726 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
727}
728
729SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
730 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
731}
732
Richard Smithc67fdd42012-03-07 08:35:16 +0000733UserDefinedLiteral::LiteralOperatorKind
734UserDefinedLiteral::getLiteralOperatorKind() const {
735 if (getNumArgs() == 0)
736 return LOK_Template;
737 if (getNumArgs() == 2)
738 return LOK_String;
739
740 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
741 QualType ParamTy =
742 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
743 if (ParamTy->isPointerType())
744 return LOK_Raw;
745 if (ParamTy->isAnyCharacterType())
746 return LOK_Character;
747 if (ParamTy->isIntegerType())
748 return LOK_Integer;
749 if (ParamTy->isFloatingType())
750 return LOK_Floating;
751
752 llvm_unreachable("unknown kind of literal operator");
753}
754
755Expr *UserDefinedLiteral::getCookedLiteral() {
756#ifndef NDEBUG
757 LiteralOperatorKind LOK = getLiteralOperatorKind();
758 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
759#endif
760 return getArg(0);
761}
762
763const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
764 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
765}
John McCallcf142162010-08-07 06:22:56 +0000766
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000767CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000768CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000769 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000770 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000771 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
772 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000773}
774
Craig Toppera31a8822013-08-22 07:09:37 +0000775CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000776 FieldDecl *Field, QualType T)
777 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
778 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
779 ? VK_XValue
780 : VK_RValue,
781 /*FIXME*/ OK_Ordinary, false, false, false, false),
782 Field(Field), Loc(Loc) {
783 assert(Field->hasInClassInitializer());
784}
785
Craig Toppera31a8822013-08-22 07:09:37 +0000786CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000787 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000788 return new (C) CXXTemporary(Destructor);
789}
790
Craig Toppera31a8822013-08-22 07:09:37 +0000791CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000792 CXXTemporary *Temp,
793 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000794 assert((SubExpr->getType()->isRecordType() ||
795 SubExpr->getType()->isArrayType()) &&
796 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000797
Douglas Gregora6e053e2010-12-15 01:34:56 +0000798 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000799}
800
Craig Toppera31a8822013-08-22 07:09:37 +0000801CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000802 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000803 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000804 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000805 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000806 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000807 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000808 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000809 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
810 Type->getType().getNonReferenceType(),
811 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000812 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000813 HadMultipleCandidates,
814 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000815 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000816 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000817}
818
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000819SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
820 return Type->getTypeLoc().getBeginLoc();
821}
822
823SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000824 return getParenOrBraceRange().getEnd();
Douglas Gregordd04d332009-01-16 18:33:17 +0000825}
Anders Carlsson6f287832009-04-21 02:22:11 +0000826
Craig Toppera31a8822013-08-22 07:09:37 +0000827CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000828 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000829 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000830 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000831 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000832 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000833 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000834 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000835 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000836 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000837 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000838 HadMultipleCandidates, ListInitialization,
839 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000840 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000841}
842
Craig Toppera31a8822013-08-22 07:09:37 +0000843CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
844 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000845 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000846 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000847 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000848 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000849 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000850 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000851 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000852 : Expr(SC, T, VK_RValue, OK_Ordinary,
853 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000854 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000855 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000856 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
857 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000858 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000859 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000860 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000861 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000862{
863 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000864 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000865
Benjamin Kramerc215e762012-08-24 11:54:20 +0000866 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000867 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000868
869 if (args[i]->isValueDependent())
870 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000871 if (args[i]->isInstantiationDependent())
872 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000873 if (args[i]->containsUnexpandedParameterPack())
874 ExprBits.ContainsUnexpandedParameterPack = true;
875
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000876 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000877 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000878 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000879}
880
Douglas Gregore31e6062012-02-07 10:09:13 +0000881LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
882 LambdaCaptureKind Kind, VarDecl *Var,
883 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000884 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000885{
886 unsigned Bits = 0;
887 if (Implicit)
888 Bits |= Capture_Implicit;
889
890 switch (Kind) {
891 case LCK_This:
892 assert(Var == 0 && "'this' capture cannot have a variable!");
893 break;
894
895 case LCK_ByCopy:
896 Bits |= Capture_ByCopy;
897 // Fall through
898 case LCK_ByRef:
899 assert(Var && "capture must have a variable!");
900 break;
Richard Smithba71c082013-05-16 06:20:58 +0000901
902 case LCK_Init:
903 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregore31e6062012-02-07 10:09:13 +0000904 }
Richard Smithba71c082013-05-16 06:20:58 +0000905 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000906}
907
Richard Smithba71c082013-05-16 06:20:58 +0000908LambdaExpr::Capture::Capture(FieldDecl *Field)
909 : DeclAndBits(Field,
910 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
911 Loc(Field->getLocation()), EllipsisLoc() {}
912
Douglas Gregore31e6062012-02-07 10:09:13 +0000913LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000914 Decl *D = DeclAndBits.getPointer();
915 if (!D)
Douglas Gregore31e6062012-02-07 10:09:13 +0000916 return LCK_This;
917
Richard Smithba71c082013-05-16 06:20:58 +0000918 if (isa<FieldDecl>(D))
919 return LCK_Init;
920
921 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000922}
923
James Dennettddd36ff2013-08-09 23:08:25 +0000924LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000925 SourceRange IntroducerRange,
926 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000927 SourceLocation CaptureDefaultLoc,
928 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000929 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000930 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000931 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000932 ArrayRef<VarDecl *> ArrayIndexVars,
933 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000934 SourceLocation ClosingBrace,
935 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000936 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
937 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000938 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000939 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000940 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000941 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000942 CaptureDefault(CaptureDefault),
943 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000944 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000945 ClosingBrace(ClosingBrace)
946{
947 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000948 CXXRecordDecl *Class = getLambdaClass();
949 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000950
951 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000952
953 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000954 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000955 Data.NumCaptures = NumCaptures;
956 Data.NumExplicitCaptures = 0;
957 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
958 Capture *ToCapture = Data.Captures;
959 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
960 if (Captures[I].isExplicit())
961 ++Data.NumExplicitCaptures;
962
963 *ToCapture++ = Captures[I];
964 }
965
966 // Copy initialization expressions for the non-static data members.
967 Stmt **Stored = getStoredStmts();
968 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
969 *Stored++ = CaptureInits[I];
970
971 // Copy the body of the lambda.
972 *Stored++ = getCallOperator()->getBody();
973
974 // Copy the array index variables, if any.
975 HasArrayIndexVars = !ArrayIndexVars.empty();
976 if (HasArrayIndexVars) {
977 assert(ArrayIndexStarts.size() == NumCaptures);
978 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
979 sizeof(VarDecl *) * ArrayIndexVars.size());
980 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
981 sizeof(unsigned) * Captures.size());
982 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000983 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000984}
985
Craig Toppera31a8822013-08-22 07:09:37 +0000986LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000987 CXXRecordDecl *Class,
988 SourceRange IntroducerRange,
989 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000990 SourceLocation CaptureDefaultLoc,
991 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000992 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000993 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000994 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000995 ArrayRef<VarDecl *> ArrayIndexVars,
996 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000997 SourceLocation ClosingBrace,
998 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000999 // Determine the type of the expression (i.e., the type of the
1000 // function object we're creating).
1001 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001002
Douglas Gregore5561632012-02-13 17:20:40 +00001003 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001004 if (!ArrayIndexVars.empty()) {
1005 Size += sizeof(unsigned) * (Captures.size() + 1);
1006 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001007 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001008 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1009 }
Douglas Gregore5561632012-02-13 17:20:40 +00001010 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001011 return new (Mem) LambdaExpr(T, IntroducerRange,
1012 CaptureDefault, CaptureDefaultLoc, Captures,
1013 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001014 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001015 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001016}
1017
Craig Toppera31a8822013-08-22 07:09:37 +00001018LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1019 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001020 unsigned NumArrayIndexVars) {
1021 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1022 if (NumArrayIndexVars)
1023 Size += sizeof(VarDecl) * NumArrayIndexVars
1024 + sizeof(unsigned) * (NumCaptures + 1);
1025 void *Mem = C.Allocate(Size);
1026 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1027}
1028
Douglas Gregorc8a73492012-02-13 15:44:47 +00001029LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001030 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001031}
1032
1033LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001034 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001035}
1036
1037LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1038 return capture_begin();
1039}
1040
1041LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1042 struct CXXRecordDecl::LambdaDefinitionData &Data
1043 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001044 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001045}
1046
1047LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1048 return explicit_capture_end();
1049}
1050
1051LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1052 return capture_end();
1053}
1054
Douglas Gregor54fcea62012-02-13 16:35:30 +00001055ArrayRef<VarDecl *>
1056LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001057 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001058
1059 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001060 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1061 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001062 VarDecl **IndexVars = getArrayIndexVars();
1063 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001064 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1065 IndexVars + IndexStarts[Index + 1]);
1066}
1067
Douglas Gregore31e6062012-02-07 10:09:13 +00001068CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1069 return getType()->getAsCXXRecordDecl();
1070}
1071
1072CXXMethodDecl *LambdaExpr::getCallOperator() const {
1073 CXXRecordDecl *Record = getLambdaClass();
Manuel Klimek2fdbea22013-08-22 12:12:24 +00001074 DeclarationName Name
1075 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1076 DeclContext::lookup_result Calls = Record->lookup(Name);
1077 assert(!Calls.empty() && "Missing lambda call operator!");
1078 assert(Calls.size() == 1 && "More than one lambda call operator!");
1079 CXXMethodDecl *Result = cast<CXXMethodDecl>(Calls.front());
1080 return Result;
Douglas Gregore31e6062012-02-07 10:09:13 +00001081}
1082
Douglas Gregor99ae8062012-02-14 17:54:36 +00001083CompoundStmt *LambdaExpr::getBody() const {
1084 if (!getStoredStmts()[NumCaptures])
1085 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1086
1087 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1088}
1089
Douglas Gregore31e6062012-02-07 10:09:13 +00001090bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001091 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001092}
1093
John McCall28fc7092011-11-10 05:35:25 +00001094ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1095 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001096 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001097 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001098 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001099 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001100 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001101 SubExpr(subexpr) {
1102 ExprWithCleanupsBits.NumObjects = objects.size();
1103 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1104 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001105}
1106
Craig Toppera31a8822013-08-22 07:09:37 +00001107ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001108 ArrayRef<CleanupObject> objects) {
1109 size_t size = sizeof(ExprWithCleanups)
1110 + objects.size() * sizeof(CleanupObject);
1111 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1112 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001113}
1114
John McCall28fc7092011-11-10 05:35:25 +00001115ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1116 : Expr(ExprWithCleanupsClass, empty) {
1117 ExprWithCleanupsBits.NumObjects = numObjects;
1118}
Chris Lattnercba86142010-05-10 00:25:06 +00001119
Craig Toppera31a8822013-08-22 07:09:37 +00001120ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1121 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001122 unsigned numObjects) {
1123 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1124 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1125 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001126}
1127
Douglas Gregor2b88c112010-09-08 00:15:04 +00001128CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001129 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001130 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001131 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001132 : Expr(CXXUnresolvedConstructExprClass,
1133 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001134 (Type->getType()->isLValueReferenceType() ? VK_LValue
1135 :Type->getType()->isRValueReferenceType()? VK_XValue
1136 :VK_RValue),
1137 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001138 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001139 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001140 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001141 LParenLoc(LParenLoc),
1142 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001143 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001144 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001145 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 if (Args[I]->containsUnexpandedParameterPack())
1147 ExprBits.ContainsUnexpandedParameterPack = true;
1148
1149 StoredArgs[I] = Args[I];
1150 }
Douglas Gregorce934142009-05-20 18:46:25 +00001151}
1152
1153CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001154CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001155 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001156 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001157 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001158 SourceLocation RParenLoc) {
1159 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001160 sizeof(Expr *) * Args.size());
1161 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001162}
1163
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001164CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001165CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001166 Stmt::EmptyShell Empty;
1167 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1168 sizeof(Expr *) * NumArgs);
1169 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1170}
1171
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001172SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1173 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001174}
1175
Craig Toppera31a8822013-08-22 07:09:37 +00001176CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001177 Expr *Base, QualType BaseType,
1178 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001179 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001180 NestedNameSpecifierLoc QualifierLoc,
1181 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001182 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001183 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001184 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001185 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001186 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001187 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001188 (QualifierLoc &&
1189 QualifierLoc.getNestedNameSpecifier()
1190 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001191 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001192 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001193 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001194 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001195 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001196 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001197 if (TemplateArgs) {
1198 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001199 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001200 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001201 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1202 Dependent,
1203 InstantiationDependent,
1204 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001205 if (ContainsUnexpandedParameterPack)
1206 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001207 } else if (TemplateKWLoc.isValid()) {
1208 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001209 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001210}
1211
Craig Toppera31a8822013-08-22 07:09:37 +00001212CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001213 Expr *Base, QualType BaseType,
1214 bool IsArrow,
1215 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001216 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001217 NamedDecl *FirstQualifierFoundInScope,
1218 DeclarationNameInfo MemberNameInfo)
1219 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001220 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001221 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001222 (QualifierLoc &&
1223 QualifierLoc.getNestedNameSpecifier()->
1224 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001225 MemberNameInfo.containsUnexpandedParameterPack())),
1226 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001227 HasTemplateKWAndArgsInfo(false),
1228 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001229 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1230 MemberNameInfo(MemberNameInfo) { }
1231
John McCall8cd78132009-11-19 22:55:06 +00001232CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001233CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001234 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001235 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001236 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001237 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001238 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001239 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001240 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001241 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001242 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1243 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001244 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001245 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001246 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001247
Abramo Bagnara7945c982012-01-27 09:46:47 +00001248 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1249 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1250 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001251
Chris Lattner5c0b4052010-10-30 05:14:06 +00001252 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001253 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1254 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001255 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001256 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001257 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001258 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001259}
1260
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001261CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001262CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001263 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001264 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001265 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001266 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001267 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001268 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001269 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001270
1271 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001272 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001273 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001274 CXXDependentScopeMemberExpr *E
1275 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001276 0, SourceLocation(),
1277 NestedNameSpecifierLoc(),
1278 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001279 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001280 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001281 return E;
1282}
1283
Douglas Gregor0da1d432011-02-28 20:01:57 +00001284bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1285 if (Base == 0)
1286 return true;
1287
Douglas Gregor25b7e052011-03-02 21:06:53 +00001288 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001289}
1290
John McCall0009fcc2011-04-26 20:42:42 +00001291static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1292 UnresolvedSetIterator end) {
1293 do {
1294 NamedDecl *decl = *begin;
1295 if (isa<UnresolvedUsingValueDecl>(decl))
1296 return false;
1297 if (isa<UsingShadowDecl>(decl))
1298 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1299
1300 // Unresolved member expressions should only contain methods and
1301 // method templates.
1302 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1303
1304 if (isa<FunctionTemplateDecl>(decl))
1305 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1306 if (cast<CXXMethodDecl>(decl)->isStatic())
1307 return false;
1308 } while (++begin != end);
1309
1310 return true;
1311}
1312
Craig Toppera31a8822013-08-22 07:09:37 +00001313UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001314 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001315 Expr *Base, QualType BaseType,
1316 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001317 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001318 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001319 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001320 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001321 const TemplateArgumentListInfo *TemplateArgs,
1322 UnresolvedSetIterator Begin,
1323 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001324 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1325 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001326 // Dependent
1327 ((Base && Base->isTypeDependent()) ||
1328 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001329 ((Base && Base->isInstantiationDependent()) ||
1330 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001331 // Contains unexpanded parameter pack
1332 ((Base && Base->containsUnexpandedParameterPack()) ||
1333 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001334 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1335 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001336
1337 // Check whether all of the members are non-static member functions,
1338 // and if so, mark give this bound-member type instead of overload type.
1339 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1340 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001341}
1342
Douglas Gregor0da1d432011-02-28 20:01:57 +00001343bool UnresolvedMemberExpr::isImplicitAccess() const {
1344 if (Base == 0)
1345 return true;
1346
Douglas Gregor25b7e052011-03-02 21:06:53 +00001347 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001348}
1349
John McCall10eae182009-11-30 22:42:35 +00001350UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001351UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001352 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001353 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001354 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001355 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001356 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001357 const TemplateArgumentListInfo *TemplateArgs,
1358 UnresolvedSetIterator Begin,
1359 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001360 std::size_t size = sizeof(UnresolvedMemberExpr);
1361 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001362 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1363 else if (TemplateKWLoc.isValid())
1364 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001365
Chris Lattner5c0b4052010-10-30 05:14:06 +00001366 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001367 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001368 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001369 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001370 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001371}
1372
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001373UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001374UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1375 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001376 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001377 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001378 if (HasTemplateKWAndArgsInfo)
1379 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001380
Chris Lattner5c0b4052010-10-30 05:14:06 +00001381 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001382 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001383 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001384 return E;
1385}
1386
John McCall58cc69d2010-01-27 01:50:18 +00001387CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1388 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1389
1390 // If there was a nested name specifier, it names the naming class.
1391 // It can't be dependent: after all, we were actually able to do the
1392 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001393 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001394 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001395 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001396 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001397 Record = T->getAsCXXRecordDecl();
1398 assert(Record && "qualifier in member expression does not name record");
1399 }
John McCall58cc69d2010-01-27 01:50:18 +00001400 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001401 else {
John McCall58cc69d2010-01-27 01:50:18 +00001402 QualType BaseType = getBaseType().getNonReferenceType();
1403 if (isArrow()) {
1404 const PointerType *PT = BaseType->getAs<PointerType>();
1405 assert(PT && "base of arrow member access is not pointer");
1406 BaseType = PT->getPointeeType();
1407 }
1408
Douglas Gregor9262f472010-04-27 18:19:34 +00001409 Record = BaseType->getAsCXXRecordDecl();
1410 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001411 }
1412
Douglas Gregor9262f472010-04-27 18:19:34 +00001413 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001414}
1415
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001416SubstNonTypeTemplateParmPackExpr::
1417SubstNonTypeTemplateParmPackExpr(QualType T,
1418 NonTypeTemplateParmDecl *Param,
1419 SourceLocation NameLoc,
1420 const TemplateArgument &ArgPack)
1421 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001422 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001423 Param(Param), Arguments(ArgPack.pack_begin()),
1424 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1425
1426TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1427 return TemplateArgument(Arguments, NumArguments);
1428}
1429
Richard Smithb15fe3a2012-09-12 00:56:43 +00001430FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1431 SourceLocation NameLoc,
1432 unsigned NumParams,
1433 Decl * const *Params)
1434 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1435 true, true, true, true),
1436 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1437 if (Params)
1438 std::uninitialized_copy(Params, Params + NumParams,
1439 reinterpret_cast<Decl**>(this+1));
1440}
1441
1442FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001443FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001444 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001445 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001446 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1447 sizeof(ParmVarDecl*) * Params.size()))
1448 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1449}
1450
1451FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001452FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1453 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001454 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1455 sizeof(ParmVarDecl*) * NumParams))
1456 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1457}
1458
Douglas Gregor29c42f22012-02-24 07:38:34 +00001459TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1460 ArrayRef<TypeSourceInfo *> Args,
1461 SourceLocation RParenLoc,
1462 bool Value)
1463 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1464 /*TypeDependent=*/false,
1465 /*ValueDependent=*/false,
1466 /*InstantiationDependent=*/false,
1467 /*ContainsUnexpandedParameterPack=*/false),
1468 Loc(Loc), RParenLoc(RParenLoc)
1469{
1470 TypeTraitExprBits.Kind = Kind;
1471 TypeTraitExprBits.Value = Value;
1472 TypeTraitExprBits.NumArgs = Args.size();
1473
1474 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1475
1476 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1477 if (Args[I]->getType()->isDependentType())
1478 setValueDependent(true);
1479 if (Args[I]->getType()->isInstantiationDependentType())
1480 setInstantiationDependent(true);
1481 if (Args[I]->getType()->containsUnexpandedParameterPack())
1482 setContainsUnexpandedParameterPack(true);
1483
1484 ToArgs[I] = Args[I];
1485 }
1486}
1487
Craig Toppera31a8822013-08-22 07:09:37 +00001488TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001489 SourceLocation Loc,
1490 TypeTrait Kind,
1491 ArrayRef<TypeSourceInfo *> Args,
1492 SourceLocation RParenLoc,
1493 bool Value) {
1494 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1495 void *Mem = C.Allocate(Size);
1496 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1497}
1498
Craig Toppera31a8822013-08-22 07:09:37 +00001499TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001500 unsigned NumArgs) {
1501 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1502 void *Mem = C.Allocate(Size);
1503 return new (Mem) TypeTraitExpr(EmptyShell());
1504}
1505
David Blaikie68e081d2011-12-20 02:48:34 +00001506void ArrayTypeTraitExpr::anchor() { }