global-context.h 2.75 KB
Newer Older
1 2 3 4 5 6 7
// 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_

8 9
#include <map>

10
#include "src/torque/declarable.h"
11
#include "src/torque/declarations.h"
12 13 14 15 16 17
#include "src/torque/type-oracle.h"

namespace v8 {
namespace internal {
namespace torque {

18
class GlobalContext : public ContextualClass<GlobalContext> {
19
 public:
20 21 22 23
  explicit GlobalContext(Ast ast)
      : verbose_(false),
        collect_language_server_data_(false),
        ast_(std::move(ast)) {
24 25
    CurrentScope::Scope current_scope(nullptr);
    CurrentSourcePosition::Scope current_source_position(
26
        SourcePosition{CurrentSourceFile::Get(), {-1, -1}, {-1, -1}});
27
    default_namespace_ =
28
        RegisterDeclarable(base::make_unique<Namespace>(kBaseNamespaceName));
29
  }
30
  static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
31 32 33 34 35
  template <class T>
  T* RegisterDeclarable(std::unique_ptr<T> d) {
    T* ptr = d.get();
    declarables_.push_back(std::move(d));
    return ptr;
36
  }
37

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

42 43
  static const std::vector<Namespace*> GetNamespaces() {
    std::vector<Namespace*> result;
44
    for (auto& declarable : AllDeclarables()) {
45 46
      if (Namespace* n = Namespace::DynamicCast(declarable.get())) {
        result.push_back(n);
47 48 49 50
      }
    }
    return result;
  }
51

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

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

60 61 62 63 64 65 66
  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_;
  }

67 68
  static void SetVerbose() { Get().verbose_ = true; }
  static bool verbose() { return Get().verbose_; }
69 70 71 72 73 74
  static void SetCollectLanguageServerData() {
    Get().collect_language_server_data_ = true;
  }
  static bool collect_language_server_data() {
    return Get().collect_language_server_data_;
  }
75
  static Ast* ast() { return &Get().ast_; }
76 77 78

 private:
  bool verbose_;
79
  bool collect_language_server_data_;
80
  Namespace* default_namespace_;
81
  Ast ast_;
82
  std::vector<std::unique_ptr<Declarable>> declarables_;
83
  std::vector<std::string> cpp_includes_;
84
  std::map<std::string, ClassType*> classes_;
85 86
};

87 88 89 90
template <class T>
T* RegisterDeclarable(std::unique_ptr<T> d) {
  return GlobalContext::Get().RegisterDeclarable(std::move(d));
}
91 92 93 94 95 96

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

#endif  // V8_TORQUE_GLOBAL_CONTEXT_H_