This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
SemaObjCProperty.cpp
2711 lines (2483 loc) · 113 KB
/
SemaObjCProperty.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for Objective C @property and
// @synthesize declarations.
//
//===----------------------------------------------------------------------===//
#include "clang/Sema/SemaInternal.h"
#include "clang/AST/ASTMutationListener.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Initialization.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallString.h"
using namespace clang;
//===----------------------------------------------------------------------===//
// Grammar actions.
//===----------------------------------------------------------------------===//
/// getImpliedARCOwnership - Given a set of property attributes and a
/// type, infer an expected lifetime. The type's ownership qualification
/// is not considered.
///
/// Returns OCL_None if the attributes as stated do not imply an ownership.
/// Never returns OCL_Autoreleasing.
static Qualifiers::ObjCLifetime getImpliedARCOwnership(
ObjCPropertyDecl::PropertyAttributeKind attrs,
QualType type) {
// retain, strong, copy, weak, and unsafe_unretained are only legal
// on properties of retainable pointer type.
if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
ObjCPropertyDecl::OBJC_PR_strong |
ObjCPropertyDecl::OBJC_PR_copy)) {
return Qualifiers::OCL_Strong;
} else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
return Qualifiers::OCL_Weak;
} else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
return Qualifiers::OCL_ExplicitNone;
}
// assign can appear on other types, so we have to check the
// property type.
if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
type->isObjCRetainableType()) {
return Qualifiers::OCL_ExplicitNone;
}
return Qualifiers::OCL_None;
}
/// Check the internal consistency of a property declaration with
/// an explicit ownership qualifier.
static void checkPropertyDeclWithOwnership(Sema &S,
ObjCPropertyDecl *property) {
if (property->isInvalidDecl()) return;
ObjCPropertyDecl::PropertyAttributeKind propertyKind
= property->getPropertyAttributes();
Qualifiers::ObjCLifetime propertyLifetime
= property->getType().getObjCLifetime();
assert(propertyLifetime != Qualifiers::OCL_None);
Qualifiers::ObjCLifetime expectedLifetime
= getImpliedARCOwnership(propertyKind, property->getType());
if (!expectedLifetime) {
// We have a lifetime qualifier but no dominating property
// attribute. That's okay, but restore reasonable invariants by
// setting the property attribute according to the lifetime
// qualifier.
ObjCPropertyDecl::PropertyAttributeKind attr;
if (propertyLifetime == Qualifiers::OCL_Strong) {
attr = ObjCPropertyDecl::OBJC_PR_strong;
} else if (propertyLifetime == Qualifiers::OCL_Weak) {
attr = ObjCPropertyDecl::OBJC_PR_weak;
} else {
assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
}
property->setPropertyAttributes(attr);
return;
}
if (propertyLifetime == expectedLifetime) return;
property->setInvalidDecl();
S.Diag(property->getLocation(),
diag::err_arc_inconsistent_property_ownership)
<< property->getDeclName()
<< expectedLifetime
<< propertyLifetime;
}
/// Check this Objective-C property against a property declared in the
/// given protocol.
static void
CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
ObjCProtocolDecl *Proto,
llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
// Have we seen this protocol before?
if (!Known.insert(Proto).second)
return;
// Look for a property with the same name.
DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
for (unsigned I = 0, N = R.size(); I != N; ++I) {
if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
return;
}
}
// Check this property against any protocols we inherit.
for (auto *P : Proto->protocols())
CheckPropertyAgainstProtocol(S, Prop, P, Known);
}
static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
// In GC mode, just look for the __weak qualifier.
if (S.getLangOpts().getGC() != LangOptions::NonGC) {
if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
// In ARC/MRC, look for an explicit ownership qualifier.
// For some reason, this only applies to __weak.
} else if (auto ownership = T.getObjCLifetime()) {
switch (ownership) {
case Qualifiers::OCL_Weak:
return ObjCDeclSpec::DQ_PR_weak;
case Qualifiers::OCL_Strong:
return ObjCDeclSpec::DQ_PR_strong;
case Qualifiers::OCL_ExplicitNone:
return ObjCDeclSpec::DQ_PR_unsafe_unretained;
case Qualifiers::OCL_Autoreleasing:
case Qualifiers::OCL_None:
return 0;
}
llvm_unreachable("bad qualifier");
}
return 0;
}
static const unsigned OwnershipMask =
(ObjCPropertyDecl::OBJC_PR_assign |
ObjCPropertyDecl::OBJC_PR_retain |
ObjCPropertyDecl::OBJC_PR_copy |
ObjCPropertyDecl::OBJC_PR_weak |
ObjCPropertyDecl::OBJC_PR_strong |
ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
static unsigned getOwnershipRule(unsigned attr) {
unsigned result = attr & OwnershipMask;
// From an ownership perspective, assign and unsafe_unretained are
// identical; make sure one also implies the other.
if (result & (ObjCPropertyDecl::OBJC_PR_assign |
ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
result |= ObjCPropertyDecl::OBJC_PR_assign |
ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
}
return result;
}
Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
SourceLocation LParenLoc,
FieldDeclarator &FD,
ObjCDeclSpec &ODS,
Selector GetterSel,
Selector SetterSel,
tok::ObjCKeywordKind MethodImplKind,
DeclContext *lexicalDC) {
unsigned Attributes = ODS.getPropertyAttributes();
FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
QualType T = TSI->getType();
if (!getOwnershipRule(Attributes)) {
Attributes |= deducePropertyOwnershipFromType(*this, T);
}
bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
// default is readwrite!
!(Attributes & ObjCDeclSpec::DQ_PR_readonly));
// Proceed with constructing the ObjCPropertyDecls.
ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
ObjCPropertyDecl *Res = nullptr;
if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
if (CDecl->IsClassExtension()) {
Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
FD,
GetterSel, ODS.getGetterNameLoc(),
SetterSel, ODS.getSetterNameLoc(),
isReadWrite, Attributes,
ODS.getPropertyAttributes(),
T, TSI, MethodImplKind);
if (!Res)
return nullptr;
}
}
if (!Res) {
Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
GetterSel, ODS.getGetterNameLoc(), SetterSel,
ODS.getSetterNameLoc(), isReadWrite, Attributes,
ODS.getPropertyAttributes(), T, TSI,
MethodImplKind);
if (lexicalDC)
Res->setLexicalDeclContext(lexicalDC);
}
// Validate the attributes on the @property.
CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
(isa<ObjCInterfaceDecl>(ClassDecl) ||
isa<ObjCProtocolDecl>(ClassDecl)));
// Check consistency if the type has explicit ownership qualification.
if (Res->getType().getObjCLifetime())
checkPropertyDeclWithOwnership(*this, Res);
llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
// For a class, compare the property against a property in our superclass.
bool FoundInSuper = false;
ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
for (unsigned I = 0, N = R.size(); I != N; ++I) {
if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
FoundInSuper = true;
break;
}
}
if (FoundInSuper)
break;
else
CurrentInterfaceDecl = Super;
}
if (FoundInSuper) {
// Also compare the property against a property in our protocols.
for (auto *P : CurrentInterfaceDecl->protocols()) {
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
}
} else {
// Slower path: look in all protocols we referenced.
for (auto *P : IFace->all_referenced_protocols()) {
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
}
}
} else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
// We don't check if class extension. Because properties in class extension
// are meant to override some of the attributes and checking has already done
// when property in class extension is constructed.
if (!Cat->IsClassExtension())
for (auto *P : Cat->protocols())
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
} else {
ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
for (auto *P : Proto->protocols())
CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
}
ActOnDocumentableDecl(Res);
return Res;
}
static ObjCPropertyDecl::PropertyAttributeKind
makePropertyAttributesAsWritten(unsigned Attributes) {
unsigned attributesAsWritten = 0;
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
if (Attributes & ObjCDeclSpec::DQ_PR_assign)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
if (Attributes & ObjCDeclSpec::DQ_PR_class)
attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
}
static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
SourceLocation LParenLoc, SourceLocation &Loc) {
if (LParenLoc.isMacroID())
return false;
SourceManager &SM = Context.getSourceManager();
std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
// Try to load the file buffer.
bool invalidTemp = false;
StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
if (invalidTemp)
return false;
const char *tokenBegin = file.data() + locInfo.second;
// Lex from the start of the given location.
Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
Context.getLangOpts(),
file.begin(), tokenBegin, file.end());
Token Tok;
do {
lexer.LexFromRawLexer(Tok);
if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Loc = Tok.getLocation();
return true;
}
} while (Tok.isNot(tok::r_paren));
return false;
}
/// Check for a mismatch in the atomicity of the given properties.
static void checkAtomicPropertyMismatch(Sema &S,
ObjCPropertyDecl *OldProperty,
ObjCPropertyDecl *NewProperty,
bool PropagateAtomicity) {
// If the atomicity of both matches, we're done.
bool OldIsAtomic =
(OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
== 0;
bool NewIsAtomic =
(NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
== 0;
if (OldIsAtomic == NewIsAtomic) return;
// Determine whether the given property is readonly and implicitly
// atomic.
auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
// Is it readonly?
auto Attrs = Property->getPropertyAttributes();
if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
// Is it nonatomic?
if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
// Was 'atomic' specified directly?
if (Property->getPropertyAttributesAsWritten() &
ObjCPropertyDecl::OBJC_PR_atomic)
return false;
return true;
};
// If we're allowed to propagate atomicity, and the new property did
// not specify atomicity at all, propagate.
const unsigned AtomicityMask =
(ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
if (PropagateAtomicity &&
((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
unsigned Attrs = NewProperty->getPropertyAttributes();
Attrs = Attrs & ~AtomicityMask;
if (OldIsAtomic)
Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
else
Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
NewProperty->overwritePropertyAttributes(Attrs);
return;
}
// One of the properties is atomic; if it's a readonly property, and
// 'atomic' wasn't explicitly specified, we're okay.
if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
(NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
return;
// Diagnose the conflict.
const IdentifierInfo *OldContextName;
auto *OldDC = OldProperty->getDeclContext();
if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
OldContextName = Category->getClassInterface()->getIdentifier();
else
OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
<< NewProperty->getDeclName() << "atomic"
<< OldContextName;
S.Diag(OldProperty->getLocation(), diag::note_property_declare);
}
ObjCPropertyDecl *
Sema::HandlePropertyInClassExtension(Scope *S,
SourceLocation AtLoc,
SourceLocation LParenLoc,
FieldDeclarator &FD,
Selector GetterSel,
SourceLocation GetterNameLoc,
Selector SetterSel,
SourceLocation SetterNameLoc,
const bool isReadWrite,
unsigned &Attributes,
const unsigned AttributesAsWritten,
QualType T,
TypeSourceInfo *TSI,
tok::ObjCKeywordKind MethodImplKind) {
ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
// Diagnose if this property is already in continuation class.
DeclContext *DC = CurContext;
IdentifierInfo *PropertyId = FD.D.getIdentifier();
ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
// We need to look in the @interface to see if the @property was
// already declared.
if (!CCPrimary) {
Diag(CDecl->getLocation(), diag::err_continuation_class);
return nullptr;
}
bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
(Attributes & ObjCDeclSpec::DQ_PR_class);
// Find the property in the extended class's primary class or
// extensions.
ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
// If we found a property in an extension, complain.
if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
Diag(AtLoc, diag::err_duplicate_property);
Diag(PIDecl->getLocation(), diag::note_property_declare);
return nullptr;
}
// Check for consistency with the previous declaration, if there is one.
if (PIDecl) {
// A readonly property declared in the primary class can be refined
// by adding a readwrite property within an extension.
// Anything else is an error.
if (!(PIDecl->isReadOnly() && isReadWrite)) {
// Tailor the diagnostics for the common case where a readwrite
// property is declared both in the @interface and the continuation.
// This is a common error where the user often intended the original
// declaration to be readonly.
unsigned diag =
(Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
(PIDecl->getPropertyAttributesAsWritten() &
ObjCPropertyDecl::OBJC_PR_readwrite)
? diag::err_use_continuation_class_redeclaration_readwrite
: diag::err_use_continuation_class;
Diag(AtLoc, diag)
<< CCPrimary->getDeclName();
Diag(PIDecl->getLocation(), diag::note_property_declare);
return nullptr;
}
// Check for consistency of getters.
if (PIDecl->getGetterName() != GetterSel) {
// If the getter was written explicitly, complain.
if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
<< PIDecl->getGetterName() << GetterSel;
Diag(PIDecl->getLocation(), diag::note_property_declare);
}
// Always adopt the getter from the original declaration.
GetterSel = PIDecl->getGetterName();
Attributes |= ObjCDeclSpec::DQ_PR_getter;
}
// Check consistency of ownership.
unsigned ExistingOwnership
= getOwnershipRule(PIDecl->getPropertyAttributes());
unsigned NewOwnership = getOwnershipRule(Attributes);
if (ExistingOwnership && NewOwnership != ExistingOwnership) {
// If the ownership was written explicitly, complain.
if (getOwnershipRule(AttributesAsWritten)) {
Diag(AtLoc, diag::warn_property_attr_mismatch);
Diag(PIDecl->getLocation(), diag::note_property_declare);
}
// Take the ownership from the original property.
Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
}
// If the redeclaration is 'weak' but the original property is not,
if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
!(PIDecl->getPropertyAttributesAsWritten()
& ObjCPropertyDecl::OBJC_PR_weak) &&
PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
Diag(AtLoc, diag::warn_property_implicitly_mismatched);
Diag(PIDecl->getLocation(), diag::note_property_declare);
}
}
// Create a new ObjCPropertyDecl with the DeclContext being
// the class extension.
ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
FD, GetterSel, GetterNameLoc,
SetterSel, SetterNameLoc,
isReadWrite,
Attributes, AttributesAsWritten,
T, TSI, MethodImplKind, DC);
// If there was no declaration of a property with the same name in
// the primary class, we're done.
if (!PIDecl) {
ProcessPropertyDecl(PDecl);
return PDecl;
}
if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
bool IncompatibleObjC = false;
QualType ConvertedType;
// Relax the strict type matching for property type in continuation class.
// Allow property object type of continuation class to be different as long
// as it narrows the object type in its primary class property. Note that
// this conversion is safe only because the wider type is for a 'readonly'
// property in primary class and 'narrowed' type for a 'readwrite' property
// in continuation class.
QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
!isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
(!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
ConvertedType, IncompatibleObjC))
|| IncompatibleObjC) {
Diag(AtLoc,
diag::err_type_mismatch_continuation_class) << PDecl->getType();
Diag(PIDecl->getLocation(), diag::note_property_declare);
return nullptr;
}
}
// Check that atomicity of property in class extension matches the previous
// declaration.
checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
// Make sure getter/setter are appropriately synthesized.
ProcessPropertyDecl(PDecl);
return PDecl;
}
ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
ObjCContainerDecl *CDecl,
SourceLocation AtLoc,
SourceLocation LParenLoc,
FieldDeclarator &FD,
Selector GetterSel,
SourceLocation GetterNameLoc,
Selector SetterSel,
SourceLocation SetterNameLoc,
const bool isReadWrite,
const unsigned Attributes,
const unsigned AttributesAsWritten,
QualType T,
TypeSourceInfo *TInfo,
tok::ObjCKeywordKind MethodImplKind,
DeclContext *lexicalDC){
IdentifierInfo *PropertyId = FD.D.getIdentifier();
// Property defaults to 'assign' if it is readwrite, unless this is ARC
// and the type is retainable.
bool isAssign;
if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
isAssign = true;
} else if (getOwnershipRule(Attributes) || !isReadWrite) {
isAssign = false;
} else {
isAssign = (!getLangOpts().ObjCAutoRefCount ||
!T->isObjCRetainableType());
}
// Issue a warning if property is 'assign' as default and its
// object, which is gc'able conforms to NSCopying protocol
if (getLangOpts().getGC() != LangOptions::NonGC &&
isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
if (const ObjCObjectPointerType *ObjPtrTy =
T->getAs<ObjCObjectPointerType>()) {
ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
if (IDecl)
if (ObjCProtocolDecl* PNSCopying =
LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
if (IDecl->ClassImplementsProtocol(PNSCopying, true))
Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
}
}
if (T->isObjCObjectType()) {
SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
StarLoc = getLocForEndOfToken(StarLoc);
Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
<< FixItHint::CreateInsertion(StarLoc, "*");
T = Context.getObjCObjectPointerType(T);
SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
}
DeclContext *DC = CDecl;
ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
FD.D.getIdentifierLoc(),
PropertyId, AtLoc,
LParenLoc, T, TInfo);
bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
(Attributes & ObjCDeclSpec::DQ_PR_class);
// Class property and instance property can have the same name.
if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Diag(PDecl->getLocation(), diag::err_duplicate_property);
Diag(prevDecl->getLocation(), diag::note_property_declare);
PDecl->setInvalidDecl();
}
else {
DC->addDecl(PDecl);
if (lexicalDC)
PDecl->setLexicalDeclContext(lexicalDC);
}
if (T->isArrayType() || T->isFunctionType()) {
Diag(AtLoc, diag::err_property_type) << T;
PDecl->setInvalidDecl();
}
ProcessDeclAttributes(S, PDecl, FD.D);
// Regardless of setter/getter attribute, we save the default getter/setter
// selector names in anticipation of declaration of setter/getter methods.
PDecl->setGetterName(GetterSel, GetterNameLoc);
PDecl->setSetterName(SetterSel, SetterNameLoc);
PDecl->setPropertyAttributesAsWritten(
makePropertyAttributesAsWritten(AttributesAsWritten));
if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
if (Attributes & ObjCDeclSpec::DQ_PR_getter)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
if (Attributes & ObjCDeclSpec::DQ_PR_setter)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
if (isReadWrite)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
if (Attributes & ObjCDeclSpec::DQ_PR_retain)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
if (Attributes & ObjCDeclSpec::DQ_PR_strong)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
if (Attributes & ObjCDeclSpec::DQ_PR_weak)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
if (isAssign)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
// In the semantic attributes, one of nonatomic or atomic is always set.
if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
else
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
// 'unsafe_unretained' is alias for 'assign'.
if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
if (isAssign)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
if (MethodImplKind == tok::objc_required)
PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
else if (MethodImplKind == tok::objc_optional)
PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
if (Attributes & ObjCDeclSpec::DQ_PR_class)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
return PDecl;
}
static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
ObjCPropertyDecl *property,
ObjCIvarDecl *ivar) {
if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
QualType ivarType = ivar->getType();
Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
// The lifetime implied by the property's attributes.
Qualifiers::ObjCLifetime propertyLifetime =
getImpliedARCOwnership(property->getPropertyAttributes(),
property->getType());
// We're fine if they match.
if (propertyLifetime == ivarLifetime) return;
// None isn't a valid lifetime for an object ivar in ARC, and
// __autoreleasing is never valid; don't diagnose twice.
if ((ivarLifetime == Qualifiers::OCL_None &&
S.getLangOpts().ObjCAutoRefCount) ||
ivarLifetime == Qualifiers::OCL_Autoreleasing)
return;
// If the ivar is private, and it's implicitly __unsafe_unretained
// becaues of its type, then pretend it was actually implicitly
// __strong. This is only sound because we're processing the
// property implementation before parsing any method bodies.
if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
propertyLifetime == Qualifiers::OCL_Strong &&
ivar->getAccessControl() == ObjCIvarDecl::Private) {
SplitQualType split = ivarType.split();
if (split.Quals.hasObjCLifetime()) {
assert(ivarType->isObjCARCImplicitlyUnretainedType());
split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
ivarType = S.Context.getQualifiedType(split);
ivar->setType(ivarType);
return;
}
}
switch (propertyLifetime) {
case Qualifiers::OCL_Strong:
S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
<< property->getDeclName()
<< ivar->getDeclName()
<< ivarLifetime;
break;
case Qualifiers::OCL_Weak:
S.Diag(ivar->getLocation(), diag::err_weak_property)
<< property->getDeclName()
<< ivar->getDeclName();
break;
case Qualifiers::OCL_ExplicitNone:
S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
<< property->getDeclName()
<< ivar->getDeclName()
<< ((property->getPropertyAttributesAsWritten()
& ObjCPropertyDecl::OBJC_PR_assign) != 0);
break;
case Qualifiers::OCL_Autoreleasing:
llvm_unreachable("properties cannot be autoreleasing");
case Qualifiers::OCL_None:
// Any other property should be ignored.
return;
}
S.Diag(property->getLocation(), diag::note_property_declare);
if (propertyImplLoc.isValid())
S.Diag(propertyImplLoc, diag::note_property_synthesize);
}
/// setImpliedPropertyAttributeForReadOnlyProperty -
/// This routine evaludates life-time attributes for a 'readonly'
/// property with no known lifetime of its own, using backing
/// 'ivar's attribute, if any. If no backing 'ivar', property's
/// life-time is assumed 'strong'.
static void setImpliedPropertyAttributeForReadOnlyProperty(
ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
Qualifiers::ObjCLifetime propertyLifetime =
getImpliedARCOwnership(property->getPropertyAttributes(),
property->getType());
if (propertyLifetime != Qualifiers::OCL_None)
return;
if (!ivar) {
// if no backing ivar, make property 'strong'.
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
return;
}
// property assumes owenership of backing ivar.
QualType ivarType = ivar->getType();
Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
if (ivarLifetime == Qualifiers::OCL_Strong)
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
else if (ivarLifetime == Qualifiers::OCL_Weak)
property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
}
static bool
isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
ObjCPropertyDecl::PropertyAttributeKind Kind) {
return (Attr1 & Kind) != (Attr2 & Kind);
}
static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
unsigned Kinds) {
return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
}
/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
/// property declaration that should be synthesised in all of the inherited
/// protocols. It also diagnoses properties declared in inherited protocols with
/// mismatched types or attributes, since any of them can be candidate for
/// synthesis.
static ObjCPropertyDecl *
SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
ObjCInterfaceDecl *ClassDecl,
ObjCPropertyDecl *Property) {
assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
"Expected a property from a protocol");
ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
ObjCInterfaceDecl::PropertyDeclOrder Properties;
for (const auto *PI : ClassDecl->all_referenced_protocols()) {
if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
Properties);
}
if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
while (SDecl) {
for (const auto *PI : SDecl->all_referenced_protocols()) {
if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
Properties);
}
SDecl = SDecl->getSuperClass();
}
}
if (Properties.empty())
return Property;
ObjCPropertyDecl *OriginalProperty = Property;
size_t SelectedIndex = 0;
for (const auto &Prop : llvm::enumerate(Properties)) {
// Select the 'readwrite' property if such property exists.
if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
Property = Prop.value();
SelectedIndex = Prop.index();
}
}
if (Property != OriginalProperty) {
// Check that the old property is compatible with the new one.
Properties[SelectedIndex] = OriginalProperty;
}
QualType RHSType = S.Context.getCanonicalType(Property->getType());
unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
enum MismatchKind {
IncompatibleType = 0,
HasNoExpectedAttribute,
HasUnexpectedAttribute,
DifferentGetter,
DifferentSetter
};
// Represents a property from another protocol that conflicts with the
// selected declaration.
struct MismatchingProperty {
const ObjCPropertyDecl *Prop;
MismatchKind Kind;
StringRef AttributeName;
};
SmallVector<MismatchingProperty, 4> Mismatches;
for (ObjCPropertyDecl *Prop : Properties) {
// Verify the property attributes.
unsigned Attr = Prop->getPropertyAttributesAsWritten();
if (Attr != OriginalAttributes) {
auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
: HasUnexpectedAttribute;
Mismatches.push_back({Prop, Kind, AttributeName});
};
// The ownership might be incompatible unless the property has no explicit
// ownership.
bool HasOwnership = (Attr & (ObjCPropertyDecl::OBJC_PR_retain |
ObjCPropertyDecl::OBJC_PR_strong |
ObjCPropertyDecl::OBJC_PR_copy |
ObjCPropertyDecl::OBJC_PR_assign |
ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
ObjCPropertyDecl::OBJC_PR_weak)) != 0;
if (HasOwnership &&
isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
ObjCPropertyDecl::OBJC_PR_copy)) {
Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
continue;
}
if (HasOwnership && areIncompatiblePropertyAttributes(
OriginalAttributes, Attr,
ObjCPropertyDecl::OBJC_PR_retain |
ObjCPropertyDecl::OBJC_PR_strong)) {
Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
ObjCPropertyDecl::OBJC_PR_strong),
"retain (or strong)");
continue;
}
if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
ObjCPropertyDecl::OBJC_PR_atomic)) {
Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
continue;
}
}
if (Property->getGetterName() != Prop->getGetterName()) {
Mismatches.push_back({Prop, DifferentGetter, ""});
continue;
}
if (!Property->isReadOnly() && !Prop->isReadOnly() &&
Property->getSetterName() != Prop->getSetterName()) {
Mismatches.push_back({Prop, DifferentSetter, ""});
continue;
}
QualType LHSType = S.Context.getCanonicalType(Prop->getType());
if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
bool IncompatibleObjC = false;
QualType ConvertedType;
if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
|| IncompatibleObjC) {
Mismatches.push_back({Prop, IncompatibleType, ""});
continue;
}
}
}
if (Mismatches.empty())
return Property;
// Diagnose incompability.
{
bool HasIncompatibleAttributes = false;
for (const auto &Note : Mismatches)
HasIncompatibleAttributes =
Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
// Promote the warning to an error if there are incompatible attributes or
// incompatible types together with readwrite/readonly incompatibility.
auto Diag = S.Diag(Property->getLocation(),
Property != OriginalProperty || HasIncompatibleAttributes
? diag::err_protocol_property_mismatch
: diag::warn_protocol_property_mismatch);
Diag << Mismatches[0].Kind;
switch (Mismatches[0].Kind) {
case IncompatibleType:
Diag << Property->getType();
break;
case HasNoExpectedAttribute:
case HasUnexpectedAttribute:
Diag << Mismatches[0].AttributeName;
break;
case DifferentGetter:
Diag << Property->getGetterName();
break;
case DifferentSetter:
Diag << Property->getSetterName();
break;
}
}
for (const auto &Note : Mismatches) {
auto Diag =
S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
<< Note.Kind;
switch (Note.Kind) {
case IncompatibleType:
Diag << Note.Prop->getType();
break;
case HasNoExpectedAttribute:
case HasUnexpectedAttribute:
Diag << Note.AttributeName;
break;
case DifferentGetter:
Diag << Note.Prop->getGetterName();
break;
case DifferentSetter:
Diag << Note.Prop->getSetterName();
break;
}