aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/analysis/PostTypeChecker.cpp
blob: 19d0b708db4e3355d13578aa5fde46125061cf20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <libsolidity/analysis/PostTypeChecker.h>
#include <libsolidity/ast/AST.h>
#include <libsolidity/analysis/SemVerHandler.h>
#include <libsolidity/interface/ErrorReporter.h>
#include <libsolidity/interface/Version.h>

#include <libdevcore/Algorithms.h>

#include <boost/range/adaptor/map.hpp>

#include <memory>

using namespace std;
using namespace dev;
using namespace dev::solidity;


bool PostTypeChecker::check(ASTNode const& _astRoot)
{
    _astRoot.accept(*this);
    return Error::containsOnlyWarnings(m_errorReporter.errors());
}

bool PostTypeChecker::visit(ContractDefinition const&)
{
    solAssert(!m_currentConstVariable, "");
    solAssert(m_constVariableDependencies.empty(), "");
    return true;
}

void PostTypeChecker::endVisit(ContractDefinition const&)
{
    solAssert(!m_currentConstVariable, "");
    for (auto declaration: m_constVariables)
        if (auto identifier = findCycle(*declaration))
            m_errorReporter.typeError(
                declaration->location(),
                "The value of the constant " + declaration->name() +
                " has a cyclic dependency via " + identifier->name() + "."
            );

    m_constVariables.clear();
    m_constVariableDependencies.clear();
}

bool PostTypeChecker::visit(VariableDeclaration const& _variable)
{
    solAssert(!m_currentConstVariable, "");
    if (_variable.isConstant())
    {
        m_currentConstVariable = &_variable;
        m_constVariables.push_back(&_variable);
    }
    return true;
}

void PostTypeChecker::endVisit(VariableDeclaration const& _variable)
{
    if (_variable.isConstant())
    {
        solAssert(m_currentConstVariable == &_variable, "");
        m_currentConstVariable = nullptr;
    }
}

bool PostTypeChecker::visit(Identifier const& _identifier)
{
    if (m_currentConstVariable)
        if (auto var = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration))
            if (var->isConstant())
                m_constVariableDependencies[m_currentConstVariable].insert(var);
    return true;
}

VariableDeclaration const* PostTypeChecker::findCycle(VariableDeclaration const& _startingFrom)
{
    auto visitor = [&](VariableDeclaration const& _variable, CycleDetector<VariableDeclaration>& _cycleDetector)
    {
        // Iterating through the dependencies needs to be deterministic and thus cannot
        // depend on the memory layout.
        // Because of that, we sort by AST node id.
        vector<VariableDeclaration const*> dependencies(
            m_constVariableDependencies[&_variable].begin(),
            m_constVariableDependencies[&_variable].end()
        );
        sort(dependencies.begin(), dependencies.end(), [](VariableDeclaration const* _a, VariableDeclaration const* _b) -> bool
        {
            return _a->id() < _b->id();
        });
        for (auto v: dependencies)
            if (_cycleDetector.run(*v))
                return;
    };
    return CycleDetector<VariableDeclaration>(visitor).run(_startingFrom);
}