Coverage Report

Created: 2020-09-19 12:23

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