blob: 93567a4f8f58e57d3e9e7fe7a3b3a509d6db8e56 [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 Dunbar64789f82008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner8c7c6a12008-06-17 18:05:57 +000018
Anders Carlssona66cad42007-08-21 17:43:55 +000019using namespace clang;
20using namespace CodeGen;
21
Chris Lattner6ee20e32008-06-24 17:04:18 +000022/// Emits an instance of NSConstantString representing the object.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000023llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
24 std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
25 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000026 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000027 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner6ee20e32008-06-24 17:04:18 +000028}
29
30/// Emit a selector.
31llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
32 // Untyped selector.
33 // Note that this implementation allows for non-constant strings to be passed
34 // as arguments to @selector(). Currently, the only thing preventing this
35 // behaviour is the type checking in the front end.
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000036 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner6ee20e32008-06-24 17:04:18 +000037}
38
Daniel Dunbarf1f7f192008-08-20 00:28:19 +000039llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
40 // FIXME: This should pass the Decl not the name.
41 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
42}
Chris Lattner6ee20e32008-06-24 17:04:18 +000043
44
Daniel Dunbara04840b2008-08-23 03:46:30 +000045RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner6ee20e32008-06-24 17:04:18 +000046 // Only the lookup mechanism and first two arguments of the method
47 // implementation vary between runtimes. We can get the receiver and
48 // arguments in generic code.
49
Daniel Dunbarfc69bde2008-08-11 18:12:00 +000050 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner6ee20e32008-06-24 17:04:18 +000051 const Expr *ReceiverExpr = E->getReceiver();
52 bool isSuperMessage = false;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000053 bool isClassMessage = false;
Chris Lattner6ee20e32008-06-24 17:04:18 +000054 // Find the receiver
55 llvm::Value *Receiver;
56 if (!ReceiverExpr) {
Daniel Dunbar434627a2008-08-16 00:25:02 +000057 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
58
59 // Very special case, super send in class method. The receiver is
60 // self (the class object) and the send uses super semantics.
61 if (!OID) {
62 assert(!strcmp(E->getClassName()->getName(), "super") &&
63 "Unexpected missing class interface in message send.");
Daniel Dunbar434627a2008-08-16 00:25:02 +000064 isSuperMessage = true;
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000065 Receiver = LoadObjCSelf();
66 } else {
67 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner6ee20e32008-06-24 17:04:18 +000068 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000069
70 isClassMessage = true;
Chris Lattner69909292008-08-10 01:53:14 +000071 } else if (const PredefinedExpr *PDE =
72 dyn_cast<PredefinedExpr>(E->getReceiver())) {
73 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner6ee20e32008-06-24 17:04:18 +000074 isSuperMessage = true;
75 Receiver = LoadObjCSelf();
76 } else {
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000077 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner6ee20e32008-06-24 17:04:18 +000078 }
79
Chris Lattner6ee20e32008-06-24 17:04:18 +000080 if (isSuperMessage) {
Chris Lattner8384c142008-06-26 04:42:20 +000081 // super is only valid in an Objective-C method
82 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbara04840b2008-08-23 03:46:30 +000083 return Runtime.GenerateMessageSendSuper(*this, E,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000084 OMD->getClassInterface(),
85 Receiver,
86 isClassMessage);
Chris Lattner6ee20e32008-06-24 17:04:18 +000087 }
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +000088 return Runtime.GenerateMessageSend(*this, E, Receiver, isClassMessage);
Anders Carlssona66cad42007-08-21 17:43:55 +000089}
90
Daniel Dunbar6b57d432008-08-26 08:29:31 +000091/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
92/// the LLVM function and sets the other context used by
93/// CodeGenFunction.
94
95// FIXME: This should really be merged with GenerateCode.
96void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +000097 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner8c7c6a12008-06-17 18:05:57 +000098 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
99
100 // Create a marker to make it easy to insert allocas into the entryblock
101 // later. Don't create this with the builder, because we don't want it
102 // folded.
103 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
104 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
105 EntryBB);
106
107 FnRetTy = OMD->getResultType();
108 CurFuncDecl = OMD;
109
110 Builder.SetInsertPoint(EntryBB);
111
112 // Emit allocs for param decls. Give the LLVM Argument nodes names.
113 llvm::Function::arg_iterator AI = CurFn->arg_begin();
114
Daniel Dunbarace33292008-08-16 03:19:19 +0000115 // Name the struct return argument.
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000116 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarace33292008-08-16 03:19:19 +0000117 AI->setName("agg.result");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000118 ++AI;
119 }
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000120
Daniel Dunbarace33292008-08-16 03:19:19 +0000121 // Add implicit parameters to the decl map.
122 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000123 ++AI;
Daniel Dunbarace33292008-08-16 03:19:19 +0000124
125 EmitParmDecl(*OMD->getCmdDecl(), AI);
126 ++AI;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000127
128 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
129 assert(AI != CurFn->arg_end() && "Argument mismatch!");
130 EmitParmDecl(*OMD->getParamDecl(i), AI);
131 }
Daniel Dunbarace33292008-08-16 03:19:19 +0000132 assert(AI == CurFn->arg_end() && "Argument mismatch");
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000133}
Daniel Dunbarace33292008-08-16 03:19:19 +0000134
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000135/// Generate an Objective-C method. An Objective-C method is a C function with
136/// its pointer, name, and types registered in the class struture.
137void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
138 StartObjCMethod(OMD);
139 EmitStmt(OMD->getBody());
140
141 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
142 if (S) {
143 FinishFunction(S->getRBracLoc());
144 } else {
145 FinishFunction();
146 }
147}
148
149// FIXME: I wasn't sure about the synthesis approach. If we end up
150// generating an AST for the whole body we can just fall back to
151// having a GenerateFunction which takes the body Stmt.
152
153/// GenerateObjCGetter - Generate an Objective-C property getter
154/// function. The given Decl must be either an ObjCCategoryImplDecl
155/// or an ObjCImplementationDecl.
156void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
157 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
158 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
159 assert(OMD && "Invalid call to generate getter (empty method)");
160 // FIXME: This is rather murky, we create this here since they will
161 // not have been created by Sema for us.
162 OMD->createImplicitParams(getContext());
163 StartObjCMethod(OMD);
164
165 // FIXME: What about nonatomic?
166 SourceLocation Loc = PD->getLocation();
167 ValueDecl *Self = OMD->getSelfDecl();
168 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
169 DeclRefExpr Base(Self, Self->getType(), Loc);
170 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
171 true, true);
172 ReturnStmt Return(Loc, &IvarRef);
173 EmitStmt(&Return);
174
175 FinishFunction();
176}
177
178/// GenerateObjCSetter - Generate an Objective-C property setter
179/// function. The given Decl must be either an ObjCCategoryImplDecl
180/// or an ObjCImplementationDecl.
181void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
182 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
183 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
184 assert(OMD && "Invalid call to generate setter (empty method)");
185 // FIXME: This is rather murky, we create this here since they will
186 // not have been created by Sema for us.
187 OMD->createImplicitParams(getContext());
188 StartObjCMethod(OMD);
189
190 switch (PD->getSetterKind()) {
191 case ObjCPropertyDecl::Assign: break;
192 case ObjCPropertyDecl::Copy:
193 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
194 break;
195 case ObjCPropertyDecl::Retain:
196 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
197 break;
198 }
199
200 // FIXME: What about nonatomic?
201 SourceLocation Loc = PD->getLocation();
202 ValueDecl *Self = OMD->getSelfDecl();
203 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
204 DeclRefExpr Base(Self, Self->getType(), Loc);
205 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
206 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
207 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
208 true, true);
209 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
210 Ivar->getType(), Loc);
211 EmitStmt(&Assign);
212
213 FinishFunction();
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000214}
215
Daniel Dunbarace33292008-08-16 03:19:19 +0000216llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
217 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
218 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000219}
220
Ted Kremenekfa4ebab2008-04-09 15:51:31 +0000221CGObjCRuntime::~CGObjCRuntime() {}