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#include "vm/compiler/frontend/bytecode_scope_builder.h"
6
7#include "vm/compiler/frontend/bytecode_reader.h"
8
9namespace dart {
10namespace kernel {
11
12#define Z (zone_)
13
14BytecodeScopeBuilder::BytecodeScopeBuilder(ParsedFunction* parsed_function)
15 : parsed_function_(parsed_function),
16 zone_(parsed_function->zone()),
17 scope_(nullptr) {}
18
19void BytecodeScopeBuilder::BuildScopes() {
20 if (parsed_function_->scope() != nullptr) {
21 return; // Scopes are already built.
22 }
23
24 const Function& function = parsed_function_->function();
25
26 LocalScope* enclosing_scope = nullptr;
27 if (function.IsImplicitClosureFunction() && !function.is_static()) {
28 // Create artificial enclosing scope for the tear-off that contains
29 // captured receiver value. This ensure that AssertAssignable will correctly
30 // load instantiator type arguments if they are needed.
31 LocalVariable* receiver_variable =
32 MakeReceiverVariable(/* is_parameter = */ false);
33 receiver_variable->set_is_captured();
34 enclosing_scope = new (Z) LocalScope(NULL, 0, 0);
35 enclosing_scope->set_context_level(0);
36 enclosing_scope->AddVariable(receiver_variable);
37 enclosing_scope->AddContextVariable(receiver_variable);
38 }
39 scope_ = new (Z) LocalScope(enclosing_scope, 0, 0);
40 scope_->set_begin_token_pos(function.token_pos());
41 scope_->set_end_token_pos(function.end_token_pos());
42
43 // Add function type arguments variable before current context variable.
44 if ((function.IsGeneric() || function.HasGenericParent())) {
45 LocalVariable* type_args_var = MakeVariable(
46 Symbols::FunctionTypeArgumentsVar(), AbstractType::dynamic_type());
47 scope_->AddVariable(type_args_var);
48 parsed_function_->set_function_type_arguments(type_args_var);
49 }
50
51 bool needs_expr_temp = false;
52 if (parsed_function_->has_arg_desc_var()) {
53 needs_expr_temp = true;
54 scope_->AddVariable(parsed_function_->arg_desc_var());
55 }
56
57 LocalVariable* context_var = parsed_function_->current_context_var();
58 context_var->set_is_forced_stack();
59 scope_->AddVariable(context_var);
60
61 parsed_function_->set_scope(scope_);
62
63 switch (function.kind()) {
64 case FunctionLayout::kImplicitClosureFunction: {
65 ASSERT(function.NumImplicitParameters() == 1);
66
67 LocalVariable* closure_parameter = MakeVariable(
68 Symbols::ClosureParameter(), AbstractType::dynamic_type());
69 closure_parameter->set_is_forced_stack();
70 scope_->InsertParameterAt(0, closure_parameter);
71
72 // Type check all parameters by default.
73 // This may be overridden with parameter flags in
74 // BytecodeReaderHelper::ParseForwarderFunction.
75 AddParameters(function, LocalVariable::kDoTypeCheck);
76 break;
77 }
78
79 case FunctionLayout::kImplicitGetter:
80 case FunctionLayout::kImplicitSetter: {
81 const bool is_setter = function.IsImplicitSetterFunction();
82 const bool is_method = !function.IsStaticFunction();
83 const Field& field = Field::Handle(Z, function.accessor_field());
84 intptr_t pos = 0;
85 if (is_method) {
86 MakeReceiverVariable(/* is_parameter = */ true);
87 ++pos;
88 }
89 if (is_setter) {
90 LocalVariable* setter_value = MakeVariable(
91 Symbols::Value(),
92 AbstractType::ZoneHandle(Z, function.ParameterTypeAt(pos)));
93 scope_->InsertParameterAt(pos++, setter_value);
94
95 if (is_method) {
96 if (field.is_covariant()) {
97 setter_value->set_is_explicit_covariant_parameter();
98 } else {
99 const bool needs_type_check =
100 field.is_generic_covariant_impl() &&
101 kernel::ProcedureAttributesOf(field, Z).has_non_this_uses;
102 if (!needs_type_check) {
103 setter_value->set_type_check_mode(
104 LocalVariable::kTypeCheckedByCaller);
105 }
106 }
107 }
108 }
109 break;
110 }
111 case FunctionLayout::kImplicitStaticGetter: {
112 ASSERT(!IsStaticFieldGetterGeneratedAsInitializer(function, Z));
113 break;
114 }
115 case FunctionLayout::kDynamicInvocationForwarder: {
116 // Create [this] variable.
117 MakeReceiverVariable(/* is_parameter = */ true);
118
119 // Type check all parameters by default.
120 // This may be overridden with parameter flags in
121 // BytecodeReaderHelper::ParseForwarderFunction.
122 AddParameters(function, LocalVariable::kDoTypeCheck);
123 break;
124 }
125 case FunctionLayout::kMethodExtractor: {
126 // Add a receiver parameter. Though it is captured, we emit code to
127 // explicitly copy it to a fixed offset in a freshly-allocated context
128 // instead of using the generic code for regular functions.
129 // Therefore, it isn't necessary to mark it as captured here.
130 MakeReceiverVariable(/* is_parameter = */ true);
131 break;
132 }
133 default:
134 UNREACHABLE();
135 }
136
137 if (needs_expr_temp) {
138 scope_->AddVariable(parsed_function_->EnsureExpressionTemp());
139 }
140 if (parsed_function_->function().MayHaveUncheckedEntryPoint()) {
141 scope_->AddVariable(parsed_function_->EnsureEntryPointsTemp());
142 }
143 parsed_function_->AllocateVariables();
144}
145
146// TODO(alexmarkov): pass bitvectors of parameter covariance to set type
147// check mode before AllocateVariables.
148void BytecodeScopeBuilder::AddParameters(const Function& function,
149 LocalVariable::TypeCheckMode mode) {
150 for (intptr_t i = function.NumImplicitParameters(),
151 n = function.NumParameters();
152 i < n; ++i) {
153 // LocalVariable caches handles, so new handles are created for each
154 // parameter.
155 String& name = String::ZoneHandle(Z, function.ParameterNameAt(i));
156 AbstractType& type =
157 AbstractType::ZoneHandle(Z, function.ParameterTypeAt(i));
158
159 LocalVariable* variable = MakeVariable(name, type);
160 variable->set_type_check_mode(mode);
161 scope_->InsertParameterAt(i, variable);
162 }
163}
164
165LocalVariable* BytecodeScopeBuilder::MakeVariable(const String& name,
166 const AbstractType& type) {
167 return new (Z) LocalVariable(TokenPosition::kNoSource,
168 TokenPosition::kNoSource, name, type, nullptr);
169}
170
171LocalVariable* BytecodeScopeBuilder::MakeReceiverVariable(bool is_parameter) {
172 const auto& cls = Class::Handle(Z, parsed_function_->function().Owner());
173 const auto& type = Type::ZoneHandle(Z, cls.DeclarationType());
174 LocalVariable* receiver_variable = MakeVariable(Symbols::This(), type);
175 parsed_function_->set_receiver_var(receiver_variable);
176 if (is_parameter) {
177 scope_->InsertParameterAt(0, receiver_variable);
178 }
179 return receiver_variable;
180}
181
182} // namespace kernel
183} // namespace dart
184