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 var mgroup
= mmodule
.mgroup
450 for parent
in mmodule
.in_importation
.direct_greaters
do
451 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
453 for mclass
in mmodule
.intro_mclasses
do
454 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
456 for mclassdef
in mmodule
.mclassdefs
do
457 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
462 # Build a new `MModule` from a `node`.
464 # REQUIRE `node.labels.has("MModule")`
465 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
466 if mentities
.has_key
(node
) then return mentities
[node
].as(MModule)
467 assert node
.labels
.has
("MModule")
468 var ins
= node
.in_nodes
("DECLARES")
469 var mgroup
: nullable MGroup = null
470 if not ins
.is_empty
then
471 mgroup
= to_mgroup
(model
, ins
.first
)
473 var name
= node
["name"].to_s
474 var location
= to_location
(node
["location"].to_s
)
475 var mmodule
= new MModule(model
, mgroup
, name
, location
)
476 mentities
[node
] = mmodule
477 set_doc
(node
, mmodule
)
478 var imported_mmodules
= new Array[MModule]
479 for smod
in node
.out_nodes
("IMPORTS") do
480 imported_mmodules
.add to_mmodule
(model
, smod
)
482 mmodule
.set_imported_mmodules
(imported_mmodules
)
486 # Build a `NeoNode` representing `mclass`.
487 private fun mclass_node
(mclass
: MClass): NeoNode do
488 var node
= make_node
(mclass
)
489 node
.labels
.add
"MClass"
490 node
["full_name"] = mclass
.full_name
491 node
["kind"] = mclass
.kind
.to_s
492 node
["visibility"] = mclass
.visibility
.to_s
493 if not mclass
.mparameters
.is_empty
then
494 var parameter_names
= new Array[String]
495 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
496 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
498 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
502 # Build a new `MClass` from a `node`.
504 # REQUIRE `node.labels.has("MClass")`
505 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
506 if mentities
.has_key
(node
) then return mentities
[node
].as(MClass)
507 assert node
.labels
.has
("MClass")
508 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
509 var name
= node
["name"].to_s
510 var kind
= to_kind
(node
["kind"].to_s
)
511 var visibility
= to_visibility
(node
["visibility"].to_s
)
512 var parameter_names
= new Array[String]
513 if node
.has_key
("parameter_names") then
514 for e
in node
["parameter_names"].as(JsonArray) do
515 parameter_names
.add e
.to_s
518 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
519 mentities
[node
] = mclass
520 set_doc
(node
, mclass
)
524 # Build a `NeoNode` representing `mclassdef`.
525 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
526 var node
= make_node
(mclassdef
)
527 node
.labels
.add
"MClassDef"
528 node
["is_intro"] = mclassdef
.is_intro
529 node
["location"] = mclassdef
.location
.to_s
530 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
531 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
532 for mproperty
in mclassdef
.intro_mproperties
do
533 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
535 for mpropdef
in mclassdef
.mpropdefs
do
536 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
538 for sup
in mclassdef
.supertypes
do
539 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
544 # Build a new `MClassDef` from a `node`.
546 # REQUIRE `node.labels.has("MClassDef")`
547 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
548 if mentities
.has_key
(node
) then return mentities
[node
].as(MClassDef)
549 assert node
.labels
.has
("MClassDef")
550 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
551 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
552 var location
= to_location
(node
["location"].to_s
)
553 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
554 mentities
[node
] = mclassdef
555 set_doc
(node
, mclassdef
)
556 var supertypes
= new Array[MClassType]
557 for sup
in node
.out_nodes
("INHERITS") do
558 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
560 mclassdef
.set_supertypes
(supertypes
)
561 mclassdef
.add_in_hierarchy
565 # Build a `NeoNode` representing `mproperty`.
566 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
567 var node
= make_node
(mproperty
)
568 node
.labels
.add
"MProperty"
569 node
["full_name"] = mproperty
.full_name
570 node
["visibility"] = mproperty
.visibility
.to_s
571 if mproperty
isa MMethod then
572 node
.labels
.add
"MMethod"
573 node
["is_init"] = mproperty
.is_init
574 else if mproperty
isa MAttribute then
575 node
.labels
.add
"MAttribute"
576 else if mproperty
isa MVirtualTypeProp then
577 node
.labels
.add
"MVirtualTypeProp"
579 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
583 # Build a new `MProperty` from a `node`.
585 # REQUIRE `node.labels.has("MProperty")`
586 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
587 if mentities
.has_key
(node
) then return mentities
[node
].as(MProperty)
588 assert node
.labels
.has
("MProperty")
589 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
590 var name
= node
["name"].to_s
591 var visibility
= to_visibility
(node
["visibility"].to_s
)
592 var mprop
: nullable MProperty = null
593 if node
.labels
.has
("MMethod") then
594 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
595 mprop
.is_init
= node
["is_init"].as(Bool)
596 else if node
.labels
.has
("MAttribute") then
597 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
598 else if node
.labels
.has
("MVirtualTypeProp") then
599 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
601 if mprop
== null then
602 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
605 mentities
[node
] = mprop
607 for npropdef
in node
.in_nodes
("DEFINES") do
608 var mpropdef
= to_mpropdef
(model
, npropdef
)
609 if npropdef
["is_intro"].as(Bool) then
610 mprop
.mpropdefs
.unshift mpropdef
612 mprop
.mpropdefs
.add mpropdef
618 # Build a `NeoNode` representing `mpropdef`.
619 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
620 var node
= make_node
(mpropdef
)
621 node
.labels
.add
"MPropDef"
622 node
["is_intro"] = mpropdef
.is_intro
623 node
["location"] = mpropdef
.location
.to_s
624 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
625 if mpropdef
isa MMethodDef then
626 node
.labels
.add
"MMethodDef"
627 node
["is_abstract"] = mpropdef
.is_abstract
628 node
["is_intern"] = mpropdef
.is_intern
629 node
["is_extern"] = mpropdef
.is_extern
630 var msignature
= mpropdef
.msignature
631 if msignature
!= null then
632 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
634 else if mpropdef
isa MAttributeDef then
635 node
.labels
.add
"MAttributeDef"
636 else if mpropdef
isa MVirtualTypeDef then
637 node
.labels
.add
"MVirtualTypeDef"
638 var bound
= mpropdef
.bound
639 if bound
!= null then
640 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
646 # Build a new `MPropDef` from a `node`.
648 # REQUIRE `node.labels.has("MPropDef")`
649 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
650 if mentities
.has_key
(node
) then return mentities
[node
].as(MPropDef)
651 assert node
.labels
.has
("MPropDef")
652 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
653 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
654 var location
= to_location
(node
["location"].to_s
)
655 var mpropdef
: nullable MPropDef = null
656 if node
.labels
.has
("MMethodDef") then
657 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
658 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
659 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
660 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
661 mentities
[node
] = mpropdef
662 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
663 else if node
.labels
.has
("MAttributeDef") then
664 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
665 mentities
[node
] = mpropdef
666 else if node
.labels
.has
("MVirtualTypeDef") then
667 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
668 mentities
[node
] = mpropdef
669 var bound
= node
.out_nodes
("BOUND")
670 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
672 if mpropdef
== null then
673 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
676 set_doc
(node
, mpropdef
)
680 # Build a `NeoNode` representing `mtype`.
681 private fun mtype_node
(mtype
: MType): NeoNode do
682 var node
= make_node
(mtype
)
683 node
.labels
.add
"MType"
684 if mtype
isa MClassType then
685 node
.labels
.add
"MClassType"
686 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
687 for arg
in mtype
.arguments
do
688 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
690 if mtype
isa MGenericType then
691 node
.labels
.add
"MGenericType"
693 else if mtype
isa MVirtualType then
694 node
.labels
.add
"MVirtualType"
695 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
696 else if mtype
isa MParameterType then
697 node
.labels
.add
"MParameterType"
698 node
["rank"] = mtype
.rank
699 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
700 else if mtype
isa MNullableType then
701 node
.labels
.add
"MNullableType"
702 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
703 else if mtype
isa MSignature then
704 node
.labels
.add
"MSignature"
705 var names
= new JsonArray
707 for mparameter
in mtype
.mparameters
do
708 names
.add mparameter
.name
709 var pnode
= to_node
(mparameter
)
711 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
713 if not names
.is_empty
then node
["parameter_names"] = names
714 var return_mtype
= mtype
.return_mtype
715 if return_mtype
!= null then
716 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
722 # Build a new `MType` from a `node`.
724 # REQUIRE `node.labels.has("MType")`
725 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
726 if mentities
.has_key
(node
) then return mentities
[node
].as(MType)
727 assert node
.labels
.has
("MType")
728 if node
.labels
.has
("MClassType") then
729 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
730 var args
= new Array[MType]
731 for narg
in node
.out_nodes
("ARGUMENT") do
732 args
.add to_mtype
(model
, narg
)
734 var mtype
= mclass
.get_mtype
(args
)
735 mentities
[node
] = mtype
737 else if node
.labels
.has
("MParameterType") then
738 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
739 var rank
= node
["rank"].to_s
.to_i
740 var mtype
= mclass
.mparameters
[rank
]
741 mentities
[node
] = mtype
743 else if node
.labels
.has
("MNullableType") then
744 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
745 var mtype
= intype
.as_nullable
746 mentities
[node
] = mtype
748 else if node
.labels
.has
("MVirtualType") then
749 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
750 assert mproperty
isa MVirtualTypeProp
751 var mtype
= mproperty
.mvirtualtype
752 mentities
[node
] = mtype
754 else if node
.labels
.has
("MSignature") then
755 # Get all param nodes
756 var mparam_nodes
= new HashMap[String, MParameter]
757 for pnode
in node
.out_nodes
("PARAMETER") do
758 var mparam
= to_mparameter
(model
, pnode
)
759 mparam_nodes
[mparam
.name
] = mparam
761 # Load params in the good order
762 var mparam_names
= node
["parameter_names"]
763 var mparameters
= new Array[MParameter]
764 if mparam_names
isa JsonArray then
765 for mparam_name
in mparam_names
do
766 var mparam
= mparam_nodes
[mparam_name
.to_s
]
767 mparameters
.add mparam
770 var return_mtype
: nullable MType = null
771 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
772 if not ret_nodes
.is_empty
then
773 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
775 var mtype
= new MSignature(mparameters
, return_mtype
)
776 mentities
[node
] = mtype
779 print
"not yet implemented to_mtype for {node.labels.join(",")}"
783 # Build a `NeoNode` representing `mparameter`.
784 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
785 var node
= make_node
(mparameter
)
786 node
.labels
.add
"MParameter"
787 node
["name"] = mparameter
.name
788 node
["is_vararg"] = mparameter
.is_vararg
789 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
793 # Build a new `MParameter` from `node`.
795 # REQUIRE `node.labels.has("MParameter")`
796 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
797 if mentities
.has_key
(node
) then return mentities
[node
].as(MParameter)
798 assert node
.labels
.has
("MParameter")
799 var name
= node
["name"].to_s
800 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
801 var is_vararg
= node
["is_vararg"].as(Bool)
802 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
803 mentities
[node
] = mparameter
807 # Get a `Location` from its string representation.
808 private fun to_location
(loc
: String): Location do
810 var parts
= loc
.split_with
(":")
811 var file
= new SourceFile.from_string
(parts
[0], "")
812 var pos
= parts
[1].split_with
("--")
813 var pos1
= pos
[0].split_with
(",")
814 var pos2
= pos
[1].split_with
(",")
815 var line_s
= pos1
[0].to_i
816 var line_e
= pos2
[0].to_i
817 var column_s
= pos1
[1].to_i
819 if pos2
.length
== 2 then pos2
[1].to_i
820 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
823 # Get a `MVisibility` from its string representation.
824 private fun to_visibility
(vis
: String): MVisibility do
825 if vis
== intrude_visibility
.to_s
then
826 return intrude_visibility
827 else if vis
== public_visibility
.to_s
then
828 return public_visibility
829 else if vis
== protected_visibility
.to_s
then
830 return protected_visibility
831 else if vis
== private_visibility
.to_s
then
832 return private_visibility
834 return none_visibility
838 # Get a `MKind` from its string representation.
839 private fun to_kind
(kind
: String): MClassKind do
840 if kind
== abstract_kind
.to_s
then
842 else if kind
== concrete_kind
.to_s
then
844 else if kind
== interface_kind
.to_s
then
845 return interface_kind
846 else if kind
== enum_kind
.to_s
then
848 else if kind
== extern_kind
.to_s
then
854 # Extract the `MDoc` from `node` and link it to `mentity`.
855 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
856 if node
.has_key
("mdoc") then
857 var lines
= new Array[String]
858 for e
in node
["mdoc"].as(JsonArray) do
859 lines
.add e
.to_s
#.replace("\n", "\\n")
862 mdoc
.content
.add_all
(lines
)
863 mdoc
.original_mentity
= mentity