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