neo: correct the documentation of neo.nit according to PR #781.
[nit.git] / src / neo.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Save and load a `Model` to/from a Neo4j graph.
16 #
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.
20 #
21 # SEE: `Neo4jClient`
22 #
23 # NeoNodes can also be translated back to MEntities to rebuild a Nit `Model`.
24 #
25 # Structure of the nit `Model` in the graph:
26 #
27 # Note : Any null or empty attribute will not be saved in the database.
28 #
29 # For any `MEntity` (in addition to specific data):
30 #
31 # * labels: model name (`model_name`) and `MEntity`.
32 # * `name`: short (unqualified) name.
33 # * `mdoc`: JSON array representing the associated Markdown documentation
34 # (one item by line).
35 #
36 # Note : All nodes described here are MEntities.
37 #
38 # `MProject`
39 #
40 # * labels: `MProject`, `model_name` and `MEntity`.
41 # * `(:MProject)-[:ROOT]->(:MGroup)`: root of the group tree.
42 #
43 # `MGroup`
44 #
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
49 # group.
50 # * `(:MGroup)-[:DECLARES]->(:MModule)`: modules that are direct children of
51 # this group.
52 # * `(:MGroup)-[:NESTS]->(:MGroup)`: nested groups that are direct children of
53 # this group.
54 #
55 # `MModule`
56 #
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
62 # module.
63 # * `(:MModule)-[:DEFINES]->(:MClassDef)`: all class definitons contained in
64 # this module.
65 #
66 # `MClass`
67 #
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`
75 #
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`.
80 #
81 # `MClassDef`
82 #
83 # * labels: `MClassDef`, `model_name` and `MEntity`.
84 # * `is_intro`: Does this definition introduce the class?
85 # * `location`: origin of the definition. SEE: `Location.to_s`
86 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
87 # classdef.
88 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
89 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
90 # the classdef.
91 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
92 # classdef (introductions and redefinitions).
93 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
94 #
95 # `MProperty`
96 #
97 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
98 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
99 # entity.
100 # * `full_name`: fully qualified name.
101 # * `visibility`: visibility of the property.
102 # * `is_init`: Indicates if the property is a constructor. Exists only if the
103 # node is a `MMethod`.
104 # * `(:MProperty)-[:INTRO_CLASSDEF]->(:MClassDef)`: classdef that introduces
105 # the property.
106 #
107 # `MPropDef`
108 #
109 # * labels: `MPropDef`, `model_name` and `MEntity`. Must also have `MMethodDef`,
110 # `MAttributeDef` or `MVirtualTypeDef`, depending on the class of the
111 # represented entity.
112 # * `is_intro`: Does this definition introduce the property?
113 # * `location`: origin of the definition. SEE: `Location.to_s`.
114 # * `(:MPropDef)-[:DEFINES]->(:MProperty)`: associated property.
115 #
116 # Additional attributes and relationship for `MMethodDef`:
117 #
118 # * `is_abstract`: Is the method definition abstract?
119 # * `is_intern`: Is the method definition intern?
120 # * `is_extern`: Is the method definition extern?
121 # * `(:MMethodDef)-[:SIGNATURE]->(:MSignature)`: signature attached to the
122 # property definition.
123 #
124 # Additional relationship for `MVirtualTypeDef`:
125 #
126 # * `(:MVirtualTypeDef)-[:BOUND]->(:MType)`: type to which the virtual type
127 # is bound in this definition. Exists only if this definition bound the virtual
128 # type to an effective type.
129 #
130 # `MType`
131 #
132 # * labels: `MType`, `model_name` and `MEntity`. Must also have `MClassType`,
133 # `MNullableType`, `MVirtualType` or `MSignature`, depending on the class of
134 # the represented entity.
135 #
136 # Additional label and relationships for `MClassType`:
137 #
138 # * If it is a `MGenericType`, also has the `MGenericType` label.
139 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
140 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
141 #
142 # Arguments are named following the `parameter_names` attribute of the
143 # `MClass` referred by `CLASS`.
144 #
145 # Additional relationship for `MVirtualType`:
146 #
147 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
148 # determines the type (usually a `MVirtualTypeProp`).
149 #
150 # Additional attribute and relationship for `MParameterType`:
151 #
152 # * `rank`: position of the parameter (0 for the first parameter).
153 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
154 # belong.
155 #
156 # Additional relationship for `MNullableType`:
157 #
158 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
159 #
160 # Additional attribute and relationships for `MSignature`:
161 #
162 # * `parameter_names`: JSON array representing the list of the parameter names.
163 # * `(:MSignature)-[:PARAMETER]->(:MParameter)`: parameters.
164 # * `(:MSignature)-[:RETURNTYPE]->(:MType)`: return type. Does not exist for
165 # procedures.
166 #
167 # In order to maintain the correct parameters order, each `MSignature` node
168 # contains an array of parameter names corresponding to the parameter order in
169 # the signature.
170 #
171 # For example, if the source code contains:
172 #
173 # fun foo(a: A, b: B, c: C)
174 #
175 # The `MSignature` node will contain a property
176 # `parameter_names = ["a", "b", "c"]` so the MSignature can be reconstructed
177 # with the parameters in the correct order.
178 #
179 # `MParameter`
180 #
181 # * labels: `MParameter`, `model_name` and `MEntity`.
182 # * `is_vararg`: Is the parameter a vararg?
183 # * `rank`: position of the parameter (0 for the first parameter).
184 # * `(:MParameter)-[:TYPE]->(:MType)`: static type of the parameter.
185 #
186 # MParameters are also ranked by their position in the corresponding signature.
187 # Rank 0 for the first parameter, 1 for the next one and etc.
188 module neo
189
190 import model
191 import neo4j
192 import toolcontext
193
194 # Helper class that can save and load a `Model` into a Neo4j database.
195 class NeoModel
196
197 # The model name.
198 #
199 # Because we use only one Neo4j instance to store all the models,
200 # we need to mark their appartenance to a particular model and avoid loading all models.
201 #
202 # The name is used as a Neo label on each created nodes and used to load nodes from base.
203 var model_name: String
204
205 # The toolcontext used to init the `NeoModel` tool.
206 var toolcontext: ToolContext
207
208 # The Neo4j `client` used to communicate with the Neo4j instance.
209 var client: Neo4jClient
210
211 # Fill `model` using base pointed by `client`.
212 fun load(model: Model): Model do
213 toolcontext.info("Locate all mentities...", 1)
214 var nodes = client.nodes_with_label(model_name)
215
216 toolcontext.info("Preload nodes...", 1)
217 pull_all_nodes(nodes)
218 toolcontext.info("Preload edges...", 1)
219 pull_all_edges(nodes)
220
221 toolcontext.info("Build model...", 1)
222 nodes = client.nodes_with_labels([model_name, "MProject"])
223 for node in nodes do to_mproject(model, node)
224 nodes = client.nodes_with_labels([model_name, "MGroup"])
225 for node in nodes do to_mgroup(model, node)
226 nodes = client.nodes_with_labels([model_name, "MModule"])
227 for node in nodes do to_mmodule(model, node)
228 nodes = client.nodes_with_labels([model_name, "MClass"])
229 for node in nodes do to_mclass(model, node)
230 nodes = client.nodes_with_labels([model_name, "MClassDef"])
231 for node in nodes do to_mclassdef(model, node)
232 nodes = client.nodes_with_labels([model_name, "MProperty"])
233 for node in nodes do to_mproperty(model, node)
234 nodes = client.nodes_with_labels([model_name, "MPropDef"])
235 for node in nodes do to_mpropdef(model, node)
236 return model
237 end
238
239 # Save `model` in the base pointed by `client`.
240 fun save(model: Model) do
241 var nodes = collect_model_nodes(model)
242 toolcontext.info("Save {nodes.length} nodes...", 1)
243 push_all(nodes)
244 var edges = collect_model_edges(model)
245 toolcontext.info("Save {edges.length} edges...", 1)
246 push_all(edges)
247 end
248
249 # Save `neo_entities` in base using batch mode.
250 private fun push_all(neo_entities: Collection[NeoEntity]) do
251 var batch = new NeoBatch(client)
252 var len = neo_entities.length
253 var sum = 0
254 var i = 1
255 for nentity in neo_entities do
256 batch.save_entity(nentity)
257 if i == batch_max_size then
258 do_batch(batch)
259 sum += batch_max_size
260 toolcontext.info(" {sum * 100 / len}% done", 1)
261 batch = new NeoBatch(client)
262 i = 1
263 else
264 i += 1
265 end
266 end
267 do_batch(batch)
268 end
269
270 # Load content for all `nodes` from base.
271 #
272 # Content corresponds to properties and labels that are loaded in batch mode.
273 private fun pull_all_nodes(nodes: Collection[NeoNode]) do
274 var batch = new NeoBatch(client)
275 var len = nodes.length
276 var sum = 0
277 var i = 1
278 for node in nodes do
279 batch.load_node(node)
280 if i == batch_max_size then
281 do_batch(batch)
282 sum += batch_max_size
283 toolcontext.info(" {sum * 100 / len}% done", 1)
284 batch = new NeoBatch(client)
285 i = 1
286 else
287 i += 1
288 end
289 end
290 do_batch(batch)
291 end
292
293 # Load all edges from base linked to `nodes`.
294 #
295 # Edges are loaded in batch mode.
296 private fun pull_all_edges(nodes: Collection[NeoNode]) do
297 var batch = new NeoBatch(client)
298 var len = nodes.length
299 var sum = 0
300 var i = 1
301 for node in nodes do
302 batch.load_node_edges(node)
303 if i == batch_max_size then
304 do_batch(batch)
305 sum += batch_max_size
306 toolcontext.info(" {sum * 100 / len}% done", 1)
307 batch = new NeoBatch(client)
308 i = 1
309 else
310 i += 1
311 end
312 end
313 do_batch(batch)
314 end
315
316 # How many operation can be executed in one batch?
317 private var batch_max_size = 1000
318
319 # Execute `batch` and check for errors.
320 #
321 # Abort if `batch.execute` returns errors.
322 private fun do_batch(batch: NeoBatch) do
323 var errors = batch.execute
324 if not errors.is_empty then
325 print errors
326 exit(1)
327 end
328 end
329
330 # Collect all nodes from the current `model`.
331 private fun collect_model_nodes(model: Model): Collection[NeoNode] do
332 for mproject in model.mprojects do
333 to_node(mproject)
334 for mgroup in mproject.mgroups do to_node(mgroup)
335 end
336 return nodes.values
337 end
338
339 # Collect all edges from the current `model`.
340 #
341 # Actually collect all out_edges from all nodes.
342 private fun collect_model_edges(model: Model): Collection[NeoEdge] do
343 var edges = new HashSet[NeoEdge]
344 for node in nodes.values do edges.add_all(node.out_edges)
345 return edges
346 end
347
348 # Mentities associated to nodes.
349 private var mentities = new HashMap[NeoNode, MEntity]
350
351 # Nodes associated with MEntities.
352 private var nodes = new HashMap[MEntity, NeoNode]
353
354 # Get the `NeoNode` associated with `mentity`.
355 # `mentities` are stored locally to avoid duplication.
356 fun to_node(mentity: MEntity): NeoNode do
357 if nodes.has_key(mentity) then return nodes[mentity]
358 if mentity isa MProject then return mproject_node(mentity)
359 if mentity isa MGroup then return mgroup_node(mentity)
360 if mentity isa MModule then return mmodule_node(mentity)
361 if mentity isa MClass then return mclass_node(mentity)
362 if mentity isa MClassDef then return mclassdef_node(mentity)
363 if mentity isa MProperty then return mproperty_node(mentity)
364 if mentity isa MPropDef then return mpropdef_node(mentity)
365 if mentity isa MType then return mtype_node(mentity)
366 if mentity isa MParameter then return mparameter_node(mentity)
367 abort
368 end
369
370 # Make a new `NeoNode` based on `mentity`.
371 private fun make_node(mentity: MEntity): NeoNode do
372 var node = new NeoNode
373 nodes[mentity] = node
374 node.labels.add "MEntity"
375 node.labels.add model_name
376 node["name"] = mentity.name
377 if mentity.mdoc != null then node["mdoc"] = new JsonArray.from(mentity.mdoc.content)
378 return node
379 end
380
381 # Build a `NeoNode` representing `mproject`.
382 private fun mproject_node(mproject: MProject): NeoNode do
383 var node = make_node(mproject)
384 node.labels.add "MProject"
385 var root = mproject.root
386 if root != null then
387 node.out_edges.add(new NeoEdge(node, "ROOT", to_node(root)))
388 end
389 return node
390 end
391
392 # Build a new `MProject` from a `node`.
393 #
394 # REQUIRE `node.labels.has("MProject")`
395 private fun to_mproject(model: Model, node: NeoNode): MProject do
396 if mentities.has_key(node) then return mentities[node].as(MProject)
397 assert node.labels.has("MProject")
398 var mproject = new MProject(node["name"].to_s, model)
399 mentities[node] = mproject
400 set_doc(node, mproject)
401 mproject.root = to_mgroup(model, node.out_nodes("ROOT").first)
402 return mproject
403 end
404
405 # Build a `NeoNode` representing `mgroup`.
406 private fun mgroup_node(mgroup: MGroup): NeoNode do
407 var node = make_node(mgroup)
408 node.labels.add "MGroup"
409 node["full_name"] = mgroup.full_name
410 var parent = mgroup.parent
411 node.out_edges.add(new NeoEdge(node, "PROJECT", to_node(mgroup.mproject)))
412 if parent != null then
413 node.out_edges.add(new NeoEdge(node, "PARENT", to_node(parent)))
414 end
415 for mmodule in mgroup.mmodules do
416 node.out_edges.add(new NeoEdge(node, "DECLARES", to_node(mmodule)))
417 end
418 for subgroup in mgroup.in_nesting.direct_smallers do
419 node.in_edges.add(new NeoEdge(node, "NESTS", to_node(subgroup)))
420 end
421 return node
422 end
423
424 # Build a new `MGroup` from a `node`.
425 #
426 # REQUIRE `node.labels.has("MGroup")`
427 private fun to_mgroup(model: Model, node: NeoNode): MGroup do
428 if mentities.has_key(node) then return mentities[node].as(MGroup)
429 assert node.labels.has("MGroup")
430 var mproject = to_mproject(model, node.out_nodes("PROJECT").first)
431 var parent: nullable MGroup = null
432 var out = node.out_nodes("PARENT")
433 if not out.is_empty then
434 parent = to_mgroup(model, out.first)
435 end
436 var mgroup = new MGroup(node["name"].to_s, mproject, parent)
437 mentities[node] = mgroup
438 set_doc(node, mgroup)
439 return mgroup
440 end
441
442 # Build a `NeoNode` representing `mmodule`.
443 private fun mmodule_node(mmodule: MModule): NeoNode do
444 var node = make_node(mmodule)
445 node.labels.add "MModule"
446 node["full_name"] = mmodule.full_name
447 node["location"] = mmodule.location.to_s
448 for parent in mmodule.in_importation.direct_greaters do
449 node.out_edges.add(new NeoEdge(node, "IMPORTS", to_node(parent)))
450 end
451 for mclass in mmodule.intro_mclasses do
452 node.out_edges.add(new NeoEdge(node, "INTRODUCES", to_node(mclass)))
453 end
454 for mclassdef in mmodule.mclassdefs do
455 node.out_edges.add(new NeoEdge(node, "DEFINES", to_node(mclassdef)))
456 end
457 return node
458 end
459
460 # Build a new `MModule` from a `node`.
461 #
462 # REQUIRE `node.labels.has("MModule")`
463 private fun to_mmodule(model: Model, node: NeoNode): MModule do
464 if mentities.has_key(node) then return mentities[node].as(MModule)
465 assert node.labels.has("MModule")
466 var ins = node.in_nodes("DECLARES")
467 var mgroup: nullable MGroup = null
468 if not ins.is_empty then
469 mgroup = to_mgroup(model, ins.first)
470 end
471 var name = node["name"].to_s
472 var location = to_location(node["location"].to_s)
473 var mmodule = new MModule(model, mgroup, name, location)
474 mentities[node] = mmodule
475 set_doc(node, mmodule)
476 var imported_mmodules = new Array[MModule]
477 for smod in node.out_nodes("IMPORTS") do
478 imported_mmodules.add to_mmodule(model, smod)
479 end
480 mmodule.set_imported_mmodules(imported_mmodules)
481 return mmodule
482 end
483
484 # Build a `NeoNode` representing `mclass`.
485 private fun mclass_node(mclass: MClass): NeoNode do
486 var node = make_node(mclass)
487 node.labels.add "MClass"
488 node["full_name"] = mclass.full_name
489 node["kind"] = mclass.kind.to_s
490 node["visibility"] = mclass.visibility.to_s
491 if not mclass.mparameters.is_empty then
492 var parameter_names = new Array[String]
493 for p in mclass.mparameters do parameter_names.add(p.name)
494 node["parameter_names"] = new JsonArray.from(parameter_names)
495 end
496 node.out_edges.add(new NeoEdge(node, "CLASSTYPE", to_node(mclass.mclass_type)))
497 return node
498 end
499
500 # Build a new `MClass` from a `node`.
501 #
502 # REQUIRE `node.labels.has("MClass")`
503 private fun to_mclass(model: Model, node: NeoNode): MClass do
504 if mentities.has_key(node) then return mentities[node].as(MClass)
505 assert node.labels.has("MClass")
506 var mmodule = to_mmodule(model, node.in_nodes("INTRODUCES").first)
507 var name = node["name"].to_s
508 var kind = to_kind(node["kind"].to_s)
509 var visibility = to_visibility(node["visibility"].to_s)
510 var parameter_names = new Array[String]
511 if node.has_key("parameter_names") then
512 for e in node["parameter_names"].as(JsonArray) do
513 parameter_names.add e.to_s
514 end
515 end
516 var mclass = new MClass(mmodule, name, parameter_names, kind, visibility)
517 mentities[node] = mclass
518 set_doc(node, mclass)
519 return mclass
520 end
521
522 # Build a `NeoNode` representing `mclassdef`.
523 private fun mclassdef_node(mclassdef: MClassDef): NeoNode do
524 var node = make_node(mclassdef)
525 node.labels.add "MClassDef"
526 node["is_intro"] = mclassdef.is_intro
527 node["location"] = mclassdef.location.to_s
528 node.out_edges.add(new NeoEdge(node, "BOUNDTYPE", to_node(mclassdef.bound_mtype)))
529 node.out_edges.add(new NeoEdge(node, "MCLASS", to_node(mclassdef.mclass)))
530 for mproperty in mclassdef.intro_mproperties do
531 node.out_edges.add(new NeoEdge(node, "INTRODUCES", to_node(mproperty)))
532 end
533 for mpropdef in mclassdef.mpropdefs do
534 node.out_edges.add(new NeoEdge(node, "DECLARES", to_node(mpropdef)))
535 end
536 for sup in mclassdef.supertypes do
537 node.out_edges.add(new NeoEdge(node, "INHERITS", to_node(sup)))
538 end
539 return node
540 end
541
542 # Build a new `MClassDef` from a `node`.
543 #
544 # REQUIRE `node.labels.has("MClassDef")`
545 private fun to_mclassdef(model: Model, node: NeoNode): MClassDef do
546 if mentities.has_key(node) then return mentities[node].as(MClassDef)
547 assert node.labels.has("MClassDef")
548 var mmodule = to_mmodule(model, node.in_nodes("DEFINES").first)
549 var mtype = to_mtype(model, node.out_nodes("BOUNDTYPE").first).as(MClassType)
550 var location = to_location(node["location"].to_s)
551 var mclassdef = new MClassDef(mmodule, mtype, location)
552 mentities[node] = mclassdef
553 set_doc(node, mclassdef)
554 var supertypes = new Array[MClassType]
555 for sup in node.out_nodes("INHERITS") do
556 supertypes.add to_mtype(model, sup).as(MClassType)
557 end
558 mclassdef.set_supertypes(supertypes)
559 mclassdef.add_in_hierarchy
560 return mclassdef
561 end
562
563 # Build a `NeoNode` representing `mproperty`.
564 private fun mproperty_node(mproperty: MProperty): NeoNode do
565 var node = make_node(mproperty)
566 node.labels.add "MProperty"
567 node["full_name"] = mproperty.full_name
568 node["visibility"] = mproperty.visibility.to_s
569 if mproperty isa MMethod then
570 node.labels.add "MMethod"
571 node["is_init"] = mproperty.is_init
572 else if mproperty isa MAttribute then
573 node.labels.add "MAttribute"
574 else if mproperty isa MVirtualTypeProp then
575 node.labels.add "MVirtualTypeProp"
576 end
577 node.out_edges.add(new NeoEdge(node, "INTRO_CLASSDEF", to_node(mproperty.intro_mclassdef)))
578 return node
579 end
580
581 # Build a new `MProperty` from a `node`.
582 #
583 # REQUIRE `node.labels.has("MProperty")`
584 private fun to_mproperty(model: Model, node: NeoNode): MProperty do
585 if mentities.has_key(node) then return mentities[node].as(MProperty)
586 assert node.labels.has("MProperty")
587 var intro_mclassdef = to_mclassdef(model, node.out_nodes("INTRO_CLASSDEF").first)
588 var name = node["name"].to_s
589 var visibility = to_visibility(node["visibility"].to_s)
590 var mprop: nullable MProperty = null
591 if node.labels.has("MMethod") then
592 mprop = new MMethod(intro_mclassdef, name, visibility)
593 mprop.is_init = node["is_init"].as(Bool)
594 else if node.labels.has("MAttribute") then
595 mprop = new MAttribute(intro_mclassdef, name, visibility)
596 else if node.labels.has("MVirtualTypeProp") then
597 mprop = new MVirtualTypeProp(intro_mclassdef, name, visibility)
598 end
599 if mprop == null then
600 print "not yet implemented to_mproperty for {node.labels.join(",")}"
601 abort
602 end
603 mentities[node] = mprop
604 set_doc(node, mprop)
605 for npropdef in node.in_nodes("DEFINES") do
606 var mpropdef = to_mpropdef(model, npropdef)
607 if npropdef["is_intro"].as(Bool) then
608 mprop.mpropdefs.unshift mpropdef
609 else
610 mprop.mpropdefs.add mpropdef
611 end
612 end
613 return mprop
614 end
615
616 # Build a `NeoNode` representing `mpropdef`.
617 private fun mpropdef_node(mpropdef: MPropDef): NeoNode do
618 var node = make_node(mpropdef)
619 node.labels.add "MPropDef"
620 node["is_intro"] = mpropdef.is_intro
621 node["location"] = mpropdef.location.to_s
622 node.out_edges.add(new NeoEdge(node, "DEFINES", to_node(mpropdef.mproperty)))
623 if mpropdef isa MMethodDef then
624 node.labels.add "MMethodDef"
625 node["is_abstract"] = mpropdef.is_abstract
626 node["is_intern"] = mpropdef.is_intern
627 node["is_extern"] = mpropdef.is_extern
628 var msignature = mpropdef.msignature
629 if msignature != null then
630 node.out_edges.add(new NeoEdge(node, "SIGNATURE", to_node(msignature)))
631 end
632 else if mpropdef isa MAttributeDef then
633 node.labels.add "MAttributeDef"
634 else if mpropdef isa MVirtualTypeDef then
635 node.labels.add "MVirtualTypeDef"
636 var bound = mpropdef.bound
637 if bound != null then
638 node.out_edges.add(new NeoEdge(node, "BOUND", to_node(bound)))
639 end
640 end
641 return node
642 end
643
644 # Build a new `MPropDef` from a `node`.
645 #
646 # REQUIRE `node.labels.has("MPropDef")`
647 private fun to_mpropdef(model: Model, node: NeoNode): MPropDef do
648 if mentities.has_key(node) then return mentities[node].as(MPropDef)
649 assert node.labels.has("MPropDef")
650 var mclassdef = to_mclassdef(model, node.in_nodes("DECLARES").first)
651 var mproperty = to_mproperty(model, node.out_nodes("DEFINES").first)
652 var location = to_location(node["location"].to_s)
653 var mpropdef: nullable MPropDef = null
654 if node.labels.has("MMethodDef") then
655 mpropdef = new MMethodDef(mclassdef, mproperty.as(MMethod), location)
656 mpropdef.is_abstract = node["is_abstract"].as(Bool)
657 mpropdef.is_intern = node["is_intern"].as(Bool)
658 mpropdef.is_extern = node["is_extern"].as(Bool)
659 mentities[node] = mpropdef
660 mpropdef.msignature = to_mtype(model, node.out_nodes("SIGNATURE").first).as(MSignature)
661 else if node.labels.has("MAttributeDef") then
662 mpropdef = new MAttributeDef(mclassdef, mproperty.as(MAttribute), location)
663 mentities[node] = mpropdef
664 else if node.labels.has("MVirtualTypeDef") then
665 mpropdef = new MVirtualTypeDef(mclassdef, mproperty.as(MVirtualTypeProp), location)
666 mentities[node] = mpropdef
667 var bound = node.out_nodes("BOUND")
668 if not bound.is_empty then mpropdef.bound = to_mtype(model, bound.first)
669 end
670 if mpropdef == null then
671 print "not yet implemented to_mpropdef for {node.labels.join(",")}"
672 abort
673 end
674 set_doc(node, mpropdef)
675 return mpropdef
676 end
677
678 # Build a `NeoNode` representing `mtype`.
679 private fun mtype_node(mtype: MType): NeoNode do
680 var node = make_node(mtype)
681 node.labels.add "MType"
682 if mtype isa MClassType then
683 node.labels.add "MClassType"
684 node.out_edges.add(new NeoEdge(node, "CLASS", to_node(mtype.mclass)))
685 for arg in mtype.arguments do
686 node.out_edges.add(new NeoEdge(node, "ARGUMENT", to_node(arg)))
687 end
688 if mtype isa MGenericType then
689 node.labels.add "MGenericType"
690 end
691 else if mtype isa MVirtualType then
692 node.labels.add "MVirtualType"
693 node.out_edges.add(new NeoEdge(node, "PROPERTY", to_node(mtype.mproperty)))
694 else if mtype isa MParameterType then
695 node.labels.add "MParameterType"
696 node["rank"] = mtype.rank
697 node.out_edges.add(new NeoEdge(node, "CLASS", to_node(mtype.mclass)))
698 else if mtype isa MNullableType then
699 node.labels.add "MNullableType"
700 node.out_edges.add(new NeoEdge(node, "TYPE", to_node(mtype.mtype)))
701 else if mtype isa MSignature then
702 node.labels.add "MSignature"
703 var names = new JsonArray
704 var rank = 0
705 for mparameter in mtype.mparameters do
706 names.add mparameter.name
707 var pnode = to_node(mparameter)
708 pnode["rank"] = rank
709 node.out_edges.add(new NeoEdge(node, "PARAMETER", pnode))
710 end
711 if not names.is_empty then node["parameter_names"] = names
712 var return_mtype = mtype.return_mtype
713 if return_mtype != null then
714 node.out_edges.add(new NeoEdge(node, "RETURNTYPE", to_node(return_mtype)))
715 end
716 end
717 return node
718 end
719
720 # Build a new `MType` from a `node`.
721 #
722 # REQUIRE `node.labels.has("MType")`
723 private fun to_mtype(model: Model, node: NeoNode): MType do
724 if mentities.has_key(node) then return mentities[node].as(MType)
725 assert node.labels.has("MType")
726 if node.labels.has("MClassType") then
727 var mclass = to_mclass(model, node.out_nodes("CLASS").first)
728 var args = new Array[MType]
729 for narg in node.out_nodes("ARGUMENT") do
730 args.add to_mtype(model, narg)
731 end
732 var mtype = mclass.get_mtype(args)
733 mentities[node] = mtype
734 return mtype
735 else if node.labels.has("MParameterType") then
736 var mclass = to_mclass(model, node.out_nodes("CLASS").first)
737 var rank = node["rank"].to_s.to_i
738 var mtype = mclass.mparameters[rank]
739 mentities[node] = mtype
740 return mtype
741 else if node.labels.has("MNullableType") then
742 var intype = to_mtype(model, node.out_nodes("TYPE").first)
743 var mtype = intype.as_nullable
744 mentities[node] = mtype
745 return mtype
746 else if node.labels.has("MVirtualType") then
747 var mproperty = to_mproperty(model, node.out_nodes("PROPERTY").first)
748 assert mproperty isa MVirtualTypeProp
749 var mtype = mproperty.mvirtualtype
750 mentities[node] = mtype
751 return mtype
752 else if node.labels.has("MSignature") then
753 # Get all param nodes
754 var mparam_nodes = new HashMap[String, MParameter]
755 for pnode in node.out_nodes("PARAMETER") do
756 var mparam = to_mparameter(model, pnode)
757 mparam_nodes[mparam.name] = mparam
758 end
759 # Load params in the good order
760 var mparam_names = node["parameter_names"]
761 var mparameters = new Array[MParameter]
762 if mparam_names isa JsonArray then
763 for mparam_name in mparam_names do
764 var mparam = mparam_nodes[mparam_name.to_s]
765 mparameters.add mparam
766 end
767 end
768 var return_mtype: nullable MType = null
769 var ret_nodes = node.out_nodes("RETURNTYPE")
770 if not ret_nodes.is_empty then
771 return_mtype = to_mtype(model, ret_nodes.first)
772 end
773 var mtype = new MSignature(mparameters, return_mtype)
774 mentities[node] = mtype
775 return mtype
776 end
777 print "not yet implemented to_mtype for {node.labels.join(",")}"
778 abort
779 end
780
781 # Build a `NeoNode` representing `mparameter`.
782 private fun mparameter_node(mparameter: MParameter): NeoNode do
783 var node = make_node(mparameter)
784 node.labels.add "MParameter"
785 node["name"] = mparameter.name
786 node["is_vararg"] = mparameter.is_vararg
787 node.out_edges.add(new NeoEdge(node, "TYPE", to_node(mparameter.mtype)))
788 return node
789 end
790
791 # Build a new `MParameter` from `node`.
792 #
793 # REQUIRE `node.labels.has("MParameter")`
794 private fun to_mparameter(model: Model, node: NeoNode): MParameter do
795 if mentities.has_key(node) then return mentities[node].as(MParameter)
796 assert node.labels.has("MParameter")
797 var name = node["name"].to_s
798 var mtype = to_mtype(model, node.out_nodes("TYPE").first)
799 var is_vararg = node["is_vararg"].as(Bool)
800 var mparameter = new MParameter(name, mtype, is_vararg)
801 mentities[node] = mparameter
802 return mparameter
803 end
804
805 # Get a `Location` from its string representation.
806 private fun to_location(loc: String): Location do
807 #TODO filepath
808 var parts = loc.split_with(":")
809 var file = new SourceFile.from_string(parts[0], "")
810 var pos = parts[1].split_with("--")
811 var pos1 = pos[0].split_with(",")
812 var pos2 = pos[1].split_with(",")
813 var line_s = pos1[0].to_i
814 var line_e = pos2[0].to_i
815 var column_s = pos1[1].to_i
816 var column_e = 0
817 if pos2.length == 2 then pos2[1].to_i
818 return new Location(file, line_s, line_e, column_s, column_e)
819 end
820
821 # Get a `MVisibility` from its string representation.
822 private fun to_visibility(vis: String): MVisibility do
823 if vis == intrude_visibility.to_s then
824 return intrude_visibility
825 else if vis == public_visibility.to_s then
826 return public_visibility
827 else if vis == protected_visibility.to_s then
828 return protected_visibility
829 else if vis == private_visibility.to_s then
830 return private_visibility
831 else
832 return none_visibility
833 end
834 end
835
836 # Get a `MKind` from its string representation.
837 private fun to_kind(kind: String): MClassKind do
838 if kind == abstract_kind.to_s then
839 return abstract_kind
840 else if kind == concrete_kind.to_s then
841 return concrete_kind
842 else if kind == interface_kind.to_s then
843 return interface_kind
844 else if kind == enum_kind.to_s then
845 return enum_kind
846 else if kind == extern_kind.to_s then
847 return extern_kind
848 end
849 abort
850 end
851
852 # Extract the `MDoc` from `node` and link it to `mentity`.
853 private fun set_doc(node: NeoNode, mentity: MEntity) do
854 if node.has_key("mdoc") then
855 var lines = new Array[String]
856 for e in node["mdoc"].as(JsonArray) do
857 lines.add e.to_s#.replace("\n", "\\n")
858 end
859 var mdoc = new MDoc
860 mdoc.content.add_all(lines)
861 mdoc.original_mentity = mentity
862 mentity.mdoc = mdoc
863 end
864 end
865 end