-
Notifications
You must be signed in to change notification settings - Fork 0
/
typedecl-analyze.jl
73 lines (63 loc) · 2.4 KB
/
typedecl-analyze.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#######################################################################
# Analyzing type declarations
###############################
#
# The actual analysis will be done by the analysis for use-site variance;
# this file only prepares type declarations and transforms them to full types
# amenable by further analysis.
#
#######################################################################
"""
(JlASTTypeExpr, JlASTTypeExpr) → (JlASTTypeExpr, JlASTTypeExpr)
Using type parameters of `tyDecl`, wraps:
- `tyDecl` (type declaration proper) into a where-type with the parameters
in the reversed order;
- the number of type variables;
- `super` (declared supertype of `tyDecl`) into a where-type with bare
parameters (i.e. type vars without bounds) in the reversed order.
E.g. for `(Foo{X,Y<:Ref{X}}, Bar{X})`,
returns `(Foo{X,Y} where Y<:Ref{X} where X, Bar{X} where Y where X)`.
We don't preserve bounds on type variables for the supertype because
eventually, we are interested on non-use-site-variance shape of the supertype
itself, so we don't want to pollute that info with bounds that are already
accounted for in the type declaration itself.
"""
tyDeclAndSuper2FullTypes(tyDecl :: JlASTTypeExpr, super :: JlASTTypeExpr) = begin
(tyDeclBare, tyArgs, tyArgsBare) = splitTyDecl(tyDecl)
(
wrapTyInWhere(tyDeclBare, tyArgs),
length(tyArgs),
wrapTyInWhere(super, tyArgsBare)
)
end
wrapTyInWhere(ty :: JlASTTypeExpr, varDecls :: Vector) =
foldr(wrapTyInWhere, varDecls; init=ty)
wrapTyInWhere(varDecl :: JlASTTypeExpr, ty :: JlASTTypeExpr) =
:($ty where $varDecl)
"""
(JlASTTypeExpr) → (JlASTTypeExpr, Vector{JlASTTypeExpr})
Extracts type parameters from `tyDecl` and returns a tuple of:
- type declaration with variable names only (without bounds)
- original type parameter declarations
- variable names (without bounds)
E.g. for `Foo{Int<:X<:Number, Y<:Vector{X}}`, returns
`(Foo{X, Y}, [Int<:X<:Number, Y<:Vector{X}])`
"""
splitTyDecl(tyDecl :: JlASTTypeExpr) :: Tuple = begin
if @capture(tyDecl, N_{ARGS__})
varsOnly = map(extractVarName, ARGS)
( :($N{$(varsOnly...)}), ARGS, varsOnly )
else
(tyDecl, [], [])
end
end
extractVarName(varDecl :: JlASTTypeExpr) :: Symbol =
if @capture(varDecl, LB_<:V_<:UB_)
V
elseif @capture(varDecl, V_<:UB_)
V
elseif @capture(varDecl, V_>:LB_)
V
else
varDecl
end