blob: 0f53bbd804c958fc0828322205df0941f449ffa5 [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"
17#include "clang/AST/Expr.h"
18using namespace clang;
19
20Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
21 ExprTy **Strings,
22 unsigned NumStrings) {
23 SourceLocation AtLoc = AtLocs[0];
24 StringLiteral* S = static_cast<StringLiteral *>(Strings[0]);
25 if (NumStrings > 1) {
26 // Concatenate objc strings.
27 StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]);
28 SourceLocation EndLoc = ES->getSourceRange().getEnd();
29 unsigned Length = 0;
30 for (unsigned i = 0; i < NumStrings; i++)
31 Length += static_cast<StringLiteral *>(Strings[i])->getByteLength();
32 char *strBuf = new char [Length];
33 char *p = strBuf;
34 bool isWide = false;
35 for (unsigned i = 0; i < NumStrings; i++) {
36 S = static_cast<StringLiteral *>(Strings[i]);
37 if (S->isWide())
38 isWide = true;
39 memcpy(p, S->getStrData(), S->getByteLength());
40 p += S->getByteLength();
41 delete S;
42 }
43 S = new StringLiteral(strBuf, Length,
44 isWide, Context.getPointerType(Context.CharTy),
45 AtLoc, EndLoc);
46 }
47
48 if (CheckBuiltinCFStringArgument(S))
49 return true;
50
Ted Kremenek42730c52008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner09493052008-01-04 22:32:30 +000052 // Initialize the constant string interface lazily. This assumes
53 // the NSConstantString interface is seen in this translation unit.
54 IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
55 ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary,
56 SourceLocation(), TUScope);
Ted Kremenek42730c52008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner09493052008-01-04 22:32:30 +000058 if (!strIFace)
59 return Diag(S->getLocStart(), diag::err_undef_interface,
60 NSIdent->getName());
Ted Kremenek42730c52008-01-07 19:49:32 +000061 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner09493052008-01-04 22:32:30 +000062 }
Ted Kremenek42730c52008-01-07 19:49:32 +000063 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner09493052008-01-04 22:32:30 +000064 t = Context.getPointerType(t);
65 return new ObjCStringLiteral(S, t, AtLoc);
66}
67
68Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
69 SourceLocation EncodeLoc,
70 SourceLocation LParenLoc,
71 TypeTy *Ty,
72 SourceLocation RParenLoc) {
73 QualType EncodedType = QualType::getFromOpaquePtr(Ty);
74
75 QualType t = Context.getPointerType(Context.CharTy);
76 return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc);
77}
78
79Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
80 SourceLocation AtLoc,
81 SourceLocation SelLoc,
82 SourceLocation LParenLoc,
83 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000084 QualType t = Context.getObjCSelType();
Chris Lattner09493052008-01-04 22:32:30 +000085 return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc);
86}
87
88Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
89 SourceLocation AtLoc,
90 SourceLocation ProtoLoc,
91 SourceLocation LParenLoc,
92 SourceLocation RParenLoc) {
Ted Kremenek42730c52008-01-07 19:49:32 +000093 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner09493052008-01-04 22:32:30 +000094 if (!PDecl) {
95 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
96 return true;
97 }
98
Ted Kremenek42730c52008-01-07 19:49:32 +000099 QualType t = Context.getObjCProtoType();
Chris Lattner09493052008-01-04 22:32:30 +0000100 if (t.isNull())
101 return true;
102 t = Context.getPointerType(t);
103 return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc);
104}
105
106bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
Ted Kremenek42730c52008-01-07 19:49:32 +0000107 ObjCMethodDecl *Method) {
Chris Lattner09493052008-01-04 22:32:30 +0000108 bool anyIncompatibleArgs = false;
109
110 for (unsigned i = 0; i < NumArgs; i++) {
111 Expr *argExpr = Args[i];
112 assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
113
114 QualType lhsType = Method->getParamDecl(i)->getType();
115 QualType rhsType = argExpr->getType();
116
117 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
118 if (const ArrayType *ary = lhsType->getAsArrayType())
119 lhsType = Context.getPointerType(ary->getElementType());
120 else if (lhsType->isFunctionType())
121 lhsType = Context.getPointerType(lhsType);
122
123 AssignConvertType Result = CheckSingleAssignmentConstraints(lhsType,
124 argExpr);
125 if (Args[i] != argExpr) // The expression was converted.
126 Args[i] = argExpr; // Make sure we store the converted expression.
127
128 anyIncompatibleArgs |=
129 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
130 argExpr, "sending");
131 }
132 return anyIncompatibleArgs;
133}
134
135// ActOnClassMessage - used for both unary and keyword messages.
136// ArgExprs is optional - if it is present, the number of expressions
137// is obtained from Sel.getNumArgs().
138Sema::ExprResult Sema::ActOnClassMessage(
139 Scope *S,
140 IdentifierInfo *receiverName, Selector Sel,
141 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
142{
143 assert(receiverName && "missing receiver class name");
144
145 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000147 if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) {
148 ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass();
149 if (ClassDecl && CurMethodDecl->isInstance()) {
150 // Synthesize a cast to the super class. This hack allows us to loosely
151 // represent super without creating a special expression node.
152 IdentifierInfo &II = Context.Idents.get("self");
153 ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false);
Ted Kremenek42730c52008-01-07 19:49:32 +0000154 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner09493052008-01-04 22:32:30 +0000155 superTy = Context.getPointerType(superTy);
156 ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(),
157 SourceLocation(), ReceiverExpr.Val);
158 // We are really in an instance method, redirect.
159 return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac,
160 Args, NumArgs);
161 }
162 // We are sending a message to 'super' within a class method. Do nothing,
163 // the receiver will pass through as 'super' (how convenient:-).
164 } else
165 ClassDecl = getObjCInterfaceDecl(receiverName);
166
167 // FIXME: can ClassDecl ever be null?
Ted Kremenek42730c52008-01-07 19:49:32 +0000168 ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Chris Lattner09493052008-01-04 22:32:30 +0000169 QualType returnType;
170
171 // Before we give up, check if the selector is an instance method.
172 if (!Method)
173 Method = ClassDecl->lookupInstanceMethod(Sel);
174 if (!Method) {
175 Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(),
176 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000177 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000178 } else {
179 returnType = Method->getResultType();
180 if (Sel.getNumArgs()) {
181 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
182 return true;
183 }
184 }
185 return new ObjCMessageExpr(receiverName, Sel, returnType, Method,
186 lbrac, rbrac, ArgExprs, NumArgs);
187}
188
189// ActOnInstanceMessage - used for both unary and keyword messages.
190// ArgExprs is optional - if it is present, the number of expressions
191// is obtained from Sel.getNumArgs().
192Sema::ExprResult Sema::ActOnInstanceMessage(
193 ExprTy *receiver, Selector Sel,
194 SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs)
195{
196 assert(receiver && "missing receiver expression");
197
198 Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
199 Expr *RExpr = static_cast<Expr *>(receiver);
200 QualType receiverType = RExpr->getType();
201 QualType returnType;
Ted Kremenek42730c52008-01-07 19:49:32 +0000202 ObjCMethodDecl *Method = 0;
Chris Lattner09493052008-01-04 22:32:30 +0000203
Chris Lattnere1b957d2008-02-01 06:43:02 +0000204 // FIXME:
205 // FIXME: This code is not stripping off type qualifiers or typedefs!
206 // FIXME:
Ted Kremenek42730c52008-01-07 19:49:32 +0000207 if (receiverType == Context.getObjCIdType() ||
208 receiverType == Context.getObjCClassType()) {
Chris Lattner09493052008-01-04 22:32:30 +0000209 Method = InstanceMethodPool[Sel].Method;
210 if (!Method)
211 Method = FactoryMethodPool[Sel].Method;
212 if (!Method) {
213 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
214 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000215 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000216 } else {
217 returnType = Method->getResultType();
218 if (Sel.getNumArgs())
219 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
220 return true;
221 }
222 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000223 bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType);
Chris Lattner09493052008-01-04 22:32:30 +0000224 // FIXME (snaroff): checking in this code from Patrick. Needs to be
225 // revisited. how do we get the ClassDecl from the receiver expression?
226 if (!receiverIsQualId)
Chris Lattnere1b957d2008-02-01 06:43:02 +0000227 while (const PointerType *PTy = receiverType->getAsPointerType())
228 receiverType = PTy->getPointeeType();
229
Ted Kremenek42730c52008-01-07 19:49:32 +0000230 ObjCInterfaceDecl* ClassDecl = 0;
231 if (ObjCQualifiedInterfaceType *QIT =
232 dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) {
Chris Lattner09493052008-01-04 22:32:30 +0000233 ClassDecl = QIT->getDecl();
234 Method = ClassDecl->lookupInstanceMethod(Sel);
235 if (!Method) {
236 // search protocols
237 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000238 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner09493052008-01-04 22:32:30 +0000239 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
240 break;
241 }
242 }
243 if (!Method)
244 Diag(lbrac, diag::warn_method_not_found_in_protocol,
245 std::string("-"), Sel.getName(),
246 SourceRange(lbrac, rbrac));
247 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000248 else if (ObjCQualifiedIdType *QIT =
249 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner09493052008-01-04 22:32:30 +0000250 // search protocols
251 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000252 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner09493052008-01-04 22:32:30 +0000253 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
254 break;
255 }
256 if (!Method)
257 Diag(lbrac, diag::warn_method_not_found_in_protocol,
258 std::string("-"), Sel.getName(),
259 SourceRange(lbrac, rbrac));
260 }
261 else {
Chris Lattnere1b957d2008-02-01 06:43:02 +0000262 ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType);
263 if (OCIReceiver == 0) {
Fariborz Jahanianbc662012008-01-25 17:43:39 +0000264 Diag(lbrac, diag::error_bad_receiver_type, receiverType.getAsString());
265 return true;
266 }
Chris Lattnere1b957d2008-02-01 06:43:02 +0000267 ClassDecl = OCIReceiver->getDecl();
Chris Lattner09493052008-01-04 22:32:30 +0000268 // FIXME: consider using InstanceMethodPool, since it will be faster
269 // than the following method (which can do *many* linear searches). The
270 // idea is to add class info to InstanceMethodPool...
271 Method = ClassDecl->lookupInstanceMethod(Sel);
272 }
273 if (!Method) {
274 // If we have an implementation in scope, check "private" methods.
275 if (ClassDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +0000276 if (ObjCImplementationDecl *ImpDecl =
277 ObjCImplementations[ClassDecl->getIdentifier()])
Chris Lattner09493052008-01-04 22:32:30 +0000278 Method = ImpDecl->getInstanceMethod(Sel);
279 // If we still haven't found a method, look in the global pool. This
280 // behavior isn't very desirable, however we need it for GCC
281 // compatibility.
282 if (!Method)
283 Method = InstanceMethodPool[Sel].Method;
284 }
285 if (!Method) {
286 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
287 SourceRange(lbrac, rbrac));
Ted Kremenek42730c52008-01-07 19:49:32 +0000288 returnType = Context.getObjCIdType();
Chris Lattner09493052008-01-04 22:32:30 +0000289 } else {
290 returnType = Method->getResultType();
291 if (Sel.getNumArgs())
292 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
293 return true;
294 }
295 }
296 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
297 ArgExprs, NumArgs);
298}