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