ExprObjC.h revision 2725ca8eb3354975ca77ed4b88ede7b60b216b9a
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/Expr.h" 18#include "clang/Basic/IdentifierTable.h" 19 20namespace clang { 21 class IdentifierInfo; 22 class ASTContext; 23 class ObjCMethodDecl; 24 class ObjCPropertyDecl; 25 26/// ObjCStringLiteral, used for Objective-C string literals 27/// i.e. @"foo". 28class ObjCStringLiteral : public Expr { 29 Stmt *String; 30 SourceLocation AtLoc; 31public: 32 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 33 : Expr(ObjCStringLiteralClass, T, false, false), String(SL), AtLoc(L) {} 34 explicit ObjCStringLiteral(EmptyShell Empty) 35 : Expr(ObjCStringLiteralClass, Empty) {} 36 37 StringLiteral *getString() { return cast<StringLiteral>(String); } 38 const StringLiteral *getString() const { return cast<StringLiteral>(String); } 39 void setString(StringLiteral *S) { String = S; } 40 41 SourceLocation getAtLoc() const { return AtLoc; } 42 void setAtLoc(SourceLocation L) { AtLoc = L; } 43 44 virtual SourceRange getSourceRange() const { 45 return SourceRange(AtLoc, String->getLocEnd()); 46 } 47 48 static bool classof(const Stmt *T) { 49 return T->getStmtClass() == ObjCStringLiteralClass; 50 } 51 static bool classof(const ObjCStringLiteral *) { return true; } 52 53 // Iterators 54 virtual child_iterator child_begin(); 55 virtual child_iterator child_end(); 56}; 57 58/// ObjCEncodeExpr, used for @encode in Objective-C. @encode has the same type 59/// and behavior as StringLiteral except that the string initializer is obtained 60/// from ASTContext with the encoding type as an argument. 61class ObjCEncodeExpr : public Expr { 62 TypeSourceInfo *EncodedType; 63 SourceLocation AtLoc, RParenLoc; 64public: 65 ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType, 66 SourceLocation at, SourceLocation rp) 67 : Expr(ObjCEncodeExprClass, T, EncodedType->getType()->isDependentType(), 68 EncodedType->getType()->isDependentType()), 69 EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {} 70 71 explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){} 72 73 74 SourceLocation getAtLoc() const { return AtLoc; } 75 void setAtLoc(SourceLocation L) { AtLoc = L; } 76 SourceLocation getRParenLoc() const { return RParenLoc; } 77 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 78 79 QualType getEncodedType() const { return EncodedType->getType(); } 80 81 TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; } 82 void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) { 83 EncodedType = EncType; 84 } 85 86 virtual SourceRange getSourceRange() const { 87 return SourceRange(AtLoc, RParenLoc); 88 } 89 90 static bool classof(const Stmt *T) { 91 return T->getStmtClass() == ObjCEncodeExprClass; 92 } 93 static bool classof(const ObjCEncodeExpr *) { return true; } 94 95 // Iterators 96 virtual child_iterator child_begin(); 97 virtual child_iterator child_end(); 98}; 99 100/// ObjCSelectorExpr used for @selector in Objective-C. 101class ObjCSelectorExpr : public Expr { 102 Selector SelName; 103 SourceLocation AtLoc, RParenLoc; 104public: 105 ObjCSelectorExpr(QualType T, Selector selInfo, 106 SourceLocation at, SourceLocation rp) 107 : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), AtLoc(at), 108 RParenLoc(rp){} 109 explicit ObjCSelectorExpr(EmptyShell Empty) 110 : Expr(ObjCSelectorExprClass, Empty) {} 111 112 Selector getSelector() const { return SelName; } 113 void setSelector(Selector S) { SelName = S; } 114 115 SourceLocation getAtLoc() const { return AtLoc; } 116 SourceLocation getRParenLoc() const { return RParenLoc; } 117 void setAtLoc(SourceLocation L) { AtLoc = L; } 118 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 119 120 virtual SourceRange getSourceRange() const { 121 return SourceRange(AtLoc, RParenLoc); 122 } 123 124 /// getNumArgs - Return the number of actual arguments to this call. 125 unsigned getNumArgs() const { return SelName.getNumArgs(); } 126 127 static bool classof(const Stmt *T) { 128 return T->getStmtClass() == ObjCSelectorExprClass; 129 } 130 static bool classof(const ObjCSelectorExpr *) { return true; } 131 132 // Iterators 133 virtual child_iterator child_begin(); 134 virtual child_iterator child_end(); 135}; 136 137/// ObjCProtocolExpr used for protocol expression in Objective-C. This is used 138/// as: @protocol(foo), as in: 139/// obj conformsToProtocol:@protocol(foo)] 140/// The return type is "Protocol*". 141class ObjCProtocolExpr : public Expr { 142 ObjCProtocolDecl *TheProtocol; 143 SourceLocation AtLoc, RParenLoc; 144public: 145 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol, 146 SourceLocation at, SourceLocation rp) 147 : Expr(ObjCProtocolExprClass, T, false, false), TheProtocol(protocol), 148 AtLoc(at), RParenLoc(rp) {} 149 explicit ObjCProtocolExpr(EmptyShell Empty) 150 : Expr(ObjCProtocolExprClass, Empty) {} 151 152 ObjCProtocolDecl *getProtocol() const { return TheProtocol; } 153 void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; } 154 155 SourceLocation getAtLoc() const { return AtLoc; } 156 SourceLocation getRParenLoc() const { return RParenLoc; } 157 void setAtLoc(SourceLocation L) { AtLoc = L; } 158 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 159 160 virtual SourceRange getSourceRange() const { 161 return SourceRange(AtLoc, RParenLoc); 162 } 163 164 static bool classof(const Stmt *T) { 165 return T->getStmtClass() == ObjCProtocolExprClass; 166 } 167 static bool classof(const ObjCProtocolExpr *) { return true; } 168 169 // Iterators 170 virtual child_iterator child_begin(); 171 virtual child_iterator child_end(); 172}; 173 174/// ObjCIvarRefExpr - A reference to an ObjC instance variable. 175class ObjCIvarRefExpr : public Expr { 176 class ObjCIvarDecl *D; 177 SourceLocation Loc; 178 Stmt *Base; 179 bool IsArrow:1; // True if this is "X->F", false if this is "X.F". 180 bool IsFreeIvar:1; // True if ivar reference has no base (self assumed). 181 182public: 183 ObjCIvarRefExpr(ObjCIvarDecl *d, 184 QualType t, SourceLocation l, Expr *base=0, 185 bool arrow = false, bool freeIvar = false) : 186 Expr(ObjCIvarRefExprClass, t, false, false), D(d), 187 Loc(l), Base(base), IsArrow(arrow), 188 IsFreeIvar(freeIvar) {} 189 190 explicit ObjCIvarRefExpr(EmptyShell Empty) 191 : Expr(ObjCIvarRefExprClass, Empty) {} 192 193 ObjCIvarDecl *getDecl() { return D; } 194 const ObjCIvarDecl *getDecl() const { return D; } 195 void setDecl(ObjCIvarDecl *d) { D = d; } 196 197 const Expr *getBase() const { return cast<Expr>(Base); } 198 Expr *getBase() { return cast<Expr>(Base); } 199 void setBase(Expr * base) { Base = base; } 200 201 bool isArrow() const { return IsArrow; } 202 bool isFreeIvar() const { return IsFreeIvar; } 203 void setIsArrow(bool A) { IsArrow = A; } 204 void setIsFreeIvar(bool A) { IsFreeIvar = A; } 205 206 SourceLocation getLocation() const { return Loc; } 207 void setLocation(SourceLocation L) { Loc = L; } 208 209 virtual SourceRange getSourceRange() const { 210 return isFreeIvar() ? SourceRange(Loc) 211 : SourceRange(getBase()->getLocStart(), Loc); 212 } 213 214 static bool classof(const Stmt *T) { 215 return T->getStmtClass() == ObjCIvarRefExprClass; 216 } 217 static bool classof(const ObjCIvarRefExpr *) { return true; } 218 219 // Iterators 220 virtual child_iterator child_begin(); 221 virtual child_iterator child_end(); 222}; 223 224/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 225/// property. 226/// 227class ObjCPropertyRefExpr : public Expr { 228private: 229 ObjCPropertyDecl *AsProperty; 230 SourceLocation IdLoc; 231 Stmt *Base; 232public: 233 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 234 SourceLocation l, Expr *base) 235 : Expr(ObjCPropertyRefExprClass, t, false, false), AsProperty(PD), 236 IdLoc(l), Base(base) { 237 } 238 239 explicit ObjCPropertyRefExpr(EmptyShell Empty) 240 : Expr(ObjCPropertyRefExprClass, Empty) {} 241 242 ObjCPropertyDecl *getProperty() const { return AsProperty; } 243 void setProperty(ObjCPropertyDecl *D) { AsProperty = D; } 244 245 const Expr *getBase() const { return cast<Expr>(Base); } 246 Expr *getBase() { return cast<Expr>(Base); } 247 void setBase(Expr *base) { Base = base; } 248 249 SourceLocation getLocation() const { return IdLoc; } 250 void setLocation(SourceLocation L) { IdLoc = L; } 251 252 virtual SourceRange getSourceRange() const { 253 return SourceRange(getBase()->getLocStart(), IdLoc); 254 } 255 256 static bool classof(const Stmt *T) { 257 return T->getStmtClass() == ObjCPropertyRefExprClass; 258 } 259 static bool classof(const ObjCPropertyRefExpr *) { return true; } 260 261 // Iterators 262 virtual child_iterator child_begin(); 263 virtual child_iterator child_end(); 264}; 265 266/// ObjCImplicitSetterGetterRefExpr - A dot-syntax expression to access two 267/// methods; one to set a value to an 'ivar' (Setter) and the other to access 268/// an 'ivar' (Setter). 269/// An example for use of this AST is: 270/// @code 271/// @interface Test { } 272/// - (Test *)crash; 273/// - (void)setCrash: (Test*)value; 274/// @end 275/// void foo(Test *p1, Test *p2) 276/// { 277/// p2.crash = p1.crash; // Uses ObjCImplicitSetterGetterRefExpr AST 278/// } 279/// @endcode 280class ObjCImplicitSetterGetterRefExpr : public Expr { 281 /// Setter - Setter method user declared for setting its 'ivar' to a value 282 ObjCMethodDecl *Setter; 283 /// Getter - Getter method user declared for accessing 'ivar' it controls. 284 ObjCMethodDecl *Getter; 285 /// Location of the member in the dot syntax notation. This is location 286 /// of the getter method. 287 SourceLocation MemberLoc; 288 // FIXME: Swizzle these into a single pointer. 289 Stmt *Base; 290 ObjCInterfaceDecl *InterfaceDecl; 291 /// Location of the receiver class in the dot syntax notation 292 /// used to call a class method setter/getter. 293 SourceLocation ClassLoc; 294 295public: 296 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 297 QualType t, 298 ObjCMethodDecl *setter, 299 SourceLocation l, Expr *base) 300 : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false), 301 Setter(setter), Getter(getter), MemberLoc(l), Base(base), 302 InterfaceDecl(0), ClassLoc(SourceLocation()) { 303 } 304 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 305 QualType t, 306 ObjCMethodDecl *setter, 307 SourceLocation l, ObjCInterfaceDecl *C, SourceLocation CL) 308 : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false), 309 Setter(setter), Getter(getter), MemberLoc(l), Base(0), InterfaceDecl(C), 310 ClassLoc(CL) { 311 } 312 explicit ObjCImplicitSetterGetterRefExpr(EmptyShell Empty) 313 : Expr(ObjCImplicitSetterGetterRefExprClass, Empty){} 314 315 ObjCMethodDecl *getGetterMethod() const { return Getter; } 316 ObjCMethodDecl *getSetterMethod() const { return Setter; } 317 ObjCInterfaceDecl *getInterfaceDecl() const { return InterfaceDecl; } 318 void setGetterMethod(ObjCMethodDecl *D) { Getter = D; } 319 void setSetterMethod(ObjCMethodDecl *D) { Setter = D; } 320 void setInterfaceDecl(ObjCInterfaceDecl *D) { InterfaceDecl = D; } 321 322 virtual SourceRange getSourceRange() const { 323 if (Base) 324 return SourceRange(getBase()->getLocStart(), MemberLoc); 325 return SourceRange(ClassLoc, MemberLoc); 326 } 327 const Expr *getBase() const { return cast_or_null<Expr>(Base); } 328 Expr *getBase() { return cast_or_null<Expr>(Base); } 329 void setBase(Expr *base) { Base = base; } 330 331 SourceLocation getLocation() const { return MemberLoc; } 332 void setLocation(SourceLocation L) { MemberLoc = L; } 333 SourceLocation getClassLoc() const { return ClassLoc; } 334 void setClassLoc(SourceLocation L) { ClassLoc = L; } 335 336 static bool classof(const Stmt *T) { 337 return T->getStmtClass() == ObjCImplicitSetterGetterRefExprClass; 338 } 339 static bool classof(const ObjCImplicitSetterGetterRefExpr *) { return true; } 340 341 // Iterators 342 virtual child_iterator child_begin(); 343 virtual child_iterator child_end(); 344}; 345 346/// \brief An expression that sends a message to the given Objective-C 347/// object or class. 348/// 349/// The following contains two message send expressions: 350/// 351/// \code 352/// [[NSString alloc] initWithString:@"Hello"] 353/// \endcode 354/// 355/// The innermost message send invokes the "alloc" class method on the 356/// NSString class, while the outermost message send invokes the 357/// "initWithString" instance method on the object returned from 358/// NSString's "alloc". In all, an Objective-C message send can take 359/// on four different (although related) forms: 360/// 361/// 1. Send to an object instance. 362/// 2. Send to a class. 363/// 3. Send to the superclass instance of the current class. 364/// 4. Send to the superclass of the current class. 365/// 366/// All four kinds of message sends are modeled by the ObjCMessageExpr 367/// class, and can be distinguished via \c getReceiverKind(). Example: 368/// 369class ObjCMessageExpr : public Expr { 370 /// \brief The number of arguments in the message send, not 371 /// including the receiver. 372 unsigned NumArgs : 16; 373 374 /// \brief The kind of message send this is, which is one of the 375 /// ReceiverKind values. 376 /// 377 /// We pad this out to a byte to avoid excessive masking and shifting. 378 unsigned Kind : 8; 379 380 /// \brief Whether we have an actual method prototype in \c 381 /// SelectorOrMethod. 382 /// 383 /// When non-zero, we have a method declaration; otherwise, we just 384 /// have a selector. 385 unsigned HasMethod : 8; 386 387 /// \brief When the message expression is a send to 'super', this is 388 /// the location of the 'super' keyword. 389 SourceLocation SuperLoc; 390 391 /// \brief Stores either the selector that this message is sending 392 /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer 393 /// referring to the method that we type-checked against. 394 uintptr_t SelectorOrMethod; 395 396 /// \brief The source locations of the open and close square 397 /// brackets ('[' and ']', respectively). 398 SourceLocation LBracLoc, RBracLoc; 399 400 ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs) 401 : Expr(ObjCMessageExprClass, Empty), NumArgs(NumArgs), Kind(0), 402 HasMethod(0), SelectorOrMethod(0) { } 403 404 ObjCMessageExpr(QualType T, 405 SourceLocation LBracLoc, 406 SourceLocation SuperLoc, 407 bool IsInstanceSuper, 408 QualType SuperType, 409 Selector Sel, 410 ObjCMethodDecl *Method, 411 Expr **Args, unsigned NumArgs, 412 SourceLocation RBracLoc); 413 ObjCMessageExpr(QualType T, 414 SourceLocation LBracLoc, 415 TypeSourceInfo *Receiver, 416 Selector Sel, 417 ObjCMethodDecl *Method, 418 Expr **Args, unsigned NumArgs, 419 SourceLocation RBracLoc); 420 ObjCMessageExpr(QualType T, 421 SourceLocation LBracLoc, 422 Expr *Receiver, 423 Selector Sel, 424 ObjCMethodDecl *Method, 425 Expr **Args, unsigned NumArgs, 426 SourceLocation RBracLoc); 427 428 /// \brief Retrieve the pointer value of the message receiver. 429 void *getReceiverPointer() const { 430 return *const_cast<void **>( 431 reinterpret_cast<const void * const*>(this + 1)); 432 } 433 434 /// \brief Set the pointer value of the message receiver. 435 void setReceiverPointer(void *Value) { 436 *reinterpret_cast<void **>(this + 1) = Value; 437 } 438 439public: 440 /// \brief The kind of receiver this message is sending to. 441 enum ReceiverKind { 442 /// \brief The receiver is a class. 443 Class = 0, 444 /// \brief The receiver is an object instance. 445 Instance, 446 /// \brief The receiver is a superclass. 447 SuperClass, 448 /// \brief The receiver is the instance of the superclass object. 449 SuperInstance 450 }; 451 452 /// \brief Create a message send to super. 453 /// 454 /// \param Context The ASTContext in which this expression will be created. 455 /// 456 /// \param T The result type of this message. 457 /// 458 /// \param LBrac The location of the open square bracket '['. 459 /// 460 /// \param SuperLoc The location of the "super" keyword. 461 /// 462 /// \param IsInstanceSuper Whether this is an instance "super" 463 /// message (otherwise, it's a class "super" message). 464 /// 465 /// \param Sel The selector used to determine which method gets called. 466 /// 467 /// \param Method The Objective-C method against which this message 468 /// send was type-checked. May be NULL. 469 /// 470 /// \param Args The message send arguments. 471 /// 472 /// \param NumArgs The number of arguments. 473 /// 474 /// \param RBracLoc The location of the closing square bracket ']'. 475 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 476 SourceLocation LBracLoc, 477 SourceLocation SuperLoc, 478 bool IsInstanceSuper, 479 QualType SuperType, 480 Selector Sel, 481 ObjCMethodDecl *Method, 482 Expr **Args, unsigned NumArgs, 483 SourceLocation RBracLoc); 484 485 /// \brief Create a class message send. 486 /// 487 /// \param Context The ASTContext in which this expression will be created. 488 /// 489 /// \param T The result type of this message. 490 /// 491 /// \param LBrac The location of the open square bracket '['. 492 /// 493 /// \param Receiver The type of the receiver, including 494 /// source-location information. 495 /// 496 /// \param Sel The selector used to determine which method gets called. 497 /// 498 /// \param Method The Objective-C method against which this message 499 /// send was type-checked. May be NULL. 500 /// 501 /// \param Args The message send arguments. 502 /// 503 /// \param NumArgs The number of arguments. 504 /// 505 /// \param RBracLoc The location of the closing square bracket ']'. 506 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 507 SourceLocation LBracLoc, 508 TypeSourceInfo *Receiver, 509 Selector Sel, 510 ObjCMethodDecl *Method, 511 Expr **Args, unsigned NumArgs, 512 SourceLocation RBracLoc); 513 514 /// \brief Create an instance message send. 515 /// 516 /// \param Context The ASTContext in which this expression will be created. 517 /// 518 /// \param T The result type of this message. 519 /// 520 /// \param LBrac The location of the open square bracket '['. 521 /// 522 /// \param Receiver The expression used to produce the object that 523 /// will receive this message. 524 /// 525 /// \param Sel The selector used to determine which method gets called. 526 /// 527 /// \param Method The Objective-C method against which this message 528 /// send was type-checked. May be NULL. 529 /// 530 /// \param Args The message send arguments. 531 /// 532 /// \param NumArgs The number of arguments. 533 /// 534 /// \param RBracLoc The location of the closing square bracket ']'. 535 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 536 SourceLocation LBracLoc, 537 Expr *Receiver, 538 Selector Sel, 539 ObjCMethodDecl *Method, 540 Expr **Args, unsigned NumArgs, 541 SourceLocation RBracLoc); 542 543 /// \brief Create an empty Objective-C message expression, to be 544 /// filled in by subsequent calls. 545 /// 546 /// \param Context The context in which the message send will be created. 547 /// 548 /// \param NumArgs The number of message arguments, not including 549 /// the receiver. 550 static ObjCMessageExpr *CreateEmpty(ASTContext &Context, unsigned NumArgs); 551 552 /// \brief Determine the kind of receiver that this message is being 553 /// sent to. 554 ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; } 555 556 /// \brief Determine whether this is an instance message to either a 557 /// computed object or to super. 558 bool isInstanceMessage() const { 559 return getReceiverKind() == Instance || getReceiverKind() == SuperInstance; 560 } 561 562 /// \brief Determine whether this is an class message to either a 563 /// specified class or to super. 564 bool isClassMessage() const { 565 return getReceiverKind() == Class || getReceiverKind() == SuperClass; 566 } 567 568 /// \brief Returns the receiver of an instance message. 569 /// 570 /// \brief Returns the object expression for an instance message, or 571 /// NULL for a message that is not an instance message. 572 Expr *getInstanceReceiver() { 573 if (getReceiverKind() == Instance) 574 return static_cast<Expr *>(getReceiverPointer()); 575 576 return 0; 577 } 578 const Expr *getInstanceReceiver() const { 579 return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver(); 580 } 581 582 /// \brief Turn this message send into an instance message that 583 /// computes the receiver object with the given expression. 584 void setInstanceReceiver(Expr *rec) { 585 Kind = Instance; 586 setReceiverPointer(rec); 587 } 588 589 /// \brief Returns the type of a class message send, or NULL if the 590 /// message is not a class message. 591 QualType getClassReceiver() const { 592 if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo()) 593 return TSInfo->getType(); 594 595 return QualType(); 596 } 597 598 /// \brief Returns a type-source information of a class message 599 /// send, or NULL if the message is not a class message. 600 TypeSourceInfo *getClassReceiverTypeInfo() const { 601 if (getReceiverKind() == Class) 602 return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer()); 603 return 0; 604 } 605 606 void setClassReceiver(TypeSourceInfo *TSInfo) { 607 Kind = Class; 608 setReceiverPointer(TSInfo); 609 } 610 611 /// \brief Retrieve the location of the 'super' keyword for a class 612 /// or instance message to 'super', otherwise an invalid source location. 613 SourceLocation getSuperLoc() const { 614 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 615 return SuperLoc; 616 617 return SourceLocation(); 618 } 619 620 /// \brief Retrieve the Objective-C interface to which this message 621 /// is being directed, if known. 622 /// 623 /// This routine cross-cuts all of the different kinds of message 624 /// sends to determine what the underlying (statically known) type 625 /// of the receiver will be; use \c getReceiverKind() to determine 626 /// whether the message is a class or an instance method, whether it 627 /// is a send to super or not, etc. 628 /// 629 /// \returns The Objective-C interface if known, otherwise NULL. 630 ObjCInterfaceDecl *getReceiverInterface() const; 631 632 /// \brief Retrieve the type referred to by 'super'. 633 /// 634 /// The returned type will either be an ObjCInterfaceType (for an 635 /// class message to super) or an ObjCObjectPointerType that refers 636 /// to a class (for an instance message to super); 637 QualType getSuperType() const { 638 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 639 return QualType::getFromOpaquePtr(getReceiverPointer()); 640 641 return QualType(); 642 } 643 644 void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) { 645 Kind = IsInstanceSuper? SuperInstance : SuperClass; 646 SuperLoc = Loc; 647 setReceiverPointer(T.getAsOpaquePtr()); 648 } 649 650 Selector getSelector() const; 651 652 void setSelector(Selector S) { 653 HasMethod = false; 654 SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr()); 655 } 656 657 const ObjCMethodDecl *getMethodDecl() const { 658 if (HasMethod) 659 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod); 660 661 return 0; 662 } 663 664 ObjCMethodDecl *getMethodDecl() { 665 if (HasMethod) 666 return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod); 667 668 return 0; 669 } 670 671 void setMethodDecl(ObjCMethodDecl *MD) { 672 HasMethod = true; 673 SelectorOrMethod = reinterpret_cast<uintptr_t>(MD); 674 } 675 676 /// \brief Return the number of actual arguments in this message, 677 /// not counting the receiver. 678 unsigned getNumArgs() const { return NumArgs; } 679 680 /// \brief Retrieve the arguments to this message, not including the 681 /// receiver. 682 Stmt **getArgs() { 683 return reinterpret_cast<Stmt **>(this + 1) + 1; 684 } 685 const Stmt * const *getArgs() const { 686 return reinterpret_cast<const Stmt * const *>(this + 1) + 1; 687 } 688 689 /// getArg - Return the specified argument. 690 Expr *getArg(unsigned Arg) { 691 assert(Arg < NumArgs && "Arg access out of range!"); 692 return cast<Expr>(getArgs()[Arg]); 693 } 694 const Expr *getArg(unsigned Arg) const { 695 assert(Arg < NumArgs && "Arg access out of range!"); 696 return cast<Expr>(getArgs()[Arg]); 697 } 698 /// setArg - Set the specified argument. 699 void setArg(unsigned Arg, Expr *ArgExpr) { 700 assert(Arg < NumArgs && "Arg access out of range!"); 701 getArgs()[Arg] = ArgExpr; 702 } 703 704 SourceLocation getLeftLoc() const { return LBracLoc; } 705 SourceLocation getRightLoc() const { return RBracLoc; } 706 707 void setLeftLoc(SourceLocation L) { LBracLoc = L; } 708 void setRightLoc(SourceLocation L) { RBracLoc = L; } 709 710 void setSourceRange(SourceRange R) { 711 LBracLoc = R.getBegin(); 712 RBracLoc = R.getEnd(); 713 } 714 virtual SourceRange getSourceRange() const { 715 return SourceRange(LBracLoc, RBracLoc); 716 } 717 718 static bool classof(const Stmt *T) { 719 return T->getStmtClass() == ObjCMessageExprClass; 720 } 721 static bool classof(const ObjCMessageExpr *) { return true; } 722 723 // Iterators 724 virtual child_iterator child_begin(); 725 virtual child_iterator child_end(); 726 727 typedef ExprIterator arg_iterator; 728 typedef ConstExprIterator const_arg_iterator; 729 730 arg_iterator arg_begin() { return getArgs(); } 731 arg_iterator arg_end() { return getArgs() + NumArgs; } 732 const_arg_iterator arg_begin() const { return getArgs(); } 733 const_arg_iterator arg_end() const { return getArgs() + NumArgs; } 734}; 735 736/// ObjCSuperExpr - Represents the "super" expression in Objective-C, 737/// which refers to the object on which the current method is executing. 738/// 739/// FIXME: This class is intended for removal, once its remaining 740/// clients have been altered to represent "super" internally. 741class ObjCSuperExpr : public Expr { 742 SourceLocation Loc; 743public: 744 ObjCSuperExpr(SourceLocation L, QualType Type) 745 : Expr(ObjCSuperExprClass, Type, false, false), Loc(L) { } 746 explicit ObjCSuperExpr(EmptyShell Empty) : Expr(ObjCSuperExprClass, Empty) {} 747 748 SourceLocation getLoc() const { return Loc; } 749 void setLoc(SourceLocation L) { Loc = L; } 750 751 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 752 753 static bool classof(const Stmt *T) { 754 return T->getStmtClass() == ObjCSuperExprClass; 755 } 756 static bool classof(const ObjCSuperExpr *) { return true; } 757 758 // Iterators 759 virtual child_iterator child_begin(); 760 virtual child_iterator child_end(); 761}; 762 763/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type. 764/// (similiar in spirit to MemberExpr). 765class ObjCIsaExpr : public Expr { 766 /// Base - the expression for the base object pointer. 767 Stmt *Base; 768 769 /// IsaMemberLoc - This is the location of the 'isa'. 770 SourceLocation IsaMemberLoc; 771 772 /// IsArrow - True if this is "X->F", false if this is "X.F". 773 bool IsArrow; 774public: 775 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty) 776 : Expr(ObjCIsaExprClass, ty, false, false), 777 Base(base), IsaMemberLoc(l), IsArrow(isarrow) {} 778 779 /// \brief Build an empty expression. 780 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { } 781 782 void setBase(Expr *E) { Base = E; } 783 Expr *getBase() const { return cast<Expr>(Base); } 784 785 bool isArrow() const { return IsArrow; } 786 void setArrow(bool A) { IsArrow = A; } 787 788 /// getMemberLoc - Return the location of the "member", in X->F, it is the 789 /// location of 'F'. 790 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; } 791 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; } 792 793 virtual SourceRange getSourceRange() const { 794 return SourceRange(getBase()->getLocStart(), IsaMemberLoc); 795 } 796 797 virtual SourceLocation getExprLoc() const { return IsaMemberLoc; } 798 799 static bool classof(const Stmt *T) { 800 return T->getStmtClass() == ObjCIsaExprClass; 801 } 802 static bool classof(const ObjCIsaExpr *) { return true; } 803 804 // Iterators 805 virtual child_iterator child_begin(); 806 virtual child_iterator child_end(); 807}; 808 809} // end namespace clang 810 811#endif 812