Add scoped variables

This commit is contained in:
Bhargava Shastry 2021-05-05 18:40:26 +02:00
parent 0e15a794d8
commit a9a80213b3
2 changed files with 23 additions and 8 deletions

View File

@ -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:
@ -649,7 +659,7 @@ string FunctionCallGenerator::lhs(vector<pair<SolidityTypePtr, string>> _functio
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

View File

@ -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;