blob: 7f1a287d1facb5a855ce80b91880f0ae92a6b81e [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
56UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT) {
57 // Optionally remove one level of pointer, reference or array indirection.
58 const Type *Ty = QT.getTypePtr();
59 if (QT->isPointerType() || QT->isReferenceType())
60 Ty = QT->getPointeeType().getTypePtr();
61 else if (QT->isArrayType())
62 Ty = cast<ArrayType>(QT)->getElementType().getTypePtr();
63
64 // Loop all record redeclaration looking for an uuid attribute.
65 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
66 for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(),
67 E = RD->redecls_end(); I != E; ++I) {
68 if (UuidAttr *Uuid = I->getAttr<UuidAttr>())
69 return Uuid;
70 }
71
72 return 0;
73}
74
David Majnemer8eaab6f2013-08-13 06:32:20 +000075StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
76 StringRef Uuid;
77 if (isTypeOperand())
78 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand())->getGuid();
79 else {
80 // Special case: __uuidof(0) means an all-zero GUID.
81 Expr *Op = getExprOperand();
82 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
83 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
84 else
85 Uuid = "00000000-0000-0000-0000-000000000000";
86 }
87 return Uuid;
88}
89
Douglas Gregor747eb782010-07-08 06:14:04 +000090// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +000091SourceLocation CXXScalarValueInitExpr::getLocStart() const {
92 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +000093}
94
Sebastian Redlbd150f42008-11-21 19:14:01 +000095// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +000096CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
97 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +000098 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +000099 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000100 SourceRange typeIdParens, Expr *arraySize,
101 InitializationStyle initializationStyle,
102 Expr *initializer, QualType ty,
103 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000104 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000105 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000106 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000107 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000108 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +0000109 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
110 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000111 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000112 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +0000113 assert((initializer != 0 || initializationStyle == NoInit) &&
114 "Only NoInit can have no initializer.");
115 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Benjamin Kramerc215e762012-08-24 11:54:20 +0000116 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000117 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000118 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000119 if (arraySize->isInstantiationDependent())
120 ExprBits.InstantiationDependent = true;
121
Douglas Gregora6e053e2010-12-15 01:34:56 +0000122 if (arraySize->containsUnexpandedParameterPack())
123 ExprBits.ContainsUnexpandedParameterPack = true;
124
Sebastian Redl351bb782008-12-02 14:43:59 +0000125 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000126 }
127
Sebastian Redl6047f072012-02-16 12:22:20 +0000128 if (initializer) {
129 if (initializer->isInstantiationDependent())
130 ExprBits.InstantiationDependent = true;
131
132 if (initializer->containsUnexpandedParameterPack())
133 ExprBits.ContainsUnexpandedParameterPack = true;
134
135 SubExprs[i++] = initializer;
136 }
137
Benjamin Kramerc215e762012-08-24 11:54:20 +0000138 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000139 if (placementArgs[j]->isInstantiationDependent())
140 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000141 if (placementArgs[j]->containsUnexpandedParameterPack())
142 ExprBits.ContainsUnexpandedParameterPack = true;
143
Sebastian Redlbd150f42008-11-21 19:14:01 +0000144 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000145 }
David Blaikie3a0de212012-11-08 22:53:48 +0000146
147 switch (getInitializationStyle()) {
148 case CallInit:
149 this->Range.setEnd(DirectInitRange.getEnd()); break;
150 case ListInit:
151 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000152 default:
153 if (TypeIdParens.isValid())
154 this->Range.setEnd(TypeIdParens.getEnd());
155 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000156 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000157}
158
Craig Toppera31a8822013-08-22 07:09:37 +0000159void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000160 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000161 assert(SubExprs == 0 && "SubExprs already allocated");
162 Array = isArray;
163 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000164
165 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000166 SubExprs = new (C) Stmt*[TotalSize];
167}
168
Craig Toppera31a8822013-08-22 07:09:37 +0000169bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000170 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000171 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000172}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000173
Sebastian Redlbd150f42008-11-21 19:14:01 +0000174// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000175QualType CXXDeleteExpr::getDestroyedType() const {
176 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000177 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000178 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000179
180 if (ArgType->isDependentType() && !ArgType->isPointerType())
181 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000182
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000183 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000184}
185
Douglas Gregorad8a3362009-09-04 17:36:40 +0000186// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000187PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
188 : Type(Info)
189{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000190 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000191}
192
Craig Toppera31a8822013-08-22 07:09:37 +0000193CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000194 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
195 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
196 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
197 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000198 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000199 Context.getPointerType(Context.getFunctionType(
200 Context.VoidTy, None,
201 FunctionProtoType::ExtProtoInfo(
202 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000203 VK_RValue, OK_Ordinary,
204 /*isTypeDependent=*/(Base->isTypeDependent() ||
205 (DestroyedType.getTypeSourceInfo() &&
206 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000207 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000208 (Base->isInstantiationDependent() ||
209 (QualifierLoc &&
210 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
211 (ScopeType &&
212 ScopeType->getType()->isInstantiationDependentType()) ||
213 (DestroyedType.getTypeSourceInfo() &&
214 DestroyedType.getTypeSourceInfo()->getType()
215 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000216 // ContainsUnexpandedParameterPack
217 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000218 (QualifierLoc &&
219 QualifierLoc.getNestedNameSpecifier()
220 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000221 (ScopeType &&
222 ScopeType->getType()->containsUnexpandedParameterPack()) ||
223 (DestroyedType.getTypeSourceInfo() &&
224 DestroyedType.getTypeSourceInfo()->getType()
225 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000226 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000227 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000228 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
229 DestroyedType(DestroyedType) { }
230
Douglas Gregor678f90d2010-02-25 01:56:36 +0000231QualType CXXPseudoDestructorExpr::getDestroyedType() const {
232 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
233 return TInfo->getType();
234
235 return QualType();
236}
237
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000238SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000239 SourceLocation End = DestroyedType.getLocation();
240 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000241 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000242 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000243}
244
John McCalld14a8642009-11-21 08:51:07 +0000245// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000246UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000247UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000248 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000249 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000250 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000251 const DeclarationNameInfo &NameInfo,
252 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000253 const TemplateArgumentListInfo *Args,
254 UnresolvedSetIterator Begin,
255 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000256{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000257 assert(Args || TemplateKWLoc.isValid());
258 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000259 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000260 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000261 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
262 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000263 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000264 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000265}
266
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000267UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000268UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000269 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000270 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000271 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000272 if (HasTemplateKWAndArgsInfo)
273 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000274
Chris Lattner5c0b4052010-10-30 05:14:06 +0000275 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000276 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000277 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000278 return E;
279}
280
Craig Toppera31a8822013-08-22 07:09:37 +0000281OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000282 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000283 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000284 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000285 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000286 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000287 UnresolvedSetIterator End,
288 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000289 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000290 bool KnownContainsUnexpandedParameterPack)
291 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
292 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000293 (KnownInstantiationDependent ||
294 NameInfo.isInstantiationDependent() ||
295 (QualifierLoc &&
296 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000297 (KnownContainsUnexpandedParameterPack ||
298 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000299 (QualifierLoc &&
300 QualifierLoc.getNestedNameSpecifier()
301 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000302 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
303 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000304 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000305{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000306 NumResults = End - Begin;
307 if (NumResults) {
308 // Determine whether this expression is type-dependent.
309 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
310 if ((*I)->getDeclContext()->isDependentContext() ||
311 isa<UnresolvedUsingValueDecl>(*I)) {
312 ExprBits.TypeDependent = true;
313 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000314 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000315 }
316 }
317
318 Results = static_cast<DeclAccessPair *>(
319 C.Allocate(sizeof(DeclAccessPair) * NumResults,
320 llvm::alignOf<DeclAccessPair>()));
321 memcpy(Results, &*Begin.getIterator(),
322 NumResults * sizeof(DeclAccessPair));
323 }
324
325 // If we have explicit template arguments, check for dependent
326 // template arguments and whether they contain any unexpanded pack
327 // expansions.
328 if (TemplateArgs) {
329 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000330 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000332 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
333 Dependent,
334 InstantiationDependent,
335 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000336
337 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000338 ExprBits.TypeDependent = true;
339 ExprBits.ValueDependent = true;
340 }
341 if (InstantiationDependent)
342 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000343 if (ContainsUnexpandedParameterPack)
344 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000345 } else if (TemplateKWLoc.isValid()) {
346 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000347 }
348
349 if (isTypeDependent())
350 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000351}
352
Craig Toppera31a8822013-08-22 07:09:37 +0000353void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000354 UnresolvedSetIterator Begin,
355 UnresolvedSetIterator End) {
356 assert(Results == 0 && "Results already initialized!");
357 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000358 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359 Results = static_cast<DeclAccessPair *>(
360 C.Allocate(sizeof(DeclAccessPair) * NumResults,
361
362 llvm::alignOf<DeclAccessPair>()));
363 memcpy(Results, &*Begin.getIterator(),
364 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000365 }
366}
367
John McCall8c12dc42010-04-22 18:44:12 +0000368CXXRecordDecl *OverloadExpr::getNamingClass() const {
369 if (isa<UnresolvedLookupExpr>(this))
370 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
371 else
372 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
373}
374
John McCall8cd78132009-11-19 22:55:06 +0000375// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000376DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000377 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000378 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000379 const DeclarationNameInfo &NameInfo,
380 const TemplateArgumentListInfo *Args)
381 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
382 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000383 (NameInfo.isInstantiationDependent() ||
384 (QualifierLoc &&
385 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000386 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000387 (QualifierLoc &&
388 QualifierLoc.getNestedNameSpecifier()
389 ->containsUnexpandedParameterPack()))),
390 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000391 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000392{
393 if (Args) {
394 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000395 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000396 bool ContainsUnexpandedParameterPack
397 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000398 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
399 Dependent,
400 InstantiationDependent,
401 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000402 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000403 } else if (TemplateKWLoc.isValid()) {
404 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000405 }
406}
407
John McCalle66edc12009-11-24 19:00:30 +0000408DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000409DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000410 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000411 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000412 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000413 const TemplateArgumentListInfo *Args) {
414 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000415 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000416 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
417 else if (TemplateKWLoc.isValid())
418 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000419 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000420 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
421 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000422}
423
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000424DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000425DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000426 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000427 unsigned NumTemplateArgs) {
428 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000429 if (HasTemplateKWAndArgsInfo)
430 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000431 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000433 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000434 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000435 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000436 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000437 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000438}
439
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000440SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000441 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000442 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
443 return Loc;
444}
445
446SourceLocation CXXConstructExpr::getLocEnd() const {
447 if (isa<CXXTemporaryObjectExpr>(this))
448 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000449
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000450 if (ParenOrBraceRange.isValid())
451 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000452
453 SourceLocation End = Loc;
454 for (unsigned I = getNumArgs(); I > 0; --I) {
455 const Expr *Arg = getArg(I-1);
456 if (!Arg->isDefaultArgument()) {
457 SourceLocation NewEnd = Arg->getLocEnd();
458 if (NewEnd.isValid()) {
459 End = NewEnd;
460 break;
461 }
462 }
463 }
464
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000465 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000466}
467
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000468SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000469 OverloadedOperatorKind Kind = getOperator();
470 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
471 if (getNumArgs() == 1)
472 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000473 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000474 else
475 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000476 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000477 } else if (Kind == OO_Arrow) {
478 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000479 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000480 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000481 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000482 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000483 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000484 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000485 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000486 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000487 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000488 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000489 }
490}
491
Ted Kremenek98a24e32011-03-30 17:41:19 +0000492Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000493 const Expr *Callee = getCallee()->IgnoreParens();
494 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000495 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000496 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
497 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
498 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000499
500 // FIXME: Will eventually need to cope with member pointers.
501 return 0;
502}
503
Ted Kremenek98a24e32011-03-30 17:41:19 +0000504CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
505 if (const MemberExpr *MemExpr =
506 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
507 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
508
509 // FIXME: Will eventually need to cope with member pointers.
510 return 0;
511}
512
513
David Blaikiec0f58662012-05-03 16:25:49 +0000514CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000515 Expr* ThisArg = getImplicitObjectArgument();
516 if (!ThisArg)
517 return 0;
518
519 if (ThisArg->getType()->isAnyPointerType())
520 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
521
522 return ThisArg->getType()->getAsCXXRecordDecl();
523}
524
Douglas Gregoref986e82009-11-12 15:31:47 +0000525
Douglas Gregore200adc2008-10-27 19:41:14 +0000526//===----------------------------------------------------------------------===//
527// Named casts
528//===----------------------------------------------------------------------===//
529
530/// getCastName - Get the name of the C++ cast being used, e.g.,
531/// "static_cast", "dynamic_cast", "reinterpret_cast", or
532/// "const_cast". The returned pointer must not be freed.
533const char *CXXNamedCastExpr::getCastName() const {
534 switch (getStmtClass()) {
535 case CXXStaticCastExprClass: return "static_cast";
536 case CXXDynamicCastExprClass: return "dynamic_cast";
537 case CXXReinterpretCastExprClass: return "reinterpret_cast";
538 case CXXConstCastExprClass: return "const_cast";
539 default: return "<invalid cast>";
540 }
541}
Douglas Gregordd04d332009-01-16 18:33:17 +0000542
Craig Toppera31a8822013-08-22 07:09:37 +0000543CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000544 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000545 CastKind K, Expr *Op,
546 const CXXCastPath *BasePath,
547 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000548 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000549 SourceLocation RParenLoc,
550 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000551 unsigned PathSize = (BasePath ? BasePath->size() : 0);
552 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
553 + PathSize * sizeof(CXXBaseSpecifier*));
554 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000555 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000556 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000557 if (PathSize) E->setCastPath(*BasePath);
558 return E;
559}
560
Craig Toppera31a8822013-08-22 07:09:37 +0000561CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000562 unsigned PathSize) {
563 void *Buffer =
564 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
565 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
566}
567
Craig Toppera31a8822013-08-22 07:09:37 +0000568CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000569 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000570 CastKind K, Expr *Op,
571 const CXXCastPath *BasePath,
572 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000573 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000574 SourceLocation RParenLoc,
575 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000576 unsigned PathSize = (BasePath ? BasePath->size() : 0);
577 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
578 + PathSize * sizeof(CXXBaseSpecifier*));
579 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000580 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000581 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000582 if (PathSize) E->setCastPath(*BasePath);
583 return E;
584}
585
Craig Toppera31a8822013-08-22 07:09:37 +0000586CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000587 unsigned PathSize) {
588 void *Buffer =
589 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
590 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
591}
592
Anders Carlsson267c0c92011-04-11 01:43:55 +0000593/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
594/// to always be null. For example:
595///
596/// struct A { };
597/// struct B final : A { };
598/// struct C { };
599///
600/// C *f(B* b) { return dynamic_cast<C*>(b); }
601bool CXXDynamicCastExpr::isAlwaysNull() const
602{
603 QualType SrcType = getSubExpr()->getType();
604 QualType DestType = getType();
605
606 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
607 SrcType = SrcPTy->getPointeeType();
608 DestType = DestType->castAs<PointerType>()->getPointeeType();
609 }
610
Alexis Hunt78e2b912012-06-19 23:44:55 +0000611 if (DestType->isVoidType())
612 return false;
613
Anders Carlsson267c0c92011-04-11 01:43:55 +0000614 const CXXRecordDecl *SrcRD =
615 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
616
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000617 if (!SrcRD->hasAttr<FinalAttr>())
618 return false;
619
Anders Carlsson267c0c92011-04-11 01:43:55 +0000620 const CXXRecordDecl *DestRD =
621 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
622
623 return !DestRD->isDerivedFrom(SrcRD);
624}
625
John McCallcf142162010-08-07 06:22:56 +0000626CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000627CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
628 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000629 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000630 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000631 SourceLocation RParenLoc,
632 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000633 unsigned PathSize = (BasePath ? BasePath->size() : 0);
634 void *Buffer =
635 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
636 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000637 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000638 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000639 if (PathSize) E->setCastPath(*BasePath);
640 return E;
641}
642
643CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000644CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000645 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
646 + PathSize * sizeof(CXXBaseSpecifier*));
647 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
648}
649
Craig Toppera31a8822013-08-22 07:09:37 +0000650CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000651 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000652 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000653 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000654 SourceLocation RParenLoc,
655 SourceRange AngleBrackets) {
656 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000657}
658
Craig Toppera31a8822013-08-22 07:09:37 +0000659CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000660 return new (C) CXXConstCastExpr(EmptyShell());
661}
662
663CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000664CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000665 TypeSourceInfo *Written, CastKind K, Expr *Op,
666 const CXXCastPath *BasePath,
667 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000668 unsigned PathSize = (BasePath ? BasePath->size() : 0);
669 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
670 + PathSize * sizeof(CXXBaseSpecifier*));
671 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000672 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000673 if (PathSize) E->setCastPath(*BasePath);
674 return E;
675}
676
677CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000678CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000679 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
680 + PathSize * sizeof(CXXBaseSpecifier*));
681 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
682}
683
Eli Friedman89fe0d52013-08-15 22:02:56 +0000684SourceLocation CXXFunctionalCastExpr::getLocStart() const {
685 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
686}
687
688SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
689 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
690}
691
Richard Smithc67fdd42012-03-07 08:35:16 +0000692UserDefinedLiteral::LiteralOperatorKind
693UserDefinedLiteral::getLiteralOperatorKind() const {
694 if (getNumArgs() == 0)
695 return LOK_Template;
696 if (getNumArgs() == 2)
697 return LOK_String;
698
699 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
700 QualType ParamTy =
701 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
702 if (ParamTy->isPointerType())
703 return LOK_Raw;
704 if (ParamTy->isAnyCharacterType())
705 return LOK_Character;
706 if (ParamTy->isIntegerType())
707 return LOK_Integer;
708 if (ParamTy->isFloatingType())
709 return LOK_Floating;
710
711 llvm_unreachable("unknown kind of literal operator");
712}
713
714Expr *UserDefinedLiteral::getCookedLiteral() {
715#ifndef NDEBUG
716 LiteralOperatorKind LOK = getLiteralOperatorKind();
717 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
718#endif
719 return getArg(0);
720}
721
722const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
723 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
724}
John McCallcf142162010-08-07 06:22:56 +0000725
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000726CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000727CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000728 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000729 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000730 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
731 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000732}
733
Craig Toppera31a8822013-08-22 07:09:37 +0000734CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000735 FieldDecl *Field, QualType T)
736 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
737 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
738 ? VK_XValue
739 : VK_RValue,
740 /*FIXME*/ OK_Ordinary, false, false, false, false),
741 Field(Field), Loc(Loc) {
742 assert(Field->hasInClassInitializer());
743}
744
Craig Toppera31a8822013-08-22 07:09:37 +0000745CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000746 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000747 return new (C) CXXTemporary(Destructor);
748}
749
Craig Toppera31a8822013-08-22 07:09:37 +0000750CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000751 CXXTemporary *Temp,
752 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000753 assert((SubExpr->getType()->isRecordType() ||
754 SubExpr->getType()->isArrayType()) &&
755 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000756
Douglas Gregora6e053e2010-12-15 01:34:56 +0000757 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000758}
759
Craig Toppera31a8822013-08-22 07:09:37 +0000760CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000761 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000762 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000763 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000764 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000765 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000766 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000767 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000768 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
769 Type->getType().getNonReferenceType(),
770 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000771 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000772 HadMultipleCandidates,
773 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000774 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000775 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000776}
777
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000778SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
779 return Type->getTypeLoc().getBeginLoc();
780}
781
782SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000783 return getParenOrBraceRange().getEnd();
Douglas Gregordd04d332009-01-16 18:33:17 +0000784}
Anders Carlsson6f287832009-04-21 02:22:11 +0000785
Craig Toppera31a8822013-08-22 07:09:37 +0000786CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000787 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000788 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000789 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000790 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000791 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000792 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000793 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000794 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000795 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000796 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000797 HadMultipleCandidates, ListInitialization,
798 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000799 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000800}
801
Craig Toppera31a8822013-08-22 07:09:37 +0000802CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
803 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000804 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000805 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000806 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000807 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000808 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000809 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000810 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000811 : Expr(SC, T, VK_RValue, OK_Ordinary,
812 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000813 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000814 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000815 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
816 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000817 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000818 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000819 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000820 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000821{
822 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000823 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000824
Benjamin Kramerc215e762012-08-24 11:54:20 +0000825 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000826 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000827
828 if (args[i]->isValueDependent())
829 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000830 if (args[i]->isInstantiationDependent())
831 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000832 if (args[i]->containsUnexpandedParameterPack())
833 ExprBits.ContainsUnexpandedParameterPack = true;
834
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000835 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000836 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000837 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000838}
839
Douglas Gregore31e6062012-02-07 10:09:13 +0000840LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
841 LambdaCaptureKind Kind, VarDecl *Var,
842 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000843 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000844{
845 unsigned Bits = 0;
846 if (Implicit)
847 Bits |= Capture_Implicit;
848
849 switch (Kind) {
850 case LCK_This:
851 assert(Var == 0 && "'this' capture cannot have a variable!");
852 break;
853
854 case LCK_ByCopy:
855 Bits |= Capture_ByCopy;
856 // Fall through
857 case LCK_ByRef:
858 assert(Var && "capture must have a variable!");
859 break;
Richard Smithba71c082013-05-16 06:20:58 +0000860
861 case LCK_Init:
862 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregore31e6062012-02-07 10:09:13 +0000863 }
Richard Smithba71c082013-05-16 06:20:58 +0000864 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000865}
866
Richard Smithba71c082013-05-16 06:20:58 +0000867LambdaExpr::Capture::Capture(FieldDecl *Field)
868 : DeclAndBits(Field,
869 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
870 Loc(Field->getLocation()), EllipsisLoc() {}
871
Douglas Gregore31e6062012-02-07 10:09:13 +0000872LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000873 Decl *D = DeclAndBits.getPointer();
874 if (!D)
Douglas Gregore31e6062012-02-07 10:09:13 +0000875 return LCK_This;
876
Richard Smithba71c082013-05-16 06:20:58 +0000877 if (isa<FieldDecl>(D))
878 return LCK_Init;
879
880 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000881}
882
James Dennettddd36ff2013-08-09 23:08:25 +0000883LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000884 SourceRange IntroducerRange,
885 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000886 SourceLocation CaptureDefaultLoc,
887 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000888 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000889 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000890 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000891 ArrayRef<VarDecl *> ArrayIndexVars,
892 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000893 SourceLocation ClosingBrace,
894 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000895 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
896 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000897 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000898 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000899 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000900 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000901 CaptureDefault(CaptureDefault),
902 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000903 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000904 ClosingBrace(ClosingBrace)
905{
906 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000907 CXXRecordDecl *Class = getLambdaClass();
908 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000909
910 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000911
912 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000913 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000914 Data.NumCaptures = NumCaptures;
915 Data.NumExplicitCaptures = 0;
916 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
917 Capture *ToCapture = Data.Captures;
918 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
919 if (Captures[I].isExplicit())
920 ++Data.NumExplicitCaptures;
921
922 *ToCapture++ = Captures[I];
923 }
924
925 // Copy initialization expressions for the non-static data members.
926 Stmt **Stored = getStoredStmts();
927 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
928 *Stored++ = CaptureInits[I];
929
930 // Copy the body of the lambda.
931 *Stored++ = getCallOperator()->getBody();
932
933 // Copy the array index variables, if any.
934 HasArrayIndexVars = !ArrayIndexVars.empty();
935 if (HasArrayIndexVars) {
936 assert(ArrayIndexStarts.size() == NumCaptures);
937 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
938 sizeof(VarDecl *) * ArrayIndexVars.size());
939 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
940 sizeof(unsigned) * Captures.size());
941 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000942 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000943}
944
Craig Toppera31a8822013-08-22 07:09:37 +0000945LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000946 CXXRecordDecl *Class,
947 SourceRange IntroducerRange,
948 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000949 SourceLocation CaptureDefaultLoc,
950 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000951 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000952 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000953 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000954 ArrayRef<VarDecl *> ArrayIndexVars,
955 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000956 SourceLocation ClosingBrace,
957 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000958 // Determine the type of the expression (i.e., the type of the
959 // function object we're creating).
960 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000961
Douglas Gregore5561632012-02-13 17:20:40 +0000962 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +0000963 if (!ArrayIndexVars.empty()) {
964 Size += sizeof(unsigned) * (Captures.size() + 1);
965 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +0000966 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +0000967 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
968 }
Douglas Gregore5561632012-02-13 17:20:40 +0000969 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +0000970 return new (Mem) LambdaExpr(T, IntroducerRange,
971 CaptureDefault, CaptureDefaultLoc, Captures,
972 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000973 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000974 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000975}
976
Craig Toppera31a8822013-08-22 07:09:37 +0000977LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
978 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +0000979 unsigned NumArrayIndexVars) {
980 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
981 if (NumArrayIndexVars)
982 Size += sizeof(VarDecl) * NumArrayIndexVars
983 + sizeof(unsigned) * (NumCaptures + 1);
984 void *Mem = C.Allocate(Size);
985 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
986}
987
Douglas Gregorc8a73492012-02-13 15:44:47 +0000988LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000989 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000990}
991
992LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000993 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000994}
995
996LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
997 return capture_begin();
998}
999
1000LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1001 struct CXXRecordDecl::LambdaDefinitionData &Data
1002 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001003 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001004}
1005
1006LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1007 return explicit_capture_end();
1008}
1009
1010LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1011 return capture_end();
1012}
1013
Douglas Gregor54fcea62012-02-13 16:35:30 +00001014ArrayRef<VarDecl *>
1015LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001016 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001017
1018 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001019 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1020 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001021 VarDecl **IndexVars = getArrayIndexVars();
1022 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001023 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1024 IndexVars + IndexStarts[Index + 1]);
1025}
1026
Douglas Gregore31e6062012-02-07 10:09:13 +00001027CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1028 return getType()->getAsCXXRecordDecl();
1029}
1030
1031CXXMethodDecl *LambdaExpr::getCallOperator() const {
1032 CXXRecordDecl *Record = getLambdaClass();
Manuel Klimek2fdbea22013-08-22 12:12:24 +00001033 DeclarationName Name
1034 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1035 DeclContext::lookup_result Calls = Record->lookup(Name);
1036 assert(!Calls.empty() && "Missing lambda call operator!");
1037 assert(Calls.size() == 1 && "More than one lambda call operator!");
1038 CXXMethodDecl *Result = cast<CXXMethodDecl>(Calls.front());
1039 return Result;
Douglas Gregore31e6062012-02-07 10:09:13 +00001040}
1041
Douglas Gregor99ae8062012-02-14 17:54:36 +00001042CompoundStmt *LambdaExpr::getBody() const {
1043 if (!getStoredStmts()[NumCaptures])
1044 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1045
1046 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1047}
1048
Douglas Gregore31e6062012-02-07 10:09:13 +00001049bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001050 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001051}
1052
John McCall28fc7092011-11-10 05:35:25 +00001053ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1054 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001055 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001056 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001057 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001058 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001059 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001060 SubExpr(subexpr) {
1061 ExprWithCleanupsBits.NumObjects = objects.size();
1062 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1063 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001064}
1065
Craig Toppera31a8822013-08-22 07:09:37 +00001066ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001067 ArrayRef<CleanupObject> objects) {
1068 size_t size = sizeof(ExprWithCleanups)
1069 + objects.size() * sizeof(CleanupObject);
1070 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1071 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001072}
1073
John McCall28fc7092011-11-10 05:35:25 +00001074ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1075 : Expr(ExprWithCleanupsClass, empty) {
1076 ExprWithCleanupsBits.NumObjects = numObjects;
1077}
Chris Lattnercba86142010-05-10 00:25:06 +00001078
Craig Toppera31a8822013-08-22 07:09:37 +00001079ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1080 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001081 unsigned numObjects) {
1082 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1083 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1084 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001085}
1086
Douglas Gregor2b88c112010-09-08 00:15:04 +00001087CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001088 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001089 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001090 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001091 : Expr(CXXUnresolvedConstructExprClass,
1092 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001093 (Type->getType()->isLValueReferenceType() ? VK_LValue
1094 :Type->getType()->isRValueReferenceType()? VK_XValue
1095 :VK_RValue),
1096 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001097 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001098 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001099 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001100 LParenLoc(LParenLoc),
1101 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001102 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001103 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001104 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001105 if (Args[I]->containsUnexpandedParameterPack())
1106 ExprBits.ContainsUnexpandedParameterPack = true;
1107
1108 StoredArgs[I] = Args[I];
1109 }
Douglas Gregorce934142009-05-20 18:46:25 +00001110}
1111
1112CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001113CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001114 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001115 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001116 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001117 SourceLocation RParenLoc) {
1118 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001119 sizeof(Expr *) * Args.size());
1120 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001121}
1122
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001123CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001124CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001125 Stmt::EmptyShell Empty;
1126 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1127 sizeof(Expr *) * NumArgs);
1128 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1129}
1130
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001131SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1132 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001133}
1134
Craig Toppera31a8822013-08-22 07:09:37 +00001135CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001136 Expr *Base, QualType BaseType,
1137 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001138 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001139 NestedNameSpecifierLoc QualifierLoc,
1140 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001141 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001142 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001143 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001144 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001145 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001147 (QualifierLoc &&
1148 QualifierLoc.getNestedNameSpecifier()
1149 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001150 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001151 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001152 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001153 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001154 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001155 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 if (TemplateArgs) {
1157 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001158 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001159 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001160 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1161 Dependent,
1162 InstantiationDependent,
1163 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001164 if (ContainsUnexpandedParameterPack)
1165 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001166 } else if (TemplateKWLoc.isValid()) {
1167 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001168 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001169}
1170
Craig Toppera31a8822013-08-22 07:09:37 +00001171CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001172 Expr *Base, QualType BaseType,
1173 bool IsArrow,
1174 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001175 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001176 NamedDecl *FirstQualifierFoundInScope,
1177 DeclarationNameInfo MemberNameInfo)
1178 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001179 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001180 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001181 (QualifierLoc &&
1182 QualifierLoc.getNestedNameSpecifier()->
1183 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001184 MemberNameInfo.containsUnexpandedParameterPack())),
1185 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001186 HasTemplateKWAndArgsInfo(false),
1187 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001188 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1189 MemberNameInfo(MemberNameInfo) { }
1190
John McCall8cd78132009-11-19 22:55:06 +00001191CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001192CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001193 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001194 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001195 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001196 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001197 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001198 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001199 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001200 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001201 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1202 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001203 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001204 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001205 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Abramo Bagnara7945c982012-01-27 09:46:47 +00001207 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1208 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1209 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001210
Chris Lattner5c0b4052010-10-30 05:14:06 +00001211 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001212 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1213 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001214 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001215 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001216 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001217 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001218}
1219
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001220CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001221CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001222 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001223 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001224 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001225 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001226 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001227 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001228 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001229
1230 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001231 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001232 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001233 CXXDependentScopeMemberExpr *E
1234 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001235 0, SourceLocation(),
1236 NestedNameSpecifierLoc(),
1237 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001238 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001239 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001240 return E;
1241}
1242
Douglas Gregor0da1d432011-02-28 20:01:57 +00001243bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1244 if (Base == 0)
1245 return true;
1246
Douglas Gregor25b7e052011-03-02 21:06:53 +00001247 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001248}
1249
John McCall0009fcc2011-04-26 20:42:42 +00001250static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1251 UnresolvedSetIterator end) {
1252 do {
1253 NamedDecl *decl = *begin;
1254 if (isa<UnresolvedUsingValueDecl>(decl))
1255 return false;
1256 if (isa<UsingShadowDecl>(decl))
1257 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1258
1259 // Unresolved member expressions should only contain methods and
1260 // method templates.
1261 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1262
1263 if (isa<FunctionTemplateDecl>(decl))
1264 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1265 if (cast<CXXMethodDecl>(decl)->isStatic())
1266 return false;
1267 } while (++begin != end);
1268
1269 return true;
1270}
1271
Craig Toppera31a8822013-08-22 07:09:37 +00001272UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001273 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001274 Expr *Base, QualType BaseType,
1275 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001276 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001277 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001278 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001279 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001280 const TemplateArgumentListInfo *TemplateArgs,
1281 UnresolvedSetIterator Begin,
1282 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001283 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1284 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001285 // Dependent
1286 ((Base && Base->isTypeDependent()) ||
1287 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001288 ((Base && Base->isInstantiationDependent()) ||
1289 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001290 // Contains unexpanded parameter pack
1291 ((Base && Base->containsUnexpandedParameterPack()) ||
1292 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001293 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1294 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001295
1296 // Check whether all of the members are non-static member functions,
1297 // and if so, mark give this bound-member type instead of overload type.
1298 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1299 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001300}
1301
Douglas Gregor0da1d432011-02-28 20:01:57 +00001302bool UnresolvedMemberExpr::isImplicitAccess() const {
1303 if (Base == 0)
1304 return true;
1305
Douglas Gregor25b7e052011-03-02 21:06:53 +00001306 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001307}
1308
John McCall10eae182009-11-30 22:42:35 +00001309UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001310UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001311 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001312 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001313 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001314 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001315 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001316 const TemplateArgumentListInfo *TemplateArgs,
1317 UnresolvedSetIterator Begin,
1318 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001319 std::size_t size = sizeof(UnresolvedMemberExpr);
1320 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001321 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1322 else if (TemplateKWLoc.isValid())
1323 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001324
Chris Lattner5c0b4052010-10-30 05:14:06 +00001325 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001326 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001327 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001328 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001329 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001330}
1331
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001332UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001333UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1334 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001335 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001336 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001337 if (HasTemplateKWAndArgsInfo)
1338 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001339
Chris Lattner5c0b4052010-10-30 05:14:06 +00001340 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001341 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001342 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001343 return E;
1344}
1345
John McCall58cc69d2010-01-27 01:50:18 +00001346CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1347 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1348
1349 // If there was a nested name specifier, it names the naming class.
1350 // It can't be dependent: after all, we were actually able to do the
1351 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001352 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001353 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001354 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001355 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001356 Record = T->getAsCXXRecordDecl();
1357 assert(Record && "qualifier in member expression does not name record");
1358 }
John McCall58cc69d2010-01-27 01:50:18 +00001359 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001360 else {
John McCall58cc69d2010-01-27 01:50:18 +00001361 QualType BaseType = getBaseType().getNonReferenceType();
1362 if (isArrow()) {
1363 const PointerType *PT = BaseType->getAs<PointerType>();
1364 assert(PT && "base of arrow member access is not pointer");
1365 BaseType = PT->getPointeeType();
1366 }
1367
Douglas Gregor9262f472010-04-27 18:19:34 +00001368 Record = BaseType->getAsCXXRecordDecl();
1369 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001370 }
1371
Douglas Gregor9262f472010-04-27 18:19:34 +00001372 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001373}
1374
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001375SubstNonTypeTemplateParmPackExpr::
1376SubstNonTypeTemplateParmPackExpr(QualType T,
1377 NonTypeTemplateParmDecl *Param,
1378 SourceLocation NameLoc,
1379 const TemplateArgument &ArgPack)
1380 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001381 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001382 Param(Param), Arguments(ArgPack.pack_begin()),
1383 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1384
1385TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1386 return TemplateArgument(Arguments, NumArguments);
1387}
1388
Richard Smithb15fe3a2012-09-12 00:56:43 +00001389FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1390 SourceLocation NameLoc,
1391 unsigned NumParams,
1392 Decl * const *Params)
1393 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1394 true, true, true, true),
1395 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1396 if (Params)
1397 std::uninitialized_copy(Params, Params + NumParams,
1398 reinterpret_cast<Decl**>(this+1));
1399}
1400
1401FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001402FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001403 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001404 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001405 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1406 sizeof(ParmVarDecl*) * Params.size()))
1407 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1408}
1409
1410FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001411FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1412 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001413 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1414 sizeof(ParmVarDecl*) * NumParams))
1415 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1416}
1417
Douglas Gregor29c42f22012-02-24 07:38:34 +00001418TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1419 ArrayRef<TypeSourceInfo *> Args,
1420 SourceLocation RParenLoc,
1421 bool Value)
1422 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1423 /*TypeDependent=*/false,
1424 /*ValueDependent=*/false,
1425 /*InstantiationDependent=*/false,
1426 /*ContainsUnexpandedParameterPack=*/false),
1427 Loc(Loc), RParenLoc(RParenLoc)
1428{
1429 TypeTraitExprBits.Kind = Kind;
1430 TypeTraitExprBits.Value = Value;
1431 TypeTraitExprBits.NumArgs = Args.size();
1432
1433 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1434
1435 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1436 if (Args[I]->getType()->isDependentType())
1437 setValueDependent(true);
1438 if (Args[I]->getType()->isInstantiationDependentType())
1439 setInstantiationDependent(true);
1440 if (Args[I]->getType()->containsUnexpandedParameterPack())
1441 setContainsUnexpandedParameterPack(true);
1442
1443 ToArgs[I] = Args[I];
1444 }
1445}
1446
Craig Toppera31a8822013-08-22 07:09:37 +00001447TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001448 SourceLocation Loc,
1449 TypeTrait Kind,
1450 ArrayRef<TypeSourceInfo *> Args,
1451 SourceLocation RParenLoc,
1452 bool Value) {
1453 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1454 void *Mem = C.Allocate(Size);
1455 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1456}
1457
Craig Toppera31a8822013-08-22 07:09:37 +00001458TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001459 unsigned NumArgs) {
1460 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1461 void *Mem = C.Allocate(Size);
1462 return new (Mem) TypeTraitExpr(EmptyShell());
1463}
1464
David Blaikie68e081d2011-12-20 02:48:34 +00001465void ArrayTypeTraitExpr::anchor() { }