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