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