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