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