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