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