GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 2 / 197
Functions: 0.0% 0 / 2 / 53
Branches: 0.0% 0 / 2 / 142

src/modelica/ModelicaFileImpl.hpp
Line Branch Exec Source
1 #ifndef MODELICA_MODELICAFILE_IMPL_H
2 #define MODELICA_MODELICAFILE_IMPL_H
3
4 #include "antlr/modelicaLexer.h"
5 #include "../utilities/core/Filesystem.hpp"
6 #include "../utilities/core/Logger.hpp"
7 #include "antlr/modelicaParser.h"
8 #include <algorithm>
9 #include <cassert>
10 #include <memory>
11 #include <string>
12 #include <string_view>
13 #include <unordered_map>
14 #include <unordered_set>
15 #include <utility>
16 #include <vector>
17
18 namespace openstudio::modelica::detail {
19
20 class ClassDefinitionImpl;
21 class ComponentClauseImpl;
22 class ConnectClauseImpl;
23 class ModelicaSyntaxWrapper;
24 //template <typename T>
25 template <typename T, class Derived>
26 class ModelicaSyntax;
27
28 class ModelicaFileImpl : public std::enable_shared_from_this<ModelicaFileImpl>
29 {
30 template <typename T, class Derived>
31 friend class ModelicaSyntax;
32
33 public:
34 ModelicaFileImpl(const openstudio::path& path);
35
36 void parse(std::string_view text);
37
38 std::string getText() const;
39 std::string printTree() const;
40
41 std::vector<ClassDefinitionImpl*> getClassDefinitions();
42 ClassDefinitionImpl* getClassDefinitionByName(const std::string& name);
43 ClassDefinitionImpl* addClassDefinition(const std::string& text);
44
45 protected:
46 void updateCTX();
47
48 modelicaParser* parser() const;
49
50 template <typename T, class Derived>
51 ✗ void addModelicaSyntax(ModelicaSyntax<T, Derived>* syntax) {
52 ✗ m_syntaxStore.emplace_back(std::make_unique<ModelicaSyntaxWrapper>(syntax));
53 ✗ }
54
55 template <typename T, class Derived>
56 ✗ void removeModelicaSyntax(ModelicaSyntax<T, Derived>* syntax) {
57 ✗ std::erase_if(m_syntaxStore, [syntax](const auto& wrapper) { return *wrapper == syntax; });
58 ✗ }
59
60 private:
61 std::unique_ptr<antlr4::ANTLRInputStream> m_inputStream{nullptr};
62 std::unique_ptr<modelicaLexer> m_modelicaLexer{nullptr};
63 std::unique_ptr<antlr4::CommonTokenStream> m_tokenStream{nullptr};
64 std::unique_ptr<modelicaParser> m_modelicaParser{nullptr};
65 std::vector<std::unique_ptr<ModelicaSyntaxWrapper>> m_syntaxStore;
66 // cppcheck-suppress unusedStructMember
67 std::unordered_map<std::string, std::unique_ptr<ClassDefinitionImpl>> m_classDefinitions;
68 // cppcheck-suppress unusedStructMember
69 std::vector<ClassDefinitionImpl*> m_cachedClassDefinitions;
70 bool m_classDefinitionsDirty = true;
71
72 void ensureClassDefinitionCache();
73 void rebuildClassDefinitionCache();
74
75 − REGISTER_LOGGER("openstudio.modelica.ModelicaFileImpl");
76 };
77
78 template <typename T, class Derived>
79 class ModelicaSyntax
80 {
81 friend class ModelicaFileImpl;
82
83 public:
84 ✗ ModelicaSyntax(std::shared_ptr<ModelicaFileImpl> modelicaFile, T* ctx) // NOLINT
85 ✗ : m_modelicaFile(std::move(modelicaFile)), m_ctx(ctx), m_id(Derived::idFromCTX(ctx)) {
86 ✗ if (auto file = m_modelicaFile.lock()) {
87 ✗ file->addModelicaSyntax(this);
88 }
89 ✗ }
90 ModelicaSyntax(const ModelicaSyntax&) = delete; // NOLINT
91 ModelicaSyntax(ModelicaSyntax&&) = default; // NOLINT
92 ModelicaSyntax& operator=(const ModelicaSyntax&) = delete;
93 ModelicaSyntax& operator=(ModelicaSyntax&&) = delete;
94 ✗ virtual ~ModelicaSyntax() {
95 ✗ if (auto file = m_modelicaFile.lock()) {
96 ✗ file->removeModelicaSyntax(this);
97 }
98 ✗ }
99
100 ✗ std::string getText() const {
101 ✗ if (auto file = m_modelicaFile.lock()) {
102 ✗ auto* parser = file->parser();
103 ✗ if (parser) {
104 ✗ auto* tokens = dynamic_cast<antlr4::CommonTokenStream*>(parser->getTokenStream());
105 ✗ if (tokens && ctx()->getStart() && ctx()->getStop()) {
106 ✗ return tokens->getText(ctx()->getStart(), ctx()->getStop());
107 }
108 }
109 }
110 ✗ return ctx()->getText(); // fallback
111 }
112
113 ✗ void updateCTX() {
114 ✗ auto file = m_modelicaFile.lock();
115 ✗ if (!file) {
116 ✗ return;
117 }
118 ✗ auto* parser = file->parser();
119 ✗ if (!parser) {
120 ✗ return;
121 }
122 ✗ std::string currentID = id();
123 ✗ setCTX(nullptr);
124
125 // Find the same class definition in the newly parsed tree
126 ✗ auto allContexts = antlr4::tree::xpath::XPath::findAll(parser->stored_definition(), std::string(Derived::xpath), parser);
127
128 ✗ for (auto* ctx : allContexts) {
129 ✗ auto* typed_ctx = dynamic_cast<T*>(ctx);
130 ✗ if (!typed_ctx) {
131 ✗ continue;
132 }
133 ✗ const auto id = Derived::idFromCTX(typed_ctx);
134 ✗ if (id == currentID) {
135 assert(typed_ctx != nullptr);
136 ✗ setCTX(typed_ctx);
137 ✗ break;
138 }
139 }
140 ✗ if (!m_ctx) {
141 ✗ m_active = false;
142 }
143 ✗ }
144
145 ✗ std::string id() const {
146 ✗ return m_id;
147 }
148
149 ✗ bool isActive() const {
150 ✗ return m_active;
151 }
152
153 protected:
154 ✗ T* ctx() const {
155 ✗ return m_ctx;
156 }
157
158 ✗ void setCTX(T* newCtx) {
159 ✗ m_ctx = newCtx;
160 ✗ m_active = (m_ctx != nullptr);
161 ✗ }
162
163 ModelicaFileImpl* modelicaFile() {
164 return m_modelicaFile.lock().get();
165 }
166
167 ✗ modelicaParser* parser() {
168 ✗ auto file = m_modelicaFile.lock();
169 ✗ return file ? file->parser() : nullptr;
170 ✗ }
171
172 ✗ std::shared_ptr<ModelicaFileImpl> modelicaFileShared() const {
173 ✗ return m_modelicaFile.lock();
174 }
175
176 private:
177 std::weak_ptr<ModelicaFileImpl> m_modelicaFile;
178 T* m_ctx;
179 std::string m_id;
180 bool m_active = true;
181 };
182
183 // Non-owning type-erased wrapper for ModelicaSyntax templates
184 class ModelicaSyntaxWrapper
185 {
186 public:
187 // Constructor template that accepts any ModelicaSyntax<T>*
188 template <typename T, class Derived>
189 ✗ ModelicaSyntaxWrapper(ModelicaSyntax<T, Derived>* syntax) : m_impl(std::make_unique<Impl<T, Derived>>(syntax)) {}
190
191 // Forward common operations through type erasure
192 ✗ void updateCTX() {
193 ✗ m_impl->updateCTX();
194 ✗ }
195 //std::string id() const { return m_impl->id(); }
196 //ModelicaFile* modelicaFile() { return m_impl->modelicaFile(); }
197
198 // Comparison operators for removal from store
199 template <typename T, class Derived>
200 ✗ bool operator==(const ModelicaSyntax<T, Derived>* syntax) const {
201 ✗ return m_impl && m_impl->equals(syntax);
202 }
203
204 template <typename T, class Derived>
205 bool operator!=(const ModelicaSyntax<T, Derived>* syntax) const {
206 return !(*this == syntax);
207 }
208
209 private:
210 // Abstract base for type erasure
211 struct ImplBase
212 {
213 ✗ ImplBase() = default;
214 ImplBase(const ImplBase&) = default;
215 ImplBase(ImplBase&&) = default;
216 ImplBase& operator=(const ImplBase&) = default;
217 ImplBase& operator=(ImplBase&&) = default;
218 ✗ virtual ~ImplBase() = default;
219 virtual void updateCTX() = 0;
220 virtual bool equals(const void* ptr) const = 0;
221 };
222
223 // Concrete implementation that holds a non-owning pointer
224 template <typename T, class Derived>
225 struct Impl : ImplBase
226 {
227 ModelicaSyntax<T, Derived>* syntax; // Non-owning pointer
228
229 ✗ explicit Impl(ModelicaSyntax<T, Derived>* s) : syntax(s) {}
230
231 ✗ void updateCTX() override {
232 ✗ if (syntax) {
233 ✗ syntax->updateCTX();
234 }
235 ✗ }
236 //std::string id() const override { return syntax->id(); }
237 //ModelicaFile* modelicaFile() override { return syntax->modelicaFile(); }
238 ✗ bool equals(const void* ptr) const override {
239 ✗ return syntax == ptr;
240 }
241 };
242
243 std::unique_ptr<ImplBase> m_impl;
244 };
245
246 class ClassDefinitionImpl : public ModelicaSyntax<modelicaParser::Class_definitionContext, ClassDefinitionImpl>
247 {
248 public:
249 friend class ConnectClauseImpl;
250 ✗ ClassDefinitionImpl(std::shared_ptr<ModelicaFileImpl> modelicaFile, modelicaParser::Class_definitionContext* ctx)
251 ✗ : ModelicaSyntax<modelicaParser::Class_definitionContext, ClassDefinitionImpl>(std::move(modelicaFile), ctx) {}
252 // cppcheck-suppress unusedStructMember
253 static constexpr std::string_view xpath = "//class_definition";
254 std::string longClassSpecifier() const;
255
256 std::vector<ComponentClauseImpl*> componentClauses();
257 void addComponentClause(const std::string& text);
258 void markComponentClausesDirty();
259 void setClassContext(modelicaParser::Class_definitionContext* ctx);
260 std::vector<ConnectClauseImpl*> getConnectClauses();
261 ConnectClauseImpl* addConnectClause(const std::string& source, const std::string& target);
262 bool removeConnectClause(const std::string& source, const std::string& target);
263
264 static std::string idFromCTX(modelicaParser::Class_definitionContext* ctx);
265
266 private:
267 void ensureComponentClauseCache();
268 void rebuildComponentClauseCache();
269 void deactivateComponentClauses();
270 void ensureConnectionCache();
271 void rebuildConnectionCache();
272 void deactivateConnections();
273 void markConnectionsDirty();
274 void removeConnectClause(const ConnectClauseImpl* connection);
275 modelicaParser::Equation_sectionContext* selectEquationSection() const;
276 static std::pair<std::string, std::string> validateAndNormalizeConnectClause(const std::string& source, const std::string& target);
277
278 // cppcheck-suppress unusedStructMember
279 std::unordered_map<std::string, std::unique_ptr<ComponentClauseImpl>> m_componentClauses;
280 // cppcheck-suppress unusedStructMember
281 std::vector<ComponentClauseImpl*> m_cachedComponentClauses;
282 bool m_componentClausesDirty = true;
283 // cppcheck-suppress unusedStructMember
284 std::unordered_map<std::string, std::unique_ptr<ConnectClauseImpl>> m_connections;
285 // cppcheck-suppress unusedStructMember
286 std::vector<ConnectClauseImpl*> m_cachedConnections;
287 bool m_connectionsDirty = true;
288
289 − REGISTER_LOGGER("openstudio.modelica.ClassDefinitionImpl");
290 };
291
292 class ComponentClauseImpl : public ModelicaSyntax<modelicaParser::Component_clauseContext, ComponentClauseImpl>
293 {
294 public:
295 ✗ ComponentClauseImpl(std::shared_ptr<ModelicaFileImpl> modelicaFile, modelicaParser::Component_clauseContext* ctx)
296 ✗ : ModelicaSyntax<modelicaParser::Component_clauseContext, ComponentClauseImpl>(std::move(modelicaFile), ctx) {}
297 // cppcheck-suppress unusedStructMember
298 static constexpr std::string_view xpath = "//component_clause";
299
300 //std::vector<ComponentDeclaration> componentDeclarations();
301 ✗ void setComponentContext(modelicaParser::Component_clauseContext* ctx) {
302 ✗ setCTX(ctx);
303 ✗ }
304
305 static std::string idFromCTX(modelicaParser::Component_clauseContext* ctx);
306 };
307
308 class ConnectClauseImpl : public ModelicaSyntax<modelicaParser::Connect_clauseContext, ConnectClauseImpl>
309 {
310 public:
311 friend class ClassDefinitionImpl;
312 ✗ ConnectClauseImpl(std::shared_ptr<ModelicaFileImpl> modelicaFile, ClassDefinitionImpl* owner, modelicaParser::Connect_clauseContext* ctx)
313 ✗ : ModelicaSyntax<modelicaParser::Connect_clauseContext, ConnectClauseImpl>(std::move(modelicaFile), ctx), m_owner(owner) {}
314 // cppcheck-suppress unusedStructMember
315 static constexpr std::string_view xpath = "//connect_clause";
316
317 std::string source() const;
318 std::string target() const;
319 void remove();
320 ✗ void setConnectClauseContext(modelicaParser::Connect_clauseContext* ctx) {
321 ✗ setCTX(ctx);
322 ✗ }
323 static std::string idFromCTX(modelicaParser::Connect_clauseContext* ctx);
324 static std::string connectionIdFromEndpoints(const std::string& source, const std::string& target);
325
326 private:
327 modelicaParser::EquationContext* equationContext() const;
328
329 ClassDefinitionImpl* m_owner;
330 };
331
332 } // namespace openstudio::modelica::detail
333
334 #endif // MODELICA_MODELICAFILE_IMPL_H
335