2012-09-21 08:09:11 +08:00
|
|
|
//== BodyFarm.cpp - Factory for conjuring up fake bodies ----------*- C++ -*-//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// BodyFarm is a factory for creating faux implementations for functions/methods
|
|
|
|
// for analysis purposes.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "BodyFarm.h"
|
2012-09-21 08:09:11 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2012-10-12 04:58:18 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2012-09-21 08:09:11 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2012-09-22 01:54:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper creation functions for constructing faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-09-21 08:09:11 +08:00
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
static bool isDispatchBlock(QualType Ty) {
|
|
|
|
// Is it a block pointer?
|
|
|
|
const BlockPointerType *BPT = Ty->getAs<BlockPointerType>();
|
|
|
|
if (!BPT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check if the block pointer type takes no arguments and
|
|
|
|
// returns void.
|
|
|
|
const FunctionProtoType *FT =
|
|
|
|
BPT->getPointeeType()->getAs<FunctionProtoType>();
|
2014-01-26 00:55:45 +08:00
|
|
|
if (!FT || !FT->getReturnType()->isVoidType() || FT->getNumParams() != 0)
|
2012-09-21 08:52:24 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
namespace {
|
|
|
|
class ASTMaker {
|
|
|
|
public:
|
|
|
|
ASTMaker(ASTContext &C) : C(C) {}
|
|
|
|
|
2012-09-22 02:33:54 +08:00
|
|
|
/// Create a new BinaryOperator representing a simple assignment.
|
|
|
|
BinaryOperator *makeAssignment(const Expr *LHS, const Expr *RHS, QualType Ty);
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create a new BinaryOperator representing a comparison.
|
|
|
|
BinaryOperator *makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op);
|
|
|
|
|
|
|
|
/// Create a new compound stmt using the provided statements.
|
|
|
|
CompoundStmt *makeCompound(ArrayRef<Stmt*>);
|
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
/// Create a new DeclRefExpr for the referenced variable.
|
2012-09-22 01:54:35 +08:00
|
|
|
DeclRefExpr *makeDeclRefExpr(const VarDecl *D);
|
|
|
|
|
2012-09-22 02:33:52 +08:00
|
|
|
/// Create a new UnaryOperator representing a dereference.
|
|
|
|
UnaryOperator *makeDereference(const Expr *Arg, QualType Ty);
|
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
/// Create an implicit cast for an integer conversion.
|
2012-10-12 08:18:19 +08:00
|
|
|
Expr *makeIntegralCast(const Expr *Arg, QualType Ty);
|
2012-09-22 02:13:23 +08:00
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create an implicit cast to a builtin boolean type.
|
|
|
|
ImplicitCastExpr *makeIntegralCastToBoolean(const Expr *Arg);
|
|
|
|
|
2012-09-22 02:13:27 +08:00
|
|
|
// Create an implicit cast for lvalue-to-rvaluate conversions.
|
|
|
|
ImplicitCastExpr *makeLvalueToRvalue(const Expr *Arg, QualType Ty);
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
/// Create an Objective-C bool literal.
|
|
|
|
ObjCBoolLiteralExpr *makeObjCBool(bool Val);
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
/// Create an Objective-C ivar reference.
|
|
|
|
ObjCIvarRefExpr *makeObjCIvarRef(const Expr *Base, const ObjCIvarDecl *IVar);
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
/// Create a Return statement.
|
|
|
|
ReturnStmt *makeReturn(const Expr *RetVal);
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
private:
|
|
|
|
ASTContext &C;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-09-22 02:33:54 +08:00
|
|
|
BinaryOperator *ASTMaker::makeAssignment(const Expr *LHS, const Expr *RHS,
|
|
|
|
QualType Ty) {
|
|
|
|
return new (C) BinaryOperator(const_cast<Expr*>(LHS), const_cast<Expr*>(RHS),
|
|
|
|
BO_Assign, Ty, VK_RValue,
|
2012-10-02 12:45:10 +08:00
|
|
|
OK_Ordinary, SourceLocation(), false);
|
2012-09-22 02:33:54 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
BinaryOperator *ASTMaker::makeComparison(const Expr *LHS, const Expr *RHS,
|
|
|
|
BinaryOperator::Opcode Op) {
|
|
|
|
assert(BinaryOperator::isLogicalOp(Op) ||
|
|
|
|
BinaryOperator::isComparisonOp(Op));
|
|
|
|
return new (C) BinaryOperator(const_cast<Expr*>(LHS),
|
|
|
|
const_cast<Expr*>(RHS),
|
|
|
|
Op,
|
|
|
|
C.getLogicalOperationType(),
|
|
|
|
VK_RValue,
|
|
|
|
OK_Ordinary, SourceLocation(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompoundStmt *ASTMaker::makeCompound(ArrayRef<Stmt *> Stmts) {
|
2012-12-30 04:03:39 +08:00
|
|
|
return new (C) CompoundStmt(C, Stmts, SourceLocation(), SourceLocation());
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
DeclRefExpr *ASTMaker::makeDeclRefExpr(const VarDecl *D) {
|
|
|
|
DeclRefExpr *DR =
|
|
|
|
DeclRefExpr::Create(/* Ctx = */ C,
|
|
|
|
/* QualifierLoc = */ NestedNameSpecifierLoc(),
|
|
|
|
/* TemplateKWLoc = */ SourceLocation(),
|
|
|
|
/* D = */ const_cast<VarDecl*>(D),
|
|
|
|
/* isEnclosingLocal = */ false,
|
|
|
|
/* NameLoc = */ SourceLocation(),
|
|
|
|
/* T = */ D->getType(),
|
|
|
|
/* VK = */ VK_LValue);
|
|
|
|
return DR;
|
|
|
|
}
|
|
|
|
|
2012-09-22 02:33:52 +08:00
|
|
|
UnaryOperator *ASTMaker::makeDereference(const Expr *Arg, QualType Ty) {
|
|
|
|
return new (C) UnaryOperator(const_cast<Expr*>(Arg), UO_Deref, Ty,
|
|
|
|
VK_LValue, OK_Ordinary, SourceLocation());
|
|
|
|
}
|
|
|
|
|
2012-09-22 02:13:27 +08:00
|
|
|
ImplicitCastExpr *ASTMaker::makeLvalueToRvalue(const Expr *Arg, QualType Ty) {
|
|
|
|
return ImplicitCastExpr::Create(C, Ty, CK_LValueToRValue,
|
2014-05-20 12:30:07 +08:00
|
|
|
const_cast<Expr*>(Arg), nullptr, VK_RValue);
|
2012-09-22 02:13:27 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 08:18:19 +08:00
|
|
|
Expr *ASTMaker::makeIntegralCast(const Expr *Arg, QualType Ty) {
|
|
|
|
if (Arg->getType() == Ty)
|
|
|
|
return const_cast<Expr*>(Arg);
|
2014-05-20 12:30:07 +08:00
|
|
|
|
2012-09-22 02:13:23 +08:00
|
|
|
return ImplicitCastExpr::Create(C, Ty, CK_IntegralCast,
|
2014-05-20 12:30:07 +08:00
|
|
|
const_cast<Expr*>(Arg), nullptr, VK_RValue);
|
2012-09-22 02:13:23 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
ImplicitCastExpr *ASTMaker::makeIntegralCastToBoolean(const Expr *Arg) {
|
|
|
|
return ImplicitCastExpr::Create(C, C.BoolTy, CK_IntegralToBoolean,
|
2014-05-20 12:30:07 +08:00
|
|
|
const_cast<Expr*>(Arg), nullptr, VK_RValue);
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCBoolLiteralExpr *ASTMaker::makeObjCBool(bool Val) {
|
|
|
|
QualType Ty = C.getBOOLDecl() ? C.getBOOLType() : C.ObjCBuiltinBoolTy;
|
|
|
|
return new (C) ObjCBoolLiteralExpr(Val, Ty, SourceLocation());
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:06:06 +08:00
|
|
|
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),
|
|
|
|
/*arrow=*/true, /*free=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
ReturnStmt *ASTMaker::makeReturn(const Expr *RetVal) {
|
2014-05-20 12:30:07 +08:00
|
|
|
return new (C) ReturnStmt(SourceLocation(), const_cast<Expr*>(RetVal),
|
|
|
|
nullptr);
|
2012-10-12 04:58:18 +08:00
|
|
|
}
|
|
|
|
|
2012-09-22 01:54:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Creation functions for faux ASTs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
typedef Stmt *(*FunctionFarmer)(ASTContext &C, const FunctionDecl *D);
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
/// Create a fake body for dispatch_once.
|
|
|
|
static Stmt *create_dispatch_once(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
// Check if we have at least two parameters.
|
|
|
|
if (D->param_size() != 2)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// Check if the first parameter is a pointer to integer type.
|
|
|
|
const ParmVarDecl *Predicate = D->getParamDecl(0);
|
|
|
|
QualType PredicateQPtrTy = Predicate->getType();
|
|
|
|
const PointerType *PredicatePtrTy = PredicateQPtrTy->getAs<PointerType>();
|
|
|
|
if (!PredicatePtrTy)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-09-21 08:52:24 +08:00
|
|
|
QualType PredicateTy = PredicatePtrTy->getPointeeType();
|
|
|
|
if (!PredicateTy->isIntegerType())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// Check if the second parameter is the proper block type.
|
|
|
|
const ParmVarDecl *Block = D->getParamDecl(1);
|
|
|
|
QualType Ty = Block->getType();
|
|
|
|
if (!isDispatchBlock(Ty))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// Everything checks out. Create a fakse body that checks the predicate,
|
|
|
|
// sets it, and calls the block. Basically, an AST dump of:
|
|
|
|
//
|
|
|
|
// void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block) {
|
|
|
|
// if (!*predicate) {
|
|
|
|
// *predicate = 1;
|
|
|
|
// block();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-09-22 01:54:35 +08:00
|
|
|
ASTMaker M(C);
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// (1) Create the call.
|
2012-09-22 01:54:35 +08:00
|
|
|
DeclRefExpr *DR = M.makeDeclRefExpr(Block);
|
2012-09-22 02:13:27 +08:00
|
|
|
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
|
2013-05-05 08:41:58 +08:00
|
|
|
CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
|
|
|
|
SourceLocation());
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// (2) Create the assignment to the predicate.
|
|
|
|
IntegerLiteral *IL =
|
|
|
|
IntegerLiteral::Create(C, llvm::APInt(C.getTypeSize(C.IntTy), (uint64_t) 1),
|
|
|
|
C.IntTy, SourceLocation());
|
2012-09-22 02:33:56 +08:00
|
|
|
BinaryOperator *B =
|
|
|
|
M.makeAssignment(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate), PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
|
|
|
M.makeIntegralCast(IL, PredicateTy),
|
|
|
|
PredicateTy);
|
|
|
|
|
2012-09-21 08:52:24 +08:00
|
|
|
// (3) Create the compound statement.
|
|
|
|
Stmt *Stmts[2];
|
|
|
|
Stmts[0] = B;
|
2012-10-12 04:58:18 +08:00
|
|
|
Stmts[1] = CE;
|
|
|
|
CompoundStmt *CS = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// (4) Create the 'if' condition.
|
2012-09-22 02:33:56 +08:00
|
|
|
ImplicitCastExpr *LValToRval =
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(Predicate),
|
|
|
|
PredicateQPtrTy),
|
|
|
|
PredicateTy),
|
|
|
|
PredicateTy);
|
|
|
|
|
|
|
|
UnaryOperator *UO = new (C) UnaryOperator(LValToRval, UO_LNot, C.IntTy,
|
|
|
|
VK_RValue, OK_Ordinary,
|
|
|
|
SourceLocation());
|
2012-09-21 08:52:24 +08:00
|
|
|
|
|
|
|
// (5) Create the 'if' statement.
|
2014-05-20 12:30:07 +08:00
|
|
|
IfStmt *If = new (C) IfStmt(C, SourceLocation(), nullptr, UO, CS);
|
2012-09-21 08:52:24 +08:00
|
|
|
return If;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
/// Create a fake body for dispatch_sync.
|
|
|
|
static Stmt *create_dispatch_sync(ASTContext &C, const FunctionDecl *D) {
|
|
|
|
// Check if we have at least two parameters.
|
|
|
|
if (D->param_size() != 2)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
// Check if the second parameter is a block.
|
|
|
|
const ParmVarDecl *PV = D->getParamDecl(1);
|
|
|
|
QualType Ty = PV->getType();
|
2012-09-21 08:52:24 +08:00
|
|
|
if (!isDispatchBlock(Ty))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
// Everything checks out. Create a fake body that just calls the block.
|
|
|
|
// This is basically just an AST dump of:
|
|
|
|
//
|
|
|
|
// void dispatch_sync(dispatch_queue_t queue, void (^block)(void)) {
|
|
|
|
// block();
|
|
|
|
// }
|
2012-09-22 01:54:35 +08:00
|
|
|
//
|
|
|
|
ASTMaker M(C);
|
|
|
|
DeclRefExpr *DR = M.makeDeclRefExpr(PV);
|
2012-09-22 02:33:52 +08:00
|
|
|
ImplicitCastExpr *ICE = M.makeLvalueToRvalue(DR, Ty);
|
2013-05-05 08:41:58 +08:00
|
|
|
CallExpr *CE = new (C) CallExpr(C, ICE, None, C.VoidTy, VK_RValue,
|
|
|
|
SourceLocation());
|
2012-09-21 08:09:11 +08:00
|
|
|
return CE;
|
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
static Stmt *create_OSAtomicCompareAndSwap(ASTContext &C, const FunctionDecl *D)
|
|
|
|
{
|
|
|
|
// There are exactly 3 arguments.
|
|
|
|
if (D->param_size() != 3)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-02-06 03:52:26 +08:00
|
|
|
// Signature:
|
|
|
|
// _Bool OSAtomicCompareAndSwapPtr(void *__oldValue,
|
|
|
|
// void *__newValue,
|
|
|
|
// void * volatile *__theValue)
|
|
|
|
// Generate body:
|
2012-10-12 04:58:18 +08:00
|
|
|
// if (oldValue == *theValue) {
|
|
|
|
// *theValue = newValue;
|
|
|
|
// return YES;
|
|
|
|
// }
|
|
|
|
// else return NO;
|
2014-01-26 00:55:45 +08:00
|
|
|
|
|
|
|
QualType ResultTy = D->getReturnType();
|
2012-10-12 08:18:19 +08:00
|
|
|
bool isBoolean = ResultTy->isBooleanType();
|
|
|
|
if (!isBoolean && !ResultTy->isIntegralType(C))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
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)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-10-12 04:58:18 +08:00
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
|
|
|
|
|
|
|
ASTMaker M(C);
|
|
|
|
// Construct the comparison.
|
|
|
|
Expr *Comparison =
|
|
|
|
M.makeComparison(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(OldValue), OldValueTy),
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
|
|
|
|
PointeeTy),
|
|
|
|
PointeeTy),
|
|
|
|
BO_EQ);
|
|
|
|
|
|
|
|
// Construct the body of the IfStmt.
|
|
|
|
Stmt *Stmts[2];
|
|
|
|
Stmts[0] =
|
|
|
|
M.makeAssignment(
|
|
|
|
M.makeDereference(
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(TheValue), TheValueTy),
|
|
|
|
PointeeTy),
|
|
|
|
M.makeLvalueToRvalue(M.makeDeclRefExpr(NewValue), NewValueTy),
|
|
|
|
NewValueTy);
|
2012-10-12 08:18:19 +08:00
|
|
|
|
|
|
|
Expr *BoolVal = M.makeObjCBool(true);
|
|
|
|
Expr *RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmts[1] = M.makeReturn(RetVal);
|
2012-10-12 04:58:18 +08:00
|
|
|
CompoundStmt *Body = M.makeCompound(ArrayRef<Stmt*>(Stmts, 2));
|
|
|
|
|
|
|
|
// Construct the else clause.
|
2012-10-12 08:18:19 +08:00
|
|
|
BoolVal = M.makeObjCBool(false);
|
|
|
|
RetVal = isBoolean ? M.makeIntegralCastToBoolean(BoolVal)
|
|
|
|
: M.makeIntegralCast(BoolVal, ResultTy);
|
|
|
|
Stmt *Else = M.makeReturn(RetVal);
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
/// Construct the If.
|
|
|
|
Stmt *If =
|
2014-05-20 12:30:07 +08:00
|
|
|
new (C) IfStmt(C, SourceLocation(), nullptr, Comparison, Body,
|
2012-10-12 04:58:18 +08:00
|
|
|
SourceLocation(), Else);
|
2014-05-20 12:30:07 +08:00
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
return If;
|
|
|
|
}
|
|
|
|
|
2012-09-21 08:09:11 +08:00
|
|
|
Stmt *BodyFarm::getBody(const FunctionDecl *D) {
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<Stmt *> &Val = Bodies[D];
|
2012-09-21 08:09:11 +08:00
|
|
|
if (Val.hasValue())
|
|
|
|
return Val.getValue();
|
2014-05-20 12:30:07 +08:00
|
|
|
|
|
|
|
Val = nullptr;
|
|
|
|
|
|
|
|
if (D->getIdentifier() == nullptr)
|
|
|
|
return nullptr;
|
2012-09-21 08:09:11 +08:00
|
|
|
|
|
|
|
StringRef Name = D->getName();
|
|
|
|
if (Name.empty())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2012-10-12 04:58:18 +08:00
|
|
|
|
|
|
|
FunctionFarmer FF;
|
|
|
|
|
|
|
|
if (Name.startswith("OSAtomicCompareAndSwap") ||
|
|
|
|
Name.startswith("objc_atomicCompareAndSwap")) {
|
|
|
|
FF = create_OSAtomicCompareAndSwap;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FF = llvm::StringSwitch<FunctionFarmer>(Name)
|
|
|
|
.Case("dispatch_sync", create_dispatch_sync)
|
|
|
|
.Case("dispatch_once", create_dispatch_once)
|
2014-05-20 12:30:07 +08:00
|
|
|
.Default(nullptr);
|
2012-09-21 08:09:11 +08:00
|
|
|
}
|
|
|
|
|
2012-10-12 04:58:18 +08:00
|
|
|
if (FF) { Val = FF(C, D); }
|
2012-09-21 08:09:11 +08:00
|
|
|
return Val.getValue();
|
|
|
|
}
|
|
|
|
|
2014-01-11 04:06:06 +08:00
|
|
|
static Stmt *createObjCPropertyGetter(ASTContext &Ctx,
|
|
|
|
const ObjCPropertyDecl *Prop) {
|
2014-01-23 11:59:10 +08:00
|
|
|
// First, find the backing ivar.
|
2014-01-11 04:06:06 +08:00
|
|
|
const ObjCIvarDecl *IVar = Prop->getPropertyIvarDecl();
|
|
|
|
if (!IVar)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-23 11:59:10 +08:00
|
|
|
|
|
|
|
// Ignore weak variables, which have special behavior.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-15 01:29:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Look to see if Sema has synthesized a body for us. This happens in
|
|
|
|
// Objective-C++ because the return value may be a C++ class type with a
|
|
|
|
// non-trivial copy constructor. We can only do this if we can find the
|
|
|
|
// @synthesize for this property, though (or if we know it's been auto-
|
|
|
|
// synthesized).
|
2014-01-15 01:29:06 +08:00
|
|
|
const ObjCImplementationDecl *ImplDecl =
|
|
|
|
IVar->getContainingInterface()->getImplementation();
|
|
|
|
if (ImplDecl) {
|
2014-03-14 23:02:45 +08:00
|
|
|
for (const auto *I : ImplDecl->property_impls()) {
|
2014-01-15 01:29:06 +08:00
|
|
|
if (I->getPropertyDecl() != Prop)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (I->getGetterCXXConstructor()) {
|
|
|
|
ASTMaker M(Ctx);
|
|
|
|
return M.makeReturn(I->getGetterCXXConstructor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Sanity check that the property is the same type as the ivar, or a
|
|
|
|
// reference to it, and that it is either an object pointer or trivially
|
|
|
|
// copyable.
|
|
|
|
if (!Ctx.hasSameUnqualifiedType(IVar->getType(),
|
|
|
|
Prop->getType().getNonReferenceType()))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-23 11:59:10 +08:00
|
|
|
if (!IVar->getType()->isObjCLifetimeType() &&
|
|
|
|
!IVar->getType().isTriviallyCopyableType(Ctx))
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// Generate our body:
|
|
|
|
// return self->_ivar;
|
2014-01-11 04:06:06 +08:00
|
|
|
ASTMaker M(Ctx);
|
|
|
|
|
|
|
|
const VarDecl *selfVar = Prop->getGetterMethodDecl()->getSelfDecl();
|
|
|
|
|
|
|
|
Expr *loadedIVar =
|
|
|
|
M.makeObjCIvarRef(
|
|
|
|
M.makeLvalueToRvalue(
|
|
|
|
M.makeDeclRefExpr(selfVar),
|
|
|
|
selfVar->getType()),
|
|
|
|
IVar);
|
|
|
|
|
|
|
|
if (!Prop->getType()->isReferenceType())
|
|
|
|
loadedIVar = M.makeLvalueToRvalue(loadedIVar, IVar->getType());
|
|
|
|
|
|
|
|
return M.makeReturn(loadedIVar);
|
|
|
|
}
|
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
Stmt *BodyFarm::getBody(const ObjCMethodDecl *D) {
|
|
|
|
// We currently only know how to synthesize property accessors.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!D->isPropertyAccessor())
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
D = D->getCanonicalDecl();
|
|
|
|
|
|
|
|
Optional<Stmt *> &Val = Bodies[D];
|
|
|
|
if (Val.hasValue())
|
|
|
|
return Val.getValue();
|
2014-05-20 12:30:07 +08:00
|
|
|
Val = nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
const ObjCPropertyDecl *Prop = D->findPropertyDecl();
|
2014-01-11 04:06:06 +08:00
|
|
|
if (!Prop)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
2014-01-23 11:59:10 +08:00
|
|
|
// For now, we only synthesize getters.
|
2014-01-11 04:06:06 +08:00
|
|
|
if (D->param_size() != 0)
|
2014-05-20 12:30:07 +08:00
|
|
|
return nullptr;
|
2014-01-11 04:06:06 +08:00
|
|
|
|
|
|
|
Val = createObjCPropertyGetter(C, Prop);
|
|
|
|
|
|
|
|
return Val.getValue();
|
|
|
|
}
|
|
|
|
|