aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/inlineasm/AsmScope.h
blob: 658480187559d044e55b3a2d592c045efea03690 (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
/*
    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/>.
*/
/**
 * Scopes for identifiers.
 */

#pragma once

#include <libsolidity/interface/Exceptions.h>

#include <libyul/YulString.h>

#include <libdevcore/Visitor.h>

#include <boost/variant.hpp>
#include <boost/optional.hpp>

#include <functional>
#include <memory>

namespace dev
{
namespace solidity
{
namespace assembly
{

struct Scope
{
    using YulType = yul::YulString;
    using LabelID = size_t;

    struct Variable { YulType type; };
    struct Label { };
    struct Function
    {
        std::vector<YulType> arguments;
        std::vector<YulType> returns;
    };

    using Identifier = boost::variant<Variable, Label, Function>;
    using Visitor = GenericVisitor<Variable const, Label const, Function const>;
    using NonconstVisitor = GenericVisitor<Variable, Label, Function>;

    bool registerVariable(yul::YulString _name, YulType const& _type);
    bool registerLabel(yul::YulString _name);
    bool registerFunction(
        yul::YulString _name,
        std::vector<YulType> const& _arguments,
        std::vector<YulType> const& _returns
    );

    /// Looks up the identifier in this or super scopes and returns a valid pointer if found
    /// or a nullptr if not found. Variable lookups up across function boundaries will fail, as
    /// will any lookups across assembly boundaries.
    /// The pointer will be invalidated if the scope is modified.
    /// @param _crossedFunction if true, we already crossed a function boundary during recursive lookup
    Identifier* lookup(yul::YulString _name);
    /// Looks up the identifier in this and super scopes (will not find variables across function
    /// boundaries and generally stops at assembly boundaries) and calls the visitor, returns
    /// false if not found.
    template <class V>
    bool lookup(yul::YulString _name, V const& _visitor)
    {
        if (Identifier* id = lookup(_name))
        {
            boost::apply_visitor(_visitor, *id);
            return true;
        }
        else
            return false;
    }
    /// @returns true if the name exists in this scope or in super scopes (also searches
    /// across function and assembly boundaries).
    bool exists(yul::YulString _name) const;

    /// @returns the number of variables directly registered inside the scope.
    size_t numberOfVariables() const;
    /// @returns true if this scope is inside a function.
    bool insideFunction() const;

    Scope* superScope = nullptr;
    /// If true, variables from the super scope are not visible here (other identifiers are),
    /// but they are still taken into account to prevent shadowing.
    bool functionScope = false;
    std::map<yul::YulString, Identifier> identifiers;
};

}
}
}