summaryrefslogtreecommitdiff
path: root/deps/v8/src/torque/global-context.h
blob: 949362ca1c4d5ad399b25395ddad926ca2bc15ad (plain)
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
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TORQUE_GLOBAL_CONTEXT_H_
#define V8_TORQUE_GLOBAL_CONTEXT_H_

#include <map>

#include "src/torque/declarable.h"
#include "src/torque/declarations.h"
#include "src/torque/type-oracle.h"

namespace v8 {
namespace internal {
namespace torque {

class GlobalContext : public ContextualClass<GlobalContext> {
 public:
  explicit GlobalContext(Ast ast) : verbose_(false), ast_(std::move(ast)) {
    CurrentScope::Scope current_scope(nullptr);
    CurrentSourcePosition::Scope current_source_position(
        SourcePosition{CurrentSourceFile::Get(), -1, -1});
    default_namespace_ =
        RegisterDeclarable(base::make_unique<Namespace>("base"));
  }
  static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
  template <class T>
  T* RegisterDeclarable(std::unique_ptr<T> d) {
    T* ptr = d.get();
    declarables_.push_back(std::move(d));
    return ptr;
  }

  static const std::vector<std::unique_ptr<Declarable>>& AllDeclarables() {
    return Get().declarables_;
  }

  static const std::vector<Namespace*> GetNamespaces() {
    std::vector<Namespace*> result;
    for (auto& declarable : AllDeclarables()) {
      if (Namespace* n = Namespace::DynamicCast(declarable.get())) {
        result.push_back(n);
      }
    }
    return result;
  }

  static void RegisterClass(const std::string& name,
                            const ClassType* new_class) {
    Get().classes_[name] = new_class;
  }

  static const std::map<std::string, const ClassType*>& GetClasses() {
    return Get().classes_;
  }

  static void AddCppInclude(std::string include_path) {
    Get().cpp_includes_.push_back(std::move(include_path));
  }
  static const std::vector<std::string>& CppIncludes() {
    return Get().cpp_includes_;
  }

  static void SetVerbose() { Get().verbose_ = true; }
  static bool verbose() { return Get().verbose_; }
  static Ast* ast() { return &Get().ast_; }

 private:
  bool verbose_;
  Namespace* default_namespace_;
  Ast ast_;
  std::vector<std::unique_ptr<Declarable>> declarables_;
  std::vector<std::string> cpp_includes_;
  std::map<std::string, const ClassType*> classes_;
};

template <class T>
T* RegisterDeclarable(std::unique_ptr<T> d) {
  return GlobalContext::Get().RegisterDeclarable(std::move(d));
}

}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_GLOBAL_CONTEXT_H_