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 <memory>
9 #include <string>
10
11 #include "src/torque/declarable.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 = "FromConstexpr";
19 static constexpr const char* kMacroEndLabelName = "__macro_end";
20 static constexpr const char* kBreakLabelName = "__break";
21 static constexpr const char* kContinueLabelName = "__continue";
22 static constexpr const char* kCatchLabelName = "__catch";
23 static constexpr const char* kNextCaseLabelName = "__NextCase";
24
25 template <class T>
FilterDeclarables(const std::vector<Declarable * > list)26 std::vector<T*> FilterDeclarables(const std::vector<Declarable*> list) {
27 std::vector<T*> result;
28 for (Declarable* declarable : list) {
29 if (T* t = T::DynamicCast(declarable)) {
30 result.push_back(t);
31 }
32 }
33 return result;
34 }
35
UnwrapTNodeTypeName(const std::string & generates)36 inline std::string UnwrapTNodeTypeName(const std::string& generates) {
37 if (generates.length() < 7 || generates.substr(0, 6) != "TNode<" ||
38 generates.substr(generates.length() - 1, 1) != ">") {
39 ReportError("generated type \"", generates,
40 "\" should be of the form \"TNode<...>\"");
41 }
42 return generates.substr(6, generates.length() - 7);
43 }
44
45 class Declarations {
46 public:
TryLookup(const QualifiedName & name)47 static std::vector<Declarable*> TryLookup(const QualifiedName& name) {
48 return CurrentScope::Get()->Lookup(name);
49 }
50
TryLookupShallow(const QualifiedName & name)51 static std::vector<Declarable*> TryLookupShallow(const QualifiedName& name) {
52 return CurrentScope::Get()->LookupShallow(name);
53 }
54
55 template <class T>
TryLookup(const QualifiedName & name)56 static std::vector<T*> TryLookup(const QualifiedName& name) {
57 return FilterDeclarables<T>(TryLookup(name));
58 }
59
Lookup(const QualifiedName & name)60 static std::vector<Declarable*> Lookup(const QualifiedName& name) {
61 std::vector<Declarable*> d = TryLookup(name);
62 if (d.empty()) {
63 ReportError("cannot find \"", name, "\"");
64 }
65 return d;
66 }
67
68 static std::vector<Declarable*> LookupGlobalScope(const QualifiedName& name);
69
70 static const TypeAlias* LookupTypeAlias(const QualifiedName& name);
71 static const Type* LookupType(const QualifiedName& name);
72 static const Type* LookupType(const Identifier* identifier);
73 static base::Optional<const Type*> TryLookupType(const QualifiedName& name);
74 static const Type* LookupGlobalType(const QualifiedName& name);
75
76 static Builtin* FindSomeInternalBuiltinWithType(
77 const BuiltinPointerType* type);
78
79 static Value* LookupValue(const QualifiedName& name);
80
81 static Macro* TryLookupMacro(const std::string& name,
82 const TypeVector& types);
83 static base::Optional<Builtin*> TryLookupBuiltin(const QualifiedName& name);
84
85 static std::vector<GenericCallable*> LookupGeneric(const std::string& name);
86 static GenericCallable* LookupUniqueGeneric(const QualifiedName& name);
87
88 static GenericType* LookupUniqueGenericType(const QualifiedName& name);
89 static GenericType* LookupGlobalUniqueGenericType(const std::string& name);
90 static base::Optional<GenericType*> TryLookupGenericType(
91 const QualifiedName& name);
92
93 static Namespace* DeclareNamespace(const std::string& name);
94 static TypeAlias* DeclareType(const Identifier* name, const Type* type);
95
96 static TypeAlias* PredeclareTypeAlias(const Identifier* name,
97 TypeDeclaration* type,
98 bool redeclaration);
99 static TorqueMacro* CreateTorqueMacro(std::string external_name,
100 std::string readable_name,
101 bool exported_to_csa,
102 Signature signature,
103 base::Optional<Statement*> body,
104 bool is_user_defined);
105 static ExternMacro* CreateExternMacro(std::string name,
106 std::string external_assembler_name,
107 Signature signature);
108 static Macro* DeclareMacro(
109 const std::string& name, bool accessible_from_csa,
110 base::Optional<std::string> external_assembler_name,
111 const Signature& signature, base::Optional<Statement*> body,
112 base::Optional<std::string> op = {}, bool is_user_defined = true);
113
114 static Method* CreateMethod(AggregateType* class_type,
115 const std::string& name, Signature signature,
116 Statement* body);
117
118 static Intrinsic* CreateIntrinsic(const std::string& name,
119 const Signature& signature);
120
121 static Intrinsic* DeclareIntrinsic(const std::string& name,
122 const Signature& signature);
123
124 static Builtin* CreateBuiltin(std::string external_name,
125 std::string readable_name, Builtin::Kind kind,
126 Signature signature,
127 base::Optional<Statement*> body);
128 static Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind,
129 const Signature& signature,
130 base::Optional<Statement*> body);
131
132 static RuntimeFunction* DeclareRuntimeFunction(const std::string& name,
133 const Signature& signature);
134
135 static ExternConstant* DeclareExternConstant(Identifier* name,
136 const Type* type,
137 std::string value);
138 static NamespaceConstant* DeclareNamespaceConstant(Identifier* name,
139 const Type* type,
140 Expression* body);
141
142 static GenericCallable* DeclareGenericCallable(
143 const std::string& name, GenericCallableDeclaration* ast_node);
144 static GenericType* DeclareGenericType(const std::string& name,
145 GenericTypeDeclaration* ast_node);
146
147 template <class T>
Declare(const std::string & name,T * d)148 static T* Declare(const std::string& name, T* d) {
149 CurrentScope::Get()->AddDeclarable(name, d);
150 return d;
151 }
152 template <class T>
Declare(const std::string & name,std::unique_ptr<T> d)153 static T* Declare(const std::string& name, std::unique_ptr<T> d) {
154 return CurrentScope::Get()->AddDeclarable(name,
155 RegisterDeclarable(std::move(d)));
156 }
157 static Macro* DeclareOperator(const std::string& name, Macro* m);
158
159 static std::string GetGeneratedCallableName(
160 const std::string& name, const TypeVector& specialized_types);
161 };
162
163 } // namespace torque
164 } // namespace internal
165 } // namespace v8
166
167 #endif // V8_TORQUE_DECLARATIONS_H_
168