@@ -1706,6 +1706,16 @@ function expand_kw_call(ctx, srcref, farg, args, kws)
17061706 ]
17071707end
17081708
1709+ # Special rule: Any becomes core.Any regardless of the module
1710+ # scope, and don't need GC roots.
1711+ function expand_ccall_argtype (ctx, ex)
1712+ if is_same_identifier_like (ex, " Any" )
1713+ @ast ctx ex " Any" :: K"core"
1714+ else
1715+ expand_forms_2 (ctx, ex)
1716+ end
1717+ end
1718+
17091719# Expand the (sym,lib) argument to ccall/cglobal
17101720function expand_C_library_symbol (ctx, ex)
17111721 expanded = expand_forms_2 (ctx, ex)
@@ -1749,44 +1759,36 @@ function expand_ccall(ctx, ex)
17491759 end
17501760 arg_types = children (arg_type_tuple)
17511761 vararg_type = nothing
1752- num_required_args = length (arg_types)
17531762 if length (arg_types) >= 1
17541763 va = arg_types[end ]
17551764 if kind (va) == K " ..."
17561765 @chk numchildren (va) == 1
17571766 # Ok: vararg function
1758- vararg_type = va
1759- if length (arg_types) <= 1
1760- throw (LoweringError (vararg_type, " C ABI prohibits vararg without one required argument" ))
1761- else
1762- num_required_args = length (arg_types) - 1
1767+ vararg_type = expand_ccall_argtype (ctx, va[1 ])
1768+ arg_types = arg_types[1 : end - 1 ]
1769+ if length (arg_types) === 0
1770+ throw (LoweringError (va, " C ABI prohibits vararg without one required argument" ))
17631771 end
17641772 end
17651773 end
17661774 # todo: use multi-range errors here
1767- if length (args) < num_required_args
1775+ if length (args) < length (arg_types)
17681776 throw (LoweringError (ex, " Too few arguments in ccall compared to argument types" ))
17691777 elseif length (args) > length (arg_types) && isnothing (vararg_type)
17701778 throw (LoweringError (ex, " More arguments than types in ccall" ))
17711779 end
17721780 sctx = with_stmts (ctx)
17731781 expanded_types = SyntaxList (ctx)
1774- for (i, argt) in enumerate ( arg_types)
1782+ for argt in arg_types
17751783 if kind (argt) == K " ..."
1776- if i == length (arg_types)
1777- argt = argt[1 ]
1778- else
1779- throw (LoweringError (argt, " only the trailing ccall argument type should have `...`" ))
1780- end
1781- end
1782- if is_same_identifier_like (argt, " Any" )
1783- # Special rule: Any becomes core.Any regardless of the module
1784- # scope, and don't need GC roots.
1785- argt = @ast ctx argt " Any" :: K"core"
1784+ throw (LoweringError (argt, " only the trailing ccall argument type should have `...`" ))
17861785 end
1787- push! (expanded_types, expand_forms_2 (ctx, argt))
1786+ push! (expanded_types, expand_ccall_argtype (ctx, argt))
17881787 end
1789- #
1788+ for _ in length (arg_types)+ 1 : length (args)
1789+ push! (expanded_types, vararg_type)
1790+ end
1791+
17901792 # An improvement might be wrap the use of types in cconvert in a special
17911793 # K"global_scope" expression which modifies the scope resolution. This
17921794 # would at least make the rules self consistent if not pretty.
@@ -1843,7 +1845,7 @@ function expand_ccall(ctx, ex)
18431845 expanded_types...
18441846 ]
18451847 ]
1846- (isnothing (vararg_type) ? 0 : num_required_args ):: K"Integer"
1848+ (isnothing (vararg_type) ? 0 : length (arg_types) ):: K"Integer"
18471849 if isnothing (cconv)
18481850 " ccall" :: K"Symbol"
18491851 else
0 commit comments