/src/WasmEdge/include/ast/component/declarator.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
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 | 0 | bool isImport() const noexcept { |
102 | 0 | return std::holds_alternative<CoreImportDecl>(Decl); |
103 | 0 | } |
104 | 0 | bool isType() const noexcept { |
105 | 0 | return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl); |
106 | 0 | } |
107 | 0 | bool isAlias() const noexcept { |
108 | 0 | return std::holds_alternative<CoreAlias>(Decl); |
109 | 0 | } |
110 | 0 | bool isExport() const noexcept { |
111 | 0 | return std::holds_alternative<CoreExportDecl>(Decl); |
112 | 0 | } |
113 | | |
114 | | private: |
115 | | std::variant<CoreImportDecl, std::unique_ptr<CoreDefType>, CoreAlias, |
116 | | CoreExportDecl> |
117 | | Decl; |
118 | | }; |
119 | | |
120 | | // exportdecl ::= en:<exportname'> ed:<externdesc> => (export en ed) |
121 | | // exportname' ::= 0x00 len:<u32> en:<exportname> => en (if len = |en|) |
122 | | // importdecl ::= in:<importname'> ed:<externdesc> => (import in ed) |
123 | | // importname' ::= 0x00 len:<u32> in:<importname> => in (if len = |in|) |
124 | | |
125 | | /// Base class of Component::ImportDecl and Component::ExportDecl node. |
126 | | class ExternDecl { |
127 | | public: |
128 | 0 | std::string_view getName() const noexcept { return Name; } |
129 | 0 | std::string &getName() noexcept { return Name; } |
130 | 0 | const ExternDesc &getExternDesc() const noexcept { return Desc; } |
131 | 0 | ExternDesc &getExternDesc() noexcept { return Desc; } |
132 | | |
133 | | private: |
134 | | std::string Name; |
135 | | ExternDesc Desc; |
136 | | }; |
137 | | |
138 | | /// AST Component::ImportDecl node. |
139 | | class ImportDecl : public ExternDecl {}; |
140 | | |
141 | | /// AST Component::ExportDecl node. |
142 | | class ExportDecl : public ExternDecl {}; |
143 | | |
144 | | // instancedecl ::= 0x00 t:<core:type> => t |
145 | | // | 0x01 t:<type> => t |
146 | | // | 0x02 a:<alias> => a |
147 | | // | 0x04 ed:<exportdecl> => ed |
148 | | |
149 | | /// AST Component::InstanceDecl node. |
150 | | class InstanceDecl { |
151 | | public: |
152 | 0 | const CoreDefType *getCoreType() const noexcept { |
153 | 0 | return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get(); |
154 | 0 | } |
155 | 0 | void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept { |
156 | 0 | Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T)); |
157 | 0 | } |
158 | | |
159 | 0 | const DefType *getType() const noexcept { |
160 | 0 | return std::get_if<std::unique_ptr<DefType>>(&Decl)->get(); |
161 | 0 | } |
162 | 0 | void setType(std::unique_ptr<DefType> &&T) noexcept { |
163 | 0 | Decl.emplace<std::unique_ptr<DefType>>(std::move(T)); |
164 | 0 | } |
165 | | |
166 | 0 | const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); } |
167 | 0 | void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); } |
168 | | |
169 | 0 | const ExportDecl &getExport() const noexcept { |
170 | 0 | return *std::get_if<ExportDecl>(&Decl); |
171 | 0 | } |
172 | 0 | void setExport(ExportDecl &&Exp) noexcept { |
173 | 0 | Decl.emplace<ExportDecl>(std::move(Exp)); |
174 | 0 | } |
175 | | |
176 | 0 | bool isCoreType() const noexcept { |
177 | 0 | return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl); |
178 | 0 | } |
179 | 0 | bool isType() const noexcept { |
180 | 0 | return std::holds_alternative<std::unique_ptr<DefType>>(Decl); |
181 | 0 | } |
182 | 0 | bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); } |
183 | 0 | bool isExportDecl() const noexcept { |
184 | 0 | return std::holds_alternative<ExportDecl>(Decl); |
185 | 0 | } |
186 | | |
187 | | private: |
188 | | std::variant<std::unique_ptr<CoreDefType>, std::unique_ptr<DefType>, Alias, |
189 | | ExportDecl> |
190 | | Decl; |
191 | | }; |
192 | | |
193 | | // componentdecl ::= 0x03 id:<importdecl> => id |
194 | | // | id:<instancedecl> => id |
195 | | |
196 | | /// AST Component::ComponentDecl node. |
197 | | class ComponentDecl { |
198 | | public: |
199 | 0 | const ImportDecl &getImport() const noexcept { |
200 | 0 | return *std::get_if<ImportDecl>(&Decl); |
201 | 0 | } |
202 | 0 | void setImport(ImportDecl &&Imp) noexcept { |
203 | 0 | Decl.emplace<ImportDecl>(std::move(Imp)); |
204 | 0 | } |
205 | | |
206 | 0 | const InstanceDecl &getInstance() const noexcept { |
207 | 0 | return *std::get_if<InstanceDecl>(&Decl); |
208 | 0 | } |
209 | 0 | void setInstance(InstanceDecl &&Inst) noexcept { |
210 | 0 | Decl.emplace<InstanceDecl>(std::move(Inst)); |
211 | 0 | } |
212 | | |
213 | 0 | bool isImportDecl() const noexcept { |
214 | 0 | return std::holds_alternative<ImportDecl>(Decl); |
215 | 0 | } |
216 | 0 | bool isInstanceDecl() const noexcept { |
217 | 0 | return std::holds_alternative<InstanceDecl>(Decl); |
218 | 0 | } |
219 | | |
220 | | private: |
221 | | std::variant<ImportDecl, InstanceDecl> Decl; |
222 | | }; |
223 | | |
224 | | } // namespace Component |
225 | | } // namespace AST |
226 | | } // namespace WasmEdge |