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