blob: d09279353bf77a5f88df009471ecb1ac59ca17b8 [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"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner85a932e2008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
22 ExprTy **Strings,
23 unsigned NumStrings) {
24 SourceLocation AtLoc = AtLocs[0];
25 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
26 if (NumStrings > 1) {
27 // Concatenate objc strings.
28 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
29 SourceLocation EndLoc = ES->getSourceRange().getEnd();
30 unsigned Length = 0;
31 for (unsigned i = 0; i < NumStrings; i++)
32 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
33 char *strBuf = new char [Length];
34 char *p = strBuf;
35 bool isWide = false;
36 for (unsigned i = 0; i < NumStrings; i++) {
37 S = static_cast<StringLiteral *>(Strings[i]);
38 if (S->isWide())
39 isWide = true;
40 memcpy(p, S->getStrData(), S->getByteLength());
41 p += S->getByteLength();
42 delete S;
43 }
44 S = new StringLiteral(strBuf, Length,
45 isWide, Context.getPointerType(Context.CharTy),
46 AtLoc, EndLoc);
47 }
48
49 if (CheckBuiltinCFStringArgument(S))
50 return true;
51
Ted Kremeneka526c5c2008-01-07 19:49:32 +000052 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000053 // Initialize the constant string interface lazily. This assumes
54 // the NSConstantString interface is seen in this translation unit.
55 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroffb327ce02008-04-02 14:35:35 +000056 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000058 if (strIFace)
59 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000060 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000062 // If there is no NSConstantString interface defined then treat constant
63 // strings as untyped objects and let the runtime figure it out later.
64 if (t == QualType()) {
65 t = Context.getObjCIdType();
66 } else {
67 t = Context.getPointerType(t);
68 }
Chris Lattner85a932e2008-01-04 22:32:30 +000069 return new ObjCStringLiteral(S, t, AtLoc);
70}
71
72Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
73 SourceLocation EncodeLoc,
74 SourceLocation LParenLoc,
75 TypeTy *Ty,
76 SourceLocation RParenLoc) {
77 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
78
79 QualType t = Context.getPointerType(Context.CharTy);
80 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
81}
82
83Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
84 SourceLocation AtLoc,
85 SourceLocation SelLoc,
86 SourceLocation LParenLoc,
87 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000088 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-01-04 22:32:30 +000089 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
90}
91
92Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
93 SourceLocation AtLoc,
94 SourceLocation ProtoLoc,
95 SourceLocation LParenLoc,
96 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000097 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000098 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +000099 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000100 return true;
101 }
102
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000104 if (t.isNull())
105 return true;
106 t = Context.getPointerType(t);
107 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
108}
109
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000110bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
111 Selector Sel, ObjCMethodDecl *Method,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000112 const char *PrefixStr,
113 SourceLocation lbrac, SourceLocation rbrac,
114 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000115 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000116 // Apply default argument promotion as for (C99 6.5.2.2p6).
117 for (unsigned i = 0; i != NumArgs; i++)
118 DefaultArgumentPromotion(Args[i]);
119
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000120 Diag(lbrac, diag::warn_method_not_found)
121 << PrefixStr << Sel.getName() << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000122 ReturnType = Context.getObjCIdType();
123 return false;
124 } else {
125 ReturnType = Method->getResultType();
126 }
127
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000128 unsigned NumNamedArgs = Sel.getNumArgs();
129 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
130
Chris Lattner85a932e2008-01-04 22:32:30 +0000131 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000132 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000133 Expr *argExpr = Args[i];
134 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
135
136 QualType lhsType = Method->getParamDecl(i)->getType();
137 QualType rhsType = argExpr->getType();
138
139 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000140 if (lhsType->isArrayType())
141 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000142 else if (lhsType->isFunctionType())
143 lhsType = Context.getPointerType(lhsType);
144
Chris Lattner987798a2008-04-02 17:17:33 +0000145 AssignConvertType Result =
146 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000147 if (Args[i] != argExpr) // The expression was converted.
148 Args[i] = argExpr; // Make sure we store the converted expression.
149
150 anyIncompatibleArgs |=
151 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
152 argExpr, "sending");
153 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000154
155 // Promote additional arguments to variadic methods.
156 if (Method->isVariadic()) {
157 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
158 DefaultArgumentPromotion(Args[i]);
159 } else {
160 // Check for extra arguments to non-variadic methods.
161 if (NumArgs != NumNamedArgs) {
162 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000163 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000164 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000165 << SourceRange(Args[NumNamedArgs]->getLocStart(),
166 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000167 }
168 }
169
Chris Lattner85a932e2008-01-04 22:32:30 +0000170 return anyIncompatibleArgs;
171}
172
173// ActOnClassMessage - used for both unary and keyword messages.
174// ArgExprs is optional - if it is present, the number of expressions
175// is obtained from Sel.getNumArgs().
176Sema::ExprResult Sema::ActOnClassMessage(
177 Scope *S,
178 IdentifierInfo *receiverName, Selector Sel,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000179 SourceLocation lbrac, SourceLocation receiverLoc, SourceLocation rbrac,
180 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000181{
182 assert(receiverName && "missing receiver class name");
183
184 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000185 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000186 bool isSuper = false;
187
Chris Lattner84692652008-11-20 05:35:30 +0000188 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000189 if (getCurMethodDecl()) {
190 isSuper = true;
191 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
192 if (!ClassDecl)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000193 return Diag(lbrac, diag::error_no_super_class)
194 << getCurMethodDecl()->getClassInterface()->getName();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000195 if (getCurMethodDecl()->isInstance()) {
196 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
197 superTy = Context.getPointerType(superTy);
198 ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy);
199 // We are really in an instance method, redirect.
200 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
201 Args, NumArgs);
202 }
203 // We are sending a message to 'super' within a class method. Do nothing,
204 // the receiver will pass through as 'super' (how convenient:-).
205 } else {
206 // 'super' has been used outside a method context. If a variable named
207 // 'super' has been declared, redirect. If not, produce a diagnostic.
208 Decl *SuperDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, S, false);
209 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
210 if (VD) {
211 ExprResult ReceiverExpr = new DeclRefExpr(VD, VD->getType(),
212 receiverLoc);
213 // We are really in an instance method, redirect.
214 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
215 Args, NumArgs);
216 }
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000217 return Diag(receiverLoc, diag::err_undeclared_var_use)
218 << receiverName->getName();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000219 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000220 } else
221 ClassDecl = getObjCInterfaceDecl(receiverName);
222
Steve Naroff7c778f12008-07-25 19:39:00 +0000223 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000224 //
225 // typedef XCElementDisplayRect XCElementGraphicsRect;
226 //
227 // @implementation XCRASlice
228 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
229 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
230 // }
231 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000232 // If necessary, the following lookup could move to getObjCInterfaceDecl().
233 if (!ClassDecl) {
234 Decl *IDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, 0, false);
235 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
236 const ObjCInterfaceType *OCIT;
237 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
238 if (OCIT)
239 ClassDecl = OCIT->getDecl();
240 }
241 }
242 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000243 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000244 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000245 Method = ClassDecl->lookupClassMethod(Sel);
246
247 // If we have an implementation in scope, check "private" methods.
248 if (!Method) {
249 if (ObjCImplementationDecl *ImpDecl =
250 ObjCImplementations[ClassDecl->getIdentifier()])
251 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000252
253 // Look through local category implementations associated with the class.
254 if (!Method) {
255 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
256 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
257 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
258 }
259 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000260 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000261 // Before we give up, check if the selector is an instance method.
262 if (!Method)
263 Method = ClassDecl->lookupInstanceMethod(Sel);
264
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000265 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "+",
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000266 lbrac, rbrac, returnType))
267 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000268
269 // If we have the ObjCInterfaceDecl* for the class that is receiving
270 // the message, use that to construct the ObjCMessageExpr. Otherwise
271 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000272 // FIXME: need to do a better job handling 'super' usage within a class
273 // For now, we simply pass the "super" identifier through (which isn't
274 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000275 if (isSuper)
Steve Narofffc93d522008-07-24 19:44:33 +0000276 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000277 lbrac, rbrac, ArgExprs, NumArgs);
278 else
Steve Narofffc93d522008-07-24 19:44:33 +0000279 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenek4df728e2008-06-24 15:50:53 +0000280 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000281}
282
283// ActOnInstanceMessage - used for both unary and keyword messages.
284// ArgExprs is optional - if it is present, the number of expressions
285// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000286Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000287 SourceLocation lbrac,
288 SourceLocation rbrac,
289 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000290 assert(receiver && "missing receiver expression");
291
292 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
293 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000294 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000295
Chris Lattnerb77792e2008-07-26 22:17:49 +0000296 QualType ReceiverCType =
297 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000298
299 // Handle messages to 'super'.
300 if (isa<ObjCSuperExpr>(RExpr)) {
301 ObjCMethodDecl *Method = 0;
302 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
303 // If we have an interface in scope, check 'super' methods.
304 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
305 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
306 Method = SuperDecl->lookupInstanceMethod(Sel);
307 }
308 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
309 lbrac, rbrac, returnType))
310 return true;
311 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
312 ArgExprs, NumArgs);
313 }
314
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000315 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000316 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
317 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000318 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
319 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000320 if (!Method)
321 Method = FactoryMethodPool[Sel].Method;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000322 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000323 lbrac, rbrac, returnType))
324 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000325 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
326 ArgExprs, NumArgs);
327 }
328
329 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000330 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000331 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000332 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000333 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000334 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000335 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000336 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000337 Method = ImpDecl->getClassMethod(Sel);
338 }
339 if (!Method)
340 Method = FactoryMethodPool[Sel].Method;
341 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000342 Method = LookupInstanceMethodInGlobalPool(
343 Sel, SourceRange(lbrac,rbrac));
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000344 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000345 lbrac, rbrac, returnType))
346 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000347 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
348 ArgExprs, NumArgs);
349 }
350
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000351 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000352 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000353
354 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
355 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000356 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000357 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000358 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
359 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
360 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
361 break;
362 }
363 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000364 Diag(lbrac, diag::warn_method_not_found_in_protocol)
365 << "-" << Sel.getName() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000366 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000367 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000368 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000369
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000370 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000371 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
372 // faster than the following method (which can do *many* linear searches).
373 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000374 Method = ClassDecl->lookupInstanceMethod(Sel);
375
376 if (!Method) {
377 // Search protocol qualifiers.
378 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
379 E = OCIReceiver->qual_end(); QI != E; ++QI) {
380 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000381 break;
382 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000383 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000384
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000385 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000386 Diag(lbrac, diag::warn_method_not_found_in_protocol)
387 << "-" << Sel.getName() << SourceRange(lbrac, rbrac);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000388 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000389 Diag(lbrac, diag::error_bad_receiver_type)
390 << RExpr->getType().getAsString() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000391 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000392 }
393
394 if (!Method) {
395 // If we have an implementation in scope, check "private" methods.
396 if (ClassDecl)
397 if (ObjCImplementationDecl *ImpDecl =
398 ObjCImplementations[ClassDecl->getIdentifier()])
399 Method = ImpDecl->getInstanceMethod(Sel);
400 // If we still haven't found a method, look in the global pool. This
401 // behavior isn't very desirable, however we need it for GCC
402 // compatibility.
403 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000404 Method = LookupInstanceMethodInGlobalPool(
405 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000406 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000407 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000408 lbrac, rbrac, returnType))
409 return true;
Chris Lattner85a932e2008-01-04 22:32:30 +0000410 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
411 ArgExprs, NumArgs);
412}
Chris Lattnereca7be62008-04-07 05:30:13 +0000413
414//===----------------------------------------------------------------------===//
415// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
416//===----------------------------------------------------------------------===//
417
418/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
419/// inheritance hierarchy of 'rProto'.
420static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
421 ObjCProtocolDecl *rProto) {
422 if (lProto == rProto)
423 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000424 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
425 E = rProto->protocol_end(); PI != E; ++PI)
426 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000427 return true;
428 return false;
429}
430
431/// ClassImplementsProtocol - Checks that 'lProto' protocol
432/// has been implemented in IDecl class, its super class or categories (if
433/// lookupCategory is true).
434static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
435 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000436 bool lookupCategory,
437 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000438
439 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000440 const ObjCList<ObjCProtocolDecl> &Protocols =
441 IDecl->getReferencedProtocols();
442
443 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
444 E = Protocols.end(); PI != E; ++PI) {
445 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000446 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000447 // This is dubious and is added to be compatible with gcc.
448 // In gcc, it is also allowed assigning a protocol-qualified 'id'
449 // type to a LHS object when protocol in qualified LHS is in list
450 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000451 // FIXME: Treat this as an extension, and flag this as an error when
452 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000453 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000454 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000455 }
456
457 // 2nd, look up the category.
458 if (lookupCategory)
459 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
460 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000461 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
462 E = CDecl->protocol_end(); PI != E; ++PI)
463 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000464 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000465 }
466
467 // 3rd, look up the super class(s)
468 if (IDecl->getSuperClass())
469 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000470 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
471 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000472
473 return false;
474}
475
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000476/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
477/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000478bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
479 bool compare) {
480 // Allow id<P..> and an 'id' or void* type in all cases.
481 if (const PointerType *PT = lhs->getAsPointerType()) {
482 QualType PointeeTy = PT->getPointeeType();
483 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
484 return true;
485 } else if (const PointerType *PT = rhs->getAsPointerType()) {
486 QualType PointeeTy = PT->getPointeeType();
487 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
488 return true;
489 }
490
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000491 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
492 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
493 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000494 QualType rtype;
495
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000496 if (!rhsQID) {
497 // Not comparing two ObjCQualifiedIdType's?
498 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000499
500 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000501 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000502 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000503 // If the RHS is a unqualified interface pointer "NSString*",
504 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000505 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
506 ObjCInterfaceDecl *rhsID = IT->getDecl();
507 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
508 // when comparing an id<P> on lhs with a static type on rhs,
509 // see if static class implements all of id's protocols, directly or
510 // through its super class and categories.
511 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
512 return false;
513 }
514 return true;
515 }
516 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000517 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000518
519 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000520 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000521 RHSProtoI = rhsQI->qual_begin();
522 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000523 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000524 RHSProtoI = rhsQID->qual_begin();
525 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000526 } else {
527 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000528 }
529
530 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
531 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
532 bool match = false;
533
534 // when comparing an id<P> on lhs with a static type on rhs,
535 // see if static class implements all of id's protocols, directly or
536 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000537 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
538 ObjCProtocolDecl *rhsProto = *RHSProtoI;
539 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
540 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000541 match = true;
542 break;
543 }
544 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000545 if (rhsQI) {
546 // If the RHS is a qualified interface pointer "NSString<P>*",
547 // make sure we check the class hierarchy.
548 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
549 ObjCInterfaceDecl *rhsID = IT->getDecl();
550 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
551 // when comparing an id<P> on lhs with a static type on rhs,
552 // see if static class implements all of id's protocols, directly or
553 // through its super class and categories.
554 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
555 match = true;
556 break;
557 }
558 }
559 }
560 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000561 if (!match)
562 return false;
563 }
564
565 return true;
566 }
567
568 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
569 assert(rhsQID && "One of the LHS/RHS should be id<x>");
570
571 if (!lhs->isPointerType())
572 return false;
573
574 QualType ltype = lhs->getAsPointerType()->getPointeeType();
575 if (const ObjCQualifiedInterfaceType *lhsQI =
576 ltype->getAsObjCQualifiedInterfaceType()) {
577 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
578 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
579 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
580 bool match = false;
581 ObjCProtocolDecl *lhsProto = *LHSProtoI;
582 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
583 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
584 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnereca7be62008-04-07 05:30:13 +0000585 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000586 match = true;
587 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000588 }
589 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000590 if (!match)
591 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000592 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000593 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000594 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000595
596 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
597 // for static type vs. qualified 'id' type, check that class implements
598 // all of 'id's protocols.
599 ObjCInterfaceDecl *lhsID = IT->getDecl();
600 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
601 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000602 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000603 return false;
604 }
605 return true;
606 }
607 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000608}
609