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