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

Remove Either struct #129

Merged
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
18 changes: 4 additions & 14 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,8 @@ static inline void tvm_transfer(schema::lazy<schema::MsgAddressInt> dest, unsign
builder b = build(out_msg.info);
b = build(b, out_msg.init);
slice payload_sl = payload.ctos();
if (b.brembits() > payload_sl.sbits()) {
out_msg.body = anyval{payload_sl};
b = build(b, out_msg.body);
} else {
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
}
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
tvm_sendmsg(b.endc(), flags);
}
static inline void tvm_transfer(slice dest, unsigned nanograms, unsigned bounce, unsigned flags, cell payload) {
Expand All @@ -317,13 +312,8 @@ static inline void tvm_transfer(slice dest, unsigned nanograms, unsigned bounce,
builder b = build(out_msg.info);
b = build(b, out_msg.init);
slice payload_sl = payload.ctos();
if (b.brembits() > payload_sl.sbits()) {
out_msg.body = anyval{payload_sl};
b = build(b, out_msg.body);
} else {
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
}
out_msg.body = ref<anyval>{payload_sl};
b = build(b, out_msg.body);
tvm_sendmsg(b.endc(), flags);
}
static inline void tvm_transfer(schema::lazy<schema::MsgAddressInt> dest, unsigned nanograms, bool bounce,
Expand Down
145 changes: 42 additions & 103 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/contract_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,20 @@ cell contract_call_prepare(address addr, Evers amount,
out_info.created_at = 0;
out_info.value = amount;

using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = out_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
} else {
message_relaxed<decltype(hdr_plus_args)> out_msg;
out_msg.info = out_info;
out_msg.body = hdr_plus_args;
return build(out_msg).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = out_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
}

template<auto Func, class... Args>
__always_inline
cell internal_body_prepare(address addr, Args... args) {
auto hdr = prepare_internal_header<Func>();
auto hdr_plus_args = std::make_tuple(hdr, args...);
using est_t = estimate_element<decltype(hdr_plus_args)>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
} else {
return build(hdr_plus_args).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
}

// Prepare message body for external call
Expand All @@ -108,13 +95,8 @@ cell external_body_prepare(address addr, Args... args) {
.function_id = uint32(id_v<Func>)
};
auto hdr_plus_args = std::make_tuple(signature_place, hdr, args...);
using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
} else {
return build(hdr_plus_args).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
}

template<auto Func, class... Args>
Expand All @@ -131,13 +113,8 @@ cell external_body_prepare_with_pubkey(address addr, uint256 pubkey, Args... arg
.function_id = uint32(id_v<Func>)
};
auto hdr_plus_args = std::make_tuple(signature_place, hdr, args...);
using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
} else {
return build(hdr_plus_args).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
return build(chain_tup).endc();
}

template<bool has_pubkey, bool has_time, bool has_expire>
Expand Down Expand Up @@ -191,19 +168,11 @@ cell external_call_prepare(address addr, uint256 pubkey, Args... args) {
.import_fee = Evers{0}
};

using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
} else {
message_relaxed<decltype(hdr_plus_args)> out_msg;
out_msg.info = msg_info;
out_msg.body = hdr_plus_args;
return build(out_msg).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
}

// Prepare message cell for external call (should later be signed by debot engine)
Expand All @@ -225,19 +194,11 @@ cell external_call_prepare_nosign(address addr, uint256 pubkey, Args... args) {
.import_fee = Evers{0}
};

using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
} else {
message_relaxed<decltype(hdr_plus_args)> out_msg;
out_msg.info = msg_info;
out_msg.body = hdr_plus_args;
return build(out_msg).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
}

// Prepare message cell for getter call (no signature/pubkey)
Expand All @@ -260,19 +221,11 @@ cell getter_call_prepare(address addr, Args... args) {
.import_fee = Evers{0}
};

using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
} else {
message_relaxed<decltype(hdr_plus_args)> out_msg;
out_msg.info = msg_info;
out_msg.body = hdr_plus_args;
return build(out_msg).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
}

// expire_val is used to place answer_id for debot external-in calls
Expand All @@ -297,19 +250,11 @@ cell external_call_prepare_with_pubkey(address addr, uint32 expire_val,
.import_fee = Evers{0}
};

using est_t = estimate_element<message<decltype(hdr_plus_args)>>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
} else {
message_relaxed<decltype(hdr_plus_args)> out_msg;
out_msg.info = msg_info;
out_msg.body = hdr_plus_args;
return build(out_msg).endc();
}
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
return build(out_msg).endc();
}

template<auto Func, class... Args>
Expand Down Expand Up @@ -353,7 +298,7 @@ void contract_deploy_impl(address addr, StateInit init,
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = out_info;
Either<StateInit, ref<StateInit>> init_ref = ref<StateInit>{init};
ref<StateInit> init_ref = ref<StateInit>{init};
out_msg.init = init_ref;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
tvm_sendmsg(build(out_msg).endc(), flags);
Expand All @@ -378,7 +323,7 @@ void contract_deploy_noop_impl(address addr, StateInit init,
auto chain_tup = make_chain_tuple(to_std_tuple(hdr));
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = out_info;
Either<StateInit, ref<StateInit>> init_ref = ref<StateInit>{init};
ref<StateInit> init_ref = ref<StateInit>{init};
out_msg.init = init_ref;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
tvm_sendmsg(build(out_msg).endc(), flags);
Expand Down Expand Up @@ -418,7 +363,7 @@ void contract_deploy_ext_noop_impl(address addr, uint256 pubkey,
auto chain_tup = make_chain_tuple(hdr_and_id);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
Either<StateInit, ref<StateInit>> init_ref = ref<StateInit>{init};
ref<StateInit> init_ref = ref<StateInit>{init};
out_msg.init = init_ref;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
tvm_sendmsg(build(out_msg).endc(), flags);
Expand Down Expand Up @@ -459,7 +404,7 @@ void contract_deploy_ext_impl(address addr, uint256 pubkey,
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = msg_info;
Either<StateInit, ref<StateInit>> init_ref = ref<StateInit>{init};
ref<StateInit> init_ref = ref<StateInit>{init};
out_msg.init = init_ref;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
tvm_sendmsg(build(out_msg).endc(), flags);
Expand All @@ -473,19 +418,13 @@ struct wait_call_result {
void await_suspend(std::experimental::coroutine_handle<>) const {}
__always_inline
RetT await_resume() const {
using est_t = estimate_element<RetT>;
if constexpr (est_t::max_bits > cell::max_bits || est_t::max_refs > cell::max_refs) {
parser p(__builtin_tvm_cast_to_slice(
temporary_data::getglob(global_id::coroutine_answer_slice)));
using LinearTup = decltype(make_chain_tuple<32, 0>(to_std_tuple(RetT{})));
// uncomment to print in remark:
//__reflect_echo<print_chain_tuple<LinearTup>().c_str()>{};
auto linear_tup = parse<LinearTup>(p);
return chain_fold<RetT>(linear_tup);
} else {
return parse<RetT>(__builtin_tvm_cast_to_slice(
temporary_data::getglob(global_id::coroutine_answer_slice)));
}
parser p(__builtin_tvm_cast_to_slice(
temporary_data::getglob(global_id::coroutine_answer_slice)));
using LinearTup = decltype(make_chain_tuple<32, 0>(to_std_tuple(RetT{})));
// uncomment to print in remark:
//__reflect_echo<print_chain_tuple<LinearTup>().c_str()>{};
auto linear_tup = parse<LinearTup>(p);
return chain_fold<RetT>(linear_tup);
}
};

Expand Down
5 changes: 3 additions & 2 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/emit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ void emit(Args... args) {
out_info.created_lt = 0;
out_info.created_at = 0;

message_relaxed<decltype(hdr_plus_args)> out_msg;
auto chain_tup = make_chain_tuple(hdr_plus_args);
message_relaxed<decltype(chain_tup)> out_msg;
out_msg.info = out_info;
out_msg.body = hdr_plus_args;
out_msg.body = ref<decltype(chain_tup)>{chain_tup};
tvm_sendmsg(build(out_msg).endc(), 0);
}

Expand Down
57 changes: 0 additions & 57 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/basics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,63 +620,6 @@ struct anydict {
};
using optcell = anydict;

template<class X>
struct EitherLeft {
bitconst<1, 0b0> tag;
X val;

DEFAULT_EQUAL(EitherLeft)
};
template<class Y>
struct EitherRight {
bitconst<1, 0b1> tag;
Y val;

DEFAULT_EQUAL(EitherRight)
};
template<typename X, typename Y>
struct Either {
using base_t = std::variant<EitherLeft<X>, EitherRight<Y>>;

Either() {}
Either(X left) : val_(EitherLeft<X>{ {}, left }) {}
Either(Y right) : val_(EitherRight<Y>{ {}, right }) {}

Either& operator=(X left) {
val_ = EitherLeft<X>{ {}, left };
return *this;
}
Either& operator=(Y right) {
val_ = EitherRight<Y>{ {}, right };
return *this;
}

bool operator == (const Either& v) const {
return val_ == v.val_;
}
template<class T>
__always_inline bool isa() const {
if constexpr (std::is_same_v<T, X>)
return std::holds_alternative<EitherLeft<X>>(val_);
else if constexpr (std::is_same_v<T, Y>)
return std::holds_alternative<EitherRight<Y>>(val_);
else
return false;
}
template<class T>
__always_inline T get() const {
if constexpr (std::is_same_v<T, X>)
return std::get<EitherLeft<X>>(val_).val;
else if constexpr (std::is_same_v<T, Y>)
return std::get<EitherRight<Y>>(val_).val;
else
static_assert(std::is_same_v<T, X> || std::is_same_v<T, Y>,
"bad get in Either variant");
}
base_t operator()() const { return val_; }
base_t val_;
};

template<typename _Tp>
struct ref {
_Tp operator()() const { return val_; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,6 @@ struct estimate_element<std::variant<Types...>> {
static constexpr unsigned min_refs = std::min({estimate_element<Types>::min_refs ... });
static constexpr unsigned max_refs = std::max({estimate_element<Types>::max_refs ... });
};
template<class X>
struct estimate_element<EitherLeft<X>> : detail::chain_to< to_std_tuple_t<EitherLeft<X>> > {};
template<class Y>
struct estimate_element<EitherRight<Y>> : detail::chain_to< to_std_tuple_t<EitherRight<Y>> > {};

template<class X, class Y>
struct estimate_element<Either<X, Y>> : detail::chain_to< to_std_tuple_t<Either<X, Y>> > {};

template<class _Tp>
struct estimate_element<lazy<_Tp>> : detail::chain_to<_Tp> {};
Expand Down
12 changes: 0 additions & 12 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/get_bitsize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ struct get_bitsize<std::variant<Types...>> {
static constexpr bool all_calculated = (get_bitsize<Types>::value && ...);
static constexpr unsigned value = all_calculated ? std::max({get_bitsize<Types>::value ... }) : 0;
};
template<class X>
struct get_bitsize<EitherLeft<X>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<EitherLeft<X>> >::value;
};
template<class Y>
struct get_bitsize<EitherRight<Y>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<EitherRight<Y>> >::value;
};
template<class X, class Y>
struct get_bitsize<Either<X, Y>> {
static constexpr unsigned value = get_bitsize< to_std_tuple_t<Either<X, Y>> >::value;
};
template<class T>
struct get_bitsize<lazy<T>> {
static constexpr unsigned value = get_bitsize<T>::value;
Expand Down
8 changes: 4 additions & 4 deletions llvm/projects/ton-compiler/cpp-sdk/tvm/schema/message.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ struct StateInit {
template<typename X>
struct message {
CommonMsgInfo info;
optional<Either<StateInit, ref<StateInit>>> init;
Either<X, ref<X>> body;
optional<ref<StateInit>> init;
optional<ref<X>> body;
};

template<typename X>
struct message_relaxed {
CommonMsgInfoRelaxed info;
optional<Either<StateInit, ref<StateInit>>> init;
Either<X, ref<X>> body;
optional<ref<StateInit>> init;
optional<ref<X>> body;
};

struct addr_std_fixed {
Expand Down
Loading