Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 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" |
Daniel Dunbar | 8d03cbe | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 17 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 20 | /// ObjCActOnStartOfMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 21 | /// and user declared, in the method definition's AST. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 22 | void Sema::ObjCActOnStartOfMethodDef(Scope *FnBodyScope, DeclTy *D) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 23 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 24 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>((Decl *)D); |
| 25 | |
| 26 | // If we don't have a valid method decl, simply return. |
| 27 | if (!MDecl) |
| 28 | return; |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 29 | |
| 30 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 31 | if (MDecl->isInstanceMethod()) |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 32 | AddInstanceMethodToGlobalPool(MDecl); |
| 33 | else |
| 34 | AddFactoryMethodToGlobalPool(MDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 35 | |
| 36 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 37 | PushDeclContext(FnBodyScope, MDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 38 | |
| 39 | // Create Decl objects for each parameter, entrring them in the scope for |
| 40 | // binding to their use. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 41 | |
| 42 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 43 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 44 | |
Daniel Dunbar | eaf91c3 | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 45 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 46 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 48 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 49 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 50 | E = MDecl->param_end(); PI != E; ++PI) |
| 51 | if ((*PI)->getIdentifier()) |
| 52 | PushOnScopeChains(*PI, FnBodyScope); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 55 | Sema::DeclTy *Sema:: |
| 56 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 57 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 58 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Chris Lattner | ae1ae49 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 59 | DeclTy * const *ProtoRefs, unsigned NumProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 60 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 61 | assert(ClassName && "Missing class identifier"); |
| 62 | |
| 63 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 64 | NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 65 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 66 | // Maybe we will complain about the shadowed template parameter. |
| 67 | DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl); |
| 68 | // Just pretend that we didn't see the previous declaration. |
| 69 | PrevDecl = 0; |
| 70 | } |
| 71 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 72 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 73 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 74 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 77 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 78 | if (IDecl) { |
| 79 | // Class already seen. Is it a forward declaration? |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 80 | if (!IDecl->isForwardDecl()) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 81 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); |
Chris Lattner | 5b25065 | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 82 | Diag(IDecl->getLocation(), diag::note_previous_definition); |
| 83 | |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 84 | // Return the previous class interface. |
| 85 | // FIXME: don't leak the objects passed in! |
| 86 | return IDecl; |
| 87 | } else { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 88 | IDecl->setLocation(AtInterfaceLoc); |
| 89 | IDecl->setForwardDecl(false); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 91 | } else { |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 92 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 93 | ClassName, ClassLoc); |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 94 | if (AttrList) |
| 95 | ProcessDeclAttributeList(IDecl, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 96 | |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 97 | ObjCInterfaceDecls[ClassName] = IDecl; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 98 | // FIXME: PushOnScopeChains |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 99 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 100 | // Remember that this needs to be removed when the scope is popped. |
| 101 | TUScope->AddDecl(IDecl); |
| 102 | } |
| 103 | |
| 104 | if (SuperName) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 105 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 106 | PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 107 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 108 | ObjCInterfaceDecl *SuperClassDecl = |
| 109 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 110 | |
| 111 | // Diagnose classes that inherit from deprecated classes. |
| 112 | if (SuperClassDecl) |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 113 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 114 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 115 | if (PrevDecl && SuperClassDecl == 0) { |
| 116 | // The previous declaration was not a class decl. Check if we have a |
| 117 | // typedef. If we do, get the underlying class type. |
| 118 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 119 | QualType T = TDecl->getUnderlyingType(); |
| 120 | if (T->isObjCInterfaceType()) { |
| 121 | if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) |
| 122 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 123 | } |
| 124 | } |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 125 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 126 | // This handles the following case: |
| 127 | // |
| 128 | // typedef int SuperClass; |
| 129 | // @interface MyClass : SuperClass {} @end |
| 130 | // |
| 131 | if (!SuperClassDecl) { |
| 132 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 133 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 134 | } |
| 135 | } |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 136 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 137 | if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 138 | if (!SuperClassDecl) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 139 | Diag(SuperLoc, diag::err_undef_superclass) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 140 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 141 | else if (SuperClassDecl->isForwardDecl()) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 142 | Diag(SuperLoc, diag::err_undef_superclass) |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 143 | << SuperClassDecl->getDeclName() << ClassName |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 144 | << SourceRange(AtInterfaceLoc, ClassLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 145 | } |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 146 | IDecl->setSuperClass(SuperClassDecl); |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 147 | IDecl->setSuperClassLoc(SuperLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 148 | IDecl->setLocEnd(SuperLoc); |
| 149 | } else { // we have a root class. |
| 150 | IDecl->setLocEnd(ClassLoc); |
| 151 | } |
| 152 | |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 153 | /// Check then save referenced protocols. |
Chris Lattner | ae1ae49 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 154 | if (NumProtoRefs) { |
| 155 | IDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 156 | IDecl->setLocEnd(EndProtoLoc); |
| 157 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 158 | |
| 159 | CheckObjCDeclScope(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 160 | return IDecl; |
| 161 | } |
| 162 | |
| 163 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 164 | /// @compatibility_alias declaration. It sets up the alias relationships. |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 165 | Sema::DeclTy *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
| 166 | IdentifierInfo *AliasName, |
| 167 | SourceLocation AliasLocation, |
| 168 | IdentifierInfo *ClassName, |
| 169 | SourceLocation ClassLocation) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 170 | // Look for previous declaration of alias name |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 171 | NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 172 | if (ADecl) { |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 173 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 174 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 175 | else |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 176 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 177 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | // Check for class declaration |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 181 | NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 182 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { |
| 183 | QualType T = TDecl->getUnderlyingType(); |
| 184 | if (T->isObjCInterfaceType()) { |
| 185 | if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) { |
| 186 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 187 | CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 191 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 192 | if (CDecl == 0) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 193 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 194 | if (CDeclU) |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 195 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 198 | |
| 199 | // Everything checked out, instantiate a new alias declaration AST. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 200 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 201 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 202 | |
| 203 | ObjCAliasDecls[AliasName] = AliasDecl; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 204 | |
| 205 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 206 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 207 | if (!CheckObjCDeclScope(AliasDecl)) |
| 208 | TUScope->AddDecl(AliasDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 210 | return AliasDecl; |
| 211 | } |
| 212 | |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 213 | Sema::DeclTy * |
| 214 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 215 | IdentifierInfo *ProtocolName, |
| 216 | SourceLocation ProtocolLoc, |
| 217 | DeclTy * const *ProtoRefs, |
| 218 | unsigned NumProtoRefs, |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 219 | SourceLocation EndProtoLoc, |
| 220 | AttributeList *AttrList) { |
| 221 | // FIXME: Deal with AttrList. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 222 | assert(ProtocolName && "Missing protocol identifier"); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 223 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 224 | if (PDecl) { |
| 225 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 226 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 227 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | 5b25065 | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 228 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 229 | // Just return the protocol we already had. |
| 230 | // FIXME: don't leak the objects passed in! |
| 231 | return PDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 232 | } |
Steve Naroff | 9a9e521 | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 233 | // Make sure the cached decl gets a valid start location. |
| 234 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 235 | PDecl->setForwardDecl(false); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 236 | } else { |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 237 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
| 238 | AtProtoInterfaceLoc,ProtocolName); |
| 239 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 240 | CurContext->addDecl(PDecl); |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 241 | PDecl->setForwardDecl(false); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 242 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 243 | } |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 244 | if (AttrList) |
| 245 | ProcessDeclAttributeList(PDecl, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 246 | if (NumProtoRefs) { |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 247 | /// Check then save referenced protocols. |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 248 | PDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 249 | PDecl->setLocEnd(EndProtoLoc); |
| 250 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 251 | |
| 252 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 253 | return PDecl; |
| 254 | } |
| 255 | |
| 256 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 257 | /// issues an error if they are not declared. It returns list of |
| 258 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 259 | void |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 260 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 261 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 262 | unsigned NumProtocols, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 263 | llvm::SmallVectorImpl<DeclTy*> &Protocols) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 264 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 265 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]; |
| 266 | if (!PDecl) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 267 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 268 | << ProtocolId[i].first; |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 269 | continue; |
| 270 | } |
Chris Lattner | faaad13 | 2009-02-14 08:22:25 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 272 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 273 | |
| 274 | // If this is a forward declaration and we are supposed to warn in this |
| 275 | // case, do it. |
| 276 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 277 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 278 | << ProtocolId[i].first; |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 279 | Protocols.push_back(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 283 | /// DiagnosePropertyMismatch - Compares two properties for their |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 284 | /// attributes and types and warns on a variety of inconsistencies. |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 285 | /// |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 286 | void |
| 287 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 288 | ObjCPropertyDecl *SuperProperty, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 289 | const IdentifierInfo *inheritedName) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 290 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 291 | Property->getPropertyAttributes(); |
| 292 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 293 | SuperProperty->getPropertyAttributes(); |
| 294 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 295 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 296 | Diag(Property->getLocation(), diag::warn_readonly_property) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 297 | << Property->getDeclName() << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 298 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 299 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 300 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 301 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 302 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 303 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 304 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 305 | << Property->getDeclName() << "retain" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 306 | |
| 307 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 308 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 309 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 310 | << Property->getDeclName() << "atomic" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 311 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 312 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 313 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 314 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 315 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 316 | << Property->getDeclName() << "getter" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 317 | |
Chris Lattner | c5cff30 | 2008-07-26 20:50:02 +0000 | [diff] [blame] | 318 | if (Context.getCanonicalType(Property->getType()) != |
| 319 | Context.getCanonicalType(SuperProperty->getType())) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 320 | Diag(Property->getLocation(), diag::warn_property_type) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 321 | << Property->getType() << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 322 | |
| 323 | } |
| 324 | |
| 325 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 326 | /// declarations in base and its super class, if any, and issues |
| 327 | /// diagnostics in a variety of inconsistant situations. |
| 328 | /// |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 329 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 330 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 331 | if (!SDecl) |
| 332 | return; |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 333 | // FIXME: O(N^2) |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 334 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 335 | E = SDecl->prop_end(); S != E; ++S) { |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 336 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 337 | // Does property in super class has declaration in current class? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 338 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 339 | E = IDecl->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 340 | ObjCPropertyDecl *PDecl = (*I); |
| 341 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 342 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 343 | SDecl->getIdentifier()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 348 | /// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list |
| 349 | /// of properties declared in a protocol and adds them to the list |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 350 | /// of properties for current class/category if it is not there already. |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 351 | void |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 352 | Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 353 | ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 354 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 355 | if (!IDecl) { |
| 356 | // Category |
| 357 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 358 | assert (CatDecl && "MergeOneProtocolPropertiesIntoClass"); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 359 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 360 | E = PDecl->prop_end(); P != E; ++P) { |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 361 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 362 | ObjCCategoryDecl::prop_iterator CP, CE; |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 363 | // Is this property already in category's list of properties? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 364 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 365 | CP != CE; ++CP) |
| 366 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 367 | break; |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 368 | if (CP != CE) |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 369 | // Property protocol already exist in class. Diagnose any mismatch. |
| 370 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 371 | } |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 374 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 375 | E = PDecl->prop_end(); P != E; ++P) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 376 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 377 | ObjCInterfaceDecl::prop_iterator CP, CE; |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 378 | // Is this property already in class's list of properties? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 379 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 380 | CP != CE; ++CP) |
| 381 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 382 | break; |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 383 | if (CP != CE) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 384 | // Property protocol already exist in class. Diagnose any mismatch. |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 385 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 386 | } |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /// MergeProtocolPropertiesIntoClass - This routine merges properties |
| 390 | /// declared in 'MergeItsProtocols' objects (which can be a class or an |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 391 | /// inherited protocol into the list of properties for class/category 'CDecl' |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 392 | /// |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 393 | void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl, |
| 394 | DeclTy *MergeItsProtocols) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 395 | Decl *ClassDecl = static_cast<Decl *>(MergeItsProtocols); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 396 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 397 | |
| 398 | if (!IDecl) { |
| 399 | // Category |
| 400 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 401 | assert (CatDecl && "MergeProtocolPropertiesIntoClass"); |
| 402 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 403 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 404 | E = MDecl->protocol_end(); P != E; ++P) |
| 405 | // Merge properties of category (*P) into IDECL's |
| 406 | MergeOneProtocolPropertiesIntoClass(CatDecl, *P); |
| 407 | |
| 408 | // Go thru the list of protocols for this category and recursively merge |
| 409 | // their properties into this class as well. |
| 410 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 411 | E = CatDecl->protocol_end(); P != E; ++P) |
| 412 | MergeProtocolPropertiesIntoClass(CatDecl, *P); |
| 413 | } else { |
| 414 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 415 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 416 | E = MD->protocol_end(); P != E; ++P) |
| 417 | MergeOneProtocolPropertiesIntoClass(CatDecl, (*P)); |
| 418 | } |
| 419 | return; |
| 420 | } |
| 421 | |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 422 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 423 | for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 424 | E = MDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 425 | // Merge properties of class (*P) into IDECL's |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 426 | MergeOneProtocolPropertiesIntoClass(IDecl, *P); |
| 427 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 428 | // Go thru the list of protocols for this class and recursively merge |
| 429 | // their properties into this class as well. |
| 430 | for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), |
| 431 | E = IDecl->protocol_end(); P != E; ++P) |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 432 | MergeProtocolPropertiesIntoClass(IDecl, *P); |
| 433 | } else { |
Argiris Kirtzidis | fc1ea13 | 2008-07-21 09:18:38 +0000 | [diff] [blame] | 434 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 435 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 436 | E = MD->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 437 | MergeOneProtocolPropertiesIntoClass(IDecl, (*P)); |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 438 | } |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 441 | /// ActOnForwardProtocolDeclaration - |
| 442 | Action::DeclTy * |
| 443 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 444 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 445 | unsigned NumElts, |
| 446 | AttributeList *attrList) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 447 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 448 | |
| 449 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 450 | IdentifierInfo *Ident = IdentList[i].first; |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 451 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 452 | if (PDecl == 0) { // Not already seen? |
| 453 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
| 454 | IdentList[i].second, Ident); |
| 455 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 456 | CurContext->addDecl(PDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 457 | } |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 458 | if (attrList) |
| 459 | ProcessDeclAttributeList(PDecl, attrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 460 | Protocols.push_back(PDecl); |
| 461 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 462 | |
| 463 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 464 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 465 | &Protocols[0], Protocols.size()); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 466 | CurContext->addDecl(PDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 467 | CheckObjCDeclScope(PDecl); |
| 468 | return PDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 471 | Sema::DeclTy *Sema:: |
| 472 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 473 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 474 | IdentifierInfo *CategoryName, |
| 475 | SourceLocation CategoryLoc, |
Chris Lattner | 45142b9 | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 476 | DeclTy * const *ProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 477 | unsigned NumProtoRefs, |
| 478 | SourceLocation EndProtoLoc) { |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 479 | ObjCCategoryDecl *CDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 480 | ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName); |
| 481 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 482 | CurContext->addDecl(CDecl); |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 483 | |
| 484 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 485 | /// Check that class of this category is already completely declared. |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 486 | if (!IDecl || IDecl->isForwardDecl()) { |
| 487 | CDecl->setInvalidDecl(); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 488 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 489 | return CDecl; |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 490 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 492 | CDecl->setClassInterface(IDecl); |
Chris Lattner | de3fea8 | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 493 | |
| 494 | // If the interface is deprecated, warn about it. |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 495 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 496 | |
| 497 | /// Check for duplicate interface declaration for this category |
| 498 | ObjCCategoryDecl *CDeclChain; |
| 499 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 500 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 501 | if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { |
| 502 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 503 | << ClassName << CategoryName; |
| 504 | Diag(CDeclChain->getLocation(), diag::note_previous_definition); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | if (!CDeclChain) |
| 509 | CDecl->insertNextClassCategory(); |
| 510 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 511 | if (NumProtoRefs) { |
Chris Lattner | 45142b9 | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 512 | CDecl->addReferencedProtocols((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs); |
| 513 | CDecl->setLocEnd(EndProtoLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 514 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 515 | |
| 516 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 517 | return CDecl; |
| 518 | } |
| 519 | |
| 520 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 521 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 522 | /// object. |
| 523 | Sema::DeclTy *Sema::ActOnStartCategoryImplementation( |
| 524 | SourceLocation AtCatImplLoc, |
| 525 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 526 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 527 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 528 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 529 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 530 | IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 531 | /// Check that class of this category is already completely declared. |
| 532 | if (!IDecl || IDecl->isForwardDecl()) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 533 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 535 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 536 | CurContext->addDecl(CDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 537 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 538 | /// TODO: Check that CatName, category name, is not used in another |
| 539 | // implementation. |
Steve Naroff | 4b9846d | 2008-09-28 14:55:53 +0000 | [diff] [blame] | 540 | ObjCCategoryImpls.push_back(CDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 541 | |
| 542 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 543 | return CDecl; |
| 544 | } |
| 545 | |
| 546 | Sema::DeclTy *Sema::ActOnStartClassImplementation( |
| 547 | SourceLocation AtClassImplLoc, |
| 548 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 549 | IdentifierInfo *SuperClassname, |
| 550 | SourceLocation SuperClassLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 551 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 552 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 553 | NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 554 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 555 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 556 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 557 | } |
| 558 | else { |
| 559 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 560 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 561 | if (!IDecl) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 562 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // Check that super class name is valid class name |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 566 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 567 | if (SuperClassname) { |
| 568 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 569 | PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 570 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 571 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 572 | << SuperClassname; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 573 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 574 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 575 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 576 | if (!SDecl) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 577 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 578 | << SuperClassname << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 579 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 580 | // This implementation and its interface do not have the same |
| 581 | // super class. |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 582 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 583 | << SDecl->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 584 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if (!IDecl) { |
| 590 | // Legacy case of @implementation with no corresponding @interface. |
| 591 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 592 | |
| 593 | // FIXME: Do we support attributes on the @implementation? If so |
| 594 | // we should copy them over. |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 595 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
| 596 | ClassName, ClassLoc, false, true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 597 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 598 | IDecl->setSuperClass(SDecl); |
| 599 | IDecl->setLocEnd(ClassLoc); |
| 600 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 601 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 602 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 603 | // Remember that this needs to be removed when the scope is popped. |
| 604 | TUScope->AddDecl(IDecl); |
| 605 | } |
| 606 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 607 | ObjCImplementationDecl* IMPDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 608 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 609 | IDecl, SDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 611 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 612 | CurContext->addDecl(IMPDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 614 | if (CheckObjCDeclScope(IMPDecl)) |
| 615 | return IMPDecl; |
| 616 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 617 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 618 | if (ObjCImplementations[ClassName]) |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 619 | // FIXME: Don't leak everything! |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 620 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 621 | else // add it to the list. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 622 | ObjCImplementations[ClassName] = IMPDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 623 | return IMPDecl; |
| 624 | } |
| 625 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 626 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 627 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 628 | SourceLocation RBrace) { |
| 629 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 630 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 631 | if (!IDecl) |
| 632 | return; |
| 633 | /// Check case of non-existing @interface decl. |
| 634 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 635 | /// Add implementations's ivar to the synthesize class's ivar list. |
| 636 | if (IDecl->ImplicitInterfaceDecl()) { |
| 637 | IDecl->addInstanceVariablesToClass(ivars, numIvars, RBrace); |
| 638 | return; |
| 639 | } |
| 640 | // If implementation has empty ivar list, just return. |
| 641 | if (numIvars == 0) |
| 642 | return; |
| 643 | |
| 644 | assert(ivars && "missing @implementation ivars"); |
| 645 | |
| 646 | // Check interface's Ivar list against those in the implementation. |
| 647 | // names and types must match. |
| 648 | // |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 649 | unsigned j = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 650 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 651 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 652 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 653 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 654 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 655 | assert (ImplIvar && "missing implementation ivar"); |
| 656 | assert (ClsIvar && "missing class ivar"); |
Chris Lattner | b545786 | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 657 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 658 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 659 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 660 | << ImplIvar->getIdentifier() |
| 661 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 662 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 663 | } |
| 664 | // TODO: Two mismatched (unequal width) Ivar bitfields should be diagnosed |
| 665 | // as error. |
| 666 | else if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 667 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 668 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 669 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 670 | return; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 671 | } |
| 672 | --numIvars; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 673 | } |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 674 | |
| 675 | if (numIvars > 0) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 676 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 677 | else if (IVI != IVE) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 678 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 681 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 682 | bool &IncompleteImpl) { |
| 683 | if (!IncompleteImpl) { |
| 684 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 685 | IncompleteImpl = true; |
| 686 | } |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 687 | Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 690 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 691 | ObjCMethodDecl *IntfMethodDecl) { |
| 692 | bool err = false; |
| 693 | QualType ImpMethodQType = |
| 694 | Context.getCanonicalType(ImpMethodDecl->getResultType()); |
| 695 | QualType IntfMethodQType = |
| 696 | Context.getCanonicalType(IntfMethodDecl->getResultType()); |
| 697 | if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) |
| 698 | err = true; |
| 699 | else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(), |
| 700 | IF=IntfMethodDecl->param_begin(), |
| 701 | EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) { |
| 702 | ImpMethodQType = Context.getCanonicalType((*IM)->getType()); |
| 703 | IntfMethodQType = Context.getCanonicalType((*IF)->getType()); |
| 704 | if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) { |
| 705 | err = true; |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | if (err) { |
| 710 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types) |
| 711 | << ImpMethodDecl->getDeclName(); |
| 712 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); |
| 713 | } |
| 714 | } |
| 715 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 716 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 717 | /// for the property in the class and in its categories and implementations |
| 718 | /// |
| 719 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
| 720 | ObjCInterfaceDecl *IDecl) const { |
| 721 | // by far the most common case. |
| 722 | if (!PDecl->isReadOnly()) |
| 723 | return false; |
| 724 | // Even if property is ready only, if interface has a user defined setter, |
| 725 | // it is not considered read only. |
| 726 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 727 | return false; |
| 728 | |
| 729 | // Main class has the property as 'readonly'. Must search |
| 730 | // through the category list to see if the property's |
| 731 | // attribute has been over-ridden to 'readwrite'. |
| 732 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 733 | Category; Category = Category->getNextClassCategory()) { |
| 734 | // Even if property is ready only, if a category has a user defined setter, |
| 735 | // it is not considered read only. |
| 736 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
| 737 | return false; |
| 738 | ObjCPropertyDecl *P = |
| 739 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 740 | if (P && !P->isReadOnly()) |
| 741 | return false; |
| 742 | } |
| 743 | |
| 744 | // Also, check for definition of a setter method in the implementation if |
| 745 | // all else failed. |
| 746 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 747 | if (ObjCImplementationDecl *IMD = |
| 748 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 749 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 750 | return false; |
| 751 | } |
| 752 | else if (ObjCCategoryImplDecl *CIMD = |
| 753 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 754 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 755 | return false; |
| 756 | } |
| 757 | } |
| 758 | return true; |
| 759 | } |
| 760 | |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 761 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most |
| 762 | /// likely improve the efficiency of selector lookups and type |
| 763 | /// checking by associating with each protocol / interface / category |
| 764 | /// the flattened instance tables. If we used an immutable set to keep |
| 765 | /// the table then it wouldn't add significant memory cost and it |
| 766 | /// would be handy for lookups. |
| 767 | |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 768 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 769 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 770 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 771 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 772 | bool& IncompleteImpl, |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 773 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 774 | const llvm::DenseSet<Selector> &ClsMap, |
| 775 | ObjCInterfaceDecl *IDecl) { |
| 776 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
| 777 | |
| 778 | // If a method lookup fails locally we still need to look and see if |
| 779 | // the method was implemented by a base class or an inherited |
| 780 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 781 | // and otherwise would terminate in a warning. |
| 782 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 783 | // check unimplemented instance methods. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 784 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 785 | E = PDecl->instmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 786 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 787 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | 4b871b3 | 2008-11-24 22:16:00 +0000 | [diff] [blame] | 788 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 789 | (!Super || !Super->lookupInstanceMethod(method->getSelector()))) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 790 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 791 | } |
| 792 | // check unimplemented class methods |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 793 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 794 | E = PDecl->classmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 795 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 796 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 797 | !ClsMap.count(method->getSelector()) && |
| 798 | (!Super || !Super->lookupClassMethod(method->getSelector()))) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 799 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 801 | // Check on this protocols's referenced protocols, recursively. |
| 802 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 803 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 804 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 807 | void Sema::ImplMethodsVsClassMethods(ObjCImplementationDecl* IMPDecl, |
| 808 | ObjCInterfaceDecl* IDecl) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 809 | llvm::DenseSet<Selector> InsMap; |
| 810 | // Check and see if instance methods in class interface have been |
| 811 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 812 | for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 813 | E = IMPDecl->instmeth_end(); I != E; ++I) |
| 814 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 815 | |
| 816 | bool IncompleteImpl = false; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 817 | for (ObjCInterfaceDecl::instmeth_iterator I = IDecl->instmeth_begin(), |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 818 | E = IDecl->instmeth_end(); I != E; ++I) { |
| 819 | if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) { |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 820 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 821 | continue; |
Fariborz Jahanian | 64c4631 | 2008-12-05 01:35:25 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 823 | |
| 824 | ObjCMethodDecl *ImpMethodDecl = |
| 825 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
| 826 | ObjCMethodDecl *IntfMethodDecl = |
| 827 | IDecl->getInstanceMethod((*I)->getSelector()); |
| 828 | assert(IntfMethodDecl && |
| 829 | "IntfMethodDecl is null in ImplMethodsVsClassMethods"); |
| 830 | // ImpMethodDecl may be null as in a @dynamic property. |
| 831 | if (ImpMethodDecl) |
| 832 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 833 | } |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 835 | llvm::DenseSet<Selector> ClsMap; |
| 836 | // Check and see if class methods in class interface have been |
| 837 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 838 | for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 839 | E = IMPDecl->classmeth_end(); I != E; ++I) |
| 840 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 841 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 842 | for (ObjCInterfaceDecl::classmeth_iterator I = IDecl->classmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 843 | E = IDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 844 | if (!ClsMap.count((*I)->getSelector())) |
| 845 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 846 | else { |
| 847 | ObjCMethodDecl *ImpMethodDecl = |
| 848 | IMPDecl->getClassMethod((*I)->getSelector()); |
| 849 | ObjCMethodDecl *IntfMethodDecl = |
| 850 | IDecl->getClassMethod((*I)->getSelector()); |
| 851 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 852 | } |
| 853 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 854 | |
| 855 | // Check the protocol list for unimplemented methods in the @implementation |
| 856 | // class. |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 857 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 858 | IDecl->getReferencedProtocols(); |
| 859 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(), |
| 860 | E = Protocols.end(); I != E; ++I) |
| 861 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *I, |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 862 | IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /// ImplCategoryMethodsVsIntfMethods - Checks that methods declared in the |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 866 | /// category interface are implemented in the category @implementation. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 867 | void Sema::ImplCategoryMethodsVsIntfMethods(ObjCCategoryImplDecl *CatImplDecl, |
| 868 | ObjCCategoryDecl *CatClassDecl) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 869 | llvm::DenseSet<Selector> InsMap; |
| 870 | // Check and see if instance methods in category interface have been |
| 871 | // implemented in its implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 872 | for (ObjCCategoryImplDecl::instmeth_iterator I =CatImplDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 873 | E = CatImplDecl->instmeth_end(); I != E; ++I) |
| 874 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 875 | |
| 876 | bool IncompleteImpl = false; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 877 | for (ObjCCategoryDecl::instmeth_iterator I = CatClassDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 878 | E = CatClassDecl->instmeth_end(); I != E; ++I) |
Fariborz Jahanian | 42d0bf9 | 2008-12-22 19:05:31 +0000 | [diff] [blame] | 879 | if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 880 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 881 | else { |
| 882 | ObjCMethodDecl *ImpMethodDecl = |
| 883 | CatImplDecl->getInstanceMethod((*I)->getSelector()); |
| 884 | ObjCMethodDecl *IntfMethodDecl = |
| 885 | CatClassDecl->getInstanceMethod((*I)->getSelector()); |
Fariborz Jahanian | 42d0bf9 | 2008-12-22 19:05:31 +0000 | [diff] [blame] | 886 | assert(IntfMethodDecl && |
| 887 | "IntfMethodDecl is null in ImplCategoryMethodsVsIntfMethods"); |
| 888 | // ImpMethodDecl may be null as in a @dynamic property. |
| 889 | if (ImpMethodDecl) |
| 890 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 891 | } |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 892 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 893 | llvm::DenseSet<Selector> ClsMap; |
| 894 | // Check and see if class methods in category interface have been |
| 895 | // implemented in its implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 896 | for (ObjCCategoryImplDecl::classmeth_iterator |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 897 | I = CatImplDecl->classmeth_begin(), E = CatImplDecl->classmeth_end(); |
| 898 | I != E; ++I) |
| 899 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 901 | for (ObjCCategoryDecl::classmeth_iterator I = CatClassDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 902 | E = CatClassDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 903 | if (!ClsMap.count((*I)->getSelector())) |
| 904 | WarnUndefinedMethod(CatImplDecl->getLocation(), *I, IncompleteImpl); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 905 | else { |
| 906 | ObjCMethodDecl *ImpMethodDecl = |
| 907 | CatImplDecl->getClassMethod((*I)->getSelector()); |
| 908 | ObjCMethodDecl *IntfMethodDecl = |
| 909 | CatClassDecl->getClassMethod((*I)->getSelector()); |
| 910 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 911 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 912 | // Check the protocol list for unimplemented methods in the @implementation |
| 913 | // class. |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 914 | for (ObjCCategoryDecl::protocol_iterator PI = CatClassDecl->protocol_begin(), |
| 915 | E = CatClassDecl->protocol_end(); PI != E; ++PI) |
| 916 | CheckProtocolMethodDefs(CatImplDecl->getLocation(), *PI, IncompleteImpl, |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 917 | InsMap, ClsMap, CatClassDecl->getClassInterface()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /// ActOnForwardClassDeclaration - |
| 921 | Action::DeclTy * |
| 922 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 923 | IdentifierInfo **IdentList, |
| 924 | unsigned NumElts) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 925 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 926 | |
| 927 | for (unsigned i = 0; i != NumElts; ++i) { |
| 928 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 929 | NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 930 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 931 | // Maybe we will complain about the shadowed template parameter. |
| 932 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 933 | // Just pretend that we didn't see the previous declaration. |
| 934 | PrevDecl = 0; |
| 935 | } |
| 936 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 937 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | d254997 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 938 | // GCC apparently allows the following idiom: |
| 939 | // |
| 940 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 941 | // @class XCElementToggler; |
| 942 | // |
| 943 | // FIXME: Make an extension? |
| 944 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
| 945 | if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 946 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 947 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | d254997 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 948 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 949 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 950 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 951 | if (!IDecl) { // Not already seen? Make a forward decl. |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 952 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
| 953 | IdentList[i], SourceLocation(), true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 954 | ObjCInterfaceDecls[IdentList[i]] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 955 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 956 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 957 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 958 | // Remember that this needs to be removed when the scope is popped. |
| 959 | TUScope->AddDecl(IDecl); |
| 960 | } |
| 961 | |
| 962 | Interfaces.push_back(IDecl); |
| 963 | } |
| 964 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 965 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 966 | &Interfaces[0], |
| 967 | Interfaces.size()); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 968 | CurContext->addDecl(CDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 969 | CheckObjCDeclScope(CDecl); |
| 970 | return CDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | |
| 974 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 975 | /// returns true, or false, accordingly. |
| 976 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 977 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 978 | const ObjCMethodDecl *PrevMethod, |
| 979 | bool matchBasedOnSizeAndAlignment) { |
| 980 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 981 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
| 982 | |
| 983 | if (T1 != T2) { |
| 984 | // The result types are different. |
| 985 | if (!matchBasedOnSizeAndAlignment) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 986 | return false; |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 987 | // Incomplete types don't have a size and alignment. |
| 988 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 989 | return false; |
| 990 | // Check is based on size and alignment. |
| 991 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 992 | return false; |
| 993 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 994 | |
| 995 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 996 | E = Method->param_end(); |
| 997 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
| 998 | |
| 999 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1000 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1001 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1002 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1003 | if (T1 != T2) { |
| 1004 | // The result types are different. |
| 1005 | if (!matchBasedOnSizeAndAlignment) |
| 1006 | return false; |
| 1007 | // Incomplete types don't have a size and alignment. |
| 1008 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1009 | return false; |
| 1010 | // Check is based on size and alignment. |
| 1011 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1012 | return false; |
| 1013 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1014 | } |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1018 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 1019 | ObjCMethodList &FirstMethod = InstanceMethodPool[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1020 | if (!FirstMethod.Method) { |
| 1021 | // Haven't seen a method with this selector name yet - add it. |
| 1022 | FirstMethod.Method = Method; |
| 1023 | FirstMethod.Next = 0; |
| 1024 | } else { |
| 1025 | // We've seen a method with this name, now check the type signature(s). |
| 1026 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 1027 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1028 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1029 | Next = Next->Next) |
| 1030 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 1031 | |
| 1032 | if (!match) { |
| 1033 | // We have a new signature for an existing method - add it. |
| 1034 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1035 | FirstMethod.Next = new ObjCMethodList(Method, FirstMethod.Next);; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Steve Naroff | ec7e088 | 2008-10-21 10:50:19 +0000 | [diff] [blame] | 1040 | // FIXME: Finish implementing -Wno-strict-selector-match. |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1041 | ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, |
| 1042 | SourceRange R) { |
| 1043 | ObjCMethodList &MethList = InstanceMethodPool[Sel]; |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1044 | bool issueWarning = false; |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1045 | |
| 1046 | if (MethList.Method && MethList.Next) { |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1047 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1048 | // This checks if the methods differ by size & alignment. |
| 1049 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1050 | issueWarning = true; |
| 1051 | } |
| 1052 | if (issueWarning && (MethList.Method && MethList.Next)) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1053 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Chris Lattner | ef2a3c6 | 2008-11-23 23:26:13 +0000 | [diff] [blame] | 1054 | Diag(MethList.Method->getLocStart(), diag::note_using_decl) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1055 | << MethList.Method->getSourceRange(); |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1056 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
Chris Lattner | ef2a3c6 | 2008-11-23 23:26:13 +0000 | [diff] [blame] | 1057 | Diag(Next->Method->getLocStart(), diag::note_also_found_decl) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1058 | << Next->Method->getSourceRange(); |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1059 | } |
| 1060 | return MethList.Method; |
| 1061 | } |
| 1062 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1063 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 1064 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1065 | if (!FirstMethod.Method) { |
| 1066 | // Haven't seen a method with this selector name yet - add it. |
| 1067 | FirstMethod.Method = Method; |
| 1068 | FirstMethod.Next = 0; |
| 1069 | } else { |
| 1070 | // We've seen a method with this name, now check the type signature(s). |
| 1071 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 1072 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1073 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1074 | Next = Next->Next) |
| 1075 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 1076 | |
| 1077 | if (!match) { |
| 1078 | // We have a new signature for an existing method - add it. |
| 1079 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1080 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1081 | FirstMethod.Next = OMI; |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1086 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1087 | /// have the property type and issue diagnostics if they don't. |
| 1088 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1089 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1090 | /// methods is the "right" thing to do. |
| 1091 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
| 1092 | ObjCContainerDecl *CD) { |
| 1093 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1094 | |
| 1095 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1096 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1097 | |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1098 | if (GetterMethod && |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1099 | GetterMethod->getResultType() != property->getType()) { |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1100 | Diag(property->getLocation(), |
| 1101 | diag::err_accessor_property_type_mismatch) |
| 1102 | << property->getDeclName() |
| 1103 | << GetterMethod->getSelector().getAsIdentifierInfo(); |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1104 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1105 | } |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1106 | |
| 1107 | if (SetterMethod) { |
Fariborz Jahanian | 5c15054 | 2008-12-06 23:12:49 +0000 | [diff] [blame] | 1108 | if (Context.getCanonicalType(SetterMethod->getResultType()) |
| 1109 | != Context.VoidTy) |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1110 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1111 | if (SetterMethod->param_size() != 1 || |
| 1112 | ((*SetterMethod->param_begin())->getType() != property->getType())) { |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1113 | Diag(property->getLocation(), |
| 1114 | diag::err_accessor_property_type_mismatch) |
| 1115 | << property->getDeclName() |
| 1116 | << SetterMethod->getSelector().getAsIdentifierInfo(); |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1117 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1118 | } |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1119 | } |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Synthesize getter/setter methods if none exist. |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1122 | // Find the default getter and if one not found, add one. |
Steve Naroff | 4cf0d3f | 2009-01-08 20:17:34 +0000 | [diff] [blame] | 1123 | // FIXME: The synthesized property we set here is misleading. We |
| 1124 | // almost always synthesize these methods unless the user explicitly |
| 1125 | // provided prototypes (which is odd, but allowed). Sema should be |
| 1126 | // typechecking that the declarations jive in that situation (which |
| 1127 | // it is not currently). |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1128 | if (!GetterMethod) { |
| 1129 | // No instance method of same name as property getter name was found. |
| 1130 | // Declare a getter method and add it to the list of methods |
| 1131 | // for this class. |
| 1132 | GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1133 | property->getLocation(), property->getGetterName(), |
| 1134 | property->getType(), CD, true, false, true, |
| 1135 | (property->getPropertyImplementation() == |
| 1136 | ObjCPropertyDecl::Optional) ? |
| 1137 | ObjCMethodDecl::Optional : |
| 1138 | ObjCMethodDecl::Required); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1139 | CD->addDecl(GetterMethod); |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1140 | } else |
| 1141 | // A user declared getter will be synthesize when @synthesize of |
| 1142 | // the property with the same name is seen in the @implementation |
| 1143 | GetterMethod->setIsSynthesized(); |
| 1144 | property->setGetterMethodDecl(GetterMethod); |
| 1145 | |
| 1146 | // Skip setter if property is read-only. |
| 1147 | if (!property->isReadOnly()) { |
| 1148 | // Find the default setter and if one not found, add one. |
| 1149 | if (!SetterMethod) { |
| 1150 | // No instance method of same name as property setter name was found. |
| 1151 | // Declare a setter method and add it to the list of methods |
| 1152 | // for this class. |
| 1153 | SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1154 | property->getLocation(), |
| 1155 | property->getSetterName(), |
| 1156 | Context.VoidTy, CD, true, false, true, |
| 1157 | (property->getPropertyImplementation() == |
| 1158 | ObjCPropertyDecl::Optional) ? |
| 1159 | ObjCMethodDecl::Optional : |
| 1160 | ObjCMethodDecl::Required); |
| 1161 | // Invent the arguments for the setter. We don't bother making a |
| 1162 | // nice name for the argument. |
| 1163 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 1164 | SourceLocation(), |
| 1165 | property->getIdentifier(), |
| 1166 | property->getType(), |
| 1167 | VarDecl::None, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1168 | 0); |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1169 | SetterMethod->setMethodParams(&Argument, 1); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1170 | CD->addDecl(SetterMethod); |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1171 | } else |
| 1172 | // A user declared setter will be synthesize when @synthesize of |
| 1173 | // the property with the same name is seen in the @implementation |
| 1174 | SetterMethod->setIsSynthesized(); |
| 1175 | property->setSetterMethodDecl(SetterMethod); |
| 1176 | } |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1177 | // Add any synthesized methods to the global pool. This allows us to |
| 1178 | // handle the following, which is supported by GCC (and part of the design). |
| 1179 | // |
| 1180 | // @interface Foo |
| 1181 | // @property double bar; |
| 1182 | // @end |
| 1183 | // |
| 1184 | // void thisIsUnfortunate() { |
| 1185 | // id foo; |
| 1186 | // double bar = [foo bar]; |
| 1187 | // } |
| 1188 | // |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1189 | if (GetterMethod) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1190 | AddInstanceMethodToGlobalPool(GetterMethod); |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1191 | if (SetterMethod) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1192 | AddInstanceMethodToGlobalPool(SetterMethod); |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1195 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1196 | // always null. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1197 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclTy *classDecl, |
| 1198 | DeclTy **allMethods, unsigned allNum, |
| 1199 | DeclTy **allProperties, unsigned pNum) { |
| 1200 | Decl *ClassDecl = static_cast<Decl *>(classDecl); |
| 1201 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1202 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1203 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1204 | // should be true. |
| 1205 | if (!ClassDecl) |
| 1206 | return; |
| 1207 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1208 | bool isInterfaceDeclKind = |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1209 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1210 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1211 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1212 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1213 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1214 | |
| 1215 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1216 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1217 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1218 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1219 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1220 | ObjCMethodDecl *Method = |
| 1221 | cast_or_null<ObjCMethodDecl>(static_cast<Decl*>(allMethods[i])); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1224 | if (Method->isInstanceMethod()) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1225 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1226 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1227 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 1228 | : false; |
Eli Friedman | 70c167d | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1229 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 1230 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1231 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1232 | << Method->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1233 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1234 | } else { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1235 | DC->addDecl(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1236 | InsMap[Method->getSelector()] = Method; |
| 1237 | /// The following allows us to typecheck messages to "id". |
| 1238 | AddInstanceMethodToGlobalPool(Method); |
| 1239 | } |
| 1240 | } |
| 1241 | else { |
| 1242 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1243 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1244 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 1245 | : false; |
Eli Friedman | 70c167d | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1246 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 1247 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1248 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1249 | << Method->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1250 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1251 | } else { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1252 | DC->addDecl(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1253 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1254 | /// The following allows us to typecheck messages to "Class". |
| 1255 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1259 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1260 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1261 | // super class. |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1262 | ComparePropertiesInBaseAndSuper(I); |
| 1263 | MergeProtocolPropertiesIntoClass(I, I); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1264 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 37aaf4f | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1265 | // Categories are used to extend the class by declaring new methods. |
| 1266 | // By the same token, they are also used to add new properties. No |
| 1267 | // need to compare the added property to those in the class. |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1268 | |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 1269 | // Merge protocol properties into category |
| 1270 | MergeProtocolPropertiesIntoClass(C, C); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1271 | } |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1272 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
| 1273 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1274 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1275 | // and adds them to the DeclContext and global method pools. |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1276 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1277 | E = CDecl->prop_end(); I != E; ++I) |
| 1278 | ProcessPropertyDecl(*I, CDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1279 | CDecl->setAtEndLoc(AtEndLoc); |
| 1280 | } |
| 1281 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1282 | IC->setLocEnd(AtEndLoc); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1283 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1284 | ImplMethodsVsClassMethods(IC, IDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1285 | } else if (ObjCCategoryImplDecl* CatImplClass = |
| 1286 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1287 | CatImplClass->setLocEnd(AtEndLoc); |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1288 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1289 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1290 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1291 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1292 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1293 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1294 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
| 1295 | ImplCategoryMethodsVsIntfMethods(CatImplClass, Categories); |
| 1296 | break; |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1305 | /// objective-c's type qualifier from the parser version of the same info. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1306 | static Decl::ObjCDeclQualifier |
| 1307 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1308 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1309 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1310 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1311 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1312 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1313 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1314 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1315 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1316 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1317 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1318 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1319 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1320 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1321 | |
| 1322 | return ret; |
| 1323 | } |
| 1324 | |
| 1325 | Sema::DeclTy *Sema::ActOnMethodDeclaration( |
| 1326 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1327 | tok::TokenKind MethodType, DeclTy *classDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1328 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1329 | Selector Sel, |
| 1330 | // optional arguments. The number of types/arguments is obtained |
| 1331 | // from the Sel.getNumArgs(). |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1332 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Fariborz Jahanian | 89d5304 | 2009-01-09 00:38:19 +0000 | [diff] [blame] | 1333 | llvm::SmallVectorImpl<Declarator> &Cdecls, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1334 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1335 | bool isVariadic) { |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1336 | Decl *ClassDecl = static_cast<Decl*>(classDecl); |
Steve Naroff | 70f1624 | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Make sure we can establish a context for the method. |
| 1339 | if (!ClassDecl) { |
| 1340 | Diag(MethodLoc, diag::error_missing_method_context); |
| 1341 | return 0; |
| 1342 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1343 | QualType resultDeclType; |
| 1344 | |
| 1345 | if (ReturnType) |
| 1346 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
| 1347 | else // get the type for "id". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1348 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1349 | |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1350 | ObjCMethodDecl* ObjCMethod = |
| 1351 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1352 | dyn_cast<DeclContext>(ClassDecl), |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1353 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 5f2e224 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 1354 | false, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1355 | MethodDeclKind == tok::objc_optional ? |
| 1356 | ObjCMethodDecl::Optional : |
| 1357 | ObjCMethodDecl::Required); |
| 1358 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1359 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1360 | |
| 1361 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 1362 | // FIXME: arg->AttrList must be stored too! |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1363 | QualType argType, originalArgType; |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1364 | |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1365 | if (ArgTypes[i]) { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1366 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1367 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1368 | if (argType->isArrayType()) { // (char *[]) -> (char **) |
| 1369 | originalArgType = argType; |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1370 | argType = Context.getArrayDecayedType(argType); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1371 | } |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1372 | else if (argType->isFunctionType()) |
| 1373 | argType = Context.getPointerType(argType); |
Fariborz Jahanian | 7b4695f | 2009-01-17 21:57:49 +0000 | [diff] [blame] | 1374 | else if (argType->isObjCInterfaceType()) { |
| 1375 | // FIXME! provide more precise location for the parameter |
| 1376 | Diag(MethodLoc, diag::err_object_as_method_param); |
| 1377 | return 0; |
| 1378 | } |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1379 | } else |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1380 | argType = Context.getObjCIdType(); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1381 | ParmVarDecl* Param; |
| 1382 | if (originalArgType.isNull()) |
| 1383 | Param = ParmVarDecl::Create(Context, ObjCMethod, |
| 1384 | SourceLocation(/*FIXME*/), |
| 1385 | ArgNames[i], argType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1386 | VarDecl::None, 0); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1387 | else |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 1388 | Param = OriginalParmVarDecl::Create(Context, ObjCMethod, |
| 1389 | SourceLocation(/*FIXME*/), |
| 1390 | ArgNames[i], argType, originalArgType, |
| 1391 | VarDecl::None, 0); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1392 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1393 | Param->setObjCDeclQualifier( |
| 1394 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
| 1395 | Params.push_back(Param); |
| 1396 | } |
| 1397 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1398 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs()); |
| 1399 | ObjCMethod->setObjCDeclQualifier( |
| 1400 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 1401 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | d1d847c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (AttrList) |
| 1404 | ProcessDeclAttributeList(ObjCMethod, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1405 | |
| 1406 | // For implementations (which can be very "coarse grain"), we add the |
| 1407 | // method now. This allows the AST to implement lookup methods that work |
| 1408 | // incrementally (without waiting until we parse the @end). It also allows |
| 1409 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1410 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1411 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1412 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1413 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1414 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1415 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1416 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1417 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1420 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1421 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1422 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1423 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1424 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1425 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1426 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1427 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | if (PrevMethod) { |
| 1431 | // You can never have two method definitions with the same name. |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1432 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1433 | << ObjCMethod->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1434 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1435 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1436 | return ObjCMethod; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1439 | void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, |
| 1440 | SourceLocation Loc, |
| 1441 | unsigned &Attributes) { |
| 1442 | // FIXME: Improve the reported location. |
| 1443 | |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1444 | // readonly and readwrite/assign/retain/copy conflict. |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1445 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1446 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1447 | ObjCDeclSpec::DQ_PR_assign | |
| 1448 | ObjCDeclSpec::DQ_PR_copy | |
| 1449 | ObjCDeclSpec::DQ_PR_retain))) { |
| 1450 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 1451 | "readwrite" : |
| 1452 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 1453 | "assign" : |
| 1454 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 1455 | "copy" : "retain"; |
| 1456 | |
Fariborz Jahanian | f189290 | 2008-12-08 19:28:10 +0000 | [diff] [blame] | 1457 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Chris Lattner | 0eab08e | 2009-01-29 18:49:48 +0000 | [diff] [blame] | 1458 | diag::err_objc_property_attr_mutually_exclusive : |
| 1459 | diag::warn_objc_property_attr_mutually_exclusive) |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1460 | << "readonly" << which; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | // Check for copy or retain on non-object types. |
| 1464 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && |
| 1465 | !Context.isObjCObjectPointerType(PropertyTy)) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1466 | Diag(Loc, diag::err_objc_property_requires_object) |
| 1467 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1468 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); |
| 1469 | } |
| 1470 | |
| 1471 | // Check for more than one of { assign, copy, retain }. |
| 1472 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 1473 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1474 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1475 | << "assign" << "copy"; |
| 1476 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1477 | } |
| 1478 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1479 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1480 | << "assign" << "retain"; |
| 1481 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1482 | } |
| 1483 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1484 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1485 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1486 | << "copy" << "retain"; |
| 1487 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | // Warn if user supplied no assignment attribute, property is |
| 1492 | // readwrite, and this is an object type. |
| 1493 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
| 1494 | ObjCDeclSpec::DQ_PR_retain)) && |
| 1495 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1496 | Context.isObjCObjectPointerType(PropertyTy)) { |
| 1497 | // Skip this warning in gc-only mode. |
| 1498 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) |
| 1499 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
| 1500 | |
| 1501 | // If non-gc code warn that this is likely inappropriate. |
| 1502 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 1503 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
| 1504 | |
| 1505 | // FIXME: Implement warning dependent on NSCopying being |
| 1506 | // implemented. See also: |
| 1507 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 1508 | // (please trim this list while you are at it). |
| 1509 | } |
| 1510 | } |
| 1511 | |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 1512 | Sema::DeclTy *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 1513 | FieldDeclarator &FD, |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1514 | ObjCDeclSpec &ODS, |
Fariborz Jahanian | b7080ae | 2008-05-06 18:09:04 +0000 | [diff] [blame] | 1515 | Selector GetterSel, |
| 1516 | Selector SetterSel, |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1517 | DeclTy *ClassCategory, |
| 1518 | bool *isOverridingProperty, |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1519 | tok::ObjCKeywordKind MethodImplKind) { |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1520 | unsigned Attributes = ODS.getPropertyAttributes(); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1521 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 1522 | // default is readwrite! |
| 1523 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
| 1524 | // property is defaulted to 'assign' if it is readwrite and is |
| 1525 | // not retain or copy |
| 1526 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
| 1527 | (isReadWrite && |
| 1528 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 1529 | !(Attributes & ObjCDeclSpec::DQ_PR_copy))); |
| 1530 | QualType T = GetTypeForDeclarator(FD.D, S); |
| 1531 | Decl *ClassDecl = static_cast<Decl *>(ClassCategory); |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1532 | |
| 1533 | // May modify Attributes. |
| 1534 | CheckObjCPropertyAttributes(T, AtLoc, Attributes); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1535 | |
| 1536 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 1537 | if (!CDecl->getIdentifier()) { |
| 1538 | // This is an anonymous category. property requires special |
| 1539 | // handling. |
| 1540 | if (ObjCInterfaceDecl *ICDecl = CDecl->getClassInterface()) { |
| 1541 | if (ObjCPropertyDecl *PIDecl = |
| 1542 | ICDecl->FindPropertyDeclaration(FD.D.getIdentifier())) { |
| 1543 | // property 'PIDecl's readonly attribute will be over-ridden |
| 1544 | // with anonymous category's readwrite property attribute! |
| 1545 | unsigned PIkind = PIDecl->getPropertyAttributes(); |
| 1546 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
Fariborz Jahanian | 965242e | 2008-12-08 18:47:29 +0000 | [diff] [blame] | 1547 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1548 | (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 1549 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
| 1550 | PIDecl->makeitReadWriteAttribute(); |
| 1551 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 1552 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 1553 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 1554 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 1555 | PIDecl->setSetterName(SetterSel); |
| 1556 | // FIXME: use a common routine with addPropertyMethods. |
| 1557 | ObjCMethodDecl *SetterDecl = |
| 1558 | ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel, |
| 1559 | Context.VoidTy, |
| 1560 | ICDecl, |
| 1561 | true, false, true, |
| 1562 | ObjCMethodDecl::Required); |
| 1563 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, |
| 1564 | SetterDecl, |
| 1565 | SourceLocation(), |
| 1566 | FD.D.getIdentifier(), |
| 1567 | T, |
| 1568 | VarDecl::None, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1569 | 0); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1570 | SetterDecl->setMethodParams(&Argument, 1); |
| 1571 | PIDecl->setSetterMethodDecl(SetterDecl); |
| 1572 | } |
| 1573 | else |
Fariborz Jahanian | 3d7aa25 | 2008-12-04 22:56:16 +0000 | [diff] [blame] | 1574 | Diag(AtLoc, diag::err_use_continuation_class) << ICDecl->getDeclName(); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1575 | *isOverridingProperty = true; |
| 1576 | return 0; |
| 1577 | } |
Fariborz Jahanian | c123566 | 2008-11-26 20:33:54 +0000 | [diff] [blame] | 1578 | // No matching property found in the main class. Just fall thru |
| 1579 | // and add property to the anonymous category. It looks like |
Ben Laurie | 74d601e | 2009-02-16 09:18:41 +0000 | [diff] [blame] | 1580 | // it works as is. This category becomes just like a category |
| 1581 | // for its primary class. |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1582 | } else { |
| 1583 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 1584 | *isOverridingProperty = true; |
| 1585 | return 0; |
| 1586 | } |
| 1587 | } |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1588 | |
Fariborz Jahanian | f8bfa0c | 2008-12-16 17:51:01 +0000 | [diff] [blame] | 1589 | Type *t = T.getTypePtr(); |
| 1590 | if (t->isArrayType() || t->isFunctionType()) |
| 1591 | Diag(AtLoc, diag::err_property_type) << T; |
| 1592 | |
Steve Naroff | dcf1e84 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1593 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 1594 | assert(DC && "ClassDecl is not a DeclContext"); |
| 1595 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc, |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 1596 | FD.D.getIdentifier(), T); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1597 | DC->addDecl(PDecl); |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1598 | |
| 1599 | ProcessDeclAttributes(PDecl, FD.D); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1600 | |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 1601 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 1602 | // selector names in anticipation of declaration of setter/getter methods. |
| 1603 | PDecl->setGetterName(GetterSel); |
| 1604 | PDecl->setSetterName(SetterSel); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1605 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1606 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1607 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1608 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1609 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1610 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1611 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1612 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1613 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1614 | |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1615 | if (isReadWrite) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1616 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1617 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1618 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1619 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1620 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1621 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1622 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1623 | |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1624 | if (isAssign) |
| 1625 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 1626 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1627 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1628 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1629 | |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1630 | if (MethodImplKind == tok::objc_required) |
| 1631 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 1632 | else if (MethodImplKind == tok::objc_optional) |
| 1633 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
| 1634 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1635 | return PDecl; |
| 1636 | } |
| 1637 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1638 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1639 | /// builds the AST node for a property implementation declaration; declared |
| 1640 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1641 | /// |
| 1642 | Sema::DeclTy *Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
| 1643 | SourceLocation PropertyLoc, |
| 1644 | bool Synthesize, |
| 1645 | DeclTy *ClassCatImpDecl, |
| 1646 | IdentifierInfo *PropertyId, |
| 1647 | IdentifierInfo *PropertyIvar) { |
| 1648 | Decl *ClassImpDecl = static_cast<Decl*>(ClassCatImpDecl); |
| 1649 | // Make sure we have a context for the property implementation declaration. |
| 1650 | if (!ClassImpDecl) { |
| 1651 | Diag(AtLoc, diag::error_missing_property_context); |
| 1652 | return 0; |
| 1653 | } |
| 1654 | ObjCPropertyDecl *property = 0; |
| 1655 | ObjCInterfaceDecl* IDecl = 0; |
| 1656 | // Find the class or category class where this property must have |
| 1657 | // a declaration. |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1658 | ObjCImplementationDecl *IC = 0; |
| 1659 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 1660 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1661 | IDecl = IC->getClassInterface(); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1662 | // We always synthesize an interface for an implementation |
| 1663 | // without an interface decl. So, IDecl is always non-zero. |
| 1664 | assert(IDecl && |
| 1665 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 1666 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1667 | // Look for this property declaration in the @implementation's @interface |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1668 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 1669 | if (!property) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1670 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1671 | return 0; |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1672 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1673 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1674 | else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1675 | if (Synthesize) { |
| 1676 | Diag(AtLoc, diag::error_synthesize_category_decl); |
| 1677 | return 0; |
| 1678 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1679 | IDecl = CatImplClass->getClassInterface(); |
| 1680 | if (!IDecl) { |
| 1681 | Diag(AtLoc, diag::error_missing_property_interface); |
| 1682 | return 0; |
| 1683 | } |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1684 | ObjCCategoryDecl *Category = |
| 1685 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 1686 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1687 | // If category for this implementation not found, it is an error which |
| 1688 | // has already been reported eralier. |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1689 | if (!Category) |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1690 | return 0; |
| 1691 | // Look for this property declaration in @implementation's category |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1692 | property = Category->FindPropertyDeclaration(PropertyId); |
| 1693 | if (!property) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1694 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1695 | << Category->getDeclName(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1696 | return 0; |
| 1697 | } |
| 1698 | } |
| 1699 | else { |
| 1700 | Diag(AtLoc, diag::error_bad_property_context); |
| 1701 | return 0; |
| 1702 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1703 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1704 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1705 | if (Synthesize) { |
| 1706 | // @synthesize |
Fariborz Jahanian | 1f02800 | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1707 | if (!PropertyIvar) |
| 1708 | PropertyIvar = PropertyId; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1709 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | beae78e | 2009-02-16 19:35:27 +0000 | [diff] [blame] | 1710 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1711 | if (!Ivar) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1712 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1713 | return 0; |
| 1714 | } |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1715 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1716 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 1717 | |
Steve Naroff | 631e392 | 2008-09-30 00:24:17 +0000 | [diff] [blame] | 1718 | // Check that type of property and its ivar are type compatible. |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1719 | if (PropType != IvarType) { |
Steve Naroff | 3557a34 | 2008-10-16 14:59:30 +0000 | [diff] [blame] | 1720 | if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1721 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1722 | << property->getDeclName() << Ivar->getDeclName(); |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
Fariborz Jahanian | 6ec8955 | 2009-01-19 20:13:47 +0000 | [diff] [blame] | 1725 | else { |
| 1726 | // FIXME! Rules for properties are somewhat different that those |
| 1727 | // for assignments. Use a new routine to consolidate all cases; |
| 1728 | // specifically for property redeclarations as well as for ivars. |
| 1729 | QualType lhsType = |
| 1730 | Context.getCanonicalType(PropType).getUnqualifiedType(); |
| 1731 | QualType rhsType = |
| 1732 | Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 1733 | if (lhsType != rhsType && |
| 1734 | lhsType->isArithmeticType()) { |
| 1735 | Diag(PropertyLoc, diag::error_property_ivar_type) |
| 1736 | << property->getDeclName() << Ivar->getDeclName(); |
| 1737 | return 0; |
| 1738 | } |
| 1739 | } |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1740 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1741 | } else if (PropertyIvar) { |
| 1742 | // @dynamic |
| 1743 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
| 1744 | return 0; |
| 1745 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1746 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1747 | ObjCPropertyImplDecl *PIDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1748 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 1749 | property, |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1750 | (Synthesize ? |
Daniel Dunbar | 14117fc | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 1751 | ObjCPropertyImplDecl::Synthesize |
| 1752 | : ObjCPropertyImplDecl::Dynamic), |
| 1753 | Ivar); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1754 | CurContext->addDecl(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1755 | if (IC) { |
| 1756 | if (Synthesize) |
| 1757 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1758 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1759 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1760 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1761 | << PropertyIvar; |
| 1762 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1763 | } |
| 1764 | |
| 1765 | if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) { |
| 1766 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1767 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
| 1768 | return 0; |
| 1769 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1770 | IC->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1771 | } |
| 1772 | else { |
| 1773 | if (Synthesize) |
| 1774 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1775 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1776 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1777 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1778 | << PropertyIvar; |
| 1779 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1780 | } |
| 1781 | |
| 1782 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1783 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
| 1784 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1785 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
| 1786 | return 0; |
| 1787 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1788 | CatImplClass->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1789 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1790 | |
| 1791 | return PIDecl; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1792 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1793 | |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1794 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Douglas Gregor | 69e781f | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1795 | if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1796 | return false; |
| 1797 | |
| 1798 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 1799 | D->setInvalidDecl(); |
| 1800 | |
| 1801 | return true; |
| 1802 | } |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1803 | |
| 1804 | /// Collect the instance variables declared in an Objective-C object. Used in |
| 1805 | /// the creation of structures from objects using the @defs directive. |
| 1806 | /// FIXME: This should be consolidated with CollectObjCIvars as it is also |
| 1807 | /// part of the AST generation logic of @defs. |
| 1808 | static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record, |
| 1809 | ASTContext& Ctx, |
| 1810 | llvm::SmallVectorImpl<Sema::DeclTy*> &ivars) { |
| 1811 | if (Class->getSuperClass()) |
| 1812 | CollectIvars(Class->getSuperClass(), Record, Ctx, ivars); |
| 1813 | |
| 1814 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
| 1815 | for (ObjCInterfaceDecl::ivar_iterator |
| 1816 | I=Class->ivar_begin(), E=Class->ivar_end(); I!=E; ++I) { |
| 1817 | |
| 1818 | ObjCIvarDecl* ID = *I; |
| 1819 | ivars.push_back(ObjCAtDefsFieldDecl::Create(Ctx, Record, |
| 1820 | ID->getLocation(), |
| 1821 | ID->getIdentifier(), |
| 1822 | ID->getType(), |
| 1823 | ID->getBitWidth())); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 1828 | /// instance variables of ClassName into Decls. |
| 1829 | void Sema::ActOnDefs(Scope *S, DeclTy *TagD, SourceLocation DeclStart, |
| 1830 | IdentifierInfo *ClassName, |
| 1831 | llvm::SmallVectorImpl<DeclTy*> &Decls) { |
| 1832 | // Check that ClassName is a valid class |
| 1833 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 1834 | if (!Class) { |
| 1835 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 1836 | return; |
| 1837 | } |
| 1838 | // Collect the instance variables |
| 1839 | CollectIvars(Class, dyn_cast<RecordDecl>((Decl*)TagD), Context, Decls); |
| 1840 | |
| 1841 | // Introduce all of these fields into the appropriate scope. |
| 1842 | for (llvm::SmallVectorImpl<DeclTy*>::iterator D = Decls.begin(); |
| 1843 | D != Decls.end(); ++D) { |
| 1844 | FieldDecl *FD = cast<FieldDecl>((Decl*)*D); |
| 1845 | if (getLangOptions().CPlusPlus) |
| 1846 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
| 1847 | else if (RecordDecl *Record = dyn_cast<RecordDecl>((Decl*)TagD)) |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1848 | Record->addDecl(FD); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1849 | } |
| 1850 | } |
| 1851 | |