ExprObjC.h revision 280031
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#include "llvm/Support/Compiler.h"
22
23namespace clang {
24  class IdentifierInfo;
25  class ASTContext;
26
27/// ObjCStringLiteral, used for Objective-C string literals
28/// i.e. @"foo".
29class ObjCStringLiteral : public Expr {
30  Stmt *String;
31  SourceLocation AtLoc;
32public:
33  ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
34    : Expr(ObjCStringLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
35           false, false),
36      String(SL), AtLoc(L) {}
37  explicit ObjCStringLiteral(EmptyShell Empty)
38    : Expr(ObjCStringLiteralClass, Empty) {}
39
40  StringLiteral *getString() { return cast<StringLiteral>(String); }
41  const StringLiteral *getString() const { return cast<StringLiteral>(String); }
42  void setString(StringLiteral *S) { String = S; }
43
44  SourceLocation getAtLoc() const { return AtLoc; }
45  void setAtLoc(SourceLocation L) { AtLoc = L; }
46
47  SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
48  SourceLocation getLocEnd() const LLVM_READONLY { return String->getLocEnd(); }
49
50  static bool classof(const Stmt *T) {
51    return T->getStmtClass() == ObjCStringLiteralClass;
52  }
53
54  // Iterators
55  child_range children() { return child_range(&String, &String+1); }
56};
57
58/// ObjCBoolLiteralExpr - Objective-C Boolean Literal.
59///
60class ObjCBoolLiteralExpr : public Expr {
61  bool Value;
62  SourceLocation Loc;
63public:
64  ObjCBoolLiteralExpr(bool val, QualType Ty, SourceLocation l) :
65  Expr(ObjCBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
66       false, false), Value(val), Loc(l) {}
67
68  explicit ObjCBoolLiteralExpr(EmptyShell Empty)
69  : Expr(ObjCBoolLiteralExprClass, Empty) { }
70
71  bool getValue() const { return Value; }
72  void setValue(bool V) { Value = V; }
73
74  SourceLocation getLocStart() const LLVM_READONLY { return Loc; }
75  SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
76
77  SourceLocation getLocation() const { return Loc; }
78  void setLocation(SourceLocation L) { Loc = L; }
79
80  static bool classof(const Stmt *T) {
81    return T->getStmtClass() == ObjCBoolLiteralExprClass;
82  }
83
84  // Iterators
85  child_range children() { return child_range(); }
86};
87
88/// ObjCBoxedExpr - used for generalized expression boxing.
89/// as in: @(strdup("hello world")) or @(random())
90/// Also used for boxing non-parenthesized numeric literals;
91/// as in: @42 or \@true (c++/objc++) or \@__yes (c/objc).
92class ObjCBoxedExpr : public Expr {
93  Stmt *SubExpr;
94  ObjCMethodDecl *BoxingMethod;
95  SourceRange Range;
96public:
97  ObjCBoxedExpr(Expr *E, QualType T, ObjCMethodDecl *method,
98                     SourceRange R)
99  : Expr(ObjCBoxedExprClass, T, VK_RValue, OK_Ordinary,
100         E->isTypeDependent(), E->isValueDependent(),
101         E->isInstantiationDependent(), E->containsUnexpandedParameterPack()),
102         SubExpr(E), BoxingMethod(method), Range(R) {}
103  explicit ObjCBoxedExpr(EmptyShell Empty)
104  : Expr(ObjCBoxedExprClass, Empty) {}
105
106  Expr *getSubExpr() { return cast<Expr>(SubExpr); }
107  const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
108
109  ObjCMethodDecl *getBoxingMethod() const {
110    return BoxingMethod;
111  }
112
113  SourceLocation getAtLoc() const { return Range.getBegin(); }
114
115  SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
116  SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
117  SourceRange getSourceRange() const LLVM_READONLY {
118    return Range;
119  }
120
121  static bool classof(const Stmt *T) {
122    return T->getStmtClass() == ObjCBoxedExprClass;
123  }
124
125  // Iterators
126  child_range children() { return child_range(&SubExpr, &SubExpr+1); }
127
128  typedef ConstExprIterator const_arg_iterator;
129
130  const_arg_iterator arg_begin() const {
131    return reinterpret_cast<Stmt const * const*>(&SubExpr);
132  }
133  const_arg_iterator arg_end() const {
134    return reinterpret_cast<Stmt const * const*>(&SubExpr + 1);
135  }
136
137  friend class ASTStmtReader;
138};
139
140/// ObjCArrayLiteral - used for objective-c array containers; as in:
141/// @[@"Hello", NSApp, [NSNumber numberWithInt:42]];
142class ObjCArrayLiteral : public Expr {
143  unsigned NumElements;
144  SourceRange Range;
145  ObjCMethodDecl *ArrayWithObjectsMethod;
146
147  ObjCArrayLiteral(ArrayRef<Expr *> Elements,
148                   QualType T, ObjCMethodDecl * Method,
149                   SourceRange SR);
150
151  explicit ObjCArrayLiteral(EmptyShell Empty, unsigned NumElements)
152    : Expr(ObjCArrayLiteralClass, Empty), NumElements(NumElements) {}
153
154public:
155  static ObjCArrayLiteral *Create(const ASTContext &C,
156                                  ArrayRef<Expr *> Elements,
157                                  QualType T, ObjCMethodDecl * Method,
158                                  SourceRange SR);
159
160  static ObjCArrayLiteral *CreateEmpty(const ASTContext &C,
161                                       unsigned NumElements);
162
163  SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
164  SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
165  SourceRange getSourceRange() const LLVM_READONLY { return Range; }
166
167  static bool classof(const Stmt *T) {
168      return T->getStmtClass() == ObjCArrayLiteralClass;
169  }
170
171  /// \brief Retrieve elements of array of literals.
172  Expr **getElements() { return reinterpret_cast<Expr **>(this + 1); }
173
174  /// \brief Retrieve elements of array of literals.
175  const Expr * const *getElements() const {
176    return reinterpret_cast<const Expr * const*>(this + 1);
177  }
178
179  /// getNumElements - Return number of elements of objective-c array literal.
180  unsigned getNumElements() const { return NumElements; }
181
182    /// getExpr - Return the Expr at the specified index.
183  Expr *getElement(unsigned Index) {
184    assert((Index < NumElements) && "Arg access out of range!");
185    return cast<Expr>(getElements()[Index]);
186  }
187  const Expr *getElement(unsigned Index) const {
188    assert((Index < NumElements) && "Arg access out of range!");
189    return cast<Expr>(getElements()[Index]);
190  }
191
192  ObjCMethodDecl *getArrayWithObjectsMethod() const {
193    return ArrayWithObjectsMethod;
194  }
195
196  // Iterators
197  child_range children() {
198    return child_range((Stmt **)getElements(),
199                       (Stmt **)getElements() + NumElements);
200  }
201
202  friend class ASTStmtReader;
203};
204
205/// \brief An element in an Objective-C dictionary literal.
206///
207struct ObjCDictionaryElement {
208  /// \brief The key for the dictionary element.
209  Expr *Key;
210
211  /// \brief The value of the dictionary element.
212  Expr *Value;
213
214  /// \brief The location of the ellipsis, if this is a pack expansion.
215  SourceLocation EllipsisLoc;
216
217  /// \brief The number of elements this pack expansion will expand to, if
218  /// this is a pack expansion and is known.
219  Optional<unsigned> NumExpansions;
220
221  /// \brief Determines whether this dictionary element is a pack expansion.
222  bool isPackExpansion() const { return EllipsisLoc.isValid(); }
223};
224} // end namespace clang
225
226namespace llvm {
227template <> struct isPodLike<clang::ObjCDictionaryElement> : std::true_type {};
228}
229
230namespace clang {
231/// ObjCDictionaryLiteral - AST node to represent objective-c dictionary
232/// literals; as in:  @{@"name" : NSUserName(), @"date" : [NSDate date] };
233class ObjCDictionaryLiteral : public Expr {
234  /// \brief Key/value pair used to store the key and value of a given element.
235  ///
236  /// Objects of this type are stored directly after the expression.
237  struct KeyValuePair {
238    Expr *Key;
239    Expr *Value;
240  };
241
242  /// \brief Data that describes an element that is a pack expansion, used if any
243  /// of the elements in the dictionary literal are pack expansions.
244  struct ExpansionData {
245    /// \brief The location of the ellipsis, if this element is a pack
246    /// expansion.
247    SourceLocation EllipsisLoc;
248
249    /// \brief If non-zero, the number of elements that this pack
250    /// expansion will expand to (+1).
251    unsigned NumExpansionsPlusOne;
252  };
253
254  /// \brief The number of elements in this dictionary literal.
255  unsigned NumElements : 31;
256
257  /// \brief Determine whether this dictionary literal has any pack expansions.
258  ///
259  /// If the dictionary literal has pack expansions, then there will
260  /// be an array of pack expansion data following the array of
261  /// key/value pairs, which provide the locations of the ellipses (if
262  /// any) and number of elements in the expansion (if known). If
263  /// there are no pack expansions, we optimize away this storage.
264  unsigned HasPackExpansions : 1;
265
266  SourceRange Range;
267  ObjCMethodDecl *DictWithObjectsMethod;
268
269  ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
270                        bool HasPackExpansions,
271                        QualType T, ObjCMethodDecl *method,
272                        SourceRange SR);
273
274  explicit ObjCDictionaryLiteral(EmptyShell Empty, unsigned NumElements,
275                                 bool HasPackExpansions)
276    : Expr(ObjCDictionaryLiteralClass, Empty), NumElements(NumElements),
277      HasPackExpansions(HasPackExpansions) {}
278
279  KeyValuePair *getKeyValues() {
280    return reinterpret_cast<KeyValuePair *>(this + 1);
281  }
282
283  const KeyValuePair *getKeyValues() const {
284    return reinterpret_cast<const KeyValuePair *>(this + 1);
285  }
286
287  ExpansionData *getExpansionData() {
288    if (!HasPackExpansions)
289      return nullptr;
290
291    return reinterpret_cast<ExpansionData *>(getKeyValues() + NumElements);
292  }
293
294  const ExpansionData *getExpansionData() const {
295    if (!HasPackExpansions)
296      return nullptr;
297
298    return reinterpret_cast<const ExpansionData *>(getKeyValues()+NumElements);
299  }
300
301public:
302  static ObjCDictionaryLiteral *Create(const ASTContext &C,
303                                       ArrayRef<ObjCDictionaryElement> VK,
304                                       bool HasPackExpansions,
305                                       QualType T, ObjCMethodDecl *method,
306                                       SourceRange SR);
307
308  static ObjCDictionaryLiteral *CreateEmpty(const ASTContext &C,
309                                            unsigned NumElements,
310                                            bool HasPackExpansions);
311
312  /// getNumElements - Return number of elements of objective-c dictionary
313  /// literal.
314  unsigned getNumElements() const { return NumElements; }
315
316  ObjCDictionaryElement getKeyValueElement(unsigned Index) const {
317    assert((Index < NumElements) && "Arg access out of range!");
318    const KeyValuePair &KV = getKeyValues()[Index];
319    ObjCDictionaryElement Result = { KV.Key, KV.Value, SourceLocation(), None };
320    if (HasPackExpansions) {
321      const ExpansionData &Expansion = getExpansionData()[Index];
322      Result.EllipsisLoc = Expansion.EllipsisLoc;
323      if (Expansion.NumExpansionsPlusOne > 0)
324        Result.NumExpansions = Expansion.NumExpansionsPlusOne - 1;
325    }
326    return Result;
327  }
328
329  ObjCMethodDecl *getDictWithObjectsMethod() const
330    { return DictWithObjectsMethod; }
331
332  SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
333  SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
334  SourceRange getSourceRange() const LLVM_READONLY { return Range; }
335
336  static bool classof(const Stmt *T) {
337      return T->getStmtClass() == ObjCDictionaryLiteralClass;
338  }
339
340  // Iterators
341  child_range children() {
342    // Note: we're taking advantage of the layout of the KeyValuePair struct
343    // here. If that struct changes, this code will need to change as well.
344    return child_range(reinterpret_cast<Stmt **>(this + 1),
345                       reinterpret_cast<Stmt **>(this + 1) + NumElements * 2);
346  }
347
348  friend class ASTStmtReader;
349  friend class ASTStmtWriter;
350};
351
352
353/// ObjCEncodeExpr, used for \@encode in Objective-C.  \@encode has the same
354/// type and behavior as StringLiteral except that the string initializer is
355/// obtained from ASTContext with the encoding type as an argument.
356class ObjCEncodeExpr : public Expr {
357  TypeSourceInfo *EncodedType;
358  SourceLocation AtLoc, RParenLoc;
359public:
360  ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType,
361                 SourceLocation at, SourceLocation rp)
362    : Expr(ObjCEncodeExprClass, T, VK_LValue, OK_Ordinary,
363           EncodedType->getType()->isDependentType(),
364           EncodedType->getType()->isDependentType(),
365           EncodedType->getType()->isInstantiationDependentType(),
366           EncodedType->getType()->containsUnexpandedParameterPack()),
367      EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {}
368
369  explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){}
370
371
372  SourceLocation getAtLoc() const { return AtLoc; }
373  void setAtLoc(SourceLocation L) { AtLoc = L; }
374  SourceLocation getRParenLoc() const { return RParenLoc; }
375  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
376
377  QualType getEncodedType() const { return EncodedType->getType(); }
378
379  TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; }
380  void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) {
381    EncodedType = EncType;
382  }
383
384  SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
385  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
386
387  static bool classof(const Stmt *T) {
388    return T->getStmtClass() == ObjCEncodeExprClass;
389  }
390
391  // Iterators
392  child_range children() { return child_range(); }
393};
394
395/// ObjCSelectorExpr used for \@selector in Objective-C.
396class ObjCSelectorExpr : public Expr {
397  Selector SelName;
398  SourceLocation AtLoc, RParenLoc;
399public:
400  ObjCSelectorExpr(QualType T, Selector selInfo,
401                   SourceLocation at, SourceLocation rp)
402    : Expr(ObjCSelectorExprClass, T, VK_RValue, OK_Ordinary, false, false,
403           false, false),
404    SelName(selInfo), AtLoc(at), RParenLoc(rp){}
405  explicit ObjCSelectorExpr(EmptyShell Empty)
406   : Expr(ObjCSelectorExprClass, Empty) {}
407
408  Selector getSelector() const { return SelName; }
409  void setSelector(Selector S) { SelName = S; }
410
411  SourceLocation getAtLoc() const { return AtLoc; }
412  SourceLocation getRParenLoc() const { return RParenLoc; }
413  void setAtLoc(SourceLocation L) { AtLoc = L; }
414  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
415
416  SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
417  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
418
419  /// getNumArgs - Return the number of actual arguments to this call.
420  unsigned getNumArgs() const { return SelName.getNumArgs(); }
421
422  static bool classof(const Stmt *T) {
423    return T->getStmtClass() == ObjCSelectorExprClass;
424  }
425
426  // Iterators
427  child_range children() { return child_range(); }
428};
429
430/// ObjCProtocolExpr used for protocol expression in Objective-C.
431///
432/// This is used as: \@protocol(foo), as in:
433/// \code
434///   [obj conformsToProtocol:@protocol(foo)]
435/// \endcode
436///
437/// The return type is "Protocol*".
438class ObjCProtocolExpr : public Expr {
439  ObjCProtocolDecl *TheProtocol;
440  SourceLocation AtLoc, ProtoLoc, RParenLoc;
441public:
442  ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
443                 SourceLocation at, SourceLocation protoLoc, SourceLocation rp)
444    : Expr(ObjCProtocolExprClass, T, VK_RValue, OK_Ordinary, false, false,
445           false, false),
446      TheProtocol(protocol), AtLoc(at), ProtoLoc(protoLoc), RParenLoc(rp) {}
447  explicit ObjCProtocolExpr(EmptyShell Empty)
448    : Expr(ObjCProtocolExprClass, Empty) {}
449
450  ObjCProtocolDecl *getProtocol() const { return TheProtocol; }
451  void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; }
452
453  SourceLocation getProtocolIdLoc() const { return ProtoLoc; }
454  SourceLocation getAtLoc() const { return AtLoc; }
455  SourceLocation getRParenLoc() const { return RParenLoc; }
456  void setAtLoc(SourceLocation L) { AtLoc = L; }
457  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
458
459  SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
460  SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
461
462  static bool classof(const Stmt *T) {
463    return T->getStmtClass() == ObjCProtocolExprClass;
464  }
465
466  // Iterators
467  child_range children() { return child_range(); }
468
469  friend class ASTStmtReader;
470  friend class ASTStmtWriter;
471};
472
473/// ObjCIvarRefExpr - A reference to an ObjC instance variable.
474class ObjCIvarRefExpr : public Expr {
475  ObjCIvarDecl *D;
476  Stmt *Base;
477  SourceLocation Loc;
478  /// OpLoc - This is the location of '.' or '->'
479  SourceLocation OpLoc;
480
481  bool IsArrow:1;      // True if this is "X->F", false if this is "X.F".
482  bool IsFreeIvar:1;   // True if ivar reference has no base (self assumed).
483
484public:
485  ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t,
486                  SourceLocation l, SourceLocation oploc,
487                  Expr *base,
488                  bool arrow = false, bool freeIvar = false) :
489    Expr(ObjCIvarRefExprClass, t, VK_LValue,
490         d->isBitField() ? OK_BitField : OK_Ordinary,
491         /*TypeDependent=*/false, base->isValueDependent(),
492         base->isInstantiationDependent(),
493         base->containsUnexpandedParameterPack()),
494    D(d), Base(base), Loc(l), OpLoc(oploc),
495    IsArrow(arrow), IsFreeIvar(freeIvar) {}
496
497  explicit ObjCIvarRefExpr(EmptyShell Empty)
498    : Expr(ObjCIvarRefExprClass, Empty) {}
499
500  ObjCIvarDecl *getDecl() { return D; }
501  const ObjCIvarDecl *getDecl() const { return D; }
502  void setDecl(ObjCIvarDecl *d) { D = d; }
503
504  const Expr *getBase() const { return cast<Expr>(Base); }
505  Expr *getBase() { return cast<Expr>(Base); }
506  void setBase(Expr * base) { Base = base; }
507
508  bool isArrow() const { return IsArrow; }
509  bool isFreeIvar() const { return IsFreeIvar; }
510  void setIsArrow(bool A) { IsArrow = A; }
511  void setIsFreeIvar(bool A) { IsFreeIvar = A; }
512
513  SourceLocation getLocation() const { return Loc; }
514  void setLocation(SourceLocation L) { Loc = L; }
515
516  SourceLocation getLocStart() const LLVM_READONLY {
517    return isFreeIvar() ? Loc : getBase()->getLocStart();
518  }
519  SourceLocation getLocEnd() const LLVM_READONLY { return Loc; }
520
521  SourceLocation getOpLoc() const { return OpLoc; }
522  void setOpLoc(SourceLocation L) { OpLoc = L; }
523
524  static bool classof(const Stmt *T) {
525    return T->getStmtClass() == ObjCIvarRefExprClass;
526  }
527
528  // Iterators
529  child_range children() { return child_range(&Base, &Base+1); }
530};
531
532/// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC
533/// property.
534class ObjCPropertyRefExpr : public Expr {
535private:
536  /// If the bool is true, this is an implicit property reference; the
537  /// pointer is an (optional) ObjCMethodDecl and Setter may be set.
538  /// if the bool is false, this is an explicit property reference;
539  /// the pointer is an ObjCPropertyDecl and Setter is always null.
540  llvm::PointerIntPair<NamedDecl*, 1, bool> PropertyOrGetter;
541
542  /// \brief Indicates whether the property reference will result in a message
543  /// to the getter, the setter, or both.
544  /// This applies to both implicit and explicit property references.
545  enum MethodRefFlags {
546    MethodRef_None = 0,
547    MethodRef_Getter = 0x1,
548    MethodRef_Setter = 0x2
549  };
550
551  /// \brief Contains the Setter method pointer and MethodRefFlags bit flags.
552  llvm::PointerIntPair<ObjCMethodDecl *, 2, unsigned> SetterAndMethodRefFlags;
553
554  // FIXME: Maybe we should store the property identifier here,
555  // because it's not rederivable from the other data when there's an
556  // implicit property with no getter (because the 'foo' -> 'setFoo:'
557  // transformation is lossy on the first character).
558
559  SourceLocation IdLoc;
560
561  /// \brief When the receiver in property access is 'super', this is
562  /// the location of the 'super' keyword.  When it's an interface,
563  /// this is that interface.
564  SourceLocation ReceiverLoc;
565  llvm::PointerUnion3<Stmt*, const Type*, ObjCInterfaceDecl*> Receiver;
566
567public:
568  ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
569                      ExprValueKind VK, ExprObjectKind OK,
570                      SourceLocation l, Expr *base)
571    : Expr(ObjCPropertyRefExprClass, t, VK, OK,
572           /*TypeDependent=*/false, base->isValueDependent(),
573           base->isInstantiationDependent(),
574           base->containsUnexpandedParameterPack()),
575      PropertyOrGetter(PD, false), SetterAndMethodRefFlags(),
576      IdLoc(l), ReceiverLoc(), Receiver(base) {
577    assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
578  }
579
580  ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
581                      ExprValueKind VK, ExprObjectKind OK,
582                      SourceLocation l, SourceLocation sl, QualType st)
583    : Expr(ObjCPropertyRefExprClass, t, VK, OK,
584           /*TypeDependent=*/false, false, st->isInstantiationDependentType(),
585           st->containsUnexpandedParameterPack()),
586      PropertyOrGetter(PD, false), SetterAndMethodRefFlags(),
587      IdLoc(l), ReceiverLoc(sl), Receiver(st.getTypePtr()) {
588    assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
589  }
590
591  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
592                      QualType T, ExprValueKind VK, ExprObjectKind OK,
593                      SourceLocation IdLoc, Expr *Base)
594    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false,
595           Base->isValueDependent(), Base->isInstantiationDependent(),
596           Base->containsUnexpandedParameterPack()),
597      PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
598      IdLoc(IdLoc), ReceiverLoc(), Receiver(Base) {
599    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
600  }
601
602  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
603                      QualType T, ExprValueKind VK, ExprObjectKind OK,
604                      SourceLocation IdLoc,
605                      SourceLocation SuperLoc, QualType SuperTy)
606    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
607      PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
608      IdLoc(IdLoc), ReceiverLoc(SuperLoc), Receiver(SuperTy.getTypePtr()) {
609    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
610  }
611
612  ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
613                      QualType T, ExprValueKind VK, ExprObjectKind OK,
614                      SourceLocation IdLoc,
615                      SourceLocation ReceiverLoc, ObjCInterfaceDecl *Receiver)
616    : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
617      PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
618      IdLoc(IdLoc), ReceiverLoc(ReceiverLoc), Receiver(Receiver) {
619    assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
620  }
621
622  explicit ObjCPropertyRefExpr(EmptyShell Empty)
623    : Expr(ObjCPropertyRefExprClass, Empty) {}
624
625  bool isImplicitProperty() const { return PropertyOrGetter.getInt(); }
626  bool isExplicitProperty() const { return !PropertyOrGetter.getInt(); }
627
628  ObjCPropertyDecl *getExplicitProperty() const {
629    assert(!isImplicitProperty());
630    return cast<ObjCPropertyDecl>(PropertyOrGetter.getPointer());
631  }
632
633  ObjCMethodDecl *getImplicitPropertyGetter() const {
634    assert(isImplicitProperty());
635    return cast_or_null<ObjCMethodDecl>(PropertyOrGetter.getPointer());
636  }
637
638  ObjCMethodDecl *getImplicitPropertySetter() const {
639    assert(isImplicitProperty());
640    return SetterAndMethodRefFlags.getPointer();
641  }
642
643  Selector getGetterSelector() const {
644    if (isImplicitProperty())
645      return getImplicitPropertyGetter()->getSelector();
646    return getExplicitProperty()->getGetterName();
647  }
648
649  Selector getSetterSelector() const {
650    if (isImplicitProperty())
651      return getImplicitPropertySetter()->getSelector();
652    return getExplicitProperty()->getSetterName();
653  }
654
655  /// \brief True if the property reference will result in a message to the
656  /// getter.
657  /// This applies to both implicit and explicit property references.
658  bool isMessagingGetter() const {
659    return SetterAndMethodRefFlags.getInt() & MethodRef_Getter;
660  }
661
662  /// \brief True if the property reference will result in a message to the
663  /// setter.
664  /// This applies to both implicit and explicit property references.
665  bool isMessagingSetter() const {
666    return SetterAndMethodRefFlags.getInt() & MethodRef_Setter;
667  }
668
669  void setIsMessagingGetter(bool val = true) {
670    setMethodRefFlag(MethodRef_Getter, val);
671  }
672
673  void setIsMessagingSetter(bool val = true) {
674    setMethodRefFlag(MethodRef_Setter, val);
675  }
676
677  const Expr *getBase() const {
678    return cast<Expr>(Receiver.get<Stmt*>());
679  }
680  Expr *getBase() {
681    return cast<Expr>(Receiver.get<Stmt*>());
682  }
683
684  SourceLocation getLocation() const { return IdLoc; }
685
686  SourceLocation getReceiverLocation() const { return ReceiverLoc; }
687  QualType getSuperReceiverType() const {
688    return QualType(Receiver.get<const Type*>(), 0);
689  }
690  QualType getGetterResultType() const {
691    QualType ResultType;
692    if (isExplicitProperty()) {
693      const ObjCPropertyDecl *PDecl = getExplicitProperty();
694      if (const ObjCMethodDecl *Getter = PDecl->getGetterMethodDecl())
695        ResultType = Getter->getReturnType();
696      else
697        ResultType = PDecl->getType();
698    } else {
699      const ObjCMethodDecl *Getter = getImplicitPropertyGetter();
700      if (Getter)
701        ResultType = Getter->getReturnType(); // with reference!
702    }
703    return ResultType;
704  }
705
706  QualType getSetterArgType() const {
707    QualType ArgType;
708    if (isImplicitProperty()) {
709      const ObjCMethodDecl *Setter = getImplicitPropertySetter();
710      ObjCMethodDecl::param_const_iterator P = Setter->param_begin();
711      ArgType = (*P)->getType();
712    } else {
713      if (ObjCPropertyDecl *PDecl = getExplicitProperty())
714        if (const ObjCMethodDecl *Setter = PDecl->getSetterMethodDecl()) {
715          ObjCMethodDecl::param_const_iterator P = Setter->param_begin();
716          ArgType = (*P)->getType();
717        }
718      if (ArgType.isNull())
719        ArgType = getType();
720    }
721    return ArgType;
722  }
723
724  ObjCInterfaceDecl *getClassReceiver() const {
725    return Receiver.get<ObjCInterfaceDecl*>();
726  }
727  bool isObjectReceiver() const { return Receiver.is<Stmt*>(); }
728  bool isSuperReceiver() const { return Receiver.is<const Type*>(); }
729  bool isClassReceiver() const { return Receiver.is<ObjCInterfaceDecl*>(); }
730
731  SourceLocation getLocStart() const LLVM_READONLY {
732    return isObjectReceiver() ? getBase()->getLocStart() :getReceiverLocation();
733  }
734  SourceLocation getLocEnd() const LLVM_READONLY { return IdLoc; }
735
736  static bool classof(const Stmt *T) {
737    return T->getStmtClass() == ObjCPropertyRefExprClass;
738  }
739
740  // Iterators
741  child_range children() {
742    if (Receiver.is<Stmt*>()) {
743      Stmt **begin = reinterpret_cast<Stmt**>(&Receiver); // hack!
744      return child_range(begin, begin+1);
745    }
746    return child_range();
747  }
748
749private:
750  friend class ASTStmtReader;
751  friend class ASTStmtWriter;
752  void setExplicitProperty(ObjCPropertyDecl *D, unsigned methRefFlags) {
753    PropertyOrGetter.setPointer(D);
754    PropertyOrGetter.setInt(false);
755    SetterAndMethodRefFlags.setPointer(nullptr);
756    SetterAndMethodRefFlags.setInt(methRefFlags);
757  }
758  void setImplicitProperty(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
759                           unsigned methRefFlags) {
760    PropertyOrGetter.setPointer(Getter);
761    PropertyOrGetter.setInt(true);
762    SetterAndMethodRefFlags.setPointer(Setter);
763    SetterAndMethodRefFlags.setInt(methRefFlags);
764  }
765  void setBase(Expr *Base) { Receiver = Base; }
766  void setSuperReceiver(QualType T) { Receiver = T.getTypePtr(); }
767  void setClassReceiver(ObjCInterfaceDecl *D) { Receiver = D; }
768
769  void setLocation(SourceLocation L) { IdLoc = L; }
770  void setReceiverLocation(SourceLocation Loc) { ReceiverLoc = Loc; }
771
772  void setMethodRefFlag(MethodRefFlags flag, bool val) {
773    unsigned f = SetterAndMethodRefFlags.getInt();
774    if (val)
775      f |= flag;
776    else
777      f &= ~flag;
778    SetterAndMethodRefFlags.setInt(f);
779  }
780};
781
782/// ObjCSubscriptRefExpr - used for array and dictionary subscripting.
783/// array[4] = array[3]; dictionary[key] = dictionary[alt_key];
784///
785class ObjCSubscriptRefExpr : public Expr {
786  // Location of ']' in an indexing expression.
787  SourceLocation RBracket;
788  // array/dictionary base expression.
789  // for arrays, this is a numeric expression. For dictionaries, this is
790  // an objective-c object pointer expression.
791  enum { BASE, KEY, END_EXPR };
792  Stmt* SubExprs[END_EXPR];
793
794  ObjCMethodDecl *GetAtIndexMethodDecl;
795
796  // For immutable objects this is null. When ObjCSubscriptRefExpr is to read
797  // an indexed object this is null too.
798  ObjCMethodDecl *SetAtIndexMethodDecl;
799
800public:
801
802  ObjCSubscriptRefExpr(Expr *base, Expr *key, QualType T,
803                       ExprValueKind VK, ExprObjectKind OK,
804                       ObjCMethodDecl *getMethod,
805                       ObjCMethodDecl *setMethod, SourceLocation RB)
806    : Expr(ObjCSubscriptRefExprClass, T, VK, OK,
807           base->isTypeDependent() || key->isTypeDependent(),
808           base->isValueDependent() || key->isValueDependent(),
809           base->isInstantiationDependent() || key->isInstantiationDependent(),
810           (base->containsUnexpandedParameterPack() ||
811            key->containsUnexpandedParameterPack())),
812      RBracket(RB),
813  GetAtIndexMethodDecl(getMethod),
814  SetAtIndexMethodDecl(setMethod)
815    {SubExprs[BASE] = base; SubExprs[KEY] = key;}
816
817  explicit ObjCSubscriptRefExpr(EmptyShell Empty)
818    : Expr(ObjCSubscriptRefExprClass, Empty) {}
819
820  static ObjCSubscriptRefExpr *Create(const ASTContext &C,
821                                      Expr *base,
822                                      Expr *key, QualType T,
823                                      ObjCMethodDecl *getMethod,
824                                      ObjCMethodDecl *setMethod,
825                                      SourceLocation RB);
826
827  SourceLocation getRBracket() const { return RBracket; }
828  void setRBracket(SourceLocation RB) { RBracket = RB; }
829
830  SourceLocation getLocStart() const LLVM_READONLY {
831    return SubExprs[BASE]->getLocStart();
832  }
833  SourceLocation getLocEnd() const LLVM_READONLY { return RBracket; }
834
835  static bool classof(const Stmt *T) {
836    return T->getStmtClass() == ObjCSubscriptRefExprClass;
837  }
838
839  Expr *getBaseExpr() const { return cast<Expr>(SubExprs[BASE]); }
840  void setBaseExpr(Stmt *S) { SubExprs[BASE] = S; }
841
842  Expr *getKeyExpr() const { return cast<Expr>(SubExprs[KEY]); }
843  void setKeyExpr(Stmt *S) { SubExprs[KEY] = S; }
844
845  ObjCMethodDecl *getAtIndexMethodDecl() const {
846    return GetAtIndexMethodDecl;
847  }
848
849  ObjCMethodDecl *setAtIndexMethodDecl() const {
850    return SetAtIndexMethodDecl;
851  }
852
853  bool isArraySubscriptRefExpr() const {
854    return getKeyExpr()->getType()->isIntegralOrEnumerationType();
855  }
856
857  child_range children() {
858    return child_range(SubExprs, SubExprs+END_EXPR);
859  }
860private:
861  friend class ASTStmtReader;
862};
863
864
865/// \brief An expression that sends a message to the given Objective-C
866/// object or class.
867///
868/// The following contains two message send expressions:
869///
870/// \code
871///   [[NSString alloc] initWithString:@"Hello"]
872/// \endcode
873///
874/// The innermost message send invokes the "alloc" class method on the
875/// NSString class, while the outermost message send invokes the
876/// "initWithString" instance method on the object returned from
877/// NSString's "alloc". In all, an Objective-C message send can take
878/// on four different (although related) forms:
879///
880///   1. Send to an object instance.
881///   2. Send to a class.
882///   3. Send to the superclass instance of the current class.
883///   4. Send to the superclass of the current class.
884///
885/// All four kinds of message sends are modeled by the ObjCMessageExpr
886/// class, and can be distinguished via \c getReceiverKind(). Example:
887///
888class ObjCMessageExpr : public Expr {
889  /// \brief Stores either the selector that this message is sending
890  /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer
891  /// referring to the method that we type-checked against.
892  uintptr_t SelectorOrMethod;
893
894  enum { NumArgsBitWidth = 16 };
895
896  /// \brief The number of arguments in the message send, not
897  /// including the receiver.
898  unsigned NumArgs : NumArgsBitWidth;
899
900  void setNumArgs(unsigned Num) {
901    assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
902    NumArgs = Num;
903  }
904
905  /// \brief The kind of message send this is, which is one of the
906  /// ReceiverKind values.
907  ///
908  /// We pad this out to a byte to avoid excessive masking and shifting.
909  unsigned Kind : 8;
910
911  /// \brief Whether we have an actual method prototype in \c
912  /// SelectorOrMethod.
913  ///
914  /// When non-zero, we have a method declaration; otherwise, we just
915  /// have a selector.
916  unsigned HasMethod : 1;
917
918  /// \brief Whether this message send is a "delegate init call",
919  /// i.e. a call of an init method on self from within an init method.
920  unsigned IsDelegateInitCall : 1;
921
922  /// \brief Whether this message send was implicitly generated by
923  /// the implementation rather than explicitly written by the user.
924  unsigned IsImplicit : 1;
925
926  /// \brief Whether the locations of the selector identifiers are in a
927  /// "standard" position, a enum SelectorLocationsKind.
928  unsigned SelLocsKind : 2;
929
930  /// \brief When the message expression is a send to 'super', this is
931  /// the location of the 'super' keyword.
932  SourceLocation SuperLoc;
933
934  /// \brief The source locations of the open and close square
935  /// brackets ('[' and ']', respectively).
936  SourceLocation LBracLoc, RBracLoc;
937
938  ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs)
939    : Expr(ObjCMessageExprClass, Empty), SelectorOrMethod(0), Kind(0),
940      HasMethod(0), IsDelegateInitCall(0), IsImplicit(0), SelLocsKind(0) {
941    setNumArgs(NumArgs);
942  }
943
944  ObjCMessageExpr(QualType T, ExprValueKind VK,
945                  SourceLocation LBracLoc,
946                  SourceLocation SuperLoc,
947                  bool IsInstanceSuper,
948                  QualType SuperType,
949                  Selector Sel,
950                  ArrayRef<SourceLocation> SelLocs,
951                  SelectorLocationsKind SelLocsK,
952                  ObjCMethodDecl *Method,
953                  ArrayRef<Expr *> Args,
954                  SourceLocation RBracLoc,
955                  bool isImplicit);
956  ObjCMessageExpr(QualType T, ExprValueKind VK,
957                  SourceLocation LBracLoc,
958                  TypeSourceInfo *Receiver,
959                  Selector Sel,
960                  ArrayRef<SourceLocation> SelLocs,
961                  SelectorLocationsKind SelLocsK,
962                  ObjCMethodDecl *Method,
963                  ArrayRef<Expr *> Args,
964                  SourceLocation RBracLoc,
965                  bool isImplicit);
966  ObjCMessageExpr(QualType T, ExprValueKind VK,
967                  SourceLocation LBracLoc,
968                  Expr *Receiver,
969                  Selector Sel,
970                  ArrayRef<SourceLocation> SelLocs,
971                  SelectorLocationsKind SelLocsK,
972                  ObjCMethodDecl *Method,
973                  ArrayRef<Expr *> Args,
974                  SourceLocation RBracLoc,
975                  bool isImplicit);
976
977  void initArgsAndSelLocs(ArrayRef<Expr *> Args,
978                          ArrayRef<SourceLocation> SelLocs,
979                          SelectorLocationsKind SelLocsK);
980
981  /// \brief Retrieve the pointer value of the message receiver.
982  void *getReceiverPointer() const {
983    return *const_cast<void **>(
984                             reinterpret_cast<const void * const*>(this + 1));
985  }
986
987  /// \brief Set the pointer value of the message receiver.
988  void setReceiverPointer(void *Value) {
989    *reinterpret_cast<void **>(this + 1) = Value;
990  }
991
992  SelectorLocationsKind getSelLocsKind() const {
993    return (SelectorLocationsKind)SelLocsKind;
994  }
995  bool hasStandardSelLocs() const {
996    return getSelLocsKind() != SelLoc_NonStandard;
997  }
998
999  /// \brief Get a pointer to the stored selector identifiers locations array.
1000  /// No locations will be stored if HasStandardSelLocs is true.
1001  SourceLocation *getStoredSelLocs() {
1002    return reinterpret_cast<SourceLocation*>(getArgs() + getNumArgs());
1003  }
1004  const SourceLocation *getStoredSelLocs() const {
1005    return reinterpret_cast<const SourceLocation*>(getArgs() + getNumArgs());
1006  }
1007
1008  /// \brief Get the number of stored selector identifiers locations.
1009  /// No locations will be stored if HasStandardSelLocs is true.
1010  unsigned getNumStoredSelLocs() const {
1011    if (hasStandardSelLocs())
1012      return 0;
1013    return getNumSelectorLocs();
1014  }
1015
1016  static ObjCMessageExpr *alloc(const ASTContext &C,
1017                                ArrayRef<Expr *> Args,
1018                                SourceLocation RBraceLoc,
1019                                ArrayRef<SourceLocation> SelLocs,
1020                                Selector Sel,
1021                                SelectorLocationsKind &SelLocsK);
1022  static ObjCMessageExpr *alloc(const ASTContext &C,
1023                                unsigned NumArgs,
1024                                unsigned NumStoredSelLocs);
1025
1026public:
1027  /// \brief The kind of receiver this message is sending to.
1028  enum ReceiverKind {
1029    /// \brief The receiver is a class.
1030    Class = 0,
1031    /// \brief The receiver is an object instance.
1032    Instance,
1033    /// \brief The receiver is a superclass.
1034    SuperClass,
1035    /// \brief The receiver is the instance of the superclass object.
1036    SuperInstance
1037  };
1038
1039  /// \brief Create a message send to super.
1040  ///
1041  /// \param Context The ASTContext in which this expression will be created.
1042  ///
1043  /// \param T The result type of this message.
1044  ///
1045  /// \param VK The value kind of this message.  A message returning
1046  /// a l-value or r-value reference will be an l-value or x-value,
1047  /// respectively.
1048  ///
1049  /// \param LBracLoc The location of the open square bracket '['.
1050  ///
1051  /// \param SuperLoc The location of the "super" keyword.
1052  ///
1053  /// \param IsInstanceSuper Whether this is an instance "super"
1054  /// message (otherwise, it's a class "super" message).
1055  ///
1056  /// \param Sel The selector used to determine which method gets called.
1057  ///
1058  /// \param Method The Objective-C method against which this message
1059  /// send was type-checked. May be NULL.
1060  ///
1061  /// \param Args The message send arguments.
1062  ///
1063  /// \param RBracLoc The location of the closing square bracket ']'.
1064  static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1065                                 ExprValueKind VK,
1066                                 SourceLocation LBracLoc,
1067                                 SourceLocation SuperLoc,
1068                                 bool IsInstanceSuper,
1069                                 QualType SuperType,
1070                                 Selector Sel,
1071                                 ArrayRef<SourceLocation> SelLocs,
1072                                 ObjCMethodDecl *Method,
1073                                 ArrayRef<Expr *> Args,
1074                                 SourceLocation RBracLoc,
1075                                 bool isImplicit);
1076
1077  /// \brief Create a class message send.
1078  ///
1079  /// \param Context The ASTContext in which this expression will be created.
1080  ///
1081  /// \param T The result type of this message.
1082  ///
1083  /// \param VK The value kind of this message.  A message returning
1084  /// a l-value or r-value reference will be an l-value or x-value,
1085  /// respectively.
1086  ///
1087  /// \param LBracLoc The location of the open square bracket '['.
1088  ///
1089  /// \param Receiver The type of the receiver, including
1090  /// source-location information.
1091  ///
1092  /// \param Sel The selector used to determine which method gets called.
1093  ///
1094  /// \param Method The Objective-C method against which this message
1095  /// send was type-checked. May be NULL.
1096  ///
1097  /// \param Args The message send arguments.
1098  ///
1099  /// \param RBracLoc The location of the closing square bracket ']'.
1100  static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1101                                 ExprValueKind VK,
1102                                 SourceLocation LBracLoc,
1103                                 TypeSourceInfo *Receiver,
1104                                 Selector Sel,
1105                                 ArrayRef<SourceLocation> SelLocs,
1106                                 ObjCMethodDecl *Method,
1107                                 ArrayRef<Expr *> Args,
1108                                 SourceLocation RBracLoc,
1109                                 bool isImplicit);
1110
1111  /// \brief Create an instance message send.
1112  ///
1113  /// \param Context The ASTContext in which this expression will be created.
1114  ///
1115  /// \param T The result type of this message.
1116  ///
1117  /// \param VK The value kind of this message.  A message returning
1118  /// a l-value or r-value reference will be an l-value or x-value,
1119  /// respectively.
1120  ///
1121  /// \param LBracLoc The location of the open square bracket '['.
1122  ///
1123  /// \param Receiver The expression used to produce the object that
1124  /// will receive this message.
1125  ///
1126  /// \param Sel The selector used to determine which method gets called.
1127  ///
1128  /// \param Method The Objective-C method against which this message
1129  /// send was type-checked. May be NULL.
1130  ///
1131  /// \param Args The message send arguments.
1132  ///
1133  /// \param RBracLoc The location of the closing square bracket ']'.
1134  static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1135                                 ExprValueKind VK,
1136                                 SourceLocation LBracLoc,
1137                                 Expr *Receiver,
1138                                 Selector Sel,
1139                                 ArrayRef<SourceLocation> SeLocs,
1140                                 ObjCMethodDecl *Method,
1141                                 ArrayRef<Expr *> Args,
1142                                 SourceLocation RBracLoc,
1143                                 bool isImplicit);
1144
1145  /// \brief Create an empty Objective-C message expression, to be
1146  /// filled in by subsequent calls.
1147  ///
1148  /// \param Context The context in which the message send will be created.
1149  ///
1150  /// \param NumArgs The number of message arguments, not including
1151  /// the receiver.
1152  static ObjCMessageExpr *CreateEmpty(const ASTContext &Context,
1153                                      unsigned NumArgs,
1154                                      unsigned NumStoredSelLocs);
1155
1156  /// \brief Indicates whether the message send was implicitly
1157  /// generated by the implementation. If false, it was written explicitly
1158  /// in the source code.
1159  bool isImplicit() const { return IsImplicit; }
1160
1161  /// \brief Determine the kind of receiver that this message is being
1162  /// sent to.
1163  ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; }
1164
1165  /// \brief Source range of the receiver.
1166  SourceRange getReceiverRange() const;
1167
1168  /// \brief Determine whether this is an instance message to either a
1169  /// computed object or to super.
1170  bool isInstanceMessage() const {
1171    return getReceiverKind() == Instance || getReceiverKind() == SuperInstance;
1172  }
1173
1174  /// \brief Determine whether this is an class message to either a
1175  /// specified class or to super.
1176  bool isClassMessage() const {
1177    return getReceiverKind() == Class || getReceiverKind() == SuperClass;
1178  }
1179
1180  /// \brief Returns the object expression (receiver) for an instance message,
1181  /// or null for a message that is not an instance message.
1182  Expr *getInstanceReceiver() {
1183    if (getReceiverKind() == Instance)
1184      return static_cast<Expr *>(getReceiverPointer());
1185
1186    return nullptr;
1187  }
1188  const Expr *getInstanceReceiver() const {
1189    return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver();
1190  }
1191
1192  /// \brief Turn this message send into an instance message that
1193  /// computes the receiver object with the given expression.
1194  void setInstanceReceiver(Expr *rec) {
1195    Kind = Instance;
1196    setReceiverPointer(rec);
1197  }
1198
1199  /// \brief Returns the type of a class message send, or NULL if the
1200  /// message is not a class message.
1201  QualType getClassReceiver() const {
1202    if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo())
1203      return TSInfo->getType();
1204
1205    return QualType();
1206  }
1207
1208  /// \brief Returns a type-source information of a class message
1209  /// send, or NULL if the message is not a class message.
1210  TypeSourceInfo *getClassReceiverTypeInfo() const {
1211    if (getReceiverKind() == Class)
1212      return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer());
1213    return nullptr;
1214  }
1215
1216  void setClassReceiver(TypeSourceInfo *TSInfo) {
1217    Kind = Class;
1218    setReceiverPointer(TSInfo);
1219  }
1220
1221  /// \brief Retrieve the location of the 'super' keyword for a class
1222  /// or instance message to 'super', otherwise an invalid source location.
1223  SourceLocation getSuperLoc() const {
1224    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
1225      return SuperLoc;
1226
1227    return SourceLocation();
1228  }
1229
1230  /// \brief Retrieve the receiver type to which this message is being directed.
1231  ///
1232  /// This routine cross-cuts all of the different kinds of message
1233  /// sends to determine what the underlying (statically known) type
1234  /// of the receiver will be; use \c getReceiverKind() to determine
1235  /// whether the message is a class or an instance method, whether it
1236  /// is a send to super or not, etc.
1237  ///
1238  /// \returns The type of the receiver.
1239  QualType getReceiverType() const;
1240
1241  /// \brief Retrieve the Objective-C interface to which this message
1242  /// is being directed, if known.
1243  ///
1244  /// This routine cross-cuts all of the different kinds of message
1245  /// sends to determine what the underlying (statically known) type
1246  /// of the receiver will be; use \c getReceiverKind() to determine
1247  /// whether the message is a class or an instance method, whether it
1248  /// is a send to super or not, etc.
1249  ///
1250  /// \returns The Objective-C interface if known, otherwise NULL.
1251  ObjCInterfaceDecl *getReceiverInterface() const;
1252
1253  /// \brief Retrieve the type referred to by 'super'.
1254  ///
1255  /// The returned type will either be an ObjCInterfaceType (for an
1256  /// class message to super) or an ObjCObjectPointerType that refers
1257  /// to a class (for an instance message to super);
1258  QualType getSuperType() const {
1259    if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
1260      return QualType::getFromOpaquePtr(getReceiverPointer());
1261
1262    return QualType();
1263  }
1264
1265  void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) {
1266    Kind = IsInstanceSuper? SuperInstance : SuperClass;
1267    SuperLoc = Loc;
1268    setReceiverPointer(T.getAsOpaquePtr());
1269  }
1270
1271  Selector getSelector() const;
1272
1273  void setSelector(Selector S) {
1274    HasMethod = false;
1275    SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr());
1276  }
1277
1278  const ObjCMethodDecl *getMethodDecl() const {
1279    if (HasMethod)
1280      return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod);
1281
1282    return nullptr;
1283  }
1284
1285  ObjCMethodDecl *getMethodDecl() {
1286    if (HasMethod)
1287      return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod);
1288
1289    return nullptr;
1290  }
1291
1292  void setMethodDecl(ObjCMethodDecl *MD) {
1293    HasMethod = true;
1294    SelectorOrMethod = reinterpret_cast<uintptr_t>(MD);
1295  }
1296
1297  ObjCMethodFamily getMethodFamily() const {
1298    if (HasMethod) return getMethodDecl()->getMethodFamily();
1299    return getSelector().getMethodFamily();
1300  }
1301
1302  /// \brief Return the number of actual arguments in this message,
1303  /// not counting the receiver.
1304  unsigned getNumArgs() const { return NumArgs; }
1305
1306  /// \brief Retrieve the arguments to this message, not including the
1307  /// receiver.
1308  Expr **getArgs() {
1309    return reinterpret_cast<Expr **>(this + 1) + 1;
1310  }
1311  const Expr * const *getArgs() const {
1312    return reinterpret_cast<const Expr * const *>(this + 1) + 1;
1313  }
1314
1315  /// getArg - Return the specified argument.
1316  Expr *getArg(unsigned Arg) {
1317    assert(Arg < NumArgs && "Arg access out of range!");
1318    return cast<Expr>(getArgs()[Arg]);
1319  }
1320  const Expr *getArg(unsigned Arg) const {
1321    assert(Arg < NumArgs && "Arg access out of range!");
1322    return cast<Expr>(getArgs()[Arg]);
1323  }
1324  /// setArg - Set the specified argument.
1325  void setArg(unsigned Arg, Expr *ArgExpr) {
1326    assert(Arg < NumArgs && "Arg access out of range!");
1327    getArgs()[Arg] = ArgExpr;
1328  }
1329
1330  /// isDelegateInitCall - Answers whether this message send has been
1331  /// tagged as a "delegate init call", i.e. a call to a method in the
1332  /// -init family on self from within an -init method implementation.
1333  bool isDelegateInitCall() const { return IsDelegateInitCall; }
1334  void setDelegateInitCall(bool isDelegate) { IsDelegateInitCall = isDelegate; }
1335
1336  SourceLocation getLeftLoc() const { return LBracLoc; }
1337  SourceLocation getRightLoc() const { return RBracLoc; }
1338
1339  SourceLocation getSelectorStartLoc() const {
1340    if (isImplicit())
1341      return getLocStart();
1342    return getSelectorLoc(0);
1343  }
1344  SourceLocation getSelectorLoc(unsigned Index) const {
1345    assert(Index < getNumSelectorLocs() && "Index out of range!");
1346    if (hasStandardSelLocs())
1347      return getStandardSelectorLoc(Index, getSelector(),
1348                                   getSelLocsKind() == SelLoc_StandardWithSpace,
1349                               llvm::makeArrayRef(const_cast<Expr**>(getArgs()),
1350                                                  getNumArgs()),
1351                                   RBracLoc);
1352    return getStoredSelLocs()[Index];
1353  }
1354
1355  void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
1356
1357  unsigned getNumSelectorLocs() const {
1358    if (isImplicit())
1359      return 0;
1360    Selector Sel = getSelector();
1361    if (Sel.isUnarySelector())
1362      return 1;
1363    return Sel.getNumArgs();
1364  }
1365
1366  void setSourceRange(SourceRange R) {
1367    LBracLoc = R.getBegin();
1368    RBracLoc = R.getEnd();
1369  }
1370  SourceLocation getLocStart() const LLVM_READONLY { return LBracLoc; }
1371  SourceLocation getLocEnd() const LLVM_READONLY { return RBracLoc; }
1372
1373  static bool classof(const Stmt *T) {
1374    return T->getStmtClass() == ObjCMessageExprClass;
1375  }
1376
1377  // Iterators
1378  child_range children();
1379
1380  typedef ExprIterator arg_iterator;
1381  typedef ConstExprIterator const_arg_iterator;
1382
1383  arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); }
1384  arg_iterator arg_end()   {
1385    return reinterpret_cast<Stmt **>(getArgs() + NumArgs);
1386  }
1387  const_arg_iterator arg_begin() const {
1388    return reinterpret_cast<Stmt const * const*>(getArgs());
1389  }
1390  const_arg_iterator arg_end() const {
1391    return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
1392  }
1393
1394  friend class ASTStmtReader;
1395  friend class ASTStmtWriter;
1396};
1397
1398/// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
1399/// (similar in spirit to MemberExpr).
1400class ObjCIsaExpr : public Expr {
1401  /// Base - the expression for the base object pointer.
1402  Stmt *Base;
1403
1404  /// IsaMemberLoc - This is the location of the 'isa'.
1405  SourceLocation IsaMemberLoc;
1406
1407  /// OpLoc - This is the location of '.' or '->'
1408  SourceLocation OpLoc;
1409
1410  /// IsArrow - True if this is "X->F", false if this is "X.F".
1411  bool IsArrow;
1412public:
1413  ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, SourceLocation oploc,
1414              QualType ty)
1415    : Expr(ObjCIsaExprClass, ty, VK_LValue, OK_Ordinary,
1416           /*TypeDependent=*/false, base->isValueDependent(),
1417           base->isInstantiationDependent(),
1418           /*ContainsUnexpandedParameterPack=*/false),
1419      Base(base), IsaMemberLoc(l), OpLoc(oploc), IsArrow(isarrow) {}
1420
1421  /// \brief Build an empty expression.
1422  explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) { }
1423
1424  void setBase(Expr *E) { Base = E; }
1425  Expr *getBase() const { return cast<Expr>(Base); }
1426
1427  bool isArrow() const { return IsArrow; }
1428  void setArrow(bool A) { IsArrow = A; }
1429
1430  /// getMemberLoc - Return the location of the "member", in X->F, it is the
1431  /// location of 'F'.
1432  SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; }
1433  void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; }
1434
1435  SourceLocation getOpLoc() const { return OpLoc; }
1436  void setOpLoc(SourceLocation L) { OpLoc = L; }
1437
1438  SourceLocation getLocStart() const LLVM_READONLY {
1439    return getBase()->getLocStart();
1440  }
1441
1442  SourceLocation getBaseLocEnd() const LLVM_READONLY {
1443    return getBase()->getLocEnd();
1444  }
1445
1446  SourceLocation getLocEnd() const LLVM_READONLY { return IsaMemberLoc; }
1447
1448  SourceLocation getExprLoc() const LLVM_READONLY { return IsaMemberLoc; }
1449
1450  static bool classof(const Stmt *T) {
1451    return T->getStmtClass() == ObjCIsaExprClass;
1452  }
1453
1454  // Iterators
1455  child_range children() { return child_range(&Base, &Base+1); }
1456};
1457
1458
1459/// ObjCIndirectCopyRestoreExpr - Represents the passing of a function
1460/// argument by indirect copy-restore in ARC.  This is used to support
1461/// passing indirect arguments with the wrong lifetime, e.g. when
1462/// passing the address of a __strong local variable to an 'out'
1463/// parameter.  This expression kind is only valid in an "argument"
1464/// position to some sort of call expression.
1465///
1466/// The parameter must have type 'pointer to T', and the argument must
1467/// have type 'pointer to U', where T and U agree except possibly in
1468/// qualification.  If the argument value is null, then a null pointer
1469/// is passed;  otherwise it points to an object A, and:
1470/// 1. A temporary object B of type T is initialized, either by
1471///    zero-initialization (used when initializing an 'out' parameter)
1472///    or copy-initialization (used when initializing an 'inout'
1473///    parameter).
1474/// 2. The address of the temporary is passed to the function.
1475/// 3. If the call completes normally, A is move-assigned from B.
1476/// 4. Finally, A is destroyed immediately.
1477///
1478/// Currently 'T' must be a retainable object lifetime and must be
1479/// __autoreleasing;  this qualifier is ignored when initializing
1480/// the value.
1481class ObjCIndirectCopyRestoreExpr : public Expr {
1482  Stmt *Operand;
1483
1484  // unsigned ObjCIndirectCopyRestoreBits.ShouldCopy : 1;
1485
1486  friend class ASTReader;
1487  friend class ASTStmtReader;
1488
1489  void setShouldCopy(bool shouldCopy) {
1490    ObjCIndirectCopyRestoreExprBits.ShouldCopy = shouldCopy;
1491  }
1492
1493  explicit ObjCIndirectCopyRestoreExpr(EmptyShell Empty)
1494    : Expr(ObjCIndirectCopyRestoreExprClass, Empty) { }
1495
1496public:
1497  ObjCIndirectCopyRestoreExpr(Expr *operand, QualType type, bool shouldCopy)
1498    : Expr(ObjCIndirectCopyRestoreExprClass, type, VK_LValue, OK_Ordinary,
1499           operand->isTypeDependent(), operand->isValueDependent(),
1500           operand->isInstantiationDependent(),
1501           operand->containsUnexpandedParameterPack()),
1502      Operand(operand) {
1503    setShouldCopy(shouldCopy);
1504  }
1505
1506  Expr *getSubExpr() { return cast<Expr>(Operand); }
1507  const Expr *getSubExpr() const { return cast<Expr>(Operand); }
1508
1509  /// shouldCopy - True if we should do the 'copy' part of the
1510  /// copy-restore.  If false, the temporary will be zero-initialized.
1511  bool shouldCopy() const { return ObjCIndirectCopyRestoreExprBits.ShouldCopy; }
1512
1513  child_range children() { return child_range(&Operand, &Operand+1); }
1514
1515  // Source locations are determined by the subexpression.
1516  SourceLocation getLocStart() const LLVM_READONLY {
1517    return Operand->getLocStart();
1518  }
1519  SourceLocation getLocEnd() const LLVM_READONLY { return Operand->getLocEnd();}
1520
1521  SourceLocation getExprLoc() const LLVM_READONLY {
1522    return getSubExpr()->getExprLoc();
1523  }
1524
1525  static bool classof(const Stmt *s) {
1526    return s->getStmtClass() == ObjCIndirectCopyRestoreExprClass;
1527  }
1528};
1529
1530/// \brief An Objective-C "bridged" cast expression, which casts between
1531/// Objective-C pointers and C pointers, transferring ownership in the process.
1532///
1533/// \code
1534/// NSString *str = (__bridge_transfer NSString *)CFCreateString();
1535/// \endcode
1536class ObjCBridgedCastExpr : public ExplicitCastExpr {
1537  SourceLocation LParenLoc;
1538  SourceLocation BridgeKeywordLoc;
1539  unsigned Kind : 2;
1540
1541  friend class ASTStmtReader;
1542  friend class ASTStmtWriter;
1543
1544public:
1545  ObjCBridgedCastExpr(SourceLocation LParenLoc, ObjCBridgeCastKind Kind,
1546                      CastKind CK, SourceLocation BridgeKeywordLoc,
1547                      TypeSourceInfo *TSInfo, Expr *Operand)
1548    : ExplicitCastExpr(ObjCBridgedCastExprClass, TSInfo->getType(), VK_RValue,
1549                       CK, Operand, 0, TSInfo),
1550      LParenLoc(LParenLoc), BridgeKeywordLoc(BridgeKeywordLoc), Kind(Kind) { }
1551
1552  /// \brief Construct an empty Objective-C bridged cast.
1553  explicit ObjCBridgedCastExpr(EmptyShell Shell)
1554    : ExplicitCastExpr(ObjCBridgedCastExprClass, Shell, 0) { }
1555
1556  SourceLocation getLParenLoc() const { return LParenLoc; }
1557
1558  /// \brief Determine which kind of bridge is being performed via this cast.
1559  ObjCBridgeCastKind getBridgeKind() const {
1560    return static_cast<ObjCBridgeCastKind>(Kind);
1561  }
1562
1563  /// \brief Retrieve the kind of bridge being performed as a string.
1564  StringRef getBridgeKindName() const;
1565
1566  /// \brief The location of the bridge keyword.
1567  SourceLocation getBridgeKeywordLoc() const { return BridgeKeywordLoc; }
1568
1569  SourceLocation getLocStart() const LLVM_READONLY { return LParenLoc; }
1570  SourceLocation getLocEnd() const LLVM_READONLY {
1571    return getSubExpr()->getLocEnd();
1572  }
1573
1574  static bool classof(const Stmt *T) {
1575    return T->getStmtClass() == ObjCBridgedCastExprClass;
1576  }
1577};
1578
1579}  // end namespace clang
1580
1581#endif
1582