• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 15832914654

23 Jun 2025 07:08PM UTC coverage: 64.215% (+0.02%) from 64.191%
15832914654

Pull #102

github

web-flow
Merge dd90f12a7 into 989478b05
Pull Request #102: simplifies string enums for plugins

13 of 17 new or added lines in 6 files covered. (76.47%)

48 existing lines in 2 files now uncovered.

8226 of 12810 relevant lines covered (64.22%)

136.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.17
/src/serializer/json_serializer.cpp
1
#include "sdfg/serializer/json_serializer.h"
2

3
#include <cassert>
4
#include <memory>
5
#include <unordered_map>
6
#include <utility>
7
#include <vector>
8

9
#include "sdfg/builder/structured_sdfg_builder.h"
10
#include "sdfg/data_flow/library_node.h"
11
#include "sdfg/element.h"
12
#include "sdfg/serializer/library_node_serializer_registry.h"
13
#include "sdfg/structured_control_flow/block.h"
14
#include "sdfg/structured_control_flow/for.h"
15
#include "sdfg/structured_control_flow/if_else.h"
16
#include "sdfg/structured_control_flow/return.h"
17
#include "sdfg/structured_control_flow/sequence.h"
18
#include "sdfg/structured_control_flow/while.h"
19
#include "sdfg/structured_sdfg.h"
20
#include "sdfg/symbolic/symbolic.h"
21
#include "sdfg/types/function.h"
22
#include "sdfg/types/scalar.h"
23
#include "sdfg/types/type.h"
24
#include "symengine/expression.h"
25
#include "symengine/logic.h"
26
#include "symengine/symengine_rcp.h"
27

28
namespace sdfg {
29
namespace serializer {
30

31
FunctionType function_type_from_string(const std::string& str) {
3✔
32
    if (str == FunctionType_CPU.value()) {
3✔
33
        return FunctionType_CPU;
3✔
34
    } else if (str == FunctionType_NV_GLOBAL.value()) {
×
35
        return FunctionType_NV_GLOBAL;
×
36
    }
37

NEW
38
    return FunctionType(str);
×
39
}
3✔
40

41
types::StorageType storage_type_from_string(const std::string& str) {
31✔
42
    if (str == types::StorageType_CPU_Heap.value()) {
31✔
43
        return types::StorageType_CPU_Heap;
×
44
    } else if (str == types::StorageType_CPU_Stack.value()) {
31✔
45
        return types::StorageType_CPU_Stack;
31✔
46
    } else if (str == types::StorageType_NV_Global.value()) {
×
47
        return types::StorageType_NV_Global;
×
48
    } else if (str == types::StorageType_NV_Shared.value()) {
×
49
        return types::StorageType_NV_Shared;
×
50
    } else if (str == types::StorageType_NV_Constant.value()) {
×
51
        return types::StorageType_NV_Constant;
×
52
    } else if (str == types::StorageType_NV_Generic.value()) {
×
53
        return types::StorageType_NV_Generic;
×
54
    }
55

NEW
56
    return types::StorageType(str);
×
57
}
31✔
58

59
structured_control_flow::ScheduleType schedule_type_from_string(const std::string& str) {
1✔
60
    if (str == structured_control_flow::ScheduleType_Sequential.value()) {
1✔
61
        return structured_control_flow::ScheduleType_Sequential;
1✔
62
    } else if (str == structured_control_flow::ScheduleType_CPU_Parallel.value()) {
×
63
        return structured_control_flow::ScheduleType_CPU_Parallel;
×
64
    }
65

NEW
66
    return structured_control_flow::ScheduleType(str);
×
67
}
1✔
68

69
/*
70
 * * JSONSerializer class
71
 * * Serialization logic
72
 */
73

74
nlohmann::json JSONSerializer::serialize(std::unique_ptr<sdfg::StructuredSDFG>& sdfg) {
3✔
75
    nlohmann::json j;
3✔
76

77
    j["name"] = sdfg->name();
3✔
78
    j["type"] = std::string(sdfg->type().value());
3✔
79

80
    j["structures"] = nlohmann::json::array();
3✔
81
    for (const auto& structure_name : sdfg->structures()) {
3✔
82
        const auto& structure = sdfg->structure(structure_name);
×
83
        nlohmann::json structure_json;
×
84
        structure_definition_to_json(structure_json, structure);
×
85
        j["structures"].push_back(structure_json);
×
86
    }
×
87

88
    j["containers"] = nlohmann::json::object();
3✔
89
    for (const auto& container : sdfg->containers()) {
8✔
90
        nlohmann::json desc;
5✔
91
        type_to_json(desc, sdfg->type(container));
5✔
92
        j["containers"][container] = desc;
5✔
93
    }
5✔
94

95
    j["arguments"] = nlohmann::json::array();
3✔
96
    for (const auto& argument : sdfg->arguments()) {
5✔
97
        j["arguments"].push_back(argument);
2✔
98
    }
99

100
    j["externals"] = nlohmann::json::array();
3✔
101
    for (const auto& external : sdfg->externals()) {
3✔
102
        j["externals"].push_back(external);
×
103
    }
104

105
    j["metadata"] = nlohmann::json::object();
3✔
106
    for (const auto& entry : sdfg->metadata()) {
4✔
107
        j["metadata"][entry.first] = entry.second;
1✔
108
    }
109

110
    // Walk the SDFG
111
    nlohmann::json root_json;
3✔
112
    sequence_to_json(root_json, sdfg->root());
3✔
113
    j["root"] = root_json;
3✔
114

115
    return j;
3✔
116
}
3✔
117

118
void JSONSerializer::dataflow_to_json(nlohmann::json& j, const data_flow::DataFlowGraph& dataflow) {
20✔
119
    j["type"] = "dataflow";
20✔
120
    j["nodes"] = nlohmann::json::array();
20✔
121
    j["edges"] = nlohmann::json::array();
20✔
122

123
    for (auto& node : dataflow.nodes()) {
40✔
124
        nlohmann::json node_json;
20✔
125
        node_json["element_id"] = node.element_id();
20✔
126

127
        node_json["debug_info"] = nlohmann::json::object();
20✔
128
        debug_info_to_json(node_json["debug_info"], node.debug_info());
20✔
129

130
        if (auto tasklet = dynamic_cast<const data_flow::Tasklet*>(&node)) {
20✔
131
            node_json["type"] = "tasklet";
5✔
132
            node_json["code"] = tasklet->code();
5✔
133
            node_json["inputs"] = nlohmann::json::array();
5✔
134
            for (auto& input : tasklet->inputs()) {
15✔
135
                nlohmann::json input_json;
10✔
136
                nlohmann::json type_json;
10✔
137
                type_to_json(type_json, input.second);
10✔
138
                input_json["type"] = type_json;
10✔
139
                input_json["name"] = input.first;
10✔
140
                node_json["inputs"].push_back(input_json);
10✔
141
            }
10✔
142
            node_json["output"] = nlohmann::json::object();
5✔
143
            node_json["output"]["name"] = tasklet->output().first;
5✔
144
            nlohmann::json type_json;
5✔
145
            type_to_json(type_json, tasklet->output().second);
5✔
146
            node_json["output"]["type"] = type_json;
5✔
147
            // node_json["conditional"] = tasklet->is_conditional();
148
            // if (tasklet->is_conditional()) {
149
            //     node_json["condition"] = dumps_expression(tasklet->condition());
150
            // }
151
        } else if (auto lib_node = dynamic_cast<const data_flow::LibraryNode*>(&node)) {
20✔
152
            auto serializer_fn =
153
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(
×
154
                    lib_node->code().value());
×
155
            if (serializer_fn == nullptr) {
×
156
                throw std::runtime_error("Unknown library node code: " +
×
157
                                         std::string(lib_node->code().value()));
×
158
            }
159
            auto serializer = serializer_fn();
×
160
            auto lib_node_json = serializer->serialize(*lib_node);
×
161
            node_json.merge_patch(lib_node_json);
×
162
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
15✔
163
            node_json["type"] = "access_node";
15✔
164
            node_json["data"] = code_node->data();
15✔
165
        } else {
15✔
166
            throw std::runtime_error("Unknown node type");
×
167
        }
168

169
        j["nodes"].push_back(node_json);
20✔
170
    }
20✔
171

172
    for (auto& edge : dataflow.edges()) {
35✔
173
        nlohmann::json edge_json;
15✔
174
        edge_json["element_id"] = edge.element_id();
15✔
175

176
        edge_json["debug_info"] = nlohmann::json::object();
15✔
177
        debug_info_to_json(edge_json["debug_info"], edge.debug_info());
15✔
178

179
        edge_json["src"] = edge.src().element_id();
15✔
180
        edge_json["dst"] = edge.dst().element_id();
15✔
181

182
        edge_json["src_conn"] = edge.src_conn();
15✔
183
        edge_json["dst_conn"] = edge.dst_conn();
15✔
184

185
        edge_json["subset"] = nlohmann::json::array();
15✔
186
        for (auto& subset : edge.subset()) {
21✔
187
            edge_json["subset"].push_back(expression(subset));
6✔
188
        }
189

190
        j["edges"].push_back(edge_json);
15✔
191
    }
15✔
192
}
20✔
193

194
void JSONSerializer::block_to_json(nlohmann::json& j, const structured_control_flow::Block& block) {
18✔
195
    j["type"] = "block";
18✔
196
    j["element_id"] = block.element_id();
18✔
197

198
    j["debug_info"] = nlohmann::json::object();
18✔
199
    debug_info_to_json(j["debug_info"], block.debug_info());
18✔
200

201
    nlohmann::json dataflow_json;
18✔
202
    dataflow_to_json(dataflow_json, block.dataflow());
18✔
203
    j["dataflow"] = dataflow_json;
18✔
204
}
18✔
205

206
void JSONSerializer::for_to_json(nlohmann::json& j, const structured_control_flow::For& for_node) {
2✔
207
    j["type"] = "for";
2✔
208
    j["element_id"] = for_node.element_id();
2✔
209

210
    j["debug_info"] = nlohmann::json::object();
2✔
211
    debug_info_to_json(j["debug_info"], for_node.debug_info());
2✔
212

213
    j["indvar"] = expression(for_node.indvar());
2✔
214
    j["init"] = expression(for_node.init());
2✔
215
    j["condition"] = expression(for_node.condition());
2✔
216
    j["update"] = expression(for_node.update());
2✔
217

218
    nlohmann::json body_json;
2✔
219
    sequence_to_json(body_json, for_node.root());
2✔
220
    j["root"] = body_json;
2✔
221
}
2✔
222

223
void JSONSerializer::if_else_to_json(nlohmann::json& j,
2✔
224
                                     const structured_control_flow::IfElse& if_else_node) {
225
    j["type"] = "if_else";
2✔
226
    j["element_id"] = if_else_node.element_id();
2✔
227

228
    j["debug_info"] = nlohmann::json::object();
2✔
229
    debug_info_to_json(j["debug_info"], if_else_node.debug_info());
2✔
230

231
    j["branches"] = nlohmann::json::array();
2✔
232
    for (size_t i = 0; i < if_else_node.size(); i++) {
6✔
233
        nlohmann::json branch_json;
4✔
234
        branch_json["condition"] = expression(if_else_node.at(i).second);
4✔
235
        nlohmann::json body_json;
4✔
236
        sequence_to_json(body_json, if_else_node.at(i).first);
4✔
237
        branch_json["root"] = body_json;
4✔
238
        j["branches"].push_back(branch_json);
4✔
239
    }
4✔
240
}
2✔
241

242
void JSONSerializer::while_node_to_json(nlohmann::json& j,
5✔
243
                                        const structured_control_flow::While& while_node) {
244
    j["type"] = "while";
5✔
245
    j["element_id"] = while_node.element_id();
5✔
246

247
    j["debug_info"] = nlohmann::json::object();
5✔
248
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
249

250
    nlohmann::json body_json;
5✔
251
    sequence_to_json(body_json, while_node.root());
5✔
252
    j["root"] = body_json;
5✔
253
}
5✔
254

255
void JSONSerializer::break_node_to_json(nlohmann::json& j,
2✔
256
                                        const structured_control_flow::Break& break_node) {
257
    j["type"] = "break";
2✔
258
    j["element_id"] = break_node.element_id();
2✔
259

260
    j["debug_info"] = nlohmann::json::object();
2✔
261
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
262
}
2✔
263

264
void JSONSerializer::continue_node_to_json(nlohmann::json& j,
2✔
265
                                           const structured_control_flow::Continue& continue_node) {
266
    j["type"] = "continue";
2✔
267
    j["element_id"] = continue_node.element_id();
2✔
268

269
    j["debug_info"] = nlohmann::json::object();
2✔
270
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
271
}
2✔
272

273
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
274
    j["type"] = "map";
2✔
275
    j["element_id"] = map_node.element_id();
2✔
276

277
    j["debug_info"] = nlohmann::json::object();
2✔
278
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
279

280
    j["indvar"] = expression(map_node.indvar());
2✔
281
    j["init"] = expression(map_node.init());
2✔
282
    j["condition"] = expression(map_node.condition());
2✔
283
    j["update"] = expression(map_node.update());
2✔
284

285
    j["schedule_type"] = std::string(map_node.schedule_type().value());
2✔
286

287
    nlohmann::json body_json;
2✔
288
    sequence_to_json(body_json, map_node.root());
2✔
289
    j["root"] = body_json;
2✔
290
}
2✔
291

292
void JSONSerializer::return_node_to_json(nlohmann::json& j,
2✔
293
                                         const structured_control_flow::Return& return_node) {
294
    j["type"] = "return";
2✔
295
    j["element_id"] = return_node.element_id();
2✔
296

297
    j["debug_info"] = nlohmann::json::object();
2✔
298
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
299
}
2✔
300

301
void JSONSerializer::sequence_to_json(nlohmann::json& j,
19✔
302
                                      const structured_control_flow::Sequence& sequence) {
303
    j["type"] = "sequence";
19✔
304
    j["element_id"] = sequence.element_id();
19✔
305

306
    j["debug_info"] = nlohmann::json::object();
19✔
307
    debug_info_to_json(j["debug_info"], sequence.debug_info());
19✔
308

309
    j["children"] = nlohmann::json::array();
19✔
310
    j["transitions"] = nlohmann::json::array();
19✔
311

312
    for (size_t i = 0; i < sequence.size(); i++) {
39✔
313
        nlohmann::json child_json;
20✔
314
        auto& child = sequence.at(i).first;
20✔
315
        auto& transition = sequence.at(i).second;
20✔
316

317
        if (auto block = dynamic_cast<const structured_control_flow::Block*>(&child)) {
20✔
318
            block_to_json(child_json, *block);
16✔
319
        } else if (auto for_node = dynamic_cast<const structured_control_flow::For*>(&child)) {
20✔
320
            for_to_json(child_json, *for_node);
×
321
        } else if (auto sequence_node =
4✔
322
                       dynamic_cast<const structured_control_flow::Sequence*>(&child)) {
4✔
323
            sequence_to_json(child_json, *sequence_node);
×
324
        } else if (auto condition_node =
4✔
325
                       dynamic_cast<const structured_control_flow::IfElse*>(&child)) {
4✔
326
            if_else_to_json(child_json, *condition_node);
×
327
        } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&child)) {
4✔
328
            while_node_to_json(child_json, *while_node);
×
329
        } else if (auto return_node =
4✔
330
                       dynamic_cast<const structured_control_flow::Return*>(&child)) {
4✔
331
            return_node_to_json(child_json, *return_node);
×
332
        } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&child)) {
4✔
333
            break_node_to_json(child_json, *break_node);
2✔
334
        } else if (auto continue_node =
4✔
335
                       dynamic_cast<const structured_control_flow::Continue*>(&child)) {
2✔
336
            continue_node_to_json(child_json, *continue_node);
2✔
337
        } else if (auto map_node = dynamic_cast<const structured_control_flow::Map*>(&child)) {
2✔
338
            map_to_json(child_json, *map_node);
×
339
        } else {
×
340
            throw std::runtime_error("Unknown child type");
×
341
        }
342

343
        j["children"].push_back(child_json);
20✔
344

345
        // Add transition information
346
        nlohmann::json transition_json;
20✔
347
        transition_json["type"] = "transition";
20✔
348
        transition_json["element_id"] = transition.element_id();
20✔
349

350
        transition_json["debug_info"] = nlohmann::json::object();
20✔
351
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
20✔
352

353
        transition_json["assignments"] = nlohmann::json::array();
20✔
354
        for (const auto& assignment : transition.assignments()) {
22✔
355
            nlohmann::json assignment_json;
2✔
356
            assignment_json["symbol"] = expression(assignment.first);
2✔
357
            assignment_json["expression"] = expression(assignment.second);
2✔
358
            transition_json["assignments"].push_back(assignment_json);
2✔
359
        }
2✔
360

361
        j["transitions"].push_back(transition_json);
20✔
362
    }
20✔
363
}
19✔
364

365
void JSONSerializer::type_to_json(nlohmann::json& j, const types::IType& type) {
43✔
366
    if (auto scalar_type = dynamic_cast<const types::Scalar*>(&type)) {
43✔
367
        j["type"] = "scalar";
33✔
368
        j["primitive_type"] = scalar_type->primitive_type();
33✔
369
        j["storage_type"] = std::string(scalar_type->storage_type().value());
33✔
370
        j["initializer"] = scalar_type->initializer();
33✔
371
        j["alignment"] = scalar_type->alignment();
33✔
372
    } else if (auto array_type = dynamic_cast<const types::Array*>(&type)) {
43✔
373
        j["type"] = "array";
3✔
374
        nlohmann::json element_type_json;
3✔
375
        type_to_json(element_type_json, array_type->element_type());
3✔
376
        j["element_type"] = element_type_json;
3✔
377
        j["num_elements"] = expression(array_type->num_elements());
3✔
378
        j["storage_type"] = std::string(array_type->storage_type().value());
3✔
379
        j["initializer"] = array_type->initializer();
3✔
380
        j["alignment"] = array_type->alignment();
3✔
381
    } else if (auto pointer_type = dynamic_cast<const types::Pointer*>(&type)) {
10✔
382
        j["type"] = "pointer";
3✔
383
        nlohmann::json pointee_type_json;
3✔
384
        type_to_json(pointee_type_json, pointer_type->pointee_type());
3✔
385
        j["pointee_type"] = pointee_type_json;
3✔
386
        j["storage_type"] = std::string(pointer_type->storage_type().value());
3✔
387
        j["initializer"] = pointer_type->initializer();
3✔
388
        j["alignment"] = pointer_type->alignment();
3✔
389
    } else if (auto structure_type = dynamic_cast<const types::Structure*>(&type)) {
7✔
390
        j["type"] = "structure";
2✔
391
        j["name"] = structure_type->name();
2✔
392
        j["storage_type"] = std::string(structure_type->storage_type().value());
2✔
393
        j["initializer"] = structure_type->initializer();
2✔
394
        j["alignment"] = structure_type->alignment();
2✔
395
    } else if (auto function_type = dynamic_cast<const types::Function*>(&type)) {
4✔
396
        j["type"] = "function";
2✔
397
        nlohmann::json return_type_json;
2✔
398
        type_to_json(return_type_json, function_type->return_type());
2✔
399
        j["return_type"] = return_type_json;
2✔
400
        j["params"] = nlohmann::json::array();
2✔
401
        for (size_t i = 0; i < function_type->num_params(); i++) {
5✔
402
            nlohmann::json param_json;
3✔
403
            type_to_json(param_json, function_type->param_type(symbolic::integer(i)));
3✔
404
            j["params"].push_back(param_json);
3✔
405
        }
3✔
406
        j["is_var_arg"] = function_type->is_var_arg();
2✔
407
        j["storage_type"] = std::string(function_type->storage_type().value());
2✔
408
        j["initializer"] = function_type->initializer();
2✔
409
        j["alignment"] = function_type->alignment();
2✔
410
    } else {
2✔
411
        throw std::runtime_error("Unknown type");
×
412
    }
413
}
43✔
414

415
void JSONSerializer::structure_definition_to_json(nlohmann::json& j,
1✔
416
                                                  const types::StructureDefinition& definition) {
417
    j["name"] = definition.name();
1✔
418
    j["members"] = nlohmann::json::array();
1✔
419
    for (size_t i = 0; i < definition.num_members(); i++) {
3✔
420
        nlohmann::json member_json;
2✔
421
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
422
        j["members"].push_back(member_json);
2✔
423
    }
2✔
424
    j["is_packed"] = definition.is_packed();
1✔
425
}
1✔
426

427
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
109✔
428
    j["has"] = debug_info.has();
109✔
429
    j["filename"] = debug_info.filename();
109✔
430
    j["start_line"] = debug_info.start_line();
109✔
431
    j["start_column"] = debug_info.start_column();
109✔
432
    j["end_line"] = debug_info.end_line();
109✔
433
    j["end_column"] = debug_info.end_column();
109✔
434
}
109✔
435

436
/*
437
 * * Deserialization logic
438
 */
439

440
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
3✔
441
    assert(j.contains("name"));
3✔
442
    assert(j["name"].is_string());
3✔
443
    assert(j.contains("type"));
3✔
444
    assert(j["type"].is_string());
3✔
445

446
    FunctionType function_type = function_type_from_string(j["type"].get<std::string>());
3✔
447
    builder::StructuredSDFGBuilder builder(j["name"], function_type);
3✔
448

449
    // deserialize structures
450
    assert(j.contains("structures"));
3✔
451
    assert(j["structures"].is_array());
3✔
452
    for (const auto& structure : j["structures"]) {
3✔
453
        assert(structure.contains("name"));
×
454
        assert(structure["name"].is_string());
×
455
        json_to_structure_definition(structure, builder);
×
456
    }
457

458
    nlohmann::json& containers = j["containers"];
3✔
459

460
    // deserialize externals
461
    for (const auto& name : j["externals"]) {
3✔
462
        auto& type_desc = containers.at(name.get<std::string>());
×
463
        auto type = json_to_type(type_desc);
×
464
        builder.add_container(name, *type, false, true);
×
465
    }
×
466

467
    // deserialize arguments
468
    for (const auto& name : j["arguments"]) {
5✔
469
        auto& type_desc = containers.at(name.get<std::string>());
2✔
470
        auto type = json_to_type(type_desc);
2✔
471
        builder.add_container(name, *type, true, false);
2✔
472
    }
2✔
473

474
    // deserialize transients
475
    for (const auto& entry : containers.items()) {
8✔
476
        if (builder.subject().is_argument(entry.key())) {
5✔
477
            continue;
2✔
478
        }
479
        if (builder.subject().is_external(entry.key())) {
3✔
480
            continue;
×
481
        }
482
        auto type = json_to_type(entry.value());
3✔
483
        builder.add_container(entry.key(), *type, false, false);
3✔
484
    }
3✔
485

486
    // deserialize root node
487
    assert(j.contains("root"));
3✔
488
    auto& root = builder.subject().root();
3✔
489
    json_to_sequence(j["root"], builder, root);
3✔
490

491
    // deserialize metadata
492
    assert(j.contains("metadata"));
3✔
493
    assert(j["metadata"].is_object());
3✔
494
    for (const auto& entry : j["metadata"].items()) {
4✔
495
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
496
    }
497

498
    return builder.move();
3✔
499
}
3✔
500

501
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j,
1✔
502
                                                  builder::StructuredSDFGBuilder& builder) {
503
    assert(j.contains("name"));
1✔
504
    assert(j["name"].is_string());
1✔
505
    assert(j.contains("members"));
1✔
506
    assert(j["members"].is_array());
1✔
507
    assert(j.contains("is_packed"));
1✔
508
    assert(j["is_packed"].is_boolean());
1✔
509
    auto is_packed = j["is_packed"];
1✔
510
    auto& definition = builder.add_structure(j["name"], is_packed);
1✔
511
    for (const auto& member : j["members"]) {
3✔
512
        nlohmann::json member_json;
2✔
513
        auto member_type = json_to_type(member);
2✔
514
        definition.add_member(*member_type);
2✔
515
    }
2✔
516
}
1✔
517

518
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(
4✔
519
    const nlohmann::json& j) {
520
    std::vector<std::pair<std::string, types::Scalar>> arguments;
4✔
521
    for (const auto& argument : j) {
12✔
522
        assert(argument.contains("name"));
8✔
523
        assert(argument["name"].is_string());
8✔
524
        assert(argument.contains("type"));
8✔
525
        assert(argument["type"].is_object());
8✔
526
        std::string name = argument["name"];
8✔
527
        auto type = json_to_type(argument["type"]);
8✔
528
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
8✔
529
    }
8✔
530
    return arguments;
4✔
531
}
4✔
532

533
void JSONSerializer::json_to_dataflow(const nlohmann::json& j,
10✔
534
                                      builder::StructuredSDFGBuilder& builder,
535
                                      structured_control_flow::Block& parent) {
536
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
10✔
537

538
    assert(j.contains("nodes"));
10✔
539
    assert(j["nodes"].is_array());
10✔
540
    for (const auto& node : j["nodes"]) {
26✔
541
        assert(node.contains("type"));
16✔
542
        assert(node["type"].is_string());
16✔
543
        assert(node.contains("element_id"));
16✔
544
        assert(node["element_id"].is_number_integer());
16✔
545
        std::string type = node["type"];
16✔
546
        if (type == "tasklet") {
16✔
547
            assert(node.contains("code"));
4✔
548
            assert(node["code"].is_number_integer());
4✔
549
            assert(node.contains("inputs"));
4✔
550
            assert(node["inputs"].is_array());
4✔
551
            assert(node.contains("output"));
4✔
552
            assert(node["output"].is_object());
4✔
553
            assert(node["output"].contains("name"));
4✔
554
            assert(node["output"].contains("type"));
4✔
555
            auto inputs = json_to_arguments(node["inputs"]);
4✔
556

557
            auto output_name = node["output"]["name"];
4✔
558
            auto output_type = json_to_type(node["output"]["type"]);
4✔
559
            auto& output_type_scalar = dynamic_cast<types::Scalar&>(*output_type);
4✔
560

561
            auto& tasklet =
4✔
562
                builder.add_tasklet(parent, node["code"], {output_name, output_type_scalar}, inputs,
8✔
563
                                    json_to_debug_info(node["debug_info"]));
4✔
564
            tasklet.element_id_ = node["element_id"];
4✔
565
            nodes_map.insert({node["element_id"], tasklet});
4✔
566
        } else if (type == "library_node") {
16✔
567
            assert(node.contains("code"));
×
568
            assert(node.contains("inputs"));
×
569
            assert(node["inputs"].is_array());
×
570
            assert(node.contains("outputs"));
×
571
            assert(node["outputs"].is_array());
×
NEW
572
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
573

574
            auto serializer_fn =
575
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
576
            if (serializer_fn == nullptr) {
×
577
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
578
            }
579
            auto serializer = serializer_fn();
×
580
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
581
            lib_node.element_id_ = node["element_id"];
×
582
            nodes_map.insert({node["element_id"], lib_node});
×
583
        } else if (type == "access_node") {
12✔
584
            assert(node.contains("data"));
12✔
585
            auto& access_node =
12✔
586
                builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
587
            access_node.element_id_ = node["element_id"];
12✔
588
            nodes_map.insert({node["element_id"], access_node});
12✔
589
        } else {
12✔
590
            throw std::runtime_error("Unknown node type");
×
591
        }
592
    }
16✔
593

594
    assert(j.contains("edges"));
10✔
595
    assert(j["edges"].is_array());
10✔
596
    for (const auto& edge : j["edges"]) {
22✔
597
        assert(edge.contains("src"));
12✔
598
        assert(edge["src"].is_number_integer());
12✔
599
        assert(edge.contains("dst"));
12✔
600
        assert(edge["dst"].is_number_integer());
12✔
601
        assert(edge.contains("src_conn"));
12✔
602
        assert(edge["src_conn"].is_string());
12✔
603
        assert(edge.contains("dst_conn"));
12✔
604
        assert(edge["dst_conn"].is_string());
12✔
605
        assert(edge.contains("subset"));
12✔
606
        assert(edge["subset"].is_array());
12✔
607

608
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
609
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
610
        auto& source = nodes_map.at(edge["src"]);
12✔
611
        auto& target = nodes_map.at(edge["dst"]);
12✔
612

613
        assert(edge.contains("subset"));
12✔
614
        assert(edge["subset"].is_array());
12✔
615
        std::vector<symbolic::Expression> subset;
12✔
616
        for (const auto& subset_str : edge["subset"]) {
16✔
617
            assert(subset_str.is_string());
4✔
618
            SymEngine::Expression subset_expr(subset_str);
4✔
619
            subset.push_back(subset_expr);
4✔
620
        }
4✔
621
        auto& memlet =
12✔
622
            builder.add_memlet(parent, source, edge["src_conn"], target, edge["dst_conn"], subset,
24✔
623
                               json_to_debug_info(edge["debug_info"]));
12✔
624
        memlet.element_id_ = edge["element_id"];
12✔
625
    }
12✔
626
}
10✔
627

628
void JSONSerializer::json_to_sequence(const nlohmann::json& j,
12✔
629
                                      builder::StructuredSDFGBuilder& builder,
630
                                      structured_control_flow::Sequence& sequence) {
631
    assert(j.contains("type"));
12✔
632
    assert(j["type"].is_string());
12✔
633
    assert(j.contains("children"));
12✔
634
    assert(j["children"].is_array());
12✔
635
    assert(j.contains("transitions"));
12✔
636
    assert(j["transitions"].is_array());
12✔
637
    assert(j["transitions"].size() == j["children"].size());
12✔
638

639
    sequence.element_id_ = j["element_id"];
12✔
640
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
12✔
641

642
    std::string type = j["type"];
12✔
643
    if (type == "sequence") {
12✔
644
        for (size_t i = 0; i < j["children"].size(); i++) {
22✔
645
            auto& child = j["children"][i];
10✔
646
            auto& transition = j["transitions"][i];
10✔
647
            assert(child.contains("type"));
10✔
648
            assert(child["type"].is_string());
10✔
649

650
            assert(transition.contains("type"));
10✔
651
            assert(transition["type"].is_string());
10✔
652
            assert(transition.contains("assignments"));
10✔
653
            assert(transition["assignments"].is_array());
10✔
654
            control_flow::Assignments assignments;
10✔
655
            for (const auto& assignment : transition["assignments"]) {
11✔
656
                assert(assignment.contains("symbol"));
1✔
657
                assert(assignment["symbol"].is_string());
1✔
658
                assert(assignment.contains("expression"));
1✔
659
                assert(assignment["expression"].is_string());
1✔
660
                SymEngine::Expression expr(assignment["expression"]);
1✔
661
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
662
            }
1✔
663

664
            if (child["type"] == "block") {
10✔
665
                json_to_block_node(child, builder, sequence, assignments);
8✔
666
            } else if (child["type"] == "for") {
10✔
667
                json_to_for_node(child, builder, sequence, assignments);
×
668
            } else if (child["type"] == "if_else") {
2✔
669
                json_to_if_else_node(child, builder, sequence, assignments);
×
670
            } else if (child["type"] == "while") {
2✔
671
                json_to_while_node(child, builder, sequence, assignments);
×
672
            } else if (child["type"] == "break") {
2✔
673
                json_to_break_node(child, builder, sequence, assignments);
1✔
674
            } else if (child["type"] == "continue") {
2✔
675
                json_to_continue_node(child, builder, sequence, assignments);
1✔
676
            } else if (child["type"] == "return") {
1✔
677
                json_to_return_node(child, builder, sequence, assignments);
×
678
            } else if (child["type"] == "map") {
×
679
                json_to_map_node(child, builder, sequence, assignments);
×
680
            } else if (child["type"] == "sequence") {
×
681
                auto& subseq = builder.add_sequence(sequence, assignments,
×
682
                                                    json_to_debug_info(child["debug_info"]));
×
683
                json_to_sequence(child, builder, subseq);
×
684
            } else {
×
685
                throw std::runtime_error("Unknown child type");
×
686
            }
687

688
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
10✔
689
            sequence.at(i).second.element_id_ = transition["element_id"];
10✔
690
        }
10✔
691
    } else {
12✔
692
        throw std::runtime_error("expected sequence type");
×
693
    }
694
}
12✔
695

696
void JSONSerializer::json_to_block_node(const nlohmann::json& j,
9✔
697
                                        builder::StructuredSDFGBuilder& builder,
698
                                        structured_control_flow::Sequence& parent,
699
                                        control_flow::Assignments& assignments) {
700
    assert(j.contains("type"));
9✔
701
    assert(j["type"].is_string());
9✔
702
    assert(j.contains("dataflow"));
9✔
703
    assert(j["dataflow"].is_object());
9✔
704
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
9✔
705
    block.element_id_ = j["element_id"];
9✔
706
    assert(j["dataflow"].contains("type"));
9✔
707
    assert(j["dataflow"]["type"].is_string());
9✔
708
    std::string type = j["dataflow"]["type"];
9✔
709
    if (type == "dataflow") {
9✔
710
        json_to_dataflow(j["dataflow"], builder, block);
9✔
711
    } else {
9✔
712
        throw std::runtime_error("Unknown dataflow type");
×
713
    }
714
}
9✔
715

716
void JSONSerializer::json_to_for_node(const nlohmann::json& j,
1✔
717
                                      builder::StructuredSDFGBuilder& builder,
718
                                      structured_control_flow::Sequence& parent,
719
                                      control_flow::Assignments& assignments) {
720
    assert(j.contains("type"));
1✔
721
    assert(j["type"].is_string());
1✔
722
    assert(j.contains("indvar"));
1✔
723
    assert(j["indvar"].is_string());
1✔
724
    assert(j.contains("init"));
1✔
725
    assert(j["init"].is_string());
1✔
726
    assert(j.contains("condition"));
1✔
727
    assert(j["condition"].is_string());
1✔
728
    assert(j.contains("update"));
1✔
729
    assert(j["update"].is_string());
1✔
730
    assert(j.contains("root"));
1✔
731
    assert(j["root"].is_object());
1✔
732

733
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
734
    SymEngine::Expression init(j["init"]);
1✔
735
    SymEngine::Expression condition_expr(j["condition"]);
1✔
736
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
1✔
737
                .is_null());
738
    symbolic::Condition condition =
739
        SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
740
    SymEngine::Expression update(j["update"]);
1✔
741
    auto& for_node = builder.add_for(parent, indvar, condition, init, update, assignments,
2✔
742
                                     json_to_debug_info(j["debug_info"]));
1✔
743
    for_node.element_id_ = j["element_id"];
1✔
744

745
    assert(j["root"].contains("type"));
1✔
746
    assert(j["root"]["type"].is_string());
1✔
747
    assert(j["root"]["type"] == "sequence");
1✔
748
    json_to_sequence(j["root"], builder, for_node.root());
1✔
749
}
1✔
750

751
void JSONSerializer::json_to_if_else_node(const nlohmann::json& j,
1✔
752
                                          builder::StructuredSDFGBuilder& builder,
753
                                          structured_control_flow::Sequence& parent,
754
                                          control_flow::Assignments& assignments) {
755
    assert(j.contains("type"));
1✔
756
    assert(j["type"].is_string());
1✔
757
    assert(j["type"] == "if_else");
1✔
758
    assert(j.contains("branches"));
1✔
759
    assert(j["branches"].is_array());
1✔
760
    auto& if_else_node =
1✔
761
        builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
762
    if_else_node.element_id_ = j["element_id"];
1✔
763
    for (const auto& branch : j["branches"]) {
3✔
764
        assert(branch.contains("condition"));
2✔
765
        assert(branch["condition"].is_string());
2✔
766
        assert(branch.contains("root"));
2✔
767
        assert(branch["root"].is_object());
2✔
768
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
769
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
2✔
770
                    .is_null());
771
        symbolic::Condition condition =
772
            SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
2✔
773
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
774
        assert(branch["root"].contains("type"));
2✔
775
        assert(branch["root"]["type"].is_string());
2✔
776
        std::string type = branch["root"]["type"];
2✔
777
        if (type == "sequence") {
2✔
778
            json_to_sequence(branch["root"], builder, branch_node);
2✔
779
        } else {
2✔
780
            throw std::runtime_error("Unknown child type");
×
781
        }
782
    }
2✔
783
}
1✔
784

785
void JSONSerializer::json_to_while_node(const nlohmann::json& j,
3✔
786
                                        builder::StructuredSDFGBuilder& builder,
787
                                        structured_control_flow::Sequence& parent,
788
                                        control_flow::Assignments& assignments) {
789
    assert(j.contains("type"));
3✔
790
    assert(j["type"].is_string());
3✔
791
    assert(j["type"] == "while");
3✔
792
    assert(j.contains("root"));
3✔
793
    assert(j["root"].is_object());
3✔
794

795
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
796
    while_node.element_id_ = j["element_id"];
3✔
797

798
    assert(j["root"]["type"] == "sequence");
3✔
799
    json_to_sequence(j["root"], builder, while_node.root());
3✔
800
}
3✔
801

802
void JSONSerializer::json_to_break_node(const nlohmann::json& j,
1✔
803
                                        builder::StructuredSDFGBuilder& builder,
804
                                        structured_control_flow::Sequence& parent,
805
                                        control_flow::Assignments& assignments) {
806
    assert(j.contains("type"));
1✔
807
    assert(j["type"].is_string());
1✔
808
    assert(j["type"] == "break");
1✔
809
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
810
    node.element_id_ = j["element_id"];
1✔
811
}
1✔
812

813
void JSONSerializer::json_to_continue_node(const nlohmann::json& j,
1✔
814
                                           builder::StructuredSDFGBuilder& builder,
815
                                           structured_control_flow::Sequence& parent,
816
                                           control_flow::Assignments& assignments) {
817
    assert(j.contains("type"));
1✔
818
    assert(j["type"].is_string());
1✔
819
    assert(j["type"] == "continue");
1✔
820
    auto& node = builder.add_continue(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
821
    node.element_id_ = j["element_id"];
1✔
822
}
1✔
823

824
void JSONSerializer::json_to_map_node(const nlohmann::json& j,
1✔
825
                                      builder::StructuredSDFGBuilder& builder,
826
                                      structured_control_flow::Sequence& parent,
827
                                      control_flow::Assignments& assignments) {
828
    assert(j.contains("type"));
1✔
829
    assert(j["type"].is_string());
1✔
830
    assert(j["type"] == "map");
1✔
831
    assert(j.contains("indvar"));
1✔
832
    assert(j["indvar"].is_string());
1✔
833
    assert(j.contains("init"));
1✔
834
    assert(j["init"].is_string());
1✔
835
    assert(j.contains("condition"));
1✔
836
    assert(j["condition"].is_string());
1✔
837
    assert(j.contains("update"));
1✔
838
    assert(j["update"].is_string());
1✔
839
    assert(j.contains("root"));
1✔
840
    assert(j["root"].is_object());
1✔
841
    assert(j.contains("schedule_type"));
1✔
842
    assert(j["schedule_type"].is_string());
1✔
843

844
    structured_control_flow::ScheduleType schedule_type =
845
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
846

847
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
848
    SymEngine::Expression init(j["init"]);
1✔
849
    SymEngine::Expression condition_expr(j["condition"]);
1✔
850
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic())
1✔
851
                .is_null());
852
    symbolic::Condition condition =
853
        SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
854
    SymEngine::Expression update(j["update"]);
1✔
855

856
    auto& map_node = builder.add_map(parent, indvar, condition, init, update, schedule_type,
2✔
857
                                     assignments, json_to_debug_info(j["debug_info"]));
1✔
858
    map_node.element_id_ = j["element_id"];
1✔
859

860
    assert(j["root"].contains("type"));
1✔
861
    assert(j["root"]["type"].is_string());
1✔
862
    assert(j["root"]["type"] == "sequence");
1✔
863
    json_to_sequence(j["root"], builder, map_node.root());
1✔
864
}
1✔
865

866
void JSONSerializer::json_to_return_node(const nlohmann::json& j,
1✔
867
                                         builder::StructuredSDFGBuilder& builder,
868
                                         structured_control_flow::Sequence& parent,
869
                                         control_flow::Assignments& assignments) {
870
    assert(j.contains("type"));
1✔
871
    assert(j["type"].is_string());
1✔
872
    assert(j["type"] == "return");
1✔
873

874
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
875
    node.element_id_ = j["element_id"];
1✔
876
}
1✔
877

878
std::unique_ptr<types::IType> JSONSerializer::json_to_type(const nlohmann::json& j) {
31✔
879
    if (j.contains("type")) {
31✔
880
        if (j["type"] == "scalar") {
31✔
881
            // Deserialize scalar type
882
            assert(j.contains("primitive_type"));
25✔
883
            types::PrimitiveType primitive_type = j["primitive_type"];
25✔
884
            assert(j.contains("storage_type"));
25✔
885
            types::StorageType storage_type =
886
                storage_type_from_string(j["storage_type"].get<std::string>());
25✔
887
            assert(j.contains("initializer"));
25✔
888
            std::string initializer = j["initializer"];
25✔
889
            assert(j.contains("alignment"));
25✔
890
            size_t alignment = j["alignment"];
25✔
891
            return std::make_unique<types::Scalar>(storage_type, alignment, initializer,
25✔
892
                                                   primitive_type);
893
        } else if (j["type"] == "array") {
31✔
894
            // Deserialize array type
895
            assert(j.contains("element_type"));
2✔
896
            std::unique_ptr<types::IType> member_type = json_to_type(j["element_type"]);
2✔
897
            assert(j.contains("num_elements"));
2✔
898
            std::string num_elements_str = j["num_elements"];
2✔
899
            // Convert num_elements_str to symbolic::Expression
900
            SymEngine::Expression num_elements(num_elements_str);
2✔
901
            assert(j.contains("storage_type"));
2✔
902
            types::StorageType storage_type =
903
                storage_type_from_string(j["storage_type"].get<std::string>());
2✔
904
            assert(j.contains("initializer"));
2✔
905
            std::string initializer = j["initializer"];
2✔
906
            assert(j.contains("alignment"));
2✔
907
            size_t alignment = j["alignment"];
2✔
908
            return std::make_unique<types::Array>(storage_type, alignment, initializer,
2✔
909
                                                  *member_type, num_elements);
2✔
910
        } else if (j["type"] == "pointer") {
6✔
911
            // Deserialize pointer type
912
            assert(j.contains("pointee_type"));
2✔
913
            std::unique_ptr<types::IType> pointee_type = json_to_type(j["pointee_type"]);
2✔
914
            assert(j.contains("storage_type"));
2✔
915
            types::StorageType storage_type =
916
                storage_type_from_string(j["storage_type"].get<std::string>());
2✔
917
            assert(j.contains("initializer"));
2✔
918
            std::string initializer = j["initializer"];
2✔
919
            assert(j.contains("alignment"));
2✔
920
            size_t alignment = j["alignment"];
2✔
921
            return std::make_unique<types::Pointer>(storage_type, alignment, initializer,
2✔
922
                                                    *pointee_type);
2✔
923
        } else if (j["type"] == "structure") {
4✔
924
            // Deserialize structure type
925
            assert(j.contains("name"));
1✔
926
            std::string name = j["name"];
1✔
927
            assert(j.contains("storage_type"));
1✔
928
            types::StorageType storage_type =
929
                storage_type_from_string(j["storage_type"].get<std::string>());
1✔
930
            assert(j.contains("initializer"));
1✔
931
            std::string initializer = j["initializer"];
1✔
932
            assert(j.contains("alignment"));
1✔
933
            size_t alignment = j["alignment"];
1✔
934
            return std::make_unique<types::Structure>(storage_type, alignment, initializer, name);
1✔
935
        } else if (j["type"] == "function") {
2✔
936
            // Deserialize function type
937
            assert(j.contains("return_type"));
1✔
938
            std::unique_ptr<types::IType> return_type = json_to_type(j["return_type"]);
1✔
939
            assert(j.contains("params"));
1✔
940
            std::vector<std::unique_ptr<types::IType>> params;
1✔
941
            for (const auto& param : j["params"]) {
3✔
942
                params.push_back(json_to_type(param));
2✔
943
            }
944
            assert(j.contains("is_var_arg"));
1✔
945
            bool is_var_arg = j["is_var_arg"];
1✔
946
            assert(j.contains("storage_type"));
1✔
947
            types::StorageType storage_type =
948
                storage_type_from_string(j["storage_type"].get<std::string>());
1✔
949
            assert(j.contains("initializer"));
1✔
950
            std::string initializer = j["initializer"];
1✔
951
            assert(j.contains("alignment"));
1✔
952
            size_t alignment = j["alignment"];
1✔
953
            auto function = std::make_unique<types::Function>(storage_type, alignment, initializer,
1✔
954
                                                              *return_type, is_var_arg);
1✔
955
            for (const auto& param : params) {
3✔
956
                function->add_param(*param);
2✔
957
            }
958
            return function->clone();
1✔
959

960
        } else {
1✔
961
            throw std::runtime_error("Unknown type");
×
962
        }
963
    } else {
964
        throw std::runtime_error("Type not found");
×
965
    }
966
}
31✔
967

968
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
68✔
969
    assert(j.contains("has"));
68✔
970
    assert(j["has"].is_boolean());
68✔
971
    if (!j["has"]) {
68✔
972
        return DebugInfo();
68✔
973
    }
974
    assert(j.contains("filename"));
×
975
    assert(j["filename"].is_string());
×
976
    std::string filename = j["filename"];
×
977
    assert(j.contains("start_line"));
×
978
    assert(j["start_line"].is_number_integer());
×
979
    size_t start_line = j["start_line"];
×
980
    assert(j.contains("start_column"));
×
981
    assert(j["start_column"].is_number_integer());
×
982
    size_t start_column = j["start_column"];
×
983
    assert(j.contains("end_line"));
×
984
    assert(j["end_line"].is_number_integer());
×
985
    size_t end_line = j["end_line"];
×
986
    assert(j.contains("end_column"));
×
987
    assert(j["end_column"].is_number_integer());
×
988
    size_t end_column = j["end_column"];
×
989
    return DebugInfo(filename, start_line, start_column, end_line, end_column);
×
990
}
68✔
991

992
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
33✔
993
    JSONSymbolicPrinter printer;
33✔
994
    return printer.apply(expr);
33✔
995
};
33✔
996

997
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
998
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
999
    str_ = parenthesize(str_);
×
1000
};
×
1001

1002
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
1003
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
1004
    str_ = parenthesize(str_);
×
1005
};
×
1006

1007
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
1008
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
1009
    str_ = parenthesize(str_);
×
1010
};
×
1011

1012
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
1013
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1014
    str_ = parenthesize(str_);
4✔
1015
};
4✔
1016

1017
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1018
    std::ostringstream s;
×
1019
    auto container = x.get_args();
×
1020
    if (container.size() == 1) {
×
1021
        s << apply(*container.begin());
×
1022
    } else {
×
1023
        s << "min(";
×
1024
        s << apply(*container.begin());
×
1025

1026
        // Recursively apply __daisy_min to the arguments
1027
        SymEngine::vec_basic subargs;
×
1028
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1029
            subargs.push_back(*it);
×
1030
        }
×
1031
        auto submin = SymEngine::min(subargs);
×
1032
        s << ", " << apply(submin);
×
1033

1034
        s << ")";
×
1035
    }
×
1036

1037
    str_ = s.str();
×
1038
};
×
1039

1040
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1041
    std::ostringstream s;
×
1042
    auto container = x.get_args();
×
1043
    if (container.size() == 1) {
×
1044
        s << apply(*container.begin());
×
1045
    } else {
×
1046
        s << "max(";
×
1047
        s << apply(*container.begin());
×
1048

1049
        // Recursively apply __daisy_max to the arguments
1050
        SymEngine::vec_basic subargs;
×
1051
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1052
            subargs.push_back(*it);
×
1053
        }
×
1054
        auto submax = SymEngine::max(subargs);
×
1055
        s << ", " << apply(submax);
×
1056

1057
        s << ")";
×
1058
    }
×
1059

1060
    str_ = s.str();
×
1061
};
×
1062

1063
}  // namespace serializer
1064
}  // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc