1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Load nit source files and build the associated model
21 # FIXME split this module into submodules
22 # FIXME add missing error checks
32 private import more_collections
36 redef class ToolContext
38 var opt_path
: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
40 # Option --only-metamodel
41 var opt_only_metamodel
: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
44 var opt_only_parse
: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
49 option_context
.add_option
(opt_path
, opt_only_parse
, opt_only_metamodel
)
52 fun modelbuilder
: ModelBuilder do return modelbuilder_real
.as(not null)
53 private var modelbuilder_real
: nullable ModelBuilder = null
55 # Run `process_mainmodule` on all phases
56 fun run_global_phases
(mmodules
: Array[MModule])
58 assert not mmodules
.is_empty
60 if mmodules
.length
== 1 then
61 mainmodule
= mmodules
.first
63 # We need a main module, so we build it by importing all modules
64 mainmodule
= new MModule(modelbuilder
.model
, null, "<main>", new Location(null, 0, 0, 0, 0))
65 mainmodule
.set_imported_mmodules
(mmodules
)
67 for phase
in phases_list
do
68 phase
.process_mainmodule
(mainmodule
)
74 # Specific action to execute on the whole program
75 # Called by the `ToolContext::run_global_phases`
77 fun process_mainmodule
(mainmodule
: MModule) do end
81 # A model builder knows how to load nit source files and build the associated model
83 # The model where new modules, classes and properties are added
86 # The toolcontext used to control the interaction with the user (getting options and displaying messages)
87 var toolcontext
: ToolContext
89 # Run phases on all loaded modules
92 var mmodules
= model
.mmodules
.to_a
93 model
.mmodule_importation_hierarchy
.sort
(mmodules
)
94 var nmodules
= new Array[AModule]
96 nmodules
.add
(mmodule2nmodule
[mm
])
98 toolcontext
.run_phases
(nmodules
)
100 if toolcontext
.opt_only_metamodel
.value
then
101 self.toolcontext
.info
("*** ONLY METAMODEL", 1)
106 # Instantiate a modelbuilder for a model and a toolcontext
107 # Important, the options of the toolcontext must be correctly set (parse_option already called)
108 init(model
: Model, toolcontext
: ToolContext)
111 self.toolcontext
= toolcontext
112 assert toolcontext
.modelbuilder_real
== null
113 toolcontext
.modelbuilder_real
= self
115 # Setup the paths value
116 paths
.append
(toolcontext
.opt_path
.value
)
118 var path_env
= "NIT_PATH".environ
119 if not path_env
.is_empty
then
120 paths
.append
(path_env
.split_with
(':'))
123 path_env
= "NIT_DIR".environ
124 if not path_env
.is_empty
then
125 var libname
= "{path_env}/lib"
126 if libname
.file_exists
then paths
.add
(libname
)
129 var libname
= "{sys.program_name.dirname}/../lib"
130 if libname
.file_exists
then paths
.add
(libname
.simplify_path
)
133 # Load a bunch of modules.
134 # `modules` can contains filenames or module names.
135 # Imported modules are automatically loaded and modelized.
136 # The result is the corresponding model elements.
137 # Errors and warnings are printed with the toolcontext.
139 # Note: class and property model element are not analysed.
140 fun parse
(modules
: Sequence[String]): Array[MModule]
143 # Parse and recursively load
144 self.toolcontext
.info
("*** PARSE ***", 1)
145 var mmodules
= new ArraySet[MModule]
147 var nmodule
= self.load_module
(a
)
148 if nmodule
== null then continue # Skip error
149 mmodules
.add
(nmodule
.mmodule
.as(not null))
152 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
154 self.toolcontext
.check_errors
156 if toolcontext
.opt_only_parse
.value
then
157 self.toolcontext
.info
("*** ONLY PARSE...", 1)
164 # Return a class named `name` visible by the module `mmodule`.
165 # Visibility in modules is correctly handled.
166 # If no such a class exists, then null is returned.
167 # If more than one class exists, then an error on `anode` is displayed and null is returned.
168 # FIXME: add a way to handle class name conflict
169 fun try_get_mclass_by_name
(anode
: ANode, mmodule
: MModule, name
: String): nullable MClass
171 var classes
= model
.get_mclasses_by_name
(name
)
172 if classes
== null then
176 var res
: nullable MClass = null
177 for mclass
in classes
do
178 if not mmodule
.in_importation
<= mclass
.intro_mmodule
then continue
179 if not mmodule
.is_visible
(mclass
.intro_mmodule
, mclass
.visibility
) then continue
183 error
(anode
, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
190 # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
191 # Visibility in modules is correctly handled.
192 # Protected properties are returned (it is up to the caller to check and reject protected properties).
193 # If no such a property exists, then null is returned.
194 # If more than one property exists, then an error on `anode` is displayed and null is returned.
195 # FIXME: add a way to handle property name conflict
196 fun try_get_mproperty_by_name2
(anode
: ANode, mmodule
: MModule, mtype
: MType, name
: String): nullable MProperty
198 var props
= self.model
.get_mproperties_by_name
(name
)
199 if props
== null then
203 var cache
= self.try_get_mproperty_by_name2_cache
[mmodule
, mtype
, name
]
204 if cache
!= null then return cache
206 var res
: nullable MProperty = null
207 var ress
: nullable Array[MProperty] = null
208 for mprop
in props
do
209 if not mtype
.has_mproperty
(mmodule
, mprop
) then continue
210 if not mmodule
.is_visible
(mprop
.intro_mclassdef
.mmodule
, mprop
.visibility
) then continue
214 var restype
= res
.intro_mclassdef
.bound_mtype
215 var mproptype
= mprop
.intro_mclassdef
.bound_mtype
216 if restype
.is_subtype
(mmodule
, null, mproptype
) then
218 else if mproptype
.is_subtype
(mmodule
, null, restype
) then
221 if ress
== null then ress
= new Array[MProperty]
227 var restype
= res
.intro_mclassdef
.bound_mtype
229 var mproptype
= mprop
.intro_mclassdef
.bound_mtype
230 if not restype
.is_subtype
(mmodule
, null, mproptype
) then
231 self.error
(anode
, "Ambigous property name '{name}' for {mtype}; conflict between {mprop.full_name} and {res.full_name}")
237 self.try_get_mproperty_by_name2_cache
[mmodule
, mtype
, name
] = res
241 private var try_get_mproperty_by_name2_cache
: HashMap3[MModule, MType, String, nullable MProperty] = new HashMap3[MModule, MType, String, nullable MProperty]
244 # Alias for try_get_mproperty_by_name2(anode, mclassdef.mmodule, mclassdef.mtype, name)
245 fun try_get_mproperty_by_name
(anode
: ANode, mclassdef
: MClassDef, name
: String): nullable MProperty
247 return try_get_mproperty_by_name2
(anode
, mclassdef
.mmodule
, mclassdef
.bound_mtype
, name
)
250 # The list of directories to search for top level modules
251 # The list is initially set with :
252 # * the toolcontext --path option
253 # * the NIT_PATH environment variable
254 # * some heuristics including the NIT_DIR environment variable and the progname of the process
255 # Path can be added (or removed) by the client
256 var paths
: Array[String] = new Array[String]
258 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
259 # If `mmodule` is set, then the module search starts from it up to the top level (see `paths`);
260 # if `mmodule` is null then the module is searched in the top level only.
261 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
262 # FIXME: add a way to handle module name conflict
263 fun get_mmodule_by_name
(anode
: ANode, mmodule
: nullable MModule, name
: String): nullable MModule
265 # First, look in groups of the module
266 if mmodule
!= null then
267 var mgroup
= mmodule
.mgroup
268 while mgroup
!= null do
269 var dirname
= mgroup
.filepath
270 if dirname
== null then break # virtual group
271 if dirname
.has_suffix
(".nit") then break # singleton project
273 # Second, try the directory to find a file
274 var try_file
= dirname
+ "/" + name
+ ".nit"
275 if try_file
.file_exists
then
276 var res
= self.load_module
(try_file
.simplify_path
)
277 if res
== null then return null # Forward error
278 return res
.mmodule
.as(not null)
281 # Third, try if the requested module is itself a group
282 try_file
= dirname
+ "/" + name
+ "/" + name
+ ".nit"
283 if try_file
.file_exists
then
284 mgroup
= get_mgroup
(dirname
+ "/" + name
)
285 var res
= self.load_module
(try_file
.simplify_path
)
286 if res
== null then return null # Forward error
287 return res
.mmodule
.as(not null)
290 mgroup
= mgroup
.parent
294 # Look at some known directories
295 var lookpaths
= self.paths
297 # Look in the directory of module project also (even if not explicitely in the path)
298 if mmodule
!= null and mmodule
.mgroup
!= null then
299 # path of the root group
300 var dirname
= mmodule
.mgroup
.mproject
.root
.filepath
301 if dirname
!= null then
302 dirname
= dirname
.join_path
("..").simplify_path
303 if not lookpaths
.has
(dirname
) and dirname
.file_exists
then
304 lookpaths
= lookpaths
.to_a
305 lookpaths
.add
(dirname
)
310 var candidate
= search_module_in_paths
(anode
.hot_location
, name
, lookpaths
)
312 if candidate
== null then
313 if mmodule
!= null then
314 error
(anode
, "Error: cannot find module {name} from {mmodule}. tried {lookpaths.join(", ")}")
316 error
(anode
, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
320 var res
= self.load_module
(candidate
)
321 if res
== null then return null # Forward error
322 return res
.mmodule
.as(not null)
325 # Search a module `name` from path `lookpaths`.
326 # If found, the path of the file is returned
327 private fun search_module_in_paths
(location
: nullable Location, name
: String, lookpaths
: Collection[String]): nullable String
329 var candidate
: nullable String = null
330 for dirname
in lookpaths
do
331 var try_file
= (dirname
+ "/" + name
+ ".nit").simplify_path
332 if try_file
.file_exists
then
333 if candidate
== null then
335 else if candidate
!= try_file
then
336 # try to disambiguate conflicting modules
337 var abs_candidate
= module_absolute_path
(candidate
)
338 var abs_try_file
= module_absolute_path
(try_file
)
339 if abs_candidate
!= abs_try_file
then
340 toolcontext
.error
(location
, "Error: conflicting module file for {name}: {candidate} {try_file}")
344 try_file
= (dirname
+ "/" + name
+ "/" + name
+ ".nit").simplify_path
345 if try_file
.file_exists
then
346 if candidate
== null then
348 else if candidate
!= try_file
then
349 # try to disambiguate conflicting modules
350 var abs_candidate
= module_absolute_path
(candidate
)
351 var abs_try_file
= module_absolute_path
(try_file
)
352 if abs_candidate
!= abs_try_file
then
353 toolcontext
.error
(location
, "Error: conflicting module file for {name}: {candidate} {try_file}")
361 # cache for `identify_file` by realpath
362 private var identified_files
= new HashMap[String, nullable ModulePath]
364 # Identify a source file
365 # Load the associated project and groups if required
366 private fun identify_file
(path
: String): nullable ModulePath
368 # special case for not a nit file
369 if path
.file_extension
!= "nit" then
370 # search in known -I paths
371 var candidate
= search_module_in_paths
(null, path
, self.paths
)
373 # Found nothins? maybe it is a group...
374 if candidate
== null and path
.file_exists
then
375 var mgroup
= get_mgroup
(path
)
376 if mgroup
!= null then
377 var owner_path
= mgroup
.filepath
.join_path
(mgroup
.name
+ ".nit")
378 if owner_path
.file_exists
then candidate
= owner_path
382 if candidate
== null then
383 toolcontext
.error
(null, "Error: cannot find module `{path}`.")
389 # Fast track, the path is already known
390 var pn
= path
.basename
(".nit")
391 var rp
= module_absolute_path
(path
)
392 if identified_files
.has_key
(rp
) then return identified_files
[rp
]
395 var mgrouppath
= path
.join_path
("..").simplify_path
396 var mgroup
= get_mgroup
(mgrouppath
)
398 if mgroup
== null then
400 var mproject
= new MProject(pn
, model
)
401 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
402 mgroup
.filepath
= path
403 mproject
.root
= mgroup
404 toolcontext
.info
("found project `{pn}` at {path}", 2)
407 var res
= new ModulePath(pn
, path
, mgroup
)
408 mgroup
.module_paths
.add
(res
)
410 identified_files
[rp
] = res
415 private var mgroups
= new HashMap[String, nullable MGroup]
417 # return the mgroup associated to a directory path
418 # if the directory is not a group null is returned
419 private fun get_mgroup
(dirpath
: String): nullable MGroup
421 var rdp
= module_absolute_path
(dirpath
)
422 if mgroups
.has_key
(rdp
) then
426 # Hack, a dir is determined by the presence of a honomymous nit file
427 var pn
= rdp
.basename
(".nit")
428 var mp
= dirpath
.join_path
(pn
+ ".nit").simplify_path
430 if not mp
.file_exists
then return null
432 # check parent directory
433 var parentpath
= dirpath
.join_path
("..").simplify_path
434 var parent
= get_mgroup
(parentpath
)
437 if parent
== null then
438 # no parent, thus new project
439 var mproject
= new MProject(pn
, model
)
440 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
441 mproject
.root
= mgroup
442 toolcontext
.info
("found project `{mproject}` at {dirpath}", 2)
444 mgroup
= new MGroup(pn
, parent
.mproject
, parent
)
445 toolcontext
.info
("found sub group `{mgroup.full_name}` at {dirpath}", 2)
447 mgroup
.filepath
= dirpath
448 mgroups
[rdp
] = mgroup
452 # Transform relative paths (starting with '../') into absolute paths
453 private fun module_absolute_path
(path
: String): String do
454 return getcwd
.join_path
(path
).simplify_path
457 # Try to load a module AST using a path.
458 # Display an error if there is a problem (IO / lexer / parser) and return null
459 fun load_module_ast
(filename
: String): nullable AModule
461 if filename
.file_extension
!= "nit" then
462 self.toolcontext
.error
(null, "Error: file {filename} is not a valid nit module.")
465 if not filename
.file_exists
then
466 self.toolcontext
.error
(null, "Error: file {filename} not found.")
470 self.toolcontext
.info
("load module {filename}", 2)
473 var file
= new IFStream.open
(filename
)
474 var lexer
= new Lexer(new SourceFile(filename
, file
))
475 var parser
= new Parser(lexer
)
476 var tree
= parser
.parse
478 var mod_name
= filename
.basename
(".nit")
480 # Handle lexer and parser error
481 var nmodule
= tree
.n_base
482 if nmodule
== null then
483 var neof
= tree
.n_eof
484 assert neof
isa AError
485 error
(neof
, neof
.message
)
492 # Try to load a module and its imported modules using a path.
493 # Display an error if there is a problem (IO / lexer / parser / importation) and return null
494 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
495 fun load_module
(filename
: String): nullable AModule
497 # Look for the module
498 var file
= identify_file
(filename
)
499 if file
== null then return null # forward error
501 # Already known and loaded? then return it
502 var mmodule
= file
.mmodule
503 if mmodule
!= null then
504 return mmodule2nmodule
[mmodule
]
508 var nmodule
= load_module_ast
(file
.filepath
)
509 if nmodule
== null then return null # forward error
511 # build the mmodule and load imported modules
512 mmodule
= build_a_mmodule
(file
.mgroup
, file
.name
, nmodule
)
514 if mmodule
== null then return null # forward error
516 # Update the file information
517 file
.mmodule
= mmodule
519 # Load imported module
520 build_module_importation
(nmodule
)
525 fun load_rt_module
(parent
: MModule, nmodule
: AModule, mod_name
: String): nullable AModule
528 var mmodule
= new MModule(model
, parent
.mgroup
, mod_name
, nmodule
.location
)
529 nmodule
.mmodule
= mmodule
530 nmodules
.add
(nmodule
)
531 self.mmodule2nmodule
[mmodule
] = nmodule
533 var imported_modules
= new Array[MModule]
535 imported_modules
.add
(parent
)
536 mmodule
.set_visibility_for
(parent
, intrude_visibility
)
538 mmodule
.set_imported_mmodules
(imported_modules
)
543 # Visit the AST and create the `MModule` object
544 private fun build_a_mmodule
(mgroup
: nullable MGroup, mod_name
: String, nmodule
: AModule): nullable MModule
546 # Check the module name
547 var decl
= nmodule
.n_moduledecl
549 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
551 var decl_name
= decl
.n_name
.n_id
.text
552 if decl_name
!= mod_name
then
553 error
(decl
.n_name
, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
558 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
559 nmodule
.mmodule
= mmodule
560 nmodules
.add
(nmodule
)
561 self.mmodule2nmodule
[mmodule
] = nmodule
564 var ndoc
= decl
.n_doc
565 if ndoc
!= null then mmodule
.mdoc
= ndoc
.to_mdoc
571 # Analysis the module importation and fill the module_importation_hierarchy
572 private fun build_module_importation
(nmodule
: AModule)
574 if nmodule
.is_importation_done
then return
575 nmodule
.is_importation_done
= true
576 var mmodule
= nmodule
.mmodule
.as(not null)
578 var imported_modules
= new Array[MModule]
579 for aimport
in nmodule
.n_imports
do
581 if not aimport
isa AStdImport then
584 var mod_name
= aimport
.n_name
.n_id
.text
585 var sup
= self.get_mmodule_by_name
(aimport
.n_name
, mmodule
, mod_name
)
586 if sup
== null then continue # Skip error
587 aimport
.mmodule
= sup
588 imported_modules
.add
(sup
)
589 var mvisibility
= aimport
.n_visibility
.mvisibility
590 if mvisibility
== protected_visibility
then
591 error
(aimport
.n_visibility
, "Error: only properties can be protected.")
594 if sup
== mmodule
then
595 error
(aimport
.n_name
, "Error: Dependency loop in module {mmodule}.")
597 if sup
.in_importation
< mmodule
then
598 error
(aimport
.n_name
, "Error: Dependency loop between modules {mmodule} and {sup}.")
601 mmodule
.set_visibility_for
(sup
, mvisibility
)
604 var mod_name
= "standard"
605 var sup
= self.get_mmodule_by_name
(nmodule
, null, mod_name
)
606 if sup
!= null then # Skip error
607 imported_modules
.add
(sup
)
608 mmodule
.set_visibility_for
(sup
, public_visibility
)
611 self.toolcontext
.info
("{mmodule} imports {imported_modules.join(", ")}", 3)
612 mmodule
.set_imported_mmodules
(imported_modules
)
615 # All the loaded modules
616 var nmodules
: Array[AModule] = new Array[AModule]
618 # Register the nmodule associated to each mmodule
619 # FIXME: why not refine the `MModule` class with a nullable attribute?
620 var mmodule2nmodule
: HashMap[MModule, AModule] = new HashMap[MModule, AModule]
622 # Helper function to display an error on a node.
623 # Alias for `self.toolcontext.error(n.hot_location, text)`
624 fun error
(n
: ANode, text
: String)
626 self.toolcontext
.error
(n
.hot_location
, text
)
629 # Helper function to display a warning on a node.
630 # Alias for: `self.toolcontext.warning(n.hot_location, text)`
631 fun warning
(n
: ANode, text
: String)
633 self.toolcontext
.warning
(n
.hot_location
, text
)
636 # Force to get the primitive method named `name` on the type `recv` or do a fatal error on `n`
637 fun force_get_primitive_method
(n
: ANode, name
: String, recv
: MClass, mmodule
: MModule): MMethod
639 var res
= mmodule
.try_get_primitive_method
(name
, recv
)
641 self.toolcontext
.fatal_error
(n
.hot_location
, "Fatal Error: {recv} must have a property named {name}.")
648 # placeholder to a module file identified but not always loaded in a project
649 private class ModulePath
650 # The name of the module
651 # (it's the basename of the filepath)
654 # The human path of the module
657 # The group (and the project) of the possible module
660 # The loaded module (if any)
661 var mmodule
: nullable MModule = null
663 redef fun to_s
do return filepath
667 # modules paths associated with the group
668 private var module_paths
= new Array[ModulePath]
671 redef class AStdImport
672 # The imported module once determined
673 var mmodule
: nullable MModule = null
677 # The associated MModule once build by a `ModelBuilder`
678 var mmodule
: nullable MModule
679 # Flag that indicate if the importation is already completed
680 var is_importation_done
: Bool = false
683 redef class AVisibility
684 # The visibility level associated with the AST node class
685 fun mvisibility
: MVisibility is abstract
687 redef class AIntrudeVisibility
688 redef fun mvisibility
do return intrude_visibility
690 redef class APublicVisibility
691 redef fun mvisibility
do return public_visibility
693 redef class AProtectedVisibility
694 redef fun mvisibility
do return protected_visibility
696 redef class APrivateVisibility
697 redef fun mvisibility
do return private_visibility
701 private var mdoc_cache
: nullable MDoc
705 if res
!= null then return res
707 for c
in n_comment
do
709 if text
.length
< 2 then
713 assert text
.chars
[0] == '#'
714 if text
.chars
[1] == ' ' then
715 text
= text
.substring_from
(2) # eat starting `#` and space
717 text
= text
.substring_from
(1) # eat atarting `#` only
719 if text
.chars
.last
== '\n' then text
= text
.substring
(0, text
.length-1
) # drop \n
720 res
.content
.add
(text
)