Merge pull request #11247 from ethereum/setValueForCurrentScopeHelper

Add SetValueForCurrentScope helper.
This commit is contained in:
Daniel Kirchner 2021-04-16 12:42:25 +02:00 committed by GitHub
commit f9b23ca845
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 15 deletions

View File

@ -133,4 +133,26 @@ private:
std::function<void(void)> m_f; std::function<void(void)> m_f;
}; };
/// RAII utility class that sets the value of a variable for the current scope and restores it to its old value
/// during its destructor.
template<typename V>
class ScopedSaveAndRestore
{
public:
explicit ScopedSaveAndRestore(V& _variable, V&& _value): m_variable(_variable), m_oldValue(std::move(_value))
{
std::swap(m_variable, m_oldValue);
}
ScopedSaveAndRestore(ScopedSaveAndRestore const&) = delete;
~ScopedSaveAndRestore() { std::swap(m_variable, m_oldValue); }
ScopedSaveAndRestore& operator=(ScopedSaveAndRestore const&) = delete;
private:
V& m_variable;
V m_oldValue;
};
template<typename V, typename... Args>
ScopedSaveAndRestore(V, Args...) -> ScopedSaveAndRestore<V>;
} }

View File

@ -156,16 +156,11 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
{ {
// Save all information. We might rather reinstantiate this class, // Save all information. We might rather reinstantiate this class,
// but this could be difficult if it is subclassed. // but this could be difficult if it is subclassed.
map<YulString, AssignedValue> value; ScopedSaveAndRestore valueResetter(m_value, {});
size_t loopDepth{0}; ScopedSaveAndRestore loopDepthResetter(m_loopDepth, 0u);
unordered_map<YulString, set<YulString>> references; ScopedSaveAndRestore referencesResetter(m_references, {});
unordered_map<YulString, YulString> storage; ScopedSaveAndRestore storageResetter(m_storage, {});
unordered_map<YulString, YulString> memory; ScopedSaveAndRestore memoryResetter(m_memory, {});
swap(m_value, value);
swap(m_loopDepth, loopDepth);
swap(m_references, references);
swap(m_storage, storage);
swap(m_memory, memory);
pushScope(true); pushScope(true);
for (auto const& parameter: _fun.parameters) for (auto const& parameter: _fun.parameters)
@ -182,11 +177,6 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
// statement. // statement.
popScope(); popScope();
swap(m_value, value);
swap(m_loopDepth, loopDepth);
swap(m_references, references);
swap(m_storage, storage);
swap(m_memory, memory);
} }
void DataFlowAnalyzer::operator()(ForLoop& _for) void DataFlowAnalyzer::operator()(ForLoop& _for)