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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
|
//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
//
// 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 declarations.
//
//===----------------------------------------------------------------------===//
#include "Sema.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Parse/Scope.h"
using namespace clang;
/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
/// and user declared, in the method definition's AST.
void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
assert(getCurMethodDecl() == 0 && "Method parsing confused");
ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
assert(MDecl != 0 && "Not a method declarator!");
// Allow the rest of sema to find private method decl implementations.
if (MDecl->isInstance())
AddInstanceMethodToGlobalPool(MDecl);
else
AddFactoryMethodToGlobalPool(MDecl);
// Allow all of Sema to see that we are entering a method definition.
PushDeclContext(MDecl);
// Create Decl objects for each parameter, entrring them in the scope for
// binding to their use.
struct DeclaratorChunk::ParamInfo PI;
// Insert the invisible arguments, self and _cmd!
PI.Ident = &Context.Idents.get("self");
PI.IdentLoc = SourceLocation(); // synthesized vars have a null location.
QualType selfTy;
if (MDecl->isInstance()) {
selfTy = Context.getObjCIdType();
if (ObjCInterfaceDecl *OID = MDecl->getClassInterface()) {
// There may be no interface context due to error in declaration of the
// interface (which has been reported). Recover gracefully
selfTy = Context.getObjCInterfaceType(OID);
selfTy = Context.getPointerType(selfTy);
}
} else // we have a factory method.
selfTy = Context.getObjCClassType();
getCurMethodDecl()->setSelfDecl(CreateImplicitParameter(FnBodyScope,
PI.Ident, PI.IdentLoc, selfTy));
PI.Ident = &Context.Idents.get("_cmd");
getCurMethodDecl()->setCmdDecl(CreateImplicitParameter(FnBodyScope,
PI.Ident, PI.IdentLoc, Context.getObjCSelType()));
// Introduce all of the other parameters into this scope.
for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
ParmVarDecl *PDecl = MDecl->getParamDecl(i);
IdentifierInfo *II = PDecl->getIdentifier();
if (II)
PushOnScopeChains(PDecl, FnBodyScope);
}
}
Sema::DeclTy *Sema::ActOnStartClassInterface(
SourceLocation AtInterfaceLoc,
IdentifierInfo *ClassName, SourceLocation ClassLoc,
IdentifierInfo *SuperName, SourceLocation SuperLoc,
IdentifierInfo **ProtocolNames, unsigned NumProtocols,
SourceLocation EndProtoLoc, AttributeList *AttrList) {
assert(ClassName && "Missing class identifier");
// Check for another declaration kind with the same name.
Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Diag(ClassLoc, diag::err_redefinition_different_kind,
ClassName->getName());
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
}
ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
if (IDecl) {
// Class already seen. Is it a forward declaration?
if (!IDecl->isForwardDecl())
Diag(AtInterfaceLoc, diag::err_duplicate_class_def, IDecl->getName());
else {
IDecl->setLocation(AtInterfaceLoc);
IDecl->setForwardDecl(false);
IDecl->AllocIntfRefProtocols(NumProtocols);
}
}
else {
IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc, NumProtocols,
ClassName, ClassLoc);
ObjCInterfaceDecls[ClassName] = IDecl;
// Remember that this needs to be removed when the scope is popped.
TUScope->AddDecl(IDecl);
}
if (SuperName) {
ObjCInterfaceDecl* SuperClassEntry = 0;
// Check if a different kind of symbol declared in this scope.
PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Diag(SuperLoc, diag::err_redefinition_different_kind,
SuperName->getName());
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
}
else {
// Check that super class is previously defined
SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
if (!SuperClassEntry || SuperClassEntry->isForwardDecl()) {
Diag(AtInterfaceLoc, diag::err_undef_superclass,
SuperClassEntry ? SuperClassEntry->getName()
: SuperName->getName(),
ClassName->getName());
}
}
IDecl->setSuperClass(SuperClassEntry);
IDecl->setSuperClassLoc(SuperLoc);
IDecl->setLocEnd(SuperLoc);
} else { // we have a root class.
IDecl->setLocEnd(ClassLoc);
}
/// Check then save referenced protocols
if (NumProtocols) {
for (unsigned int i = 0; i != NumProtocols; i++) {
ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtocolNames[i]];
if (!RefPDecl || RefPDecl->isForwardDecl())
Diag(ClassLoc, diag::warn_undef_protocolref,
ProtocolNames[i]->getName(),
ClassName->getName());
IDecl->setIntfRefProtocols(i, RefPDecl);
}
IDecl->setLocEnd(EndProtoLoc);
}
return IDecl;
}
/// ActOnCompatiblityAlias - this action is called after complete parsing of
/// @compaatibility_alias declaration. It sets up the alias relationships.
Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
IdentifierInfo *AliasName,
SourceLocation AliasLocation,
IdentifierInfo *ClassName,
SourceLocation ClassLocation) {
// Look for previous declaration of alias name
Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
if (ADecl) {
if (isa<ObjCCompatibleAliasDecl>(ADecl)) {
Diag(AliasLocation, diag::warn_previous_alias_decl);
Diag(ADecl->getLocation(), diag::warn_previous_declaration);
}
else {
Diag(AliasLocation, diag::err_conflicting_aliasing_type,
AliasName->getName());
Diag(ADecl->getLocation(), diag::err_previous_declaration);
}
return 0;
}
// Check for class declaration
Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
if (CDecl == 0) {
Diag(ClassLocation, diag::warn_undef_interface, ClassName->getName());
if (CDeclU)
Diag(CDeclU->getLocation(), diag::warn_previous_declaration);
return 0;
}
// Everything checked out, instantiate a new alias declaration AST.
ObjCCompatibleAliasDecl *AliasDecl =
ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
ObjCAliasDecls[AliasName] = AliasDecl;
TUScope->AddDecl(AliasDecl);
return AliasDecl;
}
Sema::DeclTy *Sema::ActOnStartProtocolInterface(
SourceLocation AtProtoInterfaceLoc,
IdentifierInfo *ProtocolName, SourceLocation ProtocolLoc,
IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
SourceLocation EndProtoLoc) {
assert(ProtocolName && "Missing protocol identifier");
ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
if (PDecl) {
// Protocol already seen. Better be a forward protocol declaration
if (!PDecl->isForwardDecl()) {
Diag(ProtocolLoc, diag::err_duplicate_protocol_def,
ProtocolName->getName());
// Just return the protocol we already had.
// FIXME: don't leak the objects passed in!
return PDecl;
}
PDecl->setForwardDecl(false);
PDecl->AllocReferencedProtocols(NumProtoRefs);
} else {
PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc, NumProtoRefs,
ProtocolName);
PDecl->setForwardDecl(false);
ObjCProtocols[ProtocolName] = PDecl;
}
if (NumProtoRefs) {
/// Check then save referenced protocols.
for (unsigned int i = 0; i != NumProtoRefs; i++) {
ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
if (!RefPDecl || RefPDecl->isForwardDecl())
Diag(ProtocolLoc, diag::warn_undef_protocolref,
ProtoRefNames[i]->getName(),
ProtocolName->getName());
PDecl->setReferencedProtocols(i, RefPDecl);
}
PDecl->setLocEnd(EndProtoLoc);
}
return PDecl;
}
/// FindProtocolDeclaration - This routine looks up protocols and
/// issuer error if they are not declared. It returns list of protocol
/// declarations in its 'Protocols' argument.
void
Sema::FindProtocolDeclaration(SourceLocation TypeLoc,
IdentifierInfo **ProtocolId,
unsigned NumProtocols,
llvm::SmallVector<DeclTy *,8> &Protocols) {
for (unsigned i = 0; i != NumProtocols; ++i) {
ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i]];
if (!PDecl)
Diag(TypeLoc, diag::err_undeclared_protocol,
ProtocolId[i]->getName());
else
Protocols.push_back(PDecl);
}
}
/// DiagnosePropertyMismatch - Compares two properties for their
/// attributes and types and warns on a variety of inconsistancies.
///
void
Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
ObjCPropertyDecl *SuperProperty,
const char *inheritedName) {
ObjCPropertyDecl::PropertyAttributeKind CAttr =
Property->getPropertyAttributes();
ObjCPropertyDecl::PropertyAttributeKind SAttr =
SuperProperty->getPropertyAttributes();
if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
&& (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Diag(Property->getLocation(), diag::warn_readonly_property,
Property->getName(), inheritedName);
if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
!= (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
Diag(Property->getLocation(), diag::warn_property_attribute,
Property->getName(), "copy", inheritedName,
SourceRange());
else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
!= (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
Diag(Property->getLocation(), diag::warn_property_attribute,
Property->getName(), "retain", inheritedName,
SourceRange());
if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
!= (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
Diag(Property->getLocation(), diag::warn_property_attribute,
Property->getName(), "atomic", inheritedName,
SourceRange());
if (Property->getSetterName() != SuperProperty->getSetterName())
Diag(Property->getLocation(), diag::warn_property_attribute,
Property->getName(), "setter", inheritedName,
SourceRange());
if (Property->getGetterName() != SuperProperty->getGetterName())
Diag(Property->getLocation(), diag::warn_property_attribute,
Property->getName(), "getter", inheritedName,
SourceRange());
if (Property->getCanonicalType() != SuperProperty->getCanonicalType())
Diag(Property->getLocation(), diag::warn_property_type,
Property->getType().getAsString(),
inheritedName);
}
/// ComparePropertiesInBaseAndSuper - This routine compares property
/// declarations in base and its super class, if any, and issues
/// diagnostics in a variety of inconsistant situations.
///
void
Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
if (!SDecl)
return;
for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
E = SDecl->classprop_end(); S != E; ++S) {
ObjCPropertyDecl *SuperPDecl = (*S);
// Does property in super class has declaration in current class?
for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
E = IDecl->classprop_end(); I != E; ++I) {
ObjCPropertyDecl *PDecl = (*I);
if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
DiagnosePropertyMismatch(PDecl, SuperPDecl, SDecl->getName());
}
}
}
/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
/// of properties declared in a protocol and adds them to the list
/// of properties for current class if it is not there already.
void
Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
ObjCProtocolDecl *PDecl)
{
llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
E = PDecl->classprop_end(); P != E; ++P) {
ObjCPropertyDecl *Pr = (*P);
ObjCInterfaceDecl::classprop_iterator CP, CE;
// Is this property already in class's list of properties?
for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
CP != CE; ++CP)
if ((*CP)->getIdentifier() == Pr->getIdentifier())
break;
if (CP == CE)
// Add this property to list of properties for thie class.
mergeProperties.push_back(Pr);
else
// Property protocol already exist in class. Diagnose any mismatch.
DiagnosePropertyMismatch((*CP), Pr, PDecl->getName());
}
IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
}
/// MergeProtocolPropertiesIntoClass - This routine merges properties
/// declared in 'MergeItsProtocols' objects (which can be a class or an
/// inherited protocol into the list of properties for class 'IDecl'
///
void
Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
DeclTy *MergeItsProtocols) {
Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
if (ObjCInterfaceDecl *MDecl =
dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
E = MDecl->protocol_end(); P != E; ++P)
MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
// Merge properties of class (*P) into IDECL's
;
// Go thru the list of protocols for this class and recursively merge
// their properties into this class as well.
for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
E = IDecl->protocol_end(); P != E; ++P)
MergeProtocolPropertiesIntoClass(IDecl, (*P));
}
else if (ObjCProtocolDecl *MDecl =
dyn_cast<ObjCProtocolDecl>(ClassDecl))
for (ObjCProtocolDecl::protocol_iterator P = MDecl->protocol_begin(),
E = MDecl->protocol_end(); P != E; ++P)
MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
else
assert(false && "MergeProtocolPropertiesIntoClass - bad object kind");
}
/// ActOnForwardProtocolDeclaration -
Action::DeclTy *
Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
IdentifierInfo **IdentList, unsigned NumElts) {
llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
for (unsigned i = 0; i != NumElts; ++i) {
IdentifierInfo *Ident = IdentList[i];
ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
if (PDecl == 0) { // Not already seen?
// FIXME: Pass in the location of the identifier!
PDecl = ObjCProtocolDecl::Create(Context, AtProtocolLoc, 0, Ident);
}
Protocols.push_back(PDecl);
}
return ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
&Protocols[0], Protocols.size());
}
Sema::DeclTy *Sema::ActOnStartCategoryInterface(
SourceLocation AtInterfaceLoc,
IdentifierInfo *ClassName, SourceLocation ClassLoc,
IdentifierInfo *CategoryName, SourceLocation CategoryLoc,
IdentifierInfo **ProtoRefNames, unsigned NumProtoRefs,
SourceLocation EndProtoLoc) {
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
ObjCCategoryDecl *CDecl =
ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
CDecl->setClassInterface(IDecl);
/// Check that class of this category is already completely declared.
if (!IDecl || IDecl->isForwardDecl())
Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
else {
/// Check for duplicate interface declaration for this category
ObjCCategoryDecl *CDeclChain;
for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
CDeclChain = CDeclChain->getNextClassCategory()) {
if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
Diag(CategoryLoc, diag::warn_dup_category_def, ClassName->getName(),
CategoryName->getName());
break;
}
}
if (!CDeclChain)
CDecl->insertNextClassCategory();
}
if (NumProtoRefs) {
llvm::SmallVector<ObjCProtocolDecl*, 32> RefProtocols;
/// Check and then save the referenced protocols.
for (unsigned int i = 0; i != NumProtoRefs; i++) {
ObjCProtocolDecl* RefPDecl = ObjCProtocols[ProtoRefNames[i]];
if (!RefPDecl || RefPDecl->isForwardDecl()) {
Diag(CategoryLoc, diag::warn_undef_protocolref,
ProtoRefNames[i]->getName(),
CategoryName->getName());
}
if (RefPDecl)
RefProtocols.push_back(RefPDecl);
}
if (!RefProtocols.empty())
CDecl->setReferencedProtocolList(&RefProtocols[0], RefProtocols.size());
}
CDecl->setLocEnd(EndProtoLoc);
return CDecl;
}
/// ActOnStartCategoryImplementation - Perform semantic checks on the
/// category implementation declaration and build an ObjCCategoryImplDecl
/// object.
Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
SourceLocation AtCatImplLoc,
IdentifierInfo *ClassName, SourceLocation ClassLoc,
IdentifierInfo *CatName, SourceLocation CatLoc) {
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
ObjCCategoryImplDecl *CDecl =
ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
/// Check that class of this category is already completely declared.
if (!IDecl || IDecl->isForwardDecl())
Diag(ClassLoc, diag::err_undef_interface, ClassName->getName());
/// TODO: Check that CatName, category name, is not used in another
// implementation.
return CDecl;
}
Sema::DeclTy *Sema::ActOnStartClassImplementation(
SourceLocation AtClassImplLoc,
IdentifierInfo *ClassName, SourceLocation ClassLoc,
IdentifierInfo *SuperClassname,
SourceLocation SuperClassLoc) {
ObjCInterfaceDecl* IDecl = 0;
// Check for another declaration kind with the same name.
Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Diag(ClassLoc, diag::err_redefinition_different_kind,
ClassName->getName());
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
}
else {
// Is there an interface declaration of this class; if not, warn!
IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
if (!IDecl)
Diag(ClassLoc, diag::warn_undef_interface, ClassName->getName());
}
// Check that super class name is valid class name
ObjCInterfaceDecl* SDecl = 0;
if (SuperClassname) {
// Check if a different kind of symbol declared in this scope.
PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Diag(SuperClassLoc, diag::err_redefinition_different_kind,
SuperClassname->getName());
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
}
else {
SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
if (!SDecl)
Diag(SuperClassLoc, diag::err_undef_superclass,
SuperClassname->getName(), ClassName->getName());
else if (IDecl && IDecl->getSuperClass() != SDecl) {
// This implementation and its interface do not have the same
// super class.
Diag(SuperClassLoc, diag::err_conflicting_super_class,
SDecl->getName());
Diag(SDecl->getLocation(), diag::err_previous_definition);
}
}
}
if (!IDecl) {
// Legacy case of @implementation with no corresponding @interface.
// Build, chain & install the interface decl into the identifier.
IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, 0, ClassName,
ClassLoc, false, true);
ObjCInterfaceDecls[ClassName] = IDecl;
IDecl->setSuperClass(SDecl);
IDecl->setLocEnd(ClassLoc);
// Remember that this needs to be removed when the scope is popped.
TUScope->AddDecl(IDecl);
}
ObjCImplementationDecl* IMPDecl =
ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
IDecl, SDecl);
// Check that there is no duplicate implementation of this class.
if (ObjCImplementations[ClassName])
// FIXME: Don't leak everything!
Diag(ClassLoc, diag::err_dup_implementation_class, ClassName->getName());
else // add it to the list.
ObjCImplementations[ClassName] = IMPDecl;
return IMPDecl;
}
void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
ObjCIvarDecl **ivars, unsigned numIvars,
SourceLocation RBrace) {
assert(ImpDecl && "missing implementation decl");
ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
if (!IDecl)
return;
/// Check case of non-existing @interface decl.
/// (legacy objective-c @implementation decl without an @interface decl).
/// Add implementations's ivar to the synthesize class's ivar list.
if (IDecl->ImplicitInterfaceDecl()) {
IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
return;
}
// If implementation has empty ivar list, just return.
if (numIvars == 0)
return;
assert(ivars && "missing @implementation ivars");
// Check interface's Ivar list against those in the implementation.
// names and types must match.
//
unsigned j = 0;
ObjCInterfaceDecl::ivar_iterator
IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
for (; numIvars > 0 && IVI != IVE; ++IVI) {
ObjCIvarDecl* ImplIvar = ivars[j++];
ObjCIvarDecl* ClsIvar = *IVI;
assert (ImplIvar && "missing implementation ivar");
assert (ClsIvar && "missing class ivar");
if (ImplIvar->getCanonicalType() != ClsIvar->getCanonicalType()) {
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type,
ImplIvar->getIdentifier()->getName());
Diag(ClsIvar->getLocation(), diag::err_previous_definition,
ClsIvar->getIdentifier()->getName());
}
// TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
// as error.
else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name,
ImplIvar->getIdentifier()->getName());
Diag(ClsIvar->getLocation(), diag::err_previous_definition,
ClsIvar->getIdentifier()->getName());
return;
}
--numIvars;
}
if (numIvars > 0)
Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
else if (IVI != IVE)
Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
}
void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
bool &IncompleteImpl) {
if (!IncompleteImpl) {
Diag(ImpLoc, diag::warn_incomplete_impl);
IncompleteImpl = true;
}
Diag(ImpLoc, diag::warn_undef_method_impl, method->getSelector().getName());
}
/// CheckProtocolMethodDefs - This routine checks unimplemented methods
/// Declared in protocol, and those referenced by it.
void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
ObjCProtocolDecl *PDecl,
bool& IncompleteImpl,
const llvm::DenseSet<Selector> &InsMap,
const llvm::DenseSet<Selector> &ClsMap) {
// check unimplemented instance methods.
for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
E = PDecl->instmeth_end(); I != E; ++I) {
ObjCMethodDecl *method = *I;
if (!InsMap.count(method->getSelector()) &&
method->getImplementationControl() != ObjCMethodDecl::Optional)
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
}
// check unimplemented class methods
for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
E = PDecl->classmeth_end(); I != E; ++I) {
ObjCMethodDecl *method = *I;
if (!ClsMap.count(method->getSelector()) &&
method->getImplementationControl() != ObjCMethodDecl::Optional)
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
}
// Check on this protocols's referenced protocols, recursively
ObjCProtocolDecl** RefPDecl = PDecl->getReferencedProtocols();
for (unsigned i = 0; i < PDecl->getNumReferencedProtocols(); i++)
CheckProtocolMethodDefs(ImpLoc, RefPDecl[i], IncompleteImpl, InsMap, ClsMap);
}
void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
ObjCInterfaceDecl* IDecl) {
llvm::DenseSet<Selector> InsMap;
// Check and see if instance methods in class interface have been
// implemented in the implementation class.
for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
E = IMPDecl->instmeth_end(); I != E; ++I)
InsMap.insert((*I)->getSelector());
bool IncompleteImpl = false;
for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
E = IDecl->instmeth_end(); I != E; ++I)
if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
llvm::DenseSet<Selector> ClsMap;
// Check and see if class methods in class interface have been
// implemented in the implementation class.
for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
E = IMPDecl->classmeth_end(); I != E; ++I)
ClsMap.insert((*I)->getSelector());
for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
E = IDecl->classmeth_end(); I != E; ++I)
if (!ClsMap.count((*I)->getSelector()))
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
// Check the protocol list for unimplemented methods in the @implementation
// class.
ObjCProtocolDecl** protocols = IDecl->getReferencedProtocols();
for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++)
CheckProtocolMethodDefs(IMPDecl->getLocation(), protocols[i],
IncompleteImpl, InsMap, ClsMap);
}
/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
/// category interface is implemented in the category @implementation.
void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
ObjCCategoryDecl *CatClassDecl) {
llvm::DenseSet<Selector> InsMap;
// Check and see if instance methods in category interface have been
// implemented in its implementation class.
for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
E = CatImplDecl->instmeth_end(); I != E; ++I)
InsMap.insert((*I)->getSelector());
bool IncompleteImpl = false;
for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
E = CatClassDecl->instmeth_end(); I != E; ++I)
if (!InsMap.count((*I)->getSelector()))
WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
llvm::DenseSet<Selector> ClsMap;
// Check and see if class methods in category interface have been
// implemented in its implementation class.
for (ObjCCategoryImplDecl::classmeth_iterator
I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
I != E; ++I)
ClsMap.insert((*I)->getSelector());
for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
E = CatClassDecl->classmeth_end(); I != E; ++I)
if (!ClsMap.count((*I)->getSelector()))
WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
// Check the protocol list for unimplemented methods in the @implementation
// class.
ObjCProtocolDecl** protocols = CatClassDecl->getReferencedProtocols();
for (unsigned i = 0; i < CatClassDecl->getNumReferencedProtocols(); i++) {
ObjCProtocolDecl* PDecl = protocols[i];
CheckProtocolMethodDefs(CatImplDecl->getLocation(), PDecl, IncompleteImpl,
InsMap, ClsMap);
}
}
/// ActOnForwardClassDeclaration -
Action::DeclTy *
Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
IdentifierInfo **IdentList, unsigned NumElts)
{
llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
for (unsigned i = 0; i != NumElts; ++i) {
// Check for another declaration kind with the same name.
Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
// GCC apparently allows the following idiom:
//
// typedef NSObject < XCElementTogglerP > XCElementToggler;
// @class XCElementToggler;
//
// FIXME: Make an extension?
TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
Diag(AtClassLoc, diag::err_redefinition_different_kind,
IdentList[i]->getName());
Diag(PrevDecl->getLocation(), diag::err_previous_definition);
}
}
ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
if (!IDecl) { // Not already seen? Make a forward decl.
IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, 0, IdentList[i],
SourceLocation(), true);
ObjCInterfaceDecls[IdentList[i]] = IDecl;
// Remember that this needs to be removed when the scope is popped.
TUScope->AddDecl(IDecl);
}
Interfaces.push_back(IDecl);
}
return ObjCClassDecl::Create(Context, AtClassLoc,
&Interfaces[0], Interfaces.size());
}
/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
/// returns true, or false, accordingly.
/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
const ObjCMethodDecl *PrevMethod) {
if (Method->getResultType().getCanonicalType() !=
PrevMethod->getResultType().getCanonicalType())
return false;
for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
ParmVarDecl *ParamDecl = Method->getParamDecl(i);
ParmVarDecl *PrevParamDecl = PrevMethod->getParamDecl(i);
if (Context.getCanonicalType(ParamDecl->getType()) !=
Context.getCanonicalType(PrevParamDecl->getType()))
return false;
}
return true;
}
void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
if (!FirstMethod.Method) {
// Haven't seen a method with this selector name yet - add it.
FirstMethod.Method = Method;
FirstMethod.Next = 0;
} else {
// We've seen a method with this name, now check the type signature(s).
bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Next = Next->Next)
match = MatchTwoMethodDeclarations(Method, Next->Method);
if (!match) {
// We have a new signature for an existing method - add it.
// This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
FirstMethod.Next = OMI;
}
}
}
void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
if (!FirstMethod.Method) {
// Haven't seen a method with this selector name yet - add it.
FirstMethod.Method = Method;
FirstMethod.Next = 0;
} else {
// We've seen a method with this name, now check the type signature(s).
bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
Next = Next->Next)
match = MatchTwoMethodDeclarations(Method, Next->Method);
if (!match) {
// We have a new signature for an existing method - add it.
// This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
FirstMethod.Next = OMI;
}
}
}
// Note: For class/category implemenations, allMethods/allProperties is
// always null.
void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
DeclTy **allMethods, unsigned allNum,
DeclTy **allProperties, unsigned pNum) {
Decl *ClassDecl = static_cast<Decl *>(classDecl);
// FIXME: If we don't have a ClassDecl, we have an error. We should consider
// always passing in a decl. If the decl has an error, isInvalidDecl()
// should be true.
if (!ClassDecl)
return;
llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
bool isInterfaceDeclKind =
isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
|| isa<ObjCProtocolDecl>(ClassDecl);
bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
if (pNum != 0)
if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
else
assert(false && "ActOnAtEnd - property declaration misplaced");
for (unsigned i = 0; i < allNum; i++ ) {
ObjCMethodDecl *Method =
cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
if (!Method) continue; // Already issued a diagnostic.
if (Method->isInstance()) {
/// Check for instance method of the same name with incompatible types
const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
: false;
if (isInterfaceDeclKind && PrevMethod && !match
|| checkIdenticalMethods && match) {
Diag(Method->getLocation(), diag::error_duplicate_method_decl,
Method->getSelector().getName());
Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
} else {
insMethods.push_back(Method);
InsMap[Method->getSelector()] = Method;
/// The following allows us to typecheck messages to "id".
AddInstanceMethodToGlobalPool(Method);
}
}
else {
/// Check for class method of the same name with incompatible types
const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
: false;
if (isInterfaceDeclKind && PrevMethod && !match
|| checkIdenticalMethods && match) {
Diag(Method->getLocation(), diag::error_duplicate_method_decl,
Method->getSelector().getName());
Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
} else {
clsMethods.push_back(Method);
ClsMap[Method->getSelector()] = Method;
/// The following allows us to typecheck messages to "Class".
AddFactoryMethodToGlobalPool(Method);
}
}
}
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
// Compares properties declaraed in this class to those of its
// super class.
ComparePropertiesInBaseAndSuper(I);
MergeProtocolPropertiesIntoClass(I, I);
for (ObjCInterfaceDecl::classprop_iterator P = I->classprop_begin(),
E = I->classprop_end(); P != E; ++P) {
// FIXME: It would be really nice if we could avoid this. Injecting
// methods into the interface makes it hard to distinguish "real" methods
// from synthesized "property" methods (that aren't in the source).
// This complicicates the rewriter's life.
I->addPropertyMethods(Context, *P, insMethods);
}
I->addMethods(&insMethods[0], insMethods.size(),
&clsMethods[0], clsMethods.size(), AtEndLoc);
} else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
P->addMethods(&insMethods[0], insMethods.size(),
&clsMethods[0], clsMethods.size(), AtEndLoc);
}
else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
C->addMethods(&insMethods[0], insMethods.size(),
&clsMethods[0], clsMethods.size(), AtEndLoc);
}
else if (ObjCImplementationDecl *IC =
dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
IC->setLocEnd(AtEndLoc);
if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
ImplMethodsVsClassMethods(IC, IDecl);
} else {
ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
CatImplClass->setLocEnd(AtEndLoc);
ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
// Find category interface decl and then check that all methods declared
// in this interface is implemented in the category @implementation.
if (IDecl) {
for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Categories; Categories = Categories->getNextClassCategory()) {
if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
break;
}
}
}
}
}
/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
/// objective-c's type qualifier from the parser version of the same info.
static Decl::ObjCDeclQualifier
CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
if (PQTVal & ObjCDeclSpec::DQ_In)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
if (PQTVal & ObjCDeclSpec::DQ_Inout)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
if (PQTVal & ObjCDeclSpec::DQ_Out)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
if (PQTVal & ObjCDeclSpec::DQ_Byref)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
if (PQTVal & ObjCDeclSpec::DQ_Oneway)
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
return ret;
}
Sema::DeclTy *Sema::ActOnMethodDeclaration(
SourceLocation MethodLoc, SourceLocation EndLoc,
tok::TokenKind MethodType, DeclTy *classDecl,
ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
Selector Sel,
// optional arguments. The number of types/arguments is obtained
// from the Sel.getNumArgs().
ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
bool isVariadic) {
Decl *ClassDecl = static_cast<Decl*>(classDecl);
// Make sure we can establish a context for the method.
if (!ClassDecl) {
Diag(MethodLoc, diag::error_missing_method_context);
return 0;
}
QualType resultDeclType;
if (ReturnType)
resultDeclType = QualType::getFromOpaquePtr(ReturnType);
else // get the type for "id".
resultDeclType = Context.getObjCIdType();
ObjCMethodDecl* ObjCMethod =
ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
ClassDecl, AttrList,
MethodType == tok::minus, isVariadic,
false,
MethodDeclKind == tok::objc_optional ?
ObjCMethodDecl::Optional :
ObjCMethodDecl::Required);
llvm::SmallVector<ParmVarDecl*, 16> Params;
for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
// FIXME: arg->AttrList must be stored too!
QualType argType;
if (ArgTypes[i])
argType = QualType::getFromOpaquePtr(ArgTypes[i]);
else
argType = Context.getObjCIdType();
ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
SourceLocation(/*FIXME*/),
ArgNames[i], argType,
VarDecl::None, 0, 0);
Param->setObjCDeclQualifier(
CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
Params.push_back(Param);
}
ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
ObjCMethod->setObjCDeclQualifier(
CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
const ObjCMethodDecl *PrevMethod = 0;
// For implementations (which can be very "coarse grain"), we add the
// method now. This allows the AST to implement lookup methods that work
// incrementally (without waiting until we parse the @end). It also allows
// us to flag multiple declaration errors as they occur.
if (ObjCImplementationDecl *ImpDecl =
dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
if (MethodType == tok::minus) {
PrevMethod = ImpDecl->getInstanceMethod(Sel);
ImpDecl->addInstanceMethod(ObjCMethod);
} else {
PrevMethod = ImpDecl->getClassMethod(Sel);
ImpDecl->addClassMethod(ObjCMethod);
}
}
else if (ObjCCategoryImplDecl *CatImpDecl =
dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
if (MethodType == tok::minus) {
PrevMethod = CatImpDecl->getInstanceMethod(Sel);
CatImpDecl->addInstanceMethod(ObjCMethod);
} else {
PrevMethod = CatImpDecl->getClassMethod(Sel);
CatImpDecl->addClassMethod(ObjCMethod);
}
}
if (PrevMethod) {
// You can never have two method definitions with the same name.
Diag(ObjCMethod->getLocation(), diag::error_duplicate_method_decl,
ObjCMethod->getSelector().getName());
Diag(PrevMethod->getLocation(), diag::err_previous_declaration);
}
return ObjCMethod;
}
Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
FieldDeclarator &FD,
ObjCDeclSpec &ODS,
Selector GetterSel,
Selector SetterSel,
tok::ObjCKeywordKind MethodImplKind) {
QualType T = GetTypeForDeclarator(FD.D, S);
ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
FD.D.getIdentifier(), T);
// Regardless of setter/getter attribute, we save the default getter/setter
// selector names in anticipation of declaration of setter/getter methods.
PDecl->setGetterName(GetterSel);
PDecl->setSetterName(SetterSel);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readonly)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_getter)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_setter)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_assign)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_readwrite)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_retain)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_copy)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
if (ODS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic)
PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
if (MethodImplKind == tok::objc_required)
PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
else if (MethodImplKind == tok::objc_optional)
PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
return PDecl;
}
/// ActOnPropertyImplDecl - This routine performs semantic checks and
/// builds the AST node for a property implementation declaration; declared
/// as @synthesize or @dynamic.
///
Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
SourceLocation PropertyLoc,
bool Synthesize,
DeclTy *ClassCatImpDecl,
IdentifierInfo *PropertyId,
IdentifierInfo *PropertyIvar) {
Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
// Make sure we have a context for the property implementation declaration.
if (!ClassImpDecl) {
Diag(AtLoc, diag::error_missing_property_context);
return 0;
}
ObjCPropertyDecl *property = 0;
ObjCInterfaceDecl* IDecl = 0;
// Find the class or category class where this property must have
// a declaration.
ObjCImplementationDecl *IC = 0;
ObjCCategoryImplDecl* CatImplClass = 0;
if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
IDecl = getObjCInterfaceDecl(IC->getIdentifier());
// We always synthesize an interface for an implementation
// without an interface decl. So, IDecl is always non-zero.
assert(IDecl &&
"ActOnPropertyImplDecl - @implementation without @interface");
// Look for this property declaration in the @implementation's @interface
property = IDecl->FindPropertyDeclaration(PropertyId);
if (!property) {
Diag(PropertyLoc, diag::error_bad_property_decl, IDecl->getName());
return 0;
}
}
else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
if (Synthesize) {
Diag(AtLoc, diag::error_synthesize_category_decl);
return 0;
}
IDecl = CatImplClass->getClassInterface();
if (!IDecl) {
Diag(AtLoc, diag::error_missing_property_interface);
return 0;
}
ObjCCategoryDecl *Category =
IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
// If category for this implementation not found, it is an error which
// has already been reported eralier.
if (!Category)
return 0;
// Look for this property declaration in @implementation's category
property = Category->FindPropertyDeclaration(PropertyId);
if (!property) {
Diag(PropertyLoc, diag::error_bad_category_property_decl,
Category->getName());
return 0;
}
}
else {
Diag(AtLoc, diag::error_bad_property_context);
return 0;
}
ObjCIvarDecl *Ivar = 0;
// Check that we have a valid, previously declared ivar for @synthesize
if (Synthesize) {
// @synthesize
if (!PropertyIvar)
PropertyIvar = PropertyId;
// Check that this is a previously declared 'ivar' in 'IDecl' interface
Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
if (!Ivar) {
Diag(PropertyLoc, diag::error_missing_property_ivar_decl,
PropertyId->getName());
return 0;
}
// Check that type of property and its ivar match.
if (Ivar->getCanonicalType() != property->getCanonicalType()) {
Diag(PropertyLoc, diag::error_property_ivar_type, property->getName(),
Ivar->getName());
return 0;
}
} else if (PropertyIvar) {
// @dynamic
Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
return 0;
}
assert (property && "ActOnPropertyImplDecl - property declaration missing");
ObjCPropertyImplDecl *PIDecl =
ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
(Synthesize ?
ObjCPropertyImplDecl::OBJC_PR_IMPL_SYNTHSIZE
: ObjCPropertyImplDecl::OBJC_PR_IMPL_DYNAMIC),
Ivar);
if (IC)
IC->addPropertyImplementation(PIDecl);
else
CatImplClass->addPropertyImplementation(PIDecl);
return PIDecl;
}
|