/src/WasmEdge/include/ast/component/declarator.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2025 Second State INC |
3 | | |
4 | | //===-- wasmedge/ast/component/declarator.h - Declarator class definitions ===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the Declarator related class. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/alias.h" |
17 | | #include "ast/component/descriptor.h" |
18 | | |
19 | | #include <string> |
20 | | #include <string_view> |
21 | | #include <variant> |
22 | | |
23 | | namespace WasmEdge { |
24 | | namespace AST { |
25 | | namespace Component { |
26 | | |
27 | | // Need the forward declaration. |
28 | | class CoreDefType; |
29 | | class DefType; |
30 | | |
31 | | // core:importdecl ::= m:<core:name> n:<core:name> d:<core:importdesc> |
32 | | // => (import m n d) |
33 | | |
34 | | /// AST Component::CoreImportDecl node. |
35 | | class CoreImportDecl { |
36 | | public: |
37 | 0 | std::string_view getModuleName() const noexcept { return ModName; } |
38 | 0 | std::string &getModuleName() noexcept { return ModName; } |
39 | 0 | std::string_view getName() const noexcept { return Name; } |
40 | 0 | std::string &getName() noexcept { return Name; } |
41 | 0 | CoreImportDesc getImportDesc() const noexcept { return Desc; } |
42 | 0 | CoreImportDesc &getImportDesc() noexcept { return Desc; } |
43 | | |
44 | | private: |
45 | | std::string ModName; |
46 | | std::string Name; |
47 | | CoreImportDesc Desc; |
48 | | }; |
49 | | |
50 | | // core:exportdecl ::= n:<core:name> d:<core:importdesc> => (export n d) |
51 | | |
52 | | /// AST Component::CoreExportDecl node. |
53 | | class CoreExportDecl { |
54 | | public: |
55 | 0 | std::string_view getName() const noexcept { return Name; } |
56 | 0 | std::string &getName() noexcept { return Name; } |
57 | 0 | CoreImportDesc getImportDesc() const noexcept { return Desc; } |
58 | 0 | CoreImportDesc &getImportDesc() noexcept { return Desc; } |
59 | | |
60 | | private: |
61 | | std::string Name; |
62 | | CoreImportDesc Desc; |
63 | | }; |
64 | | |
65 | | // core:moduledecl ::= 0x00 i:<core:importdecl> => i |
66 | | // | 0x01 t:<core:type> => t |
67 | | // | 0x02 a:<core:alias> => a |
68 | | // | 0x03 e:<core:exportdecl> => e |
69 | | |
70 | | /// AST Component::CoreModuleDecl node. |
71 | | class CoreModuleDecl { |
72 | | public: |
73 | 0 | const CoreImportDecl &getImport() const noexcept { |
74 | 0 | return *std::get_if<CoreImportDecl>(&Decl); |
75 | 0 | } |
76 | 0 | void setImport(CoreImportDecl &&Imp) noexcept { |
77 | 0 | Decl.emplace<CoreImportDecl>(std::move(Imp)); |
78 | 0 | } |
79 | | |
80 | 0 | const CoreDefType *getType() const noexcept { |
81 | 0 | return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get(); |
82 | 0 | } |
83 | 0 | void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept { |
84 | 0 | Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp)); |
85 | 0 | } |
86 | | |
87 | 0 | const CoreAlias &getAlias() const noexcept { |
88 | 0 | return *std::get_if<CoreAlias>(&Decl); |
89 | 0 | } |
90 | 0 | void setAlias(CoreAlias &&A) noexcept { |
91 | 0 | Decl.emplace<CoreAlias>(std::move(A)); |
92 | 0 | } |
93 | | |
94 | 0 | const CoreExportDecl &getExport() const noexcept { |
95 | 0 | return *std::get_if<CoreExportDecl>(&Decl); |
96 | 0 | } |
97 | 0 | void setExport(CoreExportDecl &&Exp) noexcept { |
98 | 0 | Decl.emplace<CoreExportDecl>(std::move(Exp)); |
99 | 0 | } |
100 | | |
101 | | private: |
102 | | std::variant<CoreImportDecl, std::unique_ptr<CoreDefType>, CoreAlias, |
103 | | CoreExportDecl> |
104 | | Decl; |
105 | | }; |
106 | | |
107 | | // exportdecl ::= en:<exportname'> ed:<externdesc> => (export en ed) |
108 | | // exportname' ::= 0x00 len:<u32> en:<exportname> => en (if len = |en|) |
109 | | // importdecl ::= in:<importname'> ed:<externdesc> => (import in ed) |
110 | | // importname' ::= 0x00 len:<u32> in:<importname> => in (if len = |in|) |
111 | | |
112 | | /// Base class of Component::ImportDecl and Component::ExportDecl node. |
113 | | class ExternDecl { |
114 | | public: |
115 | 0 | std::string_view getName() const noexcept { return Name; } |
116 | 0 | std::string &getName() noexcept { return Name; } |
117 | 0 | ExternDesc getExternDesc() const noexcept { return Desc; } |
118 | 0 | ExternDesc &getExternDesc() noexcept { return Desc; } |
119 | | |
120 | | private: |
121 | | std::string Name; |
122 | | ExternDesc Desc; |
123 | | }; |
124 | | |
125 | | /// AST Component::ImportDecl node. |
126 | | class ImportDecl : public ExternDecl {}; |
127 | | |
128 | | /// AST Component::ExportDecl node. |
129 | | class ExportDecl : public ExternDecl {}; |
130 | | |
131 | | // instancedecl ::= 0x00 t:<core:type> => t |
132 | | // | 0x01 t:<type> => t |
133 | | // | 0x02 a:<alias> => a |
134 | | // | 0x04 ed:<exportdecl> => ed |
135 | | |
136 | | /// AST Component::InstanceDecl node. |
137 | | class InstanceDecl { |
138 | | public: |
139 | 0 | const CoreDefType *getCoreType() const noexcept { |
140 | 0 | return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get(); |
141 | 0 | } |
142 | 0 | void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept { |
143 | 0 | Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T)); |
144 | 0 | } |
145 | | |
146 | 0 | const DefType *getType() const noexcept { |
147 | 0 | return std::get_if<std::unique_ptr<DefType>>(&Decl)->get(); |
148 | 0 | } |
149 | 0 | void setType(std::unique_ptr<DefType> &&T) noexcept { |
150 | 0 | Decl.emplace<std::unique_ptr<DefType>>(std::move(T)); |
151 | 0 | } |
152 | | |
153 | 0 | const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); } |
154 | 0 | void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); } |
155 | | |
156 | 0 | const ExportDecl &getExport() const noexcept { |
157 | 0 | return *std::get_if<ExportDecl>(&Decl); |
158 | 0 | } |
159 | 0 | void setExport(ExportDecl &&Exp) noexcept { |
160 | 0 | Decl.emplace<ExportDecl>(std::move(Exp)); |
161 | 0 | } |
162 | | |
163 | 0 | bool isCoreType() const noexcept { |
164 | 0 | return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl); |
165 | 0 | } |
166 | 0 | bool isType() const noexcept { |
167 | 0 | return std::holds_alternative<std::unique_ptr<DefType>>(Decl); |
168 | 0 | } |
169 | 0 | bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); } |
170 | 0 | bool isExportDecl() const noexcept { |
171 | 0 | return std::holds_alternative<ExportDecl>(Decl); |
172 | 0 | } |
173 | | |
174 | | private: |
175 | | std::variant<std::unique_ptr<CoreDefType>, std::unique_ptr<DefType>, Alias, |
176 | | ExportDecl> |
177 | | Decl; |
178 | | }; |
179 | | |
180 | | // componentdecl ::= 0x03 id:<importdecl> => id |
181 | | // | id:<instancedecl> => id |
182 | | |
183 | | /// AST Component::ComponentDecl node. |
184 | | class ComponentDecl { |
185 | | public: |
186 | 0 | const ImportDecl &getImport() const noexcept { |
187 | 0 | return *std::get_if<ImportDecl>(&Decl); |
188 | 0 | } |
189 | 0 | void setImport(ImportDecl &&Imp) noexcept { |
190 | 0 | Decl.emplace<ImportDecl>(std::move(Imp)); |
191 | 0 | } |
192 | | |
193 | 0 | const InstanceDecl &getInstance() const noexcept { |
194 | 0 | return *std::get_if<InstanceDecl>(&Decl); |
195 | 0 | } |
196 | 0 | void setInstance(InstanceDecl &&Inst) noexcept { |
197 | 0 | Decl.emplace<InstanceDecl>(std::move(Inst)); |
198 | 0 | } |
199 | | |
200 | 0 | bool isImportDecl() const noexcept { |
201 | 0 | return std::holds_alternative<ImportDecl>(Decl); |
202 | 0 | } |
203 | 0 | bool isInstanceDecl() const noexcept { |
204 | 0 | return std::holds_alternative<InstanceDecl>(Decl); |
205 | 0 | } |
206 | | |
207 | | private: |
208 | | std::variant<ImportDecl, InstanceDecl> Decl; |
209 | | }; |
210 | | |
211 | | } // namespace Component |
212 | | } // namespace AST |
213 | | } // namespace WasmEdge |