blob: 3738c0e4f2c9a59c686ad0682b350db6a0e5ccef [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
David Majnemer143c55e2013-09-27 07:04:31 +000043QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000044 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000045 Qualifiers Quals;
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000048}
49
David Majnemer143c55e2013-09-27 07:04:31 +000050QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000051 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000052 Qualifiers Quals;
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000055}
56
Nico Webercf4ff5862012-10-11 10:13:44 +000057// static
David Majnemer59c0ec22013-09-07 06:59:46 +000058UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
Nico Webercf4ff5862012-10-11 10:13:44 +000060 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
David Majnemer68c880b2013-09-27 07:57:34 +000065 Ty = Ty->getBaseElementTypeUnsafe();
Nico Webercf4ff5862012-10-11 10:13:44 +000066
67 // Loop all record redeclaration looking for an uuid attribute.
68 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
David Majnemer59c0ec22013-09-07 06:59:46 +000069 if (!RD)
70 return 0;
71
David Majnemer5d22e7e2013-09-07 07:11:04 +000072 // __uuidof can grab UUIDs from template arguments.
David Majnemer59c0ec22013-09-07 06:59:46 +000073 if (ClassTemplateSpecializationDecl *CTSD =
74 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
75 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
76 UuidAttr *UuidForRD = 0;
77
78 for (unsigned I = 0, N = TAL.size(); I != N; ++I) {
79 const TemplateArgument &TA = TAL[I];
80 bool SeenMultipleGUIDs = false;
81
82 UuidAttr *UuidForTA = 0;
83 if (TA.getKind() == TemplateArgument::Type)
84 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
85 else if (TA.getKind() == TemplateArgument::Declaration)
86 UuidForTA =
87 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
88
89 // If the template argument has a UUID, there are three cases:
90 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000091 // - This is a different UUID than previously seen for this RecordDecl.
92 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000093 if (UuidForTA) {
94 if (!UuidForRD)
95 UuidForRD = UuidForTA;
96 else if (UuidForRD != UuidForTA)
97 SeenMultipleGUIDs = true;
98 }
99
100 // Seeing multiple UUIDs means that we couldn't find a UUID
101 if (SeenMultipleGUIDs) {
102 if (RDHasMultipleGUIDsPtr)
103 *RDHasMultipleGUIDsPtr = true;
104 return 0;
105 }
106 }
107
108 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000109 }
110
111 for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
112 E = RD->redecls_end();
113 I != E; ++I)
114 if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
115 return Uuid;
Nico Webercf4ff5862012-10-11 10:13:44 +0000116
117 return 0;
118}
119
David Majnemer8eaab6f2013-08-13 06:32:20 +0000120StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
121 StringRef Uuid;
122 if (isTypeOperand())
David Majnemer143c55e2013-09-27 07:04:31 +0000123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
David Majnemer8eaab6f2013-08-13 06:32:20 +0000124 else {
125 // Special case: __uuidof(0) means an all-zero GUID.
126 Expr *Op = getExprOperand();
127 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
128 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
129 else
130 Uuid = "00000000-0000-0000-0000-000000000000";
131 }
132 return Uuid;
133}
134
Douglas Gregor747eb782010-07-08 06:14:04 +0000135// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000136SourceLocation CXXScalarValueInitExpr::getLocStart() const {
137 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000138}
139
Sebastian Redlbd150f42008-11-21 19:14:01 +0000140// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000141CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
142 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000143 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000144 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000145 SourceRange typeIdParens, Expr *arraySize,
146 InitializationStyle initializationStyle,
147 Expr *initializer, QualType ty,
148 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000149 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000150 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000151 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000152 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000153 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +0000154 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
155 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000156 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000157 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +0000158 assert((initializer != 0 || initializationStyle == NoInit) &&
159 "Only NoInit can have no initializer.");
160 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Benjamin Kramerc215e762012-08-24 11:54:20 +0000161 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000162 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000163 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000164 if (arraySize->isInstantiationDependent())
165 ExprBits.InstantiationDependent = true;
166
Douglas Gregora6e053e2010-12-15 01:34:56 +0000167 if (arraySize->containsUnexpandedParameterPack())
168 ExprBits.ContainsUnexpandedParameterPack = true;
169
Sebastian Redl351bb782008-12-02 14:43:59 +0000170 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000171 }
172
Sebastian Redl6047f072012-02-16 12:22:20 +0000173 if (initializer) {
174 if (initializer->isInstantiationDependent())
175 ExprBits.InstantiationDependent = true;
176
177 if (initializer->containsUnexpandedParameterPack())
178 ExprBits.ContainsUnexpandedParameterPack = true;
179
180 SubExprs[i++] = initializer;
181 }
182
Benjamin Kramerc215e762012-08-24 11:54:20 +0000183 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000184 if (placementArgs[j]->isInstantiationDependent())
185 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000186 if (placementArgs[j]->containsUnexpandedParameterPack())
187 ExprBits.ContainsUnexpandedParameterPack = true;
188
Sebastian Redlbd150f42008-11-21 19:14:01 +0000189 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000190 }
David Blaikie3a0de212012-11-08 22:53:48 +0000191
192 switch (getInitializationStyle()) {
193 case CallInit:
194 this->Range.setEnd(DirectInitRange.getEnd()); break;
195 case ListInit:
196 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000197 default:
198 if (TypeIdParens.isValid())
199 this->Range.setEnd(TypeIdParens.getEnd());
200 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000201 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000202}
203
Craig Toppera31a8822013-08-22 07:09:37 +0000204void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000205 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000206 assert(SubExprs == 0 && "SubExprs already allocated");
207 Array = isArray;
208 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000209
210 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000211 SubExprs = new (C) Stmt*[TotalSize];
212}
213
Craig Toppera31a8822013-08-22 07:09:37 +0000214bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000215 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000216 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000217}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000218
Sebastian Redlbd150f42008-11-21 19:14:01 +0000219// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000220QualType CXXDeleteExpr::getDestroyedType() const {
221 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000222 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000223 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000224
225 if (ArgType->isDependentType() && !ArgType->isPointerType())
226 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000227
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000228 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000229}
230
Douglas Gregorad8a3362009-09-04 17:36:40 +0000231// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000232PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
233 : Type(Info)
234{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000235 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000236}
237
Craig Toppera31a8822013-08-22 07:09:37 +0000238CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000239 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
240 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
241 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
242 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000243 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000244 Context.getPointerType(Context.getFunctionType(
245 Context.VoidTy, None,
246 FunctionProtoType::ExtProtoInfo(
247 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000248 VK_RValue, OK_Ordinary,
249 /*isTypeDependent=*/(Base->isTypeDependent() ||
250 (DestroyedType.getTypeSourceInfo() &&
251 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000252 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000253 (Base->isInstantiationDependent() ||
254 (QualifierLoc &&
255 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
256 (ScopeType &&
257 ScopeType->getType()->isInstantiationDependentType()) ||
258 (DestroyedType.getTypeSourceInfo() &&
259 DestroyedType.getTypeSourceInfo()->getType()
260 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000261 // ContainsUnexpandedParameterPack
262 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000263 (QualifierLoc &&
264 QualifierLoc.getNestedNameSpecifier()
265 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000266 (ScopeType &&
267 ScopeType->getType()->containsUnexpandedParameterPack()) ||
268 (DestroyedType.getTypeSourceInfo() &&
269 DestroyedType.getTypeSourceInfo()->getType()
270 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000271 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000272 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000273 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
274 DestroyedType(DestroyedType) { }
275
Douglas Gregor678f90d2010-02-25 01:56:36 +0000276QualType CXXPseudoDestructorExpr::getDestroyedType() const {
277 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
278 return TInfo->getType();
279
280 return QualType();
281}
282
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000283SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000284 SourceLocation End = DestroyedType.getLocation();
285 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000286 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000287 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000288}
289
John McCalld14a8642009-11-21 08:51:07 +0000290// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000291UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000292UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000293 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000294 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000295 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000296 const DeclarationNameInfo &NameInfo,
297 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000298 const TemplateArgumentListInfo *Args,
299 UnresolvedSetIterator Begin,
300 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000301{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000302 assert(Args || TemplateKWLoc.isValid());
303 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000304 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000305 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000306 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
307 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000308 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000309 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000310}
311
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000312UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000313UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000314 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000315 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000316 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000317 if (HasTemplateKWAndArgsInfo)
318 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000319
Chris Lattner5c0b4052010-10-30 05:14:06 +0000320 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000321 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000322 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000323 return E;
324}
325
Craig Toppera31a8822013-08-22 07:09:37 +0000326OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000327 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000328 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000329 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000330 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000331 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000332 UnresolvedSetIterator End,
333 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000334 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000335 bool KnownContainsUnexpandedParameterPack)
336 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
337 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000338 (KnownInstantiationDependent ||
339 NameInfo.isInstantiationDependent() ||
340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000342 (KnownContainsUnexpandedParameterPack ||
343 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000344 (QualifierLoc &&
345 QualifierLoc.getNestedNameSpecifier()
346 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000347 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
348 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000349 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000350{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000351 NumResults = End - Begin;
352 if (NumResults) {
353 // Determine whether this expression is type-dependent.
354 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
355 if ((*I)->getDeclContext()->isDependentContext() ||
356 isa<UnresolvedUsingValueDecl>(*I)) {
357 ExprBits.TypeDependent = true;
358 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000359 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000360 }
361 }
362
363 Results = static_cast<DeclAccessPair *>(
364 C.Allocate(sizeof(DeclAccessPair) * NumResults,
365 llvm::alignOf<DeclAccessPair>()));
366 memcpy(Results, &*Begin.getIterator(),
367 NumResults * sizeof(DeclAccessPair));
368 }
369
370 // If we have explicit template arguments, check for dependent
371 // template arguments and whether they contain any unexpanded pack
372 // expansions.
373 if (TemplateArgs) {
374 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000375 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000376 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
378 Dependent,
379 InstantiationDependent,
380 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000381
382 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000383 ExprBits.TypeDependent = true;
384 ExprBits.ValueDependent = true;
385 }
386 if (InstantiationDependent)
387 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388 if (ContainsUnexpandedParameterPack)
389 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000390 } else if (TemplateKWLoc.isValid()) {
391 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000392 }
393
394 if (isTypeDependent())
395 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000396}
397
Craig Toppera31a8822013-08-22 07:09:37 +0000398void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000399 UnresolvedSetIterator Begin,
400 UnresolvedSetIterator End) {
401 assert(Results == 0 && "Results already initialized!");
402 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000403 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000404 Results = static_cast<DeclAccessPair *>(
405 C.Allocate(sizeof(DeclAccessPair) * NumResults,
406
407 llvm::alignOf<DeclAccessPair>()));
408 memcpy(Results, &*Begin.getIterator(),
409 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000410 }
411}
412
John McCall8c12dc42010-04-22 18:44:12 +0000413CXXRecordDecl *OverloadExpr::getNamingClass() const {
414 if (isa<UnresolvedLookupExpr>(this))
415 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
416 else
417 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
418}
419
John McCall8cd78132009-11-19 22:55:06 +0000420// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000421DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000422 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000423 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000424 const DeclarationNameInfo &NameInfo,
425 const TemplateArgumentListInfo *Args)
426 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
427 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000428 (NameInfo.isInstantiationDependent() ||
429 (QualifierLoc &&
430 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000431 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000432 (QualifierLoc &&
433 QualifierLoc.getNestedNameSpecifier()
434 ->containsUnexpandedParameterPack()))),
435 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000436 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000437{
438 if (Args) {
439 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000440 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000441 bool ContainsUnexpandedParameterPack
442 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000443 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
444 Dependent,
445 InstantiationDependent,
446 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000447 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000448 } else if (TemplateKWLoc.isValid()) {
449 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000450 }
451}
452
John McCalle66edc12009-11-24 19:00:30 +0000453DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000454DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000455 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000456 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000457 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000458 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000459 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000460 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000461 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000462 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
463 else if (TemplateKWLoc.isValid())
464 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000465 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000466 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
467 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000468}
469
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000470DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000471DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000472 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000473 unsigned NumTemplateArgs) {
474 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000475 if (HasTemplateKWAndArgsInfo)
476 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000477 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000478 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000479 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000480 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000481 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000482 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000483 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000484}
485
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000486SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000487 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000488 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
489 return Loc;
490}
491
492SourceLocation CXXConstructExpr::getLocEnd() const {
493 if (isa<CXXTemporaryObjectExpr>(this))
494 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000495
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000496 if (ParenOrBraceRange.isValid())
497 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000498
499 SourceLocation End = Loc;
500 for (unsigned I = getNumArgs(); I > 0; --I) {
501 const Expr *Arg = getArg(I-1);
502 if (!Arg->isDefaultArgument()) {
503 SourceLocation NewEnd = Arg->getLocEnd();
504 if (NewEnd.isValid()) {
505 End = NewEnd;
506 break;
507 }
508 }
509 }
510
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000511 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000512}
513
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000514SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000515 OverloadedOperatorKind Kind = getOperator();
516 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
517 if (getNumArgs() == 1)
518 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000519 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000520 else
521 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000522 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000523 } else if (Kind == OO_Arrow) {
524 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000525 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000526 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000527 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000528 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000529 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000530 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000531 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000532 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000533 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000534 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000535 }
536}
537
Ted Kremenek98a24e32011-03-30 17:41:19 +0000538Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000539 const Expr *Callee = getCallee()->IgnoreParens();
540 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000541 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000542 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
543 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
544 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000545
546 // FIXME: Will eventually need to cope with member pointers.
547 return 0;
548}
549
Ted Kremenek98a24e32011-03-30 17:41:19 +0000550CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
551 if (const MemberExpr *MemExpr =
552 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
553 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
554
555 // FIXME: Will eventually need to cope with member pointers.
556 return 0;
557}
558
559
David Blaikiec0f58662012-05-03 16:25:49 +0000560CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000561 Expr* ThisArg = getImplicitObjectArgument();
562 if (!ThisArg)
563 return 0;
564
565 if (ThisArg->getType()->isAnyPointerType())
566 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
567
568 return ThisArg->getType()->getAsCXXRecordDecl();
569}
570
Douglas Gregoref986e82009-11-12 15:31:47 +0000571
Douglas Gregore200adc2008-10-27 19:41:14 +0000572//===----------------------------------------------------------------------===//
573// Named casts
574//===----------------------------------------------------------------------===//
575
576/// getCastName - Get the name of the C++ cast being used, e.g.,
577/// "static_cast", "dynamic_cast", "reinterpret_cast", or
578/// "const_cast". The returned pointer must not be freed.
579const char *CXXNamedCastExpr::getCastName() const {
580 switch (getStmtClass()) {
581 case CXXStaticCastExprClass: return "static_cast";
582 case CXXDynamicCastExprClass: return "dynamic_cast";
583 case CXXReinterpretCastExprClass: return "reinterpret_cast";
584 case CXXConstCastExprClass: return "const_cast";
585 default: return "<invalid cast>";
586 }
587}
Douglas Gregordd04d332009-01-16 18:33:17 +0000588
Craig Toppera31a8822013-08-22 07:09:37 +0000589CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000590 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000591 CastKind K, Expr *Op,
592 const CXXCastPath *BasePath,
593 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000594 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000595 SourceLocation RParenLoc,
596 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000597 unsigned PathSize = (BasePath ? BasePath->size() : 0);
598 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
599 + PathSize * sizeof(CXXBaseSpecifier*));
600 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000601 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000602 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000603 if (PathSize) E->setCastPath(*BasePath);
604 return E;
605}
606
Craig Toppera31a8822013-08-22 07:09:37 +0000607CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000608 unsigned PathSize) {
609 void *Buffer =
610 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
611 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
612}
613
Craig Toppera31a8822013-08-22 07:09:37 +0000614CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000615 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000616 CastKind K, Expr *Op,
617 const CXXCastPath *BasePath,
618 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000619 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000620 SourceLocation RParenLoc,
621 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000622 unsigned PathSize = (BasePath ? BasePath->size() : 0);
623 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
624 + PathSize * sizeof(CXXBaseSpecifier*));
625 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000626 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000627 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000628 if (PathSize) E->setCastPath(*BasePath);
629 return E;
630}
631
Craig Toppera31a8822013-08-22 07:09:37 +0000632CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000633 unsigned PathSize) {
634 void *Buffer =
635 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
636 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
637}
638
Anders Carlsson267c0c92011-04-11 01:43:55 +0000639/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
640/// to always be null. For example:
641///
642/// struct A { };
643/// struct B final : A { };
644/// struct C { };
645///
646/// C *f(B* b) { return dynamic_cast<C*>(b); }
647bool CXXDynamicCastExpr::isAlwaysNull() const
648{
649 QualType SrcType = getSubExpr()->getType();
650 QualType DestType = getType();
651
652 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
653 SrcType = SrcPTy->getPointeeType();
654 DestType = DestType->castAs<PointerType>()->getPointeeType();
655 }
656
Alexis Hunt78e2b912012-06-19 23:44:55 +0000657 if (DestType->isVoidType())
658 return false;
659
Anders Carlsson267c0c92011-04-11 01:43:55 +0000660 const CXXRecordDecl *SrcRD =
661 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
662
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000663 if (!SrcRD->hasAttr<FinalAttr>())
664 return false;
665
Anders Carlsson267c0c92011-04-11 01:43:55 +0000666 const CXXRecordDecl *DestRD =
667 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
668
669 return !DestRD->isDerivedFrom(SrcRD);
670}
671
John McCallcf142162010-08-07 06:22:56 +0000672CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000673CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
674 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000675 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000676 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000677 SourceLocation RParenLoc,
678 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
680 void *Buffer =
681 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
682 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000683 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000684 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000685 if (PathSize) E->setCastPath(*BasePath);
686 return E;
687}
688
689CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000690CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000691 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
692 + PathSize * sizeof(CXXBaseSpecifier*));
693 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
694}
695
Craig Toppera31a8822013-08-22 07:09:37 +0000696CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000697 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000698 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000699 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000700 SourceLocation RParenLoc,
701 SourceRange AngleBrackets) {
702 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000703}
704
Craig Toppera31a8822013-08-22 07:09:37 +0000705CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000706 return new (C) CXXConstCastExpr(EmptyShell());
707}
708
709CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000710CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000711 TypeSourceInfo *Written, CastKind K, Expr *Op,
712 const CXXCastPath *BasePath,
713 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000714 unsigned PathSize = (BasePath ? BasePath->size() : 0);
715 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
716 + PathSize * sizeof(CXXBaseSpecifier*));
717 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000718 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000719 if (PathSize) E->setCastPath(*BasePath);
720 return E;
721}
722
723CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000724CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000725 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
726 + PathSize * sizeof(CXXBaseSpecifier*));
727 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
728}
729
Eli Friedman89fe0d52013-08-15 22:02:56 +0000730SourceLocation CXXFunctionalCastExpr::getLocStart() const {
731 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
732}
733
734SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
735 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
736}
737
Richard Smithc67fdd42012-03-07 08:35:16 +0000738UserDefinedLiteral::LiteralOperatorKind
739UserDefinedLiteral::getLiteralOperatorKind() const {
740 if (getNumArgs() == 0)
741 return LOK_Template;
742 if (getNumArgs() == 2)
743 return LOK_String;
744
745 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
746 QualType ParamTy =
747 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
748 if (ParamTy->isPointerType())
749 return LOK_Raw;
750 if (ParamTy->isAnyCharacterType())
751 return LOK_Character;
752 if (ParamTy->isIntegerType())
753 return LOK_Integer;
754 if (ParamTy->isFloatingType())
755 return LOK_Floating;
756
757 llvm_unreachable("unknown kind of literal operator");
758}
759
760Expr *UserDefinedLiteral::getCookedLiteral() {
761#ifndef NDEBUG
762 LiteralOperatorKind LOK = getLiteralOperatorKind();
763 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
764#endif
765 return getArg(0);
766}
767
768const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
769 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
770}
John McCallcf142162010-08-07 06:22:56 +0000771
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000772CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000773CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000774 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000775 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000776 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
777 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000778}
779
Craig Toppera31a8822013-08-22 07:09:37 +0000780CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000781 FieldDecl *Field, QualType T)
782 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
783 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
784 ? VK_XValue
785 : VK_RValue,
786 /*FIXME*/ OK_Ordinary, false, false, false, false),
787 Field(Field), Loc(Loc) {
788 assert(Field->hasInClassInitializer());
789}
790
Craig Toppera31a8822013-08-22 07:09:37 +0000791CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000792 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000793 return new (C) CXXTemporary(Destructor);
794}
795
Craig Toppera31a8822013-08-22 07:09:37 +0000796CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000797 CXXTemporary *Temp,
798 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000799 assert((SubExpr->getType()->isRecordType() ||
800 SubExpr->getType()->isArrayType()) &&
801 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000802
Douglas Gregora6e053e2010-12-15 01:34:56 +0000803 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000804}
805
Craig Toppera31a8822013-08-22 07:09:37 +0000806CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000807 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000808 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000809 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000810 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000811 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000812 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000813 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000814 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
815 Type->getType().getNonReferenceType(),
816 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000817 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000818 HadMultipleCandidates,
819 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000820 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000821 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000822}
823
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000824SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
825 return Type->getTypeLoc().getBeginLoc();
826}
827
828SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000829 SourceLocation Loc = getParenOrBraceRange().getEnd();
830 if (Loc.isInvalid() && getNumArgs())
831 Loc = getArg(getNumArgs()-1)->getLocEnd();
832 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000833}
Anders Carlsson6f287832009-04-21 02:22:11 +0000834
Craig Toppera31a8822013-08-22 07:09:37 +0000835CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000836 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000837 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000838 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000839 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000840 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000841 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000842 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000843 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000844 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000845 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000846 HadMultipleCandidates, ListInitialization,
847 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000848 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000849}
850
Craig Toppera31a8822013-08-22 07:09:37 +0000851CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
852 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000853 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000854 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000855 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000856 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000857 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000858 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000859 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000860 : Expr(SC, T, VK_RValue, OK_Ordinary,
861 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000862 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000863 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000864 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
865 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000866 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000867 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000868 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000869 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000870{
871 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000872 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000873
Benjamin Kramerc215e762012-08-24 11:54:20 +0000874 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000875 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000876
877 if (args[i]->isValueDependent())
878 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000879 if (args[i]->isInstantiationDependent())
880 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000881 if (args[i]->containsUnexpandedParameterPack())
882 ExprBits.ContainsUnexpandedParameterPack = true;
883
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000884 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000885 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000886 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000887}
888
Douglas Gregore31e6062012-02-07 10:09:13 +0000889LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
890 LambdaCaptureKind Kind, VarDecl *Var,
891 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000892 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000893{
894 unsigned Bits = 0;
895 if (Implicit)
896 Bits |= Capture_Implicit;
897
898 switch (Kind) {
899 case LCK_This:
900 assert(Var == 0 && "'this' capture cannot have a variable!");
901 break;
902
903 case LCK_ByCopy:
904 Bits |= Capture_ByCopy;
905 // Fall through
906 case LCK_ByRef:
907 assert(Var && "capture must have a variable!");
908 break;
909 }
Richard Smithba71c082013-05-16 06:20:58 +0000910 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000911}
912
913LambdaCaptureKind 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 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000919}
920
James Dennettddd36ff2013-08-09 23:08:25 +0000921LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000922 SourceRange IntroducerRange,
923 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000924 SourceLocation CaptureDefaultLoc,
925 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000926 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000927 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000928 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000929 ArrayRef<VarDecl *> ArrayIndexVars,
930 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000931 SourceLocation ClosingBrace,
932 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000933 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
934 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000935 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000936 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000937 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000938 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000939 CaptureDefault(CaptureDefault),
940 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000941 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000942 ClosingBrace(ClosingBrace)
943{
944 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000945 CXXRecordDecl *Class = getLambdaClass();
946 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000947
948 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000949
950 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000951 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000952 Data.NumCaptures = NumCaptures;
953 Data.NumExplicitCaptures = 0;
954 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
955 Capture *ToCapture = Data.Captures;
956 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
957 if (Captures[I].isExplicit())
958 ++Data.NumExplicitCaptures;
959
960 *ToCapture++ = Captures[I];
961 }
962
963 // Copy initialization expressions for the non-static data members.
964 Stmt **Stored = getStoredStmts();
965 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
966 *Stored++ = CaptureInits[I];
967
968 // Copy the body of the lambda.
969 *Stored++ = getCallOperator()->getBody();
970
971 // Copy the array index variables, if any.
972 HasArrayIndexVars = !ArrayIndexVars.empty();
973 if (HasArrayIndexVars) {
974 assert(ArrayIndexStarts.size() == NumCaptures);
975 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
976 sizeof(VarDecl *) * ArrayIndexVars.size());
977 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
978 sizeof(unsigned) * Captures.size());
979 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000980 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000981}
982
Craig Toppera31a8822013-08-22 07:09:37 +0000983LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000984 CXXRecordDecl *Class,
985 SourceRange IntroducerRange,
986 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000987 SourceLocation CaptureDefaultLoc,
988 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000989 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000990 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000991 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000992 ArrayRef<VarDecl *> ArrayIndexVars,
993 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000994 SourceLocation ClosingBrace,
995 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000996 // Determine the type of the expression (i.e., the type of the
997 // function object we're creating).
998 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000999
Douglas Gregore5561632012-02-13 17:20:40 +00001000 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001001 if (!ArrayIndexVars.empty()) {
1002 Size += sizeof(unsigned) * (Captures.size() + 1);
1003 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001004 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001005 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1006 }
Douglas Gregore5561632012-02-13 17:20:40 +00001007 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001008 return new (Mem) LambdaExpr(T, IntroducerRange,
1009 CaptureDefault, CaptureDefaultLoc, Captures,
1010 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001011 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001012 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001013}
1014
Craig Toppera31a8822013-08-22 07:09:37 +00001015LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1016 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001017 unsigned NumArrayIndexVars) {
1018 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1019 if (NumArrayIndexVars)
1020 Size += sizeof(VarDecl) * NumArrayIndexVars
1021 + sizeof(unsigned) * (NumCaptures + 1);
1022 void *Mem = C.Allocate(Size);
1023 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1024}
1025
Douglas Gregorc8a73492012-02-13 15:44:47 +00001026LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001027 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001028}
1029
1030LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001031 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001032}
1033
1034LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1035 return capture_begin();
1036}
1037
1038LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1039 struct CXXRecordDecl::LambdaDefinitionData &Data
1040 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001041 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001042}
1043
1044LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1045 return explicit_capture_end();
1046}
1047
1048LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1049 return capture_end();
1050}
1051
Douglas Gregor54fcea62012-02-13 16:35:30 +00001052ArrayRef<VarDecl *>
1053LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001054 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001055
1056 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001057 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1058 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001059 VarDecl **IndexVars = getArrayIndexVars();
1060 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001061 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1062 IndexVars + IndexStarts[Index + 1]);
1063}
1064
Douglas Gregore31e6062012-02-07 10:09:13 +00001065CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1066 return getType()->getAsCXXRecordDecl();
1067}
1068
1069CXXMethodDecl *LambdaExpr::getCallOperator() const {
1070 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001071 return Record->getLambdaCallOperator();
1072}
1073
1074TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1075 CXXRecordDecl *Record = getLambdaClass();
1076 return Record->getGenericLambdaTemplateParameterList();
1077
Douglas Gregore31e6062012-02-07 10:09:13 +00001078}
1079
Douglas Gregor99ae8062012-02-14 17:54:36 +00001080CompoundStmt *LambdaExpr::getBody() const {
1081 if (!getStoredStmts()[NumCaptures])
1082 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1083
1084 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1085}
1086
Douglas Gregore31e6062012-02-07 10:09:13 +00001087bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001088 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001089}
1090
John McCall28fc7092011-11-10 05:35:25 +00001091ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1092 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001093 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001094 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001095 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001096 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001097 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001098 SubExpr(subexpr) {
1099 ExprWithCleanupsBits.NumObjects = objects.size();
1100 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1101 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001102}
1103
Craig Toppera31a8822013-08-22 07:09:37 +00001104ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001105 ArrayRef<CleanupObject> objects) {
1106 size_t size = sizeof(ExprWithCleanups)
1107 + objects.size() * sizeof(CleanupObject);
1108 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1109 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001110}
1111
John McCall28fc7092011-11-10 05:35:25 +00001112ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1113 : Expr(ExprWithCleanupsClass, empty) {
1114 ExprWithCleanupsBits.NumObjects = numObjects;
1115}
Chris Lattnercba86142010-05-10 00:25:06 +00001116
Craig Toppera31a8822013-08-22 07:09:37 +00001117ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1118 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001119 unsigned numObjects) {
1120 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1121 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1122 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001123}
1124
Douglas Gregor2b88c112010-09-08 00:15:04 +00001125CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001126 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001127 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001128 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001129 : Expr(CXXUnresolvedConstructExprClass,
1130 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001131 (Type->getType()->isLValueReferenceType() ? VK_LValue
1132 :Type->getType()->isRValueReferenceType()? VK_XValue
1133 :VK_RValue),
1134 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001135 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001136 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001137 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001138 LParenLoc(LParenLoc),
1139 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001140 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001141 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001142 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001143 if (Args[I]->containsUnexpandedParameterPack())
1144 ExprBits.ContainsUnexpandedParameterPack = true;
1145
1146 StoredArgs[I] = Args[I];
1147 }
Douglas Gregorce934142009-05-20 18:46:25 +00001148}
1149
1150CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001151CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001152 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001153 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001154 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001155 SourceLocation RParenLoc) {
1156 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001157 sizeof(Expr *) * Args.size());
1158 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001159}
1160
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001161CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001162CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001163 Stmt::EmptyShell Empty;
1164 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1165 sizeof(Expr *) * NumArgs);
1166 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1167}
1168
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001169SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1170 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001171}
1172
Craig Toppera31a8822013-08-22 07:09:37 +00001173CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001174 Expr *Base, QualType BaseType,
1175 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001176 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001177 NestedNameSpecifierLoc QualifierLoc,
1178 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001179 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001180 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001181 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001182 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001183 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001184 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001185 (QualifierLoc &&
1186 QualifierLoc.getNestedNameSpecifier()
1187 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001188 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001189 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001190 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001191 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001192 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001193 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001194 if (TemplateArgs) {
1195 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001196 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001197 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001198 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1199 Dependent,
1200 InstantiationDependent,
1201 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001202 if (ContainsUnexpandedParameterPack)
1203 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001204 } else if (TemplateKWLoc.isValid()) {
1205 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001206 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001207}
1208
Craig Toppera31a8822013-08-22 07:09:37 +00001209CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001210 Expr *Base, QualType BaseType,
1211 bool IsArrow,
1212 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001213 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001214 NamedDecl *FirstQualifierFoundInScope,
1215 DeclarationNameInfo MemberNameInfo)
1216 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001217 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001219 (QualifierLoc &&
1220 QualifierLoc.getNestedNameSpecifier()->
1221 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001222 MemberNameInfo.containsUnexpandedParameterPack())),
1223 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001224 HasTemplateKWAndArgsInfo(false),
1225 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001226 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1227 MemberNameInfo(MemberNameInfo) { }
1228
John McCall8cd78132009-11-19 22:55:06 +00001229CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001230CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001231 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001232 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001233 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001234 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001235 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001236 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001237 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001238 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001239 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1240 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001241 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001242 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001243 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Abramo Bagnara7945c982012-01-27 09:46:47 +00001245 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1246 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1247 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001248
Chris Lattner5c0b4052010-10-30 05:14:06 +00001249 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001250 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1251 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001252 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001253 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001254 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001255 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001256}
1257
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001258CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001259CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001260 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001261 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001262 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001263 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001264 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001265 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001266 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001267
1268 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001269 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001270 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001271 CXXDependentScopeMemberExpr *E
1272 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001273 0, SourceLocation(),
1274 NestedNameSpecifierLoc(),
1275 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001276 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001277 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001278 return E;
1279}
1280
Douglas Gregor0da1d432011-02-28 20:01:57 +00001281bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1282 if (Base == 0)
1283 return true;
1284
Douglas Gregor25b7e052011-03-02 21:06:53 +00001285 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001286}
1287
John McCall0009fcc2011-04-26 20:42:42 +00001288static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1289 UnresolvedSetIterator end) {
1290 do {
1291 NamedDecl *decl = *begin;
1292 if (isa<UnresolvedUsingValueDecl>(decl))
1293 return false;
1294 if (isa<UsingShadowDecl>(decl))
1295 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1296
1297 // Unresolved member expressions should only contain methods and
1298 // method templates.
1299 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1300
1301 if (isa<FunctionTemplateDecl>(decl))
1302 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1303 if (cast<CXXMethodDecl>(decl)->isStatic())
1304 return false;
1305 } while (++begin != end);
1306
1307 return true;
1308}
1309
Craig Toppera31a8822013-08-22 07:09:37 +00001310UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001311 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001312 Expr *Base, QualType BaseType,
1313 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001314 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001315 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001316 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001317 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001318 const TemplateArgumentListInfo *TemplateArgs,
1319 UnresolvedSetIterator Begin,
1320 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001321 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1322 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001323 // Dependent
1324 ((Base && Base->isTypeDependent()) ||
1325 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001326 ((Base && Base->isInstantiationDependent()) ||
1327 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001328 // Contains unexpanded parameter pack
1329 ((Base && Base->containsUnexpandedParameterPack()) ||
1330 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001331 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1332 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001333
1334 // Check whether all of the members are non-static member functions,
1335 // and if so, mark give this bound-member type instead of overload type.
1336 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1337 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001338}
1339
Douglas Gregor0da1d432011-02-28 20:01:57 +00001340bool UnresolvedMemberExpr::isImplicitAccess() const {
1341 if (Base == 0)
1342 return true;
1343
Douglas Gregor25b7e052011-03-02 21:06:53 +00001344 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001345}
1346
John McCall10eae182009-11-30 22:42:35 +00001347UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001348UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001349 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001350 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001351 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001352 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001353 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001354 const TemplateArgumentListInfo *TemplateArgs,
1355 UnresolvedSetIterator Begin,
1356 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001357 std::size_t size = sizeof(UnresolvedMemberExpr);
1358 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001359 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1360 else if (TemplateKWLoc.isValid())
1361 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001362
Chris Lattner5c0b4052010-10-30 05:14:06 +00001363 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001364 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001365 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001366 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001367 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001368}
1369
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001370UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001371UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1372 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001373 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001374 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001375 if (HasTemplateKWAndArgsInfo)
1376 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001377
Chris Lattner5c0b4052010-10-30 05:14:06 +00001378 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001379 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001380 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001381 return E;
1382}
1383
John McCall58cc69d2010-01-27 01:50:18 +00001384CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1385 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1386
1387 // If there was a nested name specifier, it names the naming class.
1388 // It can't be dependent: after all, we were actually able to do the
1389 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001390 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001391 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001392 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001393 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001394 Record = T->getAsCXXRecordDecl();
1395 assert(Record && "qualifier in member expression does not name record");
1396 }
John McCall58cc69d2010-01-27 01:50:18 +00001397 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001398 else {
John McCall58cc69d2010-01-27 01:50:18 +00001399 QualType BaseType = getBaseType().getNonReferenceType();
1400 if (isArrow()) {
1401 const PointerType *PT = BaseType->getAs<PointerType>();
1402 assert(PT && "base of arrow member access is not pointer");
1403 BaseType = PT->getPointeeType();
1404 }
1405
Douglas Gregor9262f472010-04-27 18:19:34 +00001406 Record = BaseType->getAsCXXRecordDecl();
1407 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001408 }
1409
Douglas Gregor9262f472010-04-27 18:19:34 +00001410 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001411}
1412
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001413SubstNonTypeTemplateParmPackExpr::
1414SubstNonTypeTemplateParmPackExpr(QualType T,
1415 NonTypeTemplateParmDecl *Param,
1416 SourceLocation NameLoc,
1417 const TemplateArgument &ArgPack)
1418 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001419 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001420 Param(Param), Arguments(ArgPack.pack_begin()),
1421 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1422
1423TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1424 return TemplateArgument(Arguments, NumArguments);
1425}
1426
Richard Smithb15fe3a2012-09-12 00:56:43 +00001427FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1428 SourceLocation NameLoc,
1429 unsigned NumParams,
1430 Decl * const *Params)
1431 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1432 true, true, true, true),
1433 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1434 if (Params)
1435 std::uninitialized_copy(Params, Params + NumParams,
1436 reinterpret_cast<Decl**>(this+1));
1437}
1438
1439FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001440FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001441 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001442 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001443 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1444 sizeof(ParmVarDecl*) * Params.size()))
1445 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1446}
1447
1448FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001449FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1450 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001451 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1452 sizeof(ParmVarDecl*) * NumParams))
1453 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1454}
1455
Douglas Gregor29c42f22012-02-24 07:38:34 +00001456TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1457 ArrayRef<TypeSourceInfo *> Args,
1458 SourceLocation RParenLoc,
1459 bool Value)
1460 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1461 /*TypeDependent=*/false,
1462 /*ValueDependent=*/false,
1463 /*InstantiationDependent=*/false,
1464 /*ContainsUnexpandedParameterPack=*/false),
1465 Loc(Loc), RParenLoc(RParenLoc)
1466{
1467 TypeTraitExprBits.Kind = Kind;
1468 TypeTraitExprBits.Value = Value;
1469 TypeTraitExprBits.NumArgs = Args.size();
1470
1471 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1472
1473 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1474 if (Args[I]->getType()->isDependentType())
1475 setValueDependent(true);
1476 if (Args[I]->getType()->isInstantiationDependentType())
1477 setInstantiationDependent(true);
1478 if (Args[I]->getType()->containsUnexpandedParameterPack())
1479 setContainsUnexpandedParameterPack(true);
1480
1481 ToArgs[I] = Args[I];
1482 }
1483}
1484
Craig Toppera31a8822013-08-22 07:09:37 +00001485TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001486 SourceLocation Loc,
1487 TypeTrait Kind,
1488 ArrayRef<TypeSourceInfo *> Args,
1489 SourceLocation RParenLoc,
1490 bool Value) {
1491 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1492 void *Mem = C.Allocate(Size);
1493 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1494}
1495
Craig Toppera31a8822013-08-22 07:09:37 +00001496TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001497 unsigned NumArgs) {
1498 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1499 void *Mem = C.Allocate(Size);
1500 return new (Mem) TypeTraitExpr(EmptyShell());
1501}
1502
David Blaikie68e081d2011-12-20 02:48:34 +00001503void ArrayTypeTraitExpr::anchor() { }