DeclObjC.h revision 360784
1//===- DeclObjC.h - Classes for representing declarations -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file defines the DeclObjC interface and subclasses.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_DECLOBJC_H
14#define LLVM_CLANG_AST_DECLOBJC_H
15
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/ExternalASTSource.h"
19#include "clang/AST/Redeclarable.h"
20#include "clang/AST/SelectorLocationsKind.h"
21#include "clang/AST/Type.h"
22#include "clang/Basic/IdentifierTable.h"
23#include "clang/Basic/LLVM.h"
24#include "clang/Basic/SourceLocation.h"
25#include "clang/Basic/Specifiers.h"
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/DenseSet.h"
29#include "llvm/ADT/None.h"
30#include "llvm/ADT/PointerIntPair.h"
31#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/ADT/iterator_range.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/TrailingObjects.h"
36#include <cassert>
37#include <cstddef>
38#include <cstdint>
39#include <iterator>
40#include <string>
41#include <utility>
42
43namespace clang {
44
45class ASTContext;
46class CompoundStmt;
47class CXXCtorInitializer;
48class Expr;
49class ObjCCategoryDecl;
50class ObjCCategoryImplDecl;
51class ObjCImplementationDecl;
52class ObjCInterfaceDecl;
53class ObjCIvarDecl;
54class ObjCPropertyDecl;
55class ObjCPropertyImplDecl;
56class ObjCProtocolDecl;
57class Stmt;
58
59class ObjCListBase {
60protected:
61  /// List is an array of pointers to objects that are not owned by this object.
62  void **List = nullptr;
63  unsigned NumElts = 0;
64
65public:
66  ObjCListBase() = default;
67  ObjCListBase(const ObjCListBase &) = delete;
68  ObjCListBase &operator=(const ObjCListBase &) = delete;
69
70  unsigned size() const { return NumElts; }
71  bool empty() const { return NumElts == 0; }
72
73protected:
74  void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
75};
76
77/// ObjCList - This is a simple template class used to hold various lists of
78/// decls etc, which is heavily used by the ObjC front-end.  This only use case
79/// this supports is setting the list all at once and then reading elements out
80/// of it.
81template <typename T>
82class ObjCList : public ObjCListBase {
83public:
84  void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
85    ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
86  }
87
88  using iterator = T* const *;
89
90  iterator begin() const { return (iterator)List; }
91  iterator end() const { return (iterator)List+NumElts; }
92
93  T* operator[](unsigned Idx) const {
94    assert(Idx < NumElts && "Invalid access");
95    return (T*)List[Idx];
96  }
97};
98
99/// A list of Objective-C protocols, along with the source
100/// locations at which they were referenced.
101class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
102  SourceLocation *Locations = nullptr;
103
104  using ObjCList<ObjCProtocolDecl>::set;
105
106public:
107  ObjCProtocolList() = default;
108
109  using loc_iterator = const SourceLocation *;
110
111  loc_iterator loc_begin() const { return Locations; }
112  loc_iterator loc_end() const { return Locations + size(); }
113
114  void set(ObjCProtocolDecl* const* InList, unsigned Elts,
115           const SourceLocation *Locs, ASTContext &Ctx);
116};
117
118/// ObjCMethodDecl - Represents an instance or class method declaration.
119/// ObjC methods can be declared within 4 contexts: class interfaces,
120/// categories, protocols, and class implementations. While C++ member
121/// functions leverage C syntax, Objective-C method syntax is modeled after
122/// Smalltalk (using colons to specify argument types/expressions).
123/// Here are some brief examples:
124///
125/// Setter/getter instance methods:
126/// - (void)setMenu:(NSMenu *)menu;
127/// - (NSMenu *)menu;
128///
129/// Instance method that takes 2 NSView arguments:
130/// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
131///
132/// Getter class method:
133/// + (NSMenu *)defaultMenu;
134///
135/// A selector represents a unique name for a method. The selector names for
136/// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
137///
138class ObjCMethodDecl : public NamedDecl, public DeclContext {
139  // This class stores some data in DeclContext::ObjCMethodDeclBits
140  // to save some space. Use the provided accessors to access it.
141
142public:
143  enum ImplementationControl { None, Required, Optional };
144
145private:
146  /// Return type of this method.
147  QualType MethodDeclType;
148
149  /// Type source information for the return type.
150  TypeSourceInfo *ReturnTInfo;
151
152  /// Array of ParmVarDecls for the formal parameters of this method
153  /// and optionally followed by selector locations.
154  void *ParamsAndSelLocs = nullptr;
155  unsigned NumParams = 0;
156
157  /// List of attributes for this method declaration.
158  SourceLocation DeclEndLoc; // the location of the ';' or '{'.
159
160  /// The following are only used for method definitions, null otherwise.
161  LazyDeclStmtPtr Body;
162
163  /// SelfDecl - Decl for the implicit self parameter. This is lazily
164  /// constructed by createImplicitParams.
165  ImplicitParamDecl *SelfDecl = nullptr;
166
167  /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
168  /// constructed by createImplicitParams.
169  ImplicitParamDecl *CmdDecl = nullptr;
170
171  ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
172                 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
173                 DeclContext *contextDecl, bool isInstance = true,
174                 bool isVariadic = false, bool isPropertyAccessor = false,
175                 bool isSynthesizedAccessorStub = false,
176                 bool isImplicitlyDeclared = false, bool isDefined = false,
177                 ImplementationControl impControl = None,
178                 bool HasRelatedResultType = false);
179
180  SelectorLocationsKind getSelLocsKind() const {
181    return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
182  }
183
184  void setSelLocsKind(SelectorLocationsKind Kind) {
185    ObjCMethodDeclBits.SelLocsKind = Kind;
186  }
187
188  bool hasStandardSelLocs() const {
189    return getSelLocsKind() != SelLoc_NonStandard;
190  }
191
192  /// Get a pointer to the stored selector identifiers locations array.
193  /// No locations will be stored if HasStandardSelLocs is true.
194  SourceLocation *getStoredSelLocs() {
195    return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
196  }
197  const SourceLocation *getStoredSelLocs() const {
198    return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
199  }
200
201  /// Get a pointer to the stored selector identifiers locations array.
202  /// No locations will be stored if HasStandardSelLocs is true.
203  ParmVarDecl **getParams() {
204    return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
205  }
206  const ParmVarDecl *const *getParams() const {
207    return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
208  }
209
210  /// Get the number of stored selector identifiers locations.
211  /// No locations will be stored if HasStandardSelLocs is true.
212  unsigned getNumStoredSelLocs() const {
213    if (hasStandardSelLocs())
214      return 0;
215    return getNumSelectorLocs();
216  }
217
218  void setParamsAndSelLocs(ASTContext &C,
219                           ArrayRef<ParmVarDecl*> Params,
220                           ArrayRef<SourceLocation> SelLocs);
221
222  /// A definition will return its interface declaration.
223  /// An interface declaration will return its definition.
224  /// Otherwise it will return itself.
225  ObjCMethodDecl *getNextRedeclarationImpl() override;
226
227public:
228  friend class ASTDeclReader;
229  friend class ASTDeclWriter;
230
231  static ObjCMethodDecl *
232  Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
233         Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
234         DeclContext *contextDecl, bool isInstance = true,
235         bool isVariadic = false, bool isPropertyAccessor = false,
236         bool isSynthesizedAccessorStub = false,
237         bool isImplicitlyDeclared = false, bool isDefined = false,
238         ImplementationControl impControl = None,
239         bool HasRelatedResultType = false);
240
241  static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
242
243  ObjCMethodDecl *getCanonicalDecl() override;
244  const ObjCMethodDecl *getCanonicalDecl() const {
245    return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
246  }
247
248  ObjCDeclQualifier getObjCDeclQualifier() const {
249    return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier);
250  }
251
252  void setObjCDeclQualifier(ObjCDeclQualifier QV) {
253    ObjCMethodDeclBits.objcDeclQualifier = QV;
254  }
255
256  /// Determine whether this method has a result type that is related
257  /// to the message receiver's type.
258  bool hasRelatedResultType() const {
259    return ObjCMethodDeclBits.RelatedResultType;
260  }
261
262  /// Note whether this method has a related result type.
263  void setRelatedResultType(bool RRT = true) {
264    ObjCMethodDeclBits.RelatedResultType = RRT;
265  }
266
267  /// True if this is a method redeclaration in the same interface.
268  bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; }
269  void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; }
270  void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
271
272  /// True if redeclared in the same interface.
273  bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; }
274  void setHasRedeclaration(bool HRD) const {
275    ObjCMethodDeclBits.HasRedeclaration = HRD;
276  }
277
278  /// Returns the location where the declarator ends. It will be
279  /// the location of ';' for a method declaration and the location of '{'
280  /// for a method definition.
281  SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
282
283  // Location information, modeled after the Stmt API.
284  SourceLocation getBeginLoc() const LLVM_READONLY { return getLocation(); }
285  SourceLocation getEndLoc() const LLVM_READONLY;
286  SourceRange getSourceRange() const override LLVM_READONLY {
287    return SourceRange(getLocation(), getEndLoc());
288  }
289
290  SourceLocation getSelectorStartLoc() const {
291    if (isImplicit())
292      return getBeginLoc();
293    return getSelectorLoc(0);
294  }
295
296  SourceLocation getSelectorLoc(unsigned Index) const {
297    assert(Index < getNumSelectorLocs() && "Index out of range!");
298    if (hasStandardSelLocs())
299      return getStandardSelectorLoc(Index, getSelector(),
300                                   getSelLocsKind() == SelLoc_StandardWithSpace,
301                                    parameters(),
302                                   DeclEndLoc);
303    return getStoredSelLocs()[Index];
304  }
305
306  void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
307
308  unsigned getNumSelectorLocs() const {
309    if (isImplicit())
310      return 0;
311    Selector Sel = getSelector();
312    if (Sel.isUnarySelector())
313      return 1;
314    return Sel.getNumArgs();
315  }
316
317  ObjCInterfaceDecl *getClassInterface();
318  const ObjCInterfaceDecl *getClassInterface() const {
319    return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
320  }
321
322  Selector getSelector() const { return getDeclName().getObjCSelector(); }
323
324  QualType getReturnType() const { return MethodDeclType; }
325  void setReturnType(QualType T) { MethodDeclType = T; }
326  SourceRange getReturnTypeSourceRange() const;
327
328  /// Determine the type of an expression that sends a message to this
329  /// function. This replaces the type parameters with the types they would
330  /// get if the receiver was parameterless (e.g. it may replace the type
331  /// parameter with 'id').
332  QualType getSendResultType() const;
333
334  /// Determine the type of an expression that sends a message to this
335  /// function with the given receiver type.
336  QualType getSendResultType(QualType receiverType) const;
337
338  TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
339  void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
340
341  // Iterator access to formal parameters.
342  unsigned param_size() const { return NumParams; }
343
344  using param_const_iterator = const ParmVarDecl *const *;
345  using param_iterator = ParmVarDecl *const *;
346  using param_range = llvm::iterator_range<param_iterator>;
347  using param_const_range = llvm::iterator_range<param_const_iterator>;
348
349  param_const_iterator param_begin() const {
350    return param_const_iterator(getParams());
351  }
352
353  param_const_iterator param_end() const {
354    return param_const_iterator(getParams() + NumParams);
355  }
356
357  param_iterator param_begin() { return param_iterator(getParams()); }
358  param_iterator param_end() { return param_iterator(getParams() + NumParams); }
359
360  // This method returns and of the parameters which are part of the selector
361  // name mangling requirements.
362  param_const_iterator sel_param_end() const {
363    return param_begin() + getSelector().getNumArgs();
364  }
365
366  // ArrayRef access to formal parameters.  This should eventually
367  // replace the iterator interface above.
368  ArrayRef<ParmVarDecl*> parameters() const {
369    return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
370                              NumParams);
371  }
372
373  ParmVarDecl *getParamDecl(unsigned Idx) {
374    assert(Idx < NumParams && "Index out of bounds!");
375    return getParams()[Idx];
376  }
377  const ParmVarDecl *getParamDecl(unsigned Idx) const {
378    return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx);
379  }
380
381  /// Sets the method's parameters and selector source locations.
382  /// If the method is implicit (not coming from source) \p SelLocs is
383  /// ignored.
384  void setMethodParams(ASTContext &C,
385                       ArrayRef<ParmVarDecl*> Params,
386                       ArrayRef<SourceLocation> SelLocs = llvm::None);
387
388  // Iterator access to parameter types.
389  struct GetTypeFn {
390    QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); }
391  };
392
393  using param_type_iterator =
394      llvm::mapped_iterator<param_const_iterator, GetTypeFn>;
395
396  param_type_iterator param_type_begin() const {
397    return llvm::map_iterator(param_begin(), GetTypeFn());
398  }
399
400  param_type_iterator param_type_end() const {
401    return llvm::map_iterator(param_end(), GetTypeFn());
402  }
403
404  /// createImplicitParams - Used to lazily create the self and cmd
405  /// implict parameters. This must be called prior to using getSelfDecl()
406  /// or getCmdDecl(). The call is ignored if the implicit parameters
407  /// have already been created.
408  void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
409
410  /// \return the type for \c self and set \arg selfIsPseudoStrong and
411  /// \arg selfIsConsumed accordingly.
412  QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
413                       bool &selfIsPseudoStrong, bool &selfIsConsumed) const;
414
415  ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
416  void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
417  ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
418  void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
419
420  /// Determines the family of this method.
421  ObjCMethodFamily getMethodFamily() const;
422
423  bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; }
424  void setInstanceMethod(bool isInst) {
425    ObjCMethodDeclBits.IsInstance = isInst;
426  }
427
428  bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; }
429  void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; }
430
431  bool isClassMethod() const { return !isInstanceMethod(); }
432
433  bool isPropertyAccessor() const {
434    return ObjCMethodDeclBits.IsPropertyAccessor;
435  }
436
437  void setPropertyAccessor(bool isAccessor) {
438    ObjCMethodDeclBits.IsPropertyAccessor = isAccessor;
439  }
440
441  bool isSynthesizedAccessorStub() const {
442    return ObjCMethodDeclBits.IsSynthesizedAccessorStub;
443  }
444
445  void setSynthesizedAccessorStub(bool isSynthesizedAccessorStub) {
446    ObjCMethodDeclBits.IsSynthesizedAccessorStub = isSynthesizedAccessorStub;
447  }
448
449  bool isDefined() const { return ObjCMethodDeclBits.IsDefined; }
450  void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; }
451
452  /// Whether this method overrides any other in the class hierarchy.
453  ///
454  /// A method is said to override any method in the class's
455  /// base classes, its protocols, or its categories' protocols, that has
456  /// the same selector and is of the same kind (class or instance).
457  /// A method in an implementation is not considered as overriding the same
458  /// method in the interface or its categories.
459  bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; }
460  void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; }
461
462  /// Return overridden methods for the given \p Method.
463  ///
464  /// An ObjC method is considered to override any method in the class's
465  /// base classes (and base's categories), its protocols, or its categories'
466  /// protocols, that has
467  /// the same selector and is of the same kind (class or instance).
468  /// A method in an implementation is not considered as overriding the same
469  /// method in the interface or its categories.
470  void getOverriddenMethods(
471                     SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
472
473  /// True if the method was a definition but its body was skipped.
474  bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; }
475  void setHasSkippedBody(bool Skipped = true) {
476    ObjCMethodDeclBits.HasSkippedBody = Skipped;
477  }
478
479  /// True if the method is tagged as objc_direct
480  bool isDirectMethod() const;
481
482  /// Returns the property associated with this method's selector.
483  ///
484  /// Note that even if this particular method is not marked as a property
485  /// accessor, it is still possible for it to match a property declared in a
486  /// superclass. Pass \c false if you only want to check the current class.
487  const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
488
489  // Related to protocols declared in  \@protocol
490  void setDeclImplementation(ImplementationControl ic) {
491    ObjCMethodDeclBits.DeclImplementation = ic;
492  }
493
494  ImplementationControl getImplementationControl() const {
495    return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
496  }
497
498  bool isOptional() const {
499    return getImplementationControl() == Optional;
500  }
501
502  /// Returns true if this specific method declaration is marked with the
503  /// designated initializer attribute.
504  bool isThisDeclarationADesignatedInitializer() const;
505
506  /// Returns true if the method selector resolves to a designated initializer
507  /// in the class's interface.
508  ///
509  /// \param InitMethod if non-null and the function returns true, it receives
510  /// the method declaration that was marked with the designated initializer
511  /// attribute.
512  bool isDesignatedInitializerForTheInterface(
513      const ObjCMethodDecl **InitMethod = nullptr) const;
514
515  /// Determine whether this method has a body.
516  bool hasBody() const override { return Body.isValid(); }
517
518  /// Retrieve the body of this method, if it has one.
519  Stmt *getBody() const override;
520
521  void setLazyBody(uint64_t Offset) { Body = Offset; }
522
523  CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
524  void setBody(Stmt *B) { Body = B; }
525
526  /// Returns whether this specific method is a definition.
527  bool isThisDeclarationADefinition() const { return hasBody(); }
528
529  /// Is this method defined in the NSObject base class?
530  bool definedInNSObject(const ASTContext &) const;
531
532  // Implement isa/cast/dyncast/etc.
533  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
534  static bool classofKind(Kind K) { return K == ObjCMethod; }
535
536  static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
537    return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
538  }
539
540  static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
541    return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
542  }
543};
544
545/// Describes the variance of a given generic parameter.
546enum class ObjCTypeParamVariance : uint8_t {
547  /// The parameter is invariant: must match exactly.
548  Invariant,
549
550  /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
551  /// the type parameter is covariant and T is a subtype of U.
552  Covariant,
553
554  /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
555  /// when the type parameter is covariant and U is a subtype of T.
556  Contravariant,
557};
558
559/// Represents the declaration of an Objective-C type parameter.
560///
561/// \code
562/// @interface NSDictionary<Key : id<NSCopying>, Value>
563/// @end
564/// \endcode
565///
566/// In the example above, both \c Key and \c Value are represented by
567/// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
568/// while \c Value gets an implicit bound of \c id.
569///
570/// Objective-C type parameters are typedef-names in the grammar,
571class ObjCTypeParamDecl : public TypedefNameDecl {
572  /// Index of this type parameter in the type parameter list.
573  unsigned Index : 14;
574
575  /// The variance of the type parameter.
576  unsigned Variance : 2;
577
578  /// The location of the variance, if any.
579  SourceLocation VarianceLoc;
580
581  /// The location of the ':', which will be valid when the bound was
582  /// explicitly specified.
583  SourceLocation ColonLoc;
584
585  ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
586                    ObjCTypeParamVariance variance, SourceLocation varianceLoc,
587                    unsigned index,
588                    SourceLocation nameLoc, IdentifierInfo *name,
589                    SourceLocation colonLoc, TypeSourceInfo *boundInfo)
590      : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
591                        boundInfo),
592        Index(index), Variance(static_cast<unsigned>(variance)),
593        VarianceLoc(varianceLoc), ColonLoc(colonLoc) {}
594
595  void anchor() override;
596
597public:
598  friend class ASTDeclReader;
599  friend class ASTDeclWriter;
600
601  static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
602                                   ObjCTypeParamVariance variance,
603                                   SourceLocation varianceLoc,
604                                   unsigned index,
605                                   SourceLocation nameLoc,
606                                   IdentifierInfo *name,
607                                   SourceLocation colonLoc,
608                                   TypeSourceInfo *boundInfo);
609  static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
610
611  SourceRange getSourceRange() const override LLVM_READONLY;
612
613  /// Determine the variance of this type parameter.
614  ObjCTypeParamVariance getVariance() const {
615    return static_cast<ObjCTypeParamVariance>(Variance);
616  }
617
618  /// Set the variance of this type parameter.
619  void setVariance(ObjCTypeParamVariance variance) {
620    Variance = static_cast<unsigned>(variance);
621  }
622
623  /// Retrieve the location of the variance keyword.
624  SourceLocation getVarianceLoc() const { return VarianceLoc; }
625
626  /// Retrieve the index into its type parameter list.
627  unsigned getIndex() const { return Index; }
628
629  /// Whether this type parameter has an explicitly-written type bound, e.g.,
630  /// "T : NSView".
631  bool hasExplicitBound() const { return ColonLoc.isValid(); }
632
633  /// Retrieve the location of the ':' separating the type parameter name
634  /// from the explicitly-specified bound.
635  SourceLocation getColonLoc() const { return ColonLoc; }
636
637  // Implement isa/cast/dyncast/etc.
638  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
639  static bool classofKind(Kind K) { return K == ObjCTypeParam; }
640};
641
642/// Stores a list of Objective-C type parameters for a parameterized class
643/// or a category/extension thereof.
644///
645/// \code
646/// @interface NSArray<T> // stores the <T>
647/// @end
648/// \endcode
649class ObjCTypeParamList final
650    : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
651  /// Stores the components of a SourceRange as a POD.
652  struct PODSourceRange {
653    unsigned Begin;
654    unsigned End;
655  };
656
657  union {
658    /// Location of the left and right angle brackets.
659    PODSourceRange Brackets;
660
661    // Used only for alignment.
662    ObjCTypeParamDecl *AlignmentHack;
663  };
664
665  /// The number of parameters in the list, which are tail-allocated.
666  unsigned NumParams;
667
668  ObjCTypeParamList(SourceLocation lAngleLoc,
669                    ArrayRef<ObjCTypeParamDecl *> typeParams,
670                    SourceLocation rAngleLoc);
671
672public:
673  friend TrailingObjects;
674
675  /// Create a new Objective-C type parameter list.
676  static ObjCTypeParamList *create(ASTContext &ctx,
677                                   SourceLocation lAngleLoc,
678                                   ArrayRef<ObjCTypeParamDecl *> typeParams,
679                                   SourceLocation rAngleLoc);
680
681  /// Iterate through the type parameters in the list.
682  using iterator = ObjCTypeParamDecl **;
683
684  iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
685
686  iterator end() { return begin() + size(); }
687
688  /// Determine the number of type parameters in this list.
689  unsigned size() const { return NumParams; }
690
691  // Iterate through the type parameters in the list.
692  using const_iterator = ObjCTypeParamDecl * const *;
693
694  const_iterator begin() const {
695    return getTrailingObjects<ObjCTypeParamDecl *>();
696  }
697
698  const_iterator end() const {
699    return begin() + size();
700  }
701
702  ObjCTypeParamDecl *front() const {
703    assert(size() > 0 && "empty Objective-C type parameter list");
704    return *begin();
705  }
706
707  ObjCTypeParamDecl *back() const {
708    assert(size() > 0 && "empty Objective-C type parameter list");
709    return *(end() - 1);
710  }
711
712  SourceLocation getLAngleLoc() const {
713    return SourceLocation::getFromRawEncoding(Brackets.Begin);
714  }
715
716  SourceLocation getRAngleLoc() const {
717    return SourceLocation::getFromRawEncoding(Brackets.End);
718  }
719
720  SourceRange getSourceRange() const {
721    return SourceRange(getLAngleLoc(), getRAngleLoc());
722  }
723
724  /// Gather the default set of type arguments to be substituted for
725  /// these type parameters when dealing with an unspecialized type.
726  void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
727};
728
729enum class ObjCPropertyQueryKind : uint8_t {
730  OBJC_PR_query_unknown = 0x00,
731  OBJC_PR_query_instance,
732  OBJC_PR_query_class
733};
734
735/// Represents one property declaration in an Objective-C interface.
736///
737/// For example:
738/// \code{.mm}
739/// \@property (assign, readwrite) int MyProperty;
740/// \endcode
741class ObjCPropertyDecl : public NamedDecl {
742  void anchor() override;
743
744public:
745  enum PropertyAttributeKind {
746    OBJC_PR_noattr    = 0x00,
747    OBJC_PR_readonly  = 0x01,
748    OBJC_PR_getter    = 0x02,
749    OBJC_PR_assign    = 0x04,
750    OBJC_PR_readwrite = 0x08,
751    OBJC_PR_retain    = 0x10,
752    OBJC_PR_copy      = 0x20,
753    OBJC_PR_nonatomic = 0x40,
754    OBJC_PR_setter    = 0x80,
755    OBJC_PR_atomic    = 0x100,
756    OBJC_PR_weak      = 0x200,
757    OBJC_PR_strong    = 0x400,
758    OBJC_PR_unsafe_unretained = 0x800,
759    /// Indicates that the nullability of the type was spelled with a
760    /// property attribute rather than a type qualifier.
761    OBJC_PR_nullability = 0x1000,
762    OBJC_PR_null_resettable = 0x2000,
763    OBJC_PR_class = 0x4000,
764    OBJC_PR_direct = 0x8000
765    // Adding a property should change NumPropertyAttrsBits
766  };
767
768  enum {
769    /// Number of bits fitting all the property attributes.
770    NumPropertyAttrsBits = 16
771  };
772
773  enum SetterKind { Assign, Retain, Copy, Weak };
774  enum PropertyControl { None, Required, Optional };
775
776private:
777  // location of \@property
778  SourceLocation AtLoc;
779
780  // location of '(' starting attribute list or null.
781  SourceLocation LParenLoc;
782
783  QualType DeclType;
784  TypeSourceInfo *DeclTypeSourceInfo;
785  unsigned PropertyAttributes : NumPropertyAttrsBits;
786  unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
787
788  // \@required/\@optional
789  unsigned PropertyImplementation : 2;
790
791  // getter name of NULL if no getter
792  Selector GetterName;
793
794  // setter name of NULL if no setter
795  Selector SetterName;
796
797  // location of the getter attribute's value
798  SourceLocation GetterNameLoc;
799
800  // location of the setter attribute's value
801  SourceLocation SetterNameLoc;
802
803  // Declaration of getter instance method
804  ObjCMethodDecl *GetterMethodDecl = nullptr;
805
806  // Declaration of setter instance method
807  ObjCMethodDecl *SetterMethodDecl = nullptr;
808
809  // Synthesize ivar for this property
810  ObjCIvarDecl *PropertyIvarDecl = nullptr;
811
812  ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
813                   SourceLocation AtLocation,  SourceLocation LParenLocation,
814                   QualType T, TypeSourceInfo *TSI,
815                   PropertyControl propControl)
816    : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
817      LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
818      PropertyAttributes(OBJC_PR_noattr),
819      PropertyAttributesAsWritten(OBJC_PR_noattr),
820      PropertyImplementation(propControl), GetterName(Selector()),
821      SetterName(Selector()) {}
822
823public:
824  static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
825                                  SourceLocation L,
826                                  IdentifierInfo *Id, SourceLocation AtLocation,
827                                  SourceLocation LParenLocation,
828                                  QualType T,
829                                  TypeSourceInfo *TSI,
830                                  PropertyControl propControl = None);
831
832  static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
833
834  SourceLocation getAtLoc() const { return AtLoc; }
835  void setAtLoc(SourceLocation L) { AtLoc = L; }
836
837  SourceLocation getLParenLoc() const { return LParenLoc; }
838  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
839
840  TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
841
842  QualType getType() const { return DeclType; }
843
844  void setType(QualType T, TypeSourceInfo *TSI) {
845    DeclType = T;
846    DeclTypeSourceInfo = TSI;
847  }
848
849  /// Retrieve the type when this property is used with a specific base object
850  /// type.
851  QualType getUsageType(QualType objectType) const;
852
853  PropertyAttributeKind getPropertyAttributes() const {
854    return PropertyAttributeKind(PropertyAttributes);
855  }
856
857  void setPropertyAttributes(PropertyAttributeKind PRVal) {
858    PropertyAttributes |= PRVal;
859  }
860
861  void overwritePropertyAttributes(unsigned PRVal) {
862    PropertyAttributes = PRVal;
863  }
864
865  PropertyAttributeKind getPropertyAttributesAsWritten() const {
866    return PropertyAttributeKind(PropertyAttributesAsWritten);
867  }
868
869  void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
870    PropertyAttributesAsWritten = PRVal;
871  }
872
873  // Helper methods for accessing attributes.
874
875  /// isReadOnly - Return true iff the property has a setter.
876  bool isReadOnly() const {
877    return (PropertyAttributes & OBJC_PR_readonly);
878  }
879
880  /// isAtomic - Return true if the property is atomic.
881  bool isAtomic() const {
882    return (PropertyAttributes & OBJC_PR_atomic);
883  }
884
885  /// isRetaining - Return true if the property retains its value.
886  bool isRetaining() const {
887    return (PropertyAttributes &
888            (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
889  }
890
891  bool isInstanceProperty() const { return !isClassProperty(); }
892  bool isClassProperty() const { return PropertyAttributes & OBJC_PR_class; }
893  bool isDirectProperty() const { return PropertyAttributes & OBJC_PR_direct; }
894
895  ObjCPropertyQueryKind getQueryKind() const {
896    return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
897                               ObjCPropertyQueryKind::OBJC_PR_query_instance;
898  }
899
900  static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
901    return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
902                             ObjCPropertyQueryKind::OBJC_PR_query_instance;
903  }
904
905  /// getSetterKind - Return the method used for doing assignment in
906  /// the property setter. This is only valid if the property has been
907  /// defined to have a setter.
908  SetterKind getSetterKind() const {
909    if (PropertyAttributes & OBJC_PR_strong)
910      return getType()->isBlockPointerType() ? Copy : Retain;
911    if (PropertyAttributes & OBJC_PR_retain)
912      return Retain;
913    if (PropertyAttributes & OBJC_PR_copy)
914      return Copy;
915    if (PropertyAttributes & OBJC_PR_weak)
916      return Weak;
917    return Assign;
918  }
919
920  Selector getGetterName() const { return GetterName; }
921  SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
922
923  void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
924    GetterName = Sel;
925    GetterNameLoc = Loc;
926  }
927
928  Selector getSetterName() const { return SetterName; }
929  SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
930
931  void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
932    SetterName = Sel;
933    SetterNameLoc = Loc;
934  }
935
936  ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
937  void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
938
939  ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
940  void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
941
942  // Related to \@optional/\@required declared in \@protocol
943  void setPropertyImplementation(PropertyControl pc) {
944    PropertyImplementation = pc;
945  }
946
947  PropertyControl getPropertyImplementation() const {
948    return PropertyControl(PropertyImplementation);
949  }
950
951  bool isOptional() const {
952    return getPropertyImplementation() == PropertyControl::Optional;
953  }
954
955  void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
956    PropertyIvarDecl = Ivar;
957  }
958
959  ObjCIvarDecl *getPropertyIvarDecl() const {
960    return PropertyIvarDecl;
961  }
962
963  SourceRange getSourceRange() const override LLVM_READONLY {
964    return SourceRange(AtLoc, getLocation());
965  }
966
967  /// Get the default name of the synthesized ivar.
968  IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
969
970  /// Lookup a property by name in the specified DeclContext.
971  static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
972                                            const IdentifierInfo *propertyID,
973                                            ObjCPropertyQueryKind queryKind);
974
975  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
976  static bool classofKind(Kind K) { return K == ObjCProperty; }
977};
978
979/// ObjCContainerDecl - Represents a container for method declarations.
980/// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
981/// ObjCProtocolDecl, and ObjCImplDecl.
982///
983class ObjCContainerDecl : public NamedDecl, public DeclContext {
984  // This class stores some data in DeclContext::ObjCContainerDeclBits
985  // to save some space. Use the provided accessors to access it.
986
987  // These two locations in the range mark the end of the method container.
988  // The first points to the '@' token, and the second to the 'end' token.
989  SourceRange AtEnd;
990
991  void anchor() override;
992
993public:
994  ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id,
995                    SourceLocation nameLoc, SourceLocation atStartLoc);
996
997  // Iterator access to instance/class properties.
998  using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>;
999  using prop_range =
1000      llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>;
1001
1002  prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
1003
1004  prop_iterator prop_begin() const {
1005    return prop_iterator(decls_begin());
1006  }
1007
1008  prop_iterator prop_end() const {
1009    return prop_iterator(decls_end());
1010  }
1011
1012  using instprop_iterator =
1013      filtered_decl_iterator<ObjCPropertyDecl,
1014                             &ObjCPropertyDecl::isInstanceProperty>;
1015  using instprop_range = llvm::iterator_range<instprop_iterator>;
1016
1017  instprop_range instance_properties() const {
1018    return instprop_range(instprop_begin(), instprop_end());
1019  }
1020
1021  instprop_iterator instprop_begin() const {
1022    return instprop_iterator(decls_begin());
1023  }
1024
1025  instprop_iterator instprop_end() const {
1026    return instprop_iterator(decls_end());
1027  }
1028
1029  using classprop_iterator =
1030      filtered_decl_iterator<ObjCPropertyDecl,
1031                             &ObjCPropertyDecl::isClassProperty>;
1032  using classprop_range = llvm::iterator_range<classprop_iterator>;
1033
1034  classprop_range class_properties() const {
1035    return classprop_range(classprop_begin(), classprop_end());
1036  }
1037
1038  classprop_iterator classprop_begin() const {
1039    return classprop_iterator(decls_begin());
1040  }
1041
1042  classprop_iterator classprop_end() const {
1043    return classprop_iterator(decls_end());
1044  }
1045
1046  // Iterator access to instance/class methods.
1047  using method_iterator = specific_decl_iterator<ObjCMethodDecl>;
1048  using method_range =
1049      llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>;
1050
1051  method_range methods() const {
1052    return method_range(meth_begin(), meth_end());
1053  }
1054
1055  method_iterator meth_begin() const {
1056    return method_iterator(decls_begin());
1057  }
1058
1059  method_iterator meth_end() const {
1060    return method_iterator(decls_end());
1061  }
1062
1063  using instmeth_iterator =
1064      filtered_decl_iterator<ObjCMethodDecl,
1065                             &ObjCMethodDecl::isInstanceMethod>;
1066  using instmeth_range = llvm::iterator_range<instmeth_iterator>;
1067
1068  instmeth_range instance_methods() const {
1069    return instmeth_range(instmeth_begin(), instmeth_end());
1070  }
1071
1072  instmeth_iterator instmeth_begin() const {
1073    return instmeth_iterator(decls_begin());
1074  }
1075
1076  instmeth_iterator instmeth_end() const {
1077    return instmeth_iterator(decls_end());
1078  }
1079
1080  using classmeth_iterator =
1081      filtered_decl_iterator<ObjCMethodDecl,
1082                             &ObjCMethodDecl::isClassMethod>;
1083  using classmeth_range = llvm::iterator_range<classmeth_iterator>;
1084
1085  classmeth_range class_methods() const {
1086    return classmeth_range(classmeth_begin(), classmeth_end());
1087  }
1088
1089  classmeth_iterator classmeth_begin() const {
1090    return classmeth_iterator(decls_begin());
1091  }
1092
1093  classmeth_iterator classmeth_end() const {
1094    return classmeth_iterator(decls_end());
1095  }
1096
1097  // Get the local instance/class method declared in this interface.
1098  ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1099                            bool AllowHidden = false) const;
1100
1101  ObjCMethodDecl *getInstanceMethod(Selector Sel,
1102                                    bool AllowHidden = false) const {
1103    return getMethod(Sel, true/*isInstance*/, AllowHidden);
1104  }
1105
1106  ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1107    return getMethod(Sel, false/*isInstance*/, AllowHidden);
1108  }
1109
1110  bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1111  ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1112
1113  ObjCPropertyDecl *
1114  FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1115                          ObjCPropertyQueryKind QueryKind) const;
1116
1117  using PropertyMap =
1118      llvm::DenseMap<std::pair<IdentifierInfo *, unsigned/*isClassProperty*/>,
1119                     ObjCPropertyDecl *>;
1120  using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>;
1121  using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>;
1122
1123  /// This routine collects list of properties to be implemented in the class.
1124  /// This includes, class's and its conforming protocols' properties.
1125  /// Note, the superclass's properties are not included in the list.
1126  virtual void collectPropertiesToImplement(PropertyMap &PM,
1127                                            PropertyDeclOrder &PO) const {}
1128
1129  SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
1130
1131  void setAtStartLoc(SourceLocation Loc) {
1132    ObjCContainerDeclBits.AtStart = Loc;
1133  }
1134
1135  // Marks the end of the container.
1136  SourceRange getAtEndRange() const { return AtEnd; }
1137
1138  void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; }
1139
1140  SourceRange getSourceRange() const override LLVM_READONLY {
1141    return SourceRange(getAtStartLoc(), getAtEndRange().getEnd());
1142  }
1143
1144  // Implement isa/cast/dyncast/etc.
1145  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1146
1147  static bool classofKind(Kind K) {
1148    return K >= firstObjCContainer &&
1149           K <= lastObjCContainer;
1150  }
1151
1152  static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1153    return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1154  }
1155
1156  static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1157    return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1158  }
1159};
1160
1161/// Represents an ObjC class declaration.
1162///
1163/// For example:
1164///
1165/// \code
1166///   // MostPrimitive declares no super class (not particularly useful).
1167///   \@interface MostPrimitive
1168///     // no instance variables or methods.
1169///   \@end
1170///
1171///   // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1172///   \@interface NSResponder : NSObject \<NSCoding>
1173///   { // instance variables are represented by ObjCIvarDecl.
1174///     id nextResponder; // nextResponder instance variable.
1175///   }
1176///   - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1177///   - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1178///   \@end                                    // to an NSEvent.
1179/// \endcode
1180///
1181///   Unlike C/C++, forward class declarations are accomplished with \@class.
1182///   Unlike C/C++, \@class allows for a list of classes to be forward declared.
1183///   Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1184///   typically inherit from NSObject (an exception is NSProxy).
1185///
1186class ObjCInterfaceDecl : public ObjCContainerDecl
1187                        , public Redeclarable<ObjCInterfaceDecl> {
1188  friend class ASTContext;
1189
1190  /// TypeForDecl - This indicates the Type object that represents this
1191  /// TypeDecl.  It is a cache maintained by ASTContext::getObjCInterfaceType
1192  mutable const Type *TypeForDecl = nullptr;
1193
1194  struct DefinitionData {
1195    /// The definition of this class, for quick access from any
1196    /// declaration.
1197    ObjCInterfaceDecl *Definition = nullptr;
1198
1199    /// When non-null, this is always an ObjCObjectType.
1200    TypeSourceInfo *SuperClassTInfo = nullptr;
1201
1202    /// Protocols referenced in the \@interface  declaration
1203    ObjCProtocolList ReferencedProtocols;
1204
1205    /// Protocols reference in both the \@interface and class extensions.
1206    ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1207
1208    /// List of categories and class extensions defined for this class.
1209    ///
1210    /// Categories are stored as a linked list in the AST, since the categories
1211    /// and class extensions come long after the initial interface declaration,
1212    /// and we avoid dynamically-resized arrays in the AST wherever possible.
1213    ObjCCategoryDecl *CategoryList = nullptr;
1214
1215    /// IvarList - List of all ivars defined by this class; including class
1216    /// extensions and implementation. This list is built lazily.
1217    ObjCIvarDecl *IvarList = nullptr;
1218
1219    /// Indicates that the contents of this Objective-C class will be
1220    /// completed by the external AST source when required.
1221    mutable unsigned ExternallyCompleted : 1;
1222
1223    /// Indicates that the ivar cache does not yet include ivars
1224    /// declared in the implementation.
1225    mutable unsigned IvarListMissingImplementation : 1;
1226
1227    /// Indicates that this interface decl contains at least one initializer
1228    /// marked with the 'objc_designated_initializer' attribute.
1229    unsigned HasDesignatedInitializers : 1;
1230
1231    enum InheritedDesignatedInitializersState {
1232      /// We didn't calculate whether the designated initializers should be
1233      /// inherited or not.
1234      IDI_Unknown = 0,
1235
1236      /// Designated initializers are inherited for the super class.
1237      IDI_Inherited = 1,
1238
1239      /// The class does not inherit designated initializers.
1240      IDI_NotInherited = 2
1241    };
1242
1243    /// One of the \c InheritedDesignatedInitializersState enumeratos.
1244    mutable unsigned InheritedDesignatedInitializers : 2;
1245
1246    /// The location of the last location in this declaration, before
1247    /// the properties/methods. For example, this will be the '>', '}', or
1248    /// identifier,
1249    SourceLocation EndLoc;
1250
1251    DefinitionData()
1252        : ExternallyCompleted(false), IvarListMissingImplementation(true),
1253          HasDesignatedInitializers(false),
1254          InheritedDesignatedInitializers(IDI_Unknown) {}
1255  };
1256
1257  /// The type parameters associated with this class, if any.
1258  ObjCTypeParamList *TypeParamList = nullptr;
1259
1260  /// Contains a pointer to the data associated with this class,
1261  /// which will be NULL if this class has not yet been defined.
1262  ///
1263  /// The bit indicates when we don't need to check for out-of-date
1264  /// declarations. It will be set unless modules are enabled.
1265  llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1266
1267  ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1268                    IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1269                    SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1270                    bool IsInternal);
1271
1272  void anchor() override;
1273
1274  void LoadExternalDefinition() const;
1275
1276  DefinitionData &data() const {
1277    assert(Data.getPointer() && "Declaration has no definition!");
1278    return *Data.getPointer();
1279  }
1280
1281  /// Allocate the definition data for this class.
1282  void allocateDefinitionData();
1283
1284  using redeclarable_base = Redeclarable<ObjCInterfaceDecl>;
1285
1286  ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1287    return getNextRedeclaration();
1288  }
1289
1290  ObjCInterfaceDecl *getPreviousDeclImpl() override {
1291    return getPreviousDecl();
1292  }
1293
1294  ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1295    return getMostRecentDecl();
1296  }
1297
1298public:
1299  static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1300                                   SourceLocation atLoc,
1301                                   IdentifierInfo *Id,
1302                                   ObjCTypeParamList *typeParamList,
1303                                   ObjCInterfaceDecl *PrevDecl,
1304                                   SourceLocation ClassLoc = SourceLocation(),
1305                                   bool isInternal = false);
1306
1307  static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1308
1309  /// Retrieve the type parameters of this class.
1310  ///
1311  /// This function looks for a type parameter list for the given
1312  /// class; if the class has been declared (with \c \@class) but not
1313  /// defined (with \c \@interface), it will search for a declaration that
1314  /// has type parameters, skipping any declarations that do not.
1315  ObjCTypeParamList *getTypeParamList() const;
1316
1317  /// Set the type parameters of this class.
1318  ///
1319  /// This function is used by the AST importer, which must import the type
1320  /// parameters after creating their DeclContext to avoid loops.
1321  void setTypeParamList(ObjCTypeParamList *TPL);
1322
1323  /// Retrieve the type parameters written on this particular declaration of
1324  /// the class.
1325  ObjCTypeParamList *getTypeParamListAsWritten() const {
1326    return TypeParamList;
1327  }
1328
1329  SourceRange getSourceRange() const override LLVM_READONLY {
1330    if (isThisDeclarationADefinition())
1331      return ObjCContainerDecl::getSourceRange();
1332
1333    return SourceRange(getAtStartLoc(), getLocation());
1334  }
1335
1336  /// Indicate that this Objective-C class is complete, but that
1337  /// the external AST source will be responsible for filling in its contents
1338  /// when a complete class is required.
1339  void setExternallyCompleted();
1340
1341  /// Indicate that this interface decl contains at least one initializer
1342  /// marked with the 'objc_designated_initializer' attribute.
1343  void setHasDesignatedInitializers();
1344
1345  /// Returns true if this interface decl contains at least one initializer
1346  /// marked with the 'objc_designated_initializer' attribute.
1347  bool hasDesignatedInitializers() const;
1348
1349  /// Returns true if this interface decl declares a designated initializer
1350  /// or it inherites one from its super class.
1351  bool declaresOrInheritsDesignatedInitializers() const {
1352    return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1353  }
1354
1355  const ObjCProtocolList &getReferencedProtocols() const {
1356    assert(hasDefinition() && "Caller did not check for forward reference!");
1357    if (data().ExternallyCompleted)
1358      LoadExternalDefinition();
1359
1360    return data().ReferencedProtocols;
1361  }
1362
1363  ObjCImplementationDecl *getImplementation() const;
1364  void setImplementation(ObjCImplementationDecl *ImplD);
1365
1366  ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1367
1368  // Get the local instance/class method declared in a category.
1369  ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1370  ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1371
1372  ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1373    return isInstance ? getCategoryInstanceMethod(Sel)
1374                      : getCategoryClassMethod(Sel);
1375  }
1376
1377  using protocol_iterator = ObjCProtocolList::iterator;
1378  using protocol_range = llvm::iterator_range<protocol_iterator>;
1379
1380  protocol_range protocols() const {
1381    return protocol_range(protocol_begin(), protocol_end());
1382  }
1383
1384  protocol_iterator protocol_begin() const {
1385    // FIXME: Should make sure no callers ever do this.
1386    if (!hasDefinition())
1387      return protocol_iterator();
1388
1389    if (data().ExternallyCompleted)
1390      LoadExternalDefinition();
1391
1392    return data().ReferencedProtocols.begin();
1393  }
1394
1395  protocol_iterator protocol_end() const {
1396    // FIXME: Should make sure no callers ever do this.
1397    if (!hasDefinition())
1398      return protocol_iterator();
1399
1400    if (data().ExternallyCompleted)
1401      LoadExternalDefinition();
1402
1403    return data().ReferencedProtocols.end();
1404  }
1405
1406  using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
1407  using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
1408
1409  protocol_loc_range protocol_locs() const {
1410    return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1411  }
1412
1413  protocol_loc_iterator protocol_loc_begin() const {
1414    // FIXME: Should make sure no callers ever do this.
1415    if (!hasDefinition())
1416      return protocol_loc_iterator();
1417
1418    if (data().ExternallyCompleted)
1419      LoadExternalDefinition();
1420
1421    return data().ReferencedProtocols.loc_begin();
1422  }
1423
1424  protocol_loc_iterator protocol_loc_end() const {
1425    // FIXME: Should make sure no callers ever do this.
1426    if (!hasDefinition())
1427      return protocol_loc_iterator();
1428
1429    if (data().ExternallyCompleted)
1430      LoadExternalDefinition();
1431
1432    return data().ReferencedProtocols.loc_end();
1433  }
1434
1435  using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator;
1436  using all_protocol_range = llvm::iterator_range<all_protocol_iterator>;
1437
1438  all_protocol_range all_referenced_protocols() const {
1439    return all_protocol_range(all_referenced_protocol_begin(),
1440                              all_referenced_protocol_end());
1441  }
1442
1443  all_protocol_iterator all_referenced_protocol_begin() const {
1444    // FIXME: Should make sure no callers ever do this.
1445    if (!hasDefinition())
1446      return all_protocol_iterator();
1447
1448    if (data().ExternallyCompleted)
1449      LoadExternalDefinition();
1450
1451    return data().AllReferencedProtocols.empty()
1452             ? protocol_begin()
1453             : data().AllReferencedProtocols.begin();
1454  }
1455
1456  all_protocol_iterator all_referenced_protocol_end() const {
1457    // FIXME: Should make sure no callers ever do this.
1458    if (!hasDefinition())
1459      return all_protocol_iterator();
1460
1461    if (data().ExternallyCompleted)
1462      LoadExternalDefinition();
1463
1464    return data().AllReferencedProtocols.empty()
1465             ? protocol_end()
1466             : data().AllReferencedProtocols.end();
1467  }
1468
1469  using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
1470  using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
1471
1472  ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1473
1474  ivar_iterator ivar_begin() const {
1475    if (const ObjCInterfaceDecl *Def = getDefinition())
1476      return ivar_iterator(Def->decls_begin());
1477
1478    // FIXME: Should make sure no callers ever do this.
1479    return ivar_iterator();
1480  }
1481
1482  ivar_iterator ivar_end() const {
1483    if (const ObjCInterfaceDecl *Def = getDefinition())
1484      return ivar_iterator(Def->decls_end());
1485
1486    // FIXME: Should make sure no callers ever do this.
1487    return ivar_iterator();
1488  }
1489
1490  unsigned ivar_size() const {
1491    return std::distance(ivar_begin(), ivar_end());
1492  }
1493
1494  bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1495
1496  ObjCIvarDecl *all_declared_ivar_begin();
1497  const ObjCIvarDecl *all_declared_ivar_begin() const {
1498    // Even though this modifies IvarList, it's conceptually const:
1499    // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1500    return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1501  }
1502  void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1503
1504  /// setProtocolList - Set the list of protocols that this interface
1505  /// implements.
1506  void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1507                       const SourceLocation *Locs, ASTContext &C) {
1508    data().ReferencedProtocols.set(List, Num, Locs, C);
1509  }
1510
1511  /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1512  /// into the protocol list for this class.
1513  void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1514                                       unsigned Num,
1515                                       ASTContext &C);
1516
1517  /// Produce a name to be used for class's metadata. It comes either via
1518  /// objc_runtime_name attribute or class name.
1519  StringRef getObjCRuntimeNameAsString() const;
1520
1521  /// Returns the designated initializers for the interface.
1522  ///
1523  /// If this declaration does not have methods marked as designated
1524  /// initializers then the interface inherits the designated initializers of
1525  /// its super class.
1526  void getDesignatedInitializers(
1527                  llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1528
1529  /// Returns true if the given selector is a designated initializer for the
1530  /// interface.
1531  ///
1532  /// If this declaration does not have methods marked as designated
1533  /// initializers then the interface inherits the designated initializers of
1534  /// its super class.
1535  ///
1536  /// \param InitMethod if non-null and the function returns true, it receives
1537  /// the method that was marked as a designated initializer.
1538  bool
1539  isDesignatedInitializer(Selector Sel,
1540                          const ObjCMethodDecl **InitMethod = nullptr) const;
1541
1542  /// Determine whether this particular declaration of this class is
1543  /// actually also a definition.
1544  bool isThisDeclarationADefinition() const {
1545    return getDefinition() == this;
1546  }
1547
1548  /// Determine whether this class has been defined.
1549  bool hasDefinition() const {
1550    // If the name of this class is out-of-date, bring it up-to-date, which
1551    // might bring in a definition.
1552    // Note: a null value indicates that we don't have a definition and that
1553    // modules are enabled.
1554    if (!Data.getOpaqueValue())
1555      getMostRecentDecl();
1556
1557    return Data.getPointer();
1558  }
1559
1560  /// Retrieve the definition of this class, or NULL if this class
1561  /// has been forward-declared (with \@class) but not yet defined (with
1562  /// \@interface).
1563  ObjCInterfaceDecl *getDefinition() {
1564    return hasDefinition()? Data.getPointer()->Definition : nullptr;
1565  }
1566
1567  /// Retrieve the definition of this class, or NULL if this class
1568  /// has been forward-declared (with \@class) but not yet defined (with
1569  /// \@interface).
1570  const ObjCInterfaceDecl *getDefinition() const {
1571    return hasDefinition()? Data.getPointer()->Definition : nullptr;
1572  }
1573
1574  /// Starts the definition of this Objective-C class, taking it from
1575  /// a forward declaration (\@class) to a definition (\@interface).
1576  void startDefinition();
1577
1578  /// Retrieve the superclass type.
1579  const ObjCObjectType *getSuperClassType() const {
1580    if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1581      return TInfo->getType()->castAs<ObjCObjectType>();
1582
1583    return nullptr;
1584  }
1585
1586  // Retrieve the type source information for the superclass.
1587  TypeSourceInfo *getSuperClassTInfo() const {
1588    // FIXME: Should make sure no callers ever do this.
1589    if (!hasDefinition())
1590      return nullptr;
1591
1592    if (data().ExternallyCompleted)
1593      LoadExternalDefinition();
1594
1595    return data().SuperClassTInfo;
1596  }
1597
1598  // Retrieve the declaration for the superclass of this class, which
1599  // does not include any type arguments that apply to the superclass.
1600  ObjCInterfaceDecl *getSuperClass() const;
1601
1602  void setSuperClass(TypeSourceInfo *superClass) {
1603    data().SuperClassTInfo = superClass;
1604  }
1605
1606  /// Iterator that walks over the list of categories, filtering out
1607  /// those that do not meet specific criteria.
1608  ///
1609  /// This class template is used for the various permutations of category
1610  /// and extension iterators.
1611  template<bool (*Filter)(ObjCCategoryDecl *)>
1612  class filtered_category_iterator {
1613    ObjCCategoryDecl *Current = nullptr;
1614
1615    void findAcceptableCategory();
1616
1617  public:
1618    using value_type = ObjCCategoryDecl *;
1619    using reference = value_type;
1620    using pointer = value_type;
1621    using difference_type = std::ptrdiff_t;
1622    using iterator_category = std::input_iterator_tag;
1623
1624    filtered_category_iterator() = default;
1625    explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1626        : Current(Current) {
1627      findAcceptableCategory();
1628    }
1629
1630    reference operator*() const { return Current; }
1631    pointer operator->() const { return Current; }
1632
1633    filtered_category_iterator &operator++();
1634
1635    filtered_category_iterator operator++(int) {
1636      filtered_category_iterator Tmp = *this;
1637      ++(*this);
1638      return Tmp;
1639    }
1640
1641    friend bool operator==(filtered_category_iterator X,
1642                           filtered_category_iterator Y) {
1643      return X.Current == Y.Current;
1644    }
1645
1646    friend bool operator!=(filtered_category_iterator X,
1647                           filtered_category_iterator Y) {
1648      return X.Current != Y.Current;
1649    }
1650  };
1651
1652private:
1653  /// Test whether the given category is visible.
1654  ///
1655  /// Used in the \c visible_categories_iterator.
1656  static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1657
1658public:
1659  /// Iterator that walks over the list of categories and extensions
1660  /// that are visible, i.e., not hidden in a non-imported submodule.
1661  using visible_categories_iterator =
1662      filtered_category_iterator<isVisibleCategory>;
1663
1664  using visible_categories_range =
1665      llvm::iterator_range<visible_categories_iterator>;
1666
1667  visible_categories_range visible_categories() const {
1668    return visible_categories_range(visible_categories_begin(),
1669                                    visible_categories_end());
1670  }
1671
1672  /// Retrieve an iterator to the beginning of the visible-categories
1673  /// list.
1674  visible_categories_iterator visible_categories_begin() const {
1675    return visible_categories_iterator(getCategoryListRaw());
1676  }
1677
1678  /// Retrieve an iterator to the end of the visible-categories list.
1679  visible_categories_iterator visible_categories_end() const {
1680    return visible_categories_iterator();
1681  }
1682
1683  /// Determine whether the visible-categories list is empty.
1684  bool visible_categories_empty() const {
1685    return visible_categories_begin() == visible_categories_end();
1686  }
1687
1688private:
1689  /// Test whether the given category... is a category.
1690  ///
1691  /// Used in the \c known_categories_iterator.
1692  static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1693
1694public:
1695  /// Iterator that walks over all of the known categories and
1696  /// extensions, including those that are hidden.
1697  using known_categories_iterator = filtered_category_iterator<isKnownCategory>;
1698  using known_categories_range =
1699     llvm::iterator_range<known_categories_iterator>;
1700
1701  known_categories_range known_categories() const {
1702    return known_categories_range(known_categories_begin(),
1703                                  known_categories_end());
1704  }
1705
1706  /// Retrieve an iterator to the beginning of the known-categories
1707  /// list.
1708  known_categories_iterator known_categories_begin() const {
1709    return known_categories_iterator(getCategoryListRaw());
1710  }
1711
1712  /// Retrieve an iterator to the end of the known-categories list.
1713  known_categories_iterator known_categories_end() const {
1714    return known_categories_iterator();
1715  }
1716
1717  /// Determine whether the known-categories list is empty.
1718  bool known_categories_empty() const {
1719    return known_categories_begin() == known_categories_end();
1720  }
1721
1722private:
1723  /// Test whether the given category is a visible extension.
1724  ///
1725  /// Used in the \c visible_extensions_iterator.
1726  static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1727
1728public:
1729  /// Iterator that walks over all of the visible extensions, skipping
1730  /// any that are known but hidden.
1731  using visible_extensions_iterator =
1732      filtered_category_iterator<isVisibleExtension>;
1733
1734  using visible_extensions_range =
1735      llvm::iterator_range<visible_extensions_iterator>;
1736
1737  visible_extensions_range visible_extensions() const {
1738    return visible_extensions_range(visible_extensions_begin(),
1739                                    visible_extensions_end());
1740  }
1741
1742  /// Retrieve an iterator to the beginning of the visible-extensions
1743  /// list.
1744  visible_extensions_iterator visible_extensions_begin() const {
1745    return visible_extensions_iterator(getCategoryListRaw());
1746  }
1747
1748  /// Retrieve an iterator to the end of the visible-extensions list.
1749  visible_extensions_iterator visible_extensions_end() const {
1750    return visible_extensions_iterator();
1751  }
1752
1753  /// Determine whether the visible-extensions list is empty.
1754  bool visible_extensions_empty() const {
1755    return visible_extensions_begin() == visible_extensions_end();
1756  }
1757
1758private:
1759  /// Test whether the given category is an extension.
1760  ///
1761  /// Used in the \c known_extensions_iterator.
1762  static bool isKnownExtension(ObjCCategoryDecl *Cat);
1763
1764public:
1765  friend class ASTDeclReader;
1766  friend class ASTDeclWriter;
1767  friend class ASTReader;
1768
1769  /// Iterator that walks over all of the known extensions.
1770  using known_extensions_iterator =
1771      filtered_category_iterator<isKnownExtension>;
1772  using known_extensions_range =
1773      llvm::iterator_range<known_extensions_iterator>;
1774
1775  known_extensions_range known_extensions() const {
1776    return known_extensions_range(known_extensions_begin(),
1777                                  known_extensions_end());
1778  }
1779
1780  /// Retrieve an iterator to the beginning of the known-extensions
1781  /// list.
1782  known_extensions_iterator known_extensions_begin() const {
1783    return known_extensions_iterator(getCategoryListRaw());
1784  }
1785
1786  /// Retrieve an iterator to the end of the known-extensions list.
1787  known_extensions_iterator known_extensions_end() const {
1788    return known_extensions_iterator();
1789  }
1790
1791  /// Determine whether the known-extensions list is empty.
1792  bool known_extensions_empty() const {
1793    return known_extensions_begin() == known_extensions_end();
1794  }
1795
1796  /// Retrieve the raw pointer to the start of the category/extension
1797  /// list.
1798  ObjCCategoryDecl* getCategoryListRaw() const {
1799    // FIXME: Should make sure no callers ever do this.
1800    if (!hasDefinition())
1801      return nullptr;
1802
1803    if (data().ExternallyCompleted)
1804      LoadExternalDefinition();
1805
1806    return data().CategoryList;
1807  }
1808
1809  /// Set the raw pointer to the start of the category/extension
1810  /// list.
1811  void setCategoryListRaw(ObjCCategoryDecl *category) {
1812    data().CategoryList = category;
1813  }
1814
1815  ObjCPropertyDecl
1816    *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1817                                       ObjCPropertyQueryKind QueryKind) const;
1818
1819  void collectPropertiesToImplement(PropertyMap &PM,
1820                                    PropertyDeclOrder &PO) const override;
1821
1822  /// isSuperClassOf - Return true if this class is the specified class or is a
1823  /// super class of the specified interface class.
1824  bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1825    // If RHS is derived from LHS it is OK; else it is not OK.
1826    while (I != nullptr) {
1827      if (declaresSameEntity(this, I))
1828        return true;
1829
1830      I = I->getSuperClass();
1831    }
1832    return false;
1833  }
1834
1835  /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1836  /// to be incompatible with __weak references. Returns true if it is.
1837  bool isArcWeakrefUnavailable() const;
1838
1839  /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1840  /// classes must not be auto-synthesized. Returns class decl. if it must not
1841  /// be; 0, otherwise.
1842  const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1843
1844  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1845                                       ObjCInterfaceDecl *&ClassDeclared);
1846  ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1847    ObjCInterfaceDecl *ClassDeclared;
1848    return lookupInstanceVariable(IVarName, ClassDeclared);
1849  }
1850
1851  ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1852
1853  // Lookup a method. First, we search locally. If a method isn't
1854  // found, we search referenced protocols and class categories.
1855  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1856                               bool shallowCategoryLookup = false,
1857                               bool followSuper = true,
1858                               const ObjCCategoryDecl *C = nullptr) const;
1859
1860  /// Lookup an instance method for a given selector.
1861  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1862    return lookupMethod(Sel, true/*isInstance*/);
1863  }
1864
1865  /// Lookup a class method for a given selector.
1866  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1867    return lookupMethod(Sel, false/*isInstance*/);
1868  }
1869
1870  ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1871
1872  /// Lookup a method in the classes implementation hierarchy.
1873  ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1874                                      bool Instance=true) const;
1875
1876  ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1877    return lookupPrivateMethod(Sel, false);
1878  }
1879
1880  /// Lookup a setter or getter in the class hierarchy,
1881  /// including in all categories except for category passed
1882  /// as argument.
1883  ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1884                                         const ObjCCategoryDecl *Cat,
1885                                         bool IsClassProperty) const {
1886    return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1887                        false/*shallowCategoryLookup*/,
1888                        true /* followsSuper */,
1889                        Cat);
1890  }
1891
1892  SourceLocation getEndOfDefinitionLoc() const {
1893    if (!hasDefinition())
1894      return getLocation();
1895
1896    return data().EndLoc;
1897  }
1898
1899  void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1900
1901  /// Retrieve the starting location of the superclass.
1902  SourceLocation getSuperClassLoc() const;
1903
1904  /// isImplicitInterfaceDecl - check that this is an implicitly declared
1905  /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1906  /// declaration without an \@interface declaration.
1907  bool isImplicitInterfaceDecl() const {
1908    return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1909  }
1910
1911  /// ClassImplementsProtocol - Checks that 'lProto' protocol
1912  /// has been implemented in IDecl class, its super class or categories (if
1913  /// lookupCategory is true).
1914  bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1915                               bool lookupCategory,
1916                               bool RHSIsQualifiedID = false);
1917
1918  using redecl_range = redeclarable_base::redecl_range;
1919  using redecl_iterator = redeclarable_base::redecl_iterator;
1920
1921  using redeclarable_base::redecls_begin;
1922  using redeclarable_base::redecls_end;
1923  using redeclarable_base::redecls;
1924  using redeclarable_base::getPreviousDecl;
1925  using redeclarable_base::getMostRecentDecl;
1926  using redeclarable_base::isFirstDecl;
1927
1928  /// Retrieves the canonical declaration of this Objective-C class.
1929  ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1930  const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1931
1932  // Low-level accessor
1933  const Type *getTypeForDecl() const { return TypeForDecl; }
1934  void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1935
1936  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1937  static bool classofKind(Kind K) { return K == ObjCInterface; }
1938
1939private:
1940  const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1941  bool inheritsDesignatedInitializers() const;
1942};
1943
1944/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1945/// instance variables are identical to C. The only exception is Objective-C
1946/// supports C++ style access control. For example:
1947///
1948///   \@interface IvarExample : NSObject
1949///   {
1950///     id defaultToProtected;
1951///   \@public:
1952///     id canBePublic; // same as C++.
1953///   \@protected:
1954///     id canBeProtected; // same as C++.
1955///   \@package:
1956///     id canBePackage; // framework visibility (not available in C++).
1957///   }
1958///
1959class ObjCIvarDecl : public FieldDecl {
1960  void anchor() override;
1961
1962public:
1963  enum AccessControl {
1964    None, Private, Protected, Public, Package
1965  };
1966
1967private:
1968  ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1969               SourceLocation IdLoc, IdentifierInfo *Id,
1970               QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1971               bool synthesized)
1972      : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1973                  /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1974        DeclAccess(ac), Synthesized(synthesized) {}
1975
1976public:
1977  static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1978                              SourceLocation StartLoc, SourceLocation IdLoc,
1979                              IdentifierInfo *Id, QualType T,
1980                              TypeSourceInfo *TInfo,
1981                              AccessControl ac, Expr *BW = nullptr,
1982                              bool synthesized=false);
1983
1984  static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1985
1986  /// Return the class interface that this ivar is logically contained
1987  /// in; this is either the interface where the ivar was declared, or the
1988  /// interface the ivar is conceptually a part of in the case of synthesized
1989  /// ivars.
1990  const ObjCInterfaceDecl *getContainingInterface() const;
1991
1992  ObjCIvarDecl *getNextIvar() { return NextIvar; }
1993  const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1994  void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1995
1996  void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1997
1998  AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1999
2000  AccessControl getCanonicalAccessControl() const {
2001    return DeclAccess == None ? Protected : AccessControl(DeclAccess);
2002  }
2003
2004  void setSynthesize(bool synth) { Synthesized = synth; }
2005  bool getSynthesize() const { return Synthesized; }
2006
2007  /// Retrieve the type of this instance variable when viewed as a member of a
2008  /// specific object type.
2009  QualType getUsageType(QualType objectType) const;
2010
2011  // Implement isa/cast/dyncast/etc.
2012  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2013  static bool classofKind(Kind K) { return K == ObjCIvar; }
2014
2015private:
2016  /// NextIvar - Next Ivar in the list of ivars declared in class; class's
2017  /// extensions and class's implementation
2018  ObjCIvarDecl *NextIvar = nullptr;
2019
2020  // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
2021  unsigned DeclAccess : 3;
2022  unsigned Synthesized : 1;
2023};
2024
2025/// Represents a field declaration created by an \@defs(...).
2026class ObjCAtDefsFieldDecl : public FieldDecl {
2027  ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
2028                      SourceLocation IdLoc, IdentifierInfo *Id,
2029                      QualType T, Expr *BW)
2030      : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
2031                  /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
2032                  BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
2033
2034  void anchor() override;
2035
2036public:
2037  static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
2038                                     SourceLocation StartLoc,
2039                                     SourceLocation IdLoc, IdentifierInfo *Id,
2040                                     QualType T, Expr *BW);
2041
2042  static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2043
2044  // Implement isa/cast/dyncast/etc.
2045  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2046  static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
2047};
2048
2049/// Represents an Objective-C protocol declaration.
2050///
2051/// Objective-C protocols declare a pure abstract type (i.e., no instance
2052/// variables are permitted).  Protocols originally drew inspiration from
2053/// C++ pure virtual functions (a C++ feature with nice semantics and lousy
2054/// syntax:-). Here is an example:
2055///
2056/// \code
2057/// \@protocol NSDraggingInfo <refproto1, refproto2>
2058/// - (NSWindow *)draggingDestinationWindow;
2059/// - (NSImage *)draggedImage;
2060/// \@end
2061/// \endcode
2062///
2063/// This says that NSDraggingInfo requires two methods and requires everything
2064/// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
2065/// well.
2066///
2067/// \code
2068/// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
2069/// \@end
2070/// \endcode
2071///
2072/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
2073/// protocols are in distinct namespaces. For example, Cocoa defines both
2074/// an NSObject protocol and class (which isn't allowed in Java). As a result,
2075/// protocols are referenced using angle brackets as follows:
2076///
2077/// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
2078class ObjCProtocolDecl : public ObjCContainerDecl,
2079                         public Redeclarable<ObjCProtocolDecl> {
2080  struct DefinitionData {
2081    // The declaration that defines this protocol.
2082    ObjCProtocolDecl *Definition;
2083
2084    /// Referenced protocols
2085    ObjCProtocolList ReferencedProtocols;
2086  };
2087
2088  /// Contains a pointer to the data associated with this class,
2089  /// which will be NULL if this class has not yet been defined.
2090  ///
2091  /// The bit indicates when we don't need to check for out-of-date
2092  /// declarations. It will be set unless modules are enabled.
2093  llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
2094
2095  ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
2096                   SourceLocation nameLoc, SourceLocation atStartLoc,
2097                   ObjCProtocolDecl *PrevDecl);
2098
2099  void anchor() override;
2100
2101  DefinitionData &data() const {
2102    assert(Data.getPointer() && "Objective-C protocol has no definition!");
2103    return *Data.getPointer();
2104  }
2105
2106  void allocateDefinitionData();
2107
2108  using redeclarable_base = Redeclarable<ObjCProtocolDecl>;
2109
2110  ObjCProtocolDecl *getNextRedeclarationImpl() override {
2111    return getNextRedeclaration();
2112  }
2113
2114  ObjCProtocolDecl *getPreviousDeclImpl() override {
2115    return getPreviousDecl();
2116  }
2117
2118  ObjCProtocolDecl *getMostRecentDeclImpl() override {
2119    return getMostRecentDecl();
2120  }
2121
2122public:
2123  friend class ASTDeclReader;
2124  friend class ASTDeclWriter;
2125  friend class ASTReader;
2126
2127  static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2128                                  IdentifierInfo *Id,
2129                                  SourceLocation nameLoc,
2130                                  SourceLocation atStartLoc,
2131                                  ObjCProtocolDecl *PrevDecl);
2132
2133  static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2134
2135  const ObjCProtocolList &getReferencedProtocols() const {
2136    assert(hasDefinition() && "No definition available!");
2137    return data().ReferencedProtocols;
2138  }
2139
2140  using protocol_iterator = ObjCProtocolList::iterator;
2141  using protocol_range = llvm::iterator_range<protocol_iterator>;
2142
2143  protocol_range protocols() const {
2144    return protocol_range(protocol_begin(), protocol_end());
2145  }
2146
2147  protocol_iterator protocol_begin() const {
2148    if (!hasDefinition())
2149      return protocol_iterator();
2150
2151    return data().ReferencedProtocols.begin();
2152  }
2153
2154  protocol_iterator protocol_end() const {
2155    if (!hasDefinition())
2156      return protocol_iterator();
2157
2158    return data().ReferencedProtocols.end();
2159  }
2160
2161  using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2162  using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2163
2164  protocol_loc_range protocol_locs() const {
2165    return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2166  }
2167
2168  protocol_loc_iterator protocol_loc_begin() const {
2169    if (!hasDefinition())
2170      return protocol_loc_iterator();
2171
2172    return data().ReferencedProtocols.loc_begin();
2173  }
2174
2175  protocol_loc_iterator protocol_loc_end() const {
2176    if (!hasDefinition())
2177      return protocol_loc_iterator();
2178
2179    return data().ReferencedProtocols.loc_end();
2180  }
2181
2182  unsigned protocol_size() const {
2183    if (!hasDefinition())
2184      return 0;
2185
2186    return data().ReferencedProtocols.size();
2187  }
2188
2189  /// setProtocolList - Set the list of protocols that this interface
2190  /// implements.
2191  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2192                       const SourceLocation *Locs, ASTContext &C) {
2193    assert(hasDefinition() && "Protocol is not defined");
2194    data().ReferencedProtocols.set(List, Num, Locs, C);
2195  }
2196
2197  ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2198
2199  // Lookup a method. First, we search locally. If a method isn't
2200  // found, we search referenced protocols and class categories.
2201  ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2202
2203  ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2204    return lookupMethod(Sel, true/*isInstance*/);
2205  }
2206
2207  ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2208    return lookupMethod(Sel, false/*isInstance*/);
2209  }
2210
2211  /// Determine whether this protocol has a definition.
2212  bool hasDefinition() const {
2213    // If the name of this protocol is out-of-date, bring it up-to-date, which
2214    // might bring in a definition.
2215    // Note: a null value indicates that we don't have a definition and that
2216    // modules are enabled.
2217    if (!Data.getOpaqueValue())
2218      getMostRecentDecl();
2219
2220    return Data.getPointer();
2221  }
2222
2223  /// Retrieve the definition of this protocol, if any.
2224  ObjCProtocolDecl *getDefinition() {
2225    return hasDefinition()? Data.getPointer()->Definition : nullptr;
2226  }
2227
2228  /// Retrieve the definition of this protocol, if any.
2229  const ObjCProtocolDecl *getDefinition() const {
2230    return hasDefinition()? Data.getPointer()->Definition : nullptr;
2231  }
2232
2233  /// Determine whether this particular declaration is also the
2234  /// definition.
2235  bool isThisDeclarationADefinition() const {
2236    return getDefinition() == this;
2237  }
2238
2239  /// Starts the definition of this Objective-C protocol.
2240  void startDefinition();
2241
2242  /// Produce a name to be used for protocol's metadata. It comes either via
2243  /// objc_runtime_name attribute or protocol name.
2244  StringRef getObjCRuntimeNameAsString() const;
2245
2246  SourceRange getSourceRange() const override LLVM_READONLY {
2247    if (isThisDeclarationADefinition())
2248      return ObjCContainerDecl::getSourceRange();
2249
2250    return SourceRange(getAtStartLoc(), getLocation());
2251  }
2252
2253  using redecl_range = redeclarable_base::redecl_range;
2254  using redecl_iterator = redeclarable_base::redecl_iterator;
2255
2256  using redeclarable_base::redecls_begin;
2257  using redeclarable_base::redecls_end;
2258  using redeclarable_base::redecls;
2259  using redeclarable_base::getPreviousDecl;
2260  using redeclarable_base::getMostRecentDecl;
2261  using redeclarable_base::isFirstDecl;
2262
2263  /// Retrieves the canonical declaration of this Objective-C protocol.
2264  ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2265  const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2266
2267  void collectPropertiesToImplement(PropertyMap &PM,
2268                                    PropertyDeclOrder &PO) const override;
2269
2270  void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2271                                          ProtocolPropertySet &PS,
2272                                          PropertyDeclOrder &PO) const;
2273
2274  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2275  static bool classofKind(Kind K) { return K == ObjCProtocol; }
2276};
2277
2278/// ObjCCategoryDecl - Represents a category declaration. A category allows
2279/// you to add methods to an existing class (without subclassing or modifying
2280/// the original class interface or implementation:-). Categories don't allow
2281/// you to add instance data. The following example adds "myMethod" to all
2282/// NSView's within a process:
2283///
2284/// \@interface NSView (MyViewMethods)
2285/// - myMethod;
2286/// \@end
2287///
2288/// Categories also allow you to split the implementation of a class across
2289/// several files (a feature more naturally supported in C++).
2290///
2291/// Categories were originally inspired by dynamic languages such as Common
2292/// Lisp and Smalltalk.  More traditional class-based languages (C++, Java)
2293/// don't support this level of dynamism, which is both powerful and dangerous.
2294class ObjCCategoryDecl : public ObjCContainerDecl {
2295  /// Interface belonging to this category
2296  ObjCInterfaceDecl *ClassInterface;
2297
2298  /// The type parameters associated with this category, if any.
2299  ObjCTypeParamList *TypeParamList = nullptr;
2300
2301  /// referenced protocols in this category.
2302  ObjCProtocolList ReferencedProtocols;
2303
2304  /// Next category belonging to this class.
2305  /// FIXME: this should not be a singly-linked list.  Move storage elsewhere.
2306  ObjCCategoryDecl *NextClassCategory = nullptr;
2307
2308  /// The location of the category name in this declaration.
2309  SourceLocation CategoryNameLoc;
2310
2311  /// class extension may have private ivars.
2312  SourceLocation IvarLBraceLoc;
2313  SourceLocation IvarRBraceLoc;
2314
2315  ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2316                   SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2317                   IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2318                   ObjCTypeParamList *typeParamList,
2319                   SourceLocation IvarLBraceLoc = SourceLocation(),
2320                   SourceLocation IvarRBraceLoc = SourceLocation());
2321
2322  void anchor() override;
2323
2324public:
2325  friend class ASTDeclReader;
2326  friend class ASTDeclWriter;
2327
2328  static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2329                                  SourceLocation AtLoc,
2330                                  SourceLocation ClassNameLoc,
2331                                  SourceLocation CategoryNameLoc,
2332                                  IdentifierInfo *Id,
2333                                  ObjCInterfaceDecl *IDecl,
2334                                  ObjCTypeParamList *typeParamList,
2335                                  SourceLocation IvarLBraceLoc=SourceLocation(),
2336                                  SourceLocation IvarRBraceLoc=SourceLocation());
2337  static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2338
2339  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2340  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2341
2342  /// Retrieve the type parameter list associated with this category or
2343  /// extension.
2344  ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2345
2346  /// Set the type parameters of this category.
2347  ///
2348  /// This function is used by the AST importer, which must import the type
2349  /// parameters after creating their DeclContext to avoid loops.
2350  void setTypeParamList(ObjCTypeParamList *TPL);
2351
2352
2353  ObjCCategoryImplDecl *getImplementation() const;
2354  void setImplementation(ObjCCategoryImplDecl *ImplD);
2355
2356  /// setProtocolList - Set the list of protocols that this interface
2357  /// implements.
2358  void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2359                       const SourceLocation *Locs, ASTContext &C) {
2360    ReferencedProtocols.set(List, Num, Locs, C);
2361  }
2362
2363  const ObjCProtocolList &getReferencedProtocols() const {
2364    return ReferencedProtocols;
2365  }
2366
2367  using protocol_iterator = ObjCProtocolList::iterator;
2368  using protocol_range = llvm::iterator_range<protocol_iterator>;
2369
2370  protocol_range protocols() const {
2371    return protocol_range(protocol_begin(), protocol_end());
2372  }
2373
2374  protocol_iterator protocol_begin() const {
2375    return ReferencedProtocols.begin();
2376  }
2377
2378  protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2379  unsigned protocol_size() const { return ReferencedProtocols.size(); }
2380
2381  using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2382  using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2383
2384  protocol_loc_range protocol_locs() const {
2385    return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2386  }
2387
2388  protocol_loc_iterator protocol_loc_begin() const {
2389    return ReferencedProtocols.loc_begin();
2390  }
2391
2392  protocol_loc_iterator protocol_loc_end() const {
2393    return ReferencedProtocols.loc_end();
2394  }
2395
2396  ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2397
2398  /// Retrieve the pointer to the next stored category (or extension),
2399  /// which may be hidden.
2400  ObjCCategoryDecl *getNextClassCategoryRaw() const {
2401    return NextClassCategory;
2402  }
2403
2404  bool IsClassExtension() const { return getIdentifier() == nullptr; }
2405
2406  using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2407  using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2408
2409  ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2410
2411  ivar_iterator ivar_begin() const {
2412    return ivar_iterator(decls_begin());
2413  }
2414
2415  ivar_iterator ivar_end() const {
2416    return ivar_iterator(decls_end());
2417  }
2418
2419  unsigned ivar_size() const {
2420    return std::distance(ivar_begin(), ivar_end());
2421  }
2422
2423  bool ivar_empty() const {
2424    return ivar_begin() == ivar_end();
2425  }
2426
2427  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2428  void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2429
2430  void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2431  SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2432  void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2433  SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2434
2435  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2436  static bool classofKind(Kind K) { return K == ObjCCategory; }
2437};
2438
2439class ObjCImplDecl : public ObjCContainerDecl {
2440  /// Class interface for this class/category implementation
2441  ObjCInterfaceDecl *ClassInterface;
2442
2443  void anchor() override;
2444
2445protected:
2446  ObjCImplDecl(Kind DK, DeclContext *DC,
2447               ObjCInterfaceDecl *classInterface,
2448               IdentifierInfo *Id,
2449               SourceLocation nameLoc, SourceLocation atStartLoc)
2450      : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc),
2451        ClassInterface(classInterface) {}
2452
2453public:
2454  const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2455  ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2456  void setClassInterface(ObjCInterfaceDecl *IFace);
2457
2458  void addInstanceMethod(ObjCMethodDecl *method) {
2459    // FIXME: Context should be set correctly before we get here.
2460    method->setLexicalDeclContext(this);
2461    addDecl(method);
2462  }
2463
2464  void addClassMethod(ObjCMethodDecl *method) {
2465    // FIXME: Context should be set correctly before we get here.
2466    method->setLexicalDeclContext(this);
2467    addDecl(method);
2468  }
2469
2470  void addPropertyImplementation(ObjCPropertyImplDecl *property);
2471
2472  ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2473                            ObjCPropertyQueryKind queryKind) const;
2474  ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2475
2476  // Iterator access to properties.
2477  using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>;
2478  using propimpl_range =
2479      llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>;
2480
2481  propimpl_range property_impls() const {
2482    return propimpl_range(propimpl_begin(), propimpl_end());
2483  }
2484
2485  propimpl_iterator propimpl_begin() const {
2486    return propimpl_iterator(decls_begin());
2487  }
2488
2489  propimpl_iterator propimpl_end() const {
2490    return propimpl_iterator(decls_end());
2491  }
2492
2493  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2494
2495  static bool classofKind(Kind K) {
2496    return K >= firstObjCImpl && K <= lastObjCImpl;
2497  }
2498};
2499
2500/// ObjCCategoryImplDecl - An object of this class encapsulates a category
2501/// \@implementation declaration. If a category class has declaration of a
2502/// property, its implementation must be specified in the category's
2503/// \@implementation declaration. Example:
2504/// \@interface I \@end
2505/// \@interface I(CATEGORY)
2506///    \@property int p1, d1;
2507/// \@end
2508/// \@implementation I(CATEGORY)
2509///  \@dynamic p1,d1;
2510/// \@end
2511///
2512/// ObjCCategoryImplDecl
2513class ObjCCategoryImplDecl : public ObjCImplDecl {
2514  // Category name location
2515  SourceLocation CategoryNameLoc;
2516
2517  ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2518                       ObjCInterfaceDecl *classInterface,
2519                       SourceLocation nameLoc, SourceLocation atStartLoc,
2520                       SourceLocation CategoryNameLoc)
2521      : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id,
2522                     nameLoc, atStartLoc),
2523        CategoryNameLoc(CategoryNameLoc) {}
2524
2525  void anchor() override;
2526
2527public:
2528  friend class ASTDeclReader;
2529  friend class ASTDeclWriter;
2530
2531  static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2532                                      IdentifierInfo *Id,
2533                                      ObjCInterfaceDecl *classInterface,
2534                                      SourceLocation nameLoc,
2535                                      SourceLocation atStartLoc,
2536                                      SourceLocation CategoryNameLoc);
2537  static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2538
2539  ObjCCategoryDecl *getCategoryDecl() const;
2540
2541  SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2542
2543  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2544  static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2545};
2546
2547raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2548
2549/// ObjCImplementationDecl - Represents a class definition - this is where
2550/// method definitions are specified. For example:
2551///
2552/// @code
2553/// \@implementation MyClass
2554/// - (void)myMethod { /* do something */ }
2555/// \@end
2556/// @endcode
2557///
2558/// In a non-fragile runtime, instance variables can appear in the class
2559/// interface, class extensions (nameless categories), and in the implementation
2560/// itself, as well as being synthesized as backing storage for properties.
2561///
2562/// In a fragile runtime, instance variables are specified in the class
2563/// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2564/// we allow instance variables to be specified in the implementation. When
2565/// specified, they need to be \em identical to the interface.
2566class ObjCImplementationDecl : public ObjCImplDecl {
2567  /// Implementation Class's super class.
2568  ObjCInterfaceDecl *SuperClass;
2569  SourceLocation SuperLoc;
2570
2571  /// \@implementation may have private ivars.
2572  SourceLocation IvarLBraceLoc;
2573  SourceLocation IvarRBraceLoc;
2574
2575  /// Support for ivar initialization.
2576  /// The arguments used to initialize the ivars
2577  LazyCXXCtorInitializersPtr IvarInitializers;
2578  unsigned NumIvarInitializers = 0;
2579
2580  /// Do the ivars of this class require initialization other than
2581  /// zero-initialization?
2582  bool HasNonZeroConstructors : 1;
2583
2584  /// Do the ivars of this class require non-trivial destruction?
2585  bool HasDestructors : 1;
2586
2587  ObjCImplementationDecl(DeclContext *DC,
2588                         ObjCInterfaceDecl *classInterface,
2589                         ObjCInterfaceDecl *superDecl,
2590                         SourceLocation nameLoc, SourceLocation atStartLoc,
2591                         SourceLocation superLoc = SourceLocation(),
2592                         SourceLocation IvarLBraceLoc=SourceLocation(),
2593                         SourceLocation IvarRBraceLoc=SourceLocation())
2594      : ObjCImplDecl(ObjCImplementation, DC, classInterface,
2595                     classInterface ? classInterface->getIdentifier()
2596                                    : nullptr,
2597                     nameLoc, atStartLoc),
2598         SuperClass(superDecl), SuperLoc(superLoc),
2599         IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc),
2600         HasNonZeroConstructors(false), HasDestructors(false) {}
2601
2602  void anchor() override;
2603
2604public:
2605  friend class ASTDeclReader;
2606  friend class ASTDeclWriter;
2607
2608  static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2609                                        ObjCInterfaceDecl *classInterface,
2610                                        ObjCInterfaceDecl *superDecl,
2611                                        SourceLocation nameLoc,
2612                                        SourceLocation atStartLoc,
2613                                     SourceLocation superLoc = SourceLocation(),
2614                                        SourceLocation IvarLBraceLoc=SourceLocation(),
2615                                        SourceLocation IvarRBraceLoc=SourceLocation());
2616
2617  static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2618
2619  /// init_iterator - Iterates through the ivar initializer list.
2620  using init_iterator = CXXCtorInitializer **;
2621
2622  /// init_const_iterator - Iterates through the ivar initializer list.
2623  using init_const_iterator = CXXCtorInitializer * const *;
2624
2625  using init_range = llvm::iterator_range<init_iterator>;
2626  using init_const_range = llvm::iterator_range<init_const_iterator>;
2627
2628  init_range inits() { return init_range(init_begin(), init_end()); }
2629
2630  init_const_range inits() const {
2631    return init_const_range(init_begin(), init_end());
2632  }
2633
2634  /// init_begin() - Retrieve an iterator to the first initializer.
2635  init_iterator init_begin() {
2636    const auto *ConstThis = this;
2637    return const_cast<init_iterator>(ConstThis->init_begin());
2638  }
2639
2640  /// begin() - Retrieve an iterator to the first initializer.
2641  init_const_iterator init_begin() const;
2642
2643  /// init_end() - Retrieve an iterator past the last initializer.
2644  init_iterator       init_end()       {
2645    return init_begin() + NumIvarInitializers;
2646  }
2647
2648  /// end() - Retrieve an iterator past the last initializer.
2649  init_const_iterator init_end() const {
2650    return init_begin() + NumIvarInitializers;
2651  }
2652
2653  /// getNumArgs - Number of ivars which must be initialized.
2654  unsigned getNumIvarInitializers() const {
2655    return NumIvarInitializers;
2656  }
2657
2658  void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2659    NumIvarInitializers = numNumIvarInitializers;
2660  }
2661
2662  void setIvarInitializers(ASTContext &C,
2663                           CXXCtorInitializer ** initializers,
2664                           unsigned numInitializers);
2665
2666  /// Do any of the ivars of this class (not counting its base classes)
2667  /// require construction other than zero-initialization?
2668  bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2669  void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2670
2671  /// Do any of the ivars of this class (not counting its base classes)
2672  /// require non-trivial destruction?
2673  bool hasDestructors() const { return HasDestructors; }
2674  void setHasDestructors(bool val) { HasDestructors = val; }
2675
2676  /// getIdentifier - Get the identifier that names the class
2677  /// interface associated with this implementation.
2678  IdentifierInfo *getIdentifier() const {
2679    return getClassInterface()->getIdentifier();
2680  }
2681
2682  /// getName - Get the name of identifier for the class interface associated
2683  /// with this implementation as a StringRef.
2684  //
2685  // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2686  // meaning.
2687  StringRef getName() const {
2688    assert(getIdentifier() && "Name is not a simple identifier");
2689    return getIdentifier()->getName();
2690  }
2691
2692  /// Get the name of the class associated with this interface.
2693  //
2694  // FIXME: Move to StringRef API.
2695  std::string getNameAsString() const {
2696    return getName();
2697  }
2698
2699  /// Produce a name to be used for class's metadata. It comes either via
2700  /// class's objc_runtime_name attribute or class name.
2701  StringRef getObjCRuntimeNameAsString() const;
2702
2703  const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2704  ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2705  SourceLocation getSuperClassLoc() const { return SuperLoc; }
2706
2707  void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2708
2709  void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2710  SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2711  void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2712  SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2713
2714  using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2715  using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2716
2717  ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2718
2719  ivar_iterator ivar_begin() const {
2720    return ivar_iterator(decls_begin());
2721  }
2722
2723  ivar_iterator ivar_end() const {
2724    return ivar_iterator(decls_end());
2725  }
2726
2727  unsigned ivar_size() const {
2728    return std::distance(ivar_begin(), ivar_end());
2729  }
2730
2731  bool ivar_empty() const {
2732    return ivar_begin() == ivar_end();
2733  }
2734
2735  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2736  static bool classofKind(Kind K) { return K == ObjCImplementation; }
2737};
2738
2739raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2740
2741/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2742/// declared as \@compatibility_alias alias class.
2743class ObjCCompatibleAliasDecl : public NamedDecl {
2744  /// Class that this is an alias of.
2745  ObjCInterfaceDecl *AliasedClass;
2746
2747  ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2748                          ObjCInterfaceDecl* aliasedClass)
2749      : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2750
2751  void anchor() override;
2752
2753public:
2754  static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2755                                         SourceLocation L, IdentifierInfo *Id,
2756                                         ObjCInterfaceDecl* aliasedClass);
2757
2758  static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2759                                                     unsigned ID);
2760
2761  const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2762  ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2763  void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2764
2765  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2766  static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2767};
2768
2769/// ObjCPropertyImplDecl - Represents implementation declaration of a property
2770/// in a class or category implementation block. For example:
2771/// \@synthesize prop1 = ivar1;
2772///
2773class ObjCPropertyImplDecl : public Decl {
2774public:
2775  enum Kind {
2776    Synthesize,
2777    Dynamic
2778  };
2779
2780private:
2781  SourceLocation AtLoc;   // location of \@synthesize or \@dynamic
2782
2783  /// For \@synthesize, the location of the ivar, if it was written in
2784  /// the source code.
2785  ///
2786  /// \code
2787  /// \@synthesize int a = b
2788  /// \endcode
2789  SourceLocation IvarLoc;
2790
2791  /// Property declaration being implemented
2792  ObjCPropertyDecl *PropertyDecl;
2793
2794  /// Null for \@dynamic. Required for \@synthesize.
2795  ObjCIvarDecl *PropertyIvarDecl;
2796
2797  /// The getter's definition, which has an empty body if synthesized.
2798  ObjCMethodDecl *GetterMethodDecl = nullptr;
2799  /// The getter's definition, which has an empty body if synthesized.
2800  ObjCMethodDecl *SetterMethodDecl = nullptr;
2801
2802  /// Null for \@dynamic. Non-null if property must be copy-constructed in
2803  /// getter.
2804  Expr *GetterCXXConstructor = nullptr;
2805
2806  /// Null for \@dynamic. Non-null if property has assignment operator to call
2807  /// in Setter synthesis.
2808  Expr *SetterCXXAssignment = nullptr;
2809
2810  ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2811                       ObjCPropertyDecl *property,
2812                       Kind PK,
2813                       ObjCIvarDecl *ivarDecl,
2814                       SourceLocation ivarLoc)
2815      : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2816        IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) {
2817    assert(PK == Dynamic || PropertyIvarDecl);
2818  }
2819
2820public:
2821  friend class ASTDeclReader;
2822
2823  static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2824                                      SourceLocation atLoc, SourceLocation L,
2825                                      ObjCPropertyDecl *property,
2826                                      Kind PK,
2827                                      ObjCIvarDecl *ivarDecl,
2828                                      SourceLocation ivarLoc);
2829
2830  static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2831
2832  SourceRange getSourceRange() const override LLVM_READONLY;
2833
2834  SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
2835  void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2836
2837  ObjCPropertyDecl *getPropertyDecl() const {
2838    return PropertyDecl;
2839  }
2840  void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2841
2842  Kind getPropertyImplementation() const {
2843    return PropertyIvarDecl ? Synthesize : Dynamic;
2844  }
2845
2846  ObjCIvarDecl *getPropertyIvarDecl() const {
2847    return PropertyIvarDecl;
2848  }
2849  SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2850
2851  void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2852                           SourceLocation IvarLoc) {
2853    PropertyIvarDecl = Ivar;
2854    this->IvarLoc = IvarLoc;
2855  }
2856
2857  /// For \@synthesize, returns true if an ivar name was explicitly
2858  /// specified.
2859  ///
2860  /// \code
2861  /// \@synthesize int a = b; // true
2862  /// \@synthesize int a; // false
2863  /// \endcode
2864  bool isIvarNameSpecified() const {
2865    return IvarLoc.isValid() && IvarLoc != getLocation();
2866  }
2867
2868  ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2869  void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; }
2870
2871  ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2872  void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; }
2873
2874  Expr *getGetterCXXConstructor() const {
2875    return GetterCXXConstructor;
2876  }
2877
2878  void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2879    GetterCXXConstructor = getterCXXConstructor;
2880  }
2881
2882  Expr *getSetterCXXAssignment() const {
2883    return SetterCXXAssignment;
2884  }
2885
2886  void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2887    SetterCXXAssignment = setterCXXAssignment;
2888  }
2889
2890  static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2891  static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2892};
2893
2894template<bool (*Filter)(ObjCCategoryDecl *)>
2895void
2896ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2897findAcceptableCategory() {
2898  while (Current && !Filter(Current))
2899    Current = Current->getNextClassCategoryRaw();
2900}
2901
2902template<bool (*Filter)(ObjCCategoryDecl *)>
2903inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2904ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2905  Current = Current->getNextClassCategoryRaw();
2906  findAcceptableCategory();
2907  return *this;
2908}
2909
2910inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2911  return !Cat->isHidden();
2912}
2913
2914inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2915  return Cat->IsClassExtension() && !Cat->isHidden();
2916}
2917
2918inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2919  return Cat->IsClassExtension();
2920}
2921
2922} // namespace clang
2923
2924#endif // LLVM_CLANG_AST_DECLOBJC_H
2925