Coverage Report

Created: 2021-01-19 06:58

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