ExprCXX.h revision 2fbdfcdf3bbf7b941853d38b123930755e837437
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/Basic/TypeTraits.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/UnresolvedSet.h" 20#include "clang/AST/TemplateBase.h" 21 22namespace clang { 23 24 class CXXConstructorDecl; 25 class CXXDestructorDecl; 26 class CXXMethodDecl; 27 class CXXTemporary; 28 class TemplateArgumentListInfo; 29 30//===--------------------------------------------------------------------===// 31// C++ Expressions. 32//===--------------------------------------------------------------------===// 33 34/// \brief A call to an overloaded operator written using operator 35/// syntax. 36/// 37/// Represents a call to an overloaded operator written using operator 38/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 39/// normal call, this AST node provides better information about the 40/// syntactic representation of the call. 41/// 42/// In a C++ template, this expression node kind will be used whenever 43/// any of the arguments are type-dependent. In this case, the 44/// function itself will be a (possibly empty) set of functions and 45/// function templates that were found by name lookup at template 46/// definition time. 47class CXXOperatorCallExpr : public CallExpr { 48 /// \brief The overloaded operator. 49 OverloadedOperatorKind Operator; 50 51public: 52 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn, 53 Expr **args, unsigned numargs, QualType t, 54 SourceLocation operatorloc) 55 : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc), 56 Operator(Op) {} 57 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) : 58 CallExpr(C, CXXOperatorCallExprClass, Empty) { } 59 60 61 /// getOperator - Returns the kind of overloaded operator that this 62 /// expression refers to. 63 OverloadedOperatorKind getOperator() const { return Operator; } 64 void setOperator(OverloadedOperatorKind Kind) { Operator = Kind; } 65 66 /// getOperatorLoc - Returns the location of the operator symbol in 67 /// the expression. When @c getOperator()==OO_Call, this is the 68 /// location of the right parentheses; when @c 69 /// getOperator()==OO_Subscript, this is the location of the right 70 /// bracket. 71 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 72 73 virtual SourceRange getSourceRange() const; 74 75 static bool classof(const Stmt *T) { 76 return T->getStmtClass() == CXXOperatorCallExprClass; 77 } 78 static bool classof(const CXXOperatorCallExpr *) { return true; } 79}; 80 81/// CXXMemberCallExpr - Represents a call to a member function that 82/// may be written either with member call syntax (e.g., "obj.func()" 83/// or "objptr->func()") or with normal function-call syntax 84/// ("func()") within a member function that ends up calling a member 85/// function. The callee in either case is a MemberExpr that contains 86/// both the object argument and the member function, while the 87/// arguments are the arguments within the parentheses (not including 88/// the object argument). 89class CXXMemberCallExpr : public CallExpr { 90public: 91 CXXMemberCallExpr(ASTContext &C, Expr *fn, Expr **args, unsigned numargs, 92 QualType t, SourceLocation rparenloc) 93 : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {} 94 95 CXXMemberCallExpr(ASTContext &C, EmptyShell Empty) 96 : CallExpr(C, CXXMemberCallExprClass, Empty) { } 97 98 /// getImplicitObjectArgument - Retrieves the implicit object 99 /// argument for the member call. For example, in "x.f(5)", this 100 /// operation would return "x". 101 Expr *getImplicitObjectArgument(); 102 103 virtual SourceRange getSourceRange() const; 104 105 static bool classof(const Stmt *T) { 106 return T->getStmtClass() == CXXMemberCallExprClass; 107 } 108 static bool classof(const CXXMemberCallExpr *) { return true; } 109}; 110 111/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 112/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 113/// const_cast. 114/// 115/// This abstract class is inherited by all of the classes 116/// representing "named" casts, e.g., CXXStaticCastExpr, 117/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 118class CXXNamedCastExpr : public ExplicitCastExpr { 119private: 120 SourceLocation Loc; // the location of the casting op 121 122protected: 123 CXXNamedCastExpr(StmtClass SC, QualType ty, CastKind kind, Expr *op, 124 CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy, 125 SourceLocation l) 126 : ExplicitCastExpr(SC, ty, kind, op, BasePath, writtenTy), Loc(l) {} 127 128 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell) 129 : ExplicitCastExpr(SC, Shell) { } 130 131public: 132 const char *getCastName() const; 133 134 /// \brief Retrieve the location of the cast operator keyword, e.g., 135 /// "static_cast". 136 SourceLocation getOperatorLoc() const { return Loc; } 137 void setOperatorLoc(SourceLocation L) { Loc = L; } 138 139 virtual SourceRange getSourceRange() const { 140 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 141 } 142 static bool classof(const Stmt *T) { 143 switch (T->getStmtClass()) { 144 case CXXStaticCastExprClass: 145 case CXXDynamicCastExprClass: 146 case CXXReinterpretCastExprClass: 147 case CXXConstCastExprClass: 148 return true; 149 default: 150 return false; 151 } 152 } 153 static bool classof(const CXXNamedCastExpr *) { return true; } 154}; 155 156/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]). 157/// 158/// This expression node represents a C++ static cast, e.g., 159/// @c static_cast<int>(1.0). 160class CXXStaticCastExpr : public CXXNamedCastExpr { 161public: 162 CXXStaticCastExpr(QualType ty, CastKind kind, Expr *op, 163 CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy, 164 SourceLocation l) 165 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, kind, op, BasePath, writtenTy, l) {} 166 167 explicit CXXStaticCastExpr(EmptyShell Empty) 168 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty) { } 169 170 static bool classof(const Stmt *T) { 171 return T->getStmtClass() == CXXStaticCastExprClass; 172 } 173 static bool classof(const CXXStaticCastExpr *) { return true; } 174}; 175 176/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 177/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 178/// determine how to perform the type cast. 179/// 180/// This expression node represents a dynamic cast, e.g., 181/// @c dynamic_cast<Derived*>(BasePtr). 182class CXXDynamicCastExpr : public CXXNamedCastExpr { 183public: 184 CXXDynamicCastExpr(QualType ty, CastKind kind, Expr *op, 185 CXXBaseSpecifierArray BasePath, TypeSourceInfo *writtenTy, 186 SourceLocation l) 187 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, kind, op, BasePath, 188 writtenTy, l) {} 189 190 explicit CXXDynamicCastExpr(EmptyShell Empty) 191 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty) { } 192 193 static bool classof(const Stmt *T) { 194 return T->getStmtClass() == CXXDynamicCastExprClass; 195 } 196 static bool classof(const CXXDynamicCastExpr *) { return true; } 197}; 198 199/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 200/// [expr.reinterpret.cast]), which provides a differently-typed view 201/// of a value but performs no actual work at run time. 202/// 203/// This expression node represents a reinterpret cast, e.g., 204/// @c reinterpret_cast<int>(VoidPtr). 205class CXXReinterpretCastExpr : public CXXNamedCastExpr { 206public: 207 CXXReinterpretCastExpr(QualType ty, CastKind kind, Expr *op, 208 CXXBaseSpecifierArray BasePath, 209 TypeSourceInfo *writtenTy, SourceLocation l) 210 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, kind, op, BasePath, 211 writtenTy, l) {} 212 213 explicit CXXReinterpretCastExpr(EmptyShell Empty) 214 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty) { } 215 216 static bool classof(const Stmt *T) { 217 return T->getStmtClass() == CXXReinterpretCastExprClass; 218 } 219 static bool classof(const CXXReinterpretCastExpr *) { return true; } 220}; 221 222/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 223/// which can remove type qualifiers but does not change the underlying value. 224/// 225/// This expression node represents a const cast, e.g., 226/// @c const_cast<char*>(PtrToConstChar). 227class CXXConstCastExpr : public CXXNamedCastExpr { 228public: 229 CXXConstCastExpr(QualType ty, Expr *op, TypeSourceInfo *writtenTy, 230 SourceLocation l) 231 : CXXNamedCastExpr(CXXConstCastExprClass, ty, CK_NoOp, op, 232 CXXBaseSpecifierArray(), writtenTy, l) {} 233 234 explicit CXXConstCastExpr(EmptyShell Empty) 235 : CXXNamedCastExpr(CXXConstCastExprClass, Empty) { } 236 237 static bool classof(const Stmt *T) { 238 return T->getStmtClass() == CXXConstCastExprClass; 239 } 240 static bool classof(const CXXConstCastExpr *) { return true; } 241}; 242 243/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 244/// 245class CXXBoolLiteralExpr : public Expr { 246 bool Value; 247 SourceLocation Loc; 248public: 249 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 250 Expr(CXXBoolLiteralExprClass, Ty, false, false), Value(val), Loc(l) {} 251 252 explicit CXXBoolLiteralExpr(EmptyShell Empty) 253 : Expr(CXXBoolLiteralExprClass, Empty) { } 254 255 bool getValue() const { return Value; } 256 void setValue(bool V) { Value = V; } 257 258 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 259 260 SourceLocation getLocation() const { return Loc; } 261 void setLocation(SourceLocation L) { Loc = L; } 262 263 static bool classof(const Stmt *T) { 264 return T->getStmtClass() == CXXBoolLiteralExprClass; 265 } 266 static bool classof(const CXXBoolLiteralExpr *) { return true; } 267 268 // Iterators 269 virtual child_iterator child_begin(); 270 virtual child_iterator child_end(); 271}; 272 273/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal 274class CXXNullPtrLiteralExpr : public Expr { 275 SourceLocation Loc; 276public: 277 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) : 278 Expr(CXXNullPtrLiteralExprClass, Ty, false, false), Loc(l) {} 279 280 explicit CXXNullPtrLiteralExpr(EmptyShell Empty) 281 : Expr(CXXNullPtrLiteralExprClass, Empty) { } 282 283 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 284 285 SourceLocation getLocation() const { return Loc; } 286 void setLocation(SourceLocation L) { Loc = L; } 287 288 static bool classof(const Stmt *T) { 289 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 290 } 291 static bool classof(const CXXNullPtrLiteralExpr *) { return true; } 292 293 virtual child_iterator child_begin(); 294 virtual child_iterator child_end(); 295}; 296 297/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 298/// the type_info that corresponds to the supplied type, or the (possibly 299/// dynamic) type of the supplied expression. 300/// 301/// This represents code like @c typeid(int) or @c typeid(*objPtr) 302class CXXTypeidExpr : public Expr { 303private: 304 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 305 SourceRange Range; 306 307public: 308 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 309 : Expr(CXXTypeidExprClass, Ty, 310 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 311 false, 312 // typeid is value-dependent if the type or expression are dependent 313 Operand->getType()->isDependentType()), 314 Operand(Operand), Range(R) { } 315 316 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R) 317 : Expr(CXXTypeidExprClass, Ty, 318 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 319 false, 320 // typeid is value-dependent if the type or expression are dependent 321 Operand->isTypeDependent() || Operand->isValueDependent()), 322 Operand(Operand), Range(R) { } 323 324 CXXTypeidExpr(EmptyShell Empty, bool isExpr) 325 : Expr(CXXTypeidExprClass, Empty) { 326 if (isExpr) 327 Operand = (Expr*)0; 328 else 329 Operand = (TypeSourceInfo*)0; 330 } 331 332 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 333 334 /// \brief Retrieves the type operand of this typeid() expression after 335 /// various required adjustments (removing reference types, cv-qualifiers). 336 QualType getTypeOperand() const; 337 338 /// \brief Retrieve source information for the type operand. 339 TypeSourceInfo *getTypeOperandSourceInfo() const { 340 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 341 return Operand.get<TypeSourceInfo *>(); 342 } 343 344 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 345 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 346 Operand = TSI; 347 } 348 349 Expr *getExprOperand() const { 350 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 351 return static_cast<Expr*>(Operand.get<Stmt *>()); 352 } 353 354 virtual SourceRange getSourceRange() const { return Range; } 355 void setSourceRange(SourceRange R) { Range = R; } 356 357 static bool classof(const Stmt *T) { 358 return T->getStmtClass() == CXXTypeidExprClass; 359 } 360 static bool classof(const CXXTypeidExpr *) { return true; } 361 362 // Iterators 363 virtual child_iterator child_begin(); 364 virtual child_iterator child_end(); 365}; 366 367/// CXXThisExpr - Represents the "this" expression in C++, which is a 368/// pointer to the object on which the current member function is 369/// executing (C++ [expr.prim]p3). Example: 370/// 371/// @code 372/// class Foo { 373/// public: 374/// void bar(); 375/// void test() { this->bar(); } 376/// }; 377/// @endcode 378class CXXThisExpr : public Expr { 379 SourceLocation Loc; 380 bool Implicit : 1; 381 382public: 383 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit) 384 : Expr(CXXThisExprClass, Type, 385 // 'this' is type-dependent if the class type of the enclosing 386 // member function is dependent (C++ [temp.dep.expr]p2) 387 Type->isDependentType(), Type->isDependentType()), 388 Loc(L), Implicit(isImplicit) { } 389 390 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {} 391 392 SourceLocation getLocation() const { return Loc; } 393 void setLocation(SourceLocation L) { Loc = L; } 394 395 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 396 397 bool isImplicit() const { return Implicit; } 398 void setImplicit(bool I) { Implicit = I; } 399 400 static bool classof(const Stmt *T) { 401 return T->getStmtClass() == CXXThisExprClass; 402 } 403 static bool classof(const CXXThisExpr *) { return true; } 404 405 // Iterators 406 virtual child_iterator child_begin(); 407 virtual child_iterator child_end(); 408}; 409 410/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 411/// 'throw' and 'throw' assignment-expression. When 412/// assignment-expression isn't present, Op will be null. 413/// 414class CXXThrowExpr : public Expr { 415 Stmt *Op; 416 SourceLocation ThrowLoc; 417public: 418 // Ty is the void type which is used as the result type of the 419 // exepression. The l is the location of the throw keyword. expr 420 // can by null, if the optional expression to throw isn't present. 421 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 422 Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {} 423 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {} 424 425 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 426 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 427 void setSubExpr(Expr *E) { Op = E; } 428 429 SourceLocation getThrowLoc() const { return ThrowLoc; } 430 void setThrowLoc(SourceLocation L) { ThrowLoc = L; } 431 432 virtual SourceRange getSourceRange() const { 433 if (getSubExpr() == 0) 434 return SourceRange(ThrowLoc, ThrowLoc); 435 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 436 } 437 438 static bool classof(const Stmt *T) { 439 return T->getStmtClass() == CXXThrowExprClass; 440 } 441 static bool classof(const CXXThrowExpr *) { return true; } 442 443 // Iterators 444 virtual child_iterator child_begin(); 445 virtual child_iterator child_end(); 446}; 447 448/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 449/// function call argument that was created from the corresponding 450/// parameter's default argument, when the call did not explicitly 451/// supply arguments for all of the parameters. 452class CXXDefaultArgExpr : public Expr { 453 /// \brief The parameter whose default is being used. 454 /// 455 /// When the bit is set, the subexpression is stored after the 456 /// CXXDefaultArgExpr itself. When the bit is clear, the parameter's 457 /// actual default expression is the subexpression. 458 llvm::PointerIntPair<ParmVarDecl *, 1, bool> Param; 459 460 /// \brief The location where the default argument expression was used. 461 SourceLocation Loc; 462 463protected: 464 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param) 465 : Expr(SC, 466 param->hasUnparsedDefaultArg() 467 ? param->getType().getNonReferenceType() 468 : param->getDefaultArg()->getType(), 469 false, false), 470 Param(param, false), Loc(Loc) { } 471 472 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param, 473 Expr *SubExpr) 474 : Expr(SC, SubExpr->getType(), false, false), Param(param, true), Loc(Loc) 475 { 476 *reinterpret_cast<Expr **>(this + 1) = SubExpr; 477 } 478 479protected: 480 virtual void DoDestroy(ASTContext &C); 481 482public: 483 // Param is the parameter whose default argument is used by this 484 // expression. 485 static CXXDefaultArgExpr *Create(ASTContext &C, SourceLocation Loc, 486 ParmVarDecl *Param) { 487 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param); 488 } 489 490 // Param is the parameter whose default argument is used by this 491 // expression, and SubExpr is the expression that will actually be used. 492 static CXXDefaultArgExpr *Create(ASTContext &C, 493 SourceLocation Loc, 494 ParmVarDecl *Param, 495 Expr *SubExpr); 496 497 // Retrieve the parameter that the argument was created from. 498 const ParmVarDecl *getParam() const { return Param.getPointer(); } 499 ParmVarDecl *getParam() { return Param.getPointer(); } 500 501 // Retrieve the actual argument to the function call. 502 const Expr *getExpr() const { 503 if (Param.getInt()) 504 return *reinterpret_cast<Expr const * const*> (this + 1); 505 return getParam()->getDefaultArg(); 506 } 507 Expr *getExpr() { 508 if (Param.getInt()) 509 return *reinterpret_cast<Expr **> (this + 1); 510 return getParam()->getDefaultArg(); 511 } 512 513 /// \brief Retrieve the location where this default argument was actually 514 /// used. 515 SourceLocation getUsedLocation() const { return Loc; } 516 517 virtual SourceRange getSourceRange() const { 518 // Default argument expressions have no representation in the 519 // source, so they have an empty source range. 520 return SourceRange(); 521 } 522 523 static bool classof(const Stmt *T) { 524 return T->getStmtClass() == CXXDefaultArgExprClass; 525 } 526 static bool classof(const CXXDefaultArgExpr *) { return true; } 527 528 // Iterators 529 virtual child_iterator child_begin(); 530 virtual child_iterator child_end(); 531}; 532 533/// CXXTemporary - Represents a C++ temporary. 534class CXXTemporary { 535 /// Destructor - The destructor that needs to be called. 536 const CXXDestructorDecl *Destructor; 537 538 CXXTemporary(const CXXDestructorDecl *destructor) 539 : Destructor(destructor) { } 540 ~CXXTemporary() { } 541 542public: 543 static CXXTemporary *Create(ASTContext &C, 544 const CXXDestructorDecl *Destructor); 545 546 void Destroy(ASTContext &Ctx); 547 548 const CXXDestructorDecl *getDestructor() const { return Destructor; } 549}; 550 551/// CXXBindTemporaryExpr - Represents binding an expression to a temporary, 552/// so its destructor can be called later. 553class CXXBindTemporaryExpr : public Expr { 554 CXXTemporary *Temp; 555 556 Stmt *SubExpr; 557 558 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* subexpr) 559 : Expr(CXXBindTemporaryExprClass, subexpr->getType(), false, false), 560 Temp(temp), SubExpr(subexpr) { } 561 ~CXXBindTemporaryExpr() { } 562 563protected: 564 virtual void DoDestroy(ASTContext &C); 565 566public: 567 static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp, 568 Expr* SubExpr); 569 570 CXXTemporary *getTemporary() { return Temp; } 571 const CXXTemporary *getTemporary() const { return Temp; } 572 573 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 574 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 575 void setSubExpr(Expr *E) { SubExpr = E; } 576 577 virtual SourceRange getSourceRange() const { 578 return SubExpr->getSourceRange(); 579 } 580 581 // Implement isa/cast/dyncast/etc. 582 static bool classof(const Stmt *T) { 583 return T->getStmtClass() == CXXBindTemporaryExprClass; 584 } 585 static bool classof(const CXXBindTemporaryExpr *) { return true; } 586 587 // Iterators 588 virtual child_iterator child_begin(); 589 virtual child_iterator child_end(); 590}; 591 592/// CXXBindReferenceExpr - Represents binding an expression to a reference. 593/// In the example: 594/// 595/// const int &i = 10; 596/// 597/// a bind reference expression is inserted to indicate that 10 is bound to 598/// a reference. (Ans also that a temporary needs to be created to hold the 599/// value). 600class CXXBindReferenceExpr : public Expr { 601 // SubExpr - The expression being bound. 602 Stmt *SubExpr; 603 604 // ExtendsLifetime - Whether binding this reference extends the lifetime of 605 // the expression being bound. FIXME: Add C++ reference. 606 bool ExtendsLifetime; 607 608 /// RequiresTemporaryCopy - Whether binding the subexpression requires a 609 /// temporary copy. 610 bool RequiresTemporaryCopy; 611 612 CXXBindReferenceExpr(Expr *subexpr, bool ExtendsLifetime, 613 bool RequiresTemporaryCopy) 614 : Expr(CXXBindReferenceExprClass, subexpr->getType(), false, false), 615 SubExpr(subexpr), ExtendsLifetime(ExtendsLifetime), 616 RequiresTemporaryCopy(RequiresTemporaryCopy) { } 617 ~CXXBindReferenceExpr() { } 618 619protected: 620 virtual void DoDestroy(ASTContext &C); 621 622public: 623 static CXXBindReferenceExpr *Create(ASTContext &C, Expr *SubExpr, 624 bool ExtendsLifetime, 625 bool RequiresTemporaryCopy); 626 627 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 628 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 629 void setSubExpr(Expr *E) { SubExpr = E; } 630 631 virtual SourceRange getSourceRange() const { 632 return SubExpr->getSourceRange(); 633 } 634 635 /// requiresTemporaryCopy - Whether binding the subexpression requires a 636 /// temporary copy. 637 bool requiresTemporaryCopy() const { return RequiresTemporaryCopy; } 638 639 // extendsLifetime - Whether binding this reference extends the lifetime of 640 // the expression being bound. FIXME: Add C++ reference. 641 bool extendsLifetime() { return ExtendsLifetime; } 642 643 // Implement isa/cast/dyncast/etc. 644 static bool classof(const Stmt *T) { 645 return T->getStmtClass() == CXXBindReferenceExprClass; 646 } 647 static bool classof(const CXXBindReferenceExpr *) { return true; } 648 649 // Iterators 650 virtual child_iterator child_begin(); 651 virtual child_iterator child_end(); 652}; 653 654/// CXXConstructExpr - Represents a call to a C++ constructor. 655class CXXConstructExpr : public Expr { 656public: 657 enum ConstructionKind { 658 CK_Complete, 659 CK_NonVirtualBase, 660 CK_VirtualBase 661 }; 662 663private: 664 CXXConstructorDecl *Constructor; 665 666 SourceLocation Loc; 667 bool Elidable : 1; 668 bool ZeroInitialization : 1; 669 unsigned ConstructKind : 2; 670 Stmt **Args; 671 unsigned NumArgs; 672 673protected: 674 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 675 SourceLocation Loc, 676 CXXConstructorDecl *d, bool elidable, 677 Expr **args, unsigned numargs, 678 bool ZeroInitialization = false, 679 ConstructionKind ConstructKind = CK_Complete); 680 ~CXXConstructExpr() { } 681 682 virtual void DoDestroy(ASTContext &C); 683 684public: 685 /// \brief Construct an empty C++ construction expression that will store 686 /// \p numargs arguments. 687 CXXConstructExpr(EmptyShell Empty, ASTContext &C, unsigned numargs); 688 689 static CXXConstructExpr *Create(ASTContext &C, QualType T, 690 SourceLocation Loc, 691 CXXConstructorDecl *D, bool Elidable, 692 Expr **Args, unsigned NumArgs, 693 bool ZeroInitialization = false, 694 ConstructionKind ConstructKind = CK_Complete); 695 696 697 CXXConstructorDecl* getConstructor() const { return Constructor; } 698 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 699 700 SourceLocation getLocation() const { return Loc; } 701 void setLocation(SourceLocation Loc) { this->Loc = Loc; } 702 703 /// \brief Whether this construction is elidable. 704 bool isElidable() const { return Elidable; } 705 void setElidable(bool E) { Elidable = E; } 706 707 /// \brief Whether this construction first requires 708 /// zero-initialization before the initializer is called. 709 bool requiresZeroInitialization() const { return ZeroInitialization; } 710 void setRequiresZeroInitialization(bool ZeroInit) { 711 ZeroInitialization = ZeroInit; 712 } 713 714 /// \brief Determines whether this constructor is actually constructing 715 /// a base class (rather than a complete object). 716 ConstructionKind getConstructionKind() const { 717 return (ConstructionKind)ConstructKind; 718 } 719 void setConstructionKind(ConstructionKind CK) { 720 ConstructKind = CK; 721 } 722 723 typedef ExprIterator arg_iterator; 724 typedef ConstExprIterator const_arg_iterator; 725 726 arg_iterator arg_begin() { return Args; } 727 arg_iterator arg_end() { return Args + NumArgs; } 728 const_arg_iterator arg_begin() const { return Args; } 729 const_arg_iterator arg_end() const { return Args + NumArgs; } 730 731 Expr **getArgs() const { return reinterpret_cast<Expr **>(Args); } 732 unsigned getNumArgs() const { return NumArgs; } 733 734 /// getArg - Return the specified argument. 735 Expr *getArg(unsigned Arg) { 736 assert(Arg < NumArgs && "Arg access out of range!"); 737 return cast<Expr>(Args[Arg]); 738 } 739 const Expr *getArg(unsigned Arg) const { 740 assert(Arg < NumArgs && "Arg access out of range!"); 741 return cast<Expr>(Args[Arg]); 742 } 743 744 /// setArg - Set the specified argument. 745 void setArg(unsigned Arg, Expr *ArgExpr) { 746 assert(Arg < NumArgs && "Arg access out of range!"); 747 Args[Arg] = ArgExpr; 748 } 749 750 virtual SourceRange getSourceRange() const; 751 752 static bool classof(const Stmt *T) { 753 return T->getStmtClass() == CXXConstructExprClass || 754 T->getStmtClass() == CXXTemporaryObjectExprClass; 755 } 756 static bool classof(const CXXConstructExpr *) { return true; } 757 758 // Iterators 759 virtual child_iterator child_begin(); 760 virtual child_iterator child_end(); 761}; 762 763/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 764/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 765/// x = int(0.5); 766class CXXFunctionalCastExpr : public ExplicitCastExpr { 767 SourceLocation TyBeginLoc; 768 SourceLocation RParenLoc; 769public: 770 CXXFunctionalCastExpr(QualType ty, TypeSourceInfo *writtenTy, 771 SourceLocation tyBeginLoc, CastKind kind, 772 Expr *castExpr, CXXBaseSpecifierArray BasePath, 773 SourceLocation rParenLoc) 774 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, kind, castExpr, 775 BasePath, writtenTy), 776 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 777 778 explicit CXXFunctionalCastExpr(EmptyShell Shell) 779 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell) { } 780 781 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 782 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 783 SourceLocation getRParenLoc() const { return RParenLoc; } 784 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 785 786 virtual SourceRange getSourceRange() const { 787 return SourceRange(TyBeginLoc, RParenLoc); 788 } 789 static bool classof(const Stmt *T) { 790 return T->getStmtClass() == CXXFunctionalCastExprClass; 791 } 792 static bool classof(const CXXFunctionalCastExpr *) { return true; } 793}; 794 795/// @brief Represents a C++ functional cast expression that builds a 796/// temporary object. 797/// 798/// This expression type represents a C++ "functional" cast 799/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 800/// constructor to build a temporary object. If N == 0 but no 801/// constructor will be called (because the functional cast is 802/// performing a value-initialized an object whose class type has no 803/// user-declared constructors), CXXZeroInitValueExpr will represent 804/// the functional cast. Finally, with N == 1 arguments the functional 805/// cast expression will be represented by CXXFunctionalCastExpr. 806/// Example: 807/// @code 808/// struct X { X(int, float); } 809/// 810/// X create_X() { 811/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 812/// }; 813/// @endcode 814class CXXTemporaryObjectExpr : public CXXConstructExpr { 815 SourceLocation TyBeginLoc; 816 SourceLocation RParenLoc; 817 818public: 819 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 820 QualType writtenTy, SourceLocation tyBeginLoc, 821 Expr **Args,unsigned NumArgs, 822 SourceLocation rParenLoc, 823 bool ZeroInitialization = false); 824 825 ~CXXTemporaryObjectExpr() { } 826 827 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 828 SourceLocation getRParenLoc() const { return RParenLoc; } 829 830 virtual SourceRange getSourceRange() const { 831 return SourceRange(TyBeginLoc, RParenLoc); 832 } 833 static bool classof(const Stmt *T) { 834 return T->getStmtClass() == CXXTemporaryObjectExprClass; 835 } 836 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 837}; 838 839/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 840/// Expression "T()" which creates a value-initialized rvalue of type 841/// T, which is either a non-class type or a class type without any 842/// user-defined constructors. 843/// 844class CXXZeroInitValueExpr : public Expr { 845 SourceLocation TyBeginLoc; 846 SourceLocation RParenLoc; 847 848public: 849 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 850 SourceLocation rParenLoc ) : 851 Expr(CXXZeroInitValueExprClass, ty, false, false), 852 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 853 854 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 855 SourceLocation getRParenLoc() const { return RParenLoc; } 856 857 /// @brief Whether this initialization expression was 858 /// implicitly-generated. 859 bool isImplicit() const { 860 return TyBeginLoc.isInvalid() && RParenLoc.isInvalid(); 861 } 862 863 virtual SourceRange getSourceRange() const { 864 return SourceRange(TyBeginLoc, RParenLoc); 865 } 866 867 static bool classof(const Stmt *T) { 868 return T->getStmtClass() == CXXZeroInitValueExprClass; 869 } 870 static bool classof(const CXXZeroInitValueExpr *) { return true; } 871 872 // Iterators 873 virtual child_iterator child_begin(); 874 virtual child_iterator child_end(); 875}; 876 877/// CXXNewExpr - A new expression for memory allocation and constructor calls, 878/// e.g: "new CXXNewExpr(foo)". 879class CXXNewExpr : public Expr { 880 // Was the usage ::new, i.e. is the global new to be used? 881 bool GlobalNew : 1; 882 // Was the form (type-id) used? Otherwise, it was new-type-id. 883 bool ParenTypeId : 1; 884 // Is there an initializer? If not, built-ins are uninitialized, else they're 885 // value-initialized. 886 bool Initializer : 1; 887 // Do we allocate an array? If so, the first SubExpr is the size expression. 888 bool Array : 1; 889 // The number of placement new arguments. 890 unsigned NumPlacementArgs : 14; 891 // The number of constructor arguments. This may be 1 even for non-class 892 // types; use the pseudo copy constructor. 893 unsigned NumConstructorArgs : 14; 894 // Contains an optional array size expression, any number of optional 895 // placement arguments, and any number of optional constructor arguments, 896 // in that order. 897 Stmt **SubExprs; 898 // Points to the allocation function used. 899 FunctionDecl *OperatorNew; 900 // Points to the deallocation function used in case of error. May be null. 901 FunctionDecl *OperatorDelete; 902 // Points to the constructor used. Cannot be null if AllocType is a record; 903 // it would still point at the default constructor (even an implicit one). 904 // Must be null for all other types. 905 CXXConstructorDecl *Constructor; 906 907 SourceLocation StartLoc; 908 SourceLocation EndLoc; 909 910public: 911 CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 912 Expr **placementArgs, unsigned numPlaceArgs, bool ParenTypeId, 913 Expr *arraySize, CXXConstructorDecl *constructor, bool initializer, 914 Expr **constructorArgs, unsigned numConsArgs, 915 FunctionDecl *operatorDelete, QualType ty, 916 SourceLocation startLoc, SourceLocation endLoc); 917 918 virtual void DoDestroy(ASTContext &C); 919 920 QualType getAllocatedType() const { 921 assert(getType()->isPointerType()); 922 return getType()->getAs<PointerType>()->getPointeeType(); 923 } 924 925 FunctionDecl *getOperatorNew() const { return OperatorNew; } 926 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 927 CXXConstructorDecl *getConstructor() const { return Constructor; } 928 929 bool isArray() const { return Array; } 930 Expr *getArraySize() { 931 return Array ? cast<Expr>(SubExprs[0]) : 0; 932 } 933 const Expr *getArraySize() const { 934 return Array ? cast<Expr>(SubExprs[0]) : 0; 935 } 936 937 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 938 Expr *getPlacementArg(unsigned i) { 939 assert(i < NumPlacementArgs && "Index out of range"); 940 return cast<Expr>(SubExprs[Array + i]); 941 } 942 const Expr *getPlacementArg(unsigned i) const { 943 assert(i < NumPlacementArgs && "Index out of range"); 944 return cast<Expr>(SubExprs[Array + i]); 945 } 946 947 bool isGlobalNew() const { return GlobalNew; } 948 bool isParenTypeId() const { return ParenTypeId; } 949 bool hasInitializer() const { return Initializer; } 950 951 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 952 Expr *getConstructorArg(unsigned i) { 953 assert(i < NumConstructorArgs && "Index out of range"); 954 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 955 } 956 const Expr *getConstructorArg(unsigned i) const { 957 assert(i < NumConstructorArgs && "Index out of range"); 958 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 959 } 960 961 typedef ExprIterator arg_iterator; 962 typedef ConstExprIterator const_arg_iterator; 963 964 arg_iterator placement_arg_begin() { 965 return SubExprs + Array; 966 } 967 arg_iterator placement_arg_end() { 968 return SubExprs + Array + getNumPlacementArgs(); 969 } 970 const_arg_iterator placement_arg_begin() const { 971 return SubExprs + Array; 972 } 973 const_arg_iterator placement_arg_end() const { 974 return SubExprs + Array + getNumPlacementArgs(); 975 } 976 977 arg_iterator constructor_arg_begin() { 978 return SubExprs + Array + getNumPlacementArgs(); 979 } 980 arg_iterator constructor_arg_end() { 981 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 982 } 983 const_arg_iterator constructor_arg_begin() const { 984 return SubExprs + Array + getNumPlacementArgs(); 985 } 986 const_arg_iterator constructor_arg_end() const { 987 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 988 } 989 990 virtual SourceRange getSourceRange() const { 991 return SourceRange(StartLoc, EndLoc); 992 } 993 994 static bool classof(const Stmt *T) { 995 return T->getStmtClass() == CXXNewExprClass; 996 } 997 static bool classof(const CXXNewExpr *) { return true; } 998 999 // Iterators 1000 virtual child_iterator child_begin(); 1001 virtual child_iterator child_end(); 1002}; 1003 1004/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 1005/// calls, e.g. "delete[] pArray". 1006class CXXDeleteExpr : public Expr { 1007 // Is this a forced global delete, i.e. "::delete"? 1008 bool GlobalDelete : 1; 1009 // Is this the array form of delete, i.e. "delete[]"? 1010 bool ArrayForm : 1; 1011 // Points to the operator delete overload that is used. Could be a member. 1012 FunctionDecl *OperatorDelete; 1013 // The pointer expression to be deleted. 1014 Stmt *Argument; 1015 // Location of the expression. 1016 SourceLocation Loc; 1017public: 1018 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 1019 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 1020 : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete), 1021 ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg), 1022 Loc(loc) { } 1023 1024 bool isGlobalDelete() const { return GlobalDelete; } 1025 bool isArrayForm() const { return ArrayForm; } 1026 1027 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1028 1029 Expr *getArgument() { return cast<Expr>(Argument); } 1030 const Expr *getArgument() const { return cast<Expr>(Argument); } 1031 1032 virtual SourceRange getSourceRange() const { 1033 return SourceRange(Loc, Argument->getLocEnd()); 1034 } 1035 1036 static bool classof(const Stmt *T) { 1037 return T->getStmtClass() == CXXDeleteExprClass; 1038 } 1039 static bool classof(const CXXDeleteExpr *) { return true; } 1040 1041 // Iterators 1042 virtual child_iterator child_begin(); 1043 virtual child_iterator child_end(); 1044}; 1045 1046/// \brief Structure used to store the type being destroyed by a 1047/// pseudo-destructor expression. 1048class PseudoDestructorTypeStorage { 1049 /// \brief Either the type source information or the name of the type, if 1050 /// it couldn't be resolved due to type-dependence. 1051 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 1052 1053 /// \brief The starting source location of the pseudo-destructor type. 1054 SourceLocation Location; 1055 1056public: 1057 PseudoDestructorTypeStorage() { } 1058 1059 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 1060 : Type(II), Location(Loc) { } 1061 1062 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 1063 1064 TypeSourceInfo *getTypeSourceInfo() const { 1065 return Type.dyn_cast<TypeSourceInfo *>(); 1066 } 1067 1068 IdentifierInfo *getIdentifier() const { 1069 return Type.dyn_cast<IdentifierInfo *>(); 1070 } 1071 1072 SourceLocation getLocation() const { return Location; } 1073}; 1074 1075/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 1076/// 1077/// A pseudo-destructor is an expression that looks like a member access to a 1078/// destructor of a scalar type, except that scalar types don't have 1079/// destructors. For example: 1080/// 1081/// \code 1082/// typedef int T; 1083/// void f(int *p) { 1084/// p->T::~T(); 1085/// } 1086/// \endcode 1087/// 1088/// Pseudo-destructors typically occur when instantiating templates such as: 1089/// 1090/// \code 1091/// template<typename T> 1092/// void destroy(T* ptr) { 1093/// ptr->T::~T(); 1094/// } 1095/// \endcode 1096/// 1097/// for scalar types. A pseudo-destructor expression has no run-time semantics 1098/// beyond evaluating the base expression. 1099class CXXPseudoDestructorExpr : public Expr { 1100 /// \brief The base expression (that is being destroyed). 1101 Stmt *Base; 1102 1103 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 1104 /// period ('.'). 1105 bool IsArrow : 1; 1106 1107 /// \brief The location of the '.' or '->' operator. 1108 SourceLocation OperatorLoc; 1109 1110 /// \brief The nested-name-specifier that follows the operator, if present. 1111 NestedNameSpecifier *Qualifier; 1112 1113 /// \brief The source range that covers the nested-name-specifier, if 1114 /// present. 1115 SourceRange QualifierRange; 1116 1117 /// \brief The type that precedes the '::' in a qualified pseudo-destructor 1118 /// expression. 1119 TypeSourceInfo *ScopeType; 1120 1121 /// \brief The location of the '::' in a qualified pseudo-destructor 1122 /// expression. 1123 SourceLocation ColonColonLoc; 1124 1125 /// \brief The location of the '~'. 1126 SourceLocation TildeLoc; 1127 1128 /// \brief The type being destroyed, or its name if we were unable to 1129 /// resolve the name. 1130 PseudoDestructorTypeStorage DestroyedType; 1131 1132public: 1133 CXXPseudoDestructorExpr(ASTContext &Context, 1134 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 1135 NestedNameSpecifier *Qualifier, 1136 SourceRange QualifierRange, 1137 TypeSourceInfo *ScopeType, 1138 SourceLocation ColonColonLoc, 1139 SourceLocation TildeLoc, 1140 PseudoDestructorTypeStorage DestroyedType) 1141 : Expr(CXXPseudoDestructorExprClass, 1142 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0, 1143 false, 0, false, 1144 false, 0, 0, 1145 FunctionType::ExtInfo())), 1146 /*isTypeDependent=*/(Base->isTypeDependent() || 1147 (DestroyedType.getTypeSourceInfo() && 1148 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())), 1149 /*isValueDependent=*/Base->isValueDependent()), 1150 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), 1151 OperatorLoc(OperatorLoc), Qualifier(Qualifier), 1152 QualifierRange(QualifierRange), 1153 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc), 1154 DestroyedType(DestroyedType) { } 1155 1156 void setBase(Expr *E) { Base = E; } 1157 Expr *getBase() const { return cast<Expr>(Base); } 1158 1159 /// \brief Determines whether this member expression actually had 1160 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1161 /// x->Base::foo. 1162 bool hasQualifier() const { return Qualifier != 0; } 1163 1164 /// \brief If the member name was qualified, retrieves the source range of 1165 /// the nested-name-specifier that precedes the member name. Otherwise, 1166 /// returns an empty source range. 1167 SourceRange getQualifierRange() const { return QualifierRange; } 1168 1169 /// \brief If the member name was qualified, retrieves the 1170 /// nested-name-specifier that precedes the member name. Otherwise, returns 1171 /// NULL. 1172 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1173 1174 /// \brief Determine whether this pseudo-destructor expression was written 1175 /// using an '->' (otherwise, it used a '.'). 1176 bool isArrow() const { return IsArrow; } 1177 void setArrow(bool A) { IsArrow = A; } 1178 1179 /// \brief Retrieve the location of the '.' or '->' operator. 1180 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1181 1182 /// \brief Retrieve the scope type in a qualified pseudo-destructor 1183 /// expression. 1184 /// 1185 /// Pseudo-destructor expressions can have extra qualification within them 1186 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 1187 /// Here, if the object type of the expression is (or may be) a scalar type, 1188 /// \p T may also be a scalar type and, therefore, cannot be part of a 1189 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 1190 /// destructor expression. 1191 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 1192 1193 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor 1194 /// expression. 1195 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 1196 1197 /// \brief Retrieve the location of the '~'. 1198 SourceLocation getTildeLoc() const { return TildeLoc; } 1199 1200 /// \brief Retrieve the source location information for the type 1201 /// being destroyed. 1202 /// 1203 /// This type-source information is available for non-dependent 1204 /// pseudo-destructor expressions and some dependent pseudo-destructor 1205 /// expressions. Returns NULL if we only have the identifier for a 1206 /// dependent pseudo-destructor expression. 1207 TypeSourceInfo *getDestroyedTypeInfo() const { 1208 return DestroyedType.getTypeSourceInfo(); 1209 } 1210 1211 /// \brief In a dependent pseudo-destructor expression for which we do not 1212 /// have full type information on the destroyed type, provides the name 1213 /// of the destroyed type. 1214 IdentifierInfo *getDestroyedTypeIdentifier() const { 1215 return DestroyedType.getIdentifier(); 1216 } 1217 1218 /// \brief Retrieve the type being destroyed. 1219 QualType getDestroyedType() const; 1220 1221 /// \brief Retrieve the starting location of the type being destroyed. 1222 SourceLocation getDestroyedTypeLoc() const { 1223 return DestroyedType.getLocation(); 1224 } 1225 1226 virtual SourceRange getSourceRange() const; 1227 1228 static bool classof(const Stmt *T) { 1229 return T->getStmtClass() == CXXPseudoDestructorExprClass; 1230 } 1231 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 1232 1233 // Iterators 1234 virtual child_iterator child_begin(); 1235 virtual child_iterator child_end(); 1236}; 1237 1238/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1239/// implementation of TR1/C++0x type trait templates. 1240/// Example: 1241/// __is_pod(int) == true 1242/// __is_enum(std::string) == false 1243class UnaryTypeTraitExpr : public Expr { 1244 /// UTT - The trait. 1245 UnaryTypeTrait UTT; 1246 1247 /// Loc - The location of the type trait keyword. 1248 SourceLocation Loc; 1249 1250 /// RParen - The location of the closing paren. 1251 SourceLocation RParen; 1252 1253 /// QueriedType - The type we're testing. 1254 QualType QueriedType; 1255 1256public: 1257 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried, 1258 SourceLocation rparen, QualType ty) 1259 : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()), 1260 UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { } 1261 1262 virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1263 1264 UnaryTypeTrait getTrait() const { return UTT; } 1265 1266 QualType getQueriedType() const { return QueriedType; } 1267 1268 bool EvaluateTrait(ASTContext&) const; 1269 1270 static bool classof(const Stmt *T) { 1271 return T->getStmtClass() == UnaryTypeTraitExprClass; 1272 } 1273 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1274 1275 // Iterators 1276 virtual child_iterator child_begin(); 1277 virtual child_iterator child_end(); 1278}; 1279 1280/// \brief A reference to an overloaded function set, either an 1281/// \t UnresolvedLookupExpr or an \t UnresolvedMemberExpr. 1282class OverloadExpr : public Expr { 1283 /// The results. These are undesugared, which is to say, they may 1284 /// include UsingShadowDecls. Access is relative to the naming 1285 /// class. 1286 UnresolvedSet<4> Results; 1287 1288 /// The common name of these declarations. 1289 DeclarationName Name; 1290 1291 /// The scope specifier, if any. 1292 NestedNameSpecifier *Qualifier; 1293 1294 /// The source range of the scope specifier. 1295 SourceRange QualifierRange; 1296 1297 /// The location of the name. 1298 SourceLocation NameLoc; 1299 1300 /// True if the name was a template-id. 1301 bool HasExplicitTemplateArgs; 1302 1303protected: 1304 OverloadExpr(StmtClass K, QualType T, bool Dependent, 1305 NestedNameSpecifier *Qualifier, SourceRange QRange, 1306 DeclarationName Name, SourceLocation NameLoc, 1307 bool HasTemplateArgs) 1308 : Expr(K, T, Dependent, Dependent), 1309 Name(Name), Qualifier(Qualifier), QualifierRange(QRange), 1310 NameLoc(NameLoc), HasExplicitTemplateArgs(HasTemplateArgs) 1311 {} 1312 1313public: 1314 /// Computes whether an unresolved lookup on the given declarations 1315 /// and optional template arguments is type- and value-dependent. 1316 static bool ComputeDependence(UnresolvedSetIterator Begin, 1317 UnresolvedSetIterator End, 1318 const TemplateArgumentListInfo *Args); 1319 1320 /// Finds the overloaded expression in the given expression of 1321 /// OverloadTy. 1322 /// 1323 /// \return the expression (which must be there) and true if it is 1324 /// within an address-of operator. 1325 static llvm::PointerIntPair<OverloadExpr*,1> find(Expr *E) { 1326 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 1327 1328 bool op = false; 1329 E = E->IgnoreParens(); 1330 if (isa<UnaryOperator>(E)) 1331 op = true, E = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); 1332 return llvm::PointerIntPair<OverloadExpr*,1>(cast<OverloadExpr>(E), op); 1333 } 1334 1335 void addDecls(UnresolvedSetIterator Begin, UnresolvedSetIterator End) { 1336 Results.append(Begin, End); 1337 } 1338 1339 /// Gets the naming class of this lookup, if any. 1340 CXXRecordDecl *getNamingClass() const; 1341 1342 typedef UnresolvedSetImpl::iterator decls_iterator; 1343 decls_iterator decls_begin() const { return Results.begin(); } 1344 decls_iterator decls_end() const { return Results.end(); } 1345 1346 /// Gets the decls as an unresolved set. 1347 const UnresolvedSetImpl &getDecls() { return Results; } 1348 1349 /// Gets the number of declarations in the unresolved set. 1350 unsigned getNumDecls() const { return Results.size(); } 1351 1352 /// Gets the name looked up. 1353 DeclarationName getName() const { return Name; } 1354 void setName(DeclarationName N) { Name = N; } 1355 1356 /// Gets the location of the name. 1357 SourceLocation getNameLoc() const { return NameLoc; } 1358 void setNameLoc(SourceLocation Loc) { NameLoc = Loc; } 1359 1360 /// Fetches the nested-name qualifier, if one was given. 1361 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1362 1363 /// Fetches the range of the nested-name qualifier. 1364 SourceRange getQualifierRange() const { return QualifierRange; } 1365 1366 /// \brief Determines whether this expression had an explicit 1367 /// template argument list, e.g. f<int>. 1368 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 1369 1370 ExplicitTemplateArgumentList &getExplicitTemplateArgs(); // defined far below 1371 1372 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1373 return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs(); 1374 } 1375 1376 ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 1377 if (hasExplicitTemplateArgs()) 1378 return &getExplicitTemplateArgs(); 1379 return 0; 1380 } 1381 1382 static bool classof(const Stmt *T) { 1383 return T->getStmtClass() == UnresolvedLookupExprClass || 1384 T->getStmtClass() == UnresolvedMemberExprClass; 1385 } 1386 static bool classof(const OverloadExpr *) { return true; } 1387}; 1388 1389/// \brief A reference to a name which we were able to look up during 1390/// parsing but could not resolve to a specific declaration. This 1391/// arises in several ways: 1392/// * we might be waiting for argument-dependent lookup 1393/// * the name might resolve to an overloaded function 1394/// and eventually: 1395/// * the lookup might have included a function template 1396/// These never include UnresolvedUsingValueDecls, which are always 1397/// class members and therefore appear only in 1398/// UnresolvedMemberLookupExprs. 1399class UnresolvedLookupExpr : public OverloadExpr { 1400 /// True if these lookup results should be extended by 1401 /// argument-dependent lookup if this is the operand of a function 1402 /// call. 1403 bool RequiresADL; 1404 1405 /// True if these lookup results are overloaded. This is pretty 1406 /// trivially rederivable if we urgently need to kill this field. 1407 bool Overloaded; 1408 1409 /// The naming class (C++ [class.access.base]p5) of the lookup, if 1410 /// any. This can generally be recalculated from the context chain, 1411 /// but that can be fairly expensive for unqualified lookups. If we 1412 /// want to improve memory use here, this could go in a union 1413 /// against the qualified-lookup bits. 1414 CXXRecordDecl *NamingClass; 1415 1416 UnresolvedLookupExpr(QualType T, bool Dependent, CXXRecordDecl *NamingClass, 1417 NestedNameSpecifier *Qualifier, SourceRange QRange, 1418 DeclarationName Name, SourceLocation NameLoc, 1419 bool RequiresADL, bool Overloaded, bool HasTemplateArgs) 1420 : OverloadExpr(UnresolvedLookupExprClass, T, Dependent, Qualifier, QRange, 1421 Name, NameLoc, HasTemplateArgs), 1422 RequiresADL(RequiresADL), Overloaded(Overloaded), NamingClass(NamingClass) 1423 {} 1424 1425public: 1426 static UnresolvedLookupExpr *Create(ASTContext &C, 1427 bool Dependent, 1428 CXXRecordDecl *NamingClass, 1429 NestedNameSpecifier *Qualifier, 1430 SourceRange QualifierRange, 1431 DeclarationName Name, 1432 SourceLocation NameLoc, 1433 bool ADL, bool Overloaded) { 1434 return new(C) UnresolvedLookupExpr(Dependent ? C.DependentTy : C.OverloadTy, 1435 Dependent, NamingClass, 1436 Qualifier, QualifierRange, 1437 Name, NameLoc, ADL, Overloaded, false); 1438 } 1439 1440 static UnresolvedLookupExpr *Create(ASTContext &C, 1441 bool Dependent, 1442 CXXRecordDecl *NamingClass, 1443 NestedNameSpecifier *Qualifier, 1444 SourceRange QualifierRange, 1445 DeclarationName Name, 1446 SourceLocation NameLoc, 1447 bool ADL, 1448 const TemplateArgumentListInfo &Args); 1449 1450 /// True if this declaration should be extended by 1451 /// argument-dependent lookup. 1452 bool requiresADL() const { return RequiresADL; } 1453 1454 /// True if this lookup is overloaded. 1455 bool isOverloaded() const { return Overloaded; } 1456 1457 /// Gets the 'naming class' (in the sense of C++0x 1458 /// [class.access.base]p5) of the lookup. This is the scope 1459 /// that was looked in to find these results. 1460 CXXRecordDecl *getNamingClass() const { return NamingClass; } 1461 1462 // Note that, inconsistently with the explicit-template-argument AST 1463 // nodes, users are *forbidden* from calling these methods on objects 1464 // without explicit template arguments. 1465 1466 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 1467 assert(hasExplicitTemplateArgs()); 1468 return *reinterpret_cast<ExplicitTemplateArgumentList*>(this + 1); 1469 } 1470 1471 /// Gets a reference to the explicit template argument list. 1472 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1473 assert(hasExplicitTemplateArgs()); 1474 return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1); 1475 } 1476 1477 /// \brief Copies the template arguments (if present) into the given 1478 /// structure. 1479 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1480 getExplicitTemplateArgs().copyInto(List); 1481 } 1482 1483 SourceLocation getLAngleLoc() const { 1484 return getExplicitTemplateArgs().LAngleLoc; 1485 } 1486 1487 SourceLocation getRAngleLoc() const { 1488 return getExplicitTemplateArgs().RAngleLoc; 1489 } 1490 1491 TemplateArgumentLoc const *getTemplateArgs() const { 1492 return getExplicitTemplateArgs().getTemplateArgs(); 1493 } 1494 1495 unsigned getNumTemplateArgs() const { 1496 return getExplicitTemplateArgs().NumTemplateArgs; 1497 } 1498 1499 virtual SourceRange getSourceRange() const { 1500 SourceRange Range(getNameLoc()); 1501 if (getQualifier()) Range.setBegin(getQualifierRange().getBegin()); 1502 if (hasExplicitTemplateArgs()) Range.setEnd(getRAngleLoc()); 1503 return Range; 1504 } 1505 1506 virtual StmtIterator child_begin(); 1507 virtual StmtIterator child_end(); 1508 1509 static bool classof(const Stmt *T) { 1510 return T->getStmtClass() == UnresolvedLookupExprClass; 1511 } 1512 static bool classof(const UnresolvedLookupExpr *) { return true; } 1513}; 1514 1515/// \brief A qualified reference to a name whose declaration cannot 1516/// yet be resolved. 1517/// 1518/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 1519/// it expresses a reference to a declaration such as 1520/// X<T>::value. The difference, however, is that an 1521/// DependentScopeDeclRefExpr node is used only within C++ templates when 1522/// the qualification (e.g., X<T>::) refers to a dependent type. In 1523/// this case, X<T>::value cannot resolve to a declaration because the 1524/// declaration will differ from on instantiation of X<T> to the 1525/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 1526/// qualifier (X<T>::) and the name of the entity being referenced 1527/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 1528/// declaration can be found. 1529class DependentScopeDeclRefExpr : public Expr { 1530 /// The name of the entity we will be referencing. 1531 DeclarationName Name; 1532 1533 /// Location of the name of the declaration we're referencing. 1534 SourceLocation Loc; 1535 1536 /// QualifierRange - The source range that covers the 1537 /// nested-name-specifier. 1538 SourceRange QualifierRange; 1539 1540 /// \brief The nested-name-specifier that qualifies this unresolved 1541 /// declaration name. 1542 NestedNameSpecifier *Qualifier; 1543 1544 /// \brief Whether the name includes explicit template arguments. 1545 bool HasExplicitTemplateArgs; 1546 1547 DependentScopeDeclRefExpr(QualType T, 1548 NestedNameSpecifier *Qualifier, 1549 SourceRange QualifierRange, 1550 DeclarationName Name, 1551 SourceLocation NameLoc, 1552 bool HasExplicitTemplateArgs) 1553 : Expr(DependentScopeDeclRefExprClass, T, true, true), 1554 Name(Name), Loc(NameLoc), 1555 QualifierRange(QualifierRange), Qualifier(Qualifier), 1556 HasExplicitTemplateArgs(HasExplicitTemplateArgs) 1557 {} 1558 1559public: 1560 static DependentScopeDeclRefExpr *Create(ASTContext &C, 1561 NestedNameSpecifier *Qualifier, 1562 SourceRange QualifierRange, 1563 DeclarationName Name, 1564 SourceLocation NameLoc, 1565 const TemplateArgumentListInfo *TemplateArgs = 0); 1566 1567 /// \brief Retrieve the name that this expression refers to. 1568 DeclarationName getDeclName() const { return Name; } 1569 1570 /// \brief Retrieve the location of the name within the expression. 1571 SourceLocation getLocation() const { return Loc; } 1572 1573 /// \brief Retrieve the source range of the nested-name-specifier. 1574 SourceRange getQualifierRange() const { return QualifierRange; } 1575 1576 /// \brief Retrieve the nested-name-specifier that qualifies this 1577 /// declaration. 1578 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1579 1580 /// Determines whether this lookup had explicit template arguments. 1581 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 1582 1583 // Note that, inconsistently with the explicit-template-argument AST 1584 // nodes, users are *forbidden* from calling these methods on objects 1585 // without explicit template arguments. 1586 1587 /// Gets a reference to the explicit template argument list. 1588 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1589 assert(hasExplicitTemplateArgs()); 1590 return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1); 1591 } 1592 1593 /// \brief Copies the template arguments (if present) into the given 1594 /// structure. 1595 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1596 getExplicitTemplateArgs().copyInto(List); 1597 } 1598 1599 SourceLocation getLAngleLoc() const { 1600 return getExplicitTemplateArgs().LAngleLoc; 1601 } 1602 1603 SourceLocation getRAngleLoc() const { 1604 return getExplicitTemplateArgs().RAngleLoc; 1605 } 1606 1607 TemplateArgumentLoc const *getTemplateArgs() const { 1608 return getExplicitTemplateArgs().getTemplateArgs(); 1609 } 1610 1611 unsigned getNumTemplateArgs() const { 1612 return getExplicitTemplateArgs().NumTemplateArgs; 1613 } 1614 1615 virtual SourceRange getSourceRange() const { 1616 SourceRange Range(QualifierRange.getBegin(), getLocation()); 1617 if (hasExplicitTemplateArgs()) 1618 Range.setEnd(getRAngleLoc()); 1619 return Range; 1620 } 1621 1622 static bool classof(const Stmt *T) { 1623 return T->getStmtClass() == DependentScopeDeclRefExprClass; 1624 } 1625 static bool classof(const DependentScopeDeclRefExpr *) { return true; } 1626 1627 virtual StmtIterator child_begin(); 1628 virtual StmtIterator child_end(); 1629}; 1630 1631class CXXExprWithTemporaries : public Expr { 1632 Stmt *SubExpr; 1633 1634 CXXTemporary **Temps; 1635 unsigned NumTemps; 1636 1637 CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps, 1638 unsigned NumTemps); 1639 ~CXXExprWithTemporaries(); 1640 1641protected: 1642 virtual void DoDestroy(ASTContext &C); 1643 1644public: 1645 static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr, 1646 CXXTemporary **Temps, 1647 unsigned NumTemps); 1648 1649 unsigned getNumTemporaries() const { return NumTemps; } 1650 CXXTemporary *getTemporary(unsigned i) { 1651 assert(i < NumTemps && "Index out of range"); 1652 return Temps[i]; 1653 } 1654 const CXXTemporary *getTemporary(unsigned i) const { 1655 return const_cast<CXXExprWithTemporaries*>(this)->getTemporary(i); 1656 } 1657 1658 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 1659 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 1660 void setSubExpr(Expr *E) { SubExpr = E; } 1661 1662 virtual SourceRange getSourceRange() const { 1663 return SubExpr->getSourceRange(); 1664 } 1665 1666 // Implement isa/cast/dyncast/etc. 1667 static bool classof(const Stmt *T) { 1668 return T->getStmtClass() == CXXExprWithTemporariesClass; 1669 } 1670 static bool classof(const CXXExprWithTemporaries *) { return true; } 1671 1672 // Iterators 1673 virtual child_iterator child_begin(); 1674 virtual child_iterator child_end(); 1675}; 1676 1677/// \brief Describes an explicit type conversion that uses functional 1678/// notion but could not be resolved because one or more arguments are 1679/// type-dependent. 1680/// 1681/// The explicit type conversions expressed by 1682/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 1683/// where \c T is some type and \c a1, a2, ..., aN are values, and 1684/// either \C T is a dependent type or one or more of the \c a's is 1685/// type-dependent. For example, this would occur in a template such 1686/// as: 1687/// 1688/// \code 1689/// template<typename T, typename A1> 1690/// inline T make_a(const A1& a1) { 1691/// return T(a1); 1692/// } 1693/// \endcode 1694/// 1695/// When the returned expression is instantiated, it may resolve to a 1696/// constructor call, conversion function call, or some kind of type 1697/// conversion. 1698class CXXUnresolvedConstructExpr : public Expr { 1699 /// \brief The starting location of the type 1700 SourceLocation TyBeginLoc; 1701 1702 /// \brief The type being constructed. 1703 QualType Type; 1704 1705 /// \brief The location of the left parentheses ('('). 1706 SourceLocation LParenLoc; 1707 1708 /// \brief The location of the right parentheses (')'). 1709 SourceLocation RParenLoc; 1710 1711 /// \brief The number of arguments used to construct the type. 1712 unsigned NumArgs; 1713 1714 CXXUnresolvedConstructExpr(SourceLocation TyBegin, 1715 QualType T, 1716 SourceLocation LParenLoc, 1717 Expr **Args, 1718 unsigned NumArgs, 1719 SourceLocation RParenLoc); 1720 1721public: 1722 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 1723 SourceLocation TyBegin, 1724 QualType T, 1725 SourceLocation LParenLoc, 1726 Expr **Args, 1727 unsigned NumArgs, 1728 SourceLocation RParenLoc); 1729 1730 /// \brief Retrieve the source location where the type begins. 1731 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 1732 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 1733 1734 /// \brief Retrieve the type that is being constructed, as specified 1735 /// in the source code. 1736 QualType getTypeAsWritten() const { return Type; } 1737 void setTypeAsWritten(QualType T) { Type = T; } 1738 1739 /// \brief Retrieve the location of the left parentheses ('(') that 1740 /// precedes the argument list. 1741 SourceLocation getLParenLoc() const { return LParenLoc; } 1742 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1743 1744 /// \brief Retrieve the location of the right parentheses (')') that 1745 /// follows the argument list. 1746 SourceLocation getRParenLoc() const { return RParenLoc; } 1747 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1748 1749 /// \brief Retrieve the number of arguments. 1750 unsigned arg_size() const { return NumArgs; } 1751 1752 typedef Expr** arg_iterator; 1753 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 1754 arg_iterator arg_end() { return arg_begin() + NumArgs; } 1755 1756 typedef const Expr* const * const_arg_iterator; 1757 const_arg_iterator arg_begin() const { 1758 return reinterpret_cast<const Expr* const *>(this + 1); 1759 } 1760 const_arg_iterator arg_end() const { 1761 return arg_begin() + NumArgs; 1762 } 1763 1764 Expr *getArg(unsigned I) { 1765 assert(I < NumArgs && "Argument index out-of-range"); 1766 return *(arg_begin() + I); 1767 } 1768 1769 const Expr *getArg(unsigned I) const { 1770 assert(I < NumArgs && "Argument index out-of-range"); 1771 return *(arg_begin() + I); 1772 } 1773 1774 virtual SourceRange getSourceRange() const { 1775 return SourceRange(TyBeginLoc, RParenLoc); 1776 } 1777 static bool classof(const Stmt *T) { 1778 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 1779 } 1780 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 1781 1782 // Iterators 1783 virtual child_iterator child_begin(); 1784 virtual child_iterator child_end(); 1785}; 1786 1787/// \brief Represents a C++ member access expression where the actual 1788/// member referenced could not be resolved because the base 1789/// expression or the member name was dependent. 1790/// 1791/// Like UnresolvedMemberExprs, these can be either implicit or 1792/// explicit accesses. It is only possible to get one of these with 1793/// an implicit access if a qualifier is provided. 1794class CXXDependentScopeMemberExpr : public Expr { 1795 /// \brief The expression for the base pointer or class reference, 1796 /// e.g., the \c x in x.f. Can be null in implicit accesses. 1797 Stmt *Base; 1798 1799 /// \brief The type of the base expression. Never null, even for 1800 /// implicit accesses. 1801 QualType BaseType; 1802 1803 /// \brief Whether this member expression used the '->' operator or 1804 /// the '.' operator. 1805 bool IsArrow : 1; 1806 1807 /// \brief Whether this member expression has explicitly-specified template 1808 /// arguments. 1809 bool HasExplicitTemplateArgs : 1; 1810 1811 /// \brief The location of the '->' or '.' operator. 1812 SourceLocation OperatorLoc; 1813 1814 /// \brief The nested-name-specifier that precedes the member name, if any. 1815 NestedNameSpecifier *Qualifier; 1816 1817 /// \brief The source range covering the nested name specifier. 1818 SourceRange QualifierRange; 1819 1820 /// \brief In a qualified member access expression such as t->Base::f, this 1821 /// member stores the resolves of name lookup in the context of the member 1822 /// access expression, to be used at instantiation time. 1823 /// 1824 /// FIXME: This member, along with the Qualifier and QualifierRange, could 1825 /// be stuck into a structure that is optionally allocated at the end of 1826 /// the CXXDependentScopeMemberExpr, to save space in the common case. 1827 NamedDecl *FirstQualifierFoundInScope; 1828 1829 /// \brief The member to which this member expression refers, which 1830 /// can be name, overloaded operator, or destructor. 1831 /// FIXME: could also be a template-id 1832 DeclarationName Member; 1833 1834 /// \brief The location of the member name. 1835 SourceLocation MemberLoc; 1836 1837 /// \brief Retrieve the explicit template argument list that followed the 1838 /// member template name, if any. 1839 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1840 assert(HasExplicitTemplateArgs); 1841 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1842 } 1843 1844 /// \brief Retrieve the explicit template argument list that followed the 1845 /// member template name, if any. 1846 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1847 return const_cast<CXXDependentScopeMemberExpr *>(this) 1848 ->getExplicitTemplateArgumentList(); 1849 } 1850 1851 CXXDependentScopeMemberExpr(ASTContext &C, 1852 Expr *Base, QualType BaseType, bool IsArrow, 1853 SourceLocation OperatorLoc, 1854 NestedNameSpecifier *Qualifier, 1855 SourceRange QualifierRange, 1856 NamedDecl *FirstQualifierFoundInScope, 1857 DeclarationName Member, 1858 SourceLocation MemberLoc, 1859 const TemplateArgumentListInfo *TemplateArgs); 1860 1861public: 1862 CXXDependentScopeMemberExpr(ASTContext &C, 1863 Expr *Base, QualType BaseType, 1864 bool IsArrow, 1865 SourceLocation OperatorLoc, 1866 NestedNameSpecifier *Qualifier, 1867 SourceRange QualifierRange, 1868 NamedDecl *FirstQualifierFoundInScope, 1869 DeclarationName Member, 1870 SourceLocation MemberLoc) 1871 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true), 1872 Base(Base), BaseType(BaseType), IsArrow(IsArrow), 1873 HasExplicitTemplateArgs(false), OperatorLoc(OperatorLoc), 1874 Qualifier(Qualifier), QualifierRange(QualifierRange), 1875 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1876 Member(Member), MemberLoc(MemberLoc) { } 1877 1878 static CXXDependentScopeMemberExpr * 1879 Create(ASTContext &C, 1880 Expr *Base, QualType BaseType, bool IsArrow, 1881 SourceLocation OperatorLoc, 1882 NestedNameSpecifier *Qualifier, 1883 SourceRange QualifierRange, 1884 NamedDecl *FirstQualifierFoundInScope, 1885 DeclarationName Member, 1886 SourceLocation MemberLoc, 1887 const TemplateArgumentListInfo *TemplateArgs); 1888 1889 /// \brief True if this is an implicit access, i.e. one in which the 1890 /// member being accessed was not written in the source. The source 1891 /// location of the operator is invalid in this case. 1892 bool isImplicitAccess() const { return Base == 0; } 1893 1894 /// \brief Retrieve the base object of this member expressions, 1895 /// e.g., the \c x in \c x.m. 1896 Expr *getBase() const { 1897 assert(!isImplicitAccess()); 1898 return cast<Expr>(Base); 1899 } 1900 void setBase(Expr *E) { Base = E; } 1901 1902 QualType getBaseType() const { return BaseType; } 1903 1904 /// \brief Determine whether this member expression used the '->' 1905 /// operator; otherwise, it used the '.' operator. 1906 bool isArrow() const { return IsArrow; } 1907 void setArrow(bool A) { IsArrow = A; } 1908 1909 /// \brief Retrieve the location of the '->' or '.' operator. 1910 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1911 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1912 1913 /// \brief Retrieve the nested-name-specifier that qualifies the member 1914 /// name. 1915 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1916 1917 /// \brief Retrieve the source range covering the nested-name-specifier 1918 /// that qualifies the member name. 1919 SourceRange getQualifierRange() const { return QualifierRange; } 1920 1921 /// \brief Retrieve the first part of the nested-name-specifier that was 1922 /// found in the scope of the member access expression when the member access 1923 /// was initially parsed. 1924 /// 1925 /// This function only returns a useful result when member access expression 1926 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 1927 /// returned by this function describes what was found by unqualified name 1928 /// lookup for the identifier "Base" within the scope of the member access 1929 /// expression itself. At template instantiation time, this information is 1930 /// combined with the results of name lookup into the type of the object 1931 /// expression itself (the class type of x). 1932 NamedDecl *getFirstQualifierFoundInScope() const { 1933 return FirstQualifierFoundInScope; 1934 } 1935 1936 /// \brief Retrieve the name of the member that this expression 1937 /// refers to. 1938 DeclarationName getMember() const { return Member; } 1939 void setMember(DeclarationName N) { Member = N; } 1940 1941 // \brief Retrieve the location of the name of the member that this 1942 // expression refers to. 1943 SourceLocation getMemberLoc() const { return MemberLoc; } 1944 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1945 1946 /// \brief Determines whether this member expression actually had a C++ 1947 /// template argument list explicitly specified, e.g., x.f<int>. 1948 bool hasExplicitTemplateArgs() const { 1949 return HasExplicitTemplateArgs; 1950 } 1951 1952 /// \brief Copies the template arguments (if present) into the given 1953 /// structure. 1954 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1955 assert(HasExplicitTemplateArgs); 1956 getExplicitTemplateArgumentList()->copyInto(List); 1957 } 1958 1959 /// \brief Retrieve the location of the left angle bracket following the 1960 /// member name ('<'), if any. 1961 SourceLocation getLAngleLoc() const { 1962 assert(HasExplicitTemplateArgs); 1963 return getExplicitTemplateArgumentList()->LAngleLoc; 1964 } 1965 1966 /// \brief Retrieve the template arguments provided as part of this 1967 /// template-id. 1968 const TemplateArgumentLoc *getTemplateArgs() const { 1969 assert(HasExplicitTemplateArgs); 1970 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1971 } 1972 1973 /// \brief Retrieve the number of template arguments provided as part of this 1974 /// template-id. 1975 unsigned getNumTemplateArgs() const { 1976 assert(HasExplicitTemplateArgs); 1977 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1978 } 1979 1980 /// \brief Retrieve the location of the right angle bracket following the 1981 /// template arguments ('>'). 1982 SourceLocation getRAngleLoc() const { 1983 assert(HasExplicitTemplateArgs); 1984 return getExplicitTemplateArgumentList()->RAngleLoc; 1985 } 1986 1987 virtual SourceRange getSourceRange() const { 1988 SourceRange Range; 1989 if (!isImplicitAccess()) 1990 Range.setBegin(Base->getSourceRange().getBegin()); 1991 else if (getQualifier()) 1992 Range.setBegin(getQualifierRange().getBegin()); 1993 else 1994 Range.setBegin(MemberLoc); 1995 1996 if (hasExplicitTemplateArgs()) 1997 Range.setEnd(getRAngleLoc()); 1998 else 1999 Range.setEnd(MemberLoc); 2000 return Range; 2001 } 2002 2003 static bool classof(const Stmt *T) { 2004 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 2005 } 2006 static bool classof(const CXXDependentScopeMemberExpr *) { return true; } 2007 2008 // Iterators 2009 virtual child_iterator child_begin(); 2010 virtual child_iterator child_end(); 2011}; 2012 2013/// \brief Represents a C++ member access expression for which lookup 2014/// produced a set of overloaded functions. 2015/// 2016/// The member access may be explicit or implicit: 2017/// struct A { 2018/// int a, b; 2019/// int explicitAccess() { return this->a + this->A::b; } 2020/// int implicitAccess() { return a + A::b; } 2021/// }; 2022/// 2023/// In the final AST, an explicit access always becomes a MemberExpr. 2024/// An implicit access may become either a MemberExpr or a 2025/// DeclRefExpr, depending on whether the member is static. 2026class UnresolvedMemberExpr : public OverloadExpr { 2027 /// \brief Whether this member expression used the '->' operator or 2028 /// the '.' operator. 2029 bool IsArrow : 1; 2030 2031 /// \brief Whether the lookup results contain an unresolved using 2032 /// declaration. 2033 bool HasUnresolvedUsing : 1; 2034 2035 /// \brief The expression for the base pointer or class reference, 2036 /// e.g., the \c x in x.f. This can be null if this is an 'unbased' 2037 /// member expression 2038 Stmt *Base; 2039 2040 /// \brief The type of the base expression; never null. 2041 QualType BaseType; 2042 2043 /// \brief The location of the '->' or '.' operator. 2044 SourceLocation OperatorLoc; 2045 2046 UnresolvedMemberExpr(QualType T, bool Dependent, 2047 bool HasUnresolvedUsing, 2048 Expr *Base, QualType BaseType, bool IsArrow, 2049 SourceLocation OperatorLoc, 2050 NestedNameSpecifier *Qualifier, 2051 SourceRange QualifierRange, 2052 DeclarationName Member, 2053 SourceLocation MemberLoc, 2054 const TemplateArgumentListInfo *TemplateArgs); 2055 2056public: 2057 static UnresolvedMemberExpr * 2058 Create(ASTContext &C, bool Dependent, bool HasUnresolvedUsing, 2059 Expr *Base, QualType BaseType, bool IsArrow, 2060 SourceLocation OperatorLoc, 2061 NestedNameSpecifier *Qualifier, 2062 SourceRange QualifierRange, 2063 DeclarationName Member, 2064 SourceLocation MemberLoc, 2065 const TemplateArgumentListInfo *TemplateArgs); 2066 2067 /// \brief True if this is an implicit access, i.e. one in which the 2068 /// member being accessed was not written in the source. The source 2069 /// location of the operator is invalid in this case. 2070 bool isImplicitAccess() const { return Base == 0; } 2071 2072 /// \brief Retrieve the base object of this member expressions, 2073 /// e.g., the \c x in \c x.m. 2074 Expr *getBase() { 2075 assert(!isImplicitAccess()); 2076 return cast<Expr>(Base); 2077 } 2078 const Expr *getBase() const { 2079 assert(!isImplicitAccess()); 2080 return cast<Expr>(Base); 2081 } 2082 void setBase(Expr *E) { Base = E; } 2083 2084 QualType getBaseType() const { return BaseType; } 2085 2086 /// \brief Determine whether this member expression used the '->' 2087 /// operator; otherwise, it used the '.' operator. 2088 bool isArrow() const { return IsArrow; } 2089 void setArrow(bool A) { IsArrow = A; } 2090 2091 /// \brief Retrieve the location of the '->' or '.' operator. 2092 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2093 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 2094 2095 /// \brief Retrieves the naming class of this lookup. 2096 CXXRecordDecl *getNamingClass() const; 2097 2098 /// \brief Retrieve the name of the member that this expression 2099 /// refers to. 2100 DeclarationName getMemberName() const { return getName(); } 2101 void setMemberName(DeclarationName N) { setName(N); } 2102 2103 // \brief Retrieve the location of the name of the member that this 2104 // expression refers to. 2105 SourceLocation getMemberLoc() const { return getNameLoc(); } 2106 void setMemberLoc(SourceLocation L) { setNameLoc(L); } 2107 2108 /// \brief Retrieve the explicit template argument list that followed the 2109 /// member template name. 2110 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 2111 assert(hasExplicitTemplateArgs()); 2112 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 2113 } 2114 2115 /// \brief Retrieve the explicit template argument list that followed the 2116 /// member template name, if any. 2117 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 2118 assert(hasExplicitTemplateArgs()); 2119 return *reinterpret_cast<const ExplicitTemplateArgumentList *>(this + 1); 2120 } 2121 2122 /// \brief Copies the template arguments into the given structure. 2123 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2124 getExplicitTemplateArgs().copyInto(List); 2125 } 2126 2127 /// \brief Retrieve the location of the left angle bracket following 2128 /// the member name ('<'). 2129 SourceLocation getLAngleLoc() const { 2130 return getExplicitTemplateArgs().LAngleLoc; 2131 } 2132 2133 /// \brief Retrieve the template arguments provided as part of this 2134 /// template-id. 2135 const TemplateArgumentLoc *getTemplateArgs() const { 2136 return getExplicitTemplateArgs().getTemplateArgs(); 2137 } 2138 2139 /// \brief Retrieve the number of template arguments provided as 2140 /// part of this template-id. 2141 unsigned getNumTemplateArgs() const { 2142 return getExplicitTemplateArgs().NumTemplateArgs; 2143 } 2144 2145 /// \brief Retrieve the location of the right angle bracket 2146 /// following the template arguments ('>'). 2147 SourceLocation getRAngleLoc() const { 2148 return getExplicitTemplateArgs().RAngleLoc; 2149 } 2150 2151 virtual SourceRange getSourceRange() const { 2152 SourceRange Range; 2153 if (!isImplicitAccess()) 2154 Range.setBegin(Base->getSourceRange().getBegin()); 2155 else if (getQualifier()) 2156 Range.setBegin(getQualifierRange().getBegin()); 2157 else 2158 Range.setBegin(getMemberLoc()); 2159 2160 if (hasExplicitTemplateArgs()) 2161 Range.setEnd(getRAngleLoc()); 2162 else 2163 Range.setEnd(getMemberLoc()); 2164 return Range; 2165 } 2166 2167 static bool classof(const Stmt *T) { 2168 return T->getStmtClass() == UnresolvedMemberExprClass; 2169 } 2170 static bool classof(const UnresolvedMemberExpr *) { return true; } 2171 2172 // Iterators 2173 virtual child_iterator child_begin(); 2174 virtual child_iterator child_end(); 2175}; 2176 2177inline ExplicitTemplateArgumentList &OverloadExpr::getExplicitTemplateArgs() { 2178 if (isa<UnresolvedLookupExpr>(this)) 2179 return cast<UnresolvedLookupExpr>(this)->getExplicitTemplateArgs(); 2180 else 2181 return cast<UnresolvedMemberExpr>(this)->getExplicitTemplateArgs(); 2182} 2183 2184} // end namespace clang 2185 2186#endif 2187