• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 "stringLiteral.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)25 void StringLiteral::TransformChildren([[maybe_unused]] const NodeTransformer &cb,
26                                       [[maybe_unused]] std::string_view const transformationName)
27 {
28 }
29 
Iterate(const NodeTraverser & cb) const30 void StringLiteral::Iterate([[maybe_unused]] const NodeTraverser &cb) const {}
31 
Dump(ir::AstDumper * dumper) const32 void StringLiteral::Dump(ir::AstDumper *dumper) const
33 {
34     dumper->Add({{"type", "StringLiteral"}, {"value", str_}});
35 }
36 
37 static unsigned int constexpr CHAR_UPPER_HALF = 128U;
38 
Dump(ir::SrcDumper * dumper) const39 void StringLiteral::Dump(ir::SrcDumper *dumper) const
40 {
41     std::string str(str_);
42     std::string escapedStr;
43     escapedStr.push_back('\"');
44     for (size_t i = 0, j = str_.Length(); i < j; ++i) {
45         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
46         const char c = str_.Bytes()[i];
47         // check if a given character is printable
48         // the cast is necessary to avoid undefined behaviour
49         if (std::isprint(static_cast<unsigned char>(c)) != 0U || static_cast<unsigned char>(c) >= CHAR_UPPER_HALF) {
50             escapedStr.push_back(c);
51         } else {
52             escapedStr.push_back('\\');
53             if (c == '\n') {
54                 escapedStr.push_back('n');
55             } else if (c == '\t') {
56                 escapedStr.push_back('t');
57             } else if (c == '\v') {
58                 escapedStr.push_back('v');
59             } else if (c == '\f') {
60                 escapedStr.push_back('f');
61             } else if (c == '\r') {
62                 escapedStr.push_back('r');
63             } else if (c == '\0') {
64                 escapedStr.push_back('0');
65             } else {
66                 UNREACHABLE();
67             }
68         }
69     }
70     escapedStr.push_back('\"');
71     dumper->Add(escapedStr);
72 }
73 
Compile(compiler::PandaGen * pg) const74 void StringLiteral::Compile(compiler::PandaGen *pg) const
75 {
76     pg->GetAstCompiler()->Compile(this);
77 }
78 
Compile(compiler::ETSGen * etsg) const79 void StringLiteral::Compile(compiler::ETSGen *etsg) const
80 {
81     etsg->GetAstCompiler()->Compile(this);
82 }
83 
Check(checker::TSChecker * checker)84 checker::Type *StringLiteral::Check(checker::TSChecker *checker)
85 {
86     return checker->GetAnalyzer()->Check(this);
87 }
88 
Check(checker::ETSChecker * checker)89 checker::Type *StringLiteral::Check(checker::ETSChecker *checker)
90 {
91     return checker->GetAnalyzer()->Check(this);
92 }
93 
Clone(ArenaAllocator * const allocator,AstNode * const parent)94 StringLiteral *StringLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent)
95 {
96     if (auto *const clone = allocator->New<StringLiteral>(str_); clone != nullptr) {
97         if (parent != nullptr) {
98             clone->SetParent(parent);
99         }
100         clone->SetRange(Range());
101         return clone;
102     }
103 
104     throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
105 }
106 }  // namespace ark::es2panda::ir
107