1
- # Lowering Pass 2 - syntax desugaring
1
+ # Lowering Pass 2 - syntax desugaring
2
2
3
3
struct DesugaringContext{GraphType} <: AbstractLoweringContext
4
4
graph:: GraphType
74
74
75
75
function check_no_parameters (ex:: SyntaxTree , msg)
76
76
i = find_parameters_ind (children (ex))
77
- if i > 0
77
+ if i > 0
78
78
throw (LoweringError (ex[i], msg))
79
79
end
80
80
end
@@ -523,7 +523,7 @@ function expand_compare_chain(ctx, ex)
523
523
end
524
524
525
525
# -------------------------------------------------------------------------------
526
- # Expansion of array indexing
526
+ # Expansion of array indexing
527
527
function _arg_to_temp (ctx, stmts, ex, eq_is_kw= false )
528
528
k = kind (ex)
529
529
if is_effect_free (ex)
@@ -939,6 +939,29 @@ function expand_comprehension_to_loops(ctx, ex)
939
939
]
940
940
end
941
941
942
+ function expand_splat (ctx, ex, topfunc, args)
943
+ return @ast ctx ex [K " call"
944
+ " _apply_iterate" :: K"core"
945
+ " iterate" :: K"top"
946
+ topfunc
947
+ expand_forms_2 (ctx, _wrap_unsplatted_args (ctx, ex, args))...
948
+ ]
949
+ end
950
+
951
+ function expand_array (ctx, ex, topfunc)
952
+ args = children (ex)
953
+ check_no_assignment (args)
954
+ topfunc = @ast ctx ex topfunc:: K"top"
955
+ if any (kind (arg) == K " ..." for arg in args)
956
+ expand_splat (ctx, ex, topfunc, args)
957
+ else
958
+ @ast ctx ex [K " call"
959
+ topfunc
960
+ expand_forms_2 (ctx, args)...
961
+ ]
962
+ end
963
+ end
964
+
942
965
# -------------------------------------------------------------------------------
943
966
# Expansion of array concatenation notation `[a b ; c d]` etc
944
967
@@ -1735,10 +1758,10 @@ function expand_ccall(ctx, ex)
1735
1758
if is_core_Any (raw_argt)
1736
1759
push! (unsafe_args, exarg)
1737
1760
else
1738
- cconverted_arg = emit_assign_tmp (sctx,
1761
+ cconverted_arg = emit_assign_tmp (sctx,
1739
1762
@ast ctx argt [K " call"
1740
1763
" cconvert" :: K"top"
1741
- argt
1764
+ argt
1742
1765
exarg
1743
1766
]
1744
1767
)
@@ -1856,12 +1879,7 @@ function expand_call(ctx, ex)
1856
1879
end
1857
1880
if any (kind (arg) == K " ..." for arg in args)
1858
1881
# Splatting, eg, `f(a, xs..., b)`
1859
- @ast ctx ex [K " call"
1860
- " _apply_iterate" :: K"core"
1861
- " iterate" :: K"top"
1862
- expand_forms_2 (ctx, farg)
1863
- expand_forms_2 (ctx, _wrap_unsplatted_args (ctx, ex, args))...
1864
- ]
1882
+ expand_splat (ctx, ex, expand_forms_2 (ctx, farg), args)
1865
1883
elseif kind (farg) == K " Identifier" && farg. name_val == " include"
1866
1884
# world age special case
1867
1885
r = ssavar (ctx, ex)
@@ -1905,7 +1923,7 @@ function expand_dot(ctx, ex)
1905
1923
throw (LoweringError (rhs, " Unrecognized field access syntax" ))
1906
1924
end
1907
1925
@ast ctx ex [K " call"
1908
- " getproperty" :: K"top"
1926
+ " getproperty" :: K"top"
1909
1927
ex[1 ]
1910
1928
rhs
1911
1929
]
@@ -3092,7 +3110,7 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
3092
3110
]
3093
3111
]
3094
3112
]
3095
- [K " removable"
3113
+ [K " removable"
3096
3114
isnothing (bare_func_name) ? " nothing" :: K"core" : bare_func_name
3097
3115
]
3098
3116
]
@@ -3131,7 +3149,7 @@ end
3131
3149
3132
3150
function expand_arrow (ctx, ex)
3133
3151
@chk numchildren (ex) == 2
3134
- expand_forms_2 (ctx,
3152
+ expand_forms_2 (ctx,
3135
3153
@ast ctx ex [K " function"
3136
3154
expand_arrow_arglist (ctx, ex[1 ], string (kind (ex)))
3137
3155
ex[2 ]
@@ -3515,7 +3533,7 @@ function default_inner_constructors(ctx, srcref, global_struct_name,
3515
3533
[K " curly"
3516
3534
" Type" :: K"core"
3517
3535
[K " curly"
3518
- global_struct_name
3536
+ global_struct_name
3519
3537
typevar_names...
3520
3538
]
3521
3539
]
@@ -3570,7 +3588,7 @@ function default_outer_constructor(ctx, srcref, global_struct_name,
3570
3588
typevar_names, typevar_stmts, field_names, field_types)
3571
3589
@ast ctx srcref [K " function"
3572
3590
[K " where"
3573
- [K " call"
3591
+ [K " call"
3574
3592
# We use `::Type{$global_struct_name}` here rather than just
3575
3593
# `struct_name` because global_struct_name is a binding to a
3576
3594
# type - we know we're not creating a new `Function` and
@@ -4189,7 +4207,7 @@ function expand_module(ctx, ex::SyntaxTree)
4189
4207
" x" :: K"Identifier"
4190
4208
]
4191
4209
[K " call"
4192
- " eval" :: K"core"
4210
+ " eval" :: K"core"
4193
4211
modname :: K"Identifier"
4194
4212
" x" :: K"Identifier"
4195
4213
]
@@ -4216,11 +4234,11 @@ function expand_module(ctx, ex::SyntaxTree)
4216
4234
" x" :: K"Identifier"
4217
4235
]
4218
4236
[K " call"
4219
- " _call_latest" :: K"core"
4220
- " include" :: K"top"
4221
- " mapexpr" :: K"Identifier"
4222
- modname :: K"Identifier"
4223
- " x" :: K"Identifier"
4237
+ " _call_latest" :: K"core"
4238
+ " include" :: K"top"
4239
+ " mapexpr" :: K"Identifier"
4240
+ modname :: K"Identifier"
4241
+ " x" :: K"Identifier"
4224
4242
]
4225
4243
]
4226
4244
]
@@ -4254,7 +4272,7 @@ end
4254
4272
4255
4273
"""
4256
4274
Lowering pass 2 - desugaring
4257
-
4275
+
4258
4276
This pass simplifies expressions by expanding complicated syntax sugar into a
4259
4277
small set of core syntactic forms. For example, field access syntax `a.b` is
4260
4278
expanded to a function call `getproperty(a, :b)`.
@@ -4377,7 +4395,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
4377
4395
if numchildren (ex) == 1 && kind (ex[1 ]) == K " String"
4378
4396
ex[1 ]
4379
4397
else
4380
- @ast ctx ex [K " call"
4398
+ @ast ctx ex [K " call"
4381
4399
" string" :: K"top"
4382
4400
expand_forms_2 (ctx, children (ex))...
4383
4401
]
@@ -4393,7 +4411,7 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
4393
4411
elseif any_assignment (children (ex))
4394
4412
expand_forms_2 (ctx, expand_named_tuple (ctx, ex, children (ex)))
4395
4413
else
4396
- expand_forms_2 (ctx, @ast ctx ex [K " call"
4414
+ expand_forms_2 (ctx, @ast ctx ex [K " call"
4397
4415
" tuple" :: K"core"
4398
4416
children (ex)...
4399
4417
])
@@ -4438,23 +4456,11 @@ function expand_forms_2(ctx::DesugaringContext, ex::SyntaxTree, docs=nothing)
4438
4456
]
4439
4457
elseif k == K " vect"
4440
4458
check_no_parameters (ex, " unexpected semicolon in array expression" )
4441
- check_no_assignment (children (ex))
4442
- @ast ctx ex [K " call"
4443
- " vect" :: K"top"
4444
- expand_forms_2 (ctx, children (ex))...
4445
- ]
4459
+ expand_array (ctx, ex, " vect" )
4446
4460
elseif k == K " hcat"
4447
- check_no_assignment (children (ex))
4448
- @ast ctx ex [K " call"
4449
- " hcat" :: K"top"
4450
- expand_forms_2 (ctx, children (ex))...
4451
- ]
4461
+ expand_array (ctx, ex, " hcat" )
4452
4462
elseif k == K " typed_hcat"
4453
- check_no_assignment (children (ex))
4454
- @ast ctx ex [K " call"
4455
- " typed_hcat" :: K"top"
4456
- expand_forms_2 (ctx, children (ex))...
4457
- ]
4463
+ expand_array (ctx, ex, " typed_hcat" )
4458
4464
elseif k == K " opaque_closure"
4459
4465
expand_forms_2 (ctx, expand_opaque_closure (ctx, ex))
4460
4466
elseif k == K " vcat" || k == K " typed_vcat"
@@ -4513,4 +4519,3 @@ function expand_forms_2(ctx::MacroExpansionContext, ex::SyntaxTree)
4513
4519
ex1 = expand_forms_2 (ctx1, reparent (ctx1, ex))
4514
4520
ctx1, ex1
4515
4521
end
4516
-
0 commit comments