Coverage Report

Created: 2020-02-25 14:32

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