ExprCXX.h revision a3a7b8eea87c90a5a257f685749222b212ddaf36
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 355 virtual SourceRange getSourceRange() const { 356 if (getSubExpr() == 0) 357 return SourceRange(ThrowLoc, ThrowLoc); 358 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 359 } 360 361 static bool classof(const Stmt *T) { 362 return T->getStmtClass() == CXXThrowExprClass; 363 } 364 static bool classof(const CXXThrowExpr *) { return true; } 365 366 // Iterators 367 virtual child_iterator child_begin(); 368 virtual child_iterator child_end(); 369}; 370 371/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 372/// function call argument that was created from the corresponding 373/// parameter's default argument, when the call did not explicitly 374/// supply arguments for all of the parameters. 375class CXXDefaultArgExpr : public Expr { 376 ParmVarDecl *Param; 377public: 378 // Param is the parameter whose default argument is used by this 379 // expression. 380 explicit CXXDefaultArgExpr(ParmVarDecl *param) 381 : Expr(CXXDefaultArgExprClass, 382 param->hasUnparsedDefaultArg()? param->getType().getNonReferenceType() 383 : param->getDefaultArg()->getType()), 384 Param(param) { } 385 386 // Retrieve the parameter that the argument was created from. 387 const ParmVarDecl *getParam() const { return Param; } 388 ParmVarDecl *getParam() { return Param; } 389 390 // Retrieve the actual argument to the function call. 391 const Expr *getExpr() const { return Param->getDefaultArg(); } 392 Expr *getExpr() { return Param->getDefaultArg(); } 393 394 virtual SourceRange getSourceRange() const { 395 // Default argument expressions have no representation in the 396 // source, so they have an empty source range. 397 return SourceRange(); 398 } 399 400 static bool classof(const Stmt *T) { 401 return T->getStmtClass() == CXXDefaultArgExprClass; 402 } 403 static bool classof(const CXXDefaultArgExpr *) { return true; } 404 405 // Iterators 406 virtual child_iterator child_begin(); 407 virtual child_iterator child_end(); 408}; 409 410/// CXXConstructExpr - Represents a call to a C++ constructor. 411class CXXConstructExpr : public Expr { 412 VarDecl *VD; 413 CXXConstructorDecl *Constructor; 414 415 bool Elidable; 416 417 Stmt **Args; 418 unsigned NumArgs; 419 420 421protected: 422 CXXConstructExpr(ASTContext &C, StmtClass SC, VarDecl *vd, QualType T, 423 CXXConstructorDecl *d, bool elidable, 424 Expr **args, unsigned numargs); 425 ~CXXConstructExpr() { } 426 427public: 428 static CXXConstructExpr *Create(ASTContext &C, VarDecl *VD, QualType T, 429 CXXConstructorDecl *D, bool Elidable, 430 Expr **Args, unsigned NumArgs); 431 432 void Destroy(ASTContext &C); 433 434 const VarDecl* getVarDecl() const { return VD; } 435 const CXXConstructorDecl* getConstructor() const { return Constructor; } 436 437 typedef ExprIterator arg_iterator; 438 typedef ConstExprIterator const_arg_iterator; 439 440 arg_iterator arg_begin() { return Args; } 441 arg_iterator arg_end() { return Args + NumArgs; } 442 const_arg_iterator arg_begin() const { return Args; } 443 const_arg_iterator arg_end() const { return Args + NumArgs; } 444 445 unsigned getNumArgs() const { return NumArgs; } 446 447 virtual SourceRange getSourceRange() const { return SourceRange(); } 448 449 static bool classof(const Stmt *T) { 450 return T->getStmtClass() == CXXConstructExprClass || 451 T->getStmtClass() == CXXTemporaryObjectExprClass; 452 } 453 static bool classof(const CXXConstructExpr *) { return true; } 454 455 // Iterators 456 virtual child_iterator child_begin(); 457 virtual child_iterator child_end(); 458}; 459 460/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 461/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 462/// x = int(0.5); 463class CXXFunctionalCastExpr : public ExplicitCastExpr { 464 SourceLocation TyBeginLoc; 465 SourceLocation RParenLoc; 466public: 467 CXXFunctionalCastExpr(QualType ty, QualType writtenTy, 468 SourceLocation tyBeginLoc, Expr *castExpr, 469 SourceLocation rParenLoc) : 470 ExplicitCastExpr(CXXFunctionalCastExprClass, ty, castExpr, writtenTy), 471 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 472 473 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 474 SourceLocation getRParenLoc() const { return RParenLoc; } 475 476 virtual SourceRange getSourceRange() const { 477 return SourceRange(TyBeginLoc, RParenLoc); 478 } 479 static bool classof(const Stmt *T) { 480 return T->getStmtClass() == CXXFunctionalCastExprClass; 481 } 482 static bool classof(const CXXFunctionalCastExpr *) { return true; } 483}; 484 485/// @brief Represents a C++ functional cast expression that builds a 486/// temporary object. 487/// 488/// This expression type represents a C++ "functional" cast 489/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 490/// constructor to build a temporary object. If N == 0 but no 491/// constructor will be called (because the functional cast is 492/// performing a value-initialized an object whose class type has no 493/// user-declared constructors), CXXZeroInitValueExpr will represent 494/// the functional cast. Finally, with N == 1 arguments the functional 495/// cast expression will be represented by CXXFunctionalCastExpr. 496/// Example: 497/// @code 498/// struct X { X(int, float); } 499/// 500/// X create_X() { 501/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 502/// }; 503/// @endcode 504class CXXTemporaryObjectExpr : public CXXConstructExpr { 505 SourceLocation TyBeginLoc; 506 SourceLocation RParenLoc; 507 508public: 509 CXXTemporaryObjectExpr(ASTContext &C, VarDecl *vd, 510 CXXConstructorDecl *Cons, QualType writtenTy, 511 SourceLocation tyBeginLoc, Expr **Args, 512 unsigned NumArgs, SourceLocation rParenLoc); 513 514 ~CXXTemporaryObjectExpr() { } 515 516 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 517 SourceLocation getRParenLoc() const { return RParenLoc; } 518 519 virtual SourceRange getSourceRange() const { 520 return SourceRange(TyBeginLoc, RParenLoc); 521 } 522 static bool classof(const Stmt *T) { 523 return T->getStmtClass() == CXXTemporaryObjectExprClass; 524 } 525 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 526}; 527 528/// CXXZeroInitValueExpr - [C++ 5.2.3p2] 529/// Expression "T()" which creates a value-initialized rvalue of type 530/// T, which is either a non-class type or a class type without any 531/// user-defined constructors. 532/// 533class CXXZeroInitValueExpr : public Expr { 534 SourceLocation TyBeginLoc; 535 SourceLocation RParenLoc; 536 537public: 538 CXXZeroInitValueExpr(QualType ty, SourceLocation tyBeginLoc, 539 SourceLocation rParenLoc ) : 540 Expr(CXXZeroInitValueExprClass, ty), 541 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 542 543 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 544 SourceLocation getRParenLoc() const { return RParenLoc; } 545 546 /// @brief Whether this initialization expression was 547 /// implicitly-generated. 548 bool isImplicit() const { 549 return TyBeginLoc.isInvalid() && RParenLoc.isInvalid(); 550 } 551 552 virtual SourceRange getSourceRange() const { 553 return SourceRange(TyBeginLoc, RParenLoc); 554 } 555 556 static bool classof(const Stmt *T) { 557 return T->getStmtClass() == CXXZeroInitValueExprClass; 558 } 559 static bool classof(const CXXZeroInitValueExpr *) { return true; } 560 561 // Iterators 562 virtual child_iterator child_begin(); 563 virtual child_iterator child_end(); 564}; 565 566/// CXXConditionDeclExpr - Condition declaration of a if/switch/while/for 567/// statement, e.g: "if (int x = f()) {...}". 568/// The main difference with DeclRefExpr is that CXXConditionDeclExpr owns the 569/// decl that it references. 570/// 571class CXXConditionDeclExpr : public DeclRefExpr { 572public: 573 CXXConditionDeclExpr(SourceLocation startLoc, 574 SourceLocation eqLoc, VarDecl *var) 575 : DeclRefExpr(CXXConditionDeclExprClass, var, 576 var->getType().getNonReferenceType(), startLoc, 577 var->getType()->isDependentType(), 578 /*FIXME:integral constant?*/ 579 var->getType()->isDependentType()) {} 580 581 virtual void Destroy(ASTContext& Ctx); 582 583 SourceLocation getStartLoc() const { return getLocation(); } 584 585 VarDecl *getVarDecl() { return cast<VarDecl>(getDecl()); } 586 const VarDecl *getVarDecl() const { return cast<VarDecl>(getDecl()); } 587 588 virtual SourceRange getSourceRange() const { 589 return SourceRange(getStartLoc(), getVarDecl()->getInit()->getLocEnd()); 590 } 591 592 static bool classof(const Stmt *T) { 593 return T->getStmtClass() == CXXConditionDeclExprClass; 594 } 595 static bool classof(const CXXConditionDeclExpr *) { return true; } 596 597 // Iterators 598 virtual child_iterator child_begin(); 599 virtual child_iterator child_end(); 600}; 601 602/// CXXNewExpr - A new expression for memory allocation and constructor calls, 603/// e.g: "new CXXNewExpr(foo)". 604class CXXNewExpr : public Expr { 605 // Was the usage ::new, i.e. is the global new to be used? 606 bool GlobalNew : 1; 607 // Was the form (type-id) used? Otherwise, it was new-type-id. 608 bool ParenTypeId : 1; 609 // Is there an initializer? If not, built-ins are uninitialized, else they're 610 // value-initialized. 611 bool Initializer : 1; 612 // Do we allocate an array? If so, the first SubExpr is the size expression. 613 bool Array : 1; 614 // The number of placement new arguments. 615 unsigned NumPlacementArgs : 14; 616 // The number of constructor arguments. This may be 1 even for non-class 617 // types; use the pseudo copy constructor. 618 unsigned NumConstructorArgs : 14; 619 // Contains an optional array size expression, any number of optional 620 // placement arguments, and any number of optional constructor arguments, 621 // in that order. 622 Stmt **SubExprs; 623 // Points to the allocation function used. 624 FunctionDecl *OperatorNew; 625 // Points to the deallocation function used in case of error. May be null. 626 FunctionDecl *OperatorDelete; 627 // Points to the constructor used. Cannot be null if AllocType is a record; 628 // it would still point at the default constructor (even an implicit one). 629 // Must be null for all other types. 630 CXXConstructorDecl *Constructor; 631 632 SourceLocation StartLoc; 633 SourceLocation EndLoc; 634 635public: 636 CXXNewExpr(bool globalNew, FunctionDecl *operatorNew, Expr **placementArgs, 637 unsigned numPlaceArgs, bool ParenTypeId, Expr *arraySize, 638 CXXConstructorDecl *constructor, bool initializer, 639 Expr **constructorArgs, unsigned numConsArgs, 640 FunctionDecl *operatorDelete, QualType ty, 641 SourceLocation startLoc, SourceLocation endLoc); 642 ~CXXNewExpr() { 643 delete[] SubExprs; 644 } 645 646 QualType getAllocatedType() const { 647 assert(getType()->isPointerType()); 648 return getType()->getAsPointerType()->getPointeeType(); 649 } 650 651 FunctionDecl *getOperatorNew() const { return OperatorNew; } 652 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 653 CXXConstructorDecl *getConstructor() const { return Constructor; } 654 655 bool isArray() const { return Array; } 656 Expr *getArraySize() { 657 return Array ? cast<Expr>(SubExprs[0]) : 0; 658 } 659 const Expr *getArraySize() const { 660 return Array ? cast<Expr>(SubExprs[0]) : 0; 661 } 662 663 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 664 Expr *getPlacementArg(unsigned i) { 665 assert(i < NumPlacementArgs && "Index out of range"); 666 return cast<Expr>(SubExprs[Array + i]); 667 } 668 const Expr *getPlacementArg(unsigned i) const { 669 assert(i < NumPlacementArgs && "Index out of range"); 670 return cast<Expr>(SubExprs[Array + i]); 671 } 672 673 bool isGlobalNew() const { return GlobalNew; } 674 bool isParenTypeId() const { return ParenTypeId; } 675 bool hasInitializer() const { return Initializer; } 676 677 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 678 Expr *getConstructorArg(unsigned i) { 679 assert(i < NumConstructorArgs && "Index out of range"); 680 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 681 } 682 const Expr *getConstructorArg(unsigned i) const { 683 assert(i < NumConstructorArgs && "Index out of range"); 684 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 685 } 686 687 typedef ExprIterator arg_iterator; 688 typedef ConstExprIterator const_arg_iterator; 689 690 arg_iterator placement_arg_begin() { 691 return SubExprs + Array; 692 } 693 arg_iterator placement_arg_end() { 694 return SubExprs + Array + getNumPlacementArgs(); 695 } 696 const_arg_iterator placement_arg_begin() const { 697 return SubExprs + Array; 698 } 699 const_arg_iterator placement_arg_end() const { 700 return SubExprs + Array + getNumPlacementArgs(); 701 } 702 703 arg_iterator constructor_arg_begin() { 704 return SubExprs + Array + getNumPlacementArgs(); 705 } 706 arg_iterator constructor_arg_end() { 707 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 708 } 709 const_arg_iterator constructor_arg_begin() const { 710 return SubExprs + Array + getNumPlacementArgs(); 711 } 712 const_arg_iterator constructor_arg_end() const { 713 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 714 } 715 716 virtual SourceRange getSourceRange() const { 717 return SourceRange(StartLoc, EndLoc); 718 } 719 720 static bool classof(const Stmt *T) { 721 return T->getStmtClass() == CXXNewExprClass; 722 } 723 static bool classof(const CXXNewExpr *) { return true; } 724 725 // Iterators 726 virtual child_iterator child_begin(); 727 virtual child_iterator child_end(); 728}; 729 730/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 731/// calls, e.g. "delete[] pArray". 732class CXXDeleteExpr : public Expr { 733 // Is this a forced global delete, i.e. "::delete"? 734 bool GlobalDelete : 1; 735 // Is this the array form of delete, i.e. "delete[]"? 736 bool ArrayForm : 1; 737 // Points to the operator delete overload that is used. Could be a member. 738 FunctionDecl *OperatorDelete; 739 // The pointer expression to be deleted. 740 Stmt *Argument; 741 // Location of the expression. 742 SourceLocation Loc; 743public: 744 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 745 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 746 : Expr(CXXDeleteExprClass, ty, false, false), GlobalDelete(globalDelete), 747 ArrayForm(arrayForm), OperatorDelete(operatorDelete), Argument(arg), 748 Loc(loc) { } 749 750 bool isGlobalDelete() const { return GlobalDelete; } 751 bool isArrayForm() const { return ArrayForm; } 752 753 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 754 755 Expr *getArgument() { return cast<Expr>(Argument); } 756 const Expr *getArgument() const { return cast<Expr>(Argument); } 757 758 virtual SourceRange getSourceRange() const { 759 return SourceRange(Loc, Argument->getLocEnd()); 760 } 761 762 static bool classof(const Stmt *T) { 763 return T->getStmtClass() == CXXDeleteExprClass; 764 } 765 static bool classof(const CXXDeleteExpr *) { return true; } 766 767 // Iterators 768 virtual child_iterator child_begin(); 769 virtual child_iterator child_end(); 770}; 771 772/// \brief Represents the name of a function that has not been 773/// resolved to any declaration. 774/// 775/// Unresolved function names occur when a function name is 776/// encountered prior to an open parentheses ('(') in a C++ function 777/// call, and the function name itself did not resolve to a 778/// declaration. These function names can only be resolved when they 779/// form the postfix-expression of a function call, so that 780/// argument-dependent lookup finds declarations corresponding to 781/// these functions. 782 783/// @code 784/// template<typename T> void f(T x) { 785/// g(x); // g is an unresolved function name (that is also a dependent name) 786/// } 787/// @endcode 788class UnresolvedFunctionNameExpr : public Expr { 789 /// The name that was present in the source 790 DeclarationName Name; 791 792 /// The location of this name in the source code 793 SourceLocation Loc; 794 795public: 796 UnresolvedFunctionNameExpr(DeclarationName N, QualType T, SourceLocation L) 797 : Expr(UnresolvedFunctionNameExprClass, T, false, false), Name(N), Loc(L) { } 798 799 /// \brief Retrieves the name that occurred in the source code. 800 DeclarationName getName() const { return Name; } 801 802 /// getLocation - Retrieves the location in the source code where 803 /// the name occurred. 804 SourceLocation getLocation() const { return Loc; } 805 806 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 807 808 UnresolvedFunctionNameExpr* Clone(ASTContext &C) const; 809 810 static bool classof(const Stmt *T) { 811 return T->getStmtClass() == UnresolvedFunctionNameExprClass; 812 } 813 static bool classof(const UnresolvedFunctionNameExpr *) { return true; } 814 815 // Iterators 816 virtual child_iterator child_begin(); 817 virtual child_iterator child_end(); 818}; 819 820/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 821/// implementation of TR1/C++0x type trait templates. 822/// Example: 823/// __is_pod(int) == true 824/// __is_enum(std::string) == false 825class UnaryTypeTraitExpr : public Expr { 826 /// UTT - The trait. 827 UnaryTypeTrait UTT; 828 829 /// Loc - The location of the type trait keyword. 830 SourceLocation Loc; 831 832 /// RParen - The location of the closing paren. 833 SourceLocation RParen; 834 835 /// QueriedType - The type we're testing. 836 QualType QueriedType; 837 838public: 839 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, QualType queried, 840 SourceLocation rparen, QualType ty) 841 : Expr(UnaryTypeTraitExprClass, ty, false, queried->isDependentType()), 842 UTT(utt), Loc(loc), RParen(rparen), QueriedType(queried) { } 843 844 virtual SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 845 846 UnaryTypeTrait getTrait() const { return UTT; } 847 848 QualType getQueriedType() const { return QueriedType; } 849 850 bool EvaluateTrait() const; 851 852 static bool classof(const Stmt *T) { 853 return T->getStmtClass() == UnaryTypeTraitExprClass; 854 } 855 static bool classof(const UnaryTypeTraitExpr *) { return true; } 856 857 // Iterators 858 virtual child_iterator child_begin(); 859 virtual child_iterator child_end(); 860}; 861 862/// QualifiedDeclRefExpr - A reference to a declared variable, 863/// function, enum, etc., that includes a qualification, e.g., 864/// "N::foo". 865class QualifiedDeclRefExpr : public DeclRefExpr { 866 /// QualifierRange - The source range that covers the 867 /// nested-name-specifier. 868 SourceRange QualifierRange; 869 870 /// \brief The nested-name-specifier that qualifies this declaration 871 /// name. 872 NestedNameSpecifier *NNS; 873 874public: 875 QualifiedDeclRefExpr(NamedDecl *d, QualType t, SourceLocation l, bool TD, 876 bool VD, SourceRange R, NestedNameSpecifier *NNS) 877 : DeclRefExpr(QualifiedDeclRefExprClass, d, t, l, TD, VD), 878 QualifierRange(R), NNS(NNS) { } 879 880 /// \brief Retrieve the source range of the nested-name-specifier. 881 SourceRange getQualifierRange() const { return QualifierRange; } 882 883 /// \brief Retrieve the nested-name-specifier that qualifies this 884 /// declaration. 885 NestedNameSpecifier *getQualifier() const { return NNS; } 886 887 virtual SourceRange getSourceRange() const { 888 return SourceRange(QualifierRange.getBegin(), getLocation()); 889 } 890 891 static bool classof(const Stmt *T) { 892 return T->getStmtClass() == QualifiedDeclRefExprClass; 893 } 894 static bool classof(const QualifiedDeclRefExpr *) { return true; } 895}; 896 897/// \brief A qualified reference to a name whose declaration cannot 898/// yet be resolved. 899/// 900/// UnresolvedDeclRefExpr is similar to QualifiedDeclRefExpr in that 901/// it expresses a qualified reference to a declaration such as 902/// X<T>::value. The difference, however, is that an 903/// UnresolvedDeclRefExpr node is used only within C++ templates when 904/// the qualification (e.g., X<T>::) refers to a dependent type. In 905/// this case, X<T>::value cannot resolve to a declaration because the 906/// declaration will differ from on instantiation of X<T> to the 907/// next. Therefore, UnresolvedDeclRefExpr keeps track of the 908/// qualifier (X<T>::) and the name of the entity being referenced 909/// ("value"). Such expressions will instantiate to 910/// QualifiedDeclRefExprs. 911class UnresolvedDeclRefExpr : public Expr { 912 /// The name of the entity we will be referencing. 913 DeclarationName Name; 914 915 /// Location of the name of the declaration we're referencing. 916 SourceLocation Loc; 917 918 /// QualifierRange - The source range that covers the 919 /// nested-name-specifier. 920 SourceRange QualifierRange; 921 922 /// \brief The nested-name-specifier that qualifies this unresolved 923 /// declaration name. 924 NestedNameSpecifier *NNS; 925 926public: 927 UnresolvedDeclRefExpr(DeclarationName N, QualType T, SourceLocation L, 928 SourceRange R, NestedNameSpecifier *NNS) 929 : Expr(UnresolvedDeclRefExprClass, T, true, true), 930 Name(N), Loc(L), QualifierRange(R), NNS(NNS) { } 931 932 /// \brief Retrieve the name that this expression refers to. 933 DeclarationName getDeclName() const { return Name; } 934 935 /// \brief Retrieve the location of the name within the expression. 936 SourceLocation getLocation() const { return Loc; } 937 938 /// \brief Retrieve the source range of the nested-name-specifier. 939 SourceRange getQualifierRange() const { return QualifierRange; } 940 941 /// \brief Retrieve the nested-name-specifier that qualifies this 942 /// declaration. 943 NestedNameSpecifier *getQualifier() const { return NNS; } 944 945 virtual SourceRange getSourceRange() const { 946 return SourceRange(QualifierRange.getBegin(), getLocation()); 947 } 948 949 static bool classof(const Stmt *T) { 950 return T->getStmtClass() == UnresolvedDeclRefExprClass; 951 } 952 static bool classof(const UnresolvedDeclRefExpr *) { return true; } 953 954 virtual StmtIterator child_begin(); 955 virtual StmtIterator child_end(); 956}; 957 958class CXXExprWithTemporaries : public Expr { 959 Stmt *SubExpr; 960 961 CXXTempVarDecl **Decls; 962 unsigned NumDecls; 963 964public: 965 CXXExprWithTemporaries(Expr *subexpr, CXXTempVarDecl **decls, 966 unsigned numdecls); 967 ~CXXExprWithTemporaries(); 968 969 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 970 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 971 972 virtual SourceRange getSourceRange() const { return SourceRange(); } 973 974 // Implement isa/cast/dyncast/etc. 975 static bool classof(const Stmt *T) { 976 return T->getStmtClass() == CXXExprWithTemporariesClass; 977 } 978 static bool classof(const CXXExprWithTemporaries *) { return true; } 979 980 // Iterators 981 virtual child_iterator child_begin(); 982 virtual child_iterator child_end(); 983}; 984 985} // end namespace clang 986 987#endif 988