blob: bc2f7f2d6f74f03d893cba9f77566c2812255732 [file] [log] [blame]
Chris Lattner09493052008-01-04 22:32:30 +00001//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective-C expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclObjC.h"
Steve Naroff9ed3e772008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Daniel Dunbar8d03cbe2008-08-11 03:27:53 +000018#include "clang/Basic/Diagnostic.h"
Chris Lattner09493052008-01-04 22:32:30 +000019using namespace clang;
20
21Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
22 ExprTy **Strings,
23 unsigned NumStrings) {
24 SourceLocation AtLoc = AtLocs[0];
25 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
26 if (NumStrings > 1) {
27 // Concatenate objc strings.
28 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
29 SourceLocation EndLoc = ES->getSourceRange().getEnd();
30 unsigned Length = 0;
31 for (unsigned i = 0; i < NumStrings; i++)
32 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
33 char *strBuf = new char [Length];
34 char *p = strBuf;
35 bool isWide = false;
36 for (unsigned i = 0; i < NumStrings; i++) {
37 S = static_cast<StringLiteral *>(Strings[i]);
38 if (S->isWide())
39 isWide = true;
40 memcpy(p, S->getStrData(), S->getByteLength());
41 p += S->getByteLength();
42 delete S;
43 }
44 S = new StringLiteral(strBuf, Length,
45 isWide, Context.getPointerType(Context.CharTy),
46 AtLoc, EndLoc);
47 }
48
49 if (CheckBuiltinCFStringArgument(S))
50 return true;
51
Ted Kremenek42730c52008-01-07 19:49:32 +000052 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner09493052008-01-04 22:32:30 +000053 // Initialize the constant string interface lazily. This assumes
54 // the NSConstantString interface is seen in this translation unit.
55 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
Steve Naroff6384a012008-04-02 14:35:35 +000056 Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattnerbac12452008-06-21 21:44:18 +000058 if (strIFace)
59 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000060 }
Ted Kremenek42730c52008-01-07 19:49:32 +000061 QualType t = Context.getObjCConstantStringInterface();
Chris Lattnerbac12452008-06-21 21:44:18 +000062 // If there is no NSConstantString interface defined then treat constant
63 // strings as untyped objects and let the runtime figure it out later.
64 if (t == QualType()) {
65 t = Context.getObjCIdType();
66 } else {
67 t = Context.getPointerType(t);
68 }
Chris Lattner09493052008-01-04 22:32:30 +000069 return new ObjCStringLiteral(S, t, AtLoc);
70}
71
72Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
73 SourceLocation EncodeLoc,
74 SourceLocation LParenLoc,
75 TypeTy *Ty,
76 SourceLocation RParenLoc) {
77 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
78
79 QualType t = Context.getPointerType(Context.CharTy);
80 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
81}
82
83Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
84 SourceLocation AtLoc,
85 SourceLocation SelLoc,
86 SourceLocation LParenLoc,
87 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000088 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-01-04 22:32:30 +000089 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
90}
91
92Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
93 SourceLocation AtLoc,
94 SourceLocation ProtoLoc,
95 SourceLocation LParenLoc,
96 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000097 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000098 if (!PDecl) {
99 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
100 return true;
101 }
102
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-01-04 22:32:30 +0000104 if (t.isNull())
105 return true;
106 t = Context.getPointerType(t);
107 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
108}
109
Daniel Dunbar7776e102008-09-11 00:50:25 +0000110bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
111 Selector Sel, ObjCMethodDecl *Method,
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000112 const char *PrefixStr,
113 SourceLocation lbrac, SourceLocation rbrac,
114 QualType &ReturnType) {
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000115 if (!Method) {
Daniel Dunbar753d1102008-09-11 00:04:36 +0000116 // Apply default argument promotion as for (C99 6.5.2.2p6).
117 for (unsigned i = 0; i != NumArgs; i++)
118 DefaultArgumentPromotion(Args[i]);
119
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000120 Diag(lbrac, diag::warn_method_not_found, std::string(PrefixStr),
121 Sel.getName(), SourceRange(lbrac, rbrac));
122 ReturnType = Context.getObjCIdType();
123 return false;
124 } else {
125 ReturnType = Method->getResultType();
126 }
127
Daniel Dunbar7776e102008-09-11 00:50:25 +0000128 unsigned NumNamedArgs = Sel.getNumArgs();
129 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
130
Chris Lattner09493052008-01-04 22:32:30 +0000131 bool anyIncompatibleArgs = false;
Daniel Dunbar7776e102008-09-11 00:50:25 +0000132 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner09493052008-01-04 22:32:30 +0000133 Expr *argExpr = Args[i];
134 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
135
136 QualType lhsType = Method->getParamDecl(i)->getType();
137 QualType rhsType = argExpr->getType();
138
139 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattnera2ef0822008-04-02 17:17:33 +0000140 if (lhsType->isArrayType())
141 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner09493052008-01-04 22:32:30 +0000142 else if (lhsType->isFunctionType())
143 lhsType = Context.getPointerType(lhsType);
144
Chris Lattnera2ef0822008-04-02 17:17:33 +0000145 AssignConvertType Result =
146 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner09493052008-01-04 22:32:30 +0000147 if (Args[i] != argExpr) // The expression was converted.
148 Args[i] = argExpr; // Make sure we store the converted expression.
149
150 anyIncompatibleArgs |=
151 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
152 argExpr, "sending");
153 }
Daniel Dunbar7776e102008-09-11 00:50:25 +0000154
155 // Promote additional arguments to variadic methods.
156 if (Method->isVariadic()) {
157 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
158 DefaultArgumentPromotion(Args[i]);
159 } else {
160 // Check for extra arguments to non-variadic methods.
161 if (NumArgs != NumNamedArgs) {
162 Diag(Args[NumNamedArgs]->getLocStart(),
163 diag::err_typecheck_call_too_many_args,
164 Method->getSourceRange(),
165 SourceRange(Args[NumNamedArgs]->getLocStart(),
166 Args[NumArgs-1]->getLocEnd()));
167 }
168 }
169
Chris Lattner09493052008-01-04 22:32:30 +0000170 return anyIncompatibleArgs;
171}
172
173// ActOnClassMessage - used for both unary and keyword messages.
174// ArgExprs is optional - if it is present, the number of expressions
175// is obtained from Sel.getNumArgs().
176Sema::ExprResult Sema::ActOnClassMessage(
177 Scope *S,
178 IdentifierInfo *receiverName, Selector Sel,
179 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
180{
181 assert(receiverName && "missing receiver class name");
182
183 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000184 ObjCInterfaceDecl* ClassDecl = 0;
Steve Naroff91a69202008-07-24 19:44:33 +0000185 bool isSuper = false;
186
Daniel Dunbar4837ae72008-08-14 22:04:54 +0000187 if (receiverName == SuperID && getCurMethodDecl()) {
Steve Naroff91a69202008-07-24 19:44:33 +0000188 isSuper = true;
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000189 ClassDecl = getCurMethodDecl()->getClassInterface()->getSuperClass();
Steve Naroff9fcf5b62008-03-28 21:37:05 +0000190 if (!ClassDecl)
191 return Diag(lbrac, diag::error_no_super_class,
Argiris Kirtzidis95256e62008-06-28 06:07:14 +0000192 getCurMethodDecl()->getClassInterface()->getName());
193 if (getCurMethodDecl()->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000194 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner09493052008-01-04 22:32:30 +0000195 superTy = Context.getPointerType(superTy);
Douglas Gregord8606632008-11-04 14:56:14 +0000196 ExprResult ReceiverExpr = new ObjCSuperExpr(SourceLocation(), superTy);
Chris Lattner09493052008-01-04 22:32:30 +0000197 // We are really in an instance method, redirect.
198 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
199 Args, NumArgs);
200 }
201 // We are sending a message to 'super' within a class method. Do nothing,
202 // the receiver will pass through as 'super' (how convenient:-).
203 } else
204 ClassDecl = getObjCInterfaceDecl(receiverName);
205
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000206 // The following code allows for the following GCC-ism:
Steve Naroff51c6cff2008-06-04 23:08:38 +0000207 //
208 // typedef XCElementDisplayRect XCElementGraphicsRect;
209 //
210 // @implementation XCRASlice
211 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
212 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
213 // }
214 //
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000215 // If necessary, the following lookup could move to getObjCInterfaceDecl().
216 if (!ClassDecl) {
217 Decl *IDecl = LookupDecl(receiverName, Decl::IDNS_Ordinary, 0, false);
218 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
219 const ObjCInterfaceType *OCIT;
220 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
221 if (OCIT)
222 ClassDecl = OCIT->getDecl();
223 }
224 }
225 assert(ClassDecl && "missing interface declaration");
Steve Naroff51c6cff2008-06-04 23:08:38 +0000226 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000227 QualType returnType;
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000228 Method = ClassDecl->lookupClassMethod(Sel);
229
230 // If we have an implementation in scope, check "private" methods.
231 if (!Method) {
232 if (ObjCImplementationDecl *ImpDecl =
233 ObjCImplementations[ClassDecl->getIdentifier()])
234 Method = ImpDecl->getClassMethod(Sel);
Steve Naroff4b9846d2008-09-28 14:55:53 +0000235
236 // Look through local category implementations associated with the class.
237 if (!Method) {
238 for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
239 if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
240 Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
241 }
242 }
Steve Naroff616c55c2008-06-04 14:43:54 +0000243 }
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000244 // Before we give up, check if the selector is an instance method.
245 if (!Method)
246 Method = ClassDecl->lookupInstanceMethod(Sel);
247
Daniel Dunbar7776e102008-09-11 00:50:25 +0000248 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "+",
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000249 lbrac, rbrac, returnType))
250 return true;
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000251
252 // If we have the ObjCInterfaceDecl* for the class that is receiving
253 // the message, use that to construct the ObjCMessageExpr. Otherwise
254 // pass on the IdentifierInfo* for the class.
Steve Naroff91a69202008-07-24 19:44:33 +0000255 // FIXME: need to do a better job handling 'super' usage within a class
256 // For now, we simply pass the "super" identifier through (which isn't
257 // consistent with instance methods.
Steve Naroff2c7e0e52008-07-25 19:39:00 +0000258 if (isSuper)
Steve Naroff91a69202008-07-24 19:44:33 +0000259 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000260 lbrac, rbrac, ArgExprs, NumArgs);
261 else
Steve Naroff91a69202008-07-24 19:44:33 +0000262 return new ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
Ted Kremenekee2c9fd2008-06-24 15:50:53 +0000263 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner09493052008-01-04 22:32:30 +0000264}
265
266// ActOnInstanceMessage - used for both unary and keyword messages.
267// ArgExprs is optional - if it is present, the number of expressions
268// is obtained from Sel.getNumArgs().
Chris Lattner4d018542008-07-21 06:31:05 +0000269Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000270 SourceLocation lbrac,
271 SourceLocation rbrac,
272 ExprTy **Args, unsigned NumArgs) {
Chris Lattner09493052008-01-04 22:32:30 +0000273 assert(receiver && "missing receiver expression");
274
275 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
276 Expr *RExpr = static_cast<Expr *>(receiver);
Chris Lattner09493052008-01-04 22:32:30 +0000277 QualType returnType;
Steve Naroff7c7817e2008-05-31 02:19:15 +0000278
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000279 QualType ReceiverCType =
280 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Chris Lattner09493052008-01-04 22:32:30 +0000281
Chris Lattner4cff4b72008-07-21 05:57:44 +0000282 // Handle messages to id.
Steve Naroffef5b9fc2008-09-29 16:51:41 +0000283 if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
284 ReceiverCType->getAsBlockPointerType()) {
Steve Naroff68354f32008-09-30 14:38:43 +0000285 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
286 Sel, SourceRange(lbrac,rbrac));
Chris Lattner05fd1cc2008-02-01 06:57:39 +0000287 if (!Method)
288 Method = FactoryMethodPool[Sel].Method;
Daniel Dunbar7776e102008-09-11 00:50:25 +0000289 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000290 lbrac, rbrac, returnType))
291 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000292 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
293 ArgExprs, NumArgs);
294 }
295
296 // Handle messages to Class.
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000297 if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000298 ObjCMethodDecl *Method = 0;
Chris Lattner55a24332008-07-21 06:44:27 +0000299 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroff1a5027c2008-06-05 14:49:39 +0000300 // If we have an implementation in scope, check "private" methods.
Chris Lattner55a24332008-07-21 06:44:27 +0000301 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff1a5027c2008-06-05 14:49:39 +0000302 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner4cff4b72008-07-21 05:57:44 +0000303 ObjCImplementations[ClassDecl->getIdentifier()])
Steve Naroff1a5027c2008-06-05 14:49:39 +0000304 Method = ImpDecl->getClassMethod(Sel);
305 }
306 if (!Method)
307 Method = FactoryMethodPool[Sel].Method;
308 if (!Method)
Steve Naroff68354f32008-09-30 14:38:43 +0000309 Method = LookupInstanceMethodInGlobalPool(
310 Sel, SourceRange(lbrac,rbrac));
Daniel Dunbar7776e102008-09-11 00:50:25 +0000311 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000312 lbrac, rbrac, returnType))
313 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000314 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
315 ArgExprs, NumArgs);
316 }
317
Chris Lattnerf9147652008-07-21 06:12:56 +0000318 ObjCMethodDecl *Method = 0;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000319 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattnerf9147652008-07-21 06:12:56 +0000320
321 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
322 // long as one of the protocols implements the selector (if not, warn).
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000323 if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000324 // Search protocols
Chris Lattner4cff4b72008-07-21 05:57:44 +0000325 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
326 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
327 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
328 break;
329 }
330 if (!Method)
331 Diag(lbrac, diag::warn_method_not_found_in_protocol,
332 std::string("-"), Sel.getName(),
Chris Lattner3977eb72008-07-21 06:16:07 +0000333 RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000334 } else if (const ObjCInterfaceType *OCIReceiver =
Chris Lattnerd5a56aa2008-07-26 22:17:49 +0000335 ReceiverCType->getAsPointerToObjCInterfaceType()) {
Chris Lattnerf9147652008-07-21 06:12:56 +0000336 // We allow sending a message to a pointer to an interface (an object).
Chris Lattnere1b957d2008-02-01 06:43:02 +0000337
Chris Lattner4cff4b72008-07-21 05:57:44 +0000338 ClassDecl = OCIReceiver->getDecl();
Steve Naroff68354f32008-09-30 14:38:43 +0000339 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
340 // faster than the following method (which can do *many* linear searches).
341 // The idea is to add class info to InstanceMethodPool.
Chris Lattner4cff4b72008-07-21 05:57:44 +0000342 Method = ClassDecl->lookupInstanceMethod(Sel);
343
344 if (!Method) {
345 // Search protocol qualifiers.
346 for (ObjCQualifiedIdType::qual_iterator QI = OCIReceiver->qual_begin(),
347 E = OCIReceiver->qual_end(); QI != E; ++QI) {
348 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner09493052008-01-04 22:32:30 +0000349 break;
350 }
Chris Lattner09493052008-01-04 22:32:30 +0000351 }
Chris Lattner00cb97b2008-07-21 05:27:51 +0000352
Chris Lattner4cff4b72008-07-21 05:57:44 +0000353 if (!Method && !OCIReceiver->qual_empty())
354 Diag(lbrac, diag::warn_method_not_found_in_protocol,
355 std::string("-"), Sel.getName(),
Chris Lattner09493052008-01-04 22:32:30 +0000356 SourceRange(lbrac, rbrac));
Chris Lattnerf9147652008-07-21 06:12:56 +0000357 } else {
358 Diag(lbrac, diag::error_bad_receiver_type,
Chris Lattner3977eb72008-07-21 06:16:07 +0000359 RExpr->getType().getAsString(), RExpr->getSourceRange());
Chris Lattnerf9147652008-07-21 06:12:56 +0000360 return true;
Chris Lattner4cff4b72008-07-21 05:57:44 +0000361 }
362
363 if (!Method) {
364 // If we have an implementation in scope, check "private" methods.
365 if (ClassDecl)
366 if (ObjCImplementationDecl *ImpDecl =
367 ObjCImplementations[ClassDecl->getIdentifier()])
368 Method = ImpDecl->getInstanceMethod(Sel);
369 // If we still haven't found a method, look in the global pool. This
370 // behavior isn't very desirable, however we need it for GCC
371 // compatibility.
372 if (!Method)
Steve Naroff68354f32008-09-30 14:38:43 +0000373 Method = LookupInstanceMethodInGlobalPool(
374 Sel, SourceRange(lbrac,rbrac));
Chris Lattner4cff4b72008-07-21 05:57:44 +0000375 }
Daniel Dunbar7776e102008-09-11 00:50:25 +0000376 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, "-",
Daniel Dunbar842b5e22008-09-11 00:01:56 +0000377 lbrac, rbrac, returnType))
378 return true;
Chris Lattner09493052008-01-04 22:32:30 +0000379 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
380 ArgExprs, NumArgs);
381}
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000382
383//===----------------------------------------------------------------------===//
384// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
385//===----------------------------------------------------------------------===//
386
387/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
388/// inheritance hierarchy of 'rProto'.
389static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
390 ObjCProtocolDecl *rProto) {
391 if (lProto == rProto)
392 return true;
Chris Lattner0be08822008-07-21 21:32:27 +0000393 for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
394 E = rProto->protocol_end(); PI != E; ++PI)
395 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000396 return true;
397 return false;
398}
399
400/// ClassImplementsProtocol - Checks that 'lProto' protocol
401/// has been implemented in IDecl class, its super class or categories (if
402/// lookupCategory is true).
403static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
404 ObjCInterfaceDecl *IDecl,
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000405 bool lookupCategory,
406 bool RHSIsQualifiedID = false) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000407
408 // 1st, look up the class.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000409 const ObjCList<ObjCProtocolDecl> &Protocols =
410 IDecl->getReferencedProtocols();
411
412 for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
413 E = Protocols.end(); PI != E; ++PI) {
414 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000415 return true;
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000416 // This is dubious and is added to be compatible with gcc.
417 // In gcc, it is also allowed assigning a protocol-qualified 'id'
418 // type to a LHS object when protocol in qualified LHS is in list
419 // of protocols in the rhs 'id' object. This IMO, should be a bug.
Ted Kremenek5a5cd832008-06-04 20:48:08 +0000420 // FIXME: Treat this as an extension, and flag this as an error when
421 // GCC extensions are not enabled.
Chris Lattner8bcb5252008-07-21 18:19:38 +0000422 if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000423 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000424 }
425
426 // 2nd, look up the category.
427 if (lookupCategory)
428 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
429 CDecl = CDecl->getNextClassCategory()) {
Chris Lattner0be08822008-07-21 21:32:27 +0000430 for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
431 E = CDecl->protocol_end(); PI != E; ++PI)
432 if (ProtocolCompatibleWithProtocol(lProto, *PI))
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000433 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000434 }
435
436 // 3rd, look up the super class(s)
437 if (IDecl->getSuperClass())
438 return
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000439 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
440 RHSIsQualifiedID);
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000441
442 return false;
443}
444
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000445/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
446/// ObjCQualifiedIDType.
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000447bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
448 bool compare) {
449 // Allow id<P..> and an 'id' or void* type in all cases.
450 if (const PointerType *PT = lhs->getAsPointerType()) {
451 QualType PointeeTy = PT->getPointeeType();
452 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
453 return true;
454 } else if (const PointerType *PT = rhs->getAsPointerType()) {
455 QualType PointeeTy = PT->getPointeeType();
456 if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
457 return true;
458 }
459
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000460 if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
461 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
462 const ObjCQualifiedInterfaceType *rhsQI = 0;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000463 QualType rtype;
464
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000465 if (!rhsQID) {
466 // Not comparing two ObjCQualifiedIdType's?
467 if (!rhs->isPointerType()) return false;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000468
469 rtype = rhs->getAsPointerType()->getPointeeType();
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000470 rhsQI = rtype->getAsObjCQualifiedInterfaceType();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000471 if (rhsQI == 0) {
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000472 // If the RHS is a unqualified interface pointer "NSString*",
473 // make sure we check the class hierarchy.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000474 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
475 ObjCInterfaceDecl *rhsID = IT->getDecl();
476 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
477 // when comparing an id<P> on lhs with a static type on rhs,
478 // see if static class implements all of id's protocols, directly or
479 // through its super class and categories.
480 if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
481 return false;
482 }
483 return true;
484 }
485 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000486 }
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000487
488 ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000489 if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000490 RHSProtoI = rhsQI->qual_begin();
491 RHSProtoE = rhsQI->qual_end();
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000492 } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000493 RHSProtoI = rhsQID->qual_begin();
494 RHSProtoE = rhsQID->qual_end();
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000495 } else {
496 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000497 }
498
499 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
500 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
501 bool match = false;
502
503 // when comparing an id<P> on lhs with a static type on rhs,
504 // see if static class implements all of id's protocols, directly or
505 // through its super class and categories.
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000506 for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
507 ObjCProtocolDecl *rhsProto = *RHSProtoI;
508 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
509 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000510 match = true;
511 break;
512 }
513 }
Steve Naroffd6ba7fd2008-06-01 02:43:50 +0000514 if (rhsQI) {
515 // If the RHS is a qualified interface pointer "NSString<P>*",
516 // make sure we check the class hierarchy.
517 if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
518 ObjCInterfaceDecl *rhsID = IT->getDecl();
519 for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
520 // when comparing an id<P> on lhs with a static type on rhs,
521 // see if static class implements all of id's protocols, directly or
522 // through its super class and categories.
523 if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
524 match = true;
525 break;
526 }
527 }
528 }
529 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000530 if (!match)
531 return false;
532 }
533
534 return true;
535 }
536
537 const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
538 assert(rhsQID && "One of the LHS/RHS should be id<x>");
539
540 if (!lhs->isPointerType())
541 return false;
542
543 QualType ltype = lhs->getAsPointerType()->getPointeeType();
544 if (const ObjCQualifiedInterfaceType *lhsQI =
545 ltype->getAsObjCQualifiedInterfaceType()) {
546 ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
547 ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
548 for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
549 bool match = false;
550 ObjCProtocolDecl *lhsProto = *LHSProtoI;
551 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
552 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
553 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000554 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000555 match = true;
556 break;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000557 }
558 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000559 if (!match)
560 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000561 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000562 return true;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000563 }
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000564
565 if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
566 // for static type vs. qualified 'id' type, check that class implements
567 // all of 'id's protocols.
568 ObjCInterfaceDecl *lhsID = IT->getDecl();
569 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
570 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniane2665642008-06-04 19:00:03 +0000571 if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
Chris Lattnerb2a0e612008-04-20 02:09:31 +0000572 return false;
573 }
574 return true;
575 }
576 return false;
Chris Lattnerfe1f4032008-04-07 05:30:13 +0000577}
578