ExprCXX.h revision 1548d14f4092a817f7d90ad3e7a65266dc85fbc5
1//===--- ExprCXX.h - Classes for representing expressions -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Expr interface and subclasses for C++ expressions. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_EXPRCXX_H 15#define LLVM_CLANG_AST_EXPRCXX_H 16 17#include "clang/AST/Expr.h" 18#include "clang/AST/UnresolvedSet.h" 19#include "clang/AST/TemplateBase.h" 20#include "clang/Basic/ExpressionTraits.h" 21#include "clang/Basic/Lambda.h" 22#include "clang/Basic/TypeTraits.h" 23 24namespace clang { 25 26class CXXConstructorDecl; 27class CXXDestructorDecl; 28class CXXMethodDecl; 29class CXXTemporary; 30class TemplateArgumentListInfo; 31 32//===--------------------------------------------------------------------===// 33// C++ Expressions. 34//===--------------------------------------------------------------------===// 35 36/// \brief A call to an overloaded operator written using operator 37/// syntax. 38/// 39/// Represents a call to an overloaded operator written using operator 40/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 41/// normal call, this AST node provides better information about the 42/// syntactic representation of the call. 43/// 44/// In a C++ template, this expression node kind will be used whenever 45/// any of the arguments are type-dependent. In this case, the 46/// function itself will be a (possibly empty) set of functions and 47/// function templates that were found by name lookup at template 48/// definition time. 49class CXXOperatorCallExpr : public CallExpr { 50 /// \brief The overloaded operator. 51 OverloadedOperatorKind Operator; 52 53public: 54 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn, 55 Expr **args, unsigned numargs, QualType t, 56 ExprValueKind VK, SourceLocation operatorloc) 57 : CallExpr(C, CXXOperatorCallExprClass, fn, 0, args, numargs, t, VK, 58 operatorloc), 59 Operator(Op) {} 60 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) : 61 CallExpr(C, CXXOperatorCallExprClass, Empty) { } 62 63 64 /// getOperator - Returns the kind of overloaded operator that this 65 /// expression refers to. 66 OverloadedOperatorKind getOperator() const { return Operator; } 67 void setOperator(OverloadedOperatorKind Kind) { Operator = Kind; } 68 69 /// getOperatorLoc - Returns the location of the operator symbol in 70 /// the expression. When @c getOperator()==OO_Call, this is the 71 /// location of the right parentheses; when @c 72 /// getOperator()==OO_Subscript, this is the location of the right 73 /// bracket. 74 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 75 76 SourceRange getSourceRange() const; 77 78 static bool classof(const Stmt *T) { 79 return T->getStmtClass() == CXXOperatorCallExprClass; 80 } 81 static bool classof(const CXXOperatorCallExpr *) { return true; } 82}; 83 84/// CXXMemberCallExpr - Represents a call to a member function that 85/// may be written either with member call syntax (e.g., "obj.func()" 86/// or "objptr->func()") or with normal function-call syntax 87/// ("func()") within a member function that ends up calling a member 88/// function. The callee in either case is a MemberExpr that contains 89/// both the object argument and the member function, while the 90/// arguments are the arguments within the parentheses (not including 91/// the object argument). 92class CXXMemberCallExpr : public CallExpr { 93public: 94 CXXMemberCallExpr(ASTContext &C, Expr *fn, Expr **args, unsigned numargs, 95 QualType t, ExprValueKind VK, SourceLocation RP) 96 : CallExpr(C, CXXMemberCallExprClass, fn, 0, args, numargs, t, VK, RP) {} 97 98 CXXMemberCallExpr(ASTContext &C, EmptyShell Empty) 99 : CallExpr(C, CXXMemberCallExprClass, Empty) { } 100 101 /// getImplicitObjectArgument - Retrieves the implicit object 102 /// argument for the member call. For example, in "x.f(5)", this 103 /// operation would return "x". 104 Expr *getImplicitObjectArgument() const; 105 106 /// Retrieves the declaration of the called method. 107 CXXMethodDecl *getMethodDecl() const; 108 109 /// getRecordDecl - Retrieves the CXXRecordDecl for the underlying type of 110 /// the implicit object argument. Note that this is may not be the same 111 /// declaration as that of the class context of the CXXMethodDecl which this 112 /// function is calling. 113 /// FIXME: Returns 0 for member pointer call exprs. 114 CXXRecordDecl *getRecordDecl(); 115 116 static bool classof(const Stmt *T) { 117 return T->getStmtClass() == CXXMemberCallExprClass; 118 } 119 static bool classof(const CXXMemberCallExpr *) { return true; } 120}; 121 122/// CUDAKernelCallExpr - Represents a call to a CUDA kernel function. 123class CUDAKernelCallExpr : public CallExpr { 124private: 125 enum { CONFIG, END_PREARG }; 126 127public: 128 CUDAKernelCallExpr(ASTContext &C, Expr *fn, CallExpr *Config, 129 Expr **args, unsigned numargs, QualType t, 130 ExprValueKind VK, SourceLocation RP) 131 : CallExpr(C, CUDAKernelCallExprClass, fn, END_PREARG, args, numargs, t, VK, 132 RP) { 133 setConfig(Config); 134 } 135 136 CUDAKernelCallExpr(ASTContext &C, EmptyShell Empty) 137 : CallExpr(C, CUDAKernelCallExprClass, END_PREARG, Empty) { } 138 139 const CallExpr *getConfig() const { 140 return cast_or_null<CallExpr>(getPreArg(CONFIG)); 141 } 142 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); } 143 void setConfig(CallExpr *E) { setPreArg(CONFIG, E); } 144 145 static bool classof(const Stmt *T) { 146 return T->getStmtClass() == CUDAKernelCallExprClass; 147 } 148 static bool classof(const CUDAKernelCallExpr *) { return true; } 149}; 150 151/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 152/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 153/// const_cast. 154/// 155/// This abstract class is inherited by all of the classes 156/// representing "named" casts, e.g., CXXStaticCastExpr, 157/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 158class CXXNamedCastExpr : public ExplicitCastExpr { 159private: 160 SourceLocation Loc; // the location of the casting op 161 SourceLocation RParenLoc; // the location of the right parenthesis 162 163protected: 164 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 165 CastKind kind, Expr *op, unsigned PathSize, 166 TypeSourceInfo *writtenTy, SourceLocation l, 167 SourceLocation RParenLoc) 168 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l), 169 RParenLoc(RParenLoc) {} 170 171 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 172 : ExplicitCastExpr(SC, Shell, PathSize) { } 173 174 friend class ASTStmtReader; 175 176public: 177 const char *getCastName() const; 178 179 /// \brief Retrieve the location of the cast operator keyword, e.g., 180 /// "static_cast". 181 SourceLocation getOperatorLoc() const { return Loc; } 182 183 /// \brief Retrieve the location of the closing parenthesis. 184 SourceLocation getRParenLoc() const { return RParenLoc; } 185 186 SourceRange getSourceRange() const { 187 return SourceRange(Loc, RParenLoc); 188 } 189 static bool classof(const Stmt *T) { 190 switch (T->getStmtClass()) { 191 case CXXStaticCastExprClass: 192 case CXXDynamicCastExprClass: 193 case CXXReinterpretCastExprClass: 194 case CXXConstCastExprClass: 195 return true; 196 default: 197 return false; 198 } 199 } 200 static bool classof(const CXXNamedCastExpr *) { return true; } 201}; 202 203/// CXXStaticCastExpr - A C++ @c static_cast expression 204/// (C++ [expr.static.cast]). 205/// 206/// This expression node represents a C++ static cast, e.g., 207/// @c static_cast<int>(1.0). 208class CXXStaticCastExpr : public CXXNamedCastExpr { 209 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op, 210 unsigned pathSize, TypeSourceInfo *writtenTy, 211 SourceLocation l, SourceLocation RParenLoc) 212 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize, 213 writtenTy, l, RParenLoc) {} 214 215 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize) 216 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) { } 217 218public: 219 static CXXStaticCastExpr *Create(ASTContext &Context, QualType T, 220 ExprValueKind VK, CastKind K, Expr *Op, 221 const CXXCastPath *Path, 222 TypeSourceInfo *Written, SourceLocation L, 223 SourceLocation RParenLoc); 224 static CXXStaticCastExpr *CreateEmpty(ASTContext &Context, 225 unsigned PathSize); 226 227 static bool classof(const Stmt *T) { 228 return T->getStmtClass() == CXXStaticCastExprClass; 229 } 230 static bool classof(const CXXStaticCastExpr *) { return true; } 231}; 232 233/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 234/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 235/// determine how to perform the type cast. 236/// 237/// This expression node represents a dynamic cast, e.g., 238/// @c dynamic_cast<Derived*>(BasePtr). 239class CXXDynamicCastExpr : public CXXNamedCastExpr { 240 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind, 241 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy, 242 SourceLocation l, SourceLocation RParenLoc) 243 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize, 244 writtenTy, l, RParenLoc) {} 245 246 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize) 247 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) { } 248 249public: 250 static CXXDynamicCastExpr *Create(ASTContext &Context, QualType T, 251 ExprValueKind VK, CastKind Kind, Expr *Op, 252 const CXXCastPath *Path, 253 TypeSourceInfo *Written, SourceLocation L, 254 SourceLocation RParenLoc); 255 256 static CXXDynamicCastExpr *CreateEmpty(ASTContext &Context, 257 unsigned pathSize); 258 259 bool isAlwaysNull() const; 260 261 static bool classof(const Stmt *T) { 262 return T->getStmtClass() == CXXDynamicCastExprClass; 263 } 264 static bool classof(const CXXDynamicCastExpr *) { return true; } 265}; 266 267/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 268/// [expr.reinterpret.cast]), which provides a differently-typed view 269/// of a value but performs no actual work at run time. 270/// 271/// This expression node represents a reinterpret cast, e.g., 272/// @c reinterpret_cast<int>(VoidPtr). 273class CXXReinterpretCastExpr : public CXXNamedCastExpr { 274 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind, 275 Expr *op, unsigned pathSize, 276 TypeSourceInfo *writtenTy, SourceLocation l, 277 SourceLocation RParenLoc) 278 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op, 279 pathSize, writtenTy, l, RParenLoc) {} 280 281 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize) 282 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) { } 283 284public: 285 static CXXReinterpretCastExpr *Create(ASTContext &Context, QualType T, 286 ExprValueKind VK, CastKind Kind, 287 Expr *Op, const CXXCastPath *Path, 288 TypeSourceInfo *WrittenTy, SourceLocation L, 289 SourceLocation RParenLoc); 290 static CXXReinterpretCastExpr *CreateEmpty(ASTContext &Context, 291 unsigned pathSize); 292 293 static bool classof(const Stmt *T) { 294 return T->getStmtClass() == CXXReinterpretCastExprClass; 295 } 296 static bool classof(const CXXReinterpretCastExpr *) { return true; } 297}; 298 299/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 300/// which can remove type qualifiers but does not change the underlying value. 301/// 302/// This expression node represents a const cast, e.g., 303/// @c const_cast<char*>(PtrToConstChar). 304class CXXConstCastExpr : public CXXNamedCastExpr { 305 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op, 306 TypeSourceInfo *writtenTy, SourceLocation l, 307 SourceLocation RParenLoc) 308 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op, 309 0, writtenTy, l, RParenLoc) {} 310 311 explicit CXXConstCastExpr(EmptyShell Empty) 312 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) { } 313 314public: 315 static CXXConstCastExpr *Create(ASTContext &Context, QualType T, 316 ExprValueKind VK, Expr *Op, 317 TypeSourceInfo *WrittenTy, SourceLocation L, 318 SourceLocation RParenLoc); 319 static CXXConstCastExpr *CreateEmpty(ASTContext &Context); 320 321 static bool classof(const Stmt *T) { 322 return T->getStmtClass() == CXXConstCastExprClass; 323 } 324 static bool classof(const CXXConstCastExpr *) { return true; } 325}; 326 327/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 328/// 329class CXXBoolLiteralExpr : public Expr { 330 bool Value; 331 SourceLocation Loc; 332public: 333 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 334 Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 335 false, false), 336 Value(val), Loc(l) {} 337 338 explicit CXXBoolLiteralExpr(EmptyShell Empty) 339 : Expr(CXXBoolLiteralExprClass, Empty) { } 340 341 bool getValue() const { return Value; } 342 void setValue(bool V) { Value = V; } 343 344 SourceRange getSourceRange() const { return SourceRange(Loc); } 345 346 SourceLocation getLocation() const { return Loc; } 347 void setLocation(SourceLocation L) { Loc = L; } 348 349 static bool classof(const Stmt *T) { 350 return T->getStmtClass() == CXXBoolLiteralExprClass; 351 } 352 static bool classof(const CXXBoolLiteralExpr *) { return true; } 353 354 // Iterators 355 child_range children() { return child_range(); } 356}; 357 358/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal 359class CXXNullPtrLiteralExpr : public Expr { 360 SourceLocation Loc; 361public: 362 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) : 363 Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 364 false, false), 365 Loc(l) {} 366 367 explicit CXXNullPtrLiteralExpr(EmptyShell Empty) 368 : Expr(CXXNullPtrLiteralExprClass, Empty) { } 369 370 SourceRange getSourceRange() const { return SourceRange(Loc); } 371 372 SourceLocation getLocation() const { return Loc; } 373 void setLocation(SourceLocation L) { Loc = L; } 374 375 static bool classof(const Stmt *T) { 376 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 377 } 378 static bool classof(const CXXNullPtrLiteralExpr *) { return true; } 379 380 child_range children() { return child_range(); } 381}; 382 383/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 384/// the type_info that corresponds to the supplied type, or the (possibly 385/// dynamic) type of the supplied expression. 386/// 387/// This represents code like @c typeid(int) or @c typeid(*objPtr) 388class CXXTypeidExpr : public Expr { 389private: 390 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 391 SourceRange Range; 392 393public: 394 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 395 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 396 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 397 false, 398 // typeid is value-dependent if the type or expression are dependent 399 Operand->getType()->isDependentType(), 400 Operand->getType()->isInstantiationDependentType(), 401 Operand->getType()->containsUnexpandedParameterPack()), 402 Operand(Operand), Range(R) { } 403 404 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R) 405 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 406 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 407 false, 408 // typeid is value-dependent if the type or expression are dependent 409 Operand->isTypeDependent() || Operand->isValueDependent(), 410 Operand->isInstantiationDependent(), 411 Operand->containsUnexpandedParameterPack()), 412 Operand(Operand), Range(R) { } 413 414 CXXTypeidExpr(EmptyShell Empty, bool isExpr) 415 : Expr(CXXTypeidExprClass, Empty) { 416 if (isExpr) 417 Operand = (Expr*)0; 418 else 419 Operand = (TypeSourceInfo*)0; 420 } 421 422 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 423 424 /// \brief Retrieves the type operand of this typeid() expression after 425 /// various required adjustments (removing reference types, cv-qualifiers). 426 QualType getTypeOperand() const; 427 428 /// \brief Retrieve source information for the type operand. 429 TypeSourceInfo *getTypeOperandSourceInfo() const { 430 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 431 return Operand.get<TypeSourceInfo *>(); 432 } 433 434 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 435 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 436 Operand = TSI; 437 } 438 439 Expr *getExprOperand() const { 440 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 441 return static_cast<Expr*>(Operand.get<Stmt *>()); 442 } 443 444 void setExprOperand(Expr *E) { 445 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 446 Operand = E; 447 } 448 449 SourceRange getSourceRange() const { return Range; } 450 void setSourceRange(SourceRange R) { Range = R; } 451 452 static bool classof(const Stmt *T) { 453 return T->getStmtClass() == CXXTypeidExprClass; 454 } 455 static bool classof(const CXXTypeidExpr *) { return true; } 456 457 // Iterators 458 child_range children() { 459 if (isTypeOperand()) return child_range(); 460 Stmt **begin = reinterpret_cast<Stmt**>(&Operand); 461 return child_range(begin, begin + 1); 462 } 463}; 464 465/// CXXUuidofExpr - A microsoft C++ @c __uuidof expression, which gets 466/// the _GUID that corresponds to the supplied type or expression. 467/// 468/// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr) 469class CXXUuidofExpr : public Expr { 470private: 471 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 472 SourceRange Range; 473 474public: 475 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 476 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, 477 false, Operand->getType()->isDependentType(), 478 Operand->getType()->isInstantiationDependentType(), 479 Operand->getType()->containsUnexpandedParameterPack()), 480 Operand(Operand), Range(R) { } 481 482 CXXUuidofExpr(QualType Ty, Expr *Operand, SourceRange R) 483 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, 484 false, Operand->isTypeDependent(), 485 Operand->isInstantiationDependent(), 486 Operand->containsUnexpandedParameterPack()), 487 Operand(Operand), Range(R) { } 488 489 CXXUuidofExpr(EmptyShell Empty, bool isExpr) 490 : Expr(CXXUuidofExprClass, Empty) { 491 if (isExpr) 492 Operand = (Expr*)0; 493 else 494 Operand = (TypeSourceInfo*)0; 495 } 496 497 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 498 499 /// \brief Retrieves the type operand of this __uuidof() expression after 500 /// various required adjustments (removing reference types, cv-qualifiers). 501 QualType getTypeOperand() const; 502 503 /// \brief Retrieve source information for the type operand. 504 TypeSourceInfo *getTypeOperandSourceInfo() const { 505 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 506 return Operand.get<TypeSourceInfo *>(); 507 } 508 509 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 510 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 511 Operand = TSI; 512 } 513 514 Expr *getExprOperand() const { 515 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 516 return static_cast<Expr*>(Operand.get<Stmt *>()); 517 } 518 519 void setExprOperand(Expr *E) { 520 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 521 Operand = E; 522 } 523 524 SourceRange getSourceRange() const { return Range; } 525 void setSourceRange(SourceRange R) { Range = R; } 526 527 static bool classof(const Stmt *T) { 528 return T->getStmtClass() == CXXUuidofExprClass; 529 } 530 static bool classof(const CXXUuidofExpr *) { return true; } 531 532 // Iterators 533 child_range children() { 534 if (isTypeOperand()) return child_range(); 535 Stmt **begin = reinterpret_cast<Stmt**>(&Operand); 536 return child_range(begin, begin + 1); 537 } 538}; 539 540/// CXXThisExpr - Represents the "this" expression in C++, which is a 541/// pointer to the object on which the current member function is 542/// executing (C++ [expr.prim]p3). Example: 543/// 544/// @code 545/// class Foo { 546/// public: 547/// void bar(); 548/// void test() { this->bar(); } 549/// }; 550/// @endcode 551class CXXThisExpr : public Expr { 552 SourceLocation Loc; 553 bool Implicit : 1; 554 555public: 556 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit) 557 : Expr(CXXThisExprClass, Type, VK_RValue, OK_Ordinary, 558 // 'this' is type-dependent if the class type of the enclosing 559 // member function is dependent (C++ [temp.dep.expr]p2) 560 Type->isDependentType(), Type->isDependentType(), 561 Type->isInstantiationDependentType(), 562 /*ContainsUnexpandedParameterPack=*/false), 563 Loc(L), Implicit(isImplicit) { } 564 565 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {} 566 567 SourceLocation getLocation() const { return Loc; } 568 void setLocation(SourceLocation L) { Loc = L; } 569 570 SourceRange getSourceRange() const { return SourceRange(Loc); } 571 572 bool isImplicit() const { return Implicit; } 573 void setImplicit(bool I) { Implicit = I; } 574 575 static bool classof(const Stmt *T) { 576 return T->getStmtClass() == CXXThisExprClass; 577 } 578 static bool classof(const CXXThisExpr *) { return true; } 579 580 // Iterators 581 child_range children() { return child_range(); } 582}; 583 584/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 585/// 'throw' and 'throw' assignment-expression. When 586/// assignment-expression isn't present, Op will be null. 587/// 588class CXXThrowExpr : public Expr { 589 Stmt *Op; 590 SourceLocation ThrowLoc; 591 /// \brief Whether the thrown variable (if any) is in scope. 592 unsigned IsThrownVariableInScope : 1; 593 594 friend class ASTStmtReader; 595 596public: 597 // Ty is the void type which is used as the result type of the 598 // exepression. The l is the location of the throw keyword. expr 599 // can by null, if the optional expression to throw isn't present. 600 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l, 601 bool IsThrownVariableInScope) : 602 Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 603 expr && expr->isInstantiationDependent(), 604 expr && expr->containsUnexpandedParameterPack()), 605 Op(expr), ThrowLoc(l), IsThrownVariableInScope(IsThrownVariableInScope) {} 606 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {} 607 608 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 609 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 610 611 SourceLocation getThrowLoc() const { return ThrowLoc; } 612 613 /// \brief Determines whether the variable thrown by this expression (if any!) 614 /// is within the innermost try block. 615 /// 616 /// This information is required to determine whether the NRVO can apply to 617 /// this variable. 618 bool isThrownVariableInScope() const { return IsThrownVariableInScope; } 619 620 SourceRange getSourceRange() const { 621 if (getSubExpr() == 0) 622 return SourceRange(ThrowLoc, ThrowLoc); 623 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 624 } 625 626 static bool classof(const Stmt *T) { 627 return T->getStmtClass() == CXXThrowExprClass; 628 } 629 static bool classof(const CXXThrowExpr *) { return true; } 630 631 // Iterators 632 child_range children() { 633 return child_range(&Op, Op ? &Op+1 : &Op); 634 } 635}; 636 637/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 638/// function call argument that was created from the corresponding 639/// parameter's default argument, when the call did not explicitly 640/// supply arguments for all of the parameters. 641class CXXDefaultArgExpr : public Expr { 642 /// \brief The parameter whose default is being used. 643 /// 644 /// When the bit is set, the subexpression is stored after the 645 /// CXXDefaultArgExpr itself. When the bit is clear, the parameter's 646 /// actual default expression is the subexpression. 647 llvm::PointerIntPair<ParmVarDecl *, 1, bool> Param; 648 649 /// \brief The location where the default argument expression was used. 650 SourceLocation Loc; 651 652 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param) 653 : Expr(SC, 654 param->hasUnparsedDefaultArg() 655 ? param->getType().getNonReferenceType() 656 : param->getDefaultArg()->getType(), 657 param->getDefaultArg()->getValueKind(), 658 param->getDefaultArg()->getObjectKind(), false, false, false, false), 659 Param(param, false), Loc(Loc) { } 660 661 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param, 662 Expr *SubExpr) 663 : Expr(SC, SubExpr->getType(), 664 SubExpr->getValueKind(), SubExpr->getObjectKind(), 665 false, false, false, false), 666 Param(param, true), Loc(Loc) { 667 *reinterpret_cast<Expr **>(this + 1) = SubExpr; 668 } 669 670public: 671 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {} 672 673 674 // Param is the parameter whose default argument is used by this 675 // expression. 676 static CXXDefaultArgExpr *Create(ASTContext &C, SourceLocation Loc, 677 ParmVarDecl *Param) { 678 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param); 679 } 680 681 // Param is the parameter whose default argument is used by this 682 // expression, and SubExpr is the expression that will actually be used. 683 static CXXDefaultArgExpr *Create(ASTContext &C, 684 SourceLocation Loc, 685 ParmVarDecl *Param, 686 Expr *SubExpr); 687 688 // Retrieve the parameter that the argument was created from. 689 const ParmVarDecl *getParam() const { return Param.getPointer(); } 690 ParmVarDecl *getParam() { return Param.getPointer(); } 691 692 // Retrieve the actual argument to the function call. 693 const Expr *getExpr() const { 694 if (Param.getInt()) 695 return *reinterpret_cast<Expr const * const*> (this + 1); 696 return getParam()->getDefaultArg(); 697 } 698 Expr *getExpr() { 699 if (Param.getInt()) 700 return *reinterpret_cast<Expr **> (this + 1); 701 return getParam()->getDefaultArg(); 702 } 703 704 /// \brief Retrieve the location where this default argument was actually 705 /// used. 706 SourceLocation getUsedLocation() const { return Loc; } 707 708 SourceRange getSourceRange() const { 709 // Default argument expressions have no representation in the 710 // source, so they have an empty source range. 711 return SourceRange(); 712 } 713 714 static bool classof(const Stmt *T) { 715 return T->getStmtClass() == CXXDefaultArgExprClass; 716 } 717 static bool classof(const CXXDefaultArgExpr *) { return true; } 718 719 // Iterators 720 child_range children() { return child_range(); } 721 722 friend class ASTStmtReader; 723 friend class ASTStmtWriter; 724}; 725 726/// CXXTemporary - Represents a C++ temporary. 727class CXXTemporary { 728 /// Destructor - The destructor that needs to be called. 729 const CXXDestructorDecl *Destructor; 730 731 CXXTemporary(const CXXDestructorDecl *destructor) 732 : Destructor(destructor) { } 733 734public: 735 static CXXTemporary *Create(ASTContext &C, 736 const CXXDestructorDecl *Destructor); 737 738 const CXXDestructorDecl *getDestructor() const { return Destructor; } 739}; 740 741/// \brief Represents binding an expression to a temporary. 742/// 743/// This ensures the destructor is called for the temporary. It should only be 744/// needed for non-POD, non-trivially destructable class types. For example: 745/// 746/// \code 747/// struct S { 748/// S() { } // User defined constructor makes S non-POD. 749/// ~S() { } // User defined destructor makes it non-trivial. 750/// }; 751/// void test() { 752/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr. 753/// } 754/// \endcode 755class CXXBindTemporaryExpr : public Expr { 756 CXXTemporary *Temp; 757 758 Stmt *SubExpr; 759 760 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr) 761 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), 762 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(), 763 SubExpr->isValueDependent(), 764 SubExpr->isInstantiationDependent(), 765 SubExpr->containsUnexpandedParameterPack()), 766 Temp(temp), SubExpr(SubExpr) { } 767 768public: 769 CXXBindTemporaryExpr(EmptyShell Empty) 770 : Expr(CXXBindTemporaryExprClass, Empty), Temp(0), SubExpr(0) {} 771 772 static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp, 773 Expr* SubExpr); 774 775 CXXTemporary *getTemporary() { return Temp; } 776 const CXXTemporary *getTemporary() const { return Temp; } 777 void setTemporary(CXXTemporary *T) { Temp = T; } 778 779 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 780 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 781 void setSubExpr(Expr *E) { SubExpr = E; } 782 783 SourceRange getSourceRange() const { 784 return SubExpr->getSourceRange(); 785 } 786 787 // Implement isa/cast/dyncast/etc. 788 static bool classof(const Stmt *T) { 789 return T->getStmtClass() == CXXBindTemporaryExprClass; 790 } 791 static bool classof(const CXXBindTemporaryExpr *) { return true; } 792 793 // Iterators 794 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 795}; 796 797/// CXXConstructExpr - Represents a call to a C++ constructor. 798class CXXConstructExpr : public Expr { 799public: 800 enum ConstructionKind { 801 CK_Complete, 802 CK_NonVirtualBase, 803 CK_VirtualBase, 804 CK_Delegating 805 }; 806 807private: 808 CXXConstructorDecl *Constructor; 809 810 SourceLocation Loc; 811 SourceRange ParenRange; 812 unsigned NumArgs : 16; 813 bool Elidable : 1; 814 bool HadMultipleCandidates : 1; 815 bool ListInitialization : 1; 816 bool ZeroInitialization : 1; 817 unsigned ConstructKind : 2; 818 Stmt **Args; 819 820protected: 821 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 822 SourceLocation Loc, 823 CXXConstructorDecl *d, bool elidable, 824 Expr **args, unsigned numargs, 825 bool HadMultipleCandidates, 826 bool ListInitialization, 827 bool ZeroInitialization, 828 ConstructionKind ConstructKind, 829 SourceRange ParenRange); 830 831 /// \brief Construct an empty C++ construction expression. 832 CXXConstructExpr(StmtClass SC, EmptyShell Empty) 833 : Expr(SC, Empty), Constructor(0), NumArgs(0), Elidable(false), 834 HadMultipleCandidates(false), ListInitialization(false), 835 ZeroInitialization(false), ConstructKind(0), Args(0) 836 { } 837 838public: 839 /// \brief Construct an empty C++ construction expression. 840 explicit CXXConstructExpr(EmptyShell Empty) 841 : Expr(CXXConstructExprClass, Empty), Constructor(0), 842 NumArgs(0), Elidable(false), HadMultipleCandidates(false), 843 ListInitialization(false), ZeroInitialization(false), 844 ConstructKind(0), Args(0) 845 { } 846 847 static CXXConstructExpr *Create(ASTContext &C, QualType T, 848 SourceLocation Loc, 849 CXXConstructorDecl *D, bool Elidable, 850 Expr **Args, unsigned NumArgs, 851 bool HadMultipleCandidates, 852 bool ListInitialization, 853 bool ZeroInitialization, 854 ConstructionKind ConstructKind, 855 SourceRange ParenRange); 856 857 CXXConstructorDecl* getConstructor() const { return Constructor; } 858 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 859 860 SourceLocation getLocation() const { return Loc; } 861 void setLocation(SourceLocation Loc) { this->Loc = Loc; } 862 863 /// \brief Whether this construction is elidable. 864 bool isElidable() const { return Elidable; } 865 void setElidable(bool E) { Elidable = E; } 866 867 /// \brief Whether the referred constructor was resolved from 868 /// an overloaded set having size greater than 1. 869 bool hadMultipleCandidates() const { return HadMultipleCandidates; } 870 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; } 871 872 /// \brief Whether this constructor call was written as list-initialization. 873 bool isListInitialization() const { return ListInitialization; } 874 void setListInitialization(bool V) { ListInitialization = V; } 875 876 /// \brief Whether this construction first requires 877 /// zero-initialization before the initializer is called. 878 bool requiresZeroInitialization() const { return ZeroInitialization; } 879 void setRequiresZeroInitialization(bool ZeroInit) { 880 ZeroInitialization = ZeroInit; 881 } 882 883 /// \brief Determines whether this constructor is actually constructing 884 /// a base class (rather than a complete object). 885 ConstructionKind getConstructionKind() const { 886 return (ConstructionKind)ConstructKind; 887 } 888 void setConstructionKind(ConstructionKind CK) { 889 ConstructKind = CK; 890 } 891 892 typedef ExprIterator arg_iterator; 893 typedef ConstExprIterator const_arg_iterator; 894 895 arg_iterator arg_begin() { return Args; } 896 arg_iterator arg_end() { return Args + NumArgs; } 897 const_arg_iterator arg_begin() const { return Args; } 898 const_arg_iterator arg_end() const { return Args + NumArgs; } 899 900 Expr **getArgs() const { return reinterpret_cast<Expr **>(Args); } 901 unsigned getNumArgs() const { return NumArgs; } 902 903 /// getArg - Return the specified argument. 904 Expr *getArg(unsigned Arg) { 905 assert(Arg < NumArgs && "Arg access out of range!"); 906 return cast<Expr>(Args[Arg]); 907 } 908 const Expr *getArg(unsigned Arg) const { 909 assert(Arg < NumArgs && "Arg access out of range!"); 910 return cast<Expr>(Args[Arg]); 911 } 912 913 /// setArg - Set the specified argument. 914 void setArg(unsigned Arg, Expr *ArgExpr) { 915 assert(Arg < NumArgs && "Arg access out of range!"); 916 Args[Arg] = ArgExpr; 917 } 918 919 SourceRange getSourceRange() const; 920 SourceRange getParenRange() const { return ParenRange; } 921 922 static bool classof(const Stmt *T) { 923 return T->getStmtClass() == CXXConstructExprClass || 924 T->getStmtClass() == CXXTemporaryObjectExprClass; 925 } 926 static bool classof(const CXXConstructExpr *) { return true; } 927 928 // Iterators 929 child_range children() { 930 return child_range(&Args[0], &Args[0]+NumArgs); 931 } 932 933 friend class ASTStmtReader; 934}; 935 936/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 937/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 938/// x = int(0.5); 939class CXXFunctionalCastExpr : public ExplicitCastExpr { 940 SourceLocation TyBeginLoc; 941 SourceLocation RParenLoc; 942 943 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK, 944 TypeSourceInfo *writtenTy, 945 SourceLocation tyBeginLoc, CastKind kind, 946 Expr *castExpr, unsigned pathSize, 947 SourceLocation rParenLoc) 948 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, 949 castExpr, pathSize, writtenTy), 950 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 951 952 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize) 953 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) { } 954 955public: 956 static CXXFunctionalCastExpr *Create(ASTContext &Context, QualType T, 957 ExprValueKind VK, 958 TypeSourceInfo *Written, 959 SourceLocation TyBeginLoc, 960 CastKind Kind, Expr *Op, 961 const CXXCastPath *Path, 962 SourceLocation RPLoc); 963 static CXXFunctionalCastExpr *CreateEmpty(ASTContext &Context, 964 unsigned PathSize); 965 966 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 967 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 968 SourceLocation getRParenLoc() const { return RParenLoc; } 969 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 970 971 SourceRange getSourceRange() const { 972 return SourceRange(TyBeginLoc, RParenLoc); 973 } 974 static bool classof(const Stmt *T) { 975 return T->getStmtClass() == CXXFunctionalCastExprClass; 976 } 977 static bool classof(const CXXFunctionalCastExpr *) { return true; } 978}; 979 980/// @brief Represents a C++ functional cast expression that builds a 981/// temporary object. 982/// 983/// This expression type represents a C++ "functional" cast 984/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 985/// constructor to build a temporary object. With N == 1 arguments the 986/// functional cast expression will be represented by CXXFunctionalCastExpr. 987/// Example: 988/// @code 989/// struct X { X(int, float); } 990/// 991/// X create_X() { 992/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 993/// }; 994/// @endcode 995class CXXTemporaryObjectExpr : public CXXConstructExpr { 996 TypeSourceInfo *Type; 997 998public: 999 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 1000 TypeSourceInfo *Type, 1001 Expr **Args,unsigned NumArgs, 1002 SourceRange parenRange, 1003 bool HadMultipleCandidates, 1004 bool ZeroInitialization = false); 1005 explicit CXXTemporaryObjectExpr(EmptyShell Empty) 1006 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty), Type() { } 1007 1008 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 1009 1010 SourceRange getSourceRange() const; 1011 1012 static bool classof(const Stmt *T) { 1013 return T->getStmtClass() == CXXTemporaryObjectExprClass; 1014 } 1015 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 1016 1017 friend class ASTStmtReader; 1018}; 1019 1020/// \brief A C++ lambda expression, which produces a function object 1021/// (of unspecified type) that can be invoked later. 1022/// 1023/// Example: 1024/// \code 1025/// void low_pass_filter(std::vector<double> &values, double cutoff) { 1026/// values.erase(std::remove_if(values.begin(), values.end(), 1027// [=](double value) { return value > cutoff; }); 1028/// } 1029/// \endcode 1030/// 1031/// Lambda expressions can capture local variables, either by copying 1032/// the values of those local variables at the time the function 1033/// object is constructed (not when it is called!) or by holding a 1034/// reference to the local variable. These captures can occur either 1035/// implicitly or can be written explicitly between the square 1036/// brackets ([...]) that start the lambda expression. 1037class LambdaExpr : public Expr { 1038 enum { 1039 /// \brief Flag used by the Capture class to indicate that the given 1040 /// capture was implicit. 1041 Capture_Implicit = 0x01, 1042 1043 /// \brief Flag used by the Capture class to indciate that the 1044 /// given capture was by-copy. 1045 Capture_ByCopy = 0x02 1046 }; 1047 1048 /// \brief The source range that covers the lambda introducer ([...]). 1049 SourceRange IntroducerRange; 1050 1051 /// \brief The number of captures. 1052 unsigned NumCaptures : 16; 1053 1054 /// \brief The default capture kind, which is a value of type 1055 /// LambdaCaptureDefault. 1056 unsigned CaptureDefault : 2; 1057 1058 /// \brief Whether this lambda had an explicit parameter list vs. an 1059 /// implicit (and empty) parameter list. 1060 unsigned ExplicitParams : 1; 1061 1062 /// \brief Whether this lambda had the result type explicitly specified. 1063 unsigned ExplicitResultType : 1; 1064 1065 /// \brief Whether there are any array index variables stored at the end of 1066 /// this lambda expression. 1067 unsigned HasArrayIndexVars : 1; 1068 1069 /// \brief The location of the closing brace ('}') that completes 1070 /// the lambda. 1071 /// 1072 /// The location of the brace is also available by looking up the 1073 /// function call operator in the lambda class. However, it is 1074 /// stored here to improve the performance of getSourceRange(), and 1075 /// to avoid having to deserialize the function call operator from a 1076 /// module file just to determine the source range. 1077 SourceLocation ClosingBrace; 1078 1079 // Note: The capture initializers are stored directly after the lambda 1080 // expression, along with the index variables used to initialize by-copy 1081 // array captures. 1082 1083public: 1084 /// \brief Describes the capture of either a variable or 'this'. 1085 class Capture { 1086 llvm::PointerIntPair<VarDecl *, 2> VarAndBits; 1087 SourceLocation Loc; 1088 SourceLocation EllipsisLoc; 1089 1090 friend class ASTStmtReader; 1091 friend class ASTStmtWriter; 1092 1093 public: 1094 /// \brief Create a new capture. 1095 /// 1096 /// \param Loc The source location associated with this capture. 1097 /// 1098 /// \param Kind The kind of capture (this, byref, bycopy). 1099 /// 1100 /// \param Implicit Whether the capture was implicit or explicit. 1101 /// 1102 /// \param Var The local variable being captured, or null if capturing this. 1103 /// 1104 /// \param EllipsisLoc The location of the ellipsis (...) for a 1105 /// capture that is a pack expansion, or an invalid source 1106 /// location to indicate that this is not a pack expansion. 1107 Capture(SourceLocation Loc, bool Implicit, 1108 LambdaCaptureKind Kind, VarDecl *Var = 0, 1109 SourceLocation EllipsisLoc = SourceLocation()); 1110 1111 /// \brief Determine the kind of capture. 1112 LambdaCaptureKind getCaptureKind() const; 1113 1114 /// \brief Determine whether this capture handles the C++ 'this' 1115 /// pointer. 1116 bool capturesThis() const { return VarAndBits.getPointer() == 0; } 1117 1118 /// \brief Determine whether this capture handles a variable. 1119 bool capturesVariable() const { return VarAndBits.getPointer() != 0; } 1120 1121 /// \brief Retrieve the declaration of the local variable being 1122 /// captured. 1123 /// 1124 /// This operation is only valid if this capture does not capture 1125 /// 'this'. 1126 VarDecl *getCapturedVar() const { 1127 assert(!capturesThis() && "No variable available for 'this' capture"); 1128 return VarAndBits.getPointer(); 1129 } 1130 1131 /// \brief Determine whether this was an implicit capture (not 1132 /// written between the square brackets introducing the lambda). 1133 bool isImplicit() const { return VarAndBits.getInt() & Capture_Implicit; } 1134 1135 /// \brief Determine whether this was an explicit capture, written 1136 /// between the square brackets introducing the lambda. 1137 bool isExplicit() const { return !isImplicit(); } 1138 1139 /// \brief Retrieve the source location of the capture. 1140 /// 1141 /// For an explicit capture, this returns the location of the 1142 /// explicit capture in the source. For an implicit capture, this 1143 /// returns the location at which the variable or 'this' was first 1144 /// used. 1145 SourceLocation getLocation() const { return Loc; } 1146 1147 /// \brief Determine whether this capture is a pack expansion, 1148 /// which captures a function parameter pack. 1149 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 1150 1151 /// \brief Retrieve the location of the ellipsis for a capture 1152 /// that is a pack expansion. 1153 SourceLocation getEllipsisLoc() const { 1154 assert(isPackExpansion() && "No ellipsis location for a non-expansion"); 1155 return EllipsisLoc; 1156 } 1157 }; 1158 1159private: 1160 /// \brief Construct a lambda expression. 1161 LambdaExpr(QualType T, SourceRange IntroducerRange, 1162 LambdaCaptureDefault CaptureDefault, 1163 ArrayRef<Capture> Captures, 1164 bool ExplicitParams, 1165 bool ExplicitResultType, 1166 ArrayRef<Expr *> CaptureInits, 1167 ArrayRef<VarDecl *> ArrayIndexVars, 1168 ArrayRef<unsigned> ArrayIndexStarts, 1169 SourceLocation ClosingBrace); 1170 1171 /// \brief Construct an empty lambda expression. 1172 LambdaExpr(EmptyShell Empty, unsigned NumCaptures, bool HasArrayIndexVars) 1173 : Expr(LambdaExprClass, Empty), 1174 NumCaptures(NumCaptures), CaptureDefault(LCD_None), ExplicitParams(false), 1175 ExplicitResultType(false), HasArrayIndexVars(true) { 1176 getStoredStmts()[NumCaptures] = 0; 1177 } 1178 1179 Stmt **getStoredStmts() const { 1180 return reinterpret_cast<Stmt **>(const_cast<LambdaExpr *>(this) + 1); 1181 } 1182 1183 /// \brief Retrieve the mapping from captures to the first array index 1184 /// variable. 1185 unsigned *getArrayIndexStarts() const { 1186 return reinterpret_cast<unsigned *>(getStoredStmts() + NumCaptures + 1); 1187 } 1188 1189 /// \brief Retrieve the complete set of array-index variables. 1190 VarDecl **getArrayIndexVars() const { 1191 return reinterpret_cast<VarDecl **>( 1192 getArrayIndexStarts() + NumCaptures + 1); 1193 } 1194 1195public: 1196 /// \brief Construct a new lambda expression. 1197 static LambdaExpr *Create(ASTContext &C, 1198 CXXRecordDecl *Class, 1199 SourceRange IntroducerRange, 1200 LambdaCaptureDefault CaptureDefault, 1201 ArrayRef<Capture> Captures, 1202 bool ExplicitParams, 1203 bool ExplicitResultType, 1204 ArrayRef<Expr *> CaptureInits, 1205 ArrayRef<VarDecl *> ArrayIndexVars, 1206 ArrayRef<unsigned> ArrayIndexStarts, 1207 SourceLocation ClosingBrace); 1208 1209 /// \brief Construct a new lambda expression that will be deserialized from 1210 /// an external source. 1211 static LambdaExpr *CreateDeserialized(ASTContext &C, unsigned NumCaptures, 1212 unsigned NumArrayIndexVars); 1213 1214 /// \brief Determine the default capture kind for this lambda. 1215 LambdaCaptureDefault getCaptureDefault() const { 1216 return static_cast<LambdaCaptureDefault>(CaptureDefault); 1217 } 1218 1219 /// \brief An iterator that walks over the captures of the lambda, 1220 /// both implicit and explicit. 1221 typedef const Capture *capture_iterator; 1222 1223 /// \brief Retrieve an iterator pointing to the first lambda capture. 1224 capture_iterator capture_begin() const; 1225 1226 /// \brief Retrieve an iterator pointing past the end of the 1227 /// sequence of lambda captures. 1228 capture_iterator capture_end() const; 1229 1230 /// \brief Determine the number of captures in this lambda. 1231 unsigned capture_size() const { return NumCaptures; } 1232 1233 /// \brief Retrieve an iterator pointing to the first explicit 1234 /// lambda capture. 1235 capture_iterator explicit_capture_begin() const; 1236 1237 /// \brief Retrieve an iterator pointing past the end of the sequence of 1238 /// explicit lambda captures. 1239 capture_iterator explicit_capture_end() const; 1240 1241 /// \brief Retrieve an iterator pointing to the first implicit 1242 /// lambda capture. 1243 capture_iterator implicit_capture_begin() const; 1244 1245 /// \brief Retrieve an iterator pointing past the end of the sequence of 1246 /// implicit lambda captures. 1247 capture_iterator implicit_capture_end() const; 1248 1249 /// \brief Iterator that walks over the capture initialization 1250 /// arguments. 1251 typedef Expr **capture_init_iterator; 1252 1253 /// \brief Retrieve the first initialization argument for this 1254 /// lambda expression (which initializes the first capture field). 1255 capture_init_iterator capture_init_begin() const { 1256 return reinterpret_cast<Expr **>(getStoredStmts()); 1257 } 1258 1259 /// \brief Retrieve the iterator pointing one past the last 1260 /// initialization argument for this lambda expression. 1261 capture_init_iterator capture_init_end() const { 1262 return capture_init_begin() + NumCaptures; 1263 } 1264 1265 /// \brief Retrieve the set of index variables used in the capture 1266 /// initializer of an array captured by copy. 1267 /// 1268 /// \param Iter The iterator that points at the capture initializer for 1269 /// which we are extracting the corresponding index variables. 1270 ArrayRef<VarDecl *> getCaptureInitIndexVars(capture_init_iterator Iter) const; 1271 1272 /// \brief Retrieve the source range covering the lambda introducer, 1273 /// which contains the explicit capture list surrounded by square 1274 /// brackets ([...]). 1275 SourceRange getIntroducerRange() const { return IntroducerRange; } 1276 1277 /// \brief Retrieve the class that corresponds to the lambda, which 1278 /// stores the captures in its fields and provides the various 1279 /// operations permitted on a lambda (copying, calling). 1280 CXXRecordDecl *getLambdaClass() const; 1281 1282 /// \brief Retrieve the function call operator associated with this 1283 /// lambda expression. 1284 CXXMethodDecl *getCallOperator() const; 1285 1286 /// \brief Retrieve the body of the lambda. 1287 CompoundStmt *getBody() const; 1288 1289 /// \brief Determine whether the lambda is mutable, meaning that any 1290 /// captures values can be modified. 1291 bool isMutable() const; 1292 1293 /// \brief Determine whether this lambda has an explicit parameter 1294 /// list vs. an implicit (empty) parameter list. 1295 bool hasExplicitParameters() const { return ExplicitParams; } 1296 1297 /// \brief Whether this lambda had its result type explicitly specified. 1298 bool hasExplicitResultType() const { return ExplicitResultType; } 1299 1300 static bool classof(const Stmt *T) { 1301 return T->getStmtClass() == LambdaExprClass; 1302 } 1303 static bool classof(const LambdaExpr *) { return true; } 1304 1305 SourceRange getSourceRange() const { 1306 return SourceRange(IntroducerRange.getBegin(), ClosingBrace); 1307 } 1308 1309 child_range children() { 1310 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1); 1311 } 1312 1313 friend class ASTStmtReader; 1314 friend class ASTStmtWriter; 1315}; 1316 1317/// CXXScalarValueInitExpr - [C++ 5.2.3p2] 1318/// Expression "T()" which creates a value-initialized rvalue of type 1319/// T, which is a non-class type. 1320/// 1321class CXXScalarValueInitExpr : public Expr { 1322 SourceLocation RParenLoc; 1323 TypeSourceInfo *TypeInfo; 1324 1325 friend class ASTStmtReader; 1326 1327public: 1328 /// \brief Create an explicitly-written scalar-value initialization 1329 /// expression. 1330 CXXScalarValueInitExpr(QualType Type, 1331 TypeSourceInfo *TypeInfo, 1332 SourceLocation rParenLoc ) : 1333 Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, 1334 false, false, Type->isInstantiationDependentType(), false), 1335 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {} 1336 1337 explicit CXXScalarValueInitExpr(EmptyShell Shell) 1338 : Expr(CXXScalarValueInitExprClass, Shell) { } 1339 1340 TypeSourceInfo *getTypeSourceInfo() const { 1341 return TypeInfo; 1342 } 1343 1344 SourceLocation getRParenLoc() const { return RParenLoc; } 1345 1346 SourceRange getSourceRange() const; 1347 1348 static bool classof(const Stmt *T) { 1349 return T->getStmtClass() == CXXScalarValueInitExprClass; 1350 } 1351 static bool classof(const CXXScalarValueInitExpr *) { return true; } 1352 1353 // Iterators 1354 child_range children() { return child_range(); } 1355}; 1356 1357/// CXXNewExpr - A new expression for memory allocation and constructor calls, 1358/// e.g: "new CXXNewExpr(foo)". 1359class CXXNewExpr : public Expr { 1360 // Was the usage ::new, i.e. is the global new to be used? 1361 bool GlobalNew : 1; 1362 // Is there an initializer? If not, built-ins are uninitialized, else they're 1363 // value-initialized. 1364 bool Initializer : 1; 1365 // Do we allocate an array? If so, the first SubExpr is the size expression. 1366 bool Array : 1; 1367 // If this is an array allocation, does the usual deallocation 1368 // function for the allocated type want to know the allocated size? 1369 bool UsualArrayDeleteWantsSize : 1; 1370 // Whether the referred constructor (if any) was resolved from an 1371 // overload set having size greater than 1. 1372 bool HadMultipleCandidates : 1; 1373 // The number of placement new arguments. 1374 unsigned NumPlacementArgs : 13; 1375 // The number of constructor arguments. This may be 1 even for non-class 1376 // types; use the pseudo copy constructor. 1377 unsigned NumConstructorArgs : 14; 1378 // Contains an optional array size expression, any number of optional 1379 // placement arguments, and any number of optional constructor arguments, 1380 // in that order. 1381 Stmt **SubExprs; 1382 // Points to the allocation function used. 1383 FunctionDecl *OperatorNew; 1384 // Points to the deallocation function used in case of error. May be null. 1385 FunctionDecl *OperatorDelete; 1386 // Points to the constructor used. Cannot be null if AllocType is a record; 1387 // it would still point at the default constructor (even an implicit one). 1388 // Must be null for all other types. 1389 CXXConstructorDecl *Constructor; 1390 1391 /// \brief The allocated type-source information, as written in the source. 1392 TypeSourceInfo *AllocatedTypeInfo; 1393 1394 /// \brief If the allocated type was expressed as a parenthesized type-id, 1395 /// the source range covering the parenthesized type-id. 1396 SourceRange TypeIdParens; 1397 1398 SourceLocation StartLoc; 1399 SourceLocation EndLoc; 1400 SourceLocation ConstructorLParen; 1401 SourceLocation ConstructorRParen; 1402 1403 friend class ASTStmtReader; 1404public: 1405 CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 1406 Expr **placementArgs, unsigned numPlaceArgs, 1407 SourceRange TypeIdParens, 1408 Expr *arraySize, CXXConstructorDecl *constructor, bool initializer, 1409 Expr **constructorArgs, unsigned numConsArgs, 1410 bool HadMultipleCandidates, 1411 FunctionDecl *operatorDelete, bool usualArrayDeleteWantsSize, 1412 QualType ty, TypeSourceInfo *AllocatedTypeInfo, 1413 SourceLocation startLoc, SourceLocation endLoc, 1414 SourceLocation constructorLParen, 1415 SourceLocation constructorRParen); 1416 explicit CXXNewExpr(EmptyShell Shell) 1417 : Expr(CXXNewExprClass, Shell), SubExprs(0) { } 1418 1419 void AllocateArgsArray(ASTContext &C, bool isArray, unsigned numPlaceArgs, 1420 unsigned numConsArgs); 1421 1422 QualType getAllocatedType() const { 1423 assert(getType()->isPointerType()); 1424 return getType()->getAs<PointerType>()->getPointeeType(); 1425 } 1426 1427 TypeSourceInfo *getAllocatedTypeSourceInfo() const { 1428 return AllocatedTypeInfo; 1429 } 1430 1431 /// \brief True if the allocation result needs to be null-checked. 1432 /// C++0x [expr.new]p13: 1433 /// If the allocation function returns null, initialization shall 1434 /// not be done, the deallocation function shall not be called, 1435 /// and the value of the new-expression shall be null. 1436 /// An allocation function is not allowed to return null unless it 1437 /// has a non-throwing exception-specification. The '03 rule is 1438 /// identical except that the definition of a non-throwing 1439 /// exception specification is just "is it throw()?". 1440 bool shouldNullCheckAllocation(ASTContext &Ctx) const; 1441 1442 FunctionDecl *getOperatorNew() const { return OperatorNew; } 1443 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; } 1444 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1445 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; } 1446 CXXConstructorDecl *getConstructor() const { return Constructor; } 1447 void setConstructor(CXXConstructorDecl *D) { Constructor = D; } 1448 1449 bool isArray() const { return Array; } 1450 Expr *getArraySize() { 1451 return Array ? cast<Expr>(SubExprs[0]) : 0; 1452 } 1453 const Expr *getArraySize() const { 1454 return Array ? cast<Expr>(SubExprs[0]) : 0; 1455 } 1456 1457 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 1458 Expr **getPlacementArgs() { 1459 return reinterpret_cast<Expr **>(SubExprs + Array); 1460 } 1461 1462 Expr *getPlacementArg(unsigned i) { 1463 assert(i < NumPlacementArgs && "Index out of range"); 1464 return cast<Expr>(SubExprs[Array + i]); 1465 } 1466 const Expr *getPlacementArg(unsigned i) const { 1467 assert(i < NumPlacementArgs && "Index out of range"); 1468 return cast<Expr>(SubExprs[Array + i]); 1469 } 1470 1471 bool isParenTypeId() const { return TypeIdParens.isValid(); } 1472 SourceRange getTypeIdParens() const { return TypeIdParens; } 1473 1474 bool isGlobalNew() const { return GlobalNew; } 1475 bool hasInitializer() const { return Initializer; } 1476 1477 /// Answers whether the usual array deallocation function for the 1478 /// allocated type expects the size of the allocation as a 1479 /// parameter. 1480 bool doesUsualArrayDeleteWantSize() const { 1481 return UsualArrayDeleteWantsSize; 1482 } 1483 1484 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 1485 1486 Expr **getConstructorArgs() { 1487 return reinterpret_cast<Expr **>(SubExprs + Array + NumPlacementArgs); 1488 } 1489 1490 Expr *getConstructorArg(unsigned i) { 1491 assert(i < NumConstructorArgs && "Index out of range"); 1492 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1493 } 1494 const Expr *getConstructorArg(unsigned i) const { 1495 assert(i < NumConstructorArgs && "Index out of range"); 1496 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1497 } 1498 1499 /// \brief Whether the new expression refers a constructor that was 1500 /// resolved from an overloaded set having size greater than 1. 1501 bool hadMultipleCandidates() const { return HadMultipleCandidates; } 1502 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; } 1503 1504 typedef ExprIterator arg_iterator; 1505 typedef ConstExprIterator const_arg_iterator; 1506 1507 arg_iterator placement_arg_begin() { 1508 return SubExprs + Array; 1509 } 1510 arg_iterator placement_arg_end() { 1511 return SubExprs + Array + getNumPlacementArgs(); 1512 } 1513 const_arg_iterator placement_arg_begin() const { 1514 return SubExprs + Array; 1515 } 1516 const_arg_iterator placement_arg_end() const { 1517 return SubExprs + Array + getNumPlacementArgs(); 1518 } 1519 1520 arg_iterator constructor_arg_begin() { 1521 return SubExprs + Array + getNumPlacementArgs(); 1522 } 1523 arg_iterator constructor_arg_end() { 1524 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1525 } 1526 const_arg_iterator constructor_arg_begin() const { 1527 return SubExprs + Array + getNumPlacementArgs(); 1528 } 1529 const_arg_iterator constructor_arg_end() const { 1530 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1531 } 1532 1533 typedef Stmt **raw_arg_iterator; 1534 raw_arg_iterator raw_arg_begin() { return SubExprs; } 1535 raw_arg_iterator raw_arg_end() { 1536 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1537 } 1538 const_arg_iterator raw_arg_begin() const { return SubExprs; } 1539 const_arg_iterator raw_arg_end() const { return constructor_arg_end(); } 1540 1541 SourceLocation getStartLoc() const { return StartLoc; } 1542 SourceLocation getEndLoc() const { return EndLoc; } 1543 1544 SourceLocation getConstructorLParen() const { return ConstructorLParen; } 1545 SourceLocation getConstructorRParen() const { return ConstructorRParen; } 1546 1547 SourceRange getSourceRange() const { 1548 return SourceRange(StartLoc, EndLoc); 1549 } 1550 1551 static bool classof(const Stmt *T) { 1552 return T->getStmtClass() == CXXNewExprClass; 1553 } 1554 static bool classof(const CXXNewExpr *) { return true; } 1555 1556 // Iterators 1557 child_range children() { 1558 return child_range(&SubExprs[0], 1559 &SubExprs[0] + Array + getNumPlacementArgs() 1560 + getNumConstructorArgs()); 1561 } 1562}; 1563 1564/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 1565/// calls, e.g. "delete[] pArray". 1566class CXXDeleteExpr : public Expr { 1567 // Is this a forced global delete, i.e. "::delete"? 1568 bool GlobalDelete : 1; 1569 // Is this the array form of delete, i.e. "delete[]"? 1570 bool ArrayForm : 1; 1571 // ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied 1572 // to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm 1573 // will be true). 1574 bool ArrayFormAsWritten : 1; 1575 // Does the usual deallocation function for the element type require 1576 // a size_t argument? 1577 bool UsualArrayDeleteWantsSize : 1; 1578 // Points to the operator delete overload that is used. Could be a member. 1579 FunctionDecl *OperatorDelete; 1580 // The pointer expression to be deleted. 1581 Stmt *Argument; 1582 // Location of the expression. 1583 SourceLocation Loc; 1584public: 1585 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 1586 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize, 1587 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 1588 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false, 1589 arg->isInstantiationDependent(), 1590 arg->containsUnexpandedParameterPack()), 1591 GlobalDelete(globalDelete), 1592 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten), 1593 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize), 1594 OperatorDelete(operatorDelete), Argument(arg), Loc(loc) { } 1595 explicit CXXDeleteExpr(EmptyShell Shell) 1596 : Expr(CXXDeleteExprClass, Shell), OperatorDelete(0), Argument(0) { } 1597 1598 bool isGlobalDelete() const { return GlobalDelete; } 1599 bool isArrayForm() const { return ArrayForm; } 1600 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; } 1601 1602 /// Answers whether the usual array deallocation function for the 1603 /// allocated type expects the size of the allocation as a 1604 /// parameter. This can be true even if the actual deallocation 1605 /// function that we're using doesn't want a size. 1606 bool doesUsualArrayDeleteWantSize() const { 1607 return UsualArrayDeleteWantsSize; 1608 } 1609 1610 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1611 1612 Expr *getArgument() { return cast<Expr>(Argument); } 1613 const Expr *getArgument() const { return cast<Expr>(Argument); } 1614 1615 /// \brief Retrieve the type being destroyed. If the type being 1616 /// destroyed is a dependent type which may or may not be a pointer, 1617 /// return an invalid type. 1618 QualType getDestroyedType() const; 1619 1620 SourceRange getSourceRange() const { 1621 return SourceRange(Loc, Argument->getLocEnd()); 1622 } 1623 1624 static bool classof(const Stmt *T) { 1625 return T->getStmtClass() == CXXDeleteExprClass; 1626 } 1627 static bool classof(const CXXDeleteExpr *) { return true; } 1628 1629 // Iterators 1630 child_range children() { return child_range(&Argument, &Argument+1); } 1631 1632 friend class ASTStmtReader; 1633}; 1634 1635/// \brief Structure used to store the type being destroyed by a 1636/// pseudo-destructor expression. 1637class PseudoDestructorTypeStorage { 1638 /// \brief Either the type source information or the name of the type, if 1639 /// it couldn't be resolved due to type-dependence. 1640 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 1641 1642 /// \brief The starting source location of the pseudo-destructor type. 1643 SourceLocation Location; 1644 1645public: 1646 PseudoDestructorTypeStorage() { } 1647 1648 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 1649 : Type(II), Location(Loc) { } 1650 1651 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 1652 1653 TypeSourceInfo *getTypeSourceInfo() const { 1654 return Type.dyn_cast<TypeSourceInfo *>(); 1655 } 1656 1657 IdentifierInfo *getIdentifier() const { 1658 return Type.dyn_cast<IdentifierInfo *>(); 1659 } 1660 1661 SourceLocation getLocation() const { return Location; } 1662}; 1663 1664/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 1665/// 1666/// A pseudo-destructor is an expression that looks like a member access to a 1667/// destructor of a scalar type, except that scalar types don't have 1668/// destructors. For example: 1669/// 1670/// \code 1671/// typedef int T; 1672/// void f(int *p) { 1673/// p->T::~T(); 1674/// } 1675/// \endcode 1676/// 1677/// Pseudo-destructors typically occur when instantiating templates such as: 1678/// 1679/// \code 1680/// template<typename T> 1681/// void destroy(T* ptr) { 1682/// ptr->T::~T(); 1683/// } 1684/// \endcode 1685/// 1686/// for scalar types. A pseudo-destructor expression has no run-time semantics 1687/// beyond evaluating the base expression. 1688class CXXPseudoDestructorExpr : public Expr { 1689 /// \brief The base expression (that is being destroyed). 1690 Stmt *Base; 1691 1692 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 1693 /// period ('.'). 1694 bool IsArrow : 1; 1695 1696 /// \brief The location of the '.' or '->' operator. 1697 SourceLocation OperatorLoc; 1698 1699 /// \brief The nested-name-specifier that follows the operator, if present. 1700 NestedNameSpecifierLoc QualifierLoc; 1701 1702 /// \brief The type that precedes the '::' in a qualified pseudo-destructor 1703 /// expression. 1704 TypeSourceInfo *ScopeType; 1705 1706 /// \brief The location of the '::' in a qualified pseudo-destructor 1707 /// expression. 1708 SourceLocation ColonColonLoc; 1709 1710 /// \brief The location of the '~'. 1711 SourceLocation TildeLoc; 1712 1713 /// \brief The type being destroyed, or its name if we were unable to 1714 /// resolve the name. 1715 PseudoDestructorTypeStorage DestroyedType; 1716 1717 friend class ASTStmtReader; 1718 1719public: 1720 CXXPseudoDestructorExpr(ASTContext &Context, 1721 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 1722 NestedNameSpecifierLoc QualifierLoc, 1723 TypeSourceInfo *ScopeType, 1724 SourceLocation ColonColonLoc, 1725 SourceLocation TildeLoc, 1726 PseudoDestructorTypeStorage DestroyedType); 1727 1728 explicit CXXPseudoDestructorExpr(EmptyShell Shell) 1729 : Expr(CXXPseudoDestructorExprClass, Shell), 1730 Base(0), IsArrow(false), QualifierLoc(), ScopeType(0) { } 1731 1732 Expr *getBase() const { return cast<Expr>(Base); } 1733 1734 /// \brief Determines whether this member expression actually had 1735 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1736 /// x->Base::foo. 1737 bool hasQualifier() const { return QualifierLoc; } 1738 1739 /// \brief Retrieves the nested-name-specifier that qualifies the type name, 1740 /// with source-location information. 1741 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 1742 1743 /// \brief If the member name was qualified, retrieves the 1744 /// nested-name-specifier that precedes the member name. Otherwise, returns 1745 /// NULL. 1746 NestedNameSpecifier *getQualifier() const { 1747 return QualifierLoc.getNestedNameSpecifier(); 1748 } 1749 1750 /// \brief Determine whether this pseudo-destructor expression was written 1751 /// using an '->' (otherwise, it used a '.'). 1752 bool isArrow() const { return IsArrow; } 1753 1754 /// \brief Retrieve the location of the '.' or '->' operator. 1755 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1756 1757 /// \brief Retrieve the scope type in a qualified pseudo-destructor 1758 /// expression. 1759 /// 1760 /// Pseudo-destructor expressions can have extra qualification within them 1761 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 1762 /// Here, if the object type of the expression is (or may be) a scalar type, 1763 /// \p T may also be a scalar type and, therefore, cannot be part of a 1764 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 1765 /// destructor expression. 1766 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 1767 1768 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor 1769 /// expression. 1770 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 1771 1772 /// \brief Retrieve the location of the '~'. 1773 SourceLocation getTildeLoc() const { return TildeLoc; } 1774 1775 /// \brief Retrieve the source location information for the type 1776 /// being destroyed. 1777 /// 1778 /// This type-source information is available for non-dependent 1779 /// pseudo-destructor expressions and some dependent pseudo-destructor 1780 /// expressions. Returns NULL if we only have the identifier for a 1781 /// dependent pseudo-destructor expression. 1782 TypeSourceInfo *getDestroyedTypeInfo() const { 1783 return DestroyedType.getTypeSourceInfo(); 1784 } 1785 1786 /// \brief In a dependent pseudo-destructor expression for which we do not 1787 /// have full type information on the destroyed type, provides the name 1788 /// of the destroyed type. 1789 IdentifierInfo *getDestroyedTypeIdentifier() const { 1790 return DestroyedType.getIdentifier(); 1791 } 1792 1793 /// \brief Retrieve the type being destroyed. 1794 QualType getDestroyedType() const; 1795 1796 /// \brief Retrieve the starting location of the type being destroyed. 1797 SourceLocation getDestroyedTypeLoc() const { 1798 return DestroyedType.getLocation(); 1799 } 1800 1801 /// \brief Set the name of destroyed type for a dependent pseudo-destructor 1802 /// expression. 1803 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) { 1804 DestroyedType = PseudoDestructorTypeStorage(II, Loc); 1805 } 1806 1807 /// \brief Set the destroyed type. 1808 void setDestroyedType(TypeSourceInfo *Info) { 1809 DestroyedType = PseudoDestructorTypeStorage(Info); 1810 } 1811 1812 SourceRange getSourceRange() const; 1813 1814 static bool classof(const Stmt *T) { 1815 return T->getStmtClass() == CXXPseudoDestructorExprClass; 1816 } 1817 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 1818 1819 // Iterators 1820 child_range children() { return child_range(&Base, &Base + 1); } 1821}; 1822 1823/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1824/// implementation of TR1/C++0x type trait templates. 1825/// Example: 1826/// __is_pod(int) == true 1827/// __is_enum(std::string) == false 1828class UnaryTypeTraitExpr : public Expr { 1829 /// UTT - The trait. A UnaryTypeTrait enum in MSVC compat unsigned. 1830 unsigned UTT : 31; 1831 /// The value of the type trait. Unspecified if dependent. 1832 bool Value : 1; 1833 1834 /// Loc - The location of the type trait keyword. 1835 SourceLocation Loc; 1836 1837 /// RParen - The location of the closing paren. 1838 SourceLocation RParen; 1839 1840 /// The type being queried. 1841 TypeSourceInfo *QueriedType; 1842 1843public: 1844 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, 1845 TypeSourceInfo *queried, bool value, 1846 SourceLocation rparen, QualType ty) 1847 : Expr(UnaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 1848 false, queried->getType()->isDependentType(), 1849 queried->getType()->isInstantiationDependentType(), 1850 queried->getType()->containsUnexpandedParameterPack()), 1851 UTT(utt), Value(value), Loc(loc), RParen(rparen), QueriedType(queried) { } 1852 1853 explicit UnaryTypeTraitExpr(EmptyShell Empty) 1854 : Expr(UnaryTypeTraitExprClass, Empty), UTT(0), Value(false), 1855 QueriedType() { } 1856 1857 SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1858 1859 UnaryTypeTrait getTrait() const { return static_cast<UnaryTypeTrait>(UTT); } 1860 1861 QualType getQueriedType() const { return QueriedType->getType(); } 1862 1863 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 1864 1865 bool getValue() const { return Value; } 1866 1867 static bool classof(const Stmt *T) { 1868 return T->getStmtClass() == UnaryTypeTraitExprClass; 1869 } 1870 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1871 1872 // Iterators 1873 child_range children() { return child_range(); } 1874 1875 friend class ASTStmtReader; 1876}; 1877 1878/// BinaryTypeTraitExpr - A GCC or MS binary type trait, as used in the 1879/// implementation of TR1/C++0x type trait templates. 1880/// Example: 1881/// __is_base_of(Base, Derived) == true 1882class BinaryTypeTraitExpr : public Expr { 1883 /// BTT - The trait. A BinaryTypeTrait enum in MSVC compat unsigned. 1884 unsigned BTT : 8; 1885 1886 /// The value of the type trait. Unspecified if dependent. 1887 bool Value : 1; 1888 1889 /// Loc - The location of the type trait keyword. 1890 SourceLocation Loc; 1891 1892 /// RParen - The location of the closing paren. 1893 SourceLocation RParen; 1894 1895 /// The lhs type being queried. 1896 TypeSourceInfo *LhsType; 1897 1898 /// The rhs type being queried. 1899 TypeSourceInfo *RhsType; 1900 1901public: 1902 BinaryTypeTraitExpr(SourceLocation loc, BinaryTypeTrait btt, 1903 TypeSourceInfo *lhsType, TypeSourceInfo *rhsType, 1904 bool value, SourceLocation rparen, QualType ty) 1905 : Expr(BinaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, false, 1906 lhsType->getType()->isDependentType() || 1907 rhsType->getType()->isDependentType(), 1908 (lhsType->getType()->isInstantiationDependentType() || 1909 rhsType->getType()->isInstantiationDependentType()), 1910 (lhsType->getType()->containsUnexpandedParameterPack() || 1911 rhsType->getType()->containsUnexpandedParameterPack())), 1912 BTT(btt), Value(value), Loc(loc), RParen(rparen), 1913 LhsType(lhsType), RhsType(rhsType) { } 1914 1915 1916 explicit BinaryTypeTraitExpr(EmptyShell Empty) 1917 : Expr(BinaryTypeTraitExprClass, Empty), BTT(0), Value(false), 1918 LhsType(), RhsType() { } 1919 1920 SourceRange getSourceRange() const { 1921 return SourceRange(Loc, RParen); 1922 } 1923 1924 BinaryTypeTrait getTrait() const { 1925 return static_cast<BinaryTypeTrait>(BTT); 1926 } 1927 1928 QualType getLhsType() const { return LhsType->getType(); } 1929 QualType getRhsType() const { return RhsType->getType(); } 1930 1931 TypeSourceInfo *getLhsTypeSourceInfo() const { return LhsType; } 1932 TypeSourceInfo *getRhsTypeSourceInfo() const { return RhsType; } 1933 1934 bool getValue() const { assert(!isTypeDependent()); return Value; } 1935 1936 static bool classof(const Stmt *T) { 1937 return T->getStmtClass() == BinaryTypeTraitExprClass; 1938 } 1939 static bool classof(const BinaryTypeTraitExpr *) { return true; } 1940 1941 // Iterators 1942 child_range children() { return child_range(); } 1943 1944 friend class ASTStmtReader; 1945}; 1946 1947/// ArrayTypeTraitExpr - An Embarcadero array type trait, as used in the 1948/// implementation of __array_rank and __array_extent. 1949/// Example: 1950/// __array_rank(int[10][20]) == 2 1951/// __array_extent(int, 1) == 20 1952class ArrayTypeTraitExpr : public Expr { 1953 virtual void anchor(); 1954 1955 /// ATT - The trait. An ArrayTypeTrait enum in MSVC compat unsigned. 1956 unsigned ATT : 2; 1957 1958 /// The value of the type trait. Unspecified if dependent. 1959 uint64_t Value; 1960 1961 /// The array dimension being queried, or -1 if not used 1962 Expr *Dimension; 1963 1964 /// Loc - The location of the type trait keyword. 1965 SourceLocation Loc; 1966 1967 /// RParen - The location of the closing paren. 1968 SourceLocation RParen; 1969 1970 /// The type being queried. 1971 TypeSourceInfo *QueriedType; 1972 1973public: 1974 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att, 1975 TypeSourceInfo *queried, uint64_t value, 1976 Expr *dimension, SourceLocation rparen, QualType ty) 1977 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 1978 false, queried->getType()->isDependentType(), 1979 (queried->getType()->isInstantiationDependentType() || 1980 (dimension && dimension->isInstantiationDependent())), 1981 queried->getType()->containsUnexpandedParameterPack()), 1982 ATT(att), Value(value), Dimension(dimension), 1983 Loc(loc), RParen(rparen), QueriedType(queried) { } 1984 1985 1986 explicit ArrayTypeTraitExpr(EmptyShell Empty) 1987 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0), Value(false), 1988 QueriedType() { } 1989 1990 virtual ~ArrayTypeTraitExpr() { } 1991 1992 virtual SourceRange getSourceRange() const { 1993 return SourceRange(Loc, RParen); 1994 } 1995 1996 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); } 1997 1998 QualType getQueriedType() const { return QueriedType->getType(); } 1999 2000 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 2001 2002 uint64_t getValue() const { assert(!isTypeDependent()); return Value; } 2003 2004 Expr *getDimensionExpression() const { return Dimension; } 2005 2006 static bool classof(const Stmt *T) { 2007 return T->getStmtClass() == ArrayTypeTraitExprClass; 2008 } 2009 static bool classof(const ArrayTypeTraitExpr *) { return true; } 2010 2011 // Iterators 2012 child_range children() { return child_range(); } 2013 2014 friend class ASTStmtReader; 2015}; 2016 2017/// ExpressionTraitExpr - An expression trait intrinsic 2018/// Example: 2019/// __is_lvalue_expr(std::cout) == true 2020/// __is_lvalue_expr(1) == false 2021class ExpressionTraitExpr : public Expr { 2022 /// ET - The trait. A ExpressionTrait enum in MSVC compat unsigned. 2023 unsigned ET : 31; 2024 /// The value of the type trait. Unspecified if dependent. 2025 bool Value : 1; 2026 2027 /// Loc - The location of the type trait keyword. 2028 SourceLocation Loc; 2029 2030 /// RParen - The location of the closing paren. 2031 SourceLocation RParen; 2032 2033 Expr* QueriedExpression; 2034public: 2035 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, 2036 Expr *queried, bool value, 2037 SourceLocation rparen, QualType resultType) 2038 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary, 2039 false, // Not type-dependent 2040 // Value-dependent if the argument is type-dependent. 2041 queried->isTypeDependent(), 2042 queried->isInstantiationDependent(), 2043 queried->containsUnexpandedParameterPack()), 2044 ET(et), Value(value), Loc(loc), RParen(rparen), 2045 QueriedExpression(queried) { } 2046 2047 explicit ExpressionTraitExpr(EmptyShell Empty) 2048 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false), 2049 QueriedExpression() { } 2050 2051 SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 2052 2053 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); } 2054 2055 Expr *getQueriedExpression() const { return QueriedExpression; } 2056 2057 bool getValue() const { return Value; } 2058 2059 static bool classof(const Stmt *T) { 2060 return T->getStmtClass() == ExpressionTraitExprClass; 2061 } 2062 static bool classof(const ExpressionTraitExpr *) { return true; } 2063 2064 // Iterators 2065 child_range children() { return child_range(); } 2066 2067 friend class ASTStmtReader; 2068}; 2069 2070 2071/// \brief A reference to an overloaded function set, either an 2072/// \t UnresolvedLookupExpr or an \t UnresolvedMemberExpr. 2073class OverloadExpr : public Expr { 2074 /// The results. These are undesugared, which is to say, they may 2075 /// include UsingShadowDecls. Access is relative to the naming 2076 /// class. 2077 // FIXME: Allocate this data after the OverloadExpr subclass. 2078 DeclAccessPair *Results; 2079 unsigned NumResults; 2080 2081 /// The common name of these declarations. 2082 DeclarationNameInfo NameInfo; 2083 2084 /// \brief The nested-name-specifier that qualifies the name, if any. 2085 NestedNameSpecifierLoc QualifierLoc; 2086 2087protected: 2088 /// \brief Whether the name includes info for explicit template 2089 /// keyword and arguments. 2090 bool HasTemplateKWAndArgsInfo; 2091 2092 /// \brief Return the optional template keyword and arguments info. 2093 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo(); // defined far below. 2094 2095 /// \brief Return the optional template keyword and arguments info. 2096 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2097 return const_cast<OverloadExpr*>(this)->getTemplateKWAndArgsInfo(); 2098 } 2099 2100 OverloadExpr(StmtClass K, ASTContext &C, 2101 NestedNameSpecifierLoc QualifierLoc, 2102 SourceLocation TemplateKWLoc, 2103 const DeclarationNameInfo &NameInfo, 2104 const TemplateArgumentListInfo *TemplateArgs, 2105 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2106 bool KnownDependent, 2107 bool KnownInstantiationDependent, 2108 bool KnownContainsUnexpandedParameterPack); 2109 2110 OverloadExpr(StmtClass K, EmptyShell Empty) 2111 : Expr(K, Empty), Results(0), NumResults(0), 2112 QualifierLoc(), HasTemplateKWAndArgsInfo(false) { } 2113 2114 void initializeResults(ASTContext &C, 2115 UnresolvedSetIterator Begin, 2116 UnresolvedSetIterator End); 2117 2118public: 2119 struct FindResult { 2120 OverloadExpr *Expression; 2121 bool IsAddressOfOperand; 2122 bool HasFormOfMemberPointer; 2123 }; 2124 2125 /// Finds the overloaded expression in the given expression of 2126 /// OverloadTy. 2127 /// 2128 /// \return the expression (which must be there) and true if it has 2129 /// the particular form of a member pointer expression 2130 static FindResult find(Expr *E) { 2131 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 2132 2133 FindResult Result; 2134 2135 E = E->IgnoreParens(); 2136 if (isa<UnaryOperator>(E)) { 2137 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 2138 E = cast<UnaryOperator>(E)->getSubExpr(); 2139 OverloadExpr *Ovl = cast<OverloadExpr>(E->IgnoreParens()); 2140 2141 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier()); 2142 Result.IsAddressOfOperand = true; 2143 Result.Expression = Ovl; 2144 } else { 2145 Result.HasFormOfMemberPointer = false; 2146 Result.IsAddressOfOperand = false; 2147 Result.Expression = cast<OverloadExpr>(E); 2148 } 2149 2150 return Result; 2151 } 2152 2153 /// Gets the naming class of this lookup, if any. 2154 CXXRecordDecl *getNamingClass() const; 2155 2156 typedef UnresolvedSetImpl::iterator decls_iterator; 2157 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); } 2158 decls_iterator decls_end() const { 2159 return UnresolvedSetIterator(Results + NumResults); 2160 } 2161 2162 /// Gets the number of declarations in the unresolved set. 2163 unsigned getNumDecls() const { return NumResults; } 2164 2165 /// Gets the full name info. 2166 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2167 2168 /// Gets the name looked up. 2169 DeclarationName getName() const { return NameInfo.getName(); } 2170 2171 /// Gets the location of the name. 2172 SourceLocation getNameLoc() const { return NameInfo.getLoc(); } 2173 2174 /// Fetches the nested-name qualifier, if one was given. 2175 NestedNameSpecifier *getQualifier() const { 2176 return QualifierLoc.getNestedNameSpecifier(); 2177 } 2178 2179 /// Fetches the nested-name qualifier with source-location information, if 2180 /// one was given. 2181 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2182 2183 /// \brief Retrieve the location of the template keyword preceding 2184 /// this name, if any. 2185 SourceLocation getTemplateKeywordLoc() const { 2186 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2187 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2188 } 2189 2190 /// \brief Retrieve the location of the left angle bracket starting the 2191 /// explicit template argument list following the name, if any. 2192 SourceLocation getLAngleLoc() const { 2193 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2194 return getTemplateKWAndArgsInfo()->LAngleLoc; 2195 } 2196 2197 /// \brief Retrieve the location of the right angle bracket ending the 2198 /// explicit template argument list following the name, if any. 2199 SourceLocation getRAngleLoc() const { 2200 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2201 return getTemplateKWAndArgsInfo()->RAngleLoc; 2202 } 2203 2204 /// Determines whether the name was preceded by the template keyword. 2205 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2206 2207 /// Determines whether this expression had explicit template arguments. 2208 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2209 2210 // Note that, inconsistently with the explicit-template-argument AST 2211 // nodes, users are *forbidden* from calling these methods on objects 2212 // without explicit template arguments. 2213 2214 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2215 assert(hasExplicitTemplateArgs()); 2216 return *getTemplateKWAndArgsInfo(); 2217 } 2218 2219 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2220 return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs(); 2221 } 2222 2223 TemplateArgumentLoc const *getTemplateArgs() const { 2224 return getExplicitTemplateArgs().getTemplateArgs(); 2225 } 2226 2227 unsigned getNumTemplateArgs() const { 2228 return getExplicitTemplateArgs().NumTemplateArgs; 2229 } 2230 2231 /// Copies the template arguments into the given structure. 2232 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2233 getExplicitTemplateArgs().copyInto(List); 2234 } 2235 2236 /// \brief Retrieves the optional explicit template arguments. 2237 /// This points to the same data as getExplicitTemplateArgs(), but 2238 /// returns null if there are no explicit template arguments. 2239 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2240 if (!hasExplicitTemplateArgs()) return 0; 2241 return &getExplicitTemplateArgs(); 2242 } 2243 2244 static bool classof(const Stmt *T) { 2245 return T->getStmtClass() == UnresolvedLookupExprClass || 2246 T->getStmtClass() == UnresolvedMemberExprClass; 2247 } 2248 static bool classof(const OverloadExpr *) { return true; } 2249 2250 friend class ASTStmtReader; 2251 friend class ASTStmtWriter; 2252}; 2253 2254/// \brief A reference to a name which we were able to look up during 2255/// parsing but could not resolve to a specific declaration. This 2256/// arises in several ways: 2257/// * we might be waiting for argument-dependent lookup 2258/// * the name might resolve to an overloaded function 2259/// and eventually: 2260/// * the lookup might have included a function template 2261/// These never include UnresolvedUsingValueDecls, which are always 2262/// class members and therefore appear only in 2263/// UnresolvedMemberLookupExprs. 2264class UnresolvedLookupExpr : public OverloadExpr { 2265 /// True if these lookup results should be extended by 2266 /// argument-dependent lookup if this is the operand of a function 2267 /// call. 2268 bool RequiresADL; 2269 2270 /// True if namespace ::std should be considered an associated namespace 2271 /// for the purposes of argument-dependent lookup. See C++0x [stmt.ranged]p1. 2272 bool StdIsAssociatedNamespace; 2273 2274 /// True if these lookup results are overloaded. This is pretty 2275 /// trivially rederivable if we urgently need to kill this field. 2276 bool Overloaded; 2277 2278 /// The naming class (C++ [class.access.base]p5) of the lookup, if 2279 /// any. This can generally be recalculated from the context chain, 2280 /// but that can be fairly expensive for unqualified lookups. If we 2281 /// want to improve memory use here, this could go in a union 2282 /// against the qualified-lookup bits. 2283 CXXRecordDecl *NamingClass; 2284 2285 UnresolvedLookupExpr(ASTContext &C, 2286 CXXRecordDecl *NamingClass, 2287 NestedNameSpecifierLoc QualifierLoc, 2288 SourceLocation TemplateKWLoc, 2289 const DeclarationNameInfo &NameInfo, 2290 bool RequiresADL, bool Overloaded, 2291 const TemplateArgumentListInfo *TemplateArgs, 2292 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2293 bool StdIsAssociatedNamespace) 2294 : OverloadExpr(UnresolvedLookupExprClass, C, QualifierLoc, TemplateKWLoc, 2295 NameInfo, TemplateArgs, Begin, End, false, false, false), 2296 RequiresADL(RequiresADL), 2297 StdIsAssociatedNamespace(StdIsAssociatedNamespace), 2298 Overloaded(Overloaded), NamingClass(NamingClass) 2299 {} 2300 2301 UnresolvedLookupExpr(EmptyShell Empty) 2302 : OverloadExpr(UnresolvedLookupExprClass, Empty), 2303 RequiresADL(false), StdIsAssociatedNamespace(false), Overloaded(false), 2304 NamingClass(0) 2305 {} 2306 2307 friend class ASTStmtReader; 2308 2309public: 2310 static UnresolvedLookupExpr *Create(ASTContext &C, 2311 CXXRecordDecl *NamingClass, 2312 NestedNameSpecifierLoc QualifierLoc, 2313 const DeclarationNameInfo &NameInfo, 2314 bool ADL, bool Overloaded, 2315 UnresolvedSetIterator Begin, 2316 UnresolvedSetIterator End, 2317 bool StdIsAssociatedNamespace = false) { 2318 assert((ADL || !StdIsAssociatedNamespace) && 2319 "std considered associated namespace when not performing ADL"); 2320 return new(C) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, 2321 SourceLocation(), NameInfo, 2322 ADL, Overloaded, 0, Begin, End, 2323 StdIsAssociatedNamespace); 2324 } 2325 2326 static UnresolvedLookupExpr *Create(ASTContext &C, 2327 CXXRecordDecl *NamingClass, 2328 NestedNameSpecifierLoc QualifierLoc, 2329 SourceLocation TemplateKWLoc, 2330 const DeclarationNameInfo &NameInfo, 2331 bool ADL, 2332 const TemplateArgumentListInfo *Args, 2333 UnresolvedSetIterator Begin, 2334 UnresolvedSetIterator End); 2335 2336 static UnresolvedLookupExpr *CreateEmpty(ASTContext &C, 2337 bool HasTemplateKWAndArgsInfo, 2338 unsigned NumTemplateArgs); 2339 2340 /// True if this declaration should be extended by 2341 /// argument-dependent lookup. 2342 bool requiresADL() const { return RequiresADL; } 2343 2344 /// True if namespace ::std should be artificially added to the set of 2345 /// associated namespaecs for argument-dependent lookup purposes. 2346 bool isStdAssociatedNamespace() const { return StdIsAssociatedNamespace; } 2347 2348 /// True if this lookup is overloaded. 2349 bool isOverloaded() const { return Overloaded; } 2350 2351 /// Gets the 'naming class' (in the sense of C++0x 2352 /// [class.access.base]p5) of the lookup. This is the scope 2353 /// that was looked in to find these results. 2354 CXXRecordDecl *getNamingClass() const { return NamingClass; } 2355 2356 SourceRange getSourceRange() const { 2357 SourceRange Range(getNameInfo().getSourceRange()); 2358 if (getQualifierLoc()) 2359 Range.setBegin(getQualifierLoc().getBeginLoc()); 2360 if (hasExplicitTemplateArgs()) 2361 Range.setEnd(getRAngleLoc()); 2362 return Range; 2363 } 2364 2365 child_range children() { return child_range(); } 2366 2367 static bool classof(const Stmt *T) { 2368 return T->getStmtClass() == UnresolvedLookupExprClass; 2369 } 2370 static bool classof(const UnresolvedLookupExpr *) { return true; } 2371}; 2372 2373/// \brief A qualified reference to a name whose declaration cannot 2374/// yet be resolved. 2375/// 2376/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 2377/// it expresses a reference to a declaration such as 2378/// X<T>::value. The difference, however, is that an 2379/// DependentScopeDeclRefExpr node is used only within C++ templates when 2380/// the qualification (e.g., X<T>::) refers to a dependent type. In 2381/// this case, X<T>::value cannot resolve to a declaration because the 2382/// declaration will differ from on instantiation of X<T> to the 2383/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 2384/// qualifier (X<T>::) and the name of the entity being referenced 2385/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 2386/// declaration can be found. 2387class DependentScopeDeclRefExpr : public Expr { 2388 /// \brief The nested-name-specifier that qualifies this unresolved 2389 /// declaration name. 2390 NestedNameSpecifierLoc QualifierLoc; 2391 2392 /// The name of the entity we will be referencing. 2393 DeclarationNameInfo NameInfo; 2394 2395 /// \brief Whether the name includes info for explicit template 2396 /// keyword and arguments. 2397 bool HasTemplateKWAndArgsInfo; 2398 2399 /// \brief Return the optional template keyword and arguments info. 2400 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2401 if (!HasTemplateKWAndArgsInfo) return 0; 2402 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 2403 } 2404 /// \brief Return the optional template keyword and arguments info. 2405 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2406 return const_cast<DependentScopeDeclRefExpr*>(this) 2407 ->getTemplateKWAndArgsInfo(); 2408 } 2409 2410 DependentScopeDeclRefExpr(QualType T, 2411 NestedNameSpecifierLoc QualifierLoc, 2412 SourceLocation TemplateKWLoc, 2413 const DeclarationNameInfo &NameInfo, 2414 const TemplateArgumentListInfo *Args); 2415 2416public: 2417 static DependentScopeDeclRefExpr *Create(ASTContext &C, 2418 NestedNameSpecifierLoc QualifierLoc, 2419 SourceLocation TemplateKWLoc, 2420 const DeclarationNameInfo &NameInfo, 2421 const TemplateArgumentListInfo *TemplateArgs); 2422 2423 static DependentScopeDeclRefExpr *CreateEmpty(ASTContext &C, 2424 bool HasTemplateKWAndArgsInfo, 2425 unsigned NumTemplateArgs); 2426 2427 /// \brief Retrieve the name that this expression refers to. 2428 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2429 2430 /// \brief Retrieve the name that this expression refers to. 2431 DeclarationName getDeclName() const { return NameInfo.getName(); } 2432 2433 /// \brief Retrieve the location of the name within the expression. 2434 SourceLocation getLocation() const { return NameInfo.getLoc(); } 2435 2436 /// \brief Retrieve the nested-name-specifier that qualifies the 2437 /// name, with source location information. 2438 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2439 2440 2441 /// \brief Retrieve the nested-name-specifier that qualifies this 2442 /// declaration. 2443 NestedNameSpecifier *getQualifier() const { 2444 return QualifierLoc.getNestedNameSpecifier(); 2445 } 2446 2447 /// \brief Retrieve the location of the template keyword preceding 2448 /// this name, if any. 2449 SourceLocation getTemplateKeywordLoc() const { 2450 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2451 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2452 } 2453 2454 /// \brief Retrieve the location of the left angle bracket starting the 2455 /// explicit template argument list following the name, if any. 2456 SourceLocation getLAngleLoc() const { 2457 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2458 return getTemplateKWAndArgsInfo()->LAngleLoc; 2459 } 2460 2461 /// \brief Retrieve the location of the right angle bracket ending the 2462 /// explicit template argument list following the name, if any. 2463 SourceLocation getRAngleLoc() const { 2464 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2465 return getTemplateKWAndArgsInfo()->RAngleLoc; 2466 } 2467 2468 /// Determines whether the name was preceded by the template keyword. 2469 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2470 2471 /// Determines whether this lookup had explicit template arguments. 2472 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2473 2474 // Note that, inconsistently with the explicit-template-argument AST 2475 // nodes, users are *forbidden* from calling these methods on objects 2476 // without explicit template arguments. 2477 2478 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2479 assert(hasExplicitTemplateArgs()); 2480 return *reinterpret_cast<ASTTemplateArgumentListInfo*>(this + 1); 2481 } 2482 2483 /// Gets a reference to the explicit template argument list. 2484 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2485 assert(hasExplicitTemplateArgs()); 2486 return *reinterpret_cast<const ASTTemplateArgumentListInfo*>(this + 1); 2487 } 2488 2489 /// \brief Retrieves the optional explicit template arguments. 2490 /// This points to the same data as getExplicitTemplateArgs(), but 2491 /// returns null if there are no explicit template arguments. 2492 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2493 if (!hasExplicitTemplateArgs()) return 0; 2494 return &getExplicitTemplateArgs(); 2495 } 2496 2497 /// \brief Copies the template arguments (if present) into the given 2498 /// structure. 2499 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2500 getExplicitTemplateArgs().copyInto(List); 2501 } 2502 2503 TemplateArgumentLoc const *getTemplateArgs() const { 2504 return getExplicitTemplateArgs().getTemplateArgs(); 2505 } 2506 2507 unsigned getNumTemplateArgs() const { 2508 return getExplicitTemplateArgs().NumTemplateArgs; 2509 } 2510 2511 SourceRange getSourceRange() const { 2512 SourceRange Range(QualifierLoc.getBeginLoc(), getLocation()); 2513 if (hasExplicitTemplateArgs()) 2514 Range.setEnd(getRAngleLoc()); 2515 return Range; 2516 } 2517 2518 static bool classof(const Stmt *T) { 2519 return T->getStmtClass() == DependentScopeDeclRefExprClass; 2520 } 2521 static bool classof(const DependentScopeDeclRefExpr *) { return true; } 2522 2523 child_range children() { return child_range(); } 2524 2525 friend class ASTStmtReader; 2526 friend class ASTStmtWriter; 2527}; 2528 2529/// Represents an expression --- generally a full-expression --- which 2530/// introduces cleanups to be run at the end of the sub-expression's 2531/// evaluation. The most common source of expression-introduced 2532/// cleanups is temporary objects in C++, but several other kinds of 2533/// expressions can create cleanups, including basically every 2534/// call in ARC that returns an Objective-C pointer. 2535/// 2536/// This expression also tracks whether the sub-expression contains a 2537/// potentially-evaluated block literal. The lifetime of a block 2538/// literal is the extent of the enclosing scope. 2539class ExprWithCleanups : public Expr { 2540public: 2541 /// The type of objects that are kept in the cleanup. 2542 /// It's useful to remember the set of blocks; we could also 2543 /// remember the set of temporaries, but there's currently 2544 /// no need. 2545 typedef BlockDecl *CleanupObject; 2546 2547private: 2548 Stmt *SubExpr; 2549 2550 ExprWithCleanups(EmptyShell, unsigned NumObjects); 2551 ExprWithCleanups(Expr *SubExpr, ArrayRef<CleanupObject> Objects); 2552 2553 CleanupObject *getObjectsBuffer() { 2554 return reinterpret_cast<CleanupObject*>(this + 1); 2555 } 2556 const CleanupObject *getObjectsBuffer() const { 2557 return reinterpret_cast<const CleanupObject*>(this + 1); 2558 } 2559 friend class ASTStmtReader; 2560 2561public: 2562 static ExprWithCleanups *Create(ASTContext &C, EmptyShell empty, 2563 unsigned numObjects); 2564 2565 static ExprWithCleanups *Create(ASTContext &C, Expr *subexpr, 2566 ArrayRef<CleanupObject> objects); 2567 2568 ArrayRef<CleanupObject> getObjects() const { 2569 return ArrayRef<CleanupObject>(getObjectsBuffer(), getNumObjects()); 2570 } 2571 2572 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; } 2573 2574 CleanupObject getObject(unsigned i) const { 2575 assert(i < getNumObjects() && "Index out of range"); 2576 return getObjects()[i]; 2577 } 2578 2579 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 2580 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 2581 2582 /// setSubExpr - As with any mutator of the AST, be very careful 2583 /// when modifying an existing AST to preserve its invariants. 2584 void setSubExpr(Expr *E) { SubExpr = E; } 2585 2586 SourceRange getSourceRange() const { 2587 return SubExpr->getSourceRange(); 2588 } 2589 2590 // Implement isa/cast/dyncast/etc. 2591 static bool classof(const Stmt *T) { 2592 return T->getStmtClass() == ExprWithCleanupsClass; 2593 } 2594 static bool classof(const ExprWithCleanups *) { return true; } 2595 2596 // Iterators 2597 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 2598}; 2599 2600/// \brief Describes an explicit type conversion that uses functional 2601/// notion but could not be resolved because one or more arguments are 2602/// type-dependent. 2603/// 2604/// The explicit type conversions expressed by 2605/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 2606/// where \c T is some type and \c a1, a2, ..., aN are values, and 2607/// either \C T is a dependent type or one or more of the \c a's is 2608/// type-dependent. For example, this would occur in a template such 2609/// as: 2610/// 2611/// \code 2612/// template<typename T, typename A1> 2613/// inline T make_a(const A1& a1) { 2614/// return T(a1); 2615/// } 2616/// \endcode 2617/// 2618/// When the returned expression is instantiated, it may resolve to a 2619/// constructor call, conversion function call, or some kind of type 2620/// conversion. 2621class CXXUnresolvedConstructExpr : public Expr { 2622 /// \brief The type being constructed. 2623 TypeSourceInfo *Type; 2624 2625 /// \brief The location of the left parentheses ('('). 2626 SourceLocation LParenLoc; 2627 2628 /// \brief The location of the right parentheses (')'). 2629 SourceLocation RParenLoc; 2630 2631 /// \brief The number of arguments used to construct the type. 2632 unsigned NumArgs; 2633 2634 CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 2635 SourceLocation LParenLoc, 2636 Expr **Args, 2637 unsigned NumArgs, 2638 SourceLocation RParenLoc); 2639 2640 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) 2641 : Expr(CXXUnresolvedConstructExprClass, Empty), Type(), NumArgs(NumArgs) { } 2642 2643 friend class ASTStmtReader; 2644 2645public: 2646 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 2647 TypeSourceInfo *Type, 2648 SourceLocation LParenLoc, 2649 Expr **Args, 2650 unsigned NumArgs, 2651 SourceLocation RParenLoc); 2652 2653 static CXXUnresolvedConstructExpr *CreateEmpty(ASTContext &C, 2654 unsigned NumArgs); 2655 2656 /// \brief Retrieve the type that is being constructed, as specified 2657 /// in the source code. 2658 QualType getTypeAsWritten() const { return Type->getType(); } 2659 2660 /// \brief Retrieve the type source information for the type being 2661 /// constructed. 2662 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 2663 2664 /// \brief Retrieve the location of the left parentheses ('(') that 2665 /// precedes the argument list. 2666 SourceLocation getLParenLoc() const { return LParenLoc; } 2667 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2668 2669 /// \brief Retrieve the location of the right parentheses (')') that 2670 /// follows the argument list. 2671 SourceLocation getRParenLoc() const { return RParenLoc; } 2672 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2673 2674 /// \brief Retrieve the number of arguments. 2675 unsigned arg_size() const { return NumArgs; } 2676 2677 typedef Expr** arg_iterator; 2678 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 2679 arg_iterator arg_end() { return arg_begin() + NumArgs; } 2680 2681 typedef const Expr* const * const_arg_iterator; 2682 const_arg_iterator arg_begin() const { 2683 return reinterpret_cast<const Expr* const *>(this + 1); 2684 } 2685 const_arg_iterator arg_end() const { 2686 return arg_begin() + NumArgs; 2687 } 2688 2689 Expr *getArg(unsigned I) { 2690 assert(I < NumArgs && "Argument index out-of-range"); 2691 return *(arg_begin() + I); 2692 } 2693 2694 const Expr *getArg(unsigned I) const { 2695 assert(I < NumArgs && "Argument index out-of-range"); 2696 return *(arg_begin() + I); 2697 } 2698 2699 void setArg(unsigned I, Expr *E) { 2700 assert(I < NumArgs && "Argument index out-of-range"); 2701 *(arg_begin() + I) = E; 2702 } 2703 2704 SourceRange getSourceRange() const; 2705 2706 static bool classof(const Stmt *T) { 2707 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 2708 } 2709 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 2710 2711 // Iterators 2712 child_range children() { 2713 Stmt **begin = reinterpret_cast<Stmt**>(this+1); 2714 return child_range(begin, begin + NumArgs); 2715 } 2716}; 2717 2718/// \brief Represents a C++ member access expression where the actual 2719/// member referenced could not be resolved because the base 2720/// expression or the member name was dependent. 2721/// 2722/// Like UnresolvedMemberExprs, these can be either implicit or 2723/// explicit accesses. It is only possible to get one of these with 2724/// an implicit access if a qualifier is provided. 2725class CXXDependentScopeMemberExpr : public Expr { 2726 /// \brief The expression for the base pointer or class reference, 2727 /// e.g., the \c x in x.f. Can be null in implicit accesses. 2728 Stmt *Base; 2729 2730 /// \brief The type of the base expression. Never null, even for 2731 /// implicit accesses. 2732 QualType BaseType; 2733 2734 /// \brief Whether this member expression used the '->' operator or 2735 /// the '.' operator. 2736 bool IsArrow : 1; 2737 2738 /// \brief Whether this member expression has info for explicit template 2739 /// keyword and arguments. 2740 bool HasTemplateKWAndArgsInfo : 1; 2741 2742 /// \brief The location of the '->' or '.' operator. 2743 SourceLocation OperatorLoc; 2744 2745 /// \brief The nested-name-specifier that precedes the member name, if any. 2746 NestedNameSpecifierLoc QualifierLoc; 2747 2748 /// \brief In a qualified member access expression such as t->Base::f, this 2749 /// member stores the resolves of name lookup in the context of the member 2750 /// access expression, to be used at instantiation time. 2751 /// 2752 /// FIXME: This member, along with the QualifierLoc, could 2753 /// be stuck into a structure that is optionally allocated at the end of 2754 /// the CXXDependentScopeMemberExpr, to save space in the common case. 2755 NamedDecl *FirstQualifierFoundInScope; 2756 2757 /// \brief The member to which this member expression refers, which 2758 /// can be name, overloaded operator, or destructor. 2759 /// FIXME: could also be a template-id 2760 DeclarationNameInfo MemberNameInfo; 2761 2762 /// \brief Return the optional template keyword and arguments info. 2763 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2764 if (!HasTemplateKWAndArgsInfo) return 0; 2765 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 2766 } 2767 /// \brief Return the optional template keyword and arguments info. 2768 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2769 return const_cast<CXXDependentScopeMemberExpr*>(this) 2770 ->getTemplateKWAndArgsInfo(); 2771 } 2772 2773 CXXDependentScopeMemberExpr(ASTContext &C, 2774 Expr *Base, QualType BaseType, bool IsArrow, 2775 SourceLocation OperatorLoc, 2776 NestedNameSpecifierLoc QualifierLoc, 2777 SourceLocation TemplateKWLoc, 2778 NamedDecl *FirstQualifierFoundInScope, 2779 DeclarationNameInfo MemberNameInfo, 2780 const TemplateArgumentListInfo *TemplateArgs); 2781 2782public: 2783 CXXDependentScopeMemberExpr(ASTContext &C, 2784 Expr *Base, QualType BaseType, 2785 bool IsArrow, 2786 SourceLocation OperatorLoc, 2787 NestedNameSpecifierLoc QualifierLoc, 2788 NamedDecl *FirstQualifierFoundInScope, 2789 DeclarationNameInfo MemberNameInfo); 2790 2791 static CXXDependentScopeMemberExpr * 2792 Create(ASTContext &C, 2793 Expr *Base, QualType BaseType, bool IsArrow, 2794 SourceLocation OperatorLoc, 2795 NestedNameSpecifierLoc QualifierLoc, 2796 SourceLocation TemplateKWLoc, 2797 NamedDecl *FirstQualifierFoundInScope, 2798 DeclarationNameInfo MemberNameInfo, 2799 const TemplateArgumentListInfo *TemplateArgs); 2800 2801 static CXXDependentScopeMemberExpr * 2802 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 2803 unsigned NumTemplateArgs); 2804 2805 /// \brief True if this is an implicit access, i.e. one in which the 2806 /// member being accessed was not written in the source. The source 2807 /// location of the operator is invalid in this case. 2808 bool isImplicitAccess() const; 2809 2810 /// \brief Retrieve the base object of this member expressions, 2811 /// e.g., the \c x in \c x.m. 2812 Expr *getBase() const { 2813 assert(!isImplicitAccess()); 2814 return cast<Expr>(Base); 2815 } 2816 2817 QualType getBaseType() const { return BaseType; } 2818 2819 /// \brief Determine whether this member expression used the '->' 2820 /// operator; otherwise, it used the '.' operator. 2821 bool isArrow() const { return IsArrow; } 2822 2823 /// \brief Retrieve the location of the '->' or '.' operator. 2824 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2825 2826 /// \brief Retrieve the nested-name-specifier that qualifies the member 2827 /// name. 2828 NestedNameSpecifier *getQualifier() const { 2829 return QualifierLoc.getNestedNameSpecifier(); 2830 } 2831 2832 /// \brief Retrieve the nested-name-specifier that qualifies the member 2833 /// name, with source location information. 2834 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2835 2836 2837 /// \brief Retrieve the first part of the nested-name-specifier that was 2838 /// found in the scope of the member access expression when the member access 2839 /// was initially parsed. 2840 /// 2841 /// This function only returns a useful result when member access expression 2842 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 2843 /// returned by this function describes what was found by unqualified name 2844 /// lookup for the identifier "Base" within the scope of the member access 2845 /// expression itself. At template instantiation time, this information is 2846 /// combined with the results of name lookup into the type of the object 2847 /// expression itself (the class type of x). 2848 NamedDecl *getFirstQualifierFoundInScope() const { 2849 return FirstQualifierFoundInScope; 2850 } 2851 2852 /// \brief Retrieve the name of the member that this expression 2853 /// refers to. 2854 const DeclarationNameInfo &getMemberNameInfo() const { 2855 return MemberNameInfo; 2856 } 2857 2858 /// \brief Retrieve the name of the member that this expression 2859 /// refers to. 2860 DeclarationName getMember() const { return MemberNameInfo.getName(); } 2861 2862 // \brief Retrieve the location of the name of the member that this 2863 // expression refers to. 2864 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); } 2865 2866 /// \brief Retrieve the location of the template keyword preceding the 2867 /// member name, if any. 2868 SourceLocation getTemplateKeywordLoc() const { 2869 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2870 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2871 } 2872 2873 /// \brief Retrieve the location of the left angle bracket starting the 2874 /// explicit template argument list following the member name, if any. 2875 SourceLocation getLAngleLoc() const { 2876 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2877 return getTemplateKWAndArgsInfo()->LAngleLoc; 2878 } 2879 2880 /// \brief Retrieve the location of the right angle bracket ending the 2881 /// explicit template argument list following the member name, if any. 2882 SourceLocation getRAngleLoc() const { 2883 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2884 return getTemplateKWAndArgsInfo()->RAngleLoc; 2885 } 2886 2887 /// Determines whether the member name was preceded by the template keyword. 2888 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2889 2890 /// \brief Determines whether this member expression actually had a C++ 2891 /// template argument list explicitly specified, e.g., x.f<int>. 2892 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2893 2894 /// \brief Retrieve the explicit template argument list that followed the 2895 /// member template name, if any. 2896 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2897 assert(hasExplicitTemplateArgs()); 2898 return *reinterpret_cast<ASTTemplateArgumentListInfo *>(this + 1); 2899 } 2900 2901 /// \brief Retrieve the explicit template argument list that followed the 2902 /// member template name, if any. 2903 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2904 return const_cast<CXXDependentScopeMemberExpr *>(this) 2905 ->getExplicitTemplateArgs(); 2906 } 2907 2908 /// \brief Retrieves the optional explicit template arguments. 2909 /// This points to the same data as getExplicitTemplateArgs(), but 2910 /// returns null if there are no explicit template arguments. 2911 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2912 if (!hasExplicitTemplateArgs()) return 0; 2913 return &getExplicitTemplateArgs(); 2914 } 2915 2916 /// \brief Copies the template arguments (if present) into the given 2917 /// structure. 2918 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2919 getExplicitTemplateArgs().copyInto(List); 2920 } 2921 2922 /// \brief Initializes the template arguments using the given structure. 2923 void initializeTemplateArgumentsFrom(const TemplateArgumentListInfo &List) { 2924 getExplicitTemplateArgs().initializeFrom(List); 2925 } 2926 2927 /// \brief Retrieve the template arguments provided as part of this 2928 /// template-id. 2929 const TemplateArgumentLoc *getTemplateArgs() const { 2930 return getExplicitTemplateArgs().getTemplateArgs(); 2931 } 2932 2933 /// \brief Retrieve the number of template arguments provided as part of this 2934 /// template-id. 2935 unsigned getNumTemplateArgs() const { 2936 return getExplicitTemplateArgs().NumTemplateArgs; 2937 } 2938 2939 SourceRange getSourceRange() const { 2940 SourceRange Range; 2941 if (!isImplicitAccess()) 2942 Range.setBegin(Base->getSourceRange().getBegin()); 2943 else if (getQualifier()) 2944 Range.setBegin(getQualifierLoc().getBeginLoc()); 2945 else 2946 Range.setBegin(MemberNameInfo.getBeginLoc()); 2947 2948 if (hasExplicitTemplateArgs()) 2949 Range.setEnd(getRAngleLoc()); 2950 else 2951 Range.setEnd(MemberNameInfo.getEndLoc()); 2952 return Range; 2953 } 2954 2955 static bool classof(const Stmt *T) { 2956 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 2957 } 2958 static bool classof(const CXXDependentScopeMemberExpr *) { return true; } 2959 2960 // Iterators 2961 child_range children() { 2962 if (isImplicitAccess()) return child_range(); 2963 return child_range(&Base, &Base + 1); 2964 } 2965 2966 friend class ASTStmtReader; 2967 friend class ASTStmtWriter; 2968}; 2969 2970/// \brief Represents a C++ member access expression for which lookup 2971/// produced a set of overloaded functions. 2972/// 2973/// The member access may be explicit or implicit: 2974/// struct A { 2975/// int a, b; 2976/// int explicitAccess() { return this->a + this->A::b; } 2977/// int implicitAccess() { return a + A::b; } 2978/// }; 2979/// 2980/// In the final AST, an explicit access always becomes a MemberExpr. 2981/// An implicit access may become either a MemberExpr or a 2982/// DeclRefExpr, depending on whether the member is static. 2983class UnresolvedMemberExpr : public OverloadExpr { 2984 /// \brief Whether this member expression used the '->' operator or 2985 /// the '.' operator. 2986 bool IsArrow : 1; 2987 2988 /// \brief Whether the lookup results contain an unresolved using 2989 /// declaration. 2990 bool HasUnresolvedUsing : 1; 2991 2992 /// \brief The expression for the base pointer or class reference, 2993 /// e.g., the \c x in x.f. This can be null if this is an 'unbased' 2994 /// member expression 2995 Stmt *Base; 2996 2997 /// \brief The type of the base expression; never null. 2998 QualType BaseType; 2999 3000 /// \brief The location of the '->' or '.' operator. 3001 SourceLocation OperatorLoc; 3002 3003 UnresolvedMemberExpr(ASTContext &C, bool HasUnresolvedUsing, 3004 Expr *Base, QualType BaseType, bool IsArrow, 3005 SourceLocation OperatorLoc, 3006 NestedNameSpecifierLoc QualifierLoc, 3007 SourceLocation TemplateKWLoc, 3008 const DeclarationNameInfo &MemberNameInfo, 3009 const TemplateArgumentListInfo *TemplateArgs, 3010 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3011 3012 UnresolvedMemberExpr(EmptyShell Empty) 3013 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false), 3014 HasUnresolvedUsing(false), Base(0) { } 3015 3016 friend class ASTStmtReader; 3017 3018public: 3019 static UnresolvedMemberExpr * 3020 Create(ASTContext &C, bool HasUnresolvedUsing, 3021 Expr *Base, QualType BaseType, bool IsArrow, 3022 SourceLocation OperatorLoc, 3023 NestedNameSpecifierLoc QualifierLoc, 3024 SourceLocation TemplateKWLoc, 3025 const DeclarationNameInfo &MemberNameInfo, 3026 const TemplateArgumentListInfo *TemplateArgs, 3027 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3028 3029 static UnresolvedMemberExpr * 3030 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 3031 unsigned NumTemplateArgs); 3032 3033 /// \brief True if this is an implicit access, i.e. one in which the 3034 /// member being accessed was not written in the source. The source 3035 /// location of the operator is invalid in this case. 3036 bool isImplicitAccess() const; 3037 3038 /// \brief Retrieve the base object of this member expressions, 3039 /// e.g., the \c x in \c x.m. 3040 Expr *getBase() { 3041 assert(!isImplicitAccess()); 3042 return cast<Expr>(Base); 3043 } 3044 const Expr *getBase() const { 3045 assert(!isImplicitAccess()); 3046 return cast<Expr>(Base); 3047 } 3048 3049 QualType getBaseType() const { return BaseType; } 3050 3051 /// \brief Determine whether the lookup results contain an unresolved using 3052 /// declaration. 3053 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; } 3054 3055 /// \brief Determine whether this member expression used the '->' 3056 /// operator; otherwise, it used the '.' operator. 3057 bool isArrow() const { return IsArrow; } 3058 3059 /// \brief Retrieve the location of the '->' or '.' operator. 3060 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3061 3062 /// \brief Retrieves the naming class of this lookup. 3063 CXXRecordDecl *getNamingClass() const; 3064 3065 /// \brief Retrieve the full name info for the member that this expression 3066 /// refers to. 3067 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); } 3068 3069 /// \brief Retrieve the name of the member that this expression 3070 /// refers to. 3071 DeclarationName getMemberName() const { return getName(); } 3072 3073 // \brief Retrieve the location of the name of the member that this 3074 // expression refers to. 3075 SourceLocation getMemberLoc() const { return getNameLoc(); } 3076 3077 SourceRange getSourceRange() const { 3078 SourceRange Range = getMemberNameInfo().getSourceRange(); 3079 if (!isImplicitAccess()) 3080 Range.setBegin(Base->getSourceRange().getBegin()); 3081 else if (getQualifierLoc()) 3082 Range.setBegin(getQualifierLoc().getBeginLoc()); 3083 3084 if (hasExplicitTemplateArgs()) 3085 Range.setEnd(getRAngleLoc()); 3086 return Range; 3087 } 3088 3089 static bool classof(const Stmt *T) { 3090 return T->getStmtClass() == UnresolvedMemberExprClass; 3091 } 3092 static bool classof(const UnresolvedMemberExpr *) { return true; } 3093 3094 // Iterators 3095 child_range children() { 3096 if (isImplicitAccess()) return child_range(); 3097 return child_range(&Base, &Base + 1); 3098 } 3099}; 3100 3101/// \brief Represents a C++0x noexcept expression (C++ [expr.unary.noexcept]). 3102/// 3103/// The noexcept expression tests whether a given expression might throw. Its 3104/// result is a boolean constant. 3105class CXXNoexceptExpr : public Expr { 3106 bool Value : 1; 3107 Stmt *Operand; 3108 SourceRange Range; 3109 3110 friend class ASTStmtReader; 3111 3112public: 3113 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val, 3114 SourceLocation Keyword, SourceLocation RParen) 3115 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary, 3116 /*TypeDependent*/false, 3117 /*ValueDependent*/Val == CT_Dependent, 3118 Val == CT_Dependent || Operand->isInstantiationDependent(), 3119 Operand->containsUnexpandedParameterPack()), 3120 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen) 3121 { } 3122 3123 CXXNoexceptExpr(EmptyShell Empty) 3124 : Expr(CXXNoexceptExprClass, Empty) 3125 { } 3126 3127 Expr *getOperand() const { return static_cast<Expr*>(Operand); } 3128 3129 SourceRange getSourceRange() const { return Range; } 3130 3131 bool getValue() const { return Value; } 3132 3133 static bool classof(const Stmt *T) { 3134 return T->getStmtClass() == CXXNoexceptExprClass; 3135 } 3136 static bool classof(const CXXNoexceptExpr *) { return true; } 3137 3138 // Iterators 3139 child_range children() { return child_range(&Operand, &Operand + 1); } 3140}; 3141 3142/// \brief Represents a C++0x pack expansion that produces a sequence of 3143/// expressions. 3144/// 3145/// A pack expansion expression contains a pattern (which itself is an 3146/// expression) followed by an ellipsis. For example: 3147/// 3148/// \code 3149/// template<typename F, typename ...Types> 3150/// void forward(F f, Types &&...args) { 3151/// f(static_cast<Types&&>(args)...); 3152/// } 3153/// \endcode 3154/// 3155/// Here, the argument to the function object \c f is a pack expansion whose 3156/// pattern is \c static_cast<Types&&>(args). When the \c forward function 3157/// template is instantiated, the pack expansion will instantiate to zero or 3158/// or more function arguments to the function object \c f. 3159class PackExpansionExpr : public Expr { 3160 SourceLocation EllipsisLoc; 3161 3162 /// \brief The number of expansions that will be produced by this pack 3163 /// expansion expression, if known. 3164 /// 3165 /// When zero, the number of expansions is not known. Otherwise, this value 3166 /// is the number of expansions + 1. 3167 unsigned NumExpansions; 3168 3169 Stmt *Pattern; 3170 3171 friend class ASTStmtReader; 3172 friend class ASTStmtWriter; 3173 3174public: 3175 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc, 3176 llvm::Optional<unsigned> NumExpansions) 3177 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(), 3178 Pattern->getObjectKind(), /*TypeDependent=*/true, 3179 /*ValueDependent=*/true, /*InstantiationDependent=*/true, 3180 /*ContainsUnexpandedParameterPack=*/false), 3181 EllipsisLoc(EllipsisLoc), 3182 NumExpansions(NumExpansions? *NumExpansions + 1 : 0), 3183 Pattern(Pattern) { } 3184 3185 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { } 3186 3187 /// \brief Retrieve the pattern of the pack expansion. 3188 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); } 3189 3190 /// \brief Retrieve the pattern of the pack expansion. 3191 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); } 3192 3193 /// \brief Retrieve the location of the ellipsis that describes this pack 3194 /// expansion. 3195 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 3196 3197 /// \brief Determine the number of expansions that will be produced when 3198 /// this pack expansion is instantiated, if already known. 3199 llvm::Optional<unsigned> getNumExpansions() const { 3200 if (NumExpansions) 3201 return NumExpansions - 1; 3202 3203 return llvm::Optional<unsigned>(); 3204 } 3205 3206 SourceRange getSourceRange() const { 3207 return SourceRange(Pattern->getLocStart(), EllipsisLoc); 3208 } 3209 3210 static bool classof(const Stmt *T) { 3211 return T->getStmtClass() == PackExpansionExprClass; 3212 } 3213 static bool classof(const PackExpansionExpr *) { return true; } 3214 3215 // Iterators 3216 child_range children() { 3217 return child_range(&Pattern, &Pattern + 1); 3218 } 3219}; 3220 3221inline ASTTemplateKWAndArgsInfo *OverloadExpr::getTemplateKWAndArgsInfo() { 3222 if (!HasTemplateKWAndArgsInfo) return 0; 3223 if (isa<UnresolvedLookupExpr>(this)) 3224 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3225 (cast<UnresolvedLookupExpr>(this) + 1); 3226 else 3227 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3228 (cast<UnresolvedMemberExpr>(this) + 1); 3229} 3230 3231/// \brief Represents an expression that computes the length of a parameter 3232/// pack. 3233/// 3234/// \code 3235/// template<typename ...Types> 3236/// struct count { 3237/// static const unsigned value = sizeof...(Types); 3238/// }; 3239/// \endcode 3240class SizeOfPackExpr : public Expr { 3241 /// \brief The location of the 'sizeof' keyword. 3242 SourceLocation OperatorLoc; 3243 3244 /// \brief The location of the name of the parameter pack. 3245 SourceLocation PackLoc; 3246 3247 /// \brief The location of the closing parenthesis. 3248 SourceLocation RParenLoc; 3249 3250 /// \brief The length of the parameter pack, if known. 3251 /// 3252 /// When this expression is value-dependent, the length of the parameter pack 3253 /// is unknown. When this expression is not value-dependent, the length is 3254 /// known. 3255 unsigned Length; 3256 3257 /// \brief The parameter pack itself. 3258 NamedDecl *Pack; 3259 3260 friend class ASTStmtReader; 3261 friend class ASTStmtWriter; 3262 3263public: 3264 /// \brief Creates a value-dependent expression that computes the length of 3265 /// the given parameter pack. 3266 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3267 SourceLocation PackLoc, SourceLocation RParenLoc) 3268 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3269 /*TypeDependent=*/false, /*ValueDependent=*/true, 3270 /*InstantiationDependent=*/true, 3271 /*ContainsUnexpandedParameterPack=*/false), 3272 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3273 Length(0), Pack(Pack) { } 3274 3275 /// \brief Creates an expression that computes the length of 3276 /// the given parameter pack, which is already known. 3277 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3278 SourceLocation PackLoc, SourceLocation RParenLoc, 3279 unsigned Length) 3280 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3281 /*TypeDependent=*/false, /*ValueDependent=*/false, 3282 /*InstantiationDependent=*/false, 3283 /*ContainsUnexpandedParameterPack=*/false), 3284 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3285 Length(Length), Pack(Pack) { } 3286 3287 /// \brief Create an empty expression. 3288 SizeOfPackExpr(EmptyShell Empty) : Expr(SizeOfPackExprClass, Empty) { } 3289 3290 /// \brief Determine the location of the 'sizeof' keyword. 3291 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3292 3293 /// \brief Determine the location of the parameter pack. 3294 SourceLocation getPackLoc() const { return PackLoc; } 3295 3296 /// \brief Determine the location of the right parenthesis. 3297 SourceLocation getRParenLoc() const { return RParenLoc; } 3298 3299 /// \brief Retrieve the parameter pack. 3300 NamedDecl *getPack() const { return Pack; } 3301 3302 /// \brief Retrieve the length of the parameter pack. 3303 /// 3304 /// This routine may only be invoked when the expression is not 3305 /// value-dependent. 3306 unsigned getPackLength() const { 3307 assert(!isValueDependent() && 3308 "Cannot get the length of a value-dependent pack size expression"); 3309 return Length; 3310 } 3311 3312 SourceRange getSourceRange() const { 3313 return SourceRange(OperatorLoc, RParenLoc); 3314 } 3315 3316 static bool classof(const Stmt *T) { 3317 return T->getStmtClass() == SizeOfPackExprClass; 3318 } 3319 static bool classof(const SizeOfPackExpr *) { return true; } 3320 3321 // Iterators 3322 child_range children() { return child_range(); } 3323}; 3324 3325/// \brief Represents a reference to a non-type template parameter 3326/// that has been substituted with a template argument. 3327class SubstNonTypeTemplateParmExpr : public Expr { 3328 /// \brief The replaced parameter. 3329 NonTypeTemplateParmDecl *Param; 3330 3331 /// \brief The replacement expression. 3332 Stmt *Replacement; 3333 3334 /// \brief The location of the non-type template parameter reference. 3335 SourceLocation NameLoc; 3336 3337 friend class ASTReader; 3338 friend class ASTStmtReader; 3339 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty) 3340 : Expr(SubstNonTypeTemplateParmExprClass, Empty) { } 3341 3342public: 3343 SubstNonTypeTemplateParmExpr(QualType type, 3344 ExprValueKind valueKind, 3345 SourceLocation loc, 3346 NonTypeTemplateParmDecl *param, 3347 Expr *replacement) 3348 : Expr(SubstNonTypeTemplateParmExprClass, type, valueKind, OK_Ordinary, 3349 replacement->isTypeDependent(), replacement->isValueDependent(), 3350 replacement->isInstantiationDependent(), 3351 replacement->containsUnexpandedParameterPack()), 3352 Param(param), Replacement(replacement), NameLoc(loc) {} 3353 3354 SourceLocation getNameLoc() const { return NameLoc; } 3355 SourceRange getSourceRange() const { return NameLoc; } 3356 3357 Expr *getReplacement() const { return cast<Expr>(Replacement); } 3358 3359 NonTypeTemplateParmDecl *getParameter() const { return Param; } 3360 3361 static bool classof(const Stmt *s) { 3362 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass; 3363 } 3364 static bool classof(const SubstNonTypeTemplateParmExpr *) { 3365 return true; 3366 } 3367 3368 // Iterators 3369 child_range children() { return child_range(&Replacement, &Replacement+1); } 3370}; 3371 3372/// \brief Represents a reference to a non-type template parameter pack that 3373/// has been substituted with a non-template argument pack. 3374/// 3375/// When a pack expansion in the source code contains multiple parameter packs 3376/// and those parameter packs correspond to different levels of template 3377/// parameter lists, this node node is used to represent a non-type template 3378/// parameter pack from an outer level, which has already had its argument pack 3379/// substituted but that still lives within a pack expansion that itself 3380/// could not be instantiated. When actually performing a substitution into 3381/// that pack expansion (e.g., when all template parameters have corresponding 3382/// arguments), this type will be replaced with the appropriate underlying 3383/// expression at the current pack substitution index. 3384class SubstNonTypeTemplateParmPackExpr : public Expr { 3385 /// \brief The non-type template parameter pack itself. 3386 NonTypeTemplateParmDecl *Param; 3387 3388 /// \brief A pointer to the set of template arguments that this 3389 /// parameter pack is instantiated with. 3390 const TemplateArgument *Arguments; 3391 3392 /// \brief The number of template arguments in \c Arguments. 3393 unsigned NumArguments; 3394 3395 /// \brief The location of the non-type template parameter pack reference. 3396 SourceLocation NameLoc; 3397 3398 friend class ASTReader; 3399 friend class ASTStmtReader; 3400 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty) 3401 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) { } 3402 3403public: 3404 SubstNonTypeTemplateParmPackExpr(QualType T, 3405 NonTypeTemplateParmDecl *Param, 3406 SourceLocation NameLoc, 3407 const TemplateArgument &ArgPack); 3408 3409 /// \brief Retrieve the non-type template parameter pack being substituted. 3410 NonTypeTemplateParmDecl *getParameterPack() const { return Param; } 3411 3412 /// \brief Retrieve the location of the parameter pack name. 3413 SourceLocation getParameterPackLocation() const { return NameLoc; } 3414 3415 /// \brief Retrieve the template argument pack containing the substituted 3416 /// template arguments. 3417 TemplateArgument getArgumentPack() const; 3418 3419 SourceRange getSourceRange() const { return NameLoc; } 3420 3421 static bool classof(const Stmt *T) { 3422 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass; 3423 } 3424 static bool classof(const SubstNonTypeTemplateParmPackExpr *) { 3425 return true; 3426 } 3427 3428 // Iterators 3429 child_range children() { return child_range(); } 3430}; 3431 3432/// \brief Represents a prvalue temporary that written into memory so that 3433/// a reference can bind to it. 3434/// 3435/// Prvalue expressions are materialized when they need to have an address 3436/// in memory for a reference to bind to. This happens when binding a 3437/// reference to the result of a conversion, e.g., 3438/// 3439/// \code 3440/// const int &r = 1.0; 3441/// \endcode 3442/// 3443/// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is 3444/// then materialized via a \c MaterializeTemporaryExpr, and the reference 3445/// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues 3446/// (either an lvalue or an xvalue, depending on the kind of reference binding 3447/// to it), maintaining the invariant that references always bind to glvalues. 3448class MaterializeTemporaryExpr : public Expr { 3449 /// \brief The temporary-generating expression whose value will be 3450 /// materialized. 3451 Stmt *Temporary; 3452 3453 friend class ASTStmtReader; 3454 friend class ASTStmtWriter; 3455 3456public: 3457 MaterializeTemporaryExpr(QualType T, Expr *Temporary, 3458 bool BoundToLvalueReference) 3459 : Expr(MaterializeTemporaryExprClass, T, 3460 BoundToLvalueReference? VK_LValue : VK_XValue, OK_Ordinary, 3461 Temporary->isTypeDependent(), Temporary->isValueDependent(), 3462 Temporary->isInstantiationDependent(), 3463 Temporary->containsUnexpandedParameterPack()), 3464 Temporary(Temporary) { } 3465 3466 MaterializeTemporaryExpr(EmptyShell Empty) 3467 : Expr(MaterializeTemporaryExprClass, Empty) { } 3468 3469 /// \brief Retrieve the temporary-generating subexpression whose value will 3470 /// be materialized into a glvalue. 3471 Expr *GetTemporaryExpr() const { return reinterpret_cast<Expr *>(Temporary); } 3472 3473 /// \brief Determine whether this materialized temporary is bound to an 3474 /// lvalue reference; otherwise, it's bound to an rvalue reference. 3475 bool isBoundToLvalueReference() const { 3476 return getValueKind() == VK_LValue; 3477 } 3478 3479 SourceRange getSourceRange() const { return Temporary->getSourceRange(); } 3480 3481 static bool classof(const Stmt *T) { 3482 return T->getStmtClass() == MaterializeTemporaryExprClass; 3483 } 3484 static bool classof(const MaterializeTemporaryExpr *) { 3485 return true; 3486 } 3487 3488 // Iterators 3489 child_range children() { return child_range(&Temporary, &Temporary + 1); } 3490}; 3491 3492} // end namespace clang 3493 3494#endif 3495