modelbuilder: node is optionnal in Modelbuilder::force_get_primitive_method
[nit.git] / src / modelbuilder.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Load nit source files and build the associated model
18 #
19 # FIXME better doc
20 #
21 # FIXME split this module into submodules
22 # FIXME add missing error checks
23 module modelbuilder
24
25 import model
26 import phase
27
28 private import more_collections
29
30 ###
31
32 redef class ToolContext
33 # Option --path
34 var opt_path = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
35
36 # Option --only-metamodel
37 var opt_only_metamodel = new OptionBool("Stop after meta-model processing", "--only-metamodel")
38
39 # Option --only-parse
40 var opt_only_parse = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
41
42 # Option --ignore-visibility
43 var opt_ignore_visibility = new OptionBool("Do not check, and produce errors, on visibility issues.", "--ignore-visibility")
44
45 redef init
46 do
47 super
48 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel, opt_ignore_visibility)
49 end
50
51 # The modelbuilder 1-to-1 associated with the toolcontext
52 fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
53
54 private var modelbuilder_real: nullable ModelBuilder = null
55
56 # Run `process_mainmodule` on all phases
57 fun run_global_phases(mmodules: Array[MModule])
58 do
59 assert not mmodules.is_empty
60 var mainmodule
61 if mmodules.length == 1 then
62 mainmodule = mmodules.first
63 else
64 # We need a main module, so we build it by importing all modules
65 mainmodule = new MModule(modelbuilder.model, null, mmodules.first.name, new Location(mmodules.first.location.file, 0, 0, 0, 0))
66 mainmodule.is_fictive = true
67 mainmodule.set_imported_mmodules(mmodules)
68 end
69 for phase in phases_list do
70 if phase.disabled then continue
71 phase.process_mainmodule(mainmodule, mmodules)
72 end
73 end
74 end
75
76 redef class Phase
77 # Specific action to execute on the whole program.
78 # Called by the `ToolContext::run_global_phases`.
79 #
80 # `mainmodule` is the main module of the program.
81 # It could be an implicit module (called like the first given_mmodules).
82 #
83 # `given_modules` is the list of explicitely requested modules.
84 # from the command-line for instance.
85 #
86 # REQUIRE: `not given_modules.is_empty`
87 # REQUIRE: `(given_modules.length == 1) == (mainmodule == given_modules.first)`
88 #
89 # @toimplement
90 fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do end
91 end
92
93
94 # A model builder knows how to load nit source files and build the associated model
95 class ModelBuilder
96 # The model where new modules, classes and properties are added
97 var model: Model
98
99 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
100 var toolcontext: ToolContext
101
102 # Run phases on all loaded modules
103 fun run_phases
104 do
105 var mmodules = model.mmodules.to_a
106 model.mmodule_importation_hierarchy.sort(mmodules)
107 var nmodules = new Array[AModule]
108 for mm in mmodules do
109 nmodules.add(mmodule2nmodule[mm])
110 end
111 toolcontext.run_phases(nmodules)
112
113 if toolcontext.opt_only_metamodel.value then
114 self.toolcontext.info("*** ONLY METAMODEL", 1)
115 exit(0)
116 end
117 end
118
119 # Instantiate a modelbuilder for a model and a toolcontext
120 # Important, the options of the toolcontext must be correctly set (parse_option already called)
121 init(model: Model, toolcontext: ToolContext)
122 do
123 self.model = model
124 self.toolcontext = toolcontext
125 assert toolcontext.modelbuilder_real == null
126 toolcontext.modelbuilder_real = self
127
128 # Setup the paths value
129 paths.append(toolcontext.opt_path.value)
130
131 var path_env = "NIT_PATH".environ
132 if not path_env.is_empty then
133 paths.append(path_env.split_with(':'))
134 end
135
136 var nit_dir = toolcontext.nit_dir
137 if nit_dir != null then
138 var libname = "{nit_dir}/lib"
139 if libname.file_exists then paths.add(libname)
140 end
141 end
142
143 # Load a bunch of modules.
144 # `modules` can contains filenames or module names.
145 # Imported modules are automatically loaded and modelized.
146 # The result is the corresponding model elements.
147 # Errors and warnings are printed with the toolcontext.
148 #
149 # Note: class and property model element are not analysed.
150 fun parse(modules: Sequence[String]): Array[MModule]
151 do
152 var time0 = get_time
153 # Parse and recursively load
154 self.toolcontext.info("*** PARSE ***", 1)
155 var mmodules = new ArraySet[MModule]
156 for a in modules do
157 var nmodule = self.load_module(a)
158 if nmodule == null then continue # Skip error
159 mmodules.add(nmodule.mmodule.as(not null))
160 end
161 var time1 = get_time
162 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
163
164 self.toolcontext.check_errors
165
166 if toolcontext.opt_only_parse.value then
167 self.toolcontext.info("*** ONLY PARSE...", 1)
168 exit(0)
169 end
170
171 return mmodules.to_a
172 end
173
174 # Return a class named `name` visible by the module `mmodule`.
175 # Visibility in modules is correctly handled.
176 # If no such a class exists, then null is returned.
177 # If more than one class exists, then an error on `anode` is displayed and null is returned.
178 # FIXME: add a way to handle class name conflict
179 fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
180 do
181 var classes = model.get_mclasses_by_name(name)
182 if classes == null then
183 return null
184 end
185
186 var res: nullable MClass = null
187 for mclass in classes do
188 if not mmodule.in_importation <= mclass.intro_mmodule then continue
189 if not mmodule.is_visible(mclass.intro_mmodule, mclass.visibility) then continue
190 if res == null then
191 res = mclass
192 else
193 error(anode, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
194 return null
195 end
196 end
197 return res
198 end
199
200 # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
201 # Visibility in modules is correctly handled.
202 # Protected properties are returned (it is up to the caller to check and reject protected properties).
203 # If no such a property exists, then null is returned.
204 # If more than one property exists, then an error on `anode` is displayed and null is returned.
205 # FIXME: add a way to handle property name conflict
206 fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
207 do
208 var props = self.model.get_mproperties_by_name(name)
209 if props == null then
210 return null
211 end
212
213 var cache = self.try_get_mproperty_by_name2_cache[mmodule, mtype, name]
214 if cache != null then return cache
215
216 var res: nullable MProperty = null
217 var ress: nullable Array[MProperty] = null
218 for mprop in props do
219 if not mtype.has_mproperty(mmodule, mprop) then continue
220 if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue
221 if res == null then
222 res = mprop
223 continue
224 end
225
226 # Two global properties?
227 # First, special case for init, keep the most specific ones
228 if res isa MMethod and mprop isa MMethod and res.is_init and mprop.is_init then
229 var restype = res.intro_mclassdef.bound_mtype
230 var mproptype = mprop.intro_mclassdef.bound_mtype
231 if mproptype.is_subtype(mmodule, null, restype) then
232 # found a most specific constructor, so keep it
233 res = mprop
234 continue
235 end
236 end
237
238 # Ok, just keep all prop in the ress table
239 if ress == null then
240 ress = new Array[MProperty]
241 ress.add(res)
242 end
243 ress.add(mprop)
244 end
245
246 # There is conflict?
247 if ress != null and res isa MMethod and res.is_init then
248 # special case forinit again
249 var restype = res.intro_mclassdef.bound_mtype
250 var ress2 = new Array[MProperty]
251 for mprop in ress do
252 var mproptype = mprop.intro_mclassdef.bound_mtype
253 if not restype.is_subtype(mmodule, null, mproptype) then
254 ress2.add(mprop)
255 else if not mprop isa MMethod or not mprop.is_init then
256 ress2.add(mprop)
257 end
258 end
259 if ress2.is_empty then
260 ress = null
261 else
262 ress = ress2
263 ress.add(res)
264 end
265 end
266
267 if ress != null then
268 assert ress.length > 1
269 var s = new Array[String]
270 for mprop in ress do s.add mprop.full_name
271 self.error(anode, "Ambigous property name '{name}' for {mtype}; conflict between {s.join(" and ")}")
272 end
273
274 self.try_get_mproperty_by_name2_cache[mmodule, mtype, name] = res
275 return res
276 end
277
278 private var try_get_mproperty_by_name2_cache = new HashMap3[MModule, MType, String, nullable MProperty]
279
280
281 # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
282 fun try_get_mproperty_by_name(anode: ANode, mclassdef: MClassDef, name: String): nullable MProperty
283 do
284 return try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.bound_mtype, name)
285 end
286
287 # The list of directories to search for top level modules
288 # The list is initially set with :
289 # * the toolcontext --path option
290 # * the NIT_PATH environment variable
291 # * `toolcontext.nit_dir`
292 # Path can be added (or removed) by the client
293 var paths = new Array[String]
294
295 # Like (an used by) `get_mmodule_by_name` but just return the ModulePath
296 private fun search_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
297 do
298 # First, look in groups
299 var c = mgroup
300 while c != null do
301 var dirname = c.filepath
302 if dirname == null then break # virtual group
303 if dirname.has_suffix(".nit") then break # singleton project
304
305 # Second, try the directory to find a file
306 var try_file = dirname + "/" + name + ".nit"
307 if try_file.file_exists then
308 var res = self.identify_file(try_file.simplify_path)
309 assert res != null
310 return res
311 end
312
313 # Third, try if the requested module is itself a group
314 try_file = dirname + "/" + name + "/" + name + ".nit"
315 if try_file.file_exists then
316 var res = self.identify_file(try_file.simplify_path)
317 assert res != null
318 return res
319 end
320
321 c = c.parent
322 end
323
324 # Look at some known directories
325 var lookpaths = self.paths
326
327 # Look in the directory of the group project also (even if not explicitely in the path)
328 if mgroup != null then
329 # path of the root group
330 var dirname = mgroup.mproject.root.filepath
331 if dirname != null then
332 dirname = dirname.join_path("..").simplify_path
333 if not lookpaths.has(dirname) and dirname.file_exists then
334 lookpaths = lookpaths.to_a
335 lookpaths.add(dirname)
336 end
337 end
338 end
339
340 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
341
342 if candidate == null then
343 if mgroup != null then
344 error(anode, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}")
345 else
346 error(anode, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
347 end
348 return null
349 end
350 return candidate
351 end
352
353 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
354 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
355 # if `mgroup` is null then the module is searched in the top level only.
356 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
357 fun get_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable MModule
358 do
359 var path = search_mmodule_by_name(anode, mgroup, name)
360 if path == null then return null # Forward error
361 var res = self.load_module(path.filepath)
362 if res == null then return null # Forward error
363 return res.mmodule.as(not null)
364 end
365
366 # Search a module `name` from path `lookpaths`.
367 # If found, the path of the file is returned
368 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
369 do
370 var candidate: nullable String = null
371 for dirname in lookpaths do
372 var try_file = (dirname + "/" + name + ".nit").simplify_path
373 if try_file.file_exists then
374 if candidate == null then
375 candidate = try_file
376 else if candidate != try_file then
377 # try to disambiguate conflicting modules
378 var abs_candidate = module_absolute_path(candidate)
379 var abs_try_file = module_absolute_path(try_file)
380 if abs_candidate != abs_try_file then
381 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
382 end
383 end
384 end
385 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
386 if try_file.file_exists then
387 if candidate == null then
388 candidate = try_file
389 else if candidate != try_file then
390 # try to disambiguate conflicting modules
391 var abs_candidate = module_absolute_path(candidate)
392 var abs_try_file = module_absolute_path(try_file)
393 if abs_candidate != abs_try_file then
394 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
395 end
396 end
397 end
398 end
399 if candidate == null then return null
400 return identify_file(candidate)
401 end
402
403 # cache for `identify_file` by realpath
404 private var identified_files = new HashMap[String, nullable ModulePath]
405
406 # Identify a source file
407 # Load the associated project and groups if required
408 private fun identify_file(path: String): nullable ModulePath
409 do
410 # special case for not a nit file
411 if path.file_extension != "nit" then
412 # search in known -I paths
413 var res = search_module_in_paths(null, path, self.paths)
414 if res != null then return res
415
416 # Found nothins? maybe it is a group...
417 var candidate = null
418 if path.file_exists then
419 var mgroup = get_mgroup(path)
420 if mgroup != null then
421 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
422 if owner_path.file_exists then candidate = owner_path
423 end
424 end
425
426 if candidate == null then
427 toolcontext.error(null, "Error: cannot find module `{path}`.")
428 return null
429 end
430 path = candidate
431 end
432
433 # Fast track, the path is already known
434 var pn = path.basename(".nit")
435 var rp = module_absolute_path(path)
436 if identified_files.has_key(rp) then return identified_files[rp]
437
438 # Search for a group
439 var mgrouppath = path.join_path("..").simplify_path
440 var mgroup = get_mgroup(mgrouppath)
441
442 if mgroup == null then
443 # singleton project
444 var mproject = new MProject(pn, model)
445 mgroup = new MGroup(pn, mproject, null) # same name for the root group
446 mgroup.filepath = path
447 mproject.root = mgroup
448 toolcontext.info("found project `{pn}` at {path}", 2)
449 end
450
451 var res = new ModulePath(pn, path, mgroup)
452 mgroup.module_paths.add(res)
453
454 identified_files[rp] = res
455 return res
456 end
457
458 # groups by path
459 private var mgroups = new HashMap[String, nullable MGroup]
460
461 # return the mgroup associated to a directory path
462 # if the directory is not a group null is returned
463 private fun get_mgroup(dirpath: String): nullable MGroup
464 do
465 var rdp = module_absolute_path(dirpath)
466 if mgroups.has_key(rdp) then
467 return mgroups[rdp]
468 end
469
470 # Hack, a group is determined by:
471 # * the presence of a honomymous nit file
472 # * the fact that the directory is named `src`
473 var pn = rdp.basename(".nit")
474 var mp = dirpath.join_path(pn + ".nit").simplify_path
475
476 var dirpath2 = dirpath
477 if not mp.file_exists then
478 if pn == "src" then
479 # With a src directory, the group name is the name of the parent directory
480 dirpath2 = rdp.dirname
481 pn = dirpath2.basename("")
482 else
483 return null
484 end
485 end
486
487 # check parent directory
488 var parentpath = dirpath.join_path("..").simplify_path
489 var parent = get_mgroup(parentpath)
490
491 var mgroup
492 if parent == null then
493 # no parent, thus new project
494 var mproject = new MProject(pn, model)
495 mgroup = new MGroup(pn, mproject, null) # same name for the root group
496 mproject.root = mgroup
497 toolcontext.info("found project `{mproject}` at {dirpath}", 2)
498 else
499 mgroup = new MGroup(pn, parent.mproject, parent)
500 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
501 end
502 var readme = dirpath2.join_path("README.md")
503 if not readme.file_exists then readme = dirpath2.join_path("README")
504 if readme.file_exists then
505 var mdoc = new MDoc
506 var s = new IFStream.open(readme)
507 while not s.eof do
508 mdoc.content.add(s.read_line)
509 end
510 mgroup.mdoc = mdoc
511 mdoc.original_mentity = mgroup
512 end
513 mgroup.filepath = dirpath
514 mgroups[rdp] = mgroup
515 return mgroup
516 end
517
518 # Transform relative paths (starting with '../') into absolute paths
519 private fun module_absolute_path(path: String): String do
520 return getcwd.join_path(path).simplify_path
521 end
522
523 # Try to load a module AST using a path.
524 # Display an error if there is a problem (IO / lexer / parser) and return null
525 fun load_module_ast(filename: String): nullable AModule
526 do
527 if filename.file_extension != "nit" then
528 self.toolcontext.error(null, "Error: file {filename} is not a valid nit module.")
529 return null
530 end
531 if not filename.file_exists then
532 self.toolcontext.error(null, "Error: file {filename} not found.")
533 return null
534 end
535
536 self.toolcontext.info("load module {filename}", 2)
537
538 # Load the file
539 var file = new IFStream.open(filename)
540 var lexer = new Lexer(new SourceFile(filename, file))
541 var parser = new Parser(lexer)
542 var tree = parser.parse
543 file.close
544
545 # Handle lexer and parser error
546 var nmodule = tree.n_base
547 if nmodule == null then
548 var neof = tree.n_eof
549 assert neof isa AError
550 error(neof, neof.message)
551 return null
552 end
553
554 return nmodule
555 end
556
557 # Try to load a module and its imported modules using a path.
558 # Display an error if there is a problem (IO / lexer / parser / importation) and return null
559 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
560 fun load_module(filename: String): nullable AModule
561 do
562 # Look for the module
563 var file = identify_file(filename)
564 if file == null then return null # forward error
565
566 # Already known and loaded? then return it
567 var mmodule = file.mmodule
568 if mmodule != null then
569 return mmodule2nmodule[mmodule]
570 end
571
572 # Load it manually
573 var nmodule = load_module_ast(file.filepath)
574 if nmodule == null then return null # forward error
575
576 # build the mmodule and load imported modules
577 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
578
579 if mmodule == null then return null # forward error
580
581 # Update the file information
582 file.mmodule = mmodule
583
584 # Load imported module
585 build_module_importation(nmodule)
586
587 return nmodule
588 end
589
590 # Injection of a new module without source.
591 # Used by the interpreted
592 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
593 do
594 # Create the module
595
596 var mgroup = null
597 if parent != null then mgroup = parent.mgroup
598 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
599 nmodule.mmodule = mmodule
600 nmodules.add(nmodule)
601 self.mmodule2nmodule[mmodule] = nmodule
602
603 if parent!= null then
604 var imported_modules = new Array[MModule]
605 imported_modules.add(parent)
606 mmodule.set_visibility_for(parent, intrude_visibility)
607 mmodule.set_imported_mmodules(imported_modules)
608 else
609 build_module_importation(nmodule)
610 end
611
612 return nmodule
613 end
614
615 # Visit the AST and create the `MModule` object
616 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
617 do
618 # Check the module name
619 var decl = nmodule.n_moduledecl
620 if decl == null then
621 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
622 else
623 var decl_name = decl.n_name.n_id.text
624 if decl_name != mod_name then
625 error(decl.n_name, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
626 end
627 end
628
629 # Create the module
630 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
631 nmodule.mmodule = mmodule
632 nmodules.add(nmodule)
633 self.mmodule2nmodule[mmodule] = nmodule
634
635 if decl != null then
636 var ndoc = decl.n_doc
637 if ndoc != null then
638 var mdoc = ndoc.to_mdoc
639 mmodule.mdoc = mdoc
640 mdoc.original_mentity = mmodule
641 else
642 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
643 end
644 end
645
646 return mmodule
647 end
648
649 # Analysis the module importation and fill the module_importation_hierarchy
650 private fun build_module_importation(nmodule: AModule)
651 do
652 if nmodule.is_importation_done then return
653 nmodule.is_importation_done = true
654 var mmodule = nmodule.mmodule.as(not null)
655 var stdimport = true
656 var imported_modules = new Array[MModule]
657 for aimport in nmodule.n_imports do
658 stdimport = false
659 if not aimport isa AStdImport then
660 continue
661 end
662 var mgroup = mmodule.mgroup
663 if aimport.n_name.n_quad != null then mgroup = null # Start from top level
664 for grp in aimport.n_name.n_path do
665 var path = search_mmodule_by_name(grp, mgroup, grp.text)
666 if path == null then return # Skip error
667 mgroup = path.mgroup
668 end
669 var mod_name = aimport.n_name.n_id.text
670 var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name)
671 if sup == null then continue # Skip error
672 aimport.mmodule = sup
673 imported_modules.add(sup)
674 var mvisibility = aimport.n_visibility.mvisibility
675 if mvisibility == protected_visibility then
676 error(aimport.n_visibility, "Error: only properties can be protected.")
677 return
678 end
679 if sup == mmodule then
680 error(aimport.n_name, "Error: Dependency loop in module {mmodule}.")
681 end
682 if sup.in_importation < mmodule then
683 error(aimport.n_name, "Error: Dependency loop between modules {mmodule} and {sup}.")
684 return
685 end
686 mmodule.set_visibility_for(sup, mvisibility)
687 end
688 if stdimport then
689 var mod_name = "standard"
690 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
691 if sup != null then # Skip error
692 imported_modules.add(sup)
693 mmodule.set_visibility_for(sup, public_visibility)
694 end
695 end
696 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
697 mmodule.set_imported_mmodules(imported_modules)
698
699 # TODO: Correctly check for useless importation
700 # It is even doable?
701 var directs = mmodule.in_importation.direct_greaters
702 for nim in nmodule.n_imports do
703 if not nim isa AStdImport then continue
704 var im = nim.mmodule
705 if im == null then continue
706 if directs.has(im) then continue
707 # This generates so much noise that it is simpler to just comment it
708 #warning(nim, "Warning: possible useless importation of {im}")
709 end
710 end
711
712 # All the loaded modules
713 var nmodules = new Array[AModule]
714
715 # Register the nmodule associated to each mmodule
716 # FIXME: why not refine the `MModule` class with a nullable attribute?
717 var mmodule2nmodule = new HashMap[MModule, AModule]
718
719 # Helper function to display an error on a node.
720 # Alias for `self.toolcontext.error(n.hot_location, text)`
721 fun error(n: ANode, text: String)
722 do
723 self.toolcontext.error(n.hot_location, text)
724 end
725
726 # Helper function to display a warning on a node.
727 # Alias for: `self.toolcontext.warning(n.hot_location, text)`
728 fun warning(n: ANode, tag, text: String)
729 do
730 self.toolcontext.warning(n.hot_location, tag, text)
731 end
732
733 # Helper function to display an advice on a node.
734 # Alias for: `self.toolcontext.advice(n.hot_location, text)`
735 fun advice(n: ANode, tag, text: String)
736 do
737 self.toolcontext.advice(n.hot_location, tag, text)
738 end
739
740 # Force to get the primitive method named `name` on the type `recv` or do a fatal error on `n`
741 fun force_get_primitive_method(n: nullable ANode, name: String, recv: MClass, mmodule: MModule): MMethod
742 do
743 var res = mmodule.try_get_primitive_method(name, recv)
744 if res == null then
745 var l = null
746 if n != null then l = n.hot_location
747 self.toolcontext.fatal_error(l, "Fatal Error: {recv} must have a property named {name}.")
748 abort
749 end
750 return res
751 end
752 end
753
754 # placeholder to a module file identified but not always loaded in a project
755 private class ModulePath
756 # The name of the module
757 # (it's the basename of the filepath)
758 var name: String
759
760 # The human path of the module
761 var filepath: String
762
763 # The group (and the project) of the possible module
764 var mgroup: MGroup
765
766 # The loaded module (if any)
767 var mmodule: nullable MModule = null
768
769 redef fun to_s do return filepath
770 end
771
772 redef class MGroup
773 # modules paths associated with the group
774 private var module_paths = new Array[ModulePath]
775 end
776
777 redef class AStdImport
778 # The imported module once determined
779 var mmodule: nullable MModule = null
780 end
781
782 redef class AModule
783 # The associated MModule once build by a `ModelBuilder`
784 var mmodule: nullable MModule
785 # Flag that indicate if the importation is already completed
786 var is_importation_done: Bool = false
787 end
788
789 redef class AVisibility
790 # The visibility level associated with the AST node class
791 fun mvisibility: MVisibility is abstract
792 end
793 redef class AIntrudeVisibility
794 redef fun mvisibility do return intrude_visibility
795 end
796 redef class APublicVisibility
797 redef fun mvisibility do return public_visibility
798 end
799 redef class AProtectedVisibility
800 redef fun mvisibility do return protected_visibility
801 end
802 redef class APrivateVisibility
803 redef fun mvisibility do return private_visibility
804 end
805
806 redef class ADoc
807 private var mdoc_cache: nullable MDoc
808 fun to_mdoc: MDoc
809 do
810 var res = mdoc_cache
811 if res != null then return res
812 res = new MDoc
813 for c in n_comment do
814 var text = c.text
815 if text.length < 2 then
816 res.content.add ""
817 continue
818 end
819 assert text.chars[0] == '#'
820 if text.chars[1] == ' ' then
821 text = text.substring_from(2) # eat starting `#` and space
822 else
823 text = text.substring_from(1) # eat atarting `#` only
824 end
825 if text.chars.last == '\n' then text = text.substring(0, text.length-1) # drop \n
826 res.content.add(text)
827 end
828 mdoc_cache = res
829 return res
830 end
831 end