blob: 36aa2892f2af7e009e4043d613f3a86683b05576 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Kremeneka758d092007-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 Kramer471c8b42012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregorb4609802008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000020#include "clang/Basic/IdentifierTable.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000021using namespace clang;
22
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000023
Ted Kremeneka758d092007-08-24 20:21:10 +000024//===----------------------------------------------------------------------===//
25// Child Iterators for iterating over subexpressions/substatements
26//===----------------------------------------------------------------------===//
27
Richard Smith0d729102012-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 Gregor57fdc8a2010-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 Pichet01b7c302010-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 Weberc5f80462012-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 Majnemerc80eb462013-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 Gregored8abf12010-07-08 06:14:04 +000090// CXXScalarValueInitExpr
Erik Verbruggen65d78312012-12-25 14:51:39 +000091SourceLocation CXXScalarValueInitExpr::getLocStart() const {
92 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregorab6677e2010-09-08 00:15:04 +000093}
94
Sebastian Redl4c5d3202008-11-21 19:14:01 +000095// CXXNewExpr
Craig Topper32b5a1e2013-08-22 07:09:37 +000096CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
97 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl2aed8b82012-02-16 12:22:20 +000098 bool usualArrayDeleteWantsSize,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +000099 ArrayRef<Expr*> placementArgs,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000100 SourceRange typeIdParens, Expr *arraySize,
101 InitializationStyle initializationStyle,
102 Expr *initializer, QualType ty,
103 TypeSourceInfo *allocatedTypeInfo,
David Blaikie53056412012-11-07 00:12:38 +0000104 SourceRange Range, SourceRange directInitRange)
John McCallf89e55a2010-11-18 06:31:45 +0000105 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000106 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000107 ty->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000108 ty->containsUnexpandedParameterPack()),
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000109 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
110 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie53056412012-11-07 00:12:38 +0000111 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000112 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl2aed8b82012-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 Kramer3b6bef92012-08-24 11:54:20 +0000116 AllocateArgsArray(C, arraySize != 0, placementArgs.size(), initializer != 0);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000117 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000118 if (Array) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000119 if (arraySize->isInstantiationDependent())
120 ExprBits.InstantiationDependent = true;
121
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000122 if (arraySize->containsUnexpandedParameterPack())
123 ExprBits.ContainsUnexpandedParameterPack = true;
124
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000125 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000126 }
127
Sebastian Redl2aed8b82012-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 Kramer3b6bef92012-08-24 11:54:20 +0000138 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000139 if (placementArgs[j]->isInstantiationDependent())
140 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000141 if (placementArgs[j]->containsUnexpandedParameterPack())
142 ExprBits.ContainsUnexpandedParameterPack = true;
143
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000144 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000145 }
David Blaikiec2fc67e2012-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 Friedman6e40c952013-06-17 22:35:10 +0000152 default:
153 if (TypeIdParens.isValid())
154 this->Range.setEnd(TypeIdParens.getEnd());
155 break;
David Blaikiec2fc67e2012-11-08 22:53:48 +0000156 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000157}
158
Craig Topper32b5a1e2013-08-22 07:09:37 +0000159void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000160 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattner59218632010-05-10 01:22:27 +0000161 assert(SubExprs == 0 && "SubExprs already allocated");
162 Array = isArray;
163 NumPlacementArgs = numPlaceArgs;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000164
165 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattner59218632010-05-10 01:22:27 +0000166 SubExprs = new (C) Stmt*[TotalSize];
167}
168
Craig Topper32b5a1e2013-08-22 07:09:37 +0000169bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000170 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000171 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000172}
Chris Lattner59218632010-05-10 01:22:27 +0000173
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000174// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000175QualType CXXDeleteExpr::getDestroyedType() const {
176 const Expr *Arg = getArgument();
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000177 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000178 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000179
180 if (ArgType->isDependentType() && !ArgType->isPointerType())
181 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000182
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000183 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000184}
185
Douglas Gregora71d8192009-09-04 17:36:40 +0000186// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000187PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
188 : Type(Info)
189{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000190 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000191}
192
Craig Topper32b5a1e2013-08-22 07:09:37 +0000193CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregorf3db29f2011-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 McCalle23cf432010-12-14 08:05:40 +0000198 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckneref072032013-08-27 23:08:25 +0000199 Context.getPointerType(Context.getFunctionType(
200 Context.VoidTy, None,
201 FunctionProtoType::ExtProtoInfo(
202 Context.getDefaultCallingConvention(false, true)))),
John McCalle23cf432010-12-14 08:05:40 +0000203 VK_RValue, OK_Ordinary,
204 /*isTypeDependent=*/(Base->isTypeDependent() ||
205 (DestroyedType.getTypeSourceInfo() &&
206 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000207 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-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 Gregorbebbe0d2010-12-15 01:34:56 +0000216 // ContainsUnexpandedParameterPack
217 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000218 (QualifierLoc &&
219 QualifierLoc.getNestedNameSpecifier()
220 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000221 (ScopeType &&
222 ScopeType->getType()->containsUnexpandedParameterPack()) ||
223 (DestroyedType.getTypeSourceInfo() &&
224 DestroyedType.getTypeSourceInfo()->getType()
225 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000226 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000227 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000228 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
229 DestroyedType(DestroyedType) { }
230
Douglas Gregora2e7dd22010-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 Verbruggen65d78312012-12-25 14:51:39 +0000238SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000239 SourceLocation End = DestroyedType.getLocation();
240 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000241 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen65d78312012-12-25 14:51:39 +0000242 return End;
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000243}
244
John McCallba135432009-11-21 08:51:07 +0000245// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000246UnresolvedLookupExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000247UnresolvedLookupExpr::Create(const ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000248 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000249 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000250 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000251 const DeclarationNameInfo &NameInfo,
252 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000253 const TemplateArgumentListInfo *Args,
254 UnresolvedSetIterator Begin,
255 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000256{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000257 assert(Args || TemplateKWLoc.isValid());
258 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000259 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000260 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000261 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
262 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000263 ADL, /*Overload*/ true, Args,
Richard Smithb1502bc2012-10-18 17:56:02 +0000264 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000265}
266
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000267UnresolvedLookupExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000268UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000269 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000270 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000271 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000272 if (HasTemplateKWAndArgsInfo)
273 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000274
Chris Lattner32488542010-10-30 05:14:06 +0000275 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000276 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000277 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000278 return E;
279}
280
Craig Topper32b5a1e2013-08-22 07:09:37 +0000281OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000282 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000283 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000284 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000285 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000286 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000287 UnresolvedSetIterator End,
288 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000289 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000290 bool KnownContainsUnexpandedParameterPack)
291 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
292 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000293 (KnownInstantiationDependent ||
294 NameInfo.isInstantiationDependent() ||
295 (QualifierLoc &&
296 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000297 (KnownContainsUnexpandedParameterPack ||
298 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000299 (QualifierLoc &&
300 QualifierLoc.getNestedNameSpecifier()
301 ->containsUnexpandedParameterPack()))),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000302 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
303 Results(0), NumResults(End - Begin),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000304 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000305{
Douglas Gregorbebbe0d2010-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 Smith7657fd72012-08-13 21:29:18 +0000314 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-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 Gregor561f8122011-07-01 01:22:09 +0000330 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000331 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000332 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
333 Dependent,
334 InstantiationDependent,
335 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000336
337 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000338 ExprBits.TypeDependent = true;
339 ExprBits.ValueDependent = true;
340 }
341 if (InstantiationDependent)
342 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000343 if (ContainsUnexpandedParameterPack)
344 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000345 } else if (TemplateKWLoc.isValid()) {
346 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000347 }
348
349 if (isTypeDependent())
350 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000351}
352
Craig Topper32b5a1e2013-08-22 07:09:37 +0000353void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000354 UnresolvedSetIterator Begin,
355 UnresolvedSetIterator End) {
356 assert(Results == 0 && "Results already initialized!");
357 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000358 if (NumResults) {
Douglas Gregorbebbe0d2010-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 Gregor928e6fc2010-05-23 19:36:40 +0000365 }
366}
367
John McCalle9ee23e2010-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 McCall865d4472009-11-19 22:55:06 +0000375// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000376DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000377 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000378 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-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 Gregor561f8122011-07-01 01:22:09 +0000383 (NameInfo.isInstantiationDependent() ||
384 (QualifierLoc &&
385 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000386 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000387 (QualifierLoc &&
388 QualifierLoc.getNestedNameSpecifier()
389 ->containsUnexpandedParameterPack()))),
390 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000391 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000392{
393 if (Args) {
394 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000395 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000396 bool ContainsUnexpandedParameterPack
397 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000398 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
399 Dependent,
400 InstantiationDependent,
401 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000402 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000403 } else if (TemplateKWLoc.isValid()) {
404 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000405 }
406}
407
John McCallf7a1a742009-11-24 19:00:30 +0000408DependentScopeDeclRefExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000409DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000410 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000411 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000412 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000413 const TemplateArgumentListInfo *Args) {
414 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000415 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000416 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
417 else if (TemplateKWLoc.isValid())
418 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000419 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000420 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
421 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000422}
423
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000424DependentScopeDeclRefExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000425DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000427 unsigned NumTemplateArgs) {
428 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000429 if (HasTemplateKWAndArgsInfo)
430 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000431 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000432 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000433 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000434 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000435 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000436 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000437 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000438}
439
Erik Verbruggen65d78312012-12-25 14:51:39 +0000440SourceLocation CXXConstructExpr::getLocStart() const {
John McCall2882eca2011-02-21 06:23:05 +0000441 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen65d78312012-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 McCall2882eca2011-02-21 06:23:05 +0000449
Douglas Gregor40749ee2010-11-03 00:35:38 +0000450 if (ParenRange.isValid())
Erik Verbruggen65d78312012-12-25 14:51:39 +0000451 return ParenRange.getEnd();
Douglas Gregor40749ee2010-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 Verbruggen65d78312012-12-25 14:51:39 +0000465 return End;
Ted Kremeneke3837682009-12-23 04:00:48 +0000466}
467
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000468SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregorb4609802008-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 Kyrtzidis3539b0c2012-05-01 22:19:11 +0000473 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000474 else
475 // Postfix operator
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000476 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000477 } else if (Kind == OO_Arrow) {
478 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000479 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000480 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000481 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000482 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000483 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000484 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000485 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000486 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000487 } else {
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000488 return getOperatorLoc();
Douglas Gregorb4609802008-11-14 16:09:21 +0000489 }
490}
491
Ted Kremenekb2771592011-03-30 17:41:19 +0000492Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose51e87c52012-08-03 23:08:39 +0000493 const Expr *Callee = getCallee()->IgnoreParens();
494 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor88a35142008-12-22 05:46:06 +0000495 return MemExpr->getBase();
Jordan Rose51e87c52012-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 Gregor88a35142008-12-22 05:46:06 +0000499
500 // FIXME: Will eventually need to cope with member pointers.
501 return 0;
502}
503
Ted Kremenekb2771592011-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 Blaikie0cf3c0e2012-05-03 16:25:49 +0000514CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth007a9b12010-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 Gregor00b98c22009-11-12 15:31:47 +0000525
Douglas Gregor49badde2008-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 Gregor506ae412009-01-16 18:33:17 +0000542
Craig Topper32b5a1e2013-08-22 07:09:37 +0000543CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000544 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000545 CastKind K, Expr *Op,
546 const CXXCastPath *BasePath,
547 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000548 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000549 SourceLocation RParenLoc,
550 SourceRange AngleBrackets) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000555 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000556 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000557 if (PathSize) E->setCastPath(*BasePath);
558 return E;
559}
560
Craig Topper32b5a1e2013-08-22 07:09:37 +0000561CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-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 Topper32b5a1e2013-08-22 07:09:37 +0000568CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000569 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000570 CastKind K, Expr *Op,
571 const CXXCastPath *BasePath,
572 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000573 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000574 SourceLocation RParenLoc,
575 SourceRange AngleBrackets) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000580 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000581 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000582 if (PathSize) E->setCastPath(*BasePath);
583 return E;
584}
585
Craig Topper32b5a1e2013-08-22 07:09:37 +0000586CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallf871d0c2010-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 Carlsson0fee3302011-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
Sean Hunt5ca86392012-06-19 23:44:55 +0000611 if (DestType->isVoidType())
612 return false;
613
Anders Carlsson0fee3302011-04-11 01:43:55 +0000614 const CXXRecordDecl *SrcRD =
615 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
616
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000617 if (!SrcRD->hasAttr<FinalAttr>())
618 return false;
619
Anders Carlsson0fee3302011-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 McCallf871d0c2010-08-07 06:22:56 +0000626CXXReinterpretCastExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000627CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
628 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000629 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000630 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000631 SourceLocation RParenLoc,
632 SourceRange AngleBrackets) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000637 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000638 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000639 if (PathSize) E->setCastPath(*BasePath);
640 return E;
641}
642
643CXXReinterpretCastExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000644CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallf871d0c2010-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 Topper32b5a1e2013-08-22 07:09:37 +0000650CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000651 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000652 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000653 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000654 SourceLocation RParenLoc,
655 SourceRange AngleBrackets) {
656 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000657}
658
Craig Topper32b5a1e2013-08-22 07:09:37 +0000659CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallf871d0c2010-08-07 06:22:56 +0000660 return new (C) CXXConstCastExpr(EmptyShell());
661}
662
663CXXFunctionalCastExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000664CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedmancdd4b782013-08-15 22:02:56 +0000665 TypeSourceInfo *Written, CastKind K, Expr *Op,
666 const CXXCastPath *BasePath,
667 SourceLocation L, SourceLocation R) {
John McCallf871d0c2010-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 Friedmancdd4b782013-08-15 22:02:56 +0000672 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallf871d0c2010-08-07 06:22:56 +0000673 if (PathSize) E->setCastPath(*BasePath);
674 return E;
675}
676
677CXXFunctionalCastExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000678CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallf871d0c2010-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 Friedmancdd4b782013-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 Smith9fcce652012-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 McCallf871d0c2010-08-07 06:22:56 +0000725
Douglas Gregor65222e82009-12-23 18:19:08 +0000726CXXDefaultArgExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +0000727CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor036aed12009-12-23 23:03:06 +0000728 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000729 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000730 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
731 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000732}
733
Craig Topper32b5a1e2013-08-22 07:09:37 +0000734CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smithc3bf52c2013-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 Topper32b5a1e2013-08-22 07:09:37 +0000745CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000746 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000747 return new (C) CXXTemporary(Destructor);
748}
749
Craig Topper32b5a1e2013-08-22 07:09:37 +0000750CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000751 CXXTemporary *Temp,
752 Expr* SubExpr) {
Peter Collingbournebceb7552011-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 Carlssonfceb0a82009-05-30 20:03:25 +0000756
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000757 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000758}
759
Craig Topper32b5a1e2013-08-22 07:09:37 +0000760CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000761 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000762 TypeSourceInfo *Type,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000763 ArrayRef<Expr*> Args,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000764 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000765 bool HadMultipleCandidates,
Richard Smithc83c2302012-12-19 01:39:02 +0000766 bool ListInitialization,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000767 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000768 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
769 Type->getType().getNonReferenceType(),
770 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000771 Cons, false, Args,
Richard Smithc83c2302012-12-19 01:39:02 +0000772 HadMultipleCandidates,
773 ListInitialization, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000774 CXXConstructExpr::CK_Complete, parenRange),
775 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000776}
777
Erik Verbruggen65d78312012-12-25 14:51:39 +0000778SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
779 return Type->getTypeLoc().getBeginLoc();
780}
781
782SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
783 return getParenRange().getEnd();
Douglas Gregor506ae412009-01-16 18:33:17 +0000784}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000785
Craig Topper32b5a1e2013-08-22 07:09:37 +0000786CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000787 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000788 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000789 ArrayRef<Expr*> Args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000790 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000791 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000792 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000793 ConstructionKind ConstructKind,
794 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000795 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000796 Elidable, Args,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000797 HadMultipleCandidates, ListInitialization,
798 ZeroInitialization, ConstructKind,
799 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000800}
801
Craig Topper32b5a1e2013-08-22 07:09:37 +0000802CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
803 QualType T, SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000804 CXXConstructorDecl *D, bool elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000805 ArrayRef<Expr*> args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000806 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000807 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000808 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000809 ConstructionKind ConstructKind,
810 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000811 : Expr(SC, T, VK_RValue, OK_Ordinary,
812 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000813 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000814 T->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000815 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(args.size()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000816 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000817 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000818 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000819 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000820{
821 if (NumArgs) {
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000822 Args = new (C) Stmt*[args.size()];
Douglas Gregor16006c92009-12-16 18:50:27 +0000823
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000824 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000825 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000826
827 if (args[i]->isValueDependent())
828 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000829 if (args[i]->isInstantiationDependent())
830 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000831 if (args[i]->containsUnexpandedParameterPack())
832 ExprBits.ContainsUnexpandedParameterPack = true;
833
Douglas Gregor16006c92009-12-16 18:50:27 +0000834 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000835 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000836 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000837}
838
Douglas Gregor01d08012012-02-07 10:09:13 +0000839LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
840 LambdaCaptureKind Kind, VarDecl *Var,
841 SourceLocation EllipsisLoc)
Richard Smith0d8e9642013-05-16 06:20:58 +0000842 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregor01d08012012-02-07 10:09:13 +0000843{
844 unsigned Bits = 0;
845 if (Implicit)
846 Bits |= Capture_Implicit;
847
848 switch (Kind) {
849 case LCK_This:
850 assert(Var == 0 && "'this' capture cannot have a variable!");
851 break;
852
853 case LCK_ByCopy:
854 Bits |= Capture_ByCopy;
855 // Fall through
856 case LCK_ByRef:
857 assert(Var && "capture must have a variable!");
858 break;
Richard Smith0d8e9642013-05-16 06:20:58 +0000859
860 case LCK_Init:
861 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregor01d08012012-02-07 10:09:13 +0000862 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000863 DeclAndBits.setInt(Bits);
Douglas Gregor01d08012012-02-07 10:09:13 +0000864}
865
Richard Smith0d8e9642013-05-16 06:20:58 +0000866LambdaExpr::Capture::Capture(FieldDecl *Field)
867 : DeclAndBits(Field,
868 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
869 Loc(Field->getLocation()), EllipsisLoc() {}
870
Douglas Gregor01d08012012-02-07 10:09:13 +0000871LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smith0d8e9642013-05-16 06:20:58 +0000872 Decl *D = DeclAndBits.getPointer();
873 if (!D)
Douglas Gregor01d08012012-02-07 10:09:13 +0000874 return LCK_This;
875
Richard Smith0d8e9642013-05-16 06:20:58 +0000876 if (isa<FieldDecl>(D))
877 return LCK_Init;
878
879 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregor01d08012012-02-07 10:09:13 +0000880}
881
James Dennettf68af642013-08-09 23:08:25 +0000882LambdaExpr::LambdaExpr(QualType T,
Douglas Gregor01d08012012-02-07 10:09:13 +0000883 SourceRange IntroducerRange,
884 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000885 SourceLocation CaptureDefaultLoc,
886 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000887 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000888 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000889 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000890 ArrayRef<VarDecl *> ArrayIndexVars,
891 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000892 SourceLocation ClosingBrace,
893 bool ContainsUnexpandedParameterPack)
Douglas Gregor01d08012012-02-07 10:09:13 +0000894 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
895 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith612409e2012-07-25 03:56:55 +0000896 ContainsUnexpandedParameterPack),
Douglas Gregor01d08012012-02-07 10:09:13 +0000897 IntroducerRange(IntroducerRange),
James Dennettf68af642013-08-09 23:08:25 +0000898 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000899 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000900 CaptureDefault(CaptureDefault),
901 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000902 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000903 ClosingBrace(ClosingBrace)
904{
905 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000906 CXXRecordDecl *Class = getLambdaClass();
907 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000908
909 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000910
911 // Copy captures.
Craig Topper32b5a1e2013-08-22 07:09:37 +0000912 const ASTContext &Context = Class->getASTContext();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000913 Data.NumCaptures = NumCaptures;
914 Data.NumExplicitCaptures = 0;
915 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
916 Capture *ToCapture = Data.Captures;
917 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
918 if (Captures[I].isExplicit())
919 ++Data.NumExplicitCaptures;
920
921 *ToCapture++ = Captures[I];
922 }
923
924 // Copy initialization expressions for the non-static data members.
925 Stmt **Stored = getStoredStmts();
926 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
927 *Stored++ = CaptureInits[I];
928
929 // Copy the body of the lambda.
930 *Stored++ = getCallOperator()->getBody();
931
932 // Copy the array index variables, if any.
933 HasArrayIndexVars = !ArrayIndexVars.empty();
934 if (HasArrayIndexVars) {
935 assert(ArrayIndexStarts.size() == NumCaptures);
936 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
937 sizeof(VarDecl *) * ArrayIndexVars.size());
938 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
939 sizeof(unsigned) * Captures.size());
940 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000941 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000942}
943
Craig Topper32b5a1e2013-08-22 07:09:37 +0000944LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregor01d08012012-02-07 10:09:13 +0000945 CXXRecordDecl *Class,
946 SourceRange IntroducerRange,
947 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000948 SourceLocation CaptureDefaultLoc,
949 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000950 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000951 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000952 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000953 ArrayRef<VarDecl *> ArrayIndexVars,
954 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000955 SourceLocation ClosingBrace,
956 bool ContainsUnexpandedParameterPack) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000957 // Determine the type of the expression (i.e., the type of the
958 // function object we're creating).
959 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000960
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000961 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smith88d2f672012-08-21 05:42:49 +0000962 if (!ArrayIndexVars.empty()) {
963 Size += sizeof(unsigned) * (Captures.size() + 1);
964 // Realign for following VarDecl array.
Richard Smitha796b6c2012-08-21 18:18:06 +0000965 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smith88d2f672012-08-21 05:42:49 +0000966 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
967 }
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000968 void *Mem = Context.Allocate(Size);
James Dennettf68af642013-08-09 23:08:25 +0000969 return new (Mem) LambdaExpr(T, IntroducerRange,
970 CaptureDefault, CaptureDefaultLoc, Captures,
971 ExplicitParams, ExplicitResultType,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000972 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000973 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregor01d08012012-02-07 10:09:13 +0000974}
975
Craig Topper32b5a1e2013-08-22 07:09:37 +0000976LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
977 unsigned NumCaptures,
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000978 unsigned NumArrayIndexVars) {
979 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
980 if (NumArrayIndexVars)
981 Size += sizeof(VarDecl) * NumArrayIndexVars
982 + sizeof(unsigned) * (NumCaptures + 1);
983 void *Mem = C.Allocate(Size);
984 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
985}
986
Douglas Gregorda8962a2012-02-13 15:44:47 +0000987LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000988 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000989}
990
991LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000992 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000993}
994
995LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
996 return capture_begin();
997}
998
999LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1000 struct CXXRecordDecl::LambdaDefinitionData &Data
1001 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001002 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +00001003}
1004
1005LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1006 return explicit_capture_end();
1007}
1008
1009LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1010 return capture_end();
1011}
1012
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001013ArrayRef<VarDecl *>
1014LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001015 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001016
1017 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +00001018 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1019 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001020 VarDecl **IndexVars = getArrayIndexVars();
1021 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001022 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1023 IndexVars + IndexStarts[Index + 1]);
1024}
1025
Douglas Gregor01d08012012-02-07 10:09:13 +00001026CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1027 return getType()->getAsCXXRecordDecl();
1028}
1029
1030CXXMethodDecl *LambdaExpr::getCallOperator() const {
1031 CXXRecordDecl *Record = getLambdaClass();
Manuel Klimek152b4e42013-08-22 12:12:24 +00001032 DeclarationName Name
1033 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1034 DeclContext::lookup_result Calls = Record->lookup(Name);
1035 assert(!Calls.empty() && "Missing lambda call operator!");
1036 assert(Calls.size() == 1 && "More than one lambda call operator!");
1037 CXXMethodDecl *Result = cast<CXXMethodDecl>(Calls.front());
1038 return Result;
Douglas Gregor01d08012012-02-07 10:09:13 +00001039}
1040
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00001041CompoundStmt *LambdaExpr::getBody() const {
1042 if (!getStoredStmts()[NumCaptures])
1043 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1044
1045 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1046}
1047
Douglas Gregor01d08012012-02-07 10:09:13 +00001048bool LambdaExpr::isMutable() const {
David Blaikie4ef832f2012-08-10 00:55:35 +00001049 return !getCallOperator()->isConst();
Douglas Gregor01d08012012-02-07 10:09:13 +00001050}
1051
John McCall80ee6e82011-11-10 05:35:25 +00001052ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1053 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +00001054 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001055 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001056 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001057 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001058 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +00001059 SubExpr(subexpr) {
1060 ExprWithCleanupsBits.NumObjects = objects.size();
1061 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1062 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001063}
1064
Craig Topper32b5a1e2013-08-22 07:09:37 +00001065ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall80ee6e82011-11-10 05:35:25 +00001066 ArrayRef<CleanupObject> objects) {
1067 size_t size = sizeof(ExprWithCleanups)
1068 + objects.size() * sizeof(CleanupObject);
1069 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1070 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +00001071}
1072
John McCall80ee6e82011-11-10 05:35:25 +00001073ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1074 : Expr(ExprWithCleanupsClass, empty) {
1075 ExprWithCleanupsBits.NumObjects = numObjects;
1076}
Chris Lattnerd2598362010-05-10 00:25:06 +00001077
Craig Topper32b5a1e2013-08-22 07:09:37 +00001078ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1079 EmptyShell empty,
John McCall80ee6e82011-11-10 05:35:25 +00001080 unsigned numObjects) {
1081 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1082 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1083 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +00001084}
1085
Douglas Gregorab6677e2010-09-08 00:15:04 +00001086CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001087 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001088 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001089 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +00001090 : Expr(CXXUnresolvedConstructExprClass,
1091 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +00001092 (Type->getType()->isLValueReferenceType() ? VK_LValue
1093 :Type->getType()->isRValueReferenceType()? VK_XValue
1094 :VK_RValue),
1095 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001096 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001097 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +00001098 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001099 LParenLoc(LParenLoc),
1100 RParenLoc(RParenLoc),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001101 NumArgs(Args.size()) {
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001102 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001103 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001104 if (Args[I]->containsUnexpandedParameterPack())
1105 ExprBits.ContainsUnexpandedParameterPack = true;
1106
1107 StoredArgs[I] = Args[I];
1108 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001109}
1110
1111CXXUnresolvedConstructExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001112CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +00001113 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001114 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001115 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001116 SourceLocation RParenLoc) {
1117 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001118 sizeof(Expr *) * Args.size());
1119 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001120}
1121
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001122CXXUnresolvedConstructExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001123CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001124 Stmt::EmptyShell Empty;
1125 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1126 sizeof(Expr *) * NumArgs);
1127 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1128}
1129
Erik Verbruggen65d78312012-12-25 14:51:39 +00001130SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1131 return Type->getTypeLoc().getBeginLoc();
Douglas Gregorab6677e2010-09-08 00:15:04 +00001132}
1133
Craig Topper32b5a1e2013-08-22 07:09:37 +00001134CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001135 Expr *Base, QualType BaseType,
1136 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001137 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001138 NestedNameSpecifierLoc QualifierLoc,
1139 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001140 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001141 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001142 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001143 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001144 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001145 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001146 (QualifierLoc &&
1147 QualifierLoc.getNestedNameSpecifier()
1148 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001149 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001150 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001151 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001152 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001153 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001154 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001155 if (TemplateArgs) {
1156 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001157 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001158 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001159 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1160 Dependent,
1161 InstantiationDependent,
1162 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001163 if (ContainsUnexpandedParameterPack)
1164 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001165 } else if (TemplateKWLoc.isValid()) {
1166 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001167 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001168}
1169
Craig Topper32b5a1e2013-08-22 07:09:37 +00001170CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001171 Expr *Base, QualType BaseType,
1172 bool IsArrow,
1173 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001174 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001175 NamedDecl *FirstQualifierFoundInScope,
1176 DeclarationNameInfo MemberNameInfo)
1177 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001178 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001179 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001180 (QualifierLoc &&
1181 QualifierLoc.getNestedNameSpecifier()->
1182 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001183 MemberNameInfo.containsUnexpandedParameterPack())),
1184 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001185 HasTemplateKWAndArgsInfo(false),
1186 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001187 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1188 MemberNameInfo(MemberNameInfo) { }
1189
John McCall865d4472009-11-19 22:55:06 +00001190CXXDependentScopeMemberExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001191CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001192 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001193 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001194 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001195 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001196 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001197 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001198 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001199 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001200 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1201 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001202 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001203 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001204 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001206 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1207 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1208 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001209
Chris Lattner32488542010-10-30 05:14:06 +00001210 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001211 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1212 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001213 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001214 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001215 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001216 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001217}
1218
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001219CXXDependentScopeMemberExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001220CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001221 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001222 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001223 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001224 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001225 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001226 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001227 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001228
1229 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001230 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001231 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001232 CXXDependentScopeMemberExpr *E
1233 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001234 0, SourceLocation(),
1235 NestedNameSpecifierLoc(),
1236 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001237 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001238 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001239 return E;
1240}
1241
Douglas Gregor4c9be892011-02-28 20:01:57 +00001242bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1243 if (Base == 0)
1244 return true;
1245
Douglas Gregor75e85042011-03-02 21:06:53 +00001246 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001247}
1248
John McCall864c0412011-04-26 20:42:42 +00001249static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1250 UnresolvedSetIterator end) {
1251 do {
1252 NamedDecl *decl = *begin;
1253 if (isa<UnresolvedUsingValueDecl>(decl))
1254 return false;
1255 if (isa<UsingShadowDecl>(decl))
1256 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1257
1258 // Unresolved member expressions should only contain methods and
1259 // method templates.
1260 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1261
1262 if (isa<FunctionTemplateDecl>(decl))
1263 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1264 if (cast<CXXMethodDecl>(decl)->isStatic())
1265 return false;
1266 } while (++begin != end);
1267
1268 return true;
1269}
1270
Craig Topper32b5a1e2013-08-22 07:09:37 +00001271UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001272 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001273 Expr *Base, QualType BaseType,
1274 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001275 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001276 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001277 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001278 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001279 const TemplateArgumentListInfo *TemplateArgs,
1280 UnresolvedSetIterator Begin,
1281 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001282 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1283 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001284 // Dependent
1285 ((Base && Base->isTypeDependent()) ||
1286 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001287 ((Base && Base->isInstantiationDependent()) ||
1288 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001289 // Contains unexpanded parameter pack
1290 ((Base && Base->containsUnexpandedParameterPack()) ||
1291 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001292 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1293 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001294
1295 // Check whether all of the members are non-static member functions,
1296 // and if so, mark give this bound-member type instead of overload type.
1297 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1298 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001299}
1300
Douglas Gregor4c9be892011-02-28 20:01:57 +00001301bool UnresolvedMemberExpr::isImplicitAccess() const {
1302 if (Base == 0)
1303 return true;
1304
Douglas Gregor75e85042011-03-02 21:06:53 +00001305 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001306}
1307
John McCall129e2df2009-11-30 22:42:35 +00001308UnresolvedMemberExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001309UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001310 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001311 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001312 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001313 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001314 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001315 const TemplateArgumentListInfo *TemplateArgs,
1316 UnresolvedSetIterator Begin,
1317 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001318 std::size_t size = sizeof(UnresolvedMemberExpr);
1319 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001320 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1321 else if (TemplateKWLoc.isValid())
1322 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001323
Chris Lattner32488542010-10-30 05:14:06 +00001324 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001325 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001326 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001327 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001328 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001329}
1330
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001331UnresolvedMemberExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001332UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1333 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001334 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001335 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001336 if (HasTemplateKWAndArgsInfo)
1337 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001338
Chris Lattner32488542010-10-30 05:14:06 +00001339 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001340 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001341 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001342 return E;
1343}
1344
John McCallc373d482010-01-27 01:50:18 +00001345CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1346 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1347
1348 // If there was a nested name specifier, it names the naming class.
1349 // It can't be dependent: after all, we were actually able to do the
1350 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001351 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001352 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001353 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001354 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001355 Record = T->getAsCXXRecordDecl();
1356 assert(Record && "qualifier in member expression does not name record");
1357 }
John McCallc373d482010-01-27 01:50:18 +00001358 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001359 else {
John McCallc373d482010-01-27 01:50:18 +00001360 QualType BaseType = getBaseType().getNonReferenceType();
1361 if (isArrow()) {
1362 const PointerType *PT = BaseType->getAs<PointerType>();
1363 assert(PT && "base of arrow member access is not pointer");
1364 BaseType = PT->getPointeeType();
1365 }
1366
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001367 Record = BaseType->getAsCXXRecordDecl();
1368 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001369 }
1370
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001371 return Record;
John McCallc373d482010-01-27 01:50:18 +00001372}
1373
Douglas Gregorc7793c72011-01-15 01:15:58 +00001374SubstNonTypeTemplateParmPackExpr::
1375SubstNonTypeTemplateParmPackExpr(QualType T,
1376 NonTypeTemplateParmDecl *Param,
1377 SourceLocation NameLoc,
1378 const TemplateArgument &ArgPack)
1379 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001380 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001381 Param(Param), Arguments(ArgPack.pack_begin()),
1382 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1383
1384TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1385 return TemplateArgument(Arguments, NumArguments);
1386}
1387
Richard Smith9a4db032012-09-12 00:56:43 +00001388FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1389 SourceLocation NameLoc,
1390 unsigned NumParams,
1391 Decl * const *Params)
1392 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1393 true, true, true, true),
1394 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1395 if (Params)
1396 std::uninitialized_copy(Params, Params + NumParams,
1397 reinterpret_cast<Decl**>(this+1));
1398}
1399
1400FunctionParmPackExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001401FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smith9a4db032012-09-12 00:56:43 +00001402 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001403 ArrayRef<Decl *> Params) {
Richard Smith9a4db032012-09-12 00:56:43 +00001404 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1405 sizeof(ParmVarDecl*) * Params.size()))
1406 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1407}
1408
1409FunctionParmPackExpr *
Craig Topper32b5a1e2013-08-22 07:09:37 +00001410FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1411 unsigned NumParams) {
Richard Smith9a4db032012-09-12 00:56:43 +00001412 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1413 sizeof(ParmVarDecl*) * NumParams))
1414 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1415}
1416
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001417TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1418 ArrayRef<TypeSourceInfo *> Args,
1419 SourceLocation RParenLoc,
1420 bool Value)
1421 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1422 /*TypeDependent=*/false,
1423 /*ValueDependent=*/false,
1424 /*InstantiationDependent=*/false,
1425 /*ContainsUnexpandedParameterPack=*/false),
1426 Loc(Loc), RParenLoc(RParenLoc)
1427{
1428 TypeTraitExprBits.Kind = Kind;
1429 TypeTraitExprBits.Value = Value;
1430 TypeTraitExprBits.NumArgs = Args.size();
1431
1432 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1433
1434 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1435 if (Args[I]->getType()->isDependentType())
1436 setValueDependent(true);
1437 if (Args[I]->getType()->isInstantiationDependentType())
1438 setInstantiationDependent(true);
1439 if (Args[I]->getType()->containsUnexpandedParameterPack())
1440 setContainsUnexpandedParameterPack(true);
1441
1442 ToArgs[I] = Args[I];
1443 }
1444}
1445
Craig Topper32b5a1e2013-08-22 07:09:37 +00001446TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001447 SourceLocation Loc,
1448 TypeTrait Kind,
1449 ArrayRef<TypeSourceInfo *> Args,
1450 SourceLocation RParenLoc,
1451 bool Value) {
1452 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1453 void *Mem = C.Allocate(Size);
1454 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1455}
1456
Craig Topper32b5a1e2013-08-22 07:09:37 +00001457TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001458 unsigned NumArgs) {
1459 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1460 void *Mem = C.Allocate(Size);
1461 return new (Mem) TypeTraitExpr(EmptyShell());
1462}
1463
David Blaikie99ba9e32011-12-20 02:48:34 +00001464void ArrayTypeTraitExpr::anchor() { }