Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_TORQUE_GLOBAL_CONTEXT_H_
6 : #define V8_TORQUE_GLOBAL_CONTEXT_H_
7 :
8 : #include <map>
9 :
10 : #include "src/torque/declarable.h"
11 : #include "src/torque/declarations.h"
12 : #include "src/torque/type-oracle.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 : namespace torque {
17 :
18 2 : class GlobalContext : public ContextualClass<GlobalContext> {
19 : public:
20 1 : explicit GlobalContext(Ast ast) : verbose_(false), ast_(std::move(ast)) {
21 1 : CurrentScope::Scope current_scope(nullptr);
22 : CurrentSourcePosition::Scope current_source_position(
23 1 : SourcePosition{CurrentSourceFile::Get(), {-1, -1}, {-1, -1}});
24 : default_namespace_ =
25 2 : RegisterDeclarable(base::make_unique<Namespace>(kBaseNamespaceName));
26 1 : }
27 142156 : static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
28 : template <class T>
29 6097 : T* RegisterDeclarable(std::unique_ptr<T> d) {
30 : T* ptr = d.get();
31 12299 : declarables_.push_back(std::move(d));
32 6097 : return ptr;
33 : }
34 :
35 : static const std::vector<std::unique_ptr<Declarable>>& AllDeclarables() {
36 : return Get().declarables_;
37 : }
38 :
39 25 : static const std::vector<Namespace*> GetNamespaces() {
40 : std::vector<Namespace*> result;
41 44748 : for (auto& declarable : AllDeclarables()) {
42 44698 : if (Namespace* n = Namespace::DynamicCast(declarable.get())) {
43 575 : result.push_back(n);
44 : }
45 : }
46 25 : return result;
47 : }
48 :
49 19 : static void RegisterClass(const std::string& name, ClassType* new_class) {
50 19 : Get().classes_[name] = new_class;
51 19 : }
52 :
53 : static const std::map<std::string, ClassType*>& GetClasses() {
54 : return Get().classes_;
55 : }
56 :
57 15 : static void AddCppInclude(std::string include_path) {
58 15 : Get().cpp_includes_.push_back(std::move(include_path));
59 15 : }
60 : static const std::vector<std::string>& CppIncludes() {
61 : return Get().cpp_includes_;
62 : }
63 :
64 0 : static void SetVerbose() { Get().verbose_ = true; }
65 6796 : static bool verbose() { return Get().verbose_; }
66 : static Ast* ast() { return &Get().ast_; }
67 :
68 : private:
69 : bool verbose_;
70 : Namespace* default_namespace_;
71 : Ast ast_;
72 : std::vector<std::unique_ptr<Declarable>> declarables_;
73 : std::vector<std::string> cpp_includes_;
74 : std::map<std::string, ClassType*> classes_;
75 : };
76 :
77 : template <class T>
78 6201 : T* RegisterDeclarable(std::unique_ptr<T> d) {
79 12297 : return GlobalContext::Get().RegisterDeclarable(std::move(d));
80 : }
81 :
82 : } // namespace torque
83 : } // namespace internal
84 : } // namespace v8
85 :
86 : #endif // V8_TORQUE_GLOBAL_CONTEXT_H_
|