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