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