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
36 class Declarations {
37 public:
TryLookup(const QualifiedName & name)38 static std::vector<Declarable*> TryLookup(const QualifiedName& name) {
39 return CurrentScope::Get()->Lookup(name);
40 }
41
TryLookupShallow(const QualifiedName & name)42 static std::vector<Declarable*> TryLookupShallow(const QualifiedName& name) {
43 return CurrentScope::Get()->LookupShallow(name);
44 }
45
46 template <class T>
TryLookup(const QualifiedName & name)47 static std::vector<T*> TryLookup(const QualifiedName& name) {
48 return FilterDeclarables<T>(TryLookup(name));
49 }
50
Lookup(const QualifiedName & name)51 static std::vector<Declarable*> Lookup(const QualifiedName& name) {
52 std::vector<Declarable*> d = TryLookup(name);
53 if (d.empty()) {
54 ReportError("cannot find \"", name, "\"");
55 }
56 return d;
57 }
58
59 static std::vector<Declarable*> LookupGlobalScope(const QualifiedName& name);
60
61 static const TypeAlias* LookupTypeAlias(const QualifiedName& name);
62 static const Type* LookupType(const QualifiedName& name);
63 static const Type* LookupType(const Identifier* identifier);
64 static base::Optional<const Type*> TryLookupType(const QualifiedName& name);
65 static const Type* LookupGlobalType(const QualifiedName& name);
66
67 static Builtin* FindSomeInternalBuiltinWithType(
68 const BuiltinPointerType* type);
69
70 static Value* LookupValue(const QualifiedName& name);
71
72 static Macro* TryLookupMacro(const std::string& name,
73 const TypeVector& types);
74 static base::Optional<Builtin*> TryLookupBuiltin(const QualifiedName& name);
75
76 static std::vector<GenericCallable*> LookupGeneric(const std::string& name);
77 static GenericCallable* LookupUniqueGeneric(const QualifiedName& name);
78
79 static GenericType* LookupUniqueGenericType(const QualifiedName& name);
80 static GenericType* LookupGlobalUniqueGenericType(const std::string& name);
81 static base::Optional<GenericType*> TryLookupGenericType(
82 const QualifiedName& name);
83
84 static Namespace* DeclareNamespace(const std::string& name);
85 static TypeAlias* DeclareType(const Identifier* name, const Type* type);
86
87 static const TypeAlias* PredeclareTypeAlias(const Identifier* name,
88 TypeDeclaration* type,
89 bool redeclaration);
90 static TorqueMacro* CreateTorqueMacro(std::string external_name,
91 std::string readable_name,
92 bool exported_to_csa,
93 Signature signature,
94 base::Optional<Statement*> body,
95 bool is_user_defined);
96 static ExternMacro* CreateExternMacro(std::string name,
97 std::string external_assembler_name,
98 Signature signature);
99 static Macro* DeclareMacro(
100 const std::string& name, bool accessible_from_csa,
101 base::Optional<std::string> external_assembler_name,
102 const Signature& signature, base::Optional<Statement*> body,
103 base::Optional<std::string> op = {}, bool is_user_defined = true);
104
105 static Method* CreateMethod(AggregateType* class_type,
106 const std::string& name, Signature signature,
107 Statement* body);
108
109 static Intrinsic* CreateIntrinsic(const std::string& name,
110 const Signature& signature);
111
112 static Intrinsic* DeclareIntrinsic(const std::string& name,
113 const Signature& signature);
114
115 static Builtin* CreateBuiltin(std::string external_name,
116 std::string readable_name, Builtin::Kind kind,
117 Signature signature,
118 base::Optional<Statement*> body);
119 static Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind,
120 const Signature& signature,
121 base::Optional<Statement*> body);
122
123 static RuntimeFunction* DeclareRuntimeFunction(const std::string& name,
124 const Signature& signature);
125
126 static void DeclareExternConstant(Identifier* name, const Type* type,
127 std::string value);
128 static NamespaceConstant* DeclareNamespaceConstant(Identifier* name,
129 const Type* type,
130 Expression* body);
131
132 static GenericCallable* DeclareGenericCallable(
133 const std::string& name, GenericCallableDeclaration* ast_node);
134 static GenericType* DeclareGenericType(const std::string& name,
135 GenericTypeDeclaration* ast_node);
136
137 template <class T>
Declare(const std::string & name,T * d)138 static T* Declare(const std::string& name, T* d) {
139 CurrentScope::Get()->AddDeclarable(name, d);
140 return d;
141 }
142 template <class T>
Declare(const std::string & name,std::unique_ptr<T> d)143 static T* Declare(const std::string& name, std::unique_ptr<T> d) {
144 return CurrentScope::Get()->AddDeclarable(name,
145 RegisterDeclarable(std::move(d)));
146 }
147 static Macro* DeclareOperator(const std::string& name, Macro* m);
148
149 static std::string GetGeneratedCallableName(
150 const std::string& name, const TypeVector& specialized_types);
151 };
152
153 } // namespace torque
154 } // namespace internal
155 } // namespace v8
156
157 #endif // V8_TORQUE_DECLARATIONS_H_
158