blob: 8ce782b961e891c45a02a6a416288ae28e53398d [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 Lattner39c28bb2009-02-18 06:48:40 +000018#include "llvm/ADT/SmallString.h"
Steve Naroff61f72cb2009-03-09 21:12:44 +000019#include "clang/Lex/Preprocessor.h"
20
Chris Lattner85a932e2008-01-04 22:32:30 +000021using namespace clang;
22
Mike Stump1eb44332009-09-09 15:08:12 +000023Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Chris Lattner39c28bb2009-02-18 06:48:40 +000024 ExprTy **strings,
Chris Lattner85a932e2008-01-04 22:32:30 +000025 unsigned NumStrings) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000026 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
27
Chris Lattnerf4b136f2009-02-18 06:13:04 +000028 // Most ObjC strings are formed out of a single piece. However, we *can*
29 // have strings formed out of multiple @ strings with multiple pptokens in
30 // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
31 // StringLiteral for ObjCStringLiteral to hold onto.
Chris Lattner39c28bb2009-02-18 06:48:40 +000032 StringLiteral *S = Strings[0];
Mike Stump1eb44332009-09-09 15:08:12 +000033
Chris Lattnerf4b136f2009-02-18 06:13:04 +000034 // If we have a multi-part string, merge it all together.
35 if (NumStrings != 1) {
Chris Lattner85a932e2008-01-04 22:32:30 +000036 // Concatenate objc strings.
Chris Lattner39c28bb2009-02-18 06:48:40 +000037 llvm::SmallString<128> StrBuf;
38 llvm::SmallVector<SourceLocation, 8> StrLocs;
Mike Stump1eb44332009-09-09 15:08:12 +000039
Chris Lattner726e1682009-02-18 05:49:11 +000040 for (unsigned i = 0; i != NumStrings; ++i) {
Chris Lattner39c28bb2009-02-18 06:48:40 +000041 S = Strings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000042
Chris Lattner39c28bb2009-02-18 06:48:40 +000043 // ObjC strings can't be wide.
Chris Lattnerf4b136f2009-02-18 06:13:04 +000044 if (S->isWide()) {
45 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
46 << S->getSourceRange();
47 return true;
48 }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattner39c28bb2009-02-18 06:48:40 +000050 // Get the string data.
51 StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
Mike Stump1eb44332009-09-09 15:08:12 +000052
Chris Lattner39c28bb2009-02-18 06:48:40 +000053 // Get the locations of the string tokens.
54 StrLocs.append(S->tokloc_begin(), S->tokloc_end());
Mike Stump1eb44332009-09-09 15:08:12 +000055
Chris Lattner39c28bb2009-02-18 06:48:40 +000056 // Free the temporary string.
Ted Kremenek8189cde2009-02-07 01:47:29 +000057 S->Destroy(Context);
Chris Lattner85a932e2008-01-04 22:32:30 +000058 }
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner39c28bb2009-02-18 06:48:40 +000060 // Create the aggregate string with the appropriate content and location
61 // information.
62 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
Chris Lattner2085fd62009-02-18 06:40:38 +000063 Context.getPointerType(Context.CharTy),
Chris Lattner39c28bb2009-02-18 06:48:40 +000064 &StrLocs[0], StrLocs.size());
Chris Lattner85a932e2008-01-04 22:32:30 +000065 }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Chris Lattner69039812009-02-18 06:01:06 +000067 // Verify that this composite string is acceptable for ObjC strings.
68 if (CheckObjCString(S))
Chris Lattner85a932e2008-01-04 22:32:30 +000069 return true;
Chris Lattnera0af1fe2009-02-18 06:06:56 +000070
71 // Initialize the constant string interface lazily. This assumes
Steve Naroffd9fd7642009-04-07 14:18:33 +000072 // the NSString interface is seen in this translation unit. Note: We
73 // don't use NSConstantString, since the runtime team considers this
74 // interface private (even though it appears in the header files).
Chris Lattnera0af1fe2009-02-18 06:06:56 +000075 QualType Ty = Context.getObjCConstantStringInterface();
76 if (!Ty.isNull()) {
Steve Naroff14108da2009-07-10 23:34:53 +000077 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattner13fd7e52008-06-21 21:44:18 +000078 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000079 IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
John McCallf36e02d2009-10-09 21:13:30 +000080 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, LookupOrdinaryName);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000081 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
82 Context.setObjCConstantStringInterface(StrIF);
83 Ty = Context.getObjCConstantStringInterface();
Steve Naroff14108da2009-07-10 23:34:53 +000084 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +000085 } else {
Steve Naroffd9fd7642009-04-07 14:18:33 +000086 // If there is no NSString interface defined then treat constant
Chris Lattnera0af1fe2009-02-18 06:06:56 +000087 // strings as untyped objects and let the runtime figure it out later.
88 Ty = Context.getObjCIdType();
89 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattnerf4b136f2009-02-18 06:13:04 +000092 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
Chris Lattner85a932e2008-01-04 22:32:30 +000093}
94
Mike Stump1eb44332009-09-09 15:08:12 +000095Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
Anders Carlssonfc0f0212009-06-07 18:45:35 +000096 QualType EncodedType,
97 SourceLocation RParenLoc) {
98 QualType StrTy;
Mike Stump1eb44332009-09-09 15:08:12 +000099 if (EncodedType->isDependentType())
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000100 StrTy = Context.DependentTy;
101 else {
102 std::string Str;
103 Context.getObjCEncodingForType(EncodedType, Str);
104
105 // The type of @encode is the same as the type of the corresponding string,
106 // which is an array type.
107 StrTy = Context.CharTy;
108 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
109 if (getLangOptions().CPlusPlus)
110 StrTy.addConst();
111 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
112 ArrayType::Normal, 0);
113 }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000115 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
116}
117
Chris Lattner85a932e2008-01-04 22:32:30 +0000118Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
119 SourceLocation EncodeLoc,
120 SourceLocation LParenLoc,
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000121 TypeTy *ty,
Chris Lattner85a932e2008-01-04 22:32:30 +0000122 SourceLocation RParenLoc) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000123 // FIXME: Preserve type source info ?
124 QualType EncodedType = GetTypeFromParser(ty);
Chris Lattner85a932e2008-01-04 22:32:30 +0000125
Anders Carlssonfc0f0212009-06-07 18:45:35 +0000126 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000127}
128
129Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
130 SourceLocation AtLoc,
131 SourceLocation SelLoc,
132 SourceLocation LParenLoc,
133 SourceLocation RParenLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
Fariborz Jahanian835ed7f2009-08-22 21:13:55 +0000135 SourceRange(LParenLoc, RParenLoc), false);
Fariborz Jahanian7ff22de2009-06-16 16:25:00 +0000136 if (!Method)
137 Method = LookupFactoryMethodInGlobalPool(Sel,
138 SourceRange(LParenLoc, RParenLoc));
139 if (!Method)
140 Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
141
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000142 QualType Ty = Context.getObjCSelType();
David Chisnalla8fa96e2010-02-03 02:09:30 +0000143 ObjCSelectorExpr *E =
144 new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
145 // Make sure that we have seen this selector. There are lots of checks we
146 // should be doing on this selector. For example, when this is passed as the
147 // second argument to objc_msgSend() on the Mac runtime, or as the selector
148 // argument to the -performSelector:. We can do these checks at run time
149 // with the GNU runtimes, but the Apple runtimes let you sneak stack
150 // corruption in easily by passing the wrong selector to these functions if
151 // there is no static checking.
152 //
153 // Only log a warning on the GNU runtime.
154 E->setMethodDecl(LookupInstanceMethodInGlobalPool(Sel,
155 SourceRange(LParenLoc, LParenLoc), !LangOpts.NeXTRuntime));
156 return E;
Chris Lattner85a932e2008-01-04 22:32:30 +0000157}
158
159Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
160 SourceLocation AtLoc,
161 SourceLocation ProtoLoc,
162 SourceLocation LParenLoc,
163 SourceLocation RParenLoc) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000164 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner85a932e2008-01-04 22:32:30 +0000165 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000166 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000167 return true;
168 }
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000170 QualType Ty = Context.getObjCProtoType();
171 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000172 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000173 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000174 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000175}
176
Mike Stump1eb44332009-09-09 15:08:12 +0000177bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
178 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000179 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000180 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000181 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000182 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000183 // Apply default argument promotion as for (C99 6.5.2.2p6).
184 for (unsigned i = 0; i != NumArgs; i++)
185 DefaultArgumentPromotion(Args[i]);
186
Chris Lattner077bf5e2008-11-24 03:33:13 +0000187 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
188 diag::warn_inst_method_not_found;
189 Diag(lbrac, DiagID)
190 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000191 ReturnType = Context.getObjCIdType();
192 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Chris Lattner077bf5e2008-11-24 03:33:13 +0000195 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000197 unsigned NumNamedArgs = Sel.getNumArgs();
198 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
199
Chris Lattner312531a2009-04-12 08:11:20 +0000200 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000201 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000202 Expr *argExpr = Args[i];
203 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattner89951a82009-02-20 18:43:26 +0000205 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000206 QualType rhsType = argExpr->getType();
207
Mike Stump1eb44332009-09-09 15:08:12 +0000208 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000209 if (lhsType->isArrayType())
210 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000211 else if (lhsType->isFunctionType())
212 lhsType = Context.getPointerType(lhsType);
213
Mike Stump1eb44332009-09-09 15:08:12 +0000214 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000215 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000216 if (Args[i] != argExpr) // The expression was converted.
217 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000218
219 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000220 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
Douglas Gregor68647482009-12-16 03:45:30 +0000221 argExpr, AA_Sending);
Chris Lattner85a932e2008-01-04 22:32:30 +0000222 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000223
224 // Promote additional arguments to variadic methods.
225 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000226 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000227 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000228 } else {
229 // Check for extra arguments to non-variadic methods.
230 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000231 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000232 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000233 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000234 << SourceRange(Args[NumNamedArgs]->getLocStart(),
235 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000236 }
237 }
238
Chris Lattner312531a2009-04-12 08:11:20 +0000239 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000240}
241
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000242bool Sema::isSelfExpr(Expr *RExpr) {
243 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
244 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
245 return true;
246 return false;
247}
248
Steve Narofff1afaf62009-02-26 15:55:06 +0000249// Helper method for ActOnClassMethod/ActOnInstanceMethod.
250// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000251// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000252// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000253ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000254 ObjCInterfaceDecl *ClassDecl) {
255 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000256 // lookup in class and all superclasses
257 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000258 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000259 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Steve Naroff5609ec02009-03-08 18:56:13 +0000261 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000262 if (!Method)
263 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Steve Naroff5609ec02009-03-08 18:56:13 +0000265 // Before we give up, check if the selector is an instance method.
266 // But only in the root. This matches gcc's behaviour and what the
267 // runtime expects.
268 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000269 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000270 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000271 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000272 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000273 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Steve Naroff5609ec02009-03-08 18:56:13 +0000276 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000277 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000278 return Method;
279}
280
281ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
282 ObjCInterfaceDecl *ClassDecl) {
283 ObjCMethodDecl *Method = 0;
284 while (ClassDecl && !Method) {
285 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000286 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000287 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Steve Naroff5609ec02009-03-08 18:56:13 +0000289 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000290 if (!Method)
291 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000292 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000293 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000294 return Method;
295}
296
Steve Naroff61f72cb2009-03-09 21:12:44 +0000297Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
298 IdentifierInfo &receiverName,
299 IdentifierInfo &propertyName,
300 SourceLocation &receiverNameLoc,
301 SourceLocation &propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000303 IdentifierInfo *receiverNamePtr = &receiverName;
304 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Steve Naroff61f72cb2009-03-09 21:12:44 +0000306 // Search for a declared property first.
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Steve Naroff61f72cb2009-03-09 21:12:44 +0000308 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000309 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000310
311 // If this reference is in an @implementation, check for 'private' methods.
312 if (!Getter)
313 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
314 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000315 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000316 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000317
318 if (Getter) {
319 // FIXME: refactor/share with ActOnMemberReference().
320 // Check if we can reference this property.
321 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
322 return ExprError();
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Steve Naroff61f72cb2009-03-09 21:12:44 +0000325 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000326 Selector SetterSel =
327 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000328 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000330 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000331 if (!Setter) {
332 // If this reference is in an @implementation, also check for 'private'
333 // methods.
334 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
335 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000336 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000337 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000338 }
339 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000340 if (!Setter)
341 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000342
343 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
344 return ExprError();
345
346 if (Getter || Setter) {
347 QualType PType;
348
349 if (Getter)
350 PType = Getter->getResultType();
351 else {
352 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
353 E = Setter->param_end(); PI != E; ++PI)
354 PType = (*PI)->getType();
355 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000356 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000357 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000358 propertyNameLoc, IFace, receiverNameLoc));
359 }
360 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
361 << &propertyName << Context.getObjCInterfaceType(IFace));
362}
363
364
Chris Lattner85a932e2008-01-04 22:32:30 +0000365// ActOnClassMessage - used for both unary and keyword messages.
366// ArgExprs is optional - if it is present, the number of expressions
367// is obtained from Sel.getNumArgs().
368Sema::ExprResult Sema::ActOnClassMessage(
369 Scope *S,
370 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000371 SourceLocation lbrac, SourceLocation receiverLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000372 SourceLocation selectorLoc, SourceLocation rbrac,
373 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000374 assert(receiverName && "missing receiver class name");
375
376 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000377 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000378 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner84692652008-11-20 05:35:30 +0000380 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000381 if (getCurMethodDecl()) {
382 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000383 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
384 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000385 return Diag(lbrac, diag::error_no_super_class_message)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000386 << getCurMethodDecl()->getDeclName();
387 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000388 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000389 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000390 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000391 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000392 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000393 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
394 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000395 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000396 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000397 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000398 }
399 // We are sending a message to 'super' within a class method. Do nothing,
400 // the receiver will pass through as 'super' (how convenient:-).
401 } else {
402 // 'super' has been used outside a method context. If a variable named
403 // 'super' has been declared, redirect. If not, produce a diagnostic.
John McCallf36e02d2009-10-09 21:13:30 +0000404 NamedDecl *SuperDecl
405 = LookupSingleName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000406 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
407 if (VD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000408 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
Ted Kremenek8189cde2009-02-07 01:47:29 +0000409 receiverLoc);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000410 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000411 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000412 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000413 }
Fariborz Jahaniane4fb8282010-01-22 23:04:44 +0000414 else if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(SuperDecl)) {
415 const ObjCInterfaceType *OCIT;
416 OCIT = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>();
417 if (!OCIT) {
418 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
419 return true;
420 }
421 ClassDecl = OCIT->getDecl();
422 }
423 else
424 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Mike Stump1eb44332009-09-09 15:08:12 +0000425 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000426 } else
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000427 ClassDecl = getObjCInterfaceDecl(receiverName, receiverLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Steve Naroff7c778f12008-07-25 19:39:00 +0000429 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000430 //
431 // typedef XCElementDisplayRect XCElementGraphicsRect;
432 //
433 // @implementation XCRASlice
434 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
435 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
436 // }
437 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000438 // If necessary, the following lookup could move to getObjCInterfaceDecl().
439 if (!ClassDecl) {
John McCallf36e02d2009-10-09 21:13:30 +0000440 NamedDecl *IDecl
441 = LookupSingleName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000442 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
443 const ObjCInterfaceType *OCIT;
John McCall183700f2009-09-21 23:43:11 +0000444 OCIT = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>();
Chris Lattner64540d72009-03-29 05:01:10 +0000445 if (!OCIT) {
446 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
447 return true;
448 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000449 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000450 }
451 }
452 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000453 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000454 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000455 if (ClassDecl->isForwardDecl()) {
456 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000457 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000458 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
459 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000460 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000461 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000462 }
463 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000464 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Steve Naroff7c778f12008-07-25 19:39:00 +0000466 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000467 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000468 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000469
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000470 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
471 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
473 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000474 lbrac, rbrac, returnType))
475 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000476
Anders Carlsson187ba152009-05-26 15:22:25 +0000477 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Mike Stump390b4cc2009-05-16 07:39:55 +0000479 // If we have the ObjCInterfaceDecl* for the class that is receiving the
480 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
481 // IdentifierInfo* for the class.
482 // FIXME: need to do a better job handling 'super' usage within a class. For
483 // now, we simply pass the "super" identifier through (which isn't consistent
484 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000485 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000486 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
487 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000488 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000489 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
490 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000491}
492
493// ActOnInstanceMessage - used for both unary and keyword messages.
494// ArgExprs is optional - if it is present, the number of expressions
495// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000496Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000497 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000498 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000499 SourceLocation rbrac,
500 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000501 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner85a932e2008-01-04 22:32:30 +0000503 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
504 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattnerd0d45992009-04-29 05:48:32 +0000506 // If necessary, apply function/array conversion to the receiver.
507 // C99 6.7.5.3p[7,8].
Douglas Gregora873dfc2010-02-03 00:27:59 +0000508 DefaultFunctionArrayLvalueConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattner85a932e2008-01-04 22:32:30 +0000510 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000511 QualType ReceiverCType =
512 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000513
514 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000515 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000516 ObjCMethodDecl *Method = 0;
517 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
518 // If we have an interface in scope, check 'super' methods.
519 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000520 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000521 Method = SuperDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
523 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000524 // If we have implementations in scope, check "private" methods.
525 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
526 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000527 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000528
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000529 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
530 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000531
Chris Lattner077bf5e2008-11-24 03:33:13 +0000532 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000533 lbrac, rbrac, returnType))
534 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Anders Carlsson187ba152009-05-26 15:22:25 +0000536 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000537 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
538 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000539 }
540
Mike Stump1eb44332009-09-09 15:08:12 +0000541 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000542 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000543 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000544 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
545 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000546 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000547 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000548 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000549 lbrac, rbrac, returnType))
550 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000551 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000552 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
553 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000556 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000557 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000558 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000559 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner6562fda2008-07-21 06:44:27 +0000561 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000562 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
563 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000564 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Steve Narofff1afaf62009-02-26 15:55:06 +0000566 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000567 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
569 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000570 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000571 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000572 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
573 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000574 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000575 if (!Method) {
576 // If not messaging 'self', look for any factory method named 'Sel'.
577 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000578 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000579 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000580 // If no class (factory) method was found, check if an _instance_
581 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000582 Method = LookupInstanceMethodInGlobalPool(
583 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000584 if (Method)
585 if (const ObjCInterfaceDecl *ID =
586 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
587 if (ID->getSuperClass())
588 Diag(lbrac, diag::warn_root_inst_method_not_found)
589 << Sel << SourceRange(lbrac, rbrac);
590 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000591 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000592 }
593 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000594 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000595 lbrac, rbrac, returnType))
596 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000597 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000598 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
599 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000602 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000603 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
605 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000606 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000607 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000608 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000609 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000610 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000611 E = QIdTy->qual_end(); I != E; ++I) {
612 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000613 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000614 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000615 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000616 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000617 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000620 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000621 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Steve Naroff14108da2009-07-10 23:34:53 +0000623 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000624 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000625 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000626 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000627 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000629 if (!Method) {
630 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000631 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000632 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000633 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000634 break;
635 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000636 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000637 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000638 // If we have implementations in scope, check "private" methods.
639 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Steve Naroff5609ec02009-03-08 18:56:13 +0000641 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000642 // If we still haven't found a method, look in the global pool. This
643 // behavior isn't very desirable, however we need it for GCC
644 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000645 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000646 Method = LookupInstanceMethodInGlobalPool(
647 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000648 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000649 Diag(lbrac, diag::warn_maynot_respond)
Steve Naroff14108da2009-07-10 23:34:53 +0000650 << OCIType->getInterfaceDecl()->getIdentifier()->getName() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000651 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000652 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000653 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000654 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
655 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000656 } else if (!Context.getObjCIdType().isNull() &&
657 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000658 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000659 ReceiverCType->isScalarType()))) {
660 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000661 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000662 << RExpr->getType() << RExpr->getSourceRange();
Eli Friedman73c39ab2009-10-20 08:27:19 +0000663 if (ReceiverCType->isPointerType())
664 ImpCastExprToType(RExpr, Context.getObjCIdType(), CastExpr::CK_BitCast);
665 else
666 ImpCastExprToType(RExpr, Context.getObjCIdType(),
667 CastExpr::CK_IntegralToPointer);
Chris Lattner0c73f372009-03-09 21:19:16 +0000668 } else {
669 // Reject other random receiver types (e.g. structs).
670 Diag(lbrac, diag::err_bad_receiver_type)
671 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000672 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000675 if (Method)
676 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000677 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000678 lbrac, rbrac, returnType))
679 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000680 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000681 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
682 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000683}
Chris Lattnereca7be62008-04-07 05:30:13 +0000684