Sen API
Sen Libraries
Loading...
Searching...
No Matches
fom_documents.h
Go to the documentation of this file.
1// === fom_documents.h =================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_LIBS_CORE_SRC_LANG_FOM_DOCUMENTS_H
9#define SEN_LIBS_CORE_SRC_LANG_FOM_DOCUMENTS_H
10
11// sen
16#include "sen/core/meta/event.h"
19#include "sen/core/meta/type.h"
21
22// xml parser
23#include "pugixml.hpp"
24
25// std
26#include <array>
27#include <cstddef>
28#include <cstdint>
29#include <filesystem>
30#include <map>
31#include <memory>
32#include <optional>
33#include <string>
34#include <unordered_map>
35#include <utility>
36#include <vector>
37
38namespace sen::lang::fom
39{
40
43{
44 std::filesystem::path path;
45 pugi::xml_document xml;
46 pugi::xml_node objectModel;
47 std::string name;
48 std::string package;
49 bool isExtension {};
50 bool built {};
51 std::vector<Document*> dependencies;
52 std::vector<Document*> mappingImports;
53 std::vector<Document*> extensionImports;
54 std::vector<pugi::xml_node> classNodes;
55 std::vector<pugi::xml_node> interactionNodes;
56
60 std::vector<ConstTypeHandle<>> storage;
61};
62
65struct MappingDocument final
66{
67 std::filesystem::path path;
68 pugi::xml_document xml;
69};
70
73{
74 std::filesystem::path path;
75 std::unique_ptr<TypeSet> typeSet;
76};
77
80{
81 pugi::xml_node node;
83};
84
89{
90 std::vector<Declaration> all;
92 bool ownerIsComplete {false};
93};
94
99{
100 SEN_NOCOPY_NOMOVE(FomDocuments)
101
102public:
103 FomDocuments(const std::vector<std::filesystem::path>& paths,
104 const std::vector<std::filesystem::path>& extensions,
105 const std::vector<std::filesystem::path>& mappings,
106 TypeSettings settings);
107 ~FomDocuments() = default;
108
109public:
112 [[nodiscard]] std::vector<DocumentTypeSet> takeTypeSets();
113 [[nodiscard]] const TypeSet& rootTypeSet() const& noexcept;
114
117 [[nodiscard]] std::unique_ptr<TypeSet> takeRootTypeSet() &&;
118
119private:
120 //--------------------------------------------------------------------------------------------------------------
121 // Reading
122 //--------------------------------------------------------------------------------------------------------------
123
127 void readDirectory(const std::filesystem::path& path);
128
131 void readExtension(const std::filesystem::path& path);
132
135 Document& readDocument(const std::filesystem::path& path, const std::string& package, bool isExtension);
136
139 void resolveDependencies();
140
143 [[nodiscard]] Document* findDocument(const std::string& identification) const;
144
145 //--------------------------------------------------------------------------------------------------------------
146 // Indexing
147 //--------------------------------------------------------------------------------------------------------------
148
150 void indexDeclarations();
151
153 void record(const std::string& key, pugi::xml_node node, Document& document);
154
158 [[nodiscard]] std::optional<Declaration> ownedElsewhere(const std::string& key, const Document& document) const;
159
161 [[nodiscard]] std::vector<Declaration> contributions(const std::string& key,
162 pugi::xml_node own,
163 const Document& document) const;
164
167 [[nodiscard]] std::optional<Declaration> introducedType(const std::string& category,
168 const std::string& fomName) const;
169
172 [[nodiscard]] std::optional<Declaration> externallyOwnedType(const std::string& category,
173 const std::string& fomName,
174 const Document& document) const;
175
177 void buildRootTypeSet();
178
179 //--------------------------------------------------------------------------------------------------------------
180 // Building
181 //--------------------------------------------------------------------------------------------------------------
182
185 void ensureBuilt(Document& document);
186
188 void validateImportGraph() const;
189
191 void populateTypes(Document& document);
192
194 template <typename F>
195 [[nodiscard]] ConstTypeHandle<> processTypeSource(Document& document, const pugi::xpath_node& node, F func);
196
197 template <typename F>
198 void processTypeSource(Document& document, const pugi::xpath_node_set& nodes, F func);
199
201 template <typename F>
202 [[nodiscard]] std::optional<ConstTypeHandle<>> processTypeQuery(Document& document,
203 const std::string& query,
204 const std::string& queryPostFix,
205 F func);
206
208 [[nodiscard]] ConstTypeHandle<> storeType(ConstTypeHandle<> type, Document& document);
209
212 [[nodiscard]] std::pair<ConstTypeHandle<>, Document*> getOrCreateType(const std::string& fomName, Document& document);
213
216 [[nodiscard]] std::pair<ConstTypeHandle<>, Document*> getOrCreateExtensionType(const std::string& fomName,
217 Document& document);
218
221 void addExtensionImport(Document& document, Document& imported, const std::string& fomName);
222
224 [[nodiscard]] bool importsTransitively(const Document& from,
225 const Document& target,
226 std::vector<const Document*>& visited) const;
227
229 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchType(const std::string& fomName,
230 Document& document);
231 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchTypeHere(const std::string& fomName,
232 Document& document);
233 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchBySenName(
234 const std::string& qualifiedName) const;
235
238 struct Category
239 {
240 const char* container;
241 const char* element;
242 ConstTypeHandle<> (FomDocuments::*build)(pugi::xml_node, Document&);
243 };
244
247 [[nodiscard]] static const std::array<Category, 5>& dataTypeCategories();
248
250 [[nodiscard]] static std::string categoryQuery(const Category& category);
251
252 [[nodiscard]] ConstTypeHandle<> simpleData(pugi::xml_node node, Document& document);
253 [[nodiscard]] ConstTypeHandle<> arrayType(pugi::xml_node node, Document& document);
254 [[nodiscard]] ConstTypeHandle<> recordType(pugi::xml_node node, Document& document);
255 [[nodiscard]] ConstTypeHandle<> variantRecord(pugi::xml_node node, Document& document);
256 [[nodiscard]] ConstTypeHandle<> enumeration(pugi::xml_node node, Document& document);
257 [[nodiscard]] ConstTypeHandle<> classNode(pugi::xml_node node, Document& document);
258
261 void applyMappings(ClassSpec& classSpec,
262 std::vector<PropertySpec>& propertySpecs,
263 const MaybeConstTypeHandle<>& parentType,
264 Document& document);
265
269 [[nodiscard]] PropertySpec propertyFromAttribute(pugi::xml_node attrNode,
270 Document& document,
271 const ClassAnnotations* annotations,
272 bool fromExtension = false);
273
275 void basicDataRepresentation(pugi::xml_node node, const Document& document);
276
278 [[nodiscard]] ConstTypeHandle<> optionalPropertyType(const std::string& fomElementTypeName,
279 Document& document,
280 bool fromExtension = false);
281
282 //--------------------------------------------------------------------------------------------------------------
283 // Interactions
284 //--------------------------------------------------------------------------------------------------------------
285
286 enum class CallableEnum : uint8_t
287 {
288 method,
289 event
290 };
291
292 struct FoundInteraction
293 {
294 pugi::xml_node node;
295 Document* document {};
296 };
297
298 [[nodiscard]] FoundInteraction findInteraction(const std::string& path, Document& document);
299 [[nodiscard]] FoundInteraction findInteractionAnywhere(const std::string& path, const std::string& askedBy);
300 [[nodiscard]] Arg argFromParameter(pugi::xml_node paramNode, Document& document, bool fromExtension = false);
301 [[nodiscard]] std::vector<Arg> collectArgs(pugi::xml_node interactionNode,
302 const std::vector<std::string>& ignoredParams,
303 Document& document);
304 [[nodiscard]] ConstTypeHandle<> structFromInteraction(pugi::xml_node node,
305 Document& document,
306 const std::vector<std::string>& ignoredParams);
307 [[nodiscard]] CallableSpec makeCallableSpec(pugi::xml_node interactionNode,
308 const std::vector<std::string>& ignoredParams,
309 Document& document,
310 CallableEnum callableEnum,
311 bool packArguments);
312 [[nodiscard]] EventSpec makeEventSpec(pugi::xml_node interactionNode,
313 const std::vector<std::string>& ignoredParams,
314 Document& document,
315 bool packArguments);
316 [[nodiscard]] MethodSpec makeMethodSpec(pugi::xml_node interactionNode,
317 pugi::xml_node returnNode,
318 const std::vector<std::string>& ignoredParams,
319 Document& document,
320 bool packArguments,
321 bool localOnly,
322 const std::string& qualifiedClassName);
323
324 //--------------------------------------------------------------------------------------------------------------
325 // Names
326 //--------------------------------------------------------------------------------------------------------------
327
328 [[nodiscard]] std::string senName(const std::string& fomTypeName) const;
329 [[nodiscard]] std::string senQualifiedName(const Document& document, const std::string& fomTypeName) const;
330 [[nodiscard]] std::string withPackage(const Document& document, const std::string& str) const;
331 [[nodiscard]] ConstTypeHandle<> representationToType(const std::string& representation) const;
332
333private:
334 TypeSettings settings_;
335 std::vector<std::unique_ptr<Document>> documents_;
336 std::vector<MappingDocument> mappings_;
337
341 std::map<std::string, std::vector<std::string>> argsStructIgnores_;
342
347 std::vector<std::string> packedWithIgnores_;
348
350 std::vector<std::filesystem::path> extensionPaths_;
351
353 std::map<std::string, Declarations> declarations_;
354
355 std::unordered_map<std::string, ConstTypeHandle<>> representations_;
356 std::unordered_map<std::string, std::string> units_;
357 std::unique_ptr<TypeSet> rootSet_;
358 std::optional<TypeHandle<ClassType>> rootClass_;
359
362 std::vector<std::pair<std::string, const Document*>> resolving_;
363
366 size_t extensionResolutionDepth_ {};
367};
368
369} // namespace sen::lang::fom
370
371#endif // SEN_LIBS_CORE_SRC_LANG_FOM_DOCUMENTS_H
A registry of custom types.
Definition type_registry.h:36
std::unique_ptr< TypeSet > takeRootTypeSet() &&
Hands over the root set, which leaves this reader without one. Rvalue-qualified so a second call is a...
const TypeSet & rootTypeSet() const &noexcept
FomDocuments(const std::vector< std::filesystem::path > &paths, const std::vector< std::filesystem::path > &extensions, const std::vector< std::filesystem::path > &mappings, TypeSettings settings)
std::vector< DocumentTypeSet > takeTypeSets()
One set per document, in the order the documents were read, which follows the directories given on th...
Definition stl_resolver.h:97
A set of types coming from a STL file.
Definition stl_resolver.h:34
std::optional< ConstTypeHandle< T > > MaybeConstTypeHandle
Definition type.h:322
TypeHandle< const T > ConstTypeHandle
Definition type.h:319
Definition fom_common.h:28
std::vector< ConstTypeHandle<> > storage
Definition fom_documents.h:60
std::filesystem::path path
Definition fom_documents.h:74
CustomTypeRegistry registry
The types this document owns. They become its type set, so this decides which generated file each typ...
Definition fom_documents.h:59
std::vector< Document * > extensionImports
documents a member contributed by an extension pulled in
Definition fom_documents.h:53
bool built
guards ensureBuilt's recursion
Definition fom_documents.h:50
pugi::xml_node objectModel
Definition fom_documents.h:46
std::vector< Document * > dependencies
Definition fom_documents.h:51
std::unique_ptr< TypeSet > typeSet
Definition fom_documents.h:75
std::vector< Declaration > all
in the order the documents were read
Definition fom_documents.h:90
std::string package
the directory name in lower case
Definition fom_documents.h:48
std::string name
modelIdentification/name, used to resolve dependency references
Definition fom_documents.h:47
bool ownerIsComplete
the owner's declaration carries semantics
Definition fom_documents.h:92
Document * document
Definition fom_documents.h:82
pugi::xml_document xml
Definition fom_documents.h:68
std::vector< Document * > mappingImports
documents a mapping pulled in
Definition fom_documents.h:52
std::filesystem::path path
Definition fom_documents.h:44
std::vector< pugi::xml_node > classNodes
Definition fom_documents.h:54
std::vector< pugi::xml_node > interactionNodes
Definition fom_documents.h:55
std::filesystem::path path
Definition fom_documents.h:67
pugi::xml_document xml
Definition fom_documents.h:45
bool isExtension
from the extensions input: adds members, never owns a type
Definition fom_documents.h:49
pugi::xml_node node
Definition fom_documents.h:81
Document * owner
null when only extensions declare the type
Definition fom_documents.h:91
One declaring node and the document it came from, so a refusal can name the file.
Definition fom_documents.h:80
Everything that declares one type. The complete definition owns the type and decides its package; eve...
Definition fom_documents.h:89
One document that was read, and the nodes in it that declare something.
Definition fom_documents.h:43
A type set and the path it was built from. One caller resolves a single FOM file by path.
Definition fom_documents.h:73
A mapping file and where it was read from, so a refusal can name the file that asked for something th...
Definition fom_documents.h:66
STL namespace.
Data of a property.
Definition property.h:40
Definition stl_resolver.h:103