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