Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 24 | ExprTy **strings, |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 25 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 26 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 27 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 28 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 29 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 30 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 31 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 32 | StringLiteral *S = Strings[0]; |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 33 | |
| 34 | // If we have a multi-part string, merge it all together. |
| 35 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 36 | // Concatenate objc strings. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 37 | llvm::SmallString<128> StrBuf; |
| 38 | llvm::SmallVector<SourceLocation, 8> StrLocs; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 40 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 41 | S = Strings[i]; |
| 42 | |
| 43 | // ObjC strings can't be wide. |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 44 | if (S->isWide()) { |
| 45 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 46 | << S->getSourceRange(); |
| 47 | return true; |
| 48 | } |
| 49 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 50 | // Get the string data. |
| 51 | StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength()); |
| 52 | |
| 53 | // Get the locations of the string tokens. |
| 54 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
| 55 | |
| 56 | // Free the temporary string. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 57 | S->Destroy(Context); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 59 | |
| 60 | // Create the aggregate string with the appropriate content and location |
| 61 | // information. |
| 62 | S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false, |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 63 | Context.getPointerType(Context.CharTy), |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 64 | &StrLocs[0], StrLocs.size()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 67 | // Verify that this composite string is acceptable for ObjC strings. |
| 68 | if (CheckObjCString(S)) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 69 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 70 | |
| 71 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 72 | // the NSString interface is seen in this translation unit. Note: We |
| 73 | // don't use NSConstantString, since the runtime team considers this |
| 74 | // interface private (even though it appears in the header files). |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 75 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 76 | if (!Ty.isNull()) { |
| 77 | Ty = Context.getPointerType(Ty); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 78 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 79 | IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 80 | NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName); |
| 81 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 82 | Context.setObjCConstantStringInterface(StrIF); |
| 83 | Ty = Context.getObjCConstantStringInterface(); |
| 84 | Ty = Context.getPointerType(Ty); |
| 85 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 86 | // If there is no NSString interface defined then treat constant |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 87 | // strings as untyped objects and let the runtime figure it out later. |
| 88 | Ty = Context.getObjCIdType(); |
| 89 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 91 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 92 | return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 96 | SourceLocation EncodeLoc, |
| 97 | SourceLocation LParenLoc, |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 98 | TypeTy *ty, |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 99 | SourceLocation RParenLoc) { |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 100 | QualType EncodedType = QualType::getFromOpaquePtr(ty); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 101 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 102 | std::string Str; |
| 103 | Context.getObjCEncodingForType(EncodedType, Str); |
| 104 | |
| 105 | // The type of @encode is the same as the type of the corresponding string, |
| 106 | // which is an array type. |
| 107 | QualType StrTy = Context.CharTy; |
| 108 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 109 | if (getLangOptions().CPlusPlus) |
| 110 | StrTy.addConst(); |
| 111 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 112 | ArrayType::Normal, 0); |
| 113 | |
| 114 | return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 118 | SourceLocation AtLoc, |
| 119 | SourceLocation SelLoc, |
| 120 | SourceLocation LParenLoc, |
| 121 | SourceLocation RParenLoc) { |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 122 | QualType Ty = Context.getObjCSelType(); |
| 123 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 127 | SourceLocation AtLoc, |
| 128 | SourceLocation ProtoLoc, |
| 129 | SourceLocation LParenLoc, |
| 130 | SourceLocation RParenLoc) { |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 131 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 132 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 133 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 137 | QualType Ty = Context.getObjCProtoType(); |
| 138 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 139 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 140 | Ty = Context.getPointerType(Ty); |
| 141 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 144 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
| 145 | Selector Sel, ObjCMethodDecl *Method, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 146 | bool isClassMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 147 | SourceLocation lbrac, SourceLocation rbrac, |
| 148 | QualType &ReturnType) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 149 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 150 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
| 151 | for (unsigned i = 0; i != NumArgs; i++) |
| 152 | DefaultArgumentPromotion(Args[i]); |
| 153 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 154 | unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found : |
| 155 | diag::warn_inst_method_not_found; |
| 156 | Diag(lbrac, DiagID) |
| 157 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 158 | ReturnType = Context.getObjCIdType(); |
| 159 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 161 | |
| 162 | ReturnType = Method->getResultType(); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 163 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 164 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| 165 | assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!"); |
| 166 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 167 | bool IsError = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 168 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 169 | Expr *argExpr = Args[i]; |
| 170 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 171 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 172 | QualType lhsType = Method->param_begin()[i]->getType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 173 | QualType rhsType = argExpr->getType(); |
| 174 | |
| 175 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 176 | if (lhsType->isArrayType()) |
| 177 | lhsType = Context.getArrayDecayedType(lhsType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 178 | else if (lhsType->isFunctionType()) |
| 179 | lhsType = Context.getPointerType(lhsType); |
| 180 | |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 181 | AssignConvertType Result = |
| 182 | CheckSingleAssignmentConstraints(lhsType, argExpr); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 183 | if (Args[i] != argExpr) // The expression was converted. |
| 184 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 185 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 186 | IsError |= |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 187 | DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, |
| 188 | argExpr, "sending"); |
| 189 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 190 | |
| 191 | // Promote additional arguments to variadic methods. |
| 192 | if (Method->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 193 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 194 | IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 195 | } else { |
| 196 | // Check for extra arguments to non-variadic methods. |
| 197 | if (NumArgs != NumNamedArgs) { |
| 198 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 199 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 200 | << 2 /*method*/ << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 201 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 202 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 206 | return IsError; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 209 | bool Sema::isSelfExpr(Expr *RExpr) { |
| 210 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr)) |
| 211 | if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) |
| 212 | return true; |
| 213 | return false; |
| 214 | } |
| 215 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 216 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 217 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 218 | // If failed, then we search in class's root for an instance method. |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 219 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 220 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 221 | ObjCInterfaceDecl *ClassDecl) { |
| 222 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 223 | // lookup in class and all superclasses |
| 224 | while (ClassDecl && !Method) { |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 225 | if (ObjCImplementationDecl *ImpDecl |
| 226 | = LookupObjCImplementation(ClassDecl->getIdentifier())) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 227 | Method = ImpDecl->getClassMethod(Context, Sel); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 228 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 229 | // Look through local category implementations associated with the class. |
| 230 | if (!Method) { |
| 231 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) { |
| 232 | if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 233 | Method = ObjCCategoryImpls[i]->getClassMethod(Context, Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 234 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 235 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 236 | |
| 237 | // Before we give up, check if the selector is an instance method. |
| 238 | // But only in the root. This matches gcc's behaviour and what the |
| 239 | // runtime expects. |
| 240 | if (!Method && !ClassDecl->getSuperClass()) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 241 | Method = ClassDecl->lookupInstanceMethod(Context, Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 242 | // Look through local category implementations associated |
| 243 | // with the root class. |
| 244 | if (!Method) |
| 245 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 246 | } |
| 247 | |
| 248 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 249 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 250 | return Method; |
| 251 | } |
| 252 | |
| 253 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 254 | ObjCInterfaceDecl *ClassDecl) { |
| 255 | ObjCMethodDecl *Method = 0; |
| 256 | while (ClassDecl && !Method) { |
| 257 | // If we have implementations in scope, check "private" methods. |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 258 | if (ObjCImplementationDecl *ImpDecl |
| 259 | = LookupObjCImplementation(ClassDecl->getIdentifier())) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 260 | Method = ImpDecl->getInstanceMethod(Context, Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 261 | |
| 262 | // Look through local category implementations associated with the class. |
| 263 | if (!Method) { |
| 264 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) { |
| 265 | if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 266 | Method = ObjCCategoryImpls[i]->getInstanceMethod(Context, Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 270 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 271 | return Method; |
| 272 | } |
| 273 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 274 | Action::OwningExprResult Sema::ActOnClassPropertyRefExpr( |
| 275 | IdentifierInfo &receiverName, |
| 276 | IdentifierInfo &propertyName, |
| 277 | SourceLocation &receiverNameLoc, |
| 278 | SourceLocation &propertyNameLoc) { |
| 279 | |
| 280 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName); |
| 281 | |
| 282 | // Search for a declared property first. |
| 283 | |
| 284 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 285 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Context, Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 286 | |
| 287 | // If this reference is in an @implementation, check for 'private' methods. |
| 288 | if (!Getter) |
| 289 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 290 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 291 | if (ObjCImplementationDecl *ImpDecl |
| 292 | = LookupObjCImplementation(ClassDecl->getIdentifier())) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 293 | Getter = ImpDecl->getClassMethod(Context, Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 294 | |
| 295 | if (Getter) { |
| 296 | // FIXME: refactor/share with ActOnMemberReference(). |
| 297 | // Check if we can reference this property. |
| 298 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 299 | return ExprError(); |
| 300 | } |
| 301 | |
| 302 | // Look for the matching setter, in case it is needed. |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 303 | Selector SetterSel = |
| 304 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 305 | PP.getSelectorTable(), &propertyName); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 307 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(Context, SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 308 | if (!Setter) { |
| 309 | // If this reference is in an @implementation, also check for 'private' |
| 310 | // methods. |
| 311 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 312 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 313 | if (ObjCImplementationDecl *ImpDecl |
| 314 | = LookupObjCImplementation(ClassDecl->getIdentifier())) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 315 | Setter = ImpDecl->getClassMethod(Context, SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 316 | } |
| 317 | // Look through local category implementations associated with the class. |
| 318 | if (!Setter) { |
| 319 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 320 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 321 | Setter = ObjCCategoryImpls[i]->getClassMethod(Context, SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 326 | return ExprError(); |
| 327 | |
| 328 | if (Getter || Setter) { |
| 329 | QualType PType; |
| 330 | |
| 331 | if (Getter) |
| 332 | PType = Getter->getResultType(); |
| 333 | else { |
| 334 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 335 | E = Setter->param_end(); PI != E; ++PI) |
| 336 | PType = (*PI)->getType(); |
| 337 | } |
| 338 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter, |
| 339 | propertyNameLoc, IFace, receiverNameLoc)); |
| 340 | } |
| 341 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 342 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 343 | } |
| 344 | |
| 345 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 346 | // ActOnClassMessage - used for both unary and keyword messages. |
| 347 | // ArgExprs is optional - if it is present, the number of expressions |
| 348 | // is obtained from Sel.getNumArgs(). |
| 349 | Sema::ExprResult Sema::ActOnClassMessage( |
| 350 | Scope *S, |
| 351 | IdentifierInfo *receiverName, Selector Sel, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 352 | SourceLocation lbrac, SourceLocation receiverLoc, |
| 353 | SourceLocation selectorLoc, SourceLocation rbrac, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 354 | ExprTy **Args, unsigned NumArgs) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 355 | { |
| 356 | assert(receiverName && "missing receiver class name"); |
| 357 | |
| 358 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 359 | ObjCInterfaceDecl* ClassDecl = 0; |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 360 | bool isSuper = false; |
| 361 | |
Chris Lattner | 8469265 | 2008-11-20 05:35:30 +0000 | [diff] [blame] | 362 | if (receiverName->isStr("super")) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 363 | if (getCurMethodDecl()) { |
| 364 | isSuper = true; |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 365 | ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface(); |
| 366 | if (!OID) |
| 367 | return Diag(lbrac, diag::error_no_super_class_message) |
| 368 | << getCurMethodDecl()->getDeclName(); |
| 369 | ClassDecl = OID->getSuperClass(); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 370 | if (!ClassDecl) |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 371 | return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 372 | if (getCurMethodDecl()->isInstanceMethod()) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 373 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
| 374 | superTy = Context.getPointerType(superTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 375 | ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(), |
| 376 | superTy); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 377 | // We are really in an instance method, redirect. |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 378 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, |
| 379 | selectorLoc, rbrac, Args, NumArgs); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 380 | } |
| 381 | // We are sending a message to 'super' within a class method. Do nothing, |
| 382 | // the receiver will pass through as 'super' (how convenient:-). |
| 383 | } else { |
| 384 | // 'super' has been used outside a method context. If a variable named |
| 385 | // 'super' has been declared, redirect. If not, produce a diagnostic. |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 386 | NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 387 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl); |
| 388 | if (VD) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 389 | ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(), |
| 390 | receiverLoc); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 391 | // We are really in an instance method, redirect. |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 392 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, |
| 393 | selectorLoc, rbrac, Args, NumArgs); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 395 | return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName; |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 396 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 397 | } else |
| 398 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 399 | |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 400 | // The following code allows for the following GCC-ism: |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 401 | // |
| 402 | // typedef XCElementDisplayRect XCElementGraphicsRect; |
| 403 | // |
| 404 | // @implementation XCRASlice |
| 405 | // - whatever { // Note that XCElementGraphicsRect is a typedef name. |
| 406 | // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init]; |
| 407 | // } |
| 408 | // |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 409 | // If necessary, the following lookup could move to getObjCInterfaceDecl(). |
| 410 | if (!ClassDecl) { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 411 | NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 412 | if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) { |
| 413 | const ObjCInterfaceType *OCIT; |
| 414 | OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType(); |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame] | 415 | if (!OCIT) { |
| 416 | Diag(receiverLoc, diag::err_invalid_receiver_to_message); |
| 417 | return true; |
| 418 | } |
Fariborz Jahanian | ebff1fe | 2009-01-16 20:35:09 +0000 | [diff] [blame] | 419 | ClassDecl = OCIT->getDecl(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | assert(ClassDecl && "missing interface declaration"); |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 423 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 424 | QualType returnType; |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 425 | if (ClassDecl->isForwardDecl()) { |
| 426 | // A forward class used in messaging is tread as a 'Class' |
Fariborz Jahanian | 9f8f026 | 2009-05-08 23:45:49 +0000 | [diff] [blame] | 427 | Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 428 | Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac)); |
| 429 | if (Method) |
Fariborz Jahanian | 9f8f026 | 2009-05-08 23:45:49 +0000 | [diff] [blame] | 430 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 431 | << Method->getDeclName(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 432 | } |
| 433 | if (!Method) |
| 434 | Method = ClassDecl->lookupClassMethod(Context, Sel); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 435 | |
| 436 | // If we have an implementation in scope, check "private" methods. |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 437 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 438 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 440 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 441 | return true; |
Anders Carlsson | 59843ad | 2009-02-14 19:08:58 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 443 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 444 | lbrac, rbrac, returnType)) |
| 445 | return true; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 446 | |
| 447 | // If we have the ObjCInterfaceDecl* for the class that is receiving |
| 448 | // the message, use that to construct the ObjCMessageExpr. Otherwise |
| 449 | // pass on the IdentifierInfo* for the class. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 450 | // FIXME: need to do a better job handling 'super' usage within a class |
| 451 | // For now, we simply pass the "super" identifier through (which isn't |
| 452 | // consistent with instance methods. |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 453 | if (isSuper) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 454 | return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 455 | lbrac, rbrac, ArgExprs, NumArgs); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 456 | else |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 457 | return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method, |
| 458 | lbrac, rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 462 | // ArgExprs is optional - if it is present, the number of expressions |
| 463 | // is obtained from Sel.getNumArgs(). |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 464 | Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 465 | SourceLocation lbrac, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 466 | SourceLocation receiverLoc, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 467 | SourceLocation rbrac, |
| 468 | ExprTy **Args, unsigned NumArgs) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 469 | assert(receiver && "missing receiver expression"); |
| 470 | |
| 471 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 472 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | d0d4599 | 2009-04-29 05:48:32 +0000 | [diff] [blame] | 473 | |
| 474 | // If necessary, apply function/array conversion to the receiver. |
| 475 | // C99 6.7.5.3p[7,8]. |
| 476 | DefaultFunctionArrayConversion(RExpr); |
| 477 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 478 | QualType returnType; |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 479 | QualType ReceiverCType = |
| 480 | Context.getCanonicalType(RExpr->getType()).getUnqualifiedType(); |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 481 | |
| 482 | // Handle messages to 'super'. |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 483 | if (isa<ObjCSuperExpr>(RExpr)) { |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 484 | ObjCMethodDecl *Method = 0; |
| 485 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 486 | // If we have an interface in scope, check 'super' methods. |
| 487 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 488 | if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 489 | Method = SuperDecl->lookupInstanceMethod(Context, Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 490 | |
| 491 | if (!Method) |
| 492 | // If we have implementations in scope, check "private" methods. |
| 493 | Method = LookupPrivateInstanceMethod(Sel, SuperDecl); |
| 494 | } |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 495 | } |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 497 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 498 | return true; |
Anders Carlsson | 59843ad | 2009-02-14 19:08:58 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 500 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 501 | lbrac, rbrac, returnType)) |
| 502 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 503 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 504 | rbrac, ArgExprs, NumArgs); |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 507 | // Handle messages to id. |
Steve Naroff | 6c4088e | 2008-09-29 16:51:41 +0000 | [diff] [blame] | 508 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) || |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 509 | ReceiverCType->isBlockPointerType()) { |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 510 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool( |
| 511 | Sel, SourceRange(lbrac,rbrac)); |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 512 | if (!Method) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 513 | Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac)); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 514 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 515 | lbrac, rbrac, returnType)) |
| 516 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 517 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 518 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Handle messages to Class. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 522 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 523 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 525 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 526 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 527 | // First check the public methods in the class interface. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 528 | Method = ClassDecl->lookupClassMethod(Context, Sel); |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 529 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 530 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 531 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 532 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 533 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 534 | return true; |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 535 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 536 | if (!Method) { |
| 537 | // If not messaging 'self', look for any factory method named 'Sel'. |
| 538 | if (!isSelfExpr(RExpr)) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 539 | Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac)); |
Fariborz Jahanian | b1006c7 | 2009-03-04 17:50:39 +0000 | [diff] [blame] | 540 | if (!Method) { |
Fariborz Jahanian | 041f2fd | 2009-05-05 18:34:37 +0000 | [diff] [blame] | 541 | // If no class (factory) method was found, check if an _instance_ |
| 542 | // method of the same name exists in the root class only. |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 543 | Method = LookupInstanceMethodInGlobalPool( |
| 544 | Sel, SourceRange(lbrac,rbrac)); |
Fariborz Jahanian | 041f2fd | 2009-05-05 18:34:37 +0000 | [diff] [blame] | 545 | if (Method) |
| 546 | if (const ObjCInterfaceDecl *ID = |
| 547 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 548 | if (ID->getSuperClass()) |
| 549 | Diag(lbrac, diag::warn_root_inst_method_not_found) |
| 550 | << Sel << SourceRange(lbrac, rbrac); |
| 551 | } |
Fariborz Jahanian | b1006c7 | 2009-03-04 17:50:39 +0000 | [diff] [blame] | 552 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 555 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 556 | lbrac, rbrac, returnType)) |
| 557 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 558 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 559 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 562 | ObjCMethodDecl *Method = 0; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 563 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 564 | |
| 565 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 566 | // long as one of the protocols implements the selector (if not, warn). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 567 | if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) { |
Steve Naroff | f7f52e7 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 568 | // Search protocols for instance methods. |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 569 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
| 570 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 571 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Context, Sel))) |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 572 | break; |
Steve Naroff | ebaa768 | 2009-04-07 15:07:57 +0000 | [diff] [blame] | 573 | // Since we aren't supporting "Class<foo>", look for a class method. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 574 | if (PDecl && (Method = PDecl->lookupClassMethod(Context, Sel))) |
Steve Naroff | ebaa768 | 2009-04-07 15:07:57 +0000 | [diff] [blame] | 575 | break; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 576 | } |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 577 | } else if (const ObjCInterfaceType *OCIType = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 578 | ReceiverCType->getAsPointerToObjCInterfaceType()) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 579 | // We allow sending a message to a pointer to an interface (an object). |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 580 | |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 581 | ClassDecl = OCIType->getDecl(); |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 582 | // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be |
| 583 | // faster than the following method (which can do *many* linear searches). |
| 584 | // The idea is to add class info to InstanceMethodPool. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 585 | Method = ClassDecl->lookupInstanceMethod(Context, Sel); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 586 | |
| 587 | if (!Method) { |
| 588 | // Search protocol qualifiers. |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 589 | for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(), |
| 590 | E = OCIType->qual_end(); QI != E; ++QI) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 591 | if ((Method = (*QI)->lookupInstanceMethod(Context, Sel))) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 592 | break; |
| 593 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 594 | } |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 595 | if (!Method) { |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 596 | // If we have implementations in scope, check "private" methods. |
| 597 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 598 | |
| 599 | if (!Method && !isSelfExpr(RExpr)) { |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 600 | // If we still haven't found a method, look in the global pool. This |
| 601 | // behavior isn't very desirable, however we need it for GCC |
| 602 | // compatibility. FIXME: should we deviate?? |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 603 | if (OCIType->qual_empty()) { |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 604 | Method = LookupInstanceMethodInGlobalPool( |
| 605 | Sel, SourceRange(lbrac,rbrac)); |
| 606 | if (Method && !OCIType->getDecl()->isForwardDecl()) |
| 607 | Diag(lbrac, diag::warn_maynot_respond) |
| 608 | << OCIType->getDecl()->getIdentifier()->getName() << Sel; |
| 609 | } |
Fariborz Jahanian | 268bc8c | 2009-03-03 22:19:15 +0000 | [diff] [blame] | 610 | } |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 611 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 612 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 613 | return true; |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 614 | } else if (!Context.getObjCIdType().isNull() && |
| 615 | (ReceiverCType->isPointerType() || |
| 616 | (ReceiverCType->isIntegerType() && |
| 617 | ReceiverCType->isScalarType()))) { |
| 618 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
Steve Naroff | 8e2945a | 2009-03-01 17:14:31 +0000 | [diff] [blame] | 619 | Diag(lbrac, diag::warn_bad_receiver_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 620 | << RExpr->getType() << RExpr->getSourceRange(); |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 621 | ImpCastExprToType(RExpr, Context.getObjCIdType()); |
| 622 | } else { |
| 623 | // Reject other random receiver types (e.g. structs). |
| 624 | Diag(lbrac, diag::err_bad_receiver_type) |
| 625 | << RExpr->getType() << RExpr->getSourceRange(); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 626 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 629 | if (Method) |
| 630 | DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 631 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 632 | lbrac, rbrac, returnType)) |
| 633 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 634 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 635 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 636 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 637 | |
| 638 | //===----------------------------------------------------------------------===// |
| 639 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | |
| 642 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 643 | /// inheritance hierarchy of 'rProto'. |
| 644 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 645 | ObjCProtocolDecl *rProto) { |
| 646 | if (lProto == rProto) |
| 647 | return true; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 648 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 649 | E = rProto->protocol_end(); PI != E; ++PI) |
| 650 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 651 | return true; |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 656 | /// has been implemented in IDecl class, its super class or categories (if |
| 657 | /// lookupCategory is true). |
| 658 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 659 | ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 660 | bool lookupCategory, |
| 661 | bool RHSIsQualifiedID = false) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 662 | |
| 663 | // 1st, look up the class. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 664 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 665 | IDecl->getReferencedProtocols(); |
| 666 | |
| 667 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 668 | E = Protocols.end(); PI != E; ++PI) { |
| 669 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 670 | return true; |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 671 | // This is dubious and is added to be compatible with gcc. |
| 672 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 673 | // type to a LHS object when protocol in qualified LHS is in list |
| 674 | // of protocols in the rhs 'id' object. This IMO, should be a bug. |
Ted Kremenek | fd5b2ce | 2008-06-04 20:48:08 +0000 | [diff] [blame] | 675 | // FIXME: Treat this as an extension, and flag this as an error when |
| 676 | // GCC extensions are not enabled. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 677 | if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto)) |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 678 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | // 2nd, look up the category. |
| 682 | if (lookupCategory) |
| 683 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 684 | CDecl = CDecl->getNextClassCategory()) { |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 685 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 686 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 687 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 688 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | // 3rd, look up the super class(s) |
| 692 | if (IDecl->getSuperClass()) |
| 693 | return |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 694 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 695 | RHSIsQualifiedID); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 696 | |
| 697 | return false; |
| 698 | } |
| 699 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 700 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 701 | /// ObjCQualifiedIDType. |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 702 | /// FIXME: Move to ASTContext::typesAreCompatible() and friends. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 703 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 704 | bool compare) { |
| 705 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 706 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 707 | QualType PointeeTy = PT->getPointeeType(); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 708 | if (PointeeTy->isVoidType() || |
| 709 | Context.isObjCIdStructType(PointeeTy) || |
| 710 | Context.isObjCClassStructType(PointeeTy)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 711 | return true; |
| 712 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 713 | QualType PointeeTy = PT->getPointeeType(); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 714 | if (PointeeTy->isVoidType() || |
| 715 | Context.isObjCIdStructType(PointeeTy) || |
| 716 | Context.isObjCClassStructType(PointeeTy)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 720 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 721 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 722 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 723 | QualType rtype; |
| 724 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 725 | if (!rhsQID) { |
| 726 | // Not comparing two ObjCQualifiedIdType's? |
| 727 | if (!rhs->isPointerType()) return false; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 728 | |
| 729 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 730 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 731 | if (rhsQI == 0) { |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 732 | // If the RHS is a unqualified interface pointer "NSString*", |
| 733 | // make sure we check the class hierarchy. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 734 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 735 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 736 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 737 | // when comparing an id<P> on lhs with a static type on rhs, |
| 738 | // see if static class implements all of id's protocols, directly or |
| 739 | // through its super class and categories. |
| 740 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 741 | return false; |
| 742 | } |
| 743 | return true; |
| 744 | } |
| 745 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 746 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 747 | |
| 748 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 749 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 750 | RHSProtoI = rhsQI->qual_begin(); |
| 751 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 752 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 753 | RHSProtoI = rhsQID->qual_begin(); |
| 754 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 755 | } else { |
| 756 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 760 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 761 | bool match = false; |
| 762 | |
| 763 | // when comparing an id<P> on lhs with a static type on rhs, |
| 764 | // see if static class implements all of id's protocols, directly or |
| 765 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 766 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 767 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 768 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 769 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 770 | match = true; |
| 771 | break; |
| 772 | } |
| 773 | } |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 774 | if (rhsQI) { |
| 775 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 776 | // make sure we check the class hierarchy. |
| 777 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 778 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 779 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 780 | // when comparing an id<P> on lhs with a static type on rhs, |
| 781 | // see if static class implements all of id's protocols, directly or |
| 782 | // through its super class and categories. |
| 783 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 784 | match = true; |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 790 | if (!match) |
| 791 | return false; |
| 792 | } |
| 793 | |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 798 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 799 | |
| 800 | if (!lhs->isPointerType()) |
| 801 | return false; |
| 802 | |
| 803 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 804 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 805 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 806 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 807 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 808 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 809 | bool match = false; |
| 810 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 811 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 812 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 813 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 814 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 815 | match = true; |
| 816 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 819 | if (!match) |
| 820 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 821 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 822 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 823 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 824 | |
| 825 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 826 | // for static type vs. qualified 'id' type, check that class implements |
| 827 | // all of 'id's protocols. |
| 828 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 829 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 830 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 831 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 832 | return false; |
| 833 | } |
| 834 | return true; |
| 835 | } |
| 836 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 837 | } |
| 838 | |