#include "clang/Analysis/BodyFarm.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/NestedNameSpecifier.h"
#include "clang/Analysis/CodeInjector.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/OperatorKinds.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "body-farm"
using namespace clang;
static bool isDispatchBlock(QualType Ty) {
const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
if (!BPT)
return false;
const FunctionProtoType *FT =
BPT->getPointeeType()->getAs<FunctionProtoType>();
return FT && FT->getReturnType()->isVoidType() && FT->getNumParams() == 0;
}
namespace {
class ASTMaker {
public:
ASTMaker(ASTContext &C) : C(C) {}
BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
BinaryOperator::Opcode Op);
CompoundStmt *makeCompound(ArrayRef<Stmt*>);
DeclRefExpr *makeDeclRefExpr(const VarDecl *D,
bool RefersToEnclosingVariableOrCapture = false);
UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
ImplicitCastExpr *
makeLvalueToRvalue(const VarDecl *Decl,
bool RefersToEnclosingVariableOrCapture = false);
ImplicitCastExpr *makeImplicitCast(const Expr *Arg, QualType Ty,
CastKind CK = CK_LValueToRValue);
CastExpr *makeReferenceCast(const Expr *Arg, QualType Ty);
ObjCBoolLiteralExpr *makeObjCBool(bool Val);
ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
ReturnStmt *makeReturn(const Expr *RetVal);
IntegerLiteral *makeIntegerLiteral(uint64_t Value, QualType Ty);
MemberExpr *makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
bool IsArrow = false,
ExprValueKind ValueKind = VK_LValue);
ValueDecl *findMemberField(const RecordDecl *RD, StringRef Name);
private:
ASTContext &C;
};
}
BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
QualType Ty) {
return BinaryOperator::Create(
C, const_cast<Expr *>(LHS), const_cast<Expr *>(RHS), BO_Assign, Ty,
VK_PRValue, OK_Ordinary, SourceLocation(), FPOptionsOverride());
}
BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
BinaryOperator::Opcode Op) {
assert(BinaryOperator::isLogicalOp(Op) ||
BinaryOperator::isComparisonOp(Op));
return BinaryOperator::Create(
C, const_cast<Expr *>(LHS), const_cast<Expr *>(RHS), Op,
C.getLogicalOperationType(), VK_PRValue, OK_Ordinary, SourceLocation(),
FPOptionsOverride());
}
CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
return CompoundStmt::Create(C, Stmts, FPOptionsOverride(), SourceLocation(),
SourceLocation());
}
DeclRefExpr *ASTMaker::makeDeclRefExpr(
const VarDecl *D,
bool RefersToEnclosingVariableOrCapture) {
QualType Type = D->getType().getNonReferenceType();
DeclRefExpr *DR = DeclRefExpr::Create(
C, NestedNameSpecifierLoc(), SourceLocation(), const_cast<VarDecl *>(D),
RefersToEnclosingVariableOrCapture, SourceLocation(), Type, VK_LValue);
return DR;
}
UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
return UnaryOperator::Create(C, const_cast<Expr *>(Arg), UO_Deref, Ty,
VK_LValue, OK_Ordinary, SourceLocation(),
false, FPOptionsOverride());
}
ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
return makeImplicitCast(Arg, Ty, CK_LValueToRValue);
}
ImplicitCastExpr *
ASTMaker::makeLvalueToRvalue(const VarDecl *Arg,
bool RefersToEnclosingVariableOrCapture) {
QualType Type = Arg->getType().getNonReferenceType();
return makeLvalueToRvalue(makeDeclRefExpr(Arg,
RefersToEnclosingVariableOrCapture),
Type);
}
ImplicitCastExpr *ASTMaker::makeImplicitCast(const Expr *Arg, QualType Ty,
CastKind CK) {
return ImplicitCastExpr::Create(C, Ty,
CK,
const_cast<Expr *>(Arg),
nullptr,
VK_PRValue,
FPOptionsOverride());
}
CastExpr *ASTMaker::makeReferenceCast(const Expr *Arg, QualType Ty) {
assert(Ty->isReferenceType());
return CXXStaticCastExpr::Create(
C, Ty.getNonReferenceType(),
Ty->isLValueReferenceType() ? VK_LValue : VK_XValue, CK_NoOp,
const_cast<Expr *>(Arg), nullptr,
C.getTrivialTypeSourceInfo(Ty), FPOptionsOverride(),
SourceLocation(), SourceLocation(), SourceRange());
}
Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
if (Arg->getType() == Ty)
return const_cast<Expr*>(Arg);
return makeImplicitCast(Arg, Ty, CK_IntegralCast);
}
ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
return makeImplicitCast(Arg, C.BoolTy, CK_IntegralToBoolean);
}
ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
}
ObjCIvarRefExpr *ASTMaker::makeObjCIvarRef(const Expr *Base,
const ObjCIvarDecl *IVar) {
return new (C) ObjCIvarRefExpr(const_cast<ObjCIvarDecl*>(IVar),
IVar->getType(), SourceLocation(),
SourceLocation(), const_cast<Expr*>(Base),
true, false);
}
ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
return ReturnStmt::Create(C, SourceLocation(), const_cast<Expr *>(RetVal),
nullptr);
}
IntegerLiteral *ASTMaker::makeIntegerLiteral(uint64_t Value, QualType Ty) {
llvm::APInt APValue = llvm::APInt(C.getTypeSize(Ty), Value);
return IntegerLiteral::Create(C, APValue, Ty, SourceLocation());
}
MemberExpr *ASTMaker::makeMemberExpression(Expr *base, ValueDecl *MemberDecl,
bool IsArrow,
ExprValueKind ValueKind) {
DeclAccessPair FoundDecl = DeclAccessPair::make(MemberDecl, AS_public);
return MemberExpr::Create(
C, base, IsArrow, SourceLocation(), NestedNameSpecifierLoc(),
SourceLocation(), MemberDecl, FoundDecl,
DeclarationNameInfo(MemberDecl->getDeclName(), SourceLocation()),
nullptr, MemberDecl->getType(), ValueKind,
OK_Ordinary, NOUR_None);
}
ValueDecl *ASTMaker::findMemberField(const RecordDecl *RD, StringRef Name) {
CXXBasePaths Paths(
false,
false,
false);
const IdentifierInfo &II = C.Idents.get(Name);
DeclarationName DeclName = C.DeclarationNames.getIdentifier(&II);
DeclContextLookupResult Decls = RD->lookup(DeclName);
for (NamedDecl *FoundDecl : Decls)
if (!FoundDecl->getDeclContext()->isFunctionOrMethod())
return cast<ValueDecl>(FoundDecl);
return nullptr;
}
typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
static CallExpr *create_call_once_funcptr_call(ASTContext &C, ASTMaker M,
const ParmVarDecl *Callback,
ArrayRef<Expr *> CallArgs) {
QualType Ty = Callback->getType();
DeclRefExpr *Call = M.makeDeclRefExpr(Callback);
Expr *SubExpr;
if (Ty->isRValueReferenceType()) {
SubExpr = M.makeImplicitCast(
Call, Ty.getNonReferenceType(), CK_LValueToRValue);
} else if (Ty->isLValueReferenceType() &&
Call->getType()->isFunctionType()) {
Ty = C.getPointerType(Ty.getNonReferenceType());
SubExpr = M.makeImplicitCast(Call, Ty, CK_FunctionToPointerDecay);
} else if (Ty->isLValueReferenceType()
&& Call->getType()->isPointerType()
&& Call->getType()->getPointeeType()->isFunctionType()){
SubExpr = Call;
} else {
llvm_unreachable("Unexpected state");
}
return CallExpr::Create(C, SubExpr, CallArgs, C.VoidTy, VK_PRValue,
SourceLocation(), FPOptionsOverride());
}
static CallExpr *create_call_once_lambda_call(ASTContext &C, ASTMaker M,
const ParmVarDecl *Callback,
CXXRecordDecl *CallbackDecl,
ArrayRef<Expr *> CallArgs) {
assert(CallbackDecl != nullptr);
assert(CallbackDecl->isLambda());
FunctionDecl *callOperatorDecl = CallbackDecl->getLambdaCallOperator();
assert(callOperatorDecl != nullptr);
DeclRefExpr *callOperatorDeclRef =
DeclRefExpr::Create( C,
NestedNameSpecifierLoc(),
SourceLocation(),
const_cast<FunctionDecl *>(callOperatorDecl),
false,
SourceLocation(),
callOperatorDecl->getType(),
VK_LValue);
return CXXOperatorCallExpr::Create(
C, OO_Call, callOperatorDeclRef,
CallArgs,
C.VoidTy,
VK_PRValue,
SourceLocation(),
FPOptionsOverride());
}
static Stmt *create_std_move_forward(ASTContext &C, const FunctionDecl *D) {
LLVM_DEBUG(llvm::dbgs() << "Generating body for std::move / std::forward\n");
ASTMaker M(C);
QualType ReturnType = D->getType()->castAs<FunctionType>()->getReturnType();
Expr *Param = M.makeDeclRefExpr(D->getParamDecl(0));
Expr *Cast = M.makeReferenceCast(Param, ReturnType);
return M.makeReturn(Cast);
}
static Stmt *create_call_once(ASTContext &C, const FunctionDecl *D) {
LLVM_DEBUG(llvm::dbgs() << "Generating body for call_once\n");
if (D->param_size() < 2)
return nullptr;
ASTMaker M(C);
const ParmVarDecl *Flag = D->getParamDecl(0);
const ParmVarDecl *Callback = D->getParamDecl(1);
if (!Callback->getType()->isReferenceType()) {
llvm::dbgs() << "libcxx03 std::call_once implementation, skipping.\n";
return nullptr;
}
if (!Flag->getType()->isReferenceType()) {
llvm::dbgs() << "unknown std::call_once implementation, skipping.\n";
return nullptr;
}
QualType CallbackType = Callback->getType().getNonReferenceType();
CXXRecordDecl *CallbackRecordDecl = CallbackType->getAsCXXRecordDecl();
QualType FlagType = Flag->getType().getNonReferenceType();
auto *FlagRecordDecl = FlagType->getAsRecordDecl();
if (!FlagRecordDecl) {
LLVM_DEBUG(llvm::dbgs() << "Flag field is not a record: "
<< "unknown std::call_once implementation, "
<< "ignoring the call.\n");
return nullptr;
}
ValueDecl *FlagFieldDecl = M.findMemberField(FlagRecordDecl, "__state_");
if (!FlagFieldDecl) {
FlagFieldDecl = M.findMemberField(FlagRecordDecl, "_M_once");
}
if (!FlagFieldDecl) {
LLVM_DEBUG(llvm::dbgs() << "No field _M_once or __state_ found on "
<< "std::once_flag struct: unknown std::call_once "
<< "implementation, ignoring the call.");
return nullptr;
}
bool isLambdaCall = CallbackRecordDecl && CallbackRecordDecl->isLambda();
if (CallbackRecordDecl && !isLambdaCall) {
LLVM_DEBUG(llvm::dbgs()
<< "Not supported: synthesizing body for functors when "
<< "body farming std::call_once, ignoring the call.");
return nullptr;
}
SmallVector<Expr *, 5> CallArgs;
const FunctionProtoType *CallbackFunctionType;
if (isLambdaCall) {
CallArgs.push_back(
M.makeDeclRefExpr(Callback,
true));
CallbackFunctionType = CallbackRecordDecl->getLambdaCallOperator()
->getType()
->getAs<FunctionProtoType>();
} else if (!CallbackType->getPointeeType().isNull()) {
CallbackFunctionType =
CallbackType->getPointeeType()->getAs<FunctionProtoType>();
} else {
CallbackFunctionType = CallbackType->getAs<FunctionProtoType>();
}
if (!CallbackFunctionType)
return nullptr;
if (D->getNumParams() != CallbackFunctionType->getNumParams() + 2) {
LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
<< "params passed to std::call_once, "
<< "ignoring the call\n");
return nullptr;
}
for (unsigned int ParamIdx = 2; ParamIdx < D->getNumParams(); ParamIdx++) {
const ParmVarDecl *PDecl = D->getParamDecl(ParamIdx);
assert(PDecl);
if (CallbackFunctionType->getParamType(ParamIdx - 2)
.getNonReferenceType()
.getCanonicalType() !=
PDecl->getType().getNonReferenceType().getCanonicalType()) {
LLVM_DEBUG(llvm::dbgs() << "Types of params of the callback do not match "
<< "params passed to std::call_once, "
<< "ignoring the call\n");
return nullptr;
}
Expr *ParamExpr = M.makeDeclRefExpr(PDecl);
if (!CallbackFunctionType->getParamType(ParamIdx - 2)->isReferenceType()) {
QualType PTy = PDecl->getType().getNonReferenceType();
ParamExpr = M.makeLvalueToRvalue(ParamExpr, PTy);
}
CallArgs.push_back(ParamExpr);
}
CallExpr *CallbackCall;
if (isLambdaCall) {
CallbackCall = create_call_once_lambda_call(C, M, Callback,
CallbackRecordDecl, CallArgs);
} else {
CallbackCall = create_call_once_funcptr_call(C, M, Callback, CallArgs);
}
DeclRefExpr *FlagDecl =
M.makeDeclRefExpr(Flag,
true);
MemberExpr *Deref = M.makeMemberExpression(FlagDecl, FlagFieldDecl);
assert(Deref->isLValue());
QualType DerefType = Deref->getType();
UnaryOperator *FlagCheck = UnaryOperator::Create(
C,
M.makeImplicitCast(M.makeLvalueToRvalue(Deref, DerefType), DerefType,
CK_IntegralToBoolean),
UO_LNot,
C.IntTy,
VK_PRValue,
OK_Ordinary, SourceLocation(),
false, FPOptionsOverride());
BinaryOperator *FlagAssignment = M.makeAssignment(
Deref, M.makeIntegralCast(M.makeIntegerLiteral(1, C.IntTy), DerefType),
DerefType);
auto *Out =
IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
nullptr,
nullptr,
FlagCheck,
SourceLocation(),
SourceLocation(),
M.makeCompound({CallbackCall, FlagAssignment}));
return Out;
}
static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
if (D->param_size() != 2)
return nullptr;
const ParmVarDecl *Predicate = D->getParamDecl(0);
QualType PredicateQPtrTy = Predicate->getType();
const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
if (!PredicatePtrTy)
return nullptr;
QualType PredicateTy = PredicatePtrTy->getPointeeType();
if (!PredicateTy->isIntegerType())
return nullptr;
const ParmVarDecl *Block = D->getParamDecl(1);
QualType Ty = Block->getType();
if (!isDispatchBlock(Ty))
return nullptr;
ASTMaker M(C);
CallExpr *CE = CallExpr::Create(
C,
M.makeLvalueToRvalue(Block),
None,
C.VoidTy,
VK_PRValue,
SourceLocation(), FPOptionsOverride());
Expr *DoneValue =
UnaryOperator::Create(C, M.makeIntegerLiteral(0, C.LongTy), UO_Not,
C.LongTy, VK_PRValue, OK_Ordinary, SourceLocation(),
false, FPOptionsOverride());
BinaryOperator *B =
M.makeAssignment(
M.makeDereference(
M.makeLvalueToRvalue(
M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
PredicateTy),
M.makeIntegralCast(DoneValue, PredicateTy),
PredicateTy);
Stmt *Stmts[] = { B, CE };
CompoundStmt *CS = M.makeCompound(Stmts);
ImplicitCastExpr *LValToRval =
M.makeLvalueToRvalue(
M.makeDereference(
M.makeLvalueToRvalue(
M.makeDeclRefExpr(Predicate),
PredicateQPtrTy),
PredicateTy),
PredicateTy);
Expr *GuardCondition = M.makeComparison(LValToRval, DoneValue, BO_NE);
auto *If = IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
nullptr,
nullptr,
GuardCondition,
SourceLocation(),
SourceLocation(),
CS);
return If;
}
static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
if (D->param_size() != 2)
return nullptr;
const ParmVarDecl *PV = D->getParamDecl(1);
QualType Ty = PV->getType();
if (!isDispatchBlock(Ty))
return nullptr;
ASTMaker M(C);
DeclRefExpr *DR = M.makeDeclRefExpr(PV);
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
CallExpr *CE = CallExpr::Create(C, ICE, None, C.VoidTy, VK_PRValue,
SourceLocation(), FPOptionsOverride());
return CE;
}
static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
{
if (D->param_size() != 3)
return nullptr;
QualType ResultTy = D->getReturnType();
bool isBoolean = ResultTy->isBooleanType();
if (!isBoolean && !ResultTy->isIntegralType(C))
return nullptr;
const ParmVarDecl *OldValue = D->getParamDecl(0);
QualType OldValueTy = OldValue->getType();
const ParmVarDecl *NewValue = D->getParamDecl(1);
QualType NewValueTy = NewValue->getType();
assert(OldValueTy == NewValueTy);
const ParmVarDecl *TheValue = D->getParamDecl(2);
QualType TheValueTy = TheValue->getType();
const PointerType *PT = TheValueTy->getAs<PointerType>();
if (!PT)
return nullptr;
QualType PointeeTy = PT->getPointeeType();
ASTMaker M(C);
Expr *Comparison =
M.makeComparison(
M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
M.makeLvalueToRvalue(
M.makeDereference(
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
PointeeTy),
PointeeTy),
BO_EQ);
Stmt *Stmts[2];
Stmts[0] =
M.makeAssignment(
M.makeDereference(
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
PointeeTy),
M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
NewValueTy);
Expr *BoolVal = M.makeObjCBool(true);
Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
: M.makeIntegralCast(BoolVal, ResultTy);
Stmts[1] = M.makeReturn(RetVal);
CompoundStmt *Body = M.makeCompound(Stmts);
BoolVal = M.makeObjCBool(false);
RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
: M.makeIntegralCast(BoolVal, ResultTy);
Stmt *Else = M.makeReturn(RetVal);
auto *If =
IfStmt::Create(C, SourceLocation(), IfStatementKind::Ordinary,
nullptr,
nullptr, Comparison,
SourceLocation(),
SourceLocation(), Body, SourceLocation(), Else);
return If;
}
Stmt *BodyFarm::getBody(const FunctionDecl *D) {
Optional<Stmt *> &Val = Bodies[D];
if (Val)
return Val.value();
Val = nullptr;
if (D->getIdentifier() == nullptr)
return nullptr;
StringRef Name = D->getName();
if (Name.empty())
return nullptr;
FunctionFarmer FF;
if (unsigned BuiltinID = D->getBuiltinID()) {
switch (BuiltinID) {
case Builtin::BIas_const:
case Builtin::BIforward:
case Builtin::BImove:
case Builtin::BImove_if_noexcept:
FF = create_std_move_forward;
break;
default:
FF = nullptr;
break;
}
} else if (Name.startswith("OSAtomicCompareAndSwap") ||
Name.startswith("objc_atomicCompareAndSwap")) {
FF = create_OSAtomicCompareAndSwap;
} else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
FF = create_call_once;
} else {
FF = llvm::StringSwitch<FunctionFarmer>(Name)
.Case("dispatch_sync", create_dispatch_sync)
.Case("dispatch_once", create_dispatch_once)
.Default(nullptr);
}
if (FF) { Val = FF(C, D); }
else if (Injector) { Val = Injector->getBody(D); }
return *Val;
}
static const ObjCIvarDecl *findBackingIvar(const ObjCPropertyDecl *Prop) {
const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
if (IVar)
return IVar;
if (!Prop->isReadOnly())
return nullptr;
auto *Container = cast<ObjCContainerDecl>(Prop->getDeclContext());
const ObjCInterfaceDecl *PrimaryInterface = nullptr;
if (auto *InterfaceDecl = dyn_cast<ObjCInterfaceDecl>(Container)) {
PrimaryInterface = InterfaceDecl;
} else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(Container)) {
PrimaryInterface = CategoryDecl->getClassInterface();
} else if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container)) {
PrimaryInterface = ImplDecl->getClassInterface();
} else {
return nullptr;
}
auto *ShadowingProp = PrimaryInterface->FindPropertyVisibleInPrimaryClass(
Prop->getIdentifier(), Prop->getQueryKind());
if (ShadowingProp && ShadowingProp != Prop) {
IVar = ShadowingProp->getPropertyIvarDecl();
}
return IVar;
}
static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
const ObjCMethodDecl *MD) {
const ObjCIvarDecl *IVar = nullptr;
const ObjCPropertyDecl *Prop = nullptr;
if (MD->isSynthesizedAccessorStub()) {
const ObjCInterfaceDecl *IntD = MD->getClassInterface();
const ObjCImplementationDecl *ImpD = IntD->getImplementation();
for (const auto *PI : ImpD->property_impls()) {
if (const ObjCPropertyDecl *Candidate = PI->getPropertyDecl()) {
if (Candidate->getGetterName() == MD->getSelector()) {
Prop = Candidate;
IVar = Prop->getPropertyIvarDecl();
}
}
}
}
if (!IVar) {
Prop = MD->findPropertyDecl();
IVar = findBackingIvar(Prop);
}
if (!IVar || !Prop)
return nullptr;
if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak)
return nullptr;
const ObjCImplementationDecl *ImplDecl =
IVar->getContainingInterface()->getImplementation();
if (ImplDecl) {
for (const auto *I : ImplDecl->property_impls()) {
if (I->getPropertyDecl() != Prop)
continue;
if (I->getGetterCXXConstructor()) {
ASTMaker M(Ctx);
return M.makeReturn(I->getGetterCXXConstructor());
}
}
}
if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
Prop->getType().getNonReferenceType()))
return nullptr;
if (!IVar->getType()->isObjCLifetimeType() &&
!IVar->getType().isTriviallyCopyableType(Ctx))
return nullptr;
ASTMaker M(Ctx);
const VarDecl *selfVar = MD->getSelfDecl();
if (!selfVar)
return nullptr;
Expr *loadedIVar = M.makeObjCIvarRef(
M.makeLvalueToRvalue(M.makeDeclRefExpr(selfVar), selfVar->getType()),
IVar);
if (!MD->getReturnType()->isReferenceType())
loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
return M.makeReturn(loadedIVar);
}
Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
if (!D->isPropertyAccessor())
return nullptr;
D = D->getCanonicalDecl();
if (!D->isImplicit())
return nullptr;
Optional<Stmt *> &Val = Bodies[D];
if (Val)
return Val.value();
Val = nullptr;
if (D->param_size() != 0)
return nullptr;
const ObjCInterfaceDecl *OID = D->getClassInterface();
if (dyn_cast<ObjCInterfaceDecl>(D->getParent()) != OID)
for (auto *Ext : OID->known_extensions()) {
auto *OMD = Ext->getInstanceMethod(D->getSelector());
if (OMD && !OMD->isImplicit())
return nullptr;
}
Val = createObjCPropertyGetter(C, D);
return *Val;
}