Coverage Report

Created: 2026-08-13 06:09

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
19
#include <string>
20
#include <string_view>
21
#include <variant>
22
23
namespace WasmEdge {
24
namespace AST {
25
namespace Component {
26
27
// Need the forward declaration.
28
class CoreDefType;
29
class DefType;
30
31
// core:importdecl ::= m:<core:name> n:<core:name> d:<core:importdesc>
32
//                   => (import m n d)
33
34
/// AST Component::CoreImportDecl node.
35
class CoreImportDecl {
36
public:
37
0
  std::string_view getModuleName() const noexcept { return ModName; }
38
4.75k
  std::string &getModuleName() noexcept { return ModName; }
39
0
  std::string_view getName() const noexcept { return Name; }
40
4.74k
  std::string &getName() noexcept { return Name; }
41
542
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
42
4.74k
  CoreImportDesc &getImportDesc() noexcept { return Desc; }
43
44
private:
45
  std::string ModName;
46
  std::string Name;
47
  CoreImportDesc Desc;
48
};
49
50
// core:exportdecl ::= n:<core:name> d:<core:importdesc> => (export n d)
51
52
/// AST Component::CoreExportDecl node.
53
class CoreExportDecl {
54
public:
55
0
  std::string_view getName() const noexcept { return Name; }
56
1.33k
  std::string &getName() noexcept { return Name; }
57
716
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
58
1.33k
  CoreImportDesc &getImportDesc() noexcept { return Desc; }
59
60
private:
61
  std::string Name;
62
  CoreImportDesc Desc;
63
};
64
65
// core:moduledecl ::= 0x00 i:<core:importdecl> => i
66
//                   | 0x01 t:<core:type>       => t
67
//                   | 0x02 a:<core:alias>      => a
68
//                   | 0x03 e:<core:exportdecl> => e
69
70
/// AST Component::CoreModuleDecl node.
71
class CoreModuleDecl {
72
public:
73
542
  const CoreImportDecl &getImport() const noexcept {
74
542
    return *std::get_if<CoreImportDecl>(&Decl);
75
542
  }
76
4.72k
  void setImport(CoreImportDecl &&Imp) noexcept {
77
4.72k
    Decl.emplace<CoreImportDecl>(std::move(Imp));
78
4.72k
  }
79
80
48
  const CoreDefType *getType() const noexcept {
81
48
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
82
48
  }
83
290
  void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept {
84
290
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp));
85
290
  }
86
87
138
  const CoreAlias &getAlias() const noexcept {
88
138
    return *std::get_if<CoreAlias>(&Decl);
89
138
  }
90
311
  void setAlias(CoreAlias &&A) noexcept {
91
311
    Decl.emplace<CoreAlias>(std::move(A));
92
311
  }
93
94
716
  const CoreExportDecl &getExport() const noexcept {
95
716
    return *std::get_if<CoreExportDecl>(&Decl);
96
716
  }
97
1.32k
  void setExport(CoreExportDecl &&Exp) noexcept {
98
1.32k
    Decl.emplace<CoreExportDecl>(std::move(Exp));
99
1.32k
  }
100
101
1.44k
  bool isImport() const noexcept {
102
1.44k
    return std::holds_alternative<CoreImportDecl>(Decl);
103
1.44k
  }
104
902
  bool isType() const noexcept {
105
902
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
106
902
  }
107
854
  bool isAlias() const noexcept {
108
854
    return std::holds_alternative<CoreAlias>(Decl);
109
854
  }
110
716
  bool isExport() const noexcept {
111
716
    return std::holds_alternative<CoreExportDecl>(Decl);
112
716
  }
113
114
private:
115
  std::variant<CoreImportDecl, std::unique_ptr<CoreDefType>, CoreAlias,
116
               CoreExportDecl>
117
      Decl;
118
};
119
120
// exportdecl  ::= en:<exportname'> ed:<externdesc> => (export en ed)
121
// exportname' ::= 0x00 len:<u32> en:<exportname>   => en (if len = |en|)
122
// importdecl  ::= in:<importname'> ed:<externdesc> => (import in ed)
123
// importname' ::= 0x00 len:<u32> in:<importname>   => in (if len = |in|)
124
125
/// Base class of Component::ImportDecl and Component::ExportDecl node.
126
class ExternDecl {
127
public:
128
820
  std::string_view getName() const noexcept { return Name; }
129
1.57k
  std::string &getName() noexcept { return Name; }
130
825
  const ExternDesc &getExternDesc() const noexcept { return Desc; }
131
1.56k
  ExternDesc &getExternDesc() noexcept { return Desc; }
132
133
private:
134
  std::string Name;
135
  ExternDesc Desc;
136
};
137
138
/// AST Component::ImportDecl node.
139
class ImportDecl : public ExternDecl {};
140
141
/// AST Component::ExportDecl node.
142
class ExportDecl : public ExternDecl {};
143
144
// instancedecl ::= 0x00 t:<core:type>   => t
145
//                | 0x01 t:<type>        => t
146
//                | 0x02 a:<alias>       => a
147
//                | 0x04 ed:<exportdecl> => ed
148
149
/// AST Component::InstanceDecl node.
150
class InstanceDecl {
151
public:
152
43
  const CoreDefType *getCoreType() const noexcept {
153
43
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
154
43
  }
155
596
  void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept {
156
596
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T));
157
596
  }
158
159
810
  const DefType *getType() const noexcept {
160
810
    return std::get_if<std::unique_ptr<DefType>>(&Decl)->get();
161
810
  }
162
2.13k
  void setType(std::unique_ptr<DefType> &&T) noexcept {
163
2.13k
    Decl.emplace<std::unique_ptr<DefType>>(std::move(T));
164
2.13k
  }
165
166
350
  const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); }
167
1.35k
  void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); }
168
169
485
  const ExportDecl &getExport() const noexcept {
170
485
    return *std::get_if<ExportDecl>(&Decl);
171
485
  }
172
431
  void setExport(ExportDecl &&Exp) noexcept {
173
431
    Decl.emplace<ExportDecl>(std::move(Exp));
174
431
  }
175
176
1.29k
  bool isCoreType() const noexcept {
177
1.29k
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
178
1.29k
  }
179
1.24k
  bool isType() const noexcept {
180
1.24k
    return std::holds_alternative<std::unique_ptr<DefType>>(Decl);
181
1.24k
  }
182
439
  bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); }
183
490
  bool isExportDecl() const noexcept {
184
490
    return std::holds_alternative<ExportDecl>(Decl);
185
490
  }
186
187
private:
188
  std::variant<std::unique_ptr<CoreDefType>, std::unique_ptr<DefType>, Alias,
189
               ExportDecl>
190
      Decl;
191
};
192
193
// componentdecl ::= 0x03 id:<importdecl> => id
194
//                 | id:<instancedecl>    => id
195
196
/// AST Component::ComponentDecl node.
197
class ComponentDecl {
198
public:
199
342
  const ImportDecl &getImport() const noexcept {
200
342
    return *std::get_if<ImportDecl>(&Decl);
201
342
  }
202
1.11k
  void setImport(ImportDecl &&Imp) noexcept {
203
1.11k
    Decl.emplace<ImportDecl>(std::move(Imp));
204
1.11k
  }
205
206
1.03k
  const InstanceDecl &getInstance() const noexcept {
207
1.03k
    return *std::get_if<InstanceDecl>(&Decl);
208
1.03k
  }
209
3.56k
  void setInstance(InstanceDecl &&Inst) noexcept {
210
3.56k
    Decl.emplace<InstanceDecl>(std::move(Inst));
211
3.56k
  }
212
213
1.37k
  bool isImportDecl() const noexcept {
214
1.37k
    return std::holds_alternative<ImportDecl>(Decl);
215
1.37k
  }
216
1.03k
  bool isInstanceDecl() const noexcept {
217
1.03k
    return std::holds_alternative<InstanceDecl>(Decl);
218
1.03k
  }
219
220
private:
221
  std::variant<ImportDecl, InstanceDecl> Decl;
222
};
223
224
} // namespace Component
225
} // namespace AST
226
} // namespace WasmEdge