ExprCXX.h revision 39da0b8145eaec7da7004f9b3645c5c9f4f63b1d
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/Decl.h" 20 21namespace clang { 22 23 class CXXConstructorDecl; 24 class CXXDestructorDecl; 25 class CXXMethodDecl; 26 class CXXTemporary; 27 28//===--------------------------------------------------------------------===// 29// C++ Expressions. 30//===--------------------------------------------------------------------===// 31 32/// \brief A call to an overloaded operator written using operator 33/// syntax. 34/// 35/// Represents a call to an overloaded operator written using operator 36/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 37/// normal call, this AST node provides better information about the 38/// syntactic representation of the call. 39/// 40/// In a C++ template, this expression node kind will be used whenever 41/// any of the arguments are type-dependent. In this case, the 42/// function itself will be a (possibly empty) set of functions and 43/// function templates that were found by name lookup at template 44/// definition time. 45class CXXOperatorCallExpr : public CallExpr { 46 /// \brief The overloaded operator. 47 OverloadedOperatorKind Operator; 48 49public: 50 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn, 51 Expr **args, unsigned numargs, QualType t, 52 SourceLocation operatorloc) 53 : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc), 54 Operator(Op) {} 55 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) : 56 CallExpr(C, CXXOperatorCallExprClass, Empty) { } 57 58 59 /// getOperator - Returns the kind of overloaded operator that this 60 /// expression refers to. 61 OverloadedOperatorKind getOperator() const { return Operator; } 62 void setOperator(OverloadedOperatorKind Kind) { Operator = Kind; } 63 64 /// getOperatorLoc - Returns the location of the operator symbol in 65 /// the expression. When @c getOperator()==OO_Call, this is the 66 /// location of the right parentheses; when @c 67 /// getOperator()==OO_Subscript, this is the location of the right 68 /// bracket. 69 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 70 71 virtual SourceRange getSourceRange() const; 72 73 static bool classof(const Stmt *T) { 74 return T->getStmtClass() == CXXOperatorCallExprClass; 75 } 76 static bool classof(const CXXOperatorCallExpr *) { return true; } 77}; 78 79/// CXXMemberCallExpr - Represents a call to a member function that 80/// may be written either with member call syntax (e.g., "obj.func()" 81/// or "objptr->func()") or with normal function-call syntax 82/// ("func()") within a member function that ends up calling a member 83/// function. The callee in either case is a MemberExpr that contains 84/// both the object argument and the member function, while the 85/// arguments are the arguments within the parentheses (not including 86/// the object argument). 87class CXXMemberCallExpr : public CallExpr { 88public: 89 CXXMemberCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, 90 QualType t, SourceLocation rparenloc) 91 : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {} 92 93 /// getImplicitObjectArgument - Retrieves the implicit object 94 /// argument for the member call. For example, in "x.f(5)", this 95 /// operation would return "x". 96 Expr *getImplicitObjectArgument(); 97 98 static bool classof(const Stmt *T) { 99 return T->getStmtClass() == CXXMemberCallExprClass; 100 } 101 static bool classof(const CXXMemberCallExpr *) { return true; } 102}; 103 104/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 105/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 106/// const_cast. 107/// 108/// This abstract class is inherited by all of the classes 109/// representing "named" casts, e.g., CXXStaticCastExpr, 110/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 111class CXXNamedCastExpr : public ExplicitCastExpr { 112private: 113 SourceLocation Loc; // the location of the casting op 114 115protected: 116 CXXNamedCastExpr(StmtClass SC, QualType ty, const CastInfo &info, Expr *op, 117 QualType writtenTy, SourceLocation l) 118 : ExplicitCastExpr(SC, ty, info, op, writtenTy), Loc(l) {} 119 120public: 121 const char *getCastName() const; 122 123 /// \brief Retrieve the location of the cast operator keyword, e.g., 124 /// "static_cast". 125 SourceLocation getOperatorLoc() const { return Loc; } 126 void setOperatorLoc(SourceLocation L) { Loc = L; } 127 128 virtual SourceRange getSourceRange() const { 129 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 130 } 131 static bool classof(const Stmt *T) { 132 switch (T->getStmtClass()) { 133 case CXXNamedCastExprClass: 134 case CXXStaticCastExprClass: 135 case CXXDynamicCastExprClass: 136 case CXXReinterpretCastExprClass: 137 case CXXConstCastExprClass: 138 return true; 139 default: 140 return false; 141 } 142 } 143 static bool classof(const CXXNamedCastExpr *) { return true; } 144}; 145 146/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]). 147/// 148/// This expression node represents a C++ static cast, e.g., 149/// @c static_cast<int>(1.0). 150class CXXStaticCastExpr : public CXXNamedCastExpr { 151public: 152 CXXStaticCastExpr(QualType ty, const CastInfo &info, Expr *op, 153 QualType writtenTy, SourceLocation l) 154 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, info, op, writtenTy, l) {} 155 156 static bool classof(const Stmt *T) { 157 return T->getStmtClass() == CXXStaticCastExprClass; 158 } 159 static bool classof(const CXXStaticCastExpr *) { return true; } 160}; 161 162/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 163/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 164/// determine how to perform the type cast. 165/// 166/// This expression node represents a dynamic cast, e.g., 167/// @c dynamic_cast<Derived*>(BasePtr). 168class CXXDynamicCastExpr : public CXXNamedCastExpr { 169public: 170 CXXDynamicCastExpr(QualType ty, CastKind kind, Expr *op, QualType writtenTy, 171 SourceLocation l) 172 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, kind, op, writtenTy, l) {} 173 174 static bool classof(const Stmt *T) { 175 return T->getStmtClass() == CXXDynamicCastExprClass; 176 } 177 static bool classof(const CXXDynamicCastExpr *) { return true; } 178}; 179 180/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 181/// [expr.reinterpret.cast]), which provides a differently-typed view 182/// of a value but performs no actual work at run time. 183/// 184/// This expression node represents a reinterpret cast, e.g., 185/// @c reinterpret_cast<int>(VoidPtr). 186class CXXReinterpretCastExpr : public CXXNamedCastExpr { 187public: 188 CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy, 189 SourceLocation l) 190 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, CK_BitCast, op, 191 writtenTy, l) {} 192 193 static bool classof(const Stmt *T) { 194 return T->getStmtClass() == CXXReinterpretCastExprClass; 195 } 196 static bool classof(const CXXReinterpretCastExpr *) { return true; } 197}; 198 199/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 200/// which can remove type qualifiers but does not change the underlying value. 201/// 202/// This expression node represents a const cast, e.g., 203/// @c const_cast<char*>(PtrToConstChar). 204class CXXConstCastExpr : public CXXNamedCastExpr { 205public: 206 CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy, 207 SourceLocation l) 208 : CXXNamedCastExpr(CXXConstCastExprClass, ty, CK_NoOp, op, writtenTy, l) {} 209 210 static bool classof(const Stmt *T) { 211 return T->getStmtClass() == CXXConstCastExprClass; 212 } 213 static bool classof(const CXXConstCastExpr *) { return true; } 214}; 215 216/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 217/// 218class CXXBoolLiteralExpr : public Expr { 219 bool Value; 220 SourceLocation Loc; 221public: 222 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 223 Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {} 224 225 bool getValue() const { return Value; } 226 227 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 228 229 static bool classof(const Stmt *T) { 230 return T->getStmtClass() == CXXBoolLiteralExprClass; 231 } 232 static bool classof(const CXXBoolLiteralExpr *) { return true; } 233 234 // Iterators 235 virtual child_iterator child_begin(); 236 virtual child_iterator child_end(); 237}; 238 239/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal 240class CXXNullPtrLiteralExpr : public Expr { 241 SourceLocation Loc; 242public: 243 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) : 244 Expr(CXXNullPtrLiteralExprClass, Ty), Loc(l) {} 245 246 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 247 248 static bool classof(const Stmt *T) { 249 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 250 } 251 static bool classof(const CXXNullPtrLiteralExpr *) { return true; } 252 253 virtual child_iterator child_begin(); 254 virtual child_iterator child_end(); 255}; 256 257/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 258/// the type_info that corresponds to the supplied type, or the (possibly 259/// dynamic) type of the supplied expression. 260/// 261/// This represents code like @c typeid(int) or @c typeid(*objPtr) 262class CXXTypeidExpr : public Expr { 263private: 264 bool isTypeOp : 1; 265 union { 266 void *Ty; 267 Stmt *Ex; 268 } Operand; 269 SourceRange Range; 270 271public: 272 CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) : 273 Expr(CXXTypeidExprClass, Ty, 274 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 275 false, 276 // typeid is value-dependent if the type or expression are dependent 277 (isTypeOp ? QualType::getFromOpaquePtr(op)->isDependentType() 278 : static_cast<Expr*>(op)->isValueDependent())), 279 isTypeOp(isTypeOp), Range(r) { 280 if (isTypeOp) 281 Operand.Ty = op; 282 else 283 // op was an Expr*, so cast it back to that to be safe 284 Operand.Ex = static_cast<Expr*>(op); 285 } 286 287 bool isTypeOperand() const { return isTypeOp; } 288 QualType getTypeOperand() const { 289 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 290 return QualType::getFromOpaquePtr(Operand.Ty); 291 } 292 Expr* getExprOperand() const { 293 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 294 return static_cast<Expr*>(Operand.Ex); 295 } 296 297 virtual SourceRange getSourceRange() const { 298 return Range; 299 } 300 static bool classof(const Stmt *T) { 301 return T->getStmtClass() == CXXTypeidExprClass; 302 } 303 static bool classof(const CXXTypeidExpr *) { return true; } 304 305 // Iterators 306 virtual child_iterator child_begin(); 307 virtual child_iterator child_end(); 308}; 309 310/// CXXThisExpr - Represents the "this" expression in C++, which is a 311/// pointer to the object on which the current member function is 312/// executing (C++ [expr.prim]p3). Example: 313/// 314/// @code 315/// class Foo { 316/// public: 317/// void bar(); 318/// void test() { this->bar(); } 319/// }; 320/// @endcode 321class CXXThisExpr : public Expr { 322 SourceLocation Loc; 323 324public: 325 CXXThisExpr(SourceLocation L, QualType Type) 326 : Expr(CXXThisExprClass, Type, 327 // 'this' is type-dependent if the class type of the enclosing 328 // member function is dependent (C++ [temp.dep.expr]p2) 329 Type->isDependentType(), Type->isDependentType()), 330 Loc(L) { } 331 332 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 333 334 static bool classof(const Stmt *T) { 335 return T->getStmtClass() == CXXThisExprClass; 336 } 337 static bool classof(const CXXThisExpr *) { return true; } 338 339 // Iterators 340 virtual child_iterator child_begin(); 341 virtual child_iterator child_end(); 342}; 343 344/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 345/// 'throw' and 'throw' assignment-expression. When 346/// assignment-expression isn't present, Op will be null. 347/// 348class CXXThrowExpr : public Expr { 349 Stmt *Op; 350 SourceLocation ThrowLoc; 351public: 352 // Ty is the void type which is used as the result type of the 353 // exepression. The l is the location of the throw keyword. expr 354 // can by null, if the optional expression to throw isn't present. 355 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 356 Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {} 357 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 358 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 359 void setSubExpr(Expr *E) { Op = E; } 360 361 SourceLocation getThrowLoc() const { return ThrowLoc; } 362 void setThrowLoc(SourceLocation L) { ThrowLoc = L; } 363 364 virtual SourceRange getSourceRange() const { 365 if (getSubExpr() == 0) 366 return SourceRange(ThrowLoc, ThrowLoc); 367 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 368 } 369 370 static bool classof(const Stmt *T) { 371 return T->getStmtClass() == CXXThrowExprClass; 372 } 373 static bool classof(const CXXThrowExpr *) { return true; } 374 375 // Iterators 376 virtual child_iterator child_begin(); 377 virtual child_iterator child_end(); 378}; 379 380/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 381/// function call argument that was created from the corresponding 382/// parameter's default argument, when the call did not explicitly 383/// supply arguments for all of the parameters. 384class CXXDefaultArgExpr : public Expr { 385 ParmVarDecl *Param; 386 387protected: 388 CXXDefaultArgExpr(StmtClass SC, ParmVarDecl *param) 389 : Expr(SC, param->hasUnparsedDefaultArg() ? 390 param->getType().getNonReferenceType() 391 : param->getDefaultArg()->getType()), 392 Param(param) { } 393 394public: 395 // Param is the parameter whose default argument is used by this 396 // expression. 397 static CXXDefaultArgExpr *Create(ASTContext &C, ParmVarDecl *Param) { 398 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Param); 399 } 400 401 // Retrieve the parameter that the argument was created from. 402 const ParmVarDecl *getParam() const { return Param; } 403 ParmVarDecl *getParam() { return Param; } 404 405 // Retrieve the actual argument to the function call. 406 const Expr *getExpr() const { return Param->getDefaultArg(); } 407 Expr *getExpr() { return Param->getDefaultArg(); } 408 409 virtual SourceRange getSourceRange() const { 410 // Default argument expressions have no representation in the 411 // source, so they have an empty source range. 412 return SourceRange(); 413 } 414 415 static bool classof(const Stmt *T) { 416 return T->getStmtClass() == CXXDefaultArgExprClass; 417 } 418 static bool classof(const CXXDefaultArgExpr *) { return true; } 419 420 // Iterators 421 virtual child_iterator child_begin(); 422 virtual child_iterator child_end(); 423}; 424 425/// CXXTemporary - Represents a C++ temporary. 426class CXXTemporary { 427 /// Destructor - The destructor that needs to be called. 428 const CXXDestructorDecl *Destructor; 429 430 CXXTemporary(const CXXDestructorDecl *destructor) 431 : Destructor(destructor) { } 432 ~CXXTemporary() { } 433 434public: 435 static CXXTemporary *Create(ASTContext &C, 436 const CXXDestructorDecl *Destructor); 437 438 void Destroy(ASTContext &Ctx); 439 440 const CXXDestructorDecl *getDestructor() const { return Destructor; } 441}; 442 443/// CXXBindTemporaryExpr - Represents binding an expression to a temporary, 444/// so its destructor can be called later. 445class CXXBindTemporaryExpr : public Expr { 446 CXXTemporary *Temp; 447 448 Stmt *SubExpr; 449 450 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* subexpr) 451 : Expr(CXXBindTemporaryExprClass, 452 subexpr->getType()), Temp(temp), SubExpr(subexpr) { } 453 ~CXXBindTemporaryExpr() { } 454 455protected: 456 virtual void DoDestroy(ASTContext &C); 457 458public: 459 static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp, 460 Expr* SubExpr); 461 462 CXXTemporary *getTemporary() { return Temp; } 463 const CXXTemporary *getTemporary() const { return Temp; } 464 465 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 466 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 467 void setSubExpr(Expr *E) { SubExpr = E; } 468 469 virtual SourceRange getSourceRange() const { return SourceRange(); } 470 471 // Implement isa/cast/dyncast/etc. 472 static bool classof(const Stmt *T) { 473 return T->getStmtClass() == CXXBindTemporaryExprClass; 474 } 475 static bool classof(const CXXBindTemporaryExpr *) { return true; } 476 477 // Iterators 478 virtual child_iterator child_begin(); 479 virtual child_iterator child_end(); 480}; 481 482/// CXXConstructExpr - Represents a call to a C++ constructor. 483class CXXConstructExpr : public Expr { 484 CXXConstructorDecl *Constructor; 485 486 bool Elidable; 487 488 Stmt **Args; 489 unsigned NumArgs; 490 491protected: 492 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 493 CXXConstructorDecl *d, bool elidable, 494 Expr **args, unsigned numargs); 495 ~CXXConstructExpr() { } 496 497 virtual void DoDestroy(ASTContext &C); 498 499public: 500 /// \brief Construct an empty C++ construction expression that will store 501 /// \p numargs arguments. 502 CXXConstructExpr(EmptyShell Empty, ASTContext &C, unsigned numargs); 503 504 static CXXConstructExpr *Create(ASTContext &C, QualType T, 505 CXXConstructorDecl *D, bool Elidable, 506 Expr **Args, unsigned NumArgs); 507 508 509 CXXConstructorDecl* getConstructor() const { return Constructor; } 510 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 511 512 /// \brief Whether this construction is elidable. 513 bool isElidable() const { return Elidable; } 514 void setElidable(bool E) { Elidable = E; } 515 516 typedef ExprIterator arg_iterator; 517 typedef ConstExprIterator const_arg_iterator; 518 519 arg_iterator arg_begin() { return Args; } 520 arg_iterator arg_end() { return Args + NumArgs; } 521 const_arg_iterator arg_begin() const { return Args; } 522 const_arg_iterator arg_end() const { return Args + NumArgs; } 523 524 unsigned getNumArgs() const { return NumArgs; } 525 526 /// getArg - Return the specified argument. 527 Expr *getArg(unsigned Arg) { 528 assert(Arg < NumArgs && "Arg access out of range!"); 529 return cast<Expr>(Args[Arg]); 530 } 531 const Expr *getArg(unsigned Arg) const { 532 assert(Arg < NumArgs && "Arg access out of range!"); 533 return cast<Expr>(Args[Arg]); 534 } 535 536 /// setArg - Set the specified argument. 537 void setArg(unsigned Arg, Expr *ArgExpr) { 538 assert(Arg < NumArgs && "Arg access out of range!"); 539 Args[Arg] = ArgExpr; 540 } 541 542 virtual SourceRange getSourceRange() const { return SourceRange(); } 543 544 static bool classof(const Stmt *T) { 545 return T->getStmtClass() == CXXConstructExprClass || 546 T->getStmtClass() == CXXTemporaryObjectExprClass; 547 } 548 static bool classof(const CXXConstructExpr *) { return true; } 549 550 // Iterators 551 virtual child_iterator child_begin(); 552 virtual child_iterator child_end(); 553}; 554 555/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 556/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 557/// x = int(0.5); 558class CXXFunctionalCastExpr : public ExplicitCastExpr { 559 SourceLocation TyBeginLoc; 560 SourceLocation RParenLoc; 561public: 562 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 563 SourceLocation tyBeginLoc, CastKind kind, 564 Expr *castExpr, SourceLocation rParenLoc) 565 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, kind, castExpr, 566 writtenTy), 567 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 568 569 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 570 SourceLocation getRParenLoc() const { return RParenLoc; } 571 572 virtual SourceRange getSourceRange() const { 573 return SourceRange(TyBeginLoc, RParenLoc); 574 } 575 static bool classof(const Stmt *T) { 576 return T->getStmtClass() == CXXFunctionalCastExprClass; 577 } 578 static bool classof(const CXXFunctionalCastExpr *) { return true; } 579}; 580 581/// @brief Represents a C++ functional cast expression that builds a 582/// temporary object. 583/// 584/// This expression type represents a C++ "functional" cast 585/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 586/// constructor to build a temporary object. If N == 0 but no 587/// constructor will be called (because the functional cast is 588/// performing a value-initialized an object whose class type has no 589/// user-declared constructors), CXXZeroInitValueExpr will represent 590/// the functional cast. Finally, with N == 1 arguments the functional 591/// cast expression will be represented by CXXFunctionalCastExpr. 592/// Example: 593/// @code 594/// struct X { X(int, float); } 595/// 596/// X create_X() { 597/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 598/// }; 599/// @endcode 600class CXXTemporaryObjectExpr : public CXXConstructExpr { 601 SourceLocation TyBeginLoc; 602 SourceLocation RParenLoc; 603 604public: 605 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 606 QualType writtenTy, SourceLocation tyBeginLoc, 607 Expr **Args,unsigned NumArgs, 608 SourceLocation rParenLoc); 609 610 ~CXXTemporaryObjectExpr() { } 611 612 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 613 SourceLocation getRParenLoc() const { return RParenLoc; } 614 615 virtual SourceRange getSourceRange() const { 616 return SourceRange(TyBeginLoc, RParenLoc); 617 } 618 static bool classof(const Stmt *T) { 619 return T->getStmtClass() == CXXTemporaryObjectExprClass; 620 } 621 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 622}; 623 624/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 625/// Expression "T()" which creates a value-initialized rvalue of type 626/// T, which is either a non-class type or a class type without any 627/// user-defined constructors. 628/// 629class CXXZeroInitValueExpr : public Expr { 630 SourceLocation TyBeginLoc; 631 SourceLocation RParenLoc; 632 633public: 634 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 635 SourceLocation rParenLoc ) : 636 Expr(CXXZeroInitValueExprClass, ty, false, false), 637 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 638 639 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 640 SourceLocation getRParenLoc() const { return RParenLoc; } 641 642 /// @brief Whether this initialization expression was 643 /// implicitly-generated. 644 bool isImplicit() const { 645 return TyBeginLoc.isInvalid() && RParenLoc.isInvalid(); 646 } 647 648 virtual SourceRange getSourceRange() const { 649 return SourceRange(TyBeginLoc, RParenLoc); 650 } 651 652 static bool classof(const Stmt *T) { 653 return T->getStmtClass() == CXXZeroInitValueExprClass; 654 } 655 static bool classof(const CXXZeroInitValueExpr *) { return true; } 656 657 // Iterators 658 virtual child_iterator child_begin(); 659 virtual child_iterator child_end(); 660}; 661 662/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 663/// statement, e.g: "if (int x = f()) {...}". 664/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 665/// decl that it references. 666/// 667class CXXConditionDeclExpr : public DeclRefExpr { 668public: 669 CXXConditionDeclExpr(SourceLocation startLoc, 670 SourceLocation eqLoc, VarDecl *var) 671 : DeclRefExpr(CXXConditionDeclExprClass, var, 672 var->getType().getNonReferenceType(), startLoc, 673 var->getType()->isDependentType(), 674 /*FIXME:integral constant?*/ 675 var->getType()->isDependentType()) {} 676 677 SourceLocation getStartLoc() const { return getLocation(); } 678 679 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 680 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 681 682 virtual SourceRange getSourceRange() const { 683 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 684 } 685 686 static bool classof(const Stmt *T) { 687 return T->getStmtClass() == CXXConditionDeclExprClass; 688 } 689 static bool classof(const CXXConditionDeclExpr *) { return true; } 690 691 // Iterators 692 virtual child_iterator child_begin(); 693 virtual child_iterator child_end(); 694}; 695 696/// CXXNewExpr - A new expression for memory allocation and constructor calls, 697/// e.g: "new CXXNewExpr(foo)". 698class CXXNewExpr : public Expr { 699 // Was the usage ::new, i.e. is the global new to be used? 700 bool GlobalNew : 1; 701 // Was the form (type-id) used? Otherwise, it was new-type-id. 702 bool ParenTypeId : 1; 703 // Is there an initializer? If not, built-ins are uninitialized, else they're 704 // value-initialized. 705 bool Initializer : 1; 706 // Do we allocate an array? If so, the first SubExpr is the size expression. 707 bool Array : 1; 708 // The number of placement new arguments. 709 unsigned NumPlacementArgs : 14; 710 // The number of constructor arguments. This may be 1 even for non-class 711 // types; use the pseudo copy constructor. 712 unsigned NumConstructorArgs : 14; 713 // Contains an optional array size expression, any number of optional 714 // placement arguments, and any number of optional constructor arguments, 715 // in that order. 716 Stmt **SubExprs; 717 // Points to the allocation function used. 718 FunctionDecl *OperatorNew; 719 // Points to the deallocation function used in case of error. May be null. 720 FunctionDecl *OperatorDelete; 721 // Points to the constructor used. Cannot be null if AllocType is a record; 722 // it would still point at the default constructor (even an implicit one). 723 // Must be null for all other types. 724 CXXConstructorDecl *Constructor; 725 726 SourceLocation StartLoc; 727 SourceLocation EndLoc; 728 729public: 730 CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs, 731 unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize, 732 CXXConstructorDecl *constructor, bool initializer, 733 Expr **constructorArgs, unsigned numConsArgs, 734 FunctionDecl *operatorDelete, QualType ty, 735 SourceLocation startLoc, SourceLocation endLoc); 736 ~CXXNewExpr() { 737 delete[] SubExprs; 738 } 739 740 QualType getAllocatedType() const { 741 assert(getType()->isPointerType()); 742 return getType()->getAs<PointerType>()->getPointeeType(); 743 } 744 745 FunctionDecl *getOperatorNew() const { return OperatorNew; } 746 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 747 CXXConstructorDecl *getConstructor() const { return Constructor; } 748 749 bool isArray() const { return Array; } 750 Expr *getArraySize() { 751 return Array ? cast<Expr>(SubExprs[0]) : 0; 752 } 753 const Expr *getArraySize() const { 754 return Array ? cast<Expr>(SubExprs[0]) : 0; 755 } 756 757 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 758 Expr *getPlacementArg(unsigned i) { 759 assert(i < NumPlacementArgs && "Index out of range"); 760 return cast<Expr>(SubExprs[Array + i]); 761 } 762 const Expr *getPlacementArg(unsigned i) const { 763 assert(i < NumPlacementArgs && "Index out of range"); 764 return cast<Expr>(SubExprs[Array + i]); 765 } 766 767 bool isGlobalNew() const { return GlobalNew; } 768 bool isParenTypeId() const { return ParenTypeId; } 769 bool hasInitializer() const { return Initializer; } 770 771 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 772 Expr *getConstructorArg(unsigned i) { 773 assert(i < NumConstructorArgs && "Index out of range"); 774 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 775 } 776 const Expr *getConstructorArg(unsigned i) const { 777 assert(i < NumConstructorArgs && "Index out of range"); 778 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 779 } 780 781 typedef ExprIterator arg_iterator; 782 typedef ConstExprIterator const_arg_iterator; 783 784 arg_iterator placement_arg_begin() { 785 return SubExprs + Array; 786 } 787 arg_iterator placement_arg_end() { 788 return SubExprs + Array + getNumPlacementArgs(); 789 } 790 const_arg_iterator placement_arg_begin() const { 791 return SubExprs + Array; 792 } 793 const_arg_iterator placement_arg_end() const { 794 return SubExprs + Array + getNumPlacementArgs(); 795 } 796 797 arg_iterator constructor_arg_begin() { 798 return SubExprs + Array + getNumPlacementArgs(); 799 } 800 arg_iterator constructor_arg_end() { 801 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 802 } 803 const_arg_iterator constructor_arg_begin() const { 804 return SubExprs + Array + getNumPlacementArgs(); 805 } 806 const_arg_iterator constructor_arg_end() const { 807 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 808 } 809 810 virtual SourceRange getSourceRange() const { 811 return SourceRange(StartLoc, EndLoc); 812 } 813 814 static bool classof(const Stmt *T) { 815 return T->getStmtClass() == CXXNewExprClass; 816 } 817 static bool classof(const CXXNewExpr *) { return true; } 818 819 // Iterators 820 virtual child_iterator child_begin(); 821 virtual child_iterator child_end(); 822}; 823 824/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 825/// calls, e.g. "delete[] pArray". 826class CXXDeleteExpr : public Expr { 827 // Is this a forced global delete, i.e. "::delete"? 828 bool GlobalDelete : 1; 829 // Is this the array form of delete, i.e. "delete[]"? 830 bool ArrayForm : 1; 831 // Points to the operator delete overload that is used. Could be a member. 832 FunctionDecl *OperatorDelete; 833 // The pointer expression to be deleted. 834 Stmt *Argument; 835 // Location of the expression. 836 SourceLocation Loc; 837public: 838 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 839 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 840 : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete), 841 ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg), 842 Loc(loc) { } 843 844 bool isGlobalDelete() const { return GlobalDelete; } 845 bool isArrayForm() const { return ArrayForm; } 846 847 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 848 849 Expr *getArgument() { return cast<Expr>(Argument); } 850 const Expr *getArgument() const { return cast<Expr>(Argument); } 851 852 virtual SourceRange getSourceRange() const { 853 return SourceRange(Loc, Argument->getLocEnd()); 854 } 855 856 static bool classof(const Stmt *T) { 857 return T->getStmtClass() == CXXDeleteExprClass; 858 } 859 static bool classof(const CXXDeleteExpr *) { return true; } 860 861 // Iterators 862 virtual child_iterator child_begin(); 863 virtual child_iterator child_end(); 864}; 865 866/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 867/// 868/// Example: 869/// 870/// \code 871/// template<typename T> 872/// void destroy(T* ptr) { 873/// ptr->~T(); 874/// } 875/// \endcode 876/// 877/// When the template is parsed, the expression \c ptr->~T will be stored as 878/// a member reference expression. If it then instantiated with a scalar type 879/// as a template argument for T, the resulting expression will be a 880/// pseudo-destructor expression. 881class CXXPseudoDestructorExpr : public Expr { 882 /// \brief The base expression (that is being destroyed). 883 Stmt *Base; 884 885 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 886 /// period ('.'). 887 bool IsArrow : 1; 888 889 /// \brief The location of the '.' or '->' operator. 890 SourceLocation OperatorLoc; 891 892 /// \brief The nested-name-specifier that follows the operator, if present. 893 NestedNameSpecifier *Qualifier; 894 895 /// \brief The source range that covers the nested-name-specifier, if 896 /// present. 897 SourceRange QualifierRange; 898 899 /// \brief The type being destroyed. 900 QualType DestroyedType; 901 902 /// \brief The location of the type after the '~'. 903 SourceLocation DestroyedTypeLoc; 904 905public: 906 CXXPseudoDestructorExpr(ASTContext &Context, 907 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 908 NestedNameSpecifier *Qualifier, 909 SourceRange QualifierRange, 910 QualType DestroyedType, 911 SourceLocation DestroyedTypeLoc) 912 : Expr(CXXPseudoDestructorExprClass, 913 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0, 914 false, 0)), 915 /*isTypeDependent=*/false, 916 /*isValueDependent=*/Base->isValueDependent()), 917 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), 918 OperatorLoc(OperatorLoc), Qualifier(Qualifier), 919 QualifierRange(QualifierRange), DestroyedType(DestroyedType), 920 DestroyedTypeLoc(DestroyedTypeLoc) { } 921 922 void setBase(Expr *E) { Base = E; } 923 Expr *getBase() const { return cast<Expr>(Base); } 924 925 /// \brief Determines whether this member expression actually had 926 /// a C++ nested-name-specifier prior to the name of the member, e.g., 927 /// x->Base::foo. 928 bool hasQualifier() const { return Qualifier != 0; } 929 930 /// \brief If the member name was qualified, retrieves the source range of 931 /// the nested-name-specifier that precedes the member name. Otherwise, 932 /// returns an empty source range. 933 SourceRange getQualifierRange() const { return QualifierRange; } 934 935 /// \brief If the member name was qualified, retrieves the 936 /// nested-name-specifier that precedes the member name. Otherwise, returns 937 /// NULL. 938 NestedNameSpecifier *getQualifier() const { return Qualifier; } 939 940 /// \brief Determine whether this pseudo-destructor expression was written 941 /// using an '->' (otherwise, it used a '.'). 942 bool isArrow() const { return IsArrow; } 943 void setArrow(bool A) { IsArrow = A; } 944 945 /// \brief Retrieve the location of the '.' or '->' operator. 946 SourceLocation getOperatorLoc() const { return OperatorLoc; } 947 948 /// \brief Retrieve the type that is being destroyed. 949 QualType getDestroyedType() const { return DestroyedType; } 950 951 /// \brief Retrieve the location of the type being destroyed. 952 SourceLocation getDestroyedTypeLoc() const { return DestroyedTypeLoc; } 953 954 virtual SourceRange getSourceRange() const { 955 return SourceRange(Base->getLocStart(), DestroyedTypeLoc); 956 } 957 958 static bool classof(const Stmt *T) { 959 return T->getStmtClass() == CXXPseudoDestructorExprClass; 960 } 961 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 962 963 // Iterators 964 virtual child_iterator child_begin(); 965 virtual child_iterator child_end(); 966}; 967 968/// \brief Represents the name of a function that has not been 969/// resolved to any declaration. 970/// 971/// Unresolved function names occur when a function name is 972/// encountered prior to an open parentheses ('(') in a C++ function 973/// call, and the function name itself did not resolve to a 974/// declaration. These function names can only be resolved when they 975/// form the postfix-expression of a function call, so that 976/// argument-dependent lookup finds declarations corresponding to 977/// these functions. 978 979/// @code 980/// template<typename T> void f(T x) { 981/// g(x); // g is an unresolved function name (that is also a dependent name) 982/// } 983/// @endcode 984class UnresolvedFunctionNameExpr : public Expr { 985 /// The name that was present in the source 986 DeclarationName Name; 987 988 /// The location of this name in the source code 989 SourceLocation Loc; 990 991public: 992 UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L) 993 : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { } 994 995 /// \brief Retrieves the name that occurred in the source code. 996 DeclarationName getName() const { return Name; } 997 998 /// getLocation - Retrieves the location in the source code where 999 /// the name occurred. 1000 SourceLocation getLocation() const { return Loc; } 1001 1002 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 1003 1004 static bool classof(const Stmt *T) { 1005 return T->getStmtClass() == UnresolvedFunctionNameExprClass; 1006 } 1007 static bool classof(const UnresolvedFunctionNameExpr *) { return true; } 1008 1009 // Iterators 1010 virtual child_iterator child_begin(); 1011 virtual child_iterator child_end(); 1012}; 1013 1014/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1015/// implementation of TR1/C++0x type trait templates. 1016/// Example: 1017/// __is_pod(int) == true 1018/// __is_enum(std::string) == false 1019class UnaryTypeTraitExpr : public Expr { 1020 /// UTT - The trait. 1021 UnaryTypeTrait UTT; 1022 1023 /// Loc - The location of the type trait keyword. 1024 SourceLocation Loc; 1025 1026 /// RParen - The location of the closing paren. 1027 SourceLocation RParen; 1028 1029 /// QueriedType - The type we're testing. 1030 QualType QueriedType; 1031 1032public: 1033 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried, 1034 SourceLocation rparen, QualType ty) 1035 : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()), 1036 UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { } 1037 1038 virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1039 1040 UnaryTypeTrait getTrait() const { return UTT; } 1041 1042 QualType getQueriedType() const { return QueriedType; } 1043 1044 bool EvaluateTrait(ASTContext&) const; 1045 1046 static bool classof(const Stmt *T) { 1047 return T->getStmtClass() == UnaryTypeTraitExprClass; 1048 } 1049 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1050 1051 // Iterators 1052 virtual child_iterator child_begin(); 1053 virtual child_iterator child_end(); 1054}; 1055 1056/// QualifiedDeclRefExpr - A reference to a declared variable, 1057/// function, enum, etc., that includes a qualification, e.g., 1058/// "N::foo". 1059class QualifiedDeclRefExpr : public DeclRefExpr { 1060 /// QualifierRange - The source range that covers the 1061 /// nested-name-specifier. 1062 SourceRange QualifierRange; 1063 1064 /// \brief The nested-name-specifier that qualifies this declaration 1065 /// name. 1066 NestedNameSpecifier *NNS; 1067 1068public: 1069 QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, 1070 bool VD, SourceRange R, NestedNameSpecifier *NNS) 1071 : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD), 1072 QualifierRange(R), NNS(NNS) { } 1073 1074 /// \brief Retrieve the source range of the nested-name-specifier. 1075 SourceRange getQualifierRange() const { return QualifierRange; } 1076 1077 /// \brief Retrieve the nested-name-specifier that qualifies this 1078 /// declaration. 1079 NestedNameSpecifier *getQualifier() const { return NNS; } 1080 1081 virtual SourceRange getSourceRange() const { 1082 return SourceRange(QualifierRange.getBegin(), getLocation()); 1083 } 1084 1085 static bool classof(const Stmt *T) { 1086 return T->getStmtClass() == QualifiedDeclRefExprClass; 1087 } 1088 static bool classof(const QualifiedDeclRefExpr *) { return true; } 1089}; 1090 1091/// \brief A qualified reference to a name whose declaration cannot 1092/// yet be resolved. 1093/// 1094/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that 1095/// it expresses a qualified reference to a declaration such as 1096/// X<T>::value. The difference, however, is that an 1097/// UnresolvedDeclRefExpr node is used only within C++ templates when 1098/// the qualification (e.g., X<T>::) refers to a dependent type. In 1099/// this case, X<T>::value cannot resolve to a declaration because the 1100/// declaration will differ from on instantiation of X<T> to the 1101/// next. Therefore, UnresolvedDeclRefExpr keeps track of the 1102/// qualifier (X<T>::) and the name of the entity being referenced 1103/// ("value"). Such expressions will instantiate to 1104/// QualifiedDeclRefExprs. 1105class UnresolvedDeclRefExpr : public Expr { 1106 /// The name of the entity we will be referencing. 1107 DeclarationName Name; 1108 1109 /// Location of the name of the declaration we're referencing. 1110 SourceLocation Loc; 1111 1112 /// QualifierRange - The source range that covers the 1113 /// nested-name-specifier. 1114 SourceRange QualifierRange; 1115 1116 /// \brief The nested-name-specifier that qualifies this unresolved 1117 /// declaration name. 1118 NestedNameSpecifier *NNS; 1119 1120 /// \brief Whether this expr is an address of (&) operand. 1121 bool IsAddressOfOperand; 1122 1123public: 1124 UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L, 1125 SourceRange R, NestedNameSpecifier *NNS, 1126 bool IsAddressOfOperand) 1127 : Expr(UnresolvedDeclRefExprClass, T, true, true), 1128 Name(N), Loc(L), QualifierRange(R), NNS(NNS), 1129 IsAddressOfOperand(IsAddressOfOperand) { } 1130 1131 /// \brief Retrieve the name that this expression refers to. 1132 DeclarationName getDeclName() const { return Name; } 1133 1134 /// \brief Retrieve the location of the name within the expression. 1135 SourceLocation getLocation() const { return Loc; } 1136 1137 /// \brief Retrieve the source range of the nested-name-specifier. 1138 SourceRange getQualifierRange() const { return QualifierRange; } 1139 1140 /// \brief Retrieve the nested-name-specifier that qualifies this 1141 /// declaration. 1142 NestedNameSpecifier *getQualifier() const { return NNS; } 1143 1144 /// \brief Retrieve whether this is an address of (&) operand. 1145 1146 bool isAddressOfOperand() const { return IsAddressOfOperand; } 1147 virtual SourceRange getSourceRange() const { 1148 return SourceRange(QualifierRange.getBegin(), getLocation()); 1149 } 1150 1151 static bool classof(const Stmt *T) { 1152 return T->getStmtClass() == UnresolvedDeclRefExprClass; 1153 } 1154 static bool classof(const UnresolvedDeclRefExpr *) { return true; } 1155 1156 virtual StmtIterator child_begin(); 1157 virtual StmtIterator child_end(); 1158}; 1159 1160/// \brief An expression that refers to a C++ template-id, such as 1161/// @c isa<FunctionDecl>. 1162class TemplateIdRefExpr : public Expr { 1163 /// \brief If this template-id was qualified-id, e.g., @c std::sort<int>, 1164 /// this nested name specifier contains the @c std::. 1165 NestedNameSpecifier *Qualifier; 1166 1167 /// \brief If this template-id was a qualified-id, e.g., @c std::sort<int>, 1168 /// this covers the source code range of the @c std::. 1169 SourceRange QualifierRange; 1170 1171 /// \brief The actual template to which this template-id refers. 1172 TemplateName Template; 1173 1174 /// \brief The source location of the template name. 1175 SourceLocation TemplateNameLoc; 1176 1177 /// \brief The source location of the left angle bracket ('<'); 1178 SourceLocation LAngleLoc; 1179 1180 /// \brief The source location of the right angle bracket ('>'); 1181 SourceLocation RAngleLoc; 1182 1183 /// \brief The number of template arguments in TemplateArgs. 1184 unsigned NumTemplateArgs; 1185 1186 TemplateIdRefExpr(QualType T, 1187 NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 1188 TemplateName Template, SourceLocation TemplateNameLoc, 1189 SourceLocation LAngleLoc, 1190 const TemplateArgument *TemplateArgs, 1191 unsigned NumTemplateArgs, 1192 SourceLocation RAngleLoc); 1193 1194 virtual void DoDestroy(ASTContext &Context); 1195 1196public: 1197 static TemplateIdRefExpr * 1198 Create(ASTContext &Context, QualType T, 1199 NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 1200 TemplateName Template, SourceLocation TemplateNameLoc, 1201 SourceLocation LAngleLoc, const TemplateArgument *TemplateArgs, 1202 unsigned NumTemplateArgs, SourceLocation RAngleLoc); 1203 1204 /// \brief Retrieve the nested name specifier used to qualify the name of 1205 /// this template-id, e.g., the "std::sort" in @c std::sort<int>, or NULL 1206 /// if this template-id was an unqualified-id. 1207 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1208 1209 /// \brief Retrieve the source range describing the nested name specifier 1210 /// used to qualified the name of this template-id, if the name was qualified. 1211 SourceRange getQualifierRange() const { return QualifierRange; } 1212 1213 /// \brief Retrieve the name of the template referenced, e.g., "sort" in 1214 /// @c std::sort<int>; 1215 TemplateName getTemplateName() const { return Template; } 1216 1217 /// \brief Retrieve the location of the name of the template referenced, e.g., 1218 /// the location of "sort" in @c std::sort<int>. 1219 SourceLocation getTemplateNameLoc() const { return TemplateNameLoc; } 1220 1221 /// \brief Retrieve the location of the left angle bracket following the 1222 /// template name ('<'). 1223 SourceLocation getLAngleLoc() const { return LAngleLoc; } 1224 1225 /// \brief Retrieve the template arguments provided as part of this 1226 /// template-id. 1227 const TemplateArgument *getTemplateArgs() const { 1228 return reinterpret_cast<const TemplateArgument *>(this + 1); 1229 } 1230 1231 /// \brief Retrieve the number of template arguments provided as part of this 1232 /// template-id. 1233 unsigned getNumTemplateArgs() const { return NumTemplateArgs; } 1234 1235 /// \brief Retrieve the location of the right angle bracket following the 1236 /// template arguments ('>'). 1237 SourceLocation getRAngleLoc() const { return RAngleLoc; } 1238 1239 virtual SourceRange getSourceRange() const { 1240 return SourceRange(Qualifier? QualifierRange.getBegin() : TemplateNameLoc, 1241 RAngleLoc); 1242 } 1243 1244 // Iterators 1245 virtual child_iterator child_begin(); 1246 virtual child_iterator child_end(); 1247 1248 static bool classof(const Stmt *T) { 1249 return T->getStmtClass() == TemplateIdRefExprClass; 1250 } 1251 static bool classof(const TemplateIdRefExpr *) { return true; } 1252}; 1253 1254class CXXExprWithTemporaries : public Expr { 1255 Stmt *SubExpr; 1256 1257 CXXTemporary **Temps; 1258 unsigned NumTemps; 1259 1260 bool ShouldDestroyTemps; 1261 1262 CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps, 1263 unsigned NumTemps, bool ShouldDestroyTemps); 1264 ~CXXExprWithTemporaries(); 1265 1266protected: 1267 virtual void DoDestroy(ASTContext &C); 1268 1269public: 1270 static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr, 1271 CXXTemporary **Temps, unsigned NumTemps, 1272 bool ShouldDestroyTemporaries); 1273 1274 unsigned getNumTemporaries() const { return NumTemps; } 1275 CXXTemporary *getTemporary(unsigned i) { 1276 assert(i < NumTemps && "Index out of range"); 1277 return Temps[i]; 1278 } 1279 const CXXTemporary *getTemporary(unsigned i) const { 1280 assert(i < NumTemps && "Index out of range"); 1281 return Temps[i]; 1282 } 1283 1284 bool shouldDestroyTemporaries() const { return ShouldDestroyTemps; } 1285 1286 void removeLastTemporary() { NumTemps--; } 1287 1288 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 1289 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 1290 void setSubExpr(Expr *E) { SubExpr = E; } 1291 1292 virtual SourceRange getSourceRange() const { return SourceRange(); } 1293 1294 // Implement isa/cast/dyncast/etc. 1295 static bool classof(const Stmt *T) { 1296 return T->getStmtClass() == CXXExprWithTemporariesClass; 1297 } 1298 static bool classof(const CXXExprWithTemporaries *) { return true; } 1299 1300 // Iterators 1301 virtual child_iterator child_begin(); 1302 virtual child_iterator child_end(); 1303}; 1304 1305/// \brief Describes an explicit type conversion that uses functional 1306/// notion but could not be resolved because one or more arguments are 1307/// type-dependent. 1308/// 1309/// The explicit type conversions expressed by 1310/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 1311/// where \c T is some type and \c a1, a2, ..., aN are values, and 1312/// either \C T is a dependent type or one or more of the \c a's is 1313/// type-dependent. For example, this would occur in a template such 1314/// as: 1315/// 1316/// \code 1317/// template<typename T, typename A1> 1318/// inline T make_a(const A1& a1) { 1319/// return T(a1); 1320/// } 1321/// \endcode 1322/// 1323/// When the returned expression is instantiated, it may resolve to a 1324/// constructor call, conversion function call, or some kind of type 1325/// conversion. 1326class CXXUnresolvedConstructExpr : public Expr { 1327 /// \brief The starting location of the type 1328 SourceLocation TyBeginLoc; 1329 1330 /// \brief The type being constructed. 1331 QualType Type; 1332 1333 /// \brief The location of the left parentheses ('('). 1334 SourceLocation LParenLoc; 1335 1336 /// \brief The location of the right parentheses (')'). 1337 SourceLocation RParenLoc; 1338 1339 /// \brief The number of arguments used to construct the type. 1340 unsigned NumArgs; 1341 1342 CXXUnresolvedConstructExpr(SourceLocation TyBegin, 1343 QualType T, 1344 SourceLocation LParenLoc, 1345 Expr **Args, 1346 unsigned NumArgs, 1347 SourceLocation RParenLoc); 1348 1349public: 1350 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 1351 SourceLocation TyBegin, 1352 QualType T, 1353 SourceLocation LParenLoc, 1354 Expr **Args, 1355 unsigned NumArgs, 1356 SourceLocation RParenLoc); 1357 1358 /// \brief Retrieve the source location where the type begins. 1359 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 1360 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 1361 1362 /// \brief Retrieve the type that is being constructed, as specified 1363 /// in the source code. 1364 QualType getTypeAsWritten() const { return Type; } 1365 void setTypeAsWritten(QualType T) { Type = T; } 1366 1367 /// \brief Retrieve the location of the left parentheses ('(') that 1368 /// precedes the argument list. 1369 SourceLocation getLParenLoc() const { return LParenLoc; } 1370 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1371 1372 /// \brief Retrieve the location of the right parentheses (')') that 1373 /// follows the argument list. 1374 SourceLocation getRParenLoc() const { return RParenLoc; } 1375 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1376 1377 /// \brief Retrieve the number of arguments. 1378 unsigned arg_size() const { return NumArgs; } 1379 1380 typedef Expr** arg_iterator; 1381 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 1382 arg_iterator arg_end() { return arg_begin() + NumArgs; } 1383 1384 Expr *getArg(unsigned I) { 1385 assert(I < NumArgs && "Argument index out-of-range"); 1386 return *(arg_begin() + I); 1387 } 1388 1389 virtual SourceRange getSourceRange() const { 1390 return SourceRange(TyBeginLoc, RParenLoc); 1391 } 1392 static bool classof(const Stmt *T) { 1393 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 1394 } 1395 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 1396 1397 // Iterators 1398 virtual child_iterator child_begin(); 1399 virtual child_iterator child_end(); 1400}; 1401 1402/// \brief Represents a C++ member access expression where the actual member 1403/// referenced could not be resolved, e.g., because the base expression or the 1404/// member name was dependent. 1405class CXXUnresolvedMemberExpr : public Expr { 1406 /// \brief The expression for the base pointer or class reference, 1407 /// e.g., the \c x in x.f. 1408 Stmt *Base; 1409 1410 /// \brief Whether this member expression used the '->' operator or 1411 /// the '.' operator. 1412 bool IsArrow : 1; 1413 1414 /// \brief Whether this member expression has explicitly-specified template 1415 /// arguments. 1416 bool HasExplicitTemplateArgumentList : 1; 1417 1418 /// \brief The location of the '->' or '.' operator. 1419 SourceLocation OperatorLoc; 1420 1421 /// \brief The nested-name-specifier that precedes the member name, if any. 1422 NestedNameSpecifier *Qualifier; 1423 1424 /// \brief The source range covering the nested name specifier. 1425 SourceRange QualifierRange; 1426 1427 /// \brief In a qualified member access expression such as t->Base::f, this 1428 /// member stores the resolves of name lookup in the context of the member 1429 /// access expression, to be used at instantiation time. 1430 /// 1431 /// FIXME: This member, along with the Qualifier and QualifierRange, could 1432 /// be stuck into a structure that is optionally allocated at the end of 1433 /// the CXXUnresolvedMemberExpr, to save space in the common case. 1434 NamedDecl *FirstQualifierFoundInScope; 1435 1436 /// \brief The member to which this member expression refers, which 1437 /// can be name, overloaded operator, or destructor. 1438 /// FIXME: could also be a template-id 1439 DeclarationName Member; 1440 1441 /// \brief The location of the member name. 1442 SourceLocation MemberLoc; 1443 1444 /// \brief Retrieve the explicit template argument list that followed the 1445 /// member template name, if any. 1446 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1447 if (!HasExplicitTemplateArgumentList) 1448 return 0; 1449 1450 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1451 } 1452 1453 /// \brief Retrieve the explicit template argument list that followed the 1454 /// member template name, if any. 1455 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1456 return const_cast<CXXUnresolvedMemberExpr *>(this) 1457 ->getExplicitTemplateArgumentList(); 1458 } 1459 1460 CXXUnresolvedMemberExpr(ASTContext &C, 1461 Expr *Base, bool IsArrow, 1462 SourceLocation OperatorLoc, 1463 NestedNameSpecifier *Qualifier, 1464 SourceRange QualifierRange, 1465 NamedDecl *FirstQualifierFoundInScope, 1466 DeclarationName Member, 1467 SourceLocation MemberLoc, 1468 bool HasExplicitTemplateArgs, 1469 SourceLocation LAngleLoc, 1470 const TemplateArgument *TemplateArgs, 1471 unsigned NumTemplateArgs, 1472 SourceLocation RAngleLoc); 1473 1474public: 1475 CXXUnresolvedMemberExpr(ASTContext &C, 1476 Expr *Base, bool IsArrow, 1477 SourceLocation OperatorLoc, 1478 NestedNameSpecifier *Qualifier, 1479 SourceRange QualifierRange, 1480 NamedDecl *FirstQualifierFoundInScope, 1481 DeclarationName Member, 1482 SourceLocation MemberLoc) 1483 : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true), 1484 Base(Base), IsArrow(IsArrow), HasExplicitTemplateArgumentList(false), 1485 OperatorLoc(OperatorLoc), 1486 Qualifier(Qualifier), QualifierRange(QualifierRange), 1487 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1488 Member(Member), MemberLoc(MemberLoc) { } 1489 1490 static CXXUnresolvedMemberExpr * 1491 Create(ASTContext &C, 1492 Expr *Base, bool IsArrow, 1493 SourceLocation OperatorLoc, 1494 NestedNameSpecifier *Qualifier, 1495 SourceRange QualifierRange, 1496 NamedDecl *FirstQualifierFoundInScope, 1497 DeclarationName Member, 1498 SourceLocation MemberLoc, 1499 bool HasExplicitTemplateArgs, 1500 SourceLocation LAngleLoc, 1501 const TemplateArgument *TemplateArgs, 1502 unsigned NumTemplateArgs, 1503 SourceLocation RAngleLoc); 1504 1505 /// \brief Retrieve the base object of this member expressions, 1506 /// e.g., the \c x in \c x.m. 1507 Expr *getBase() { return cast<Expr>(Base); } 1508 void setBase(Expr *E) { Base = E; } 1509 1510 /// \brief Determine whether this member expression used the '->' 1511 /// operator; otherwise, it used the '.' operator. 1512 bool isArrow() const { return IsArrow; } 1513 void setArrow(bool A) { IsArrow = A; } 1514 1515 /// \brief Retrieve the location of the '->' or '.' operator. 1516 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1517 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1518 1519 /// \brief Retrieve the nested-name-specifier that qualifies the member 1520 /// name. 1521 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1522 1523 /// \brief Retrieve the source range covering the nested-name-specifier 1524 /// that qualifies the member name. 1525 SourceRange getQualifierRange() const { return QualifierRange; } 1526 1527 /// \brief Retrieve the first part of the nested-name-specifier that was 1528 /// found in the scope of the member access expression when the member access 1529 /// was initially parsed. 1530 /// 1531 /// This function only returns a useful result when member access expression 1532 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 1533 /// returned by this function describes what was found by unqualified name 1534 /// lookup for the identifier "Base" within the scope of the member access 1535 /// expression itself. At template instantiation time, this information is 1536 /// combined with the results of name lookup into the type of the object 1537 /// expression itself (the class type of x). 1538 NamedDecl *getFirstQualifierFoundInScope() const { 1539 return FirstQualifierFoundInScope; 1540 } 1541 1542 /// \brief Retrieve the name of the member that this expression 1543 /// refers to. 1544 DeclarationName getMember() const { return Member; } 1545 void setMember(DeclarationName N) { Member = N; } 1546 1547 // \brief Retrieve the location of the name of the member that this 1548 // expression refers to. 1549 SourceLocation getMemberLoc() const { return MemberLoc; } 1550 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1551 1552 /// \brief Determines whether this member expression actually had a C++ 1553 /// template argument list explicitly specified, e.g., x.f<int>. 1554 bool hasExplicitTemplateArgumentList() { 1555 return HasExplicitTemplateArgumentList; 1556 } 1557 1558 /// \brief Retrieve the location of the left angle bracket following the 1559 /// member name ('<'), if any. 1560 SourceLocation getLAngleLoc() const { 1561 if (!HasExplicitTemplateArgumentList) 1562 return SourceLocation(); 1563 1564 return getExplicitTemplateArgumentList()->LAngleLoc; 1565 } 1566 1567 /// \brief Retrieve the template arguments provided as part of this 1568 /// template-id. 1569 const TemplateArgument *getTemplateArgs() const { 1570 if (!HasExplicitTemplateArgumentList) 1571 return 0; 1572 1573 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1574 } 1575 1576 /// \brief Retrieve the number of template arguments provided as part of this 1577 /// template-id. 1578 unsigned getNumTemplateArgs() const { 1579 if (!HasExplicitTemplateArgumentList) 1580 return 0; 1581 1582 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1583 } 1584 1585 /// \brief Retrieve the location of the right angle bracket following the 1586 /// template arguments ('>'). 1587 SourceLocation getRAngleLoc() const { 1588 if (!HasExplicitTemplateArgumentList) 1589 return SourceLocation(); 1590 1591 return getExplicitTemplateArgumentList()->RAngleLoc; 1592 } 1593 1594 virtual SourceRange getSourceRange() const { 1595 if (HasExplicitTemplateArgumentList) 1596 return SourceRange(Base->getSourceRange().getBegin(), 1597 getRAngleLoc()); 1598 1599 return SourceRange(Base->getSourceRange().getBegin(), 1600 MemberLoc); 1601 } 1602 1603 static bool classof(const Stmt *T) { 1604 return T->getStmtClass() == CXXUnresolvedMemberExprClass; 1605 } 1606 static bool classof(const CXXUnresolvedMemberExpr *) { return true; } 1607 1608 // Iterators 1609 virtual child_iterator child_begin(); 1610 virtual child_iterator child_end(); 1611}; 1612 1613} // end namespace clang 1614 1615#endif 1616