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.



455
456
457
458
459
460
461
# File 'lib/t_ruby/cache.rb', line 455

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.



453
454
455
# File 'lib/t_ruby/cache.rb', line 453

def errors
  @errors
end

#file_typesObject (readonly)

Returns the value of attribute file_types.



453
454
455
# File 'lib/t_ruby/cache.rb', line 453

def file_types
  @file_types
end

#warningsObject (readonly)

Returns the value of attribute warnings.



453
454
455
# File 'lib/t_ruby/cache.rb', line 453

def warnings
  @warnings
end

Instance Method Details

#all_typesObject

Get all registered types



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

def all_types
  @global_registry.keys
end

#check_allObject

Check cross-file type consistency



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/t_ruby/cache.rb', line 487

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



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/t_ruby/cache.rb', line 508

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



548
549
550
551
552
553
# File 'lib/t_ruby/cache.rb', line 548

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

#find_definition(name) ⇒ Object

Find where a type is defined



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

def find_definition(name)
  @global_registry[name]
end

#register_file(file_path, ir_program) ⇒ Object

Register types from a file



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/t_ruby/cache.rb', line 464

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