ExprObjC.h revision f8730d7c313c421d5d7a2b9d97541fc89d5a52d4
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  TypeSourceInfo *EncodedType;
63  SourceLocation AtLoc, RParenLoc;
64public:
65  ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType,
66                 SourceLocation at, SourceLocation rp)
67    : Expr(ObjCEncodeExprClass, T, EncodedType->getType()->isDependentType(),
68           EncodedType->getType()->isDependentType()),
69      EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {}
70
71  explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){}
72
73
74  SourceLocation getAtLoc() const { return AtLoc; }
75  void setAtLoc(SourceLocation L) { AtLoc = L; }
76  SourceLocation getRParenLoc() const { return RParenLoc; }
77  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
78
79  QualType getEncodedType() const { return EncodedType->getType(); }
80
81  TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; }
82  void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) {
83    EncodedType = EncType;
84  }
85
86  virtual SourceRange getSourceRange() const {
87    return SourceRange(AtLoc, RParenLoc);
88  }
89
90  static bool classof(const Stmt *T) {
91    return T->getStmtClass() == ObjCEncodeExprClass;
92  }
93  static bool classof(const ObjCEncodeExpr *) { return true; }
94
95  // Iterators
96  virtual child_iterator child_begin();
97  virtual child_iterator child_end();
98};
99
100/// ObjCSelectorExpr used for @selector in Objective-C.
101class ObjCSelectorExpr : public Expr {
102  Selector SelName;
103  SourceLocation AtLoc, RParenLoc;
104public:
105  ObjCSelectorExpr(QualType T, Selector selInfo,
106                   SourceLocation at, SourceLocation rp)
107  : Expr(ObjCSelectorExprClass, T, false, false), SelName(selInfo), AtLoc(at),
108    RParenLoc(rp){}
109  explicit ObjCSelectorExpr(EmptyShell Empty)
110   : Expr(ObjCSelectorExprClass, Empty) {}
111
112  Selector getSelector() const { return SelName; }
113  void setSelector(Selector S) { SelName = S; }
114
115  SourceLocation getAtLoc() const { return AtLoc; }
116  SourceLocation getRParenLoc() const { return RParenLoc; }
117  void setAtLoc(SourceLocation L) { AtLoc = L; }
118  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
119
120  virtual SourceRange getSourceRange() const {
121    return SourceRange(AtLoc, RParenLoc);
122  }
123
124  /// getNumArgs - Return the number of actual arguments to this call.
125  unsigned getNumArgs() const { return SelName.getNumArgs(); }
126
127  static bool classof(const Stmt *T) {
128    return T->getStmtClass() == ObjCSelectorExprClass;
129  }
130  static bool classof(const ObjCSelectorExpr *) { return true; }
131
132  // Iterators
133  virtual child_iterator child_begin();
134  virtual child_iterator child_end();
135};
136
137/// ObjCProtocolExpr used for protocol expression in Objective-C.  This is used
138/// as: @protocol(foo), as in:
139///   obj conformsToProtocol:@protocol(foo)]
140/// The return type is "Protocol*".
141class ObjCProtocolExpr : public Expr {
142  ObjCProtocolDecl *TheProtocol;
143  SourceLocation AtLoc, RParenLoc;
144public:
145  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
146                   SourceLocation at, SourceLocation rp)
147  : Expr(ObjCProtocolExprClass, T, false, false), TheProtocol(protocol),
148    AtLoc(at), RParenLoc(rp) {}
149  explicit ObjCProtocolExpr(EmptyShell Empty)
150    : Expr(ObjCProtocolExprClass, Empty) {}
151
152  ObjCProtocolDecl *getProtocol() const { return TheProtocol; }
153  void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; }
154
155  SourceLocation getAtLoc() const { return AtLoc; }
156  SourceLocation getRParenLoc() const { return RParenLoc; }
157  void setAtLoc(SourceLocation L) { AtLoc = L; }
158  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
159
160  virtual SourceRange getSourceRange() const {
161    return SourceRange(AtLoc, RParenLoc);
162  }
163
164  static bool classof(const Stmt *T) {
165    return T->getStmtClass() == ObjCProtocolExprClass;
166  }
167  static bool classof(const ObjCProtocolExpr *) { return true; }
168
169  // Iterators
170  virtual child_iterator child_begin();
171  virtual child_iterator child_end();
172};
173
174/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
175class ObjCIvarRefExpr : public Expr {
176  class ObjCIvarDecl *D;
177  SourceLocation Loc;
178  Stmt *Base;
179  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
180  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
181
182public:
183  ObjCIvarRefExpr(ObjCIvarDecl *d,
184                  QualType t, SourceLocation l, Expr *base,
185                  bool arrow = false, bool freeIvar = false) :
186    Expr(ObjCIvarRefExprClass, t, /*TypeDependent=*/false,
187         base->isValueDependent()), D(d),
188         Loc(l), Base(base), IsArrow(arrow),
189         IsFreeIvar(freeIvar) {}
190
191  explicit ObjCIvarRefExpr(EmptyShell Empty)
192    : Expr(ObjCIvarRefExprClass, Empty) {}
193
194  ObjCIvarDecl *getDecl() { return D; }
195  const ObjCIvarDecl *getDecl() const { return D; }
196  void setDecl(ObjCIvarDecl *d) { D = d; }
197
198  const Expr *getBase() const { return cast<Expr>(Base); }
199  Expr *getBase() { return cast<Expr>(Base); }
200  void setBase(Expr * base) { Base = base; }
201
202  bool isArrow() const { return IsArrow; }
203  bool isFreeIvar() const { return IsFreeIvar; }
204  void setIsArrow(bool A) { IsArrow = A; }
205  void setIsFreeIvar(bool A) { IsFreeIvar = A; }
206
207  SourceLocation getLocation() const { return Loc; }
208  void setLocation(SourceLocation L) { Loc = L; }
209
210  virtual SourceRange getSourceRange() const {
211    return isFreeIvar() ? SourceRange(Loc)
212    : SourceRange(getBase()->getLocStart(), Loc);
213  }
214
215  static bool classof(const Stmt *T) {
216    return T->getStmtClass() == ObjCIvarRefExprClass;
217  }
218  static bool classof(const ObjCIvarRefExpr *) { return true; }
219
220  // Iterators
221  virtual child_iterator child_begin();
222  virtual child_iterator child_end();
223};
224
225/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC
226/// property.
227///
228class ObjCPropertyRefExpr : public Expr {
229private:
230  ObjCPropertyDecl *AsProperty;
231  SourceLocation IdLoc;
232  Stmt *Base;
233public:
234  ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
235                      SourceLocation l, Expr *base)
236    : Expr(ObjCPropertyRefExprClass, t, /*TypeDependent=*/false,
237           base->isValueDependent()),
238      AsProperty(PD), IdLoc(l), Base(base) {
239  }
240
241  explicit ObjCPropertyRefExpr(EmptyShell Empty)
242    : Expr(ObjCPropertyRefExprClass, Empty) {}
243
244  ObjCPropertyDecl *getProperty() const { return AsProperty; }
245
246  const Expr *getBase() const { return cast<Expr>(Base); }
247  Expr *getBase() { return cast<Expr>(Base); }
248
249  SourceLocation getLocation() const { return IdLoc; }
250
251  virtual SourceRange getSourceRange() const {
252    return SourceRange(getBase()->getLocStart(), IdLoc);
253  }
254
255  static bool classof(const Stmt *T) {
256    return T->getStmtClass() == ObjCPropertyRefExprClass;
257  }
258  static bool classof(const ObjCPropertyRefExpr *) { return true; }
259
260  // Iterators
261  virtual child_iterator child_begin();
262  virtual child_iterator child_end();
263private:
264  friend class ASTStmtReader;
265  void setProperty(ObjCPropertyDecl *D) { AsProperty = D; }
266  void setBase(Expr *base) { Base = base; }
267  void setLocation(SourceLocation L) { IdLoc = L; }
268};
269
270/// ObjCImplicitSetterGetterRefExpr - A dot-syntax expression to access two
271/// methods; one to set a value to an 'ivar' (Setter) and the other to access
272/// an 'ivar' (Setter).
273/// An example for use of this AST is:
274/// @code
275///  @interface Test { }
276///  - (Test *)crash;
277///  - (void)setCrash: (Test*)value;
278/// @end
279/// void  foo(Test *p1, Test *p2)
280/// {
281///    p2.crash  = p1.crash; // Uses ObjCImplicitSetterGetterRefExpr AST
282/// }
283/// @endcode
284class ObjCImplicitSetterGetterRefExpr : public Expr {
285  /// Setter - Setter method user declared for setting its 'ivar' to a value
286  ObjCMethodDecl *Setter;
287  /// Getter - Getter method user declared for accessing 'ivar' it controls.
288  ObjCMethodDecl *Getter;
289  /// Location of the member in the dot syntax notation. This is location
290  /// of the getter method.
291  SourceLocation MemberLoc;
292  // FIXME: Swizzle these into a single pointer.
293  Stmt *Base;
294  ObjCInterfaceDecl *InterfaceDecl;
295  /// Location of the receiver class in the dot syntax notation
296  /// used to call a class method setter/getter.
297  SourceLocation ClassLoc;
298
299public:
300  ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter,
301                 QualType t,
302                 ObjCMethodDecl *setter,
303                 SourceLocation l, Expr *base)
304    : Expr(ObjCImplicitSetterGetterRefExprClass, t, /*TypeDependent=*/false,
305           base->isValueDependent()),
306      Setter(setter), Getter(getter), MemberLoc(l), Base(base),
307      InterfaceDecl(0), ClassLoc(SourceLocation()) {
308    }
309  ObjCImplicitSetterGetterRefExpr(ObjCMethodDecl *getter,
310                 QualType t,
311                 ObjCMethodDecl *setter,
312                 SourceLocation l, ObjCInterfaceDecl *C, SourceLocation CL)
313    : Expr(ObjCImplicitSetterGetterRefExprClass, t, false, false),
314      Setter(setter), Getter(getter), MemberLoc(l), Base(0), InterfaceDecl(C),
315      ClassLoc(CL) {
316    }
317  explicit ObjCImplicitSetterGetterRefExpr(EmptyShell Empty)
318           : Expr(ObjCImplicitSetterGetterRefExprClass, Empty){}
319
320  ObjCMethodDecl *getGetterMethod() const { return Getter; }
321  ObjCMethodDecl *getSetterMethod() const { return Setter; }
322  ObjCInterfaceDecl *getInterfaceDecl() const { return InterfaceDecl; }
323  void setGetterMethod(ObjCMethodDecl *D) { Getter = D; }
324  void setSetterMethod(ObjCMethodDecl *D) { Setter = D; }
325  void setInterfaceDecl(ObjCInterfaceDecl *D) { InterfaceDecl = D; }
326
327  virtual SourceRange getSourceRange() const {
328    if (Base)
329      return SourceRange(getBase()->getLocStart(), MemberLoc);
330    return SourceRange(ClassLoc, MemberLoc);
331  }
332  const Expr *getBase() const { return cast_or_null<Expr>(Base); }
333  Expr *getBase() { return cast_or_null<Expr>(Base); }
334  void setBase(Expr *base) { Base = base; }
335
336  SourceLocation getLocation() const { return MemberLoc; }
337  void setLocation(SourceLocation L) { MemberLoc = L; }
338  SourceLocation getClassLoc() const { return ClassLoc; }
339  void setClassLoc(SourceLocation L) { ClassLoc = L; }
340
341  static bool classof(const Stmt *T) {
342    return T->getStmtClass() == ObjCImplicitSetterGetterRefExprClass;
343  }
344  static bool classof(const ObjCImplicitSetterGetterRefExpr *) { return true; }
345
346  // Iterators
347  virtual child_iterator child_begin();
348  virtual child_iterator child_end();
349};
350
351/// \brief An expression that sends a message to the given Objective-C
352/// object or class.
353///
354/// The following contains two message send expressions:
355///
356/// \code
357///   [[NSString alloc] initWithString:@"Hello"]
358/// \endcode
359///
360/// The innermost message send invokes the "alloc" class method on the
361/// NSString class, while the outermost message send invokes the
362/// "initWithString" instance method on the object returned from
363/// NSString's "alloc". In all, an Objective-C message send can take
364/// on four different (although related) forms:
365///
366///   1. Send to an object instance.
367///   2. Send to a class.
368///   3. Send to the superclass instance of the current class.
369///   4. Send to the superclass of the current class.
370///
371/// All four kinds of message sends are modeled by the ObjCMessageExpr
372/// class, and can be distinguished via \c getReceiverKind(). Example:
373///
374class ObjCMessageExpr : public Expr {
375  /// \brief The number of arguments in the message send, not
376  /// including the receiver.
377  unsigned NumArgs : 16;
378
379  /// \brief The kind of message send this is, which is one of the
380  /// ReceiverKind values.
381  ///
382  /// We pad this out to a byte to avoid excessive masking and shifting.
383  unsigned Kind : 8;
384
385  /// \brief Whether we have an actual method prototype in \c
386  /// SelectorOrMethod.
387  ///
388  /// When non-zero, we have a method declaration; otherwise, we just
389  /// have a selector.
390  unsigned HasMethod : 8;
391
392  /// \brief When the message expression is a send to 'super', this is
393  /// the location of the 'super' keyword.
394  SourceLocation SuperLoc;
395
396  /// \brief Stores either the selector that this message is sending
397  /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer
398  /// referring to the method that we type-checked against.
399  uintptr_t SelectorOrMethod;
400
401  /// \brief The source locations of the open and close square
402  /// brackets ('[' and ']', respectively).
403  SourceLocation LBracLoc, RBracLoc;
404
405  ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs)
406    : Expr(ObjCMessageExprClass, Empty), NumArgs(NumArgs), Kind(0),
407      HasMethod(0), SelectorOrMethod(0) { }
408
409  ObjCMessageExpr(QualType T,
410                  SourceLocation LBracLoc,
411                  SourceLocation SuperLoc,
412                  bool IsInstanceSuper,
413                  QualType SuperType,
414                  Selector Sel,
415                  ObjCMethodDecl *Method,
416                  Expr **Args, unsigned NumArgs,
417                  SourceLocation RBracLoc);
418  ObjCMessageExpr(QualType T,
419                  SourceLocation LBracLoc,
420                  TypeSourceInfo *Receiver,
421                  Selector Sel,
422                  ObjCMethodDecl *Method,
423                  Expr **Args, unsigned NumArgs,
424                  SourceLocation RBracLoc);
425  ObjCMessageExpr(QualType T,
426                  SourceLocation LBracLoc,
427                  Expr *Receiver,
428                  Selector Sel,
429                  ObjCMethodDecl *Method,
430                  Expr **Args, unsigned NumArgs,
431                  SourceLocation RBracLoc);
432
433  /// \brief Retrieve the pointer value of the message receiver.
434  void *getReceiverPointer() const {
435    return *const_cast<void **>(
436                             reinterpret_cast<const void * const*>(this + 1));
437  }
438
439  /// \brief Set the pointer value of the message receiver.
440  void setReceiverPointer(void *Value) {
441    *reinterpret_cast<void **>(this + 1) = Value;
442  }
443
444public:
445  /// \brief The kind of receiver this message is sending to.
446  enum ReceiverKind {
447    /// \brief The receiver is a class.
448    Class = 0,
449    /// \brief The receiver is an object instance.
450    Instance,
451    /// \brief The receiver is a superclass.
452    SuperClass,
453    /// \brief The receiver is the instance of the superclass object.
454    SuperInstance
455  };
456
457  /// \brief Create a message send to super.
458  ///
459  /// \param Context The ASTContext in which this expression will be created.
460  ///
461  /// \param T The result type of this message.
462  ///
463  /// \param LBrac The location of the open square bracket '['.
464  ///
465  /// \param SuperLoc The location of the "super" keyword.
466  ///
467  /// \param IsInstanceSuper Whether this is an instance "super"
468  /// message (otherwise, it's a class "super" message).
469  ///
470  /// \param Sel The selector used to determine which method gets called.
471  ///
472  /// \param Method The Objective-C method against which this message
473  /// send was type-checked. May be NULL.
474  ///
475  /// \param Args The message send arguments.
476  ///
477  /// \param NumArgs The number of arguments.
478  ///
479  /// \param RBracLoc The location of the closing square bracket ']'.
480  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
481                                 SourceLocation LBracLoc,
482                                 SourceLocation SuperLoc,
483                                 bool IsInstanceSuper,
484                                 QualType SuperType,
485                                 Selector Sel,
486                                 ObjCMethodDecl *Method,
487                                 Expr **Args, unsigned NumArgs,
488                                 SourceLocation RBracLoc);
489
490  /// \brief Create a class message send.
491  ///
492  /// \param Context The ASTContext in which this expression will be created.
493  ///
494  /// \param T The result type of this message.
495  ///
496  /// \param LBrac The location of the open square bracket '['.
497  ///
498  /// \param Receiver The type of the receiver, including
499  /// source-location information.
500  ///
501  /// \param Sel The selector used to determine which method gets called.
502  ///
503  /// \param Method The Objective-C method against which this message
504  /// send was type-checked. May be NULL.
505  ///
506  /// \param Args The message send arguments.
507  ///
508  /// \param NumArgs The number of arguments.
509  ///
510  /// \param RBracLoc The location of the closing square bracket ']'.
511  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
512                                 SourceLocation LBracLoc,
513                                 TypeSourceInfo *Receiver,
514                                 Selector Sel,
515                                 ObjCMethodDecl *Method,
516                                 Expr **Args, unsigned NumArgs,
517                                 SourceLocation RBracLoc);
518
519  /// \brief Create an instance message send.
520  ///
521  /// \param Context The ASTContext in which this expression will be created.
522  ///
523  /// \param T The result type of this message.
524  ///
525  /// \param LBrac The location of the open square bracket '['.
526  ///
527  /// \param Receiver The expression used to produce the object that
528  /// will receive this message.
529  ///
530  /// \param Sel The selector used to determine which method gets called.
531  ///
532  /// \param Method The Objective-C method against which this message
533  /// send was type-checked. May be NULL.
534  ///
535  /// \param Args The message send arguments.
536  ///
537  /// \param NumArgs The number of arguments.
538  ///
539  /// \param RBracLoc The location of the closing square bracket ']'.
540  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
541                                 SourceLocation LBracLoc,
542                                 Expr *Receiver,
543                                 Selector Sel,
544                                 ObjCMethodDecl *Method,
545                                 Expr **Args, unsigned NumArgs,
546                                 SourceLocation RBracLoc);
547
548  /// \brief Create an empty Objective-C message expression, to be
549  /// filled in by subsequent calls.
550  ///
551  /// \param Context The context in which the message send will be created.
552  ///
553  /// \param NumArgs The number of message arguments, not including
554  /// the receiver.
555  static ObjCMessageExpr *CreateEmpty(ASTContext &Context, unsigned NumArgs);
556
557  /// \brief Determine the kind of receiver that this message is being
558  /// sent to.
559  ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; }
560
561  /// \brief Determine whether this is an instance message to either a
562  /// computed object or to super.
563  bool isInstanceMessage() const {
564    return getReceiverKind() == Instance || getReceiverKind() == SuperInstance;
565  }
566
567  /// \brief Determine whether this is an class message to either a
568  /// specified class or to super.
569  bool isClassMessage() const {
570    return getReceiverKind() == Class || getReceiverKind() == SuperClass;
571  }
572
573  /// \brief Returns the receiver of an instance message.
574  ///
575  /// \brief Returns the object expression for an instance message, or
576  /// NULL for a message that is not an instance message.
577  Expr *getInstanceReceiver() {
578    if (getReceiverKind() == Instance)
579      return static_cast<Expr *>(getReceiverPointer());
580
581    return 0;
582  }
583  const Expr *getInstanceReceiver() const {
584    return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver();
585  }
586
587  /// \brief Turn this message send into an instance message that
588  /// computes the receiver object with the given expression.
589  void setInstanceReceiver(Expr *rec) {
590    Kind = Instance;
591    setReceiverPointer(rec);
592  }
593
594  /// \brief Returns the type of a class message send, or NULL if the
595  /// message is not a class message.
596  QualType getClassReceiver() const {
597    if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo())
598      return TSInfo->getType();
599
600    return QualType();
601  }
602
603  /// \brief Returns a type-source information of a class message
604  /// send, or NULL if the message is not a class message.
605  TypeSourceInfo *getClassReceiverTypeInfo() const {
606    if (getReceiverKind() == Class)
607      return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer());
608    return 0;
609  }
610
611  void setClassReceiver(TypeSourceInfo *TSInfo) {
612    Kind = Class;
613    setReceiverPointer(TSInfo);
614  }
615
616  /// \brief Retrieve the location of the 'super' keyword for a class
617  /// or instance message to 'super', otherwise an invalid source location.
618  SourceLocation getSuperLoc() const {
619    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
620      return SuperLoc;
621
622    return SourceLocation();
623  }
624
625  /// \brief Retrieve the Objective-C interface to which this message
626  /// is being directed, if known.
627  ///
628  /// This routine cross-cuts all of the different kinds of message
629  /// sends to determine what the underlying (statically known) type
630  /// of the receiver will be; use \c getReceiverKind() to determine
631  /// whether the message is a class or an instance method, whether it
632  /// is a send to super or not, etc.
633  ///
634  /// \returns The Objective-C interface if known, otherwise NULL.
635  ObjCInterfaceDecl *getReceiverInterface() const;
636
637  /// \brief Retrieve the type referred to by 'super'.
638  ///
639  /// The returned type will either be an ObjCInterfaceType (for an
640  /// class message to super) or an ObjCObjectPointerType that refers
641  /// to a class (for an instance message to super);
642  QualType getSuperType() const {
643    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
644      return QualType::getFromOpaquePtr(getReceiverPointer());
645
646    return QualType();
647  }
648
649  void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) {
650    Kind = IsInstanceSuper? SuperInstance : SuperClass;
651    SuperLoc = Loc;
652    setReceiverPointer(T.getAsOpaquePtr());
653  }
654
655  Selector getSelector() const;
656
657  void setSelector(Selector S) {
658    HasMethod = false;
659    SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr());
660  }
661
662  const ObjCMethodDecl *getMethodDecl() const {
663    if (HasMethod)
664      return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod);
665
666    return 0;
667  }
668
669  ObjCMethodDecl *getMethodDecl() {
670    if (HasMethod)
671      return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod);
672
673    return 0;
674  }
675
676  void setMethodDecl(ObjCMethodDecl *MD) {
677    HasMethod = true;
678    SelectorOrMethod = reinterpret_cast<uintptr_t>(MD);
679  }
680
681  /// \brief Return the number of actual arguments in this message,
682  /// not counting the receiver.
683  unsigned getNumArgs() const { return NumArgs; }
684
685  /// \brief Retrieve the arguments to this message, not including the
686  /// receiver.
687  Stmt **getArgs() {
688    return reinterpret_cast<Stmt **>(this + 1) + 1;
689  }
690  const Stmt * const *getArgs() const {
691    return reinterpret_cast<const Stmt * const *>(this + 1) + 1;
692  }
693
694  /// getArg - Return the specified argument.
695  Expr *getArg(unsigned Arg) {
696    assert(Arg < NumArgs && "Arg access out of range!");
697    return cast<Expr>(getArgs()[Arg]);
698  }
699  const Expr *getArg(unsigned Arg) const {
700    assert(Arg < NumArgs && "Arg access out of range!");
701    return cast<Expr>(getArgs()[Arg]);
702  }
703  /// setArg - Set the specified argument.
704  void setArg(unsigned Arg, Expr *ArgExpr) {
705    assert(Arg < NumArgs && "Arg access out of range!");
706    getArgs()[Arg] = ArgExpr;
707  }
708
709  SourceLocation getLeftLoc() const { return LBracLoc; }
710  SourceLocation getRightLoc() const { return RBracLoc; }
711
712  void setLeftLoc(SourceLocation L) { LBracLoc = L; }
713  void setRightLoc(SourceLocation L) { RBracLoc = L; }
714
715  void setSourceRange(SourceRange R) {
716    LBracLoc = R.getBegin();
717    RBracLoc = R.getEnd();
718  }
719  virtual SourceRange getSourceRange() const {
720    return SourceRange(LBracLoc, RBracLoc);
721  }
722
723  static bool classof(const Stmt *T) {
724    return T->getStmtClass() == ObjCMessageExprClass;
725  }
726  static bool classof(const ObjCMessageExpr *) { return true; }
727
728  // Iterators
729  virtual child_iterator child_begin();
730  virtual child_iterator child_end();
731
732  typedef ExprIterator arg_iterator;
733  typedef ConstExprIterator const_arg_iterator;
734
735  arg_iterator arg_begin() { return getArgs(); }
736  arg_iterator arg_end()   { return getArgs() + NumArgs; }
737  const_arg_iterator arg_begin() const { return getArgs(); }
738  const_arg_iterator arg_end() const { return getArgs() + NumArgs; }
739};
740
741/// ObjCSuperExpr - Represents the "super" expression in Objective-C,
742/// which refers to the object on which the current method is executing.
743///
744/// FIXME: This class is intended for removal, once its remaining
745/// clients have been altered to represent "super" internally.
746class ObjCSuperExpr : public Expr {
747  SourceLocation Loc;
748public:
749  ObjCSuperExpr(SourceLocation L, QualType Type)
750    : Expr(ObjCSuperExprClass, Type, false, false), Loc(L) { }
751  explicit ObjCSuperExpr(EmptyShell Empty) : Expr(ObjCSuperExprClass, Empty) {}
752
753  SourceLocation getLoc() const { return Loc; }
754  void setLoc(SourceLocation L) { Loc = L; }
755
756  virtual SourceRange getSourceRange() const { return SourceRange(Loc); }
757
758  static bool classof(const Stmt *T) {
759    return T->getStmtClass() == ObjCSuperExprClass;
760  }
761  static bool classof(const ObjCSuperExpr *) { return true; }
762
763  // Iterators
764  virtual child_iterator child_begin();
765  virtual child_iterator child_end();
766};
767
768/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
769/// (similiar in spirit to MemberExpr).
770class ObjCIsaExpr : public Expr {
771  /// Base - the expression for the base object pointer.
772  Stmt *Base;
773
774  /// IsaMemberLoc - This is the location of the 'isa'.
775  SourceLocation IsaMemberLoc;
776
777  /// IsArrow - True if this is "X->F", false if this is "X.F".
778  bool IsArrow;
779public:
780  ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty)
781    : Expr(ObjCIsaExprClass, ty, /*TypeDependent=*/false,
782           base->isValueDependent()),
783      Base(base), IsaMemberLoc(l), IsArrow(isarrow) {}
784
785  /// \brief Build an empty expression.
786  explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { }
787
788  void setBase(Expr *E) { Base = E; }
789  Expr *getBase() const { return cast<Expr>(Base); }
790
791  bool isArrow() const { return IsArrow; }
792  void setArrow(bool A) { IsArrow = A; }
793
794  /// getMemberLoc - Return the location of the "member", in X->F, it is the
795  /// location of 'F'.
796  SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; }
797  void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; }
798
799  virtual SourceRange getSourceRange() const {
800    return SourceRange(getBase()->getLocStart(), IsaMemberLoc);
801  }
802
803  virtual SourceLocation getExprLoc() const { return IsaMemberLoc; }
804
805  static bool classof(const Stmt *T) {
806    return T->getStmtClass() == ObjCIsaExprClass;
807  }
808  static bool classof(const ObjCIsaExpr *) { return true; }
809
810  // Iterators
811  virtual child_iterator child_begin();
812  virtual child_iterator child_end();
813};
814
815}  // end namespace clang
816
817#endif
818