Coverage Report

Created: 2020-02-25 14:32

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