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