Class: Udb::ExtensionTerm

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/udb/logic.rb

Overview

a terminal for an Extension with a version specifier (a-la an ExtensionRequirement) we don’t use ExtensionRequirement for terminals just to keep LogicNode independent of the rest of UDB

Defined Under Namespace

Classes: ComparisonOp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, op, ver) ⇒ ExtensionTerm

Returns a new instance of ExtensionTerm.



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/udb/logic.rb', line 149

def initialize(name, op, ver)
  @name = name
  @op = T.let(
    if op.is_a?(String)
      ComparisonOp.deserialize(op)
    else
      op
    end,

    ComparisonOp)
  @version = ver.is_a?(String) ? VersionSpec.new(ver) : ver
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



140
141
142
# File 'lib/udb/logic.rb', line 140

def name
  @name
end

#versionObject (readonly)

Returns the value of attribute version.



143
144
145
# File 'lib/udb/logic.rb', line 143

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/udb/logic.rb', line 262

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

  other_ext = other
  if @op == ComparisonOp::Equal && other_ext.comparison == ComparisonOp::Equal
    if @name == other_ext.name
      T.must(@version <=> other_ext.version)
    else
      T.must(@name <=> other_ext.name)
    end
  else
    if @name == other_ext.name
      if min_possible_version == other_ext.min_possible_version
        max_possible_version <=> other_ext.max_possible_version
      else
        min_possible_version <=> other_ext.min_possible_version
      end
    else
      T.must(@name <=> other_ext.name)
    end
  end
end

#comparisonObject



146
# File 'lib/udb/logic.rb', line 146

def comparison = @op

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


295
296
297
298
299
300
# File 'lib/udb/logic.rb', line 295

def eql?(other)
  return false unless other.is_a?(ExtensionTerm)
  return false unless hash == other.hash

  to_s == other.to_s
end

#hashObject



287
# File 'lib/udb/logic.rb', line 287

def hash = @hash ||= to_s.hash

#matches_any_version?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/udb/logic.rb', line 163

def matches_any_version?
  @op == ComparisonOp::Equal && @version == VersionSpec.new("0")
end

#max_possible_versionObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/udb/logic.rb', line 239

def max_possible_version
  case @op
  when ComparisonOp::Equal, ComparisonOp::LessThanOrEqual, ComparisonOp::Compatible
    @version
  when ComparisonOp::LessThan
    if @version.zero?
      nil
    else
      @version.decrement_patch
    end
  when ComparisonOp::GreaterThanOrEqual, ComparisonOp::GreaterThan
    VersionSpec.new("0")
  else
    T.absurd(@op)
  end
end

#min_possible_versionObject

return the minimum version possible that would satisfy this term



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/udb/logic.rb', line 224

def min_possible_version
  case @op
  when ComparisonOp::Equal, ComparisonOp::GreaterThanOrEqual, ComparisonOp::Compatible
    @version
  when ComparisonOp::GreaterThan
    @version.increment_patch
  when ComparisonOp::LessThanOrEqual, ComparisonOp::LessThan
    VersionSpec.new("0")
  else
    T.absurd(@op)
  end
end

#requirement_specObject



213
214
215
# File 'lib/udb/logic.rb', line 213

def requirement_spec
  @requirement_spec ||= RequirementSpec.new("#{@op.serialize} #{@version}")
end

#to_condition(cfg_arch) ⇒ Object



181
182
183
# File 'lib/udb/logic.rb', line 181

def to_condition(cfg_arch)
  Condition.new({ "extension" => { "name" => name, "version" => "#{@op.serialize} #{@version}" } }, cfg_arch)
end

#to_ext_req(cfg_arch) ⇒ Object



168
169
170
# File 'lib/udb/logic.rb', line 168

def to_ext_req(cfg_arch)
  cfg_arch.extension_requirement(@name, "#{@op.serialize} #{@version}")
end

#to_ext_ver(cfg_arch) ⇒ Object



174
175
176
177
178
# File 'lib/udb/logic.rb', line 174

def to_ext_ver(cfg_arch)
  raise "Not an extension version" unless @op == ComparisonOp::Equal

  cfg_arch.extension_version(@name, @version.to_s)
end

#to_hObject



196
197
198
199
200
201
# File 'lib/udb/logic.rb', line 196

def to_h
  {
    "name" => @name,
    "version" => "#{@op.serialize} #{@version}"
  }
end

#to_idl(cfg_arch) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/udb/logic.rb', line 204

def to_idl(cfg_arch)
  if @op == ComparisonOp::GreaterThanOrEqual && @version.eql?("0")
    "implemented?(ExtensionName::#{@name})"
  else
    "implemented_version?(ExtensionName::#{@name}, \"#{@op.serialize} #{@version}\")"
  end
end

#to_sObject



186
187
188
# File 'lib/udb/logic.rb', line 186

def to_s
  @to_s ||= "#{@name}#{@op.serialize}#{@version}"
end

#to_s_prettyObject



191
192
193
# File 'lib/udb/logic.rb', line 191

def to_s_pretty
  "Extension #{@name}, version #{@version}"
end

#to_z3(solver, cfg_arch) ⇒ Object



218
219
220
221
# File 'lib/udb/logic.rb', line 218

def to_z3(solver, cfg_arch)
  ext = solver.ext_req(name, requirement_spec, cfg_arch)
  ext.term
end