Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
scope_builder.h
Go to the documentation of this file.
1// Copyright (c) 2018, 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_COMPILER_FRONTEND_SCOPE_BUILDER_H_
6#define RUNTIME_VM_COMPILER_FRONTEND_SCOPE_BUILDER_H_
7
8#if defined(DART_PRECOMPILED_RUNTIME)
9#error "AOT runtime should not use compiler sources (including header files)"
10#endif // defined(DART_PRECOMPILED_RUNTIME)
11
14#include "vm/hash_map.h"
15#include "vm/object.h"
16#include "vm/parser.h" // For ParsedFunction.
17
18namespace dart {
19namespace kernel {
20
21class ScopeBuildingResult;
22
24 public:
25 explicit ScopeBuilder(ParsedFunction* parsed_function);
26
27 virtual ~ScopeBuilder() = default;
28
30
31 private:
32 void VisitField();
33
34 void VisitProcedure();
35
36 void VisitConstructor();
37
38 void VisitFunctionNode();
39 void VisitNode();
40 void VisitInitializer();
41 void VisitExpression();
42 void VisitStatement();
43 void VisitListOfExpressions();
44 void VisitListOfNamedExpressions();
45 void VisitArguments();
46 void VisitVariableDeclaration();
47 void VisitVariableGet(intptr_t declaration_binary_offset);
48 void VisitDartType();
49 void VisitInterfaceType(bool simple);
50 void VisitFunctionType(bool simple);
51 void VisitRecordType();
52 void VisitTypeParameterType();
53 void VisitIntersectionType();
54 void VisitExtensionType();
55 void VisitFutureOrType();
56 void HandleLocalFunction(intptr_t parent_kernel_offset);
57
58 AbstractType& BuildAndVisitVariableType();
59
60 void EnterScope(intptr_t kernel_offset);
61 void ExitScope(TokenPosition start_position, TokenPosition end_position);
62
63 virtual void ReportUnexpectedTag(const char* variant, Tag tag);
64
65 // This enum controls which parameters would be marked as requiring type
66 // check on the callee side.
67 enum ParameterTypeCheckMode {
68 // All parameters will be checked.
69 kTypeCheckAllParameters,
70
71 // Only parameters marked as covariant or generic-covariant-impl will be
72 // checked.
73 kTypeCheckForNonDynamicallyInvokedMethod,
74
75 // Only parameters *not* marked as covariant or generic-covariant-impl will
76 // be checked. The rest would be checked in the method itself.
77 // Inverse of kTypeCheckForNonDynamicallyInvokedMethod.
78 kTypeCheckEverythingNotCheckedInNonDynamicallyInvokedMethod,
79
80 // No parameters will be checked.
81 kTypeCheckForStaticFunction,
82
83 // No non-covariant checks are performed, and any covariant checks are
84 // performed by the target.
85 kTypeCheckForImplicitClosureFunction,
86 };
87
88 // This assumes that the reader is at a FunctionNode,
89 // about to read the positional parameters.
90 void AddPositionalAndNamedParameters(
91 intptr_t pos,
92 ParameterTypeCheckMode type_check_mode,
93 const ProcedureAttributesMetadata& attrs);
94
95 // This assumes that the reader is at a FunctionNode,
96 // about to read a parameter (i.e. VariableDeclaration).
97 void AddVariableDeclarationParameter(
98 intptr_t pos,
99 ParameterTypeCheckMode type_check_mode,
100 const ProcedureAttributesMetadata& attrs);
101
102 LocalVariable* MakeVariable(
103 TokenPosition declaration_pos,
104 TokenPosition token_pos,
105 const String& name,
106 const AbstractType& type,
107 intptr_t kernel_offset = LocalVariable::kNoKernelOffset,
108 bool is_late = false,
109 const InferredTypeMetadata* inferred_type_md = nullptr,
110 const InferredTypeMetadata* inferred_arg_type_md = nullptr);
111
112 void AddExceptionVariable(GrowableArray<LocalVariable*>* variables,
113 const char* prefix,
114 intptr_t nesting_depth);
115
116 void FinalizeExceptionVariable(GrowableArray<LocalVariable*>* variables,
117 GrowableArray<LocalVariable*>* raw_variables,
118 const String& symbol,
119 intptr_t nesting_depth);
120
121 void AddTryVariables();
122 void AddCatchVariables();
123 void FinalizeCatchVariables();
124 void AddSwitchVariable();
125
126 // Record an assignment or reference to a variable. If the occurrence is
127 // in a nested function, ensure that the variable is handled properly as a
128 // captured variable.
129 LocalVariable* LookupVariable(intptr_t declaration_binary_offset);
130
131 StringIndex GetNameFromVariableDeclaration(intptr_t kernel_offset,
132 const Function& function);
133
134 const String& GenerateName(const char* prefix, intptr_t suffix);
135
136 void HandleLoadReceiver();
137 void HandleSpecialLoad(LocalVariable** variable,
138 const String& symbol,
139 intptr_t kernel_offset);
140
141 ScriptPtr Script() { return active_class_.ActiveScript(); }
142
143 struct DepthState {
144 explicit DepthState(intptr_t function)
145 : loop_(0), function_(function), try_(0), catch_(0), finally_(0) {}
146
147 intptr_t loop_;
148 intptr_t function_;
149 intptr_t try_;
150 intptr_t catch_;
151 intptr_t finally_;
152 };
153
154 ScopeBuildingResult* result_;
155 ParsedFunction* parsed_function_;
156
157 ActiveClass active_class_;
158
159 TranslationHelper translation_helper_;
160 Zone* zone_;
161
162 LocalScope* current_function_scope_;
163 LocalScope* scope_;
164 DepthState depth_;
165
166 intptr_t name_index_;
167
168 bool needs_expr_temp_;
169 TokenPosition first_body_token_position_ = TokenPosition::kNoSource;
170
171 KernelReaderHelper helper_;
172 ConstantReader constant_reader_;
173 InferredTypeMetadataHelper inferred_type_metadata_helper_;
174 InferredTypeMetadataHelper inferred_arg_type_metadata_helper_;
175 ProcedureAttributesMetadataHelper procedure_attributes_metadata_helper_;
176 TypeTranslator type_translator_;
177
178 DISALLOW_COPY_AND_ASSIGN(ScopeBuilder);
179};
180
185
187 public:
194
195 bool IsClosureWithEmptyContext(intptr_t function_node_offset) {
196 for (intptr_t i = 0; i < closure_offsets_without_captures.length(); ++i) {
197 if (closure_offsets_without_captures[i] == function_node_offset) {
198 return true;
199 }
200 }
201 return false;
202 }
203
207
208 // Only non-null for factory constructor functions.
210
211 // Non-nullptr when the function contains a switch statement.
213
214 // Non-nullptr when the function contains a return inside a finally block.
216
217 // Non-nullptr when the function is a setter.
219
220 // Variables used in exception handlers, one per exception handler nesting
221 // level.
225
226 // These are used to access the raw exception/stacktrace variables (and are
227 // used to put them into the captured variables in the context).
231
232 // Remembers closure function kernel offsets that do not capture any
233 // variables.
235
236 private:
238};
239
240} // namespace kernel
241} // namespace dart
242
243#endif // RUNTIME_VM_COMPILER_FRONTEND_SCOPE_BUILDER_H_
SkPoint pos
intptr_t length() const
static constexpr intptr_t kNoKernelOffset
Definition scopes.h:77
virtual ~ScopeBuilder()=default
virtual void ReportUnexpectedTag(const char *variant, Tag tag)
ScopeBuildingResult * BuildScopes()
IntMap< LocalScope * > scopes
GrowableArray< LocalVariable * > catch_context_variables
GrowableArray< LocalVariable * > stack_trace_variables
GrowableArray< LocalVariable * > raw_exception_variables
bool IsClosureWithEmptyContext(intptr_t function_node_offset)
GrowableArray< intptr_t > closure_offsets_without_captures
GrowableArray< LocalVariable * > exception_variables
IntMap< LocalVariable * > locals
GrowableArray< LocalVariable * > raw_stack_trace_variables
GrowableArray< FunctionScope > function_scopes
Dart_NativeFunction function
Definition fuchsia.cc:51
const char *const name
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581