/src/WasmEdge/include/ast/component/instance.h
Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: 2019-2024 Second State INC |
3 | | |
4 | | //===-- wasmedge/ast/component/instance.h - Instance class definitions ----===// |
5 | | // |
6 | | // Part of the WasmEdge Project. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | /// |
10 | | /// \file |
11 | | /// This file contains the declaration of the Instance node related classes. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | #pragma once |
15 | | |
16 | | #include "ast/component/sort.h" |
17 | | #include "common/span.h" |
18 | | |
19 | | #include <string> |
20 | | #include <variant> |
21 | | #include <vector> |
22 | | |
23 | | namespace WasmEdge { |
24 | | namespace AST { |
25 | | namespace Component { |
26 | | |
27 | | // core:instantiatearg ::= n:<core:name> 0x12 i:<instanceidx> |
28 | | // => (with n (instance i)) |
29 | | // instantiatearg ::= n:<name> si:<sortidx> |
30 | | // => (with n si) |
31 | | |
32 | | /// AST Component::InstantiateArg class template. |
33 | | template <typename IndexType> class InstantiateArgImpl { |
34 | | public: |
35 | 0 | std::string_view getName() const noexcept { return Name; } Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<unsigned int>::getName() const Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<WasmEdge::AST::Component::SortIndex<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> > >::getName() const |
36 | 0 | std::string &getName() noexcept { return Name; } Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<unsigned int>::getName() Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<WasmEdge::AST::Component::SortIndex<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> > >::getName() |
37 | 0 | IndexType getIndex() const noexcept { return Idx; } Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<WasmEdge::AST::Component::SortIndex<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> > >::getIndex() const Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<unsigned int>::getIndex() const |
38 | 0 | IndexType &getIndex() noexcept { return Idx; } Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<unsigned int>::getIndex() Unexecuted instantiation: WasmEdge::AST::Component::InstantiateArgImpl<WasmEdge::AST::Component::SortIndex<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> > >::getIndex() |
39 | | |
40 | | private: |
41 | | std::string Name; |
42 | | IndexType Idx; |
43 | | }; |
44 | | |
45 | | /// AST Component::CoreInstantiateArg aliasing. |
46 | | using CoreInstantiateArg = InstantiateArgImpl<uint32_t>; |
47 | | /// AST Component::InstantiateArg aliasing. |
48 | | using InstantiateArg = InstantiateArgImpl<SortIndex<Sort>>; |
49 | | |
50 | | // core:inlineexport ::= n:<core:name> si:<core:sortidx> |
51 | | // => (export n si) |
52 | | // inlineexport ::= n:<exportname> si:<sortidx> |
53 | | // => (export n si) |
54 | | |
55 | | /// AST Component::InlineExport class template. |
56 | | template <typename SortType> class InlineExportImpl { |
57 | | public: |
58 | 0 | std::string_view getName() const noexcept { return Name; } Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort>::getName() const Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::getName() const |
59 | 0 | std::string &getName() noexcept { return Name; } Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort>::getName() Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::getName() |
60 | 0 | SortIndex<SortType> getSortIdx() const noexcept { return SortIdx; } Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort>::getSortIdx() const Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::getSortIdx() const |
61 | 0 | SortIndex<SortType> &getSortIdx() noexcept { return SortIdx; } Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort>::getSortIdx() Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::getSortIdx() |
62 | | |
63 | | private: |
64 | | std::string Name; |
65 | | SortIndex<SortType> SortIdx; |
66 | | }; |
67 | | |
68 | | /// AST Component::InlineExportVec class template. For InlineExportImpl vector. |
69 | | template <typename SortType> class InlineExportImplVec { |
70 | | public: |
71 | | InlineExportImplVec() noexcept = default; |
72 | | InlineExportImplVec(std::vector<InlineExportImpl<SortType>> &&Es) noexcept |
73 | 0 | : Exports(std::move(Es)) {} Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImplVec<WasmEdge::AST::Component::CoreSort>::InlineExportImplVec(std::__1::vector<WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort>, std::__1::allocator<WasmEdge::AST::Component::InlineExportImpl<WasmEdge::AST::Component::CoreSort> > >&&) Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImplVec<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::InlineExportImplVec(std::__1::vector<WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >, std::__1::allocator<WasmEdge::AST::Component::InlineExportImpl<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> > > >&&) |
74 | | InlineExportImplVec( |
75 | | const std::vector<InlineExportImpl<SortType>> &Es) noexcept |
76 | | : Exports(Es) {} |
77 | | |
78 | 0 | Span<const InlineExportImpl<SortType>> getExports() const noexcept { |
79 | 0 | return Exports; |
80 | 0 | } Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImplVec<WasmEdge::AST::Component::CoreSort>::getExports() const Unexecuted instantiation: WasmEdge::AST::Component::InlineExportImplVec<std::__1::variant<WasmEdge::AST::Component::CoreSort, WasmEdge::AST::Component::SortCase> >::getExports() const |
81 | | |
82 | | private: |
83 | | std::vector<InlineExportImpl<SortType>> Exports; |
84 | | }; |
85 | | |
86 | | // TODO: COMPONENT - The InlineExportImplVec is only the vector of InlineExport. |
87 | | // This class can be removed in the future. |
88 | | |
89 | | /// AST Component::CoreInlineExports aliasing. |
90 | | using CoreInlineExports = InlineExportImplVec<CoreSort>; |
91 | | /// AST Component::InlineExports aliasing. |
92 | | using InlineExports = InlineExportImplVec<Sort>; |
93 | | |
94 | | // core:instance ::= ie:<core:instanceexpr> |
95 | | // => (instance ie) |
96 | | // core:instanceexpr ::= 0x00 m:<moduleidx> arg*:vec(<core:instantiatearg>) |
97 | | // => (instantiate m arg*) |
98 | | // | 0x01 e*:vec(<core:inlineexport>) |
99 | | // => e* |
100 | | // instance ::= ie:<instanceexpr> |
101 | | // => (instance ie) |
102 | | // instanceexpr ::= 0x00 c:<componentidx> arg*:vec(<instantiatearg>) |
103 | | // => (instantiate c arg*) |
104 | | // | 0x01 e*:vec(<inlineexport>) |
105 | | // => e* |
106 | | |
107 | | /// AST Component::CoreInstantiate class. |
108 | | class CoreInstantiate { |
109 | | public: |
110 | 0 | CoreInstantiate() noexcept : ModuleIdx(0) {} |
111 | | CoreInstantiate(uint32_t Idx, std::vector<CoreInstantiateArg> &&Args) noexcept |
112 | 0 | : ModuleIdx(Idx), Args(std::move(Args)) {} |
113 | | |
114 | 0 | uint32_t getModuleIdx() const noexcept { return ModuleIdx; } |
115 | 0 | Span<const CoreInstantiateArg> getArgs() const noexcept { return Args; } |
116 | | |
117 | | private: |
118 | | uint32_t ModuleIdx; |
119 | | std::vector<CoreInstantiateArg> Args; |
120 | | }; |
121 | | |
122 | | /// AST Component::Instantiate class. |
123 | | class Instantiate { |
124 | | public: |
125 | 0 | Instantiate() noexcept : ComponentIdx(0) {} |
126 | | Instantiate(uint32_t Idx, std::vector<InstantiateArg> &&Args) noexcept |
127 | 0 | : ComponentIdx(Idx), Args(std::move(Args)) {} |
128 | | |
129 | 0 | uint32_t getComponentIdx() const noexcept { return ComponentIdx; } |
130 | 0 | Span<const InstantiateArg> getArgs() const noexcept { return Args; } |
131 | | |
132 | | private: |
133 | | uint32_t ComponentIdx; |
134 | | std::vector<InstantiateArg> Args; |
135 | | }; |
136 | | |
137 | | // TODO: COMPONENT - Re-construct the Expr variant into a class and rename in |
138 | | // the future. |
139 | | |
140 | | /// AST Component::CoreInstanceExpr aliasing. (For AST Component::CoreInstance) |
141 | | using CoreInstanceExpr = std::variant<CoreInstantiate, CoreInlineExports>; |
142 | | /// AST Component::InstanceExpr aliasing. (For AST Component::Instance) |
143 | | using InstanceExpr = std::variant<Instantiate, InlineExports>; |
144 | | |
145 | | } // namespace Component |
146 | | } // namespace AST |
147 | | } // namespace WasmEdge |