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/file-visitor.h" |
14 | #include "src/torque/global-context.h" |
15 | #include "src/torque/types.h" |
16 | #include "src/torque/utils.h" |
17 | |
18 | namespace v8 { |
19 | namespace internal { |
20 | namespace torque { |
21 | |
22 | class DeclarationVisitor : public FileVisitor { |
23 | public: |
24 | void Visit(Ast* ast) { |
25 | CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace()); |
26 | for (Declaration* child : ast->declarations()) Visit(child); |
27 | } |
28 | |
29 | void Visit(Declaration* decl); |
30 | |
31 | Namespace* GetOrCreateNamespace(const std::string& name) { |
32 | std::vector<Namespace*> existing_namespaces = FilterDeclarables<Namespace>( |
33 | Declarations::TryLookupShallow(QualifiedName(name))); |
34 | if (existing_namespaces.empty()) { |
35 | return Declarations::DeclareNamespace(name); |
36 | } |
37 | DCHECK_EQ(1, existing_namespaces.size()); |
38 | return existing_namespaces.front(); |
39 | } |
40 | |
41 | void Visit(NamespaceDeclaration* decl) { |
42 | CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name)); |
43 | for (Declaration* child : decl->declarations) Visit(child); |
44 | } |
45 | |
46 | void Visit(TypeDeclaration* decl); |
47 | |
48 | void DeclareMethods(AggregateType* container, |
49 | const std::vector<Declaration*>& methods); |
50 | void Visit(StructDeclaration* decl); |
51 | void Visit(ClassDeclaration* decl); |
52 | |
53 | void Visit(TypeAliasDeclaration* decl) { |
54 | const Type* type = Declarations::GetType(decl->type); |
55 | type->AddAlias(decl->name->value); |
56 | Declarations::DeclareType(decl->name, type, true); |
57 | } |
58 | |
59 | Builtin* CreateBuiltin(BuiltinDeclaration* decl, std::string external_name, |
60 | std::string readable_name, Signature signature, |
61 | base::Optional<Statement*> body); |
62 | void Visit(ExternalBuiltinDeclaration* decl, const Signature& signature, |
63 | base::Optional<Statement*> body) { |
64 | Declarations::Declare( |
65 | decl->name, |
66 | CreateBuiltin(decl, decl->name, decl->name, signature, base::nullopt)); |
67 | } |
68 | |
69 | void Visit(ExternalRuntimeDeclaration* decl, const Signature& sig, |
70 | base::Optional<Statement*> body); |
71 | void Visit(ExternalMacroDeclaration* decl, const Signature& sig, |
72 | base::Optional<Statement*> body); |
73 | void Visit(TorqueBuiltinDeclaration* decl, const Signature& signature, |
74 | base::Optional<Statement*> body); |
75 | void Visit(TorqueMacroDeclaration* decl, const Signature& signature, |
76 | base::Optional<Statement*> body); |
77 | void Visit(IntrinsicDeclaration* decl, const Signature& signature, |
78 | base::Optional<Statement*> body); |
79 | |
80 | void Visit(CallableNode* decl, const Signature& signature, |
81 | base::Optional<Statement*> body); |
82 | |
83 | void Visit(ConstDeclaration* decl); |
84 | void Visit(StandardDeclaration* decl); |
85 | void Visit(GenericDeclaration* decl); |
86 | void Visit(SpecializationDeclaration* decl); |
87 | void Visit(ExternConstDeclaration* decl); |
88 | void Visit(CppIncludeDeclaration* decl); |
89 | |
90 | Signature MakeSpecializedSignature(const SpecializationKey& key); |
91 | Callable* SpecializeImplicit(const SpecializationKey& key); |
92 | Callable* Specialize(const SpecializationKey& key, CallableNode* declaration, |
93 | base::Optional<const CallableNodeSignature*> signature, |
94 | base::Optional<Statement*> body); |
95 | |
96 | void FinalizeStructsAndClasses(); |
97 | |
98 | private: |
99 | void DeclareSpecializedTypes(const SpecializationKey& key); |
100 | |
101 | void FinalizeStructFieldsAndMethods(StructType* struct_type, |
102 | StructDeclaration* struct_declaration); |
103 | void FinalizeClassFieldsAndMethods(ClassType* class_type, |
104 | ClassDeclaration* class_declaration); |
105 | |
106 | std::vector<std::tuple<Scope*, StructDeclaration*, StructType*>> |
107 | struct_declarations_; |
108 | std::vector<std::tuple<Scope*, ClassDeclaration*, ClassType*>> |
109 | class_declarations_; |
110 | }; |
111 | |
112 | } // namespace torque |
113 | } // namespace internal |
114 | } // namespace v8 |
115 | |
116 | #endif // V8_TORQUE_DECLARATION_VISITOR_H_ |
117 | |