Coverage Report

Created: 2022-07-16 07:03

/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
55.1k
void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
46
55.1k
  List = nullptr;
47
55.1k
  if (Elts == 0) 
return6.03k
; // Setting to an empty list is a noop.
48
49
49.1k
  List = new (Ctx) void*[Elts];
50
49.1k
  NumElts = Elts;
51
49.1k
  memcpy(List, InList, sizeof(void*)*Elts);
52
49.1k
}
53
54
void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
55
66.3k
                           const SourceLocation *Locs, ASTContext &Ctx) {
56
66.3k
  if (Elts == 0)
57
21.9k
    return;
58
59
44.3k
  Locations = new (Ctx) SourceLocation[Elts];
60
44.3k
  memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
61
44.3k
  set(InList, Elts, Ctx);
62
44.3k
}
63
64
//===----------------------------------------------------------------------===//
65
// ObjCInterfaceDecl
66
//===----------------------------------------------------------------------===//
67
68
ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
69
                                     IdentifierInfo *Id, SourceLocation nameLoc,
70
                                     SourceLocation atStartLoc)
71
374k
    : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
72
374k
  setAtStartLoc(atStartLoc);
73
374k
}
74
75
0
void ObjCContainerDecl::anchor() {}
76
77
/// getIvarDecl - This method looks up an ivar in this ContextDecl.
78
///
79
ObjCIvarDecl *
80
233k
ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
81
233k
  lookup_result R = lookup(Id);
82
233k
  for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
83
237k
       Ivar != IvarEnd; 
++Ivar4.16k
) {
84
9.36k
    if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
85
5.20k
      return ivar;
86
9.36k
  }
87
228k
  return nullptr;
88
233k
}
89
90
// Get the local instance/class method declared in this interface.
91
ObjCMethodDecl *
92
ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
93
12.3M
                             bool AllowHidden) const {
94
  // If this context is a hidden protocol definition, don't find any
95
  // methods there.
96
12.3M
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
97
2.07M
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
98
2.07M
      if (!Def->isUnconditionallyVisible() && 
!AllowHidden32
)
99
6
        return nullptr;
100
2.07M
  }
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.3M
  lookup_result R = lookup(Sel);
110
12.3M
  for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
111
12.3M
       Meth != MethEnd; 
++Meth9.35k
) {
112
243k
    auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
113
243k
    if (MD && MD->isInstanceMethod() == isInstance)
114
233k
      return MD;
115
243k
  }
116
12.0M
  return nullptr;
117
12.3M
}
118
119
/// This routine returns 'true' if a user declared setter method was
120
/// found in the class, its protocols, its super classes or categories.
121
/// It also returns 'true' if one of its categories has declared a 'readwrite'
122
/// property.  This is because, user must provide a setter method for the
123
/// category's 'readwrite' property.
124
bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
125
46
    const ObjCPropertyDecl *Property) const {
126
46
  Selector Sel = Property->getSetterName();
127
46
  lookup_result R = lookup(Sel);
128
46
  for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
129
57
       Meth != MethEnd; 
++Meth11
) {
130
16
    auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
131
16
    if (MD && MD->isInstanceMethod() && !MD->isImplicit())
132
5
      return true;
133
16
  }
134
135
41
  if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
136
    // Also look into categories, including class extensions, looking
137
    // for a user declared instance method.
138
22
    for (const auto *Cat : ID->visible_categories()) {
139
11
      if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
140
6
        if (!MD->isImplicit())
141
2
          return true;
142
9
      if (Cat->IsClassExtension())
143
3
        continue;
144
      // Also search through the categories looking for a 'readwrite'
145
      // declaration of this property. If one found, presumably a setter will
146
      // be provided (properties declared in categories will not get
147
      // auto-synthesized).
148
6
      for (const auto *P : Cat->properties())
149
4
        if (P->getIdentifier() == Property->getIdentifier()) {
150
2
          if (P->getPropertyAttributes() &
151
2
              ObjCPropertyAttribute::kind_readwrite)
152
2
            return true;
153
0
          break;
154
2
        }
155
6
    }
156
157
    // Also look into protocols, for a user declared instance method.
158
18
    for (const auto *Proto : ID->all_referenced_protocols())
159
10
      if (Proto->HasUserDeclaredSetterMethod(Property))
160
2
        return true;
161
162
    // And in its super class.
163
16
    ObjCInterfaceDecl *OSC = ID->getSuperClass();
164
25
    while (OSC) {
165
13
      if (OSC->HasUserDeclaredSetterMethod(Property))
166
4
        return true;
167
9
      OSC = OSC->getSuperClass();
168
9
    }
169
16
  }
170
31
  if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
171
14
    for (const auto *PI : PD->protocols())
172
6
      if (PI->HasUserDeclaredSetterMethod(Property))
173
2
        return true;
174
29
  return false;
175
31
}
176
177
ObjCPropertyDecl *
178
ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
179
                                   const IdentifierInfo *propertyID,
180
394k
                                   ObjCPropertyQueryKind queryKind) {
181
  // If this context is a hidden protocol definition, don't find any
182
  // property.
183
394k
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
184
25.4k
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
185
25.4k
      if (!Def->isUnconditionallyVisible())
186
0
        return nullptr;
187
25.4k
  }
188
189
  // If context is class, then lookup property in its visible extensions.
190
  // This comes before property is looked up in primary class.
191
394k
  if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
192
313k
    for (const auto *Ext : IDecl->visible_extensions())
193
1.69k
      if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
194
1.69k
                                                       propertyID,
195
1.69k
                                                       queryKind))
196
23
        return PD;
197
313k
  }
198
199
394k
  DeclContext::lookup_result R = DC->lookup(propertyID);
200
394k
  ObjCPropertyDecl *classProp = nullptr;
201
400k
  for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
202
394k
       
++I5.39k
)
203
11.3k
    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.46k
      if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
207
6.46k
           
!PD->isClassProperty()1.76k
) ||
208
6.46k
          
(4.70k
queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class4.70k
&&
209
4.70k
           
PD->isClassProperty()355
) ||
210
6.46k
          
(4.60k
queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance4.60k
&&
211
4.60k
           
!PD->isClassProperty()4.34k
))
212
5.96k
        return PD;
213
214
500
      if (PD->isClassProperty())
215
251
        classProp = PD;
216
500
    }
217
218
388k
  if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
219
    // We can't find the instance property, return the class property.
220
544
    return classProp;
221
222
388k
  return nullptr;
223
388k
}
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
789k
                                                 bool IsInstance) const {
237
789k
  for (auto *LookupResult : lookup(Id)) {
238
4.20k
    if (auto *Prop = dyn_cast<ObjCPropertyDecl>(LookupResult)) {
239
4.19k
      if (Prop->isInstanceProperty() == IsInstance) {
240
4.19k
        return Prop;
241
4.19k
      }
242
4.19k
    }
243
4.20k
  }
244
785k
  return nullptr;
245
789k
}
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.2k
    ObjCPropertyQueryKind QueryKind) const {
252
  // Don't find properties within hidden protocol definitions.
253
35.2k
  if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
254
19.9k
    if (const ObjCProtocolDecl *Def = Proto->getDefinition())
255
19.9k
      if (!Def->isUnconditionallyVisible())
256
6
        return nullptr;
257
19.9k
  }
258
259
  // Search the extensions of a class first; they override what's in
260
  // the class itself.
261
35.2k
  if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
262
9.53k
    for (const auto *Ext : ClassDecl->visible_extensions()) {
263
787
      if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
264
326
        return P;
265
787
    }
266
9.53k
  }
267
268
34.9k
  if (ObjCPropertyDecl *PD =
269
34.9k
        ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
270
34.9k
                                           QueryKind))
271
5.80k
    return PD;
272
273
29.1k
  switch (getKind()) {
274
0
    default:
275
0
      break;
276
19.0k
    case Decl::ObjCProtocol: {
277
19.0k
      const auto *PID = cast<ObjCProtocolDecl>(this);
278
19.0k
      for (const auto *I : PID->protocols())
279
5.51k
        if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
280
5.51k
                                                             QueryKind))
281
194
          return P;
282
18.8k
      break;
283
19.0k
    }
284
18.8k
    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.47k
    case Decl::ObjCCategory: {
306
5.47k
      const auto *OCD = cast<ObjCCategoryDecl>(this);
307
      // Look through protocols.
308
5.47k
      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.46k
      break;
314
5.47k
    }
315
29.1k
  }
316
26.8k
  return nullptr;
317
29.1k
}
318
319
0
void ObjCInterfaceDecl::anchor() {}
320
321
224k
ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
322
  // If this particular declaration has a type parameter list, return it.
323
224k
  if (ObjCTypeParamList *written = getTypeParamListAsWritten())
324
161k
    return written;
325
326
  // If there is a definition, return its type parameter list.
327
63.8k
  if (const ObjCInterfaceDecl *def = getDefinition())
328
42.6k
    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.9k
  
for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); 21.1k
decl;
334
35.4k
       
decl = decl->getPreviousDecl()32.8k
) {
335
35.4k
    if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
336
2.63k
      return written;
337
35.4k
  }
338
339
18.4k
  return nullptr;
340
21.1k
}
341
342
288k
void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
343
288k
  TypeParamList = TPL;
344
288k
  if (!TPL)
345
263k
    return;
346
  // Set the declaration context of each of the type parameters.
347
25.4k
  for (auto *typeParam : *TypeParamList)
348
34.3k
    typeParam->setDeclContext(this);
349
25.4k
}
350
351
3.04M
ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
352
  // FIXME: Should make sure no callers ever do this.
353
3.04M
  if (!hasDefinition())
354
1.28k
    return nullptr;
355
356
3.04M
  if (data().ExternallyCompleted)
357
0
    LoadExternalDefinition();
358
359
3.04M
  if (const ObjCObjectType *superType = getSuperClassType()) {
360
1.78M
    if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
361
1.78M
      if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
362
1.78M
        return superDef;
363
364
6
      return superDecl;
365
1.78M
    }
366
1.78M
  }
367
368
1.25M
  return nullptr;
369
3.04M
}
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.51k
                       ObjCPropertyQueryKind QueryKind) const {
385
  // FIXME: Should make sure no callers ever do this.
386
6.51k
  if (!hasDefinition())
387
0
    return nullptr;
388
389
6.51k
  if (data().ExternallyCompleted)
390
0
    LoadExternalDefinition();
391
392
6.51k
  if (ObjCPropertyDecl *PD =
393
6.51k
      ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
394
6.51k
                                         QueryKind))
395
153
    return PD;
396
397
  // Look through protocols.
398
6.36k
  for (const auto *I : all_referenced_protocols())
399
13.7k
    if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
400
13.7k
                                                         QueryKind))
401
689
      return P;
402
403
5.67k
  return nullptr;
404
6.36k
}
405
406
void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
407
8.83k
                                                     PropertyDeclOrder &PO) const {
408
8.83k
  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.83k
  for (const auto *Ext : known_extensions()) {
413
588
    const ObjCCategoryDecl *ClassExt = Ext;
414
588
    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
588
  }
419
8.83k
  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.83k
}
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.1k
  while (Class) {
439
18.8k
    if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
440
26
      return Class;
441
18.8k
    Class = Class->getSuperClass();
442
18.8k
  }
443
12.2k
  return nullptr;
444
12.2k
}
445
446
void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
447
                              ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
448
125
                              ASTContext &C) {
449
125
  if (data().ExternallyCompleted)
450
0
    LoadExternalDefinition();
451
452
125
  if (data().AllReferencedProtocols.empty() &&
453
125
      
data().ReferencedProtocols.empty()122
) {
454
111
    data().AllReferencedProtocols.set(ExtList, ExtNum, C);
455
111
    return;
456
111
  }
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
14
  SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
462
38
  for (unsigned i = 0; i < ExtNum; 
i++24
) {
463
24
    bool protocolExists = false;
464
24
    ObjCProtocolDecl *ProtoInExtension = ExtList[i];
465
24
    for (auto *Proto : all_referenced_protocols()) {
466
24
      if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
467
3
        protocolExists = true;
468
3
        break;
469
3
      }
470
24
    }
471
    // Do we want to warn on a protocol in extension class which
472
    // already exist in the class? Probably not.
473
24
    if (!protocolExists)
474
21
      ProtocolRefs.push_back(ProtoInExtension);
475
24
  }
476
477
14
  if (ProtocolRefs.empty())
478
1
    return;
479
480
  // Merge ProtocolRefs into class's protocol list;
481
13
  ProtocolRefs.append(all_referenced_protocol_begin(),
482
13
                      all_referenced_protocol_end());
483
484
13
  data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
485
13
}
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
80.6k
void ObjCInterfaceDecl::allocateDefinitionData() {
615
80.6k
  assert(!hasDefinition() && "ObjC class already has a definition");
616
0
  Data.setPointer(new (getASTContext()) DefinitionData());
617
80.6k
  Data.getPointer()->Definition = this;
618
80.6k
}
619
620
74.6k
void ObjCInterfaceDecl::startDefinition() {
621
74.6k
  allocateDefinitionData();
622
623
  // Update all of the declarations with a pointer to the definition.
624
108k
  for (auto *RD : redecls()) {
625
108k
    if (RD != this)
626
33.8k
      RD->Data = Data;
627
108k
  }
628
74.6k
}
629
630
ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
631
157k
                                              ObjCInterfaceDecl *&clsDeclared) {
632
  // FIXME: Should make sure no callers ever do this.
633
157k
  if (!hasDefinition())
634
0
    return nullptr;
635
636
157k
  if (data().ExternallyCompleted)
637
0
    LoadExternalDefinition();
638
639
157k
  ObjCInterfaceDecl* ClassDecl = this;
640
380k
  while (ClassDecl != nullptr) {
641
228k
    if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
642
4.92k
      clsDeclared = ClassDecl;
643
4.92k
      return I;
644
4.92k
    }
645
646
223k
    for (const auto *Ext : ClassDecl->visible_extensions()) {
647
2.89k
      if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
648
154
        clsDeclared = ClassDecl;
649
154
        return I;
650
154
      }
651
2.89k
    }
652
653
222k
    ClassDecl = ClassDecl->getSuperClass();
654
222k
  }
655
152k
  return nullptr;
656
157k
}
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
123k
  const ObjCInterfaceDecl* ClassDecl = this;
703
123k
  ObjCMethodDecl *MethodDecl = nullptr;
704
705
123k
  if (data().ExternallyCompleted)
706
0
    LoadExternalDefinition();
707
708
176k
  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
117k
    for (const auto *Cat : ClassDecl->visible_categories())
715
424k
      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
134k
      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
107k
    if (!shallowCategoryLookup)
726
231k
      
for (const auto *Cat : ClassDecl->visible_categories())53.1k
{
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
107k
    if (!followSuper)
738
54.2k
      return nullptr;
739
740
    // 5. Get to the super class (if any).
741
52.7k
    ClassDecl = ClassDecl->getSuperClass();
742
52.7k
  }
743
27.4k
  return nullptr;
744
123k
}
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.45k
    Method = Instance ? 
ImpDecl->getInstanceMethod(Sel)4.54k
762
6.45k
                      : 
ImpDecl->getClassMethod(Sel)1.90k
;
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.59k
&&
!getSuperClass()7.37k
) {
772
4.50k
    Method = lookupInstanceMethod(Sel);
773
    // Look through local category implementations associated
774
    // with the root class.
775
4.50k
    if (!Method)
776
4.30k
      Method = lookupPrivateMethod(Sel, true);
777
4.50k
  }
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.19M
      DeclEndLoc(endLoc) {
797
798
  // Initialized the bits stored in DeclContext.
799
1.19M
  ObjCMethodDeclBits.Family =
800
1.19M
      static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
801
1.19M
  setInstanceMethod(isInstance);
802
1.19M
  setVariadic(isVariadic);
803
1.19M
  setPropertyAccessor(isPropertyAccessor);
804
1.19M
  setSynthesizedAccessorStub(isSynthesizedAccessorStub);
805
1.19M
  setDefined(isDefined);
806
1.19M
  setIsRedeclaration(false);
807
1.19M
  setHasRedeclaration(false);
808
1.19M
  setDeclImplementation(impControl);
809
1.19M
  setObjCDeclQualifier(OBJC_TQ_None);
810
1.19M
  setRelatedResultType(HasRelatedResultType);
811
1.19M
  setSelLocsKind(SelLoc_StandardNoSpace);
812
1.19M
  setOverriding(false);
813
1.19M
  setHasSkippedBody(false);
814
815
1.19M
  setImplicit(isImplicitlyDeclared);
816
1.19M
}
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.16M
    bool HasRelatedResultType) {
825
1.16M
  return new (C, contextDecl) ObjCMethodDecl(
826
1.16M
      beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
827
1.16M
      isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
828
1.16M
      isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
829
1.16M
}
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.50M
bool ObjCMethodDecl::isDirectMethod() const {
837
2.50M
  return hasAttr<ObjCDirectAttr>() &&
838
2.50M
         
!getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting1.72k
;
839
2.50M
}
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
349k
Stmt *ObjCMethodDecl::getBody() const {
875
349k
  return Body.get(getASTContext().getExternalSource());
876
349k
}
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
837k
                                         ArrayRef<SourceLocation> SelLocs) {
888
837k
  ParamsAndSelLocs = nullptr;
889
837k
  NumParams = Params.size();
890
837k
  if (Params.empty() && 
SelLocs.empty()146k
)
891
96.2k
    return;
892
893
740k
  static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
894
740k
                "Alignment not sufficient for SourceLocation");
895
896
740k
  unsigned Size = sizeof(ParmVarDecl *) * NumParams +
897
740k
                  sizeof(SourceLocation) * SelLocs.size();
898
740k
  ParamsAndSelLocs = C.Allocate(Size);
899
740k
  std::copy(Params.begin(), Params.end(), getParams());
900
740k
  std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
901
740k
}
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
811k
                                     ArrayRef<SourceLocation> SelLocs) {
912
811k
  assert((!SelLocs.empty() || isImplicit()) &&
913
811k
         "No selector locs for non-implicit method");
914
811k
  if (isImplicit())
915
150k
    return setParamsAndSelLocs(C, Params, llvm::None);
916
917
660k
  setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
918
660k
                                        DeclEndLoc));
919
660k
  if (getSelLocsKind() != SelLoc_NonStandard)
920
499k
    return setParamsAndSelLocs(C, Params, llvm::None);
921
922
160k
  setParamsAndSelLocs(C, Params, SelLocs);
923
160k
}
924
925
/// A definition will return its interface declaration.
926
/// An interface declaration will return its definition.
927
/// Otherwise it will return itself.
928
52.4k
ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
929
52.4k
  ASTContext &Ctx = getASTContext();
930
52.4k
  ObjCMethodDecl *Redecl = nullptr;
931
52.4k
  if (hasRedeclaration())
932
26
    Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
933
52.4k
  if (Redecl)
934
26
    return Redecl;
935
936
52.4k
  auto *CtxD = cast<Decl>(getDeclContext());
937
938
52.4k
  if (!CtxD->isInvalidDecl()) {
939
52.4k
    if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
940
35.4k
      if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
941
584
        if (!ImplD->isInvalidDecl())
942
584
          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
943
944
35.4k
    } else 
if (auto *16.9k
CD16.9k
= dyn_cast<ObjCCategoryDecl>(CtxD)) {
945
9.50k
      if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
946
119
        if (!ImplD->isInvalidDecl())
947
119
          Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
948
949
9.50k
    } else 
if (auto *7.46k
ImplD7.46k
= 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.77k
    } 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
52.4k
  }
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
52.4k
  if (Redecl && 
cast<Decl>(Redecl->getDeclContext())->isInvalidDecl()1.28k
)
965
0
    Redecl = nullptr;
966
967
52.4k
  if (!Redecl && 
isRedeclaration()51.1k
) {
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
52.3k
  return Redecl ? 
Redecl1.28k
:
this51.1k
;
975
52.4k
}
976
977
219k
ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
978
219k
  auto *CtxD = cast<Decl>(getDeclContext());
979
219k
  const auto &Sel = getSelector();
980
981
219k
  if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
982
11.6k
    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.6k
      if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
990
4.77k
        return MD;
991
6.84k
      for (auto *Ext : IFD->known_extensions())
992
434
        if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
993
105
          return MD;
994
6.84k
    }
995
207k
  } 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
213k
  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
213k
  return this;
1010
213k
}
1011
1012
157k
SourceLocation ObjCMethodDecl::getEndLoc() const {
1013
157k
  if (Stmt *Body = getBody())
1014
67.2k
    return Body->getEndLoc();
1015
90.2k
  return DeclEndLoc;
1016
157k
}
1017
1018
304k
ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
1019
304k
  auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
1020
304k
  if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
1021
96.0k
    return family;
1022
1023
  // Check for an explicit attribute.
1024
207k
  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
207k
  family = getSelector().getMethodFamily();
1040
207k
  switch (family) {
1041
148k
  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
52.1k
  case OMF_init:
1046
52.1k
    if (!isInstanceMethod() || 
!getReturnType()->isObjCObjectPointerType()52.0k
)
1047
83
      family = OMF_None;
1048
52.1k
    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.73k
  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.77k
  case OMF_autorelease:
1066
1.87k
  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
918
  case OMF_performSelector:
1078
918
    if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
1079
30
      family = OMF_None;
1080
888
    else {
1081
888
      unsigned noParams = param_size();
1082
888
      if (noParams < 1 || noParams > 3)
1083
0
        family = OMF_None;
1084
888
      else {
1085
888
        ObjCMethodDecl::param_type_iterator it = param_type_begin();
1086
888
        QualType ArgT = (*it);
1087
888
        if (!ArgT->isObjCSelType()) {
1088
0
          family = OMF_None;
1089
0
          break;
1090
0
        }
1091
1.77k
        
while (888
--noParams) {
1092
882
          it++;
1093
882
          ArgT = (*it);
1094
882
          if (!ArgT->isObjCIdType()) {
1095
0
            family = OMF_None;
1096
0
            break;
1097
0
          }
1098
882
        }
1099
888
      }
1100
888
    }
1101
918
    break;
1102
1103
207k
  }
1104
1105
  // Cache the result.
1106
207k
  ObjCMethodDeclBits.Family = family;
1107
207k
  return family;
1108
207k
}
1109
1110
QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1111
                                     const ObjCInterfaceDecl *OID,
1112
                                     bool &selfIsPseudoStrong,
1113
1.15M
                                     bool &selfIsConsumed) const {
1114
1.15M
  QualType selfTy;
1115
1.15M
  selfIsPseudoStrong = false;
1116
1.15M
  selfIsConsumed = false;
1117
1.15M
  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
955k
    if (OID) {
1121
893k
      selfTy = Context.getObjCInterfaceType(OID);
1122
893k
      selfTy = Context.getObjCObjectPointerType(selfTy);
1123
893k
    } else {
1124
62.2k
      selfTy = Context.getObjCIdType();
1125
62.2k
    }
1126
955k
  } else // we have a factory method.
1127
202k
    selfTy = Context.getObjCClassType();
1128
1129
1.15M
  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.15M
}
1154
1155
void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1156
1.14M
                                          const ObjCInterfaceDecl *OID) {
1157
1.14M
  bool selfIsPseudoStrong, selfIsConsumed;
1158
1.14M
  QualType selfTy =
1159
1.14M
    getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1160
1.14M
  auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1161
1.14M
                                         &Context.Idents.get("self"), selfTy,
1162
1.14M
                                         ImplicitParamDecl::ObjCSelf);
1163
1.14M
  setSelfDecl(Self);
1164
1165
1.14M
  if (selfIsConsumed)
1166
2.42k
    Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1167
1168
1.14M
  if (selfIsPseudoStrong)
1169
34.0k
    Self->setARCPseudoStrong(true);
1170
1171
1.14M
  setCmdDecl(ImplicitParamDecl::Create(
1172
1.14M
      Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1173
1.14M
      Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
1174
1.14M
}
1175
1176
2.91M
ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1177
2.91M
  if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1178
2.26M
    return ID;
1179
648k
  if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1180
512k
    return CD->getClassInterface();
1181
135k
  if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1182
69.2k
    return IMD->getClassInterface();
1183
66.3k
  if (isa<ObjCProtocolDecl>(getDeclContext()))
1184
66.3k
    return nullptr;
1185
0
  llvm_unreachable("unknown method context");
1186
0
}
1187
1188
3.18k
ObjCCategoryDecl *ObjCMethodDecl::getCategory() {
1189
3.18k
  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
47.4k
                                             TypeSourceInfo *boundInfo) {
1446
47.4k
  auto *TPDecl =
1447
47.4k
    new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1448
47.4k
                                    nameLoc, name, colonLoc, boundInfo);
1449
47.4k
  QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1450
47.4k
  TPDecl->setTypeForDecl(TPType.getTypePtr());
1451
47.4k
  return TPDecl;
1452
47.4k
}
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
52.0k
    : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size()) {
1482
52.0k
  std::copy(typeParams.begin(), typeParams.end(), begin());
1483
52.0k
}
1484
1485
ObjCTypeParamList *ObjCTypeParamList::create(
1486
                     ASTContext &ctx,
1487
                     SourceLocation lAngleLoc,
1488
                     ArrayRef<ObjCTypeParamDecl *> typeParams,
1489
52.0k
                     SourceLocation rAngleLoc) {
1490
52.0k
  void *mem =
1491
52.0k
      ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1492
52.0k
                   alignof(ObjCTypeParamList));
1493
52.0k
  return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1494
52.0k
}
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
235k
                                             bool isInternal){
1515
235k
  auto *Result = new (C, DC)
1516
235k
      ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1517
235k
                        isInternal);
1518
235k
  Result->Data.setInt(!C.getLangOpts().Modules);
1519
235k
  C.getObjCInterfaceType(Result, PrevDecl);
1520
235k
  return Result;
1521
235k
}
1522
1523
ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1524
50.2k
                                                         unsigned ID) {
1525
50.2k
  auto *Result = new (C, ID)
1526
50.2k
      ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1527
50.2k
                        SourceLocation(), nullptr, false);
1528
50.2k
  Result->Data.setInt(!C.getLangOpts().Modules);
1529
50.2k
  return Result;
1530
50.2k
}
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
285k
      redeclarable_base(C) {
1540
285k
  setPreviousDecl(PrevDecl);
1541
1542
  // Copy the 'data' pointer over.
1543
285k
  if (PrevDecl)
1544
125k
    Data = PrevDecl->Data;
1545
1546
285k
  setImplicit(IsInternal);
1547
1548
285k
  setTypeParamList(typeParamList);
1549
285k
}
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.8k
void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1567
  // Check for a complete definition and recover if not so.
1568
42.8k
  if (!isThisDeclarationADefinition())
1569
0
    return;
1570
42.8k
  data().HasDesignatedInitializers = true;
1571
42.8k
}
1572
1573
7.19k
bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1574
  // Check for a complete definition and recover if not so.
1575
7.19k
  if (!isThisDeclarationADefinition())
1576
1
    return false;
1577
7.19k
  if (data().ExternallyCompleted)
1578
0
    LoadExternalDefinition();
1579
1580
7.19k
  return data().HasDesignatedInitializers;
1581
7.19k
}
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.12k
ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1593
8.12k
  if (ObjCInterfaceDecl *ID =
1594
8.12k
      const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1595
8.12k
    return ID->getObjCRuntimeNameAsString();
1596
1597
0
  return getName();
1598
8.12k
}
1599
1600
778k
ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1601
778k
  if (const ObjCInterfaceDecl *Def = getDefinition()) {
1602
778k
    if (data().ExternallyCompleted)
1603
0
      LoadExternalDefinition();
1604
1605
778k
    return getASTContext().getObjCImplementation(
1606
778k
             const_cast<ObjCInterfaceDecl*>(Def));
1607
778k
  }
1608
1609
  // FIXME: Should make sure no callers ever do this.
1610
46
  return nullptr;
1611
778k
}
1612
1613
4.86k
void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1614
4.86k
  getASTContext().setObjCImplementation(getDefinition(), ImplD);
1615
4.86k
}
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
131k
ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1644
  // FIXME: Should make sure no callers ever do this.
1645
131k
  if (!hasDefinition())
1646
0
    return nullptr;
1647
1648
131k
  ObjCIvarDecl *curIvar = nullptr;
1649
131k
  if (!data().IvarList) {
1650
    // Force ivar deserialization upfront, before building IvarList.
1651
85.3k
    (void)ivar_empty();
1652
85.3k
    for (const auto *Ext : known_extensions()) {
1653
1.62k
      (void)Ext->ivar_empty();
1654
1.62k
    }
1655
85.3k
    if (!ivar_empty()) {
1656
33.2k
      ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1657
33.2k
      data().IvarList = *I; ++I;
1658
136k
      for (curIvar = data().IvarList; I != E; 
curIvar = *I, ++I103k
)
1659
103k
        curIvar->setNextIvar(*I);
1660
33.2k
    }
1661
1662
85.3k
    for (const auto *Ext : known_extensions()) {
1663
1.62k
      if (!Ext->ivar_empty()) {
1664
272
        ObjCCategoryDecl::ivar_iterator
1665
272
          I = Ext->ivar_begin(),
1666
272
          E = Ext->ivar_end();
1667
272
        if (!data().IvarList) {
1668
135
          data().IvarList = *I; ++I;
1669
135
          curIvar = data().IvarList;
1670
135
        }
1671
541
        for ( ;I != E; 
curIvar = *I, ++I269
)
1672
269
          curIvar->setNextIvar(*I);
1673
272
      }
1674
1.62k
    }
1675
85.3k
    data().IvarListMissingImplementation = true;
1676
85.3k
  }
1677
1678
  // cached and complete!
1679
131k
  if (!data().IvarListMissingImplementation)
1680
10.0k
      return data().IvarList;
1681
1682
121k
  if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1683
9.60k
    data().IvarListMissingImplementation = false;
1684
9.60k
    if (!ImplDecl->ivar_empty()) {
1685
1.23k
      SmallVector<SynthesizeIvarChunk, 16> layout;
1686
2.29k
      for (auto *IV : ImplDecl->ivars()) {
1687
2.29k
        if (IV->getSynthesize() && 
!IV->isInvalidDecl()2.00k
) {
1688
1.99k
          layout.push_back(SynthesizeIvarChunk(
1689
1.99k
                             IV->getASTContext().getTypeSize(IV->getType()), IV));
1690
1.99k
          continue;
1691
1.99k
        }
1692
302
        if (!data().IvarList)
1693
151
          data().IvarList = IV;
1694
151
        else
1695
151
          curIvar->setNextIvar(IV);
1696
302
        curIvar = IV;
1697
302
      }
1698
1699
1.23k
      if (!layout.empty()) {
1700
        // Order synthesized ivars by their size.
1701
1.04k
        llvm::stable_sort(layout);
1702
1.04k
        unsigned Ix = 0, EIx = layout.size();
1703
1.04k
        if (!data().IvarList) {
1704
849
          data().IvarList = layout[0].Ivar; Ix++;
1705
849
          curIvar = data().IvarList;
1706
849
        }
1707
2.19k
        for ( ; Ix != EIx; 
curIvar = layout[Ix].Ivar, Ix++1.14k
)
1708
1.14k
          curIvar->setNextIvar(layout[Ix].Ivar);
1709
1.04k
      }
1710
1.23k
    }
1711
9.60k
  }
1712
121k
  return data().IvarList;
1713
131k
}
1714
1715
/// FindCategoryDeclaration - Finds category declaration in the list of
1716
/// categories for this class and returns it. Name of the category is passed
1717
/// in 'CategoryId'. If category not found, return 0;
1718
///
1719
ObjCCategoryDecl *
1720
45.8k
ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1721
  // FIXME: Should make sure no callers ever do this.
1722
45.8k
  if (!hasDefinition())
1723
3
    return nullptr;
1724
1725
45.8k
  if (data().ExternallyCompleted)
1726
0
    LoadExternalDefinition();
1727
1728
45.8k
  for (auto *Cat : visible_categories())
1729
149k
    if (Cat->getIdentifier() == CategoryId)
1730
3.16k
      return Cat;
1731
1732
42.6k
  return nullptr;
1733
45.8k
}
1734
1735
ObjCMethodDecl *
1736
19.2k
ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1737
157k
  for (const auto *Cat : visible_categories()) {
1738
157k
    if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1739
1.51k
      if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1740
35
        return MD;
1741
157k
  }
1742
1743
19.2k
  return nullptr;
1744
19.2k
}
1745
1746
7.43k
ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1747
41.8k
  for (const auto *Cat : visible_categories()) {
1748
41.8k
    if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1749
1.37k
      if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1750
11
        return MD;
1751
41.8k
  }
1752
1753
7.42k
  return nullptr;
1754
7.43k
}
1755
1756
/// ClassImplementsProtocol - Checks that 'lProto' protocol
1757
/// has been implemented in IDecl class, its super class or categories (if
1758
/// lookupCategory is true).
1759
bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1760
                                    bool lookupCategory,
1761
1.40k
                                    bool RHSIsQualifiedID) {
1762
1.40k
  if (!hasDefinition())
1763
2
    return false;
1764
1765
1.39k
  ObjCInterfaceDecl *IDecl = this;
1766
  // 1st, look up the class.
1767
1.39k
  for (auto *PI : IDecl->protocols()){
1768
1.35k
    if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1769
1.06k
      return true;
1770
    // This is dubious and is added to be compatible with gcc.  In gcc, it is
1771
    // also allowed assigning a protocol-qualified 'id' type to a LHS object
1772
    // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1773
    // object. This IMO, should be a bug.
1774
    // FIXME: Treat this as an extension, and flag this as an error when GCC
1775
    // extensions are not enabled.
1776
283
    if (RHSIsQualifiedID &&
1777
283
        
getASTContext().ProtocolCompatibleWithProtocol(PI, lProto)0
)
1778
0
      return true;
1779
283
  }
1780
1781
  // 2nd, look up the category.
1782
329
  if (lookupCategory)
1783
168
    for (const auto *Cat : visible_categories()) {
1784
154
      for (auto *PI : Cat->protocols())
1785
21
        if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1786
14
          return true;
1787
154
    }
1788
1789
  // 3rd, look up the super class(s)
1790
315
  if (IDecl->getSuperClass())
1791
209
    return
1792
209
  IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1793
209
                                                  RHSIsQualifiedID);
1794
1795
106
  return false;
1796
315
}
1797
1798
//===----------------------------------------------------------------------===//
1799
// ObjCIvarDecl
1800
//===----------------------------------------------------------------------===//
1801
1802
0
void ObjCIvarDecl::anchor() {}
1803
1804
ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1805
                                   SourceLocation StartLoc,
1806
                                   SourceLocation IdLoc, IdentifierInfo *Id,
1807
                                   QualType T, TypeSourceInfo *TInfo,
1808
                                   AccessControl ac, Expr *BW,
1809
139k
                                   bool synthesized) {
1810
139k
  if (DC) {
1811
    // Ivar's can only appear in interfaces, implementations (via synthesized
1812
    // properties), and class extensions (via direct declaration, or synthesized
1813
    // properties).
1814
    //
1815
    // FIXME: This should really be asserting this:
1816
    //   (isa<ObjCCategoryDecl>(DC) &&
1817
    //    cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1818
    // but unfortunately we sometimes place ivars into non-class extension
1819
    // categories on error. This breaks an AST invariant, and should not be
1820
    // fixed.
1821
139k
    assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||
1822
139k
            isa<ObjCCategoryDecl>(DC)) &&
1823
139k
           "Invalid ivar decl context!");
1824
    // Once a new ivar is created in any of class/class-extension/implementation
1825
    // decl contexts, the previously built IvarList must be rebuilt.
1826
0
    auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1827
139k
    if (!ID) {
1828
2.53k
      if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
1829
2.29k
        ID = IM->getClassInterface();
1830
247
      else
1831
247
        ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1832
2.53k
    }
1833
139k
    ID->setIvarList(nullptr);
1834
139k
  }
1835
1836
0
  return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1837
139k
                                  synthesized);
1838
139k
}
1839
1840
1.23k
ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1841
1.23k
  return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1842
1.23k
                                  nullptr, QualType(), nullptr,
1843
1.23k
                                  ObjCIvarDecl::None, nullptr, false);
1844
1.23k
}
1845
1846
8.36k
ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() {
1847
8.36k
  auto *DC = cast<ObjCContainerDecl>(getDeclContext());
1848
1849
8.36k
  switch (DC->getKind()) {
1850
0
  default:
1851
0
  case ObjCCategoryImpl:
1852
0
  case ObjCProtocol:
1853
0
    llvm_unreachable("invalid ivar container!");
1854
1855
    // Ivars can only appear in class extension categories.
1856
236
  case ObjCCategory: {
1857
236
    auto *CD = cast<ObjCCategoryDecl>(DC);
1858
236
    assert(CD->IsClassExtension() && "invalid container for ivar!");
1859
0
    return CD->getClassInterface();
1860
0
  }
1861
1862
2.45k
  case ObjCImplementation:
1863
2.45k
    return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1864
1865
5.67k
  case ObjCInterface:
1866
5.67k
    return cast<ObjCInterfaceDecl>(DC);
1867
8.36k
  }
1868
8.36k
}
1869
1870
5.56k
QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1871
5.56k
  return getType().substObjCMemberType(objectType, getDeclContext(),
1872
5.56k
                                       ObjCSubstitutionContext::Property);
1873
5.56k
}
1874
1875
//===----------------------------------------------------------------------===//
1876
// ObjCAtDefsFieldDecl
1877
//===----------------------------------------------------------------------===//
1878
1879
0
void ObjCAtDefsFieldDecl::anchor() {}
1880
1881
ObjCAtDefsFieldDecl
1882
*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1883
                             SourceLocation StartLoc,  SourceLocation IdLoc,
1884
7
                             IdentifierInfo *Id, QualType T, Expr *BW) {
1885
7
  return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1886
7
}
1887
1888
ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1889
0
                                                             unsigned ID) {
1890
0
  return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1891
0
                                         SourceLocation(), nullptr, QualType(),
1892
0
                                         nullptr);
1893
0
}
1894
1895
//===----------------------------------------------------------------------===//
1896
// ObjCProtocolDecl
1897
//===----------------------------------------------------------------------===//
1898
1899
0
void ObjCProtocolDecl::anchor() {}
1900
1901
ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1902
                                   IdentifierInfo *Id, SourceLocation nameLoc,
1903
                                   SourceLocation atStartLoc,
1904
                                   ObjCProtocolDecl *PrevDecl)
1905
    : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1906
26.1k
      redeclarable_base(C) {
1907
26.1k
  setPreviousDecl(PrevDecl);
1908
26.1k
  if (PrevDecl)
1909
5.01k
    Data = PrevDecl->Data;
1910
26.1k
}
1911
1912
ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1913
                                           IdentifierInfo *Id,
1914
                                           SourceLocation nameLoc,
1915
                                           SourceLocation atStartLoc,
1916
23.4k
                                           ObjCProtocolDecl *PrevDecl) {
1917
23.4k
  auto *Result =
1918
23.4k
      new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1919
23.4k
  Result->Data.setInt(!C.getLangOpts().Modules);
1920
23.4k
  return Result;
1921
23.4k
}
1922
1923
ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1924
2.68k
                                                       unsigned ID) {
1925
2.68k
  ObjCProtocolDecl *Result =
1926
2.68k
      new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1927
2.68k
                                   SourceLocation(), nullptr);
1928
2.68k
  Result->Data.setInt(!C.getLangOpts().Modules);
1929
2.68k
  return Result;
1930
2.68k
}
1931
1932
245
bool ObjCProtocolDecl::isNonRuntimeProtocol() const {
1933
245
  return hasAttr<ObjCNonRuntimeProtocolAttr>();
1934
245
}
1935
1936
void ObjCProtocolDecl::getImpliedProtocols(
1937
27
    llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const {
1938
27
  std::queue<const ObjCProtocolDecl *> WorkQueue;
1939
27
  WorkQueue.push(this);
1940
1941
63
  while (!WorkQueue.empty()) {
1942
36
    const auto *PD = WorkQueue.front();
1943
36
    WorkQueue.pop();
1944
36
    for (const auto *Parent : PD->protocols()) {
1945
17
      const auto *Can = Parent->getCanonicalDecl();
1946
17
      auto Result = IPs.insert(Can);
1947
17
      if (Result.second)
1948
9
        WorkQueue.push(Parent);
1949
17
    }
1950
36
  }
1951
27
}
1952
1953
99
ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1954
99
  ObjCProtocolDecl *PDecl = this;
1955
1956
99
  if (Name == getIdentifier())
1957
47
    return PDecl;
1958
1959
52
  for (auto *I : protocols())
1960
13
    if ((PDecl = I->lookupProtocolNamed(Name)))
1961
8
      return PDecl;
1962
1963
44
  return nullptr;
1964
52
}
1965
1966
// lookupMethod - Lookup a instance/class method in the protocol and protocols
1967
// it inherited.
1968
ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1969
188k
                                               bool isInstance) const {
1970
188k
  ObjCMethodDecl *MethodDecl = nullptr;
1971
1972
  // If there is no definition or the definition is hidden, we don't find
1973
  // anything.
1974
188k
  const ObjCProtocolDecl *Def = getDefinition();
1975
188k
  if (!Def || !Def->isUnconditionallyVisible())
1976
7
    return nullptr;
1977
1978
188k
  if ((MethodDecl = getMethod(Sel, isInstance)))
1979
4.24k
    return MethodDecl;
1980
1981
183k
  for (const auto *I : protocols())
1982
47.1k
    if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1983
433
      return MethodDecl;
1984
183k
  return nullptr;
1985
183k
}
1986
1987
20.0k
void ObjCProtocolDecl::allocateDefinitionData() {
1988
20.0k
  assert(!Data.getPointer() && "Protocol already has a definition!");
1989
0
  Data.setPointer(new (getASTContext()) DefinitionData);
1990
20.0k
  Data.getPointer()->Definition = this;
1991
20.0k
}
1992
1993
18.0k
void ObjCProtocolDecl::startDefinition() {
1994
18.0k
  allocateDefinitionData();
1995
1996
  // Update all of the declarations with a pointer to the definition.
1997
18.0k
  for (auto *RD : redecls())
1998
22.8k
    RD->Data = this->Data;
1999
18.0k
}
2000
2001
void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
2002
2.64k
                                                    PropertyDeclOrder &PO) const {
2003
2.64k
  if (const ObjCProtocolDecl *PDecl = getDefinition()) {
2004
2.64k
    for (auto *Prop : PDecl->properties()) {
2005
      // Insert into PM if not there already.
2006
2.60k
      PM.insert(std::make_pair(
2007
2.60k
          std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
2008
2.60k
          Prop));
2009
2.60k
      PO.push_back(Prop);
2010
2.60k
    }
2011
    // Scan through protocol's protocols.
2012
2.64k
    for (const auto *PI : PDecl->protocols())
2013
323
      PI->collectPropertiesToImplement(PM, PO);
2014
2.64k
  }
2015
2.64k
}
2016
2017
void ObjCProtocolDecl::collectInheritedProtocolProperties(
2018
    const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
2019
151
    PropertyDeclOrder &PO) const {
2020
151
  if (const ObjCProtocolDecl *PDecl = getDefinition()) {
2021
151
    if (!PS.insert(PDecl).second)
2022
13
      return;
2023
150
    
for (auto *Prop : PDecl->properties())138
{
2024
150
      if (Prop == Property)
2025
90
        continue;
2026
60
      if (Prop->getIdentifier() == Property->getIdentifier()) {
2027
20
        PO.push_back(Prop);
2028
20
        return;
2029
20
      }
2030
60
    }
2031
    // Scan through protocol's protocols which did not have a matching property.
2032
118
    for (const auto *PI : PDecl->protocols())
2033
24
      PI->collectInheritedProtocolProperties(Property, PS, PO);
2034
118
  }
2035
151
}
2036
2037
StringRef
2038
831
ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
2039
831
  if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
2040
26
    return ObjCRTName->getMetadataName();
2041
2042
805
  return getName();
2043
831
}
2044
2045
//===----------------------------------------------------------------------===//
2046
// ObjCCategoryDecl
2047
//===----------------------------------------------------------------------===//
2048
2049
0
void ObjCCategoryDecl::anchor() {}
2050
2051
ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2052
                                   SourceLocation ClassNameLoc,
2053
                                   SourceLocation CategoryNameLoc,
2054
                                   IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2055
                                   ObjCTypeParamList *typeParamList,
2056
                                   SourceLocation IvarLBraceLoc,
2057
                                   SourceLocation IvarRBraceLoc)
2058
    : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
2059
      ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
2060
57.5k
      IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
2061
57.5k
  setTypeParamList(typeParamList);
2062
57.5k
}
2063
2064
ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
2065
                                           SourceLocation AtLoc,
2066
                                           SourceLocation ClassNameLoc,
2067
                                           SourceLocation CategoryNameLoc,
2068
                                           IdentifierInfo *Id,
2069
                                           ObjCInterfaceDecl *IDecl,
2070
                                           ObjCTypeParamList *typeParamList,
2071
                                           SourceLocation IvarLBraceLoc,
2072
43.6k
                                           SourceLocation IvarRBraceLoc) {
2073
43.6k
  auto *CatDecl =
2074
43.6k
      new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
2075
43.6k
                                   IDecl, typeParamList, IvarLBraceLoc,
2076
43.6k
                                   IvarRBraceLoc);
2077
43.6k
  if (IDecl) {
2078
    // Link this category into its class's category list.
2079
43.6k
    CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
2080
43.6k
    if (IDecl->hasDefinition()) {
2081
43.6k
      IDecl->setCategoryListRaw(CatDecl);
2082
43.6k
      if (ASTMutationListener *L = C.getASTMutationListener())
2083
1.59k
        L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2084
43.6k
    }
2085
43.6k
  }
2086
2087
43.6k
  return CatDecl;
2088
43.6k
}
2089
2090
ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
2091
13.9k
                                                       unsigned ID) {
2092
13.9k
  return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2093
13.9k
                                      SourceLocation(), SourceLocation(),
2094
13.9k
                                      nullptr, nullptr, nullptr);
2095
13.9k
}
2096
2097
868k
ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2098
868k
  return getASTContext().getObjCImplementation(
2099
868k
                                           const_cast<ObjCCategoryDecl*>(this));
2100
868k
}
2101
2102
515
void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2103
515
  getASTContext().setObjCImplementation(this, ImplD);
2104
515
}
2105
2106
59.9k
void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2107
59.9k
  TypeParamList = TPL;
2108
59.9k
  if (!TPL)
2109
49.4k
    return;
2110
  // Set the declaration context of each of the type parameters.
2111
10.5k
  for (auto *typeParam : *TypeParamList)
2112
13.0k
    typeParam->setDeclContext(this);
2113
10.5k
}
2114
2115
//===----------------------------------------------------------------------===//
2116
// ObjCCategoryImplDecl
2117
//===----------------------------------------------------------------------===//
2118
2119
0
void ObjCCategoryImplDecl::anchor() {}
2120
2121
ObjCCategoryImplDecl *
2122
ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
2123
                             IdentifierInfo *Id,
2124
                             ObjCInterfaceDecl *ClassInterface,
2125
                             SourceLocation nameLoc,
2126
                             SourceLocation atStartLoc,
2127
525
                             SourceLocation CategoryNameLoc) {
2128
525
  if (ClassInterface && 
ClassInterface->hasDefinition()512
)
2129
510
    ClassInterface = ClassInterface->getDefinition();
2130
525
  return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2131
525
                                          atStartLoc, CategoryNameLoc);
2132
525
}
2133
2134
ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2135
7
                                                               unsigned ID) {
2136
7
  return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2137
7
                                          SourceLocation(), SourceLocation(),
2138
7
                                          SourceLocation());
2139
7
}
2140
2141
2.03k
ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2142
  // The class interface might be NULL if we are working with invalid code.
2143
2.03k
  if (const ObjCInterfaceDecl *ID = getClassInterface())
2144
2.02k
    return ID->FindCategoryDeclaration(getIdentifier());
2145
11
  return nullptr;
2146
2.03k
}
2147
2148
0
void ObjCImplDecl::anchor() {}
2149
2150
3.00k
void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2151
  // FIXME: The context should be correct before we get here.
2152
3.00k
  property->setLexicalDeclContext(this);
2153
3.00k
  addDecl(property);
2154
3.00k
}
2155
2156
43
void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2157
43
  ASTContext &Ctx = getASTContext();
2158
2159
43
  if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2160
36
    if (IFace)
2161
36
      Ctx.setObjCImplementation(IFace, ImplD);
2162
2163
36
  } else 
if (auto *7
ImplD7
= dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2164
7
    if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2165
7
      Ctx.setObjCImplementation(CD, ImplD);
2166
7
  }
2167
2168
43
  ClassInterface = IFace;
2169
43
}
2170
2171
/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2172
/// properties implemented in this \@implementation block and returns
2173
/// the implemented property that uses it.
2174
ObjCPropertyImplDecl *ObjCImplDecl::
2175
4.86k
FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2176
4.86k
  for (auto *PID : property_impls())
2177
7.78k
    if (PID->getPropertyIvarDecl() &&
2178
7.78k
        
PID->getPropertyIvarDecl()->getIdentifier() == ivarId7.61k
)
2179
304
      return PID;
2180
4.56k
  return nullptr;
2181
4.86k
}
2182
2183
/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2184
/// added to the list of those properties \@synthesized/\@dynamic in this
2185
/// category \@implementation block.
2186
ObjCPropertyImplDecl *ObjCImplDecl::
2187
FindPropertyImplDecl(IdentifierInfo *Id,
2188
8.39k
                     ObjCPropertyQueryKind QueryKind) const {
2189
8.39k
  ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2190
8.39k
  for (auto *PID : property_impls())
2191
    // If queryKind is unknown, we return the instance property if one
2192
    // exists; otherwise we return the class property.
2193
24.3k
    if (PID->getPropertyDecl()->getIdentifier() == Id) {
2194
3.86k
      if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2195
3.86k
           
!PID->getPropertyDecl()->isClassProperty()2
) ||
2196
3.86k
          
(3.86k
QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class3.86k
&&
2197
3.86k
           
PID->getPropertyDecl()->isClassProperty()8
) ||
2198
3.86k
          
(3.85k
QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance3.85k
&&
2199
3.85k
           
!PID->getPropertyDecl()->isClassProperty()3.85k
))
2200
3.85k
        return PID;
2201
2202
3
      if (PID->getPropertyDecl()->isClassProperty())
2203
0
        ClassPropImpl = PID;
2204
3
    }
2205
2206
4.53k
  if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2207
    // We can't find the instance property, return the class property.
2208
1.75k
    return ClassPropImpl;
2209
2210
2.77k
  return nullptr;
2211
4.53k
}
2212
2213
raw_ostream &clang::operator<<(raw_ostream &OS,
2214
10
                               const ObjCCategoryImplDecl &CID) {
2215
10
  OS << CID.getName();
2216
10
  return OS;
2217
10
}
2218
2219
//===----------------------------------------------------------------------===//
2220
// ObjCImplementationDecl
2221
//===----------------------------------------------------------------------===//
2222
2223
0
void ObjCImplementationDecl::anchor() {}
2224
2225
ObjCImplementationDecl *
2226
ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2227
                               ObjCInterfaceDecl *ClassInterface,
2228
                               ObjCInterfaceDecl *SuperDecl,
2229
                               SourceLocation nameLoc,
2230
                               SourceLocation atStartLoc,
2231
                               SourceLocation superLoc,
2232
                               SourceLocation IvarLBraceLoc,
2233
4.85k
                               SourceLocation IvarRBraceLoc) {
2234
4.85k
  if (ClassInterface && ClassInterface->hasDefinition())
2235
4.85k
    ClassInterface = ClassInterface->getDefinition();
2236
4.85k
  return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2237
4.85k
                                            nameLoc, atStartLoc, superLoc,
2238
4.85k
                                            IvarLBraceLoc, IvarRBraceLoc);
2239
4.85k
}
2240
2241
ObjCImplementationDecl *
2242
36
ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2243
36
  return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2244
36
                                            SourceLocation(), SourceLocation());
2245
36
}
2246
2247
void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2248
                                             CXXCtorInitializer ** initializers,
2249
79
                                                 unsigned numInitializers) {
2250
79
  if (numInitializers > 0) {
2251
76
    NumIvarInitializers = numInitializers;
2252
76
    auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
2253
76
    memcpy(ivarInitializers, initializers,
2254
76
           numInitializers * sizeof(CXXCtorInitializer*));
2255
76
    IvarInitializers = ivarInitializers;
2256
76
  }
2257
79
}
2258
2259
ObjCImplementationDecl::init_const_iterator
2260
192
ObjCImplementationDecl::init_begin() const {
2261
192
  return IvarInitializers.get(getASTContext().getExternalSource());
2262
192
}
2263
2264
raw_ostream &clang::operator<<(raw_ostream &OS,
2265
21
                               const ObjCImplementationDecl &ID) {
2266
21
  OS << ID.getName();
2267
21
  return OS;
2268
21
}
2269
2270
//===----------------------------------------------------------------------===//
2271
// ObjCCompatibleAliasDecl
2272
//===----------------------------------------------------------------------===//
2273
2274
0
void ObjCCompatibleAliasDecl::anchor() {}
2275
2276
ObjCCompatibleAliasDecl *
2277
ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2278
                                SourceLocation L,
2279
                                IdentifierInfo *Id,
2280
243
                                ObjCInterfaceDecl* AliasedClass) {
2281
243
  return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2282
243
}
2283
2284
ObjCCompatibleAliasDecl *
2285
3
ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2286
3
  return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2287
3
                                             nullptr, nullptr);
2288
3
}
2289
2290
//===----------------------------------------------------------------------===//
2291
// ObjCPropertyDecl
2292
//===----------------------------------------------------------------------===//
2293
2294
0
void ObjCPropertyDecl::anchor() {}
2295
2296
ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2297
                                           SourceLocation L,
2298
                                           IdentifierInfo *Id,
2299
                                           SourceLocation AtLoc,
2300
                                           SourceLocation LParenLoc,
2301
                                           QualType T,
2302
                                           TypeSourceInfo *TSI,
2303
353k
                                           PropertyControl propControl) {
2304
353k
  return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2305
353k
                                      propControl);
2306
353k
}
2307
2308
ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2309
3.93k
                                                       unsigned ID) {
2310
3.93k
  return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2311
3.93k
                                      SourceLocation(), SourceLocation(),
2312
3.93k
                                      QualType(), nullptr, None);
2313
3.93k
}
2314
2315
1.27k
QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2316
1.27k
  return DeclType.substObjCMemberType(objectType, getDeclContext(),
2317
1.27k
                                      ObjCSubstitutionContext::Property);
2318
1.27k
}
2319
2320
470k
bool ObjCPropertyDecl::isDirectProperty() const {
2321
470k
  return (PropertyAttributes & ObjCPropertyAttribute::kind_direct) &&
2322
470k
         
!getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting84
;
2323
470k
}
2324
2325
//===----------------------------------------------------------------------===//
2326
// ObjCPropertyImplDecl
2327
//===----------------------------------------------------------------------===//
2328
2329
ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2330
                                                   DeclContext *DC,
2331
                                                   SourceLocation atLoc,
2332
                                                   SourceLocation L,
2333
                                                   ObjCPropertyDecl *property,
2334
                                                   Kind PK,
2335
                                                   ObjCIvarDecl *ivar,
2336
3.01k
                                                   SourceLocation ivarLoc) {
2337
3.01k
  return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2338
3.01k
                                          ivarLoc);
2339
3.01k
}
2340
2341
ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2342
16
                                                               unsigned ID) {
2343
16
  return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2344
16
                                          SourceLocation(), nullptr, Dynamic,
2345
16
                                          nullptr, SourceLocation());
2346
16
}
2347
2348
141
SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2349
141
  SourceLocation EndLoc = getLocation();
2350
141
  if (IvarLoc.isValid())
2351
141
    EndLoc = IvarLoc;
2352
2353
141
  return SourceRange(AtLoc, EndLoc);
2354
141
}