Class: Udb::DatabaseObject

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

Overview

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

Direct Known Subclasses

CsrField, TopLevelDatabaseObject

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)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/udb/obj/database_obj.rb', line 88

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:



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

def arch
  @arch
end

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

Returns:

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


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

def data
  @data
end

#data_pathPathname (readonly)

Returns:

  • (Pathname)


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

def data_path
  @data_path
end

#long_nameString (readonly)

Returns:

  • (String)


54
55
56
# File 'lib/udb/obj/database_obj.rb', line 54

def long_name
  @long_name
end

#nameString (readonly)

Returns:

  • (String)


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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Integer?

Parameters:

  • other (T.untyped)

Returns:

  • (Integer, nil)


116
117
118
119
120
121
# File 'lib/udb/obj/database_obj.rb', line 116

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

  @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



126
127
128
# File 'lib/udb/obj/database_obj.rb', line 126

def __source
  @data["$source"]
end

#cfg_archnil, ConfiguredArchitecture

Returns:



68
69
70
71
72
# File 'lib/udb/obj/database_obj.rb', line 68

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

  @cfg_arch
end

#cfg_arch?Boolean

Returns:

  • (Boolean)


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

def cfg_arch? = !@cfg_arch.nil?

#clone(arch: nil) ⇒ DatabaseObject

clone this, and set the arch at the same time

Parameters:

Returns:



109
110
111
112
113
# File 'lib/udb/obj/database_obj.rb', line 109

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)


136
137
138
139
140
141
# File 'lib/udb/obj/database_obj.rb', line 136

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

  @cache[fn_name] ||= yield
end

#defined_by_conditionAbstractCondition

Returns Condition for when the object exists.

Returns:



145
146
147
148
149
150
151
152
153
154
# File 'lib/udb/obj/database_obj.rb', line 145

def defined_by_condition
  @defined_by_condition ||=
    begin
      if @data.key?("definedBy")
        Condition.new(@data["definedBy"], @cfg_arch)
      else
        AlwaysTrueCondition.new(@cfg_arch)
      end
    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



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
222
223
224
225
226
227
228
229
# File 'lib/udb/obj/database_obj.rb', line 168

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



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

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

#kindString

Returns:

  • (String)


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

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, Integer>)

    Path to the scalar you want.

Returns:

  • (Integer)

    THe source line number of path in the YAML file



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
293
294
295
296
297
298
299
300
# File 'lib/udb/obj/database_obj.rb', line 244

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