Coverage Report

Created: 2019-07-24 05:18

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