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