global-context.h 3 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
  GlobalContext(GlobalContext&&) V8_NOEXCEPT = default;
  GlobalContext& operator=(GlobalContext&&) V8_NOEXCEPT = default;
22
  explicit GlobalContext(Ast ast)
23
      : collect_language_server_data_(false),
24
        force_assert_statements_(false),
25
        ast_(std::move(ast)) {
26 27
    CurrentScope::Scope current_scope(nullptr);
    CurrentSourcePosition::Scope current_source_position(
28
        SourcePosition{CurrentSourceFile::Get(), {-1, -1}, {-1, -1}});
29
    default_namespace_ =
30
        RegisterDeclarable(base::make_unique<Namespace>(kBaseNamespaceName));
31
  }
32
  static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
33 34 35 36 37
  template <class T>
  T* RegisterDeclarable(std::unique_ptr<T> d) {
    T* ptr = d.get();
    declarables_.push_back(std::move(d));
    return ptr;
38
  }
39

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

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

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

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

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

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 76 77 78 79 80
  static void SetForceAssertStatements() {
    Get().force_assert_statements_ = true;
  }
  static bool force_assert_statements() {
    return Get().force_assert_statements_;
  }
81
  static Ast* ast() { return &Get().ast_; }
82 83

 private:
84
  bool collect_language_server_data_;
85
  bool force_assert_statements_;
86
  Namespace* default_namespace_;
87
  Ast ast_;
88
  std::vector<std::unique_ptr<Declarable>> declarables_;
89
  std::vector<std::string> cpp_includes_;
90
  std::map<std::string, ClassType*> classes_;
91 92

  friend class LanguageServerData;
93 94
};

95 96 97 98
template <class T>
T* RegisterDeclarable(std::unique_ptr<T> d) {
  return GlobalContext::Get().RegisterDeclarable(std::move(d));
}
99 100 101 102 103 104

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

#endif  // V8_TORQUE_GLOBAL_CONTEXT_H_