ExprObjC.h revision c44d91c446f6d59722da53dba9d686560e81b762
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, 185 bool arrow = false, bool freeIvar = false) : 186 Expr(ObjCIvarRefExprClass, t, /*TypeDependent=*/false, 187 base->isValueDependent()), D(d), 188 Loc(l), Base(base), IsArrow(arrow), 189 IsFreeIvar(freeIvar) {} 190 191 explicit ObjCIvarRefExpr(EmptyShell Empty) 192 : Expr(ObjCIvarRefExprClass, Empty) {} 193 194 ObjCIvarDecl *getDecl() { return D; } 195 const ObjCIvarDecl *getDecl() const { return D; } 196 void setDecl(ObjCIvarDecl *d) { D = d; } 197 198 const Expr *getBase() const { return cast<Expr>(Base); } 199 Expr *getBase() { return cast<Expr>(Base); } 200 void setBase(Expr * base) { Base = base; } 201 202 bool isArrow() const { return IsArrow; } 203 bool isFreeIvar() const { return IsFreeIvar; } 204 void setIsArrow(bool A) { IsArrow = A; } 205 void setIsFreeIvar(bool A) { IsFreeIvar = A; } 206 207 SourceLocation getLocation() const { return Loc; } 208 void setLocation(SourceLocation L) { Loc = L; } 209 210 virtual SourceRange getSourceRange() const { 211 return isFreeIvar() ? SourceRange(Loc) 212 : SourceRange(getBase()->getLocStart(), Loc); 213 } 214 215 static bool classof(const Stmt *T) { 216 return T->getStmtClass() == ObjCIvarRefExprClass; 217 } 218 static bool classof(const ObjCIvarRefExpr *) { return true; } 219 220 // Iterators 221 virtual child_iterator child_begin(); 222 virtual child_iterator child_end(); 223}; 224 225/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 226/// property. 227/// 228class ObjCPropertyRefExpr : public Expr { 229private: 230 ObjCPropertyDecl *AsProperty; 231 SourceLocation IdLoc; 232 233 /// \brief When the receiver in property access is 'super', this is 234 /// the location of the 'super' keyword. 235 SourceLocation SuperLoc; 236 237 /// \brief When the receiver in property access is 'super', this is 238 /// the type associated with 'super' keyword. A null type indicates 239 /// that this is not a 'super' receiver. 240 llvm::PointerUnion<Stmt*, Type*> BaseExprOrSuperType; 241 242public: 243 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 244 SourceLocation l, Expr *base) 245 : Expr(ObjCPropertyRefExprClass, t, /*TypeDependent=*/false, 246 base->isValueDependent()), 247 AsProperty(PD), IdLoc(l), BaseExprOrSuperType(base) { 248 } 249 250 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 251 SourceLocation l, SourceLocation sl, QualType st) 252 : Expr(ObjCPropertyRefExprClass, t, /*TypeDependent=*/false, false), 253 AsProperty(PD), IdLoc(l), SuperLoc(sl), 254 BaseExprOrSuperType(st.getTypePtr()) { 255 } 256 257 explicit ObjCPropertyRefExpr(EmptyShell Empty) 258 : Expr(ObjCPropertyRefExprClass, Empty) {} 259 260 ObjCPropertyDecl *getProperty() const { return AsProperty; } 261 262 const Expr *getBase() const { 263 return cast<Expr>(BaseExprOrSuperType.get<Stmt*>()); 264 } 265 Expr *getBase() { 266 return cast<Expr>(BaseExprOrSuperType.get<Stmt*>()); 267 } 268 269 SourceLocation getLocation() const { return IdLoc; } 270 271 SourceLocation getSuperLocation() const { return SuperLoc; } 272 QualType getSuperType() const { 273 Type *t = BaseExprOrSuperType.get<Type*>(); 274 return QualType(t, 0); 275 } 276 bool isSuperReceiver() const { return BaseExprOrSuperType.is<Type*>(); } 277 278 virtual SourceRange getSourceRange() const { 279 return SourceRange( 280 (BaseExprOrSuperType.is<Stmt*>() ? getBase()->getLocStart() 281 : getSuperLocation()), 282 IdLoc); 283 } 284 285 static bool classof(const Stmt *T) { 286 return T->getStmtClass() == ObjCPropertyRefExprClass; 287 } 288 static bool classof(const ObjCPropertyRefExpr *) { return true; } 289 290 // Iterators 291 virtual child_iterator child_begin(); 292 virtual child_iterator child_end(); 293private: 294 friend class ASTStmtReader; 295 void setProperty(ObjCPropertyDecl *D) { AsProperty = D; } 296 void setBase(Expr *base) { BaseExprOrSuperType = base; } 297 void setLocation(SourceLocation L) { IdLoc = L; } 298 void setSuperLocation(SourceLocation Loc) { SuperLoc = Loc; } 299 void setSuperType(QualType T) { BaseExprOrSuperType = T.getTypePtr(); } 300}; 301 302/// ObjCImplicitSetterGetterRefExpr - A dot-syntax expression to access two 303/// methods; one to set a value to an 'ivar' (Setter) and the other to access 304/// an 'ivar' (Setter). 305/// An example for use of this AST is: 306/// @code 307/// @interface Test { } 308/// - (Test *)crash; 309/// - (void)setCrash: (Test*)value; 310/// @end 311/// void foo(Test *p1, Test *p2) 312/// { 313/// p2.crash = p1.crash; // Uses ObjCImplicitSetterGetterRefExpr AST 314/// } 315/// @endcode 316class ObjCImplicitSetterGetterRefExpr : public Expr { 317 /// Setter - Setter method user declared for setting its 'ivar' to a value 318 ObjCMethodDecl *Setter; 319 /// Getter - Getter method user declared for accessing 'ivar' it controls. 320 ObjCMethodDecl *Getter; 321 /// Location of the member in the dot syntax notation. This is location 322 /// of the getter method. 323 SourceLocation MemberLoc; 324 // FIXME: Swizzle these into a single pointer. 325 Stmt *Base; 326 ObjCInterfaceDecl *InterfaceDecl; 327 /// \brief Location of the receiver class in the dot syntax notation 328 /// used to call a class method setter/getter. 329 SourceLocation ClassLoc; 330 331 /// \brief When the receiver in dot-syntax expression is 'super', 332 /// this is the location of the 'super' keyword. 333 SourceLocation SuperLoc; 334 335 /// \brief When the receiver in dot-syntax expression is 'super', this is 336 /// the type associated with 'super' keyword. 337 QualType SuperTy; 338 339public: 340 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 341 QualType t, 342 ObjCMethodDecl *setter, 343 SourceLocation l, Expr *base) 344 : Expr(ObjCImplicitSetterGetterRefExprClass, t, /*TypeDependent=*/false, 345 base->isValueDependent()), 346 Setter(setter), Getter(getter), MemberLoc(l), Base(base), 347 InterfaceDecl(0), ClassLoc(SourceLocation()) {} 348 349 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 350 QualType t, 351 ObjCMethodDecl *setter, 352 SourceLocation l, 353 SourceLocation sl, 354 QualType st) 355 : Expr(ObjCImplicitSetterGetterRefExprClass, t, /*TypeDependent=*/false, 356 false), 357 Setter(setter), Getter(getter), MemberLoc(l), 358 Base(0), InterfaceDecl(0), ClassLoc(SourceLocation()), 359 SuperLoc(sl), SuperTy(st) { 360 } 361 362 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 363 QualType t, 364 ObjCMethodDecl *setter, 365 SourceLocation l, ObjCInterfaceDecl *C, SourceLocation CL) 366 : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false), 367 Setter(setter), Getter(getter), MemberLoc(l), Base(0), InterfaceDecl(C), 368 ClassLoc(CL) {} 369 explicit ObjCImplicitSetterGetterRefExpr(EmptyShell Empty) 370 : Expr(ObjCImplicitSetterGetterRefExprClass, Empty){} 371 372 ObjCMethodDecl *getGetterMethod() const { return Getter; } 373 ObjCMethodDecl *getSetterMethod() const { return Setter; } 374 ObjCInterfaceDecl *getInterfaceDecl() const { return InterfaceDecl; } 375 void setGetterMethod(ObjCMethodDecl *D) { Getter = D; } 376 void setSetterMethod(ObjCMethodDecl *D) { Setter = D; } 377 void setInterfaceDecl(ObjCInterfaceDecl *D) { InterfaceDecl = D; } 378 379 virtual SourceRange getSourceRange() const { 380 if (isSuperReceiver()) 381 return SourceRange(getSuperLocation(), MemberLoc); 382 if (Base) 383 return SourceRange(getBase()->getLocStart(), MemberLoc); 384 return SourceRange(ClassLoc, MemberLoc); 385 } 386 const Expr *getBase() const { return cast_or_null<Expr>(Base); } 387 Expr *getBase() { return cast_or_null<Expr>(Base); } 388 void setBase(Expr *base) { Base = base; } 389 390 SourceLocation getLocation() const { return MemberLoc; } 391 void setLocation(SourceLocation L) { MemberLoc = L; } 392 SourceLocation getClassLoc() const { return ClassLoc; } 393 void setClassLoc(SourceLocation L) { ClassLoc = L; } 394 395 SourceLocation getSuperLocation() const { return SuperLoc; } 396 QualType getSuperType() const { return SuperTy; } 397 /// \brief When the receiver in dot-syntax expression is 'super', this 398 /// method returns true if both Base expression and Interface are null. 399 bool isSuperReceiver() const { return InterfaceDecl == 0 && Base == 0; } 400 401 static bool classof(const Stmt *T) { 402 return T->getStmtClass() == ObjCImplicitSetterGetterRefExprClass; 403 } 404 static bool classof(const ObjCImplicitSetterGetterRefExpr *) { return true; } 405 406 // Iterators 407 virtual child_iterator child_begin(); 408 virtual child_iterator child_end(); 409 410private: 411 friend class ASTStmtReader; 412 void setSuperLocation(SourceLocation Loc) { SuperLoc = Loc; } 413 void setSuperType(QualType T) { SuperTy = T; } 414}; 415 416/// \brief An expression that sends a message to the given Objective-C 417/// object or class. 418/// 419/// The following contains two message send expressions: 420/// 421/// \code 422/// [[NSString alloc] initWithString:@"Hello"] 423/// \endcode 424/// 425/// The innermost message send invokes the "alloc" class method on the 426/// NSString class, while the outermost message send invokes the 427/// "initWithString" instance method on the object returned from 428/// NSString's "alloc". In all, an Objective-C message send can take 429/// on four different (although related) forms: 430/// 431/// 1. Send to an object instance. 432/// 2. Send to a class. 433/// 3. Send to the superclass instance of the current class. 434/// 4. Send to the superclass of the current class. 435/// 436/// All four kinds of message sends are modeled by the ObjCMessageExpr 437/// class, and can be distinguished via \c getReceiverKind(). Example: 438/// 439class ObjCMessageExpr : public Expr { 440 /// \brief The number of arguments in the message send, not 441 /// including the receiver. 442 unsigned NumArgs : 16; 443 444 /// \brief The kind of message send this is, which is one of the 445 /// ReceiverKind values. 446 /// 447 /// We pad this out to a byte to avoid excessive masking and shifting. 448 unsigned Kind : 8; 449 450 /// \brief Whether we have an actual method prototype in \c 451 /// SelectorOrMethod. 452 /// 453 /// When non-zero, we have a method declaration; otherwise, we just 454 /// have a selector. 455 unsigned HasMethod : 8; 456 457 /// \brief When the message expression is a send to 'super', this is 458 /// the location of the 'super' keyword. 459 SourceLocation SuperLoc; 460 461 /// \brief Stores either the selector that this message is sending 462 /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer 463 /// referring to the method that we type-checked against. 464 uintptr_t SelectorOrMethod; 465 466 /// \brief The source locations of the open and close square 467 /// brackets ('[' and ']', respectively). 468 SourceLocation LBracLoc, RBracLoc; 469 470 ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs) 471 : Expr(ObjCMessageExprClass, Empty), NumArgs(NumArgs), Kind(0), 472 HasMethod(0), SelectorOrMethod(0) { } 473 474 ObjCMessageExpr(QualType T, 475 SourceLocation LBracLoc, 476 SourceLocation SuperLoc, 477 bool IsInstanceSuper, 478 QualType SuperType, 479 Selector Sel, 480 ObjCMethodDecl *Method, 481 Expr **Args, unsigned NumArgs, 482 SourceLocation RBracLoc); 483 ObjCMessageExpr(QualType T, 484 SourceLocation LBracLoc, 485 TypeSourceInfo *Receiver, 486 Selector Sel, 487 ObjCMethodDecl *Method, 488 Expr **Args, unsigned NumArgs, 489 SourceLocation RBracLoc); 490 ObjCMessageExpr(QualType T, 491 SourceLocation LBracLoc, 492 Expr *Receiver, 493 Selector Sel, 494 ObjCMethodDecl *Method, 495 Expr **Args, unsigned NumArgs, 496 SourceLocation RBracLoc); 497 498 /// \brief Retrieve the pointer value of the message receiver. 499 void *getReceiverPointer() const { 500 return *const_cast<void **>( 501 reinterpret_cast<const void * const*>(this + 1)); 502 } 503 504 /// \brief Set the pointer value of the message receiver. 505 void setReceiverPointer(void *Value) { 506 *reinterpret_cast<void **>(this + 1) = Value; 507 } 508 509public: 510 /// \brief The kind of receiver this message is sending to. 511 enum ReceiverKind { 512 /// \brief The receiver is a class. 513 Class = 0, 514 /// \brief The receiver is an object instance. 515 Instance, 516 /// \brief The receiver is a superclass. 517 SuperClass, 518 /// \brief The receiver is the instance of the superclass object. 519 SuperInstance 520 }; 521 522 /// \brief Create a message send to super. 523 /// 524 /// \param Context The ASTContext in which this expression will be created. 525 /// 526 /// \param T The result type of this message. 527 /// 528 /// \param LBrac The location of the open square bracket '['. 529 /// 530 /// \param SuperLoc The location of the "super" keyword. 531 /// 532 /// \param IsInstanceSuper Whether this is an instance "super" 533 /// message (otherwise, it's a class "super" message). 534 /// 535 /// \param Sel The selector used to determine which method gets called. 536 /// 537 /// \param Method The Objective-C method against which this message 538 /// send was type-checked. May be NULL. 539 /// 540 /// \param Args The message send arguments. 541 /// 542 /// \param NumArgs The number of arguments. 543 /// 544 /// \param RBracLoc The location of the closing square bracket ']'. 545 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 546 SourceLocation LBracLoc, 547 SourceLocation SuperLoc, 548 bool IsInstanceSuper, 549 QualType SuperType, 550 Selector Sel, 551 ObjCMethodDecl *Method, 552 Expr **Args, unsigned NumArgs, 553 SourceLocation RBracLoc); 554 555 /// \brief Create a class message send. 556 /// 557 /// \param Context The ASTContext in which this expression will be created. 558 /// 559 /// \param T The result type of this message. 560 /// 561 /// \param LBrac The location of the open square bracket '['. 562 /// 563 /// \param Receiver The type of the receiver, including 564 /// source-location information. 565 /// 566 /// \param Sel The selector used to determine which method gets called. 567 /// 568 /// \param Method The Objective-C method against which this message 569 /// send was type-checked. May be NULL. 570 /// 571 /// \param Args The message send arguments. 572 /// 573 /// \param NumArgs The number of arguments. 574 /// 575 /// \param RBracLoc The location of the closing square bracket ']'. 576 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 577 SourceLocation LBracLoc, 578 TypeSourceInfo *Receiver, 579 Selector Sel, 580 ObjCMethodDecl *Method, 581 Expr **Args, unsigned NumArgs, 582 SourceLocation RBracLoc); 583 584 /// \brief Create an instance message send. 585 /// 586 /// \param Context The ASTContext in which this expression will be created. 587 /// 588 /// \param T The result type of this message. 589 /// 590 /// \param LBrac The location of the open square bracket '['. 591 /// 592 /// \param Receiver The expression used to produce the object that 593 /// will receive this message. 594 /// 595 /// \param Sel The selector used to determine which method gets called. 596 /// 597 /// \param Method The Objective-C method against which this message 598 /// send was type-checked. May be NULL. 599 /// 600 /// \param Args The message send arguments. 601 /// 602 /// \param NumArgs The number of arguments. 603 /// 604 /// \param RBracLoc The location of the closing square bracket ']'. 605 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 606 SourceLocation LBracLoc, 607 Expr *Receiver, 608 Selector Sel, 609 ObjCMethodDecl *Method, 610 Expr **Args, unsigned NumArgs, 611 SourceLocation RBracLoc); 612 613 /// \brief Create an empty Objective-C message expression, to be 614 /// filled in by subsequent calls. 615 /// 616 /// \param Context The context in which the message send will be created. 617 /// 618 /// \param NumArgs The number of message arguments, not including 619 /// the receiver. 620 static ObjCMessageExpr *CreateEmpty(ASTContext &Context, unsigned NumArgs); 621 622 /// \brief Determine the kind of receiver that this message is being 623 /// sent to. 624 ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; } 625 626 /// \brief Determine whether this is an instance message to either a 627 /// computed object or to super. 628 bool isInstanceMessage() const { 629 return getReceiverKind() == Instance || getReceiverKind() == SuperInstance; 630 } 631 632 /// \brief Determine whether this is an class message to either a 633 /// specified class or to super. 634 bool isClassMessage() const { 635 return getReceiverKind() == Class || getReceiverKind() == SuperClass; 636 } 637 638 /// \brief Returns the receiver of an instance message. 639 /// 640 /// \brief Returns the object expression for an instance message, or 641 /// NULL for a message that is not an instance message. 642 Expr *getInstanceReceiver() { 643 if (getReceiverKind() == Instance) 644 return static_cast<Expr *>(getReceiverPointer()); 645 646 return 0; 647 } 648 const Expr *getInstanceReceiver() const { 649 return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver(); 650 } 651 652 /// \brief Turn this message send into an instance message that 653 /// computes the receiver object with the given expression. 654 void setInstanceReceiver(Expr *rec) { 655 Kind = Instance; 656 setReceiverPointer(rec); 657 } 658 659 /// \brief Returns the type of a class message send, or NULL if the 660 /// message is not a class message. 661 QualType getClassReceiver() const { 662 if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo()) 663 return TSInfo->getType(); 664 665 return QualType(); 666 } 667 668 /// \brief Returns a type-source information of a class message 669 /// send, or NULL if the message is not a class message. 670 TypeSourceInfo *getClassReceiverTypeInfo() const { 671 if (getReceiverKind() == Class) 672 return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer()); 673 return 0; 674 } 675 676 void setClassReceiver(TypeSourceInfo *TSInfo) { 677 Kind = Class; 678 setReceiverPointer(TSInfo); 679 } 680 681 /// \brief Retrieve the location of the 'super' keyword for a class 682 /// or instance message to 'super', otherwise an invalid source location. 683 SourceLocation getSuperLoc() const { 684 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 685 return SuperLoc; 686 687 return SourceLocation(); 688 } 689 690 /// \brief Retrieve the Objective-C interface to which this message 691 /// is being directed, if known. 692 /// 693 /// This routine cross-cuts all of the different kinds of message 694 /// sends to determine what the underlying (statically known) type 695 /// of the receiver will be; use \c getReceiverKind() to determine 696 /// whether the message is a class or an instance method, whether it 697 /// is a send to super or not, etc. 698 /// 699 /// \returns The Objective-C interface if known, otherwise NULL. 700 ObjCInterfaceDecl *getReceiverInterface() const; 701 702 /// \brief Retrieve the type referred to by 'super'. 703 /// 704 /// The returned type will either be an ObjCInterfaceType (for an 705 /// class message to super) or an ObjCObjectPointerType that refers 706 /// to a class (for an instance message to super); 707 QualType getSuperType() const { 708 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 709 return QualType::getFromOpaquePtr(getReceiverPointer()); 710 711 return QualType(); 712 } 713 714 void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) { 715 Kind = IsInstanceSuper? SuperInstance : SuperClass; 716 SuperLoc = Loc; 717 setReceiverPointer(T.getAsOpaquePtr()); 718 } 719 720 Selector getSelector() const; 721 722 void setSelector(Selector S) { 723 HasMethod = false; 724 SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr()); 725 } 726 727 const ObjCMethodDecl *getMethodDecl() const { 728 if (HasMethod) 729 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod); 730 731 return 0; 732 } 733 734 ObjCMethodDecl *getMethodDecl() { 735 if (HasMethod) 736 return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod); 737 738 return 0; 739 } 740 741 void setMethodDecl(ObjCMethodDecl *MD) { 742 HasMethod = true; 743 SelectorOrMethod = reinterpret_cast<uintptr_t>(MD); 744 } 745 746 /// \brief Return the number of actual arguments in this message, 747 /// not counting the receiver. 748 unsigned getNumArgs() const { return NumArgs; } 749 750 /// \brief Retrieve the arguments to this message, not including the 751 /// receiver. 752 Stmt **getArgs() { 753 return reinterpret_cast<Stmt **>(this + 1) + 1; 754 } 755 const Stmt * const *getArgs() const { 756 return reinterpret_cast<const Stmt * const *>(this + 1) + 1; 757 } 758 759 /// getArg - Return the specified argument. 760 Expr *getArg(unsigned Arg) { 761 assert(Arg < NumArgs && "Arg access out of range!"); 762 return cast<Expr>(getArgs()[Arg]); 763 } 764 const Expr *getArg(unsigned Arg) const { 765 assert(Arg < NumArgs && "Arg access out of range!"); 766 return cast<Expr>(getArgs()[Arg]); 767 } 768 /// setArg - Set the specified argument. 769 void setArg(unsigned Arg, Expr *ArgExpr) { 770 assert(Arg < NumArgs && "Arg access out of range!"); 771 getArgs()[Arg] = ArgExpr; 772 } 773 774 SourceLocation getLeftLoc() const { return LBracLoc; } 775 SourceLocation getRightLoc() const { return RBracLoc; } 776 777 void setLeftLoc(SourceLocation L) { LBracLoc = L; } 778 void setRightLoc(SourceLocation L) { RBracLoc = L; } 779 780 void setSourceRange(SourceRange R) { 781 LBracLoc = R.getBegin(); 782 RBracLoc = R.getEnd(); 783 } 784 virtual SourceRange getSourceRange() const { 785 return SourceRange(LBracLoc, RBracLoc); 786 } 787 788 static bool classof(const Stmt *T) { 789 return T->getStmtClass() == ObjCMessageExprClass; 790 } 791 static bool classof(const ObjCMessageExpr *) { return true; } 792 793 // Iterators 794 virtual child_iterator child_begin(); 795 virtual child_iterator child_end(); 796 797 typedef ExprIterator arg_iterator; 798 typedef ConstExprIterator const_arg_iterator; 799 800 arg_iterator arg_begin() { return getArgs(); } 801 arg_iterator arg_end() { return getArgs() + NumArgs; } 802 const_arg_iterator arg_begin() const { return getArgs(); } 803 const_arg_iterator arg_end() const { return getArgs() + NumArgs; } 804}; 805 806/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type. 807/// (similiar in spirit to MemberExpr). 808class ObjCIsaExpr : public Expr { 809 /// Base - the expression for the base object pointer. 810 Stmt *Base; 811 812 /// IsaMemberLoc - This is the location of the 'isa'. 813 SourceLocation IsaMemberLoc; 814 815 /// IsArrow - True if this is "X->F", false if this is "X.F". 816 bool IsArrow; 817public: 818 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty) 819 : Expr(ObjCIsaExprClass, ty, /*TypeDependent=*/false, 820 base->isValueDependent()), 821 Base(base), IsaMemberLoc(l), IsArrow(isarrow) {} 822 823 /// \brief Build an empty expression. 824 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { } 825 826 void setBase(Expr *E) { Base = E; } 827 Expr *getBase() const { return cast<Expr>(Base); } 828 829 bool isArrow() const { return IsArrow; } 830 void setArrow(bool A) { IsArrow = A; } 831 832 /// getMemberLoc - Return the location of the "member", in X->F, it is the 833 /// location of 'F'. 834 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; } 835 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; } 836 837 virtual SourceRange getSourceRange() const { 838 return SourceRange(getBase()->getLocStart(), IsaMemberLoc); 839 } 840 841 virtual SourceLocation getExprLoc() const { return IsaMemberLoc; } 842 843 static bool classof(const Stmt *T) { 844 return T->getStmtClass() == ObjCIsaExprClass; 845 } 846 static bool classof(const ObjCIsaExpr *) { return true; } 847 848 // Iterators 849 virtual child_iterator child_begin(); 850 virtual child_iterator child_end(); 851}; 852 853} // end namespace clang 854 855#endif 856