Flutter Engine
The Flutter Engine
parser.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_PARSER_H_
6#define RUNTIME_VM_PARSER_H_
7
8#include "include/dart_api.h"
9
11#include "platform/assert.h"
12#include "platform/globals.h"
13#include "vm/allocation.h"
14#include "vm/class_finalizer.h"
15#include "vm/hash_table.h"
16#include "vm/kernel.h"
17#include "vm/object.h"
18#include "vm/raw_object.h"
19#include "vm/scopes.h"
20#include "vm/token.h"
21
22namespace dart {
23
24// Forward declarations.
25
26namespace kernel {
27
28class ScopeBuildingResult;
29
30} // namespace kernel
31
32class ArgumentsDescriptor;
33class BitVector;
34class Isolate;
35class LocalScope;
36class LocalVariable;
37struct RegExpCompileData;
38template <typename T>
39class GrowableArray;
40class Parser;
41
43 public:
44 // Typedefs needed for the DirectChainedHashMap template.
45 typedef const Field* Key;
46 typedef const Field* Value;
47 typedef const Field* Pair;
48
49 static Key KeyOf(Pair kv) { return kv; }
50
51 static Value ValueOf(Pair kv) { return kv; }
52
53 static inline uword Hash(Key key) {
54 const TokenPosition token_pos = key->token_pos();
55 if (token_pos.IsReal()) {
56 return token_pos.Hash();
57 }
58 return key->kernel_offset();
59 }
60
61 static inline bool IsKeyEqual(Pair pair, Key key) {
62 return pair->Original() == key->Original();
63 }
64};
65
67
68// The class ParsedFunction holds the result of parsing a function.
70 public:
72
73 const Function& function() const { return function_; }
74 const Code& code() const { return code_; }
75
76 LocalScope* scope() const { return scope_; }
78 ASSERT(scope_ == nullptr);
79 ASSERT(scope != nullptr);
80 scope_ = scope;
81 }
82
84 return regexp_compile_data_;
85 }
87
89 return function_type_arguments_;
90 }
93 function_type_arguments_ = function_type_arguments;
94 }
96 return parent_type_arguments_;
97 }
99 ASSERT(parent_type_arguments != nullptr);
100 parent_type_arguments_ = parent_type_arguments;
101 }
102
103 LocalVariable* suspend_state_var() const { return suspend_state_var_; }
105 ASSERT(suspend_state_var != nullptr);
106 suspend_state_var_ = suspend_state_var;
107 }
108
110 default_parameter_values_ = list;
111#if defined(DEBUG)
112 if (list == nullptr) return;
113 for (intptr_t i = 0; i < list->length(); i++) {
114 DEBUG_ASSERT(list->At(i)->IsNotTemporaryScopedHandle());
115 }
116#endif
117 }
118
119 const Instance& DefaultParameterValueAt(intptr_t i) const {
120 ASSERT(default_parameter_values_ != nullptr);
121 return *default_parameter_values_->At(i);
122 }
123
125 return default_parameter_values_;
126 }
127
128 LocalVariable* current_context_var() const { return current_context_var_; }
129
130 bool has_arg_desc_var() const { return arg_desc_var_ != nullptr; }
131 LocalVariable* arg_desc_var() const { return arg_desc_var_; }
132
134 ASSERT(receiver_var_ != nullptr);
135 return receiver_var_;
136 }
138 ASSERT(receiver_var_ == nullptr);
139 ASSERT(value != nullptr);
140 receiver_var_ = value;
141 }
142 bool has_receiver_var() const { return receiver_var_ != nullptr; }
143
144 void set_receiver_used() { receiver_used_ = true; }
145 bool is_receiver_used() const {
146 ASSERT(kernel_scopes_ != nullptr);
147 ASSERT(!receiver_used_ || receiver_var() != nullptr);
148 return receiver_used_;
149 }
150
153 return expression_temp_var_;
154 }
157 expression_temp_var_ = value;
158 }
160 return expression_temp_var_ != nullptr;
161 }
162
165 return entry_points_temp_var_;
166 }
169 entry_points_temp_var_ = value;
170 }
172 return entry_points_temp_var_ != nullptr;
173 }
174
177 return finally_return_temp_var_;
178 }
181 finally_return_temp_var_ = value;
182 }
184 return finally_return_temp_var_ != nullptr;
185 }
186 void EnsureFinallyReturnTemp(bool is_async);
187
190
191 const FieldSet* guarded_fields() const { return &guarded_fields_; }
192
193 VariableIndex first_parameter_index() const { return first_parameter_index_; }
194 int num_stack_locals() const { return num_stack_locals_; }
195
196 void AllocateVariables();
198
199 void record_await() { have_seen_await_expr_ = true; }
200 bool have_seen_await() const { return have_seen_await_expr_; }
201 bool is_forwarding_stub() const {
202 return forwarding_stub_super_target_ != nullptr;
203 }
205 return forwarding_stub_super_target_;
206 }
207 void MarkForwardingStub(const Function* forwarding_target) {
208 forwarding_stub_super_target_ = forwarding_target;
209 }
210
211 Thread* thread() const { return thread_; }
212 Isolate* isolate() const { return thread_->isolate(); }
213 Zone* zone() const { return thread_->zone(); }
214
215 // Adds only relevant fields: field must be unique and its guarded_cid()
216 // relevant.
217 void AddToGuardedFields(const Field* field) const;
218
219 void Bailout(const char* origin, const char* reason) const;
220
222
224 return raw_type_arguments_var_;
225 }
226
227 void SetRawTypeArgumentsVariable(LocalVariable* raw_type_arguments_var) {
228 raw_type_arguments_var_ = raw_type_arguments_var;
229 }
230
232 raw_parameters_ = raw_parameters;
233 }
234
236 return raw_parameters_->At(i);
237 }
238
240 ASSERT((i >= 0) && (i < function_.NumParameters()));
241 ASSERT(scope() != nullptr);
242 return scope()->VariableAt(i);
243 }
244
245 // Remembers the set of covariant parameters.
246 // [covariant_parameters] is a bitvector of function.NumParameters() length.
247 void SetCovariantParameters(const BitVector* covariant_parameters);
248
249 // Remembers the set of generic-covariant-impl parameters.
250 // [covariant_parameters] is a bitvector of function.NumParameters() length.
252 const BitVector* generic_covariant_impl_parameters);
253
255 return covariant_parameters_ != nullptr;
256 }
257
258 // Returns true if i-th parameter is covariant.
259 // SetCovariantParameters should be called before using this method.
260 bool IsCovariantParameter(intptr_t i) const;
261
262 // Returns true if i-th parameter is generic-covariant-impl.
263 // SetGenericCovariantImplParameters should be called before using this
264 // method.
265 bool IsGenericCovariantImplParameter(intptr_t i) const;
266
267 // Variables needed for the InvokeFieldDispatcher for dynamic closure calls,
268 // because they are both read and written to by the builders.
270 DynamicClosureCallVars(Zone* zone, intptr_t num_named)
272
273#define FOR_EACH_DYNAMIC_CLOSURE_CALL_VARIABLE(V) \
274 V(current_function, Function, CurrentFunction) \
275 V(current_num_processed, Smi, CurrentNumProcessed) \
276 V(current_param_index, Smi, CurrentParamIndex) \
277 V(current_type_param, Dynamic, CurrentTypeParam) \
278 V(function_type_args, Dynamic, FunctionTypeArgs)
279
280#define DEFINE_FIELD(Name, _, __) LocalVariable* Name = nullptr;
282#undef DEFINE_FIELD
283
284 // An array of local variables, one for each named parameter in the
285 // saved arguments descriptor.
287 };
288
290 return dynamic_closure_call_vars_;
291 }
292 DynamicClosureCallVars* EnsureDynamicClosureCallVars();
293
294 private:
295 Thread* thread_;
296 const Function& function_;
297 Code& code_;
298 LocalScope* scope_;
299 RegExpCompileData* regexp_compile_data_;
300 LocalVariable* function_type_arguments_;
301 LocalVariable* parent_type_arguments_;
302 LocalVariable* suspend_state_var_ = nullptr;
303 LocalVariable* current_context_var_;
304 LocalVariable* arg_desc_var_;
305 LocalVariable* receiver_var_ = nullptr;
306 LocalVariable* expression_temp_var_;
307 LocalVariable* entry_points_temp_var_;
308 LocalVariable* finally_return_temp_var_;
309 DynamicClosureCallVars* dynamic_closure_call_vars_;
310 mutable FieldSet guarded_fields_;
311 ZoneGrowableArray<const Instance*>* default_parameter_values_;
312 bool receiver_used_ = false;
313
314 LocalVariable* raw_type_arguments_var_;
315 ZoneGrowableArray<LocalVariable*>* raw_parameters_ = nullptr;
316
317 VariableIndex first_parameter_index_;
318 int num_stack_locals_;
319 bool have_seen_await_expr_;
320
321 const Function* forwarding_stub_super_target_ = nullptr;
322 kernel::ScopeBuildingResult* kernel_scopes_;
323
324 const BitVector* covariant_parameters_ = nullptr;
325 const BitVector* generic_covariant_impl_parameters_ = nullptr;
326
327 friend class Parser;
329};
330
331class Parser : public ValueObject {
332 public:
333 // Parse a function to retrieve parameter information that is not retained in
334 // the Function object. Returns either an error if the parse fails (which
335 // could be the case for local functions), or a flat array of entries for each
336 // parameter. Each parameter entry contains: * a Dart bool indicating whether
337 // the parameter was declared final * its default value (or null if none was
338 // declared) * an array of metadata (or null if no metadata was declared).
339 enum {
344 };
345
346 private:
347 DISALLOW_COPY_AND_ASSIGN(Parser);
348};
349
350} // namespace dart
351
352#endif // RUNTIME_VM_PARSER_H_
#define DEBUG_ASSERT(cond)
Definition: assert.h:321
const T & At(intptr_t index) const
intptr_t length() const
static uword Hash(Key key)
Definition: parser.h:53
const Field * Key
Definition: parser.h:45
const Field * Pair
Definition: parser.h:47
static Value ValueOf(Pair kv)
Definition: parser.h:51
const Field * Value
Definition: parser.h:46
static bool IsKeyEqual(Pair pair, Key key)
Definition: parser.h:61
static Key KeyOf(Pair kv)
Definition: parser.h:49
FieldPtr Original() const
Definition: object.cc:11739
intptr_t NumParameters() const
Definition: object.cc:8877
LocalVariable * VariableAt(intptr_t index) const
Definition: scopes.h:398
void Bailout(const char *origin, const char *reason) const
Definition: parser.cc:118
bool is_receiver_used() const
Definition: parser.h:145
void set_function_type_arguments(LocalVariable *function_type_arguments)
Definition: parser.h:91
bool has_entry_points_temp_var() const
Definition: parser.h:171
void SetRegExpCompileData(RegExpCompileData *regexp_compile_data)
Definition: parser.cc:173
LocalVariable * expression_temp_var() const
Definition: parser.h:151
ParsedFunction(Thread *thread, const Function &function)
Definition: parser.cc:47
const Function & function() const
Definition: parser.h:73
LocalVariable * RawTypeArgumentsVariable() const
Definition: parser.h:223
void record_await()
Definition: parser.h:199
ZoneGrowableArray< const Instance * > * default_parameter_values() const
Definition: parser.h:124
int num_stack_locals() const
Definition: parser.h:194
VariableIndex first_parameter_index() const
Definition: parser.h:193
LocalScope * scope() const
Definition: parser.h:76
kernel::ScopeBuildingResult * EnsureKernelScopes()
Definition: parser.cc:126
void SetRawTypeArgumentsVariable(LocalVariable *raw_type_arguments_var)
Definition: parser.h:227
void AddToGuardedFields(const Field *field) const
Definition: parser.cc:88
void set_parent_type_arguments(LocalVariable *parent_type_arguments)
Definition: parser.h:98
void set_default_parameter_values(ZoneGrowableArray< const Instance * > *list)
Definition: parser.h:109
LocalVariable * EnsureExpressionTemp()
Definition: parser.cc:134
void MarkForwardingStub(const Function *forwarding_target)
Definition: parser.h:207
const Function * forwarding_stub_super_target() const
Definition: parser.h:204
LocalVariable * parent_type_arguments() const
Definition: parser.h:95
const FieldSet * guarded_fields() const
Definition: parser.h:191
void set_expression_temp_var(LocalVariable *value)
Definition: parser.h:155
LocalVariable * EnsureEntryPointsTemp()
Definition: parser.cc:146
void EnsureFinallyReturnTemp(bool is_async)
Definition: parser.cc:158
bool has_receiver_var() const
Definition: parser.h:142
void AllocateIrregexpVariables(intptr_t num_stack_locals)
Definition: parser.cc:295
void set_receiver_used()
Definition: parser.h:144
LocalVariable * arg_desc_var() const
Definition: parser.h:131
void set_entry_points_temp_var(LocalVariable *value)
Definition: parser.h:167
LocalVariable * entry_points_temp_var() const
Definition: parser.h:163
bool IsGenericCovariantImplParameter(intptr_t i) const
Definition: parser.cc:329
void SetCovariantParameters(const BitVector *covariant_parameters)
Definition: parser.cc:308
RegExpCompileData * regexp_compile_data() const
Definition: parser.h:83
Zone * zone() const
Definition: parser.h:213
bool has_arg_desc_var() const
Definition: parser.h:130
const Instance & DefaultParameterValueAt(intptr_t i) const
Definition: parser.h:119
Thread * thread() const
Definition: parser.h:211
bool is_forwarding_stub() const
Definition: parser.h:201
Isolate * isolate() const
Definition: parser.h:212
DynamicClosureCallVars * dynamic_closure_call_vars() const
Definition: parser.h:289
void set_receiver_var(LocalVariable *value)
Definition: parser.h:137
void set_scope(LocalScope *scope)
Definition: parser.h:77
void AllocateVariables()
Definition: parser.cc:180
LocalVariable * ParameterVariable(intptr_t i) const
Definition: parser.h:239
LocalVariable * current_context_var() const
Definition: parser.h:128
void set_finally_return_temp_var(LocalVariable *value)
Definition: parser.h:179
LocalVariable * suspend_state_var() const
Definition: parser.h:103
LocalVariable * RawParameterVariable(intptr_t i) const
Definition: parser.h:235
bool IsCovariantParameter(intptr_t i) const
Definition: parser.cc:323
LocalVariable * receiver_var() const
Definition: parser.h:133
bool has_expression_temp_var() const
Definition: parser.h:159
void SetRawParameters(ZoneGrowableArray< LocalVariable * > *raw_parameters)
Definition: parser.h:231
void set_suspend_state_var(LocalVariable *suspend_state_var)
Definition: parser.h:104
LocalVariable * finally_return_temp_var() const
Definition: parser.h:175
bool HasCovariantParametersInfo() const
Definition: parser.h:254
bool has_finally_return_temp_var() const
Definition: parser.h:183
void SetGenericCovariantImplParameters(const BitVector *generic_covariant_impl_parameters)
Definition: parser.cc:315
const Code & code() const
Definition: parser.h:74
LocalVariable * function_type_arguments() const
Definition: parser.h:88
DynamicClosureCallVars * EnsureDynamicClosureCallVars()
Definition: parser.cc:336
bool have_seen_await() const
Definition: parser.h:200
@ kParameterDefaultValueOffset
Definition: parser.h:341
@ kParameterIsFinalOffset
Definition: parser.h:340
@ kParameterMetadataOffset
Definition: parser.h:342
@ kParameterEntrySize
Definition: parser.h:343
Zone * zone() const
Definition: thread_state.h:37
Isolate * isolate() const
Definition: thread.h:534
uword Hash() const
#define ASSERT(E)
uint8_t value
Definition: dart_vm.cc:33
uintptr_t uword
Definition: globals.h:501
DirectChainedHashMap< FieldKeyValueTrait > FieldSet
Definition: parser.h:66
#define FOR_EACH_DYNAMIC_CLOSURE_CALL_VARIABLE(V)
Definition: parser.h:273
#define DEFINE_FIELD(Name, _, __)
Definition: parser.h:280
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: globals.h:581
DynamicClosureCallVars(Zone *zone, intptr_t num_named)
Definition: parser.h:270
ZoneGrowableArray< LocalVariable * > named_argument_parameter_indices
Definition: parser.h:286