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