11module CCall
22
33using JuliaSyntax, JuliaLowering
4- using JuliaLowering: is_identifier_like, numchildren, children, MacroExpansionError, @ast , SyntaxTree
5-
6- # Hacky utils
7- # macro K_str(str)
8- # JuliaSyntax.Kind(str[1].value)
9- # end
10- #
11- # # Needed because we can't lower kwarg calls yet ehehe :-/
12- # function mac_ex_error(ex, msg, pos)
13- # kwargs = Core.apply_type(Core.NamedTuple, (:position,))((pos,))
14- # Core.kwcall(kwargs, MacroExpansionError, ex, msg)
15- # end
16-
17- macro ast_str (str)
18- ex = parsestmt (JuliaLowering. SyntaxTree, str, filename= string (__source__. file))
19- ctx1, ex1 = JuliaLowering. expand_forms_1 (__module__, ex)
20- @assert kind (ex1) == K " call" && ex1[1 ]. value === JuliaLowering. interpolate_ast
21- cs = map (e-> esc (Expr (e)), ex1[3 : end ])
22- :(JuliaLowering. interpolate_ast ($ (ex1[2 ][1 ]), $ (cs... )))
23- end
4+ using JuliaLowering: is_identifier_like, numchildren, children, MacroExpansionError,
5+ @ast , SyntaxTree, @SyntaxTree
246
257function ccall_macro_parse (ex)
268 if kind (ex) != K " ::"
8567function ccall_macro_lower (ex, convention, func, rettype, types, args, num_varargs)
8668 statements = SyntaxTree[]
8769 if kind (func) == K " $"
88- check = ast """ quote
70+ check = @SyntaxTree quote
8971 func = $ (func[1 ])
9072 if ! isa (func, Ptr{Cvoid})
9173 name = :($ (func[1 ]))
9274 throw (ArgumentError (" interpolated function `$name ` was not a `Ptr{Cvoid}`, but $(typeof (func)) " ))
9375 end
94- end"""
76+ end
9577 func = check[1 ][1 ]
9678 push! (statements, check)
9779 end
@@ -100,16 +82,16 @@ function ccall_macro_lower(ex, convention, func, rettype, types, args, num_varar
10082 cargs = SyntaxTree[]
10183 for (i, (type, arg)) in enumerate (zip (types, args))
10284 argi = @ast ex arg " arg$i " :: K"Identifier"
103- # TODO : Is there any safe way to use SSAValue here ?
104- push! (statements, ast " :(local $argi = Base.cconvert($type, $arg))" )
85+ # TODO : Can we allow this macro to emit SSAValue?
86+ push! (statements, @SyntaxTree :(local $ argi = Base. cconvert ($ type, $ arg)))
10587 push! (roots, argi)
106- push! (cargs, ast " :(Base.unsafe_convert($type, $argi))" )
88+ push! (cargs, @SyntaxTree :(Base. unsafe_convert ($ type, $ argi)))
10789 end
10890 push! (statements,
10991 @ast ex ex [K " foreigncall"
11092 func
11193 rettype
112- ast " :(Core.svec($(types...)))"
94+ @SyntaxTree :(Core. svec ($ (types... )))
11395 # Is this num_varargs correct? It seems wrong?
11496 num_varargs:: K"Integer"
11597 convention:: K"Symbol"
0 commit comments