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