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