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 | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 21 | ExprTy **Strings, |
| 22 | unsigned NumStrings) { |
| 23 | SourceLocation AtLoc = AtLocs[0]; |
| 24 | StringLiteral* S = static_cast<StringLiteral *>(Strings[0]); |
| 25 | if (NumStrings > 1) { |
| 26 | // Concatenate objc strings. |
| 27 | StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]); |
| 28 | SourceLocation EndLoc = ES->getSourceRange().getEnd(); |
| 29 | unsigned Length = 0; |
| 30 | for (unsigned i = 0; i < NumStrings; i++) |
| 31 | Length += static_cast<StringLiteral *>(Strings[i])->getByteLength(); |
| 32 | char *strBuf = new char [Length]; |
| 33 | char *p = strBuf; |
| 34 | bool isWide = false; |
| 35 | for (unsigned i = 0; i < NumStrings; i++) { |
| 36 | S = static_cast<StringLiteral *>(Strings[i]); |
| 37 | if (S->isWide()) |
| 38 | isWide = true; |
| 39 | memcpy(p, S->getStrData(), S->getByteLength()); |
| 40 | p += S->getByteLength(); |
| 41 | delete S; |
| 42 | } |
| 43 | S = new StringLiteral(strBuf, Length, |
| 44 | isWide, Context.getPointerType(Context.CharTy), |
| 45 | AtLoc, EndLoc); |
| 46 | } |
| 47 | |
| 48 | if (CheckBuiltinCFStringArgument(S)) |
| 49 | return true; |
| 50 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 51 | if (Context.getObjCConstantStringInterface().isNull()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 52 | // Initialize the constant string interface lazily. This assumes |
| 53 | // the NSConstantString interface is seen in this translation unit. |
| 54 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 55 | Decl *IFace = LookupName(TUScope, NSIdent, LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 56 | ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 57 | if (strIFace) |
| 58 | Context.setObjCConstantStringInterface(strIFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 60 | QualType t = Context.getObjCConstantStringInterface(); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 61 | // If there is no NSConstantString interface defined then treat constant |
| 62 | // strings as untyped objects and let the runtime figure it out later. |
| 63 | if (t == QualType()) { |
| 64 | t = Context.getObjCIdType(); |
| 65 | } else { |
| 66 | t = Context.getPointerType(t); |
| 67 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 68 | return new ObjCStringLiteral(S, t, AtLoc); |
| 69 | } |
| 70 | |
| 71 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 72 | SourceLocation EncodeLoc, |
| 73 | SourceLocation LParenLoc, |
| 74 | TypeTy *Ty, |
| 75 | SourceLocation RParenLoc) { |
| 76 | QualType EncodedType = QualType::getFromOpaquePtr(Ty); |
| 77 | |
| 78 | QualType t = Context.getPointerType(Context.CharTy); |
| 79 | return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); |
| 80 | } |
| 81 | |
| 82 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 83 | SourceLocation AtLoc, |
| 84 | SourceLocation SelLoc, |
| 85 | SourceLocation LParenLoc, |
| 86 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 87 | QualType t = Context.getObjCSelType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 88 | return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); |
| 89 | } |
| 90 | |
| 91 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 92 | SourceLocation AtLoc, |
| 93 | SourceLocation ProtoLoc, |
| 94 | SourceLocation LParenLoc, |
| 95 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 97 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 98 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 102 | QualType t = Context.getObjCProtoType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 103 | if (t.isNull()) |
| 104 | return true; |
| 105 | t = Context.getPointerType(t); |
| 106 | return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); |
| 107 | } |
| 108 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 109 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
| 110 | Selector Sel, ObjCMethodDecl *Method, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 111 | bool isClassMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 112 | SourceLocation lbrac, SourceLocation rbrac, |
| 113 | QualType &ReturnType) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 114 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 115 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
| 116 | for (unsigned i = 0; i != NumArgs; i++) |
| 117 | DefaultArgumentPromotion(Args[i]); |
| 118 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 119 | unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found : |
| 120 | diag::warn_inst_method_not_found; |
| 121 | Diag(lbrac, DiagID) |
| 122 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 123 | ReturnType = Context.getObjCIdType(); |
| 124 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 126 | |
| 127 | ReturnType = Method->getResultType(); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 129 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| 130 | assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!"); |
| 131 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 132 | bool anyIncompatibleArgs = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 133 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 134 | Expr *argExpr = Args[i]; |
| 135 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 136 | |
| 137 | QualType lhsType = Method->getParamDecl(i)->getType(); |
| 138 | QualType rhsType = argExpr->getType(); |
| 139 | |
| 140 | // 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] | 141 | if (lhsType->isArrayType()) |
| 142 | lhsType = Context.getArrayDecayedType(lhsType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 143 | else if (lhsType->isFunctionType()) |
| 144 | lhsType = Context.getPointerType(lhsType); |
| 145 | |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 146 | AssignConvertType Result = |
| 147 | CheckSingleAssignmentConstraints(lhsType, argExpr); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 148 | if (Args[i] != argExpr) // The expression was converted. |
| 149 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 150 | |
| 151 | anyIncompatibleArgs |= |
| 152 | DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, |
| 153 | argExpr, "sending"); |
| 154 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 155 | |
| 156 | // Promote additional arguments to variadic methods. |
| 157 | if (Method->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 158 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) |
| 159 | DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 160 | } else { |
| 161 | // Check for extra arguments to non-variadic methods. |
| 162 | if (NumArgs != NumNamedArgs) { |
| 163 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 164 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 165 | << 2 /*method*/ << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 166 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 167 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 171 | return anyIncompatibleArgs; |
| 172 | } |
| 173 | |
| 174 | // ActOnClassMessage - used for both unary and keyword messages. |
| 175 | // ArgExprs is optional - if it is present, the number of expressions |
| 176 | // is obtained from Sel.getNumArgs(). |
| 177 | Sema::ExprResult Sema::ActOnClassMessage( |
| 178 | Scope *S, |
| 179 | IdentifierInfo *receiverName, Selector Sel, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 180 | SourceLocation lbrac, SourceLocation receiverLoc, SourceLocation rbrac, |
| 181 | ExprTy **Args, unsigned NumArgs) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 182 | { |
| 183 | assert(receiverName && "missing receiver class name"); |
| 184 | |
| 185 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 186 | ObjCInterfaceDecl* ClassDecl = 0; |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 187 | bool isSuper = false; |
| 188 | |
Chris Lattner | 8469265 | 2008-11-20 05:35:30 +0000 | [diff] [blame] | 189 | if (receiverName->isStr("super")) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 190 | if (getCurMethodDecl()) { |
| 191 | isSuper = true; |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 192 | ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface(); |
| 193 | if (!OID) |
| 194 | return Diag(lbrac, diag::error_no_super_class_message) |
| 195 | << getCurMethodDecl()->getDeclName(); |
| 196 | ClassDecl = OID->getSuperClass(); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 197 | if (!ClassDecl) |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 198 | return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 199 | if (getCurMethodDecl()->isInstanceMethod()) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 200 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
| 201 | superTy = Context.getPointerType(superTy); |
| 202 | ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy); |
| 203 | // We are really in an instance method, redirect. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 204 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 205 | Args, NumArgs); |
| 206 | } |
| 207 | // We are sending a message to 'super' within a class method. Do nothing, |
| 208 | // the receiver will pass through as 'super' (how convenient:-). |
| 209 | } else { |
| 210 | // 'super' has been used outside a method context. If a variable named |
| 211 | // 'super' has been declared, redirect. If not, produce a diagnostic. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 212 | Decl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 213 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl); |
| 214 | if (VD) { |
| 215 | ExprResult ReceiverExpr = new DeclRefExpr(VD, VD->getType(), |
| 216 | receiverLoc); |
| 217 | // We are really in an instance method, redirect. |
Douglas Gregor | 5ac8aff | 2009-01-26 22:44:13 +0000 | [diff] [blame] | 218 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, rbrac, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 219 | Args, NumArgs); |
| 220 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 221 | return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName; |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 222 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 223 | } else |
| 224 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 225 | |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 226 | // The following code allows for the following GCC-ism: |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 227 | // |
| 228 | // typedef XCElementDisplayRect XCElementGraphicsRect; |
| 229 | // |
| 230 | // @implementation XCRASlice |
| 231 | // - whatever { // Note that XCElementGraphicsRect is a typedef name. |
| 232 | // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init]; |
| 233 | // } |
| 234 | // |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 235 | // If necessary, the following lookup could move to getObjCInterfaceDecl(). |
| 236 | if (!ClassDecl) { |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 237 | Decl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 238 | if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) { |
| 239 | const ObjCInterfaceType *OCIT; |
| 240 | OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType(); |
Fariborz Jahanian | ebff1fe | 2009-01-16 20:35:09 +0000 | [diff] [blame] | 241 | if (!OCIT) |
| 242 | return Diag(receiverLoc, diag::err_invalid_receiver_to_message); |
| 243 | ClassDecl = OCIT->getDecl(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | assert(ClassDecl && "missing interface declaration"); |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 247 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 248 | QualType returnType; |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 249 | Method = ClassDecl->lookupClassMethod(Sel); |
| 250 | |
| 251 | // If we have an implementation in scope, check "private" methods. |
| 252 | if (!Method) { |
| 253 | if (ObjCImplementationDecl *ImpDecl = |
| 254 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 255 | Method = ImpDecl->getClassMethod(Sel); |
Steve Naroff | e84a864 | 2008-09-28 14:55:53 +0000 | [diff] [blame] | 256 | |
| 257 | // Look through local category implementations associated with the class. |
| 258 | if (!Method) { |
| 259 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) { |
| 260 | if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl) |
| 261 | Method = ObjCCategoryImpls[i]->getClassMethod(Sel); |
| 262 | } |
| 263 | } |
Steve Naroff | 9ad23d6 | 2008-06-04 14:43:54 +0000 | [diff] [blame] | 264 | } |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 265 | // Before we give up, check if the selector is an instance method. |
| 266 | if (!Method) |
| 267 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 268 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 269 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 270 | lbrac, rbrac, returnType)) |
| 271 | return true; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 272 | |
| 273 | // If we have the ObjCInterfaceDecl* for the class that is receiving |
| 274 | // the message, use that to construct the ObjCMessageExpr. Otherwise |
| 275 | // pass on the IdentifierInfo* for the class. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 276 | // FIXME: need to do a better job handling 'super' usage within a class |
| 277 | // For now, we simply pass the "super" identifier through (which isn't |
| 278 | // consistent with instance methods. |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 279 | if (isSuper) |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 280 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 281 | lbrac, rbrac, ArgExprs, NumArgs); |
| 282 | else |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 283 | return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method, |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 284 | lbrac, rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 288 | // ArgExprs is optional - if it is present, the number of expressions |
| 289 | // is obtained from Sel.getNumArgs(). |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 290 | Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 291 | SourceLocation lbrac, |
| 292 | SourceLocation rbrac, |
| 293 | ExprTy **Args, unsigned NumArgs) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 294 | assert(receiver && "missing receiver expression"); |
| 295 | |
| 296 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 297 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 298 | QualType returnType; |
Steve Naroff | 94a82c9 | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 299 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 300 | QualType ReceiverCType = |
| 301 | Context.getCanonicalType(RExpr->getType()).getUnqualifiedType(); |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 302 | |
| 303 | // Handle messages to 'super'. |
| 304 | if (isa<ObjCSuperExpr>(RExpr)) { |
| 305 | ObjCMethodDecl *Method = 0; |
| 306 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 307 | // If we have an interface in scope, check 'super' methods. |
| 308 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 309 | if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) |
| 310 | Method = SuperDecl->lookupInstanceMethod(Sel); |
| 311 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 312 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 313 | lbrac, rbrac, returnType)) |
| 314 | return true; |
| 315 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 316 | ArgExprs, NumArgs); |
| 317 | } |
| 318 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 319 | // Handle messages to id. |
Steve Naroff | 6c4088e | 2008-09-29 16:51:41 +0000 | [diff] [blame] | 320 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) || |
| 321 | ReceiverCType->getAsBlockPointerType()) { |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 322 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool( |
| 323 | Sel, SourceRange(lbrac,rbrac)); |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 324 | if (!Method) |
| 325 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 326 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 327 | lbrac, rbrac, returnType)) |
| 328 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 329 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 330 | ArgExprs, NumArgs); |
| 331 | } |
| 332 | |
| 333 | // Handle messages to Class. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 334 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 335 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 336 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 337 | // If we have an implementation in scope, check "private" methods. |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 338 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 339 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 340 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 341 | Method = ImpDecl->getClassMethod(Sel); |
| 342 | } |
| 343 | if (!Method) |
| 344 | Method = FactoryMethodPool[Sel].Method; |
| 345 | if (!Method) |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 346 | Method = LookupInstanceMethodInGlobalPool( |
| 347 | Sel, SourceRange(lbrac,rbrac)); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 348 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 349 | lbrac, rbrac, returnType)) |
| 350 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 351 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 352 | ArgExprs, NumArgs); |
| 353 | } |
| 354 | |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 355 | ObjCMethodDecl *Method = 0; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 356 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 357 | |
| 358 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 359 | // long as one of the protocols implements the selector (if not, warn). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 360 | if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 361 | // Search protocols |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 362 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
| 363 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
| 364 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 365 | break; |
| 366 | } |
| 367 | if (!Method) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 368 | Diag(lbrac, diag::warn_method_not_found_in_protocol) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 369 | << Sel << RExpr->getSourceRange(); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 370 | } else if (const ObjCInterfaceType *OCIReceiver = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 371 | ReceiverCType->getAsPointerToObjCInterfaceType()) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 372 | // 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] | 373 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 374 | ClassDecl = OCIReceiver->getDecl(); |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 375 | // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be |
| 376 | // faster than the following method (which can do *many* linear searches). |
| 377 | // The idea is to add class info to InstanceMethodPool. |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 378 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 379 | |
| 380 | if (!Method) { |
| 381 | // Search protocol qualifiers. |
| 382 | for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(), |
| 383 | E = OCIReceiver->qual_end(); QI != E; ++QI) { |
| 384 | if ((Method = (*QI)->lookupInstanceMethod(Sel))) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 385 | break; |
| 386 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | c188d30 | 2008-07-21 05:27:51 +0000 | [diff] [blame] | 388 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 389 | if (!Method && !OCIReceiver->qual_empty()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 390 | Diag(lbrac, diag::warn_method_not_found_in_protocol) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 391 | << Sel << SourceRange(lbrac, rbrac); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 392 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 393 | Diag(lbrac, diag::error_bad_receiver_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 394 | << RExpr->getType() << RExpr->getSourceRange(); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 395 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (!Method) { |
| 399 | // If we have an implementation in scope, check "private" methods. |
| 400 | if (ClassDecl) |
| 401 | if (ObjCImplementationDecl *ImpDecl = |
| 402 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 403 | Method = ImpDecl->getInstanceMethod(Sel); |
| 404 | // If we still haven't found a method, look in the global pool. This |
| 405 | // behavior isn't very desirable, however we need it for GCC |
| 406 | // compatibility. |
| 407 | if (!Method) |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 408 | Method = LookupInstanceMethodInGlobalPool( |
| 409 | Sel, SourceRange(lbrac,rbrac)); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 411 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 412 | lbrac, rbrac, returnType)) |
| 413 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 414 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 415 | ArgExprs, NumArgs); |
| 416 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 417 | |
| 418 | //===----------------------------------------------------------------------===// |
| 419 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 420 | //===----------------------------------------------------------------------===// |
| 421 | |
| 422 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 423 | /// inheritance hierarchy of 'rProto'. |
| 424 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 425 | ObjCProtocolDecl *rProto) { |
| 426 | if (lProto == rProto) |
| 427 | return true; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 428 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 429 | E = rProto->protocol_end(); PI != E; ++PI) |
| 430 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 431 | return true; |
| 432 | return false; |
| 433 | } |
| 434 | |
| 435 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 436 | /// has been implemented in IDecl class, its super class or categories (if |
| 437 | /// lookupCategory is true). |
| 438 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 439 | ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 440 | bool lookupCategory, |
| 441 | bool RHSIsQualifiedID = false) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 442 | |
| 443 | // 1st, look up the class. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 444 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 445 | IDecl->getReferencedProtocols(); |
| 446 | |
| 447 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 448 | E = Protocols.end(); PI != E; ++PI) { |
| 449 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 450 | return true; |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 451 | // This is dubious and is added to be compatible with gcc. |
| 452 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 453 | // type to a LHS object when protocol in qualified LHS is in list |
| 454 | // of protocols in the rhs 'id' object. This IMO, should be a bug. |
Ted Kremenek | fd5b2ce | 2008-06-04 20:48:08 +0000 | [diff] [blame] | 455 | // FIXME: Treat this as an extension, and flag this as an error when |
| 456 | // GCC extensions are not enabled. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 457 | if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto)) |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 458 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // 2nd, look up the category. |
| 462 | if (lookupCategory) |
| 463 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 464 | CDecl = CDecl->getNextClassCategory()) { |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 465 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 466 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 467 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 468 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // 3rd, look up the super class(s) |
| 472 | if (IDecl->getSuperClass()) |
| 473 | return |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 474 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 475 | RHSIsQualifiedID); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 476 | |
| 477 | return false; |
| 478 | } |
| 479 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 480 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 481 | /// ObjCQualifiedIDType. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 482 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 483 | bool compare) { |
| 484 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 485 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 486 | QualType PointeeTy = PT->getPointeeType(); |
| 487 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 488 | return true; |
| 489 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 490 | QualType PointeeTy = PT->getPointeeType(); |
| 491 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 492 | return true; |
| 493 | } |
| 494 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 495 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 496 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 497 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 498 | QualType rtype; |
| 499 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 500 | if (!rhsQID) { |
| 501 | // Not comparing two ObjCQualifiedIdType's? |
| 502 | if (!rhs->isPointerType()) return false; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 503 | |
| 504 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 505 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 506 | if (rhsQI == 0) { |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 507 | // If the RHS is a unqualified interface pointer "NSString*", |
| 508 | // make sure we check the class hierarchy. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 509 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 510 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 511 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 512 | // when comparing an id<P> on lhs with a static type on rhs, |
| 513 | // see if static class implements all of id's protocols, directly or |
| 514 | // through its super class and categories. |
| 515 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 516 | return false; |
| 517 | } |
| 518 | return true; |
| 519 | } |
| 520 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 522 | |
| 523 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 524 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 525 | RHSProtoI = rhsQI->qual_begin(); |
| 526 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 527 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 528 | RHSProtoI = rhsQID->qual_begin(); |
| 529 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 530 | } else { |
| 531 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 535 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 536 | bool match = false; |
| 537 | |
| 538 | // when comparing an id<P> on lhs with a static type on rhs, |
| 539 | // see if static class implements all of id's protocols, directly or |
| 540 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 541 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 542 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 543 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 544 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 545 | match = true; |
| 546 | break; |
| 547 | } |
| 548 | } |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 549 | if (rhsQI) { |
| 550 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 551 | // make sure we check the class hierarchy. |
| 552 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 553 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 554 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 555 | // when comparing an id<P> on lhs with a static type on rhs, |
| 556 | // see if static class implements all of id's protocols, directly or |
| 557 | // through its super class and categories. |
| 558 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 559 | match = true; |
| 560 | break; |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 565 | if (!match) |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | return true; |
| 570 | } |
| 571 | |
| 572 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 573 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 574 | |
| 575 | if (!lhs->isPointerType()) |
| 576 | return false; |
| 577 | |
| 578 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 579 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 580 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 581 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 582 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 583 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 584 | bool match = false; |
| 585 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 586 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 587 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 588 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 589 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 590 | match = true; |
| 591 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 594 | if (!match) |
| 595 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 597 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 598 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 599 | |
| 600 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 601 | // for static type vs. qualified 'id' type, check that class implements |
| 602 | // all of 'id's protocols. |
| 603 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 604 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 605 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 606 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | return true; |
| 610 | } |
| 611 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 612 | } |
| 613 | |