1 /**
2 * Copyright (c) 2021 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 "identifier.h"
17
18 #include "varbinder/scope.h"
19 #include "checker/ETSchecker.h"
20 #include "checker/TSchecker.h"
21 #include "compiler/core/pandagen.h"
22 #include "compiler/core/ETSGen.h"
23 #include "ir/astDump.h"
24 #include "ir/srcDump.h"
25
26 namespace panda::es2panda::ir {
Identifier(Tag const tag,Identifier const & other,ArenaAllocator * const allocator)27 Identifier::Identifier([[maybe_unused]] Tag const tag, Identifier const &other, ArenaAllocator *const allocator)
28 : AnnotatedExpression(static_cast<AnnotatedExpression const &>(other), allocator), decorators_(allocator->Adapter())
29 {
30 name_ = other.name_;
31 flags_ = other.flags_;
32 variable_ = other.variable_;
33
34 for (auto *decorator : other.decorators_) {
35 decorators_.emplace_back(decorator->Clone(allocator, this));
36 }
37 }
38
39 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)40 Identifier *Identifier::Clone(ArenaAllocator *const allocator, AstNode *const parent)
41 {
42 if (auto *const clone = allocator->New<Identifier>(Tag {}, *this, allocator); clone != nullptr) {
43 if (parent != nullptr) {
44 clone->SetParent(parent);
45 }
46 return clone;
47 }
48 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
49 }
50
TransformChildren(const NodeTransformer & cb)51 void Identifier::TransformChildren(const NodeTransformer &cb)
52 {
53 if (TypeAnnotation() != nullptr) {
54 SetTsTypeAnnotation(static_cast<TypeNode *>(cb(TypeAnnotation())));
55 }
56
57 for (auto *&it : decorators_) {
58 it = cb(it)->AsDecorator();
59 }
60 }
61
Iterate(const NodeTraverser & cb) const62 void Identifier::Iterate(const NodeTraverser &cb) const
63 {
64 if (TypeAnnotation() != nullptr) {
65 cb(TypeAnnotation());
66 }
67
68 for (auto *it : decorators_) {
69 cb(it);
70 }
71 }
72
ValidateExpression()73 ValidationInfo Identifier::ValidateExpression()
74 {
75 if ((flags_ & IdentifierFlags::OPTIONAL) != 0U) {
76 return {"Unexpected token '?'.", Start()};
77 }
78
79 if (TypeAnnotation() != nullptr) {
80 return {"Unexpected token.", TypeAnnotation()->Start()};
81 }
82
83 ValidationInfo info;
84 return info;
85 }
86
Dump(ir::AstDumper * dumper) const87 void Identifier::Dump(ir::AstDumper *dumper) const
88 {
89 dumper->Add({{"type", IsPrivateIdent() ? "PrivateIdentifier" : "Identifier"},
90 {"name", name_},
91 {"typeAnnotation", AstDumper::Optional(TypeAnnotation())},
92 {"optional", AstDumper::Optional(IsOptional())},
93 {"decorators", decorators_}});
94 }
95
Dump(ir::SrcDumper * dumper) const96 void Identifier::Dump(ir::SrcDumper *dumper) const
97 {
98 if (IsPrivateIdent()) {
99 dumper->Add("private ");
100 }
101 dumper->Add(std::string(name_));
102 if (IsOptional()) {
103 dumper->Add("?");
104 }
105 }
106
Compile(compiler::PandaGen * pg) const107 void Identifier::Compile(compiler::PandaGen *pg) const
108 {
109 pg->GetAstCompiler()->Compile(this);
110 }
111
Compile(compiler::ETSGen * etsg) const112 void Identifier::Compile(compiler::ETSGen *etsg) const
113 {
114 etsg->GetAstCompiler()->Compile(this);
115 }
116
Check(checker::TSChecker * checker)117 checker::Type *Identifier::Check(checker::TSChecker *checker)
118 {
119 return checker->GetAnalyzer()->Check(this);
120 }
121
Check(checker::ETSChecker * checker)122 checker::Type *Identifier::Check(checker::ETSChecker *checker)
123 {
124 return checker->GetAnalyzer()->Check(this);
125 }
126 } // namespace panda::es2panda::ir
127