loader: new method `ModelBuilder::mmodule2node` to protect the access to `mmodule2nmo...
[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 # Loading of Nit source files
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 elements 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 # Load imported module
74 build_module_importation(nmodule)
75
76 mmodules.add(nmodule.mmodule.as(not null))
77 end
78 var time1 = get_time
79 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
80
81 self.toolcontext.check_errors
82
83 if toolcontext.opt_only_parse.value then
84 self.toolcontext.info("*** ONLY PARSE...", 1)
85 exit(0)
86 end
87
88 return mmodules.to_a
89 end
90
91 # The list of directories to search for top level modules
92 # The list is initially set with:
93 #
94 # * the toolcontext --path option
95 # * the NIT_PATH environment variable
96 # * `toolcontext.nit_dir`
97 # Path can be added (or removed) by the client
98 var paths = new Array[String]
99
100 # Like (and used by) `get_mmodule_by_name` but just return the ModulePath
101 fun search_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
102 do
103 # First, look in groups
104 var c = mgroup
105 while c != null do
106 var dirname = c.filepath
107 if dirname == null then break # virtual group
108 if dirname.has_suffix(".nit") then break # singleton project
109
110 # Second, try the directory to find a file
111 var try_file = dirname + "/" + name + ".nit"
112 if try_file.file_exists then
113 var res = self.identify_file(try_file.simplify_path)
114 assert res != null
115 return res
116 end
117
118 # Third, try if the requested module is itself a group
119 try_file = dirname + "/" + name + "/" + name + ".nit"
120 if try_file.file_exists then
121 var res = self.identify_file(try_file.simplify_path)
122 assert res != null
123 return res
124 end
125
126 c = c.parent
127 end
128
129 # Look at some known directories
130 var lookpaths = self.paths
131
132 # Look in the directory of the group project also (even if not explicitly in the path)
133 if mgroup != null then
134 # path of the root group
135 var dirname = mgroup.mproject.root.filepath
136 if dirname != null then
137 dirname = dirname.join_path("..").simplify_path
138 if not lookpaths.has(dirname) and dirname.file_exists then
139 lookpaths = lookpaths.to_a
140 lookpaths.add(dirname)
141 end
142 end
143 end
144
145 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
146
147 if candidate == null then
148 if mgroup != null then
149 error(anode, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}")
150 else
151 error(anode, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
152 end
153 return null
154 end
155 return candidate
156 end
157
158 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
159 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
160 # if `mgroup` is null then the module is searched in the top level only.
161 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
162 fun get_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable MModule
163 do
164 var path = search_mmodule_by_name(anode, mgroup, name)
165 if path == null then return null # Forward error
166 var res = self.load_module(path.filepath)
167 if res == null then return null # Forward error
168 # Load imported module
169 build_module_importation(res)
170 return res.mmodule.as(not null)
171 end
172
173 # Search a module `name` from path `lookpaths`.
174 # If found, the path of the file is returned
175 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
176 do
177 var candidate: nullable String = null
178 for dirname in lookpaths do
179 var try_file = (dirname + "/" + name + ".nit").simplify_path
180 if try_file.file_exists then
181 if candidate == null then
182 candidate = try_file
183 else if candidate != try_file then
184 # try to disambiguate conflicting modules
185 var abs_candidate = module_absolute_path(candidate)
186 var abs_try_file = module_absolute_path(try_file)
187 if abs_candidate != abs_try_file then
188 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
189 end
190 end
191 end
192 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
193 if try_file.file_exists then
194 if candidate == null then
195 candidate = try_file
196 else if candidate != try_file then
197 # try to disambiguate conflicting modules
198 var abs_candidate = module_absolute_path(candidate)
199 var abs_try_file = module_absolute_path(try_file)
200 if abs_candidate != abs_try_file then
201 toolcontext.error(location, "Error: conflicting module file for {name}: {candidate} {try_file}")
202 end
203 end
204 end
205 end
206 if candidate == null then return null
207 return identify_file(candidate)
208 end
209
210 # Cache for `identify_file` by realpath
211 private var identified_files_by_path = new HashMap[String, nullable ModulePath]
212
213 # All the currently identified modules.
214 # See `identify_file`.
215 var identified_files = new Array[ModulePath]
216
217 # Identify a source file
218 # Load the associated project and groups if required
219 #
220 # Silently return `null` if `path` is not a valid module path.
221 fun identify_file(path: String): nullable ModulePath
222 do
223 # special case for not a nit file
224 if path.file_extension != "nit" then
225 # search dirless files in known -I paths
226 if path.dirname == "" then
227 var res = search_module_in_paths(null, path, self.paths)
228 if res != null then return res
229 end
230
231 # Found nothing? maybe it is a group...
232 var candidate = null
233 if path.file_exists then
234 var mgroup = get_mgroup(path)
235 if mgroup != null then
236 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
237 if owner_path.file_exists then candidate = owner_path
238 end
239 end
240
241 if candidate == null then
242 return null
243 end
244 path = candidate
245 end
246
247 # Fast track, the path is already known
248 var pn = path.basename(".nit")
249 var rp = module_absolute_path(path)
250 if identified_files_by_path.has_key(rp) then return identified_files_by_path[rp]
251
252 # Search for a group
253 var mgrouppath = path.join_path("..").simplify_path
254 var mgroup = get_mgroup(mgrouppath)
255
256 if mgroup == null then
257 # singleton project
258 var mproject = new MProject(pn, model)
259 mgroup = new MGroup(pn, mproject, null) # same name for the root group
260 mgroup.filepath = path
261 mproject.root = mgroup
262 toolcontext.info("found project `{pn}` at {path}", 2)
263 end
264
265 var res = new ModulePath(pn, path, mgroup)
266 mgroup.module_paths.add(res)
267
268 identified_files_by_path[rp] = res
269 identified_files.add(res)
270 return res
271 end
272
273 # Groups by path
274 private var mgroups = new HashMap[String, nullable MGroup]
275
276 # Return the mgroup associated to a directory path.
277 # If the directory is not a group null is returned.
278 fun get_mgroup(dirpath: String): nullable MGroup
279 do
280 var rdp = module_absolute_path(dirpath)
281 if mgroups.has_key(rdp) then
282 return mgroups[rdp]
283 end
284
285 # Hack, a group is determined by:
286 # * the presence of a honomymous nit file
287 # * the fact that the directory is named `src`
288 var pn = rdp.basename(".nit")
289 var mp = dirpath.join_path(pn + ".nit").simplify_path
290
291 var dirpath2 = dirpath
292 if not mp.file_exists then
293 if pn == "src" then
294 # With a src directory, the group name is the name of the parent directory
295 dirpath2 = rdp.dirname
296 pn = dirpath2.basename("")
297 else
298 return null
299 end
300 end
301
302 # check parent directory
303 var parentpath = dirpath.join_path("..").simplify_path
304 var parent = get_mgroup(parentpath)
305
306 var mgroup
307 if parent == null then
308 # no parent, thus new project
309 var mproject = new MProject(pn, model)
310 mgroup = new MGroup(pn, mproject, null) # same name for the root group
311 mproject.root = mgroup
312 toolcontext.info("found project `{mproject}` at {dirpath}", 2)
313 else
314 mgroup = new MGroup(pn, parent.mproject, parent)
315 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
316 end
317 var readme = dirpath2.join_path("README.md")
318 if not readme.file_exists then readme = dirpath2.join_path("README")
319 if readme.file_exists then
320 var mdoc = new MDoc
321 var s = new IFStream.open(readme)
322 while not s.eof do
323 mdoc.content.add(s.read_line)
324 end
325 mgroup.mdoc = mdoc
326 mdoc.original_mentity = mgroup
327 end
328 mgroup.filepath = dirpath
329 mgroups[rdp] = mgroup
330 return mgroup
331 end
332
333 # Force the identification of all ModulePath of the group and sub-groups.
334 fun visit_group(mgroup: MGroup) do
335 var p = mgroup.filepath
336 for f in p.files do
337 var fp = p/f
338 var g = get_mgroup(fp)
339 if g != null then visit_group(g)
340 identify_file(fp)
341 end
342 end
343
344 # Transform relative paths (starting with '../') into absolute paths
345 private fun module_absolute_path(path: String): String do
346 return getcwd.join_path(path).simplify_path
347 end
348
349 # Try to load a module AST using a path.
350 # Display an error if there is a problem (IO / lexer / parser) and return null
351 fun load_module_ast(filename: String): nullable AModule
352 do
353 if filename.file_extension != "nit" then
354 self.toolcontext.error(null, "Error: file {filename} is not a valid nit module.")
355 return null
356 end
357 if not filename.file_exists then
358 self.toolcontext.error(null, "Error: file {filename} not found.")
359 return null
360 end
361
362 self.toolcontext.info("load module {filename}", 2)
363
364 # Load the file
365 var file = new IFStream.open(filename)
366 var lexer = new Lexer(new SourceFile(filename, file))
367 var parser = new Parser(lexer)
368 var tree = parser.parse
369 file.close
370
371 # Handle lexer and parser error
372 var nmodule = tree.n_base
373 if nmodule == null then
374 var neof = tree.n_eof
375 assert neof isa AError
376 error(neof, neof.message)
377 return null
378 end
379
380 return nmodule
381 end
382
383 # Try to load a module using a path.
384 # Display an error if there is a problem (IO / lexer / parser) and return null.
385 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
386 #
387 # The MModule is created however, the importation is not performed,
388 # therefore you should call `build_module_importation`.
389 fun load_module(filename: String): nullable AModule
390 do
391 # Look for the module
392 var file = identify_file(filename)
393 if file == null then
394 toolcontext.error(null, "Error: cannot find module `{filename}`.")
395 return null
396 end
397
398 # Already known and loaded? then return it
399 var mmodule = file.mmodule
400 if mmodule != null then
401 return mmodule2nmodule[mmodule]
402 end
403
404 # Load it manually
405 var nmodule = load_module_ast(file.filepath)
406 if nmodule == null then return null # forward error
407
408 # build the mmodule and load imported modules
409 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
410
411 if mmodule == null then return null # forward error
412
413 # Update the file information
414 file.mmodule = mmodule
415
416 return nmodule
417 end
418
419 # Injection of a new module without source.
420 # Used by the interpreter.
421 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
422 do
423 # Create the module
424
425 var mgroup = null
426 if parent != null then mgroup = parent.mgroup
427 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
428 nmodule.mmodule = mmodule
429 nmodules.add(nmodule)
430 self.mmodule2nmodule[mmodule] = nmodule
431
432 if parent!= null then
433 var imported_modules = new Array[MModule]
434 imported_modules.add(parent)
435 mmodule.set_visibility_for(parent, intrude_visibility)
436 mmodule.set_imported_mmodules(imported_modules)
437 else
438 build_module_importation(nmodule)
439 end
440
441 return nmodule
442 end
443
444 # Visit the AST and create the `MModule` object
445 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
446 do
447 # Check the module name
448 var decl = nmodule.n_moduledecl
449 if decl == null then
450 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
451 else
452 var decl_name = decl.n_name.n_id.text
453 if decl_name != mod_name then
454 error(decl.n_name, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
455 end
456 end
457
458 # Create the module
459 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
460 nmodule.mmodule = mmodule
461 nmodules.add(nmodule)
462 self.mmodule2nmodule[mmodule] = nmodule
463
464 if decl != null then
465 var ndoc = decl.n_doc
466 if ndoc != null then
467 var mdoc = ndoc.to_mdoc
468 mmodule.mdoc = mdoc
469 mdoc.original_mentity = mmodule
470 else
471 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
472 end
473 end
474
475 return mmodule
476 end
477
478 # Analyze the module importation and fill the module_importation_hierarchy
479 #
480 # Unless you used `load_module`, the importation is already done and this method does a no-op.
481 fun build_module_importation(nmodule: AModule)
482 do
483 if nmodule.is_importation_done then return
484 nmodule.is_importation_done = true
485 var mmodule = nmodule.mmodule.as(not null)
486 var stdimport = true
487 var imported_modules = new Array[MModule]
488 for aimport in nmodule.n_imports do
489 stdimport = false
490 if not aimport isa AStdImport then
491 continue
492 end
493 var mgroup = mmodule.mgroup
494 if aimport.n_name.n_quad != null then mgroup = null # Start from top level
495 for grp in aimport.n_name.n_path do
496 var path = search_mmodule_by_name(grp, mgroup, grp.text)
497 if path == null then return # Skip error
498 mgroup = path.mgroup
499 end
500 var mod_name = aimport.n_name.n_id.text
501 var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name)
502 if sup == null then continue # Skip error
503 aimport.mmodule = sup
504 imported_modules.add(sup)
505 var mvisibility = aimport.n_visibility.mvisibility
506 if mvisibility == protected_visibility then
507 error(aimport.n_visibility, "Error: only properties can be protected.")
508 return
509 end
510 if sup == mmodule then
511 error(aimport.n_name, "Error: Dependency loop in module {mmodule}.")
512 end
513 if sup.in_importation < mmodule then
514 error(aimport.n_name, "Error: Dependency loop between modules {mmodule} and {sup}.")
515 return
516 end
517 mmodule.set_visibility_for(sup, mvisibility)
518 end
519 if stdimport then
520 var mod_name = "standard"
521 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
522 if sup != null then # Skip error
523 imported_modules.add(sup)
524 mmodule.set_visibility_for(sup, public_visibility)
525 end
526 end
527 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
528 mmodule.set_imported_mmodules(imported_modules)
529
530 # Force standard to be public if imported
531 for sup in mmodule.in_importation.greaters do
532 if sup.name == "standard" then
533 mmodule.set_visibility_for(sup, public_visibility)
534 end
535 end
536
537 # TODO: Correctly check for useless importation
538 # It is even doable?
539 var directs = mmodule.in_importation.direct_greaters
540 for nim in nmodule.n_imports do
541 if not nim isa AStdImport then continue
542 var im = nim.mmodule
543 if im == null then continue
544 if directs.has(im) then continue
545 # This generates so much noise that it is simpler to just comment it
546 #warning(nim, "Warning: possible useless importation of {im}")
547 end
548 end
549
550 # All the loaded modules
551 var nmodules = new Array[AModule]
552
553 # Register the nmodule associated to each mmodule
554 # FIXME: why not refine the `MModule` class with a nullable attribute?
555 var mmodule2nmodule = new HashMap[MModule, AModule]
556
557 # Retrieve the associated AST node of a mmodule.
558 # This method is used to associate model entity with syntactic entities.
559 #
560 # If the module is not associated with a node, returns null.
561 fun mmodule2node(mmodule: MModule): nullable AModule
562 do
563 return mmodule2nmodule.get_or_null(mmodule)
564 end
565 end
566
567 # File-system location of a module (file) that is identified but not always loaded.
568 class ModulePath
569 # The name of the module
570 # (it's the basename of the filepath)
571 var name: String
572
573 # The human path of the module
574 var filepath: String
575
576 # The group (and the project) of the possible module
577 var mgroup: MGroup
578
579 # The loaded module (if any)
580 var mmodule: nullable MModule = null
581
582 redef fun to_s do return filepath
583 end
584
585 redef class MGroup
586 # Modules paths associated with the group
587 var module_paths = new Array[ModulePath]
588
589 # Is the group interesting for a final user?
590 #
591 # Groups are mandatory in the model but for simple projects they are not
592 # always interesting.
593 #
594 # A interesting group has, at least, one of the following true:
595 #
596 # * it has 2 modules or more
597 # * it has a subgroup
598 # * it has a documentation
599 fun is_interesting: Bool
600 do
601 return module_paths.length > 1 or mmodules.length > 1 or not in_nesting.direct_smallers.is_empty or mdoc != null
602 end
603
604 end
605
606 redef class AStdImport
607 # The imported module once determined
608 var mmodule: nullable MModule = null
609 end
610
611 redef class AModule
612 # The associated MModule once build by a `ModelBuilder`
613 var mmodule: nullable MModule
614 # Flag that indicate if the importation is already completed
615 var is_importation_done: Bool = false
616 end