Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaObjCProperty.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
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 semantic analysis for Objective C @property and
10
//  @synthesize declarations.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Sema/SemaInternal.h"
15
#include "clang/AST/ASTMutationListener.h"
16
#include "clang/AST/DeclObjC.h"
17
#include "clang/AST/ExprCXX.h"
18
#include "clang/AST/ExprObjC.h"
19
#include "clang/Basic/SourceManager.h"
20
#include "clang/Lex/Lexer.h"
21
#include "clang/Lex/Preprocessor.h"
22
#include "clang/Sema/Initialization.h"
23
#include "llvm/ADT/DenseSet.h"
24
#include "llvm/ADT/SmallString.h"
25
26
using namespace clang;
27
28
//===----------------------------------------------------------------------===//
29
// Grammar actions.
30
//===----------------------------------------------------------------------===//
31
32
/// getImpliedARCOwnership - Given a set of property attributes and a
33
/// type, infer an expected lifetime.  The type's ownership qualification
34
/// is not considered.
35
///
36
/// Returns OCL_None if the attributes as stated do not imply an ownership.
37
/// Never returns OCL_Autoreleasing.
38
static Qualifiers::ObjCLifetime
39
1.28k
getImpliedARCOwnership(ObjCPropertyAttribute::Kind attrs, QualType type) {
40
  // retain, strong, copy, weak, and unsafe_unretained are only legal
41
  // on properties of retainable pointer type.
42
1.28k
  if (attrs &
43
1.28k
      (ObjCPropertyAttribute::kind_retain | ObjCPropertyAttribute::kind_strong |
44
696
       ObjCPropertyAttribute::kind_copy)) {
45
696
    return Qualifiers::OCL_Strong;
46
591
  } else if (attrs & ObjCPropertyAttribute::kind_weak) {
47
205
    return Qualifiers::OCL_Weak;
48
386
  } else if (attrs & ObjCPropertyAttribute::kind_unsafe_unretained) {
49
315
    return Qualifiers::OCL_ExplicitNone;
50
315
  }
51
52
  // assign can appear on other types, so we have to check the
53
  // property type.
54
71
  if (attrs & ObjCPropertyAttribute::kind_assign &&
55
0
      type->isObjCRetainableType()) {
56
0
    return Qualifiers::OCL_ExplicitNone;
57
0
  }
58
59
71
  return Qualifiers::OCL_None;
60
71
}
61
62
/// Check the internal consistency of a property declaration with
63
/// an explicit ownership qualifier.
64
static void checkPropertyDeclWithOwnership(Sema &S,
65
164
                                           ObjCPropertyDecl *property) {
66
164
  if (property->isInvalidDecl()) 
return0
;
67
68
164
  ObjCPropertyAttribute::Kind propertyKind = property->getPropertyAttributes();
69
164
  Qualifiers::ObjCLifetime propertyLifetime
70
164
    = property->getType().getObjCLifetime();
71
72
164
  assert(propertyLifetime != Qualifiers::OCL_None);
73
74
164
  Qualifiers::ObjCLifetime expectedLifetime
75
164
    = getImpliedARCOwnership(propertyKind, property->getType());
76
164
  if (!expectedLifetime) {
77
    // We have a lifetime qualifier but no dominating property
78
    // attribute.  That's okay, but restore reasonable invariants by
79
    // setting the property attribute according to the lifetime
80
    // qualifier.
81
0
    ObjCPropertyAttribute::Kind attr;
82
0
    if (propertyLifetime == Qualifiers::OCL_Strong) {
83
0
      attr = ObjCPropertyAttribute::kind_strong;
84
0
    } else if (propertyLifetime == Qualifiers::OCL_Weak) {
85
0
      attr = ObjCPropertyAttribute::kind_weak;
86
0
    } else {
87
0
      assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
88
0
      attr = ObjCPropertyAttribute::kind_unsafe_unretained;
89
0
    }
90
0
    property->setPropertyAttributes(attr);
91
0
    return;
92
0
  }
93
94
164
  if (propertyLifetime == expectedLifetime) 
return43
;
95
96
121
  property->setInvalidDecl();
97
121
  S.Diag(property->getLocation(),
98
121
         diag::err_arc_inconsistent_property_ownership)
99
121
    << property->getDeclName()
100
121
    << expectedLifetime
101
121
    << propertyLifetime;
102
121
}
103
104
/// Check this Objective-C property against a property declared in the
105
/// given protocol.
106
static void
107
CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
108
                             ObjCProtocolDecl *Proto,
109
447k
                             llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
110
  // Have we seen this protocol before?
111
447k
  if (!Known.insert(Proto).second)
112
4.73k
    return;
113
114
  // Look for a property with the same name.
115
443k
  DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
116
443k
  for (unsigned I = 0, N = R.size(); I != N; 
++I0
) {
117
1.83k
    if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
118
1.83k
      S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
119
1.83k
      return;
120
1.83k
    }
121
1.83k
  }
122
123
  // Check this property against any protocols we inherit.
124
441k
  for (auto *P : Proto->protocols())
125
148k
    CheckPropertyAgainstProtocol(S, Prop, P, Known);
126
441k
}
127
128
153k
static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
129
  // In GC mode, just look for the __weak qualifier.
130
153k
  if (S.getLangOpts().getGC() != LangOptions::NonGC) {
131
29
    if (T.isObjCGCWeak())
132
0
      return ObjCPropertyAttribute::kind_weak;
133
134
    // In ARC/MRC, look for an explicit ownership qualifier.
135
    // For some reason, this only applies to __weak.
136
153k
  } else if (auto ownership = T.getObjCLifetime()) {
137
36
    switch (ownership) {
138
33
    case Qualifiers::OCL_Weak:
139
33
      return ObjCPropertyAttribute::kind_weak;
140
3
    case Qualifiers::OCL_Strong:
141
3
      return ObjCPropertyAttribute::kind_strong;
142
0
    case Qualifiers::OCL_ExplicitNone:
143
0
      return ObjCPropertyAttribute::kind_unsafe_unretained;
144
0
    case Qualifiers::OCL_Autoreleasing:
145
0
    case Qualifiers::OCL_None:
146
0
      return 0;
147
0
    }
148
0
    llvm_unreachable("bad qualifier");
149
0
  }
150
151
153k
  return 0;
152
153k
}
153
154
static const unsigned OwnershipMask =
155
    (ObjCPropertyAttribute::kind_assign | ObjCPropertyAttribute::kind_retain |
156
     ObjCPropertyAttribute::kind_copy | ObjCPropertyAttribute::kind_weak |
157
     ObjCPropertyAttribute::kind_strong |
158
     ObjCPropertyAttribute::kind_unsafe_unretained);
159
160
1.38M
static unsigned getOwnershipRule(unsigned attr) {
161
1.38M
  unsigned result = attr & OwnershipMask;
162
163
  // From an ownership perspective, assign and unsafe_unretained are
164
  // identical; make sure one also implies the other.
165
1.38M
  if (result & (ObjCPropertyAttribute::kind_assign |
166
17.6k
                ObjCPropertyAttribute::kind_unsafe_unretained)) {
167
17.6k
    result |= ObjCPropertyAttribute::kind_assign |
168
17.6k
              ObjCPropertyAttribute::kind_unsafe_unretained;
169
17.6k
  }
170
171
1.38M
  return result;
172
1.38M
}
173
174
Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
175
                          SourceLocation LParenLoc,
176
                          FieldDeclarator &FD,
177
                          ObjCDeclSpec &ODS,
178
                          Selector GetterSel,
179
                          Selector SetterSel,
180
                          tok::ObjCKeywordKind MethodImplKind,
181
461k
                          DeclContext *lexicalDC) {
182
461k
  unsigned Attributes = ODS.getPropertyAttributes();
183
461k
  FD.D.setObjCWeakProperty((Attributes & ObjCPropertyAttribute::kind_weak) !=
184
461k
                           0);
185
461k
  TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
186
461k
  QualType T = TSI->getType();
187
461k
  if (!getOwnershipRule(Attributes)) {
188
153k
    Attributes |= deducePropertyOwnershipFromType(*this, T);
189
153k
  }
190
461k
  bool isReadWrite = ((Attributes & ObjCPropertyAttribute::kind_readwrite) ||
191
                      // default is readwrite!
192
460k
                      !(Attributes & ObjCPropertyAttribute::kind_readonly));
193
194
  // Proceed with constructing the ObjCPropertyDecls.
195
461k
  ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
196
461k
  ObjCPropertyDecl *Res = nullptr;
197
461k
  if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
198
64.0k
    if (CDecl->IsClassExtension()) {
199
670
      Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
200
670
                                           FD,
201
670
                                           GetterSel, ODS.getGetterNameLoc(),
202
670
                                           SetterSel, ODS.getSetterNameLoc(),
203
670
                                           isReadWrite, Attributes,
204
670
                                           ODS.getPropertyAttributes(),
205
670
                                           T, TSI, MethodImplKind);
206
670
      if (!Res)
207
12
        return nullptr;
208
461k
    }
209
64.0k
  }
210
211
461k
  if (!Res) {
212
460k
    Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
213
460k
                             GetterSel, ODS.getGetterNameLoc(), SetterSel,
214
460k
                             ODS.getSetterNameLoc(), isReadWrite, Attributes,
215
460k
                             ODS.getPropertyAttributes(), T, TSI,
216
460k
                             MethodImplKind);
217
460k
    if (lexicalDC)
218
0
      Res->setLexicalDeclContext(lexicalDC);
219
460k
  }
220
221
  // Validate the attributes on the @property.
222
461k
  CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
223
461k
                              (isa<ObjCInterfaceDecl>(ClassDecl) ||
224
70.4k
                               isa<ObjCProtocolDecl>(ClassDecl)));
225
226
  // Check consistency if the type has explicit ownership qualification.
227
461k
  if (Res->getType().getObjCLifetime())
228
164
    checkPropertyDeclWithOwnership(*this, Res);
229
230
461k
  llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
231
461k
  if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
232
    // For a class, compare the property against a property in our superclass.
233
391k
    bool FoundInSuper = false;
234
391k
    ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
235
984k
    while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
236
596k
      DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
237
596k
      for (unsigned I = 0, N = R.size(); I != N; 
++I1
) {
238
3.64k
        if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
239
3.64k
          DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
240
3.64k
          FoundInSuper = true;
241
3.64k
          break;
242
3.64k
        }
243
3.64k
      }
244
596k
      if (FoundInSuper)
245
3.64k
        break;
246
592k
      else
247
592k
        CurrentInterfaceDecl = Super;
248
596k
    }
249
250
391k
    if (FoundInSuper) {
251
      // Also compare the property against a property in our protocols.
252
146
      for (auto *P : CurrentInterfaceDecl->protocols()) {
253
146
        CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
254
146
      }
255
387k
    } else {
256
      // Slower path: look in all protocols we referenced.
257
294k
      for (auto *P : IFace->all_referenced_protocols()) {
258
294k
        CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
259
294k
      }
260
387k
    }
261
70.4k
  } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
262
    // We don't check if class extension. Because properties in class extension
263
    // are meant to override some of the attributes and checking has already done
264
    // when property in class extension is constructed.
265
64.0k
    if (!Cat->IsClassExtension())
266
63.4k
      for (auto *P : Cat->protocols())
267
112
        CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
268
6.36k
  } else {
269
6.36k
    ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
270
6.36k
    for (auto *P : Proto->protocols())
271
4.72k
      CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
272
6.36k
  }
273
274
461k
  ActOnDocumentableDecl(Res);
275
461k
  return Res;
276
461k
}
277
278
static ObjCPropertyAttribute::Kind
279
461k
makePropertyAttributesAsWritten(unsigned Attributes) {
280
461k
  unsigned attributesAsWritten = 0;
281
461k
  if (Attributes & ObjCPropertyAttribute::kind_readonly)
282
308k
    attributesAsWritten |= ObjCPropertyAttribute::kind_readonly;
283
461k
  if (Attributes & ObjCPropertyAttribute::kind_readwrite)
284
1.56k
    attributesAsWritten |= ObjCPropertyAttribute::kind_readwrite;
285
461k
  if (Attributes & ObjCPropertyAttribute::kind_getter)
286
22.2k
    attributesAsWritten |= ObjCPropertyAttribute::kind_getter;
287
461k
  if (Attributes & ObjCPropertyAttribute::kind_setter)
288
89
    attributesAsWritten |= ObjCPropertyAttribute::kind_setter;
289
461k
  if (Attributes & ObjCPropertyAttribute::kind_assign)
290
7.64k
    attributesAsWritten |= ObjCPropertyAttribute::kind_assign;
291
461k
  if (Attributes & ObjCPropertyAttribute::kind_retain)
292
26.2k
    attributesAsWritten |= ObjCPropertyAttribute::kind_retain;
293
461k
  if (Attributes & ObjCPropertyAttribute::kind_strong)
294
15.6k
    attributesAsWritten |= ObjCPropertyAttribute::kind_strong;
295
461k
  if (Attributes & ObjCPropertyAttribute::kind_weak)
296
1.29k
    attributesAsWritten |= ObjCPropertyAttribute::kind_weak;
297
461k
  if (Attributes & ObjCPropertyAttribute::kind_copy)
298
257k
    attributesAsWritten |= ObjCPropertyAttribute::kind_copy;
299
461k
  if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained)
300
44
    attributesAsWritten |= ObjCPropertyAttribute::kind_unsafe_unretained;
301
461k
  if (Attributes & ObjCPropertyAttribute::kind_nonatomic)
302
5.25k
    attributesAsWritten |= ObjCPropertyAttribute::kind_nonatomic;
303
461k
  if (Attributes & ObjCPropertyAttribute::kind_atomic)
304
3.35k
    attributesAsWritten |= ObjCPropertyAttribute::kind_atomic;
305
461k
  if (Attributes & ObjCPropertyAttribute::kind_class)
306
86.2k
    attributesAsWritten |= ObjCPropertyAttribute::kind_class;
307
461k
  if (Attributes & ObjCPropertyAttribute::kind_direct)
308
49
    attributesAsWritten |= ObjCPropertyAttribute::kind_direct;
309
310
461k
  return (ObjCPropertyAttribute::Kind)attributesAsWritten;
311
461k
}
312
313
static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
314
8
                                 SourceLocation LParenLoc, SourceLocation &Loc) {
315
8
  if (LParenLoc.isMacroID())
316
0
    return false;
317
318
8
  SourceManager &SM = Context.getSourceManager();
319
8
  std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
320
  // Try to load the file buffer.
321
8
  bool invalidTemp = false;
322
8
  StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
323
8
  if (invalidTemp)
324
0
    return false;
325
8
  const char *tokenBegin = file.data() + locInfo.second;
326
327
  // Lex from the start of the given location.
328
8
  Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
329
8
              Context.getLangOpts(),
330
8
              file.begin(), tokenBegin, file.end());
331
8
  Token Tok;
332
34
  do {
333
34
    lexer.LexFromRawLexer(Tok);
334
34
    if (Tok.is(tok::raw_identifier) && 
Tok.getRawIdentifier() == attrName16
) {
335
6
      Loc = Tok.getLocation();
336
6
      return true;
337
6
    }
338
28
  } while (Tok.isNot(tok::r_paren));
339
2
  return false;
340
8
}
341
342
/// Check for a mismatch in the atomicity of the given properties.
343
static void checkAtomicPropertyMismatch(Sema &S,
344
                                        ObjCPropertyDecl *OldProperty,
345
                                        ObjCPropertyDecl *NewProperty,
346
5.60k
                                        bool PropagateAtomicity) {
347
  // If the atomicity of both matches, we're done.
348
5.60k
  bool OldIsAtomic = (OldProperty->getPropertyAttributes() &
349
5.60k
                      ObjCPropertyAttribute::kind_nonatomic) == 0;
350
5.60k
  bool NewIsAtomic = (NewProperty->getPropertyAttributes() &
351
5.60k
                      ObjCPropertyAttribute::kind_nonatomic) == 0;
352
5.60k
  if (OldIsAtomic == NewIsAtomic) 
return5.59k
;
353
354
  // Determine whether the given property is readonly and implicitly
355
  // atomic.
356
11
  auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
357
    // Is it readonly?
358
8
    auto Attrs = Property->getPropertyAttributes();
359
8
    if ((Attrs & ObjCPropertyAttribute::kind_readonly) == 0)
360
3
      return false;
361
362
    // Is it nonatomic?
363
5
    if (Attrs & ObjCPropertyAttribute::kind_nonatomic)
364
0
      return false;
365
366
    // Was 'atomic' specified directly?
367
5
    if (Property->getPropertyAttributesAsWritten() &
368
5
        ObjCPropertyAttribute::kind_atomic)
369
3
      return false;
370
371
2
    return true;
372
2
  };
373
374
  // If we're allowed to propagate atomicity, and the new property did
375
  // not specify atomicity at all, propagate.
376
11
  const unsigned AtomicityMask = (ObjCPropertyAttribute::kind_atomic |
377
11
                                  ObjCPropertyAttribute::kind_nonatomic);
378
11
  if (PropagateAtomicity &&
379
8
      ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
380
3
    unsigned Attrs = NewProperty->getPropertyAttributes();
381
3
    Attrs = Attrs & ~AtomicityMask;
382
3
    if (OldIsAtomic)
383
0
      Attrs |= ObjCPropertyAttribute::kind_atomic;
384
3
    else
385
3
      Attrs |= ObjCPropertyAttribute::kind_nonatomic;
386
387
3
    NewProperty->overwritePropertyAttributes(Attrs);
388
3
    return;
389
3
  }
390
391
  // One of the properties is atomic; if it's a readonly property, and
392
  // 'atomic' wasn't explicitly specified, we're okay.
393
8
  if ((OldIsAtomic && 
isImplicitlyReadonlyAtomic(OldProperty)6
) ||
394
6
      (NewIsAtomic && 
isImplicitlyReadonlyAtomic(NewProperty)2
))
395
2
    return;
396
397
  // Diagnose the conflict.
398
6
  const IdentifierInfo *OldContextName;
399
6
  auto *OldDC = OldProperty->getDeclContext();
400
6
  if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
401
0
    OldContextName = Category->getClassInterface()->getIdentifier();
402
6
  else
403
6
    OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
404
405
6
  S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
406
6
    << NewProperty->getDeclName() << "atomic"
407
6
    << OldContextName;
408
6
  S.Diag(OldProperty->getLocation(), diag::note_property_declare);
409
6
}
410
411
ObjCPropertyDecl *
412
Sema::HandlePropertyInClassExtension(Scope *S,
413
                                     SourceLocation AtLoc,
414
                                     SourceLocation LParenLoc,
415
                                     FieldDeclarator &FD,
416
                                     Selector GetterSel,
417
                                     SourceLocation GetterNameLoc,
418
                                     Selector SetterSel,
419
                                     SourceLocation SetterNameLoc,
420
                                     const bool isReadWrite,
421
                                     unsigned &Attributes,
422
                                     const unsigned AttributesAsWritten,
423
                                     QualType T,
424
                                     TypeSourceInfo *TSI,
425
670
                                     tok::ObjCKeywordKind MethodImplKind) {
426
670
  ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
427
  // Diagnose if this property is already in continuation class.
428
670
  DeclContext *DC = CurContext;
429
670
  IdentifierInfo *PropertyId = FD.D.getIdentifier();
430
670
  ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
431
432
  // We need to look in the @interface to see if the @property was
433
  // already declared.
434
670
  if (!CCPrimary) {
435
0
    Diag(CDecl->getLocation(), diag::err_continuation_class);
436
0
    return nullptr;
437
0
  }
438
439
670
  bool isClassProperty =
440
670
      (AttributesAsWritten & ObjCPropertyAttribute::kind_class) ||
441
669
      (Attributes & ObjCPropertyAttribute::kind_class);
442
443
  // Find the property in the extended class's primary class or
444
  // extensions.
445
670
  ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
446
670
      PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
447
448
  // If we found a property in an extension, complain.
449
670
  if (PIDecl && 
isa<ObjCCategoryDecl>(PIDecl->getDeclContext())137
) {
450
4
    Diag(AtLoc, diag::err_duplicate_property);
451
4
    Diag(PIDecl->getLocation(), diag::note_property_declare);
452
4
    return nullptr;
453
4
  }
454
455
  // Check for consistency with the previous declaration, if there is one.
456
666
  if (PIDecl) {
457
    // A readonly property declared in the primary class can be refined
458
    // by adding a readwrite property within an extension.
459
    // Anything else is an error.
460
133
    if (!(PIDecl->isReadOnly() && 
isReadWrite129
)) {
461
      // Tailor the diagnostics for the common case where a readwrite
462
      // property is declared both in the @interface and the continuation.
463
      // This is a common error where the user often intended the original
464
      // declaration to be readonly.
465
4
      unsigned diag =
466
4
          (Attributes & ObjCPropertyAttribute::kind_readwrite) &&
467
1
                  (PIDecl->getPropertyAttributesAsWritten() &
468
1
                   ObjCPropertyAttribute::kind_readwrite)
469
1
              ? diag::err_use_continuation_class_redeclaration_readwrite
470
3
              : diag::err_use_continuation_class;
471
4
      Diag(AtLoc, diag)
472
4
        << CCPrimary->getDeclName();
473
4
      Diag(PIDecl->getLocation(), diag::note_property_declare);
474
4
      return nullptr;
475
4
    }
476
477
    // Check for consistency of getters.
478
129
    if (PIDecl->getGetterName() != GetterSel) {
479
     // If the getter was written explicitly, complain.
480
2
     if (AttributesAsWritten & ObjCPropertyAttribute::kind_getter) {
481
1
       Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
482
1
           << PIDecl->getGetterName() << GetterSel;
483
1
       Diag(PIDecl->getLocation(), diag::note_property_declare);
484
1
     }
485
486
      // Always adopt the getter from the original declaration.
487
2
      GetterSel = PIDecl->getGetterName();
488
2
      Attributes |= ObjCPropertyAttribute::kind_getter;
489
2
    }
490
491
    // Check consistency of ownership.
492
129
    unsigned ExistingOwnership
493
129
      = getOwnershipRule(PIDecl->getPropertyAttributes());
494
129
    unsigned NewOwnership = getOwnershipRule(Attributes);
495
129
    if (ExistingOwnership && 
NewOwnership != ExistingOwnership43
) {
496
      // If the ownership was written explicitly, complain.
497
8
      if (getOwnershipRule(AttributesAsWritten)) {
498
2
        Diag(AtLoc, diag::warn_property_attr_mismatch);
499
2
        Diag(PIDecl->getLocation(), diag::note_property_declare);
500
2
      }
501
502
      // Take the ownership from the original property.
503
8
      Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
504
8
    }
505
506
    // If the redeclaration is 'weak' but the original property is not,
507
129
    if ((Attributes & ObjCPropertyAttribute::kind_weak) &&
508
10
        !(PIDecl->getPropertyAttributesAsWritten() &
509
10
          ObjCPropertyAttribute::kind_weak) &&
510
5
        PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
511
5
        PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
512
1
      Diag(AtLoc, diag::warn_property_implicitly_mismatched);
513
1
      Diag(PIDecl->getLocation(), diag::note_property_declare);
514
1
    }
515
129
  }
516
517
  // Create a new ObjCPropertyDecl with the DeclContext being
518
  // the class extension.
519
662
  ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
520
662
                                               FD, GetterSel, GetterNameLoc,
521
662
                                               SetterSel, SetterNameLoc,
522
662
                                               isReadWrite,
523
662
                                               Attributes, AttributesAsWritten,
524
662
                                               T, TSI, MethodImplKind, DC);
525
526
  // If there was no declaration of a property with the same name in
527
  // the primary class, we're done.
528
662
  if (!PIDecl) {
529
533
    ProcessPropertyDecl(PDecl);
530
533
    return PDecl;
531
533
  }
532
533
129
  if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
534
8
    bool IncompatibleObjC = false;
535
8
    QualType ConvertedType;
536
    // Relax the strict type matching for property type in continuation class.
537
    // Allow property object type of continuation class to be different as long
538
    // as it narrows the object type in its primary class property. Note that
539
    // this conversion is safe only because the wider type is for a 'readonly'
540
    // property in primary class and 'narrowed' type for a 'readwrite' property
541
    // in continuation class.
542
8
    QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
543
8
    QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
544
8
    if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
545
7
        !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
546
6
        (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
547
6
                                  ConvertedType, IncompatibleObjC))
548
6
        || IncompatibleObjC) {
549
4
      Diag(AtLoc,
550
4
          diag::err_type_mismatch_continuation_class) << PDecl->getType();
551
4
      Diag(PIDecl->getLocation(), diag::note_property_declare);
552
4
      return nullptr;
553
4
    }
554
125
  }
555
556
  // Check that atomicity of property in class extension matches the previous
557
  // declaration.
558
125
  checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
559
560
  // Make sure getter/setter are appropriately synthesized.
561
125
  ProcessPropertyDecl(PDecl);
562
125
  return PDecl;
563
125
}
564
565
ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
566
                                           ObjCContainerDecl *CDecl,
567
                                           SourceLocation AtLoc,
568
                                           SourceLocation LParenLoc,
569
                                           FieldDeclarator &FD,
570
                                           Selector GetterSel,
571
                                           SourceLocation GetterNameLoc,
572
                                           Selector SetterSel,
573
                                           SourceLocation SetterNameLoc,
574
                                           const bool isReadWrite,
575
                                           const unsigned Attributes,
576
                                           const unsigned AttributesAsWritten,
577
                                           QualType T,
578
                                           TypeSourceInfo *TInfo,
579
                                           tok::ObjCKeywordKind MethodImplKind,
580
461k
                                           DeclContext *lexicalDC){
581
461k
  IdentifierInfo *PropertyId = FD.D.getIdentifier();
582
583
  // Property defaults to 'assign' if it is readwrite, unless this is ARC
584
  // and the type is retainable.
585
461k
  bool isAssign;
586
461k
  if (Attributes & (ObjCPropertyAttribute::kind_assign |
587
7.69k
                    ObjCPropertyAttribute::kind_unsafe_unretained)) {
588
7.69k
    isAssign = true;
589
453k
  } else if (getOwnershipRule(Attributes) || 
!isReadWrite153k
) {
590
388k
    isAssign = false;
591
65.5k
  } else {
592
65.5k
    isAssign = (!getLangOpts().ObjCAutoRefCount ||
593
1.91k
                !T->isObjCRetainableType());
594
65.5k
  }
595
596
  // Issue a warning if property is 'assign' as default and its
597
  // object, which is gc'able conforms to NSCopying protocol
598
461k
  if (getLangOpts().getGC() != LangOptions::NonGC && 
isAssign55
&&
599
46
      !(Attributes & ObjCPropertyAttribute::kind_assign)) {
600
27
    if (const ObjCObjectPointerType *ObjPtrTy =
601
9
          T->getAs<ObjCObjectPointerType>()) {
602
9
      ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
603
9
      if (IDecl)
604
5
        if (ObjCProtocolDecl* PNSCopying =
605
2
            LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
606
2
          if (IDecl->ClassImplementsProtocol(PNSCopying, true))
607
2
            Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
608
9
    }
609
27
  }
610
611
461k
  if (T->isObjCObjectType()) {
612
2
    SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
613
2
    StarLoc = getLocForEndOfToken(StarLoc);
614
2
    Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
615
2
      << FixItHint::CreateInsertion(StarLoc, "*");
616
2
    T = Context.getObjCObjectPointerType(T);
617
2
    SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
618
2
    TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
619
2
  }
620
621
461k
  DeclContext *DC = CDecl;
622
461k
  ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
623
461k
                                                     FD.D.getIdentifierLoc(),
624
461k
                                                     PropertyId, AtLoc,
625
461k
                                                     LParenLoc, T, TInfo);
626
627
461k
  bool isClassProperty =
628
461k
      (AttributesAsWritten & ObjCPropertyAttribute::kind_class) ||
629
375k
      (Attributes & ObjCPropertyAttribute::kind_class);
630
  // Class property and instance property can have the same name.
631
461k
  if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
632
2
          DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
633
2
    Diag(PDecl->getLocation(), diag::err_duplicate_property);
634
2
    Diag(prevDecl->getLocation(), diag::note_property_declare);
635
2
    PDecl->setInvalidDecl();
636
2
  }
637
461k
  else {
638
461k
    DC->addDecl(PDecl);
639
461k
    if (lexicalDC)
640
662
      PDecl->setLexicalDeclContext(lexicalDC);
641
461k
  }
642
643
461k
  if (T->isArrayType() || 
T->isFunctionType()461k
) {
644
5
    Diag(AtLoc, diag::err_property_type) << T;
645
5
    PDecl->setInvalidDecl();
646
5
  }
647
648
461k
  ProcessDeclAttributes(S, PDecl, FD.D);
649
650
  // Regardless of setter/getter attribute, we save the default getter/setter
651
  // selector names in anticipation of declaration of setter/getter methods.
652
461k
  PDecl->setGetterName(GetterSel, GetterNameLoc);
653
461k
  PDecl->setSetterName(SetterSel, SetterNameLoc);
654
461k
  PDecl->setPropertyAttributesAsWritten(
655
461k
                          makePropertyAttributesAsWritten(AttributesAsWritten));
656
657
461k
  if (Attributes & ObjCPropertyAttribute::kind_readonly)
658
308k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readonly);
659
660
461k
  if (Attributes & ObjCPropertyAttribute::kind_getter)
661
22.2k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_getter);
662
663
461k
  if (Attributes & ObjCPropertyAttribute::kind_setter)
664
89
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_setter);
665
666
461k
  if (isReadWrite)
667
153k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readwrite);
668
669
461k
  if (Attributes & ObjCPropertyAttribute::kind_retain)
670
26.2k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_retain);
671
672
461k
  if (Attributes & ObjCPropertyAttribute::kind_strong)
673
15.6k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong);
674
675
461k
  if (Attributes & ObjCPropertyAttribute::kind_weak)
676
1.33k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_weak);
677
678
461k
  if (Attributes & ObjCPropertyAttribute::kind_copy)
679
257k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_copy);
680
681
461k
  if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained)
682
48
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained);
683
684
461k
  if (isAssign)
685
73.1k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign);
686
687
  // In the semantic attributes, one of nonatomic or atomic is always set.
688
461k
  if (Attributes & ObjCPropertyAttribute::kind_nonatomic)
689
5.25k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nonatomic);
690
456k
  else
691
456k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_atomic);
692
693
  // 'unsafe_unretained' is alias for 'assign'.
694
461k
  if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained)
695
48
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign);
696
461k
  if (isAssign)
697
73.1k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained);
698
699
461k
  if (MethodImplKind == tok::objc_required)
700
1.60k
    PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
701
460k
  else if (MethodImplKind == tok::objc_optional)
702
1.29k
    PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
703
704
461k
  if (Attributes & ObjCPropertyAttribute::kind_nullability)
705
143k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nullability);
706
707
461k
  if (Attributes & ObjCPropertyAttribute::kind_null_resettable)
708
18.2k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_null_resettable);
709
710
461k
  if (Attributes & ObjCPropertyAttribute::kind_class)
711
86.2k
    PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_class);
712
713
461k
  if ((Attributes & ObjCPropertyAttribute::kind_direct) ||
714
461k
      CDecl->hasAttr<ObjCDirectMembersAttr>()) {
715
54
    if (isa<ObjCProtocolDecl>(CDecl)) {
716
1
      Diag(PDecl->getLocation(), diag::err_objc_direct_on_protocol) << true;
717
53
    } else if (getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
718
53
      PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_direct);
719
0
    } else {
720
0
      Diag(PDecl->getLocation(), diag::warn_objc_direct_property_ignored)
721
0
          << PDecl->getDeclName();
722
0
    }
723
54
  }
724
725
461k
  return PDecl;
726
461k
}
727
728
static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
729
                                 ObjCPropertyDecl *property,
730
702
                                 ObjCIvarDecl *ivar) {
731
702
  if (property->isInvalidDecl() || 
ivar->isInvalidDecl()634
)
return71
;
732
733
631
  QualType ivarType = ivar->getType();
734
631
  Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
735
736
  // The lifetime implied by the property's attributes.
737
631
  Qualifiers::ObjCLifetime propertyLifetime =
738
631
    getImpliedARCOwnership(property->getPropertyAttributes(),
739
631
                           property->getType());
740
741
  // We're fine if they match.
742
631
  if (propertyLifetime == ivarLifetime) 
return529
;
743
744
  // None isn't a valid lifetime for an object ivar in ARC, and
745
  // __autoreleasing is never valid; don't diagnose twice.
746
102
  if ((ivarLifetime == Qualifiers::OCL_None &&
747
56
       S.getLangOpts().ObjCAutoRefCount) ||
748
48
      ivarLifetime == Qualifiers::OCL_Autoreleasing)
749
58
    return;
750
751
  // If the ivar is private, and it's implicitly __unsafe_unretained
752
  // because of its type, then pretend it was actually implicitly
753
  // __strong.  This is only sound because we're processing the
754
  // property implementation before parsing any method bodies.
755
44
  if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
756
6
      propertyLifetime == Qualifiers::OCL_Strong &&
757
5
      ivar->getAccessControl() == ObjCIvarDecl::Private) {
758
2
    SplitQualType split = ivarType.split();
759
2
    if (split.Quals.hasObjCLifetime()) {
760
2
      assert(ivarType->isObjCARCImplicitlyUnretainedType());
761
2
      split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
762
2
      ivarType = S.Context.getQualifiedType(split);
763
2
      ivar->setType(ivarType);
764
2
      return;
765
2
    }
766
42
  }
767
768
42
  switch (propertyLifetime) {
769
9
  case Qualifiers::OCL_Strong:
770
9
    S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
771
9
      << property->getDeclName()
772
9
      << ivar->getDeclName()
773
9
      << ivarLifetime;
774
9
    break;
775
776
6
  case Qualifiers::OCL_Weak:
777
6
    S.Diag(ivar->getLocation(), diag::err_weak_property)
778
6
      << property->getDeclName()
779
6
      << ivar->getDeclName();
780
6
    break;
781
782
27
  case Qualifiers::OCL_ExplicitNone:
783
27
    S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
784
27
        << property->getDeclName() << ivar->getDeclName()
785
27
        << ((property->getPropertyAttributesAsWritten() &
786
27
             ObjCPropertyAttribute::kind_assign) != 0);
787
27
    break;
788
789
0
  case Qualifiers::OCL_Autoreleasing:
790
0
    llvm_unreachable("properties cannot be autoreleasing");
791
792
0
  case Qualifiers::OCL_None:
793
    // Any other property should be ignored.
794
0
    return;
795
42
  }
796
797
42
  S.Diag(property->getLocation(), diag::note_property_declare);
798
42
  if (propertyImplLoc.isValid())
799
41
    S.Diag(propertyImplLoc, diag::note_property_synthesize);
800
42
}
801
802
/// setImpliedPropertyAttributeForReadOnlyProperty -
803
/// This routine evaludates life-time attributes for a 'readonly'
804
/// property with no known lifetime of its own, using backing
805
/// 'ivar's attribute, if any. If no backing 'ivar', property's
806
/// life-time is assumed 'strong'.
807
static void setImpliedPropertyAttributeForReadOnlyProperty(
808
112
              ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
809
112
  Qualifiers::ObjCLifetime propertyLifetime =
810
112
    getImpliedARCOwnership(property->getPropertyAttributes(),
811
112
                           property->getType());
812
112
  if (propertyLifetime != Qualifiers::OCL_None)
813
54
    return;
814
815
58
  if (!ivar) {
816
    // if no backing ivar, make property 'strong'.
817
43
    property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong);
818
43
    return;
819
43
  }
820
  // property assumes owenership of backing ivar.
821
15
  QualType ivarType = ivar->getType();
822
15
  Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
823
15
  if (ivarLifetime == Qualifiers::OCL_Strong)
824
10
    property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong);
825
5
  else if (ivarLifetime == Qualifiers::OCL_Weak)
826
5
    property->setPropertyAttributes(ObjCPropertyAttribute::kind_weak);
827
15
}
828
829
static bool isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
830
27
                                            ObjCPropertyAttribute::Kind Kind) {
831
27
  return (Attr1 & Kind) != (Attr2 & Kind);
832
27
}
833
834
static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
835
11
                                              unsigned Kinds) {
836
11
  return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
837
11
}
838
839
/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
840
/// property declaration that should be synthesised in all of the inherited
841
/// protocols. It also diagnoses properties declared in inherited protocols with
842
/// mismatched types or attributes, since any of them can be candidate for
843
/// synthesis.
844
static ObjCPropertyDecl *
845
SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
846
                                        ObjCInterfaceDecl *ClassDecl,
847
88
                                        ObjCPropertyDecl *Property) {
848
88
  assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
849
88
         "Expected a property from a protocol");
850
88
  ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
851
88
  ObjCInterfaceDecl::PropertyDeclOrder Properties;
852
119
  for (const auto *PI : ClassDecl->all_referenced_protocols()) {
853
119
    if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
854
119
      PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
855
119
                                                Properties);
856
119
  }
857
88
  if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
858
81
    while (SDecl) {
859
6
      for (const auto *PI : SDecl->all_referenced_protocols()) {
860
6
        if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
861
6
          PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
862
6
                                                    Properties);
863
6
      }
864
41
      SDecl = SDecl->getSuperClass();
865
41
    }
866
40
  }
867
868
88
  if (Properties.empty())
869
72
    return Property;
870
871
16
  ObjCPropertyDecl *OriginalProperty = Property;
872
16
  size_t SelectedIndex = 0;
873
20
  for (const auto &Prop : llvm::enumerate(Properties)) {
874
    // Select the 'readwrite' property if such property exists.
875
20
    if (Property->isReadOnly() && 
!Prop.value()->isReadOnly()10
) {
876
3
      Property = Prop.value();
877
3
      SelectedIndex = Prop.index();
878
3
    }
879
20
  }
880
16
  if (Property != OriginalProperty) {
881
    // Check that the old property is compatible with the new one.
882
3
    Properties[SelectedIndex] = OriginalProperty;
883
3
  }
884
885
16
  QualType RHSType = S.Context.getCanonicalType(Property->getType());
886
16
  unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
887
16
  enum MismatchKind {
888
16
    IncompatibleType = 0,
889
16
    HasNoExpectedAttribute,
890
16
    HasUnexpectedAttribute,
891
16
    DifferentGetter,
892
16
    DifferentSetter
893
16
  };
894
  // Represents a property from another protocol that conflicts with the
895
  // selected declaration.
896
16
  struct MismatchingProperty {
897
16
    const ObjCPropertyDecl *Prop;
898
16
    MismatchKind Kind;
899
16
    StringRef AttributeName;
900
16
  };
901
16
  SmallVector<MismatchingProperty, 4> Mismatches;
902
20
  for (ObjCPropertyDecl *Prop : Properties) {
903
    // Verify the property attributes.
904
20
    unsigned Attr = Prop->getPropertyAttributesAsWritten();
905
20
    if (Attr != OriginalAttributes) {
906
5
      auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
907
2
        MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
908
3
                                                 : HasUnexpectedAttribute;
909
5
        Mismatches.push_back({Prop, Kind, AttributeName});
910
5
      };
911
      // The ownership might be incompatible unless the property has no explicit
912
      // ownership.
913
18
      bool HasOwnership =
914
18
          (Attr & (ObjCPropertyAttribute::kind_retain |
915
18
                   ObjCPropertyAttribute::kind_strong |
916
18
                   ObjCPropertyAttribute::kind_copy |
917
18
                   ObjCPropertyAttribute::kind_assign |
918
18
                   ObjCPropertyAttribute::kind_unsafe_unretained |
919
18
                   ObjCPropertyAttribute::kind_weak)) != 0;
920
18
      if (HasOwnership &&
921
13
          isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
922
2
                                          ObjCPropertyAttribute::kind_copy)) {
923
2
        Diag(OriginalAttributes & ObjCPropertyAttribute::kind_copy, "copy");
924
2
        continue;
925
2
      }
926
16
      if (HasOwnership && areIncompatiblePropertyAttributes(
927
11
                              OriginalAttributes, Attr,
928
11
                              ObjCPropertyAttribute::kind_retain |
929
2
                                  ObjCPropertyAttribute::kind_strong)) {
930
2
        Diag(OriginalAttributes & (ObjCPropertyAttribute::kind_retain |
931
2
                                   ObjCPropertyAttribute::kind_strong),
932
2
             "retain (or strong)");
933
2
        continue;
934
2
      }
935
14
      if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
936
1
                                          ObjCPropertyAttribute::kind_atomic)) {
937
1
        Diag(OriginalAttributes & ObjCPropertyAttribute::kind_atomic, "atomic");
938
1
        continue;
939
1
      }
940
15
    }
941
15
    if (Property->getGetterName() != Prop->getGetterName()) {
942
1
      Mismatches.push_back({Prop, DifferentGetter, ""});
943
1
      continue;
944
1
    }
945
14
    if (!Property->isReadOnly() && 
!Prop->isReadOnly()13
&&
946
6
        Property->getSetterName() != Prop->getSetterName()) {
947
1
      Mismatches.push_back({Prop, DifferentSetter, ""});
948
1
      continue;
949
1
    }
950
13
    QualType LHSType = S.Context.getCanonicalType(Prop->getType());
951
13
    if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
952
6
      bool IncompatibleObjC = false;
953
6
      QualType ConvertedType;
954
6
      if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
955
6
          || 
IncompatibleObjC0
) {
956
6
        Mismatches.push_back({Prop, IncompatibleType, ""});
957
6
        continue;
958
6
      }
959
6
    }
960
13
  }
961
962
16
  if (Mismatches.empty())
963
5
    return Property;
964
965
  // Diagnose incompability.
966
11
  {
967
11
    bool HasIncompatibleAttributes = false;
968
11
    for (const auto &Note : Mismatches)
969
13
      HasIncompatibleAttributes =
970
7
          Note.Kind != IncompatibleType ? true : 
HasIncompatibleAttributes6
;
971
    // Promote the warning to an error if there are incompatible attributes or
972
    // incompatible types together with readwrite/readonly incompatibility.
973
11
    auto Diag = S.Diag(Property->getLocation(),
974
11
                       Property != OriginalProperty || 
HasIncompatibleAttributes9
975
8
                           ? diag::err_protocol_property_mismatch
976
3
                           : diag::warn_protocol_property_mismatch);
977
11
    Diag << Mismatches[0].Kind;
978
11
    switch (Mismatches[0].Kind) {
979
5
    case IncompatibleType:
980
5
      Diag << Property->getType();
981
5
      break;
982
4
    case HasNoExpectedAttribute:
983
4
    case HasUnexpectedAttribute:
984
4
      Diag << Mismatches[0].AttributeName;
985
4
      break;
986
1
    case DifferentGetter:
987
1
      Diag << Property->getGetterName();
988
1
      break;
989
1
    case DifferentSetter:
990
1
      Diag << Property->getSetterName();
991
1
      break;
992
11
    }
993
11
  }
994
13
  
for (const auto &Note : Mismatches)11
{
995
13
    auto Diag =
996
13
        S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
997
13
        << Note.Kind;
998
13
    switch (Note.Kind) {
999
6
    case IncompatibleType:
1000
6
      Diag << Note.Prop->getType();
1001
6
      break;
1002
5
    case HasNoExpectedAttribute:
1003
5
    case HasUnexpectedAttribute:
1004
5
      Diag << Note.AttributeName;
1005
5
      break;
1006
1
    case DifferentGetter:
1007
1
      Diag << Note.Prop->getGetterName();
1008
1
      break;
1009
1
    case DifferentSetter:
1010
1
      Diag << Note.Prop->getSetterName();
1011
1
      break;
1012
13
    }
1013
13
  }
1014
11
  if (AtLoc.isValid())
1015
11
    S.Diag(AtLoc, diag::note_property_synthesize);
1016
1017
11
  return Property;
1018
11
}
1019
1020
/// Determine whether any storage attributes were written on the property.
1021
static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1022
380
                                       ObjCPropertyQueryKind QueryKind) {
1023
380
  if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) 
return true318
;
1024
1025
  // If this is a readwrite property in a class extension that refines
1026
  // a readonly property in the original class definition, check it as
1027
  // well.
1028
1029
  // If it's a readonly property, we're not interested.
1030
62
  if (Prop->isReadOnly()) 
return false43
;
1031
1032
  // Is it declared in an extension?
1033
19
  auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1034
19
  if (!Category || 
!Category->IsClassExtension()8
)
return false11
;
1035
1036
  // Find the corresponding property in the primary class definition.
1037
8
  auto OrigClass = Category->getClassInterface();
1038
7
  for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1039
7
    if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1040
7
      return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1041
7
  }
1042
1043
  // Look through all of the protocols.
1044
1
  for (const auto *Proto : OrigClass->all_referenced_protocols()) {
1045
1
    if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1046
1
            Prop->getIdentifier(), QueryKind))
1047
1
      return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1048
1
  }
1049
1050
0
  return false;
1051
1
}
1052
1053
/// Create a synthesized property accessor stub inside the \@implementation.
1054
static ObjCMethodDecl *
1055
RedeclarePropertyAccessor(ASTContext &Context, ObjCImplementationDecl *Impl,
1056
                          ObjCMethodDecl *AccessorDecl, SourceLocation AtLoc,
1057
5.07k
                          SourceLocation PropertyLoc) {
1058
5.07k
  ObjCMethodDecl *Decl = AccessorDecl;
1059
5.07k
  ObjCMethodDecl *ImplDecl = ObjCMethodDecl::Create(
1060
2.86k
      Context, AtLoc.isValid() ? AtLoc : 
Decl->getBeginLoc()2.20k
,
1061
2.86k
      PropertyLoc.isValid() ? PropertyLoc : 
Decl->getEndLoc()2.20k
,
1062
5.07k
      Decl->getSelector(), Decl->getReturnType(),
1063
5.07k
      Decl->getReturnTypeSourceInfo(), Impl, Decl->isInstanceMethod(),
1064
5.07k
      Decl->isVariadic(), Decl->isPropertyAccessor(),
1065
5.07k
      /* isSynthesized*/ true, Decl->isImplicit(), Decl->isDefined(),
1066
5.07k
      Decl->getImplementationControl(), Decl->hasRelatedResultType());
1067
5.07k
  ImplDecl->getMethodFamily();
1068
5.07k
  if (Decl->hasAttrs())
1069
110
    ImplDecl->setAttrs(Decl->getAttrs());
1070
5.07k
  ImplDecl->setSelfDecl(Decl->getSelfDecl());
1071
5.07k
  ImplDecl->setCmdDecl(Decl->getCmdDecl());
1072
5.07k
  SmallVector<SourceLocation, 1> SelLocs;
1073
5.07k
  Decl->getSelectorLocs(SelLocs);
1074
5.07k
  ImplDecl->setMethodParams(Context, Decl->parameters(), SelLocs);
1075
5.07k
  ImplDecl->setLexicalDeclContext(Impl);
1076
5.07k
  ImplDecl->setDefined(false);
1077
5.07k
  return ImplDecl;
1078
5.07k
}
1079
1080
/// ActOnPropertyImplDecl - This routine performs semantic checks and
1081
/// builds the AST node for a property implementation declaration; declared
1082
/// as \@synthesize or \@dynamic.
1083
///
1084
Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1085
                                  SourceLocation AtLoc,
1086
                                  SourceLocation PropertyLoc,
1087
                                  bool Synthesize,
1088
                                  IdentifierInfo *PropertyId,
1089
                                  IdentifierInfo *PropertyIvar,
1090
                                  SourceLocation PropertyIvarLoc,
1091
2.99k
                                  ObjCPropertyQueryKind QueryKind) {
1092
2.99k
  ObjCContainerDecl *ClassImpDecl =
1093
2.99k
    dyn_cast<ObjCContainerDecl>(CurContext);
1094
  // Make sure we have a context for the property implementation declaration.
1095
2.99k
  if (!ClassImpDecl) {
1096
1
    Diag(AtLoc, diag::err_missing_property_context);
1097
1
    return nullptr;
1098
1
  }
1099
2.99k
  if (PropertyIvarLoc.isInvalid())
1100
1.22k
    PropertyIvarLoc = PropertyLoc;
1101
2.99k
  SourceLocation PropertyDiagLoc = PropertyLoc;
1102
2.99k
  if (PropertyDiagLoc.isInvalid())
1103
1.23k
    PropertyDiagLoc = ClassImpDecl->getBeginLoc();
1104
2.99k
  ObjCPropertyDecl *property = nullptr;
1105
2.99k
  ObjCInterfaceDecl *IDecl = nullptr;
1106
  // Find the class or category class where this property must have
1107
  // a declaration.
1108
2.99k
  ObjCImplementationDecl *IC = nullptr;
1109
2.99k
  ObjCCategoryImplDecl *CatImplClass = nullptr;
1110
2.99k
  if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1111
2.98k
    IDecl = IC->getClassInterface();
1112
    // We always synthesize an interface for an implementation
1113
    // without an interface decl. So, IDecl is always non-zero.
1114
2.98k
    assert(IDecl &&
1115
2.98k
           "ActOnPropertyImplDecl - @implementation without @interface");
1116
1117
    // Look for this property declaration in the @implementation's @interface
1118
2.98k
    property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
1119
2.98k
    if (!property) {
1120
2
      Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
1121
2
      return nullptr;
1122
2
    }
1123
2.98k
    if (property->isClassProperty() && 
Synthesize8
) {
1124
1
      Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
1125
1
      return nullptr;
1126
1
    }
1127
2.98k
    unsigned PIkind = property->getPropertyAttributesAsWritten();
1128
2.98k
    if ((PIkind & (ObjCPropertyAttribute::kind_atomic |
1129
2.40k
                   ObjCPropertyAttribute::kind_nonatomic)) == 0) {
1130
2.40k
      if (AtLoc.isValid())
1131
1.51k
        Diag(AtLoc, diag::warn_implicit_atomic_property);
1132
892
      else
1133
892
        Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1134
2.40k
      Diag(property->getLocation(), diag::note_property_declare);
1135
2.40k
    }
1136
1137
2.98k
    if (const ObjCCategoryDecl *CD =
1138
245
        dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1139
245
      if (!CD->IsClassExtension()) {
1140
2
        Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
1141
2
        Diag(property->getLocation(), diag::note_property_declare);
1142
2
        return nullptr;
1143
2
      }
1144
2.97k
    }
1145
2.97k
    if (Synthesize && 
(PIkind & ObjCPropertyAttribute::kind_readonly)2.76k
&&
1146
361
        property->hasAttr<IBOutletAttr>() && 
!AtLoc.isValid()8
) {
1147
8
      bool ReadWriteProperty = false;
1148
      // Search into the class extensions and see if 'readonly property is
1149
      // redeclared 'readwrite', then no warning is to be issued.
1150
2
      for (auto *Ext : IDecl->known_extensions()) {
1151
2
        DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1152
2
        if (!R.empty())
1153
0
          if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1154
0
            PIkind = ExtProp->getPropertyAttributesAsWritten();
1155
0
            if (PIkind & ObjCPropertyAttribute::kind_readwrite) {
1156
0
              ReadWriteProperty = true;
1157
0
              break;
1158
0
            }
1159
0
          }
1160
2
      }
1161
1162
8
      if (!ReadWriteProperty) {
1163
8
        Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
1164
8
            << property;
1165
8
        SourceLocation readonlyLoc;
1166
8
        if (LocPropertyAttribute(Context, "readonly",
1167
6
                                 property->getLParenLoc(), readonlyLoc)) {
1168
6
          SourceLocation endLoc =
1169
6
            readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1170
6
          SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
1171
6
          Diag(property->getLocation(),
1172
6
               diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1173
6
          FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1174
6
        }
1175
8
      }
1176
8
    }
1177
2.97k
    if (Synthesize && 
isa<ObjCProtocolDecl>(property->getDeclContext())2.76k
)
1178
88
      property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1179
88
                                                         property);
1180
1181
9
  } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1182
9
    if (Synthesize) {
1183
2
      Diag(AtLoc, diag::err_synthesize_category_decl);
1184
2
      return nullptr;
1185
2
    }
1186
7
    IDecl = CatImplClass->getClassInterface();
1187
7
    if (!IDecl) {
1188
1
      Diag(AtLoc, diag::err_missing_property_interface);
1189
1
      return nullptr;
1190
1
    }
1191
6
    ObjCCategoryDecl *Category =
1192
6
    IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1193
1194
    // If category for this implementation not found, it is an error which
1195
    // has already been reported eralier.
1196
6
    if (!Category)
1197
0
      return nullptr;
1198
    // Look for this property declaration in @implementation's category
1199
6
    property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
1200
6
    if (!property) {
1201
1
      Diag(PropertyLoc, diag::err_bad_category_property_decl)
1202
1
      << Category->getDeclName();
1203
1
      return nullptr;
1204
1
    }
1205
0
  } else {
1206
0
    Diag(AtLoc, diag::err_bad_property_context);
1207
0
    return nullptr;
1208
0
  }
1209
2.98k
  ObjCIvarDecl *Ivar = nullptr;
1210
2.98k
  bool CompleteTypeErr = false;
1211
2.98k
  bool compat = true;
1212
  // Check that we have a valid, previously declared ivar for @synthesize
1213
2.98k
  if (Synthesize) {
1214
    // @synthesize
1215
2.76k
    if (!PropertyIvar)
1216
993
      PropertyIvar = PropertyId;
1217
    // Check that this is a previously declared 'ivar' in 'IDecl' interface
1218
2.76k
    ObjCInterfaceDecl *ClassDeclared;
1219
2.76k
    Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1220
2.76k
    QualType PropType = property->getType();
1221
2.76k
    QualType PropertyIvarType = PropType.getNonReferenceType();
1222
1223
2.76k
    if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
1224
2.76k
                            diag::err_incomplete_synthesized_property,
1225
4
                            property->getDeclName())) {
1226
4
      Diag(property->getLocation(), diag::note_property_declare);
1227
4
      CompleteTypeErr = true;
1228
4
    }
1229
1230
2.76k
    if (getLangOpts().ObjCAutoRefCount &&
1231
687
        (property->getPropertyAttributesAsWritten() &
1232
687
         ObjCPropertyAttribute::kind_readonly) &&
1233
126
        PropertyIvarType->isObjCRetainableType()) {
1234
112
      setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1235
112
    }
1236
1237
2.76k
    ObjCPropertyAttribute::Kind kind = property->getPropertyAttributes();
1238
1239
2.76k
    bool isARCWeak = false;
1240
2.76k
    if (kind & ObjCPropertyAttribute::kind_weak) {
1241
      // Add GC __weak to the ivar type if the property is weak.
1242
112
      if (getLangOpts().getGC() != LangOptions::NonGC) {
1243
3
        assert(!getLangOpts().ObjCAutoRefCount);
1244
3
        if (PropertyIvarType.isObjCGCStrong()) {
1245
0
          Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1246
0
          Diag(property->getLocation(), diag::note_property_declare);
1247
3
        } else {
1248
3
          PropertyIvarType =
1249
3
            Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1250
3
        }
1251
1252
      // Otherwise, check whether ARC __weak is enabled and works with
1253
      // the property type.
1254
109
      } else {
1255
109
        if (!getLangOpts().ObjCWeak) {
1256
          // Only complain here when synthesizing an ivar.
1257
11
          if (!Ivar) {
1258
4
            Diag(PropertyDiagLoc,
1259
4
                 getLangOpts().ObjCWeakRuntime
1260
2
                   ? diag::err_synthesizing_arc_weak_property_disabled
1261
2
                   : diag::err_synthesizing_arc_weak_property_no_runtime);
1262
4
            Diag(property->getLocation(), diag::note_property_declare);
1263
4
          }
1264
11
          CompleteTypeErr = true; // suppress later diagnostics about the ivar
1265
98
        } else {
1266
98
          isARCWeak = true;
1267
98
          if (const ObjCObjectPointerType *ObjT =
1268
98
                PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1269
98
            const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1270
98
            if (ObjI && 
ObjI->isArcWeakrefUnavailable()67
) {
1271
6
              Diag(property->getLocation(),
1272
6
                   diag::err_arc_weak_unavailable_property)
1273
6
                << PropertyIvarType;
1274
6
              Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1275
6
                << ClassImpDecl->getName();
1276
6
            }
1277
98
          }
1278
98
        }
1279
109
      }
1280
112
    }
1281
1282
2.76k
    if (AtLoc.isInvalid()) {
1283
      // Check when default synthesizing a property that there is
1284
      // an ivar matching property name and issue warning; since this
1285
      // is the most common case of not using an ivar used for backing
1286
      // property in non-default synthesis case.
1287
1.23k
      ObjCInterfaceDecl *ClassDeclared=nullptr;
1288
1.23k
      ObjCIvarDecl *originalIvar =
1289
1.23k
      IDecl->lookupInstanceVariable(property->getIdentifier(),
1290
1.23k
                                    ClassDeclared);
1291
1.23k
      if (originalIvar) {
1292
4
        Diag(PropertyDiagLoc,
1293
4
             diag::warn_autosynthesis_property_ivar_match)
1294
4
        << PropertyId << (Ivar == nullptr) << PropertyIvar
1295
4
        << originalIvar->getIdentifier();
1296
4
        Diag(property->getLocation(), diag::note_property_declare);
1297
4
        Diag(originalIvar->getLocation(), diag::note_ivar_decl);
1298
4
      }
1299
1.23k
    }
1300
1301
2.76k
    if (!Ivar) {
1302
      // In ARC, give the ivar a lifetime qualifier based on the
1303
      // property attributes.
1304
1.98k
      if ((getLangOpts().ObjCAutoRefCount || 
isARCWeak1.48k
) &&
1305
506
          !PropertyIvarType.getObjCLifetime() &&
1306
446
          PropertyIvarType->isObjCRetainableType()) {
1307
1308
        // It's an error if we have to do this and the user didn't
1309
        // explicitly write an ownership attribute on the property.
1310
380
        if (!hasWrittenStorageAttribute(property, QueryKind) &&
1311
60
            !(kind & ObjCPropertyAttribute::kind_strong)) {
1312
0
          Diag(PropertyDiagLoc,
1313
0
               diag::err_arc_objc_property_default_assign_on_object);
1314
0
          Diag(property->getLocation(), diag::note_property_declare);
1315
380
        } else {
1316
380
          Qualifiers::ObjCLifetime lifetime =
1317
380
            getImpliedARCOwnership(kind, PropertyIvarType);
1318
380
          assert(lifetime && "no lifetime for property?");
1319
1320
380
          Qualifiers qs;
1321
380
          qs.addObjCLifetime(lifetime);
1322
380
          PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1323
380
        }
1324
380
      }
1325
1326
1.98k
      Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
1327
1.98k
                                  PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
1328
1.98k
                                  PropertyIvarType, /*TInfo=*/nullptr,
1329
1.98k
                                  ObjCIvarDecl::Private,
1330
1.98k
                                  (Expr *)nullptr, true);
1331
1.98k
      if (RequireNonAbstractType(PropertyIvarLoc,
1332
1.98k
                                 PropertyIvarType,
1333
1.98k
                                 diag::err_abstract_type_in_decl,
1334
1
                                 AbstractSynthesizedIvarType)) {
1335
1
        Diag(property->getLocation(), diag::note_property_declare);
1336
        // An abstract type is as bad as an incomplete type.
1337
1
        CompleteTypeErr = true;
1338
1
      }
1339
1.98k
      if (!CompleteTypeErr) {
1340
1.97k
        const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
1341
1.97k
        if (RecordTy && 
RecordTy->getDecl()->hasFlexibleArrayMember()40
) {
1342
2
          Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
1343
2
            << PropertyIvarType;
1344
2
          CompleteTypeErr = true; // suppress later diagnostics about the ivar
1345
2
        }
1346
1.97k
      }
1347
1.98k
      if (CompleteTypeErr)
1348
11
        Ivar->setInvalidDecl();
1349
1.98k
      ClassImpDecl->addDecl(Ivar);
1350
1.98k
      IDecl->makeDeclVisibleInContext(Ivar);
1351
1352
1.98k
      if (getLangOpts().ObjCRuntime.isFragile())
1353
2
        Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
1354
2
            << PropertyId;
1355
      // Note! I deliberately want it to fall thru so, we have a
1356
      // a property implementation and to avoid future warnings.
1357
779
    } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
1358
747
               !declaresSameEntity(ClassDeclared, IDecl)) {
1359
31
      Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
1360
31
      << property->getDeclName() << Ivar->getDeclName()
1361
31
      << ClassDeclared->getDeclName();
1362
31
      Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1363
31
      << Ivar << Ivar->getName();
1364
      // Note! I deliberately want it to fall thru so more errors are caught.
1365
31
    }
1366
2.76k
    property->setPropertyIvarDecl(Ivar);
1367
1368
2.76k
    QualType IvarType = Context.getCanonicalType(Ivar->getType());
1369
1370
    // Check that type of property and its ivar are type compatible.
1371
2.76k
    if (!Context.hasSameType(PropertyIvarType, IvarType)) {
1372
197
      if (isa<ObjCObjectPointerType>(PropertyIvarType)
1373
103
          && isa<ObjCObjectPointerType>(IvarType))
1374
103
        compat =
1375
103
          Context.canAssignObjCInterfaces(
1376
103
                                  PropertyIvarType->getAs<ObjCObjectPointerType>(),
1377
103
                                  IvarType->getAs<ObjCObjectPointerType>());
1378
94
      else {
1379
94
        compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1380
94
                                             IvarType)
1381
94
                    == Compatible);
1382
94
      }
1383
197
      if (!compat) {
1384
2
        Diag(PropertyDiagLoc, diag::err_property_ivar_type)
1385
2
          << property->getDeclName() << PropType
1386
2
          << Ivar->getDeclName() << IvarType;
1387
2
        Diag(Ivar->getLocation(), diag::note_ivar_decl);
1388
        // Note! I deliberately want it to fall thru so, we have a
1389
        // a property implementation and to avoid future warnings.
1390
2
      }
1391
195
      else {
1392
        // FIXME! Rules for properties are somewhat different that those
1393
        // for assignments. Use a new routine to consolidate all cases;
1394
        // specifically for property redeclarations as well as for ivars.
1395
195
        QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1396
195
        QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1397
195
        if (lhsType != rhsType &&
1398
19
            lhsType->isArithmeticType()) {
1399
1
          Diag(PropertyDiagLoc, diag::err_property_ivar_type)
1400
1
            << property->getDeclName() << PropType
1401
1
            << Ivar->getDeclName() << IvarType;
1402
1
          Diag(Ivar->getLocation(), diag::note_ivar_decl);
1403
          // Fall thru - see previous comment
1404
1
        }
1405
195
      }
1406
      // __weak is explicit. So it works on Canonical type.
1407
197
      if ((PropType.isObjCGCWeak() && 
!IvarType.isObjCGCWeak()2
&&
1408
2
           getLangOpts().getGC() != LangOptions::NonGC)) {
1409
2
        Diag(PropertyDiagLoc, diag::err_weak_property)
1410
2
        << property->getDeclName() << Ivar->getDeclName();
1411
2
        Diag(Ivar->getLocation(), diag::note_ivar_decl);
1412
        // Fall thru - see previous comment
1413
2
      }
1414
      // Fall thru - see previous comment
1415
197
      if ((property->getType()->isObjCObjectPointerType() ||
1416
184
           
PropType.isObjCGCStrong()13
) && IvarType.isObjCGCWeak() &&
1417
4
          getLangOpts().getGC() != LangOptions::NonGC) {
1418
4
        Diag(PropertyDiagLoc, diag::err_strong_property)
1419
4
        << property->getDeclName() << Ivar->getDeclName();
1420
        // Fall thru - see previous comment
1421
4
      }
1422
197
    }
1423
2.76k
    if (getLangOpts().ObjCAutoRefCount || 
isARCWeak2.07k
||
1424
2.06k
        Ivar->getType().getObjCLifetime())
1425
702
      checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
1426
220
  } else if (PropertyIvar)
1427
    // @dynamic
1428
0
    Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
1429
1430
2.98k
  assert (property && "ActOnPropertyImplDecl - property declaration missing");
1431
2.98k
  ObjCPropertyImplDecl *PIDecl =
1432
2.98k
  ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1433
2.98k
                               property,
1434
2.98k
                               (Synthesize ?
1435
2.76k
                                ObjCPropertyImplDecl::Synthesize
1436
220
                                : ObjCPropertyImplDecl::Dynamic),
1437
2.98k
                               Ivar, PropertyIvarLoc);
1438
1439
2.98k
  if (CompleteTypeErr || 
!compat2.96k
)
1440
20
    PIDecl->setInvalidDecl();
1441
1442
2.98k
  if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1443
2.98k
    getterMethod->createImplicitParams(Context, IDecl);
1444
1445
    // Redeclare the getter within the implementation as DeclContext.
1446
2.98k
    if (Synthesize) {
1447
      // If the method hasn't been overridden, create a synthesized implementation.
1448
2.76k
      ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1449
2.76k
          getterMethod->getSelector(), getterMethod->isInstanceMethod());
1450
2.76k
      if (!OMD)
1451
2.70k
        OMD = RedeclarePropertyAccessor(Context, IC, getterMethod, AtLoc,
1452
2.70k
                                        PropertyLoc);
1453
2.76k
      PIDecl->setGetterMethodDecl(OMD);
1454
2.76k
    }
1455
1456
2.98k
    if (getLangOpts().CPlusPlus && 
Synthesize248
&&
!CompleteTypeErr238
&&
1457
235
        Ivar->getType()->isRecordType()) {
1458
      // For Objective-C++, need to synthesize the AST for the IVAR object to be
1459
      // returned by the getter as it must conform to C++'s copy-return rules.
1460
      // FIXME. Eventually we want to do this for Objective-C as well.
1461
35
      SynthesizedFunctionScope Scope(*this, getterMethod);
1462
35
      ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1463
35
      DeclRefExpr *SelfExpr = new (Context)
1464
35
          DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1465
35
                      PropertyDiagLoc);
1466
35
      MarkDeclRefReferenced(SelfExpr);
1467
35
      Expr *LoadSelfExpr = ImplicitCastExpr::Create(
1468
35
          Context, SelfDecl->getType(), CK_LValueToRValue, SelfExpr, nullptr,
1469
35
          VK_RValue, FPOptionsOverride());
1470
35
      Expr *IvarRefExpr =
1471
35
        new (Context) ObjCIvarRefExpr(Ivar,
1472
35
                                      Ivar->getUsageType(SelfDecl->getType()),
1473
35
                                      PropertyDiagLoc,
1474
35
                                      Ivar->getLocation(),
1475
35
                                      LoadSelfExpr, true, true);
1476
35
      ExprResult Res = PerformCopyInitialization(
1477
35
          InitializedEntity::InitializeResult(PropertyDiagLoc,
1478
35
                                              getterMethod->getReturnType(),
1479
35
                                              /*NRVO=*/false),
1480
35
          PropertyDiagLoc, IvarRefExpr);
1481
35
      if (!Res.isInvalid()) {
1482
35
        Expr *ResExpr = Res.getAs<Expr>();
1483
35
        if (ResExpr)
1484
35
          ResExpr = MaybeCreateExprWithCleanups(ResExpr);
1485
35
        PIDecl->setGetterCXXConstructor(ResExpr);
1486
35
      }
1487
35
    }
1488
2.98k
    if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1489
9
        !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1490
3
      Diag(getterMethod->getLocation(),
1491
3
           diag::warn_property_getter_owning_mismatch);
1492
3
      Diag(property->getLocation(), diag::note_property_declare);
1493
3
    }
1494
2.98k
    if (getLangOpts().ObjCAutoRefCount && 
Synthesize709
)
1495
687
      switch (getterMethod->getMethodFamily()) {
1496
1
        case OMF_retain:
1497
1
        case OMF_retainCount:
1498
1
        case OMF_release:
1499
1
        case OMF_autorelease:
1500
1
          Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1501
1
            << 1 << getterMethod->getSelector();
1502
1
          break;
1503
686
        default:
1504
686
          break;
1505
2.98k
      }
1506
2.98k
  }
1507
1508
2.98k
  if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1509
2.59k
    setterMethod->createImplicitParams(Context, IDecl);
1510
1511
    // Redeclare the setter within the implementation as DeclContext.
1512
2.59k
    if (Synthesize) {
1513
2.40k
      ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1514
2.40k
          setterMethod->getSelector(), setterMethod->isInstanceMethod());
1515
2.40k
      if (!OMD)
1516
2.37k
        OMD = RedeclarePropertyAccessor(Context, IC, setterMethod,
1517
2.37k
                                        AtLoc, PropertyLoc);
1518
2.40k
      PIDecl->setSetterMethodDecl(OMD);
1519
2.40k
    }
1520
1521
2.59k
    if (getLangOpts().CPlusPlus && 
Synthesize205
&&
!CompleteTypeErr196
&&
1522
196
        Ivar->getType()->isRecordType()) {
1523
      // FIXME. Eventually we want to do this for Objective-C as well.
1524
35
      SynthesizedFunctionScope Scope(*this, setterMethod);
1525
35
      ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1526
35
      DeclRefExpr *SelfExpr = new (Context)
1527
35
          DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1528
35
                      PropertyDiagLoc);
1529
35
      MarkDeclRefReferenced(SelfExpr);
1530
35
      Expr *LoadSelfExpr = ImplicitCastExpr::Create(
1531
35
          Context, SelfDecl->getType(), CK_LValueToRValue, SelfExpr, nullptr,
1532
35
          VK_RValue, FPOptionsOverride());
1533
35
      Expr *lhs =
1534
35
        new (Context) ObjCIvarRefExpr(Ivar,
1535
35
                                      Ivar->getUsageType(SelfDecl->getType()),
1536
35
                                      PropertyDiagLoc,
1537
35
                                      Ivar->getLocation(),
1538
35
                                      LoadSelfExpr, true, true);
1539
35
      ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1540
35
      ParmVarDecl *Param = (*P);
1541
35
      QualType T = Param->getType().getNonReferenceType();
1542
35
      DeclRefExpr *rhs = new (Context)
1543
35
          DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc);
1544
35
      MarkDeclRefReferenced(rhs);
1545
35
      ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
1546
35
                                  BO_Assign, lhs, rhs);
1547
35
      if (property->getPropertyAttributes() &
1548
25
          ObjCPropertyAttribute::kind_atomic) {
1549
25
        Expr *callExpr = Res.getAs<Expr>();
1550
25
        if (const CXXOperatorCallExpr *CXXCE =
1551
25
              dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1552
25
          if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
1553
25
            if (!FuncDecl->isTrivial())
1554
12
              if (property->getType()->isReferenceType()) {
1555
3
                Diag(PropertyDiagLoc,
1556
3
                     diag::err_atomic_property_nontrivial_assign_op)
1557
3
                    << property->getType();
1558
3
                Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl)
1559
3
                    << FuncDecl;
1560
3
              }
1561
25
      }
1562
35
      PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
1563
35
    }
1564
2.59k
  }
1565
1566
2.98k
  if (IC) {
1567
2.97k
    if (Synthesize)
1568
2.76k
      if (ObjCPropertyImplDecl *PPIDecl =
1569
2
          IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1570
2
        Diag(PropertyLoc, diag::err_duplicate_ivar_use)
1571
2
        << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1572
2
        << PropertyIvar;
1573
2
        Diag(PPIDecl->getLocation(), diag::note_previous_use);
1574
2
      }
1575
1576
2.97k
    if (ObjCPropertyImplDecl *PPIDecl
1577
2
        = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
1578
2
      Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
1579
2
      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1580
2
      return nullptr;
1581
2
    }
1582
2.97k
    IC->addPropertyImplementation(PIDecl);
1583
2.97k
    if (getLangOpts().ObjCDefaultSynthProperties &&
1584
2.97k
        getLangOpts().ObjCRuntime.isNonFragile() &&
1585
2.93k
        !IDecl->isObjCRequiresPropertyDefs()) {
1586
      // Diagnose if an ivar was lazily synthesdized due to a previous
1587
      // use and if 1) property is @dynamic or 2) property is synthesized
1588
      // but it requires an ivar of different name.
1589
2.93k
      ObjCInterfaceDecl *ClassDeclared=nullptr;
1590
2.93k
      ObjCIvarDecl *Ivar = nullptr;
1591
2.93k
      if (!Synthesize)
1592
205
        Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1593
2.72k
      else {
1594
2.72k
        if (PropertyIvar && PropertyIvar != PropertyId)
1595
1.71k
          Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1596
2.72k
      }
1597
      // Issue diagnostics only if Ivar belongs to current class.
1598
2.93k
      if (Ivar && 
Ivar->getSynthesize()35
&&
1599
0
          declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
1600
0
        Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1601
0
        << PropertyId;
1602
0
        Ivar->setInvalidDecl();
1603
0
      }
1604
2.93k
    }
1605
5
  } else {
1606
5
    if (Synthesize)
1607
0
      if (ObjCPropertyImplDecl *PPIDecl =
1608
0
          CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1609
0
        Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
1610
0
        << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1611
0
        << PropertyIvar;
1612
0
        Diag(PPIDecl->getLocation(), diag::note_previous_use);
1613
0
      }
1614
1615
5
    if (ObjCPropertyImplDecl *PPIDecl =
1616
0
        CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
1617
0
      Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
1618
0
      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1619
0
      return nullptr;
1620
0
    }
1621
5
    CatImplClass->addPropertyImplementation(PIDecl);
1622
5
  }
1623
1624
2.98k
  if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic &&
1625
219
      PIDecl->getPropertyDecl() &&
1626
219
      PIDecl->getPropertyDecl()->isDirectProperty()) {
1627
3
    Diag(PropertyLoc, diag::err_objc_direct_dynamic_property);
1628
3
    Diag(PIDecl->getPropertyDecl()->getLocation(),
1629
3
         diag::note_previous_declaration);
1630
3
    return nullptr;
1631
3
  }
1632
1633
2.97k
  return PIDecl;
1634
2.97k
}
1635
1636
//===----------------------------------------------------------------------===//
1637
// Helper methods.
1638
//===----------------------------------------------------------------------===//
1639
1640
/// DiagnosePropertyMismatch - Compares two properties for their
1641
/// attributes and types and warns on a variety of inconsistencies.
1642
///
1643
void
1644
Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1645
                               ObjCPropertyDecl *SuperProperty,
1646
                               const IdentifierInfo *inheritedName,
1647
5.47k
                               bool OverridingProtocolProperty) {
1648
5.47k
  ObjCPropertyAttribute::Kind CAttr = Property->getPropertyAttributes();
1649
5.47k
  ObjCPropertyAttribute::Kind SAttr = SuperProperty->getPropertyAttributes();
1650
1651
  // We allow readonly properties without an explicit ownership
1652
  // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1653
  // to be overridden by a property with any explicit ownership in the subclass.
1654
5.47k
  if (!OverridingProtocolProperty &&
1655
3.64k
      !getOwnershipRule(SAttr) && 
getOwnershipRule(CAttr)2.77k
)
1656
2.14k
    ;
1657
3.33k
  else {
1658
3.33k
    if ((CAttr & ObjCPropertyAttribute::kind_readonly) &&
1659
2.42k
        (SAttr & ObjCPropertyAttribute::kind_readwrite))
1660
1
      Diag(Property->getLocation(), diag::warn_readonly_property)
1661
1
        << Property->getDeclName() << inheritedName;
1662
3.33k
    if ((CAttr & ObjCPropertyAttribute::kind_copy) !=
1663
3.33k
        (SAttr & ObjCPropertyAttribute::kind_copy))
1664
5
      Diag(Property->getLocation(), diag::warn_property_attribute)
1665
5
        << Property->getDeclName() << "copy" << inheritedName;
1666
3.32k
    else if (!(SAttr & ObjCPropertyAttribute::kind_readonly)) {
1667
214
      unsigned CAttrRetain = (CAttr & (ObjCPropertyAttribute::kind_retain |
1668
214
                                       ObjCPropertyAttribute::kind_strong));
1669
214
      unsigned SAttrRetain = (SAttr & (ObjCPropertyAttribute::kind_retain |
1670
214
                                       ObjCPropertyAttribute::kind_strong));
1671
214
      bool CStrong = (CAttrRetain != 0);
1672
214
      bool SStrong = (SAttrRetain != 0);
1673
214
      if (CStrong != SStrong)
1674
0
        Diag(Property->getLocation(), diag::warn_property_attribute)
1675
0
          << Property->getDeclName() << "retain (or strong)" << inheritedName;
1676
214
    }
1677
3.33k
  }
1678
1679
  // Check for nonatomic; note that nonatomic is effectively
1680
  // meaningless for readonly properties, so don't diagnose if the
1681
  // atomic property is 'readonly'.
1682
5.47k
  checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
1683
  // Readonly properties from protocols can be implemented as "readwrite"
1684
  // with a custom setter name.
1685
5.47k
  if (Property->getSetterName() != SuperProperty->getSetterName() &&
1686
2
      !(SuperProperty->isReadOnly() &&
1687
2
        isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
1688
1
    Diag(Property->getLocation(), diag::warn_property_attribute)
1689
1
      << Property->getDeclName() << "setter" << inheritedName;
1690
1
    Diag(SuperProperty->getLocation(), diag::note_property_declare);
1691
1
  }
1692
5.47k
  if (Property->getGetterName() != SuperProperty->getGetterName()) {
1693
0
    Diag(Property->getLocation(), diag::warn_property_attribute)
1694
0
      << Property->getDeclName() << "getter" << inheritedName;
1695
0
    Diag(SuperProperty->getLocation(), diag::note_property_declare);
1696
0
  }
1697
1698
5.47k
  QualType LHSType =
1699
5.47k
    Context.getCanonicalType(SuperProperty->getType());
1700
5.47k
  QualType RHSType =
1701
5.47k
    Context.getCanonicalType(Property->getType());
1702
1703
5.47k
  if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
1704
    // Do cases not handled in above.
1705
    // FIXME. For future support of covariant property types, revisit this.
1706
37
    bool IncompatibleObjC = false;
1707
37
    QualType ConvertedType;
1708
37
    if (!isObjCPointerConversion(RHSType, LHSType,
1709
37
                                 ConvertedType, IncompatibleObjC) ||
1710
26
        
IncompatibleObjC15
) {
1711
26
        Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1712
26
        << Property->getType() << SuperProperty->getType() << inheritedName;
1713
26
      Diag(SuperProperty->getLocation(), diag::note_property_declare);
1714
26
    }
1715
37
  }
1716
5.47k
}
1717
1718
bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1719
                                            ObjCMethodDecl *GetterMethod,
1720
462k
                                            SourceLocation Loc) {
1721
462k
  if (!GetterMethod)
1722
461k
    return false;
1723
1.71k
  QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
1724
1.71k
  QualType PropertyRValueType =
1725
1.71k
      property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1726
1.71k
  bool compat = Context.hasSameType(PropertyRValueType, GetterType);
1727
1.71k
  if (!compat) {
1728
30
    const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1729
30
    const ObjCObjectPointerType *getterObjCPtr = nullptr;
1730
30
    if ((propertyObjCPtr =
1731
30
             PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
1732
24
        (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1733
24
      compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
1734
6
    else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
1735
2
              != Compatible) {
1736
2
          Diag(Loc, diag::err_property_accessor_type)
1737
2
            << property->getDeclName() << PropertyRValueType
1738
2
            << GetterMethod->getSelector() << GetterType;
1739
2
          Diag(GetterMethod->getLocation(), diag::note_declared_at);
1740
2
          return true;
1741
4
    } else {
1742
4
      compat = true;
1743
4
      QualType lhsType = Context.getCanonicalType(PropertyRValueType);
1744
4
      QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1745
4
      if (lhsType != rhsType && 
lhsType->isArithmeticType()2
)
1746
2
        compat = false;
1747
4
    }
1748
30
  }
1749
1750
1.70k
  if (!compat) {
1751
5
    Diag(Loc, diag::warn_accessor_property_type_mismatch)
1752
5
    << property->getDeclName()
1753
5
    << GetterMethod->getSelector();
1754
5
    Diag(GetterMethod->getLocation(), diag::note_declared_at);
1755
5
    return true;
1756
5
  }
1757
1758
1.70k
  return false;
1759
1.70k
}
1760
1761
/// CollectImmediateProperties - This routine collects all properties in
1762
/// the class and its conforming protocols; but not those in its super class.
1763
static void
1764
CollectImmediateProperties(ObjCContainerDecl *CDecl,
1765
                           ObjCContainerDecl::PropertyMap &PropMap,
1766
                           ObjCContainerDecl::PropertyMap &SuperPropMap,
1767
                           bool CollectClassPropsOnly = false,
1768
6.60k
                           bool IncludeProtocols = true) {
1769
6.60k
  if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1770
2.75k
    for (auto *Prop : IDecl->properties()) {
1771
2.75k
      if (CollectClassPropsOnly && 
!Prop->isClassProperty()2.69k
)
1772
2.65k
        continue;
1773
106
      PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1774
106
          Prop;
1775
106
    }
1776
1777
    // Collect the properties from visible extensions.
1778
4.80k
    for (auto *Ext : IDecl->visible_extensions())
1779
450
      CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1780
450
                                 CollectClassPropsOnly, IncludeProtocols);
1781
1782
4.80k
    if (IncludeProtocols) {
1783
      // Scan through class's protocols.
1784
4.79k
      for (auto *PI : IDecl->all_referenced_protocols())
1785
543
        CollectImmediateProperties(PI, PropMap, SuperPropMap,
1786
543
                                   CollectClassPropsOnly);
1787
4.79k
    }
1788
4.80k
  }
1789
6.60k
  if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1790
252
    for (auto *Prop : CATDecl->properties()) {
1791
252
      if (CollectClassPropsOnly && 
!Prop->isClassProperty()230
)
1792
230
        continue;
1793
22
      PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1794
22
          Prop;
1795
22
    }
1796
956
    if (IncludeProtocols) {
1797
      // Scan through class's protocols.
1798
956
      for (auto *PI : CATDecl->protocols())
1799
121
        CollectImmediateProperties(PI, PropMap, SuperPropMap,
1800
121
                                   CollectClassPropsOnly);
1801
956
    }
1802
956
  }
1803
5.65k
  else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1804
247
    for (auto *Prop : PDecl->properties()) {
1805
247
      if (CollectClassPropsOnly && 
!Prop->isClassProperty()220
)
1806
195
        continue;
1807
52
      ObjCPropertyDecl *PropertyFromSuper =
1808
52
          SuperPropMap[std::make_pair(Prop->getIdentifier(),
1809
52
                                      Prop->isClassProperty())];
1810
      // Exclude property for protocols which conform to class's super-class,
1811
      // as super-class has to implement the property.
1812
52
      if (!PropertyFromSuper ||
1813
43
          
PropertyFromSuper->getIdentifier() != Prop->getIdentifier()9
) {
1814
43
        ObjCPropertyDecl *&PropEntry =
1815
43
            PropMap[std::make_pair(Prop->getIdentifier(),
1816
43
                                   Prop->isClassProperty())];
1817
43
        if (!PropEntry)
1818
43
          PropEntry = Prop;
1819
43
      }
1820
52
    }
1821
    // Scan through protocol's protocols.
1822
851
    for (auto *PI : PDecl->protocols())
1823
187
      CollectImmediateProperties(PI, PropMap, SuperPropMap,
1824
187
                                 CollectClassPropsOnly);
1825
851
  }
1826
6.60k
}
1827
1828
/// CollectSuperClassPropertyImplementations - This routine collects list of
1829
/// properties to be implemented in super class(s) and also coming from their
1830
/// conforming protocols.
1831
static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1832
6.72k
                                    ObjCInterfaceDecl::PropertyMap &PropMap) {
1833
6.72k
  if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1834
3.12k
    ObjCInterfaceDecl::PropertyDeclOrder PO;
1835
6.77k
    while (SDecl) {
1836
3.65k
      SDecl->collectPropertiesToImplement(PropMap, PO);
1837
3.65k
      SDecl = SDecl->getSuperClass();
1838
3.65k
    }
1839
3.12k
  }
1840
6.72k
}
1841
1842
/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1843
/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1844
/// declared in class 'IFace'.
1845
bool
1846
Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1847
1.97k
                                     ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1848
1.97k
  if (!IV->getSynthesize())
1849
1.68k
    return false;
1850
291
  ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1851
291
                                            Method->isInstanceMethod());
1852
291
  if (!IMD || 
!IMD->isPropertyAccessor()83
)
1853
237
    return false;
1854
1855
  // look up a property declaration whose one of its accessors is implemented
1856
  // by this method.
1857
156
  
for (const auto *Property : IFace->instance_properties())54
{
1858
156
    if ((Property->getGetterName() == IMD->getSelector() ||
1859
139
         Property->getSetterName() == IMD->getSelector()) &&
1860
44
        (Property->getPropertyIvarDecl() == IV))
1861
33
      return true;
1862
156
  }
1863
  // Also look up property declaration in class extension whose one of its
1864
  // accessors is implemented by this method.
1865
21
  for (const auto *Ext : IFace->known_extensions())
1866
2
    for (const auto *Property : Ext->instance_properties())
1867
2
      if ((Property->getGetterName() == IMD->getSelector() ||
1868
1
           Property->getSetterName() == IMD->getSelector()) &&
1869
1
          (Property->getPropertyIvarDecl() == IV))
1870
1
        return true;
1871
20
  return false;
1872
21
}
1873
1874
static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1875
34
                                         ObjCPropertyDecl *Prop) {
1876
34
  bool SuperClassImplementsGetter = false;
1877
34
  bool SuperClassImplementsSetter = false;
1878
34
  if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly)
1879
15
    SuperClassImplementsSetter = true;
1880
1881
58
  while (IDecl->getSuperClass()) {
1882
37
    ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1883
37
    if (!SuperClassImplementsGetter && 
SDecl->getInstanceMethod(Prop->getGetterName())35
)
1884
13
      SuperClassImplementsGetter = true;
1885
1886
37
    if (!SuperClassImplementsSetter && 
SDecl->getInstanceMethod(Prop->getSetterName())18
)
1887
9
      SuperClassImplementsSetter = true;
1888
37
    if (SuperClassImplementsGetter && 
SuperClassImplementsSetter15
)
1889
13
      return true;
1890
24
    IDecl = IDecl->getSuperClass();
1891
24
  }
1892
21
  return false;
1893
34
}
1894
1895
/// Default synthesizes all properties which must be synthesized
1896
/// in class's \@implementation.
1897
void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1898
                                       ObjCInterfaceDecl *IDecl,
1899
4.55k
                                       SourceLocation AtEnd) {
1900
4.55k
  ObjCInterfaceDecl::PropertyMap PropMap;
1901
4.55k
  ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1902
4.55k
  IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
1903
4.55k
  if (PropMap.empty())
1904
3.12k
    return;
1905
1.42k
  ObjCInterfaceDecl::PropertyMap SuperPropMap;
1906
1.42k
  CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1907
1908
4.56k
  for (unsigned i = 0, e = PropertyOrder.size(); i != e; 
i++3.14k
) {
1909
3.14k
    ObjCPropertyDecl *Prop = PropertyOrder[i];
1910
    // Is there a matching property synthesize/dynamic?
1911
3.14k
    if (Prop->isInvalidDecl() ||
1912
3.07k
        Prop->isClassProperty() ||
1913
2.99k
        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1914
161
      continue;
1915
    // Property may have been synthesized by user.
1916
2.98k
    if (IMPDecl->FindPropertyImplDecl(
1917
2.98k
            Prop->getIdentifier(), Prop->getQueryKind()))
1918
1.60k
      continue;
1919
1.37k
    ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Prop->getGetterName());
1920
1.37k
    if (ImpMethod && 
!ImpMethod->getBody()135
) {
1921
135
      if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly)
1922
40
        continue;
1923
95
      ImpMethod = IMPDecl->getInstanceMethod(Prop->getSetterName());
1924
95
      if (ImpMethod && 
!ImpMethod->getBody()43
)
1925
43
        continue;
1926
1.29k
    }
1927
1.29k
    if (ObjCPropertyImplDecl *PID =
1928
2
        IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1929
2
      Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1930
2
        << Prop->getIdentifier();
1931
2
      if (PID->getLocation().isValid())
1932
2
        Diag(PID->getLocation(), diag::note_property_synthesize);
1933
2
      continue;
1934
2
    }
1935
1.29k
    ObjCPropertyDecl *PropInSuperClass =
1936
1.29k
        SuperPropMap[std::make_pair(Prop->getIdentifier(),
1937
1.29k
                                    Prop->isClassProperty())];
1938
1.29k
    if (ObjCProtocolDecl *Proto =
1939
34
          dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
1940
      // We won't auto-synthesize properties declared in protocols.
1941
      // Suppress the warning if class's superclass implements property's
1942
      // getter and implements property's setter (if readwrite property).
1943
      // Or, if property is going to be implemented in its super class.
1944
34
      if (!SuperClassImplementsProperty(IDecl, Prop) && 
!PropInSuperClass21
) {
1945
15
        Diag(IMPDecl->getLocation(),
1946
15
             diag::warn_auto_synthesizing_protocol_property)
1947
15
          << Prop << Proto;
1948
15
        Diag(Prop->getLocation(), diag::note_property_declare);
1949
15
        std::string FixIt =
1950
15
            (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1951
15
        Diag(AtEnd, diag::note_add_synthesize_directive)
1952
15
            << FixItHint::CreateInsertion(AtEnd, FixIt);
1953
15
      }
1954
34
      continue;
1955
34
    }
1956
    // If property to be implemented in the super class, ignore.
1957
1.26k
    if (PropInSuperClass) {
1958
26
      if ((Prop->getPropertyAttributes() &
1959
26
           ObjCPropertyAttribute::kind_readwrite) &&
1960
24
          (PropInSuperClass->getPropertyAttributes() &
1961
24
           ObjCPropertyAttribute::kind_readonly) &&
1962
11
          !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1963
9
          !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1964
3
        Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1965
3
        << Prop->getIdentifier();
1966
3
        Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1967
23
      } else {
1968
23
        Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1969
23
        << Prop->getIdentifier();
1970
23
        Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1971
23
        Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1972
23
      }
1973
26
      continue;
1974
26
    }
1975
    // We use invalid SourceLocations for the synthesized ivars since they
1976
    // aren't really synthesized at a particular location; they just exist.
1977
    // Saying that they are located at the @implementation isn't really going
1978
    // to help users.
1979
1.23k
    ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1980
1.23k
      ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1981
1.23k
                            true,
1982
1.23k
                            /* property = */ Prop->getIdentifier(),
1983
1.23k
                            /* ivar = */ Prop->getDefaultSynthIvarName(Context),
1984
1.23k
                            Prop->getLocation(), Prop->getQueryKind()));
1985
1.23k
    if (PIDecl && !Prop->isUnavailable()) {
1986
1.22k
      Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
1987
1.22k
      Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1988
1.22k
    }
1989
1.23k
  }
1990
1.42k
}
1991
1992
void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1993
5.31k
                                       SourceLocation AtEnd) {
1994
5.31k
  if (!LangOpts.ObjCDefaultSynthProperties || 
LangOpts.ObjCRuntime.isFragile()5.18k
)
1995
368
    return;
1996
4.94k
  ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1997
4.94k
  if (!IC)
1998
386
    return;
1999
4.55k
  if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
2000
4.55k
    if (!IDecl->isObjCRequiresPropertyDefs())
2001
4.55k
      DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
2002
4.55k
}
2003
2004
static void DiagnoseUnimplementedAccessor(
2005
    Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
2006
    ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
2007
    ObjCPropertyDecl *Prop,
2008
163
    llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
2009
  // Check to see if we have a corresponding selector in SMap and with the
2010
  // right method type.
2011
324
  auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) {
2012
324
    return x->getSelector() == Method &&
2013
46
           x->isClassMethod() == Prop->isClassProperty();
2014
324
  });
2015
  // When reporting on missing property setter/getter implementation in
2016
  // categories, do not report when they are declared in primary class,
2017
  // class's protocol, or one of it super classes. This is because,
2018
  // the class is going to implement them.
2019
163
  if (I == SMap.end() &&
2020
121
      (PrimaryClass == nullptr ||
2021
16
       !PrimaryClass->lookupPropertyAccessor(Method, C,
2022
112
                                             Prop->isClassProperty()))) {
2023
112
    unsigned diag =
2024
112
        isa<ObjCCategoryDecl>(CDecl)
2025
7
            ? (Prop->isClassProperty()
2026
0
                   ? diag::warn_impl_required_in_category_for_class_property
2027
7
                   : diag::warn_setter_getter_impl_required_in_category)
2028
105
            : (Prop->isClassProperty()
2029
78
                   ? diag::warn_impl_required_for_class_property
2030
27
                   : diag::warn_setter_getter_impl_required);
2031
112
    S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
2032
112
    S.Diag(Prop->getLocation(), diag::note_property_declare);
2033
112
    if (S.LangOpts.ObjCDefaultSynthProperties &&
2034
104
        S.LangOpts.ObjCRuntime.isNonFragile())
2035
96
      if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
2036
89
        if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
2037
10
          S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
2038
112
  }
2039
163
}
2040
2041
void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
2042
                                           ObjCContainerDecl *CDecl,
2043
5.29k
                                           bool SynthesizeProperties) {
2044
5.29k
  ObjCContainerDecl::PropertyMap PropMap;
2045
5.29k
  ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
2046
2047
  // Since we don't synthesize class properties, we should emit diagnose even
2048
  // if SynthesizeProperties is true.
2049
5.29k
  ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2050
  // Gather properties which need not be implemented in this class
2051
  // or category.
2052
5.29k
  if (!IDecl)
2053
506
    if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
2054
      // For categories, no need to implement properties declared in
2055
      // its primary class (and its super classes) if property is
2056
      // declared in one of those containers.
2057
506
      if ((IDecl = C->getClassInterface())) {
2058
506
        ObjCInterfaceDecl::PropertyDeclOrder PO;
2059
506
        IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
2060
506
      }
2061
506
    }
2062
5.29k
  if (IDecl)
2063
5.29k
    CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
2064
2065
  // When SynthesizeProperties is true, we only check class properties.
2066
5.29k
  CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2067
5.29k
                             SynthesizeProperties/*CollectClassPropsOnly*/);
2068
2069
  // Scan the @interface to see if any of the protocols it adopts
2070
  // require an explicit implementation, via attribute
2071
  // 'objc_protocol_requires_explicit_implementation'.
2072
5.29k
  if (IDecl) {
2073
5.29k
    std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
2074
2075
649
    for (auto *PDecl : IDecl->all_referenced_protocols()) {
2076
649
      if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2077
640
        continue;
2078
      // Lazily construct a set of all the properties in the @interface
2079
      // of the class, without looking at the superclass.  We cannot
2080
      // use the call to CollectImmediateProperties() above as that
2081
      // utilizes information from the super class's properties as well
2082
      // as scans the adopted protocols.  This work only triggers for protocols
2083
      // with the attribute, which is very rare, and only occurs when
2084
      // analyzing the @implementation.
2085
9
      if (!LazyMap) {
2086
9
        ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2087
9
        LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2088
9
        CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
2089
9
                                   /* CollectClassPropsOnly */ false,
2090
9
                                   /* IncludeProtocols */ false);
2091
9
      }
2092
      // Add the properties of 'PDecl' to the list of properties that
2093
      // need to be implemented.
2094
3
      for (auto *PropDecl : PDecl->properties()) {
2095
3
        if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2096
3
                                      PropDecl->isClassProperty())])
2097
1
          continue;
2098
2
        PropMap[std::make_pair(PropDecl->getIdentifier(),
2099
2
                               PropDecl->isClassProperty())] = PropDecl;
2100
2
      }
2101
9
    }
2102
5.29k
  }
2103
2104
5.29k
  if (PropMap.empty())
2105
5.19k
    return;
2106
2107
102
  llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
2108
102
  for (const auto *I : IMPDecl->property_impls())
2109
78
    PropImplMap.insert(I->getPropertyDecl());
2110
2111
102
  llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
2112
  // Collect property accessors implemented in current implementation.
2113
102
  for (const auto *I : IMPDecl->methods())
2114
228
    InsMap.insert(I);
2115
2116
102
  ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
2117
102
  ObjCInterfaceDecl *PrimaryClass = nullptr;
2118
102
  if (C && 
!C->IsClassExtension()23
)
2119
23
    if ((PrimaryClass = C->getClassInterface()))
2120
      // Report unimplemented properties in the category as well.
2121
23
      if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2122
        // When reporting on missing setter/getters, do not report when
2123
        // setter/getter is implemented in category's primary class
2124
        // implementation.
2125
7
        for (const auto *I : IMP->methods())
2126
29
          InsMap.insert(I);
2127
7
      }
2128
2129
102
  for (ObjCContainerDecl::PropertyMap::iterator
2130
272
       P = PropMap.begin(), E = PropMap.end(); P != E; 
++P170
) {
2131
170
    ObjCPropertyDecl *Prop = P->second;
2132
    // Is there a matching property synthesize/dynamic?
2133
170
    if (Prop->isInvalidDecl() ||
2134
170
        Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
2135
169
        PropImplMap.count(Prop) ||
2136
110
        Prop->getAvailability() == AR_Unavailable)
2137
60
      continue;
2138
2139
    // Diagnose unimplemented getters and setters.
2140
110
    DiagnoseUnimplementedAccessor(*this,
2141
110
          PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2142
110
    if (!Prop->isReadOnly())
2143
53
      DiagnoseUnimplementedAccessor(*this,
2144
53
                                    PrimaryClass, Prop->getSetterName(),
2145
53
                                    IMPDecl, CDecl, C, Prop, InsMap);
2146
110
  }
2147
102
}
2148
2149
5.29k
void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
2150
2.98k
  for (const auto *propertyImpl : impDecl->property_impls()) {
2151
2.98k
    const auto *property = propertyImpl->getPropertyDecl();
2152
    // Warn about null_resettable properties with synthesized setters,
2153
    // because the setter won't properly handle nil.
2154
2.98k
    if (propertyImpl->getPropertyImplementation() ==
2155
2.98k
            ObjCPropertyImplDecl::Synthesize &&
2156
2.76k
        (property->getPropertyAttributes() &
2157
2.76k
         ObjCPropertyAttribute::kind_null_resettable) &&
2158
5
        property->getGetterMethodDecl() && property->getSetterMethodDecl()) {
2159
5
      auto *getterImpl = propertyImpl->getGetterMethodDecl();
2160
5
      auto *setterImpl = propertyImpl->getSetterMethodDecl();
2161
5
      if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) &&
2162
4
          (!setterImpl || setterImpl->isSynthesizedAccessorStub())) {
2163
2
        SourceLocation loc = propertyImpl->getLocation();
2164
2
        if (loc.isInvalid())
2165
1
          loc = impDecl->getBeginLoc();
2166
2167
2
        Diag(loc, diag::warn_null_resettable_setter)
2168
2
          << setterImpl->getSelector() << property->getDeclName();
2169
2
      }
2170
5
    }
2171
2.98k
  }
2172
5.29k
}
2173
2174
void
2175
Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
2176
4.79k
                                       ObjCInterfaceDecl* IDecl) {
2177
  // Rules apply in non-GC mode only
2178
4.79k
  if (getLangOpts().getGC() != LangOptions::NonGC)
2179
69
    return;
2180
4.72k
  ObjCContainerDecl::PropertyMap PM;
2181
4.72k
  for (auto *Prop : IDecl->properties())
2182
2.71k
    PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
2183
4.72k
  for (const auto *Ext : IDecl->known_extensions())
2184
448
    for (auto *Prop : Ext->properties())
2185
226
      PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
2186
2187
4.72k
  for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2188
7.59k
       I != E; 
++I2.87k
) {
2189
2.87k
    const ObjCPropertyDecl *Property = I->second;
2190
2.87k
    ObjCMethodDecl *GetterMethod = nullptr;
2191
2.87k
    ObjCMethodDecl *SetterMethod = nullptr;
2192
2193
2.87k
    unsigned Attributes = Property->getPropertyAttributes();
2194
2.87k
    unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
2195
2196
2.87k
    if (!(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic) &&
2197
2.85k
        !(AttributesAsWritten & ObjCPropertyAttribute::kind_nonatomic)) {
2198
2.33k
      GetterMethod = Property->isClassProperty() ?
2199
48
                     IMPDecl->getClassMethod(Property->getGetterName()) :
2200
2.28k
                     IMPDecl->getInstanceMethod(Property->getGetterName());
2201
2.33k
      SetterMethod = Property->isClassProperty() ?
2202
48
                     IMPDecl->getClassMethod(Property->getSetterName()) :
2203
2.28k
                     IMPDecl->getInstanceMethod(Property->getSetterName());
2204
2.33k
      if (GetterMethod && 
GetterMethod->isSynthesizedAccessorStub()2.10k
)
2205
1.97k
        GetterMethod = nullptr;
2206
2.33k
      if (SetterMethod && 
SetterMethod->isSynthesizedAccessorStub()1.87k
)
2207
1.79k
        SetterMethod = nullptr;
2208
2.33k
      if (GetterMethod) {
2209
129
        Diag(GetterMethod->getLocation(),
2210
129
             diag::warn_default_atomic_custom_getter_setter)
2211
129
          << Property->getIdentifier() << 0;
2212
129
        Diag(Property->getLocation(), diag::note_property_declare);
2213
129
      }
2214
2.33k
      if (SetterMethod) {
2215
86
        Diag(SetterMethod->getLocation(),
2216
86
             diag::warn_default_atomic_custom_getter_setter)
2217
86
          << Property->getIdentifier() << 1;
2218
86
        Diag(Property->getLocation(), diag::note_property_declare);
2219
86
      }
2220
2.33k
    }
2221
2222
    // We only care about readwrite atomic property.
2223
2.87k
    if ((Attributes & ObjCPropertyAttribute::kind_nonatomic) ||
2224
2.35k
        !(Attributes & ObjCPropertyAttribute::kind_readwrite))
2225
772
      continue;
2226
2.10k
    if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2227
1.99k
            Property->getIdentifier(), Property->getQueryKind())) {
2228
1.99k
      if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2229
162
        continue;
2230
1.83k
      GetterMethod = PIDecl->getGetterMethodDecl();
2231
1.83k
      SetterMethod = PIDecl->getSetterMethodDecl();
2232
1.83k
      if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2233
1.80k
        GetterMethod = nullptr;
2234
1.83k
      if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2235
1.81k
        SetterMethod = nullptr;
2236
1.83k
      if ((bool)GetterMethod ^ (bool)SetterMethod) {
2237
20
        SourceLocation MethodLoc =
2238
12
          (GetterMethod ? GetterMethod->getLocation()
2239
8
                        : SetterMethod->getLocation());
2240
20
        Diag(MethodLoc, diag::warn_atomic_property_rule)
2241
20
          << Property->getIdentifier() << (GetterMethod != nullptr)
2242
20
          << (SetterMethod != nullptr);
2243
        // fixit stuff.
2244
20
        if (Property->getLParenLoc().isValid() &&
2245
13
            !(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic)) {
2246
          // @property () ... case.
2247
11
          SourceLocation AfterLParen =
2248
11
            getLocForEndOfToken(Property->getLParenLoc());
2249
10
          StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2250
1
                                                      : "nonatomic";
2251
11
          Diag(Property->getLocation(),
2252
11
               diag::note_atomic_property_fixup_suggest)
2253
11
            << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2254
9
        } else if (Property->getLParenLoc().isInvalid()) {
2255
          //@property id etc.
2256
7
          SourceLocation startLoc =
2257
7
            Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2258
7
          Diag(Property->getLocation(),
2259
7
               diag::note_atomic_property_fixup_suggest)
2260
7
            << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
2261
7
        } else
2262
2
          Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
2263
20
        Diag(Property->getLocation(), diag::note_property_declare);
2264
20
      }
2265
1.83k
    }
2266
2.10k
  }
2267
4.72k
}
2268
2269
4.79k
void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
2270
4.79k
  if (getLangOpts().getGC() == LangOptions::GCOnly)
2271
6
    return;
2272
2273
4.78k
  for (const auto *PID : D->property_impls()) {
2274
2.97k
    const ObjCPropertyDecl *PD = PID->getPropertyDecl();
2275
2.97k
    if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
2276
2.96k
        !PD->isClassProperty()) {
2277
2.95k
      ObjCMethodDecl *IM = PID->getGetterMethodDecl();
2278
2.95k
      if (IM && 
!IM->isSynthesizedAccessorStub()2.75k
)
2279
104
        continue;
2280
2.85k
      ObjCMethodDecl *method = PD->getGetterMethodDecl();
2281
2.85k
      if (!method)
2282
0
        continue;
2283
2.85k
      ObjCMethodFamily family = method->getMethodFamily();
2284
2.85k
      if (family == OMF_alloc || 
family == OMF_copy2.85k
||
2285
2.85k
          family == OMF_mutableCopy || family == OMF_new) {
2286
11
        if (getLangOpts().ObjCAutoRefCount)
2287
11
          Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
2288
0
        else
2289
0
          Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
2290
2291
        // Look for a getter explicitly declared alongside the property.
2292
        // If we find one, use its location for the note.
2293
11
        SourceLocation noteLoc = PD->getLocation();
2294
11
        SourceLocation fixItLoc;
2295
19
        for (auto *getterRedecl : method->redecls()) {
2296
19
          if (getterRedecl->isImplicit())
2297
15
            continue;
2298
4
          if (getterRedecl->getDeclContext() != PD->getDeclContext())
2299
2
            continue;
2300
2
          noteLoc = getterRedecl->getLocation();
2301
2
          fixItLoc = getterRedecl->getEndLoc();
2302
2
        }
2303
2304
11
        Preprocessor &PP = getPreprocessor();
2305
11
        TokenValue tokens[] = {
2306
11
          tok::kw___attribute, tok::l_paren, tok::l_paren,
2307
11
          PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2308
11
          PP.getIdentifierInfo("none"), tok::r_paren,
2309
11
          tok::r_paren, tok::r_paren
2310
11
        };
2311
11
        StringRef spelling = "__attribute__((objc_method_family(none)))";
2312
11
        StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2313
11
        if (!macroName.empty())
2314
2
          spelling = macroName;
2315
2316
11
        auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2317
11
            << method->getDeclName() << spelling;
2318
11
        if (fixItLoc.isValid()) {
2319
2
          SmallString<64> fixItText(" ");
2320
2
          fixItText += spelling;
2321
2
          noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2322
2
        }
2323
11
      }
2324
2.85k
    }
2325
2.97k
  }
2326
4.78k
}
2327
2328
void Sema::DiagnoseMissingDesignatedInitOverrides(
2329
                                            const ObjCImplementationDecl *ImplD,
2330
14
                                            const ObjCInterfaceDecl *IFD) {
2331
14
  assert(IFD->hasDesignatedInitializers());
2332
14
  const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2333
14
  if (!SuperD)
2334
3
    return;
2335
2336
11
  SelectorSet InitSelSet;
2337
11
  for (const auto *I : ImplD->instance_methods())
2338
22
    if (I->getMethodFamily() == OMF_init)
2339
22
      InitSelSet.insert(I->getSelector());
2340
2341
11
  SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2342
11
  SuperD->getDesignatedInitializers(DesignatedInits);
2343
11
  for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2344
22
         I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; 
++I11
) {
2345
11
    const ObjCMethodDecl *MD = *I;
2346
11
    if (!InitSelSet.count(MD->getSelector())) {
2347
      // Don't emit a diagnostic if the overriding method in the subclass is
2348
      // marked as unavailable.
2349
7
      bool Ignore = false;
2350
7
      if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2351
1
        Ignore = IMD->isUnavailable();
2352
6
      } else {
2353
        // Check the methods declared in the class extensions too.
2354
6
        for (auto *Ext : IFD->visible_extensions())
2355
1
          if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) {
2356
1
            Ignore = IMD->isUnavailable();
2357
1
            break;
2358
1
          }
2359
6
      }
2360
7
      if (!Ignore) {
2361
5
        Diag(ImplD->getLocation(),
2362
5
             diag::warn_objc_implementation_missing_designated_init_override)
2363
5
          << MD->getSelector();
2364
5
        Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2365
5
      }
2366
7
    }
2367
11
  }
2368
11
}
2369
2370
/// AddPropertyAttrs - Propagates attributes from a property to the
2371
/// implicitly-declared getter or setter for that property.
2372
static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2373
614k
                             ObjCPropertyDecl *Property) {
2374
  // Should we just clone all attributes over?
2375
565k
  for (const auto *A : Property->attrs()) {
2376
565k
    if (isa<DeprecatedAttr>(A) ||
2377
565k
        isa<UnavailableAttr>(A) ||
2378
565k
        isa<AvailabilityAttr>(A))
2379
559k
      PropertyMethod->addAttr(A->clone(S.Context));
2380
565k
  }
2381
614k
}
2382
2383
/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2384
/// have the property type and issue diagnostics if they don't.
2385
/// Also synthesize a getter/setter method if none exist (and update the
2386
/// appropriate lookup tables.
2387
461k
void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
2388
461k
  ObjCMethodDecl *GetterMethod, *SetterMethod;
2389
461k
  ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
2390
461k
  if (CD->isInvalidDecl())
2391
2
    return;
2392
2393
461k
  bool IsClassProperty = property->isClassProperty();
2394
461k
  GetterMethod = IsClassProperty ?
2395
86.2k
    CD->getClassMethod(property->getGetterName()) :
2396
375k
    CD->getInstanceMethod(property->getGetterName());
2397
2398
  // if setter or getter is not found in class extension, it might be
2399
  // in the primary class.
2400
461k
  if (!GetterMethod)
2401
461k
    if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2402
63.7k
      if (CatDecl->IsClassExtension())
2403
658
        GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2404
1
                         getClassMethod(property->getGetterName()) :
2405
657
                       CatDecl->getClassInterface()->
2406
657
                         getInstanceMethod(property->getGetterName());
2407
2408
461k
  SetterMethod = IsClassProperty ?
2409
86.2k
                 CD->getClassMethod(property->getSetterName()) :
2410
375k
                 CD->getInstanceMethod(property->getSetterName());
2411
461k
  if (!SetterMethod)
2412
461k
    if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2413
64.0k
      if (CatDecl->IsClassExtension())
2414
658
        SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2415
1
                          getClassMethod(property->getSetterName()) :
2416
657
                       CatDecl->getClassInterface()->
2417
657
                          getInstanceMethod(property->getSetterName());
2418
461k
  DiagnosePropertyAccessorMismatch(property, GetterMethod,
2419
461k
                                   property->getLocation());
2420
2421
  // synthesizing accessors must not result in a direct method that is not
2422
  // monomorphic
2423
461k
  if (!GetterMethod) {
2424
461k
    if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) {
2425
63.6k
      auto *ExistingGetter = CatDecl->getClassInterface()->lookupMethod(
2426
63.6k
          property->getGetterName(), !IsClassProperty, true, false, CatDecl);
2427
63.6k
      if (ExistingGetter) {
2428
134
        if (ExistingGetter->isDirectMethod() || 
property->isDirectProperty()128
) {
2429
9
          Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl)
2430
9
              << property->isDirectProperty() << 1 /* property */
2431
9
              << ExistingGetter->isDirectMethod()
2432
9
              << ExistingGetter->getDeclName();
2433
9
          Diag(ExistingGetter->getLocation(), diag::note_previous_declaration);
2434
9
        }
2435
134
      }
2436
63.6k
    }
2437
461k
  }
2438
2439
461k
  if (!property->isReadOnly() && 
!SetterMethod153k
) {
2440
152k
    if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) {
2441
7.92k
      auto *ExistingSetter = CatDecl->getClassInterface()->lookupMethod(
2442
7.92k
          property->getSetterName(), !IsClassProperty, true, false, CatDecl);
2443
7.92k
      if (ExistingSetter) {
2444
25
        if (ExistingSetter->isDirectMethod() || property->isDirectProperty()) {
2445
0
          Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl)
2446
0
              << property->isDirectProperty() << 1 /* property */
2447
0
              << ExistingSetter->isDirectMethod()
2448
0
              << ExistingSetter->getDeclName();
2449
0
          Diag(ExistingSetter->getLocation(), diag::note_previous_declaration);
2450
0
        }
2451
25
      }
2452
7.92k
    }
2453
152k
  }
2454
2455
461k
  if (!property->isReadOnly() && 
SetterMethod153k
) {
2456
103
    if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2457
103
        Context.VoidTy)
2458
0
      Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2459
103
    if (SetterMethod->param_size() != 1 ||
2460
103
        !Context.hasSameUnqualifiedType(
2461
103
          (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2462
0
          property->getType().getNonReferenceType())) {
2463
0
      Diag(property->getLocation(),
2464
0
           diag::warn_accessor_property_type_mismatch)
2465
0
        << property->getDeclName()
2466
0
        << SetterMethod->getSelector();
2467
0
      Diag(SetterMethod->getLocation(), diag::note_declared_at);
2468
0
    }
2469
103
  }
2470
2471
  // Synthesize getter/setter methods if none exist.
2472
  // Find the default getter and if one not found, add one.
2473
  // FIXME: The synthesized property we set here is misleading. We almost always
2474
  // synthesize these methods unless the user explicitly provided prototypes
2475
  // (which is odd, but allowed). Sema should be typechecking that the
2476
  // declarations jive in that situation (which it is not currently).
2477
461k
  if (!GetterMethod) {
2478
    // No instance/class method of same name as property getter name was found.
2479
    // Declare a getter method and add it to the list of methods
2480
    // for this class.
2481
461k
    SourceLocation Loc = property->getLocation();
2482
2483
    // The getter returns the declared property type with all qualifiers
2484
    // removed.
2485
461k
    QualType resultTy = property->getType().getAtomicUnqualifiedType();
2486
2487
    // If the property is null_resettable, the getter returns nonnull.
2488
461k
    if (property->getPropertyAttributes() &
2489
18.2k
        ObjCPropertyAttribute::kind_null_resettable) {
2490
18.2k
      QualType modifiedTy = resultTy;
2491
18.2k
      if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
2492
18.2k
        if (*nullability == NullabilityKind::Unspecified)
2493
18.2k
          resultTy = Context.getAttributedType(attr::TypeNonNull,
2494
18.2k
                                               modifiedTy, modifiedTy);
2495
18.2k
      }
2496
18.2k
    }
2497
2498
461k
    GetterMethod = ObjCMethodDecl::Create(
2499
461k
        Context, Loc, Loc, property->getGetterName(), resultTy, nullptr, CD,
2500
461k
        !IsClassProperty, /*isVariadic=*/false,
2501
461k
        /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
2502
461k
        /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
2503
461k
        (property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2504
1.28k
            ? ObjCMethodDecl::Optional
2505
459k
            : ObjCMethodDecl::Required);
2506
461k
    CD->addDecl(GetterMethod);
2507
2508
461k
    AddPropertyAttrs(*this, GetterMethod, property);
2509
2510
461k
    if (property->isDirectProperty())
2511
42
      GetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2512
2513
461k
    if (property->hasAttr<NSReturnsNotRetainedAttr>())
2514
3
      GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2515
3
                                                                     Loc));
2516
2517
461k
    if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2518
2.98k
      GetterMethod->addAttr(
2519
2.98k
        ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
2520
2521
461k
    if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2522
2
      GetterMethod->addAttr(SectionAttr::CreateImplicit(
2523
2
          Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2524
2
          SectionAttr::GNU_section));
2525
2526
461k
    if (getLangOpts().ObjCAutoRefCount)
2527
7.31k
      CheckARCMethodDecl(GetterMethod);
2528
461k
  } else
2529
    // A user declared getter will be synthesize when @synthesize of
2530
    // the property with the same name is seen in the @implementation
2531
544
    GetterMethod->setPropertyAccessor(true);
2532
2533
461k
  GetterMethod->createImplicitParams(Context,
2534
461k
                                     GetterMethod->getClassInterface());
2535
461k
  property->setGetterMethodDecl(GetterMethod);
2536
2537
  // Skip setter if property is read-only.
2538
461k
  if (!property->isReadOnly()) {
2539
    // Find the default setter and if one not found, add one.
2540
153k
    if (!SetterMethod) {
2541
      // No instance/class method of same name as property setter name was
2542
      // found.
2543
      // Declare a setter method and add it to the list of methods
2544
      // for this class.
2545
152k
      SourceLocation Loc = property->getLocation();
2546
2547
152k
      SetterMethod =
2548
152k
        ObjCMethodDecl::Create(Context, Loc, Loc,
2549
152k
                               property->getSetterName(), Context.VoidTy,
2550
152k
                               nullptr, CD, !IsClassProperty,
2551
152k
                               /*isVariadic=*/false,
2552
152k
                               /*isPropertyAccessor=*/true,
2553
152k
                               /*isSynthesizedAccessorStub=*/false,
2554
152k
                               /*isImplicitlyDeclared=*/true,
2555
152k
                               /*isDefined=*/false,
2556
152k
                               (property->getPropertyImplementation() ==
2557
152k
                                ObjCPropertyDecl::Optional) ?
2558
94
                                ObjCMethodDecl::Optional :
2559
152k
                                ObjCMethodDecl::Required);
2560
2561
      // Remove all qualifiers from the setter's parameter type.
2562
152k
      QualType paramTy =
2563
152k
          property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2564
2565
      // If the property is null_resettable, the setter accepts a
2566
      // nullable value.
2567
152k
      if (property->getPropertyAttributes() &
2568
18.2k
          ObjCPropertyAttribute::kind_null_resettable) {
2569
18.2k
        QualType modifiedTy = paramTy;
2570
18.2k
        if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2571
18.2k
          if (*nullability == NullabilityKind::Unspecified)
2572
18.2k
            paramTy = Context.getAttributedType(attr::TypeNullable,
2573
18.2k
                                                modifiedTy, modifiedTy);
2574
18.2k
        }
2575
18.2k
      }
2576
2577
      // Invent the arguments for the setter. We don't bother making a
2578
      // nice name for the argument.
2579
152k
      ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2580
152k
                                                  Loc, Loc,
2581
152k
                                                  property->getIdentifier(),
2582
152k
                                                  paramTy,
2583
152k
                                                  /*TInfo=*/nullptr,
2584
152k
                                                  SC_None,
2585
152k
                                                  nullptr);
2586
152k
      SetterMethod->setMethodParams(Context, Argument, None);
2587
2588
152k
      AddPropertyAttrs(*this, SetterMethod, property);
2589
2590
152k
      if (property->isDirectProperty())
2591
16
        SetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2592
2593
152k
      CD->addDecl(SetterMethod);
2594
152k
      if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2595
2
        SetterMethod->addAttr(SectionAttr::CreateImplicit(
2596
2
            Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2597
2
            SectionAttr::GNU_section));
2598
      // It's possible for the user to have set a very odd custom
2599
      // setter selector that causes it to have a method family.
2600
152k
      if (getLangOpts().ObjCAutoRefCount)
2601
4.12k
        CheckARCMethodDecl(SetterMethod);
2602
152k
    } else
2603
      // A user declared setter will be synthesize when @synthesize of
2604
      // the property with the same name is seen in the @implementation
2605
103
      SetterMethod->setPropertyAccessor(true);
2606
2607
153k
    SetterMethod->createImplicitParams(Context,
2608
153k
                                       SetterMethod->getClassInterface());
2609
153k
    property->setSetterMethodDecl(SetterMethod);
2610
153k
  }
2611
  // Add any synthesized methods to the global pool. This allows us to
2612
  // handle the following, which is supported by GCC (and part of the design).
2613
  //
2614
  // @interface Foo
2615
  // @property double bar;
2616
  // @end
2617
  //
2618
  // void thisIsUnfortunate() {
2619
  //   id foo;
2620
  //   double bar = [foo bar];
2621
  // }
2622
  //
2623
461k
  if (!IsClassProperty) {
2624
375k
    if (GetterMethod)
2625
375k
      AddInstanceMethodToGlobalPool(GetterMethod);
2626
375k
    if (SetterMethod)
2627
151k
      AddInstanceMethodToGlobalPool(SetterMethod);
2628
86.2k
  } else {
2629
86.2k
    if (GetterMethod)
2630
86.2k
      AddFactoryMethodToGlobalPool(GetterMethod);
2631
86.2k
    if (SetterMethod)
2632
1.63k
      AddFactoryMethodToGlobalPool(SetterMethod);
2633
86.2k
  }
2634
2635
461k
  ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2636
461k
  if (!CurrentClass) {
2637
70.4k
    if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2638
64.0k
      CurrentClass = Cat->getClassInterface();
2639
6.36k
    else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2640
0
      CurrentClass = Impl->getClassInterface();
2641
70.4k
  }
2642
461k
  if (GetterMethod)
2643
461k
    CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2644
461k
  if (SetterMethod)
2645
153k
    CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
2646
461k
}
2647
2648
void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
2649
                                       SourceLocation Loc,
2650
                                       unsigned &Attributes,
2651
461k
                                       bool propertyInPrimaryClass) {
2652
  // FIXME: Improve the reported location.
2653
461k
  if (!PDecl || PDecl->isInvalidDecl())
2654
7
    return;
2655
2656
461k
  if ((Attributes & ObjCPropertyAttribute::kind_readonly) &&
2657
308k
      (Attributes & ObjCPropertyAttribute::kind_readwrite))
2658
4
    Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2659
4
    << "readonly" << "readwrite";
2660
2661
461k
  ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2662
461k
  QualType PropertyTy = PropertyDecl->getType();
2663
2664
  // Check for copy or retain on non-object types.
2665
461k
  if ((Attributes &
2666
461k
       (ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy |
2667
461k
        ObjCPropertyAttribute::kind_retain |
2668
461k
        ObjCPropertyAttribute::kind_strong)) &&
2669
300k
      !PropertyTy->isObjCRetainableType() &&
2670
13
      !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
2671
11
    Diag(Loc, diag::err_objc_property_requires_object)
2672
11
        << (Attributes & ObjCPropertyAttribute::kind_weak
2673
1
                ? "weak"
2674
10
                : Attributes & ObjCPropertyAttribute::kind_copy
2675
3
                      ? "copy"
2676
7
                      : "retain (or strong)");
2677
11
    Attributes &=
2678
11
        ~(ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy |
2679
11
          ObjCPropertyAttribute::kind_retain |
2680
11
          ObjCPropertyAttribute::kind_strong);
2681
11
    PropertyDecl->setInvalidDecl();
2682
11
  }
2683
2684
  // Check for assign on object types.
2685
461k
  if ((Attributes & ObjCPropertyAttribute::kind_assign) &&
2686
7.64k
      !(Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) &&
2687
7.64k
      PropertyTy->isObjCRetainableType() &&
2688
5.44k
      !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2689
5.42k
    Diag(Loc, diag::warn_objc_property_assign_on_object);
2690
5.42k
  }
2691
2692
  // Check for more than one of { assign, copy, retain }.
2693
461k
  if (Attributes & ObjCPropertyAttribute::kind_assign) {
2694
7.64k
    if (Attributes & ObjCPropertyAttribute::kind_copy) {
2695
4
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2696
4
        << "assign" << "copy";
2697
4
      Attributes &= ~ObjCPropertyAttribute::kind_copy;
2698
4
    }
2699
7.64k
    if (Attributes & ObjCPropertyAttribute::kind_retain) {
2700
5
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2701
5
        << "assign" << "retain";
2702
5
      Attributes &= ~ObjCPropertyAttribute::kind_retain;
2703
5
    }
2704
7.64k
    if (Attributes & ObjCPropertyAttribute::kind_strong) {
2705
2
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2706
2
        << "assign" << "strong";
2707
2
      Attributes &= ~ObjCPropertyAttribute::kind_strong;
2708
2
    }
2709
7.64k
    if (getLangOpts().ObjCAutoRefCount &&
2710
412
        (Attributes & ObjCPropertyAttribute::kind_weak)) {
2711
2
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2712
2
        << "assign" << "weak";
2713
2
      Attributes &= ~ObjCPropertyAttribute::kind_weak;
2714
2
    }
2715
7.64k
    if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
2716
2
      Diag(Loc, diag::warn_iboutletcollection_property_assign);
2717
453k
  } else if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) {
2718
44
    if (Attributes & ObjCPropertyAttribute::kind_copy) {
2719
3
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2720
3
        << "unsafe_unretained" << "copy";
2721
3
      Attributes &= ~ObjCPropertyAttribute::kind_copy;
2722
3
    }
2723
44
    if (Attributes & ObjCPropertyAttribute::kind_retain) {
2724
2
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2725
2
        << "unsafe_unretained" << "retain";
2726
2
      Attributes &= ~ObjCPropertyAttribute::kind_retain;
2727
2
    }
2728
44
    if (Attributes & ObjCPropertyAttribute::kind_strong) {
2729
2
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2730
2
        << "unsafe_unretained" << "strong";
2731
2
      Attributes &= ~ObjCPropertyAttribute::kind_strong;
2732
2
    }
2733
44
    if (getLangOpts().ObjCAutoRefCount &&
2734
31
        (Attributes & ObjCPropertyAttribute::kind_weak)) {
2735
0
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2736
0
        << "unsafe_unretained" << "weak";
2737
0
      Attributes &= ~ObjCPropertyAttribute::kind_weak;
2738
0
    }
2739
453k
  } else if (Attributes & ObjCPropertyAttribute::kind_copy) {
2740
257k
    if (Attributes & ObjCPropertyAttribute::kind_retain) {
2741
4
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2742
4
        << "copy" << "retain";
2743
4
      Attributes &= ~ObjCPropertyAttribute::kind_retain;
2744
4
    }
2745
257k
    if (Attributes & ObjCPropertyAttribute::kind_strong) {
2746
1
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2747
1
        << "copy" << "strong";
2748
1
      Attributes &= ~ObjCPropertyAttribute::kind_strong;
2749
1
    }
2750
257k
    if (Attributes & ObjCPropertyAttribute::kind_weak) {
2751
1
      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2752
1
        << "copy" << "weak";
2753
1
      Attributes &= ~ObjCPropertyAttribute::kind_weak;
2754
1
    }
2755
196k
  } else if ((Attributes & ObjCPropertyAttribute::kind_retain) &&
2756
26.2k
             (Attributes & ObjCPropertyAttribute::kind_weak)) {
2757
3
    Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "retain"
2758
3
                                                               << "weak";
2759
3
    Attributes &= ~ObjCPropertyAttribute::kind_retain;
2760
196k
  } else if ((Attributes & ObjCPropertyAttribute::kind_strong) &&
2761
15.6k
             (Attributes & ObjCPropertyAttribute::kind_weak)) {
2762
0
    Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "strong"
2763
0
                                                               << "weak";
2764
0
    Attributes &= ~ObjCPropertyAttribute::kind_weak;
2765
0
  }
2766
2767
461k
  if (Attributes & ObjCPropertyAttribute::kind_weak) {
2768
    // 'weak' and 'nonnull' are mutually exclusive.
2769
1.32k
    if (auto nullability = PropertyTy->getNullability(Context)) {
2770
1.16k
      if (*nullability == NullabilityKind::NonNull)
2771
3
        Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2772
3
          << "nonnull" << "weak";
2773
1.16k
    }
2774
1.32k
  }
2775
2776
461k
  if ((Attributes & ObjCPropertyAttribute::kind_atomic) &&
2777
3.35k
      (Attributes & ObjCPropertyAttribute::kind_nonatomic)) {
2778
6
    Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) << "atomic"
2779
6
                                                               << "nonatomic";
2780
6
    Attributes &= ~ObjCPropertyAttribute::kind_atomic;
2781
6
  }
2782
2783
  // Warn if user supplied no assignment attribute, property is
2784
  // readwrite, and this is an object type.
2785
461k
  if (!getOwnershipRule(Attributes) && 
PropertyTy->isObjCRetainableType()153k
) {
2786
6.09k
    if (Attributes & ObjCPropertyAttribute::kind_readonly) {
2787
      // do nothing
2788
366
    } else if (getLangOpts().ObjCAutoRefCount) {
2789
      // With arc, @property definitions should default to strong when
2790
      // not specified.
2791
56
      PropertyDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong);
2792
310
    } else if (PropertyTy->isObjCObjectPointerType()) {
2793
274
      bool isAnyClassTy = (PropertyTy->isObjCClassType() ||
2794
264
                           PropertyTy->isObjCQualifiedClassType());
2795
      // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2796
      // issue any warning.
2797
274
      if (isAnyClassTy && 
getLangOpts().getGC() == LangOptions::NonGC12
)
2798
12
        ;
2799
262
      else if (propertyInPrimaryClass) {
2800
        // Don't issue warning on property with no life time in class
2801
        // extension as it is inherited from property in primary class.
2802
        // Skip this warning in gc-only mode.
2803
247
        if (getLangOpts().getGC() != LangOptions::GCOnly)
2804
247
          Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2805
2806
        // If non-gc code warn that this is likely inappropriate.
2807
247
        if (getLangOpts().getGC() == LangOptions::NonGC)
2808
238
          Diag(Loc, diag::warn_objc_property_default_assign_on_object);
2809
247
      }
2810
274
    }
2811
2812
    // FIXME: Implement warning dependent on NSCopying being
2813
    // implemented. See also:
2814
    // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2815
    // (please trim this list while you are at it).
2816
6.09k
  }
2817
2818
461k
  if (!(Attributes & ObjCPropertyAttribute::kind_copy) &&
2819
204k
      !(Attributes & ObjCPropertyAttribute::kind_readonly) &&
2820
85.1k
      getLangOpts().getGC() == LangOptions::GCOnly &&
2821
4
      PropertyTy->isBlockPointerType())
2822
2
    Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
2823
461k
  else if ((Attributes & ObjCPropertyAttribute::kind_retain) &&
2824
26.2k
           !(Attributes & ObjCPropertyAttribute::kind_readonly) &&
2825
10.2k
           !(Attributes & ObjCPropertyAttribute::kind_strong) &&
2826
10.2k
           PropertyTy->isBlockPointerType())
2827
3
    Diag(Loc, diag::warn_objc_property_retain_of_block);
2828
2829
461k
  if ((Attributes & ObjCPropertyAttribute::kind_readonly) &&
2830
308k
      (Attributes & ObjCPropertyAttribute::kind_setter))
2831
1
    Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2832
461k
}