Add ScopedSaveAndRestore helper.

This commit is contained in:
Daniel Kirchner 2021-04-13 13:20:59 +02:00
parent 0289994da5
commit afae46dcb5
2 changed files with 27 additions and 15 deletions

View File

@ -133,4 +133,26 @@ private:
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,
// but this could be difficult if it is subclassed.
map<YulString, AssignedValue> value;
size_t loopDepth{0};
unordered_map<YulString, set<YulString>> references;
unordered_map<YulString, YulString> storage;
unordered_map<YulString, YulString> memory;
swap(m_value, value);
swap(m_loopDepth, loopDepth);
swap(m_references, references);
swap(m_storage, storage);
swap(m_memory, memory);
ScopedSaveAndRestore valueResetter(m_value, {});
ScopedSaveAndRestore loopDepthResetter(m_loopDepth, 0u);
ScopedSaveAndRestore referencesResetter(m_references, {});
ScopedSaveAndRestore storageResetter(m_storage, {});
ScopedSaveAndRestore memoryResetter(m_memory, {});
pushScope(true);
for (auto const& parameter: _fun.parameters)
@ -182,11 +177,6 @@ void DataFlowAnalyzer::operator()(FunctionDefinition& _fun)
// statement.
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)