Coverage Report

Created: 2025-08-25 06:58

/src/WasmEdge/include/ast/component/declarator.h
Line
Count
Source (jump to first uncovered line)
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
#include "ast/type.h"
19
20
#include <memory>
21
#include <string>
22
#include <string_view>
23
#include <variant>
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
0
  std::string_view getModuleName() const noexcept { return ModName; }
40
0
  std::string &getModuleName() noexcept { return ModName; }
41
0
  std::string_view getName() const noexcept { return Name; }
42
0
  std::string &getName() noexcept { return Name; }
43
0
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
44
0
  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
0
  std::string_view getName() const noexcept { return Name; }
58
0
  std::string &getName() noexcept { return Name; }
59
0
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
60
0
  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
0
  const CoreImportDecl &getImport() const noexcept {
76
0
    return *std::get_if<CoreImportDecl>(&Decl);
77
0
  }
78
0
  void setImport(CoreImportDecl &&Imp) noexcept {
79
0
    Decl.emplace<CoreImportDecl>(std::move(Imp));
80
0
  }
81
82
0
  const CoreDefType *getType() const noexcept {
83
0
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
84
0
  }
85
0
  void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept {
86
0
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp));
87
0
  }
88
89
0
  const CoreAlias &getAlias() const noexcept {
90
0
    return *std::get_if<CoreAlias>(&Decl);
91
0
  }
92
0
  void setAlias(CoreAlias &&A) noexcept {
93
0
    Decl.emplace<CoreAlias>(std::move(A));
94
0
  }
95
96
0
  const CoreExportDecl &getExport() const noexcept {
97
0
    return *std::get_if<CoreExportDecl>(&Decl);
98
0
  }
99
0
  void setExport(CoreExportDecl &&Exp) noexcept {
100
0
    Decl.emplace<CoreExportDecl>(std::move(Exp));
101
0
  }
102
103
private:
104
  std::variant<CoreImportDecl, std::unique_ptr<CoreDefType>, CoreAlias,
105
               CoreExportDecl>
106
      Decl;
107
};
108
109
// exportdecl  ::= en:<exportname'> ed:<externdesc> => (export en ed)
110
// exportname' ::= 0x00 len:<u32> en:<exportname>   => en (if len = |en|)
111
// importdecl  ::= in:<importname'> ed:<externdesc> => (import in ed)
112
// importname' ::= 0x00 len:<u32> in:<importname>   => in (if len = |in|)
113
114
/// Base class of Component::ImportDecl and Component::ExportDecl node.
115
class ExternDecl {
116
public:
117
0
  std::string_view getName() const noexcept { return Name; }
118
0
  std::string &getName() noexcept { return Name; }
119
0
  ExternDesc getExternDesc() const noexcept { return Desc; }
120
0
  ExternDesc &getExternDesc() noexcept { return Desc; }
121
122
private:
123
  std::string Name;
124
  ExternDesc Desc;
125
};
126
127
/// AST Component::ImportDecl node.
128
class ImportDecl : public ExternDecl {};
129
130
/// AST Component::ExportDecl node.
131
class ExportDecl : public ExternDecl {};
132
133
// instancedecl ::= 0x00 t:<core:type>   => t
134
//                | 0x01 t:<type>        => t
135
//                | 0x02 a:<alias>       => a
136
//                | 0x04 ed:<exportdecl> => ed
137
138
/// AST Component::InstanceDecl node.
139
class InstanceDecl {
140
public:
141
0
  const CoreDefType *getCoreType() const noexcept {
142
0
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
143
0
  }
144
0
  void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept {
145
0
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T));
146
0
  }
147
148
0
  const DefType *getType() const noexcept {
149
0
    return std::get_if<std::unique_ptr<DefType>>(&Decl)->get();
150
0
  }
151
0
  void setType(std::unique_ptr<DefType> &&T) noexcept {
152
0
    Decl.emplace<std::unique_ptr<DefType>>(std::move(T));
153
0
  }
154
155
0
  const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); }
156
0
  void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); }
157
158
0
  const ExportDecl &getExport() const noexcept {
159
0
    return *std::get_if<ExportDecl>(&Decl);
160
0
  }
161
0
  void setExport(ExportDecl &&Exp) noexcept {
162
0
    Decl.emplace<ExportDecl>(std::move(Exp));
163
0
  }
164
165
0
  bool isCoreType() const noexcept {
166
0
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
167
0
  }
168
0
  bool isType() const noexcept {
169
0
    return std::holds_alternative<std::unique_ptr<DefType>>(Decl);
170
0
  }
171
0
  bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); }
172
0
  bool isExportDecl() const noexcept {
173
0
    return std::holds_alternative<ExportDecl>(Decl);
174
0
  }
175
176
private:
177
  std::variant<std::unique_ptr<CoreDefType>, std::unique_ptr<DefType>, Alias,
178
               ExportDecl>
179
      Decl;
180
};
181
182
// componentdecl ::= 0x03 id:<importdecl> => id
183
//                 | id:<instancedecl>    => id
184
185
/// AST Component::ComponentDecl node.
186
class ComponentDecl {
187
public:
188
0
  const ImportDecl &getImport() const noexcept {
189
0
    return *std::get_if<ImportDecl>(&Decl);
190
0
  }
191
0
  void setImport(ImportDecl &&Imp) noexcept {
192
0
    Decl.emplace<ImportDecl>(std::move(Imp));
193
0
  }
194
195
0
  const InstanceDecl &getInstance() const noexcept {
196
0
    return *std::get_if<InstanceDecl>(&Decl);
197
0
  }
198
0
  void setInstance(InstanceDecl &&Inst) noexcept {
199
0
    Decl.emplace<InstanceDecl>(std::move(Inst));
200
0
  }
201
202
0
  bool isImportDecl() const noexcept {
203
0
    return std::holds_alternative<ImportDecl>(Decl);
204
0
  }
205
0
  bool isInstanceDecl() const noexcept {
206
0
    return std::holds_alternative<InstanceDecl>(Decl);
207
0
  }
208
209
private:
210
  std::variant<ImportDecl, InstanceDecl> Decl;
211
};
212
213
} // namespace Component
214
} // namespace AST
215
} // namespace WasmEdge