Coverage Report

Created: 2021-01-19 06:58

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