ExprCXX.h revision 63c00d7f35fa060c0a446c9df3a4402d9c7757fe
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/UnresolvedSet.h" 20#include "clang/AST/TemplateBase.h" 21 22namespace clang { 23 24class CXXConstructorDecl; 25class CXXDestructorDecl; 26class CXXMethodDecl; 27class CXXTemporary; 28class TemplateArgumentListInfo; 29 30//===--------------------------------------------------------------------===// 31// C++ Expressions. 32//===--------------------------------------------------------------------===// 33 34/// \brief A call to an overloaded operator written using operator 35/// syntax. 36/// 37/// Represents a call to an overloaded operator written using operator 38/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 39/// normal call, this AST node provides better information about the 40/// syntactic representation of the call. 41/// 42/// In a C++ template, this expression node kind will be used whenever 43/// any of the arguments are type-dependent. In this case, the 44/// function itself will be a (possibly empty) set of functions and 45/// function templates that were found by name lookup at template 46/// definition time. 47class CXXOperatorCallExpr : public CallExpr { 48 /// \brief The overloaded operator. 49 OverloadedOperatorKind Operator; 50 51public: 52 CXXOperatorCallExpr(ASTContext& C, OverloadedOperatorKind Op, Expr *fn, 53 Expr **args, unsigned numargs, QualType t, 54 ExprValueKind VK, SourceLocation operatorloc) 55 : CallExpr(C, CXXOperatorCallExprClass, fn, 0, args, numargs, t, VK, 56 operatorloc), 57 Operator(Op) {} 58 explicit CXXOperatorCallExpr(ASTContext& C, EmptyShell Empty) : 59 CallExpr(C, CXXOperatorCallExprClass, Empty) { } 60 61 62 /// getOperator - Returns the kind of overloaded operator that this 63 /// expression refers to. 64 OverloadedOperatorKind getOperator() const { return Operator; } 65 void setOperator(OverloadedOperatorKind Kind) { Operator = Kind; } 66 67 /// getOperatorLoc - Returns the location of the operator symbol in 68 /// the expression. When @c getOperator()==OO_Call, this is the 69 /// location of the right parentheses; when @c 70 /// getOperator()==OO_Subscript, this is the location of the right 71 /// bracket. 72 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 73 74 SourceRange getSourceRange() const; 75 76 static bool classof(const Stmt *T) { 77 return T->getStmtClass() == CXXOperatorCallExprClass; 78 } 79 static bool classof(const CXXOperatorCallExpr *) { return true; } 80}; 81 82/// CXXMemberCallExpr - Represents a call to a member function that 83/// may be written either with member call syntax (e.g., "obj.func()" 84/// or "objptr->func()") or with normal function-call syntax 85/// ("func()") within a member function that ends up calling a member 86/// function. The callee in either case is a MemberExpr that contains 87/// both the object argument and the member function, while the 88/// arguments are the arguments within the parentheses (not including 89/// the object argument). 90class CXXMemberCallExpr : public CallExpr { 91public: 92 CXXMemberCallExpr(ASTContext &C, Expr *fn, Expr **args, unsigned numargs, 93 QualType t, ExprValueKind VK, SourceLocation RP) 94 : CallExpr(C, CXXMemberCallExprClass, fn, 0, args, numargs, t, VK, RP) {} 95 96 CXXMemberCallExpr(ASTContext &C, EmptyShell Empty) 97 : CallExpr(C, CXXMemberCallExprClass, Empty) { } 98 99 /// getImplicitObjectArgument - Retrieves the implicit object 100 /// argument for the member call. For example, in "x.f(5)", this 101 /// operation would return "x". 102 Expr *getImplicitObjectArgument(); 103 104 /// getRecordDecl - Retrieves the CXXRecordDecl for the underlying type of 105 /// the implicit object argument. Note that this is may not be the same 106 /// declaration as that of the class context of the CXXMethodDecl which this 107 /// function is calling. 108 /// FIXME: Returns 0 for member pointer call exprs. 109 CXXRecordDecl *getRecordDecl(); 110 111 SourceRange getSourceRange() const; 112 113 static bool classof(const Stmt *T) { 114 return T->getStmtClass() == CXXMemberCallExprClass; 115 } 116 static bool classof(const CXXMemberCallExpr *) { return true; } 117}; 118 119/// CXXNamedCastExpr - Abstract class common to all of the C++ "named" 120/// casts, @c static_cast, @c dynamic_cast, @c reinterpret_cast, or @c 121/// const_cast. 122/// 123/// This abstract class is inherited by all of the classes 124/// representing "named" casts, e.g., CXXStaticCastExpr, 125/// CXXDynamicCastExpr, CXXReinterpretCastExpr, and CXXConstCastExpr. 126class CXXNamedCastExpr : public ExplicitCastExpr { 127private: 128 SourceLocation Loc; // the location of the casting op 129 SourceLocation RParenLoc; // the location of the right parenthesis 130 131protected: 132 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 133 CastKind kind, Expr *op, unsigned PathSize, 134 TypeSourceInfo *writtenTy, SourceLocation l, 135 SourceLocation RParenLoc) 136 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l), 137 RParenLoc(RParenLoc) {} 138 139 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 140 : ExplicitCastExpr(SC, Shell, PathSize) { } 141 142 friend class ASTStmtReader; 143 144public: 145 const char *getCastName() const; 146 147 /// \brief Retrieve the location of the cast operator keyword, e.g., 148 /// "static_cast". 149 SourceLocation getOperatorLoc() const { return Loc; } 150 151 /// \brief Retrieve the location of the closing parenthesis. 152 SourceLocation getRParenLoc() const { return RParenLoc; } 153 154 SourceRange getSourceRange() const { 155 return SourceRange(Loc, RParenLoc); 156 } 157 static bool classof(const Stmt *T) { 158 switch (T->getStmtClass()) { 159 case CXXStaticCastExprClass: 160 case CXXDynamicCastExprClass: 161 case CXXReinterpretCastExprClass: 162 case CXXConstCastExprClass: 163 return true; 164 default: 165 return false; 166 } 167 } 168 static bool classof(const CXXNamedCastExpr *) { return true; } 169}; 170 171/// CXXStaticCastExpr - A C++ @c static_cast expression (C++ [expr.static.cast]). 172/// 173/// This expression node represents a C++ static cast, e.g., 174/// @c static_cast<int>(1.0). 175class CXXStaticCastExpr : public CXXNamedCastExpr { 176 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op, 177 unsigned pathSize, TypeSourceInfo *writtenTy, 178 SourceLocation l, SourceLocation RParenLoc) 179 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize, 180 writtenTy, l, RParenLoc) {} 181 182 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize) 183 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) { } 184 185public: 186 static CXXStaticCastExpr *Create(ASTContext &Context, QualType T, 187 ExprValueKind VK, CastKind K, Expr *Op, 188 const CXXCastPath *Path, 189 TypeSourceInfo *Written, SourceLocation L, 190 SourceLocation RParenLoc); 191 static CXXStaticCastExpr *CreateEmpty(ASTContext &Context, 192 unsigned PathSize); 193 194 static bool classof(const Stmt *T) { 195 return T->getStmtClass() == CXXStaticCastExprClass; 196 } 197 static bool classof(const CXXStaticCastExpr *) { return true; } 198}; 199 200/// CXXDynamicCastExpr - A C++ @c dynamic_cast expression 201/// (C++ [expr.dynamic.cast]), which may perform a run-time check to 202/// determine how to perform the type cast. 203/// 204/// This expression node represents a dynamic cast, e.g., 205/// @c dynamic_cast<Derived*>(BasePtr). 206class CXXDynamicCastExpr : public CXXNamedCastExpr { 207 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind, 208 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy, 209 SourceLocation l, SourceLocation RParenLoc) 210 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize, 211 writtenTy, l, RParenLoc) {} 212 213 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize) 214 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) { } 215 216public: 217 static CXXDynamicCastExpr *Create(ASTContext &Context, QualType T, 218 ExprValueKind VK, CastKind Kind, Expr *Op, 219 const CXXCastPath *Path, 220 TypeSourceInfo *Written, SourceLocation L, 221 SourceLocation RParenLoc); 222 223 static CXXDynamicCastExpr *CreateEmpty(ASTContext &Context, 224 unsigned pathSize); 225 226 static bool classof(const Stmt *T) { 227 return T->getStmtClass() == CXXDynamicCastExprClass; 228 } 229 static bool classof(const CXXDynamicCastExpr *) { return true; } 230}; 231 232/// CXXReinterpretCastExpr - A C++ @c reinterpret_cast expression (C++ 233/// [expr.reinterpret.cast]), which provides a differently-typed view 234/// of a value but performs no actual work at run time. 235/// 236/// This expression node represents a reinterpret cast, e.g., 237/// @c reinterpret_cast<int>(VoidPtr). 238class CXXReinterpretCastExpr : public CXXNamedCastExpr { 239 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind, 240 Expr *op, unsigned pathSize, 241 TypeSourceInfo *writtenTy, SourceLocation l, 242 SourceLocation RParenLoc) 243 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op, 244 pathSize, writtenTy, l, RParenLoc) {} 245 246 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize) 247 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) { } 248 249public: 250 static CXXReinterpretCastExpr *Create(ASTContext &Context, QualType T, 251 ExprValueKind VK, CastKind Kind, 252 Expr *Op, const CXXCastPath *Path, 253 TypeSourceInfo *WrittenTy, SourceLocation L, 254 SourceLocation RParenLoc); 255 static CXXReinterpretCastExpr *CreateEmpty(ASTContext &Context, 256 unsigned pathSize); 257 258 static bool classof(const Stmt *T) { 259 return T->getStmtClass() == CXXReinterpretCastExprClass; 260 } 261 static bool classof(const CXXReinterpretCastExpr *) { return true; } 262}; 263 264/// CXXConstCastExpr - A C++ @c const_cast expression (C++ [expr.const.cast]), 265/// which can remove type qualifiers but does not change the underlying value. 266/// 267/// This expression node represents a const cast, e.g., 268/// @c const_cast<char*>(PtrToConstChar). 269class CXXConstCastExpr : public CXXNamedCastExpr { 270 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op, 271 TypeSourceInfo *writtenTy, SourceLocation l, 272 SourceLocation RParenLoc) 273 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op, 274 0, writtenTy, l, RParenLoc) {} 275 276 explicit CXXConstCastExpr(EmptyShell Empty) 277 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) { } 278 279public: 280 static CXXConstCastExpr *Create(ASTContext &Context, QualType T, 281 ExprValueKind VK, Expr *Op, 282 TypeSourceInfo *WrittenTy, SourceLocation L, 283 SourceLocation RParenLoc); 284 static CXXConstCastExpr *CreateEmpty(ASTContext &Context); 285 286 static bool classof(const Stmt *T) { 287 return T->getStmtClass() == CXXConstCastExprClass; 288 } 289 static bool classof(const CXXConstCastExpr *) { return true; } 290}; 291 292/// CXXBoolLiteralExpr - [C++ 2.13.5] C++ Boolean Literal. 293/// 294class CXXBoolLiteralExpr : public Expr { 295 bool Value; 296 SourceLocation Loc; 297public: 298 CXXBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) : 299 Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 300 false), 301 Value(val), Loc(l) {} 302 303 explicit CXXBoolLiteralExpr(EmptyShell Empty) 304 : Expr(CXXBoolLiteralExprClass, Empty) { } 305 306 bool getValue() const { return Value; } 307 void setValue(bool V) { Value = V; } 308 309 SourceRange getSourceRange() const { return SourceRange(Loc); } 310 311 SourceLocation getLocation() const { return Loc; } 312 void setLocation(SourceLocation L) { Loc = L; } 313 314 static bool classof(const Stmt *T) { 315 return T->getStmtClass() == CXXBoolLiteralExprClass; 316 } 317 static bool classof(const CXXBoolLiteralExpr *) { return true; } 318 319 // Iterators 320 child_range children() { return child_range(); } 321}; 322 323/// CXXNullPtrLiteralExpr - [C++0x 2.14.7] C++ Pointer Literal 324class CXXNullPtrLiteralExpr : public Expr { 325 SourceLocation Loc; 326public: 327 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation l) : 328 Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 329 false), 330 Loc(l) {} 331 332 explicit CXXNullPtrLiteralExpr(EmptyShell Empty) 333 : Expr(CXXNullPtrLiteralExprClass, Empty) { } 334 335 SourceRange getSourceRange() const { return SourceRange(Loc); } 336 337 SourceLocation getLocation() const { return Loc; } 338 void setLocation(SourceLocation L) { Loc = L; } 339 340 static bool classof(const Stmt *T) { 341 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 342 } 343 static bool classof(const CXXNullPtrLiteralExpr *) { return true; } 344 345 child_range children() { return child_range(); } 346}; 347 348/// CXXTypeidExpr - A C++ @c typeid expression (C++ [expr.typeid]), which gets 349/// the type_info that corresponds to the supplied type, or the (possibly 350/// dynamic) type of the supplied expression. 351/// 352/// This represents code like @c typeid(int) or @c typeid(*objPtr) 353class CXXTypeidExpr : public Expr { 354private: 355 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 356 SourceRange Range; 357 358public: 359 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 360 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 361 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 362 false, 363 // typeid is value-dependent if the type or expression are dependent 364 Operand->getType()->isDependentType(), 365 Operand->getType()->containsUnexpandedParameterPack()), 366 Operand(Operand), Range(R) { } 367 368 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R) 369 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 370 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 371 false, 372 // typeid is value-dependent if the type or expression are dependent 373 Operand->isTypeDependent() || Operand->isValueDependent(), 374 Operand->containsUnexpandedParameterPack()), 375 Operand(Operand), Range(R) { } 376 377 CXXTypeidExpr(EmptyShell Empty, bool isExpr) 378 : Expr(CXXTypeidExprClass, Empty) { 379 if (isExpr) 380 Operand = (Expr*)0; 381 else 382 Operand = (TypeSourceInfo*)0; 383 } 384 385 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 386 387 /// \brief Retrieves the type operand of this typeid() expression after 388 /// various required adjustments (removing reference types, cv-qualifiers). 389 QualType getTypeOperand() const; 390 391 /// \brief Retrieve source information for the type operand. 392 TypeSourceInfo *getTypeOperandSourceInfo() const { 393 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 394 return Operand.get<TypeSourceInfo *>(); 395 } 396 397 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 398 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 399 Operand = TSI; 400 } 401 402 Expr *getExprOperand() const { 403 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 404 return static_cast<Expr*>(Operand.get<Stmt *>()); 405 } 406 407 void setExprOperand(Expr *E) { 408 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 409 Operand = E; 410 } 411 412 SourceRange getSourceRange() const { return Range; } 413 void setSourceRange(SourceRange R) { Range = R; } 414 415 static bool classof(const Stmt *T) { 416 return T->getStmtClass() == CXXTypeidExprClass; 417 } 418 static bool classof(const CXXTypeidExpr *) { return true; } 419 420 // Iterators 421 child_range children() { 422 if (isTypeOperand()) return child_range(); 423 Stmt **begin = reinterpret_cast<Stmt**>(&Operand); 424 return child_range(begin, begin + 1); 425 } 426}; 427 428/// CXXUuidofExpr - A microsoft C++ @c __uuidof expression, which gets 429/// the _GUID that corresponds to the supplied type or expression. 430/// 431/// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr) 432class CXXUuidofExpr : public Expr { 433private: 434 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 435 SourceRange Range; 436 437public: 438 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 439 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, 440 false, Operand->getType()->isDependentType(), 441 Operand->getType()->containsUnexpandedParameterPack()), 442 Operand(Operand), Range(R) { } 443 444 CXXUuidofExpr(QualType Ty, Expr *Operand, SourceRange R) 445 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, 446 false, Operand->isTypeDependent(), 447 Operand->containsUnexpandedParameterPack()), 448 Operand(Operand), Range(R) { } 449 450 CXXUuidofExpr(EmptyShell Empty, bool isExpr) 451 : Expr(CXXUuidofExprClass, Empty) { 452 if (isExpr) 453 Operand = (Expr*)0; 454 else 455 Operand = (TypeSourceInfo*)0; 456 } 457 458 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 459 460 /// \brief Retrieves the type operand of this __uuidof() expression after 461 /// various required adjustments (removing reference types, cv-qualifiers). 462 QualType getTypeOperand() const; 463 464 /// \brief Retrieve source information for the type operand. 465 TypeSourceInfo *getTypeOperandSourceInfo() const { 466 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 467 return Operand.get<TypeSourceInfo *>(); 468 } 469 470 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 471 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 472 Operand = TSI; 473 } 474 475 Expr *getExprOperand() const { 476 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 477 return static_cast<Expr*>(Operand.get<Stmt *>()); 478 } 479 480 void setExprOperand(Expr *E) { 481 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 482 Operand = E; 483 } 484 485 SourceRange getSourceRange() const { return Range; } 486 void setSourceRange(SourceRange R) { Range = R; } 487 488 static bool classof(const Stmt *T) { 489 return T->getStmtClass() == CXXUuidofExprClass; 490 } 491 static bool classof(const CXXUuidofExpr *) { return true; } 492 493 // Iterators 494 child_range children() { 495 if (isTypeOperand()) return child_range(); 496 Stmt **begin = reinterpret_cast<Stmt**>(&Operand); 497 return child_range(begin, begin + 1); 498 } 499}; 500 501/// CXXThisExpr - Represents the "this" expression in C++, which is a 502/// pointer to the object on which the current member function is 503/// executing (C++ [expr.prim]p3). Example: 504/// 505/// @code 506/// class Foo { 507/// public: 508/// void bar(); 509/// void test() { this->bar(); } 510/// }; 511/// @endcode 512class CXXThisExpr : public Expr { 513 SourceLocation Loc; 514 bool Implicit : 1; 515 516public: 517 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit) 518 : Expr(CXXThisExprClass, Type, VK_RValue, OK_Ordinary, 519 // 'this' is type-dependent if the class type of the enclosing 520 // member function is dependent (C++ [temp.dep.expr]p2) 521 Type->isDependentType(), Type->isDependentType(), 522 /*ContainsUnexpandedParameterPack=*/false), 523 Loc(L), Implicit(isImplicit) { } 524 525 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {} 526 527 SourceLocation getLocation() const { return Loc; } 528 void setLocation(SourceLocation L) { Loc = L; } 529 530 SourceRange getSourceRange() const { return SourceRange(Loc); } 531 532 bool isImplicit() const { return Implicit; } 533 void setImplicit(bool I) { Implicit = I; } 534 535 static bool classof(const Stmt *T) { 536 return T->getStmtClass() == CXXThisExprClass; 537 } 538 static bool classof(const CXXThisExpr *) { return true; } 539 540 // Iterators 541 child_range children() { return child_range(); } 542}; 543 544/// CXXThrowExpr - [C++ 15] C++ Throw Expression. This handles 545/// 'throw' and 'throw' assignment-expression. When 546/// assignment-expression isn't present, Op will be null. 547/// 548class CXXThrowExpr : public Expr { 549 Stmt *Op; 550 SourceLocation ThrowLoc; 551public: 552 // Ty is the void type which is used as the result type of the 553 // exepression. The l is the location of the throw keyword. expr 554 // can by null, if the optional expression to throw isn't present. 555 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l) : 556 Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 557 expr && expr->containsUnexpandedParameterPack()), 558 Op(expr), ThrowLoc(l) {} 559 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {} 560 561 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 562 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 563 void setSubExpr(Expr *E) { Op = E; } 564 565 SourceLocation getThrowLoc() const { return ThrowLoc; } 566 void setThrowLoc(SourceLocation L) { ThrowLoc = L; } 567 568 SourceRange getSourceRange() const { 569 if (getSubExpr() == 0) 570 return SourceRange(ThrowLoc, ThrowLoc); 571 return SourceRange(ThrowLoc, getSubExpr()->getSourceRange().getEnd()); 572 } 573 574 static bool classof(const Stmt *T) { 575 return T->getStmtClass() == CXXThrowExprClass; 576 } 577 static bool classof(const CXXThrowExpr *) { return true; } 578 579 // Iterators 580 child_range children() { 581 return child_range(&Op, Op ? &Op+1 : &Op); 582 } 583}; 584 585/// CXXDefaultArgExpr - C++ [dcl.fct.default]. This wraps up a 586/// function call argument that was created from the corresponding 587/// parameter's default argument, when the call did not explicitly 588/// supply arguments for all of the parameters. 589class CXXDefaultArgExpr : public Expr { 590 /// \brief The parameter whose default is being used. 591 /// 592 /// When the bit is set, the subexpression is stored after the 593 /// CXXDefaultArgExpr itself. When the bit is clear, the parameter's 594 /// actual default expression is the subexpression. 595 llvm::PointerIntPair<ParmVarDecl *, 1, bool> Param; 596 597 /// \brief The location where the default argument expression was used. 598 SourceLocation Loc; 599 600 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param) 601 : Expr(SC, 602 param->hasUnparsedDefaultArg() 603 ? param->getType().getNonReferenceType() 604 : param->getDefaultArg()->getType(), 605 param->getDefaultArg()->getValueKind(), 606 param->getDefaultArg()->getObjectKind(), false, false, false), 607 Param(param, false), Loc(Loc) { } 608 609 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param, 610 Expr *SubExpr) 611 : Expr(SC, SubExpr->getType(), 612 SubExpr->getValueKind(), SubExpr->getObjectKind(), 613 false, false, false), 614 Param(param, true), Loc(Loc) { 615 *reinterpret_cast<Expr **>(this + 1) = SubExpr; 616 } 617 618public: 619 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {} 620 621 622 // Param is the parameter whose default argument is used by this 623 // expression. 624 static CXXDefaultArgExpr *Create(ASTContext &C, SourceLocation Loc, 625 ParmVarDecl *Param) { 626 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param); 627 } 628 629 // Param is the parameter whose default argument is used by this 630 // expression, and SubExpr is the expression that will actually be used. 631 static CXXDefaultArgExpr *Create(ASTContext &C, 632 SourceLocation Loc, 633 ParmVarDecl *Param, 634 Expr *SubExpr); 635 636 // Retrieve the parameter that the argument was created from. 637 const ParmVarDecl *getParam() const { return Param.getPointer(); } 638 ParmVarDecl *getParam() { return Param.getPointer(); } 639 640 // Retrieve the actual argument to the function call. 641 const Expr *getExpr() const { 642 if (Param.getInt()) 643 return *reinterpret_cast<Expr const * const*> (this + 1); 644 return getParam()->getDefaultArg(); 645 } 646 Expr *getExpr() { 647 if (Param.getInt()) 648 return *reinterpret_cast<Expr **> (this + 1); 649 return getParam()->getDefaultArg(); 650 } 651 652 /// \brief Retrieve the location where this default argument was actually 653 /// used. 654 SourceLocation getUsedLocation() const { return Loc; } 655 656 SourceRange getSourceRange() const { 657 // Default argument expressions have no representation in the 658 // source, so they have an empty source range. 659 return SourceRange(); 660 } 661 662 static bool classof(const Stmt *T) { 663 return T->getStmtClass() == CXXDefaultArgExprClass; 664 } 665 static bool classof(const CXXDefaultArgExpr *) { return true; } 666 667 // Iterators 668 child_range children() { return child_range(); } 669 670 friend class ASTStmtReader; 671 friend class ASTStmtWriter; 672}; 673 674/// CXXTemporary - Represents a C++ temporary. 675class CXXTemporary { 676 /// Destructor - The destructor that needs to be called. 677 const CXXDestructorDecl *Destructor; 678 679 CXXTemporary(const CXXDestructorDecl *destructor) 680 : Destructor(destructor) { } 681 682public: 683 static CXXTemporary *Create(ASTContext &C, 684 const CXXDestructorDecl *Destructor); 685 686 const CXXDestructorDecl *getDestructor() const { return Destructor; } 687}; 688 689/// \brief Represents binding an expression to a temporary. 690/// 691/// This ensures the destructor is called for the temporary. It should only be 692/// needed for non-POD, non-trivially destructable class types. For example: 693/// 694/// \code 695/// struct S { 696/// S() { } // User defined constructor makes S non-POD. 697/// ~S() { } // User defined destructor makes it non-trivial. 698/// }; 699/// void test() { 700/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr. 701/// } 702/// \endcode 703class CXXBindTemporaryExpr : public Expr { 704 CXXTemporary *Temp; 705 706 Stmt *SubExpr; 707 708 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr) 709 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), 710 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(), 711 SubExpr->isValueDependent(), 712 SubExpr->containsUnexpandedParameterPack()), 713 Temp(temp), SubExpr(SubExpr) { } 714 715public: 716 CXXBindTemporaryExpr(EmptyShell Empty) 717 : Expr(CXXBindTemporaryExprClass, Empty), Temp(0), SubExpr(0) {} 718 719 static CXXBindTemporaryExpr *Create(ASTContext &C, CXXTemporary *Temp, 720 Expr* SubExpr); 721 722 CXXTemporary *getTemporary() { return Temp; } 723 const CXXTemporary *getTemporary() const { return Temp; } 724 void setTemporary(CXXTemporary *T) { Temp = T; } 725 726 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 727 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 728 void setSubExpr(Expr *E) { SubExpr = E; } 729 730 SourceRange getSourceRange() const { 731 return SubExpr->getSourceRange(); 732 } 733 734 // Implement isa/cast/dyncast/etc. 735 static bool classof(const Stmt *T) { 736 return T->getStmtClass() == CXXBindTemporaryExprClass; 737 } 738 static bool classof(const CXXBindTemporaryExpr *) { return true; } 739 740 // Iterators 741 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 742}; 743 744/// CXXConstructExpr - Represents a call to a C++ constructor. 745class CXXConstructExpr : public Expr { 746public: 747 enum ConstructionKind { 748 CK_Complete, 749 CK_NonVirtualBase, 750 CK_VirtualBase 751 }; 752 753private: 754 CXXConstructorDecl *Constructor; 755 756 SourceLocation Loc; 757 SourceRange ParenRange; 758 bool Elidable : 1; 759 bool ZeroInitialization : 1; 760 unsigned ConstructKind : 2; 761 Stmt **Args; 762 unsigned NumArgs; 763 764protected: 765 CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T, 766 SourceLocation Loc, 767 CXXConstructorDecl *d, bool elidable, 768 Expr **args, unsigned numargs, 769 bool ZeroInitialization = false, 770 ConstructionKind ConstructKind = CK_Complete, 771 SourceRange ParenRange = SourceRange()); 772 773 /// \brief Construct an empty C++ construction expression. 774 CXXConstructExpr(StmtClass SC, EmptyShell Empty) 775 : Expr(SC, Empty), Constructor(0), Elidable(0), ZeroInitialization(0), 776 ConstructKind(0), Args(0), NumArgs(0) { } 777 778public: 779 /// \brief Construct an empty C++ construction expression. 780 explicit CXXConstructExpr(EmptyShell Empty) 781 : Expr(CXXConstructExprClass, Empty), Constructor(0), 782 Elidable(0), ZeroInitialization(0), 783 ConstructKind(0), Args(0), NumArgs(0) { } 784 785 static CXXConstructExpr *Create(ASTContext &C, QualType T, 786 SourceLocation Loc, 787 CXXConstructorDecl *D, bool Elidable, 788 Expr **Args, unsigned NumArgs, 789 bool ZeroInitialization = false, 790 ConstructionKind ConstructKind = CK_Complete, 791 SourceRange ParenRange = SourceRange()); 792 793 794 CXXConstructorDecl* getConstructor() const { return Constructor; } 795 void setConstructor(CXXConstructorDecl *C) { Constructor = C; } 796 797 SourceLocation getLocation() const { return Loc; } 798 void setLocation(SourceLocation Loc) { this->Loc = Loc; } 799 800 /// \brief Whether this construction is elidable. 801 bool isElidable() const { return Elidable; } 802 void setElidable(bool E) { Elidable = E; } 803 804 /// \brief Whether this construction first requires 805 /// zero-initialization before the initializer is called. 806 bool requiresZeroInitialization() const { return ZeroInitialization; } 807 void setRequiresZeroInitialization(bool ZeroInit) { 808 ZeroInitialization = ZeroInit; 809 } 810 811 /// \brief Determines whether this constructor is actually constructing 812 /// a base class (rather than a complete object). 813 ConstructionKind getConstructionKind() const { 814 return (ConstructionKind)ConstructKind; 815 } 816 void setConstructionKind(ConstructionKind CK) { 817 ConstructKind = CK; 818 } 819 820 typedef ExprIterator arg_iterator; 821 typedef ConstExprIterator const_arg_iterator; 822 823 arg_iterator arg_begin() { return Args; } 824 arg_iterator arg_end() { return Args + NumArgs; } 825 const_arg_iterator arg_begin() const { return Args; } 826 const_arg_iterator arg_end() const { return Args + NumArgs; } 827 828 Expr **getArgs() const { return reinterpret_cast<Expr **>(Args); } 829 unsigned getNumArgs() const { return NumArgs; } 830 831 /// getArg - Return the specified argument. 832 Expr *getArg(unsigned Arg) { 833 assert(Arg < NumArgs && "Arg access out of range!"); 834 return cast<Expr>(Args[Arg]); 835 } 836 const Expr *getArg(unsigned Arg) const { 837 assert(Arg < NumArgs && "Arg access out of range!"); 838 return cast<Expr>(Args[Arg]); 839 } 840 841 /// setArg - Set the specified argument. 842 void setArg(unsigned Arg, Expr *ArgExpr) { 843 assert(Arg < NumArgs && "Arg access out of range!"); 844 Args[Arg] = ArgExpr; 845 } 846 847 SourceRange getSourceRange() const; 848 SourceRange getParenRange() const { return ParenRange; } 849 850 static bool classof(const Stmt *T) { 851 return T->getStmtClass() == CXXConstructExprClass || 852 T->getStmtClass() == CXXTemporaryObjectExprClass; 853 } 854 static bool classof(const CXXConstructExpr *) { return true; } 855 856 // Iterators 857 child_range children() { 858 return child_range(&Args[0], &Args[0]+NumArgs); 859 } 860 861 friend class ASTStmtReader; 862}; 863 864/// CXXFunctionalCastExpr - Represents an explicit C++ type conversion 865/// that uses "functional" notion (C++ [expr.type.conv]). Example: @c 866/// x = int(0.5); 867class CXXFunctionalCastExpr : public ExplicitCastExpr { 868 SourceLocation TyBeginLoc; 869 SourceLocation RParenLoc; 870 871 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK, 872 TypeSourceInfo *writtenTy, 873 SourceLocation tyBeginLoc, CastKind kind, 874 Expr *castExpr, unsigned pathSize, 875 SourceLocation rParenLoc) 876 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, 877 castExpr, pathSize, writtenTy), 878 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {} 879 880 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize) 881 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) { } 882 883public: 884 static CXXFunctionalCastExpr *Create(ASTContext &Context, QualType T, 885 ExprValueKind VK, 886 TypeSourceInfo *Written, 887 SourceLocation TyBeginLoc, 888 CastKind Kind, Expr *Op, 889 const CXXCastPath *Path, 890 SourceLocation RPLoc); 891 static CXXFunctionalCastExpr *CreateEmpty(ASTContext &Context, 892 unsigned PathSize); 893 894 SourceLocation getTypeBeginLoc() const { return TyBeginLoc; } 895 void setTypeBeginLoc(SourceLocation L) { TyBeginLoc = L; } 896 SourceLocation getRParenLoc() const { return RParenLoc; } 897 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 898 899 SourceRange getSourceRange() const { 900 return SourceRange(TyBeginLoc, RParenLoc); 901 } 902 static bool classof(const Stmt *T) { 903 return T->getStmtClass() == CXXFunctionalCastExprClass; 904 } 905 static bool classof(const CXXFunctionalCastExpr *) { return true; } 906}; 907 908/// @brief Represents a C++ functional cast expression that builds a 909/// temporary object. 910/// 911/// This expression type represents a C++ "functional" cast 912/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 913/// constructor to build a temporary object. With N == 1 arguments the 914/// functional cast expression will be represented by CXXFunctionalCastExpr. 915/// Example: 916/// @code 917/// struct X { X(int, float); } 918/// 919/// X create_X() { 920/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 921/// }; 922/// @endcode 923class CXXTemporaryObjectExpr : public CXXConstructExpr { 924 TypeSourceInfo *Type; 925 926public: 927 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 928 TypeSourceInfo *Type, 929 Expr **Args,unsigned NumArgs, 930 SourceRange parenRange, 931 bool ZeroInitialization = false); 932 explicit CXXTemporaryObjectExpr(EmptyShell Empty) 933 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty), Type() { } 934 935 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 936 937 SourceRange getSourceRange() const; 938 939 static bool classof(const Stmt *T) { 940 return T->getStmtClass() == CXXTemporaryObjectExprClass; 941 } 942 static bool classof(const CXXTemporaryObjectExpr *) { return true; } 943 944 friend class ASTStmtReader; 945}; 946 947/// CXXScalarValueInitExpr - [C++ 5.2.3p2] 948/// Expression "T()" which creates a value-initialized rvalue of type 949/// T, which is a non-class type. 950/// 951class CXXScalarValueInitExpr : public Expr { 952 SourceLocation RParenLoc; 953 TypeSourceInfo *TypeInfo; 954 955 friend class ASTStmtReader; 956 957public: 958 /// \brief Create an explicitly-written scalar-value initialization 959 /// expression. 960 CXXScalarValueInitExpr(QualType Type, 961 TypeSourceInfo *TypeInfo, 962 SourceLocation rParenLoc ) : 963 Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, 964 false, false, false), 965 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {} 966 967 explicit CXXScalarValueInitExpr(EmptyShell Shell) 968 : Expr(CXXScalarValueInitExprClass, Shell) { } 969 970 TypeSourceInfo *getTypeSourceInfo() const { 971 return TypeInfo; 972 } 973 974 SourceLocation getRParenLoc() const { return RParenLoc; } 975 976 SourceRange getSourceRange() const; 977 978 static bool classof(const Stmt *T) { 979 return T->getStmtClass() == CXXScalarValueInitExprClass; 980 } 981 static bool classof(const CXXScalarValueInitExpr *) { return true; } 982 983 // Iterators 984 child_range children() { return child_range(); } 985}; 986 987/// CXXNewExpr - A new expression for memory allocation and constructor calls, 988/// e.g: "new CXXNewExpr(foo)". 989class CXXNewExpr : public Expr { 990 // Was the usage ::new, i.e. is the global new to be used? 991 bool GlobalNew : 1; 992 // Is there an initializer? If not, built-ins are uninitialized, else they're 993 // value-initialized. 994 bool Initializer : 1; 995 // Do we allocate an array? If so, the first SubExpr is the size expression. 996 bool Array : 1; 997 // If this is an array allocation, does the usual deallocation 998 // function for the allocated type want to know the allocated size? 999 bool UsualArrayDeleteWantsSize : 1; 1000 // The number of placement new arguments. 1001 unsigned NumPlacementArgs : 14; 1002 // The number of constructor arguments. This may be 1 even for non-class 1003 // types; use the pseudo copy constructor. 1004 unsigned NumConstructorArgs : 14; 1005 // Contains an optional array size expression, any number of optional 1006 // placement arguments, and any number of optional constructor arguments, 1007 // in that order. 1008 Stmt **SubExprs; 1009 // Points to the allocation function used. 1010 FunctionDecl *OperatorNew; 1011 // Points to the deallocation function used in case of error. May be null. 1012 FunctionDecl *OperatorDelete; 1013 // Points to the constructor used. Cannot be null if AllocType is a record; 1014 // it would still point at the default constructor (even an implicit one). 1015 // Must be null for all other types. 1016 CXXConstructorDecl *Constructor; 1017 1018 /// \brief The allocated type-source information, as written in the source. 1019 TypeSourceInfo *AllocatedTypeInfo; 1020 1021 /// \brief If the allocated type was expressed as a parenthesized type-id, 1022 /// the source range covering the parenthesized type-id. 1023 SourceRange TypeIdParens; 1024 1025 SourceLocation StartLoc; 1026 SourceLocation EndLoc; 1027 SourceLocation ConstructorLParen; 1028 SourceLocation ConstructorRParen; 1029 1030 friend class ASTStmtReader; 1031public: 1032 CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 1033 Expr **placementArgs, unsigned numPlaceArgs, 1034 SourceRange TypeIdParens, 1035 Expr *arraySize, CXXConstructorDecl *constructor, bool initializer, 1036 Expr **constructorArgs, unsigned numConsArgs, 1037 FunctionDecl *operatorDelete, bool usualArrayDeleteWantsSize, 1038 QualType ty, TypeSourceInfo *AllocatedTypeInfo, 1039 SourceLocation startLoc, SourceLocation endLoc, 1040 SourceLocation constructorLParen, 1041 SourceLocation constructorRParen); 1042 explicit CXXNewExpr(EmptyShell Shell) 1043 : Expr(CXXNewExprClass, Shell), SubExprs(0) { } 1044 1045 void AllocateArgsArray(ASTContext &C, bool isArray, unsigned numPlaceArgs, 1046 unsigned numConsArgs); 1047 1048 QualType getAllocatedType() const { 1049 assert(getType()->isPointerType()); 1050 return getType()->getAs<PointerType>()->getPointeeType(); 1051 } 1052 1053 TypeSourceInfo *getAllocatedTypeSourceInfo() const { 1054 return AllocatedTypeInfo; 1055 } 1056 1057 FunctionDecl *getOperatorNew() const { return OperatorNew; } 1058 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; } 1059 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1060 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; } 1061 CXXConstructorDecl *getConstructor() const { return Constructor; } 1062 void setConstructor(CXXConstructorDecl *D) { Constructor = D; } 1063 1064 bool isArray() const { return Array; } 1065 Expr *getArraySize() { 1066 return Array ? cast<Expr>(SubExprs[0]) : 0; 1067 } 1068 const Expr *getArraySize() const { 1069 return Array ? cast<Expr>(SubExprs[0]) : 0; 1070 } 1071 1072 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 1073 Expr **getPlacementArgs() { 1074 return reinterpret_cast<Expr **>(SubExprs + Array); 1075 } 1076 1077 Expr *getPlacementArg(unsigned i) { 1078 assert(i < NumPlacementArgs && "Index out of range"); 1079 return cast<Expr>(SubExprs[Array + i]); 1080 } 1081 const Expr *getPlacementArg(unsigned i) const { 1082 assert(i < NumPlacementArgs && "Index out of range"); 1083 return cast<Expr>(SubExprs[Array + i]); 1084 } 1085 1086 bool isParenTypeId() const { return TypeIdParens.isValid(); } 1087 SourceRange getTypeIdParens() const { return TypeIdParens; } 1088 1089 bool isGlobalNew() const { return GlobalNew; } 1090 bool hasInitializer() const { return Initializer; } 1091 1092 /// Answers whether the usual array deallocation function for the 1093 /// allocated type expects the size of the allocation as a 1094 /// parameter. 1095 bool doesUsualArrayDeleteWantSize() const { 1096 return UsualArrayDeleteWantsSize; 1097 } 1098 1099 unsigned getNumConstructorArgs() const { return NumConstructorArgs; } 1100 1101 Expr **getConstructorArgs() { 1102 return reinterpret_cast<Expr **>(SubExprs + Array + NumPlacementArgs); 1103 } 1104 1105 Expr *getConstructorArg(unsigned i) { 1106 assert(i < NumConstructorArgs && "Index out of range"); 1107 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1108 } 1109 const Expr *getConstructorArg(unsigned i) const { 1110 assert(i < NumConstructorArgs && "Index out of range"); 1111 return cast<Expr>(SubExprs[Array + NumPlacementArgs + i]); 1112 } 1113 1114 typedef ExprIterator arg_iterator; 1115 typedef ConstExprIterator const_arg_iterator; 1116 1117 arg_iterator placement_arg_begin() { 1118 return SubExprs + Array; 1119 } 1120 arg_iterator placement_arg_end() { 1121 return SubExprs + Array + getNumPlacementArgs(); 1122 } 1123 const_arg_iterator placement_arg_begin() const { 1124 return SubExprs + Array; 1125 } 1126 const_arg_iterator placement_arg_end() const { 1127 return SubExprs + Array + getNumPlacementArgs(); 1128 } 1129 1130 arg_iterator constructor_arg_begin() { 1131 return SubExprs + Array + getNumPlacementArgs(); 1132 } 1133 arg_iterator constructor_arg_end() { 1134 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1135 } 1136 const_arg_iterator constructor_arg_begin() const { 1137 return SubExprs + Array + getNumPlacementArgs(); 1138 } 1139 const_arg_iterator constructor_arg_end() const { 1140 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1141 } 1142 1143 typedef Stmt **raw_arg_iterator; 1144 raw_arg_iterator raw_arg_begin() { return SubExprs; } 1145 raw_arg_iterator raw_arg_end() { 1146 return SubExprs + Array + getNumPlacementArgs() + getNumConstructorArgs(); 1147 } 1148 const_arg_iterator raw_arg_begin() const { return SubExprs; } 1149 const_arg_iterator raw_arg_end() const { return constructor_arg_end(); } 1150 1151 SourceLocation getStartLoc() const { return StartLoc; } 1152 SourceLocation getEndLoc() const { return EndLoc; } 1153 1154 SourceLocation getConstructorLParen() const { return ConstructorLParen; } 1155 SourceLocation getConstructorRParen() const { return ConstructorRParen; } 1156 1157 SourceRange getSourceRange() const { 1158 return SourceRange(StartLoc, EndLoc); 1159 } 1160 1161 static bool classof(const Stmt *T) { 1162 return T->getStmtClass() == CXXNewExprClass; 1163 } 1164 static bool classof(const CXXNewExpr *) { return true; } 1165 1166 // Iterators 1167 child_range children() { 1168 return child_range(&SubExprs[0], 1169 &SubExprs[0] + Array + getNumPlacementArgs() 1170 + getNumConstructorArgs()); 1171 } 1172}; 1173 1174/// CXXDeleteExpr - A delete expression for memory deallocation and destructor 1175/// calls, e.g. "delete[] pArray". 1176class CXXDeleteExpr : public Expr { 1177 // Is this a forced global delete, i.e. "::delete"? 1178 bool GlobalDelete : 1; 1179 // Is this the array form of delete, i.e. "delete[]"? 1180 bool ArrayForm : 1; 1181 // ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied 1182 // to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm 1183 // will be true). 1184 bool ArrayFormAsWritten : 1; 1185 // Does the usual deallocation function for the element type require 1186 // a size_t argument? 1187 bool UsualArrayDeleteWantsSize : 1; 1188 // Points to the operator delete overload that is used. Could be a member. 1189 FunctionDecl *OperatorDelete; 1190 // The pointer expression to be deleted. 1191 Stmt *Argument; 1192 // Location of the expression. 1193 SourceLocation Loc; 1194public: 1195 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 1196 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize, 1197 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 1198 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false, 1199 arg->containsUnexpandedParameterPack()), 1200 GlobalDelete(globalDelete), 1201 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten), 1202 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize), 1203 OperatorDelete(operatorDelete), Argument(arg), Loc(loc) { } 1204 explicit CXXDeleteExpr(EmptyShell Shell) 1205 : Expr(CXXDeleteExprClass, Shell), OperatorDelete(0), Argument(0) { } 1206 1207 bool isGlobalDelete() const { return GlobalDelete; } 1208 bool isArrayForm() const { return ArrayForm; } 1209 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; } 1210 1211 /// Answers whether the usual array deallocation function for the 1212 /// allocated type expects the size of the allocation as a 1213 /// parameter. This can be true even if the actual deallocation 1214 /// function that we're using doesn't want a size. 1215 bool doesUsualArrayDeleteWantSize() const { 1216 return UsualArrayDeleteWantsSize; 1217 } 1218 1219 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1220 1221 Expr *getArgument() { return cast<Expr>(Argument); } 1222 const Expr *getArgument() const { return cast<Expr>(Argument); } 1223 1224 /// \brief Retrieve the type being destroyed. If the type being 1225 /// destroyed is a dependent type which may or may not be a pointer, 1226 /// return an invalid type. 1227 QualType getDestroyedType() const; 1228 1229 SourceRange getSourceRange() const { 1230 return SourceRange(Loc, Argument->getLocEnd()); 1231 } 1232 1233 static bool classof(const Stmt *T) { 1234 return T->getStmtClass() == CXXDeleteExprClass; 1235 } 1236 static bool classof(const CXXDeleteExpr *) { return true; } 1237 1238 // Iterators 1239 child_range children() { return child_range(&Argument, &Argument+1); } 1240 1241 friend class ASTStmtReader; 1242}; 1243 1244/// \brief Structure used to store the type being destroyed by a 1245/// pseudo-destructor expression. 1246class PseudoDestructorTypeStorage { 1247 /// \brief Either the type source information or the name of the type, if 1248 /// it couldn't be resolved due to type-dependence. 1249 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 1250 1251 /// \brief The starting source location of the pseudo-destructor type. 1252 SourceLocation Location; 1253 1254public: 1255 PseudoDestructorTypeStorage() { } 1256 1257 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 1258 : Type(II), Location(Loc) { } 1259 1260 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 1261 1262 TypeSourceInfo *getTypeSourceInfo() const { 1263 return Type.dyn_cast<TypeSourceInfo *>(); 1264 } 1265 1266 IdentifierInfo *getIdentifier() const { 1267 return Type.dyn_cast<IdentifierInfo *>(); 1268 } 1269 1270 SourceLocation getLocation() const { return Location; } 1271}; 1272 1273/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 1274/// 1275/// A pseudo-destructor is an expression that looks like a member access to a 1276/// destructor of a scalar type, except that scalar types don't have 1277/// destructors. For example: 1278/// 1279/// \code 1280/// typedef int T; 1281/// void f(int *p) { 1282/// p->T::~T(); 1283/// } 1284/// \endcode 1285/// 1286/// Pseudo-destructors typically occur when instantiating templates such as: 1287/// 1288/// \code 1289/// template<typename T> 1290/// void destroy(T* ptr) { 1291/// ptr->T::~T(); 1292/// } 1293/// \endcode 1294/// 1295/// for scalar types. A pseudo-destructor expression has no run-time semantics 1296/// beyond evaluating the base expression. 1297class CXXPseudoDestructorExpr : public Expr { 1298 /// \brief The base expression (that is being destroyed). 1299 Stmt *Base; 1300 1301 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 1302 /// period ('.'). 1303 bool IsArrow : 1; 1304 1305 /// \brief The location of the '.' or '->' operator. 1306 SourceLocation OperatorLoc; 1307 1308 /// \brief The nested-name-specifier that follows the operator, if present. 1309 NestedNameSpecifier *Qualifier; 1310 1311 /// \brief The source range that covers the nested-name-specifier, if 1312 /// present. 1313 SourceRange QualifierRange; 1314 1315 /// \brief The type that precedes the '::' in a qualified pseudo-destructor 1316 /// expression. 1317 TypeSourceInfo *ScopeType; 1318 1319 /// \brief The location of the '::' in a qualified pseudo-destructor 1320 /// expression. 1321 SourceLocation ColonColonLoc; 1322 1323 /// \brief The location of the '~'. 1324 SourceLocation TildeLoc; 1325 1326 /// \brief The type being destroyed, or its name if we were unable to 1327 /// resolve the name. 1328 PseudoDestructorTypeStorage DestroyedType; 1329 1330public: 1331 CXXPseudoDestructorExpr(ASTContext &Context, 1332 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 1333 NestedNameSpecifier *Qualifier, 1334 SourceRange QualifierRange, 1335 TypeSourceInfo *ScopeType, 1336 SourceLocation ColonColonLoc, 1337 SourceLocation TildeLoc, 1338 PseudoDestructorTypeStorage DestroyedType); 1339 1340 explicit CXXPseudoDestructorExpr(EmptyShell Shell) 1341 : Expr(CXXPseudoDestructorExprClass, Shell), 1342 Base(0), IsArrow(false), Qualifier(0), ScopeType(0) { } 1343 1344 void setBase(Expr *E) { Base = E; } 1345 Expr *getBase() const { return cast<Expr>(Base); } 1346 1347 /// \brief Determines whether this member expression actually had 1348 /// a C++ nested-name-specifier prior to the name of the member, e.g., 1349 /// x->Base::foo. 1350 bool hasQualifier() const { return Qualifier != 0; } 1351 1352 /// \brief If the member name was qualified, retrieves the source range of 1353 /// the nested-name-specifier that precedes the member name. Otherwise, 1354 /// returns an empty source range. 1355 SourceRange getQualifierRange() const { return QualifierRange; } 1356 void setQualifierRange(SourceRange R) { QualifierRange = R; } 1357 1358 /// \brief If the member name was qualified, retrieves the 1359 /// nested-name-specifier that precedes the member name. Otherwise, returns 1360 /// NULL. 1361 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1362 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 1363 1364 /// \brief Determine whether this pseudo-destructor expression was written 1365 /// using an '->' (otherwise, it used a '.'). 1366 bool isArrow() const { return IsArrow; } 1367 void setArrow(bool A) { IsArrow = A; } 1368 1369 /// \brief Retrieve the location of the '.' or '->' operator. 1370 SourceLocation getOperatorLoc() const { return OperatorLoc; } 1371 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 1372 1373 /// \brief Retrieve the scope type in a qualified pseudo-destructor 1374 /// expression. 1375 /// 1376 /// Pseudo-destructor expressions can have extra qualification within them 1377 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 1378 /// Here, if the object type of the expression is (or may be) a scalar type, 1379 /// \p T may also be a scalar type and, therefore, cannot be part of a 1380 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 1381 /// destructor expression. 1382 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 1383 void setScopeTypeInfo(TypeSourceInfo *Info) { ScopeType = Info; } 1384 1385 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor 1386 /// expression. 1387 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 1388 void setColonColonLoc(SourceLocation L) { ColonColonLoc = L; } 1389 1390 /// \brief Retrieve the location of the '~'. 1391 SourceLocation getTildeLoc() const { return TildeLoc; } 1392 void setTildeLoc(SourceLocation L) { TildeLoc = L; } 1393 1394 /// \brief Retrieve the source location information for the type 1395 /// being destroyed. 1396 /// 1397 /// This type-source information is available for non-dependent 1398 /// pseudo-destructor expressions and some dependent pseudo-destructor 1399 /// expressions. Returns NULL if we only have the identifier for a 1400 /// dependent pseudo-destructor expression. 1401 TypeSourceInfo *getDestroyedTypeInfo() const { 1402 return DestroyedType.getTypeSourceInfo(); 1403 } 1404 1405 /// \brief In a dependent pseudo-destructor expression for which we do not 1406 /// have full type information on the destroyed type, provides the name 1407 /// of the destroyed type. 1408 IdentifierInfo *getDestroyedTypeIdentifier() const { 1409 return DestroyedType.getIdentifier(); 1410 } 1411 1412 /// \brief Retrieve the type being destroyed. 1413 QualType getDestroyedType() const; 1414 1415 /// \brief Retrieve the starting location of the type being destroyed. 1416 SourceLocation getDestroyedTypeLoc() const { 1417 return DestroyedType.getLocation(); 1418 } 1419 1420 /// \brief Set the name of destroyed type for a dependent pseudo-destructor 1421 /// expression. 1422 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) { 1423 DestroyedType = PseudoDestructorTypeStorage(II, Loc); 1424 } 1425 1426 /// \brief Set the destroyed type. 1427 void setDestroyedType(TypeSourceInfo *Info) { 1428 DestroyedType = PseudoDestructorTypeStorage(Info); 1429 } 1430 1431 SourceRange getSourceRange() const; 1432 1433 static bool classof(const Stmt *T) { 1434 return T->getStmtClass() == CXXPseudoDestructorExprClass; 1435 } 1436 static bool classof(const CXXPseudoDestructorExpr *) { return true; } 1437 1438 // Iterators 1439 child_range children() { return child_range(&Base, &Base + 1); } 1440}; 1441 1442/// UnaryTypeTraitExpr - A GCC or MS unary type trait, as used in the 1443/// implementation of TR1/C++0x type trait templates. 1444/// Example: 1445/// __is_pod(int) == true 1446/// __is_enum(std::string) == false 1447class UnaryTypeTraitExpr : public Expr { 1448 /// UTT - The trait. A UnaryTypeTrait enum in MSVC compat unsigned. 1449 unsigned UTT : 31; 1450 /// The value of the type trait. Unspecified if dependent. 1451 bool Value : 1; 1452 1453 /// Loc - The location of the type trait keyword. 1454 SourceLocation Loc; 1455 1456 /// RParen - The location of the closing paren. 1457 SourceLocation RParen; 1458 1459 /// The type being queried. 1460 TypeSourceInfo *QueriedType; 1461 1462public: 1463 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, 1464 TypeSourceInfo *queried, bool value, 1465 SourceLocation rparen, QualType ty) 1466 : Expr(UnaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 1467 false, queried->getType()->isDependentType(), 1468 queried->getType()->containsUnexpandedParameterPack()), 1469 UTT(utt), Value(value), Loc(loc), RParen(rparen), QueriedType(queried) { } 1470 1471 explicit UnaryTypeTraitExpr(EmptyShell Empty) 1472 : Expr(UnaryTypeTraitExprClass, Empty), UTT(0), Value(false), 1473 QueriedType() { } 1474 1475 SourceRange getSourceRange() const { return SourceRange(Loc, RParen);} 1476 1477 UnaryTypeTrait getTrait() const { return static_cast<UnaryTypeTrait>(UTT); } 1478 1479 QualType getQueriedType() const { return QueriedType->getType(); } 1480 1481 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 1482 1483 bool getValue() const { return Value; } 1484 1485 static bool classof(const Stmt *T) { 1486 return T->getStmtClass() == UnaryTypeTraitExprClass; 1487 } 1488 static bool classof(const UnaryTypeTraitExpr *) { return true; } 1489 1490 // Iterators 1491 child_range children() { return child_range(); } 1492 1493 friend class ASTStmtReader; 1494}; 1495 1496/// BinaryTypeTraitExpr - A GCC or MS binary type trait, as used in the 1497/// implementation of TR1/C++0x type trait templates. 1498/// Example: 1499/// __is_base_of(Base, Derived) == true 1500class BinaryTypeTraitExpr : public Expr { 1501 /// BTT - The trait. A BinaryTypeTrait enum in MSVC compat unsigned. 1502 unsigned BTT : 8; 1503 1504 /// The value of the type trait. Unspecified if dependent. 1505 bool Value : 1; 1506 1507 /// Loc - The location of the type trait keyword. 1508 SourceLocation Loc; 1509 1510 /// RParen - The location of the closing paren. 1511 SourceLocation RParen; 1512 1513 /// The lhs type being queried. 1514 TypeSourceInfo *LhsType; 1515 1516 /// The rhs type being queried. 1517 TypeSourceInfo *RhsType; 1518 1519public: 1520 BinaryTypeTraitExpr(SourceLocation loc, BinaryTypeTrait btt, 1521 TypeSourceInfo *lhsType, TypeSourceInfo *rhsType, 1522 bool value, SourceLocation rparen, QualType ty) 1523 : Expr(BinaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, false, 1524 lhsType->getType()->isDependentType() || 1525 rhsType->getType()->isDependentType(), 1526 (lhsType->getType()->containsUnexpandedParameterPack() || 1527 rhsType->getType()->containsUnexpandedParameterPack())), 1528 BTT(btt), Value(value), Loc(loc), RParen(rparen), 1529 LhsType(lhsType), RhsType(rhsType) { } 1530 1531 1532 explicit BinaryTypeTraitExpr(EmptyShell Empty) 1533 : Expr(BinaryTypeTraitExprClass, Empty), BTT(0), Value(false), 1534 LhsType(), RhsType() { } 1535 1536 SourceRange getSourceRange() const { 1537 return SourceRange(Loc, RParen); 1538 } 1539 1540 BinaryTypeTrait getTrait() const { 1541 return static_cast<BinaryTypeTrait>(BTT); 1542 } 1543 1544 QualType getLhsType() const { return LhsType->getType(); } 1545 QualType getRhsType() const { return RhsType->getType(); } 1546 1547 TypeSourceInfo *getLhsTypeSourceInfo() const { return LhsType; } 1548 TypeSourceInfo *getRhsTypeSourceInfo() const { return RhsType; } 1549 1550 bool getValue() const { assert(!isTypeDependent()); return Value; } 1551 1552 static bool classof(const Stmt *T) { 1553 return T->getStmtClass() == BinaryTypeTraitExprClass; 1554 } 1555 static bool classof(const BinaryTypeTraitExpr *) { return true; } 1556 1557 // Iterators 1558 child_range children() { return child_range(); } 1559 1560 friend class ASTStmtReader; 1561}; 1562 1563/// \brief A reference to an overloaded function set, either an 1564/// \t UnresolvedLookupExpr or an \t UnresolvedMemberExpr. 1565class OverloadExpr : public Expr { 1566 /// The results. These are undesugared, which is to say, they may 1567 /// include UsingShadowDecls. Access is relative to the naming 1568 /// class. 1569 // FIXME: Allocate this data after the OverloadExpr subclass. 1570 DeclAccessPair *Results; 1571 unsigned NumResults; 1572 1573 /// The common name of these declarations. 1574 DeclarationNameInfo NameInfo; 1575 1576 /// The scope specifier, if any. 1577 NestedNameSpecifier *Qualifier; 1578 1579 /// The source range of the scope specifier. 1580 SourceRange QualifierRange; 1581 1582protected: 1583 /// True if the name was a template-id. 1584 bool HasExplicitTemplateArgs; 1585 1586 OverloadExpr(StmtClass K, ASTContext &C, 1587 NestedNameSpecifier *Qualifier, SourceRange QRange, 1588 const DeclarationNameInfo &NameInfo, 1589 const TemplateArgumentListInfo *TemplateArgs, 1590 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 1591 bool KnownDependent = false, 1592 bool KnownContainsUnexpandedParameterPack = false); 1593 1594 OverloadExpr(StmtClass K, EmptyShell Empty) 1595 : Expr(K, Empty), Results(0), NumResults(0), 1596 Qualifier(0), HasExplicitTemplateArgs(false) { } 1597 1598 void initializeResults(ASTContext &C, 1599 UnresolvedSetIterator Begin, 1600 UnresolvedSetIterator End); 1601 1602public: 1603 struct FindResult { 1604 OverloadExpr *Expression; 1605 bool IsAddressOfOperand; 1606 bool HasFormOfMemberPointer; 1607 }; 1608 1609 /// Finds the overloaded expression in the given expression of 1610 /// OverloadTy. 1611 /// 1612 /// \return the expression (which must be there) and true if it has 1613 /// the particular form of a member pointer expression 1614 static FindResult find(Expr *E) { 1615 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 1616 1617 FindResult Result; 1618 1619 E = E->IgnoreParens(); 1620 if (isa<UnaryOperator>(E)) { 1621 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 1622 E = cast<UnaryOperator>(E)->getSubExpr(); 1623 OverloadExpr *Ovl = cast<OverloadExpr>(E->IgnoreParens()); 1624 1625 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier()); 1626 Result.IsAddressOfOperand = true; 1627 Result.Expression = Ovl; 1628 } else { 1629 Result.HasFormOfMemberPointer = false; 1630 Result.IsAddressOfOperand = false; 1631 Result.Expression = cast<OverloadExpr>(E); 1632 } 1633 1634 return Result; 1635 } 1636 1637 /// Gets the naming class of this lookup, if any. 1638 CXXRecordDecl *getNamingClass() const; 1639 1640 typedef UnresolvedSetImpl::iterator decls_iterator; 1641 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); } 1642 decls_iterator decls_end() const { 1643 return UnresolvedSetIterator(Results + NumResults); 1644 } 1645 1646 /// Gets the number of declarations in the unresolved set. 1647 unsigned getNumDecls() const { return NumResults; } 1648 1649 /// Gets the full name info. 1650 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 1651 void setNameInfo(const DeclarationNameInfo &N) { NameInfo = N; } 1652 1653 /// Gets the name looked up. 1654 DeclarationName getName() const { return NameInfo.getName(); } 1655 void setName(DeclarationName N) { NameInfo.setName(N); } 1656 1657 /// Gets the location of the name. 1658 SourceLocation getNameLoc() const { return NameInfo.getLoc(); } 1659 void setNameLoc(SourceLocation Loc) { NameInfo.setLoc(Loc); } 1660 1661 /// Fetches the nested-name qualifier, if one was given. 1662 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1663 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 1664 1665 /// Fetches the range of the nested-name qualifier. 1666 SourceRange getQualifierRange() const { return QualifierRange; } 1667 void setQualifierRange(SourceRange R) { QualifierRange = R; } 1668 1669 /// \brief Determines whether this expression had an explicit 1670 /// template argument list, e.g. f<int>. 1671 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 1672 1673 ExplicitTemplateArgumentList &getExplicitTemplateArgs(); // defined far below 1674 1675 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1676 return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs(); 1677 } 1678 1679 /// \brief Retrieves the optional explicit template arguments. 1680 /// This points to the same data as getExplicitTemplateArgs(), but 1681 /// returns null if there are no explicit template arguments. 1682 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 1683 if (!hasExplicitTemplateArgs()) return 0; 1684 return &getExplicitTemplateArgs(); 1685 } 1686 1687 static bool classof(const Stmt *T) { 1688 return T->getStmtClass() == UnresolvedLookupExprClass || 1689 T->getStmtClass() == UnresolvedMemberExprClass; 1690 } 1691 static bool classof(const OverloadExpr *) { return true; } 1692 1693 friend class ASTStmtReader; 1694 friend class ASTStmtWriter; 1695}; 1696 1697/// \brief A reference to a name which we were able to look up during 1698/// parsing but could not resolve to a specific declaration. This 1699/// arises in several ways: 1700/// * we might be waiting for argument-dependent lookup 1701/// * the name might resolve to an overloaded function 1702/// and eventually: 1703/// * the lookup might have included a function template 1704/// These never include UnresolvedUsingValueDecls, which are always 1705/// class members and therefore appear only in 1706/// UnresolvedMemberLookupExprs. 1707class UnresolvedLookupExpr : public OverloadExpr { 1708 /// True if these lookup results should be extended by 1709 /// argument-dependent lookup if this is the operand of a function 1710 /// call. 1711 bool RequiresADL; 1712 1713 /// True if these lookup results are overloaded. This is pretty 1714 /// trivially rederivable if we urgently need to kill this field. 1715 bool Overloaded; 1716 1717 /// The naming class (C++ [class.access.base]p5) of the lookup, if 1718 /// any. This can generally be recalculated from the context chain, 1719 /// but that can be fairly expensive for unqualified lookups. If we 1720 /// want to improve memory use here, this could go in a union 1721 /// against the qualified-lookup bits. 1722 CXXRecordDecl *NamingClass; 1723 1724 UnresolvedLookupExpr(ASTContext &C, 1725 CXXRecordDecl *NamingClass, 1726 NestedNameSpecifier *Qualifier, SourceRange QRange, 1727 const DeclarationNameInfo &NameInfo, 1728 bool RequiresADL, bool Overloaded, 1729 const TemplateArgumentListInfo *TemplateArgs, 1730 UnresolvedSetIterator Begin, UnresolvedSetIterator End) 1731 : OverloadExpr(UnresolvedLookupExprClass, C, Qualifier, QRange, NameInfo, 1732 TemplateArgs, Begin, End), 1733 RequiresADL(RequiresADL), Overloaded(Overloaded), NamingClass(NamingClass) 1734 {} 1735 1736 UnresolvedLookupExpr(EmptyShell Empty) 1737 : OverloadExpr(UnresolvedLookupExprClass, Empty), 1738 RequiresADL(false), Overloaded(false), NamingClass(0) 1739 {} 1740 1741public: 1742 static UnresolvedLookupExpr *Create(ASTContext &C, 1743 CXXRecordDecl *NamingClass, 1744 NestedNameSpecifier *Qualifier, 1745 SourceRange QualifierRange, 1746 const DeclarationNameInfo &NameInfo, 1747 bool ADL, bool Overloaded, 1748 UnresolvedSetIterator Begin, 1749 UnresolvedSetIterator End) { 1750 return new(C) UnresolvedLookupExpr(C, NamingClass, Qualifier, 1751 QualifierRange, NameInfo, ADL, 1752 Overloaded, 0, Begin, End); 1753 } 1754 1755 static UnresolvedLookupExpr *Create(ASTContext &C, 1756 CXXRecordDecl *NamingClass, 1757 NestedNameSpecifier *Qualifier, 1758 SourceRange QualifierRange, 1759 const DeclarationNameInfo &NameInfo, 1760 bool ADL, 1761 const TemplateArgumentListInfo &Args, 1762 UnresolvedSetIterator Begin, 1763 UnresolvedSetIterator End); 1764 1765 static UnresolvedLookupExpr *CreateEmpty(ASTContext &C, 1766 bool HasExplicitTemplateArgs, 1767 unsigned NumTemplateArgs); 1768 1769 /// True if this declaration should be extended by 1770 /// argument-dependent lookup. 1771 bool requiresADL() const { return RequiresADL; } 1772 void setRequiresADL(bool V) { RequiresADL = V; } 1773 1774 /// True if this lookup is overloaded. 1775 bool isOverloaded() const { return Overloaded; } 1776 void setOverloaded(bool V) { Overloaded = V; } 1777 1778 /// Gets the 'naming class' (in the sense of C++0x 1779 /// [class.access.base]p5) of the lookup. This is the scope 1780 /// that was looked in to find these results. 1781 CXXRecordDecl *getNamingClass() const { return NamingClass; } 1782 void setNamingClass(CXXRecordDecl *D) { NamingClass = D; } 1783 1784 // Note that, inconsistently with the explicit-template-argument AST 1785 // nodes, users are *forbidden* from calling these methods on objects 1786 // without explicit template arguments. 1787 1788 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 1789 assert(hasExplicitTemplateArgs()); 1790 return *reinterpret_cast<ExplicitTemplateArgumentList*>(this + 1); 1791 } 1792 1793 /// Gets a reference to the explicit template argument list. 1794 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1795 assert(hasExplicitTemplateArgs()); 1796 return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1); 1797 } 1798 1799 /// \brief Retrieves the optional explicit template arguments. 1800 /// This points to the same data as getExplicitTemplateArgs(), but 1801 /// returns null if there are no explicit template arguments. 1802 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 1803 if (!hasExplicitTemplateArgs()) return 0; 1804 return &getExplicitTemplateArgs(); 1805 } 1806 1807 /// \brief Copies the template arguments (if present) into the given 1808 /// structure. 1809 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1810 getExplicitTemplateArgs().copyInto(List); 1811 } 1812 1813 SourceLocation getLAngleLoc() const { 1814 return getExplicitTemplateArgs().LAngleLoc; 1815 } 1816 1817 SourceLocation getRAngleLoc() const { 1818 return getExplicitTemplateArgs().RAngleLoc; 1819 } 1820 1821 TemplateArgumentLoc const *getTemplateArgs() const { 1822 return getExplicitTemplateArgs().getTemplateArgs(); 1823 } 1824 1825 unsigned getNumTemplateArgs() const { 1826 return getExplicitTemplateArgs().NumTemplateArgs; 1827 } 1828 1829 SourceRange getSourceRange() const { 1830 SourceRange Range(getNameInfo().getSourceRange()); 1831 if (getQualifier()) Range.setBegin(getQualifierRange().getBegin()); 1832 if (hasExplicitTemplateArgs()) Range.setEnd(getRAngleLoc()); 1833 return Range; 1834 } 1835 1836 child_range children() { return child_range(); } 1837 1838 static bool classof(const Stmt *T) { 1839 return T->getStmtClass() == UnresolvedLookupExprClass; 1840 } 1841 static bool classof(const UnresolvedLookupExpr *) { return true; } 1842}; 1843 1844/// \brief A qualified reference to a name whose declaration cannot 1845/// yet be resolved. 1846/// 1847/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 1848/// it expresses a reference to a declaration such as 1849/// X<T>::value. The difference, however, is that an 1850/// DependentScopeDeclRefExpr node is used only within C++ templates when 1851/// the qualification (e.g., X<T>::) refers to a dependent type. In 1852/// this case, X<T>::value cannot resolve to a declaration because the 1853/// declaration will differ from on instantiation of X<T> to the 1854/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 1855/// qualifier (X<T>::) and the name of the entity being referenced 1856/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 1857/// declaration can be found. 1858class DependentScopeDeclRefExpr : public Expr { 1859 /// The name of the entity we will be referencing. 1860 DeclarationNameInfo NameInfo; 1861 1862 /// QualifierRange - The source range that covers the 1863 /// nested-name-specifier. 1864 SourceRange QualifierRange; 1865 1866 /// \brief The nested-name-specifier that qualifies this unresolved 1867 /// declaration name. 1868 NestedNameSpecifier *Qualifier; 1869 1870 /// \brief Whether the name includes explicit template arguments. 1871 bool HasExplicitTemplateArgs; 1872 1873 DependentScopeDeclRefExpr(QualType T, 1874 NestedNameSpecifier *Qualifier, 1875 SourceRange QualifierRange, 1876 const DeclarationNameInfo &NameInfo, 1877 const TemplateArgumentListInfo *Args); 1878 1879public: 1880 static DependentScopeDeclRefExpr *Create(ASTContext &C, 1881 NestedNameSpecifier *Qualifier, 1882 SourceRange QualifierRange, 1883 const DeclarationNameInfo &NameInfo, 1884 const TemplateArgumentListInfo *TemplateArgs = 0); 1885 1886 static DependentScopeDeclRefExpr *CreateEmpty(ASTContext &C, 1887 bool HasExplicitTemplateArgs, 1888 unsigned NumTemplateArgs); 1889 1890 /// \brief Retrieve the name that this expression refers to. 1891 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 1892 void setNameInfo(const DeclarationNameInfo &N) { NameInfo = N; } 1893 1894 /// \brief Retrieve the name that this expression refers to. 1895 DeclarationName getDeclName() const { return NameInfo.getName(); } 1896 void setDeclName(DeclarationName N) { NameInfo.setName(N); } 1897 1898 /// \brief Retrieve the location of the name within the expression. 1899 SourceLocation getLocation() const { return NameInfo.getLoc(); } 1900 void setLocation(SourceLocation L) { NameInfo.setLoc(L); } 1901 1902 /// \brief Retrieve the source range of the nested-name-specifier. 1903 SourceRange getQualifierRange() const { return QualifierRange; } 1904 void setQualifierRange(SourceRange R) { QualifierRange = R; } 1905 1906 /// \brief Retrieve the nested-name-specifier that qualifies this 1907 /// declaration. 1908 NestedNameSpecifier *getQualifier() const { return Qualifier; } 1909 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 1910 1911 /// Determines whether this lookup had explicit template arguments. 1912 bool hasExplicitTemplateArgs() const { return HasExplicitTemplateArgs; } 1913 1914 // Note that, inconsistently with the explicit-template-argument AST 1915 // nodes, users are *forbidden* from calling these methods on objects 1916 // without explicit template arguments. 1917 1918 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 1919 assert(hasExplicitTemplateArgs()); 1920 return *reinterpret_cast<ExplicitTemplateArgumentList*>(this + 1); 1921 } 1922 1923 /// Gets a reference to the explicit template argument list. 1924 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 1925 assert(hasExplicitTemplateArgs()); 1926 return *reinterpret_cast<const ExplicitTemplateArgumentList*>(this + 1); 1927 } 1928 1929 /// \brief Retrieves the optional explicit template arguments. 1930 /// This points to the same data as getExplicitTemplateArgs(), but 1931 /// returns null if there are no explicit template arguments. 1932 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 1933 if (!hasExplicitTemplateArgs()) return 0; 1934 return &getExplicitTemplateArgs(); 1935 } 1936 1937 /// \brief Copies the template arguments (if present) into the given 1938 /// structure. 1939 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 1940 getExplicitTemplateArgs().copyInto(List); 1941 } 1942 1943 SourceLocation getLAngleLoc() const { 1944 return getExplicitTemplateArgs().LAngleLoc; 1945 } 1946 1947 SourceLocation getRAngleLoc() const { 1948 return getExplicitTemplateArgs().RAngleLoc; 1949 } 1950 1951 TemplateArgumentLoc const *getTemplateArgs() const { 1952 return getExplicitTemplateArgs().getTemplateArgs(); 1953 } 1954 1955 unsigned getNumTemplateArgs() const { 1956 return getExplicitTemplateArgs().NumTemplateArgs; 1957 } 1958 1959 SourceRange getSourceRange() const { 1960 SourceRange Range(QualifierRange.getBegin(), getLocation()); 1961 if (hasExplicitTemplateArgs()) 1962 Range.setEnd(getRAngleLoc()); 1963 return Range; 1964 } 1965 1966 static bool classof(const Stmt *T) { 1967 return T->getStmtClass() == DependentScopeDeclRefExprClass; 1968 } 1969 static bool classof(const DependentScopeDeclRefExpr *) { return true; } 1970 1971 child_range children() { return child_range(); } 1972 1973 friend class ASTStmtReader; 1974 friend class ASTStmtWriter; 1975}; 1976 1977/// Represents an expression --- generally a full-expression --- which 1978/// introduces cleanups to be run at the end of the sub-expression's 1979/// evaluation. The most common source of expression-introduced 1980/// cleanups is temporary objects in C++, but several other C++ 1981/// expressions can create cleanups. 1982class ExprWithCleanups : public Expr { 1983 Stmt *SubExpr; 1984 1985 CXXTemporary **Temps; 1986 unsigned NumTemps; 1987 1988 ExprWithCleanups(ASTContext &C, Expr *SubExpr, 1989 CXXTemporary **Temps, unsigned NumTemps); 1990 1991public: 1992 ExprWithCleanups(EmptyShell Empty) 1993 : Expr(ExprWithCleanupsClass, Empty), 1994 SubExpr(0), Temps(0), NumTemps(0) {} 1995 1996 static ExprWithCleanups *Create(ASTContext &C, Expr *SubExpr, 1997 CXXTemporary **Temps, 1998 unsigned NumTemps); 1999 2000 unsigned getNumTemporaries() const { return NumTemps; } 2001 void setNumTemporaries(ASTContext &C, unsigned N); 2002 2003 CXXTemporary *getTemporary(unsigned i) { 2004 assert(i < NumTemps && "Index out of range"); 2005 return Temps[i]; 2006 } 2007 const CXXTemporary *getTemporary(unsigned i) const { 2008 return const_cast<ExprWithCleanups*>(this)->getTemporary(i); 2009 } 2010 void setTemporary(unsigned i, CXXTemporary *T) { 2011 assert(i < NumTemps && "Index out of range"); 2012 Temps[i] = T; 2013 } 2014 2015 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 2016 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 2017 void setSubExpr(Expr *E) { SubExpr = E; } 2018 2019 SourceRange getSourceRange() const { 2020 return SubExpr->getSourceRange(); 2021 } 2022 2023 // Implement isa/cast/dyncast/etc. 2024 static bool classof(const Stmt *T) { 2025 return T->getStmtClass() == ExprWithCleanupsClass; 2026 } 2027 static bool classof(const ExprWithCleanups *) { return true; } 2028 2029 // Iterators 2030 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 2031}; 2032 2033/// \brief Describes an explicit type conversion that uses functional 2034/// notion but could not be resolved because one or more arguments are 2035/// type-dependent. 2036/// 2037/// The explicit type conversions expressed by 2038/// CXXUnresolvedConstructExpr have the form \c T(a1, a2, ..., aN), 2039/// where \c T is some type and \c a1, a2, ..., aN are values, and 2040/// either \C T is a dependent type or one or more of the \c a's is 2041/// type-dependent. For example, this would occur in a template such 2042/// as: 2043/// 2044/// \code 2045/// template<typename T, typename A1> 2046/// inline T make_a(const A1& a1) { 2047/// return T(a1); 2048/// } 2049/// \endcode 2050/// 2051/// When the returned expression is instantiated, it may resolve to a 2052/// constructor call, conversion function call, or some kind of type 2053/// conversion. 2054class CXXUnresolvedConstructExpr : public Expr { 2055 /// \brief The type being constructed. 2056 TypeSourceInfo *Type; 2057 2058 /// \brief The location of the left parentheses ('('). 2059 SourceLocation LParenLoc; 2060 2061 /// \brief The location of the right parentheses (')'). 2062 SourceLocation RParenLoc; 2063 2064 /// \brief The number of arguments used to construct the type. 2065 unsigned NumArgs; 2066 2067 CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 2068 SourceLocation LParenLoc, 2069 Expr **Args, 2070 unsigned NumArgs, 2071 SourceLocation RParenLoc); 2072 2073 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) 2074 : Expr(CXXUnresolvedConstructExprClass, Empty), Type(), NumArgs(NumArgs) { } 2075 2076 friend class ASTStmtReader; 2077 2078public: 2079 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 2080 TypeSourceInfo *Type, 2081 SourceLocation LParenLoc, 2082 Expr **Args, 2083 unsigned NumArgs, 2084 SourceLocation RParenLoc); 2085 2086 static CXXUnresolvedConstructExpr *CreateEmpty(ASTContext &C, 2087 unsigned NumArgs); 2088 2089 /// \brief Retrieve the type that is being constructed, as specified 2090 /// in the source code. 2091 QualType getTypeAsWritten() const { return Type->getType(); } 2092 2093 /// \brief Retrieve the type source information for the type being 2094 /// constructed. 2095 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 2096 2097 /// \brief Retrieve the location of the left parentheses ('(') that 2098 /// precedes the argument list. 2099 SourceLocation getLParenLoc() const { return LParenLoc; } 2100 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 2101 2102 /// \brief Retrieve the location of the right parentheses (')') that 2103 /// follows the argument list. 2104 SourceLocation getRParenLoc() const { return RParenLoc; } 2105 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 2106 2107 /// \brief Retrieve the number of arguments. 2108 unsigned arg_size() const { return NumArgs; } 2109 2110 typedef Expr** arg_iterator; 2111 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 2112 arg_iterator arg_end() { return arg_begin() + NumArgs; } 2113 2114 typedef const Expr* const * const_arg_iterator; 2115 const_arg_iterator arg_begin() const { 2116 return reinterpret_cast<const Expr* const *>(this + 1); 2117 } 2118 const_arg_iterator arg_end() const { 2119 return arg_begin() + NumArgs; 2120 } 2121 2122 Expr *getArg(unsigned I) { 2123 assert(I < NumArgs && "Argument index out-of-range"); 2124 return *(arg_begin() + I); 2125 } 2126 2127 const Expr *getArg(unsigned I) const { 2128 assert(I < NumArgs && "Argument index out-of-range"); 2129 return *(arg_begin() + I); 2130 } 2131 2132 void setArg(unsigned I, Expr *E) { 2133 assert(I < NumArgs && "Argument index out-of-range"); 2134 *(arg_begin() + I) = E; 2135 } 2136 2137 SourceRange getSourceRange() const; 2138 2139 static bool classof(const Stmt *T) { 2140 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 2141 } 2142 static bool classof(const CXXUnresolvedConstructExpr *) { return true; } 2143 2144 // Iterators 2145 child_range children() { 2146 Stmt **begin = reinterpret_cast<Stmt**>(this+1); 2147 return child_range(begin, begin + NumArgs); 2148 } 2149}; 2150 2151/// \brief Represents a C++ member access expression where the actual 2152/// member referenced could not be resolved because the base 2153/// expression or the member name was dependent. 2154/// 2155/// Like UnresolvedMemberExprs, these can be either implicit or 2156/// explicit accesses. It is only possible to get one of these with 2157/// an implicit access if a qualifier is provided. 2158class CXXDependentScopeMemberExpr : public Expr { 2159 /// \brief The expression for the base pointer or class reference, 2160 /// e.g., the \c x in x.f. Can be null in implicit accesses. 2161 Stmt *Base; 2162 2163 /// \brief The type of the base expression. Never null, even for 2164 /// implicit accesses. 2165 QualType BaseType; 2166 2167 /// \brief Whether this member expression used the '->' operator or 2168 /// the '.' operator. 2169 bool IsArrow : 1; 2170 2171 /// \brief Whether this member expression has explicitly-specified template 2172 /// arguments. 2173 bool HasExplicitTemplateArgs : 1; 2174 2175 /// \brief The location of the '->' or '.' operator. 2176 SourceLocation OperatorLoc; 2177 2178 /// \brief The nested-name-specifier that precedes the member name, if any. 2179 NestedNameSpecifier *Qualifier; 2180 2181 /// \brief The source range covering the nested name specifier. 2182 SourceRange QualifierRange; 2183 2184 /// \brief In a qualified member access expression such as t->Base::f, this 2185 /// member stores the resolves of name lookup in the context of the member 2186 /// access expression, to be used at instantiation time. 2187 /// 2188 /// FIXME: This member, along with the Qualifier and QualifierRange, could 2189 /// be stuck into a structure that is optionally allocated at the end of 2190 /// the CXXDependentScopeMemberExpr, to save space in the common case. 2191 NamedDecl *FirstQualifierFoundInScope; 2192 2193 /// \brief The member to which this member expression refers, which 2194 /// can be name, overloaded operator, or destructor. 2195 /// FIXME: could also be a template-id 2196 DeclarationNameInfo MemberNameInfo; 2197 2198 CXXDependentScopeMemberExpr(ASTContext &C, 2199 Expr *Base, QualType BaseType, bool IsArrow, 2200 SourceLocation OperatorLoc, 2201 NestedNameSpecifier *Qualifier, 2202 SourceRange QualifierRange, 2203 NamedDecl *FirstQualifierFoundInScope, 2204 DeclarationNameInfo MemberNameInfo, 2205 const TemplateArgumentListInfo *TemplateArgs); 2206 2207public: 2208 CXXDependentScopeMemberExpr(ASTContext &C, 2209 Expr *Base, QualType BaseType, 2210 bool IsArrow, 2211 SourceLocation OperatorLoc, 2212 NestedNameSpecifier *Qualifier, 2213 SourceRange QualifierRange, 2214 NamedDecl *FirstQualifierFoundInScope, 2215 DeclarationNameInfo MemberNameInfo); 2216 2217 static CXXDependentScopeMemberExpr * 2218 Create(ASTContext &C, 2219 Expr *Base, QualType BaseType, bool IsArrow, 2220 SourceLocation OperatorLoc, 2221 NestedNameSpecifier *Qualifier, 2222 SourceRange QualifierRange, 2223 NamedDecl *FirstQualifierFoundInScope, 2224 DeclarationNameInfo MemberNameInfo, 2225 const TemplateArgumentListInfo *TemplateArgs); 2226 2227 static CXXDependentScopeMemberExpr * 2228 CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs, 2229 unsigned NumTemplateArgs); 2230 2231 /// \brief True if this is an implicit access, i.e. one in which the 2232 /// member being accessed was not written in the source. The source 2233 /// location of the operator is invalid in this case. 2234 bool isImplicitAccess() const { return Base == 0; } 2235 2236 /// \brief Retrieve the base object of this member expressions, 2237 /// e.g., the \c x in \c x.m. 2238 Expr *getBase() const { 2239 assert(!isImplicitAccess()); 2240 return cast<Expr>(Base); 2241 } 2242 void setBase(Expr *E) { Base = E; } 2243 2244 QualType getBaseType() const { return BaseType; } 2245 void setBaseType(QualType T) { BaseType = T; } 2246 2247 /// \brief Determine whether this member expression used the '->' 2248 /// operator; otherwise, it used the '.' operator. 2249 bool isArrow() const { return IsArrow; } 2250 void setArrow(bool A) { IsArrow = A; } 2251 2252 /// \brief Retrieve the location of the '->' or '.' operator. 2253 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2254 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 2255 2256 /// \brief Retrieve the nested-name-specifier that qualifies the member 2257 /// name. 2258 NestedNameSpecifier *getQualifier() const { return Qualifier; } 2259 void setQualifier(NestedNameSpecifier *NNS) { Qualifier = NNS; } 2260 2261 /// \brief Retrieve the source range covering the nested-name-specifier 2262 /// that qualifies the member name. 2263 SourceRange getQualifierRange() const { return QualifierRange; } 2264 void setQualifierRange(SourceRange R) { QualifierRange = R; } 2265 2266 /// \brief Retrieve the first part of the nested-name-specifier that was 2267 /// found in the scope of the member access expression when the member access 2268 /// was initially parsed. 2269 /// 2270 /// This function only returns a useful result when member access expression 2271 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 2272 /// returned by this function describes what was found by unqualified name 2273 /// lookup for the identifier "Base" within the scope of the member access 2274 /// expression itself. At template instantiation time, this information is 2275 /// combined with the results of name lookup into the type of the object 2276 /// expression itself (the class type of x). 2277 NamedDecl *getFirstQualifierFoundInScope() const { 2278 return FirstQualifierFoundInScope; 2279 } 2280 void setFirstQualifierFoundInScope(NamedDecl *D) { 2281 FirstQualifierFoundInScope = D; 2282 } 2283 2284 /// \brief Retrieve the name of the member that this expression 2285 /// refers to. 2286 const DeclarationNameInfo &getMemberNameInfo() const { 2287 return MemberNameInfo; 2288 } 2289 void setMemberNameInfo(const DeclarationNameInfo &N) { MemberNameInfo = N; } 2290 2291 /// \brief Retrieve the name of the member that this expression 2292 /// refers to. 2293 DeclarationName getMember() const { return MemberNameInfo.getName(); } 2294 void setMember(DeclarationName N) { MemberNameInfo.setName(N); } 2295 2296 // \brief Retrieve the location of the name of the member that this 2297 // expression refers to. 2298 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); } 2299 void setMemberLoc(SourceLocation L) { MemberNameInfo.setLoc(L); } 2300 2301 /// \brief Determines whether this member expression actually had a C++ 2302 /// template argument list explicitly specified, e.g., x.f<int>. 2303 bool hasExplicitTemplateArgs() const { 2304 return HasExplicitTemplateArgs; 2305 } 2306 2307 /// \brief Retrieve the explicit template argument list that followed the 2308 /// member template name, if any. 2309 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 2310 assert(HasExplicitTemplateArgs); 2311 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 2312 } 2313 2314 /// \brief Retrieve the explicit template argument list that followed the 2315 /// member template name, if any. 2316 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 2317 return const_cast<CXXDependentScopeMemberExpr *>(this) 2318 ->getExplicitTemplateArgs(); 2319 } 2320 2321 /// \brief Retrieves the optional explicit template arguments. 2322 /// This points to the same data as getExplicitTemplateArgs(), but 2323 /// returns null if there are no explicit template arguments. 2324 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 2325 if (!hasExplicitTemplateArgs()) return 0; 2326 return &getExplicitTemplateArgs(); 2327 } 2328 2329 /// \brief Copies the template arguments (if present) into the given 2330 /// structure. 2331 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2332 getExplicitTemplateArgs().copyInto(List); 2333 } 2334 2335 /// \brief Initializes the template arguments using the given structure. 2336 void initializeTemplateArgumentsFrom(const TemplateArgumentListInfo &List) { 2337 getExplicitTemplateArgs().initializeFrom(List); 2338 } 2339 2340 /// \brief Retrieve the location of the left angle bracket following the 2341 /// member name ('<'), if any. 2342 SourceLocation getLAngleLoc() const { 2343 return getExplicitTemplateArgs().LAngleLoc; 2344 } 2345 2346 /// \brief Retrieve the template arguments provided as part of this 2347 /// template-id. 2348 const TemplateArgumentLoc *getTemplateArgs() const { 2349 return getExplicitTemplateArgs().getTemplateArgs(); 2350 } 2351 2352 /// \brief Retrieve the number of template arguments provided as part of this 2353 /// template-id. 2354 unsigned getNumTemplateArgs() const { 2355 return getExplicitTemplateArgs().NumTemplateArgs; 2356 } 2357 2358 /// \brief Retrieve the location of the right angle bracket following the 2359 /// template arguments ('>'). 2360 SourceLocation getRAngleLoc() const { 2361 return getExplicitTemplateArgs().RAngleLoc; 2362 } 2363 2364 SourceRange getSourceRange() const { 2365 SourceRange Range; 2366 if (!isImplicitAccess()) 2367 Range.setBegin(Base->getSourceRange().getBegin()); 2368 else if (getQualifier()) 2369 Range.setBegin(getQualifierRange().getBegin()); 2370 else 2371 Range.setBegin(MemberNameInfo.getBeginLoc()); 2372 2373 if (hasExplicitTemplateArgs()) 2374 Range.setEnd(getRAngleLoc()); 2375 else 2376 Range.setEnd(MemberNameInfo.getEndLoc()); 2377 return Range; 2378 } 2379 2380 static bool classof(const Stmt *T) { 2381 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 2382 } 2383 static bool classof(const CXXDependentScopeMemberExpr *) { return true; } 2384 2385 // Iterators 2386 child_range children() { 2387 if (isImplicitAccess()) return child_range(); 2388 return child_range(&Base, &Base + 1); 2389 } 2390 2391 friend class ASTStmtReader; 2392 friend class ASTStmtWriter; 2393}; 2394 2395/// \brief Represents a C++ member access expression for which lookup 2396/// produced a set of overloaded functions. 2397/// 2398/// The member access may be explicit or implicit: 2399/// struct A { 2400/// int a, b; 2401/// int explicitAccess() { return this->a + this->A::b; } 2402/// int implicitAccess() { return a + A::b; } 2403/// }; 2404/// 2405/// In the final AST, an explicit access always becomes a MemberExpr. 2406/// An implicit access may become either a MemberExpr or a 2407/// DeclRefExpr, depending on whether the member is static. 2408class UnresolvedMemberExpr : public OverloadExpr { 2409 /// \brief Whether this member expression used the '->' operator or 2410 /// the '.' operator. 2411 bool IsArrow : 1; 2412 2413 /// \brief Whether the lookup results contain an unresolved using 2414 /// declaration. 2415 bool HasUnresolvedUsing : 1; 2416 2417 /// \brief The expression for the base pointer or class reference, 2418 /// e.g., the \c x in x.f. This can be null if this is an 'unbased' 2419 /// member expression 2420 Stmt *Base; 2421 2422 /// \brief The type of the base expression; never null. 2423 QualType BaseType; 2424 2425 /// \brief The location of the '->' or '.' operator. 2426 SourceLocation OperatorLoc; 2427 2428 UnresolvedMemberExpr(ASTContext &C, bool HasUnresolvedUsing, 2429 Expr *Base, QualType BaseType, bool IsArrow, 2430 SourceLocation OperatorLoc, 2431 NestedNameSpecifier *Qualifier, 2432 SourceRange QualifierRange, 2433 const DeclarationNameInfo &MemberNameInfo, 2434 const TemplateArgumentListInfo *TemplateArgs, 2435 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 2436 2437 UnresolvedMemberExpr(EmptyShell Empty) 2438 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false), 2439 HasUnresolvedUsing(false), Base(0) { } 2440 2441public: 2442 static UnresolvedMemberExpr * 2443 Create(ASTContext &C, bool HasUnresolvedUsing, 2444 Expr *Base, QualType BaseType, bool IsArrow, 2445 SourceLocation OperatorLoc, 2446 NestedNameSpecifier *Qualifier, 2447 SourceRange QualifierRange, 2448 const DeclarationNameInfo &MemberNameInfo, 2449 const TemplateArgumentListInfo *TemplateArgs, 2450 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 2451 2452 static UnresolvedMemberExpr * 2453 CreateEmpty(ASTContext &C, bool HasExplicitTemplateArgs, 2454 unsigned NumTemplateArgs); 2455 2456 /// \brief True if this is an implicit access, i.e. one in which the 2457 /// member being accessed was not written in the source. The source 2458 /// location of the operator is invalid in this case. 2459 bool isImplicitAccess() const { return Base == 0; } 2460 2461 /// \brief Retrieve the base object of this member expressions, 2462 /// e.g., the \c x in \c x.m. 2463 Expr *getBase() { 2464 assert(!isImplicitAccess()); 2465 return cast<Expr>(Base); 2466 } 2467 const Expr *getBase() const { 2468 assert(!isImplicitAccess()); 2469 return cast<Expr>(Base); 2470 } 2471 void setBase(Expr *E) { Base = E; } 2472 2473 QualType getBaseType() const { return BaseType; } 2474 void setBaseType(QualType T) { BaseType = T; } 2475 2476 /// \brief Determine whether the lookup results contain an unresolved using 2477 /// declaration. 2478 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; } 2479 void setHasUnresolvedUsing(bool V) { HasUnresolvedUsing = V; } 2480 2481 /// \brief Determine whether this member expression used the '->' 2482 /// operator; otherwise, it used the '.' operator. 2483 bool isArrow() const { return IsArrow; } 2484 void setArrow(bool A) { IsArrow = A; } 2485 2486 /// \brief Retrieve the location of the '->' or '.' operator. 2487 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2488 void setOperatorLoc(SourceLocation L) { OperatorLoc = L; } 2489 2490 /// \brief Retrieves the naming class of this lookup. 2491 CXXRecordDecl *getNamingClass() const; 2492 2493 /// \brief Retrieve the full name info for the member that this expression 2494 /// refers to. 2495 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); } 2496 void setMemberNameInfo(const DeclarationNameInfo &N) { setNameInfo(N); } 2497 2498 /// \brief Retrieve the name of the member that this expression 2499 /// refers to. 2500 DeclarationName getMemberName() const { return getName(); } 2501 void setMemberName(DeclarationName N) { setName(N); } 2502 2503 // \brief Retrieve the location of the name of the member that this 2504 // expression refers to. 2505 SourceLocation getMemberLoc() const { return getNameLoc(); } 2506 void setMemberLoc(SourceLocation L) { setNameLoc(L); } 2507 2508 /// \brief Retrieve the explicit template argument list that followed the 2509 /// member template name. 2510 ExplicitTemplateArgumentList &getExplicitTemplateArgs() { 2511 assert(hasExplicitTemplateArgs()); 2512 return *reinterpret_cast<ExplicitTemplateArgumentList *>(this + 1); 2513 } 2514 2515 /// \brief Retrieve the explicit template argument list that followed the 2516 /// member template name, if any. 2517 const ExplicitTemplateArgumentList &getExplicitTemplateArgs() const { 2518 assert(hasExplicitTemplateArgs()); 2519 return *reinterpret_cast<const ExplicitTemplateArgumentList *>(this + 1); 2520 } 2521 2522 /// \brief Retrieves the optional explicit template arguments. 2523 /// This points to the same data as getExplicitTemplateArgs(), but 2524 /// returns null if there are no explicit template arguments. 2525 const ExplicitTemplateArgumentList *getOptionalExplicitTemplateArgs() { 2526 if (!hasExplicitTemplateArgs()) return 0; 2527 return &getExplicitTemplateArgs(); 2528 } 2529 2530 /// \brief Copies the template arguments into the given structure. 2531 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2532 getExplicitTemplateArgs().copyInto(List); 2533 } 2534 2535 /// \brief Retrieve the location of the left angle bracket following 2536 /// the member name ('<'). 2537 SourceLocation getLAngleLoc() const { 2538 return getExplicitTemplateArgs().LAngleLoc; 2539 } 2540 2541 /// \brief Retrieve the template arguments provided as part of this 2542 /// template-id. 2543 const TemplateArgumentLoc *getTemplateArgs() const { 2544 return getExplicitTemplateArgs().getTemplateArgs(); 2545 } 2546 2547 /// \brief Retrieve the number of template arguments provided as 2548 /// part of this template-id. 2549 unsigned getNumTemplateArgs() const { 2550 return getExplicitTemplateArgs().NumTemplateArgs; 2551 } 2552 2553 /// \brief Retrieve the location of the right angle bracket 2554 /// following the template arguments ('>'). 2555 SourceLocation getRAngleLoc() const { 2556 return getExplicitTemplateArgs().RAngleLoc; 2557 } 2558 2559 SourceRange getSourceRange() const { 2560 SourceRange Range = getMemberNameInfo().getSourceRange(); 2561 if (!isImplicitAccess()) 2562 Range.setBegin(Base->getSourceRange().getBegin()); 2563 else if (getQualifier()) 2564 Range.setBegin(getQualifierRange().getBegin()); 2565 2566 if (hasExplicitTemplateArgs()) 2567 Range.setEnd(getRAngleLoc()); 2568 return Range; 2569 } 2570 2571 static bool classof(const Stmt *T) { 2572 return T->getStmtClass() == UnresolvedMemberExprClass; 2573 } 2574 static bool classof(const UnresolvedMemberExpr *) { return true; } 2575 2576 // Iterators 2577 child_range children() { 2578 if (isImplicitAccess()) return child_range(); 2579 return child_range(&Base, &Base + 1); 2580 } 2581}; 2582 2583/// \brief Represents a C++0x noexcept expression (C++ [expr.unary.noexcept]). 2584/// 2585/// The noexcept expression tests whether a given expression might throw. Its 2586/// result is a boolean constant. 2587class CXXNoexceptExpr : public Expr { 2588 bool Value : 1; 2589 Stmt *Operand; 2590 SourceRange Range; 2591 2592 friend class ASTStmtReader; 2593 2594public: 2595 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val, 2596 SourceLocation Keyword, SourceLocation RParen) 2597 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary, 2598 /*TypeDependent*/false, 2599 /*ValueDependent*/Val == CT_Dependent, 2600 Operand->containsUnexpandedParameterPack()), 2601 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen) 2602 { } 2603 2604 CXXNoexceptExpr(EmptyShell Empty) 2605 : Expr(CXXNoexceptExprClass, Empty) 2606 { } 2607 2608 Expr *getOperand() const { return static_cast<Expr*>(Operand); } 2609 2610 SourceRange getSourceRange() const { return Range; } 2611 2612 bool getValue() const { return Value; } 2613 2614 static bool classof(const Stmt *T) { 2615 return T->getStmtClass() == CXXNoexceptExprClass; 2616 } 2617 static bool classof(const CXXNoexceptExpr *) { return true; } 2618 2619 // Iterators 2620 child_range children() { return child_range(&Operand, &Operand + 1); } 2621}; 2622 2623/// \brief Represents a C++0x pack expansion that produces a sequence of 2624/// expressions. 2625/// 2626/// A pack expansion expression contains a pattern (which itself is an 2627/// expression) followed by an ellipsis. For example: 2628/// 2629/// \code 2630/// template<typename F, typename ...Types> 2631/// void forward(F f, Types &&...args) { 2632/// f(static_cast<Types&&>(args)...); 2633/// } 2634/// \endcode 2635/// 2636/// Here, the argument to the function object \c f is a pack expansion whose 2637/// pattern is \c static_cast<Types&&>(args). When the \c forward function 2638/// template is instantiated, the pack expansion will instantiate to zero or 2639/// or more function arguments to the function object \c f. 2640class PackExpansionExpr : public Expr { 2641 SourceLocation EllipsisLoc; 2642 2643 /// \brief The number of expansions that will be produced by this pack 2644 /// expansion expression, if known. 2645 /// 2646 /// When zero, the number of expansions is not known. Otherwise, this value 2647 /// is the number of expansions + 1. 2648 unsigned NumExpansions; 2649 2650 Stmt *Pattern; 2651 2652 friend class ASTStmtReader; 2653 friend class ASTStmtWriter; 2654 2655public: 2656 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc, 2657 llvm::Optional<unsigned> NumExpansions) 2658 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(), 2659 Pattern->getObjectKind(), /*TypeDependent=*/true, 2660 /*ValueDependent=*/true, /*ContainsUnexpandedParameterPack=*/false), 2661 EllipsisLoc(EllipsisLoc), 2662 NumExpansions(NumExpansions? *NumExpansions + 1 : 0), 2663 Pattern(Pattern) { } 2664 2665 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { } 2666 2667 /// \brief Retrieve the pattern of the pack expansion. 2668 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); } 2669 2670 /// \brief Retrieve the pattern of the pack expansion. 2671 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); } 2672 2673 /// \brief Retrieve the location of the ellipsis that describes this pack 2674 /// expansion. 2675 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 2676 2677 /// \brief Determine the number of expansions that will be produced when 2678 /// this pack expansion is instantiated, if already known. 2679 llvm::Optional<unsigned> getNumExpansions() const { 2680 if (NumExpansions) 2681 return NumExpansions - 1; 2682 2683 return llvm::Optional<unsigned>(); 2684 } 2685 2686 SourceRange getSourceRange() const { 2687 return SourceRange(Pattern->getLocStart(), EllipsisLoc); 2688 } 2689 2690 static bool classof(const Stmt *T) { 2691 return T->getStmtClass() == PackExpansionExprClass; 2692 } 2693 static bool classof(const PackExpansionExpr *) { return true; } 2694 2695 // Iterators 2696 child_range children() { 2697 return child_range(&Pattern, &Pattern + 1); 2698 } 2699}; 2700 2701inline ExplicitTemplateArgumentList &OverloadExpr::getExplicitTemplateArgs() { 2702 if (isa<UnresolvedLookupExpr>(this)) 2703 return cast<UnresolvedLookupExpr>(this)->getExplicitTemplateArgs(); 2704 else 2705 return cast<UnresolvedMemberExpr>(this)->getExplicitTemplateArgs(); 2706} 2707 2708/// \brief Represents an expression that computes the length of a parameter 2709/// pack. 2710/// 2711/// \code 2712/// template<typename ...Types> 2713/// struct count { 2714/// static const unsigned value = sizeof...(Types); 2715/// }; 2716/// \endcode 2717class SizeOfPackExpr : public Expr { 2718 /// \brief The location of the 'sizeof' keyword. 2719 SourceLocation OperatorLoc; 2720 2721 /// \brief The location of the name of the parameter pack. 2722 SourceLocation PackLoc; 2723 2724 /// \brief The location of the closing parenthesis. 2725 SourceLocation RParenLoc; 2726 2727 /// \brief The length of the parameter pack, if known. 2728 /// 2729 /// When this expression is value-dependent, the length of the parameter pack 2730 /// is unknown. When this expression is not value-dependent, the length is 2731 /// known. 2732 unsigned Length; 2733 2734 /// \brief The parameter pack itself. 2735 NamedDecl *Pack; 2736 2737 friend class ASTStmtReader; 2738 friend class ASTStmtWriter; 2739 2740public: 2741 /// \brief Creates a value-dependent expression that computes the length of 2742 /// the given parameter pack. 2743 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 2744 SourceLocation PackLoc, SourceLocation RParenLoc) 2745 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 2746 /*TypeDependent=*/false, /*ValueDependent=*/true, 2747 /*ContainsUnexpandedParameterPack=*/false), 2748 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 2749 Length(0), Pack(Pack) { } 2750 2751 /// \brief Creates an expression that computes the length of 2752 /// the given parameter pack, which is already known. 2753 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 2754 SourceLocation PackLoc, SourceLocation RParenLoc, 2755 unsigned Length) 2756 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 2757 /*TypeDependent=*/false, /*ValueDependent=*/false, 2758 /*ContainsUnexpandedParameterPack=*/false), 2759 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 2760 Length(Length), Pack(Pack) { } 2761 2762 /// \brief Create an empty expression. 2763 SizeOfPackExpr(EmptyShell Empty) : Expr(SizeOfPackExprClass, Empty) { } 2764 2765 /// \brief Determine the location of the 'sizeof' keyword. 2766 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2767 2768 /// \brief Determine the location of the parameter pack. 2769 SourceLocation getPackLoc() const { return PackLoc; } 2770 2771 /// \brief Determine the location of the right parenthesis. 2772 SourceLocation getRParenLoc() const { return RParenLoc; } 2773 2774 /// \brief Retrieve the parameter pack. 2775 NamedDecl *getPack() const { return Pack; } 2776 2777 /// \brief Retrieve the length of the parameter pack. 2778 /// 2779 /// This routine may only be invoked when the expression is not 2780 /// value-dependent. 2781 unsigned getPackLength() const { 2782 assert(!isValueDependent() && 2783 "Cannot get the length of a value-dependent pack size expression"); 2784 return Length; 2785 } 2786 2787 SourceRange getSourceRange() const { 2788 return SourceRange(OperatorLoc, RParenLoc); 2789 } 2790 2791 static bool classof(const Stmt *T) { 2792 return T->getStmtClass() == SizeOfPackExprClass; 2793 } 2794 static bool classof(const SizeOfPackExpr *) { return true; } 2795 2796 // Iterators 2797 child_range children() { return child_range(); } 2798}; 2799 2800/// \brief Represents a reference to a non-type template parameter pack that 2801/// has been substituted with a non-template argument pack. 2802/// 2803/// When a pack expansion in the source code contains multiple parameter packs 2804/// and those parameter packs correspond to different levels of template 2805/// parameter lists, this node node is used to represent a non-type template 2806/// parameter pack from an outer level, which has already had its argument pack 2807/// substituted but that still lives within a pack expansion that itself 2808/// could not be instantiated. When actually performing a substitution into 2809/// that pack expansion (e.g., when all template parameters have corresponding 2810/// arguments), this type will be replaced with the appropriate underlying 2811/// expression at the current pack substitution index. 2812class SubstNonTypeTemplateParmPackExpr : public Expr { 2813 /// \brief The non-type template parameter pack itself. 2814 NonTypeTemplateParmDecl *Param; 2815 2816 /// \brief A pointer to the set of template arguments that this 2817 /// parameter pack is instantiated with. 2818 const TemplateArgument *Arguments; 2819 2820 /// \brief The number of template arguments in \c Arguments. 2821 unsigned NumArguments; 2822 2823 /// \brief The location of the non-type template parameter pack reference. 2824 SourceLocation NameLoc; 2825 2826 friend class ASTStmtReader; 2827 friend class ASTStmtWriter; 2828 2829public: 2830 SubstNonTypeTemplateParmPackExpr(QualType T, 2831 NonTypeTemplateParmDecl *Param, 2832 SourceLocation NameLoc, 2833 const TemplateArgument &ArgPack); 2834 2835 SubstNonTypeTemplateParmPackExpr(EmptyShell Empty) 2836 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) { } 2837 2838 /// \brief Retrieve the non-type template parameter pack being substituted. 2839 NonTypeTemplateParmDecl *getParameterPack() const { return Param; } 2840 2841 /// \brief Retrieve the location of the parameter pack name. 2842 SourceLocation getParameterPackLocation() const { return NameLoc; } 2843 2844 /// \brief Retrieve the template argument pack containing the substituted 2845 /// template arguments. 2846 TemplateArgument getArgumentPack() const; 2847 2848 SourceRange getSourceRange() const { return NameLoc; } 2849 2850 static bool classof(const Stmt *T) { 2851 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass; 2852 } 2853 static bool classof(const SubstNonTypeTemplateParmPackExpr *) { 2854 return true; 2855 } 2856 2857 // Iterators 2858 child_range children() { return child_range(); } 2859}; 2860 2861} // end namespace clang 2862 2863#endif 2864