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