1
- use std:: collections:: HashMap ;
2
1
use std:: fmt:: Write ;
3
2
4
3
use anyhow:: Result ;
9
8
pub use ns:: Ns ;
10
9
pub mod source;
11
10
pub use source:: { Files , Source } ;
11
+ mod types;
12
+ pub use types:: { TypeInfo , Types } ;
13
+ mod path;
14
+ pub use path:: name_package_module;
12
15
13
16
#[ derive( Default , Copy , Clone , PartialEq , Eq , Debug ) ]
14
17
pub enum Direction {
@@ -17,229 +20,6 @@ pub enum Direction {
17
20
Export ,
18
21
}
19
22
20
- #[ derive( Default ) ]
21
- pub struct Types {
22
- type_info : HashMap < TypeId , TypeInfo > ,
23
- }
24
-
25
- #[ derive( Default , Clone , Copy , Debug ) ]
26
- pub struct TypeInfo {
27
- /// Whether or not this type is ever used (transitively) within the
28
- /// parameter of an imported function.
29
- ///
30
- /// This means that it's used in a context where ownership isn't
31
- /// relinquished.
32
- pub borrowed : bool ,
33
-
34
- /// Whether or not this type is ever used (transitively) within the
35
- /// parameter or result of an export, or the result of an import.
36
- ///
37
- /// This means that it's used in a context where ownership is required and
38
- /// memory management is necessary.
39
- pub owned : bool ,
40
-
41
- /// Whether or not this type is ever used (transitively) within the
42
- /// error case in the result of a function.
43
- pub error : bool ,
44
-
45
- /// Whether or not this type (transitively) has a list (or string).
46
- pub has_list : bool ,
47
-
48
- /// Whether or not this type (transitively) has a resource (or handle).
49
- pub has_resource : bool ,
50
-
51
- /// Whether or not this type (transitively) has a borrow handle.
52
- pub has_borrow_handle : bool ,
53
-
54
- /// Whether or not this type (transitively) has an own handle.
55
- pub has_own_handle : bool ,
56
- }
57
-
58
- impl std:: ops:: BitOrAssign for TypeInfo {
59
- fn bitor_assign ( & mut self , rhs : Self ) {
60
- self . borrowed |= rhs. borrowed ;
61
- self . owned |= rhs. owned ;
62
- self . error |= rhs. error ;
63
- self . has_list |= rhs. has_list ;
64
- self . has_resource |= rhs. has_resource ;
65
- self . has_borrow_handle |= rhs. has_borrow_handle ;
66
- self . has_own_handle |= rhs. has_own_handle ;
67
- }
68
- }
69
-
70
- impl TypeInfo {
71
- pub fn is_clone ( & self ) -> bool {
72
- !self . has_resource
73
- }
74
- pub fn is_copy ( & self ) -> bool {
75
- !self . has_list && !self . has_resource
76
- }
77
- }
78
-
79
- impl Types {
80
- pub fn analyze ( & mut self , resolve : & Resolve ) {
81
- for ( t, _) in resolve. types . iter ( ) {
82
- self . type_id_info ( resolve, t) ;
83
- }
84
- for ( _, world) in resolve. worlds . iter ( ) {
85
- for ( import, ( _, item) ) in world
86
- . imports
87
- . iter ( )
88
- . map ( |i| ( true , i) )
89
- . chain ( world. exports . iter ( ) . map ( |i| ( false , i) ) )
90
- {
91
- match item {
92
- WorldItem :: Function ( f) => {
93
- self . type_info_func ( resolve, f, import) ;
94
- }
95
- WorldItem :: Interface ( id) => {
96
- for ( _, f) in resolve. interfaces [ * id] . functions . iter ( ) {
97
- self . type_info_func ( resolve, f, import) ;
98
- }
99
- }
100
- WorldItem :: Type ( _) => { }
101
- }
102
- }
103
- }
104
- }
105
-
106
- fn type_info_func ( & mut self , resolve : & Resolve , func : & Function , import : bool ) {
107
- let mut live = LiveTypes :: default ( ) ;
108
- for ( _, ty) in func. params . iter ( ) {
109
- self . type_info ( resolve, ty) ;
110
- live. add_type ( resolve, ty) ;
111
- }
112
- for id in live. iter ( ) {
113
- if resolve. types [ id] . name . is_some ( ) {
114
- let info = self . type_info . get_mut ( & id) . unwrap ( ) ;
115
- if import {
116
- info. borrowed = true ;
117
- } else {
118
- info. owned = true ;
119
- }
120
- }
121
- }
122
- let mut live = LiveTypes :: default ( ) ;
123
- for ty in func. results . iter_types ( ) {
124
- self . type_info ( resolve, ty) ;
125
- live. add_type ( resolve, ty) ;
126
- }
127
- for id in live. iter ( ) {
128
- if resolve. types [ id] . name . is_some ( ) {
129
- self . type_info . get_mut ( & id) . unwrap ( ) . owned = true ;
130
- }
131
- }
132
-
133
- for ty in func. results . iter_types ( ) {
134
- let id = match ty {
135
- Type :: Id ( id) => * id,
136
- _ => continue ,
137
- } ;
138
- let err = match & resolve. types [ id] . kind {
139
- TypeDefKind :: Result ( Result_ { err, .. } ) => err,
140
- _ => continue ,
141
- } ;
142
- if let Some ( Type :: Id ( id) ) = err {
143
- // When an interface `use`s a type from another interface, it creates a new typeid
144
- // referring to the definition typeid. Chase any chain of references down to the
145
- // typeid of the definition.
146
- fn resolve_type_definition_id ( resolve : & Resolve , mut id : TypeId ) -> TypeId {
147
- loop {
148
- match resolve. types [ id] . kind {
149
- TypeDefKind :: Type ( Type :: Id ( def_id) ) => id = def_id,
150
- _ => return id,
151
- }
152
- }
153
- }
154
- let id = resolve_type_definition_id ( resolve, * id) ;
155
- self . type_info . get_mut ( & id) . unwrap ( ) . error = true ;
156
- }
157
- }
158
- }
159
-
160
- pub fn get ( & self , id : TypeId ) -> TypeInfo {
161
- self . type_info [ & id]
162
- }
163
-
164
- pub fn type_id_info ( & mut self , resolve : & Resolve , ty : TypeId ) -> TypeInfo {
165
- if let Some ( info) = self . type_info . get ( & ty) {
166
- return * info;
167
- }
168
- let mut info = TypeInfo :: default ( ) ;
169
- match & resolve. types [ ty] . kind {
170
- TypeDefKind :: Record ( r) => {
171
- for field in r. fields . iter ( ) {
172
- info |= self . type_info ( resolve, & field. ty ) ;
173
- }
174
- }
175
- TypeDefKind :: Resource => {
176
- info. has_resource = true ;
177
- }
178
- TypeDefKind :: Handle ( handle) => {
179
- match handle {
180
- Handle :: Borrow ( _) => info. has_borrow_handle = true ,
181
- Handle :: Own ( _) => info. has_own_handle = true ,
182
- }
183
- info. has_resource = true ;
184
- }
185
- TypeDefKind :: Tuple ( t) => {
186
- for ty in t. types . iter ( ) {
187
- info |= self . type_info ( resolve, ty) ;
188
- }
189
- }
190
- TypeDefKind :: Flags ( _) => { }
191
- TypeDefKind :: Enum ( _) => { }
192
- TypeDefKind :: Variant ( v) => {
193
- for case in v. cases . iter ( ) {
194
- info |= self . optional_type_info ( resolve, case. ty . as_ref ( ) ) ;
195
- }
196
- }
197
- TypeDefKind :: List ( ty) => {
198
- info = self . type_info ( resolve, ty) ;
199
- info. has_list = true ;
200
- }
201
- TypeDefKind :: Type ( ty) => {
202
- info = self . type_info ( resolve, ty) ;
203
- }
204
- TypeDefKind :: Option ( ty) => {
205
- info = self . type_info ( resolve, ty) ;
206
- }
207
- TypeDefKind :: Result ( r) => {
208
- info = self . optional_type_info ( resolve, r. ok . as_ref ( ) ) ;
209
- info |= self . optional_type_info ( resolve, r. err . as_ref ( ) ) ;
210
- }
211
- TypeDefKind :: Future ( ty) => {
212
- info = self . optional_type_info ( resolve, ty. as_ref ( ) ) ;
213
- }
214
- TypeDefKind :: Stream ( stream) => {
215
- info = self . optional_type_info ( resolve, stream. element . as_ref ( ) ) ;
216
- info |= self . optional_type_info ( resolve, stream. end . as_ref ( ) ) ;
217
- }
218
- TypeDefKind :: Unknown => unreachable ! ( ) ,
219
- }
220
- let prev = self . type_info . insert ( ty, info) ;
221
- assert ! ( prev. is_none( ) ) ;
222
- info
223
- }
224
-
225
- pub fn type_info ( & mut self , resolve : & Resolve , ty : & Type ) -> TypeInfo {
226
- let mut info = TypeInfo :: default ( ) ;
227
- match ty {
228
- Type :: String => info. has_list = true ,
229
- Type :: Id ( id) => return self . type_id_info ( resolve, * id) ,
230
- _ => { }
231
- }
232
- info
233
- }
234
-
235
- fn optional_type_info ( & mut self , resolve : & Resolve , ty : Option < & Type > ) -> TypeInfo {
236
- match ty {
237
- Some ( ty) => self . type_info ( resolve, ty) ,
238
- None => TypeInfo :: default ( ) ,
239
- }
240
- }
241
- }
242
-
243
23
pub trait WorldGenerator {
244
24
fn generate ( & mut self , resolve : & Resolve , id : WorldId , files : & mut Files ) -> Result < ( ) > {
245
25
let world = & resolve. worlds [ id] ;
0 commit comments