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 <cstdint>
28#include <filesystem>
29#include <map>
30#include <memory>
31#include <optional>
32#include <string>
33#include <unordered_map>
34#include <utility>
35#include <vector>
36
37namespace sen::lang::fom
38{
39
42{
43 std::filesystem::path path;
44 pugi::xml_document xml;
45 pugi::xml_node objectModel;
46 std::string name;
47 std::string package;
48 bool isExtension {};
49 bool built {};
50 std::vector<Document*> dependencies;
51 std::vector<Document*> mappingImports;
52 std::vector<pugi::xml_node> classNodes;
53 std::vector<pugi::xml_node> interactionNodes;
54
58 std::vector<ConstTypeHandle<>> storage;
59};
60
63struct MappingDocument final
64{
65 std::filesystem::path path;
66 pugi::xml_document xml;
67};
68
71{
72 std::filesystem::path path;
73 std::unique_ptr<TypeSet> typeSet;
74};
75
78{
79 pugi::xml_node node;
81};
82
87{
88 std::vector<Declaration> all;
90 bool ownerIsComplete {false};
91};
92
97{
98 SEN_NOCOPY_NOMOVE(FomDocuments)
99
100public:
101 FomDocuments(const std::vector<std::filesystem::path>& paths,
102 const std::vector<std::filesystem::path>& extensions,
103 const std::vector<std::filesystem::path>& mappings,
104 TypeSettings settings);
105 ~FomDocuments() = default;
106
107public:
110 [[nodiscard]] std::vector<DocumentTypeSet> takeTypeSets();
111 [[nodiscard]] const TypeSet& rootTypeSet() const& noexcept;
112
115 [[nodiscard]] std::unique_ptr<TypeSet> takeRootTypeSet() &&;
116
117private:
118 //--------------------------------------------------------------------------------------------------------------
119 // Reading
120 //--------------------------------------------------------------------------------------------------------------
121
125 void readDirectory(const std::filesystem::path& path);
126
129 void readExtension(const std::filesystem::path& path);
130
133 Document& readDocument(const std::filesystem::path& path, const std::string& package, bool isExtension);
134
137 void resolveDependencies();
138
141 [[nodiscard]] Document* findDocument(const std::string& identification) const;
142
143 //--------------------------------------------------------------------------------------------------------------
144 // Indexing
145 //--------------------------------------------------------------------------------------------------------------
146
148 void indexDeclarations();
149
151 void record(const std::string& key, pugi::xml_node node, Document& document);
152
156 [[nodiscard]] std::optional<Declaration> ownedElsewhere(const std::string& key, const Document& document) const;
157
159 [[nodiscard]] std::vector<Declaration> contributions(const std::string& key,
160 pugi::xml_node own,
161 const Document& document) const;
162
165 [[nodiscard]] std::optional<Declaration> introducedType(const std::string& category,
166 const std::string& fomName) const;
167
169 void buildRootTypeSet();
170
171 //--------------------------------------------------------------------------------------------------------------
172 // Building
173 //--------------------------------------------------------------------------------------------------------------
174
177 void ensureBuilt(Document& document);
178
180 void populateTypes(Document& document);
181
183 template <typename F>
184 [[nodiscard]] ConstTypeHandle<> processTypeSource(Document& document, const pugi::xpath_node& node, F func);
185
186 template <typename F>
187 void processTypeSource(Document& document, const pugi::xpath_node_set& nodes, F func);
188
190 template <typename F>
191 [[nodiscard]] std::optional<ConstTypeHandle<>> processTypeQuery(Document& document,
192 const std::string& query,
193 const std::string& queryPostFix,
194 F func);
195
197 [[nodiscard]] ConstTypeHandle<> storeType(ConstTypeHandle<> type, Document& document);
198
201 [[nodiscard]] std::pair<ConstTypeHandle<>, Document*> getOrCreateType(const std::string& fomName, Document& document);
202
204 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchType(const std::string& fomName,
205 Document& document);
206 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchTypeHere(const std::string& fomName,
207 Document& document);
208 [[nodiscard]] std::optional<std::pair<ConstTypeHandle<>, Document*>> searchBySenName(
209 const std::string& qualifiedName) const;
210
213 struct Category
214 {
215 const char* container;
216 const char* element;
217 ConstTypeHandle<> (FomDocuments::*build)(pugi::xml_node, Document&);
218 };
219
222 [[nodiscard]] static const std::array<Category, 5>& dataTypeCategories();
223
225 [[nodiscard]] static std::string categoryQuery(const Category& category);
226
227 [[nodiscard]] ConstTypeHandle<> simpleData(pugi::xml_node node, Document& document);
228 [[nodiscard]] ConstTypeHandle<> arrayType(pugi::xml_node node, Document& document);
229 [[nodiscard]] ConstTypeHandle<> recordType(pugi::xml_node node, Document& document);
230 [[nodiscard]] ConstTypeHandle<> variantRecord(pugi::xml_node node, Document& document);
231 [[nodiscard]] ConstTypeHandle<> enumeration(pugi::xml_node node, Document& document);
232 [[nodiscard]] ConstTypeHandle<> classNode(pugi::xml_node node, Document& document);
233
236 void applyMappings(ClassSpec& classSpec,
237 std::vector<PropertySpec>& propertySpecs,
238 const MaybeConstTypeHandle<>& parentType,
239 Document& document);
240
244 [[nodiscard]] PropertySpec propertyFromAttribute(pugi::xml_node attrNode,
245 Document& document,
246 const ClassAnnotations* annotations);
247
249 void basicDataRepresentation(pugi::xml_node node, const Document& document);
250
252 [[nodiscard]] ConstTypeHandle<> optionalPropertyType(const std::string& fomElementTypeName, Document& document);
253
254 //--------------------------------------------------------------------------------------------------------------
255 // Interactions
256 //--------------------------------------------------------------------------------------------------------------
257
258 enum class CallableEnum : uint8_t
259 {
260 method,
261 event
262 };
263
264 struct FoundInteraction
265 {
266 pugi::xml_node node;
267 Document* document {};
268 };
269
270 [[nodiscard]] FoundInteraction findInteraction(const std::string& path, Document& document);
271 [[nodiscard]] FoundInteraction findInteractionAnywhere(const std::string& path, const std::string& askedBy);
272 [[nodiscard]] Arg argFromParameter(pugi::xml_node paramNode, Document& document);
273 [[nodiscard]] std::vector<Arg> collectArgs(pugi::xml_node interactionNode,
274 const std::vector<std::string>& ignoredParams,
275 Document& document);
276 [[nodiscard]] ConstTypeHandle<> structFromInteraction(pugi::xml_node node,
277 Document& document,
278 const std::vector<std::string>& ignoredParams);
279 [[nodiscard]] CallableSpec makeCallableSpec(pugi::xml_node interactionNode,
280 const std::vector<std::string>& ignoredParams,
281 Document& document,
282 CallableEnum callableEnum,
283 bool packArguments);
284 [[nodiscard]] EventSpec makeEventSpec(pugi::xml_node interactionNode,
285 const std::vector<std::string>& ignoredParams,
286 Document& document,
287 bool packArguments);
288 [[nodiscard]] MethodSpec makeMethodSpec(pugi::xml_node interactionNode,
289 pugi::xml_node returnNode,
290 const std::vector<std::string>& ignoredParams,
291 Document& document,
292 bool packArguments,
293 bool localOnly,
294 const std::string& qualifiedClassName);
295
296 //--------------------------------------------------------------------------------------------------------------
297 // Names
298 //--------------------------------------------------------------------------------------------------------------
299
300 [[nodiscard]] std::string senName(const std::string& fomTypeName) const;
301 [[nodiscard]] std::string senQualifiedName(const Document& document, const std::string& fomTypeName) const;
302 [[nodiscard]] std::string withPackage(const Document& document, const std::string& str) const;
303 [[nodiscard]] ConstTypeHandle<> representationToType(const std::string& representation) const;
304
305private:
306 TypeSettings settings_;
307 std::vector<std::unique_ptr<Document>> documents_;
308 std::vector<MappingDocument> mappings_;
309
313 std::map<std::string, std::vector<std::string>> argsStructIgnores_;
314
319 std::vector<std::string> packedWithIgnores_;
320
322 std::vector<std::filesystem::path> extensionPaths_;
323
325 std::map<std::string, Declarations> declarations_;
326
327 std::unordered_map<std::string, ConstTypeHandle<>> representations_;
328 std::unordered_map<std::string, std::string> units_;
329 std::unique_ptr<TypeSet> rootSet_;
330 std::optional<TypeHandle<ClassType>> rootClass_;
331
334 std::vector<std::pair<std::string, const Document*>> resolving_;
335};
336
337} // namespace sen::lang::fom
338
339#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:58
std::filesystem::path path
Definition fom_documents.h:72
CustomTypeRegistry registry
The types this document owns. They become its type set, so this decides which generated file each typ...
Definition fom_documents.h:57
bool built
guards ensureBuilt's recursion
Definition fom_documents.h:49
pugi::xml_node objectModel
Definition fom_documents.h:45
std::vector< Document * > dependencies
Definition fom_documents.h:50
std::unique_ptr< TypeSet > typeSet
Definition fom_documents.h:73
std::vector< Declaration > all
in the order the documents were read
Definition fom_documents.h:88
std::string package
the directory name in lower case
Definition fom_documents.h:47
std::string name
modelIdentification/name, used to resolve dependency references
Definition fom_documents.h:46
bool ownerIsComplete
the owner's declaration carries semantics
Definition fom_documents.h:90
Document * document
Definition fom_documents.h:80
pugi::xml_document xml
Definition fom_documents.h:66
std::vector< Document * > mappingImports
documents a mapping pulled in
Definition fom_documents.h:51
std::filesystem::path path
Definition fom_documents.h:43
std::vector< pugi::xml_node > classNodes
Definition fom_documents.h:52
std::vector< pugi::xml_node > interactionNodes
Definition fom_documents.h:53
std::filesystem::path path
Definition fom_documents.h:65
pugi::xml_document xml
Definition fom_documents.h:44
bool isExtension
from the extensions input: adds members, never owns a type
Definition fom_documents.h:48
pugi::xml_node node
Definition fom_documents.h:79
Document * owner
null when only extensions declare the type
Definition fom_documents.h:89
One declaring node and the document it came from, so a refusal can name the file.
Definition fom_documents.h:78
Everything that declares one type. The complete definition owns the type and decides its package; eve...
Definition fom_documents.h:87
One document that was read, and the nodes in it that declare something.
Definition fom_documents.h:42
A type set and the path it was built from. One caller resolves a single FOM file by path.
Definition fom_documents.h:71
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:64
STL namespace.
Data of a property.
Definition property.h:40
Definition stl_resolver.h:103