|
| 1 | +/* |
| 2 | + This file is part of TON Blockchain Library. |
| 3 | +
|
| 4 | + TON Blockchain Library is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU Lesser General Public License as published by |
| 6 | + the Free Software Foundation, either version 2 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + TON Blockchain Library 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 Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public License |
| 15 | + along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include "src-file.h" |
| 20 | +#include "symtable.h" |
| 21 | +#include "td/utils/Status.h" |
| 22 | +#include <set> |
| 23 | +#include <string> |
| 24 | + |
| 25 | +namespace tolk { |
| 26 | + |
| 27 | +extern std::string tolk_version; |
| 28 | + |
| 29 | +class GlobalPragma { |
| 30 | + std::string name_; |
| 31 | + bool enabled_ = false; |
| 32 | + const char* deprecated_from_v_ = nullptr; |
| 33 | + |
| 34 | +public: |
| 35 | + explicit GlobalPragma(std::string name) : name_(std::move(name)) { } |
| 36 | + |
| 37 | + const std::string& name() const { return name_; } |
| 38 | + |
| 39 | + bool enabled() const { return enabled_; } |
| 40 | + void enable(SrcLocation loc); |
| 41 | + void always_on_and_deprecated(const char* deprecated_from_v); |
| 42 | +}; |
| 43 | + |
| 44 | +// CompilerSettings contains settings that can be passed via cmd line or (partially) wasm envelope. |
| 45 | +// They are filled once at start and are immutable since the compilation started. |
| 46 | +struct CompilerSettings { |
| 47 | + enum class FsReadCallbackKind { Realpath, ReadFile }; |
| 48 | + |
| 49 | + using FsReadCallback = std::function<td::Result<std::string>(FsReadCallbackKind, const char*)>; |
| 50 | + |
| 51 | + int verbosity = 0; |
| 52 | + int optimization_level = 2; |
| 53 | + bool stack_layout_comments = true; |
| 54 | + |
| 55 | + std::string entrypoint_filename; |
| 56 | + std::string output_filename; |
| 57 | + std::string boc_output_filename; |
| 58 | + std::string stdlib_filename; |
| 59 | + |
| 60 | + FsReadCallback read_callback; |
| 61 | +}; |
| 62 | + |
| 63 | +// CompilerState contains a mutable state that is changed while the compilation is going on. |
| 64 | +// It's a "global state" of all compilation. |
| 65 | +// Historically, in FunC, this global state was spread along many global C++ variables. |
| 66 | +// Now, no global C++ variables except `CompilerState G` are present. |
| 67 | +struct CompilerState { |
| 68 | + CompilerSettings settings; |
| 69 | + |
| 70 | + SymTable symbols; |
| 71 | + int scope_level = 0; |
| 72 | + SymDef* sym_def[SymTable::SIZE_PRIME + 1]{}; |
| 73 | + SymDef* global_sym_def[SymTable::SIZE_PRIME + 1]{}; |
| 74 | + std::vector<std::pair<int, SymDef>> symbol_stack; |
| 75 | + std::vector<SrcLocation> scope_opened_at; |
| 76 | + |
| 77 | + AllRegisteredSrcFiles all_src_files; |
| 78 | + |
| 79 | + int glob_func_cnt = 0, glob_var_cnt = 0, const_cnt = 0; |
| 80 | + std::vector<SymDef*> glob_func, glob_vars, glob_get_methods; |
| 81 | + std::set<std::string> prohibited_var_names; |
| 82 | + |
| 83 | + std::string generated_from; |
| 84 | + GlobalPragma pragma_allow_post_modification{"allow-post-modification"}; |
| 85 | + GlobalPragma pragma_compute_asm_ltr{"compute-asm-ltr"}; |
| 86 | + GlobalPragma pragma_remove_unused_functions{"remove-unused-functions"}; |
| 87 | + |
| 88 | + bool is_verbosity(int gt_eq) const { return settings.verbosity >= gt_eq; } |
| 89 | +}; |
| 90 | + |
| 91 | +extern CompilerState G; |
| 92 | + |
| 93 | +} // namespace tolk |
0 commit comments