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