redef class ToolContext
# Option --path
- readable var _opt_path: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
+ var opt_path: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
# Option --only-metamodel
- readable var _opt_only_metamodel: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
+ var opt_only_metamodel: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
# Option --only-parse
- readable var _opt_only_parse: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
+ var opt_only_parse: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
redef init
do
fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
private var modelbuilder_real: nullable ModelBuilder = null
+ fun run_global_phases(mainmodule: MModule)
+ do
+ for phase in phases_list do
+ phase.process_mainmodule(mainmodule)
+ end
+ end
end
+redef class Phase
+ # Specific action to execute on the whole program
+ # Called by the `ToolContext::run_global_phases`
+ # @toimplement
+ fun process_mainmodule(mainmodule: MModule) do end
+end
+
+
# A model builder knows how to load nit source files and build the associated model
class ModelBuilder
# The model where new modules, classes and properties are added
nmodules.add(mmodule2nmodule[mm])
end
toolcontext.run_phases(nmodules)
+
+ if toolcontext.opt_only_metamodel.value then
+ self.toolcontext.info("*** ONLY METAMODEL", 1)
+ exit(0)
+ end
end
# Instantiate a modelbuilder for a model and a toolcontext
end
# Load a bunch of modules.
- # `modules' can contains filenames or module names.
+ # `modules` can contains filenames or module names.
# Imported modules are automatically loaded and modelized.
# The result is the corresponding model elements.
# Errors and warnings are printed with the toolcontext.
var time0 = get_time
# Parse and recursively load
self.toolcontext.info("*** PARSE ***", 1)
- var mmodules = new Array[MModule]
+ var mmodules = new ArraySet[MModule]
for a in modules do
- var nmodule = self.load_module(null, a)
+ var nmodule = self.load_module(a)
if nmodule == null then continue # Skip error
mmodules.add(nmodule.mmodule.as(not null))
end
self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
self.toolcontext.check_errors
- return mmodules
+
+ if toolcontext.opt_only_parse.value then
+ self.toolcontext.info("*** ONLY PARSE...", 1)
+ exit(0)
+ end
+
+ return mmodules.to_a
end
- # Return a class named `name' visible by the module `mmodule'.
+ # Return a class named `name` visible by the module `mmodule`.
# Visibility in modules is correctly handled.
# If no such a class exists, then null is returned.
- # If more than one class exists, then an error on `anode' is displayed and null is returned.
+ # If more than one class exists, then an error on `anode` is displayed and null is returned.
# FIXME: add a way to handle class name conflict
fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
do
return res
end
- # Return a property named `name' on the type `mtype' visible in the module `mmodule'.
+ # Return a property named `name` on the type `mtype` visible in the module `mmodule`.
# Visibility in modules is correctly handled.
# Protected properties are returned (it is up to the caller to check and reject protected properties).
# If no such a property exists, then null is returned.
- # If more than one property exists, then an error on `anode' is displayed and null is returned.
+ # If more than one property exists, then an error on `anode` is displayed and null is returned.
# FIXME: add a way to handle property name conflict
fun try_get_mproperty_by_name2(anode: ANode, mmodule: MModule, mtype: MType, name: String): nullable MProperty
do
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.
+ # 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
do
- var origmmodule = mmodule
- var modules = model.get_mmodules_by_name(name)
-
+ # what path where tried to display on error message
var tries = new Array[String]
- var lastmodule = mmodule
- while mmodule != null do
- var dirname = mmodule.location.file.filename.dirname
-
- # Determine the owner
- var owner: nullable MModule
- if dirname.basename("") != mmodule.name then
- owner = mmodule.direct_owner
- else
- owner = mmodule
- end
-
- # First, try the already known nested modules
- if modules != null then
- for candidate in modules do
- if candidate.direct_owner == owner then
- return candidate
- end
+ # 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
- end
- # 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(owner, 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 an owner
- try_file = dirname + "/" + name + "/" + name + ".nit"
- if try_file.file_exists then
- var res = self.load_module(owner, try_file.simplify_path)
- if res == null then return null # Forward error
- return res.mmodule.as(not null)
- end
-
- lastmodule = mmodule
- mmodule = mmodule.direct_owner
- end
-
- if modules != null then
- for candidate in modules do
- if candidate.direct_owner == null then
- return candidate
+ # 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
+
+ mgroup = mgroup.parent
end
end
# Look at some known directories
var lookpaths = self.paths
- # Look in the directory of the last module also (event if not in the path)
- if lastmodule != null then
- var dirname = lastmodule.location.file.filename.dirname
- if dirname.basename("") == lastmodule.name then
- dirname = dirname.dirname
- end
- if not lookpaths.has(dirname) then
- lookpaths = lookpaths.to_a
- lookpaths.add(dirname)
+ # Look in the directory of module project also (even if not explicitely in the path)
+ if mmodule != null and mmodule.mgroup != null then
+ # path of the root group
+ var dirname = mmodule.mgroup.mproject.root.filepath
+ if dirname != null then
+ dirname = dirname.join_path("..").simplify_path
+ if not lookpaths.has(dirname) and dirname.file_exists then
+ lookpaths = lookpaths.to_a
+ lookpaths.add(dirname)
+ end
end
end
if candidate == null then
candidate = try_file
else if candidate != try_file then
- error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}")
+ # try to disambiguate conflicting modules
+ 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}")
+ end
end
end
try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
if candidate == null then
candidate = try_file
else if candidate != try_file then
- error(anode, "Error: conflicting module file for {name}: {candidate} {try_file}")
+ # try to disambiguate conflicting modules
+ 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}")
+ end
end
end
end
if candidate == null then
- if origmmodule != null then
- error(anode, "Error: cannot find module {name} from {origmmodule}. tried {tries.join(", ")}")
+ 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(mmodule, candidate)
+ var res = self.load_module(candidate)
if res == null then return null # Forward error
return res.mmodule.as(not null)
end
- # Try to load a module using a path.
+ # cache for `identify_file` by realpath
+ private var identified_files = new HashMap[String, nullable ModulePath]
+
+ # Identify a source file
+ # 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
+ end
+
+ # Fast track, the path is already known
+ var pn = path.basename(".nit")
+ var rp = module_absolute_path(path)
+ if identified_files.has_key(rp) then return identified_files[rp]
+
+ # Search for a group
+ var mgrouppath = path.join_path("..").simplify_path
+ var mgroup = get_mgroup(mgrouppath)
+
+ if mgroup == null then
+ # singleton project
+ var mproject = new MProject(pn, model)
+ mgroup = new MGroup(pn, mproject, null) # same name for the root group
+ mgroup.filepath = path
+ mproject.root = mgroup
+ toolcontext.info("found project `{pn}` at {path}", 2)
+ end
+
+ var res = new ModulePath(pn, path, mgroup)
+
+ identified_files[rp] = res
+ return res
+ end
+
+ # groups by path
+ private var mgroups = new HashMap[String, nullable MGroup]
+
+ # return the mgroup associated to a directory path
+ # if the directory is not a group null is returned
+ private fun get_mgroup(dirpath: String): nullable MGroup
+ do
+ var rdp = module_absolute_path(dirpath)
+ if mgroups.has_key(rdp) then
+ return mgroups[rdp]
+ end
+
+ # Hack, a dir is determined by the presence of a honomymous nit file
+ var pn = rdp.basename(".nit")
+ var mp = dirpath.join_path(pn + ".nit").simplify_path
+
+ if not mp.file_exists then return null
+
+ # check parent directory
+ var parentpath = dirpath.join_path("..").simplify_path
+ var parent = get_mgroup(parentpath)
+
+ var mgroup
+ if parent == null then
+ # no parent, thus new project
+ var mproject = new MProject(pn, model)
+ mgroup = new MGroup(pn, mproject, null) # same name for the root group
+ mproject.root = mgroup
+ toolcontext.info("found project `{mproject}` at {dirpath}", 2)
+ else
+ mgroup = new MGroup(pn, parent.mproject, parent)
+ toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
+ end
+ mgroup.filepath = dirpath
+ mgroups[rdp] = mgroup
+ return mgroup
+ end
+
+ # Transform relative paths (starting with '../') into absolute paths
+ private fun module_absolute_path(path: String): String do
+ return getcwd.join_path(path).simplify_path
+ end
+
+ # Try to load a module AST using a path.
# Display an error if there is a problem (IO / lexer / parser) and return null
- # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
- fun load_module(owner: nullable MModule, filename: String): nullable AModule
+ fun load_module_ast(filename: String): nullable AModule
do
+ if filename.file_extension != "nit" then
+ self.toolcontext.error(null, "Error: file {filename} is not a valid nit module.")
+ return null
+ end
if not filename.file_exists then
self.toolcontext.error(null, "Error: file {filename} not found.")
return null
end
- var x = if owner != null then owner.to_s else "."
- self.toolcontext.info("load module {filename} in {x}", 2)
+ self.toolcontext.info("load module {filename}", 2)
# Load the file
var file = new IFStream.open(filename)
var parser = new Parser(lexer)
var tree = parser.parse
file.close
+ var mod_name = filename.basename(".nit")
# Handle lexer and parser error
var nmodule = tree.n_base
return null
end
+ return nmodule
+ end
+
+ # Try to load a module and its imported modules using a path.
+ # Display an error if there is a problem (IO / lexer / parser / importation) and return null
+ # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
+ fun load_module(filename: String): nullable AModule
+ do
+ # Look for the module
+ var file = identify_file(filename)
+ if file == null then return null # forward error
+
+ # Already known and loaded? then return it
+ var mmodule = file.mmodule
+ if mmodule != null then
+ return mmodule2nmodule[mmodule]
+ end
+
+ # Load it manually
+ var nmodule = load_module_ast(filename)
+ if nmodule == null then return null # forward error
+
+ # build the mmodule and load imported modules
+ mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
+
+ if mmodule == null then return null # forward error
+
+ # Update the file information
+ file.mmodule = mmodule
+
+ return nmodule
+ end
+
+ fun load_rt_module(parent: MModule, nmodule: AModule, mod_name: String): nullable AModule
+ do
+ # Create the module
+ var mmodule = new MModule(model, parent.mgroup, mod_name, nmodule.location)
+ nmodule.mmodule = mmodule
+ nmodules.add(nmodule)
+ self.mmodule2nmodule[mmodule] = nmodule
+
+ var imported_modules = new Array[MModule]
+
+ imported_modules.add(parent)
+ mmodule.set_visibility_for(parent, intrude_visibility)
+
+ mmodule.set_imported_mmodules(imported_modules)
+
+ return nmodule
+ end
+
+ # Visit the AST and create the `MModule` object
+ # Then, recursively load imported modules
+ private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
+ do
# Check the module name
- var mod_name = filename.basename(".nit")
var decl = nmodule.n_moduledecl
if decl == null then
#warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
end
# Create the module
- var mmodule = new MModule(model, owner, mod_name, nmodule.location)
+ var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
nmodule.mmodule = mmodule
nmodules.add(nmodule)
self.mmodule2nmodule[mmodule] = nmodule
build_module_importation(nmodule)
- return nmodule
+ return mmodule
end
# Analysis the module importation and fill the module_importation_hierarchy
var mod_name = aimport.n_name.n_id.text
var sup = self.get_mmodule_by_name(aimport.n_name, mmodule, mod_name)
if sup == null then continue # Skip error
+ aimport.mmodule = sup
imported_modules.add(sup)
var mvisibility = aimport.n_visibility.mvisibility
+ if mvisibility == protected_visibility then
+ error(aimport.n_visibility, "Error: only properties can be protected.")
+ return
+ end
mmodule.set_visibility_for(sup, mvisibility)
end
if stdimport then
var nmodules: Array[AModule] = new Array[AModule]
# Register the nmodule associated to each mmodule
- # FIXME: why not refine the MModule class with a nullable attribute?
+ # FIXME: why not refine the `MModule` class with a nullable attribute?
var mmodule2nmodule: HashMap[MModule, AModule] = new HashMap[MModule, AModule]
# Helper function to display an error on a node.
- # Alias for `self.toolcontext.error(n.hot_location, text)'
+ # Alias for `self.toolcontext.error(n.hot_location, text)`
fun error(n: ANode, text: String)
do
self.toolcontext.error(n.hot_location, text)
end
# Helper function to display a warning on a node.
- # Alias for: `self.toolcontext.warning(n.hot_location, text)'
+ # Alias for: `self.toolcontext.warning(n.hot_location, text)`
fun warning(n: ANode, text: String)
do
self.toolcontext.warning(n.hot_location, text)
end
- # Force to get the primitive method named `name' on the type `recv' or do a fatal error on `n'
- fun force_get_primitive_method(n: ANode, name: String, recv: MType, mmodule: MModule): MMethod
+ # Force to get the primitive method named `name` on the type `recv` or do a fatal error on `n`
+ fun force_get_primitive_method(n: ANode, name: String, recv: MClass, mmodule: MModule): MMethod
do
var res = mmodule.try_get_primitive_method(name, recv)
if res == null then
end
end
+# placeholder to a module file identified but not always loaded in a project
+private class ModulePath
+ # The name of the module
+ # (it's the basename of the filepath)
+ var name: String
+
+ # The human path of the module
+ var filepath: String
+
+ # The group (and the project) of the possible module
+ var mgroup: MGroup
+
+ # The loaded module (if any)
+ var mmodule: nullable MModule = null
+
+ redef fun to_s do return filepath
+end
+
+
+redef class AStdImport
+ # The imported module once determined
+ var mmodule: nullable MModule = null
+end
+
redef class AModule
- # The associated MModule once build by a `ModelBuilder'
+ # The associated MModule once build by a `ModelBuilder`
var mmodule: nullable MModule
# Flag that indicate if the importation is already completed
var is_importation_done: Bool = false