blob: 28794d4f62156de5c852102a6d3e380ef0de0ffe [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,
Eli Friedmancdd4b782013-08-15 22:02:56 +0000663 TypeSourceInfo *Written, CastKind K, Expr *Op,
664 const CXXCastPath *BasePath,
665 SourceLocation L, SourceLocation R) {
John McCallf871d0c2010-08-07 06:22:56 +0000666 unsigned PathSize = (BasePath ? BasePath->size() : 0);
667 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
668 + PathSize * sizeof(CXXBaseSpecifier*));
669 CXXFunctionalCastExpr *E =
Eli Friedmancdd4b782013-08-15 22:02:56 +0000670 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, 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
Eli Friedmancdd4b782013-08-15 22:02:56 +0000682SourceLocation CXXFunctionalCastExpr::getLocStart() const {
683 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
684}
685
686SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
687 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
688}
689
Richard Smith9fcce652012-03-07 08:35:16 +0000690UserDefinedLiteral::LiteralOperatorKind
691UserDefinedLiteral::getLiteralOperatorKind() const {
692 if (getNumArgs() == 0)
693 return LOK_Template;
694 if (getNumArgs() == 2)
695 return LOK_String;
696
697 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
698 QualType ParamTy =
699 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
700 if (ParamTy->isPointerType())
701 return LOK_Raw;
702 if (ParamTy->isAnyCharacterType())
703 return LOK_Character;
704 if (ParamTy->isIntegerType())
705 return LOK_Integer;
706 if (ParamTy->isFloatingType())
707 return LOK_Floating;
708
709 llvm_unreachable("unknown kind of literal operator");
710}
711
712Expr *UserDefinedLiteral::getCookedLiteral() {
713#ifndef NDEBUG
714 LiteralOperatorKind LOK = getLiteralOperatorKind();
715 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
716#endif
717 return getArg(0);
718}
719
720const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
721 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
722}
John McCallf871d0c2010-08-07 06:22:56 +0000723
Douglas Gregor65222e82009-12-23 18:19:08 +0000724CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000725CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
726 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000727 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000728 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
729 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000730}
731
Richard Smithc3bf52c2013-04-20 22:23:05 +0000732CXXDefaultInitExpr::CXXDefaultInitExpr(ASTContext &C, SourceLocation Loc,
733 FieldDecl *Field, QualType T)
734 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
735 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
736 ? VK_XValue
737 : VK_RValue,
738 /*FIXME*/ OK_Ordinary, false, false, false, false),
739 Field(Field), Loc(Loc) {
740 assert(Field->hasInClassInitializer());
741}
742
Mike Stump1eb44332009-09-09 15:08:12 +0000743CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000744 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000745 return new (C) CXXTemporary(Destructor);
746}
747
Mike Stump1eb44332009-09-09 15:08:12 +0000748CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000749 CXXTemporary *Temp,
750 Expr* SubExpr) {
Peter Collingbournebceb7552011-11-27 22:09:28 +0000751 assert((SubExpr->getType()->isRecordType() ||
752 SubExpr->getType()->isArrayType()) &&
753 "Expression bound to a temporary must have record or array type!");
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000754
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000755 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000756}
757
Anders Carlsson8e587a12009-05-30 20:56:46 +0000758CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000759 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000760 TypeSourceInfo *Type,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000761 ArrayRef<Expr*> Args,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000762 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000763 bool HadMultipleCandidates,
Richard Smithc83c2302012-12-19 01:39:02 +0000764 bool ListInitialization,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000765 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000766 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
767 Type->getType().getNonReferenceType(),
768 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000769 Cons, false, Args,
Richard Smithc83c2302012-12-19 01:39:02 +0000770 HadMultipleCandidates,
771 ListInitialization, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000772 CXXConstructExpr::CK_Complete, parenRange),
773 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000774}
775
Erik Verbruggen65d78312012-12-25 14:51:39 +0000776SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
777 return Type->getTypeLoc().getBeginLoc();
778}
779
780SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
781 return getParenRange().getEnd();
Douglas Gregor506ae412009-01-16 18:33:17 +0000782}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000783
Mike Stump1eb44332009-09-09 15:08:12 +0000784CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000785 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000786 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000787 ArrayRef<Expr*> Args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000788 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000789 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000790 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000791 ConstructionKind ConstructKind,
792 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000793 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000794 Elidable, Args,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000795 HadMultipleCandidates, ListInitialization,
796 ZeroInitialization, ConstructKind,
797 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000798}
799
Mike Stump1eb44332009-09-09 15:08:12 +0000800CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000801 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000802 CXXConstructorDecl *D, bool elidable,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000803 ArrayRef<Expr*> args,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000804 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000805 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000806 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000807 ConstructionKind ConstructKind,
808 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000809 : Expr(SC, T, VK_RValue, OK_Ordinary,
810 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000811 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000812 T->containsUnexpandedParameterPack()),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000813 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(args.size()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000814 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000815 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000816 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000817 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000818{
819 if (NumArgs) {
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000820 Args = new (C) Stmt*[args.size()];
Douglas Gregor16006c92009-12-16 18:50:27 +0000821
Benjamin Kramer3b6bef92012-08-24 11:54:20 +0000822 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor16006c92009-12-16 18:50:27 +0000823 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000824
825 if (args[i]->isValueDependent())
826 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000827 if (args[i]->isInstantiationDependent())
828 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000829 if (args[i]->containsUnexpandedParameterPack())
830 ExprBits.ContainsUnexpandedParameterPack = true;
831
Douglas Gregor16006c92009-12-16 18:50:27 +0000832 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000833 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000834 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000835}
836
Douglas Gregor01d08012012-02-07 10:09:13 +0000837LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
838 LambdaCaptureKind Kind, VarDecl *Var,
839 SourceLocation EllipsisLoc)
Richard Smith0d8e9642013-05-16 06:20:58 +0000840 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregor01d08012012-02-07 10:09:13 +0000841{
842 unsigned Bits = 0;
843 if (Implicit)
844 Bits |= Capture_Implicit;
845
846 switch (Kind) {
847 case LCK_This:
848 assert(Var == 0 && "'this' capture cannot have a variable!");
849 break;
850
851 case LCK_ByCopy:
852 Bits |= Capture_ByCopy;
853 // Fall through
854 case LCK_ByRef:
855 assert(Var && "capture must have a variable!");
856 break;
Richard Smith0d8e9642013-05-16 06:20:58 +0000857
858 case LCK_Init:
859 llvm_unreachable("don't use this constructor for an init-capture");
Douglas Gregor01d08012012-02-07 10:09:13 +0000860 }
Richard Smith0d8e9642013-05-16 06:20:58 +0000861 DeclAndBits.setInt(Bits);
Douglas Gregor01d08012012-02-07 10:09:13 +0000862}
863
Richard Smith0d8e9642013-05-16 06:20:58 +0000864LambdaExpr::Capture::Capture(FieldDecl *Field)
865 : DeclAndBits(Field,
866 Field->getType()->isReferenceType() ? 0 : Capture_ByCopy),
867 Loc(Field->getLocation()), EllipsisLoc() {}
868
Douglas Gregor01d08012012-02-07 10:09:13 +0000869LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
Richard Smith0d8e9642013-05-16 06:20:58 +0000870 Decl *D = DeclAndBits.getPointer();
871 if (!D)
Douglas Gregor01d08012012-02-07 10:09:13 +0000872 return LCK_This;
873
Richard Smith0d8e9642013-05-16 06:20:58 +0000874 if (isa<FieldDecl>(D))
875 return LCK_Init;
876
877 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregor01d08012012-02-07 10:09:13 +0000878}
879
James Dennettf68af642013-08-09 23:08:25 +0000880LambdaExpr::LambdaExpr(QualType T,
Douglas Gregor01d08012012-02-07 10:09:13 +0000881 SourceRange IntroducerRange,
882 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000883 SourceLocation CaptureDefaultLoc,
884 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000885 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000886 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000887 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000888 ArrayRef<VarDecl *> ArrayIndexVars,
889 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000890 SourceLocation ClosingBrace,
891 bool ContainsUnexpandedParameterPack)
Douglas Gregor01d08012012-02-07 10:09:13 +0000892 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
893 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith612409e2012-07-25 03:56:55 +0000894 ContainsUnexpandedParameterPack),
Douglas Gregor01d08012012-02-07 10:09:13 +0000895 IntroducerRange(IntroducerRange),
James Dennettf68af642013-08-09 23:08:25 +0000896 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000897 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000898 CaptureDefault(CaptureDefault),
899 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000900 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000901 ClosingBrace(ClosingBrace)
902{
903 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000904 CXXRecordDecl *Class = getLambdaClass();
905 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000906
907 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000908
909 // Copy captures.
910 ASTContext &Context = Class->getASTContext();
911 Data.NumCaptures = NumCaptures;
912 Data.NumExplicitCaptures = 0;
913 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
914 Capture *ToCapture = Data.Captures;
915 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
916 if (Captures[I].isExplicit())
917 ++Data.NumExplicitCaptures;
918
919 *ToCapture++ = Captures[I];
920 }
921
922 // Copy initialization expressions for the non-static data members.
923 Stmt **Stored = getStoredStmts();
924 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
925 *Stored++ = CaptureInits[I];
926
927 // Copy the body of the lambda.
928 *Stored++ = getCallOperator()->getBody();
929
930 // Copy the array index variables, if any.
931 HasArrayIndexVars = !ArrayIndexVars.empty();
932 if (HasArrayIndexVars) {
933 assert(ArrayIndexStarts.size() == NumCaptures);
934 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
935 sizeof(VarDecl *) * ArrayIndexVars.size());
936 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
937 sizeof(unsigned) * Captures.size());
938 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000939 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000940}
941
James Dennettf68af642013-08-09 23:08:25 +0000942LambdaExpr *LambdaExpr::Create(ASTContext &Context,
Douglas Gregor01d08012012-02-07 10:09:13 +0000943 CXXRecordDecl *Class,
944 SourceRange IntroducerRange,
945 LambdaCaptureDefault CaptureDefault,
James Dennettf68af642013-08-09 23:08:25 +0000946 SourceLocation CaptureDefaultLoc,
947 ArrayRef<Capture> Captures,
Douglas Gregor01d08012012-02-07 10:09:13 +0000948 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000949 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000950 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000951 ArrayRef<VarDecl *> ArrayIndexVars,
952 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000953 SourceLocation ClosingBrace,
954 bool ContainsUnexpandedParameterPack) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000955 // Determine the type of the expression (i.e., the type of the
956 // function object we're creating).
957 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000958
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000959 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smith88d2f672012-08-21 05:42:49 +0000960 if (!ArrayIndexVars.empty()) {
961 Size += sizeof(unsigned) * (Captures.size() + 1);
962 // Realign for following VarDecl array.
Richard Smitha796b6c2012-08-21 18:18:06 +0000963 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smith88d2f672012-08-21 05:42:49 +0000964 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
965 }
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000966 void *Mem = Context.Allocate(Size);
James Dennettf68af642013-08-09 23:08:25 +0000967 return new (Mem) LambdaExpr(T, IntroducerRange,
968 CaptureDefault, CaptureDefaultLoc, Captures,
969 ExplicitParams, ExplicitResultType,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000970 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith612409e2012-07-25 03:56:55 +0000971 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregor01d08012012-02-07 10:09:13 +0000972}
973
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000974LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
975 unsigned NumArrayIndexVars) {
976 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
977 if (NumArrayIndexVars)
978 Size += sizeof(VarDecl) * NumArrayIndexVars
979 + sizeof(unsigned) * (NumCaptures + 1);
980 void *Mem = C.Allocate(Size);
981 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
982}
983
Douglas Gregorda8962a2012-02-13 15:44:47 +0000984LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000985 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000986}
987
988LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000989 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000990}
991
992LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
993 return capture_begin();
994}
995
996LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
997 struct CXXRecordDecl::LambdaDefinitionData &Data
998 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000999 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +00001000}
1001
1002LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1003 return explicit_capture_end();
1004}
1005
1006LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1007 return capture_end();
1008}
1009
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001010ArrayRef<VarDecl *>
1011LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001012 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001013
1014 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +00001015 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1016 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +00001017 VarDecl **IndexVars = getArrayIndexVars();
1018 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +00001019 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1020 IndexVars + IndexStarts[Index + 1]);
1021}
1022
Douglas Gregor01d08012012-02-07 10:09:13 +00001023CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1024 return getType()->getAsCXXRecordDecl();
1025}
1026
1027CXXMethodDecl *LambdaExpr::getCallOperator() const {
1028 CXXRecordDecl *Record = getLambdaClass();
Faisal Valiecb58192013-08-22 01:49:11 +00001029 return Record->getLambdaCallOperator();
1030}
1031
1032TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1033 CXXRecordDecl *Record = getLambdaClass();
1034 return Record->getGenericLambdaTemplateParameterList();
1035
Douglas Gregor01d08012012-02-07 10:09:13 +00001036}
1037
Douglas Gregor9d36f5d2012-02-14 17:54:36 +00001038CompoundStmt *LambdaExpr::getBody() const {
1039 if (!getStoredStmts()[NumCaptures])
1040 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1041
1042 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1043}
1044
Douglas Gregor01d08012012-02-07 10:09:13 +00001045bool LambdaExpr::isMutable() const {
David Blaikie4ef832f2012-08-10 00:55:35 +00001046 return !getCallOperator()->isConst();
Douglas Gregor01d08012012-02-07 10:09:13 +00001047}
1048
John McCall80ee6e82011-11-10 05:35:25 +00001049ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1050 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +00001051 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +00001052 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001053 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001054 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +00001056 SubExpr(subexpr) {
1057 ExprWithCleanupsBits.NumObjects = objects.size();
1058 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1059 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +00001060}
1061
John McCall80ee6e82011-11-10 05:35:25 +00001062ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
1063 ArrayRef<CleanupObject> objects) {
1064 size_t size = sizeof(ExprWithCleanups)
1065 + objects.size() * sizeof(CleanupObject);
1066 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1067 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +00001068}
1069
John McCall80ee6e82011-11-10 05:35:25 +00001070ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1071 : Expr(ExprWithCleanupsClass, empty) {
1072 ExprWithCleanupsBits.NumObjects = numObjects;
1073}
Chris Lattnerd2598362010-05-10 00:25:06 +00001074
John McCall80ee6e82011-11-10 05:35:25 +00001075ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
1076 unsigned numObjects) {
1077 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1078 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1079 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +00001080}
1081
Douglas Gregorab6677e2010-09-08 00:15:04 +00001082CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001083 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001084 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001085 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +00001086 : Expr(CXXUnresolvedConstructExprClass,
1087 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +00001088 (Type->getType()->isLValueReferenceType() ? VK_LValue
1089 :Type->getType()->isRValueReferenceType()? VK_XValue
1090 :VK_RValue),
1091 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001092 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001093 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +00001094 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001095 LParenLoc(LParenLoc),
1096 RParenLoc(RParenLoc),
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001097 NumArgs(Args.size()) {
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001098 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001099 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001100 if (Args[I]->containsUnexpandedParameterPack())
1101 ExprBits.ContainsUnexpandedParameterPack = true;
1102
1103 StoredArgs[I] = Args[I];
1104 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001105}
1106
1107CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001108CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +00001109 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001110 SourceLocation LParenLoc,
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001111 ArrayRef<Expr*> Args,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001112 SourceLocation RParenLoc) {
1113 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramer3b6bef92012-08-24 11:54:20 +00001114 sizeof(Expr *) * Args.size());
1115 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001116}
1117
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001118CXXUnresolvedConstructExpr *
1119CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1120 Stmt::EmptyShell Empty;
1121 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1122 sizeof(Expr *) * NumArgs);
1123 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1124}
1125
Erik Verbruggen65d78312012-12-25 14:51:39 +00001126SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1127 return Type->getTypeLoc().getBeginLoc();
Douglas Gregorab6677e2010-09-08 00:15:04 +00001128}
1129
John McCall865d4472009-11-19 22:55:06 +00001130CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001131 Expr *Base, QualType BaseType,
1132 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001133 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001134 NestedNameSpecifierLoc QualifierLoc,
1135 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001136 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001137 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001138 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001139 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001140 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001141 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001142 (QualifierLoc &&
1143 QualifierLoc.getNestedNameSpecifier()
1144 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001145 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001146 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001147 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001148 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001149 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001150 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001151 if (TemplateArgs) {
1152 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001153 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001154 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001155 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1156 Dependent,
1157 InstantiationDependent,
1158 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001159 if (ContainsUnexpandedParameterPack)
1160 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001161 } else if (TemplateKWLoc.isValid()) {
1162 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001163 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001164}
1165
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001166CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1167 Expr *Base, QualType BaseType,
1168 bool IsArrow,
1169 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001170 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001171 NamedDecl *FirstQualifierFoundInScope,
1172 DeclarationNameInfo MemberNameInfo)
1173 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001174 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001175 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001176 (QualifierLoc &&
1177 QualifierLoc.getNestedNameSpecifier()->
1178 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001179 MemberNameInfo.containsUnexpandedParameterPack())),
1180 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001181 HasTemplateKWAndArgsInfo(false),
1182 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001183 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1184 MemberNameInfo(MemberNameInfo) { }
1185
John McCall865d4472009-11-19 22:55:06 +00001186CXXDependentScopeMemberExpr *
1187CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001188 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001189 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001190 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001191 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001192 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001193 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001194 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001195 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001196 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1197 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001198 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001199 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001200 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001202 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1203 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1204 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001205
Chris Lattner32488542010-10-30 05:14:06 +00001206 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001207 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1208 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001209 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001210 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001211 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001212 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001213}
1214
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001215CXXDependentScopeMemberExpr *
1216CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001217 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001218 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001219 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001220 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001221 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001222 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001223 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001224
1225 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001226 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001227 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001228 CXXDependentScopeMemberExpr *E
1229 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001230 0, SourceLocation(),
1231 NestedNameSpecifierLoc(),
1232 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001233 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001234 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001235 return E;
1236}
1237
Douglas Gregor4c9be892011-02-28 20:01:57 +00001238bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1239 if (Base == 0)
1240 return true;
1241
Douglas Gregor75e85042011-03-02 21:06:53 +00001242 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001243}
1244
John McCall864c0412011-04-26 20:42:42 +00001245static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1246 UnresolvedSetIterator end) {
1247 do {
1248 NamedDecl *decl = *begin;
1249 if (isa<UnresolvedUsingValueDecl>(decl))
1250 return false;
1251 if (isa<UsingShadowDecl>(decl))
1252 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1253
1254 // Unresolved member expressions should only contain methods and
1255 // method templates.
1256 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1257
1258 if (isa<FunctionTemplateDecl>(decl))
1259 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1260 if (cast<CXXMethodDecl>(decl)->isStatic())
1261 return false;
1262 } while (++begin != end);
1263
1264 return true;
1265}
1266
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001267UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001268 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001269 Expr *Base, QualType BaseType,
1270 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001271 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001272 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001273 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001274 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001275 const TemplateArgumentListInfo *TemplateArgs,
1276 UnresolvedSetIterator Begin,
1277 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001278 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1279 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001280 // Dependent
1281 ((Base && Base->isTypeDependent()) ||
1282 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001283 ((Base && Base->isInstantiationDependent()) ||
1284 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001285 // Contains unexpanded parameter pack
1286 ((Base && Base->containsUnexpandedParameterPack()) ||
1287 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001288 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1289 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001290
1291 // Check whether all of the members are non-static member functions,
1292 // and if so, mark give this bound-member type instead of overload type.
1293 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1294 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001295}
1296
Douglas Gregor4c9be892011-02-28 20:01:57 +00001297bool UnresolvedMemberExpr::isImplicitAccess() const {
1298 if (Base == 0)
1299 return true;
1300
Douglas Gregor75e85042011-03-02 21:06:53 +00001301 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001302}
1303
John McCall129e2df2009-11-30 22:42:35 +00001304UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001305UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001306 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001307 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001308 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001309 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001310 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001311 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001312 const TemplateArgumentListInfo *TemplateArgs,
1313 UnresolvedSetIterator Begin,
1314 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001315 std::size_t size = sizeof(UnresolvedMemberExpr);
1316 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001317 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1318 else if (TemplateKWLoc.isValid())
1319 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001320
Chris Lattner32488542010-10-30 05:14:06 +00001321 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001322 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001323 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001324 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001325 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001326}
1327
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001328UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001329UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001330 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001331 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001332 if (HasTemplateKWAndArgsInfo)
1333 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001334
Chris Lattner32488542010-10-30 05:14:06 +00001335 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001336 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001337 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001338 return E;
1339}
1340
John McCallc373d482010-01-27 01:50:18 +00001341CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1342 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1343
1344 // If there was a nested name specifier, it names the naming class.
1345 // It can't be dependent: after all, we were actually able to do the
1346 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001347 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001348 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001349 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001350 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001351 Record = T->getAsCXXRecordDecl();
1352 assert(Record && "qualifier in member expression does not name record");
1353 }
John McCallc373d482010-01-27 01:50:18 +00001354 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001355 else {
John McCallc373d482010-01-27 01:50:18 +00001356 QualType BaseType = getBaseType().getNonReferenceType();
1357 if (isArrow()) {
1358 const PointerType *PT = BaseType->getAs<PointerType>();
1359 assert(PT && "base of arrow member access is not pointer");
1360 BaseType = PT->getPointeeType();
1361 }
1362
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001363 Record = BaseType->getAsCXXRecordDecl();
1364 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001365 }
1366
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001367 return Record;
John McCallc373d482010-01-27 01:50:18 +00001368}
1369
Douglas Gregorc7793c72011-01-15 01:15:58 +00001370SubstNonTypeTemplateParmPackExpr::
1371SubstNonTypeTemplateParmPackExpr(QualType T,
1372 NonTypeTemplateParmDecl *Param,
1373 SourceLocation NameLoc,
1374 const TemplateArgument &ArgPack)
1375 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001376 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001377 Param(Param), Arguments(ArgPack.pack_begin()),
1378 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1379
1380TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1381 return TemplateArgument(Arguments, NumArguments);
1382}
1383
Richard Smith9a4db032012-09-12 00:56:43 +00001384FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1385 SourceLocation NameLoc,
1386 unsigned NumParams,
1387 Decl * const *Params)
1388 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1389 true, true, true, true),
1390 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1391 if (Params)
1392 std::uninitialized_copy(Params, Params + NumParams,
1393 reinterpret_cast<Decl**>(this+1));
1394}
1395
1396FunctionParmPackExpr *
1397FunctionParmPackExpr::Create(ASTContext &Context, QualType T,
1398 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001399 ArrayRef<Decl *> Params) {
Richard Smith9a4db032012-09-12 00:56:43 +00001400 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1401 sizeof(ParmVarDecl*) * Params.size()))
1402 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1403}
1404
1405FunctionParmPackExpr *
1406FunctionParmPackExpr::CreateEmpty(ASTContext &Context, unsigned NumParams) {
1407 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1408 sizeof(ParmVarDecl*) * NumParams))
1409 FunctionParmPackExpr(QualType(), 0, SourceLocation(), 0, 0);
1410}
1411
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001412TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1413 ArrayRef<TypeSourceInfo *> Args,
1414 SourceLocation RParenLoc,
1415 bool Value)
1416 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1417 /*TypeDependent=*/false,
1418 /*ValueDependent=*/false,
1419 /*InstantiationDependent=*/false,
1420 /*ContainsUnexpandedParameterPack=*/false),
1421 Loc(Loc), RParenLoc(RParenLoc)
1422{
1423 TypeTraitExprBits.Kind = Kind;
1424 TypeTraitExprBits.Value = Value;
1425 TypeTraitExprBits.NumArgs = Args.size();
1426
1427 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1428
1429 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1430 if (Args[I]->getType()->isDependentType())
1431 setValueDependent(true);
1432 if (Args[I]->getType()->isInstantiationDependentType())
1433 setInstantiationDependent(true);
1434 if (Args[I]->getType()->containsUnexpandedParameterPack())
1435 setContainsUnexpandedParameterPack(true);
1436
1437 ToArgs[I] = Args[I];
1438 }
1439}
1440
1441TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1442 SourceLocation Loc,
1443 TypeTrait Kind,
1444 ArrayRef<TypeSourceInfo *> Args,
1445 SourceLocation RParenLoc,
1446 bool Value) {
1447 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1448 void *Mem = C.Allocate(Size);
1449 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1450}
1451
1452TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1453 unsigned NumArgs) {
1454 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1455 void *Mem = C.Allocate(Size);
1456 return new (Mem) TypeTraitExpr(EmptyShell());
1457}
1458
David Blaikie99ba9e32011-12-20 02:48:34 +00001459void ArrayTypeTraitExpr::anchor() { }