• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_ETS_MODULE_H
17 #define ES2PANDA_IR_ETS_MODULE_H
18 
19 #include "ir/statements/blockStatement.h"
20 #include "ir/annotationAllowed.h"
21 #include "ir/jsDocAllowed.h"
22 #include "ir/expressions/identifier.h"
23 #include "ir/srcDump.h"
24 
25 namespace ark::es2panda::parser {
26 class Program;
27 }  // namespace ark::es2panda::parser
28 
29 namespace ark::es2panda::ir {
30 
31 using ENUMBITOPS_OPERATORS;
32 
33 enum class ModuleFlag : uint32_t {
34     NONE = 0,
35     ETSSCRIPT = 1U << 0U,
36     NAMESPACE = 1U << 1U,
37     NAMESPACE_CHAIN_LAST_NODE = 1U << 2U
38 };
39 }  // namespace ark::es2panda::ir
40 
41 template <>
42 struct enumbitops::IsAllowedType<ark::es2panda::ir::ModuleFlag> : std::true_type {
43 };
44 
45 namespace ark::es2panda::ir {
46 
47 class ETSModule : public JsDocAllowed<AnnotationAllowed<BlockStatement>> {
48 public:
49     explicit ETSModule(ArenaAllocator *allocator, ArenaVector<Statement *> &&statementList, Identifier *ident,
50                        ModuleFlag flag, parser::Program *program)
51         : JsDocAllowed<AnnotationAllowed<BlockStatement>>(allocator, std::move(statementList)),
52           ident_(ident),
53           flag_(flag),
54           program_(program)
55     {
56         type_ = AstNodeType::ETS_MODULE;
57     }
58 
59     ir::Identifier *Ident()
60     {
61         return ident_;
62     }
63 
64     const ir::Identifier *Ident() const
65     {
66         return ident_;
67     }
68 
69     parser::Program *Program()
70     {
71         return program_;
72     }
73 
74     [[nodiscard]] bool IsETSScript() const noexcept
75     {
76         return (flag_ & ModuleFlag::ETSSCRIPT) != 0;
77     }
78 
79     [[nodiscard]] bool IsNamespace() const noexcept
80     {
81         return (flag_ & ModuleFlag::NAMESPACE) != 0;
82     }
83 
84     [[nodiscard]] bool IsNamespaceChainLastNode() const noexcept
85     {
86         return (flag_ & ModuleFlag::NAMESPACE_CHAIN_LAST_NODE) != 0;
87     }
88 
89     void SetNamespaceChainLastNode() noexcept
90     {
91         ES2PANDA_ASSERT(IsNamespace());
92         flag_ |= ModuleFlag::NAMESPACE_CHAIN_LAST_NODE;
93     }
94 
95     const parser::Program *Program() const
96     {
97         return program_;
98     }
99     void Dump(ir::SrcDumper *dumper) const override;
100     void Accept(ASTVisitorT *v) override
101     {
102         v->Accept(this);
103     }
104 
105     ETSModule *Construct(ArenaAllocator *allocator) override;
106     void CopyTo(AstNode *other) const override;
107 
108 private:
109     friend class SizeOfNodeTest;
110     Identifier *ident_;
111     ModuleFlag flag_;
112     parser::Program *program_;
113 };
114 }  // namespace ark::es2panda::ir
115 
116 #endif
117