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) ⇒ DatabaseObject

Returns a new instance of DatabaseObject.



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

#archObject (readonly)

Use when Architecture class is sufficient



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

def arch
  @arch
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#data_pathObject (readonly)

Returns the value of attribute data_path.



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

def data_path
  @data_path
end

#long_nameObject (readonly)

Returns the value of attribute long_name.



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

def long_name
  @long_name
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



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

#__sourceObject



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

def __source
  @data["$source"]
end

#cfg_archObject



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) ⇒ Object



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) ⇒ Object



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

def defer(fn_name, &_block)
  return @cache[fn_name] if @cache.key?(fn_name)

  @cache[fn_name] = yield
end

#defined_by_conditionObject



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

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] }) ⇒ Object



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

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

#kindObject



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

def kind = @kind.serialize

#source_line(path) ⇒ Object



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

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