blob: 97814afa18bf73a6a56a37f0c872ca7eccc88b60 [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneke3a0c142007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor993603d2008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000020using namespace clang;
21
Douglas Gregor9da64192010-04-26 22:37:10 +000022
Ted Kremeneke3a0c142007-08-24 20:21:10 +000023//===----------------------------------------------------------------------===//
24// Child Iterators for iterating over subexpressions/substatements
25//===----------------------------------------------------------------------===//
26
Richard Smithef8bf432012-08-13 20:08:14 +000027bool CXXTypeidExpr::isPotentiallyEvaluated() const {
28 if (isTypeOperand())
29 return false;
30
31 // C++11 [expr.typeid]p3:
32 // When typeid is applied to an expression other than a glvalue of
33 // polymorphic class type, [...] the expression is an unevaluated operand.
34 const Expr *E = getExprOperand();
35 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
36 if (RD->isPolymorphic() && E->isGLValue())
37 return true;
38
39 return false;
40}
41
Douglas Gregor9da64192010-04-26 22:37:10 +000042QualType CXXTypeidExpr::getTypeOperand() const {
43 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
44 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
45 .getUnqualifiedType();
46}
47
Francois Pichet9f4f2072010-09-08 12:20:18 +000048QualType CXXUuidofExpr::getTypeOperand() const {
49 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
50 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
51 .getUnqualifiedType();
52}
53
Douglas Gregor747eb782010-07-08 06:14:04 +000054// CXXScalarValueInitExpr
Douglas Gregor2b88c112010-09-08 00:15:04 +000055SourceRange CXXScalarValueInitExpr::getSourceRange() const {
56 SourceLocation Start = RParenLoc;
57 if (TypeInfo)
58 Start = TypeInfo->getTypeLoc().getBeginLoc();
59 return SourceRange(Start, RParenLoc);
60}
61
Sebastian Redlbd150f42008-11-21 19:14:01 +000062// CXXNewExpr
Ted Kremenek9d6eb402010-02-11 22:51:03 +000063CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redlc3a3c602012-02-16 11:35:52 +000064 FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +000065 bool usualArrayDeleteWantsSize,
66 Expr **placementArgs, unsigned numPlaceArgs,
67 SourceRange typeIdParens, Expr *arraySize,
68 InitializationStyle initializationStyle,
69 Expr *initializer, QualType ty,
70 TypeSourceInfo *allocatedTypeInfo,
71 SourceLocation startLoc, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +000072 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000073 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000074 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000075 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +000076 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
77 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +000078 StartLoc(startLoc), DirectInitRange(directInitRange),
79 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Sebastian Redl6047f072012-02-16 12:22:20 +000080 assert((initializer != 0 || initializationStyle == NoInit) &&
81 "Only NoInit can have no initializer.");
82 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
83 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +000084 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000085 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000086 if (arraySize->isInstantiationDependent())
87 ExprBits.InstantiationDependent = true;
88
Douglas Gregora6e053e2010-12-15 01:34:56 +000089 if (arraySize->containsUnexpandedParameterPack())
90 ExprBits.ContainsUnexpandedParameterPack = true;
91
Sebastian Redl351bb782008-12-02 14:43:59 +000092 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000093 }
94
Sebastian Redl6047f072012-02-16 12:22:20 +000095 if (initializer) {
96 if (initializer->isInstantiationDependent())
97 ExprBits.InstantiationDependent = true;
98
99 if (initializer->containsUnexpandedParameterPack())
100 ExprBits.ContainsUnexpandedParameterPack = true;
101
102 SubExprs[i++] = initializer;
103 }
104
Douglas Gregora6e053e2010-12-15 01:34:56 +0000105 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000106 if (placementArgs[j]->isInstantiationDependent())
107 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000108 if (placementArgs[j]->containsUnexpandedParameterPack())
109 ExprBits.ContainsUnexpandedParameterPack = true;
110
Sebastian Redlbd150f42008-11-21 19:14:01 +0000111 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000112 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000113}
114
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000115void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000116 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000117 assert(SubExprs == 0 && "SubExprs already allocated");
118 Array = isArray;
119 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000120
121 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000122 SubExprs = new (C) Stmt*[TotalSize];
123}
124
Sebastian Redl31ad7542011-03-13 17:09:40 +0000125bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000126 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000127 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000128}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000129
Sebastian Redl6047f072012-02-16 12:22:20 +0000130SourceLocation CXXNewExpr::getEndLoc() const {
131 switch (getInitializationStyle()) {
132 case NoInit:
133 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
134 case CallInit:
135 return DirectInitRange.getEnd();
136 case ListInit:
137 return getInitializer()->getSourceRange().getEnd();
138 }
Matt Beaumont-Gay2150d382012-02-16 22:15:50 +0000139 llvm_unreachable("bogus initialization style");
Sebastian Redl6047f072012-02-16 12:22:20 +0000140}
141
Sebastian Redlbd150f42008-11-21 19:14:01 +0000142// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000143QualType CXXDeleteExpr::getDestroyedType() const {
144 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000145 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000146 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000147
148 if (ArgType->isDependentType() && !ArgType->isPointerType())
149 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000150
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000151 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000152}
153
Douglas Gregorad8a3362009-09-04 17:36:40 +0000154// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000155PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
156 : Type(Info)
157{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000158 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000159}
160
John McCalldb40c7f2010-12-14 08:05:40 +0000161CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000162 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
163 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
164 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
165 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000166 : Expr(CXXPseudoDestructorExprClass,
167 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
168 FunctionProtoType::ExtProtoInfo())),
169 VK_RValue, OK_Ordinary,
170 /*isTypeDependent=*/(Base->isTypeDependent() ||
171 (DestroyedType.getTypeSourceInfo() &&
172 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000173 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000174 (Base->isInstantiationDependent() ||
175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
177 (ScopeType &&
178 ScopeType->getType()->isInstantiationDependentType()) ||
179 (DestroyedType.getTypeSourceInfo() &&
180 DestroyedType.getTypeSourceInfo()->getType()
181 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000182 // ContainsUnexpandedParameterPack
183 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000184 (QualifierLoc &&
185 QualifierLoc.getNestedNameSpecifier()
186 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000187 (ScopeType &&
188 ScopeType->getType()->containsUnexpandedParameterPack()) ||
189 (DestroyedType.getTypeSourceInfo() &&
190 DestroyedType.getTypeSourceInfo()->getType()
191 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000192 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000193 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000194 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
195 DestroyedType(DestroyedType) { }
196
Douglas Gregor678f90d2010-02-25 01:56:36 +0000197QualType CXXPseudoDestructorExpr::getDestroyedType() const {
198 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
199 return TInfo->getType();
200
201 return QualType();
202}
203
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000204SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000205 SourceLocation End = DestroyedType.getLocation();
206 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000207 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000208 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000209}
210
John McCalld14a8642009-11-21 08:51:07 +0000211// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000212UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000213UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000214 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000215 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000216 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000217 const DeclarationNameInfo &NameInfo,
218 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000219 const TemplateArgumentListInfo *Args,
220 UnresolvedSetIterator Begin,
221 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000222{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000223 assert(Args || TemplateKWLoc.isValid());
224 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000225 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000226 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000227 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
228 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000229 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000230 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000231}
232
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000233UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000234UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
235 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000236 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000237 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000238 if (HasTemplateKWAndArgsInfo)
239 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000240
Chris Lattner5c0b4052010-10-30 05:14:06 +0000241 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000242 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000243 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000244 return E;
245}
246
Douglas Gregora6e053e2010-12-15 01:34:56 +0000247OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000248 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000249 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000250 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000251 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000252 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000253 UnresolvedSetIterator End,
254 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000255 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000256 bool KnownContainsUnexpandedParameterPack)
257 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
258 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000259 (KnownInstantiationDependent ||
260 NameInfo.isInstantiationDependent() ||
261 (QualifierLoc &&
262 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000263 (KnownContainsUnexpandedParameterPack ||
264 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000265 (QualifierLoc &&
266 QualifierLoc.getNestedNameSpecifier()
267 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000268 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
269 Results(0), NumResults(End - Begin),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000270 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000271{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000272 NumResults = End - Begin;
273 if (NumResults) {
274 // Determine whether this expression is type-dependent.
275 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
276 if ((*I)->getDeclContext()->isDependentContext() ||
277 isa<UnresolvedUsingValueDecl>(*I)) {
278 ExprBits.TypeDependent = true;
279 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000280 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000281 }
282 }
283
284 Results = static_cast<DeclAccessPair *>(
285 C.Allocate(sizeof(DeclAccessPair) * NumResults,
286 llvm::alignOf<DeclAccessPair>()));
287 memcpy(Results, &*Begin.getIterator(),
288 NumResults * sizeof(DeclAccessPair));
289 }
290
291 // If we have explicit template arguments, check for dependent
292 // template arguments and whether they contain any unexpanded pack
293 // expansions.
294 if (TemplateArgs) {
295 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000296 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000297 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000298 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
299 Dependent,
300 InstantiationDependent,
301 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000302
303 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000304 ExprBits.TypeDependent = true;
305 ExprBits.ValueDependent = true;
306 }
307 if (InstantiationDependent)
308 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000309 if (ContainsUnexpandedParameterPack)
310 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000311 } else if (TemplateKWLoc.isValid()) {
312 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000313 }
314
315 if (isTypeDependent())
316 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000317}
318
319void OverloadExpr::initializeResults(ASTContext &C,
320 UnresolvedSetIterator Begin,
321 UnresolvedSetIterator End) {
322 assert(Results == 0 && "Results already initialized!");
323 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000324 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000325 Results = static_cast<DeclAccessPair *>(
326 C.Allocate(sizeof(DeclAccessPair) * NumResults,
327
328 llvm::alignOf<DeclAccessPair>()));
329 memcpy(Results, &*Begin.getIterator(),
330 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000331 }
332}
333
John McCall8c12dc42010-04-22 18:44:12 +0000334CXXRecordDecl *OverloadExpr::getNamingClass() const {
335 if (isa<UnresolvedLookupExpr>(this))
336 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
337 else
338 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
339}
340
John McCall8cd78132009-11-19 22:55:06 +0000341// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000342DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000343 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000344 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 const DeclarationNameInfo &NameInfo,
346 const TemplateArgumentListInfo *Args)
347 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
348 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000349 (NameInfo.isInstantiationDependent() ||
350 (QualifierLoc &&
351 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000352 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000353 (QualifierLoc &&
354 QualifierLoc.getNestedNameSpecifier()
355 ->containsUnexpandedParameterPack()))),
356 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000357 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000358{
359 if (Args) {
360 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000361 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000362 bool ContainsUnexpandedParameterPack
363 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000364 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
365 Dependent,
366 InstantiationDependent,
367 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000368 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 } else if (TemplateKWLoc.isValid()) {
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000371 }
372}
373
John McCalle66edc12009-11-24 19:00:30 +0000374DependentScopeDeclRefExpr *
375DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000376 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000378 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000379 const TemplateArgumentListInfo *Args) {
380 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000381 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000382 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
383 else if (TemplateKWLoc.isValid())
384 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000385 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
387 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000388}
389
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000390DependentScopeDeclRefExpr *
391DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000392 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000393 unsigned NumTemplateArgs) {
394 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000395 if (HasTemplateKWAndArgsInfo)
396 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000397 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000398 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000399 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000400 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000401 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000402 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000403 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000404}
405
Chandler Carruth01718152010-10-25 08:47:36 +0000406SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000407 if (isa<CXXTemporaryObjectExpr>(this))
408 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
409
Douglas Gregor15417cf2010-11-03 00:35:38 +0000410 if (ParenRange.isValid())
411 return SourceRange(Loc, ParenRange.getEnd());
412
413 SourceLocation End = Loc;
414 for (unsigned I = getNumArgs(); I > 0; --I) {
415 const Expr *Arg = getArg(I-1);
416 if (!Arg->isDefaultArgument()) {
417 SourceLocation NewEnd = Arg->getLocEnd();
418 if (NewEnd.isValid()) {
419 End = NewEnd;
420 break;
421 }
422 }
423 }
424
425 return SourceRange(Loc, End);
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000426}
427
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000428SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000429 OverloadedOperatorKind Kind = getOperator();
430 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
431 if (getNumArgs() == 1)
432 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000433 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000434 else
435 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000436 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000437 } else if (Kind == OO_Arrow) {
438 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000439 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000440 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000441 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000442 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000443 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000444 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000445 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000446 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000447 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000448 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000449 }
450}
451
Ted Kremenek98a24e32011-03-30 17:41:19 +0000452Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000453 const Expr *Callee = getCallee()->IgnoreParens();
454 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000455 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000456 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
457 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
458 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000459
460 // FIXME: Will eventually need to cope with member pointers.
461 return 0;
462}
463
Ted Kremenek98a24e32011-03-30 17:41:19 +0000464CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
465 if (const MemberExpr *MemExpr =
466 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
467 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
468
469 // FIXME: Will eventually need to cope with member pointers.
470 return 0;
471}
472
473
David Blaikiec0f58662012-05-03 16:25:49 +0000474CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000475 Expr* ThisArg = getImplicitObjectArgument();
476 if (!ThisArg)
477 return 0;
478
479 if (ThisArg->getType()->isAnyPointerType())
480 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
481
482 return ThisArg->getType()->getAsCXXRecordDecl();
483}
484
Douglas Gregoref986e82009-11-12 15:31:47 +0000485
Douglas Gregore200adc2008-10-27 19:41:14 +0000486//===----------------------------------------------------------------------===//
487// Named casts
488//===----------------------------------------------------------------------===//
489
490/// getCastName - Get the name of the C++ cast being used, e.g.,
491/// "static_cast", "dynamic_cast", "reinterpret_cast", or
492/// "const_cast". The returned pointer must not be freed.
493const char *CXXNamedCastExpr::getCastName() const {
494 switch (getStmtClass()) {
495 case CXXStaticCastExprClass: return "static_cast";
496 case CXXDynamicCastExprClass: return "dynamic_cast";
497 case CXXReinterpretCastExprClass: return "reinterpret_cast";
498 case CXXConstCastExprClass: return "const_cast";
499 default: return "<invalid cast>";
500 }
501}
Douglas Gregordd04d332009-01-16 18:33:17 +0000502
John McCallcf142162010-08-07 06:22:56 +0000503CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000504 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000505 CastKind K, Expr *Op,
506 const CXXCastPath *BasePath,
507 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000508 SourceLocation L,
509 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000510 unsigned PathSize = (BasePath ? BasePath->size() : 0);
511 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
512 + PathSize * sizeof(CXXBaseSpecifier*));
513 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000514 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
515 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000516 if (PathSize) E->setCastPath(*BasePath);
517 return E;
518}
519
520CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
521 unsigned PathSize) {
522 void *Buffer =
523 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
524 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
525}
526
527CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000528 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000529 CastKind K, Expr *Op,
530 const CXXCastPath *BasePath,
531 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000532 SourceLocation L,
533 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000534 unsigned PathSize = (BasePath ? BasePath->size() : 0);
535 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
536 + PathSize * sizeof(CXXBaseSpecifier*));
537 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000538 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
539 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000540 if (PathSize) E->setCastPath(*BasePath);
541 return E;
542}
543
544CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
545 unsigned PathSize) {
546 void *Buffer =
547 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
548 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
549}
550
Anders Carlsson267c0c92011-04-11 01:43:55 +0000551/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
552/// to always be null. For example:
553///
554/// struct A { };
555/// struct B final : A { };
556/// struct C { };
557///
558/// C *f(B* b) { return dynamic_cast<C*>(b); }
559bool CXXDynamicCastExpr::isAlwaysNull() const
560{
561 QualType SrcType = getSubExpr()->getType();
562 QualType DestType = getType();
563
564 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
565 SrcType = SrcPTy->getPointeeType();
566 DestType = DestType->castAs<PointerType>()->getPointeeType();
567 }
568
Alexis Hunt78e2b912012-06-19 23:44:55 +0000569 if (DestType->isVoidType())
570 return false;
571
Anders Carlsson267c0c92011-04-11 01:43:55 +0000572 const CXXRecordDecl *SrcRD =
573 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
574
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000575 if (!SrcRD->hasAttr<FinalAttr>())
576 return false;
577
Anders Carlsson267c0c92011-04-11 01:43:55 +0000578 const CXXRecordDecl *DestRD =
579 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
580
581 return !DestRD->isDerivedFrom(SrcRD);
582}
583
John McCallcf142162010-08-07 06:22:56 +0000584CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000585CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
586 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000587 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000588 TypeSourceInfo *WrittenTy, SourceLocation L,
589 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000590 unsigned PathSize = (BasePath ? BasePath->size() : 0);
591 void *Buffer =
592 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
593 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000594 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
595 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000596 if (PathSize) E->setCastPath(*BasePath);
597 return E;
598}
599
600CXXReinterpretCastExpr *
601CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
602 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
603 + PathSize * sizeof(CXXBaseSpecifier*));
604 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
605}
606
John McCall7decc9e2010-11-18 06:31:45 +0000607CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
608 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000609 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000610 SourceLocation L,
611 SourceLocation RParenLoc) {
612 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000613}
614
615CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
616 return new (C) CXXConstCastExpr(EmptyShell());
617}
618
619CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000620CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000621 TypeSourceInfo *Written, SourceLocation L,
622 CastKind K, Expr *Op, const CXXCastPath *BasePath,
623 SourceLocation R) {
624 unsigned PathSize = (BasePath ? BasePath->size() : 0);
625 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
626 + PathSize * sizeof(CXXBaseSpecifier*));
627 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000628 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000629 if (PathSize) E->setCastPath(*BasePath);
630 return E;
631}
632
633CXXFunctionalCastExpr *
634CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
635 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
636 + PathSize * sizeof(CXXBaseSpecifier*));
637 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
638}
639
Richard Smithc67fdd42012-03-07 08:35:16 +0000640UserDefinedLiteral::LiteralOperatorKind
641UserDefinedLiteral::getLiteralOperatorKind() const {
642 if (getNumArgs() == 0)
643 return LOK_Template;
644 if (getNumArgs() == 2)
645 return LOK_String;
646
647 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
648 QualType ParamTy =
649 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
650 if (ParamTy->isPointerType())
651 return LOK_Raw;
652 if (ParamTy->isAnyCharacterType())
653 return LOK_Character;
654 if (ParamTy->isIntegerType())
655 return LOK_Integer;
656 if (ParamTy->isFloatingType())
657 return LOK_Floating;
658
659 llvm_unreachable("unknown kind of literal operator");
660}
661
662Expr *UserDefinedLiteral::getCookedLiteral() {
663#ifndef NDEBUG
664 LiteralOperatorKind LOK = getLiteralOperatorKind();
665 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
666#endif
667 return getArg(0);
668}
669
670const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
671 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
672}
John McCallcf142162010-08-07 06:22:56 +0000673
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000674CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000675CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
676 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000677 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000678 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
679 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000680}
681
Mike Stump11289f42009-09-09 15:08:12 +0000682CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000683 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000684 return new (C) CXXTemporary(Destructor);
685}
686
Mike Stump11289f42009-09-09 15:08:12 +0000687CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000688 CXXTemporary *Temp,
689 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000690 assert((SubExpr->getType()->isRecordType() ||
691 SubExpr->getType()->isArrayType()) &&
692 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000693
Douglas Gregora6e053e2010-12-15 01:34:56 +0000694 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000695}
696
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000697CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000698 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000699 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000700 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000701 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000702 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000703 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000704 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000705 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
706 Type->getType().getNonReferenceType(),
707 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000708 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000709 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000710 CXXConstructExpr::CK_Complete, parenRange),
711 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000712}
713
714SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000715 return SourceRange(Type->getTypeLoc().getBeginLoc(),
716 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000717}
Anders Carlsson6f287832009-04-21 02:22:11 +0000718
Mike Stump11289f42009-09-09 15:08:12 +0000719CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000720 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000721 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000722 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000723 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000724 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000725 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000726 ConstructionKind ConstructKind,
727 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000728 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000729 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000730 HadMultipleCandidates, ListInitialization,
731 ZeroInitialization, ConstructKind,
732 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000733}
734
Mike Stump11289f42009-09-09 15:08:12 +0000735CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000736 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000737 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000738 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000739 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000740 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000741 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000742 ConstructionKind ConstructKind,
743 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000744 : Expr(SC, T, VK_RValue, OK_Ordinary,
745 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000746 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000747 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000748 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000749 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000750 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000751 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000752 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000753{
754 if (NumArgs) {
755 Args = new (C) Stmt*[NumArgs];
756
757 for (unsigned i = 0; i != NumArgs; ++i) {
758 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000759
760 if (args[i]->isValueDependent())
761 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000762 if (args[i]->isInstantiationDependent())
763 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000764 if (args[i]->containsUnexpandedParameterPack())
765 ExprBits.ContainsUnexpandedParameterPack = true;
766
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000767 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000768 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000769 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000770}
771
Douglas Gregore31e6062012-02-07 10:09:13 +0000772LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
773 LambdaCaptureKind Kind, VarDecl *Var,
774 SourceLocation EllipsisLoc)
775 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
776{
777 unsigned Bits = 0;
778 if (Implicit)
779 Bits |= Capture_Implicit;
780
781 switch (Kind) {
782 case LCK_This:
783 assert(Var == 0 && "'this' capture cannot have a variable!");
784 break;
785
786 case LCK_ByCopy:
787 Bits |= Capture_ByCopy;
788 // Fall through
789 case LCK_ByRef:
790 assert(Var && "capture must have a variable!");
791 break;
792 }
793 VarAndBits.setInt(Bits);
794}
795
796LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
797 if (capturesThis())
798 return LCK_This;
799
800 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
801}
802
803LambdaExpr::LambdaExpr(QualType T,
804 SourceRange IntroducerRange,
805 LambdaCaptureDefault CaptureDefault,
806 ArrayRef<Capture> Captures,
807 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000808 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000809 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000810 ArrayRef<VarDecl *> ArrayIndexVars,
811 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000812 SourceLocation ClosingBrace,
813 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000814 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
815 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000816 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000817 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000818 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000819 CaptureDefault(CaptureDefault),
820 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000821 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000822 ClosingBrace(ClosingBrace)
823{
824 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000825 CXXRecordDecl *Class = getLambdaClass();
826 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000827
828 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000829
830 // Copy captures.
831 ASTContext &Context = Class->getASTContext();
832 Data.NumCaptures = NumCaptures;
833 Data.NumExplicitCaptures = 0;
834 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
835 Capture *ToCapture = Data.Captures;
836 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
837 if (Captures[I].isExplicit())
838 ++Data.NumExplicitCaptures;
839
840 *ToCapture++ = Captures[I];
841 }
842
843 // Copy initialization expressions for the non-static data members.
844 Stmt **Stored = getStoredStmts();
845 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
846 *Stored++ = CaptureInits[I];
847
848 // Copy the body of the lambda.
849 *Stored++ = getCallOperator()->getBody();
850
851 // Copy the array index variables, if any.
852 HasArrayIndexVars = !ArrayIndexVars.empty();
853 if (HasArrayIndexVars) {
854 assert(ArrayIndexStarts.size() == NumCaptures);
855 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
856 sizeof(VarDecl *) * ArrayIndexVars.size());
857 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
858 sizeof(unsigned) * Captures.size());
859 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000860 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000861}
862
863LambdaExpr *LambdaExpr::Create(ASTContext &Context,
864 CXXRecordDecl *Class,
865 SourceRange IntroducerRange,
866 LambdaCaptureDefault CaptureDefault,
867 ArrayRef<Capture> Captures,
868 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000869 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000870 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000871 ArrayRef<VarDecl *> ArrayIndexVars,
872 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000873 SourceLocation ClosingBrace,
874 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000875 // Determine the type of the expression (i.e., the type of the
876 // function object we're creating).
877 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000878
Douglas Gregore5561632012-02-13 17:20:40 +0000879 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +0000880 if (!ArrayIndexVars.empty()) {
881 Size += sizeof(unsigned) * (Captures.size() + 1);
882 // Realign for following VarDecl array.
883 unsigned Align = llvm::alignOf<VarDecl*>();
884 Size = (Size + Align - 1) & ~(Align - 1);
885 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
886 }
Douglas Gregore5561632012-02-13 17:20:40 +0000887 void *Mem = Context.Allocate(Size);
888 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000889 Captures, ExplicitParams, ExplicitResultType,
890 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000891 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000892}
893
Douglas Gregor99ae8062012-02-14 17:54:36 +0000894LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
895 unsigned NumArrayIndexVars) {
896 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
897 if (NumArrayIndexVars)
898 Size += sizeof(VarDecl) * NumArrayIndexVars
899 + sizeof(unsigned) * (NumCaptures + 1);
900 void *Mem = C.Allocate(Size);
901 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
902}
903
Douglas Gregorc8a73492012-02-13 15:44:47 +0000904LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000905 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000906}
907
908LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000909 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000910}
911
912LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
913 return capture_begin();
914}
915
916LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
917 struct CXXRecordDecl::LambdaDefinitionData &Data
918 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000919 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000920}
921
922LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
923 return explicit_capture_end();
924}
925
926LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
927 return capture_end();
928}
929
Douglas Gregor54fcea62012-02-13 16:35:30 +0000930ArrayRef<VarDecl *>
931LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000932 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000933
934 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000935 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
936 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000937 VarDecl **IndexVars = getArrayIndexVars();
938 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000939 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
940 IndexVars + IndexStarts[Index + 1]);
941}
942
Douglas Gregore31e6062012-02-07 10:09:13 +0000943CXXRecordDecl *LambdaExpr::getLambdaClass() const {
944 return getType()->getAsCXXRecordDecl();
945}
946
947CXXMethodDecl *LambdaExpr::getCallOperator() const {
948 CXXRecordDecl *Record = getLambdaClass();
949 DeclarationName Name
950 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
951 DeclContext::lookup_result Calls = Record->lookup(Name);
952 assert(Calls.first != Calls.second && "Missing lambda call operator!");
953 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
954 assert(Calls.first == Calls.second && "More than lambda one call operator?");
955 return Result;
956}
957
Douglas Gregor99ae8062012-02-14 17:54:36 +0000958CompoundStmt *LambdaExpr::getBody() const {
959 if (!getStoredStmts()[NumCaptures])
960 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
961
962 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
963}
964
Douglas Gregore31e6062012-02-07 10:09:13 +0000965bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +0000966 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +0000967}
968
John McCall28fc7092011-11-10 05:35:25 +0000969ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
970 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000971 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000972 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000973 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000974 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000975 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000976 SubExpr(subexpr) {
977 ExprWithCleanupsBits.NumObjects = objects.size();
978 for (unsigned i = 0, e = objects.size(); i != e; ++i)
979 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000980}
981
John McCall28fc7092011-11-10 05:35:25 +0000982ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
983 ArrayRef<CleanupObject> objects) {
984 size_t size = sizeof(ExprWithCleanups)
985 + objects.size() * sizeof(CleanupObject);
986 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
987 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000988}
989
John McCall28fc7092011-11-10 05:35:25 +0000990ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
991 : Expr(ExprWithCleanupsClass, empty) {
992 ExprWithCleanupsBits.NumObjects = numObjects;
993}
Chris Lattnercba86142010-05-10 00:25:06 +0000994
John McCall28fc7092011-11-10 05:35:25 +0000995ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
996 unsigned numObjects) {
997 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
998 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
999 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001000}
1001
Douglas Gregor2b88c112010-09-08 00:15:04 +00001002CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001003 SourceLocation LParenLoc,
1004 Expr **Args,
1005 unsigned NumArgs,
1006 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001007 : Expr(CXXUnresolvedConstructExprClass,
1008 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001009 (Type->getType()->isLValueReferenceType() ? VK_LValue
1010 :Type->getType()->isRValueReferenceType()? VK_XValue
1011 :VK_RValue),
1012 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001013 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001014 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001015 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001016 LParenLoc(LParenLoc),
1017 RParenLoc(RParenLoc),
1018 NumArgs(NumArgs) {
1019 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001020 for (unsigned I = 0; I != NumArgs; ++I) {
1021 if (Args[I]->containsUnexpandedParameterPack())
1022 ExprBits.ContainsUnexpandedParameterPack = true;
1023
1024 StoredArgs[I] = Args[I];
1025 }
Douglas Gregorce934142009-05-20 18:46:25 +00001026}
1027
1028CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001029CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001030 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001031 SourceLocation LParenLoc,
1032 Expr **Args,
1033 unsigned NumArgs,
1034 SourceLocation RParenLoc) {
1035 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1036 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +00001037 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +00001038 Args, NumArgs, RParenLoc);
1039}
1040
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001041CXXUnresolvedConstructExpr *
1042CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1043 Stmt::EmptyShell Empty;
1044 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1045 sizeof(Expr *) * NumArgs);
1046 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1047}
1048
Douglas Gregor2b88c112010-09-08 00:15:04 +00001049SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1050 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1051}
1052
John McCall8cd78132009-11-19 22:55:06 +00001053CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001054 Expr *Base, QualType BaseType,
1055 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001056 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001057 NestedNameSpecifierLoc QualifierLoc,
1058 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001059 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001060 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001061 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001062 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001063 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001064 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001065 (QualifierLoc &&
1066 QualifierLoc.getNestedNameSpecifier()
1067 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001068 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001069 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001070 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001071 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001072 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001073 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001074 if (TemplateArgs) {
1075 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001076 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001077 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001078 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1079 Dependent,
1080 InstantiationDependent,
1081 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001082 if (ContainsUnexpandedParameterPack)
1083 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001084 } else if (TemplateKWLoc.isValid()) {
1085 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001086 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001087}
1088
Douglas Gregora6e053e2010-12-15 01:34:56 +00001089CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1090 Expr *Base, QualType BaseType,
1091 bool IsArrow,
1092 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001093 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001094 NamedDecl *FirstQualifierFoundInScope,
1095 DeclarationNameInfo MemberNameInfo)
1096 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001097 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001098 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001099 (QualifierLoc &&
1100 QualifierLoc.getNestedNameSpecifier()->
1101 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001102 MemberNameInfo.containsUnexpandedParameterPack())),
1103 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001104 HasTemplateKWAndArgsInfo(false),
1105 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001106 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1107 MemberNameInfo(MemberNameInfo) { }
1108
John McCall8cd78132009-11-19 22:55:06 +00001109CXXDependentScopeMemberExpr *
1110CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001111 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001112 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001113 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001114 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001115 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001116 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001117 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001118 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001119 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1120 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001121 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001122 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001123 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001124
Abramo Bagnara7945c982012-01-27 09:46:47 +00001125 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1126 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1127 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001128
Chris Lattner5c0b4052010-10-30 05:14:06 +00001129 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001130 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1131 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001132 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001133 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001134 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001135 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001136}
1137
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001138CXXDependentScopeMemberExpr *
1139CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001140 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001141 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001142 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001143 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001144 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001145 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001146 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001147
1148 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001149 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001150 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001151 CXXDependentScopeMemberExpr *E
1152 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001153 0, SourceLocation(),
1154 NestedNameSpecifierLoc(),
1155 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001156 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001157 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001158 return E;
1159}
1160
Douglas Gregor0da1d432011-02-28 20:01:57 +00001161bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1162 if (Base == 0)
1163 return true;
1164
Douglas Gregor25b7e052011-03-02 21:06:53 +00001165 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001166}
1167
John McCall0009fcc2011-04-26 20:42:42 +00001168static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1169 UnresolvedSetIterator end) {
1170 do {
1171 NamedDecl *decl = *begin;
1172 if (isa<UnresolvedUsingValueDecl>(decl))
1173 return false;
1174 if (isa<UsingShadowDecl>(decl))
1175 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1176
1177 // Unresolved member expressions should only contain methods and
1178 // method templates.
1179 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1180
1181 if (isa<FunctionTemplateDecl>(decl))
1182 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1183 if (cast<CXXMethodDecl>(decl)->isStatic())
1184 return false;
1185 } while (++begin != end);
1186
1187 return true;
1188}
1189
Douglas Gregora6e053e2010-12-15 01:34:56 +00001190UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001191 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001192 Expr *Base, QualType BaseType,
1193 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001194 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001195 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001196 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001197 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001198 const TemplateArgumentListInfo *TemplateArgs,
1199 UnresolvedSetIterator Begin,
1200 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001201 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1202 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001203 // Dependent
1204 ((Base && Base->isTypeDependent()) ||
1205 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001206 ((Base && Base->isInstantiationDependent()) ||
1207 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001208 // Contains unexpanded parameter pack
1209 ((Base && Base->containsUnexpandedParameterPack()) ||
1210 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001211 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1212 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001213
1214 // Check whether all of the members are non-static member functions,
1215 // and if so, mark give this bound-member type instead of overload type.
1216 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1217 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001218}
1219
Douglas Gregor0da1d432011-02-28 20:01:57 +00001220bool UnresolvedMemberExpr::isImplicitAccess() const {
1221 if (Base == 0)
1222 return true;
1223
Douglas Gregor25b7e052011-03-02 21:06:53 +00001224 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001225}
1226
John McCall10eae182009-11-30 22:42:35 +00001227UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001228UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001229 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001230 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001231 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001232 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001233 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001234 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001235 const TemplateArgumentListInfo *TemplateArgs,
1236 UnresolvedSetIterator Begin,
1237 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001238 std::size_t size = sizeof(UnresolvedMemberExpr);
1239 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001240 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1241 else if (TemplateKWLoc.isValid())
1242 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001243
Chris Lattner5c0b4052010-10-30 05:14:06 +00001244 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001245 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001246 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001247 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001248 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001249}
1250
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001251UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001252UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001253 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001254 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001255 if (HasTemplateKWAndArgsInfo)
1256 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001257
Chris Lattner5c0b4052010-10-30 05:14:06 +00001258 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001259 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001260 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001261 return E;
1262}
1263
John McCall58cc69d2010-01-27 01:50:18 +00001264CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1265 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1266
1267 // If there was a nested name specifier, it names the naming class.
1268 // It can't be dependent: after all, we were actually able to do the
1269 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001270 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001271 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001272 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001273 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001274 Record = T->getAsCXXRecordDecl();
1275 assert(Record && "qualifier in member expression does not name record");
1276 }
John McCall58cc69d2010-01-27 01:50:18 +00001277 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001278 else {
John McCall58cc69d2010-01-27 01:50:18 +00001279 QualType BaseType = getBaseType().getNonReferenceType();
1280 if (isArrow()) {
1281 const PointerType *PT = BaseType->getAs<PointerType>();
1282 assert(PT && "base of arrow member access is not pointer");
1283 BaseType = PT->getPointeeType();
1284 }
1285
Douglas Gregor9262f472010-04-27 18:19:34 +00001286 Record = BaseType->getAsCXXRecordDecl();
1287 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001288 }
1289
Douglas Gregor9262f472010-04-27 18:19:34 +00001290 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001291}
1292
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001293SubstNonTypeTemplateParmPackExpr::
1294SubstNonTypeTemplateParmPackExpr(QualType T,
1295 NonTypeTemplateParmDecl *Param,
1296 SourceLocation NameLoc,
1297 const TemplateArgument &ArgPack)
1298 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001299 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001300 Param(Param), Arguments(ArgPack.pack_begin()),
1301 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1302
1303TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1304 return TemplateArgument(Arguments, NumArguments);
1305}
1306
Douglas Gregor29c42f22012-02-24 07:38:34 +00001307TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1308 ArrayRef<TypeSourceInfo *> Args,
1309 SourceLocation RParenLoc,
1310 bool Value)
1311 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1312 /*TypeDependent=*/false,
1313 /*ValueDependent=*/false,
1314 /*InstantiationDependent=*/false,
1315 /*ContainsUnexpandedParameterPack=*/false),
1316 Loc(Loc), RParenLoc(RParenLoc)
1317{
1318 TypeTraitExprBits.Kind = Kind;
1319 TypeTraitExprBits.Value = Value;
1320 TypeTraitExprBits.NumArgs = Args.size();
1321
1322 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1323
1324 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1325 if (Args[I]->getType()->isDependentType())
1326 setValueDependent(true);
1327 if (Args[I]->getType()->isInstantiationDependentType())
1328 setInstantiationDependent(true);
1329 if (Args[I]->getType()->containsUnexpandedParameterPack())
1330 setContainsUnexpandedParameterPack(true);
1331
1332 ToArgs[I] = Args[I];
1333 }
1334}
1335
1336TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T,
1337 SourceLocation Loc,
1338 TypeTrait Kind,
1339 ArrayRef<TypeSourceInfo *> Args,
1340 SourceLocation RParenLoc,
1341 bool Value) {
1342 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1343 void *Mem = C.Allocate(Size);
1344 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1345}
1346
1347TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
1348 unsigned NumArgs) {
1349 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1350 void *Mem = C.Allocate(Size);
1351 return new (Mem) TypeTraitExpr(EmptyShell());
1352}
1353
David Blaikie68e081d2011-12-20 02:48:34 +00001354void ArrayTypeTraitExpr::anchor() { }