ExprCXX.h revision 42e5b50f4dc897f252e0d476063a7f9846d96624
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 CXXTempVarDecl; 25 26//===--------------------------------------------------------------------===// 27// C++ Expressions. 28//===--------------------------------------------------------------------===// 29 30/// \brief A call to an overloaded operator written using operator 31/// syntax. 32/// 33/// Represents a call to an overloaded operator written using operator 34/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 35/// normal call, this AST node provides better information about the 36/// syntactic representation of the call. 37/// 38/// In a C++ template, this expression node kind will be used whenever 39/// any of the arguments are type-dependent. In this case, the 40/// function itself will be a (possibly empty) set of functions and 41/// function templates that were found by name lookup at template 42/// definition time. 43class CXXOperatorCallExpr : public CallExpr { 44 /// \brief The overloaded operator. 45 OverloadedOperatorKind Operator; 46 47public: 48 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn, 49 Expr **args, unsigned numargs, QualType t, 50 SourceLocation operatorloc) 51 : CallExpr(C, CXXOperatorCallExprClass, fn, args, numargs, t, operatorloc), 52 Operator(Op) {} 53 54 /// getOperator - Returns the kind of overloaded operator that this 55 /// expression refers to. 56 OverloadedOperatorKind getOperator() const { return Operator; } 57 58 /// getOperatorLoc - Returns the location of the operator symbol in 59 /// the expression. When @c getOperator()==OO_Call, this is the 60 /// location of the right parentheses; when @c 61 /// getOperator()==OO_Subscript, this is the location of the right 62 /// bracket. 63 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 64 65 virtual SourceRange getSourceRange() const; 66 67 static bool classof(const Stmt *T) { 68 return T->getStmtClass() == CXXOperatorCallExprClass; 69 } 70 static bool classof(const CXXOperatorCallExpr *) { return true; } 71}; 72 73/// CXXMemberCallExpr - Represents a call to a member function that 74/// may be written either with member call syntax (e.g., "obj.func()" 75/// or "objptr->func()") or with normal function-call syntax 76/// ("func()") within a member function that ends up calling a member 77/// function. The callee in either case is a MemberExpr that contains 78/// both the object argument and the member function, while the 79/// arguments are the arguments within the parentheses (not including 80/// the object argument). 81class CXXMemberCallExpr : public CallExpr { 82public: 83 CXXMemberCallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, 84 QualType t, SourceLocation rparenloc) 85 : CallExpr(C, CXXMemberCallExprClass, fn, args, numargs, t, rparenloc) {} 86 87 /// getImplicitObjectArgument - Retrieves the implicit object 88 /// argument for the member call. For example, in "x.f(5)", this 89 /// operation would return "x". 90 Expr *getImplicitObjectArgument(); 91 92 static bool classof(const Stmt *T) { 93 return T->getStmtClass() == CXXMemberCallExprClass; 94 } 95 static bool classof(const CXXMemberCallExpr *) { return true; } 96}; 97 98/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 99/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 100/// const_cast. 101/// 102/// This abstract class is inherited by all of the classes 103/// representing "named" casts, e.g., CXXStaticCastExpr, 104/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 105class CXXNamedCastExpr : public ExplicitCastExpr { 106private: 107 SourceLocation Loc; // the location of the casting op 108 109protected: 110 CXXNamedCastExpr(StmtClass SC, QualType ty, Expr *op, QualType writtenTy, 111 SourceLocation l) 112 : ExplicitCastExpr(SC, ty, op, writtenTy), Loc(l) {} 113 114public: 115 const char *getCastName() const; 116 117 /// \brief Retrieve the location of the cast operator keyword, e.g., 118 /// "static_cast". 119 SourceLocation getOperatorLoc() const { return Loc; } 120 void setOperatorLoc(SourceLocation L) { Loc = L; } 121 122 virtual SourceRange getSourceRange() const { 123 return SourceRange(Loc, getSubExpr()->getSourceRange().getEnd()); 124 } 125 static bool classof(const Stmt *T) { 126 switch (T->getStmtClass()) { 127 case CXXNamedCastExprClass: 128 case CXXStaticCastExprClass: 129 case CXXDynamicCastExprClass: 130 case CXXReinterpretCastExprClass: 131 case CXXConstCastExprClass: 132 return true; 133 default: 134 return false; 135 } 136 } 137 static bool classof(const CXXNamedCastExpr *) { return true; } 138}; 139 140/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]). 141/// 142/// This expression node represents a C++ static cast, e.g., 143/// @c static_cast<int>(1.0). 144class CXXStaticCastExpr : public CXXNamedCastExpr { 145public: 146 CXXStaticCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l) 147 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, op, writtenTy, l) {} 148 149 static bool classof(const Stmt *T) { 150 return T->getStmtClass() == CXXStaticCastExprClass; 151 } 152 static bool classof(const CXXStaticCastExpr *) { return true; } 153}; 154 155/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 156/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 157/// determine how to perform the type cast. 158/// 159/// This expression node represents a dynamic cast, e.g., 160/// @c dynamic_cast<Derived*>(BasePtr). 161class CXXDynamicCastExpr : public CXXNamedCastExpr { 162public: 163 CXXDynamicCastExpr(QualType ty, Expr *op, QualType writtenTy, SourceLocation l) 164 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, op, writtenTy, l) {} 165 166 static bool classof(const Stmt *T) { 167 return T->getStmtClass() == CXXDynamicCastExprClass; 168 } 169 static bool classof(const CXXDynamicCastExpr *) { return true; } 170}; 171 172/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 173/// [expr.reinterpret.cast]), which provides a differently-typed view 174/// of a value but performs no actual work at run time. 175/// 176/// This expression node represents a reinterpret cast, e.g., 177/// @c reinterpret_cast<int>(VoidPtr). 178class CXXReinterpretCastExpr : public CXXNamedCastExpr { 179public: 180 CXXReinterpretCastExpr(QualType ty, Expr *op, QualType writtenTy, 181 SourceLocation l) 182 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, op, writtenTy, l) {} 183 184 static bool classof(const Stmt *T) { 185 return T->getStmtClass() == CXXReinterpretCastExprClass; 186 } 187 static bool classof(const CXXReinterpretCastExpr *) { return true; } 188}; 189 190/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 191/// which can remove type qualifiers but does not change the underlying value. 192/// 193/// This expression node represents a const cast, e.g., 194/// @c const_cast<char*>(PtrToConstChar). 195class CXXConstCastExpr : public CXXNamedCastExpr { 196public: 197 CXXConstCastExpr(QualType ty, Expr *op, QualType writtenTy, 198 SourceLocation l) 199 : CXXNamedCastExpr(CXXConstCastExprClass, ty, op, writtenTy, l) {} 200 201 static bool classof(const Stmt *T) { 202 return T->getStmtClass() == CXXConstCastExprClass; 203 } 204 static bool classof(const CXXConstCastExpr *) { return true; } 205}; 206 207/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 208/// 209class CXXBoolLiteralExpr : public Expr { 210 bool Value; 211 SourceLocation Loc; 212public: 213 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 214 Expr(CXXBoolLiteralExprClass, Ty), Value(val), Loc(l) {} 215 216 CXXBoolLiteralExpr* Clone(ASTContext &C) const; 217 218 bool getValue() const { return Value; } 219 220 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 221 222 static bool classof(const Stmt *T) { 223 return T->getStmtClass() == CXXBoolLiteralExprClass; 224 } 225 static bool classof(const CXXBoolLiteralExpr *) { return true; } 226 227 // Iterators 228 virtual child_iterator child_begin(); 229 virtual child_iterator child_end(); 230}; 231 232/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal 233class CXXNullPtrLiteralExpr : public Expr { 234 SourceLocation Loc; 235public: 236 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) : 237 Expr(CXXNullPtrLiteralExprClass, Ty), Loc(l) {} 238 239 CXXNullPtrLiteralExpr* Clone(ASTContext &C) const; 240 241 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 242 243 static bool classof(const Stmt *T) { 244 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 245 } 246 static bool classof(const CXXNullPtrLiteralExpr *) { return true; } 247 248 virtual child_iterator child_begin(); 249 virtual child_iterator child_end(); 250}; 251 252/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 253/// the type_info that corresponds to the supplied type, or the (possibly 254/// dynamic) type of the supplied expression. 255/// 256/// This represents code like @c typeid(int) or @c typeid(*objPtr) 257class CXXTypeidExpr : public Expr { 258private: 259 bool isTypeOp : 1; 260 union { 261 void *Ty; 262 Stmt *Ex; 263 } Operand; 264 SourceRange Range; 265 266public: 267 CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) : 268 Expr(CXXTypeidExprClass, Ty, 269 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 270 false, 271 // typeid is value-dependent if the type or expression are dependent 272 (isTypeOp ? QualType::getFromOpaquePtr(op)->isDependentType() 273 : static_cast<Expr*>(op)->isValueDependent())), 274 isTypeOp(isTypeOp), Range(r) { 275 if (isTypeOp) 276 Operand.Ty = op; 277 else 278 // op was an Expr*, so cast it back to that to be safe 279 Operand.Ex = static_cast<Expr*>(op); 280 } 281 282 bool isTypeOperand() const { return isTypeOp; } 283 QualType getTypeOperand() const { 284 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 285 return QualType::getFromOpaquePtr(Operand.Ty); 286 } 287 Expr* getExprOperand() const { 288 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 289 return static_cast<Expr*>(Operand.Ex); 290 } 291 292 virtual SourceRange getSourceRange() const { 293 return Range; 294 } 295 static bool classof(const Stmt *T) { 296 return T->getStmtClass() == CXXTypeidExprClass; 297 } 298 static bool classof(const CXXTypeidExpr *) { return true; } 299 300 // Iterators 301 virtual child_iterator child_begin(); 302 virtual child_iterator child_end(); 303}; 304 305/// CXXThisExpr - Represents the "this" expression in C++, which is a 306/// pointer to the object on which the current member function is 307/// executing (C++ [expr.prim]p3). Example: 308/// 309/// @code 310/// class Foo { 311/// public: 312/// void bar(); 313/// void test() { this->bar(); } 314/// }; 315/// @endcode 316class CXXThisExpr : public Expr { 317 SourceLocation Loc; 318 319public: 320 CXXThisExpr(SourceLocation L, QualType Type) 321 : Expr(CXXThisExprClass, Type, 322 // 'this' is type-dependent if the class type of the enclosing 323 // member function is dependent (C++ [temp.dep.expr]p2) 324 Type->isDependentType(), Type->isDependentType()), 325 Loc(L) { } 326 327 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 328 329 static bool classof(const Stmt *T) { 330 return T->getStmtClass() == CXXThisExprClass; 331 } 332 static bool classof(const CXXThisExpr *) { return true; } 333 334 // Iterators 335 virtual child_iterator child_begin(); 336 virtual child_iterator child_end(); 337}; 338 339/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 340/// 'throw' and 'throw' assignment-expression. When 341/// assignment-expression isn't present, Op will be null. 342/// 343class CXXThrowExpr : public Expr { 344 Stmt *Op; 345 SourceLocation ThrowLoc; 346public: 347 // Ty is the void type which is used as the result type of the 348 // exepression. The l is the location of the throw keyword. expr 349 // can by null, if the optional expression to throw isn't present. 350 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 351 Expr(CXXThrowExprClass, Ty, false, false), Op(expr), ThrowLoc(l) {} 352 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 353 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 354 void setSubExpr(Expr *E) { Op = E; } 355 356 SourceLocation getThrowLoc() const { return ThrowLoc; } 357 void setThrowLoc(SourceLocation L) { ThrowLoc = L; } 358 359 virtual SourceRange getSourceRange() const { 360 if (getSubExpr() == 0) 361 return SourceRange(ThrowLoc, ThrowLoc); 362 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 363 } 364 365 static bool classof(const Stmt *T) { 366 return T->getStmtClass() == CXXThrowExprClass; 367 } 368 static bool classof(const CXXThrowExpr *) { return true; } 369 370 // Iterators 371 virtual child_iterator child_begin(); 372 virtual child_iterator child_end(); 373}; 374 375/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 376/// function call argument that was created from the corresponding 377/// parameter's default argument, when the call did not explicitly 378/// supply arguments for all of the parameters. 379class CXXDefaultArgExpr : public Expr { 380 ParmVarDecl *Param; 381public: 382 // Param is the parameter whose default argument is used by this 383 // expression. 384 explicit CXXDefaultArgExpr(ParmVarDecl *param) 385 : Expr(CXXDefaultArgExprClass, 386 param->hasUnparsedDefaultArg()? param->getType().getNonReferenceType() 387 : param->getDefaultArg()->getType()), 388 Param(param) { } 389 390 // Retrieve the parameter that the argument was created from. 391 const ParmVarDecl *getParam() const { return Param; } 392 ParmVarDecl *getParam() { return Param; } 393 394 // Retrieve the actual argument to the function call. 395 const Expr *getExpr() const { return Param->getDefaultArg(); } 396 Expr *getExpr() { return Param->getDefaultArg(); } 397 398 virtual SourceRange getSourceRange() const { 399 // Default argument expressions have no representation in the 400 // source, so they have an empty source range. 401 return SourceRange(); 402 } 403 404 static bool classof(const Stmt *T) { 405 return T->getStmtClass() == CXXDefaultArgExprClass; 406 } 407 static bool classof(const CXXDefaultArgExpr *) { return true; } 408 409 // Iterators 410 virtual child_iterator child_begin(); 411 virtual child_iterator child_end(); 412}; 413 414/// CXXConstructExpr - Represents a call to a C++ constructor. 415class CXXConstructExpr : public Expr { 416 VarDecl *VD; 417 CXXConstructorDecl *Constructor; 418 419 bool Elidable; 420 421 Stmt **Args; 422 unsigned NumArgs; 423 424 425protected: 426 CXXConstructExpr(ASTContext &C, StmtClass SC, VarDecl *vd, QualType T, 427 CXXConstructorDecl *d, bool elidable, 428 Expr **args, unsigned numargs); 429 ~CXXConstructExpr() { } 430 431public: 432 static CXXConstructExpr *Create(ASTContext &C, VarDecl *VD, QualType T, 433 CXXConstructorDecl *D, bool Elidable, 434 Expr **Args, unsigned NumArgs); 435 436 void Destroy(ASTContext &C); 437 438 VarDecl* getVarDecl() const { return VD; } 439 CXXConstructorDecl* getConstructor() const { return Constructor; } 440 441 /// \brief Whether this construction is elidable. 442 bool isElidable() const { return Elidable; } 443 444 typedef ExprIterator arg_iterator; 445 typedef ConstExprIterator const_arg_iterator; 446 447 arg_iterator arg_begin() { return Args; } 448 arg_iterator arg_end() { return Args + NumArgs; } 449 const_arg_iterator arg_begin() const { return Args; } 450 const_arg_iterator arg_end() const { return Args + NumArgs; } 451 452 unsigned getNumArgs() const { return NumArgs; } 453 454 virtual SourceRange getSourceRange() const { return SourceRange(); } 455 456 static bool classof(const Stmt *T) { 457 return T->getStmtClass() == CXXConstructExprClass || 458 T->getStmtClass() == CXXTemporaryObjectExprClass; 459 } 460 static bool classof(const CXXConstructExpr *) { return true; } 461 462 // Iterators 463 virtual child_iterator child_begin(); 464 virtual child_iterator child_end(); 465}; 466 467/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 468/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 469/// x = int(0.5); 470class CXXFunctionalCastExpr : public ExplicitCastExpr { 471 SourceLocation TyBeginLoc; 472 SourceLocation RParenLoc; 473public: 474 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 475 SourceLocation tyBeginLoc, Expr *castExpr, 476 SourceLocation rParenLoc) : 477 ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy), 478 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 479 480 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 481 SourceLocation getRParenLoc() const { return RParenLoc; } 482 483 virtual SourceRange getSourceRange() const { 484 return SourceRange(TyBeginLoc, RParenLoc); 485 } 486 static bool classof(const Stmt *T) { 487 return T->getStmtClass() == CXXFunctionalCastExprClass; 488 } 489 static bool classof(const CXXFunctionalCastExpr *) { return true; } 490}; 491 492/// @brief Represents a C++ functional cast expression that builds a 493/// temporary object. 494/// 495/// This expression type represents a C++ "functional" cast 496/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 497/// constructor to build a temporary object. If N == 0 but no 498/// constructor will be called (because the functional cast is 499/// performing a value-initialized an object whose class type has no 500/// user-declared constructors), CXXZeroInitValueExpr will represent 501/// the functional cast. Finally, with N == 1 arguments the functional 502/// cast expression will be represented by CXXFunctionalCastExpr. 503/// Example: 504/// @code 505/// struct X { X(int, float); } 506/// 507/// X create_X() { 508/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 509/// }; 510/// @endcode 511class CXXTemporaryObjectExpr : public CXXConstructExpr { 512 SourceLocation TyBeginLoc; 513 SourceLocation RParenLoc; 514 515public: 516 CXXTemporaryObjectExpr(ASTContext &C, VarDecl *vd, 517 CXXConstructorDecl *Cons, QualType writtenTy, 518 SourceLocation tyBeginLoc, Expr **Args, 519 unsigned NumArgs, SourceLocation rParenLoc); 520 521 ~CXXTemporaryObjectExpr() { } 522 523 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 524 SourceLocation getRParenLoc() const { return RParenLoc; } 525 526 virtual SourceRange getSourceRange() const { 527 return SourceRange(TyBeginLoc, RParenLoc); 528 } 529 static bool classof(const Stmt *T) { 530 return T->getStmtClass() == CXXTemporaryObjectExprClass; 531 } 532 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 533}; 534 535/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 536/// Expression "T()" which creates a value-initialized rvalue of type 537/// T, which is either a non-class type or a class type without any 538/// user-defined constructors. 539/// 540class CXXZeroInitValueExpr : public Expr { 541 SourceLocation TyBeginLoc; 542 SourceLocation RParenLoc; 543 544public: 545 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 546 SourceLocation rParenLoc ) : 547 Expr(CXXZeroInitValueExprClass, ty, false, false), 548 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 549 550 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 551 SourceLocation getRParenLoc() const { return RParenLoc; } 552 553 /// @brief Whether this initialization expression was 554 /// implicitly-generated. 555 bool isImplicit() const { 556 return TyBeginLoc.isInvalid() && RParenLoc.isInvalid(); 557 } 558 559 virtual SourceRange getSourceRange() const { 560 return SourceRange(TyBeginLoc, RParenLoc); 561 } 562 563 CXXZeroInitValueExpr* Clone(ASTContext &C) const; 564 565 static bool classof(const Stmt *T) { 566 return T->getStmtClass() == CXXZeroInitValueExprClass; 567 } 568 static bool classof(const CXXZeroInitValueExpr *) { return true; } 569 570 // Iterators 571 virtual child_iterator child_begin(); 572 virtual child_iterator child_end(); 573}; 574 575/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 576/// statement, e.g: "if (int x = f()) {...}". 577/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 578/// decl that it references. 579/// 580class CXXConditionDeclExpr : public DeclRefExpr { 581public: 582 CXXConditionDeclExpr(SourceLocation startLoc, 583 SourceLocation eqLoc, VarDecl *var) 584 : DeclRefExpr(CXXConditionDeclExprClass, var, 585 var->getType().getNonReferenceType(), startLoc, 586 var->getType()->isDependentType(), 587 /*FIXME:integral constant?*/ 588 var->getType()->isDependentType()) {} 589 590 virtual void Destroy(ASTContext& Ctx); 591 592 SourceLocation getStartLoc() const { return getLocation(); } 593 594 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 595 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 596 597 virtual SourceRange getSourceRange() const { 598 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 599 } 600 601 static bool classof(const Stmt *T) { 602 return T->getStmtClass() == CXXConditionDeclExprClass; 603 } 604 static bool classof(const CXXConditionDeclExpr *) { return true; } 605 606 // Iterators 607 virtual child_iterator child_begin(); 608 virtual child_iterator child_end(); 609}; 610 611/// CXXNewExpr - A new expression for memory allocation and constructor calls, 612/// e.g: "new CXXNewExpr(foo)". 613class CXXNewExpr : public Expr { 614 // Was the usage ::new, i.e. is the global new to be used? 615 bool GlobalNew : 1; 616 // Was the form (type-id) used? Otherwise, it was new-type-id. 617 bool ParenTypeId : 1; 618 // Is there an initializer? If not, built-ins are uninitialized, else they're 619 // value-initialized. 620 bool Initializer : 1; 621 // Do we allocate an array? If so, the first SubExpr is the size expression. 622 bool Array : 1; 623 // The number of placement new arguments. 624 unsigned NumPlacementArgs : 14; 625 // The number of constructor arguments. This may be 1 even for non-class 626 // types; use the pseudo copy constructor. 627 unsigned NumConstructorArgs : 14; 628 // Contains an optional array size expression, any number of optional 629 // placement arguments, and any number of optional constructor arguments, 630 // in that order. 631 Stmt **SubExprs; 632 // Points to the allocation function used. 633 FunctionDecl *OperatorNew; 634 // Points to the deallocation function used in case of error. May be null. 635 FunctionDecl *OperatorDelete; 636 // Points to the constructor used. Cannot be null if AllocType is a record; 637 // it would still point at the default constructor (even an implicit one). 638 // Must be null for all other types. 639 CXXConstructorDecl *Constructor; 640 641 SourceLocation StartLoc; 642 SourceLocation EndLoc; 643 644public: 645 CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs, 646 unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize, 647 CXXConstructorDecl *constructor, bool initializer, 648 Expr **constructorArgs, unsigned numConsArgs, 649 FunctionDecl *operatorDelete, QualType ty, 650 SourceLocation startLoc, SourceLocation endLoc); 651 ~CXXNewExpr() { 652 delete[] SubExprs; 653 } 654 655 QualType getAllocatedType() const { 656 assert(getType()->isPointerType()); 657 return getType()->getAsPointerType()->getPointeeType(); 658 } 659 660 FunctionDecl *getOperatorNew() const { return OperatorNew; } 661 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 662 CXXConstructorDecl *getConstructor() const { return Constructor; } 663 664 bool isArray() const { return Array; } 665 Expr *getArraySize() { 666 return Array ? cast<Expr>(SubExprs[0]) : 0; 667 } 668 const Expr *getArraySize() const { 669 return Array ? cast<Expr>(SubExprs[0]) : 0; 670 } 671 672 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 673 Expr *getPlacementArg(unsigned i) { 674 assert(i < NumPlacementArgs && "Index out of range"); 675 return cast<Expr>(SubExprs[Array + i]); 676 } 677 const Expr *getPlacementArg(unsigned i) const { 678 assert(i < NumPlacementArgs && "Index out of range"); 679 return cast<Expr>(SubExprs[Array + i]); 680 } 681 682 bool isGlobalNew() const { return GlobalNew; } 683 bool isParenTypeId() const { return ParenTypeId; } 684 bool hasInitializer() const { return Initializer; } 685 686 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 687 Expr *getConstructorArg(unsigned i) { 688 assert(i < NumConstructorArgs && "Index out of range"); 689 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 690 } 691 const Expr *getConstructorArg(unsigned i) const { 692 assert(i < NumConstructorArgs && "Index out of range"); 693 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 694 } 695 696 typedef ExprIterator arg_iterator; 697 typedef ConstExprIterator const_arg_iterator; 698 699 arg_iterator placement_arg_begin() { 700 return SubExprs + Array; 701 } 702 arg_iterator placement_arg_end() { 703 return SubExprs + Array + getNumPlacementArgs(); 704 } 705 const_arg_iterator placement_arg_begin() const { 706 return SubExprs + Array; 707 } 708 const_arg_iterator placement_arg_end() const { 709 return SubExprs + Array + getNumPlacementArgs(); 710 } 711 712 arg_iterator constructor_arg_begin() { 713 return SubExprs + Array + getNumPlacementArgs(); 714 } 715 arg_iterator constructor_arg_end() { 716 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 717 } 718 const_arg_iterator constructor_arg_begin() const { 719 return SubExprs + Array + getNumPlacementArgs(); 720 } 721 const_arg_iterator constructor_arg_end() const { 722 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 723 } 724 725 virtual SourceRange getSourceRange() const { 726 return SourceRange(StartLoc, EndLoc); 727 } 728 729 static bool classof(const Stmt *T) { 730 return T->getStmtClass() == CXXNewExprClass; 731 } 732 static bool classof(const CXXNewExpr *) { return true; } 733 734 // Iterators 735 virtual child_iterator child_begin(); 736 virtual child_iterator child_end(); 737}; 738 739/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 740/// calls, e.g. "delete[] pArray". 741class CXXDeleteExpr : public Expr { 742 // Is this a forced global delete, i.e. "::delete"? 743 bool GlobalDelete : 1; 744 // Is this the array form of delete, i.e. "delete[]"? 745 bool ArrayForm : 1; 746 // Points to the operator delete overload that is used. Could be a member. 747 FunctionDecl *OperatorDelete; 748 // The pointer expression to be deleted. 749 Stmt *Argument; 750 // Location of the expression. 751 SourceLocation Loc; 752public: 753 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 754 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 755 : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete), 756 ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg), 757 Loc(loc) { } 758 759 bool isGlobalDelete() const { return GlobalDelete; } 760 bool isArrayForm() const { return ArrayForm; } 761 762 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 763 764 Expr *getArgument() { return cast<Expr>(Argument); } 765 const Expr *getArgument() const { return cast<Expr>(Argument); } 766 767 virtual SourceRange getSourceRange() const { 768 return SourceRange(Loc, Argument->getLocEnd()); 769 } 770 771 static bool classof(const Stmt *T) { 772 return T->getStmtClass() == CXXDeleteExprClass; 773 } 774 static bool classof(const CXXDeleteExpr *) { return true; } 775 776 // Iterators 777 virtual child_iterator child_begin(); 778 virtual child_iterator child_end(); 779}; 780 781/// \brief Represents the name of a function that has not been 782/// resolved to any declaration. 783/// 784/// Unresolved function names occur when a function name is 785/// encountered prior to an open parentheses ('(') in a C++ function 786/// call, and the function name itself did not resolve to a 787/// declaration. These function names can only be resolved when they 788/// form the postfix-expression of a function call, so that 789/// argument-dependent lookup finds declarations corresponding to 790/// these functions. 791 792/// @code 793/// template<typename T> void f(T x) { 794/// g(x); // g is an unresolved function name (that is also a dependent name) 795/// } 796/// @endcode 797class UnresolvedFunctionNameExpr : public Expr { 798 /// The name that was present in the source 799 DeclarationName Name; 800 801 /// The location of this name in the source code 802 SourceLocation Loc; 803 804public: 805 UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L) 806 : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { } 807 808 /// \brief Retrieves the name that occurred in the source code. 809 DeclarationName getName() const { return Name; } 810 811 /// getLocation - Retrieves the location in the source code where 812 /// the name occurred. 813 SourceLocation getLocation() const { return Loc; } 814 815 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 816 817 UnresolvedFunctionNameExpr* Clone(ASTContext &C) const; 818 819 static bool classof(const Stmt *T) { 820 return T->getStmtClass() == UnresolvedFunctionNameExprClass; 821 } 822 static bool classof(const UnresolvedFunctionNameExpr *) { return true; } 823 824 // Iterators 825 virtual child_iterator child_begin(); 826 virtual child_iterator child_end(); 827}; 828 829/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 830/// implementation of TR1/C++0x type trait templates. 831/// Example: 832/// __is_pod(int) == true 833/// __is_enum(std::string) == false 834class UnaryTypeTraitExpr : public Expr { 835 /// UTT - The trait. 836 UnaryTypeTrait UTT; 837 838 /// Loc - The location of the type trait keyword. 839 SourceLocation Loc; 840 841 /// RParen - The location of the closing paren. 842 SourceLocation RParen; 843 844 /// QueriedType - The type we're testing. 845 QualType QueriedType; 846 847public: 848 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried, 849 SourceLocation rparen, QualType ty) 850 : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()), 851 UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { } 852 853 virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 854 855 UnaryTypeTrait getTrait() const { return UTT; } 856 857 QualType getQueriedType() const { return QueriedType; } 858 859 bool EvaluateTrait() const; 860 861 static bool classof(const Stmt *T) { 862 return T->getStmtClass() == UnaryTypeTraitExprClass; 863 } 864 static bool classof(const UnaryTypeTraitExpr *) { return true; } 865 866 // Iterators 867 virtual child_iterator child_begin(); 868 virtual child_iterator child_end(); 869}; 870 871/// QualifiedDeclRefExpr - A reference to a declared variable, 872/// function, enum, etc., that includes a qualification, e.g., 873/// "N::foo". 874class QualifiedDeclRefExpr : public DeclRefExpr { 875 /// QualifierRange - The source range that covers the 876 /// nested-name-specifier. 877 SourceRange QualifierRange; 878 879 /// \brief The nested-name-specifier that qualifies this declaration 880 /// name. 881 NestedNameSpecifier *NNS; 882 883public: 884 QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, 885 bool VD, SourceRange R, NestedNameSpecifier *NNS) 886 : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD), 887 QualifierRange(R), NNS(NNS) { } 888 889 /// \brief Retrieve the source range of the nested-name-specifier. 890 SourceRange getQualifierRange() const { return QualifierRange; } 891 892 /// \brief Retrieve the nested-name-specifier that qualifies this 893 /// declaration. 894 NestedNameSpecifier *getQualifier() const { return NNS; } 895 896 virtual SourceRange getSourceRange() const { 897 return SourceRange(QualifierRange.getBegin(), getLocation()); 898 } 899 900 static bool classof(const Stmt *T) { 901 return T->getStmtClass() == QualifiedDeclRefExprClass; 902 } 903 static bool classof(const QualifiedDeclRefExpr *) { return true; } 904}; 905 906/// \brief A qualified reference to a name whose declaration cannot 907/// yet be resolved. 908/// 909/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that 910/// it expresses a qualified reference to a declaration such as 911/// X<T>::value. The difference, however, is that an 912/// UnresolvedDeclRefExpr node is used only within C++ templates when 913/// the qualification (e.g., X<T>::) refers to a dependent type. In 914/// this case, X<T>::value cannot resolve to a declaration because the 915/// declaration will differ from on instantiation of X<T> to the 916/// next. Therefore, UnresolvedDeclRefExpr keeps track of the 917/// qualifier (X<T>::) and the name of the entity being referenced 918/// ("value"). Such expressions will instantiate to 919/// QualifiedDeclRefExprs. 920class UnresolvedDeclRefExpr : public Expr { 921 /// The name of the entity we will be referencing. 922 DeclarationName Name; 923 924 /// Location of the name of the declaration we're referencing. 925 SourceLocation Loc; 926 927 /// QualifierRange - The source range that covers the 928 /// nested-name-specifier. 929 SourceRange QualifierRange; 930 931 /// \brief The nested-name-specifier that qualifies this unresolved 932 /// declaration name. 933 NestedNameSpecifier *NNS; 934 935public: 936 UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L, 937 SourceRange R, NestedNameSpecifier *NNS) 938 : Expr(UnresolvedDeclRefExprClass, T, true, true), 939 Name(N), Loc(L), QualifierRange(R), NNS(NNS) { } 940 941 /// \brief Retrieve the name that this expression refers to. 942 DeclarationName getDeclName() const { return Name; } 943 944 /// \brief Retrieve the location of the name within the expression. 945 SourceLocation getLocation() const { return Loc; } 946 947 /// \brief Retrieve the source range of the nested-name-specifier. 948 SourceRange getQualifierRange() const { return QualifierRange; } 949 950 /// \brief Retrieve the nested-name-specifier that qualifies this 951 /// declaration. 952 NestedNameSpecifier *getQualifier() const { return NNS; } 953 954 virtual SourceRange getSourceRange() const { 955 return SourceRange(QualifierRange.getBegin(), getLocation()); 956 } 957 958 static bool classof(const Stmt *T) { 959 return T->getStmtClass() == UnresolvedDeclRefExprClass; 960 } 961 static bool classof(const UnresolvedDeclRefExpr *) { return true; } 962 963 virtual StmtIterator child_begin(); 964 virtual StmtIterator child_end(); 965}; 966 967class CXXExprWithTemporaries : public Expr { 968 Stmt *SubExpr; 969 970 CXXTempVarDecl **Decls; 971 unsigned NumDecls; 972 973public: 974 CXXExprWithTemporaries(Expr *subexpr, CXXTempVarDecl **decls, 975 unsigned numdecls); 976 ~CXXExprWithTemporaries(); 977 978 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 979 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 980 981 virtual SourceRange getSourceRange() const { return SourceRange(); } 982 983 // Implement isa/cast/dyncast/etc. 984 static bool classof(const Stmt *T) { 985 return T->getStmtClass() == CXXExprWithTemporariesClass; 986 } 987 static bool classof(const CXXExprWithTemporaries *) { return true; } 988 989 // Iterators 990 virtual child_iterator child_begin(); 991 virtual child_iterator child_end(); 992}; 993 994/// \brief Describes an explicit type conversion that uses functional 995/// notion but could not be resolved because one or more arguments are 996/// type-dependent. 997/// 998/// The explicit type conversions expressed by 999/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 1000/// where \c T is some type and \c a1, a2, ..., aN are values, and 1001/// either \C T is a dependent type or one or more of the \c a's is 1002/// type-dependent. For example, this would occur in a template such 1003/// as: 1004/// 1005/// \code 1006/// template<typename T, typename A1> 1007/// inline T make_a(const A1& a1) { 1008/// return T(a1); 1009/// } 1010/// \endcode 1011/// 1012/// When the returned expression is instantiated, it may resolve to a 1013/// constructor call, conversion function call, or some kind of type 1014/// conversion. 1015class CXXUnresolvedConstructExpr : public Expr { 1016 /// \brief The starting location of the type 1017 SourceLocation TyBeginLoc; 1018 1019 /// \brief The type being constructed. 1020 QualType Type; 1021 1022 /// \brief The location of the left parentheses ('('). 1023 SourceLocation LParenLoc; 1024 1025 /// \brief The location of the right parentheses (')'). 1026 SourceLocation RParenLoc; 1027 1028 /// \brief The number of arguments used to construct the type. 1029 unsigned NumArgs; 1030 1031 CXXUnresolvedConstructExpr(SourceLocation TyBegin, 1032 QualType T, 1033 SourceLocation LParenLoc, 1034 Expr **Args, 1035 unsigned NumArgs, 1036 SourceLocation RParenLoc); 1037 1038public: 1039 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 1040 SourceLocation TyBegin, 1041 QualType T, 1042 SourceLocation LParenLoc, 1043 Expr **Args, 1044 unsigned NumArgs, 1045 SourceLocation RParenLoc); 1046 1047 /// \brief Retrieve the source location where the type begins. 1048 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 1049 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 1050 1051 /// \brief Retrieve the type that is being constructed, as specified 1052 /// in the source code. 1053 QualType getTypeAsWritten() const { return Type; } 1054 void setTypeAsWritten(QualType T) { Type = T; } 1055 1056 /// \brief Retrieve the location of the left parentheses ('(') that 1057 /// precedes the argument list. 1058 SourceLocation getLParenLoc() const { return LParenLoc; } 1059 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1060 1061 /// \brief Retrieve the location of the right parentheses (')') that 1062 /// follows the argument list. 1063 SourceLocation getRParenLoc() const { return RParenLoc; } 1064 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1065 1066 /// \brief Retrieve the number of arguments. 1067 unsigned arg_size() const { return NumArgs; } 1068 1069 typedef Expr** arg_iterator; 1070 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 1071 arg_iterator arg_end() { return arg_begin() + NumArgs; } 1072 1073 Expr *getArg(unsigned I) { 1074 assert(I < NumArgs && "Argument index out-of-range"); 1075 return *(arg_begin() + I); 1076 } 1077 1078 virtual SourceRange getSourceRange() const { 1079 return SourceRange(TyBeginLoc, RParenLoc); 1080 } 1081 static bool classof(const Stmt *T) { 1082 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 1083 } 1084 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 1085 1086 // Iterators 1087 virtual child_iterator child_begin(); 1088 virtual child_iterator child_end(); 1089}; 1090 1091} // end namespace clang 1092 1093#endif 1094