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