SemaDeclObjC.cpp revision ae6f6fd1527a1da84679a6f0439dec3bbbd6ca7b
1//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Parse/DeclSpec.h"
19
20using namespace clang;
21
22/// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible
23/// and user declared, in the method definition's AST.
24void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) {
25  assert(getCurMethodDecl() == 0 && "Method parsing confused");
26  ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D);
27
28  // If we don't have a valid method decl, simply return.
29  if (!MDecl)
30    return;
31
32  // Allow the rest of sema to find private method decl implementations.
33  if (MDecl->isInstance())
34    AddInstanceMethodToGlobalPool(MDecl);
35  else
36    AddFactoryMethodToGlobalPool(MDecl);
37
38  // Allow all of Sema to see that we are entering a method definition.
39  PushDeclContext(MDecl);
40
41  // Create Decl objects for each parameter, entrring them in the scope for
42  // binding to their use.
43
44  // Insert the invisible arguments, self and _cmd!
45  MDecl->createImplicitParams(Context);
46
47  PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
48  PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
49
50  // Introduce all of the other parameters into this scope.
51  for (unsigned i = 0, e = MDecl->getNumParams(); i != e; ++i) {
52    ParmVarDecl *PDecl = MDecl->getParamDecl(i);
53    IdentifierInfo *II = PDecl->getIdentifier();
54    if (II)
55      PushOnScopeChains(PDecl, FnBodyScope);
56  }
57}
58
59Sema::DeclTy *Sema::
60ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
61                         IdentifierInfo *ClassName, SourceLocation ClassLoc,
62                         IdentifierInfo *SuperName, SourceLocation SuperLoc,
63                         DeclTy * const *ProtoRefs, unsigned NumProtoRefs,
64                         SourceLocation EndProtoLoc, AttributeList *AttrList) {
65  assert(ClassName && "Missing class identifier");
66
67  // Check for another declaration kind with the same name.
68  Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
69  if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
70    // Maybe we will complain about the shadowed template parameter.
71    DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl);
72    // Just pretend that we didn't see the previous declaration.
73    PrevDecl = 0;
74  }
75
76  if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
77    Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
78    Diag(PrevDecl->getLocation(), diag::note_previous_definition);
79  }
80
81  ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
82  if (IDecl) {
83    // Class already seen. Is it a forward declaration?
84    if (!IDecl->isForwardDecl()) {
85      Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
86      Diag(IDecl->getLocation(), diag::note_previous_definition);
87
88      // Return the previous class interface.
89      // FIXME: don't leak the objects passed in!
90      return IDecl;
91    } else {
92      IDecl->setLocation(AtInterfaceLoc);
93      IDecl->setForwardDecl(false);
94    }
95  } else {
96    IDecl = ObjCInterfaceDecl::Create(Context, AtInterfaceLoc,
97                                      ClassName, ClassLoc);
98    if (AttrList)
99      ProcessDeclAttributeList(IDecl, AttrList);
100
101    ObjCInterfaceDecls[ClassName] = IDecl;
102    // Remember that this needs to be removed when the scope is popped.
103    TUScope->AddDecl(IDecl);
104  }
105
106  if (SuperName) {
107    ObjCInterfaceDecl* SuperClassEntry = 0;
108    // Check if a different kind of symbol declared in this scope.
109    PrevDecl = LookupDecl(SuperName, Decl::IDNS_Ordinary, TUScope);
110    if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
111      Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
112      Diag(PrevDecl->getLocation(), diag::note_previous_definition);
113    }
114    else {
115      // Check that super class is previously defined
116      SuperClassEntry = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
117
118      if (!SuperClassEntry)
119        Diag(SuperLoc, diag::err_undef_superclass)
120          << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
121      else if (SuperClassEntry->isForwardDecl())
122        Diag(SuperLoc, diag::err_undef_superclass)
123          << SuperClassEntry->getDeclName() << ClassName
124          << SourceRange(AtInterfaceLoc, ClassLoc);
125    }
126    IDecl->setSuperClass(SuperClassEntry);
127    IDecl->setSuperClassLoc(SuperLoc);
128    IDecl->setLocEnd(SuperLoc);
129  } else { // we have a root class.
130    IDecl->setLocEnd(ClassLoc);
131  }
132
133  /// Check then save referenced protocols.
134  if (NumProtoRefs) {
135    IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
136    IDecl->setLocEnd(EndProtoLoc);
137  }
138
139  CheckObjCDeclScope(IDecl);
140  return IDecl;
141}
142
143/// ActOnCompatiblityAlias - this action is called after complete parsing of
144/// @compatibility_alias declaration. It sets up the alias relationships.
145Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
146                                           IdentifierInfo *AliasName,
147                                           SourceLocation AliasLocation,
148                                           IdentifierInfo *ClassName,
149                                           SourceLocation ClassLocation) {
150  // Look for previous declaration of alias name
151  Decl *ADecl = LookupDecl(AliasName, Decl::IDNS_Ordinary, TUScope);
152  if (ADecl) {
153    if (isa<ObjCCompatibleAliasDecl>(ADecl))
154      Diag(AliasLocation, diag::warn_previous_alias_decl);
155    else
156      Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
157    Diag(ADecl->getLocation(), diag::note_previous_declaration);
158    return 0;
159  }
160  // Check for class declaration
161  Decl *CDeclU = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
162  ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
163  if (CDecl == 0) {
164    Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
165    if (CDeclU)
166      Diag(CDeclU->getLocation(), diag::note_previous_declaration);
167    return 0;
168  }
169
170  // Everything checked out, instantiate a new alias declaration AST.
171  ObjCCompatibleAliasDecl *AliasDecl =
172    ObjCCompatibleAliasDecl::Create(Context, AtLoc, AliasName, CDecl);
173
174  ObjCAliasDecls[AliasName] = AliasDecl;
175
176  if (!CheckObjCDeclScope(AliasDecl))
177    TUScope->AddDecl(AliasDecl);
178
179  return AliasDecl;
180}
181
182Sema::DeclTy *
183Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
184                                  IdentifierInfo *ProtocolName,
185                                  SourceLocation ProtocolLoc,
186                                  DeclTy * const *ProtoRefs,
187                                  unsigned NumProtoRefs,
188                                  SourceLocation EndProtoLoc,
189                                  AttributeList *AttrList) {
190  // FIXME: Deal with AttrList.
191  assert(ProtocolName && "Missing protocol identifier");
192  ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName];
193  if (PDecl) {
194    // Protocol already seen. Better be a forward protocol declaration
195    if (!PDecl->isForwardDecl()) {
196      Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName;
197      Diag(PDecl->getLocation(), diag::note_previous_definition);
198      // Just return the protocol we already had.
199      // FIXME: don't leak the objects passed in!
200      return PDecl;
201    }
202    // Make sure the cached decl gets a valid start location.
203    PDecl->setLocation(AtProtoInterfaceLoc);
204    PDecl->setForwardDecl(false);
205  } else {
206    PDecl = ObjCProtocolDecl::Create(Context, AtProtoInterfaceLoc,ProtocolName);
207    PDecl->setForwardDecl(false);
208    ObjCProtocols[ProtocolName] = PDecl;
209  }
210
211  if (NumProtoRefs) {
212    /// Check then save referenced protocols.
213    PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
214    PDecl->setLocEnd(EndProtoLoc);
215  }
216
217  CheckObjCDeclScope(PDecl);
218  return PDecl;
219}
220
221/// FindProtocolDeclaration - This routine looks up protocols and
222/// issues an error if they are not declared. It returns list of
223/// protocol declarations in its 'Protocols' argument.
224void
225Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
226                              const IdentifierLocPair *ProtocolId,
227                              unsigned NumProtocols,
228                              llvm::SmallVectorImpl<DeclTy*> &Protocols) {
229  for (unsigned i = 0; i != NumProtocols; ++i) {
230    ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first];
231    if (!PDecl) {
232      Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
233        << ProtocolId[i].first;
234      continue;
235    }
236
237    // If this is a forward declaration and we are supposed to warn in this
238    // case, do it.
239    if (WarnOnDeclarations && PDecl->isForwardDecl())
240      Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
241        << ProtocolId[i].first;
242    Protocols.push_back(PDecl);
243  }
244}
245
246/// DiagnosePropertyMismatch - Compares two properties for their
247/// attributes and types and warns on a variety of inconsistencies.
248///
249void
250Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
251                               ObjCPropertyDecl *SuperProperty,
252                               const IdentifierInfo *inheritedName) {
253  ObjCPropertyDecl::PropertyAttributeKind CAttr =
254  Property->getPropertyAttributes();
255  ObjCPropertyDecl::PropertyAttributeKind SAttr =
256  SuperProperty->getPropertyAttributes();
257  if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
258      && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
259    Diag(Property->getLocation(), diag::warn_readonly_property)
260      << Property->getDeclName() << inheritedName;
261  if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
262      != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
263    Diag(Property->getLocation(), diag::warn_property_attribute)
264      << Property->getDeclName() << "copy" << inheritedName;
265  else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
266           != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
267    Diag(Property->getLocation(), diag::warn_property_attribute)
268      << Property->getDeclName() << "retain" << inheritedName;
269
270  if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
271      != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
272    Diag(Property->getLocation(), diag::warn_property_attribute)
273      << Property->getDeclName() << "atomic" << inheritedName;
274  if (Property->getSetterName() != SuperProperty->getSetterName())
275    Diag(Property->getLocation(), diag::warn_property_attribute)
276      << Property->getDeclName() << "setter" << inheritedName;
277  if (Property->getGetterName() != SuperProperty->getGetterName())
278    Diag(Property->getLocation(), diag::warn_property_attribute)
279      << Property->getDeclName() << "getter" << inheritedName;
280
281  if (Context.getCanonicalType(Property->getType()) !=
282          Context.getCanonicalType(SuperProperty->getType()))
283    Diag(Property->getLocation(), diag::warn_property_type)
284      << Property->getType() << inheritedName;
285
286}
287
288/// ComparePropertiesInBaseAndSuper - This routine compares property
289/// declarations in base and its super class, if any, and issues
290/// diagnostics in a variety of inconsistant situations.
291///
292void
293Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
294  ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
295  if (!SDecl)
296    return;
297  // FIXME: O(N^2)
298  for (ObjCInterfaceDecl::classprop_iterator S = SDecl->classprop_begin(),
299       E = SDecl->classprop_end(); S != E; ++S) {
300    ObjCPropertyDecl *SuperPDecl = (*S);
301    // Does property in super class has declaration in current class?
302    for (ObjCInterfaceDecl::classprop_iterator I = IDecl->classprop_begin(),
303         E = IDecl->classprop_end(); I != E; ++I) {
304      ObjCPropertyDecl *PDecl = (*I);
305      if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
306          DiagnosePropertyMismatch(PDecl, SuperPDecl,
307                                   SDecl->getIdentifier());
308    }
309  }
310}
311
312/// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list
313/// of properties declared in a protocol and adds them to the list
314/// of properties for current class if it is not there already.
315void
316Sema::MergeOneProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
317                                          ObjCProtocolDecl *PDecl) {
318  llvm::SmallVector<ObjCPropertyDecl*, 16> mergeProperties;
319  for (ObjCProtocolDecl::classprop_iterator P = PDecl->classprop_begin(),
320       E = PDecl->classprop_end(); P != E; ++P) {
321    ObjCPropertyDecl *Pr = (*P);
322    ObjCInterfaceDecl::classprop_iterator CP, CE;
323    // Is this property already in  class's list of properties?
324    for (CP = IDecl->classprop_begin(), CE = IDecl->classprop_end();
325         CP != CE; ++CP)
326      if ((*CP)->getIdentifier() == Pr->getIdentifier())
327        break;
328    if (CP == CE)
329      // Add this property to list of properties for thie class.
330      mergeProperties.push_back(Pr);
331    else
332      // Property protocol already exist in class. Diagnose any mismatch.
333      DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
334    }
335  IDecl->mergeProperties(&mergeProperties[0], mergeProperties.size());
336}
337
338/// MergeProtocolPropertiesIntoClass - This routine merges properties
339/// declared in 'MergeItsProtocols' objects (which can be a class or an
340/// inherited protocol into the list of properties for class 'IDecl'
341///
342
343void
344Sema::MergeProtocolPropertiesIntoClass(ObjCInterfaceDecl *IDecl,
345                                       DeclTy *MergeItsProtocols) {
346  Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols);
347  if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
348    for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
349         E = MDecl->protocol_end(); P != E; ++P)
350      // Merge properties of class (*P) into IDECL's
351      MergeOneProtocolPropertiesIntoClass(IDecl, *P);
352
353    // Go thru the list of protocols for this class and recursively merge
354    // their properties into this class as well.
355    for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
356         E = IDecl->protocol_end(); P != E; ++P)
357      MergeProtocolPropertiesIntoClass(IDecl, *P);
358  } else {
359    ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
360    for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
361         E = MD->protocol_end(); P != E; ++P)
362      MergeOneProtocolPropertiesIntoClass(IDecl, (*P));
363  }
364}
365
366/// ActOnForwardProtocolDeclaration -
367Action::DeclTy *
368Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
369                                      const IdentifierLocPair *IdentList,
370                                      unsigned NumElts) {
371  llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
372
373  for (unsigned i = 0; i != NumElts; ++i) {
374    IdentifierInfo *Ident = IdentList[i].first;
375    ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident];
376    if (PDecl == 0) // Not already seen?
377      PDecl = ObjCProtocolDecl::Create(Context, IdentList[i].second, Ident);
378
379    Protocols.push_back(PDecl);
380  }
381
382  ObjCForwardProtocolDecl *PDecl =
383    ObjCForwardProtocolDecl::Create(Context, AtProtocolLoc,
384                                    &Protocols[0], Protocols.size());
385
386  CheckObjCDeclScope(PDecl);
387  return PDecl;
388}
389
390Sema::DeclTy *Sema::
391ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
392                            IdentifierInfo *ClassName, SourceLocation ClassLoc,
393                            IdentifierInfo *CategoryName,
394                            SourceLocation CategoryLoc,
395                            DeclTy * const *ProtoRefs,
396                            unsigned NumProtoRefs,
397                            SourceLocation EndProtoLoc) {
398  ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
399
400  ObjCCategoryDecl *CDecl =
401    ObjCCategoryDecl::Create(Context, AtInterfaceLoc, CategoryName);
402  CDecl->setClassInterface(IDecl);
403
404  /// Check that class of this category is already completely declared.
405  if (!IDecl || IDecl->isForwardDecl())
406    Diag(ClassLoc, diag::err_undef_interface) << ClassName;
407  else {
408    /// Check for duplicate interface declaration for this category
409    ObjCCategoryDecl *CDeclChain;
410    for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
411         CDeclChain = CDeclChain->getNextClassCategory()) {
412      if (CategoryName && CDeclChain->getIdentifier() == CategoryName) {
413        Diag(CategoryLoc, diag::warn_dup_category_def)
414          << ClassName << CategoryName;
415        Diag(CDeclChain->getLocation(), diag::note_previous_definition);
416        break;
417      }
418    }
419    if (!CDeclChain)
420      CDecl->insertNextClassCategory();
421  }
422
423  if (NumProtoRefs) {
424    CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs);
425    CDecl->setLocEnd(EndProtoLoc);
426  }
427
428  CheckObjCDeclScope(CDecl);
429  return CDecl;
430}
431
432/// ActOnStartCategoryImplementation - Perform semantic checks on the
433/// category implementation declaration and build an ObjCCategoryImplDecl
434/// object.
435Sema::DeclTy *Sema::ActOnStartCategoryImplementation(
436                      SourceLocation AtCatImplLoc,
437                      IdentifierInfo *ClassName, SourceLocation ClassLoc,
438                      IdentifierInfo *CatName, SourceLocation CatLoc) {
439  ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName);
440  ObjCCategoryImplDecl *CDecl =
441    ObjCCategoryImplDecl::Create(Context, AtCatImplLoc, CatName, IDecl);
442  /// Check that class of this category is already completely declared.
443  if (!IDecl || IDecl->isForwardDecl())
444    Diag(ClassLoc, diag::err_undef_interface) << ClassName;
445
446  /// TODO: Check that CatName, category name, is not used in another
447  // implementation.
448  ObjCCategoryImpls.push_back(CDecl);
449
450  CheckObjCDeclScope(CDecl);
451  return CDecl;
452}
453
454Sema::DeclTy *Sema::ActOnStartClassImplementation(
455                      SourceLocation AtClassImplLoc,
456                      IdentifierInfo *ClassName, SourceLocation ClassLoc,
457                      IdentifierInfo *SuperClassname,
458                      SourceLocation SuperClassLoc) {
459  ObjCInterfaceDecl* IDecl = 0;
460  // Check for another declaration kind with the same name.
461  Decl *PrevDecl = LookupDecl(ClassName, Decl::IDNS_Ordinary, TUScope);
462  if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
463    Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
464    Diag(PrevDecl->getLocation(), diag::note_previous_definition);
465  }
466  else {
467    // Is there an interface declaration of this class; if not, warn!
468    IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
469    if (!IDecl)
470      Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
471  }
472
473  // Check that super class name is valid class name
474  ObjCInterfaceDecl* SDecl = 0;
475  if (SuperClassname) {
476    // Check if a different kind of symbol declared in this scope.
477    PrevDecl = LookupDecl(SuperClassname, Decl::IDNS_Ordinary, TUScope);
478    if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
479      Diag(SuperClassLoc, diag::err_redefinition_different_kind)
480        << SuperClassname;
481      Diag(PrevDecl->getLocation(), diag::note_previous_definition);
482    } else {
483      SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
484      if (!SDecl)
485        Diag(SuperClassLoc, diag::err_undef_superclass)
486          << SuperClassname << ClassName;
487      else if (IDecl && IDecl->getSuperClass() != SDecl) {
488        // This implementation and its interface do not have the same
489        // super class.
490        Diag(SuperClassLoc, diag::err_conflicting_super_class)
491          << SDecl->getDeclName();
492        Diag(SDecl->getLocation(), diag::note_previous_definition);
493      }
494    }
495  }
496
497  if (!IDecl) {
498    // Legacy case of @implementation with no corresponding @interface.
499    // Build, chain & install the interface decl into the identifier.
500
501    // FIXME: Do we support attributes on the @implementation? If so
502    // we should copy them over.
503    IDecl = ObjCInterfaceDecl::Create(Context, AtClassImplLoc, ClassName,
504                                      ClassLoc, false, true);
505    ObjCInterfaceDecls[ClassName] = IDecl;
506    IDecl->setSuperClass(SDecl);
507    IDecl->setLocEnd(ClassLoc);
508
509    // Remember that this needs to be removed when the scope is popped.
510    TUScope->AddDecl(IDecl);
511  }
512
513  ObjCImplementationDecl* IMPDecl =
514    ObjCImplementationDecl::Create(Context, AtClassImplLoc, ClassName,
515                                   IDecl, SDecl);
516
517  if (CheckObjCDeclScope(IMPDecl))
518    return IMPDecl;
519
520  // Check that there is no duplicate implementation of this class.
521  if (ObjCImplementations[ClassName])
522    // FIXME: Don't leak everything!
523    Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
524  else // add it to the list.
525    ObjCImplementations[ClassName] = IMPDecl;
526  return IMPDecl;
527}
528
529void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
530                                    ObjCIvarDecl **ivars, unsigned numIvars,
531                                    SourceLocation RBrace) {
532  assert(ImpDecl && "missing implementation decl");
533  ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(ImpDecl->getIdentifier());
534  if (!IDecl)
535    return;
536  /// Check case of non-existing @interface decl.
537  /// (legacy objective-c @implementation decl without an @interface decl).
538  /// Add implementations's ivar to the synthesize class's ivar list.
539  if (IDecl->ImplicitInterfaceDecl()) {
540    IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace);
541    return;
542  }
543  // If implementation has empty ivar list, just return.
544  if (numIvars == 0)
545    return;
546
547  assert(ivars && "missing @implementation ivars");
548
549  // Check interface's Ivar list against those in the implementation.
550  // names and types must match.
551  //
552  unsigned j = 0;
553  ObjCInterfaceDecl::ivar_iterator
554    IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
555  for (; numIvars > 0 && IVI != IVE; ++IVI) {
556    ObjCIvarDecl* ImplIvar = ivars[j++];
557    ObjCIvarDecl* ClsIvar = *IVI;
558    assert (ImplIvar && "missing implementation ivar");
559    assert (ClsIvar && "missing class ivar");
560    if (Context.getCanonicalType(ImplIvar->getType()) !=
561        Context.getCanonicalType(ClsIvar->getType())) {
562      Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
563        << ImplIvar->getIdentifier()
564        << ImplIvar->getType() << ClsIvar->getType();
565      Diag(ClsIvar->getLocation(), diag::note_previous_definition);
566    }
567    // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed
568    // as error.
569    else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
570      Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
571        << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
572      Diag(ClsIvar->getLocation(), diag::note_previous_definition);
573      return;
574    }
575    --numIvars;
576  }
577
578  if (numIvars > 0)
579    Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
580  else if (IVI != IVE)
581    Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
582}
583
584void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
585                               bool &IncompleteImpl) {
586  if (!IncompleteImpl) {
587    Diag(ImpLoc, diag::warn_incomplete_impl);
588    IncompleteImpl = true;
589  }
590  Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName();
591}
592
593void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
594                                       ObjCMethodDecl *IntfMethodDecl) {
595  bool err = false;
596  QualType ImpMethodQType =
597    Context.getCanonicalType(ImpMethodDecl->getResultType());
598  QualType IntfMethodQType =
599    Context.getCanonicalType(IntfMethodDecl->getResultType());
600  if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType))
601    err = true;
602  else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(),
603            IF=IntfMethodDecl->param_begin(),
604            EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) {
605    ImpMethodQType = Context.getCanonicalType((*IM)->getType());
606    IntfMethodQType = Context.getCanonicalType((*IF)->getType());
607    if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) {
608      err = true;
609      break;
610    }
611  }
612  if (err) {
613    Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types)
614    << ImpMethodDecl->getDeclName();
615    Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
616  }
617}
618
619/// FIXME: Type hierarchies in Objective-C can be deep. We could most
620/// likely improve the efficiency of selector lookups and type
621/// checking by associating with each protocol / interface / category
622/// the flattened instance tables. If we used an immutable set to keep
623/// the table then it wouldn't add significant memory cost and it
624/// would be handy for lookups.
625
626/// CheckProtocolMethodDefs - This routine checks unimplemented methods
627/// Declared in protocol, and those referenced by it.
628void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
629                                   ObjCProtocolDecl *PDecl,
630                                   bool& IncompleteImpl,
631                                   const llvm::DenseSet<Selector> &InsMap,
632                                   const llvm::DenseSet<Selector> &ClsMap,
633                                   ObjCInterfaceDecl *IDecl) {
634  ObjCInterfaceDecl *Super = IDecl->getSuperClass();
635
636  // If a method lookup fails locally we still need to look and see if
637  // the method was implemented by a base class or an inherited
638  // protocol. This lookup is slow, but occurs rarely in correct code
639  // and otherwise would terminate in a warning.
640
641  // check unimplemented instance methods.
642  for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
643       E = PDecl->instmeth_end(); I != E; ++I) {
644    ObjCMethodDecl *method = *I;
645    if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
646        !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
647        (!Super || !Super->lookupInstanceMethod(method->getSelector())))
648      WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
649  }
650  // check unimplemented class methods
651  for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(),
652       E = PDecl->classmeth_end(); I != E; ++I) {
653    ObjCMethodDecl *method = *I;
654    if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
655        !ClsMap.count(method->getSelector()) &&
656        (!Super || !Super->lookupClassMethod(method->getSelector())))
657      WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
658  }
659  // Check on this protocols's referenced protocols, recursively.
660  for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
661       E = PDecl->protocol_end(); PI != E; ++PI)
662    CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
663}
664
665void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl,
666                                     ObjCInterfaceDecl* IDecl) {
667  llvm::DenseSet<Selector> InsMap;
668  // Check and see if instance methods in class interface have been
669  // implemented in the implementation class.
670  for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(),
671       E = IMPDecl->instmeth_end(); I != E; ++I)
672    InsMap.insert((*I)->getSelector());
673
674  bool IncompleteImpl = false;
675  for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(),
676       E = IDecl->instmeth_end(); I != E; ++I)
677    if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector()))
678      WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
679    else if (!(*I)->isSynthesized()){
680      ObjCMethodDecl *ImpMethodDecl =
681        IMPDecl->getInstanceMethod((*I)->getSelector());
682      ObjCMethodDecl *IntfMethodDecl =
683        IDecl->getInstanceMethod((*I)->getSelector());
684      WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
685
686    }
687
688  llvm::DenseSet<Selector> ClsMap;
689  // Check and see if class methods in class interface have been
690  // implemented in the implementation class.
691  for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(),
692       E = IMPDecl->classmeth_end(); I != E; ++I)
693    ClsMap.insert((*I)->getSelector());
694
695  for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(),
696       E = IDecl->classmeth_end(); I != E; ++I)
697    if (!ClsMap.count((*I)->getSelector()))
698      WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
699    else {
700      ObjCMethodDecl *ImpMethodDecl =
701        IMPDecl->getClassMethod((*I)->getSelector());
702      ObjCMethodDecl *IntfMethodDecl =
703        IDecl->getClassMethod((*I)->getSelector());
704      WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
705    }
706
707
708  // Check the protocol list for unimplemented methods in the @implementation
709  // class.
710  const ObjCList<ObjCProtocolDecl> &Protocols =
711    IDecl->getReferencedProtocols();
712  for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
713       E = Protocols.end(); I != E; ++I)
714    CheckProtocolMethodDefs(IMPDecl->getLocation(), *I,
715                            IncompleteImpl, InsMap, ClsMap, IDecl);
716}
717
718/// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the
719/// category interface are implemented in the category @implementation.
720void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl,
721                                            ObjCCategoryDecl *CatClassDecl) {
722  llvm::DenseSet<Selector> InsMap;
723  // Check and see if instance methods in category interface have been
724  // implemented in its implementation class.
725  for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(),
726       E = CatImplDecl->instmeth_end(); I != E; ++I)
727    InsMap.insert((*I)->getSelector());
728
729  bool IncompleteImpl = false;
730  for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(),
731       E = CatClassDecl->instmeth_end(); I != E; ++I)
732    if (!InsMap.count((*I)->getSelector()))
733      WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
734    else {
735      ObjCMethodDecl *ImpMethodDecl =
736        CatImplDecl->getInstanceMethod((*I)->getSelector());
737      ObjCMethodDecl *IntfMethodDecl =
738        CatClassDecl->getInstanceMethod((*I)->getSelector());
739      WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
740    }
741
742  llvm::DenseSet<Selector> ClsMap;
743  // Check and see if class methods in category interface have been
744  // implemented in its implementation class.
745  for (ObjCCategoryImplDecl::classmeth_iterator
746       I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end();
747       I != E; ++I)
748    ClsMap.insert((*I)->getSelector());
749
750  for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(),
751       E = CatClassDecl->classmeth_end(); I != E; ++I)
752    if (!ClsMap.count((*I)->getSelector()))
753      WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl);
754    else {
755      ObjCMethodDecl *ImpMethodDecl =
756        CatImplDecl->getClassMethod((*I)->getSelector());
757      ObjCMethodDecl *IntfMethodDecl =
758        CatClassDecl->getClassMethod((*I)->getSelector());
759      WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
760    }
761  // Check the protocol list for unimplemented methods in the @implementation
762  // class.
763  for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(),
764       E = CatClassDecl->protocol_end(); PI != E; ++PI)
765    CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl,
766                            InsMap, ClsMap, CatClassDecl->getClassInterface());
767}
768
769/// ActOnForwardClassDeclaration -
770Action::DeclTy *
771Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
772                                   IdentifierInfo **IdentList, unsigned NumElts)
773{
774  llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
775
776  for (unsigned i = 0; i != NumElts; ++i) {
777    // Check for another declaration kind with the same name.
778    Decl *PrevDecl = LookupDecl(IdentList[i], Decl::IDNS_Ordinary, TUScope);
779    if (PrevDecl && isTemplateParameterDecl(PrevDecl)) {
780      // Maybe we will complain about the shadowed template parameter.
781      DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
782      // Just pretend that we didn't see the previous declaration.
783      PrevDecl = 0;
784    }
785
786    if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
787      // GCC apparently allows the following idiom:
788      //
789      // typedef NSObject < XCElementTogglerP > XCElementToggler;
790      // @class XCElementToggler;
791      //
792      // FIXME: Make an extension?
793      TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
794      if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
795        Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
796        Diag(PrevDecl->getLocation(), diag::note_previous_definition);
797      }
798    }
799    ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
800    if (!IDecl) {  // Not already seen?  Make a forward decl.
801      IDecl = ObjCInterfaceDecl::Create(Context, AtClassLoc, IdentList[i],
802                                        SourceLocation(), true);
803      ObjCInterfaceDecls[IdentList[i]] = IDecl;
804
805      // Remember that this needs to be removed when the scope is popped.
806      TUScope->AddDecl(IDecl);
807    }
808
809    Interfaces.push_back(IDecl);
810  }
811
812  ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, AtClassLoc,
813                                               &Interfaces[0],
814                                               Interfaces.size());
815
816  CheckObjCDeclScope(CDecl);
817  return CDecl;
818}
819
820
821/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
822/// returns true, or false, accordingly.
823/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
824bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
825                                      const ObjCMethodDecl *PrevMethod,
826                                      bool matchBasedOnSizeAndAlignment) {
827  QualType T1 = Context.getCanonicalType(Method->getResultType());
828  QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
829
830  if (T1 != T2) {
831    // The result types are different.
832    if (!matchBasedOnSizeAndAlignment)
833      return false;
834    // Incomplete types don't have a size and alignment.
835    if (T1->isIncompleteType() || T2->isIncompleteType())
836      return false;
837    // Check is based on size and alignment.
838    if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
839      return false;
840  }
841  for (unsigned i = 0, e = Method->getNumParams(); i != e; ++i) {
842    T1 = Context.getCanonicalType(Method->getParamDecl(i)->getType());
843    T2 = Context.getCanonicalType(PrevMethod->getParamDecl(i)->getType());
844    if (T1 != T2) {
845      // The result types are different.
846      if (!matchBasedOnSizeAndAlignment)
847        return false;
848      // Incomplete types don't have a size and alignment.
849      if (T1->isIncompleteType() || T2->isIncompleteType())
850        return false;
851      // Check is based on size and alignment.
852      if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
853        return false;
854    }
855  }
856  return true;
857}
858
859void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
860  ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()];
861  if (!FirstMethod.Method) {
862    // Haven't seen a method with this selector name yet - add it.
863    FirstMethod.Method = Method;
864    FirstMethod.Next = 0;
865  } else {
866    // We've seen a method with this name, now check the type signature(s).
867    bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
868
869    for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
870         Next = Next->Next)
871      match = MatchTwoMethodDeclarations(Method, Next->Method);
872
873    if (!match) {
874      // We have a new signature for an existing method - add it.
875      // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
876      FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);;
877    }
878  }
879}
880
881// FIXME: Finish implementing -Wno-strict-selector-match.
882ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
883                                                       SourceRange R) {
884  ObjCMethodList &MethList = InstanceMethodPool[Sel];
885  bool issueWarning = false;
886
887  if (MethList.Method && MethList.Next) {
888    for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
889      // This checks if the methods differ by size & alignment.
890      if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
891        issueWarning = true;
892  }
893  if (issueWarning && (MethList.Method && MethList.Next)) {
894    Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
895    Diag(MethList.Method->getLocStart(), diag::note_using_decl)
896      << MethList.Method->getSourceRange();
897    for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
898      Diag(Next->Method->getLocStart(), diag::note_also_found_decl)
899        << Next->Method->getSourceRange();
900  }
901  return MethList.Method;
902}
903
904void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
905  ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()];
906  if (!FirstMethod.Method) {
907    // Haven't seen a method with this selector name yet - add it.
908    FirstMethod.Method = Method;
909    FirstMethod.Next = 0;
910  } else {
911    // We've seen a method with this name, now check the type signature(s).
912    bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
913
914    for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
915         Next = Next->Next)
916      match = MatchTwoMethodDeclarations(Method, Next->Method);
917
918    if (!match) {
919      // We have a new signature for an existing method - add it.
920      // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
921      struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next);
922      FirstMethod.Next = OMI;
923    }
924  }
925}
926
927/// diagnosePropertySetterGetterMismatch - Make sure that use-defined
928/// setter/getter methods have the property type and issue diagnostics
929/// if they don't.
930///
931void
932Sema::diagnosePropertySetterGetterMismatch(ObjCPropertyDecl *property,
933                                           const ObjCMethodDecl *GetterMethod,
934                                           const ObjCMethodDecl *SetterMethod) {
935  if (GetterMethod &&
936      GetterMethod->getResultType() != property->getType())
937    Diag(property->getLocation(),
938         diag::err_accessor_property_type_mismatch)
939      << property->getDeclName()
940      << GetterMethod->getSelector().getAsIdentifierInfo();
941
942  if (SetterMethod) {
943    if (SetterMethod->getResultType() != Context.VoidPtrTy)
944      Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
945    if (SetterMethod->getNumParams() != 1 ||
946        (SetterMethod->getParamDecl(0)->getType() != property->getType()))
947      Diag(property->getLocation(),
948           diag::err_accessor_property_type_mismatch)
949        << property->getDeclName()
950        << SetterMethod->getSelector().getAsIdentifierInfo();
951  }
952}
953
954// Note: For class/category implemenations, allMethods/allProperties is
955// always null.
956void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl,
957                      DeclTy **allMethods, unsigned allNum,
958                      DeclTy **allProperties, unsigned pNum) {
959  Decl *ClassDecl = static_cast<Decl *>(classDecl);
960
961  // FIXME: If we don't have a ClassDecl, we have an error. We should consider
962  // always passing in a decl. If the decl has an error, isInvalidDecl()
963  // should be true.
964  if (!ClassDecl)
965    return;
966
967  llvm::SmallVector<ObjCMethodDecl*, 32> insMethods;
968  llvm::SmallVector<ObjCMethodDecl*, 16> clsMethods;
969
970  llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
971  llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
972
973  bool isInterfaceDeclKind =
974        isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
975         || isa<ObjCProtocolDecl>(ClassDecl);
976  bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
977
978  if (pNum != 0) {
979    if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl))
980      IDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
981    else if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
982      CDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
983    else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(ClassDecl))
984      PDecl->addProperties((ObjCPropertyDecl**)allProperties, pNum);
985    else
986      assert(false && "ActOnAtEnd - property declaration misplaced");
987  }
988
989  for (unsigned i = 0; i < allNum; i++ ) {
990    ObjCMethodDecl *Method =
991      cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i]));
992
993    if (!Method) continue;  // Already issued a diagnostic.
994    if (Method->isInstance()) {
995      /// Check for instance method of the same name with incompatible types
996      const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
997      bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
998                              : false;
999      if (isInterfaceDeclKind && PrevMethod && !match
1000          || checkIdenticalMethods && match) {
1001          Diag(Method->getLocation(), diag::err_duplicate_method_decl)
1002            << Method->getDeclName();
1003          Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1004      } else {
1005        insMethods.push_back(Method);
1006        InsMap[Method->getSelector()] = Method;
1007        /// The following allows us to typecheck messages to "id".
1008        AddInstanceMethodToGlobalPool(Method);
1009      }
1010    }
1011    else {
1012      /// Check for class method of the same name with incompatible types
1013      const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
1014      bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
1015                              : false;
1016      if (isInterfaceDeclKind && PrevMethod && !match
1017          || checkIdenticalMethods && match) {
1018        Diag(Method->getLocation(), diag::err_duplicate_method_decl)
1019          << Method->getDeclName();
1020        Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1021      } else {
1022        clsMethods.push_back(Method);
1023        ClsMap[Method->getSelector()] = Method;
1024        /// The following allows us to typecheck messages to "Class".
1025        AddFactoryMethodToGlobalPool(Method);
1026      }
1027    }
1028  }
1029  // Save the size so we can detect if we've added any property methods.
1030  unsigned int insMethodsSizePriorToPropAdds = insMethods.size();
1031  unsigned int clsMethodsSizePriorToPropAdds = clsMethods.size();
1032
1033  if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
1034    // Compares properties declared in this class to those of its
1035    // super class.
1036    ComparePropertiesInBaseAndSuper(I);
1037    MergeProtocolPropertiesIntoClass(I, I);
1038    for (ObjCInterfaceDecl::classprop_iterator i = I->classprop_begin(),
1039         e = I->classprop_end(); i != e; ++i) {
1040      diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1041                                           InsMap[(*i)->getSetterName()]);
1042      I->addPropertyMethods(Context, *i, insMethods, InsMap);
1043    }
1044    I->addMethods(&insMethods[0], insMethods.size(),
1045                  &clsMethods[0], clsMethods.size(), AtEndLoc);
1046
1047  } else if (ObjCProtocolDecl *P = dyn_cast<ObjCProtocolDecl>(ClassDecl)) {
1048    for (ObjCProtocolDecl::classprop_iterator i = P->classprop_begin(),
1049         e = P->classprop_end(); i != e; ++i) {
1050      diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1051                                           InsMap[(*i)->getSetterName()]);
1052      P->addPropertyMethods(Context, *i, insMethods, InsMap);
1053    }
1054    P->addMethods(&insMethods[0], insMethods.size(),
1055                  &clsMethods[0], clsMethods.size(), AtEndLoc);
1056  }
1057  else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1058    // FIXME: Need to compare properties to those in interface?
1059
1060    // FIXME: If we merge properties into class we should probably
1061    // merge them into category as well?
1062    for (ObjCCategoryDecl::classprop_iterator i = C->classprop_begin(),
1063         e = C->classprop_end(); i != e; ++i) {
1064      diagnosePropertySetterGetterMismatch((*i), InsMap[(*i)->getGetterName()],
1065                                           InsMap[(*i)->getSetterName()]);
1066      C->addPropertyMethods(Context, *i, insMethods, InsMap);
1067    }
1068    C->addMethods(&insMethods[0], insMethods.size(),
1069                  &clsMethods[0], clsMethods.size(), AtEndLoc);
1070  }
1071  else if (ObjCImplementationDecl *IC =
1072                dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
1073    IC->setLocEnd(AtEndLoc);
1074    if (ObjCInterfaceDecl* IDecl = getObjCInterfaceDecl(IC->getIdentifier()))
1075      ImplMethodsVsClassMethods(IC, IDecl);
1076  } else {
1077    ObjCCategoryImplDecl* CatImplClass = cast<ObjCCategoryImplDecl>(ClassDecl);
1078    CatImplClass->setLocEnd(AtEndLoc);
1079    ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface();
1080    // Find category interface decl and then check that all methods declared
1081    // in this interface are implemented in the category @implementation.
1082    if (IDecl) {
1083      for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
1084           Categories; Categories = Categories->getNextClassCategory()) {
1085        if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
1086          ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories);
1087          break;
1088        }
1089      }
1090    }
1091  }
1092  // Add any synthesized methods to the global pool. This allows us to
1093  // handle the following, which is supported by GCC (and part of the design).
1094  //
1095  // @interface Foo
1096  // @property double bar;
1097  // @end
1098  //
1099  // void thisIsUnfortunate() {
1100  //   id foo;
1101  //   double bar = [foo bar];
1102  // }
1103  //
1104  if (insMethodsSizePriorToPropAdds < insMethods.size())
1105    for (unsigned i = insMethodsSizePriorToPropAdds; i < insMethods.size(); i++)
1106      AddInstanceMethodToGlobalPool(insMethods[i]);
1107  if (clsMethodsSizePriorToPropAdds < clsMethods.size())
1108    for (unsigned i = clsMethodsSizePriorToPropAdds; i < clsMethods.size(); i++)
1109      AddFactoryMethodToGlobalPool(clsMethods[i]);
1110}
1111
1112
1113/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1114/// objective-c's type qualifier from the parser version of the same info.
1115static Decl::ObjCDeclQualifier
1116CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1117  Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1118  if (PQTVal & ObjCDeclSpec::DQ_In)
1119    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1120  if (PQTVal & ObjCDeclSpec::DQ_Inout)
1121    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1122  if (PQTVal & ObjCDeclSpec::DQ_Out)
1123    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1124  if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1125    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1126  if (PQTVal & ObjCDeclSpec::DQ_Byref)
1127    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1128  if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1129    ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
1130
1131  return ret;
1132}
1133
1134Sema::DeclTy *Sema::ActOnMethodDeclaration(
1135    SourceLocation MethodLoc, SourceLocation EndLoc,
1136    tok::TokenKind MethodType, DeclTy *classDecl,
1137    ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
1138    Selector Sel,
1139    // optional arguments. The number of types/arguments is obtained
1140    // from the Sel.getNumArgs().
1141    ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames,
1142    AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1143    bool isVariadic) {
1144  Decl *ClassDecl = static_cast<Decl*>(classDecl);
1145
1146  // Make sure we can establish a context for the method.
1147  if (!ClassDecl) {
1148    Diag(MethodLoc, diag::error_missing_method_context);
1149    return 0;
1150  }
1151  QualType resultDeclType;
1152
1153  if (ReturnType)
1154    resultDeclType = QualType::getFromOpaquePtr(ReturnType);
1155  else // get the type for "id".
1156    resultDeclType = Context.getObjCIdType();
1157
1158  ObjCMethodDecl* ObjCMethod =
1159    ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
1160                           ClassDecl,
1161                           MethodType == tok::minus, isVariadic,
1162                           false,
1163                           MethodDeclKind == tok::objc_optional ?
1164                           ObjCMethodDecl::Optional :
1165                           ObjCMethodDecl::Required);
1166
1167  llvm::SmallVector<ParmVarDecl*, 16> Params;
1168
1169  for (unsigned i = 0; i < Sel.getNumArgs(); i++) {
1170    // FIXME: arg->AttrList must be stored too!
1171    QualType argType;
1172
1173    if (ArgTypes[i])
1174      argType = QualType::getFromOpaquePtr(ArgTypes[i]);
1175    else
1176      argType = Context.getObjCIdType();
1177    ParmVarDecl* Param = ParmVarDecl::Create(Context, ObjCMethod,
1178                                             SourceLocation(/*FIXME*/),
1179                                             ArgNames[i], argType,
1180                                             VarDecl::None, 0, 0);
1181    Param->setObjCDeclQualifier(
1182      CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier()));
1183    Params.push_back(Param);
1184  }
1185
1186  ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs());
1187  ObjCMethod->setObjCDeclQualifier(
1188    CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1189  const ObjCMethodDecl *PrevMethod = 0;
1190
1191  if (AttrList)
1192    ProcessDeclAttributeList(ObjCMethod, AttrList);
1193
1194  // For implementations (which can be very "coarse grain"), we add the
1195  // method now. This allows the AST to implement lookup methods that work
1196  // incrementally (without waiting until we parse the @end). It also allows
1197  // us to flag multiple declaration errors as they occur.
1198  if (ObjCImplementationDecl *ImpDecl =
1199        dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
1200    if (MethodType == tok::minus) {
1201      PrevMethod = ImpDecl->getInstanceMethod(Sel);
1202      ImpDecl->addInstanceMethod(ObjCMethod);
1203    } else {
1204      PrevMethod = ImpDecl->getClassMethod(Sel);
1205      ImpDecl->addClassMethod(ObjCMethod);
1206    }
1207  }
1208  else if (ObjCCategoryImplDecl *CatImpDecl =
1209            dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
1210    if (MethodType == tok::minus) {
1211      PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1212      CatImpDecl->addInstanceMethod(ObjCMethod);
1213    } else {
1214      PrevMethod = CatImpDecl->getClassMethod(Sel);
1215      CatImpDecl->addClassMethod(ObjCMethod);
1216    }
1217  }
1218  if (PrevMethod) {
1219    // You can never have two method definitions with the same name.
1220    Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
1221      << ObjCMethod->getDeclName();
1222    Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
1223  }
1224  return ObjCMethod;
1225}
1226
1227void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
1228                                       SourceLocation Loc,
1229                                       unsigned &Attributes) {
1230  // FIXME: Improve the reported location.
1231
1232  // readonly and readwrite conflict.
1233  if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1234      (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) {
1235    Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1236      << "readonly" << "readwrite";
1237    Attributes &= ~ObjCDeclSpec::DQ_PR_readonly;
1238  }
1239
1240  // Check for copy or retain on non-object types.
1241  if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1242      !Context.isObjCObjectPointerType(PropertyTy)) {
1243    Diag(Loc, diag::err_objc_property_requires_object)
1244      << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1245    Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1246  }
1247
1248  // Check for more than one of { assign, copy, retain }.
1249  if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1250    if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1251      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1252        << "assign" << "copy";
1253      Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1254    }
1255    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1256      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1257        << "assign" << "retain";
1258      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1259    }
1260  } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1261    if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1262      Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1263        << "copy" << "retain";
1264      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1265    }
1266  }
1267
1268  // Warn if user supplied no assignment attribute, property is
1269  // readwrite, and this is an object type.
1270  if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1271                      ObjCDeclSpec::DQ_PR_retain)) &&
1272      !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1273      Context.isObjCObjectPointerType(PropertyTy)) {
1274    // Skip this warning in gc-only mode.
1275    if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1276      Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1277
1278    // If non-gc code warn that this is likely inappropriate.
1279    if (getLangOptions().getGCMode() == LangOptions::NonGC)
1280      Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1281
1282    // FIXME: Implement warning dependent on NSCopying being
1283    // implemented. See also:
1284    // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1285    // (please trim this list while you are at it).
1286  }
1287}
1288
1289Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
1290                                  FieldDeclarator &FD,
1291                                  ObjCDeclSpec &ODS,
1292                                  Selector GetterSel,
1293                                  Selector SetterSel,
1294                                  DeclTy *ClassCategory,
1295                                  bool *isOverridingProperty,
1296                                  tok::ObjCKeywordKind MethodImplKind) {
1297  unsigned Attributes = ODS.getPropertyAttributes();
1298  bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
1299                      // default is readwrite!
1300                      !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
1301  // property is defaulted to 'assign' if it is readwrite and is
1302  // not retain or copy
1303  bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
1304                   (isReadWrite &&
1305                    !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1306                    !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
1307  QualType T = GetTypeForDeclarator(FD.D, S);
1308  Decl *ClassDecl = static_cast<Decl *>(ClassCategory);
1309
1310  // May modify Attributes.
1311  CheckObjCPropertyAttributes(T, AtLoc, Attributes);
1312
1313  if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
1314    if (!CDecl->getIdentifier()) {
1315      // This is an anonymous category. property requires special
1316      // handling.
1317      if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) {
1318        if (ObjCPropertyDecl *PIDecl =
1319            ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) {
1320          // property 'PIDecl's readonly attribute will be over-ridden
1321          // with anonymous category's readwrite property attribute!
1322          unsigned PIkind = PIDecl->getPropertyAttributes();
1323          if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
1324            if ((Attributes & ObjCPropertyDecl::OBJC_PR_retain) !=
1325                (PIkind & ObjCPropertyDecl::OBJC_PR_retain) ||
1326                (Attributes & ObjCPropertyDecl::OBJC_PR_copy) !=
1327                (PIkind & ObjCPropertyDecl::OBJC_PR_copy) ||
1328                (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) !=
1329                (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic))
1330              Diag(AtLoc, diag::warn_property_attr_mismatch);
1331            PIDecl->makeitReadWriteAttribute();
1332            if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1333              PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1334            if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1335              PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1336            PIDecl->setSetterName(SetterSel);
1337            // FIXME: use a common routine with addPropertyMethods.
1338            ObjCMethodDecl *SetterDecl =
1339              ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel,
1340                                     Context.VoidTy,
1341                                     ICDecl,
1342                                     true, false, true,
1343                                     ObjCMethodDecl::Required);
1344            ParmVarDecl *Argument = ParmVarDecl::Create(Context,
1345                                                        SetterDecl,
1346                                                        SourceLocation(),
1347                                                        FD.D.getIdentifier(),
1348                                                        T,
1349                                                        VarDecl::None,
1350                                                        0, 0);
1351            SetterDecl->setMethodParams(&Argument, 1);
1352            PIDecl->setSetterMethodDecl(SetterDecl);
1353          }
1354          else
1355            Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName();
1356          *isOverridingProperty = true;
1357          return 0;
1358        }
1359        // No matching property found in the main class. Just fall thru
1360        // and add property to the anonymous category. It looks like
1361	// it works as is. This category becomes just like a category
1362	// for its primary class.
1363      } else {
1364          Diag(CDecl->getLocation(), diag::err_continuation_class);
1365          *isOverridingProperty = true;
1366          return 0;
1367      }
1368    }
1369
1370  ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, AtLoc,
1371                                                     FD.D.getIdentifier(), T);
1372  // Regardless of setter/getter attribute, we save the default getter/setter
1373  // selector names in anticipation of declaration of setter/getter methods.
1374  PDecl->setGetterName(GetterSel);
1375  PDecl->setSetterName(SetterSel);
1376
1377  if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
1378    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
1379
1380  if (Attributes & ObjCDeclSpec::DQ_PR_getter)
1381    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
1382
1383  if (Attributes & ObjCDeclSpec::DQ_PR_setter)
1384    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
1385
1386  if (isReadWrite)
1387    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
1388
1389  if (Attributes & ObjCDeclSpec::DQ_PR_retain)
1390    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
1391
1392  if (Attributes & ObjCDeclSpec::DQ_PR_copy)
1393    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
1394
1395  if (isAssign)
1396    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
1397
1398  if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
1399    PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
1400
1401  if (MethodImplKind == tok::objc_required)
1402    PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
1403  else if (MethodImplKind == tok::objc_optional)
1404    PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
1405
1406  return PDecl;
1407}
1408
1409/// ActOnPropertyImplDecl - This routine performs semantic checks and
1410/// builds the AST node for a property implementation declaration; declared
1411/// as @synthesize or @dynamic.
1412///
1413Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
1414                                          SourceLocation PropertyLoc,
1415                                          bool Synthesize,
1416                                          DeclTy *ClassCatImpDecl,
1417                                          IdentifierInfo *PropertyId,
1418                                          IdentifierInfo *PropertyIvar) {
1419  Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl);
1420  // Make sure we have a context for the property implementation declaration.
1421  if (!ClassImpDecl) {
1422    Diag(AtLoc, diag::error_missing_property_context);
1423    return 0;
1424  }
1425  ObjCPropertyDecl *property = 0;
1426  ObjCInterfaceDecl* IDecl = 0;
1427  // Find the class or category class where this property must have
1428  // a declaration.
1429  ObjCImplementationDecl *IC = 0;
1430  ObjCCategoryImplDecl* CatImplClass = 0;
1431  if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1432    IDecl = getObjCInterfaceDecl(IC->getIdentifier());
1433    // We always synthesize an interface for an implementation
1434    // without an interface decl. So, IDecl is always non-zero.
1435    assert(IDecl &&
1436           "ActOnPropertyImplDecl - @implementation without @interface");
1437
1438    // Look for this property declaration in the @implementation's @interface
1439    property = IDecl->FindPropertyDeclaration(PropertyId);
1440    if (!property) {
1441      Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
1442      return 0;
1443    }
1444  }
1445  else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1446    if (Synthesize) {
1447      Diag(AtLoc, diag::error_synthesize_category_decl);
1448      return 0;
1449    }
1450    IDecl = CatImplClass->getClassInterface();
1451    if (!IDecl) {
1452      Diag(AtLoc, diag::error_missing_property_interface);
1453      return 0;
1454    }
1455    ObjCCategoryDecl *Category =
1456      IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1457
1458    // If category for this implementation not found, it is an error which
1459    // has already been reported eralier.
1460    if (!Category)
1461      return 0;
1462    // Look for this property declaration in @implementation's category
1463    property = Category->FindPropertyDeclaration(PropertyId);
1464    if (!property) {
1465      Diag(PropertyLoc, diag::error_bad_category_property_decl)
1466        << Category->getDeclName();
1467      return 0;
1468    }
1469  }
1470  else {
1471    Diag(AtLoc, diag::error_bad_property_context);
1472    return 0;
1473  }
1474  ObjCIvarDecl *Ivar = 0;
1475  // Check that we have a valid, previously declared ivar for @synthesize
1476  if (Synthesize) {
1477    // @synthesize
1478    if (!PropertyIvar)
1479      PropertyIvar = PropertyId;
1480    // Check that this is a previously declared 'ivar' in 'IDecl' interface
1481    Ivar = IDecl->FindIvarDeclaration(PropertyIvar);
1482    if (!Ivar) {
1483      Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
1484      return 0;
1485    }
1486    QualType PropType = Context.getCanonicalType(property->getType());
1487    QualType IvarType = Context.getCanonicalType(Ivar->getType());
1488
1489    // Check that type of property and its ivar are type compatible.
1490    if (PropType != IvarType) {
1491      if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
1492        Diag(PropertyLoc, diag::error_property_ivar_type)
1493          << property->getDeclName() << Ivar->getDeclName();
1494        return 0;
1495      }
1496    }
1497  } else if (PropertyIvar) {
1498    // @dynamic
1499    Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
1500    return 0;
1501  }
1502  assert (property && "ActOnPropertyImplDecl - property declaration missing");
1503  ObjCPropertyImplDecl *PIDecl =
1504    ObjCPropertyImplDecl::Create(Context, AtLoc, PropertyLoc, property,
1505                                 (Synthesize ?
1506                                  ObjCPropertyImplDecl::Synthesize
1507                                  : ObjCPropertyImplDecl::Dynamic),
1508                                 Ivar);
1509  if (IC) {
1510    if (Synthesize)
1511      if (ObjCPropertyImplDecl *PPIDecl =
1512          IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1513        Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1514          << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1515          << PropertyIvar;
1516        Diag(PPIDecl->getLocation(), diag::note_previous_use);
1517      }
1518
1519    if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) {
1520      Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1521      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1522      return 0;
1523    }
1524    IC->addPropertyImplementation(PIDecl);
1525  }
1526  else {
1527    if (Synthesize)
1528      if (ObjCPropertyImplDecl *PPIDecl =
1529          CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1530        Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1531          << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1532          << PropertyIvar;
1533        Diag(PPIDecl->getLocation(), diag::note_previous_use);
1534      }
1535
1536    if (ObjCPropertyImplDecl *PPIDecl =
1537          CatImplClass->FindPropertyImplDecl(PropertyId)) {
1538      Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1539      Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1540      return 0;
1541    }
1542    CatImplClass->addPropertyImplementation(PIDecl);
1543  }
1544
1545  return PIDecl;
1546}
1547
1548bool Sema::CheckObjCDeclScope(Decl *D)
1549{
1550  if (isa<TranslationUnitDecl>(CurContext))
1551    return false;
1552
1553  Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1554  D->setInvalidDecl();
1555
1556  return true;
1557}
1558