blob: 6b40c89a8c887e8b4ebad02018456908422c2a4b [file] [log] [blame]
Chris Lattner85a932e2008-01-04 22:32:30 +00001//===--- 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 Narofff494b572008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Chris Lattner39c28bb2009-02-18 06:48:40 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000022 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000023 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000024 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
25
Chris Lattnerf4b136f2009-02-18 06:13:04 +000026 // Most ObjC strings are formed out of a single piece. However, we *can*
27 // have strings formed out of multiple @ strings with multiple pptokens in
28 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
29 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000030 StringLiteral *S = Strings[0];
Chris Lattnerf4b136f2009-02-18 06:13:04 +000031
32 // If we have a multi-part string, merge it all together.
33 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000034 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000035 llvm::SmallString<128> StrBuf;
36 llvm::SmallVector<SourceLocation, 8> StrLocs;
Chris Lattner726e1682009-02-18 05:49:11 +000037
Chris Lattner726e1682009-02-18 05:49:11 +000038 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000039 S = Strings[i];
40
41 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000042 if (S->isWide()) {
43 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
44 << S->getSourceRange();
45 return true;
46 }
47
Chris Lattner39c28bb2009-02-18 06:48:40 +000048 // Get the string data.
49 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
50
51 // Get the locations of the string tokens.
52 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
53
54 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000055 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000056 }
Chris Lattner39c28bb2009-02-18 06:48:40 +000057
58 // Create the aggregate string with the appropriate content and location
59 // information.
60 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000061 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000062 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000063 }
64
Chris Lattner69039812009-02-18 06:01:06 +000065 // Verify that this composite string is acceptable for ObjC strings.
66 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000067 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000068
69 // Initialize the constant string interface lazily. This assumes
70 // the NSConstantString interface is seen in this translation unit.
71 QualType Ty = Context.getObjCConstantStringInterface();
72 if (!Ty.isNull()) {
73 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000074 } else {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000075 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
76 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
77 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
78 Context.setObjCConstantStringInterface(StrIF);
79 Ty = Context.getObjCConstantStringInterface();
80 Ty = Context.getPointerType(Ty);
81 } else {
82 // If there is no NSConstantString interface defined then treat constant
83 // strings as untyped objects and let the runtime figure it out later.
84 Ty = Context.getObjCIdType();
85 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000086 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087
Chris Lattnerf4b136f2009-02-18 06:13:04 +000088 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000089}
90
91Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
92 SourceLocation EncodeLoc,
93 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000094 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000095 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000096 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +000097
Chris Lattnereaf2bb82009-02-24 22:18:39 +000098 std::string Str;
99 Context.getObjCEncodingForType(EncodedType, Str);
100
101 // The type of @encode is the same as the type of the corresponding string,
102 // which is an array type.
103 QualType StrTy = Context.CharTy;
104 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
105 if (getLangOptions().CPlusPlus)
106 StrTy.addConst();
107 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
108 ArrayType::Normal, 0);
109
110 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000111}
112
113Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
114 SourceLocation AtLoc,
115 SourceLocation SelLoc,
116 SourceLocation LParenLoc,
117 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000118 QualType Ty = Context.getObjCSelType();
119 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000120}
121
122Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
123 SourceLocation AtLoc,
124 SourceLocation ProtoLoc,
125 SourceLocation LParenLoc,
126 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000127 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000128 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000129 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000130 return true;
131 }
132
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000133 QualType Ty = Context.getObjCProtoType();
134 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000135 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000136 Ty = Context.getPointerType(Ty);
137 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000138}
139
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000140bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
141 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000142 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000143 SourceLocation lbrac, SourceLocation rbrac,
144 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000145 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000146 // Apply default argument promotion as for (C99 6.5.2.2p6).
147 for (unsigned i = 0; i != NumArgs; i++)
148 DefaultArgumentPromotion(Args[i]);
149
Chris Lattner077bf5e2008-11-24 03:33:13 +0000150 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
151 diag::warn_inst_method_not_found;
152 Diag(lbrac, DiagID)
153 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000154 ReturnType = Context.getObjCIdType();
155 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000156 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000157
158 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000159
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000160 unsigned NumNamedArgs = Sel.getNumArgs();
161 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
162
Chris Lattner85a932e2008-01-04 22:32:30 +0000163 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000164 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000165 Expr *argExpr = Args[i];
166 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
167
Chris Lattner89951a82009-02-20 18:43:26 +0000168 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000169 QualType rhsType = argExpr->getType();
170
171 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000172 if (lhsType->isArrayType())
173 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000174 else if (lhsType->isFunctionType())
175 lhsType = Context.getPointerType(lhsType);
176
Chris Lattner987798a2008-04-02 17:17:33 +0000177 AssignConvertType Result =
178 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000179 if (Args[i] != argExpr) // The expression was converted.
180 Args[i] = argExpr; // Make sure we store the converted expression.
181
182 anyIncompatibleArgs |=
183 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
184 argExpr, "sending");
185 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000186
187 // Promote additional arguments to variadic methods.
188 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000189 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
190 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000191 } else {
192 // Check for extra arguments to non-variadic methods.
193 if (NumArgs != NumNamedArgs) {
194 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000195 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000196 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000197 << SourceRange(Args[NumNamedArgs]->getLocStart(),
198 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000199 }
200 }
201
Chris Lattner85a932e2008-01-04 22:32:30 +0000202 return anyIncompatibleArgs;
203}
204
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000205bool Sema::isSelfExpr(Expr *RExpr) {
206 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
207 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
208 return true;
209 return false;
210}
211
Steve Narofff1afaf62009-02-26 15:55:06 +0000212// Helper method for ActOnClassMethod/ActOnInstanceMethod.
213// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000214// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000215// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000216ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000217 ObjCInterfaceDecl *ClassDecl) {
218 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000219 // lookup in class and all superclasses
220 while (ClassDecl && !Method) {
221 if (ObjCImplementationDecl *ImpDecl =
222 ObjCImplementations[ClassDecl->getIdentifier()])
223 Method = ImpDecl->getClassMethod(Sel);
Steve Narofff1afaf62009-02-26 15:55:06 +0000224
Steve Naroff5609ec02009-03-08 18:56:13 +0000225 // Look through local category implementations associated with the class.
226 if (!Method) {
227 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
228 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
229 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
230 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000231 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000232
233 // Before we give up, check if the selector is an instance method.
234 // But only in the root. This matches gcc's behaviour and what the
235 // runtime expects.
236 if (!Method && !ClassDecl->getSuperClass()) {
237 Method = ClassDecl->lookupInstanceMethod(Sel);
238 // Look through local category implementations associated
239 // with the root class.
240 if (!Method)
241 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
242 }
243
244 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000245 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000246 return Method;
247}
248
249ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
250 ObjCInterfaceDecl *ClassDecl) {
251 ObjCMethodDecl *Method = 0;
252 while (ClassDecl && !Method) {
253 // If we have implementations in scope, check "private" methods.
254 if (ObjCImplementationDecl *ImpDecl =
255 ObjCImplementations[ClassDecl->getIdentifier()])
256 Method = ImpDecl->getInstanceMethod(Sel);
257
258 // Look through local category implementations associated with the class.
259 if (!Method) {
260 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
261 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
262 Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel);
263 }
264 }
265 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000266 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000267 return Method;
268}
269
Chris Lattner85a932e2008-01-04 22:32:30 +0000270// ActOnClassMessage - used for both unary and keyword messages.
271// ArgExprs is optional - if it is present, the number of expressions
272// is obtained from Sel.getNumArgs().
273Sema::ExprResult Sema::ActOnClassMessage(
274 Scope *S,
275 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000276 SourceLocation lbrac, SourceLocation receiverLoc,
277 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000278 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000279{
280 assert(receiverName && "missing receiver class name");
281
282 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000283 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000284 bool isSuper = false;
285
Chris Lattner84692652008-11-20 05:35:30 +0000286 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000287 if (getCurMethodDecl()) {
288 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000289 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
290 if (!OID)
291 return Diag(lbrac, diag::error_no_super_class_message)
292 << getCurMethodDecl()->getDeclName();
293 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000294 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000295 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000296 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000297 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
298 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000299 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
300 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000301 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000302 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
303 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000304 }
305 // We are sending a message to 'super' within a class method. Do nothing,
306 // the receiver will pass through as 'super' (how convenient:-).
307 } else {
308 // 'super' has been used outside a method context. If a variable named
309 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000310 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000311 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
312 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000313 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
314 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000315 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000316 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
317 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000318 }
Chris Lattner08631c52008-11-23 21:45:46 +0000319 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000320 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000321 } else
322 ClassDecl = getObjCInterfaceDecl(receiverName);
323
Steve Naroff7c778f12008-07-25 19:39:00 +0000324 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000325 //
326 // typedef XCElementDisplayRect XCElementGraphicsRect;
327 //
328 // @implementation XCRASlice
329 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
330 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
331 // }
332 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000333 // If necessary, the following lookup could move to getObjCInterfaceDecl().
334 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000335 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000336 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
337 const ObjCInterfaceType *OCIT;
338 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000339 if (!OCIT)
340 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
341 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000342 }
343 }
344 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000345 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000346 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000347 Method = ClassDecl->lookupClassMethod(Sel);
348
349 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000350 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000351 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000352
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000353 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
354 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000355
Chris Lattner077bf5e2008-11-24 03:33:13 +0000356 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000357 lbrac, rbrac, returnType))
358 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000359
360 // If we have the ObjCInterfaceDecl* for the class that is receiving
361 // the message, use that to construct the ObjCMessageExpr. Otherwise
362 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000363 // FIXME: need to do a better job handling 'super' usage within a class
364 // For now, we simply pass the "super" identifier through (which isn't
365 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000366 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000367 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
368 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000369 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000370 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
371 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000372}
373
374// ActOnInstanceMessage - used for both unary and keyword messages.
375// ArgExprs is optional - if it is present, the number of expressions
376// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000377Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000378 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000379 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000380 SourceLocation rbrac,
381 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000382 assert(receiver && "missing receiver expression");
383
384 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
385 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000386 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000387
Chris Lattnerb77792e2008-07-26 22:17:49 +0000388 QualType ReceiverCType =
389 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000390
391 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000392 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000393 ObjCMethodDecl *Method = 0;
394 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
395 // If we have an interface in scope, check 'super' methods.
396 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000397 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000398 Method = SuperDecl->lookupInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000399
400 if (!Method)
401 // If we have implementations in scope, check "private" methods.
402 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
403 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000404 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000405
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000406 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
407 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000408
Chris Lattner077bf5e2008-11-24 03:33:13 +0000409 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000410 lbrac, rbrac, returnType))
411 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000412 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
413 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000414 }
415
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000416 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000417 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
418 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000419 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
420 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000421 if (!Method)
422 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000423 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000424 lbrac, rbrac, returnType))
425 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000426 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
427 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000428 }
429
430 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000431 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000432 ObjCMethodDecl *Method = 0;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000433
Chris Lattner6562fda2008-07-21 06:44:27 +0000434 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000435 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
436 // First check the public methods in the class interface.
437 Method = ClassDecl->lookupClassMethod(Sel);
438
Steve Narofff1afaf62009-02-26 15:55:06 +0000439 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000440 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroffd526c2f2009-02-23 02:25:40 +0000441 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000442 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
443 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000444 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000445 if (!Method) {
446 // If not messaging 'self', look for any factory method named 'Sel'.
447 if (!isSelfExpr(RExpr)) {
448 Method = FactoryMethodPool[Sel].Method;
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000449 if (!Method) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000450 Method = LookupInstanceMethodInGlobalPool(
451 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000452 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000453 }
454 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000455 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000456 lbrac, rbrac, returnType))
457 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000458 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
459 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000460 }
461
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000462 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000463 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000464
465 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
466 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000467 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000468 // Search protocols for instance methods.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000469 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
470 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
471 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
472 break;
473 }
Steve Naroff279d8962009-02-23 15:40:48 +0000474 } else if (const ObjCInterfaceType *OCIType =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000475 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000476 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000477
Steve Naroff279d8962009-02-23 15:40:48 +0000478 ClassDecl = OCIType->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000479 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
480 // faster than the following method (which can do *many* linear searches).
481 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000482 Method = ClassDecl->lookupInstanceMethod(Sel);
483
484 if (!Method) {
485 // Search protocol qualifiers.
Steve Naroff279d8962009-02-23 15:40:48 +0000486 for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
487 E = OCIType->qual_end(); QI != E; ++QI) {
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000488 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000489 break;
490 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000491 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000492 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000493 // If we have implementations in scope, check "private" methods.
494 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
495
496 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000497 // If we still haven't found a method, look in the global pool. This
498 // behavior isn't very desirable, however we need it for GCC
499 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000500 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000501 Method = LookupInstanceMethodInGlobalPool(
502 Sel, SourceRange(lbrac,rbrac));
503 if (Method && !OCIType->getDecl()->isForwardDecl())
504 Diag(lbrac, diag::warn_maynot_respond)
505 << OCIType->getDecl()->getIdentifier()->getName() << Sel;
506 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000507 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000508 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000509 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
510 return true;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000511 } else {
Steve Naroff8e2945a2009-03-01 17:14:31 +0000512 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000513 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000514 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000515 }
516
Chris Lattner077bf5e2008-11-24 03:33:13 +0000517 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000518 lbrac, rbrac, returnType))
519 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000520 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
521 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000522}
Chris Lattnereca7be62008-04-07 05:30:13 +0000523
524//===----------------------------------------------------------------------===//
525// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
526//===----------------------------------------------------------------------===//
527
528/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
529/// inheritance hierarchy of 'rProto'.
530static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
531 ObjCProtocolDecl *rProto) {
532 if (lProto == rProto)
533 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000534 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
535 E = rProto->protocol_end(); PI != E; ++PI)
536 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000537 return true;
538 return false;
539}
540
541/// ClassImplementsProtocol - Checks that 'lProto' protocol
542/// has been implemented in IDecl class, its super class or categories (if
543/// lookupCategory is true).
544static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
545 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000546 bool lookupCategory,
547 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000548
549 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000550 const ObjCList<ObjCProtocolDecl> &Protocols =
551 IDecl->getReferencedProtocols();
552
553 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
554 E = Protocols.end(); PI != E; ++PI) {
555 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000556 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000557 // This is dubious and is added to be compatible with gcc.
558 // In gcc, it is also allowed assigning a protocol-qualified 'id'
559 // type to a LHS object when protocol in qualified LHS is in list
560 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000561 // FIXME: Treat this as an extension, and flag this as an error when
562 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000563 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000564 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000565 }
566
567 // 2nd, look up the category.
568 if (lookupCategory)
569 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
570 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000571 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
572 E = CDecl->protocol_end(); PI != E; ++PI)
573 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000574 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000575 }
576
577 // 3rd, look up the super class(s)
578 if (IDecl->getSuperClass())
579 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000580 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
581 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000582
583 return false;
584}
585
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000586/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
587/// ObjCQualifiedIDType.
Steve Naroff15edf0d2009-03-03 15:43:24 +0000588/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
Chris Lattnereca7be62008-04-07 05:30:13 +0000589bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
590 bool compare) {
591 // Allow id<P..> and an 'id' or void* type in all cases.
592 if (const PointerType *PT = lhs->getAsPointerType()) {
593 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000594 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000595 return true;
596 } else if (const PointerType *PT = rhs->getAsPointerType()) {
597 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000598 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000599 return true;
600 }
601
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000602 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
603 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
604 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000605 QualType rtype;
606
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000607 if (!rhsQID) {
608 // Not comparing two ObjCQualifiedIdType's?
609 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000610
611 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000612 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000613 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000614 // If the RHS is a unqualified interface pointer "NSString*",
615 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000616 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
617 ObjCInterfaceDecl *rhsID = IT->getDecl();
618 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
619 // when comparing an id<P> on lhs with a static type on rhs,
620 // see if static class implements all of id's protocols, directly or
621 // through its super class and categories.
622 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
623 return false;
624 }
625 return true;
626 }
627 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000628 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000629
630 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000631 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000632 RHSProtoI = rhsQI->qual_begin();
633 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000634 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000635 RHSProtoI = rhsQID->qual_begin();
636 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000637 } else {
638 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000639 }
640
641 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
642 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
643 bool match = false;
644
645 // when comparing an id<P> on lhs with a static type on rhs,
646 // see if static class implements all of id's protocols, directly or
647 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000648 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
649 ObjCProtocolDecl *rhsProto = *RHSProtoI;
650 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000651 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000652 match = true;
653 break;
654 }
655 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000656 if (rhsQI) {
657 // If the RHS is a qualified interface pointer "NSString<P>*",
658 // make sure we check the class hierarchy.
659 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
660 ObjCInterfaceDecl *rhsID = IT->getDecl();
661 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
662 // when comparing an id<P> on lhs with a static type on rhs,
663 // see if static class implements all of id's protocols, directly or
664 // through its super class and categories.
665 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
666 match = true;
667 break;
668 }
669 }
670 }
671 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000672 if (!match)
673 return false;
674 }
675
676 return true;
677 }
678
679 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
680 assert(rhsQID && "One of the LHS/RHS should be id<x>");
681
682 if (!lhs->isPointerType())
683 return false;
684
685 QualType ltype = lhs->getAsPointerType()->getPointeeType();
686 if (const ObjCQualifiedInterfaceType *lhsQI =
687 ltype->getAsObjCQualifiedInterfaceType()) {
688 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
689 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
690 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
691 bool match = false;
692 ObjCProtocolDecl *lhsProto = *LHSProtoI;
693 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
694 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
695 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000696 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000697 match = true;
698 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000699 }
700 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000701 if (!match)
702 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000703 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000704 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000705 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000706
707 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
708 // for static type vs. qualified 'id' type, check that class implements
709 // all of 'id's protocols.
710 ObjCInterfaceDecl *lhsID = IT->getDecl();
711 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
712 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000713 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000714 return false;
715 }
716 return true;
717 }
718 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000719}
720