Skip to content

Commit d9ea78d

Browse files
committed
Desugaring of export/public
1 parent 4c76d88 commit d9ea78d

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

JuliaLowering/src/desugaring.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3908,6 +3908,16 @@ function expand_import(ctx, ex)
39083908
]
39093909
end
39103910

3911+
# Expand `public` or `export`
3912+
function expand_public(ctx, ex)
3913+
@ast ctx ex [K"call"
3914+
module_public::K"Value"
3915+
ctx.mod::K"Value"
3916+
(kind(ex) == K"export")::K"Bool"
3917+
(e.name_val::K"String" for e in children(ex))...
3918+
]
3919+
end
3920+
39113921
#-------------------------------------------------------------------------------
39123922
# Expand module definitions
39133923

@@ -4148,7 +4158,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
41484158
elseif k == K"import" || k == K"using"
41494159
expand_import(ctx, ex)
41504160
elseif k == K"export" || k == K"public"
4151-
TODO(ex)
4161+
expand_public(ctx, ex)
41524162
elseif k == K"abstract" || k == K"primitive"
41534163
expand_forms_2(ctx, expand_abstract_or_primitive_type(ctx, ex))
41544164
elseif k == K"struct"

JuliaLowering/src/runtime.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,12 @@ function module_import(into_mod::Module, is_using::Bool,
193193
nothing
194194
end
195195

196+
function module_public(mod::Module, is_exported::Bool, identifiers...)
197+
for ident in identifiers
198+
@ccall jl_module_public(mod::Module, Symbol(ident)::Symbol, is_exported::Cint)::Cvoid
199+
end
200+
end
201+
196202
# Return the current exception. In JuliaLowering we use this rather than the
197203
# special form `K"the_exception"` to reduces the number of special forms.
198204
Base.@assume_effects :removable :nothrow function current_exception()

JuliaLowering/test/import.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ JuliaLowering.include_string(test_mod, """
1414
@test test_mod.st2 === JuliaLowering.SyntaxTree
1515
@test test_mod.parsestmt === JuliaSyntax.parsestmt
1616

17+
JuliaLowering.include_string(test_mod, """
18+
x = 1
19+
y = 2
20+
export x
21+
public y
22+
""")
23+
@test Base.isexported(test_mod, :x)
24+
@test Base.ispublic(test_mod, :x)
25+
@test Base.ispublic(test_mod, :y)
26+
@test !Base.isexported(test_mod, :y)
27+
1728
C = JuliaLowering.include_string(test_mod, """
1829
module C
1930
module D

JuliaLowering/test/import_ir.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@ function f()
4545
# └─────────┘ ── this syntax is only allowed in top level code
4646
end
4747

48+
########################################
49+
# Export
50+
export a, b, c
51+
#---------------------
52+
1 (call JuliaLowering.module_public TestMod true "a" "b" "c")
53+
2 (return %₁)
54+
55+
########################################
56+
# Public
57+
public a, b, c
58+
#---------------------
59+
1 (call JuliaLowering.module_public TestMod false "a" "b" "c")
60+
2 (return %₁)
61+

0 commit comments

Comments
 (0)