mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Add scoped variables
This commit is contained in:
parent
0e15a794d8
commit
a9a80213b3
@ -359,9 +359,15 @@ string BlockStmtGenerator::visit()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
block << indentation() + "{\n";
|
block << indentation() + "{\n";
|
||||||
|
|
||||||
|
// Create blockscope inside current function state
|
||||||
|
state->currentFunctionState()->scopes.push_back(
|
||||||
|
make_shared<BlockScope>()
|
||||||
|
);
|
||||||
state->indent();
|
state->indent();
|
||||||
block << visitChildren();
|
block << visitChildren();
|
||||||
state->unindent();
|
state->unindent();
|
||||||
|
state->currentFunctionState()->scopes.pop_back();
|
||||||
block << indentation() << "}\n";
|
block << indentation() << "}\n";
|
||||||
return block.str();
|
return block.str();
|
||||||
}
|
}
|
||||||
@ -434,6 +440,10 @@ pair<SolidityTypePtr, string> ExpressionGenerator::randomLValueExpression()
|
|||||||
liveVariables += state->currentFunctionState()->outputs |
|
liveVariables += state->currentFunctionState()->outputs |
|
||||||
ranges::views::transform([](auto& _item) { return _item; }) |
|
ranges::views::transform([](auto& _item) { return _item; }) |
|
||||||
ranges::to<vector<pair<SolidityTypePtr, string>>>();
|
ranges::to<vector<pair<SolidityTypePtr, string>>>();
|
||||||
|
for (auto const& scope: state->currentFunctionState()->scopes)
|
||||||
|
liveVariables += scope->variables |
|
||||||
|
ranges::views::transform([](auto& _item) { return _item; }) |
|
||||||
|
ranges::to<vector<pair<SolidityTypePtr, string>>>();
|
||||||
return liveVariables[state->uRandDist->distributionOneToN(liveVariables.size()) - 1];
|
return liveVariables[state->uRandDist->distributionOneToN(liveVariables.size()) - 1];
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@ -638,18 +648,18 @@ string FunctionCallGenerator::lhs(vector<pair<SolidityTypePtr, string>> _functio
|
|||||||
if (useExistingVars)
|
if (useExistingVars)
|
||||||
{
|
{
|
||||||
auto vars = assignToVars |
|
auto vars = assignToVars |
|
||||||
ranges::views::transform([](auto const& _item) { return _item.second.value().second; }) |
|
ranges::views::transform([](auto const& _item) { return _item.second.value().second; }) |
|
||||||
ranges::to<vector<string>>();
|
ranges::to<vector<string>>();
|
||||||
callStmtLhs << "("
|
callStmtLhs << "("
|
||||||
<< boost::algorithm::join(vars, ",")
|
<< boost::algorithm::join(vars, ",")
|
||||||
<< ") = ";
|
<< ") = ";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto newVars = _functionReturnTypeNames |
|
auto newVars = _functionReturnTypeNames |
|
||||||
ranges::views::transform([&](auto const& _item) -> string {
|
ranges::views::transform([&](auto const& _item) -> string {
|
||||||
state->currentFunctionState()->addLocal(_item.first);
|
state->currentFunctionState()->addLocal(_item.first);
|
||||||
string varName = state->currentFunctionState()->locals.back().second;
|
string varName = state->currentFunctionState()->scopes.back()->variables.back().second;
|
||||||
return std::visit(
|
return std::visit(
|
||||||
GenericVisitor{[](auto const& _it) { return _it->toString(); }},
|
GenericVisitor{[](auto const& _it) { return _it->toString(); }},
|
||||||
_item.first
|
_item.first
|
||||||
|
@ -418,6 +418,11 @@ struct SourceState
|
|||||||
std::string sourceName;
|
std::string sourceName;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct BlockScope
|
||||||
|
{
|
||||||
|
std::vector<std::pair<SolidityTypePtr, std::string>> variables;
|
||||||
|
};
|
||||||
|
|
||||||
struct FunctionState
|
struct FunctionState
|
||||||
{
|
{
|
||||||
enum class Params
|
enum class Params
|
||||||
@ -437,7 +442,7 @@ struct FunctionState
|
|||||||
{
|
{
|
||||||
inputs.clear();
|
inputs.clear();
|
||||||
outputs.clear();
|
outputs.clear();
|
||||||
locals.clear();
|
scopes.clear();
|
||||||
}
|
}
|
||||||
void addInput(SolidityTypePtr _input)
|
void addInput(SolidityTypePtr _input)
|
||||||
{
|
{
|
||||||
@ -451,13 +456,13 @@ struct FunctionState
|
|||||||
}
|
}
|
||||||
void addLocal(SolidityTypePtr _local)
|
void addLocal(SolidityTypePtr _local)
|
||||||
{
|
{
|
||||||
locals.emplace_back(_local, "l" + std::to_string(numLocals++));
|
scopes.back()->variables.emplace_back(_local, "l" + std::to_string(numLocals++));
|
||||||
}
|
}
|
||||||
std::string params(Params _p);
|
std::string params(Params _p);
|
||||||
|
|
||||||
std::vector<std::pair<SolidityTypePtr, std::string>> inputs;
|
std::vector<std::pair<SolidityTypePtr, std::string>> inputs;
|
||||||
std::vector<std::pair<SolidityTypePtr, std::string>> outputs;
|
std::vector<std::pair<SolidityTypePtr, std::string>> outputs;
|
||||||
std::vector<std::pair<SolidityTypePtr, std::string>> locals;
|
std::vector<std::shared_ptr<BlockScope>> scopes;
|
||||||
std::shared_ptr<FunctionType> type;
|
std::shared_ptr<FunctionType> type;
|
||||||
unsigned numInputs;
|
unsigned numInputs;
|
||||||
unsigned numOutpus;
|
unsigned numOutpus;
|
||||||
|
Loading…
Reference in New Issue
Block a user