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/typeNode.h>
22 #include <ir/statements/variableDeclaration.h>
23 #include <ir/expressions/arrayExpression.h>
24 #include <ir/expressions/identifier.h>
25 #include <ir/expressions/objectExpression.h>
26 #include <typescript/core/destructuringContext.h>
27
28 namespace panda::es2panda::ir {
29
Iterate(const NodeTraverser & cb) const30 void VariableDeclarator::Iterate(const NodeTraverser &cb) const
31 {
32 cb(id_);
33
34 if (init_) {
35 cb(init_);
36 }
37 }
38
Dump(ir::AstDumper * dumper) const39 void VariableDeclarator::Dump(ir::AstDumper *dumper) const
40 {
41 dumper->Add({{"type", "VariableDeclarator"},
42 {"id", id_},
43 {"definite", AstDumper::Optional(definite_)},
44 {"init", AstDumper::Nullable(init_)}});
45 }
46
Compile(compiler::PandaGen * pg) const47 void VariableDeclarator::Compile(compiler::PandaGen *pg) const
48 {
49 const ir::VariableDeclaration *decl = parent_->AsVariableDeclaration();
50 if (decl->Declare()) {
51 return;
52 }
53
54 compiler::LReference lref = compiler::LReference::CreateLRef(pg, id_, true);
55
56 if (init_) {
57 init_->Compile(pg);
58 } else {
59 if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::VAR) {
60 return;
61 }
62 if (decl->Kind() == ir::VariableDeclaration::VariableDeclarationKind::LET && !decl->Parent()->IsCatchClause()) {
63 pg->LoadConst(this, compiler::Constant::JS_UNDEFINED);
64 }
65 }
66
67 lref.SetValue();
68 }
69
CheckSimpleVariableDeclaration(checker::Checker * checker,const ir::VariableDeclarator * declarator)70 static void CheckSimpleVariableDeclaration(checker::Checker *checker, const ir::VariableDeclarator *declarator)
71 {
72 CHECK_NOT_NULL(declarator);
73 binder::Variable *bindingVar = declarator->Id()->AsIdentifier()->Variable();
74 CHECK_NOT_NULL(bindingVar);
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