blob: 73347b2e01d68a7db1422d7732fa51be51037c10 [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
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
Benjamin Kramer471c8b42012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.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"
Ted Kremeneka758d092007-08-24 20:21:10 +000020using namespace clang;
21
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000022
Ted Kremeneka758d092007-08-24 20:21:10 +000023//===----------------------------------------------------------------------===//
24// Child Iterators for iterating over subexpressions/substatements
25//===----------------------------------------------------------------------===//
26
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000027QualType CXXTypeidExpr::getTypeOperand() const {
28 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
29 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
30 .getUnqualifiedType();
31}
32
Francois Pichet01b7c302010-09-08 12:20:18 +000033QualType CXXUuidofExpr::getTypeOperand() const {
34 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
35 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
36 .getUnqualifiedType();
37}
38
Douglas Gregored8abf12010-07-08 06:14:04 +000039// CXXScalarValueInitExpr
Douglas Gregorab6677e2010-09-08 00:15:04 +000040SourceRange CXXScalarValueInitExpr::getSourceRange() const {
41 SourceLocation Start = RParenLoc;
42 if (TypeInfo)
43 Start = TypeInfo->getTypeLoc().getBeginLoc();
44 return SourceRange(Start, RParenLoc);
45}
46
Sebastian Redl4c5d3202008-11-21 19:14:01 +000047// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000048CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl1548d142012-02-16 11:35:52 +000049 FunctionDecl *operatorDelete,
Sebastian Redl2aed8b82012-02-16 12:22:20 +000050 bool usualArrayDeleteWantsSize,
51 Expr **placementArgs, unsigned numPlaceArgs,
52 SourceRange typeIdParens, Expr *arraySize,
53 InitializationStyle initializationStyle,
54 Expr *initializer, QualType ty,
55 TypeSourceInfo *allocatedTypeInfo,
56 SourceLocation startLoc, SourceRange directInitRange)
John McCallf89e55a2010-11-18 06:31:45 +000057 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000058 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +000059 ty->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000060 ty->containsUnexpandedParameterPack()),
Sebastian Redl2aed8b82012-02-16 12:22:20 +000061 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
62 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerd162cf12012-02-26 20:37:14 +000063 StartLoc(startLoc), DirectInitRange(directInitRange),
64 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl2aed8b82012-02-16 12:22:20 +000065 assert((initializer != 0 || initializationStyle == NoInit) &&
66 "Only NoInit can have no initializer.");
67 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
68 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redl4c5d3202008-11-21 19:14:01 +000069 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000070 if (Array) {
Douglas Gregor561f8122011-07-01 01:22:09 +000071 if (arraySize->isInstantiationDependent())
72 ExprBits.InstantiationDependent = true;
73
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000074 if (arraySize->containsUnexpandedParameterPack())
75 ExprBits.ContainsUnexpandedParameterPack = true;
76
Sebastian Redlcee63fb2008-12-02 14:43:59 +000077 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000078 }
79
Sebastian Redl2aed8b82012-02-16 12:22:20 +000080 if (initializer) {
81 if (initializer->isInstantiationDependent())
82 ExprBits.InstantiationDependent = true;
83
84 if (initializer->containsUnexpandedParameterPack())
85 ExprBits.ContainsUnexpandedParameterPack = true;
86
87 SubExprs[i++] = initializer;
88 }
89
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000090 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor561f8122011-07-01 01:22:09 +000091 if (placementArgs[j]->isInstantiationDependent())
92 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000093 if (placementArgs[j]->containsUnexpandedParameterPack())
94 ExprBits.ContainsUnexpandedParameterPack = true;
95
Sebastian Redl4c5d3202008-11-21 19:14:01 +000096 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000097 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +000098}
99
Chris Lattner59218632010-05-10 01:22:27 +0000100void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000101 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattner59218632010-05-10 01:22:27 +0000102 assert(SubExprs == 0 && "SubExprs already allocated");
103 Array = isArray;
104 NumPlacementArgs = numPlaceArgs;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000105
106 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattner59218632010-05-10 01:22:27 +0000107 SubExprs = new (C) Stmt*[TotalSize];
108}
109
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000110bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000111 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000112 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000113}
Chris Lattner59218632010-05-10 01:22:27 +0000114
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000115SourceLocation CXXNewExpr::getEndLoc() const {
116 switch (getInitializationStyle()) {
117 case NoInit:
118 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
119 case CallInit:
120 return DirectInitRange.getEnd();
121 case ListInit:
122 return getInitializer()->getSourceRange().getEnd();
123 }
Matt Beaumont-Gayebc6af12012-02-16 22:15:50 +0000124 llvm_unreachable("bogus initialization style");
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000125}
126
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000127// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000128QualType CXXDeleteExpr::getDestroyedType() const {
129 const Expr *Arg = getArgument();
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000130 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000131 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000132
133 if (ArgType->isDependentType() && !ArgType->isPointerType())
134 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000135
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000136 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000137}
138
Douglas Gregora71d8192009-09-04 17:36:40 +0000139// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000140PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
141 : Type(Info)
142{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000143 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000144}
145
John McCalle23cf432010-12-14 08:05:40 +0000146CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000147 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
148 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
149 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
150 PseudoDestructorTypeStorage DestroyedType)
John McCalle23cf432010-12-14 08:05:40 +0000151 : Expr(CXXPseudoDestructorExprClass,
152 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
153 FunctionProtoType::ExtProtoInfo())),
154 VK_RValue, OK_Ordinary,
155 /*isTypeDependent=*/(Base->isTypeDependent() ||
156 (DestroyedType.getTypeSourceInfo() &&
157 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000158 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000159 (Base->isInstantiationDependent() ||
160 (QualifierLoc &&
161 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
162 (ScopeType &&
163 ScopeType->getType()->isInstantiationDependentType()) ||
164 (DestroyedType.getTypeSourceInfo() &&
165 DestroyedType.getTypeSourceInfo()->getType()
166 ->isInstantiationDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000167 // ContainsUnexpandedParameterPack
168 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000169 (QualifierLoc &&
170 QualifierLoc.getNestedNameSpecifier()
171 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000172 (ScopeType &&
173 ScopeType->getType()->containsUnexpandedParameterPack()) ||
174 (DestroyedType.getTypeSourceInfo() &&
175 DestroyedType.getTypeSourceInfo()->getType()
176 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000177 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000178 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000179 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
180 DestroyedType(DestroyedType) { }
181
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000182QualType CXXPseudoDestructorExpr::getDestroyedType() const {
183 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
184 return TInfo->getType();
185
186 return QualType();
187}
188
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000189SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000190 SourceLocation End = DestroyedType.getLocation();
191 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000192 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000193 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000194}
195
John McCallba135432009-11-21 08:51:07 +0000196// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000197UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000198UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000199 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000200 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000201 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000202 const DeclarationNameInfo &NameInfo,
203 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000204 const TemplateArgumentListInfo *Args,
205 UnresolvedSetIterator Begin,
206 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000207{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000208 assert(Args || TemplateKWLoc.isValid());
209 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000210 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000211 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000212 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
213 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000214 ADL, /*Overload*/ true, Args,
Richard Smithad762fc2011-04-14 22:09:26 +0000215 Begin, End, /*StdIsAssociated=*/false);
John McCallf7a1a742009-11-24 19:00:30 +0000216}
217
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000218UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000219UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
220 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000221 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000222 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000223 if (HasTemplateKWAndArgsInfo)
224 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000225
Chris Lattner32488542010-10-30 05:14:06 +0000226 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000227 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000228 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000229 return E;
230}
231
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000232OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000233 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000234 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000235 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000236 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000237 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000238 UnresolvedSetIterator End,
239 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000240 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000241 bool KnownContainsUnexpandedParameterPack)
242 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
243 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 (KnownInstantiationDependent ||
245 NameInfo.isInstantiationDependent() ||
246 (QualifierLoc &&
247 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000248 (KnownContainsUnexpandedParameterPack ||
249 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000250 (QualifierLoc &&
251 QualifierLoc.getNestedNameSpecifier()
252 ->containsUnexpandedParameterPack()))),
Benjamin Kramerd162cf12012-02-26 20:37:14 +0000253 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
254 Results(0), NumResults(End - Begin),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000255 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000256{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000257 NumResults = End - Begin;
258 if (NumResults) {
259 // Determine whether this expression is type-dependent.
260 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
261 if ((*I)->getDeclContext()->isDependentContext() ||
262 isa<UnresolvedUsingValueDecl>(*I)) {
263 ExprBits.TypeDependent = true;
264 ExprBits.ValueDependent = true;
265 }
266 }
267
268 Results = static_cast<DeclAccessPair *>(
269 C.Allocate(sizeof(DeclAccessPair) * NumResults,
270 llvm::alignOf<DeclAccessPair>()));
271 memcpy(Results, &*Begin.getIterator(),
272 NumResults * sizeof(DeclAccessPair));
273 }
274
275 // If we have explicit template arguments, check for dependent
276 // template arguments and whether they contain any unexpanded pack
277 // expansions.
278 if (TemplateArgs) {
279 bool Dependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000280 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000281 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000282 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
283 Dependent,
284 InstantiationDependent,
285 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000286
287 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000288 ExprBits.TypeDependent = true;
289 ExprBits.ValueDependent = true;
290 }
291 if (InstantiationDependent)
292 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000293 if (ContainsUnexpandedParameterPack)
294 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000295 } else if (TemplateKWLoc.isValid()) {
296 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000297 }
298
299 if (isTypeDependent())
300 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000301}
302
303void OverloadExpr::initializeResults(ASTContext &C,
304 UnresolvedSetIterator Begin,
305 UnresolvedSetIterator End) {
306 assert(Results == 0 && "Results already initialized!");
307 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000308 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000309 Results = static_cast<DeclAccessPair *>(
310 C.Allocate(sizeof(DeclAccessPair) * NumResults,
311
312 llvm::alignOf<DeclAccessPair>()));
313 memcpy(Results, &*Begin.getIterator(),
314 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000315 }
316}
317
John McCalle9ee23e2010-04-22 18:44:12 +0000318CXXRecordDecl *OverloadExpr::getNamingClass() const {
319 if (isa<UnresolvedLookupExpr>(this))
320 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
321 else
322 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
323}
324
John McCall865d4472009-11-19 22:55:06 +0000325// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000326DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000327 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000328 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000329 const DeclarationNameInfo &NameInfo,
330 const TemplateArgumentListInfo *Args)
331 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
332 true, true,
Douglas Gregor561f8122011-07-01 01:22:09 +0000333 (NameInfo.isInstantiationDependent() ||
334 (QualifierLoc &&
335 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000336 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000337 (QualifierLoc &&
338 QualifierLoc.getNestedNameSpecifier()
339 ->containsUnexpandedParameterPack()))),
340 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000341 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000342{
343 if (Args) {
344 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000345 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000346 bool ContainsUnexpandedParameterPack
347 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000348 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
349 Dependent,
350 InstantiationDependent,
351 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000352 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000353 } else if (TemplateKWLoc.isValid()) {
354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000355 }
356}
357
John McCallf7a1a742009-11-24 19:00:30 +0000358DependentScopeDeclRefExpr *
359DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000360 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000361 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000362 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000363 const TemplateArgumentListInfo *Args) {
364 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000365 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000366 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
367 else if (TemplateKWLoc.isValid())
368 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000369 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000370 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
371 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000372}
373
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000374DependentScopeDeclRefExpr *
375DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000376 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000377 unsigned NumTemplateArgs) {
378 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000379 if (HasTemplateKWAndArgsInfo)
380 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000381 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000382 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000383 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000384 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000385 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000386 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000387 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000388}
389
Chandler Carruth428edaf2010-10-25 08:47:36 +0000390SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000391 if (isa<CXXTemporaryObjectExpr>(this))
392 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
393
Douglas Gregor40749ee2010-11-03 00:35:38 +0000394 if (ParenRange.isValid())
395 return SourceRange(Loc, ParenRange.getEnd());
396
397 SourceLocation End = Loc;
398 for (unsigned I = getNumArgs(); I > 0; --I) {
399 const Expr *Arg = getArg(I-1);
400 if (!Arg->isDefaultArgument()) {
401 SourceLocation NewEnd = Arg->getLocEnd();
402 if (NewEnd.isValid()) {
403 End = NewEnd;
404 break;
405 }
406 }
407 }
408
409 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000410}
411
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000412SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregorb4609802008-11-14 16:09:21 +0000413 OverloadedOperatorKind Kind = getOperator();
414 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
415 if (getNumArgs() == 1)
416 // Prefix operator
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000417 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000418 else
419 // Postfix operator
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000420 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000421 } else if (Kind == OO_Arrow) {
422 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000423 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000424 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000425 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000426 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregorb4609802008-11-14 16:09:21 +0000427 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000428 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000429 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis3539b0c2012-05-01 22:19:11 +0000430 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregorb4609802008-11-14 16:09:21 +0000431 } else {
Argyrios Kyrtzidis4548ca22012-04-30 22:12:22 +0000432 return getOperatorLoc();
Douglas Gregorb4609802008-11-14 16:09:21 +0000433 }
434}
435
Ted Kremenekb2771592011-03-30 17:41:19 +0000436Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
437 if (const MemberExpr *MemExpr =
438 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-12-22 05:46:06 +0000439 return MemExpr->getBase();
440
441 // FIXME: Will eventually need to cope with member pointers.
442 return 0;
443}
444
Ted Kremenekb2771592011-03-30 17:41:19 +0000445CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
446 if (const MemberExpr *MemExpr =
447 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
448 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
449
450 // FIXME: Will eventually need to cope with member pointers.
451 return 0;
452}
453
454
David Blaikie0cf3c0e2012-05-03 16:25:49 +0000455CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth007a9b12010-10-27 06:55:41 +0000456 Expr* ThisArg = getImplicitObjectArgument();
457 if (!ThisArg)
458 return 0;
459
460 if (ThisArg->getType()->isAnyPointerType())
461 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
462
463 return ThisArg->getType()->getAsCXXRecordDecl();
464}
465
Douglas Gregor00b98c22009-11-12 15:31:47 +0000466
Douglas Gregor49badde2008-10-27 19:41:14 +0000467//===----------------------------------------------------------------------===//
468// Named casts
469//===----------------------------------------------------------------------===//
470
471/// getCastName - Get the name of the C++ cast being used, e.g.,
472/// "static_cast", "dynamic_cast", "reinterpret_cast", or
473/// "const_cast". The returned pointer must not be freed.
474const char *CXXNamedCastExpr::getCastName() const {
475 switch (getStmtClass()) {
476 case CXXStaticCastExprClass: return "static_cast";
477 case CXXDynamicCastExprClass: return "dynamic_cast";
478 case CXXReinterpretCastExprClass: return "reinterpret_cast";
479 case CXXConstCastExprClass: return "const_cast";
480 default: return "<invalid cast>";
481 }
482}
Douglas Gregor506ae412009-01-16 18:33:17 +0000483
John McCallf871d0c2010-08-07 06:22:56 +0000484CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000485 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000486 CastKind K, Expr *Op,
487 const CXXCastPath *BasePath,
488 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000489 SourceLocation L,
490 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000491 unsigned PathSize = (BasePath ? BasePath->size() : 0);
492 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
493 + PathSize * sizeof(CXXBaseSpecifier*));
494 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000495 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
496 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000497 if (PathSize) E->setCastPath(*BasePath);
498 return E;
499}
500
501CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
502 unsigned PathSize) {
503 void *Buffer =
504 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
505 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
506}
507
508CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000509 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000510 CastKind K, Expr *Op,
511 const CXXCastPath *BasePath,
512 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000513 SourceLocation L,
514 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000515 unsigned PathSize = (BasePath ? BasePath->size() : 0);
516 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
517 + PathSize * sizeof(CXXBaseSpecifier*));
518 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000519 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
520 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000521 if (PathSize) E->setCastPath(*BasePath);
522 return E;
523}
524
525CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
526 unsigned PathSize) {
527 void *Buffer =
528 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
529 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
530}
531
Anders Carlsson0fee3302011-04-11 01:43:55 +0000532/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
533/// to always be null. For example:
534///
535/// struct A { };
536/// struct B final : A { };
537/// struct C { };
538///
539/// C *f(B* b) { return dynamic_cast<C*>(b); }
540bool CXXDynamicCastExpr::isAlwaysNull() const
541{
542 QualType SrcType = getSubExpr()->getType();
543 QualType DestType = getType();
544
545 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
546 SrcType = SrcPTy->getPointeeType();
547 DestType = DestType->castAs<PointerType>()->getPointeeType();
548 }
549
Sean Hunt5ca86392012-06-19 23:44:55 +0000550 if (DestType->isVoidType())
551 return false;
552
Anders Carlsson0fee3302011-04-11 01:43:55 +0000553 const CXXRecordDecl *SrcRD =
554 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
555
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000556 if (!SrcRD->hasAttr<FinalAttr>())
557 return false;
558
Anders Carlsson0fee3302011-04-11 01:43:55 +0000559 const CXXRecordDecl *DestRD =
560 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
561
562 return !DestRD->isDerivedFrom(SrcRD);
563}
564
John McCallf871d0c2010-08-07 06:22:56 +0000565CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000566CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
567 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000568 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000569 TypeSourceInfo *WrittenTy, SourceLocation L,
570 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000571 unsigned PathSize = (BasePath ? BasePath->size() : 0);
572 void *Buffer =
573 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
574 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000575 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
576 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000577 if (PathSize) E->setCastPath(*BasePath);
578 return E;
579}
580
581CXXReinterpretCastExpr *
582CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
583 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
584 + PathSize * sizeof(CXXBaseSpecifier*));
585 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
586}
587
John McCallf89e55a2010-11-18 06:31:45 +0000588CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
589 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000590 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000591 SourceLocation L,
592 SourceLocation RParenLoc) {
593 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000594}
595
596CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
597 return new (C) CXXConstCastExpr(EmptyShell());
598}
599
600CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000601CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000602 TypeSourceInfo *Written, SourceLocation L,
603 CastKind K, Expr *Op, const CXXCastPath *BasePath,
604 SourceLocation R) {
605 unsigned PathSize = (BasePath ? BasePath->size() : 0);
606 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
607 + PathSize * sizeof(CXXBaseSpecifier*));
608 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000609 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000610 if (PathSize) E->setCastPath(*BasePath);
611 return E;
612}
613
614CXXFunctionalCastExpr *
615CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
616 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
617 + PathSize * sizeof(CXXBaseSpecifier*));
618 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
619}
620
Richard Smith9fcce652012-03-07 08:35:16 +0000621UserDefinedLiteral::LiteralOperatorKind
622UserDefinedLiteral::getLiteralOperatorKind() const {
623 if (getNumArgs() == 0)
624 return LOK_Template;
625 if (getNumArgs() == 2)
626 return LOK_String;
627
628 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
629 QualType ParamTy =
630 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
631 if (ParamTy->isPointerType())
632 return LOK_Raw;
633 if (ParamTy->isAnyCharacterType())
634 return LOK_Character;
635 if (ParamTy->isIntegerType())
636 return LOK_Integer;
637 if (ParamTy->isFloatingType())
638 return LOK_Floating;
639
640 llvm_unreachable("unknown kind of literal operator");
641}
642
643Expr *UserDefinedLiteral::getCookedLiteral() {
644#ifndef NDEBUG
645 LiteralOperatorKind LOK = getLiteralOperatorKind();
646 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
647#endif
648 return getArg(0);
649}
650
651const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
652 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
653}
John McCallf871d0c2010-08-07 06:22:56 +0000654
Douglas Gregor65222e82009-12-23 18:19:08 +0000655CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000656CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
657 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000658 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000659 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
660 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000661}
662
Mike Stump1eb44332009-09-09 15:08:12 +0000663CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000664 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000665 return new (C) CXXTemporary(Destructor);
666}
667
Mike Stump1eb44332009-09-09 15:08:12 +0000668CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000669 CXXTemporary *Temp,
670 Expr* SubExpr) {
Peter Collingbournebceb7552011-11-27 22:09:28 +0000671 assert((SubExpr->getType()->isRecordType() ||
672 SubExpr->getType()->isArrayType()) &&
673 "Expression bound to a temporary must have record or array type!");
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000674
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000675 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000676}
677
Anders Carlsson8e587a12009-05-30 20:56:46 +0000678CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000679 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000680 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000681 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000682 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000683 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000684 bool HadMultipleCandidates,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000685 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000686 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
687 Type->getType().getNonReferenceType(),
688 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000689 Cons, false, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000690 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000691 CXXConstructExpr::CK_Complete, parenRange),
692 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000693}
694
695SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000696 return SourceRange(Type->getTypeLoc().getBeginLoc(),
697 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000698}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000699
Mike Stump1eb44332009-09-09 15:08:12 +0000700CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000701 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000702 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000703 Expr **Args, unsigned NumArgs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000704 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000705 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000706 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000707 ConstructionKind ConstructKind,
708 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000709 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000710 Elidable, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000711 HadMultipleCandidates, ListInitialization,
712 ZeroInitialization, ConstructKind,
713 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000714}
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000717 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000718 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000719 Expr **args, unsigned numargs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000720 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000721 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000722 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000723 ConstructionKind ConstructKind,
724 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000725 : Expr(SC, T, VK_RValue, OK_Ordinary,
726 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000727 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000728 T->containsUnexpandedParameterPack()),
Douglas Gregora48e6762011-09-26 14:47:03 +0000729 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000730 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000731 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000732 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000733 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000734{
735 if (NumArgs) {
736 Args = new (C) Stmt*[NumArgs];
737
738 for (unsigned i = 0; i != NumArgs; ++i) {
739 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000740
741 if (args[i]->isValueDependent())
742 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000743 if (args[i]->isInstantiationDependent())
744 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000745 if (args[i]->containsUnexpandedParameterPack())
746 ExprBits.ContainsUnexpandedParameterPack = true;
747
Douglas Gregor16006c92009-12-16 18:50:27 +0000748 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000749 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000750 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000751}
752
Douglas Gregor01d08012012-02-07 10:09:13 +0000753LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
754 LambdaCaptureKind Kind, VarDecl *Var,
755 SourceLocation EllipsisLoc)
756 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
757{
758 unsigned Bits = 0;
759 if (Implicit)
760 Bits |= Capture_Implicit;
761
762 switch (Kind) {
763 case LCK_This:
764 assert(Var == 0 && "'this' capture cannot have a variable!");
765 break;
766
767 case LCK_ByCopy:
768 Bits |= Capture_ByCopy;
769 // Fall through
770 case LCK_ByRef:
771 assert(Var && "capture must have a variable!");
772 break;
773 }
774 VarAndBits.setInt(Bits);
775}
776
777LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
778 if (capturesThis())
779 return LCK_This;
780
781 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
782}
783
784LambdaExpr::LambdaExpr(QualType T,
785 SourceRange IntroducerRange,
786 LambdaCaptureDefault CaptureDefault,
787 ArrayRef<Capture> Captures,
788 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000789 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000790 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000791 ArrayRef<VarDecl *> ArrayIndexVars,
792 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000793 SourceLocation ClosingBrace)
Douglas Gregor01d08012012-02-07 10:09:13 +0000794 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
795 T->isDependentType(), T->isDependentType(), T->isDependentType(),
796 /*ContainsUnexpandedParameterPack=*/false),
797 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000798 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000799 CaptureDefault(CaptureDefault),
800 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000801 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000802 ClosingBrace(ClosingBrace)
803{
804 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000805 CXXRecordDecl *Class = getLambdaClass();
806 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000807
808 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000809
810 // Copy captures.
811 ASTContext &Context = Class->getASTContext();
812 Data.NumCaptures = NumCaptures;
813 Data.NumExplicitCaptures = 0;
814 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
815 Capture *ToCapture = Data.Captures;
816 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
817 if (Captures[I].isExplicit())
818 ++Data.NumExplicitCaptures;
819
820 *ToCapture++ = Captures[I];
821 }
822
823 // Copy initialization expressions for the non-static data members.
824 Stmt **Stored = getStoredStmts();
825 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
826 *Stored++ = CaptureInits[I];
827
828 // Copy the body of the lambda.
829 *Stored++ = getCallOperator()->getBody();
830
831 // Copy the array index variables, if any.
832 HasArrayIndexVars = !ArrayIndexVars.empty();
833 if (HasArrayIndexVars) {
834 assert(ArrayIndexStarts.size() == NumCaptures);
835 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
836 sizeof(VarDecl *) * ArrayIndexVars.size());
837 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
838 sizeof(unsigned) * Captures.size());
839 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor5878cbc2012-02-21 04:17:39 +0000840 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000841}
842
843LambdaExpr *LambdaExpr::Create(ASTContext &Context,
844 CXXRecordDecl *Class,
845 SourceRange IntroducerRange,
846 LambdaCaptureDefault CaptureDefault,
847 ArrayRef<Capture> Captures,
848 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000849 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000850 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000851 ArrayRef<VarDecl *> ArrayIndexVars,
852 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000853 SourceLocation ClosingBrace) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000854 // Determine the type of the expression (i.e., the type of the
855 // function object we're creating).
856 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000857
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000858 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
859 if (!ArrayIndexVars.empty())
860 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
861 + sizeof(unsigned) * (Captures.size() + 1);
862 void *Mem = Context.Allocate(Size);
863 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000864 Captures, ExplicitParams, ExplicitResultType,
865 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregorf54486a2012-04-04 17:40:10 +0000866 ClosingBrace);
Douglas Gregor01d08012012-02-07 10:09:13 +0000867}
868
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000869LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
870 unsigned NumArrayIndexVars) {
871 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
872 if (NumArrayIndexVars)
873 Size += sizeof(VarDecl) * NumArrayIndexVars
874 + sizeof(unsigned) * (NumCaptures + 1);
875 void *Mem = C.Allocate(Size);
876 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
877}
878
Douglas Gregorda8962a2012-02-13 15:44:47 +0000879LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000880 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000881}
882
883LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000884 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000885}
886
887LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
888 return capture_begin();
889}
890
891LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
892 struct CXXRecordDecl::LambdaDefinitionData &Data
893 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000894 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000895}
896
897LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
898 return explicit_capture_end();
899}
900
901LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
902 return capture_end();
903}
904
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000905ArrayRef<VarDecl *>
906LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000907 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000908
909 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000910 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
911 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000912 VarDecl **IndexVars = getArrayIndexVars();
913 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000914 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
915 IndexVars + IndexStarts[Index + 1]);
916}
917
Douglas Gregor01d08012012-02-07 10:09:13 +0000918CXXRecordDecl *LambdaExpr::getLambdaClass() const {
919 return getType()->getAsCXXRecordDecl();
920}
921
922CXXMethodDecl *LambdaExpr::getCallOperator() const {
923 CXXRecordDecl *Record = getLambdaClass();
924 DeclarationName Name
925 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
926 DeclContext::lookup_result Calls = Record->lookup(Name);
927 assert(Calls.first != Calls.second && "Missing lambda call operator!");
928 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
929 assert(Calls.first == Calls.second && "More than lambda one call operator?");
930 return Result;
931}
932
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000933CompoundStmt *LambdaExpr::getBody() const {
934 if (!getStoredStmts()[NumCaptures])
935 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
936
937 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
938}
939
Douglas Gregor01d08012012-02-07 10:09:13 +0000940bool LambdaExpr::isMutable() const {
941 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
942}
943
John McCall80ee6e82011-11-10 05:35:25 +0000944ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
945 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000946 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000947 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000948 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000949 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000950 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000951 SubExpr(subexpr) {
952 ExprWithCleanupsBits.NumObjects = objects.size();
953 for (unsigned i = 0, e = objects.size(); i != e; ++i)
954 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000955}
956
John McCall80ee6e82011-11-10 05:35:25 +0000957ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
958 ArrayRef<CleanupObject> objects) {
959 size_t size = sizeof(ExprWithCleanups)
960 + objects.size() * sizeof(CleanupObject);
961 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
962 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000963}
964
John McCall80ee6e82011-11-10 05:35:25 +0000965ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
966 : Expr(ExprWithCleanupsClass, empty) {
967 ExprWithCleanupsBits.NumObjects = numObjects;
968}
Chris Lattnerd2598362010-05-10 00:25:06 +0000969
John McCall80ee6e82011-11-10 05:35:25 +0000970ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
971 unsigned numObjects) {
972 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
973 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
974 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000975}
976
Douglas Gregorab6677e2010-09-08 00:15:04 +0000977CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000978 SourceLocation LParenLoc,
979 Expr **Args,
980 unsigned NumArgs,
981 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000982 : Expr(CXXUnresolvedConstructExprClass,
983 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000984 (Type->getType()->isLValueReferenceType() ? VK_LValue
985 :Type->getType()->isRValueReferenceType()? VK_XValue
986 :VK_RValue),
987 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000988 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000989 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000990 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000991 LParenLoc(LParenLoc),
992 RParenLoc(RParenLoc),
993 NumArgs(NumArgs) {
994 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000995 for (unsigned I = 0; I != NumArgs; ++I) {
996 if (Args[I]->containsUnexpandedParameterPack())
997 ExprBits.ContainsUnexpandedParameterPack = true;
998
999 StoredArgs[I] = Args[I];
1000 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001001}
1002
1003CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001004CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +00001005 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001006 SourceLocation LParenLoc,
1007 Expr **Args,
1008 unsigned NumArgs,
1009 SourceLocation RParenLoc) {
1010 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1011 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +00001012 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +00001013 Args, NumArgs, RParenLoc);
1014}
1015
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001016CXXUnresolvedConstructExpr *
1017CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1018 Stmt::EmptyShell Empty;
1019 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1020 sizeof(Expr *) * NumArgs);
1021 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1022}
1023
Douglas Gregorab6677e2010-09-08 00:15:04 +00001024SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1025 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1026}
1027
John McCall865d4472009-11-19 22:55:06 +00001028CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001029 Expr *Base, QualType BaseType,
1030 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001031 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001032 NestedNameSpecifierLoc QualifierLoc,
1033 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001034 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001035 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001036 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001037 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001038 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001039 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001040 (QualifierLoc &&
1041 QualifierLoc.getNestedNameSpecifier()
1042 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001043 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001044 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001045 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001046 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001047 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001048 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001049 if (TemplateArgs) {
1050 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001051 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001052 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001053 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1054 Dependent,
1055 InstantiationDependent,
1056 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001057 if (ContainsUnexpandedParameterPack)
1058 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001059 } else if (TemplateKWLoc.isValid()) {
1060 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001061 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001062}
1063
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001064CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1065 Expr *Base, QualType BaseType,
1066 bool IsArrow,
1067 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001068 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001069 NamedDecl *FirstQualifierFoundInScope,
1070 DeclarationNameInfo MemberNameInfo)
1071 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001072 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001073 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001074 (QualifierLoc &&
1075 QualifierLoc.getNestedNameSpecifier()->
1076 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001077 MemberNameInfo.containsUnexpandedParameterPack())),
1078 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001079 HasTemplateKWAndArgsInfo(false),
1080 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001081 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1082 MemberNameInfo(MemberNameInfo) { }
1083
John McCall865d4472009-11-19 22:55:06 +00001084CXXDependentScopeMemberExpr *
1085CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001086 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001087 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001088 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001089 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001090 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001091 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001092 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001093 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001094 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1095 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001096 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001097 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001098 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001100 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1101 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1102 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001103
Chris Lattner32488542010-10-30 05:14:06 +00001104 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001105 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1106 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001107 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001108 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001109 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001110 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001111}
1112
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001113CXXDependentScopeMemberExpr *
1114CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001115 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001116 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001117 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001118 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001119 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001120 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001121 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001122
1123 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001124 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001125 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001126 CXXDependentScopeMemberExpr *E
1127 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001128 0, SourceLocation(),
1129 NestedNameSpecifierLoc(),
1130 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001131 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001132 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001133 return E;
1134}
1135
Douglas Gregor4c9be892011-02-28 20:01:57 +00001136bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1137 if (Base == 0)
1138 return true;
1139
Douglas Gregor75e85042011-03-02 21:06:53 +00001140 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001141}
1142
John McCall864c0412011-04-26 20:42:42 +00001143static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1144 UnresolvedSetIterator end) {
1145 do {
1146 NamedDecl *decl = *begin;
1147 if (isa<UnresolvedUsingValueDecl>(decl))
1148 return false;
1149 if (isa<UsingShadowDecl>(decl))
1150 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1151
1152 // Unresolved member expressions should only contain methods and
1153 // method templates.
1154 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1155
1156 if (isa<FunctionTemplateDecl>(decl))
1157 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1158 if (cast<CXXMethodDecl>(decl)->isStatic())
1159 return false;
1160 } while (++begin != end);
1161
1162 return true;
1163}
1164
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001165UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001166 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001167 Expr *Base, QualType BaseType,
1168 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001169 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001170 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001171 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001172 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001173 const TemplateArgumentListInfo *TemplateArgs,
1174 UnresolvedSetIterator Begin,
1175 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001176 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1177 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001178 // Dependent
1179 ((Base && Base->isTypeDependent()) ||
1180 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001181 ((Base && Base->isInstantiationDependent()) ||
1182 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001183 // Contains unexpanded parameter pack
1184 ((Base && Base->containsUnexpandedParameterPack()) ||
1185 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001186 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1187 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001188
1189 // Check whether all of the members are non-static member functions,
1190 // and if so, mark give this bound-member type instead of overload type.
1191 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1192 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001193}
1194
Douglas Gregor4c9be892011-02-28 20:01:57 +00001195bool UnresolvedMemberExpr::isImplicitAccess() const {
1196 if (Base == 0)
1197 return true;
1198
Douglas Gregor75e85042011-03-02 21:06:53 +00001199 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001200}
1201
John McCall129e2df2009-11-30 22:42:35 +00001202UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001203UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001204 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001205 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001206 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001207 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001208 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001209 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001210 const TemplateArgumentListInfo *TemplateArgs,
1211 UnresolvedSetIterator Begin,
1212 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001213 std::size_t size = sizeof(UnresolvedMemberExpr);
1214 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001215 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1216 else if (TemplateKWLoc.isValid())
1217 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001218
Chris Lattner32488542010-10-30 05:14:06 +00001219 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001220 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001221 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001222 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001223 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001224}
1225
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001226UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001227UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001228 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001229 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001230 if (HasTemplateKWAndArgsInfo)
1231 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001232
Chris Lattner32488542010-10-30 05:14:06 +00001233 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001234 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001235 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001236 return E;
1237}
1238
John McCallc373d482010-01-27 01:50:18 +00001239CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1240 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1241
1242 // If there was a nested name specifier, it names the naming class.
1243 // It can't be dependent: after all, we were actually able to do the
1244 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001245 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001246 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001247 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001248 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001249 Record = T->getAsCXXRecordDecl();
1250 assert(Record && "qualifier in member expression does not name record");
1251 }
John McCallc373d482010-01-27 01:50:18 +00001252 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001253 else {
John McCallc373d482010-01-27 01:50:18 +00001254 QualType BaseType = getBaseType().getNonReferenceType();
1255 if (isArrow()) {
1256 const PointerType *PT = BaseType->getAs<PointerType>();
1257 assert(PT && "base of arrow member access is not pointer");
1258 BaseType = PT->getPointeeType();
1259 }
1260
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001261 Record = BaseType->getAsCXXRecordDecl();
1262 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001263 }
1264
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001265 return Record;
John McCallc373d482010-01-27 01:50:18 +00001266}
1267
Douglas Gregorc7793c72011-01-15 01:15:58 +00001268SubstNonTypeTemplateParmPackExpr::
1269SubstNonTypeTemplateParmPackExpr(QualType T,
1270 NonTypeTemplateParmDecl *Param,
1271 SourceLocation NameLoc,
1272 const TemplateArgument &ArgPack)
1273 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001274 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001275 Param(Param), Arguments(ArgPack.pack_begin()),
1276 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1277
1278TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1279 return TemplateArgument(Arguments, NumArguments);
1280}
1281
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00001282TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1283 ArrayRef<TypeSourceInfo *> Args,
1284 SourceLocation RParenLoc,
1285 bool Value)
1286 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1287 /*TypeDependent=*/false,
1288 /*ValueDependent=*/false,
1289 /*InstantiationDependent=*/false,
1290 /*ContainsUnexpandedParameterPack=*/false),
1291 Loc(Loc), RParenLoc(RParenLoc)
1292{
1293 TypeTraitExprBits.Kind = Kind;
1294 TypeTraitExprBits.Value = Value;
1295 TypeTraitExprBits.NumArgs = Args.size();
1296
1297 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1298
1299 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1300 if (Args[I]->getType()->isDependentType())
1301 setValueDependent(true);
1302 if (Args[I]->getType()->isInstantiationDependentType())
1303 setInstantiationDependent(true);
1304 if (Args[I]->getType()->containsUnexpandedParameterPack())
1305 setContainsUnexpandedParameterPack(true);
1306
1307 ToArgs[I] = Args[I];
1308 }
1309}
1310
1311TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1312 SourceLocation Loc,
1313 TypeTrait Kind,
1314 ArrayRef<TypeSourceInfo *> Args,
1315 SourceLocation RParenLoc,
1316 bool Value) {
1317 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1318 void *Mem = C.Allocate(Size);
1319 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1320}
1321
1322TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1323 unsigned NumArgs) {
1324 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1325 void *Mem = C.Allocate(Size);
1326 return new (Mem) TypeTraitExpr(EmptyShell());
1327}
1328
David Blaikie99ba9e32011-12-20 02:48:34 +00001329void ArrayTypeTraitExpr::anchor() { }