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