Coverage Report

Created: 2026-11-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.52k
  std::string_view getModuleName() const noexcept { return ModName; }
41
3.45k
  std::string &getModuleName() noexcept { return ModName; }
42
1.10k
  std::string_view getName() const noexcept { return Name; }
43
3.44k
  std::string &getName() noexcept { return Name; }
44
920
  const CoreImportDesc &getImportDesc() const noexcept { return Desc; }
45
3.44k
  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
328
  std::string_view getName() const noexcept { return Name; }
59
1.02k
  std::string &getName() noexcept { return Name; }
60
370
  const CoreImportDesc &getImportDesc() const noexcept { return Desc; }
61
1.02k
  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
920
  const CoreImportDecl &getImport() const noexcept {
77
920
    return *std::get_if<CoreImportDecl>(&Decl);
78
920
  }
79
3.42k
  void setImport(CoreImportDecl &&Imp) noexcept {
80
3.42k
    Decl.emplace<CoreImportDecl>(std::move(Imp));
81
3.42k
  }
82
83
339
  const CoreDefType *getType() const noexcept {
84
339
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
85
339
  }
86
431
  void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept {
87
431
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp));
88
431
  }
89
90
59
  const CoreAlias &getAlias() const noexcept {
91
59
    return *std::get_if<CoreAlias>(&Decl);
92
59
  }
93
133
  void setAlias(CoreAlias &&A) noexcept {
94
133
    Decl.emplace<CoreAlias>(std::move(A));
95
133
  }
96
97
370
  const CoreExportDecl &getExport() const noexcept {
98
370
    return *std::get_if<CoreExportDecl>(&Decl);
99
370
  }
100
1.00k
  void setExport(CoreExportDecl &&Exp) noexcept {
101
1.00k
    Decl.emplace<CoreExportDecl>(std::move(Exp));
102
1.00k
  }
103
104
1.68k
  bool isImport() const noexcept {
105
1.68k
    return std::holds_alternative<CoreImportDecl>(Decl);
106
1.68k
  }
107
768
  bool isType() const noexcept {
108
768
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
109
768
  }
110
429
  bool isAlias() const noexcept {
111
429
    return std::holds_alternative<CoreAlias>(Decl);
112
429
  }
113
370
  bool isExport() const noexcept {
114
370
    return std::holds_alternative<CoreExportDecl>(Decl);
115
370
  }
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
1.35k
  std::string_view getName() const noexcept { return Name; }
130
929
  std::string &getName() noexcept { return Name; }
131
514
  const ExternDesc &getExternDesc() const noexcept { return Desc; }
132
913
  ExternDesc &getExternDesc() noexcept { return Desc; }
133
929
  std::vector<std::string> &getImplements() noexcept { return Implements; }
134
503
  Span<const std::string> getImplements() const noexcept { return Implements; }
135
929
  std::vector<std::string> &getExternalIds() noexcept { return ExternalIds; }
136
0
  Span<const std::string> getExternalIds() const noexcept {
137
0
    return ExternalIds;
138
0
  }
139
929
  std::vector<std::string> &getVersionSuffixes() noexcept {
140
929
    return VersionSuffixes;
141
929
  }
142
503
  Span<const std::string> getVersionSuffixes() const noexcept {
143
503
    return VersionSuffixes;
144
503
  }
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
94
  const CoreDefType *getCoreType() const noexcept {
169
94
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
170
94
  }
171
477
  void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept {
172
477
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T));
173
477
  }
174
175
4.75k
  const DefType *getType() const noexcept {
176
4.75k
    return std::get_if<std::unique_ptr<DefType>>(&Decl)->get();
177
4.75k
  }
178
6.64k
  void setType(std::unique_ptr<DefType> &&T) noexcept {
179
6.64k
    Decl.emplace<std::unique_ptr<DefType>>(std::move(T));
180
6.64k
  }
181
182
1.10k
  const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); }
183
4.35k
  void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); }
184
185
728
  const ExportDecl &getExport() const noexcept {
186
728
    return *std::get_if<ExportDecl>(&Decl);
187
728
  }
188
600
  void setExport(ExportDecl &&Exp) noexcept {
189
600
    Decl.emplace<ExportDecl>(std::move(Exp));
190
600
  }
191
192
6.32k
  bool isCoreType() const noexcept {
193
6.32k
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
194
6.32k
  }
195
6.23k
  bool isType() const noexcept {
196
6.23k
    return std::holds_alternative<std::unique_ptr<DefType>>(Decl);
197
6.23k
  }
198
1.48k
  bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); }
199
728
  bool isExportDecl() const noexcept {
200
728
    return std::holds_alternative<ExportDecl>(Decl);
201
728
  }
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
134
  const ImportDecl &getImport() const noexcept {
216
134
    return *std::get_if<ImportDecl>(&Decl);
217
134
  }
218
308
  void setImport(ImportDecl &&Imp) noexcept {
219
308
    Decl.emplace<ImportDecl>(std::move(Imp));
220
308
  }
221
222
3.03k
  const InstanceDecl &getInstance() const noexcept {
223
3.03k
    return *std::get_if<InstanceDecl>(&Decl);
224
3.03k
  }
225
7.93k
  void setInstance(InstanceDecl &&Inst) noexcept {
226
7.93k
    Decl.emplace<InstanceDecl>(std::move(Inst));
227
7.93k
  }
228
229
3.17k
  bool isImportDecl() const noexcept {
230
3.17k
    return std::holds_alternative<ImportDecl>(Decl);
231
3.17k
  }
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