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