2007-08-22 01:43:55 +08:00
|
|
|
//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-22 01:43:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Objective-C code as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-09 23:51:31 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2007-08-22 01:43:55 +08:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-29 16:11:39 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-06-18 02:05:57 +08:00
|
|
|
|
2007-08-22 01:43:55 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2008-06-25 01:04:18 +08:00
|
|
|
/// Emits an instance of NSConstantString representing the object.
|
2008-08-12 08:12:39 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
|
|
|
|
std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
|
|
|
|
llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
|
2008-08-20 08:28:19 +08:00
|
|
|
// FIXME: This bitcast should just be made an invariant on the Runtime.
|
2008-08-12 08:12:39 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a selector.
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
|
|
|
|
// Untyped selector.
|
|
|
|
// Note that this implementation allows for non-constant strings to be passed
|
|
|
|
// as arguments to @selector(). Currently, the only thing preventing this
|
|
|
|
// behaviour is the type checking in the front end.
|
2008-08-12 02:12:00 +08:00
|
|
|
return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-20 08:28:19 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
|
|
|
|
// FIXME: This should pass the Decl not the name.
|
|
|
|
return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
|
|
|
|
}
|
2008-06-25 01:04:18 +08:00
|
|
|
|
|
|
|
|
2008-08-23 11:46:30 +08:00
|
|
|
RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
|
2008-06-25 01:04:18 +08:00
|
|
|
// Only the lookup mechanism and first two arguments of the method
|
|
|
|
// implementation vary between runtimes. We can get the receiver and
|
|
|
|
// arguments in generic code.
|
|
|
|
|
2008-08-12 02:12:00 +08:00
|
|
|
CGObjCRuntime &Runtime = CGM.getObjCRuntime();
|
2008-06-25 01:04:18 +08:00
|
|
|
const Expr *ReceiverExpr = E->getReceiver();
|
|
|
|
bool isSuperMessage = false;
|
2008-08-25 16:19:24 +08:00
|
|
|
bool isClassMessage = false;
|
2008-06-25 01:04:18 +08:00
|
|
|
// Find the receiver
|
|
|
|
llvm::Value *Receiver;
|
|
|
|
if (!ReceiverExpr) {
|
2008-08-16 08:25:02 +08:00
|
|
|
const ObjCInterfaceDecl *OID = E->getClassInfo().first;
|
|
|
|
|
|
|
|
// Very special case, super send in class method. The receiver is
|
|
|
|
// self (the class object) and the send uses super semantics.
|
|
|
|
if (!OID) {
|
|
|
|
assert(!strcmp(E->getClassName()->getName(), "super") &&
|
|
|
|
"Unexpected missing class interface in message send.");
|
|
|
|
isSuperMessage = true;
|
2008-08-25 16:19:24 +08:00
|
|
|
Receiver = LoadObjCSelf();
|
|
|
|
} else {
|
|
|
|
Receiver = Runtime.GetClass(Builder, OID);
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
2008-08-25 16:19:24 +08:00
|
|
|
|
|
|
|
isClassMessage = true;
|
2008-08-10 09:53:14 +08:00
|
|
|
} else if (const PredefinedExpr *PDE =
|
|
|
|
dyn_cast<PredefinedExpr>(E->getReceiver())) {
|
|
|
|
assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
|
2008-06-25 01:04:18 +08:00
|
|
|
isSuperMessage = true;
|
|
|
|
Receiver = LoadObjCSelf();
|
|
|
|
} else {
|
2008-08-12 13:28:47 +08:00
|
|
|
Receiver = EmitScalarExpr(E->getReceiver());
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
|
|
|
|
2008-08-30 11:02:31 +08:00
|
|
|
CallArgList Args;
|
|
|
|
for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
|
|
|
|
i != e; ++i)
|
|
|
|
EmitCallArg(*i, Args);
|
|
|
|
|
2008-06-25 01:04:18 +08:00
|
|
|
if (isSuperMessage) {
|
2008-06-26 12:42:20 +08:00
|
|
|
// super is only valid in an Objective-C method
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
2008-08-23 11:46:30 +08:00
|
|
|
return Runtime.GenerateMessageSendSuper(*this, E,
|
2008-08-25 16:19:24 +08:00
|
|
|
OMD->getClassInterface(),
|
|
|
|
Receiver,
|
2008-08-30 11:02:31 +08:00
|
|
|
isClassMessage,
|
|
|
|
Args);
|
2008-06-25 01:04:18 +08:00
|
|
|
}
|
2008-08-30 11:02:31 +08:00
|
|
|
return Runtime.GenerateMessageSend(*this, E, Receiver, isClassMessage, Args);
|
2007-08-22 01:43:55 +08:00
|
|
|
}
|
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
|
|
|
|
/// the LLVM function and sets the other context used by
|
|
|
|
/// CodeGenFunction.
|
|
|
|
|
|
|
|
// FIXME: This should really be merged with GenerateCode.
|
|
|
|
void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
|
2008-08-16 06:20:32 +08:00
|
|
|
CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
|
2008-06-18 02:05:57 +08:00
|
|
|
llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
|
|
|
|
|
|
|
|
// Create a marker to make it easy to insert allocas into the entryblock
|
|
|
|
// later. Don't create this with the builder, because we don't want it
|
|
|
|
// folded.
|
|
|
|
llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
|
|
|
|
AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
|
|
|
|
EntryBB);
|
|
|
|
|
|
|
|
FnRetTy = OMD->getResultType();
|
|
|
|
CurFuncDecl = OMD;
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(EntryBB);
|
|
|
|
|
|
|
|
// Emit allocs for param decls. Give the LLVM Argument nodes names.
|
|
|
|
llvm::Function::arg_iterator AI = CurFn->arg_begin();
|
|
|
|
|
2008-08-16 11:19:19 +08:00
|
|
|
// Name the struct return argument.
|
2008-06-18 02:05:57 +08:00
|
|
|
if (hasAggregateLLVMType(OMD->getResultType())) {
|
2008-08-16 11:19:19 +08:00
|
|
|
AI->setName("agg.result");
|
2008-06-18 02:05:57 +08:00
|
|
|
++AI;
|
|
|
|
}
|
2008-08-16 11:19:19 +08:00
|
|
|
|
2008-06-18 02:05:57 +08:00
|
|
|
// Add implicit parameters to the decl map.
|
2008-08-16 11:19:19 +08:00
|
|
|
EmitParmDecl(*OMD->getSelfDecl(), AI);
|
|
|
|
++AI;
|
|
|
|
|
|
|
|
EmitParmDecl(*OMD->getCmdDecl(), AI);
|
2008-06-18 02:05:57 +08:00
|
|
|
++AI;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
|
|
|
|
assert(AI != CurFn->arg_end() && "Argument mismatch!");
|
|
|
|
EmitParmDecl(*OMD->getParamDecl(i), AI);
|
|
|
|
}
|
2008-08-16 11:19:19 +08:00
|
|
|
assert(AI == CurFn->arg_end() && "Argument mismatch");
|
2008-08-26 16:29:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate an Objective-C method. An Objective-C method is a C function with
|
|
|
|
/// its pointer, name, and types registered in the class struture.
|
|
|
|
void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
|
|
|
|
StartObjCMethod(OMD);
|
|
|
|
EmitStmt(OMD->getBody());
|
|
|
|
|
|
|
|
const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
|
|
|
|
if (S) {
|
|
|
|
FinishFunction(S->getRBracLoc());
|
|
|
|
} else {
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: I wasn't sure about the synthesis approach. If we end up
|
|
|
|
// generating an AST for the whole body we can just fall back to
|
|
|
|
// having a GenerateFunction which takes the body Stmt.
|
|
|
|
|
|
|
|
/// GenerateObjCGetter - Generate an Objective-C property getter
|
|
|
|
/// function. The given Decl must be either an ObjCCategoryImplDecl
|
|
|
|
/// or an ObjCImplementationDecl.
|
|
|
|
void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
|
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
|
|
|
|
assert(OMD && "Invalid call to generate getter (empty method)");
|
|
|
|
// FIXME: This is rather murky, we create this here since they will
|
|
|
|
// not have been created by Sema for us.
|
|
|
|
OMD->createImplicitParams(getContext());
|
|
|
|
StartObjCMethod(OMD);
|
|
|
|
|
|
|
|
// FIXME: What about nonatomic?
|
|
|
|
SourceLocation Loc = PD->getLocation();
|
|
|
|
ValueDecl *Self = OMD->getSelfDecl();
|
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
|
|
|
DeclRefExpr Base(Self, Self->getType(), Loc);
|
|
|
|
ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
|
|
|
|
true, true);
|
|
|
|
ReturnStmt Return(Loc, &IvarRef);
|
|
|
|
EmitStmt(&Return);
|
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GenerateObjCSetter - Generate an Objective-C property setter
|
|
|
|
/// function. The given Decl must be either an ObjCCategoryImplDecl
|
|
|
|
/// or an ObjCImplementationDecl.
|
|
|
|
void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
|
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
|
|
|
|
assert(OMD && "Invalid call to generate setter (empty method)");
|
|
|
|
// FIXME: This is rather murky, we create this here since they will
|
|
|
|
// not have been created by Sema for us.
|
|
|
|
OMD->createImplicitParams(getContext());
|
|
|
|
StartObjCMethod(OMD);
|
|
|
|
|
|
|
|
switch (PD->getSetterKind()) {
|
|
|
|
case ObjCPropertyDecl::Assign: break;
|
|
|
|
case ObjCPropertyDecl::Copy:
|
|
|
|
CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
|
|
|
|
break;
|
|
|
|
case ObjCPropertyDecl::Retain:
|
|
|
|
CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
|
|
|
|
break;
|
|
|
|
}
|
2008-08-16 11:19:19 +08:00
|
|
|
|
2008-08-26 16:29:31 +08:00
|
|
|
// FIXME: What about nonatomic?
|
|
|
|
SourceLocation Loc = PD->getLocation();
|
|
|
|
ValueDecl *Self = OMD->getSelfDecl();
|
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
|
|
|
DeclRefExpr Base(Self, Self->getType(), Loc);
|
|
|
|
ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
|
|
|
|
DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
|
|
|
|
ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
|
|
|
|
true, true);
|
|
|
|
BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
|
|
|
|
Ivar->getType(), Loc);
|
|
|
|
EmitStmt(&Assign);
|
|
|
|
|
|
|
|
FinishFunction();
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
|
|
|
|
2008-08-16 11:19:19 +08:00
|
|
|
llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
|
2008-06-18 02:05:57 +08:00
|
|
|
}
|
|
|
|
|
2008-08-27 14:57:25 +08:00
|
|
|
RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
|
|
|
|
// Determine getter selector.
|
|
|
|
Selector S;
|
|
|
|
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) {
|
|
|
|
S = MD->getSelector();
|
|
|
|
} else {
|
|
|
|
S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Improve location information.
|
|
|
|
SourceLocation Loc = E->getLocation();
|
|
|
|
// PropertyRefExprs are always instance messages.
|
|
|
|
// FIXME: Is there any reason to try and pass the method here?
|
|
|
|
ObjCMessageExpr GetExpr(const_cast<Expr*>(E->getBase()),
|
|
|
|
S, E->getType(), 0, Loc, Loc,
|
|
|
|
0, 0);
|
|
|
|
|
|
|
|
return EmitObjCMessageExpr(&GetExpr);
|
|
|
|
}
|
|
|
|
|
2008-08-29 16:11:39 +08:00
|
|
|
void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
|
|
|
|
RValue Src) {
|
|
|
|
ErrorUnsupported(E, "Objective-C property setter call");
|
|
|
|
}
|
|
|
|
|
2008-04-09 23:51:31 +08:00
|
|
|
CGObjCRuntime::~CGObjCRuntime() {}
|