1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/compiler/modules.v

211 lines
5.6 KiB
V
Raw Normal View History

2020-02-03 07:00:36 +03:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-07-21 18:53:35 +03:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module compiler
2019-07-21 18:53:35 +03:00
2020-02-09 12:08:04 +03:00
import (
filepath
os
v.pref
)
2019-08-04 01:03:52 +03:00
2019-10-30 17:07:41 +03:00
pub const (
2020-02-09 12:08:04 +03:00
v_modules_path = pref.default_module_path
2019-10-04 15:48:09 +03:00
)
2019-10-25 16:34:12 +03:00
// Holds import information scoped to the parsed file
struct ImportTable {
mut:
imports map[string]string // alias => module
2019-12-20 00:29:37 +03:00
used_imports []string // alias
import_tok_idx map[string]int // module => idx
2019-10-25 16:34:12 +03:00
}
// Once we have a module format we can read from module file instead
// this is not optimal
fn (table &Table) qualify_module(mod string, file_path string) string {
for m in table.imports {
if m.contains('.') && m.contains(mod) {
m_parts := m.split('.')
2020-02-17 22:31:23 +03:00
m_path := m_parts.join(filepath.separator)
2019-12-20 00:29:37 +03:00
if mod == m_parts[m_parts.len - 1] && file_path.contains(m_path) {
2019-10-25 16:34:12 +03:00
return m
}
}
}
return mod
}
fn new_import_table() ImportTable {
return ImportTable{
2019-12-20 00:29:37 +03:00
imports: map[string]string
2019-10-25 16:34:12 +03:00
}
}
fn (p mut Parser) register_import(mod string, tok_idx int) {
p.register_import_alias(mod, mod, tok_idx)
}
fn (p mut Parser) register_import_alias(alias string, mod string, tok_idx int) {
// NOTE: come back here
// if alias in it.imports && it.imports[alias] == mod {}
if alias in p.import_table.imports && p.import_table.imports[alias] != mod {
p.error('cannot import $mod as $alias: import name $alias already in use')
2019-10-25 16:34:12 +03:00
}
if mod.contains('.internal.') && !p.is_vgen {
2019-10-25 16:34:12 +03:00
mod_parts := mod.split('.')
mut internal_mod_parts := []string
for part in mod_parts {
2019-12-20 00:29:37 +03:00
if part == 'internal' {
break
}
2019-10-25 16:34:12 +03:00
internal_mod_parts << part
}
internal_parent := internal_mod_parts.join('.')
if !p.mod.starts_with(internal_parent) {
p.error('module $mod can only be imported internally by libs')
}
}
p.import_table.imports[alias] = mod
p.import_table.import_tok_idx[mod] = tok_idx
}
fn (it &ImportTable) get_import_tok_idx(mod string) int {
return it.import_tok_idx[mod]
}
fn (it &ImportTable) known_import(mod string) bool {
return mod in it.imports || it.is_aliased(mod)
}
fn (it &ImportTable) known_alias(alias string) bool {
return alias in it.imports
}
fn (it &ImportTable) is_aliased(mod string) bool {
for _, val in it.imports {
if val == mod {
return true
}
}
return false
}
fn (it &ImportTable) resolve_alias(alias string) string {
return it.imports[alias]
}
fn (it mut ImportTable) register_used_import(alias string) {
if !(alias in it.used_imports) {
it.used_imports << alias
}
}
fn (it &ImportTable) is_used_import(alias string) bool {
return alias in it.used_imports
}
// should module be accessable
pub fn (p &Parser) is_mod_in_scope(mod string) bool {
mut mods_in_scope := ['', 'builtin', 'main', p.mod]
for _, m in p.import_table.imports {
mods_in_scope << m
}
return mod in mods_in_scope
}
2019-10-25 16:34:12 +03:00
// return resolved dep graph (order deps)
pub fn (v &V) resolve_deps() &DepGraph {
graph := v.import_graph()
deps_resolved := graph.resolve()
if !deps_resolved.acyclic {
verror('import cycle detected between the following modules: \n' + deps_resolved.display_cycles())
}
return deps_resolved
}
// graph of all imported modules
2019-12-20 00:29:37 +03:00
pub fn (v &V) import_graph() &DepGraph {
2019-10-25 16:34:12 +03:00
mut graph := new_dep_graph()
for p in v.parsers {
2019-07-21 18:53:35 +03:00
mut deps := []string
2019-10-25 16:34:12 +03:00
for _, m in p.import_table.imports {
2019-07-21 18:53:35 +03:00
deps << m
}
2019-10-25 16:34:12 +03:00
graph.add(p.mod, deps)
2019-07-21 18:53:35 +03:00
}
2019-10-25 16:34:12 +03:00
return graph
2019-07-21 18:53:35 +03:00
}
// get ordered imports (module speficic dag method)
2019-12-20 00:29:37 +03:00
pub fn (graph &DepGraph) imports() []string {
2019-07-21 18:53:35 +03:00
mut mods := []string
for node in graph.nodes {
mods << node.name
}
return mods
}
2019-12-20 00:29:37 +03:00
[inline]
fn (v &V) module_path(mod string) string {
2019-10-04 15:48:09 +03:00
// submodule support
2020-02-17 22:31:23 +03:00
return mod.replace('.', filepath.separator)
2019-10-04 15:48:09 +03:00
}
2019-09-01 22:51:16 +03:00
// 'strings' => 'VROOT/vlib/strings'
// 'installed_mod' => '~/.vmodules/installed_mod'
// 'local_mod' => '/path/to/current/dir/local_mod'
2019-12-20 00:29:37 +03:00
fn (v mut V) set_module_lookup_paths() {
mlookup_path := if v.pref.vpath.len > 0 { v.pref.vpath } else { v_modules_path }
// Module search order:
// 0) V test files are very commonly located right inside the folder of the
// module, which they test. Adding the parent folder of the module folder
// with the _test.v files, *guarantees* that the tested module can be found
// without needing to set custom options/flags.
// 1) search in the *same* directory, as the compiled final v program source
2019-12-20 00:29:37 +03:00
// (i.e. the . in `v .` or file.v in `v file.v`)
// 2) search in the modules/ in the same directory.
// 3) search in vlib/
// 4.1) search in -vpath (if given)
// 4.2) search in ~/.vmodules/ (i.e. modules installed with vpm) (no -vpath)
2019-12-20 00:29:37 +03:00
v.module_lookup_paths = []
if v.pref.is_test {
2019-12-23 13:09:22 +03:00
v.module_lookup_paths << filepath.basedir(v.compiled_dir) // pdir of _test.v
}
v.module_lookup_paths << v.compiled_dir
2019-12-20 00:29:37 +03:00
v.module_lookup_paths << filepath.join(v.compiled_dir,'modules')
v.module_lookup_paths << v.pref.vlib_path
v.module_lookup_paths << mlookup_path
if v.pref.user_mod_path.len > 0 {
v.module_lookup_paths << v.pref.user_mod_path
}
if v.pref.is_verbose {
v.log('v.module_lookup_paths: $v.module_lookup_paths')
}
}
fn (v &V) find_module_path(mod string) ?string {
mod_path := v.module_path(mod)
for lookup_path in v.module_lookup_paths {
2019-12-20 00:29:37 +03:00
try_path := filepath.join(lookup_path,mod_path)
if v.pref.is_verbose {
println(' >> trying to find $mod in $try_path ...')
}
if os.is_dir(try_path) {
2019-12-20 00:29:37 +03:00
if v.pref.is_verbose {
println(' << found $try_path .')
}
return try_path
2019-09-01 22:51:16 +03:00
}
2019-08-04 01:03:52 +03:00
}
return error('module "$mod" not found')
2019-09-01 22:51:16 +03:00
}
2019-12-20 00:29:37 +03:00
[inline]
fn mod_gen_name(mod string) string {
return mod.replace('.', '_dot_')
}
2019-12-20 00:29:37 +03:00
[inline]
fn mod_gen_name_rev(mod string) string {
return mod.replace('_dot_', '.')
2019-10-12 22:31:05 +03:00
}