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