22#include "definition.h"
33#include "graph.h"
44#include "handle.h"
5+ #include "reference.h"
56#include "rustbindings.h"
67#include "utils.h"
78
@@ -291,6 +292,42 @@ static VALUE rdxr_declaration_descendants(VALUE self) {
291292 return self ;
292293}
293294
295+ // Size function for the Declaration#references enumerator
296+ static VALUE declaration_references_size (VALUE self , VALUE _args , VALUE _eobj ) {
297+ HandleData * data ;
298+ TypedData_Get_Struct (self , HandleData , & handle_type , data );
299+
300+ void * graph ;
301+ TypedData_Get_Struct (data -> graph_obj , void * , & graph_type , graph );
302+
303+ struct ReferencesIter * iter = rdx_declaration_references_iter_new (graph , data -> id );
304+ size_t len = rdx_references_iter_len (iter );
305+ rdx_references_iter_free (iter );
306+
307+ return SIZET2NUM (len );
308+ }
309+
310+ // Returns an enumerator for all references to this declaration
311+ //
312+ // Declaration#references: () -> Enumerator[Reference]
313+ static VALUE rdxr_declaration_references (VALUE self ) {
314+ if (!rb_block_given_p ()) {
315+ return rb_enumeratorize_with_size (self , rb_str_new2 ("references" ), 0 , NULL , declaration_references_size );
316+ }
317+
318+ HandleData * data ;
319+ TypedData_Get_Struct (self , HandleData , & handle_type , data );
320+
321+ void * graph ;
322+ TypedData_Get_Struct (data -> graph_obj , void * , & graph_type , graph );
323+
324+ void * iter = rdx_declaration_references_iter_new (graph , data -> id );
325+ VALUE args = rb_ary_new_from_args (2 , data -> graph_obj , ULL2NUM ((uintptr_t )iter ));
326+ rb_ensure (rdxi_references_yield , args , rdxi_references_ensure , args );
327+
328+ return self ;
329+ }
330+
294331void rdxi_initialize_declaration (VALUE mRubydex ) {
295332 cDeclaration = rb_define_class_under (mRubydex , "Declaration" , rb_cObject );
296333 cNamespace = rb_define_class_under (mRubydex , "Namespace" , cDeclaration );
@@ -309,6 +346,7 @@ void rdxi_initialize_declaration(VALUE mRubydex) {
309346 rb_define_method (cDeclaration , "name" , rdxr_declaration_name , 0 );
310347 rb_define_method (cDeclaration , "unqualified_name" , rdxr_declaration_unqualified_name , 0 );
311348 rb_define_method (cDeclaration , "definitions" , rdxr_declaration_definitions , 0 );
349+ rb_define_method (cDeclaration , "references" , rdxr_declaration_references , 0 );
312350 rb_define_method (cDeclaration , "owner" , rdxr_declaration_owner , 0 );
313351
314352 // Namespace only methods
0 commit comments