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