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