1 //===- ExprObjC.h - Classes for representing ObjC 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 // This file defines the ExprObjC interface and subclasses. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_EXPROBJC_H 14 #define LLVM_CLANG_AST_EXPROBJC_H 15 16 #include "clang/AST/ComputeDependence.h" 17 #include "clang/AST/Decl.h" 18 #include "clang/AST/DeclObjC.h" 19 #include "clang/AST/DependenceFlags.h" 20 #include "clang/AST/Expr.h" 21 #include "clang/AST/OperationKinds.h" 22 #include "clang/AST/SelectorLocationsKind.h" 23 #include "clang/AST/Stmt.h" 24 #include "clang/AST/Type.h" 25 #include "clang/Basic/IdentifierTable.h" 26 #include "clang/Basic/LLVM.h" 27 #include "clang/Basic/SourceLocation.h" 28 #include "clang/Basic/Specifiers.h" 29 #include "llvm/ADT/ArrayRef.h" 30 #include "llvm/ADT/PointerIntPair.h" 31 #include "llvm/ADT/PointerUnion.h" 32 #include "llvm/ADT/StringRef.h" 33 #include "llvm/ADT/iterator_range.h" 34 #include "llvm/Support/Casting.h" 35 #include "llvm/Support/Compiler.h" 36 #include "llvm/Support/TrailingObjects.h" 37 #include "llvm/Support/VersionTuple.h" 38 #include "llvm/Support/type_traits.h" 39 #include <cassert> 40 #include <cstddef> 41 #include <cstdint> 42 #include <optional> 43 44 namespace clang { 45 46 class ASTContext; 47 class CXXBaseSpecifier; 48 49 /// ObjCStringLiteral, used for Objective-C string literals 50 /// i.e. @"foo". 51 class ObjCStringLiteral : public Expr { 52 Stmt *String; 53 SourceLocation AtLoc; 54 55 public: 56 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 57 : Expr(ObjCStringLiteralClass, T, VK_PRValue, OK_Ordinary), String(SL), 58 AtLoc(L) { 59 setDependence(ExprDependence::None); 60 } 61 explicit ObjCStringLiteral(EmptyShell Empty) 62 : Expr(ObjCStringLiteralClass, Empty) {} 63 64 StringLiteral *getString() { return cast<StringLiteral>(String); } 65 const StringLiteral *getString() const { return cast<StringLiteral>(String); } 66 void setString(StringLiteral *S) { String = S; } 67 68 SourceLocation getAtLoc() const { return AtLoc; } 69 void setAtLoc(SourceLocation L) { AtLoc = L; } 70 71 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; } 72 SourceLocation getEndLoc() const LLVM_READONLY { return String->getEndLoc(); } 73 74 // Iterators 75 child_range children() { return child_range(&String, &String+1); } 76 77 const_child_range children() const { 78 return const_child_range(&String, &String + 1); 79 } 80 81 static bool classof(const Stmt *T) { 82 return T->getStmtClass() == ObjCStringLiteralClass; 83 } 84 }; 85 86 /// ObjCBoolLiteralExpr - Objective-C Boolean Literal. 87 class ObjCBoolLiteralExpr : public Expr { 88 bool Value; 89 SourceLocation Loc; 90 91 public: 92 ObjCBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) 93 : Expr(ObjCBoolLiteralExprClass, Ty, VK_PRValue, OK_Ordinary), Value(val), 94 Loc(l) { 95 setDependence(ExprDependence::None); 96 } 97 explicit ObjCBoolLiteralExpr(EmptyShell Empty) 98 : Expr(ObjCBoolLiteralExprClass, Empty) {} 99 100 bool getValue() const { return Value; } 101 void setValue(bool V) { Value = V; } 102 103 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; } 104 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } 105 106 SourceLocation getLocation() const { return Loc; } 107 void setLocation(SourceLocation L) { Loc = L; } 108 109 // Iterators 110 child_range children() { 111 return child_range(child_iterator(), child_iterator()); 112 } 113 114 const_child_range children() const { 115 return const_child_range(const_child_iterator(), const_child_iterator()); 116 } 117 118 static bool classof(const Stmt *T) { 119 return T->getStmtClass() == ObjCBoolLiteralExprClass; 120 } 121 }; 122 123 /// ObjCBoxedExpr - used for generalized expression boxing. 124 /// as in: @(strdup("hello world")), @(random()) or @(view.frame) 125 /// Also used for boxing non-parenthesized numeric literals; 126 /// as in: @42 or \@true (c++/objc++) or \@__objc_yes (c/objc). 127 class ObjCBoxedExpr : public Expr { 128 Stmt *SubExpr; 129 ObjCMethodDecl *BoxingMethod; 130 SourceRange Range; 131 132 public: 133 friend class ASTStmtReader; 134 135 ObjCBoxedExpr(Expr *E, QualType T, ObjCMethodDecl *method, SourceRange R) 136 : Expr(ObjCBoxedExprClass, T, VK_PRValue, OK_Ordinary), SubExpr(E), 137 BoxingMethod(method), Range(R) { 138 setDependence(computeDependence(this)); 139 } 140 explicit ObjCBoxedExpr(EmptyShell Empty) 141 : Expr(ObjCBoxedExprClass, Empty) {} 142 143 Expr *getSubExpr() { return cast<Expr>(SubExpr); } 144 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); } 145 146 ObjCMethodDecl *getBoxingMethod() const { 147 return BoxingMethod; 148 } 149 150 // Indicates whether this boxed expression can be emitted as a compile-time 151 // constant. 152 bool isExpressibleAsConstantInitializer() const { 153 return !BoxingMethod && SubExpr; 154 } 155 156 SourceLocation getAtLoc() const { return Range.getBegin(); } 157 158 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } 159 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 160 161 SourceRange getSourceRange() const LLVM_READONLY { 162 return Range; 163 } 164 165 // Iterators 166 child_range children() { return child_range(&SubExpr, &SubExpr+1); } 167 168 const_child_range children() const { 169 return const_child_range(&SubExpr, &SubExpr + 1); 170 } 171 172 using const_arg_iterator = ConstExprIterator; 173 174 const_arg_iterator arg_begin() const { 175 return reinterpret_cast<Stmt const * const*>(&SubExpr); 176 } 177 178 const_arg_iterator arg_end() const { 179 return reinterpret_cast<Stmt const * const*>(&SubExpr + 1); 180 } 181 182 static bool classof(const Stmt *T) { 183 return T->getStmtClass() == ObjCBoxedExprClass; 184 } 185 }; 186 187 /// ObjCArrayLiteral - used for objective-c array containers; as in: 188 /// @[@"Hello", NSApp, [NSNumber numberWithInt:42]]; 189 class ObjCArrayLiteral final 190 : public Expr, 191 private llvm::TrailingObjects<ObjCArrayLiteral, Expr *> { 192 unsigned NumElements; 193 SourceRange Range; 194 ObjCMethodDecl *ArrayWithObjectsMethod; 195 196 ObjCArrayLiteral(ArrayRef<Expr *> Elements, 197 QualType T, ObjCMethodDecl * Method, 198 SourceRange SR); 199 200 explicit ObjCArrayLiteral(EmptyShell Empty, unsigned NumElements) 201 : Expr(ObjCArrayLiteralClass, Empty), NumElements(NumElements) {} 202 203 public: 204 friend class ASTStmtReader; 205 friend TrailingObjects; 206 207 static ObjCArrayLiteral *Create(const ASTContext &C, 208 ArrayRef<Expr *> Elements, 209 QualType T, ObjCMethodDecl * Method, 210 SourceRange SR); 211 212 static ObjCArrayLiteral *CreateEmpty(const ASTContext &C, 213 unsigned NumElements); 214 215 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } 216 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 217 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 218 219 /// Retrieve elements of array of literals. 220 Expr **getElements() { return getTrailingObjects<Expr *>(); } 221 222 /// Retrieve elements of array of literals. 223 const Expr * const *getElements() const { 224 return getTrailingObjects<Expr *>(); 225 } 226 227 /// getNumElements - Return number of elements of objective-c array literal. 228 unsigned getNumElements() const { return NumElements; } 229 230 /// getElement - Return the Element at the specified index. 231 Expr *getElement(unsigned Index) { 232 assert((Index < NumElements) && "Arg access out of range!"); 233 return getElements()[Index]; 234 } 235 const Expr *getElement(unsigned Index) const { 236 assert((Index < NumElements) && "Arg access out of range!"); 237 return getElements()[Index]; 238 } 239 240 ObjCMethodDecl *getArrayWithObjectsMethod() const { 241 return ArrayWithObjectsMethod; 242 } 243 244 // Iterators 245 child_range children() { 246 return child_range(reinterpret_cast<Stmt **>(getElements()), 247 reinterpret_cast<Stmt **>(getElements()) + NumElements); 248 } 249 250 const_child_range children() const { 251 auto Children = const_cast<ObjCArrayLiteral *>(this)->children(); 252 return const_child_range(Children.begin(), Children.end()); 253 } 254 255 static bool classof(const Stmt *T) { 256 return T->getStmtClass() == ObjCArrayLiteralClass; 257 } 258 }; 259 260 /// An element in an Objective-C dictionary literal. 261 /// 262 struct ObjCDictionaryElement { 263 /// The key for the dictionary element. 264 Expr *Key; 265 266 /// The value of the dictionary element. 267 Expr *Value; 268 269 /// The location of the ellipsis, if this is a pack expansion. 270 SourceLocation EllipsisLoc; 271 272 /// The number of elements this pack expansion will expand to, if 273 /// this is a pack expansion and is known. 274 std::optional<unsigned> NumExpansions; 275 276 /// Determines whether this dictionary element is a pack expansion. 277 bool isPackExpansion() const { return EllipsisLoc.isValid(); } 278 }; 279 280 } // namespace clang 281 282 namespace clang { 283 284 /// Internal struct for storing Key/value pair. 285 struct ObjCDictionaryLiteral_KeyValuePair { 286 Expr *Key; 287 Expr *Value; 288 }; 289 290 /// Internal struct to describes an element that is a pack 291 /// expansion, used if any of the elements in the dictionary literal 292 /// are pack expansions. 293 struct ObjCDictionaryLiteral_ExpansionData { 294 /// The location of the ellipsis, if this element is a pack 295 /// expansion. 296 SourceLocation EllipsisLoc; 297 298 /// If non-zero, the number of elements that this pack 299 /// expansion will expand to (+1). 300 unsigned NumExpansionsPlusOne; 301 }; 302 303 /// ObjCDictionaryLiteral - AST node to represent objective-c dictionary 304 /// literals; as in: @{@"name" : NSUserName(), @"date" : [NSDate date] }; 305 class ObjCDictionaryLiteral final 306 : public Expr, 307 private llvm::TrailingObjects<ObjCDictionaryLiteral, 308 ObjCDictionaryLiteral_KeyValuePair, 309 ObjCDictionaryLiteral_ExpansionData> { 310 /// The number of elements in this dictionary literal. 311 unsigned NumElements : 31; 312 313 /// Determine whether this dictionary literal has any pack expansions. 314 /// 315 /// If the dictionary literal has pack expansions, then there will 316 /// be an array of pack expansion data following the array of 317 /// key/value pairs, which provide the locations of the ellipses (if 318 /// any) and number of elements in the expansion (if known). If 319 /// there are no pack expansions, we optimize away this storage. 320 LLVM_PREFERRED_TYPE(bool) 321 unsigned HasPackExpansions : 1; 322 323 SourceRange Range; 324 ObjCMethodDecl *DictWithObjectsMethod; 325 326 using KeyValuePair = ObjCDictionaryLiteral_KeyValuePair; 327 using ExpansionData = ObjCDictionaryLiteral_ExpansionData; 328 329 ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK, 330 bool HasPackExpansions, 331 QualType T, ObjCMethodDecl *method, 332 SourceRange SR); 333 334 explicit ObjCDictionaryLiteral(EmptyShell Empty, unsigned NumElements, 335 bool HasPackExpansions) 336 : Expr(ObjCDictionaryLiteralClass, Empty), NumElements(NumElements), 337 HasPackExpansions(HasPackExpansions) {} 338 339 size_t numTrailingObjects(OverloadToken<KeyValuePair>) const { 340 return NumElements; 341 } 342 343 public: 344 friend class ASTStmtReader; 345 friend class ASTStmtWriter; 346 friend TrailingObjects; 347 348 static ObjCDictionaryLiteral *Create(const ASTContext &C, 349 ArrayRef<ObjCDictionaryElement> VK, 350 bool HasPackExpansions, 351 QualType T, ObjCMethodDecl *method, 352 SourceRange SR); 353 354 static ObjCDictionaryLiteral *CreateEmpty(const ASTContext &C, 355 unsigned NumElements, 356 bool HasPackExpansions); 357 358 /// getNumElements - Return number of elements of objective-c dictionary 359 /// literal. 360 unsigned getNumElements() const { return NumElements; } 361 362 ObjCDictionaryElement getKeyValueElement(unsigned Index) const { 363 assert((Index < NumElements) && "Arg access out of range!"); 364 const KeyValuePair &KV = getTrailingObjects<KeyValuePair>()[Index]; 365 ObjCDictionaryElement Result = {KV.Key, KV.Value, SourceLocation(), 366 std::nullopt}; 367 if (HasPackExpansions) { 368 const ExpansionData &Expansion = 369 getTrailingObjects<ExpansionData>()[Index]; 370 Result.EllipsisLoc = Expansion.EllipsisLoc; 371 if (Expansion.NumExpansionsPlusOne > 0) 372 Result.NumExpansions = Expansion.NumExpansionsPlusOne - 1; 373 } 374 return Result; 375 } 376 377 ObjCMethodDecl *getDictWithObjectsMethod() const { 378 return DictWithObjectsMethod; 379 } 380 381 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); } 382 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); } 383 SourceRange getSourceRange() const LLVM_READONLY { return Range; } 384 385 // Iterators 386 child_range children() { 387 // Note: we're taking advantage of the layout of the KeyValuePair struct 388 // here. If that struct changes, this code will need to change as well. 389 static_assert(sizeof(KeyValuePair) == sizeof(Stmt *) * 2, 390 "KeyValuePair is expected size"); 391 return child_range( 392 reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()), 393 reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()) + 394 NumElements * 2); 395 } 396 397 const_child_range children() const { 398 auto Children = const_cast<ObjCDictionaryLiteral *>(this)->children(); 399 return const_child_range(Children.begin(), Children.end()); 400 } 401 402 static bool classof(const Stmt *T) { 403 return T->getStmtClass() == ObjCDictionaryLiteralClass; 404 } 405 }; 406 407 /// ObjCEncodeExpr, used for \@encode in Objective-C. \@encode has the same 408 /// type and behavior as StringLiteral except that the string initializer is 409 /// obtained from ASTContext with the encoding type as an argument. 410 class ObjCEncodeExpr : public Expr { 411 TypeSourceInfo *EncodedType; 412 SourceLocation AtLoc, RParenLoc; 413 414 public: 415 ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType, SourceLocation at, 416 SourceLocation rp) 417 : Expr(ObjCEncodeExprClass, T, VK_LValue, OK_Ordinary), 418 EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) { 419 setDependence(computeDependence(this)); 420 } 421 422 explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){} 423 424 SourceLocation getAtLoc() const { return AtLoc; } 425 void setAtLoc(SourceLocation L) { AtLoc = L; } 426 SourceLocation getRParenLoc() const { return RParenLoc; } 427 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 428 429 QualType getEncodedType() const { return EncodedType->getType(); } 430 431 TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; } 432 433 void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) { 434 EncodedType = EncType; 435 } 436 437 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; } 438 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 439 440 // Iterators 441 child_range children() { 442 return child_range(child_iterator(), child_iterator()); 443 } 444 445 const_child_range children() const { 446 return const_child_range(const_child_iterator(), const_child_iterator()); 447 } 448 449 static bool classof(const Stmt *T) { 450 return T->getStmtClass() == ObjCEncodeExprClass; 451 } 452 }; 453 454 /// ObjCSelectorExpr used for \@selector in Objective-C. 455 class ObjCSelectorExpr : public Expr { 456 Selector SelName; 457 SourceLocation AtLoc, RParenLoc; 458 459 public: 460 ObjCSelectorExpr(QualType T, Selector selInfo, SourceLocation at, 461 SourceLocation rp) 462 : Expr(ObjCSelectorExprClass, T, VK_PRValue, OK_Ordinary), 463 SelName(selInfo), AtLoc(at), RParenLoc(rp) { 464 setDependence(ExprDependence::None); 465 } 466 explicit ObjCSelectorExpr(EmptyShell Empty) 467 : Expr(ObjCSelectorExprClass, Empty) {} 468 469 Selector getSelector() const { return SelName; } 470 void setSelector(Selector S) { SelName = S; } 471 472 SourceLocation getAtLoc() const { return AtLoc; } 473 SourceLocation getRParenLoc() const { return RParenLoc; } 474 void setAtLoc(SourceLocation L) { AtLoc = L; } 475 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 476 477 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; } 478 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 479 480 /// getNumArgs - Return the number of actual arguments to this call. 481 unsigned getNumArgs() const { return SelName.getNumArgs(); } 482 483 // Iterators 484 child_range children() { 485 return child_range(child_iterator(), child_iterator()); 486 } 487 488 const_child_range children() const { 489 return const_child_range(const_child_iterator(), const_child_iterator()); 490 } 491 492 static bool classof(const Stmt *T) { 493 return T->getStmtClass() == ObjCSelectorExprClass; 494 } 495 }; 496 497 /// ObjCProtocolExpr used for protocol expression in Objective-C. 498 /// 499 /// This is used as: \@protocol(foo), as in: 500 /// \code 501 /// [obj conformsToProtocol:@protocol(foo)] 502 /// \endcode 503 /// 504 /// The return type is "Protocol*". 505 class ObjCProtocolExpr : public Expr { 506 ObjCProtocolDecl *TheProtocol; 507 SourceLocation AtLoc, ProtoLoc, RParenLoc; 508 509 public: 510 friend class ASTStmtReader; 511 friend class ASTStmtWriter; 512 513 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol, SourceLocation at, 514 SourceLocation protoLoc, SourceLocation rp) 515 : Expr(ObjCProtocolExprClass, T, VK_PRValue, OK_Ordinary), 516 TheProtocol(protocol), AtLoc(at), ProtoLoc(protoLoc), RParenLoc(rp) { 517 setDependence(ExprDependence::None); 518 } 519 explicit ObjCProtocolExpr(EmptyShell Empty) 520 : Expr(ObjCProtocolExprClass, Empty) {} 521 522 ObjCProtocolDecl *getProtocol() const { return TheProtocol; } 523 void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; } 524 525 SourceLocation getProtocolIdLoc() const { return ProtoLoc; } 526 SourceLocation getAtLoc() const { return AtLoc; } 527 SourceLocation getRParenLoc() const { return RParenLoc; } 528 void setAtLoc(SourceLocation L) { AtLoc = L; } 529 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 530 531 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; } 532 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; } 533 534 // Iterators 535 child_range children() { 536 return child_range(child_iterator(), child_iterator()); 537 } 538 539 const_child_range children() const { 540 return const_child_range(const_child_iterator(), const_child_iterator()); 541 } 542 543 static bool classof(const Stmt *T) { 544 return T->getStmtClass() == ObjCProtocolExprClass; 545 } 546 }; 547 548 /// ObjCIvarRefExpr - A reference to an ObjC instance variable. 549 class ObjCIvarRefExpr : public Expr { 550 ObjCIvarDecl *D; 551 Stmt *Base; 552 SourceLocation Loc; 553 554 /// OpLoc - This is the location of '.' or '->' 555 SourceLocation OpLoc; 556 557 // True if this is "X->F", false if this is "X.F". 558 LLVM_PREFERRED_TYPE(bool) 559 bool IsArrow : 1; 560 561 // True if ivar reference has no base (self assumed). 562 LLVM_PREFERRED_TYPE(bool) 563 bool IsFreeIvar : 1; 564 565 public: 566 ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, 567 SourceLocation oploc, Expr *base, bool arrow = false, 568 bool freeIvar = false) 569 : Expr(ObjCIvarRefExprClass, t, VK_LValue, 570 d->isBitField() ? OK_BitField : OK_Ordinary), 571 D(d), Base(base), Loc(l), OpLoc(oploc), IsArrow(arrow), 572 IsFreeIvar(freeIvar) { 573 setDependence(computeDependence(this)); 574 } 575 576 explicit ObjCIvarRefExpr(EmptyShell Empty) 577 : Expr(ObjCIvarRefExprClass, Empty) {} 578 579 ObjCIvarDecl *getDecl() { return D; } 580 const ObjCIvarDecl *getDecl() const { return D; } 581 void setDecl(ObjCIvarDecl *d) { D = d; } 582 583 const Expr *getBase() const { return cast<Expr>(Base); } 584 Expr *getBase() { return cast<Expr>(Base); } 585 void setBase(Expr * base) { Base = base; } 586 587 bool isArrow() const { return IsArrow; } 588 bool isFreeIvar() const { return IsFreeIvar; } 589 void setIsArrow(bool A) { IsArrow = A; } 590 void setIsFreeIvar(bool A) { IsFreeIvar = A; } 591 592 SourceLocation getLocation() const { return Loc; } 593 void setLocation(SourceLocation L) { Loc = L; } 594 595 SourceLocation getBeginLoc() const LLVM_READONLY { 596 return isFreeIvar() ? Loc : getBase()->getBeginLoc(); 597 } 598 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; } 599 600 SourceLocation getOpLoc() const { return OpLoc; } 601 void setOpLoc(SourceLocation L) { OpLoc = L; } 602 603 // Iterators 604 child_range children() { return child_range(&Base, &Base+1); } 605 606 const_child_range children() const { 607 return const_child_range(&Base, &Base + 1); 608 } 609 610 static bool classof(const Stmt *T) { 611 return T->getStmtClass() == ObjCIvarRefExprClass; 612 } 613 }; 614 615 /// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 616 /// property. 617 class ObjCPropertyRefExpr : public Expr { 618 private: 619 /// If the bool is true, this is an implicit property reference; the 620 /// pointer is an (optional) ObjCMethodDecl and Setter may be set. 621 /// if the bool is false, this is an explicit property reference; 622 /// the pointer is an ObjCPropertyDecl and Setter is always null. 623 llvm::PointerIntPair<NamedDecl *, 1, bool> PropertyOrGetter; 624 625 /// Indicates whether the property reference will result in a message 626 /// to the getter, the setter, or both. 627 /// This applies to both implicit and explicit property references. 628 enum MethodRefFlags { 629 MethodRef_None = 0, 630 MethodRef_Getter = 0x1, 631 MethodRef_Setter = 0x2 632 }; 633 634 /// Contains the Setter method pointer and MethodRefFlags bit flags. 635 llvm::PointerIntPair<ObjCMethodDecl *, 2, unsigned> SetterAndMethodRefFlags; 636 637 // FIXME: Maybe we should store the property identifier here, 638 // because it's not rederivable from the other data when there's an 639 // implicit property with no getter (because the 'foo' -> 'setFoo:' 640 // transformation is lossy on the first character). 641 642 SourceLocation IdLoc; 643 644 /// When the receiver in property access is 'super', this is 645 /// the location of the 'super' keyword. When it's an interface, 646 /// this is that interface. 647 SourceLocation ReceiverLoc; 648 llvm::PointerUnion<Stmt *, const Type *, ObjCInterfaceDecl *> Receiver; 649 650 public: 651 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, ExprValueKind VK, 652 ExprObjectKind OK, SourceLocation l, Expr *base) 653 : Expr(ObjCPropertyRefExprClass, t, VK, OK), PropertyOrGetter(PD, false), 654 IdLoc(l), Receiver(base) { 655 assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject)); 656 setDependence(computeDependence(this)); 657 } 658 659 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, ExprValueKind VK, 660 ExprObjectKind OK, SourceLocation l, SourceLocation sl, 661 QualType st) 662 : Expr(ObjCPropertyRefExprClass, t, VK, OK), PropertyOrGetter(PD, false), 663 IdLoc(l), ReceiverLoc(sl), Receiver(st.getTypePtr()) { 664 assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject)); 665 setDependence(computeDependence(this)); 666 } 667 668 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 669 QualType T, ExprValueKind VK, ExprObjectKind OK, 670 SourceLocation IdLoc, Expr *Base) 671 : Expr(ObjCPropertyRefExprClass, T, VK, OK), 672 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0), 673 IdLoc(IdLoc), Receiver(Base) { 674 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject)); 675 setDependence(computeDependence(this)); 676 } 677 678 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 679 QualType T, ExprValueKind VK, ExprObjectKind OK, 680 SourceLocation IdLoc, SourceLocation SuperLoc, 681 QualType SuperTy) 682 : Expr(ObjCPropertyRefExprClass, T, VK, OK), 683 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0), 684 IdLoc(IdLoc), ReceiverLoc(SuperLoc), Receiver(SuperTy.getTypePtr()) { 685 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject)); 686 setDependence(computeDependence(this)); 687 } 688 689 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 690 QualType T, ExprValueKind VK, ExprObjectKind OK, 691 SourceLocation IdLoc, SourceLocation ReceiverLoc, 692 ObjCInterfaceDecl *Receiver) 693 : Expr(ObjCPropertyRefExprClass, T, VK, OK), 694 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0), 695 IdLoc(IdLoc), ReceiverLoc(ReceiverLoc), Receiver(Receiver) { 696 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject)); 697 setDependence(computeDependence(this)); 698 } 699 700 explicit ObjCPropertyRefExpr(EmptyShell Empty) 701 : Expr(ObjCPropertyRefExprClass, Empty) {} 702 703 bool isImplicitProperty() const { return PropertyOrGetter.getInt(); } 704 bool isExplicitProperty() const { return !PropertyOrGetter.getInt(); } 705 706 ObjCPropertyDecl *getExplicitProperty() const { 707 assert(!isImplicitProperty()); 708 return cast<ObjCPropertyDecl>(PropertyOrGetter.getPointer()); 709 } 710 711 ObjCMethodDecl *getImplicitPropertyGetter() const { 712 assert(isImplicitProperty()); 713 return cast_or_null<ObjCMethodDecl>(PropertyOrGetter.getPointer()); 714 } 715 716 ObjCMethodDecl *getImplicitPropertySetter() const { 717 assert(isImplicitProperty()); 718 return SetterAndMethodRefFlags.getPointer(); 719 } 720 721 Selector getGetterSelector() const { 722 if (isImplicitProperty()) 723 return getImplicitPropertyGetter()->getSelector(); 724 return getExplicitProperty()->getGetterName(); 725 } 726 727 Selector getSetterSelector() const { 728 if (isImplicitProperty()) 729 return getImplicitPropertySetter()->getSelector(); 730 return getExplicitProperty()->getSetterName(); 731 } 732 733 /// True if the property reference will result in a message to the 734 /// getter. 735 /// This applies to both implicit and explicit property references. 736 bool isMessagingGetter() const { 737 return SetterAndMethodRefFlags.getInt() & MethodRef_Getter; 738 } 739 740 /// True if the property reference will result in a message to the 741 /// setter. 742 /// This applies to both implicit and explicit property references. 743 bool isMessagingSetter() const { 744 return SetterAndMethodRefFlags.getInt() & MethodRef_Setter; 745 } 746 747 void setIsMessagingGetter(bool val = true) { 748 setMethodRefFlag(MethodRef_Getter, val); 749 } 750 751 void setIsMessagingSetter(bool val = true) { 752 setMethodRefFlag(MethodRef_Setter, val); 753 } 754 755 const Expr *getBase() const { return cast<Expr>(cast<Stmt *>(Receiver)); } 756 Expr *getBase() { return cast<Expr>(cast<Stmt *>(Receiver)); } 757 758 SourceLocation getLocation() const { return IdLoc; } 759 760 SourceLocation getReceiverLocation() const { return ReceiverLoc; } 761 762 QualType getSuperReceiverType() const { 763 return QualType(cast<const Type *>(Receiver), 0); 764 } 765 766 ObjCInterfaceDecl *getClassReceiver() const { 767 return cast<ObjCInterfaceDecl *>(Receiver); 768 } 769 770 bool isObjectReceiver() const { return isa<Stmt *>(Receiver); } 771 bool isSuperReceiver() const { return isa<const Type *>(Receiver); } 772 bool isClassReceiver() const { return isa<ObjCInterfaceDecl *>(Receiver); } 773 774 /// Determine the type of the base, regardless of the kind of receiver. 775 QualType getReceiverType(const ASTContext &ctx) const; 776 777 SourceLocation getBeginLoc() const LLVM_READONLY { 778 return isObjectReceiver() ? getBase()->getBeginLoc() 779 : getReceiverLocation(); 780 } 781 782 SourceLocation getEndLoc() const LLVM_READONLY { return IdLoc; } 783 784 // Iterators 785 child_range children() { 786 if (isa<Stmt *>(Receiver)) { 787 Stmt **begin = reinterpret_cast<Stmt**>(&Receiver); // hack! 788 return child_range(begin, begin+1); 789 } 790 return child_range(child_iterator(), child_iterator()); 791 } 792 793 const_child_range children() const { 794 auto Children = const_cast<ObjCPropertyRefExpr *>(this)->children(); 795 return const_child_range(Children.begin(), Children.end()); 796 } 797 798 static bool classof(const Stmt *T) { 799 return T->getStmtClass() == ObjCPropertyRefExprClass; 800 } 801 802 private: 803 friend class ASTStmtReader; 804 friend class ASTStmtWriter; 805 806 void setExplicitProperty(ObjCPropertyDecl *D, unsigned methRefFlags) { 807 PropertyOrGetter.setPointer(D); 808 PropertyOrGetter.setInt(false); 809 SetterAndMethodRefFlags.setPointer(nullptr); 810 SetterAndMethodRefFlags.setInt(methRefFlags); 811 } 812 813 void setImplicitProperty(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 814 unsigned methRefFlags) { 815 PropertyOrGetter.setPointer(Getter); 816 PropertyOrGetter.setInt(true); 817 SetterAndMethodRefFlags.setPointer(Setter); 818 SetterAndMethodRefFlags.setInt(methRefFlags); 819 } 820 821 void setBase(Expr *Base) { Receiver = Base; } 822 void setSuperReceiver(QualType T) { Receiver = T.getTypePtr(); } 823 void setClassReceiver(ObjCInterfaceDecl *D) { Receiver = D; } 824 825 void setLocation(SourceLocation L) { IdLoc = L; } 826 void setReceiverLocation(SourceLocation Loc) { ReceiverLoc = Loc; } 827 828 void setMethodRefFlag(MethodRefFlags flag, bool val) { 829 unsigned f = SetterAndMethodRefFlags.getInt(); 830 if (val) 831 f |= flag; 832 else 833 f &= ~flag; 834 SetterAndMethodRefFlags.setInt(f); 835 } 836 }; 837 838 /// ObjCSubscriptRefExpr - used for array and dictionary subscripting. 839 /// array[4] = array[3]; dictionary[key] = dictionary[alt_key]; 840 class ObjCSubscriptRefExpr : public Expr { 841 // Location of ']' in an indexing expression. 842 SourceLocation RBracket; 843 844 // array/dictionary base expression. 845 // for arrays, this is a numeric expression. For dictionaries, this is 846 // an objective-c object pointer expression. 847 enum { BASE, KEY, END_EXPR }; 848 Stmt* SubExprs[END_EXPR]; 849 850 ObjCMethodDecl *GetAtIndexMethodDecl; 851 852 // For immutable objects this is null. When ObjCSubscriptRefExpr is to read 853 // an indexed object this is null too. 854 ObjCMethodDecl *SetAtIndexMethodDecl; 855 856 public: 857 ObjCSubscriptRefExpr(Expr *base, Expr *key, QualType T, ExprValueKind VK, 858 ExprObjectKind OK, ObjCMethodDecl *getMethod, 859 ObjCMethodDecl *setMethod, SourceLocation RB) 860 : Expr(ObjCSubscriptRefExprClass, T, VK, OK), RBracket(RB), 861 GetAtIndexMethodDecl(getMethod), SetAtIndexMethodDecl(setMethod) { 862 SubExprs[BASE] = base; 863 SubExprs[KEY] = key; 864 setDependence(computeDependence(this)); 865 } 866 867 explicit ObjCSubscriptRefExpr(EmptyShell Empty) 868 : Expr(ObjCSubscriptRefExprClass, Empty) {} 869 870 SourceLocation getRBracket() const { return RBracket; } 871 void setRBracket(SourceLocation RB) { RBracket = RB; } 872 873 SourceLocation getBeginLoc() const LLVM_READONLY { 874 return SubExprs[BASE]->getBeginLoc(); 875 } 876 877 SourceLocation getEndLoc() const LLVM_READONLY { return RBracket; } 878 879 Expr *getBaseExpr() const { return cast<Expr>(SubExprs[BASE]); } 880 void setBaseExpr(Stmt *S) { SubExprs[BASE] = S; } 881 882 Expr *getKeyExpr() const { return cast<Expr>(SubExprs[KEY]); } 883 void setKeyExpr(Stmt *S) { SubExprs[KEY] = S; } 884 885 ObjCMethodDecl *getAtIndexMethodDecl() const { 886 return GetAtIndexMethodDecl; 887 } 888 889 ObjCMethodDecl *setAtIndexMethodDecl() const { 890 return SetAtIndexMethodDecl; 891 } 892 893 bool isArraySubscriptRefExpr() const { 894 return getKeyExpr()->getType()->isIntegralOrEnumerationType(); 895 } 896 897 child_range children() { 898 return child_range(SubExprs, SubExprs+END_EXPR); 899 } 900 901 const_child_range children() const { 902 return const_child_range(SubExprs, SubExprs + END_EXPR); 903 } 904 905 static bool classof(const Stmt *T) { 906 return T->getStmtClass() == ObjCSubscriptRefExprClass; 907 } 908 909 private: 910 friend class ASTStmtReader; 911 }; 912 913 /// An expression that sends a message to the given Objective-C 914 /// object or class. 915 /// 916 /// The following contains two message send expressions: 917 /// 918 /// \code 919 /// [[NSString alloc] initWithString:@"Hello"] 920 /// \endcode 921 /// 922 /// The innermost message send invokes the "alloc" class method on the 923 /// NSString class, while the outermost message send invokes the 924 /// "initWithString" instance method on the object returned from 925 /// NSString's "alloc". In all, an Objective-C message send can take 926 /// on four different (although related) forms: 927 /// 928 /// 1. Send to an object instance. 929 /// 2. Send to a class. 930 /// 3. Send to the superclass instance of the current class. 931 /// 4. Send to the superclass of the current class. 932 /// 933 /// All four kinds of message sends are modeled by the ObjCMessageExpr 934 /// class, and can be distinguished via \c getReceiverKind(). Example: 935 /// 936 /// The "void *" trailing objects are actually ONE void * (the 937 /// receiver pointer), and NumArgs Expr *. But due to the 938 /// implementation of children(), these must be together contiguously. 939 class ObjCMessageExpr final 940 : public Expr, 941 private llvm::TrailingObjects<ObjCMessageExpr, void *, SourceLocation> { 942 public: 943 /// The kind of receiver this message is sending to. 944 enum ReceiverKind { 945 /// The receiver is a class. 946 Class = 0, 947 948 /// The receiver is an object instance. 949 Instance, 950 951 /// The receiver is a superclass. 952 SuperClass, 953 954 /// The receiver is the instance of the superclass object. 955 SuperInstance 956 }; 957 958 private: 959 /// Stores either the selector that this message is sending 960 /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer 961 /// referring to the method that we type-checked against. 962 uintptr_t SelectorOrMethod = 0; 963 964 enum { NumArgsBitWidth = 16 }; 965 966 /// The number of arguments in the message send, not 967 /// including the receiver. 968 unsigned NumArgs : NumArgsBitWidth; 969 970 /// The kind of message send this is, which is one of the 971 /// ReceiverKind values. 972 /// 973 /// We pad this out to a byte to avoid excessive masking and shifting. 974 LLVM_PREFERRED_TYPE(ReceiverKind) 975 unsigned Kind : 8; 976 977 /// Whether we have an actual method prototype in \c 978 /// SelectorOrMethod. 979 /// 980 /// When non-zero, we have a method declaration; otherwise, we just 981 /// have a selector. 982 LLVM_PREFERRED_TYPE(bool) 983 unsigned HasMethod : 1; 984 985 /// Whether this message send is a "delegate init call", 986 /// i.e. a call of an init method on self from within an init method. 987 LLVM_PREFERRED_TYPE(bool) 988 unsigned IsDelegateInitCall : 1; 989 990 /// Whether this message send was implicitly generated by 991 /// the implementation rather than explicitly written by the user. 992 LLVM_PREFERRED_TYPE(bool) 993 unsigned IsImplicit : 1; 994 995 /// Whether the locations of the selector identifiers are in a 996 /// "standard" position, a enum SelectorLocationsKind. 997 LLVM_PREFERRED_TYPE(SelectorLocationsKind) 998 unsigned SelLocsKind : 2; 999 1000 /// When the message expression is a send to 'super', this is 1001 /// the location of the 'super' keyword. 1002 SourceLocation SuperLoc; 1003 1004 /// The source locations of the open and close square 1005 /// brackets ('[' and ']', respectively). 1006 SourceLocation LBracLoc, RBracLoc; 1007 1008 ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs) 1009 : Expr(ObjCMessageExprClass, Empty), Kind(0), HasMethod(false), 1010 IsDelegateInitCall(false), IsImplicit(false), SelLocsKind(0) { 1011 setNumArgs(NumArgs); 1012 } 1013 1014 ObjCMessageExpr(QualType T, ExprValueKind VK, 1015 SourceLocation LBracLoc, 1016 SourceLocation SuperLoc, 1017 bool IsInstanceSuper, 1018 QualType SuperType, 1019 Selector Sel, 1020 ArrayRef<SourceLocation> SelLocs, 1021 SelectorLocationsKind SelLocsK, 1022 ObjCMethodDecl *Method, 1023 ArrayRef<Expr *> Args, 1024 SourceLocation RBracLoc, 1025 bool isImplicit); 1026 ObjCMessageExpr(QualType T, ExprValueKind VK, 1027 SourceLocation LBracLoc, 1028 TypeSourceInfo *Receiver, 1029 Selector Sel, 1030 ArrayRef<SourceLocation> SelLocs, 1031 SelectorLocationsKind SelLocsK, 1032 ObjCMethodDecl *Method, 1033 ArrayRef<Expr *> Args, 1034 SourceLocation RBracLoc, 1035 bool isImplicit); 1036 ObjCMessageExpr(QualType T, ExprValueKind VK, 1037 SourceLocation LBracLoc, 1038 Expr *Receiver, 1039 Selector Sel, 1040 ArrayRef<SourceLocation> SelLocs, 1041 SelectorLocationsKind SelLocsK, 1042 ObjCMethodDecl *Method, 1043 ArrayRef<Expr *> Args, 1044 SourceLocation RBracLoc, 1045 bool isImplicit); 1046 1047 size_t numTrailingObjects(OverloadToken<void *>) const { return NumArgs + 1; } 1048 1049 void setNumArgs(unsigned Num) { 1050 assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!"); 1051 NumArgs = Num; 1052 } 1053 1054 void initArgsAndSelLocs(ArrayRef<Expr *> Args, 1055 ArrayRef<SourceLocation> SelLocs, 1056 SelectorLocationsKind SelLocsK); 1057 1058 /// Retrieve the pointer value of the message receiver. 1059 void *getReceiverPointer() const { return *getTrailingObjects<void *>(); } 1060 1061 /// Set the pointer value of the message receiver. 1062 void setReceiverPointer(void *Value) { 1063 *getTrailingObjects<void *>() = Value; 1064 } 1065 1066 SelectorLocationsKind getSelLocsKind() const { 1067 return (SelectorLocationsKind)SelLocsKind; 1068 } 1069 1070 bool hasStandardSelLocs() const { 1071 return getSelLocsKind() != SelLoc_NonStandard; 1072 } 1073 1074 /// Get a pointer to the stored selector identifiers locations array. 1075 /// No locations will be stored if HasStandardSelLocs is true. 1076 SourceLocation *getStoredSelLocs() { 1077 return getTrailingObjects<SourceLocation>(); 1078 } 1079 const SourceLocation *getStoredSelLocs() const { 1080 return getTrailingObjects<SourceLocation>(); 1081 } 1082 1083 /// Get the number of stored selector identifiers locations. 1084 /// No locations will be stored if HasStandardSelLocs is true. 1085 unsigned getNumStoredSelLocs() const { 1086 if (hasStandardSelLocs()) 1087 return 0; 1088 return getNumSelectorLocs(); 1089 } 1090 1091 static ObjCMessageExpr *alloc(const ASTContext &C, 1092 ArrayRef<Expr *> Args, 1093 SourceLocation RBraceLoc, 1094 ArrayRef<SourceLocation> SelLocs, 1095 Selector Sel, 1096 SelectorLocationsKind &SelLocsK); 1097 static ObjCMessageExpr *alloc(const ASTContext &C, 1098 unsigned NumArgs, 1099 unsigned NumStoredSelLocs); 1100 1101 public: 1102 friend class ASTStmtReader; 1103 friend class ASTStmtWriter; 1104 friend TrailingObjects; 1105 1106 /// Create a message send to super. 1107 /// 1108 /// \param Context The ASTContext in which this expression will be created. 1109 /// 1110 /// \param T The result type of this message. 1111 /// 1112 /// \param VK The value kind of this message. A message returning 1113 /// a l-value or r-value reference will be an l-value or x-value, 1114 /// respectively. 1115 /// 1116 /// \param LBracLoc The location of the open square bracket '['. 1117 /// 1118 /// \param SuperLoc The location of the "super" keyword. 1119 /// 1120 /// \param IsInstanceSuper Whether this is an instance "super" 1121 /// message (otherwise, it's a class "super" message). 1122 /// 1123 /// \param Sel The selector used to determine which method gets called. 1124 /// 1125 /// \param Method The Objective-C method against which this message 1126 /// send was type-checked. May be nullptr. 1127 /// 1128 /// \param Args The message send arguments. 1129 /// 1130 /// \param RBracLoc The location of the closing square bracket ']'. 1131 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T, 1132 ExprValueKind VK, 1133 SourceLocation LBracLoc, 1134 SourceLocation SuperLoc, 1135 bool IsInstanceSuper, 1136 QualType SuperType, 1137 Selector Sel, 1138 ArrayRef<SourceLocation> SelLocs, 1139 ObjCMethodDecl *Method, 1140 ArrayRef<Expr *> Args, 1141 SourceLocation RBracLoc, 1142 bool isImplicit); 1143 1144 /// Create a class message send. 1145 /// 1146 /// \param Context The ASTContext in which this expression will be created. 1147 /// 1148 /// \param T The result type of this message. 1149 /// 1150 /// \param VK The value kind of this message. A message returning 1151 /// a l-value or r-value reference will be an l-value or x-value, 1152 /// respectively. 1153 /// 1154 /// \param LBracLoc The location of the open square bracket '['. 1155 /// 1156 /// \param Receiver The type of the receiver, including 1157 /// source-location information. 1158 /// 1159 /// \param Sel The selector used to determine which method gets called. 1160 /// 1161 /// \param Method The Objective-C method against which this message 1162 /// send was type-checked. May be nullptr. 1163 /// 1164 /// \param Args The message send arguments. 1165 /// 1166 /// \param RBracLoc The location of the closing square bracket ']'. 1167 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T, 1168 ExprValueKind VK, 1169 SourceLocation LBracLoc, 1170 TypeSourceInfo *Receiver, 1171 Selector Sel, 1172 ArrayRef<SourceLocation> SelLocs, 1173 ObjCMethodDecl *Method, 1174 ArrayRef<Expr *> Args, 1175 SourceLocation RBracLoc, 1176 bool isImplicit); 1177 1178 /// Create an instance message send. 1179 /// 1180 /// \param Context The ASTContext in which this expression will be created. 1181 /// 1182 /// \param T The result type of this message. 1183 /// 1184 /// \param VK The value kind of this message. A message returning 1185 /// a l-value or r-value reference will be an l-value or x-value, 1186 /// respectively. 1187 /// 1188 /// \param LBracLoc The location of the open square bracket '['. 1189 /// 1190 /// \param Receiver The expression used to produce the object that 1191 /// will receive this message. 1192 /// 1193 /// \param Sel The selector used to determine which method gets called. 1194 /// 1195 /// \param Method The Objective-C method against which this message 1196 /// send was type-checked. May be nullptr. 1197 /// 1198 /// \param Args The message send arguments. 1199 /// 1200 /// \param RBracLoc The location of the closing square bracket ']'. 1201 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T, 1202 ExprValueKind VK, 1203 SourceLocation LBracLoc, 1204 Expr *Receiver, 1205 Selector Sel, 1206 ArrayRef<SourceLocation> SeLocs, 1207 ObjCMethodDecl *Method, 1208 ArrayRef<Expr *> Args, 1209 SourceLocation RBracLoc, 1210 bool isImplicit); 1211 1212 /// Create an empty Objective-C message expression, to be 1213 /// filled in by subsequent calls. 1214 /// 1215 /// \param Context The context in which the message send will be created. 1216 /// 1217 /// \param NumArgs The number of message arguments, not including 1218 /// the receiver. 1219 static ObjCMessageExpr *CreateEmpty(const ASTContext &Context, 1220 unsigned NumArgs, 1221 unsigned NumStoredSelLocs); 1222 1223 /// Indicates whether the message send was implicitly 1224 /// generated by the implementation. If false, it was written explicitly 1225 /// in the source code. 1226 bool isImplicit() const { return IsImplicit; } 1227 1228 /// Determine the kind of receiver that this message is being 1229 /// sent to. 1230 ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; } 1231 1232 /// \return the return type of the message being sent. 1233 /// This is not always the type of the message expression itself because 1234 /// of references (the expression would not have a reference type). 1235 /// It is also not always the declared return type of the method because 1236 /// of `instancetype` (in that case it's an expression type). 1237 QualType getCallReturnType(ASTContext &Ctx) const; 1238 1239 /// Source range of the receiver. 1240 SourceRange getReceiverRange() const; 1241 1242 /// Determine whether this is an instance message to either a 1243 /// computed object or to super. 1244 bool isInstanceMessage() const { 1245 return getReceiverKind() == Instance || getReceiverKind() == SuperInstance; 1246 } 1247 1248 /// Determine whether this is an class message to either a 1249 /// specified class or to super. 1250 bool isClassMessage() const { 1251 return getReceiverKind() == Class || getReceiverKind() == SuperClass; 1252 } 1253 1254 /// Returns the object expression (receiver) for an instance message, 1255 /// or null for a message that is not an instance message. 1256 Expr *getInstanceReceiver() { 1257 if (getReceiverKind() == Instance) 1258 return static_cast<Expr *>(getReceiverPointer()); 1259 1260 return nullptr; 1261 } 1262 const Expr *getInstanceReceiver() const { 1263 return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver(); 1264 } 1265 1266 /// Turn this message send into an instance message that 1267 /// computes the receiver object with the given expression. 1268 void setInstanceReceiver(Expr *rec) { 1269 Kind = Instance; 1270 setReceiverPointer(rec); 1271 } 1272 1273 /// Returns the type of a class message send, or NULL if the 1274 /// message is not a class message. 1275 QualType getClassReceiver() const { 1276 if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo()) 1277 return TSInfo->getType(); 1278 1279 return {}; 1280 } 1281 1282 /// Returns a type-source information of a class message 1283 /// send, or nullptr if the message is not a class message. 1284 TypeSourceInfo *getClassReceiverTypeInfo() const { 1285 if (getReceiverKind() == Class) 1286 return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer()); 1287 return nullptr; 1288 } 1289 1290 void setClassReceiver(TypeSourceInfo *TSInfo) { 1291 Kind = Class; 1292 setReceiverPointer(TSInfo); 1293 } 1294 1295 /// Retrieve the location of the 'super' keyword for a class 1296 /// or instance message to 'super', otherwise an invalid source location. 1297 SourceLocation getSuperLoc() const { 1298 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 1299 return SuperLoc; 1300 1301 return SourceLocation(); 1302 } 1303 1304 /// Retrieve the receiver type to which this message is being directed. 1305 /// 1306 /// This routine cross-cuts all of the different kinds of message 1307 /// sends to determine what the underlying (statically known) type 1308 /// of the receiver will be; use \c getReceiverKind() to determine 1309 /// whether the message is a class or an instance method, whether it 1310 /// is a send to super or not, etc. 1311 /// 1312 /// \returns The type of the receiver. 1313 QualType getReceiverType() const; 1314 1315 /// Retrieve the Objective-C interface to which this message 1316 /// is being directed, if known. 1317 /// 1318 /// This routine cross-cuts all of the different kinds of message 1319 /// sends to determine what the underlying (statically known) type 1320 /// of the receiver will be; use \c getReceiverKind() to determine 1321 /// whether the message is a class or an instance method, whether it 1322 /// is a send to super or not, etc. 1323 /// 1324 /// \returns The Objective-C interface if known, otherwise nullptr. 1325 ObjCInterfaceDecl *getReceiverInterface() const; 1326 1327 /// Retrieve the type referred to by 'super'. 1328 /// 1329 /// The returned type will either be an ObjCInterfaceType (for an 1330 /// class message to super) or an ObjCObjectPointerType that refers 1331 /// to a class (for an instance message to super); 1332 QualType getSuperType() const { 1333 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 1334 return QualType::getFromOpaquePtr(getReceiverPointer()); 1335 1336 return QualType(); 1337 } 1338 1339 void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) { 1340 Kind = IsInstanceSuper? SuperInstance : SuperClass; 1341 SuperLoc = Loc; 1342 setReceiverPointer(T.getAsOpaquePtr()); 1343 } 1344 1345 Selector getSelector() const; 1346 1347 void setSelector(Selector S) { 1348 HasMethod = false; 1349 SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr()); 1350 } 1351 1352 const ObjCMethodDecl *getMethodDecl() const { 1353 if (HasMethod) 1354 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod); 1355 1356 return nullptr; 1357 } 1358 1359 ObjCMethodDecl *getMethodDecl() { 1360 if (HasMethod) 1361 return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod); 1362 1363 return nullptr; 1364 } 1365 1366 void setMethodDecl(ObjCMethodDecl *MD) { 1367 HasMethod = true; 1368 SelectorOrMethod = reinterpret_cast<uintptr_t>(MD); 1369 } 1370 1371 ObjCMethodFamily getMethodFamily() const { 1372 if (HasMethod) return getMethodDecl()->getMethodFamily(); 1373 return getSelector().getMethodFamily(); 1374 } 1375 1376 /// Return the number of actual arguments in this message, 1377 /// not counting the receiver. 1378 unsigned getNumArgs() const { return NumArgs; } 1379 1380 /// Retrieve the arguments to this message, not including the 1381 /// receiver. 1382 Expr **getArgs() { 1383 return reinterpret_cast<Expr **>(getTrailingObjects<void *>() + 1); 1384 } 1385 const Expr * const *getArgs() const { 1386 return reinterpret_cast<const Expr *const *>(getTrailingObjects<void *>() + 1387 1); 1388 } 1389 1390 /// getArg - Return the specified argument. 1391 Expr *getArg(unsigned Arg) { 1392 assert(Arg < NumArgs && "Arg access out of range!"); 1393 return getArgs()[Arg]; 1394 } 1395 const Expr *getArg(unsigned Arg) const { 1396 assert(Arg < NumArgs && "Arg access out of range!"); 1397 return getArgs()[Arg]; 1398 } 1399 1400 /// setArg - Set the specified argument. 1401 void setArg(unsigned Arg, Expr *ArgExpr) { 1402 assert(Arg < NumArgs && "Arg access out of range!"); 1403 getArgs()[Arg] = ArgExpr; 1404 } 1405 1406 /// isDelegateInitCall - Answers whether this message send has been 1407 /// tagged as a "delegate init call", i.e. a call to a method in the 1408 /// -init family on self from within an -init method implementation. 1409 bool isDelegateInitCall() const { return IsDelegateInitCall; } 1410 void setDelegateInitCall(bool isDelegate) { IsDelegateInitCall = isDelegate; } 1411 1412 SourceLocation getLeftLoc() const { return LBracLoc; } 1413 SourceLocation getRightLoc() const { return RBracLoc; } 1414 1415 SourceLocation getSelectorStartLoc() const { 1416 if (isImplicit()) 1417 return getBeginLoc(); 1418 return getSelectorLoc(0); 1419 } 1420 1421 SourceLocation getSelectorLoc(unsigned Index) const { 1422 assert(Index < getNumSelectorLocs() && "Index out of range!"); 1423 if (hasStandardSelLocs()) 1424 return getStandardSelectorLoc( 1425 Index, getSelector(), getSelLocsKind() == SelLoc_StandardWithSpace, 1426 llvm::ArrayRef(const_cast<Expr **>(getArgs()), getNumArgs()), 1427 RBracLoc); 1428 return getStoredSelLocs()[Index]; 1429 } 1430 1431 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const; 1432 1433 unsigned getNumSelectorLocs() const { 1434 if (isImplicit()) 1435 return 0; 1436 Selector Sel = getSelector(); 1437 if (Sel.isUnarySelector()) 1438 return 1; 1439 return Sel.getNumArgs(); 1440 } 1441 1442 void setSourceRange(SourceRange R) { 1443 LBracLoc = R.getBegin(); 1444 RBracLoc = R.getEnd(); 1445 } 1446 1447 SourceLocation getBeginLoc() const LLVM_READONLY { return LBracLoc; } 1448 SourceLocation getEndLoc() const LLVM_READONLY { return RBracLoc; } 1449 1450 // Iterators 1451 child_range children(); 1452 1453 const_child_range children() const; 1454 1455 using arg_iterator = ExprIterator; 1456 using const_arg_iterator = ConstExprIterator; 1457 1458 llvm::iterator_range<arg_iterator> arguments() { 1459 return llvm::make_range(arg_begin(), arg_end()); 1460 } 1461 1462 llvm::iterator_range<const_arg_iterator> arguments() const { 1463 return llvm::make_range(arg_begin(), arg_end()); 1464 } 1465 1466 arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); } 1467 1468 arg_iterator arg_end() { 1469 return reinterpret_cast<Stmt **>(getArgs() + NumArgs); 1470 } 1471 1472 const_arg_iterator arg_begin() const { 1473 return reinterpret_cast<Stmt const * const*>(getArgs()); 1474 } 1475 1476 const_arg_iterator arg_end() const { 1477 return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs); 1478 } 1479 1480 static bool classof(const Stmt *T) { 1481 return T->getStmtClass() == ObjCMessageExprClass; 1482 } 1483 }; 1484 1485 /// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type. 1486 /// (similar in spirit to MemberExpr). 1487 class ObjCIsaExpr : public Expr { 1488 /// Base - the expression for the base object pointer. 1489 Stmt *Base; 1490 1491 /// IsaMemberLoc - This is the location of the 'isa'. 1492 SourceLocation IsaMemberLoc; 1493 1494 /// OpLoc - This is the location of '.' or '->' 1495 SourceLocation OpLoc; 1496 1497 /// IsArrow - True if this is "X->F", false if this is "X.F". 1498 bool IsArrow; 1499 1500 public: 1501 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, SourceLocation oploc, 1502 QualType ty) 1503 : Expr(ObjCIsaExprClass, ty, VK_LValue, OK_Ordinary), Base(base), 1504 IsaMemberLoc(l), OpLoc(oploc), IsArrow(isarrow) { 1505 setDependence(computeDependence(this)); 1506 } 1507 1508 /// Build an empty expression. 1509 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) {} 1510 1511 void setBase(Expr *E) { Base = E; } 1512 Expr *getBase() const { return cast<Expr>(Base); } 1513 1514 bool isArrow() const { return IsArrow; } 1515 void setArrow(bool A) { IsArrow = A; } 1516 1517 /// getMemberLoc - Return the location of the "member", in X->F, it is the 1518 /// location of 'F'. 1519 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; } 1520 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; } 1521 1522 SourceLocation getOpLoc() const { return OpLoc; } 1523 void setOpLoc(SourceLocation L) { OpLoc = L; } 1524 1525 SourceLocation getBeginLoc() const LLVM_READONLY { 1526 return getBase()->getBeginLoc(); 1527 } 1528 1529 SourceLocation getBaseLocEnd() const LLVM_READONLY { 1530 return getBase()->getEndLoc(); 1531 } 1532 1533 SourceLocation getEndLoc() const LLVM_READONLY { return IsaMemberLoc; } 1534 1535 SourceLocation getExprLoc() const LLVM_READONLY { return IsaMemberLoc; } 1536 1537 // Iterators 1538 child_range children() { return child_range(&Base, &Base+1); } 1539 1540 const_child_range children() const { 1541 return const_child_range(&Base, &Base + 1); 1542 } 1543 1544 static bool classof(const Stmt *T) { 1545 return T->getStmtClass() == ObjCIsaExprClass; 1546 } 1547 }; 1548 1549 /// ObjCIndirectCopyRestoreExpr - Represents the passing of a function 1550 /// argument by indirect copy-restore in ARC. This is used to support 1551 /// passing indirect arguments with the wrong lifetime, e.g. when 1552 /// passing the address of a __strong local variable to an 'out' 1553 /// parameter. This expression kind is only valid in an "argument" 1554 /// position to some sort of call expression. 1555 /// 1556 /// The parameter must have type 'pointer to T', and the argument must 1557 /// have type 'pointer to U', where T and U agree except possibly in 1558 /// qualification. If the argument value is null, then a null pointer 1559 /// is passed; otherwise it points to an object A, and: 1560 /// 1. A temporary object B of type T is initialized, either by 1561 /// zero-initialization (used when initializing an 'out' parameter) 1562 /// or copy-initialization (used when initializing an 'inout' 1563 /// parameter). 1564 /// 2. The address of the temporary is passed to the function. 1565 /// 3. If the call completes normally, A is move-assigned from B. 1566 /// 4. Finally, A is destroyed immediately. 1567 /// 1568 /// Currently 'T' must be a retainable object lifetime and must be 1569 /// __autoreleasing; this qualifier is ignored when initializing 1570 /// the value. 1571 class ObjCIndirectCopyRestoreExpr : public Expr { 1572 friend class ASTReader; 1573 friend class ASTStmtReader; 1574 1575 Stmt *Operand; 1576 1577 // unsigned ObjCIndirectCopyRestoreBits.ShouldCopy : 1; 1578 1579 explicit ObjCIndirectCopyRestoreExpr(EmptyShell Empty) 1580 : Expr(ObjCIndirectCopyRestoreExprClass, Empty) {} 1581 1582 void setShouldCopy(bool shouldCopy) { 1583 ObjCIndirectCopyRestoreExprBits.ShouldCopy = shouldCopy; 1584 } 1585 1586 public: 1587 ObjCIndirectCopyRestoreExpr(Expr *operand, QualType type, bool shouldCopy) 1588 : Expr(ObjCIndirectCopyRestoreExprClass, type, VK_LValue, OK_Ordinary), 1589 Operand(operand) { 1590 setShouldCopy(shouldCopy); 1591 setDependence(computeDependence(this)); 1592 } 1593 1594 Expr *getSubExpr() { return cast<Expr>(Operand); } 1595 const Expr *getSubExpr() const { return cast<Expr>(Operand); } 1596 1597 /// shouldCopy - True if we should do the 'copy' part of the 1598 /// copy-restore. If false, the temporary will be zero-initialized. 1599 bool shouldCopy() const { return ObjCIndirectCopyRestoreExprBits.ShouldCopy; } 1600 1601 child_range children() { return child_range(&Operand, &Operand+1); } 1602 1603 const_child_range children() const { 1604 return const_child_range(&Operand, &Operand + 1); 1605 } 1606 1607 // Source locations are determined by the subexpression. 1608 SourceLocation getBeginLoc() const LLVM_READONLY { 1609 return Operand->getBeginLoc(); 1610 } 1611 SourceLocation getEndLoc() const LLVM_READONLY { 1612 return Operand->getEndLoc(); 1613 } 1614 1615 SourceLocation getExprLoc() const LLVM_READONLY { 1616 return getSubExpr()->getExprLoc(); 1617 } 1618 1619 static bool classof(const Stmt *s) { 1620 return s->getStmtClass() == ObjCIndirectCopyRestoreExprClass; 1621 } 1622 }; 1623 1624 /// An Objective-C "bridged" cast expression, which casts between 1625 /// Objective-C pointers and C pointers, transferring ownership in the process. 1626 /// 1627 /// \code 1628 /// NSString *str = (__bridge_transfer NSString *)CFCreateString(); 1629 /// \endcode 1630 class ObjCBridgedCastExpr final 1631 : public ExplicitCastExpr, 1632 private llvm::TrailingObjects<ObjCBridgedCastExpr, CXXBaseSpecifier *> { 1633 friend class ASTStmtReader; 1634 friend class ASTStmtWriter; 1635 friend class CastExpr; 1636 friend TrailingObjects; 1637 1638 SourceLocation LParenLoc; 1639 SourceLocation BridgeKeywordLoc; 1640 LLVM_PREFERRED_TYPE(ObjCBridgeCastKind) 1641 unsigned Kind : 2; 1642 1643 public: 1644 ObjCBridgedCastExpr(SourceLocation LParenLoc, ObjCBridgeCastKind Kind, 1645 CastKind CK, SourceLocation BridgeKeywordLoc, 1646 TypeSourceInfo *TSInfo, Expr *Operand) 1647 : ExplicitCastExpr(ObjCBridgedCastExprClass, TSInfo->getType(), 1648 VK_PRValue, CK, Operand, 0, false, TSInfo), 1649 LParenLoc(LParenLoc), BridgeKeywordLoc(BridgeKeywordLoc), Kind(Kind) {} 1650 1651 /// Construct an empty Objective-C bridged cast. 1652 explicit ObjCBridgedCastExpr(EmptyShell Shell) 1653 : ExplicitCastExpr(ObjCBridgedCastExprClass, Shell, 0, false) {} 1654 1655 SourceLocation getLParenLoc() const { return LParenLoc; } 1656 1657 /// Determine which kind of bridge is being performed via this cast. 1658 ObjCBridgeCastKind getBridgeKind() const { 1659 return static_cast<ObjCBridgeCastKind>(Kind); 1660 } 1661 1662 /// Retrieve the kind of bridge being performed as a string. 1663 StringRef getBridgeKindName() const; 1664 1665 /// The location of the bridge keyword. 1666 SourceLocation getBridgeKeywordLoc() const { return BridgeKeywordLoc; } 1667 1668 SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; } 1669 1670 SourceLocation getEndLoc() const LLVM_READONLY { 1671 return getSubExpr()->getEndLoc(); 1672 } 1673 1674 static bool classof(const Stmt *T) { 1675 return T->getStmtClass() == ObjCBridgedCastExprClass; 1676 } 1677 }; 1678 1679 /// A runtime availability query. 1680 /// 1681 /// There are 2 ways to spell this node: 1682 /// \code 1683 /// @available(macos 10.10, ios 8, *); // Objective-C 1684 /// __builtin_available(macos 10.10, ios 8, *); // C, C++, and Objective-C 1685 /// \endcode 1686 /// 1687 /// Note that we only need to keep track of one \c VersionTuple here, which is 1688 /// the one that corresponds to the current deployment target. This is meant to 1689 /// be used in the condition of an \c if, but it is also usable as top level 1690 /// expressions. 1691 /// 1692 class ObjCAvailabilityCheckExpr : public Expr { 1693 friend class ASTStmtReader; 1694 1695 VersionTuple VersionToCheck; 1696 SourceLocation AtLoc, RParen; 1697 1698 public: 1699 ObjCAvailabilityCheckExpr(VersionTuple VersionToCheck, SourceLocation AtLoc, 1700 SourceLocation RParen, QualType Ty) 1701 : Expr(ObjCAvailabilityCheckExprClass, Ty, VK_PRValue, OK_Ordinary), 1702 VersionToCheck(VersionToCheck), AtLoc(AtLoc), RParen(RParen) { 1703 setDependence(ExprDependence::None); 1704 } 1705 1706 explicit ObjCAvailabilityCheckExpr(EmptyShell Shell) 1707 : Expr(ObjCAvailabilityCheckExprClass, Shell) {} 1708 1709 SourceLocation getBeginLoc() const { return AtLoc; } 1710 SourceLocation getEndLoc() const { return RParen; } 1711 SourceRange getSourceRange() const { return {AtLoc, RParen}; } 1712 1713 /// This may be '*', in which case this should fold to true. 1714 bool hasVersion() const { return !VersionToCheck.empty(); } 1715 VersionTuple getVersion() const { return VersionToCheck; } 1716 1717 child_range children() { 1718 return child_range(child_iterator(), child_iterator()); 1719 } 1720 1721 const_child_range children() const { 1722 return const_child_range(const_child_iterator(), const_child_iterator()); 1723 } 1724 1725 static bool classof(const Stmt *T) { 1726 return T->getStmtClass() == ObjCAvailabilityCheckExprClass; 1727 } 1728 }; 1729 1730 } // namespace clang 1731 1732 #endif // LLVM_CLANG_AST_EXPROBJC_H 1733