Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 51 | if (Context.getObjCConstantStringInterface().isNull()) { |
Chris Lattner | 85a932e | 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 | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 55 | Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 56 | ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 57 | if (strIFace) |
| 58 | Context.setObjCConstantStringInterface(strIFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 60 | QualType t = Context.getObjCConstantStringInterface(); |
Chris Lattner | 13fd7e5 | 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 | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 87 | QualType t = Context.getObjCSelType(); |
Chris Lattner | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId]; |
Chris Lattner | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 102 | QualType t = Context.getObjCProtoType(); |
Chris Lattner | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 110 | ObjCMethodDecl *Method) { |
Chris Lattner | 85a932e | 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 | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 121 | if (lhsType->isArrayType()) |
| 122 | lhsType = Context.getArrayDecayedType(lhsType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 123 | else if (lhsType->isFunctionType()) |
| 124 | lhsType = Context.getPointerType(lhsType); |
| 125 | |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 126 | AssignConvertType Result = |
| 127 | CheckSingleAssignmentConstraints(lhsType, argExpr); |
Chris Lattner | 85a932e | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 149 | ObjCInterfaceDecl* ClassDecl = 0; |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 150 | if (!strcmp(receiverName->getName(), "super") && getCurMethodDecl()) { |
| 151 | ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass(); |
Steve Naroff | 9511096 | 2008-03-28 21:37:05 +0000 | [diff] [blame] | 152 | if (!ClassDecl) |
| 153 | return Diag(lbrac, diag::error_no_super_class, |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 154 | getCurMethodDecl()->getClassInterface()->getName()); |
| 155 | if (getCurMethodDecl()->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 156 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 157 | superTy = Context.getPointerType(superTy); |
Chris Lattner | 0d17f6f | 2008-06-21 18:04:54 +0000 | [diff] [blame] | 158 | ExprResult ReceiverExpr = new PreDefinedExpr(SourceLocation(), superTy, |
| 159 | PreDefinedExpr::ObjCSuper); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 160 | // We are really in an instance method, redirect. |
| 161 | return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac, |
| 162 | Args, NumArgs); |
| 163 | } |
| 164 | // We are sending a message to 'super' within a class method. Do nothing, |
| 165 | // the receiver will pass through as 'super' (how convenient:-). |
| 166 | } else |
| 167 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 168 | |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 169 | // ClassDecl is null in the following case. |
| 170 | // |
| 171 | // typedef XCElementDisplayRect XCElementGraphicsRect; |
| 172 | // |
| 173 | // @implementation XCRASlice |
| 174 | // - whatever { // Note that XCElementGraphicsRect is a typedef name. |
| 175 | // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init]; |
| 176 | // } |
| 177 | // |
| 178 | // FIXME: Investigate why GCC allows the above. |
| 179 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 180 | QualType returnType; |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 181 | if (ClassDecl) { |
| 182 | Method = ClassDecl->lookupClassMethod(Sel); |
| 183 | |
| 184 | // If we have an implementation in scope, check "private" methods. |
| 185 | if (!Method) { |
| 186 | if (ObjCImplementationDecl *ImpDecl = |
| 187 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 188 | Method = ImpDecl->getClassMethod(Sel); |
| 189 | } |
| 190 | // Before we give up, check if the selector is an instance method. |
| 191 | if (!Method) |
| 192 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Steve Naroff | 9ad23d6 | 2008-06-04 14:43:54 +0000 | [diff] [blame] | 193 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 194 | if (!Method) { |
| 195 | Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(), |
| 196 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 197 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 198 | } else { |
| 199 | returnType = Method->getResultType(); |
| 200 | if (Sel.getNumArgs()) { |
| 201 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 202 | return true; |
| 203 | } |
| 204 | } |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 205 | |
| 206 | // If we have the ObjCInterfaceDecl* for the class that is receiving |
| 207 | // the message, use that to construct the ObjCMessageExpr. Otherwise |
| 208 | // pass on the IdentifierInfo* for the class. |
| 209 | if (ClassDecl) |
| 210 | return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method, |
| 211 | lbrac, rbrac, ArgExprs, NumArgs); |
| 212 | else |
| 213 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 214 | lbrac, rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 218 | // ArgExprs is optional - if it is present, the number of expressions |
| 219 | // is obtained from Sel.getNumArgs(). |
| 220 | Sema::ExprResult Sema::ActOnInstanceMessage( |
| 221 | ExprTy *receiver, Selector Sel, |
| 222 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 223 | { |
| 224 | assert(receiver && "missing receiver expression"); |
| 225 | |
| 226 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 227 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 228 | QualType returnType; |
Steve Naroff | 94a82c9 | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 229 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 230 | QualType receiverType = |
| 231 | RExpr->getType().getCanonicalType().getUnqualifiedType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 232 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 233 | // Handle messages to id. |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 234 | if (receiverType == Context.getObjCIdType().getCanonicalType()) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 235 | ObjCMethodDecl *Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 236 | if (!Method) |
| 237 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 238 | if (!Method) { |
| 239 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 240 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 241 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 242 | } else { |
| 243 | returnType = Method->getResultType(); |
| 244 | if (Sel.getNumArgs()) |
| 245 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 246 | return true; |
| 247 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 248 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 249 | ArgExprs, NumArgs); |
| 250 | } |
| 251 | |
| 252 | // Handle messages to Class. |
| 253 | if (receiverType == Context.getObjCClassType().getCanonicalType()) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 254 | ObjCMethodDecl *Method = 0; |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 255 | if (getCurMethodDecl()) { |
| 256 | ObjCInterfaceDecl* ClassDecl = getCurMethodDecl()->getClassInterface(); |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 257 | // If we have an implementation in scope, check "private" methods. |
| 258 | if (ClassDecl) |
| 259 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 260 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 261 | Method = ImpDecl->getClassMethod(Sel); |
| 262 | } |
| 263 | if (!Method) |
| 264 | Method = FactoryMethodPool[Sel].Method; |
| 265 | if (!Method) |
| 266 | Method = InstanceMethodPool[Sel].Method; |
| 267 | if (!Method) { |
| 268 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 269 | SourceRange(lbrac, rbrac)); |
| 270 | returnType = Context.getObjCIdType(); |
| 271 | } else { |
| 272 | returnType = Method->getResultType(); |
| 273 | if (Sel.getNumArgs()) |
| 274 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 275 | return true; |
| 276 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 277 | |
| 278 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 279 | ArgExprs, NumArgs); |
| 280 | } |
| 281 | |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 282 | ObjCMethodDecl *Method = 0; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 283 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 284 | |
| 285 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 286 | // long as one of the protocols implements the selector (if not, warn). |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 287 | if (ObjCQualifiedIdType *QIT = |
| 288 | dyn_cast<ObjCQualifiedIdType>(receiverType)) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 289 | // Search protocols |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 290 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
| 291 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
| 292 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 293 | break; |
| 294 | } |
| 295 | if (!Method) |
| 296 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 297 | std::string("-"), Sel.getName(), |
| 298 | SourceRange(lbrac, rbrac)); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 299 | } else if (const ObjCInterfaceType *OCIReceiver = |
| 300 | receiverType->getAsPointerToObjCInterfaceType()) { |
| 301 | // 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] | 302 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 303 | ClassDecl = OCIReceiver->getDecl(); |
| 304 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 305 | // than the following method (which can do *many* linear searches). The |
| 306 | // idea is to add class info to InstanceMethodPool. |
| 307 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 308 | |
| 309 | if (!Method) { |
| 310 | // Search protocol qualifiers. |
| 311 | for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(), |
| 312 | E = OCIReceiver->qual_end(); QI != E; ++QI) { |
| 313 | if ((Method = (*QI)->lookupInstanceMethod(Sel))) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | c188d30 | 2008-07-21 05:27:51 +0000 | [diff] [blame] | 317 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 318 | if (!Method && !OCIReceiver->qual_empty()) |
| 319 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 320 | std::string("-"), Sel.getName(), |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 321 | SourceRange(lbrac, rbrac)); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame^] | 322 | } else { |
| 323 | Diag(lbrac, diag::error_bad_receiver_type, |
| 324 | RExpr->getType().getAsString()); |
| 325 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (!Method) { |
| 329 | // If we have an implementation in scope, check "private" methods. |
| 330 | if (ClassDecl) |
| 331 | if (ObjCImplementationDecl *ImpDecl = |
| 332 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 333 | Method = ImpDecl->getInstanceMethod(Sel); |
| 334 | // If we still haven't found a method, look in the global pool. This |
| 335 | // behavior isn't very desirable, however we need it for GCC |
| 336 | // compatibility. |
| 337 | if (!Method) |
| 338 | Method = InstanceMethodPool[Sel].Method; |
| 339 | } |
| 340 | if (!Method) { |
| 341 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 342 | SourceRange(lbrac, rbrac)); |
| 343 | returnType = Context.getObjCIdType(); |
| 344 | } else { |
| 345 | returnType = Method->getResultType(); |
| 346 | if (Sel.getNumArgs()) |
| 347 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 348 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 349 | } |
| 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; |
| 364 | ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols(); |
| 365 | for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++) |
| 366 | if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i])) |
| 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. |
| 380 | ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols(); |
| 381 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) { |
| 382 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 383 | return true; |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 384 | // This is dubious and is added to be compatible with gcc. |
| 385 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 386 | // type to a LHS object when protocol in qualified LHS is in list |
| 387 | // 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] | 388 | // FIXME: Treat this as an extension, and flag this as an error when |
| 389 | // GCC extensions are not enabled. |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 390 | else if (RHSIsQualifiedID && |
| 391 | ProtocolCompatibleWithProtocol(protoList[i], lProto)) |
| 392 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // 2nd, look up the category. |
| 396 | if (lookupCategory) |
| 397 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 398 | CDecl = CDecl->getNextClassCategory()) { |
| 399 | protoList = CDecl->getReferencedProtocols(); |
| 400 | for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) { |
| 401 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 402 | return true; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // 3rd, look up the super class(s) |
| 407 | if (IDecl->getSuperClass()) |
| 408 | return |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 409 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 410 | RHSIsQualifiedID); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 415 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 416 | /// ObjCQualifiedIDType. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 417 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 418 | bool compare) { |
| 419 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 420 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 421 | QualType PointeeTy = PT->getPointeeType(); |
| 422 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 423 | return true; |
| 424 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 425 | QualType PointeeTy = PT->getPointeeType(); |
| 426 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 427 | return true; |
| 428 | } |
| 429 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 430 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 431 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 432 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 433 | QualType rtype; |
| 434 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 435 | if (!rhsQID) { |
| 436 | // Not comparing two ObjCQualifiedIdType's? |
| 437 | if (!rhs->isPointerType()) return false; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 438 | |
| 439 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 440 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 441 | if (rhsQI == 0) { |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 442 | // If the RHS is a unqualified interface pointer "NSString*", |
| 443 | // make sure we check the class hierarchy. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 444 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 445 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 446 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 447 | // when comparing an id<P> on lhs with a static type on rhs, |
| 448 | // see if static class implements all of id's protocols, directly or |
| 449 | // through its super class and categories. |
| 450 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 451 | return false; |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 457 | |
| 458 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 459 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 460 | RHSProtoI = rhsQI->qual_begin(); |
| 461 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 462 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 463 | RHSProtoI = rhsQID->qual_begin(); |
| 464 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 465 | } else { |
| 466 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 470 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 471 | bool match = false; |
| 472 | |
| 473 | // when comparing an id<P> on lhs with a static type on rhs, |
| 474 | // see if static class implements all of id's protocols, directly or |
| 475 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 476 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 477 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 478 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 479 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 480 | match = true; |
| 481 | break; |
| 482 | } |
| 483 | } |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 484 | if (rhsQI) { |
| 485 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 486 | // make sure we check the class hierarchy. |
| 487 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 488 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 489 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 490 | // when comparing an id<P> on lhs with a static type on rhs, |
| 491 | // see if static class implements all of id's protocols, directly or |
| 492 | // through its super class and categories. |
| 493 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 494 | match = true; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 500 | if (!match) |
| 501 | return false; |
| 502 | } |
| 503 | |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 508 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 509 | |
| 510 | if (!lhs->isPointerType()) |
| 511 | return false; |
| 512 | |
| 513 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 514 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 515 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 516 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 517 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 518 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 519 | bool match = false; |
| 520 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 521 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 522 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 523 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 524 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 525 | match = true; |
| 526 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 529 | if (!match) |
| 530 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 531 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 532 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 533 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 534 | |
| 535 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 536 | // for static type vs. qualified 'id' type, check that class implements |
| 537 | // all of 'id's protocols. |
| 538 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 539 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 540 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 541 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 547 | } |
| 548 | |