blob: 2a808f8e0598f55ad497d60ba4cb3a0a8fb772c5 [file] [log] [blame]
Anders Carlsson55085182007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Carlsson55085182007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek2979ec72008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson55085182007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner41110242008-06-17 18:05:57 +000019
Anders Carlsson55085182007-08-21 17:43:55 +000020using namespace clang;
21using namespace CodeGen;
22
Chris Lattner8fdf3282008-06-24 17:04:18 +000023/// Emits an instance of NSConstantString representing the object.
Daniel Dunbarbbce49b2008-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 Dunbared7c6182008-08-20 00:28:19 +000027 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000028 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000037 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000038}
39
Daniel Dunbared7c6182008-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 Lattner8fdf3282008-06-24 17:04:18 +000044
45
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000046RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-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 Dunbar208ff5e2008-08-11 18:12:00 +000051 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000052 const Expr *ReceiverExpr = E->getReceiver();
53 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000054 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000055 // Find the receiver
56 llvm::Value *Receiver;
57 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-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 Dunbarddb2a3d2008-08-16 00:25:02 +000065 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000066 Receiver = LoadObjCSelf();
67 } else {
68 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000069 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000070
71 isClassMessage = true;
Chris Lattnerd9f69102008-08-10 01:53:14 +000072 } else if (const PredefinedExpr *PDE =
73 dyn_cast<PredefinedExpr>(E->getReceiver())) {
74 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner8fdf3282008-06-24 17:04:18 +000075 isSuperMessage = true;
76 Receiver = LoadObjCSelf();
77 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000078 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000079 }
80
Daniel Dunbar19cd87e2008-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 Lattner8fdf3282008-06-24 17:04:18 +000086 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000087 // super is only valid in an Objective-C method
88 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000089 return Runtime.GenerateMessageSendSuper(*this, E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000090 OMD->getClassInterface(),
91 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000092 isClassMessage,
93 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000094 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000095 return Runtime.GenerateMessageSend(*this, E, Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +000096}
97
Daniel Dunbaraf05bb92008-08-26 08:29:31 +000098/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
99/// the LLVM function and sets the other context used by
100/// CodeGenFunction.
101
102// FIXME: This should really be merged with GenerateCode.
103void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000104 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner41110242008-06-17 18:05:57 +0000105 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
106
107 // Create a marker to make it easy to insert allocas into the entryblock
108 // later. Don't create this with the builder, because we don't want it
109 // folded.
110 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
111 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
112 EntryBB);
113
114 FnRetTy = OMD->getResultType();
115 CurFuncDecl = OMD;
116
117 Builder.SetInsertPoint(EntryBB);
118
119 // Emit allocs for param decls. Give the LLVM Argument nodes names.
120 llvm::Function::arg_iterator AI = CurFn->arg_begin();
121
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000122 // Name the struct return argument.
Chris Lattner41110242008-06-17 18:05:57 +0000123 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000124 AI->setName("agg.result");
Chris Lattner41110242008-06-17 18:05:57 +0000125 ++AI;
126 }
Chris Lattner41110242008-06-17 18:05:57 +0000127
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000128 // Add implicit parameters to the decl map.
129 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner41110242008-06-17 18:05:57 +0000130 ++AI;
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000131
132 EmitParmDecl(*OMD->getCmdDecl(), AI);
133 ++AI;
Chris Lattner41110242008-06-17 18:05:57 +0000134
135 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
136 assert(AI != CurFn->arg_end() && "Argument mismatch!");
137 EmitParmDecl(*OMD->getParamDecl(i), AI);
138 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000139 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000140}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000141
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000142/// Generate an Objective-C method. An Objective-C method is a C function with
143/// its pointer, name, and types registered in the class struture.
144void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
145 StartObjCMethod(OMD);
146 EmitStmt(OMD->getBody());
147
148 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
149 if (S) {
150 FinishFunction(S->getRBracLoc());
151 } else {
152 FinishFunction();
153 }
154}
155
156// FIXME: I wasn't sure about the synthesis approach. If we end up
157// generating an AST for the whole body we can just fall back to
158// having a GenerateFunction which takes the body Stmt.
159
160/// GenerateObjCGetter - Generate an Objective-C property getter
161/// function. The given Decl must be either an ObjCCategoryImplDecl
162/// or an ObjCImplementationDecl.
163void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
164 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
165 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
166 assert(OMD && "Invalid call to generate getter (empty method)");
167 // FIXME: This is rather murky, we create this here since they will
168 // not have been created by Sema for us.
169 OMD->createImplicitParams(getContext());
170 StartObjCMethod(OMD);
171
172 // FIXME: What about nonatomic?
173 SourceLocation Loc = PD->getLocation();
174 ValueDecl *Self = OMD->getSelfDecl();
175 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
176 DeclRefExpr Base(Self, Self->getType(), Loc);
177 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
178 true, true);
179 ReturnStmt Return(Loc, &IvarRef);
180 EmitStmt(&Return);
181
182 FinishFunction();
183}
184
185/// GenerateObjCSetter - Generate an Objective-C property setter
186/// function. The given Decl must be either an ObjCCategoryImplDecl
187/// or an ObjCImplementationDecl.
188void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
189 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
190 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
191 assert(OMD && "Invalid call to generate setter (empty method)");
192 // FIXME: This is rather murky, we create this here since they will
193 // not have been created by Sema for us.
194 OMD->createImplicitParams(getContext());
195 StartObjCMethod(OMD);
196
197 switch (PD->getSetterKind()) {
198 case ObjCPropertyDecl::Assign: break;
199 case ObjCPropertyDecl::Copy:
200 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
201 break;
202 case ObjCPropertyDecl::Retain:
203 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
204 break;
205 }
206
207 // FIXME: What about nonatomic?
208 SourceLocation Loc = PD->getLocation();
209 ValueDecl *Self = OMD->getSelfDecl();
210 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
211 DeclRefExpr Base(Self, Self->getType(), Loc);
212 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
213 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
214 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
215 true, true);
216 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
217 Ivar->getType(), Loc);
218 EmitStmt(&Assign);
219
220 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000221}
222
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000223llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
224 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
225 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000226}
227
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000228RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
229 // Determine getter selector.
230 Selector S;
231 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) {
232 S = MD->getSelector();
233 } else {
234 S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName();
235 }
236
237 // FIXME: Improve location information.
238 SourceLocation Loc = E->getLocation();
239 // PropertyRefExprs are always instance messages.
240 // FIXME: Is there any reason to try and pass the method here?
241 ObjCMessageExpr GetExpr(const_cast<Expr*>(E->getBase()),
242 S, E->getType(), 0, Loc, Loc,
243 0, 0);
244
245 return EmitObjCMessageExpr(&GetExpr);
246}
247
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000248void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
249 RValue Src) {
250 ErrorUnsupported(E, "Objective-C property setter call");
251}
252
Ted Kremenek2979ec72008-04-09 15:51:31 +0000253CGObjCRuntime::~CGObjCRuntime() {}