ExprCXX.h revision 96be6917e1c4ba1f4435a14c9e7c6c139571d086
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, CastKind kind, Expr *op, 117 QualType writtenTy, SourceLocation l) 118 : ExplicitCastExpr(SC, ty, kind, 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, CastKind kind, Expr *op, 153 QualType writtenTy, SourceLocation l) 154 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, kind, 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, CastKind kind, Expr *op, 189 QualType writtenTy, SourceLocation l) 190 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, kind, 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 { 470 return SubExpr->getSourceRange(); 471 } 472 473 // Implement isa/cast/dyncast/etc. 474 static bool classof(const Stmt *T) { 475 return T->getStmtClass() == CXXBindTemporaryExprClass; 476 } 477 static bool classof(const CXXBindTemporaryExpr *) { return true; } 478 479 // Iterators 480 virtual child_iterator child_begin(); 481 virtual child_iterator child_end(); 482}; 483 484/// CXXConstructExpr - Represents a call to a C++ constructor. 485class CXXConstructExpr : public Expr { 486 CXXConstructorDecl *Constructor; 487 488 bool Elidable; 489 490 Stmt **Args; 491 unsigned NumArgs; 492 493protected: 494 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 495 CXXConstructorDecl *d, bool elidable, 496 Expr **args, unsigned numargs); 497 ~CXXConstructExpr() { } 498 499 virtual void DoDestroy(ASTContext &C); 500 501public: 502 /// \brief Construct an empty C++ construction expression that will store 503 /// \p numargs arguments. 504 CXXConstructExpr(EmptyShell Empty, ASTContext &C, unsigned numargs); 505 506 static CXXConstructExpr *Create(ASTContext &C, QualType T, 507 CXXConstructorDecl *D, bool Elidable, 508 Expr **Args, unsigned NumArgs); 509 510 511 CXXConstructorDecl* getConstructor() const { return Constructor; } 512 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 513 514 /// \brief Whether this construction is elidable. 515 bool isElidable() const { return Elidable; } 516 void setElidable(bool E) { Elidable = E; } 517 518 typedef ExprIterator arg_iterator; 519 typedef ConstExprIterator const_arg_iterator; 520 521 arg_iterator arg_begin() { return Args; } 522 arg_iterator arg_end() { return Args + NumArgs; } 523 const_arg_iterator arg_begin() const { return Args; } 524 const_arg_iterator arg_end() const { return Args + NumArgs; } 525 526 unsigned getNumArgs() const { return NumArgs; } 527 528 /// getArg - Return the specified argument. 529 Expr *getArg(unsigned Arg) { 530 assert(Arg < NumArgs && "Arg access out of range!"); 531 return cast<Expr>(Args[Arg]); 532 } 533 const Expr *getArg(unsigned Arg) const { 534 assert(Arg < NumArgs && "Arg access out of range!"); 535 return cast<Expr>(Args[Arg]); 536 } 537 538 /// setArg - Set the specified argument. 539 void setArg(unsigned Arg, Expr *ArgExpr) { 540 assert(Arg < NumArgs && "Arg access out of range!"); 541 Args[Arg] = ArgExpr; 542 } 543 544 virtual SourceRange getSourceRange() const { 545 // FIXME: Should we know where the parentheses are, if there are any? 546 if (NumArgs == 0) 547 return SourceRange(); 548 549 return SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd()); 550 } 551 552 static bool classof(const Stmt *T) { 553 return T->getStmtClass() == CXXConstructExprClass || 554 T->getStmtClass() == CXXTemporaryObjectExprClass; 555 } 556 static bool classof(const CXXConstructExpr *) { return true; } 557 558 // Iterators 559 virtual child_iterator child_begin(); 560 virtual child_iterator child_end(); 561}; 562 563/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 564/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 565/// x = int(0.5); 566class CXXFunctionalCastExpr : public ExplicitCastExpr { 567 SourceLocation TyBeginLoc; 568 SourceLocation RParenLoc; 569public: 570 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 571 SourceLocation tyBeginLoc, CastKind kind, 572 Expr *castExpr, SourceLocation rParenLoc) 573 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, kind, castExpr, 574 writtenTy), 575 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 576 577 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 578 SourceLocation getRParenLoc() const { return RParenLoc; } 579 580 virtual SourceRange getSourceRange() const { 581 return SourceRange(TyBeginLoc, RParenLoc); 582 } 583 static bool classof(const Stmt *T) { 584 return T->getStmtClass() == CXXFunctionalCastExprClass; 585 } 586 static bool classof(const CXXFunctionalCastExpr *) { return true; } 587}; 588 589/// @brief Represents a C++ functional cast expression that builds a 590/// temporary object. 591/// 592/// This expression type represents a C++ "functional" cast 593/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 594/// constructor to build a temporary object. If N == 0 but no 595/// constructor will be called (because the functional cast is 596/// performing a value-initialized an object whose class type has no 597/// user-declared constructors), CXXZeroInitValueExpr will represent 598/// the functional cast. Finally, with N == 1 arguments the functional 599/// cast expression will be represented by CXXFunctionalCastExpr. 600/// Example: 601/// @code 602/// struct X { X(int, float); } 603/// 604/// X create_X() { 605/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 606/// }; 607/// @endcode 608class CXXTemporaryObjectExpr : public CXXConstructExpr { 609 SourceLocation TyBeginLoc; 610 SourceLocation RParenLoc; 611 612public: 613 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 614 QualType writtenTy, SourceLocation tyBeginLoc, 615 Expr **Args,unsigned NumArgs, 616 SourceLocation rParenLoc); 617 618 ~CXXTemporaryObjectExpr() { } 619 620 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 621 SourceLocation getRParenLoc() const { return RParenLoc; } 622 623 virtual SourceRange getSourceRange() const { 624 return SourceRange(TyBeginLoc, RParenLoc); 625 } 626 static bool classof(const Stmt *T) { 627 return T->getStmtClass() == CXXTemporaryObjectExprClass; 628 } 629 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 630}; 631 632/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 633/// Expression "T()" which creates a value-initialized rvalue of type 634/// T, which is either a non-class type or a class type without any 635/// user-defined constructors. 636/// 637class CXXZeroInitValueExpr : public Expr { 638 SourceLocation TyBeginLoc; 639 SourceLocation RParenLoc; 640 641public: 642 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 643 SourceLocation rParenLoc ) : 644 Expr(CXXZeroInitValueExprClass, ty, false, false), 645 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 646 647 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 648 SourceLocation getRParenLoc() const { return RParenLoc; } 649 650 /// @brief Whether this initialization expression was 651 /// implicitly-generated. 652 bool isImplicit() const { 653 return TyBeginLoc.isInvalid() && RParenLoc.isInvalid(); 654 } 655 656 virtual SourceRange getSourceRange() const { 657 return SourceRange(TyBeginLoc, RParenLoc); 658 } 659 660 static bool classof(const Stmt *T) { 661 return T->getStmtClass() == CXXZeroInitValueExprClass; 662 } 663 static bool classof(const CXXZeroInitValueExpr *) { return true; } 664 665 // Iterators 666 virtual child_iterator child_begin(); 667 virtual child_iterator child_end(); 668}; 669 670/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 671/// statement, e.g: "if (int x = f()) {...}". 672/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 673/// decl that it references. 674/// 675class CXXConditionDeclExpr : public DeclRefExpr { 676public: 677 CXXConditionDeclExpr(SourceLocation startLoc, 678 SourceLocation eqLoc, VarDecl *var) 679 : DeclRefExpr(CXXConditionDeclExprClass, var, 680 var->getType().getNonReferenceType(), startLoc, 681 var->getType()->isDependentType(), 682 /*FIXME:integral constant?*/ 683 var->getType()->isDependentType()) {} 684 685 SourceLocation getStartLoc() const { return getLocation(); } 686 687 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 688 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 689 690 virtual SourceRange getSourceRange() const { 691 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 692 } 693 694 static bool classof(const Stmt *T) { 695 return T->getStmtClass() == CXXConditionDeclExprClass; 696 } 697 static bool classof(const CXXConditionDeclExpr *) { return true; } 698 699 // Iterators 700 virtual child_iterator child_begin(); 701 virtual child_iterator child_end(); 702}; 703 704/// CXXNewExpr - A new expression for memory allocation and constructor calls, 705/// e.g: "new CXXNewExpr(foo)". 706class CXXNewExpr : public Expr { 707 // Was the usage ::new, i.e. is the global new to be used? 708 bool GlobalNew : 1; 709 // Was the form (type-id) used? Otherwise, it was new-type-id. 710 bool ParenTypeId : 1; 711 // Is there an initializer? If not, built-ins are uninitialized, else they're 712 // value-initialized. 713 bool Initializer : 1; 714 // Do we allocate an array? If so, the first SubExpr is the size expression. 715 bool Array : 1; 716 // The number of placement new arguments. 717 unsigned NumPlacementArgs : 14; 718 // The number of constructor arguments. This may be 1 even for non-class 719 // types; use the pseudo copy constructor. 720 unsigned NumConstructorArgs : 14; 721 // Contains an optional array size expression, any number of optional 722 // placement arguments, and any number of optional constructor arguments, 723 // in that order. 724 Stmt **SubExprs; 725 // Points to the allocation function used. 726 FunctionDecl *OperatorNew; 727 // Points to the deallocation function used in case of error. May be null. 728 FunctionDecl *OperatorDelete; 729 // Points to the constructor used. Cannot be null if AllocType is a record; 730 // it would still point at the default constructor (even an implicit one). 731 // Must be null for all other types. 732 CXXConstructorDecl *Constructor; 733 734 SourceLocation StartLoc; 735 SourceLocation EndLoc; 736 737public: 738 CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs, 739 unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize, 740 CXXConstructorDecl *constructor, bool initializer, 741 Expr **constructorArgs, unsigned numConsArgs, 742 FunctionDecl *operatorDelete, QualType ty, 743 SourceLocation startLoc, SourceLocation endLoc); 744 ~CXXNewExpr() { 745 delete[] SubExprs; 746 } 747 748 QualType getAllocatedType() const { 749 assert(getType()->isPointerType()); 750 return getType()->getAs<PointerType>()->getPointeeType(); 751 } 752 753 FunctionDecl *getOperatorNew() const { return OperatorNew; } 754 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 755 CXXConstructorDecl *getConstructor() const { return Constructor; } 756 757 bool isArray() const { return Array; } 758 Expr *getArraySize() { 759 return Array ? cast<Expr>(SubExprs[0]) : 0; 760 } 761 const Expr *getArraySize() const { 762 return Array ? cast<Expr>(SubExprs[0]) : 0; 763 } 764 765 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 766 Expr *getPlacementArg(unsigned i) { 767 assert(i < NumPlacementArgs && "Index out of range"); 768 return cast<Expr>(SubExprs[Array + i]); 769 } 770 const Expr *getPlacementArg(unsigned i) const { 771 assert(i < NumPlacementArgs && "Index out of range"); 772 return cast<Expr>(SubExprs[Array + i]); 773 } 774 775 bool isGlobalNew() const { return GlobalNew; } 776 bool isParenTypeId() const { return ParenTypeId; } 777 bool hasInitializer() const { return Initializer; } 778 779 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 780 Expr *getConstructorArg(unsigned i) { 781 assert(i < NumConstructorArgs && "Index out of range"); 782 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 783 } 784 const Expr *getConstructorArg(unsigned i) const { 785 assert(i < NumConstructorArgs && "Index out of range"); 786 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 787 } 788 789 typedef ExprIterator arg_iterator; 790 typedef ConstExprIterator const_arg_iterator; 791 792 arg_iterator placement_arg_begin() { 793 return SubExprs + Array; 794 } 795 arg_iterator placement_arg_end() { 796 return SubExprs + Array + getNumPlacementArgs(); 797 } 798 const_arg_iterator placement_arg_begin() const { 799 return SubExprs + Array; 800 } 801 const_arg_iterator placement_arg_end() const { 802 return SubExprs + Array + getNumPlacementArgs(); 803 } 804 805 arg_iterator constructor_arg_begin() { 806 return SubExprs + Array + getNumPlacementArgs(); 807 } 808 arg_iterator constructor_arg_end() { 809 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 810 } 811 const_arg_iterator constructor_arg_begin() const { 812 return SubExprs + Array + getNumPlacementArgs(); 813 } 814 const_arg_iterator constructor_arg_end() const { 815 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 816 } 817 818 virtual SourceRange getSourceRange() const { 819 return SourceRange(StartLoc, EndLoc); 820 } 821 822 static bool classof(const Stmt *T) { 823 return T->getStmtClass() == CXXNewExprClass; 824 } 825 static bool classof(const CXXNewExpr *) { return true; } 826 827 // Iterators 828 virtual child_iterator child_begin(); 829 virtual child_iterator child_end(); 830}; 831 832/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 833/// calls, e.g. "delete[] pArray". 834class CXXDeleteExpr : public Expr { 835 // Is this a forced global delete, i.e. "::delete"? 836 bool GlobalDelete : 1; 837 // Is this the array form of delete, i.e. "delete[]"? 838 bool ArrayForm : 1; 839 // Points to the operator delete overload that is used. Could be a member. 840 FunctionDecl *OperatorDelete; 841 // The pointer expression to be deleted. 842 Stmt *Argument; 843 // Location of the expression. 844 SourceLocation Loc; 845public: 846 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 847 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 848 : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete), 849 ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg), 850 Loc(loc) { } 851 852 bool isGlobalDelete() const { return GlobalDelete; } 853 bool isArrayForm() const { return ArrayForm; } 854 855 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 856 857 Expr *getArgument() { return cast<Expr>(Argument); } 858 const Expr *getArgument() const { return cast<Expr>(Argument); } 859 860 virtual SourceRange getSourceRange() const { 861 return SourceRange(Loc, Argument->getLocEnd()); 862 } 863 864 static bool classof(const Stmt *T) { 865 return T->getStmtClass() == CXXDeleteExprClass; 866 } 867 static bool classof(const CXXDeleteExpr *) { return true; } 868 869 // Iterators 870 virtual child_iterator child_begin(); 871 virtual child_iterator child_end(); 872}; 873 874/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 875/// 876/// Example: 877/// 878/// \code 879/// template<typename T> 880/// void destroy(T* ptr) { 881/// ptr->~T(); 882/// } 883/// \endcode 884/// 885/// When the template is parsed, the expression \c ptr->~T will be stored as 886/// a member reference expression. If it then instantiated with a scalar type 887/// as a template argument for T, the resulting expression will be a 888/// pseudo-destructor expression. 889class CXXPseudoDestructorExpr : public Expr { 890 /// \brief The base expression (that is being destroyed). 891 Stmt *Base; 892 893 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 894 /// period ('.'). 895 bool IsArrow : 1; 896 897 /// \brief The location of the '.' or '->' operator. 898 SourceLocation OperatorLoc; 899 900 /// \brief The nested-name-specifier that follows the operator, if present. 901 NestedNameSpecifier *Qualifier; 902 903 /// \brief The source range that covers the nested-name-specifier, if 904 /// present. 905 SourceRange QualifierRange; 906 907 /// \brief The type being destroyed. 908 QualType DestroyedType; 909 910 /// \brief The location of the type after the '~'. 911 SourceLocation DestroyedTypeLoc; 912 913public: 914 CXXPseudoDestructorExpr(ASTContext &Context, 915 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 916 NestedNameSpecifier *Qualifier, 917 SourceRange QualifierRange, 918 QualType DestroyedType, 919 SourceLocation DestroyedTypeLoc) 920 : Expr(CXXPseudoDestructorExprClass, 921 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0, 922 false, 0)), 923 /*isTypeDependent=*/false, 924 /*isValueDependent=*/Base->isValueDependent()), 925 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow), 926 OperatorLoc(OperatorLoc), Qualifier(Qualifier), 927 QualifierRange(QualifierRange), DestroyedType(DestroyedType), 928 DestroyedTypeLoc(DestroyedTypeLoc) { } 929 930 void setBase(Expr *E) { Base = E; } 931 Expr *getBase() const { return cast<Expr>(Base); } 932 933 /// \brief Determines whether this member expression actually had 934 /// a C++ nested-name-specifier prior to the name of the member, e.g., 935 /// x->Base::foo. 936 bool hasQualifier() const { return Qualifier != 0; } 937 938 /// \brief If the member name was qualified, retrieves the source range of 939 /// the nested-name-specifier that precedes the member name. Otherwise, 940 /// returns an empty source range. 941 SourceRange getQualifierRange() const { return QualifierRange; } 942 943 /// \brief If the member name was qualified, retrieves the 944 /// nested-name-specifier that precedes the member name. Otherwise, returns 945 /// NULL. 946 NestedNameSpecifier *getQualifier() const { return Qualifier; } 947 948 /// \brief Determine whether this pseudo-destructor expression was written 949 /// using an '->' (otherwise, it used a '.'). 950 bool isArrow() const { return IsArrow; } 951 void setArrow(bool A) { IsArrow = A; } 952 953 /// \brief Retrieve the location of the '.' or '->' operator. 954 SourceLocation getOperatorLoc() const { return OperatorLoc; } 955 956 /// \brief Retrieve the type that is being destroyed. 957 QualType getDestroyedType() const { return DestroyedType; } 958 959 /// \brief Retrieve the location of the type being destroyed. 960 SourceLocation getDestroyedTypeLoc() const { return DestroyedTypeLoc; } 961 962 virtual SourceRange getSourceRange() const { 963 return SourceRange(Base->getLocStart(), DestroyedTypeLoc); 964 } 965 966 static bool classof(const Stmt *T) { 967 return T->getStmtClass() == CXXPseudoDestructorExprClass; 968 } 969 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 970 971 // Iterators 972 virtual child_iterator child_begin(); 973 virtual child_iterator child_end(); 974}; 975 976/// \brief Represents the name of a function that has not been 977/// resolved to any declaration. 978/// 979/// Unresolved function names occur when a function name is 980/// encountered prior to an open parentheses ('(') in a C++ function 981/// call, and the function name itself did not resolve to a 982/// declaration. These function names can only be resolved when they 983/// form the postfix-expression of a function call, so that 984/// argument-dependent lookup finds declarations corresponding to 985/// these functions. 986 987/// @code 988/// template<typename T> void f(T x) { 989/// g(x); // g is an unresolved function name (that is also a dependent name) 990/// } 991/// @endcode 992class UnresolvedFunctionNameExpr : public Expr { 993 /// The name that was present in the source 994 DeclarationName Name; 995 996 /// The location of this name in the source code 997 SourceLocation Loc; 998 999public: 1000 UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L) 1001 : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { } 1002 1003 /// \brief Retrieves the name that occurred in the source code. 1004 DeclarationName getName() const { return Name; } 1005 1006 /// getLocation - Retrieves the location in the source code where 1007 /// the name occurred. 1008 SourceLocation getLocation() const { return Loc; } 1009 1010 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 1011 1012 static bool classof(const Stmt *T) { 1013 return T->getStmtClass() == UnresolvedFunctionNameExprClass; 1014 } 1015 static bool classof(const UnresolvedFunctionNameExpr *) { return true; } 1016 1017 // Iterators 1018 virtual child_iterator child_begin(); 1019 virtual child_iterator child_end(); 1020}; 1021 1022/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1023/// implementation of TR1/C++0x type trait templates. 1024/// Example: 1025/// __is_pod(int) == true 1026/// __is_enum(std::string) == false 1027class UnaryTypeTraitExpr : public Expr { 1028 /// UTT - The trait. 1029 UnaryTypeTrait UTT; 1030 1031 /// Loc - The location of the type trait keyword. 1032 SourceLocation Loc; 1033 1034 /// RParen - The location of the closing paren. 1035 SourceLocation RParen; 1036 1037 /// QueriedType - The type we're testing. 1038 QualType QueriedType; 1039 1040public: 1041 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried, 1042 SourceLocation rparen, QualType ty) 1043 : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()), 1044 UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { } 1045 1046 virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1047 1048 UnaryTypeTrait getTrait() const { return UTT; } 1049 1050 QualType getQueriedType() const { return QueriedType; } 1051 1052 bool EvaluateTrait(ASTContext&) const; 1053 1054 static bool classof(const Stmt *T) { 1055 return T->getStmtClass() == UnaryTypeTraitExprClass; 1056 } 1057 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1058 1059 // Iterators 1060 virtual child_iterator child_begin(); 1061 virtual child_iterator child_end(); 1062}; 1063 1064/// QualifiedDeclRefExpr - A reference to a declared variable, 1065/// function, enum, etc., that includes a qualification, e.g., 1066/// "N::foo". 1067class QualifiedDeclRefExpr : public DeclRefExpr { 1068 /// QualifierRange - The source range that covers the 1069 /// nested-name-specifier. 1070 SourceRange QualifierRange; 1071 1072 /// \brief The nested-name-specifier that qualifies this declaration 1073 /// name. 1074 NestedNameSpecifier *NNS; 1075 1076public: 1077 QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, 1078 bool VD, SourceRange R, NestedNameSpecifier *NNS) 1079 : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD), 1080 QualifierRange(R), NNS(NNS) { } 1081 1082 /// \brief Retrieve the source range of the nested-name-specifier. 1083 SourceRange getQualifierRange() const { return QualifierRange; } 1084 1085 /// \brief Retrieve the nested-name-specifier that qualifies this 1086 /// declaration. 1087 NestedNameSpecifier *getQualifier() const { return NNS; } 1088 1089 virtual SourceRange getSourceRange() const { 1090 return SourceRange(QualifierRange.getBegin(), getLocation()); 1091 } 1092 1093 static bool classof(const Stmt *T) { 1094 return T->getStmtClass() == QualifiedDeclRefExprClass; 1095 } 1096 static bool classof(const QualifiedDeclRefExpr *) { return true; } 1097}; 1098 1099/// \brief A qualified reference to a name whose declaration cannot 1100/// yet be resolved. 1101/// 1102/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that 1103/// it expresses a qualified reference to a declaration such as 1104/// X<T>::value. The difference, however, is that an 1105/// UnresolvedDeclRefExpr node is used only within C++ templates when 1106/// the qualification (e.g., X<T>::) refers to a dependent type. In 1107/// this case, X<T>::value cannot resolve to a declaration because the 1108/// declaration will differ from on instantiation of X<T> to the 1109/// next. Therefore, UnresolvedDeclRefExpr keeps track of the 1110/// qualifier (X<T>::) and the name of the entity being referenced 1111/// ("value"). Such expressions will instantiate to 1112/// QualifiedDeclRefExprs. 1113class UnresolvedDeclRefExpr : public Expr { 1114 /// The name of the entity we will be referencing. 1115 DeclarationName Name; 1116 1117 /// Location of the name of the declaration we're referencing. 1118 SourceLocation Loc; 1119 1120 /// QualifierRange - The source range that covers the 1121 /// nested-name-specifier. 1122 SourceRange QualifierRange; 1123 1124 /// \brief The nested-name-specifier that qualifies this unresolved 1125 /// declaration name. 1126 NestedNameSpecifier *NNS; 1127 1128 /// \brief Whether this expr is an address of (&) operand. 1129 bool IsAddressOfOperand; 1130 1131public: 1132 UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L, 1133 SourceRange R, NestedNameSpecifier *NNS, 1134 bool IsAddressOfOperand) 1135 : Expr(UnresolvedDeclRefExprClass, T, true, true), 1136 Name(N), Loc(L), QualifierRange(R), NNS(NNS), 1137 IsAddressOfOperand(IsAddressOfOperand) { } 1138 1139 /// \brief Retrieve the name that this expression refers to. 1140 DeclarationName getDeclName() const { return Name; } 1141 1142 /// \brief Retrieve the location of the name within the expression. 1143 SourceLocation getLocation() const { return Loc; } 1144 1145 /// \brief Retrieve the source range of the nested-name-specifier. 1146 SourceRange getQualifierRange() const { return QualifierRange; } 1147 1148 /// \brief Retrieve the nested-name-specifier that qualifies this 1149 /// declaration. 1150 NestedNameSpecifier *getQualifier() const { return NNS; } 1151 1152 /// \brief Retrieve whether this is an address of (&) operand. 1153 1154 bool isAddressOfOperand() const { return IsAddressOfOperand; } 1155 virtual SourceRange getSourceRange() const { 1156 return SourceRange(QualifierRange.getBegin(), getLocation()); 1157 } 1158 1159 static bool classof(const Stmt *T) { 1160 return T->getStmtClass() == UnresolvedDeclRefExprClass; 1161 } 1162 static bool classof(const UnresolvedDeclRefExpr *) { return true; } 1163 1164 virtual StmtIterator child_begin(); 1165 virtual StmtIterator child_end(); 1166}; 1167 1168/// \brief An expression that refers to a C++ template-id, such as 1169/// @c isa<FunctionDecl>. 1170class TemplateIdRefExpr : public Expr { 1171 /// \brief If this template-id was qualified-id, e.g., @c std::sort<int>, 1172 /// this nested name specifier contains the @c std::. 1173 NestedNameSpecifier *Qualifier; 1174 1175 /// \brief If this template-id was a qualified-id, e.g., @c std::sort<int>, 1176 /// this covers the source code range of the @c std::. 1177 SourceRange QualifierRange; 1178 1179 /// \brief The actual template to which this template-id refers. 1180 TemplateName Template; 1181 1182 /// \brief The source location of the template name. 1183 SourceLocation TemplateNameLoc; 1184 1185 /// \brief The source location of the left angle bracket ('<'); 1186 SourceLocation LAngleLoc; 1187 1188 /// \brief The source location of the right angle bracket ('>'); 1189 SourceLocation RAngleLoc; 1190 1191 /// \brief The number of template arguments in TemplateArgs. 1192 unsigned NumTemplateArgs; 1193 1194 TemplateIdRefExpr(QualType T, 1195 NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 1196 TemplateName Template, SourceLocation TemplateNameLoc, 1197 SourceLocation LAngleLoc, 1198 const TemplateArgument *TemplateArgs, 1199 unsigned NumTemplateArgs, 1200 SourceLocation RAngleLoc); 1201 1202 virtual void DoDestroy(ASTContext &Context); 1203 1204public: 1205 static TemplateIdRefExpr * 1206 Create(ASTContext &Context, QualType T, 1207 NestedNameSpecifier *Qualifier, SourceRange QualifierRange, 1208 TemplateName Template, SourceLocation TemplateNameLoc, 1209 SourceLocation LAngleLoc, const TemplateArgument *TemplateArgs, 1210 unsigned NumTemplateArgs, SourceLocation RAngleLoc); 1211 1212 /// \brief Retrieve the nested name specifier used to qualify the name of 1213 /// this template-id, e.g., the "std::sort" in @c std::sort<int>, or NULL 1214 /// if this template-id was an unqualified-id. 1215 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1216 1217 /// \brief Retrieve the source range describing the nested name specifier 1218 /// used to qualified the name of this template-id, if the name was qualified. 1219 SourceRange getQualifierRange() const { return QualifierRange; } 1220 1221 /// \brief Retrieve the name of the template referenced, e.g., "sort" in 1222 /// @c std::sort<int>; 1223 TemplateName getTemplateName() const { return Template; } 1224 1225 /// \brief Retrieve the location of the name of the template referenced, e.g., 1226 /// the location of "sort" in @c std::sort<int>. 1227 SourceLocation getTemplateNameLoc() const { return TemplateNameLoc; } 1228 1229 /// \brief Retrieve the location of the left angle bracket following the 1230 /// template name ('<'). 1231 SourceLocation getLAngleLoc() const { return LAngleLoc; } 1232 1233 /// \brief Retrieve the template arguments provided as part of this 1234 /// template-id. 1235 const TemplateArgument *getTemplateArgs() const { 1236 return reinterpret_cast<const TemplateArgument *>(this + 1); 1237 } 1238 1239 /// \brief Retrieve the number of template arguments provided as part of this 1240 /// template-id. 1241 unsigned getNumTemplateArgs() const { return NumTemplateArgs; } 1242 1243 /// \brief Retrieve the location of the right angle bracket following the 1244 /// template arguments ('>'). 1245 SourceLocation getRAngleLoc() const { return RAngleLoc; } 1246 1247 virtual SourceRange getSourceRange() const { 1248 return SourceRange(Qualifier? QualifierRange.getBegin() : TemplateNameLoc, 1249 RAngleLoc); 1250 } 1251 1252 // Iterators 1253 virtual child_iterator child_begin(); 1254 virtual child_iterator child_end(); 1255 1256 static bool classof(const Stmt *T) { 1257 return T->getStmtClass() == TemplateIdRefExprClass; 1258 } 1259 static bool classof(const TemplateIdRefExpr *) { return true; } 1260}; 1261 1262class CXXExprWithTemporaries : public Expr { 1263 Stmt *SubExpr; 1264 1265 CXXTemporary **Temps; 1266 unsigned NumTemps; 1267 1268 bool ShouldDestroyTemps; 1269 1270 CXXExprWithTemporaries(Expr *SubExpr, CXXTemporary **Temps, 1271 unsigned NumTemps, bool ShouldDestroyTemps); 1272 ~CXXExprWithTemporaries(); 1273 1274protected: 1275 virtual void DoDestroy(ASTContext &C); 1276 1277public: 1278 static CXXExprWithTemporaries *Create(ASTContext &C, Expr *SubExpr, 1279 CXXTemporary **Temps, unsigned NumTemps, 1280 bool ShouldDestroyTemporaries); 1281 1282 unsigned getNumTemporaries() const { return NumTemps; } 1283 CXXTemporary *getTemporary(unsigned i) { 1284 assert(i < NumTemps && "Index out of range"); 1285 return Temps[i]; 1286 } 1287 const CXXTemporary *getTemporary(unsigned i) const { 1288 assert(i < NumTemps && "Index out of range"); 1289 return Temps[i]; 1290 } 1291 1292 bool shouldDestroyTemporaries() const { return ShouldDestroyTemps; } 1293 1294 void removeLastTemporary() { NumTemps--; } 1295 1296 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 1297 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 1298 void setSubExpr(Expr *E) { SubExpr = E; } 1299 1300 virtual SourceRange getSourceRange() const { 1301 return SubExpr->getSourceRange(); 1302 } 1303 1304 // Implement isa/cast/dyncast/etc. 1305 static bool classof(const Stmt *T) { 1306 return T->getStmtClass() == CXXExprWithTemporariesClass; 1307 } 1308 static bool classof(const CXXExprWithTemporaries *) { return true; } 1309 1310 // Iterators 1311 virtual child_iterator child_begin(); 1312 virtual child_iterator child_end(); 1313}; 1314 1315/// \brief Describes an explicit type conversion that uses functional 1316/// notion but could not be resolved because one or more arguments are 1317/// type-dependent. 1318/// 1319/// The explicit type conversions expressed by 1320/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 1321/// where \c T is some type and \c a1, a2, ..., aN are values, and 1322/// either \C T is a dependent type or one or more of the \c a's is 1323/// type-dependent. For example, this would occur in a template such 1324/// as: 1325/// 1326/// \code 1327/// template<typename T, typename A1> 1328/// inline T make_a(const A1& a1) { 1329/// return T(a1); 1330/// } 1331/// \endcode 1332/// 1333/// When the returned expression is instantiated, it may resolve to a 1334/// constructor call, conversion function call, or some kind of type 1335/// conversion. 1336class CXXUnresolvedConstructExpr : public Expr { 1337 /// \brief The starting location of the type 1338 SourceLocation TyBeginLoc; 1339 1340 /// \brief The type being constructed. 1341 QualType Type; 1342 1343 /// \brief The location of the left parentheses ('('). 1344 SourceLocation LParenLoc; 1345 1346 /// \brief The location of the right parentheses (')'). 1347 SourceLocation RParenLoc; 1348 1349 /// \brief The number of arguments used to construct the type. 1350 unsigned NumArgs; 1351 1352 CXXUnresolvedConstructExpr(SourceLocation TyBegin, 1353 QualType T, 1354 SourceLocation LParenLoc, 1355 Expr **Args, 1356 unsigned NumArgs, 1357 SourceLocation RParenLoc); 1358 1359public: 1360 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 1361 SourceLocation TyBegin, 1362 QualType T, 1363 SourceLocation LParenLoc, 1364 Expr **Args, 1365 unsigned NumArgs, 1366 SourceLocation RParenLoc); 1367 1368 /// \brief Retrieve the source location where the type begins. 1369 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 1370 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 1371 1372 /// \brief Retrieve the type that is being constructed, as specified 1373 /// in the source code. 1374 QualType getTypeAsWritten() const { return Type; } 1375 void setTypeAsWritten(QualType T) { Type = T; } 1376 1377 /// \brief Retrieve the location of the left parentheses ('(') that 1378 /// precedes the argument list. 1379 SourceLocation getLParenLoc() const { return LParenLoc; } 1380 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1381 1382 /// \brief Retrieve the location of the right parentheses (')') that 1383 /// follows the argument list. 1384 SourceLocation getRParenLoc() const { return RParenLoc; } 1385 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1386 1387 /// \brief Retrieve the number of arguments. 1388 unsigned arg_size() const { return NumArgs; } 1389 1390 typedef Expr** arg_iterator; 1391 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 1392 arg_iterator arg_end() { return arg_begin() + NumArgs; } 1393 1394 Expr *getArg(unsigned I) { 1395 assert(I < NumArgs && "Argument index out-of-range"); 1396 return *(arg_begin() + I); 1397 } 1398 1399 virtual SourceRange getSourceRange() const { 1400 return SourceRange(TyBeginLoc, RParenLoc); 1401 } 1402 static bool classof(const Stmt *T) { 1403 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 1404 } 1405 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 1406 1407 // Iterators 1408 virtual child_iterator child_begin(); 1409 virtual child_iterator child_end(); 1410}; 1411 1412/// \brief Represents a C++ member access expression where the actual member 1413/// referenced could not be resolved, e.g., because the base expression or the 1414/// member name was dependent. 1415class CXXUnresolvedMemberExpr : public Expr { 1416 /// \brief The expression for the base pointer or class reference, 1417 /// e.g., the \c x in x.f. 1418 Stmt *Base; 1419 1420 /// \brief Whether this member expression used the '->' operator or 1421 /// the '.' operator. 1422 bool IsArrow : 1; 1423 1424 /// \brief Whether this member expression has explicitly-specified template 1425 /// arguments. 1426 bool HasExplicitTemplateArgumentList : 1; 1427 1428 /// \brief The location of the '->' or '.' operator. 1429 SourceLocation OperatorLoc; 1430 1431 /// \brief The nested-name-specifier that precedes the member name, if any. 1432 NestedNameSpecifier *Qualifier; 1433 1434 /// \brief The source range covering the nested name specifier. 1435 SourceRange QualifierRange; 1436 1437 /// \brief In a qualified member access expression such as t->Base::f, this 1438 /// member stores the resolves of name lookup in the context of the member 1439 /// access expression, to be used at instantiation time. 1440 /// 1441 /// FIXME: This member, along with the Qualifier and QualifierRange, could 1442 /// be stuck into a structure that is optionally allocated at the end of 1443 /// the CXXUnresolvedMemberExpr, to save space in the common case. 1444 NamedDecl *FirstQualifierFoundInScope; 1445 1446 /// \brief The member to which this member expression refers, which 1447 /// can be name, overloaded operator, or destructor. 1448 /// FIXME: could also be a template-id 1449 DeclarationName Member; 1450 1451 /// \brief The location of the member name. 1452 SourceLocation MemberLoc; 1453 1454 /// \brief Retrieve the explicit template argument list that followed the 1455 /// member template name, if any. 1456 ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() { 1457 if (!HasExplicitTemplateArgumentList) 1458 return 0; 1459 1460 return reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 1461 } 1462 1463 /// \brief Retrieve the explicit template argument list that followed the 1464 /// member template name, if any. 1465 const ExplicitTemplateArgumentList *getExplicitTemplateArgumentList() const { 1466 return const_cast<CXXUnresolvedMemberExpr *>(this) 1467 ->getExplicitTemplateArgumentList(); 1468 } 1469 1470 CXXUnresolvedMemberExpr(ASTContext &C, 1471 Expr *Base, bool IsArrow, 1472 SourceLocation OperatorLoc, 1473 NestedNameSpecifier *Qualifier, 1474 SourceRange QualifierRange, 1475 NamedDecl *FirstQualifierFoundInScope, 1476 DeclarationName Member, 1477 SourceLocation MemberLoc, 1478 bool HasExplicitTemplateArgs, 1479 SourceLocation LAngleLoc, 1480 const TemplateArgument *TemplateArgs, 1481 unsigned NumTemplateArgs, 1482 SourceLocation RAngleLoc); 1483 1484public: 1485 CXXUnresolvedMemberExpr(ASTContext &C, 1486 Expr *Base, bool IsArrow, 1487 SourceLocation OperatorLoc, 1488 NestedNameSpecifier *Qualifier, 1489 SourceRange QualifierRange, 1490 NamedDecl *FirstQualifierFoundInScope, 1491 DeclarationName Member, 1492 SourceLocation MemberLoc) 1493 : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true), 1494 Base(Base), IsArrow(IsArrow), HasExplicitTemplateArgumentList(false), 1495 OperatorLoc(OperatorLoc), 1496 Qualifier(Qualifier), QualifierRange(QualifierRange), 1497 FirstQualifierFoundInScope(FirstQualifierFoundInScope), 1498 Member(Member), MemberLoc(MemberLoc) { } 1499 1500 static CXXUnresolvedMemberExpr * 1501 Create(ASTContext &C, 1502 Expr *Base, bool IsArrow, 1503 SourceLocation OperatorLoc, 1504 NestedNameSpecifier *Qualifier, 1505 SourceRange QualifierRange, 1506 NamedDecl *FirstQualifierFoundInScope, 1507 DeclarationName Member, 1508 SourceLocation MemberLoc, 1509 bool HasExplicitTemplateArgs, 1510 SourceLocation LAngleLoc, 1511 const TemplateArgument *TemplateArgs, 1512 unsigned NumTemplateArgs, 1513 SourceLocation RAngleLoc); 1514 1515 /// \brief Retrieve the base object of this member expressions, 1516 /// e.g., the \c x in \c x.m. 1517 Expr *getBase() { return cast<Expr>(Base); } 1518 void setBase(Expr *E) { Base = E; } 1519 1520 /// \brief Determine whether this member expression used the '->' 1521 /// operator; otherwise, it used the '.' operator. 1522 bool isArrow() const { return IsArrow; } 1523 void setArrow(bool A) { IsArrow = A; } 1524 1525 /// \brief Retrieve the location of the '->' or '.' operator. 1526 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1527 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1528 1529 /// \brief Retrieve the nested-name-specifier that qualifies the member 1530 /// name. 1531 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1532 1533 /// \brief Retrieve the source range covering the nested-name-specifier 1534 /// that qualifies the member name. 1535 SourceRange getQualifierRange() const { return QualifierRange; } 1536 1537 /// \brief Retrieve the first part of the nested-name-specifier that was 1538 /// found in the scope of the member access expression when the member access 1539 /// was initially parsed. 1540 /// 1541 /// This function only returns a useful result when member access expression 1542 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 1543 /// returned by this function describes what was found by unqualified name 1544 /// lookup for the identifier "Base" within the scope of the member access 1545 /// expression itself. At template instantiation time, this information is 1546 /// combined with the results of name lookup into the type of the object 1547 /// expression itself (the class type of x). 1548 NamedDecl *getFirstQualifierFoundInScope() const { 1549 return FirstQualifierFoundInScope; 1550 } 1551 1552 /// \brief Retrieve the name of the member that this expression 1553 /// refers to. 1554 DeclarationName getMember() const { return Member; } 1555 void setMember(DeclarationName N) { Member = N; } 1556 1557 // \brief Retrieve the location of the name of the member that this 1558 // expression refers to. 1559 SourceLocation getMemberLoc() const { return MemberLoc; } 1560 void setMemberLoc(SourceLocation L) { MemberLoc = L; } 1561 1562 /// \brief Determines whether this member expression actually had a C++ 1563 /// template argument list explicitly specified, e.g., x.f<int>. 1564 bool hasExplicitTemplateArgumentList() { 1565 return HasExplicitTemplateArgumentList; 1566 } 1567 1568 /// \brief Retrieve the location of the left angle bracket following the 1569 /// member name ('<'), if any. 1570 SourceLocation getLAngleLoc() const { 1571 if (!HasExplicitTemplateArgumentList) 1572 return SourceLocation(); 1573 1574 return getExplicitTemplateArgumentList()->LAngleLoc; 1575 } 1576 1577 /// \brief Retrieve the template arguments provided as part of this 1578 /// template-id. 1579 const TemplateArgument *getTemplateArgs() const { 1580 if (!HasExplicitTemplateArgumentList) 1581 return 0; 1582 1583 return getExplicitTemplateArgumentList()->getTemplateArgs(); 1584 } 1585 1586 /// \brief Retrieve the number of template arguments provided as part of this 1587 /// template-id. 1588 unsigned getNumTemplateArgs() const { 1589 if (!HasExplicitTemplateArgumentList) 1590 return 0; 1591 1592 return getExplicitTemplateArgumentList()->NumTemplateArgs; 1593 } 1594 1595 /// \brief Retrieve the location of the right angle bracket following the 1596 /// template arguments ('>'). 1597 SourceLocation getRAngleLoc() const { 1598 if (!HasExplicitTemplateArgumentList) 1599 return SourceLocation(); 1600 1601 return getExplicitTemplateArgumentList()->RAngleLoc; 1602 } 1603 1604 virtual SourceRange getSourceRange() const { 1605 if (HasExplicitTemplateArgumentList) 1606 return SourceRange(Base->getSourceRange().getBegin(), 1607 getRAngleLoc()); 1608 1609 return SourceRange(Base->getSourceRange().getBegin(), 1610 MemberLoc); 1611 } 1612 1613 static bool classof(const Stmt *T) { 1614 return T->getStmtClass() == CXXUnresolvedMemberExprClass; 1615 } 1616 static bool classof(const CXXUnresolvedMemberExpr *) { return true; } 1617 1618 // Iterators 1619 virtual child_iterator child_begin(); 1620 virtual child_iterator child_end(); 1621}; 1622 1623} // end namespace clang 1624 1625#endif 1626