blob: e976876c2b3ce9668d04370b990c52a66b7b4b43 [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"
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
51 if (Context.getObjcConstantStringInterface().isNull()) {
52 // 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);
57 ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace);
58 if (!strIFace)
59 return Diag(S->getLocStart(), diag::err_undef_interface,
60 NSIdent->getName());
61 Context.setObjcConstantStringInterface(strIFace);
62 }
63 QualType t = Context.getObjcConstantStringInterface();
64 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) {
84 QualType t = Context.getObjcSelType();
85 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) {
93 ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId];
94 if (!PDecl) {
95 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
96 return true;
97 }
98
99 QualType t = Context.getObjcProtoType();
100 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,
107 ObjcMethodDecl *Method) {
108 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);
146 ObjcInterfaceDecl* ClassDecl = 0;
147 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);
154 QualType superTy = Context.getObjcInterfaceType(ClassDecl);
155 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?
168 ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
169 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));
177 returnType = Context.getObjcIdType();
178 } 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;
202 ObjcMethodDecl *Method = 0;
203
204 if (receiverType == Context.getObjcIdType() ||
205 receiverType == Context.getObjcClassType()) {
206 Method = InstanceMethodPool[Sel].Method;
207 if (!Method)
208 Method = FactoryMethodPool[Sel].Method;
209 if (!Method) {
210 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
211 SourceRange(lbrac, rbrac));
212 returnType = Context.getObjcIdType();
213 } else {
214 returnType = Method->getResultType();
215 if (Sel.getNumArgs())
216 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
217 return true;
218 }
219 } else {
220 bool receiverIsQualId = dyn_cast<ObjcQualifiedIdType>(receiverType) != 0;
221 // FIXME (snaroff): checking in this code from Patrick. Needs to be
222 // revisited. how do we get the ClassDecl from the receiver expression?
223 if (!receiverIsQualId)
224 while (receiverType->isPointerType()) {
225 PointerType *pointerType =
226 static_cast<PointerType*>(receiverType.getTypePtr());
227 receiverType = pointerType->getPointeeType();
228 }
229 ObjcInterfaceDecl* ClassDecl = 0;
230 if (ObjcQualifiedInterfaceType *QIT =
231 dyn_cast<ObjcQualifiedInterfaceType>(receiverType)) {
232 ClassDecl = QIT->getDecl();
233 Method = ClassDecl->lookupInstanceMethod(Sel);
234 if (!Method) {
235 // search protocols
236 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
237 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
238 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
239 break;
240 }
241 }
242 if (!Method)
243 Diag(lbrac, diag::warn_method_not_found_in_protocol,
244 std::string("-"), Sel.getName(),
245 SourceRange(lbrac, rbrac));
246 }
247 else if (ObjcQualifiedIdType *QIT =
248 dyn_cast<ObjcQualifiedIdType>(receiverType)) {
249 // search protocols
250 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
251 ObjcProtocolDecl *PDecl = QIT->getProtocols(i);
252 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
253 break;
254 }
255 if (!Method)
256 Diag(lbrac, diag::warn_method_not_found_in_protocol,
257 std::string("-"), Sel.getName(),
258 SourceRange(lbrac, rbrac));
259 }
260 else {
261 assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) &&
262 "bad receiver type");
263 ClassDecl = static_cast<ObjcInterfaceType*>(
264 receiverType.getTypePtr())->getDecl();
265 // FIXME: consider using InstanceMethodPool, since it will be faster
266 // than the following method (which can do *many* linear searches). The
267 // idea is to add class info to InstanceMethodPool...
268 Method = ClassDecl->lookupInstanceMethod(Sel);
269 }
270 if (!Method) {
271 // If we have an implementation in scope, check "private" methods.
272 if (ClassDecl)
273 if (ObjcImplementationDecl *ImpDecl =
274 ObjcImplementations[ClassDecl->getIdentifier()])
275 Method = ImpDecl->getInstanceMethod(Sel);
276 // If we still haven't found a method, look in the global pool. This
277 // behavior isn't very desirable, however we need it for GCC
278 // compatibility.
279 if (!Method)
280 Method = InstanceMethodPool[Sel].Method;
281 }
282 if (!Method) {
283 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
284 SourceRange(lbrac, rbrac));
285 returnType = Context.getObjcIdType();
286 } else {
287 returnType = Method->getResultType();
288 if (Sel.getNumArgs())
289 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
290 return true;
291 }
292 }
293 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
294 ArgExprs, NumArgs);
295}