1/**
2 * Copyright (c) 2021-2024 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#ifndef ES2PANDA_IR_TS_AS_EXPRESSION_H
17#define ES2PANDA_IR_TS_AS_EXPRESSION_H
18
19#include "ir/astDump.h"
20#include "ir/srcDump.h"
21#include "ir/expression.h"
22namespace ark::es2panda::checker {
23class ETSAnalyzer;
24}  // namespace ark::es2panda::checker
25
26namespace ark::es2panda::compiler {
27class ETSCompiler;
28}  // namespace ark::es2panda::compiler
29namespace ark::es2panda::ir {
30class TSAsExpression : public AnnotatedExpression {
31public:
32    TSAsExpression() = delete;
33    ~TSAsExpression() override = default;
34
35    NO_COPY_SEMANTIC(TSAsExpression);
36    NO_MOVE_SEMANTIC(TSAsExpression);
37
38    explicit TSAsExpression(Expression *expression, TypeNode *typeAnnotation, bool isConst)
39        : AnnotatedExpression(AstNodeType::TS_AS_EXPRESSION, typeAnnotation), expression_(expression), isConst_(isConst)
40    {
41    }
42    // NOTE (vivienvoros): these friend relationships can be removed once there are getters for private fields
43    friend class checker::ETSAnalyzer;
44    friend class compiler::ETSCompiler;
45
46    [[nodiscard]] const Expression *Expr() const noexcept
47    {
48        return expression_;
49    }
50
51    [[nodiscard]] Expression *Expr() noexcept;
52    void SetExpr(Expression *expr) noexcept;
53
54    [[nodiscard]] bool IsConst() const noexcept
55    {
56        return isConst_;
57    }
58
59    [[nodiscard]] TSAsExpression *Clone(ArenaAllocator *allocator, AstNode *parent) override;
60
61    void SetUncheckedCast(bool isUncheckedCast) noexcept
62    {
63        isUncheckedCast_ = isUncheckedCast;
64    }
65
66    void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
67    void Iterate(const NodeTraverser &cb) const override;
68    void Dump(ir::AstDumper *dumper) const override;
69    void Dump(ir::SrcDumper *dumper) const override;
70    void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
71    void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
72    checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
73    checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
74
75    void Accept(ASTVisitorT *v) override
76    {
77        v->Accept(this);
78    }
79
80private:
81    Expression *expression_;
82    bool isUncheckedCast_ {true};
83    bool isConst_;
84};
85}  // namespace ark::es2panda::ir
86
87#endif
88