• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <memory>
10 
11 #include "src/common/globals.h"
12 #include "src/torque/ast.h"
13 #include "src/torque/contextual.h"
14 #include "src/torque/cpp-builder.h"
15 #include "src/torque/declarable.h"
16 
17 namespace v8 {
18 namespace internal {
19 namespace torque {
20 
21 class GlobalContext : public ContextualClass<GlobalContext> {
22  public:
23   GlobalContext(GlobalContext&&) V8_NOEXCEPT = default;
24   GlobalContext& operator=(GlobalContext&&) V8_NOEXCEPT = default;
25   explicit GlobalContext(Ast ast);
26 
GetDefaultNamespace()27   static Namespace* GetDefaultNamespace() { return Get().default_namespace_; }
28   template <class T>
RegisterDeclarable(std::unique_ptr<T> d)29   T* RegisterDeclarable(std::unique_ptr<T> d) {
30     T* ptr = d.get();
31     declarables_.push_back(std::move(d));
32     return ptr;
33   }
34 
AllDeclarables()35   static const std::vector<std::unique_ptr<Declarable>>& AllDeclarables() {
36     return Get().declarables_;
37   }
38 
AddCppInclude(std::string include_path)39   static void AddCppInclude(std::string include_path) {
40     Get().cpp_includes_.insert(std::move(include_path));
41   }
CppIncludes()42   static const std::set<std::string>& CppIncludes() {
43     return Get().cpp_includes_;
44   }
45 
SetCollectLanguageServerData()46   static void SetCollectLanguageServerData() {
47     Get().collect_language_server_data_ = true;
48   }
collect_language_server_data()49   static bool collect_language_server_data() {
50     return Get().collect_language_server_data_;
51   }
SetCollectKytheData()52   static void SetCollectKytheData() { Get().collect_kythe_data_ = true; }
collect_kythe_data()53   static bool collect_kythe_data() { return Get().collect_kythe_data_; }
SetForceAssertStatements()54   static void SetForceAssertStatements() {
55     Get().force_assert_statements_ = true;
56   }
force_assert_statements()57   static bool force_assert_statements() {
58     return Get().force_assert_statements_;
59   }
SetAnnotateIR()60   static void SetAnnotateIR() { Get().annotate_ir_ = true; }
annotate_ir()61   static bool annotate_ir() { return Get().annotate_ir_; }
ast()62   static Ast* ast() { return &Get().ast_; }
MakeUniqueName(const std::string & base)63   static std::string MakeUniqueName(const std::string& base) {
64     return base + "_" + std::to_string(Get().fresh_ids_[base]++);
65   }
66 
67   struct PerFileStreams {
PerFileStreamsPerFileStreams68     PerFileStreams()
69         : file(SourceId::Invalid()),
70           csa_header(csa_headerfile),
71           csa_cc(csa_ccfile),
72           class_definition_cc(class_definition_ccfile) {}
73     SourceId file;
74     std::stringstream csa_headerfile;
75     cpp::File csa_header;
76     std::stringstream csa_ccfile;
77     cpp::File csa_cc;
78 
79     std::stringstream class_definition_headerfile;
80 
81     // The beginning of the generated -inl.inc file, which includes declarations
82     // for functions corresponding to Torque macros.
83     std::stringstream class_definition_inline_headerfile_macro_declarations;
84     // The second part of the generated -inl.inc file, which includes
85     // definitions for functions declared in the first part.
86     std::stringstream class_definition_inline_headerfile_macro_definitions;
87     // The portion of the generated -inl.inc file containing member function
88     // definitions for the generated class.
89     std::stringstream class_definition_inline_headerfile;
90 
91     std::stringstream class_definition_ccfile;
92     cpp::File class_definition_cc;
93 
94     std::set<SourceId> required_builtin_includes;
95   };
GeneratedPerFile(SourceId file)96   static PerFileStreams& GeneratedPerFile(SourceId file) {
97     PerFileStreams& result = Get().generated_per_file_[file];
98     result.file = file;
99     return result;
100   }
101 
SetInstanceTypesInitialized()102   static void SetInstanceTypesInitialized() {
103     DCHECK(!Get().instance_types_initialized_);
104     Get().instance_types_initialized_ = true;
105   }
IsInstanceTypesInitialized()106   static bool IsInstanceTypesInitialized() {
107     return Get().instance_types_initialized_;
108   }
EnsureInCCOutputList(TorqueMacro * macro,SourceId source)109   static void EnsureInCCOutputList(TorqueMacro* macro, SourceId source) {
110     GlobalContext& c = Get();
111     auto item = std::make_pair(macro, source);
112     if (c.macros_for_cc_output_set_.insert(item).second) {
113       c.macros_for_cc_output_.push_back(item);
114     }
115   }
116   static const std::vector<std::pair<TorqueMacro*, SourceId>>&
AllMacrosForCCOutput()117   AllMacrosForCCOutput() {
118     return Get().macros_for_cc_output_;
119   }
120 
121  private:
122   bool collect_language_server_data_;
123   bool collect_kythe_data_;
124   bool force_assert_statements_;
125   bool annotate_ir_;
126   Namespace* default_namespace_;
127   Ast ast_;
128   std::vector<std::unique_ptr<Declarable>> declarables_;
129   std::set<std::string> cpp_includes_;
130   std::map<SourceId, PerFileStreams> generated_per_file_;
131   std::map<std::string, size_t> fresh_ids_;
132   std::vector<std::pair<TorqueMacro*, SourceId>> macros_for_cc_output_;
133   std::set<std::pair<TorqueMacro*, SourceId>> macros_for_cc_output_set_;
134   bool instance_types_initialized_ = false;
135 
136   friend class LanguageServerData;
137 };
138 
139 template <class T>
RegisterDeclarable(std::unique_ptr<T> d)140 T* RegisterDeclarable(std::unique_ptr<T> d) {
141   return GlobalContext::Get().RegisterDeclarable(std::move(d));
142 }
143 
144 class TargetArchitecture : public ContextualClass<TargetArchitecture> {
145  public:
146   explicit TargetArchitecture(bool force_32bit);
147 
TaggedSize()148   static size_t TaggedSize() { return Get().tagged_size_; }
RawPtrSize()149   static size_t RawPtrSize() { return Get().raw_ptr_size_; }
ExternalPointerSize()150   static size_t ExternalPointerSize() { return Get().external_ptr_size_; }
MaxHeapAlignment()151   static size_t MaxHeapAlignment() { return TaggedSize(); }
ArePointersCompressed()152   static bool ArePointersCompressed() { return TaggedSize() < RawPtrSize(); }
SmiTagAndShiftSize()153   static int SmiTagAndShiftSize() { return Get().smi_tag_and_shift_size_; }
154 
155  private:
156   const size_t tagged_size_;
157   const size_t raw_ptr_size_;
158   const int smi_tag_and_shift_size_;
159   const size_t external_ptr_size_;
160 };
161 
162 }  // namespace torque
163 }  // namespace internal
164 }  // namespace v8
165 
166 #endif  // V8_TORQUE_GLOBAL_CONTEXT_H_
167