• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "stringConstantsLowering.h"
17 #include "checker/ETSchecker.h"
18 
19 namespace ark::es2panda::compiler {
20 
Name() const21 std::string_view StringConstantsLowering::Name() const
22 {
23     return "StringConstantsLowering";
24 }
25 
FoldConcat(public_lib::Context * ctx,ir::BinaryExpression * const concat)26 static ir::AstNode *FoldConcat(public_lib::Context *ctx, ir::BinaryExpression *const concat)
27 {
28     auto const lhs = concat->Left()->AsStringLiteral();
29     auto const rhs = concat->Right()->AsStringLiteral();
30     auto const resStr = util::UString(lhs->Str().Mutf8() + rhs->Str().Mutf8(), ctx->allocator).View();
31 
32     auto resNode = util::NodeAllocator::Alloc<ir::StringLiteral>(ctx->allocator, resStr);
33     ES2PANDA_ASSERT(resNode != nullptr);
34     resNode->SetParent(concat->Parent());
35     resNode->SetRange({lhs->Range().start, rhs->Range().end});
36     return resNode;
37 }
38 
Perform(public_lib::Context * ctx,parser::Program * program)39 bool StringConstantsLowering::Perform(public_lib::Context *ctx, parser::Program *program)
40 {
41     for (auto &[_, ext_programs] : program->ExternalSources()) {
42         (void)_;
43         for (auto *extProg : ext_programs) {
44             if (program->GetFlag(parser::ProgramFlags::AST_STRING_CONSTANT_LOWERED)) {
45                 continue;
46             }
47             Perform(ctx, extProg);
48             program->SetFlag(parser::ProgramFlags::AST_STRING_CONSTANT_LOWERED);
49         }
50     }
51 
52     program->Ast()->TransformChildrenRecursivelyPostorder(
53         [ctx](checker::AstNodePtr const node) -> checker::AstNodePtr {
54             if (node->IsBinaryExpression()) {
55                 auto const binOp = node->AsBinaryExpression();
56                 if (binOp->OperatorType() == lexer::TokenType::PUNCTUATOR_PLUS && binOp->Left()->IsStringLiteral() &&
57                     binOp->Right()->IsStringLiteral()) {
58                     return FoldConcat(ctx, binOp);
59                 }
60             }
61             return node;
62         },
63         Name());
64 
65     return true;
66 }
67 
68 }  // namespace ark::es2panda::compiler
69