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 a `Model` to/from a Neo4j graph.
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 the graph:
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 # * `kind`: kind of the class (`interface`, `abstract class`, etc.)
71 # * `visibility`: visibility of the class.
72 # * `parameter_names`: JSON array listing the name of each formal generic
73 # parameter (in order of declaration).
74 # * `(:MClass)-[:CLASSTYPE]->(:MClassType)`: SEE: `MClass.mclass_type`
76 # Arguments in the `CLASSTYPE` are named following the `parameter_names`
77 # attribute of the `MClassDef` that introduces the class. A class definition
78 # introduces a class if and only if it has this class as `MCLASS` and
79 # has `is_intro` set to `true`.
83 # * labels: `MClassDef`, `model_name` and `MEntity`.
84 # * `location`: origin of the definition. SEE: `Location.to_s`
85 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
87 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
88 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
90 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
91 # classdef (introductions and redefinitions).
92 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
96 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
97 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
99 # * `full_name`: fully qualified name.
100 # * `visibility`: visibility of the property.
101 # * `is_init`: Indicates if the property is a constructor. Exists only if the
102 # node is a `MMethod`.
103 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
108 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
109 # `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
110 # represented entity.
111 # * `location`: origin of the definition. SEE: `Location.to_s`.
112 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
114 # Additional attributes and relationship for `MMethodDef`:
116 # * `is_abstract`: Is the method definition abstract?
117 # * `is_intern`: Is the method definition intern?
118 # * `is_extern`: Is the method definition extern?
119 # * `(:MMethodDef)-[:SIGNATURE]->(:MSignature)`: signature attached to the
120 # property definition.
122 # Additional relationship for `MAttributeDef`:
124 # * `(:MAttributeDef)-[:TYPE]->(:MType)`: static type of the attribute,
127 # Additional relationship for `MVirtualTypeDef`:
129 # * `(:MVirtualTypeDef)-[:BOUND]->(:MType)`: type to which the virtual type
130 # is bound in this definition. Exists only if this definition bound the virtual
131 # type to an effective type.
135 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
136 # `MNullableType`, `MVirtualType` or `MSignature`, depending on the class of
137 # the represented entity.
139 # Additional label and relationships for `MClassType`:
141 # * If it is a `MGenericType`, also has the `MGenericType` label.
142 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
143 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
145 # Arguments are named following the `parameter_names` attribute of the
146 # `MClass` referred by `CLASS`.
148 # Additional relationship for `MVirtualType`:
150 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
151 # determines the type (usually a `MVirtualTypeProp`).
153 # Additional attribute and relationship for `MParameterType`:
155 # * `rank`: position of the parameter (0 for the first parameter).
156 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
159 # Additional relationship for `MNullableType`:
161 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
163 # Additional attribute and relationships for `MSignature`:
165 # * `parameter_names`: JSON array representing the list of the parameter names.
166 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
167 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
170 # In order to maintain the correct parameters order, each `MSignature` node
171 # contains an array of parameter names corresponding to the parameter order in
174 # For example, if the source code contains:
176 # fun foo(a: A, b: B, c: C)
178 # The `MSignature` node will contain a property
179 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
180 # with the parameters in the correct order.
184 # * labels: `MParameter`, `model_name` and `MEntity`.
185 # * `is_vararg`: Is the parameter a vararg?
186 # * `rank`: position of the parameter (0 for the first parameter).
187 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
189 # MParameters are also ranked by their position in the corresponding signature.
190 # Rank 0 for the first parameter, 1 for the next one and etc.
197 # Helper class that can save and load a `Model` into a Neo4j database.
202 # Because we use only one Neo4j instance to store all the models,
203 # we need to mark their appartenance to a particular model and avoid loading all models.
205 # The name is used as a Neo label on each created nodes and used to load nodes from base.
206 var model_name
: String
208 # The toolcontext used to init the `NeoModel` tool.
209 var toolcontext
: ToolContext
211 # The Neo4j `client` used to communicate with the Neo4j instance.
212 var client
: Neo4jClient
214 # Fill `model` using base pointed by `client`.
215 fun load
(model
: Model): Model do
216 var nodes
: Array[NeoNode]
218 toolcontext
.info
("Loading project node...", 1)
219 nodes
= client
.nodes_with_labels
([model_name
, "MProject"])
220 for node
in nodes
do to_mproject
(model
, node
)
221 toolcontext
.info
("Loading groups...", 1)
222 nodes
= client
.nodes_with_labels
([model_name
, "MGroup"])
223 for node
in nodes
do to_mgroup
(model
, node
)
224 toolcontext
.info
("Loading modules...", 1)
225 nodes
= client
.nodes_with_labels
([model_name
, "MModule"])
226 for node
in nodes
do to_mmodule
(model
, node
)
227 toolcontext
.info
("Loading classes...", 1)
228 nodes
= client
.nodes_with_labels
([model_name
, "MClass"])
229 for node
in nodes
do to_mclass
(model
, node
)
230 toolcontext
.info
("Loading class definitions...", 1)
231 nodes
= client
.nodes_with_labels
([model_name
, "MClassDef"])
232 for node
in nodes
do to_mclassdef
(model
, node
)
233 toolcontext
.info
("Loading properties...", 1)
234 nodes
= client
.nodes_with_labels
([model_name
, "MProperty"])
235 for node
in nodes
do to_mproperty
(model
, node
)
236 toolcontext
.info
("Loading property definitions...", 1)
237 nodes
= client
.nodes_with_labels
([model_name
, "MPropDef"])
238 for node
in nodes
do to_mpropdef
(model
, node
)
242 # Save `model` in the base pointed by `client`.
243 fun save
(model
: Model) do
244 var nodes
= collect_model_nodes
(model
)
245 toolcontext
.info
("Save {nodes.length} nodes...", 1)
247 var edges
= collect_model_edges
(model
)
248 toolcontext
.info
("Save {edges.length} edges...", 1)
252 # Save `neo_entities` in base using batch mode.
253 private fun push_all
(neo_entities
: Collection[NeoEntity]) do
254 var batch
= new NeoBatch(client
)
255 var len
= neo_entities
.length
258 for nentity
in neo_entities
do
259 batch
.save_entity
(nentity
)
260 if i
== batch_max_size
then
262 sum
+= batch_max_size
263 toolcontext
.info
(" {sum * 100 / len}% done", 1)
264 batch
= new NeoBatch(client
)
273 # How many operation can be executed in one batch?
274 private var batch_max_size
= 1000
276 # Execute `batch` and check for errors.
278 # Abort if `batch.execute` returns errors.
279 private fun do_batch
(batch
: NeoBatch) do
280 var errors
= batch
.execute
281 if not errors
.is_empty
then
287 # Collect all nodes from the current `model`.
288 private fun collect_model_nodes
(model
: Model): Collection[NeoNode] do
289 for mproject
in model
.mprojects
do
291 for mgroup
in mproject
.mgroups
do to_node
(mgroup
)
296 # Collect all edges from the current `model`.
298 # Actually collect all out_edges from all nodes.
299 private fun collect_model_edges
(model
: Model): Collection[NeoEdge] do
300 var edges
= new HashSet[NeoEdge]
301 for node
in nodes
.values
do edges
.add_all
(node
.out_edges
)
305 # Mentities associated to nodes.
306 private var mentities
= new HashMap[NeoNode, MEntity]
308 # Nodes associated with MEntities.
309 private var nodes
= new HashMap[MEntity, NeoNode]
311 # Get the `NeoNode` associated with `mentity`.
312 # `mentities` are stored locally to avoid duplication.
313 fun to_node
(mentity
: MEntity): NeoNode do
314 if nodes
.has_key
(mentity
) then return nodes
[mentity
]
315 if mentity
isa MProject then return mproject_node
(mentity
)
316 if mentity
isa MGroup then return mgroup_node
(mentity
)
317 if mentity
isa MModule then return mmodule_node
(mentity
)
318 if mentity
isa MClass then return mclass_node
(mentity
)
319 if mentity
isa MClassDef then return mclassdef_node
(mentity
)
320 if mentity
isa MProperty then return mproperty_node
(mentity
)
321 if mentity
isa MPropDef then return mpropdef_node
(mentity
)
322 if mentity
isa MType then return mtype_node
(mentity
)
323 if mentity
isa MParameter then return mparameter_node
(mentity
)
327 # Make a new `NeoNode` based on `mentity`.
328 private fun make_node
(mentity
: MEntity): NeoNode do
329 var node
= new NeoNode
330 nodes
[mentity
] = node
331 node
.labels
.add
"MEntity"
332 node
.labels
.add model_name
333 node
["name"] = mentity
.name
334 if mentity
.mdoc
!= null then node
["mdoc"] = new JsonArray.from
(mentity
.mdoc
.content
)
338 # Build a `NeoNode` representing `mproject`.
339 private fun mproject_node
(mproject
: MProject): NeoNode do
340 var node
= make_node
(mproject
)
341 node
.labels
.add
"MProject"
342 var root
= mproject
.root
344 node
.out_edges
.add
(new NeoEdge(node
, "ROOT", to_node
(root
)))
349 # Build a new `MProject` from a `node`.
351 # REQUIRE `node.labels.has("MProject")`
352 private fun to_mproject
(model
: Model, node
: NeoNode): MProject do
353 if mentities
.has_key
(node
) then return mentities
[node
].as(MProject)
354 assert node
.labels
.has
("MProject")
355 var mproject
= new MProject(node
["name"].to_s
, model
)
356 mentities
[node
] = mproject
357 set_doc
(node
, mproject
)
358 mproject
.root
= to_mgroup
(model
, node
.out_nodes
("ROOT").first
)
362 # Build a `NeoNode` representing `mgroup`.
363 private fun mgroup_node
(mgroup
: MGroup): NeoNode do
364 var node
= make_node
(mgroup
)
365 node
.labels
.add
"MGroup"
366 node
["full_name"] = mgroup
.full_name
367 var parent
= mgroup
.parent
368 node
.out_edges
.add
(new NeoEdge(node
, "PROJECT", to_node
(mgroup
.mproject
)))
369 if parent
!= null then
370 node
.out_edges
.add
(new NeoEdge(node
, "PARENT", to_node
(parent
)))
372 for mmodule
in mgroup
.mmodules
do
373 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mmodule
)))
375 for subgroup
in mgroup
.in_nesting
.direct_smallers
do
376 node
.in_edges
.add
(new NeoEdge(node
, "NESTS", to_node
(subgroup
)))
381 # Build a new `MGroup` from a `node`.
383 # REQUIRE `node.labels.has("MGroup")`
384 private fun to_mgroup
(model
: Model, node
: NeoNode): MGroup do
385 if mentities
.has_key
(node
) then return mentities
[node
].as(MGroup)
386 assert node
.labels
.has
("MGroup")
387 var mproject
= to_mproject
(model
, node
.out_nodes
("PROJECT").first
)
388 var parent
: nullable MGroup = null
389 var out
= node
.out_nodes
("PARENT")
390 if not out
.is_empty
then
391 parent
= to_mgroup
(model
, out
.first
)
393 var mgroup
= new MGroup(node
["name"].to_s
, mproject
, parent
)
394 mentities
[node
] = mgroup
395 set_doc
(node
, mgroup
)
399 # Build a `NeoNode` representing `mmodule`.
400 private fun mmodule_node
(mmodule
: MModule): NeoNode do
401 var node
= make_node
(mmodule
)
402 node
.labels
.add
"MModule"
403 node
["full_name"] = mmodule
.full_name
404 node
["location"] = mmodule
.location
.to_s
405 for parent
in mmodule
.in_importation
.direct_greaters
do
406 node
.out_edges
.add
(new NeoEdge(node
, "IMPORTS", to_node
(parent
)))
408 for mclass
in mmodule
.intro_mclasses
do
409 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mclass
)))
411 for mclassdef
in mmodule
.mclassdefs
do
412 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mclassdef
)))
417 # Build a new `MModule` from a `node`.
419 # REQUIRE `node.labels.has("MModule")`
420 private fun to_mmodule
(model
: Model, node
: NeoNode): MModule do
421 if mentities
.has_key
(node
) then return mentities
[node
].as(MModule)
422 assert node
.labels
.has
("MModule")
423 var ins
= node
.in_nodes
("DECLARES")
424 var mgroup
: nullable MGroup = null
425 if not ins
.is_empty
then
426 mgroup
= to_mgroup
(model
, ins
.first
)
428 var name
= node
["name"].to_s
429 var location
= to_location
(node
["location"].to_s
)
430 var mmodule
= new MModule(model
, mgroup
, name
, location
)
431 mentities
[node
] = mmodule
432 set_doc
(node
, mmodule
)
433 var imported_mmodules
= new Array[MModule]
434 for smod
in node
.out_nodes
("IMPORTS") do
435 imported_mmodules
.add to_mmodule
(model
, smod
)
437 mmodule
.set_imported_mmodules
(imported_mmodules
)
441 # Build a `NeoNode` representing `mclass`.
442 private fun mclass_node
(mclass
: MClass): NeoNode do
443 var node
= make_node
(mclass
)
444 node
.labels
.add
"MClass"
445 node
["full_name"] = mclass
.full_name
446 node
["kind"] = mclass
.kind
.to_s
447 node
["visibility"] = mclass
.visibility
.to_s
448 if not mclass
.mparameters
.is_empty
then
449 var parameter_names
= new Array[String]
450 for p
in mclass
.mparameters
do parameter_names
.add
(p
.name
)
451 node
["parameter_names"] = new JsonArray.from
(parameter_names
)
453 node
.out_edges
.add
(new NeoEdge(node
, "CLASSTYPE", to_node
(mclass
.mclass_type
)))
457 # Build a new `MClass` from a `node`.
459 # REQUIRE `node.labels.has("MClass")`
460 private fun to_mclass
(model
: Model, node
: NeoNode): MClass do
461 if mentities
.has_key
(node
) then return mentities
[node
].as(MClass)
462 assert node
.labels
.has
("MClass")
463 var mmodule
= to_mmodule
(model
, node
.in_nodes
("INTRODUCES").first
)
464 var name
= node
["name"].to_s
465 var kind
= to_kind
(node
["kind"].to_s
)
466 var visibility
= to_visibility
(node
["visibility"].to_s
)
467 var parameter_names
= new Array[String]
468 if node
.has_key
("parameter_names") then
469 for e
in node
["parameter_names"].as(JsonArray) do
470 parameter_names
.add e
.to_s
473 var mclass
= new MClass(mmodule
, name
, parameter_names
, kind
, visibility
)
474 mentities
[node
] = mclass
475 set_doc
(node
, mclass
)
479 # Build a `NeoNode` representing `mclassdef`.
480 private fun mclassdef_node
(mclassdef
: MClassDef): NeoNode do
481 var node
= make_node
(mclassdef
)
482 node
.labels
.add
"MClassDef"
483 node
["location"] = mclassdef
.location
.to_s
484 node
.out_edges
.add
(new NeoEdge(node
, "BOUNDTYPE", to_node
(mclassdef
.bound_mtype
)))
485 node
.out_edges
.add
(new NeoEdge(node
, "MCLASS", to_node
(mclassdef
.mclass
)))
486 for mproperty
in mclassdef
.intro_mproperties
do
487 node
.out_edges
.add
(new NeoEdge(node
, "INTRODUCES", to_node
(mproperty
)))
489 for mpropdef
in mclassdef
.mpropdefs
do
490 node
.out_edges
.add
(new NeoEdge(node
, "DECLARES", to_node
(mpropdef
)))
492 for sup
in mclassdef
.supertypes
do
493 node
.out_edges
.add
(new NeoEdge(node
, "INHERITS", to_node
(sup
)))
498 # Build a new `MClassDef` from a `node`.
500 # REQUIRE `node.labels.has("MClassDef")`
501 private fun to_mclassdef
(model
: Model, node
: NeoNode): MClassDef do
502 if mentities
.has_key
(node
) then return mentities
[node
].as(MClassDef)
503 assert node
.labels
.has
("MClassDef")
504 var mmodule
= to_mmodule
(model
, node
.in_nodes
("DEFINES").first
)
505 var mtype
= to_mtype
(model
, node
.out_nodes
("BOUNDTYPE").first
).as(MClassType)
506 var location
= to_location
(node
["location"].to_s
)
507 var mclassdef
= new MClassDef(mmodule
, mtype
, location
)
508 mentities
[node
] = mclassdef
509 set_doc
(node
, mclassdef
)
510 var supertypes
= new Array[MClassType]
511 for sup
in node
.out_nodes
("INHERITS") do
512 supertypes
.add to_mtype
(model
, sup
).as(MClassType)
514 mclassdef
.set_supertypes
(supertypes
)
515 mclassdef
.add_in_hierarchy
519 # Build a `NeoNode` representing `mproperty`.
520 private fun mproperty_node
(mproperty
: MProperty): NeoNode do
521 var node
= make_node
(mproperty
)
522 node
.labels
.add
"MProperty"
523 node
["full_name"] = mproperty
.full_name
524 node
["visibility"] = mproperty
.visibility
.to_s
525 if mproperty
isa MMethod then
526 node
.labels
.add
"MMethod"
527 node
["is_init"] = mproperty
.is_init
528 else if mproperty
isa MAttribute then
529 node
.labels
.add
"MAttribute"
530 else if mproperty
isa MVirtualTypeProp then
531 node
.labels
.add
"MVirtualTypeProp"
533 node
.out_edges
.add
(new NeoEdge(node
, "INTRO_CLASSDEF", to_node
(mproperty
.intro_mclassdef
)))
537 # Build a new `MProperty` from a `node`.
539 # REQUIRE `node.labels.has("MProperty")`
540 private fun to_mproperty
(model
: Model, node
: NeoNode): MProperty do
541 if mentities
.has_key
(node
) then return mentities
[node
].as(MProperty)
542 assert node
.labels
.has
("MProperty")
543 var intro_mclassdef
= to_mclassdef
(model
, node
.out_nodes
("INTRO_CLASSDEF").first
)
544 var name
= node
["name"].to_s
545 var visibility
= to_visibility
(node
["visibility"].to_s
)
546 var mprop
: nullable MProperty = null
547 if node
.labels
.has
("MMethod") then
548 mprop
= new MMethod(intro_mclassdef
, name
, visibility
)
549 mprop
.is_init
= node
["is_init"].as(Bool)
550 else if node
.labels
.has
("MAttribute") then
551 mprop
= new MAttribute(intro_mclassdef
, name
, visibility
)
552 else if node
.labels
.has
("MVirtualTypeProp") then
553 mprop
= new MVirtualTypeProp(intro_mclassdef
, name
, visibility
)
555 if mprop
== null then
556 print
"not yet implemented to_mproperty for {node.labels.join(",")}"
559 mentities
[node
] = mprop
564 # Build a `NeoNode` representing `mpropdef`.
565 private fun mpropdef_node
(mpropdef
: MPropDef): NeoNode do
566 var node
= make_node
(mpropdef
)
567 node
.labels
.add
"MPropDef"
568 node
["location"] = mpropdef
.location
.to_s
569 node
.out_edges
.add
(new NeoEdge(node
, "DEFINES", to_node
(mpropdef
.mproperty
)))
570 if mpropdef
isa MMethodDef then
571 node
.labels
.add
"MMethodDef"
572 node
["is_abstract"] = mpropdef
.is_abstract
573 node
["is_intern"] = mpropdef
.is_intern
574 node
["is_extern"] = mpropdef
.is_extern
575 var msignature
= mpropdef
.msignature
576 if msignature
!= null then
577 node
.out_edges
.add
(new NeoEdge(node
, "SIGNATURE", to_node
(msignature
)))
579 else if mpropdef
isa MAttributeDef then
580 node
.labels
.add
"MAttributeDef"
581 var static_mtype
= mpropdef
.static_mtype
582 if static_mtype
!= null then
583 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(static_mtype
)))
585 else if mpropdef
isa MVirtualTypeDef then
586 node
.labels
.add
"MVirtualTypeDef"
587 var bound
= mpropdef
.bound
588 if bound
!= null then
589 node
.out_edges
.add
(new NeoEdge(node
, "BOUND", to_node
(bound
)))
595 # Build a new `MPropDef` from a `node`.
597 # REQUIRE `node.labels.has("MPropDef")`
598 private fun to_mpropdef
(model
: Model, node
: NeoNode): MPropDef do
599 if mentities
.has_key
(node
) then return mentities
[node
].as(MPropDef)
600 assert node
.labels
.has
("MPropDef")
601 var mclassdef
= to_mclassdef
(model
, node
.in_nodes
("DECLARES").first
)
602 var mproperty
= to_mproperty
(model
, node
.out_nodes
("DEFINES").first
)
603 var location
= to_location
(node
["location"].to_s
)
604 var mpropdef
: nullable MPropDef = null
605 if node
.labels
.has
("MMethodDef") then
606 mpropdef
= new MMethodDef(mclassdef
, mproperty
.as(MMethod), location
)
607 mpropdef
.is_abstract
= node
["is_abstract"].as(Bool)
608 mpropdef
.is_intern
= node
["is_intern"].as(Bool)
609 mpropdef
.is_extern
= node
["is_extern"].as(Bool)
610 mentities
[node
] = mpropdef
611 mpropdef
.msignature
= to_mtype
(model
, node
.out_nodes
("SIGNATURE").first
).as(MSignature)
612 else if node
.labels
.has
("MAttributeDef") then
613 mpropdef
= new MAttributeDef(mclassdef
, mproperty
.as(MAttribute), location
)
614 mentities
[node
] = mpropdef
615 var static_mtype
= node
.out_nodes
("TYPE")
616 if not static_mtype
.is_empty
then mpropdef
.static_mtype
= to_mtype
(model
, static_mtype
.first
)
617 else if node
.labels
.has
("MVirtualTypeDef") then
618 mpropdef
= new MVirtualTypeDef(mclassdef
, mproperty
.as(MVirtualTypeProp), location
)
619 mentities
[node
] = mpropdef
620 var bound
= node
.out_nodes
("BOUND")
621 if not bound
.is_empty
then mpropdef
.bound
= to_mtype
(model
, bound
.first
)
623 if mpropdef
== null then
624 print
"not yet implemented to_mpropdef for {node.labels.join(",")}"
627 set_doc
(node
, mpropdef
)
631 # Build a `NeoNode` representing `mtype`.
632 private fun mtype_node
(mtype
: MType): NeoNode do
633 var node
= make_node
(mtype
)
634 node
.labels
.add
"MType"
635 if mtype
isa MClassType then
636 node
.labels
.add
"MClassType"
637 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
638 for arg
in mtype
.arguments
do
639 node
.out_edges
.add
(new NeoEdge(node
, "ARGUMENT", to_node
(arg
)))
641 if mtype
isa MGenericType then
642 node
.labels
.add
"MGenericType"
644 else if mtype
isa MVirtualType then
645 node
.labels
.add
"MVirtualType"
646 node
.out_edges
.add
(new NeoEdge(node
, "PROPERTY", to_node
(mtype
.mproperty
)))
647 else if mtype
isa MParameterType then
648 node
.labels
.add
"MParameterType"
649 node
["rank"] = mtype
.rank
650 node
.out_edges
.add
(new NeoEdge(node
, "CLASS", to_node
(mtype
.mclass
)))
651 else if mtype
isa MNullableType then
652 node
.labels
.add
"MNullableType"
653 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mtype
.mtype
)))
654 else if mtype
isa MSignature then
655 node
.labels
.add
"MSignature"
656 var names
= new JsonArray
658 for mparameter
in mtype
.mparameters
do
659 names
.add mparameter
.name
660 var pnode
= to_node
(mparameter
)
662 node
.out_edges
.add
(new NeoEdge(node
, "PARAMETER", pnode
))
664 if not names
.is_empty
then node
["parameter_names"] = names
665 var return_mtype
= mtype
.return_mtype
666 if return_mtype
!= null then
667 node
.out_edges
.add
(new NeoEdge(node
, "RETURNTYPE", to_node
(return_mtype
)))
673 # Build a new `MType` from a `node`.
675 # REQUIRE `node.labels.has("MType")`
676 private fun to_mtype
(model
: Model, node
: NeoNode): MType do
677 if mentities
.has_key
(node
) then return mentities
[node
].as(MType)
678 assert node
.labels
.has
("MType")
679 if node
.labels
.has
("MClassType") then
680 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
681 var args
= new Array[MType]
682 for narg
in node
.out_nodes
("ARGUMENT") do
683 args
.add to_mtype
(model
, narg
)
685 var mtype
= mclass
.get_mtype
(args
)
686 mentities
[node
] = mtype
688 else if node
.labels
.has
("MParameterType") then
689 var mclass
= to_mclass
(model
, node
.out_nodes
("CLASS").first
)
690 var rank
= node
["rank"].to_s
.to_i
691 var mtype
= mclass
.mparameters
[rank
]
692 mentities
[node
] = mtype
694 else if node
.labels
.has
("MNullableType") then
695 var intype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
696 var mtype
= intype
.as_nullable
697 mentities
[node
] = mtype
699 else if node
.labels
.has
("MVirtualType") then
700 var mproperty
= to_mproperty
(model
, node
.out_nodes
("PROPERTY").first
)
701 assert mproperty
isa MVirtualTypeProp
702 var mtype
= mproperty
.mvirtualtype
703 mentities
[node
] = mtype
705 else if node
.labels
.has
("MSignature") then
706 # Get all param nodes
707 var mparam_nodes
= new HashMap[String, MParameter]
708 for pnode
in node
.out_nodes
("PARAMETER") do
709 var mparam
= to_mparameter
(model
, pnode
)
710 mparam_nodes
[mparam
.name
] = mparam
712 # Load params in the good order
713 var mparam_names
= node
["parameter_names"]
714 var mparameters
= new Array[MParameter]
715 if mparam_names
isa JsonArray then
716 for mparam_name
in mparam_names
do
717 var mparam
= mparam_nodes
[mparam_name
.to_s
]
718 mparameters
.add mparam
721 var return_mtype
: nullable MType = null
722 var ret_nodes
= node
.out_nodes
("RETURNTYPE")
723 if not ret_nodes
.is_empty
then
724 return_mtype
= to_mtype
(model
, ret_nodes
.first
)
726 var mtype
= new MSignature(mparameters
, return_mtype
)
727 mentities
[node
] = mtype
730 print
"not yet implemented to_mtype for {node.labels.join(",")}"
734 # Build a `NeoNode` representing `mparameter`.
735 private fun mparameter_node
(mparameter
: MParameter): NeoNode do
736 var node
= make_node
(mparameter
)
737 node
.labels
.add
"MParameter"
738 node
["name"] = mparameter
.name
739 node
["is_vararg"] = mparameter
.is_vararg
740 node
.out_edges
.add
(new NeoEdge(node
, "TYPE", to_node
(mparameter
.mtype
)))
744 # Build a new `MParameter` from `node`.
746 # REQUIRE `node.labels.has("MParameter")`
747 private fun to_mparameter
(model
: Model, node
: NeoNode): MParameter do
748 if mentities
.has_key
(node
) then return mentities
[node
].as(MParameter)
749 assert node
.labels
.has
("MParameter")
750 var name
= node
["name"].to_s
751 var mtype
= to_mtype
(model
, node
.out_nodes
("TYPE").first
)
752 var is_vararg
= node
["is_vararg"].as(Bool)
753 var mparameter
= new MParameter(name
, mtype
, is_vararg
)
754 mentities
[node
] = mparameter
758 # Get a `Location` from its string representation.
759 private fun to_location
(loc
: String): Location do
761 var parts
= loc
.split_with
(":")
762 var file
= new SourceFile.from_string
(parts
[0], "")
763 var pos
= parts
[1].split_with
("--")
764 var pos1
= pos
[0].split_with
(",")
765 var pos2
= pos
[1].split_with
(",")
766 var line_s
= pos1
[0].to_i
767 var line_e
= pos2
[0].to_i
768 var column_s
= pos1
[1].to_i
770 if pos2
.length
== 2 then pos2
[1].to_i
771 return new Location(file
, line_s
, line_e
, column_s
, column_e
)
774 # Get a `MVisibility` from its string representation.
775 private fun to_visibility
(vis
: String): MVisibility do
776 if vis
== intrude_visibility
.to_s
then
777 return intrude_visibility
778 else if vis
== public_visibility
.to_s
then
779 return public_visibility
780 else if vis
== protected_visibility
.to_s
then
781 return protected_visibility
782 else if vis
== private_visibility
.to_s
then
783 return private_visibility
785 return none_visibility
789 # Get a `MKind` from its string representation.
790 private fun to_kind
(kind
: String): MClassKind do
791 if kind
== abstract_kind
.to_s
then
793 else if kind
== concrete_kind
.to_s
then
795 else if kind
== interface_kind
.to_s
then
796 return interface_kind
797 else if kind
== enum_kind
.to_s
then
799 else if kind
== extern_kind
.to_s
then
805 # Extract the `MDoc` from `node` and link it to `mentity`.
806 private fun set_doc
(node
: NeoNode, mentity
: MEntity) do
807 if node
.has_key
("mdoc") then
808 var lines
= new Array[String]
809 for e
in node
["mdoc"].as(JsonArray) do
810 lines
.add e
.to_s
#.replace("\n", "\\n")
813 mdoc
.content
.add_all
(lines
)
814 mdoc
.original_mentity
= mentity