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