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 <binder/scope.h>
19 #include <compiler/core/pandagen.h>
20 #include <typescript/checker.h>
21 #include <ir/astDump.h>
22 #include <ir/typeNode.h>
23 #include <ir/base/decorator.h>
24 #include <ir/base/scriptFunction.h>
25 #include <ir/expressions/assignmentExpression.h>
26 #include <ir/statements/functionDeclaration.h>
27 #include <ir/statements/variableDeclarator.h>
28 #include <ir/expression.h>
29
30 namespace panda::es2panda::ir {
31
Iterate(const NodeTraverser & cb) const32 void Identifier::Iterate(const NodeTraverser &cb) const
33 {
34 if (typeAnnotation_) {
35 cb(typeAnnotation_);
36 }
37
38 for (auto *it : decorators_) {
39 cb(it);
40 }
41 }
42
Dump(ir::AstDumper * dumper) const43 void Identifier::Dump(ir::AstDumper *dumper) const
44 {
45 dumper->Add({{"type", "Identifier"},
46 {"name", name_},
47 {"typeAnnotation", AstDumper::Optional(typeAnnotation_)},
48 {"optional", AstDumper::Optional(IsOptional())},
49 {"decorators", decorators_}});
50 }
51
Compile(compiler::PandaGen * pg) const52 void Identifier::Compile(compiler::PandaGen *pg) const
53 {
54 binder::ScopeFindResult res = pg->Scope()->Find(name_);
55 if (res.variable) {
56 pg->LoadVar(this, res);
57 return;
58 }
59
60 if (name_.Is("NaN")) {
61 pg->LoadConst(this, compiler::Constant::JS_NAN);
62 return;
63 }
64
65 if (name_.Is("Infinity")) {
66 pg->LoadConst(this, compiler::Constant::JS_INFINITY);
67 return;
68 }
69
70 if (name_.Is("globalThis")) {
71 pg->LoadConst(this, compiler::Constant::JS_GLOBAL);
72 return;
73 }
74
75 if (name_.Is("undefined")) {
76 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
77 return;
78 }
79
80 pg->TryLoadGlobalByName(this, name_);
81 }
82
Check(checker::Checker * checker) const83 checker::Type *Identifier::Check(checker::Checker *checker) const
84 {
85 if (!Variable()) {
86 if (name_.Is("undefined")) {
87 return checker->GlobalUndefinedType();
88 }
89
90 checker->ThrowTypeError({"Cannot find name ", name_}, Start());
91 }
92
93 const binder::Decl *decl = Variable()->Declaration();
94
95 if (decl->IsTypeAliasDecl() || decl->IsInterfaceDecl()) {
96 checker->ThrowTypeError({name_, " only refers to a type, but is being used as a value here."}, Start());
97 }
98
99 return checker->GetTypeOfVariable(Variable());
100 }
101
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)102 void Identifier::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
103 {
104 if (typeAnnotation_) {
105 typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
106 }
107
108 for (auto iter = decorators_.begin(); iter != decorators_.end(); iter++) {
109 *iter = std::get<ir::AstNode *>(cb(*iter))->AsDecorator();
110 }
111 }
112
113 } // namespace panda::es2panda::ir
114