1 /*
2 * Copyright (c) 2021-2025 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 "optionalLowering.h"
17 #include "checker/ETSchecker.h"
18 #include "compiler/lowering/util.h"
19 #include "compiler/lowering/scopesInit/scopesInitPhase.h"
20 #include "ir/statements/blockStatement.h"
21 #include "ir/expressions/memberExpression.h"
22 #include "parser/ETSparser.h"
23 #include "varbinder/ETSBinder.h"
24
25 namespace ark::es2panda::compiler {
26
Name() const27 std::string_view OptionalLowering::Name() const
28 {
29 return "OptionalLowering";
30 }
31
32 template <typename Expr, typename GetSource, typename SetSource>
LowerOptionalExpr(GetSource const & getSource,SetSource const & setSource,public_lib::Context * ctx,Expr * const expr,ir::ChainExpression * const chain)33 static ir::AstNode *LowerOptionalExpr(GetSource const &getSource, SetSource const &setSource, public_lib::Context *ctx,
34 Expr *const expr, ir::ChainExpression *const chain)
35 {
36 auto *const allocator = ctx->allocator;
37 auto *const parser = ctx->parser->AsETSParser();
38 auto *const varbinder = ctx->parserProgram->VarBinder();
39
40 auto expressionCtx = varbinder::LexicalScope<varbinder::Scope>::Enter(varbinder, NearestScope(expr));
41 auto *tmpIdent = Gensym(allocator);
42 ES2PANDA_ASSERT(tmpIdent != nullptr);
43 auto *tmpIdentClone = tmpIdent->Clone(allocator, nullptr);
44
45 // '0's act as placeholders
46 auto *sequenceExpr = parser->CreateFormattedExpression(
47 "let @@I1 = 0;"
48 "(@@I2 == null ? undefined : 0);",
49 tmpIdent, tmpIdentClone);
50 sequenceExpr->SetParent(chain->Parent());
51 InitScopesPhaseETS::RunExternalNode(sequenceExpr, ctx->parserProgram->VarBinder());
52
53 auto const &stmts = sequenceExpr->AsBlockExpression()->Statements();
54 stmts[0]->AsVariableDeclaration()->Declarators()[0]->SetInit(getSource(expr));
55 stmts[1]->AsExpressionStatement()->GetExpression()->AsConditionalExpression()->SetAlternate(chain->GetExpression());
56
57 setSource(expr, parser->CreateFormattedExpression("@@I1", tmpIdentClone->Clone(allocator, nullptr)));
58 return sequenceExpr;
59 }
60
LowerExpression(public_lib::Context * ctx,ir::MemberExpression * const expr,ir::ChainExpression * chain)61 static ir::AstNode *LowerExpression(public_lib::Context *ctx, ir::MemberExpression *const expr,
62 ir::ChainExpression *chain)
63 {
64 ES2PANDA_ASSERT(expr->IsOptional());
65 expr->ClearOptional();
66 return LowerOptionalExpr<ir::MemberExpression>([](auto *e) { return e->Object(); },
67 [](auto *e, auto *obj) { e->SetObject(obj); }, ctx, expr, chain);
68 }
69
LowerExpression(public_lib::Context * ctx,ir::CallExpression * const expr,ir::ChainExpression * chain)70 static ir::AstNode *LowerExpression(public_lib::Context *ctx, ir::CallExpression *const expr,
71 ir::ChainExpression *chain)
72 {
73 ES2PANDA_ASSERT(expr->IsOptional());
74 expr->ClearOptional();
75 return LowerOptionalExpr<ir::CallExpression>([](auto *e) { return e->Callee(); },
76 [](auto *e, auto *callee) { e->SetCallee(callee); }, ctx, expr, chain);
77 }
78
FindOptionalInChain(ir::Expression * expr)79 static ir::Expression *FindOptionalInChain(ir::Expression *expr)
80 {
81 if (expr->IsMemberExpression()) {
82 auto typed = expr->AsMemberExpression();
83 return typed->IsOptional() ? typed : FindOptionalInChain(typed->Object());
84 }
85 if (expr->IsCallExpression()) {
86 auto typed = expr->AsCallExpression();
87 return typed->IsOptional() ? typed : FindOptionalInChain(typed->Callee());
88 }
89 if (expr->IsTSNonNullExpression()) {
90 return FindOptionalInChain(expr->AsTSNonNullExpression()->Expr());
91 }
92 ES2PANDA_UNREACHABLE();
93 }
94
LowerChain(public_lib::Context * ctx,ir::ChainExpression * const chain)95 static ir::AstNode *LowerChain(public_lib::Context *ctx, ir::ChainExpression *const chain)
96 {
97 auto optional = FindOptionalInChain(chain->GetExpression());
98 if (optional->IsMemberExpression()) {
99 return LowerExpression(ctx, optional->AsMemberExpression(), chain);
100 }
101 if (optional->IsCallExpression()) {
102 return LowerExpression(ctx, optional->AsCallExpression(), chain);
103 }
104 ES2PANDA_UNREACHABLE();
105 }
106
PerformForModule(public_lib::Context * ctx,parser::Program * program)107 bool OptionalLowering::PerformForModule(public_lib::Context *ctx, parser::Program *program)
108 {
109 program->Ast()->TransformChildrenRecursively(
110 // CC-OFFNXT(G.FMT.14-CPP) project code style
111 [ctx](ir::AstNode *const node) -> ir::AstNode * {
112 if (node->IsChainExpression()) {
113 return RefineSourceRanges(LowerChain(ctx, node->AsChainExpression()));
114 }
115 return node;
116 },
117 Name());
118
119 return true;
120 }
121
PostconditionForModule(public_lib::Context * ctx,const parser::Program * program)122 bool OptionalLowering::PostconditionForModule([[maybe_unused]] public_lib::Context *ctx, const parser::Program *program)
123 {
124 return !program->Ast()->IsAnyChild([](const ir::AstNode *node) {
125 return node->IsChainExpression() || (node->IsMemberExpression() && node->AsMemberExpression()->IsOptional()) ||
126 (node->IsCallExpression() && node->AsCallExpression()->IsOptional());
127 });
128 }
129
130 } // namespace ark::es2panda::compiler
131