• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-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_STATEMENT_ANNOTATION_DECLARATION_H
17 #define ES2PANDA_IR_STATEMENT_ANNOTATION_DECLARATION_H
18 
19 #include "ir/annotationAllowed.h"
20 #include "varbinder/scope.h"
21 #include "varbinder/variable.h"
22 #include "ir/statement.h"
23 #include "ir/astNode.h"
24 #include "ir/expressions/identifier.h"
25 
26 namespace ark::es2panda::ir {
27 
28 using ENUMBITOPS_OPERATORS;
29 
30 enum class RetentionPolicy : uint32_t { SOURCE = 1U << 0U, BYTECODE = 1U << 1U, RUNTIME = 1U << 2U };
31 }  // namespace ark::es2panda::ir
32 
33 template <>
34 struct enumbitops::IsAllowedType<ark::es2panda::ir::RetentionPolicy> : std::true_type {
35 };
36 
37 namespace ark::es2panda::ir {
38 class AnnotationDeclaration : public AnnotationAllowed<Statement> {
39 public:
40     explicit AnnotationDeclaration(Expression *expr, ArenaAllocator *allocator)
41         : AnnotationAllowed<Statement>(AstNodeType::ANNOTATION_DECLARATION, allocator),
42           expr_(expr),
43           properties_(allocator->Adapter())
44     {
45     }
46     explicit AnnotationDeclaration(Expression *expr, ArenaVector<AstNode *> &&properties, ArenaAllocator *allocator)
47         : AnnotationAllowed<Statement>(AstNodeType::ANNOTATION_DECLARATION, allocator),
48           expr_(expr),
49           properties_(std::move(properties))
50     {
51     }
52 
53     const util::StringView &InternalName() const
54     {
55         return internalName_;
56     }
57 
58     void SetInternalName(util::StringView internalName)
59     {
60         internalName_ = internalName;
61     }
62 
63     [[nodiscard]] const Expression *Expr() const noexcept
64     {
65         return expr_;
66     }
67 
68     [[nodiscard]] Expression *Expr() noexcept
69     {
70         return expr_;
71     }
72 
73     [[nodiscard]] ArenaVector<AstNode *> &Properties() noexcept
74     {
75         return properties_;
76     }
77 
78     [[nodiscard]] const ArenaVector<AstNode *> &Properties() const noexcept
79     {
80         return properties_;
81     }
82 
83     [[nodiscard]] const ArenaVector<AstNode *> *PropertiesPtr() const
84     {
85         return &Properties();
86     }
87 
88     void AddProperties(ArenaVector<AstNode *> &&properties)
89     {
90         properties_ = std::move(properties);
91     }
92 
93     [[nodiscard]] bool IsSourceRetention() const noexcept
94     {
95         return (policy_ & RetentionPolicy::SOURCE) != 0;
96     }
97 
98     [[nodiscard]] bool IsBytecodeRetention() const noexcept
99     {
100         return (policy_ & RetentionPolicy::BYTECODE) != 0;
101     }
102 
103     [[nodiscard]] bool IsRuntimeRetention() const noexcept
104     {
105         return (policy_ & RetentionPolicy::RUNTIME) != 0;
106     }
107 
108     void SetSourceRetention() noexcept
109     {
110         policy_ = RetentionPolicy::SOURCE;
111     }
112 
113     void SetBytecodeRetention() noexcept
114     {
115         policy_ = RetentionPolicy::BYTECODE;
116     }
117 
118     void SetRuntimeRetention() noexcept
119     {
120         policy_ = RetentionPolicy::RUNTIME;
121     }
122 
123     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
124     void Iterate(const NodeTraverser &cb) const override;
125     void Dump(ir::AstDumper *dumper) const override;
126     void Dump(ir::SrcDumper *dumper) const override;
127     void Compile(compiler::PandaGen *pg) const override;
128     void Compile(compiler::ETSGen *etsg) const override;
129     checker::Type *Check(checker::TSChecker *checker) override;
130     checker::VerifiedType Check(checker::ETSChecker *checker) override;
131 
132     void Accept(ASTVisitorT *v) override
133     {
134         v->Accept(this);
135     }
136 
137     [[nodiscard]] bool IsScopeBearer() const noexcept override
138     {
139         return true;
140     }
141 
142     [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override
143     {
144         return scope_;
145     }
146 
147     void SetScope(varbinder::LocalScope *scope)
148     {
149         ES2PANDA_ASSERT(scope_ == nullptr);
150         scope_ = scope;
151     }
152 
153     void ClearScope() noexcept override
154     {
155         scope_ = nullptr;
156     }
157 
158     Identifier *GetBaseName() const;
159 
160 protected:
161     AstNode *Construct(ArenaAllocator *allocator) override;
162     void CopyTo(AstNode *other) const override;
163 
164 private:
165     friend class SizeOfNodeTest;
166     util::StringView internalName_ {};
167     varbinder::LocalScope *scope_ {};
168     Expression *expr_;
169     ArenaVector<AstNode *> properties_;
170     RetentionPolicy policy_ = RetentionPolicy::BYTECODE;
171 };
172 }  // namespace ark::es2panda::ir
173 
174 #endif