blob: 055fc3b8142b160c59093f9e55da34d323601c0f [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
Ted Kremenekad7fe862010-02-11 22:51:03 +000096CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl1548d142012-02-16 11:35:52 +000097 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
Chris Lattner59218632010-05-10 01:22:27 +0000159void CXXNewExpr::AllocateArgsArray(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
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000169bool CXXNewExpr::shouldNullCheckAllocation(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
John McCalle23cf432010-12-14 08:05:40 +0000193CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(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,
Dmitri Gribenko55431692013-05-05 00:41:58 +0000199 Context.getPointerType(Context.getFunctionType(Context.VoidTy, None,
John McCalle23cf432010-12-14 08:05:40 +0000200 FunctionProtoType::ExtProtoInfo())),
201 VK_RValue, OK_Ordinary,
202 /*isTypeDependent=*/(Base->isTypeDependent() ||
203 (DestroyedType.getTypeSourceInfo() &&
204 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000205 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000206 (Base->isInstantiationDependent() ||
207 (QualifierLoc &&
208 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
209 (ScopeType &&
210 ScopeType->getType()->isInstantiationDependentType()) ||
211 (DestroyedType.getTypeSourceInfo() &&
212 DestroyedType.getTypeSourceInfo()->getType()
213 ->isInstantiationDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000214 // ContainsUnexpandedParameterPack
215 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000216 (QualifierLoc &&
217 QualifierLoc.getNestedNameSpecifier()
218 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000219 (ScopeType &&
220 ScopeType->getType()->containsUnexpandedParameterPack()) ||
221 (DestroyedType.getTypeSourceInfo() &&
222 DestroyedType.getTypeSourceInfo()->getType()
223 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000224 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000225 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000226 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
227 DestroyedType(DestroyedType) { }
228
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000229QualType CXXPseudoDestructorExpr::getDestroyedType() const {
230 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
231 return TInfo->getType();
232
233 return QualType();
234}
235
Erik Verbruggen65d78312012-12-25 14:51:39 +0000236SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000237 SourceLocation End = DestroyedType.getLocation();
238 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000239 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen65d78312012-12-25 14:51:39 +0000240 return End;
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000241}
242
John McCallba135432009-11-21 08:51:07 +0000243// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000244UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000245UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000246 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000247 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000248 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000249 const DeclarationNameInfo &NameInfo,
250 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000251 const TemplateArgumentListInfo *Args,
252 UnresolvedSetIterator Begin,
253 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000254{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000255 assert(Args || TemplateKWLoc.isValid());
256 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000257 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000258 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000259 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
260 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000261 ADL, /*Overload*/ true, Args,
Richard Smithb1502bc2012-10-18 17:56:02 +0000262 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000263}
264
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000265UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000266UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
267 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000268 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000269 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000270 if (HasTemplateKWAndArgsInfo)
271 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000272
Chris Lattner32488542010-10-30 05:14:06 +0000273 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000274 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000275 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000276 return E;
277}
278
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000279OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000280 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000281 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000282 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000283 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000284 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000285 UnresolvedSetIterator End,
286 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000287 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000288 bool KnownContainsUnexpandedParameterPack)
289 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
290 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000291 (KnownInstantiationDependent ||
292 NameInfo.isInstantiationDependent() ||
293 (QualifierLoc &&
294 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000295 (KnownContainsUnexpandedParameterPack ||
296 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000297 (QualifierLoc &&
298 QualifierLoc.getNestedNameSpecifier()
299 ->containsUnexpandedParameterPack()))),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000300 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
301 Results(0), NumResults(End - Begin),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000302 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000303{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000304 NumResults = End - Begin;
305 if (NumResults) {
306 // Determine whether this expression is type-dependent.
307 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
308 if ((*I)->getDeclContext()->isDependentContext() ||
309 isa<UnresolvedUsingValueDecl>(*I)) {
310 ExprBits.TypeDependent = true;
311 ExprBits.ValueDependent = true;
Richard Smith7657fd72012-08-13 21:29:18 +0000312 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000313 }
314 }
315
316 Results = static_cast<DeclAccessPair *>(
317 C.Allocate(sizeof(DeclAccessPair) * NumResults,
318 llvm::alignOf<DeclAccessPair>()));
319 memcpy(Results, &*Begin.getIterator(),
320 NumResults * sizeof(DeclAccessPair));
321 }
322
323 // If we have explicit template arguments, check for dependent
324 // template arguments and whether they contain any unexpanded pack
325 // expansions.
326 if (TemplateArgs) {
327 bool Dependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000328 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000329 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000330 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
331 Dependent,
332 InstantiationDependent,
333 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000334
335 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000336 ExprBits.TypeDependent = true;
337 ExprBits.ValueDependent = true;
338 }
339 if (InstantiationDependent)
340 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000341 if (ContainsUnexpandedParameterPack)
342 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000343 } else if (TemplateKWLoc.isValid()) {
344 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000345 }
346
347 if (isTypeDependent())
348 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000349}
350
351void OverloadExpr::initializeResults(ASTContext &C,
352 UnresolvedSetIterator Begin,
353 UnresolvedSetIterator End) {
354 assert(Results == 0 && "Results already initialized!");
355 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000356 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000357 Results = static_cast<DeclAccessPair *>(
358 C.Allocate(sizeof(DeclAccessPair) * NumResults,
359
360 llvm::alignOf<DeclAccessPair>()));
361 memcpy(Results, &*Begin.getIterator(),
362 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000363 }
364}
365
John McCalle9ee23e2010-04-22 18:44:12 +0000366CXXRecordDecl *OverloadExpr::getNamingClass() const {
367 if (isa<UnresolvedLookupExpr>(this))
368 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
369 else
370 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
371}
372
John McCall865d4472009-11-19 22:55:06 +0000373// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000374DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000375 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000376 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000377 const DeclarationNameInfo &NameInfo,
378 const TemplateArgumentListInfo *Args)
379 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
380 true, true,
Douglas Gregor561f8122011-07-01 01:22:09 +0000381 (NameInfo.isInstantiationDependent() ||
382 (QualifierLoc &&
383 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000384 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000385 (QualifierLoc &&
386 QualifierLoc.getNestedNameSpecifier()
387 ->containsUnexpandedParameterPack()))),
388 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000389 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000390{
391 if (Args) {
392 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000393 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000394 bool ContainsUnexpandedParameterPack
395 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000396 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
397 Dependent,
398 InstantiationDependent,
399 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000400 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000401 } else if (TemplateKWLoc.isValid()) {
402 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000403 }
404}
405
John McCallf7a1a742009-11-24 19:00:30 +0000406DependentScopeDeclRefExpr *
407DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000408 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000409 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000410 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000411 const TemplateArgumentListInfo *Args) {
412 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000413 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000414 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
415 else if (TemplateKWLoc.isValid())
416 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000417 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000418 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
419 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000420}
421
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000422DependentScopeDeclRefExpr *
423DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000424 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000425 unsigned NumTemplateArgs) {
426 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000427 if (HasTemplateKWAndArgsInfo)
428 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000429 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000430 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000431 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000432 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000433 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000434 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000435 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000436}
437
Erik Verbruggen65d78312012-12-25 14:51:39 +0000438SourceLocation CXXConstructExpr::getLocStart() const {
John McCall2882eca2011-02-21 06:23:05 +0000439 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen65d78312012-12-25 14:51:39 +0000440 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
441 return Loc;
442}
443
444SourceLocation CXXConstructExpr::getLocEnd() const {
445 if (isa<CXXTemporaryObjectExpr>(this))
446 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall2882eca2011-02-21 06:23:05 +0000447
Douglas Gregor40749ee2010-11-03 00:35:38 +0000448 if (ParenRange.isValid())
Erik Verbruggen65d78312012-12-25 14:51:39 +0000449 return ParenRange.getEnd();
Douglas Gregor40749ee2010-11-03 00:35:38 +0000450
451 SourceLocation End = Loc;
452 for (unsigned I = getNumArgs(); I > 0; --I) {
453 const Expr *Arg = getArg(I-1);
454 if (!Arg->isDefaultArgument()) {
455 SourceLocation NewEnd = Arg->getLocEnd();
456 if (NewEnd.isValid()) {
457 End = NewEnd;
458 break;
459 }
460 }
461 }
462
Erik Verbruggen65d78312012-12-25 14:51:39 +0000463 return End;
Ted Kremeneke3837682009-12-23 04:00:48 +0000464}
465
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000466SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregorb4609802008-11-14 16:09:21 +0000467 OverloadedOperatorKind Kind = getOperator();
468 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
469 if (getNumArgs() == 1)
470 // Prefix operator
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000471 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000472 else
473 // Postfix operator
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000474 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000475 } else if (Kind == OO_Arrow) {
476 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000477 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000478 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000479 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000480 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000481 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000482 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000483 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000484 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000485 } else {
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000486 return getOperatorLoc();
Douglas Gregorb4609802008-11-14 16:09:21 +0000487 }
488}
489
Ted Kremenekb2771592011-03-30 17:41:19 +0000490Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose51e87c52012-08-03 23:08:39 +0000491 const Expr *Callee = getCallee()->IgnoreParens();
492 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor88a35142008-12-22 05:46:06 +0000493 return MemExpr->getBase();
Jordan Rose51e87c52012-08-03 23:08:39 +0000494 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
495 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
496 return BO->getLHS();
Douglas Gregor88a35142008-12-22 05:46:06 +0000497
498 // FIXME: Will eventually need to cope with member pointers.
499 return 0;
500}
501
Ted Kremenekb2771592011-03-30 17:41:19 +0000502CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
503 if (const MemberExpr *MemExpr =
504 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
505 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
506
507 // FIXME: Will eventually need to cope with member pointers.
508 return 0;
509}
510
511
David Blaikie0cf3c0e2012-05-03 16:25:49 +0000512CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth007a9b12010-10-27 06:55:41 +0000513 Expr* ThisArg = getImplicitObjectArgument();
514 if (!ThisArg)
515 return 0;
516
517 if (ThisArg->getType()->isAnyPointerType())
518 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
519
520 return ThisArg->getType()->getAsCXXRecordDecl();
521}
522
Douglas Gregor00b98c22009-11-12 15:31:47 +0000523
Douglas Gregor49badde2008-10-27 19:41:14 +0000524//===----------------------------------------------------------------------===//
525// Named casts
526//===----------------------------------------------------------------------===//
527
528/// getCastName - Get the name of the C++ cast being used, e.g.,
529/// "static_cast", "dynamic_cast", "reinterpret_cast", or
530/// "const_cast". The returned pointer must not be freed.
531const char *CXXNamedCastExpr::getCastName() const {
532 switch (getStmtClass()) {
533 case CXXStaticCastExprClass: return "static_cast";
534 case CXXDynamicCastExprClass: return "dynamic_cast";
535 case CXXReinterpretCastExprClass: return "reinterpret_cast";
536 case CXXConstCastExprClass: return "const_cast";
537 default: return "<invalid cast>";
538 }
539}
Douglas Gregor506ae412009-01-16 18:33:17 +0000540
John McCallf871d0c2010-08-07 06:22:56 +0000541CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000542 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000543 CastKind K, Expr *Op,
544 const CXXCastPath *BasePath,
545 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000546 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000547 SourceLocation RParenLoc,
548 SourceRange AngleBrackets) {
John McCallf871d0c2010-08-07 06:22:56 +0000549 unsigned PathSize = (BasePath ? BasePath->size() : 0);
550 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
551 + PathSize * sizeof(CXXBaseSpecifier*));
552 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000553 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000554 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000555 if (PathSize) E->setCastPath(*BasePath);
556 return E;
557}
558
559CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
560 unsigned PathSize) {
561 void *Buffer =
562 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
563 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
564}
565
566CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000567 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000568 CastKind K, Expr *Op,
569 const CXXCastPath *BasePath,
570 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000571 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000572 SourceLocation RParenLoc,
573 SourceRange AngleBrackets) {
John McCallf871d0c2010-08-07 06:22:56 +0000574 unsigned PathSize = (BasePath ? BasePath->size() : 0);
575 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
576 + PathSize * sizeof(CXXBaseSpecifier*));
577 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000578 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000579 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000580 if (PathSize) E->setCastPath(*BasePath);
581 return E;
582}
583
584CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
585 unsigned PathSize) {
586 void *Buffer =
587 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
588 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
589}
590
Anders Carlsson0fee3302011-04-11 01:43:55 +0000591/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
592/// to always be null. For example:
593///
594/// struct A { };
595/// struct B final : A { };
596/// struct C { };
597///
598/// C *f(B* b) { return dynamic_cast<C*>(b); }
599bool CXXDynamicCastExpr::isAlwaysNull() const
600{
601 QualType SrcType = getSubExpr()->getType();
602 QualType DestType = getType();
603
604 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
605 SrcType = SrcPTy->getPointeeType();
606 DestType = DestType->castAs<PointerType>()->getPointeeType();
607 }
608
Sean Hunt5ca86392012-06-19 23:44:55 +0000609 if (DestType->isVoidType())
610 return false;
611
Anders Carlsson0fee3302011-04-11 01:43:55 +0000612 const CXXRecordDecl *SrcRD =
613 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
614
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000615 if (!SrcRD->hasAttr<FinalAttr>())
616 return false;
617
Anders Carlsson0fee3302011-04-11 01:43:55 +0000618 const CXXRecordDecl *DestRD =
619 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
620
621 return !DestRD->isDerivedFrom(SrcRD);
622}
623
John McCallf871d0c2010-08-07 06:22:56 +0000624CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000625CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
626 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000627 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000628 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000629 SourceLocation RParenLoc,
630 SourceRange AngleBrackets) {
John McCallf871d0c2010-08-07 06:22:56 +0000631 unsigned PathSize = (BasePath ? BasePath->size() : 0);
632 void *Buffer =
633 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
634 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000635 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000636 RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000637 if (PathSize) E->setCastPath(*BasePath);
638 return E;
639}
640
641CXXReinterpretCastExpr *
642CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
643 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
644 + PathSize * sizeof(CXXBaseSpecifier*));
645 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
646}
647
John McCallf89e55a2010-11-18 06:31:45 +0000648CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
649 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000650 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000651 SourceLocation L,
Fariborz Jahanianf799ae12013-02-22 22:02:53 +0000652 SourceLocation RParenLoc,
653 SourceRange AngleBrackets) {
654 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallf871d0c2010-08-07 06:22:56 +0000655}
656
657CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
658 return new (C) CXXConstCastExpr(EmptyShell());
659}
660
661CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000662CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000663 TypeSourceInfo *Written, SourceLocation L,
664 CastKind K, Expr *Op, const CXXCastPath *BasePath,
665 SourceLocation R) {
666 unsigned PathSize = (BasePath ? BasePath->size() : 0);
667 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
668 + PathSize * sizeof(CXXBaseSpecifier*));
669 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000670 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000671 if (PathSize) E->setCastPath(*BasePath);
672 return E;
673}
674
675CXXFunctionalCastExpr *
676CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
677 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
678 + PathSize * sizeof(CXXBaseSpecifier*));
679 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
680}
681
Richard Smith9fcce652012-03-07 08:35:16 +0000682UserDefinedLiteral::LiteralOperatorKind
683UserDefinedLiteral::getLiteralOperatorKind() const {
684 if (getNumArgs() == 0)
685 return LOK_Template;
686 if (getNumArgs() == 2)
687 return LOK_String;
688
689 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
690 QualType ParamTy =
691 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
692 if (ParamTy->isPointerType())
693 return LOK_Raw;
694 if (ParamTy->isAnyCharacterType())
695 return LOK_Character;
696 if (ParamTy->isIntegerType())
697 return LOK_Integer;
698 if (ParamTy->isFloatingType())
699 return LOK_Floating;
700
701 llvm_unreachable("unknown kind of literal operator");
702}
703
704Expr *UserDefinedLiteral::getCookedLiteral() {
705#ifndef NDEBUG
706 LiteralOperatorKind LOK = getLiteralOperatorKind();
707 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
708#endif
709 return getArg(0);
710}
711
712const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
713 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
714}
John McCallf871d0c2010-08-07 06:22:56 +0000715
Douglas Gregor65222e82009-12-23 18:19:08 +0000716CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000717CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
718 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000719 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000720 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
721 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000722}
723
Richard Smithc3bf52c2013-04-20 22:23:05 +0000724CXXDefaultInitExpr::CXXDefaultInitExpr(ASTContext &C, SourceLocation Loc,
725 FieldDecl *Field, QualType T)
726 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
727 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
728 ? VK_XValue
729 : VK_RValue,
730 /*FIXME*/ OK_Ordinary, false, false, false, false),
731 Field(Field), Loc(Loc) {
732 assert(Field->hasInClassInitializer());
733}
734
Mike Stump1eb44332009-09-09 15:08:12 +0000735CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000736 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000737 return new (C) CXXTemporary(Destructor);
738}
739
Mike Stump1eb44332009-09-09 15:08:12 +0000740CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000741 CXXTemporary *Temp,
742 Expr* SubExpr) {
Peter Collingbournebceb7552011-11-27 22:09:28 +0000743 assert((SubExpr->getType()->isRecordType() ||
744 SubExpr->getType()->isArrayType()) &&
745 "Expression bound to a temporary must have record or array type!");
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000746
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000747 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000748}
749
Anders Carlsson8e587a12009-05-30 20:56:46 +0000750CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000751 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000752 TypeSourceInfo *Type,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000753 ArrayRef<Expr*> Args,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000754 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000755 bool HadMultipleCandidates,
Richard Smithc83c2302012-12-19 01:39:02 +0000756 bool ListInitialization,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000757 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000758 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
759 Type->getType().getNonReferenceType(),
760 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000761 Cons, false, Args,
Richard Smithc83c2302012-12-19 01:39:02 +0000762 HadMultipleCandidates,
763 ListInitialization, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000764 CXXConstructExpr::CK_Complete, parenRange),
765 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000766}
767
Erik Verbruggen65d78312012-12-25 14:51:39 +0000768SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
769 return Type->getTypeLoc().getBeginLoc();
770}
771
772SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
773 return getParenRange().getEnd();
Douglas Gregor506ae412009-01-16 18:33:17 +0000774}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000775
Mike Stump1eb44332009-09-09 15:08:12 +0000776CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000777 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000778 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000779 ArrayRef<Expr*> Args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000780 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000781 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000782 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000783 ConstructionKind ConstructKind,
784 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000785 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000786 Elidable, Args,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000787 HadMultipleCandidates, ListInitialization,
788 ZeroInitialization, ConstructKind,
789 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000790}
791
Mike Stump1eb44332009-09-09 15:08:12 +0000792CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000793 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000794 CXXConstructorDecl *D, bool elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000795 ArrayRef<Expr*> args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000796 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000797 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000798 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000799 ConstructionKind ConstructKind,
800 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000801 : Expr(SC, T, VK_RValue, OK_Ordinary,
802 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000803 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000804 T->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000805 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(args.size()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000806 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000807 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000808 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000809 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000810{
811 if (NumArgs) {
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000812 Args = new (C) Stmt*[args.size()];
Douglas Gregor16006c92009-12-16 18:50:27 +0000813
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000814 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000815 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000816
817 if (args[i]->isValueDependent())
818 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000819 if (args[i]->isInstantiationDependent())
820 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000821 if (args[i]->containsUnexpandedParameterPack())
822 ExprBits.ContainsUnexpandedParameterPack = true;
823
Douglas Gregor16006c92009-12-16 18:50:27 +0000824 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000825 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000826 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000827}
828
Douglas Gregor01d08012012-02-07 10:09:13 +0000829LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
830 LambdaCaptureKind Kind, VarDecl *Var,
831 SourceLocation EllipsisLoc)
Richard Smith0d8e9642013-05-16 06:20:58 +0000832 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregor01d08012012-02-07 10:09:13 +0000833{
834 unsigned Bits = 0;
835 if (Implicit)
836 Bits |= Capture_Implicit;
837
838 switch (Kind) {
839 case LCK_This:
840 assert(Var == 0 && "'this' capture cannot have a variable!");
841 break;
842
843 case LCK_ByCopy:
844 Bits |= Capture_ByCopy;
845 // Fall through
846 case LCK_ByRef:
847 assert(Var && "capture must have a variable!");
848 break;
Richard Smith0d8e9642013-05-16 06:20:58 +0000849
850 case LCK_Init:
851 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregor01d08012012-02-07 10:09:13 +0000852 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000853 DeclAndBits.setInt(Bits);
Douglas Gregor01d08012012-02-07 10:09:13 +0000854}
855
Richard Smith0d8e9642013-05-16 06:20:58 +0000856LambdaExpr::Capture::Capture(FieldDecl *Field)
857 : DeclAndBits(Field,
858 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
859 Loc(Field->getLocation()), EllipsisLoc() {}
860
Douglas Gregor01d08012012-02-07 10:09:13 +0000861LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smith0d8e9642013-05-16 06:20:58 +0000862 Decl *D = DeclAndBits.getPointer();
863 if (!D)
Douglas Gregor01d08012012-02-07 10:09:13 +0000864 return LCK_This;
865
Richard Smith0d8e9642013-05-16 06:20:58 +0000866 if (isa<FieldDecl>(D))
867 return LCK_Init;
868
869 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregor01d08012012-02-07 10:09:13 +0000870}
871
James Dennettf68af642013-08-09 23:08:25 +0000872LambdaExpr::LambdaExpr(QualType T,
Douglas Gregor01d08012012-02-07 10:09:13 +0000873 SourceRange IntroducerRange,
874 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000875 SourceLocation CaptureDefaultLoc,
876 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000877 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000878 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000879 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000880 ArrayRef<VarDecl *> ArrayIndexVars,
881 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000882 SourceLocation ClosingBrace,
883 bool ContainsUnexpandedParameterPack)
Douglas Gregor01d08012012-02-07 10:09:13 +0000884 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
885 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith612409e2012-07-25 03:56:55 +0000886 ContainsUnexpandedParameterPack),
Douglas Gregor01d08012012-02-07 10:09:13 +0000887 IntroducerRange(IntroducerRange),
James Dennettf68af642013-08-09 23:08:25 +0000888 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000889 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000890 CaptureDefault(CaptureDefault),
891 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000892 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000893 ClosingBrace(ClosingBrace)
894{
895 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000896 CXXRecordDecl *Class = getLambdaClass();
897 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000898
899 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000900
901 // Copy captures.
902 ASTContext &Context = Class->getASTContext();
903 Data.NumCaptures = NumCaptures;
904 Data.NumExplicitCaptures = 0;
905 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
906 Capture *ToCapture = Data.Captures;
907 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
908 if (Captures[I].isExplicit())
909 ++Data.NumExplicitCaptures;
910
911 *ToCapture++ = Captures[I];
912 }
913
914 // Copy initialization expressions for the non-static data members.
915 Stmt **Stored = getStoredStmts();
916 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
917 *Stored++ = CaptureInits[I];
918
919 // Copy the body of the lambda.
920 *Stored++ = getCallOperator()->getBody();
921
922 // Copy the array index variables, if any.
923 HasArrayIndexVars = !ArrayIndexVars.empty();
924 if (HasArrayIndexVars) {
925 assert(ArrayIndexStarts.size() == NumCaptures);
926 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
927 sizeof(VarDecl *) * ArrayIndexVars.size());
928 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
929 sizeof(unsigned) * Captures.size());
930 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000931 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000932}
933
James Dennettf68af642013-08-09 23:08:25 +0000934LambdaExpr *LambdaExpr::Create(ASTContext &Context,
Douglas Gregor01d08012012-02-07 10:09:13 +0000935 CXXRecordDecl *Class,
936 SourceRange IntroducerRange,
937 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000938 SourceLocation CaptureDefaultLoc,
939 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000940 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000941 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000942 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000943 ArrayRef<VarDecl *> ArrayIndexVars,
944 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000945 SourceLocation ClosingBrace,
946 bool ContainsUnexpandedParameterPack) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000947 // Determine the type of the expression (i.e., the type of the
948 // function object we're creating).
949 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000950
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000951 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smith88d2f672012-08-21 05:42:49 +0000952 if (!ArrayIndexVars.empty()) {
953 Size += sizeof(unsigned) * (Captures.size() + 1);
954 // Realign for following VarDecl array.
Richard Smitha796b6c2012-08-21 18:18:06 +0000955 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smith88d2f672012-08-21 05:42:49 +0000956 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
957 }
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000958 void *Mem = Context.Allocate(Size);
James Dennettf68af642013-08-09 23:08:25 +0000959 return new (Mem) LambdaExpr(T, IntroducerRange,
960 CaptureDefault, CaptureDefaultLoc, Captures,
961 ExplicitParams, ExplicitResultType,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000962 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000963 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregor01d08012012-02-07 10:09:13 +0000964}
965
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000966LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
967 unsigned NumArrayIndexVars) {
968 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
969 if (NumArrayIndexVars)
970 Size += sizeof(VarDecl) * NumArrayIndexVars
971 + sizeof(unsigned) * (NumCaptures + 1);
972 void *Mem = C.Allocate(Size);
973 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
974}
975
Douglas Gregorda8962a2012-02-13 15:44:47 +0000976LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000977 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000978}
979
980LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000981 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000982}
983
984LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
985 return capture_begin();
986}
987
988LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
989 struct CXXRecordDecl::LambdaDefinitionData &Data
990 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000991 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000992}
993
994LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
995 return explicit_capture_end();
996}
997
998LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
999 return capture_end();
1000}
1001
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001002ArrayRef<VarDecl *>
1003LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001004 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001005
1006 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +00001007 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1008 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001009 VarDecl **IndexVars = getArrayIndexVars();
1010 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001011 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1012 IndexVars + IndexStarts[Index + 1]);
1013}
1014
Douglas Gregor01d08012012-02-07 10:09:13 +00001015CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1016 return getType()->getAsCXXRecordDecl();
1017}
1018
1019CXXMethodDecl *LambdaExpr::getCallOperator() const {
1020 CXXRecordDecl *Record = getLambdaClass();
1021 DeclarationName Name
1022 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1023 DeclContext::lookup_result Calls = Record->lookup(Name);
David Blaikie3bc93e32012-12-19 00:45:41 +00001024 assert(!Calls.empty() && "Missing lambda call operator!");
1025 assert(Calls.size() == 1 && "More than one lambda call operator!");
1026 CXXMethodDecl *Result = cast<CXXMethodDecl>(Calls.front());
Douglas Gregor01d08012012-02-07 10:09:13 +00001027 return Result;
1028}
1029
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00001030CompoundStmt *LambdaExpr::getBody() const {
1031 if (!getStoredStmts()[NumCaptures])
1032 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1033
1034 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1035}
1036
Douglas Gregor01d08012012-02-07 10:09:13 +00001037bool LambdaExpr::isMutable() const {
David Blaikie4ef832f2012-08-10 00:55:35 +00001038 return !getCallOperator()->isConst();
Douglas Gregor01d08012012-02-07 10:09:13 +00001039}
1040
John McCall80ee6e82011-11-10 05:35:25 +00001041ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1042 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +00001043 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001044 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001045 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001046 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001047 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +00001048 SubExpr(subexpr) {
1049 ExprWithCleanupsBits.NumObjects = objects.size();
1050 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1051 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001052}
1053
John McCall80ee6e82011-11-10 05:35:25 +00001054ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
1055 ArrayRef<CleanupObject> objects) {
1056 size_t size = sizeof(ExprWithCleanups)
1057 + objects.size() * sizeof(CleanupObject);
1058 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1059 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +00001060}
1061
John McCall80ee6e82011-11-10 05:35:25 +00001062ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1063 : Expr(ExprWithCleanupsClass, empty) {
1064 ExprWithCleanupsBits.NumObjects = numObjects;
1065}
Chris Lattnerd2598362010-05-10 00:25:06 +00001066
John McCall80ee6e82011-11-10 05:35:25 +00001067ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
1068 unsigned numObjects) {
1069 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1070 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1071 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +00001072}
1073
Douglas Gregorab6677e2010-09-08 00:15:04 +00001074CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001075 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001076 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001077 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +00001078 : Expr(CXXUnresolvedConstructExprClass,
1079 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +00001080 (Type->getType()->isLValueReferenceType() ? VK_LValue
1081 :Type->getType()->isRValueReferenceType()? VK_XValue
1082 :VK_RValue),
1083 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001084 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001085 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +00001086 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001087 LParenLoc(LParenLoc),
1088 RParenLoc(RParenLoc),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001089 NumArgs(Args.size()) {
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001090 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001091 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001092 if (Args[I]->containsUnexpandedParameterPack())
1093 ExprBits.ContainsUnexpandedParameterPack = true;
1094
1095 StoredArgs[I] = Args[I];
1096 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001097}
1098
1099CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001100CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +00001101 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001102 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001103 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001104 SourceLocation RParenLoc) {
1105 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001106 sizeof(Expr *) * Args.size());
1107 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001108}
1109
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001110CXXUnresolvedConstructExpr *
1111CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1112 Stmt::EmptyShell Empty;
1113 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1114 sizeof(Expr *) * NumArgs);
1115 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1116}
1117
Erik Verbruggen65d78312012-12-25 14:51:39 +00001118SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1119 return Type->getTypeLoc().getBeginLoc();
Douglas Gregorab6677e2010-09-08 00:15:04 +00001120}
1121
John McCall865d4472009-11-19 22:55:06 +00001122CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001123 Expr *Base, QualType BaseType,
1124 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001125 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001126 NestedNameSpecifierLoc QualifierLoc,
1127 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001128 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001129 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001130 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001131 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001132 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001133 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001134 (QualifierLoc &&
1135 QualifierLoc.getNestedNameSpecifier()
1136 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001137 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001138 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001139 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001140 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001141 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001142 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001143 if (TemplateArgs) {
1144 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001145 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001146 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001147 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1148 Dependent,
1149 InstantiationDependent,
1150 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001151 if (ContainsUnexpandedParameterPack)
1152 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001153 } else if (TemplateKWLoc.isValid()) {
1154 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001155 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001156}
1157
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001158CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1159 Expr *Base, QualType BaseType,
1160 bool IsArrow,
1161 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001162 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001163 NamedDecl *FirstQualifierFoundInScope,
1164 DeclarationNameInfo MemberNameInfo)
1165 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001166 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001167 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001168 (QualifierLoc &&
1169 QualifierLoc.getNestedNameSpecifier()->
1170 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001171 MemberNameInfo.containsUnexpandedParameterPack())),
1172 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001173 HasTemplateKWAndArgsInfo(false),
1174 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001175 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1176 MemberNameInfo(MemberNameInfo) { }
1177
John McCall865d4472009-11-19 22:55:06 +00001178CXXDependentScopeMemberExpr *
1179CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001180 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001181 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001182 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001183 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001184 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001185 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001186 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001187 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001188 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1189 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001190 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001191 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001192 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001194 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1195 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1196 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001197
Chris Lattner32488542010-10-30 05:14:06 +00001198 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001199 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1200 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001201 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001202 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001203 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001204 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001205}
1206
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001207CXXDependentScopeMemberExpr *
1208CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001209 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001210 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001211 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001212 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001213 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001214 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001215 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001216
1217 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001218 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001219 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001220 CXXDependentScopeMemberExpr *E
1221 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001222 0, SourceLocation(),
1223 NestedNameSpecifierLoc(),
1224 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001225 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001226 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001227 return E;
1228}
1229
Douglas Gregor4c9be892011-02-28 20:01:57 +00001230bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1231 if (Base == 0)
1232 return true;
1233
Douglas Gregor75e85042011-03-02 21:06:53 +00001234 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001235}
1236
John McCall864c0412011-04-26 20:42:42 +00001237static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1238 UnresolvedSetIterator end) {
1239 do {
1240 NamedDecl *decl = *begin;
1241 if (isa<UnresolvedUsingValueDecl>(decl))
1242 return false;
1243 if (isa<UsingShadowDecl>(decl))
1244 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1245
1246 // Unresolved member expressions should only contain methods and
1247 // method templates.
1248 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1249
1250 if (isa<FunctionTemplateDecl>(decl))
1251 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1252 if (cast<CXXMethodDecl>(decl)->isStatic())
1253 return false;
1254 } while (++begin != end);
1255
1256 return true;
1257}
1258
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001259UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001260 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001261 Expr *Base, QualType BaseType,
1262 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001263 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001265 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001266 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001267 const TemplateArgumentListInfo *TemplateArgs,
1268 UnresolvedSetIterator Begin,
1269 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001270 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1271 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001272 // Dependent
1273 ((Base && Base->isTypeDependent()) ||
1274 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001275 ((Base && Base->isInstantiationDependent()) ||
1276 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001277 // Contains unexpanded parameter pack
1278 ((Base && Base->containsUnexpandedParameterPack()) ||
1279 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001280 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1281 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001282
1283 // Check whether all of the members are non-static member functions,
1284 // and if so, mark give this bound-member type instead of overload type.
1285 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1286 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001287}
1288
Douglas Gregor4c9be892011-02-28 20:01:57 +00001289bool UnresolvedMemberExpr::isImplicitAccess() const {
1290 if (Base == 0)
1291 return true;
1292
Douglas Gregor75e85042011-03-02 21:06:53 +00001293 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001294}
1295
John McCall129e2df2009-11-30 22:42:35 +00001296UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001297UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001298 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001299 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001300 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001301 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001302 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001303 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001304 const TemplateArgumentListInfo *TemplateArgs,
1305 UnresolvedSetIterator Begin,
1306 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001307 std::size_t size = sizeof(UnresolvedMemberExpr);
1308 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001309 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1310 else if (TemplateKWLoc.isValid())
1311 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001312
Chris Lattner32488542010-10-30 05:14:06 +00001313 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001314 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001315 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001316 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001317 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001318}
1319
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001320UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001321UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001322 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001323 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001324 if (HasTemplateKWAndArgsInfo)
1325 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001326
Chris Lattner32488542010-10-30 05:14:06 +00001327 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001328 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001329 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001330 return E;
1331}
1332
John McCallc373d482010-01-27 01:50:18 +00001333CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1334 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1335
1336 // If there was a nested name specifier, it names the naming class.
1337 // It can't be dependent: after all, we were actually able to do the
1338 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001339 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001340 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001341 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001342 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001343 Record = T->getAsCXXRecordDecl();
1344 assert(Record && "qualifier in member expression does not name record");
1345 }
John McCallc373d482010-01-27 01:50:18 +00001346 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001347 else {
John McCallc373d482010-01-27 01:50:18 +00001348 QualType BaseType = getBaseType().getNonReferenceType();
1349 if (isArrow()) {
1350 const PointerType *PT = BaseType->getAs<PointerType>();
1351 assert(PT && "base of arrow member access is not pointer");
1352 BaseType = PT->getPointeeType();
1353 }
1354
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001355 Record = BaseType->getAsCXXRecordDecl();
1356 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001357 }
1358
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001359 return Record;
John McCallc373d482010-01-27 01:50:18 +00001360}
1361
Douglas Gregorc7793c72011-01-15 01:15:58 +00001362SubstNonTypeTemplateParmPackExpr::
1363SubstNonTypeTemplateParmPackExpr(QualType T,
1364 NonTypeTemplateParmDecl *Param,
1365 SourceLocation NameLoc,
1366 const TemplateArgument &ArgPack)
1367 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001368 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001369 Param(Param), Arguments(ArgPack.pack_begin()),
1370 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1371
1372TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1373 return TemplateArgument(Arguments, NumArguments);
1374}
1375
Richard Smith9a4db032012-09-12 00:56:43 +00001376FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1377 SourceLocation NameLoc,
1378 unsigned NumParams,
1379 Decl * const *Params)
1380 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1381 true, true, true, true),
1382 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1383 if (Params)
1384 std::uninitialized_copy(Params, Params + NumParams,
1385 reinterpret_cast<Decl**>(this+1));
1386}
1387
1388FunctionParmPackExpr *
1389FunctionParmPackExpr::Create(ASTContext &Context, QualType T,
1390 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001391 ArrayRef<Decl *> Params) {
Richard Smith9a4db032012-09-12 00:56:43 +00001392 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1393 sizeof(ParmVarDecl*) * Params.size()))
1394 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1395}
1396
1397FunctionParmPackExpr *
1398FunctionParmPackExpr::CreateEmpty(ASTContext &Context, unsigned NumParams) {
1399 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1400 sizeof(ParmVarDecl*) * NumParams))
1401 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1402}
1403
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001404TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1405 ArrayRef<TypeSourceInfo *> Args,
1406 SourceLocation RParenLoc,
1407 bool Value)
1408 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1409 /*TypeDependent=*/false,
1410 /*ValueDependent=*/false,
1411 /*InstantiationDependent=*/false,
1412 /*ContainsUnexpandedParameterPack=*/false),
1413 Loc(Loc), RParenLoc(RParenLoc)
1414{
1415 TypeTraitExprBits.Kind = Kind;
1416 TypeTraitExprBits.Value = Value;
1417 TypeTraitExprBits.NumArgs = Args.size();
1418
1419 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1420
1421 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1422 if (Args[I]->getType()->isDependentType())
1423 setValueDependent(true);
1424 if (Args[I]->getType()->isInstantiationDependentType())
1425 setInstantiationDependent(true);
1426 if (Args[I]->getType()->containsUnexpandedParameterPack())
1427 setContainsUnexpandedParameterPack(true);
1428
1429 ToArgs[I] = Args[I];
1430 }
1431}
1432
1433TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1434 SourceLocation Loc,
1435 TypeTrait Kind,
1436 ArrayRef<TypeSourceInfo *> Args,
1437 SourceLocation RParenLoc,
1438 bool Value) {
1439 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1440 void *Mem = C.Allocate(Size);
1441 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1442}
1443
1444TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1445 unsigned NumArgs) {
1446 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1447 void *Mem = C.Allocate(Size);
1448 return new (Mem) TypeTraitExpr(EmptyShell());
1449}
1450
David Blaikie99ba9e32011-12-20 02:48:34 +00001451void ArrayTypeTraitExpr::anchor() { }