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