Class: RubyBindgen::Generators::Rice::TypeIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bindgen/generators/rice/type_index.rb

Overview

Builds shared lookups for typedef resolution and simple-name qualification. Rice uses this index while walking the AST instead of threading raw hashes through unrelated qualification and inheritance code paths.

Instance Method Summary collapse

Constructor Details

#initializeTypeIndex

Returns a new instance of TypeIndex.



8
9
10
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 8

def initialize
  clear
end

Instance Method Details

#build!(cursor) ⇒ Object

Walk the translation unit once and collect the shared type lookups used by the Rice generator.

Only top-level typedefs/aliases are indexed. Member aliases are skipped because they are only valid through the owning class scope and cannot be reused as generic replacements elsewhere in generated code.



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 31

def build!(cursor)
  clear
  
  cursor.find_by_kind(true, :cursor_typedef_decl, :cursor_type_alias_decl,
                      :cursor_class_template, :cursor_class_decl, :cursor_struct) do |child|
    record_type(child)
  end
  
  self
end

#clearObject

Reset both lookup tables before a fresh AST walk.

‘@typedefs` maps canonical type spellings to the typedef/alias cursor that should win for that canonical type, for example:

"int" => cursor for `using MyInt = int`

‘@qualified_names` maps a simple name to the preferred qualified name:

"Box" => "Example::Box"


20
21
22
23
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 20

def clear
  @typedefs = {}
  @qualified_names = {}
end

#qualified_name_for(simple_name) ⇒ Object

Find the preferred qualified name for a simple type name.

Example:

qualified_name_for("Box")

returns “Example::Box”



56
57
58
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 56

def qualified_name_for(simple_name)
  @qualified_names[simple_name]
end

#record_qualified_name(simple_name, qualified_name, prefer_existing: false) ⇒ Object

Store the preferred qualified spelling for a simple name.

‘prefer_existing: true` is used for class declarations so an earlier alias like `Example::Box` is not overwritten later by the class/template declaration for the same simple name.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 94

def record_qualified_name(simple_name, qualified_name, prefer_existing: false)
  return if simple_name.nil? || simple_name.empty?
  return if simple_name == qualified_name
  
  existing = @qualified_names[simple_name]
  return if prefer_existing && existing
  
  if existing.nil? || prefer_replacement?(existing, qualified_name)
    @qualified_names[simple_name] = qualified_name
  end
end

#record_type(child) ⇒ Object

Record one discovered cursor into the appropriate lookup table.

Typedefs and aliases populate both:

  • canonical type -> preferred typedef cursor

  • simple name -> preferred qualified spelling

Class/template declarations only populate the simple-name lookup because they do not stand in for another canonical type.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 68

def record_type(child)
  case child.kind
  when :cursor_typedef_decl, :cursor_type_alias_decl
    parent_kind = child.semantic_parent.kind
    return if parent_kind == :cursor_class_decl || parent_kind == :cursor_struct ||
              parent_kind == :cursor_class_template || parent_kind == :cursor_class_template_partial_specialization
  
    canonical = child.underlying_type.canonical.spelling
    existing = @typedefs[canonical]
    if existing.nil? || prefer_replacement?(existing.qualified_name, child.qualified_name)
      @typedefs[canonical] = child
    end
  
    record_qualified_name(child.spelling, child.qualified_name)
  when :cursor_class_template, :cursor_class_decl, :cursor_struct
    return if child.spelling.empty?
  
    record_qualified_name(child.spelling, child.qualified_name, prefer_existing: true)
  end
end

#typedef_for(canonical_spelling) ⇒ Object

Find the preferred typedef/alias cursor for a canonical type spelling.

Example:

typedef_for("int")

returns the cursor for ‘using MyInt = int`



47
48
49
# File 'lib/ruby-bindgen/generators/rice/type_index.rb', line 47

def typedef_for(canonical_spelling)
  @typedefs[canonical_spelling]
end