• 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 "variableDeclarator.h"
17 
18 #include <compiler/base/lreference.h>
19 #include <compiler/core/pandagen.h>
20 #include <ir/astDump.h>
21 #include <ir/expression.h>
22 #include <ir/typeNode.h>
23 #include <ir/statements/variableDeclaration.h>
24 #include <ir/expressions/arrayExpression.h>
25 #include <ir/expressions/identifier.h>
26 #include <ir/expressions/objectExpression.h>
27 #include <typescript/checker.h>
28 #include <typescript/core/destructuringContext.h>
29 
30 namespace panda::es2panda::ir {
31 
Iterate(const NodeTraverser & cb) const32 void VariableDeclarator::Iterate(const NodeTraverser &cb) const
33 {
34     cb(id_);
35 
36     if (init_) {
37         cb(init_);
38     }
39 }
40 
Dump(ir::AstDumper * dumper) const41 void VariableDeclarator::Dump(ir::AstDumper *dumper) const
42 {
43     dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullable(init_)}});
44 }
45 
Compile(compiler::PandaGen * pg) const46 void VariableDeclarator::Compile(compiler::PandaGen *pg) const
47 {
48     compiler::LReference lref = compiler::LReference::CreateLRef(pg, id_, true);
49     const ir::VariableDeclaration *decl = parent_->AsVariableDeclaration();
50 
51     if (init_) {
52         init_->Compile(pg);
53     } else {
54         if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::VAR) {
55             return;
56         }
57         if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::LET && !decl->Parent()->IsCatchClause()) {
58             pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
59         }
60     }
61 
62     lref.SetValue();
63 }
64 
CheckSimpleVariableDeclaration(checker::Checker * checker,const ir::VariableDeclarator * declarator)65 static void CheckSimpleVariableDeclaration(checker::Checker *checker, const ir::VariableDeclarator *declarator)
66 {
67     binder::Variable *bindingVar = declarator->Id()->AsIdentifier()->Variable();
68     checker::Type *previousType = bindingVar->TsType();
69     const ir::Expression *typeAnnotation = declarator->Id()->AsIdentifier()->TypeAnnotation();
70     const ir::Expression *initializer = declarator->Init();
71     bool isConst = declarator->Parent()->AsVariableDeclaration()->Kind() ==
72                    ir::VariableDeclaration::VariableDeclarationKind::CONST;
73 
74     if (isConst) {
75         checker->AddStatus(checker::CheckerStatus::IN_CONST_CONTEXT);
76     }
77 
78     if (typeAnnotation) {
79         typeAnnotation->Check(checker);
80     }
81 
82     if (typeAnnotation && initializer) {
83         checker::Type *annotationType = typeAnnotation->AsTypeNode()->GetType(checker);
84         checker->ElaborateElementwise(annotationType, initializer, declarator->Id()->Start());
85         bindingVar->SetTsType(annotationType);
86     } else if (typeAnnotation) {
87         bindingVar->SetTsType(typeAnnotation->AsTypeNode()->GetType(checker));
88     } else if (initializer) {
89         checker::Type *initializerType = checker->CheckTypeCached(initializer);
90 
91         if (!isConst) {
92             initializerType = checker->GetBaseTypeOfLiteralType(initializerType);
93         }
94 
95         bindingVar->SetTsType(initializerType);
96     } else {
97         checker->ThrowTypeError({"Variable ", declarator->Id()->AsIdentifier()->Name(), " implicitly has an any type."},
98                                 declarator->Id()->Start());
99     }
100 
101     if (previousType) {
102         checker->IsTypeIdenticalTo(bindingVar->TsType(), previousType,
103                                    {"Subsequent variable declaration must have the same type. Variable '",
104                                     bindingVar->Name(), "' must be of type '", previousType, "', but here has type '",
105                                     bindingVar->TsType(), "'."},
106                                    declarator->Id()->Start());
107     }
108 
109     checker->RemoveStatus(checker::CheckerStatus::IN_CONST_CONTEXT);
110 }
111 
Check(checker::Checker * checker) const112 checker::Type *VariableDeclarator::Check(checker::Checker *checker) const
113 {
114     auto found = checker->NodeCache().find(this);
115 
116     if (found != checker->NodeCache().end()) {
117         return nullptr;
118     }
119 
120     if (id_->IsIdentifier()) {
121         CheckSimpleVariableDeclaration(checker, this);
122         checker->NodeCache().insert({this, nullptr});
123         return nullptr;
124     }
125 
126     if (id_->IsArrayPattern()) {
127         auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
128         checker::ArrayDestructuringContext(checker, id_, false, id_->AsArrayPattern()->TypeAnnotation() == nullptr,
129                                            id_->AsArrayPattern()->TypeAnnotation(), init_)
130             .Start();
131 
132         checker->NodeCache().insert({this, nullptr});
133         return nullptr;
134     }
135 
136     ASSERT(id_->IsObjectPattern());
137     auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
138     checker::ObjectDestructuringContext(checker, id_, false, id_->AsObjectPattern()->TypeAnnotation() == nullptr,
139                                         id_->AsObjectPattern()->TypeAnnotation(), init_)
140         .Start();
141 
142     checker->NodeCache().insert({this, nullptr});
143     return nullptr;
144 }
145 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)146 void VariableDeclarator::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
147 {
148     id_ = std::get<ir::AstNode *>(cb(id_))->AsExpression();
149 
150     if (init_) {
151         init_ = std::get<ir::AstNode *>(cb(init_))->AsExpression();
152     }
153 }
154 
155 }  // namespace panda::es2panda::ir
156