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