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