Coverage Report

Created: 2026-07-16 07: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
5.34k
  std::string &getModuleName() noexcept { return ModName; }
39
0
  std::string_view getName() const noexcept { return Name; }
40
5.33k
  std::string &getName() noexcept { return Name; }
41
198
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
42
5.33k
  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
869
  std::string &getName() noexcept { return Name; }
57
535
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
58
869
  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
198
  const CoreImportDecl &getImport() const noexcept {
74
198
    return *std::get_if<CoreImportDecl>(&Decl);
75
198
  }
76
5.31k
  void setImport(CoreImportDecl &&Imp) noexcept {
77
5.31k
    Decl.emplace<CoreImportDecl>(std::move(Imp));
78
5.31k
  }
79
80
58
  const CoreDefType *getType() const noexcept {
81
58
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
82
58
  }
83
774
  void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept {
84
774
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp));
85
774
  }
86
87
55
  const CoreAlias &getAlias() const noexcept {
88
55
    return *std::get_if<CoreAlias>(&Decl);
89
55
  }
90
131
  void setAlias(CoreAlias &&A) noexcept {
91
131
    Decl.emplace<CoreAlias>(std::move(A));
92
131
  }
93
94
535
  const CoreExportDecl &getExport() const noexcept {
95
535
    return *std::get_if<CoreExportDecl>(&Decl);
96
535
  }
97
860
  void setExport(CoreExportDecl &&Exp) noexcept {
98
860
    Decl.emplace<CoreExportDecl>(std::move(Exp));
99
860
  }
100
101
846
  bool isImport() const noexcept {
102
846
    return std::holds_alternative<CoreImportDecl>(Decl);
103
846
  }
104
648
  bool isType() const noexcept {
105
648
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
106
648
  }
107
590
  bool isAlias() const noexcept {
108
590
    return std::holds_alternative<CoreAlias>(Decl);
109
590
  }
110
535
  bool isExport() const noexcept {
111
535
    return std::holds_alternative<CoreExportDecl>(Decl);
112
535
  }
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
48
  std::string_view getName() const noexcept { return Name; }
129
1.07k
  std::string &getName() noexcept { return Name; }
130
60
  const ExternDesc &getExternDesc() const noexcept { return Desc; }
131
1.05k
  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
346
  const CoreDefType *getCoreType() const noexcept {
153
346
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
154
346
  }
155
1.79k
  void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept {
156
1.79k
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T));
157
1.79k
  }
158
159
133
  const DefType *getType() const noexcept {
160
133
    return std::get_if<std::unique_ptr<DefType>>(&Decl)->get();
161
133
  }
162
2.14k
  void setType(std::unique_ptr<DefType> &&T) noexcept {
163
2.14k
    Decl.emplace<std::unique_ptr<DefType>>(std::move(T));
164
2.14k
  }
165
166
3
  const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); }
167
7.02k
  void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); }
168
169
35
  const ExportDecl &getExport() const noexcept {
170
35
    return *std::get_if<ExportDecl>(&Decl);
171
35
  }
172
277
  void setExport(ExportDecl &&Exp) noexcept {
173
277
    Decl.emplace<ExportDecl>(std::move(Exp));
174
277
  }
175
176
513
  bool isCoreType() const noexcept {
177
513
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
178
513
  }
179
167
  bool isType() const noexcept {
180
167
    return std::holds_alternative<std::unique_ptr<DefType>>(Decl);
181
167
  }
182
34
  bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); }
183
45
  bool isExportDecl() const noexcept {
184
45
    return std::holds_alternative<ExportDecl>(Decl);
185
45
  }
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
27
  const ImportDecl &getImport() const noexcept {
200
27
    return *std::get_if<ImportDecl>(&Decl);
201
27
  }
202
770
  void setImport(ImportDecl &&Imp) noexcept {
203
770
    Decl.emplace<ImportDecl>(std::move(Imp));
204
770
  }
205
206
212
  const InstanceDecl &getInstance() const noexcept {
207
212
    return *std::get_if<InstanceDecl>(&Decl);
208
212
  }
209
9.85k
  void setInstance(InstanceDecl &&Inst) noexcept {
210
9.85k
    Decl.emplace<InstanceDecl>(std::move(Inst));
211
9.85k
  }
212
213
239
  bool isImportDecl() const noexcept {
214
239
    return std::holds_alternative<ImportDecl>(Decl);
215
239
  }
216
212
  bool isInstanceDecl() const noexcept {
217
212
    return std::holds_alternative<InstanceDecl>(Decl);
218
212
  }
219
220
private:
221
  std::variant<ImportDecl, InstanceDecl> Decl;
222
};
223
224
} // namespace Component
225
} // namespace AST
226
} // namespace WasmEdge