blob: 1da74b9c320ee8bd3cc013be5d80e43d3a2d4cb1 [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 Lattner85a932e2008-01-04 22:32:30 +000018using namespace clang;
19
20Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
21 ExprTy **Strings,
22 unsigned NumStrings) {
23 SourceLocation AtLoc = AtLocs[0];
24 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
25 if (NumStrings > 1) {
26 // Concatenate objc strings.
27 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
28 SourceLocation EndLoc = ES->getSourceRange().getEnd();
29 unsigned Length = 0;
30 for (unsigned i = 0; i < NumStrings; i++)
31 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
Chris Lattner726e1682009-02-18 05:49:11 +000032
33 // FIXME: This should not be allocated by SEMA!
34 char *strBuf = new char[Length];
Chris Lattner85a932e2008-01-04 22:32:30 +000035 char *p = strBuf;
36 bool isWide = false;
Chris Lattner726e1682009-02-18 05:49:11 +000037 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner85a932e2008-01-04 22:32:30 +000038 S = static_cast<StringLiteral *>(Strings[i]);
39 if (S->isWide())
40 isWide = true;
41 memcpy(p, S->getStrData(), S->getByteLength());
42 p += S->getByteLength();
Ted Kremenek8189cde2009-02-07 01:47:29 +000043 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000044 }
Chris Lattner726e1682009-02-18 05:49:11 +000045 // FIXME: PASS LOCATIONS PROPERLY.
46 S = new (Context) StringLiteral(Context, strBuf, Length, isWide,
47 Context.getPointerType(Context.CharTy),
48 AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000049 }
50
Chris Lattner69039812009-02-18 06:01:06 +000051 // Verify that this composite string is acceptable for ObjC strings.
52 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000053 return true;
54
Ted Kremeneka526c5c2008-01-07 19:49:32 +000055 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000056 // Initialize the constant string interface lazily. This assumes
57 // the NSConstantString interface is seen in this translation unit.
58 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000059 NamedDecl *IFace = LookupName(TUScope, NSIdent, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000061 if (strIFace)
62 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000063 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000064 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000065 // If there is no NSConstantString interface defined then treat constant
66 // strings as untyped objects and let the runtime figure it out later.
67 if (t == QualType()) {
68 t = Context.getObjCIdType();
69 } else {
70 t = Context.getPointerType(t);
71 }
Ted Kremenek8189cde2009-02-07 01:47:29 +000072 return new (Context) ObjCStringLiteral(S, t, AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000073}
74
75Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
76 SourceLocation EncodeLoc,
77 SourceLocation LParenLoc,
78 TypeTy *Ty,
79 SourceLocation RParenLoc) {
80 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
81
82 QualType t = Context.getPointerType(Context.CharTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +000083 return new (Context) ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000084}
85
86Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
87 SourceLocation AtLoc,
88 SourceLocation SelLoc,
89 SourceLocation LParenLoc,
90 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000091 QualType t = Context.getObjCSelType();
Ted Kremenek8189cde2009-02-07 01:47:29 +000092 return new (Context) ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000093}
94
95Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
96 SourceLocation AtLoc,
97 SourceLocation ProtoLoc,
98 SourceLocation LParenLoc,
99 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000100 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000101 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000102 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000103 return true;
104 }
105
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000106 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000107 if (t.isNull())
108 return true;
109 t = Context.getPointerType(t);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000110 return new (Context) ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000111}
112
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000113bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
114 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000115 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000116 SourceLocation lbrac, SourceLocation rbrac,
117 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000118 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000119 // Apply default argument promotion as for (C99 6.5.2.2p6).
120 for (unsigned i = 0; i != NumArgs; i++)
121 DefaultArgumentPromotion(Args[i]);
122
Chris Lattner077bf5e2008-11-24 03:33:13 +0000123 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
124 diag::warn_inst_method_not_found;
125 Diag(lbrac, DiagID)
126 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000127 ReturnType = Context.getObjCIdType();
128 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000129 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000130
131 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000132
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000133 unsigned NumNamedArgs = Sel.getNumArgs();
134 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
135
Chris Lattner85a932e2008-01-04 22:32:30 +0000136 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000137 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000138 Expr *argExpr = Args[i];
139 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
140
141 QualType lhsType = Method->getParamDecl(i)->getType();
142 QualType rhsType = argExpr->getType();
143
144 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000145 if (lhsType->isArrayType())
146 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000147 else if (lhsType->isFunctionType())
148 lhsType = Context.getPointerType(lhsType);
149
Chris Lattner987798a2008-04-02 17:17:33 +0000150 AssignConvertType Result =
151 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000152 if (Args[i] != argExpr) // The expression was converted.
153 Args[i] = argExpr; // Make sure we store the converted expression.
154
155 anyIncompatibleArgs |=
156 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
157 argExpr, "sending");
158 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000159
160 // Promote additional arguments to variadic methods.
161 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000162 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
163 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000164 } else {
165 // Check for extra arguments to non-variadic methods.
166 if (NumArgs != NumNamedArgs) {
167 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000168 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000169 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000170 << SourceRange(Args[NumNamedArgs]->getLocStart(),
171 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000172 }
173 }
174
Chris Lattner85a932e2008-01-04 22:32:30 +0000175 return anyIncompatibleArgs;
176}
177
178// ActOnClassMessage - used for both unary and keyword messages.
179// ArgExprs is optional - if it is present, the number of expressions
180// is obtained from Sel.getNumArgs().
181Sema::ExprResult Sema::ActOnClassMessage(
182 Scope *S,
183 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000184 SourceLocation lbrac, SourceLocation receiverLoc,
185 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000186 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000187{
188 assert(receiverName && "missing receiver class name");
189
190 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000191 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000192 bool isSuper = false;
193
Chris Lattner84692652008-11-20 05:35:30 +0000194 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000195 if (getCurMethodDecl()) {
196 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000197 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
198 if (!OID)
199 return Diag(lbrac, diag::error_no_super_class_message)
200 << getCurMethodDecl()->getDeclName();
201 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000202 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000203 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000204 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000205 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
206 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000207 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
208 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000209 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000210 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
211 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000212 }
213 // We are sending a message to 'super' within a class method. Do nothing,
214 // the receiver will pass through as 'super' (how convenient:-).
215 } else {
216 // 'super' has been used outside a method context. If a variable named
217 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000218 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000219 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
220 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000221 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
222 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000223 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000224 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
225 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000226 }
Chris Lattner08631c52008-11-23 21:45:46 +0000227 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000228 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000229 } else
230 ClassDecl = getObjCInterfaceDecl(receiverName);
231
Steve Naroff7c778f12008-07-25 19:39:00 +0000232 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000233 //
234 // typedef XCElementDisplayRect XCElementGraphicsRect;
235 //
236 // @implementation XCRASlice
237 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
238 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
239 // }
240 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000241 // If necessary, the following lookup could move to getObjCInterfaceDecl().
242 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000243 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000244 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
245 const ObjCInterfaceType *OCIT;
246 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000247 if (!OCIT)
248 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
249 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000250 }
251 }
252 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000253 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000254 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000255 Method = ClassDecl->lookupClassMethod(Sel);
256
257 // If we have an implementation in scope, check "private" methods.
258 if (!Method) {
259 if (ObjCImplementationDecl *ImpDecl =
260 ObjCImplementations[ClassDecl->getIdentifier()])
261 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000262
263 // Look through local category implementations associated with the class.
264 if (!Method) {
265 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
266 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
267 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
268 }
269 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000270 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000271 // Before we give up, check if the selector is an instance method.
272 if (!Method)
273 Method = ClassDecl->lookupInstanceMethod(Sel);
274
Chris Lattner76a642f2009-02-15 22:43:40 +0000275 if (Method)
276 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000277
Chris Lattner077bf5e2008-11-24 03:33:13 +0000278 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000279 lbrac, rbrac, returnType))
280 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000281
282 // If we have the ObjCInterfaceDecl* for the class that is receiving
283 // the message, use that to construct the ObjCMessageExpr. Otherwise
284 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000285 // FIXME: need to do a better job handling 'super' usage within a class
286 // For now, we simply pass the "super" identifier through (which isn't
287 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000288 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000289 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
290 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000291 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000292 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
293 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000294}
295
296// ActOnInstanceMessage - used for both unary and keyword messages.
297// ArgExprs is optional - if it is present, the number of expressions
298// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000299Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000300 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000301 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000302 SourceLocation rbrac,
303 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000304 assert(receiver && "missing receiver expression");
305
306 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
307 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000308 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000309
Chris Lattnerb77792e2008-07-26 22:17:49 +0000310 QualType ReceiverCType =
311 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000312
313 // Handle messages to 'super'.
314 if (isa<ObjCSuperExpr>(RExpr)) {
315 ObjCMethodDecl *Method = 0;
316 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
317 // If we have an interface in scope, check 'super' methods.
318 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
319 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
320 Method = SuperDecl->lookupInstanceMethod(Sel);
321 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000322
Chris Lattner76a642f2009-02-15 22:43:40 +0000323 if (Method)
324 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000325
Chris Lattner077bf5e2008-11-24 03:33:13 +0000326 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000327 lbrac, rbrac, returnType))
328 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000329 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
330 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000331 }
332
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000333 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000334 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
335 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000336 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
337 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000338 if (!Method)
339 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000340 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000341 lbrac, rbrac, returnType))
342 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000343 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
344 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000345 }
346
347 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000348 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000349 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000350 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000351 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000352 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000353 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000354 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000355 Method = ImpDecl->getClassMethod(Sel);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000356
Chris Lattner76a642f2009-02-15 22:43:40 +0000357 if (Method)
358 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Steve Naroffe2af8b12008-06-05 14:49:39 +0000359 }
360 if (!Method)
361 Method = FactoryMethodPool[Sel].Method;
362 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000363 Method = LookupInstanceMethodInGlobalPool(
364 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000365 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000366 lbrac, rbrac, returnType))
367 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000368 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
369 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000370 }
371
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000372 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000373 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000374
375 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
376 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000377 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000378 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000379 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
380 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
381 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
382 break;
383 }
384 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000385 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000386 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000387 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000388 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000389 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000390
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000391 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000392 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
393 // faster than the following method (which can do *many* linear searches).
394 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000395 Method = ClassDecl->lookupInstanceMethod(Sel);
396
397 if (!Method) {
398 // Search protocol qualifiers.
399 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
400 E = OCIReceiver->qual_end(); QI != E; ++QI) {
401 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000402 break;
403 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000404 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000405
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000406 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000407 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000408 << Sel << SourceRange(lbrac, rbrac);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000409
Chris Lattner76a642f2009-02-15 22:43:40 +0000410 if (Method)
411 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000412 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000413 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000414 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000415 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000416 }
417
418 if (!Method) {
419 // If we have an implementation in scope, check "private" methods.
420 if (ClassDecl)
421 if (ObjCImplementationDecl *ImpDecl =
422 ObjCImplementations[ClassDecl->getIdentifier()])
423 Method = ImpDecl->getInstanceMethod(Sel);
424 // If we still haven't found a method, look in the global pool. This
425 // behavior isn't very desirable, however we need it for GCC
426 // compatibility.
427 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000428 Method = LookupInstanceMethodInGlobalPool(
429 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000430 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000431 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000432 lbrac, rbrac, returnType))
433 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000434 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
435 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000436}
Chris Lattnereca7be62008-04-07 05:30:13 +0000437
438//===----------------------------------------------------------------------===//
439// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
440//===----------------------------------------------------------------------===//
441
442/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
443/// inheritance hierarchy of 'rProto'.
444static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
445 ObjCProtocolDecl *rProto) {
446 if (lProto == rProto)
447 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000448 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
449 E = rProto->protocol_end(); PI != E; ++PI)
450 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000451 return true;
452 return false;
453}
454
455/// ClassImplementsProtocol - Checks that 'lProto' protocol
456/// has been implemented in IDecl class, its super class or categories (if
457/// lookupCategory is true).
458static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
459 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000460 bool lookupCategory,
461 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000462
463 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000464 const ObjCList<ObjCProtocolDecl> &Protocols =
465 IDecl->getReferencedProtocols();
466
467 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
468 E = Protocols.end(); PI != E; ++PI) {
469 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000470 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000471 // This is dubious and is added to be compatible with gcc.
472 // In gcc, it is also allowed assigning a protocol-qualified 'id'
473 // type to a LHS object when protocol in qualified LHS is in list
474 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000475 // FIXME: Treat this as an extension, and flag this as an error when
476 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000477 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000478 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000479 }
480
481 // 2nd, look up the category.
482 if (lookupCategory)
483 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
484 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000485 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
486 E = CDecl->protocol_end(); PI != E; ++PI)
487 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000488 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000489 }
490
491 // 3rd, look up the super class(s)
492 if (IDecl->getSuperClass())
493 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000494 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
495 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000496
497 return false;
498}
499
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000500/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
501/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000502bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
503 bool compare) {
504 // Allow id<P..> and an 'id' or void* type in all cases.
505 if (const PointerType *PT = lhs->getAsPointerType()) {
506 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000507 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000508 return true;
509 } else if (const PointerType *PT = rhs->getAsPointerType()) {
510 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000511 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000512 return true;
513 }
514
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000515 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
516 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
517 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000518 QualType rtype;
519
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000520 if (!rhsQID) {
521 // Not comparing two ObjCQualifiedIdType's?
522 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000523
524 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000525 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000526 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000527 // If the RHS is a unqualified interface pointer "NSString*",
528 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000529 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
530 ObjCInterfaceDecl *rhsID = IT->getDecl();
531 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
532 // when comparing an id<P> on lhs with a static type on rhs,
533 // see if static class implements all of id's protocols, directly or
534 // through its super class and categories.
535 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
536 return false;
537 }
538 return true;
539 }
540 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000541 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000542
543 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000544 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000545 RHSProtoI = rhsQI->qual_begin();
546 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000547 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000548 RHSProtoI = rhsQID->qual_begin();
549 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000550 } else {
551 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000552 }
553
554 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
555 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
556 bool match = false;
557
558 // when comparing an id<P> on lhs with a static type on rhs,
559 // see if static class implements all of id's protocols, directly or
560 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000561 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
562 ObjCProtocolDecl *rhsProto = *RHSProtoI;
563 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000564 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000565 match = true;
566 break;
567 }
568 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000569 if (rhsQI) {
570 // If the RHS is a qualified interface pointer "NSString<P>*",
571 // make sure we check the class hierarchy.
572 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
573 ObjCInterfaceDecl *rhsID = IT->getDecl();
574 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
575 // when comparing an id<P> on lhs with a static type on rhs,
576 // see if static class implements all of id's protocols, directly or
577 // through its super class and categories.
578 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
579 match = true;
580 break;
581 }
582 }
583 }
584 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000585 if (!match)
586 return false;
587 }
588
589 return true;
590 }
591
592 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
593 assert(rhsQID && "One of the LHS/RHS should be id<x>");
594
595 if (!lhs->isPointerType())
596 return false;
597
598 QualType ltype = lhs->getAsPointerType()->getPointeeType();
599 if (const ObjCQualifiedInterfaceType *lhsQI =
600 ltype->getAsObjCQualifiedInterfaceType()) {
601 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
602 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
603 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
604 bool match = false;
605 ObjCProtocolDecl *lhsProto = *LHSProtoI;
606 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
607 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
608 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000609 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000610 match = true;
611 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000612 }
613 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000614 if (!match)
615 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000616 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000617 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000618 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000619
620 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
621 // for static type vs. qualified 'id' type, check that class implements
622 // all of 'id's protocols.
623 ObjCInterfaceDecl *lhsID = IT->getDecl();
624 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
625 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000626 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000627 return false;
628 }
629 return true;
630 }
631 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000632}
633