Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/DeclObjC.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DeclObjC.cpp - ObjC Declaration AST Node Implementation ------------===//
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 implements the Objective-C related Decl classes.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/DeclObjC.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/ASTMutationListener.h"
16
#include "clang/AST/Attr.h"
17
#include "clang/AST/Decl.h"
18
#include "clang/AST/DeclBase.h"
19
#include "clang/AST/Stmt.h"
20
#include "clang/AST/Type.h"
21
#include "clang/AST/TypeLoc.h"
22
#include "clang/Basic/IdentifierTable.h"
23
#include "clang/Basic/LLVM.h"
24
#include "clang/Basic/LangOptions.h"
25
#include "clang/Basic/SourceLocation.h"
26
#include "llvm/ADT/None.h"
27
#include "llvm/ADT/SmallString.h"
28
#include "llvm/ADT/SmallVector.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/ErrorHandling.h"
31
#include "llvm/Support/raw_ostream.h"
32
#include <algorithm>
33
#include <cassert>
34
#include <cstdint>
35
#include <cstring>
36
#include <utility>
37
38
using namespace clang;
39
40
//===----------------------------------------------------------------------===//
41
// ObjCListBase
42
//===----------------------------------------------------------------------===//
43
44
70.7k
void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
45
70.7k
  List = nullptr;
46
70.7k
  if (Elts == 0) 
return7.47k
; // Setting to an empty list is a noop.
47
63.2k
48
63.2k
  List = new (Ctx) void*[Elts];
49
63.2k
  NumElts = Elts;
50
63.2k
  memcpy(List, InList, sizeof(void*)*Elts);
51
63.2k
}
52
53
void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
54
83.2k
                           const SourceLocation *Locs, ASTContext &Ctx) {
55
83.2k
  if (Elts == 0)
56
26.2k
    return;
57
56.9k
58
56.9k
  Locations = new (Ctx) SourceLocation[Elts];
59
56.9k
  memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
60
56.9k
  set(InList, Elts, Ctx);
61
56.9k
}
62
63
//===----------------------------------------------------------------------===//
64
// ObjCInterfaceDecl
65
//===----------------------------------------------------------------------===//
66
67
ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
68
                                     IdentifierInfo *Id, SourceLocation nameLoc,
69
                                     SourceLocation atStartLoc)
70
465k
    : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
71
465k
  setAtStartLoc(atStartLoc);
72
465k
}
73
74
0
void ObjCContainerDecl::anchor() {}
75
76
/// getIvarDecl - This method looks up an ivar in this ContextDecl.
77
///
78
ObjCIvarDecl *
79
293k
ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
80
293k
  lookup_result R = lookup(Id);
81
293k
  for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
82
297k
       Ivar != IvarEnd; 
++Ivar4.11k
) {
83
9.26k
    if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
84
5.15k
      return ivar;
85
9.26k
  }
86
288k
  return nullptr;
87
293k
}
88
89
// Get the local instance/class method declared in this interface.
90
ObjCMethodDecl *
91
ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
92
16.3M
                             bool AllowHidden) const {
93
  // If this context is a hidden protocol definition, don't find any
94
  // methods there.
95
16.3M
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
96
2.72M
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
97
2.72M
      if (!Def->isUnconditionallyVisible() && 
!AllowHidden18
)
98
6
        return nullptr;
99
16.3M
  }
100
16.3M
101
  // Since instance & class methods can have the same name, the loop below
102
  // ensures we get the correct method.
103
  //
104
  // @interface Whatever
105
  // - (int) class_method;
106
  // + (float) class_method;
107
  // @end
108
16.3M
  lookup_result R = lookup(Sel);
109
16.3M
  for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
110
16.3M
       Meth != MethEnd; 
++Meth11.3k
) {
111
291k
    auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
112
291k
    if (MD && MD->isInstanceMethod() == isInstance)
113
280k
      return MD;
114
291k
  }
115
16.0M
  return nullptr;
116
16.3M
}
117
118
/// This routine returns 'true' if a user declared setter method was
119
/// found in the class, its protocols, its super classes or categories.
120
/// It also returns 'true' if one of its categories has declared a 'readwrite'
121
/// property.  This is because, user must provide a setter method for the
122
/// category's 'readwrite' property.
123
bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
124
46
    const ObjCPropertyDecl *Property) const {
125
46
  Selector Sel = Property->getSetterName();
126
46
  lookup_result R = lookup(Sel);
127
46
  for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
128
57
       Meth != MethEnd; 
++Meth11
) {
129
16
    auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
130
16
    if (MD && MD->isInstanceMethod() && !MD->isImplicit())
131
5
      return true;
132
16
  }
133
46
134
41
  if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
135
    // Also look into categories, including class extensions, looking
136
    // for a user declared instance method.
137
11
    for (const auto *Cat : ID->visible_categories()) {
138
11
      if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
139
6
        if (!MD->isImplicit())
140
2
          return true;
141
9
      if (Cat->IsClassExtension())
142
3
        continue;
143
      // Also search through the categories looking for a 'readwrite'
144
      // declaration of this property. If one found, presumably a setter will
145
      // be provided (properties declared in categories will not get
146
      // auto-synthesized).
147
6
      for (const auto *P : Cat->properties())
148
4
        if (P->getIdentifier() == Property->getIdentifier()) {
149
2
          if (P->getPropertyAttributes() &
150
2
              ObjCPropertyAttribute::kind_readwrite)
151
2
            return true;
152
0
          break;
153
0
        }
154
6
    }
155
22
156
    // Also look into protocols, for a user declared instance method.
157
18
    for (const auto *Proto : ID->all_referenced_protocols())
158
10
      if (Proto->HasUserDeclaredSetterMethod(Property))
159
2
        return true;
160
18
161
    // And in its super class.
162
16
    ObjCInterfaceDecl *OSC = ID->getSuperClass();
163
25
    while (OSC) {
164
13
      if (OSC->HasUserDeclaredSetterMethod(Property))
165
4
        return true;
166
9
      OSC = OSC->getSuperClass();
167
9
    }
168
16
  }
169
31
  if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
170
14
    for (const auto *PI : PD->protocols())
171
6
      if (PI->HasUserDeclaredSetterMethod(Property))
172
2
        return true;
173
29
  return false;
174
31
}
175
176
ObjCPropertyDecl *
177
ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
178
                                   const IdentifierInfo *propertyID,
179
480k
                                   ObjCPropertyQueryKind queryKind) {
180
  // If this context is a hidden protocol definition, don't find any
181
  // property.
182
480k
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
183
7.58k
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
184
7.58k
      if (!Def->isUnconditionallyVisible())
185
0
        return nullptr;
186
480k
  }
187
480k
188
  // If context is class, then lookup property in its visible extensions.
189
  // This comes before property is looked up in primary class.
190
480k
  if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
191
401k
    for (const auto *Ext : IDecl->visible_extensions())
192
1.15k
      if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
193
20
                                                       propertyID,
194
20
                                                       queryKind))
195
20
        return PD;
196
401k
  }
197
480k
198
480k
  DeclContext::lookup_result R = DC->lookup(propertyID);
199
480k
  ObjCPropertyDecl *classProp = nullptr;
200
486k
  for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
201
6.71k
       ++I)
202
11.9k
    if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
203
      // If queryKind is unknown, we return the instance property if one
204
      // exists; otherwise we return the class property.
205
5.82k
      if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
206
1.75k
           !PD->isClassProperty()) ||
207
4.07k
          (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
208
404
           PD->isClassProperty()) ||
209
3.98k
          (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
210
3.66k
           !PD->isClassProperty()))
211
5.19k
        return PD;
212
627
213
627
      if (PD->isClassProperty())
214
316
        classProp = PD;
215
627
    }
216
480k
217
475k
  if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
218
    // We can't find the instance property, return the class property.
219
540
    return classProp;
220
474k
221
474k
  return nullptr;
222
474k
}
223
224
IdentifierInfo *
225
1.23k
ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
226
1.23k
  SmallString<128> ivarName;
227
1.23k
  {
228
1.23k
    llvm::raw_svector_ostream os(ivarName);
229
1.23k
    os << '_' << getIdentifier()->getName();
230
1.23k
  }
231
1.23k
  return &Ctx.Idents.get(ivarName.str());
232
1.23k
}
233
234
/// FindPropertyDeclaration - Finds declaration of the property given its name
235
/// in 'PropertyId' and returns it. It returns 0, if not found.
236
ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
237
    const IdentifierInfo *PropertyId,
238
17.1k
    ObjCPropertyQueryKind QueryKind) const {
239
  // Don't find properties within hidden protocol definitions.
240
17.1k
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
241
1.23k
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
242
1.23k
      if (!Def->isUnconditionallyVisible())
243
6
        return nullptr;
244
17.1k
  }
245
17.1k
246
  // Search the extensions of a class first; they override what's in
247
  // the class itself.
248
17.1k
  if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
249
780
    for (const auto *Ext : ClassDecl->visible_extensions()) {
250
780
      if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
251
326
        return P;
252
780
    }
253
9.51k
  }
254
17.1k
255
16.8k
  if (ObjCPropertyDecl *PD =
256
5.05k
        ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
257
5.05k
                                           QueryKind))
258
5.05k
    return PD;
259
11.7k
260
11.7k
  switch (getKind()) {
261
0
    default:
262
0
      break;
263
1.07k
    case Decl::ObjCProtocol: {
264
1.07k
      const auto *PID = cast<ObjCProtocolDecl>(this);
265
1.07k
      for (const auto *I : PID->protocols())
266
322
        if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
267
18
                                                             QueryKind))
268
18
          return P;
269
1.05k
      break;
270
1.07k
    }
271
4.64k
    case Decl::ObjCInterface: {
272
4.64k
      const auto *OID = cast<ObjCInterfaceDecl>(this);
273
      // Look through categories (but not extensions; they were handled above).
274
5.66k
      for (const auto *Cat : OID->visible_categories()) {
275
5.66k
        if (!Cat->IsClassExtension())
276
5.60k
          if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
277
29
                                             PropertyId, QueryKind))
278
29
            return P;
279
5.66k
      }
280
4.64k
281
      // Look through protocols.
282
4.61k
      for (const auto *I : OID->all_referenced_protocols())
283
540
        if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
284
117
                                                             QueryKind))
285
117
          return P;
286
4.61k
287
      // Finally, check the super class.
288
4.49k
      if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
289
1.91k
        return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
290
2.58k
      break;
291
2.58k
    }
292
6.04k
    case Decl::ObjCCategory: {
293
6.04k
      const auto *OCD = cast<ObjCCategoryDecl>(this);
294
      // Look through protocols.
295
6.04k
      if (!OCD->IsClassExtension())
296
5.58k
        for (const auto *I : OCD->protocols())
297
23
          if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
298
10
                                                               QueryKind))
299
10
            return P;
300
6.03k
      break;
301
9.66k
    }
302
9.66k
  }
303
9.66k
  return nullptr;
304
9.66k
}
305
306
0
void ObjCInterfaceDecl::anchor() {}
307
308
284k
ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
309
  // If this particular declaration has a type parameter list, return it.
310
284k
  if (ObjCTypeParamList *written = getTypeParamListAsWritten())
311
211k
    return written;
312
73.1k
313
  // If there is a definition, return its type parameter list.
314
73.1k
  if (const ObjCInterfaceDecl *def = getDefinition())
315
45.3k
    return def->getTypeParamListAsWritten();
316
27.7k
317
  // Otherwise, look at previous declarations to determine whether any
318
  // of them has a type parameter list, skipping over those
319
  // declarations that do not.
320
71.0k
  
for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); 27.7k
decl;
321
46.6k
       
decl = decl->getPreviousDecl()43.2k
) {
322
46.6k
    if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
323
3.48k
      return written;
324
46.6k
  }
325
27.7k
326
24.3k
  return nullptr;
327
27.7k
}
328
329
356k
void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
330
356k
  TypeParamList = TPL;
331
356k
  if (!TPL)
332
323k
    return;
333
  // Set the declaration context of each of the type parameters.
334
33.6k
  for (auto *typeParam : *TypeParamList)
335
45.3k
    typeParam->setDeclContext(this);
336
33.6k
}
337
338
3.95M
ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
339
  // FIXME: Should make sure no callers ever do this.
340
3.95M
  if (!hasDefinition())
341
1.27k
    return nullptr;
342
3.95M
343
3.95M
  if (data().ExternallyCompleted)
344
0
    LoadExternalDefinition();
345
3.95M
346
3.95M
  if (const ObjCObjectType *superType = getSuperClassType()) {
347
2.34M
    if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
348
2.34M
      if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
349
2.34M
        return superDef;
350
6
351
6
      return superDecl;
352
6
    }
353
2.34M
  }
354
1.61M
355
1.61M
  return nullptr;
356
1.61M
}
357
358
206
SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
359
206
  if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
360
161
    return superTInfo->getTypeLoc().getBeginLoc();
361
45
362
45
  return SourceLocation();
363
45
}
364
365
/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
366
/// with name 'PropertyId' in the primary class; including those in protocols
367
/// (direct or indirect) used by the primary class.
368
ObjCPropertyDecl *
369
ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
370
                       IdentifierInfo *PropertyId,
371
682
                       ObjCPropertyQueryKind QueryKind) const {
372
  // FIXME: Should make sure no callers ever do this.
373
682
  if (!hasDefinition())
374
0
    return nullptr;
375
682
376
682
  if (data().ExternallyCompleted)
377
0
    LoadExternalDefinition();
378
682
379
682
  if (ObjCPropertyDecl *PD =
380
138
      ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
381
138
                                         QueryKind))
382
138
    return PD;
383
544
384
  // Look through protocols.
385
544
  for (const auto *I : all_referenced_protocols())
386
302
    if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
387
11
                                                         QueryKind))
388
11
      return P;
389
544
390
533
  return nullptr;
391
544
}
392
393
void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
394
8.71k
                                                     PropertyDeclOrder &PO) const {
395
4.18k
  for (auto *Prop : properties()) {
396
4.18k
    PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
397
4.18k
    PO.push_back(Prop);
398
4.18k
  }
399
549
  for (const auto *Ext : known_extensions()) {
400
549
    const ObjCCategoryDecl *ClassExt = Ext;
401
331
    for (auto *Prop : ClassExt->properties()) {
402
331
      PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
403
331
      PO.push_back(Prop);
404
331
    }
405
549
  }
406
8.71k
  for (const auto *PI : all_referenced_protocols())
407
2.30k
    PI->collectPropertiesToImplement(PM, PO);
408
  // Note, the properties declared only in class extensions are still copied
409
  // into the main @interface's property list, and therefore we don't
410
  // explicitly, have to search class extension properties.
411
8.71k
}
412
413
496
bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
414
496
  const ObjCInterfaceDecl *Class = this;
415
995
  while (Class) {
416
569
    if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
417
70
      return true;
418
499
    Class = Class->getSuperClass();
419
499
  }
420
426
  return false;
421
496
}
422
423
12.1k
const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
424
12.1k
  const ObjCInterfaceDecl *Class = this;
425
30.7k
  while (Class) {
426
18.6k
    if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
427
26
      return Class;
428
18.6k
    Class = Class->getSuperClass();
429
18.6k
  }
430
12.0k
  return nullptr;
431
12.1k
}
432
433
void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
434
                              ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
435
121
                              ASTContext &C) {
436
121
  if (data().ExternallyCompleted)
437
0
    LoadExternalDefinition();
438
121
439
121
  if (data().AllReferencedProtocols.empty() &&
440
120
      data().ReferencedProtocols.empty()) {
441
107
    data().AllReferencedProtocols.set(ExtList, ExtNum, C);
442
107
    return;
443
107
  }
444
14
445
  // Check for duplicate protocol in class's protocol list.
446
  // This is O(n*m). But it is extremely rare and number of protocols in
447
  // class or its extension are very few.
448
14
  SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
449
40
  for (unsigned i = 0; i < ExtNum; 
i++26
) {
450
26
    bool protocolExists = false;
451
26
    ObjCProtocolDecl *ProtoInExtension = ExtList[i];
452
26
    for (auto *Proto : all_referenced_protocols()) {
453
26
      if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
454
3
        protocolExists = true;
455
3
        break;
456
3
      }
457
26
    }
458
    // Do we want to warn on a protocol in extension class which
459
    // already exist in the class? Probably not.
460
26
    if (!protocolExists)
461
23
      ProtocolRefs.push_back(ProtoInExtension);
462
26
  }
463
14
464
14
  if (ProtocolRefs.empty())
465
1
    return;
466
13
467
  // Merge ProtocolRefs into class's protocol list;
468
13
  ProtocolRefs.append(all_referenced_protocol_begin(),
469
13
                      all_referenced_protocol_end());
470
13
471
13
  data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
472
13
}
473
474
const ObjCInterfaceDecl *
475
1.04k
ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
476
1.04k
  const ObjCInterfaceDecl *IFace = this;
477
1.23k
  while (IFace) {
478
1.23k
    if (IFace->hasDesignatedInitializers())
479
323
      return IFace;
480
915
    if (!IFace->inheritsDesignatedInitializers())
481
721
      break;
482
194
    IFace = IFace->getSuperClass();
483
194
  }
484
721
  return nullptr;
485
1.04k
}
486
487
676
static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
488
980
  for (const auto *MD : D->instance_methods()) {
489
980
    if (MD->getMethodFamily() == OMF_init && 
!MD->isOverriding()536
)
490
337
      return true;
491
980
  }
492
339
  for (const auto *Ext : D->visible_extensions()) {
493
58
    for (const auto *MD : Ext->instance_methods()) {
494
58
      if (MD->getMethodFamily() == OMF_init && 
!MD->isOverriding()1
)
495
1
        return true;
496
58
    }
497
16
  }
498
338
  if (const auto *ImplD = D->getImplementation()) {
499
677
    for (const auto *MD : ImplD->instance_methods()) {
500
677
      if (MD->getMethodFamily() == OMF_init && 
!MD->isOverriding()352
)
501
71
        return true;
502
677
    }
503
297
  }
504
267
  return false;
505
338
}
506
507
1.06k
bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
508
1.06k
  switch (data().InheritedDesignatedInitializers) {
509
93
  case DefinitionData::IDI_Inherited:
510
93
    return true;
511
299
  case DefinitionData::IDI_NotInherited:
512
299
    return false;
513
676
  case DefinitionData::IDI_Unknown:
514
    // If the class introduced initializers we conservatively assume that we
515
    // don't know if any of them is a designated initializer to avoid possible
516
    // misleading warnings.
517
676
    if (isIntroducingInitializers(this)) {
518
409
      data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
519
267
    } else {
520
267
      if (auto SuperD = getSuperClass()) {
521
251
        data().InheritedDesignatedInitializers =
522
251
          SuperD->declaresOrInheritsDesignatedInitializers() ?
523
135
            DefinitionData::IDI_Inherited :
524
116
            DefinitionData::IDI_NotInherited;
525
16
      } else {
526
16
        data().InheritedDesignatedInitializers =
527
16
          DefinitionData::IDI_NotInherited;
528
16
      }
529
267
    }
530
676
    assert(data().InheritedDesignatedInitializers
531
676
             != DefinitionData::IDI_Unknown);
532
676
    return data().InheritedDesignatedInitializers ==
533
676
        DefinitionData::IDI_Inherited;
534
0
  }
535
0
536
0
  llvm_unreachable("unexpected InheritedDesignatedInitializers value");
537
0
}
538
539
void ObjCInterfaceDecl::getDesignatedInitializers(
540
11
    llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
541
  // Check for a complete definition and recover if not so.
542
11
  if (!isThisDeclarationADefinition())
543
0
    return;
544
11
  if (data().ExternallyCompleted)
545
0
    LoadExternalDefinition();
546
11
547
11
  const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
548
11
  if (!IFace)
549
3
    return;
550
8
551
8
  for (const auto *MD : IFace->instance_methods())
552
12
    if (MD->isThisDeclarationADesignatedInitializer())
553
8
      Methods.push_back(MD);
554
3
  for (const auto *Ext : IFace->visible_extensions()) {
555
3
    for (const auto *MD : Ext->instance_methods())
556
3
      if (MD->isThisDeclarationADesignatedInitializer())
557
3
        Methods.push_back(MD);
558
3
  }
559
8
}
560
561
bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
562
1.03k
                                      const ObjCMethodDecl **InitMethod) const {
563
1.03k
  bool HasCompleteDef = isThisDeclarationADefinition();
564
  // During deserialization the data record for the ObjCInterfaceDecl could
565
  // be made invariant by reusing the canonical decl. Take this into account
566
  // when checking for the complete definition.
567
1.03k
  if (!HasCompleteDef && 
getCanonicalDecl()->hasDefinition()2
&&
568
1
      getCanonicalDecl()->getDefinition() == getDefinition())
569
1
    HasCompleteDef = true;
570
1.03k
571
  // Check for a complete definition and recover if not so.
572
1.03k
  if (!HasCompleteDef)
573
1
    return false;
574
1.03k
575
1.03k
  if (data().ExternallyCompleted)
576
0
    LoadExternalDefinition();
577
1.03k
578
1.03k
  const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
579
1.03k
  if (!IFace)
580
718
    return false;
581
315
582
315
  if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
583
295
    if (MD->isThisDeclarationADesignatedInitializer()) {
584
286
      if (InitMethod)
585
18
        *InitMethod = MD;
586
286
      return true;
587
286
    }
588
29
  }
589
29
  for (const auto *Ext : IFace->visible_extensions()) {
590
16
    if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
591
11
      if (MD->isThisDeclarationADesignatedInitializer()) {
592
11
        if (InitMethod)
593
4
          *InitMethod = MD;
594
11
        return true;
595
11
      }
596
11
    }
597
16
  }
598
18
  return false;
599
29
}
600
601
100k
void ObjCInterfaceDecl::allocateDefinitionData() {
602
100k
  assert(!hasDefinition() && "ObjC class already has a definition");
603
100k
  Data.setPointer(new (getASTContext()) DefinitionData());
604
100k
  Data.getPointer()->Definition = this;
605
100k
606
  // Make the type point at the definition, now that we have one.
607
100k
  if (TypeForDecl)
608
93.1k
    cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
609
100k
}
610
611
93.1k
void ObjCInterfaceDecl::startDefinition() {
612
93.1k
  allocateDefinitionData();
613
93.1k
614
  // Update all of the declarations with a pointer to the definition.
615
137k
  for (auto *RD : redecls()) {
616
137k
    if (RD != this)
617
44.6k
      RD->Data = Data;
618
137k
  }
619
93.1k
}
620
621
ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
622
199k
                                              ObjCInterfaceDecl *&clsDeclared) {
623
  // FIXME: Should make sure no callers ever do this.
624
199k
  if (!hasDefinition())
625
0
    return nullptr;
626
199k
627
199k
  if (data().ExternallyCompleted)
628
0
    LoadExternalDefinition();
629
199k
630
199k
  ObjCInterfaceDecl* ClassDecl = this;
631
482k
  while (ClassDecl != nullptr) {
632
288k
    if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
633
4.90k
      clsDeclared = ClassDecl;
634
4.90k
      return I;
635
4.90k
    }
636
283k
637
283k
    for (const auto *Ext : ClassDecl->visible_extensions()) {
638
2.84k
      if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
639
136
        clsDeclared = ClassDecl;
640
136
        return I;
641
136
      }
642
2.84k
    }
643
283k
644
283k
    ClassDecl = ClassDecl->getSuperClass();
645
283k
  }
646
194k
  return nullptr;
647
199k
}
648
649
/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
650
/// class whose name is passed as argument. If it is not one of the super classes
651
/// the it returns NULL.
652
ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
653
27
                                        const IdentifierInfo*ICName) {
654
  // FIXME: Should make sure no callers ever do this.
655
27
  if (!hasDefinition())
656
0
    return nullptr;
657
27
658
27
  if (data().ExternallyCompleted)
659
0
    LoadExternalDefinition();
660
27
661
27
  ObjCInterfaceDecl* ClassDecl = this;
662
32
  while (ClassDecl != nullptr) {
663
30
    if (ClassDecl->getIdentifier() == ICName)
664
25
      return ClassDecl;
665
5
    ClassDecl = ClassDecl->getSuperClass();
666
5
  }
667
2
  return nullptr;
668
27
}
669
670
ObjCProtocolDecl *
671
56
ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
672
56
  for (auto *P : all_referenced_protocols())
673
6
    if (P->lookupProtocolNamed(Name))
674
6
      return P;
675
50
  ObjCInterfaceDecl *SuperClass = getSuperClass();
676
44
  return SuperClass ? 
SuperClass->lookupNestedProtocol(Name)6
: nullptr;
677
56
}
678
679
/// lookupMethod - This method returns an instance/class method by looking in
680
/// the class, its categories, and its super classes (using a linear search).
681
/// When argument category "C" is specified, any implicit method found
682
/// in this category is ignored.
683
ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
684
                                                bool isInstance,
685
                                                bool shallowCategoryLookup,
686
                                                bool followSuper,
687
                                                const ObjCCategoryDecl *C) const
688
152k
{
689
  // FIXME: Should make sure no callers ever do this.
690
152k
  if (!hasDefinition())
691
79
    return nullptr;
692
152k
693
152k
  const ObjCInterfaceDecl* ClassDecl = this;
694
152k
  ObjCMethodDecl *MethodDecl = nullptr;
695
152k
696
152k
  if (data().ExternallyCompleted)
697
0
    LoadExternalDefinition();
698
152k
699
230k
  while (ClassDecl) {
700
    // 1. Look through primary class.
701
192k
    if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
702
31.8k
      return MethodDecl;
703
160k
704
    // 2. Didn't find one yet - now look through categories.
705
160k
    for (const auto *Cat : ClassDecl->visible_categories())
706
770k
      if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
707
6.73k
        if (C != Cat || 
!MethodDecl->isImplicit()7
)
708
6.72k
          return MethodDecl;
709
160k
710
    // 3. Didn't find one yet - look through primary class's protocols.
711
153k
    for (const auto *I : ClassDecl->protocols())
712
178k
      if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
713
3.59k
        return MethodDecl;
714
153k
715
    // 4. Didn't find one yet - now look through categories' protocols
716
150k
    if (!shallowCategoryLookup)
717
525k
      
for (const auto *Cat : ClassDecl->visible_categories())78.7k
{
718
        // Didn't find one yet - look through protocols.
719
525k
        const ObjCList<ObjCProtocolDecl> &Protocols =
720
525k
          Cat->getReferencedProtocols();
721
525k
        for (auto *Protocol : Protocols)
722
6.14k
          if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
723
433
            if (C != Cat || 
!MethodDecl->isImplicit()5
)
724
429
              return MethodDecl;
725
525k
      }
726
150k
727
150k
728
149k
    if (!followSuper)
729
71.4k
      return nullptr;
730
78.3k
731
    // 5. Get to the super class (if any).
732
78.3k
    ClassDecl = ClassDecl->getSuperClass();
733
78.3k
  }
734
38.2k
  return nullptr;
735
152k
}
736
737
// Will search "local" class/category implementations for a method decl.
738
// If failed, then we search in class's root for an instance method.
739
// Returns 0 if no method is found.
740
ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
741
                                   const Selector &Sel,
742
53.7k
                                   bool Instance) const {
743
  // FIXME: Should make sure no callers ever do this.
744
53.7k
  if (!hasDefinition())
745
104
    return nullptr;
746
53.6k
747
53.6k
  if (data().ExternallyCompleted)
748
0
    LoadExternalDefinition();
749
53.6k
750
53.6k
  ObjCMethodDecl *Method = nullptr;
751
53.6k
  if (ObjCImplementationDecl *ImpDecl = getImplementation())
752
20.9k
    Method = Instance ? 
ImpDecl->getInstanceMethod(Sel)11.8k
753
9.09k
                      : ImpDecl->getClassMethod(Sel);
754
53.6k
755
  // Look through local category implementations associated with the class.
756
53.6k
  if (!Method)
757
52.3k
    Method = getCategoryMethod(Sel, Instance);
758
53.6k
759
  // Before we give up, check if the selector is an instance method.
760
  // But only in the root. This matches gcc's behavior and what the
761
  // runtime expects.
762
53.6k
  if (!Instance && 
!Method18.3k
&&
!getSuperClass()18.1k
) {
763
8.07k
    Method = lookupInstanceMethod(Sel);
764
    // Look through local category implementations associated
765
    // with the root class.
766
8.07k
    if (!Method)
767
7.88k
      Method = lookupPrivateMethod(Sel, true);
768
8.07k
  }
769
53.6k
770
53.6k
  if (!Method && 
getSuperClass()52.1k
)
771
23.7k
    return getSuperClass()->lookupPrivateMethod(Sel, Instance);
772
29.9k
  return Method;
773
29.9k
}
774
775
//===----------------------------------------------------------------------===//
776
// ObjCMethodDecl
777
//===----------------------------------------------------------------------===//
778
779
ObjCMethodDecl::ObjCMethodDecl(
780
    SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
781
    QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
782
    bool isInstance, bool isVariadic, bool isPropertyAccessor,
783
    bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
784
    ImplementationControl impControl, bool HasRelatedResultType)
785
    : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
786
      DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
787
1.53M
      DeclEndLoc(endLoc) {
788
1.53M
789
  // Initialized the bits stored in DeclContext.
790
1.53M
  ObjCMethodDeclBits.Family =
791
1.53M
      static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
792
1.53M
  setInstanceMethod(isInstance);
793
1.53M
  setVariadic(isVariadic);
794
1.53M
  setPropertyAccessor(isPropertyAccessor);
795
1.53M
  setSynthesizedAccessorStub(isSynthesizedAccessorStub);
796
1.53M
  setDefined(isDefined);
797
1.53M
  setIsRedeclaration(false);
798
1.53M
  setHasRedeclaration(false);
799
1.53M
  setDeclImplementation(impControl);
800
1.53M
  setObjCDeclQualifier(OBJC_TQ_None);
801
1.53M
  setRelatedResultType(HasRelatedResultType);
802
1.53M
  setSelLocsKind(SelLoc_StandardNoSpace);
803
1.53M
  setOverriding(false);
804
1.53M
  setHasSkippedBody(false);
805
1.53M
806
1.53M
  setImplicit(isImplicitlyDeclared);
807
1.53M
}
808
809
ObjCMethodDecl *ObjCMethodDecl::Create(
810
    ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
811
    Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
812
    DeclContext *contextDecl, bool isInstance, bool isVariadic,
813
    bool isPropertyAccessor, bool isSynthesizedAccessorStub,
814
    bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
815
1.50M
    bool HasRelatedResultType) {
816
1.50M
  return new (C, contextDecl) ObjCMethodDecl(
817
1.50M
      beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
818
1.50M
      isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
819
1.50M
      isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
820
1.50M
}
821
822
30.8k
ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
823
30.8k
  return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
824
30.8k
                                    Selector(), QualType(), nullptr, nullptr);
825
30.8k
}
826
827
3.13M
bool ObjCMethodDecl::isDirectMethod() const {
828
3.13M
  return hasAttr<ObjCDirectAttr>();
829
3.13M
}
830
831
321
bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
832
321
  return getMethodFamily() == OMF_init &&
833
321
      hasAttr<ObjCDesignatedInitializerAttr>();
834
321
}
835
836
230
bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
837
230
  if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
838
0
    return PD->getIdentifier() == Ctx.getNSObjectName();
839
230
  if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
840
230
    return ID->getIdentifier() == Ctx.getNSObjectName();
841
0
  return false;
842
0
}
843
844
bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
845
904
    const ObjCMethodDecl **InitMethod) const {
846
904
  if (getMethodFamily() != OMF_init)
847
0
    return false;
848
904
  const DeclContext *DC = getDeclContext();
849
904
  if (isa<ObjCProtocolDecl>(DC))
850
0
    return false;
851
904
  if (const ObjCInterfaceDecl *ID = getClassInterface())
852
904
    return ID->isDesignatedInitializer(getSelector(), InitMethod);
853
0
  return false;
854
0
}
855
856
339k
Stmt *ObjCMethodDecl::getBody() const {
857
339k
  return Body.get(getASTContext().getExternalSource());
858
339k
}
859
860
13
void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
861
13
  assert(PrevMethod);
862
13
  getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
863
13
  setIsRedeclaration(true);
864
13
  PrevMethod->setHasRedeclaration(true);
865
13
}
866
867
void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
868
                                         ArrayRef<ParmVarDecl*> Params,
869
1.07M
                                         ArrayRef<SourceLocation> SelLocs) {
870
1.07M
  ParamsAndSelLocs = nullptr;
871
1.07M
  NumParams = Params.size();
872
1.07M
  if (Params.empty() && 
SelLocs.empty()176k
)
873
114k
    return;
874
957k
875
957k
  static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
876
957k
                "Alignment not sufficient for SourceLocation");
877
957k
878
957k
  unsigned Size = sizeof(ParmVarDecl *) * NumParams +
879
957k
                  sizeof(SourceLocation) * SelLocs.size();
880
957k
  ParamsAndSelLocs = C.Allocate(Size);
881
957k
  std::copy(Params.begin(), Params.end(), getParams());
882
957k
  std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
883
957k
}
884
885
void ObjCMethodDecl::getSelectorLocs(
886
13.8k
                               SmallVectorImpl<SourceLocation> &SelLocs) const {
887
18.3k
  for (unsigned i = 0, e = getNumSelectorLocs(); i != e; 
++i4.49k
)
888
4.49k
    SelLocs.push_back(getSelectorLoc(i));
889
13.8k
}
890
891
void ObjCMethodDecl::setMethodParams(ASTContext &C,
892
                                     ArrayRef<ParmVarDecl*> Params,
893
1.04M
                                     ArrayRef<SourceLocation> SelLocs) {
894
1.04M
  assert((!SelLocs.empty() || isImplicit()) &&
895
1.04M
         "No selector locs for non-implicit method");
896
1.04M
  if (isImplicit())
897
185k
    return setParamsAndSelLocs(C, Params, llvm::None);
898
857k
899
857k
  setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
900
857k
                                        DeclEndLoc));
901
857k
  if (getSelLocsKind() != SelLoc_NonStandard)
902
649k
    return setParamsAndSelLocs(C, Params, llvm::None);
903
207k
904
207k
  setParamsAndSelLocs(C, Params, SelLocs);
905
207k
}
906
907
/// A definition will return its interface declaration.
908
/// An interface declaration will return its definition.
909
/// Otherwise it will return itself.
910
57.0k
ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
911
57.0k
  ASTContext &Ctx = getASTContext();
912
57.0k
  ObjCMethodDecl *Redecl = nullptr;
913
57.0k
  if (hasRedeclaration())
914
20
    Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
915
57.0k
  if (Redecl)
916
20
    return Redecl;
917
57.0k
918
57.0k
  auto *CtxD = cast<Decl>(getDeclContext());
919
57.0k
920
57.0k
  if (!CtxD->isInvalidDecl()) {
921
57.0k
    if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
922
38.7k
      if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
923
559
        if (!ImplD->isInvalidDecl())
924
559
          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
925
38.7k
926
18.2k
    } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
927
10.6k
      if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
928
125
        if (!ImplD->isInvalidDecl())
929
125
          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
930
10.6k
931
7.64k
    } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
932
669
      if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
933
669
        if (!IFD->isInvalidDecl())
934
669
          Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
935
669
936
6.97k
    } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
937
126
      if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
938
126
        if (!CatD->isInvalidDecl())
939
126
          Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
940
126
    }
941
57.0k
  }
942
57.0k
943
  // Ensure that the discovered method redeclaration has a valid declaration
944
  // context. Used to prevent infinite loops when iterating redeclarations in
945
  // a partially invalid AST.
946
57.0k
  if (Redecl && 
cast<Decl>(Redecl->getDeclContext())->isInvalidDecl()1.26k
)
947
0
    Redecl = nullptr;
948
57.0k
949
57.0k
  if (!Redecl && 
isRedeclaration()55.7k
) {
950
    // This is the last redeclaration, go back to the first method.
951
9
    return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
952
9
                                                    isInstanceMethod());
953
9
  }
954
57.0k
955
57.0k
  return Redecl ? 
Redecl1.26k
:
this55.7k
;
956
57.0k
}
957
958
228k
ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
959
228k
  auto *CtxD = cast<Decl>(getDeclContext());
960
228k
  const auto &Sel = getSelector();
961
228k
962
228k
  if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
963
10.5k
    if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) {
964
      // When the container is the ObjCImplementationDecl (the primary
965
      // @implementation), then the canonical Decl is either in
966
      // the class Interface, or in any of its extension.
967
      //
968
      // So when we don't find it in the ObjCInterfaceDecl,
969
      // sift through extensions too.
970
10.5k
      if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
971
4.79k
        return MD;
972
5.77k
      for (auto *Ext : IFD->known_extensions())
973
432
        if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
974
105
          return MD;
975
5.77k
    }
976
218k
  } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
977
578
    if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
978
566
      if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod()))
979
348
        return MD;
980
223k
  }
981
223k
982
223k
  if (isRedeclaration()) {
983
    // It is possible that we have not done deserializing the ObjCMethod yet.
984
14
    ObjCMethodDecl *MD =
985
14
        cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod());
986
13
    return MD ? MD : 
this1
;
987
14
  }
988
223k
989
223k
  return this;
990
223k
}
991
992
151k
SourceLocation ObjCMethodDecl::getEndLoc() const {
993
151k
  if (Stmt *Body = getBody())
994
56.5k
    return Body->getEndLoc();
995
95.1k
  return DeclEndLoc;
996
95.1k
}
997
998
335k
ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
999
335k
  auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
1000
335k
  if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
1001
88.4k
    return family;
1002
247k
1003
  // Check for an explicit attribute.
1004
247k
  if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
1005
    // The unfortunate necessity of mapping between enums here is due
1006
    // to the attributes framework.
1007
26
    switch (attr->getFamily()) {
1008
0
    case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
1009
0
    case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
1010
0
    case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
1011
22
    case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
1012
0
    case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
1013
4
    case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
1014
26
    }
1015
26
    ObjCMethodDeclBits.Family = family;
1016
26
    return family;
1017
26
  }
1018
247k
1019
247k
  family = getSelector().getMethodFamily();
1020
247k
  switch (family) {
1021
171k
  case OMF_None: break;
1022
0
1023
  // init only has a conventional meaning for an instance method, and
1024
  // it has to return an object.
1025
67.3k
  case OMF_init:
1026
67.3k
    if (!isInstanceMethod() || 
!getReturnType()->isObjCObjectPointerType()67.2k
)
1027
82
      family = OMF_None;
1028
67.3k
    break;
1029
0
1030
  // alloc/copy/new have a conventional meaning for both class and
1031
  // instance methods, but they require an object return.
1032
5.02k
  case OMF_alloc:
1033
5.02k
  case OMF_copy:
1034
5.02k
  case OMF_mutableCopy:
1035
5.02k
  case OMF_new:
1036
5.02k
    if (!getReturnType()->isObjCObjectPointerType())
1037
71
      family = OMF_None;
1038
5.02k
    break;
1039
5.02k
1040
  // These selectors have a conventional meaning only for instance methods.
1041
1.97k
  case OMF_dealloc:
1042
1.97k
  case OMF_finalize:
1043
1.97k
  case OMF_retain:
1044
1.97k
  case OMF_release:
1045
1.97k
  case OMF_autorelease:
1046
1.97k
  case OMF_retainCount:
1047
1.97k
  case OMF_self:
1048
1.97k
    if (!isInstanceMethod())
1049
13
      family = OMF_None;
1050
1.97k
    break;
1051
1.97k
1052
19
  case OMF_initialize:
1053
19
    if (isInstanceMethod() || 
!getReturnType()->isVoidType()17
)
1054
3
      family = OMF_None;
1055
19
    break;
1056
1.97k
1057
908
  case OMF_performSelector:
1058
908
    if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
1059
14
      family = OMF_None;
1060
894
    else {
1061
894
      unsigned noParams = param_size();
1062
894
      if (noParams < 1 || noParams > 3)
1063
0
        family = OMF_None;
1064
894
      else {
1065
894
        ObjCMethodDecl::param_type_iterator it = param_type_begin();
1066
894
        QualType ArgT = (*it);
1067
894
        if (!ArgT->isObjCSelType()) {
1068
0
          family = OMF_None;
1069
0
          break;
1070
0
        }
1071
1.78k
        
while (894
--noParams) {
1072
888
          it++;
1073
888
          ArgT = (*it);
1074
888
          if (!ArgT->isObjCIdType()) {
1075
0
            family = OMF_None;
1076
0
            break;
1077
0
          }
1078
888
        }
1079
894
      }
1080
894
    }
1081
908
    break;
1082
247k
1083
247k
  }
1084
247k
1085
  // Cache the result.
1086
247k
  ObjCMethodDeclBits.Family = family;
1087
247k
  return family;
1088
247k
}
1089
1090
QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1091
                                     const ObjCInterfaceDecl *OID,
1092
                                     bool &selfIsPseudoStrong,
1093
1.50M
                                     bool &selfIsConsumed) const {
1094
1.50M
  QualType selfTy;
1095
1.50M
  selfIsPseudoStrong = false;
1096
1.50M
  selfIsConsumed = false;
1097
1.50M
  if (isInstanceMethod()) {
1098
    // There may be no interface context due to error in declaration
1099
    // of the interface (which has been reported). Recover gracefully.
1100
1.24M
    if (OID) {
1101
1.16M
      selfTy = Context.getObjCInterfaceType(OID);
1102
1.16M
      selfTy = Context.getObjCObjectPointerType(selfTy);
1103
78.0k
    } else {
1104
78.0k
      selfTy = Context.getObjCIdType();
1105
78.0k
    }
1106
1.24M
  } else // we have a factory method.
1107
262k
    selfTy = Context.getObjCClassType();
1108
1.50M
1109
1.50M
  if (Context.getLangOpts().ObjCAutoRefCount) {
1110
26.8k
    if (isInstanceMethod()) {
1111
23.8k
      selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
1112
23.8k
1113
      // 'self' is always __strong.  It's actually pseudo-strong except
1114
      // in init methods (or methods labeled ns_consumes_self), though.
1115
23.8k
      Qualifiers qs;
1116
23.8k
      qs.setObjCLifetime(Qualifiers::OCL_Strong);
1117
23.8k
      selfTy = Context.getQualifiedType(selfTy, qs);
1118
23.8k
1119
      // In addition, 'self' is const unless this is an init method.
1120
23.8k
      if (getMethodFamily() != OMF_init && 
!selfIsConsumed22.2k
) {
1121
22.1k
        selfTy = selfTy.withConst();
1122
22.1k
        selfIsPseudoStrong = true;
1123
22.1k
      }
1124
23.8k
    }
1125
2.99k
    else {
1126
2.99k
      assert(isClassMethod());
1127
      // 'self' is always const in class methods.
1128
2.99k
      selfTy = selfTy.withConst();
1129
2.99k
      selfIsPseudoStrong = true;
1130
2.99k
    }
1131
26.8k
  }
1132
1.50M
  return selfTy;
1133
1.50M
}
1134
1135
void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1136
1.48M
                                          const ObjCInterfaceDecl *OID) {
1137
1.48M
  bool selfIsPseudoStrong, selfIsConsumed;
1138
1.48M
  QualType selfTy =
1139
1.48M
    getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1140
1.48M
  auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1141
1.48M
                                         &Context.Idents.get("self"), selfTy,
1142
1.48M
                                         ImplicitParamDecl::ObjCSelf);
1143
1.48M
  setSelfDecl(Self);
1144
1.48M
1145
1.48M
  if (selfIsConsumed)
1146
1.62k
    Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1147
1.48M
1148
1.48M
  if (selfIsPseudoStrong)
1149
25.1k
    Self->setARCPseudoStrong(true);
1150
1.48M
1151
1.48M
  setCmdDecl(ImplicitParamDecl::Create(
1152
1.48M
      Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1153
1.48M
      Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
1154
1.48M
}
1155
1156
3.83M
ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1157
3.83M
  if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1158
3.00M
    return ID;
1159
828k
  if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1160
674k
    return CD->getClassInterface();
1161
153k
  if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1162
70.9k
    return IMD->getClassInterface();
1163
82.6k
  if (isa<ObjCProtocolDecl>(getDeclContext()))
1164
82.6k
    return nullptr;
1165
0
  llvm_unreachable("unknown method context");
1166
0
}
1167
1168
11.4k
SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1169
11.4k
  const auto *TSI = getReturnTypeSourceInfo();
1170
11.4k
  if (TSI)
1171
10.8k
    return TSI->getTypeLoc().getSourceRange();
1172
571
  return SourceRange();
1173
571
}
1174
1175
85
QualType ObjCMethodDecl::getSendResultType() const {
1176
85
  ASTContext &Ctx = getASTContext();
1177
85
  return getReturnType().getNonLValueExprType(Ctx)
1178
85
           .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1179
85
}
1180
1181
25.1k
QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1182
  // FIXME: Handle related result types here.
1183
25.1k
1184
25.1k
  return getReturnType().getNonLValueExprType(getASTContext())
1185
25.1k
           .substObjCMemberType(receiverType, getDeclContext(),
1186
25.1k
                                ObjCSubstitutionContext::Result);
1187
25.1k
}
1188
1189
static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1190
                                            const ObjCMethodDecl *Method,
1191
                               SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1192
1.66k
                                            bool MovedToSuper) {
1193
1.66k
  if (!Container)
1194
0
    return;
1195
1.66k
1196
  // In categories look for overridden methods from protocols. A method from
1197
  // category is not "overridden" since it is considered as the "same" method
1198
  // (same USR) as the one from the interface.
1199
1.66k
  if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1200
    // Check whether we have a matching method at this category but only if we
1201
    // are at the super class level.
1202
535
    if (MovedToSuper)
1203
104
      if (ObjCMethodDecl *
1204
104
            Overridden = Container->getMethod(Method->getSelector(),
1205
12
                                              Method->isInstanceMethod(),
1206
12
                                              /*AllowHidden=*/true))
1207
12
        if (Method != Overridden) {
1208
          // We found an override at this category; there is no need to look
1209
          // into its protocols.
1210
12
          Methods.push_back(Overridden);
1211
12
          return;
1212
12
        }
1213
523
1214
523
    for (const auto *P : Category->protocols())
1215
30
      CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1216
523
    return;
1217
523
  }
1218
1.13k
1219
  // Check whether we have a matching method at this level.
1220
1.13k
  if (const ObjCMethodDecl *
1221
1.13k
        Overridden = Container->getMethod(Method->getSelector(),
1222
908
                                          Method->isInstanceMethod(),
1223
908
                                          /*AllowHidden=*/true))
1224
908
    if (Method != Overridden) {
1225
      // We found an override at this level; there is no need to look
1226
      // into other protocols or categories.
1227
474
      Methods.push_back(Overridden);
1228
474
      return;
1229
474
    }
1230
656
1231
656
  if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1232
128
    for (const auto *P : Protocol->protocols())
1233
77
      CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1234
128
  }
1235
656
1236
656
  if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1237
528
    for (const auto *P : Interface->protocols())
1238
115
      CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1239
528
1240
528
    for (const auto *Cat : Interface->known_categories())
1241
535
      CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
1242
528
1243
528
    if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1244
434
      return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1245
434
                                             /*MovedToSuper=*/true);
1246
528
  }
1247
656
}
1248
1249
static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1250
                                            const ObjCMethodDecl *Method,
1251
474
                             SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1252
474
  CollectOverriddenMethodsRecurse(Container, Method, Methods,
1253
474
                                  /*MovedToSuper=*/false);
1254
474
}
1255
1256
static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1257
474
                          SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1258
474
  assert(Method->isOverriding());
1259
474
1260
474
  if (const auto *ProtD =
1261
20
          dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1262
20
    CollectOverriddenMethods(ProtD, Method, overridden);
1263
20
1264
454
  } else if (const auto *IMD =
1265
20
                 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1266
20
    const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1267
20
    if (!ID)
1268
0
      return;
1269
    // Start searching for overridden methods using the method from the
1270
    // interface as starting point.
1271
20
    if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1272
10
                                                    Method->isInstanceMethod(),
1273
10
                                                    /*AllowHidden=*/true))
1274
10
      Method = IFaceMeth;
1275
20
    CollectOverriddenMethods(ID, Method, overridden);
1276
20
1277
434
  } else if (const auto *CatD =
1278
32
                 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1279
32
    const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1280
32
    if (!ID)
1281
0
      return;
1282
    // Start searching for overridden methods using the method from the
1283
    // interface as starting point.
1284
32
    if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1285
2
                                                     Method->isInstanceMethod(),
1286
2
                                                     /*AllowHidden=*/true))
1287
2
      Method = IFaceMeth;
1288
32
    CollectOverriddenMethods(ID, Method, overridden);
1289
32
1290
402
  } else {
1291
402
    CollectOverriddenMethods(
1292
402
                  dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1293
402
                  Method, overridden);
1294
402
  }
1295
474
}
1296
1297
void ObjCMethodDecl::getOverriddenMethods(
1298
20.0k
                    SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1299
20.0k
  const ObjCMethodDecl *Method = this;
1300
20.0k
1301
20.0k
  if (Method->isRedeclaration()) {
1302
2
    Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
1303
2
                   getMethod(Method->getSelector(), Method->isInstanceMethod());
1304
2
  }
1305
20.0k
1306
20.0k
  if (Method->isOverriding()) {
1307
474
    collectOverriddenMethodsSlow(Method, Overridden);
1308
474
    assert(!Overridden.empty() &&
1309
474
           "ObjCMethodDecl's overriding bit is not as expected");
1310
474
  }
1311
20.0k
}
1312
1313
const ObjCPropertyDecl *
1314
25.2k
ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1315
25.2k
  Selector Sel = getSelector();
1316
25.2k
  unsigned NumArgs = Sel.getNumArgs();
1317
25.2k
  if (NumArgs > 1)
1318
3.19k
    return nullptr;
1319
22.0k
1320
22.0k
  if (isPropertyAccessor()) {
1321
2.55k
    const auto *Container = cast<ObjCContainerDecl>(getParent());
1322
    // For accessor stubs, go back to the interface.
1323
2.55k
    if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container))
1324
0
      if (isSynthesizedAccessorStub())
1325
0
        Container = ImplDecl->getClassInterface();
1326
2.55k
1327
2.55k
    bool IsGetter = (NumArgs == 0);
1328
2.55k
    bool IsInstance = isInstanceMethod();
1329
2.55k
1330
    /// Local function that attempts to find a matching property within the
1331
    /// given Objective-C container.
1332
2.55k
    auto findMatchingProperty =
1333
2.64k
      [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
1334
2.64k
      if (IsInstance) {
1335
6.31k
        for (const auto *I : Container->instance_properties()) {
1336
5.55k
          Selector NextSel = IsGetter ? I->getGetterName()
1337
759
                                      : I->getSetterName();
1338
6.31k
          if (NextSel == Sel)
1339
2.00k
            return I;
1340
6.31k
        }
1341
548
      } else {
1342
885
        for (const auto *I : Container->class_properties()) {
1343
780
          Selector NextSel = IsGetter ? I->getGetterName()
1344
105
                                      : I->getSetterName();
1345
885
          if (NextSel == Sel)
1346
548
            return I;
1347
885
        }
1348
548
      }
1349
2.64k
1350
95
      return nullptr;
1351
2.64k
    };
1352
2.55k
1353
    // Look in the container we were given.
1354
2.55k
    if (const auto *Found = findMatchingProperty(Container))
1355
2.50k
      return Found;
1356
48
1357
    // If we're in a category or extension, look in the main class.
1358
48
    const ObjCInterfaceDecl *ClassDecl = nullptr;
1359
48
    if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1360
3
      ClassDecl = Category->getClassInterface();
1361
3
      if (const auto *Found = findMatchingProperty(ClassDecl))
1362
0
        return Found;
1363
45
    } else {
1364
      // Determine whether the container is a class.
1365
45
      ClassDecl = cast<ObjCInterfaceDecl>(Container);
1366
45
    }
1367
48
    assert(ClassDecl && "Failed to find main class");
1368
48
1369
    // If we have a class, check its visible extensions.
1370
92
    for (const auto *Ext : ClassDecl->visible_extensions()) {
1371
92
      if (Ext == Container)
1372
0
        continue;
1373
92
      if (const auto *Found = findMatchingProperty(Ext))
1374
48
        return Found;
1375
92
    }
1376
48
1377
0
    assert(isSynthesizedAccessorStub() && "expected an accessor stub");
1378
0
1379
0
    for (const auto *Cat : ClassDecl->known_categories()) {
1380
0
      if (Cat == Container)
1381
0
        continue;
1382
0
      if (const auto *Found = findMatchingProperty(Cat))
1383
0
        return Found;
1384
0
    }
1385
0
1386
0
    llvm_unreachable("Marked as a property accessor but no property found!");
1387
19.4k
  }
1388
19.4k
1389
19.4k
  if (!CheckOverrides)
1390
358
    return nullptr;
1391
19.0k
1392
19.0k
  using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1393
19.0k
1394
19.0k
  OverridesTy Overrides;
1395
19.0k
  getOverriddenMethods(Overrides);
1396
19.0k
  for (const auto *Override : Overrides)
1397
392
    if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
1398
34
      return Prop;
1399
19.0k
1400
19.0k
  return nullptr;
1401
19.0k
}
1402
1403
//===----------------------------------------------------------------------===//
1404
// ObjCTypeParamDecl
1405
//===----------------------------------------------------------------------===//
1406
1407
0
void ObjCTypeParamDecl::anchor() {}
1408
1409
ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1410
                                             ObjCTypeParamVariance variance,
1411
                                             SourceLocation varianceLoc,
1412
                                             unsigned index,
1413
                                             SourceLocation nameLoc,
1414
                                             IdentifierInfo *name,
1415
                                             SourceLocation colonLoc,
1416
62.5k
                                             TypeSourceInfo *boundInfo) {
1417
62.5k
  auto *TPDecl =
1418
62.5k
    new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1419
62.5k
                                    nameLoc, name, colonLoc, boundInfo);
1420
62.5k
  QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1421
62.5k
  TPDecl->setTypeForDecl(TPType.getTypePtr());
1422
62.5k
  return TPDecl;
1423
62.5k
}
1424
1425
ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1426
28.0k
                                                         unsigned ID) {
1427
28.0k
  return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1428
28.0k
                                         ObjCTypeParamVariance::Invariant,
1429
28.0k
                                         SourceLocation(), 0, SourceLocation(),
1430
28.0k
                                         nullptr, SourceLocation(), nullptr);
1431
28.0k
}
1432
1433
84
SourceRange ObjCTypeParamDecl::getSourceRange() const {
1434
84
  SourceLocation startLoc = VarianceLoc;
1435
84
  if (startLoc.isInvalid())
1436
80
    startLoc = getLocation();
1437
84
1438
84
  if (hasExplicitBound()) {
1439
70
    return SourceRange(startLoc,
1440
70
                       getTypeSourceInfo()->getTypeLoc().getEndLoc());
1441
70
  }
1442
14
1443
14
  return SourceRange(startLoc);
1444
14
}
1445
1446
//===----------------------------------------------------------------------===//
1447
// ObjCTypeParamList
1448
//===----------------------------------------------------------------------===//
1449
ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1450
                                     ArrayRef<ObjCTypeParamDecl *> typeParams,
1451
                                     SourceLocation rAngleLoc)
1452
68.6k
    : NumParams(typeParams.size()) {
1453
68.6k
  Brackets.Begin = lAngleLoc.getRawEncoding();
1454
68.6k
  Brackets.End = rAngleLoc.getRawEncoding();
1455
68.6k
  std::copy(typeParams.begin(), typeParams.end(), begin());
1456
68.6k
}
1457
1458
ObjCTypeParamList *ObjCTypeParamList::create(
1459
                     ASTContext &ctx,
1460
                     SourceLocation lAngleLoc,
1461
                     ArrayRef<ObjCTypeParamDecl *> typeParams,
1462
68.6k
                     SourceLocation rAngleLoc) {
1463
68.6k
  void *mem =
1464
68.6k
      ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1465
68.6k
                   alignof(ObjCTypeParamList));
1466
68.6k
  return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1467
68.6k
}
1468
1469
void ObjCTypeParamList::gatherDefaultTypeArgs(
1470
0
       SmallVectorImpl<QualType> &typeArgs) const {
1471
0
  typeArgs.reserve(size());
1472
0
  for (auto typeParam : *this)
1473
0
    typeArgs.push_back(typeParam->getUnderlyingType());
1474
0
}
1475
1476
//===----------------------------------------------------------------------===//
1477
// ObjCInterfaceDecl
1478
//===----------------------------------------------------------------------===//
1479
1480
ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1481
                                             DeclContext *DC,
1482
                                             SourceLocation atLoc,
1483
                                             IdentifierInfo *Id,
1484
                                             ObjCTypeParamList *typeParamList,
1485
                                             ObjCInterfaceDecl *PrevDecl,
1486
                                             SourceLocation ClassLoc,
1487
291k
                                             bool isInternal){
1488
291k
  auto *Result = new (C, DC)
1489
291k
      ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1490
291k
                        isInternal);
1491
291k
  Result->Data.setInt(!C.getLangOpts().Modules);
1492
291k
  C.getObjCInterfaceType(Result, PrevDecl);
1493
291k
  return Result;
1494
291k
}
1495
1496
ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1497
63.0k
                                                         unsigned ID) {
1498
63.0k
  auto *Result = new (C, ID)
1499
63.0k
      ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1500
63.0k
                        SourceLocation(), nullptr, false);
1501
63.0k
  Result->Data.setInt(!C.getLangOpts().Modules);
1502
63.0k
  return Result;
1503
63.0k
}
1504
1505
ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1506
                                     SourceLocation AtLoc, IdentifierInfo *Id,
1507
                                     ObjCTypeParamList *typeParamList,
1508
                                     SourceLocation CLoc,
1509
                                     ObjCInterfaceDecl *PrevDecl,
1510
                                     bool IsInternal)
1511
    : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1512
354k
      redeclarable_base(C) {
1513
354k
  setPreviousDecl(PrevDecl);
1514
354k
1515
  // Copy the 'data' pointer over.
1516
354k
  if (PrevDecl)
1517
165k
    Data = PrevDecl->Data;
1518
354k
1519
354k
  setImplicit(IsInternal);
1520
354k
1521
354k
  setTypeParamList(typeParamList);
1522
354k
}
1523
1524
0
void ObjCInterfaceDecl::LoadExternalDefinition() const {
1525
0
  assert(data().ExternallyCompleted && "Class is not externally completed");
1526
0
  data().ExternallyCompleted = false;
1527
0
  getASTContext().getExternalSource()->CompleteType(
1528
0
                                        const_cast<ObjCInterfaceDecl *>(this));
1529
0
}
1530
1531
0
void ObjCInterfaceDecl::setExternallyCompleted() {
1532
0
  assert(getASTContext().getExternalSource() &&
1533
0
         "Class can't be externally completed without an external source");
1534
0
  assert(hasDefinition() &&
1535
0
         "Forward declarations can't be externally completed");
1536
0
  data().ExternallyCompleted = true;
1537
0
}
1538
1539
56.5k
void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1540
  // Check for a complete definition and recover if not so.
1541
56.5k
  if (!isThisDeclarationADefinition())
1542
0
    return;
1543
56.5k
  data().HasDesignatedInitializers = true;
1544
56.5k
}
1545
1546
7.14k
bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1547
  // Check for a complete definition and recover if not so.
1548
7.14k
  if (!isThisDeclarationADefinition())
1549
3
    return false;
1550
7.13k
  if (data().ExternallyCompleted)
1551
0
    LoadExternalDefinition();
1552
7.13k
1553
7.13k
  return data().HasDesignatedInitializers;
1554
7.13k
}
1555
1556
StringRef
1557
21.6k
ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1558
21.6k
  if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1559
155
    return ObjCRTName->getMetadataName();
1560
21.5k
1561
21.5k
  return getName();
1562
21.5k
}
1563
1564
StringRef
1565
7.91k
ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1566
7.91k
  if (ObjCInterfaceDecl *ID =
1567
7.91k
      const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1568
7.91k
    return ID->getObjCRuntimeNameAsString();
1569
0
1570
0
  return getName();
1571
0
}
1572
1573
1.01M
ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1574
1.01M
  if (const ObjCInterfaceDecl *Def = getDefinition()) {
1575
1.01M
    if (data().ExternallyCompleted)
1576
0
      LoadExternalDefinition();
1577
1.01M
1578
1.01M
    return getASTContext().getObjCImplementation(
1579
1.01M
             const_cast<ObjCInterfaceDecl*>(Def));
1580
1.01M
  }
1581
42
1582
  // FIXME: Should make sure no callers ever do this.
1583
42
  return nullptr;
1584
42
}
1585
1586
4.78k
void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1587
4.78k
  getASTContext().setObjCImplementation(getDefinition(), ImplD);
1588
4.78k
}
1589
1590
namespace {
1591
1592
struct SynthesizeIvarChunk {
1593
  uint64_t Size;
1594
  ObjCIvarDecl *Ivar;
1595
1596
  SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1597
1.97k
      : Size(size), Ivar(ivar) {}
1598
};
1599
1600
bool operator<(const SynthesizeIvarChunk & LHS,
1601
1.02k
               const SynthesizeIvarChunk &RHS) {
1602
1.02k
    return LHS.Size < RHS.Size;
1603
1.02k
}
1604
1605
} // namespace
1606
1607
/// all_declared_ivar_begin - return first ivar declared in this class,
1608
/// its extensions and its implementation. Lazily build the list on first
1609
/// access.
1610
///
1611
/// Caveat: The list returned by this method reflects the current
1612
/// state of the parser. The cache will be updated for every ivar
1613
/// added by an extension or the implementation when they are
1614
/// encountered.
1615
/// See also ObjCIvarDecl::Create().
1616
160k
ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1617
  // FIXME: Should make sure no callers ever do this.
1618
160k
  if (!hasDefinition())
1619
0
    return nullptr;
1620
160k
1621
160k
  ObjCIvarDecl *curIvar = nullptr;
1622
160k
  if (!data().IvarList) {
1623
103k
    if (!ivar_empty()) {
1624
42.9k
      ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1625
42.9k
      data().IvarList = *I; ++I;
1626
178k
      for (curIvar = data().IvarList; I != E; 
curIvar = *I, ++I136k
)
1627
136k
        curIvar->setNextIvar(*I);
1628
42.9k
    }
1629
103k
1630
1.62k
    for (const auto *Ext : known_extensions()) {
1631
1.62k
      if (!Ext->ivar_empty()) {
1632
196
        ObjCCategoryDecl::ivar_iterator
1633
196
          I = Ext->ivar_begin(),
1634
196
          E = Ext->ivar_end();
1635
196
        if (!data().IvarList) {
1636
86
          data().IvarList = *I; ++I;
1637
86
          curIvar = data().IvarList;
1638
86
        }
1639
411
        for ( ;I != E; 
curIvar = *I, ++I215
)
1640
215
          curIvar->setNextIvar(*I);
1641
196
      }
1642
1.62k
    }
1643
103k
    data().IvarListMissingImplementation = true;
1644
103k
  }
1645
160k
1646
  // cached and complete!
1647
160k
  if (!data().IvarListMissingImplementation)
1648
9.96k
      return data().IvarList;
1649
150k
1650
150k
  if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1651
9.32k
    data().IvarListMissingImplementation = false;
1652
9.32k
    if (!ImplDecl->ivar_empty()) {
1653
1.20k
      SmallVector<SynthesizeIvarChunk, 16> layout;
1654
2.26k
      for (auto *IV : ImplDecl->ivars()) {
1655
2.26k
        if (IV->getSynthesize() && 
!IV->isInvalidDecl()1.98k
) {
1656
1.97k
          layout.push_back(SynthesizeIvarChunk(
1657
1.97k
                             IV->getASTContext().getTypeSize(IV->getType()), IV));
1658
1.97k
          continue;
1659
1.97k
        }
1660
289
        if (!data().IvarList)
1661
147
          data().IvarList = IV;
1662
142
        else
1663
142
          curIvar->setNextIvar(IV);
1664
289
        curIvar = IV;
1665
289
      }
1666
1.20k
1667
1.20k
      if (!layout.empty()) {
1668
        // Order synthesized ivars by their size.
1669
1.02k
        llvm::stable_sort(layout);
1670
1.02k
        unsigned Ix = 0, EIx = layout.size();
1671
1.02k
        if (!data().IvarList) {
1672
835
          data().IvarList = layout[0].Ivar; Ix++;
1673
835
          curIvar = data().IvarList;
1674
835
        }
1675
2.16k
        for ( ; Ix != EIx; 
curIvar = layout[Ix].Ivar, Ix++1.14k
)
1676
1.14k
          curIvar->setNextIvar(layout[Ix].Ivar);
1677
1.02k
      }
1678
1.20k
    }
1679
9.32k
  }
1680
150k
  return data().IvarList;
1681
150k
}
1682
1683
/// FindCategoryDeclaration - Finds category declaration in the list of
1684
/// categories for this class and returns it. Name of the category is passed
1685
/// in 'CategoryId'. If category not found, return 0;
1686
///
1687
ObjCCategoryDecl *
1688
57.8k
ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1689
  // FIXME: Should make sure no callers ever do this.
1690
57.8k
  if (!hasDefinition())
1691
3
    return nullptr;
1692
57.8k
1693
57.8k
  if (data().ExternallyCompleted)
1694
0
    LoadExternalDefinition();
1695
57.8k
1696
57.8k
  for (auto *Cat : visible_categories())
1697
190k
    if (Cat->getIdentifier() == CategoryId)
1698
2.74k
      return Cat;
1699
57.8k
1700
55.1k
  return nullptr;
1701
57.8k
}
1702
1703
ObjCMethodDecl *
1704
34.2k
ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1705
354k
  for (const auto *Cat : visible_categories()) {
1706
354k
    if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1707
1.51k
      if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1708
35
        return MD;
1709
354k
  }
1710
34.2k
1711
34.1k
  return nullptr;
1712
34.2k
}
1713
1714
18.1k
ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1715
135k
  for (const auto *Cat : visible_categories()) {
1716
135k
    if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1717
1.37k
      if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1718
13
        return MD;
1719
135k
  }
1720
18.1k
1721
18.1k
  return nullptr;
1722
18.1k
}
1723
1724
/// ClassImplementsProtocol - Checks that 'lProto' protocol
1725
/// has been implemented in IDecl class, its super class or categories (if
1726
/// lookupCategory is true).
1727
bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1728
                                    bool lookupCategory,
1729
1.46k
                                    bool RHSIsQualifiedID) {
1730
1.46k
  if (!hasDefinition())
1731
2
    return false;
1732
1.46k
1733
1.46k
  ObjCInterfaceDecl *IDecl = this;
1734
  // 1st, look up the class.
1735
1.42k
  for (auto *PI : IDecl->protocols()){
1736
1.42k
    if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1737
1.13k
      return true;
1738
    // This is dubious and is added to be compatible with gcc.  In gcc, it is
1739
    // also allowed assigning a protocol-qualified 'id' type to a LHS object
1740
    // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1741
    // object. This IMO, should be a bug.
1742
    // FIXME: Treat this as an extension, and flag this as an error when GCC
1743
    // extensions are not enabled.
1744
283
    if (RHSIsQualifiedID &&
1745
0
        getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
1746
0
      return true;
1747
329
  }
1748
329
1749
  // 2nd, look up the category.
1750
329
  if (lookupCategory)
1751
168
    for (const auto *Cat : visible_categories()) {
1752
154
      for (auto *PI : Cat->protocols())
1753
21
        if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1754
14
          return true;
1755
154
    }
1756
329
1757
  // 3rd, look up the super class(s)
1758
315
  if (IDecl->getSuperClass())
1759
209
    return
1760
209
  IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1761
209
                                                  RHSIsQualifiedID);
1762
106
1763
106
  return false;
1764
106
}
1765
1766
//===----------------------------------------------------------------------===//
1767
// ObjCIvarDecl
1768
//===----------------------------------------------------------------------===//
1769
1770
0
void ObjCIvarDecl::anchor() {}
1771
1772
ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1773
                                   SourceLocation StartLoc,
1774
                                   SourceLocation IdLoc, IdentifierInfo *Id,
1775
                                   QualType T, TypeSourceInfo *TInfo,
1776
                                   AccessControl ac, Expr *BW,
1777
181k
                                   bool synthesized) {
1778
181k
  if (DC) {
1779
    // Ivar's can only appear in interfaces, implementations (via synthesized
1780
    // properties), and class extensions (via direct declaration, or synthesized
1781
    // properties).
1782
    //
1783
    // FIXME: This should really be asserting this:
1784
    //   (isa<ObjCCategoryDecl>(DC) &&
1785
    //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1786
    // but unfortunately we sometimes place ivars into non-class extension
1787
    // categories on error. This breaks an AST invariant, and should not be
1788
    // fixed.
1789
181k
    assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1790
181k
            isa<ObjCCategoryDecl>(DC)) &&
1791
181k
           "Invalid ivar decl context!");
1792
    // Once a new ivar is created in any of class/class-extension/implementation
1793
    // decl contexts, the previously built IvarList must be rebuilt.
1794
181k
    auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1795
181k
    if (!ID) {
1796
2.43k
      if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
1797
2.25k
        ID = IM->getClassInterface();
1798
178
      else
1799
178
        ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1800
2.43k
    }
1801
181k
    ID->setIvarList(nullptr);
1802
181k
  }
1803
181k
1804
181k
  return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1805
181k
                                  synthesized);
1806
181k
}
1807
1808
1.19k
ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1809
1.19k
  return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1810
1.19k
                                  nullptr, QualType(), nullptr,
1811
1.19k
                                  ObjCIvarDecl::None, nullptr, false);
1812
1.19k
}
1813
1814
8.30k
const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1815
8.30k
  const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
1816
8.30k
1817
8.30k
  switch (DC->getKind()) {
1818
0
  default:
1819
0
  case ObjCCategoryImpl:
1820
0
  case ObjCProtocol:
1821
0
    llvm_unreachable("invalid ivar container!");
1822
0
1823
    // Ivars can only appear in class extension categories.
1824
198
  case ObjCCategory: {
1825
198
    const auto *CD = cast<ObjCCategoryDecl>(DC);
1826
198
    assert(CD->IsClassExtension() && "invalid container for ivar!");
1827
198
    return CD->getClassInterface();
1828
0
  }
1829
0
1830
2.37k
  case ObjCImplementation:
1831
2.37k
    return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1832
0
1833
5.74k
  case ObjCInterface:
1834
5.74k
    return cast<ObjCInterfaceDecl>(DC);
1835
8.30k
  }
1836
8.30k
}
1837
1838
5.53k
QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1839
5.53k
  return getType().substObjCMemberType(objectType, getDeclContext(),
1840
5.53k
                                       ObjCSubstitutionContext::Property);
1841
5.53k
}
1842
1843
//===----------------------------------------------------------------------===//
1844
// ObjCAtDefsFieldDecl
1845
//===----------------------------------------------------------------------===//
1846
1847
0
void ObjCAtDefsFieldDecl::anchor() {}
1848
1849
ObjCAtDefsFieldDecl
1850
*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1851
                             SourceLocation StartLoc,  SourceLocation IdLoc,
1852
7
                             IdentifierInfo *Id, QualType T, Expr *BW) {
1853
7
  return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1854
7
}
1855
1856
ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1857
0
                                                             unsigned ID) {
1858
0
  return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1859
0
                                         SourceLocation(), nullptr, QualType(),
1860
0
                                         nullptr);
1861
0
}
1862
1863
//===----------------------------------------------------------------------===//
1864
// ObjCProtocolDecl
1865
//===----------------------------------------------------------------------===//
1866
1867
0
void ObjCProtocolDecl::anchor() {}
1868
1869
ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1870
                                   IdentifierInfo *Id, SourceLocation nameLoc,
1871
                                   SourceLocation atStartLoc,
1872
                                   ObjCProtocolDecl *PrevDecl)
1873
    : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1874
32.4k
      redeclarable_base(C) {
1875
32.4k
  setPreviousDecl(PrevDecl);
1876
32.4k
  if (PrevDecl)
1877
6.50k
    Data = PrevDecl->Data;
1878
32.4k
}
1879
1880
ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1881
                                           IdentifierInfo *Id,
1882
                                           SourceLocation nameLoc,
1883
                                           SourceLocation atStartLoc,
1884
29.5k
                                           ObjCProtocolDecl *PrevDecl) {
1885
29.5k
  auto *Result =
1886
29.5k
      new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1887
29.5k
  Result->Data.setInt(!C.getLangOpts().Modules);
1888
29.5k
  return Result;
1889
29.5k
}
1890
1891
ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1892
2.93k
                                                       unsigned ID) {
1893
2.93k
  ObjCProtocolDecl *Result =
1894
2.93k
      new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1895
2.93k
                                   SourceLocation(), nullptr);
1896
2.93k
  Result->Data.setInt(!C.getLangOpts().Modules);
1897
2.93k
  return Result;
1898
2.93k
}
1899
1900
99
ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1901
99
  ObjCProtocolDecl *PDecl = this;
1902
99
1903
99
  if (Name == getIdentifier())
1904
47
    return PDecl;
1905
52
1906
52
  for (auto *I : protocols())
1907
13
    if ((PDecl = I->lookupProtocolNamed(Name)))
1908
8
      return PDecl;
1909
52
1910
44
  return nullptr;
1911
52
}
1912
1913
// lookupMethod - Lookup a instance/class method in the protocol and protocols
1914
// it inherited.
1915
ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1916
245k
                                               bool isInstance) const {
1917
245k
  ObjCMethodDecl *MethodDecl = nullptr;
1918
245k
1919
  // If there is no definition or the definition is hidden, we don't find
1920
  // anything.
1921
245k
  const ObjCProtocolDecl *Def = getDefinition();
1922
245k
  if (!Def || !Def->isUnconditionallyVisible())
1923
7
    return nullptr;
1924
245k
1925
245k
  if ((MethodDecl = getMethod(Sel, isInstance)))
1926
4.29k
    return MethodDecl;
1927
240k
1928
240k
  for (const auto *I : protocols())
1929
60.4k
    if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1930
451
      return MethodDecl;
1931
240k
  return nullptr;
1932
240k
}
1933
1934
24.8k
void ObjCProtocolDecl::allocateDefinitionData() {
1935
24.8k
  assert(!Data.getPointer() && "Protocol already has a definition!");
1936
24.8k
  Data.setPointer(new (getASTContext()) DefinitionData);
1937
24.8k
  Data.getPointer()->Definition = this;
1938
24.8k
}
1939
1940
22.5k
void ObjCProtocolDecl::startDefinition() {
1941
22.5k
  allocateDefinitionData();
1942
22.5k
1943
  // Update all of the declarations with a pointer to the definition.
1944
22.5k
  for (auto *RD : redecls())
1945
28.8k
    RD->Data = this->Data;
1946
22.5k
}
1947
1948
void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1949
2.60k
                                                    PropertyDeclOrder &PO) const {
1950
2.60k
  if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1951
2.63k
    for (auto *Prop : PDecl->properties()) {
1952
      // Insert into PM if not there already.
1953
2.63k
      PM.insert(std::make_pair(
1954
2.63k
          std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1955
2.63k
          Prop));
1956
2.63k
      PO.push_back(Prop);
1957
2.63k
    }
1958
    // Scan through protocol's protocols.
1959
2.59k
    for (const auto *PI : PDecl->protocols())
1960
295
      PI->collectPropertiesToImplement(PM, PO);
1961
2.59k
  }
1962
2.60k
}
1963
1964
void ObjCProtocolDecl::collectInheritedProtocolProperties(
1965
    const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
1966
149
    PropertyDeclOrder &PO) const {
1967
149
  if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1968
149
    if (!PS.insert(PDecl).second)
1969
13
      return;
1970
148
    
for (auto *Prop : PDecl->properties())136
{
1971
148
      if (Prop == Property)
1972
88
        continue;
1973
60
      if (Prop->getIdentifier() == Property->getIdentifier()) {
1974
20
        PO.push_back(Prop);
1975
20
        return;
1976
20
      }
1977
60
    }
1978
    // Scan through protocol's protocols which did not have a matching property.
1979
116
    for (const auto *PI : PDecl->protocols())
1980
24
      PI->collectInheritedProtocolProperties(Property, PS, PO);
1981
116
  }
1982
149
}
1983
1984
StringRef
1985
717
ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
1986
717
  if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1987
26
    return ObjCRTName->getMetadataName();
1988
691
1989
691
  return getName();
1990
691
}
1991
1992
//===----------------------------------------------------------------------===//
1993
// ObjCCategoryDecl
1994
//===----------------------------------------------------------------------===//
1995
1996
0
void ObjCCategoryDecl::anchor() {}
1997
1998
ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1999
                                   SourceLocation ClassNameLoc,
2000
                                   SourceLocation CategoryNameLoc,
2001
                                   IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2002
                                   ObjCTypeParamList *typeParamList,
2003
                                   SourceLocation IvarLBraceLoc,
2004
                                   SourceLocation IvarRBraceLoc)
2005
    : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
2006
      ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
2007
73.5k
      IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
2008
73.5k
  setTypeParamList(typeParamList);
2009
73.5k
}
2010
2011
ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
2012
                                           SourceLocation AtLoc,
2013
                                           SourceLocation ClassNameLoc,
2014
                                           SourceLocation CategoryNameLoc,
2015
                                           IdentifierInfo *Id,
2016
                                           ObjCInterfaceDecl *IDecl,
2017
                                           ObjCTypeParamList *typeParamList,
2018
                                           SourceLocation IvarLBraceLoc,
2019
56.0k
                                           SourceLocation IvarRBraceLoc) {
2020
56.0k
  auto *CatDecl =
2021
56.0k
      new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
2022
56.0k
                                   IDecl, typeParamList, IvarLBraceLoc,
2023
56.0k
                                   IvarRBraceLoc);
2024
56.0k
  if (IDecl) {
2025
    // Link this category into its class's category list.
2026
56.0k
    CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
2027
56.0k
    if (IDecl->hasDefinition()) {
2028
56.0k
      IDecl->setCategoryListRaw(CatDecl);
2029
56.0k
      if (ASTMutationListener *L = C.getASTMutationListener())
2030
1.76k
        L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2031
56.0k
    }
2032
56.0k
  }
2033
56.0k
2034
56.0k
  return CatDecl;
2035
56.0k
}
2036
2037
ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
2038
17.4k
                                                       unsigned ID) {
2039
17.4k
  return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2040
17.4k
                                      SourceLocation(), SourceLocation(),
2041
17.4k
                                      nullptr, nullptr, nullptr);
2042
17.4k
}
2043
2044
1.37M
ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2045
1.37M
  return getASTContext().getObjCImplementation(
2046
1.37M
                                           const_cast<ObjCCategoryDecl*>(this));
2047
1.37M
}
2048
2049
512
void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2050
512
  getASTContext().setObjCImplementation(this, ImplD);
2051
512
}
2052
2053
76.0k
void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2054
76.0k
  TypeParamList = TPL;
2055
76.0k
  if (!TPL)
2056
62.1k
    return;
2057
  // Set the declaration context of each of the type parameters.
2058
13.8k
  for (auto *typeParam : *TypeParamList)
2059
17.1k
    typeParam->setDeclContext(this);
2060
13.8k
}
2061
2062
//===----------------------------------------------------------------------===//
2063
// ObjCCategoryImplDecl
2064
//===----------------------------------------------------------------------===//
2065
2066
0
void ObjCCategoryImplDecl::anchor() {}
2067
2068
ObjCCategoryImplDecl *
2069
ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
2070
                             IdentifierInfo *Id,
2071
                             ObjCInterfaceDecl *ClassInterface,
2072
                             SourceLocation nameLoc,
2073
                             SourceLocation atStartLoc,
2074
522
                             SourceLocation CategoryNameLoc) {
2075
522
  if (ClassInterface && 
ClassInterface->hasDefinition()509
)
2076
507
    ClassInterface = ClassInterface->getDefinition();
2077
522
  return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2078
522
                                          atStartLoc, CategoryNameLoc);
2079
522
}
2080
2081
ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2082
7
                                                               unsigned ID) {
2083
7
  return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2084
7
                                          SourceLocation(), SourceLocation(),
2085
7
                                          SourceLocation());
2086
7
}
2087
2088
1.61k
ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2089
  // The class interface might be NULL if we are working with invalid code.
2090
1.61k
  if (const ObjCInterfaceDecl *ID = getClassInterface())
2091
1.60k
    return ID->FindCategoryDeclaration(getIdentifier());
2092
11
  return nullptr;
2093
11
}
2094
2095
0
void ObjCImplDecl::anchor() {}
2096
2097
2.98k
void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2098
  // FIXME: The context should be correct before we get here.
2099
2.98k
  property->setLexicalDeclContext(this);
2100
2.98k
  addDecl(property);
2101
2.98k
}
2102
2103
37
void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2104
37
  ASTContext &Ctx = getASTContext();
2105
37
2106
37
  if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2107
30
    if (IFace)
2108
30
      Ctx.setObjCImplementation(IFace, ImplD);
2109
30
2110
7
  } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2111
7
    if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2112
7
      Ctx.setObjCImplementation(CD, ImplD);
2113
7
  }
2114
37
2115
37
  ClassInterface = IFace;
2116
37
}
2117
2118
/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2119
/// properties implemented in this \@implementation block and returns
2120
/// the implemented property that uses it.
2121
ObjCPropertyImplDecl *ObjCImplDecl::
2122
4.85k
FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2123
4.85k
  for (auto *PID : property_impls())
2124
7.76k
    if (PID->getPropertyIvarDecl() &&
2125
7.60k
        PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2126
302
      return PID;
2127
4.54k
  return nullptr;
2128
4.85k
}
2129
2130
/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2131
/// added to the list of those properties \@synthesized/\@dynamic in this
2132
/// category \@implementation block.
2133
ObjCPropertyImplDecl *ObjCImplDecl::
2134
FindPropertyImplDecl(IdentifierInfo *Id,
2135
8.31k
                     ObjCPropertyQueryKind QueryKind) const {
2136
8.31k
  ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2137
8.31k
  for (auto *PID : property_impls())
2138
    // If queryKind is unknown, we return the instance property if one
2139
    // exists; otherwise we return the class property.
2140
24.2k
    if (PID->getPropertyDecl()->getIdentifier() == Id) {
2141
3.83k
      if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2142
2
           !PID->getPropertyDecl()->isClassProperty()) ||
2143
3.82k
          (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2144
7
           PID->getPropertyDecl()->isClassProperty()) ||
2145
3.82k
          (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2146
3.82k
           !PID->getPropertyDecl()->isClassProperty()))
2147
3.82k
        return PID;
2148
2
2149
2
      if (PID->getPropertyDecl()->isClassProperty())
2150
0
        ClassPropImpl = PID;
2151
2
    }
2152
8.31k
2153
4.48k
  if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2154
    // We can't find the instance property, return the class property.
2155
1.74k
    return ClassPropImpl;
2156
2.74k
2157
2.74k
  return nullptr;
2158
2.74k
}
2159
2160
raw_ostream &clang::operator<<(raw_ostream &OS,
2161
447
                               const ObjCCategoryImplDecl &CID) {
2162
447
  OS << CID.getName();
2163
447
  return OS;
2164
447
}
2165
2166
//===----------------------------------------------------------------------===//
2167
// ObjCImplementationDecl
2168
//===----------------------------------------------------------------------===//
2169
2170
0
void ObjCImplementationDecl::anchor() {}
2171
2172
ObjCImplementationDecl *
2173
ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2174
                               ObjCInterfaceDecl *ClassInterface,
2175
                               ObjCInterfaceDecl *SuperDecl,
2176
                               SourceLocation nameLoc,
2177
                               SourceLocation atStartLoc,
2178
                               SourceLocation superLoc,
2179
                               SourceLocation IvarLBraceLoc,
2180
4.78k
                               SourceLocation IvarRBraceLoc) {
2181
4.78k
  if (ClassInterface && ClassInterface->hasDefinition())
2182
4.78k
    ClassInterface = ClassInterface->getDefinition();
2183
4.78k
  return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2184
4.78k
                                            nameLoc, atStartLoc, superLoc,
2185
4.78k
                                            IvarLBraceLoc, IvarRBraceLoc);
2186
4.78k
}
2187
2188
ObjCImplementationDecl *
2189
30
ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2190
30
  return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2191
30
                                            SourceLocation(), SourceLocation());
2192
30
}
2193
2194
void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2195
                                             CXXCtorInitializer ** initializers,
2196
67
                                                 unsigned numInitializers) {
2197
67
  if (numInitializers > 0) {
2198
64
    NumIvarInitializers = numInitializers;
2199
64
    auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
2200
64
    memcpy(ivarInitializers, initializers,
2201
64
           numInitializers * sizeof(CXXCtorInitializer*));
2202
64
    IvarInitializers = ivarInitializers;
2203
64
  }
2204
67
}
2205
2206
ObjCImplementationDecl::init_const_iterator
2207
168
ObjCImplementationDecl::init_begin() const {
2208
168
  return IvarInitializers.get(getASTContext().getExternalSource());
2209
168
}
2210
2211
raw_ostream &clang::operator<<(raw_ostream &OS,
2212
21
                               const ObjCImplementationDecl &ID) {
2213
21
  OS << ID.getName();
2214
21
  return OS;
2215
21
}
2216
2217
//===----------------------------------------------------------------------===//
2218
// ObjCCompatibleAliasDecl
2219
//===----------------------------------------------------------------------===//
2220
2221
0
void ObjCCompatibleAliasDecl::anchor() {}
2222
2223
ObjCCompatibleAliasDecl *
2224
ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2225
                                SourceLocation L,
2226
                                IdentifierInfo *Id,
2227
243
                                ObjCInterfaceDecl* AliasedClass) {
2228
243
  return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2229
243
}
2230
2231
ObjCCompatibleAliasDecl *
2232
3
ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2233
3
  return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2234
3
                                             nullptr, nullptr);
2235
3
}
2236
2237
//===----------------------------------------------------------------------===//
2238
// ObjCPropertyDecl
2239
//===----------------------------------------------------------------------===//
2240
2241
0
void ObjCPropertyDecl::anchor() {}
2242
2243
ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2244
                                           SourceLocation L,
2245
                                           IdentifierInfo *Id,
2246
                                           SourceLocation AtLoc,
2247
                                           SourceLocation LParenLoc,
2248
                                           QualType T,
2249
                                           TypeSourceInfo *TSI,
2250
463k
                                           PropertyControl propControl) {
2251
463k
  return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2252
463k
                                      propControl);
2253
463k
}
2254
2255
ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2256
4.33k
                                                       unsigned ID) {
2257
4.33k
  return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2258
4.33k
                                      SourceLocation(), SourceLocation(),
2259
4.33k
                                      QualType(), nullptr, None);
2260
4.33k
}
2261
2262
1.24k
QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2263
1.24k
  return DeclType.substObjCMemberType(objectType, getDeclContext(),
2264
1.24k
                                      ObjCSubstitutionContext::Property);
2265
1.24k
}
2266
2267
//===----------------------------------------------------------------------===//
2268
// ObjCPropertyImplDecl
2269
//===----------------------------------------------------------------------===//
2270
2271
ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2272
                                                   DeclContext *DC,
2273
                                                   SourceLocation atLoc,
2274
                                                   SourceLocation L,
2275
                                                   ObjCPropertyDecl *property,
2276
                                                   Kind PK,
2277
                                                   ObjCIvarDecl *ivar,
2278
2.98k
                                                   SourceLocation ivarLoc) {
2279
2.98k
  return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2280
2.98k
                                          ivarLoc);
2281
2.98k
}
2282
2283
ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2284
16
                                                               unsigned ID) {
2285
16
  return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2286
16
                                          SourceLocation(), nullptr, Dynamic,
2287
16
                                          nullptr, SourceLocation());
2288
16
}
2289
2290
141
SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2291
141
  SourceLocation EndLoc = getLocation();
2292
141
  if (IvarLoc.isValid())
2293
141
    EndLoc = IvarLoc;
2294
141
2295
141
  return SourceRange(AtLoc, EndLoc);
2296
141
}