Coverage Report

Created: 2021-08-24 07:12

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