11use ra_ap_base_db:: SourceDatabase ;
22use ra_ap_hir:: db:: DefDatabase ;
3+ use ra_ap_hir_def:: type_ref:: TypeRefId ;
4+ use ra_ap_hir_def:: type_ref:: TypesMap ;
35use std:: hash:: Hash ;
46use std:: hash:: Hasher ;
57use std:: { cmp:: Ordering , collections:: HashMap , path:: PathBuf } ;
@@ -127,7 +129,7 @@ pub fn extract_crate_graph(trap_provider: &trap::TrapFileProvider, db: &RootData
127129 }
128130 ModuleDefId :: ConstId ( konst) => {
129131 let konst = db. const_data ( konst) ;
130- let type_ = emit_hir_typeref ( trap, & konst. type_ref ) ;
132+ let type_ = emit_hir_typeref_ ( trap, & konst . types_map , & konst. type_ref ) ;
131133 values. push ( trap. emit ( generated:: ValueItem {
132134 id : trap:: TrapId :: Star ,
133135 name : name. as_str ( ) . to_owned ( ) ,
@@ -136,7 +138,8 @@ pub fn extract_crate_graph(trap_provider: &trap::TrapFileProvider, db: &RootData
136138 }
137139 ModuleDefId :: StaticId ( statik) => {
138140 let statik = db. static_data ( statik) ;
139- let type_ = emit_hir_typeref ( trap, & statik. type_ref ) ;
141+ let type_ =
142+ emit_hir_typeref_ ( trap, & statik. types_map , & statik. type_ref ) ;
140143 values. push ( trap. emit ( generated:: ValueItem {
141144 id : trap:: TrapId :: Star ,
142145 name : name. as_str ( ) . to_owned ( ) ,
@@ -164,25 +167,23 @@ pub fn extract_crate_graph(trap_provider: &trap::TrapFileProvider, db: &RootData
164167 let variant_data = db. enum_variant_data ( variant_id) ;
165168 let type_ = match variant_data. variant_data . as_ref ( ) {
166169 ra_ap_hir_def:: data:: adt:: VariantData :: Unit => {
167- Some ( emit_hir_typeref ( trap, & ret_type) )
170+ Some ( emit_hir_typeref ( trap, TypesMap :: EMPTY , & ret_type) )
168171 }
169- ra_ap_hir_def:: data:: adt:: VariantData :: Tuple ( arena) => {
172+ ra_ap_hir_def:: data:: adt:: VariantData :: Tuple {
173+ fields,
174+ types_map,
175+ } => {
170176 let params: Vec < _ > =
171- arena . values ( ) . map ( |f| f. type_ref . as_ref ( ) ) . collect ( ) ;
177+ fields . values ( ) . map ( |f| & types_map [ f. type_ref ] ) . collect ( ) ;
172178 Some (
173179 emit_hir_fn (
174- trap,
175- None ,
176- & params[ ..] ,
177- & ret_type,
178- false ,
179- false ,
180- false ,
180+ trap, types_map, None , & params, & ret_type, false ,
181+ false , false ,
181182 )
182183 . into ( ) ,
183184 )
184185 }
185- ra_ap_hir_def:: data:: adt:: VariantData :: Record ( _ ) => {
186+ ra_ap_hir_def:: data:: adt:: VariantData :: Record { .. } => {
186187 // record enums are not "values"
187188 None
188189 }
@@ -283,9 +284,9 @@ pub fn extract_crate_graph(trap_provider: &trap::TrapFileProvider, db: &RootData
283284 . impls ( )
284285 . map ( |imp| {
285286 let imp = db. impl_data ( imp) ;
286- let self_ty = emit_hir_typeref ( trap, & imp. self_ty ) ;
287+ let self_ty = emit_hir_typeref_ ( trap, & imp . types_map , & imp. self_ty ) ;
287288 let target_trait = match imp. target_trait . as_ref ( ) {
288- Some ( t) => emit_hir_path ( & t. path ) ,
289+ Some ( t) => emit_hir_path ( & imp . types_map [ t. path ] ) ,
289290 None => vec ! [ ] ,
290291 } ;
291292 let mut method_names = Vec :: new ( ) ;
@@ -330,20 +331,21 @@ pub fn extract_crate_graph(trap_provider: &trap::TrapFileProvider, db: &RootData
330331
331332fn emit_hir_type_bound (
332333 trap : & mut TrapFile ,
334+ types_map : & TypesMap ,
333335 type_bound : & ra_ap_hir_def:: type_ref:: TypeBound ,
334336) -> Option < trap:: Label < generated:: TypeBoundType > > {
335337 match type_bound {
336338 ra_ap_hir_def:: type_ref:: TypeBound :: Path ( path, _) => Some (
337339 trap. emit ( generated:: TraitTypeBound {
338340 id : trap:: TrapId :: Star ,
339- path : emit_hir_path ( path) ,
341+ path : emit_hir_path ( & types_map [ * path] ) ,
340342 } )
341343 . into ( ) ,
342344 ) ,
343345 ra_ap_hir_def:: type_ref:: TypeBound :: ForLifetime ( names, path) => Some (
344346 trap. emit ( generated:: ForLifetimeTypeBound {
345347 id : trap:: TrapId :: Star ,
346- path : emit_hir_path ( path) ,
348+ path : emit_hir_path ( & types_map [ * path] ) ,
347349 names : names. into_iter ( ) . map ( |x| x. as_str ( ) . to_owned ( ) ) . collect ( ) ,
348350 } )
349351 . into ( ) ,
@@ -355,6 +357,7 @@ fn emit_hir_type_bound(
355357 } )
356358 . into ( ) ,
357359 ) ,
360+ ra_ap_hir_def:: type_ref:: TypeBound :: Use ( _) => None , //TODO
358361 ra_ap_hir_def:: type_ref:: TypeBound :: Error => None ,
359362 }
360363}
@@ -366,17 +369,21 @@ fn emit_hir_path(path: &ra_ap_hir_def::path::Path) -> Vec<String> {
366369}
367370fn emit_hir_fn (
368371 trap : & mut TrapFile ,
372+ types_map : & TypesMap ,
369373 self_type : Option < & TypeRef > ,
370374 params : & [ & TypeRef ] ,
371375 ret_type : & TypeRef ,
372376 is_async : bool ,
373377 is_const : bool ,
374378 is_unsafe : bool ,
375379) -> trap:: Label < generated:: FunctionType > {
376- let ret_type = emit_hir_typeref ( trap, ret_type) ;
380+ let ret_type = emit_hir_typeref ( trap, types_map , ret_type) ;
377381
378- let self_type = self_type. map ( |ty| emit_hir_typeref ( trap, ty) ) ;
379- let params = params. iter ( ) . map ( |t| emit_hir_typeref ( trap, t) ) . collect ( ) ;
382+ let self_type = self_type. map ( |ty| emit_hir_typeref ( trap, types_map, ty) ) ;
383+ let params = params
384+ . iter ( )
385+ . map ( |t| emit_hir_typeref ( trap, types_map, t) )
386+ . collect ( ) ;
380387
381388 trap. emit ( generated:: FunctionType {
382389 id : trap:: TrapId :: Star ,
@@ -393,24 +400,40 @@ fn emit_hir_fn_data(
393400 trap : & mut TrapFile ,
394401 function : & FunctionData ,
395402) -> trap:: Label < generated:: FunctionType > {
396- let params: Vec < _ > = function. params . iter ( ) . map ( |x| x . as_ref ( ) ) . collect ( ) ;
403+ let params = & function. params ;
397404 let ( self_type, params) = if function. has_self_param ( ) {
398405 ( Some ( params[ 0 ] ) , & params[ 1 ..] )
399406 } else {
400407 ( None , & params[ ..] )
401408 } ;
402- let ret_type = function. ret_type . as_ref ( ) ;
409+ let self_type = self_type. map ( |x| & function. types_map [ x] ) ;
410+ let params: Vec < _ > = params. iter ( ) . map ( |x| & function. types_map [ * x] ) . collect ( ) ;
411+ let ret_type = & function. types_map [ function. ret_type ] ;
403412 emit_hir_fn (
404413 trap,
414+ & function. types_map ,
405415 self_type,
406- params,
416+ & params,
407417 ret_type,
408418 function. is_async ( ) ,
409419 function. is_const ( ) ,
410420 function. is_unsafe ( ) ,
411421 )
412422}
413- fn emit_hir_typeref ( trap : & mut TrapFile , ty : & TypeRef ) -> trap:: Label < generated:: Type > {
423+
424+ fn emit_hir_typeref_ (
425+ trap : & mut TrapFile ,
426+ types_map : & TypesMap ,
427+ ty : & TypeRefId ,
428+ ) -> trap:: Label < generated:: Type > {
429+ emit_hir_typeref ( trap, types_map, & types_map[ * ty] )
430+ }
431+
432+ fn emit_hir_typeref (
433+ trap : & mut TrapFile ,
434+ types_map : & TypesMap ,
435+ ty : & TypeRef ,
436+ ) -> trap:: Label < generated:: Type > {
414437 match ty {
415438 TypeRef :: Never => trap
416439 . emit ( generated:: NeverType {
@@ -427,7 +450,7 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
427450 TypeRef :: Tuple ( fields) => {
428451 let fields = fields
429452 . iter ( )
430- . map ( |field| emit_hir_typeref ( trap, field) )
453+ . map ( |field| emit_hir_typeref_ ( trap, types_map , field) )
431454 . collect ( ) ;
432455
433456 trap. emit ( generated:: TupleType {
@@ -437,7 +460,7 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
437460 . into ( )
438461 }
439462 TypeRef :: RawPtr ( type_ref, mutability) => {
440- let type_ = emit_hir_typeref ( trap, type_ref) ;
463+ let type_ = emit_hir_typeref_ ( trap, types_map , type_ref) ;
441464
442465 trap. emit ( generated:: RawPtrType {
443466 id : trap:: TrapId :: Star ,
@@ -446,19 +469,22 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
446469 } )
447470 . into ( )
448471 }
449- TypeRef :: Reference ( type_ref, lifetime_ref, mutability) => {
450- let type_ = emit_hir_typeref ( trap, type_ref) ;
451- let lifetime = lifetime_ref. as_ref ( ) . map ( |x| x. name . as_str ( ) . to_owned ( ) ) ;
472+ TypeRef :: Reference ( ref_type) => {
473+ let type_ = emit_hir_typeref_ ( trap, types_map, & ref_type. ty ) ;
474+ let lifetime = ref_type
475+ . lifetime
476+ . as_ref ( )
477+ . map ( |x| x. name . as_str ( ) . to_owned ( ) ) ;
452478 trap. emit ( generated:: ReferenceType {
453479 id : trap:: TrapId :: Star ,
454- is_mut : mutability. is_mut ( ) ,
480+ is_mut : ref_type . mutability . is_mut ( ) ,
455481 type_,
456482 lifetime,
457483 } )
458484 . into ( )
459485 }
460- TypeRef :: Array ( type_ref , _const_ref ) => {
461- let type_ = emit_hir_typeref ( trap, type_ref ) ;
486+ TypeRef :: Array ( array_type ) => {
487+ let type_ = emit_hir_typeref_ ( trap, types_map , & array_type . ty ) ;
462488 // TODO: handle array size constant
463489 trap. emit ( generated:: ArrayType {
464490 id : trap:: TrapId :: Star ,
@@ -467,24 +493,25 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
467493 . into ( )
468494 }
469495 TypeRef :: Slice ( type_ref) => {
470- let type_ = emit_hir_typeref ( trap, type_ref) ;
496+ let type_ = emit_hir_typeref_ ( trap, types_map , type_ref) ;
471497 trap. emit ( generated:: SliceType {
472498 id : trap:: TrapId :: Star ,
473499 type_,
474500 } )
475501 . into ( )
476502 }
477- TypeRef :: Fn ( params , _ , is_unsafe , _symbol ) => {
478- let ( ret_type, params) = params. split_last ( ) . unwrap ( ) ;
479- let params: Vec < _ > = params. as_ref ( ) . iter ( ) . map ( |x| & x. 1 ) . collect ( ) ;
503+ TypeRef :: Fn ( fn_type ) => {
504+ let ( ret_type, params) = fn_type . params ( ) . split_last ( ) . unwrap ( ) ;
505+ let params: Vec < _ > = params. as_ref ( ) . iter ( ) . map ( |x| & types_map [ x. 1 ] ) . collect ( ) ;
480506 emit_hir_fn (
481507 trap,
508+ types_map,
482509 None ,
483- & params[ .. ] ,
484- & ret_type. 1 ,
510+ & params,
511+ & types_map [ ret_type. 1 ] ,
485512 false ,
486513 false ,
487- * is_unsafe,
514+ fn_type . is_unsafe ( ) ,
488515 )
489516 . into ( )
490517 }
@@ -499,7 +526,7 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
499526 TypeRef :: ImplTrait ( type_bounds) => {
500527 let type_bounds = type_bounds
501528 . iter ( )
502- . flat_map ( |t| emit_hir_type_bound ( trap, t) )
529+ . flat_map ( |t| emit_hir_type_bound ( trap, types_map , t) )
503530 . collect ( ) ;
504531 trap. emit ( generated:: ImplTraitType {
505532 id : trap:: TrapId :: Star ,
@@ -510,7 +537,7 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> trap::Label<generated:
510537 TypeRef :: DynTrait ( type_bounds) => {
511538 let type_bounds = type_bounds
512539 . iter ( )
513- . flat_map ( |t| emit_hir_type_bound ( trap, t) )
540+ . flat_map ( |t| emit_hir_type_bound ( trap, types_map , t) )
514541 . collect ( ) ;
515542 trap. emit ( generated:: DynTraitType {
516543 id : trap:: TrapId :: Star ,
@@ -531,19 +558,26 @@ fn emit_variant_data(
531558 variant : & VariantData ,
532559) -> Option < trap:: Label < generated:: VariantData > > {
533560 match variant {
534- VariantData :: Record ( field_data) | VariantData :: Tuple ( field_data) => {
561+ VariantData :: Record {
562+ fields : field_data,
563+ types_map,
564+ }
565+ | VariantData :: Tuple {
566+ fields : field_data,
567+ types_map,
568+ } => {
535569 let mut types = Vec :: new ( ) ;
536570 let mut fields = Vec :: new ( ) ;
537571 for field in field_data. values ( ) {
538- let tp = emit_hir_typeref ( trap, & field. type_ref ) ;
572+ let tp = emit_hir_typeref_ ( trap, types_map , & field. type_ref ) ;
539573 fields. push ( field. name . as_str ( ) . to_owned ( ) ) ;
540574 types. push ( tp) ;
541575 }
542576 Some ( trap. emit ( generated:: VariantData {
543577 id : trap:: TrapId :: Star ,
544578 types,
545579 fields,
546- is_record : matches ! ( variant, VariantData :: Record ( _ ) ) ,
580+ is_record : matches ! ( variant, VariantData :: Record { .. } ) ,
547581 } ) )
548582 }
549583 VariantData :: Unit => None ,
0 commit comments