blob: a73f386dabec810309937624c70b867138b87ad5 [file] [log] [blame]
Chris Lattner09493052008-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 Naroff9ed3e772008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +000052 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner09493052008-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 Naroff6384a012008-04-02 14:35:35 +000056 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattnerbac12452008-06-21 21:44:18 +000058 if (strIFace)
59 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000060 }
Ted Kremenek42730c52008-01-07 19:49:32 +000061 QualType t = Context.getObjCConstantStringInterface();
Chris Lattnerbac12452008-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 Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +000088 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-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 Kremenek42730c52008-01-07 19:49:32 +000097 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000098 if (!PDecl) {
Chris Lattner65cae292008-11-19 08:23:25 +000099 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner09493052008-01-04 22:32:30 +0000100 return true;
101 }
102
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-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 Dunbar7776e102008-09-11 00:50:25 +0000110bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
111 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000112 bool isClassMessage,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000113 SourceLocation lbrac, SourceLocation rbrac,
114 QualType &ReturnType) {
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000115 if (!Method) {
Daniel Dunbar753d1102008-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 Lattner3a8f2942008-11-24 03:33:13 +0000120 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
121 diag::warn_inst_method_not_found;
122 Diag(lbrac, DiagID)
123 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000124 ReturnType = Context.getObjCIdType();
125 return false;
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000126 }
Chris Lattner3a8f2942008-11-24 03:33:13 +0000127
128 ReturnType = Method->getResultType();
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000129
Daniel Dunbar7776e102008-09-11 00:50:25 +0000130 unsigned NumNamedArgs = Sel.getNumArgs();
131 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
132
Chris Lattner09493052008-01-04 22:32:30 +0000133 bool anyIncompatibleArgs = false;
Daniel Dunbar7776e102008-09-11 00:50:25 +0000134 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner09493052008-01-04 22:32:30 +0000135 Expr *argExpr = Args[i];
136 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
137
138 QualType lhsType = Method->getParamDecl(i)->getType();
139 QualType rhsType = argExpr->getType();
140
141 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnera2ef0822008-04-02 17:17:33 +0000142 if (lhsType->isArrayType())
143 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000144 else if (lhsType->isFunctionType())
145 lhsType = Context.getPointerType(lhsType);
146
Chris Lattnera2ef0822008-04-02 17:17:33 +0000147 AssignConvertType Result =
148 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-01-04 22:32:30 +0000149 if (Args[i] != argExpr) // The expression was converted.
150 Args[i] = argExpr; // Make sure we store the converted expression.
151
152 anyIncompatibleArgs |=
153 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
154 argExpr, "sending");
155 }
Daniel Dunbar7776e102008-09-11 00:50:25 +0000156
157 // Promote additional arguments to variadic methods.
158 if (Method->isVariadic()) {
Anders Carlssonfde627e2009-01-13 05:48:52 +0000159 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
160 if (!Args[i]->getType()->isPODType()) {
161 Diag(Args[i]->getLocStart(),
162 diag::warn_cannot_pass_non_pod_arg_to_vararg) <<
163 Args[i]->getType() << 2; // Method
164 }
165
Daniel Dunbar7776e102008-09-11 00:50:25 +0000166 DefaultArgumentPromotion(Args[i]);
Anders Carlssonfde627e2009-01-13 05:48:52 +0000167 }
Daniel Dunbar7776e102008-09-11 00:50:25 +0000168 } else {
169 // Check for extra arguments to non-variadic methods.
170 if (NumArgs != NumNamedArgs) {
171 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattner8ba580c2008-11-19 05:08:23 +0000172 diag::err_typecheck_call_too_many_args)
Chris Lattner66beaba2008-11-21 18:44:24 +0000173 << 2 /*method*/ << Method->getSourceRange()
Chris Lattner8ba580c2008-11-19 05:08:23 +0000174 << SourceRange(Args[NumNamedArgs]->getLocStart(),
175 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar7776e102008-09-11 00:50:25 +0000176 }
177 }
178
Chris Lattner09493052008-01-04 22:32:30 +0000179 return anyIncompatibleArgs;
180}
181
182// ActOnClassMessage - used for both unary and keyword messages.
183// ArgExprs is optional - if it is present, the number of expressions
184// is obtained from Sel.getNumArgs().
185Sema::ExprResult Sema::ActOnClassMessage(
186 Scope *S,
187 IdentifierInfo *receiverName, Selector Sel,
Steve Naroffc64a53d2008-11-19 15:54:23 +0000188 SourceLocation lbrac, SourceLocation receiverLoc, SourceLocation rbrac,
189 ExprTy **Args, unsigned NumArgs)
Chris Lattner09493052008-01-04 22:32:30 +0000190{
191 assert(receiverName && "missing receiver class name");
192
193 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 ObjCInterfaceDecl* ClassDecl = 0;
Steve Naroff91a69202008-07-24 19:44:33 +0000195 bool isSuper = false;
196
Chris Lattner87fada82008-11-20 05:35:30 +0000197 if (receiverName->isStr("super")) {
Steve Naroffc64a53d2008-11-19 15:54:23 +0000198 if (getCurMethodDecl()) {
199 isSuper = true;
Fariborz Jahaniana04fcb62009-01-07 21:01:41 +0000200 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
201 if (!OID)
202 return Diag(lbrac, diag::error_no_super_class_message)
203 << getCurMethodDecl()->getDeclName();
204 ClassDecl = OID->getSuperClass();
Steve Naroffc64a53d2008-11-19 15:54:23 +0000205 if (!ClassDecl)
Fariborz Jahaniana04fcb62009-01-07 21:01:41 +0000206 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregor5d764842009-01-09 17:18:27 +0000207 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroffc64a53d2008-11-19 15:54:23 +0000208 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
209 superTy = Context.getPointerType(superTy);
210 ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy);
211 // We are really in an instance method, redirect.
212 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
213 Args, NumArgs);
214 }
215 // We are sending a message to 'super' within a class method. Do nothing,
216 // the receiver will pass through as 'super' (how convenient:-).
217 } else {
218 // 'super' has been used outside a method context. If a variable named
219 // 'super' has been declared, redirect. If not, produce a diagnostic.
220 Decl *SuperDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, S, false);
221 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
222 if (VD) {
223 ExprResult ReceiverExpr = new DeclRefExpr(VD, VD->getType(),
224 receiverLoc);
225 // We are really in an instance method, redirect.
226 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
227 Args, NumArgs);
228 }
Chris Lattnerb1753422008-11-23 21:45:46 +0000229 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroffc64a53d2008-11-19 15:54:23 +0000230 }
Chris Lattner09493052008-01-04 22:32:30 +0000231 } else
232 ClassDecl = getObjCInterfaceDecl(receiverName);
233
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000234 // The following code allows for the following GCC-ism:
Steve Naroff51c6cff2008-06-04 23:08:38 +0000235 //
236 // typedef XCElementDisplayRect XCElementGraphicsRect;
237 //
238 // @implementation XCRASlice
239 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
240 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
241 // }
242 //
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000243 // If necessary, the following lookup could move to getObjCInterfaceDecl().
244 if (!ClassDecl) {
245 Decl *IDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, 0, false);
246 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
247 const ObjCInterfaceType *OCIT;
248 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
249 if (OCIT)
250 ClassDecl = OCIT->getDecl();
251 }
252 }
253 assert(ClassDecl && "missing interface declaration");
Steve Naroff51c6cff2008-06-04 23:08:38 +0000254 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000255 QualType returnType;
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000256 Method = ClassDecl->lookupClassMethod(Sel);
257
258 // If we have an implementation in scope, check "private" methods.
259 if (!Method) {
260 if (ObjCImplementationDecl *ImpDecl =
261 ObjCImplementations[ClassDecl->getIdentifier()])
262 Method = ImpDecl->getClassMethod(Sel);
Steve Naroff4b9846d2008-09-28 14:55:53 +0000263
264 // Look through local category implementations associated with the class.
265 if (!Method) {
266 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
267 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
268 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
269 }
270 }
Steve Naroff616c55c2008-06-04 14:43:54 +0000271 }
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000272 // Before we give up, check if the selector is an instance method.
273 if (!Method)
274 Method = ClassDecl->lookupInstanceMethod(Sel);
275
Chris Lattner3a8f2942008-11-24 03:33:13 +0000276 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000277 lbrac, rbrac, returnType))
278 return true;
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000279
280 // If we have the ObjCInterfaceDecl* for the class that is receiving
281 // the message, use that to construct the ObjCMessageExpr. Otherwise
282 // pass on the IdentifierInfo* for the class.
Steve Naroff91a69202008-07-24 19:44:33 +0000283 // FIXME: need to do a better job handling 'super' usage within a class
284 // For now, we simply pass the "super" identifier through (which isn't
285 // consistent with instance methods.
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000286 if (isSuper)
Steve Naroff91a69202008-07-24 19:44:33 +0000287 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000288 lbrac, rbrac, ArgExprs, NumArgs);
289 else
Steve Naroff91a69202008-07-24 19:44:33 +0000290 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000291 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000292}
293
294// ActOnInstanceMessage - used for both unary and keyword messages.
295// ArgExprs is optional - if it is present, the number of expressions
296// is obtained from Sel.getNumArgs().
Chris Lattner4d018542008-07-21 06:31:05 +0000297Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000298 SourceLocation lbrac,
299 SourceLocation rbrac,
300 ExprTy **Args, unsigned NumArgs) {
Chris Lattner09493052008-01-04 22:32:30 +0000301 assert(receiver && "missing receiver expression");
302
303 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
304 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner09493052008-01-04 22:32:30 +0000305 QualType returnType;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000306
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000307 QualType ReceiverCType =
308 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff7926f102008-11-17 22:29:32 +0000309
310 // Handle messages to 'super'.
311 if (isa<ObjCSuperExpr>(RExpr)) {
312 ObjCMethodDecl *Method = 0;
313 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
314 // If we have an interface in scope, check 'super' methods.
315 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
316 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
317 Method = SuperDecl->lookupInstanceMethod(Sel);
318 }
Chris Lattner3a8f2942008-11-24 03:33:13 +0000319 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff7926f102008-11-17 22:29:32 +0000320 lbrac, rbrac, returnType))
321 return true;
322 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
323 ArgExprs, NumArgs);
324 }
325
Chris Lattner4cff4b72008-07-21 05:57:44 +0000326 // Handle messages to id.
Steve Naroffef5b9fc2008-09-29 16:51:41 +0000327 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
328 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff68354f32008-09-30 14:38:43 +0000329 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
330 Sel, SourceRange(lbrac,rbrac));
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000331 if (!Method)
332 Method = FactoryMethodPool[Sel].Method;
Chris Lattner3a8f2942008-11-24 03:33:13 +0000333 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000334 lbrac, rbrac, returnType))
335 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000336 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
337 ArgExprs, NumArgs);
338 }
339
340 // Handle messages to Class.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000341 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000342 ObjCMethodDecl *Method = 0;
Chris Lattner55a24332008-07-21 06:44:27 +0000343 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +0000344 // If we have an implementation in scope, check "private" methods.
Chris Lattner55a24332008-07-21 06:44:27 +0000345 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff1a5027c2008-06-05 14:49:39 +0000346 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner4cff4b72008-07-21 05:57:44 +0000347 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroff1a5027c2008-06-05 14:49:39 +0000348 Method = ImpDecl->getClassMethod(Sel);
349 }
350 if (!Method)
351 Method = FactoryMethodPool[Sel].Method;
352 if (!Method)
Steve Naroff68354f32008-09-30 14:38:43 +0000353 Method = LookupInstanceMethodInGlobalPool(
354 Sel, SourceRange(lbrac,rbrac));
Chris Lattner3a8f2942008-11-24 03:33:13 +0000355 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000356 lbrac, rbrac, returnType))
357 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000358 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
359 ArgExprs, NumArgs);
360 }
361
Chris Lattnerf9147652008-07-21 06:12:56 +0000362 ObjCMethodDecl *Method = 0;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000363 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattnerf9147652008-07-21 06:12:56 +0000364
365 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
366 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000367 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000368 // Search protocols
Chris Lattner4cff4b72008-07-21 05:57:44 +0000369 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
370 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
371 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
372 break;
373 }
374 if (!Method)
Chris Lattner8ba580c2008-11-19 05:08:23 +0000375 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner3a8f2942008-11-24 03:33:13 +0000376 << Sel << RExpr->getSourceRange();
Chris Lattnerf9147652008-07-21 06:12:56 +0000377 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000378 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000379 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnere1b957d2008-02-01 06:43:02 +0000380
Chris Lattner4cff4b72008-07-21 05:57:44 +0000381 ClassDecl = OCIReceiver->getDecl();
Steve Naroff68354f32008-09-30 14:38:43 +0000382 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
383 // faster than the following method (which can do *many* linear searches).
384 // The idea is to add class info to InstanceMethodPool.
Chris Lattner4cff4b72008-07-21 05:57:44 +0000385 Method = ClassDecl->lookupInstanceMethod(Sel);
386
387 if (!Method) {
388 // Search protocol qualifiers.
389 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
390 E = OCIReceiver->qual_end(); QI != E; ++QI) {
391 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner09493052008-01-04 22:32:30 +0000392 break;
393 }
Chris Lattner09493052008-01-04 22:32:30 +0000394 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000395
Chris Lattner4cff4b72008-07-21 05:57:44 +0000396 if (!Method && !OCIReceiver->qual_empty())
Chris Lattner8ba580c2008-11-19 05:08:23 +0000397 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner3a8f2942008-11-24 03:33:13 +0000398 << Sel << SourceRange(lbrac, rbrac);
Chris Lattnerf9147652008-07-21 06:12:56 +0000399 } else {
Chris Lattner8ba580c2008-11-19 05:08:23 +0000400 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000401 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattnerf9147652008-07-21 06:12:56 +0000402 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000403 }
404
405 if (!Method) {
406 // If we have an implementation in scope, check "private" methods.
407 if (ClassDecl)
408 if (ObjCImplementationDecl *ImpDecl =
409 ObjCImplementations[ClassDecl->getIdentifier()])
410 Method = ImpDecl->getInstanceMethod(Sel);
411 // If we still haven't found a method, look in the global pool. This
412 // behavior isn't very desirable, however we need it for GCC
413 // compatibility.
414 if (!Method)
Steve Naroff68354f32008-09-30 14:38:43 +0000415 Method = LookupInstanceMethodInGlobalPool(
416 Sel, SourceRange(lbrac,rbrac));
Chris Lattner4cff4b72008-07-21 05:57:44 +0000417 }
Chris Lattner3a8f2942008-11-24 03:33:13 +0000418 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000419 lbrac, rbrac, returnType))
420 return true;
Chris Lattner09493052008-01-04 22:32:30 +0000421 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
422 ArgExprs, NumArgs);
423}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000424
425//===----------------------------------------------------------------------===//
426// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
427//===----------------------------------------------------------------------===//
428
429/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
430/// inheritance hierarchy of 'rProto'.
431static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
432 ObjCProtocolDecl *rProto) {
433 if (lProto == rProto)
434 return true;
Chris Lattner0be08822008-07-21 21:32:27 +0000435 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
436 E = rProto->protocol_end(); PI != E; ++PI)
437 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000438 return true;
439 return false;
440}
441
442/// ClassImplementsProtocol - Checks that 'lProto' protocol
443/// has been implemented in IDecl class, its super class or categories (if
444/// lookupCategory is true).
445static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
446 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000447 bool lookupCategory,
448 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000449
450 // 1st, look up the class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000451 const ObjCList<ObjCProtocolDecl> &Protocols =
452 IDecl->getReferencedProtocols();
453
454 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
455 E = Protocols.end(); PI != E; ++PI) {
456 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000457 return true;
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000458 // This is dubious and is added to be compatible with gcc.
459 // In gcc, it is also allowed assigning a protocol-qualified 'id'
460 // type to a LHS object when protocol in qualified LHS is in list
461 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenek5a5cd832008-06-04 20:48:08 +0000462 // FIXME: Treat this as an extension, and flag this as an error when
463 // GCC extensions are not enabled.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000464 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000465 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000466 }
467
468 // 2nd, look up the category.
469 if (lookupCategory)
470 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
471 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner0be08822008-07-21 21:32:27 +0000472 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
473 E = CDecl->protocol_end(); PI != E; ++PI)
474 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000475 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000476 }
477
478 // 3rd, look up the super class(s)
479 if (IDecl->getSuperClass())
480 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000481 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
482 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000483
484 return false;
485}
486
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000487/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
488/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000489bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
490 bool compare) {
491 // Allow id<P..> and an 'id' or void* type in all cases.
492 if (const PointerType *PT = lhs->getAsPointerType()) {
493 QualType PointeeTy = PT->getPointeeType();
494 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
495 return true;
496 } else if (const PointerType *PT = rhs->getAsPointerType()) {
497 QualType PointeeTy = PT->getPointeeType();
498 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
499 return true;
500 }
501
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000502 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
503 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
504 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000505 QualType rtype;
506
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000507 if (!rhsQID) {
508 // Not comparing two ObjCQualifiedIdType's?
509 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000510
511 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000512 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000513 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000514 // If the RHS is a unqualified interface pointer "NSString*",
515 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000516 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
517 ObjCInterfaceDecl *rhsID = IT->getDecl();
518 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
519 // when comparing an id<P> on lhs with a static type on rhs,
520 // see if static class implements all of id's protocols, directly or
521 // through its super class and categories.
522 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
523 return false;
524 }
525 return true;
526 }
527 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000528 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000529
530 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000531 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000532 RHSProtoI = rhsQI->qual_begin();
533 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000534 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000535 RHSProtoI = rhsQID->qual_begin();
536 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000537 } else {
538 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000539 }
540
541 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
542 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
543 bool match = false;
544
545 // when comparing an id<P> on lhs with a static type on rhs,
546 // see if static class implements all of id's protocols, directly or
547 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000548 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
549 ObjCProtocolDecl *rhsProto = *RHSProtoI;
550 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman70c167d2008-12-16 20:15:50 +0000551 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000552 match = true;
553 break;
554 }
555 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000556 if (rhsQI) {
557 // If the RHS is a qualified interface pointer "NSString<P>*",
558 // make sure we check the class hierarchy.
559 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
560 ObjCInterfaceDecl *rhsID = IT->getDecl();
561 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
562 // when comparing an id<P> on lhs with a static type on rhs,
563 // see if static class implements all of id's protocols, directly or
564 // through its super class and categories.
565 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
566 match = true;
567 break;
568 }
569 }
570 }
571 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000572 if (!match)
573 return false;
574 }
575
576 return true;
577 }
578
579 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
580 assert(rhsQID && "One of the LHS/RHS should be id<x>");
581
582 if (!lhs->isPointerType())
583 return false;
584
585 QualType ltype = lhs->getAsPointerType()->getPointeeType();
586 if (const ObjCQualifiedInterfaceType *lhsQI =
587 ltype->getAsObjCQualifiedInterfaceType()) {
588 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
589 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
590 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
591 bool match = false;
592 ObjCProtocolDecl *lhsProto = *LHSProtoI;
593 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
594 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
595 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman70c167d2008-12-16 20:15:50 +0000596 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000597 match = true;
598 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000599 }
600 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000601 if (!match)
602 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000603 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000604 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000605 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000606
607 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
608 // for static type vs. qualified 'id' type, check that class implements
609 // all of 'id's protocols.
610 ObjCInterfaceDecl *lhsID = IT->getDecl();
611 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
612 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000613 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000614 return false;
615 }
616 return true;
617 }
618 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000619}
620