ExprObjC.h revision 8d9ed7980405e91a12e33338a78fb99620adf553
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/Basic/IdentifierTable.h" 20 21namespace clang { 22 class IdentifierInfo; 23 class ASTContext; 24 25/// ObjCStringLiteral, used for Objective-C string literals 26/// i.e. @"foo". 27class ObjCStringLiteral : public Expr { 28 Stmt *String; 29 SourceLocation AtLoc; 30public: 31 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 32 : Expr(ObjCStringLiteralClass, T, VK_RValue, OK_Ordinary, false, false, 33 false, false), 34 String(SL), AtLoc(L) {} 35 explicit ObjCStringLiteral(EmptyShell Empty) 36 : Expr(ObjCStringLiteralClass, Empty) {} 37 38 StringLiteral *getString() { return cast<StringLiteral>(String); } 39 const StringLiteral *getString() const { return cast<StringLiteral>(String); } 40 void setString(StringLiteral *S) { String = S; } 41 42 SourceLocation getAtLoc() const { return AtLoc; } 43 void setAtLoc(SourceLocation L) { AtLoc = L; } 44 45 SourceRange getSourceRange() const { 46 return SourceRange(AtLoc, String->getLocEnd()); 47 } 48 49 static bool classof(const Stmt *T) { 50 return T->getStmtClass() == ObjCStringLiteralClass; 51 } 52 static bool classof(const ObjCStringLiteral *) { return true; } 53 54 // Iterators 55 child_range children() { return child_range(&String, &String+1); } 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, VK_LValue, OK_Ordinary, 68 EncodedType->getType()->isDependentType(), 69 EncodedType->getType()->isDependentType(), 70 EncodedType->getType()->isInstantiationDependentType(), 71 EncodedType->getType()->containsUnexpandedParameterPack()), 72 EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {} 73 74 explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){} 75 76 77 SourceLocation getAtLoc() const { return AtLoc; } 78 void setAtLoc(SourceLocation L) { AtLoc = L; } 79 SourceLocation getRParenLoc() const { return RParenLoc; } 80 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 81 82 QualType getEncodedType() const { return EncodedType->getType(); } 83 84 TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; } 85 void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) { 86 EncodedType = EncType; 87 } 88 89 SourceRange getSourceRange() const { 90 return SourceRange(AtLoc, RParenLoc); 91 } 92 93 static bool classof(const Stmt *T) { 94 return T->getStmtClass() == ObjCEncodeExprClass; 95 } 96 static bool classof(const ObjCEncodeExpr *) { return true; } 97 98 // Iterators 99 child_range children() { return child_range(); } 100}; 101 102/// ObjCSelectorExpr used for @selector in Objective-C. 103class ObjCSelectorExpr : public Expr { 104 Selector SelName; 105 SourceLocation AtLoc, RParenLoc; 106public: 107 ObjCSelectorExpr(QualType T, Selector selInfo, 108 SourceLocation at, SourceLocation rp) 109 : Expr(ObjCSelectorExprClass, T, VK_RValue, OK_Ordinary, false, false, 110 false, false), 111 SelName(selInfo), AtLoc(at), RParenLoc(rp){} 112 explicit ObjCSelectorExpr(EmptyShell Empty) 113 : Expr(ObjCSelectorExprClass, Empty) {} 114 115 Selector getSelector() const { return SelName; } 116 void setSelector(Selector S) { SelName = S; } 117 118 SourceLocation getAtLoc() const { return AtLoc; } 119 SourceLocation getRParenLoc() const { return RParenLoc; } 120 void setAtLoc(SourceLocation L) { AtLoc = L; } 121 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 122 123 SourceRange getSourceRange() const { 124 return SourceRange(AtLoc, RParenLoc); 125 } 126 127 /// getNumArgs - Return the number of actual arguments to this call. 128 unsigned getNumArgs() const { return SelName.getNumArgs(); } 129 130 static bool classof(const Stmt *T) { 131 return T->getStmtClass() == ObjCSelectorExprClass; 132 } 133 static bool classof(const ObjCSelectorExpr *) { return true; } 134 135 // Iterators 136 child_range children() { return child_range(); } 137}; 138 139/// ObjCProtocolExpr used for protocol expression in Objective-C. This is used 140/// as: @protocol(foo), as in: 141/// obj conformsToProtocol:@protocol(foo)] 142/// The return type is "Protocol*". 143class ObjCProtocolExpr : public Expr { 144 ObjCProtocolDecl *TheProtocol; 145 SourceLocation AtLoc, RParenLoc; 146public: 147 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol, 148 SourceLocation at, SourceLocation rp) 149 : Expr(ObjCProtocolExprClass, T, VK_RValue, OK_Ordinary, false, false, 150 false, false), 151 TheProtocol(protocol), AtLoc(at), RParenLoc(rp) {} 152 explicit ObjCProtocolExpr(EmptyShell Empty) 153 : Expr(ObjCProtocolExprClass, Empty) {} 154 155 ObjCProtocolDecl *getProtocol() const { return TheProtocol; } 156 void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; } 157 158 SourceLocation getAtLoc() const { return AtLoc; } 159 SourceLocation getRParenLoc() const { return RParenLoc; } 160 void setAtLoc(SourceLocation L) { AtLoc = L; } 161 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 162 163 SourceRange getSourceRange() const { 164 return SourceRange(AtLoc, RParenLoc); 165 } 166 167 static bool classof(const Stmt *T) { 168 return T->getStmtClass() == ObjCProtocolExprClass; 169 } 170 static bool classof(const ObjCProtocolExpr *) { return true; } 171 172 // Iterators 173 child_range children() { return child_range(); } 174}; 175 176/// ObjCIvarRefExpr - A reference to an ObjC instance variable. 177class ObjCIvarRefExpr : public Expr { 178 class ObjCIvarDecl *D; 179 SourceLocation Loc; 180 Stmt *Base; 181 bool IsArrow:1; // True if this is "X->F", false if this is "X.F". 182 bool IsFreeIvar:1; // True if ivar reference has no base (self assumed). 183 184public: 185 ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, 186 SourceLocation l, Expr *base, 187 bool arrow = false, bool freeIvar = false) : 188 Expr(ObjCIvarRefExprClass, t, VK_LValue, OK_Ordinary, 189 /*TypeDependent=*/false, base->isValueDependent(), 190 base->isInstantiationDependent(), 191 base->containsUnexpandedParameterPack()), 192 D(d), Loc(l), Base(base), IsArrow(arrow), IsFreeIvar(freeIvar) {} 193 194 explicit ObjCIvarRefExpr(EmptyShell Empty) 195 : Expr(ObjCIvarRefExprClass, Empty) {} 196 197 ObjCIvarDecl *getDecl() { return D; } 198 const ObjCIvarDecl *getDecl() const { return D; } 199 void setDecl(ObjCIvarDecl *d) { D = d; } 200 201 const Expr *getBase() const { return cast<Expr>(Base); } 202 Expr *getBase() { return cast<Expr>(Base); } 203 void setBase(Expr * base) { Base = base; } 204 205 bool isArrow() const { return IsArrow; } 206 bool isFreeIvar() const { return IsFreeIvar; } 207 void setIsArrow(bool A) { IsArrow = A; } 208 void setIsFreeIvar(bool A) { IsFreeIvar = A; } 209 210 SourceLocation getLocation() const { return Loc; } 211 void setLocation(SourceLocation L) { Loc = L; } 212 213 SourceRange getSourceRange() const { 214 return isFreeIvar() ? SourceRange(Loc) 215 : SourceRange(getBase()->getLocStart(), Loc); 216 } 217 218 static bool classof(const Stmt *T) { 219 return T->getStmtClass() == ObjCIvarRefExprClass; 220 } 221 static bool classof(const ObjCIvarRefExpr *) { return true; } 222 223 // Iterators 224 child_range children() { return child_range(&Base, &Base+1); } 225}; 226 227/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 228/// property. 229/// 230class ObjCPropertyRefExpr : public Expr { 231private: 232 /// If the bool is true, this is an implicit property reference; the 233 /// pointer is an (optional) ObjCMethodDecl and Setter may be set. 234 /// if the bool is false, this is an explicit property reference; 235 /// the pointer is an ObjCPropertyDecl and Setter is always null. 236 llvm::PointerIntPair<NamedDecl*, 1, bool> PropertyOrGetter; 237 ObjCMethodDecl *Setter; 238 239 SourceLocation IdLoc; 240 241 /// \brief When the receiver in property access is 'super', this is 242 /// the location of the 'super' keyword. When it's an interface, 243 /// this is that interface. 244 SourceLocation ReceiverLoc; 245 llvm::PointerUnion3<Stmt*, const Type*, ObjCInterfaceDecl*> Receiver; 246 247public: 248 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 249 ExprValueKind VK, ExprObjectKind OK, 250 SourceLocation l, Expr *base) 251 : Expr(ObjCPropertyRefExprClass, t, VK, OK, 252 /*TypeDependent=*/false, base->isValueDependent(), 253 base->isInstantiationDependent(), 254 base->containsUnexpandedParameterPack()), 255 PropertyOrGetter(PD, false), Setter(0), 256 IdLoc(l), ReceiverLoc(), Receiver(base) { 257 } 258 259 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 260 ExprValueKind VK, ExprObjectKind OK, 261 SourceLocation l, SourceLocation sl, QualType st) 262 : Expr(ObjCPropertyRefExprClass, t, VK, OK, 263 /*TypeDependent=*/false, false, st->isInstantiationDependentType(), 264 st->containsUnexpandedParameterPack()), 265 PropertyOrGetter(PD, false), Setter(0), 266 IdLoc(l), ReceiverLoc(sl), Receiver(st.getTypePtr()) { 267 } 268 269 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 270 QualType T, ExprValueKind VK, ExprObjectKind OK, 271 SourceLocation IdLoc, Expr *Base) 272 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, 273 Base->isValueDependent(), Base->isInstantiationDependent(), 274 Base->containsUnexpandedParameterPack()), 275 PropertyOrGetter(Getter, true), Setter(Setter), 276 IdLoc(IdLoc), ReceiverLoc(), Receiver(Base) { 277 } 278 279 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 280 QualType T, ExprValueKind VK, ExprObjectKind OK, 281 SourceLocation IdLoc, 282 SourceLocation SuperLoc, QualType SuperTy) 283 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false), 284 PropertyOrGetter(Getter, true), Setter(Setter), 285 IdLoc(IdLoc), ReceiverLoc(SuperLoc), Receiver(SuperTy.getTypePtr()) { 286 } 287 288 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter, 289 QualType T, ExprValueKind VK, ExprObjectKind OK, 290 SourceLocation IdLoc, 291 SourceLocation ReceiverLoc, ObjCInterfaceDecl *Receiver) 292 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false), 293 PropertyOrGetter(Getter, true), Setter(Setter), 294 IdLoc(IdLoc), ReceiverLoc(ReceiverLoc), Receiver(Receiver) { 295 } 296 297 explicit ObjCPropertyRefExpr(EmptyShell Empty) 298 : Expr(ObjCPropertyRefExprClass, Empty) {} 299 300 bool isImplicitProperty() const { return PropertyOrGetter.getInt(); } 301 bool isExplicitProperty() const { return !PropertyOrGetter.getInt(); } 302 303 ObjCPropertyDecl *getExplicitProperty() const { 304 assert(!isImplicitProperty()); 305 return cast<ObjCPropertyDecl>(PropertyOrGetter.getPointer()); 306 } 307 308 ObjCMethodDecl *getImplicitPropertyGetter() const { 309 assert(isImplicitProperty()); 310 return cast_or_null<ObjCMethodDecl>(PropertyOrGetter.getPointer()); 311 } 312 313 ObjCMethodDecl *getImplicitPropertySetter() const { 314 assert(isImplicitProperty()); 315 return Setter; 316 } 317 318 Selector getGetterSelector() const { 319 if (isImplicitProperty()) 320 return getImplicitPropertyGetter()->getSelector(); 321 return getExplicitProperty()->getGetterName(); 322 } 323 324 Selector getSetterSelector() const { 325 if (isImplicitProperty()) 326 return getImplicitPropertySetter()->getSelector(); 327 return getExplicitProperty()->getSetterName(); 328 } 329 330 const Expr *getBase() const { 331 return cast<Expr>(Receiver.get<Stmt*>()); 332 } 333 Expr *getBase() { 334 return cast<Expr>(Receiver.get<Stmt*>()); 335 } 336 337 SourceLocation getLocation() const { return IdLoc; } 338 339 SourceLocation getReceiverLocation() const { return ReceiverLoc; } 340 QualType getSuperReceiverType() const { 341 return QualType(Receiver.get<const Type*>(), 0); 342 } 343 QualType getGetterResultType() const { 344 QualType ResultType; 345 if (isExplicitProperty()) { 346 const ObjCPropertyDecl *PDecl = getExplicitProperty(); 347 if (const ObjCMethodDecl *Getter = PDecl->getGetterMethodDecl()) 348 ResultType = Getter->getResultType(); 349 else 350 ResultType = getType(); 351 } else { 352 const ObjCMethodDecl *Getter = getImplicitPropertyGetter(); 353 ResultType = Getter->getResultType(); // with reference! 354 } 355 return ResultType; 356 } 357 358 QualType getSetterArgType() const { 359 QualType ArgType; 360 if (isImplicitProperty()) { 361 const ObjCMethodDecl *Setter = getImplicitPropertySetter(); 362 ObjCMethodDecl::param_iterator P = Setter->param_begin(); 363 ArgType = (*P)->getType(); 364 } else { 365 if (ObjCPropertyDecl *PDecl = getExplicitProperty()) 366 if (const ObjCMethodDecl *Setter = PDecl->getSetterMethodDecl()) { 367 ObjCMethodDecl::param_iterator P = Setter->param_begin(); 368 ArgType = (*P)->getType(); 369 } 370 if (ArgType.isNull()) 371 ArgType = getType(); 372 } 373 return ArgType; 374 } 375 376 ObjCInterfaceDecl *getClassReceiver() const { 377 return Receiver.get<ObjCInterfaceDecl*>(); 378 } 379 bool isObjectReceiver() const { return Receiver.is<Stmt*>(); } 380 bool isSuperReceiver() const { return Receiver.is<const Type*>(); } 381 bool isClassReceiver() const { return Receiver.is<ObjCInterfaceDecl*>(); } 382 383 SourceRange getSourceRange() const { 384 return SourceRange((isObjectReceiver() ? getBase()->getLocStart() 385 : getReceiverLocation()), 386 IdLoc); 387 } 388 389 static bool classof(const Stmt *T) { 390 return T->getStmtClass() == ObjCPropertyRefExprClass; 391 } 392 static bool classof(const ObjCPropertyRefExpr *) { return true; } 393 394 // Iterators 395 child_range children() { 396 if (Receiver.is<Stmt*>()) { 397 Stmt **begin = reinterpret_cast<Stmt**>(&Receiver); // hack! 398 return child_range(begin, begin+1); 399 } 400 return child_range(); 401 } 402 403private: 404 friend class ASTStmtReader; 405 void setExplicitProperty(ObjCPropertyDecl *D) { 406 PropertyOrGetter.setPointer(D); 407 PropertyOrGetter.setInt(false); 408 Setter = 0; 409 } 410 void setImplicitProperty(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter) { 411 PropertyOrGetter.setPointer(Getter); 412 PropertyOrGetter.setInt(true); 413 this->Setter = Setter; 414 } 415 void setBase(Expr *Base) { Receiver = Base; } 416 void setSuperReceiver(QualType T) { Receiver = T.getTypePtr(); } 417 void setClassReceiver(ObjCInterfaceDecl *D) { Receiver = D; } 418 419 void setLocation(SourceLocation L) { IdLoc = L; } 420 void setReceiverLocation(SourceLocation Loc) { ReceiverLoc = Loc; } 421}; 422 423/// \brief An expression that sends a message to the given Objective-C 424/// object or class. 425/// 426/// The following contains two message send expressions: 427/// 428/// \code 429/// [[NSString alloc] initWithString:@"Hello"] 430/// \endcode 431/// 432/// The innermost message send invokes the "alloc" class method on the 433/// NSString class, while the outermost message send invokes the 434/// "initWithString" instance method on the object returned from 435/// NSString's "alloc". In all, an Objective-C message send can take 436/// on four different (although related) forms: 437/// 438/// 1. Send to an object instance. 439/// 2. Send to a class. 440/// 3. Send to the superclass instance of the current class. 441/// 4. Send to the superclass of the current class. 442/// 443/// All four kinds of message sends are modeled by the ObjCMessageExpr 444/// class, and can be distinguished via \c getReceiverKind(). Example: 445/// 446class ObjCMessageExpr : public Expr { 447 enum { NumArgsBitWidth = 16 }; 448 449 /// \brief The number of arguments in the message send, not 450 /// including the receiver. 451 unsigned NumArgs : NumArgsBitWidth; 452 453 void setNumArgs(unsigned Num) { 454 assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!"); 455 NumArgs = Num; 456 } 457 458 /// \brief The kind of message send this is, which is one of the 459 /// ReceiverKind values. 460 /// 461 /// We pad this out to a byte to avoid excessive masking and shifting. 462 unsigned Kind : 8; 463 464 /// \brief Whether we have an actual method prototype in \c 465 /// SelectorOrMethod. 466 /// 467 /// When non-zero, we have a method declaration; otherwise, we just 468 /// have a selector. 469 unsigned HasMethod : 1; 470 471 /// \brief Whether this message send is a "delegate init call", 472 /// i.e. a call of an init method on self from within an init method. 473 unsigned IsDelegateInitCall : 1; 474 475 /// \brief When the message expression is a send to 'super', this is 476 /// the location of the 'super' keyword. 477 SourceLocation SuperLoc; 478 479 /// \brief Stores either the selector that this message is sending 480 /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer 481 /// referring to the method that we type-checked against. 482 uintptr_t SelectorOrMethod; 483 484 /// \brief Location of the selector. 485 SourceLocation SelectorLoc; 486 487 /// \brief The source locations of the open and close square 488 /// brackets ('[' and ']', respectively). 489 SourceLocation LBracLoc, RBracLoc; 490 491 ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs) 492 : Expr(ObjCMessageExprClass, Empty), Kind(0), 493 HasMethod(0), IsDelegateInitCall(0), SelectorOrMethod(0) { 494 setNumArgs(NumArgs); 495 } 496 497 ObjCMessageExpr(QualType T, ExprValueKind VK, 498 SourceLocation LBracLoc, 499 SourceLocation SuperLoc, 500 bool IsInstanceSuper, 501 QualType SuperType, 502 Selector Sel, 503 SourceLocation SelLoc, 504 ObjCMethodDecl *Method, 505 ArrayRef<Expr *> Args, 506 SourceLocation RBracLoc); 507 ObjCMessageExpr(QualType T, ExprValueKind VK, 508 SourceLocation LBracLoc, 509 TypeSourceInfo *Receiver, 510 Selector Sel, 511 SourceLocation SelLoc, 512 ObjCMethodDecl *Method, 513 ArrayRef<Expr *> Args, 514 SourceLocation RBracLoc); 515 ObjCMessageExpr(QualType T, ExprValueKind VK, 516 SourceLocation LBracLoc, 517 Expr *Receiver, 518 Selector Sel, 519 SourceLocation SelLoc, 520 ObjCMethodDecl *Method, 521 ArrayRef<Expr *> Args, 522 SourceLocation RBracLoc); 523 524 /// \brief Retrieve the pointer value of the message receiver. 525 void *getReceiverPointer() const { 526 return *const_cast<void **>( 527 reinterpret_cast<const void * const*>(this + 1)); 528 } 529 530 /// \brief Set the pointer value of the message receiver. 531 void setReceiverPointer(void *Value) { 532 *reinterpret_cast<void **>(this + 1) = Value; 533 } 534 535public: 536 /// \brief The kind of receiver this message is sending to. 537 enum ReceiverKind { 538 /// \brief The receiver is a class. 539 Class = 0, 540 /// \brief The receiver is an object instance. 541 Instance, 542 /// \brief The receiver is a superclass. 543 SuperClass, 544 /// \brief The receiver is the instance of the superclass object. 545 SuperInstance 546 }; 547 548 /// \brief Create a message send to super. 549 /// 550 /// \param Context The ASTContext in which this expression will be created. 551 /// 552 /// \param T The result type of this message. 553 /// 554 /// \param VK The value kind of this message. A message returning 555 /// a l-value or r-value reference will be an l-value or x-value, 556 /// respectively. 557 /// 558 /// \param LBrac The location of the open square bracket '['. 559 /// 560 /// \param SuperLoc The location of the "super" keyword. 561 /// 562 /// \param IsInstanceSuper Whether this is an instance "super" 563 /// message (otherwise, it's a class "super" message). 564 /// 565 /// \param Sel The selector used to determine which method gets called. 566 /// 567 /// \param Method The Objective-C method against which this message 568 /// send was type-checked. May be NULL. 569 /// 570 /// \param Args The message send arguments. 571 /// 572 /// \param NumArgs The number of arguments. 573 /// 574 /// \param RBracLoc The location of the closing square bracket ']'. 575 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 576 ExprValueKind VK, 577 SourceLocation LBracLoc, 578 SourceLocation SuperLoc, 579 bool IsInstanceSuper, 580 QualType SuperType, 581 Selector Sel, 582 ArrayRef<SourceLocation> SelLocs, 583 ObjCMethodDecl *Method, 584 ArrayRef<Expr *> Args, 585 SourceLocation RBracLoc); 586 587 /// \brief Create a class message send. 588 /// 589 /// \param Context The ASTContext in which this expression will be created. 590 /// 591 /// \param T The result type of this message. 592 /// 593 /// \param VK The value kind of this message. A message returning 594 /// a l-value or r-value reference will be an l-value or x-value, 595 /// respectively. 596 /// 597 /// \param LBrac The location of the open square bracket '['. 598 /// 599 /// \param Receiver The type of the receiver, including 600 /// source-location information. 601 /// 602 /// \param Sel The selector used to determine which method gets called. 603 /// 604 /// \param Method The Objective-C method against which this message 605 /// send was type-checked. May be NULL. 606 /// 607 /// \param Args The message send arguments. 608 /// 609 /// \param NumArgs The number of arguments. 610 /// 611 /// \param RBracLoc The location of the closing square bracket ']'. 612 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 613 ExprValueKind VK, 614 SourceLocation LBracLoc, 615 TypeSourceInfo *Receiver, 616 Selector Sel, 617 ArrayRef<SourceLocation> SelLocs, 618 ObjCMethodDecl *Method, 619 ArrayRef<Expr *> Args, 620 SourceLocation RBracLoc); 621 622 /// \brief Create an instance message send. 623 /// 624 /// \param Context The ASTContext in which this expression will be created. 625 /// 626 /// \param T The result type of this message. 627 /// 628 /// \param VK The value kind of this message. A message returning 629 /// a l-value or r-value reference will be an l-value or x-value, 630 /// respectively. 631 /// 632 /// \param LBrac The location of the open square bracket '['. 633 /// 634 /// \param Receiver The expression used to produce the object that 635 /// will receive this message. 636 /// 637 /// \param Sel The selector used to determine which method gets called. 638 /// 639 /// \param Method The Objective-C method against which this message 640 /// send was type-checked. May be NULL. 641 /// 642 /// \param Args The message send arguments. 643 /// 644 /// \param NumArgs The number of arguments. 645 /// 646 /// \param RBracLoc The location of the closing square bracket ']'. 647 static ObjCMessageExpr *Create(ASTContext &Context, QualType T, 648 ExprValueKind VK, 649 SourceLocation LBracLoc, 650 Expr *Receiver, 651 Selector Sel, 652 ArrayRef<SourceLocation> SeLocs, 653 ObjCMethodDecl *Method, 654 ArrayRef<Expr *> Args, 655 SourceLocation RBracLoc); 656 657 /// \brief Create an empty Objective-C message expression, to be 658 /// filled in by subsequent calls. 659 /// 660 /// \param Context The context in which the message send will be created. 661 /// 662 /// \param NumArgs The number of message arguments, not including 663 /// the receiver. 664 static ObjCMessageExpr *CreateEmpty(ASTContext &Context, unsigned NumArgs); 665 666 /// \brief Determine the kind of receiver that this message is being 667 /// sent to. 668 ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; } 669 670 /// \brief Source range of the receiver. 671 SourceRange getReceiverRange() const; 672 673 /// \brief Determine whether this is an instance message to either a 674 /// computed object or to super. 675 bool isInstanceMessage() const { 676 return getReceiverKind() == Instance || getReceiverKind() == SuperInstance; 677 } 678 679 /// \brief Determine whether this is an class message to either a 680 /// specified class or to super. 681 bool isClassMessage() const { 682 return getReceiverKind() == Class || getReceiverKind() == SuperClass; 683 } 684 685 /// \brief Returns the receiver of an instance message. 686 /// 687 /// \brief Returns the object expression for an instance message, or 688 /// NULL for a message that is not an instance message. 689 Expr *getInstanceReceiver() { 690 if (getReceiverKind() == Instance) 691 return static_cast<Expr *>(getReceiverPointer()); 692 693 return 0; 694 } 695 const Expr *getInstanceReceiver() const { 696 return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver(); 697 } 698 699 /// \brief Turn this message send into an instance message that 700 /// computes the receiver object with the given expression. 701 void setInstanceReceiver(Expr *rec) { 702 Kind = Instance; 703 setReceiverPointer(rec); 704 } 705 706 /// \brief Returns the type of a class message send, or NULL if the 707 /// message is not a class message. 708 QualType getClassReceiver() const { 709 if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo()) 710 return TSInfo->getType(); 711 712 return QualType(); 713 } 714 715 /// \brief Returns a type-source information of a class message 716 /// send, or NULL if the message is not a class message. 717 TypeSourceInfo *getClassReceiverTypeInfo() const { 718 if (getReceiverKind() == Class) 719 return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer()); 720 return 0; 721 } 722 723 void setClassReceiver(TypeSourceInfo *TSInfo) { 724 Kind = Class; 725 setReceiverPointer(TSInfo); 726 } 727 728 /// \brief Retrieve the location of the 'super' keyword for a class 729 /// or instance message to 'super', otherwise an invalid source location. 730 SourceLocation getSuperLoc() const { 731 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 732 return SuperLoc; 733 734 return SourceLocation(); 735 } 736 737 /// \brief Retrieve the Objective-C interface to which this message 738 /// is being directed, if known. 739 /// 740 /// This routine cross-cuts all of the different kinds of message 741 /// sends to determine what the underlying (statically known) type 742 /// of the receiver will be; use \c getReceiverKind() to determine 743 /// whether the message is a class or an instance method, whether it 744 /// is a send to super or not, etc. 745 /// 746 /// \returns The Objective-C interface if known, otherwise NULL. 747 ObjCInterfaceDecl *getReceiverInterface() const; 748 749 /// \brief Retrieve the type referred to by 'super'. 750 /// 751 /// The returned type will either be an ObjCInterfaceType (for an 752 /// class message to super) or an ObjCObjectPointerType that refers 753 /// to a class (for an instance message to super); 754 QualType getSuperType() const { 755 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass) 756 return QualType::getFromOpaquePtr(getReceiverPointer()); 757 758 return QualType(); 759 } 760 761 void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) { 762 Kind = IsInstanceSuper? SuperInstance : SuperClass; 763 SuperLoc = Loc; 764 setReceiverPointer(T.getAsOpaquePtr()); 765 } 766 767 Selector getSelector() const; 768 769 void setSelector(Selector S) { 770 HasMethod = false; 771 SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr()); 772 } 773 774 const ObjCMethodDecl *getMethodDecl() const { 775 if (HasMethod) 776 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod); 777 778 return 0; 779 } 780 781 ObjCMethodDecl *getMethodDecl() { 782 if (HasMethod) 783 return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod); 784 785 return 0; 786 } 787 788 void setMethodDecl(ObjCMethodDecl *MD) { 789 HasMethod = true; 790 SelectorOrMethod = reinterpret_cast<uintptr_t>(MD); 791 } 792 793 ObjCMethodFamily getMethodFamily() const { 794 if (HasMethod) return getMethodDecl()->getMethodFamily(); 795 return getSelector().getMethodFamily(); 796 } 797 798 /// \brief Return the number of actual arguments in this message, 799 /// not counting the receiver. 800 unsigned getNumArgs() const { return NumArgs; } 801 802 /// \brief Retrieve the arguments to this message, not including the 803 /// receiver. 804 Expr **getArgs() { 805 return reinterpret_cast<Expr **>(this + 1) + 1; 806 } 807 const Expr * const *getArgs() const { 808 return reinterpret_cast<const Expr * const *>(this + 1) + 1; 809 } 810 811 /// getArg - Return the specified argument. 812 Expr *getArg(unsigned Arg) { 813 assert(Arg < NumArgs && "Arg access out of range!"); 814 return cast<Expr>(getArgs()[Arg]); 815 } 816 const Expr *getArg(unsigned Arg) const { 817 assert(Arg < NumArgs && "Arg access out of range!"); 818 return cast<Expr>(getArgs()[Arg]); 819 } 820 /// setArg - Set the specified argument. 821 void setArg(unsigned Arg, Expr *ArgExpr) { 822 assert(Arg < NumArgs && "Arg access out of range!"); 823 getArgs()[Arg] = ArgExpr; 824 } 825 826 /// isDelegateInitCall - Answers whether this message send has been 827 /// tagged as a "delegate init call", i.e. a call to a method in the 828 /// -init family on self from within an -init method implementation. 829 bool isDelegateInitCall() const { return IsDelegateInitCall; } 830 void setDelegateInitCall(bool isDelegate) { IsDelegateInitCall = isDelegate; } 831 832 SourceLocation getLeftLoc() const { return LBracLoc; } 833 SourceLocation getRightLoc() const { return RBracLoc; } 834 SourceLocation getSelectorLoc() const { return SelectorLoc; } 835 836 void setSourceRange(SourceRange R) { 837 LBracLoc = R.getBegin(); 838 RBracLoc = R.getEnd(); 839 } 840 SourceRange getSourceRange() const { 841 return SourceRange(LBracLoc, RBracLoc); 842 } 843 844 static bool classof(const Stmt *T) { 845 return T->getStmtClass() == ObjCMessageExprClass; 846 } 847 static bool classof(const ObjCMessageExpr *) { return true; } 848 849 // Iterators 850 child_range children(); 851 852 typedef ExprIterator arg_iterator; 853 typedef ConstExprIterator const_arg_iterator; 854 855 arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); } 856 arg_iterator arg_end() { 857 return reinterpret_cast<Stmt **>(getArgs() + NumArgs); 858 } 859 const_arg_iterator arg_begin() const { 860 return reinterpret_cast<Stmt const * const*>(getArgs()); 861 } 862 const_arg_iterator arg_end() const { 863 return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs); 864 } 865 866 friend class ASTStmtReader; 867 friend class ASTStmtWriter; 868}; 869 870/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type. 871/// (similar in spirit to MemberExpr). 872class ObjCIsaExpr : public Expr { 873 /// Base - the expression for the base object pointer. 874 Stmt *Base; 875 876 /// IsaMemberLoc - This is the location of the 'isa'. 877 SourceLocation IsaMemberLoc; 878 879 /// IsArrow - True if this is "X->F", false if this is "X.F". 880 bool IsArrow; 881public: 882 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty) 883 : Expr(ObjCIsaExprClass, ty, VK_LValue, OK_Ordinary, 884 /*TypeDependent=*/false, base->isValueDependent(), 885 base->isInstantiationDependent(), 886 /*ContainsUnexpandedParameterPack=*/false), 887 Base(base), IsaMemberLoc(l), IsArrow(isarrow) {} 888 889 /// \brief Build an empty expression. 890 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { } 891 892 void setBase(Expr *E) { Base = E; } 893 Expr *getBase() const { return cast<Expr>(Base); } 894 895 bool isArrow() const { return IsArrow; } 896 void setArrow(bool A) { IsArrow = A; } 897 898 /// getMemberLoc - Return the location of the "member", in X->F, it is the 899 /// location of 'F'. 900 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; } 901 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; } 902 903 SourceRange getSourceRange() const { 904 return SourceRange(getBase()->getLocStart(), IsaMemberLoc); 905 } 906 907 SourceLocation getExprLoc() const { return IsaMemberLoc; } 908 909 static bool classof(const Stmt *T) { 910 return T->getStmtClass() == ObjCIsaExprClass; 911 } 912 static bool classof(const ObjCIsaExpr *) { return true; } 913 914 // Iterators 915 child_range children() { return child_range(&Base, &Base+1); } 916}; 917 918 919/// ObjCIndirectCopyRestoreExpr - Represents the passing of a function 920/// argument by indirect copy-restore in ARC. This is used to support 921/// passing indirect arguments with the wrong lifetime, e.g. when 922/// passing the address of a __strong local variable to an 'out' 923/// parameter. This expression kind is only valid in an "argument" 924/// position to some sort of call expression. 925/// 926/// The parameter must have type 'pointer to T', and the argument must 927/// have type 'pointer to U', where T and U agree except possibly in 928/// qualification. If the argument value is null, then a null pointer 929/// is passed; otherwise it points to an object A, and: 930/// 1. A temporary object B of type T is initialized, either by 931/// zero-initialization (used when initializing an 'out' parameter) 932/// or copy-initialization (used when initializing an 'inout' 933/// parameter). 934/// 2. The address of the temporary is passed to the function. 935/// 3. If the call completes normally, A is move-assigned from B. 936/// 4. Finally, A is destroyed immediately. 937/// 938/// Currently 'T' must be a retainable object lifetime and must be 939/// __autoreleasing; this qualifier is ignored when initializing 940/// the value. 941class ObjCIndirectCopyRestoreExpr : public Expr { 942 Stmt *Operand; 943 944 // unsigned ObjCIndirectCopyRestoreBits.ShouldCopy : 1; 945 946 friend class ASTReader; 947 friend class ASTStmtReader; 948 949 void setShouldCopy(bool shouldCopy) { 950 ObjCIndirectCopyRestoreExprBits.ShouldCopy = shouldCopy; 951 } 952 953 explicit ObjCIndirectCopyRestoreExpr(EmptyShell Empty) 954 : Expr(ObjCIndirectCopyRestoreExprClass, Empty) { } 955 956public: 957 ObjCIndirectCopyRestoreExpr(Expr *operand, QualType type, bool shouldCopy) 958 : Expr(ObjCIndirectCopyRestoreExprClass, type, VK_LValue, OK_Ordinary, 959 operand->isTypeDependent(), operand->isValueDependent(), 960 operand->isInstantiationDependent(), 961 operand->containsUnexpandedParameterPack()), 962 Operand(operand) { 963 setShouldCopy(shouldCopy); 964 } 965 966 Expr *getSubExpr() { return cast<Expr>(Operand); } 967 const Expr *getSubExpr() const { return cast<Expr>(Operand); } 968 969 /// shouldCopy - True if we should do the 'copy' part of the 970 /// copy-restore. If false, the temporary will be zero-initialized. 971 bool shouldCopy() const { return ObjCIndirectCopyRestoreExprBits.ShouldCopy; } 972 973 child_range children() { return child_range(&Operand, &Operand+1); } 974 975 // Source locations are determined by the subexpression. 976 SourceRange getSourceRange() const { return Operand->getSourceRange(); } 977 SourceLocation getExprLoc() const { return getSubExpr()->getExprLoc(); } 978 979 static bool classof(const Stmt *s) { 980 return s->getStmtClass() == ObjCIndirectCopyRestoreExprClass; 981 } 982 static bool classof(const ObjCIndirectCopyRestoreExpr *) { return true; } 983}; 984 985/// \brief An Objective-C "bridged" cast expression, which casts between 986/// Objective-C pointers and C pointers, transferring ownership in the process. 987/// 988/// \code 989/// NSString *str = (__bridge_transfer NSString *)CFCreateString(); 990/// \endcode 991class ObjCBridgedCastExpr : public ExplicitCastExpr { 992 SourceLocation LParenLoc; 993 SourceLocation BridgeKeywordLoc; 994 unsigned Kind : 2; 995 996 friend class ASTStmtReader; 997 friend class ASTStmtWriter; 998 999public: 1000 ObjCBridgedCastExpr(SourceLocation LParenLoc, ObjCBridgeCastKind Kind, 1001 CastKind CK, SourceLocation BridgeKeywordLoc, 1002 TypeSourceInfo *TSInfo, Expr *Operand) 1003 : ExplicitCastExpr(ObjCBridgedCastExprClass, TSInfo->getType(), VK_RValue, 1004 CK, Operand, 0, TSInfo), 1005 LParenLoc(LParenLoc), BridgeKeywordLoc(BridgeKeywordLoc), Kind(Kind) { } 1006 1007 /// \brief Construct an empty Objective-C bridged cast. 1008 explicit ObjCBridgedCastExpr(EmptyShell Shell) 1009 : ExplicitCastExpr(ObjCBridgedCastExprClass, Shell, 0) { } 1010 1011 SourceLocation getLParenLoc() const { return LParenLoc; } 1012 1013 /// \brief Determine which kind of bridge is being performed via this cast. 1014 ObjCBridgeCastKind getBridgeKind() const { 1015 return static_cast<ObjCBridgeCastKind>(Kind); 1016 } 1017 1018 /// \brief Retrieve the kind of bridge being performed as a string. 1019 StringRef getBridgeKindName() const; 1020 1021 /// \brief The location of the bridge keyword. 1022 SourceLocation getBridgeKeywordLoc() const { return BridgeKeywordLoc; } 1023 1024 SourceRange getSourceRange() const { 1025 return SourceRange(LParenLoc, getSubExpr()->getLocEnd()); 1026 } 1027 1028 static bool classof(const Stmt *T) { 1029 return T->getStmtClass() == ObjCBridgedCastExprClass; 1030 } 1031 static bool classof(const ObjCBridgedCastExpr *) { return true; } 1032 1033}; 1034 1035} // end namespace clang 1036 1037#endif 1038