blob: 624b24aa75debd1798a5092510b760f753ae5fcc [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
Chris Lattner8fdf3282008-06-24 17:04:18 +000081 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000082 // super is only valid in an Objective-C method
83 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000084 return Runtime.GenerateMessageSendSuper(*this, E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +000085 OMD->getClassInterface(),
86 Receiver,
87 isClassMessage);
Chris Lattner8fdf3282008-06-24 17:04:18 +000088 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000089 return Runtime.GenerateMessageSend(*this, E, Receiver, isClassMessage);
Anders Carlsson55085182007-08-21 17:43:55 +000090}
91
Daniel Dunbaraf05bb92008-08-26 08:29:31 +000092/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
93/// the LLVM function and sets the other context used by
94/// CodeGenFunction.
95
96// FIXME: This should really be merged with GenerateCode.
97void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000098 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner41110242008-06-17 18:05:57 +000099 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
100
101 // Create a marker to make it easy to insert allocas into the entryblock
102 // later. Don't create this with the builder, because we don't want it
103 // folded.
104 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
105 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
106 EntryBB);
107
108 FnRetTy = OMD->getResultType();
109 CurFuncDecl = OMD;
110
111 Builder.SetInsertPoint(EntryBB);
112
113 // Emit allocs for param decls. Give the LLVM Argument nodes names.
114 llvm::Function::arg_iterator AI = CurFn->arg_begin();
115
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000116 // Name the struct return argument.
Chris Lattner41110242008-06-17 18:05:57 +0000117 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000118 AI->setName("agg.result");
Chris Lattner41110242008-06-17 18:05:57 +0000119 ++AI;
120 }
Chris Lattner41110242008-06-17 18:05:57 +0000121
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000122 // Add implicit parameters to the decl map.
123 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner41110242008-06-17 18:05:57 +0000124 ++AI;
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000125
126 EmitParmDecl(*OMD->getCmdDecl(), AI);
127 ++AI;
Chris Lattner41110242008-06-17 18:05:57 +0000128
129 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
130 assert(AI != CurFn->arg_end() && "Argument mismatch!");
131 EmitParmDecl(*OMD->getParamDecl(i), AI);
132 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000133 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000134}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000135
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000136/// Generate an Objective-C method. An Objective-C method is a C function with
137/// its pointer, name, and types registered in the class struture.
138void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
139 StartObjCMethod(OMD);
140 EmitStmt(OMD->getBody());
141
142 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
143 if (S) {
144 FinishFunction(S->getRBracLoc());
145 } else {
146 FinishFunction();
147 }
148}
149
150// FIXME: I wasn't sure about the synthesis approach. If we end up
151// generating an AST for the whole body we can just fall back to
152// having a GenerateFunction which takes the body Stmt.
153
154/// GenerateObjCGetter - Generate an Objective-C property getter
155/// function. The given Decl must be either an ObjCCategoryImplDecl
156/// or an ObjCImplementationDecl.
157void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
158 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
159 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
160 assert(OMD && "Invalid call to generate getter (empty method)");
161 // FIXME: This is rather murky, we create this here since they will
162 // not have been created by Sema for us.
163 OMD->createImplicitParams(getContext());
164 StartObjCMethod(OMD);
165
166 // FIXME: What about nonatomic?
167 SourceLocation Loc = PD->getLocation();
168 ValueDecl *Self = OMD->getSelfDecl();
169 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
170 DeclRefExpr Base(Self, Self->getType(), Loc);
171 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
172 true, true);
173 ReturnStmt Return(Loc, &IvarRef);
174 EmitStmt(&Return);
175
176 FinishFunction();
177}
178
179/// GenerateObjCSetter - Generate an Objective-C property setter
180/// function. The given Decl must be either an ObjCCategoryImplDecl
181/// or an ObjCImplementationDecl.
182void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
183 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
184 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
185 assert(OMD && "Invalid call to generate setter (empty method)");
186 // FIXME: This is rather murky, we create this here since they will
187 // not have been created by Sema for us.
188 OMD->createImplicitParams(getContext());
189 StartObjCMethod(OMD);
190
191 switch (PD->getSetterKind()) {
192 case ObjCPropertyDecl::Assign: break;
193 case ObjCPropertyDecl::Copy:
194 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
195 break;
196 case ObjCPropertyDecl::Retain:
197 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
198 break;
199 }
200
201 // FIXME: What about nonatomic?
202 SourceLocation Loc = PD->getLocation();
203 ValueDecl *Self = OMD->getSelfDecl();
204 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
205 DeclRefExpr Base(Self, Self->getType(), Loc);
206 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
207 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
208 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
209 true, true);
210 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
211 Ivar->getType(), Loc);
212 EmitStmt(&Assign);
213
214 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000215}
216
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000217llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
218 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
219 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000220}
221
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000222RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
223 // Determine getter selector.
224 Selector S;
225 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) {
226 S = MD->getSelector();
227 } else {
228 S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName();
229 }
230
231 // FIXME: Improve location information.
232 SourceLocation Loc = E->getLocation();
233 // PropertyRefExprs are always instance messages.
234 // FIXME: Is there any reason to try and pass the method here?
235 ObjCMessageExpr GetExpr(const_cast<Expr*>(E->getBase()),
236 S, E->getType(), 0, Loc, Loc,
237 0, 0);
238
239 return EmitObjCMessageExpr(&GetExpr);
240}
241
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000242void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
243 RValue Src) {
244 ErrorUnsupported(E, "Objective-C property setter call");
245}
246
Ted Kremenek2979ec72008-04-09 15:51:31 +0000247CGObjCRuntime::~CGObjCRuntime() {}