1 /*
2 	This file is part of solidity.
3 
4 	solidity is free software: you can redistribute it and/or modify
5 	it under the terms of the GNU General Public License as published by
6 	the Free Software Foundation, either version 3 of the License, or
7 	(at your option) any later version.
8 
9 	solidity is distributed in the hope that it will be useful,
10 	but WITHOUT ANY WARRANTY; without even the implied warranty of
11 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 	GNU General Public License for more details.
13 
14 	You should have received a copy of the GNU General Public License
15 	along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 // SPDX-License-Identifier: GPL-3.0
18 
19 /// Data structure representing a function call graph.
20 
21 #pragma once
22 
23 #include <libsolidity/ast/AST.h>
24 
25 #include <map>
26 #include <set>
27 #include <variant>
28 
29 namespace solidity::frontend
30 {
31 
32 /**
33  * Function call graph for a contract at the granularity of Solidity functions and modifiers.
34  * The graph can represent the situation either at contract creation or after deployment.
35  * The graph does not preserve temporal relations between calls - edges coming out of the same node
36  * show which calls were performed but not in what order.
37  *
38  * Stores also extra information about contracts that can be created and events that can be emitted
39  * from any of the functions in it.
40  */
41 struct CallGraph
42 {
43 	enum class SpecialNode
44 	{
45 		InternalDispatch,
46 		Entry,
47 	};
48 
49 	using Node = std::variant<CallableDeclaration const*, SpecialNode>;
50 
51 	struct CompareByID
52 	{
53 		using is_transparent = void;
54 		bool operator()(Node const& _lhs, Node const& _rhs) const;
55 		bool operator()(Node const& _lhs, int64_t _rhs) const;
56 		bool operator()(int64_t _lhs, Node const& _rhs) const;
57 	};
58 
59 	/// Graph edges. Edges are directed and lead from the caller to the callee.
60 	/// The map contains a key for every possible caller, even if does not actually perform
61 	/// any calls.
62 	std::map<Node, std::set<Node, CompareByID>, CompareByID> edges;
63 
64 	/// Contracts that need to be compiled before this one can be compiled.
65 	/// The value is the ast node that created the dependency.
66 	std::map<ContractDefinition const*, ASTNode const*, ASTCompareByID<ContractDefinition>> bytecodeDependency;
67 
68 	/// Events that may get emitted by functions present in the graph.
69 	std::set<EventDefinition const*, ASTNode::CompareByID> emittedEvents;
70 
71 	/// Errors that are used by functions present in the graph.
72 	std::set<ErrorDefinition const*, ASTNode::CompareByID> usedErrors;
73 };
74 
75 }
76