neo: Correct exportation of a signature.
[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 # * `location`: origin of the definition. SEE: `Location.to_s`
85 # * `(:MClassDef)-[:BOUNDTYPE]->(:MClassType)`: bounded type associated to the
86 # classdef.
87 # * `(:MClassDef)-[:MCLASS]->(:MClass)`: associated `MClass`.
88 # * `(:MClassDef)-[:INTRODUCES]->(:MProperty)`: all properties introduced by
89 # the classdef.
90 # * `(:MClassDef)-[:DECLARES]->(:MPropDef)`: all property definitions in the
91 # classdef (introductions and redefinitions).
92 # * `(:MClassDef)-[:INHERITS]->(:MClassType)`: all declared super-types
93 #
94 # `MProperty`
95 #
96 # * labels: `MProperty`, `model_name` and `MEntity`. Must also have `MMethod`,
97 # `MAttribute` or `MVirtualTypeProp`, depending on the class of the represented
98 # entity.
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
104 # the property.
105 #
106 # `MPropDef`
107 #
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.
113 #
114 # Additional attributes and relationship for `MMethodDef`:
115 #
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.
121 #
122 # Additional relationship for `MAttributeDef`:
123 #
124 # * `(:MAttributeDef)-[:TYPE]->(:MType)`: static type of the attribute,
125 # if specified.
126 #
127 # Additional relationship for `MVirtualTypeDef`:
128 #
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.
132 #
133 # `MType`
134 #
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.
138 #
139 # Additional label and relationships for `MClassType`:
140 #
141 # * If it is a `MGenericType`, also has the `MGenericType` label.
142 # * `(:MClassType)-[:CLASS]->(:MClass)`: SEE: `MClassType.mclass`
143 # * `(:MClassType)-[:ARGUMENT]->(:MType)`: type arguments.
144 #
145 # Arguments are named following the `parameter_names` attribute of the
146 # `MClass` referred by `CLASS`.
147 #
148 # Additional relationship for `MVirtualType`:
149 #
150 # * `(:MVirtualType)-[:PROPERTY]->(:MProperty)`: associated property that
151 # determines the type (usually a `MVirtualTypeProp`).
152 #
153 # Additional attribute and relationship for `MParameterType`:
154 #
155 # * `rank`: position of the parameter (0 for the first parameter).
156 # * `(:MParameterType)-[:CLASS]->(:MClass)`: generic class where the parameter
157 # belong.
158 #
159 # Additional relationship for `MNullableType`:
160 #
161 # * `(:MNullableType)-[:TYPE]->(:MType)`: base type of the nullable type.
162 #
163 # Additional attribute and relationships for `MSignature`:
164 #
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
168 # procedures.
169 #
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
172 # the signature.
173 #
174 # For example, if the source code contains:
175 #
176 # fun foo(a: A, b: B, c: C)
177 #
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.
181 #
182 # `MParameter`
183 #
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.
188 #
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.
191 module neo
192
193 import model
194 import neo4j
195 import toolcontext
196
197 # Helper class that can save and load a `Model` into a Neo4j database.
198 class NeoModel
199
200 # The model name.
201 #
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.
204 #
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
207
208 # The toolcontext used to init the `NeoModel` tool.
209 var toolcontext: ToolContext
210
211 # The Neo4j `client` used to communicate with the Neo4j instance.
212 var client: Neo4jClient
213
214 # Fill `model` using base pointed by `client`.
215 fun load(model: Model): Model do
216 toolcontext.info("Locate all mentities...", 1)
217 var nodes = client.nodes_with_label(model_name)
218
219 toolcontext.info("Preload nodes...", 1)
220 pull_all_nodes(nodes)
221 toolcontext.info("Preload edges...", 1)
222 pull_all_edges(nodes)
223
224 toolcontext.info("Build model...", 1)
225 nodes = client.nodes_with_labels([model_name, "MProject"])
226 for node in nodes do to_mproject(model, node)
227 nodes = client.nodes_with_labels([model_name, "MGroup"])
228 for node in nodes do to_mgroup(model, node)
229 nodes = client.nodes_with_labels([model_name, "MModule"])
230 for node in nodes do to_mmodule(model, node)
231 nodes = client.nodes_with_labels([model_name, "MClass"])
232 for node in nodes do to_mclass(model, node)
233 nodes = client.nodes_with_labels([model_name, "MClassDef"])
234 for node in nodes do to_mclassdef(model, node)
235 nodes = client.nodes_with_labels([model_name, "MProperty"])
236 for node in nodes do to_mproperty(model, node)
237 nodes = client.nodes_with_labels([model_name, "MPropDef"])
238 for node in nodes do to_mpropdef(model, node)
239 return model
240 end
241
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)
246 push_all(nodes)
247 var edges = collect_model_edges(model)
248 toolcontext.info("Save {edges.length} edges...", 1)
249 push_all(edges)
250 end
251
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
256 var sum = 0
257 var i = 1
258 for nentity in neo_entities do
259 batch.save_entity(nentity)
260 if i == batch_max_size then
261 do_batch(batch)
262 sum += batch_max_size
263 toolcontext.info(" {sum * 100 / len}% done", 1)
264 batch = new NeoBatch(client)
265 i = 1
266 else
267 i += 1
268 end
269 end
270 do_batch(batch)
271 end
272
273 # Load content for all `nodes` from base.
274 #
275 # Content corresponds to properties and labels that are loaded in batch mode.
276 private fun pull_all_nodes(nodes: Collection[NeoNode]) do
277 var batch = new NeoBatch(client)
278 var len = nodes.length
279 var sum = 0
280 var i = 1
281 for node in nodes do
282 batch.load_node(node)
283 if i == batch_max_size then
284 do_batch(batch)
285 sum += batch_max_size
286 toolcontext.info(" {sum * 100 / len}% done", 1)
287 batch = new NeoBatch(client)
288 i = 1
289 else
290 i += 1
291 end
292 end
293 do_batch(batch)
294 end
295
296 # Load all edges from base linked to `nodes`.
297 #
298 # Edges are loaded in batch mode.
299 private fun pull_all_edges(nodes: Collection[NeoNode]) do
300 var batch = new NeoBatch(client)
301 var len = nodes.length
302 var sum = 0
303 var i = 1
304 for node in nodes do
305 batch.load_node_edges(node)
306 if i == batch_max_size then
307 do_batch(batch)
308 sum += batch_max_size
309 toolcontext.info(" {sum * 100 / len}% done", 1)
310 batch = new NeoBatch(client)
311 i = 1
312 else
313 i += 1
314 end
315 end
316 do_batch(batch)
317 end
318
319 # How many operation can be executed in one batch?
320 private var batch_max_size = 1000
321
322 # Execute `batch` and check for errors.
323 #
324 # Abort if `batch.execute` returns errors.
325 private fun do_batch(batch: NeoBatch) do
326 var errors = batch.execute
327 if not errors.is_empty then
328 print errors
329 exit(1)
330 end
331 end
332
333 # Collect all nodes from the current `model`.
334 private fun collect_model_nodes(model: Model): Collection[NeoNode] do
335 for mproject in model.mprojects do
336 to_node(mproject)
337 for mgroup in mproject.mgroups do to_node(mgroup)
338 end
339 return nodes.values
340 end
341
342 # Collect all edges from the current `model`.
343 #
344 # Actually collect all out_edges from all nodes.
345 private fun collect_model_edges(model: Model): Collection[NeoEdge] do
346 var edges = new HashSet[NeoEdge]
347 for node in nodes.values do edges.add_all(node.out_edges)
348 return edges
349 end
350
351 # Mentities associated to nodes.
352 private var mentities = new HashMap[NeoNode, MEntity]
353
354 # Nodes associated with MEntities.
355 private var nodes = new HashMap[MEntity, NeoNode]
356
357 # Get the `NeoNode` associated with `mentity`.
358 # `mentities` are stored locally to avoid duplication.
359 fun to_node(mentity: MEntity): NeoNode do
360 if nodes.has_key(mentity) then return nodes[mentity]
361 if mentity isa MProject then return mproject_node(mentity)
362 if mentity isa MGroup then return mgroup_node(mentity)
363 if mentity isa MModule then return mmodule_node(mentity)
364 if mentity isa MClass then return mclass_node(mentity)
365 if mentity isa MClassDef then return mclassdef_node(mentity)
366 if mentity isa MProperty then return mproperty_node(mentity)
367 if mentity isa MPropDef then return mpropdef_node(mentity)
368 if mentity isa MType then return mtype_node(mentity)
369 if mentity isa MParameter then return mparameter_node(mentity)
370 abort
371 end
372
373 # Make a new `NeoNode` based on `mentity`.
374 private fun make_node(mentity: MEntity): NeoNode do
375 var node = new NeoNode
376 nodes[mentity] = node
377 node.labels.add "MEntity"
378 node.labels.add model_name
379 node["name"] = mentity.name
380 if mentity.mdoc != null then node["mdoc"] = new JsonArray.from(mentity.mdoc.content)
381 return node
382 end
383
384 # Build a `NeoNode` representing `mproject`.
385 private fun mproject_node(mproject: MProject): NeoNode do
386 var node = make_node(mproject)
387 node.labels.add "MProject"
388 var root = mproject.root
389 if root != null then
390 node.out_edges.add(new NeoEdge(node, "ROOT", to_node(root)))
391 end
392 return node
393 end
394
395 # Build a new `MProject` from a `node`.
396 #
397 # REQUIRE `node.labels.has("MProject")`
398 private fun to_mproject(model: Model, node: NeoNode): MProject do
399 if mentities.has_key(node) then return mentities[node].as(MProject)
400 assert node.labels.has("MProject")
401 var mproject = new MProject(node["name"].to_s, model)
402 mentities[node] = mproject
403 set_doc(node, mproject)
404 mproject.root = to_mgroup(model, node.out_nodes("ROOT").first)
405 return mproject
406 end
407
408 # Build a `NeoNode` representing `mgroup`.
409 private fun mgroup_node(mgroup: MGroup): NeoNode do
410 var node = make_node(mgroup)
411 node.labels.add "MGroup"
412 node["full_name"] = mgroup.full_name
413 var parent = mgroup.parent
414 node.out_edges.add(new NeoEdge(node, "PROJECT", to_node(mgroup.mproject)))
415 if parent != null then
416 node.out_edges.add(new NeoEdge(node, "PARENT", to_node(parent)))
417 end
418 for mmodule in mgroup.mmodules do
419 node.out_edges.add(new NeoEdge(node, "DECLARES", to_node(mmodule)))
420 end
421 for subgroup in mgroup.in_nesting.direct_smallers do
422 node.in_edges.add(new NeoEdge(node, "NESTS", to_node(subgroup)))
423 end
424 return node
425 end
426
427 # Build a new `MGroup` from a `node`.
428 #
429 # REQUIRE `node.labels.has("MGroup")`
430 private fun to_mgroup(model: Model, node: NeoNode): MGroup do
431 if mentities.has_key(node) then return mentities[node].as(MGroup)
432 assert node.labels.has("MGroup")
433 var mproject = to_mproject(model, node.out_nodes("PROJECT").first)
434 var parent: nullable MGroup = null
435 var out = node.out_nodes("PARENT")
436 if not out.is_empty then
437 parent = to_mgroup(model, out.first)
438 end
439 var mgroup = new MGroup(node["name"].to_s, mproject, parent)
440 mentities[node] = mgroup
441 set_doc(node, mgroup)
442 return mgroup
443 end
444
445 # Build a `NeoNode` representing `mmodule`.
446 private fun mmodule_node(mmodule: MModule): NeoNode do
447 var node = make_node(mmodule)
448 node.labels.add "MModule"
449 node["full_name"] = mmodule.full_name
450 node["location"] = mmodule.location.to_s
451 for parent in mmodule.in_importation.direct_greaters do
452 node.out_edges.add(new NeoEdge(node, "IMPORTS", to_node(parent)))
453 end
454 for mclass in mmodule.intro_mclasses do
455 node.out_edges.add(new NeoEdge(node, "INTRODUCES", to_node(mclass)))
456 end
457 for mclassdef in mmodule.mclassdefs do
458 node.out_edges.add(new NeoEdge(node, "DEFINES", to_node(mclassdef)))
459 end
460 return node
461 end
462
463 # Build a new `MModule` from a `node`.
464 #
465 # REQUIRE `node.labels.has("MModule")`
466 private fun to_mmodule(model: Model, node: NeoNode): MModule do
467 if mentities.has_key(node) then return mentities[node].as(MModule)
468 assert node.labels.has("MModule")
469 var ins = node.in_nodes("DECLARES")
470 var mgroup: nullable MGroup = null
471 if not ins.is_empty then
472 mgroup = to_mgroup(model, ins.first)
473 end
474 var name = node["name"].to_s
475 var location = to_location(node["location"].to_s)
476 var mmodule = new MModule(model, mgroup, name, location)
477 mentities[node] = mmodule
478 set_doc(node, mmodule)
479 var imported_mmodules = new Array[MModule]
480 for smod in node.out_nodes("IMPORTS") do
481 imported_mmodules.add to_mmodule(model, smod)
482 end
483 mmodule.set_imported_mmodules(imported_mmodules)
484 return mmodule
485 end
486
487 # Build a `NeoNode` representing `mclass`.
488 private fun mclass_node(mclass: MClass): NeoNode do
489 var node = make_node(mclass)
490 node.labels.add "MClass"
491 node["full_name"] = mclass.full_name
492 node["kind"] = mclass.kind.to_s
493 node["visibility"] = mclass.visibility.to_s
494 if not mclass.mparameters.is_empty then
495 var parameter_names = new Array[String]
496 for p in mclass.mparameters do parameter_names.add(p.name)
497 node["parameter_names"] = new JsonArray.from(parameter_names)
498 end
499 node.out_edges.add(new NeoEdge(node, "CLASSTYPE", to_node(mclass.mclass_type)))
500 return node
501 end
502
503 # Build a new `MClass` from a `node`.
504 #
505 # REQUIRE `node.labels.has("MClass")`
506 private fun to_mclass(model: Model, node: NeoNode): MClass do
507 if mentities.has_key(node) then return mentities[node].as(MClass)
508 assert node.labels.has("MClass")
509 var mmodule = to_mmodule(model, node.in_nodes("INTRODUCES").first)
510 var name = node["name"].to_s
511 var kind = to_kind(node["kind"].to_s)
512 var visibility = to_visibility(node["visibility"].to_s)
513 var parameter_names = new Array[String]
514 if node.has_key("parameter_names") then
515 for e in node["parameter_names"].as(JsonArray) do
516 parameter_names.add e.to_s
517 end
518 end
519 var mclass = new MClass(mmodule, name, parameter_names, kind, visibility)
520 mentities[node] = mclass
521 set_doc(node, mclass)
522 return mclass
523 end
524
525 # Build a `NeoNode` representing `mclassdef`.
526 private fun mclassdef_node(mclassdef: MClassDef): NeoNode do
527 var node = make_node(mclassdef)
528 node.labels.add "MClassDef"
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)))
534 end
535 for mpropdef in mclassdef.mpropdefs do
536 node.out_edges.add(new NeoEdge(node, "DECLARES", to_node(mpropdef)))
537 end
538 for sup in mclassdef.supertypes do
539 node.out_edges.add(new NeoEdge(node, "INHERITS", to_node(sup)))
540 end
541 return node
542 end
543
544 # Build a new `MClassDef` from a `node`.
545 #
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)
559 end
560 mclassdef.set_supertypes(supertypes)
561 mclassdef.add_in_hierarchy
562 return mclassdef
563 end
564
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"
578 end
579 node.out_edges.add(new NeoEdge(node, "INTRO_CLASSDEF", to_node(mproperty.intro_mclassdef)))
580 return node
581 end
582
583 # Build a new `MProperty` from a `node`.
584 #
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)
600 end
601 if mprop == null then
602 print "not yet implemented to_mproperty for {node.labels.join(",")}"
603 abort
604 end
605 mentities[node] = mprop
606 set_doc(node, mprop)
607 return mprop
608 end
609
610 # Build a `NeoNode` representing `mpropdef`.
611 private fun mpropdef_node(mpropdef: MPropDef): NeoNode do
612 var node = make_node(mpropdef)
613 node.labels.add "MPropDef"
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)))
624 end
625 else if mpropdef isa MAttributeDef then
626 node.labels.add "MAttributeDef"
627 var static_mtype = mpropdef.static_mtype
628 if static_mtype != null then
629 node.out_edges.add(new NeoEdge(node, "TYPE", to_node(static_mtype)))
630 end
631 else if mpropdef isa MVirtualTypeDef then
632 node.labels.add "MVirtualTypeDef"
633 var bound = mpropdef.bound
634 if bound != null then
635 node.out_edges.add(new NeoEdge(node, "BOUND", to_node(bound)))
636 end
637 end
638 return node
639 end
640
641 # Build a new `MPropDef` from a `node`.
642 #
643 # REQUIRE `node.labels.has("MPropDef")`
644 private fun to_mpropdef(model: Model, node: NeoNode): MPropDef do
645 if mentities.has_key(node) then return mentities[node].as(MPropDef)
646 assert node.labels.has("MPropDef")
647 var mclassdef = to_mclassdef(model, node.in_nodes("DECLARES").first)
648 var mproperty = to_mproperty(model, node.out_nodes("DEFINES").first)
649 var location = to_location(node["location"].to_s)
650 var mpropdef: nullable MPropDef = null
651 if node.labels.has("MMethodDef") then
652 mpropdef = new MMethodDef(mclassdef, mproperty.as(MMethod), location)
653 mpropdef.is_abstract = node["is_abstract"].as(Bool)
654 mpropdef.is_intern = node["is_intern"].as(Bool)
655 mpropdef.is_extern = node["is_extern"].as(Bool)
656 mentities[node] = mpropdef
657 mpropdef.msignature = to_mtype(model, node.out_nodes("SIGNATURE").first).as(MSignature)
658 else if node.labels.has("MAttributeDef") then
659 mpropdef = new MAttributeDef(mclassdef, mproperty.as(MAttribute), location)
660 mentities[node] = mpropdef
661 var static_mtype = node.out_nodes("TYPE")
662 if not static_mtype.is_empty then mpropdef.static_mtype = to_mtype(model, static_mtype.first)
663 else if node.labels.has("MVirtualTypeDef") then
664 mpropdef = new MVirtualTypeDef(mclassdef, mproperty.as(MVirtualTypeProp), location)
665 mentities[node] = mpropdef
666 var bound = node.out_nodes("BOUND")
667 if not bound.is_empty then mpropdef.bound = to_mtype(model, bound.first)
668 end
669 if mpropdef == null then
670 print "not yet implemented to_mpropdef for {node.labels.join(",")}"
671 abort
672 end
673 set_doc(node, mpropdef)
674 return mpropdef
675 end
676
677 # Build a `NeoNode` representing `mtype`.
678 private fun mtype_node(mtype: MType): NeoNode do
679 var node = make_node(mtype)
680 node.labels.add "MType"
681 if mtype isa MClassType then
682 node.labels.add "MClassType"
683 node.out_edges.add(new NeoEdge(node, "CLASS", to_node(mtype.mclass)))
684 for arg in mtype.arguments do
685 node.out_edges.add(new NeoEdge(node, "ARGUMENT", to_node(arg)))
686 end
687 if mtype isa MGenericType then
688 node.labels.add "MGenericType"
689 end
690 else if mtype isa MVirtualType then
691 node.labels.add "MVirtualType"
692 node.out_edges.add(new NeoEdge(node, "PROPERTY", to_node(mtype.mproperty)))
693 else if mtype isa MParameterType then
694 node.labels.add "MParameterType"
695 node["rank"] = mtype.rank
696 node.out_edges.add(new NeoEdge(node, "CLASS", to_node(mtype.mclass)))
697 else if mtype isa MNullableType then
698 node.labels.add "MNullableType"
699 node.out_edges.add(new NeoEdge(node, "TYPE", to_node(mtype.mtype)))
700 else if mtype isa MSignature then
701 node.labels.add "MSignature"
702 var names = new JsonArray
703 var rank = 0
704 for mparameter in mtype.mparameters do
705 names.add mparameter.name
706 var pnode = mparameter_node(mparameter)
707 pnode["rank"] = rank
708 node.out_edges.add(new NeoEdge(node, "PARAMETER", pnode))
709 rank += 1
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