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/declarable.h" 15 16 namespace v8 { 17 namespace internal { 18 namespace torque { 19 20 class GlobalContext : public ContextualClass<GlobalContext> { 21 public: 22 GlobalContext(GlobalContext&&) V8_NOEXCEPT = default; 23 GlobalContext& operator=(GlobalContext&&) V8_NOEXCEPT = default; 24 explicit GlobalContext(Ast ast); 25 GetDefaultNamespace()26 static Namespace* GetDefaultNamespace() { return Get().default_namespace_; } 27 template <class T> RegisterDeclarable(std::unique_ptr<T> d)28 T* RegisterDeclarable(std::unique_ptr<T> d) { 29 T* ptr = d.get(); 30 declarables_.push_back(std::move(d)); 31 return ptr; 32 } 33 AllDeclarables()34 static const std::vector<std::unique_ptr<Declarable>>& AllDeclarables() { 35 return Get().declarables_; 36 } 37 AddCppInclude(std::string include_path)38 static void AddCppInclude(std::string include_path) { 39 Get().cpp_includes_.insert(std::move(include_path)); 40 } CppIncludes()41 static const std::set<std::string>& CppIncludes() { 42 return Get().cpp_includes_; 43 } 44 SetCollectLanguageServerData()45 static void SetCollectLanguageServerData() { 46 Get().collect_language_server_data_ = true; 47 } collect_language_server_data()48 static bool collect_language_server_data() { 49 return Get().collect_language_server_data_; 50 } SetForceAssertStatements()51 static void SetForceAssertStatements() { 52 Get().force_assert_statements_ = true; 53 } force_assert_statements()54 static bool force_assert_statements() { 55 return Get().force_assert_statements_; 56 } ast()57 static Ast* ast() { return &Get().ast_; } MakeUniqueName(const std::string & base)58 static std::string MakeUniqueName(const std::string& base) { 59 return base + "_" + std::to_string(Get().fresh_ids_[base]++); 60 } 61 62 struct PerFileStreams { 63 std::stringstream csa_headerfile; 64 std::stringstream csa_ccfile; 65 std::stringstream class_definition_headerfile; 66 std::stringstream class_definition_inline_headerfile; 67 std::stringstream class_definition_ccfile; 68 }; GeneratedPerFile(SourceId file)69 static PerFileStreams& GeneratedPerFile(SourceId file) { 70 return Get().generated_per_file_[file]; 71 } 72 SetInstanceTypesInitialized()73 static void SetInstanceTypesInitialized() { 74 DCHECK(!Get().instance_types_initialized_); 75 Get().instance_types_initialized_ = true; 76 } IsInstanceTypesInitialized()77 static bool IsInstanceTypesInitialized() { 78 return Get().instance_types_initialized_; 79 } EnsureInCCOutputList(TorqueMacro * macro)80 static void EnsureInCCOutputList(TorqueMacro* macro) { 81 GlobalContext& c = Get(); 82 if (c.macros_for_cc_output_set_.insert(macro).second) { 83 c.macros_for_cc_output_.push_back(macro); 84 } 85 } AllMacrosForCCOutput()86 static const std::vector<TorqueMacro*>& AllMacrosForCCOutput() { 87 return Get().macros_for_cc_output_; 88 } 89 90 private: 91 bool collect_language_server_data_; 92 bool force_assert_statements_; 93 Namespace* default_namespace_; 94 Ast ast_; 95 std::vector<std::unique_ptr<Declarable>> declarables_; 96 std::set<std::string> cpp_includes_; 97 std::map<SourceId, PerFileStreams> generated_per_file_; 98 std::map<std::string, size_t> fresh_ids_; 99 std::vector<TorqueMacro*> macros_for_cc_output_; 100 std::unordered_set<TorqueMacro*> macros_for_cc_output_set_; 101 bool instance_types_initialized_ = false; 102 103 friend class LanguageServerData; 104 }; 105 106 template <class T> RegisterDeclarable(std::unique_ptr<T> d)107T* RegisterDeclarable(std::unique_ptr<T> d) { 108 return GlobalContext::Get().RegisterDeclarable(std::move(d)); 109 } 110 111 class TargetArchitecture : public ContextualClass<TargetArchitecture> { 112 public: 113 explicit TargetArchitecture(bool force_32bit); 114 TaggedSize()115 static size_t TaggedSize() { return Get().tagged_size_; } RawPtrSize()116 static size_t RawPtrSize() { return Get().raw_ptr_size_; } ExternalPointerSize()117 static size_t ExternalPointerSize() { return Get().external_ptr_size_; } MaxHeapAlignment()118 static size_t MaxHeapAlignment() { return TaggedSize(); } ArePointersCompressed()119 static bool ArePointersCompressed() { return TaggedSize() < RawPtrSize(); } SmiTagAndShiftSize()120 static int SmiTagAndShiftSize() { return Get().smi_tag_and_shift_size_; } 121 122 private: 123 const size_t tagged_size_; 124 const size_t raw_ptr_size_; 125 const int smi_tag_and_shift_size_; 126 const size_t external_ptr_size_; 127 }; 128 129 } // namespace torque 130 } // namespace internal 131 } // namespace v8 132 133 #endif // V8_TORQUE_GLOBAL_CONTEXT_H_ 134