Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions JuliaLowering/src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,6 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
id_inner = _insert_tree_node(graph, K"String", src; value=e)
setchildren!(graph, st_id, [id_inner])
return st_id, src
elseif e isa VersionNumber
st_id = _insert_tree_node(graph, K"VERSION", src, JS.set_numeric_flags(e.minor*10); value=e)
return st_id, src
elseif !(e isa Expr)
# There are other kinds we could potentially back-convert (e.g. Float),
# but Value should work fine.
Expand Down Expand Up @@ -407,7 +404,7 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
st_flags |= JS.BARE_MODULE_FLAG
end
child_exprs = has_version ?
Any[e.args[1], e.args[2+has_version], e.args[3+has_version]] :
Any[Expr(:mod_version, e.args[1]), e.args[2+has_version], e.args[3+has_version]] :
Any[e.args[2+has_version], e.args[3+has_version]]
elseif e.head === :do
# Expr:
Expand Down Expand Up @@ -565,6 +562,13 @@ function _insert_convert_expr(@nospecialize(e), graph::SyntaxGraph, src::SourceA
child_exprs = nothing
elseif e.head === :do_lambda
st_k = K"do"
elseif e.head === :mod_version
v = e.args[1]
@assert v isa VersionNumber
st_k = K"VERSION"
st_flags = JS.set_numeric_flags(v.minor*10)
st_attrs[:value] = v
child_exprs = nothing
end

#---------------------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions JuliaLowering/src/desugaring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1724,13 +1724,12 @@ end

# Expand the (sym,lib) argument to ccall/cglobal
function expand_C_library_symbol(ctx, ex)
expanded = expand_forms_2(ctx, ex)
if kind(ex) == K"tuple"
expanded = @ast ctx ex [K"static_eval"(meta=name_hint("function name and library expression"))
expanded
return @ast ctx ex [K"static_eval"(meta=name_hint("function name and library expression"))
mapchildren(e->expand_forms_2(ctx,e), ctx, ex)
]
end
return expanded
return expand_forms_2(ctx, ex)
end

function expand_ccall(ctx, ex)
Expand Down
16 changes: 10 additions & 6 deletions JuliaLowering/src/eval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,16 @@ function _to_lowered_expr(ex::SyntaxTree, stmt_offset::Int)
Expr(:meta, args...)
elseif k == K"static_eval"
@assert numchildren(ex) == 1
_to_lowered_expr(ex[1], stmt_offset)
elseif k == K"cfunction"
args = Any[_to_lowered_expr(e, stmt_offset) for e in children(ex)]
if kind(ex[2]) == K"static_eval"
args[2] = QuoteNode(args[2])
if kind(ex[1]) === K"tuple"
# Should just be ccall library spec
@assert numchildren(ex[1]) === 2
Expr(:tuple, _to_lowered_expr(ex[1][1], stmt_offset),
_to_lowered_expr(ex[1][2], stmt_offset))
elseif kind(ex[1]) === K"function"
QuoteNode(Expr(ex))
else
_to_lowered_expr(ex[1], stmt_offset)
end
Expr(:cfunction, args...)
else
# Allowed forms according to https://docs.julialang.org/en/v1/devdocs/ast/
#
Expand All @@ -438,6 +441,7 @@ function _to_lowered_expr(ex::SyntaxTree, stmt_offset::Int)
k == K"gc_preserve_begin" ? :gc_preserve_begin :
k == K"gc_preserve_end" ? :gc_preserve_end :
k == K"foreigncall" ? :foreigncall :
k == K"cfunction" ? :cfunction :
k == K"new_opaque_closure" ? :new_opaque_closure :
nothing
if isnothing(head)
Expand Down
9 changes: 8 additions & 1 deletion JuliaLowering/src/macro_expansion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ function expand_macro(ctx, ex)
# Compat: attempt to invoke an old-style macro if there's no applicable
# method for new-style macro arguments.
macro_args = Any[macro_loc, ctx.scope_layers[1].mod]

if length(raw_args) >= 1 && kind(raw_args[1]) === K"VERSION"
# Hack: see jl_invoke_julia_macro. We may see an extra argument
# depending on who parsed this macrocall.
macro_args[1] = Core.MacroSource(macro_loc, raw_args[1].value)
end

for arg in raw_args
# For hygiene in old-style macros, we omit any additional scope
# layer information from macro arguments. Old-style macros will
Expand All @@ -335,7 +342,7 @@ function expand_macro(ctx, ex)
# new-style macros which call old-style macros. Instead of seeing
# `Expr(:escape)` in such situations, old-style macros will now see
# `Expr(:scope_layer)` inside `macro_args`.
push!(macro_args, Expr(arg))
kind(arg) !== K"VERSION" && push!(macro_args, Expr(arg))
end
expanded = try
Base.invoke_in_world(ctx.macro_world, macfunc, macro_args...)
Expand Down
8 changes: 4 additions & 4 deletions JuliaLowering/src/syntax_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ function Base.var"@cfunction"(__context__::MacroContext, callable, return_type,
typ = Base.CFunction
else
# Kinda weird semantics here - without `$`, the callable is a top level
# expression which will be evaluated by `jl_resolve_globals_in_ir`,
# implicitly within the module where the `@cfunction` is expanded into.
fptr = @ast __context__ callable [K"static_eval"(
meta=name_hint("cfunction function name"))
# expression evaluated within the module where the `@cfunction` is
# expanded into.
fptr = @ast __context__ callable [K"inert"(
meta=CompileHints(:as_Expr, true))
callable
]
typ = Ptr{Cvoid}
Expand Down
4 changes: 2 additions & 2 deletions JuliaLowering/test/function_calls_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ ccall((:strlen, libc), Csize_t, (Cstring,), "asdfg")
1 TestMod.Cstring
2 (call top.cconvert %₁ "asdfg")
3 (call top.unsafe_convert %₁ %₂)
4 (foreigncall (static_eval (call core.tuple :strlen TestMod.libc)) (static_eval TestMod.Csize_t) (static_eval (call core.svec TestMod.Cstring)) 0 :ccall %₃ %₂)
4 (foreigncall (static_eval (tuple-p :strlen TestMod.libc)) (static_eval TestMod.Csize_t) (static_eval (call core.svec TestMod.Cstring)) 0 :ccall %₃ %₂)
5 (return %₄)

########################################
Expand Down Expand Up @@ -521,7 +521,7 @@ ccall(:foo, Csize_t, (Cstring..., Cstring...), "asdfg", "blah")
cglobal((:sym, lib), Int)
#---------------------
1 TestMod.Int
2 (call core.cglobal (static_eval (call core.tuple :sym TestMod.lib)) %₁)
2 (call core.cglobal (static_eval (tuple-p :sym TestMod.lib)) %₁)
3 (return %₂)

########################################
Expand Down
13 changes: 13 additions & 0 deletions JuliaLowering/test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ ctx, expanded = JuliaLowering.expand_forms_1(test_mod, ex, false, Base.get_world
"y"
]

@test JuliaLowering.include_string(test_mod, raw"""
v"1.14"
""") isa VersionNumber
@test JuliaLowering.include_string(test_mod, raw"""
v"1.14"
""";expr_compat_mode=true) isa VersionNumber
@test JuliaLowering.include_string(test_mod, raw"""
Base.Experimental.@VERSION
""") isa NamedTuple
@test JuliaLowering.include_string(test_mod, raw"""
Base.Experimental.@VERSION
""";expr_compat_mode=true) isa NamedTuple

# World age support for macro expansion
JuliaLowering.include_string(test_mod, raw"""
macro world_age_test()
Expand Down
42 changes: 39 additions & 3 deletions JuliaLowering/test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ function cvarargs_2(arg1::Float64, arg2::Float64)
end
""") isa Function
@test test_mod.cvarargs_2(1.1, 2.2) == "1.1 2.2"
# (function, library) syntax
@test JuliaLowering.include_string(test_mod, """
ccall((:ctest, :libccalltest), Complex{Int}, (Complex{Int},), 10 + 20im)
""") === 11 + 18im
# (function, library): library is a global
@eval test_mod libccalltest_var = "libccalltest"
@test JuliaLowering.include_string(test_mod, """
ccall((:ctest, libccalltest_var), Complex{Int}, (Complex{Int},), 10 + 20im)
""") === 11 + 18im

# cfunction
JuliaLowering.include_string(test_mod, """
Expand All @@ -85,22 +94,49 @@ cf_float = JuliaLowering.include_string(test_mod, """
""")
@test @ccall($cf_float(2::Float64, 3::Float64)::Float64) == 32.0

# Test that hygiene works with @ccallable function names (this is broken in
# Base)
# Test that hygiene works with @ccallable function names
JuliaLowering.include_string(test_mod, raw"""
f_ccallable_hygiene() = 1

module Nested
f_ccallable_hygiene() = 2
macro cfunction_hygiene()
:(@cfunction(f_ccallable_hygiene, Int, ()))
:(@cfunction($f_ccallable_hygiene, Int, ()))
end
end
""")
cf_hygiene = JuliaLowering.include_string(test_mod, """
Nested.@cfunction_hygiene
""")
@test @ccall($cf_hygiene()::Int) == 2
# Same as above, but non-interpolated symbol. Arguably this could return 20,
# but if it should, this is a bug in the macro implementation, not lowering.
# Match Base for now.
JuliaLowering.include_string(test_mod, raw"""
f_ccallable_hygiene() = 10

module Nested
f_ccallable_hygiene() = 20
macro cfunction_hygiene()
:(@cfunction(f_ccallable_hygiene, Int, ()))
end
end
""")
cf_hygiene = JuliaLowering.include_string(test_mod, """
Nested.@cfunction_hygiene
""")
@test @ccall($cf_hygiene()::Int) == 10

# quoted function in cfunction
quoted_cfn_anon = JuliaLowering.include_string(test_mod, raw"""
@cfunction((function(x); x; end), Int, (Int,))
""")
@test ccall(quoted_cfn_anon, Int, (Int,), 1) == 1

quoted_cfn_named = JuliaLowering.include_string(test_mod, raw"""
@cfunction((function fname_unused(x); x; end), Int, (Int,))
""")
@test ccall(quoted_cfn_named, Int, (Int,), 1) == 1

# Test that ccall can be passed static parameters in type signatures.
#
Expand Down
2 changes: 1 addition & 1 deletion JuliaLowering/test/misc_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ JuxtuposeTest.@emit_juxtupose
# @cfunction expansion with global generic function as function argument
@cfunction(callable, Int, (Int, Float64))
#---------------------
1 (cfunction Ptr{Nothing} (static_eval TestMod.callable) (static_eval TestMod.Int) (static_eval (call core.svec TestMod.Int TestMod.Float64)) :ccall)
1 (cfunction Ptr{Nothing} (inert callable) (static_eval TestMod.Int) (static_eval (call core.svec TestMod.Int TestMod.Float64)) :ccall)
2 (return %₁)

########################################
Expand Down
13 changes: 8 additions & 5 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,25 +519,32 @@ VecElement(arg::T) where {T} = VecElement{T}(arg)
# inference lattice element types (moved from jltypes.c)
struct Const
val
Const(@nospecialize(v)) = new(v)
end

struct PartialStruct
typ
undefs
undefs::Array{Union{Nothing,Bool}, 1}
fields::Array{Any, 1}
# N.B. The constructor for this struct is intentionally not defined here.
# It is defined in coreir.jl along with some validation logic.
global _PartialStruct
_PartialStruct(@nospecialize(typ), undef::Array{Union{Nothing,Bool}, 1}, fields::Array{Any, 1}) = new(typ, undef, fields)
end

struct InterConditional
slot::Int
thentype
elsetype
InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) = new(slot, thentype, elsetype)
end

struct PartialOpaque
typ::Type
env
parent::MethodInstance
source
PartialOpaque(@nospecialize(typ::Type), @nospecialize(env), parent::MethodInstance, source) = new(typ, env, parent, source)
end

eval(Core, quote
Expand Down Expand Up @@ -566,10 +573,6 @@ eval(Core, quote
PhiCNode(values::Array{Any, 1}) = $(Expr(:new, :PhiCNode, :values))
UpsilonNode(@nospecialize(val)) = $(Expr(:new, :UpsilonNode, :val))
UpsilonNode() = $(Expr(:new, :UpsilonNode))
Const(@nospecialize(v)) = $(Expr(:new, :Const, :v))
_PartialStruct(@nospecialize(typ), undef, fields::Array{Any, 1}) = $(Expr(:new, :PartialStruct, :typ, :undef, :fields))
PartialOpaque(@nospecialize(typ), @nospecialize(env), parent::MethodInstance, source) = $(Expr(:new, :PartialOpaque, :typ, :env, :parent, :source))
InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) = $(Expr(:new, :InterConditional, :slot, :thentype, :elsetype))
MethodMatch(@nospecialize(spec_types), sparams::SimpleVector, method::Method, fully_covers::Bool) = $(Expr(:new, :MethodMatch, :spec_types, :sparams, :method, :fully_covers))
end)

Expand Down
Loading