ExprCXX.h revision 01d08018b7cf5ce1601707cfd7a84d22015fc04e
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 ZeroInitialization : 1; 816 unsigned ConstructKind : 2; 817 Stmt **Args; 818 819protected: 820 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 821 SourceLocation Loc, 822 CXXConstructorDecl *d, bool elidable, 823 Expr **args, unsigned numargs, 824 bool HadMultipleCandidates, 825 bool ZeroInitialization = false, 826 ConstructionKind ConstructKind = CK_Complete, 827 SourceRange ParenRange = SourceRange()); 828 829 /// \brief Construct an empty C++ construction expression. 830 CXXConstructExpr(StmtClass SC, EmptyShell Empty) 831 : Expr(SC, Empty), Constructor(0), NumArgs(0), Elidable(0), 832 HadMultipleCandidates(false), ZeroInitialization(0), 833 ConstructKind(0), Args(0) { } 834 835public: 836 /// \brief Construct an empty C++ construction expression. 837 explicit CXXConstructExpr(EmptyShell Empty) 838 : Expr(CXXConstructExprClass, Empty), Constructor(0), 839 NumArgs(0), Elidable(0), HadMultipleCandidates(false), 840 ZeroInitialization(0), ConstructKind(0), Args(0) { } 841 842 static CXXConstructExpr *Create(ASTContext &C, QualType T, 843 SourceLocation Loc, 844 CXXConstructorDecl *D, bool Elidable, 845 Expr **Args, unsigned NumArgs, 846 bool HadMultipleCandidates, 847 bool ZeroInitialization = false, 848 ConstructionKind ConstructKind = CK_Complete, 849 SourceRange ParenRange = SourceRange()); 850 851 852 CXXConstructorDecl* getConstructor() const { return Constructor; } 853 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 854 855 SourceLocation getLocation() const { return Loc; } 856 void setLocation(SourceLocation Loc) { this->Loc = Loc; } 857 858 /// \brief Whether this construction is elidable. 859 bool isElidable() const { return Elidable; } 860 void setElidable(bool E) { Elidable = E; } 861 862 /// \brief Whether the referred constructor was resolved from 863 /// an overloaded set having size greater than 1. 864 bool hadMultipleCandidates() const { return HadMultipleCandidates; } 865 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; } 866 867 /// \brief Whether this construction first requires 868 /// zero-initialization before the initializer is called. 869 bool requiresZeroInitialization() const { return ZeroInitialization; } 870 void setRequiresZeroInitialization(bool ZeroInit) { 871 ZeroInitialization = ZeroInit; 872 } 873 874 /// \brief Determines whether this constructor is actually constructing 875 /// a base class (rather than a complete object). 876 ConstructionKind getConstructionKind() const { 877 return (ConstructionKind)ConstructKind; 878 } 879 void setConstructionKind(ConstructionKind CK) { 880 ConstructKind = CK; 881 } 882 883 typedef ExprIterator arg_iterator; 884 typedef ConstExprIterator const_arg_iterator; 885 886 arg_iterator arg_begin() { return Args; } 887 arg_iterator arg_end() { return Args + NumArgs; } 888 const_arg_iterator arg_begin() const { return Args; } 889 const_arg_iterator arg_end() const { return Args + NumArgs; } 890 891 Expr **getArgs() const { return reinterpret_cast<Expr **>(Args); } 892 unsigned getNumArgs() const { return NumArgs; } 893 894 /// getArg - Return the specified argument. 895 Expr *getArg(unsigned Arg) { 896 assert(Arg < NumArgs && "Arg access out of range!"); 897 return cast<Expr>(Args[Arg]); 898 } 899 const Expr *getArg(unsigned Arg) const { 900 assert(Arg < NumArgs && "Arg access out of range!"); 901 return cast<Expr>(Args[Arg]); 902 } 903 904 /// setArg - Set the specified argument. 905 void setArg(unsigned Arg, Expr *ArgExpr) { 906 assert(Arg < NumArgs && "Arg access out of range!"); 907 Args[Arg] = ArgExpr; 908 } 909 910 SourceRange getSourceRange() const; 911 SourceRange getParenRange() const { return ParenRange; } 912 913 static bool classof(const Stmt *T) { 914 return T->getStmtClass() == CXXConstructExprClass || 915 T->getStmtClass() == CXXTemporaryObjectExprClass; 916 } 917 static bool classof(const CXXConstructExpr *) { return true; } 918 919 // Iterators 920 child_range children() { 921 return child_range(&Args[0], &Args[0]+NumArgs); 922 } 923 924 friend class ASTStmtReader; 925}; 926 927/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 928/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 929/// x = int(0.5); 930class CXXFunctionalCastExpr : public ExplicitCastExpr { 931 SourceLocation TyBeginLoc; 932 SourceLocation RParenLoc; 933 934 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK, 935 TypeSourceInfo *writtenTy, 936 SourceLocation tyBeginLoc, CastKind kind, 937 Expr *castExpr, unsigned pathSize, 938 SourceLocation rParenLoc) 939 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, 940 castExpr, pathSize, writtenTy), 941 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 942 943 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize) 944 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) { } 945 946public: 947 static CXXFunctionalCastExpr *Create(ASTContext &Context, QualType T, 948 ExprValueKind VK, 949 TypeSourceInfo *Written, 950 SourceLocation TyBeginLoc, 951 CastKind Kind, Expr *Op, 952 const CXXCastPath *Path, 953 SourceLocation RPLoc); 954 static CXXFunctionalCastExpr *CreateEmpty(ASTContext &Context, 955 unsigned PathSize); 956 957 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 958 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 959 SourceLocation getRParenLoc() const { return RParenLoc; } 960 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 961 962 SourceRange getSourceRange() const { 963 return SourceRange(TyBeginLoc, RParenLoc); 964 } 965 static bool classof(const Stmt *T) { 966 return T->getStmtClass() == CXXFunctionalCastExprClass; 967 } 968 static bool classof(const CXXFunctionalCastExpr *) { return true; } 969}; 970 971/// @brief Represents a C++ functional cast expression that builds a 972/// temporary object. 973/// 974/// This expression type represents a C++ "functional" cast 975/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 976/// constructor to build a temporary object. With N == 1 arguments the 977/// functional cast expression will be represented by CXXFunctionalCastExpr. 978/// Example: 979/// @code 980/// struct X { X(int, float); } 981/// 982/// X create_X() { 983/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 984/// }; 985/// @endcode 986class CXXTemporaryObjectExpr : public CXXConstructExpr { 987 TypeSourceInfo *Type; 988 989public: 990 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 991 TypeSourceInfo *Type, 992 Expr **Args,unsigned NumArgs, 993 SourceRange parenRange, 994 bool HadMultipleCandidates, 995 bool ZeroInitialization = false); 996 explicit CXXTemporaryObjectExpr(EmptyShell Empty) 997 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty), Type() { } 998 999 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 1000 1001 SourceRange getSourceRange() const; 1002 1003 static bool classof(const Stmt *T) { 1004 return T->getStmtClass() == CXXTemporaryObjectExprClass; 1005 } 1006 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 1007 1008 friend class ASTStmtReader; 1009}; 1010 1011/// \brief A C++ lambda expression, which produces a function object 1012/// (of unspecified type) that can be invoked later. 1013/// 1014/// Example: 1015/// \code 1016/// void low_pass_filter(std::vector<double> &values, double cutoff) { 1017/// values.erase(std::remove_if(values.begin(), values.end(), 1018// [=](double value) { return value > cutoff; }); 1019/// } 1020/// \endcode 1021/// 1022/// Lambda expressions can capture local variables, either by copying 1023/// the values of those local variables at the time the function 1024/// object is constructed (not when it is called!) or by holding a 1025/// reference to the local variable. These captures can occur either 1026/// implicitly or can be written explicitly between the square 1027/// brackets ([...]) that start the lambda expression. 1028class LambdaExpr : public Expr { 1029 enum { 1030 /// \brief Flag used by the Capture class to indicate that the given 1031 /// capture was implicit. 1032 Capture_Implicit = 0x01, 1033 1034 /// \brief Flag used by the Capture class to indciate that the 1035 /// given capture was by-copy. 1036 Capture_ByCopy = 0x02 1037 }; 1038 1039 /// \brief The source range that covers the lambda introducer ([...]). 1040 SourceRange IntroducerRange; 1041 1042 /// \brief The number of captures in this lambda. 1043 unsigned NumCaptures : 16; 1044 1045 /// \brief The number of explicit captures in this lambda. 1046 unsigned NumExplicitCaptures : 13; 1047 1048 /// \brief The default capture kind, which is a value of type 1049 /// LambdaCaptureDefault. 1050 unsigned CaptureDefault : 2; 1051 1052 /// \brief Whether this lambda had an explicit parameter list vs. an 1053 /// implicit (and empty) parameter list. 1054 unsigned ExplicitParams : 1; 1055 1056 /// \brief The location of the closing brace ('}') that completes 1057 /// the lambda. 1058 /// 1059 /// The location of the brace is also available by looking up the 1060 /// function call operator in the lambda class. However, it is 1061 /// stored here to improve the performance of getSourceRange(), and 1062 /// to avoid having to deserialize the function call operator from a 1063 /// module file just to determine the source range. 1064 SourceLocation ClosingBrace; 1065 1066 // Note: The Create method allocates storage after the LambdaExpr 1067 // object, which contains the captures, followed by the capture 1068 // initializers, and finally the body of the lambda. The capture 1069 // initializers and lambda body are placed next to each other so 1070 // that the children() function can visit all of them easily. 1071 1072public: 1073 /// \brief Describes the capture of either a variable or 'this'. 1074 class Capture { 1075 llvm::PointerIntPair<VarDecl *, 2> VarAndBits; 1076 SourceLocation Loc; 1077 SourceLocation EllipsisLoc; 1078 1079 friend class ASTStmtReader; 1080 friend class ASTStmtWriter; 1081 1082 public: 1083 /// \brief Create a new capture. 1084 /// 1085 /// \param Loc The source location associated with this capture. 1086 /// 1087 /// \param Kind The kind of capture (this, byref, bycopy). 1088 /// 1089 /// \param Implicit Whether the capture was implicit or explicit. 1090 /// 1091 /// \param Var The local variable being captured, or null if capturing this. 1092 /// 1093 /// \param EllipsisLoc The location of the ellipsis (...) for a 1094 /// capture that is a pack expansion, or an invalid source 1095 /// location to indicate that this is not a pack expansion. 1096 Capture(SourceLocation Loc, bool Implicit, 1097 LambdaCaptureKind Kind, VarDecl *Var = 0, 1098 SourceLocation EllipsisLoc = SourceLocation()); 1099 1100 /// \brief Determine the kind of capture. 1101 LambdaCaptureKind getCaptureKind() const; 1102 1103 /// \brief Determine whether this capture handles the C++ 'this' 1104 /// pointer. 1105 bool capturesThis() const { return VarAndBits.getPointer() == 0; } 1106 1107 /// \brief Determine whether this capture handles a variable. 1108 bool capturesVariable() const { return VarAndBits.getPointer() != 0; } 1109 1110 /// \brief Retrieve the declaration of the local variable being 1111 /// captured. 1112 /// 1113 /// This operation is only valid if this capture does not capture 1114 /// 'this'. 1115 VarDecl *getCapturedVar() const { 1116 assert(!capturesThis() && "No variable available for 'this' capture"); 1117 return VarAndBits.getPointer(); 1118 } 1119 1120 /// \brief Determine whether this was an implicit capture (not 1121 /// written between the square brackets introducing the lambda). 1122 bool isImplicit() const { return VarAndBits.getInt() & Capture_Implicit; } 1123 1124 /// \brief Determine whether this was an explicit capture, written 1125 /// between the square brackets introducing the lambda. 1126 bool isExplicit() const { return !isImplicit(); } 1127 1128 /// \brief Retrieve the source location of the capture. 1129 /// 1130 /// For an explicit capture, this returns the location of the 1131 /// explicit capture in the source. For an implicit capture, this 1132 /// returns the location at which the variable or 'this' was first 1133 /// used. 1134 SourceLocation getLocation() const { return Loc; } 1135 1136 /// \brief Determine whether this capture is a pack expansion, 1137 /// which captures a function parameter pack. 1138 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 1139 1140 /// \brief Retrieve the location of the ellipsis for a capture 1141 /// that is a pack expansion. 1142 SourceLocation getEllipsisLoc() const { 1143 assert(isPackExpansion() && "No ellipsis location for a non-expansion"); 1144 return EllipsisLoc; 1145 } 1146 }; 1147 1148private: 1149 /// \brief Construct a lambda expression. 1150 LambdaExpr(QualType T, SourceRange IntroducerRange, 1151 LambdaCaptureDefault CaptureDefault, 1152 ArrayRef<Capture> Captures, 1153 bool ExplicitParams, 1154 ArrayRef<Expr *> CaptureInits, 1155 SourceLocation ClosingBrace); 1156 1157 Stmt **getStoredStmts() const { 1158 LambdaExpr *This = const_cast<LambdaExpr *>(this); 1159 return reinterpret_cast<Stmt **>(reinterpret_cast<Capture *>(This + 1) 1160 + NumCaptures); 1161 } 1162 1163public: 1164 /// \brief Construct a new lambda expression. 1165 static LambdaExpr *Create(ASTContext &C, 1166 CXXRecordDecl *Class, 1167 SourceRange IntroducerRange, 1168 LambdaCaptureDefault CaptureDefault, 1169 ArrayRef<Capture> Captures, 1170 bool ExplicitParams, 1171 ArrayRef<Expr *> CaptureInits, 1172 SourceLocation ClosingBrace); 1173 1174 /// \brief Determine the default capture kind for this lambda. 1175 LambdaCaptureDefault getCaptureDefault() const { 1176 return static_cast<LambdaCaptureDefault>(CaptureDefault); 1177 } 1178 1179 /// \brief An iterator that walks over the captures of the lambda, 1180 /// both implicit and explicit. 1181 typedef const Capture *capture_iterator; 1182 1183 /// \brief Retrieve an iterator pointing to the first lambda capture. 1184 capture_iterator capture_begin() const { 1185 return reinterpret_cast<const Capture *>(this + 1); 1186 } 1187 1188 /// \brief Retrieve an iterator pointing past the end of the 1189 /// sequence of lambda captures. 1190 capture_iterator capture_end() const { 1191 return capture_begin() + NumCaptures; 1192 } 1193 1194 /// \brief Retrieve an iterator pointing to the first explicit 1195 /// lambda capture. 1196 capture_iterator explicit_capture_begin() const { 1197 return capture_begin(); 1198 } 1199 1200 /// \brief Retrieve an iterator pointing past the end of the sequence of 1201 /// explicit lambda captures. 1202 capture_iterator explicit_capture_end() const { 1203 return capture_begin() + NumExplicitCaptures; 1204 } 1205 1206 /// \brief Retrieve an iterator pointing to the first implicit 1207 /// lambda capture. 1208 capture_iterator implicit_capture_begin() const { 1209 return explicit_capture_end(); 1210 } 1211 1212 /// \brief Retrieve an iterator pointing past the end of the sequence of 1213 /// implicit lambda captures. 1214 capture_iterator implicit_capture_end() const { 1215 return capture_end(); 1216 } 1217 1218 /// \brief Iterator that walks over the capture initialization 1219 /// arguments. 1220 typedef Expr **capture_init_iterator; 1221 1222 /// \brief Retrieve the first initialization argument for this 1223 /// lambda expression (which initializes the first capture field). 1224 capture_init_iterator capture_init_begin() const { 1225 return reinterpret_cast<Expr **>(getStoredStmts() + 1); 1226 } 1227 1228 /// \brief Retrieve the iterator pointing one past the last 1229 /// initialization argument for this lambda expression (which 1230 /// initializes the first capture field). 1231 capture_init_iterator capture_init_end() const { 1232 return capture_init_begin() + NumCaptures; 1233 } 1234 1235 /// \brief Retrieve the source range covering the lambda introducer, 1236 /// which contains the explicit capture list surrounded by square 1237 /// brackets ([...]). 1238 SourceRange getIntroducerRange() const { return IntroducerRange; } 1239 1240 /// \brief Retrieve the class that corresponds to the lambda, which 1241 /// stores the captures in its fields and provides the various 1242 /// operations permitted on a lambda (copying, calling). 1243 CXXRecordDecl *getLambdaClass() const; 1244 1245 /// \brief Retrieve the function call operator associated with this 1246 /// lambda expression. 1247 CXXMethodDecl *getCallOperator() const; 1248 1249 /// \brief Retrieve the body of the lambda. 1250 CompoundStmt *getBody() const { 1251 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]); 1252 } 1253 1254 /// \brief Determine whether the lambda is mutable, meaning that any 1255 /// captures values can be modified. 1256 bool isMutable() const; 1257 1258 /// \brief Determine whether this lambda has an explicit parameter 1259 /// list vs. an implicit (empty) parameter list. 1260 bool hasExplicitParameters() const { return ExplicitParams; } 1261 1262 static bool classof(const Stmt *T) { 1263 return T->getStmtClass() == LambdaExprClass; 1264 } 1265 static bool classof(const LambdaExpr *) { return true; } 1266 1267 SourceRange getSourceRange() const { 1268 return SourceRange(IntroducerRange.getBegin(), ClosingBrace); 1269 } 1270 1271 child_range children() { 1272 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1); 1273 } 1274 1275 friend class ASTStmtReader; 1276 friend class ASTStmtWriter; 1277}; 1278 1279/// CXXScalarValueInitExpr - [C++ 5.2.3p2] 1280/// Expression "T()" which creates a value-initialized rvalue of type 1281/// T, which is a non-class type. 1282/// 1283class CXXScalarValueInitExpr : public Expr { 1284 SourceLocation RParenLoc; 1285 TypeSourceInfo *TypeInfo; 1286 1287 friend class ASTStmtReader; 1288 1289public: 1290 /// \brief Create an explicitly-written scalar-value initialization 1291 /// expression. 1292 CXXScalarValueInitExpr(QualType Type, 1293 TypeSourceInfo *TypeInfo, 1294 SourceLocation rParenLoc ) : 1295 Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, 1296 false, false, Type->isInstantiationDependentType(), false), 1297 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {} 1298 1299 explicit CXXScalarValueInitExpr(EmptyShell Shell) 1300 : Expr(CXXScalarValueInitExprClass, Shell) { } 1301 1302 TypeSourceInfo *getTypeSourceInfo() const { 1303 return TypeInfo; 1304 } 1305 1306 SourceLocation getRParenLoc() const { return RParenLoc; } 1307 1308 SourceRange getSourceRange() const; 1309 1310 static bool classof(const Stmt *T) { 1311 return T->getStmtClass() == CXXScalarValueInitExprClass; 1312 } 1313 static bool classof(const CXXScalarValueInitExpr *) { return true; } 1314 1315 // Iterators 1316 child_range children() { return child_range(); } 1317}; 1318 1319/// CXXNewExpr - A new expression for memory allocation and constructor calls, 1320/// e.g: "new CXXNewExpr(foo)". 1321class CXXNewExpr : public Expr { 1322 // Was the usage ::new, i.e. is the global new to be used? 1323 bool GlobalNew : 1; 1324 // Is there an initializer? If not, built-ins are uninitialized, else they're 1325 // value-initialized. 1326 bool Initializer : 1; 1327 // Do we allocate an array? If so, the first SubExpr is the size expression. 1328 bool Array : 1; 1329 // If this is an array allocation, does the usual deallocation 1330 // function for the allocated type want to know the allocated size? 1331 bool UsualArrayDeleteWantsSize : 1; 1332 // Whether the referred constructor (if any) was resolved from an 1333 // overload set having size greater than 1. 1334 bool HadMultipleCandidates : 1; 1335 // The number of placement new arguments. 1336 unsigned NumPlacementArgs : 13; 1337 // The number of constructor arguments. This may be 1 even for non-class 1338 // types; use the pseudo copy constructor. 1339 unsigned NumConstructorArgs : 14; 1340 // Contains an optional array size expression, any number of optional 1341 // placement arguments, and any number of optional constructor arguments, 1342 // in that order. 1343 Stmt **SubExprs; 1344 // Points to the allocation function used. 1345 FunctionDecl *OperatorNew; 1346 // Points to the deallocation function used in case of error. May be null. 1347 FunctionDecl *OperatorDelete; 1348 // Points to the constructor used. Cannot be null if AllocType is a record; 1349 // it would still point at the default constructor (even an implicit one). 1350 // Must be null for all other types. 1351 CXXConstructorDecl *Constructor; 1352 1353 /// \brief The allocated type-source information, as written in the source. 1354 TypeSourceInfo *AllocatedTypeInfo; 1355 1356 /// \brief If the allocated type was expressed as a parenthesized type-id, 1357 /// the source range covering the parenthesized type-id. 1358 SourceRange TypeIdParens; 1359 1360 SourceLocation StartLoc; 1361 SourceLocation EndLoc; 1362 SourceLocation ConstructorLParen; 1363 SourceLocation ConstructorRParen; 1364 1365 friend class ASTStmtReader; 1366public: 1367 CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 1368 Expr **placementArgs, unsigned numPlaceArgs, 1369 SourceRange TypeIdParens, 1370 Expr *arraySize, CXXConstructorDecl *constructor, bool initializer, 1371 Expr **constructorArgs, unsigned numConsArgs, 1372 bool HadMultipleCandidates, 1373 FunctionDecl *operatorDelete, bool usualArrayDeleteWantsSize, 1374 QualType ty, TypeSourceInfo *AllocatedTypeInfo, 1375 SourceLocation startLoc, SourceLocation endLoc, 1376 SourceLocation constructorLParen, 1377 SourceLocation constructorRParen); 1378 explicit CXXNewExpr(EmptyShell Shell) 1379 : Expr(CXXNewExprClass, Shell), SubExprs(0) { } 1380 1381 void AllocateArgsArray(ASTContext &C, bool isArray, unsigned numPlaceArgs, 1382 unsigned numConsArgs); 1383 1384 QualType getAllocatedType() const { 1385 assert(getType()->isPointerType()); 1386 return getType()->getAs<PointerType>()->getPointeeType(); 1387 } 1388 1389 TypeSourceInfo *getAllocatedTypeSourceInfo() const { 1390 return AllocatedTypeInfo; 1391 } 1392 1393 /// \brief True if the allocation result needs to be null-checked. 1394 /// C++0x [expr.new]p13: 1395 /// If the allocation function returns null, initialization shall 1396 /// not be done, the deallocation function shall not be called, 1397 /// and the value of the new-expression shall be null. 1398 /// An allocation function is not allowed to return null unless it 1399 /// has a non-throwing exception-specification. The '03 rule is 1400 /// identical except that the definition of a non-throwing 1401 /// exception specification is just "is it throw()?". 1402 bool shouldNullCheckAllocation(ASTContext &Ctx) const; 1403 1404 FunctionDecl *getOperatorNew() const { return OperatorNew; } 1405 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; } 1406 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1407 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; } 1408 CXXConstructorDecl *getConstructor() const { return Constructor; } 1409 void setConstructor(CXXConstructorDecl *D) { Constructor = D; } 1410 1411 bool isArray() const { return Array; } 1412 Expr *getArraySize() { 1413 return Array ? cast<Expr>(SubExprs[0]) : 0; 1414 } 1415 const Expr *getArraySize() const { 1416 return Array ? cast<Expr>(SubExprs[0]) : 0; 1417 } 1418 1419 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 1420 Expr **getPlacementArgs() { 1421 return reinterpret_cast<Expr **>(SubExprs + Array); 1422 } 1423 1424 Expr *getPlacementArg(unsigned i) { 1425 assert(i < NumPlacementArgs && "Index out of range"); 1426 return cast<Expr>(SubExprs[Array + i]); 1427 } 1428 const Expr *getPlacementArg(unsigned i) const { 1429 assert(i < NumPlacementArgs && "Index out of range"); 1430 return cast<Expr>(SubExprs[Array + i]); 1431 } 1432 1433 bool isParenTypeId() const { return TypeIdParens.isValid(); } 1434 SourceRange getTypeIdParens() const { return TypeIdParens; } 1435 1436 bool isGlobalNew() const { return GlobalNew; } 1437 bool hasInitializer() const { return Initializer; } 1438 1439 /// Answers whether the usual array deallocation function for the 1440 /// allocated type expects the size of the allocation as a 1441 /// parameter. 1442 bool doesUsualArrayDeleteWantSize() const { 1443 return UsualArrayDeleteWantsSize; 1444 } 1445 1446 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 1447 1448 Expr **getConstructorArgs() { 1449 return reinterpret_cast<Expr **>(SubExprs + Array + NumPlacementArgs); 1450 } 1451 1452 Expr *getConstructorArg(unsigned i) { 1453 assert(i < NumConstructorArgs && "Index out of range"); 1454 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1455 } 1456 const Expr *getConstructorArg(unsigned i) const { 1457 assert(i < NumConstructorArgs && "Index out of range"); 1458 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1459 } 1460 1461 /// \brief Whether the new expression refers a constructor that was 1462 /// resolved from an overloaded set having size greater than 1. 1463 bool hadMultipleCandidates() const { return HadMultipleCandidates; } 1464 void setHadMultipleCandidates(bool V) { HadMultipleCandidates = V; } 1465 1466 typedef ExprIterator arg_iterator; 1467 typedef ConstExprIterator const_arg_iterator; 1468 1469 arg_iterator placement_arg_begin() { 1470 return SubExprs + Array; 1471 } 1472 arg_iterator placement_arg_end() { 1473 return SubExprs + Array + getNumPlacementArgs(); 1474 } 1475 const_arg_iterator placement_arg_begin() const { 1476 return SubExprs + Array; 1477 } 1478 const_arg_iterator placement_arg_end() const { 1479 return SubExprs + Array + getNumPlacementArgs(); 1480 } 1481 1482 arg_iterator constructor_arg_begin() { 1483 return SubExprs + Array + getNumPlacementArgs(); 1484 } 1485 arg_iterator constructor_arg_end() { 1486 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1487 } 1488 const_arg_iterator constructor_arg_begin() const { 1489 return SubExprs + Array + getNumPlacementArgs(); 1490 } 1491 const_arg_iterator constructor_arg_end() const { 1492 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1493 } 1494 1495 typedef Stmt **raw_arg_iterator; 1496 raw_arg_iterator raw_arg_begin() { return SubExprs; } 1497 raw_arg_iterator raw_arg_end() { 1498 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1499 } 1500 const_arg_iterator raw_arg_begin() const { return SubExprs; } 1501 const_arg_iterator raw_arg_end() const { return constructor_arg_end(); } 1502 1503 SourceLocation getStartLoc() const { return StartLoc; } 1504 SourceLocation getEndLoc() const { return EndLoc; } 1505 1506 SourceLocation getConstructorLParen() const { return ConstructorLParen; } 1507 SourceLocation getConstructorRParen() const { return ConstructorRParen; } 1508 1509 SourceRange getSourceRange() const { 1510 return SourceRange(StartLoc, EndLoc); 1511 } 1512 1513 static bool classof(const Stmt *T) { 1514 return T->getStmtClass() == CXXNewExprClass; 1515 } 1516 static bool classof(const CXXNewExpr *) { return true; } 1517 1518 // Iterators 1519 child_range children() { 1520 return child_range(&SubExprs[0], 1521 &SubExprs[0] + Array + getNumPlacementArgs() 1522 + getNumConstructorArgs()); 1523 } 1524}; 1525 1526/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 1527/// calls, e.g. "delete[] pArray". 1528class CXXDeleteExpr : public Expr { 1529 // Is this a forced global delete, i.e. "::delete"? 1530 bool GlobalDelete : 1; 1531 // Is this the array form of delete, i.e. "delete[]"? 1532 bool ArrayForm : 1; 1533 // ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied 1534 // to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm 1535 // will be true). 1536 bool ArrayFormAsWritten : 1; 1537 // Does the usual deallocation function for the element type require 1538 // a size_t argument? 1539 bool UsualArrayDeleteWantsSize : 1; 1540 // Points to the operator delete overload that is used. Could be a member. 1541 FunctionDecl *OperatorDelete; 1542 // The pointer expression to be deleted. 1543 Stmt *Argument; 1544 // Location of the expression. 1545 SourceLocation Loc; 1546public: 1547 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 1548 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize, 1549 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 1550 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false, 1551 arg->isInstantiationDependent(), 1552 arg->containsUnexpandedParameterPack()), 1553 GlobalDelete(globalDelete), 1554 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten), 1555 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize), 1556 OperatorDelete(operatorDelete), Argument(arg), Loc(loc) { } 1557 explicit CXXDeleteExpr(EmptyShell Shell) 1558 : Expr(CXXDeleteExprClass, Shell), OperatorDelete(0), Argument(0) { } 1559 1560 bool isGlobalDelete() const { return GlobalDelete; } 1561 bool isArrayForm() const { return ArrayForm; } 1562 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; } 1563 1564 /// Answers whether the usual array deallocation function for the 1565 /// allocated type expects the size of the allocation as a 1566 /// parameter. This can be true even if the actual deallocation 1567 /// function that we're using doesn't want a size. 1568 bool doesUsualArrayDeleteWantSize() const { 1569 return UsualArrayDeleteWantsSize; 1570 } 1571 1572 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1573 1574 Expr *getArgument() { return cast<Expr>(Argument); } 1575 const Expr *getArgument() const { return cast<Expr>(Argument); } 1576 1577 /// \brief Retrieve the type being destroyed. If the type being 1578 /// destroyed is a dependent type which may or may not be a pointer, 1579 /// return an invalid type. 1580 QualType getDestroyedType() const; 1581 1582 SourceRange getSourceRange() const { 1583 return SourceRange(Loc, Argument->getLocEnd()); 1584 } 1585 1586 static bool classof(const Stmt *T) { 1587 return T->getStmtClass() == CXXDeleteExprClass; 1588 } 1589 static bool classof(const CXXDeleteExpr *) { return true; } 1590 1591 // Iterators 1592 child_range children() { return child_range(&Argument, &Argument+1); } 1593 1594 friend class ASTStmtReader; 1595}; 1596 1597/// \brief Structure used to store the type being destroyed by a 1598/// pseudo-destructor expression. 1599class PseudoDestructorTypeStorage { 1600 /// \brief Either the type source information or the name of the type, if 1601 /// it couldn't be resolved due to type-dependence. 1602 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 1603 1604 /// \brief The starting source location of the pseudo-destructor type. 1605 SourceLocation Location; 1606 1607public: 1608 PseudoDestructorTypeStorage() { } 1609 1610 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 1611 : Type(II), Location(Loc) { } 1612 1613 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 1614 1615 TypeSourceInfo *getTypeSourceInfo() const { 1616 return Type.dyn_cast<TypeSourceInfo *>(); 1617 } 1618 1619 IdentifierInfo *getIdentifier() const { 1620 return Type.dyn_cast<IdentifierInfo *>(); 1621 } 1622 1623 SourceLocation getLocation() const { return Location; } 1624}; 1625 1626/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 1627/// 1628/// A pseudo-destructor is an expression that looks like a member access to a 1629/// destructor of a scalar type, except that scalar types don't have 1630/// destructors. For example: 1631/// 1632/// \code 1633/// typedef int T; 1634/// void f(int *p) { 1635/// p->T::~T(); 1636/// } 1637/// \endcode 1638/// 1639/// Pseudo-destructors typically occur when instantiating templates such as: 1640/// 1641/// \code 1642/// template<typename T> 1643/// void destroy(T* ptr) { 1644/// ptr->T::~T(); 1645/// } 1646/// \endcode 1647/// 1648/// for scalar types. A pseudo-destructor expression has no run-time semantics 1649/// beyond evaluating the base expression. 1650class CXXPseudoDestructorExpr : public Expr { 1651 /// \brief The base expression (that is being destroyed). 1652 Stmt *Base; 1653 1654 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 1655 /// period ('.'). 1656 bool IsArrow : 1; 1657 1658 /// \brief The location of the '.' or '->' operator. 1659 SourceLocation OperatorLoc; 1660 1661 /// \brief The nested-name-specifier that follows the operator, if present. 1662 NestedNameSpecifierLoc QualifierLoc; 1663 1664 /// \brief The type that precedes the '::' in a qualified pseudo-destructor 1665 /// expression. 1666 TypeSourceInfo *ScopeType; 1667 1668 /// \brief The location of the '::' in a qualified pseudo-destructor 1669 /// expression. 1670 SourceLocation ColonColonLoc; 1671 1672 /// \brief The location of the '~'. 1673 SourceLocation TildeLoc; 1674 1675 /// \brief The type being destroyed, or its name if we were unable to 1676 /// resolve the name. 1677 PseudoDestructorTypeStorage DestroyedType; 1678 1679 friend class ASTStmtReader; 1680 1681public: 1682 CXXPseudoDestructorExpr(ASTContext &Context, 1683 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 1684 NestedNameSpecifierLoc QualifierLoc, 1685 TypeSourceInfo *ScopeType, 1686 SourceLocation ColonColonLoc, 1687 SourceLocation TildeLoc, 1688 PseudoDestructorTypeStorage DestroyedType); 1689 1690 explicit CXXPseudoDestructorExpr(EmptyShell Shell) 1691 : Expr(CXXPseudoDestructorExprClass, Shell), 1692 Base(0), IsArrow(false), QualifierLoc(), ScopeType(0) { } 1693 1694 Expr *getBase() const { return cast<Expr>(Base); } 1695 1696 /// \brief Determines whether this member expression actually had 1697 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1698 /// x->Base::foo. 1699 bool hasQualifier() const { return QualifierLoc; } 1700 1701 /// \brief Retrieves the nested-name-specifier that qualifies the type name, 1702 /// with source-location information. 1703 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 1704 1705 /// \brief If the member name was qualified, retrieves the 1706 /// nested-name-specifier that precedes the member name. Otherwise, returns 1707 /// NULL. 1708 NestedNameSpecifier *getQualifier() const { 1709 return QualifierLoc.getNestedNameSpecifier(); 1710 } 1711 1712 /// \brief Determine whether this pseudo-destructor expression was written 1713 /// using an '->' (otherwise, it used a '.'). 1714 bool isArrow() const { return IsArrow; } 1715 1716 /// \brief Retrieve the location of the '.' or '->' operator. 1717 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1718 1719 /// \brief Retrieve the scope type in a qualified pseudo-destructor 1720 /// expression. 1721 /// 1722 /// Pseudo-destructor expressions can have extra qualification within them 1723 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 1724 /// Here, if the object type of the expression is (or may be) a scalar type, 1725 /// \p T may also be a scalar type and, therefore, cannot be part of a 1726 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 1727 /// destructor expression. 1728 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 1729 1730 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor 1731 /// expression. 1732 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 1733 1734 /// \brief Retrieve the location of the '~'. 1735 SourceLocation getTildeLoc() const { return TildeLoc; } 1736 1737 /// \brief Retrieve the source location information for the type 1738 /// being destroyed. 1739 /// 1740 /// This type-source information is available for non-dependent 1741 /// pseudo-destructor expressions and some dependent pseudo-destructor 1742 /// expressions. Returns NULL if we only have the identifier for a 1743 /// dependent pseudo-destructor expression. 1744 TypeSourceInfo *getDestroyedTypeInfo() const { 1745 return DestroyedType.getTypeSourceInfo(); 1746 } 1747 1748 /// \brief In a dependent pseudo-destructor expression for which we do not 1749 /// have full type information on the destroyed type, provides the name 1750 /// of the destroyed type. 1751 IdentifierInfo *getDestroyedTypeIdentifier() const { 1752 return DestroyedType.getIdentifier(); 1753 } 1754 1755 /// \brief Retrieve the type being destroyed. 1756 QualType getDestroyedType() const; 1757 1758 /// \brief Retrieve the starting location of the type being destroyed. 1759 SourceLocation getDestroyedTypeLoc() const { 1760 return DestroyedType.getLocation(); 1761 } 1762 1763 /// \brief Set the name of destroyed type for a dependent pseudo-destructor 1764 /// expression. 1765 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) { 1766 DestroyedType = PseudoDestructorTypeStorage(II, Loc); 1767 } 1768 1769 /// \brief Set the destroyed type. 1770 void setDestroyedType(TypeSourceInfo *Info) { 1771 DestroyedType = PseudoDestructorTypeStorage(Info); 1772 } 1773 1774 SourceRange getSourceRange() const; 1775 1776 static bool classof(const Stmt *T) { 1777 return T->getStmtClass() == CXXPseudoDestructorExprClass; 1778 } 1779 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 1780 1781 // Iterators 1782 child_range children() { return child_range(&Base, &Base + 1); } 1783}; 1784 1785/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1786/// implementation of TR1/C++0x type trait templates. 1787/// Example: 1788/// __is_pod(int) == true 1789/// __is_enum(std::string) == false 1790class UnaryTypeTraitExpr : public Expr { 1791 /// UTT - The trait. A UnaryTypeTrait enum in MSVC compat unsigned. 1792 unsigned UTT : 31; 1793 /// The value of the type trait. Unspecified if dependent. 1794 bool Value : 1; 1795 1796 /// Loc - The location of the type trait keyword. 1797 SourceLocation Loc; 1798 1799 /// RParen - The location of the closing paren. 1800 SourceLocation RParen; 1801 1802 /// The type being queried. 1803 TypeSourceInfo *QueriedType; 1804 1805public: 1806 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, 1807 TypeSourceInfo *queried, bool value, 1808 SourceLocation rparen, QualType ty) 1809 : Expr(UnaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 1810 false, queried->getType()->isDependentType(), 1811 queried->getType()->isInstantiationDependentType(), 1812 queried->getType()->containsUnexpandedParameterPack()), 1813 UTT(utt), Value(value), Loc(loc), RParen(rparen), QueriedType(queried) { } 1814 1815 explicit UnaryTypeTraitExpr(EmptyShell Empty) 1816 : Expr(UnaryTypeTraitExprClass, Empty), UTT(0), Value(false), 1817 QueriedType() { } 1818 1819 SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1820 1821 UnaryTypeTrait getTrait() const { return static_cast<UnaryTypeTrait>(UTT); } 1822 1823 QualType getQueriedType() const { return QueriedType->getType(); } 1824 1825 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 1826 1827 bool getValue() const { return Value; } 1828 1829 static bool classof(const Stmt *T) { 1830 return T->getStmtClass() == UnaryTypeTraitExprClass; 1831 } 1832 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1833 1834 // Iterators 1835 child_range children() { return child_range(); } 1836 1837 friend class ASTStmtReader; 1838}; 1839 1840/// BinaryTypeTraitExpr - A GCC or MS binary type trait, as used in the 1841/// implementation of TR1/C++0x type trait templates. 1842/// Example: 1843/// __is_base_of(Base, Derived) == true 1844class BinaryTypeTraitExpr : public Expr { 1845 /// BTT - The trait. A BinaryTypeTrait enum in MSVC compat unsigned. 1846 unsigned BTT : 8; 1847 1848 /// The value of the type trait. Unspecified if dependent. 1849 bool Value : 1; 1850 1851 /// Loc - The location of the type trait keyword. 1852 SourceLocation Loc; 1853 1854 /// RParen - The location of the closing paren. 1855 SourceLocation RParen; 1856 1857 /// The lhs type being queried. 1858 TypeSourceInfo *LhsType; 1859 1860 /// The rhs type being queried. 1861 TypeSourceInfo *RhsType; 1862 1863public: 1864 BinaryTypeTraitExpr(SourceLocation loc, BinaryTypeTrait btt, 1865 TypeSourceInfo *lhsType, TypeSourceInfo *rhsType, 1866 bool value, SourceLocation rparen, QualType ty) 1867 : Expr(BinaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, false, 1868 lhsType->getType()->isDependentType() || 1869 rhsType->getType()->isDependentType(), 1870 (lhsType->getType()->isInstantiationDependentType() || 1871 rhsType->getType()->isInstantiationDependentType()), 1872 (lhsType->getType()->containsUnexpandedParameterPack() || 1873 rhsType->getType()->containsUnexpandedParameterPack())), 1874 BTT(btt), Value(value), Loc(loc), RParen(rparen), 1875 LhsType(lhsType), RhsType(rhsType) { } 1876 1877 1878 explicit BinaryTypeTraitExpr(EmptyShell Empty) 1879 : Expr(BinaryTypeTraitExprClass, Empty), BTT(0), Value(false), 1880 LhsType(), RhsType() { } 1881 1882 SourceRange getSourceRange() const { 1883 return SourceRange(Loc, RParen); 1884 } 1885 1886 BinaryTypeTrait getTrait() const { 1887 return static_cast<BinaryTypeTrait>(BTT); 1888 } 1889 1890 QualType getLhsType() const { return LhsType->getType(); } 1891 QualType getRhsType() const { return RhsType->getType(); } 1892 1893 TypeSourceInfo *getLhsTypeSourceInfo() const { return LhsType; } 1894 TypeSourceInfo *getRhsTypeSourceInfo() const { return RhsType; } 1895 1896 bool getValue() const { assert(!isTypeDependent()); return Value; } 1897 1898 static bool classof(const Stmt *T) { 1899 return T->getStmtClass() == BinaryTypeTraitExprClass; 1900 } 1901 static bool classof(const BinaryTypeTraitExpr *) { return true; } 1902 1903 // Iterators 1904 child_range children() { return child_range(); } 1905 1906 friend class ASTStmtReader; 1907}; 1908 1909/// ArrayTypeTraitExpr - An Embarcadero array type trait, as used in the 1910/// implementation of __array_rank and __array_extent. 1911/// Example: 1912/// __array_rank(int[10][20]) == 2 1913/// __array_extent(int, 1) == 20 1914class ArrayTypeTraitExpr : public Expr { 1915 virtual void anchor(); 1916 1917 /// ATT - The trait. An ArrayTypeTrait enum in MSVC compat unsigned. 1918 unsigned ATT : 2; 1919 1920 /// The value of the type trait. Unspecified if dependent. 1921 uint64_t Value; 1922 1923 /// The array dimension being queried, or -1 if not used 1924 Expr *Dimension; 1925 1926 /// Loc - The location of the type trait keyword. 1927 SourceLocation Loc; 1928 1929 /// RParen - The location of the closing paren. 1930 SourceLocation RParen; 1931 1932 /// The type being queried. 1933 TypeSourceInfo *QueriedType; 1934 1935public: 1936 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att, 1937 TypeSourceInfo *queried, uint64_t value, 1938 Expr *dimension, SourceLocation rparen, QualType ty) 1939 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 1940 false, queried->getType()->isDependentType(), 1941 (queried->getType()->isInstantiationDependentType() || 1942 (dimension && dimension->isInstantiationDependent())), 1943 queried->getType()->containsUnexpandedParameterPack()), 1944 ATT(att), Value(value), Dimension(dimension), 1945 Loc(loc), RParen(rparen), QueriedType(queried) { } 1946 1947 1948 explicit ArrayTypeTraitExpr(EmptyShell Empty) 1949 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0), Value(false), 1950 QueriedType() { } 1951 1952 virtual ~ArrayTypeTraitExpr() { } 1953 1954 virtual SourceRange getSourceRange() const { 1955 return SourceRange(Loc, RParen); 1956 } 1957 1958 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); } 1959 1960 QualType getQueriedType() const { return QueriedType->getType(); } 1961 1962 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 1963 1964 uint64_t getValue() const { assert(!isTypeDependent()); return Value; } 1965 1966 Expr *getDimensionExpression() const { return Dimension; } 1967 1968 static bool classof(const Stmt *T) { 1969 return T->getStmtClass() == ArrayTypeTraitExprClass; 1970 } 1971 static bool classof(const ArrayTypeTraitExpr *) { return true; } 1972 1973 // Iterators 1974 child_range children() { return child_range(); } 1975 1976 friend class ASTStmtReader; 1977}; 1978 1979/// ExpressionTraitExpr - An expression trait intrinsic 1980/// Example: 1981/// __is_lvalue_expr(std::cout) == true 1982/// __is_lvalue_expr(1) == false 1983class ExpressionTraitExpr : public Expr { 1984 /// ET - The trait. A ExpressionTrait enum in MSVC compat unsigned. 1985 unsigned ET : 31; 1986 /// The value of the type trait. Unspecified if dependent. 1987 bool Value : 1; 1988 1989 /// Loc - The location of the type trait keyword. 1990 SourceLocation Loc; 1991 1992 /// RParen - The location of the closing paren. 1993 SourceLocation RParen; 1994 1995 Expr* QueriedExpression; 1996public: 1997 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, 1998 Expr *queried, bool value, 1999 SourceLocation rparen, QualType resultType) 2000 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary, 2001 false, // Not type-dependent 2002 // Value-dependent if the argument is type-dependent. 2003 queried->isTypeDependent(), 2004 queried->isInstantiationDependent(), 2005 queried->containsUnexpandedParameterPack()), 2006 ET(et), Value(value), Loc(loc), RParen(rparen), 2007 QueriedExpression(queried) { } 2008 2009 explicit ExpressionTraitExpr(EmptyShell Empty) 2010 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false), 2011 QueriedExpression() { } 2012 2013 SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 2014 2015 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); } 2016 2017 Expr *getQueriedExpression() const { return QueriedExpression; } 2018 2019 bool getValue() const { return Value; } 2020 2021 static bool classof(const Stmt *T) { 2022 return T->getStmtClass() == ExpressionTraitExprClass; 2023 } 2024 static bool classof(const ExpressionTraitExpr *) { return true; } 2025 2026 // Iterators 2027 child_range children() { return child_range(); } 2028 2029 friend class ASTStmtReader; 2030}; 2031 2032 2033/// \brief A reference to an overloaded function set, either an 2034/// \t UnresolvedLookupExpr or an \t UnresolvedMemberExpr. 2035class OverloadExpr : public Expr { 2036 /// The results. These are undesugared, which is to say, they may 2037 /// include UsingShadowDecls. Access is relative to the naming 2038 /// class. 2039 // FIXME: Allocate this data after the OverloadExpr subclass. 2040 DeclAccessPair *Results; 2041 unsigned NumResults; 2042 2043 /// The common name of these declarations. 2044 DeclarationNameInfo NameInfo; 2045 2046 /// \brief The nested-name-specifier that qualifies the name, if any. 2047 NestedNameSpecifierLoc QualifierLoc; 2048 2049protected: 2050 /// \brief Whether the name includes info for explicit template 2051 /// keyword and arguments. 2052 bool HasTemplateKWAndArgsInfo; 2053 2054 /// \brief Return the optional template keyword and arguments info. 2055 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo(); // defined far below. 2056 2057 /// \brief Return the optional template keyword and arguments info. 2058 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2059 return const_cast<OverloadExpr*>(this)->getTemplateKWAndArgsInfo(); 2060 } 2061 2062 OverloadExpr(StmtClass K, ASTContext &C, 2063 NestedNameSpecifierLoc QualifierLoc, 2064 SourceLocation TemplateKWLoc, 2065 const DeclarationNameInfo &NameInfo, 2066 const TemplateArgumentListInfo *TemplateArgs, 2067 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2068 bool KnownDependent, 2069 bool KnownInstantiationDependent, 2070 bool KnownContainsUnexpandedParameterPack); 2071 2072 OverloadExpr(StmtClass K, EmptyShell Empty) 2073 : Expr(K, Empty), Results(0), NumResults(0), 2074 QualifierLoc(), HasTemplateKWAndArgsInfo(false) { } 2075 2076 void initializeResults(ASTContext &C, 2077 UnresolvedSetIterator Begin, 2078 UnresolvedSetIterator End); 2079 2080public: 2081 struct FindResult { 2082 OverloadExpr *Expression; 2083 bool IsAddressOfOperand; 2084 bool HasFormOfMemberPointer; 2085 }; 2086 2087 /// Finds the overloaded expression in the given expression of 2088 /// OverloadTy. 2089 /// 2090 /// \return the expression (which must be there) and true if it has 2091 /// the particular form of a member pointer expression 2092 static FindResult find(Expr *E) { 2093 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 2094 2095 FindResult Result; 2096 2097 E = E->IgnoreParens(); 2098 if (isa<UnaryOperator>(E)) { 2099 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 2100 E = cast<UnaryOperator>(E)->getSubExpr(); 2101 OverloadExpr *Ovl = cast<OverloadExpr>(E->IgnoreParens()); 2102 2103 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier()); 2104 Result.IsAddressOfOperand = true; 2105 Result.Expression = Ovl; 2106 } else { 2107 Result.HasFormOfMemberPointer = false; 2108 Result.IsAddressOfOperand = false; 2109 Result.Expression = cast<OverloadExpr>(E); 2110 } 2111 2112 return Result; 2113 } 2114 2115 /// Gets the naming class of this lookup, if any. 2116 CXXRecordDecl *getNamingClass() const; 2117 2118 typedef UnresolvedSetImpl::iterator decls_iterator; 2119 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); } 2120 decls_iterator decls_end() const { 2121 return UnresolvedSetIterator(Results + NumResults); 2122 } 2123 2124 /// Gets the number of declarations in the unresolved set. 2125 unsigned getNumDecls() const { return NumResults; } 2126 2127 /// Gets the full name info. 2128 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2129 2130 /// Gets the name looked up. 2131 DeclarationName getName() const { return NameInfo.getName(); } 2132 2133 /// Gets the location of the name. 2134 SourceLocation getNameLoc() const { return NameInfo.getLoc(); } 2135 2136 /// Fetches the nested-name qualifier, if one was given. 2137 NestedNameSpecifier *getQualifier() const { 2138 return QualifierLoc.getNestedNameSpecifier(); 2139 } 2140 2141 /// Fetches the nested-name qualifier with source-location information, if 2142 /// one was given. 2143 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2144 2145 /// \brief Retrieve the location of the template keyword preceding 2146 /// this name, if any. 2147 SourceLocation getTemplateKeywordLoc() const { 2148 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2149 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2150 } 2151 2152 /// \brief Retrieve the location of the left angle bracket starting the 2153 /// explicit template argument list following the name, if any. 2154 SourceLocation getLAngleLoc() const { 2155 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2156 return getTemplateKWAndArgsInfo()->LAngleLoc; 2157 } 2158 2159 /// \brief Retrieve the location of the right angle bracket ending the 2160 /// explicit template argument list following the name, if any. 2161 SourceLocation getRAngleLoc() const { 2162 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2163 return getTemplateKWAndArgsInfo()->RAngleLoc; 2164 } 2165 2166 /// Determines whether the name was preceded by the template keyword. 2167 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2168 2169 /// Determines whether this expression had explicit template arguments. 2170 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2171 2172 // Note that, inconsistently with the explicit-template-argument AST 2173 // nodes, users are *forbidden* from calling these methods on objects 2174 // without explicit template arguments. 2175 2176 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2177 assert(hasExplicitTemplateArgs()); 2178 return *getTemplateKWAndArgsInfo(); 2179 } 2180 2181 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2182 return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs(); 2183 } 2184 2185 TemplateArgumentLoc const *getTemplateArgs() const { 2186 return getExplicitTemplateArgs().getTemplateArgs(); 2187 } 2188 2189 unsigned getNumTemplateArgs() const { 2190 return getExplicitTemplateArgs().NumTemplateArgs; 2191 } 2192 2193 /// Copies the template arguments into the given structure. 2194 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2195 getExplicitTemplateArgs().copyInto(List); 2196 } 2197 2198 /// \brief Retrieves the optional explicit template arguments. 2199 /// This points to the same data as getExplicitTemplateArgs(), but 2200 /// returns null if there are no explicit template arguments. 2201 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2202 if (!hasExplicitTemplateArgs()) return 0; 2203 return &getExplicitTemplateArgs(); 2204 } 2205 2206 static bool classof(const Stmt *T) { 2207 return T->getStmtClass() == UnresolvedLookupExprClass || 2208 T->getStmtClass() == UnresolvedMemberExprClass; 2209 } 2210 static bool classof(const OverloadExpr *) { return true; } 2211 2212 friend class ASTStmtReader; 2213 friend class ASTStmtWriter; 2214}; 2215 2216/// \brief A reference to a name which we were able to look up during 2217/// parsing but could not resolve to a specific declaration. This 2218/// arises in several ways: 2219/// * we might be waiting for argument-dependent lookup 2220/// * the name might resolve to an overloaded function 2221/// and eventually: 2222/// * the lookup might have included a function template 2223/// These never include UnresolvedUsingValueDecls, which are always 2224/// class members and therefore appear only in 2225/// UnresolvedMemberLookupExprs. 2226class UnresolvedLookupExpr : public OverloadExpr { 2227 /// True if these lookup results should be extended by 2228 /// argument-dependent lookup if this is the operand of a function 2229 /// call. 2230 bool RequiresADL; 2231 2232 /// True if namespace ::std should be considered an associated namespace 2233 /// for the purposes of argument-dependent lookup. See C++0x [stmt.ranged]p1. 2234 bool StdIsAssociatedNamespace; 2235 2236 /// True if these lookup results are overloaded. This is pretty 2237 /// trivially rederivable if we urgently need to kill this field. 2238 bool Overloaded; 2239 2240 /// The naming class (C++ [class.access.base]p5) of the lookup, if 2241 /// any. This can generally be recalculated from the context chain, 2242 /// but that can be fairly expensive for unqualified lookups. If we 2243 /// want to improve memory use here, this could go in a union 2244 /// against the qualified-lookup bits. 2245 CXXRecordDecl *NamingClass; 2246 2247 UnresolvedLookupExpr(ASTContext &C, 2248 CXXRecordDecl *NamingClass, 2249 NestedNameSpecifierLoc QualifierLoc, 2250 SourceLocation TemplateKWLoc, 2251 const DeclarationNameInfo &NameInfo, 2252 bool RequiresADL, bool Overloaded, 2253 const TemplateArgumentListInfo *TemplateArgs, 2254 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2255 bool StdIsAssociatedNamespace) 2256 : OverloadExpr(UnresolvedLookupExprClass, C, QualifierLoc, TemplateKWLoc, 2257 NameInfo, TemplateArgs, Begin, End, false, false, false), 2258 RequiresADL(RequiresADL), 2259 StdIsAssociatedNamespace(StdIsAssociatedNamespace), 2260 Overloaded(Overloaded), NamingClass(NamingClass) 2261 {} 2262 2263 UnresolvedLookupExpr(EmptyShell Empty) 2264 : OverloadExpr(UnresolvedLookupExprClass, Empty), 2265 RequiresADL(false), StdIsAssociatedNamespace(false), Overloaded(false), 2266 NamingClass(0) 2267 {} 2268 2269 friend class ASTStmtReader; 2270 2271public: 2272 static UnresolvedLookupExpr *Create(ASTContext &C, 2273 CXXRecordDecl *NamingClass, 2274 NestedNameSpecifierLoc QualifierLoc, 2275 const DeclarationNameInfo &NameInfo, 2276 bool ADL, bool Overloaded, 2277 UnresolvedSetIterator Begin, 2278 UnresolvedSetIterator End, 2279 bool StdIsAssociatedNamespace = false) { 2280 assert((ADL || !StdIsAssociatedNamespace) && 2281 "std considered associated namespace when not performing ADL"); 2282 return new(C) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, 2283 SourceLocation(), NameInfo, 2284 ADL, Overloaded, 0, Begin, End, 2285 StdIsAssociatedNamespace); 2286 } 2287 2288 static UnresolvedLookupExpr *Create(ASTContext &C, 2289 CXXRecordDecl *NamingClass, 2290 NestedNameSpecifierLoc QualifierLoc, 2291 SourceLocation TemplateKWLoc, 2292 const DeclarationNameInfo &NameInfo, 2293 bool ADL, 2294 const TemplateArgumentListInfo *Args, 2295 UnresolvedSetIterator Begin, 2296 UnresolvedSetIterator End); 2297 2298 static UnresolvedLookupExpr *CreateEmpty(ASTContext &C, 2299 bool HasTemplateKWAndArgsInfo, 2300 unsigned NumTemplateArgs); 2301 2302 /// True if this declaration should be extended by 2303 /// argument-dependent lookup. 2304 bool requiresADL() const { return RequiresADL; } 2305 2306 /// True if namespace ::std should be artificially added to the set of 2307 /// associated namespaecs for argument-dependent lookup purposes. 2308 bool isStdAssociatedNamespace() const { return StdIsAssociatedNamespace; } 2309 2310 /// True if this lookup is overloaded. 2311 bool isOverloaded() const { return Overloaded; } 2312 2313 /// Gets the 'naming class' (in the sense of C++0x 2314 /// [class.access.base]p5) of the lookup. This is the scope 2315 /// that was looked in to find these results. 2316 CXXRecordDecl *getNamingClass() const { return NamingClass; } 2317 2318 SourceRange getSourceRange() const { 2319 SourceRange Range(getNameInfo().getSourceRange()); 2320 if (getQualifierLoc()) 2321 Range.setBegin(getQualifierLoc().getBeginLoc()); 2322 if (hasExplicitTemplateArgs()) 2323 Range.setEnd(getRAngleLoc()); 2324 return Range; 2325 } 2326 2327 child_range children() { return child_range(); } 2328 2329 static bool classof(const Stmt *T) { 2330 return T->getStmtClass() == UnresolvedLookupExprClass; 2331 } 2332 static bool classof(const UnresolvedLookupExpr *) { return true; } 2333}; 2334 2335/// \brief A qualified reference to a name whose declaration cannot 2336/// yet be resolved. 2337/// 2338/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 2339/// it expresses a reference to a declaration such as 2340/// X<T>::value. The difference, however, is that an 2341/// DependentScopeDeclRefExpr node is used only within C++ templates when 2342/// the qualification (e.g., X<T>::) refers to a dependent type. In 2343/// this case, X<T>::value cannot resolve to a declaration because the 2344/// declaration will differ from on instantiation of X<T> to the 2345/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 2346/// qualifier (X<T>::) and the name of the entity being referenced 2347/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 2348/// declaration can be found. 2349class DependentScopeDeclRefExpr : public Expr { 2350 /// \brief The nested-name-specifier that qualifies this unresolved 2351 /// declaration name. 2352 NestedNameSpecifierLoc QualifierLoc; 2353 2354 /// The name of the entity we will be referencing. 2355 DeclarationNameInfo NameInfo; 2356 2357 /// \brief Whether the name includes info for explicit template 2358 /// keyword and arguments. 2359 bool HasTemplateKWAndArgsInfo; 2360 2361 /// \brief Return the optional template keyword and arguments info. 2362 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2363 if (!HasTemplateKWAndArgsInfo) return 0; 2364 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 2365 } 2366 /// \brief Return the optional template keyword and arguments info. 2367 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2368 return const_cast<DependentScopeDeclRefExpr*>(this) 2369 ->getTemplateKWAndArgsInfo(); 2370 } 2371 2372 DependentScopeDeclRefExpr(QualType T, 2373 NestedNameSpecifierLoc QualifierLoc, 2374 SourceLocation TemplateKWLoc, 2375 const DeclarationNameInfo &NameInfo, 2376 const TemplateArgumentListInfo *Args); 2377 2378public: 2379 static DependentScopeDeclRefExpr *Create(ASTContext &C, 2380 NestedNameSpecifierLoc QualifierLoc, 2381 SourceLocation TemplateKWLoc, 2382 const DeclarationNameInfo &NameInfo, 2383 const TemplateArgumentListInfo *TemplateArgs); 2384 2385 static DependentScopeDeclRefExpr *CreateEmpty(ASTContext &C, 2386 bool HasTemplateKWAndArgsInfo, 2387 unsigned NumTemplateArgs); 2388 2389 /// \brief Retrieve the name that this expression refers to. 2390 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2391 2392 /// \brief Retrieve the name that this expression refers to. 2393 DeclarationName getDeclName() const { return NameInfo.getName(); } 2394 2395 /// \brief Retrieve the location of the name within the expression. 2396 SourceLocation getLocation() const { return NameInfo.getLoc(); } 2397 2398 /// \brief Retrieve the nested-name-specifier that qualifies the 2399 /// name, with source location information. 2400 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2401 2402 2403 /// \brief Retrieve the nested-name-specifier that qualifies this 2404 /// declaration. 2405 NestedNameSpecifier *getQualifier() const { 2406 return QualifierLoc.getNestedNameSpecifier(); 2407 } 2408 2409 /// \brief Retrieve the location of the template keyword preceding 2410 /// this name, if any. 2411 SourceLocation getTemplateKeywordLoc() const { 2412 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2413 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2414 } 2415 2416 /// \brief Retrieve the location of the left angle bracket starting the 2417 /// explicit template argument list following the name, if any. 2418 SourceLocation getLAngleLoc() const { 2419 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2420 return getTemplateKWAndArgsInfo()->LAngleLoc; 2421 } 2422 2423 /// \brief Retrieve the location of the right angle bracket ending the 2424 /// explicit template argument list following the name, if any. 2425 SourceLocation getRAngleLoc() const { 2426 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2427 return getTemplateKWAndArgsInfo()->RAngleLoc; 2428 } 2429 2430 /// Determines whether the name was preceded by the template keyword. 2431 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2432 2433 /// Determines whether this lookup had explicit template arguments. 2434 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2435 2436 // Note that, inconsistently with the explicit-template-argument AST 2437 // nodes, users are *forbidden* from calling these methods on objects 2438 // without explicit template arguments. 2439 2440 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2441 assert(hasExplicitTemplateArgs()); 2442 return *reinterpret_cast<ASTTemplateArgumentListInfo*>(this + 1); 2443 } 2444 2445 /// Gets a reference to the explicit template argument list. 2446 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2447 assert(hasExplicitTemplateArgs()); 2448 return *reinterpret_cast<const ASTTemplateArgumentListInfo*>(this + 1); 2449 } 2450 2451 /// \brief Retrieves the optional explicit template arguments. 2452 /// This points to the same data as getExplicitTemplateArgs(), but 2453 /// returns null if there are no explicit template arguments. 2454 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2455 if (!hasExplicitTemplateArgs()) return 0; 2456 return &getExplicitTemplateArgs(); 2457 } 2458 2459 /// \brief Copies the template arguments (if present) into the given 2460 /// structure. 2461 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2462 getExplicitTemplateArgs().copyInto(List); 2463 } 2464 2465 TemplateArgumentLoc const *getTemplateArgs() const { 2466 return getExplicitTemplateArgs().getTemplateArgs(); 2467 } 2468 2469 unsigned getNumTemplateArgs() const { 2470 return getExplicitTemplateArgs().NumTemplateArgs; 2471 } 2472 2473 SourceRange getSourceRange() const { 2474 SourceRange Range(QualifierLoc.getBeginLoc(), getLocation()); 2475 if (hasExplicitTemplateArgs()) 2476 Range.setEnd(getRAngleLoc()); 2477 return Range; 2478 } 2479 2480 static bool classof(const Stmt *T) { 2481 return T->getStmtClass() == DependentScopeDeclRefExprClass; 2482 } 2483 static bool classof(const DependentScopeDeclRefExpr *) { return true; } 2484 2485 child_range children() { return child_range(); } 2486 2487 friend class ASTStmtReader; 2488 friend class ASTStmtWriter; 2489}; 2490 2491/// Represents an expression --- generally a full-expression --- which 2492/// introduces cleanups to be run at the end of the sub-expression's 2493/// evaluation. The most common source of expression-introduced 2494/// cleanups is temporary objects in C++, but several other kinds of 2495/// expressions can create cleanups, including basically every 2496/// call in ARC that returns an Objective-C pointer. 2497/// 2498/// This expression also tracks whether the sub-expression contains a 2499/// potentially-evaluated block literal. The lifetime of a block 2500/// literal is the extent of the enclosing scope. 2501class ExprWithCleanups : public Expr { 2502public: 2503 /// The type of objects that are kept in the cleanup. 2504 /// It's useful to remember the set of blocks; we could also 2505 /// remember the set of temporaries, but there's currently 2506 /// no need. 2507 typedef BlockDecl *CleanupObject; 2508 2509private: 2510 Stmt *SubExpr; 2511 2512 ExprWithCleanups(EmptyShell, unsigned NumObjects); 2513 ExprWithCleanups(Expr *SubExpr, ArrayRef<CleanupObject> Objects); 2514 2515 CleanupObject *getObjectsBuffer() { 2516 return reinterpret_cast<CleanupObject*>(this + 1); 2517 } 2518 const CleanupObject *getObjectsBuffer() const { 2519 return reinterpret_cast<const CleanupObject*>(this + 1); 2520 } 2521 friend class ASTStmtReader; 2522 2523public: 2524 static ExprWithCleanups *Create(ASTContext &C, EmptyShell empty, 2525 unsigned numObjects); 2526 2527 static ExprWithCleanups *Create(ASTContext &C, Expr *subexpr, 2528 ArrayRef<CleanupObject> objects); 2529 2530 ArrayRef<CleanupObject> getObjects() const { 2531 return ArrayRef<CleanupObject>(getObjectsBuffer(), getNumObjects()); 2532 } 2533 2534 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; } 2535 2536 CleanupObject getObject(unsigned i) const { 2537 assert(i < getNumObjects() && "Index out of range"); 2538 return getObjects()[i]; 2539 } 2540 2541 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 2542 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 2543 2544 /// setSubExpr - As with any mutator of the AST, be very careful 2545 /// when modifying an existing AST to preserve its invariants. 2546 void setSubExpr(Expr *E) { SubExpr = E; } 2547 2548 SourceRange getSourceRange() const { 2549 return SubExpr->getSourceRange(); 2550 } 2551 2552 // Implement isa/cast/dyncast/etc. 2553 static bool classof(const Stmt *T) { 2554 return T->getStmtClass() == ExprWithCleanupsClass; 2555 } 2556 static bool classof(const ExprWithCleanups *) { return true; } 2557 2558 // Iterators 2559 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 2560}; 2561 2562/// \brief Describes an explicit type conversion that uses functional 2563/// notion but could not be resolved because one or more arguments are 2564/// type-dependent. 2565/// 2566/// The explicit type conversions expressed by 2567/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 2568/// where \c T is some type and \c a1, a2, ..., aN are values, and 2569/// either \C T is a dependent type or one or more of the \c a's is 2570/// type-dependent. For example, this would occur in a template such 2571/// as: 2572/// 2573/// \code 2574/// template<typename T, typename A1> 2575/// inline T make_a(const A1& a1) { 2576/// return T(a1); 2577/// } 2578/// \endcode 2579/// 2580/// When the returned expression is instantiated, it may resolve to a 2581/// constructor call, conversion function call, or some kind of type 2582/// conversion. 2583class CXXUnresolvedConstructExpr : public Expr { 2584 /// \brief The type being constructed. 2585 TypeSourceInfo *Type; 2586 2587 /// \brief The location of the left parentheses ('('). 2588 SourceLocation LParenLoc; 2589 2590 /// \brief The location of the right parentheses (')'). 2591 SourceLocation RParenLoc; 2592 2593 /// \brief The number of arguments used to construct the type. 2594 unsigned NumArgs; 2595 2596 CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 2597 SourceLocation LParenLoc, 2598 Expr **Args, 2599 unsigned NumArgs, 2600 SourceLocation RParenLoc); 2601 2602 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) 2603 : Expr(CXXUnresolvedConstructExprClass, Empty), Type(), NumArgs(NumArgs) { } 2604 2605 friend class ASTStmtReader; 2606 2607public: 2608 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 2609 TypeSourceInfo *Type, 2610 SourceLocation LParenLoc, 2611 Expr **Args, 2612 unsigned NumArgs, 2613 SourceLocation RParenLoc); 2614 2615 static CXXUnresolvedConstructExpr *CreateEmpty(ASTContext &C, 2616 unsigned NumArgs); 2617 2618 /// \brief Retrieve the type that is being constructed, as specified 2619 /// in the source code. 2620 QualType getTypeAsWritten() const { return Type->getType(); } 2621 2622 /// \brief Retrieve the type source information for the type being 2623 /// constructed. 2624 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 2625 2626 /// \brief Retrieve the location of the left parentheses ('(') that 2627 /// precedes the argument list. 2628 SourceLocation getLParenLoc() const { return LParenLoc; } 2629 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2630 2631 /// \brief Retrieve the location of the right parentheses (')') that 2632 /// follows the argument list. 2633 SourceLocation getRParenLoc() const { return RParenLoc; } 2634 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2635 2636 /// \brief Retrieve the number of arguments. 2637 unsigned arg_size() const { return NumArgs; } 2638 2639 typedef Expr** arg_iterator; 2640 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 2641 arg_iterator arg_end() { return arg_begin() + NumArgs; } 2642 2643 typedef const Expr* const * const_arg_iterator; 2644 const_arg_iterator arg_begin() const { 2645 return reinterpret_cast<const Expr* const *>(this + 1); 2646 } 2647 const_arg_iterator arg_end() const { 2648 return arg_begin() + NumArgs; 2649 } 2650 2651 Expr *getArg(unsigned I) { 2652 assert(I < NumArgs && "Argument index out-of-range"); 2653 return *(arg_begin() + I); 2654 } 2655 2656 const Expr *getArg(unsigned I) const { 2657 assert(I < NumArgs && "Argument index out-of-range"); 2658 return *(arg_begin() + I); 2659 } 2660 2661 void setArg(unsigned I, Expr *E) { 2662 assert(I < NumArgs && "Argument index out-of-range"); 2663 *(arg_begin() + I) = E; 2664 } 2665 2666 SourceRange getSourceRange() const; 2667 2668 static bool classof(const Stmt *T) { 2669 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 2670 } 2671 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 2672 2673 // Iterators 2674 child_range children() { 2675 Stmt **begin = reinterpret_cast<Stmt**>(this+1); 2676 return child_range(begin, begin + NumArgs); 2677 } 2678}; 2679 2680/// \brief Represents a C++ member access expression where the actual 2681/// member referenced could not be resolved because the base 2682/// expression or the member name was dependent. 2683/// 2684/// Like UnresolvedMemberExprs, these can be either implicit or 2685/// explicit accesses. It is only possible to get one of these with 2686/// an implicit access if a qualifier is provided. 2687class CXXDependentScopeMemberExpr : public Expr { 2688 /// \brief The expression for the base pointer or class reference, 2689 /// e.g., the \c x in x.f. Can be null in implicit accesses. 2690 Stmt *Base; 2691 2692 /// \brief The type of the base expression. Never null, even for 2693 /// implicit accesses. 2694 QualType BaseType; 2695 2696 /// \brief Whether this member expression used the '->' operator or 2697 /// the '.' operator. 2698 bool IsArrow : 1; 2699 2700 /// \brief Whether this member expression has info for explicit template 2701 /// keyword and arguments. 2702 bool HasTemplateKWAndArgsInfo : 1; 2703 2704 /// \brief The location of the '->' or '.' operator. 2705 SourceLocation OperatorLoc; 2706 2707 /// \brief The nested-name-specifier that precedes the member name, if any. 2708 NestedNameSpecifierLoc QualifierLoc; 2709 2710 /// \brief In a qualified member access expression such as t->Base::f, this 2711 /// member stores the resolves of name lookup in the context of the member 2712 /// access expression, to be used at instantiation time. 2713 /// 2714 /// FIXME: This member, along with the QualifierLoc, could 2715 /// be stuck into a structure that is optionally allocated at the end of 2716 /// the CXXDependentScopeMemberExpr, to save space in the common case. 2717 NamedDecl *FirstQualifierFoundInScope; 2718 2719 /// \brief The member to which this member expression refers, which 2720 /// can be name, overloaded operator, or destructor. 2721 /// FIXME: could also be a template-id 2722 DeclarationNameInfo MemberNameInfo; 2723 2724 /// \brief Return the optional template keyword and arguments info. 2725 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2726 if (!HasTemplateKWAndArgsInfo) return 0; 2727 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 2728 } 2729 /// \brief Return the optional template keyword and arguments info. 2730 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2731 return const_cast<CXXDependentScopeMemberExpr*>(this) 2732 ->getTemplateKWAndArgsInfo(); 2733 } 2734 2735 CXXDependentScopeMemberExpr(ASTContext &C, 2736 Expr *Base, QualType BaseType, bool IsArrow, 2737 SourceLocation OperatorLoc, 2738 NestedNameSpecifierLoc QualifierLoc, 2739 SourceLocation TemplateKWLoc, 2740 NamedDecl *FirstQualifierFoundInScope, 2741 DeclarationNameInfo MemberNameInfo, 2742 const TemplateArgumentListInfo *TemplateArgs); 2743 2744public: 2745 CXXDependentScopeMemberExpr(ASTContext &C, 2746 Expr *Base, QualType BaseType, 2747 bool IsArrow, 2748 SourceLocation OperatorLoc, 2749 NestedNameSpecifierLoc QualifierLoc, 2750 NamedDecl *FirstQualifierFoundInScope, 2751 DeclarationNameInfo MemberNameInfo); 2752 2753 static CXXDependentScopeMemberExpr * 2754 Create(ASTContext &C, 2755 Expr *Base, QualType BaseType, bool IsArrow, 2756 SourceLocation OperatorLoc, 2757 NestedNameSpecifierLoc QualifierLoc, 2758 SourceLocation TemplateKWLoc, 2759 NamedDecl *FirstQualifierFoundInScope, 2760 DeclarationNameInfo MemberNameInfo, 2761 const TemplateArgumentListInfo *TemplateArgs); 2762 2763 static CXXDependentScopeMemberExpr * 2764 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 2765 unsigned NumTemplateArgs); 2766 2767 /// \brief True if this is an implicit access, i.e. one in which the 2768 /// member being accessed was not written in the source. The source 2769 /// location of the operator is invalid in this case. 2770 bool isImplicitAccess() const; 2771 2772 /// \brief Retrieve the base object of this member expressions, 2773 /// e.g., the \c x in \c x.m. 2774 Expr *getBase() const { 2775 assert(!isImplicitAccess()); 2776 return cast<Expr>(Base); 2777 } 2778 2779 QualType getBaseType() const { return BaseType; } 2780 2781 /// \brief Determine whether this member expression used the '->' 2782 /// operator; otherwise, it used the '.' operator. 2783 bool isArrow() const { return IsArrow; } 2784 2785 /// \brief Retrieve the location of the '->' or '.' operator. 2786 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2787 2788 /// \brief Retrieve the nested-name-specifier that qualifies the member 2789 /// name. 2790 NestedNameSpecifier *getQualifier() const { 2791 return QualifierLoc.getNestedNameSpecifier(); 2792 } 2793 2794 /// \brief Retrieve the nested-name-specifier that qualifies the member 2795 /// name, with source location information. 2796 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2797 2798 2799 /// \brief Retrieve the first part of the nested-name-specifier that was 2800 /// found in the scope of the member access expression when the member access 2801 /// was initially parsed. 2802 /// 2803 /// This function only returns a useful result when member access expression 2804 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 2805 /// returned by this function describes what was found by unqualified name 2806 /// lookup for the identifier "Base" within the scope of the member access 2807 /// expression itself. At template instantiation time, this information is 2808 /// combined with the results of name lookup into the type of the object 2809 /// expression itself (the class type of x). 2810 NamedDecl *getFirstQualifierFoundInScope() const { 2811 return FirstQualifierFoundInScope; 2812 } 2813 2814 /// \brief Retrieve the name of the member that this expression 2815 /// refers to. 2816 const DeclarationNameInfo &getMemberNameInfo() const { 2817 return MemberNameInfo; 2818 } 2819 2820 /// \brief Retrieve the name of the member that this expression 2821 /// refers to. 2822 DeclarationName getMember() const { return MemberNameInfo.getName(); } 2823 2824 // \brief Retrieve the location of the name of the member that this 2825 // expression refers to. 2826 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); } 2827 2828 /// \brief Retrieve the location of the template keyword preceding the 2829 /// member name, if any. 2830 SourceLocation getTemplateKeywordLoc() const { 2831 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2832 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2833 } 2834 2835 /// \brief Retrieve the location of the left angle bracket starting the 2836 /// explicit template argument list following the member name, if any. 2837 SourceLocation getLAngleLoc() const { 2838 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2839 return getTemplateKWAndArgsInfo()->LAngleLoc; 2840 } 2841 2842 /// \brief Retrieve the location of the right angle bracket ending the 2843 /// explicit template argument list following the member name, if any. 2844 SourceLocation getRAngleLoc() const { 2845 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2846 return getTemplateKWAndArgsInfo()->RAngleLoc; 2847 } 2848 2849 /// Determines whether the member name was preceded by the template keyword. 2850 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2851 2852 /// \brief Determines whether this member expression actually had a C++ 2853 /// template argument list explicitly specified, e.g., x.f<int>. 2854 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2855 2856 /// \brief Retrieve the explicit template argument list that followed the 2857 /// member template name, if any. 2858 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2859 assert(hasExplicitTemplateArgs()); 2860 return *reinterpret_cast<ASTTemplateArgumentListInfo *>(this + 1); 2861 } 2862 2863 /// \brief Retrieve the explicit template argument list that followed the 2864 /// member template name, if any. 2865 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2866 return const_cast<CXXDependentScopeMemberExpr *>(this) 2867 ->getExplicitTemplateArgs(); 2868 } 2869 2870 /// \brief Retrieves the optional explicit template arguments. 2871 /// This points to the same data as getExplicitTemplateArgs(), but 2872 /// returns null if there are no explicit template arguments. 2873 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() { 2874 if (!hasExplicitTemplateArgs()) return 0; 2875 return &getExplicitTemplateArgs(); 2876 } 2877 2878 /// \brief Copies the template arguments (if present) into the given 2879 /// structure. 2880 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2881 getExplicitTemplateArgs().copyInto(List); 2882 } 2883 2884 /// \brief Initializes the template arguments using the given structure. 2885 void initializeTemplateArgumentsFrom(const TemplateArgumentListInfo &List) { 2886 getExplicitTemplateArgs().initializeFrom(List); 2887 } 2888 2889 /// \brief Retrieve the template arguments provided as part of this 2890 /// template-id. 2891 const TemplateArgumentLoc *getTemplateArgs() const { 2892 return getExplicitTemplateArgs().getTemplateArgs(); 2893 } 2894 2895 /// \brief Retrieve the number of template arguments provided as part of this 2896 /// template-id. 2897 unsigned getNumTemplateArgs() const { 2898 return getExplicitTemplateArgs().NumTemplateArgs; 2899 } 2900 2901 SourceRange getSourceRange() const { 2902 SourceRange Range; 2903 if (!isImplicitAccess()) 2904 Range.setBegin(Base->getSourceRange().getBegin()); 2905 else if (getQualifier()) 2906 Range.setBegin(getQualifierLoc().getBeginLoc()); 2907 else 2908 Range.setBegin(MemberNameInfo.getBeginLoc()); 2909 2910 if (hasExplicitTemplateArgs()) 2911 Range.setEnd(getRAngleLoc()); 2912 else 2913 Range.setEnd(MemberNameInfo.getEndLoc()); 2914 return Range; 2915 } 2916 2917 static bool classof(const Stmt *T) { 2918 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 2919 } 2920 static bool classof(const CXXDependentScopeMemberExpr *) { return true; } 2921 2922 // Iterators 2923 child_range children() { 2924 if (isImplicitAccess()) return child_range(); 2925 return child_range(&Base, &Base + 1); 2926 } 2927 2928 friend class ASTStmtReader; 2929 friend class ASTStmtWriter; 2930}; 2931 2932/// \brief Represents a C++ member access expression for which lookup 2933/// produced a set of overloaded functions. 2934/// 2935/// The member access may be explicit or implicit: 2936/// struct A { 2937/// int a, b; 2938/// int explicitAccess() { return this->a + this->A::b; } 2939/// int implicitAccess() { return a + A::b; } 2940/// }; 2941/// 2942/// In the final AST, an explicit access always becomes a MemberExpr. 2943/// An implicit access may become either a MemberExpr or a 2944/// DeclRefExpr, depending on whether the member is static. 2945class UnresolvedMemberExpr : public OverloadExpr { 2946 /// \brief Whether this member expression used the '->' operator or 2947 /// the '.' operator. 2948 bool IsArrow : 1; 2949 2950 /// \brief Whether the lookup results contain an unresolved using 2951 /// declaration. 2952 bool HasUnresolvedUsing : 1; 2953 2954 /// \brief The expression for the base pointer or class reference, 2955 /// e.g., the \c x in x.f. This can be null if this is an 'unbased' 2956 /// member expression 2957 Stmt *Base; 2958 2959 /// \brief The type of the base expression; never null. 2960 QualType BaseType; 2961 2962 /// \brief The location of the '->' or '.' operator. 2963 SourceLocation OperatorLoc; 2964 2965 UnresolvedMemberExpr(ASTContext &C, bool HasUnresolvedUsing, 2966 Expr *Base, QualType BaseType, bool IsArrow, 2967 SourceLocation OperatorLoc, 2968 NestedNameSpecifierLoc QualifierLoc, 2969 SourceLocation TemplateKWLoc, 2970 const DeclarationNameInfo &MemberNameInfo, 2971 const TemplateArgumentListInfo *TemplateArgs, 2972 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 2973 2974 UnresolvedMemberExpr(EmptyShell Empty) 2975 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false), 2976 HasUnresolvedUsing(false), Base(0) { } 2977 2978 friend class ASTStmtReader; 2979 2980public: 2981 static UnresolvedMemberExpr * 2982 Create(ASTContext &C, bool HasUnresolvedUsing, 2983 Expr *Base, QualType BaseType, bool IsArrow, 2984 SourceLocation OperatorLoc, 2985 NestedNameSpecifierLoc QualifierLoc, 2986 SourceLocation TemplateKWLoc, 2987 const DeclarationNameInfo &MemberNameInfo, 2988 const TemplateArgumentListInfo *TemplateArgs, 2989 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 2990 2991 static UnresolvedMemberExpr * 2992 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 2993 unsigned NumTemplateArgs); 2994 2995 /// \brief True if this is an implicit access, i.e. one in which the 2996 /// member being accessed was not written in the source. The source 2997 /// location of the operator is invalid in this case. 2998 bool isImplicitAccess() const; 2999 3000 /// \brief Retrieve the base object of this member expressions, 3001 /// e.g., the \c x in \c x.m. 3002 Expr *getBase() { 3003 assert(!isImplicitAccess()); 3004 return cast<Expr>(Base); 3005 } 3006 const Expr *getBase() const { 3007 assert(!isImplicitAccess()); 3008 return cast<Expr>(Base); 3009 } 3010 3011 QualType getBaseType() const { return BaseType; } 3012 3013 /// \brief Determine whether the lookup results contain an unresolved using 3014 /// declaration. 3015 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; } 3016 3017 /// \brief Determine whether this member expression used the '->' 3018 /// operator; otherwise, it used the '.' operator. 3019 bool isArrow() const { return IsArrow; } 3020 3021 /// \brief Retrieve the location of the '->' or '.' operator. 3022 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3023 3024 /// \brief Retrieves the naming class of this lookup. 3025 CXXRecordDecl *getNamingClass() const; 3026 3027 /// \brief Retrieve the full name info for the member that this expression 3028 /// refers to. 3029 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); } 3030 3031 /// \brief Retrieve the name of the member that this expression 3032 /// refers to. 3033 DeclarationName getMemberName() const { return getName(); } 3034 3035 // \brief Retrieve the location of the name of the member that this 3036 // expression refers to. 3037 SourceLocation getMemberLoc() const { return getNameLoc(); } 3038 3039 SourceRange getSourceRange() const { 3040 SourceRange Range = getMemberNameInfo().getSourceRange(); 3041 if (!isImplicitAccess()) 3042 Range.setBegin(Base->getSourceRange().getBegin()); 3043 else if (getQualifierLoc()) 3044 Range.setBegin(getQualifierLoc().getBeginLoc()); 3045 3046 if (hasExplicitTemplateArgs()) 3047 Range.setEnd(getRAngleLoc()); 3048 return Range; 3049 } 3050 3051 static bool classof(const Stmt *T) { 3052 return T->getStmtClass() == UnresolvedMemberExprClass; 3053 } 3054 static bool classof(const UnresolvedMemberExpr *) { return true; } 3055 3056 // Iterators 3057 child_range children() { 3058 if (isImplicitAccess()) return child_range(); 3059 return child_range(&Base, &Base + 1); 3060 } 3061}; 3062 3063/// \brief Represents a C++0x noexcept expression (C++ [expr.unary.noexcept]). 3064/// 3065/// The noexcept expression tests whether a given expression might throw. Its 3066/// result is a boolean constant. 3067class CXXNoexceptExpr : public Expr { 3068 bool Value : 1; 3069 Stmt *Operand; 3070 SourceRange Range; 3071 3072 friend class ASTStmtReader; 3073 3074public: 3075 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val, 3076 SourceLocation Keyword, SourceLocation RParen) 3077 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary, 3078 /*TypeDependent*/false, 3079 /*ValueDependent*/Val == CT_Dependent, 3080 Val == CT_Dependent || Operand->isInstantiationDependent(), 3081 Operand->containsUnexpandedParameterPack()), 3082 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen) 3083 { } 3084 3085 CXXNoexceptExpr(EmptyShell Empty) 3086 : Expr(CXXNoexceptExprClass, Empty) 3087 { } 3088 3089 Expr *getOperand() const { return static_cast<Expr*>(Operand); } 3090 3091 SourceRange getSourceRange() const { return Range; } 3092 3093 bool getValue() const { return Value; } 3094 3095 static bool classof(const Stmt *T) { 3096 return T->getStmtClass() == CXXNoexceptExprClass; 3097 } 3098 static bool classof(const CXXNoexceptExpr *) { return true; } 3099 3100 // Iterators 3101 child_range children() { return child_range(&Operand, &Operand + 1); } 3102}; 3103 3104/// \brief Represents a C++0x pack expansion that produces a sequence of 3105/// expressions. 3106/// 3107/// A pack expansion expression contains a pattern (which itself is an 3108/// expression) followed by an ellipsis. For example: 3109/// 3110/// \code 3111/// template<typename F, typename ...Types> 3112/// void forward(F f, Types &&...args) { 3113/// f(static_cast<Types&&>(args)...); 3114/// } 3115/// \endcode 3116/// 3117/// Here, the argument to the function object \c f is a pack expansion whose 3118/// pattern is \c static_cast<Types&&>(args). When the \c forward function 3119/// template is instantiated, the pack expansion will instantiate to zero or 3120/// or more function arguments to the function object \c f. 3121class PackExpansionExpr : public Expr { 3122 SourceLocation EllipsisLoc; 3123 3124 /// \brief The number of expansions that will be produced by this pack 3125 /// expansion expression, if known. 3126 /// 3127 /// When zero, the number of expansions is not known. Otherwise, this value 3128 /// is the number of expansions + 1. 3129 unsigned NumExpansions; 3130 3131 Stmt *Pattern; 3132 3133 friend class ASTStmtReader; 3134 friend class ASTStmtWriter; 3135 3136public: 3137 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc, 3138 llvm::Optional<unsigned> NumExpansions) 3139 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(), 3140 Pattern->getObjectKind(), /*TypeDependent=*/true, 3141 /*ValueDependent=*/true, /*InstantiationDependent=*/true, 3142 /*ContainsUnexpandedParameterPack=*/false), 3143 EllipsisLoc(EllipsisLoc), 3144 NumExpansions(NumExpansions? *NumExpansions + 1 : 0), 3145 Pattern(Pattern) { } 3146 3147 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { } 3148 3149 /// \brief Retrieve the pattern of the pack expansion. 3150 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); } 3151 3152 /// \brief Retrieve the pattern of the pack expansion. 3153 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); } 3154 3155 /// \brief Retrieve the location of the ellipsis that describes this pack 3156 /// expansion. 3157 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 3158 3159 /// \brief Determine the number of expansions that will be produced when 3160 /// this pack expansion is instantiated, if already known. 3161 llvm::Optional<unsigned> getNumExpansions() const { 3162 if (NumExpansions) 3163 return NumExpansions - 1; 3164 3165 return llvm::Optional<unsigned>(); 3166 } 3167 3168 SourceRange getSourceRange() const { 3169 return SourceRange(Pattern->getLocStart(), EllipsisLoc); 3170 } 3171 3172 static bool classof(const Stmt *T) { 3173 return T->getStmtClass() == PackExpansionExprClass; 3174 } 3175 static bool classof(const PackExpansionExpr *) { return true; } 3176 3177 // Iterators 3178 child_range children() { 3179 return child_range(&Pattern, &Pattern + 1); 3180 } 3181}; 3182 3183inline ASTTemplateKWAndArgsInfo *OverloadExpr::getTemplateKWAndArgsInfo() { 3184 if (!HasTemplateKWAndArgsInfo) return 0; 3185 if (isa<UnresolvedLookupExpr>(this)) 3186 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3187 (cast<UnresolvedLookupExpr>(this) + 1); 3188 else 3189 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3190 (cast<UnresolvedMemberExpr>(this) + 1); 3191} 3192 3193/// \brief Represents an expression that computes the length of a parameter 3194/// pack. 3195/// 3196/// \code 3197/// template<typename ...Types> 3198/// struct count { 3199/// static const unsigned value = sizeof...(Types); 3200/// }; 3201/// \endcode 3202class SizeOfPackExpr : public Expr { 3203 /// \brief The location of the 'sizeof' keyword. 3204 SourceLocation OperatorLoc; 3205 3206 /// \brief The location of the name of the parameter pack. 3207 SourceLocation PackLoc; 3208 3209 /// \brief The location of the closing parenthesis. 3210 SourceLocation RParenLoc; 3211 3212 /// \brief The length of the parameter pack, if known. 3213 /// 3214 /// When this expression is value-dependent, the length of the parameter pack 3215 /// is unknown. When this expression is not value-dependent, the length is 3216 /// known. 3217 unsigned Length; 3218 3219 /// \brief The parameter pack itself. 3220 NamedDecl *Pack; 3221 3222 friend class ASTStmtReader; 3223 friend class ASTStmtWriter; 3224 3225public: 3226 /// \brief Creates a value-dependent expression that computes the length of 3227 /// the given parameter pack. 3228 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3229 SourceLocation PackLoc, SourceLocation RParenLoc) 3230 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3231 /*TypeDependent=*/false, /*ValueDependent=*/true, 3232 /*InstantiationDependent=*/true, 3233 /*ContainsUnexpandedParameterPack=*/false), 3234 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3235 Length(0), Pack(Pack) { } 3236 3237 /// \brief Creates an expression that computes the length of 3238 /// the given parameter pack, which is already known. 3239 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3240 SourceLocation PackLoc, SourceLocation RParenLoc, 3241 unsigned Length) 3242 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3243 /*TypeDependent=*/false, /*ValueDependent=*/false, 3244 /*InstantiationDependent=*/false, 3245 /*ContainsUnexpandedParameterPack=*/false), 3246 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3247 Length(Length), Pack(Pack) { } 3248 3249 /// \brief Create an empty expression. 3250 SizeOfPackExpr(EmptyShell Empty) : Expr(SizeOfPackExprClass, Empty) { } 3251 3252 /// \brief Determine the location of the 'sizeof' keyword. 3253 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3254 3255 /// \brief Determine the location of the parameter pack. 3256 SourceLocation getPackLoc() const { return PackLoc; } 3257 3258 /// \brief Determine the location of the right parenthesis. 3259 SourceLocation getRParenLoc() const { return RParenLoc; } 3260 3261 /// \brief Retrieve the parameter pack. 3262 NamedDecl *getPack() const { return Pack; } 3263 3264 /// \brief Retrieve the length of the parameter pack. 3265 /// 3266 /// This routine may only be invoked when the expression is not 3267 /// value-dependent. 3268 unsigned getPackLength() const { 3269 assert(!isValueDependent() && 3270 "Cannot get the length of a value-dependent pack size expression"); 3271 return Length; 3272 } 3273 3274 SourceRange getSourceRange() const { 3275 return SourceRange(OperatorLoc, RParenLoc); 3276 } 3277 3278 static bool classof(const Stmt *T) { 3279 return T->getStmtClass() == SizeOfPackExprClass; 3280 } 3281 static bool classof(const SizeOfPackExpr *) { return true; } 3282 3283 // Iterators 3284 child_range children() { return child_range(); } 3285}; 3286 3287/// \brief Represents a reference to a non-type template parameter 3288/// that has been substituted with a template argument. 3289class SubstNonTypeTemplateParmExpr : public Expr { 3290 /// \brief The replaced parameter. 3291 NonTypeTemplateParmDecl *Param; 3292 3293 /// \brief The replacement expression. 3294 Stmt *Replacement; 3295 3296 /// \brief The location of the non-type template parameter reference. 3297 SourceLocation NameLoc; 3298 3299 friend class ASTReader; 3300 friend class ASTStmtReader; 3301 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty) 3302 : Expr(SubstNonTypeTemplateParmExprClass, Empty) { } 3303 3304public: 3305 SubstNonTypeTemplateParmExpr(QualType type, 3306 ExprValueKind valueKind, 3307 SourceLocation loc, 3308 NonTypeTemplateParmDecl *param, 3309 Expr *replacement) 3310 : Expr(SubstNonTypeTemplateParmExprClass, type, valueKind, OK_Ordinary, 3311 replacement->isTypeDependent(), replacement->isValueDependent(), 3312 replacement->isInstantiationDependent(), 3313 replacement->containsUnexpandedParameterPack()), 3314 Param(param), Replacement(replacement), NameLoc(loc) {} 3315 3316 SourceLocation getNameLoc() const { return NameLoc; } 3317 SourceRange getSourceRange() const { return NameLoc; } 3318 3319 Expr *getReplacement() const { return cast<Expr>(Replacement); } 3320 3321 NonTypeTemplateParmDecl *getParameter() const { return Param; } 3322 3323 static bool classof(const Stmt *s) { 3324 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass; 3325 } 3326 static bool classof(const SubstNonTypeTemplateParmExpr *) { 3327 return true; 3328 } 3329 3330 // Iterators 3331 child_range children() { return child_range(&Replacement, &Replacement+1); } 3332}; 3333 3334/// \brief Represents a reference to a non-type template parameter pack that 3335/// has been substituted with a non-template argument pack. 3336/// 3337/// When a pack expansion in the source code contains multiple parameter packs 3338/// and those parameter packs correspond to different levels of template 3339/// parameter lists, this node node is used to represent a non-type template 3340/// parameter pack from an outer level, which has already had its argument pack 3341/// substituted but that still lives within a pack expansion that itself 3342/// could not be instantiated. When actually performing a substitution into 3343/// that pack expansion (e.g., when all template parameters have corresponding 3344/// arguments), this type will be replaced with the appropriate underlying 3345/// expression at the current pack substitution index. 3346class SubstNonTypeTemplateParmPackExpr : public Expr { 3347 /// \brief The non-type template parameter pack itself. 3348 NonTypeTemplateParmDecl *Param; 3349 3350 /// \brief A pointer to the set of template arguments that this 3351 /// parameter pack is instantiated with. 3352 const TemplateArgument *Arguments; 3353 3354 /// \brief The number of template arguments in \c Arguments. 3355 unsigned NumArguments; 3356 3357 /// \brief The location of the non-type template parameter pack reference. 3358 SourceLocation NameLoc; 3359 3360 friend class ASTReader; 3361 friend class ASTStmtReader; 3362 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty) 3363 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) { } 3364 3365public: 3366 SubstNonTypeTemplateParmPackExpr(QualType T, 3367 NonTypeTemplateParmDecl *Param, 3368 SourceLocation NameLoc, 3369 const TemplateArgument &ArgPack); 3370 3371 /// \brief Retrieve the non-type template parameter pack being substituted. 3372 NonTypeTemplateParmDecl *getParameterPack() const { return Param; } 3373 3374 /// \brief Retrieve the location of the parameter pack name. 3375 SourceLocation getParameterPackLocation() const { return NameLoc; } 3376 3377 /// \brief Retrieve the template argument pack containing the substituted 3378 /// template arguments. 3379 TemplateArgument getArgumentPack() const; 3380 3381 SourceRange getSourceRange() const { return NameLoc; } 3382 3383 static bool classof(const Stmt *T) { 3384 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass; 3385 } 3386 static bool classof(const SubstNonTypeTemplateParmPackExpr *) { 3387 return true; 3388 } 3389 3390 // Iterators 3391 child_range children() { return child_range(); } 3392}; 3393 3394/// \brief Represents a prvalue temporary that written into memory so that 3395/// a reference can bind to it. 3396/// 3397/// Prvalue expressions are materialized when they need to have an address 3398/// in memory for a reference to bind to. This happens when binding a 3399/// reference to the result of a conversion, e.g., 3400/// 3401/// \code 3402/// const int &r = 1.0; 3403/// \endcode 3404/// 3405/// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is 3406/// then materialized via a \c MaterializeTemporaryExpr, and the reference 3407/// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues 3408/// (either an lvalue or an xvalue, depending on the kind of reference binding 3409/// to it), maintaining the invariant that references always bind to glvalues. 3410class MaterializeTemporaryExpr : public Expr { 3411 /// \brief The temporary-generating expression whose value will be 3412 /// materialized. 3413 Stmt *Temporary; 3414 3415 friend class ASTStmtReader; 3416 friend class ASTStmtWriter; 3417 3418public: 3419 MaterializeTemporaryExpr(QualType T, Expr *Temporary, 3420 bool BoundToLvalueReference) 3421 : Expr(MaterializeTemporaryExprClass, T, 3422 BoundToLvalueReference? VK_LValue : VK_XValue, OK_Ordinary, 3423 Temporary->isTypeDependent(), Temporary->isValueDependent(), 3424 Temporary->isInstantiationDependent(), 3425 Temporary->containsUnexpandedParameterPack()), 3426 Temporary(Temporary) { } 3427 3428 MaterializeTemporaryExpr(EmptyShell Empty) 3429 : Expr(MaterializeTemporaryExprClass, Empty) { } 3430 3431 /// \brief Retrieve the temporary-generating subexpression whose value will 3432 /// be materialized into a glvalue. 3433 Expr *GetTemporaryExpr() const { return reinterpret_cast<Expr *>(Temporary); } 3434 3435 /// \brief Determine whether this materialized temporary is bound to an 3436 /// lvalue reference; otherwise, it's bound to an rvalue reference. 3437 bool isBoundToLvalueReference() const { 3438 return getValueKind() == VK_LValue; 3439 } 3440 3441 SourceRange getSourceRange() const { return Temporary->getSourceRange(); } 3442 3443 static bool classof(const Stmt *T) { 3444 return T->getStmtClass() == MaterializeTemporaryExprClass; 3445 } 3446 static bool classof(const MaterializeTemporaryExpr *) { 3447 return true; 3448 } 3449 3450 // Iterators 3451 child_range children() { return child_range(&Temporary, &Temporary + 1); } 3452}; 3453 3454} // end namespace clang 3455 3456#endif 3457