ExprObjC.h revision 2333f7727f97018d6742e1e0938133bcfad967ab
1//===--- ExprObjC.h - Classes for representing ObjC expressions -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the ExprObjC interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_EXPROBJC_H 15#define LLVM_CLANG_AST_EXPROBJC_H 16 17#include "clang/AST/Expr.h" 18#include "clang/Basic/IdentifierTable.h" 19 20namespace clang { 21 class IdentifierInfo; 22 class ASTContext; 23 class ObjCMethodDecl; 24 class ObjCPropertyDecl; 25 26/// ObjCStringLiteral, used for Objective-C string literals 27/// i.e. @"foo". 28class ObjCStringLiteral : public Expr { 29 Stmt *String; 30 SourceLocation AtLoc; 31public: 32 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 33 : Expr(ObjCStringLiteralClass, T, false, false), String(SL), AtLoc(L) {} 34 explicit ObjCStringLiteral(EmptyShell Empty) 35 : Expr(ObjCStringLiteralClass, Empty) {} 36 37 StringLiteral *getString() { return cast<StringLiteral>(String); } 38 const StringLiteral *getString() const { return cast<StringLiteral>(String); } 39 void setString(StringLiteral *S) { String = S; } 40 41 SourceLocation getAtLoc() const { return AtLoc; } 42 void setAtLoc(SourceLocation L) { AtLoc = L; } 43 44 virtual SourceRange getSourceRange() const { 45 return SourceRange(AtLoc, String->getLocEnd()); 46 } 47 48 static bool classof(const Stmt *T) { 49 return T->getStmtClass() == ObjCStringLiteralClass; 50 } 51 static bool classof(const ObjCStringLiteral *) { return true; } 52 53 // Iterators 54 virtual child_iterator child_begin(); 55 virtual child_iterator child_end(); 56}; 57 58/// ObjCEncodeExpr, used for @encode in Objective-C. @encode has the same type 59/// and behavior as StringLiteral except that the string initializer is obtained 60/// from ASTContext with the encoding type as an argument. 61class ObjCEncodeExpr : public Expr { 62 QualType EncType; 63 SourceLocation AtLoc, RParenLoc; 64public: 65 ObjCEncodeExpr(QualType T, QualType ET, 66 SourceLocation at, SourceLocation rp) 67 : Expr(ObjCEncodeExprClass, T, ET->isDependentType(), 68 ET->isDependentType()), EncType(ET), AtLoc(at), RParenLoc(rp) {} 69 70 explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){} 71 72 73 SourceLocation getAtLoc() const { return AtLoc; } 74 void setAtLoc(SourceLocation L) { AtLoc = L; } 75 SourceLocation getRParenLoc() const { return RParenLoc; } 76 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 77 78 QualType getEncodedType() const { return EncType; } 79 void setEncodedType(QualType T) { EncType = T; } 80 81 82 virtual SourceRange getSourceRange() const { 83 return SourceRange(AtLoc, RParenLoc); 84 } 85 86 static bool classof(const Stmt *T) { 87 return T->getStmtClass() == ObjCEncodeExprClass; 88 } 89 static bool classof(const ObjCEncodeExpr *) { return true; } 90 91 // Iterators 92 virtual child_iterator child_begin(); 93 virtual child_iterator child_end(); 94}; 95 96/// ObjCSelectorExpr used for @selector in Objective-C. 97class ObjCSelectorExpr : public Expr { 98 Selector SelName; 99 SourceLocation AtLoc, RParenLoc; 100public: 101 ObjCSelectorExpr(QualType T, Selector selInfo, 102 SourceLocation at, SourceLocation rp) 103 : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), AtLoc(at), 104 RParenLoc(rp){} 105 explicit ObjCSelectorExpr(EmptyShell Empty) 106 : Expr(ObjCSelectorExprClass, Empty) {} 107 108 Selector getSelector() const { return SelName; } 109 void setSelector(Selector S) { SelName = S; } 110 111 SourceLocation getAtLoc() const { return AtLoc; } 112 SourceLocation getRParenLoc() const { return RParenLoc; } 113 void setAtLoc(SourceLocation L) { AtLoc = L; } 114 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 115 116 virtual SourceRange getSourceRange() const { 117 return SourceRange(AtLoc, RParenLoc); 118 } 119 120 /// getNumArgs - Return the number of actual arguments to this call. 121 unsigned getNumArgs() const { return SelName.getNumArgs(); } 122 123 static bool classof(const Stmt *T) { 124 return T->getStmtClass() == ObjCSelectorExprClass; 125 } 126 static bool classof(const ObjCSelectorExpr *) { return true; } 127 128 // Iterators 129 virtual child_iterator child_begin(); 130 virtual child_iterator child_end(); 131}; 132 133/// ObjCProtocolExpr used for protocol expression in Objective-C. This is used 134/// as: @protocol(foo), as in: 135/// obj conformsToProtocol:@protocol(foo)] 136/// The return type is "Protocol*". 137class ObjCProtocolExpr : public Expr { 138 ObjCProtocolDecl *TheProtocol; 139 SourceLocation AtLoc, RParenLoc; 140public: 141 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol, 142 SourceLocation at, SourceLocation rp) 143 : Expr(ObjCProtocolExprClass, T, false, false), TheProtocol(protocol), 144 AtLoc(at), RParenLoc(rp) {} 145 explicit ObjCProtocolExpr(EmptyShell Empty) 146 : Expr(ObjCProtocolExprClass, Empty) {} 147 148 ObjCProtocolDecl *getProtocol() const { return TheProtocol; } 149 void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; } 150 151 SourceLocation getAtLoc() const { return AtLoc; } 152 SourceLocation getRParenLoc() const { return RParenLoc; } 153 void setAtLoc(SourceLocation L) { AtLoc = L; } 154 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 155 156 virtual SourceRange getSourceRange() const { 157 return SourceRange(AtLoc, RParenLoc); 158 } 159 160 static bool classof(const Stmt *T) { 161 return T->getStmtClass() == ObjCProtocolExprClass; 162 } 163 static bool classof(const ObjCProtocolExpr *) { return true; } 164 165 // Iterators 166 virtual child_iterator child_begin(); 167 virtual child_iterator child_end(); 168}; 169 170/// ObjCIvarRefExpr - A reference to an ObjC instance variable. 171class ObjCIvarRefExpr : public Expr { 172 class ObjCIvarDecl *D; 173 SourceLocation Loc; 174 Stmt *Base; 175 bool IsArrow:1; // True if this is "X->F", false if this is "X.F". 176 bool IsFreeIvar:1; // True if ivar reference has no base (self assumed). 177 178public: 179 ObjCIvarRefExpr(ObjCIvarDecl *d, 180 QualType t, SourceLocation l, Expr *base=0, 181 bool arrow = false, bool freeIvar = false) : 182 Expr(ObjCIvarRefExprClass, t, false, false), D(d), 183 Loc(l), Base(base), IsArrow(arrow), 184 IsFreeIvar(freeIvar) {} 185 186 explicit ObjCIvarRefExpr(EmptyShell Empty) 187 : Expr(ObjCIvarRefExprClass, Empty) {} 188 189 ObjCIvarDecl *getDecl() { return D; } 190 const ObjCIvarDecl *getDecl() const { return D; } 191 void setDecl(ObjCIvarDecl *d) { D = d; } 192 193 const Expr *getBase() const { return cast<Expr>(Base); } 194 Expr *getBase() { return cast<Expr>(Base); } 195 void setBase(Expr * base) { Base = base; } 196 197 bool isArrow() const { return IsArrow; } 198 bool isFreeIvar() const { return IsFreeIvar; } 199 void setIsArrow(bool A) { IsArrow = A; } 200 void setIsFreeIvar(bool A) { IsFreeIvar = A; } 201 202 SourceLocation getLocation() const { return Loc; } 203 void setLocation(SourceLocation L) { Loc = L; } 204 205 virtual SourceRange getSourceRange() const { 206 return isFreeIvar() ? SourceRange(Loc) 207 : SourceRange(getBase()->getLocStart(), Loc); 208 } 209 210 static bool classof(const Stmt *T) { 211 return T->getStmtClass() == ObjCIvarRefExprClass; 212 } 213 static bool classof(const ObjCIvarRefExpr *) { return true; } 214 215 // Iterators 216 virtual child_iterator child_begin(); 217 virtual child_iterator child_end(); 218}; 219 220/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 221/// property. 222/// 223class ObjCPropertyRefExpr : public Expr { 224private: 225 ObjCPropertyDecl *AsProperty; 226 SourceLocation IdLoc; 227 Stmt *Base; 228public: 229 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 230 SourceLocation l, Expr *base) 231 : Expr(ObjCPropertyRefExprClass, t, false, false), AsProperty(PD), 232 IdLoc(l), Base(base) { 233 } 234 235 explicit ObjCPropertyRefExpr(EmptyShell Empty) 236 : Expr(ObjCPropertyRefExprClass, Empty) {} 237 238 ObjCPropertyDecl *getProperty() const { return AsProperty; } 239 void setProperty(ObjCPropertyDecl *D) { AsProperty = D; } 240 241 const Expr *getBase() const { return cast<Expr>(Base); } 242 Expr *getBase() { return cast<Expr>(Base); } 243 void setBase(Expr *base) { Base = base; } 244 245 SourceLocation getLocation() const { return IdLoc; } 246 void setLocation(SourceLocation L) { IdLoc = L; } 247 248 virtual SourceRange getSourceRange() const { 249 return SourceRange(getBase()->getLocStart(), IdLoc); 250 } 251 252 static bool classof(const Stmt *T) { 253 return T->getStmtClass() == ObjCPropertyRefExprClass; 254 } 255 static bool classof(const ObjCPropertyRefExpr *) { return true; } 256 257 // Iterators 258 virtual child_iterator child_begin(); 259 virtual child_iterator child_end(); 260}; 261 262/// ObjCImplicitSetterGetterRefExpr - A dot-syntax expression to access two 263/// methods; one to set a value to an 'ivar' (Setter) and the other to access 264/// an 'ivar' (Setter). 265/// An example for use of this AST is: 266/// @code 267/// @interface Test { } 268/// - (Test *)crash; 269/// - (void)setCrash: (Test*)value; 270/// @end 271/// void foo(Test *p1, Test *p2) 272/// { 273/// p2.crash = p1.crash; // Uses ObjCImplicitSetterGetterRefExpr AST 274/// } 275/// @endcode 276class ObjCImplicitSetterGetterRefExpr : public Expr { 277 /// Setter - Setter method user declared for setting its 'ivar' to a value 278 ObjCMethodDecl *Setter; 279 /// Getter - Getter method user declared for accessing 'ivar' it controls. 280 ObjCMethodDecl *Getter; 281 /// Location of the member in the dot syntax notation. This is location 282 /// of the getter method. 283 SourceLocation MemberLoc; 284 // FIXME: Swizzle these into a single pointer. 285 Stmt *Base; 286 ObjCInterfaceDecl *InterfaceDecl; 287 /// Location of the receiver class in the dot syntax notation 288 /// used to call a class method setter/getter. 289 SourceLocation ClassLoc; 290 291public: 292 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 293 QualType t, 294 ObjCMethodDecl *setter, 295 SourceLocation l, Expr *base) 296 : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false), 297 Setter(setter), Getter(getter), MemberLoc(l), Base(base), 298 InterfaceDecl(0), ClassLoc(SourceLocation()) { 299 } 300 ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter, 301 QualType t, 302 ObjCMethodDecl *setter, 303 SourceLocation l, ObjCInterfaceDecl *C, SourceLocation CL) 304 : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false), 305 Setter(setter), Getter(getter), MemberLoc(l), Base(0), InterfaceDecl(C), 306 ClassLoc(CL) { 307 } 308 explicit ObjCImplicitSetterGetterRefExpr(EmptyShell Empty) 309 : Expr(ObjCImplicitSetterGetterRefExprClass, Empty){} 310 311 ObjCMethodDecl *getGetterMethod() const { return Getter; } 312 ObjCMethodDecl *getSetterMethod() const { return Setter; } 313 ObjCInterfaceDecl *getInterfaceDecl() const { return InterfaceDecl; } 314 void setGetterMethod(ObjCMethodDecl *D) { Getter = D; } 315 void setSetterMethod(ObjCMethodDecl *D) { Setter = D; } 316 void setInterfaceDecl(ObjCInterfaceDecl *D) { InterfaceDecl = D; } 317 318 virtual SourceRange getSourceRange() const { 319 if (Base) 320 return SourceRange(getBase()->getLocStart(), MemberLoc); 321 return SourceRange(ClassLoc, MemberLoc); 322 } 323 const Expr *getBase() const { return cast_or_null<Expr>(Base); } 324 Expr *getBase() { return cast_or_null<Expr>(Base); } 325 void setBase(Expr *base) { Base = base; } 326 327 SourceLocation getLocation() const { return MemberLoc; } 328 void setLocation(SourceLocation L) { MemberLoc = L; } 329 SourceLocation getClassLoc() const { return ClassLoc; } 330 void setClassLoc(SourceLocation L) { ClassLoc = L; } 331 332 static bool classof(const Stmt *T) { 333 return T->getStmtClass() == ObjCImplicitSetterGetterRefExprClass; 334 } 335 static bool classof(const ObjCImplicitSetterGetterRefExpr *) { return true; } 336 337 // Iterators 338 virtual child_iterator child_begin(); 339 virtual child_iterator child_end(); 340}; 341 342class ObjCMessageExpr : public Expr { 343 // SubExprs - The receiver and arguments of the message expression. 344 Stmt **SubExprs; 345 346 // NumArgs - The number of arguments (not including the receiver) to the 347 // message expression. 348 unsigned NumArgs; 349 350 // A unigue name for this message. 351 Selector SelName; 352 353 // A method prototype for this message (optional). 354 // FIXME: Since method decls contain the selector, and most messages have a 355 // prototype, consider devising a scheme for unifying SelName/MethodProto. 356 ObjCMethodDecl *MethodProto; 357 358 SourceLocation LBracloc, RBracloc; 359 360 // Constants for indexing into SubExprs. 361 enum { RECEIVER=0, ARGS_START=1 }; 362 363 // Bit-swizzling flags. 364 enum { IsInstMeth=0, IsClsMethDeclUnknown, IsClsMethDeclKnown, Flags=0x3 }; 365 unsigned getFlag() const { return (uintptr_t) SubExprs[RECEIVER] & Flags; } 366 367public: 368 /// This constructor is used to represent class messages where the 369 /// ObjCInterfaceDecl* of the receiver is not known. 370 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 371 QualType retType, ObjCMethodDecl *methDecl, 372 SourceLocation LBrac, SourceLocation RBrac, 373 Expr **ArgExprs, unsigned NumArgs); 374 375 /// This constructor is used to represent class messages where the 376 /// ObjCInterfaceDecl* of the receiver is known. 377 // FIXME: clsName should be typed to ObjCInterfaceType 378 ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 379 QualType retType, ObjCMethodDecl *methDecl, 380 SourceLocation LBrac, SourceLocation RBrac, 381 Expr **ArgExprs, unsigned NumArgs); 382 383 // constructor for instance messages. 384 ObjCMessageExpr(Expr *receiver, Selector selInfo, 385 QualType retType, ObjCMethodDecl *methDecl, 386 SourceLocation LBrac, SourceLocation RBrac, 387 Expr **ArgExprs, unsigned NumArgs); 388 389 explicit ObjCMessageExpr(EmptyShell Empty) 390 : Expr(ObjCMessageExprClass, Empty), SubExprs(0), NumArgs(0) {} 391 392 ~ObjCMessageExpr() { 393 delete [] SubExprs; 394 } 395 396 /// getReceiver - Returns the receiver of the message expression. 397 /// This can be NULL if the message is for class methods. For 398 /// class methods, use getClassName. 399 /// FIXME: need to handle/detect 'super' usage within a class method. 400 Expr *getReceiver() { 401 uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 402 return (x & Flags) == IsInstMeth ? (Expr*) x : 0; 403 } 404 const Expr *getReceiver() const { 405 return const_cast<ObjCMessageExpr*>(this)->getReceiver(); 406 } 407 // FIXME: need setters for different receiver types. 408 void setReceiver(Expr *rec) { SubExprs[RECEIVER] = rec; } 409 Selector getSelector() const { return SelName; } 410 void setSelector(Selector S) { SelName = S; } 411 412 const ObjCMethodDecl *getMethodDecl() const { return MethodProto; } 413 ObjCMethodDecl *getMethodDecl() { return MethodProto; } 414 void setMethodDecl(ObjCMethodDecl *MD) { MethodProto = MD; } 415 416 typedef std::pair<ObjCInterfaceDecl*, IdentifierInfo*> ClassInfo; 417 418 /// getClassInfo - For class methods, this returns both the ObjCInterfaceDecl* 419 /// and IdentifierInfo* of the invoked class. Both can be NULL if this 420 /// is an instance message, and the ObjCInterfaceDecl* can be NULL if none 421 /// was available when this ObjCMessageExpr object was constructed. 422 ClassInfo getClassInfo() const; 423 void setClassInfo(const ClassInfo &C); 424 425 /// getClassName - For class methods, this returns the invoked class, 426 /// and returns NULL otherwise. For instance methods, use getReceiver. 427 IdentifierInfo *getClassName() const { 428 return getClassInfo().second; 429 } 430 431 /// getNumArgs - Return the number of actual arguments to this call. 432 unsigned getNumArgs() const { return NumArgs; } 433 void setNumArgs(unsigned nArgs) { 434 NumArgs = nArgs; 435 // FIXME: should always allocate SubExprs via the ASTContext's 436 // allocator. 437 if (!SubExprs) 438 SubExprs = new Stmt* [NumArgs + 1]; 439 } 440 441 /// getArg - Return the specified argument. 442 Expr *getArg(unsigned Arg) { 443 assert(Arg < NumArgs && "Arg access out of range!"); 444 return cast<Expr>(SubExprs[Arg+ARGS_START]); 445 } 446 const Expr *getArg(unsigned Arg) const { 447 assert(Arg < NumArgs && "Arg access out of range!"); 448 return cast<Expr>(SubExprs[Arg+ARGS_START]); 449 } 450 /// setArg - Set the specified argument. 451 void setArg(unsigned Arg, Expr *ArgExpr) { 452 assert(Arg < NumArgs && "Arg access out of range!"); 453 SubExprs[Arg+ARGS_START] = ArgExpr; 454 } 455 456 SourceLocation getLeftLoc() const { return LBracloc; } 457 SourceLocation getRightLoc() const { return RBracloc; } 458 459 void setLeftLoc(SourceLocation L) { LBracloc = L; } 460 void setRightLoc(SourceLocation L) { RBracloc = L; } 461 462 void setSourceRange(SourceRange R) { 463 LBracloc = R.getBegin(); 464 RBracloc = R.getEnd(); 465 } 466 virtual SourceRange getSourceRange() const { 467 return SourceRange(LBracloc, RBracloc); 468 } 469 470 static bool classof(const Stmt *T) { 471 return T->getStmtClass() == ObjCMessageExprClass; 472 } 473 static bool classof(const ObjCMessageExpr *) { return true; } 474 475 // Iterators 476 virtual child_iterator child_begin(); 477 virtual child_iterator child_end(); 478 479 typedef ExprIterator arg_iterator; 480 typedef ConstExprIterator const_arg_iterator; 481 482 arg_iterator arg_begin() { return &SubExprs[ARGS_START]; } 483 arg_iterator arg_end() { return &SubExprs[ARGS_START] + NumArgs; } 484 const_arg_iterator arg_begin() const { return &SubExprs[ARGS_START]; } 485 const_arg_iterator arg_end() const { return &SubExprs[ARGS_START] + NumArgs; } 486}; 487 488/// ObjCSuperExpr - Represents the "super" expression in Objective-C, 489/// which refers to the object on which the current method is executing. 490class ObjCSuperExpr : public Expr { 491 SourceLocation Loc; 492public: 493 ObjCSuperExpr(SourceLocation L, QualType Type) 494 : Expr(ObjCSuperExprClass, Type, false, false), Loc(L) { } 495 explicit ObjCSuperExpr(EmptyShell Empty) : Expr(ObjCSuperExprClass, Empty) {} 496 497 SourceLocation getLoc() const { return Loc; } 498 void setLoc(SourceLocation L) { Loc = L; } 499 500 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 501 502 static bool classof(const Stmt *T) { 503 return T->getStmtClass() == ObjCSuperExprClass; 504 } 505 static bool classof(const ObjCSuperExpr *) { return true; } 506 507 // Iterators 508 virtual child_iterator child_begin(); 509 virtual child_iterator child_end(); 510}; 511 512/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type. 513/// (similiar in spirit to MemberExpr). 514class ObjCIsaExpr : public Expr { 515 /// Base - the expression for the base object pointer. 516 Stmt *Base; 517 518 /// IsaMemberLoc - This is the location of the 'isa'. 519 SourceLocation IsaMemberLoc; 520 521 /// IsArrow - True if this is "X->F", false if this is "X.F". 522 bool IsArrow; 523public: 524 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty) 525 : Expr(ObjCIsaExprClass, ty, false, false), 526 Base(base), IsaMemberLoc(l), IsArrow(isarrow) {} 527 528 /// \brief Build an empty expression. 529 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { } 530 531 void setBase(Expr *E) { Base = E; } 532 Expr *getBase() const { return cast<Expr>(Base); } 533 534 bool isArrow() const { return IsArrow; } 535 void setArrow(bool A) { IsArrow = A; } 536 537 /// getMemberLoc - Return the location of the "member", in X->F, it is the 538 /// location of 'F'. 539 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; } 540 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; } 541 542 virtual SourceRange getSourceRange() const { 543 return SourceRange(getBase()->getLocStart(), IsaMemberLoc); 544 } 545 546 virtual SourceLocation getExprLoc() const { return IsaMemberLoc; } 547 548 static bool classof(const Stmt *T) { 549 return T->getStmtClass() == ObjCIsaExprClass; 550 } 551 static bool classof(const ObjCIsaExpr *) { return true; } 552 553 // Iterators 554 virtual child_iterator child_begin(); 555 virtual child_iterator child_end(); 556}; 557 558} // end namespace clang 559 560#endif 561