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 "catchClause.h"
17
18 #include <compiler/core/pandagen.h>
19 #include <compiler/base/lreference.h>
20 #include <typescript/checker.h>
21 #include <ir/astDump.h>
22 #include <ir/expressions/arrayExpression.h>
23 #include <ir/expressions/identifier.h>
24 #include <ir/expressions/objectExpression.h>
25 #include <ir/statements/blockStatement.h>
26
27 namespace panda::es2panda::ir {
28
Iterate(const NodeTraverser & cb) const29 void CatchClause::Iterate(const NodeTraverser &cb) const
30 {
31 if (param_) {
32 cb(param_);
33 }
34
35 cb(body_);
36 }
37
Dump(ir::AstDumper * dumper) const38 void CatchClause::Dump(ir::AstDumper *dumper) const
39 {
40 dumper->Add({{"type", "CatchClause"}, {"body", body_}, {"param", AstDumper::Nullable(param_)}});
41 }
42
Compile(compiler::PandaGen * pg) const43 void CatchClause::Compile(compiler::PandaGen *pg) const
44 {
45 compiler::LocalRegScope lrs(pg, scope_->ParamScope());
46
47 if (param_) {
48 auto lref = compiler::LReference::CreateLRef(pg, param_, true);
49 lref.SetValue();
50 }
51
52 ASSERT(scope_ == body_->Scope());
53 body_->Compile(pg);
54 }
55
Check(checker::Checker * checker) const56 checker::Type *CatchClause::Check(checker::Checker *checker) const
57 {
58 const ir::Expression *typeAnnotation = nullptr;
59
60 if (param_->IsIdentifier()) {
61 typeAnnotation = param_->AsIdentifier()->TypeAnnotation();
62 } else if (param_->IsArrayPattern()) {
63 typeAnnotation = param_->AsArrayPattern()->TypeAnnotation();
64 } else {
65 ASSERT(param_->IsObjectPattern());
66 typeAnnotation = param_->AsObjectPattern()->TypeAnnotation();
67 }
68
69 if (typeAnnotation) {
70 checker::Type *catchParamType = typeAnnotation->Check(checker);
71
72 if (!catchParamType->HasTypeFlag(checker::TypeFlag::ANY_OR_UNKNOWN)) {
73 checker->ThrowTypeError("Catch clause variable type annotation must be 'any' or 'unknown' if specified",
74 Start());
75 }
76 }
77
78 body_->Check(checker);
79
80 return nullptr;
81 }
82
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)83 void CatchClause::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
84 {
85 if (param_) {
86 auto paramScopeCtx = binder::LexicalScope<binder::CatchParamScope>::Enter(binder, scope_->ParamScope());
87 param_ = std::get<ir::AstNode *>(cb(param_))->AsExpression();
88 }
89
90 auto scopeCtx = binder::LexicalScope<binder::CatchScope>::Enter(binder, scope_);
91 body_ = std::get<ir::AstNode *>(cb(body_))->AsBlockStatement();
92 }
93
94 } // namespace panda::es2panda::ir
95