Class: TRuby::CrossFileTypeChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/t_ruby/cache.rb

Overview

Cross-file Type Checker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_checker: nil) ⇒ CrossFileTypeChecker

Returns a new instance of CrossFileTypeChecker.



444
445
446
447
448
449
450
# File 'lib/t_ruby/cache.rb', line 444

def initialize(type_checker: nil)
  @type_checker = type_checker || TypeChecker.new
  @file_types = {}  # file_path => { types: [], functions: [], interfaces: [] }
  @global_registry = {}  # name => { file: path, kind: :type/:func/:interface, definition: ... }
  @errors = []
  @warnings = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



442
443
444
# File 'lib/t_ruby/cache.rb', line 442

def errors
  @errors
end

#file_typesObject (readonly)

Returns the value of attribute file_types.



442
443
444
# File 'lib/t_ruby/cache.rb', line 442

def file_types
  @file_types
end

#warningsObject (readonly)

Returns the value of attribute warnings.



442
443
444
# File 'lib/t_ruby/cache.rb', line 442

def warnings
  @warnings
end

Instance Method Details

#all_typesObject

Get all registered types



531
532
533
# File 'lib/t_ruby/cache.rb', line 531

def all_types
  @global_registry.keys
end

#check_allObject

Check cross-file type consistency



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/t_ruby/cache.rb', line 476

def check_all
  @errors = []
  @warnings = []

  # Check for duplicate definitions
  check_duplicate_definitions

  # Check for unresolved type references
  check_unresolved_references

  # Check interface implementations
  check_interface_implementations

  {
    success: @errors.empty?,
    errors: @errors,
    warnings: @warnings
  }
end

#check_file(file_path, ir_program) ⇒ Object

Check a specific file against global types



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/t_ruby/cache.rb', line 497

def check_file(file_path, ir_program)
  file_errors = []

  ir_program.declarations.each do |decl|
    case decl
    when IR::MethodDef
      # Check parameter types
      decl.params.each do |param|
        if param.type_annotation
          unless type_exists?(param.type_annotation)
            file_errors << {
              file: file_path,
              message: "Unknown type '#{type_name(param.type_annotation)}' in parameter '#{param.name}'"
            }
          end
        end
      end

      # Check return type
      if decl.return_type
        unless type_exists?(decl.return_type)
          file_errors << {
            file: file_path,
            message: "Unknown return type '#{type_name(decl.return_type)}' in function '#{decl.name}'"
          }
        end
      end
    end
  end

  file_errors
end

#clearObject

Clear all registrations



541
542
543
544
545
546
# File 'lib/t_ruby/cache.rb', line 541

def clear
  @file_types.clear
  @global_registry.clear
  @errors.clear
  @warnings.clear
end

#find_definition(name) ⇒ Object

Find where a type is defined



536
537
538
# File 'lib/t_ruby/cache.rb', line 536

def find_definition(name)
  @global_registry[name]
end

#register_file(file_path, ir_program) ⇒ Object

Register types from a file



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/t_ruby/cache.rb', line 453

def register_file(file_path, ir_program)
  types = []
  functions = []
  interfaces = []

  ir_program.declarations.each do |decl|
    case decl
    when IR::TypeAlias
      types << { name: decl.name, definition: decl.definition }
      register_global(decl.name, file_path, :type, decl)
    when IR::Interface
      interfaces << { name: decl.name, members: decl.members }
      register_global(decl.name, file_path, :interface, decl)
    when IR::MethodDef
      functions << { name: decl.name, params: decl.params, return_type: decl.return_type }
      register_global(decl.name, file_path, :function, decl)
    end
  end

  @file_types[file_path] = { types: types, functions: functions, interfaces: interfaces }
end