ExprObjC.h revision 5daf570d0ce027e18ed5f9d66e6b2a14a40b720d
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 StringLiteral *String; 30 SourceLocation AtLoc; 31public: 32 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L) 33 : Expr(ObjCStringLiteralClass, T), String(SL), AtLoc(L) {} 34 35 StringLiteral* getString() { return String; } 36 37 const StringLiteral* getString() const { return String; } 38 39 SourceLocation getAtLoc() const { return AtLoc; } 40 41 virtual SourceRange getSourceRange() const { 42 return SourceRange(AtLoc, String->getLocEnd()); 43 } 44 45 static bool classof(const Stmt *T) { 46 return T->getStmtClass() == ObjCStringLiteralClass; 47 } 48 static bool classof(const ObjCStringLiteral *) { return true; } 49 50 // Iterators 51 virtual child_iterator child_begin(); 52 virtual child_iterator child_end(); 53 54 virtual void EmitImpl(llvm::Serializer& S) const; 55 static ObjCStringLiteral* CreateImpl(llvm::Deserializer& D, ASTContext& C); 56}; 57 58/// ObjCEncodeExpr, used for @encode in Objective-C. 59class ObjCEncodeExpr : public Expr { 60 QualType EncType; 61 SourceLocation AtLoc, RParenLoc; 62public: 63 ObjCEncodeExpr(QualType T, QualType ET, 64 SourceLocation at, SourceLocation rp) 65 : Expr(ObjCEncodeExprClass, T), EncType(ET), AtLoc(at), RParenLoc(rp) {} 66 67 SourceLocation getAtLoc() const { return AtLoc; } 68 SourceLocation getRParenLoc() const { return RParenLoc; } 69 70 virtual SourceRange getSourceRange() const { 71 return SourceRange(AtLoc, RParenLoc); 72 } 73 74 QualType getEncodedType() const { return EncType; } 75 76 static bool classof(const Stmt *T) { 77 return T->getStmtClass() == ObjCEncodeExprClass; 78 } 79 static bool classof(const ObjCEncodeExpr *) { return true; } 80 81 // Iterators 82 virtual child_iterator child_begin(); 83 virtual child_iterator child_end(); 84 85 virtual void EmitImpl(llvm::Serializer& S) const; 86 static ObjCEncodeExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 87}; 88 89/// ObjCSelectorExpr used for @selector in Objective-C. 90class ObjCSelectorExpr : public Expr { 91 Selector SelName; 92 SourceLocation AtLoc, RParenLoc; 93public: 94 ObjCSelectorExpr(QualType T, Selector selInfo, 95 SourceLocation at, SourceLocation rp) 96 : Expr(ObjCSelectorExprClass, T), SelName(selInfo), 97 AtLoc(at), RParenLoc(rp) {} 98 99 Selector getSelector() const { return SelName; } 100 101 SourceLocation getAtLoc() const { return AtLoc; } 102 SourceLocation getRParenLoc() const { return RParenLoc; } 103 104 virtual SourceRange getSourceRange() const { 105 return SourceRange(AtLoc, RParenLoc); 106 } 107 108 /// getNumArgs - Return the number of actual arguments to this call. 109 unsigned getNumArgs() const { return SelName.getNumArgs(); } 110 111 static bool classof(const Stmt *T) { 112 return T->getStmtClass() == ObjCSelectorExprClass; 113 } 114 static bool classof(const ObjCSelectorExpr *) { return true; } 115 116 // Iterators 117 virtual child_iterator child_begin(); 118 virtual child_iterator child_end(); 119 120 virtual void EmitImpl(llvm::Serializer& S) const; 121 static ObjCSelectorExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 122}; 123 124/// ObjCProtocolExpr used for protocol in Objective-C. 125class ObjCProtocolExpr : public Expr { 126 ObjCProtocolDecl *Protocol; 127 SourceLocation AtLoc, RParenLoc; 128public: 129 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol, 130 SourceLocation at, SourceLocation rp) 131 : Expr(ObjCProtocolExprClass, T), Protocol(protocol), 132 AtLoc(at), RParenLoc(rp) {} 133 134 ObjCProtocolDecl *getProtocol() const { return Protocol; } 135 136 SourceLocation getAtLoc() const { return AtLoc; } 137 SourceLocation getRParenLoc() const { return RParenLoc; } 138 139 virtual SourceRange getSourceRange() const { 140 return SourceRange(AtLoc, RParenLoc); 141 } 142 143 static bool classof(const Stmt *T) { 144 return T->getStmtClass() == ObjCProtocolExprClass; 145 } 146 static bool classof(const ObjCProtocolExpr *) { return true; } 147 148 // Iterators 149 virtual child_iterator child_begin(); 150 virtual child_iterator child_end(); 151 152 virtual void EmitImpl(llvm::Serializer& S) const; 153 static ObjCProtocolExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 154}; 155 156/// ObjCIvarRefExpr - A reference to an ObjC instance variable. 157class ObjCIvarRefExpr : public Expr { 158 class ObjCIvarDecl *D; 159 SourceLocation Loc; 160 Stmt *Base; 161 bool IsArrow:1; // True if this is "X->F", false if this is "X.F". 162 bool IsFreeIvar:1; // True if ivar reference has no base (self assumed). 163 164public: 165 ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t, SourceLocation l, Expr *base=0, 166 bool arrow = false, bool freeIvar = false) : 167 Expr(ObjCIvarRefExprClass, t), D(d), Loc(l), Base(base), IsArrow(arrow), 168 IsFreeIvar(freeIvar) {} 169 170 ObjCIvarDecl *getDecl() { return D; } 171 const ObjCIvarDecl *getDecl() const { return D; } 172 virtual SourceRange getSourceRange() const { 173 return isFreeIvar() ? SourceRange(Loc) 174 : SourceRange(getBase()->getLocStart(), Loc); 175 } 176 const Expr *getBase() const { return cast<Expr>(Base); } 177 Expr *getBase() { return cast<Expr>(Base); } 178 void setBase(Expr * base) { Base = base; } 179 bool isArrow() const { return IsArrow; } 180 bool isFreeIvar() const { return IsFreeIvar; } 181 182 SourceLocation getLocation() const { return Loc; } 183 184 static bool classof(const Stmt *T) { 185 return T->getStmtClass() == ObjCIvarRefExprClass; 186 } 187 static bool classof(const ObjCIvarRefExpr *) { return true; } 188 189 // Iterators 190 virtual child_iterator child_begin(); 191 virtual child_iterator child_end(); 192 193 virtual void EmitImpl(llvm::Serializer& S) const; 194 static ObjCIvarRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 195}; 196 197/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC 198/// property. 199/// 200class ObjCPropertyRefExpr : public Expr { 201private: 202 ObjCPropertyDecl *AsProperty; 203 SourceLocation Loc; 204 Stmt *Base; 205 206public: 207 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t, 208 SourceLocation l, Expr *base) 209 : Expr(ObjCPropertyRefExprClass, t), AsProperty(PD), Loc(l), Base(base) { 210 } 211 ObjCPropertyDecl *getProperty() const { 212 return AsProperty; 213 } 214 215 virtual SourceRange getSourceRange() const { 216 return SourceRange(getBase()->getLocStart(), Loc); 217 } 218 const Expr *getBase() const { return cast<Expr>(Base); } 219 Expr *getBase() { return cast<Expr>(Base); } 220 void setBase(Expr * base) { Base = base; } 221 222 SourceLocation getLocation() const { return Loc; } 223 224 static bool classof(const Stmt *T) { 225 return T->getStmtClass() == ObjCPropertyRefExprClass; 226 } 227 static bool classof(const ObjCPropertyRefExpr *) { return true; } 228 229 // Iterators 230 virtual child_iterator child_begin(); 231 virtual child_iterator child_end(); 232 233 virtual void EmitImpl(llvm::Serializer& S) const; 234 static ObjCPropertyRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 235}; 236 237/// ObjCKVCRefExpr - A dot-syntax expression to access "implicit" properties 238/// (i.e. methods following the property naming convention). KVC stands for 239/// Key Value Encoding, a generic concept for accessing or setting a 'Key' 240/// value for an object. 241/// 242 243class ObjCKVCRefExpr : public Expr { 244private: 245 246 ObjCMethodDecl *Setter; 247 ObjCMethodDecl *Getter; 248 SourceLocation Loc; 249 Stmt *Base; 250 251public: 252 ObjCKVCRefExpr(ObjCMethodDecl *getter, 253 QualType t, 254 SourceLocation l, Expr *base) 255 : Expr(ObjCKVCRefExprClass, t), Setter(0), 256 Getter(getter), Loc(l), Base(base) { 257 } 258 259 ObjCMethodDecl *getGetterMethod() const { 260 return Getter; 261 } 262 void setSetterMethod(ObjCMethodDecl *setter) { 263 Setter = setter; 264 } 265 ObjCMethodDecl *getSetterMethod() const { 266 return Setter; 267 } 268 269 virtual SourceRange getSourceRange() const { 270 return SourceRange(getBase()->getLocStart(), Loc); 271 } 272 const Expr *getBase() const { return cast<Expr>(Base); } 273 Expr *getBase() { return cast<Expr>(Base); } 274 void setBase(Expr * base) { Base = base; } 275 276 SourceLocation getLocation() const { return Loc; } 277 278 static bool classof(const Stmt *T) { 279 return T->getStmtClass() == ObjCKVCRefExprClass; 280 } 281 static bool classof(const ObjCKVCRefExpr *) { return true; } 282 283 // Iterators 284 virtual child_iterator child_begin(); 285 virtual child_iterator child_end(); 286 287 virtual void EmitImpl(llvm::Serializer& S) const; 288 static ObjCKVCRefExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 289}; 290 291class ObjCMessageExpr : public Expr { 292 // SubExprs - The receiver and arguments of the message expression. 293 Stmt **SubExprs; 294 295 // NumArgs - The number of arguments (not including the receiver) to the 296 // message expression. 297 unsigned NumArgs; 298 299 // A unigue name for this message. 300 Selector SelName; 301 302 // A method prototype for this message (optional). 303 // FIXME: Since method decls contain the selector, and most messages have a 304 // prototype, consider devising a scheme for unifying SelName/MethodProto. 305 ObjCMethodDecl *MethodProto; 306 307 SourceLocation LBracloc, RBracloc; 308 309 // Constants for indexing into SubExprs. 310 enum { RECEIVER=0, ARGS_START=1 }; 311 312 // Bit-swizziling flags. 313 enum { IsInstMeth=0, IsClsMethDeclUnknown, IsClsMethDeclKnown, Flags=0x3 }; 314 unsigned getFlag() const { return (uintptr_t) SubExprs[RECEIVER] & Flags; } 315 316 // constructor used during deserialization 317 ObjCMessageExpr(Selector selInfo, QualType retType, 318 SourceLocation LBrac, SourceLocation RBrac, 319 Stmt **subexprs, unsigned nargs) 320 : Expr(ObjCMessageExprClass, retType), SubExprs(subexprs), 321 NumArgs(nargs), SelName(selInfo), MethodProto(NULL), 322 LBracloc(LBrac), RBracloc(RBrac) {} 323 324public: 325 /// This constructor is used to represent class messages where the 326 /// ObjCInterfaceDecl* of the receiver is not known. 327 ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, 328 QualType retType, ObjCMethodDecl *methDecl, 329 SourceLocation LBrac, SourceLocation RBrac, 330 Expr **ArgExprs, unsigned NumArgs); 331 332 /// This constructor is used to represent class messages where the 333 /// ObjCInterfaceDecl* of the receiver is known. 334 // FIXME: clsName should be typed to ObjCInterfaceType 335 ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, 336 QualType retType, ObjCMethodDecl *methDecl, 337 SourceLocation LBrac, SourceLocation RBrac, 338 Expr **ArgExprs, unsigned NumArgs); 339 340 // constructor for instance messages. 341 ObjCMessageExpr(Expr *receiver, Selector selInfo, 342 QualType retType, ObjCMethodDecl *methDecl, 343 SourceLocation LBrac, SourceLocation RBrac, 344 Expr **ArgExprs, unsigned NumArgs); 345 346 ~ObjCMessageExpr() { 347 delete [] SubExprs; 348 } 349 350 /// getReceiver - Returns the receiver of the message expression. 351 /// This can be NULL if the message is for class methods. For 352 /// class methods, use getClassName. 353 /// FIXME: need to handle/detect 'super' usage within a class method. 354 Expr *getReceiver() { 355 uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; 356 return (x & Flags) == IsInstMeth ? (Expr*) x : 0; 357 } 358 const Expr *getReceiver() const { 359 return const_cast<ObjCMessageExpr*>(this)->getReceiver(); 360 } 361 362 Selector getSelector() const { return SelName; } 363 364 const ObjCMethodDecl *getMethodDecl() const { return MethodProto; } 365 ObjCMethodDecl *getMethodDecl() { return MethodProto; } 366 367 typedef std::pair<ObjCInterfaceDecl*, IdentifierInfo*> ClassInfo; 368 369 /// getClassInfo - For class methods, this returns both the ObjCInterfaceDecl* 370 /// and IdentifierInfo* of the invoked class. Both can be NULL if this 371 /// is an instance message, and the ObjCInterfaceDecl* can be NULL if none 372 /// was available when this ObjCMessageExpr object was constructed. 373 ClassInfo getClassInfo() const; 374 375 /// getClassName - For class methods, this returns the invoked class, 376 /// and returns NULL otherwise. For instance methods, use getReceiver. 377 IdentifierInfo *getClassName() const { 378 return getClassInfo().second; 379 } 380 381 382 /// getNumArgs - Return the number of actual arguments to this call. 383 unsigned getNumArgs() const { return NumArgs; } 384 385 /// getArg - Return the specified argument. 386 Expr *getArg(unsigned Arg) { 387 assert(Arg < NumArgs && "Arg access out of range!"); 388 return cast<Expr>(SubExprs[Arg+ARGS_START]); 389 } 390 const Expr *getArg(unsigned Arg) const { 391 assert(Arg < NumArgs && "Arg access out of range!"); 392 return cast<Expr>(SubExprs[Arg+ARGS_START]); 393 } 394 /// setArg - Set the specified argument. 395 void setArg(unsigned Arg, Expr *ArgExpr) { 396 assert(Arg < NumArgs && "Arg access out of range!"); 397 SubExprs[Arg+ARGS_START] = ArgExpr; 398 } 399 400 virtual SourceRange getSourceRange() const { 401 return SourceRange(LBracloc, RBracloc); 402 } 403 404 static bool classof(const Stmt *T) { 405 return T->getStmtClass() == ObjCMessageExprClass; 406 } 407 static bool classof(const ObjCMessageExpr *) { return true; } 408 409 // Iterators 410 virtual child_iterator child_begin(); 411 virtual child_iterator child_end(); 412 413 typedef ExprIterator arg_iterator; 414 typedef ConstExprIterator const_arg_iterator; 415 416 arg_iterator arg_begin() { return &SubExprs[ARGS_START]; } 417 arg_iterator arg_end() { return &SubExprs[ARGS_START] + NumArgs; } 418 const_arg_iterator arg_begin() const { return &SubExprs[ARGS_START]; } 419 const_arg_iterator arg_end() const { return &SubExprs[ARGS_START] + NumArgs; } 420 421 // Serialization. 422 virtual void EmitImpl(llvm::Serializer& S) const; 423 static ObjCMessageExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 424}; 425 426/// ObjCSuperExpr - Represents the "super" expression in Objective-C, 427/// which refers to the object on which the current method is executing. 428class ObjCSuperExpr : public Expr { 429 SourceLocation Loc; 430 431public: 432 ObjCSuperExpr(SourceLocation L, QualType Type) 433 : Expr(ObjCSuperExprClass, Type), Loc(L) { } 434 435 virtual SourceRange getSourceRange() const { return SourceRange(Loc); } 436 437 static bool classof(const Stmt *T) { 438 return T->getStmtClass() == ObjCSuperExprClass; 439 } 440 static bool classof(const ObjCSuperExpr *) { return true; } 441 442 // Iterators 443 virtual child_iterator child_begin(); 444 virtual child_iterator child_end(); 445 446 virtual void EmitImpl(llvm::Serializer& S) const; 447 static ObjCSuperExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C); 448}; 449 450} // end namespace clang 451 452#endif 453