Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 280 additions & 89 deletions cpp2rust/converter/converter.cpp

Large diffs are not rendered by default.

68 changes: 46 additions & 22 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

bool VisitRecoveryExpr(clang::RecoveryExpr *expr);
bool VisitExprWithCleanups(clang::ExprWithCleanups *expr);

bool VisitCXXBindTemporaryExpr(clang::CXXBindTemporaryExpr *expr);

bool VisitCastExpr(clang::CastExpr *expr);
bool VisitMaterializeTemporaryExpr(clang::MaterializeTemporaryExpr *expr);

virtual void EmitFilePreamble();
Expand Down Expand Up @@ -287,11 +291,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual const char *GetConstructorWrite() const;

// A variable named name that holds zero-initialized storage for a record,
// in which a constructor constructs the record.
virtual std::string
CreatePlacementStorage(const std::string &record_name,
std::string_view name = "__v") const;
virtual std::string CreatePlacementStorage(clang::QualType type,
std::string_view name = "__v");

// The receiver of a constructor that constructs the record in the storage of
// the variable named name.
Expand All @@ -301,6 +302,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
// the converted constructor constructs.
virtual std::string GetFieldPlacementReceiver(std::string_view field) const;

virtual std::string GetFieldReceiver(std::string_view receiver,
std::string_view field) const;

virtual std::string GetElementReceiver(std::string_view receiver,
std::string_view index) const;

// The receiver of the constructor that a delegating constructor calls.
virtual const char *GetDelegatingReceiver() const;

Expand Down Expand Up @@ -770,8 +777,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

void ConvertCXXConstructExprArgs(clang::CXXConstructExpr *expr);

void ConvertArrayConstruct(clang::CXXConstructExpr *expr);

virtual void ConvertArrayCXXConstructExpr(clang::CXXConstructExpr *expr);

clang::CXXConstructExpr *GetElementConstruct(clang::CXXConstructExpr *expr);

virtual bool
VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr);

Expand Down Expand Up @@ -1276,14 +1287,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
bool DeferTemporaries(const clang::Expr *call,
const TempMaterializationCtx *temps);

// The number of array init loops being converted, which copy arrays
// The indices of the array init loops being converted, which copy arrays
// element by element, e.g., in implicit copy constructors, and the number
// of loops outside each of them, whose indices their common expressions
// use.
unsigned array_init_depth_ = 0;
std::vector<std::string> array_init_indices_;
std::unordered_map<const clang::OpaqueValueExpr *, unsigned>
array_init_outer_depth_;
std::string ArrayInitIndexName() const;

unsigned materialized_temp_id_ = 0;
std::string NewTempName() {
Expand All @@ -1302,10 +1312,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
// Owning temporaries needed by references bound before a rule expansion.
std::string *full_expression_temporaries_ = nullptr;

// A construction that constructs its object in place, in the storage that
// receiver gives to the constructor. A local object also gets, in
// object_tail, the arguments of the object! macro after its name, if the
// constructor call is all that its construction emits.
struct InPlaceConstruct {
const clang::Expr *expr = nullptr;
std::string receiver;
Expand All @@ -1328,18 +1334,36 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
const clang::Expr *init,
std::string *declarations);

// Converts init, which constructs an object by construct, to a statement that
// constructs the object in the storage that receiver gives.
void ConstructInPlace(clang::Expr *init, const clang::Expr *construct,
std::string receiver);
void ConstructInPlace(clang::Expr *init, std::string receiver);

void ConvertIntoStorage(clang::Expr *expr, std::string receiver,
std::optional<std::string> *object_tail = nullptr);

bool ForwardsStorage(const clang::Expr *expr, const clang::Expr *operand);

void ConvertConditionalIntoStorage(clang::ConditionalOperator *expr);

void BuildOrWriteIntoStorage(clang::QualType type, clang::Expr *expr,
std::string_view receiver);

virtual void ConvertStoredValue(clang::QualType type, clang::Expr *expr);

void ConvertInitListIntoStorage(const clang::InitListExpr *list,
std::string_view receiver);

void ConvertArrayConstructIntoStorage(clang::CXXConstructExpr *expr,
clang::QualType type,
std::string_view receiver);

void ConvertElementsIntoStorage(
std::string_view receiver, uint64_t begin, uint64_t end,
const std::function<void(std::string_view)> &element);

bool ConvertInBlockStorage(clang::Expr *producer);

// The construction of decl by its initializer, if it can be done in the
// storage of decl.
const clang::Expr *GetInPlaceVarConstruct(clang::VarDecl *decl);
bool ConstructsVarInPlace(clang::VarDecl *decl);

// Declares decl, which construct constructs in place, with the object!
// macro, which also destroys it at the end of its scope.
void EmitLocalObject(clang::VarDecl *decl, const clang::Expr *construct);
void EmitLocalObject(clang::VarDecl *decl);
bool retain_bound_temporaries_ = false;
class HoistMaterializedTempBindings {
Converter &c;
Expand Down
162 changes: 112 additions & 50 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <clang/AST/ExprConcepts.h>
#include <clang/AST/Mangle.h>
#include <clang/AST/ParentMapContext.h>
#include <clang/AST/StmtVisitor.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Basic/Version.h>
#include <clang/Lex/Lexer.h>
Expand Down Expand Up @@ -1314,23 +1315,6 @@ bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) {
IsRValueConvertingConstructor(ctor));
}

static const clang::Expr *IgnorePlacementWrappers(const clang::Expr *init) {
while (true) {
init = init->IgnoreParens();
if (auto *cleanups = clang::dyn_cast<clang::ExprWithCleanups>(init)) {
init = cleanups->getSubExpr();
} else if (auto *default_init =
clang::dyn_cast<clang::CXXDefaultInitExpr>(init)) {
init = default_init->getExpr();
} else if (auto *cast = clang::dyn_cast<clang::CXXFunctionalCastExpr>(init);
cast && cast->getCastKind() == clang::CK_ConstructorConversion) {
init = cast->getSubExpr();
} else {
return init;
}
}
}

bool UsesReturnSlot(clang::QualType type) {
auto *record = type->getAsCXXRecordDecl();
return record && record->hasDefinition() && IsUserDefinedDecl(record) &&
Expand All @@ -1346,48 +1330,126 @@ bool IsReturnSlotCall(const clang::Expr *expr) {
return !callee || IsUserDefinedDecl(callee);
}

const clang::Expr *GetPlacementInitializer(const clang::Expr *init) {
auto *call = IgnorePlacementWrappers(init);
if (auto *bind = clang::dyn_cast<clang::CXXBindTemporaryExpr>(call)) {
call = bind->getSubExpr();
static bool IsStorageConstructor(const clang::CXXConstructorDecl *ctor) {
auto *parent = ctor->getParent();
if (parent->isLambda() || !IsUserDefinedDecl(parent)) {
return false;
}
if (IsReturnSlotCall(call)) {
return call;
if (ctor->isCopyOrMoveConstructor()) {
return IsConvertibleCopyOrMoveConstructor(ctor);
}
return GetInPlaceConstruct(init);
return ctor->isUserProvided() || IsConvertibleDefaultConstructor(ctor);
}

static bool IsStorageConstruct(const clang::CXXConstructExpr *expr) {
return !expr->isElidable() && !expr->getType()->isArrayType() &&
IsStorageConstructor(expr->getConstructor());
}

static bool IsStorageArrayConstruct(const clang::CXXConstructExpr *expr) {
return expr->getType()->isConstantArrayType() &&
IsStorageConstructor(expr->getConstructor());
}

const clang::InitListExpr *GetSemanticForm(const clang::InitListExpr *list) {
if (auto *semantic = list->getSemanticForm()) {
return semantic;
}
return list;
}

bool IsStorageInitList(const clang::InitListExpr *list) {
if (list->isTransparent() || IsInitExprOfStringLiteral(list)) {
return false;
}
if (list->getType()->isConstantArrayType()) {
return true;
}
auto *record = list->getType()->getAsRecordDecl();
return record && !record->isUnion() && IsUserDefinedDecl(record);
}

namespace {
class StorageTarget : public clang::ConstStmtVisitor<StorageTarget, bool> {
public:
bool VisitStmt(const clang::Stmt *) { return false; }

bool VisitParenExpr(const clang::ParenExpr *expr) {
return Visit(expr->getSubExpr());
}

bool VisitExprWithCleanups(const clang::ExprWithCleanups *expr) {
return Visit(expr->getSubExpr());
}

bool VisitCXXBindTemporaryExpr(const clang::CXXBindTemporaryExpr *expr) {
return Visit(expr->getSubExpr());
}

bool VisitCXXDefaultInitExpr(const clang::CXXDefaultInitExpr *expr) {
return Visit(expr->getExpr());
}

bool VisitCastExpr(const clang::CastExpr *expr) {
return (expr->getCastKind() == clang::CK_ConstructorConversion ||
expr->getCastKind() == clang::CK_UserDefinedConversion ||
(expr->getCastKind() == clang::CK_NoOp && expr->isPRValue() &&
expr->getSubExpr()->isPRValue())) &&
Visit(expr->getSubExpr());
}

bool VisitBinComma(const clang::BinaryOperator *expr) {
return Visit(expr->getRHS());
}

bool VisitConditionalOperator(const clang::ConditionalOperator *expr) {
return expr->isPRValue() && expr->getType()->isRecordType() &&
(Visit(expr->getTrueExpr()) || Visit(expr->getFalseExpr()));
}

bool VisitCXXConstructExpr(const clang::CXXConstructExpr *expr) {
return IsStorageConstruct(expr) || IsStorageArrayConstruct(expr);
}

bool VisitInitListExpr(const clang::InitListExpr *expr) {
auto *list = GetSemanticForm(expr);
if (list->isTransparent()) {
return Visit(list->getInit(0));
}
if (!IsStorageInitList(list)) {
return false;
}
return llvm::any_of(list->inits(),
[&](const clang::Expr *init) { return Visit(init); }) ||
(list->hasArrayFiller() && Visit(list->getArrayFiller()));
}

bool VisitArrayInitLoopExpr(const clang::ArrayInitLoopExpr *expr) {
return Visit(expr->getSubExpr());
}

bool VisitCallExpr(const clang::CallExpr *expr) {
return IsReturnSlotCall(expr);
}
};
} // namespace

bool BuildsInStorage(const clang::Expr *expr) {
return StorageTarget().Visit(expr);
}

const clang::Expr *
GetTemporaryPlacementInitializer(const clang::ASTContext &ctx,
const clang::Expr *temporary,
clang::QualType type) {
auto *init = GetPlacementInitializer(temporary->IgnoreImplicit());
if (!init || !ctx.hasSameUnqualifiedType(init->getType(), type)) {
auto *init = temporary->IgnoreImplicit();
if (!BuildsInStorage(init) ||
!ctx.hasSameUnqualifiedType(init->getType(), type)) {
return nullptr;
}
return init;
}

const clang::CXXConstructExpr *GetInPlaceConstruct(const clang::Expr *init) {
init = IgnorePlacementWrappers(init);
auto *construct = clang::dyn_cast<clang::CXXConstructExpr>(init);
if (!construct || construct->isElidable() ||
construct->getType()->isArrayType()) {
return nullptr;
}
auto *ctor = construct->getConstructor();
auto *parent = ctor->getParent();
if (parent->isLambda() || !IsUserDefinedDecl(parent)) {
return nullptr;
}
if (ctor->isCopyOrMoveConstructor()) {
return IsConvertibleCopyOrMoveConstructor(ctor) ? construct : nullptr;
}
return ctor->isUserProvided() || IsConvertibleDefaultConstructor(ctor)
? construct
: nullptr;
}

bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) {
return decl->isThisDeclarationADefinition() && !decl->isDependentContext() &&
!decl->isInvalidDecl();
Expand Down Expand Up @@ -1660,12 +1722,12 @@ unsigned GetArraySize(clang::QualType array_type) {
}

static std::string GetLocationID(const clang::Decl *decl) {
auto id = GetFileName(decl) + std::to_string(GetLineNumber(decl)) +
std::to_string(GetColumnNumber(decl));
auto id = std::format("{}:{}:{}", GetFileName(decl), GetLineNumber(decl),
GetColumnNumber(decl));
if (auto loc = decl->getLocation(); loc.isMacroID()) {
const auto &src_mgr = decl->getASTContext().getSourceManager();
id += std::to_string(src_mgr.getSpellingLineNumber(loc)) +
std::to_string(src_mgr.getSpellingColumnNumber(loc));
id += std::format(":{}:{}", src_mgr.getSpellingLineNumber(loc),
src_mgr.getSpellingColumnNumber(loc));
}
return id;
}
Expand Down
11 changes: 5 additions & 6 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,15 @@ bool HasDefaultedCopyAssignment(const clang::RecordDecl *decl);

bool IsRValueConvertingConstructor(const clang::CXXConstructorDecl *ctor);

// The call of a translated constructor that init, the initializer of an
// object, is. The constructor can construct the object in its storage, as it
// is translated to a function that receives the storage.
const clang::CXXConstructExpr *GetInPlaceConstruct(const clang::Expr *init);

bool UsesReturnSlot(clang::QualType type);

bool IsReturnSlotCall(const clang::Expr *expr);

const clang::Expr *GetPlacementInitializer(const clang::Expr *init);
const clang::InitListExpr *GetSemanticForm(const clang::InitListExpr *list);

bool IsStorageInitList(const clang::InitListExpr *list);

bool BuildsInStorage(const clang::Expr *expr);

const clang::Expr *
GetTemporaryPlacementInitializer(const clang::ASTContext &ctx,
Expand Down
4 changes: 1 addition & 3 deletions cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ class AddressTakenVisitor

bool TraverseDecltypeType(clang::DecltypeType *, bool) { return true; }

// A variable that a translated constructor constructs is passed to the
// constructor as `this`, which constructs it in place.
bool VisitVarDecl(clang::VarDecl *decl) {
if (decl->isLocalVarDecl() && decl->hasInit() &&
GetPlacementInitializer(decl->getInit())) {
BuildsInStorage(decl->getInit())) {
AddressTaken(decl);
}
return true;
Expand Down
Loading
Loading