modelbuilder: split in 3 modules
[nit.git] / src / loader.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 module loader
19
20 import modelbuilder_base
21
22 redef class ToolContext
23 # Option --path
24 var opt_path = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
25
26 # Option --only-metamodel
27 var opt_only_metamodel = new OptionBool("Stop after meta-model processing", "--only-metamodel")
28
29 # Option --only-parse
30 var opt_only_parse = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
31
32 redef init
33 do
34 super
35 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel)
36 end
37 end
38
39 redef class ModelBuilder
40 redef init
41 do
42 super
43
44 # Setup the paths value
45 paths.append(toolcontext.opt_path.value)
46
47 var path_env = "NIT_PATH".environ
48 if not path_env.is_empty then
49 paths.append(path_env.split_with(':'))
50 end
51
52 var nit_dir = toolcontext.nit_dir
53 var libname = "{nit_dir}/lib"
54 if libname.file_exists then paths.add(libname)
55 end
56
57 # Load a bunch of modules.
58 # `modules` can contains filenames or module names.
59 # Imported modules are automatically loaded and modelized.
60 # The result is the corresponding model elements.
61 # Errors and warnings are printed with the toolcontext.
62 #
63 # Note: class and property model element are not analysed.
64 fun parse(modules: Sequence[String]): Array[MModule]
65 do
66 var time0 = get_time
67 # Parse and recursively load
68 self.toolcontext.info("*** PARSE ***", 1)
69 var mmodules = new ArraySet[MModule]
70 for a in modules do
71 var nmodule = self.load_module(a)
72 if nmodule == null then continue # Skip error
73 mmodules.add(nmodule.mmodule.as(not null))
74 end
75 var time1 = get_time
76 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
77
78 self.toolcontext.check_errors
79
80 if toolcontext.opt_only_parse.value then
81 self.toolcontext.info("*** ONLY PARSE...", 1)
82 exit(0)
83 end
84
85 return mmodules.to_a
86 end
87
88 # The list of directories to search for top level modules
89 # The list is initially set with :
90 # * the toolcontext --path option
91 # * the NIT_PATH environment variable
92 # * `toolcontext.nit_dir`
93 # Path can be added (or removed) by the client
94 var paths = new Array[String]
95
96 # Like (an used by) `get_mmodule_by_name` but just return the ModulePath
97 private fun search_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
98 do
99 # First, look in groups
100 var c = mgroup
101 while c != null do
102 var dirname = c.filepath
103 if dirname == null then break # virtual group
104 if dirname.has_suffix(".nit") then break # singleton project
105
106 # Second, try the directory to find a file
107 var try_file = dirname + "/" + name + ".nit"
108 if try_file.file_exists then
109 var res = self.identify_file(try_file.simplify_path)
110 assert res != null
111 return res
112 end
113
114 # Third, try if the requested module is itself a group
115 try_file = dirname + "/" + name + "/" + name + ".nit"
116 if try_file.file_exists then
117 var res = self.identify_file(try_file.simplify_path)
118 assert res != null
119 return res
120 end
121
122 c = c.parent
123 end
124
125 # Look at some known directories
126 var lookpaths = self.paths
127
128 # Look in the directory of the group project also (even if not explicitely in the path)
129 if mgroup != null then
130 # path of the root group
131 var dirname = mgroup.mproject.root.filepath
132 if dirname != null then
133 dirname = dirname.join_path("..").simplify_path
134 if not lookpaths.has(dirname) and dirname.file_exists then
135 lookpaths = lookpaths.to_a
136 lookpaths.add(dirname)
137 end
138 end
139 end
140
141 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
142
143 if candidate == null then
144 if mgroup != null then
145 error(anode, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}")
146 else
147 error(anode, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
148 end
149 return null
150 end
151 return candidate
152 end
153
154 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
155 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
156 # if `mgroup` is null then the module is searched in the top level only.
157 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
158 fun get_mmodule_by_name(anode: ANode, mgroup: nullable MGroup, name: String): nullable MModule
159 do
160 var path = search_mmodule_by_name(anode, mgroup, name)
161 if path == null then return null # Forward error
162 var res = self.load_module(path.filepath)
163 if res == null then return null # Forward error
164 return res.mmodule.as(not null)
165 end
166
167 # Search a module `name` from path `lookpaths`.
168 # If found, the path of the file is returned
169 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
170 do
171 var candidate: nullable String = null
172 for dirname in lookpaths do
173 var try_file = (dirname + "/" + name + ".nit").simplify_path
174 if try_file.file_exists then
175 if candidate == null then
176 candidate = try_file
177 else if candidate != try_file then
178 # try to disambiguate conflicting modules
179 var abs_candidate = module_absolute_path(candidate)
180 var abs_try_file = module_absolute_path(try_file)
181 if abs_candidate != abs_try_file then
182 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
183 end
184 end
185 end
186 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
187 if try_file.file_exists then
188 if candidate == null then
189 candidate = try_file
190 else if candidate != try_file then
191 # try to disambiguate conflicting modules
192 var abs_candidate = module_absolute_path(candidate)
193 var abs_try_file = module_absolute_path(try_file)
194 if abs_candidate != abs_try_file then
195 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
196 end
197 end
198 end
199 end
200 if candidate == null then return null
201 return identify_file(candidate)
202 end
203
204 # cache for `identify_file` by realpath
205 private var identified_files = new HashMap[String, nullable ModulePath]
206
207 # Identify a source file
208 # Load the associated project and groups if required
209 private fun identify_file(path: String): nullable ModulePath
210 do
211 # special case for not a nit file
212 if path.file_extension != "nit" then
213 # search in known -I paths
214 var res = search_module_in_paths(null, path, self.paths)
215 if res != null then return res
216
217 # Found nothins? maybe it is a group...
218 var candidate = null
219 if path.file_exists then
220 var mgroup = get_mgroup(path)
221 if mgroup != null then
222 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
223 if owner_path.file_exists then candidate = owner_path
224 end
225 end
226
227 if candidate == null then
228 toolcontext.error(null, "Error: cannot find module `{path}`.")
229 return null
230 end
231 path = candidate
232 end
233
234 # Fast track, the path is already known
235 var pn = path.basename(".nit")
236 var rp = module_absolute_path(path)
237 if identified_files.has_key(rp) then return identified_files[rp]
238
239 # Search for a group
240 var mgrouppath = path.join_path("..").simplify_path
241 var mgroup = get_mgroup(mgrouppath)
242
243 if mgroup == null then
244 # singleton project
245 var mproject = new MProject(pn, model)
246 mgroup = new MGroup(pn, mproject, null) # same name for the root group
247 mgroup.filepath = path
248 mproject.root = mgroup
249 toolcontext.info("found project `{pn}` at {path}", 2)
250 end
251
252 var res = new ModulePath(pn, path, mgroup)
253 mgroup.module_paths.add(res)
254
255 identified_files[rp] = res
256 return res
257 end
258
259 # groups by path
260 private var mgroups = new HashMap[String, nullable MGroup]
261
262 # return the mgroup associated to a directory path
263 # if the directory is not a group null is returned
264 private fun get_mgroup(dirpath: String): nullable MGroup
265 do
266 var rdp = module_absolute_path(dirpath)
267 if mgroups.has_key(rdp) then
268 return mgroups[rdp]
269 end
270
271 # Hack, a group is determined by:
272 # * the presence of a honomymous nit file
273 # * the fact that the directory is named `src`
274 var pn = rdp.basename(".nit")
275 var mp = dirpath.join_path(pn + ".nit").simplify_path
276
277 var dirpath2 = dirpath
278 if not mp.file_exists then
279 if pn == "src" then
280 # With a src directory, the group name is the name of the parent directory
281 dirpath2 = rdp.dirname
282 pn = dirpath2.basename("")
283 else
284 return null
285 end
286 end
287
288 # check parent directory
289 var parentpath = dirpath.join_path("..").simplify_path
290 var parent = get_mgroup(parentpath)
291
292 var mgroup
293 if parent == null then
294 # no parent, thus new project
295 var mproject = new MProject(pn, model)
296 mgroup = new MGroup(pn, mproject, null) # same name for the root group
297 mproject.root = mgroup
298 toolcontext.info("found project `{mproject}` at {dirpath}", 2)
299 else
300 mgroup = new MGroup(pn, parent.mproject, parent)
301 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
302 end
303 var readme = dirpath2.join_path("README.md")
304 if not readme.file_exists then readme = dirpath2.join_path("README")
305 if readme.file_exists then
306 var mdoc = new MDoc
307 var s = new IFStream.open(readme)
308 while not s.eof do
309 mdoc.content.add(s.read_line)
310 end
311 mgroup.mdoc = mdoc
312 mdoc.original_mentity = mgroup
313 end
314 mgroup.filepath = dirpath
315 mgroups[rdp] = mgroup
316 return mgroup
317 end
318
319 # Transform relative paths (starting with '../') into absolute paths
320 private fun module_absolute_path(path: String): String do
321 return getcwd.join_path(path).simplify_path
322 end
323
324 # Try to load a module AST using a path.
325 # Display an error if there is a problem (IO / lexer / parser) and return null
326 fun load_module_ast(filename: String): nullable AModule
327 do
328 if filename.file_extension != "nit" then
329 self.toolcontext.error(null, "Error: file {filename} is not a valid nit module.")
330 return null
331 end
332 if not filename.file_exists then
333 self.toolcontext.error(null, "Error: file {filename} not found.")
334 return null
335 end
336
337 self.toolcontext.info("load module {filename}", 2)
338
339 # Load the file
340 var file = new IFStream.open(filename)
341 var lexer = new Lexer(new SourceFile(filename, file))
342 var parser = new Parser(lexer)
343 var tree = parser.parse
344 file.close
345
346 # Handle lexer and parser error
347 var nmodule = tree.n_base
348 if nmodule == null then
349 var neof = tree.n_eof
350 assert neof isa AError
351 error(neof, neof.message)
352 return null
353 end
354
355 return nmodule
356 end
357
358 # Try to load a module and its imported modules using a path.
359 # Display an error if there is a problem (IO / lexer / parser / importation) and return null
360 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
361 fun load_module(filename: String): nullable AModule
362 do
363 # Look for the module
364 var file = identify_file(filename)
365 if file == null then return null # forward error
366
367 # Already known and loaded? then return it
368 var mmodule = file.mmodule
369 if mmodule != null then
370 return mmodule2nmodule[mmodule]
371 end
372
373 # Load it manually
374 var nmodule = load_module_ast(file.filepath)
375 if nmodule == null then return null # forward error
376
377 # build the mmodule and load imported modules
378 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
379
380 if mmodule == null then return null # forward error
381
382 # Update the file information
383 file.mmodule = mmodule
384
385 # Load imported module
386 build_module_importation(nmodule)
387
388 return nmodule
389 end
390
391 # Injection of a new module without source.
392 # Used by the interpreted
393 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
394 do
395 # Create the module
396
397 var mgroup = null
398 if parent != null then mgroup = parent.mgroup
399 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
400 nmodule.mmodule = mmodule
401 nmodules.add(nmodule)
402 self.mmodule2nmodule[mmodule] = nmodule
403
404 if parent!= null then
405 var imported_modules = new Array[MModule]
406 imported_modules.add(parent)
407 mmodule.set_visibility_for(parent, intrude_visibility)
408 mmodule.set_imported_mmodules(imported_modules)
409 else
410 build_module_importation(nmodule)
411 end
412
413 return nmodule
414 end
415
416 # Visit the AST and create the `MModule` object
417 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
418 do
419 # Check the module name
420 var decl = nmodule.n_moduledecl
421 if decl == null then
422 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
423 else
424 var decl_name = decl.n_name.n_id.text
425 if decl_name != mod_name then
426 error(decl.n_name, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
427 end
428 end
429
430 # Create the module
431 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
432 nmodule.mmodule = mmodule
433 nmodules.add(nmodule)
434 self.mmodule2nmodule[mmodule] = nmodule
435
436 if decl != null then
437 var ndoc = decl.n_doc
438 if ndoc != null then
439 var mdoc = ndoc.to_mdoc
440 mmodule.mdoc = mdoc
441 mdoc.original_mentity = mmodule
442 else
443 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
444 end
445 end
446
447 return mmodule
448 end
449
450 # Analysis the module importation and fill the module_importation_hierarchy
451 private fun build_module_importation(nmodule: AModule)
452 do
453 if nmodule.is_importation_done then return
454 nmodule.is_importation_done = true
455 var mmodule = nmodule.mmodule.as(not null)
456 var stdimport = true
457 var imported_modules = new Array[MModule]
458 for aimport in nmodule.n_imports do
459 stdimport = false
460 if not aimport isa AStdImport then
461 continue
462 end
463 var mgroup = mmodule.mgroup
464 if aimport.n_name.n_quad != null then mgroup = null # Start from top level
465 for grp in aimport.n_name.n_path do
466 var path = search_mmodule_by_name(grp, mgroup, grp.text)
467 if path == null then return # Skip error
468 mgroup = path.mgroup
469 end
470 var mod_name = aimport.n_name.n_id.text
471 var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name)
472 if sup == null then continue # Skip error
473 aimport.mmodule = sup
474 imported_modules.add(sup)
475 var mvisibility = aimport.n_visibility.mvisibility
476 if mvisibility == protected_visibility then
477 error(aimport.n_visibility, "Error: only properties can be protected.")
478 return
479 end
480 if sup == mmodule then
481 error(aimport.n_name, "Error: Dependency loop in module {mmodule}.")
482 end
483 if sup.in_importation < mmodule then
484 error(aimport.n_name, "Error: Dependency loop between modules {mmodule} and {sup}.")
485 return
486 end
487 mmodule.set_visibility_for(sup, mvisibility)
488 end
489 if stdimport then
490 var mod_name = "standard"
491 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
492 if sup != null then # Skip error
493 imported_modules.add(sup)
494 mmodule.set_visibility_for(sup, public_visibility)
495 end
496 end
497 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
498 mmodule.set_imported_mmodules(imported_modules)
499
500 # Force standard to be public if imported
501 for sup in mmodule.in_importation.greaters do
502 if sup.name == "standard" then
503 mmodule.set_visibility_for(sup, public_visibility)
504 end
505 end
506
507 # TODO: Correctly check for useless importation
508 # It is even doable?
509 var directs = mmodule.in_importation.direct_greaters
510 for nim in nmodule.n_imports do
511 if not nim isa AStdImport then continue
512 var im = nim.mmodule
513 if im == null then continue
514 if directs.has(im) then continue
515 # This generates so much noise that it is simpler to just comment it
516 #warning(nim, "Warning: possible useless importation of {im}")
517 end
518 end
519
520 # All the loaded modules
521 var nmodules = new Array[AModule]
522
523 # Register the nmodule associated to each mmodule
524 # FIXME: why not refine the `MModule` class with a nullable attribute?
525 var mmodule2nmodule = new HashMap[MModule, AModule]
526 end
527
528 # placeholder to a module file identified but not always loaded in a project
529 private class ModulePath
530 # The name of the module
531 # (it's the basename of the filepath)
532 var name: String
533
534 # The human path of the module
535 var filepath: String
536
537 # The group (and the project) of the possible module
538 var mgroup: MGroup
539
540 # The loaded module (if any)
541 var mmodule: nullable MModule = null
542
543 redef fun to_s do return filepath
544 end
545
546 redef class MGroup
547 # modules paths associated with the group
548 private var module_paths = new Array[ModulePath]
549 end
550
551 redef class AStdImport
552 # The imported module once determined
553 var mmodule: nullable MModule = null
554 end
555
556 redef class AModule
557 # The associated MModule once build by a `ModelBuilder`
558 var mmodule: nullable MModule
559 # Flag that indicate if the importation is already completed
560 var is_importation_done: Bool = false
561 end