1 // Copyright 2019 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_OBJECTS_FUNCTION_KIND_H_
6 #define V8_OBJECTS_FUNCTION_KIND_H_
7
8 #include "src/base/bounds.h"
9 #include "src/base/macros.h"
10
11 namespace v8 {
12 namespace internal {
13
14 enum FunctionKind : uint8_t {
15 // BEGIN constructable functions
16 kNormalFunction,
17 kModule,
18 kAsyncModule,
19 // BEGIN class constructors
20 // BEGIN base constructors
21 kBaseConstructor,
22 // BEGIN default constructors
23 kDefaultBaseConstructor,
24 // END base constructors
25 // BEGIN derived constructors
26 kDefaultDerivedConstructor,
27 // END default constructors
28 kDerivedConstructor,
29 // END derived constructors
30 // END class constructors
31 // END constructable functions.
32 // BEGIN accessors
33 kGetterFunction,
34 kSetterFunction,
35 // END accessors
36 // BEGIN arrow functions
37 kArrowFunction,
38 // BEGIN async functions
39 kAsyncArrowFunction,
40 // END arrow functions
41 kAsyncFunction,
42 // BEGIN concise methods 1
43 kAsyncConciseMethod,
44 // BEGIN generators
45 kAsyncConciseGeneratorMethod,
46 // END concise methods 1
47 kAsyncGeneratorFunction,
48 // END async functions
49 kGeneratorFunction,
50 // BEGIN concise methods 2
51 kConciseGeneratorMethod,
52 // END generators
53 kConciseMethod,
54 kClassMembersInitializerFunction,
55 // END concise methods 2
56
57 kLastFunctionKind = kClassMembersInitializerFunction,
58 };
59
60 constexpr int kFunctionKindBitSize = 5;
61 STATIC_ASSERT(kLastFunctionKind < (1 << kFunctionKindBitSize));
62
IsArrowFunction(FunctionKind kind)63 inline bool IsArrowFunction(FunctionKind kind) {
64 return base::IsInRange(kind, FunctionKind::kArrowFunction,
65 FunctionKind::kAsyncArrowFunction);
66 }
67
IsModule(FunctionKind kind)68 inline bool IsModule(FunctionKind kind) {
69 return base::IsInRange(kind, FunctionKind::kModule,
70 FunctionKind::kAsyncModule);
71 }
72
IsAsyncModule(FunctionKind kind)73 inline bool IsAsyncModule(FunctionKind kind) {
74 return kind == FunctionKind::kAsyncModule;
75 }
76
IsAsyncGeneratorFunction(FunctionKind kind)77 inline bool IsAsyncGeneratorFunction(FunctionKind kind) {
78 return base::IsInRange(kind, FunctionKind::kAsyncConciseGeneratorMethod,
79 FunctionKind::kAsyncGeneratorFunction);
80 }
81
IsGeneratorFunction(FunctionKind kind)82 inline bool IsGeneratorFunction(FunctionKind kind) {
83 return base::IsInRange(kind, FunctionKind::kAsyncConciseGeneratorMethod,
84 FunctionKind::kConciseGeneratorMethod);
85 }
86
IsAsyncFunction(FunctionKind kind)87 inline bool IsAsyncFunction(FunctionKind kind) {
88 return base::IsInRange(kind, FunctionKind::kAsyncArrowFunction,
89 FunctionKind::kAsyncGeneratorFunction);
90 }
91
IsResumableFunction(FunctionKind kind)92 inline bool IsResumableFunction(FunctionKind kind) {
93 return IsGeneratorFunction(kind) || IsAsyncFunction(kind) || IsModule(kind);
94 }
95
IsConciseMethod(FunctionKind kind)96 inline bool IsConciseMethod(FunctionKind kind) {
97 return base::IsInRange(kind, FunctionKind::kAsyncConciseMethod,
98 FunctionKind::kAsyncConciseGeneratorMethod) ||
99 base::IsInRange(kind, FunctionKind::kConciseGeneratorMethod,
100 FunctionKind::kClassMembersInitializerFunction);
101 }
102
IsStrictFunctionWithoutPrototype(FunctionKind kind)103 inline bool IsStrictFunctionWithoutPrototype(FunctionKind kind) {
104 return base::IsInRange(kind, FunctionKind::kGetterFunction,
105 FunctionKind::kAsyncArrowFunction) ||
106 base::IsInRange(kind, FunctionKind::kAsyncConciseMethod,
107 FunctionKind::kAsyncConciseGeneratorMethod) ||
108 base::IsInRange(kind, FunctionKind::kConciseGeneratorMethod,
109 FunctionKind::kClassMembersInitializerFunction);
110 }
111
IsGetterFunction(FunctionKind kind)112 inline bool IsGetterFunction(FunctionKind kind) {
113 return kind == FunctionKind::kGetterFunction;
114 }
115
IsSetterFunction(FunctionKind kind)116 inline bool IsSetterFunction(FunctionKind kind) {
117 return kind == FunctionKind::kSetterFunction;
118 }
119
IsAccessorFunction(FunctionKind kind)120 inline bool IsAccessorFunction(FunctionKind kind) {
121 return base::IsInRange(kind, FunctionKind::kGetterFunction,
122 FunctionKind::kSetterFunction);
123 }
124
IsDefaultConstructor(FunctionKind kind)125 inline bool IsDefaultConstructor(FunctionKind kind) {
126 return base::IsInRange(kind, FunctionKind::kDefaultBaseConstructor,
127 FunctionKind::kDefaultDerivedConstructor);
128 }
129
IsBaseConstructor(FunctionKind kind)130 inline bool IsBaseConstructor(FunctionKind kind) {
131 return base::IsInRange(kind, FunctionKind::kBaseConstructor,
132 FunctionKind::kDefaultBaseConstructor);
133 }
134
IsDerivedConstructor(FunctionKind kind)135 inline bool IsDerivedConstructor(FunctionKind kind) {
136 return base::IsInRange(kind, FunctionKind::kDefaultDerivedConstructor,
137 FunctionKind::kDerivedConstructor);
138 }
139
IsClassConstructor(FunctionKind kind)140 inline bool IsClassConstructor(FunctionKind kind) {
141 return base::IsInRange(kind, FunctionKind::kBaseConstructor,
142 FunctionKind::kDerivedConstructor);
143 }
144
IsClassMembersInitializerFunction(FunctionKind kind)145 inline bool IsClassMembersInitializerFunction(FunctionKind kind) {
146 return kind == FunctionKind::kClassMembersInitializerFunction;
147 }
148
IsConstructable(FunctionKind kind)149 inline bool IsConstructable(FunctionKind kind) {
150 return base::IsInRange(kind, FunctionKind::kNormalFunction,
151 FunctionKind::kDerivedConstructor);
152 }
153
FunctionKind2String(FunctionKind kind)154 inline const char* FunctionKind2String(FunctionKind kind) {
155 switch (kind) {
156 case FunctionKind::kNormalFunction:
157 return "NormalFunction";
158 case FunctionKind::kArrowFunction:
159 return "ArrowFunction";
160 case FunctionKind::kGeneratorFunction:
161 return "GeneratorFunction";
162 case FunctionKind::kConciseMethod:
163 return "ConciseMethod";
164 case FunctionKind::kDerivedConstructor:
165 return "DerivedConstructor";
166 case FunctionKind::kBaseConstructor:
167 return "BaseConstructor";
168 case FunctionKind::kGetterFunction:
169 return "GetterFunction";
170 case FunctionKind::kSetterFunction:
171 return "SetterFunction";
172 case FunctionKind::kAsyncFunction:
173 return "AsyncFunction";
174 case FunctionKind::kModule:
175 return "Module";
176 case FunctionKind::kAsyncModule:
177 return "AsyncModule";
178 case FunctionKind::kClassMembersInitializerFunction:
179 return "ClassMembersInitializerFunction";
180 case FunctionKind::kDefaultBaseConstructor:
181 return "DefaultBaseConstructor";
182 case FunctionKind::kDefaultDerivedConstructor:
183 return "DefaultDerivedConstructor";
184 case FunctionKind::kAsyncArrowFunction:
185 return "AsyncArrowFunction";
186 case FunctionKind::kAsyncConciseMethod:
187 return "AsyncConciseMethod";
188 case FunctionKind::kConciseGeneratorMethod:
189 return "ConciseGeneratorMethod";
190 case FunctionKind::kAsyncConciseGeneratorMethod:
191 return "AsyncConciseGeneratorMethod";
192 case FunctionKind::kAsyncGeneratorFunction:
193 return "AsyncGeneratorFunction";
194 }
195 UNREACHABLE();
196 }
197
198 inline std::ostream& operator<<(std::ostream& os, FunctionKind kind) {
199 return os << FunctionKind2String(kind);
200 }
201
202 } // namespace internal
203 } // namespace v8
204
205 #endif // V8_OBJECTS_FUNCTION_KIND_H_
206