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  * Parser for Yul code and data object container.
20  */
21 
22 #pragma once
23 
24 #include <libyul/YulString.h>
25 #include <libyul/Object.h>
26 #include <libyul/Dialect.h>
27 
28 #include <liblangutil/ErrorReporter.h>
29 #include <liblangutil/ParserBase.h>
30 
31 #include <libsolutil/Common.h>
32 
33 #include <memory>
34 
35 namespace solidity::langutil
36 {
37 class Scanner;
38 }
39 
40 namespace solidity::yul
41 {
42 
43 /**
44  * Yul object parser. Invokes the inline assembly parser.
45  */
46 class ObjectParser: public langutil::ParserBase
47 {
48 public:
ObjectParser(langutil::ErrorReporter & _errorReporter,Dialect const & _dialect)49 	explicit ObjectParser(langutil::ErrorReporter& _errorReporter, Dialect const& _dialect):
50 		ParserBase(_errorReporter), m_dialect(_dialect) {}
51 
52 	/// Parses a Yul object.
53 	/// Falls back to code-only parsing if the source starts with `{`.
54 	/// @param _reuseScanner if true, do check for end of input after the last `}`.
55 	/// @returns an empty shared pointer on error.
56 	std::shared_ptr<Object> parse(std::shared_ptr<langutil::Scanner> const& _scanner, bool _reuseScanner);
57 
58 private:
59 	std::optional<SourceNameMap> tryParseSourceNameMapping() const;
60 	std::shared_ptr<Object> parseObject(Object* _containingObject = nullptr);
61 	std::shared_ptr<Block> parseCode(std::optional<SourceNameMap> _sourceNames);
62 	std::shared_ptr<Block> parseBlock(std::optional<SourceNameMap> _sourceNames);
63 	void parseData(Object& _containingObject);
64 
65 	/// Tries to parse a name that is non-empty and unique inside the containing object.
66 	YulString parseUniqueName(Object const* _containingObject);
67 	void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
68 
69 	Dialect const& m_dialect;
70 };
71 
72 }
73