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; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 150 | if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) { |
| 151 | ClassDecl = CurMethodDecl->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, |
| 154 | CurMethodDecl->getClassInterface()->getName()); |
| 155 | if (CurMethodDecl->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 | } |
| 205 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 206 | lbrac, rbrac, ArgExprs, NumArgs); |
| 207 | } |
| 208 | |
| 209 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 210 | // ArgExprs is optional - if it is present, the number of expressions |
| 211 | // is obtained from Sel.getNumArgs(). |
| 212 | Sema::ExprResult Sema::ActOnInstanceMessage( |
| 213 | ExprTy *receiver, Selector Sel, |
| 214 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 215 | { |
| 216 | assert(receiver && "missing receiver expression"); |
| 217 | |
| 218 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 219 | Expr *RExpr = static_cast<Expr *>(receiver); |
Steve Naroff | 94a82c9 | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 220 | QualType receiverType; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 221 | QualType returnType; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 222 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 94a82c9 | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 223 | |
| 224 | receiverType = RExpr->getType().getCanonicalType().getUnqualifiedType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 225 | |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 226 | if (receiverType == Context.getObjCIdType().getCanonicalType()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 227 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 228 | if (!Method) |
| 229 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 230 | if (!Method) { |
| 231 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 232 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 233 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 234 | } else { |
| 235 | returnType = Method->getResultType(); |
| 236 | if (Sel.getNumArgs()) |
| 237 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 238 | return true; |
| 239 | } |
Steve Naroff | e2af8b1 | 2008-06-05 14:49:39 +0000 | [diff] [blame] | 240 | } else if (receiverType == Context.getObjCClassType().getCanonicalType()) { |
| 241 | if (CurMethodDecl) { |
| 242 | ObjCInterfaceDecl* ClassDecl = CurMethodDecl->getClassInterface(); |
| 243 | // If we have an implementation in scope, check "private" methods. |
| 244 | if (ClassDecl) |
| 245 | if (ObjCImplementationDecl *ImpDecl = |
| 246 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 247 | Method = ImpDecl->getClassMethod(Sel); |
| 248 | } |
| 249 | if (!Method) |
| 250 | Method = FactoryMethodPool[Sel].Method; |
| 251 | if (!Method) |
| 252 | Method = InstanceMethodPool[Sel].Method; |
| 253 | if (!Method) { |
| 254 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 255 | SourceRange(lbrac, rbrac)); |
| 256 | returnType = Context.getObjCIdType(); |
| 257 | } else { |
| 258 | returnType = Method->getResultType(); |
| 259 | if (Sel.getNumArgs()) |
| 260 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 261 | return true; |
| 262 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 263 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 264 | bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 265 | // FIXME (snaroff): checking in this code from Patrick. Needs to be |
| 266 | // revisited. how do we get the ClassDecl from the receiver expression? |
| 267 | if (!receiverIsQualId) |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 268 | while (const PointerType *PTy = receiverType->getAsPointerType()) |
| 269 | receiverType = PTy->getPointeeType(); |
| 270 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 271 | ObjCInterfaceDecl* ClassDecl = 0; |
| 272 | if (ObjCQualifiedInterfaceType *QIT = |
| 273 | dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 274 | ClassDecl = QIT->getDecl(); |
| 275 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 276 | if (!Method) { |
| 277 | // search protocols |
| 278 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 279 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 280 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | if (!Method) |
| 285 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 286 | std::string("-"), Sel.getName(), |
| 287 | SourceRange(lbrac, rbrac)); |
| 288 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 289 | else if (ObjCQualifiedIdType *QIT = |
| 290 | dyn_cast<ObjCQualifiedIdType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 291 | // search protocols |
| 292 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 293 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 294 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 295 | break; |
| 296 | } |
| 297 | if (!Method) |
| 298 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 299 | std::string("-"), Sel.getName(), |
| 300 | SourceRange(lbrac, rbrac)); |
| 301 | } |
| 302 | else { |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 303 | ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType); |
| 304 | if (OCIReceiver == 0) { |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame^] | 305 | Diag(lbrac, diag::error_bad_receiver_type, |
| 306 | RExpr->getType().getAsString()); |
| 307 | return true; |
Fariborz Jahanian | 1dad5b2 | 2008-01-25 17:43:39 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 309 | ClassDecl = OCIReceiver->getDecl(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 310 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 311 | // than the following method (which can do *many* linear searches). The |
| 312 | // idea is to add class info to InstanceMethodPool... |
| 313 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 314 | } |
| 315 | if (!Method) { |
| 316 | // If we have an implementation in scope, check "private" methods. |
| 317 | if (ClassDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 318 | if (ObjCImplementationDecl *ImpDecl = |
| 319 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 320 | Method = ImpDecl->getInstanceMethod(Sel); |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 321 | // If we still haven't found a method, look in the global pool. This |
| 322 | // behavior isn't very desirable, however we need it for GCC |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 323 | // compatibility. |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 324 | if (!Method) |
| 325 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 326 | } |
| 327 | if (!Method) { |
| 328 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 329 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 330 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 331 | } else { |
| 332 | returnType = Method->getResultType(); |
| 333 | if (Sel.getNumArgs()) |
| 334 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 335 | return true; |
| 336 | } |
| 337 | } |
| 338 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 339 | ArgExprs, NumArgs); |
| 340 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 341 | |
| 342 | //===----------------------------------------------------------------------===// |
| 343 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
| 346 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 347 | /// inheritance hierarchy of 'rProto'. |
| 348 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 349 | ObjCProtocolDecl *rProto) { |
| 350 | if (lProto == rProto) |
| 351 | return true; |
| 352 | ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols(); |
| 353 | for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++) |
| 354 | if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i])) |
| 355 | return true; |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 360 | /// has been implemented in IDecl class, its super class or categories (if |
| 361 | /// lookupCategory is true). |
| 362 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 363 | ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 364 | bool lookupCategory, |
| 365 | bool RHSIsQualifiedID = false) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 366 | |
| 367 | // 1st, look up the class. |
| 368 | ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols(); |
| 369 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) { |
| 370 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 371 | return true; |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 372 | // This is dubious and is added to be compatible with gcc. |
| 373 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 374 | // type to a LHS object when protocol in qualified LHS is in list |
| 375 | // 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] | 376 | // FIXME: Treat this as an extension, and flag this as an error when |
| 377 | // GCC extensions are not enabled. |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 378 | else if (RHSIsQualifiedID && |
| 379 | ProtocolCompatibleWithProtocol(protoList[i], lProto)) |
| 380 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | // 2nd, look up the category. |
| 384 | if (lookupCategory) |
| 385 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 386 | CDecl = CDecl->getNextClassCategory()) { |
| 387 | protoList = CDecl->getReferencedProtocols(); |
| 388 | for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) { |
| 389 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 390 | return true; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // 3rd, look up the super class(s) |
| 395 | if (IDecl->getSuperClass()) |
| 396 | return |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 397 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 398 | RHSIsQualifiedID); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 399 | |
| 400 | return false; |
| 401 | } |
| 402 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 403 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 404 | /// ObjCQualifiedIDType. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 405 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 406 | bool compare) { |
| 407 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 408 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 409 | QualType PointeeTy = PT->getPointeeType(); |
| 410 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 411 | return true; |
| 412 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 413 | QualType PointeeTy = PT->getPointeeType(); |
| 414 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 415 | return true; |
| 416 | } |
| 417 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 418 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 419 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 420 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 421 | QualType rtype; |
| 422 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 423 | if (!rhsQID) { |
| 424 | // Not comparing two ObjCQualifiedIdType's? |
| 425 | if (!rhs->isPointerType()) return false; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 426 | |
| 427 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 428 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 429 | if (rhsQI == 0) { |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 430 | // If the RHS is a unqualified interface pointer "NSString*", |
| 431 | // make sure we check the class hierarchy. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 432 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 433 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 434 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 435 | // when comparing an id<P> on lhs with a static type on rhs, |
| 436 | // see if static class implements all of id's protocols, directly or |
| 437 | // through its super class and categories. |
| 438 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 439 | return false; |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 445 | |
| 446 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 447 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 448 | RHSProtoI = rhsQI->qual_begin(); |
| 449 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 450 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 451 | RHSProtoI = rhsQID->qual_begin(); |
| 452 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 453 | } else { |
| 454 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 458 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 459 | bool match = false; |
| 460 | |
| 461 | // when comparing an id<P> on lhs with a static type on rhs, |
| 462 | // see if static class implements all of id's protocols, directly or |
| 463 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 464 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 465 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 466 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 467 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 468 | match = true; |
| 469 | break; |
| 470 | } |
| 471 | } |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 472 | if (rhsQI) { |
| 473 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 474 | // make sure we check the class hierarchy. |
| 475 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 476 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 477 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 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. |
| 481 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 482 | match = true; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 488 | if (!match) |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 496 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 497 | |
| 498 | if (!lhs->isPointerType()) |
| 499 | return false; |
| 500 | |
| 501 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 502 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 503 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 504 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 505 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 506 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 507 | bool match = false; |
| 508 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 509 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 510 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 511 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 512 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 513 | match = true; |
| 514 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 517 | if (!match) |
| 518 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 519 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 520 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 522 | |
| 523 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 524 | // for static type vs. qualified 'id' type, check that class implements |
| 525 | // all of 'id's protocols. |
| 526 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 527 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 528 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 529 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 535 | } |
| 536 | |