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