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

daisytuner / sdfglib / 16069945621

04 Jul 2025 08:56AM UTC coverage: 64.375% (-0.2%) from 64.606%
16069945621

push

github

web-flow
Merge pull request #137 from daisytuner/clang-format

runs clang-format on codebase

609 of 827 new or added lines in 63 files covered. (73.64%)

46 existing lines in 30 files now uncovered.

8578 of 13325 relevant lines covered (64.38%)

177.24 hits per line

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

82.13
/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

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

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

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
            node_json["type"] = "library_node";
×
153
            auto serializer_fn =
NEW
154
                LibraryNodeSerializerRegistry::instance().get_library_node_serializer(lib_node->code().value());
×
155
            if (serializer_fn == nullptr) {
×
NEW
156
                throw std::runtime_error("Unknown library node code: " + std::string(lib_node->code().value()));
×
157
            }
158
            auto serializer = serializer_fn();
×
159
            auto lib_node_json = serializer->serialize(*lib_node);
×
160
            node_json.merge_patch(lib_node_json);
×
161
        } else if (auto code_node = dynamic_cast<const data_flow::AccessNode*>(&node)) {
15✔
162
            node_json["type"] = "access_node";
15✔
163
            node_json["data"] = code_node->data();
15✔
164
        } else {
15✔
165
            throw std::runtime_error("Unknown node type");
×
166
        }
167

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

244
    j["debug_info"] = nlohmann::json::object();
5✔
245
    debug_info_to_json(j["debug_info"], while_node.debug_info());
5✔
246

247
    nlohmann::json body_json;
5✔
248
    sequence_to_json(body_json, while_node.root());
5✔
249
    j["root"] = body_json;
5✔
250
}
5✔
251

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

256
    j["debug_info"] = nlohmann::json::object();
2✔
257
    debug_info_to_json(j["debug_info"], break_node.debug_info());
2✔
258
}
2✔
259

260
void JSONSerializer::continue_node_to_json(nlohmann::json& j, const structured_control_flow::Continue& continue_node) {
2✔
261
    j["type"] = "continue";
2✔
262
    j["element_id"] = continue_node.element_id();
2✔
263

264
    j["debug_info"] = nlohmann::json::object();
2✔
265
    debug_info_to_json(j["debug_info"], continue_node.debug_info());
2✔
266
}
2✔
267

268
void JSONSerializer::map_to_json(nlohmann::json& j, const structured_control_flow::Map& map_node) {
2✔
269
    j["type"] = "map";
2✔
270
    j["element_id"] = map_node.element_id();
2✔
271

272
    j["debug_info"] = nlohmann::json::object();
2✔
273
    debug_info_to_json(j["debug_info"], map_node.debug_info());
2✔
274

275
    j["indvar"] = expression(map_node.indvar());
2✔
276
    j["init"] = expression(map_node.init());
2✔
277
    j["condition"] = expression(map_node.condition());
2✔
278
    j["update"] = expression(map_node.update());
2✔
279

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

282
    nlohmann::json body_json;
2✔
283
    sequence_to_json(body_json, map_node.root());
2✔
284
    j["root"] = body_json;
2✔
285
}
2✔
286

287
void JSONSerializer::return_node_to_json(nlohmann::json& j, const structured_control_flow::Return& return_node) {
2✔
288
    j["type"] = "return";
2✔
289
    j["element_id"] = return_node.element_id();
2✔
290

291
    j["debug_info"] = nlohmann::json::object();
2✔
292
    debug_info_to_json(j["debug_info"], return_node.debug_info());
2✔
293
}
2✔
294

295
void JSONSerializer::sequence_to_json(nlohmann::json& j, const structured_control_flow::Sequence& sequence) {
19✔
296
    j["type"] = "sequence";
19✔
297
    j["element_id"] = sequence.element_id();
19✔
298

299
    j["debug_info"] = nlohmann::json::object();
19✔
300
    debug_info_to_json(j["debug_info"], sequence.debug_info());
19✔
301

302
    j["children"] = nlohmann::json::array();
19✔
303
    j["transitions"] = nlohmann::json::array();
19✔
304

305
    for (size_t i = 0; i < sequence.size(); i++) {
39✔
306
        nlohmann::json child_json;
20✔
307
        auto& child = sequence.at(i).first;
20✔
308
        auto& transition = sequence.at(i).second;
20✔
309

310
        if (auto block = dynamic_cast<const structured_control_flow::Block*>(&child)) {
20✔
311
            block_to_json(child_json, *block);
16✔
312
        } else if (auto for_node = dynamic_cast<const structured_control_flow::For*>(&child)) {
20✔
313
            for_to_json(child_json, *for_node);
×
314
        } else if (auto sequence_node = dynamic_cast<const structured_control_flow::Sequence*>(&child)) {
4✔
UNCOV
315
            sequence_to_json(child_json, *sequence_node);
×
316
        } else if (auto condition_node = dynamic_cast<const structured_control_flow::IfElse*>(&child)) {
4✔
UNCOV
317
            if_else_to_json(child_json, *condition_node);
×
318
        } else if (auto while_node = dynamic_cast<const structured_control_flow::While*>(&child)) {
4✔
319
            while_node_to_json(child_json, *while_node);
×
320
        } else if (auto return_node = dynamic_cast<const structured_control_flow::Return*>(&child)) {
4✔
UNCOV
321
            return_node_to_json(child_json, *return_node);
×
322
        } else if (auto break_node = dynamic_cast<const structured_control_flow::Break*>(&child)) {
4✔
323
            break_node_to_json(child_json, *break_node);
2✔
324
        } else if (auto continue_node = dynamic_cast<const structured_control_flow::Continue*>(&child)) {
4✔
325
            continue_node_to_json(child_json, *continue_node);
2✔
326
        } else if (auto map_node = dynamic_cast<const structured_control_flow::Map*>(&child)) {
2✔
327
            map_to_json(child_json, *map_node);
×
328
        } else {
×
329
            throw std::runtime_error("Unknown child type");
×
330
        }
331

332
        j["children"].push_back(child_json);
20✔
333

334
        // Add transition information
335
        nlohmann::json transition_json;
20✔
336
        transition_json["type"] = "transition";
20✔
337
        transition_json["element_id"] = transition.element_id();
20✔
338

339
        transition_json["debug_info"] = nlohmann::json::object();
20✔
340
        debug_info_to_json(transition_json["debug_info"], transition.debug_info());
20✔
341

342
        transition_json["assignments"] = nlohmann::json::array();
20✔
343
        for (const auto& assignment : transition.assignments()) {
22✔
344
            nlohmann::json assignment_json;
2✔
345
            assignment_json["symbol"] = expression(assignment.first);
2✔
346
            assignment_json["expression"] = expression(assignment.second);
2✔
347
            transition_json["assignments"].push_back(assignment_json);
2✔
348
        }
2✔
349

350
        j["transitions"].push_back(transition_json);
20✔
351
    }
20✔
352
}
19✔
353

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

404
void JSONSerializer::structure_definition_to_json(nlohmann::json& j, const types::StructureDefinition& definition) {
1✔
405
    j["name"] = definition.name();
1✔
406
    j["members"] = nlohmann::json::array();
1✔
407
    for (size_t i = 0; i < definition.num_members(); i++) {
3✔
408
        nlohmann::json member_json;
2✔
409
        type_to_json(member_json, definition.member_type(symbolic::integer(i)));
2✔
410
        j["members"].push_back(member_json);
2✔
411
    }
2✔
412
    j["is_packed"] = definition.is_packed();
1✔
413
}
1✔
414

415
void JSONSerializer::debug_info_to_json(nlohmann::json& j, const DebugInfo& debug_info) {
109✔
416
    j["has"] = debug_info.has();
109✔
417
    j["filename"] = debug_info.filename();
109✔
418
    j["start_line"] = debug_info.start_line();
109✔
419
    j["start_column"] = debug_info.start_column();
109✔
420
    j["end_line"] = debug_info.end_line();
109✔
421
    j["end_column"] = debug_info.end_column();
109✔
422
}
109✔
423

424
/*
425
 * * Deserialization logic
426
 */
427

428
std::unique_ptr<StructuredSDFG> JSONSerializer::deserialize(nlohmann::json& j) {
3✔
429
    assert(j.contains("name"));
3✔
430
    assert(j["name"].is_string());
3✔
431
    assert(j.contains("type"));
3✔
432
    assert(j["type"].is_string());
3✔
433

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

437
    // deserialize structures
438
    assert(j.contains("structures"));
3✔
439
    assert(j["structures"].is_array());
3✔
440
    for (const auto& structure : j["structures"]) {
3✔
441
        assert(structure.contains("name"));
×
442
        assert(structure["name"].is_string());
×
443
        json_to_structure_definition(structure, builder);
×
444
    }
445

446
    nlohmann::json& containers = j["containers"];
3✔
447

448
    // deserialize externals
449
    for (const auto& name : j["externals"]) {
3✔
450
        auto& type_desc = containers.at(name.get<std::string>());
×
451
        auto type = json_to_type(type_desc);
×
452
        builder.add_container(name, *type, false, true);
×
453
    }
×
454

455
    // deserialize arguments
456
    for (const auto& name : j["arguments"]) {
5✔
457
        auto& type_desc = containers.at(name.get<std::string>());
2✔
458
        auto type = json_to_type(type_desc);
2✔
459
        builder.add_container(name, *type, true, false);
2✔
460
    }
2✔
461

462
    // deserialize transients
463
    for (const auto& entry : containers.items()) {
8✔
464
        if (builder.subject().is_argument(entry.key())) {
5✔
465
            continue;
2✔
466
        }
467
        if (builder.subject().is_external(entry.key())) {
3✔
468
            continue;
×
469
        }
470
        auto type = json_to_type(entry.value());
3✔
471
        builder.add_container(entry.key(), *type, false, false);
3✔
472
    }
3✔
473

474
    // deserialize root node
475
    assert(j.contains("root"));
3✔
476
    auto& root = builder.subject().root();
3✔
477
    json_to_sequence(j["root"], builder, root);
3✔
478

479
    // deserialize metadata
480
    assert(j.contains("metadata"));
3✔
481
    assert(j["metadata"].is_object());
3✔
482
    for (const auto& entry : j["metadata"].items()) {
4✔
483
        builder.subject().add_metadata(entry.key(), entry.value());
1✔
484
    }
485

486
    return builder.move();
3✔
487
}
3✔
488

489
void JSONSerializer::json_to_structure_definition(const nlohmann::json& j, builder::StructuredSDFGBuilder& builder) {
1✔
490
    assert(j.contains("name"));
1✔
491
    assert(j["name"].is_string());
1✔
492
    assert(j.contains("members"));
1✔
493
    assert(j["members"].is_array());
1✔
494
    assert(j.contains("is_packed"));
1✔
495
    assert(j["is_packed"].is_boolean());
1✔
496
    auto is_packed = j["is_packed"];
1✔
497
    auto& definition = builder.add_structure(j["name"], is_packed);
1✔
498
    for (const auto& member : j["members"]) {
3✔
499
        nlohmann::json member_json;
2✔
500
        auto member_type = json_to_type(member);
2✔
501
        definition.add_member(*member_type);
2✔
502
    }
2✔
503
}
1✔
504

505
std::vector<std::pair<std::string, types::Scalar>> JSONSerializer::json_to_arguments(const nlohmann::json& j) {
4✔
506
    std::vector<std::pair<std::string, types::Scalar>> arguments;
4✔
507
    for (const auto& argument : j) {
12✔
508
        assert(argument.contains("name"));
8✔
509
        assert(argument["name"].is_string());
8✔
510
        assert(argument.contains("type"));
8✔
511
        assert(argument["type"].is_object());
8✔
512
        std::string name = argument["name"];
8✔
513
        auto type = json_to_type(argument["type"]);
8✔
514
        arguments.emplace_back(name, *dynamic_cast<types::Scalar*>(type.get()));
8✔
515
    }
8✔
516
    return arguments;
4✔
517
}
4✔
518

519
void JSONSerializer::json_to_dataflow(
10✔
520
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
521
) {
522
    std::unordered_map<size_t, data_flow::DataFlowNode&> nodes_map;
10✔
523

524
    assert(j.contains("nodes"));
10✔
525
    assert(j["nodes"].is_array());
10✔
526
    for (const auto& node : j["nodes"]) {
26✔
527
        assert(node.contains("type"));
16✔
528
        assert(node["type"].is_string());
16✔
529
        assert(node.contains("element_id"));
16✔
530
        assert(node["element_id"].is_number_integer());
16✔
531
        std::string type = node["type"];
16✔
532
        if (type == "tasklet") {
16✔
533
            assert(node.contains("code"));
4✔
534
            assert(node["code"].is_number_integer());
4✔
535
            assert(node.contains("inputs"));
4✔
536
            assert(node["inputs"].is_array());
4✔
537
            assert(node.contains("output"));
4✔
538
            assert(node["output"].is_object());
4✔
539
            assert(node["output"].contains("name"));
4✔
540
            assert(node["output"].contains("type"));
4✔
541
            auto inputs = json_to_arguments(node["inputs"]);
4✔
542

543
            auto output_name = node["output"]["name"];
4✔
544
            auto output_type = json_to_type(node["output"]["type"]);
4✔
545
            auto& output_type_scalar = dynamic_cast<types::Scalar&>(*output_type);
4✔
546

547
            auto& tasklet = builder.add_tasklet(
8✔
548
                parent, node["code"], {output_name, output_type_scalar}, inputs, json_to_debug_info(node["debug_info"])
4✔
549
            );
550
            tasklet.element_id_ = node["element_id"];
4✔
551
            nodes_map.insert({node["element_id"], tasklet});
4✔
552
        } else if (type == "library_node") {
16✔
553
            assert(node.contains("code"));
×
554
            assert(node.contains("inputs"));
×
555
            assert(node["inputs"].is_array());
×
556
            assert(node.contains("outputs"));
×
557
            assert(node["outputs"].is_array());
×
558
            data_flow::LibraryNodeCode code(node["code"].get<std::string>());
×
559

NEW
560
            auto serializer_fn = LibraryNodeSerializerRegistry::instance().get_library_node_serializer(code.value());
×
561
            if (serializer_fn == nullptr) {
×
562
                throw std::runtime_error("Unknown library node code: " + std::string(code.value()));
×
563
            }
564
            auto serializer = serializer_fn();
×
565
            auto& lib_node = serializer->deserialize(node, builder, parent);
×
566
            lib_node.element_id_ = node["element_id"];
×
567
            nodes_map.insert({node["element_id"], lib_node});
×
568
        } else if (type == "access_node") {
12✔
569
            assert(node.contains("data"));
12✔
570
            auto& access_node = builder.add_access(parent, node["data"], json_to_debug_info(node["debug_info"]));
12✔
571
            access_node.element_id_ = node["element_id"];
12✔
572
            nodes_map.insert({node["element_id"], access_node});
12✔
573
        } else {
12✔
574
            throw std::runtime_error("Unknown node type");
×
575
        }
576
    }
16✔
577

578
    assert(j.contains("edges"));
10✔
579
    assert(j["edges"].is_array());
10✔
580
    for (const auto& edge : j["edges"]) {
22✔
581
        assert(edge.contains("src"));
12✔
582
        assert(edge["src"].is_number_integer());
12✔
583
        assert(edge.contains("dst"));
12✔
584
        assert(edge["dst"].is_number_integer());
12✔
585
        assert(edge.contains("src_conn"));
12✔
586
        assert(edge["src_conn"].is_string());
12✔
587
        assert(edge.contains("dst_conn"));
12✔
588
        assert(edge["dst_conn"].is_string());
12✔
589
        assert(edge.contains("subset"));
12✔
590
        assert(edge["subset"].is_array());
12✔
591

592
        assert(nodes_map.find(edge["src"]) != nodes_map.end());
12✔
593
        assert(nodes_map.find(edge["dst"]) != nodes_map.end());
12✔
594
        auto& source = nodes_map.at(edge["src"]);
12✔
595
        auto& target = nodes_map.at(edge["dst"]);
12✔
596

597
        assert(edge.contains("subset"));
12✔
598
        assert(edge["subset"].is_array());
12✔
599
        std::vector<symbolic::Expression> subset;
12✔
600
        for (const auto& subset_str : edge["subset"]) {
16✔
601
            assert(subset_str.is_string());
4✔
602
            SymEngine::Expression subset_expr(subset_str);
4✔
603
            subset.push_back(subset_expr);
4✔
604
        }
4✔
605
        auto& memlet = builder.add_memlet(
24✔
606
            parent, source, edge["src_conn"], target, edge["dst_conn"], subset, json_to_debug_info(edge["debug_info"])
12✔
607
        );
608
        memlet.element_id_ = edge["element_id"];
12✔
609
    }
12✔
610
}
10✔
611

612
void JSONSerializer::json_to_sequence(
12✔
613
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& sequence
614
) {
615
    assert(j.contains("type"));
12✔
616
    assert(j["type"].is_string());
12✔
617
    assert(j.contains("children"));
12✔
618
    assert(j["children"].is_array());
12✔
619
    assert(j.contains("transitions"));
12✔
620
    assert(j["transitions"].is_array());
12✔
621
    assert(j["transitions"].size() == j["children"].size());
12✔
622

623
    sequence.element_id_ = j["element_id"];
12✔
624
    sequence.debug_info_ = json_to_debug_info(j["debug_info"]);
12✔
625

626
    std::string type = j["type"];
12✔
627
    if (type == "sequence") {
12✔
628
        for (size_t i = 0; i < j["children"].size(); i++) {
22✔
629
            auto& child = j["children"][i];
10✔
630
            auto& transition = j["transitions"][i];
10✔
631
            assert(child.contains("type"));
10✔
632
            assert(child["type"].is_string());
10✔
633

634
            assert(transition.contains("type"));
10✔
635
            assert(transition["type"].is_string());
10✔
636
            assert(transition.contains("assignments"));
10✔
637
            assert(transition["assignments"].is_array());
10✔
638
            control_flow::Assignments assignments;
10✔
639
            for (const auto& assignment : transition["assignments"]) {
11✔
640
                assert(assignment.contains("symbol"));
1✔
641
                assert(assignment["symbol"].is_string());
1✔
642
                assert(assignment.contains("expression"));
1✔
643
                assert(assignment["expression"].is_string());
1✔
644
                SymEngine::Expression expr(assignment["expression"]);
1✔
645
                assignments.insert({symbolic::symbol(assignment["symbol"]), expr});
1✔
646
            }
1✔
647

648
            if (child["type"] == "block") {
10✔
649
                json_to_block_node(child, builder, sequence, assignments);
8✔
650
            } else if (child["type"] == "for") {
10✔
651
                json_to_for_node(child, builder, sequence, assignments);
×
652
            } else if (child["type"] == "if_else") {
2✔
653
                json_to_if_else_node(child, builder, sequence, assignments);
×
654
            } else if (child["type"] == "while") {
2✔
655
                json_to_while_node(child, builder, sequence, assignments);
×
656
            } else if (child["type"] == "break") {
2✔
657
                json_to_break_node(child, builder, sequence, assignments);
1✔
658
            } else if (child["type"] == "continue") {
2✔
659
                json_to_continue_node(child, builder, sequence, assignments);
1✔
660
            } else if (child["type"] == "return") {
1✔
661
                json_to_return_node(child, builder, sequence, assignments);
×
662
            } else if (child["type"] == "map") {
×
663
                json_to_map_node(child, builder, sequence, assignments);
×
664
            } else if (child["type"] == "sequence") {
×
NEW
665
                auto& subseq = builder.add_sequence(sequence, assignments, json_to_debug_info(child["debug_info"]));
×
666
                json_to_sequence(child, builder, subseq);
×
667
            } else {
×
668
                throw std::runtime_error("Unknown child type");
×
669
            }
670

671
            sequence.at(i).second.debug_info_ = json_to_debug_info(transition["debug_info"]);
10✔
672
            sequence.at(i).second.element_id_ = transition["element_id"];
10✔
673
        }
10✔
674
    } else {
12✔
675
        throw std::runtime_error("expected sequence type");
×
676
    }
677
}
12✔
678

679
void JSONSerializer::json_to_block_node(
9✔
680
    const nlohmann::json& j,
681
    builder::StructuredSDFGBuilder& builder,
682
    structured_control_flow::Sequence& parent,
683
    control_flow::Assignments& assignments
684
) {
685
    assert(j.contains("type"));
9✔
686
    assert(j["type"].is_string());
9✔
687
    assert(j.contains("dataflow"));
9✔
688
    assert(j["dataflow"].is_object());
9✔
689
    auto& block = builder.add_block(parent, assignments, json_to_debug_info(j["debug_info"]));
9✔
690
    block.element_id_ = j["element_id"];
9✔
691
    assert(j["dataflow"].contains("type"));
9✔
692
    assert(j["dataflow"]["type"].is_string());
9✔
693
    std::string type = j["dataflow"]["type"];
9✔
694
    if (type == "dataflow") {
9✔
695
        json_to_dataflow(j["dataflow"], builder, block);
9✔
696
    } else {
9✔
697
        throw std::runtime_error("Unknown dataflow type");
×
698
    }
699
}
9✔
700

701
void JSONSerializer::json_to_for_node(
1✔
702
    const nlohmann::json& j,
703
    builder::StructuredSDFGBuilder& builder,
704
    structured_control_flow::Sequence& parent,
705
    control_flow::Assignments& assignments
706
) {
707
    assert(j.contains("type"));
1✔
708
    assert(j["type"].is_string());
1✔
709
    assert(j.contains("indvar"));
1✔
710
    assert(j["indvar"].is_string());
1✔
711
    assert(j.contains("init"));
1✔
712
    assert(j["init"].is_string());
1✔
713
    assert(j.contains("condition"));
1✔
714
    assert(j["condition"].is_string());
1✔
715
    assert(j.contains("update"));
1✔
716
    assert(j["update"].is_string());
1✔
717
    assert(j.contains("root"));
1✔
718
    assert(j["root"].is_object());
1✔
719

720
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
721
    SymEngine::Expression init(j["init"]);
1✔
722
    SymEngine::Expression condition_expr(j["condition"]);
1✔
723
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
724
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
725
    SymEngine::Expression update(j["update"]);
1✔
726
    auto& for_node =
1✔
727
        builder.add_for(parent, indvar, condition, init, update, assignments, json_to_debug_info(j["debug_info"]));
1✔
728
    for_node.element_id_ = j["element_id"];
1✔
729

730
    assert(j["root"].contains("type"));
1✔
731
    assert(j["root"]["type"].is_string());
1✔
732
    assert(j["root"]["type"] == "sequence");
1✔
733
    json_to_sequence(j["root"], builder, for_node.root());
1✔
734
}
1✔
735

736
void JSONSerializer::json_to_if_else_node(
1✔
737
    const nlohmann::json& j,
738
    builder::StructuredSDFGBuilder& builder,
739
    structured_control_flow::Sequence& parent,
740
    control_flow::Assignments& assignments
741
) {
742
    assert(j.contains("type"));
1✔
743
    assert(j["type"].is_string());
1✔
744
    assert(j["type"] == "if_else");
1✔
745
    assert(j.contains("branches"));
1✔
746
    assert(j["branches"].is_array());
1✔
747
    auto& if_else_node = builder.add_if_else(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
748
    if_else_node.element_id_ = j["element_id"];
1✔
749
    for (const auto& branch : j["branches"]) {
3✔
750
        assert(branch.contains("condition"));
2✔
751
        assert(branch["condition"].is_string());
2✔
752
        assert(branch.contains("root"));
2✔
753
        assert(branch["root"].is_object());
2✔
754
        SymEngine::Expression condition_expr(branch["condition"]);
2✔
755
        assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
2✔
756
        symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()
2✔
757
        );
758
        auto& branch_node = builder.add_case(if_else_node, condition);
2✔
759
        assert(branch["root"].contains("type"));
2✔
760
        assert(branch["root"]["type"].is_string());
2✔
761
        std::string type = branch["root"]["type"];
2✔
762
        if (type == "sequence") {
2✔
763
            json_to_sequence(branch["root"], builder, branch_node);
2✔
764
        } else {
2✔
765
            throw std::runtime_error("Unknown child type");
×
766
        }
767
    }
2✔
768
}
1✔
769

770
void JSONSerializer::json_to_while_node(
3✔
771
    const nlohmann::json& j,
772
    builder::StructuredSDFGBuilder& builder,
773
    structured_control_flow::Sequence& parent,
774
    control_flow::Assignments& assignments
775
) {
776
    assert(j.contains("type"));
3✔
777
    assert(j["type"].is_string());
3✔
778
    assert(j["type"] == "while");
3✔
779
    assert(j.contains("root"));
3✔
780
    assert(j["root"].is_object());
3✔
781

782
    auto& while_node = builder.add_while(parent, assignments, json_to_debug_info(j["debug_info"]));
3✔
783
    while_node.element_id_ = j["element_id"];
3✔
784

785
    assert(j["root"]["type"] == "sequence");
3✔
786
    json_to_sequence(j["root"], builder, while_node.root());
3✔
787
}
3✔
788

789
void JSONSerializer::json_to_break_node(
1✔
790
    const nlohmann::json& j,
791
    builder::StructuredSDFGBuilder& builder,
792
    structured_control_flow::Sequence& parent,
793
    control_flow::Assignments& assignments
794
) {
795
    assert(j.contains("type"));
1✔
796
    assert(j["type"].is_string());
1✔
797
    assert(j["type"] == "break");
1✔
798
    auto& node = builder.add_break(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
799
    node.element_id_ = j["element_id"];
1✔
800
}
1✔
801

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

815
void JSONSerializer::json_to_map_node(
1✔
816
    const nlohmann::json& j,
817
    builder::StructuredSDFGBuilder& builder,
818
    structured_control_flow::Sequence& parent,
819
    control_flow::Assignments& assignments
820
) {
821
    assert(j.contains("type"));
1✔
822
    assert(j["type"].is_string());
1✔
823
    assert(j["type"] == "map");
1✔
824
    assert(j.contains("indvar"));
1✔
825
    assert(j["indvar"].is_string());
1✔
826
    assert(j.contains("init"));
1✔
827
    assert(j["init"].is_string());
1✔
828
    assert(j.contains("condition"));
1✔
829
    assert(j["condition"].is_string());
1✔
830
    assert(j.contains("update"));
1✔
831
    assert(j["update"].is_string());
1✔
832
    assert(j.contains("root"));
1✔
833
    assert(j["root"].is_object());
1✔
834
    assert(j.contains("schedule_type"));
1✔
835
    assert(j["schedule_type"].is_string());
1✔
836

837
    structured_control_flow::ScheduleType schedule_type =
838
        schedule_type_from_string(j["schedule_type"].get<std::string>());
1✔
839

840
    symbolic::Symbol indvar = symbolic::symbol(j["indvar"]);
1✔
841
    SymEngine::Expression init(j["init"]);
1✔
842
    SymEngine::Expression condition_expr(j["condition"]);
1✔
843
    assert(!SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic()).is_null());
1✔
844
    symbolic::Condition condition = SymEngine::rcp_static_cast<const SymEngine::Boolean>(condition_expr.get_basic());
1✔
845
    SymEngine::Expression update(j["update"]);
1✔
846

847
    auto& map_node = builder.add_map(
2✔
848
        parent, indvar, condition, init, update, schedule_type, assignments, json_to_debug_info(j["debug_info"])
1✔
849
    );
850
    map_node.element_id_ = j["element_id"];
1✔
851

852
    assert(j["root"].contains("type"));
1✔
853
    assert(j["root"]["type"].is_string());
1✔
854
    assert(j["root"]["type"] == "sequence");
1✔
855
    json_to_sequence(j["root"], builder, map_node.root());
1✔
856
}
1✔
857

858
void JSONSerializer::json_to_return_node(
1✔
859
    const nlohmann::json& j,
860
    builder::StructuredSDFGBuilder& builder,
861
    structured_control_flow::Sequence& parent,
862
    control_flow::Assignments& assignments
863
) {
864
    assert(j.contains("type"));
1✔
865
    assert(j["type"].is_string());
1✔
866
    assert(j["type"] == "return");
1✔
867

868
    auto& node = builder.add_return(parent, assignments, json_to_debug_info(j["debug_info"]));
1✔
869
    node.element_id_ = j["element_id"];
1✔
870
}
1✔
871

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

946
        } else {
1✔
947
            throw std::runtime_error("Unknown type");
×
948
        }
949
    } else {
950
        throw std::runtime_error("Type not found");
×
951
    }
952
}
31✔
953

954
DebugInfo JSONSerializer::json_to_debug_info(const nlohmann::json& j) {
68✔
955
    assert(j.contains("has"));
68✔
956
    assert(j["has"].is_boolean());
68✔
957
    if (!j["has"]) {
68✔
958
        return DebugInfo();
68✔
959
    }
960
    assert(j.contains("filename"));
×
961
    assert(j["filename"].is_string());
×
962
    std::string filename = j["filename"];
×
963
    assert(j.contains("start_line"));
×
964
    assert(j["start_line"].is_number_integer());
×
965
    size_t start_line = j["start_line"];
×
966
    assert(j.contains("start_column"));
×
967
    assert(j["start_column"].is_number_integer());
×
968
    size_t start_column = j["start_column"];
×
969
    assert(j.contains("end_line"));
×
970
    assert(j["end_line"].is_number_integer());
×
971
    size_t end_line = j["end_line"];
×
972
    assert(j.contains("end_column"));
×
973
    assert(j["end_column"].is_number_integer());
×
974
    size_t end_column = j["end_column"];
×
975
    return DebugInfo(filename, start_line, start_column, end_line, end_column);
×
976
}
68✔
977

978
std::string JSONSerializer::expression(const symbolic::Expression& expr) {
33✔
979
    JSONSymbolicPrinter printer;
33✔
980
    return printer.apply(expr);
33✔
981
};
33✔
982

983
void JSONSymbolicPrinter::bvisit(const SymEngine::Equality& x) {
×
984
    str_ = apply(x.get_args()[0]) + " == " + apply(x.get_args()[1]);
×
985
    str_ = parenthesize(str_);
×
986
};
×
987

988
void JSONSymbolicPrinter::bvisit(const SymEngine::Unequality& x) {
×
989
    str_ = apply(x.get_args()[0]) + " != " + apply(x.get_args()[1]);
×
990
    str_ = parenthesize(str_);
×
991
};
×
992

993
void JSONSymbolicPrinter::bvisit(const SymEngine::LessThan& x) {
×
994
    str_ = apply(x.get_args()[0]) + " <= " + apply(x.get_args()[1]);
×
995
    str_ = parenthesize(str_);
×
996
};
×
997

998
void JSONSymbolicPrinter::bvisit(const SymEngine::StrictLessThan& x) {
4✔
999
    str_ = apply(x.get_args()[0]) + " < " + apply(x.get_args()[1]);
4✔
1000
    str_ = parenthesize(str_);
4✔
1001
};
4✔
1002

1003
void JSONSymbolicPrinter::bvisit(const SymEngine::Min& x) {
×
1004
    std::ostringstream s;
×
1005
    auto container = x.get_args();
×
1006
    if (container.size() == 1) {
×
1007
        s << apply(*container.begin());
×
1008
    } else {
×
1009
        s << "min(";
×
1010
        s << apply(*container.begin());
×
1011

1012
        // Recursively apply __daisy_min to the arguments
1013
        SymEngine::vec_basic subargs;
×
1014
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1015
            subargs.push_back(*it);
×
1016
        }
×
1017
        auto submin = SymEngine::min(subargs);
×
1018
        s << ", " << apply(submin);
×
1019

1020
        s << ")";
×
1021
    }
×
1022

1023
    str_ = s.str();
×
1024
};
×
1025

1026
void JSONSymbolicPrinter::bvisit(const SymEngine::Max& x) {
×
1027
    std::ostringstream s;
×
1028
    auto container = x.get_args();
×
1029
    if (container.size() == 1) {
×
1030
        s << apply(*container.begin());
×
1031
    } else {
×
1032
        s << "max(";
×
1033
        s << apply(*container.begin());
×
1034

1035
        // Recursively apply __daisy_max to the arguments
1036
        SymEngine::vec_basic subargs;
×
1037
        for (auto it = ++(container.begin()); it != container.end(); ++it) {
×
1038
            subargs.push_back(*it);
×
1039
        }
×
1040
        auto submax = SymEngine::max(subargs);
×
1041
        s << ", " << apply(submax);
×
1042

1043
        s << ")";
×
1044
    }
×
1045

1046
    str_ = s.str();
×
1047
};
×
1048

1049
} // namespace serializer
1050
} // 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

© 2025 Coveralls, Inc