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 | 51c6cff | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 172 | // ClassDecl is null in the following case. |
| 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 | // |
| 181 | // FIXME: Investigate why GCC allows the above. |
| 182 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 183 | QualType returnType; |
Steve Naroff | 51c6cff | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 184 | if (ClassDecl) { |
| 185 | Method = ClassDecl->lookupClassMethod(Sel); |
| 186 | |
| 187 | // If we have an implementation in scope, check "private" methods. |
| 188 | if (!Method) { |
| 189 | if (ObjCImplementationDecl *ImpDecl = |
| 190 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 191 | Method = ImpDecl->getClassMethod(Sel); |
| 192 | } |
| 193 | // Before we give up, check if the selector is an instance method. |
| 194 | if (!Method) |
| 195 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Steve Naroff | 616c55c | 2008-06-04 14:43:54 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 197 | if (!Method) { |
| 198 | Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(), |
| 199 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 200 | returnType = Context.getObjCIdType(); |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 201 | } else { |
| 202 | returnType = Method->getResultType(); |
| 203 | if (Sel.getNumArgs()) { |
| 204 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 205 | return true; |
| 206 | } |
| 207 | } |
Ted Kremenek | ee2c9fd | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 208 | |
| 209 | // If we have the ObjCInterfaceDecl* for the class that is receiving |
| 210 | // the message, use that to construct the ObjCMessageExpr. Otherwise |
| 211 | // pass on the IdentifierInfo* for the class. |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 212 | // FIXME: need to do a better job handling 'super' usage within a class |
| 213 | // For now, we simply pass the "super" identifier through (which isn't |
| 214 | // consistent with instance methods. |
| 215 | if (isSuper || !ClassDecl) |
| 216 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
Ted Kremenek | ee2c9fd | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 217 | lbrac, rbrac, ArgExprs, NumArgs); |
| 218 | else |
Steve Naroff | 91a6920 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 219 | return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method, |
Ted Kremenek | ee2c9fd | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 220 | lbrac, rbrac, ArgExprs, NumArgs); |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 224 | // ArgExprs is optional - if it is present, the number of expressions |
| 225 | // is obtained from Sel.getNumArgs(). |
Chris Lattner | 4d01854 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 226 | Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 227 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 228 | { |
| 229 | assert(receiver && "missing receiver expression"); |
| 230 | |
| 231 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 232 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 233 | QualType returnType; |
Steve Naroff | 7c7817e | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 235 | QualType receiverType = |
| 236 | RExpr->getType().getCanonicalType().getUnqualifiedType(); |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 238 | // Handle messages to id. |
Steve Naroff | 1a5027c | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 239 | if (receiverType == Context.getObjCIdType().getCanonicalType()) { |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 240 | ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 05fd1cc | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 241 | if (!Method) |
| 242 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 243 | if (!Method) { |
| 244 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 245 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 246 | returnType = Context.getObjCIdType(); |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 247 | } else { |
| 248 | returnType = Method->getResultType(); |
| 249 | if (Sel.getNumArgs()) |
| 250 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 251 | return true; |
| 252 | } |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 253 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 254 | ArgExprs, NumArgs); |
| 255 | } |
| 256 | |
| 257 | // Handle messages to Class. |
| 258 | if (receiverType == Context.getObjCClassType().getCanonicalType()) { |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 259 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 55a2433 | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 260 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
Steve Naroff | 1a5027c | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 261 | // If we have an implementation in scope, check "private" methods. |
Chris Lattner | 55a2433 | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 262 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Steve Naroff | 1a5027c | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 263 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 264 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Steve Naroff | 1a5027c | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 265 | Method = ImpDecl->getClassMethod(Sel); |
| 266 | } |
| 267 | if (!Method) |
| 268 | Method = FactoryMethodPool[Sel].Method; |
| 269 | if (!Method) |
| 270 | Method = InstanceMethodPool[Sel].Method; |
| 271 | if (!Method) { |
| 272 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
Chris Lattner | 3977eb7 | 2008-07-21 06:16:07 +0000 | [diff] [blame] | 273 | RExpr->getSourceRange()); |
Steve Naroff | 1a5027c | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 274 | returnType = Context.getObjCIdType(); |
| 275 | } else { |
| 276 | returnType = Method->getResultType(); |
| 277 | if (Sel.getNumArgs()) |
| 278 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 279 | return true; |
| 280 | } |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 281 | |
| 282 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 283 | ArgExprs, NumArgs); |
| 284 | } |
| 285 | |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 286 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 287 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 288 | |
| 289 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 290 | // long as one of the protocols implements the selector (if not, warn). |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 291 | if (ObjCQualifiedIdType *QIT = |
| 292 | dyn_cast<ObjCQualifiedIdType>(receiverType)) { |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 293 | // Search protocols |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 294 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
| 295 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
| 296 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 297 | break; |
| 298 | } |
| 299 | if (!Method) |
| 300 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 301 | std::string("-"), Sel.getName(), |
Chris Lattner | 3977eb7 | 2008-07-21 06:16:07 +0000 | [diff] [blame] | 302 | RExpr->getSourceRange()); |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 303 | } else if (const ObjCInterfaceType *OCIReceiver = |
| 304 | receiverType->getAsPointerToObjCInterfaceType()) { |
| 305 | // 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] | 306 | |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 307 | ClassDecl = OCIReceiver->getDecl(); |
| 308 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 309 | // than the following method (which can do *many* linear searches). The |
| 310 | // idea is to add class info to InstanceMethodPool. |
| 311 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 312 | |
| 313 | if (!Method) { |
| 314 | // Search protocol qualifiers. |
| 315 | for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(), |
| 316 | E = OCIReceiver->qual_end(); QI != E; ++QI) { |
| 317 | if ((Method = (*QI)->lookupInstanceMethod(Sel))) |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 318 | break; |
| 319 | } |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 320 | } |
Chris Lattner | 00cb97b | 2008-07-21 05:27:51 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 322 | if (!Method && !OCIReceiver->qual_empty()) |
| 323 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 324 | std::string("-"), Sel.getName(), |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 325 | SourceRange(lbrac, rbrac)); |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 326 | } else { |
| 327 | Diag(lbrac, diag::error_bad_receiver_type, |
Chris Lattner | 3977eb7 | 2008-07-21 06:16:07 +0000 | [diff] [blame] | 328 | RExpr->getType().getAsString(), RExpr->getSourceRange()); |
Chris Lattner | f914765 | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 329 | return true; |
Chris Lattner | 4cff4b7 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (!Method) { |
| 333 | // If we have an implementation in scope, check "private" methods. |
| 334 | if (ClassDecl) |
| 335 | if (ObjCImplementationDecl *ImpDecl = |
| 336 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 337 | Method = ImpDecl->getInstanceMethod(Sel); |
| 338 | // If we still haven't found a method, look in the global pool. This |
| 339 | // behavior isn't very desirable, however we need it for GCC |
| 340 | // compatibility. |
| 341 | if (!Method) |
| 342 | Method = InstanceMethodPool[Sel].Method; |
| 343 | } |
| 344 | if (!Method) { |
| 345 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 346 | SourceRange(lbrac, rbrac)); |
| 347 | returnType = Context.getObjCIdType(); |
| 348 | } else { |
| 349 | returnType = Method->getResultType(); |
| 350 | if (Sel.getNumArgs()) |
| 351 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 352 | return true; |
Chris Lattner | 0949305 | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 353 | } |
| 354 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 355 | ArgExprs, NumArgs); |
| 356 | } |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 357 | |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 360 | //===----------------------------------------------------------------------===// |
| 361 | |
| 362 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 363 | /// inheritance hierarchy of 'rProto'. |
| 364 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 365 | ObjCProtocolDecl *rProto) { |
| 366 | if (lProto == rProto) |
| 367 | return true; |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 368 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 369 | E = rProto->protocol_end(); PI != E; ++PI) |
| 370 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 371 | return true; |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 376 | /// has been implemented in IDecl class, its super class or categories (if |
| 377 | /// lookupCategory is true). |
| 378 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 379 | ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | e266564 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 380 | bool lookupCategory, |
| 381 | bool RHSIsQualifiedID = false) { |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 382 | |
| 383 | // 1st, look up the class. |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 384 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 385 | IDecl->getReferencedProtocols(); |
| 386 | |
| 387 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 388 | E = Protocols.end(); PI != E; ++PI) { |
| 389 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 390 | return true; |
Fariborz Jahanian | e266564 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 391 | // This is dubious and is added to be compatible with gcc. |
| 392 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 393 | // type to a LHS object when protocol in qualified LHS is in list |
| 394 | // 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] | 395 | // FIXME: Treat this as an extension, and flag this as an error when |
| 396 | // GCC extensions are not enabled. |
Chris Lattner | 8bcb525 | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 397 | if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto)) |
Fariborz Jahanian | e266564 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 398 | return true; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // 2nd, look up the category. |
| 402 | if (lookupCategory) |
| 403 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 404 | CDecl = CDecl->getNextClassCategory()) { |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 405 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 406 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 407 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 408 | return true; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // 3rd, look up the super class(s) |
| 412 | if (IDecl->getSuperClass()) |
| 413 | return |
Fariborz Jahanian | e266564 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 414 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 415 | RHSIsQualifiedID); |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 416 | |
| 417 | return false; |
| 418 | } |
| 419 | |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 420 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 421 | /// ObjCQualifiedIDType. |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 422 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 423 | bool compare) { |
| 424 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 425 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 426 | QualType PointeeTy = PT->getPointeeType(); |
| 427 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 428 | return true; |
| 429 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 430 | QualType PointeeTy = PT->getPointeeType(); |
| 431 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 432 | return true; |
| 433 | } |
| 434 | |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 435 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 436 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 437 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 438 | QualType rtype; |
| 439 | |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 440 | if (!rhsQID) { |
| 441 | // Not comparing two ObjCQualifiedIdType's? |
| 442 | if (!rhs->isPointerType()) return false; |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 443 | |
| 444 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 445 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 446 | if (rhsQI == 0) { |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 447 | // If the RHS is a unqualified interface pointer "NSString*", |
| 448 | // make sure we check the class hierarchy. |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 449 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 450 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 451 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 452 | // when comparing an id<P> on lhs with a static type on rhs, |
| 453 | // see if static class implements all of id's protocols, directly or |
| 454 | // through its super class and categories. |
| 455 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 456 | return false; |
| 457 | } |
| 458 | return true; |
| 459 | } |
| 460 | } |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 461 | } |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 462 | |
| 463 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 464 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 465 | RHSProtoI = rhsQI->qual_begin(); |
| 466 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 467 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 468 | RHSProtoI = rhsQID->qual_begin(); |
| 469 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 470 | } else { |
| 471 | return false; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 475 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 476 | bool match = false; |
| 477 | |
| 478 | // when comparing an id<P> on lhs with a static type on rhs, |
| 479 | // see if static class implements all of id's protocols, directly or |
| 480 | // through its super class and categories. |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 481 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 482 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 483 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 484 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 485 | match = true; |
| 486 | break; |
| 487 | } |
| 488 | } |
Steve Naroff | d6ba7fd | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 489 | if (rhsQI) { |
| 490 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 491 | // make sure we check the class hierarchy. |
| 492 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 493 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 494 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 495 | // when comparing an id<P> on lhs with a static type on rhs, |
| 496 | // see if static class implements all of id's protocols, directly or |
| 497 | // through its super class and categories. |
| 498 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 499 | match = true; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 505 | if (!match) |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 513 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 514 | |
| 515 | if (!lhs->isPointerType()) |
| 516 | return false; |
| 517 | |
| 518 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 519 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 520 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 521 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 522 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 523 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 524 | bool match = false; |
| 525 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 526 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 527 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 528 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 529 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 530 | match = true; |
| 531 | break; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 534 | if (!match) |
| 535 | return false; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 536 | } |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 537 | return true; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 538 | } |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 539 | |
| 540 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 541 | // for static type vs. qualified 'id' type, check that class implements |
| 542 | // all of 'id's protocols. |
| 543 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 544 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 545 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | e266564 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 546 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b2a0e61 | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 547 | return false; |
| 548 | } |
| 549 | return true; |
| 550 | } |
| 551 | return false; |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 552 | } |
| 553 | |