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