/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 | | #include "common/span.h" |
19 | | |
20 | | #include <memory> |
21 | | #include <string> |
22 | | #include <string_view> |
23 | | #include <variant> |
24 | | #include <vector> |
25 | | |
26 | | namespace WasmEdge { |
27 | | namespace AST { |
28 | | namespace Component { |
29 | | |
30 | | // Need the forward declaration. |
31 | | class CoreDefType; |
32 | | class DefType; |
33 | | |
34 | | // core:importdecl ::= m:<core:name> n:<core:name> d:<core:importdesc> |
35 | | // => (import m n d) |
36 | | |
37 | | /// AST Component::CoreImportDecl node. |
38 | | class CoreImportDecl { |
39 | | public: |
40 | 1.73k | std::string_view getModuleName() const noexcept { return ModName; } |
41 | 2.86k | std::string &getModuleName() noexcept { return ModName; } |
42 | 1.26k | std::string_view getName() const noexcept { return Name; } |
43 | 2.85k | std::string &getName() noexcept { return Name; } |
44 | 1.10k | const CoreImportDesc &getImportDesc() const noexcept { return Desc; } |
45 | 2.85k | CoreImportDesc &getImportDesc() noexcept { return Desc; } |
46 | | |
47 | | private: |
48 | | std::string ModName; |
49 | | std::string Name; |
50 | | CoreImportDesc Desc; |
51 | | }; |
52 | | |
53 | | // core:exportdecl ::= n:<core:name> d:<core:importdesc> => (export n d) |
54 | | |
55 | | /// AST Component::CoreExportDecl node. |
56 | | class CoreExportDecl { |
57 | | public: |
58 | 346 | std::string_view getName() const noexcept { return Name; } |
59 | 956 | std::string &getName() noexcept { return Name; } |
60 | 378 | const CoreImportDesc &getImportDesc() const noexcept { return Desc; } |
61 | 956 | CoreImportDesc &getImportDesc() noexcept { return Desc; } |
62 | | |
63 | | private: |
64 | | std::string Name; |
65 | | CoreImportDesc Desc; |
66 | | }; |
67 | | |
68 | | // core:moduledecl ::= 0x00 i:<core:importdecl> => i |
69 | | // | 0x01 rt:<core:rectype> => (type rt) |
70 | | // | 0x02 a:<core:alias> => a |
71 | | // | 0x03 e:<core:exportdecl> => e |
72 | | |
73 | | /// AST Component::CoreModuleDecl node. |
74 | | class CoreModuleDecl { |
75 | | public: |
76 | 1.10k | const CoreImportDecl &getImport() const noexcept { |
77 | 1.10k | return *std::get_if<CoreImportDecl>(&Decl); |
78 | 1.10k | } |
79 | 2.83k | void setImport(CoreImportDecl &&Imp) noexcept { |
80 | 2.83k | Decl.emplace<CoreImportDecl>(std::move(Imp)); |
81 | 2.83k | } |
82 | | |
83 | 474 | const CoreDefType *getType() const noexcept { |
84 | 474 | return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get(); |
85 | 474 | } |
86 | 674 | void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept { |
87 | 674 | Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp)); |
88 | 674 | } |
89 | | |
90 | 57 | const CoreAlias &getAlias() const noexcept { |
91 | 57 | return *std::get_if<CoreAlias>(&Decl); |
92 | 57 | } |
93 | 143 | void setAlias(CoreAlias &&A) noexcept { |
94 | 143 | Decl.emplace<CoreAlias>(std::move(A)); |
95 | 143 | } |
96 | | |
97 | 378 | const CoreExportDecl &getExport() const noexcept { |
98 | 378 | return *std::get_if<CoreExportDecl>(&Decl); |
99 | 378 | } |
100 | 945 | void setExport(CoreExportDecl &&Exp) noexcept { |
101 | 945 | Decl.emplace<CoreExportDecl>(std::move(Exp)); |
102 | 945 | } |
103 | | |
104 | 2.01k | bool isImport() const noexcept { |
105 | 2.01k | return std::holds_alternative<CoreImportDecl>(Decl); |
106 | 2.01k | } |
107 | 909 | bool isType() const noexcept { |
108 | 909 | return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl); |
109 | 909 | } |
110 | 435 | bool isAlias() const noexcept { |
111 | 435 | return std::holds_alternative<CoreAlias>(Decl); |
112 | 435 | } |
113 | 378 | bool isExport() const noexcept { |
114 | 378 | return std::holds_alternative<CoreExportDecl>(Decl); |
115 | 378 | } |
116 | | |
117 | | private: |
118 | | std::variant<CoreImportDecl, std::unique_ptr<CoreDefType>, CoreAlias, |
119 | | CoreExportDecl> |
120 | | Decl; |
121 | | }; |
122 | | |
123 | | // importdecl ::= na:<nameattributes> et:<externtype> => (import na et) |
124 | | // exportdecl ::= na:<nameattributes> et:<externtype> => (export na et) |
125 | | |
126 | | /// Base class of Component::ImportDecl and Component::ExportDecl node. |
127 | | class ExternDecl { |
128 | | public: |
129 | 672 | std::string_view getName() const noexcept { return Name; } |
130 | 870 | std::string &getName() noexcept { return Name; } |
131 | 284 | const ExternDesc &getExternDesc() const noexcept { return Desc; } |
132 | 854 | ExternDesc &getExternDesc() noexcept { return Desc; } |
133 | 870 | std::vector<std::string> &getImplements() noexcept { return Implements; } |
134 | 270 | Span<const std::string> getImplements() const noexcept { return Implements; } |
135 | 870 | std::vector<std::string> &getExternalIds() noexcept { return ExternalIds; } |
136 | 0 | Span<const std::string> getExternalIds() const noexcept { |
137 | 0 | return ExternalIds; |
138 | 0 | } |
139 | 870 | std::vector<std::string> &getVersionSuffixes() noexcept { |
140 | 870 | return VersionSuffixes; |
141 | 870 | } |
142 | 270 | Span<const std::string> getVersionSuffixes() const noexcept { |
143 | 270 | return VersionSuffixes; |
144 | 270 | } |
145 | | |
146 | | private: |
147 | | std::string Name; |
148 | | ExternDesc Desc; |
149 | | std::vector<std::string> Implements; |
150 | | std::vector<std::string> ExternalIds; |
151 | | std::vector<std::string> VersionSuffixes; |
152 | | }; |
153 | | |
154 | | /// AST Component::ImportDecl node. |
155 | | class ImportDecl : public ExternDecl {}; |
156 | | |
157 | | /// AST Component::ExportDecl node. |
158 | | class ExportDecl : public ExternDecl {}; |
159 | | |
160 | | // instancedecl ::= 0x00 t:<core:type> => t |
161 | | // | 0x01 t:<type> => t |
162 | | // | 0x02 a:<alias> => a |
163 | | // | 0x04 ed:<exportdecl> => ed |
164 | | |
165 | | /// AST Component::InstanceDecl node. |
166 | | class InstanceDecl { |
167 | | public: |
168 | 78 | const CoreDefType *getCoreType() const noexcept { |
169 | 78 | return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get(); |
170 | 78 | } |
171 | 704 | void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept { |
172 | 704 | Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T)); |
173 | 704 | } |
174 | | |
175 | 4.24k | const DefType *getType() const noexcept { |
176 | 4.24k | return std::get_if<std::unique_ptr<DefType>>(&Decl)->get(); |
177 | 4.24k | } |
178 | 5.87k | void setType(std::unique_ptr<DefType> &&T) noexcept { |
179 | 5.87k | Decl.emplace<std::unique_ptr<DefType>>(std::move(T)); |
180 | 5.87k | } |
181 | | |
182 | 996 | const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); } |
183 | 4.12k | void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); } |
184 | | |
185 | 299 | const ExportDecl &getExport() const noexcept { |
186 | 299 | return *std::get_if<ExportDecl>(&Decl); |
187 | 299 | } |
188 | 528 | void setExport(ExportDecl &&Exp) noexcept { |
189 | 528 | Decl.emplace<ExportDecl>(std::move(Exp)); |
190 | 528 | } |
191 | | |
192 | 5.48k | bool isCoreType() const noexcept { |
193 | 5.48k | return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl); |
194 | 5.48k | } |
195 | 5.41k | bool isType() const noexcept { |
196 | 5.41k | return std::holds_alternative<std::unique_ptr<DefType>>(Decl); |
197 | 5.41k | } |
198 | 1.16k | bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); } |
199 | 299 | bool isExportDecl() const noexcept { |
200 | 299 | return std::holds_alternative<ExportDecl>(Decl); |
201 | 299 | } |
202 | | |
203 | | private: |
204 | | std::variant<std::unique_ptr<CoreDefType>, std::unique_ptr<DefType>, Alias, |
205 | | ExportDecl> |
206 | | Decl; |
207 | | }; |
208 | | |
209 | | // componentdecl ::= 0x03 id:<importdecl> => id |
210 | | // | id:<instancedecl> => id |
211 | | |
212 | | /// AST Component::ComponentDecl node. |
213 | | class ComponentDecl { |
214 | | public: |
215 | 118 | const ImportDecl &getImport() const noexcept { |
216 | 118 | return *std::get_if<ImportDecl>(&Decl); |
217 | 118 | } |
218 | 320 | void setImport(ImportDecl &&Imp) noexcept { |
219 | 320 | Decl.emplace<ImportDecl>(std::move(Imp)); |
220 | 320 | } |
221 | | |
222 | 2.76k | const InstanceDecl &getInstance() const noexcept { |
223 | 2.76k | return *std::get_if<InstanceDecl>(&Decl); |
224 | 2.76k | } |
225 | 6.94k | void setInstance(InstanceDecl &&Inst) noexcept { |
226 | 6.94k | Decl.emplace<InstanceDecl>(std::move(Inst)); |
227 | 6.94k | } |
228 | | |
229 | 2.88k | bool isImportDecl() const noexcept { |
230 | 2.88k | return std::holds_alternative<ImportDecl>(Decl); |
231 | 2.88k | } |
232 | 0 | bool isInstanceDecl() const noexcept { |
233 | 0 | return std::holds_alternative<InstanceDecl>(Decl); |
234 | 0 | } |
235 | | |
236 | | private: |
237 | | std::variant<ImportDecl, InstanceDecl> Decl; |
238 | | }; |
239 | | |
240 | | } // namespace Component |
241 | | } // namespace AST |
242 | | } // namespace WasmEdge |