blob: 4cf435c331529774fb87a549e285e993a71f71c5 [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
Ted Kremeneka526c5c2008-01-07 19:49:32 +000051 if (Context.getObjCConstantStringInterface().isNull()) {
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +000057 ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000058 if (!strIFace)
59 return Diag(S->getLocStart(), diag::err_undef_interface,
60 NSIdent->getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +000061 Context.setObjCConstantStringInterface(strIFace);
Chris Lattner85a932e2008-01-04 22:32:30 +000062 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +000063 QualType t = Context.getObjCConstantStringInterface();
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +000084 QualType t = Context.getObjCSelType();
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +000093 ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
Chris Lattner85a932e2008-01-04 22:32:30 +000094 if (!PDecl) {
95 Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName());
96 return true;
97 }
98
Ted Kremeneka526c5c2008-01-07 19:49:32 +000099 QualType t = Context.getObjCProtoType();
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000107 ObjCMethodDecl *Method) {
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000146 ObjCInterfaceDecl* ClassDecl = 0;
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000154 QualType superTy = Context.getObjCInterfaceType(ClassDecl);
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000168 ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel);
Chris Lattner85a932e2008-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 Kremeneka526c5c2008-01-07 19:49:32 +0000177 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-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);
Chris Lattner6e10a082008-02-01 06:57:39 +0000200 QualType receiverType = RExpr->getType().getCanonicalType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000201 QualType returnType;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000202 ObjCMethodDecl *Method = 0;
Chris Lattner85a932e2008-01-04 22:32:30 +0000203
Chris Lattner6e10a082008-02-01 06:57:39 +0000204 // FIXME: This code is not stripping off type qualifiers! Should it?
205 if (receiverType == Context.getObjCIdType().getCanonicalType() ||
206 receiverType == Context.getObjCClassType().getCanonicalType()) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000207 Method = InstanceMethodPool[Sel].Method;
Chris Lattner6e10a082008-02-01 06:57:39 +0000208 if (!Method)
209 Method = FactoryMethodPool[Sel].Method;
Chris Lattner85a932e2008-01-04 22:32:30 +0000210 if (!Method) {
211 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
212 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000213 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000214 } else {
215 returnType = Method->getResultType();
216 if (Sel.getNumArgs())
217 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
218 return true;
219 }
220 } else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000221 bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType);
Chris Lattner85a932e2008-01-04 22:32:30 +0000222 // FIXME (snaroff): checking in this code from Patrick. Needs to be
223 // revisited. how do we get the ClassDecl from the receiver expression?
224 if (!receiverIsQualId)
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000225 while (const PointerType *PTy = receiverType->getAsPointerType())
226 receiverType = PTy->getPointeeType();
227
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000228 ObjCInterfaceDecl* ClassDecl = 0;
229 if (ObjCQualifiedInterfaceType *QIT =
230 dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000231 ClassDecl = QIT->getDecl();
232 Method = ClassDecl->lookupInstanceMethod(Sel);
233 if (!Method) {
234 // search protocols
235 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000236 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner85a932e2008-01-04 22:32:30 +0000237 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
238 break;
239 }
240 }
241 if (!Method)
242 Diag(lbrac, diag::warn_method_not_found_in_protocol,
243 std::string("-"), Sel.getName(),
244 SourceRange(lbrac, rbrac));
245 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000246 else if (ObjCQualifiedIdType *QIT =
247 dyn_cast<ObjCQualifiedIdType>(receiverType)) {
Chris Lattner85a932e2008-01-04 22:32:30 +0000248 // search protocols
249 for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000250 ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
Chris Lattner85a932e2008-01-04 22:32:30 +0000251 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
252 break;
253 }
254 if (!Method)
255 Diag(lbrac, diag::warn_method_not_found_in_protocol,
256 std::string("-"), Sel.getName(),
257 SourceRange(lbrac, rbrac));
258 }
259 else {
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000260 ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType);
261 if (OCIReceiver == 0) {
Chris Lattner6e10a082008-02-01 06:57:39 +0000262 Diag(lbrac, diag::error_bad_receiver_type,
263 RExpr->getType().getAsString());
Fariborz Jahanian1dad5b22008-01-25 17:43:39 +0000264 return true;
265 }
Chris Lattnerfb8cc1d2008-02-01 06:43:02 +0000266 ClassDecl = OCIReceiver->getDecl();
Chris Lattner85a932e2008-01-04 22:32:30 +0000267 // FIXME: consider using InstanceMethodPool, since it will be faster
268 // than the following method (which can do *many* linear searches). The
269 // idea is to add class info to InstanceMethodPool...
270 Method = ClassDecl->lookupInstanceMethod(Sel);
271 }
272 if (!Method) {
273 // If we have an implementation in scope, check "private" methods.
274 if (ClassDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000275 if (ObjCImplementationDecl *ImpDecl =
276 ObjCImplementations[ClassDecl->getIdentifier()])
Chris Lattner85a932e2008-01-04 22:32:30 +0000277 Method = ImpDecl->getInstanceMethod(Sel);
Chris Lattnerc81c8142008-02-25 21:04:36 +0000278 // If we still haven't found a method, look in the global pool. This
279 // behavior isn't very desirable, however we need it for GCC
Chris Lattner85a932e2008-01-04 22:32:30 +0000280 // compatibility.
Chris Lattnerc81c8142008-02-25 21:04:36 +0000281 if (!Method)
282 Method = InstanceMethodPool[Sel].Method;
Chris Lattner85a932e2008-01-04 22:32:30 +0000283 }
284 if (!Method) {
285 Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(),
286 SourceRange(lbrac, rbrac));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000287 returnType = Context.getObjCIdType();
Chris Lattner85a932e2008-01-04 22:32:30 +0000288 } else {
289 returnType = Method->getResultType();
290 if (Sel.getNumArgs())
291 if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method))
292 return true;
293 }
294 }
295 return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac,
296 ArgExprs, NumArgs);
297}