ExprCXX.h revision cdd4b78583120222b82148626119b3e80ae1d291
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(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(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(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(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(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(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(ASTContext &Context, QualType T, 345 ExprValueKind VK, Expr *Op, 346 TypeSourceInfo *WrittenTy, SourceLocation L, 347 SourceLocation RParenLoc, 348 SourceRange AngleBrackets); 349 static CXXConstCastExpr *CreateEmpty(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(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(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() 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() 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 there is none. 739 static UuidAttr *GetUuidAttrOfType(QualType QT); 740 741 // Iterators 742 child_range children() { 743 if (isTypeOperand()) return child_range(); 744 Stmt **begin = reinterpret_cast<Stmt**>(&Operand); 745 return child_range(begin, begin + 1); 746 } 747}; 748 749/// \brief Represents the \c this expression in C++. 750/// 751/// This is a pointer to the object on which the current member function is 752/// executing (C++ [expr.prim]p3). Example: 753/// 754/// \code 755/// class Foo { 756/// public: 757/// void bar(); 758/// void test() { this->bar(); } 759/// }; 760/// \endcode 761class CXXThisExpr : public Expr { 762 SourceLocation Loc; 763 bool Implicit : 1; 764 765public: 766 CXXThisExpr(SourceLocation L, QualType Type, bool isImplicit) 767 : Expr(CXXThisExprClass, Type, VK_RValue, OK_Ordinary, 768 // 'this' is type-dependent if the class type of the enclosing 769 // member function is dependent (C++ [temp.dep.expr]p2) 770 Type->isDependentType(), Type->isDependentType(), 771 Type->isInstantiationDependentType(), 772 /*ContainsUnexpandedParameterPack=*/false), 773 Loc(L), Implicit(isImplicit) { } 774 775 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {} 776 777 SourceLocation getLocation() const { return Loc; } 778 void setLocation(SourceLocation L) { Loc = L; } 779 780 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 781 SourceLocation getLocEnd() const LLVM_READONLY { return Loc; } 782 783 bool isImplicit() const { return Implicit; } 784 void setImplicit(bool I) { Implicit = I; } 785 786 static bool classof(const Stmt *T) { 787 return T->getStmtClass() == CXXThisExprClass; 788 } 789 790 // Iterators 791 child_range children() { return child_range(); } 792}; 793 794/// \brief A C++ throw-expression (C++ [except.throw]). 795/// 796/// This handles 'throw' (for re-throwing the current exception) and 797/// 'throw' assignment-expression. When assignment-expression isn't 798/// present, Op will be null. 799class CXXThrowExpr : public Expr { 800 Stmt *Op; 801 SourceLocation ThrowLoc; 802 /// \brief Whether the thrown variable (if any) is in scope. 803 unsigned IsThrownVariableInScope : 1; 804 805 friend class ASTStmtReader; 806 807public: 808 // \p Ty is the void type which is used as the result type of the 809 // expression. The \p l is the location of the throw keyword. \p expr 810 // can by null, if the optional expression to throw isn't present. 811 CXXThrowExpr(Expr *expr, QualType Ty, SourceLocation l, 812 bool IsThrownVariableInScope) : 813 Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 814 expr && expr->isInstantiationDependent(), 815 expr && expr->containsUnexpandedParameterPack()), 816 Op(expr), ThrowLoc(l), IsThrownVariableInScope(IsThrownVariableInScope) {} 817 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {} 818 819 const Expr *getSubExpr() const { return cast_or_null<Expr>(Op); } 820 Expr *getSubExpr() { return cast_or_null<Expr>(Op); } 821 822 SourceLocation getThrowLoc() const { return ThrowLoc; } 823 824 /// \brief Determines whether the variable thrown by this expression (if any!) 825 /// is within the innermost try block. 826 /// 827 /// This information is required to determine whether the NRVO can apply to 828 /// this variable. 829 bool isThrownVariableInScope() const { return IsThrownVariableInScope; } 830 831 SourceLocation getLocStart() const LLVM_READONLY { return ThrowLoc; } 832 SourceLocation getLocEnd() const LLVM_READONLY { 833 if (getSubExpr() == 0) 834 return ThrowLoc; 835 return getSubExpr()->getLocEnd(); 836 } 837 838 static bool classof(const Stmt *T) { 839 return T->getStmtClass() == CXXThrowExprClass; 840 } 841 842 // Iterators 843 child_range children() { 844 return child_range(&Op, Op ? &Op+1 : &Op); 845 } 846}; 847 848/// \brief A default argument (C++ [dcl.fct.default]). 849/// 850/// This wraps up a function call argument that was created from the 851/// corresponding parameter's default argument, when the call did not 852/// explicitly supply arguments for all of the parameters. 853class CXXDefaultArgExpr : public Expr { 854 /// \brief The parameter whose default is being used. 855 /// 856 /// When the bit is set, the subexpression is stored after the 857 /// CXXDefaultArgExpr itself. When the bit is clear, the parameter's 858 /// actual default expression is the subexpression. 859 llvm::PointerIntPair<ParmVarDecl *, 1, bool> Param; 860 861 /// \brief The location where the default argument expression was used. 862 SourceLocation Loc; 863 864 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param) 865 : Expr(SC, 866 param->hasUnparsedDefaultArg() 867 ? param->getType().getNonReferenceType() 868 : param->getDefaultArg()->getType(), 869 param->getDefaultArg()->getValueKind(), 870 param->getDefaultArg()->getObjectKind(), false, false, false, false), 871 Param(param, false), Loc(Loc) { } 872 873 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *param, 874 Expr *SubExpr) 875 : Expr(SC, SubExpr->getType(), 876 SubExpr->getValueKind(), SubExpr->getObjectKind(), 877 false, false, false, false), 878 Param(param, true), Loc(Loc) { 879 *reinterpret_cast<Expr **>(this + 1) = SubExpr; 880 } 881 882public: 883 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {} 884 885 // \p Param is the parameter whose default argument is used by this 886 // expression. 887 static CXXDefaultArgExpr *Create(ASTContext &C, SourceLocation Loc, 888 ParmVarDecl *Param) { 889 return new (C) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param); 890 } 891 892 // \p Param is the parameter whose default argument is used by this 893 // expression, and \p SubExpr is the expression that will actually be used. 894 static CXXDefaultArgExpr *Create(ASTContext &C, 895 SourceLocation Loc, 896 ParmVarDecl *Param, 897 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(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(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(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(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 ParenRange; 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(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 ParenRange); 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(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 ParenRange); 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() const { return reinterpret_cast<Expr **>(Args); } 1165 unsigned getNumArgs() const { return NumArgs; } 1166 1167 /// \brief Return the specified argument. 1168 Expr *getArg(unsigned Arg) { 1169 assert(Arg < NumArgs && "Arg access out of range!"); 1170 return cast<Expr>(Args[Arg]); 1171 } 1172 const Expr *getArg(unsigned Arg) const { 1173 assert(Arg < NumArgs && "Arg access out of range!"); 1174 return cast<Expr>(Args[Arg]); 1175 } 1176 1177 /// \brief Set the specified argument. 1178 void setArg(unsigned Arg, Expr *ArgExpr) { 1179 assert(Arg < NumArgs && "Arg access out of range!"); 1180 Args[Arg] = ArgExpr; 1181 } 1182 1183 SourceLocation getLocStart() const LLVM_READONLY; 1184 SourceLocation getLocEnd() const LLVM_READONLY; 1185 SourceRange getParenRange() const { return ParenRange; } 1186 void setParenRange(SourceRange Range) { ParenRange = Range; } 1187 1188 static bool classof(const Stmt *T) { 1189 return T->getStmtClass() == CXXConstructExprClass || 1190 T->getStmtClass() == CXXTemporaryObjectExprClass; 1191 } 1192 1193 // Iterators 1194 child_range children() { 1195 return child_range(&Args[0], &Args[0]+NumArgs); 1196 } 1197 1198 friend class ASTStmtReader; 1199}; 1200 1201/// \brief Represents an explicit C++ type conversion that uses "functional" 1202/// notation (C++ [expr.type.conv]). 1203/// 1204/// Example: 1205/// \code 1206/// x = int(0.5); 1207/// \endcode 1208class CXXFunctionalCastExpr : public ExplicitCastExpr { 1209 SourceLocation LParenLoc; 1210 SourceLocation RParenLoc; 1211 1212 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK, 1213 TypeSourceInfo *writtenTy, 1214 CastKind kind, Expr *castExpr, unsigned pathSize, 1215 SourceLocation lParenLoc, SourceLocation rParenLoc) 1216 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, 1217 castExpr, pathSize, writtenTy), 1218 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {} 1219 1220 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize) 1221 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) { } 1222 1223public: 1224 static CXXFunctionalCastExpr *Create(ASTContext &Context, QualType T, 1225 ExprValueKind VK, 1226 TypeSourceInfo *Written, 1227 CastKind Kind, Expr *Op, 1228 const CXXCastPath *Path, 1229 SourceLocation LPLoc, 1230 SourceLocation RPLoc); 1231 static CXXFunctionalCastExpr *CreateEmpty(ASTContext &Context, 1232 unsigned PathSize); 1233 1234 SourceLocation getLParenLoc() const { return LParenLoc; } 1235 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1236 SourceLocation getRParenLoc() const { return RParenLoc; } 1237 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1238 1239 SourceLocation getLocStart() const LLVM_READONLY; 1240 SourceLocation getLocEnd() const LLVM_READONLY; 1241 1242 static bool classof(const Stmt *T) { 1243 return T->getStmtClass() == CXXFunctionalCastExprClass; 1244 } 1245}; 1246 1247/// @brief Represents a C++ functional cast expression that builds a 1248/// temporary object. 1249/// 1250/// This expression type represents a C++ "functional" cast 1251/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 1252/// constructor to build a temporary object. With N == 1 arguments the 1253/// functional cast expression will be represented by CXXFunctionalCastExpr. 1254/// Example: 1255/// \code 1256/// struct X { X(int, float); } 1257/// 1258/// X create_X() { 1259/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 1260/// }; 1261/// \endcode 1262class CXXTemporaryObjectExpr : public CXXConstructExpr { 1263 TypeSourceInfo *Type; 1264 1265public: 1266 CXXTemporaryObjectExpr(ASTContext &C, CXXConstructorDecl *Cons, 1267 TypeSourceInfo *Type, 1268 ArrayRef<Expr *> Args, 1269 SourceRange parenRange, 1270 bool HadMultipleCandidates, 1271 bool ListInitialization, 1272 bool ZeroInitialization); 1273 explicit CXXTemporaryObjectExpr(EmptyShell Empty) 1274 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty), Type() { } 1275 1276 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 1277 1278 SourceLocation getLocStart() const LLVM_READONLY; 1279 SourceLocation getLocEnd() const LLVM_READONLY; 1280 1281 static bool classof(const Stmt *T) { 1282 return T->getStmtClass() == CXXTemporaryObjectExprClass; 1283 } 1284 1285 friend class ASTStmtReader; 1286}; 1287 1288/// \brief A C++ lambda expression, which produces a function object 1289/// (of unspecified type) that can be invoked later. 1290/// 1291/// Example: 1292/// \code 1293/// void low_pass_filter(std::vector<double> &values, double cutoff) { 1294/// values.erase(std::remove_if(values.begin(), values.end(), 1295/// [=](double value) { return value > cutoff; }); 1296/// } 1297/// \endcode 1298/// 1299/// C++11 lambda expressions can capture local variables, either by copying 1300/// the values of those local variables at the time the function 1301/// object is constructed (not when it is called!) or by holding a 1302/// reference to the local variable. These captures can occur either 1303/// implicitly or can be written explicitly between the square 1304/// brackets ([...]) that start the lambda expression. 1305/// 1306/// C++1y introduces a new form of "capture" called an init-capture that 1307/// includes an initializing expression (rather than capturing a variable), 1308/// and which can never occur implicitly. 1309class LambdaExpr : public Expr { 1310 enum { 1311 /// \brief Flag used by the Capture class to indicate that the given 1312 /// capture was implicit. 1313 Capture_Implicit = 0x01, 1314 1315 /// \brief Flag used by the Capture class to indicate that the 1316 /// given capture was by-copy. 1317 /// 1318 /// This includes the case of a non-reference init-capture. 1319 Capture_ByCopy = 0x02 1320 }; 1321 1322 /// \brief The source range that covers the lambda introducer ([...]). 1323 SourceRange IntroducerRange; 1324 1325 /// \brief The source location of this lambda's capture-default ('=' or '&'). 1326 SourceLocation CaptureDefaultLoc; 1327 1328 /// \brief The number of captures. 1329 unsigned NumCaptures : 16; 1330 1331 /// \brief The default capture kind, which is a value of type 1332 /// LambdaCaptureDefault. 1333 unsigned CaptureDefault : 2; 1334 1335 /// \brief Whether this lambda had an explicit parameter list vs. an 1336 /// implicit (and empty) parameter list. 1337 unsigned ExplicitParams : 1; 1338 1339 /// \brief Whether this lambda had the result type explicitly specified. 1340 unsigned ExplicitResultType : 1; 1341 1342 /// \brief Whether there are any array index variables stored at the end of 1343 /// this lambda expression. 1344 unsigned HasArrayIndexVars : 1; 1345 1346 /// \brief The location of the closing brace ('}') that completes 1347 /// the lambda. 1348 /// 1349 /// The location of the brace is also available by looking up the 1350 /// function call operator in the lambda class. However, it is 1351 /// stored here to improve the performance of getSourceRange(), and 1352 /// to avoid having to deserialize the function call operator from a 1353 /// module file just to determine the source range. 1354 SourceLocation ClosingBrace; 1355 1356 // Note: The capture initializers are stored directly after the lambda 1357 // expression, along with the index variables used to initialize by-copy 1358 // array captures. 1359 1360public: 1361 /// \brief Describes the capture of a variable or of \c this, or of a 1362 /// C++1y init-capture. 1363 class Capture { 1364 llvm::PointerIntPair<Decl *, 2> DeclAndBits; 1365 SourceLocation Loc; 1366 SourceLocation EllipsisLoc; 1367 1368 friend class ASTStmtReader; 1369 friend class ASTStmtWriter; 1370 1371 public: 1372 /// \brief Create a new capture of a variable or of \c this. 1373 /// 1374 /// \param Loc The source location associated with this capture. 1375 /// 1376 /// \param Kind The kind of capture (this, byref, bycopy), which must 1377 /// not be init-capture. 1378 /// 1379 /// \param Implicit Whether the capture was implicit or explicit. 1380 /// 1381 /// \param Var The local variable being captured, or null if capturing 1382 /// \c this. 1383 /// 1384 /// \param EllipsisLoc The location of the ellipsis (...) for a 1385 /// capture that is a pack expansion, or an invalid source 1386 /// location to indicate that this is not a pack expansion. 1387 Capture(SourceLocation Loc, bool Implicit, 1388 LambdaCaptureKind Kind, VarDecl *Var = 0, 1389 SourceLocation EllipsisLoc = SourceLocation()); 1390 1391 /// \brief Create a new init-capture. 1392 Capture(FieldDecl *Field); 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 { return getCaptureKind() == LCK_Init; } 1408 1409 /// \brief Retrieve the declaration of the local variable being 1410 /// captured. 1411 /// 1412 /// This operation is only valid if this capture is a variable capture 1413 /// (other than a capture of \c this). 1414 VarDecl *getCapturedVar() const { 1415 assert(capturesVariable() && "No variable available for 'this' capture"); 1416 return cast<VarDecl>(DeclAndBits.getPointer()); 1417 } 1418 1419 /// \brief Retrieve the field for an init-capture. 1420 /// 1421 /// This works only for an init-capture. To retrieve the FieldDecl for 1422 /// a captured variable or for a capture of \c this, use 1423 /// LambdaExpr::getLambdaClass and CXXRecordDecl::getCaptureFields. 1424 FieldDecl *getInitCaptureField() const { 1425 assert(getCaptureKind() == LCK_Init && "no field for non-init-capture"); 1426 return cast<FieldDecl>(DeclAndBits.getPointer()); 1427 } 1428 1429 /// \brief Determine whether this was an implicit capture (not 1430 /// written between the square brackets introducing the lambda). 1431 bool isImplicit() const { return DeclAndBits.getInt() & Capture_Implicit; } 1432 1433 /// \brief Determine whether this was an explicit capture (written 1434 /// between the square brackets introducing the lambda). 1435 bool isExplicit() const { return !isImplicit(); } 1436 1437 /// \brief Retrieve the source location of the capture. 1438 /// 1439 /// For an explicit capture, this returns the location of the 1440 /// explicit capture in the source. For an implicit capture, this 1441 /// returns the location at which the variable or \c this was first 1442 /// used. 1443 SourceLocation getLocation() const { return Loc; } 1444 1445 /// \brief Determine whether this capture is a pack expansion, 1446 /// which captures a function parameter pack. 1447 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 1448 1449 /// \brief Retrieve the location of the ellipsis for a capture 1450 /// that is a pack expansion. 1451 SourceLocation getEllipsisLoc() const { 1452 assert(isPackExpansion() && "No ellipsis location for a non-expansion"); 1453 return EllipsisLoc; 1454 } 1455 }; 1456 1457private: 1458 /// \brief Construct a lambda expression. 1459 LambdaExpr(QualType T, SourceRange IntroducerRange, 1460 LambdaCaptureDefault CaptureDefault, 1461 SourceLocation CaptureDefaultLoc, 1462 ArrayRef<Capture> Captures, 1463 bool ExplicitParams, 1464 bool ExplicitResultType, 1465 ArrayRef<Expr *> CaptureInits, 1466 ArrayRef<VarDecl *> ArrayIndexVars, 1467 ArrayRef<unsigned> ArrayIndexStarts, 1468 SourceLocation ClosingBrace, 1469 bool ContainsUnexpandedParameterPack); 1470 1471 /// \brief Construct an empty lambda expression. 1472 LambdaExpr(EmptyShell Empty, unsigned NumCaptures, bool HasArrayIndexVars) 1473 : Expr(LambdaExprClass, Empty), 1474 NumCaptures(NumCaptures), CaptureDefault(LCD_None), ExplicitParams(false), 1475 ExplicitResultType(false), HasArrayIndexVars(true) { 1476 getStoredStmts()[NumCaptures] = 0; 1477 } 1478 1479 Stmt **getStoredStmts() const { 1480 return reinterpret_cast<Stmt **>(const_cast<LambdaExpr *>(this) + 1); 1481 } 1482 1483 /// \brief Retrieve the mapping from captures to the first array index 1484 /// variable. 1485 unsigned *getArrayIndexStarts() const { 1486 return reinterpret_cast<unsigned *>(getStoredStmts() + NumCaptures + 1); 1487 } 1488 1489 /// \brief Retrieve the complete set of array-index variables. 1490 VarDecl **getArrayIndexVars() const { 1491 unsigned ArrayIndexSize = 1492 llvm::RoundUpToAlignment(sizeof(unsigned) * (NumCaptures + 1), 1493 llvm::alignOf<VarDecl*>()); 1494 return reinterpret_cast<VarDecl **>( 1495 reinterpret_cast<char*>(getArrayIndexStarts()) + ArrayIndexSize); 1496 } 1497 1498public: 1499 /// \brief Construct a new lambda expression. 1500 static LambdaExpr *Create(ASTContext &C, 1501 CXXRecordDecl *Class, 1502 SourceRange IntroducerRange, 1503 LambdaCaptureDefault CaptureDefault, 1504 SourceLocation CaptureDefaultLoc, 1505 ArrayRef<Capture> Captures, 1506 bool ExplicitParams, 1507 bool ExplicitResultType, 1508 ArrayRef<Expr *> CaptureInits, 1509 ArrayRef<VarDecl *> ArrayIndexVars, 1510 ArrayRef<unsigned> ArrayIndexStarts, 1511 SourceLocation ClosingBrace, 1512 bool ContainsUnexpandedParameterPack); 1513 1514 /// \brief Construct a new lambda expression that will be deserialized from 1515 /// an external source. 1516 static LambdaExpr *CreateDeserialized(ASTContext &C, unsigned NumCaptures, 1517 unsigned NumArrayIndexVars); 1518 1519 /// \brief Determine the default capture kind for this lambda. 1520 LambdaCaptureDefault getCaptureDefault() const { 1521 return static_cast<LambdaCaptureDefault>(CaptureDefault); 1522 } 1523 1524 /// \brief Retrieve the location of this lambda's capture-default, if any. 1525 SourceLocation getCaptureDefaultLoc() const { 1526 return CaptureDefaultLoc; 1527 } 1528 1529 /// \brief An iterator that walks over the captures of the lambda, 1530 /// both implicit and explicit. 1531 typedef const Capture *capture_iterator; 1532 1533 /// \brief Retrieve an iterator pointing to the first lambda capture. 1534 capture_iterator capture_begin() const; 1535 1536 /// \brief Retrieve an iterator pointing past the end of the 1537 /// sequence of lambda captures. 1538 capture_iterator capture_end() const; 1539 1540 /// \brief Determine the number of captures in this lambda. 1541 unsigned capture_size() const { return NumCaptures; } 1542 1543 /// \brief Retrieve an iterator pointing to the first explicit 1544 /// lambda capture. 1545 capture_iterator explicit_capture_begin() const; 1546 1547 /// \brief Retrieve an iterator pointing past the end of the sequence of 1548 /// explicit lambda captures. 1549 capture_iterator explicit_capture_end() const; 1550 1551 /// \brief Retrieve an iterator pointing to the first implicit 1552 /// lambda capture. 1553 capture_iterator implicit_capture_begin() const; 1554 1555 /// \brief Retrieve an iterator pointing past the end of the sequence of 1556 /// implicit lambda captures. 1557 capture_iterator implicit_capture_end() const; 1558 1559 /// \brief Iterator that walks over the capture initialization 1560 /// arguments. 1561 typedef Expr **capture_init_iterator; 1562 1563 /// \brief Retrieve the first initialization argument for this 1564 /// lambda expression (which initializes the first capture field). 1565 capture_init_iterator capture_init_begin() const { 1566 return reinterpret_cast<Expr **>(getStoredStmts()); 1567 } 1568 1569 /// \brief Retrieve the iterator pointing one past the last 1570 /// initialization argument for this lambda expression. 1571 capture_init_iterator capture_init_end() const { 1572 return capture_init_begin() + NumCaptures; 1573 } 1574 1575 /// \brief Retrieve the initializer for an init-capture. 1576 Expr *getInitCaptureInit(capture_iterator Capture) { 1577 assert(Capture >= explicit_capture_begin() && 1578 Capture <= explicit_capture_end() && Capture->isInitCapture()); 1579 return capture_init_begin()[Capture - capture_begin()]; 1580 } 1581 const Expr *getInitCaptureInit(capture_iterator Capture) const { 1582 return const_cast<LambdaExpr*>(this)->getInitCaptureInit(Capture); 1583 } 1584 1585 /// \brief Retrieve the set of index variables used in the capture 1586 /// initializer of an array captured by copy. 1587 /// 1588 /// \param Iter The iterator that points at the capture initializer for 1589 /// which we are extracting the corresponding index variables. 1590 ArrayRef<VarDecl *> getCaptureInitIndexVars(capture_init_iterator Iter) const; 1591 1592 /// \brief Retrieve the source range covering the lambda introducer, 1593 /// which contains the explicit capture list surrounded by square 1594 /// brackets ([...]). 1595 SourceRange getIntroducerRange() const { return IntroducerRange; } 1596 1597 /// \brief Retrieve the class that corresponds to the lambda. 1598 /// 1599 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the 1600 /// captures in its fields and provides the various operations permitted 1601 /// on a lambda (copying, calling). 1602 CXXRecordDecl *getLambdaClass() const; 1603 1604 /// \brief Retrieve the function call operator associated with this 1605 /// lambda expression. 1606 CXXMethodDecl *getCallOperator() const; 1607 1608 /// \brief Retrieve the body of the lambda. 1609 CompoundStmt *getBody() const; 1610 1611 /// \brief Determine whether the lambda is mutable, meaning that any 1612 /// captures values can be modified. 1613 bool isMutable() const; 1614 1615 /// \brief Determine whether this lambda has an explicit parameter 1616 /// list vs. an implicit (empty) parameter list. 1617 bool hasExplicitParameters() const { return ExplicitParams; } 1618 1619 /// \brief Whether this lambda had its result type explicitly specified. 1620 bool hasExplicitResultType() const { return ExplicitResultType; } 1621 1622 static bool classof(const Stmt *T) { 1623 return T->getStmtClass() == LambdaExprClass; 1624 } 1625 1626 SourceLocation getLocStart() const LLVM_READONLY { 1627 return IntroducerRange.getBegin(); 1628 } 1629 SourceLocation getLocEnd() const LLVM_READONLY { return ClosingBrace; } 1630 1631 child_range children() { 1632 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1); 1633 } 1634 1635 friend class ASTStmtReader; 1636 friend class ASTStmtWriter; 1637}; 1638 1639/// An expression "T()" which creates a value-initialized rvalue of type 1640/// T, which is a non-class type. See (C++98 [5.2.3p2]). 1641class CXXScalarValueInitExpr : public Expr { 1642 SourceLocation RParenLoc; 1643 TypeSourceInfo *TypeInfo; 1644 1645 friend class ASTStmtReader; 1646 1647public: 1648 /// \brief Create an explicitly-written scalar-value initialization 1649 /// expression. 1650 CXXScalarValueInitExpr(QualType Type, 1651 TypeSourceInfo *TypeInfo, 1652 SourceLocation rParenLoc ) : 1653 Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, 1654 false, false, Type->isInstantiationDependentType(), false), 1655 RParenLoc(rParenLoc), TypeInfo(TypeInfo) {} 1656 1657 explicit CXXScalarValueInitExpr(EmptyShell Shell) 1658 : Expr(CXXScalarValueInitExprClass, Shell) { } 1659 1660 TypeSourceInfo *getTypeSourceInfo() const { 1661 return TypeInfo; 1662 } 1663 1664 SourceLocation getRParenLoc() const { return RParenLoc; } 1665 1666 SourceLocation getLocStart() const LLVM_READONLY; 1667 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 1668 1669 static bool classof(const Stmt *T) { 1670 return T->getStmtClass() == CXXScalarValueInitExprClass; 1671 } 1672 1673 // Iterators 1674 child_range children() { return child_range(); } 1675}; 1676 1677/// \brief Represents a new-expression for memory allocation and constructor 1678/// calls, e.g: "new CXXNewExpr(foo)". 1679class CXXNewExpr : public Expr { 1680 /// Contains an optional array size expression, an optional initialization 1681 /// expression, and any number of optional placement arguments, in that order. 1682 Stmt **SubExprs; 1683 /// \brief Points to the allocation function used. 1684 FunctionDecl *OperatorNew; 1685 /// \brief Points to the deallocation function used in case of error. May be 1686 /// null. 1687 FunctionDecl *OperatorDelete; 1688 1689 /// \brief The allocated type-source information, as written in the source. 1690 TypeSourceInfo *AllocatedTypeInfo; 1691 1692 /// \brief If the allocated type was expressed as a parenthesized type-id, 1693 /// the source range covering the parenthesized type-id. 1694 SourceRange TypeIdParens; 1695 1696 /// \brief Range of the entire new expression. 1697 SourceRange Range; 1698 1699 /// \brief Source-range of a paren-delimited initializer. 1700 SourceRange DirectInitRange; 1701 1702 /// Was the usage ::new, i.e. is the global new to be used? 1703 bool GlobalNew : 1; 1704 /// Do we allocate an array? If so, the first SubExpr is the size expression. 1705 bool Array : 1; 1706 /// If this is an array allocation, does the usual deallocation 1707 /// function for the allocated type want to know the allocated size? 1708 bool UsualArrayDeleteWantsSize : 1; 1709 /// The number of placement new arguments. 1710 unsigned NumPlacementArgs : 13; 1711 /// What kind of initializer do we have? Could be none, parens, or braces. 1712 /// In storage, we distinguish between "none, and no initializer expr", and 1713 /// "none, but an implicit initializer expr". 1714 unsigned StoredInitializationStyle : 2; 1715 1716 friend class ASTStmtReader; 1717 friend class ASTStmtWriter; 1718public: 1719 enum InitializationStyle { 1720 NoInit, ///< New-expression has no initializer as written. 1721 CallInit, ///< New-expression has a C++98 paren-delimited initializer. 1722 ListInit ///< New-expression has a C++11 list-initializer. 1723 }; 1724 1725 CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew, 1726 FunctionDecl *operatorDelete, bool usualArrayDeleteWantsSize, 1727 ArrayRef<Expr*> placementArgs, 1728 SourceRange typeIdParens, Expr *arraySize, 1729 InitializationStyle initializationStyle, Expr *initializer, 1730 QualType ty, TypeSourceInfo *AllocatedTypeInfo, 1731 SourceRange Range, SourceRange directInitRange); 1732 explicit CXXNewExpr(EmptyShell Shell) 1733 : Expr(CXXNewExprClass, Shell), SubExprs(0) { } 1734 1735 void AllocateArgsArray(ASTContext &C, bool isArray, unsigned numPlaceArgs, 1736 bool hasInitializer); 1737 1738 QualType getAllocatedType() const { 1739 assert(getType()->isPointerType()); 1740 return getType()->getAs<PointerType>()->getPointeeType(); 1741 } 1742 1743 TypeSourceInfo *getAllocatedTypeSourceInfo() const { 1744 return AllocatedTypeInfo; 1745 } 1746 1747 /// \brief True if the allocation result needs to be null-checked. 1748 /// 1749 /// C++11 [expr.new]p13: 1750 /// If the allocation function returns null, initialization shall 1751 /// not be done, the deallocation function shall not be called, 1752 /// and the value of the new-expression shall be null. 1753 /// 1754 /// An allocation function is not allowed to return null unless it 1755 /// has a non-throwing exception-specification. The '03 rule is 1756 /// identical except that the definition of a non-throwing 1757 /// exception specification is just "is it throw()?". 1758 bool shouldNullCheckAllocation(ASTContext &Ctx) const; 1759 1760 FunctionDecl *getOperatorNew() const { return OperatorNew; } 1761 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; } 1762 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1763 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; } 1764 1765 bool isArray() const { return Array; } 1766 Expr *getArraySize() { 1767 return Array ? cast<Expr>(SubExprs[0]) : 0; 1768 } 1769 const Expr *getArraySize() const { 1770 return Array ? cast<Expr>(SubExprs[0]) : 0; 1771 } 1772 1773 unsigned getNumPlacementArgs() const { return NumPlacementArgs; } 1774 Expr **getPlacementArgs() { 1775 return reinterpret_cast<Expr **>(SubExprs + Array + hasInitializer()); 1776 } 1777 1778 Expr *getPlacementArg(unsigned i) { 1779 assert(i < NumPlacementArgs && "Index out of range"); 1780 return getPlacementArgs()[i]; 1781 } 1782 const Expr *getPlacementArg(unsigned i) const { 1783 assert(i < NumPlacementArgs && "Index out of range"); 1784 return const_cast<CXXNewExpr*>(this)->getPlacementArg(i); 1785 } 1786 1787 bool isParenTypeId() const { return TypeIdParens.isValid(); } 1788 SourceRange getTypeIdParens() const { return TypeIdParens; } 1789 1790 bool isGlobalNew() const { return GlobalNew; } 1791 1792 /// \brief Whether this new-expression has any initializer at all. 1793 bool hasInitializer() const { return StoredInitializationStyle > 0; } 1794 1795 /// \brief The kind of initializer this new-expression has. 1796 InitializationStyle getInitializationStyle() const { 1797 if (StoredInitializationStyle == 0) 1798 return NoInit; 1799 return static_cast<InitializationStyle>(StoredInitializationStyle-1); 1800 } 1801 1802 /// \brief The initializer of this new-expression. 1803 Expr *getInitializer() { 1804 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : 0; 1805 } 1806 const Expr *getInitializer() const { 1807 return hasInitializer() ? cast<Expr>(SubExprs[Array]) : 0; 1808 } 1809 1810 /// \brief Returns the CXXConstructExpr from this new-expression, or null. 1811 const CXXConstructExpr* getConstructExpr() const { 1812 return dyn_cast_or_null<CXXConstructExpr>(getInitializer()); 1813 } 1814 1815 /// Answers whether the usual array deallocation function for the 1816 /// allocated type expects the size of the allocation as a 1817 /// parameter. 1818 bool doesUsualArrayDeleteWantSize() const { 1819 return UsualArrayDeleteWantsSize; 1820 } 1821 1822 typedef ExprIterator arg_iterator; 1823 typedef ConstExprIterator const_arg_iterator; 1824 1825 arg_iterator placement_arg_begin() { 1826 return SubExprs + Array + hasInitializer(); 1827 } 1828 arg_iterator placement_arg_end() { 1829 return SubExprs + Array + hasInitializer() + getNumPlacementArgs(); 1830 } 1831 const_arg_iterator placement_arg_begin() const { 1832 return SubExprs + Array + hasInitializer(); 1833 } 1834 const_arg_iterator placement_arg_end() const { 1835 return SubExprs + Array + hasInitializer() + getNumPlacementArgs(); 1836 } 1837 1838 typedef Stmt **raw_arg_iterator; 1839 raw_arg_iterator raw_arg_begin() { return SubExprs; } 1840 raw_arg_iterator raw_arg_end() { 1841 return SubExprs + Array + hasInitializer() + getNumPlacementArgs(); 1842 } 1843 const_arg_iterator raw_arg_begin() const { return SubExprs; } 1844 const_arg_iterator raw_arg_end() const { 1845 return SubExprs + Array + hasInitializer() + getNumPlacementArgs(); 1846 } 1847 1848 SourceLocation getStartLoc() const { return Range.getBegin(); } 1849 SourceLocation getEndLoc() const { return Range.getEnd(); } 1850 1851 SourceRange getDirectInitRange() const { return DirectInitRange; } 1852 1853 SourceRange getSourceRange() const LLVM_READONLY { 1854 return Range; 1855 } 1856 SourceLocation getLocStart() const LLVM_READONLY { return getStartLoc(); } 1857 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); } 1858 1859 static bool classof(const Stmt *T) { 1860 return T->getStmtClass() == CXXNewExprClass; 1861 } 1862 1863 // Iterators 1864 child_range children() { 1865 return child_range(raw_arg_begin(), raw_arg_end()); 1866 } 1867}; 1868 1869/// \brief Represents a \c delete expression for memory deallocation and 1870/// destructor calls, e.g. "delete[] pArray". 1871class CXXDeleteExpr : public Expr { 1872 /// Points to the operator delete overload that is used. Could be a member. 1873 FunctionDecl *OperatorDelete; 1874 /// The pointer expression to be deleted. 1875 Stmt *Argument; 1876 /// Location of the expression. 1877 SourceLocation Loc; 1878 /// Is this a forced global delete, i.e. "::delete"? 1879 bool GlobalDelete : 1; 1880 /// Is this the array form of delete, i.e. "delete[]"? 1881 bool ArrayForm : 1; 1882 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is applied 1883 /// to pointer-to-array type (ArrayFormAsWritten will be false while ArrayForm 1884 /// will be true). 1885 bool ArrayFormAsWritten : 1; 1886 /// Does the usual deallocation function for the element type require 1887 /// a size_t argument? 1888 bool UsualArrayDeleteWantsSize : 1; 1889public: 1890 CXXDeleteExpr(QualType ty, bool globalDelete, bool arrayForm, 1891 bool arrayFormAsWritten, bool usualArrayDeleteWantsSize, 1892 FunctionDecl *operatorDelete, Expr *arg, SourceLocation loc) 1893 : Expr(CXXDeleteExprClass, ty, VK_RValue, OK_Ordinary, false, false, 1894 arg->isInstantiationDependent(), 1895 arg->containsUnexpandedParameterPack()), 1896 OperatorDelete(operatorDelete), Argument(arg), Loc(loc), 1897 GlobalDelete(globalDelete), 1898 ArrayForm(arrayForm), ArrayFormAsWritten(arrayFormAsWritten), 1899 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) { } 1900 explicit CXXDeleteExpr(EmptyShell Shell) 1901 : Expr(CXXDeleteExprClass, Shell), OperatorDelete(0), Argument(0) { } 1902 1903 bool isGlobalDelete() const { return GlobalDelete; } 1904 bool isArrayForm() const { return ArrayForm; } 1905 bool isArrayFormAsWritten() const { return ArrayFormAsWritten; } 1906 1907 /// Answers whether the usual array deallocation function for the 1908 /// allocated type expects the size of the allocation as a 1909 /// parameter. This can be true even if the actual deallocation 1910 /// function that we're using doesn't want a size. 1911 bool doesUsualArrayDeleteWantSize() const { 1912 return UsualArrayDeleteWantsSize; 1913 } 1914 1915 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 1916 1917 Expr *getArgument() { return cast<Expr>(Argument); } 1918 const Expr *getArgument() const { return cast<Expr>(Argument); } 1919 1920 /// \brief Retrieve the type being destroyed. 1921 /// 1922 /// If the type being destroyed is a dependent type which may or may not 1923 /// be a pointer, return an invalid type. 1924 QualType getDestroyedType() const; 1925 1926 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 1927 SourceLocation getLocEnd() const LLVM_READONLY {return Argument->getLocEnd();} 1928 1929 static bool classof(const Stmt *T) { 1930 return T->getStmtClass() == CXXDeleteExprClass; 1931 } 1932 1933 // Iterators 1934 child_range children() { return child_range(&Argument, &Argument+1); } 1935 1936 friend class ASTStmtReader; 1937}; 1938 1939/// \brief Stores the type being destroyed by a pseudo-destructor expression. 1940class PseudoDestructorTypeStorage { 1941 /// \brief Either the type source information or the name of the type, if 1942 /// it couldn't be resolved due to type-dependence. 1943 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 1944 1945 /// \brief The starting source location of the pseudo-destructor type. 1946 SourceLocation Location; 1947 1948public: 1949 PseudoDestructorTypeStorage() { } 1950 1951 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 1952 : Type(II), Location(Loc) { } 1953 1954 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 1955 1956 TypeSourceInfo *getTypeSourceInfo() const { 1957 return Type.dyn_cast<TypeSourceInfo *>(); 1958 } 1959 1960 IdentifierInfo *getIdentifier() const { 1961 return Type.dyn_cast<IdentifierInfo *>(); 1962 } 1963 1964 SourceLocation getLocation() const { return Location; } 1965}; 1966 1967/// \brief Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 1968/// 1969/// A pseudo-destructor is an expression that looks like a member access to a 1970/// destructor of a scalar type, except that scalar types don't have 1971/// destructors. For example: 1972/// 1973/// \code 1974/// typedef int T; 1975/// void f(int *p) { 1976/// p->T::~T(); 1977/// } 1978/// \endcode 1979/// 1980/// Pseudo-destructors typically occur when instantiating templates such as: 1981/// 1982/// \code 1983/// template<typename T> 1984/// void destroy(T* ptr) { 1985/// ptr->T::~T(); 1986/// } 1987/// \endcode 1988/// 1989/// for scalar types. A pseudo-destructor expression has no run-time semantics 1990/// beyond evaluating the base expression. 1991class CXXPseudoDestructorExpr : public Expr { 1992 /// \brief The base expression (that is being destroyed). 1993 Stmt *Base; 1994 1995 /// \brief Whether the operator was an arrow ('->'); otherwise, it was a 1996 /// period ('.'). 1997 bool IsArrow : 1; 1998 1999 /// \brief The location of the '.' or '->' operator. 2000 SourceLocation OperatorLoc; 2001 2002 /// \brief The nested-name-specifier that follows the operator, if present. 2003 NestedNameSpecifierLoc QualifierLoc; 2004 2005 /// \brief The type that precedes the '::' in a qualified pseudo-destructor 2006 /// expression. 2007 TypeSourceInfo *ScopeType; 2008 2009 /// \brief The location of the '::' in a qualified pseudo-destructor 2010 /// expression. 2011 SourceLocation ColonColonLoc; 2012 2013 /// \brief The location of the '~'. 2014 SourceLocation TildeLoc; 2015 2016 /// \brief The type being destroyed, or its name if we were unable to 2017 /// resolve the name. 2018 PseudoDestructorTypeStorage DestroyedType; 2019 2020 friend class ASTStmtReader; 2021 2022public: 2023 CXXPseudoDestructorExpr(ASTContext &Context, 2024 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 2025 NestedNameSpecifierLoc QualifierLoc, 2026 TypeSourceInfo *ScopeType, 2027 SourceLocation ColonColonLoc, 2028 SourceLocation TildeLoc, 2029 PseudoDestructorTypeStorage DestroyedType); 2030 2031 explicit CXXPseudoDestructorExpr(EmptyShell Shell) 2032 : Expr(CXXPseudoDestructorExprClass, Shell), 2033 Base(0), IsArrow(false), QualifierLoc(), ScopeType(0) { } 2034 2035 Expr *getBase() const { return cast<Expr>(Base); } 2036 2037 /// \brief Determines whether this member expression actually had 2038 /// a C++ nested-name-specifier prior to the name of the member, e.g., 2039 /// x->Base::foo. 2040 bool hasQualifier() const { return QualifierLoc.hasQualifier(); } 2041 2042 /// \brief Retrieves the nested-name-specifier that qualifies the type name, 2043 /// with source-location information. 2044 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2045 2046 /// \brief If the member name was qualified, retrieves the 2047 /// nested-name-specifier that precedes the member name. Otherwise, returns 2048 /// null. 2049 NestedNameSpecifier *getQualifier() const { 2050 return QualifierLoc.getNestedNameSpecifier(); 2051 } 2052 2053 /// \brief Determine whether this pseudo-destructor expression was written 2054 /// using an '->' (otherwise, it used a '.'). 2055 bool isArrow() const { return IsArrow; } 2056 2057 /// \brief Retrieve the location of the '.' or '->' operator. 2058 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2059 2060 /// \brief Retrieve the scope type in a qualified pseudo-destructor 2061 /// expression. 2062 /// 2063 /// Pseudo-destructor expressions can have extra qualification within them 2064 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 2065 /// Here, if the object type of the expression is (or may be) a scalar type, 2066 /// \p T may also be a scalar type and, therefore, cannot be part of a 2067 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 2068 /// destructor expression. 2069 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 2070 2071 /// \brief Retrieve the location of the '::' in a qualified pseudo-destructor 2072 /// expression. 2073 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 2074 2075 /// \brief Retrieve the location of the '~'. 2076 SourceLocation getTildeLoc() const { return TildeLoc; } 2077 2078 /// \brief Retrieve the source location information for the type 2079 /// being destroyed. 2080 /// 2081 /// This type-source information is available for non-dependent 2082 /// pseudo-destructor expressions and some dependent pseudo-destructor 2083 /// expressions. Returns null if we only have the identifier for a 2084 /// dependent pseudo-destructor expression. 2085 TypeSourceInfo *getDestroyedTypeInfo() const { 2086 return DestroyedType.getTypeSourceInfo(); 2087 } 2088 2089 /// \brief In a dependent pseudo-destructor expression for which we do not 2090 /// have full type information on the destroyed type, provides the name 2091 /// of the destroyed type. 2092 IdentifierInfo *getDestroyedTypeIdentifier() const { 2093 return DestroyedType.getIdentifier(); 2094 } 2095 2096 /// \brief Retrieve the type being destroyed. 2097 QualType getDestroyedType() const; 2098 2099 /// \brief Retrieve the starting location of the type being destroyed. 2100 SourceLocation getDestroyedTypeLoc() const { 2101 return DestroyedType.getLocation(); 2102 } 2103 2104 /// \brief Set the name of destroyed type for a dependent pseudo-destructor 2105 /// expression. 2106 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) { 2107 DestroyedType = PseudoDestructorTypeStorage(II, Loc); 2108 } 2109 2110 /// \brief Set the destroyed type. 2111 void setDestroyedType(TypeSourceInfo *Info) { 2112 DestroyedType = PseudoDestructorTypeStorage(Info); 2113 } 2114 2115 SourceLocation getLocStart() const LLVM_READONLY {return Base->getLocStart();} 2116 SourceLocation getLocEnd() const LLVM_READONLY; 2117 2118 static bool classof(const Stmt *T) { 2119 return T->getStmtClass() == CXXPseudoDestructorExprClass; 2120 } 2121 2122 // Iterators 2123 child_range children() { return child_range(&Base, &Base + 1); } 2124}; 2125 2126/// \brief Represents a GCC or MS unary type trait, as used in the 2127/// implementation of TR1/C++11 type trait templates. 2128/// 2129/// Example: 2130/// \code 2131/// __is_pod(int) == true 2132/// __is_enum(std::string) == false 2133/// \endcode 2134class UnaryTypeTraitExpr : public Expr { 2135 /// \brief The trait. A UnaryTypeTrait enum in MSVC compatible unsigned. 2136 unsigned UTT : 31; 2137 /// The value of the type trait. Unspecified if dependent. 2138 bool Value : 1; 2139 2140 /// \brief The location of the type trait keyword. 2141 SourceLocation Loc; 2142 2143 /// \brief The location of the closing paren. 2144 SourceLocation RParen; 2145 2146 /// \brief The type being queried. 2147 TypeSourceInfo *QueriedType; 2148 2149public: 2150 UnaryTypeTraitExpr(SourceLocation loc, UnaryTypeTrait utt, 2151 TypeSourceInfo *queried, bool value, 2152 SourceLocation rparen, QualType ty) 2153 : Expr(UnaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 2154 false, queried->getType()->isDependentType(), 2155 queried->getType()->isInstantiationDependentType(), 2156 queried->getType()->containsUnexpandedParameterPack()), 2157 UTT(utt), Value(value), Loc(loc), RParen(rparen), QueriedType(queried) { } 2158 2159 explicit UnaryTypeTraitExpr(EmptyShell Empty) 2160 : Expr(UnaryTypeTraitExprClass, Empty), UTT(0), Value(false), 2161 QueriedType() { } 2162 2163 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 2164 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; } 2165 2166 UnaryTypeTrait getTrait() const { return static_cast<UnaryTypeTrait>(UTT); } 2167 2168 QualType getQueriedType() const { return QueriedType->getType(); } 2169 2170 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 2171 2172 bool getValue() const { return Value; } 2173 2174 static bool classof(const Stmt *T) { 2175 return T->getStmtClass() == UnaryTypeTraitExprClass; 2176 } 2177 2178 // Iterators 2179 child_range children() { return child_range(); } 2180 2181 friend class ASTStmtReader; 2182}; 2183 2184/// \brief Represents a GCC or MS binary type trait, as used in the 2185/// implementation of TR1/C++11 type trait templates. 2186/// 2187/// Example: 2188/// \code 2189/// __is_base_of(Base, Derived) == true 2190/// \endcode 2191class BinaryTypeTraitExpr : public Expr { 2192 /// \brief The trait. A BinaryTypeTrait enum in MSVC compatible unsigned. 2193 unsigned BTT : 8; 2194 2195 /// The value of the type trait. Unspecified if dependent. 2196 bool Value : 1; 2197 2198 /// \brief The location of the type trait keyword. 2199 SourceLocation Loc; 2200 2201 /// \brief The location of the closing paren. 2202 SourceLocation RParen; 2203 2204 /// \brief The lhs type being queried. 2205 TypeSourceInfo *LhsType; 2206 2207 /// \brief The rhs type being queried. 2208 TypeSourceInfo *RhsType; 2209 2210public: 2211 BinaryTypeTraitExpr(SourceLocation loc, BinaryTypeTrait btt, 2212 TypeSourceInfo *lhsType, TypeSourceInfo *rhsType, 2213 bool value, SourceLocation rparen, QualType ty) 2214 : Expr(BinaryTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, false, 2215 lhsType->getType()->isDependentType() || 2216 rhsType->getType()->isDependentType(), 2217 (lhsType->getType()->isInstantiationDependentType() || 2218 rhsType->getType()->isInstantiationDependentType()), 2219 (lhsType->getType()->containsUnexpandedParameterPack() || 2220 rhsType->getType()->containsUnexpandedParameterPack())), 2221 BTT(btt), Value(value), Loc(loc), RParen(rparen), 2222 LhsType(lhsType), RhsType(rhsType) { } 2223 2224 2225 explicit BinaryTypeTraitExpr(EmptyShell Empty) 2226 : Expr(BinaryTypeTraitExprClass, Empty), BTT(0), Value(false), 2227 LhsType(), RhsType() { } 2228 2229 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 2230 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; } 2231 2232 BinaryTypeTrait getTrait() const { 2233 return static_cast<BinaryTypeTrait>(BTT); 2234 } 2235 2236 QualType getLhsType() const { return LhsType->getType(); } 2237 QualType getRhsType() const { return RhsType->getType(); } 2238 2239 TypeSourceInfo *getLhsTypeSourceInfo() const { return LhsType; } 2240 TypeSourceInfo *getRhsTypeSourceInfo() const { return RhsType; } 2241 2242 bool getValue() const { assert(!isTypeDependent()); return Value; } 2243 2244 static bool classof(const Stmt *T) { 2245 return T->getStmtClass() == BinaryTypeTraitExprClass; 2246 } 2247 2248 // Iterators 2249 child_range children() { return child_range(); } 2250 2251 friend class ASTStmtReader; 2252}; 2253 2254/// \brief A type trait used in the implementation of various C++11 and 2255/// Library TR1 trait templates. 2256/// 2257/// \code 2258/// __is_trivially_constructible(vector<int>, int*, int*) 2259/// \endcode 2260class TypeTraitExpr : public Expr { 2261 /// \brief The location of the type trait keyword. 2262 SourceLocation Loc; 2263 2264 /// \brief The location of the closing parenthesis. 2265 SourceLocation RParenLoc; 2266 2267 // Note: The TypeSourceInfos for the arguments are allocated after the 2268 // TypeTraitExpr. 2269 2270 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, 2271 ArrayRef<TypeSourceInfo *> Args, 2272 SourceLocation RParenLoc, 2273 bool Value); 2274 2275 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) { } 2276 2277 /// \brief Retrieve the argument types. 2278 TypeSourceInfo **getTypeSourceInfos() { 2279 return reinterpret_cast<TypeSourceInfo **>(this+1); 2280 } 2281 2282 /// \brief Retrieve the argument types. 2283 TypeSourceInfo * const *getTypeSourceInfos() const { 2284 return reinterpret_cast<TypeSourceInfo * const*>(this+1); 2285 } 2286 2287public: 2288 /// \brief Create a new type trait expression. 2289 static TypeTraitExpr *Create(ASTContext &C, QualType T, SourceLocation Loc, 2290 TypeTrait Kind, 2291 ArrayRef<TypeSourceInfo *> Args, 2292 SourceLocation RParenLoc, 2293 bool Value); 2294 2295 static TypeTraitExpr *CreateDeserialized(ASTContext &C, unsigned NumArgs); 2296 2297 /// \brief Determine which type trait this expression uses. 2298 TypeTrait getTrait() const { 2299 return static_cast<TypeTrait>(TypeTraitExprBits.Kind); 2300 } 2301 2302 bool getValue() const { 2303 assert(!isValueDependent()); 2304 return TypeTraitExprBits.Value; 2305 } 2306 2307 /// \brief Determine the number of arguments to this type trait. 2308 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; } 2309 2310 /// \brief Retrieve the Ith argument. 2311 TypeSourceInfo *getArg(unsigned I) const { 2312 assert(I < getNumArgs() && "Argument out-of-range"); 2313 return getArgs()[I]; 2314 } 2315 2316 /// \brief Retrieve the argument types. 2317 ArrayRef<TypeSourceInfo *> getArgs() const { 2318 return ArrayRef<TypeSourceInfo *>(getTypeSourceInfos(), getNumArgs()); 2319 } 2320 2321 typedef TypeSourceInfo **arg_iterator; 2322 arg_iterator arg_begin() { 2323 return getTypeSourceInfos(); 2324 } 2325 arg_iterator arg_end() { 2326 return getTypeSourceInfos() + getNumArgs(); 2327 } 2328 2329 typedef TypeSourceInfo const * const *arg_const_iterator; 2330 arg_const_iterator arg_begin() const { return getTypeSourceInfos(); } 2331 arg_const_iterator arg_end() const { 2332 return getTypeSourceInfos() + getNumArgs(); 2333 } 2334 2335 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 2336 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 2337 2338 static bool classof(const Stmt *T) { 2339 return T->getStmtClass() == TypeTraitExprClass; 2340 } 2341 2342 // Iterators 2343 child_range children() { return child_range(); } 2344 2345 friend class ASTStmtReader; 2346 friend class ASTStmtWriter; 2347 2348}; 2349 2350/// \brief An Embarcadero array type trait, as used in the implementation of 2351/// __array_rank and __array_extent. 2352/// 2353/// Example: 2354/// \code 2355/// __array_rank(int[10][20]) == 2 2356/// __array_extent(int, 1) == 20 2357/// \endcode 2358class ArrayTypeTraitExpr : public Expr { 2359 virtual void anchor(); 2360 2361 /// \brief The trait. An ArrayTypeTrait enum in MSVC compat unsigned. 2362 unsigned ATT : 2; 2363 2364 /// \brief The value of the type trait. Unspecified if dependent. 2365 uint64_t Value; 2366 2367 /// \brief The array dimension being queried, or -1 if not used. 2368 Expr *Dimension; 2369 2370 /// \brief The location of the type trait keyword. 2371 SourceLocation Loc; 2372 2373 /// \brief The location of the closing paren. 2374 SourceLocation RParen; 2375 2376 /// \brief The type being queried. 2377 TypeSourceInfo *QueriedType; 2378 2379public: 2380 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att, 2381 TypeSourceInfo *queried, uint64_t value, 2382 Expr *dimension, SourceLocation rparen, QualType ty) 2383 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 2384 false, queried->getType()->isDependentType(), 2385 (queried->getType()->isInstantiationDependentType() || 2386 (dimension && dimension->isInstantiationDependent())), 2387 queried->getType()->containsUnexpandedParameterPack()), 2388 ATT(att), Value(value), Dimension(dimension), 2389 Loc(loc), RParen(rparen), QueriedType(queried) { } 2390 2391 2392 explicit ArrayTypeTraitExpr(EmptyShell Empty) 2393 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0), Value(false), 2394 QueriedType() { } 2395 2396 virtual ~ArrayTypeTraitExpr() { } 2397 2398 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 2399 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; } 2400 2401 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); } 2402 2403 QualType getQueriedType() const { return QueriedType->getType(); } 2404 2405 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 2406 2407 uint64_t getValue() const { assert(!isTypeDependent()); return Value; } 2408 2409 Expr *getDimensionExpression() const { return Dimension; } 2410 2411 static bool classof(const Stmt *T) { 2412 return T->getStmtClass() == ArrayTypeTraitExprClass; 2413 } 2414 2415 // Iterators 2416 child_range children() { return child_range(); } 2417 2418 friend class ASTStmtReader; 2419}; 2420 2421/// \brief An expression trait intrinsic. 2422/// 2423/// Example: 2424/// \code 2425/// __is_lvalue_expr(std::cout) == true 2426/// __is_lvalue_expr(1) == false 2427/// \endcode 2428class ExpressionTraitExpr : public Expr { 2429 /// \brief The trait. A ExpressionTrait enum in MSVC compatible unsigned. 2430 unsigned ET : 31; 2431 /// \brief The value of the type trait. Unspecified if dependent. 2432 bool Value : 1; 2433 2434 /// \brief The location of the type trait keyword. 2435 SourceLocation Loc; 2436 2437 /// \brief The location of the closing paren. 2438 SourceLocation RParen; 2439 2440 /// \brief The expression being queried. 2441 Expr* QueriedExpression; 2442public: 2443 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, 2444 Expr *queried, bool value, 2445 SourceLocation rparen, QualType resultType) 2446 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary, 2447 false, // Not type-dependent 2448 // Value-dependent if the argument is type-dependent. 2449 queried->isTypeDependent(), 2450 queried->isInstantiationDependent(), 2451 queried->containsUnexpandedParameterPack()), 2452 ET(et), Value(value), Loc(loc), RParen(rparen), 2453 QueriedExpression(queried) { } 2454 2455 explicit ExpressionTraitExpr(EmptyShell Empty) 2456 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false), 2457 QueriedExpression() { } 2458 2459 SourceLocation getLocStart() const LLVM_READONLY { return Loc; } 2460 SourceLocation getLocEnd() const LLVM_READONLY { return RParen; } 2461 2462 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); } 2463 2464 Expr *getQueriedExpression() const { return QueriedExpression; } 2465 2466 bool getValue() const { return Value; } 2467 2468 static bool classof(const Stmt *T) { 2469 return T->getStmtClass() == ExpressionTraitExprClass; 2470 } 2471 2472 // Iterators 2473 child_range children() { return child_range(); } 2474 2475 friend class ASTStmtReader; 2476}; 2477 2478 2479/// \brief A reference to an overloaded function set, either an 2480/// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr. 2481class OverloadExpr : public Expr { 2482 /// \brief The common name of these declarations. 2483 DeclarationNameInfo NameInfo; 2484 2485 /// \brief The nested-name-specifier that qualifies the name, if any. 2486 NestedNameSpecifierLoc QualifierLoc; 2487 2488 /// The results. These are undesugared, which is to say, they may 2489 /// include UsingShadowDecls. Access is relative to the naming 2490 /// class. 2491 // FIXME: Allocate this data after the OverloadExpr subclass. 2492 DeclAccessPair *Results; 2493 unsigned NumResults; 2494 2495protected: 2496 /// \brief Whether the name includes info for explicit template 2497 /// keyword and arguments. 2498 bool HasTemplateKWAndArgsInfo; 2499 2500 /// \brief Return the optional template keyword and arguments info. 2501 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo(); // defined far below. 2502 2503 /// \brief Return the optional template keyword and arguments info. 2504 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2505 return const_cast<OverloadExpr*>(this)->getTemplateKWAndArgsInfo(); 2506 } 2507 2508 OverloadExpr(StmtClass K, ASTContext &C, 2509 NestedNameSpecifierLoc QualifierLoc, 2510 SourceLocation TemplateKWLoc, 2511 const DeclarationNameInfo &NameInfo, 2512 const TemplateArgumentListInfo *TemplateArgs, 2513 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2514 bool KnownDependent, 2515 bool KnownInstantiationDependent, 2516 bool KnownContainsUnexpandedParameterPack); 2517 2518 OverloadExpr(StmtClass K, EmptyShell Empty) 2519 : Expr(K, Empty), QualifierLoc(), Results(0), NumResults(0), 2520 HasTemplateKWAndArgsInfo(false) { } 2521 2522 void initializeResults(ASTContext &C, 2523 UnresolvedSetIterator Begin, 2524 UnresolvedSetIterator End); 2525 2526public: 2527 struct FindResult { 2528 OverloadExpr *Expression; 2529 bool IsAddressOfOperand; 2530 bool HasFormOfMemberPointer; 2531 }; 2532 2533 /// \brief Finds the overloaded expression in the given expression \p E of 2534 /// OverloadTy. 2535 /// 2536 /// \return the expression (which must be there) and true if it has 2537 /// the particular form of a member pointer expression 2538 static FindResult find(Expr *E) { 2539 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 2540 2541 FindResult Result; 2542 2543 E = E->IgnoreParens(); 2544 if (isa<UnaryOperator>(E)) { 2545 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 2546 E = cast<UnaryOperator>(E)->getSubExpr(); 2547 OverloadExpr *Ovl = cast<OverloadExpr>(E->IgnoreParens()); 2548 2549 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier()); 2550 Result.IsAddressOfOperand = true; 2551 Result.Expression = Ovl; 2552 } else { 2553 Result.HasFormOfMemberPointer = false; 2554 Result.IsAddressOfOperand = false; 2555 Result.Expression = cast<OverloadExpr>(E); 2556 } 2557 2558 return Result; 2559 } 2560 2561 /// \brief Gets the naming class of this lookup, if any. 2562 CXXRecordDecl *getNamingClass() const; 2563 2564 typedef UnresolvedSetImpl::iterator decls_iterator; 2565 decls_iterator decls_begin() const { return UnresolvedSetIterator(Results); } 2566 decls_iterator decls_end() const { 2567 return UnresolvedSetIterator(Results + NumResults); 2568 } 2569 2570 /// \brief Gets the number of declarations in the unresolved set. 2571 unsigned getNumDecls() const { return NumResults; } 2572 2573 /// \brief Gets the full name info. 2574 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2575 2576 /// \brief Gets the name looked up. 2577 DeclarationName getName() const { return NameInfo.getName(); } 2578 2579 /// \brief Gets the location of the name. 2580 SourceLocation getNameLoc() const { return NameInfo.getLoc(); } 2581 2582 /// \brief Fetches the nested-name qualifier, if one was given. 2583 NestedNameSpecifier *getQualifier() const { 2584 return QualifierLoc.getNestedNameSpecifier(); 2585 } 2586 2587 /// \brief Fetches the nested-name qualifier with source-location 2588 /// information, if one was given. 2589 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2590 2591 /// \brief Retrieve the location of the template keyword preceding 2592 /// this name, if any. 2593 SourceLocation getTemplateKeywordLoc() const { 2594 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2595 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2596 } 2597 2598 /// \brief Retrieve the location of the left angle bracket starting the 2599 /// explicit template argument list following the name, if any. 2600 SourceLocation getLAngleLoc() const { 2601 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2602 return getTemplateKWAndArgsInfo()->LAngleLoc; 2603 } 2604 2605 /// \brief Retrieve the location of the right angle bracket ending the 2606 /// explicit template argument list following the name, if any. 2607 SourceLocation getRAngleLoc() const { 2608 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2609 return getTemplateKWAndArgsInfo()->RAngleLoc; 2610 } 2611 2612 /// \brief Determines whether the name was preceded by the template keyword. 2613 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2614 2615 /// \brief Determines whether this expression had explicit template arguments. 2616 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2617 2618 // Note that, inconsistently with the explicit-template-argument AST 2619 // nodes, users are *forbidden* from calling these methods on objects 2620 // without explicit template arguments. 2621 2622 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2623 assert(hasExplicitTemplateArgs()); 2624 return *getTemplateKWAndArgsInfo(); 2625 } 2626 2627 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2628 return const_cast<OverloadExpr*>(this)->getExplicitTemplateArgs(); 2629 } 2630 2631 TemplateArgumentLoc const *getTemplateArgs() const { 2632 return getExplicitTemplateArgs().getTemplateArgs(); 2633 } 2634 2635 unsigned getNumTemplateArgs() const { 2636 return getExplicitTemplateArgs().NumTemplateArgs; 2637 } 2638 2639 /// \brief Copies the template arguments into the given structure. 2640 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2641 getExplicitTemplateArgs().copyInto(List); 2642 } 2643 2644 /// \brief Retrieves the optional explicit template arguments. 2645 /// 2646 /// This points to the same data as getExplicitTemplateArgs(), but 2647 /// returns null if there are no explicit template arguments. 2648 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 2649 if (!hasExplicitTemplateArgs()) return 0; 2650 return &getExplicitTemplateArgs(); 2651 } 2652 2653 static bool classof(const Stmt *T) { 2654 return T->getStmtClass() == UnresolvedLookupExprClass || 2655 T->getStmtClass() == UnresolvedMemberExprClass; 2656 } 2657 2658 friend class ASTStmtReader; 2659 friend class ASTStmtWriter; 2660}; 2661 2662/// \brief A reference to a name which we were able to look up during 2663/// parsing but could not resolve to a specific declaration. 2664/// 2665/// This arises in several ways: 2666/// * we might be waiting for argument-dependent lookup; 2667/// * the name might resolve to an overloaded function; 2668/// and eventually: 2669/// * the lookup might have included a function template. 2670/// 2671/// These never include UnresolvedUsingValueDecls, which are always class 2672/// members and therefore appear only in UnresolvedMemberLookupExprs. 2673class UnresolvedLookupExpr : public OverloadExpr { 2674 /// True if these lookup results should be extended by 2675 /// argument-dependent lookup if this is the operand of a function 2676 /// call. 2677 bool RequiresADL; 2678 2679 /// True if these lookup results are overloaded. This is pretty 2680 /// trivially rederivable if we urgently need to kill this field. 2681 bool Overloaded; 2682 2683 /// The naming class (C++ [class.access.base]p5) of the lookup, if 2684 /// any. This can generally be recalculated from the context chain, 2685 /// but that can be fairly expensive for unqualified lookups. If we 2686 /// want to improve memory use here, this could go in a union 2687 /// against the qualified-lookup bits. 2688 CXXRecordDecl *NamingClass; 2689 2690 UnresolvedLookupExpr(ASTContext &C, 2691 CXXRecordDecl *NamingClass, 2692 NestedNameSpecifierLoc QualifierLoc, 2693 SourceLocation TemplateKWLoc, 2694 const DeclarationNameInfo &NameInfo, 2695 bool RequiresADL, bool Overloaded, 2696 const TemplateArgumentListInfo *TemplateArgs, 2697 UnresolvedSetIterator Begin, UnresolvedSetIterator End) 2698 : OverloadExpr(UnresolvedLookupExprClass, C, QualifierLoc, TemplateKWLoc, 2699 NameInfo, TemplateArgs, Begin, End, false, false, false), 2700 RequiresADL(RequiresADL), 2701 Overloaded(Overloaded), NamingClass(NamingClass) 2702 {} 2703 2704 UnresolvedLookupExpr(EmptyShell Empty) 2705 : OverloadExpr(UnresolvedLookupExprClass, Empty), 2706 RequiresADL(false), Overloaded(false), NamingClass(0) 2707 {} 2708 2709 friend class ASTStmtReader; 2710 2711public: 2712 static UnresolvedLookupExpr *Create(ASTContext &C, 2713 CXXRecordDecl *NamingClass, 2714 NestedNameSpecifierLoc QualifierLoc, 2715 const DeclarationNameInfo &NameInfo, 2716 bool ADL, bool Overloaded, 2717 UnresolvedSetIterator Begin, 2718 UnresolvedSetIterator End) { 2719 return new(C) UnresolvedLookupExpr(C, NamingClass, QualifierLoc, 2720 SourceLocation(), NameInfo, 2721 ADL, Overloaded, 0, Begin, End); 2722 } 2723 2724 static UnresolvedLookupExpr *Create(ASTContext &C, 2725 CXXRecordDecl *NamingClass, 2726 NestedNameSpecifierLoc QualifierLoc, 2727 SourceLocation TemplateKWLoc, 2728 const DeclarationNameInfo &NameInfo, 2729 bool ADL, 2730 const TemplateArgumentListInfo *Args, 2731 UnresolvedSetIterator Begin, 2732 UnresolvedSetIterator End); 2733 2734 static UnresolvedLookupExpr *CreateEmpty(ASTContext &C, 2735 bool HasTemplateKWAndArgsInfo, 2736 unsigned NumTemplateArgs); 2737 2738 /// True if this declaration should be extended by 2739 /// argument-dependent lookup. 2740 bool requiresADL() const { return RequiresADL; } 2741 2742 /// True if this lookup is overloaded. 2743 bool isOverloaded() const { return Overloaded; } 2744 2745 /// Gets the 'naming class' (in the sense of C++0x 2746 /// [class.access.base]p5) of the lookup. This is the scope 2747 /// that was looked in to find these results. 2748 CXXRecordDecl *getNamingClass() const { return NamingClass; } 2749 2750 SourceLocation getLocStart() const LLVM_READONLY { 2751 if (NestedNameSpecifierLoc l = getQualifierLoc()) 2752 return l.getBeginLoc(); 2753 return getNameInfo().getLocStart(); 2754 } 2755 SourceLocation getLocEnd() const LLVM_READONLY { 2756 if (hasExplicitTemplateArgs()) 2757 return getRAngleLoc(); 2758 return getNameInfo().getLocEnd(); 2759 } 2760 2761 child_range children() { return child_range(); } 2762 2763 static bool classof(const Stmt *T) { 2764 return T->getStmtClass() == UnresolvedLookupExprClass; 2765 } 2766}; 2767 2768/// \brief A qualified reference to a name whose declaration cannot 2769/// yet be resolved. 2770/// 2771/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 2772/// it expresses a reference to a declaration such as 2773/// X<T>::value. The difference, however, is that an 2774/// DependentScopeDeclRefExpr node is used only within C++ templates when 2775/// the qualification (e.g., X<T>::) refers to a dependent type. In 2776/// this case, X<T>::value cannot resolve to a declaration because the 2777/// declaration will differ from on instantiation of X<T> to the 2778/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 2779/// qualifier (X<T>::) and the name of the entity being referenced 2780/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 2781/// declaration can be found. 2782class DependentScopeDeclRefExpr : public Expr { 2783 /// \brief The nested-name-specifier that qualifies this unresolved 2784 /// declaration name. 2785 NestedNameSpecifierLoc QualifierLoc; 2786 2787 /// \brief The name of the entity we will be referencing. 2788 DeclarationNameInfo NameInfo; 2789 2790 /// \brief Whether the name includes info for explicit template 2791 /// keyword and arguments. 2792 bool HasTemplateKWAndArgsInfo; 2793 2794 /// \brief Return the optional template keyword and arguments info. 2795 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 2796 if (!HasTemplateKWAndArgsInfo) return 0; 2797 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 2798 } 2799 /// \brief Return the optional template keyword and arguments info. 2800 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 2801 return const_cast<DependentScopeDeclRefExpr*>(this) 2802 ->getTemplateKWAndArgsInfo(); 2803 } 2804 2805 DependentScopeDeclRefExpr(QualType T, 2806 NestedNameSpecifierLoc QualifierLoc, 2807 SourceLocation TemplateKWLoc, 2808 const DeclarationNameInfo &NameInfo, 2809 const TemplateArgumentListInfo *Args); 2810 2811public: 2812 static DependentScopeDeclRefExpr *Create(ASTContext &C, 2813 NestedNameSpecifierLoc QualifierLoc, 2814 SourceLocation TemplateKWLoc, 2815 const DeclarationNameInfo &NameInfo, 2816 const TemplateArgumentListInfo *TemplateArgs); 2817 2818 static DependentScopeDeclRefExpr *CreateEmpty(ASTContext &C, 2819 bool HasTemplateKWAndArgsInfo, 2820 unsigned NumTemplateArgs); 2821 2822 /// \brief Retrieve the name that this expression refers to. 2823 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2824 2825 /// \brief Retrieve the name that this expression refers to. 2826 DeclarationName getDeclName() const { return NameInfo.getName(); } 2827 2828 /// \brief Retrieve the location of the name within the expression. 2829 SourceLocation getLocation() const { return NameInfo.getLoc(); } 2830 2831 /// \brief Retrieve the nested-name-specifier that qualifies the 2832 /// name, with source location information. 2833 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2834 2835 2836 /// \brief Retrieve the nested-name-specifier that qualifies this 2837 /// declaration. 2838 NestedNameSpecifier *getQualifier() const { 2839 return QualifierLoc.getNestedNameSpecifier(); 2840 } 2841 2842 /// \brief Retrieve the location of the template keyword preceding 2843 /// this name, if any. 2844 SourceLocation getTemplateKeywordLoc() const { 2845 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2846 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 2847 } 2848 2849 /// \brief Retrieve the location of the left angle bracket starting the 2850 /// explicit template argument list following the name, if any. 2851 SourceLocation getLAngleLoc() const { 2852 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2853 return getTemplateKWAndArgsInfo()->LAngleLoc; 2854 } 2855 2856 /// \brief Retrieve the location of the right angle bracket ending the 2857 /// explicit template argument list following the name, if any. 2858 SourceLocation getRAngleLoc() const { 2859 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 2860 return getTemplateKWAndArgsInfo()->RAngleLoc; 2861 } 2862 2863 /// Determines whether the name was preceded by the template keyword. 2864 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2865 2866 /// Determines whether this lookup had explicit template arguments. 2867 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2868 2869 // Note that, inconsistently with the explicit-template-argument AST 2870 // nodes, users are *forbidden* from calling these methods on objects 2871 // without explicit template arguments. 2872 2873 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 2874 assert(hasExplicitTemplateArgs()); 2875 return *reinterpret_cast<ASTTemplateArgumentListInfo*>(this + 1); 2876 } 2877 2878 /// Gets a reference to the explicit template argument list. 2879 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 2880 assert(hasExplicitTemplateArgs()); 2881 return *reinterpret_cast<const ASTTemplateArgumentListInfo*>(this + 1); 2882 } 2883 2884 /// \brief Retrieves the optional explicit template arguments. 2885 /// 2886 /// This points to the same data as getExplicitTemplateArgs(), but 2887 /// returns null if there are no explicit template arguments. 2888 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 2889 if (!hasExplicitTemplateArgs()) return 0; 2890 return &getExplicitTemplateArgs(); 2891 } 2892 2893 /// \brief Copies the template arguments (if present) into the given 2894 /// structure. 2895 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 2896 getExplicitTemplateArgs().copyInto(List); 2897 } 2898 2899 TemplateArgumentLoc const *getTemplateArgs() const { 2900 return getExplicitTemplateArgs().getTemplateArgs(); 2901 } 2902 2903 unsigned getNumTemplateArgs() const { 2904 return getExplicitTemplateArgs().NumTemplateArgs; 2905 } 2906 2907 SourceLocation getLocStart() const LLVM_READONLY { 2908 return QualifierLoc.getBeginLoc(); 2909 } 2910 SourceLocation getLocEnd() const LLVM_READONLY { 2911 if (hasExplicitTemplateArgs()) 2912 return getRAngleLoc(); 2913 return getLocation(); 2914 } 2915 2916 static bool classof(const Stmt *T) { 2917 return T->getStmtClass() == DependentScopeDeclRefExprClass; 2918 } 2919 2920 child_range children() { return child_range(); } 2921 2922 friend class ASTStmtReader; 2923 friend class ASTStmtWriter; 2924}; 2925 2926/// Represents an expression -- generally a full-expression -- that 2927/// introduces cleanups to be run at the end of the sub-expression's 2928/// evaluation. The most common source of expression-introduced 2929/// cleanups is temporary objects in C++, but several other kinds of 2930/// expressions can create cleanups, including basically every 2931/// call in ARC that returns an Objective-C pointer. 2932/// 2933/// This expression also tracks whether the sub-expression contains a 2934/// potentially-evaluated block literal. The lifetime of a block 2935/// literal is the extent of the enclosing scope. 2936class ExprWithCleanups : public Expr { 2937public: 2938 /// The type of objects that are kept in the cleanup. 2939 /// It's useful to remember the set of blocks; we could also 2940 /// remember the set of temporaries, but there's currently 2941 /// no need. 2942 typedef BlockDecl *CleanupObject; 2943 2944private: 2945 Stmt *SubExpr; 2946 2947 ExprWithCleanups(EmptyShell, unsigned NumObjects); 2948 ExprWithCleanups(Expr *SubExpr, ArrayRef<CleanupObject> Objects); 2949 2950 CleanupObject *getObjectsBuffer() { 2951 return reinterpret_cast<CleanupObject*>(this + 1); 2952 } 2953 const CleanupObject *getObjectsBuffer() const { 2954 return reinterpret_cast<const CleanupObject*>(this + 1); 2955 } 2956 friend class ASTStmtReader; 2957 2958public: 2959 static ExprWithCleanups *Create(ASTContext &C, EmptyShell empty, 2960 unsigned numObjects); 2961 2962 static ExprWithCleanups *Create(ASTContext &C, Expr *subexpr, 2963 ArrayRef<CleanupObject> objects); 2964 2965 ArrayRef<CleanupObject> getObjects() const { 2966 return ArrayRef<CleanupObject>(getObjectsBuffer(), getNumObjects()); 2967 } 2968 2969 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; } 2970 2971 CleanupObject getObject(unsigned i) const { 2972 assert(i < getNumObjects() && "Index out of range"); 2973 return getObjects()[i]; 2974 } 2975 2976 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 2977 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 2978 2979 /// As with any mutator of the AST, be very careful 2980 /// when modifying an existing AST to preserve its invariants. 2981 void setSubExpr(Expr *E) { SubExpr = E; } 2982 2983 SourceLocation getLocStart() const LLVM_READONLY { 2984 return SubExpr->getLocStart(); 2985 } 2986 SourceLocation getLocEnd() const LLVM_READONLY { return SubExpr->getLocEnd();} 2987 2988 // Implement isa/cast/dyncast/etc. 2989 static bool classof(const Stmt *T) { 2990 return T->getStmtClass() == ExprWithCleanupsClass; 2991 } 2992 2993 // Iterators 2994 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 2995}; 2996 2997/// \brief Describes an explicit type conversion that uses functional 2998/// notion but could not be resolved because one or more arguments are 2999/// type-dependent. 3000/// 3001/// The explicit type conversions expressed by 3002/// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>, 3003/// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and 3004/// either \c T is a dependent type or one or more of the <tt>a</tt>'s is 3005/// type-dependent. For example, this would occur in a template such 3006/// as: 3007/// 3008/// \code 3009/// template<typename T, typename A1> 3010/// inline T make_a(const A1& a1) { 3011/// return T(a1); 3012/// } 3013/// \endcode 3014/// 3015/// When the returned expression is instantiated, it may resolve to a 3016/// constructor call, conversion function call, or some kind of type 3017/// conversion. 3018class CXXUnresolvedConstructExpr : public Expr { 3019 /// \brief The type being constructed. 3020 TypeSourceInfo *Type; 3021 3022 /// \brief The location of the left parentheses ('('). 3023 SourceLocation LParenLoc; 3024 3025 /// \brief The location of the right parentheses (')'). 3026 SourceLocation RParenLoc; 3027 3028 /// \brief The number of arguments used to construct the type. 3029 unsigned NumArgs; 3030 3031 CXXUnresolvedConstructExpr(TypeSourceInfo *Type, 3032 SourceLocation LParenLoc, 3033 ArrayRef<Expr*> Args, 3034 SourceLocation RParenLoc); 3035 3036 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) 3037 : Expr(CXXUnresolvedConstructExprClass, Empty), Type(), NumArgs(NumArgs) { } 3038 3039 friend class ASTStmtReader; 3040 3041public: 3042 static CXXUnresolvedConstructExpr *Create(ASTContext &C, 3043 TypeSourceInfo *Type, 3044 SourceLocation LParenLoc, 3045 ArrayRef<Expr*> Args, 3046 SourceLocation RParenLoc); 3047 3048 static CXXUnresolvedConstructExpr *CreateEmpty(ASTContext &C, 3049 unsigned NumArgs); 3050 3051 /// \brief Retrieve the type that is being constructed, as specified 3052 /// in the source code. 3053 QualType getTypeAsWritten() const { return Type->getType(); } 3054 3055 /// \brief Retrieve the type source information for the type being 3056 /// constructed. 3057 TypeSourceInfo *getTypeSourceInfo() const { return Type; } 3058 3059 /// \brief Retrieve the location of the left parentheses ('(') that 3060 /// precedes the argument list. 3061 SourceLocation getLParenLoc() const { return LParenLoc; } 3062 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 3063 3064 /// \brief Retrieve the location of the right parentheses (')') that 3065 /// follows the argument list. 3066 SourceLocation getRParenLoc() const { return RParenLoc; } 3067 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3068 3069 /// \brief Retrieve the number of arguments. 3070 unsigned arg_size() const { return NumArgs; } 3071 3072 typedef Expr** arg_iterator; 3073 arg_iterator arg_begin() { return reinterpret_cast<Expr**>(this + 1); } 3074 arg_iterator arg_end() { return arg_begin() + NumArgs; } 3075 3076 typedef const Expr* const * const_arg_iterator; 3077 const_arg_iterator arg_begin() const { 3078 return reinterpret_cast<const Expr* const *>(this + 1); 3079 } 3080 const_arg_iterator arg_end() const { 3081 return arg_begin() + NumArgs; 3082 } 3083 3084 Expr *getArg(unsigned I) { 3085 assert(I < NumArgs && "Argument index out-of-range"); 3086 return *(arg_begin() + I); 3087 } 3088 3089 const Expr *getArg(unsigned I) const { 3090 assert(I < NumArgs && "Argument index out-of-range"); 3091 return *(arg_begin() + I); 3092 } 3093 3094 void setArg(unsigned I, Expr *E) { 3095 assert(I < NumArgs && "Argument index out-of-range"); 3096 *(arg_begin() + I) = E; 3097 } 3098 3099 SourceLocation getLocStart() const LLVM_READONLY; 3100 SourceLocation getLocEnd() const LLVM_READONLY { 3101 assert(RParenLoc.isValid() || NumArgs == 1); 3102 return RParenLoc.isValid() ? RParenLoc : getArg(0)->getLocEnd(); 3103 } 3104 3105 static bool classof(const Stmt *T) { 3106 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 3107 } 3108 3109 // Iterators 3110 child_range children() { 3111 Stmt **begin = reinterpret_cast<Stmt**>(this+1); 3112 return child_range(begin, begin + NumArgs); 3113 } 3114}; 3115 3116/// \brief Represents a C++ member access expression where the actual 3117/// member referenced could not be resolved because the base 3118/// expression or the member name was dependent. 3119/// 3120/// Like UnresolvedMemberExprs, these can be either implicit or 3121/// explicit accesses. It is only possible to get one of these with 3122/// an implicit access if a qualifier is provided. 3123class CXXDependentScopeMemberExpr : public Expr { 3124 /// \brief The expression for the base pointer or class reference, 3125 /// e.g., the \c x in x.f. Can be null in implicit accesses. 3126 Stmt *Base; 3127 3128 /// \brief The type of the base expression. Never null, even for 3129 /// implicit accesses. 3130 QualType BaseType; 3131 3132 /// \brief Whether this member expression used the '->' operator or 3133 /// the '.' operator. 3134 bool IsArrow : 1; 3135 3136 /// \brief Whether this member expression has info for explicit template 3137 /// keyword and arguments. 3138 bool HasTemplateKWAndArgsInfo : 1; 3139 3140 /// \brief The location of the '->' or '.' operator. 3141 SourceLocation OperatorLoc; 3142 3143 /// \brief The nested-name-specifier that precedes the member name, if any. 3144 NestedNameSpecifierLoc QualifierLoc; 3145 3146 /// \brief In a qualified member access expression such as t->Base::f, this 3147 /// member stores the resolves of name lookup in the context of the member 3148 /// access expression, to be used at instantiation time. 3149 /// 3150 /// FIXME: This member, along with the QualifierLoc, could 3151 /// be stuck into a structure that is optionally allocated at the end of 3152 /// the CXXDependentScopeMemberExpr, to save space in the common case. 3153 NamedDecl *FirstQualifierFoundInScope; 3154 3155 /// \brief The member to which this member expression refers, which 3156 /// can be name, overloaded operator, or destructor. 3157 /// 3158 /// FIXME: could also be a template-id 3159 DeclarationNameInfo MemberNameInfo; 3160 3161 /// \brief Return the optional template keyword and arguments info. 3162 ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() { 3163 if (!HasTemplateKWAndArgsInfo) return 0; 3164 return reinterpret_cast<ASTTemplateKWAndArgsInfo*>(this + 1); 3165 } 3166 /// \brief Return the optional template keyword and arguments info. 3167 const ASTTemplateKWAndArgsInfo *getTemplateKWAndArgsInfo() const { 3168 return const_cast<CXXDependentScopeMemberExpr*>(this) 3169 ->getTemplateKWAndArgsInfo(); 3170 } 3171 3172 CXXDependentScopeMemberExpr(ASTContext &C, 3173 Expr *Base, QualType BaseType, bool IsArrow, 3174 SourceLocation OperatorLoc, 3175 NestedNameSpecifierLoc QualifierLoc, 3176 SourceLocation TemplateKWLoc, 3177 NamedDecl *FirstQualifierFoundInScope, 3178 DeclarationNameInfo MemberNameInfo, 3179 const TemplateArgumentListInfo *TemplateArgs); 3180 3181public: 3182 CXXDependentScopeMemberExpr(ASTContext &C, 3183 Expr *Base, QualType BaseType, 3184 bool IsArrow, 3185 SourceLocation OperatorLoc, 3186 NestedNameSpecifierLoc QualifierLoc, 3187 NamedDecl *FirstQualifierFoundInScope, 3188 DeclarationNameInfo MemberNameInfo); 3189 3190 static CXXDependentScopeMemberExpr * 3191 Create(ASTContext &C, 3192 Expr *Base, QualType BaseType, bool IsArrow, 3193 SourceLocation OperatorLoc, 3194 NestedNameSpecifierLoc QualifierLoc, 3195 SourceLocation TemplateKWLoc, 3196 NamedDecl *FirstQualifierFoundInScope, 3197 DeclarationNameInfo MemberNameInfo, 3198 const TemplateArgumentListInfo *TemplateArgs); 3199 3200 static CXXDependentScopeMemberExpr * 3201 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 3202 unsigned NumTemplateArgs); 3203 3204 /// \brief True if this is an implicit access, i.e. one in which the 3205 /// member being accessed was not written in the source. The source 3206 /// location of the operator is invalid in this case. 3207 bool isImplicitAccess() const; 3208 3209 /// \brief Retrieve the base object of this member expressions, 3210 /// e.g., the \c x in \c x.m. 3211 Expr *getBase() const { 3212 assert(!isImplicitAccess()); 3213 return cast<Expr>(Base); 3214 } 3215 3216 QualType getBaseType() const { return BaseType; } 3217 3218 /// \brief Determine whether this member expression used the '->' 3219 /// operator; otherwise, it used the '.' operator. 3220 bool isArrow() const { return IsArrow; } 3221 3222 /// \brief Retrieve the location of the '->' or '.' operator. 3223 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3224 3225 /// \brief Retrieve the nested-name-specifier that qualifies the member 3226 /// name. 3227 NestedNameSpecifier *getQualifier() const { 3228 return QualifierLoc.getNestedNameSpecifier(); 3229 } 3230 3231 /// \brief Retrieve the nested-name-specifier that qualifies the member 3232 /// name, with source location information. 3233 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 3234 3235 3236 /// \brief Retrieve the first part of the nested-name-specifier that was 3237 /// found in the scope of the member access expression when the member access 3238 /// was initially parsed. 3239 /// 3240 /// This function only returns a useful result when member access expression 3241 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 3242 /// returned by this function describes what was found by unqualified name 3243 /// lookup for the identifier "Base" within the scope of the member access 3244 /// expression itself. At template instantiation time, this information is 3245 /// combined with the results of name lookup into the type of the object 3246 /// expression itself (the class type of x). 3247 NamedDecl *getFirstQualifierFoundInScope() const { 3248 return FirstQualifierFoundInScope; 3249 } 3250 3251 /// \brief Retrieve the name of the member that this expression 3252 /// refers to. 3253 const DeclarationNameInfo &getMemberNameInfo() const { 3254 return MemberNameInfo; 3255 } 3256 3257 /// \brief Retrieve the name of the member that this expression 3258 /// refers to. 3259 DeclarationName getMember() const { return MemberNameInfo.getName(); } 3260 3261 // \brief Retrieve the location of the name of the member that this 3262 // expression refers to. 3263 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); } 3264 3265 /// \brief Retrieve the location of the template keyword preceding the 3266 /// member name, if any. 3267 SourceLocation getTemplateKeywordLoc() const { 3268 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 3269 return getTemplateKWAndArgsInfo()->getTemplateKeywordLoc(); 3270 } 3271 3272 /// \brief Retrieve the location of the left angle bracket starting the 3273 /// explicit template argument list following the member name, if any. 3274 SourceLocation getLAngleLoc() const { 3275 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 3276 return getTemplateKWAndArgsInfo()->LAngleLoc; 3277 } 3278 3279 /// \brief Retrieve the location of the right angle bracket ending the 3280 /// explicit template argument list following the member name, if any. 3281 SourceLocation getRAngleLoc() const { 3282 if (!HasTemplateKWAndArgsInfo) return SourceLocation(); 3283 return getTemplateKWAndArgsInfo()->RAngleLoc; 3284 } 3285 3286 /// Determines whether the member name was preceded by the template keyword. 3287 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 3288 3289 /// \brief Determines whether this member expression actually had a C++ 3290 /// template argument list explicitly specified, e.g., x.f<int>. 3291 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 3292 3293 /// \brief Retrieve the explicit template argument list that followed the 3294 /// member template name, if any. 3295 ASTTemplateArgumentListInfo &getExplicitTemplateArgs() { 3296 assert(hasExplicitTemplateArgs()); 3297 return *reinterpret_cast<ASTTemplateArgumentListInfo *>(this + 1); 3298 } 3299 3300 /// \brief Retrieve the explicit template argument list that followed the 3301 /// member template name, if any. 3302 const ASTTemplateArgumentListInfo &getExplicitTemplateArgs() const { 3303 return const_cast<CXXDependentScopeMemberExpr *>(this) 3304 ->getExplicitTemplateArgs(); 3305 } 3306 3307 /// \brief Retrieves the optional explicit template arguments. 3308 /// 3309 /// This points to the same data as getExplicitTemplateArgs(), but 3310 /// returns null if there are no explicit template arguments. 3311 const ASTTemplateArgumentListInfo *getOptionalExplicitTemplateArgs() const { 3312 if (!hasExplicitTemplateArgs()) return 0; 3313 return &getExplicitTemplateArgs(); 3314 } 3315 3316 /// \brief Copies the template arguments (if present) into the given 3317 /// structure. 3318 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 3319 getExplicitTemplateArgs().copyInto(List); 3320 } 3321 3322 /// \brief Initializes the template arguments using the given structure. 3323 void initializeTemplateArgumentsFrom(const TemplateArgumentListInfo &List) { 3324 getExplicitTemplateArgs().initializeFrom(List); 3325 } 3326 3327 /// \brief Retrieve the template arguments provided as part of this 3328 /// template-id. 3329 const TemplateArgumentLoc *getTemplateArgs() const { 3330 return getExplicitTemplateArgs().getTemplateArgs(); 3331 } 3332 3333 /// \brief Retrieve the number of template arguments provided as part of this 3334 /// template-id. 3335 unsigned getNumTemplateArgs() const { 3336 return getExplicitTemplateArgs().NumTemplateArgs; 3337 } 3338 3339 SourceLocation getLocStart() const LLVM_READONLY { 3340 if (!isImplicitAccess()) 3341 return Base->getLocStart(); 3342 if (getQualifier()) 3343 return getQualifierLoc().getBeginLoc(); 3344 return MemberNameInfo.getBeginLoc(); 3345 3346 } 3347 SourceLocation getLocEnd() const LLVM_READONLY { 3348 if (hasExplicitTemplateArgs()) 3349 return getRAngleLoc(); 3350 return MemberNameInfo.getEndLoc(); 3351 } 3352 3353 static bool classof(const Stmt *T) { 3354 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 3355 } 3356 3357 // Iterators 3358 child_range children() { 3359 if (isImplicitAccess()) return child_range(); 3360 return child_range(&Base, &Base + 1); 3361 } 3362 3363 friend class ASTStmtReader; 3364 friend class ASTStmtWriter; 3365}; 3366 3367/// \brief Represents a C++ member access expression for which lookup 3368/// produced a set of overloaded functions. 3369/// 3370/// The member access may be explicit or implicit: 3371/// \code 3372/// struct A { 3373/// int a, b; 3374/// int explicitAccess() { return this->a + this->A::b; } 3375/// int implicitAccess() { return a + A::b; } 3376/// }; 3377/// \endcode 3378/// 3379/// In the final AST, an explicit access always becomes a MemberExpr. 3380/// An implicit access may become either a MemberExpr or a 3381/// DeclRefExpr, depending on whether the member is static. 3382class UnresolvedMemberExpr : public OverloadExpr { 3383 /// \brief Whether this member expression used the '->' operator or 3384 /// the '.' operator. 3385 bool IsArrow : 1; 3386 3387 /// \brief Whether the lookup results contain an unresolved using 3388 /// declaration. 3389 bool HasUnresolvedUsing : 1; 3390 3391 /// \brief The expression for the base pointer or class reference, 3392 /// e.g., the \c x in x.f. 3393 /// 3394 /// This can be null if this is an 'unbased' member expression. 3395 Stmt *Base; 3396 3397 /// \brief The type of the base expression; never null. 3398 QualType BaseType; 3399 3400 /// \brief The location of the '->' or '.' operator. 3401 SourceLocation OperatorLoc; 3402 3403 UnresolvedMemberExpr(ASTContext &C, bool HasUnresolvedUsing, 3404 Expr *Base, QualType BaseType, bool IsArrow, 3405 SourceLocation OperatorLoc, 3406 NestedNameSpecifierLoc QualifierLoc, 3407 SourceLocation TemplateKWLoc, 3408 const DeclarationNameInfo &MemberNameInfo, 3409 const TemplateArgumentListInfo *TemplateArgs, 3410 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3411 3412 UnresolvedMemberExpr(EmptyShell Empty) 3413 : OverloadExpr(UnresolvedMemberExprClass, Empty), IsArrow(false), 3414 HasUnresolvedUsing(false), Base(0) { } 3415 3416 friend class ASTStmtReader; 3417 3418public: 3419 static UnresolvedMemberExpr * 3420 Create(ASTContext &C, bool HasUnresolvedUsing, 3421 Expr *Base, QualType BaseType, bool IsArrow, 3422 SourceLocation OperatorLoc, 3423 NestedNameSpecifierLoc QualifierLoc, 3424 SourceLocation TemplateKWLoc, 3425 const DeclarationNameInfo &MemberNameInfo, 3426 const TemplateArgumentListInfo *TemplateArgs, 3427 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3428 3429 static UnresolvedMemberExpr * 3430 CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo, 3431 unsigned NumTemplateArgs); 3432 3433 /// \brief True if this is an implicit access, i.e., one in which the 3434 /// member being accessed was not written in the source. 3435 /// 3436 /// The source location of the operator is invalid in this case. 3437 bool isImplicitAccess() const; 3438 3439 /// \brief Retrieve the base object of this member expressions, 3440 /// e.g., the \c x in \c x.m. 3441 Expr *getBase() { 3442 assert(!isImplicitAccess()); 3443 return cast<Expr>(Base); 3444 } 3445 const Expr *getBase() const { 3446 assert(!isImplicitAccess()); 3447 return cast<Expr>(Base); 3448 } 3449 3450 QualType getBaseType() const { return BaseType; } 3451 3452 /// \brief Determine whether the lookup results contain an unresolved using 3453 /// declaration. 3454 bool hasUnresolvedUsing() const { return HasUnresolvedUsing; } 3455 3456 /// \brief Determine whether this member expression used the '->' 3457 /// operator; otherwise, it used the '.' operator. 3458 bool isArrow() const { return IsArrow; } 3459 3460 /// \brief Retrieve the location of the '->' or '.' operator. 3461 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3462 3463 /// \brief Retrieve the naming class of this lookup. 3464 CXXRecordDecl *getNamingClass() const; 3465 3466 /// \brief Retrieve the full name info for the member that this expression 3467 /// refers to. 3468 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); } 3469 3470 /// \brief Retrieve the name of the member that this expression 3471 /// refers to. 3472 DeclarationName getMemberName() const { return getName(); } 3473 3474 // \brief Retrieve the location of the name of the member that this 3475 // expression refers to. 3476 SourceLocation getMemberLoc() const { return getNameLoc(); } 3477 3478 // \brief Return the preferred location (the member name) for the arrow when 3479 // diagnosing a problem with this expression. 3480 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); } 3481 3482 SourceLocation getLocStart() const LLVM_READONLY { 3483 if (!isImplicitAccess()) 3484 return Base->getLocStart(); 3485 if (NestedNameSpecifierLoc l = getQualifierLoc()) 3486 return l.getBeginLoc(); 3487 return getMemberNameInfo().getLocStart(); 3488 } 3489 SourceLocation getLocEnd() const LLVM_READONLY { 3490 if (hasExplicitTemplateArgs()) 3491 return getRAngleLoc(); 3492 return getMemberNameInfo().getLocEnd(); 3493 } 3494 3495 static bool classof(const Stmt *T) { 3496 return T->getStmtClass() == UnresolvedMemberExprClass; 3497 } 3498 3499 // Iterators 3500 child_range children() { 3501 if (isImplicitAccess()) return child_range(); 3502 return child_range(&Base, &Base + 1); 3503 } 3504}; 3505 3506/// \brief Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]). 3507/// 3508/// The noexcept expression tests whether a given expression might throw. Its 3509/// result is a boolean constant. 3510class CXXNoexceptExpr : public Expr { 3511 bool Value : 1; 3512 Stmt *Operand; 3513 SourceRange Range; 3514 3515 friend class ASTStmtReader; 3516 3517public: 3518 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val, 3519 SourceLocation Keyword, SourceLocation RParen) 3520 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary, 3521 /*TypeDependent*/false, 3522 /*ValueDependent*/Val == CT_Dependent, 3523 Val == CT_Dependent || Operand->isInstantiationDependent(), 3524 Operand->containsUnexpandedParameterPack()), 3525 Value(Val == CT_Cannot), Operand(Operand), Range(Keyword, RParen) 3526 { } 3527 3528 CXXNoexceptExpr(EmptyShell Empty) 3529 : Expr(CXXNoexceptExprClass, Empty) 3530 { } 3531 3532 Expr *getOperand() const { return static_cast<Expr*>(Operand); } 3533 3534 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); } 3535 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); } 3536 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 3537 3538 bool getValue() const { return Value; } 3539 3540 static bool classof(const Stmt *T) { 3541 return T->getStmtClass() == CXXNoexceptExprClass; 3542 } 3543 3544 // Iterators 3545 child_range children() { return child_range(&Operand, &Operand + 1); } 3546}; 3547 3548/// \brief Represents a C++11 pack expansion that produces a sequence of 3549/// expressions. 3550/// 3551/// A pack expansion expression contains a pattern (which itself is an 3552/// expression) followed by an ellipsis. For example: 3553/// 3554/// \code 3555/// template<typename F, typename ...Types> 3556/// void forward(F f, Types &&...args) { 3557/// f(static_cast<Types&&>(args)...); 3558/// } 3559/// \endcode 3560/// 3561/// Here, the argument to the function object \c f is a pack expansion whose 3562/// pattern is \c static_cast<Types&&>(args). When the \c forward function 3563/// template is instantiated, the pack expansion will instantiate to zero or 3564/// or more function arguments to the function object \c f. 3565class PackExpansionExpr : public Expr { 3566 SourceLocation EllipsisLoc; 3567 3568 /// \brief The number of expansions that will be produced by this pack 3569 /// expansion expression, if known. 3570 /// 3571 /// When zero, the number of expansions is not known. Otherwise, this value 3572 /// is the number of expansions + 1. 3573 unsigned NumExpansions; 3574 3575 Stmt *Pattern; 3576 3577 friend class ASTStmtReader; 3578 friend class ASTStmtWriter; 3579 3580public: 3581 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc, 3582 Optional<unsigned> NumExpansions) 3583 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(), 3584 Pattern->getObjectKind(), /*TypeDependent=*/true, 3585 /*ValueDependent=*/true, /*InstantiationDependent=*/true, 3586 /*ContainsUnexpandedParameterPack=*/false), 3587 EllipsisLoc(EllipsisLoc), 3588 NumExpansions(NumExpansions? *NumExpansions + 1 : 0), 3589 Pattern(Pattern) { } 3590 3591 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) { } 3592 3593 /// \brief Retrieve the pattern of the pack expansion. 3594 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); } 3595 3596 /// \brief Retrieve the pattern of the pack expansion. 3597 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); } 3598 3599 /// \brief Retrieve the location of the ellipsis that describes this pack 3600 /// expansion. 3601 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 3602 3603 /// \brief Determine the number of expansions that will be produced when 3604 /// this pack expansion is instantiated, if already known. 3605 Optional<unsigned> getNumExpansions() const { 3606 if (NumExpansions) 3607 return NumExpansions - 1; 3608 3609 return None; 3610 } 3611 3612 SourceLocation getLocStart() const LLVM_READONLY { 3613 return Pattern->getLocStart(); 3614 } 3615 SourceLocation getLocEnd() const LLVM_READONLY { return EllipsisLoc; } 3616 3617 static bool classof(const Stmt *T) { 3618 return T->getStmtClass() == PackExpansionExprClass; 3619 } 3620 3621 // Iterators 3622 child_range children() { 3623 return child_range(&Pattern, &Pattern + 1); 3624 } 3625}; 3626 3627inline ASTTemplateKWAndArgsInfo *OverloadExpr::getTemplateKWAndArgsInfo() { 3628 if (!HasTemplateKWAndArgsInfo) return 0; 3629 if (isa<UnresolvedLookupExpr>(this)) 3630 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3631 (cast<UnresolvedLookupExpr>(this) + 1); 3632 else 3633 return reinterpret_cast<ASTTemplateKWAndArgsInfo*> 3634 (cast<UnresolvedMemberExpr>(this) + 1); 3635} 3636 3637/// \brief Represents an expression that computes the length of a parameter 3638/// pack. 3639/// 3640/// \code 3641/// template<typename ...Types> 3642/// struct count { 3643/// static const unsigned value = sizeof...(Types); 3644/// }; 3645/// \endcode 3646class SizeOfPackExpr : public Expr { 3647 /// \brief The location of the \c sizeof keyword. 3648 SourceLocation OperatorLoc; 3649 3650 /// \brief The location of the name of the parameter pack. 3651 SourceLocation PackLoc; 3652 3653 /// \brief The location of the closing parenthesis. 3654 SourceLocation RParenLoc; 3655 3656 /// \brief The length of the parameter pack, if known. 3657 /// 3658 /// When this expression is value-dependent, the length of the parameter pack 3659 /// is unknown. When this expression is not value-dependent, the length is 3660 /// known. 3661 unsigned Length; 3662 3663 /// \brief The parameter pack itself. 3664 NamedDecl *Pack; 3665 3666 friend class ASTStmtReader; 3667 friend class ASTStmtWriter; 3668 3669public: 3670 /// \brief Create a value-dependent expression that computes the length of 3671 /// the given parameter pack. 3672 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3673 SourceLocation PackLoc, SourceLocation RParenLoc) 3674 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3675 /*TypeDependent=*/false, /*ValueDependent=*/true, 3676 /*InstantiationDependent=*/true, 3677 /*ContainsUnexpandedParameterPack=*/false), 3678 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3679 Length(0), Pack(Pack) { } 3680 3681 /// \brief Create an expression that computes the length of 3682 /// the given parameter pack, which is already known. 3683 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 3684 SourceLocation PackLoc, SourceLocation RParenLoc, 3685 unsigned Length) 3686 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 3687 /*TypeDependent=*/false, /*ValueDependent=*/false, 3688 /*InstantiationDependent=*/false, 3689 /*ContainsUnexpandedParameterPack=*/false), 3690 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 3691 Length(Length), Pack(Pack) { } 3692 3693 /// \brief Create an empty expression. 3694 SizeOfPackExpr(EmptyShell Empty) : Expr(SizeOfPackExprClass, Empty) { } 3695 3696 /// \brief Determine the location of the 'sizeof' keyword. 3697 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3698 3699 /// \brief Determine the location of the parameter pack. 3700 SourceLocation getPackLoc() const { return PackLoc; } 3701 3702 /// \brief Determine the location of the right parenthesis. 3703 SourceLocation getRParenLoc() const { return RParenLoc; } 3704 3705 /// \brief Retrieve the parameter pack. 3706 NamedDecl *getPack() const { return Pack; } 3707 3708 /// \brief Retrieve the length of the parameter pack. 3709 /// 3710 /// This routine may only be invoked when the expression is not 3711 /// value-dependent. 3712 unsigned getPackLength() const { 3713 assert(!isValueDependent() && 3714 "Cannot get the length of a value-dependent pack size expression"); 3715 return Length; 3716 } 3717 3718 SourceLocation getLocStart() const LLVM_READONLY { return OperatorLoc; } 3719 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; } 3720 3721 static bool classof(const Stmt *T) { 3722 return T->getStmtClass() == SizeOfPackExprClass; 3723 } 3724 3725 // Iterators 3726 child_range children() { return child_range(); } 3727}; 3728 3729/// \brief Represents a reference to a non-type template parameter 3730/// that has been substituted with a template argument. 3731class SubstNonTypeTemplateParmExpr : public Expr { 3732 /// \brief The replaced parameter. 3733 NonTypeTemplateParmDecl *Param; 3734 3735 /// \brief The replacement expression. 3736 Stmt *Replacement; 3737 3738 /// \brief The location of the non-type template parameter reference. 3739 SourceLocation NameLoc; 3740 3741 friend class ASTReader; 3742 friend class ASTStmtReader; 3743 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty) 3744 : Expr(SubstNonTypeTemplateParmExprClass, Empty) { } 3745 3746public: 3747 SubstNonTypeTemplateParmExpr(QualType type, 3748 ExprValueKind valueKind, 3749 SourceLocation loc, 3750 NonTypeTemplateParmDecl *param, 3751 Expr *replacement) 3752 : Expr(SubstNonTypeTemplateParmExprClass, type, valueKind, OK_Ordinary, 3753 replacement->isTypeDependent(), replacement->isValueDependent(), 3754 replacement->isInstantiationDependent(), 3755 replacement->containsUnexpandedParameterPack()), 3756 Param(param), Replacement(replacement), NameLoc(loc) {} 3757 3758 SourceLocation getNameLoc() const { return NameLoc; } 3759 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; } 3760 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; } 3761 3762 Expr *getReplacement() const { return cast<Expr>(Replacement); } 3763 3764 NonTypeTemplateParmDecl *getParameter() const { return Param; } 3765 3766 static bool classof(const Stmt *s) { 3767 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass; 3768 } 3769 3770 // Iterators 3771 child_range children() { return child_range(&Replacement, &Replacement+1); } 3772}; 3773 3774/// \brief Represents a reference to a non-type template parameter pack that 3775/// has been substituted with a non-template argument pack. 3776/// 3777/// When a pack expansion in the source code contains multiple parameter packs 3778/// and those parameter packs correspond to different levels of template 3779/// parameter lists, this node is used to represent a non-type template 3780/// parameter pack from an outer level, which has already had its argument pack 3781/// substituted but that still lives within a pack expansion that itself 3782/// could not be instantiated. When actually performing a substitution into 3783/// that pack expansion (e.g., when all template parameters have corresponding 3784/// arguments), this type will be replaced with the appropriate underlying 3785/// expression at the current pack substitution index. 3786class SubstNonTypeTemplateParmPackExpr : public Expr { 3787 /// \brief The non-type template parameter pack itself. 3788 NonTypeTemplateParmDecl *Param; 3789 3790 /// \brief A pointer to the set of template arguments that this 3791 /// parameter pack is instantiated with. 3792 const TemplateArgument *Arguments; 3793 3794 /// \brief The number of template arguments in \c Arguments. 3795 unsigned NumArguments; 3796 3797 /// \brief The location of the non-type template parameter pack reference. 3798 SourceLocation NameLoc; 3799 3800 friend class ASTReader; 3801 friend class ASTStmtReader; 3802 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty) 3803 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) { } 3804 3805public: 3806 SubstNonTypeTemplateParmPackExpr(QualType T, 3807 NonTypeTemplateParmDecl *Param, 3808 SourceLocation NameLoc, 3809 const TemplateArgument &ArgPack); 3810 3811 /// \brief Retrieve the non-type template parameter pack being substituted. 3812 NonTypeTemplateParmDecl *getParameterPack() const { return Param; } 3813 3814 /// \brief Retrieve the location of the parameter pack name. 3815 SourceLocation getParameterPackLocation() const { return NameLoc; } 3816 3817 /// \brief Retrieve the template argument pack containing the substituted 3818 /// template arguments. 3819 TemplateArgument getArgumentPack() const; 3820 3821 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; } 3822 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; } 3823 3824 static bool classof(const Stmt *T) { 3825 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass; 3826 } 3827 3828 // Iterators 3829 child_range children() { return child_range(); } 3830}; 3831 3832/// \brief Represents a reference to a function parameter pack that has been 3833/// substituted but not yet expanded. 3834/// 3835/// When a pack expansion contains multiple parameter packs at different levels, 3836/// this node is used to represent a function parameter pack at an outer level 3837/// which we have already substituted to refer to expanded parameters, but where 3838/// the containing pack expansion cannot yet be expanded. 3839/// 3840/// \code 3841/// template<typename...Ts> struct S { 3842/// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...)); 3843/// }; 3844/// template struct S<int, int>; 3845/// \endcode 3846class FunctionParmPackExpr : public Expr { 3847 /// \brief The function parameter pack which was referenced. 3848 ParmVarDecl *ParamPack; 3849 3850 /// \brief The location of the function parameter pack reference. 3851 SourceLocation NameLoc; 3852 3853 /// \brief The number of expansions of this pack. 3854 unsigned NumParameters; 3855 3856 FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack, 3857 SourceLocation NameLoc, unsigned NumParams, 3858 Decl * const *Params); 3859 3860 friend class ASTReader; 3861 friend class ASTStmtReader; 3862 3863public: 3864 static FunctionParmPackExpr *Create(ASTContext &Context, QualType T, 3865 ParmVarDecl *ParamPack, 3866 SourceLocation NameLoc, 3867 ArrayRef<Decl *> Params); 3868 static FunctionParmPackExpr *CreateEmpty(ASTContext &Context, 3869 unsigned NumParams); 3870 3871 /// \brief Get the parameter pack which this expression refers to. 3872 ParmVarDecl *getParameterPack() const { return ParamPack; } 3873 3874 /// \brief Get the location of the parameter pack. 3875 SourceLocation getParameterPackLocation() const { return NameLoc; } 3876 3877 /// \brief Iterators over the parameters which the parameter pack expanded 3878 /// into. 3879 typedef ParmVarDecl * const *iterator; 3880 iterator begin() const { return reinterpret_cast<iterator>(this+1); } 3881 iterator end() const { return begin() + NumParameters; } 3882 3883 /// \brief Get the number of parameters in this parameter pack. 3884 unsigned getNumExpansions() const { return NumParameters; } 3885 3886 /// \brief Get an expansion of the parameter pack by index. 3887 ParmVarDecl *getExpansion(unsigned I) const { return begin()[I]; } 3888 3889 SourceLocation getLocStart() const LLVM_READONLY { return NameLoc; } 3890 SourceLocation getLocEnd() const LLVM_READONLY { return NameLoc; } 3891 3892 static bool classof(const Stmt *T) { 3893 return T->getStmtClass() == FunctionParmPackExprClass; 3894 } 3895 3896 child_range children() { return child_range(); } 3897}; 3898 3899/// \brief Represents a prvalue temporary that is written into memory so that 3900/// a reference can bind to it. 3901/// 3902/// Prvalue expressions are materialized when they need to have an address 3903/// in memory for a reference to bind to. This happens when binding a 3904/// reference to the result of a conversion, e.g., 3905/// 3906/// \code 3907/// const int &r = 1.0; 3908/// \endcode 3909/// 3910/// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is 3911/// then materialized via a \c MaterializeTemporaryExpr, and the reference 3912/// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues 3913/// (either an lvalue or an xvalue, depending on the kind of reference binding 3914/// to it), maintaining the invariant that references always bind to glvalues. 3915/// 3916/// Reference binding and copy-elision can both extend the lifetime of a 3917/// temporary. When either happens, the expression will also track the 3918/// declaration which is responsible for the lifetime extension. 3919class MaterializeTemporaryExpr : public Expr { 3920public: 3921 /// \brief The temporary-generating expression whose value will be 3922 /// materialized. 3923 Stmt *Temporary; 3924 3925 /// \brief The declaration which lifetime-extended this reference, if any. 3926 /// Either a VarDecl, or (for a ctor-initializer) a FieldDecl. 3927 const ValueDecl *ExtendingDecl; 3928 3929 friend class ASTStmtReader; 3930 friend class ASTStmtWriter; 3931 3932public: 3933 MaterializeTemporaryExpr(QualType T, Expr *Temporary, 3934 bool BoundToLvalueReference, 3935 const ValueDecl *ExtendedBy) 3936 : Expr(MaterializeTemporaryExprClass, T, 3937 BoundToLvalueReference? VK_LValue : VK_XValue, OK_Ordinary, 3938 Temporary->isTypeDependent(), Temporary->isValueDependent(), 3939 Temporary->isInstantiationDependent(), 3940 Temporary->containsUnexpandedParameterPack()), 3941 Temporary(Temporary), ExtendingDecl(ExtendedBy) { 3942 } 3943 3944 MaterializeTemporaryExpr(EmptyShell Empty) 3945 : Expr(MaterializeTemporaryExprClass, Empty) { } 3946 3947 /// \brief Retrieve the temporary-generating subexpression whose value will 3948 /// be materialized into a glvalue. 3949 Expr *GetTemporaryExpr() const { return static_cast<Expr *>(Temporary); } 3950 3951 /// \brief Retrieve the storage duration for the materialized temporary. 3952 StorageDuration getStorageDuration() const { 3953 if (!ExtendingDecl) 3954 return SD_FullExpression; 3955 // FIXME: This is not necessarily correct for a temporary materialized 3956 // within a default initializer. 3957 if (isa<FieldDecl>(ExtendingDecl)) 3958 return SD_Automatic; 3959 return cast<VarDecl>(ExtendingDecl)->getStorageDuration(); 3960 } 3961 3962 /// \brief Get the declaration which triggered the lifetime-extension of this 3963 /// temporary, if any. 3964 const ValueDecl *getExtendingDecl() const { return ExtendingDecl; } 3965 3966 void setExtendingDecl(const ValueDecl *ExtendedBy) { 3967 ExtendingDecl = ExtendedBy; 3968 } 3969 3970 /// \brief Determine whether this materialized temporary is bound to an 3971 /// lvalue reference; otherwise, it's bound to an rvalue reference. 3972 bool isBoundToLvalueReference() const { 3973 return getValueKind() == VK_LValue; 3974 } 3975 3976 SourceLocation getLocStart() const LLVM_READONLY { 3977 return Temporary->getLocStart(); 3978 } 3979 SourceLocation getLocEnd() const LLVM_READONLY { 3980 return Temporary->getLocEnd(); 3981 } 3982 3983 static bool classof(const Stmt *T) { 3984 return T->getStmtClass() == MaterializeTemporaryExprClass; 3985 } 3986 3987 // Iterators 3988 child_range children() { return child_range(&Temporary, &Temporary + 1); } 3989}; 3990 3991} // end namespace clang 3992 3993#endif 3994