Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign abi generation for multi_index and singleton #201

Merged
merged 18 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ add_unit_test( system_tests )
add_unit_test( time_tests )
add_unit_test( varint_tests )

add_test( NAME toolchain_tests COMMAND ${CMAKE_BINARY_DIR}/tools/toolchain-tester/toolchain-tester ${CMAKE_SOURCE_DIR}/tests/toolchain --cdt ${CMAKE_BINARY_DIR}/bin )
add_test( NAME toolchain_tests COMMAND ${CMAKE_BINARY_DIR}/tools/toolchain-tester/toolchain-tester ${CMAKE_SOURCE_DIR}/tests/toolchain --cdt ${CMAKE_BINARY_DIR}/bin --verbose )
set_property(TEST toolchain_tests PROPERTY LABELS toolchain_tests)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/unit/version_tests.sh ${CMAKE_BINARY_DIR}/tests/unit/version_tests.sh COPYONLY)
Expand Down
50 changes: 50 additions & 0 deletions tests/toolchain/abigen-pass/exclude_from_abi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <eosio/eosio.hpp>
#include <eosio/name.hpp>
#include <eosio/singleton.hpp>
#include <eosio/multi_index.hpp>



struct [[eosio::table]] out_of_class {
uint64_t id;
uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index<"mi.config55"_n, out_of_class> out_of_class_index;
using uout_of_class_index = eosio::multi_index<"mi.config551"_n, out_of_class>;

typedef eosio::singleton<"smpl.conf55"_n, eosio::name> smpl_config55;
typedef eosio::singleton<"config55"_n, out_of_class> config55;
typedef smpl_config55 smpl_config551;
typedef config55 config551;
using smpl_conf551 = eosio::singleton<"smpl.conf551"_n, eosio::name>;
using config552 = eosio::singleton<"config552"_n, out_of_class>;
using smpl_conf552 = smpl_conf551;
using config553 = config551;

class [[eosio::contract("singleton_contract_simple2")]] singleton_contract_simple2 : public eosio::contract {
public:
using eosio::contract::contract;

[[eosio::action]]
void whatever() {};

struct [[eosio::table]] inside_class {
uint64_t id;
uint64_t primary_key() const { return id; }
};
typedef eosio::singleton<"smpl.conf552"_n, eosio::name> smpl_conf552;
typedef eosio::singleton<"config552"_n, inside_class> config552;
typedef smpl_conf552 smpl_conf553;
typedef config552 config553;
using smpl_conf554 = eosio::singleton<"smpl.conf554"_n, eosio::name>;
using config554 = eosio::singleton<"config554"_n, inside_class>;
using smpl_conf555 = smpl_conf554;
using config555 = config554;



typedef eosio::multi_index<"mi.config553"_n, inside_class> inside_class_index;
using uinside_class_index = eosio::multi_index<"mi.config554"_n, inside_class>;
};
21 changes: 21 additions & 0 deletions tests/toolchain/abigen-pass/singleton_contract.abi
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@
"index_type": "i64",
"key_names": [],
"key_types": []
},
{
"name": "config55",
"type": "out_of_class",
"index_type": "i64",
"key_names": [],
"key_types": []
},
{
"name": "smpl.conf5",
"type": "name",
"index_type": "i64",
"key_names": [],
"key_types": []
},
{
"name": "smpl.config",
"type": "name",
"index_type": "i64",
"key_names": [],
"key_types": []
}
],
"ricardian_clauses": [],
Expand Down
25 changes: 23 additions & 2 deletions tests/toolchain/abigen-pass/singleton_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@
#include <eosio/eosio.hpp>
#include <eosio/name.hpp>
#include <eosio/singleton.hpp>

#include "exclude_from_abi.hpp"

using namespace eosio;


struct [[eosio::table]] out_of_class2 {
linh2931 marked this conversation as resolved.
Show resolved Hide resolved
uint64_t id;
uint64_t primary_key() const { return id; }
};
typedef eosio::multi_index<"mi.config5"_n, out_of_class2> out_of_class_index51;
using uout_of_class_index51 = eosio::multi_index<"mi.config51"_n, out_of_class2>;

typedef eosio::singleton<"smpl.conf5"_n, eosio::name> smpl_config5;
typedef eosio::singleton<"config5"_n, out_of_class2> config5;
typedef smpl_config5 smpl_config51;
typedef config5 config51;
using smpl_conf51 = eosio::singleton<"smpl.conf51"_n, eosio::name>;
using config52 = eosio::singleton<"config52"_n, out_of_class2>;
using smpl_conf52 = smpl_conf51;
using config53 = config51;

class [[eosio::contract("singleton_contract")]] singleton_contract : public contract {
public:
using contract::contract;
Expand All @@ -17,5 +35,8 @@ class [[eosio::contract("singleton_contract")]] singleton_contract : public cont
uint64_t x;
};

typedef eosio::singleton<"config"_n, tbl_config> config;
typedef eosio::singleton<"config"_n, tbl_config> config;
typedef eosio::singleton<"smpl.config"_n, name> smpl_config;
using smpl_config2 = smpl_config5;
typedef config551 config2; //from exclude_from_abi.hpp
};
31 changes: 0 additions & 31 deletions tests/toolchain/abigen-pass/singleton_contract_simple.abi

This file was deleted.

16 changes: 0 additions & 16 deletions tests/toolchain/abigen-pass/singleton_contract_simple.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions tests/toolchain/abigen-pass/singleton_contract_simple.json

This file was deleted.

65 changes: 64 additions & 1 deletion tools/include/eosio/abigen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ namespace eosio { namespace cdt {
private:
bool has_added_clauses = false;
abigen& ag = abigen::get();
const clang::CXXRecordDecl* contract_class = NULL;

public:
explicit eosio_abigen_visitor(CompilerInstance *CI) {
Expand Down Expand Up @@ -799,10 +800,72 @@ namespace eosio { namespace cdt {
}
return true;
}

const clang::CXXRecordDecl* find_contract_class(const clang::ASTContext &ctx) const {
const auto* translation_unit = ctx.getTranslationUnitDecl();
// scanning entire translation unit to find contract class
for (const clang::Decl* cur_decl : translation_unit->decls()) {
if (const auto* cxx_decl = llvm::dyn_cast<clang::CXXRecordDecl>(cur_decl)) {

if (cxx_decl->isEosioContract()) {
auto attr_name = cxx_decl->getEosioContractAttr()->getName().str();
auto name = attr_name.empty() ? cxx_decl->getName().str() : attr_name;
if (name == ag.get_contract_name())
return cxx_decl;
}
else {
const auto* parent_decl = llvm::dyn_cast<clang::CXXRecordDecl>(cxx_decl->getParent());
heifner marked this conversation as resolved.
Show resolved Hide resolved
if (parent_decl && parent_decl->isEosioContract()) {
auto attr_name = parent_decl->getEosioContractAttr()->getName().str();
auto name = attr_name.empty() ? parent_decl->getName().str() : attr_name;
if (name == ag.get_contract_name())
return parent_decl;
heifner marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}

return nullptr;
}

bool is_same_type(const clang::Decl* decl1, const clang::CXXRecordDecl* decl2) const {
if (!decl1 || !decl2)
return false;
if (decl1 == decl2)
return true;

// checking if declaration is a typedef or using
if (const clang::TypedefNameDecl* typedef_decl = llvm::dyn_cast<clang::TypedefNameDecl>(decl1)) {
if (const auto* cur_type = typedef_decl->getUnderlyingType().getTypePtrOrNull()) {
if (decl2 == cur_type->getAsCXXRecordDecl()) {
return true;
}
}
}

return false;
}

bool defined_in_contract(const clang::ClassTemplateSpecializationDecl* decl) {

if (!contract_class) {
contract_class = find_contract_class(decl->getASTContext());
if (!contract_class)
CDT_ERROR("abigen_error", decl->getLocation(), "contract class not found");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will CDT_ERROR throw? Should just report the error and return false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. this one doesn't throw so I changed the macro

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not an error to not find the contract class, right? Just means this type is not in a contract class.

}

for (const clang::Decl* cur_decl : contract_class->decls()) {
if (is_same_type(cur_decl, decl))
return true;
}

return false;
}

virtual bool VisitDecl(clang::Decl* decl) {
if (const auto* d = dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
bool is_singleton = d->getName() == "singleton";
if (d->getName() == "multi_index" || is_singleton) {
if ((d->getName() == "multi_index" || is_singleton) && defined_in_contract(d)) {
ag.add_table(d->getTemplateArgs()[0].getAsIntegral().getExtValue(),
d->getTemplateArgs()[1].getAsType().getTypePtr()->getAsCXXRecordDecl(), is_singleton);
}
Expand Down