• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "boxingForLocals.h"
17 
18 #include "varbinder/ETSBinder.h"
19 #include "checker/ETSchecker.h"
20 
21 namespace ark::es2panda::compiler {
22 
23 static constexpr std::string_view LOWERING_NAME = "boxing-for-locals";
24 
FindCaptured(public_lib::Context * ctx,ir::ScriptFunction * func)25 static ArenaSet<varbinder::Variable *> FindCaptured(public_lib::Context *ctx, ir::ScriptFunction *func)
26 {
27     auto *allocator = ctx->allocator;
28     auto captured = ArenaSet<varbinder::Variable *>(allocator->Adapter());
29     bool withinLambda = false;
30 
31     auto innermostArrowScopes = ArenaSet<varbinder::Scope *>(allocator->Adapter());
32     innermostArrowScopes.insert(func->Scope());
33     innermostArrowScopes.insert(func->Scope()->ParamScope());
34 
35     std::function<void(ir::AstNode *)> walker = [&](ir::AstNode *ast) {
36         if (ast->IsArrowFunctionExpression() || ast->IsClassDeclaration()) {
37             auto savedWL = withinLambda;
38             auto savedScopes = ArenaSet<varbinder::Scope *>(allocator->Adapter());
39             std::swap(innermostArrowScopes, savedScopes);
40             withinLambda = true;
41 
42             ast->Iterate(walker);
43 
44             withinLambda = savedWL;
45             std::swap(innermostArrowScopes, savedScopes);
46 
47             return;
48         }
49 
50         if (withinLambda && ast->IsScopeBearer()) {
51             innermostArrowScopes.insert(ast->Scope());
52             if (ast->Scope()->IsFunctionScope()) {
53                 innermostArrowScopes.insert(ast->Scope()->AsFunctionScope()->ParamScope());
54             }
55             if (ast->Scope()->IsCatchScope()) {
56                 innermostArrowScopes.insert(ast->Scope()->AsCatchScope()->ParamScope());
57             }
58             if (ast->Scope()->IsLoopScope()) {
59                 innermostArrowScopes.insert(ast->Scope()->AsLoopScope()->DeclScope());
60             }
61         } else if (withinLambda && ast->IsIdentifier()) {
62             auto *var = ast->AsIdentifier()->Variable();
63             if (var == nullptr) {
64                 return;
65             }
66             auto *scope = var->GetScope();
67             if (scope != nullptr && !scope->IsClassScope() && !scope->IsGlobalScope() &&
68                 innermostArrowScopes.count(scope) == 0) {
69                 captured.insert(var);
70             }
71         }
72 
73         ast->Iterate(walker);
74     };
75 
76     func->Iterate(walker);
77 
78     auto varsToBox = ArenaSet<varbinder::Variable *>(allocator->Adapter());
79     return captured;
80 }
81 
FindModified(public_lib::Context * ctx,ir::ScriptFunction * func)82 static ArenaSet<varbinder::Variable *> FindModified(public_lib::Context *ctx, ir::ScriptFunction *func)
83 {
84     auto *allocator = ctx->allocator;
85     auto modified = ArenaSet<varbinder::Variable *>(allocator->Adapter());
86 
87     std::function<void(ir::AstNode *)> walker = [&](ir::AstNode *ast) {
88         if (ast->IsAssignmentExpression()) {
89             auto assignment = ast->AsAssignmentExpression();
90             if (assignment->Left()->IsIdentifier()) {
91                 ASSERT(assignment->Left()->Variable() != nullptr);
92                 auto *var = assignment->Left()->Variable();
93                 modified.insert(var);
94             }
95         }
96     };
97 
98     func->IterateRecursively(walker);
99     return modified;
100 }
101 
FindVariablesToBox(public_lib::Context * ctx,ir::ScriptFunction * func)102 static ArenaSet<varbinder::Variable *> FindVariablesToBox(public_lib::Context *ctx, ir::ScriptFunction *func)
103 {
104     auto *allocator = ctx->allocator;
105     auto captured = FindCaptured(ctx, func);
106     auto modified = FindModified(ctx, func);
107 
108     auto varsToBox = ArenaSet<varbinder::Variable *>(allocator->Adapter());
109     std::set_intersection(captured.cbegin(), captured.cend(), modified.cbegin(), modified.cend(),
110                           std::inserter(varsToBox, varsToBox.begin()));
111 
112     return varsToBox;
113 }
114 
HandleFunctionParam(public_lib::Context * ctx,ir::ETSParameterExpression * param,ArenaMap<varbinder::Variable *,varbinder::Variable * > * varsMap)115 static void HandleFunctionParam(public_lib::Context *ctx, ir::ETSParameterExpression *param,
116                                 ArenaMap<varbinder::Variable *, varbinder::Variable *> *varsMap)
117 {
118     auto *allocator = ctx->allocator;
119     auto *checker = ctx->checker->AsETSChecker();
120     auto *varBinder = checker->VarBinder();
121 
122     auto *id = param->Ident()->AsIdentifier();
123     auto *oldVar = id->Variable();
124     auto *oldType = oldVar->TsType();
125     auto *func = param->Parent()->AsScriptFunction();
126     ASSERT(func->Body()->IsBlockStatement());  // guaranteed after expressionLambdaLowering
127     auto *body = func->Body()->AsBlockStatement();
128     auto &bodyStmts = body->Statements();
129     auto *scope = body->Scope();
130 
131     auto *initId = allocator->New<ir::Identifier>(id->Name(), allocator);
132     initId->SetVariable(id->Variable());
133     initId->SetTsType(oldType);
134 
135     // The new variable will have the same name as the parameter. This is not representable in source code.
136     auto *boxedType = checker->GlobalBuiltinBoxType(oldType);
137     ArenaVector<ir::Expression *> newInitArgs {allocator->Adapter()};
138     newInitArgs.push_back(initId);
139     auto *newInit = util::NodeAllocator::ForceSetParent<ir::ETSNewClassInstanceExpression>(
140         allocator, allocator->New<ir::OpaqueTypeNode>(boxedType), std::move(newInitArgs), nullptr);
141     auto *newDeclarator = util::NodeAllocator::ForceSetParent<ir::VariableDeclarator>(
142         allocator, ir::VariableDeclaratorFlag::CONST, allocator->New<ir::Identifier>(id->Name(), allocator), newInit);
143     ArenaVector<ir::VariableDeclarator *> declVec {allocator->Adapter()};
144     declVec.push_back(newDeclarator);
145 
146     auto *newDecl = allocator->New<varbinder::ConstDecl>(id->Name(), newDeclarator);
147     auto *newVar = allocator->New<varbinder::LocalVariable>(newDecl, oldVar->Flags());
148     newVar->SetTsType(boxedType);
149 
150     newDeclarator->Id()->AsIdentifier()->SetVariable(newVar);
151     newVar->SetScope(scope);
152     scope->EraseBinding(newVar->Name());
153     scope->InsertBinding(newVar->Name(), newVar);
154 
155     auto *newDeclaration = util::NodeAllocator::ForceSetParent<ir::VariableDeclaration>(
156         allocator, ir::VariableDeclaration::VariableDeclarationKind::CONST, allocator, std::move(declVec), false);
157     newDeclaration->SetParent(body);
158     bodyStmts.insert(bodyStmts.begin(), newDeclaration);
159 
160     auto lexScope = varbinder::LexicalScope<varbinder::Scope>::Enter(varBinder, scope);
161     auto savedContext = checker::SavedCheckerContext(checker, checker::CheckerStatus::NO_OPTS);
162     auto scopeContext = checker::ScopeContext(checker, scope);
163 
164     newDeclaration->Check(checker);
165 
166     varsMap->emplace(oldVar, newVar);
167 }
168 
HandleVariableDeclarator(public_lib::Context * ctx,ir::VariableDeclarator * declarator,ArenaMap<varbinder::Variable *,varbinder::Variable * > * varsMap)169 static ir::AstNode *HandleVariableDeclarator(public_lib::Context *ctx, ir::VariableDeclarator *declarator,
170                                              ArenaMap<varbinder::Variable *, varbinder::Variable *> *varsMap)
171 {
172     auto *allocator = ctx->allocator;
173     auto *checker = ctx->checker->AsETSChecker();
174     auto *varBinder = checker->VarBinder();
175 
176     auto *id = declarator->Id()->AsIdentifier();
177     auto *oldVar = id->Variable();
178     auto *scope = oldVar->GetScope();
179     auto *type = oldVar->TsType();
180     auto *boxedType = checker->GlobalBuiltinBoxType(type);
181 
182     auto initArgs = ArenaVector<ir::Expression *>(allocator->Adapter());
183     if (declarator->Init() != nullptr) {
184         auto *arg = declarator->Init();
185         if (arg->TsType() != type) {
186             arg = util::NodeAllocator::ForceSetParent<ir::TSAsExpression>(
187                 allocator, arg, allocator->New<ir::OpaqueTypeNode>(type), false);
188         }
189         initArgs.push_back(arg);
190     }
191     auto *newInit = util::NodeAllocator::ForceSetParent<ir::ETSNewClassInstanceExpression>(
192         allocator, allocator->New<ir::OpaqueTypeNode>(boxedType), std::move(initArgs), nullptr);
193     auto *newDeclarator = util::NodeAllocator::ForceSetParent<ir::VariableDeclarator>(
194         allocator, declarator->Flag(), allocator->New<ir::Identifier>(id->Name(), allocator), newInit);
195     newDeclarator->SetParent(declarator->Parent());
196 
197     auto *newDecl = allocator->New<varbinder::ConstDecl>(oldVar->Name(), newDeclarator);
198     auto *newVar = allocator->New<varbinder::LocalVariable>(newDecl, oldVar->Flags());
199     newDeclarator->Id()->AsIdentifier()->SetVariable(newVar);
200     newVar->SetScope(scope);
201 
202     scope->EraseBinding(oldVar->Name());
203     scope->InsertBinding(newVar->Name(), newVar);
204 
205     auto lexScope = varbinder::LexicalScope<varbinder::Scope>::Enter(varBinder, scope);
206     auto savedContext = checker::SavedCheckerContext(checker, checker::CheckerStatus::NO_OPTS);
207     auto scopeContext = checker::ScopeContext(checker, scope);
208 
209     newDeclarator->Check(checker);
210 
211     varsMap->emplace(oldVar, newVar);
212 
213     return newDeclarator;
214 }
215 
IsBeingDeclared(ir::AstNode * ast)216 static bool IsBeingDeclared(ir::AstNode *ast)
217 {
218     ASSERT(ast->IsIdentifier());
219     return (ast->Parent()->IsVariableDeclarator() && ast == ast->Parent()->AsVariableDeclarator()->Id()) ||
220            (ast->Parent()->IsETSParameterExpression() && ast == ast->Parent()->AsETSParameterExpression()->Ident());
221 }
222 
IsPartOfBoxInitializer(public_lib::Context * ctx,ir::AstNode * ast)223 static bool IsPartOfBoxInitializer(public_lib::Context *ctx, ir::AstNode *ast)
224 {
225     ASSERT(ast->IsIdentifier());
226     auto *checker = ctx->checker->AsETSChecker();
227     auto *id = ast->AsIdentifier();
228 
229     // NOTE(gogabr): rely on caching for type instantiations, so we can use pointer comparison.
230     return id->Parent()->IsETSNewClassInstanceExpression() &&
231            id->Parent()->AsETSNewClassInstanceExpression()->GetTypeRef()->TsType() ==
232                checker->GlobalBuiltinBoxType(id->TsType());
233 }
234 
OnLeftSideOfAssignment(ir::AstNode * ast)235 static bool OnLeftSideOfAssignment(ir::AstNode *ast)
236 {
237     return ast->Parent()->IsAssignmentExpression() && ast->Parent()->AsAssignmentExpression()->Left() == ast;
238 }
239 
HandleReference(public_lib::Context * ctx,ir::Identifier * id,varbinder::Variable * var)240 static ir::AstNode *HandleReference(public_lib::Context *ctx, ir::Identifier *id, varbinder::Variable *var)
241 {
242     auto *parser = ctx->parser->AsETSParser();
243     auto *checker = ctx->checker->AsETSChecker();
244 
245     // `as` is needed to account for smart types
246     auto *res = parser->CreateFormattedExpression("@@I1.get() as @@T2", var->Name(), id->TsType());
247     res->SetParent(id->Parent());
248     res->AsTSAsExpression()
249         ->Expr()
250         ->AsCallExpression()
251         ->Callee()
252         ->AsMemberExpression()
253         ->Object()
254         ->AsIdentifier()
255         ->SetVariable(var);
256 
257     // NOTE(gogabr) -- The `get` property remains without variable; this is OK for the current checker, but may need
258     // adjustment later.
259     res->Check(checker);
260 
261     ASSERT(res->TsType() == id->TsType());
262     res->SetBoxingUnboxingFlags(id->GetBoxingUnboxingFlags());
263 
264     return res;
265 }
266 
HandleAssignment(public_lib::Context * ctx,ir::AssignmentExpression * ass,ArenaMap<varbinder::Variable *,varbinder::Variable * > const & varsMap)267 static ir::AstNode *HandleAssignment(public_lib::Context *ctx, ir::AssignmentExpression *ass,
268                                      ArenaMap<varbinder::Variable *, varbinder::Variable *> const &varsMap)
269 {
270     // Should be true after opAssignment lowering
271     ASSERT(ass->OperatorType() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION);
272 
273     auto *parser = ctx->parser->AsETSParser();
274     auto *varBinder = ctx->checker->VarBinder()->AsETSBinder();
275     auto *checker = ctx->checker->AsETSChecker();
276 
277     auto *oldVar = ass->Left()->AsIdentifier()->Variable();
278     auto *newVar = varsMap.find(oldVar)->second;
279     auto *scope = newVar->GetScope();
280     auto *res = parser->CreateFormattedExpression("@@I1.set(@@E2 as @@T3) as @@T4", newVar->Name(), ass->Right(),
281                                                   oldVar->TsType(), ass->TsType());
282     res->SetParent(ass->Parent());
283 
284     // NOTE(gogabr) -- The `get` and `set` properties remain without variable; this is OK for the current checker, but
285     // may need adjustment later.
286     auto lexScope = varbinder::LexicalScope<varbinder::Scope>::Enter(varBinder, scope);
287     auto savedContext = checker::SavedCheckerContext(checker, checker::CheckerStatus::NO_OPTS);
288     auto scopeContext = checker::ScopeContext(checker, scope);
289 
290     varBinder->ResolveReferencesForScopeWithContext(res, scope);
291     res->Check(checker);
292 
293     ASSERT(res->TsType() == ass->TsType());
294     res->SetBoxingUnboxingFlags(ass->GetBoxingUnboxingFlags());
295 
296     return res;
297 }
298 
HandleScriptFunction(public_lib::Context * ctx,ir::ScriptFunction * func)299 static void HandleScriptFunction(public_lib::Context *ctx, ir::ScriptFunction *func)
300 {
301     auto *allocator = ctx->allocator;
302     auto varsToBox = FindVariablesToBox(ctx, func);
303     if (varsToBox.empty()) {
304         return;
305     }
306     auto varsMap = ArenaMap<varbinder::Variable *, varbinder::Variable *>(allocator->Adapter());
307 
308     /*
309       The function relies on the following facts:
310       - TransformChildrenRecursively handles children in order
311       - local variables are never used before declaration.
312       This ensures that varsToMap has the appropriate record by the time the variable reference is processed.
313     */
314     auto handleNode = [ctx, &varsToBox, &varsMap](ir::AstNode *ast) {
315         if (ast->IsETSParameterExpression() && varsToBox.count(ast->AsETSParameterExpression()->Variable()) > 0) {
316             HandleFunctionParam(ctx, ast->AsETSParameterExpression(), &varsMap);
317             return ast;  // modifications occur in the containing function
318         }
319         if (ast->IsVariableDeclarator() && ast->AsVariableDeclarator()->Id()->IsIdentifier() &&
320             varsToBox.count(ast->AsVariableDeclarator()->Id()->AsIdentifier()->Variable()) > 0) {
321             return HandleVariableDeclarator(ctx, ast->AsVariableDeclarator(), &varsMap);
322         }
323         if (ast->IsAssignmentExpression() && ast->AsAssignmentExpression()->Left()->IsIdentifier() &&
324             varsToBox.count(ast->AsAssignmentExpression()->Left()->AsIdentifier()->Variable()) > 0) {
325             return HandleAssignment(ctx, ast->AsAssignmentExpression(), varsMap);
326         }
327         if (ast->IsIdentifier() && !IsBeingDeclared(ast) && !IsPartOfBoxInitializer(ctx, ast) &&
328             !OnLeftSideOfAssignment(ast) && varsToBox.count(ast->AsIdentifier()->Variable()) > 0) {
329             return HandleReference(ctx, ast->AsIdentifier(), varsMap.find(ast->AsIdentifier()->Variable())->second);
330         }
331 
332         return ast;
333     };
334 
335     func->TransformChildrenRecursivelyPostorder(handleNode, LOWERING_NAME);
336 }
337 
Perform(public_lib::Context * ctx,parser::Program * program)338 bool BoxingForLocals::Perform(public_lib::Context *ctx, parser::Program *program)
339 {
340     parser::SavedFormattingFileName savedFormattingName(ctx->parser->AsETSParser(), "boxing-for-lambdas");
341 
342     if (ctx->config->options->CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB) {
343         for (auto &[_, ext_programs] : program->ExternalSources()) {
344             (void)_;
345             for (auto *extProg : ext_programs) {
346                 Perform(ctx, extProg);
347             }
348         }
349     }
350 
351     std::function<void(ir::AstNode *)> searchForFunctions = [&](ir::AstNode *ast) {
352         if (ast->IsScriptFunction()) {
353             HandleScriptFunction(ctx, ast->AsScriptFunction());  // no recursion
354         } else {
355             ast->Iterate(searchForFunctions);
356         }
357     };
358     program->Ast()->Iterate(searchForFunctions);
359     return true;
360 }
361 
Postcondition(public_lib::Context * ctx,parser::Program const * program)362 bool BoxingForLocals::Postcondition([[maybe_unused]] public_lib::Context *ctx, parser::Program const *program)
363 {
364     (void)program;
365     return true;
366 }
367 
368 }  // namespace ark::es2panda::compiler
369