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