Class: Udb::DatabaseObject

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/obj/database_obj.rb,
lib/udb/exception_code.rb

Overview

a bunch of useful methods for both proper top-level DatabaseObject and sub-objects like CsrField

Defined Under Namespace

Classes: Kind

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, data_path, arch, kind, name: nil)

Parameters:

  • data (Hash{String => T.untyped})

    Hash with fields to be added

  • data_path (String, Pathname)

    Path to the data file

  • arch (ConfiguredArchitecture)
  • kind (Kind)
  • name (String, nil) (defaults to: nil)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/udb/obj/database_obj.rb', line 83

def initialize(data, data_path, arch, kind, name: nil)
  @data = data
  @data_path = Pathname.new(data_path)
  if arch.is_a?(ConfiguredArchitecture)
    @cfg_arch = arch
  end
  @arch = arch
  raise "name must be given" if name.nil? && data["name"].nil?
  raise "do not provide name when it exists in data" if !name.nil? && !data["name"].nil?

  @name = name || data["name"]
  @long_name = data["long_name"]
  @kind = kind

  @sem = Concurrent::Semaphore.new(1)
  @cache = Concurrent::Hash.new
end

Instance Attribute Details

#archConfiguredArchitecture? (readonly)

Returns:



58
59
60
# File 'lib/udb/obj/database_obj.rb', line 58

def arch
  @arch
end

#dataHash{String => T.untyped} (readonly)

Returns:

  • (Hash{String => T.untyped})


40
41
42
# File 'lib/udb/obj/database_obj.rb', line 40

def data
  @data
end

#data_pathPathname (readonly)

Returns:

  • (Pathname)


43
44
45
# File 'lib/udb/obj/database_obj.rb', line 43

def data_path
  @data_path
end

#long_nameString (readonly)

Returns:

  • (String)


49
50
51
# File 'lib/udb/obj/database_obj.rb', line 49

def long_name
  @long_name
end

#nameString (readonly)

Returns:

  • (String)


46
47
48
# File 'lib/udb/obj/database_obj.rb', line 46

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Integer?

Parameters:

Returns:

  • (Integer, nil)


111
112
113
114
115
# File 'lib/udb/obj/database_obj.rb', line 111

def <=>(other)
  return nil unless other.is_a?(DatabaseObject)

  name <=> other.name
end

#__sourcenil, String

Returns:

  • (nil)

    if the source isn’t known

  • (String, nil)

    Source file that data for this object can be attributed to



120
121
122
# File 'lib/udb/obj/database_obj.rb', line 120

def __source
  @data["$source"]
end

#cfg_archnil, ConfiguredArchitecture

Returns:



63
64
65
66
67
# File 'lib/udb/obj/database_obj.rb', line 63

def cfg_arch
  raise "no cfg_arch" if @cfg_arch.nil?

  @cfg_arch
end

#cfg_arch?Boolean

Returns:

  • (Boolean)


70
# File 'lib/udb/obj/database_obj.rb', line 70

def cfg_arch? = !@cfg_arch.nil?

#clone(arch: nil) ⇒ DatabaseObject

clone this, and set the arch at the same time

Parameters:

Returns:



104
105
106
107
108
# File 'lib/udb/obj/database_obj.rb', line 104

def clone(arch: nil)
  obj = super()
  obj.instance_variable_set(:@arch, arch)
  obj
end

#defer(fn_name, &_block) ⇒ T.untyped

defer the calculation of ‘blk’ until later, then memoize the result

Parameters:

  • fn_name (Symbol)
  • _block (T.proc.void)

Returns:

  • (T.untyped)


130
131
132
133
134
135
# File 'lib/udb/obj/database_obj.rb', line 130

def defer(fn_name, &_block)
  cache_value = @cache[fn_name]
  return cache_value unless cache_value.nil?

  @cache[fn_name] ||= yield
end

#defined_by_conditionExtensionRequirementExpression

Returns Extension(s) that define the instruction.

Returns:



139
140
141
142
143
144
145
146
# File 'lib/udb/obj/database_obj.rb', line 139

def defined_by_condition
  @defined_by_condition ||=
    begin
      raise "ERROR: definedBy is nul for #{name}" if @data["definedBy"].nil?

      ExtensionRequirementExpression.new(@data["definedBy"], @cfg_arch)
    end
end

#description(normative: true, non_normative: true, when_cb: proc { |when_ast, text| ["When `#{when_ast.gen_adoc(0)}`", text] }) ⇒ String

Returns Description of the object, from YAML.

Parameters:

  • normative (Boolean) (defaults to: true)

    Include normative text?

  • non_normative (Boolean) (defaults to: true)

    Include non-normative text?

  • when_cb (T.proc.params(when_ast: Idl::AstNode, text: String).returns(T::Array[String])) (defaults to: proc { |when_ast, text| ["When `#{when_ast.gen_adoc(0)}`", text] })

    Callback to generate text for the un-knowable ast

Returns:

  • (String)

    Description of the object, from YAML



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/udb/obj/database_obj.rb', line 160

def description(
  normative: true,      # display normative text?
  non_normative: true,  # display non-normative text?
  when_cb: proc { |when_ast, text|
    ["When `#{when_ast.gen_adoc(0)}`", text]
  }
)
  case @data['description']
  when String
    @data['description']
  when Array
    stmts = @data['description']
    desc_lines = []
    stmts.each_with_index do |stmt, idx|
      if stmt.key?("when()")
        # conditional
        ast = @cfg_arch.idl_compiler.compile_func_body(
          stmt["when()"],
          return_type: Idl::Type.new(:boolean),
          symtab: @cfg_arch.symtab,
          name: "#{name}.description[#{idx}].when",
          input_file: __source,
          input_line: source_line(["description", idx, "when()"])
        )

        symtab = @cfg_arch.symtab.global_clone
        symtab.push(ast)
        unless ast.return_type(symtab).kind == :boolean
          ast.type_error "`when` must be a Boolean in description"
        end

        value_result = ast.value_try do
          if ast.return_value(symtab) == true
            # condition holds, add the test
            if (stmt["normative"] == true) && normative
              desc_lines << stmt["text"]
            elsif (stmt["normative"] == false) && non_normative
              desc_lines << stmt["text"]
            end
          end
          # else, value is false; don't add it
        end
        ast.value_else(value_result) do
          # value of 'when' isn't known. prune out what we do know
          # and display it
          pruned_ast = ast.prune(symtab)
          pruned_ast.freeze_tree(symtab)
          desc_lines.concat(when_cb.call(pruned_ast, stmt["text"]))
        end
        symtab.pop
        symtab.release
      else
        if (stmt["normative"] == true) && normative
          desc_lines << stmt["text"]
        elsif (stmt["normative"] == false) && non_normative
          desc_lines << stmt["text"]
        end
      end
    end
    desc_lines.join("\n\n")
  end
end

#inspectObject



124
125
126
# File 'lib/udb/obj/database_obj.rb', line 124

def inspect
  "#{self.class.name}##{name}"
end

#kindString

Returns:

  • (String)


52
# File 'lib/udb/obj/database_obj.rb', line 52

def kind = @kind.serialize

#source_line(path) ⇒ Integer

Returns THe source line number of path in the YAML file.

Examples:

00: yaml = <<~YAML
01:   misa:
02:     sw_read(): ...
03:     fields:
04:       A:
05:         type(): ...
06: YAML
misa_csr.source_line("sw_read()")  #=> 2
mis_csr.source_line("fields", "A", "type()") #=> 5

Parameters:

  • path (Array<String>)

    Path to the scalar you want.

Returns:

  • (Integer)

    THe source line number of path in the YAML file



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/udb/obj/database_obj.rb', line 236

def source_line(path)

  # find the line number of this operation() in the *original* file
  yaml_filename = __source
  raise "No $source for #{name}" if yaml_filename.nil?
  line = T.let(nil, T.untyped)
  path_idx = 0
  Psych.parse_stream(File.read(yaml_filename), filename: yaml_filename) do |doc|
    mapping = doc.children[0]
    data = T.let(
      if mapping.children.size == 2
        mapping.children[1]
      else
        mapping
      end,
      Psych::Nodes::Node)
    found = T.let(false, T::Boolean)
    while path_idx < path.size
      if data.is_a?(Psych::Nodes::Mapping)
        idx = 0
        while idx < data.children.size
          if data.children[idx].value == path[path_idx]
            if path_idx == path.size - 1
              line = data.children[idx + 1].start_line
              if data.children[idx + 1].style == Psych::Nodes::Scalar::LITERAL
                line += 1 # the string actually begins on the next line
              end
              return line
            else
              found = true
              data = data.children[idx + 1]
              path_idx += 1
              break
            end
          end
          idx += 2
        end
        raise "path #{path[path_idx]} @ #{path_idx} not found for #{self.class.name}##{name}" unless found
      elsif data.is_a?(Psych::Nodes::Sequence)
        raise "Expecting Integer" unless path[path_idx].is_a?(Integer)

        if data.children.size > path[path_idx]
          if path_idx == path.size - 1
            line = data.children[path[path_idx]].start_line
            return line
          else
            data = data.children[path[path_idx]]
            path_idx += 1
          end
        else
          raise "Index out of bounds"
        end
      end
    end
  end
  raise "Didn't find path '#{path}' in #{__source}"
end