blob: 6e40b3c4a3e3157c8929f1bd73a0c149300e03b1 [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) {
Chris Lattnerf4b136f2009-02-18 06:13:04 +000023 // Most ObjC strings are formed out of a single piece. However, we *can*
24 // have strings formed out of multiple @ strings with multiple pptokens in
25 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
26 // StringLiteral for ObjCStringLiteral to hold onto.
27 StringLiteral *S = static_cast<StringLiteral *>(Strings[0]);
28
29 // If we have a multi-part string, merge it all together.
30 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000031 // Concatenate objc strings.
32 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
33 SourceLocation EndLoc = ES->getSourceRange().getEnd();
34 unsigned Length = 0;
35 for (unsigned i = 0; i < NumStrings; i++)
36 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
Chris Lattner726e1682009-02-18 05:49:11 +000037
38 // FIXME: This should not be allocated by SEMA!
39 char *strBuf = new char[Length];
Chris Lattner85a932e2008-01-04 22:32:30 +000040 char *p = strBuf;
Chris Lattner726e1682009-02-18 05:49:11 +000041 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner85a932e2008-01-04 22:32:30 +000042 S = static_cast<StringLiteral *>(Strings[i]);
Chris Lattnerf4b136f2009-02-18 06:13:04 +000043 if (S->isWide()) {
44 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
45 << S->getSourceRange();
46 return true;
47 }
48
Chris Lattner85a932e2008-01-04 22:32:30 +000049 memcpy(p, S->getStrData(), S->getByteLength());
50 p += S->getByteLength();
Ted Kremenek8189cde2009-02-07 01:47:29 +000051 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000052 }
Chris Lattner726e1682009-02-18 05:49:11 +000053 // FIXME: PASS LOCATIONS PROPERLY.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000054 S = new (Context) StringLiteral(Context, strBuf, Length, false,
Chris Lattner726e1682009-02-18 05:49:11 +000055 Context.getPointerType(Context.CharTy),
Chris Lattnerf4b136f2009-02-18 06:13:04 +000056 AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000057 }
58
Chris Lattner69039812009-02-18 06:01:06 +000059 // Verify that this composite string is acceptable for ObjC strings.
60 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000061 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000062
63 // Initialize the constant string interface lazily. This assumes
64 // the NSConstantString interface is seen in this translation unit.
65 QualType Ty = Context.getObjCConstantStringInterface();
66 if (!Ty.isNull()) {
67 Ty = Context.getPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000068 } else {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000069 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
70 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
71 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
72 Context.setObjCConstantStringInterface(StrIF);
73 Ty = Context.getObjCConstantStringInterface();
74 Ty = Context.getPointerType(Ty);
75 } else {
76 // If there is no NSConstantString interface defined then treat constant
77 // strings as untyped objects and let the runtime figure it out later.
78 Ty = Context.getObjCIdType();
79 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000080 }
Chris Lattnera0af1fe2009-02-18 06:06:56 +000081
Chris Lattnerf4b136f2009-02-18 06:13:04 +000082 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000083}
84
85Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
86 SourceLocation EncodeLoc,
87 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +000088 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +000089 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +000090 QualType EncodedType = QualType::getFromOpaquePtr(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +000091
Chris Lattnera0af1fe2009-02-18 06:06:56 +000092 QualType Ty = Context.getPointerType(Context.CharTy);
93 return new (Context) ObjCEncodeExpr(Ty, EncodedType, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +000094}
95
96Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
97 SourceLocation AtLoc,
98 SourceLocation SelLoc,
99 SourceLocation LParenLoc,
100 SourceLocation RParenLoc) {
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000101 QualType Ty = Context.getObjCSelType();
102 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000103}
104
105Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
106 SourceLocation AtLoc,
107 SourceLocation ProtoLoc,
108 SourceLocation LParenLoc,
109 SourceLocation RParenLoc) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000110 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +0000111 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000112 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000113 return true;
114 }
115
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000116 QualType Ty = Context.getObjCProtoType();
117 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000118 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000119 Ty = Context.getPointerType(Ty);
120 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000121}
122
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000123bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
124 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000125 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000126 SourceLocation lbrac, SourceLocation rbrac,
127 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000128 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000129 // Apply default argument promotion as for (C99 6.5.2.2p6).
130 for (unsigned i = 0; i != NumArgs; i++)
131 DefaultArgumentPromotion(Args[i]);
132
Chris Lattner077bf5e2008-11-24 03:33:13 +0000133 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
134 diag::warn_inst_method_not_found;
135 Diag(lbrac, DiagID)
136 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000137 ReturnType = Context.getObjCIdType();
138 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000139 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000140
141 ReturnType = Method->getResultType();
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000142
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000143 unsigned NumNamedArgs = Sel.getNumArgs();
144 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
145
Chris Lattner85a932e2008-01-04 22:32:30 +0000146 bool anyIncompatibleArgs = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000147 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000148 Expr *argExpr = Args[i];
149 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
150
151 QualType lhsType = Method->getParamDecl(i)->getType();
152 QualType rhsType = argExpr->getType();
153
154 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000155 if (lhsType->isArrayType())
156 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000157 else if (lhsType->isFunctionType())
158 lhsType = Context.getPointerType(lhsType);
159
Chris Lattner987798a2008-04-02 17:17:33 +0000160 AssignConvertType Result =
161 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000162 if (Args[i] != argExpr) // The expression was converted.
163 Args[i] = argExpr; // Make sure we store the converted expression.
164
165 anyIncompatibleArgs |=
166 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
167 argExpr, "sending");
168 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000169
170 // Promote additional arguments to variadic methods.
171 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000172 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
173 DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000174 } else {
175 // Check for extra arguments to non-variadic methods.
176 if (NumArgs != NumNamedArgs) {
177 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000178 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000179 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000180 << SourceRange(Args[NumNamedArgs]->getLocStart(),
181 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000182 }
183 }
184
Chris Lattner85a932e2008-01-04 22:32:30 +0000185 return anyIncompatibleArgs;
186}
187
188// ActOnClassMessage - used for both unary and keyword messages.
189// ArgExprs is optional - if it is present, the number of expressions
190// is obtained from Sel.getNumArgs().
191Sema::ExprResult Sema::ActOnClassMessage(
192 Scope *S,
193 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000194 SourceLocation lbrac, SourceLocation receiverLoc,
195 SourceLocation selectorLoc, SourceLocation rbrac,
Steve Naroff5cb93b82008-11-19 15:54:23 +0000196 ExprTy **Args, unsigned NumArgs)
Chris Lattner85a932e2008-01-04 22:32:30 +0000197{
198 assert(receiverName && "missing receiver class name");
199
200 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000201 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000202 bool isSuper = false;
203
Chris Lattner84692652008-11-20 05:35:30 +0000204 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000205 if (getCurMethodDecl()) {
206 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000207 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
208 if (!OID)
209 return Diag(lbrac, diag::error_no_super_class_message)
210 << getCurMethodDecl()->getDeclName();
211 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000212 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000213 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000214 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000215 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
216 superTy = Context.getPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000217 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
218 superTy);
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 }
223 // We are sending a message to 'super' within a class method. Do nothing,
224 // the receiver will pass through as 'super' (how convenient:-).
225 } else {
226 // 'super' has been used outside a method context. If a variable named
227 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000228 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000229 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
230 if (VD) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000231 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
232 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000233 // We are really in an instance method, redirect.
Anders Carlssonff975cf2009-02-14 18:21:46 +0000234 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
235 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000236 }
Chris Lattner08631c52008-11-23 21:45:46 +0000237 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Steve Naroff5cb93b82008-11-19 15:54:23 +0000238 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000239 } else
240 ClassDecl = getObjCInterfaceDecl(receiverName);
241
Steve Naroff7c778f12008-07-25 19:39:00 +0000242 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000243 //
244 // typedef XCElementDisplayRect XCElementGraphicsRect;
245 //
246 // @implementation XCRASlice
247 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
248 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
249 // }
250 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000251 // If necessary, the following lookup could move to getObjCInterfaceDecl().
252 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000253 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000254 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
255 const ObjCInterfaceType *OCIT;
256 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000257 if (!OCIT)
258 return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
259 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000260 }
261 }
262 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000263 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000264 QualType returnType;
Steve Naroff7c778f12008-07-25 19:39:00 +0000265 Method = ClassDecl->lookupClassMethod(Sel);
266
267 // If we have an implementation in scope, check "private" methods.
268 if (!Method) {
269 if (ObjCImplementationDecl *ImpDecl =
270 ObjCImplementations[ClassDecl->getIdentifier()])
271 Method = ImpDecl->getClassMethod(Sel);
Steve Naroffe84a8642008-09-28 14:55:53 +0000272
273 // Look through local category implementations associated with the class.
274 if (!Method) {
275 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
276 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
277 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
278 }
279 }
Steve Naroff9ad23d62008-06-04 14:43:54 +0000280 }
Steve Naroff7c778f12008-07-25 19:39:00 +0000281 // Before we give up, check if the selector is an instance method.
282 if (!Method)
283 Method = ClassDecl->lookupInstanceMethod(Sel);
284
Chris Lattner76a642f2009-02-15 22:43:40 +0000285 if (Method)
286 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000287
Chris Lattner077bf5e2008-11-24 03:33:13 +0000288 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000289 lbrac, rbrac, returnType))
290 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000291
292 // If we have the ObjCInterfaceDecl* for the class that is receiving
293 // the message, use that to construct the ObjCMessageExpr. Otherwise
294 // pass on the IdentifierInfo* for the class.
Steve Narofffc93d522008-07-24 19:44:33 +0000295 // FIXME: need to do a better job handling 'super' usage within a class
296 // For now, we simply pass the "super" identifier through (which isn't
297 // consistent with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000298 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000299 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
300 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000301 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000302 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
303 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000304}
305
306// ActOnInstanceMessage - used for both unary and keyword messages.
307// ArgExprs is optional - if it is present, the number of expressions
308// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000309Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000310 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000311 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000312 SourceLocation rbrac,
313 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000314 assert(receiver && "missing receiver expression");
315
316 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
317 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner85a932e2008-01-04 22:32:30 +0000318 QualType returnType;
Steve Naroff94a82c92008-05-31 02:19:15 +0000319
Chris Lattnerb77792e2008-07-26 22:17:49 +0000320 QualType ReceiverCType =
321 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000322
323 // Handle messages to 'super'.
324 if (isa<ObjCSuperExpr>(RExpr)) {
325 ObjCMethodDecl *Method = 0;
326 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
327 // If we have an interface in scope, check 'super' methods.
328 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
329 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
330 Method = SuperDecl->lookupInstanceMethod(Sel);
331 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000332
Chris Lattner76a642f2009-02-15 22:43:40 +0000333 if (Method)
334 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000335
Chris Lattner077bf5e2008-11-24 03:33:13 +0000336 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +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);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000341 }
342
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000343 // Handle messages to id.
Steve Naroff6c4088e2008-09-29 16:51:41 +0000344 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
345 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff037cda52008-09-30 14:38:43 +0000346 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
347 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000348 if (!Method)
349 Method = FactoryMethodPool[Sel].Method;
Chris Lattner077bf5e2008-11-24 03:33:13 +0000350 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000351 lbrac, rbrac, returnType))
352 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000353 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
354 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000355 }
356
357 // Handle messages to Class.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000358 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000359 ObjCMethodDecl *Method = 0;
Chris Lattner6562fda2008-07-21 06:44:27 +0000360 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffe2af8b12008-06-05 14:49:39 +0000361 // If we have an implementation in scope, check "private" methods.
Chris Lattner6562fda2008-07-21 06:44:27 +0000362 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroffe2af8b12008-06-05 14:49:39 +0000363 if (ObjCImplementationDecl *ImpDecl =
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000364 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroffe2af8b12008-06-05 14:49:39 +0000365 Method = ImpDecl->getClassMethod(Sel);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000366
Chris Lattner76a642f2009-02-15 22:43:40 +0000367 if (Method)
368 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Steve Naroffe2af8b12008-06-05 14:49:39 +0000369 }
370 if (!Method)
371 Method = FactoryMethodPool[Sel].Method;
372 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000373 Method = LookupInstanceMethodInGlobalPool(
374 Sel, SourceRange(lbrac,rbrac));
Chris Lattner077bf5e2008-11-24 03:33:13 +0000375 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000376 lbrac, rbrac, returnType))
377 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000378 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
379 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000380 }
381
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000382 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000383 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000384
385 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
386 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000387 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000388 // Search protocols
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000389 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
390 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
391 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
392 break;
393 }
394 if (!Method)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000395 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000396 << Sel << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000397 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerb77792e2008-07-26 22:17:49 +0000398 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000399 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000400
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000401 ClassDecl = OCIReceiver->getDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000402 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
403 // faster than the following method (which can do *many* linear searches).
404 // The idea is to add class info to InstanceMethodPool.
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000405 Method = ClassDecl->lookupInstanceMethod(Sel);
406
407 if (!Method) {
408 // Search protocol qualifiers.
409 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
410 E = OCIReceiver->qual_end(); QI != E; ++QI) {
411 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000412 break;
413 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000414 }
Chris Lattnerc188d302008-07-21 05:27:51 +0000415
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000416 if (!Method && !OCIReceiver->qual_empty())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000417 Diag(lbrac, diag::warn_method_not_found_in_protocol)
Chris Lattner077bf5e2008-11-24 03:33:13 +0000418 << Sel << SourceRange(lbrac, rbrac);
Anders Carlsson59843ad2009-02-14 19:08:58 +0000419
Chris Lattner76a642f2009-02-15 22:43:40 +0000420 if (Method)
421 DiagnoseUseOfDeprecatedDecl(Method, receiverLoc);
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000422 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000423 Diag(lbrac, diag::error_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000424 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000425 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000426 }
427
428 if (!Method) {
429 // If we have an implementation in scope, check "private" methods.
430 if (ClassDecl)
431 if (ObjCImplementationDecl *ImpDecl =
432 ObjCImplementations[ClassDecl->getIdentifier()])
433 Method = ImpDecl->getInstanceMethod(Sel);
434 // If we still haven't found a method, look in the global pool. This
435 // behavior isn't very desirable, however we need it for GCC
436 // compatibility.
437 if (!Method)
Steve Naroff037cda52008-09-30 14:38:43 +0000438 Method = LookupInstanceMethodInGlobalPool(
439 Sel, SourceRange(lbrac,rbrac));
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000440 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000441 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000442 lbrac, rbrac, returnType))
443 return true;
Ted Kremenek8189cde2009-02-07 01:47:29 +0000444 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
445 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000446}
Chris Lattnereca7be62008-04-07 05:30:13 +0000447
448//===----------------------------------------------------------------------===//
449// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
450//===----------------------------------------------------------------------===//
451
452/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
453/// inheritance hierarchy of 'rProto'.
454static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
455 ObjCProtocolDecl *rProto) {
456 if (lProto == rProto)
457 return true;
Chris Lattner780f3292008-07-21 21:32:27 +0000458 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
459 E = rProto->protocol_end(); PI != E; ++PI)
460 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000461 return true;
462 return false;
463}
464
465/// ClassImplementsProtocol - Checks that 'lProto' protocol
466/// has been implemented in IDecl class, its super class or categories (if
467/// lookupCategory is true).
468static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
469 ObjCInterfaceDecl *IDecl,
Fariborz Jahanian26631702008-06-04 19:00:03 +0000470 bool lookupCategory,
471 bool RHSIsQualifiedID = false) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000472
473 // 1st, look up the class.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000474 const ObjCList<ObjCProtocolDecl> &Protocols =
475 IDecl->getReferencedProtocols();
476
477 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
478 E = Protocols.end(); PI != E; ++PI) {
479 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000480 return true;
Fariborz Jahanian26631702008-06-04 19:00:03 +0000481 // This is dubious and is added to be compatible with gcc.
482 // In gcc, it is also allowed assigning a protocol-qualified 'id'
483 // type to a LHS object when protocol in qualified LHS is in list
484 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenekfd5b2ce2008-06-04 20:48:08 +0000485 // FIXME: Treat this as an extension, and flag this as an error when
486 // GCC extensions are not enabled.
Chris Lattner3db6cae2008-07-21 18:19:38 +0000487 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahanian26631702008-06-04 19:00:03 +0000488 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000489 }
490
491 // 2nd, look up the category.
492 if (lookupCategory)
493 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
494 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner780f3292008-07-21 21:32:27 +0000495 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
496 E = CDecl->protocol_end(); PI != E; ++PI)
497 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnereca7be62008-04-07 05:30:13 +0000498 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000499 }
500
501 // 3rd, look up the super class(s)
502 if (IDecl->getSuperClass())
503 return
Fariborz Jahanian26631702008-06-04 19:00:03 +0000504 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
505 RHSIsQualifiedID);
Chris Lattnereca7be62008-04-07 05:30:13 +0000506
507 return false;
508}
509
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000510/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
511/// ObjCQualifiedIDType.
Chris Lattnereca7be62008-04-07 05:30:13 +0000512bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
513 bool compare) {
514 // Allow id<P..> and an 'id' or void* type in all cases.
515 if (const PointerType *PT = lhs->getAsPointerType()) {
516 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000517 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000518 return true;
519 } else if (const PointerType *PT = rhs->getAsPointerType()) {
520 QualType PointeeTy = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +0000521 if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
Chris Lattnereca7be62008-04-07 05:30:13 +0000522 return true;
523 }
524
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000525 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
526 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
527 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroff289d9f22008-06-01 02:43:50 +0000528 QualType rtype;
529
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000530 if (!rhsQID) {
531 // Not comparing two ObjCQualifiedIdType's?
532 if (!rhs->isPointerType()) return false;
Steve Naroff289d9f22008-06-01 02:43:50 +0000533
534 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnereca7be62008-04-07 05:30:13 +0000535 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000536 if (rhsQI == 0) {
Steve Naroff289d9f22008-06-01 02:43:50 +0000537 // If the RHS is a unqualified interface pointer "NSString*",
538 // make sure we check the class hierarchy.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000539 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
540 ObjCInterfaceDecl *rhsID = IT->getDecl();
541 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
542 // when comparing an id<P> on lhs with a static type on rhs,
543 // see if static class implements all of id's protocols, directly or
544 // through its super class and categories.
545 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
546 return false;
547 }
548 return true;
549 }
550 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000551 }
Chris Lattnereca7be62008-04-07 05:30:13 +0000552
553 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroff289d9f22008-06-01 02:43:50 +0000554 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000555 RHSProtoI = rhsQI->qual_begin();
556 RHSProtoE = rhsQI->qual_end();
Steve Naroff289d9f22008-06-01 02:43:50 +0000557 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnereca7be62008-04-07 05:30:13 +0000558 RHSProtoI = rhsQID->qual_begin();
559 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000560 } else {
561 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000562 }
563
564 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
565 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
566 bool match = false;
567
568 // when comparing an id<P> on lhs with a static type on rhs,
569 // see if static class implements all of id's protocols, directly or
570 // through its super class and categories.
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000571 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
572 ObjCProtocolDecl *rhsProto = *RHSProtoI;
573 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000574 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnereca7be62008-04-07 05:30:13 +0000575 match = true;
576 break;
577 }
578 }
Steve Naroff289d9f22008-06-01 02:43:50 +0000579 if (rhsQI) {
580 // If the RHS is a qualified interface pointer "NSString<P>*",
581 // make sure we check the class hierarchy.
582 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
583 ObjCInterfaceDecl *rhsID = IT->getDecl();
584 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
585 // when comparing an id<P> on lhs with a static type on rhs,
586 // see if static class implements all of id's protocols, directly or
587 // through its super class and categories.
588 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
589 match = true;
590 break;
591 }
592 }
593 }
594 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000595 if (!match)
596 return false;
597 }
598
599 return true;
600 }
601
602 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
603 assert(rhsQID && "One of the LHS/RHS should be id<x>");
604
605 if (!lhs->isPointerType())
606 return false;
607
608 QualType ltype = lhs->getAsPointerType()->getPointeeType();
609 if (const ObjCQualifiedInterfaceType *lhsQI =
610 ltype->getAsObjCQualifiedInterfaceType()) {
611 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
612 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
613 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
614 bool match = false;
615 ObjCProtocolDecl *lhsProto = *LHSProtoI;
616 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
617 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
618 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Eli Friedman82b4e762008-12-16 20:15:50 +0000619 (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000620 match = true;
621 break;
Chris Lattnereca7be62008-04-07 05:30:13 +0000622 }
623 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000624 if (!match)
625 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000626 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000627 return true;
Chris Lattnereca7be62008-04-07 05:30:13 +0000628 }
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000629
630 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
631 // for static type vs. qualified 'id' type, check that class implements
632 // all of 'id's protocols.
633 ObjCInterfaceDecl *lhsID = IT->getDecl();
634 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
635 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanian26631702008-06-04 19:00:03 +0000636 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb1698cf2008-04-20 02:09:31 +0000637 return false;
638 }
639 return true;
640 }
641 return false;
Chris Lattnereca7be62008-04-07 05:30:13 +0000642}
643