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



450
451
452
453
454
455
456
# File 'lib/t_ruby/cache.rb', line 450

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.



448
449
450
# File 'lib/t_ruby/cache.rb', line 448

def errors
  @errors
end

#file_typesObject (readonly)

Returns the value of attribute file_types.



448
449
450
# File 'lib/t_ruby/cache.rb', line 448

def file_types
  @file_types
end

#warningsObject (readonly)

Returns the value of attribute warnings.



448
449
450
# File 'lib/t_ruby/cache.rb', line 448

def warnings
  @warnings
end

Instance Method Details

#all_typesObject

Get all registered types



533
534
535
# File 'lib/t_ruby/cache.rb', line 533

def all_types
  @global_registry.keys
end

#check_allObject

Check cross-file type consistency



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/t_ruby/cache.rb', line 482

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



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
529
530
# File 'lib/t_ruby/cache.rb', line 503

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|
        next unless param.type_annotation && !type_exists?(param.type_annotation)

        file_errors << {
          file: file_path,
          message: "Unknown type '#{type_name(param.type_annotation)}' in parameter '#{param.name}'",
        }
      end

      # Check return type
      if decl.return_type && !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

  file_errors
end

#clearObject

Clear all registrations



543
544
545
546
547
548
# File 'lib/t_ruby/cache.rb', line 543

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

#find_definition(name) ⇒ Object

Find where a type is defined



538
539
540
# File 'lib/t_ruby/cache.rb', line 538

def find_definition(name)
  @global_registry[name]
end

#register_file(file_path, ir_program) ⇒ Object

Register types from a file



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/t_ruby/cache.rb', line 459

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