blob: 79b50114b771407ae9505a3586b3c52e80829c3f [file] [log] [blame]
Anders Carlssona66cad42007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlssona66cad42007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekfa4ebab2008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlssona66cad42007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbare6c31752008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner8c7c6a12008-06-17 18:05:57 +000019
Anders Carlssona66cad42007-08-21 17:43:55 +000020using namespace clang;
21using namespace CodeGen;
22
Chris Lattner6ee20e32008-06-24 17:04:18 +000023/// Emits an instance of NSConstantString representing the object.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000024llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
25 std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
26 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000027 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000028 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner6ee20e32008-06-24 17:04:18 +000029}
30
31/// Emit a selector.
32llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
33 // Untyped selector.
34 // Note that this implementation allows for non-constant strings to be passed
35 // as arguments to @selector(). Currently, the only thing preventing this
36 // behaviour is the type checking in the front end.
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000037 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner6ee20e32008-06-24 17:04:18 +000038}
39
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000040llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
41 // FIXME: This should pass the Decl not the name.
42 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
43}
Chris Lattner6ee20e32008-06-24 17:04:18 +000044
45
Daniel Dunbara04840b2008-08-23 03:46:30 +000046RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-06-24 17:04:18 +000047 // Only the lookup mechanism and first two arguments of the method
48 // implementation vary between runtimes. We can get the receiver and
49 // arguments in generic code.
50
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000051 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner6ee20e32008-06-24 17:04:18 +000052 const Expr *ReceiverExpr = E->getReceiver();
53 bool isSuperMessage = false;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000054 bool isClassMessage = false;
Chris Lattner6ee20e32008-06-24 17:04:18 +000055 // Find the receiver
56 llvm::Value *Receiver;
57 if (!ReceiverExpr) {
Daniel Dunbar434627a2008-08-16 00:25:02 +000058 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
59
60 // Very special case, super send in class method. The receiver is
61 // self (the class object) and the send uses super semantics.
62 if (!OID) {
63 assert(!strcmp(E->getClassName()->getName(), "super") &&
64 "Unexpected missing class interface in message send.");
Daniel Dunbar434627a2008-08-16 00:25:02 +000065 isSuperMessage = true;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000066 Receiver = LoadObjCSelf();
67 } else {
68 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner6ee20e32008-06-24 17:04:18 +000069 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000070
71 isClassMessage = true;
Chris Lattner69909292008-08-10 01:53:14 +000072 } else if (const PredefinedExpr *PDE =
73 dyn_cast<PredefinedExpr>(E->getReceiver())) {
74 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner6ee20e32008-06-24 17:04:18 +000075 isSuperMessage = true;
76 Receiver = LoadObjCSelf();
77 } else {
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000078 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner6ee20e32008-06-24 17:04:18 +000079 }
80
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000081 CallArgList Args;
82 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
83 i != e; ++i)
84 EmitCallArg(*i, Args);
85
Chris Lattner6ee20e32008-06-24 17:04:18 +000086 if (isSuperMessage) {
Chris Lattner8384c142008-06-26 04:42:20 +000087 // super is only valid in an Objective-C method
88 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbardd851282008-08-30 05:35:15 +000089 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
90 E->getSelector(),
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000091 OMD->getClassInterface(),
92 Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +000093 isClassMessage,
94 Args);
Chris Lattner6ee20e32008-06-24 17:04:18 +000095 }
Daniel Dunbardd851282008-08-30 05:35:15 +000096 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
97 Receiver, isClassMessage, Args);
Anders Carlssona66cad42007-08-21 17:43:55 +000098}
99
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000100/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
101/// the LLVM function and sets the other context used by
102/// CodeGenFunction.
103
104// FIXME: This should really be merged with GenerateCode.
105void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000106 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000107 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
108
109 // Create a marker to make it easy to insert allocas into the entryblock
110 // later. Don't create this with the builder, because we don't want it
111 // folded.
112 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
113 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
114 EntryBB);
115
116 FnRetTy = OMD->getResultType();
117 CurFuncDecl = OMD;
118
119 Builder.SetInsertPoint(EntryBB);
120
121 // Emit allocs for param decls. Give the LLVM Argument nodes names.
122 llvm::Function::arg_iterator AI = CurFn->arg_begin();
123
Daniel Dunbarace33292008-08-16 03:19:19 +0000124 // Name the struct return argument.
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000125 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarace33292008-08-16 03:19:19 +0000126 AI->setName("agg.result");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000127 ++AI;
128 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000129
Daniel Dunbarace33292008-08-16 03:19:19 +0000130 // Add implicit parameters to the decl map.
131 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000132 ++AI;
Daniel Dunbarace33292008-08-16 03:19:19 +0000133
134 EmitParmDecl(*OMD->getCmdDecl(), AI);
135 ++AI;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000136
137 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
138 assert(AI != CurFn->arg_end() && "Argument mismatch!");
139 EmitParmDecl(*OMD->getParamDecl(i), AI);
140 }
Daniel Dunbarace33292008-08-16 03:19:19 +0000141 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000142}
Daniel Dunbarace33292008-08-16 03:19:19 +0000143
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000144/// Generate an Objective-C method. An Objective-C method is a C function with
145/// its pointer, name, and types registered in the class struture.
146void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
147 StartObjCMethod(OMD);
148 EmitStmt(OMD->getBody());
149
150 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
151 if (S) {
152 FinishFunction(S->getRBracLoc());
153 } else {
154 FinishFunction();
155 }
156}
157
158// FIXME: I wasn't sure about the synthesis approach. If we end up
159// generating an AST for the whole body we can just fall back to
160// having a GenerateFunction which takes the body Stmt.
161
162/// GenerateObjCGetter - Generate an Objective-C property getter
163/// function. The given Decl must be either an ObjCCategoryImplDecl
164/// or an ObjCImplementationDecl.
165void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
166 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
167 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
168 assert(OMD && "Invalid call to generate getter (empty method)");
169 // FIXME: This is rather murky, we create this here since they will
170 // not have been created by Sema for us.
171 OMD->createImplicitParams(getContext());
172 StartObjCMethod(OMD);
173
174 // FIXME: What about nonatomic?
175 SourceLocation Loc = PD->getLocation();
176 ValueDecl *Self = OMD->getSelfDecl();
177 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
178 DeclRefExpr Base(Self, Self->getType(), Loc);
179 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
180 true, true);
181 ReturnStmt Return(Loc, &IvarRef);
182 EmitStmt(&Return);
183
184 FinishFunction();
185}
186
187/// GenerateObjCSetter - Generate an Objective-C property setter
188/// function. The given Decl must be either an ObjCCategoryImplDecl
189/// or an ObjCImplementationDecl.
190void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
191 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
192 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
193 assert(OMD && "Invalid call to generate setter (empty method)");
194 // FIXME: This is rather murky, we create this here since they will
195 // not have been created by Sema for us.
196 OMD->createImplicitParams(getContext());
197 StartObjCMethod(OMD);
198
199 switch (PD->getSetterKind()) {
200 case ObjCPropertyDecl::Assign: break;
201 case ObjCPropertyDecl::Copy:
202 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
203 break;
204 case ObjCPropertyDecl::Retain:
205 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
206 break;
207 }
208
209 // FIXME: What about nonatomic?
210 SourceLocation Loc = PD->getLocation();
211 ValueDecl *Self = OMD->getSelfDecl();
212 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
213 DeclRefExpr Base(Self, Self->getType(), Loc);
214 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
215 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
216 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
217 true, true);
218 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
219 Ivar->getType(), Loc);
220 EmitStmt(&Assign);
221
222 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000223}
224
Daniel Dunbarace33292008-08-16 03:19:19 +0000225llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
226 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
227 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000228}
229
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000230RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
231 // Determine getter selector.
232 Selector S;
233 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) {
234 S = MD->getSelector();
235 } else {
236 S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName();
237 }
238
Daniel Dunbardd851282008-08-30 05:35:15 +0000239 return CGM.getObjCRuntime().
240 GenerateMessageSend(*this, E->getType(), S,
241 EmitScalarExpr(E->getBase()),
242 false, CallArgList());
Daniel Dunbar91cc4022008-08-27 06:57:25 +0000243}
244
Daniel Dunbare6c31752008-08-29 08:11:39 +0000245void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
246 RValue Src) {
Daniel Dunbardd851282008-08-30 05:35:15 +0000247 Selector S;
248 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(E->getDecl())) {
249 S = PD->getSetterName();
250 } else {
251 // FIXME: How can we have a method decl here?
252 ErrorUnsupported(E, "Objective-C property setter call");
253 return;
254 }
255
256 CallArgList Args;
257 EmitCallArg(Src, E->getType(), Args);
258 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
259 EmitScalarExpr(E->getBase()),
260 false, Args);
Daniel Dunbare6c31752008-08-29 08:11:39 +0000261}
262
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000263CGObjCRuntime::~CGObjCRuntime() {}