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_DECLARATIONS_H_ 6 #define V8_TORQUE_DECLARATIONS_H_ 7 8 #include <string> 9 10 #include "src/torque/declarable.h" 11 #include "src/torque/scope.h" 12 #include "src/torque/utils.h" 13 14 namespace v8 { 15 namespace internal { 16 namespace torque { 17 18 static constexpr const char* const kFromConstexprMacroName = "from_constexpr"; 19 static constexpr const char* kTrueLabelName = "_True"; 20 static constexpr const char* kFalseLabelName = "_False"; 21 22 class Declarations { 23 public: Declarations()24 Declarations() 25 : unique_declaration_number_(0), 26 current_generic_specialization_(nullptr) {} 27 TryLookup(const std::string & name)28 Declarable* TryLookup(const std::string& name) { return chain_.Lookup(name); } 29 Lookup(const std::string & name)30 Declarable* Lookup(const std::string& name) { 31 Declarable* d = TryLookup(name); 32 if (d == nullptr) { 33 std::stringstream s; 34 s << "cannot find \"" << name << "\""; 35 ReportError(s.str()); 36 } 37 return d; 38 } 39 LookupGlobalScope(const std::string & name)40 Declarable* LookupGlobalScope(const std::string& name) { 41 Declarable* d = chain_.LookupGlobalScope(name); 42 if (d == nullptr) { 43 std::stringstream s; 44 s << "cannot find \"" << name << "\" in global scope"; 45 ReportError(s.str()); 46 } 47 return d; 48 } 49 50 const Type* LookupType(const std::string& name); 51 const Type* LookupGlobalType(const std::string& name); 52 const Type* GetType(TypeExpression* type_expression); 53 54 Builtin* FindSomeInternalBuiltinWithType(const FunctionPointerType* type); 55 56 Value* LookupValue(const std::string& name); 57 58 Macro* TryLookupMacro(const std::string& name, const TypeVector& types); 59 Macro* LookupMacro(const std::string& name, const TypeVector& types); 60 61 Builtin* LookupBuiltin(const std::string& name); 62 TryLookupLabel(const std::string & name)63 Label* TryLookupLabel(const std::string& name) { 64 Declarable* d = TryLookup(name); 65 return d && d->IsLabel() ? Label::cast(d) : nullptr; 66 } 67 Label* LookupLabel(const std::string& name); 68 69 GenericList* LookupGeneric(const std::string& name); 70 ModuleConstant* LookupModuleConstant(const std::string& name); 71 72 const AbstractType* DeclareAbstractType( 73 const std::string& name, const std::string& generated, 74 base::Optional<const AbstractType*> non_constexpr_version, 75 const base::Optional<std::string>& parent = {}); 76 77 void DeclareType(const std::string& name, const Type* type); 78 79 void DeclareStruct(Module* module, const std::string& name, 80 const std::vector<NameAndType>& fields); 81 82 Label* DeclareLabel(const std::string& name); 83 84 Macro* DeclareMacro(const std::string& name, const Signature& signature, 85 base::Optional<std::string> op = {}); 86 87 Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind, 88 bool external, const Signature& signature); 89 90 RuntimeFunction* DeclareRuntimeFunction(const std::string& name, 91 const Signature& signature); 92 93 Variable* DeclareVariable(const std::string& var, const Type* type, 94 bool is_const); 95 96 Parameter* DeclareParameter(const std::string& name, 97 const std::string& mangled_name, 98 const Type* type); 99 100 Label* DeclarePrivateLabel(const std::string& name); 101 102 void DeclareExternConstant(const std::string& name, const Type* type, 103 const std::string& value); 104 ModuleConstant* DeclareModuleConstant(const std::string& name, 105 const Type* type); 106 107 Generic* DeclareGeneric(const std::string& name, Module* module, 108 GenericDeclaration* generic); 109 110 TypeVector GetCurrentSpecializationTypeNamesVector(); 111 base::Optional<Generic*> GetCurrentGeneric(); 112 GetScopeChainSnapshot()113 ScopeChain::Snapshot GetScopeChainSnapshot() { return chain_.TaskSnapshot(); } 114 GetLiveVariables()115 std::set<const Variable*> GetLiveVariables() { 116 return chain_.GetLiveVariables(); 117 } 118 119 bool IsDeclaredInCurrentScope(const std::string& name); 120 next_body()121 Statement* next_body() const { return next_body_; } 122 PrintScopeChain()123 void PrintScopeChain() { chain_.Print(); } 124 125 class ModuleScopeActivator; 126 class NodeScopeActivator; 127 class CleanNodeScopeActivator; 128 class GenericScopeActivator; 129 class ScopedGenericSpecializationKey; 130 class ScopedGenericScopeChainSnapshot; 131 132 private: 133 Scope* GetModuleScope(const Module* module); 134 Scope* GetNodeScope(const AstNode* node, bool reset_scope = false); 135 Scope* GetGenericScope(Generic* generic, const TypeVector& types); 136 137 template <class T> RegisterDeclarable(std::unique_ptr<T> d)138 T* RegisterDeclarable(std::unique_ptr<T> d) { 139 T* ptr = d.get(); 140 declarables_.push_back(std::move(d)); 141 return ptr; 142 } 143 144 MacroList* GetMacroListForName(const std::string& name, 145 const Signature& signature); 146 Declare(const std::string & name,std::unique_ptr<Declarable> d)147 void Declare(const std::string& name, std::unique_ptr<Declarable> d) { 148 chain_.Declare(name, RegisterDeclarable(std::move(d))); 149 } 150 GetNextUniqueDeclarationNumber()151 int GetNextUniqueDeclarationNumber() { return unique_declaration_number_++; } 152 153 void CheckAlreadyDeclared(const std::string& name, const char* new_type); 154 155 int unique_declaration_number_; 156 ScopeChain chain_; 157 const SpecializationKey* current_generic_specialization_; 158 Statement* next_body_; 159 std::vector<std::unique_ptr<Declarable>> declarables_; 160 std::map<const Module*, Scope*> module_scopes_; 161 std::map<std::pair<const AstNode*, TypeVector>, Scope*> scopes_; 162 std::map<Generic*, ScopeChain::Snapshot> generic_declaration_scopes_; 163 }; 164 165 class Declarations::NodeScopeActivator { 166 public: NodeScopeActivator(Declarations * declarations,AstNode * node)167 NodeScopeActivator(Declarations* declarations, AstNode* node) 168 : activator_(declarations->GetNodeScope(node)) {} 169 170 private: 171 Scope::Activator activator_; 172 }; 173 174 class Declarations::ModuleScopeActivator { 175 public: ModuleScopeActivator(Declarations * declarations,const Module * module)176 ModuleScopeActivator(Declarations* declarations, const Module* module) 177 : activator_(declarations->GetModuleScope(module)) {} 178 179 private: 180 Scope::Activator activator_; 181 }; 182 183 class Declarations::CleanNodeScopeActivator { 184 public: CleanNodeScopeActivator(Declarations * declarations,AstNode * node)185 CleanNodeScopeActivator(Declarations* declarations, AstNode* node) 186 : activator_(declarations->GetNodeScope(node, true)) {} 187 188 private: 189 Scope::Activator activator_; 190 }; 191 192 class Declarations::GenericScopeActivator { 193 public: GenericScopeActivator(Declarations * declarations,const SpecializationKey & key)194 GenericScopeActivator(Declarations* declarations, 195 const SpecializationKey& key) 196 : activator_(declarations->GetGenericScope(key.first, key.second)) {} 197 198 private: 199 Scope::Activator activator_; 200 }; 201 202 class Declarations::ScopedGenericSpecializationKey { 203 public: ScopedGenericSpecializationKey(Declarations * declarations,const SpecializationKey & key)204 ScopedGenericSpecializationKey(Declarations* declarations, 205 const SpecializationKey& key) 206 : declarations_(declarations) { 207 declarations->current_generic_specialization_ = &key; 208 } ~ScopedGenericSpecializationKey()209 ~ScopedGenericSpecializationKey() { 210 declarations_->current_generic_specialization_ = nullptr; 211 } 212 213 private: 214 Declarations* declarations_; 215 }; 216 217 class Declarations::ScopedGenericScopeChainSnapshot { 218 public: ScopedGenericScopeChainSnapshot(Declarations * declarations,const SpecializationKey & key)219 ScopedGenericScopeChainSnapshot(Declarations* declarations, 220 const SpecializationKey& key) 221 : restorer_(declarations->generic_declaration_scopes_[key.first]) {} ~ScopedGenericScopeChainSnapshot()222 ~ScopedGenericScopeChainSnapshot() {} 223 224 private: 225 ScopeChain::ScopedSnapshotRestorer restorer_; 226 }; 227 228 std::string GetGeneratedCallableName(const std::string& name, 229 const TypeVector& specialized_types); 230 231 } // namespace torque 232 } // namespace internal 233 } // namespace v8 234 235 #endif // V8_TORQUE_DECLARATIONS_H_ 236