1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Save and load `Model` from/to Neo4j base.
17 # Nit models are composed by MEntities.
18 # This module creates NeoNode for each MEntity found in a `Model` and save them
19 # into Neo4j database.
23 # NeoNodes can also be translated back to MEntities to rebuild a Nit `Model`.
25 # Structure of the nit `Model` in base:
27 # Note : Any null or empty attribute will not be saved in the database.
29 # For any `MEntity` (in addition to specific data):
31 # * labels: model name (`model_name`) and `MEntity`.
32 # * `name`: short (unqualified) name.
33 # * `mdoc`: JSON array representing the associated Markdown documentation
36 # Note : All nodes described here are MEntities.
40 # * labels: `MProject`, `model_name` and `MEntity`.
41 # * `(:MProject)-[:ROOT]->(:MGroup)`: root of the group tree.
45 # * labels: `MGroup`, `model_name` and `MEntity`.
46 # * `full_name`: fully qualified name.
47 # * `(:MGroup)-[:PROJECT]->(:MProject)`: associated project.
48 # * `(:MGroup)-[:PARENT]->(:MGroup)`: parent group. Does not exist for the root
50 # * `(:MGroup)-[:DECLARES]->(:MModule)`: modules that are direct children of
52 # * `(:MGroup)-[:NESTS]->(:MGroup)`: nested groups that are direct children of
57 # * labels: `MModule`, `model_name` and `MEntity`.
58 # * `full_name`: fully qualified name.
59 # * `location`: origin of the definition. SEE: `Location.to_s`
60 # * `(:MModule)-[:IMPORTS]->(:MModule)`: modules that are imported directly.
61 # * `(:MModule)-[:INTRODUCES]->(:MClass)`: all by classes introduced by this
63 # * `(:MModule)-[:DEFINES]->(:MClassDef)`: all class definitons contained in
68 # * labels: `MClass`, `model_name` and `MEntity`.
69 # * `full_name`: fully qualified name.
70 # * `arity`: number of generic formal parameters. 0 if the class is not generic.
71 # * `kind`: kind of the class (`interface`, `abstract class`, etc.)
72 # * `visibility`: visibility of the class.
73 # * `(:MClass)-[:CLASSTYPE]->(:MClassType)`: SEE: `MClass.mclass_type`
75 # Arguments in the `CLASSTYPE` are named following the `parameter_names`
76 # attribute of the `MClassDef` that introduces the class. A class definition
77 # introduces a class if and only if it has this class as `MCLASS` and
78 # has `is_intro` set to `true`.
82 # * labels: `MClassDef`, `model_name` and `MEntity`.
83 # * `is_intro`: Does this definition introduce the class?
84 # * `location`: origin of the definition. SEE: `Location.to_s`
85 # * `parameter_names`: JSON array listing the name of each formal generic
86 # parameter (in order of declaration).
87 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
89 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
90 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
92 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
93 # classdef (introductions and redefinitions).
94 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
98 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
99 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
101 # * `full_name`: fully qualified name.
102 # * `visibility`: visibility of the property.
103 # * `is_init`: Indicates if the property is a constructor. Exists only if the
104 # node is a `MMethod`.
105 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
110 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
111 # `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
112 # represented entity.
113 # * `is_intro`: Does this definition introduce the property?
114 # * `location`: origin of the definition. SEE: `Location.to_s`.
115 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
117 # Additional attributes and relationship for `MMethodDef`:
119 # * `is_abstract`: Is the method definition abstract?
120 # * `is_intern`: Is the method definition intern?
121 # * `is_extern`: Is the method definition extern?
122 # * `(:MMethodDef)-[:SIGNATURE]->(:MSignature)`: signature attached to the
123 # property definition.
125 # Additional relationship for `MVirtualTypeDef`:
127 # * `(:MVirtualTypeDef)-[:BOUND]->(:MType)`: type to which the virtual type
128 # is bound in this definition. Exists only if this definition bound the virtual
129 # type to an effective type.
133 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
134 # `MNullableType`, `MVirtualType` or `MSignature`, depending on the class of
135 # the represented entity.
137 # Additional label and relationships for `MClassType`:
139 # * If it is a `MGenericType`, also has the `MGenericType` label.
140 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
141 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
143 # Arguments are named following the `parameter_names` attribute of the
144 # `MClassDef` that introduces the class referred by `CLASS`.
146 # Additional relationship for `MVirtualType`:
148 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
149 # determines the type (usually a `MVirtualTypeProp`).
151 # Additional attribute and relationship for `MParameterType`:
153 # * `rank`: position of the parameter (0 for the first parameter).
154 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
157 # Additional relationship for `MNullableType`:
159 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
161 # Additional attribute and relationships for `MSignature`:
163 # * `parameter_names`: JSON array representing the list of the parameter names.
164 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
165 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
168 # In order to maintain the correct parameters order, each `MSignature` node
169 # contains an array of parameter names corresponding to the parameter order in
172 # For example, if the source code contains:
174 # fun foo(a: A, b: B, c: C)
176 # The `MSignature` node will contain a property
177 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
178 # with the parameters in the correct order.
182 # * labels: `MParameter`, `model_name` and `MEntity`.
183 # * `is_vararg`: Is the parameter a vararg?
184 # * `rank`: position of the parameter (0 for the first parameter).
185 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
187 # MParameters are also ranked by their position in the corresponding signature.
188 # Rank 0 for the first parameter, 1 for the next one and etc.
195 # Helper class that can save and load a `Model` into a Neo4j database.
200 # Because we use only one Neo4j instance to store all the models,
201 # we need to mark their appartenance to a particular model and avoid loading all models.
203 # The name is used as a Neo label on each created nodes and used to load nodes from base.
204 var model_name
: String
206 # The toolcontext used to init the `NeoModel` tool.
207 var toolcontext
: ToolContext
209 # The Neo4j `client` used to communicate with the Neo4j instance.
210 var client
: Neo4jClient
212 # Fill `model` using base pointed by `client`.
213 fun load
(model
: Model): Model do
214 toolcontext
.info
("Locate all mentities...", 1)
215 var nodes
= client
.nodes_with_label
(model_name
)
217 toolcontext
.info
("Preload nodes...", 1)
218 pull_all_nodes
(nodes
)
219 toolcontext
.info
("Preload edges...", 1)
220 pull_all_edges
(nodes
)
222 toolcontext
.info
("Build model...", 1)
223 nodes
= client
.nodes_with_labels
([model_name
, "MProject"])
224 for node
in nodes
do to_mproject
(model
, node
)
225 nodes
= client
.nodes_with_labels
([model_name
, "MGroup"])
226 for node
in nodes
do to_mgroup
(model
, node
)
227 nodes
= client
.nodes_with_labels
([model_name
, "MModule"])
228 for node
in nodes
do to_mmodule
(model
, node
)
229 nodes
= client
.nodes_with_labels
([model_name
, "MClass"])
230 for node
in nodes
do to_mclass
(model
, node
)
231 nodes
= client
.nodes_with_labels
([model_name
, "MClassDef"])
232 for node
in nodes
do to_mclassdef
(model
, node
)
233 nodes
= client
.nodes_with_labels
([model_name
, "MProperty"])
234 for node
in nodes
do to_mproperty
(model
, node
)
235 nodes
= client
.nodes_with_labels
([model_name
, "MPropDef"])
236 for node
in nodes
do to_mpropdef
(model
, node
)
240 # Save `model` in the base pointed by `client`.
241 fun save
(model
: Model) do
242 var nodes
= collect_model_nodes
(model
)
243 toolcontext
.info
("Save {nodes.length} nodes...", 1)
245 var edges
= collect_model_edges
(model
)
246 toolcontext
.info
("Save {edges.length} edges...", 1)
250 # Save `neo_entities` in base using batch mode.
251 private fun push_all
(neo_entities
: Collection[NeoEntity]) do
252 var batch
= new NeoBatch(client
)
253 var len
= neo_entities
.length
256 for nentity
in neo_entities
do
257 batch
.save_entity
(nentity
)
258 if i
== batch_max_size
then
260 sum
+= batch_max_size
261 toolcontext
.info
(" {sum * 100 / len}% done", 1)
262 batch
= new NeoBatch(client
)
271 # Load content for all `nodes` from base.
273 # Content corresponds to properties and labels that are loaded in batch mode.
274 private fun pull_all_nodes
(nodes
: Collection[NeoNode]) do
275 var batch
= new NeoBatch(client
)
276 var len
= nodes
.length
280 batch
.load_node
(node
)
281 if i
== batch_max_size
then
283 sum
+= batch_max_size
284 toolcontext
.info
(" {sum * 100 / len}% done", 1)
285 batch
= new NeoBatch(client
)
294 # Load all edges from base linked to `nodes`.
296 # Edges are loaded in batch mode.
297 private fun pull_all_edges
(nodes
: Collection[NeoNode]) do
298 var batch
= new NeoBatch(client
)
299 var len
= nodes
.length
303 batch
.load_node_edges
(node
)
304 if i
== batch_max_size
then
306 sum
+= batch_max_size
307 toolcontext
.info
(" {sum * 100 / len}% done", 1)
308 batch
= new NeoBatch(client
)
317 # How many operation can be executed in one batch?
318 private var batch_max_size
= 1000
320 # Execute `batch` and check for errors.
322 # Abort if `batch.execute` returns errors.
323 private fun do_batch
(batch
: NeoBatch) do
324 var errors
= batch
.execute
325 if not errors
.is_empty
then
331 # Collect all nodes from the current `model`.
332 private fun collect_model_nodes
(model
: Model): Collection[NeoNode] do
333 for mproject
in model
.mprojects
do
335 for mgroup
in mproject
.mgroups
do to_node
(mgroup
)
340 # Collect all edges from the current `model`.
342 # Actually collect all out_edges from all nodes.
343 private fun collect_model_edges
(model
: Model): Collection[NeoEdge] do
344 var edges
= new HashSet[NeoEdge]
345 for node
in nodes
.values
do edges
.add_all
(node
.out_edges
)
349 # Mentities associated to nodes.
350 private var mentities
= new HashMap[NeoNode, MEntity]
352 # Nodes associated with MEntities.
353 private var nodes
= new HashMap[MEntity, NeoNode]
355 # Get the `NeoNode` associated with `mentity`.
356 # `mentities` are stored locally to avoid duplication.
357 fun to_node
(mentity
: MEntity): NeoNode do
358 if nodes
.has_key
(mentity
) then return nodes
[mentity
]
359 if mentity
isa MProject then return mproject_node
(mentity
)
360 if mentity
isa MGroup then return mgroup_node
(mentity
)
361 if mentity
isa MModule then return mmodule_node
(mentity
)
362 if mentity
isa MClass then return mclass_node
(mentity
)
363 if mentity
isa MClassDef then return mclassdef_node
(mentity
)
364 if mentity
isa MProperty then return mproperty_node
(mentity
)
365 if mentity
isa MPropDef then return mpropdef_node
(mentity
)
366 if mentity
isa MType then return mtype_node
(mentity
)
367 if mentity
isa MParameter then return mparameter_node
(mentity
)
371 # Make a new `NeoNode` based on `mentity`.
372 private fun make_node
(mentity
: MEntity): NeoNode do
373 var node
= new NeoNode
374 nodes
[mentity
] = node
375 node
.labels
.add
"MEntity"
376 node
.labels
.add model_name
377 node
["name"] = mentity
.name
378 if mentity
.mdoc
!= null then node
["mdoc"] = new JsonArray.from
(mentity
.mdoc
.content
)
382 # Build a `NeoNode` representing `mproject`.
383 private fun mproject_node
(mproject
: MProject): NeoNode do
384 var node
= make_node
(mproject
)
385 node
.labels
.add
"MProject"
386 var root
= mproject
.root
388 node
.out_edges
.add
(new NeoEdge(node
, "ROOT", to_node
(root
)))
393 # Build a new `MProject` from a `node`.
395 # REQUIRE `node.labels.has("MProject")`
396 private fun to_mproject
(model
: Model, node
: NeoNode): MProject do
397 if mentities
.has_key
(node
) then return mentities
[node
].as(MProject)
398 assert node
.labels
.has
("MProject")
399 var mproject
= new MProject(node
["name"].to_s
, model
)
400 mentities
[node
] = mproject
401 set_doc
(node
, mproject
)
402 mproject
.root
= to_mgroup
(model
, node
.out_nodes
("ROOT").first
)
406 # Build a `NeoNode` representing `mgroup`.
407 private fun mgroup_node
(mgroup
: MGroup): NeoNode do
408 var node
= make_node
(mgroup
)
409 node
.labels
.add
"MGroup"
410 node
["full_name"] = mgroup
.full_name
411 var parent
= mgroup
.parent
412 node
.out_edges
.add
(new NeoEdge(node
, "PROJECT", to_node
(mgroup
.mproject
)))
413 if parent
!= null then
414 node
.out_edges
.add
(new NeoEdge(node
, "PARENT", to_node
(parent
)))
416 for mmodule
in mgroup
.mmodules
do
417 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mmodule
)))
419 for subgroup
in mgroup
.in_nesting
.direct_smallers
do
420 node
.in_edges
.add
(new NeoEdge(node
, "NESTS", to_node
(subgroup
)))
425 # Build a new `MGroup` from a `node`.
427 # REQUIRE `node.labels.has("MGroup")`
428 private fun to_mgroup
(model
: Model, node
: NeoNode): MGroup do
429 if mentities
.has_key
(node
) then return mentities
[node
].as(MGroup)
430 assert node
.labels
.has
("MGroup")
431 var mproject
= to_mproject
(model
, node
.out_nodes
("PROJECT").first
)
432 var parent
: nullable MGroup = null
433 var out
= node
.out_nodes
("PARENT")
434 if not out
.is_empty
then
435 parent
= to_mgroup
(model
, out
.first
)
437 var mgroup
= new MGroup(node
["name"].to_s
, mproject
, parent
)
438 mentities
[node
] = mgroup
439 set_doc
(node
, mgroup
)
443 # Build a `NeoNode` representing `mmodule`.
444 private fun mmodule_node
(mmodule
: MModule): NeoNode do
445 var node
= make_node
(mmodule
)
446 node
.labels
.add
"MModule"
447 node
["full_name"] = mmodule
.full_name
448 node
["location"] = mmodule
.location
.to_s
449 for parent
in mmodule
.in_importation
.direct_greaters
do
450 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
452 for mclass
in mmodule
.intro_mclasses
do
453 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
455 for mclassdef
in mmodule
.mclassdefs
do
456 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
461 # Build a new `MModule` from a `node`.
463 # REQUIRE `node.labels.has("MModule")`
464 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
465 if mentities
.has_key
(node
) then return mentities
[node
].as(MModule)
466 assert node
.labels
.has
("MModule")
467 var ins
= node
.in_nodes
("DECLARES")
468 var mgroup
: nullable MGroup = null
469 if not ins
.is_empty
then
470 mgroup
= to_mgroup
(model
, ins
.first
)
472 var name
= node
["name"].to_s
473 var location
= to_location
(node
["location"].to_s
)
474 var mmodule
= new MModule(model
, mgroup
, name
, location
)
475 mentities
[node
] = mmodule
476 set_doc
(node
, mmodule
)
477 var imported_mmodules
= new Array[MModule]
478 for smod
in node
.out_nodes
("IMPORTS") do
479 imported_mmodules
.add to_mmodule
(model
, smod
)
481 mmodule
.set_imported_mmodules
(imported_mmodules
)
485 # Build a `NeoNode` representing `mclass`.
486 private fun mclass_node
(mclass
: MClass): NeoNode do
487 var node
= make_node
(mclass
)
488 node
.labels
.add
"MClass"
489 node
["full_name"] = mclass
.full_name
490 node
["kind"] = mclass
.kind
.to_s
491 node
["visibility"] = mclass
.visibility
.to_s
492 if not mclass
.mparameters
.is_empty
then
493 var parameter_names
= new Array[String]
494 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
495 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
497 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
501 # Build a new `MClass` from a `node`.
503 # REQUIRE `node.labels.has("MClass")`
504 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
505 if mentities
.has_key
(node
) then return mentities
[node
].as(MClass)
506 assert node
.labels
.has
("MClass")
507 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
508 var name
= node
["name"].to_s
509 var kind
= to_kind
(node
["kind"].to_s
)
510 var visibility
= to_visibility
(node
["visibility"].to_s
)
511 var parameter_names
= new Array[String]
512 if node
.has_key
("parameter_names") then
513 for e
in node
["parameter_names"].as(JsonArray) do
514 parameter_names
.add e
.to_s
517 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
518 mentities
[node
] = mclass
519 set_doc
(node
, mclass
)
523 # Build a `NeoNode` representing `mclassdef`.
524 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
525 var node
= make_node
(mclassdef
)
526 node
.labels
.add
"MClassDef"
527 node
["is_intro"] = mclassdef
.is_intro
528 node
["location"] = mclassdef
.location
.to_s
529 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
530 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
531 for mproperty
in mclassdef
.intro_mproperties
do
532 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
534 for mpropdef
in mclassdef
.mpropdefs
do
535 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
537 for sup
in mclassdef
.supertypes
do
538 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
543 # Build a new `MClassDef` from a `node`.
545 # REQUIRE `node.labels.has("MClassDef")`
546 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
547 if mentities
.has_key
(node
) then return mentities
[node
].as(MClassDef)
548 assert node
.labels
.has
("MClassDef")
549 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
550 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
551 var location
= to_location
(node
["location"].to_s
)
552 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
553 mentities
[node
] = mclassdef
554 set_doc
(node
, mclassdef
)
555 var supertypes
= new Array[MClassType]
556 for sup
in node
.out_nodes
("INHERITS") do
557 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
559 mclassdef
.set_supertypes
(supertypes
)
560 mclassdef
.add_in_hierarchy
564 # Build a `NeoNode` representing `mproperty`.
565 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
566 var node
= make_node
(mproperty
)
567 node
.labels
.add
"MProperty"
568 node
["full_name"] = mproperty
.full_name
569 node
["visibility"] = mproperty
.visibility
.to_s
570 if mproperty
isa MMethod then
571 node
.labels
.add
"MMethod"
572 node
["is_init"] = mproperty
.is_init
573 else if mproperty
isa MAttribute then
574 node
.labels
.add
"MAttribute"
575 else if mproperty
isa MVirtualTypeProp then
576 node
.labels
.add
"MVirtualTypeProp"
578 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
582 # Build a new `MProperty` from a `node`.
584 # REQUIRE `node.labels.has("MProperty")`
585 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
586 if mentities
.has_key
(node
) then return mentities
[node
].as(MProperty)
587 assert node
.labels
.has
("MProperty")
588 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
589 var name
= node
["name"].to_s
590 var visibility
= to_visibility
(node
["visibility"].to_s
)
591 var mprop
: nullable MProperty = null
592 if node
.labels
.has
("MMethod") then
593 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
594 mprop
.is_init
= node
["is_init"].as(Bool)
595 else if node
.labels
.has
("MAttribute") then
596 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
597 else if node
.labels
.has
("MVirtualTypeProp") then
598 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
600 if mprop
== null then
601 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
604 mentities
[node
] = mprop
609 # Build a `NeoNode` representing `mpropdef`.
610 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
611 var node
= make_node
(mpropdef
)
612 node
.labels
.add
"MPropDef"
613 node
["is_intro"] = mpropdef
.is_intro
614 node
["location"] = mpropdef
.location
.to_s
615 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
616 if mpropdef
isa MMethodDef then
617 node
.labels
.add
"MMethodDef"
618 node
["is_abstract"] = mpropdef
.is_abstract
619 node
["is_intern"] = mpropdef
.is_intern
620 node
["is_extern"] = mpropdef
.is_extern
621 var msignature
= mpropdef
.msignature
622 if msignature
!= null then
623 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
625 else if mpropdef
isa MAttributeDef then
626 node
.labels
.add
"MAttributeDef"
627 else if mpropdef
isa MVirtualTypeDef then
628 node
.labels
.add
"MVirtualTypeDef"
629 var bound
= mpropdef
.bound
630 if bound
!= null then
631 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
637 # Build a new `MPropDef` from a `node`.
639 # REQUIRE `node.labels.has("MPropDef")`
640 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
641 if mentities
.has_key
(node
) then return mentities
[node
].as(MPropDef)
642 assert node
.labels
.has
("MPropDef")
643 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
644 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
645 var location
= to_location
(node
["location"].to_s
)
646 var mpropdef
: nullable MPropDef = null
647 if node
.labels
.has
("MMethodDef") then
648 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
649 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
650 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
651 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
652 mentities
[node
] = mpropdef
653 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
654 else if node
.labels
.has
("MAttributeDef") then
655 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
656 mentities
[node
] = mpropdef
657 else if node
.labels
.has
("MVirtualTypeDef") then
658 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
659 mentities
[node
] = mpropdef
660 var bound
= node
.out_nodes
("BOUND")
661 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
663 if mpropdef
== null then
664 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
667 set_doc
(node
, mpropdef
)
671 # Build a `NeoNode` representing `mtype`.
672 private fun mtype_node
(mtype
: MType): NeoNode do
673 var node
= make_node
(mtype
)
674 node
.labels
.add
"MType"
675 if mtype
isa MClassType then
676 node
.labels
.add
"MClassType"
677 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
678 for arg
in mtype
.arguments
do
679 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
681 if mtype
isa MGenericType then
682 node
.labels
.add
"MGenericType"
684 else if mtype
isa MVirtualType then
685 node
.labels
.add
"MVirtualType"
686 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
687 else if mtype
isa MParameterType then
688 node
.labels
.add
"MParameterType"
689 node
["rank"] = mtype
.rank
690 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
691 else if mtype
isa MNullableType then
692 node
.labels
.add
"MNullableType"
693 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
694 else if mtype
isa MSignature then
695 node
.labels
.add
"MSignature"
696 var names
= new JsonArray
698 for mparameter
in mtype
.mparameters
do
699 names
.add mparameter
.name
700 var pnode
= to_node
(mparameter
)
702 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
704 if not names
.is_empty
then node
["parameter_names"] = names
705 var return_mtype
= mtype
.return_mtype
706 if return_mtype
!= null then
707 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
713 # Build a new `MType` from a `node`.
715 # REQUIRE `node.labels.has("MType")`
716 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
717 if mentities
.has_key
(node
) then return mentities
[node
].as(MType)
718 assert node
.labels
.has
("MType")
719 if node
.labels
.has
("MClassType") then
720 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
721 var args
= new Array[MType]
722 for narg
in node
.out_nodes
("ARGUMENT") do
723 args
.add to_mtype
(model
, narg
)
725 var mtype
= mclass
.get_mtype
(args
)
726 mentities
[node
] = mtype
728 else if node
.labels
.has
("MParameterType") then
729 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
730 var rank
= node
["rank"].to_s
.to_i
731 var mtype
= mclass
.mparameters
[rank
]
732 mentities
[node
] = mtype
734 else if node
.labels
.has
("MNullableType") then
735 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
736 var mtype
= intype
.as_nullable
737 mentities
[node
] = mtype
739 else if node
.labels
.has
("MVirtualType") then
740 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
741 assert mproperty
isa MVirtualTypeProp
742 var mtype
= mproperty
.mvirtualtype
743 mentities
[node
] = mtype
745 else if node
.labels
.has
("MSignature") then
746 # Get all param nodes
747 var mparam_nodes
= new HashMap[String, MParameter]
748 for pnode
in node
.out_nodes
("PARAMETER") do
749 var mparam
= to_mparameter
(model
, pnode
)
750 mparam_nodes
[mparam
.name
] = mparam
752 # Load params in the good order
753 var mparam_names
= node
["parameter_names"]
754 var mparameters
= new Array[MParameter]
755 if mparam_names
isa JsonArray then
756 for mparam_name
in mparam_names
do
757 var mparam
= mparam_nodes
[mparam_name
.to_s
]
758 mparameters
.add mparam
761 var return_mtype
: nullable MType = null
762 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
763 if not ret_nodes
.is_empty
then
764 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
766 var mtype
= new MSignature(mparameters
, return_mtype
)
767 mentities
[node
] = mtype
770 print
"not yet implemented to_mtype for {node.labels.join(",")}"
774 # Build a `NeoNode` representing `mparameter`.
775 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
776 var node
= make_node
(mparameter
)
777 node
.labels
.add
"MParameter"
778 node
["name"] = mparameter
.name
779 node
["is_vararg"] = mparameter
.is_vararg
780 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
784 # Build a new `MParameter` from `node`.
786 # REQUIRE `node.labels.has("MParameter")`
787 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
788 if mentities
.has_key
(node
) then return mentities
[node
].as(MParameter)
789 assert node
.labels
.has
("MParameter")
790 var name
= node
["name"].to_s
791 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
792 var is_vararg
= node
["is_vararg"].as(Bool)
793 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
794 mentities
[node
] = mparameter
798 # Get a `Location` from its string representation.
799 private fun to_location
(loc
: String): Location do
801 var parts
= loc
.split_with
(":")
802 var file
= new SourceFile.from_string
(parts
[0], "")
803 var pos
= parts
[1].split_with
("--")
804 var pos1
= pos
[0].split_with
(",")
805 var pos2
= pos
[1].split_with
(",")
806 var line_s
= pos1
[0].to_i
807 var line_e
= pos2
[0].to_i
808 var column_s
= pos1
[1].to_i
810 if pos2
.length
== 2 then pos2
[1].to_i
811 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
814 # Get a `MVisibility` from its string representation.
815 private fun to_visibility
(vis
: String): MVisibility do
816 if vis
== intrude_visibility
.to_s
then
817 return intrude_visibility
818 else if vis
== public_visibility
.to_s
then
819 return public_visibility
820 else if vis
== protected_visibility
.to_s
then
821 return protected_visibility
822 else if vis
== private_visibility
.to_s
then
823 return private_visibility
825 return none_visibility
829 # Get a `MKind` from its string representation.
830 private fun to_kind
(kind
: String): MClassKind do
831 if kind
== abstract_kind
.to_s
then
833 else if kind
== concrete_kind
.to_s
then
835 else if kind
== interface_kind
.to_s
then
836 return interface_kind
837 else if kind
== enum_kind
.to_s
then
839 else if kind
== extern_kind
.to_s
then
845 # Extract the `MDoc` from `node` and link it to `mentity`.
846 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
847 if node
.has_key
("mdoc") then
848 var lines
= new Array[String]
849 for e
in node
["mdoc"].as(JsonArray) do
850 lines
.add e
.to_s
#.replace("\n", "\\n")
853 mdoc
.content
.add_all
(lines
)
854 mdoc
.original_mentity
= mentity