Coverage Report

Created: 2026-08-14 06:41

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
1.93k
  std::string &getModuleName() noexcept { return ModName; }
39
0
  std::string_view getName() const noexcept { return Name; }
40
1.92k
  std::string &getName() noexcept { return Name; }
41
661
  CoreImportDesc getImportDesc() const noexcept { return Desc; }
42
1.92k
  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
822
  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
661
  const CoreImportDecl &getImport() const noexcept {
74
661
    return *std::get_if<CoreImportDecl>(&Decl);
75
661
  }
76
1.91k
  void setImport(CoreImportDecl &&Imp) noexcept {
77
1.91k
    Decl.emplace<CoreImportDecl>(std::move(Imp));
78
1.91k
  }
79
80
66
  const CoreDefType *getType() const noexcept {
81
66
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
82
66
  }
83
325
  void setType(std::unique_ptr<CoreDefType> &&Imp) noexcept {
84
325
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(Imp));
85
325
  }
86
87
199
  const CoreAlias &getAlias() const noexcept {
88
199
    return *std::get_if<CoreAlias>(&Decl);
89
199
  }
90
494
  void setAlias(CoreAlias &&A) noexcept {
91
494
    Decl.emplace<CoreAlias>(std::move(A));
92
494
  }
93
94
822
  const CoreExportDecl &getExport() const noexcept {
95
822
    return *std::get_if<CoreExportDecl>(&Decl);
96
822
  }
97
1.32k
  void setExport(CoreExportDecl &&Exp) noexcept {
98
1.32k
    Decl.emplace<CoreExportDecl>(std::move(Exp));
99
1.32k
  }
100
101
1.74k
  bool isImport() const noexcept {
102
1.74k
    return std::holds_alternative<CoreImportDecl>(Decl);
103
1.74k
  }
104
1.08k
  bool isType() const noexcept {
105
1.08k
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
106
1.08k
  }
107
1.02k
  bool isAlias() const noexcept {
108
1.02k
    return std::holds_alternative<CoreAlias>(Decl);
109
1.02k
  }
110
822
  bool isExport() const noexcept {
111
822
    return std::holds_alternative<CoreExportDecl>(Decl);
112
822
  }
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
1.18k
  std::string_view getName() const noexcept { return Name; }
129
1.75k
  std::string &getName() noexcept { return Name; }
130
1.19k
  const ExternDesc &getExternDesc() const noexcept { return Desc; }
131
1.72k
  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
38
  const CoreDefType *getCoreType() const noexcept {
153
38
    return std::get_if<std::unique_ptr<CoreDefType>>(&Decl)->get();
154
38
  }
155
561
  void setCoreType(std::unique_ptr<CoreDefType> &&T) noexcept {
156
561
    Decl.emplace<std::unique_ptr<CoreDefType>>(std::move(T));
157
561
  }
158
159
799
  const DefType *getType() const noexcept {
160
799
    return std::get_if<std::unique_ptr<DefType>>(&Decl)->get();
161
799
  }
162
2.16k
  void setType(std::unique_ptr<DefType> &&T) noexcept {
163
2.16k
    Decl.emplace<std::unique_ptr<DefType>>(std::move(T));
164
2.16k
  }
165
166
345
  const Alias &getAlias() const noexcept { return *std::get_if<Alias>(&Decl); }
167
1.56k
  void setAlias(Alias &&A) noexcept { Decl.emplace<Alias>(std::move(A)); }
168
169
500
  const ExportDecl &getExport() const noexcept {
170
500
    return *std::get_if<ExportDecl>(&Decl);
171
500
  }
172
430
  void setExport(ExportDecl &&Exp) noexcept {
173
430
    Decl.emplace<ExportDecl>(std::move(Exp));
174
430
  }
175
176
1.27k
  bool isCoreType() const noexcept {
177
1.27k
    return std::holds_alternative<std::unique_ptr<CoreDefType>>(Decl);
178
1.27k
  }
179
1.23k
  bool isType() const noexcept {
180
1.23k
    return std::holds_alternative<std::unique_ptr<DefType>>(Decl);
181
1.23k
  }
182
437
  bool isAlias() const noexcept { return std::holds_alternative<Alias>(Decl); }
183
503
  bool isExportDecl() const noexcept {
184
503
    return std::holds_alternative<ExportDecl>(Decl);
185
503
  }
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
695
  const ImportDecl &getImport() const noexcept {
200
695
    return *std::get_if<ImportDecl>(&Decl);
201
695
  }
202
1.28k
  void setImport(ImportDecl &&Imp) noexcept {
203
1.28k
    Decl.emplace<ImportDecl>(std::move(Imp));
204
1.28k
  }
205
206
1.01k
  const InstanceDecl &getInstance() const noexcept {
207
1.01k
    return *std::get_if<InstanceDecl>(&Decl);
208
1.01k
  }
209
3.31k
  void setInstance(InstanceDecl &&Inst) noexcept {
210
3.31k
    Decl.emplace<InstanceDecl>(std::move(Inst));
211
3.31k
  }
212
213
1.71k
  bool isImportDecl() const noexcept {
214
1.71k
    return std::holds_alternative<ImportDecl>(Decl);
215
1.71k
  }
216
1.01k
  bool isInstanceDecl() const noexcept {
217
1.01k
    return std::holds_alternative<InstanceDecl>(Decl);
218
1.01k
  }
219
220
private:
221
  std::variant<ImportDecl, InstanceDecl> Decl;
222
};
223
224
} // namespace Component
225
} // namespace AST
226
} // namespace WasmEdge