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