Line data Source code
1 :
2 : // Copyright 2019 the V8 project authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 :
6 : #ifndef V8_FUNCTION_KIND_H_
7 : #define V8_FUNCTION_KIND_H_
8 :
9 : #include "src/utils.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : enum FunctionKind : uint8_t {
15 : // BEGIN constructable functions
16 : kNormalFunction,
17 : kModule,
18 : // BEGIN class constructors
19 : // BEGIN base constructors
20 : kBaseConstructor,
21 : // BEGIN default constructors
22 : kDefaultBaseConstructor,
23 : // END base constructors
24 : // BEGIN derived cosntructors
25 : kDefaultDerivedConstructor,
26 : // END default constructors
27 : kDerivedConstructor,
28 : // END derived costructors
29 : // END class cosntructors
30 : // END constructable functions.
31 : // BEGIN accessors
32 : kGetterFunction,
33 : kSetterFunction,
34 : // END accessors
35 : // BEGIN arrow functions
36 : kArrowFunction,
37 : // BEGIN async functions
38 : kAsyncArrowFunction,
39 : // END arrow functions
40 : kAsyncFunction,
41 : // BEGIN concise methods 1
42 : kAsyncConciseMethod,
43 : // BEGIN generators
44 : kAsyncConciseGeneratorMethod,
45 : // END concise methods 1
46 : kAsyncGeneratorFunction,
47 : // END async functions
48 : kGeneratorFunction,
49 : // BEGIN concise methods 2
50 : kConciseGeneratorMethod,
51 : // END generators
52 : kConciseMethod,
53 : kClassMembersInitializerFunction,
54 : // END concise methods 2
55 :
56 : kLastFunctionKind = kClassMembersInitializerFunction,
57 : };
58 :
59 : inline bool IsArrowFunction(FunctionKind kind) {
60 : return IsInRange(kind, FunctionKind::kArrowFunction,
61 : FunctionKind::kAsyncArrowFunction);
62 : }
63 :
64 : inline bool IsModule(FunctionKind kind) {
65 : return kind == FunctionKind::kModule;
66 : }
67 :
68 1375 : inline bool IsAsyncGeneratorFunction(FunctionKind kind) {
69 : return IsInRange(kind, FunctionKind::kAsyncConciseGeneratorMethod,
70 1375 : FunctionKind::kAsyncGeneratorFunction);
71 : }
72 :
73 2834348 : inline bool IsGeneratorFunction(FunctionKind kind) {
74 : return IsInRange(kind, FunctionKind::kAsyncConciseGeneratorMethod,
75 2834348 : FunctionKind::kConciseGeneratorMethod);
76 : }
77 :
78 2803174 : inline bool IsAsyncFunction(FunctionKind kind) {
79 : return IsInRange(kind, FunctionKind::kAsyncArrowFunction,
80 2803174 : FunctionKind::kAsyncGeneratorFunction);
81 : }
82 :
83 8653 : inline bool IsResumableFunction(FunctionKind kind) {
84 142163141 : return IsGeneratorFunction(kind) || IsAsyncFunction(kind) || IsModule(kind);
85 : }
86 :
87 : inline bool IsConciseMethod(FunctionKind kind) {
88 : return IsInRange(kind, FunctionKind::kAsyncConciseMethod,
89 19614823 : FunctionKind::kAsyncConciseGeneratorMethod) ||
90 : IsInRange(kind, FunctionKind::kConciseGeneratorMethod,
91 : FunctionKind::kClassMembersInitializerFunction);
92 : }
93 :
94 : inline bool IsStrictFunctionWithoutPrototype(FunctionKind kind) {
95 : return IsInRange(kind, FunctionKind::kGetterFunction,
96 14993911 : FunctionKind::kAsyncArrowFunction) ||
97 : IsInRange(kind, FunctionKind::kAsyncConciseMethod,
98 30772730 : FunctionKind::kAsyncConciseGeneratorMethod) ||
99 : IsInRange(kind, FunctionKind::kConciseGeneratorMethod,
100 : FunctionKind::kClassMembersInitializerFunction);
101 : }
102 :
103 : inline bool IsGetterFunction(FunctionKind kind) {
104 : return kind == FunctionKind::kGetterFunction;
105 : }
106 :
107 : inline bool IsSetterFunction(FunctionKind kind) {
108 : return kind == FunctionKind::kSetterFunction;
109 : }
110 :
111 : inline bool IsAccessorFunction(FunctionKind kind) {
112 : return IsInRange(kind, FunctionKind::kGetterFunction,
113 : FunctionKind::kSetterFunction);
114 : }
115 :
116 : inline bool IsDefaultConstructor(FunctionKind kind) {
117 : return IsInRange(kind, FunctionKind::kDefaultBaseConstructor,
118 : FunctionKind::kDefaultDerivedConstructor);
119 : }
120 :
121 : inline bool IsBaseConstructor(FunctionKind kind) {
122 : return IsInRange(kind, FunctionKind::kBaseConstructor,
123 : FunctionKind::kDefaultBaseConstructor);
124 : }
125 :
126 : inline bool IsDerivedConstructor(FunctionKind kind) {
127 : return IsInRange(kind, FunctionKind::kDefaultDerivedConstructor,
128 : FunctionKind::kDerivedConstructor);
129 : }
130 :
131 3229 : inline bool IsClassConstructor(FunctionKind kind) {
132 : return IsInRange(kind, FunctionKind::kBaseConstructor,
133 3229 : FunctionKind::kDerivedConstructor);
134 : }
135 :
136 : inline bool IsClassMembersInitializerFunction(FunctionKind kind) {
137 43358 : return kind == FunctionKind::kClassMembersInitializerFunction;
138 : }
139 :
140 : inline bool IsConstructable(FunctionKind kind) {
141 : return IsInRange(kind, FunctionKind::kNormalFunction,
142 : FunctionKind::kDerivedConstructor);
143 : }
144 :
145 : inline std::ostream& operator<<(std::ostream& os, FunctionKind kind) {
146 : switch (kind) {
147 : case FunctionKind::kNormalFunction:
148 : return os << "NormalFunction";
149 : case FunctionKind::kArrowFunction:
150 : return os << "ArrowFunction";
151 : case FunctionKind::kGeneratorFunction:
152 : return os << "GeneratorFunction";
153 : case FunctionKind::kConciseMethod:
154 : return os << "ConciseMethod";
155 : case FunctionKind::kDerivedConstructor:
156 : return os << "DerivedConstructor";
157 : case FunctionKind::kBaseConstructor:
158 : return os << "BaseConstructor";
159 : case FunctionKind::kGetterFunction:
160 : return os << "GetterFunction";
161 : case FunctionKind::kSetterFunction:
162 : return os << "SetterFunction";
163 : case FunctionKind::kAsyncFunction:
164 : return os << "AsyncFunction";
165 : case FunctionKind::kModule:
166 : return os << "Module";
167 : case FunctionKind::kClassMembersInitializerFunction:
168 : return os << "ClassMembersInitializerFunction";
169 : case FunctionKind::kDefaultBaseConstructor:
170 : return os << "DefaultBaseConstructor";
171 : case FunctionKind::kDefaultDerivedConstructor:
172 : return os << "DefaultDerivedConstructor";
173 : case FunctionKind::kAsyncArrowFunction:
174 : return os << "AsyncArrowFunction";
175 : case FunctionKind::kAsyncConciseMethod:
176 : return os << "AsyncConciseMethod";
177 : case FunctionKind::kConciseGeneratorMethod:
178 : return os << "ConciseGeneratorMethod";
179 : case FunctionKind::kAsyncConciseGeneratorMethod:
180 : return os << "AsyncConciseGeneratorMethod";
181 : case FunctionKind::kAsyncGeneratorFunction:
182 : return os << "AsyncGeneratorFunction";
183 : }
184 : UNREACHABLE();
185 : }
186 :
187 : } // namespace internal
188 : } // namespace v8
189 :
190 : #endif // V8_FUNCTION_KIND_H_
|