2007-08-21 17:43:55 +00:00
|
|
|
//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:25 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-21 17:43:55 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Objective-C code as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-09 15:51:31 +00:00
|
|
|
#include "CGObjCRuntime.h"
|
2007-08-21 17:43:55 +00:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2008-08-29 08:11:39 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 05:35:13 +00:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-09-03 00:27:26 +00:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-08-30 19:51:14 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-09-24 04:04:31 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2008-06-17 18:05:57 +00:00
|
|
|
|
2007-08-21 17:43:55 +00:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2008-06-24 17:04:18 +00:00
|
|
|
/// Emits an instance of NSConstantString representing the object.
|
2008-11-25 21:53:21 +00:00
|
|
|
llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
|
|
|
|
{
|
|
|
|
std::string String(E->getString()->getStrData(),
|
|
|
|
E->getString()->getByteLength());
|
2008-08-12 00:12:39 +00:00
|
|
|
llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
|
2008-08-20 00:28:19 +00:00
|
|
|
// FIXME: This bitcast should just be made an invariant on the Runtime.
|
2008-08-12 00:12:39 +00:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-06-24 17:04:18 +00: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-11 18:12:00 +00:00
|
|
|
return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
|
2008-06-24 17:04:18 +00:00
|
|
|
}
|
|
|
|
|
2008-08-20 00:28:19 +00: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-24 17:04:18 +00:00
|
|
|
|
|
|
|
|
2008-08-23 03:46:30 +00:00
|
|
|
RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
|
2008-06-24 17:04:18 +00: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-11 18:12:00 +00:00
|
|
|
CGObjCRuntime &Runtime = CGM.getObjCRuntime();
|
2008-06-24 17:04:18 +00:00
|
|
|
const Expr *ReceiverExpr = E->getReceiver();
|
|
|
|
bool isSuperMessage = false;
|
2008-08-25 08:19:24 +00:00
|
|
|
bool isClassMessage = false;
|
2008-06-24 17:04:18 +00:00
|
|
|
// Find the receiver
|
|
|
|
llvm::Value *Receiver;
|
|
|
|
if (!ReceiverExpr) {
|
2008-08-16 00:25:02 +00: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) {
|
2008-11-20 04:42:34 +00:00
|
|
|
assert(E->getClassName()->isStr("super") &&
|
2008-08-16 00:25:02 +00:00
|
|
|
"Unexpected missing class interface in message send.");
|
|
|
|
isSuperMessage = true;
|
2008-08-25 08:19:24 +00:00
|
|
|
Receiver = LoadObjCSelf();
|
|
|
|
} else {
|
|
|
|
Receiver = Runtime.GetClass(Builder, OID);
|
2008-06-24 17:04:18 +00:00
|
|
|
}
|
2008-08-25 08:19:24 +00:00
|
|
|
|
|
|
|
isClassMessage = true;
|
2008-11-04 14:56:14 +00:00
|
|
|
} else if (isa<ObjCSuperExpr>(E->getReceiver())) {
|
2008-06-24 17:04:18 +00:00
|
|
|
isSuperMessage = true;
|
|
|
|
Receiver = LoadObjCSelf();
|
|
|
|
} else {
|
2008-08-12 05:28:47 +00:00
|
|
|
Receiver = EmitScalarExpr(E->getReceiver());
|
2008-06-24 17:04:18 +00:00
|
|
|
}
|
|
|
|
|
2008-08-30 03:02:31 +00:00
|
|
|
CallArgList Args;
|
|
|
|
for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
|
|
|
|
i != e; ++i)
|
2008-09-09 01:06:48 +00:00
|
|
|
Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
|
2008-08-30 03:02:31 +00:00
|
|
|
|
2008-06-24 17:04:18 +00:00
|
|
|
if (isSuperMessage) {
|
2008-06-26 04:42:20 +00:00
|
|
|
// super is only valid in an Objective-C method
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
2009-02-28 20:07:56 +00:00
|
|
|
bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
|
2008-08-30 05:35:15 +00:00
|
|
|
return Runtime.GenerateMessageSendSuper(*this, E->getType(),
|
|
|
|
E->getSelector(),
|
2008-08-25 08:19:24 +00:00
|
|
|
OMD->getClassInterface(),
|
2009-02-28 20:07:56 +00:00
|
|
|
isCategoryImpl,
|
2008-08-25 08:19:24 +00:00
|
|
|
Receiver,
|
2008-08-30 03:02:31 +00:00
|
|
|
isClassMessage,
|
|
|
|
Args);
|
2008-06-24 17:04:18 +00:00
|
|
|
}
|
2008-08-30 05:35:15 +00:00
|
|
|
return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
|
|
|
|
Receiver, isClassMessage, Args);
|
2007-08-21 17:43:55 +00:00
|
|
|
}
|
|
|
|
|
2008-08-26 08:29:31 +00:00
|
|
|
/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
|
|
|
|
/// the LLVM function and sets the other context used by
|
|
|
|
/// CodeGenFunction.
|
2009-01-10 21:06:09 +00:00
|
|
|
void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
|
|
|
|
const ObjCContainerDecl *CD) {
|
2008-09-09 23:14:03 +00:00
|
|
|
FunctionArgList Args;
|
2009-01-10 21:06:09 +00:00
|
|
|
llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
|
2008-09-04 23:41:35 +00:00
|
|
|
|
2008-09-09 23:14:03 +00:00
|
|
|
CGM.SetMethodAttributes(OMD, Fn);
|
2008-06-17 18:05:57 +00:00
|
|
|
|
2008-09-09 23:14:03 +00:00
|
|
|
Args.push_back(std::make_pair(OMD->getSelfDecl(),
|
|
|
|
OMD->getSelfDecl()->getType()));
|
|
|
|
Args.push_back(std::make_pair(OMD->getCmdDecl(),
|
|
|
|
OMD->getCmdDecl()->getType()));
|
2008-06-17 18:05:57 +00:00
|
|
|
|
2009-02-20 18:43:26 +00:00
|
|
|
for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
|
|
|
|
E = OMD->param_end(); PI != E; ++PI)
|
|
|
|
Args.push_back(std::make_pair(*PI, (*PI)->getType()));
|
2008-08-16 03:19:19 +00:00
|
|
|
|
2008-10-18 18:22:23 +00:00
|
|
|
StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
|
2008-08-26 08:29:31 +00: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) {
|
2009-02-25 01:09:46 +00:00
|
|
|
// Check if we should generate debug info for this method.
|
|
|
|
if (CGM.getDebugInfo() && !OMD->getAttr<NodebugAttr>())
|
|
|
|
DebugInfo = CGM.getDebugInfo();
|
2009-01-10 21:06:09 +00:00
|
|
|
StartObjCMethod(OMD, OMD->getClassInterface());
|
2008-08-26 08:29:31 +00:00
|
|
|
EmitStmt(OMD->getBody());
|
2008-10-18 18:22:23 +00:00
|
|
|
FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
|
2008-08-26 08:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2009-01-10 22:55:25 +00:00
|
|
|
/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
|
|
|
|
/// is illegal within a category.
|
2008-12-09 20:23:04 +00:00
|
|
|
void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
|
|
|
|
const ObjCPropertyImplDecl *PID) {
|
2008-09-24 04:04:31 +00:00
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
2008-08-26 08:29:31 +00:00
|
|
|
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.
|
2008-12-09 20:23:04 +00:00
|
|
|
OMD->createImplicitParams(getContext(), IMP->getClassInterface());
|
2009-01-10 21:06:09 +00:00
|
|
|
StartObjCMethod(OMD, IMP->getClassInterface());
|
2008-08-26 08:29:31 +00:00
|
|
|
|
2008-09-24 04:04:31 +00:00
|
|
|
// Determine if we should use an objc_getProperty call for
|
2008-12-08 23:56:17 +00:00
|
|
|
// this. Non-atomic properties are directly evaluated.
|
|
|
|
// atomic 'copy' and 'retain' properties are also directly
|
|
|
|
// evaluated in gc-only mode.
|
2008-09-24 04:04:31 +00:00
|
|
|
if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
|
2008-12-08 23:56:17 +00:00
|
|
|
!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) &&
|
|
|
|
(PD->getSetterKind() == ObjCPropertyDecl::Copy ||
|
|
|
|
PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
|
2008-09-24 04:04:31 +00:00
|
|
|
llvm::Value *GetPropertyFn =
|
|
|
|
CGM.getObjCRuntime().GetPropertyGetFunction();
|
|
|
|
|
|
|
|
if (!GetPropertyFn) {
|
|
|
|
CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
|
|
|
|
FinishFunction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
|
|
|
|
// FIXME: Can't this be simpler? This might even be worse than the
|
|
|
|
// corresponding gcc code.
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
ValueDecl *Cmd = OMD->getCmdDecl();
|
|
|
|
llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
|
|
|
|
QualType IdTy = getContext().getObjCIdType();
|
|
|
|
llvm::Value *SelfAsId =
|
|
|
|
Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
|
2008-12-09 20:23:04 +00:00
|
|
|
llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
|
2008-09-24 04:04:31 +00:00
|
|
|
llvm::Value *True =
|
2009-02-04 00:55:44 +00:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
|
2008-09-24 04:04:31 +00:00
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
|
2009-02-03 23:43:59 +00:00
|
|
|
// FIXME: We shouldn't need to get the function info here, the
|
|
|
|
// runtime already should have computed it to build the function.
|
2009-02-02 23:23:47 +00:00
|
|
|
RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args),
|
2009-02-02 22:03:45 +00:00
|
|
|
GetPropertyFn, Args);
|
2008-09-24 04:04:31 +00:00
|
|
|
// We need to fix the type here. Ivars with copy & retain are
|
|
|
|
// always objects so we don't need to worry about complex or
|
|
|
|
// aggregates.
|
|
|
|
RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
|
|
|
|
Types.ConvertType(PD->getType())));
|
|
|
|
EmitReturnOfRValue(RV, PD->getType());
|
|
|
|
} else {
|
2008-12-15 20:35:07 +00:00
|
|
|
FieldDecl *Field =
|
|
|
|
IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar);
|
2009-02-03 00:09:52 +00:00
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
|
|
|
|
LoadObjCSelf(), Ivar, Field, 0);
|
2008-11-26 22:36:09 +00:00
|
|
|
if (hasAggregateLLVMType(Ivar->getType())) {
|
|
|
|
EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType());
|
|
|
|
}
|
2009-03-03 18:49:40 +00:00
|
|
|
else {
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
RValue RV = EmitLoadOfLValue(LV, Ivar->getType());
|
|
|
|
RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
|
|
|
|
Types.ConvertType(PD->getType())));
|
|
|
|
EmitReturnOfRValue(RV, PD->getType());
|
|
|
|
}
|
2008-09-24 04:04:31 +00:00
|
|
|
}
|
2008-08-26 08:29:31 +00:00
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GenerateObjCSetter - Generate an Objective-C property setter
|
2009-01-10 22:55:25 +00:00
|
|
|
/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
|
|
|
|
/// is illegal within a category.
|
2008-12-09 20:23:04 +00:00
|
|
|
void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
|
|
|
|
const ObjCPropertyImplDecl *PID) {
|
2008-09-24 06:32:09 +00:00
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
2008-08-26 08:29:31 +00:00
|
|
|
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.
|
2008-12-09 20:23:04 +00:00
|
|
|
OMD->createImplicitParams(getContext(), IMP->getClassInterface());
|
2009-01-10 21:06:09 +00:00
|
|
|
StartObjCMethod(OMD, IMP->getClassInterface());
|
2008-08-16 03:19:19 +00:00
|
|
|
|
2008-09-24 06:32:09 +00:00
|
|
|
bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
|
|
|
|
bool IsAtomic =
|
|
|
|
!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
|
|
|
|
|
|
|
|
// Determine if we should use an objc_setProperty call for
|
|
|
|
// this. Properties with 'copy' semantics always use it, as do
|
|
|
|
// non-atomic properties with 'release' semantics as long as we are
|
|
|
|
// not in gc-only mode.
|
|
|
|
if (IsCopy ||
|
|
|
|
(CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
|
|
|
|
PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
|
|
|
|
llvm::Value *SetPropertyFn =
|
|
|
|
CGM.getObjCRuntime().GetPropertySetFunction();
|
|
|
|
|
|
|
|
if (!SetPropertyFn) {
|
|
|
|
CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
|
|
|
|
FinishFunction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit objc_setProperty((id) self, _cmd, offset, arg,
|
|
|
|
// <is-atomic>, <is-copy>).
|
|
|
|
// FIXME: Can't this be simpler? This might even be worse than the
|
|
|
|
// corresponding gcc code.
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
ValueDecl *Cmd = OMD->getCmdDecl();
|
|
|
|
llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
|
|
|
|
QualType IdTy = getContext().getObjCIdType();
|
|
|
|
llvm::Value *SelfAsId =
|
|
|
|
Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
|
2008-12-09 20:23:04 +00:00
|
|
|
llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
|
2009-02-20 18:43:26 +00:00
|
|
|
llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
|
2008-09-24 06:32:09 +00:00
|
|
|
llvm::Value *ArgAsId =
|
|
|
|
Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
|
|
|
|
Types.ConvertType(IdTy));
|
|
|
|
llvm::Value *True =
|
2009-02-04 00:55:44 +00:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
|
2008-09-24 06:32:09 +00:00
|
|
|
llvm::Value *False =
|
2009-02-04 00:55:44 +00:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
|
2008-09-24 06:32:09 +00:00
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False),
|
|
|
|
getContext().BoolTy));
|
|
|
|
Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False),
|
|
|
|
getContext().BoolTy));
|
2009-02-03 23:43:59 +00:00
|
|
|
// FIXME: We shouldn't need to get the function info here, the
|
|
|
|
// runtime already should have computed it to build the function.
|
|
|
|
EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
|
2009-02-02 23:23:47 +00:00
|
|
|
SetPropertyFn, Args);
|
2008-09-24 06:32:09 +00:00
|
|
|
} else {
|
|
|
|
SourceLocation Loc = PD->getLocation();
|
|
|
|
ValueDecl *Self = OMD->getSelfDecl();
|
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
|
|
|
DeclRefExpr Base(Self, Self->getType(), Loc);
|
2009-02-20 18:43:26 +00:00
|
|
|
ParmVarDecl *ArgDecl = *OMD->param_begin();
|
2008-09-24 06:32:09 +00:00
|
|
|
DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
|
2008-12-13 22:20:28 +00:00
|
|
|
ObjCInterfaceDecl *OI = IMP->getClassInterface();
|
2008-12-18 17:29:46 +00:00
|
|
|
ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
|
2008-09-24 06:32:09 +00:00
|
|
|
true, true);
|
2008-12-18 17:29:46 +00:00
|
|
|
getContext().setFieldDecl(OI, Ivar, &IvarRef);
|
2008-09-24 06:32:09 +00:00
|
|
|
BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
|
|
|
|
Ivar->getType(), Loc);
|
|
|
|
EmitStmt(&Assign);
|
|
|
|
}
|
2008-08-26 08:29:31 +00:00
|
|
|
|
|
|
|
FinishFunction();
|
2008-06-17 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 04:04:31 +00:00
|
|
|
llvm::Value *CodeGenFunction::LoadObjCSelf() {
|
2008-08-16 03:19:19 +00:00
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
2009-03-20 21:53:12 +00:00
|
|
|
// See if we need to lazily forward self inside a block literal.
|
|
|
|
BlockForwardSelf();
|
2008-08-16 03:19:19 +00:00
|
|
|
return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
|
2008-06-17 18:05:57 +00:00
|
|
|
}
|
|
|
|
|
2009-02-03 00:09:52 +00:00
|
|
|
QualType CodeGenFunction::TypeOfSelfObject() {
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
|
|
|
|
const PointerType *PTy =
|
|
|
|
cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
|
|
|
|
return PTy->getPointeeType();
|
|
|
|
}
|
|
|
|
|
2009-03-20 19:18:21 +00:00
|
|
|
RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp,
|
|
|
|
const Selector &S) {
|
|
|
|
llvm::Value *Receiver = LoadObjCSelf();
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
bool isClassMessage = OMD->isClassMethod();
|
|
|
|
bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
|
|
|
|
return CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
|
|
|
|
Exp->getType(),
|
|
|
|
S,
|
|
|
|
OMD->getClassInterface(),
|
|
|
|
isCategoryImpl,
|
|
|
|
Receiver,
|
|
|
|
isClassMessage,
|
|
|
|
CallArgList());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-22 18:39:36 +00:00
|
|
|
RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) {
|
2008-11-22 22:30:21 +00:00
|
|
|
// FIXME: Split it into two separate routines.
|
2008-11-22 18:39:36 +00:00
|
|
|
if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
|
|
|
|
Selector S = E->getProperty()->getGetterName();
|
2009-03-20 19:18:21 +00:00
|
|
|
if (isa<ObjCSuperExpr>(E->getBase()))
|
|
|
|
return EmitObjCSuperPropertyGet(E, S);
|
2008-11-22 18:39:36 +00:00
|
|
|
return CGM.getObjCRuntime().
|
2008-11-22 22:30:21 +00:00
|
|
|
GenerateMessageSend(*this, Exp->getType(), S,
|
|
|
|
EmitScalarExpr(E->getBase()),
|
|
|
|
false, CallArgList());
|
|
|
|
}
|
2009-01-15 18:32:35 +00:00
|
|
|
else {
|
2009-01-16 01:50:29 +00:00
|
|
|
const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp);
|
|
|
|
Selector S = KE->getGetterMethod()->getSelector();
|
2009-03-10 18:03:11 +00:00
|
|
|
llvm::Value *Receiver;
|
|
|
|
if (KE->getClassProp()) {
|
|
|
|
const ObjCInterfaceDecl *OID = KE->getClassProp();
|
|
|
|
Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
|
|
|
|
}
|
2009-03-20 19:18:21 +00:00
|
|
|
else if (isa<ObjCSuperExpr>(KE->getBase()))
|
|
|
|
return EmitObjCSuperPropertyGet(KE, S);
|
2009-03-10 18:03:11 +00:00
|
|
|
else
|
|
|
|
Receiver = EmitScalarExpr(KE->getBase());
|
2008-11-22 22:30:21 +00:00
|
|
|
return CGM.getObjCRuntime().
|
|
|
|
GenerateMessageSend(*this, Exp->getType(), S,
|
2009-03-10 18:03:11 +00:00
|
|
|
Receiver,
|
|
|
|
KE->getClassProp() != 0, CallArgList());
|
2008-11-22 18:39:36 +00:00
|
|
|
}
|
2008-08-27 06:57:25 +00:00
|
|
|
}
|
|
|
|
|
2009-03-20 19:18:21 +00:00
|
|
|
void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp,
|
|
|
|
const Selector &S,
|
|
|
|
RValue Src) {
|
|
|
|
CallArgList Args;
|
|
|
|
llvm::Value *Receiver = LoadObjCSelf();
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
bool isClassMessage = OMD->isClassMethod();
|
|
|
|
bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
|
|
|
|
Args.push_back(std::make_pair(Src, Exp->getType()));
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSendSuper(*this,
|
|
|
|
Exp->getType(),
|
|
|
|
S,
|
|
|
|
OMD->getClassInterface(),
|
|
|
|
isCategoryImpl,
|
|
|
|
Receiver,
|
|
|
|
isClassMessage,
|
|
|
|
Args);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-22 22:30:21 +00:00
|
|
|
void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp,
|
2008-08-29 08:11:39 +00:00
|
|
|
RValue Src) {
|
2008-11-22 22:30:21 +00:00
|
|
|
// FIXME: Split it into two separate routines.
|
|
|
|
if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) {
|
|
|
|
Selector S = E->getProperty()->getSetterName();
|
2009-03-20 19:18:21 +00:00
|
|
|
if (isa<ObjCSuperExpr>(E->getBase())) {
|
|
|
|
EmitObjCSuperPropertySet(E, S, Src);
|
|
|
|
return;
|
|
|
|
}
|
2008-11-22 22:30:21 +00:00
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(Src, E->getType()));
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
|
|
|
|
EmitScalarExpr(E->getBase()),
|
|
|
|
false, Args);
|
|
|
|
}
|
|
|
|
else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) {
|
|
|
|
Selector S = E->getSetterMethod()->getSelector();
|
|
|
|
CallArgList Args;
|
2009-03-10 18:03:11 +00:00
|
|
|
llvm::Value *Receiver;
|
|
|
|
if (E->getClassProp()) {
|
|
|
|
const ObjCInterfaceDecl *OID = E->getClassProp();
|
|
|
|
Receiver = CGM.getObjCRuntime().GetClass(Builder, OID);
|
|
|
|
}
|
2009-03-20 17:22:23 +00:00
|
|
|
else if (isa<ObjCSuperExpr>(E->getBase())) {
|
2009-03-20 19:18:21 +00:00
|
|
|
EmitObjCSuperPropertySet(E, S, Src);
|
2009-03-20 17:22:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
2009-03-10 18:03:11 +00:00
|
|
|
Receiver = EmitScalarExpr(E->getBase());
|
2008-11-22 22:30:21 +00:00
|
|
|
Args.push_back(std::make_pair(Src, E->getType()));
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
|
2009-03-10 18:03:11 +00:00
|
|
|
Receiver,
|
|
|
|
E->getClassProp() != 0, Args);
|
2008-11-22 22:30:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
assert (0 && "bad expression node in EmitObjCPropertySet");
|
2008-08-29 08:11:39 +00:00
|
|
|
}
|
|
|
|
|
2008-08-30 19:51:14 +00:00
|
|
|
void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
|
|
|
|
{
|
2008-09-24 04:04:31 +00:00
|
|
|
llvm::Function *EnumerationMutationFn =
|
|
|
|
CGM.getObjCRuntime().EnumerationMutationFunction();
|
2008-08-31 02:33:12 +00:00
|
|
|
llvm::Value *DeclAddress;
|
|
|
|
QualType ElementTy;
|
|
|
|
|
2008-09-24 04:04:31 +00:00
|
|
|
if (!EnumerationMutationFn) {
|
|
|
|
CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-31 02:33:12 +00:00
|
|
|
if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
|
|
|
|
EmitStmt(SD);
|
2008-11-11 23:11:34 +00:00
|
|
|
assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
|
2009-01-20 01:17:11 +00:00
|
|
|
const Decl* D = SD->getSolitaryDecl();
|
2008-10-06 20:59:48 +00:00
|
|
|
ElementTy = cast<ValueDecl>(D)->getType();
|
|
|
|
DeclAddress = LocalDeclMap[D];
|
2008-08-31 02:33:12 +00:00
|
|
|
} else {
|
|
|
|
ElementTy = cast<Expr>(S.getElement())->getType();
|
|
|
|
DeclAddress = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast enumeration state.
|
|
|
|
QualType StateTy = getContext().getObjCFastEnumerationStateType();
|
|
|
|
llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
|
|
|
|
"state.ptr");
|
|
|
|
StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
|
2008-08-31 04:05:03 +00:00
|
|
|
EmitMemSetToZero(StatePtr, StateTy);
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
// Number of elements in the items array.
|
2008-08-31 04:05:03 +00:00
|
|
|
static const unsigned NumItems = 16;
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
// Get selector
|
|
|
|
llvm::SmallVector<IdentifierInfo*, 3> II;
|
|
|
|
II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
|
|
|
|
II.push_back(&CGM.getContext().Idents.get("objects"));
|
|
|
|
II.push_back(&CGM.getContext().Idents.get("count"));
|
|
|
|
Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
|
|
|
|
&II[0]);
|
|
|
|
|
|
|
|
QualType ItemsTy =
|
|
|
|
getContext().getConstantArrayType(getContext().getObjCIdType(),
|
|
|
|
llvm::APInt(32, NumItems),
|
|
|
|
ArrayType::Normal, 0);
|
|
|
|
llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
|
|
|
|
|
|
|
|
llvm::Value *Collection = EmitScalarExpr(S.getCollection());
|
|
|
|
|
|
|
|
CallArgList Args;
|
2008-09-09 01:06:48 +00:00
|
|
|
Args.push_back(std::make_pair(RValue::get(StatePtr),
|
2008-08-31 02:33:12 +00:00
|
|
|
getContext().getPointerType(StateTy)));
|
|
|
|
|
2008-09-09 01:06:48 +00:00
|
|
|
Args.push_back(std::make_pair(RValue::get(ItemsPtr),
|
2008-08-31 02:33:12 +00:00
|
|
|
getContext().getPointerType(ItemsTy)));
|
|
|
|
|
|
|
|
const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
|
|
|
|
llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
|
2008-09-09 01:06:48 +00:00
|
|
|
Args.push_back(std::make_pair(RValue::get(Count),
|
|
|
|
getContext().UnsignedLongTy));
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
RValue CountRV =
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this,
|
|
|
|
getContext().UnsignedLongTy,
|
|
|
|
FastEnumSel,
|
|
|
|
Collection, false, Args);
|
|
|
|
|
|
|
|
llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
|
|
|
|
Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *NoElements = createBasicBlock("noelements");
|
|
|
|
llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
|
|
|
|
llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
|
|
|
|
|
|
|
|
llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
|
2008-08-31 04:05:03 +00:00
|
|
|
Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
|
2008-08-31 02:33:12 +00:00
|
|
|
|
2008-08-31 04:05:03 +00:00
|
|
|
EmitBlock(SetStartMutations);
|
|
|
|
|
|
|
|
llvm::Value *StartMutationsPtr =
|
|
|
|
CreateTempAlloca(UnsignedLongLTy);
|
|
|
|
|
|
|
|
llvm::Value *StateMutationsPtrPtr =
|
|
|
|
Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
|
|
|
|
llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
|
|
|
|
"mutationsptr");
|
|
|
|
|
|
|
|
llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
|
|
|
|
"mutations");
|
|
|
|
|
|
|
|
Builder.CreateStore(StateMutations, StartMutationsPtr);
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
|
2008-08-31 02:33:12 +00:00
|
|
|
EmitBlock(LoopStart);
|
|
|
|
|
|
|
|
llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
|
|
|
|
Builder.CreateStore(Zero, CounterPtr);
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
|
2008-08-31 02:33:12 +00:00
|
|
|
EmitBlock(LoopBody);
|
|
|
|
|
2008-08-31 04:05:03 +00:00
|
|
|
StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
|
|
|
|
StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
|
|
|
|
|
|
|
|
llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
|
|
|
|
"mutations");
|
|
|
|
llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
|
|
|
|
StartMutations,
|
|
|
|
"tobool");
|
|
|
|
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
|
|
|
|
llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
|
2008-08-31 04:05:03 +00:00
|
|
|
|
|
|
|
Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
|
|
|
|
|
|
|
|
EmitBlock(WasMutated);
|
|
|
|
llvm::Value *V =
|
|
|
|
Builder.CreateBitCast(Collection,
|
|
|
|
ConvertType(getContext().getObjCIdType()),
|
|
|
|
"tmp");
|
2009-02-03 23:55:40 +00:00
|
|
|
CallArgList Args2;
|
|
|
|
Args2.push_back(std::make_pair(RValue::get(V),
|
|
|
|
getContext().getObjCIdType()));
|
|
|
|
// FIXME: We shouldn't need to get the function info here, the
|
|
|
|
// runtime already should have computed it to build the function.
|
2009-02-04 22:00:33 +00:00
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2),
|
2009-02-03 23:55:40 +00:00
|
|
|
EnumerationMutationFn, Args2);
|
2008-08-31 04:05:03 +00:00
|
|
|
|
|
|
|
EmitBlock(WasNotMutated);
|
|
|
|
|
2008-08-31 02:33:12 +00:00
|
|
|
llvm::Value *StateItemsPtr =
|
|
|
|
Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
|
|
|
|
|
|
|
|
llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
|
|
|
|
|
|
|
|
llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
|
|
|
|
"stateitems");
|
|
|
|
|
|
|
|
llvm::Value *CurrentItemPtr =
|
|
|
|
Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
|
|
|
|
|
|
|
|
llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
|
|
|
|
|
|
|
|
// Cast the item to the right type.
|
|
|
|
CurrentItem = Builder.CreateBitCast(CurrentItem,
|
|
|
|
ConvertType(ElementTy), "tmp");
|
|
|
|
|
|
|
|
if (!DeclAddress) {
|
|
|
|
LValue LV = EmitLValue(cast<Expr>(S.getElement()));
|
|
|
|
|
|
|
|
// Set the value to null.
|
|
|
|
Builder.CreateStore(CurrentItem, LV.getAddress());
|
|
|
|
} else
|
|
|
|
Builder.CreateStore(CurrentItem, DeclAddress);
|
|
|
|
|
|
|
|
// Increment the counter.
|
|
|
|
Counter = Builder.CreateAdd(Counter,
|
|
|
|
llvm::ConstantInt::get(UnsignedLongLTy, 1));
|
|
|
|
Builder.CreateStore(Counter, CounterPtr);
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
|
|
|
|
llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
|
2008-08-31 02:33:12 +00:00
|
|
|
|
2009-02-10 05:52:02 +00:00
|
|
|
BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
EmitStmt(S.getBody());
|
|
|
|
|
|
|
|
BreakContinueStack.pop_back();
|
|
|
|
|
|
|
|
EmitBlock(AfterBody);
|
|
|
|
|
2008-11-11 02:29:29 +00:00
|
|
|
llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
|
2009-01-06 18:56:31 +00:00
|
|
|
|
|
|
|
Counter = Builder.CreateLoad(CounterPtr);
|
|
|
|
Limit = Builder.CreateLoad(LimitPtr);
|
2008-08-31 02:33:12 +00:00
|
|
|
llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
|
2008-09-04 21:54:37 +00:00
|
|
|
Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
|
2008-08-31 02:33:12 +00:00
|
|
|
|
|
|
|
// Fetch more elements.
|
|
|
|
EmitBlock(FetchMore);
|
|
|
|
|
|
|
|
CountRV =
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this,
|
|
|
|
getContext().UnsignedLongTy,
|
|
|
|
FastEnumSel,
|
|
|
|
Collection, false, Args);
|
|
|
|
Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
|
|
|
|
Limit = Builder.CreateLoad(LimitPtr);
|
|
|
|
|
|
|
|
IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
|
|
|
|
Builder.CreateCondBr(IsZero, NoElements, LoopStart);
|
|
|
|
|
|
|
|
// No more elements.
|
|
|
|
EmitBlock(NoElements);
|
|
|
|
|
|
|
|
if (!DeclAddress) {
|
|
|
|
// If the element was not a declaration, set it to be null.
|
|
|
|
|
|
|
|
LValue LV = EmitLValue(cast<Expr>(S.getElement()));
|
|
|
|
|
|
|
|
// Set the value to null.
|
|
|
|
Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
|
|
|
|
LV.getAddress());
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitBlock(LoopEnd);
|
2008-08-30 19:51:14 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 10:04:29 +00:00
|
|
|
void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
|
|
|
|
{
|
2008-11-21 00:49:24 +00:00
|
|
|
CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
|
2008-09-09 10:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
|
|
|
|
{
|
|
|
|
CGM.getObjCRuntime().EmitThrowStmt(*this, S);
|
|
|
|
}
|
|
|
|
|
2008-11-15 21:26:17 +00:00
|
|
|
void CodeGenFunction::EmitObjCAtSynchronizedStmt(
|
|
|
|
const ObjCAtSynchronizedStmt &S)
|
|
|
|
{
|
2008-11-21 00:49:24 +00:00
|
|
|
CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S);
|
2008-11-15 21:26:17 +00:00
|
|
|
}
|
|
|
|
|
2008-04-09 15:51:31 +00:00
|
|
|
CGObjCRuntime::~CGObjCRuntime() {}
|