11#![ allow( dead_code) ]
22use crate :: ast_ids:: NodeKey ;
3+ use crate :: db:: { HasJar , QueryResult , SemanticDb , SemanticJar } ;
34use crate :: files:: FileId ;
4- use crate :: symbols:: SymbolId ;
5+ use crate :: symbols:: { ScopeId , SymbolId } ;
56use crate :: { FxDashMap , FxIndexSet , Name } ;
67use ruff_index:: { newtype_index, IndexVec } ;
78use rustc_hash:: FxHashMap ;
@@ -124,8 +125,15 @@ impl TypeStore {
124125 . add_function ( name, decorators)
125126 }
126127
127- fn add_class ( & self , file_id : FileId , name : & str , bases : Vec < Type > ) -> ClassTypeId {
128- self . add_or_get_module ( file_id) . add_class ( name, bases)
128+ fn add_class (
129+ & self ,
130+ file_id : FileId ,
131+ name : & str ,
132+ scope_id : ScopeId ,
133+ bases : Vec < Type > ,
134+ ) -> ClassTypeId {
135+ self . add_or_get_module ( file_id)
136+ . add_class ( name, scope_id, bases)
129137 }
130138
131139 fn add_union ( & mut self , file_id : FileId , elems : & [ Type ] ) -> UnionTypeId {
@@ -253,6 +261,24 @@ pub struct ClassTypeId {
253261 class_id : ModuleClassTypeId ,
254262}
255263
264+ impl ClassTypeId {
265+ fn get_own_class_member < Db > ( self , db : & Db , name : & Name ) -> QueryResult < Option < Type > >
266+ where
267+ Db : SemanticDb + HasJar < SemanticJar > ,
268+ {
269+ // TODO: this should distinguish instance-only members (e.g. `x: int`) and not return them
270+ let ClassType { scope_id, .. } = * db. jar ( ) ?. type_store . get_class ( self ) ;
271+ let table = db. symbol_table ( self . file_id ) ?;
272+ if let Some ( symbol_id) = table. symbol_id_by_name ( scope_id, name) {
273+ Ok ( Some ( db. infer_symbol_type ( self . file_id , symbol_id) ?) )
274+ } else {
275+ Ok ( None )
276+ }
277+ }
278+
279+ // TODO: get_own_instance_member, get_class_member, get_instance_member
280+ }
281+
256282#[ derive( Copy , Clone , Debug , Hash , Eq , PartialEq ) ]
257283pub struct UnionTypeId {
258284 file_id : FileId ,
@@ -318,9 +344,10 @@ impl ModuleTypeStore {
318344 }
319345 }
320346
321- fn add_class ( & mut self , name : & str , bases : Vec < Type > ) -> ClassTypeId {
347+ fn add_class ( & mut self , name : & str , scope_id : ScopeId , bases : Vec < Type > ) -> ClassTypeId {
322348 let class_id = self . classes . push ( ClassType {
323349 name : Name :: new ( name) ,
350+ scope_id,
324351 // TODO: if no bases are given, that should imply [object]
325352 bases,
326353 } ) ;
@@ -405,7 +432,11 @@ impl std::fmt::Display for DisplayType<'_> {
405432
406433#[ derive( Debug ) ]
407434pub ( crate ) struct ClassType {
435+ /// Name of the class at definition
408436 name : Name ,
437+ /// `ScopeId` of the class body
438+ pub ( crate ) scope_id : ScopeId ,
439+ /// Types of all class bases
409440 bases : Vec < Type > ,
410441}
411442
@@ -496,6 +527,7 @@ impl IntersectionType {
496527#[ cfg( test) ]
497528mod tests {
498529 use crate :: files:: Files ;
530+ use crate :: symbols:: SymbolTable ;
499531 use crate :: types:: { Type , TypeStore } ;
500532 use crate :: FxIndexSet ;
501533 use std:: path:: Path ;
@@ -505,7 +537,7 @@ mod tests {
505537 let store = TypeStore :: default ( ) ;
506538 let files = Files :: default ( ) ;
507539 let file_id = files. intern ( Path :: new ( "/foo" ) ) ;
508- let id = store. add_class ( file_id, "C" , Vec :: new ( ) ) ;
540+ let id = store. add_class ( file_id, "C" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
509541 assert_eq ! ( store. get_class( id) . name( ) , "C" ) ;
510542 let inst = Type :: Instance ( id) ;
511543 assert_eq ! ( format!( "{}" , inst. display( & store) ) , "C" ) ;
@@ -528,8 +560,8 @@ mod tests {
528560 let mut store = TypeStore :: default ( ) ;
529561 let files = Files :: default ( ) ;
530562 let file_id = files. intern ( Path :: new ( "/foo" ) ) ;
531- let c1 = store. add_class ( file_id, "C1" , Vec :: new ( ) ) ;
532- let c2 = store. add_class ( file_id, "C2" , Vec :: new ( ) ) ;
563+ let c1 = store. add_class ( file_id, "C1" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
564+ let c2 = store. add_class ( file_id, "C2" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
533565 let elems = vec ! [ Type :: Instance ( c1) , Type :: Instance ( c2) ] ;
534566 let id = store. add_union ( file_id, & elems) ;
535567 assert_eq ! (
@@ -545,9 +577,9 @@ mod tests {
545577 let mut store = TypeStore :: default ( ) ;
546578 let files = Files :: default ( ) ;
547579 let file_id = files. intern ( Path :: new ( "/foo" ) ) ;
548- let c1 = store. add_class ( file_id, "C1" , Vec :: new ( ) ) ;
549- let c2 = store. add_class ( file_id, "C2" , Vec :: new ( ) ) ;
550- let c3 = store. add_class ( file_id, "C3" , Vec :: new ( ) ) ;
580+ let c1 = store. add_class ( file_id, "C1" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
581+ let c2 = store. add_class ( file_id, "C2" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
582+ let c3 = store. add_class ( file_id, "C3" , SymbolTable :: root_scope_id ( ) , Vec :: new ( ) ) ;
551583 let pos = vec ! [ Type :: Instance ( c1) , Type :: Instance ( c2) ] ;
552584 let neg = vec ! [ Type :: Instance ( c3) ] ;
553585 let id = store. add_intersection ( file_id, & pos, & neg) ;
0 commit comments