blob: 2616c5c0bd1e7ce94ac7cee25c3adb95436ead6e [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");
Chris Lattnera0af1fe2009-02-18 06:06:56 +000080 NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
81 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();
143 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000144}
145
146Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
147 SourceLocation AtLoc,
148 SourceLocation ProtoLoc,
149 SourceLocation LParenLoc,
150 SourceLocation RParenLoc) {
Douglas Gregor6e378de2009-04-23 23:18:26 +0000151 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId);
Chris Lattner85a932e2008-01-04 22:32:30 +0000152 if (!PDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000153 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
Chris Lattner85a932e2008-01-04 22:32:30 +0000154 return true;
155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000157 QualType Ty = Context.getObjCProtoType();
158 if (Ty.isNull())
Chris Lattner85a932e2008-01-04 22:32:30 +0000159 return true;
Steve Naroff14108da2009-07-10 23:34:53 +0000160 Ty = Context.getObjCObjectPointerType(Ty);
Chris Lattnera0af1fe2009-02-18 06:06:56 +0000161 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
Chris Lattner85a932e2008-01-04 22:32:30 +0000162}
163
Mike Stump1eb44332009-09-09 15:08:12 +0000164bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
165 Selector Sel, ObjCMethodDecl *Method,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000166 bool isClassMessage,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000167 SourceLocation lbrac, SourceLocation rbrac,
Mike Stump1eb44332009-09-09 15:08:12 +0000168 QualType &ReturnType) {
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000169 if (!Method) {
Daniel Dunbar6660c8a2008-09-11 00:04:36 +0000170 // Apply default argument promotion as for (C99 6.5.2.2p6).
171 for (unsigned i = 0; i != NumArgs; i++)
172 DefaultArgumentPromotion(Args[i]);
173
Chris Lattner077bf5e2008-11-24 03:33:13 +0000174 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
175 diag::warn_inst_method_not_found;
176 Diag(lbrac, DiagID)
177 << Sel << isClassMessage << SourceRange(lbrac, rbrac);
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000178 ReturnType = Context.getObjCIdType();
179 return false;
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner077bf5e2008-11-24 03:33:13 +0000182 ReturnType = Method->getResultType();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000184 unsigned NumNamedArgs = Sel.getNumArgs();
185 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
186
Chris Lattner312531a2009-04-12 08:11:20 +0000187 bool IsError = false;
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000188 for (unsigned i = 0; i < NumNamedArgs; i++) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000189 Expr *argExpr = Args[i];
190 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Chris Lattner89951a82009-02-20 18:43:26 +0000192 QualType lhsType = Method->param_begin()[i]->getType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000193 QualType rhsType = argExpr->getType();
194
Mike Stump1eb44332009-09-09 15:08:12 +0000195 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
Chris Lattner987798a2008-04-02 17:17:33 +0000196 if (lhsType->isArrayType())
197 lhsType = Context.getArrayDecayedType(lhsType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000198 else if (lhsType->isFunctionType())
199 lhsType = Context.getPointerType(lhsType);
200
Mike Stump1eb44332009-09-09 15:08:12 +0000201 AssignConvertType Result =
Chris Lattner987798a2008-04-02 17:17:33 +0000202 CheckSingleAssignmentConstraints(lhsType, argExpr);
Chris Lattner85a932e2008-01-04 22:32:30 +0000203 if (Args[i] != argExpr) // The expression was converted.
204 Args[i] = argExpr; // Make sure we store the converted expression.
Mike Stump1eb44332009-09-09 15:08:12 +0000205
206 IsError |=
Chris Lattner85a932e2008-01-04 22:32:30 +0000207 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
208 argExpr, "sending");
209 }
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000210
211 // Promote additional arguments to variadic methods.
212 if (Method->isVariadic()) {
Anders Carlssondce5e2c2009-01-16 16:48:51 +0000213 for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
Chris Lattner312531a2009-04-12 08:11:20 +0000214 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000215 } else {
216 // Check for extra arguments to non-variadic methods.
217 if (NumArgs != NumNamedArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000218 Diag(Args[NumNamedArgs]->getLocStart(),
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000219 diag::err_typecheck_call_too_many_args)
Chris Lattner2c21a072008-11-21 18:44:24 +0000220 << 2 /*method*/ << Method->getSourceRange()
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000221 << SourceRange(Args[NumNamedArgs]->getLocStart(),
222 Args[NumArgs-1]->getLocEnd());
Daniel Dunbar91e19b22008-09-11 00:50:25 +0000223 }
224 }
225
Chris Lattner312531a2009-04-12 08:11:20 +0000226 return IsError;
Chris Lattner85a932e2008-01-04 22:32:30 +0000227}
228
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000229bool Sema::isSelfExpr(Expr *RExpr) {
230 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
231 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
232 return true;
233 return false;
234}
235
Steve Narofff1afaf62009-02-26 15:55:06 +0000236// Helper method for ActOnClassMethod/ActOnInstanceMethod.
237// Will search "local" class/category implementations for a method decl.
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000238// If failed, then we search in class's root for an instance method.
Steve Narofff1afaf62009-02-26 15:55:06 +0000239// Returns 0 if no method is found.
Steve Naroff5609ec02009-03-08 18:56:13 +0000240ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
Steve Narofff1afaf62009-02-26 15:55:06 +0000241 ObjCInterfaceDecl *ClassDecl) {
242 ObjCMethodDecl *Method = 0;
Steve Naroff5609ec02009-03-08 18:56:13 +0000243 // lookup in class and all superclasses
244 while (ClassDecl && !Method) {
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000245 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000246 Method = ImpDecl->getClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Steve Naroff5609ec02009-03-08 18:56:13 +0000248 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000249 if (!Method)
250 Method = ClassDecl->getCategoryClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Steve Naroff5609ec02009-03-08 18:56:13 +0000252 // Before we give up, check if the selector is an instance method.
253 // But only in the root. This matches gcc's behaviour and what the
254 // runtime expects.
255 if (!Method && !ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000256 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000257 // Look through local category implementations associated
Steve Naroff5609ec02009-03-08 18:56:13 +0000258 // with the root class.
Mike Stump1eb44332009-09-09 15:08:12 +0000259 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000260 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Steve Naroff5609ec02009-03-08 18:56:13 +0000263 ClassDecl = ClassDecl->getSuperClass();
Steve Narofff1afaf62009-02-26 15:55:06 +0000264 }
Steve Naroff5609ec02009-03-08 18:56:13 +0000265 return Method;
266}
267
268ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
269 ObjCInterfaceDecl *ClassDecl) {
270 ObjCMethodDecl *Method = 0;
271 while (ClassDecl && !Method) {
272 // If we have implementations in scope, check "private" methods.
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000273 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000274 Method = ImpDecl->getInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Steve Naroff5609ec02009-03-08 18:56:13 +0000276 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000277 if (!Method)
278 Method = ClassDecl->getCategoryInstanceMethod(Sel);
Steve Naroff5609ec02009-03-08 18:56:13 +0000279 ClassDecl = ClassDecl->getSuperClass();
Fariborz Jahanian175ba1e2009-03-04 18:15:57 +0000280 }
Steve Narofff1afaf62009-02-26 15:55:06 +0000281 return Method;
282}
283
Steve Naroff61f72cb2009-03-09 21:12:44 +0000284Action::OwningExprResult Sema::ActOnClassPropertyRefExpr(
285 IdentifierInfo &receiverName,
286 IdentifierInfo &propertyName,
287 SourceLocation &receiverNameLoc,
288 SourceLocation &propertyNameLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Steve Naroff61f72cb2009-03-09 21:12:44 +0000290 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Steve Naroff61f72cb2009-03-09 21:12:44 +0000292 // Search for a declared property first.
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Steve Naroff61f72cb2009-03-09 21:12:44 +0000294 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000295 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000296
297 // If this reference is in an @implementation, check for 'private' methods.
298 if (!Getter)
299 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
300 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000301 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000302 Getter = ImpDecl->getClassMethod(Sel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000303
304 if (Getter) {
305 // FIXME: refactor/share with ActOnMemberReference().
306 // Check if we can reference this property.
307 if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
308 return ExprError();
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Steve Naroff61f72cb2009-03-09 21:12:44 +0000311 // Look for the matching setter, in case it is needed.
Mike Stump1eb44332009-09-09 15:08:12 +0000312 Selector SetterSel =
313 SelectorTable::constructSetterName(PP.getIdentifierTable(),
Steve Narofffdc92b72009-03-10 17:24:38 +0000314 PP.getSelectorTable(), &propertyName);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000316 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000317 if (!Setter) {
318 // If this reference is in an @implementation, also check for 'private'
319 // methods.
320 if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
321 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000322 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000323 Setter = ImpDecl->getClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000324 }
325 // Look through local category implementations associated with the class.
Argyrios Kyrtzidis1cb35dd2009-07-21 00:06:20 +0000326 if (!Setter)
327 Setter = IFace->getCategoryClassMethod(SetterSel);
Steve Naroff61f72cb2009-03-09 21:12:44 +0000328
329 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
330 return ExprError();
331
332 if (Getter || Setter) {
333 QualType PType;
334
335 if (Getter)
336 PType = Getter->getResultType();
337 else {
338 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
339 E = Setter->param_end(); PI != E; ++PI)
340 PType = (*PI)->getType();
341 }
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000342 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(
Mike Stump1eb44332009-09-09 15:08:12 +0000343 Getter, PType, Setter,
Steve Naroff61f72cb2009-03-09 21:12:44 +0000344 propertyNameLoc, IFace, receiverNameLoc));
345 }
346 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
347 << &propertyName << Context.getObjCInterfaceType(IFace));
348}
349
350
Chris Lattner85a932e2008-01-04 22:32:30 +0000351// ActOnClassMessage - used for both unary and keyword messages.
352// ArgExprs is optional - if it is present, the number of expressions
353// is obtained from Sel.getNumArgs().
354Sema::ExprResult Sema::ActOnClassMessage(
355 Scope *S,
356 IdentifierInfo *receiverName, Selector Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000357 SourceLocation lbrac, SourceLocation receiverLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000358 SourceLocation selectorLoc, SourceLocation rbrac,
359 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000360 assert(receiverName && "missing receiver class name");
361
362 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 ObjCInterfaceDecl* ClassDecl = 0;
Steve Narofffc93d522008-07-24 19:44:33 +0000364 bool isSuper = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner84692652008-11-20 05:35:30 +0000366 if (receiverName->isStr("super")) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000367 if (getCurMethodDecl()) {
368 isSuper = true;
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000369 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
370 if (!OID)
Mike Stump1eb44332009-09-09 15:08:12 +0000371 return Diag(lbrac, diag::error_no_super_class_message)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000372 << getCurMethodDecl()->getDeclName();
373 ClassDecl = OID->getSuperClass();
Steve Naroff5cb93b82008-11-19 15:54:23 +0000374 if (!ClassDecl)
Fariborz Jahanian4b1e2752009-01-07 21:01:41 +0000375 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000376 if (getCurMethodDecl()->isInstanceMethod()) {
Steve Naroff5cb93b82008-11-19 15:54:23 +0000377 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Steve Naroff14108da2009-07-10 23:34:53 +0000378 superTy = Context.getObjCObjectPointerType(superTy);
Ted Kremenek8189cde2009-02-07 01:47:29 +0000379 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
380 superTy);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000381 // We are really in an instance method, redirect.
Mike Stump1eb44332009-09-09 15:08:12 +0000382 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000383 selectorLoc, rbrac, Args, NumArgs);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000384 }
385 // We are sending a message to 'super' within a class method. Do nothing,
386 // the receiver will pass through as 'super' (how convenient:-).
387 } else {
388 // 'super' has been used outside a method context. If a variable named
389 // 'super' has been declared, redirect. If not, produce a diagnostic.
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000390 NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
Steve Naroff5cb93b82008-11-19 15:54:23 +0000391 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
392 if (VD) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
Ted Kremenek8189cde2009-02-07 01:47:29 +0000394 receiverLoc);
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 }
Chris Lattner08631c52008-11-23 21:45:46 +0000399 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
Mike Stump1eb44332009-09-09 15:08:12 +0000400 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000401 } else
402 ClassDecl = getObjCInterfaceDecl(receiverName);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Steve Naroff7c778f12008-07-25 19:39:00 +0000404 // The following code allows for the following GCC-ism:
Steve Naroffcb28be62008-06-04 23:08:38 +0000405 //
406 // typedef XCElementDisplayRect XCElementGraphicsRect;
407 //
408 // @implementation XCRASlice
409 // - whatever { // Note that XCElementGraphicsRect is a typedef name.
410 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
411 // }
412 //
Steve Naroff7c778f12008-07-25 19:39:00 +0000413 // If necessary, the following lookup could move to getObjCInterfaceDecl().
414 if (!ClassDecl) {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000415 NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
Steve Naroff7c778f12008-07-25 19:39:00 +0000416 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
417 const ObjCInterfaceType *OCIT;
418 OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
Chris Lattner64540d72009-03-29 05:01:10 +0000419 if (!OCIT) {
420 Diag(receiverLoc, diag::err_invalid_receiver_to_message);
421 return true;
422 }
Fariborz Jahanianebff1fe2009-01-16 20:35:09 +0000423 ClassDecl = OCIT->getDecl();
Steve Naroff7c778f12008-07-25 19:39:00 +0000424 }
425 }
426 assert(ClassDecl && "missing interface declaration");
Steve Naroffcb28be62008-06-04 23:08:38 +0000427 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000428 QualType returnType;
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000429 if (ClassDecl->isForwardDecl()) {
430 // A forward class used in messaging is tread as a 'Class'
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000431 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000432 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
433 if (Method)
Mike Stump1eb44332009-09-09 15:08:12 +0000434 Diag(Method->getLocation(), diag::note_method_sent_forward_class)
Fariborz Jahanian9f8f0262009-05-08 23:45:49 +0000435 << Method->getDeclName();
Fariborz Jahanian89bc3142009-05-08 23:02:36 +0000436 }
437 if (!Method)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000438 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Steve Naroff7c778f12008-07-25 19:39:00 +0000440 // If we have an implementation in scope, check "private" methods.
Steve Narofff1afaf62009-02-26 15:55:06 +0000441 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000442 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Steve Naroff7c778f12008-07-25 19:39:00 +0000443
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000444 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
445 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
447 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000448 lbrac, rbrac, returnType))
449 return true;
Ted Kremenek4df728e2008-06-24 15:50:53 +0000450
Anders Carlsson187ba152009-05-26 15:22:25 +0000451 returnType = returnType.getNonReferenceType();
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Mike Stump390b4cc2009-05-16 07:39:55 +0000453 // If we have the ObjCInterfaceDecl* for the class that is receiving the
454 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the
455 // IdentifierInfo* for the class.
456 // FIXME: need to do a better job handling 'super' usage within a class. For
457 // now, we simply pass the "super" identifier through (which isn't consistent
458 // with instance methods.
Steve Naroff7c778f12008-07-25 19:39:00 +0000459 if (isSuper)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000460 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
461 lbrac, rbrac, ArgExprs, NumArgs);
Ted Kremenek4df728e2008-06-24 15:50:53 +0000462 else
Ted Kremenek8189cde2009-02-07 01:47:29 +0000463 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
464 lbrac, rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000465}
466
467// ActOnInstanceMessage - used for both unary and keyword messages.
468// ArgExprs is optional - if it is present, the number of expressions
469// is obtained from Sel.getNumArgs().
Chris Lattner1565e032008-07-21 06:31:05 +0000470Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 SourceLocation lbrac,
Anders Carlssonff975cf2009-02-14 18:21:46 +0000472 SourceLocation receiverLoc,
Chris Lattnerb77792e2008-07-26 22:17:49 +0000473 SourceLocation rbrac,
474 ExprTy **Args, unsigned NumArgs) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000475 assert(receiver && "missing receiver expression");
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Chris Lattner85a932e2008-01-04 22:32:30 +0000477 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
478 Expr *RExpr = static_cast<Expr *>(receiver);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Chris Lattnerd0d45992009-04-29 05:48:32 +0000480 // If necessary, apply function/array conversion to the receiver.
481 // C99 6.7.5.3p[7,8].
482 DefaultFunctionArrayConversion(RExpr);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner85a932e2008-01-04 22:32:30 +0000484 QualType returnType;
Chris Lattnerb77792e2008-07-26 22:17:49 +0000485 QualType ReceiverCType =
486 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
Steve Naroff87d3ef02008-11-17 22:29:32 +0000487
488 // Handle messages to 'super'.
Steve Naroff279d8962009-02-23 15:40:48 +0000489 if (isa<ObjCSuperExpr>(RExpr)) {
Steve Naroff87d3ef02008-11-17 22:29:32 +0000490 ObjCMethodDecl *Method = 0;
491 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
492 // If we have an interface in scope, check 'super' methods.
493 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
Steve Naroff5609ec02009-03-08 18:56:13 +0000494 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000495 Method = SuperDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
497 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000498 // If we have implementations in scope, check "private" methods.
499 Method = LookupPrivateInstanceMethod(Sel, SuperDecl);
500 }
Steve Naroff87d3ef02008-11-17 22:29:32 +0000501 }
Anders Carlssonff975cf2009-02-14 18:21:46 +0000502
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000503 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
504 return true;
Anders Carlsson59843ad2009-02-14 19:08:58 +0000505
Chris Lattner077bf5e2008-11-24 03:33:13 +0000506 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Steve Naroff87d3ef02008-11-17 22:29:32 +0000507 lbrac, rbrac, returnType))
508 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Anders Carlsson187ba152009-05-26 15:22:25 +0000510 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000511 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
512 rbrac, ArgExprs, NumArgs);
Steve Naroff87d3ef02008-11-17 22:29:32 +0000513 }
514
Mike Stump1eb44332009-09-09 15:08:12 +0000515 // Handle messages to id.
Steve Naroff470301b2009-07-22 16:07:01 +0000516 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() ||
Fariborz Jahanian636bed12009-05-21 21:04:28 +0000517 Context.isObjCNSObjectType(RExpr->getType())) {
Steve Naroff037cda52008-09-30 14:38:43 +0000518 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
519 Sel, SourceRange(lbrac,rbrac));
Chris Lattner6e10a082008-02-01 06:57:39 +0000520 if (!Method)
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000521 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac));
Mike Stump1eb44332009-09-09 15:08:12 +0000522 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000523 lbrac, rbrac, returnType))
524 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000525 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000526 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
527 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000530 // Handle messages to Class.
Mike Stump1eb44332009-09-09 15:08:12 +0000531 if (ReceiverCType->isObjCClassType() ||
Steve Naroff470301b2009-07-22 16:07:01 +0000532 ReceiverCType->isObjCQualifiedClassType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000533 ObjCMethodDecl *Method = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattner6562fda2008-07-21 06:44:27 +0000535 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
Steve Naroffd526c2f2009-02-23 02:25:40 +0000536 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
537 // First check the public methods in the class interface.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000538 Method = ClassDecl->lookupClassMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Steve Narofff1afaf62009-02-26 15:55:06 +0000540 if (!Method)
Steve Naroff5609ec02009-03-08 18:56:13 +0000541 Method = LookupPrivateClassMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
543 // FIXME: if we still haven't found a method, we need to look in
Steve Naroff470301b2009-07-22 16:07:01 +0000544 // protocols (if we have qualifiers).
Steve Naroffd526c2f2009-02-23 02:25:40 +0000545 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000546 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
547 return true;
Steve Naroffd526c2f2009-02-23 02:25:40 +0000548 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000549 if (!Method) {
550 // If not messaging 'self', look for any factory method named 'Sel'.
551 if (!isSelfExpr(RExpr)) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +0000552 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000553 if (!Method) {
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000554 // If no class (factory) method was found, check if an _instance_
555 // method of the same name exists in the root class only.
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000556 Method = LookupInstanceMethodInGlobalPool(
557 Sel, SourceRange(lbrac,rbrac));
Fariborz Jahanian041f2fd2009-05-05 18:34:37 +0000558 if (Method)
559 if (const ObjCInterfaceDecl *ID =
560 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
561 if (ID->getSuperClass())
562 Diag(lbrac, diag::warn_root_inst_method_not_found)
563 << Sel << SourceRange(lbrac, rbrac);
564 }
Fariborz Jahanianb1006c72009-03-04 17:50:39 +0000565 }
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000566 }
567 }
Chris Lattner077bf5e2008-11-24 03:33:13 +0000568 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000569 lbrac, rbrac, returnType))
570 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000571 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000572 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
573 rbrac, ArgExprs, NumArgs);
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000576 ObjCMethodDecl *Method = 0;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000577 ObjCInterfaceDecl* ClassDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000580 // long as one of the protocols implements the selector (if not, warn).
Mike Stump1eb44332009-09-09 15:08:12 +0000581 if (const ObjCObjectPointerType *QIdTy =
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000582 ReceiverCType->getAsObjCQualifiedIdType()) {
Steve Narofff7f52e72009-02-21 21:17:01 +0000583 // Search protocols for instance methods.
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000584 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000585 E = QIdTy->qual_end(); I != E; ++I) {
586 ObjCProtocolDecl *PDecl = *I;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000587 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000588 break;
Steve Naroffebaa7682009-04-07 15:07:57 +0000589 // Since we aren't supporting "Class<foo>", look for a class method.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000590 if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
Steve Naroffebaa7682009-04-07 15:07:57 +0000591 break;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593 } else if (const ObjCObjectPointerType *OCIType =
Steve Naroff14108da2009-07-10 23:34:53 +0000594 ReceiverCType->getAsObjCInterfacePointerType()) {
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000595 // We allow sending a message to a pointer to an interface (an object).
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Steve Naroff14108da2009-07-10 23:34:53 +0000597 ClassDecl = OCIType->getInterfaceDecl();
Steve Naroff037cda52008-09-30 14:38:43 +0000598 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
Mike Stump390b4cc2009-05-16 07:39:55 +0000599 // faster than the following method (which can do *many* linear searches).
Steve Naroff037cda52008-09-30 14:38:43 +0000600 // The idea is to add class info to InstanceMethodPool.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601 Method = ClassDecl->lookupInstanceMethod(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000603 if (!Method) {
604 // Search protocol qualifiers.
Steve Naroff14108da2009-07-10 23:34:53 +0000605 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(),
Steve Naroff279d8962009-02-23 15:40:48 +0000606 E = OCIType->qual_end(); QI != E; ++QI) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000607 if ((Method = (*QI)->lookupInstanceMethod(Sel)))
Chris Lattner85a932e2008-01-04 22:32:30 +0000608 break;
609 }
Chris Lattner85a932e2008-01-04 22:32:30 +0000610 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000611 if (!Method) {
Steve Naroff5609ec02009-03-08 18:56:13 +0000612 // If we have implementations in scope, check "private" methods.
613 Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Steve Naroff5609ec02009-03-08 18:56:13 +0000615 if (!Method && !isSelfExpr(RExpr)) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000616 // If we still haven't found a method, look in the global pool. This
617 // behavior isn't very desirable, however we need it for GCC
618 // compatibility. FIXME: should we deviate??
Steve Naroff5609ec02009-03-08 18:56:13 +0000619 if (OCIType->qual_empty()) {
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000620 Method = LookupInstanceMethodInGlobalPool(
621 Sel, SourceRange(lbrac,rbrac));
Steve Naroff14108da2009-07-10 23:34:53 +0000622 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl())
Mike Stump1eb44332009-09-09 15:08:12 +0000623 Diag(lbrac, diag::warn_maynot_respond)
Steve Naroff14108da2009-07-10 23:34:53 +0000624 << OCIType->getInterfaceDecl()->getIdentifier()->getName() << Sel;
Steve Naroff6b9dfd42009-03-04 15:11:40 +0000625 }
Fariborz Jahanian268bc8c2009-03-03 22:19:15 +0000626 }
Steve Naroff0de21fd2009-02-22 19:35:57 +0000627 }
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000628 if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
629 return true;
Chris Lattner0c73f372009-03-09 21:19:16 +0000630 } else if (!Context.getObjCIdType().isNull() &&
631 (ReceiverCType->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000632 (ReceiverCType->isIntegerType() &&
Chris Lattner0c73f372009-03-09 21:19:16 +0000633 ReceiverCType->isScalarType()))) {
634 // Implicitly convert integers and pointers to 'id' but emit a warning.
Steve Naroff8e2945a2009-03-01 17:14:31 +0000635 Diag(lbrac, diag::warn_bad_receiver_type)
Chris Lattnerd1625842008-11-24 06:25:27 +0000636 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner0c73f372009-03-09 21:19:16 +0000637 ImpCastExprToType(RExpr, Context.getObjCIdType());
638 } else {
639 // Reject other random receiver types (e.g. structs).
640 Diag(lbrac, diag::err_bad_receiver_type)
641 << RExpr->getType() << RExpr->getSourceRange();
Chris Lattner2b1cc8b2008-07-21 06:12:56 +0000642 return true;
Chris Lattnerfe1a5532008-07-21 05:57:44 +0000643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000645 if (Method)
646 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs);
Chris Lattner077bf5e2008-11-24 03:33:13 +0000647 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
Daniel Dunbar637cebb2008-09-11 00:01:56 +0000648 lbrac, rbrac, returnType))
649 return true;
Anders Carlsson187ba152009-05-26 15:22:25 +0000650 returnType = returnType.getNonReferenceType();
Ted Kremenek8189cde2009-02-07 01:47:29 +0000651 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
652 rbrac, ArgExprs, NumArgs);
Chris Lattner85a932e2008-01-04 22:32:30 +0000653}
Chris Lattnereca7be62008-04-07 05:30:13 +0000654