mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
modules: cyclic import detection
This commit is contained in:
parent
23c5f88f3e
commit
135f200ea2
@ -751,16 +751,14 @@ fn (v mut V) add_user_v_files() {
|
|||||||
v.log('user_files:')
|
v.log('user_files:')
|
||||||
println(user_files)
|
println(user_files)
|
||||||
}
|
}
|
||||||
// To store the import table for user imports
|
// import tables for user/lib files
|
||||||
mut user_imports := []FileImportTable
|
mut file_imports := []FileImportTable
|
||||||
// Parse user imports
|
// Parse user imports
|
||||||
for file in user_files {
|
for file in user_files {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file, Pass.imports)
|
||||||
p.parse()
|
p.parse()
|
||||||
user_imports << *p.import_table
|
file_imports << *p.import_table
|
||||||
}
|
}
|
||||||
// To store the import table for lib imports
|
|
||||||
mut lib_imports := []FileImportTable
|
|
||||||
// Parse lib imports
|
// Parse lib imports
|
||||||
if v.pref.build_mode == .default_mode {
|
if v.pref.build_mode == .default_mode {
|
||||||
for i := 0; i < v.table.imports.len; i++ {
|
for i := 0; i < v.table.imports.len; i++ {
|
||||||
@ -770,7 +768,7 @@ fn (v mut V) add_user_v_files() {
|
|||||||
for file in vfiles {
|
for file in vfiles {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file, Pass.imports)
|
||||||
p.parse()
|
p.parse()
|
||||||
lib_imports << *p.import_table
|
file_imports << *p.import_table
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -789,7 +787,7 @@ fn (v mut V) add_user_v_files() {
|
|||||||
for file in vfiles {
|
for file in vfiles {
|
||||||
mut p := v.new_parser(file, Pass.imports)
|
mut p := v.new_parser(file, Pass.imports)
|
||||||
p.parse()
|
p.parse()
|
||||||
lib_imports << *p.import_table
|
file_imports << *p.import_table
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -797,38 +795,38 @@ fn (v mut V) add_user_v_files() {
|
|||||||
v.log('imports:')
|
v.log('imports:')
|
||||||
println(v.table.imports)
|
println(v.table.imports)
|
||||||
}
|
}
|
||||||
// this order is important for declaration
|
// graph deps
|
||||||
mut combined_imports := []FileImportTable
|
mut dep_graph := new_mod_dep_graph()
|
||||||
for i := lib_imports.len-1; i>=0; i-- {
|
dep_graph.from_import_tables(file_imports)
|
||||||
combined_imports << lib_imports[i]
|
deps_resolved := dep_graph.resolve()
|
||||||
|
if !deps_resolved.acyclic {
|
||||||
|
deps_resolved.display()
|
||||||
|
panic('Import cycle detected.')
|
||||||
}
|
}
|
||||||
for i in user_imports {
|
// add imports in correct order
|
||||||
combined_imports << i
|
for mod in deps_resolved.imports() {
|
||||||
}
|
mod_p := v.module_path(mod)
|
||||||
// Only now add all combined lib files
|
idir := os.getwd()
|
||||||
// adding the modules each file imports first
|
mut module_path := '$idir/$mod_p'
|
||||||
for fit in combined_imports {
|
// If we are in default mode, we don't parse vlib .v files, but header .vh files in
|
||||||
for _, mod in fit.imports {
|
// TmpPath/vlib
|
||||||
mod_p := v.module_path(mod)
|
// These were generated by vfmt
|
||||||
idir := os.getwd()
|
if v.pref.build_mode == .default_mode || v.pref.build_mode == .build {
|
||||||
mut module_path := '$idir/$mod_p'
|
module_path = '$ModPath/vlib/$mod_p'
|
||||||
// If we are in default mode, we don't parse vlib .v files, but header .vh files in
|
|
||||||
// TmpPath/vlib
|
|
||||||
// These were generated by vfmt
|
|
||||||
if v.pref.build_mode == .default_mode || v.pref.build_mode == .build {
|
|
||||||
module_path = '$ModPath/vlib/$mod_p'
|
|
||||||
}
|
|
||||||
if !os.file_exists(module_path) {
|
|
||||||
module_path = '$v.lang_dir/vlib/$mod_p'
|
|
||||||
}
|
|
||||||
vfiles := v.v_files_from_dir(module_path)
|
|
||||||
for file in vfiles {
|
|
||||||
if !file in v.files {
|
|
||||||
v.files << file
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO v.files.append_array(vfiles)
|
|
||||||
}
|
}
|
||||||
|
if !os.file_exists(module_path) {
|
||||||
|
module_path = '$v.lang_dir/vlib/$mod_p'
|
||||||
|
}
|
||||||
|
vfiles := v.v_files_from_dir(module_path)
|
||||||
|
for file in vfiles {
|
||||||
|
if !file in v.files {
|
||||||
|
v.files << file
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO v.files.append_array(vfiles)
|
||||||
|
}
|
||||||
|
// add remaining files (not mods)
|
||||||
|
for fit in file_imports {
|
||||||
if !fit.file_path in v.files {
|
if !fit.file_path in v.files {
|
||||||
v.files << fit.file_path
|
v.files << fit.file_path
|
||||||
}
|
}
|
||||||
|
141
compiler/modules.v
Normal file
141
compiler/modules.v
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
||||||
|
// Use of this source code is governed by an MIT license
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
module main
|
||||||
|
|
||||||
|
struct ModDepGraphNode {
|
||||||
|
name string
|
||||||
|
deps []string
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ModDepGraph {
|
||||||
|
pub:
|
||||||
|
mut:
|
||||||
|
acyclic bool
|
||||||
|
nodes []ModDepGraphNode
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DepSet {
|
||||||
|
mut:
|
||||||
|
items []string
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(mapset mut DepSet) add(item string) {
|
||||||
|
mapset.items << item
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(mapset &DepSet) diff(otherset DepSet) DepSet {
|
||||||
|
mut diff := DepSet{}
|
||||||
|
for item in mapset.items {
|
||||||
|
if !item in otherset.items {
|
||||||
|
diff.items << item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(mapset &DepSet) size() int {
|
||||||
|
return mapset.items.len
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_mod_dep_graph() *ModDepGraph {
|
||||||
|
return &ModDepGraph{
|
||||||
|
acyclic: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph mut ModDepGraph) from_import_tables(import_tables []FileImportTable) {
|
||||||
|
for fit in import_tables {
|
||||||
|
mut deps := []string
|
||||||
|
for _, m in fit.imports {
|
||||||
|
deps << m
|
||||||
|
}
|
||||||
|
graph.add(fit.module_name, deps)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph mut ModDepGraph) add(mod string, deps []string) {
|
||||||
|
graph.nodes << ModDepGraphNode{
|
||||||
|
name: mod,
|
||||||
|
deps: deps
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph &ModDepGraph) resolve() *ModDepGraph {
|
||||||
|
mut node_names := map[string]ModDepGraphNode{}
|
||||||
|
mut node_deps := map[string]DepSet{}
|
||||||
|
|
||||||
|
for _, node in graph.nodes {
|
||||||
|
node_names[node.name] = node
|
||||||
|
|
||||||
|
mut dep_set := DepSet{}
|
||||||
|
for _, dep in node.deps {
|
||||||
|
dep_set.add(dep)
|
||||||
|
}
|
||||||
|
node_deps[node.name] = dep_set
|
||||||
|
}
|
||||||
|
|
||||||
|
mut resolved := new_mod_dep_graph()
|
||||||
|
for node_deps.size != 0 {
|
||||||
|
mut ready_set := DepSet{}
|
||||||
|
for name, deps in node_deps {
|
||||||
|
if deps.size() == 0 {
|
||||||
|
ready_set.add(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ready_set.size() == 0 {
|
||||||
|
mut g := new_mod_dep_graph()
|
||||||
|
g.acyclic = false
|
||||||
|
for name, _ in node_deps {
|
||||||
|
g.nodes << node_names[name]
|
||||||
|
}
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
|
ready_set.items.len > 0 {
|
||||||
|
mut new_set := map[string]DepSet{}
|
||||||
|
for name in ready_set.items {
|
||||||
|
// node_deps.remove(name)
|
||||||
|
resolved.nodes << node_names[name]
|
||||||
|
}
|
||||||
|
// remove once we have map.remove/delete
|
||||||
|
for k, d in node_deps {
|
||||||
|
if !k in ready_set.items {
|
||||||
|
new_set[k] = d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
node_deps = new_set
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, deps in node_deps {
|
||||||
|
node_deps[name] = deps.diff(ready_set)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolved
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph &ModDepGraph) imports() []string {
|
||||||
|
mut mods := []string
|
||||||
|
for node in graph.nodes {
|
||||||
|
if node.name == 'main' {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mods << node.name
|
||||||
|
}
|
||||||
|
return mods
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph &ModDepGraph) last_node() {
|
||||||
|
return graph.nodes[graph.nodes.len-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn(graph &ModDepGraph) display() {
|
||||||
|
for _, node in graph.nodes {
|
||||||
|
for _, dep in node.deps {
|
||||||
|
println(' * $node.name -> $dep')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -149,6 +149,7 @@ fn (p mut Parser) parse() {
|
|||||||
// Import pass - the first and the smallest pass that only analyzes imports
|
// Import pass - the first and the smallest pass that only analyzes imports
|
||||||
// fully qualify the module name, eg base64 to encoding.base64
|
// fully qualify the module name, eg base64 to encoding.base64
|
||||||
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
||||||
|
p.import_table.module_name = fq_mod
|
||||||
p.table.register_package(fq_mod)
|
p.table.register_package(fq_mod)
|
||||||
// replace "." with "_dot_" in module name for C variable names
|
// replace "." with "_dot_" in module name for C variable names
|
||||||
p.mod = fq_mod.replace('.', '_dot_')
|
p.mod = fq_mod.replace('.', '_dot_')
|
||||||
|
@ -22,8 +22,9 @@ mut:
|
|||||||
// Holds import information scoped to the parsed file
|
// Holds import information scoped to the parsed file
|
||||||
struct FileImportTable {
|
struct FileImportTable {
|
||||||
mut:
|
mut:
|
||||||
file_path string
|
module_name string
|
||||||
imports map[string]string
|
file_path string
|
||||||
|
imports map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AccessMod {
|
enum AccessMod {
|
||||||
|
Loading…
Reference in New Issue
Block a user