2007-08-21 21:43:55 +04:00
|
|
|
//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-21 21:43:55 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit Objective-C code as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-19 04:36:36 +03:00
|
|
|
#include "CGDebugInfo.h"
|
2008-04-09 19:51:31 +04:00
|
|
|
#include "CGObjCRuntime.h"
|
2007-08-21 21:43:55 +04:00
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2011-06-16 03:02:42 +04:00
|
|
|
#include "TargetInfo.h"
|
2008-08-29 12:11:39 +04:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2008-08-11 09:35:13 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-04-26 05:32:48 +04:00
|
|
|
#include "clang/AST/StmtObjC.h"
|
2008-09-03 04:27:26 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-08-30 23:51:14 +04:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-09-24 08:04:31 +04:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2011-06-16 03:02:42 +04:00
|
|
|
#include "llvm/InlineAsm.h"
|
2007-08-21 21:43:55 +04:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult;
|
|
|
|
static TryEmitResult
|
|
|
|
tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e);
|
|
|
|
|
|
|
|
/// Given the address of a variable of pointer type, find the correct
|
|
|
|
/// null to store into it.
|
|
|
|
static llvm::Constant *getNullForVariable(llvm::Value *addr) {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *type =
|
2011-06-16 03:02:42 +04:00
|
|
|
cast<llvm::PointerType>(addr->getType())->getElementType();
|
|
|
|
return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type));
|
|
|
|
}
|
|
|
|
|
2008-06-24 21:04:18 +04:00
|
|
|
/// Emits an instance of NSConstantString representing the object.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E)
|
2008-11-26 00:53:21 +03:00
|
|
|
{
|
2010-01-23 05:40:42 +03:00
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.getObjCRuntime().GenerateConstantString(E->getString());
|
2008-08-20 04:28:19 +04:00
|
|
|
// FIXME: This bitcast should just be made an invariant on the Runtime.
|
2009-07-29 22:54:39 +04:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
|
2008-06-24 21:04:18 +04: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.
|
2010-02-03 23:11:42 +03:00
|
|
|
return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
|
2008-06-24 21:04:18 +04:00
|
|
|
}
|
|
|
|
|
2008-08-20 04:28:19 +04: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 21:04:18 +04:00
|
|
|
|
2011-06-11 05:09:30 +04:00
|
|
|
/// \brief Adjust the type of the result of an Objective-C message send
|
|
|
|
/// expression when the method has a related result type.
|
|
|
|
static RValue AdjustRelatedResultType(CodeGenFunction &CGF,
|
|
|
|
const Expr *E,
|
|
|
|
const ObjCMethodDecl *Method,
|
|
|
|
RValue Result) {
|
|
|
|
if (!Method)
|
|
|
|
return Result;
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2011-06-11 05:09:30 +04:00
|
|
|
if (!Method->hasRelatedResultType() ||
|
|
|
|
CGF.getContext().hasSameType(E->getType(), Method->getResultType()) ||
|
|
|
|
!Result.isScalar())
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
// We have applied a related result type. Cast the rvalue appropriately.
|
|
|
|
return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(),
|
|
|
|
CGF.ConvertType(E->getType())));
|
|
|
|
}
|
2008-06-24 21:04:18 +04:00
|
|
|
|
2011-07-22 12:53:00 +04:00
|
|
|
/// Decide whether to extend the lifetime of the receiver of a
|
|
|
|
/// returns-inner-pointer message.
|
|
|
|
static bool
|
|
|
|
shouldExtendReceiverForInnerPointerMessage(const ObjCMessageExpr *message) {
|
|
|
|
switch (message->getReceiverKind()) {
|
|
|
|
|
|
|
|
// For a normal instance message, we should extend unless the
|
|
|
|
// receiver is loaded from a variable with precise lifetime.
|
|
|
|
case ObjCMessageExpr::Instance: {
|
|
|
|
const Expr *receiver = message->getInstanceReceiver();
|
|
|
|
const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(receiver);
|
|
|
|
if (!ice || ice->getCastKind() != CK_LValueToRValue) return true;
|
|
|
|
receiver = ice->getSubExpr()->IgnoreParens();
|
|
|
|
|
|
|
|
// Only __strong variables.
|
|
|
|
if (receiver->getType().getObjCLifetime() != Qualifiers::OCL_Strong)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// All ivars and fields have precise lifetime.
|
|
|
|
if (isa<MemberExpr>(receiver) || isa<ObjCIvarRefExpr>(receiver))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise, check for variables.
|
|
|
|
const DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(ice->getSubExpr());
|
|
|
|
if (!declRef) return true;
|
|
|
|
const VarDecl *var = dyn_cast<VarDecl>(declRef->getDecl());
|
|
|
|
if (!var) return true;
|
|
|
|
|
|
|
|
// All variables have precise lifetime except local variables with
|
|
|
|
// automatic storage duration that aren't specially marked.
|
|
|
|
return (var->hasLocalStorage() &&
|
|
|
|
!var->hasAttr<ObjCPreciseLifetimeAttr>());
|
|
|
|
}
|
|
|
|
|
|
|
|
case ObjCMessageExpr::Class:
|
|
|
|
case ObjCMessageExpr::SuperClass:
|
|
|
|
// It's never necessary for class objects.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case ObjCMessageExpr::SuperInstance:
|
|
|
|
// We generally assume that 'self' lives throughout a method call.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("invalid receiver kind");
|
|
|
|
}
|
|
|
|
|
2010-05-22 05:48:05 +04:00
|
|
|
RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E,
|
|
|
|
ReturnValueSlot Return) {
|
2008-06-24 21:04:18 +04: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.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
bool isDelegateInit = E->isDelegateInitCall();
|
|
|
|
|
2011-07-22 12:53:00 +04:00
|
|
|
const ObjCMethodDecl *method = E->getMethodDecl();
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
// We don't retain the receiver in delegate init calls, and this is
|
|
|
|
// safe because the receiver value is always loaded from 'self',
|
|
|
|
// which we zero out. We don't want to Block_copy block receivers,
|
|
|
|
// though.
|
|
|
|
bool retainSelf =
|
|
|
|
(!isDelegateInit &&
|
|
|
|
CGM.getLangOptions().ObjCAutoRefCount &&
|
2011-07-22 12:53:00 +04:00
|
|
|
method &&
|
|
|
|
method->hasAttr<NSConsumesSelfAttr>());
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2008-08-11 22:12:00 +04:00
|
|
|
CGObjCRuntime &Runtime = CGM.getObjCRuntime();
|
2008-06-24 21:04:18 +04:00
|
|
|
bool isSuperMessage = false;
|
2008-08-25 12:19:24 +04:00
|
|
|
bool isClassMessage = false;
|
2010-04-28 23:33:36 +04:00
|
|
|
ObjCInterfaceDecl *OID = 0;
|
2008-06-24 21:04:18 +04:00
|
|
|
// Find the receiver
|
2011-06-11 05:09:30 +04:00
|
|
|
QualType ReceiverType;
|
2010-04-22 07:17:06 +04:00
|
|
|
llvm::Value *Receiver = 0;
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
switch (E->getReceiverKind()) {
|
|
|
|
case ObjCMessageExpr::Instance:
|
2011-06-11 05:09:30 +04:00
|
|
|
ReceiverType = E->getInstanceReceiver()->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
if (retainSelf) {
|
|
|
|
TryEmitResult ter = tryEmitARCRetainScalarExpr(*this,
|
|
|
|
E->getInstanceReceiver());
|
|
|
|
Receiver = ter.getPointer();
|
2011-07-22 12:53:00 +04:00
|
|
|
if (ter.getInt()) retainSelf = false;
|
2011-06-16 03:02:42 +04:00
|
|
|
} else
|
|
|
|
Receiver = EmitScalarExpr(E->getInstanceReceiver());
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
break;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
case ObjCMessageExpr::Class: {
|
2011-06-11 05:09:30 +04:00
|
|
|
ReceiverType = E->getClassReceiver();
|
|
|
|
const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>();
|
2010-05-18 00:12:43 +04:00
|
|
|
assert(ObjTy && "Invalid Objective-C class message send");
|
|
|
|
OID = ObjTy->getInterface();
|
|
|
|
assert(OID && "Invalid Objective-C class message send");
|
2010-04-28 23:33:36 +04:00
|
|
|
Receiver = Runtime.GetClass(Builder, OID);
|
2008-08-25 12:19:24 +04:00
|
|
|
isClassMessage = true;
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ObjCMessageExpr::SuperInstance:
|
2011-06-11 05:09:30 +04:00
|
|
|
ReceiverType = E->getSuperType();
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
Receiver = LoadObjCSelf();
|
2008-06-24 21:04:18 +04:00
|
|
|
isSuperMessage = true;
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ObjCMessageExpr::SuperClass:
|
2011-06-11 05:09:30 +04:00
|
|
|
ReceiverType = E->getSuperType();
|
2008-06-24 21:04:18 +04:00
|
|
|
Receiver = LoadObjCSelf();
|
Overhaul the AST representation of Objective-C message send
expressions, to improve source-location information, clarify the
actual receiver of the message, and pave the way for proper C++
support. The ObjCMessageExpr node represents four different kinds of
message sends in a single AST node:
1) Send to a object instance described by an expression (e.g., [x method:5])
2) Send to a class described by the class name (e.g., [NSString method:5])
3) Send to a superclass class (e.g, [super method:5] in class method)
4) Send to a superclass instance (e.g., [super method:5] in instance method)
Previously these four cases where tangled together. Now, they have
more distinct representations. Specific changes:
1) Unchanged; the object instance is represented by an Expr*.
2) Previously stored the ObjCInterfaceDecl* referring to the class
receiving the message. Now stores a TypeSourceInfo* so that we know
how the class was spelled. This both maintains typedef information
and opens the door for more complicated C++ types (e.g., dependent
types). There was an alternative, unused representation of these
sends by naming the class via an IdentifierInfo *. In practice, we
either had an ObjCInterfaceDecl *, from which we would get the
IdentifierInfo *, or we fell into the case below...
3) Previously represented by a class message whose IdentifierInfo *
referred to "super". Sema and CodeGen would use isStr("super") to
determine if they had a send to super. Now represented as a
"class super" send, where we have both the location of the "super"
keyword and the ObjCInterfaceDecl* of the superclass we're
targetting (statically).
4) Previously represented by an instance message whose receiver is a
an ObjCSuperExpr, which Sema and CodeGen would check for via
isa<ObjCSuperExpr>(). Now represented as an "instance super" send,
where we have both the location of the "super" keyword and the
ObjCInterfaceDecl* of the superclass we're targetting
(statically). Note that ObjCSuperExpr only has one remaining use in
the AST, which is for "super.prop" references.
The new representation of ObjCMessageExpr is 2 pointers smaller than
the old one, since it combines more storage. It also eliminates a leak
when we loaded message-send expressions from a precompiled header. The
representation also feels much cleaner to me; comments welcome!
This patch attempts to maintain the same semantics we previously had
with Objective-C message sends. In several places, there are massive
changes that boil down to simply replacing a nested-if structure such
as:
if (message has a receiver expression) {
// instance message
if (isa<ObjCSuperExpr>(...)) {
// send to super
} else {
// send to an object
}
} else {
// class message
if (name->isStr("super")) {
// class send to super
} else {
// send to class
}
}
with a switch
switch (E->getReceiverKind()) {
case ObjCMessageExpr::SuperInstance: ...
case ObjCMessageExpr::Instance: ...
case ObjCMessageExpr::SuperClass: ...
case ObjCMessageExpr::Class:...
}
There are quite a few places (particularly in the checkers) where
send-to-super is effectively ignored. I've placed FIXMEs in most of
them, and attempted to address send-to-super in a reasonable way. This
could use some review.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@101972 91177308-0d34-0410-b5e6-96231b3b80d8
2010-04-21 04:45:42 +04:00
|
|
|
isSuperMessage = true;
|
|
|
|
isClassMessage = true;
|
|
|
|
break;
|
2008-06-24 21:04:18 +04:00
|
|
|
}
|
|
|
|
|
2011-07-22 12:53:00 +04:00
|
|
|
if (retainSelf)
|
|
|
|
Receiver = EmitARCRetainNonBlock(Receiver);
|
|
|
|
|
|
|
|
// In ARC, we sometimes want to "extend the lifetime"
|
|
|
|
// (i.e. retain+autorelease) of receivers of returns-inner-pointer
|
|
|
|
// messages.
|
|
|
|
if (getLangOptions().ObjCAutoRefCount && method &&
|
|
|
|
method->hasAttr<ObjCReturnsInnerPointerAttr>() &&
|
|
|
|
shouldExtendReceiverForInnerPointerMessage(E))
|
|
|
|
Receiver = EmitARCRetainAutorelease(ReceiverType, Receiver);
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
QualType ResultType =
|
2011-07-22 12:53:00 +04:00
|
|
|
method ? method->getResultType() : E->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2008-08-30 07:02:31 +04:00
|
|
|
CallArgList Args;
|
2011-07-22 12:53:00 +04:00
|
|
|
EmitCallArgs(Args, method, E->arg_begin(), E->arg_end());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
// For delegate init calls in ARC, do an unsafe store of null into
|
|
|
|
// self. This represents the call taking direct ownership of that
|
|
|
|
// value. We have to do this after emitting the other call
|
|
|
|
// arguments because they might also reference self, but we don't
|
|
|
|
// have to worry about any of them modifying self because that would
|
|
|
|
// be an undefined read and write of an object in unordered
|
|
|
|
// expressions.
|
|
|
|
if (isDelegateInit) {
|
|
|
|
assert(getLangOptions().ObjCAutoRefCount &&
|
|
|
|
"delegate init calls should only be marked in ARC");
|
|
|
|
|
|
|
|
// Do an unsafe store of null into self.
|
|
|
|
llvm::Value *selfAddr =
|
|
|
|
LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
|
|
|
|
assert(selfAddr && "no self entry for a delegate init call?");
|
|
|
|
|
|
|
|
Builder.CreateStore(getNullForVariable(selfAddr), selfAddr);
|
|
|
|
}
|
2010-06-22 00:59:55 +04:00
|
|
|
|
2011-06-11 05:09:30 +04:00
|
|
|
RValue result;
|
2008-06-24 21:04:18 +04:00
|
|
|
if (isSuperMessage) {
|
2008-06-26 08:42:20 +04:00
|
|
|
// super is only valid in an Objective-C method
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
2009-02-28 23:07:56 +03:00
|
|
|
bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
|
2011-06-11 05:09:30 +04:00
|
|
|
result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType,
|
|
|
|
E->getSelector(),
|
|
|
|
OMD->getClassInterface(),
|
|
|
|
isCategoryImpl,
|
|
|
|
Receiver,
|
|
|
|
isClassMessage,
|
|
|
|
Args,
|
2011-07-22 12:53:00 +04:00
|
|
|
method);
|
2011-06-11 05:09:30 +04:00
|
|
|
} else {
|
|
|
|
result = Runtime.GenerateMessageSend(*this, Return, ResultType,
|
|
|
|
E->getSelector(),
|
|
|
|
Receiver, Args, OID,
|
2011-07-22 12:53:00 +04:00
|
|
|
method);
|
2008-06-24 21:04:18 +04:00
|
|
|
}
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
// For delegate init calls in ARC, implicitly store the result of
|
|
|
|
// the call back into self. This takes ownership of the value.
|
|
|
|
if (isDelegateInit) {
|
|
|
|
llvm::Value *selfAddr =
|
|
|
|
LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()];
|
|
|
|
llvm::Value *newSelf = result.getScalarVal();
|
|
|
|
|
|
|
|
// The delegate return type isn't necessarily a matching type; in
|
|
|
|
// fact, it's quite likely to be 'id'.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *selfTy =
|
2011-06-16 03:02:42 +04:00
|
|
|
cast<llvm::PointerType>(selfAddr->getType())->getElementType();
|
|
|
|
newSelf = Builder.CreateBitCast(newSelf, selfTy);
|
|
|
|
|
|
|
|
Builder.CreateStore(newSelf, selfAddr);
|
|
|
|
}
|
|
|
|
|
2011-07-22 12:53:00 +04:00
|
|
|
return AdjustRelatedResultType(*this, E, method, result);
|
2007-08-21 21:43:55 +04:00
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
namespace {
|
|
|
|
struct FinishARCDealloc : EHScopeStack::Cleanup {
|
2011-07-13 00:27:29 +04:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-06-16 03:02:42 +04:00
|
|
|
const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl);
|
2011-07-13 22:26:47 +04:00
|
|
|
|
|
|
|
const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext());
|
2011-06-16 03:02:42 +04:00
|
|
|
const ObjCInterfaceDecl *iface = impl->getClassInterface();
|
|
|
|
if (!iface->getSuperClass()) return;
|
|
|
|
|
2011-07-13 22:26:47 +04:00
|
|
|
bool isCategory = isa<ObjCCategoryImplDecl>(impl);
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
// Call [super dealloc] if we have a superclass.
|
|
|
|
llvm::Value *self = CGF.LoadObjCSelf();
|
|
|
|
|
|
|
|
CallArgList args;
|
|
|
|
CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(),
|
|
|
|
CGF.getContext().VoidTy,
|
|
|
|
method->getSelector(),
|
|
|
|
iface,
|
2011-07-13 22:26:47 +04:00
|
|
|
isCategory,
|
2011-06-16 03:02:42 +04:00
|
|
|
self,
|
|
|
|
/*is class msg*/ false,
|
|
|
|
args,
|
|
|
|
method);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
|
|
|
|
/// the LLVM function and sets the other context used by
|
|
|
|
/// CodeGenFunction.
|
2009-01-11 00:06:09 +03:00
|
|
|
void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD,
|
2011-05-20 03:37:41 +04:00
|
|
|
const ObjCContainerDecl *CD,
|
|
|
|
SourceLocation StartLoc) {
|
2011-03-09 07:27:21 +03:00
|
|
|
FunctionArgList args;
|
2010-04-06 01:09:15 +04:00
|
|
|
// Check if we should generate debug info for this method.
|
2011-03-07 21:45:56 +03:00
|
|
|
if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>())
|
|
|
|
DebugInfo = CGM.getModuleDebugInfo();
|
2010-04-06 01:09:15 +04:00
|
|
|
|
2009-01-11 00:06:09 +03:00
|
|
|
llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD);
|
2008-09-05 03:41:35 +04:00
|
|
|
|
2009-04-17 04:48:04 +04:00
|
|
|
const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD);
|
|
|
|
CGM.SetInternalFunctionAttributes(OMD, Fn, FI);
|
2008-06-17 22:05:57 +04:00
|
|
|
|
2011-03-09 07:27:21 +03:00
|
|
|
args.push_back(OMD->getSelfDecl());
|
|
|
|
args.push_back(OMD->getCmdDecl());
|
2008-06-17 22:05:57 +04:00
|
|
|
|
2009-02-20 21:43:26 +03:00
|
|
|
for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(),
|
|
|
|
E = OMD->param_end(); PI != E; ++PI)
|
2011-03-09 07:27:21 +03:00
|
|
|
args.push_back(*PI);
|
2008-08-16 07:19:19 +04:00
|
|
|
|
2011-01-13 21:57:25 +03:00
|
|
|
CurGD = OMD;
|
|
|
|
|
2011-05-20 03:37:41 +04:00
|
|
|
StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc);
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
// In ARC, certain methods get an extra cleanup.
|
|
|
|
if (CGM.getLangOptions().ObjCAutoRefCount &&
|
|
|
|
OMD->isInstanceMethod() &&
|
|
|
|
OMD->getSelector().isUnarySelector()) {
|
|
|
|
const IdentifierInfo *ident =
|
|
|
|
OMD->getSelector().getIdentifierInfoForSlot(0);
|
|
|
|
if (ident->isStr("dealloc"))
|
|
|
|
EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind());
|
|
|
|
}
|
2008-08-26 12:29:31 +04:00
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
|
|
|
|
LValue lvalue, QualType type);
|
|
|
|
|
2011-02-18 22:15:13 +03:00
|
|
|
void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar,
|
|
|
|
bool IsAtomic, bool IsStrong) {
|
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
|
|
|
|
Ivar, 0);
|
|
|
|
llvm::Value *GetCopyStructFn =
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction();
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
// objc_copyStruct (ReturnValue, &structIvar,
|
|
|
|
// sizeof (Type of Ivar), isAtomic, false);
|
|
|
|
CallArgList Args;
|
2011-05-15 05:53:33 +04:00
|
|
|
RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, VoidPtrTy));
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RV, getContext().VoidPtrTy);
|
2011-05-15 05:53:33 +04:00
|
|
|
RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), VoidPtrTy));
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RV, getContext().VoidPtrTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
// sizeof (Type of Ivar)
|
|
|
|
CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
|
|
|
|
llvm::Value *SizeVal =
|
2011-05-15 05:53:33 +04:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
|
2011-02-18 22:15:13 +03:00
|
|
|
Size.getQuantity());
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(SizeVal), getContext().LongTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
llvm::Value *isAtomic =
|
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
|
|
|
|
IsAtomic ? 1 : 0);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(isAtomic), getContext().BoolTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
llvm::Value *hasStrong =
|
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy),
|
|
|
|
IsStrong ? 1 : 0);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(hasStrong), getContext().BoolTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
|
|
|
|
FunctionType::ExtInfo()),
|
|
|
|
GetCopyStructFn, ReturnValueSlot(), Args);
|
|
|
|
}
|
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
/// Generate an Objective-C method. An Objective-C method is a C function with
|
2009-09-09 19:08:12 +04:00
|
|
|
/// its pointer, name, and types registered in the class struture.
|
2008-08-26 12:29:31 +04:00
|
|
|
void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
|
2011-05-20 03:37:41 +04:00
|
|
|
StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart());
|
2009-06-30 06:35:26 +04:00
|
|
|
EmitStmt(OMD->getBody());
|
|
|
|
FinishFunction(OMD->getBodyRBrace());
|
2008-08-26 12:29:31 +04:00
|
|
|
}
|
|
|
|
|
2009-05-16 11:57:57 +04: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.
|
2008-08-26 12:29:31 +04:00
|
|
|
|
|
|
|
/// GenerateObjCGetter - Generate an Objective-C property getter
|
2009-01-11 01:55:25 +03:00
|
|
|
/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
|
|
|
|
/// is illegal within a category.
|
2008-12-09 23:23:04 +03:00
|
|
|
void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP,
|
|
|
|
const ObjCPropertyImplDecl *PID) {
|
2008-09-24 08:04:31 +04:00
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
2008-08-26 12:29:31 +04:00
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
2010-04-13 22:32:24 +04:00
|
|
|
bool IsAtomic =
|
|
|
|
!(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic);
|
2008-08-26 12:29:31 +04:00
|
|
|
ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
|
|
|
|
assert(OMD && "Invalid call to generate getter (empty method)");
|
2011-05-20 03:37:41 +04:00
|
|
|
StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
|
2010-04-13 22:32:24 +04:00
|
|
|
|
2008-09-24 08:04:31 +04:00
|
|
|
// Determine if we should use an objc_getProperty call for
|
2008-12-09 02:56:17 +03:00
|
|
|
// this. Non-atomic properties are directly evaluated.
|
|
|
|
// atomic 'copy' and 'retain' properties are also directly
|
|
|
|
// evaluated in gc-only mode.
|
2008-09-24 08:04:31 +04:00
|
|
|
if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
|
2010-04-13 22:32:24 +04:00
|
|
|
IsAtomic &&
|
2008-12-09 02:56:17 +03:00
|
|
|
(PD->getSetterKind() == ObjCPropertyDecl::Copy ||
|
|
|
|
PD->getSetterKind() == ObjCPropertyDecl::Retain)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *GetPropertyFn =
|
2008-09-24 08:04:31 +04:00
|
|
|
CGM.getObjCRuntime().GetPropertyGetFunction();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-24 08:04:31 +04:00
|
|
|
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();
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *SelfAsId =
|
2008-09-24 08:04:31 +04:00
|
|
|
Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
|
2008-12-09 23:23:04 +03:00
|
|
|
llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
|
2008-09-24 08:04:31 +04:00
|
|
|
llvm::Value *True =
|
2009-07-25 03:12:58 +04:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
|
2008-09-24 08:04:31 +04:00
|
|
|
CallArgList Args;
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(SelfAsId), IdTy);
|
|
|
|
Args.add(RValue::get(CmdVal), Cmd->getType());
|
|
|
|
Args.add(RValue::get(Offset), getContext().getPointerDiffType());
|
|
|
|
Args.add(RValue::get(True), getContext().BoolTy);
|
2009-02-04 02:43:59 +03:00
|
|
|
// FIXME: We shouldn't need to get the function info here, the
|
|
|
|
// runtime already should have computed it to build the function.
|
2010-02-06 00:31:56 +03:00
|
|
|
RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args,
|
2010-03-31 00:24:48 +04:00
|
|
|
FunctionType::ExtInfo()),
|
2009-12-24 22:25:24 +03:00
|
|
|
GetPropertyFn, ReturnValueSlot(), Args);
|
2008-09-24 08:04:31 +04: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.
|
2009-09-09 19:08:12 +04:00
|
|
|
RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
|
2008-09-24 08:04:31 +04:00
|
|
|
Types.ConvertType(PD->getType())));
|
|
|
|
EmitReturnOfRValue(RV, PD->getType());
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
// objc_getProperty does an autorelease, so we should suppress ours.
|
|
|
|
AutoreleaseResult = false;
|
2008-09-24 08:04:31 +04:00
|
|
|
} else {
|
2011-02-18 22:15:13 +03:00
|
|
|
const llvm::Triple &Triple = getContext().Target.getTriple();
|
|
|
|
QualType IVART = Ivar->getType();
|
|
|
|
if (IsAtomic &&
|
|
|
|
IVART->isScalarType() &&
|
|
|
|
(Triple.getArch() == llvm::Triple::arm ||
|
|
|
|
Triple.getArch() == llvm::Triple::thumb) &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCGetterBody(Ivar, true, false);
|
|
|
|
}
|
2011-04-06 01:41:23 +04:00
|
|
|
else if (IsAtomic &&
|
|
|
|
(IVART->isScalarType() && !IVART->isRealFloatingType()) &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCGetterBody(Ivar, true, false);
|
|
|
|
}
|
|
|
|
else if (IsAtomic &&
|
|
|
|
(IVART->isScalarType() && !IVART->isRealFloatingType()) &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86_64 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(8)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCGetterBody(Ivar, true, false);
|
|
|
|
}
|
2011-02-18 22:15:13 +03:00
|
|
|
else if (IVART->isAnyComplexType()) {
|
2010-05-06 19:45:36 +04:00
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
|
|
|
|
Ivar, 0);
|
2010-03-26 00:56:43 +03:00
|
|
|
ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(),
|
|
|
|
LV.isVolatileQualified());
|
|
|
|
StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified());
|
|
|
|
}
|
2011-02-18 22:15:13 +03:00
|
|
|
else if (hasAggregateLLVMType(IVART)) {
|
2010-04-13 22:32:24 +04:00
|
|
|
bool IsStrong = false;
|
2011-04-06 03:01:27 +04:00
|
|
|
if ((IsStrong = IvarTypeWithAggrGCObjects(IVART))
|
2010-04-13 04:38:05 +04:00
|
|
|
&& CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect
|
2010-12-27 01:13:16 +03:00
|
|
|
&& CGM.getObjCRuntime().GetGetStructFunction()) {
|
2011-02-18 22:15:13 +03:00
|
|
|
GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong);
|
2010-04-13 04:38:05 +04:00
|
|
|
}
|
2010-05-06 19:45:36 +04:00
|
|
|
else {
|
2011-04-06 20:05:26 +04:00
|
|
|
const CXXRecordDecl *classDecl = IVART->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
if (PID->getGetterCXXConstructor() &&
|
2011-05-09 22:22:59 +04:00
|
|
|
classDecl && !classDecl->hasTrivialDefaultConstructor()) {
|
2010-05-06 19:45:36 +04:00
|
|
|
ReturnStmt *Stmt =
|
|
|
|
new (getContext()) ReturnStmt(SourceLocation(),
|
2010-05-15 10:01:05 +04:00
|
|
|
PID->getGetterCXXConstructor(),
|
|
|
|
0);
|
2010-05-06 19:45:36 +04:00
|
|
|
EmitReturnStmt(*Stmt);
|
2011-04-06 01:41:23 +04:00
|
|
|
} else if (IsAtomic &&
|
|
|
|
!IVART->isAnyComplexType() &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCGetterBody(Ivar, true, false);
|
|
|
|
}
|
|
|
|
else if (IsAtomic &&
|
|
|
|
!IVART->isAnyComplexType() &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86_64 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(8)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCGetterBody(Ivar, true, false);
|
2010-05-06 19:45:36 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
|
|
|
|
Ivar, 0);
|
2011-02-18 22:15:13 +03:00
|
|
|
EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART);
|
2010-05-06 19:45:36 +04:00
|
|
|
}
|
|
|
|
}
|
2011-07-22 09:23:13 +04:00
|
|
|
} else {
|
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(),
|
|
|
|
Ivar, 0);
|
|
|
|
QualType propType = PD->getType();
|
|
|
|
|
|
|
|
llvm::Value *value;
|
|
|
|
if (propType->isReferenceType()) {
|
|
|
|
value = LV.getAddress();
|
|
|
|
} else {
|
|
|
|
// We want to load and autoreleaseReturnValue ARC __weak ivars.
|
|
|
|
if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) {
|
|
|
|
value = emitARCRetainLoadOfScalar(*this, LV, IVART);
|
|
|
|
|
|
|
|
// Otherwise we want to do a simple load, suppressing the
|
|
|
|
// final autorelease.
|
2011-06-16 03:02:42 +04:00
|
|
|
} else {
|
2011-07-22 09:23:13 +04:00
|
|
|
value = EmitLoadOfLValue(LV).getScalarVal();
|
|
|
|
AutoreleaseResult = false;
|
2011-03-29 03:47:18 +04:00
|
|
|
}
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2011-07-22 09:23:13 +04:00
|
|
|
value = Builder.CreateBitCast(value, ConvertType(propType));
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitReturnOfRValue(RValue::get(value), propType);
|
2009-03-03 21:49:40 +03:00
|
|
|
}
|
2008-09-24 08:04:31 +04:00
|
|
|
}
|
2008-08-26 12:29:31 +04:00
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
|
2011-02-18 22:15:13 +03:00
|
|
|
void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
|
|
|
|
ObjCIvarDecl *Ivar) {
|
|
|
|
// objc_copyStruct (&structIvar, &Arg,
|
|
|
|
// sizeof (struct something), true, false);
|
|
|
|
llvm::Value *GetCopyStructFn =
|
|
|
|
CGM.getObjCRuntime().GetSetStructFunction();
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
CallArgList Args;
|
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0);
|
|
|
|
RValue RV =
|
|
|
|
RValue::get(Builder.CreateBitCast(LV.getAddress(),
|
|
|
|
Types.ConvertType(getContext().VoidPtrTy)));
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RV, getContext().VoidPtrTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
|
|
|
|
llvm::Value *ArgAsPtrTy =
|
|
|
|
Builder.CreateBitCast(Arg,
|
|
|
|
Types.ConvertType(getContext().VoidPtrTy));
|
|
|
|
RV = RValue::get(ArgAsPtrTy);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RV, getContext().VoidPtrTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
// sizeof (Type of Ivar)
|
|
|
|
CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType());
|
|
|
|
llvm::Value *SizeVal =
|
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy),
|
|
|
|
Size.getQuantity());
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(SizeVal), getContext().LongTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
llvm::Value *True =
|
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(True), getContext().BoolTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
llvm::Value *False =
|
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(False), getContext().BoolTy);
|
2011-02-18 22:15:13 +03:00
|
|
|
EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
|
|
|
|
FunctionType::ExtInfo()),
|
|
|
|
GetCopyStructFn, ReturnValueSlot(), Args);
|
|
|
|
}
|
|
|
|
|
2011-04-06 20:05:26 +04:00
|
|
|
static bool
|
|
|
|
IvarAssignHasTrvialAssignment(const ObjCPropertyImplDecl *PID,
|
|
|
|
QualType IvarT) {
|
|
|
|
bool HasTrvialAssignment = true;
|
|
|
|
if (PID->getSetterCXXAssignment()) {
|
|
|
|
const CXXRecordDecl *classDecl = IvarT->getAsCXXRecordDecl();
|
|
|
|
HasTrvialAssignment =
|
|
|
|
(!classDecl || classDecl->hasTrivialCopyAssignment());
|
|
|
|
}
|
|
|
|
return HasTrvialAssignment;
|
|
|
|
}
|
|
|
|
|
2008-08-26 12:29:31 +04:00
|
|
|
/// GenerateObjCSetter - Generate an Objective-C property setter
|
2009-01-11 01:55:25 +03:00
|
|
|
/// function. The given Decl must be an ObjCImplementationDecl. @synthesize
|
|
|
|
/// is illegal within a category.
|
2008-12-09 23:23:04 +03:00
|
|
|
void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP,
|
|
|
|
const ObjCPropertyImplDecl *PID) {
|
2008-09-24 10:32:09 +04:00
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
2008-08-26 12:29:31 +04:00
|
|
|
const ObjCPropertyDecl *PD = PID->getPropertyDecl();
|
|
|
|
ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
|
|
|
|
assert(OMD && "Invalid call to generate setter (empty method)");
|
2011-05-20 03:37:41 +04:00
|
|
|
StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart());
|
2011-04-06 01:41:23 +04:00
|
|
|
const llvm::Triple &Triple = getContext().Target.getTriple();
|
|
|
|
QualType IVART = Ivar->getType();
|
2008-09-24 10:32:09 +04:00
|
|
|
bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy;
|
2009-09-09 19:08:12 +04:00
|
|
|
bool IsAtomic =
|
2008-09-24 10:32:09 +04:00
|
|
|
!(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)) {
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *SetPropertyFn =
|
2008-09-24 10:32:09 +04:00
|
|
|
CGM.getObjCRuntime().GetPropertySetFunction();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-24 10:32:09 +04:00
|
|
|
if (!SetPropertyFn) {
|
|
|
|
CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
|
|
|
|
FinishFunction();
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
// Emit objc_setProperty((id) self, _cmd, offset, arg,
|
2008-09-24 10:32:09 +04:00
|
|
|
// <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();
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *SelfAsId =
|
2008-09-24 10:32:09 +04:00
|
|
|
Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
|
2008-12-09 23:23:04 +03:00
|
|
|
llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar);
|
2009-02-20 21:43:26 +03:00
|
|
|
llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()];
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *ArgAsId =
|
2008-09-24 10:32:09 +04:00
|
|
|
Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"),
|
|
|
|
Types.ConvertType(IdTy));
|
|
|
|
llvm::Value *True =
|
2009-07-25 03:12:58 +04:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1);
|
2008-09-24 10:32:09 +04:00
|
|
|
llvm::Value *False =
|
2009-07-25 03:12:58 +04:00
|
|
|
llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0);
|
2008-09-24 10:32:09 +04:00
|
|
|
CallArgList Args;
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(SelfAsId), IdTy);
|
|
|
|
Args.add(RValue::get(CmdVal), Cmd->getType());
|
|
|
|
Args.add(RValue::get(Offset), getContext().getPointerDiffType());
|
|
|
|
Args.add(RValue::get(ArgAsId), IdTy);
|
|
|
|
Args.add(RValue::get(IsAtomic ? True : False), getContext().BoolTy);
|
|
|
|
Args.add(RValue::get(IsCopy ? True : False), getContext().BoolTy);
|
2009-05-16 11:57:57 +04:00
|
|
|
// FIXME: We shouldn't need to get the function info here, the runtime
|
|
|
|
// already should have computed it to build the function.
|
2010-02-06 00:31:56 +03:00
|
|
|
EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args,
|
2010-03-31 00:24:48 +04:00
|
|
|
FunctionType::ExtInfo()),
|
|
|
|
SetPropertyFn,
|
2009-12-24 22:25:24 +03:00
|
|
|
ReturnValueSlot(), Args);
|
2011-04-06 01:41:23 +04:00
|
|
|
} else if (IsAtomic && hasAggregateLLVMType(IVART) &&
|
|
|
|
!IVART->isAnyComplexType() &&
|
2011-04-06 20:05:26 +04:00
|
|
|
IvarAssignHasTrvialAssignment(PID, IVART) &&
|
2011-04-06 01:41:23 +04:00
|
|
|
((Triple.getArch() == llvm::Triple::x86 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4))) ||
|
|
|
|
(Triple.getArch() == llvm::Triple::x86_64 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(8))))
|
2010-12-27 01:13:16 +03:00
|
|
|
&& CGM.getObjCRuntime().GetSetStructFunction()) {
|
2011-04-06 01:41:23 +04:00
|
|
|
// objc_copyStruct (&structIvar, &Arg,
|
|
|
|
// sizeof (struct something), true, false);
|
2011-02-18 22:15:13 +03:00
|
|
|
GenerateObjCAtomicSetterBody(OMD, Ivar);
|
2010-05-06 19:45:36 +04:00
|
|
|
} else if (PID->getSetterCXXAssignment()) {
|
2010-12-05 05:00:02 +03:00
|
|
|
EmitIgnoredExpr(PID->getSetterCXXAssignment());
|
2008-09-24 10:32:09 +04:00
|
|
|
} else {
|
2011-02-18 22:15:13 +03:00
|
|
|
if (IsAtomic &&
|
|
|
|
IVART->isScalarType() &&
|
|
|
|
(Triple.getArch() == llvm::Triple::arm ||
|
|
|
|
Triple.getArch() == llvm::Triple::thumb) &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCAtomicSetterBody(OMD, Ivar);
|
2011-04-06 01:41:23 +04:00
|
|
|
}
|
|
|
|
else if (IsAtomic &&
|
|
|
|
(IVART->isScalarType() && !IVART->isRealFloatingType()) &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(4)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCAtomicSetterBody(OMD, Ivar);
|
|
|
|
}
|
|
|
|
else if (IsAtomic &&
|
|
|
|
(IVART->isScalarType() && !IVART->isRealFloatingType()) &&
|
|
|
|
Triple.getArch() == llvm::Triple::x86_64 &&
|
|
|
|
(getContext().getTypeSizeInChars(IVART)
|
|
|
|
> CharUnits::fromQuantity(8)) &&
|
|
|
|
CGM.getObjCRuntime().GetGetStructFunction()) {
|
|
|
|
GenerateObjCAtomicSetterBody(OMD, Ivar);
|
2011-02-18 22:15:13 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// FIXME: Find a clean way to avoid AST node creation.
|
2011-05-20 03:37:41 +04:00
|
|
|
SourceLocation Loc = PID->getLocStart();
|
2011-02-18 22:15:13 +03:00
|
|
|
ValueDecl *Self = OMD->getSelfDecl();
|
|
|
|
ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
|
|
|
|
DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc);
|
|
|
|
ParmVarDecl *ArgDecl = *OMD->param_begin();
|
2011-03-29 03:47:18 +04:00
|
|
|
QualType T = ArgDecl->getType();
|
|
|
|
if (T->isReferenceType())
|
|
|
|
T = cast<ReferenceType>(T)->getPointeeType();
|
|
|
|
DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc);
|
2011-02-18 22:15:13 +03:00
|
|
|
ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
|
2009-10-27 22:21:30 +03:00
|
|
|
|
2011-02-18 22:15:13 +03:00
|
|
|
// The property type can differ from the ivar type in some situations with
|
|
|
|
// Objective-C pointer types, we can always bit cast the RHS in these cases.
|
|
|
|
if (getContext().getCanonicalType(Ivar->getType()) !=
|
|
|
|
getContext().getCanonicalType(ArgDecl->getType())) {
|
|
|
|
ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack,
|
|
|
|
Ivar->getType(), CK_BitCast, &Arg,
|
|
|
|
VK_RValue);
|
|
|
|
BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign,
|
|
|
|
Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
|
|
|
|
EmitStmt(&Assign);
|
|
|
|
} else {
|
|
|
|
BinaryOperator Assign(&IvarRef, &Arg, BO_Assign,
|
|
|
|
Ivar->getType(), VK_RValue, OK_Ordinary, Loc);
|
|
|
|
EmitStmt(&Assign);
|
|
|
|
}
|
2009-10-27 22:21:30 +03:00
|
|
|
}
|
2008-09-24 10:32:09 +04:00
|
|
|
}
|
2008-08-26 12:29:31 +04:00
|
|
|
|
|
|
|
FinishFunction();
|
2008-06-17 22:05:57 +04:00
|
|
|
}
|
|
|
|
|
2011-03-22 10:05:39 +03:00
|
|
|
namespace {
|
2011-07-12 20:41:08 +04:00
|
|
|
struct DestroyIvar : EHScopeStack::Cleanup {
|
|
|
|
private:
|
|
|
|
llvm::Value *addr;
|
2011-03-22 10:05:39 +03:00
|
|
|
const ObjCIvarDecl *ivar;
|
2011-07-12 20:41:08 +04:00
|
|
|
CodeGenFunction::Destroyer &destroyer;
|
|
|
|
bool useEHCleanupForArray;
|
|
|
|
public:
|
|
|
|
DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar,
|
|
|
|
CodeGenFunction::Destroyer *destroyer,
|
|
|
|
bool useEHCleanupForArray)
|
|
|
|
: addr(addr), ivar(ivar), destroyer(*destroyer),
|
|
|
|
useEHCleanupForArray(useEHCleanupForArray) {}
|
2011-03-22 10:05:39 +03:00
|
|
|
|
2011-07-13 00:27:29 +04:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-07-12 20:41:08 +04:00
|
|
|
LValue lvalue
|
|
|
|
= CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0);
|
|
|
|
CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer,
|
2011-07-13 00:27:29 +04:00
|
|
|
flags.isForNormalCleanup() && useEHCleanupForArray);
|
2011-03-22 10:05:39 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-07-12 20:41:08 +04:00
|
|
|
/// Like CodeGenFunction::destroyARCStrong, but do it with a call.
|
|
|
|
static void destroyARCStrongWithStore(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
QualType type) {
|
|
|
|
llvm::Value *null = getNullForVariable(addr);
|
|
|
|
CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true);
|
|
|
|
}
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2011-03-22 10:05:39 +03:00
|
|
|
static void emitCXXDestructMethod(CodeGenFunction &CGF,
|
|
|
|
ObjCImplementationDecl *impl) {
|
|
|
|
CodeGenFunction::RunCleanupsScope scope(CGF);
|
|
|
|
|
|
|
|
llvm::Value *self = CGF.LoadObjCSelf();
|
|
|
|
|
2011-07-22 06:08:32 +04:00
|
|
|
const ObjCInterfaceDecl *iface = impl->getClassInterface();
|
|
|
|
for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
|
2011-03-22 10:05:39 +03:00
|
|
|
ivar; ivar = ivar->getNextIvar()) {
|
|
|
|
QualType type = ivar->getType();
|
|
|
|
|
|
|
|
// Check whether the ivar is a destructible type.
|
2011-07-12 20:41:08 +04:00
|
|
|
QualType::DestructionKind dtorKind = type.isDestructedType();
|
|
|
|
if (!dtorKind) continue;
|
|
|
|
|
|
|
|
CodeGenFunction::Destroyer *destroyer = 0;
|
|
|
|
|
|
|
|
// Use a call to objc_storeStrong to destroy strong ivars, for the
|
|
|
|
// general benefit of the tools.
|
|
|
|
if (dtorKind == QualType::DK_objc_strong_lifetime) {
|
|
|
|
destroyer = &destroyARCStrongWithStore;
|
|
|
|
|
|
|
|
// Otherwise use the default for the destruction kind.
|
|
|
|
} else {
|
|
|
|
destroyer = &CGF.getDestroyer(dtorKind);
|
2011-03-22 10:05:39 +03:00
|
|
|
}
|
2011-07-12 20:41:08 +04:00
|
|
|
|
|
|
|
CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind);
|
|
|
|
|
|
|
|
CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer,
|
|
|
|
cleanupKind & EHCleanup);
|
2011-03-22 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?");
|
|
|
|
}
|
|
|
|
|
2010-04-29 01:28:56 +04:00
|
|
|
void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
|
|
|
|
ObjCMethodDecl *MD,
|
|
|
|
bool ctor) {
|
|
|
|
MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface());
|
2011-05-20 03:37:41 +04:00
|
|
|
StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart());
|
2011-03-22 10:05:39 +03:00
|
|
|
|
|
|
|
// Emit .cxx_construct.
|
2010-04-29 01:28:56 +04:00
|
|
|
if (ctor) {
|
2011-06-16 03:02:42 +04:00
|
|
|
// Suppress the final autorelease in ARC.
|
|
|
|
AutoreleaseResult = false;
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<CXXCtorInitializer *, 8> IvarInitializers;
|
2011-03-22 10:05:39 +03:00
|
|
|
for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(),
|
|
|
|
E = IMP->init_end(); B != E; ++B) {
|
|
|
|
CXXCtorInitializer *IvarInit = (*B);
|
2010-12-04 12:14:42 +03:00
|
|
|
FieldDecl *Field = IvarInit->getAnyMember();
|
2010-04-29 01:28:56 +04:00
|
|
|
ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field);
|
2010-04-29 02:30:33 +04:00
|
|
|
LValue LV = EmitLValueForIvar(TypeOfSelfObject(),
|
|
|
|
LoadObjCSelf(), Ivar, 0);
|
2010-09-15 14:14:12 +04:00
|
|
|
EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true));
|
2010-04-29 01:28:56 +04:00
|
|
|
}
|
|
|
|
// constructor returns 'self'.
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
QualType IdTy(CGM.getContext().getObjCIdType());
|
|
|
|
llvm::Value *SelfAsId =
|
|
|
|
Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
|
|
|
|
EmitReturnOfRValue(RValue::get(SelfAsId), IdTy);
|
2011-03-22 10:05:39 +03:00
|
|
|
|
|
|
|
// Emit .cxx_destruct.
|
2010-05-06 04:20:39 +04:00
|
|
|
} else {
|
2011-03-22 10:05:39 +03:00
|
|
|
emitCXXDestructMethod(*this, IMP);
|
2010-04-29 01:28:56 +04:00
|
|
|
}
|
|
|
|
FinishFunction();
|
|
|
|
}
|
|
|
|
|
2010-04-13 04:38:05 +04:00
|
|
|
bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) {
|
|
|
|
CGFunctionInfo::const_arg_iterator it = FI.arg_begin();
|
|
|
|
it++; it++;
|
|
|
|
const ABIArgInfo &AI = it->info;
|
|
|
|
// FIXME. Is this sufficient check?
|
|
|
|
return (AI.getKind() == ABIArgInfo::Indirect);
|
|
|
|
}
|
|
|
|
|
2010-04-13 22:32:24 +04:00
|
|
|
bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) {
|
|
|
|
if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
|
|
|
|
return false;
|
|
|
|
if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>())
|
|
|
|
return FDTTy->getDecl()->hasObjectMember();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-24 08:04:31 +04:00
|
|
|
llvm::Value *CodeGenFunction::LoadObjCSelf() {
|
2008-08-16 07:19:19 +04:00
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
|
2008-06-17 22:05:57 +04:00
|
|
|
}
|
|
|
|
|
2009-02-03 03:09:52 +03:00
|
|
|
QualType CodeGenFunction::TypeOfSelfObject() {
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
|
|
|
|
ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
|
2009-07-11 03:34:53 +04:00
|
|
|
const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
|
|
|
|
getContext().getCanonicalType(selfDecl->getType()));
|
2009-02-03 03:09:52 +03:00
|
|
|
return PTy->getPointeeType();
|
|
|
|
}
|
|
|
|
|
2010-12-04 06:11:00 +03:00
|
|
|
LValue
|
|
|
|
CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
|
|
|
|
// This is a special l-value that just issues sends when we load or
|
|
|
|
// store through it.
|
|
|
|
|
|
|
|
// For certain base kinds, we need to emit the base immediately.
|
|
|
|
llvm::Value *Base;
|
|
|
|
if (E->isSuperReceiver())
|
|
|
|
Base = LoadObjCSelf();
|
|
|
|
else if (E->isClassReceiver())
|
|
|
|
Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver());
|
|
|
|
else
|
|
|
|
Base = EmitScalarExpr(E->getBase());
|
|
|
|
return LValue::MakePropertyRef(E, Base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RValue GenerateMessageSendSuper(CodeGenFunction &CGF,
|
|
|
|
ReturnValueSlot Return,
|
|
|
|
QualType ResultType,
|
|
|
|
Selector S,
|
|
|
|
llvm::Value *Receiver,
|
|
|
|
const CallArgList &CallArgs) {
|
|
|
|
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl);
|
2009-03-20 22:18:21 +03:00
|
|
|
bool isClassMessage = OMD->isClassMethod();
|
|
|
|
bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext());
|
2010-12-04 06:11:00 +03:00
|
|
|
return CGF.CGM.getObjCRuntime()
|
|
|
|
.GenerateMessageSendSuper(CGF, Return, ResultType,
|
|
|
|
S, OMD->getClassInterface(),
|
|
|
|
isCategoryImpl, Receiver,
|
|
|
|
isClassMessage, CallArgs);
|
2009-03-20 22:18:21 +03:00
|
|
|
}
|
|
|
|
|
2010-12-04 05:32:38 +03:00
|
|
|
RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
|
|
|
|
ReturnValueSlot Return) {
|
|
|
|
const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr();
|
2011-03-30 20:11:20 +04:00
|
|
|
QualType ResultType = E->getGetterResultType();
|
2010-12-02 04:19:52 +03:00
|
|
|
Selector S;
|
2011-06-11 05:09:30 +04:00
|
|
|
const ObjCMethodDecl *method;
|
2010-12-02 04:19:52 +03:00
|
|
|
if (E->isExplicitProperty()) {
|
|
|
|
const ObjCPropertyDecl *Property = E->getExplicitProperty();
|
|
|
|
S = Property->getGetterName();
|
2011-06-11 05:09:30 +04:00
|
|
|
method = Property->getGetterMethodDecl();
|
2009-07-31 02:28:39 +04:00
|
|
|
} else {
|
2011-06-11 05:09:30 +04:00
|
|
|
method = E->getImplicitPropertyGetter();
|
|
|
|
S = method->getSelector();
|
2008-11-22 21:39:36 +03:00
|
|
|
}
|
2010-12-02 04:19:52 +03:00
|
|
|
|
2010-12-04 06:11:00 +03:00
|
|
|
llvm::Value *Receiver = LV.getPropertyRefBaseAddr();
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
if (CGM.getLangOptions().ObjCAutoRefCount) {
|
|
|
|
QualType receiverType;
|
|
|
|
if (E->isSuperReceiver())
|
|
|
|
receiverType = E->getSuperReceiverType();
|
|
|
|
else if (E->isClassReceiver())
|
|
|
|
receiverType = getContext().getObjCClassType();
|
|
|
|
else
|
|
|
|
receiverType = E->getBase()->getType();
|
|
|
|
}
|
|
|
|
|
2010-12-04 06:11:00 +03:00
|
|
|
// Accesses to 'super' follow a different code path.
|
2010-12-02 04:19:52 +03:00
|
|
|
if (E->isSuperReceiver())
|
2011-06-11 05:09:30 +04:00
|
|
|
return AdjustRelatedResultType(*this, E, method,
|
|
|
|
GenerateMessageSendSuper(*this, Return,
|
|
|
|
ResultType,
|
|
|
|
S, Receiver,
|
|
|
|
CallArgList()));
|
2010-12-04 05:32:38 +03:00
|
|
|
const ObjCInterfaceDecl *ReceiverClass
|
|
|
|
= (E->isClassReceiver() ? E->getClassReceiver() : 0);
|
2011-06-11 05:09:30 +04:00
|
|
|
return AdjustRelatedResultType(*this, E, method,
|
2011-06-16 03:02:42 +04:00
|
|
|
CGM.getObjCRuntime().
|
|
|
|
GenerateMessageSend(*this, Return, ResultType, S,
|
|
|
|
Receiver, CallArgList(), ReceiverClass));
|
2008-08-27 10:57:25 +04:00
|
|
|
}
|
|
|
|
|
2010-12-04 05:32:38 +03:00
|
|
|
void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
|
|
|
|
LValue Dst) {
|
|
|
|
const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr();
|
2010-12-02 04:19:52 +03:00
|
|
|
Selector S = E->getSetterSelector();
|
2011-03-30 20:11:20 +04:00
|
|
|
QualType ArgType = E->getSetterArgType();
|
|
|
|
|
2011-02-09 01:33:23 +03:00
|
|
|
// FIXME. Other than scalars, AST is not adequate for setter and
|
|
|
|
// getter type mismatches which require conversion.
|
|
|
|
if (Src.isScalar()) {
|
|
|
|
llvm::Value *SrcVal = Src.getScalarVal();
|
|
|
|
QualType DstType = getContext().getCanonicalType(ArgType);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *DstTy = ConvertType(DstType);
|
2011-02-09 01:33:23 +03:00
|
|
|
if (SrcVal->getType() != DstTy)
|
|
|
|
Src =
|
|
|
|
RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType));
|
|
|
|
}
|
|
|
|
|
2010-12-04 06:11:00 +03:00
|
|
|
CallArgList Args;
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(Src, ArgType);
|
2010-12-04 06:11:00 +03:00
|
|
|
|
|
|
|
llvm::Value *Receiver = Dst.getPropertyRefBaseAddr();
|
|
|
|
QualType ResultType = getContext().VoidTy;
|
|
|
|
|
2010-12-02 04:19:52 +03:00
|
|
|
if (E->isSuperReceiver()) {
|
2010-12-04 06:11:00 +03:00
|
|
|
GenerateMessageSendSuper(*this, ReturnValueSlot(),
|
|
|
|
ResultType, S, Receiver, Args);
|
2010-12-02 04:19:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-04 05:32:38 +03:00
|
|
|
const ObjCInterfaceDecl *ReceiverClass
|
|
|
|
= (E->isClassReceiver() ? E->getClassReceiver() : 0);
|
2010-12-02 04:19:52 +03:00
|
|
|
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
|
2010-12-04 06:11:00 +03:00
|
|
|
ResultType, S, Receiver, Args,
|
|
|
|
ReceiverClass);
|
2008-08-29 12:11:39 +04:00
|
|
|
}
|
|
|
|
|
2009-03-23 00:03:39 +03:00
|
|
|
void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Constant *EnumerationMutationFn =
|
2008-09-24 08:04:31 +04:00
|
|
|
CGM.getObjCRuntime().EnumerationMutationFunction();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-09-24 08:04:31 +04:00
|
|
|
if (!EnumerationMutationFn) {
|
|
|
|
CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-19 04:36:36 +03:00
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
|
|
|
if (DI) {
|
|
|
|
DI->setLocation(S.getSourceRange().getBegin());
|
|
|
|
DI->EmitRegionStart(Builder);
|
|
|
|
}
|
|
|
|
|
2011-06-14 03:15:32 +04:00
|
|
|
// The local variable comes into scope immediately.
|
|
|
|
AutoVarEmission variable = AutoVarEmission::invalid();
|
|
|
|
if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement()))
|
|
|
|
variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl()));
|
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-31 06:33:12 +04:00
|
|
|
// Fast enumeration state.
|
|
|
|
QualType StateTy = getContext().getObjCFastEnumerationStateType();
|
2010-02-09 05:48:28 +03:00
|
|
|
llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr");
|
2010-05-22 21:35:42 +04:00
|
|
|
EmitNullInitialization(StatePtr, StateTy);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-31 06:33:12 +04:00
|
|
|
// Number of elements in the items array.
|
2008-08-31 08:05:03 +04:00
|
|
|
static const unsigned NumItems = 16;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Fetch the countByEnumeratingWithState:objects:count: selector.
|
2010-03-30 15:36:44 +04:00
|
|
|
IdentifierInfo *II[] = {
|
|
|
|
&CGM.getContext().Idents.get("countByEnumeratingWithState"),
|
|
|
|
&CGM.getContext().Idents.get("objects"),
|
|
|
|
&CGM.getContext().Idents.get("count")
|
|
|
|
};
|
|
|
|
Selector FastEnumSel =
|
|
|
|
CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
|
|
|
QualType ItemsTy =
|
|
|
|
getContext().getConstantArrayType(getContext().getObjCIdType(),
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::APInt(32, NumItems),
|
2008-08-31 06:33:12 +04:00
|
|
|
ArrayType::Normal, 0);
|
2010-02-09 05:48:28 +03:00
|
|
|
llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-07-27 05:07:15 +04:00
|
|
|
// Emit the collection pointer. In ARC, we do a retain.
|
|
|
|
llvm::Value *Collection;
|
|
|
|
if (getLangOptions().ObjCAutoRefCount) {
|
|
|
|
Collection = EmitARCRetainScalarExpr(S.getCollection());
|
|
|
|
|
|
|
|
// Enter a cleanup to do the release.
|
|
|
|
EmitObjCConsumeObject(S.getCollection()->getType(), Collection);
|
|
|
|
} else {
|
|
|
|
Collection = EmitScalarExpr(S.getCollection());
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-08-05 04:14:38 +04:00
|
|
|
// The 'continue' label needs to appear within the cleanup for the
|
|
|
|
// collection object.
|
|
|
|
JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next");
|
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Send it our message:
|
2008-08-31 06:33:12 +04:00
|
|
|
CallArgList Args;
|
2011-01-07 04:49:06 +03:00
|
|
|
|
|
|
|
// The first argument is a temporary of the enumeration-state type.
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// The second argument is a temporary array with space for NumItems
|
|
|
|
// pointers. We'll actually be loading elements from the array
|
|
|
|
// pointer written into the control state; this buffer is so that
|
|
|
|
// collections that *aren't* backed by arrays can still queue up
|
|
|
|
// batches of elements.
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// The third argument is the capacity of that temporary array.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
|
2009-07-25 03:12:58 +04:00
|
|
|
llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
|
2011-05-02 21:57:46 +04:00
|
|
|
Args.add(RValue::get(Count), getContext().UnsignedLongTy);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Start the enumeration.
|
2009-09-09 19:08:12 +04:00
|
|
|
RValue CountRV =
|
2010-05-22 05:48:05 +04:00
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
|
2008-08-31 06:33:12 +04:00
|
|
|
getContext().UnsignedLongTy,
|
|
|
|
FastEnumSel,
|
2010-04-28 23:33:36 +04:00
|
|
|
Collection, Args);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// The initial number of objects that were returned in the buffer.
|
|
|
|
llvm::Value *initialBufferLimit = CountRV.getScalarVal();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty");
|
|
|
|
llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit");
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// If the limit pointer was zero to begin with, the collection is
|
|
|
|
// empty; skip all this.
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"),
|
|
|
|
EmptyBB, LoopInitBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Otherwise, initialize the loop.
|
|
|
|
EmitBlock(LoopInitBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Save the initial mutations value. This is the value at an
|
|
|
|
// address that was written into the state object by
|
|
|
|
// countByEnumeratingWithState:objects:count:.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *StateMutationsPtrPtr =
|
2008-08-31 08:05:03 +04:00
|
|
|
Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
|
2008-08-31 08:05:03 +04:00
|
|
|
"mutationsptr");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::Value *initialMutations =
|
|
|
|
Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Start looping. This is the point we return to whenever we have a
|
|
|
|
// fresh, non-empty batch of objects.
|
|
|
|
llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody");
|
|
|
|
EmitBlock(LoopBodyBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// The current index into the buffer.
|
2011-03-30 15:28:58 +04:00
|
|
|
llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index");
|
2011-01-07 04:49:06 +03:00
|
|
|
index->addIncoming(zero, LoopInitBB);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// The current buffer size.
|
2011-03-30 15:28:58 +04:00
|
|
|
llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count");
|
2011-01-07 04:49:06 +03:00
|
|
|
count->addIncoming(initialBufferLimit, LoopInitBB);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Check whether the mutations value has changed from where it was
|
|
|
|
// at start. StateMutationsPtr should actually be invariant between
|
|
|
|
// refreshes.
|
2008-08-31 08:05:03 +04:00
|
|
|
StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::Value *currentMutations
|
|
|
|
= Builder.CreateLoad(StateMutationsPtr, "statemutations");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated");
|
2011-03-03 01:39:34 +03:00
|
|
|
llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations),
|
|
|
|
WasNotMutatedBB, WasMutatedBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// If so, call the enumeration-mutation function.
|
|
|
|
EmitBlock(WasMutatedBB);
|
2008-08-31 08:05:03 +04:00
|
|
|
llvm::Value *V =
|
2009-09-09 19:08:12 +04:00
|
|
|
Builder.CreateBitCast(Collection,
|
2008-08-31 08:05:03 +04:00
|
|
|
ConvertType(getContext().getObjCIdType()),
|
|
|
|
"tmp");
|
2009-02-04 02:55:40 +03:00
|
|
|
CallArgList Args2;
|
2011-05-02 21:57:46 +04:00
|
|
|
Args2.add(RValue::get(V), getContext().getObjCIdType());
|
2009-05-16 11:57:57 +04:00
|
|
|
// FIXME: We shouldn't need to get the function info here, the runtime already
|
|
|
|
// should have computed it to build the function.
|
2010-02-06 00:31:56 +03:00
|
|
|
EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2,
|
2010-03-31 00:24:48 +04:00
|
|
|
FunctionType::ExtInfo()),
|
2009-12-24 22:25:24 +03:00
|
|
|
EnumerationMutationFn, ReturnValueSlot(), Args2);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Otherwise, or if the mutation function returns, just continue.
|
|
|
|
EmitBlock(WasNotMutatedBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Initialize the element variable.
|
|
|
|
RunCleanupsScope elementVariableScope(*this);
|
2011-02-22 10:16:58 +03:00
|
|
|
bool elementIsVariable;
|
2011-01-07 04:49:06 +03:00
|
|
|
LValue elementLValue;
|
|
|
|
QualType elementType;
|
|
|
|
if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
|
2011-02-22 10:16:58 +03:00
|
|
|
// Initialize the variable, in case it's a __block variable or something.
|
|
|
|
EmitAutoVarInit(variable);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-02-22 10:16:58 +03:00
|
|
|
const VarDecl* D = cast<VarDecl>(SD->getSingleDecl());
|
2011-01-07 04:49:06 +03:00
|
|
|
DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(),
|
|
|
|
VK_LValue, SourceLocation());
|
|
|
|
elementLValue = EmitLValue(&tempDRE);
|
|
|
|
elementType = D->getType();
|
2011-02-22 10:16:58 +03:00
|
|
|
elementIsVariable = true;
|
2011-06-17 10:42:21 +04:00
|
|
|
|
|
|
|
if (D->isARCPseudoStrong())
|
|
|
|
elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone);
|
2011-01-07 04:49:06 +03:00
|
|
|
} else {
|
|
|
|
elementLValue = LValue(); // suppress warning
|
|
|
|
elementType = cast<Expr>(S.getElement())->getType();
|
2011-02-22 10:16:58 +03:00
|
|
|
elementIsVariable = false;
|
2011-01-07 04:49:06 +03:00
|
|
|
}
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *convertedElementType = ConvertType(elementType);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Fetch the buffer out of the enumeration state.
|
|
|
|
// TODO: this pointer should actually be invariant between
|
|
|
|
// refreshes, which would help us do certain loop optimizations.
|
|
|
|
llvm::Value *StateItemsPtr =
|
|
|
|
Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
|
|
|
|
llvm::Value *EnumStateItems =
|
|
|
|
Builder.CreateLoad(StateItemsPtr, "stateitems");
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Fetch the value at the current index from the buffer.
|
2009-09-09 19:08:12 +04:00
|
|
|
llvm::Value *CurrentItemPtr =
|
2011-01-07 04:49:06 +03:00
|
|
|
Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr");
|
|
|
|
llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Cast that value to the right type.
|
|
|
|
CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType,
|
|
|
|
"currentitem");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Make sure we have an l-value. Yes, this gets evaluated every
|
|
|
|
// time through the loop.
|
2011-06-17 10:42:21 +04:00
|
|
|
if (!elementIsVariable) {
|
2011-01-07 04:49:06 +03:00
|
|
|
elementLValue = EmitLValue(cast<Expr>(S.getElement()));
|
2011-06-25 06:11:03 +04:00
|
|
|
EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue);
|
2011-06-17 10:42:21 +04:00
|
|
|
} else {
|
|
|
|
EmitScalarInit(CurrentItem, elementLValue);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-02-22 10:16:58 +03:00
|
|
|
// If we do have an element variable, this assignment is the end of
|
|
|
|
// its initialization.
|
|
|
|
if (elementIsVariable)
|
|
|
|
EmitAutoVarCleanups(variable);
|
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Perform the loop body, setting up break and continue labels.
|
2009-02-10 08:52:02 +03:00
|
|
|
BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
|
2011-01-07 04:49:06 +03:00
|
|
|
{
|
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
EmitStmt(S.getBody());
|
|
|
|
}
|
2008-08-31 06:33:12 +04:00
|
|
|
BreakContinueStack.pop_back();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Destroy the element variable now.
|
|
|
|
elementVariableScope.ForceCleanup();
|
|
|
|
|
|
|
|
// Check whether there are more elements.
|
2010-07-24 01:56:41 +04:00
|
|
|
EmitBlock(AfterBody.getBlock());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch");
|
|
|
|
|
|
|
|
// First we check in the local buffer.
|
|
|
|
llvm::Value *indexPlusOne
|
|
|
|
= Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1));
|
|
|
|
|
|
|
|
// If we haven't overrun the buffer yet, we can continue.
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count),
|
|
|
|
LoopBodyBB, FetchMoreBB);
|
2009-01-06 21:56:31 +03:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
index->addIncoming(indexPlusOne, AfterBody.getBlock());
|
|
|
|
count->addIncoming(count, AfterBody.getBlock());
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// Otherwise, we have to fetch more elements.
|
|
|
|
EmitBlock(FetchMoreBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
CountRV =
|
2010-05-22 05:48:05 +04:00
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
|
2008-08-31 06:33:12 +04:00
|
|
|
getContext().UnsignedLongTy,
|
2009-09-09 19:08:12 +04:00
|
|
|
FastEnumSel,
|
2010-04-28 23:33:36 +04:00
|
|
|
Collection, Args);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
// If we got a zero count, we're done.
|
|
|
|
llvm::Value *refetchCount = CountRV.getScalarVal();
|
|
|
|
|
|
|
|
// (note that the message send might split FetchMoreBB)
|
|
|
|
index->addIncoming(zero, Builder.GetInsertBlock());
|
|
|
|
count->addIncoming(refetchCount, Builder.GetInsertBlock());
|
|
|
|
|
|
|
|
Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero),
|
|
|
|
EmptyBB, LoopBodyBB);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2008-08-31 06:33:12 +04:00
|
|
|
// No more elements.
|
2011-01-07 04:49:06 +03:00
|
|
|
EmitBlock(EmptyBB);
|
2008-08-31 06:33:12 +04:00
|
|
|
|
2011-02-22 10:16:58 +03:00
|
|
|
if (!elementIsVariable) {
|
2008-08-31 06:33:12 +04:00
|
|
|
// If the element was not a declaration, set it to be null.
|
|
|
|
|
2011-01-07 04:49:06 +03:00
|
|
|
llvm::Value *null = llvm::Constant::getNullValue(convertedElementType);
|
|
|
|
elementLValue = EmitLValue(cast<Expr>(S.getElement()));
|
2011-06-25 06:11:03 +04:00
|
|
|
EmitStoreThroughLValue(RValue::get(null), elementLValue);
|
2008-08-31 06:33:12 +04:00
|
|
|
}
|
|
|
|
|
2011-01-19 04:36:36 +03:00
|
|
|
if (DI) {
|
|
|
|
DI->setLocation(S.getSourceRange().getEnd());
|
|
|
|
DI->EmitRegionEnd(Builder);
|
|
|
|
}
|
|
|
|
|
2011-07-27 05:07:15 +04:00
|
|
|
// Leave the cleanup we entered in ARC.
|
|
|
|
if (getLangOptions().ObjCAutoRefCount)
|
|
|
|
PopCleanupBlock();
|
|
|
|
|
2010-07-24 01:56:41 +04:00
|
|
|
EmitBlock(LoopEnd.getBlock());
|
2008-08-30 23:51:14 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) {
|
2010-07-06 05:34:17 +04:00
|
|
|
CGM.getObjCRuntime().EmitTryStmt(*this, S);
|
2008-09-09 14:04:29 +04:00
|
|
|
}
|
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) {
|
2008-09-09 14:04:29 +04:00
|
|
|
CGM.getObjCRuntime().EmitThrowStmt(*this, S);
|
|
|
|
}
|
|
|
|
|
2008-11-16 00:26:17 +03:00
|
|
|
void CodeGenFunction::EmitObjCAtSynchronizedStmt(
|
2009-09-09 19:08:12 +04:00
|
|
|
const ObjCAtSynchronizedStmt &S) {
|
2010-07-06 05:34:17 +04:00
|
|
|
CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
|
2008-11-16 00:26:17 +03:00
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
/// Produce the code for a CK_ObjCProduceObject. Just does a
|
|
|
|
/// primitive retain.
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type,
|
|
|
|
llvm::Value *value) {
|
|
|
|
return EmitARCRetain(type, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct CallObjCRelease : EHScopeStack::Cleanup {
|
2011-08-04 02:24:24 +04:00
|
|
|
CallObjCRelease(llvm::Value *object) : object(object) {}
|
|
|
|
llvm::Value *object;
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2011-07-13 00:27:29 +04:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-06-16 03:02:42 +04:00
|
|
|
CGF.EmitARCRelease(object, /*precise*/ true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code for a CK_ObjCConsumeObject. Does a primitive
|
|
|
|
/// release at the end of the full-expression.
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type,
|
|
|
|
llvm::Value *object) {
|
|
|
|
// If we're in a conditional branch, we need to make the cleanup
|
2011-08-04 02:24:24 +04:00
|
|
|
// conditional.
|
|
|
|
pushFullExprCleanup<CallObjCRelease>(getARCCleanupKind(), object);
|
2011-06-16 03:02:42 +04:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type,
|
|
|
|
llvm::Value *value) {
|
|
|
|
return EmitARCRetainAutorelease(type, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM,
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *type,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef fnName) {
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName);
|
|
|
|
|
|
|
|
// In -fobjc-no-arc-runtime, emit weak references to the runtime
|
|
|
|
// support library.
|
2011-07-06 04:26:06 +04:00
|
|
|
if (!CGM.getCodeGenOpts().ObjCRuntimeHasARC)
|
2011-06-16 03:02:42 +04:00
|
|
|
if (llvm::Function *f = dyn_cast<llvm::Function>(fn))
|
|
|
|
f->setLinkage(llvm::Function::ExternalWeakLinkage);
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform an operation having the signature
|
|
|
|
/// i8* (i8*)
|
|
|
|
/// where a null input causes a no-op and returns null.
|
|
|
|
static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *value,
|
|
|
|
llvm::Constant *&fn,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef fnName) {
|
2011-06-16 03:02:42 +04:00
|
|
|
if (isa<llvm::ConstantPointerNull>(value)) return value;
|
|
|
|
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> args(1, CGF.Int8PtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(CGF.Int8PtrTy, args, false);
|
|
|
|
fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast the argument to 'id'.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *origType = value->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
llvm::CallInst *call = CGF.Builder.CreateCall(fn, value);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
|
|
|
|
// Cast the result back to the original type.
|
|
|
|
return CGF.Builder.CreateBitCast(call, origType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform an operation having the following signature:
|
|
|
|
/// i8* (i8**)
|
|
|
|
static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
llvm::Constant *&fn,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef fnName) {
|
2011-06-16 03:02:42 +04:00
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> args(1, CGF.Int8PtrPtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(CGF.Int8PtrTy, args, false);
|
|
|
|
fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast the argument to 'id*'.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *origType = addr->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
|
|
|
|
|
|
|
|
// Call the function.
|
|
|
|
llvm::CallInst *call = CGF.Builder.CreateCall(fn, addr);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
|
|
|
|
// Cast the result back to a dereference of the original type.
|
|
|
|
llvm::Value *result = call;
|
|
|
|
if (origType != CGF.Int8PtrPtrTy)
|
|
|
|
result = CGF.Builder.CreateBitCast(result,
|
|
|
|
cast<llvm::PointerType>(origType)->getElementType());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform an operation having the following signature:
|
|
|
|
/// i8* (i8**, i8*)
|
|
|
|
static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
llvm::Value *value,
|
|
|
|
llvm::Constant *&fn,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef fnName,
|
2011-06-16 03:02:42 +04:00
|
|
|
bool ignored) {
|
|
|
|
assert(cast<llvm::PointerType>(addr->getType())->getElementType()
|
|
|
|
== value->getType());
|
|
|
|
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> argTypes(2);
|
2011-06-16 03:02:42 +04:00
|
|
|
argTypes[0] = CGF.Int8PtrPtrTy;
|
|
|
|
argTypes[1] = CGF.Int8PtrTy;
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType
|
2011-06-16 03:02:42 +04:00
|
|
|
= llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false);
|
|
|
|
fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
|
|
|
|
}
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *origType = value->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy);
|
|
|
|
value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy);
|
|
|
|
|
|
|
|
llvm::CallInst *result = CGF.Builder.CreateCall2(fn, addr, value);
|
|
|
|
result->setDoesNotThrow();
|
|
|
|
|
|
|
|
if (ignored) return 0;
|
|
|
|
|
|
|
|
return CGF.Builder.CreateBitCast(result, origType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform an operation having the following signature:
|
|
|
|
/// void (i8**, i8**)
|
|
|
|
static void emitARCCopyOperation(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *dst,
|
|
|
|
llvm::Value *src,
|
|
|
|
llvm::Constant *&fn,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef fnName) {
|
2011-06-16 03:02:42 +04:00
|
|
|
assert(dst->getType() == src->getType());
|
|
|
|
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> argTypes(2, CGF.Int8PtrPtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType
|
2011-06-16 03:02:42 +04:00
|
|
|
= llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false);
|
|
|
|
fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName);
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy);
|
|
|
|
src = CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy);
|
|
|
|
|
|
|
|
llvm::CallInst *result = CGF.Builder.CreateCall2(fn, dst, src);
|
|
|
|
result->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code to do a retain. Based on the type, calls one of:
|
|
|
|
/// call i8* @objc_retain(i8* %value)
|
|
|
|
/// call i8* @objc_retainBlock(i8* %value)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) {
|
|
|
|
if (type->isBlockPointerType())
|
|
|
|
return EmitARCRetainBlock(value);
|
|
|
|
else
|
|
|
|
return EmitARCRetainNonBlock(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retain the given object, with normal retain semantics.
|
|
|
|
/// call i8* @objc_retain(i8* %value)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_retain,
|
|
|
|
"objc_retain");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retain the given block, with _Block_copy semantics.
|
|
|
|
/// call i8* @objc_retainBlock(i8* %value)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_retainBlock,
|
|
|
|
"objc_retainBlock");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Retain the given object which is the result of a function call.
|
|
|
|
/// call i8* @objc_retainAutoreleasedReturnValue(i8* %value)
|
|
|
|
///
|
|
|
|
/// Yes, this function name is one character away from a different
|
|
|
|
/// call with completely different semantics.
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) {
|
|
|
|
// Fetch the void(void) inline asm which marks that we're going to
|
|
|
|
// retain the autoreleased return value.
|
|
|
|
llvm::InlineAsm *&marker
|
|
|
|
= CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker;
|
|
|
|
if (!marker) {
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef assembly
|
2011-06-16 03:02:42 +04:00
|
|
|
= CGM.getTargetCodeGenInfo()
|
|
|
|
.getARCRetainAutoreleasedReturnValueMarker();
|
|
|
|
|
|
|
|
// If we have an empty assembly string, there's nothing to do.
|
|
|
|
if (assembly.empty()) {
|
|
|
|
|
|
|
|
// Otherwise, at -O0, build an inline asm that we're going to call
|
|
|
|
// in a moment.
|
|
|
|
} else if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
|
|
|
|
llvm::FunctionType *type =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()),
|
|
|
|
/*variadic*/ false);
|
|
|
|
|
|
|
|
marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true);
|
|
|
|
|
|
|
|
// If we're at -O1 and above, we don't want to litter the code
|
|
|
|
// with this marker yet, so leave a breadcrumb for the ARC
|
|
|
|
// optimizer to pick up.
|
|
|
|
} else {
|
|
|
|
llvm::NamedMDNode *metadata =
|
|
|
|
CGM.getModule().getOrInsertNamedMetadata(
|
|
|
|
"clang.arc.retainAutoreleasedReturnValueMarker");
|
|
|
|
assert(metadata->getNumOperands() <= 1);
|
|
|
|
if (metadata->getNumOperands() == 0) {
|
|
|
|
llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly);
|
2011-07-29 17:56:53 +04:00
|
|
|
metadata->addOperand(llvm::MDNode::get(getLLVMContext(), string));
|
2011-06-16 03:02:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the marker asm if we made one, which we do only at -O0.
|
|
|
|
if (marker) Builder.CreateCall(marker);
|
|
|
|
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue,
|
|
|
|
"objc_retainAutoreleasedReturnValue");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Release the given object.
|
|
|
|
/// call void @objc_release(i8* %value)
|
|
|
|
void CodeGenFunction::EmitARCRelease(llvm::Value *value, bool precise) {
|
|
|
|
if (isa<llvm::ConstantPointerNull>(value)) return;
|
|
|
|
|
|
|
|
llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release;
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> args(1, Int8PtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(Builder.getVoidTy(), args, false);
|
|
|
|
fn = createARCRuntimeFunction(CGM, fnType, "objc_release");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast the argument to 'id'.
|
|
|
|
value = Builder.CreateBitCast(value, Int8PtrTy);
|
|
|
|
|
|
|
|
// Call objc_release.
|
|
|
|
llvm::CallInst *call = Builder.CreateCall(fn, value);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
|
|
|
|
if (!precise) {
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<llvm::Value*,1> args;
|
2011-06-16 03:02:42 +04:00
|
|
|
call->setMetadata("clang.imprecise_release",
|
|
|
|
llvm::MDNode::get(Builder.getContext(), args));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store into a strong object. Always calls this:
|
|
|
|
/// call void @objc_storeStrong(i8** %addr, i8* %value)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr,
|
|
|
|
llvm::Value *value,
|
|
|
|
bool ignored) {
|
|
|
|
assert(cast<llvm::PointerType>(addr->getType())->getElementType()
|
|
|
|
== value->getType());
|
|
|
|
|
|
|
|
llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong;
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy };
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType
|
2011-06-16 03:02:42 +04:00
|
|
|
= llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false);
|
|
|
|
fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong");
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
|
|
|
|
llvm::Value *castValue = Builder.CreateBitCast(value, Int8PtrTy);
|
|
|
|
|
|
|
|
Builder.CreateCall2(fn, addr, castValue)->setDoesNotThrow();
|
|
|
|
|
|
|
|
if (ignored) return 0;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store into a strong object. Sometimes calls this:
|
|
|
|
/// call void @objc_storeStrong(i8** %addr, i8* %value)
|
|
|
|
/// Other times, breaks it down into components.
|
2011-06-25 06:11:03 +04:00
|
|
|
llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst,
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::Value *newValue,
|
|
|
|
bool ignored) {
|
2011-06-25 06:11:03 +04:00
|
|
|
QualType type = dst.getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
bool isBlock = type->isBlockPointerType();
|
|
|
|
|
|
|
|
// Use a store barrier at -O0 unless this is a block type or the
|
|
|
|
// lvalue is inadequately aligned.
|
|
|
|
if (shouldUseFusedARCCalls() &&
|
|
|
|
!isBlock &&
|
|
|
|
!(dst.getAlignment() && dst.getAlignment() < PointerAlignInBytes)) {
|
|
|
|
return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, split it out.
|
|
|
|
|
|
|
|
// Retain the new value.
|
|
|
|
newValue = EmitARCRetain(type, newValue);
|
|
|
|
|
|
|
|
// Read the old value.
|
2011-06-25 06:11:03 +04:00
|
|
|
llvm::Value *oldValue = EmitLoadOfScalar(dst);
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
// Store. We do this before the release so that any deallocs won't
|
|
|
|
// see the old value.
|
2011-06-25 06:11:03 +04:00
|
|
|
EmitStoreOfScalar(newValue, dst);
|
2011-06-16 03:02:42 +04:00
|
|
|
|
|
|
|
// Finally, release the old value.
|
|
|
|
EmitARCRelease(oldValue, /*precise*/ false);
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Autorelease the given object.
|
|
|
|
/// call i8* @objc_autorelease(i8* %value)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_autorelease,
|
|
|
|
"objc_autorelease");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Autorelease the given object.
|
|
|
|
/// call i8* @objc_autoreleaseReturnValue(i8* %value)
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_autoreleaseReturnValue,
|
|
|
|
"objc_autoreleaseReturnValue");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do a fused retain/autorelease of the given object.
|
|
|
|
/// call i8* @objc_retainAutoreleaseReturnValue(i8* %value)
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue,
|
|
|
|
"objc_retainAutoreleaseReturnValue");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do a fused retain/autorelease of the given object.
|
|
|
|
/// call i8* @objc_retainAutorelease(i8* %value)
|
|
|
|
/// or
|
|
|
|
/// %retain = call i8* @objc_retainBlock(i8* %value)
|
|
|
|
/// call i8* @objc_autorelease(i8* %retain)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type,
|
|
|
|
llvm::Value *value) {
|
|
|
|
if (!type->isBlockPointerType())
|
|
|
|
return EmitARCRetainAutoreleaseNonBlock(value);
|
|
|
|
|
|
|
|
if (isa<llvm::ConstantPointerNull>(value)) return value;
|
|
|
|
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *origType = value->getType();
|
2011-06-16 03:02:42 +04:00
|
|
|
value = Builder.CreateBitCast(value, Int8PtrTy);
|
|
|
|
value = EmitARCRetainBlock(value);
|
|
|
|
value = EmitARCAutorelease(value);
|
|
|
|
return Builder.CreateBitCast(value, origType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Do a fused retain/autorelease of the given object.
|
|
|
|
/// call i8* @objc_retainAutorelease(i8* %value)
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) {
|
|
|
|
return emitARCValueOperation(*this, value,
|
|
|
|
CGM.getARCEntrypoints().objc_retainAutorelease,
|
|
|
|
"objc_retainAutorelease");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// i8* @objc_loadWeak(i8** %addr)
|
|
|
|
/// Essentially objc_autorelease(objc_loadWeakRetained(addr)).
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) {
|
|
|
|
return emitARCLoadOperation(*this, addr,
|
|
|
|
CGM.getARCEntrypoints().objc_loadWeak,
|
|
|
|
"objc_loadWeak");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// i8* @objc_loadWeakRetained(i8** %addr)
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) {
|
|
|
|
return emitARCLoadOperation(*this, addr,
|
|
|
|
CGM.getARCEntrypoints().objc_loadWeakRetained,
|
|
|
|
"objc_loadWeakRetained");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// i8* @objc_storeWeak(i8** %addr, i8* %value)
|
|
|
|
/// Returns %value.
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr,
|
|
|
|
llvm::Value *value,
|
|
|
|
bool ignored) {
|
|
|
|
return emitARCStoreOperation(*this, addr, value,
|
|
|
|
CGM.getARCEntrypoints().objc_storeWeak,
|
|
|
|
"objc_storeWeak", ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// i8* @objc_initWeak(i8** %addr, i8* %value)
|
|
|
|
/// Returns %value. %addr is known to not have a current weak entry.
|
|
|
|
/// Essentially equivalent to:
|
|
|
|
/// *addr = nil; objc_storeWeak(addr, value);
|
|
|
|
void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) {
|
|
|
|
// If we're initializing to null, just write null to memory; no need
|
|
|
|
// to get the runtime involved. But don't do this if optimization
|
|
|
|
// is enabled, because accounting for this would make the optimizer
|
|
|
|
// much more complicated.
|
|
|
|
if (isa<llvm::ConstantPointerNull>(value) &&
|
|
|
|
CGM.getCodeGenOpts().OptimizationLevel == 0) {
|
|
|
|
Builder.CreateStore(value, addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emitARCStoreOperation(*this, addr, value,
|
|
|
|
CGM.getARCEntrypoints().objc_initWeak,
|
|
|
|
"objc_initWeak", /*ignored*/ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// void @objc_destroyWeak(i8** %addr)
|
|
|
|
/// Essentially objc_storeWeak(addr, nil).
|
|
|
|
void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) {
|
|
|
|
llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak;
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> args(1, Int8PtrPtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(Builder.getVoidTy(), args, false);
|
|
|
|
fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast the argument to 'id*'.
|
|
|
|
addr = Builder.CreateBitCast(addr, Int8PtrPtrTy);
|
|
|
|
|
|
|
|
llvm::CallInst *call = Builder.CreateCall(fn, addr);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// void @objc_moveWeak(i8** %dest, i8** %src)
|
|
|
|
/// Disregards the current value in %dest. Leaves %src pointing to nothing.
|
|
|
|
/// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)).
|
|
|
|
void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) {
|
|
|
|
emitARCCopyOperation(*this, dst, src,
|
|
|
|
CGM.getARCEntrypoints().objc_moveWeak,
|
|
|
|
"objc_moveWeak");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// void @objc_copyWeak(i8** %dest, i8** %src)
|
|
|
|
/// Disregards the current value in %dest. Essentially
|
|
|
|
/// objc_release(objc_initWeak(dest, objc_readWeakRetained(src)))
|
|
|
|
void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) {
|
|
|
|
emitARCCopyOperation(*this, dst, src,
|
|
|
|
CGM.getARCEntrypoints().objc_copyWeak,
|
|
|
|
"objc_copyWeak");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code to do a objc_autoreleasepool_push.
|
|
|
|
/// call i8* @objc_autoreleasePoolPush(void)
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() {
|
|
|
|
llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush;
|
|
|
|
if (!fn) {
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(Int8PtrTy, false);
|
|
|
|
fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CallInst *call = Builder.CreateCall(fn);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
|
|
|
|
return call;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code to do a primitive release.
|
|
|
|
/// call void @objc_autoreleasePoolPop(i8* %ptr)
|
|
|
|
void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) {
|
|
|
|
assert(value->getType() == Int8PtrTy);
|
|
|
|
|
|
|
|
llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop;
|
|
|
|
if (!fn) {
|
2011-07-09 21:41:47 +04:00
|
|
|
std::vector<llvm::Type*> args(1, Int8PtrTy);
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::FunctionType *fnType =
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::FunctionType::get(Builder.getVoidTy(), args, false);
|
|
|
|
|
|
|
|
// We don't want to use a weak import here; instead we should not
|
|
|
|
// fall into this path.
|
|
|
|
fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::CallInst *call = Builder.CreateCall(fn, value);
|
|
|
|
call->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code to do an MRR version objc_autoreleasepool_push.
|
|
|
|
/// Which is: [[NSAutoreleasePool alloc] init];
|
|
|
|
/// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class.
|
|
|
|
/// init is declared as: - (id) init; in its NSObject super class.
|
|
|
|
///
|
|
|
|
llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() {
|
|
|
|
CGObjCRuntime &Runtime = CGM.getObjCRuntime();
|
|
|
|
llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(Builder);
|
|
|
|
// [NSAutoreleasePool alloc]
|
|
|
|
IdentifierInfo *II = &CGM.getContext().Idents.get("alloc");
|
|
|
|
Selector AllocSel = getContext().Selectors.getSelector(0, &II);
|
|
|
|
CallArgList Args;
|
|
|
|
RValue AllocRV =
|
|
|
|
Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
|
|
|
|
getContext().getObjCIdType(),
|
|
|
|
AllocSel, Receiver, Args);
|
|
|
|
|
|
|
|
// [Receiver init]
|
|
|
|
Receiver = AllocRV.getScalarVal();
|
|
|
|
II = &CGM.getContext().Idents.get("init");
|
|
|
|
Selector InitSel = getContext().Selectors.getSelector(0, &II);
|
|
|
|
RValue InitRV =
|
|
|
|
Runtime.GenerateMessageSend(*this, ReturnValueSlot(),
|
|
|
|
getContext().getObjCIdType(),
|
|
|
|
InitSel, Receiver, Args);
|
|
|
|
return InitRV.getScalarVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce the code to do a primitive release.
|
|
|
|
/// [tmp drain];
|
|
|
|
void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) {
|
|
|
|
IdentifierInfo *II = &CGM.getContext().Idents.get("drain");
|
|
|
|
Selector DrainSel = getContext().Selectors.getSelector(0, &II);
|
|
|
|
CallArgList Args;
|
|
|
|
CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(),
|
|
|
|
getContext().VoidTy, DrainSel, Arg, Args);
|
|
|
|
}
|
|
|
|
|
2011-07-09 05:37:26 +04:00
|
|
|
void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
QualType type) {
|
|
|
|
llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy");
|
|
|
|
CGF.EmitARCRelease(ptr, /*precise*/ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
QualType type) {
|
|
|
|
llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy");
|
|
|
|
CGF.EmitARCRelease(ptr, /*precise*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *addr,
|
|
|
|
QualType type) {
|
|
|
|
CGF.EmitARCDestroyWeak(addr);
|
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
namespace {
|
|
|
|
struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *Token;
|
|
|
|
|
|
|
|
CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
|
|
|
|
|
2011-07-13 00:27:29 +04:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-06-16 03:02:42 +04:00
|
|
|
CGF.EmitObjCAutoreleasePoolPop(Token);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *Token;
|
|
|
|
|
|
|
|
CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {}
|
|
|
|
|
2011-07-13 00:27:29 +04:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) {
|
2011-06-16 03:02:42 +04:00
|
|
|
CGF.EmitObjCMRRAutoreleasePoolPop(Token);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) {
|
|
|
|
if (CGM.getLangOptions().ObjCAutoRefCount)
|
|
|
|
EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr);
|
|
|
|
else
|
|
|
|
EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
|
|
|
|
LValue lvalue,
|
|
|
|
QualType type) {
|
|
|
|
switch (type.getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
2011-06-25 06:11:03 +04:00
|
|
|
return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(),
|
2011-06-16 03:02:42 +04:00
|
|
|
false);
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("impossible lifetime!");
|
|
|
|
return TryEmitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF,
|
|
|
|
const Expr *e) {
|
|
|
|
e = e->IgnoreParens();
|
|
|
|
QualType type = e->getType();
|
|
|
|
|
|
|
|
// As a very special optimization, in ARC++, if the l-value is the
|
|
|
|
// result of a non-volatile assignment, do a simple retain of the
|
|
|
|
// result of the call to objc_storeWeak instead of reloading.
|
|
|
|
if (CGF.getLangOptions().CPlusPlus &&
|
|
|
|
!type.isVolatileQualified() &&
|
|
|
|
type.getObjCLifetime() == Qualifiers::OCL_Weak &&
|
|
|
|
isa<BinaryOperator>(e) &&
|
|
|
|
cast<BinaryOperator>(e)->getOpcode() == BO_Assign)
|
|
|
|
return TryEmitResult(CGF.EmitScalarExpr(e), false);
|
|
|
|
|
|
|
|
return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *value);
|
|
|
|
|
|
|
|
/// Given that the given expression is some sort of call (which does
|
|
|
|
/// not return retained), emit a retain following it.
|
|
|
|
static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) {
|
|
|
|
llvm::Value *value = CGF.EmitScalarExpr(e);
|
|
|
|
return emitARCRetainAfterCall(CGF, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *value) {
|
|
|
|
if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) {
|
|
|
|
CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
|
|
|
|
|
|
|
|
// Place the retain immediately following the call.
|
|
|
|
CGF.Builder.SetInsertPoint(call->getParent(),
|
|
|
|
++llvm::BasicBlock::iterator(call));
|
|
|
|
value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
|
|
|
|
|
|
|
|
CGF.Builder.restoreIP(ip);
|
|
|
|
return value;
|
|
|
|
} else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) {
|
|
|
|
CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP();
|
|
|
|
|
|
|
|
// Place the retain at the beginning of the normal destination block.
|
|
|
|
llvm::BasicBlock *BB = invoke->getNormalDest();
|
|
|
|
CGF.Builder.SetInsertPoint(BB, BB->begin());
|
|
|
|
value = CGF.EmitARCRetainAutoreleasedReturnValue(value);
|
|
|
|
|
|
|
|
CGF.Builder.restoreIP(ip);
|
|
|
|
return value;
|
|
|
|
|
|
|
|
// Bitcasts can arise because of related-result returns. Rewrite
|
|
|
|
// the operand.
|
|
|
|
} else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) {
|
|
|
|
llvm::Value *operand = bitcast->getOperand(0);
|
|
|
|
operand = emitARCRetainAfterCall(CGF, operand);
|
|
|
|
bitcast->setOperand(0, operand);
|
|
|
|
return bitcast;
|
|
|
|
|
|
|
|
// Generic fall-back case.
|
|
|
|
} else {
|
|
|
|
// Retain using the non-block variant: we never need to do a copy
|
|
|
|
// of a block that's been returned to us.
|
|
|
|
return CGF.EmitARCRetainNonBlock(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static TryEmitResult
|
|
|
|
tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) {
|
2011-07-27 05:07:15 +04:00
|
|
|
// Look through cleanups.
|
|
|
|
if (const ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(e)) {
|
|
|
|
CodeGenFunction::RunCleanupsScope scope(CGF);
|
|
|
|
return tryEmitARCRetainScalarExpr(CGF, cleanups->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
// The desired result type, if it differs from the type of the
|
|
|
|
// ultimate opaque expression.
|
2011-07-18 08:24:23 +04:00
|
|
|
llvm::Type *resultType = 0;
|
2011-06-16 03:02:42 +04:00
|
|
|
|
2011-06-22 20:32:26 +04:00
|
|
|
// If we're loading retained from a __strong xvalue, we can avoid
|
|
|
|
// an extra retain/release pair by zeroing out the source of this
|
|
|
|
// "move" operation.
|
2011-06-25 06:26:44 +04:00
|
|
|
if (e->isXValue() && !e->getType().isConstQualified() &&
|
2011-06-22 20:32:26 +04:00
|
|
|
e->getType().getObjCLifetime() == Qualifiers::OCL_Strong) {
|
|
|
|
// Emit the lvalue
|
|
|
|
LValue lv = CGF.EmitLValue(e);
|
|
|
|
|
|
|
|
// Load the object pointer and cast it to the appropriate type.
|
|
|
|
QualType exprType = e->getType();
|
2011-06-25 06:11:03 +04:00
|
|
|
llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal();
|
2011-06-22 20:32:26 +04:00
|
|
|
|
|
|
|
if (resultType)
|
|
|
|
result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
|
|
|
|
// Set the source pointer to NULL.
|
|
|
|
llvm::Value *null
|
|
|
|
= llvm::ConstantPointerNull::get(
|
|
|
|
cast<llvm::PointerType>(CGF.ConvertType(exprType)));
|
2011-06-25 06:11:03 +04:00
|
|
|
CGF.EmitStoreOfScalar(null, lv);
|
2011-06-22 20:32:26 +04:00
|
|
|
|
|
|
|
return TryEmitResult(result, true);
|
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
while (true) {
|
|
|
|
e = e->IgnoreParens();
|
|
|
|
|
|
|
|
// There's a break at the end of this if-chain; anything
|
|
|
|
// that wants to keep looping has to explicitly continue.
|
|
|
|
if (const CastExpr *ce = dyn_cast<CastExpr>(e)) {
|
|
|
|
switch (ce->getCastKind()) {
|
|
|
|
// No-op casts don't change the type, so we just ignore them.
|
|
|
|
case CK_NoOp:
|
|
|
|
e = ce->getSubExpr();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case CK_LValueToRValue: {
|
|
|
|
TryEmitResult loadResult
|
|
|
|
= tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr());
|
|
|
|
if (resultType) {
|
|
|
|
llvm::Value *value = loadResult.getPointer();
|
|
|
|
value = CGF.Builder.CreateBitCast(value, resultType);
|
|
|
|
loadResult.setPointer(value);
|
|
|
|
}
|
|
|
|
return loadResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
// These casts can change the type, so remember that and
|
|
|
|
// soldier on. We only need to remember the outermost such
|
|
|
|
// cast, though.
|
|
|
|
case CK_AnyPointerToObjCPointerCast:
|
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
|
|
|
case CK_BitCast:
|
|
|
|
if (!resultType)
|
|
|
|
resultType = CGF.ConvertType(ce->getType());
|
|
|
|
e = ce->getSubExpr();
|
|
|
|
assert(e->getType()->hasPointerRepresentation());
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// For consumptions, just emit the subexpression and thus elide
|
|
|
|
// the retain/release pair.
|
|
|
|
case CK_ObjCConsumeObject: {
|
|
|
|
llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr());
|
|
|
|
if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
return TryEmitResult(result, true);
|
|
|
|
}
|
|
|
|
|
2011-07-07 10:58:02 +04:00
|
|
|
// For reclaims, emit the subexpression as a retained call and
|
|
|
|
// skip the consumption.
|
|
|
|
case CK_ObjCReclaimReturnedObject: {
|
|
|
|
llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr());
|
|
|
|
if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
return TryEmitResult(result, true);
|
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
case CK_GetObjCProperty: {
|
|
|
|
llvm::Value *result = emitARCRetainCall(CGF, ce);
|
|
|
|
if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
return TryEmitResult(result, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip __extension__.
|
|
|
|
} else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
|
|
|
|
if (op->getOpcode() == UO_Extension) {
|
|
|
|
e = op->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For calls and message sends, use the retained-call logic.
|
|
|
|
// Delegate inits are a special case in that they're the only
|
|
|
|
// returns-retained expression that *isn't* surrounded by
|
|
|
|
// a consume.
|
|
|
|
} else if (isa<CallExpr>(e) ||
|
|
|
|
(isa<ObjCMessageExpr>(e) &&
|
|
|
|
!cast<ObjCMessageExpr>(e)->isDelegateInitCall())) {
|
|
|
|
llvm::Value *result = emitARCRetainCall(CGF, e);
|
|
|
|
if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
return TryEmitResult(result, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conservatively halt the search at any other expression kind.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We didn't find an obvious production, so emit what we've got and
|
|
|
|
// tell the caller that we didn't manage to retain.
|
|
|
|
llvm::Value *result = CGF.EmitScalarExpr(e);
|
|
|
|
if (resultType) result = CGF.Builder.CreateBitCast(result, resultType);
|
|
|
|
return TryEmitResult(result, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF,
|
|
|
|
LValue lvalue,
|
|
|
|
QualType type) {
|
|
|
|
TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type);
|
|
|
|
llvm::Value *value = result.getPointer();
|
|
|
|
if (!result.getInt())
|
|
|
|
value = CGF.EmitARCRetain(type, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EmitARCRetainScalarExpr - Semantically equivalent to
|
|
|
|
/// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a
|
|
|
|
/// best-effort attempt to peephole expressions that naturally produce
|
|
|
|
/// retained objects.
|
|
|
|
llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) {
|
|
|
|
TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
|
|
|
|
llvm::Value *value = result.getPointer();
|
|
|
|
if (!result.getInt())
|
|
|
|
value = EmitARCRetain(e->getType(), value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *
|
|
|
|
CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) {
|
|
|
|
TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e);
|
|
|
|
llvm::Value *value = result.getPointer();
|
|
|
|
if (result.getInt())
|
|
|
|
value = EmitARCAutorelease(value);
|
|
|
|
else
|
|
|
|
value = EmitARCRetainAutorelease(e->getType(), value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<LValue,llvm::Value*>
|
|
|
|
CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e,
|
|
|
|
bool ignored) {
|
|
|
|
// Evaluate the RHS first.
|
|
|
|
TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS());
|
|
|
|
llvm::Value *value = result.getPointer();
|
|
|
|
|
2011-07-28 11:23:35 +04:00
|
|
|
bool hasImmediateRetain = result.getInt();
|
|
|
|
|
|
|
|
// If we didn't emit a retained object, and the l-value is of block
|
|
|
|
// type, then we need to emit the block-retain immediately in case
|
|
|
|
// it invalidates the l-value.
|
|
|
|
if (!hasImmediateRetain && e->getType()->isBlockPointerType()) {
|
|
|
|
value = EmitARCRetainBlock(value);
|
|
|
|
hasImmediateRetain = true;
|
|
|
|
}
|
|
|
|
|
2011-06-16 03:02:42 +04:00
|
|
|
LValue lvalue = EmitLValue(e->getLHS());
|
|
|
|
|
|
|
|
// If the RHS was emitted retained, expand this.
|
2011-07-28 11:23:35 +04:00
|
|
|
if (hasImmediateRetain) {
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::Value *oldValue =
|
|
|
|
EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatileQualified(),
|
|
|
|
lvalue.getAlignment(), e->getType(),
|
|
|
|
lvalue.getTBAAInfo());
|
|
|
|
EmitStoreOfScalar(value, lvalue.getAddress(),
|
|
|
|
lvalue.isVolatileQualified(), lvalue.getAlignment(),
|
|
|
|
e->getType(), lvalue.getTBAAInfo());
|
|
|
|
EmitARCRelease(oldValue, /*precise*/ false);
|
|
|
|
} else {
|
2011-06-25 06:11:03 +04:00
|
|
|
value = EmitARCStoreStrong(lvalue, value, ignored);
|
2011-06-16 03:02:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::pair<LValue,llvm::Value*>(lvalue, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<LValue,llvm::Value*>
|
|
|
|
CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) {
|
|
|
|
llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS());
|
|
|
|
LValue lvalue = EmitLValue(e->getLHS());
|
|
|
|
|
|
|
|
EmitStoreOfScalar(value, lvalue.getAddress(),
|
|
|
|
lvalue.isVolatileQualified(), lvalue.getAlignment(),
|
|
|
|
e->getType(), lvalue.getTBAAInfo());
|
|
|
|
|
|
|
|
return std::pair<LValue,llvm::Value*>(lvalue, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitObjCAutoreleasePoolStmt(
|
|
|
|
const ObjCAutoreleasePoolStmt &ARPS) {
|
|
|
|
const Stmt *subStmt = ARPS.getSubStmt();
|
|
|
|
const CompoundStmt &S = cast<CompoundStmt>(*subStmt);
|
|
|
|
|
|
|
|
CGDebugInfo *DI = getDebugInfo();
|
|
|
|
if (DI) {
|
|
|
|
DI->setLocation(S.getLBracLoc());
|
|
|
|
DI->EmitRegionStart(Builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the current cleanup stack depth.
|
|
|
|
RunCleanupsScope Scope(*this);
|
2011-07-06 04:26:06 +04:00
|
|
|
if (CGM.getCodeGenOpts().ObjCRuntimeHasARC) {
|
2011-06-16 03:02:42 +04:00
|
|
|
llvm::Value *token = EmitObjCAutoreleasePoolPush();
|
|
|
|
EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token);
|
|
|
|
} else {
|
|
|
|
llvm::Value *token = EmitObjCMRRAutoreleasePoolPush();
|
|
|
|
EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CompoundStmt::const_body_iterator I = S.body_begin(),
|
|
|
|
E = S.body_end(); I != E; ++I)
|
|
|
|
EmitStmt(*I);
|
|
|
|
|
|
|
|
if (DI) {
|
|
|
|
DI->setLocation(S.getRBracLoc());
|
|
|
|
DI->EmitRegionEnd(Builder);
|
|
|
|
}
|
|
|
|
}
|
2011-06-25 03:21:27 +04:00
|
|
|
|
|
|
|
/// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
|
|
|
|
/// make sure it survives garbage collection until this point.
|
|
|
|
void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) {
|
|
|
|
// We just use an inline assembly.
|
|
|
|
llvm::FunctionType *extenderType
|
2011-07-29 17:56:53 +04:00
|
|
|
= llvm::FunctionType::get(VoidTy, VoidPtrTy, /*variadic*/ false);
|
2011-06-25 03:21:27 +04:00
|
|
|
llvm::Value *extender
|
|
|
|
= llvm::InlineAsm::get(extenderType,
|
|
|
|
/* assembly */ "",
|
|
|
|
/* constraints */ "r",
|
|
|
|
/* side effects */ true);
|
|
|
|
|
|
|
|
object = Builder.CreateBitCast(object, VoidPtrTy);
|
|
|
|
Builder.CreateCall(extender, object)->setDoesNotThrow();
|
|
|
|
}
|
|
|
|
|
2008-04-09 19:51:31 +04:00
|
|
|
CGObjCRuntime::~CGObjCRuntime() {}
|