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.
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
# 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 `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.
- # FIXME: add a way to handle module name conflict
- fun get_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, 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
# First, look in groups
var c = mgroup
# Second, try the directory to find a file
var try_file = dirname + "/" + name + ".nit"
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)
+ var res = self.identify_file(try_file.simplify_path)
+ assert res != null
+ return res
end
# 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.load_module(try_file.simplify_path)
- if res == null then return null # Forward error
- return res.mmodule.as(not null)
+ var res = self.identify_file(try_file.simplify_path)
+ assert res != null
+ return res
end
c = c.parent
end
return null
end
- var res = self.load_module(candidate)
+ 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
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
+ if not mp.file_exists then
+ if pn == "src" then
+ # With a src directory, the group name is the name of the parent directory
+ pn = rdp.dirname.basename("")
+ else
+ return null
+ end
+ end
# check parent directory
var parentpath = dirpath.join_path("..").simplify_path
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.mgroup, 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)