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_DECLARATION_VISITOR_H_ 6 #define V8_TORQUE_DECLARATION_VISITOR_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "src/base/macros.h" 12 #include "src/torque/declarations.h" 13 #include "src/torque/global-context.h" 14 #include "src/torque/types.h" 15 #include "src/torque/utils.h" 16 17 namespace v8 { 18 namespace internal { 19 namespace torque { 20 21 Namespace* GetOrCreateNamespace(const std::string& name); 22 23 class PredeclarationVisitor { 24 public: Predeclare(Ast * ast)25 static void Predeclare(Ast* ast) { 26 CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace()); 27 for (Declaration* child : ast->declarations()) Predeclare(child); 28 } 29 static void ResolvePredeclarations(); 30 31 private: 32 static void Predeclare(Declaration* decl); Predeclare(NamespaceDeclaration * decl)33 static void Predeclare(NamespaceDeclaration* decl) { 34 CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name)); 35 for (Declaration* child : decl->declarations) Predeclare(child); 36 } Predeclare(TypeDeclaration * decl)37 static void Predeclare(TypeDeclaration* decl) { 38 Declarations::PredeclareTypeAlias(decl->name, decl, false); 39 } Predeclare(StructDeclaration * decl)40 static void Predeclare(StructDeclaration* decl) { 41 Declarations::PredeclareTypeAlias(decl->name, decl, false); 42 } Predeclare(GenericTypeDeclaration * generic_decl)43 static void Predeclare(GenericTypeDeclaration* generic_decl) { 44 Declarations::DeclareGenericType(generic_decl->declaration->name->value, 45 generic_decl); 46 } Predeclare(GenericCallableDeclaration * generic_decl)47 static void Predeclare(GenericCallableDeclaration* generic_decl) { 48 Declarations::DeclareGenericCallable(generic_decl->declaration->name->value, 49 generic_decl); 50 } 51 }; 52 53 class DeclarationVisitor { 54 public: Visit(Ast * ast)55 static void Visit(Ast* ast) { 56 CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace()); 57 for (Declaration* child : ast->declarations()) Visit(child); 58 } 59 static void Visit(Declaration* decl); Visit(NamespaceDeclaration * decl)60 static void Visit(NamespaceDeclaration* decl) { 61 CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name)); 62 for (Declaration* child : decl->declarations) Visit(child); 63 } 64 Visit(TypeDeclaration * decl)65 static void Visit(TypeDeclaration* decl) { 66 // Looking up the type will trigger type computation; this ensures errors 67 // are reported even if the type is unused. 68 Declarations::LookupType(decl->name); 69 } Visit(StructDeclaration * decl)70 static void Visit(StructDeclaration* decl) { 71 Declarations::LookupType(decl->name); 72 } 73 74 static Builtin* CreateBuiltin(BuiltinDeclaration* decl, 75 std::string external_name, 76 std::string readable_name, Signature signature, 77 base::Optional<Statement*> body); 78 79 static void Visit(ExternalBuiltinDeclaration* decl); 80 static void Visit(ExternalRuntimeDeclaration* decl); 81 static void Visit(ExternalMacroDeclaration* decl); 82 static void Visit(TorqueBuiltinDeclaration* decl); 83 static void Visit(TorqueMacroDeclaration* decl); 84 static void Visit(IntrinsicDeclaration* decl); 85 86 static void Visit(ConstDeclaration* decl); Visit(GenericCallableDeclaration * decl)87 static void Visit(GenericCallableDeclaration* decl) { 88 // The PredeclarationVisitor already handled this case. 89 } Visit(GenericTypeDeclaration * decl)90 static void Visit(GenericTypeDeclaration* decl) { 91 // The PredeclarationVisitor already handled this case. 92 } 93 static void Visit(SpecializationDeclaration* decl); 94 static void Visit(ExternConstDeclaration* decl); 95 static void Visit(CppIncludeDeclaration* decl); 96 97 static Signature MakeSpecializedSignature( 98 const SpecializationKey<GenericCallable>& key); 99 static Callable* SpecializeImplicit( 100 const SpecializationKey<GenericCallable>& key); 101 static Callable* Specialize( 102 const SpecializationKey<GenericCallable>& key, 103 CallableDeclaration* declaration, 104 base::Optional<const SpecializationDeclaration*> explicit_specialization, 105 base::Optional<Statement*> body, SourcePosition position); 106 107 private: 108 static void DeclareSpecializedTypes( 109 const SpecializationKey<GenericCallable>& key); 110 }; 111 112 } // namespace torque 113 } // namespace internal 114 } // namespace v8 115 116 #endif // V8_TORQUE_DECLARATION_VISITOR_H_ 117