1//===- ExprCXX.h - Classes for representing expressions ---------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9/// \file 10/// Defines the clang::Expr interface and subclasses for C++ expressions. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_EXPRCXX_H 15#define LLVM_CLANG_AST_EXPRCXX_H 16 17#include "clang/AST/Decl.h" 18#include "clang/AST/DeclBase.h" 19#include "clang/AST/DeclCXX.h" 20#include "clang/AST/DeclTemplate.h" 21#include "clang/AST/DeclarationName.h" 22#include "clang/AST/Expr.h" 23#include "clang/AST/NestedNameSpecifier.h" 24#include "clang/AST/OperationKinds.h" 25#include "clang/AST/Stmt.h" 26#include "clang/AST/TemplateBase.h" 27#include "clang/AST/Type.h" 28#include "clang/AST/UnresolvedSet.h" 29#include "clang/Basic/ExceptionSpecificationType.h" 30#include "clang/Basic/ExpressionTraits.h" 31#include "clang/Basic/LLVM.h" 32#include "clang/Basic/Lambda.h" 33#include "clang/Basic/LangOptions.h" 34#include "clang/Basic/OperatorKinds.h" 35#include "clang/Basic/SourceLocation.h" 36#include "clang/Basic/Specifiers.h" 37#include "clang/Basic/TypeTraits.h" 38#include "llvm/ADT/ArrayRef.h" 39#include "llvm/ADT/None.h" 40#include "llvm/ADT/Optional.h" 41#include "llvm/ADT/PointerUnion.h" 42#include "llvm/ADT/StringRef.h" 43#include "llvm/ADT/iterator_range.h" 44#include "llvm/Support/Casting.h" 45#include "llvm/Support/Compiler.h" 46#include "llvm/Support/TrailingObjects.h" 47#include <cassert> 48#include <cstddef> 49#include <cstdint> 50#include <memory> 51 52namespace clang { 53 54class ASTContext; 55class DeclAccessPair; 56class IdentifierInfo; 57class LambdaCapture; 58class NonTypeTemplateParmDecl; 59class TemplateParameterList; 60 61//===--------------------------------------------------------------------===// 62// C++ Expressions. 63//===--------------------------------------------------------------------===// 64 65/// A call to an overloaded operator written using operator 66/// syntax. 67/// 68/// Represents a call to an overloaded operator written using operator 69/// syntax, e.g., "x + y" or "*p". While semantically equivalent to a 70/// normal call, this AST node provides better information about the 71/// syntactic representation of the call. 72/// 73/// In a C++ template, this expression node kind will be used whenever 74/// any of the arguments are type-dependent. In this case, the 75/// function itself will be a (possibly empty) set of functions and 76/// function templates that were found by name lookup at template 77/// definition time. 78class CXXOperatorCallExpr final : public CallExpr { 79 friend class ASTStmtReader; 80 friend class ASTStmtWriter; 81 82 SourceRange Range; 83 84 // CXXOperatorCallExpr has some trailing objects belonging 85 // to CallExpr. See CallExpr for the details. 86 87 SourceRange getSourceRangeImpl() const LLVM_READONLY; 88 89 CXXOperatorCallExpr(OverloadedOperatorKind OpKind, Expr *Fn, 90 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, 91 SourceLocation OperatorLoc, FPOptions FPFeatures, 92 ADLCallKind UsesADL); 93 94 CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty); 95 96public: 97 static CXXOperatorCallExpr * 98 Create(const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn, 99 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK, 100 SourceLocation OperatorLoc, FPOptions FPFeatures, 101 ADLCallKind UsesADL = NotADL); 102 103 static CXXOperatorCallExpr *CreateEmpty(const ASTContext &Ctx, 104 unsigned NumArgs, EmptyShell Empty); 105 106 /// Returns the kind of overloaded operator that this expression refers to. 107 OverloadedOperatorKind getOperator() const { 108 return static_cast<OverloadedOperatorKind>( 109 CXXOperatorCallExprBits.OperatorKind); 110 } 111 112 static bool isAssignmentOp(OverloadedOperatorKind Opc) { 113 return Opc == OO_Equal || Opc == OO_StarEqual || Opc == OO_SlashEqual || 114 Opc == OO_PercentEqual || Opc == OO_PlusEqual || 115 Opc == OO_MinusEqual || Opc == OO_LessLessEqual || 116 Opc == OO_GreaterGreaterEqual || Opc == OO_AmpEqual || 117 Opc == OO_CaretEqual || Opc == OO_PipeEqual; 118 } 119 bool isAssignmentOp() const { return isAssignmentOp(getOperator()); } 120 121 /// Is this written as an infix binary operator? 122 bool isInfixBinaryOp() const; 123 124 /// Returns the location of the operator symbol in the expression. 125 /// 126 /// When \c getOperator()==OO_Call, this is the location of the right 127 /// parentheses; when \c getOperator()==OO_Subscript, this is the location 128 /// of the right bracket. 129 SourceLocation getOperatorLoc() const { return getRParenLoc(); } 130 131 SourceLocation getExprLoc() const LLVM_READONLY { 132 OverloadedOperatorKind Operator = getOperator(); 133 return (Operator < OO_Plus || Operator >= OO_Arrow || 134 Operator == OO_PlusPlus || Operator == OO_MinusMinus) 135 ? getBeginLoc() 136 : getOperatorLoc(); 137 } 138 139 SourceLocation getBeginLoc() const { return Range.getBegin(); } 140 SourceLocation getEndLoc() const { return Range.getEnd(); } 141 SourceRange getSourceRange() const { return Range; } 142 143 static bool classof(const Stmt *T) { 144 return T->getStmtClass() == CXXOperatorCallExprClass; 145 } 146 147 // Set the FP contractability status of this operator. Only meaningful for 148 // operations on floating point types. 149 void setFPFeatures(FPOptions F) { 150 CXXOperatorCallExprBits.FPFeatures = F.getInt(); 151 } 152 FPOptions getFPFeatures() const { 153 return FPOptions(CXXOperatorCallExprBits.FPFeatures); 154 } 155 156 // Get the FP contractability status of this operator. Only meaningful for 157 // operations on floating point types. 158 bool isFPContractableWithinStatement() const { 159 return getFPFeatures().allowFPContractWithinStatement(); 160 } 161}; 162 163/// Represents a call to a member function that 164/// may be written either with member call syntax (e.g., "obj.func()" 165/// or "objptr->func()") or with normal function-call syntax 166/// ("func()") within a member function that ends up calling a member 167/// function. The callee in either case is a MemberExpr that contains 168/// both the object argument and the member function, while the 169/// arguments are the arguments within the parentheses (not including 170/// the object argument). 171class CXXMemberCallExpr final : public CallExpr { 172 // CXXMemberCallExpr has some trailing objects belonging 173 // to CallExpr. See CallExpr for the details. 174 175 CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty, 176 ExprValueKind VK, SourceLocation RP, unsigned MinNumArgs); 177 178 CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty); 179 180public: 181 static CXXMemberCallExpr *Create(const ASTContext &Ctx, Expr *Fn, 182 ArrayRef<Expr *> Args, QualType Ty, 183 ExprValueKind VK, SourceLocation RP, 184 unsigned MinNumArgs = 0); 185 186 static CXXMemberCallExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs, 187 EmptyShell Empty); 188 189 /// Retrieve the implicit object argument for the member call. 190 /// 191 /// For example, in "x.f(5)", this returns the sub-expression "x". 192 Expr *getImplicitObjectArgument() const; 193 194 /// Retrieve the type of the object argument. 195 /// 196 /// Note that this always returns a non-pointer type. 197 QualType getObjectType() const; 198 199 /// Retrieve the declaration of the called method. 200 CXXMethodDecl *getMethodDecl() const; 201 202 /// Retrieve the CXXRecordDecl for the underlying type of 203 /// the implicit object argument. 204 /// 205 /// Note that this is may not be the same declaration as that of the class 206 /// context of the CXXMethodDecl which this function is calling. 207 /// FIXME: Returns 0 for member pointer call exprs. 208 CXXRecordDecl *getRecordDecl() const; 209 210 SourceLocation getExprLoc() const LLVM_READONLY { 211 SourceLocation CLoc = getCallee()->getExprLoc(); 212 if (CLoc.isValid()) 213 return CLoc; 214 215 return getBeginLoc(); 216 } 217 218 static bool classof(const Stmt *T) { 219 return T->getStmtClass() == CXXMemberCallExprClass; 220 } 221}; 222 223/// Represents a call to a CUDA kernel function. 224class CUDAKernelCallExpr final : public CallExpr { 225 friend class ASTStmtReader; 226 227 enum { CONFIG, END_PREARG }; 228 229 // CUDAKernelCallExpr has some trailing objects belonging 230 // to CallExpr. See CallExpr for the details. 231 232 CUDAKernelCallExpr(Expr *Fn, CallExpr *Config, ArrayRef<Expr *> Args, 233 QualType Ty, ExprValueKind VK, SourceLocation RP, 234 unsigned MinNumArgs); 235 236 CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty); 237 238public: 239 static CUDAKernelCallExpr *Create(const ASTContext &Ctx, Expr *Fn, 240 CallExpr *Config, ArrayRef<Expr *> Args, 241 QualType Ty, ExprValueKind VK, 242 SourceLocation RP, unsigned MinNumArgs = 0); 243 244 static CUDAKernelCallExpr *CreateEmpty(const ASTContext &Ctx, 245 unsigned NumArgs, EmptyShell Empty); 246 247 const CallExpr *getConfig() const { 248 return cast_or_null<CallExpr>(getPreArg(CONFIG)); 249 } 250 CallExpr *getConfig() { return cast_or_null<CallExpr>(getPreArg(CONFIG)); } 251 252 static bool classof(const Stmt *T) { 253 return T->getStmtClass() == CUDAKernelCallExprClass; 254 } 255}; 256 257/// A rewritten comparison expression that was originally written using 258/// operator syntax. 259/// 260/// In C++20, the following rewrites are performed: 261/// - <tt>a == b</tt> -> <tt>b == a</tt> 262/// - <tt>a != b</tt> -> <tt>!(a == b)</tt> 263/// - <tt>a != b</tt> -> <tt>!(b == a)</tt> 264/// - For \c \@ in \c <, \c <=, \c >, \c >=, \c <=>: 265/// - <tt>a @ b</tt> -> <tt>(a <=> b) @ 0</tt> 266/// - <tt>a @ b</tt> -> <tt>0 @ (b <=> a)</tt> 267/// 268/// This expression provides access to both the original syntax and the 269/// rewritten expression. 270/// 271/// Note that the rewritten calls to \c ==, \c <=>, and \c \@ are typically 272/// \c CXXOperatorCallExprs, but could theoretically be \c BinaryOperators. 273class CXXRewrittenBinaryOperator : public Expr { 274 friend class ASTStmtReader; 275 276 /// The rewritten semantic form. 277 Stmt *SemanticForm; 278 279public: 280 CXXRewrittenBinaryOperator(Expr *SemanticForm, bool IsReversed) 281 : Expr(CXXRewrittenBinaryOperatorClass, SemanticForm->getType(), 282 SemanticForm->getValueKind(), SemanticForm->getObjectKind(), 283 SemanticForm->isTypeDependent(), SemanticForm->isValueDependent(), 284 SemanticForm->isInstantiationDependent(), 285 SemanticForm->containsUnexpandedParameterPack()), 286 SemanticForm(SemanticForm) { 287 CXXRewrittenBinaryOperatorBits.IsReversed = IsReversed; 288 } 289 CXXRewrittenBinaryOperator(EmptyShell Empty) 290 : Expr(CXXRewrittenBinaryOperatorClass, Empty), SemanticForm() {} 291 292 /// Get an equivalent semantic form for this expression. 293 Expr *getSemanticForm() { return cast<Expr>(SemanticForm); } 294 const Expr *getSemanticForm() const { return cast<Expr>(SemanticForm); } 295 296 struct DecomposedForm { 297 /// The original opcode, prior to rewriting. 298 BinaryOperatorKind Opcode; 299 /// The original left-hand side. 300 const Expr *LHS; 301 /// The original right-hand side. 302 const Expr *RHS; 303 /// The inner \c == or \c <=> operator expression. 304 const Expr *InnerBinOp; 305 }; 306 307 /// Decompose this operator into its syntactic form. 308 DecomposedForm getDecomposedForm() const LLVM_READONLY; 309 310 /// Determine whether this expression was rewritten in reverse form. 311 bool isReversed() const { return CXXRewrittenBinaryOperatorBits.IsReversed; } 312 313 BinaryOperatorKind getOperator() const { return getDecomposedForm().Opcode; } 314 const Expr *getLHS() const { return getDecomposedForm().LHS; } 315 const Expr *getRHS() const { return getDecomposedForm().RHS; } 316 317 SourceLocation getOperatorLoc() const LLVM_READONLY { 318 return getDecomposedForm().InnerBinOp->getExprLoc(); 319 } 320 SourceLocation getExprLoc() const LLVM_READONLY { return getOperatorLoc(); } 321 322 /// Compute the begin and end locations from the decomposed form. 323 /// The locations of the semantic form are not reliable if this is 324 /// a reversed expression. 325 //@{ 326 SourceLocation getBeginLoc() const LLVM_READONLY { 327 return getDecomposedForm().LHS->getBeginLoc(); 328 } 329 SourceLocation getEndLoc() const LLVM_READONLY { 330 return getDecomposedForm().RHS->getEndLoc(); 331 } 332 SourceRange getSourceRange() const LLVM_READONLY { 333 DecomposedForm DF = getDecomposedForm(); 334 return SourceRange(DF.LHS->getBeginLoc(), DF.RHS->getEndLoc()); 335 } 336 //@} 337 338 child_range children() { 339 return child_range(&SemanticForm, &SemanticForm + 1); 340 } 341 342 static bool classof(const Stmt *T) { 343 return T->getStmtClass() == CXXRewrittenBinaryOperatorClass; 344 } 345}; 346 347/// Abstract class common to all of the C++ "named"/"keyword" casts. 348/// 349/// This abstract class is inherited by all of the classes 350/// representing "named" casts: CXXStaticCastExpr for \c static_cast, 351/// CXXDynamicCastExpr for \c dynamic_cast, CXXReinterpretCastExpr for 352/// reinterpret_cast, and CXXConstCastExpr for \c const_cast. 353class CXXNamedCastExpr : public ExplicitCastExpr { 354private: 355 // the location of the casting op 356 SourceLocation Loc; 357 358 // the location of the right parenthesis 359 SourceLocation RParenLoc; 360 361 // range for '<' '>' 362 SourceRange AngleBrackets; 363 364protected: 365 friend class ASTStmtReader; 366 367 CXXNamedCastExpr(StmtClass SC, QualType ty, ExprValueKind VK, 368 CastKind kind, Expr *op, unsigned PathSize, 369 TypeSourceInfo *writtenTy, SourceLocation l, 370 SourceLocation RParenLoc, 371 SourceRange AngleBrackets) 372 : ExplicitCastExpr(SC, ty, VK, kind, op, PathSize, writtenTy), Loc(l), 373 RParenLoc(RParenLoc), AngleBrackets(AngleBrackets) {} 374 375 explicit CXXNamedCastExpr(StmtClass SC, EmptyShell Shell, unsigned PathSize) 376 : ExplicitCastExpr(SC, Shell, PathSize) {} 377 378public: 379 const char *getCastName() const; 380 381 /// Retrieve the location of the cast operator keyword, e.g., 382 /// \c static_cast. 383 SourceLocation getOperatorLoc() const { return Loc; } 384 385 /// Retrieve the location of the closing parenthesis. 386 SourceLocation getRParenLoc() const { return RParenLoc; } 387 388 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 389 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 390 SourceRange getAngleBrackets() const LLVM_READONLY { return AngleBrackets; } 391 392 static bool classof(const Stmt *T) { 393 switch (T->getStmtClass()) { 394 case CXXStaticCastExprClass: 395 case CXXDynamicCastExprClass: 396 case CXXReinterpretCastExprClass: 397 case CXXConstCastExprClass: 398 return true; 399 default: 400 return false; 401 } 402 } 403}; 404 405/// A C++ \c static_cast expression (C++ [expr.static.cast]). 406/// 407/// This expression node represents a C++ static cast, e.g., 408/// \c static_cast<int>(1.0). 409class CXXStaticCastExpr final 410 : public CXXNamedCastExpr, 411 private llvm::TrailingObjects<CXXStaticCastExpr, CXXBaseSpecifier *> { 412 CXXStaticCastExpr(QualType ty, ExprValueKind vk, CastKind kind, Expr *op, 413 unsigned pathSize, TypeSourceInfo *writtenTy, 414 SourceLocation l, SourceLocation RParenLoc, 415 SourceRange AngleBrackets) 416 : CXXNamedCastExpr(CXXStaticCastExprClass, ty, vk, kind, op, pathSize, 417 writtenTy, l, RParenLoc, AngleBrackets) {} 418 419 explicit CXXStaticCastExpr(EmptyShell Empty, unsigned PathSize) 420 : CXXNamedCastExpr(CXXStaticCastExprClass, Empty, PathSize) {} 421 422public: 423 friend class CastExpr; 424 friend TrailingObjects; 425 426 static CXXStaticCastExpr *Create(const ASTContext &Context, QualType T, 427 ExprValueKind VK, CastKind K, Expr *Op, 428 const CXXCastPath *Path, 429 TypeSourceInfo *Written, SourceLocation L, 430 SourceLocation RParenLoc, 431 SourceRange AngleBrackets); 432 static CXXStaticCastExpr *CreateEmpty(const ASTContext &Context, 433 unsigned PathSize); 434 435 static bool classof(const Stmt *T) { 436 return T->getStmtClass() == CXXStaticCastExprClass; 437 } 438}; 439 440/// A C++ @c dynamic_cast expression (C++ [expr.dynamic.cast]). 441/// 442/// This expression node represents a dynamic cast, e.g., 443/// \c dynamic_cast<Derived*>(BasePtr). Such a cast may perform a run-time 444/// check to determine how to perform the type conversion. 445class CXXDynamicCastExpr final 446 : public CXXNamedCastExpr, 447 private llvm::TrailingObjects<CXXDynamicCastExpr, CXXBaseSpecifier *> { 448 CXXDynamicCastExpr(QualType ty, ExprValueKind VK, CastKind kind, 449 Expr *op, unsigned pathSize, TypeSourceInfo *writtenTy, 450 SourceLocation l, SourceLocation RParenLoc, 451 SourceRange AngleBrackets) 452 : CXXNamedCastExpr(CXXDynamicCastExprClass, ty, VK, kind, op, pathSize, 453 writtenTy, l, RParenLoc, AngleBrackets) {} 454 455 explicit CXXDynamicCastExpr(EmptyShell Empty, unsigned pathSize) 456 : CXXNamedCastExpr(CXXDynamicCastExprClass, Empty, pathSize) {} 457 458public: 459 friend class CastExpr; 460 friend TrailingObjects; 461 462 static CXXDynamicCastExpr *Create(const ASTContext &Context, QualType T, 463 ExprValueKind VK, CastKind Kind, Expr *Op, 464 const CXXCastPath *Path, 465 TypeSourceInfo *Written, SourceLocation L, 466 SourceLocation RParenLoc, 467 SourceRange AngleBrackets); 468 469 static CXXDynamicCastExpr *CreateEmpty(const ASTContext &Context, 470 unsigned pathSize); 471 472 bool isAlwaysNull() const; 473 474 static bool classof(const Stmt *T) { 475 return T->getStmtClass() == CXXDynamicCastExprClass; 476 } 477}; 478 479/// A C++ @c reinterpret_cast expression (C++ [expr.reinterpret.cast]). 480/// 481/// This expression node represents a reinterpret cast, e.g., 482/// @c reinterpret_cast<int>(VoidPtr). 483/// 484/// A reinterpret_cast provides a differently-typed view of a value but 485/// (in Clang, as in most C++ implementations) performs no actual work at 486/// run time. 487class CXXReinterpretCastExpr final 488 : public CXXNamedCastExpr, 489 private llvm::TrailingObjects<CXXReinterpretCastExpr, 490 CXXBaseSpecifier *> { 491 CXXReinterpretCastExpr(QualType ty, ExprValueKind vk, CastKind kind, 492 Expr *op, unsigned pathSize, 493 TypeSourceInfo *writtenTy, SourceLocation l, 494 SourceLocation RParenLoc, 495 SourceRange AngleBrackets) 496 : CXXNamedCastExpr(CXXReinterpretCastExprClass, ty, vk, kind, op, 497 pathSize, writtenTy, l, RParenLoc, AngleBrackets) {} 498 499 CXXReinterpretCastExpr(EmptyShell Empty, unsigned pathSize) 500 : CXXNamedCastExpr(CXXReinterpretCastExprClass, Empty, pathSize) {} 501 502public: 503 friend class CastExpr; 504 friend TrailingObjects; 505 506 static CXXReinterpretCastExpr *Create(const ASTContext &Context, QualType T, 507 ExprValueKind VK, CastKind Kind, 508 Expr *Op, const CXXCastPath *Path, 509 TypeSourceInfo *WrittenTy, SourceLocation L, 510 SourceLocation RParenLoc, 511 SourceRange AngleBrackets); 512 static CXXReinterpretCastExpr *CreateEmpty(const ASTContext &Context, 513 unsigned pathSize); 514 515 static bool classof(const Stmt *T) { 516 return T->getStmtClass() == CXXReinterpretCastExprClass; 517 } 518}; 519 520/// A C++ \c const_cast expression (C++ [expr.const.cast]). 521/// 522/// This expression node represents a const cast, e.g., 523/// \c const_cast<char*>(PtrToConstChar). 524/// 525/// A const_cast can remove type qualifiers but does not change the underlying 526/// value. 527class CXXConstCastExpr final 528 : public CXXNamedCastExpr, 529 private llvm::TrailingObjects<CXXConstCastExpr, CXXBaseSpecifier *> { 530 CXXConstCastExpr(QualType ty, ExprValueKind VK, Expr *op, 531 TypeSourceInfo *writtenTy, SourceLocation l, 532 SourceLocation RParenLoc, SourceRange AngleBrackets) 533 : CXXNamedCastExpr(CXXConstCastExprClass, ty, VK, CK_NoOp, op, 534 0, writtenTy, l, RParenLoc, AngleBrackets) {} 535 536 explicit CXXConstCastExpr(EmptyShell Empty) 537 : CXXNamedCastExpr(CXXConstCastExprClass, Empty, 0) {} 538 539public: 540 friend class CastExpr; 541 friend TrailingObjects; 542 543 static CXXConstCastExpr *Create(const ASTContext &Context, QualType T, 544 ExprValueKind VK, Expr *Op, 545 TypeSourceInfo *WrittenTy, SourceLocation L, 546 SourceLocation RParenLoc, 547 SourceRange AngleBrackets); 548 static CXXConstCastExpr *CreateEmpty(const ASTContext &Context); 549 550 static bool classof(const Stmt *T) { 551 return T->getStmtClass() == CXXConstCastExprClass; 552 } 553}; 554 555/// A call to a literal operator (C++11 [over.literal]) 556/// written as a user-defined literal (C++11 [lit.ext]). 557/// 558/// Represents a user-defined literal, e.g. "foo"_bar or 1.23_xyz. While this 559/// is semantically equivalent to a normal call, this AST node provides better 560/// information about the syntactic representation of the literal. 561/// 562/// Since literal operators are never found by ADL and can only be declared at 563/// namespace scope, a user-defined literal is never dependent. 564class UserDefinedLiteral final : public CallExpr { 565 friend class ASTStmtReader; 566 friend class ASTStmtWriter; 567 568 /// The location of a ud-suffix within the literal. 569 SourceLocation UDSuffixLoc; 570 571 // UserDefinedLiteral has some trailing objects belonging 572 // to CallExpr. See CallExpr for the details. 573 574 UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args, QualType Ty, 575 ExprValueKind VK, SourceLocation LitEndLoc, 576 SourceLocation SuffixLoc); 577 578 UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty); 579 580public: 581 static UserDefinedLiteral *Create(const ASTContext &Ctx, Expr *Fn, 582 ArrayRef<Expr *> Args, QualType Ty, 583 ExprValueKind VK, SourceLocation LitEndLoc, 584 SourceLocation SuffixLoc); 585 586 static UserDefinedLiteral *CreateEmpty(const ASTContext &Ctx, 587 unsigned NumArgs, EmptyShell Empty); 588 589 /// The kind of literal operator which is invoked. 590 enum LiteralOperatorKind { 591 /// Raw form: operator "" X (const char *) 592 LOK_Raw, 593 594 /// Raw form: operator "" X<cs...> () 595 LOK_Template, 596 597 /// operator "" X (unsigned long long) 598 LOK_Integer, 599 600 /// operator "" X (long double) 601 LOK_Floating, 602 603 /// operator "" X (const CharT *, size_t) 604 LOK_String, 605 606 /// operator "" X (CharT) 607 LOK_Character 608 }; 609 610 /// Returns the kind of literal operator invocation 611 /// which this expression represents. 612 LiteralOperatorKind getLiteralOperatorKind() const; 613 614 /// If this is not a raw user-defined literal, get the 615 /// underlying cooked literal (representing the literal with the suffix 616 /// removed). 617 Expr *getCookedLiteral(); 618 const Expr *getCookedLiteral() const { 619 return const_cast<UserDefinedLiteral*>(this)->getCookedLiteral(); 620 } 621 622 SourceLocation getBeginLoc() const { 623 if (getLiteralOperatorKind() == LOK_Template) 624 return getRParenLoc(); 625 return getArg(0)->getBeginLoc(); 626 } 627 628 SourceLocation getEndLoc() const { return getRParenLoc(); } 629 630 /// Returns the location of a ud-suffix in the expression. 631 /// 632 /// For a string literal, there may be multiple identical suffixes. This 633 /// returns the first. 634 SourceLocation getUDSuffixLoc() const { return UDSuffixLoc; } 635 636 /// Returns the ud-suffix specified for this literal. 637 const IdentifierInfo *getUDSuffix() const; 638 639 static bool classof(const Stmt *S) { 640 return S->getStmtClass() == UserDefinedLiteralClass; 641 } 642}; 643 644/// A boolean literal, per ([C++ lex.bool] Boolean literals). 645class CXXBoolLiteralExpr : public Expr { 646public: 647 CXXBoolLiteralExpr(bool Val, QualType Ty, SourceLocation Loc) 648 : Expr(CXXBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 649 false, false) { 650 CXXBoolLiteralExprBits.Value = Val; 651 CXXBoolLiteralExprBits.Loc = Loc; 652 } 653 654 explicit CXXBoolLiteralExpr(EmptyShell Empty) 655 : Expr(CXXBoolLiteralExprClass, Empty) {} 656 657 bool getValue() const { return CXXBoolLiteralExprBits.Value; } 658 void setValue(bool V) { CXXBoolLiteralExprBits.Value = V; } 659 660 SourceLocation getBeginLoc() const { return getLocation(); } 661 SourceLocation getEndLoc() const { return getLocation(); } 662 663 SourceLocation getLocation() const { return CXXBoolLiteralExprBits.Loc; } 664 void setLocation(SourceLocation L) { CXXBoolLiteralExprBits.Loc = L; } 665 666 static bool classof(const Stmt *T) { 667 return T->getStmtClass() == CXXBoolLiteralExprClass; 668 } 669 670 // Iterators 671 child_range children() { 672 return child_range(child_iterator(), child_iterator()); 673 } 674 675 const_child_range children() const { 676 return const_child_range(const_child_iterator(), const_child_iterator()); 677 } 678}; 679 680/// The null pointer literal (C++11 [lex.nullptr]) 681/// 682/// Introduced in C++11, the only literal of type \c nullptr_t is \c nullptr. 683class CXXNullPtrLiteralExpr : public Expr { 684public: 685 CXXNullPtrLiteralExpr(QualType Ty, SourceLocation Loc) 686 : Expr(CXXNullPtrLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, 687 false, false, false) { 688 CXXNullPtrLiteralExprBits.Loc = Loc; 689 } 690 691 explicit CXXNullPtrLiteralExpr(EmptyShell Empty) 692 : Expr(CXXNullPtrLiteralExprClass, Empty) {} 693 694 SourceLocation getBeginLoc() const { return getLocation(); } 695 SourceLocation getEndLoc() const { return getLocation(); } 696 697 SourceLocation getLocation() const { return CXXNullPtrLiteralExprBits.Loc; } 698 void setLocation(SourceLocation L) { CXXNullPtrLiteralExprBits.Loc = L; } 699 700 static bool classof(const Stmt *T) { 701 return T->getStmtClass() == CXXNullPtrLiteralExprClass; 702 } 703 704 child_range children() { 705 return child_range(child_iterator(), child_iterator()); 706 } 707 708 const_child_range children() const { 709 return const_child_range(const_child_iterator(), const_child_iterator()); 710 } 711}; 712 713/// Implicit construction of a std::initializer_list<T> object from an 714/// array temporary within list-initialization (C++11 [dcl.init.list]p5). 715class CXXStdInitializerListExpr : public Expr { 716 Stmt *SubExpr = nullptr; 717 718 CXXStdInitializerListExpr(EmptyShell Empty) 719 : Expr(CXXStdInitializerListExprClass, Empty) {} 720 721public: 722 friend class ASTReader; 723 friend class ASTStmtReader; 724 725 CXXStdInitializerListExpr(QualType Ty, Expr *SubExpr) 726 : Expr(CXXStdInitializerListExprClass, Ty, VK_RValue, OK_Ordinary, 727 Ty->isDependentType(), SubExpr->isValueDependent(), 728 SubExpr->isInstantiationDependent(), 729 SubExpr->containsUnexpandedParameterPack()), 730 SubExpr(SubExpr) {} 731 732 Expr *getSubExpr() { return static_cast<Expr*>(SubExpr); } 733 const Expr *getSubExpr() const { return static_cast<const Expr*>(SubExpr); } 734 735 SourceLocation getBeginLoc() const LLVM_READONLY { 736 return SubExpr->getBeginLoc(); 737 } 738 739 SourceLocation getEndLoc() const LLVM_READONLY { 740 return SubExpr->getEndLoc(); 741 } 742 743 /// Retrieve the source range of the expression. 744 SourceRange getSourceRange() const LLVM_READONLY { 745 return SubExpr->getSourceRange(); 746 } 747 748 static bool classof(const Stmt *S) { 749 return S->getStmtClass() == CXXStdInitializerListExprClass; 750 } 751 752 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 753 754 const_child_range children() const { 755 return const_child_range(&SubExpr, &SubExpr + 1); 756 } 757}; 758 759/// A C++ \c typeid expression (C++ [expr.typeid]), which gets 760/// the \c type_info that corresponds to the supplied type, or the (possibly 761/// dynamic) type of the supplied expression. 762/// 763/// This represents code like \c typeid(int) or \c typeid(*objPtr) 764class CXXTypeidExpr : public Expr { 765private: 766 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 767 SourceRange Range; 768 769public: 770 CXXTypeidExpr(QualType Ty, TypeSourceInfo *Operand, SourceRange R) 771 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 772 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 773 false, 774 // typeid is value-dependent if the type or expression are 775 // dependent 776 Operand->getType()->isDependentType(), 777 Operand->getType()->isInstantiationDependentType(), 778 Operand->getType()->containsUnexpandedParameterPack()), 779 Operand(Operand), Range(R) {} 780 781 CXXTypeidExpr(QualType Ty, Expr *Operand, SourceRange R) 782 : Expr(CXXTypeidExprClass, Ty, VK_LValue, OK_Ordinary, 783 // typeid is never type-dependent (C++ [temp.dep.expr]p4) 784 false, 785 // typeid is value-dependent if the type or expression are 786 // dependent 787 Operand->isTypeDependent() || Operand->isValueDependent(), 788 Operand->isInstantiationDependent(), 789 Operand->containsUnexpandedParameterPack()), 790 Operand(Operand), Range(R) {} 791 792 CXXTypeidExpr(EmptyShell Empty, bool isExpr) 793 : Expr(CXXTypeidExprClass, Empty) { 794 if (isExpr) 795 Operand = (Expr*)nullptr; 796 else 797 Operand = (TypeSourceInfo*)nullptr; 798 } 799 800 /// Determine whether this typeid has a type operand which is potentially 801 /// evaluated, per C++11 [expr.typeid]p3. 802 bool isPotentiallyEvaluated() const; 803 804 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 805 806 /// Retrieves the type operand of this typeid() expression after 807 /// various required adjustments (removing reference types, cv-qualifiers). 808 QualType getTypeOperand(ASTContext &Context) const; 809 810 /// Retrieve source information for the type operand. 811 TypeSourceInfo *getTypeOperandSourceInfo() const { 812 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 813 return Operand.get<TypeSourceInfo *>(); 814 } 815 816 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 817 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)"); 818 Operand = TSI; 819 } 820 821 Expr *getExprOperand() const { 822 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 823 return static_cast<Expr*>(Operand.get<Stmt *>()); 824 } 825 826 void setExprOperand(Expr *E) { 827 assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)"); 828 Operand = E; 829 } 830 831 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } 832 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 833 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 834 void setSourceRange(SourceRange R) { Range = R; } 835 836 static bool classof(const Stmt *T) { 837 return T->getStmtClass() == CXXTypeidExprClass; 838 } 839 840 // Iterators 841 child_range children() { 842 if (isTypeOperand()) 843 return child_range(child_iterator(), child_iterator()); 844 auto **begin = reinterpret_cast<Stmt **>(&Operand); 845 return child_range(begin, begin + 1); 846 } 847 848 const_child_range children() const { 849 if (isTypeOperand()) 850 return const_child_range(const_child_iterator(), const_child_iterator()); 851 852 auto **begin = 853 reinterpret_cast<Stmt **>(&const_cast<CXXTypeidExpr *>(this)->Operand); 854 return const_child_range(begin, begin + 1); 855 } 856}; 857 858/// A member reference to an MSPropertyDecl. 859/// 860/// This expression always has pseudo-object type, and therefore it is 861/// typically not encountered in a fully-typechecked expression except 862/// within the syntactic form of a PseudoObjectExpr. 863class MSPropertyRefExpr : public Expr { 864 Expr *BaseExpr; 865 MSPropertyDecl *TheDecl; 866 SourceLocation MemberLoc; 867 bool IsArrow; 868 NestedNameSpecifierLoc QualifierLoc; 869 870public: 871 friend class ASTStmtReader; 872 873 MSPropertyRefExpr(Expr *baseExpr, MSPropertyDecl *decl, bool isArrow, 874 QualType ty, ExprValueKind VK, 875 NestedNameSpecifierLoc qualifierLoc, 876 SourceLocation nameLoc) 877 : Expr(MSPropertyRefExprClass, ty, VK, OK_Ordinary, 878 /*type-dependent*/ false, baseExpr->isValueDependent(), 879 baseExpr->isInstantiationDependent(), 880 baseExpr->containsUnexpandedParameterPack()), 881 BaseExpr(baseExpr), TheDecl(decl), 882 MemberLoc(nameLoc), IsArrow(isArrow), 883 QualifierLoc(qualifierLoc) {} 884 885 MSPropertyRefExpr(EmptyShell Empty) : Expr(MSPropertyRefExprClass, Empty) {} 886 887 SourceRange getSourceRange() const LLVM_READONLY { 888 return SourceRange(getBeginLoc(), getEndLoc()); 889 } 890 891 bool isImplicitAccess() const { 892 return getBaseExpr() && getBaseExpr()->isImplicitCXXThis(); 893 } 894 895 SourceLocation getBeginLoc() const { 896 if (!isImplicitAccess()) 897 return BaseExpr->getBeginLoc(); 898 else if (QualifierLoc) 899 return QualifierLoc.getBeginLoc(); 900 else 901 return MemberLoc; 902 } 903 904 SourceLocation getEndLoc() const { return getMemberLoc(); } 905 906 child_range children() { 907 return child_range((Stmt**)&BaseExpr, (Stmt**)&BaseExpr + 1); 908 } 909 910 const_child_range children() const { 911 auto Children = const_cast<MSPropertyRefExpr *>(this)->children(); 912 return const_child_range(Children.begin(), Children.end()); 913 } 914 915 static bool classof(const Stmt *T) { 916 return T->getStmtClass() == MSPropertyRefExprClass; 917 } 918 919 Expr *getBaseExpr() const { return BaseExpr; } 920 MSPropertyDecl *getPropertyDecl() const { return TheDecl; } 921 bool isArrow() const { return IsArrow; } 922 SourceLocation getMemberLoc() const { return MemberLoc; } 923 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 924}; 925 926/// MS property subscript expression. 927/// MSVC supports 'property' attribute and allows to apply it to the 928/// declaration of an empty array in a class or structure definition. 929/// For example: 930/// \code 931/// __declspec(property(get=GetX, put=PutX)) int x[]; 932/// \endcode 933/// The above statement indicates that x[] can be used with one or more array 934/// indices. In this case, i=p->x[a][b] will be turned into i=p->GetX(a, b), and 935/// p->x[a][b] = i will be turned into p->PutX(a, b, i). 936/// This is a syntactic pseudo-object expression. 937class MSPropertySubscriptExpr : public Expr { 938 friend class ASTStmtReader; 939 940 enum { BASE_EXPR, IDX_EXPR, NUM_SUBEXPRS = 2 }; 941 942 Stmt *SubExprs[NUM_SUBEXPRS]; 943 SourceLocation RBracketLoc; 944 945 void setBase(Expr *Base) { SubExprs[BASE_EXPR] = Base; } 946 void setIdx(Expr *Idx) { SubExprs[IDX_EXPR] = Idx; } 947 948public: 949 MSPropertySubscriptExpr(Expr *Base, Expr *Idx, QualType Ty, ExprValueKind VK, 950 ExprObjectKind OK, SourceLocation RBracketLoc) 951 : Expr(MSPropertySubscriptExprClass, Ty, VK, OK, Idx->isTypeDependent(), 952 Idx->isValueDependent(), Idx->isInstantiationDependent(), 953 Idx->containsUnexpandedParameterPack()), 954 RBracketLoc(RBracketLoc) { 955 SubExprs[BASE_EXPR] = Base; 956 SubExprs[IDX_EXPR] = Idx; 957 } 958 959 /// Create an empty array subscript expression. 960 explicit MSPropertySubscriptExpr(EmptyShell Shell) 961 : Expr(MSPropertySubscriptExprClass, Shell) {} 962 963 Expr *getBase() { return cast<Expr>(SubExprs[BASE_EXPR]); } 964 const Expr *getBase() const { return cast<Expr>(SubExprs[BASE_EXPR]); } 965 966 Expr *getIdx() { return cast<Expr>(SubExprs[IDX_EXPR]); } 967 const Expr *getIdx() const { return cast<Expr>(SubExprs[IDX_EXPR]); } 968 969 SourceLocation getBeginLoc() const LLVM_READONLY { 970 return getBase()->getBeginLoc(); 971 } 972 973 SourceLocation getEndLoc() const LLVM_READONLY { return RBracketLoc; } 974 975 SourceLocation getRBracketLoc() const { return RBracketLoc; } 976 void setRBracketLoc(SourceLocation L) { RBracketLoc = L; } 977 978 SourceLocation getExprLoc() const LLVM_READONLY { 979 return getBase()->getExprLoc(); 980 } 981 982 static bool classof(const Stmt *T) { 983 return T->getStmtClass() == MSPropertySubscriptExprClass; 984 } 985 986 // Iterators 987 child_range children() { 988 return child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS); 989 } 990 991 const_child_range children() const { 992 return const_child_range(&SubExprs[0], &SubExprs[0] + NUM_SUBEXPRS); 993 } 994}; 995 996/// A Microsoft C++ @c __uuidof expression, which gets 997/// the _GUID that corresponds to the supplied type or expression. 998/// 999/// This represents code like @c __uuidof(COMTYPE) or @c __uuidof(*comPtr) 1000class CXXUuidofExpr : public Expr { 1001private: 1002 llvm::PointerUnion<Stmt *, TypeSourceInfo *> Operand; 1003 StringRef UuidStr; 1004 SourceRange Range; 1005 1006public: 1007 CXXUuidofExpr(QualType Ty, TypeSourceInfo *Operand, StringRef UuidStr, 1008 SourceRange R) 1009 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false, 1010 Operand->getType()->isDependentType(), 1011 Operand->getType()->isInstantiationDependentType(), 1012 Operand->getType()->containsUnexpandedParameterPack()), 1013 Operand(Operand), UuidStr(UuidStr), Range(R) {} 1014 1015 CXXUuidofExpr(QualType Ty, Expr *Operand, StringRef UuidStr, SourceRange R) 1016 : Expr(CXXUuidofExprClass, Ty, VK_LValue, OK_Ordinary, false, 1017 Operand->isTypeDependent(), Operand->isInstantiationDependent(), 1018 Operand->containsUnexpandedParameterPack()), 1019 Operand(Operand), UuidStr(UuidStr), Range(R) {} 1020 1021 CXXUuidofExpr(EmptyShell Empty, bool isExpr) 1022 : Expr(CXXUuidofExprClass, Empty) { 1023 if (isExpr) 1024 Operand = (Expr*)nullptr; 1025 else 1026 Operand = (TypeSourceInfo*)nullptr; 1027 } 1028 1029 bool isTypeOperand() const { return Operand.is<TypeSourceInfo *>(); } 1030 1031 /// Retrieves the type operand of this __uuidof() expression after 1032 /// various required adjustments (removing reference types, cv-qualifiers). 1033 QualType getTypeOperand(ASTContext &Context) const; 1034 1035 /// Retrieve source information for the type operand. 1036 TypeSourceInfo *getTypeOperandSourceInfo() const { 1037 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 1038 return Operand.get<TypeSourceInfo *>(); 1039 } 1040 1041 void setTypeOperandSourceInfo(TypeSourceInfo *TSI) { 1042 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)"); 1043 Operand = TSI; 1044 } 1045 1046 Expr *getExprOperand() const { 1047 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 1048 return static_cast<Expr*>(Operand.get<Stmt *>()); 1049 } 1050 1051 void setExprOperand(Expr *E) { 1052 assert(!isTypeOperand() && "Cannot call getExprOperand for __uuidof(type)"); 1053 Operand = E; 1054 } 1055 1056 void setUuidStr(StringRef US) { UuidStr = US; } 1057 StringRef getUuidStr() const { return UuidStr; } 1058 1059 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } 1060 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 1061 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 1062 void setSourceRange(SourceRange R) { Range = R; } 1063 1064 static bool classof(const Stmt *T) { 1065 return T->getStmtClass() == CXXUuidofExprClass; 1066 } 1067 1068 // Iterators 1069 child_range children() { 1070 if (isTypeOperand()) 1071 return child_range(child_iterator(), child_iterator()); 1072 auto **begin = reinterpret_cast<Stmt **>(&Operand); 1073 return child_range(begin, begin + 1); 1074 } 1075 1076 const_child_range children() const { 1077 if (isTypeOperand()) 1078 return const_child_range(const_child_iterator(), const_child_iterator()); 1079 auto **begin = 1080 reinterpret_cast<Stmt **>(&const_cast<CXXUuidofExpr *>(this)->Operand); 1081 return const_child_range(begin, begin + 1); 1082 } 1083}; 1084 1085/// Represents the \c this expression in C++. 1086/// 1087/// This is a pointer to the object on which the current member function is 1088/// executing (C++ [expr.prim]p3). Example: 1089/// 1090/// \code 1091/// class Foo { 1092/// public: 1093/// void bar(); 1094/// void test() { this->bar(); } 1095/// }; 1096/// \endcode 1097class CXXThisExpr : public Expr { 1098public: 1099 CXXThisExpr(SourceLocation L, QualType Ty, bool IsImplicit) 1100 : Expr(CXXThisExprClass, Ty, VK_RValue, OK_Ordinary, 1101 // 'this' is type-dependent if the class type of the enclosing 1102 // member function is dependent (C++ [temp.dep.expr]p2) 1103 Ty->isDependentType(), Ty->isDependentType(), 1104 Ty->isInstantiationDependentType(), 1105 /*ContainsUnexpandedParameterPack=*/false) { 1106 CXXThisExprBits.IsImplicit = IsImplicit; 1107 CXXThisExprBits.Loc = L; 1108 } 1109 1110 CXXThisExpr(EmptyShell Empty) : Expr(CXXThisExprClass, Empty) {} 1111 1112 SourceLocation getLocation() const { return CXXThisExprBits.Loc; } 1113 void setLocation(SourceLocation L) { CXXThisExprBits.Loc = L; } 1114 1115 SourceLocation getBeginLoc() const { return getLocation(); } 1116 SourceLocation getEndLoc() const { return getLocation(); } 1117 1118 bool isImplicit() const { return CXXThisExprBits.IsImplicit; } 1119 void setImplicit(bool I) { CXXThisExprBits.IsImplicit = I; } 1120 1121 static bool classof(const Stmt *T) { 1122 return T->getStmtClass() == CXXThisExprClass; 1123 } 1124 1125 // Iterators 1126 child_range children() { 1127 return child_range(child_iterator(), child_iterator()); 1128 } 1129 1130 const_child_range children() const { 1131 return const_child_range(const_child_iterator(), const_child_iterator()); 1132 } 1133}; 1134 1135/// A C++ throw-expression (C++ [except.throw]). 1136/// 1137/// This handles 'throw' (for re-throwing the current exception) and 1138/// 'throw' assignment-expression. When assignment-expression isn't 1139/// present, Op will be null. 1140class CXXThrowExpr : public Expr { 1141 friend class ASTStmtReader; 1142 1143 /// The optional expression in the throw statement. 1144 Stmt *Operand; 1145 1146public: 1147 // \p Ty is the void type which is used as the result type of the 1148 // expression. The \p Loc is the location of the throw keyword. 1149 // \p Operand is the expression in the throw statement, and can be 1150 // null if not present. 1151 CXXThrowExpr(Expr *Operand, QualType Ty, SourceLocation Loc, 1152 bool IsThrownVariableInScope) 1153 : Expr(CXXThrowExprClass, Ty, VK_RValue, OK_Ordinary, false, false, 1154 Operand && Operand->isInstantiationDependent(), 1155 Operand && Operand->containsUnexpandedParameterPack()), 1156 Operand(Operand) { 1157 CXXThrowExprBits.ThrowLoc = Loc; 1158 CXXThrowExprBits.IsThrownVariableInScope = IsThrownVariableInScope; 1159 } 1160 CXXThrowExpr(EmptyShell Empty) : Expr(CXXThrowExprClass, Empty) {} 1161 1162 const Expr *getSubExpr() const { return cast_or_null<Expr>(Operand); } 1163 Expr *getSubExpr() { return cast_or_null<Expr>(Operand); } 1164 1165 SourceLocation getThrowLoc() const { return CXXThrowExprBits.ThrowLoc; } 1166 1167 /// Determines whether the variable thrown by this expression (if any!) 1168 /// is within the innermost try block. 1169 /// 1170 /// This information is required to determine whether the NRVO can apply to 1171 /// this variable. 1172 bool isThrownVariableInScope() const { 1173 return CXXThrowExprBits.IsThrownVariableInScope; 1174 } 1175 1176 SourceLocation getBeginLoc() const { return getThrowLoc(); } 1177 SourceLocation getEndLoc() const LLVM_READONLY { 1178 if (!getSubExpr()) 1179 return getThrowLoc(); 1180 return getSubExpr()->getEndLoc(); 1181 } 1182 1183 static bool classof(const Stmt *T) { 1184 return T->getStmtClass() == CXXThrowExprClass; 1185 } 1186 1187 // Iterators 1188 child_range children() { 1189 return child_range(&Operand, Operand ? &Operand + 1 : &Operand); 1190 } 1191 1192 const_child_range children() const { 1193 return const_child_range(&Operand, Operand ? &Operand + 1 : &Operand); 1194 } 1195}; 1196 1197/// A default argument (C++ [dcl.fct.default]). 1198/// 1199/// This wraps up a function call argument that was created from the 1200/// corresponding parameter's default argument, when the call did not 1201/// explicitly supply arguments for all of the parameters. 1202class CXXDefaultArgExpr final : public Expr { 1203 friend class ASTStmtReader; 1204 1205 /// The parameter whose default is being used. 1206 ParmVarDecl *Param; 1207 1208 /// The context where the default argument expression was used. 1209 DeclContext *UsedContext; 1210 1211 CXXDefaultArgExpr(StmtClass SC, SourceLocation Loc, ParmVarDecl *Param, 1212 DeclContext *UsedContext) 1213 : Expr(SC, 1214 Param->hasUnparsedDefaultArg() 1215 ? Param->getType().getNonReferenceType() 1216 : Param->getDefaultArg()->getType(), 1217 Param->getDefaultArg()->getValueKind(), 1218 Param->getDefaultArg()->getObjectKind(), false, false, false, 1219 false), 1220 Param(Param), UsedContext(UsedContext) { 1221 CXXDefaultArgExprBits.Loc = Loc; 1222 } 1223 1224public: 1225 CXXDefaultArgExpr(EmptyShell Empty) : Expr(CXXDefaultArgExprClass, Empty) {} 1226 1227 // \p Param is the parameter whose default argument is used by this 1228 // expression. 1229 static CXXDefaultArgExpr *Create(const ASTContext &C, SourceLocation Loc, 1230 ParmVarDecl *Param, 1231 DeclContext *UsedContext) { 1232 return new (C) 1233 CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, UsedContext); 1234 } 1235 1236 // Retrieve the parameter that the argument was created from. 1237 const ParmVarDecl *getParam() const { return Param; } 1238 ParmVarDecl *getParam() { return Param; } 1239 1240 // Retrieve the actual argument to the function call. 1241 const Expr *getExpr() const { return getParam()->getDefaultArg(); } 1242 Expr *getExpr() { return getParam()->getDefaultArg(); } 1243 1244 const DeclContext *getUsedContext() const { return UsedContext; } 1245 DeclContext *getUsedContext() { return UsedContext; } 1246 1247 /// Retrieve the location where this default argument was actually used. 1248 SourceLocation getUsedLocation() const { return CXXDefaultArgExprBits.Loc; } 1249 1250 /// Default argument expressions have no representation in the 1251 /// source, so they have an empty source range. 1252 SourceLocation getBeginLoc() const { return SourceLocation(); } 1253 SourceLocation getEndLoc() const { return SourceLocation(); } 1254 1255 SourceLocation getExprLoc() const { return getUsedLocation(); } 1256 1257 static bool classof(const Stmt *T) { 1258 return T->getStmtClass() == CXXDefaultArgExprClass; 1259 } 1260 1261 // Iterators 1262 child_range children() { 1263 return child_range(child_iterator(), child_iterator()); 1264 } 1265 1266 const_child_range children() const { 1267 return const_child_range(const_child_iterator(), const_child_iterator()); 1268 } 1269}; 1270 1271/// A use of a default initializer in a constructor or in aggregate 1272/// initialization. 1273/// 1274/// This wraps a use of a C++ default initializer (technically, 1275/// a brace-or-equal-initializer for a non-static data member) when it 1276/// is implicitly used in a mem-initializer-list in a constructor 1277/// (C++11 [class.base.init]p8) or in aggregate initialization 1278/// (C++1y [dcl.init.aggr]p7). 1279class CXXDefaultInitExpr : public Expr { 1280 friend class ASTReader; 1281 friend class ASTStmtReader; 1282 1283 /// The field whose default is being used. 1284 FieldDecl *Field; 1285 1286 /// The context where the default initializer expression was used. 1287 DeclContext *UsedContext; 1288 1289 CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc, 1290 FieldDecl *Field, QualType Ty, DeclContext *UsedContext); 1291 1292 CXXDefaultInitExpr(EmptyShell Empty) : Expr(CXXDefaultInitExprClass, Empty) {} 1293 1294public: 1295 /// \p Field is the non-static data member whose default initializer is used 1296 /// by this expression. 1297 static CXXDefaultInitExpr *Create(const ASTContext &Ctx, SourceLocation Loc, 1298 FieldDecl *Field, DeclContext *UsedContext) { 1299 return new (Ctx) CXXDefaultInitExpr(Ctx, Loc, Field, Field->getType(), UsedContext); 1300 } 1301 1302 /// Get the field whose initializer will be used. 1303 FieldDecl *getField() { return Field; } 1304 const FieldDecl *getField() const { return Field; } 1305 1306 /// Get the initialization expression that will be used. 1307 const Expr *getExpr() const { 1308 assert(Field->getInClassInitializer() && "initializer hasn't been parsed"); 1309 return Field->getInClassInitializer(); 1310 } 1311 Expr *getExpr() { 1312 assert(Field->getInClassInitializer() && "initializer hasn't been parsed"); 1313 return Field->getInClassInitializer(); 1314 } 1315 1316 const DeclContext *getUsedContext() const { return UsedContext; } 1317 DeclContext *getUsedContext() { return UsedContext; } 1318 1319 /// Retrieve the location where this default initializer expression was 1320 /// actually used. 1321 SourceLocation getUsedLocation() const { return getBeginLoc(); } 1322 1323 SourceLocation getBeginLoc() const { return CXXDefaultInitExprBits.Loc; } 1324 SourceLocation getEndLoc() const { return CXXDefaultInitExprBits.Loc; } 1325 1326 static bool classof(const Stmt *T) { 1327 return T->getStmtClass() == CXXDefaultInitExprClass; 1328 } 1329 1330 // Iterators 1331 child_range children() { 1332 return child_range(child_iterator(), child_iterator()); 1333 } 1334 1335 const_child_range children() const { 1336 return const_child_range(const_child_iterator(), const_child_iterator()); 1337 } 1338}; 1339 1340/// Represents a C++ temporary. 1341class CXXTemporary { 1342 /// The destructor that needs to be called. 1343 const CXXDestructorDecl *Destructor; 1344 1345 explicit CXXTemporary(const CXXDestructorDecl *destructor) 1346 : Destructor(destructor) {} 1347 1348public: 1349 static CXXTemporary *Create(const ASTContext &C, 1350 const CXXDestructorDecl *Destructor); 1351 1352 const CXXDestructorDecl *getDestructor() const { return Destructor; } 1353 1354 void setDestructor(const CXXDestructorDecl *Dtor) { 1355 Destructor = Dtor; 1356 } 1357}; 1358 1359/// Represents binding an expression to a temporary. 1360/// 1361/// This ensures the destructor is called for the temporary. It should only be 1362/// needed for non-POD, non-trivially destructable class types. For example: 1363/// 1364/// \code 1365/// struct S { 1366/// S() { } // User defined constructor makes S non-POD. 1367/// ~S() { } // User defined destructor makes it non-trivial. 1368/// }; 1369/// void test() { 1370/// const S &s_ref = S(); // Requires a CXXBindTemporaryExpr. 1371/// } 1372/// \endcode 1373class CXXBindTemporaryExpr : public Expr { 1374 CXXTemporary *Temp = nullptr; 1375 Stmt *SubExpr = nullptr; 1376 1377 CXXBindTemporaryExpr(CXXTemporary *temp, Expr* SubExpr) 1378 : Expr(CXXBindTemporaryExprClass, SubExpr->getType(), 1379 VK_RValue, OK_Ordinary, SubExpr->isTypeDependent(), 1380 SubExpr->isValueDependent(), 1381 SubExpr->isInstantiationDependent(), 1382 SubExpr->containsUnexpandedParameterPack()), 1383 Temp(temp), SubExpr(SubExpr) {} 1384 1385public: 1386 CXXBindTemporaryExpr(EmptyShell Empty) 1387 : Expr(CXXBindTemporaryExprClass, Empty) {} 1388 1389 static CXXBindTemporaryExpr *Create(const ASTContext &C, CXXTemporary *Temp, 1390 Expr* SubExpr); 1391 1392 CXXTemporary *getTemporary() { return Temp; } 1393 const CXXTemporary *getTemporary() const { return Temp; } 1394 void setTemporary(CXXTemporary *T) { Temp = T; } 1395 1396 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 1397 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 1398 void setSubExpr(Expr *E) { SubExpr = E; } 1399 1400 SourceLocation getBeginLoc() const LLVM_READONLY { 1401 return SubExpr->getBeginLoc(); 1402 } 1403 1404 SourceLocation getEndLoc() const LLVM_READONLY { 1405 return SubExpr->getEndLoc(); 1406 } 1407 1408 // Implement isa/cast/dyncast/etc. 1409 static bool classof(const Stmt *T) { 1410 return T->getStmtClass() == CXXBindTemporaryExprClass; 1411 } 1412 1413 // Iterators 1414 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 1415 1416 const_child_range children() const { 1417 return const_child_range(&SubExpr, &SubExpr + 1); 1418 } 1419}; 1420 1421/// Represents a call to a C++ constructor. 1422class CXXConstructExpr : public Expr { 1423 friend class ASTStmtReader; 1424 1425public: 1426 enum ConstructionKind { 1427 CK_Complete, 1428 CK_NonVirtualBase, 1429 CK_VirtualBase, 1430 CK_Delegating 1431 }; 1432 1433private: 1434 /// A pointer to the constructor which will be ultimately called. 1435 CXXConstructorDecl *Constructor; 1436 1437 SourceRange ParenOrBraceRange; 1438 1439 /// The number of arguments. 1440 unsigned NumArgs; 1441 1442 // We would like to stash the arguments of the constructor call after 1443 // CXXConstructExpr. However CXXConstructExpr is used as a base class of 1444 // CXXTemporaryObjectExpr which makes the use of llvm::TrailingObjects 1445 // impossible. 1446 // 1447 // Instead we manually stash the trailing object after the full object 1448 // containing CXXConstructExpr (that is either CXXConstructExpr or 1449 // CXXTemporaryObjectExpr). 1450 // 1451 // The trailing objects are: 1452 // 1453 // * An array of getNumArgs() "Stmt *" for the arguments of the 1454 // constructor call. 1455 1456 /// Return a pointer to the start of the trailing arguments. 1457 /// Defined just after CXXTemporaryObjectExpr. 1458 inline Stmt **getTrailingArgs(); 1459 const Stmt *const *getTrailingArgs() const { 1460 return const_cast<CXXConstructExpr *>(this)->getTrailingArgs(); 1461 } 1462 1463protected: 1464 /// Build a C++ construction expression. 1465 CXXConstructExpr(StmtClass SC, QualType Ty, SourceLocation Loc, 1466 CXXConstructorDecl *Ctor, bool Elidable, 1467 ArrayRef<Expr *> Args, bool HadMultipleCandidates, 1468 bool ListInitialization, bool StdInitListInitialization, 1469 bool ZeroInitialization, ConstructionKind ConstructKind, 1470 SourceRange ParenOrBraceRange); 1471 1472 /// Build an empty C++ construction expression. 1473 CXXConstructExpr(StmtClass SC, EmptyShell Empty, unsigned NumArgs); 1474 1475 /// Return the size in bytes of the trailing objects. Used by 1476 /// CXXTemporaryObjectExpr to allocate the right amount of storage. 1477 static unsigned sizeOfTrailingObjects(unsigned NumArgs) { 1478 return NumArgs * sizeof(Stmt *); 1479 } 1480 1481public: 1482 /// Create a C++ construction expression. 1483 static CXXConstructExpr * 1484 Create(const ASTContext &Ctx, QualType Ty, SourceLocation Loc, 1485 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args, 1486 bool HadMultipleCandidates, bool ListInitialization, 1487 bool StdInitListInitialization, bool ZeroInitialization, 1488 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange); 1489 1490 /// Create an empty C++ construction expression. 1491 static CXXConstructExpr *CreateEmpty(const ASTContext &Ctx, unsigned NumArgs); 1492 1493 /// Get the constructor that this expression will (ultimately) call. 1494 CXXConstructorDecl *getConstructor() const { return Constructor; } 1495 1496 SourceLocation getLocation() const { return CXXConstructExprBits.Loc; } 1497 void setLocation(SourceLocation Loc) { CXXConstructExprBits.Loc = Loc; } 1498 1499 /// Whether this construction is elidable. 1500 bool isElidable() const { return CXXConstructExprBits.Elidable; } 1501 void setElidable(bool E) { CXXConstructExprBits.Elidable = E; } 1502 1503 /// Whether the referred constructor was resolved from 1504 /// an overloaded set having size greater than 1. 1505 bool hadMultipleCandidates() const { 1506 return CXXConstructExprBits.HadMultipleCandidates; 1507 } 1508 void setHadMultipleCandidates(bool V) { 1509 CXXConstructExprBits.HadMultipleCandidates = V; 1510 } 1511 1512 /// Whether this constructor call was written as list-initialization. 1513 bool isListInitialization() const { 1514 return CXXConstructExprBits.ListInitialization; 1515 } 1516 void setListInitialization(bool V) { 1517 CXXConstructExprBits.ListInitialization = V; 1518 } 1519 1520 /// Whether this constructor call was written as list-initialization, 1521 /// but was interpreted as forming a std::initializer_list<T> from the list 1522 /// and passing that as a single constructor argument. 1523 /// See C++11 [over.match.list]p1 bullet 1. 1524 bool isStdInitListInitialization() const { 1525 return CXXConstructExprBits.StdInitListInitialization; 1526 } 1527 void setStdInitListInitialization(bool V) { 1528 CXXConstructExprBits.StdInitListInitialization = V; 1529 } 1530 1531 /// Whether this construction first requires 1532 /// zero-initialization before the initializer is called. 1533 bool requiresZeroInitialization() const { 1534 return CXXConstructExprBits.ZeroInitialization; 1535 } 1536 void setRequiresZeroInitialization(bool ZeroInit) { 1537 CXXConstructExprBits.ZeroInitialization = ZeroInit; 1538 } 1539 1540 /// Determine whether this constructor is actually constructing 1541 /// a base class (rather than a complete object). 1542 ConstructionKind getConstructionKind() const { 1543 return static_cast<ConstructionKind>(CXXConstructExprBits.ConstructionKind); 1544 } 1545 void setConstructionKind(ConstructionKind CK) { 1546 CXXConstructExprBits.ConstructionKind = CK; 1547 } 1548 1549 using arg_iterator = ExprIterator; 1550 using const_arg_iterator = ConstExprIterator; 1551 using arg_range = llvm::iterator_range<arg_iterator>; 1552 using const_arg_range = llvm::iterator_range<const_arg_iterator>; 1553 1554 arg_range arguments() { return arg_range(arg_begin(), arg_end()); } 1555 const_arg_range arguments() const { 1556 return const_arg_range(arg_begin(), arg_end()); 1557 } 1558 1559 arg_iterator arg_begin() { return getTrailingArgs(); } 1560 arg_iterator arg_end() { return arg_begin() + getNumArgs(); } 1561 const_arg_iterator arg_begin() const { return getTrailingArgs(); } 1562 const_arg_iterator arg_end() const { return arg_begin() + getNumArgs(); } 1563 1564 Expr **getArgs() { return reinterpret_cast<Expr **>(getTrailingArgs()); } 1565 const Expr *const *getArgs() const { 1566 return reinterpret_cast<const Expr *const *>(getTrailingArgs()); 1567 } 1568 1569 /// Return the number of arguments to the constructor call. 1570 unsigned getNumArgs() const { return NumArgs; } 1571 1572 /// Return the specified argument. 1573 Expr *getArg(unsigned Arg) { 1574 assert(Arg < getNumArgs() && "Arg access out of range!"); 1575 return getArgs()[Arg]; 1576 } 1577 const Expr *getArg(unsigned Arg) const { 1578 assert(Arg < getNumArgs() && "Arg access out of range!"); 1579 return getArgs()[Arg]; 1580 } 1581 1582 /// Set the specified argument. 1583 void setArg(unsigned Arg, Expr *ArgExpr) { 1584 assert(Arg < getNumArgs() && "Arg access out of range!"); 1585 getArgs()[Arg] = ArgExpr; 1586 } 1587 1588 SourceLocation getBeginLoc() const LLVM_READONLY; 1589 SourceLocation getEndLoc() const LLVM_READONLY; 1590 SourceRange getParenOrBraceRange() const { return ParenOrBraceRange; } 1591 void setParenOrBraceRange(SourceRange Range) { ParenOrBraceRange = Range; } 1592 1593 static bool classof(const Stmt *T) { 1594 return T->getStmtClass() == CXXConstructExprClass || 1595 T->getStmtClass() == CXXTemporaryObjectExprClass; 1596 } 1597 1598 // Iterators 1599 child_range children() { 1600 return child_range(getTrailingArgs(), getTrailingArgs() + getNumArgs()); 1601 } 1602 1603 const_child_range children() const { 1604 auto Children = const_cast<CXXConstructExpr *>(this)->children(); 1605 return const_child_range(Children.begin(), Children.end()); 1606 } 1607}; 1608 1609/// Represents a call to an inherited base class constructor from an 1610/// inheriting constructor. This call implicitly forwards the arguments from 1611/// the enclosing context (an inheriting constructor) to the specified inherited 1612/// base class constructor. 1613class CXXInheritedCtorInitExpr : public Expr { 1614private: 1615 CXXConstructorDecl *Constructor = nullptr; 1616 1617 /// The location of the using declaration. 1618 SourceLocation Loc; 1619 1620 /// Whether this is the construction of a virtual base. 1621 unsigned ConstructsVirtualBase : 1; 1622 1623 /// Whether the constructor is inherited from a virtual base class of the 1624 /// class that we construct. 1625 unsigned InheritedFromVirtualBase : 1; 1626 1627public: 1628 friend class ASTStmtReader; 1629 1630 /// Construct a C++ inheriting construction expression. 1631 CXXInheritedCtorInitExpr(SourceLocation Loc, QualType T, 1632 CXXConstructorDecl *Ctor, bool ConstructsVirtualBase, 1633 bool InheritedFromVirtualBase) 1634 : Expr(CXXInheritedCtorInitExprClass, T, VK_RValue, OK_Ordinary, false, 1635 false, false, false), 1636 Constructor(Ctor), Loc(Loc), 1637 ConstructsVirtualBase(ConstructsVirtualBase), 1638 InheritedFromVirtualBase(InheritedFromVirtualBase) { 1639 assert(!T->isDependentType()); 1640 } 1641 1642 /// Construct an empty C++ inheriting construction expression. 1643 explicit CXXInheritedCtorInitExpr(EmptyShell Empty) 1644 : Expr(CXXInheritedCtorInitExprClass, Empty), 1645 ConstructsVirtualBase(false), InheritedFromVirtualBase(false) {} 1646 1647 /// Get the constructor that this expression will call. 1648 CXXConstructorDecl *getConstructor() const { return Constructor; } 1649 1650 /// Determine whether this constructor is actually constructing 1651 /// a base class (rather than a complete object). 1652 bool constructsVBase() const { return ConstructsVirtualBase; } 1653 CXXConstructExpr::ConstructionKind getConstructionKind() const { 1654 return ConstructsVirtualBase ? CXXConstructExpr::CK_VirtualBase 1655 : CXXConstructExpr::CK_NonVirtualBase; 1656 } 1657 1658 /// Determine whether the inherited constructor is inherited from a 1659 /// virtual base of the object we construct. If so, we are not responsible 1660 /// for calling the inherited constructor (the complete object constructor 1661 /// does that), and so we don't need to pass any arguments. 1662 bool inheritedFromVBase() const { return InheritedFromVirtualBase; } 1663 1664 SourceLocation getLocation() const LLVM_READONLY { return Loc; } 1665 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 1666 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } 1667 1668 static bool classof(const Stmt *T) { 1669 return T->getStmtClass() == CXXInheritedCtorInitExprClass; 1670 } 1671 1672 child_range children() { 1673 return child_range(child_iterator(), child_iterator()); 1674 } 1675 1676 const_child_range children() const { 1677 return const_child_range(const_child_iterator(), const_child_iterator()); 1678 } 1679}; 1680 1681/// Represents an explicit C++ type conversion that uses "functional" 1682/// notation (C++ [expr.type.conv]). 1683/// 1684/// Example: 1685/// \code 1686/// x = int(0.5); 1687/// \endcode 1688class CXXFunctionalCastExpr final 1689 : public ExplicitCastExpr, 1690 private llvm::TrailingObjects<CXXFunctionalCastExpr, CXXBaseSpecifier *> { 1691 SourceLocation LParenLoc; 1692 SourceLocation RParenLoc; 1693 1694 CXXFunctionalCastExpr(QualType ty, ExprValueKind VK, 1695 TypeSourceInfo *writtenTy, 1696 CastKind kind, Expr *castExpr, unsigned pathSize, 1697 SourceLocation lParenLoc, SourceLocation rParenLoc) 1698 : ExplicitCastExpr(CXXFunctionalCastExprClass, ty, VK, kind, 1699 castExpr, pathSize, writtenTy), 1700 LParenLoc(lParenLoc), RParenLoc(rParenLoc) {} 1701 1702 explicit CXXFunctionalCastExpr(EmptyShell Shell, unsigned PathSize) 1703 : ExplicitCastExpr(CXXFunctionalCastExprClass, Shell, PathSize) {} 1704 1705public: 1706 friend class CastExpr; 1707 friend TrailingObjects; 1708 1709 static CXXFunctionalCastExpr *Create(const ASTContext &Context, QualType T, 1710 ExprValueKind VK, 1711 TypeSourceInfo *Written, 1712 CastKind Kind, Expr *Op, 1713 const CXXCastPath *Path, 1714 SourceLocation LPLoc, 1715 SourceLocation RPLoc); 1716 static CXXFunctionalCastExpr *CreateEmpty(const ASTContext &Context, 1717 unsigned PathSize); 1718 1719 SourceLocation getLParenLoc() const { return LParenLoc; } 1720 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 1721 SourceLocation getRParenLoc() const { return RParenLoc; } 1722 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 1723 1724 /// Determine whether this expression models list-initialization. 1725 bool isListInitialization() const { return LParenLoc.isInvalid(); } 1726 1727 SourceLocation getBeginLoc() const LLVM_READONLY; 1728 SourceLocation getEndLoc() const LLVM_READONLY; 1729 1730 static bool classof(const Stmt *T) { 1731 return T->getStmtClass() == CXXFunctionalCastExprClass; 1732 } 1733}; 1734 1735/// Represents a C++ functional cast expression that builds a 1736/// temporary object. 1737/// 1738/// This expression type represents a C++ "functional" cast 1739/// (C++[expr.type.conv]) with N != 1 arguments that invokes a 1740/// constructor to build a temporary object. With N == 1 arguments the 1741/// functional cast expression will be represented by CXXFunctionalCastExpr. 1742/// Example: 1743/// \code 1744/// struct X { X(int, float); } 1745/// 1746/// X create_X() { 1747/// return X(1, 3.14f); // creates a CXXTemporaryObjectExpr 1748/// }; 1749/// \endcode 1750class CXXTemporaryObjectExpr final : public CXXConstructExpr { 1751 friend class ASTStmtReader; 1752 1753 // CXXTemporaryObjectExpr has some trailing objects belonging 1754 // to CXXConstructExpr. See the comment inside CXXConstructExpr 1755 // for more details. 1756 1757 TypeSourceInfo *TSI; 1758 1759 CXXTemporaryObjectExpr(CXXConstructorDecl *Cons, QualType Ty, 1760 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, 1761 SourceRange ParenOrBraceRange, 1762 bool HadMultipleCandidates, bool ListInitialization, 1763 bool StdInitListInitialization, 1764 bool ZeroInitialization); 1765 1766 CXXTemporaryObjectExpr(EmptyShell Empty, unsigned NumArgs); 1767 1768public: 1769 static CXXTemporaryObjectExpr * 1770 Create(const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty, 1771 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, 1772 SourceRange ParenOrBraceRange, bool HadMultipleCandidates, 1773 bool ListInitialization, bool StdInitListInitialization, 1774 bool ZeroInitialization); 1775 1776 static CXXTemporaryObjectExpr *CreateEmpty(const ASTContext &Ctx, 1777 unsigned NumArgs); 1778 1779 TypeSourceInfo *getTypeSourceInfo() const { return TSI; } 1780 1781 SourceLocation getBeginLoc() const LLVM_READONLY; 1782 SourceLocation getEndLoc() const LLVM_READONLY; 1783 1784 static bool classof(const Stmt *T) { 1785 return T->getStmtClass() == CXXTemporaryObjectExprClass; 1786 } 1787}; 1788 1789Stmt **CXXConstructExpr::getTrailingArgs() { 1790 if (auto *E = dyn_cast<CXXTemporaryObjectExpr>(this)) 1791 return reinterpret_cast<Stmt **>(E + 1); 1792 assert((getStmtClass() == CXXConstructExprClass) && 1793 "Unexpected class deriving from CXXConstructExpr!"); 1794 return reinterpret_cast<Stmt **>(this + 1); 1795} 1796 1797/// A C++ lambda expression, which produces a function object 1798/// (of unspecified type) that can be invoked later. 1799/// 1800/// Example: 1801/// \code 1802/// void low_pass_filter(std::vector<double> &values, double cutoff) { 1803/// values.erase(std::remove_if(values.begin(), values.end(), 1804/// [=](double value) { return value > cutoff; }); 1805/// } 1806/// \endcode 1807/// 1808/// C++11 lambda expressions can capture local variables, either by copying 1809/// the values of those local variables at the time the function 1810/// object is constructed (not when it is called!) or by holding a 1811/// reference to the local variable. These captures can occur either 1812/// implicitly or can be written explicitly between the square 1813/// brackets ([...]) that start the lambda expression. 1814/// 1815/// C++1y introduces a new form of "capture" called an init-capture that 1816/// includes an initializing expression (rather than capturing a variable), 1817/// and which can never occur implicitly. 1818class LambdaExpr final : public Expr, 1819 private llvm::TrailingObjects<LambdaExpr, Stmt *> { 1820 /// The source range that covers the lambda introducer ([...]). 1821 SourceRange IntroducerRange; 1822 1823 /// The source location of this lambda's capture-default ('=' or '&'). 1824 SourceLocation CaptureDefaultLoc; 1825 1826 /// The number of captures. 1827 unsigned NumCaptures : 16; 1828 1829 /// The default capture kind, which is a value of type 1830 /// LambdaCaptureDefault. 1831 unsigned CaptureDefault : 2; 1832 1833 /// Whether this lambda had an explicit parameter list vs. an 1834 /// implicit (and empty) parameter list. 1835 unsigned ExplicitParams : 1; 1836 1837 /// Whether this lambda had the result type explicitly specified. 1838 unsigned ExplicitResultType : 1; 1839 1840 /// The location of the closing brace ('}') that completes 1841 /// the lambda. 1842 /// 1843 /// The location of the brace is also available by looking up the 1844 /// function call operator in the lambda class. However, it is 1845 /// stored here to improve the performance of getSourceRange(), and 1846 /// to avoid having to deserialize the function call operator from a 1847 /// module file just to determine the source range. 1848 SourceLocation ClosingBrace; 1849 1850 /// Construct a lambda expression. 1851 LambdaExpr(QualType T, SourceRange IntroducerRange, 1852 LambdaCaptureDefault CaptureDefault, 1853 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures, 1854 bool ExplicitParams, bool ExplicitResultType, 1855 ArrayRef<Expr *> CaptureInits, SourceLocation ClosingBrace, 1856 bool ContainsUnexpandedParameterPack); 1857 1858 /// Construct an empty lambda expression. 1859 LambdaExpr(EmptyShell Empty, unsigned NumCaptures) 1860 : Expr(LambdaExprClass, Empty), NumCaptures(NumCaptures), 1861 CaptureDefault(LCD_None), ExplicitParams(false), 1862 ExplicitResultType(false) { 1863 getStoredStmts()[NumCaptures] = nullptr; 1864 } 1865 1866 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); } 1867 1868 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); } 1869 1870public: 1871 friend class ASTStmtReader; 1872 friend class ASTStmtWriter; 1873 friend TrailingObjects; 1874 1875 /// Construct a new lambda expression. 1876 static LambdaExpr * 1877 Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange, 1878 LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc, 1879 ArrayRef<LambdaCapture> Captures, bool ExplicitParams, 1880 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits, 1881 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack); 1882 1883 /// Construct a new lambda expression that will be deserialized from 1884 /// an external source. 1885 static LambdaExpr *CreateDeserialized(const ASTContext &C, 1886 unsigned NumCaptures); 1887 1888 /// Determine the default capture kind for this lambda. 1889 LambdaCaptureDefault getCaptureDefault() const { 1890 return static_cast<LambdaCaptureDefault>(CaptureDefault); 1891 } 1892 1893 /// Retrieve the location of this lambda's capture-default, if any. 1894 SourceLocation getCaptureDefaultLoc() const { 1895 return CaptureDefaultLoc; 1896 } 1897 1898 /// Determine whether one of this lambda's captures is an init-capture. 1899 bool isInitCapture(const LambdaCapture *Capture) const; 1900 1901 /// An iterator that walks over the captures of the lambda, 1902 /// both implicit and explicit. 1903 using capture_iterator = const LambdaCapture *; 1904 1905 /// An iterator over a range of lambda captures. 1906 using capture_range = llvm::iterator_range<capture_iterator>; 1907 1908 /// Retrieve this lambda's captures. 1909 capture_range captures() const; 1910 1911 /// Retrieve an iterator pointing to the first lambda capture. 1912 capture_iterator capture_begin() const; 1913 1914 /// Retrieve an iterator pointing past the end of the 1915 /// sequence of lambda captures. 1916 capture_iterator capture_end() const; 1917 1918 /// Determine the number of captures in this lambda. 1919 unsigned capture_size() const { return NumCaptures; } 1920 1921 /// Retrieve this lambda's explicit captures. 1922 capture_range explicit_captures() const; 1923 1924 /// Retrieve an iterator pointing to the first explicit 1925 /// lambda capture. 1926 capture_iterator explicit_capture_begin() const; 1927 1928 /// Retrieve an iterator pointing past the end of the sequence of 1929 /// explicit lambda captures. 1930 capture_iterator explicit_capture_end() const; 1931 1932 /// Retrieve this lambda's implicit captures. 1933 capture_range implicit_captures() const; 1934 1935 /// Retrieve an iterator pointing to the first implicit 1936 /// lambda capture. 1937 capture_iterator implicit_capture_begin() const; 1938 1939 /// Retrieve an iterator pointing past the end of the sequence of 1940 /// implicit lambda captures. 1941 capture_iterator implicit_capture_end() const; 1942 1943 /// Iterator that walks over the capture initialization 1944 /// arguments. 1945 using capture_init_iterator = Expr **; 1946 1947 /// Const iterator that walks over the capture initialization 1948 /// arguments. 1949 using const_capture_init_iterator = Expr *const *; 1950 1951 /// Retrieve the initialization expressions for this lambda's captures. 1952 llvm::iterator_range<capture_init_iterator> capture_inits() { 1953 return llvm::make_range(capture_init_begin(), capture_init_end()); 1954 } 1955 1956 /// Retrieve the initialization expressions for this lambda's captures. 1957 llvm::iterator_range<const_capture_init_iterator> capture_inits() const { 1958 return llvm::make_range(capture_init_begin(), capture_init_end()); 1959 } 1960 1961 /// Retrieve the first initialization argument for this 1962 /// lambda expression (which initializes the first capture field). 1963 capture_init_iterator capture_init_begin() { 1964 return reinterpret_cast<Expr **>(getStoredStmts()); 1965 } 1966 1967 /// Retrieve the first initialization argument for this 1968 /// lambda expression (which initializes the first capture field). 1969 const_capture_init_iterator capture_init_begin() const { 1970 return reinterpret_cast<Expr *const *>(getStoredStmts()); 1971 } 1972 1973 /// Retrieve the iterator pointing one past the last 1974 /// initialization argument for this lambda expression. 1975 capture_init_iterator capture_init_end() { 1976 return capture_init_begin() + NumCaptures; 1977 } 1978 1979 /// Retrieve the iterator pointing one past the last 1980 /// initialization argument for this lambda expression. 1981 const_capture_init_iterator capture_init_end() const { 1982 return capture_init_begin() + NumCaptures; 1983 } 1984 1985 /// Retrieve the source range covering the lambda introducer, 1986 /// which contains the explicit capture list surrounded by square 1987 /// brackets ([...]). 1988 SourceRange getIntroducerRange() const { return IntroducerRange; } 1989 1990 /// Retrieve the class that corresponds to the lambda. 1991 /// 1992 /// This is the "closure type" (C++1y [expr.prim.lambda]), and stores the 1993 /// captures in its fields and provides the various operations permitted 1994 /// on a lambda (copying, calling). 1995 CXXRecordDecl *getLambdaClass() const; 1996 1997 /// Retrieve the function call operator associated with this 1998 /// lambda expression. 1999 CXXMethodDecl *getCallOperator() const; 2000 2001 /// Retrieve the function template call operator associated with this 2002 /// lambda expression. 2003 FunctionTemplateDecl *getDependentCallOperator() const; 2004 2005 /// If this is a generic lambda expression, retrieve the template 2006 /// parameter list associated with it, or else return null. 2007 TemplateParameterList *getTemplateParameterList() const; 2008 2009 /// Get the template parameters were explicitly specified (as opposed to being 2010 /// invented by use of an auto parameter). 2011 ArrayRef<NamedDecl *> getExplicitTemplateParameters() const; 2012 2013 /// Whether this is a generic lambda. 2014 bool isGenericLambda() const { return getTemplateParameterList(); } 2015 2016 /// Retrieve the body of the lambda. 2017 CompoundStmt *getBody() const; 2018 2019 /// Determine whether the lambda is mutable, meaning that any 2020 /// captures values can be modified. 2021 bool isMutable() const; 2022 2023 /// Determine whether this lambda has an explicit parameter 2024 /// list vs. an implicit (empty) parameter list. 2025 bool hasExplicitParameters() const { return ExplicitParams; } 2026 2027 /// Whether this lambda had its result type explicitly specified. 2028 bool hasExplicitResultType() const { return ExplicitResultType; } 2029 2030 static bool classof(const Stmt *T) { 2031 return T->getStmtClass() == LambdaExprClass; 2032 } 2033 2034 SourceLocation getBeginLoc() const LLVM_READONLY { 2035 return IntroducerRange.getBegin(); 2036 } 2037 2038 SourceLocation getEndLoc() const LLVM_READONLY { return ClosingBrace; } 2039 2040 child_range children() { 2041 // Includes initialization exprs plus body stmt 2042 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures + 1); 2043 } 2044 2045 const_child_range children() const { 2046 return const_child_range(getStoredStmts(), 2047 getStoredStmts() + NumCaptures + 1); 2048 } 2049}; 2050 2051/// An expression "T()" which creates a value-initialized rvalue of type 2052/// T, which is a non-class type. See (C++98 [5.2.3p2]). 2053class CXXScalarValueInitExpr : public Expr { 2054 friend class ASTStmtReader; 2055 2056 TypeSourceInfo *TypeInfo; 2057 2058public: 2059 /// Create an explicitly-written scalar-value initialization 2060 /// expression. 2061 CXXScalarValueInitExpr(QualType Type, TypeSourceInfo *TypeInfo, 2062 SourceLocation RParenLoc) 2063 : Expr(CXXScalarValueInitExprClass, Type, VK_RValue, OK_Ordinary, false, 2064 false, Type->isInstantiationDependentType(), 2065 Type->containsUnexpandedParameterPack()), 2066 TypeInfo(TypeInfo) { 2067 CXXScalarValueInitExprBits.RParenLoc = RParenLoc; 2068 } 2069 2070 explicit CXXScalarValueInitExpr(EmptyShell Shell) 2071 : Expr(CXXScalarValueInitExprClass, Shell) {} 2072 2073 TypeSourceInfo *getTypeSourceInfo() const { 2074 return TypeInfo; 2075 } 2076 2077 SourceLocation getRParenLoc() const { 2078 return CXXScalarValueInitExprBits.RParenLoc; 2079 } 2080 2081 SourceLocation getBeginLoc() const LLVM_READONLY; 2082 SourceLocation getEndLoc() const { return getRParenLoc(); } 2083 2084 static bool classof(const Stmt *T) { 2085 return T->getStmtClass() == CXXScalarValueInitExprClass; 2086 } 2087 2088 // Iterators 2089 child_range children() { 2090 return child_range(child_iterator(), child_iterator()); 2091 } 2092 2093 const_child_range children() const { 2094 return const_child_range(const_child_iterator(), const_child_iterator()); 2095 } 2096}; 2097 2098/// Represents a new-expression for memory allocation and constructor 2099/// calls, e.g: "new CXXNewExpr(foo)". 2100class CXXNewExpr final 2101 : public Expr, 2102 private llvm::TrailingObjects<CXXNewExpr, Stmt *, SourceRange> { 2103 friend class ASTStmtReader; 2104 friend class ASTStmtWriter; 2105 friend TrailingObjects; 2106 2107 /// Points to the allocation function used. 2108 FunctionDecl *OperatorNew; 2109 2110 /// Points to the deallocation function used in case of error. May be null. 2111 FunctionDecl *OperatorDelete; 2112 2113 /// The allocated type-source information, as written in the source. 2114 TypeSourceInfo *AllocatedTypeInfo; 2115 2116 /// Range of the entire new expression. 2117 SourceRange Range; 2118 2119 /// Source-range of a paren-delimited initializer. 2120 SourceRange DirectInitRange; 2121 2122 // CXXNewExpr is followed by several optional trailing objects. 2123 // They are in order: 2124 // 2125 // * An optional "Stmt *" for the array size expression. 2126 // Present if and ony if isArray(). 2127 // 2128 // * An optional "Stmt *" for the init expression. 2129 // Present if and only if hasInitializer(). 2130 // 2131 // * An array of getNumPlacementArgs() "Stmt *" for the placement new 2132 // arguments, if any. 2133 // 2134 // * An optional SourceRange for the range covering the parenthesized type-id 2135 // if the allocated type was expressed as a parenthesized type-id. 2136 // Present if and only if isParenTypeId(). 2137 unsigned arraySizeOffset() const { return 0; } 2138 unsigned initExprOffset() const { return arraySizeOffset() + isArray(); } 2139 unsigned placementNewArgsOffset() const { 2140 return initExprOffset() + hasInitializer(); 2141 } 2142 2143 unsigned numTrailingObjects(OverloadToken<Stmt *>) const { 2144 return isArray() + hasInitializer() + getNumPlacementArgs(); 2145 } 2146 2147 unsigned numTrailingObjects(OverloadToken<SourceRange>) const { 2148 return isParenTypeId(); 2149 } 2150 2151public: 2152 enum InitializationStyle { 2153 /// New-expression has no initializer as written. 2154 NoInit, 2155 2156 /// New-expression has a C++98 paren-delimited initializer. 2157 CallInit, 2158 2159 /// New-expression has a C++11 list-initializer. 2160 ListInit 2161 }; 2162 2163private: 2164 /// Build a c++ new expression. 2165 CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew, 2166 FunctionDecl *OperatorDelete, bool ShouldPassAlignment, 2167 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, 2168 SourceRange TypeIdParens, Optional<Expr *> ArraySize, 2169 InitializationStyle InitializationStyle, Expr *Initializer, 2170 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, 2171 SourceRange DirectInitRange); 2172 2173 /// Build an empty c++ new expression. 2174 CXXNewExpr(EmptyShell Empty, bool IsArray, unsigned NumPlacementArgs, 2175 bool IsParenTypeId); 2176 2177public: 2178 /// Create a c++ new expression. 2179 static CXXNewExpr * 2180 Create(const ASTContext &Ctx, bool IsGlobalNew, FunctionDecl *OperatorNew, 2181 FunctionDecl *OperatorDelete, bool ShouldPassAlignment, 2182 bool UsualArrayDeleteWantsSize, ArrayRef<Expr *> PlacementArgs, 2183 SourceRange TypeIdParens, Optional<Expr *> ArraySize, 2184 InitializationStyle InitializationStyle, Expr *Initializer, 2185 QualType Ty, TypeSourceInfo *AllocatedTypeInfo, SourceRange Range, 2186 SourceRange DirectInitRange); 2187 2188 /// Create an empty c++ new expression. 2189 static CXXNewExpr *CreateEmpty(const ASTContext &Ctx, bool IsArray, 2190 bool HasInit, unsigned NumPlacementArgs, 2191 bool IsParenTypeId); 2192 2193 QualType getAllocatedType() const { 2194 return getType()->castAs<PointerType>()->getPointeeType(); 2195 } 2196 2197 TypeSourceInfo *getAllocatedTypeSourceInfo() const { 2198 return AllocatedTypeInfo; 2199 } 2200 2201 /// True if the allocation result needs to be null-checked. 2202 /// 2203 /// C++11 [expr.new]p13: 2204 /// If the allocation function returns null, initialization shall 2205 /// not be done, the deallocation function shall not be called, 2206 /// and the value of the new-expression shall be null. 2207 /// 2208 /// C++ DR1748: 2209 /// If the allocation function is a reserved placement allocation 2210 /// function that returns null, the behavior is undefined. 2211 /// 2212 /// An allocation function is not allowed to return null unless it 2213 /// has a non-throwing exception-specification. The '03 rule is 2214 /// identical except that the definition of a non-throwing 2215 /// exception specification is just "is it throw()?". 2216 bool shouldNullCheckAllocation() const; 2217 2218 FunctionDecl *getOperatorNew() const { return OperatorNew; } 2219 void setOperatorNew(FunctionDecl *D) { OperatorNew = D; } 2220 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 2221 void setOperatorDelete(FunctionDecl *D) { OperatorDelete = D; } 2222 2223 bool isArray() const { return CXXNewExprBits.IsArray; } 2224 2225 Optional<Expr *> getArraySize() { 2226 if (!isArray()) 2227 return None; 2228 return cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]); 2229 } 2230 Optional<const Expr *> getArraySize() const { 2231 if (!isArray()) 2232 return None; 2233 return cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]); 2234 } 2235 2236 unsigned getNumPlacementArgs() const { 2237 return CXXNewExprBits.NumPlacementArgs; 2238 } 2239 2240 Expr **getPlacementArgs() { 2241 return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>() + 2242 placementNewArgsOffset()); 2243 } 2244 2245 Expr *getPlacementArg(unsigned I) { 2246 assert((I < getNumPlacementArgs()) && "Index out of range!"); 2247 return getPlacementArgs()[I]; 2248 } 2249 const Expr *getPlacementArg(unsigned I) const { 2250 return const_cast<CXXNewExpr *>(this)->getPlacementArg(I); 2251 } 2252 2253 bool isParenTypeId() const { return CXXNewExprBits.IsParenTypeId; } 2254 SourceRange getTypeIdParens() const { 2255 return isParenTypeId() ? getTrailingObjects<SourceRange>()[0] 2256 : SourceRange(); 2257 } 2258 2259 bool isGlobalNew() const { return CXXNewExprBits.IsGlobalNew; } 2260 2261 /// Whether this new-expression has any initializer at all. 2262 bool hasInitializer() const { 2263 return CXXNewExprBits.StoredInitializationStyle > 0; 2264 } 2265 2266 /// The kind of initializer this new-expression has. 2267 InitializationStyle getInitializationStyle() const { 2268 if (CXXNewExprBits.StoredInitializationStyle == 0) 2269 return NoInit; 2270 return static_cast<InitializationStyle>( 2271 CXXNewExprBits.StoredInitializationStyle - 1); 2272 } 2273 2274 /// The initializer of this new-expression. 2275 Expr *getInitializer() { 2276 return hasInitializer() 2277 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()]) 2278 : nullptr; 2279 } 2280 const Expr *getInitializer() const { 2281 return hasInitializer() 2282 ? cast<Expr>(getTrailingObjects<Stmt *>()[initExprOffset()]) 2283 : nullptr; 2284 } 2285 2286 /// Returns the CXXConstructExpr from this new-expression, or null. 2287 const CXXConstructExpr *getConstructExpr() const { 2288 return dyn_cast_or_null<CXXConstructExpr>(getInitializer()); 2289 } 2290 2291 /// Indicates whether the required alignment should be implicitly passed to 2292 /// the allocation function. 2293 bool passAlignment() const { return CXXNewExprBits.ShouldPassAlignment; } 2294 2295 /// Answers whether the usual array deallocation function for the 2296 /// allocated type expects the size of the allocation as a 2297 /// parameter. 2298 bool doesUsualArrayDeleteWantSize() const { 2299 return CXXNewExprBits.UsualArrayDeleteWantsSize; 2300 } 2301 2302 using arg_iterator = ExprIterator; 2303 using const_arg_iterator = ConstExprIterator; 2304 2305 llvm::iterator_range<arg_iterator> placement_arguments() { 2306 return llvm::make_range(placement_arg_begin(), placement_arg_end()); 2307 } 2308 2309 llvm::iterator_range<const_arg_iterator> placement_arguments() const { 2310 return llvm::make_range(placement_arg_begin(), placement_arg_end()); 2311 } 2312 2313 arg_iterator placement_arg_begin() { 2314 return getTrailingObjects<Stmt *>() + placementNewArgsOffset(); 2315 } 2316 arg_iterator placement_arg_end() { 2317 return placement_arg_begin() + getNumPlacementArgs(); 2318 } 2319 const_arg_iterator placement_arg_begin() const { 2320 return getTrailingObjects<Stmt *>() + placementNewArgsOffset(); 2321 } 2322 const_arg_iterator placement_arg_end() const { 2323 return placement_arg_begin() + getNumPlacementArgs(); 2324 } 2325 2326 using raw_arg_iterator = Stmt **; 2327 2328 raw_arg_iterator raw_arg_begin() { return getTrailingObjects<Stmt *>(); } 2329 raw_arg_iterator raw_arg_end() { 2330 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>()); 2331 } 2332 const_arg_iterator raw_arg_begin() const { 2333 return getTrailingObjects<Stmt *>(); 2334 } 2335 const_arg_iterator raw_arg_end() const { 2336 return raw_arg_begin() + numTrailingObjects(OverloadToken<Stmt *>()); 2337 } 2338 2339 SourceLocation getBeginLoc() const { return Range.getBegin(); } 2340 SourceLocation getEndLoc() const { return Range.getEnd(); } 2341 2342 SourceRange getDirectInitRange() const { return DirectInitRange; } 2343 SourceRange getSourceRange() const { return Range; } 2344 2345 static bool classof(const Stmt *T) { 2346 return T->getStmtClass() == CXXNewExprClass; 2347 } 2348 2349 // Iterators 2350 child_range children() { return child_range(raw_arg_begin(), raw_arg_end()); } 2351 2352 const_child_range children() const { 2353 return const_child_range(const_cast<CXXNewExpr *>(this)->children()); 2354 } 2355}; 2356 2357/// Represents a \c delete expression for memory deallocation and 2358/// destructor calls, e.g. "delete[] pArray". 2359class CXXDeleteExpr : public Expr { 2360 friend class ASTStmtReader; 2361 2362 /// Points to the operator delete overload that is used. Could be a member. 2363 FunctionDecl *OperatorDelete = nullptr; 2364 2365 /// The pointer expression to be deleted. 2366 Stmt *Argument = nullptr; 2367 2368public: 2369 CXXDeleteExpr(QualType Ty, bool GlobalDelete, bool ArrayForm, 2370 bool ArrayFormAsWritten, bool UsualArrayDeleteWantsSize, 2371 FunctionDecl *OperatorDelete, Expr *Arg, SourceLocation Loc) 2372 : Expr(CXXDeleteExprClass, Ty, VK_RValue, OK_Ordinary, false, 2373 Arg->isValueDependent(), Arg->isInstantiationDependent(), 2374 Arg->containsUnexpandedParameterPack()), 2375 OperatorDelete(OperatorDelete), Argument(Arg) { 2376 CXXDeleteExprBits.GlobalDelete = GlobalDelete; 2377 CXXDeleteExprBits.ArrayForm = ArrayForm; 2378 CXXDeleteExprBits.ArrayFormAsWritten = ArrayFormAsWritten; 2379 CXXDeleteExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize; 2380 CXXDeleteExprBits.Loc = Loc; 2381 } 2382 2383 explicit CXXDeleteExpr(EmptyShell Shell) : Expr(CXXDeleteExprClass, Shell) {} 2384 2385 bool isGlobalDelete() const { return CXXDeleteExprBits.GlobalDelete; } 2386 bool isArrayForm() const { return CXXDeleteExprBits.ArrayForm; } 2387 bool isArrayFormAsWritten() const { 2388 return CXXDeleteExprBits.ArrayFormAsWritten; 2389 } 2390 2391 /// Answers whether the usual array deallocation function for the 2392 /// allocated type expects the size of the allocation as a 2393 /// parameter. This can be true even if the actual deallocation 2394 /// function that we're using doesn't want a size. 2395 bool doesUsualArrayDeleteWantSize() const { 2396 return CXXDeleteExprBits.UsualArrayDeleteWantsSize; 2397 } 2398 2399 FunctionDecl *getOperatorDelete() const { return OperatorDelete; } 2400 2401 Expr *getArgument() { return cast<Expr>(Argument); } 2402 const Expr *getArgument() const { return cast<Expr>(Argument); } 2403 2404 /// Retrieve the type being destroyed. 2405 /// 2406 /// If the type being destroyed is a dependent type which may or may not 2407 /// be a pointer, return an invalid type. 2408 QualType getDestroyedType() const; 2409 2410 SourceLocation getBeginLoc() const { return CXXDeleteExprBits.Loc; } 2411 SourceLocation getEndLoc() const LLVM_READONLY { 2412 return Argument->getEndLoc(); 2413 } 2414 2415 static bool classof(const Stmt *T) { 2416 return T->getStmtClass() == CXXDeleteExprClass; 2417 } 2418 2419 // Iterators 2420 child_range children() { return child_range(&Argument, &Argument + 1); } 2421 2422 const_child_range children() const { 2423 return const_child_range(&Argument, &Argument + 1); 2424 } 2425}; 2426 2427/// Stores the type being destroyed by a pseudo-destructor expression. 2428class PseudoDestructorTypeStorage { 2429 /// Either the type source information or the name of the type, if 2430 /// it couldn't be resolved due to type-dependence. 2431 llvm::PointerUnion<TypeSourceInfo *, IdentifierInfo *> Type; 2432 2433 /// The starting source location of the pseudo-destructor type. 2434 SourceLocation Location; 2435 2436public: 2437 PseudoDestructorTypeStorage() = default; 2438 2439 PseudoDestructorTypeStorage(IdentifierInfo *II, SourceLocation Loc) 2440 : Type(II), Location(Loc) {} 2441 2442 PseudoDestructorTypeStorage(TypeSourceInfo *Info); 2443 2444 TypeSourceInfo *getTypeSourceInfo() const { 2445 return Type.dyn_cast<TypeSourceInfo *>(); 2446 } 2447 2448 IdentifierInfo *getIdentifier() const { 2449 return Type.dyn_cast<IdentifierInfo *>(); 2450 } 2451 2452 SourceLocation getLocation() const { return Location; } 2453}; 2454 2455/// Represents a C++ pseudo-destructor (C++ [expr.pseudo]). 2456/// 2457/// A pseudo-destructor is an expression that looks like a member access to a 2458/// destructor of a scalar type, except that scalar types don't have 2459/// destructors. For example: 2460/// 2461/// \code 2462/// typedef int T; 2463/// void f(int *p) { 2464/// p->T::~T(); 2465/// } 2466/// \endcode 2467/// 2468/// Pseudo-destructors typically occur when instantiating templates such as: 2469/// 2470/// \code 2471/// template<typename T> 2472/// void destroy(T* ptr) { 2473/// ptr->T::~T(); 2474/// } 2475/// \endcode 2476/// 2477/// for scalar types. A pseudo-destructor expression has no run-time semantics 2478/// beyond evaluating the base expression. 2479class CXXPseudoDestructorExpr : public Expr { 2480 friend class ASTStmtReader; 2481 2482 /// The base expression (that is being destroyed). 2483 Stmt *Base = nullptr; 2484 2485 /// Whether the operator was an arrow ('->'); otherwise, it was a 2486 /// period ('.'). 2487 bool IsArrow : 1; 2488 2489 /// The location of the '.' or '->' operator. 2490 SourceLocation OperatorLoc; 2491 2492 /// The nested-name-specifier that follows the operator, if present. 2493 NestedNameSpecifierLoc QualifierLoc; 2494 2495 /// The type that precedes the '::' in a qualified pseudo-destructor 2496 /// expression. 2497 TypeSourceInfo *ScopeType = nullptr; 2498 2499 /// The location of the '::' in a qualified pseudo-destructor 2500 /// expression. 2501 SourceLocation ColonColonLoc; 2502 2503 /// The location of the '~'. 2504 SourceLocation TildeLoc; 2505 2506 /// The type being destroyed, or its name if we were unable to 2507 /// resolve the name. 2508 PseudoDestructorTypeStorage DestroyedType; 2509 2510public: 2511 CXXPseudoDestructorExpr(const ASTContext &Context, 2512 Expr *Base, bool isArrow, SourceLocation OperatorLoc, 2513 NestedNameSpecifierLoc QualifierLoc, 2514 TypeSourceInfo *ScopeType, 2515 SourceLocation ColonColonLoc, 2516 SourceLocation TildeLoc, 2517 PseudoDestructorTypeStorage DestroyedType); 2518 2519 explicit CXXPseudoDestructorExpr(EmptyShell Shell) 2520 : Expr(CXXPseudoDestructorExprClass, Shell), IsArrow(false) {} 2521 2522 Expr *getBase() const { return cast<Expr>(Base); } 2523 2524 /// Determines whether this member expression actually had 2525 /// a C++ nested-name-specifier prior to the name of the member, e.g., 2526 /// x->Base::foo. 2527 bool hasQualifier() const { return QualifierLoc.hasQualifier(); } 2528 2529 /// Retrieves the nested-name-specifier that qualifies the type name, 2530 /// with source-location information. 2531 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2532 2533 /// If the member name was qualified, retrieves the 2534 /// nested-name-specifier that precedes the member name. Otherwise, returns 2535 /// null. 2536 NestedNameSpecifier *getQualifier() const { 2537 return QualifierLoc.getNestedNameSpecifier(); 2538 } 2539 2540 /// Determine whether this pseudo-destructor expression was written 2541 /// using an '->' (otherwise, it used a '.'). 2542 bool isArrow() const { return IsArrow; } 2543 2544 /// Retrieve the location of the '.' or '->' operator. 2545 SourceLocation getOperatorLoc() const { return OperatorLoc; } 2546 2547 /// Retrieve the scope type in a qualified pseudo-destructor 2548 /// expression. 2549 /// 2550 /// Pseudo-destructor expressions can have extra qualification within them 2551 /// that is not part of the nested-name-specifier, e.g., \c p->T::~T(). 2552 /// Here, if the object type of the expression is (or may be) a scalar type, 2553 /// \p T may also be a scalar type and, therefore, cannot be part of a 2554 /// nested-name-specifier. It is stored as the "scope type" of the pseudo- 2555 /// destructor expression. 2556 TypeSourceInfo *getScopeTypeInfo() const { return ScopeType; } 2557 2558 /// Retrieve the location of the '::' in a qualified pseudo-destructor 2559 /// expression. 2560 SourceLocation getColonColonLoc() const { return ColonColonLoc; } 2561 2562 /// Retrieve the location of the '~'. 2563 SourceLocation getTildeLoc() const { return TildeLoc; } 2564 2565 /// Retrieve the source location information for the type 2566 /// being destroyed. 2567 /// 2568 /// This type-source information is available for non-dependent 2569 /// pseudo-destructor expressions and some dependent pseudo-destructor 2570 /// expressions. Returns null if we only have the identifier for a 2571 /// dependent pseudo-destructor expression. 2572 TypeSourceInfo *getDestroyedTypeInfo() const { 2573 return DestroyedType.getTypeSourceInfo(); 2574 } 2575 2576 /// In a dependent pseudo-destructor expression for which we do not 2577 /// have full type information on the destroyed type, provides the name 2578 /// of the destroyed type. 2579 IdentifierInfo *getDestroyedTypeIdentifier() const { 2580 return DestroyedType.getIdentifier(); 2581 } 2582 2583 /// Retrieve the type being destroyed. 2584 QualType getDestroyedType() const; 2585 2586 /// Retrieve the starting location of the type being destroyed. 2587 SourceLocation getDestroyedTypeLoc() const { 2588 return DestroyedType.getLocation(); 2589 } 2590 2591 /// Set the name of destroyed type for a dependent pseudo-destructor 2592 /// expression. 2593 void setDestroyedType(IdentifierInfo *II, SourceLocation Loc) { 2594 DestroyedType = PseudoDestructorTypeStorage(II, Loc); 2595 } 2596 2597 /// Set the destroyed type. 2598 void setDestroyedType(TypeSourceInfo *Info) { 2599 DestroyedType = PseudoDestructorTypeStorage(Info); 2600 } 2601 2602 SourceLocation getBeginLoc() const LLVM_READONLY { 2603 return Base->getBeginLoc(); 2604 } 2605 SourceLocation getEndLoc() const LLVM_READONLY; 2606 2607 static bool classof(const Stmt *T) { 2608 return T->getStmtClass() == CXXPseudoDestructorExprClass; 2609 } 2610 2611 // Iterators 2612 child_range children() { return child_range(&Base, &Base + 1); } 2613 2614 const_child_range children() const { 2615 return const_child_range(&Base, &Base + 1); 2616 } 2617}; 2618 2619/// A type trait used in the implementation of various C++11 and 2620/// Library TR1 trait templates. 2621/// 2622/// \code 2623/// __is_pod(int) == true 2624/// __is_enum(std::string) == false 2625/// __is_trivially_constructible(vector<int>, int*, int*) 2626/// \endcode 2627class TypeTraitExpr final 2628 : public Expr, 2629 private llvm::TrailingObjects<TypeTraitExpr, TypeSourceInfo *> { 2630 /// The location of the type trait keyword. 2631 SourceLocation Loc; 2632 2633 /// The location of the closing parenthesis. 2634 SourceLocation RParenLoc; 2635 2636 // Note: The TypeSourceInfos for the arguments are allocated after the 2637 // TypeTraitExpr. 2638 2639 TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind, 2640 ArrayRef<TypeSourceInfo *> Args, 2641 SourceLocation RParenLoc, 2642 bool Value); 2643 2644 TypeTraitExpr(EmptyShell Empty) : Expr(TypeTraitExprClass, Empty) {} 2645 2646 size_t numTrailingObjects(OverloadToken<TypeSourceInfo *>) const { 2647 return getNumArgs(); 2648 } 2649 2650public: 2651 friend class ASTStmtReader; 2652 friend class ASTStmtWriter; 2653 friend TrailingObjects; 2654 2655 /// Create a new type trait expression. 2656 static TypeTraitExpr *Create(const ASTContext &C, QualType T, 2657 SourceLocation Loc, TypeTrait Kind, 2658 ArrayRef<TypeSourceInfo *> Args, 2659 SourceLocation RParenLoc, 2660 bool Value); 2661 2662 static TypeTraitExpr *CreateDeserialized(const ASTContext &C, 2663 unsigned NumArgs); 2664 2665 /// Determine which type trait this expression uses. 2666 TypeTrait getTrait() const { 2667 return static_cast<TypeTrait>(TypeTraitExprBits.Kind); 2668 } 2669 2670 bool getValue() const { 2671 assert(!isValueDependent()); 2672 return TypeTraitExprBits.Value; 2673 } 2674 2675 /// Determine the number of arguments to this type trait. 2676 unsigned getNumArgs() const { return TypeTraitExprBits.NumArgs; } 2677 2678 /// Retrieve the Ith argument. 2679 TypeSourceInfo *getArg(unsigned I) const { 2680 assert(I < getNumArgs() && "Argument out-of-range"); 2681 return getArgs()[I]; 2682 } 2683 2684 /// Retrieve the argument types. 2685 ArrayRef<TypeSourceInfo *> getArgs() const { 2686 return llvm::makeArrayRef(getTrailingObjects<TypeSourceInfo *>(), 2687 getNumArgs()); 2688 } 2689 2690 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 2691 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 2692 2693 static bool classof(const Stmt *T) { 2694 return T->getStmtClass() == TypeTraitExprClass; 2695 } 2696 2697 // Iterators 2698 child_range children() { 2699 return child_range(child_iterator(), child_iterator()); 2700 } 2701 2702 const_child_range children() const { 2703 return const_child_range(const_child_iterator(), const_child_iterator()); 2704 } 2705}; 2706 2707/// An Embarcadero array type trait, as used in the implementation of 2708/// __array_rank and __array_extent. 2709/// 2710/// Example: 2711/// \code 2712/// __array_rank(int[10][20]) == 2 2713/// __array_extent(int, 1) == 20 2714/// \endcode 2715class ArrayTypeTraitExpr : public Expr { 2716 /// The trait. An ArrayTypeTrait enum in MSVC compat unsigned. 2717 unsigned ATT : 2; 2718 2719 /// The value of the type trait. Unspecified if dependent. 2720 uint64_t Value = 0; 2721 2722 /// The array dimension being queried, or -1 if not used. 2723 Expr *Dimension; 2724 2725 /// The location of the type trait keyword. 2726 SourceLocation Loc; 2727 2728 /// The location of the closing paren. 2729 SourceLocation RParen; 2730 2731 /// The type being queried. 2732 TypeSourceInfo *QueriedType = nullptr; 2733 2734public: 2735 friend class ASTStmtReader; 2736 2737 ArrayTypeTraitExpr(SourceLocation loc, ArrayTypeTrait att, 2738 TypeSourceInfo *queried, uint64_t value, 2739 Expr *dimension, SourceLocation rparen, QualType ty) 2740 : Expr(ArrayTypeTraitExprClass, ty, VK_RValue, OK_Ordinary, 2741 false, queried->getType()->isDependentType(), 2742 (queried->getType()->isInstantiationDependentType() || 2743 (dimension && dimension->isInstantiationDependent())), 2744 queried->getType()->containsUnexpandedParameterPack()), 2745 ATT(att), Value(value), Dimension(dimension), 2746 Loc(loc), RParen(rparen), QueriedType(queried) {} 2747 2748 explicit ArrayTypeTraitExpr(EmptyShell Empty) 2749 : Expr(ArrayTypeTraitExprClass, Empty), ATT(0) {} 2750 2751 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 2752 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; } 2753 2754 ArrayTypeTrait getTrait() const { return static_cast<ArrayTypeTrait>(ATT); } 2755 2756 QualType getQueriedType() const { return QueriedType->getType(); } 2757 2758 TypeSourceInfo *getQueriedTypeSourceInfo() const { return QueriedType; } 2759 2760 uint64_t getValue() const { assert(!isTypeDependent()); return Value; } 2761 2762 Expr *getDimensionExpression() const { return Dimension; } 2763 2764 static bool classof(const Stmt *T) { 2765 return T->getStmtClass() == ArrayTypeTraitExprClass; 2766 } 2767 2768 // Iterators 2769 child_range children() { 2770 return child_range(child_iterator(), child_iterator()); 2771 } 2772 2773 const_child_range children() const { 2774 return const_child_range(const_child_iterator(), const_child_iterator()); 2775 } 2776}; 2777 2778/// An expression trait intrinsic. 2779/// 2780/// Example: 2781/// \code 2782/// __is_lvalue_expr(std::cout) == true 2783/// __is_lvalue_expr(1) == false 2784/// \endcode 2785class ExpressionTraitExpr : public Expr { 2786 /// The trait. A ExpressionTrait enum in MSVC compatible unsigned. 2787 unsigned ET : 31; 2788 2789 /// The value of the type trait. Unspecified if dependent. 2790 unsigned Value : 1; 2791 2792 /// The location of the type trait keyword. 2793 SourceLocation Loc; 2794 2795 /// The location of the closing paren. 2796 SourceLocation RParen; 2797 2798 /// The expression being queried. 2799 Expr* QueriedExpression = nullptr; 2800 2801public: 2802 friend class ASTStmtReader; 2803 2804 ExpressionTraitExpr(SourceLocation loc, ExpressionTrait et, 2805 Expr *queried, bool value, 2806 SourceLocation rparen, QualType resultType) 2807 : Expr(ExpressionTraitExprClass, resultType, VK_RValue, OK_Ordinary, 2808 false, // Not type-dependent 2809 // Value-dependent if the argument is type-dependent. 2810 queried->isTypeDependent(), 2811 queried->isInstantiationDependent(), 2812 queried->containsUnexpandedParameterPack()), 2813 ET(et), Value(value), Loc(loc), RParen(rparen), 2814 QueriedExpression(queried) {} 2815 2816 explicit ExpressionTraitExpr(EmptyShell Empty) 2817 : Expr(ExpressionTraitExprClass, Empty), ET(0), Value(false) {} 2818 2819 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 2820 SourceLocation getEndLoc() const LLVM_READONLY { return RParen; } 2821 2822 ExpressionTrait getTrait() const { return static_cast<ExpressionTrait>(ET); } 2823 2824 Expr *getQueriedExpression() const { return QueriedExpression; } 2825 2826 bool getValue() const { return Value; } 2827 2828 static bool classof(const Stmt *T) { 2829 return T->getStmtClass() == ExpressionTraitExprClass; 2830 } 2831 2832 // Iterators 2833 child_range children() { 2834 return child_range(child_iterator(), child_iterator()); 2835 } 2836 2837 const_child_range children() const { 2838 return const_child_range(const_child_iterator(), const_child_iterator()); 2839 } 2840}; 2841 2842/// A reference to an overloaded function set, either an 2843/// \c UnresolvedLookupExpr or an \c UnresolvedMemberExpr. 2844class OverloadExpr : public Expr { 2845 friend class ASTStmtReader; 2846 friend class ASTStmtWriter; 2847 2848 /// The common name of these declarations. 2849 DeclarationNameInfo NameInfo; 2850 2851 /// The nested-name-specifier that qualifies the name, if any. 2852 NestedNameSpecifierLoc QualifierLoc; 2853 2854protected: 2855 OverloadExpr(StmtClass SC, const ASTContext &Context, 2856 NestedNameSpecifierLoc QualifierLoc, 2857 SourceLocation TemplateKWLoc, 2858 const DeclarationNameInfo &NameInfo, 2859 const TemplateArgumentListInfo *TemplateArgs, 2860 UnresolvedSetIterator Begin, UnresolvedSetIterator End, 2861 bool KnownDependent, bool KnownInstantiationDependent, 2862 bool KnownContainsUnexpandedParameterPack); 2863 2864 OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults, 2865 bool HasTemplateKWAndArgsInfo); 2866 2867 /// Return the results. Defined after UnresolvedMemberExpr. 2868 inline DeclAccessPair *getTrailingResults(); 2869 const DeclAccessPair *getTrailingResults() const { 2870 return const_cast<OverloadExpr *>(this)->getTrailingResults(); 2871 } 2872 2873 /// Return the optional template keyword and arguments info. 2874 /// Defined after UnresolvedMemberExpr. 2875 inline ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo(); 2876 const ASTTemplateKWAndArgsInfo *getTrailingASTTemplateKWAndArgsInfo() const { 2877 return const_cast<OverloadExpr *>(this) 2878 ->getTrailingASTTemplateKWAndArgsInfo(); 2879 } 2880 2881 /// Return the optional template arguments. Defined after 2882 /// UnresolvedMemberExpr. 2883 inline TemplateArgumentLoc *getTrailingTemplateArgumentLoc(); 2884 const TemplateArgumentLoc *getTrailingTemplateArgumentLoc() const { 2885 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc(); 2886 } 2887 2888 bool hasTemplateKWAndArgsInfo() const { 2889 return OverloadExprBits.HasTemplateKWAndArgsInfo; 2890 } 2891 2892public: 2893 struct FindResult { 2894 OverloadExpr *Expression; 2895 bool IsAddressOfOperand; 2896 bool HasFormOfMemberPointer; 2897 }; 2898 2899 /// Finds the overloaded expression in the given expression \p E of 2900 /// OverloadTy. 2901 /// 2902 /// \return the expression (which must be there) and true if it has 2903 /// the particular form of a member pointer expression 2904 static FindResult find(Expr *E) { 2905 assert(E->getType()->isSpecificBuiltinType(BuiltinType::Overload)); 2906 2907 FindResult Result; 2908 2909 E = E->IgnoreParens(); 2910 if (isa<UnaryOperator>(E)) { 2911 assert(cast<UnaryOperator>(E)->getOpcode() == UO_AddrOf); 2912 E = cast<UnaryOperator>(E)->getSubExpr(); 2913 auto *Ovl = cast<OverloadExpr>(E->IgnoreParens()); 2914 2915 Result.HasFormOfMemberPointer = (E == Ovl && Ovl->getQualifier()); 2916 Result.IsAddressOfOperand = true; 2917 Result.Expression = Ovl; 2918 } else { 2919 Result.HasFormOfMemberPointer = false; 2920 Result.IsAddressOfOperand = false; 2921 Result.Expression = cast<OverloadExpr>(E); 2922 } 2923 2924 return Result; 2925 } 2926 2927 /// Gets the naming class of this lookup, if any. 2928 /// Defined after UnresolvedMemberExpr. 2929 inline CXXRecordDecl *getNamingClass(); 2930 const CXXRecordDecl *getNamingClass() const { 2931 return const_cast<OverloadExpr *>(this)->getNamingClass(); 2932 } 2933 2934 using decls_iterator = UnresolvedSetImpl::iterator; 2935 2936 decls_iterator decls_begin() const { 2937 return UnresolvedSetIterator(getTrailingResults()); 2938 } 2939 decls_iterator decls_end() const { 2940 return UnresolvedSetIterator(getTrailingResults() + getNumDecls()); 2941 } 2942 llvm::iterator_range<decls_iterator> decls() const { 2943 return llvm::make_range(decls_begin(), decls_end()); 2944 } 2945 2946 /// Gets the number of declarations in the unresolved set. 2947 unsigned getNumDecls() const { return OverloadExprBits.NumResults; } 2948 2949 /// Gets the full name info. 2950 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 2951 2952 /// Gets the name looked up. 2953 DeclarationName getName() const { return NameInfo.getName(); } 2954 2955 /// Gets the location of the name. 2956 SourceLocation getNameLoc() const { return NameInfo.getLoc(); } 2957 2958 /// Fetches the nested-name qualifier, if one was given. 2959 NestedNameSpecifier *getQualifier() const { 2960 return QualifierLoc.getNestedNameSpecifier(); 2961 } 2962 2963 /// Fetches the nested-name qualifier with source-location 2964 /// information, if one was given. 2965 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 2966 2967 /// Retrieve the location of the template keyword preceding 2968 /// this name, if any. 2969 SourceLocation getTemplateKeywordLoc() const { 2970 if (!hasTemplateKWAndArgsInfo()) 2971 return SourceLocation(); 2972 return getTrailingASTTemplateKWAndArgsInfo()->TemplateKWLoc; 2973 } 2974 2975 /// Retrieve the location of the left angle bracket starting the 2976 /// explicit template argument list following the name, if any. 2977 SourceLocation getLAngleLoc() const { 2978 if (!hasTemplateKWAndArgsInfo()) 2979 return SourceLocation(); 2980 return getTrailingASTTemplateKWAndArgsInfo()->LAngleLoc; 2981 } 2982 2983 /// Retrieve the location of the right angle bracket ending the 2984 /// explicit template argument list following the name, if any. 2985 SourceLocation getRAngleLoc() const { 2986 if (!hasTemplateKWAndArgsInfo()) 2987 return SourceLocation(); 2988 return getTrailingASTTemplateKWAndArgsInfo()->RAngleLoc; 2989 } 2990 2991 /// Determines whether the name was preceded by the template keyword. 2992 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 2993 2994 /// Determines whether this expression had explicit template arguments. 2995 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 2996 2997 TemplateArgumentLoc const *getTemplateArgs() const { 2998 if (!hasExplicitTemplateArgs()) 2999 return nullptr; 3000 return const_cast<OverloadExpr *>(this)->getTrailingTemplateArgumentLoc(); 3001 } 3002 3003 unsigned getNumTemplateArgs() const { 3004 if (!hasExplicitTemplateArgs()) 3005 return 0; 3006 3007 return getTrailingASTTemplateKWAndArgsInfo()->NumTemplateArgs; 3008 } 3009 3010 ArrayRef<TemplateArgumentLoc> template_arguments() const { 3011 return {getTemplateArgs(), getNumTemplateArgs()}; 3012 } 3013 3014 /// Copies the template arguments into the given structure. 3015 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 3016 if (hasExplicitTemplateArgs()) 3017 getTrailingASTTemplateKWAndArgsInfo()->copyInto(getTemplateArgs(), List); 3018 } 3019 3020 static bool classof(const Stmt *T) { 3021 return T->getStmtClass() == UnresolvedLookupExprClass || 3022 T->getStmtClass() == UnresolvedMemberExprClass; 3023 } 3024}; 3025 3026/// A reference to a name which we were able to look up during 3027/// parsing but could not resolve to a specific declaration. 3028/// 3029/// This arises in several ways: 3030/// * we might be waiting for argument-dependent lookup; 3031/// * the name might resolve to an overloaded function; 3032/// and eventually: 3033/// * the lookup might have included a function template. 3034/// 3035/// These never include UnresolvedUsingValueDecls, which are always class 3036/// members and therefore appear only in UnresolvedMemberLookupExprs. 3037class UnresolvedLookupExpr final 3038 : public OverloadExpr, 3039 private llvm::TrailingObjects<UnresolvedLookupExpr, DeclAccessPair, 3040 ASTTemplateKWAndArgsInfo, 3041 TemplateArgumentLoc> { 3042 friend class ASTStmtReader; 3043 friend class OverloadExpr; 3044 friend TrailingObjects; 3045 3046 /// The naming class (C++ [class.access.base]p5) of the lookup, if 3047 /// any. This can generally be recalculated from the context chain, 3048 /// but that can be fairly expensive for unqualified lookups. 3049 CXXRecordDecl *NamingClass; 3050 3051 // UnresolvedLookupExpr is followed by several trailing objects. 3052 // They are in order: 3053 // 3054 // * An array of getNumResults() DeclAccessPair for the results. These are 3055 // undesugared, which is to say, they may include UsingShadowDecls. 3056 // Access is relative to the naming class. 3057 // 3058 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified 3059 // template keyword and arguments. Present if and only if 3060 // hasTemplateKWAndArgsInfo(). 3061 // 3062 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing 3063 // location information for the explicitly specified template arguments. 3064 3065 UnresolvedLookupExpr(const ASTContext &Context, CXXRecordDecl *NamingClass, 3066 NestedNameSpecifierLoc QualifierLoc, 3067 SourceLocation TemplateKWLoc, 3068 const DeclarationNameInfo &NameInfo, bool RequiresADL, 3069 bool Overloaded, 3070 const TemplateArgumentListInfo *TemplateArgs, 3071 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3072 3073 UnresolvedLookupExpr(EmptyShell Empty, unsigned NumResults, 3074 bool HasTemplateKWAndArgsInfo); 3075 3076 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const { 3077 return getNumDecls(); 3078 } 3079 3080 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { 3081 return hasTemplateKWAndArgsInfo(); 3082 } 3083 3084public: 3085 static UnresolvedLookupExpr * 3086 Create(const ASTContext &Context, CXXRecordDecl *NamingClass, 3087 NestedNameSpecifierLoc QualifierLoc, 3088 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded, 3089 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3090 3091 static UnresolvedLookupExpr * 3092 Create(const ASTContext &Context, CXXRecordDecl *NamingClass, 3093 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, 3094 const DeclarationNameInfo &NameInfo, bool RequiresADL, 3095 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin, 3096 UnresolvedSetIterator End); 3097 3098 static UnresolvedLookupExpr *CreateEmpty(const ASTContext &Context, 3099 unsigned NumResults, 3100 bool HasTemplateKWAndArgsInfo, 3101 unsigned NumTemplateArgs); 3102 3103 /// True if this declaration should be extended by 3104 /// argument-dependent lookup. 3105 bool requiresADL() const { return UnresolvedLookupExprBits.RequiresADL; } 3106 3107 /// True if this lookup is overloaded. 3108 bool isOverloaded() const { return UnresolvedLookupExprBits.Overloaded; } 3109 3110 /// Gets the 'naming class' (in the sense of C++0x 3111 /// [class.access.base]p5) of the lookup. This is the scope 3112 /// that was looked in to find these results. 3113 CXXRecordDecl *getNamingClass() { return NamingClass; } 3114 const CXXRecordDecl *getNamingClass() const { return NamingClass; } 3115 3116 SourceLocation getBeginLoc() const LLVM_READONLY { 3117 if (NestedNameSpecifierLoc l = getQualifierLoc()) 3118 return l.getBeginLoc(); 3119 return getNameInfo().getBeginLoc(); 3120 } 3121 3122 SourceLocation getEndLoc() const LLVM_READONLY { 3123 if (hasExplicitTemplateArgs()) 3124 return getRAngleLoc(); 3125 return getNameInfo().getEndLoc(); 3126 } 3127 3128 child_range children() { 3129 return child_range(child_iterator(), child_iterator()); 3130 } 3131 3132 const_child_range children() const { 3133 return const_child_range(const_child_iterator(), const_child_iterator()); 3134 } 3135 3136 static bool classof(const Stmt *T) { 3137 return T->getStmtClass() == UnresolvedLookupExprClass; 3138 } 3139}; 3140 3141/// A qualified reference to a name whose declaration cannot 3142/// yet be resolved. 3143/// 3144/// DependentScopeDeclRefExpr is similar to DeclRefExpr in that 3145/// it expresses a reference to a declaration such as 3146/// X<T>::value. The difference, however, is that an 3147/// DependentScopeDeclRefExpr node is used only within C++ templates when 3148/// the qualification (e.g., X<T>::) refers to a dependent type. In 3149/// this case, X<T>::value cannot resolve to a declaration because the 3150/// declaration will differ from one instantiation of X<T> to the 3151/// next. Therefore, DependentScopeDeclRefExpr keeps track of the 3152/// qualifier (X<T>::) and the name of the entity being referenced 3153/// ("value"). Such expressions will instantiate to a DeclRefExpr once the 3154/// declaration can be found. 3155class DependentScopeDeclRefExpr final 3156 : public Expr, 3157 private llvm::TrailingObjects<DependentScopeDeclRefExpr, 3158 ASTTemplateKWAndArgsInfo, 3159 TemplateArgumentLoc> { 3160 friend class ASTStmtReader; 3161 friend class ASTStmtWriter; 3162 friend TrailingObjects; 3163 3164 /// The nested-name-specifier that qualifies this unresolved 3165 /// declaration name. 3166 NestedNameSpecifierLoc QualifierLoc; 3167 3168 /// The name of the entity we will be referencing. 3169 DeclarationNameInfo NameInfo; 3170 3171 DependentScopeDeclRefExpr(QualType Ty, NestedNameSpecifierLoc QualifierLoc, 3172 SourceLocation TemplateKWLoc, 3173 const DeclarationNameInfo &NameInfo, 3174 const TemplateArgumentListInfo *Args); 3175 3176 size_t numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { 3177 return hasTemplateKWAndArgsInfo(); 3178 } 3179 3180 bool hasTemplateKWAndArgsInfo() const { 3181 return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo; 3182 } 3183 3184public: 3185 static DependentScopeDeclRefExpr * 3186 Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, 3187 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, 3188 const TemplateArgumentListInfo *TemplateArgs); 3189 3190 static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context, 3191 bool HasTemplateKWAndArgsInfo, 3192 unsigned NumTemplateArgs); 3193 3194 /// Retrieve the name that this expression refers to. 3195 const DeclarationNameInfo &getNameInfo() const { return NameInfo; } 3196 3197 /// Retrieve the name that this expression refers to. 3198 DeclarationName getDeclName() const { return NameInfo.getName(); } 3199 3200 /// Retrieve the location of the name within the expression. 3201 /// 3202 /// For example, in "X<T>::value" this is the location of "value". 3203 SourceLocation getLocation() const { return NameInfo.getLoc(); } 3204 3205 /// Retrieve the nested-name-specifier that qualifies the 3206 /// name, with source location information. 3207 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 3208 3209 /// Retrieve the nested-name-specifier that qualifies this 3210 /// declaration. 3211 NestedNameSpecifier *getQualifier() const { 3212 return QualifierLoc.getNestedNameSpecifier(); 3213 } 3214 3215 /// Retrieve the location of the template keyword preceding 3216 /// this name, if any. 3217 SourceLocation getTemplateKeywordLoc() const { 3218 if (!hasTemplateKWAndArgsInfo()) 3219 return SourceLocation(); 3220 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; 3221 } 3222 3223 /// Retrieve the location of the left angle bracket starting the 3224 /// explicit template argument list following the name, if any. 3225 SourceLocation getLAngleLoc() const { 3226 if (!hasTemplateKWAndArgsInfo()) 3227 return SourceLocation(); 3228 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; 3229 } 3230 3231 /// Retrieve the location of the right angle bracket ending the 3232 /// explicit template argument list following the name, if any. 3233 SourceLocation getRAngleLoc() const { 3234 if (!hasTemplateKWAndArgsInfo()) 3235 return SourceLocation(); 3236 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; 3237 } 3238 3239 /// Determines whether the name was preceded by the template keyword. 3240 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 3241 3242 /// Determines whether this lookup had explicit template arguments. 3243 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 3244 3245 /// Copies the template arguments (if present) into the given 3246 /// structure. 3247 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 3248 if (hasExplicitTemplateArgs()) 3249 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( 3250 getTrailingObjects<TemplateArgumentLoc>(), List); 3251 } 3252 3253 TemplateArgumentLoc const *getTemplateArgs() const { 3254 if (!hasExplicitTemplateArgs()) 3255 return nullptr; 3256 3257 return getTrailingObjects<TemplateArgumentLoc>(); 3258 } 3259 3260 unsigned getNumTemplateArgs() const { 3261 if (!hasExplicitTemplateArgs()) 3262 return 0; 3263 3264 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; 3265 } 3266 3267 ArrayRef<TemplateArgumentLoc> template_arguments() const { 3268 return {getTemplateArgs(), getNumTemplateArgs()}; 3269 } 3270 3271 /// Note: getBeginLoc() is the start of the whole DependentScopeDeclRefExpr, 3272 /// and differs from getLocation().getStart(). 3273 SourceLocation getBeginLoc() const LLVM_READONLY { 3274 return QualifierLoc.getBeginLoc(); 3275 } 3276 3277 SourceLocation getEndLoc() const LLVM_READONLY { 3278 if (hasExplicitTemplateArgs()) 3279 return getRAngleLoc(); 3280 return getLocation(); 3281 } 3282 3283 static bool classof(const Stmt *T) { 3284 return T->getStmtClass() == DependentScopeDeclRefExprClass; 3285 } 3286 3287 child_range children() { 3288 return child_range(child_iterator(), child_iterator()); 3289 } 3290 3291 const_child_range children() const { 3292 return const_child_range(const_child_iterator(), const_child_iterator()); 3293 } 3294}; 3295 3296/// Represents an expression -- generally a full-expression -- that 3297/// introduces cleanups to be run at the end of the sub-expression's 3298/// evaluation. The most common source of expression-introduced 3299/// cleanups is temporary objects in C++, but several other kinds of 3300/// expressions can create cleanups, including basically every 3301/// call in ARC that returns an Objective-C pointer. 3302/// 3303/// This expression also tracks whether the sub-expression contains a 3304/// potentially-evaluated block literal. The lifetime of a block 3305/// literal is the extent of the enclosing scope. 3306class ExprWithCleanups final 3307 : public FullExpr, 3308 private llvm::TrailingObjects<ExprWithCleanups, BlockDecl *> { 3309public: 3310 /// The type of objects that are kept in the cleanup. 3311 /// It's useful to remember the set of blocks; we could also 3312 /// remember the set of temporaries, but there's currently 3313 /// no need. 3314 using CleanupObject = BlockDecl *; 3315 3316private: 3317 friend class ASTStmtReader; 3318 friend TrailingObjects; 3319 3320 ExprWithCleanups(EmptyShell, unsigned NumObjects); 3321 ExprWithCleanups(Expr *SubExpr, bool CleanupsHaveSideEffects, 3322 ArrayRef<CleanupObject> Objects); 3323 3324public: 3325 static ExprWithCleanups *Create(const ASTContext &C, EmptyShell empty, 3326 unsigned numObjects); 3327 3328 static ExprWithCleanups *Create(const ASTContext &C, Expr *subexpr, 3329 bool CleanupsHaveSideEffects, 3330 ArrayRef<CleanupObject> objects); 3331 3332 ArrayRef<CleanupObject> getObjects() const { 3333 return llvm::makeArrayRef(getTrailingObjects<CleanupObject>(), 3334 getNumObjects()); 3335 } 3336 3337 unsigned getNumObjects() const { return ExprWithCleanupsBits.NumObjects; } 3338 3339 CleanupObject getObject(unsigned i) const { 3340 assert(i < getNumObjects() && "Index out of range"); 3341 return getObjects()[i]; 3342 } 3343 3344 bool cleanupsHaveSideEffects() const { 3345 return ExprWithCleanupsBits.CleanupsHaveSideEffects; 3346 } 3347 3348 SourceLocation getBeginLoc() const LLVM_READONLY { 3349 return SubExpr->getBeginLoc(); 3350 } 3351 3352 SourceLocation getEndLoc() const LLVM_READONLY { 3353 return SubExpr->getEndLoc(); 3354 } 3355 3356 // Implement isa/cast/dyncast/etc. 3357 static bool classof(const Stmt *T) { 3358 return T->getStmtClass() == ExprWithCleanupsClass; 3359 } 3360 3361 // Iterators 3362 child_range children() { return child_range(&SubExpr, &SubExpr + 1); } 3363 3364 const_child_range children() const { 3365 return const_child_range(&SubExpr, &SubExpr + 1); 3366 } 3367}; 3368 3369/// Describes an explicit type conversion that uses functional 3370/// notion but could not be resolved because one or more arguments are 3371/// type-dependent. 3372/// 3373/// The explicit type conversions expressed by 3374/// CXXUnresolvedConstructExpr have the form <tt>T(a1, a2, ..., aN)</tt>, 3375/// where \c T is some type and \c a1, \c a2, ..., \c aN are values, and 3376/// either \c T is a dependent type or one or more of the <tt>a</tt>'s is 3377/// type-dependent. For example, this would occur in a template such 3378/// as: 3379/// 3380/// \code 3381/// template<typename T, typename A1> 3382/// inline T make_a(const A1& a1) { 3383/// return T(a1); 3384/// } 3385/// \endcode 3386/// 3387/// When the returned expression is instantiated, it may resolve to a 3388/// constructor call, conversion function call, or some kind of type 3389/// conversion. 3390class CXXUnresolvedConstructExpr final 3391 : public Expr, 3392 private llvm::TrailingObjects<CXXUnresolvedConstructExpr, Expr *> { 3393 friend class ASTStmtReader; 3394 friend TrailingObjects; 3395 3396 /// The type being constructed. 3397 TypeSourceInfo *TSI; 3398 3399 /// The location of the left parentheses ('('). 3400 SourceLocation LParenLoc; 3401 3402 /// The location of the right parentheses (')'). 3403 SourceLocation RParenLoc; 3404 3405 CXXUnresolvedConstructExpr(TypeSourceInfo *TSI, SourceLocation LParenLoc, 3406 ArrayRef<Expr *> Args, SourceLocation RParenLoc); 3407 3408 CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) 3409 : Expr(CXXUnresolvedConstructExprClass, Empty) { 3410 CXXUnresolvedConstructExprBits.NumArgs = NumArgs; 3411 } 3412 3413public: 3414 static CXXUnresolvedConstructExpr *Create(const ASTContext &Context, 3415 TypeSourceInfo *Type, 3416 SourceLocation LParenLoc, 3417 ArrayRef<Expr *> Args, 3418 SourceLocation RParenLoc); 3419 3420 static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context, 3421 unsigned NumArgs); 3422 3423 /// Retrieve the type that is being constructed, as specified 3424 /// in the source code. 3425 QualType getTypeAsWritten() const { return TSI->getType(); } 3426 3427 /// Retrieve the type source information for the type being 3428 /// constructed. 3429 TypeSourceInfo *getTypeSourceInfo() const { return TSI; } 3430 3431 /// Retrieve the location of the left parentheses ('(') that 3432 /// precedes the argument list. 3433 SourceLocation getLParenLoc() const { return LParenLoc; } 3434 void setLParenLoc(SourceLocation L) { LParenLoc = L; } 3435 3436 /// Retrieve the location of the right parentheses (')') that 3437 /// follows the argument list. 3438 SourceLocation getRParenLoc() const { return RParenLoc; } 3439 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3440 3441 /// Determine whether this expression models list-initialization. 3442 /// If so, there will be exactly one subexpression, which will be 3443 /// an InitListExpr. 3444 bool isListInitialization() const { return LParenLoc.isInvalid(); } 3445 3446 /// Retrieve the number of arguments. 3447 unsigned arg_size() const { return CXXUnresolvedConstructExprBits.NumArgs; } 3448 3449 using arg_iterator = Expr **; 3450 using arg_range = llvm::iterator_range<arg_iterator>; 3451 3452 arg_iterator arg_begin() { return getTrailingObjects<Expr *>(); } 3453 arg_iterator arg_end() { return arg_begin() + arg_size(); } 3454 arg_range arguments() { return arg_range(arg_begin(), arg_end()); } 3455 3456 using const_arg_iterator = const Expr* const *; 3457 using const_arg_range = llvm::iterator_range<const_arg_iterator>; 3458 3459 const_arg_iterator arg_begin() const { return getTrailingObjects<Expr *>(); } 3460 const_arg_iterator arg_end() const { return arg_begin() + arg_size(); } 3461 const_arg_range arguments() const { 3462 return const_arg_range(arg_begin(), arg_end()); 3463 } 3464 3465 Expr *getArg(unsigned I) { 3466 assert(I < arg_size() && "Argument index out-of-range"); 3467 return arg_begin()[I]; 3468 } 3469 3470 const Expr *getArg(unsigned I) const { 3471 assert(I < arg_size() && "Argument index out-of-range"); 3472 return arg_begin()[I]; 3473 } 3474 3475 void setArg(unsigned I, Expr *E) { 3476 assert(I < arg_size() && "Argument index out-of-range"); 3477 arg_begin()[I] = E; 3478 } 3479 3480 SourceLocation getBeginLoc() const LLVM_READONLY; 3481 SourceLocation getEndLoc() const LLVM_READONLY { 3482 if (!RParenLoc.isValid() && arg_size() > 0) 3483 return getArg(arg_size() - 1)->getEndLoc(); 3484 return RParenLoc; 3485 } 3486 3487 static bool classof(const Stmt *T) { 3488 return T->getStmtClass() == CXXUnresolvedConstructExprClass; 3489 } 3490 3491 // Iterators 3492 child_range children() { 3493 auto **begin = reinterpret_cast<Stmt **>(arg_begin()); 3494 return child_range(begin, begin + arg_size()); 3495 } 3496 3497 const_child_range children() const { 3498 auto **begin = reinterpret_cast<Stmt **>( 3499 const_cast<CXXUnresolvedConstructExpr *>(this)->arg_begin()); 3500 return const_child_range(begin, begin + arg_size()); 3501 } 3502}; 3503 3504/// Represents a C++ member access expression where the actual 3505/// member referenced could not be resolved because the base 3506/// expression or the member name was dependent. 3507/// 3508/// Like UnresolvedMemberExprs, these can be either implicit or 3509/// explicit accesses. It is only possible to get one of these with 3510/// an implicit access if a qualifier is provided. 3511class CXXDependentScopeMemberExpr final 3512 : public Expr, 3513 private llvm::TrailingObjects<CXXDependentScopeMemberExpr, 3514 ASTTemplateKWAndArgsInfo, 3515 TemplateArgumentLoc, NamedDecl *> { 3516 friend class ASTStmtReader; 3517 friend class ASTStmtWriter; 3518 friend TrailingObjects; 3519 3520 /// The expression for the base pointer or class reference, 3521 /// e.g., the \c x in x.f. Can be null in implicit accesses. 3522 Stmt *Base; 3523 3524 /// The type of the base expression. Never null, even for 3525 /// implicit accesses. 3526 QualType BaseType; 3527 3528 /// The nested-name-specifier that precedes the member name, if any. 3529 /// FIXME: This could be in principle store as a trailing object. 3530 /// However the performance impact of doing so should be investigated first. 3531 NestedNameSpecifierLoc QualifierLoc; 3532 3533 /// The member to which this member expression refers, which 3534 /// can be name, overloaded operator, or destructor. 3535 /// 3536 /// FIXME: could also be a template-id 3537 DeclarationNameInfo MemberNameInfo; 3538 3539 // CXXDependentScopeMemberExpr is followed by several trailing objects, 3540 // some of which optional. They are in order: 3541 // 3542 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified 3543 // template keyword and arguments. Present if and only if 3544 // hasTemplateKWAndArgsInfo(). 3545 // 3546 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing location 3547 // information for the explicitly specified template arguments. 3548 // 3549 // * An optional NamedDecl *. In a qualified member access expression such 3550 // as t->Base::f, this member stores the resolves of name lookup in the 3551 // context of the member access expression, to be used at instantiation 3552 // time. Present if and only if hasFirstQualifierFoundInScope(). 3553 3554 bool hasTemplateKWAndArgsInfo() const { 3555 return CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo; 3556 } 3557 3558 bool hasFirstQualifierFoundInScope() const { 3559 return CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope; 3560 } 3561 3562 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { 3563 return hasTemplateKWAndArgsInfo(); 3564 } 3565 3566 unsigned numTrailingObjects(OverloadToken<TemplateArgumentLoc>) const { 3567 return getNumTemplateArgs(); 3568 } 3569 3570 unsigned numTrailingObjects(OverloadToken<NamedDecl *>) const { 3571 return hasFirstQualifierFoundInScope(); 3572 } 3573 3574 CXXDependentScopeMemberExpr(const ASTContext &Ctx, Expr *Base, 3575 QualType BaseType, bool IsArrow, 3576 SourceLocation OperatorLoc, 3577 NestedNameSpecifierLoc QualifierLoc, 3578 SourceLocation TemplateKWLoc, 3579 NamedDecl *FirstQualifierFoundInScope, 3580 DeclarationNameInfo MemberNameInfo, 3581 const TemplateArgumentListInfo *TemplateArgs); 3582 3583 CXXDependentScopeMemberExpr(EmptyShell Empty, bool HasTemplateKWAndArgsInfo, 3584 bool HasFirstQualifierFoundInScope); 3585 3586public: 3587 static CXXDependentScopeMemberExpr * 3588 Create(const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow, 3589 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc, 3590 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope, 3591 DeclarationNameInfo MemberNameInfo, 3592 const TemplateArgumentListInfo *TemplateArgs); 3593 3594 static CXXDependentScopeMemberExpr * 3595 CreateEmpty(const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo, 3596 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope); 3597 3598 /// True if this is an implicit access, i.e. one in which the 3599 /// member being accessed was not written in the source. The source 3600 /// location of the operator is invalid in this case. 3601 bool isImplicitAccess() const { 3602 if (!Base) 3603 return true; 3604 return cast<Expr>(Base)->isImplicitCXXThis(); 3605 } 3606 3607 /// Retrieve the base object of this member expressions, 3608 /// e.g., the \c x in \c x.m. 3609 Expr *getBase() const { 3610 assert(!isImplicitAccess()); 3611 return cast<Expr>(Base); 3612 } 3613 3614 QualType getBaseType() const { return BaseType; } 3615 3616 /// Determine whether this member expression used the '->' 3617 /// operator; otherwise, it used the '.' operator. 3618 bool isArrow() const { return CXXDependentScopeMemberExprBits.IsArrow; } 3619 3620 /// Retrieve the location of the '->' or '.' operator. 3621 SourceLocation getOperatorLoc() const { 3622 return CXXDependentScopeMemberExprBits.OperatorLoc; 3623 } 3624 3625 /// Retrieve the nested-name-specifier that qualifies the member name. 3626 NestedNameSpecifier *getQualifier() const { 3627 return QualifierLoc.getNestedNameSpecifier(); 3628 } 3629 3630 /// Retrieve the nested-name-specifier that qualifies the member 3631 /// name, with source location information. 3632 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; } 3633 3634 /// Retrieve the first part of the nested-name-specifier that was 3635 /// found in the scope of the member access expression when the member access 3636 /// was initially parsed. 3637 /// 3638 /// This function only returns a useful result when member access expression 3639 /// uses a qualified member name, e.g., "x.Base::f". Here, the declaration 3640 /// returned by this function describes what was found by unqualified name 3641 /// lookup for the identifier "Base" within the scope of the member access 3642 /// expression itself. At template instantiation time, this information is 3643 /// combined with the results of name lookup into the type of the object 3644 /// expression itself (the class type of x). 3645 NamedDecl *getFirstQualifierFoundInScope() const { 3646 if (!hasFirstQualifierFoundInScope()) 3647 return nullptr; 3648 return *getTrailingObjects<NamedDecl *>(); 3649 } 3650 3651 /// Retrieve the name of the member that this expression refers to. 3652 const DeclarationNameInfo &getMemberNameInfo() const { 3653 return MemberNameInfo; 3654 } 3655 3656 /// Retrieve the name of the member that this expression refers to. 3657 DeclarationName getMember() const { return MemberNameInfo.getName(); } 3658 3659 // Retrieve the location of the name of the member that this 3660 // expression refers to. 3661 SourceLocation getMemberLoc() const { return MemberNameInfo.getLoc(); } 3662 3663 /// Retrieve the location of the template keyword preceding the 3664 /// member name, if any. 3665 SourceLocation getTemplateKeywordLoc() const { 3666 if (!hasTemplateKWAndArgsInfo()) 3667 return SourceLocation(); 3668 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->TemplateKWLoc; 3669 } 3670 3671 /// Retrieve the location of the left angle bracket starting the 3672 /// explicit template argument list following the member name, if any. 3673 SourceLocation getLAngleLoc() const { 3674 if (!hasTemplateKWAndArgsInfo()) 3675 return SourceLocation(); 3676 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->LAngleLoc; 3677 } 3678 3679 /// Retrieve the location of the right angle bracket ending the 3680 /// explicit template argument list following the member name, if any. 3681 SourceLocation getRAngleLoc() const { 3682 if (!hasTemplateKWAndArgsInfo()) 3683 return SourceLocation(); 3684 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->RAngleLoc; 3685 } 3686 3687 /// Determines whether the member name was preceded by the template keyword. 3688 bool hasTemplateKeyword() const { return getTemplateKeywordLoc().isValid(); } 3689 3690 /// Determines whether this member expression actually had a C++ 3691 /// template argument list explicitly specified, e.g., x.f<int>. 3692 bool hasExplicitTemplateArgs() const { return getLAngleLoc().isValid(); } 3693 3694 /// Copies the template arguments (if present) into the given 3695 /// structure. 3696 void copyTemplateArgumentsInto(TemplateArgumentListInfo &List) const { 3697 if (hasExplicitTemplateArgs()) 3698 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->copyInto( 3699 getTrailingObjects<TemplateArgumentLoc>(), List); 3700 } 3701 3702 /// Retrieve the template arguments provided as part of this 3703 /// template-id. 3704 const TemplateArgumentLoc *getTemplateArgs() const { 3705 if (!hasExplicitTemplateArgs()) 3706 return nullptr; 3707 3708 return getTrailingObjects<TemplateArgumentLoc>(); 3709 } 3710 3711 /// Retrieve the number of template arguments provided as part of this 3712 /// template-id. 3713 unsigned getNumTemplateArgs() const { 3714 if (!hasExplicitTemplateArgs()) 3715 return 0; 3716 3717 return getTrailingObjects<ASTTemplateKWAndArgsInfo>()->NumTemplateArgs; 3718 } 3719 3720 ArrayRef<TemplateArgumentLoc> template_arguments() const { 3721 return {getTemplateArgs(), getNumTemplateArgs()}; 3722 } 3723 3724 SourceLocation getBeginLoc() const LLVM_READONLY { 3725 if (!isImplicitAccess()) 3726 return Base->getBeginLoc(); 3727 if (getQualifier()) 3728 return getQualifierLoc().getBeginLoc(); 3729 return MemberNameInfo.getBeginLoc(); 3730 } 3731 3732 SourceLocation getEndLoc() const LLVM_READONLY { 3733 if (hasExplicitTemplateArgs()) 3734 return getRAngleLoc(); 3735 return MemberNameInfo.getEndLoc(); 3736 } 3737 3738 static bool classof(const Stmt *T) { 3739 return T->getStmtClass() == CXXDependentScopeMemberExprClass; 3740 } 3741 3742 // Iterators 3743 child_range children() { 3744 if (isImplicitAccess()) 3745 return child_range(child_iterator(), child_iterator()); 3746 return child_range(&Base, &Base + 1); 3747 } 3748 3749 const_child_range children() const { 3750 if (isImplicitAccess()) 3751 return const_child_range(const_child_iterator(), const_child_iterator()); 3752 return const_child_range(&Base, &Base + 1); 3753 } 3754}; 3755 3756/// Represents a C++ member access expression for which lookup 3757/// produced a set of overloaded functions. 3758/// 3759/// The member access may be explicit or implicit: 3760/// \code 3761/// struct A { 3762/// int a, b; 3763/// int explicitAccess() { return this->a + this->A::b; } 3764/// int implicitAccess() { return a + A::b; } 3765/// }; 3766/// \endcode 3767/// 3768/// In the final AST, an explicit access always becomes a MemberExpr. 3769/// An implicit access may become either a MemberExpr or a 3770/// DeclRefExpr, depending on whether the member is static. 3771class UnresolvedMemberExpr final 3772 : public OverloadExpr, 3773 private llvm::TrailingObjects<UnresolvedMemberExpr, DeclAccessPair, 3774 ASTTemplateKWAndArgsInfo, 3775 TemplateArgumentLoc> { 3776 friend class ASTStmtReader; 3777 friend class OverloadExpr; 3778 friend TrailingObjects; 3779 3780 /// The expression for the base pointer or class reference, 3781 /// e.g., the \c x in x.f. 3782 /// 3783 /// This can be null if this is an 'unbased' member expression. 3784 Stmt *Base; 3785 3786 /// The type of the base expression; never null. 3787 QualType BaseType; 3788 3789 /// The location of the '->' or '.' operator. 3790 SourceLocation OperatorLoc; 3791 3792 // UnresolvedMemberExpr is followed by several trailing objects. 3793 // They are in order: 3794 // 3795 // * An array of getNumResults() DeclAccessPair for the results. These are 3796 // undesugared, which is to say, they may include UsingShadowDecls. 3797 // Access is relative to the naming class. 3798 // 3799 // * An optional ASTTemplateKWAndArgsInfo for the explicitly specified 3800 // template keyword and arguments. Present if and only if 3801 // hasTemplateKWAndArgsInfo(). 3802 // 3803 // * An array of getNumTemplateArgs() TemplateArgumentLoc containing 3804 // location information for the explicitly specified template arguments. 3805 3806 UnresolvedMemberExpr(const ASTContext &Context, bool HasUnresolvedUsing, 3807 Expr *Base, QualType BaseType, bool IsArrow, 3808 SourceLocation OperatorLoc, 3809 NestedNameSpecifierLoc QualifierLoc, 3810 SourceLocation TemplateKWLoc, 3811 const DeclarationNameInfo &MemberNameInfo, 3812 const TemplateArgumentListInfo *TemplateArgs, 3813 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3814 3815 UnresolvedMemberExpr(EmptyShell Empty, unsigned NumResults, 3816 bool HasTemplateKWAndArgsInfo); 3817 3818 unsigned numTrailingObjects(OverloadToken<DeclAccessPair>) const { 3819 return getNumDecls(); 3820 } 3821 3822 unsigned numTrailingObjects(OverloadToken<ASTTemplateKWAndArgsInfo>) const { 3823 return hasTemplateKWAndArgsInfo(); 3824 } 3825 3826public: 3827 static UnresolvedMemberExpr * 3828 Create(const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base, 3829 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc, 3830 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, 3831 const DeclarationNameInfo &MemberNameInfo, 3832 const TemplateArgumentListInfo *TemplateArgs, 3833 UnresolvedSetIterator Begin, UnresolvedSetIterator End); 3834 3835 static UnresolvedMemberExpr *CreateEmpty(const ASTContext &Context, 3836 unsigned NumResults, 3837 bool HasTemplateKWAndArgsInfo, 3838 unsigned NumTemplateArgs); 3839 3840 /// True if this is an implicit access, i.e., one in which the 3841 /// member being accessed was not written in the source. 3842 /// 3843 /// The source location of the operator is invalid in this case. 3844 bool isImplicitAccess() const; 3845 3846 /// Retrieve the base object of this member expressions, 3847 /// e.g., the \c x in \c x.m. 3848 Expr *getBase() { 3849 assert(!isImplicitAccess()); 3850 return cast<Expr>(Base); 3851 } 3852 const Expr *getBase() const { 3853 assert(!isImplicitAccess()); 3854 return cast<Expr>(Base); 3855 } 3856 3857 QualType getBaseType() const { return BaseType; } 3858 3859 /// Determine whether the lookup results contain an unresolved using 3860 /// declaration. 3861 bool hasUnresolvedUsing() const { 3862 return UnresolvedMemberExprBits.HasUnresolvedUsing; 3863 } 3864 3865 /// Determine whether this member expression used the '->' 3866 /// operator; otherwise, it used the '.' operator. 3867 bool isArrow() const { return UnresolvedMemberExprBits.IsArrow; } 3868 3869 /// Retrieve the location of the '->' or '.' operator. 3870 SourceLocation getOperatorLoc() const { return OperatorLoc; } 3871 3872 /// Retrieve the naming class of this lookup. 3873 CXXRecordDecl *getNamingClass(); 3874 const CXXRecordDecl *getNamingClass() const { 3875 return const_cast<UnresolvedMemberExpr *>(this)->getNamingClass(); 3876 } 3877 3878 /// Retrieve the full name info for the member that this expression 3879 /// refers to. 3880 const DeclarationNameInfo &getMemberNameInfo() const { return getNameInfo(); } 3881 3882 /// Retrieve the name of the member that this expression refers to. 3883 DeclarationName getMemberName() const { return getName(); } 3884 3885 /// Retrieve the location of the name of the member that this 3886 /// expression refers to. 3887 SourceLocation getMemberLoc() const { return getNameLoc(); } 3888 3889 /// Return the preferred location (the member name) for the arrow when 3890 /// diagnosing a problem with this expression. 3891 SourceLocation getExprLoc() const LLVM_READONLY { return getMemberLoc(); } 3892 3893 SourceLocation getBeginLoc() const LLVM_READONLY { 3894 if (!isImplicitAccess()) 3895 return Base->getBeginLoc(); 3896 if (NestedNameSpecifierLoc l = getQualifierLoc()) 3897 return l.getBeginLoc(); 3898 return getMemberNameInfo().getBeginLoc(); 3899 } 3900 3901 SourceLocation getEndLoc() const LLVM_READONLY { 3902 if (hasExplicitTemplateArgs()) 3903 return getRAngleLoc(); 3904 return getMemberNameInfo().getEndLoc(); 3905 } 3906 3907 static bool classof(const Stmt *T) { 3908 return T->getStmtClass() == UnresolvedMemberExprClass; 3909 } 3910 3911 // Iterators 3912 child_range children() { 3913 if (isImplicitAccess()) 3914 return child_range(child_iterator(), child_iterator()); 3915 return child_range(&Base, &Base + 1); 3916 } 3917 3918 const_child_range children() const { 3919 if (isImplicitAccess()) 3920 return const_child_range(const_child_iterator(), const_child_iterator()); 3921 return const_child_range(&Base, &Base + 1); 3922 } 3923}; 3924 3925DeclAccessPair *OverloadExpr::getTrailingResults() { 3926 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this)) 3927 return ULE->getTrailingObjects<DeclAccessPair>(); 3928 return cast<UnresolvedMemberExpr>(this)->getTrailingObjects<DeclAccessPair>(); 3929} 3930 3931ASTTemplateKWAndArgsInfo *OverloadExpr::getTrailingASTTemplateKWAndArgsInfo() { 3932 if (!hasTemplateKWAndArgsInfo()) 3933 return nullptr; 3934 3935 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this)) 3936 return ULE->getTrailingObjects<ASTTemplateKWAndArgsInfo>(); 3937 return cast<UnresolvedMemberExpr>(this) 3938 ->getTrailingObjects<ASTTemplateKWAndArgsInfo>(); 3939} 3940 3941TemplateArgumentLoc *OverloadExpr::getTrailingTemplateArgumentLoc() { 3942 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this)) 3943 return ULE->getTrailingObjects<TemplateArgumentLoc>(); 3944 return cast<UnresolvedMemberExpr>(this) 3945 ->getTrailingObjects<TemplateArgumentLoc>(); 3946} 3947 3948CXXRecordDecl *OverloadExpr::getNamingClass() { 3949 if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(this)) 3950 return ULE->getNamingClass(); 3951 return cast<UnresolvedMemberExpr>(this)->getNamingClass(); 3952} 3953 3954/// Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]). 3955/// 3956/// The noexcept expression tests whether a given expression might throw. Its 3957/// result is a boolean constant. 3958class CXXNoexceptExpr : public Expr { 3959 friend class ASTStmtReader; 3960 3961 Stmt *Operand; 3962 SourceRange Range; 3963 3964public: 3965 CXXNoexceptExpr(QualType Ty, Expr *Operand, CanThrowResult Val, 3966 SourceLocation Keyword, SourceLocation RParen) 3967 : Expr(CXXNoexceptExprClass, Ty, VK_RValue, OK_Ordinary, 3968 /*TypeDependent*/ false, 3969 /*ValueDependent*/ Val == CT_Dependent, 3970 Val == CT_Dependent || Operand->isInstantiationDependent(), 3971 Operand->containsUnexpandedParameterPack()), 3972 Operand(Operand), Range(Keyword, RParen) { 3973 CXXNoexceptExprBits.Value = Val == CT_Cannot; 3974 } 3975 3976 CXXNoexceptExpr(EmptyShell Empty) : Expr(CXXNoexceptExprClass, Empty) {} 3977 3978 Expr *getOperand() const { return static_cast<Expr *>(Operand); } 3979 3980 SourceLocation getBeginLoc() const { return Range.getBegin(); } 3981 SourceLocation getEndLoc() const { return Range.getEnd(); } 3982 SourceRange getSourceRange() const { return Range; } 3983 3984 bool getValue() const { return CXXNoexceptExprBits.Value; } 3985 3986 static bool classof(const Stmt *T) { 3987 return T->getStmtClass() == CXXNoexceptExprClass; 3988 } 3989 3990 // Iterators 3991 child_range children() { return child_range(&Operand, &Operand + 1); } 3992 3993 const_child_range children() const { 3994 return const_child_range(&Operand, &Operand + 1); 3995 } 3996}; 3997 3998/// Represents a C++11 pack expansion that produces a sequence of 3999/// expressions. 4000/// 4001/// A pack expansion expression contains a pattern (which itself is an 4002/// expression) followed by an ellipsis. For example: 4003/// 4004/// \code 4005/// template<typename F, typename ...Types> 4006/// void forward(F f, Types &&...args) { 4007/// f(static_cast<Types&&>(args)...); 4008/// } 4009/// \endcode 4010/// 4011/// Here, the argument to the function object \c f is a pack expansion whose 4012/// pattern is \c static_cast<Types&&>(args). When the \c forward function 4013/// template is instantiated, the pack expansion will instantiate to zero or 4014/// or more function arguments to the function object \c f. 4015class PackExpansionExpr : public Expr { 4016 friend class ASTStmtReader; 4017 friend class ASTStmtWriter; 4018 4019 SourceLocation EllipsisLoc; 4020 4021 /// The number of expansions that will be produced by this pack 4022 /// expansion expression, if known. 4023 /// 4024 /// When zero, the number of expansions is not known. Otherwise, this value 4025 /// is the number of expansions + 1. 4026 unsigned NumExpansions; 4027 4028 Stmt *Pattern; 4029 4030public: 4031 PackExpansionExpr(QualType T, Expr *Pattern, SourceLocation EllipsisLoc, 4032 Optional<unsigned> NumExpansions) 4033 : Expr(PackExpansionExprClass, T, Pattern->getValueKind(), 4034 Pattern->getObjectKind(), /*TypeDependent=*/true, 4035 /*ValueDependent=*/true, /*InstantiationDependent=*/true, 4036 /*ContainsUnexpandedParameterPack=*/false), 4037 EllipsisLoc(EllipsisLoc), 4038 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0), 4039 Pattern(Pattern) {} 4040 4041 PackExpansionExpr(EmptyShell Empty) : Expr(PackExpansionExprClass, Empty) {} 4042 4043 /// Retrieve the pattern of the pack expansion. 4044 Expr *getPattern() { return reinterpret_cast<Expr *>(Pattern); } 4045 4046 /// Retrieve the pattern of the pack expansion. 4047 const Expr *getPattern() const { return reinterpret_cast<Expr *>(Pattern); } 4048 4049 /// Retrieve the location of the ellipsis that describes this pack 4050 /// expansion. 4051 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 4052 4053 /// Determine the number of expansions that will be produced when 4054 /// this pack expansion is instantiated, if already known. 4055 Optional<unsigned> getNumExpansions() const { 4056 if (NumExpansions) 4057 return NumExpansions - 1; 4058 4059 return None; 4060 } 4061 4062 SourceLocation getBeginLoc() const LLVM_READONLY { 4063 return Pattern->getBeginLoc(); 4064 } 4065 4066 SourceLocation getEndLoc() const LLVM_READONLY { return EllipsisLoc; } 4067 4068 static bool classof(const Stmt *T) { 4069 return T->getStmtClass() == PackExpansionExprClass; 4070 } 4071 4072 // Iterators 4073 child_range children() { 4074 return child_range(&Pattern, &Pattern + 1); 4075 } 4076 4077 const_child_range children() const { 4078 return const_child_range(&Pattern, &Pattern + 1); 4079 } 4080}; 4081 4082/// Represents an expression that computes the length of a parameter 4083/// pack. 4084/// 4085/// \code 4086/// template<typename ...Types> 4087/// struct count { 4088/// static const unsigned value = sizeof...(Types); 4089/// }; 4090/// \endcode 4091class SizeOfPackExpr final 4092 : public Expr, 4093 private llvm::TrailingObjects<SizeOfPackExpr, TemplateArgument> { 4094 friend class ASTStmtReader; 4095 friend class ASTStmtWriter; 4096 friend TrailingObjects; 4097 4098 /// The location of the \c sizeof keyword. 4099 SourceLocation OperatorLoc; 4100 4101 /// The location of the name of the parameter pack. 4102 SourceLocation PackLoc; 4103 4104 /// The location of the closing parenthesis. 4105 SourceLocation RParenLoc; 4106 4107 /// The length of the parameter pack, if known. 4108 /// 4109 /// When this expression is not value-dependent, this is the length of 4110 /// the pack. When the expression was parsed rather than instantiated 4111 /// (and thus is value-dependent), this is zero. 4112 /// 4113 /// After partial substitution into a sizeof...(X) expression (for instance, 4114 /// within an alias template or during function template argument deduction), 4115 /// we store a trailing array of partially-substituted TemplateArguments, 4116 /// and this is the length of that array. 4117 unsigned Length; 4118 4119 /// The parameter pack. 4120 NamedDecl *Pack = nullptr; 4121 4122 /// Create an expression that computes the length of 4123 /// the given parameter pack. 4124 SizeOfPackExpr(QualType SizeType, SourceLocation OperatorLoc, NamedDecl *Pack, 4125 SourceLocation PackLoc, SourceLocation RParenLoc, 4126 Optional<unsigned> Length, ArrayRef<TemplateArgument> PartialArgs) 4127 : Expr(SizeOfPackExprClass, SizeType, VK_RValue, OK_Ordinary, 4128 /*TypeDependent=*/false, /*ValueDependent=*/!Length, 4129 /*InstantiationDependent=*/!Length, 4130 /*ContainsUnexpandedParameterPack=*/false), 4131 OperatorLoc(OperatorLoc), PackLoc(PackLoc), RParenLoc(RParenLoc), 4132 Length(Length ? *Length : PartialArgs.size()), Pack(Pack) { 4133 assert((!Length || PartialArgs.empty()) && 4134 "have partial args for non-dependent sizeof... expression"); 4135 auto *Args = getTrailingObjects<TemplateArgument>(); 4136 std::uninitialized_copy(PartialArgs.begin(), PartialArgs.end(), Args); 4137 } 4138 4139 /// Create an empty expression. 4140 SizeOfPackExpr(EmptyShell Empty, unsigned NumPartialArgs) 4141 : Expr(SizeOfPackExprClass, Empty), Length(NumPartialArgs) {} 4142 4143public: 4144 static SizeOfPackExpr *Create(ASTContext &Context, SourceLocation OperatorLoc, 4145 NamedDecl *Pack, SourceLocation PackLoc, 4146 SourceLocation RParenLoc, 4147 Optional<unsigned> Length = None, 4148 ArrayRef<TemplateArgument> PartialArgs = None); 4149 static SizeOfPackExpr *CreateDeserialized(ASTContext &Context, 4150 unsigned NumPartialArgs); 4151 4152 /// Determine the location of the 'sizeof' keyword. 4153 SourceLocation getOperatorLoc() const { return OperatorLoc; } 4154 4155 /// Determine the location of the parameter pack. 4156 SourceLocation getPackLoc() const { return PackLoc; } 4157 4158 /// Determine the location of the right parenthesis. 4159 SourceLocation getRParenLoc() const { return RParenLoc; } 4160 4161 /// Retrieve the parameter pack. 4162 NamedDecl *getPack() const { return Pack; } 4163 4164 /// Retrieve the length of the parameter pack. 4165 /// 4166 /// This routine may only be invoked when the expression is not 4167 /// value-dependent. 4168 unsigned getPackLength() const { 4169 assert(!isValueDependent() && 4170 "Cannot get the length of a value-dependent pack size expression"); 4171 return Length; 4172 } 4173 4174 /// Determine whether this represents a partially-substituted sizeof... 4175 /// expression, such as is produced for: 4176 /// 4177 /// template<typename ...Ts> using X = int[sizeof...(Ts)]; 4178 /// template<typename ...Us> void f(X<Us..., 1, 2, 3, Us...>); 4179 bool isPartiallySubstituted() const { 4180 return isValueDependent() && Length; 4181 } 4182 4183 /// Get 4184 ArrayRef<TemplateArgument> getPartialArguments() const { 4185 assert(isPartiallySubstituted()); 4186 const auto *Args = getTrailingObjects<TemplateArgument>(); 4187 return llvm::makeArrayRef(Args, Args + Length); 4188 } 4189 4190 SourceLocation getBeginLoc() const LLVM_READONLY { return OperatorLoc; } 4191 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 4192 4193 static bool classof(const Stmt *T) { 4194 return T->getStmtClass() == SizeOfPackExprClass; 4195 } 4196 4197 // Iterators 4198 child_range children() { 4199 return child_range(child_iterator(), child_iterator()); 4200 } 4201 4202 const_child_range children() const { 4203 return const_child_range(const_child_iterator(), const_child_iterator()); 4204 } 4205}; 4206 4207/// Represents a reference to a non-type template parameter 4208/// that has been substituted with a template argument. 4209class SubstNonTypeTemplateParmExpr : public Expr { 4210 friend class ASTReader; 4211 friend class ASTStmtReader; 4212 4213 /// The replaced parameter. 4214 NonTypeTemplateParmDecl *Param; 4215 4216 /// The replacement expression. 4217 Stmt *Replacement; 4218 4219 explicit SubstNonTypeTemplateParmExpr(EmptyShell Empty) 4220 : Expr(SubstNonTypeTemplateParmExprClass, Empty) {} 4221 4222public: 4223 SubstNonTypeTemplateParmExpr(QualType Ty, ExprValueKind ValueKind, 4224 SourceLocation Loc, 4225 NonTypeTemplateParmDecl *Param, 4226 Expr *Replacement) 4227 : Expr(SubstNonTypeTemplateParmExprClass, Ty, ValueKind, OK_Ordinary, 4228 Replacement->isTypeDependent(), Replacement->isValueDependent(), 4229 Replacement->isInstantiationDependent(), 4230 Replacement->containsUnexpandedParameterPack()), 4231 Param(Param), Replacement(Replacement) { 4232 SubstNonTypeTemplateParmExprBits.NameLoc = Loc; 4233 } 4234 4235 SourceLocation getNameLoc() const { 4236 return SubstNonTypeTemplateParmExprBits.NameLoc; 4237 } 4238 SourceLocation getBeginLoc() const { return getNameLoc(); } 4239 SourceLocation getEndLoc() const { return getNameLoc(); } 4240 4241 Expr *getReplacement() const { return cast<Expr>(Replacement); } 4242 4243 NonTypeTemplateParmDecl *getParameter() const { return Param; } 4244 4245 static bool classof(const Stmt *s) { 4246 return s->getStmtClass() == SubstNonTypeTemplateParmExprClass; 4247 } 4248 4249 // Iterators 4250 child_range children() { return child_range(&Replacement, &Replacement + 1); } 4251 4252 const_child_range children() const { 4253 return const_child_range(&Replacement, &Replacement + 1); 4254 } 4255}; 4256 4257/// Represents a reference to a non-type template parameter pack that 4258/// has been substituted with a non-template argument pack. 4259/// 4260/// When a pack expansion in the source code contains multiple parameter packs 4261/// and those parameter packs correspond to different levels of template 4262/// parameter lists, this node is used to represent a non-type template 4263/// parameter pack from an outer level, which has already had its argument pack 4264/// substituted but that still lives within a pack expansion that itself 4265/// could not be instantiated. When actually performing a substitution into 4266/// that pack expansion (e.g., when all template parameters have corresponding 4267/// arguments), this type will be replaced with the appropriate underlying 4268/// expression at the current pack substitution index. 4269class SubstNonTypeTemplateParmPackExpr : public Expr { 4270 friend class ASTReader; 4271 friend class ASTStmtReader; 4272 4273 /// The non-type template parameter pack itself. 4274 NonTypeTemplateParmDecl *Param; 4275 4276 /// A pointer to the set of template arguments that this 4277 /// parameter pack is instantiated with. 4278 const TemplateArgument *Arguments; 4279 4280 /// The number of template arguments in \c Arguments. 4281 unsigned NumArguments; 4282 4283 /// The location of the non-type template parameter pack reference. 4284 SourceLocation NameLoc; 4285 4286 explicit SubstNonTypeTemplateParmPackExpr(EmptyShell Empty) 4287 : Expr(SubstNonTypeTemplateParmPackExprClass, Empty) {} 4288 4289public: 4290 SubstNonTypeTemplateParmPackExpr(QualType T, 4291 ExprValueKind ValueKind, 4292 NonTypeTemplateParmDecl *Param, 4293 SourceLocation NameLoc, 4294 const TemplateArgument &ArgPack); 4295 4296 /// Retrieve the non-type template parameter pack being substituted. 4297 NonTypeTemplateParmDecl *getParameterPack() const { return Param; } 4298 4299 /// Retrieve the location of the parameter pack name. 4300 SourceLocation getParameterPackLocation() const { return NameLoc; } 4301 4302 /// Retrieve the template argument pack containing the substituted 4303 /// template arguments. 4304 TemplateArgument getArgumentPack() const; 4305 4306 SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; } 4307 SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; } 4308 4309 static bool classof(const Stmt *T) { 4310 return T->getStmtClass() == SubstNonTypeTemplateParmPackExprClass; 4311 } 4312 4313 // Iterators 4314 child_range children() { 4315 return child_range(child_iterator(), child_iterator()); 4316 } 4317 4318 const_child_range children() const { 4319 return const_child_range(const_child_iterator(), const_child_iterator()); 4320 } 4321}; 4322 4323/// Represents a reference to a function parameter pack or init-capture pack 4324/// that has been substituted but not yet expanded. 4325/// 4326/// When a pack expansion contains multiple parameter packs at different levels, 4327/// this node is used to represent a function parameter pack at an outer level 4328/// which we have already substituted to refer to expanded parameters, but where 4329/// the containing pack expansion cannot yet be expanded. 4330/// 4331/// \code 4332/// template<typename...Ts> struct S { 4333/// template<typename...Us> auto f(Ts ...ts) -> decltype(g(Us(ts)...)); 4334/// }; 4335/// template struct S<int, int>; 4336/// \endcode 4337class FunctionParmPackExpr final 4338 : public Expr, 4339 private llvm::TrailingObjects<FunctionParmPackExpr, VarDecl *> { 4340 friend class ASTReader; 4341 friend class ASTStmtReader; 4342 friend TrailingObjects; 4343 4344 /// The function parameter pack which was referenced. 4345 VarDecl *ParamPack; 4346 4347 /// The location of the function parameter pack reference. 4348 SourceLocation NameLoc; 4349 4350 /// The number of expansions of this pack. 4351 unsigned NumParameters; 4352 4353 FunctionParmPackExpr(QualType T, VarDecl *ParamPack, 4354 SourceLocation NameLoc, unsigned NumParams, 4355 VarDecl *const *Params); 4356 4357public: 4358 static FunctionParmPackExpr *Create(const ASTContext &Context, QualType T, 4359 VarDecl *ParamPack, 4360 SourceLocation NameLoc, 4361 ArrayRef<VarDecl *> Params); 4362 static FunctionParmPackExpr *CreateEmpty(const ASTContext &Context, 4363 unsigned NumParams); 4364 4365 /// Get the parameter pack which this expression refers to. 4366 VarDecl *getParameterPack() const { return ParamPack; } 4367 4368 /// Get the location of the parameter pack. 4369 SourceLocation getParameterPackLocation() const { return NameLoc; } 4370 4371 /// Iterators over the parameters which the parameter pack expanded 4372 /// into. 4373 using iterator = VarDecl * const *; 4374 iterator begin() const { return getTrailingObjects<VarDecl *>(); } 4375 iterator end() const { return begin() + NumParameters; } 4376 4377 /// Get the number of parameters in this parameter pack. 4378 unsigned getNumExpansions() const { return NumParameters; } 4379 4380 /// Get an expansion of the parameter pack by index. 4381 VarDecl *getExpansion(unsigned I) const { return begin()[I]; } 4382 4383 SourceLocation getBeginLoc() const LLVM_READONLY { return NameLoc; } 4384 SourceLocation getEndLoc() const LLVM_READONLY { return NameLoc; } 4385 4386 static bool classof(const Stmt *T) { 4387 return T->getStmtClass() == FunctionParmPackExprClass; 4388 } 4389 4390 child_range children() { 4391 return child_range(child_iterator(), child_iterator()); 4392 } 4393 4394 const_child_range children() const { 4395 return const_child_range(const_child_iterator(), const_child_iterator()); 4396 } 4397}; 4398 4399/// Represents a prvalue temporary that is written into memory so that 4400/// a reference can bind to it. 4401/// 4402/// Prvalue expressions are materialized when they need to have an address 4403/// in memory for a reference to bind to. This happens when binding a 4404/// reference to the result of a conversion, e.g., 4405/// 4406/// \code 4407/// const int &r = 1.0; 4408/// \endcode 4409/// 4410/// Here, 1.0 is implicitly converted to an \c int. That resulting \c int is 4411/// then materialized via a \c MaterializeTemporaryExpr, and the reference 4412/// binds to the temporary. \c MaterializeTemporaryExprs are always glvalues 4413/// (either an lvalue or an xvalue, depending on the kind of reference binding 4414/// to it), maintaining the invariant that references always bind to glvalues. 4415/// 4416/// Reference binding and copy-elision can both extend the lifetime of a 4417/// temporary. When either happens, the expression will also track the 4418/// declaration which is responsible for the lifetime extension. 4419class MaterializeTemporaryExpr : public Expr { 4420private: 4421 friend class ASTStmtReader; 4422 friend class ASTStmtWriter; 4423 4424 llvm::PointerUnion<Stmt *, LifetimeExtendedTemporaryDecl *> State; 4425 4426public: 4427 MaterializeTemporaryExpr(QualType T, Expr *Temporary, 4428 bool BoundToLvalueReference, 4429 LifetimeExtendedTemporaryDecl *MTD = nullptr); 4430 4431 MaterializeTemporaryExpr(EmptyShell Empty) 4432 : Expr(MaterializeTemporaryExprClass, Empty) {} 4433 4434 /// Retrieve the temporary-generating subexpression whose value will 4435 /// be materialized into a glvalue. 4436 Expr *getSubExpr() const { 4437 return cast<Expr>( 4438 State.is<Stmt *>() 4439 ? State.get<Stmt *>() 4440 : State.get<LifetimeExtendedTemporaryDecl *>()->getTemporaryExpr()); 4441 } 4442 4443 /// Retrieve the storage duration for the materialized temporary. 4444 StorageDuration getStorageDuration() const { 4445 return State.is<Stmt *>() ? SD_FullExpression 4446 : State.get<LifetimeExtendedTemporaryDecl *>() 4447 ->getStorageDuration(); 4448 } 4449 4450 /// Get the storage for the constant value of a materialized temporary 4451 /// of static storage duration. 4452 APValue *getOrCreateValue(bool MayCreate) const { 4453 assert(State.is<LifetimeExtendedTemporaryDecl *>() && 4454 "the temporary has not been lifetime extended"); 4455 return State.get<LifetimeExtendedTemporaryDecl *>()->getOrCreateValue( 4456 MayCreate); 4457 } 4458 4459 LifetimeExtendedTemporaryDecl *getLifetimeExtendedTemporaryDecl() { 4460 return State.dyn_cast<LifetimeExtendedTemporaryDecl *>(); 4461 } 4462 const LifetimeExtendedTemporaryDecl * 4463 getLifetimeExtendedTemporaryDecl() const { 4464 return State.dyn_cast<LifetimeExtendedTemporaryDecl *>(); 4465 } 4466 4467 /// Get the declaration which triggered the lifetime-extension of this 4468 /// temporary, if any. 4469 ValueDecl *getExtendingDecl() { 4470 return State.is<Stmt *>() ? nullptr 4471 : State.get<LifetimeExtendedTemporaryDecl *>() 4472 ->getExtendingDecl(); 4473 } 4474 const ValueDecl *getExtendingDecl() const { 4475 return const_cast<MaterializeTemporaryExpr *>(this)->getExtendingDecl(); 4476 } 4477 4478 void setExtendingDecl(ValueDecl *ExtendedBy, unsigned ManglingNumber); 4479 4480 unsigned getManglingNumber() const { 4481 return State.is<Stmt *>() ? 0 4482 : State.get<LifetimeExtendedTemporaryDecl *>() 4483 ->getManglingNumber(); 4484 } 4485 4486 /// Determine whether this materialized temporary is bound to an 4487 /// lvalue reference; otherwise, it's bound to an rvalue reference. 4488 bool isBoundToLvalueReference() const { 4489 return getValueKind() == VK_LValue; 4490 } 4491 4492 SourceLocation getBeginLoc() const LLVM_READONLY { 4493 return getSubExpr()->getBeginLoc(); 4494 } 4495 4496 SourceLocation getEndLoc() const LLVM_READONLY { 4497 return getSubExpr()->getEndLoc(); 4498 } 4499 4500 static bool classof(const Stmt *T) { 4501 return T->getStmtClass() == MaterializeTemporaryExprClass; 4502 } 4503 4504 // Iterators 4505 child_range children() { 4506 return State.is<Stmt *>() 4507 ? child_range(State.getAddrOfPtr1(), State.getAddrOfPtr1() + 1) 4508 : State.get<LifetimeExtendedTemporaryDecl *>()->childrenExpr(); 4509 } 4510 4511 const_child_range children() const { 4512 return State.is<Stmt *>() 4513 ? const_child_range(State.getAddrOfPtr1(), 4514 State.getAddrOfPtr1() + 1) 4515 : const_cast<const LifetimeExtendedTemporaryDecl *>( 4516 State.get<LifetimeExtendedTemporaryDecl *>()) 4517 ->childrenExpr(); 4518 } 4519}; 4520 4521/// Represents a folding of a pack over an operator. 4522/// 4523/// This expression is always dependent and represents a pack expansion of the 4524/// forms: 4525/// 4526/// ( expr op ... ) 4527/// ( ... op expr ) 4528/// ( expr op ... op expr ) 4529class CXXFoldExpr : public Expr { 4530 friend class ASTStmtReader; 4531 friend class ASTStmtWriter; 4532 4533 SourceLocation LParenLoc; 4534 SourceLocation EllipsisLoc; 4535 SourceLocation RParenLoc; 4536 // When 0, the number of expansions is not known. Otherwise, this is one more 4537 // than the number of expansions. 4538 unsigned NumExpansions; 4539 Stmt *SubExprs[2]; 4540 BinaryOperatorKind Opcode; 4541 4542public: 4543 CXXFoldExpr(QualType T, SourceLocation LParenLoc, Expr *LHS, 4544 BinaryOperatorKind Opcode, SourceLocation EllipsisLoc, Expr *RHS, 4545 SourceLocation RParenLoc, Optional<unsigned> NumExpansions) 4546 : Expr(CXXFoldExprClass, T, VK_RValue, OK_Ordinary, 4547 /*Dependent*/ true, true, true, 4548 /*ContainsUnexpandedParameterPack*/ false), 4549 LParenLoc(LParenLoc), EllipsisLoc(EllipsisLoc), RParenLoc(RParenLoc), 4550 NumExpansions(NumExpansions ? *NumExpansions + 1 : 0), Opcode(Opcode) { 4551 SubExprs[0] = LHS; 4552 SubExprs[1] = RHS; 4553 } 4554 4555 CXXFoldExpr(EmptyShell Empty) : Expr(CXXFoldExprClass, Empty) {} 4556 4557 Expr *getLHS() const { return static_cast<Expr*>(SubExprs[0]); } 4558 Expr *getRHS() const { return static_cast<Expr*>(SubExprs[1]); } 4559 4560 /// Does this produce a right-associated sequence of operators? 4561 bool isRightFold() const { 4562 return getLHS() && getLHS()->containsUnexpandedParameterPack(); 4563 } 4564 4565 /// Does this produce a left-associated sequence of operators? 4566 bool isLeftFold() const { return !isRightFold(); } 4567 4568 /// Get the pattern, that is, the operand that contains an unexpanded pack. 4569 Expr *getPattern() const { return isLeftFold() ? getRHS() : getLHS(); } 4570 4571 /// Get the operand that doesn't contain a pack, for a binary fold. 4572 Expr *getInit() const { return isLeftFold() ? getLHS() : getRHS(); } 4573 4574 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 4575 BinaryOperatorKind getOperator() const { return Opcode; } 4576 4577 Optional<unsigned> getNumExpansions() const { 4578 if (NumExpansions) 4579 return NumExpansions - 1; 4580 return None; 4581 } 4582 4583 SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; } 4584 4585 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 4586 4587 static bool classof(const Stmt *T) { 4588 return T->getStmtClass() == CXXFoldExprClass; 4589 } 4590 4591 // Iterators 4592 child_range children() { return child_range(SubExprs, SubExprs + 2); } 4593 4594 const_child_range children() const { 4595 return const_child_range(SubExprs, SubExprs + 2); 4596 } 4597}; 4598 4599/// Represents an expression that might suspend coroutine execution; 4600/// either a co_await or co_yield expression. 4601/// 4602/// Evaluation of this expression first evaluates its 'ready' expression. If 4603/// that returns 'false': 4604/// -- execution of the coroutine is suspended 4605/// -- the 'suspend' expression is evaluated 4606/// -- if the 'suspend' expression returns 'false', the coroutine is 4607/// resumed 4608/// -- otherwise, control passes back to the resumer. 4609/// If the coroutine is not suspended, or when it is resumed, the 'resume' 4610/// expression is evaluated, and its result is the result of the overall 4611/// expression. 4612class CoroutineSuspendExpr : public Expr { 4613 friend class ASTStmtReader; 4614 4615 SourceLocation KeywordLoc; 4616 4617 enum SubExpr { Common, Ready, Suspend, Resume, Count }; 4618 4619 Stmt *SubExprs[SubExpr::Count]; 4620 OpaqueValueExpr *OpaqueValue = nullptr; 4621 4622public: 4623 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, Expr *Common, 4624 Expr *Ready, Expr *Suspend, Expr *Resume, 4625 OpaqueValueExpr *OpaqueValue) 4626 : Expr(SC, Resume->getType(), Resume->getValueKind(), 4627 Resume->getObjectKind(), Resume->isTypeDependent(), 4628 Resume->isValueDependent(), Common->isInstantiationDependent(), 4629 Common->containsUnexpandedParameterPack()), 4630 KeywordLoc(KeywordLoc), OpaqueValue(OpaqueValue) { 4631 SubExprs[SubExpr::Common] = Common; 4632 SubExprs[SubExpr::Ready] = Ready; 4633 SubExprs[SubExpr::Suspend] = Suspend; 4634 SubExprs[SubExpr::Resume] = Resume; 4635 } 4636 4637 CoroutineSuspendExpr(StmtClass SC, SourceLocation KeywordLoc, QualType Ty, 4638 Expr *Common) 4639 : Expr(SC, Ty, VK_RValue, OK_Ordinary, true, true, true, 4640 Common->containsUnexpandedParameterPack()), 4641 KeywordLoc(KeywordLoc) { 4642 assert(Common->isTypeDependent() && Ty->isDependentType() && 4643 "wrong constructor for non-dependent co_await/co_yield expression"); 4644 SubExprs[SubExpr::Common] = Common; 4645 SubExprs[SubExpr::Ready] = nullptr; 4646 SubExprs[SubExpr::Suspend] = nullptr; 4647 SubExprs[SubExpr::Resume] = nullptr; 4648 } 4649 4650 CoroutineSuspendExpr(StmtClass SC, EmptyShell Empty) : Expr(SC, Empty) { 4651 SubExprs[SubExpr::Common] = nullptr; 4652 SubExprs[SubExpr::Ready] = nullptr; 4653 SubExprs[SubExpr::Suspend] = nullptr; 4654 SubExprs[SubExpr::Resume] = nullptr; 4655 } 4656 4657 SourceLocation getKeywordLoc() const { return KeywordLoc; } 4658 4659 Expr *getCommonExpr() const { 4660 return static_cast<Expr*>(SubExprs[SubExpr::Common]); 4661 } 4662 4663 /// getOpaqueValue - Return the opaque value placeholder. 4664 OpaqueValueExpr *getOpaqueValue() const { return OpaqueValue; } 4665 4666 Expr *getReadyExpr() const { 4667 return static_cast<Expr*>(SubExprs[SubExpr::Ready]); 4668 } 4669 4670 Expr *getSuspendExpr() const { 4671 return static_cast<Expr*>(SubExprs[SubExpr::Suspend]); 4672 } 4673 4674 Expr *getResumeExpr() const { 4675 return static_cast<Expr*>(SubExprs[SubExpr::Resume]); 4676 } 4677 4678 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; } 4679 4680 SourceLocation getEndLoc() const LLVM_READONLY { 4681 return getCommonExpr()->getEndLoc(); 4682 } 4683 4684 child_range children() { 4685 return child_range(SubExprs, SubExprs + SubExpr::Count); 4686 } 4687 4688 const_child_range children() const { 4689 return const_child_range(SubExprs, SubExprs + SubExpr::Count); 4690 } 4691 4692 static bool classof(const Stmt *T) { 4693 return T->getStmtClass() == CoawaitExprClass || 4694 T->getStmtClass() == CoyieldExprClass; 4695 } 4696}; 4697 4698/// Represents a 'co_await' expression. 4699class CoawaitExpr : public CoroutineSuspendExpr { 4700 friend class ASTStmtReader; 4701 4702public: 4703 CoawaitExpr(SourceLocation CoawaitLoc, Expr *Operand, Expr *Ready, 4704 Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue, 4705 bool IsImplicit = false) 4706 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Operand, Ready, 4707 Suspend, Resume, OpaqueValue) { 4708 CoawaitBits.IsImplicit = IsImplicit; 4709 } 4710 4711 CoawaitExpr(SourceLocation CoawaitLoc, QualType Ty, Expr *Operand, 4712 bool IsImplicit = false) 4713 : CoroutineSuspendExpr(CoawaitExprClass, CoawaitLoc, Ty, Operand) { 4714 CoawaitBits.IsImplicit = IsImplicit; 4715 } 4716 4717 CoawaitExpr(EmptyShell Empty) 4718 : CoroutineSuspendExpr(CoawaitExprClass, Empty) {} 4719 4720 Expr *getOperand() const { 4721 // FIXME: Dig out the actual operand or store it. 4722 return getCommonExpr(); 4723 } 4724 4725 bool isImplicit() const { return CoawaitBits.IsImplicit; } 4726 void setIsImplicit(bool value = true) { CoawaitBits.IsImplicit = value; } 4727 4728 static bool classof(const Stmt *T) { 4729 return T->getStmtClass() == CoawaitExprClass; 4730 } 4731}; 4732 4733/// Represents a 'co_await' expression while the type of the promise 4734/// is dependent. 4735class DependentCoawaitExpr : public Expr { 4736 friend class ASTStmtReader; 4737 4738 SourceLocation KeywordLoc; 4739 Stmt *SubExprs[2]; 4740 4741public: 4742 DependentCoawaitExpr(SourceLocation KeywordLoc, QualType Ty, Expr *Op, 4743 UnresolvedLookupExpr *OpCoawait) 4744 : Expr(DependentCoawaitExprClass, Ty, VK_RValue, OK_Ordinary, 4745 /*TypeDependent*/ true, /*ValueDependent*/ true, 4746 /*InstantiationDependent*/ true, 4747 Op->containsUnexpandedParameterPack()), 4748 KeywordLoc(KeywordLoc) { 4749 // NOTE: A co_await expression is dependent on the coroutines promise 4750 // type and may be dependent even when the `Op` expression is not. 4751 assert(Ty->isDependentType() && 4752 "wrong constructor for non-dependent co_await/co_yield expression"); 4753 SubExprs[0] = Op; 4754 SubExprs[1] = OpCoawait; 4755 } 4756 4757 DependentCoawaitExpr(EmptyShell Empty) 4758 : Expr(DependentCoawaitExprClass, Empty) {} 4759 4760 Expr *getOperand() const { return cast<Expr>(SubExprs[0]); } 4761 4762 UnresolvedLookupExpr *getOperatorCoawaitLookup() const { 4763 return cast<UnresolvedLookupExpr>(SubExprs[1]); 4764 } 4765 4766 SourceLocation getKeywordLoc() const { return KeywordLoc; } 4767 4768 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; } 4769 4770 SourceLocation getEndLoc() const LLVM_READONLY { 4771 return getOperand()->getEndLoc(); 4772 } 4773 4774 child_range children() { return child_range(SubExprs, SubExprs + 2); } 4775 4776 const_child_range children() const { 4777 return const_child_range(SubExprs, SubExprs + 2); 4778 } 4779 4780 static bool classof(const Stmt *T) { 4781 return T->getStmtClass() == DependentCoawaitExprClass; 4782 } 4783}; 4784 4785/// Represents a 'co_yield' expression. 4786class CoyieldExpr : public CoroutineSuspendExpr { 4787 friend class ASTStmtReader; 4788 4789public: 4790 CoyieldExpr(SourceLocation CoyieldLoc, Expr *Operand, Expr *Ready, 4791 Expr *Suspend, Expr *Resume, OpaqueValueExpr *OpaqueValue) 4792 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Operand, Ready, 4793 Suspend, Resume, OpaqueValue) {} 4794 CoyieldExpr(SourceLocation CoyieldLoc, QualType Ty, Expr *Operand) 4795 : CoroutineSuspendExpr(CoyieldExprClass, CoyieldLoc, Ty, Operand) {} 4796 CoyieldExpr(EmptyShell Empty) 4797 : CoroutineSuspendExpr(CoyieldExprClass, Empty) {} 4798 4799 Expr *getOperand() const { 4800 // FIXME: Dig out the actual operand or store it. 4801 return getCommonExpr(); 4802 } 4803 4804 static bool classof(const Stmt *T) { 4805 return T->getStmtClass() == CoyieldExprClass; 4806 } 4807}; 4808 4809/// Represents a C++2a __builtin_bit_cast(T, v) expression. Used to implement 4810/// std::bit_cast. These can sometimes be evaluated as part of a constant 4811/// expression, but otherwise CodeGen to a simple memcpy in general. 4812class BuiltinBitCastExpr final 4813 : public ExplicitCastExpr, 4814 private llvm::TrailingObjects<BuiltinBitCastExpr, CXXBaseSpecifier *> { 4815 friend class ASTStmtReader; 4816 friend class CastExpr; 4817 friend class TrailingObjects; 4818 4819 SourceLocation KWLoc; 4820 SourceLocation RParenLoc; 4821 4822public: 4823 BuiltinBitCastExpr(QualType T, ExprValueKind VK, CastKind CK, Expr *SrcExpr, 4824 TypeSourceInfo *DstType, SourceLocation KWLoc, 4825 SourceLocation RParenLoc) 4826 : ExplicitCastExpr(BuiltinBitCastExprClass, T, VK, CK, SrcExpr, 0, 4827 DstType), 4828 KWLoc(KWLoc), RParenLoc(RParenLoc) {} 4829 4830 SourceLocation getBeginLoc() const LLVM_READONLY { return KWLoc; } 4831 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 4832 4833 static bool classof(const Stmt *T) { 4834 return T->getStmtClass() == BuiltinBitCastExprClass; 4835 } 4836}; 4837 4838} // namespace clang 4839 4840#endif // LLVM_CLANG_AST_EXPRCXX_H 4841