• 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"},
44                  {"id", id_},
45                  {"definite", AstDumper::Optional(definite_)},
46                  {"init", AstDumper::Nullable(init_)}});
47 }
48 
Compile(compiler::PandaGen * pg) const49 void VariableDeclarator::Compile(compiler::PandaGen *pg) const
50 {
51     const ir::VariableDeclaration *decl = parent_->AsVariableDeclaration();
52     if (decl->Declare()) {
53         return;
54     }
55 
56     compiler::LReference lref = compiler::LReference::CreateLRef(pg, id_, true);
57 
58     if (init_) {
59         init_->Compile(pg);
60     } else {
61         if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::VAR) {
62             return;
63         }
64         if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::LET && !decl->Parent()->IsCatchClause()) {
65             pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
66         }
67     }
68 
69     lref.SetValue();
70 }
71 
CheckSimpleVariableDeclaration(checker::Checker * checker,const ir::VariableDeclarator * declarator)72 static void CheckSimpleVariableDeclaration(checker::Checker *checker, const ir::VariableDeclarator *declarator)
73 {
74     binder::Variable *bindingVar = declarator->Id()->AsIdentifier()->Variable();
75     checker::Type *previousType = bindingVar->TsType();
76     const ir::Expression *typeAnnotation = declarator->Id()->AsIdentifier()->TypeAnnotation();
77     const ir::Expression *initializer = declarator->Init();
78     bool isConst = declarator->Parent()->AsVariableDeclaration()->Kind() ==
79                    ir::VariableDeclaration::VariableDeclarationKind::CONST;
80 
81     if (isConst) {
82         checker->AddStatus(checker::CheckerStatus::IN_CONST_CONTEXT);
83     }
84 
85     if (typeAnnotation) {
86         typeAnnotation->Check(checker);
87     }
88 
89     if (typeAnnotation && initializer) {
90         checker::Type *annotationType = typeAnnotation->AsTypeNode()->GetType(checker);
91         checker->ElaborateElementwise(annotationType, initializer, declarator->Id()->Start());
92         bindingVar->SetTsType(annotationType);
93     } else if (typeAnnotation) {
94         bindingVar->SetTsType(typeAnnotation->AsTypeNode()->GetType(checker));
95     } else if (initializer) {
96         checker::Type *initializerType = checker->CheckTypeCached(initializer);
97 
98         if (!isConst) {
99             initializerType = checker->GetBaseTypeOfLiteralType(initializerType);
100         }
101 
102         bindingVar->SetTsType(initializerType);
103     } else {
104         checker->ThrowTypeError({"Variable ", declarator->Id()->AsIdentifier()->Name(), " implicitly has an any type."},
105                                 declarator->Id()->Start());
106     }
107 
108     if (previousType) {
109         checker->IsTypeIdenticalTo(bindingVar->TsType(), previousType,
110                                    {"Subsequent variable declaration must have the same type. Variable '",
111                                     bindingVar->Name(), "' must be of type '", previousType, "', but here has type '",
112                                     bindingVar->TsType(), "'."},
113                                    declarator->Id()->Start());
114     }
115 
116     checker->RemoveStatus(checker::CheckerStatus::IN_CONST_CONTEXT);
117 }
118 
Check(checker::Checker * checker) const119 checker::Type *VariableDeclarator::Check(checker::Checker *checker) const
120 {
121     auto found = checker->NodeCache().find(this);
122     if (found != checker->NodeCache().end()) {
123         return nullptr;
124     }
125 
126     if (id_->IsIdentifier()) {
127         CheckSimpleVariableDeclaration(checker, this);
128         checker->NodeCache().insert({this, nullptr});
129         return nullptr;
130     }
131 
132     if (id_->IsArrayPattern()) {
133         auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
134         checker::ArrayDestructuringContext(checker, id_, false, id_->AsArrayPattern()->TypeAnnotation() == nullptr,
135                                            id_->AsArrayPattern()->TypeAnnotation(), init_)
136             .Start();
137 
138         checker->NodeCache().insert({this, nullptr});
139         return nullptr;
140     }
141 
142     ASSERT(id_->IsObjectPattern());
143     auto context = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
144     checker::ObjectDestructuringContext(checker, id_, false, id_->AsObjectPattern()->TypeAnnotation() == nullptr,
145                                         id_->AsObjectPattern()->TypeAnnotation(), init_)
146         .Start();
147 
148     checker->NodeCache().insert({this, nullptr});
149     return nullptr;
150 }
151 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)152 void VariableDeclarator::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
153 {
154     id_ = std::get<ir::AstNode *>(cb(id_))->AsExpression();
155 
156     if (init_) {
157         init_ = std::get<ir::AstNode *>(cb(init_))->AsExpression();
158     }
159 }
160 
161 }  // namespace panda::es2panda::ir
162