• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <compiler/core/pandagen.h>
19 #include <typescript/checker.h>
20 #include <ir/astDump.h>
21 
22 namespace panda::es2panda::ir {
23 
Iterate(const NodeTraverser & cb) const24 void Identifier::Iterate(const NodeTraverser &cb) const
25 {
26     if (typeAnnotation_) {
27         cb(typeAnnotation_);
28     }
29 }
30 
Dump(ir::AstDumper * dumper) const31 void Identifier::Dump(ir::AstDumper *dumper) const
32 {
33     dumper->Add({{"type", "Identifier"},
34                  {"name", name_},
35                  {"typeAnnotation", AstDumper::Optional(typeAnnotation_)},
36                  {"optional", AstDumper::Optional(IsOptional())}});
37 }
38 
Compile(compiler::PandaGen * pg) const39 void Identifier::Compile(compiler::PandaGen *pg) const
40 {
41     binder::ScopeFindResult res = pg->Scope()->Find(name_);
42     if (res.variable) {
43         pg->LoadVar(this, res);
44         return;
45     }
46 
47     if (name_.Is("NaN")) {
48         pg->LoadConst(this, compiler::Constant::JS_NAN);
49         return;
50     }
51 
52     if (name_.Is("Infinity")) {
53         pg->LoadConst(this, compiler::Constant::JS_INFINITY);
54         return;
55     }
56 
57     if (name_.Is("globalThis")) {
58         pg->LoadConst(this, compiler::Constant::JS_GLOBAL);
59         return;
60     }
61 
62     if (name_.Is("undefined")) {
63         pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
64         return;
65     }
66 
67     pg->TryLoadGlobalByName(this, name_);
68 }
69 
Check(checker::Checker * checker) const70 checker::Type *Identifier::Check(checker::Checker *checker) const
71 {
72     if (!Variable()) {
73         if (name_.Is("undefined")) {
74             return checker->GlobalUndefinedType();
75         }
76 
77         checker->ThrowTypeError({"Cannot find name ", name_}, Start());
78     }
79 
80     const binder::Decl *decl = Variable()->Declaration();
81 
82     if (decl->IsTypeAliasDecl() || decl->IsInterfaceDecl()) {
83         checker->ThrowTypeError({name_, " only refers to a type, but is being used as a value here."}, Start());
84     }
85 
86     return checker->GetTypeOfVariable(Variable());
87 }
88 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)89 void Identifier::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
90 {
91     if (typeAnnotation_) {
92         typeAnnotation_ = std::get<ir::AstNode *>(cb(typeAnnotation_))->AsExpression();
93     }
94 }
95 
96 }  // namespace panda::es2panda::ir
97