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