forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallgraph.h
142 lines (126 loc) · 4.22 KB
/
Callgraph.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
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/>.
*/
#pragma once
#include <libdevcore/CommonData.h>
#include <set>
namespace dev
{
namespace solidity
{
class Declaration;
class VariableDeclaration;
class ASTNode;
struct CallgraphNode
{
/// Variable declarations this node writes to, unknown if it contains a nullptr.
/// Note that due to aliasing, this might be incorrect for reference types.
std::set<VariableDeclaration const*> writes;
/// Variable declaration this node reads from, unknown if it contains a nullptr.
/// Note that due to aliasing, this might be incorrect for reference types.
std::set<VariableDeclaration const*> reads;
/// Roughly, a node has side effects (at statement level) if it cannot be removed
/// from the code without changing semantics. In particular, if a node
/// performs any modifications to any state, it has side effects.
/// Assignments to local variables count as side-effects,
/// usage of temporary memory does not count as a side-effect (unless this memory
/// is used elsewhere).
/// If any modification to the state is captured only by the value of the
/// expression, it does not have side effects.
bool hasSideEffects = false;
/// Reads from storage (of any account).
bool readsStorage = false;
/// Writes to storage (of any account).
bool writesStorage = false;
bool writesLogs = false;
/// Reads anything from the environment inculding:
/// - balances
/// - coinbase (block.coinbase)
/// - timestamp (block.timestamp / now)
/// - difficulty (block.difficulty)
/// - block number (block.number)
/// - gas limit (block.gaslimit)
/// - origin (tx.origin)
/// - gas (tx.gas)
/// - gas price (tx.gasprice)
/// - sender (msg.sender)
///
/// but excluding:
/// - input (msg.data)
/// - function signature (msg.sig)
/// - value transferred (msg.value)
bool readsEnvironment = false;
/// Whether it might performs an external call or create.
bool calls = false;
/// Whether it might send Ether somewhere.
bool sendsValue = false;
bool selfdestructs = false;
bool canBeUsedInPureFunction() const
{
return canBeUsedInViewFunction() && !readsStorage && !readsEnvironment;
}
bool canBeUsedInViewFunction() const
{
return !writesStorage && !writesLogs && !sendsValue && !selfdestructs;
}
/// TODO this is not assigned yet
/// The outgoing arches of the call graph node.
/// Can contain a nullptr, which means "unknown".
std::set<ASTNode const*> nextNodes;
/// TODO this is not assigned yet
/// Statements that are visited during execution but are not part of the
/// syntactic area of the current node.
/// Can contain a nullptr, which means "unknown".
std::set<ASTNode const*> calledToNodes;
CallgraphNode& operator+=(CallgraphNode const& _other)
{
writes += _other.writes;
reads += _other.reads;
if (_other.hasSideEffects)
hasSideEffects = true;
if (_other.readsStorage)
readsStorage = true;
if (_other.writesStorage)
writesStorage = true;
if (_other.writesLogs)
writesLogs = true;
if (_other.readsEnvironment)
readsEnvironment = true;
if (_other.calls)
calls = true;
if (_other.sendsValue)
sendsValue = true;
if (_other.selfdestructs)
selfdestructs = true;
calledToNodes += _other.calledToNodes;
return *this;
}
/// @returns a call graph node which assumes anything could happen.
static CallgraphNode anything()
{
CallgraphNode node;
node.reads.insert(nullptr);
node.writes.insert(nullptr);
node.hasSideEffects = true;
node.readsStorage = true;
node.writesStorage = true;
node.writesLogs = true;
node.readsEnvironment = true;
node.calls = true;
node.sendsValue = true;
node.selfdestructs = true;
return node;
}
};
}
}