Class: Udb::DatabaseObject
- Inherits:
-
Object
- Object
- Udb::DatabaseObject
- 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
Direct Known Subclasses
CsrField, InstructionSubtype, InstructionType, TopLevelDatabaseObject
Defined Under Namespace
Classes: Kind
Instance Attribute Summary collapse
- #arch ⇒ ConfiguredArchitecture? readonly
- #data ⇒ Hash{String => T.untyped} readonly
- #data_path ⇒ Pathname readonly
- #long_name ⇒ String readonly
- #name ⇒ String readonly
Instance Method Summary collapse
- #<=>(other) ⇒ Integer?
- #__source ⇒ nil, String
- #cfg_arch ⇒ nil, ConfiguredArchitecture
- #cfg_arch? ⇒ Boolean
-
#clone(arch: nil) ⇒ DatabaseObject
clone this, and set the arch at the same time.
-
#defer(fn_name, &_block) ⇒ T.untyped
defer the calculation of ‘blk’ until later, then memoize the result.
-
#defined_by_condition ⇒ ExtensionRequirementExpression
Extension(s) that define the instruction.
-
#description(normative: true, non_normative: true, when_cb: proc { |when_ast, text| ["When `#{when_ast.gen_adoc(0)}`", text] }) ⇒ String
Description of the object, from YAML.
- #initialize(data, data_path, arch, kind, name: nil) constructor
- #inspect ⇒ Object
- #kind ⇒ String
-
#source_line(path) ⇒ Integer
THe source line number of
path
in the YAML file.
Constructor Details
#initialize(data, data_path, arch, kind, name: nil)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/udb/obj/database_obj.rb', line 85 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
#arch ⇒ ConfiguredArchitecture? (readonly)
60 61 62 |
# File 'lib/udb/obj/database_obj.rb', line 60 def arch @arch end |
#data ⇒ Hash{String => T.untyped} (readonly)
42 43 44 |
# File 'lib/udb/obj/database_obj.rb', line 42 def data @data end |
#data_path ⇒ Pathname (readonly)
45 46 47 |
# File 'lib/udb/obj/database_obj.rb', line 45 def data_path @data_path end |
#long_name ⇒ String (readonly)
51 52 53 |
# File 'lib/udb/obj/database_obj.rb', line 51 def long_name @long_name end |
#name ⇒ String (readonly)
48 49 50 |
# File 'lib/udb/obj/database_obj.rb', line 48 def name @name end |
Instance Method Details
#<=>(other) ⇒ Integer?
113 114 115 116 117 |
# File 'lib/udb/obj/database_obj.rb', line 113 def <=>(other) return nil unless other.is_a?(DatabaseObject) name <=> other.name end |
#__source ⇒ nil, String
122 123 124 |
# File 'lib/udb/obj/database_obj.rb', line 122 def __source @data["$source"] end |
#cfg_arch ⇒ nil, ConfiguredArchitecture
65 66 67 68 69 |
# File 'lib/udb/obj/database_obj.rb', line 65 def cfg_arch raise "no cfg_arch" if @cfg_arch.nil? @cfg_arch end |
#cfg_arch? ⇒ Boolean
72 |
# File 'lib/udb/obj/database_obj.rb', line 72 def cfg_arch? = !@cfg_arch.nil? |
#clone(arch: nil) ⇒ DatabaseObject
clone this, and set the arch at the same time
106 107 108 109 110 |
# File 'lib/udb/obj/database_obj.rb', line 106 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
132 133 134 135 136 137 |
# File 'lib/udb/obj/database_obj.rb', line 132 def defer(fn_name, &_block) cache_value = @cache[fn_name] return cache_value unless cache_value.nil? @cache[fn_name] ||= yield end |
#defined_by_condition ⇒ ExtensionRequirementExpression
Returns Extension(s) that define the instruction.
141 142 143 144 145 146 147 148 |
# File 'lib/udb/obj/database_obj.rb', line 141 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.
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 222 223 |
# File 'lib/udb/obj/database_obj.rb', line 162 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 |
#inspect ⇒ Object
126 127 128 |
# File 'lib/udb/obj/database_obj.rb', line 126 def inspect "#{self.class.name}##{name}" end |
#kind ⇒ String
54 |
# File 'lib/udb/obj/database_obj.rb', line 54 def kind = @kind.serialize |
#source_line(path) ⇒ Integer
Returns THe source line number of path
in the YAML file.
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 293 294 |
# File 'lib/udb/obj/database_obj.rb', line 238 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 |