-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.jl
219 lines (165 loc) · 5.94 KB
/
data.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#######################################################################
# Data types for the analysis of type annotations
# and user-defined type declarations
#######################################################################
"Type of AST expression representing a Julia type"
JlASTTypeExpr = Any
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Extracting type annotations
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
Kind of a type annotation:
- complete method type signature (without return type)
- return type
- type assertion (in the method body) or type annotation (in the field)
"""
@enum TypeAnnKind mtsig retty tyassorann
"Information about a type annotation"
struct TypeAnnInfo
funName :: JlASTTypeExpr
kind :: TypeAnnKind
tyExpr :: JlASTTypeExpr
end
"List of type annotation infos"
TypeAnnInfoList = LinkedList{TypeAnnInfo}
"Data returned by `MacroTools.splitdef`"
SplitFunDef = Dict{Symbol, Any}
NOTAFUNSIG = "<NOT A FUNSIG>"
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Extracting type declarations
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
Kind of a user-defined type declaration:
- abstract type
- primitive type
- struct
- mutable struct
"""
@enum TypeDeclKind tdabst tdprim tdstrc tdmtbl
struct TyDeclArgInfo
var :: Symbol
lb :: JlASTTypeExpr
ub :: JlASTTypeExpr
end
"Information about a user-defined type declaration"
struct TypeDeclInfo
name :: JlASTTypeExpr
kind :: TypeDeclKind
tyDecl :: JlASTTypeExpr
tySuper :: JlASTTypeExpr
end
"List of type declaration infos"
TypeDeclInfoList = LinkedList{TypeDeclInfo}
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# All type info
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
struct TypeInfo
tyAnns :: TypeAnnInfoList
tyDecls :: TypeDeclInfoList
end
TypeInfo() = TypeInfo(nil(TypeAnnInfo), nil(TypeDeclInfo))
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Analyzing type annotations
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
Kind of type constructor
- TCVar means that the variable is used as a target of {}, e.g. X{Int} where X
- TCLBVar1 means that the variable has a single anonymous usage in lower bound, e.g. Ref{>:Int}
"""
@enum TypeConstructor TCTuple TCInvar TCUnion TCWhere TCLoBnd TCUpBnd TCVar TCLBVar1 TCUBVar1 TCCall TCMCall
"Stack of type constructors"
TypeConstrStack = LinkedList{TypeConstructor}
tcsempty() = nil(TypeConstructor)
const DEFAULT_LB = :(Union{})
const DEFAULT_UB = :Any
"""
Information about a type variable in environment
- `name`
- `lb` lowerbound
- `ub` upperbound
- `currPos` keeps track of the current stack of type constructors from
the place where the variable was introduced; the outermost constructor
in the stack corresponds to the current position in the type
(e.g. `-` for `Exists X.Tuple{Ref{-}}` is `Inv->Tuple->Nil`)
- `occurrs` keeps track of every occurrence described as a stack
"""
struct TyVarInfo
name :: Symbol
lb :: JlASTTypeExpr
ub :: JlASTTypeExpr
currPos :: TypeConstrStack
occurrs :: Vector{TypeConstrStack}
end
TyVarInfo(name :: Symbol, lb :: JlASTTypeExpr, ub :: JlASTTypeExpr) =
TyVarInfo(
name, lb, ub,
tcsempty(), TypeConstrStack[]
)
TyVarInfo(name :: Symbol) = TyVarInfo(name, DEFAULT_LB, DEFAULT_UB)
TyVarEnv = LinkedList{TyVarInfo}
struct TyVarSummary
name :: Symbol
lb :: JlASTTypeExpr
ub :: JlASTTypeExpr
occurrs :: Vector{TypeConstrStack}
context :: TypeConstrStack
end
TypeTyVarsSummary = Vector{TyVarSummary}
#--------------------------------------------------
# Type variable environment utilities
#--------------------------------------------------
envempty() = nil(TyVarInfo)
envlookup(
env :: TyVarEnv, name :: Symbol
) :: Union{TyVarInfo, Nothing} = begin
isempty(env) && return nothing
DataStructures.head(env).name == name ?
DataStructures.head(env) :
envlookup(DataStructures.tail(env), name)
end
envadd(env :: TyVarEnv, tv :: TyVarInfo) = cons(tv, env)
envpushconstr(
env :: TyVarEnv, constr :: TypeConstructor
) :: TyVarEnv = begin
map(
tv -> TyVarInfo(
tv.name, tv.lb, tv.ub,
cons(constr, tv.currPos), tv.occurrs
),
env
)
end
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Converting short-hand types into complete form
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@enum TypeTransformationKind TTok TTlb TTub
struct TypeTransInfo
kind :: TypeTransformationKind
expr :: JlASTTypeExpr
bound :: Union{JlASTTypeExpr, Nothing} # !== nothing only if kind != TTok
end
TypeTransInfo(kind :: TypeTransformationKind, expr :: JlASTTypeExpr) =
TypeTransInfo(kind, expr, nothing)
TypeTransInfo(expr :: JlASTTypeExpr) =
TypeTransInfo(TTok, expr)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Base functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#--------------------------------------------------
# Equality
#--------------------------------------------------
Base.:(==)(v1 :: TypeAnnInfo, v2 :: TypeAnnInfo) = structEqual(v1, v2)
Base.:(==)(v1 :: TypeDeclInfo, v2 :: TypeDeclInfo) = structEqual(v1, v2)
Base.:(==)(v1 :: TyDeclArgInfo, v2 :: TyDeclArgInfo) = structEqual(v1, v2)
Base.:(==)(v1 :: TypeInfo, v2 :: TypeInfo) = structEqual(v1, v2)
Base.:(==)(v1 :: TyVarInfo, v2 :: TyVarInfo) = structEqual(v1, v2)
Base.:(==)(v1 :: TyVarSummary, v2 :: TyVarSummary) = structEqual(v1, v2)
Base.:(==)(v1 :: TypeTransInfo, v2 :: TypeTransInfo) = structEqual(v1, v2)
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Extra functions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
csvLineString(ta :: TypeAnnInfo) =
join(
map(v -> "\"" * string(v) * "\"", Any[ta.funName, ta.kind, ta.tyExpr]),
","
) * "\n"