forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeEnv.ml
198 lines (175 loc) · 6.13 KB
/
TypeEnv.ml
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
open GenTypeCommon
type moduleEquation = {internal: bool; dep: dep}
type t = {
mutable map: entry StringMap.t;
mutable mapModuleTypes: (Typedtree.signature * t) StringMap.t;
mutable moduleEquation: moduleEquation option;
mutable moduleItem: Runtime.moduleItem;
name: string;
parent: t option;
typeEquations: type_ StringMap.t;
}
and entry = Module of t | Type of string
let createTypeEnv ~name parent =
let moduleItem = Runtime.newModuleItem ~name in
{
map = StringMap.empty;
mapModuleTypes = StringMap.empty;
moduleEquation = None;
moduleItem;
name;
parent;
typeEquations = StringMap.empty;
}
let root () = None |> createTypeEnv ~name:"__root__"
let toString typeEnv = typeEnv.name
let newModule ~name typeEnv =
if !Debug.typeEnv then
Log_.item "TypeEnv.newModule %s %s\n" (typeEnv |> toString) name;
let newTypeEnv = Some typeEnv |> createTypeEnv ~name in
typeEnv.map <- typeEnv.map |> StringMap.add name (Module newTypeEnv);
newTypeEnv
let newModuleType ~name ~signature typeEnv =
if !Debug.typeEnv then
Log_.item "TypeEnv.newModuleType %s %s\n" (typeEnv |> toString) name;
let newTypeEnv = Some typeEnv |> createTypeEnv ~name in
typeEnv.mapModuleTypes <-
typeEnv.mapModuleTypes |> StringMap.add name (signature, newTypeEnv);
newTypeEnv
let newType ~name typeEnv =
if !Debug.typeEnv then
Log_.item "TypeEnv.newType %s %s\n" (typeEnv |> toString) name;
typeEnv.map <- typeEnv.map |> StringMap.add name (Type name)
let getModule ~name typeEnv =
match typeEnv.map |> StringMap.find name with
| Module typeEnv1 -> Some typeEnv1
| Type _ -> None
| exception Not_found -> None
let expandAliasToExternalModule ~name typeEnv =
match typeEnv |> getModule ~name with
| Some {moduleEquation = Some {internal = false; dep}} ->
if !Debug.typeEnv then
Log_.item "TypeEnv.expandAliasToExternalModule %s %s aliased to %s\n"
(typeEnv |> toString) name (dep |> depToString);
Some dep
| _ -> None
let addModuleEquation ~dep ~internal typeEnv =
if !Debug.typeEnv then
Log_.item "Typenv.addModuleEquation %s %s dep:%s\n" (typeEnv |> toString)
(match internal with
| true -> "Internal"
| false -> "External")
(dep |> depToString);
typeEnv.moduleEquation <- Some {internal; dep}
let rec addTypeEquation ~flattened ~type_ typeEnv =
match flattened with
| [name] ->
{
typeEnv with
typeEquations = typeEnv.typeEquations |> StringMap.add name type_;
}
| moduleName :: rest -> (
match typeEnv |> getModule ~name:moduleName with
| Some typeEnv1 ->
{
typeEnv with
map =
typeEnv.map
|> StringMap.add moduleName
(Module (typeEnv1 |> addTypeEquation ~flattened:rest ~type_));
}
| None -> typeEnv)
| [] -> typeEnv
let addTypeEquations ~typeEquations typeEnv =
typeEquations
|> List.fold_left
(fun te (longIdent, type_) ->
te
|> addTypeEquation ~flattened:(longIdent |> Longident.flatten) ~type_)
typeEnv
let applyTypeEquations ~config ~path typeEnv =
match path with
| Path.Pident id -> (
match typeEnv.typeEquations |> StringMap.find (id |> Ident.name) with
| type_ ->
if !Debug.typeResolution then
Log_.item "Typenv.applyTypeEquations %s name:%s type_:%s\n"
(typeEnv |> toString) (id |> Ident.name)
(type_
|> EmitType.typeToString ~config ~typeNameIsInterface:(fun _ -> false)
);
Some type_
| exception Not_found -> None)
| _ -> None
let rec lookup ~name typeEnv =
match typeEnv.map |> StringMap.find name with
| _ -> Some typeEnv
| exception Not_found -> (
match typeEnv.parent with
| None -> None
| Some parent -> parent |> lookup ~name)
let rec lookupModuleType ~path typeEnv =
match path with
| [moduleTypeName] -> (
if !Debug.typeEnv then
Log_.item "Typenv.lookupModuleType %s moduleTypeName:%s\n"
(typeEnv |> toString) moduleTypeName;
match typeEnv.mapModuleTypes |> StringMap.find moduleTypeName with
| x -> Some x
| exception Not_found -> (
match typeEnv.parent with
| None -> None
| Some parent -> parent |> lookupModuleType ~path))
| moduleName :: path1 -> (
if !Debug.typeEnv then
Log_.item "Typenv.lookupModuleType %s moduleName:%s\n"
(typeEnv |> toString) moduleName;
match typeEnv.map |> StringMap.find moduleName with
| Module typeEnv1 -> typeEnv1 |> lookupModuleType ~path:path1
| Type _ -> None
| exception Not_found -> (
match typeEnv.parent with
| None -> None
| Some parent -> parent |> lookupModuleType ~path))
| [] -> None
let rec pathToList path =
match path with
| Path.Pident id -> [id |> Ident.name]
| Path.Pdot (p, s, _) -> s :: (p |> pathToList)
| Path.Papply _ -> []
let lookupModuleTypeSignature ~path typeEnv =
if !Debug.typeEnv then
Log_.item "TypeEnv.lookupModuleTypeSignature %s %s\n" (typeEnv |> toString)
(path |> Path.name);
typeEnv |> lookupModuleType ~path:(path |> pathToList |> List.rev)
let updateModuleItem ~moduleItem typeEnv = typeEnv.moduleItem <- moduleItem
let rec addModulePath ~typeEnv name =
match typeEnv.parent with
| None -> name |> ResolvedName.fromString
| Some parent ->
typeEnv.name |> addModulePath ~typeEnv:parent |> ResolvedName.dot name
let rec getModuleEquations typeEnv : ResolvedName.eq list =
let subEquations =
typeEnv.map |> StringMap.bindings
|> List.map (fun (_, entry) ->
match entry with
| Module te -> te |> getModuleEquations
| Type _ -> [])
|> List.concat
in
match (typeEnv.moduleEquation, typeEnv.parent) with
| None, _ | _, None -> subEquations
| Some {dep}, Some parent ->
[(dep |> depToResolvedName, typeEnv.name |> addModulePath ~typeEnv:parent)]
let getModuleAccessPath ~name typeEnv =
let rec accessPath typeEnv =
match typeEnv.parent with
| None -> Runtime.Root name (* not nested *)
| Some parent ->
Dot
( (match parent.parent = None with
| true -> Root typeEnv.name
| false -> parent |> accessPath),
typeEnv.moduleItem )
in
typeEnv |> accessPath