ExprObjC.h revision d162cf102449d817a35ae6755b102edcf9d4583b
1//===--- ExprObjC.h - Classes for representing ObjC expressions -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the ExprObjC interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_EXPROBJC_H
15#define LLVM_CLANG_AST_EXPROBJC_H
16
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/SelectorLocationsKind.h"
20#include "clang/Basic/IdentifierTable.h"
21
22namespace clang {
23  class IdentifierInfo;
24  class ASTContext;
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, VK_RValue, OK_Ordinary, false, false,
34           false, false),
35      String(SL), AtLoc(L) {}
36  explicit ObjCStringLiteral(EmptyShell Empty)
37    : Expr(ObjCStringLiteralClass, Empty) {}
38
39  StringLiteral *getString() { return cast<StringLiteral>(String); }
40  const StringLiteral *getString() const { return cast<StringLiteral>(String); }
41  void setString(StringLiteral *S) { String = S; }
42
43  SourceLocation getAtLoc() const { return AtLoc; }
44  void setAtLoc(SourceLocation L) { AtLoc = L; }
45
46  SourceRange getSourceRange() const {
47    return SourceRange(AtLoc, String->getLocEnd());
48  }
49
50  static bool classof(const Stmt *T) {
51    return T->getStmtClass() == ObjCStringLiteralClass;
52  }
53  static bool classof(const ObjCStringLiteral *) { return true; }
54
55  // Iterators
56  child_range children() { return child_range(&String, &String+1); }
57};
58
59/// ObjCEncodeExpr, used for @encode in Objective-C.  @encode has the same type
60/// and behavior as StringLiteral except that the string initializer is obtained
61/// from ASTContext with the encoding type as an argument.
62class ObjCEncodeExpr : public Expr {
63  TypeSourceInfo *EncodedType;
64  SourceLocation AtLoc, RParenLoc;
65public:
66  ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType,
67                 SourceLocation at, SourceLocation rp)
68    : Expr(ObjCEncodeExprClass, T, VK_LValue, OK_Ordinary,
69           EncodedType->getType()->isDependentType(),
70           EncodedType->getType()->isDependentType(),
71           EncodedType->getType()->isInstantiationDependentType(),
72           EncodedType->getType()->containsUnexpandedParameterPack()),
73      EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {}
74
75  explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){}
76
77
78  SourceLocation getAtLoc() const { return AtLoc; }
79  void setAtLoc(SourceLocation L) { AtLoc = L; }
80  SourceLocation getRParenLoc() const { return RParenLoc; }
81  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
82
83  QualType getEncodedType() const { return EncodedType->getType(); }
84
85  TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; }
86  void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) {
87    EncodedType = EncType;
88  }
89
90  SourceRange getSourceRange() const {
91    return SourceRange(AtLoc, RParenLoc);
92  }
93
94  static bool classof(const Stmt *T) {
95    return T->getStmtClass() == ObjCEncodeExprClass;
96  }
97  static bool classof(const ObjCEncodeExpr *) { return true; }
98
99  // Iterators
100  child_range children() { return child_range(); }
101};
102
103/// ObjCSelectorExpr used for @selector in Objective-C.
104class ObjCSelectorExpr : public Expr {
105  Selector SelName;
106  SourceLocation AtLoc, RParenLoc;
107public:
108  ObjCSelectorExpr(QualType T, Selector selInfo,
109                   SourceLocation at, SourceLocation rp)
110    : Expr(ObjCSelectorExprClass, T, VK_RValue, OK_Ordinary, false, false,
111           false, false),
112    SelName(selInfo), AtLoc(at), RParenLoc(rp){}
113  explicit ObjCSelectorExpr(EmptyShell Empty)
114   : Expr(ObjCSelectorExprClass, Empty) {}
115
116  Selector getSelector() const { return SelName; }
117  void setSelector(Selector S) { SelName = S; }
118
119  SourceLocation getAtLoc() const { return AtLoc; }
120  SourceLocation getRParenLoc() const { return RParenLoc; }
121  void setAtLoc(SourceLocation L) { AtLoc = L; }
122  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
123
124  SourceRange getSourceRange() const {
125    return SourceRange(AtLoc, RParenLoc);
126  }
127
128  /// getNumArgs - Return the number of actual arguments to this call.
129  unsigned getNumArgs() const { return SelName.getNumArgs(); }
130
131  static bool classof(const Stmt *T) {
132    return T->getStmtClass() == ObjCSelectorExprClass;
133  }
134  static bool classof(const ObjCSelectorExpr *) { return true; }
135
136  // Iterators
137  child_range children() { return child_range(); }
138};
139
140/// ObjCProtocolExpr used for protocol expression in Objective-C.  This is used
141/// as: @protocol(foo), as in:
142///   obj conformsToProtocol:@protocol(foo)]
143/// The return type is "Protocol*".
144class ObjCProtocolExpr : public Expr {
145  ObjCProtocolDecl *TheProtocol;
146  SourceLocation AtLoc, RParenLoc;
147public:
148  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
149                   SourceLocation at, SourceLocation rp)
150    : Expr(ObjCProtocolExprClass, T, VK_RValue, OK_Ordinary, false, false,
151           false, false),
152      TheProtocol(protocol), AtLoc(at), RParenLoc(rp) {}
153  explicit ObjCProtocolExpr(EmptyShell Empty)
154    : Expr(ObjCProtocolExprClass, Empty) {}
155
156  ObjCProtocolDecl *getProtocol() const { return TheProtocol; }
157  void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; }
158
159  SourceLocation getAtLoc() const { return AtLoc; }
160  SourceLocation getRParenLoc() const { return RParenLoc; }
161  void setAtLoc(SourceLocation L) { AtLoc = L; }
162  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
163
164  SourceRange getSourceRange() const {
165    return SourceRange(AtLoc, RParenLoc);
166  }
167
168  static bool classof(const Stmt *T) {
169    return T->getStmtClass() == ObjCProtocolExprClass;
170  }
171  static bool classof(const ObjCProtocolExpr *) { return true; }
172
173  // Iterators
174  child_range children() { return child_range(); }
175};
176
177/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
178class ObjCIvarRefExpr : public Expr {
179  ObjCIvarDecl *D;
180  Stmt *Base;
181  SourceLocation Loc;
182  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
183  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
184
185public:
186  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t,
187                  SourceLocation l, Expr *base,
188                  bool arrow = false, bool freeIvar = false) :
189    Expr(ObjCIvarRefExprClass, t, VK_LValue, OK_Ordinary,
190         /*TypeDependent=*/false, base->isValueDependent(),
191         base->isInstantiationDependent(),
192         base->containsUnexpandedParameterPack()),
193    D(d), Base(base), Loc(l), IsArrow(arrow), IsFreeIvar(freeIvar) {}
194
195  explicit ObjCIvarRefExpr(EmptyShell Empty)
196    : Expr(ObjCIvarRefExprClass, Empty) {}
197
198  ObjCIvarDecl *getDecl() { return D; }
199  const ObjCIvarDecl *getDecl() const { return D; }
200  void setDecl(ObjCIvarDecl *d) { D = d; }
201
202  const Expr *getBase() const { return cast<Expr>(Base); }
203  Expr *getBase() { return cast<Expr>(Base); }
204  void setBase(Expr * base) { Base = base; }
205
206  bool isArrow() const { return IsArrow; }
207  bool isFreeIvar() const { return IsFreeIvar; }
208  void setIsArrow(bool A) { IsArrow = A; }
209  void setIsFreeIvar(bool A) { IsFreeIvar = A; }
210
211  SourceLocation getLocation() const { return Loc; }
212  void setLocation(SourceLocation L) { Loc = L; }
213
214  SourceRange getSourceRange() const {
215    return isFreeIvar() ? SourceRange(Loc)
216    : SourceRange(getBase()->getLocStart(), Loc);
217  }
218
219  static bool classof(const Stmt *T) {
220    return T->getStmtClass() == ObjCIvarRefExprClass;
221  }
222  static bool classof(const ObjCIvarRefExpr *) { return true; }
223
224  // Iterators
225  child_range children() { return child_range(&Base, &Base+1); }
226};
227
228/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC
229/// property.
230class ObjCPropertyRefExpr : public Expr {
231private:
232  /// If the bool is true, this is an implicit property reference; the
233  /// pointer is an (optional) ObjCMethodDecl and Setter may be set.
234  /// if the bool is false, this is an explicit property reference;
235  /// the pointer is an ObjCPropertyDecl and Setter is always null.
236  llvm::PointerIntPair<NamedDecl*, 1, bool> PropertyOrGetter;
237  ObjCMethodDecl *Setter;
238
239  // FIXME: Maybe we should store the property identifier here,
240  // because it's not rederivable from the other data when there's an
241  // implicit property with no getter (because the 'foo' -> 'setFoo:'
242  // transformation is lossy on the first character).
243
244  SourceLocation IdLoc;
245
246  /// \brief When the receiver in property access is 'super', this is
247  /// the location of the 'super' keyword.  When it's an interface,
248  /// this is that interface.
249  SourceLocation ReceiverLoc;
250  llvm::PointerUnion3<Stmt*, const Type*, ObjCInterfaceDecl*> Receiver;
251
252public:
253  ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
254                      ExprValueKind VK, ExprObjectKind OK,
255                      SourceLocation l, Expr *base)
256    : Expr(ObjCPropertyRefExprClass, t, VK, OK,
257           /*TypeDependent=*/false, base->isValueDependent(),
258           base->isInstantiationDependent(),
259           base->containsUnexpandedParameterPack()),
260      PropertyOrGetter(PD, false), Setter(0),
261      IdLoc(l), ReceiverLoc(), Receiver(base) {
262    assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
263  }
264
265  ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
266                      ExprValueKind VK, ExprObjectKind OK,
267                      SourceLocation l, SourceLocation sl, QualType st)
268    : Expr(ObjCPropertyRefExprClass, t, VK, OK,
269           /*TypeDependent=*/false, false, st->isInstantiationDependentType(),
270           st->containsUnexpandedParameterPack()),
271      PropertyOrGetter(PD, false), Setter(0),
272      IdLoc(l), ReceiverLoc(sl), Receiver(st.getTypePtr()) {
273    assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
274  }
275
276  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
277                      QualType T, ExprValueKind VK, ExprObjectKind OK,
278                      SourceLocation IdLoc, Expr *Base)
279    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false,
280           Base->isValueDependent(), Base->isInstantiationDependent(),
281           Base->containsUnexpandedParameterPack()),
282      PropertyOrGetter(Getter, true), Setter(Setter),
283      IdLoc(IdLoc), ReceiverLoc(), Receiver(Base) {
284    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
285  }
286
287  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
288                      QualType T, ExprValueKind VK, ExprObjectKind OK,
289                      SourceLocation IdLoc,
290                      SourceLocation SuperLoc, QualType SuperTy)
291    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
292      PropertyOrGetter(Getter, true), Setter(Setter),
293      IdLoc(IdLoc), ReceiverLoc(SuperLoc), Receiver(SuperTy.getTypePtr()) {
294    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
295  }
296
297  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
298                      QualType T, ExprValueKind VK, ExprObjectKind OK,
299                      SourceLocation IdLoc,
300                      SourceLocation ReceiverLoc, ObjCInterfaceDecl *Receiver)
301    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
302      PropertyOrGetter(Getter, true), Setter(Setter),
303      IdLoc(IdLoc), ReceiverLoc(ReceiverLoc), Receiver(Receiver) {
304    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
305  }
306
307  explicit ObjCPropertyRefExpr(EmptyShell Empty)
308    : Expr(ObjCPropertyRefExprClass, Empty) {}
309
310  bool isImplicitProperty() const { return PropertyOrGetter.getInt(); }
311  bool isExplicitProperty() const { return !PropertyOrGetter.getInt(); }
312
313  ObjCPropertyDecl *getExplicitProperty() const {
314    assert(!isImplicitProperty());
315    return cast<ObjCPropertyDecl>(PropertyOrGetter.getPointer());
316  }
317
318  ObjCMethodDecl *getImplicitPropertyGetter() const {
319    assert(isImplicitProperty());
320    return cast_or_null<ObjCMethodDecl>(PropertyOrGetter.getPointer());
321  }
322
323  ObjCMethodDecl *getImplicitPropertySetter() const {
324    assert(isImplicitProperty());
325    return Setter;
326  }
327
328  Selector getGetterSelector() const {
329    if (isImplicitProperty())
330      return getImplicitPropertyGetter()->getSelector();
331    return getExplicitProperty()->getGetterName();
332  }
333
334  Selector getSetterSelector() const {
335    if (isImplicitProperty())
336      return getImplicitPropertySetter()->getSelector();
337    return getExplicitProperty()->getSetterName();
338  }
339
340  const Expr *getBase() const {
341    return cast<Expr>(Receiver.get<Stmt*>());
342  }
343  Expr *getBase() {
344    return cast<Expr>(Receiver.get<Stmt*>());
345  }
346
347  SourceLocation getLocation() const { return IdLoc; }
348
349  SourceLocation getReceiverLocation() const { return ReceiverLoc; }
350  QualType getSuperReceiverType() const {
351    return QualType(Receiver.get<const Type*>(), 0);
352  }
353  QualType getGetterResultType() const {
354    QualType ResultType;
355    if (isExplicitProperty()) {
356      const ObjCPropertyDecl *PDecl = getExplicitProperty();
357      if (const ObjCMethodDecl *Getter = PDecl->getGetterMethodDecl())
358        ResultType = Getter->getResultType();
359      else
360        ResultType = PDecl->getType();
361    } else {
362      const ObjCMethodDecl *Getter = getImplicitPropertyGetter();
363      if (Getter)
364        ResultType = Getter->getResultType(); // with reference!
365    }
366    return ResultType;
367  }
368
369  QualType getSetterArgType() const {
370    QualType ArgType;
371    if (isImplicitProperty()) {
372      const ObjCMethodDecl *Setter = getImplicitPropertySetter();
373      ObjCMethodDecl::param_const_iterator P = Setter->param_begin();
374      ArgType = (*P)->getType();
375    } else {
376      if (ObjCPropertyDecl *PDecl = getExplicitProperty())
377        if (const ObjCMethodDecl *Setter = PDecl->getSetterMethodDecl()) {
378          ObjCMethodDecl::param_const_iterator P = Setter->param_begin();
379          ArgType = (*P)->getType();
380        }
381      if (ArgType.isNull())
382        ArgType = getType();
383    }
384    return ArgType;
385  }
386
387  ObjCInterfaceDecl *getClassReceiver() const {
388    return Receiver.get<ObjCInterfaceDecl*>();
389  }
390  bool isObjectReceiver() const { return Receiver.is<Stmt*>(); }
391  bool isSuperReceiver() const { return Receiver.is<const Type*>(); }
392  bool isClassReceiver() const { return Receiver.is<ObjCInterfaceDecl*>(); }
393
394  SourceRange getSourceRange() const {
395    return SourceRange((isObjectReceiver() ? getBase()->getLocStart()
396                                           : getReceiverLocation()),
397                       IdLoc);
398  }
399
400  static bool classof(const Stmt *T) {
401    return T->getStmtClass() == ObjCPropertyRefExprClass;
402  }
403  static bool classof(const ObjCPropertyRefExpr *) { return true; }
404
405  // Iterators
406  child_range children() {
407    if (Receiver.is<Stmt*>()) {
408      Stmt **begin = reinterpret_cast<Stmt**>(&Receiver); // hack!
409      return child_range(begin, begin+1);
410    }
411    return child_range();
412  }
413
414private:
415  friend class ASTStmtReader;
416  void setExplicitProperty(ObjCPropertyDecl *D) {
417    PropertyOrGetter.setPointer(D);
418    PropertyOrGetter.setInt(false);
419    Setter = 0;
420  }
421  void setImplicitProperty(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter) {
422    PropertyOrGetter.setPointer(Getter);
423    PropertyOrGetter.setInt(true);
424    this->Setter = Setter;
425  }
426  void setBase(Expr *Base) { Receiver = Base; }
427  void setSuperReceiver(QualType T) { Receiver = T.getTypePtr(); }
428  void setClassReceiver(ObjCInterfaceDecl *D) { Receiver = D; }
429
430  void setLocation(SourceLocation L) { IdLoc = L; }
431  void setReceiverLocation(SourceLocation Loc) { ReceiverLoc = Loc; }
432};
433
434/// \brief An expression that sends a message to the given Objective-C
435/// object or class.
436///
437/// The following contains two message send expressions:
438///
439/// \code
440///   [[NSString alloc] initWithString:@"Hello"]
441/// \endcode
442///
443/// The innermost message send invokes the "alloc" class method on the
444/// NSString class, while the outermost message send invokes the
445/// "initWithString" instance method on the object returned from
446/// NSString's "alloc". In all, an Objective-C message send can take
447/// on four different (although related) forms:
448///
449///   1. Send to an object instance.
450///   2. Send to a class.
451///   3. Send to the superclass instance of the current class.
452///   4. Send to the superclass of the current class.
453///
454/// All four kinds of message sends are modeled by the ObjCMessageExpr
455/// class, and can be distinguished via \c getReceiverKind(). Example:
456///
457class ObjCMessageExpr : public Expr {
458  /// \brief Stores either the selector that this message is sending
459  /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer
460  /// referring to the method that we type-checked against.
461  uintptr_t SelectorOrMethod;
462
463  enum { NumArgsBitWidth = 16 };
464
465  /// \brief The number of arguments in the message send, not
466  /// including the receiver.
467  unsigned NumArgs : NumArgsBitWidth;
468
469  void setNumArgs(unsigned Num) {
470    assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
471    NumArgs = Num;
472  }
473
474  /// \brief The kind of message send this is, which is one of the
475  /// ReceiverKind values.
476  ///
477  /// We pad this out to a byte to avoid excessive masking and shifting.
478  unsigned Kind : 8;
479
480  /// \brief Whether we have an actual method prototype in \c
481  /// SelectorOrMethod.
482  ///
483  /// When non-zero, we have a method declaration; otherwise, we just
484  /// have a selector.
485  unsigned HasMethod : 1;
486
487  /// \brief Whether this message send is a "delegate init call",
488  /// i.e. a call of an init method on self from within an init method.
489  unsigned IsDelegateInitCall : 1;
490
491  /// \brief Whether this message send was implicitly generated by
492  /// the implementation rather than explicitly written by the user.
493  unsigned IsImplicit : 1;
494
495  /// \brief Whether the locations of the selector identifiers are in a
496  /// "standard" position, a enum SelectorLocationsKind.
497  unsigned SelLocsKind : 2;
498
499  /// \brief When the message expression is a send to 'super', this is
500  /// the location of the 'super' keyword.
501  SourceLocation SuperLoc;
502
503  /// \brief The source locations of the open and close square
504  /// brackets ('[' and ']', respectively).
505  SourceLocation LBracLoc, RBracLoc;
506
507  ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs)
508    : Expr(ObjCMessageExprClass, Empty), SelectorOrMethod(0), Kind(0),
509      HasMethod(0), IsDelegateInitCall(0), IsImplicit(0), SelLocsKind(0) {
510    setNumArgs(NumArgs);
511  }
512
513  ObjCMessageExpr(QualType T, ExprValueKind VK,
514                  SourceLocation LBracLoc,
515                  SourceLocation SuperLoc,
516                  bool IsInstanceSuper,
517                  QualType SuperType,
518                  Selector Sel,
519                  ArrayRef<SourceLocation> SelLocs,
520                  SelectorLocationsKind SelLocsK,
521                  ObjCMethodDecl *Method,
522                  ArrayRef<Expr *> Args,
523                  SourceLocation RBracLoc,
524                  bool isImplicit);
525  ObjCMessageExpr(QualType T, ExprValueKind VK,
526                  SourceLocation LBracLoc,
527                  TypeSourceInfo *Receiver,
528                  Selector Sel,
529                  ArrayRef<SourceLocation> SelLocs,
530                  SelectorLocationsKind SelLocsK,
531                  ObjCMethodDecl *Method,
532                  ArrayRef<Expr *> Args,
533                  SourceLocation RBracLoc,
534                  bool isImplicit);
535  ObjCMessageExpr(QualType T, ExprValueKind VK,
536                  SourceLocation LBracLoc,
537                  Expr *Receiver,
538                  Selector Sel,
539                  ArrayRef<SourceLocation> SelLocs,
540                  SelectorLocationsKind SelLocsK,
541                  ObjCMethodDecl *Method,
542                  ArrayRef<Expr *> Args,
543                  SourceLocation RBracLoc,
544                  bool isImplicit);
545
546  void initArgsAndSelLocs(ArrayRef<Expr *> Args,
547                          ArrayRef<SourceLocation> SelLocs,
548                          SelectorLocationsKind SelLocsK);
549
550  /// \brief Retrieve the pointer value of the message receiver.
551  void *getReceiverPointer() const {
552    return *const_cast<void **>(
553                             reinterpret_cast<const void * const*>(this + 1));
554  }
555
556  /// \brief Set the pointer value of the message receiver.
557  void setReceiverPointer(void *Value) {
558    *reinterpret_cast<void **>(this + 1) = Value;
559  }
560
561  SelectorLocationsKind getSelLocsKind() const {
562    return (SelectorLocationsKind)SelLocsKind;
563  }
564  bool hasStandardSelLocs() const {
565    return getSelLocsKind() != SelLoc_NonStandard;
566  }
567
568  /// \brief Get a pointer to the stored selector identifiers locations array.
569  /// No locations will be stored if HasStandardSelLocs is true.
570  SourceLocation *getStoredSelLocs() {
571    return reinterpret_cast<SourceLocation*>(getArgs() + getNumArgs());
572  }
573  const SourceLocation *getStoredSelLocs() const {
574    return reinterpret_cast<const SourceLocation*>(getArgs() + getNumArgs());
575  }
576
577  /// \brief Get the number of stored selector identifiers locations.
578  /// No locations will be stored if HasStandardSelLocs is true.
579  unsigned getNumStoredSelLocs() const {
580    if (hasStandardSelLocs())
581      return 0;
582    return getNumSelectorLocs();
583  }
584
585  static ObjCMessageExpr *alloc(ASTContext &C,
586                                ArrayRef<Expr *> Args,
587                                SourceLocation RBraceLoc,
588                                ArrayRef<SourceLocation> SelLocs,
589                                Selector Sel,
590                                SelectorLocationsKind &SelLocsK);
591  static ObjCMessageExpr *alloc(ASTContext &C,
592                                unsigned NumArgs,
593                                unsigned NumStoredSelLocs);
594
595public:
596  /// \brief The kind of receiver this message is sending to.
597  enum ReceiverKind {
598    /// \brief The receiver is a class.
599    Class = 0,
600    /// \brief The receiver is an object instance.
601    Instance,
602    /// \brief The receiver is a superclass.
603    SuperClass,
604    /// \brief The receiver is the instance of the superclass object.
605    SuperInstance
606  };
607
608  /// \brief Create a message send to super.
609  ///
610  /// \param Context The ASTContext in which this expression will be created.
611  ///
612  /// \param T The result type of this message.
613  ///
614  /// \param VK The value kind of this message.  A message returning
615  /// a l-value or r-value reference will be an l-value or x-value,
616  /// respectively.
617  ///
618  /// \param LBrac The location of the open square bracket '['.
619  ///
620  /// \param SuperLoc The location of the "super" keyword.
621  ///
622  /// \param IsInstanceSuper Whether this is an instance "super"
623  /// message (otherwise, it's a class "super" message).
624  ///
625  /// \param Sel The selector used to determine which method gets called.
626  ///
627  /// \param Method The Objective-C method against which this message
628  /// send was type-checked. May be NULL.
629  ///
630  /// \param Args The message send arguments.
631  ///
632  /// \param NumArgs The number of arguments.
633  ///
634  /// \param RBracLoc The location of the closing square bracket ']'.
635  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
636                                 ExprValueKind VK,
637                                 SourceLocation LBracLoc,
638                                 SourceLocation SuperLoc,
639                                 bool IsInstanceSuper,
640                                 QualType SuperType,
641                                 Selector Sel,
642                                 ArrayRef<SourceLocation> SelLocs,
643                                 ObjCMethodDecl *Method,
644                                 ArrayRef<Expr *> Args,
645                                 SourceLocation RBracLoc,
646                                 bool isImplicit);
647
648  /// \brief Create a class message send.
649  ///
650  /// \param Context The ASTContext in which this expression will be created.
651  ///
652  /// \param T The result type of this message.
653  ///
654  /// \param VK The value kind of this message.  A message returning
655  /// a l-value or r-value reference will be an l-value or x-value,
656  /// respectively.
657  ///
658  /// \param LBrac The location of the open square bracket '['.
659  ///
660  /// \param Receiver The type of the receiver, including
661  /// source-location information.
662  ///
663  /// \param Sel The selector used to determine which method gets called.
664  ///
665  /// \param Method The Objective-C method against which this message
666  /// send was type-checked. May be NULL.
667  ///
668  /// \param Args The message send arguments.
669  ///
670  /// \param NumArgs The number of arguments.
671  ///
672  /// \param RBracLoc The location of the closing square bracket ']'.
673  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
674                                 ExprValueKind VK,
675                                 SourceLocation LBracLoc,
676                                 TypeSourceInfo *Receiver,
677                                 Selector Sel,
678                                 ArrayRef<SourceLocation> SelLocs,
679                                 ObjCMethodDecl *Method,
680                                 ArrayRef<Expr *> Args,
681                                 SourceLocation RBracLoc,
682                                 bool isImplicit);
683
684  /// \brief Create an instance message send.
685  ///
686  /// \param Context The ASTContext in which this expression will be created.
687  ///
688  /// \param T The result type of this message.
689  ///
690  /// \param VK The value kind of this message.  A message returning
691  /// a l-value or r-value reference will be an l-value or x-value,
692  /// respectively.
693  ///
694  /// \param LBrac The location of the open square bracket '['.
695  ///
696  /// \param Receiver The expression used to produce the object that
697  /// will receive this message.
698  ///
699  /// \param Sel The selector used to determine which method gets called.
700  ///
701  /// \param Method The Objective-C method against which this message
702  /// send was type-checked. May be NULL.
703  ///
704  /// \param Args The message send arguments.
705  ///
706  /// \param NumArgs The number of arguments.
707  ///
708  /// \param RBracLoc The location of the closing square bracket ']'.
709  static ObjCMessageExpr *Create(ASTContext &Context, QualType T,
710                                 ExprValueKind VK,
711                                 SourceLocation LBracLoc,
712                                 Expr *Receiver,
713                                 Selector Sel,
714                                 ArrayRef<SourceLocation> SeLocs,
715                                 ObjCMethodDecl *Method,
716                                 ArrayRef<Expr *> Args,
717                                 SourceLocation RBracLoc,
718                                 bool isImplicit);
719
720  /// \brief Create an empty Objective-C message expression, to be
721  /// filled in by subsequent calls.
722  ///
723  /// \param Context The context in which the message send will be created.
724  ///
725  /// \param NumArgs The number of message arguments, not including
726  /// the receiver.
727  static ObjCMessageExpr *CreateEmpty(ASTContext &Context,
728                                      unsigned NumArgs,
729                                      unsigned NumStoredSelLocs);
730
731  /// \brief Indicates whether the message send was implicitly
732  /// generated by the implementation. If false, it was written explicitly
733  /// in the source code.
734  bool isImplicit() const { return IsImplicit; }
735
736  /// \brief Determine the kind of receiver that this message is being
737  /// sent to.
738  ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; }
739
740  /// \brief Source range of the receiver.
741  SourceRange getReceiverRange() const;
742
743  /// \brief Determine whether this is an instance message to either a
744  /// computed object or to super.
745  bool isInstanceMessage() const {
746    return getReceiverKind() == Instance || getReceiverKind() == SuperInstance;
747  }
748
749  /// \brief Determine whether this is an class message to either a
750  /// specified class or to super.
751  bool isClassMessage() const {
752    return getReceiverKind() == Class || getReceiverKind() == SuperClass;
753  }
754
755  /// \brief Returns the receiver of an instance message.
756  ///
757  /// \brief Returns the object expression for an instance message, or
758  /// NULL for a message that is not an instance message.
759  Expr *getInstanceReceiver() {
760    if (getReceiverKind() == Instance)
761      return static_cast<Expr *>(getReceiverPointer());
762
763    return 0;
764  }
765  const Expr *getInstanceReceiver() const {
766    return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver();
767  }
768
769  /// \brief Turn this message send into an instance message that
770  /// computes the receiver object with the given expression.
771  void setInstanceReceiver(Expr *rec) {
772    Kind = Instance;
773    setReceiverPointer(rec);
774  }
775
776  /// \brief Returns the type of a class message send, or NULL if the
777  /// message is not a class message.
778  QualType getClassReceiver() const {
779    if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo())
780      return TSInfo->getType();
781
782    return QualType();
783  }
784
785  /// \brief Returns a type-source information of a class message
786  /// send, or NULL if the message is not a class message.
787  TypeSourceInfo *getClassReceiverTypeInfo() const {
788    if (getReceiverKind() == Class)
789      return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer());
790    return 0;
791  }
792
793  void setClassReceiver(TypeSourceInfo *TSInfo) {
794    Kind = Class;
795    setReceiverPointer(TSInfo);
796  }
797
798  /// \brief Retrieve the location of the 'super' keyword for a class
799  /// or instance message to 'super', otherwise an invalid source location.
800  SourceLocation getSuperLoc() const {
801    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
802      return SuperLoc;
803
804    return SourceLocation();
805  }
806
807  /// \brief Retrieve the Objective-C interface to which this message
808  /// is being directed, if known.
809  ///
810  /// This routine cross-cuts all of the different kinds of message
811  /// sends to determine what the underlying (statically known) type
812  /// of the receiver will be; use \c getReceiverKind() to determine
813  /// whether the message is a class or an instance method, whether it
814  /// is a send to super or not, etc.
815  ///
816  /// \returns The Objective-C interface if known, otherwise NULL.
817  ObjCInterfaceDecl *getReceiverInterface() const;
818
819  /// \brief Retrieve the type referred to by 'super'.
820  ///
821  /// The returned type will either be an ObjCInterfaceType (for an
822  /// class message to super) or an ObjCObjectPointerType that refers
823  /// to a class (for an instance message to super);
824  QualType getSuperType() const {
825    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
826      return QualType::getFromOpaquePtr(getReceiverPointer());
827
828    return QualType();
829  }
830
831  void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) {
832    Kind = IsInstanceSuper? SuperInstance : SuperClass;
833    SuperLoc = Loc;
834    setReceiverPointer(T.getAsOpaquePtr());
835  }
836
837  Selector getSelector() const;
838
839  void setSelector(Selector S) {
840    HasMethod = false;
841    SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr());
842  }
843
844  const ObjCMethodDecl *getMethodDecl() const {
845    if (HasMethod)
846      return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod);
847
848    return 0;
849  }
850
851  ObjCMethodDecl *getMethodDecl() {
852    if (HasMethod)
853      return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod);
854
855    return 0;
856  }
857
858  void setMethodDecl(ObjCMethodDecl *MD) {
859    HasMethod = true;
860    SelectorOrMethod = reinterpret_cast<uintptr_t>(MD);
861  }
862
863  ObjCMethodFamily getMethodFamily() const {
864    if (HasMethod) return getMethodDecl()->getMethodFamily();
865    return getSelector().getMethodFamily();
866  }
867
868  /// \brief Return the number of actual arguments in this message,
869  /// not counting the receiver.
870  unsigned getNumArgs() const { return NumArgs; }
871
872  /// \brief Retrieve the arguments to this message, not including the
873  /// receiver.
874  Expr **getArgs() {
875    return reinterpret_cast<Expr **>(this + 1) + 1;
876  }
877  const Expr * const *getArgs() const {
878    return reinterpret_cast<const Expr * const *>(this + 1) + 1;
879  }
880
881  /// getArg - Return the specified argument.
882  Expr *getArg(unsigned Arg) {
883    assert(Arg < NumArgs && "Arg access out of range!");
884    return cast<Expr>(getArgs()[Arg]);
885  }
886  const Expr *getArg(unsigned Arg) const {
887    assert(Arg < NumArgs && "Arg access out of range!");
888    return cast<Expr>(getArgs()[Arg]);
889  }
890  /// setArg - Set the specified argument.
891  void setArg(unsigned Arg, Expr *ArgExpr) {
892    assert(Arg < NumArgs && "Arg access out of range!");
893    getArgs()[Arg] = ArgExpr;
894  }
895
896  /// isDelegateInitCall - Answers whether this message send has been
897  /// tagged as a "delegate init call", i.e. a call to a method in the
898  /// -init family on self from within an -init method implementation.
899  bool isDelegateInitCall() const { return IsDelegateInitCall; }
900  void setDelegateInitCall(bool isDelegate) { IsDelegateInitCall = isDelegate; }
901
902  SourceLocation getLeftLoc() const { return LBracLoc; }
903  SourceLocation getRightLoc() const { return RBracLoc; }
904
905  SourceLocation getSelectorStartLoc() const {
906    if (isImplicit())
907      return getLocStart();
908    return getSelectorLoc(0);
909  }
910  SourceLocation getSelectorLoc(unsigned Index) const {
911    assert(Index < getNumSelectorLocs() && "Index out of range!");
912    if (hasStandardSelLocs())
913      return getStandardSelectorLoc(Index, getSelector(),
914                                   getSelLocsKind() == SelLoc_StandardWithSpace,
915                               llvm::makeArrayRef(const_cast<Expr**>(getArgs()),
916                                                  getNumArgs()),
917                                   RBracLoc);
918    return getStoredSelLocs()[Index];
919  }
920
921  void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
922
923  unsigned getNumSelectorLocs() const {
924    if (isImplicit())
925      return 0;
926    Selector Sel = getSelector();
927    if (Sel.isUnarySelector())
928      return 1;
929    return Sel.getNumArgs();
930  }
931
932  void setSourceRange(SourceRange R) {
933    LBracLoc = R.getBegin();
934    RBracLoc = R.getEnd();
935  }
936  SourceRange getSourceRange() const {
937    return SourceRange(LBracLoc, RBracLoc);
938  }
939
940  static bool classof(const Stmt *T) {
941    return T->getStmtClass() == ObjCMessageExprClass;
942  }
943  static bool classof(const ObjCMessageExpr *) { return true; }
944
945  // Iterators
946  child_range children();
947
948  typedef ExprIterator arg_iterator;
949  typedef ConstExprIterator const_arg_iterator;
950
951  arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); }
952  arg_iterator arg_end()   {
953    return reinterpret_cast<Stmt **>(getArgs() + NumArgs);
954  }
955  const_arg_iterator arg_begin() const {
956    return reinterpret_cast<Stmt const * const*>(getArgs());
957  }
958  const_arg_iterator arg_end() const {
959    return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
960  }
961
962  friend class ASTStmtReader;
963  friend class ASTStmtWriter;
964};
965
966/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
967/// (similar in spirit to MemberExpr).
968class ObjCIsaExpr : public Expr {
969  /// Base - the expression for the base object pointer.
970  Stmt *Base;
971
972  /// IsaMemberLoc - This is the location of the 'isa'.
973  SourceLocation IsaMemberLoc;
974
975  /// IsArrow - True if this is "X->F", false if this is "X.F".
976  bool IsArrow;
977public:
978  ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, QualType ty)
979    : Expr(ObjCIsaExprClass, ty, VK_LValue, OK_Ordinary,
980           /*TypeDependent=*/false, base->isValueDependent(),
981           base->isInstantiationDependent(),
982           /*ContainsUnexpandedParameterPack=*/false),
983      Base(base), IsaMemberLoc(l), IsArrow(isarrow) {}
984
985  /// \brief Build an empty expression.
986  explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { }
987
988  void setBase(Expr *E) { Base = E; }
989  Expr *getBase() const { return cast<Expr>(Base); }
990
991  bool isArrow() const { return IsArrow; }
992  void setArrow(bool A) { IsArrow = A; }
993
994  /// getMemberLoc - Return the location of the "member", in X->F, it is the
995  /// location of 'F'.
996  SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; }
997  void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; }
998
999  SourceRange getSourceRange() const {
1000    return SourceRange(getBase()->getLocStart(), IsaMemberLoc);
1001  }
1002
1003  SourceLocation getExprLoc() const { return IsaMemberLoc; }
1004
1005  static bool classof(const Stmt *T) {
1006    return T->getStmtClass() == ObjCIsaExprClass;
1007  }
1008  static bool classof(const ObjCIsaExpr *) { return true; }
1009
1010  // Iterators
1011  child_range children() { return child_range(&Base, &Base+1); }
1012};
1013
1014
1015/// ObjCIndirectCopyRestoreExpr - Represents the passing of a function
1016/// argument by indirect copy-restore in ARC.  This is used to support
1017/// passing indirect arguments with the wrong lifetime, e.g. when
1018/// passing the address of a __strong local variable to an 'out'
1019/// parameter.  This expression kind is only valid in an "argument"
1020/// position to some sort of call expression.
1021///
1022/// The parameter must have type 'pointer to T', and the argument must
1023/// have type 'pointer to U', where T and U agree except possibly in
1024/// qualification.  If the argument value is null, then a null pointer
1025/// is passed;  otherwise it points to an object A, and:
1026/// 1. A temporary object B of type T is initialized, either by
1027///    zero-initialization (used when initializing an 'out' parameter)
1028///    or copy-initialization (used when initializing an 'inout'
1029///    parameter).
1030/// 2. The address of the temporary is passed to the function.
1031/// 3. If the call completes normally, A is move-assigned from B.
1032/// 4. Finally, A is destroyed immediately.
1033///
1034/// Currently 'T' must be a retainable object lifetime and must be
1035/// __autoreleasing;  this qualifier is ignored when initializing
1036/// the value.
1037class ObjCIndirectCopyRestoreExpr : public Expr {
1038  Stmt *Operand;
1039
1040  // unsigned ObjCIndirectCopyRestoreBits.ShouldCopy : 1;
1041
1042  friend class ASTReader;
1043  friend class ASTStmtReader;
1044
1045  void setShouldCopy(bool shouldCopy) {
1046    ObjCIndirectCopyRestoreExprBits.ShouldCopy = shouldCopy;
1047  }
1048
1049  explicit ObjCIndirectCopyRestoreExpr(EmptyShell Empty)
1050    : Expr(ObjCIndirectCopyRestoreExprClass, Empty) { }
1051
1052public:
1053  ObjCIndirectCopyRestoreExpr(Expr *operand, QualType type, bool shouldCopy)
1054    : Expr(ObjCIndirectCopyRestoreExprClass, type, VK_LValue, OK_Ordinary,
1055           operand->isTypeDependent(), operand->isValueDependent(),
1056           operand->isInstantiationDependent(),
1057           operand->containsUnexpandedParameterPack()),
1058      Operand(operand) {
1059    setShouldCopy(shouldCopy);
1060  }
1061
1062  Expr *getSubExpr() { return cast<Expr>(Operand); }
1063  const Expr *getSubExpr() const { return cast<Expr>(Operand); }
1064
1065  /// shouldCopy - True if we should do the 'copy' part of the
1066  /// copy-restore.  If false, the temporary will be zero-initialized.
1067  bool shouldCopy() const { return ObjCIndirectCopyRestoreExprBits.ShouldCopy; }
1068
1069  child_range children() { return child_range(&Operand, &Operand+1); }
1070
1071  // Source locations are determined by the subexpression.
1072  SourceRange getSourceRange() const { return Operand->getSourceRange(); }
1073  SourceLocation getExprLoc() const { return getSubExpr()->getExprLoc(); }
1074
1075  static bool classof(const Stmt *s) {
1076    return s->getStmtClass() == ObjCIndirectCopyRestoreExprClass;
1077  }
1078  static bool classof(const ObjCIndirectCopyRestoreExpr *) { return true; }
1079};
1080
1081/// \brief An Objective-C "bridged" cast expression, which casts between
1082/// Objective-C pointers and C pointers, transferring ownership in the process.
1083///
1084/// \code
1085/// NSString *str = (__bridge_transfer NSString *)CFCreateString();
1086/// \endcode
1087class ObjCBridgedCastExpr : public ExplicitCastExpr {
1088  SourceLocation LParenLoc;
1089  SourceLocation BridgeKeywordLoc;
1090  unsigned Kind : 2;
1091
1092  friend class ASTStmtReader;
1093  friend class ASTStmtWriter;
1094
1095public:
1096  ObjCBridgedCastExpr(SourceLocation LParenLoc, ObjCBridgeCastKind Kind,
1097                      CastKind CK, SourceLocation BridgeKeywordLoc,
1098                      TypeSourceInfo *TSInfo, Expr *Operand)
1099    : ExplicitCastExpr(ObjCBridgedCastExprClass, TSInfo->getType(), VK_RValue,
1100                       CK, Operand, 0, TSInfo),
1101      LParenLoc(LParenLoc), BridgeKeywordLoc(BridgeKeywordLoc), Kind(Kind) { }
1102
1103  /// \brief Construct an empty Objective-C bridged cast.
1104  explicit ObjCBridgedCastExpr(EmptyShell Shell)
1105    : ExplicitCastExpr(ObjCBridgedCastExprClass, Shell, 0) { }
1106
1107  SourceLocation getLParenLoc() const { return LParenLoc; }
1108
1109  /// \brief Determine which kind of bridge is being performed via this cast.
1110  ObjCBridgeCastKind getBridgeKind() const {
1111    return static_cast<ObjCBridgeCastKind>(Kind);
1112  }
1113
1114  /// \brief Retrieve the kind of bridge being performed as a string.
1115  StringRef getBridgeKindName() const;
1116
1117  /// \brief The location of the bridge keyword.
1118  SourceLocation getBridgeKeywordLoc() const { return BridgeKeywordLoc; }
1119
1120  SourceRange getSourceRange() const {
1121    return SourceRange(LParenLoc, getSubExpr()->getLocEnd());
1122  }
1123
1124  static bool classof(const Stmt *T) {
1125    return T->getStmtClass() == ObjCBridgedCastExprClass;
1126  }
1127  static bool classof(const ObjCBridgedCastExpr *) { return true; }
1128
1129};
1130
1131}  // end namespace clang
1132
1133#endif
1134