/src/WasmEdge/lib/validator/component_context.cpp
Line | Count | Source |
1 | | #include "validator/component_context.h" |
2 | | |
3 | | namespace WasmEdge { |
4 | | namespace Validator { |
5 | | |
6 | | using Sort = AST::Component::Sort; |
7 | | |
8 | | uint32_t |
9 | 53.9k | ComponentContext::Context::getSortIndexSize(Sort::SortType ST) const noexcept { |
10 | 53.9k | switch (ST) { |
11 | 2.37k | case Sort::SortType::Func: |
12 | 2.37k | return static_cast<uint32_t>(Funcs.size()); |
13 | 1.94k | case Sort::SortType::Value: |
14 | 1.94k | return ValueCount; |
15 | 42.4k | case Sort::SortType::Type: |
16 | 42.4k | return static_cast<uint32_t>(Types.size()); |
17 | 4.82k | case Sort::SortType::Component: |
18 | 4.82k | return static_cast<uint32_t>(Components.size()); |
19 | 2.28k | case Sort::SortType::Instance: |
20 | 2.28k | return static_cast<uint32_t>(Instances.size()); |
21 | 0 | default: |
22 | 0 | return 0; |
23 | 53.9k | } |
24 | 53.9k | } |
25 | | |
26 | | uint32_t ComponentContext::Context::getCoreSortIndexSize( |
27 | 18.3k | Sort::CoreSortType ST) const noexcept { |
28 | 18.3k | switch (ST) { |
29 | 16.9k | case Sort::CoreSortType::Func: |
30 | 16.9k | return static_cast<uint32_t>(CoreFuncs.size()); |
31 | 104 | case Sort::CoreSortType::Table: |
32 | 104 | return static_cast<uint32_t>(CoreTables.size()); |
33 | 22 | case Sort::CoreSortType::Memory: |
34 | 22 | return static_cast<uint32_t>(CoreMemories.size()); |
35 | 5 | case Sort::CoreSortType::Global: |
36 | 5 | return static_cast<uint32_t>(CoreGlobals.size()); |
37 | 70 | case Sort::CoreSortType::Tag: |
38 | 70 | return CoreTagCount; |
39 | 507 | case Sort::CoreSortType::Type: |
40 | 507 | return static_cast<uint32_t>(CoreTypes.size()); |
41 | 316 | case Sort::CoreSortType::Module: |
42 | 316 | return static_cast<uint32_t>(CoreModules.size()); |
43 | 396 | case Sort::CoreSortType::Instance: |
44 | 396 | return static_cast<uint32_t>(CoreInstances.size()); |
45 | 0 | default: |
46 | 0 | return 0; |
47 | 18.3k | } |
48 | 18.3k | } |
49 | | |
50 | 7.31k | uint32_t ComponentContext::incSortIndexSize(Sort::SortType ST) noexcept { |
51 | 7.31k | switch (ST) { |
52 | 16 | case Sort::SortType::Func: |
53 | 16 | return addFunc(); |
54 | 745 | case Sort::SortType::Value: |
55 | 745 | return addValue(); |
56 | 4.84k | case Sort::SortType::Type: |
57 | 4.84k | return addType(); |
58 | 507 | case Sort::SortType::Component: |
59 | 507 | return addComponent(); |
60 | 1.20k | case Sort::SortType::Instance: |
61 | 1.20k | return addInstance(); |
62 | 0 | default: |
63 | 0 | return 0; |
64 | 7.31k | } |
65 | 7.31k | } |
66 | | |
67 | | uint32_t |
68 | 750 | ComponentContext::incCoreSortIndexSize(Sort::CoreSortType ST) noexcept { |
69 | 750 | switch (ST) { |
70 | 309 | case Sort::CoreSortType::Func: |
71 | 309 | return addCoreFunc(); |
72 | 80 | case Sort::CoreSortType::Table: |
73 | 80 | return addCoreTable(); |
74 | 12 | case Sort::CoreSortType::Memory: |
75 | 12 | return addCoreMemory(); |
76 | 3 | case Sort::CoreSortType::Global: |
77 | 3 | return addCoreGlobal(); |
78 | 61 | case Sort::CoreSortType::Tag: |
79 | 61 | return addCoreTag(); |
80 | 285 | case Sort::CoreSortType::Type: |
81 | 285 | return addCoreType(); |
82 | 0 | case Sort::CoreSortType::Module: |
83 | 0 | return addCoreModule(); |
84 | 0 | case Sort::CoreSortType::Instance: |
85 | 0 | return addCoreInstance(); |
86 | 0 | default: |
87 | 0 | return 0; |
88 | 750 | } |
89 | 750 | } |
90 | | |
91 | | namespace { |
92 | | constexpr std::string_view ConstructorTag{"[constructor]"}; |
93 | | |
94 | | bool addStronglyUniqueName(std::unordered_set<std::string> &Names, |
95 | 7.94k | const ComponentName &Name) noexcept { |
96 | 7.94k | switch (Name.getKind()) { |
97 | 320 | case ComponentNameKind::Constructor: |
98 | 320 | case ComponentNameKind::Method: |
99 | 373 | case ComponentNameKind::Static: |
100 | 2.57k | case ComponentNameKind::InterfaceType: |
101 | 7.92k | case ComponentNameKind::Label: |
102 | 7.92k | case ComponentNameKind::LockedDep: |
103 | 7.92k | case ComponentNameKind::UnlockedDep: |
104 | 7.93k | case ComponentNameKind::Url: |
105 | 7.94k | case ComponentNameKind::Integrity: |
106 | 7.94k | break; |
107 | 0 | default: |
108 | 0 | return false; |
109 | 7.94k | } |
110 | | |
111 | 8.27k | auto toLowerString = [](std::string_view SV) { |
112 | 8.27k | std::string Result = std::string(SV); |
113 | 8.27k | std::transform( |
114 | 8.27k | Result.begin(), Result.end(), Result.begin(), |
115 | 74.7k | [](unsigned char C) { return static_cast<char>(std::tolower(C)); }); |
116 | 8.27k | return Result; |
117 | 8.27k | }; |
118 | | |
119 | | // Handle the Constructor case separately. |
120 | 7.94k | if (Name.getKind() == ComponentNameKind::Constructor) { |
121 | 320 | std::string LowerCase = toLowerString(Name.getOriginalName()); |
122 | 320 | std::string Label = std::string(Name.getNoTagName()); |
123 | | // Check for conflicts with existing constructors. |
124 | 320 | if (Names.count(LowerCase)) { |
125 | 2 | return false; |
126 | 2 | } |
127 | | |
128 | 318 | if (Names.count(toLowerString(Label))) { |
129 | 0 | if (!Names.count(Label)) { |
130 | 0 | return false; |
131 | 0 | } |
132 | | // By rule, a constructor [constructor]X and X are strongly-unique. |
133 | | // If X and its lower-case x form both exist, it means x comes from X. |
134 | 0 | } |
135 | 318 | Names.insert(LowerCase); |
136 | 318 | Names.insert(std::string(Name.getOriginalName())); |
137 | 318 | return true; |
138 | 318 | } |
139 | | |
140 | | // For case 2, L and L.L are not strongly-unique together. |
141 | 7.62k | std::string Normal = std::string(Name.getNoTagName()); |
142 | 7.62k | std::string UniForm = toLowerString(Normal); |
143 | 7.62k | std::string LdL = |
144 | 7.62k | std::string(Name.getNoTagName()) + "." + std::string(Name.getNoTagName()); |
145 | | |
146 | 7.62k | if (Names.count(LdL)) { |
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | 7.62k | if (Normal.find('.') != std::string::npos) { |
151 | 492 | std::string Left, Right; |
152 | 492 | size_t Pos = Normal.find('.'); |
153 | 492 | Left = Normal.substr(0, Pos); |
154 | 492 | Right = Normal.substr(Pos + 1); |
155 | 492 | if (Left == Right) { |
156 | | // Conflict with l.l and [*]l. |
157 | 14 | if (Names.count(toLowerString(Left))) { |
158 | 0 | return false; |
159 | 0 | } |
160 | 14 | } |
161 | 492 | } |
162 | | |
163 | | // Case 3: check existing names. |
164 | 7.62k | if (Names.count(UniForm)) { |
165 | 111 | return false; |
166 | 111 | } |
167 | | |
168 | | // Special case: check conflicts with constructor names. |
169 | 7.51k | std::string ConstrName = std::string(ConstructorTag) + UniForm; |
170 | 7.51k | if (Names.count(ConstrName)) { |
171 | 0 | if (!Names.count(std::string(ConstructorTag) + Normal)) { |
172 | 0 | return false; |
173 | 0 | } |
174 | | // By rule, a constructor [constructor]X and X are strongly-unique. |
175 | | // If [constructor]X and its lower-case [constructor]x form both exist, it |
176 | | // means [constructor]x comes from [constructor]X. |
177 | 0 | } |
178 | 7.51k | Names.insert(Normal); |
179 | 7.51k | Names.insert(UniForm); |
180 | 7.51k | return true; |
181 | 7.51k | } |
182 | | } // namespace |
183 | | |
184 | | bool ComponentContext::Context::AddImportedName( |
185 | 6.90k | const ComponentName &Name) noexcept { |
186 | 6.90k | return addStronglyUniqueName(ImportedNames, Name); |
187 | 6.90k | } |
188 | | |
189 | | bool ComponentContext::Context::AddExportedName( |
190 | 1.04k | const ComponentName &Name) noexcept { |
191 | 1.04k | return addStronglyUniqueName(ExportedNames, Name); |
192 | 1.04k | } |
193 | | } // namespace Validator |
194 | | } // namespace WasmEdge |