blob: 91e7a185b0535bb3be362715172632489f12e0ce [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();
32 char *strBuf = new char [Length];
33 char *p = strBuf;
34 bool isWide = false;
35 for (unsigned i = 0; i < NumStrings; i++) {
36 S = static_cast<StringLiteral *>(Strings[i]);
37 if (S->isWide())
38 isWide = true;
39 memcpy(p, S->getStrData(), S->getByteLength());
40 p += S->getByteLength();
Ted Kremenek8189cde2009-02-07 01:47:29 +000041 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000042 }
Ted Kremenek6e94ef52009-02-06 19:55:15 +000043 S = new (Context, 8) StringLiteral(Context, strBuf, Length, isWide,
44 Context.getPointerType(Context.CharTy),
45 AtLoc, EndLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000046 }
47
48 if (CheckBuiltinCFStringArgument(S))
49 return true;
50
Ted Kremeneka526c5c2008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-01-04 22:32:30 +000052 // Initialize the constant string interface lazily. This assumes
53 // the NSConstantString interface is seen in this translation unit.
54 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Douglas Gregor47b9a1c2009-02-04 17:27:36 +000055 NamedDecl *IFace = LookupName(TUScope, NSIdent, LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +000056 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner13fd7e52008-06-21 21:44:18 +000057 if (strIFace)
58 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000059 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000060 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner13fd7e52008-06-21 21:44:18 +000061 // If there is no NSConstantString interface defined then treat constant
62 // strings as untyped objects and let the runtime figure it out later.
63 if (t == QualType()) {
64 t = Context.getObjCIdType();
65 } else {
66 t = Context.getPointerType(t);
67 }
Ted Kremenek8189cde2009-02-07 01:47:29 +000068 return new (Context) ObjCStringLiteral(S, t, AtLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000069}
70
71Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
72 SourceLocation EncodeLoc,
73 SourceLocation LParenLoc,
74 TypeTy *Ty,
75 SourceLocation RParenLoc) {
76 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
77
78 QualType t = Context.getPointerType(Context.CharTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +000079 return new (Context) ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000080}
81
82Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
83 SourceLocation AtLoc,
84 SourceLocation SelLoc,
85 SourceLocation LParenLoc,
86 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000087 QualType t = Context.getObjCSelType();
Ted Kremenek8189cde2009-02-07 01:47:29 +000088 return new (Context) ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000089}
90
91Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
92 SourceLocation AtLoc,
93 SourceLocation ProtoLoc,
94 SourceLocation LParenLoc,
95 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +000096 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000097 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +000098 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +000099 return true;
100 }
101
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000102 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000103 if (t.isNull())
104 return true;
105 t = Context.getPointerType(t);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000106 return new (Context) ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000107}
108
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000109bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
110 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000111 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000112 SourceLocation lbrac, SourceLocation rbrac,
113 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000114 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000115 // Apply default argument promotion as for (C99 6.5.2.2p6).
116 for (unsigned i = 0; i != NumArgs; i++)
117 DefaultArgumentPromotion(Args[i]);
118
Chris Lattner077bf5e2008-11-24 03:33:13 +0000119 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
120 diag::warn_inst_method_not_found;
121 Diag(lbrac, DiagID)
122 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000123 ReturnType = Context.getObjCIdType();
124 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000125 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000126
127 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000128
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000129 unsigned NumNamedArgs = Sel.getNumArgs();
130 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
131
Chris Lattner85a932e2008-01-04 22:32:30 +0000132 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000133 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000134 Expr *argExpr = Args[i];
135 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
136
137 QualType lhsType = Method->getParamDecl(i)->getType();
138 QualType rhsType = argExpr->getType();
139
140 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000141 if (lhsType->isArrayType())
142 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000143 else if (lhsType->isFunctionType())
144 lhsType = Context.getPointerType(lhsType);
145
Chris Lattner987798a2008-04-02 17:17:33 +0000146 AssignConvertType Result =
147 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000148 if (Args[i] != argExpr) // The expression was converted.
149 Args[i] = argExpr; // Make sure we store the converted expression.
150
151 anyIncompatibleArgs |=
152 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
153 argExpr, "sending");
154 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000155
156 // Promote additional arguments to variadic methods.
157 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000158 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
159 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000160 } else {
161 // Check for extra arguments to non-variadic methods.
162 if (NumArgs != NumNamedArgs) {
163 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000164 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000165 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000166 << SourceRange(Args[NumNamedArgs]->getLocStart(),
167 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000168 }
169 }
170
Chris Lattner85a932e2008-01-04 22:32:30 +0000171 return anyIncompatibleArgs;
172}
173
174// ActOnClassMessage - used for both unary and keyword messages.
175// ArgExprs is optional - if it is present, the number of expressions
176// is obtained from Sel.getNumArgs().
177Sema::ExprResult Sema::ActOnClassMessage(
178 Scope *S,
179 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000180 SourceLocation lbrac, SourceLocation receiverLoc,
181 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000182 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000183{
184 assert(receiverName && "missing receiver class name");
185
186 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000187 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000188 bool isSuper = false;
189
Chris Lattner84692652008-11-20 05:35:30 +0000190 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000191 if (getCurMethodDecl()) {
192 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000193 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
194 if (!OID)
195 return Diag(lbrac, diag::error_no_super_class_message)
196 << getCurMethodDecl()->getDeclName();
197 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000198 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000199 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000200 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000201 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
202 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000203 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
204 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000205 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000206 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
207 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000208 }
209 // We are sending a message to 'super' within a class method. Do nothing,
210 // the receiver will pass through as 'super' (how convenient:-).
211 } else {
212 // 'super' has been used outside a method context. If a variable named
213 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000214 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000215 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
216 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000217 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
218 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000219 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000220 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
221 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000222 }
Chris Lattner08631c52008-11-23 21:45:46 +0000223 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000224 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000225 } else
226 ClassDecl = getObjCInterfaceDecl(receiverName);
227
Steve Naroff7c778f12008-07-25 19:39:00 +0000228 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000229 //
230 // typedef XCElementDisplayRect XCElementGraphicsRect;
231 //
232 // @implementation XCRASlice
233 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
234 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
235 // }
236 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000237 // If necessary, the following lookup could move to getObjCInterfaceDecl().
238 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000239 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000240 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
241 const ObjCInterfaceType *OCIT;
242 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000243 if (!OCIT)
244 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
245 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000246 }
247 }
248 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000249 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000250 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000251 Method = ClassDecl->lookupClassMethod(Sel);
252
253 // If we have an implementation in scope, check "private" methods.
254 if (!Method) {
255 if (ObjCImplementationDecl *ImpDecl =
256 ObjCImplementations[ClassDecl->getIdentifier()])
257 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000258
259 // Look through local category implementations associated with the class.
260 if (!Method) {
261 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
262 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
263 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
264 }
265 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000266 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000267 // Before we give up, check if the selector is an instance method.
268 if (!Method)
269 Method = ClassDecl->lookupInstanceMethod(Sel);
270
Anders Carlsson59843ad2009-02-14 19:08:58 +0000271 if (Method && Method->getAttr<DeprecatedAttr>())
272 Diag(receiverLoc, diag::warn_deprecated) << Method->getDeclName();
273
Chris Lattner077bf5e2008-11-24 03:33:13 +0000274 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000275 lbrac, rbrac, returnType))
276 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000277
278 // If we have the ObjCInterfaceDecl* for the class that is receiving
279 // the message, use that to construct the ObjCMessageExpr. Otherwise
280 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000281 // FIXME: need to do a better job handling 'super' usage within a class
282 // For now, we simply pass the "super" identifier through (which isn't
283 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000284 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000285 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
286 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000287 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000288 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
289 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000290}
291
292// ActOnInstanceMessage - used for both unary and keyword messages.
293// ArgExprs is optional - if it is present, the number of expressions
294// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000295Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000296 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000297 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000298 SourceLocation rbrac,
299 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000300 assert(receiver && "missing receiver expression");
301
302 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
303 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000304 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000305
Chris Lattnerb77792e2008-07-26 22:17:49 +0000306 QualType ReceiverCType =
307 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000308
309 // Handle messages to 'super'.
310 if (isa<ObjCSuperExpr>(RExpr)) {
311 ObjCMethodDecl *Method = 0;
312 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
313 // If we have an interface in scope, check 'super' methods.
314 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
315 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
316 Method = SuperDecl->lookupInstanceMethod(Sel);
317 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000318
Anders Carlsson59843ad2009-02-14 19:08:58 +0000319 if (Method && Method->getAttr<DeprecatedAttr>())
320 Diag(receiverLoc, diag::warn_deprecated) << Method->getDeclName();
321
Chris Lattner077bf5e2008-11-24 03:33:13 +0000322 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000323 lbrac, rbrac, returnType))
324 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000325 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
326 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000327 }
328
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000329 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000330 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
331 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000332 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
333 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000334 if (!Method)
335 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000336 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000337 lbrac, rbrac, returnType))
338 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000339 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
340 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000341 }
342
343 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000344 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000345 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000346 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000347 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000348 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000349 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000350 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000351 Method = ImpDecl->getClassMethod(Sel);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000352
353 if (Method && Method->getAttr<DeprecatedAttr>())
354 Diag(receiverLoc, diag::warn_deprecated) << Method->getDeclName();
Steve Naroffe2af8b12008-06-05 14:49:39 +0000355 }
356 if (!Method)
357 Method = FactoryMethodPool[Sel].Method;
358 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000359 Method = LookupInstanceMethodInGlobalPool(
360 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000361 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000362 lbrac, rbrac, returnType))
363 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000364 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
365 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000366 }
367
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000368 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000369 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000370
371 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
372 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000373 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000374 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000375 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
376 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
377 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
378 break;
379 }
380 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000381 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000382 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000383 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000384 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000385 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000386
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000387 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000388 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
389 // faster than the following method (which can do *many* linear searches).
390 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000391 Method = ClassDecl->lookupInstanceMethod(Sel);
392
393 if (!Method) {
394 // Search protocol qualifiers.
395 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
396 E = OCIReceiver->qual_end(); QI != E; ++QI) {
397 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000398 break;
399 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000400 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000401
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000402 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000403 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000404 << Sel << SourceRange(lbrac, rbrac);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000405
406 if (Method && Method->getAttr<DeprecatedAttr>())
407 Diag(receiverLoc, diag::warn_deprecated) << Method->getDeclName();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000408 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000409 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000410 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000411 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000412 }
413
414 if (!Method) {
415 // If we have an implementation in scope, check "private" methods.
416 if (ClassDecl)
417 if (ObjCImplementationDecl *ImpDecl =
418 ObjCImplementations[ClassDecl->getIdentifier()])
419 Method = ImpDecl->getInstanceMethod(Sel);
420 // If we still haven't found a method, look in the global pool. This
421 // behavior isn't very desirable, however we need it for GCC
422 // compatibility.
423 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000424 Method = LookupInstanceMethodInGlobalPool(
425 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000426 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000427 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000428 lbrac, rbrac, returnType))
429 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000430 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
431 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000432}
Chris Lattnereca7be62008-04-07 05:30:13 +0000433
434//===----------------------------------------------------------------------===//
435// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
436//===----------------------------------------------------------------------===//
437
438/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
439/// inheritance hierarchy of 'rProto'.
440static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
441 ObjCProtocolDecl *rProto) {
442 if (lProto == rProto)
443 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000444 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
445 E = rProto->protocol_end(); PI != E; ++PI)
446 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000447 return true;
448 return false;
449}
450
451/// ClassImplementsProtocol - Checks that 'lProto' protocol
452/// has been implemented in IDecl class, its super class or categories (if
453/// lookupCategory is true).
454static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
455 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000456 bool lookupCategory,
457 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000458
459 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000460 const ObjCList<ObjCProtocolDecl> &Protocols =
461 IDecl->getReferencedProtocols();
462
463 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
464 E = Protocols.end(); PI != E; ++PI) {
465 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000466 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000467 // This is dubious and is added to be compatible with gcc.
468 // In gcc, it is also allowed assigning a protocol-qualified 'id'
469 // type to a LHS object when protocol in qualified LHS is in list
470 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000471 // FIXME: Treat this as an extension, and flag this as an error when
472 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000473 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000474 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000475 }
476
477 // 2nd, look up the category.
478 if (lookupCategory)
479 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
480 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000481 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
482 E = CDecl->protocol_end(); PI != E; ++PI)
483 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000484 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000485 }
486
487 // 3rd, look up the super class(s)
488 if (IDecl->getSuperClass())
489 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000490 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
491 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000492
493 return false;
494}
495
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000496/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
497/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000498bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
499 bool compare) {
500 // Allow id<P..> and an 'id' or void* type in all cases.
501 if (const PointerType *PT = lhs->getAsPointerType()) {
502 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000503 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000504 return true;
505 } else if (const PointerType *PT = rhs->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 }
510
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000511 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
512 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
513 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000514 QualType rtype;
515
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000516 if (!rhsQID) {
517 // Not comparing two ObjCQualifiedIdType's?
518 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000519
520 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000521 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000522 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000523 // If the RHS is a unqualified interface pointer "NSString*",
524 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000525 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
526 ObjCInterfaceDecl *rhsID = IT->getDecl();
527 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
528 // when comparing an id<P> on lhs with a static type on rhs,
529 // see if static class implements all of id's protocols, directly or
530 // through its super class and categories.
531 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
532 return false;
533 }
534 return true;
535 }
536 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000537 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000538
539 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000540 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000541 RHSProtoI = rhsQI->qual_begin();
542 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000543 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000544 RHSProtoI = rhsQID->qual_begin();
545 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000546 } else {
547 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000548 }
549
550 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
551 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
552 bool match = false;
553
554 // when comparing an id<P> on lhs with a static type on rhs,
555 // see if static class implements all of id's protocols, directly or
556 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000557 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
558 ObjCProtocolDecl *rhsProto = *RHSProtoI;
559 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000560 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000561 match = true;
562 break;
563 }
564 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000565 if (rhsQI) {
566 // If the RHS is a qualified interface pointer "NSString<P>*",
567 // make sure we check the class hierarchy.
568 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
569 ObjCInterfaceDecl *rhsID = IT->getDecl();
570 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
571 // when comparing an id<P> on lhs with a static type on rhs,
572 // see if static class implements all of id's protocols, directly or
573 // through its super class and categories.
574 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
575 match = true;
576 break;
577 }
578 }
579 }
580 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000581 if (!match)
582 return false;
583 }
584
585 return true;
586 }
587
588 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
589 assert(rhsQID && "One of the LHS/RHS should be id<x>");
590
591 if (!lhs->isPointerType())
592 return false;
593
594 QualType ltype = lhs->getAsPointerType()->getPointeeType();
595 if (const ObjCQualifiedInterfaceType *lhsQI =
596 ltype->getAsObjCQualifiedInterfaceType()) {
597 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
598 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
599 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
600 bool match = false;
601 ObjCProtocolDecl *lhsProto = *LHSProtoI;
602 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
603 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
604 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000605 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000606 match = true;
607 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000608 }
609 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000610 if (!match)
611 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000612 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000613 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000614 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000615
616 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
617 // for static type vs. qualified 'id' type, check that class implements
618 // all of 'id's protocols.
619 ObjCInterfaceDecl *lhsID = IT->getDecl();
620 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
621 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000622 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000623 return false;
624 }
625 return true;
626 }
627 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000628}
629