X-Git-Url: http://nitlanguage.org diff --git a/src/modelbuilder.nit b/src/modelbuilder.nit index 58c6296..eac98df 100644 --- a/src/modelbuilder.nit +++ b/src/modelbuilder.nit @@ -22,11 +22,7 @@ # FIXME add missing error checks module modelbuilder -import parser import model -import poset -import opts -import toolcontext import phase private import more_collections @@ -43,28 +39,53 @@ redef class ToolContext # Option --only-parse var opt_only_parse: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse") + # Option --ignore-visibility + var opt_ignore_visibility: OptionBool = new OptionBool("Do not check, and produce errors, on visibility issues.", "--ignore-visibility") + redef init do super - option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel) + option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel, opt_ignore_visibility) end fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null) private var modelbuilder_real: nullable ModelBuilder = null - fun run_global_phases(mainmodule: MModule) + # Run `process_mainmodule` on all phases + fun run_global_phases(mmodules: Array[MModule]) do + assert not mmodules.is_empty + var mainmodule + if mmodules.length == 1 then + mainmodule = mmodules.first + else + # We need a main module, so we build it by importing all modules + mainmodule = new MModule(modelbuilder.model, null, mmodules.first.name, new Location(mmodules.first.location.file, 0, 0, 0, 0)) + mainmodule.is_fictive = true + mainmodule.set_imported_mmodules(mmodules) + end for phase in phases_list do - phase.process_mainmodule(mainmodule) + if phase.disabled then continue + phase.process_mainmodule(mainmodule, mmodules) end end end redef class Phase - # Specific action to execute on the whole program - # Called by the `ToolContext::run_global_phases` + # Specific action to execute on the whole program. + # Called by the `ToolContext::run_global_phases`. + # + # `mainmodule` is the main module of the program. + # It could be an implicit module (called like the first given_mmodules). + # + # `given_modules` is the list of explicitely requested modules. + # from the command-line for instance. + # + # REQUIRE: `not given_modules.is_empty` + # REQUIRE: `(given_modules.length == 1) == (mainmodule == given_modules.first)` + # # @toimplement - fun process_mainmodule(mainmodule: MModule) do end + fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do end end @@ -110,14 +131,11 @@ class ModelBuilder paths.append(path_env.split_with(':')) end - path_env = "NIT_DIR".environ - if not path_env.is_empty then - var libname = "{path_env}/lib" + var nit_dir = toolcontext.nit_dir + if nit_dir != null then + var libname = "{nit_dir}/lib" if libname.file_exists then paths.add(libname) end - - var libname = "{sys.program_name.dirname}/../lib" - if libname.file_exists then paths.add(libname.simplify_path) end # Load a bunch of modules. @@ -200,28 +218,55 @@ class ModelBuilder if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue if res == null then res = mprop - else + continue + end + + # Two global properties? + # First, special case for init, keep the most specific ones + if res isa MMethod and mprop isa MMethod and res.is_init and mprop.is_init then var restype = res.intro_mclassdef.bound_mtype var mproptype = mprop.intro_mclassdef.bound_mtype - if restype.is_subtype(mmodule, null, mproptype) then - # we keep res - else if mproptype.is_subtype(mmodule, null, restype) then + if mproptype.is_subtype(mmodule, null, restype) then + # found a most specific constructor, so keep it res = mprop - else - if ress == null then ress = new Array[MProperty] - ress.add(mprop) + continue end end + + # Ok, just keep all prop in the ress table + if ress == null then + ress = new Array[MProperty] + ress.add(res) + end + ress.add(mprop) end - if ress != null then + + # There is conflict? + if ress != null and res isa MMethod and res.is_init then + # special case forinit again var restype = res.intro_mclassdef.bound_mtype + var ress2 = new Array[MProperty] for mprop in ress do var mproptype = mprop.intro_mclassdef.bound_mtype if not restype.is_subtype(mmodule, null, mproptype) then - self.error(anode, "Ambigous property name '{name}' for {mtype}; conflict between {mprop.full_name} and {res.full_name}") - return null + ress2.add(mprop) + else if not mprop isa MMethod or not mprop.is_init then + ress2.add(mprop) end end + if ress2.is_empty then + ress = null + else + ress = ress2 + ress.add(res) + end + end + + if ress != null then + assert ress.length > 1 + var s = new Array[String] + for mprop in ress do s.add mprop.full_name + self.error(anode, "Ambigous property name '{name}' for {mtype}; conflict between {s.join(" and ")}") end self.try_get_mproperty_by_name2_cache[mmodule, mtype, name] = res @@ -241,57 +286,46 @@ class ModelBuilder # The list is initially set with : # * the toolcontext --path option # * the NIT_PATH environment variable - # * some heuristics including the NIT_DIR environment variable and the progname of the process + # * `toolcontext.nit_dir` # Path can be added (or removed) by the client var paths: Array[String] = new Array[String] - # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed. - # If `mmodule` is set, then the module search starts from it up to the top level (see `paths`); - # if `mmodule` is null then the module is searched in the top level only. - # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned. - # FIXME: add a way to handle module name conflict - fun get_mmodule_by_name(anode: ANode, mmodule: nullable MModule, name: String): nullable MModule + # Like (an used by) `get_mmodule_by_name` but just return the ModulePath + private fun search_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable ModulePath do - # what path where tried to display on error message - var tries = new Array[String] - - # First, look in groups of the module - if mmodule != null then - var mgroup = mmodule.mgroup - while mgroup != null do - var dirname = mgroup.filepath - if dirname == null then break # virtual group - if dirname.has_suffix(".nit") then break # singleton project - - # Second, try the directory to find a file - var try_file = dirname + "/" + name + ".nit" - tries.add try_file - if try_file.file_exists then - var res = self.load_module(try_file.simplify_path) - if res == null then return null # Forward error - return res.mmodule.as(not null) - end - - # Third, try if the requested module is itself a group - try_file = dirname + "/" + name + "/" + name + ".nit" - if try_file.file_exists then - mgroup = get_mgroup(dirname + "/" + name) - var res = self.load_module(try_file.simplify_path) - if res == null then return null # Forward error - return res.mmodule.as(not null) - end + # First, look in groups + var c = mgroup + while c != null do + var dirname = c.filepath + if dirname == null then break # virtual group + if dirname.has_suffix(".nit") then break # singleton project + + # Second, try the directory to find a file + var try_file = dirname + "/" + name + ".nit" + if try_file.file_exists then + var res = self.identify_file(try_file.simplify_path) + assert res != null + return res + end - mgroup = mgroup.parent + # Third, try if the requested module is itself a group + try_file = dirname + "/" + name + "/" + name + ".nit" + if try_file.file_exists then + var res = self.identify_file(try_file.simplify_path) + assert res != null + return res end + + c = c.parent end # Look at some known directories var lookpaths = self.paths - # Look in the directory of module project also (even if not explicitely in the path) - if mmodule != null and mmodule.mgroup != null then + # Look in the directory of the group project also (even if not explicitely in the path) + if mgroup != null then # path of the root group - var dirname = mmodule.mgroup.mproject.root.filepath + var dirname = mgroup.mproject.root.filepath if dirname != null then dirname = dirname.join_path("..").simplify_path if not lookpaths.has(dirname) and dirname.file_exists then @@ -301,10 +335,39 @@ class ModelBuilder end end + var candidate = search_module_in_paths(anode.hot_location, name, lookpaths) + + if candidate == null then + if mgroup != null then + error(anode, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}") + else + error(anode, "Error: cannot find module {name}. tried {lookpaths.join(", ")}") + end + return null + end + return candidate + end + + # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed. + # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`); + # if `mgroup` is null then the module is searched in the top level only. + # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned. + fun get_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable MModule + do + var path = search_mmodule_by_name(anode, mgroup, name) + if path == null then return null # Forward error + var res = self.load_module(path.filepath) + if res == null then return null # Forward error + return res.mmodule.as(not null) + end + + # Search a module `name` from path `lookpaths`. + # If found, the path of the file is returned + private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath + do var candidate: nullable String = null for dirname in lookpaths do var try_file = (dirname + "/" + name + ".nit").simplify_path - tries.add try_file if try_file.file_exists then if candidate == null then candidate = try_file @@ -313,7 +376,7 @@ class ModelBuilder var abs_candidate = module_absolute_path(candidate) var abs_try_file = module_absolute_path(try_file) if abs_candidate != abs_try_file then - error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}") + toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}") end end end @@ -326,22 +389,13 @@ class ModelBuilder var abs_candidate = module_absolute_path(candidate) var abs_try_file = module_absolute_path(try_file) if abs_candidate != abs_try_file then - error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}") + toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}") end end end end - if candidate == null then - if mmodule != null then - error(anode, "Error: cannot find module {name} from {mmodule}. tried {tries.join(", ")}") - else - error(anode, "Error: cannot find module {name}. tried {tries.join(", ")}") - end - return null - end - var res = self.load_module(candidate) - if res == null then return null # Forward error - return res.mmodule.as(not null) + if candidate == null then return null + return identify_file(candidate) end # cache for `identify_file` by realpath @@ -351,9 +405,27 @@ class ModelBuilder # Load the associated project and groups if required private fun identify_file(path: String): nullable ModulePath do - if not path.file_exists then - toolcontext.error(null, "Error: `{path}` does not exists") - return null + # special case for not a nit file + if path.file_extension != "nit" then + # search in known -I paths + var res = search_module_in_paths(null, path, self.paths) + if res != null then return res + + # Found nothins? maybe it is a group... + var candidate = null + if path.file_exists then + var mgroup = get_mgroup(path) + if mgroup != null then + var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit") + if owner_path.file_exists then candidate = owner_path + end + end + + if candidate == null then + toolcontext.error(null, "Error: cannot find module `{path}`.") + return null + end + path = candidate end # Fast track, the path is already known @@ -375,6 +447,7 @@ class ModelBuilder end var res = new ModulePath(pn, path, mgroup) + mgroup.module_paths.add(res) identified_files[rp] = res return res @@ -392,11 +465,22 @@ class ModelBuilder return mgroups[rdp] end - # Hack, a dir is determined by the presence of a honomymous nit file + # Hack, a group is determined by: + # * the presence of a honomymous nit file + # * the fact that the directory is named `src` var pn = rdp.basename(".nit") var mp = dirpath.join_path(pn + ".nit").simplify_path - if not mp.file_exists then return null + var dirpath2 = dirpath + if not mp.file_exists then + if pn == "src" then + # With a src directory, the group name is the name of the parent directory + dirpath2 = rdp.dirname + pn = dirpath2.basename("") + else + return null + end + end # check parent directory var parentpath = dirpath.join_path("..").simplify_path @@ -413,6 +497,17 @@ class ModelBuilder mgroup = new MGroup(pn, parent.mproject, parent) toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2) end + var readme = dirpath2.join_path("README.md") + if not readme.file_exists then readme = dirpath2.join_path("README") + if readme.file_exists then + var mdoc = new MDoc + var s = new IFStream.open(readme) + while not s.eof do + mdoc.content.add(s.read_line) + end + mgroup.mdoc = mdoc + mdoc.original_mentity = mgroup + end mgroup.filepath = dirpath mgroups[rdp] = mgroup return mgroup @@ -474,7 +569,7 @@ class ModelBuilder end # Load it manually - var nmodule = load_module_ast(filename) + var nmodule = load_module_ast(file.filepath) if nmodule == null then return null # forward error # build the mmodule and load imported modules @@ -529,6 +624,15 @@ class ModelBuilder nmodules.add(nmodule) self.mmodule2nmodule[mmodule] = nmodule + if decl != null then + var ndoc = decl.n_doc + if ndoc != null then + var mdoc = ndoc.to_mdoc + mmodule.mdoc = mdoc + mdoc.original_mentity = mmodule + end + end + return mmodule end @@ -545,8 +649,15 @@ class ModelBuilder if not aimport isa AStdImport then continue end + var mgroup = mmodule.mgroup + if aimport.n_name.n_quad != null then mgroup = null # Start from top level + for grp in aimport.n_name.n_path do + var path = search_mmodule_by_name(grp, mgroup, grp.text) + if path == null then return # Skip error + mgroup = path.mgroup + end var mod_name = aimport.n_name.n_id.text - var sup = self.get_mmodule_by_name(aimport.n_name, mmodule, mod_name) + var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name) if sup == null then continue # Skip error aimport.mmodule = sup imported_modules.add(sup) @@ -555,6 +666,13 @@ class ModelBuilder error(aimport.n_visibility, "Error: only properties can be protected.") return end + if sup == mmodule then + error(aimport.n_name, "Error: Dependency loop in module {mmodule}.") + end + if sup.in_importation < mmodule then + error(aimport.n_name, "Error: Dependency loop between modules {mmodule} and {sup}.") + return + end mmodule.set_visibility_for(sup, mvisibility) end if stdimport then @@ -567,6 +685,18 @@ class ModelBuilder end self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3) mmodule.set_imported_mmodules(imported_modules) + + # TODO: Correctly check for useless importation + # It is even doable? + var directs = mmodule.in_importation.direct_greaters + for nim in nmodule.n_imports do + if not nim isa AStdImport then continue + var im = nim.mmodule + if im == null then continue + if directs.has(im) then continue + # This generates so much noise that it is simpler to just comment it + #warning(nim, "Warning: possible useless importation of {im}") + end end # All the loaded modules @@ -620,6 +750,10 @@ private class ModulePath redef fun to_s do return filepath end +redef class MGroup + # modules paths associated with the group + private var module_paths = new Array[ModulePath] +end redef class AStdImport # The imported module once determined @@ -649,3 +783,30 @@ end redef class APrivateVisibility redef fun mvisibility do return private_visibility end + +redef class ADoc + private var mdoc_cache: nullable MDoc + fun to_mdoc: MDoc + do + var res = mdoc_cache + if res != null then return res + res = new MDoc + for c in n_comment do + var text = c.text + if text.length < 2 then + res.content.add "" + continue + end + assert text.chars[0] == '#' + if text.chars[1] == ' ' then + text = text.substring_from(2) # eat starting `#` and space + else + text = text.substring_from(1) # eat atarting `#` only + end + if text.chars.last == '\n' then text = text.substring(0, text.length-1) # drop \n + res.content.add(text) + end + mdoc_cache = res + return res + end +end