11# frozen_string_literal: true
2+ # typed: true
3+
4+ require "sorbet-runtime"
25
36require_relative "obj"
47
58require_relative "../idl/passes/gen_option_adoc"
69
710# A CSR field object
811class CsrField < DatabaseObject
12+ extend T ::Sig ;
13+
914 # @return [Csr] The Csr that defines this field
1015 attr_reader :parent
1116
@@ -67,7 +72,7 @@ def optional_in_cfg?(cfg_arch)
6772 # This does not take the parent CSR into account, i.e., a field can be unaffected
6873 # by ext_ver even if the parent CSR is affected
6974 def affected_by? ( ext_ver )
70- @data [ "definedBy" ] . nil? ? false : defined_by_condition . possibly_satisfied_by? ( version )
75+ @data [ "definedBy" ] . nil? ? false : defined_by_condition . possibly_satisfied_by? ( ext_ver )
7176 end
7277
7378 # @return [Idl::FunctionBodyAst] Abstract syntax tree of the type() function
@@ -168,6 +173,7 @@ def type(effective_xlen = nil)
168173 @type ||= { 32 => nil , 64 => nil }
169174 return @type [ effective_xlen ] unless @type [ effective_xlen ] . nil?
170175
176+ type = T . let ( nil , T . untyped )
171177 type =
172178 if @data . key? ( "type" )
173179 @data [ "type" ]
@@ -223,7 +229,7 @@ def type(effective_xlen = nil)
223229 def type_pretty ( effective_xlen = nil )
224230 raise ArgumentError , "Expecting Integer" unless effective_xlen . nil? || effective_xlen . is_a? ( Integer )
225231
226- str = nil
232+ str = T . let ( nil , T . nilable ( String ) )
227233 value_result = Idl ::AstNode . value_try do
228234 str = type ( effective_xlen )
229235 end
@@ -241,7 +247,7 @@ def alias
241247 if @data . key? ( "alias" )
242248 raise "Can't parse alias" unless data [ "alias" ] =~ /^[a-z][a-z0-9]+\. [A-Z0-9]+(\[ ([0-9]+)(:[0-9]+)?\] )?$/
243249
244- csr_name = Regexp . last_match ( 1 )
250+ csr_name = T . must ( Regexp . last_match ( 1 ) )
245251 csr_field = Regexp . last_match ( 2 )
246252 range = Regexp . last_match ( 3 )
247253 range_start = Regexp . last_match ( 4 )
@@ -250,7 +256,7 @@ def alias
250256 csr_field = cfg_arch . csr ( csr_name ) . field ( csr_field )
251257 range =
252258 if range . nil?
253- field . location
259+ csr_field . location
254260 elsif range_end . nil?
255261 ( range_start . to_i ..range_start . to_i )
256262 else
@@ -374,7 +380,7 @@ def reset_value
374380 else
375381 ast = pruned_reset_value_ast
376382 symtab = fill_symtab_for_reset ( ast )
377- val = nil
383+ val = T . let ( nil , T . untyped )
378384 value_result = Idl ::AstNode . value_try do
379385 val = ast . return_value ( symtab )
380386 end
@@ -398,7 +404,7 @@ def dynamic_reset_value?
398404 end
399405
400406 def reset_value_pretty
401- str = nil
407+ str = T . let ( nil , T . nilable ( String ) )
402408 value_result = Idl ::AstNode . value_try do
403409 str = reset_value
404410 end
@@ -591,6 +597,7 @@ def pruned_sw_write_ast(effective_xlen)
591597 # @param cfg_arch [ConfiguredArchitecture] A config. May be nil if the location is not configturation-dependent
592598 # @param effective_xlen [Integer] The effective xlen, needed since some fields change location with XLEN. If the field location is not determined by XLEN, then this parameter can be nil
593599 # @return [Range] the location within the CSR as a range (single bit fields will be a range of size 1)
600+ sig { params ( effective_xlen : T . nilable ( Integer ) ) . returns ( T ::Range [ Integer ] ) }
594601 def location ( effective_xlen = nil )
595602 key =
596603 if @data . key? ( "location" )
@@ -615,7 +622,9 @@ def location(effective_xlen = nil)
615622 end
616623
617624 @data [ key ] ..@data [ key ]
618- elsif @data [ key ] . is_a? ( String )
625+ else
626+ raise "Unexpected location field" unless @data [ key ] . is_a? ( String )
627+
619628 e , s = @data [ key ] . split ( "-" ) . map ( &:to_i )
620629 raise "Invalid location" if s > e
621630
0 commit comments