global-context.h 3.5 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
#include <map>
9
#include <memory>
10

11
#include "src/common/globals.h"
12 13
#include "src/torque/ast.h"
#include "src/torque/contextual.h"
14 15 16 17 18 19
#include "src/torque/declarable.h"

namespace v8 {
namespace internal {
namespace torque {

20
class GlobalContext : public ContextualClass<GlobalContext> {
21
 public:
22 23
  GlobalContext(GlobalContext&&) V8_NOEXCEPT = default;
  GlobalContext& operator=(GlobalContext&&) V8_NOEXCEPT = default;
24 25
  explicit GlobalContext(Ast ast);

26
  static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
27 28 29 30 31
  template <class T>
  T* RegisterDeclarable(std::unique_ptr<T> d) {
    T* ptr = d.get();
    declarables_.push_back(std::move(d));
    return ptr;
32
  }
33

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

38
  static void AddCppInclude(std::string include_path) {
39
    Get().cpp_includes_.insert(std::move(include_path));
40
  }
41
  static const std::set<std::string>& CppIncludes() {
42 43 44
    return Get().cpp_includes_;
  }

45 46 47 48 49 50
  static void SetCollectLanguageServerData() {
    Get().collect_language_server_data_ = true;
  }
  static bool collect_language_server_data() {
    return Get().collect_language_server_data_;
  }
51 52 53 54 55 56
  static void SetForceAssertStatements() {
    Get().force_assert_statements_ = true;
  }
  static bool force_assert_statements() {
    return Get().force_assert_statements_;
  }
57
  static Ast* ast() { return &Get().ast_; }
58 59 60
  static std::string MakeUniqueName(const std::string& base) {
    return base + "_" + std::to_string(Get().fresh_ids_[base]++);
  }
61

62 63 64 65 66 67 68 69
  struct PerFileStreams {
    std::stringstream csa_headerfile;
    std::stringstream csa_ccfile;
  };
  static PerFileStreams& GeneratedPerFile(SourceId file) {
    return Get().generated_per_file_[file];
  }

70 71 72 73 74 75 76 77
  static void SetInstanceTypesInitialized() {
    DCHECK(!Get().instance_types_initialized_);
    Get().instance_types_initialized_ = true;
  }
  static bool IsInstanceTypesInitialized() {
    return Get().instance_types_initialized_;
  }

78
 private:
79
  bool collect_language_server_data_;
80
  bool force_assert_statements_;
81
  Namespace* default_namespace_;
82
  Ast ast_;
83
  std::vector<std::unique_ptr<Declarable>> declarables_;
84
  std::set<std::string> cpp_includes_;
85
  std::map<SourceId, PerFileStreams> generated_per_file_;
86
  std::map<std::string, size_t> fresh_ids_;
87
  bool instance_types_initialized_ = false;
88 89

  friend class LanguageServerData;
90 91
};

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

97 98 99 100
class TargetArchitecture : public ContextualClass<TargetArchitecture> {
 public:
  explicit TargetArchitecture(bool force_32bit);

101 102
  static size_t TaggedSize() { return Get().tagged_size_; }
  static size_t RawPtrSize() { return Get().raw_ptr_size_; }
103
  static size_t ExternalPointerSize() { return Get().external_ptr_size_; }
104
  static size_t MaxHeapAlignment() { return TaggedSize(); }
105
  static bool ArePointersCompressed() { return TaggedSize() < RawPtrSize(); }
106
  static int SmiTagAndShiftSize() { return Get().smi_tag_and_shift_size_; }
107 108

 private:
109 110
  const size_t tagged_size_;
  const size_t raw_ptr_size_;
111
  const int smi_tag_and_shift_size_;
112
  const size_t external_ptr_size_;
113 114
};

115 116 117 118 119
}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_GLOBAL_CONTEXT_H_