Coverage Report

Created: 2022-01-22 13:19

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