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