SILICON
Loading...
Searching...
No Matches
document_conversion.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
20#include <algorithm>
21#include <array>
22#include <memory>
23#include <ranges>
24#include <stdexcept>
25#include <utility>
26
27#include <nlohmann/json.hpp>
28
29#include <core/circuit.hpp>
34
35namespace SILICON::ui {
36namespace {
37
38 using SILICON::core::Circuit;
39 using SILICON::project::Document;
41
42 [[nodiscard]] Document
43 materializeDocument(SILICON::conversion::SemanticDocument&& document)
44 {
45 if (auto* source = std::get_if<SILICON::conversion::VerilogSource>(&document.payload))
46 return {std::move(document.path), std::move(source->contents)};
47 if (auto* source = std::get_if<SILICON::conversion::BinarySource>(&document.payload))
48 return {std::move(document.path), std::move(source->contents)};
49
50 auto circuit =
51 std::make_shared<Circuit>(std::get<Circuit>(std::move(document.payload)));
52 const auto slug = SILICON::project::documentSlugForPath(document.path);
53 if (!slug)
54 throw std::invalid_argument("Converted circuit has an invalid document path");
55 DiagramScene scene;
56 scene.setSubcircuitDocumentMode(true);
57 scene.loadCircuit(std::move(circuit), GUIComponentFactory::instance(), false);
58
59 auto sceneJson = scene.serialize();
60 auto completed = nlohmann::ordered_json::parse(sceneJson);
61 completed["graphicalComponent"] = graphicalSubcircuitMetadataToJson(
63 return {std::move(document.path), completed.dump(2)};
64 }
65
66#ifdef __EMSCRIPTEN__
67 constexpr bool YOSYS_AVAILABLE = false;
68 constexpr std::string_view YOSYS_UNAVAILABLE_REASON =
69 "Conversion requires Yosys and is unavailable in the web build";
70#else
71 constexpr bool YOSYS_AVAILABLE = true;
72 constexpr std::string_view YOSYS_UNAVAILABLE_REASON{};
73#endif
74
75 constexpr std::array CONVERTERS{
77 .source = DocumentType::Circuit,
78 .target = DocumentType::Verilog,
79 .available = YOSYS_AVAILABLE,
80 .unavailableReason = YOSYS_UNAVAILABLE_REASON,
81 },
83 .source = DocumentType::Verilog,
84 .target = DocumentType::Circuit,
85 .available = YOSYS_AVAILABLE,
86 .unavailableReason = YOSYS_UNAVAILABLE_REASON,
87 },
88 };
89
90} // namespace
91
93 const DocumentType target)
94{
95 const auto found =
96 std::ranges::find_if(CONVERTERS, [source, target](const auto& converter) {
97 return converter.source == source && converter.target == target;
98 });
99 return found == CONVERTERS.end() ? nullptr : std::to_address(found);
100}
101
102std::vector<const DocumentConverter*> documentConvertersFor(const DocumentType source)
103{
104 std::vector<const DocumentConverter*> result;
105 for (const auto& converter : CONVERTERS) {
106 if (converter.source == source)
107 result.push_back(&converter);
108 }
109 return result;
110}
111
112PreparedDocumentConversion
114 const std::span<const Document> projectDocuments,
115 const SILICON::core::ComponentRegistry& registry,
116 const SILICON::core::CircuitResolver& resolver)
117{
118 const auto* converter = documentConverterFor(source.getType(), target);
119 if (!converter)
120 throw std::invalid_argument("The selected document has no registered conversion");
121 if (!converter->available)
122 throw std::runtime_error(std::string(converter->unavailableReason));
123
125 source, target, projectDocuments, registry, resolver);
126 return {
127 .choices = std::move(prepared.choices),
128 .execute =
129 [execute =
130 std::move(prepared.execute)](const std::span<const std::string> selected) {
131 auto semantic = execute(selected);
132 std::vector<Document> documents;
133 documents.reserve(semantic.documents.size());
134 for (auto& document : semantic.documents)
135 documents.push_back(materializeDocument(std::move(document)));
136 return ConversionResult{.documents = std::move(documents),
137 .activatePath = std::move(semantic.activatePath)};
138 },
139 };
140}
141
142} // namespace SILICON::ui
Graphics scene for the circuit diagram editor.
void setSubcircuitDocumentMode(bool enabled)
Enables reusable-circuit boundary I/O editing for the scene.
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
PreparedSemanticConversion prepareDocumentConversion(const Document &source, const DocumentType target, const std::span< const Document > projectDocuments, const SILICON::core::ComponentRegistry &registry, const SILICON::core::CircuitResolver &resolver)
std::optional< std::string > documentSlugForPath(const std::string_view path)
nlohmann::ordered_json graphicalSubcircuitMetadataToJson(const GraphicalSubcircuitMetadata &metadata)
Serializes graphical metadata to the JSON shape/port object.
Definition metadata.cpp:324
std::vector< const DocumentConverter * > documentConvertersFor(const DocumentType source)
PreparedDocumentConversion prepareDocumentConversion(const Document &source, const DocumentType target, const std::span< const Document > projectDocuments, const SILICON::core::ComponentRegistry &registry, const SILICON::core::CircuitResolver &resolver)
GraphicalSubcircuitMetadata synchronizeGraphicalSubcircuitMetadata(const nlohmann::json &document, const GraphicalSubcircuitMetadata &fallbackMetadata, const CircuitResolver *resolver)
Reconciles saved graphical metadata with the current circuit document.
Definition metadata.cpp:276
const DocumentConverter * documentConverterFor(const DocumentType source, const DocumentType target)
Visual metadata used to render a subcircuit as a reusable component.
Definition metadata.hpp:74