|
| 1 | +# This file is a part of Julia. License is MIT: https://julialang.org/license |
| 2 | + |
| 3 | +# Full-featured versions of _eval_import and _eval_using |
| 4 | + |
| 5 | +for m in methods(_eval_import) |
| 6 | + delete_method(m) |
| 7 | +end |
| 8 | +for m in methods(_eval_using) |
| 9 | + delete_method(m) |
| 10 | +end |
| 11 | + |
| 12 | +function eval_import_path(at::Module, from::Union{Module, Nothing}, path::Expr, keyword::String) |
| 13 | + isempty(path.args) && error("malformed import statement") |
| 14 | + |
| 15 | + i::Int = 1 |
| 16 | + function next!() |
| 17 | + i <= length(path.args) || error("invalid module path") |
| 18 | + v = path.args[i] |
| 19 | + i += 1 |
| 20 | + v isa Symbol || throw(TypeError(Symbol(keyword), "", Symbol, v)) |
| 21 | + v |
| 22 | + end |
| 23 | + v = next!() |
| 24 | + m = nothing |
| 25 | + |
| 26 | + if from !== nothing |
| 27 | + m = from |
| 28 | + elseif v !== :. |
| 29 | + # `A.B`: call the loader to obtain the root A in the current environment. |
| 30 | + if v === :Core |
| 31 | + m = Core |
| 32 | + elseif v === :Base |
| 33 | + m = Base |
| 34 | + else |
| 35 | + m = require(at, v) |
| 36 | + m isa Module || error("failed to load module $v") |
| 37 | + end |
| 38 | + i > lastindex(path.args) && return m, nothing |
| 39 | + v = next!() |
| 40 | + else |
| 41 | + # `.A.B.C`: strip off leading dots by following parent links |
| 42 | + m = at |
| 43 | + while (v = next!()) === :. |
| 44 | + m = parentmodule(m) |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + while true |
| 49 | + v === :. && error("invalid $keyword path: \".\" in identifier path") |
| 50 | + i > lastindex(path.args) && break |
| 51 | + m = getglobal(m, v) |
| 52 | + m isa Module || error("invalid $keyword path: \"$v\" does not name a module") |
| 53 | + v = next!() |
| 54 | + end |
| 55 | + m, v |
| 56 | +end |
| 57 | + |
| 58 | +function eval_import_path_all(at::Module, path::Expr, keyword::String) |
| 59 | + m, v = eval_import_path(at, nothing, path, keyword) |
| 60 | + if v !== nothing |
| 61 | + m = getglobal(m, v) |
| 62 | + m isa Module || error("invalid $keyword path: \"$v\" does not name a module") |
| 63 | + end |
| 64 | + m |
| 65 | +end |
| 66 | + |
| 67 | +function check_macro_rename(from::Symbol, to::Symbol, keyword::String) |
| 68 | + c1(sym) = bitcast(Char, UInt32(unsafe_load(unsafe_convert(Ptr{UInt8}, sym))) << 24) |
| 69 | + from_c, to_c = c1(from), c1(to) |
| 70 | + if from_c == '@' && to_c != '@' |
| 71 | + error("cannot rename macro \"$from\" to non-macro \"$to\" in \"$keyword\"") |
| 72 | + end |
| 73 | + if from_c != '@' && to_c == '@' |
| 74 | + error("cannot rename non-macro \"$from\" to macro \"$to\" in \"$keyword\"") |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +""" |
| 79 | + _eval_import(imported::Bool, to::Module, from::Union{Expr, Nothing}, paths::Expr...) |
| 80 | +
|
| 81 | +Evaluate the import paths, calling `Core._import` for each name to be imported. |
| 82 | +`imported` imports are created with `import`, `using A: x` sets this to false. |
| 83 | +The `from` is the part of the import path before the `:`. This is the lowered |
| 84 | +form of `import`, `import ...:`, and `using ...:`. |
| 85 | +
|
| 86 | +``` |
| 87 | +import A => _eval_import(true, Main, nothing, Expr(:., :A)) |
| 88 | +import A.b => _eval_import(true, Main, nothing, Expr(:., :A, :b)) |
| 89 | +import A.b as c => _eval_import(true, Main, nothing, Expr(:as, Expr(:., :A, :b), :c)) |
| 90 | +import A.B: C.d, e => _eval_import(true, Main, Expr(:., :A, :B), Expr(:., :C, :d), Expr(:., :e)) |
| 91 | +import A.B: C.d as e => _eval_import(true, Main, Expr(:., :A, :B), Expr(:as, Expr(:., :C, :d), :e)) |
| 92 | +using A.B: C.d, e => _eval_import(false, Main, Expr(:., :A, :B), Expr(:., :C, :d), Expr(:., :e)) |
| 93 | +
|
| 94 | +See also [`_import`](@ref Core._import). |
| 95 | +``` |
| 96 | +""" |
| 97 | +function _eval_import(imported::Bool, to::Module, from::Union{Expr, Nothing}, paths::Expr...) |
| 98 | + keyword = imported ? "import" : "using" |
| 99 | + fail() = error("malformed \"$keyword\" statement") |
| 100 | + from = from !== nothing ? eval_import_path_all(to, from, keyword) : nothing |
| 101 | + |
| 102 | + for path in paths |
| 103 | + path isa Expr || fail() |
| 104 | + asname = nothing |
| 105 | + if path.head === :as && length(path.args) == 2 |
| 106 | + path, asname = path.args |
| 107 | + elseif path.head !== :. |
| 108 | + fail() |
| 109 | + end |
| 110 | + old_from = from |
| 111 | + from, name = eval_import_path(to, from, path, keyword) |
| 112 | + |
| 113 | + if name !== nothing |
| 114 | + asname = asname === nothing ? name : asname |
| 115 | + check_macro_rename(name, asname, keyword) |
| 116 | + Core._import(to, from, asname, name, imported) |
| 117 | + else |
| 118 | + Core._import(to, from, asname === nothing ? nameof(from) : asname) |
| 119 | + end |
| 120 | + end |
| 121 | +end |
| 122 | + |
| 123 | +""" |
| 124 | + _eval_using(to::Module, path::Expr) |
| 125 | +
|
| 126 | +Evaluate the import path to a module and call [`Core._using`](@ref) on it, |
| 127 | +making its exports available to the `to` module; this is the lowered form of |
| 128 | +`using A`. |
| 129 | +
|
| 130 | +``` |
| 131 | +using A.B => _module_using(Main, Expr(:., :A, :B)) |
| 132 | +``` |
| 133 | +
|
| 134 | +See also [`_using`](@ref Core._using). |
| 135 | +""" |
| 136 | +function _eval_using(to::Module, path::Expr) |
| 137 | + from = eval_import_path_all(to, path, "using") |
| 138 | + Core._using(to, from) |
| 139 | + is_package = length(path.args) == 1 && path.args[1] !== :. |
| 140 | + if to == Main && is_package |
| 141 | + Core._import(to, from, nameof(from)) |
| 142 | + end |
| 143 | +end |
0 commit comments