Top Level Namespace

Defined Under Namespace

Modules: RLTK

Instance Method Summary collapse

Instance Method Details

#check_cg_array_type(array, type = RLTK::CG::Type, blame = 'el_types', strict = false) ⇒ Array<Type>

This helper function checks to make sure that an array of objects are all sub-classses of Type or instances of a sub-class of Type. If a class is present in the array parameter it is expected to be a singleton class and will be instantiated via the instance method.

Parameters:

  • array (Array<Type, Class>) —

    Array of objects to type check for code generation type.

  • type (Type) (defaults to: RLTK::CG::Type) —

    Class the objects should be an instance (or sub-class) of.

  • blame (String) (defaults to: 'el_types') —

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false) —

    Strict or non-strict checking. Uses instance_of? and is_a? respectively.

Returns:

  • (Array<Type>) —

    An array containing the objects in array with any singleton classes replaced by their instances.

Raises:

  • (ArgumentError) —

    An error is raise if a class is passed in array that hasn't included the Singleton class, if the class passed in parameter type isn't a sub-class of Type, or if the type check fails.



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/rltk/cg/type.rb', line 529

def check_cg_array_type(array, type = RLTK::CG::Type, blame = 'el_types', strict = false)
  array.map do |o|
    if o.is_a?(Class)
      type_ok = if strict then o == type else o.subclass_of?(type) end

      if type_ok
        if o.includes_module?(Singleton)
          o.instance
        else
          raise ArgumentError, "The #{o.name} class (passed in parameter #{blame}) must be instantiated directly."
        end
      else
        raise ArgumentError, "The #{o.name} class (passed in parameter #{blame}) does not inherit from the #{type.name} class."
      end

    else
      type_ok = if strict then o.instance_of(type) else o.is_a?(type) end

      if type_ok
        o
      else
        raise ArgumentError, "Parameter #{blame} must contain instances of the #{type.name} class."
      end
    end
  end
end

#check_cg_type(o, type = RLTK::CG::Type, blame = 'type', strict = false) ⇒ Type

This helper function checks to make sure that an object is a sub-class of Type or an instance of a sub-class of Type. If a class is passed in the o parameter it is expected to be a singleton class and will be instantiated via the instance method.

Parameters:

  • o (Type, Class) —

    Object to type check for code generation type.

  • type (Type) (defaults to: RLTK::CG::Type) —

    Class the object should be an instance (or sub-class) of.

  • blame (String) (defaults to: 'type') —

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false) —

    Strict or non-strict checking. Uses instance_of? and is_a? respectively.

Returns:

  • (Type) —

    The object o or an instance of the class passed in parameter o.

Raises:

  • (ArgumentError) —

    An error is raise if a class is passed in parameter o that hasn't included the Singleton class, if the class passed in parameter type isn't a sub-class of Type, or if the type check fails.



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/rltk/cg/type.rb', line 495

def check_cg_type(o, type = RLTK::CG::Type, blame = 'type', strict = false)
  if o.is_a?(Class)
    type_ok = if strict then o == type else o.subclass_of?(type) end

    if type_ok
      if o.includes_module?(Singleton)
        o.instance
      else
        raise ArgumentError, "The #{o.name} class (passed as parameter #{blame}) must be instantiated directly."
      end
    else
      raise ArgumentError, "The #{o.name} class (passed as parameter #{blame} does not inherit from the #{type.name} class."
    end
  else
    check_type(o, type, blame, strict)
  end
end

#make_ptr_to_elements(size_or_values, &block) ⇒ FFI::MemoryPointer

A helper function for creating constant array, vector, and struct types. This method should never be used by library users.

Parameters:

  • size_or_values (Array<RLTK::CG::Value>, Integer) —

    Number of values or array of values.

  • block (Proc) —

    Block evaluated if size is specified.

Returns:

  • (FFI::MemoryPointer) —

    An array of pointers to LLVM Values.



1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
# File 'lib/rltk/cg/value.rb', line 1260

def make_ptr_to_elements(size_or_values, &block)
  values =
  case size_or_values
  when Integer
    raise ArgumentError, 'Block not given.' if not block_given?

    ::Array.new(size_or_values, &block)
  else
    size_or_values
  end

  FFI::MemoryPointer.new(:pointer, values.size).write_array_of_pointer(values)
end