blob: 55589e6d21f92d32146ad8fa6457a1b615a68046 [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
David Majnemer5d22e7e2013-09-07 07:11:04 +000070 // __uuidof can grab UUIDs from template arguments.
David Majnemer59c0ec22013-09-07 06:59:46 +000071 if (ClassTemplateSpecializationDecl *CTSD =
72 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
73 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
74 UuidAttr *UuidForRD = 0;
75
76 for (unsigned I = 0, N = TAL.size(); I != N; ++I) {
77 const TemplateArgument &TA = TAL[I];
78 bool SeenMultipleGUIDs = false;
79
80 UuidAttr *UuidForTA = 0;
81 if (TA.getKind() == TemplateArgument::Type)
82 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
83 else if (TA.getKind() == TemplateArgument::Declaration)
84 UuidForTA =
85 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
86
87 // If the template argument has a UUID, there are three cases:
88 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000089 // - This is a different UUID than previously seen for this RecordDecl.
90 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000091 if (UuidForTA) {
92 if (!UuidForRD)
93 UuidForRD = UuidForTA;
94 else if (UuidForRD != UuidForTA)
95 SeenMultipleGUIDs = true;
96 }
97
98 // Seeing multiple UUIDs means that we couldn't find a UUID
99 if (SeenMultipleGUIDs) {
100 if (RDHasMultipleGUIDsPtr)
101 *RDHasMultipleGUIDsPtr = true;
102 return 0;
103 }
104 }
105
106 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000107 }
108
109 for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
110 E = RD->redecls_end();
111 I != E; ++I)
112 if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
113 return Uuid;
Nico Webercf4ff5862012-10-11 10:13:44 +0000114
115 return 0;
116}
117
David Majnemer8eaab6f2013-08-13 06:32:20 +0000118StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
119 StringRef Uuid;
120 if (isTypeOperand())
121 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand())->getGuid();
122 else {
123 // Special case: __uuidof(0) means an all-zero GUID.
124 Expr *Op = getExprOperand();
125 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
126 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
127 else
128 Uuid = "00000000-0000-0000-0000-000000000000";
129 }
130 return Uuid;
131}
132
Douglas Gregor747eb782010-07-08 06:14:04 +0000133// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000134SourceLocation CXXScalarValueInitExpr::getLocStart() const {
135 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000136}
137
Sebastian Redlbd150f42008-11-21 19:14:01 +0000138// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000139CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
140 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000141 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000142 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000143 SourceRange typeIdParens, Expr *arraySize,
144 InitializationStyle initializationStyle,
145 Expr *initializer, QualType ty,
146 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000147 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000148 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000150 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000151 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +0000152 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
153 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000154 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000155 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +0000156 assert((initializer != 0 || initializationStyle == NoInit) &&
157 "Only NoInit can have no initializer.");
158 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Benjamin Kramerc215e762012-08-24 11:54:20 +0000159 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000160 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000161 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000162 if (arraySize->isInstantiationDependent())
163 ExprBits.InstantiationDependent = true;
164
Douglas Gregora6e053e2010-12-15 01:34:56 +0000165 if (arraySize->containsUnexpandedParameterPack())
166 ExprBits.ContainsUnexpandedParameterPack = true;
167
Sebastian Redl351bb782008-12-02 14:43:59 +0000168 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000169 }
170
Sebastian Redl6047f072012-02-16 12:22:20 +0000171 if (initializer) {
172 if (initializer->isInstantiationDependent())
173 ExprBits.InstantiationDependent = true;
174
175 if (initializer->containsUnexpandedParameterPack())
176 ExprBits.ContainsUnexpandedParameterPack = true;
177
178 SubExprs[i++] = initializer;
179 }
180
Benjamin Kramerc215e762012-08-24 11:54:20 +0000181 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000182 if (placementArgs[j]->isInstantiationDependent())
183 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000184 if (placementArgs[j]->containsUnexpandedParameterPack())
185 ExprBits.ContainsUnexpandedParameterPack = true;
186
Sebastian Redlbd150f42008-11-21 19:14:01 +0000187 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000188 }
David Blaikie3a0de212012-11-08 22:53:48 +0000189
190 switch (getInitializationStyle()) {
191 case CallInit:
192 this->Range.setEnd(DirectInitRange.getEnd()); break;
193 case ListInit:
194 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000195 default:
196 if (TypeIdParens.isValid())
197 this->Range.setEnd(TypeIdParens.getEnd());
198 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000199 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000200}
201
Craig Toppera31a8822013-08-22 07:09:37 +0000202void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000203 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000204 assert(SubExprs == 0 && "SubExprs already allocated");
205 Array = isArray;
206 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000207
208 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000209 SubExprs = new (C) Stmt*[TotalSize];
210}
211
Craig Toppera31a8822013-08-22 07:09:37 +0000212bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000213 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000214 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000215}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000216
Sebastian Redlbd150f42008-11-21 19:14:01 +0000217// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000218QualType CXXDeleteExpr::getDestroyedType() const {
219 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000220 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000221 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000222
223 if (ArgType->isDependentType() && !ArgType->isPointerType())
224 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000225
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000226 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000227}
228
Douglas Gregorad8a3362009-09-04 17:36:40 +0000229// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000230PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
231 : Type(Info)
232{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000233 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000234}
235
Craig Toppera31a8822013-08-22 07:09:37 +0000236CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000237 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
238 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
239 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
240 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000241 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000242 Context.getPointerType(Context.getFunctionType(
243 Context.VoidTy, None,
244 FunctionProtoType::ExtProtoInfo(
245 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000246 VK_RValue, OK_Ordinary,
247 /*isTypeDependent=*/(Base->isTypeDependent() ||
248 (DestroyedType.getTypeSourceInfo() &&
249 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000250 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000251 (Base->isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
254 (ScopeType &&
255 ScopeType->getType()->isInstantiationDependentType()) ||
256 (DestroyedType.getTypeSourceInfo() &&
257 DestroyedType.getTypeSourceInfo()->getType()
258 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000259 // ContainsUnexpandedParameterPack
260 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000261 (QualifierLoc &&
262 QualifierLoc.getNestedNameSpecifier()
263 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000264 (ScopeType &&
265 ScopeType->getType()->containsUnexpandedParameterPack()) ||
266 (DestroyedType.getTypeSourceInfo() &&
267 DestroyedType.getTypeSourceInfo()->getType()
268 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000269 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000270 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000271 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
272 DestroyedType(DestroyedType) { }
273
Douglas Gregor678f90d2010-02-25 01:56:36 +0000274QualType CXXPseudoDestructorExpr::getDestroyedType() const {
275 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
276 return TInfo->getType();
277
278 return QualType();
279}
280
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000281SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000282 SourceLocation End = DestroyedType.getLocation();
283 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000284 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000285 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000286}
287
John McCalld14a8642009-11-21 08:51:07 +0000288// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000289UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000290UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000291 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000292 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000293 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000294 const DeclarationNameInfo &NameInfo,
295 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000296 const TemplateArgumentListInfo *Args,
297 UnresolvedSetIterator Begin,
298 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000299{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000300 assert(Args || TemplateKWLoc.isValid());
301 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000303 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000304 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
305 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000306 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000307 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000308}
309
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000310UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000311UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000312 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000313 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000314 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000315 if (HasTemplateKWAndArgsInfo)
316 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000317
Chris Lattner5c0b4052010-10-30 05:14:06 +0000318 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000319 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000320 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000321 return E;
322}
323
Craig Toppera31a8822013-08-22 07:09:37 +0000324OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000325 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000326 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000327 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000328 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000329 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000330 UnresolvedSetIterator End,
331 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000332 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000333 bool KnownContainsUnexpandedParameterPack)
334 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
335 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000336 (KnownInstantiationDependent ||
337 NameInfo.isInstantiationDependent() ||
338 (QualifierLoc &&
339 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000340 (KnownContainsUnexpandedParameterPack ||
341 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000342 (QualifierLoc &&
343 QualifierLoc.getNestedNameSpecifier()
344 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000345 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
346 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000347 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000348{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349 NumResults = End - Begin;
350 if (NumResults) {
351 // Determine whether this expression is type-dependent.
352 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
353 if ((*I)->getDeclContext()->isDependentContext() ||
354 isa<UnresolvedUsingValueDecl>(*I)) {
355 ExprBits.TypeDependent = true;
356 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000357 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358 }
359 }
360
361 Results = static_cast<DeclAccessPair *>(
362 C.Allocate(sizeof(DeclAccessPair) * NumResults,
363 llvm::alignOf<DeclAccessPair>()));
364 memcpy(Results, &*Begin.getIterator(),
365 NumResults * sizeof(DeclAccessPair));
366 }
367
368 // If we have explicit template arguments, check for dependent
369 // template arguments and whether they contain any unexpanded pack
370 // expansions.
371 if (TemplateArgs) {
372 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000373 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000374 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000375 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
376 Dependent,
377 InstantiationDependent,
378 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000379
380 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000381 ExprBits.TypeDependent = true;
382 ExprBits.ValueDependent = true;
383 }
384 if (InstantiationDependent)
385 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000386 if (ContainsUnexpandedParameterPack)
387 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000388 } else if (TemplateKWLoc.isValid()) {
389 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000390 }
391
392 if (isTypeDependent())
393 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000394}
395
Craig Toppera31a8822013-08-22 07:09:37 +0000396void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000397 UnresolvedSetIterator Begin,
398 UnresolvedSetIterator End) {
399 assert(Results == 0 && "Results already initialized!");
400 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000401 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000402 Results = static_cast<DeclAccessPair *>(
403 C.Allocate(sizeof(DeclAccessPair) * NumResults,
404
405 llvm::alignOf<DeclAccessPair>()));
406 memcpy(Results, &*Begin.getIterator(),
407 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000408 }
409}
410
John McCall8c12dc42010-04-22 18:44:12 +0000411CXXRecordDecl *OverloadExpr::getNamingClass() const {
412 if (isa<UnresolvedLookupExpr>(this))
413 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
414 else
415 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
416}
417
John McCall8cd78132009-11-19 22:55:06 +0000418// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000419DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000420 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000421 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000422 const DeclarationNameInfo &NameInfo,
423 const TemplateArgumentListInfo *Args)
424 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
425 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000426 (NameInfo.isInstantiationDependent() ||
427 (QualifierLoc &&
428 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000429 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000430 (QualifierLoc &&
431 QualifierLoc.getNestedNameSpecifier()
432 ->containsUnexpandedParameterPack()))),
433 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000434 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000435{
436 if (Args) {
437 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000438 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000439 bool ContainsUnexpandedParameterPack
440 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000441 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
442 Dependent,
443 InstantiationDependent,
444 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000445 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000446 } else if (TemplateKWLoc.isValid()) {
447 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000448 }
449}
450
John McCalle66edc12009-11-24 19:00:30 +0000451DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000452DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000453 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000454 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000455 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000456 const TemplateArgumentListInfo *Args) {
457 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000458 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000459 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
460 else if (TemplateKWLoc.isValid())
461 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000462 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000463 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
464 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000465}
466
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000467DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000468DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000469 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000470 unsigned NumTemplateArgs) {
471 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000472 if (HasTemplateKWAndArgsInfo)
473 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000474 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000475 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000476 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000477 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000478 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000479 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000480 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000481}
482
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000483SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000484 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000485 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
486 return Loc;
487}
488
489SourceLocation CXXConstructExpr::getLocEnd() const {
490 if (isa<CXXTemporaryObjectExpr>(this))
491 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000492
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000493 if (ParenOrBraceRange.isValid())
494 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000495
496 SourceLocation End = Loc;
497 for (unsigned I = getNumArgs(); I > 0; --I) {
498 const Expr *Arg = getArg(I-1);
499 if (!Arg->isDefaultArgument()) {
500 SourceLocation NewEnd = Arg->getLocEnd();
501 if (NewEnd.isValid()) {
502 End = NewEnd;
503 break;
504 }
505 }
506 }
507
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000508 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000509}
510
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000511SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000512 OverloadedOperatorKind Kind = getOperator();
513 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
514 if (getNumArgs() == 1)
515 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000516 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000517 else
518 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000519 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000520 } else if (Kind == OO_Arrow) {
521 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000522 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000523 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000524 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000525 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000526 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000527 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000528 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000529 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000530 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000531 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000532 }
533}
534
Ted Kremenek98a24e32011-03-30 17:41:19 +0000535Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000536 const Expr *Callee = getCallee()->IgnoreParens();
537 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000538 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000539 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
540 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
541 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000542
543 // FIXME: Will eventually need to cope with member pointers.
544 return 0;
545}
546
Ted Kremenek98a24e32011-03-30 17:41:19 +0000547CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
548 if (const MemberExpr *MemExpr =
549 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
550 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
551
552 // FIXME: Will eventually need to cope with member pointers.
553 return 0;
554}
555
556
David Blaikiec0f58662012-05-03 16:25:49 +0000557CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000558 Expr* ThisArg = getImplicitObjectArgument();
559 if (!ThisArg)
560 return 0;
561
562 if (ThisArg->getType()->isAnyPointerType())
563 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
564
565 return ThisArg->getType()->getAsCXXRecordDecl();
566}
567
Douglas Gregoref986e82009-11-12 15:31:47 +0000568
Douglas Gregore200adc2008-10-27 19:41:14 +0000569//===----------------------------------------------------------------------===//
570// Named casts
571//===----------------------------------------------------------------------===//
572
573/// getCastName - Get the name of the C++ cast being used, e.g.,
574/// "static_cast", "dynamic_cast", "reinterpret_cast", or
575/// "const_cast". The returned pointer must not be freed.
576const char *CXXNamedCastExpr::getCastName() const {
577 switch (getStmtClass()) {
578 case CXXStaticCastExprClass: return "static_cast";
579 case CXXDynamicCastExprClass: return "dynamic_cast";
580 case CXXReinterpretCastExprClass: return "reinterpret_cast";
581 case CXXConstCastExprClass: return "const_cast";
582 default: return "<invalid cast>";
583 }
584}
Douglas Gregordd04d332009-01-16 18:33:17 +0000585
Craig Toppera31a8822013-08-22 07:09:37 +0000586CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000587 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000588 CastKind K, Expr *Op,
589 const CXXCastPath *BasePath,
590 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000591 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000592 SourceLocation RParenLoc,
593 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000594 unsigned PathSize = (BasePath ? BasePath->size() : 0);
595 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
596 + PathSize * sizeof(CXXBaseSpecifier*));
597 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000598 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000599 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000600 if (PathSize) E->setCastPath(*BasePath);
601 return E;
602}
603
Craig Toppera31a8822013-08-22 07:09:37 +0000604CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000605 unsigned PathSize) {
606 void *Buffer =
607 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
608 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
609}
610
Craig Toppera31a8822013-08-22 07:09:37 +0000611CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000612 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000613 CastKind K, Expr *Op,
614 const CXXCastPath *BasePath,
615 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000616 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000617 SourceLocation RParenLoc,
618 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000619 unsigned PathSize = (BasePath ? BasePath->size() : 0);
620 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
621 + PathSize * sizeof(CXXBaseSpecifier*));
622 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000623 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000624 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000625 if (PathSize) E->setCastPath(*BasePath);
626 return E;
627}
628
Craig Toppera31a8822013-08-22 07:09:37 +0000629CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000630 unsigned PathSize) {
631 void *Buffer =
632 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
633 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
634}
635
Anders Carlsson267c0c92011-04-11 01:43:55 +0000636/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
637/// to always be null. For example:
638///
639/// struct A { };
640/// struct B final : A { };
641/// struct C { };
642///
643/// C *f(B* b) { return dynamic_cast<C*>(b); }
644bool CXXDynamicCastExpr::isAlwaysNull() const
645{
646 QualType SrcType = getSubExpr()->getType();
647 QualType DestType = getType();
648
649 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
650 SrcType = SrcPTy->getPointeeType();
651 DestType = DestType->castAs<PointerType>()->getPointeeType();
652 }
653
Alexis Hunt78e2b912012-06-19 23:44:55 +0000654 if (DestType->isVoidType())
655 return false;
656
Anders Carlsson267c0c92011-04-11 01:43:55 +0000657 const CXXRecordDecl *SrcRD =
658 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
659
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000660 if (!SrcRD->hasAttr<FinalAttr>())
661 return false;
662
Anders Carlsson267c0c92011-04-11 01:43:55 +0000663 const CXXRecordDecl *DestRD =
664 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
665
666 return !DestRD->isDerivedFrom(SrcRD);
667}
668
John McCallcf142162010-08-07 06:22:56 +0000669CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000670CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
671 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000672 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000673 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000674 SourceLocation RParenLoc,
675 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000676 unsigned PathSize = (BasePath ? BasePath->size() : 0);
677 void *Buffer =
678 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
679 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000680 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000681 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000682 if (PathSize) E->setCastPath(*BasePath);
683 return E;
684}
685
686CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000687CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000688 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
689 + PathSize * sizeof(CXXBaseSpecifier*));
690 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
691}
692
Craig Toppera31a8822013-08-22 07:09:37 +0000693CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000694 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000695 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000696 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000697 SourceLocation RParenLoc,
698 SourceRange AngleBrackets) {
699 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000700}
701
Craig Toppera31a8822013-08-22 07:09:37 +0000702CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000703 return new (C) CXXConstCastExpr(EmptyShell());
704}
705
706CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000707CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000708 TypeSourceInfo *Written, CastKind K, Expr *Op,
709 const CXXCastPath *BasePath,
710 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000711 unsigned PathSize = (BasePath ? BasePath->size() : 0);
712 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
713 + PathSize * sizeof(CXXBaseSpecifier*));
714 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000715 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000716 if (PathSize) E->setCastPath(*BasePath);
717 return E;
718}
719
720CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000721CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000722 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
723 + PathSize * sizeof(CXXBaseSpecifier*));
724 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
725}
726
Eli Friedman89fe0d52013-08-15 22:02:56 +0000727SourceLocation CXXFunctionalCastExpr::getLocStart() const {
728 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
729}
730
731SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
732 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
733}
734
Richard Smithc67fdd42012-03-07 08:35:16 +0000735UserDefinedLiteral::LiteralOperatorKind
736UserDefinedLiteral::getLiteralOperatorKind() const {
737 if (getNumArgs() == 0)
738 return LOK_Template;
739 if (getNumArgs() == 2)
740 return LOK_String;
741
742 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
743 QualType ParamTy =
744 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
745 if (ParamTy->isPointerType())
746 return LOK_Raw;
747 if (ParamTy->isAnyCharacterType())
748 return LOK_Character;
749 if (ParamTy->isIntegerType())
750 return LOK_Integer;
751 if (ParamTy->isFloatingType())
752 return LOK_Floating;
753
754 llvm_unreachable("unknown kind of literal operator");
755}
756
757Expr *UserDefinedLiteral::getCookedLiteral() {
758#ifndef NDEBUG
759 LiteralOperatorKind LOK = getLiteralOperatorKind();
760 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
761#endif
762 return getArg(0);
763}
764
765const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
766 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
767}
John McCallcf142162010-08-07 06:22:56 +0000768
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000769CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000770CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000771 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000772 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000773 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
774 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000775}
776
Craig Toppera31a8822013-08-22 07:09:37 +0000777CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000778 FieldDecl *Field, QualType T)
779 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
780 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
781 ? VK_XValue
782 : VK_RValue,
783 /*FIXME*/ OK_Ordinary, false, false, false, false),
784 Field(Field), Loc(Loc) {
785 assert(Field->hasInClassInitializer());
786}
787
Craig Toppera31a8822013-08-22 07:09:37 +0000788CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000789 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000790 return new (C) CXXTemporary(Destructor);
791}
792
Craig Toppera31a8822013-08-22 07:09:37 +0000793CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000794 CXXTemporary *Temp,
795 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000796 assert((SubExpr->getType()->isRecordType() ||
797 SubExpr->getType()->isArrayType()) &&
798 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000799
Douglas Gregora6e053e2010-12-15 01:34:56 +0000800 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000801}
802
Craig Toppera31a8822013-08-22 07:09:37 +0000803CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000804 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000805 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000806 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000807 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000808 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000809 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000810 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000811 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
812 Type->getType().getNonReferenceType(),
813 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000814 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000815 HadMultipleCandidates,
816 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000817 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000818 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000819}
820
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000821SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
822 return Type->getTypeLoc().getBeginLoc();
823}
824
825SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000826 SourceLocation Loc = getParenOrBraceRange().getEnd();
827 if (Loc.isInvalid() && getNumArgs())
828 Loc = getArg(getNumArgs()-1)->getLocEnd();
829 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000830}
Anders Carlsson6f287832009-04-21 02:22:11 +0000831
Craig Toppera31a8822013-08-22 07:09:37 +0000832CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000833 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000834 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000835 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000836 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000837 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000838 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000839 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000840 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000841 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000842 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000843 HadMultipleCandidates, ListInitialization,
844 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000845 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000846}
847
Craig Toppera31a8822013-08-22 07:09:37 +0000848CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
849 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000850 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000851 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000852 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000853 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000854 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000855 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000856 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000857 : Expr(SC, T, VK_RValue, OK_Ordinary,
858 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000859 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000860 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000861 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
862 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000863 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000864 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000865 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000866 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000867{
868 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000869 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000870
Benjamin Kramerc215e762012-08-24 11:54:20 +0000871 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000872 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000873
874 if (args[i]->isValueDependent())
875 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000876 if (args[i]->isInstantiationDependent())
877 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000878 if (args[i]->containsUnexpandedParameterPack())
879 ExprBits.ContainsUnexpandedParameterPack = true;
880
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000881 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000882 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000883 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000884}
885
Douglas Gregore31e6062012-02-07 10:09:13 +0000886LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
887 LambdaCaptureKind Kind, VarDecl *Var,
888 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000889 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000890{
891 unsigned Bits = 0;
892 if (Implicit)
893 Bits |= Capture_Implicit;
894
895 switch (Kind) {
896 case LCK_This:
897 assert(Var == 0 && "'this' capture cannot have a variable!");
898 break;
899
900 case LCK_ByCopy:
901 Bits |= Capture_ByCopy;
902 // Fall through
903 case LCK_ByRef:
904 assert(Var && "capture must have a variable!");
905 break;
Richard Smithba71c082013-05-16 06:20:58 +0000906
907 case LCK_Init:
908 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregore31e6062012-02-07 10:09:13 +0000909 }
Richard Smithba71c082013-05-16 06:20:58 +0000910 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000911}
912
Richard Smithba71c082013-05-16 06:20:58 +0000913LambdaExpr::Capture::Capture(FieldDecl *Field)
914 : DeclAndBits(Field,
915 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
916 Loc(Field->getLocation()), EllipsisLoc() {}
917
Douglas Gregore31e6062012-02-07 10:09:13 +0000918LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000919 Decl *D = DeclAndBits.getPointer();
920 if (!D)
Douglas Gregore31e6062012-02-07 10:09:13 +0000921 return LCK_This;
922
Richard Smithba71c082013-05-16 06:20:58 +0000923 if (isa<FieldDecl>(D))
924 return LCK_Init;
925
926 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000927}
928
James Dennettddd36ff2013-08-09 23:08:25 +0000929LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000930 SourceRange IntroducerRange,
931 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000932 SourceLocation CaptureDefaultLoc,
933 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000934 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000935 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000936 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000937 ArrayRef<VarDecl *> ArrayIndexVars,
938 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000939 SourceLocation ClosingBrace,
940 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000941 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
942 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000943 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000944 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000945 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000946 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000947 CaptureDefault(CaptureDefault),
948 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000949 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000950 ClosingBrace(ClosingBrace)
951{
952 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000953 CXXRecordDecl *Class = getLambdaClass();
954 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000955
956 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000957
958 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000959 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000960 Data.NumCaptures = NumCaptures;
961 Data.NumExplicitCaptures = 0;
962 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
963 Capture *ToCapture = Data.Captures;
964 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
965 if (Captures[I].isExplicit())
966 ++Data.NumExplicitCaptures;
967
968 *ToCapture++ = Captures[I];
969 }
970
971 // Copy initialization expressions for the non-static data members.
972 Stmt **Stored = getStoredStmts();
973 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
974 *Stored++ = CaptureInits[I];
975
976 // Copy the body of the lambda.
977 *Stored++ = getCallOperator()->getBody();
978
979 // Copy the array index variables, if any.
980 HasArrayIndexVars = !ArrayIndexVars.empty();
981 if (HasArrayIndexVars) {
982 assert(ArrayIndexStarts.size() == NumCaptures);
983 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
984 sizeof(VarDecl *) * ArrayIndexVars.size());
985 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
986 sizeof(unsigned) * Captures.size());
987 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000988 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000989}
990
Craig Toppera31a8822013-08-22 07:09:37 +0000991LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000992 CXXRecordDecl *Class,
993 SourceRange IntroducerRange,
994 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000995 SourceLocation CaptureDefaultLoc,
996 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000997 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000998 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000999 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +00001000 ArrayRef<VarDecl *> ArrayIndexVars,
1001 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001002 SourceLocation ClosingBrace,
1003 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001004 // Determine the type of the expression (i.e., the type of the
1005 // function object we're creating).
1006 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001007
Douglas Gregore5561632012-02-13 17:20:40 +00001008 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001009 if (!ArrayIndexVars.empty()) {
1010 Size += sizeof(unsigned) * (Captures.size() + 1);
1011 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001012 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001013 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1014 }
Douglas Gregore5561632012-02-13 17:20:40 +00001015 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001016 return new (Mem) LambdaExpr(T, IntroducerRange,
1017 CaptureDefault, CaptureDefaultLoc, Captures,
1018 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001019 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001020 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001021}
1022
Craig Toppera31a8822013-08-22 07:09:37 +00001023LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1024 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001025 unsigned NumArrayIndexVars) {
1026 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1027 if (NumArrayIndexVars)
1028 Size += sizeof(VarDecl) * NumArrayIndexVars
1029 + sizeof(unsigned) * (NumCaptures + 1);
1030 void *Mem = C.Allocate(Size);
1031 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1032}
1033
Douglas Gregorc8a73492012-02-13 15:44:47 +00001034LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001035 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001036}
1037
1038LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001039 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001040}
1041
1042LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1043 return capture_begin();
1044}
1045
1046LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1047 struct CXXRecordDecl::LambdaDefinitionData &Data
1048 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001049 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001050}
1051
1052LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1053 return explicit_capture_end();
1054}
1055
1056LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1057 return capture_end();
1058}
1059
Douglas Gregor54fcea62012-02-13 16:35:30 +00001060ArrayRef<VarDecl *>
1061LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001062 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001063
1064 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001065 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1066 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001067 VarDecl **IndexVars = getArrayIndexVars();
1068 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001069 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1070 IndexVars + IndexStarts[Index + 1]);
1071}
1072
Douglas Gregore31e6062012-02-07 10:09:13 +00001073CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1074 return getType()->getAsCXXRecordDecl();
1075}
1076
1077CXXMethodDecl *LambdaExpr::getCallOperator() const {
1078 CXXRecordDecl *Record = getLambdaClass();
Manuel Klimek2fdbea22013-08-22 12:12:24 +00001079 DeclarationName Name
1080 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1081 DeclContext::lookup_result Calls = Record->lookup(Name);
1082 assert(!Calls.empty() && "Missing lambda call operator!");
1083 assert(Calls.size() == 1 && "More than one lambda call operator!");
1084 CXXMethodDecl *Result = cast<CXXMethodDecl>(Calls.front());
1085 return Result;
Douglas Gregore31e6062012-02-07 10:09:13 +00001086}
1087
Douglas Gregor99ae8062012-02-14 17:54:36 +00001088CompoundStmt *LambdaExpr::getBody() const {
1089 if (!getStoredStmts()[NumCaptures])
1090 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1091
1092 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1093}
1094
Douglas Gregore31e6062012-02-07 10:09:13 +00001095bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001096 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001097}
1098
John McCall28fc7092011-11-10 05:35:25 +00001099ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1100 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001101 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001102 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001103 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001104 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001105 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001106 SubExpr(subexpr) {
1107 ExprWithCleanupsBits.NumObjects = objects.size();
1108 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1109 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001110}
1111
Craig Toppera31a8822013-08-22 07:09:37 +00001112ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001113 ArrayRef<CleanupObject> objects) {
1114 size_t size = sizeof(ExprWithCleanups)
1115 + objects.size() * sizeof(CleanupObject);
1116 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1117 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001118}
1119
John McCall28fc7092011-11-10 05:35:25 +00001120ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1121 : Expr(ExprWithCleanupsClass, empty) {
1122 ExprWithCleanupsBits.NumObjects = numObjects;
1123}
Chris Lattnercba86142010-05-10 00:25:06 +00001124
Craig Toppera31a8822013-08-22 07:09:37 +00001125ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1126 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001127 unsigned numObjects) {
1128 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1129 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1130 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001131}
1132
Douglas Gregor2b88c112010-09-08 00:15:04 +00001133CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001134 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001135 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001136 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001137 : Expr(CXXUnresolvedConstructExprClass,
1138 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001139 (Type->getType()->isLValueReferenceType() ? VK_LValue
1140 :Type->getType()->isRValueReferenceType()? VK_XValue
1141 :VK_RValue),
1142 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001143 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001144 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001145 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001146 LParenLoc(LParenLoc),
1147 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001148 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001149 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001150 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001151 if (Args[I]->containsUnexpandedParameterPack())
1152 ExprBits.ContainsUnexpandedParameterPack = true;
1153
1154 StoredArgs[I] = Args[I];
1155 }
Douglas Gregorce934142009-05-20 18:46:25 +00001156}
1157
1158CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001159CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001160 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001161 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001162 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001163 SourceLocation RParenLoc) {
1164 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001165 sizeof(Expr *) * Args.size());
1166 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001167}
1168
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001169CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001170CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001171 Stmt::EmptyShell Empty;
1172 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1173 sizeof(Expr *) * NumArgs);
1174 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1175}
1176
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001177SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1178 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001179}
1180
Craig Toppera31a8822013-08-22 07:09:37 +00001181CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001182 Expr *Base, QualType BaseType,
1183 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001184 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001185 NestedNameSpecifierLoc QualifierLoc,
1186 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001187 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001188 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001189 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001190 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001191 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001192 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001193 (QualifierLoc &&
1194 QualifierLoc.getNestedNameSpecifier()
1195 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001196 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001197 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001198 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001199 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001200 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001201 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001202 if (TemplateArgs) {
1203 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001204 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001205 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001206 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1207 Dependent,
1208 InstantiationDependent,
1209 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001210 if (ContainsUnexpandedParameterPack)
1211 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001212 } else if (TemplateKWLoc.isValid()) {
1213 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001214 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001215}
1216
Craig Toppera31a8822013-08-22 07:09:37 +00001217CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218 Expr *Base, QualType BaseType,
1219 bool IsArrow,
1220 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001221 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001222 NamedDecl *FirstQualifierFoundInScope,
1223 DeclarationNameInfo MemberNameInfo)
1224 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001225 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001226 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001227 (QualifierLoc &&
1228 QualifierLoc.getNestedNameSpecifier()->
1229 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001230 MemberNameInfo.containsUnexpandedParameterPack())),
1231 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001232 HasTemplateKWAndArgsInfo(false),
1233 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001234 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1235 MemberNameInfo(MemberNameInfo) { }
1236
John McCall8cd78132009-11-19 22:55:06 +00001237CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001238CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001239 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001240 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001241 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001242 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001243 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001244 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001245 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001246 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001247 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1248 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001249 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001250 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001251 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001252
Abramo Bagnara7945c982012-01-27 09:46:47 +00001253 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1254 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1255 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001256
Chris Lattner5c0b4052010-10-30 05:14:06 +00001257 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001258 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1259 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001260 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001261 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001262 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001263 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001264}
1265
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001266CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001267CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001268 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001269 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001270 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001271 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001272 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001273 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001274 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001275
1276 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001277 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001278 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001279 CXXDependentScopeMemberExpr *E
1280 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001281 0, SourceLocation(),
1282 NestedNameSpecifierLoc(),
1283 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001284 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001285 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001286 return E;
1287}
1288
Douglas Gregor0da1d432011-02-28 20:01:57 +00001289bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1290 if (Base == 0)
1291 return true;
1292
Douglas Gregor25b7e052011-03-02 21:06:53 +00001293 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001294}
1295
John McCall0009fcc2011-04-26 20:42:42 +00001296static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1297 UnresolvedSetIterator end) {
1298 do {
1299 NamedDecl *decl = *begin;
1300 if (isa<UnresolvedUsingValueDecl>(decl))
1301 return false;
1302 if (isa<UsingShadowDecl>(decl))
1303 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1304
1305 // Unresolved member expressions should only contain methods and
1306 // method templates.
1307 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1308
1309 if (isa<FunctionTemplateDecl>(decl))
1310 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1311 if (cast<CXXMethodDecl>(decl)->isStatic())
1312 return false;
1313 } while (++begin != end);
1314
1315 return true;
1316}
1317
Craig Toppera31a8822013-08-22 07:09:37 +00001318UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001319 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001320 Expr *Base, QualType BaseType,
1321 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001322 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001323 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001324 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001325 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001326 const TemplateArgumentListInfo *TemplateArgs,
1327 UnresolvedSetIterator Begin,
1328 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001329 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1330 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001331 // Dependent
1332 ((Base && Base->isTypeDependent()) ||
1333 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001334 ((Base && Base->isInstantiationDependent()) ||
1335 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001336 // Contains unexpanded parameter pack
1337 ((Base && Base->containsUnexpandedParameterPack()) ||
1338 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001339 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1340 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001341
1342 // Check whether all of the members are non-static member functions,
1343 // and if so, mark give this bound-member type instead of overload type.
1344 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1345 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001346}
1347
Douglas Gregor0da1d432011-02-28 20:01:57 +00001348bool UnresolvedMemberExpr::isImplicitAccess() const {
1349 if (Base == 0)
1350 return true;
1351
Douglas Gregor25b7e052011-03-02 21:06:53 +00001352 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001353}
1354
John McCall10eae182009-11-30 22:42:35 +00001355UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001356UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001357 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001358 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001359 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001360 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001361 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001362 const TemplateArgumentListInfo *TemplateArgs,
1363 UnresolvedSetIterator Begin,
1364 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001365 std::size_t size = sizeof(UnresolvedMemberExpr);
1366 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001367 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1368 else if (TemplateKWLoc.isValid())
1369 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001370
Chris Lattner5c0b4052010-10-30 05:14:06 +00001371 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001372 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001373 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001374 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001375 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001376}
1377
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001378UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001379UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1380 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001381 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001382 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001383 if (HasTemplateKWAndArgsInfo)
1384 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001385
Chris Lattner5c0b4052010-10-30 05:14:06 +00001386 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001387 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001388 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001389 return E;
1390}
1391
John McCall58cc69d2010-01-27 01:50:18 +00001392CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1393 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1394
1395 // If there was a nested name specifier, it names the naming class.
1396 // It can't be dependent: after all, we were actually able to do the
1397 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001398 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001399 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001400 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001401 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001402 Record = T->getAsCXXRecordDecl();
1403 assert(Record && "qualifier in member expression does not name record");
1404 }
John McCall58cc69d2010-01-27 01:50:18 +00001405 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001406 else {
John McCall58cc69d2010-01-27 01:50:18 +00001407 QualType BaseType = getBaseType().getNonReferenceType();
1408 if (isArrow()) {
1409 const PointerType *PT = BaseType->getAs<PointerType>();
1410 assert(PT && "base of arrow member access is not pointer");
1411 BaseType = PT->getPointeeType();
1412 }
1413
Douglas Gregor9262f472010-04-27 18:19:34 +00001414 Record = BaseType->getAsCXXRecordDecl();
1415 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001416 }
1417
Douglas Gregor9262f472010-04-27 18:19:34 +00001418 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001419}
1420
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001421SubstNonTypeTemplateParmPackExpr::
1422SubstNonTypeTemplateParmPackExpr(QualType T,
1423 NonTypeTemplateParmDecl *Param,
1424 SourceLocation NameLoc,
1425 const TemplateArgument &ArgPack)
1426 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001427 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001428 Param(Param), Arguments(ArgPack.pack_begin()),
1429 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1430
1431TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1432 return TemplateArgument(Arguments, NumArguments);
1433}
1434
Richard Smithb15fe3a2012-09-12 00:56:43 +00001435FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1436 SourceLocation NameLoc,
1437 unsigned NumParams,
1438 Decl * const *Params)
1439 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1440 true, true, true, true),
1441 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1442 if (Params)
1443 std::uninitialized_copy(Params, Params + NumParams,
1444 reinterpret_cast<Decl**>(this+1));
1445}
1446
1447FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001448FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001449 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001450 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001451 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1452 sizeof(ParmVarDecl*) * Params.size()))
1453 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1454}
1455
1456FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001457FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1458 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001459 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1460 sizeof(ParmVarDecl*) * NumParams))
1461 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1462}
1463
Douglas Gregor29c42f22012-02-24 07:38:34 +00001464TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1465 ArrayRef<TypeSourceInfo *> Args,
1466 SourceLocation RParenLoc,
1467 bool Value)
1468 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1469 /*TypeDependent=*/false,
1470 /*ValueDependent=*/false,
1471 /*InstantiationDependent=*/false,
1472 /*ContainsUnexpandedParameterPack=*/false),
1473 Loc(Loc), RParenLoc(RParenLoc)
1474{
1475 TypeTraitExprBits.Kind = Kind;
1476 TypeTraitExprBits.Value = Value;
1477 TypeTraitExprBits.NumArgs = Args.size();
1478
1479 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1480
1481 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1482 if (Args[I]->getType()->isDependentType())
1483 setValueDependent(true);
1484 if (Args[I]->getType()->isInstantiationDependentType())
1485 setInstantiationDependent(true);
1486 if (Args[I]->getType()->containsUnexpandedParameterPack())
1487 setContainsUnexpandedParameterPack(true);
1488
1489 ToArgs[I] = Args[I];
1490 }
1491}
1492
Craig Toppera31a8822013-08-22 07:09:37 +00001493TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001494 SourceLocation Loc,
1495 TypeTrait Kind,
1496 ArrayRef<TypeSourceInfo *> Args,
1497 SourceLocation RParenLoc,
1498 bool Value) {
1499 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1500 void *Mem = C.Allocate(Size);
1501 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1502}
1503
Craig Toppera31a8822013-08-22 07:09:37 +00001504TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001505 unsigned NumArgs) {
1506 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1507 void *Mem = C.Allocate(Size);
1508 return new (Mem) TypeTraitExpr(EmptyShell());
1509}
1510
David Blaikie68e081d2011-12-20 02:48:34 +00001511void ArrayTypeTraitExpr::anchor() { }