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