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