Class: RLTK::CG::FunctionType

Inherits:
Type show all
Defined in:
lib/rltk/cg/type.rb

Overview

A type representing the return an argument types for a function.

Instance Attribute Summary

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods inherited from Type

#allignment, #context, from_ptr, #hash, #kind, #size

Methods included from AbstractClass

included

Methods included from BindingClass

#==

Constructor Details

#initialize(overloaded, arg_types = nil, varargs = false) ⇒ FunctionType

Create a new function type from a pointer or description of the return type and argument types.

Parameters:

  • overloaded (FFI::Pointer, Type)

    Pointer to existing function type or the return type.

  • arg_types (Array<Type>) (defaults to: nil)

    Types of the function’s arguments.

  • varargs (Boolean) (defaults to: false)

    Weather or not this function has varargs.



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/rltk/cg/type.rb', line 318

def initialize(overloaded, arg_types = nil, varargs = false)
	@ptr =
	case overloaded
	when FFI::Pointer
		overloaded
	else
		@return_type	= check_cg_type(overloaded, Type, 'return_type')
		@arg_types	= check_cg_array_type(arg_types, Type, 'arg_types').freeze
		
		arg_types_ptr = FFI::MemoryPointer.new(:pointer, @arg_types.length)
		arg_types_ptr.write_array_of_pointer(@arg_types)
		
		Bindings.function_type(@return_type, arg_types_ptr, @arg_types.length, varargs.to_i)
	end
end

Instance Method Details

#argument_typesArray<Type> Also known as: arg_types

Returns Types of this function type’s arguments.

Returns:

  • (Array<Type>)

    Types of this function type’s arguments.



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/rltk/cg/type.rb', line 335

def argument_types
	@arg_types ||=
	begin
		num_elements = Bindings.count_param_types(@ptr)
		
		ret_ptr = FFI::MemoryPointer.new(:pointer)
		Bindings.get_param_types(@ptr, ret_ptr)
		
		types_ptr = ret_ptr.get_pointer(0)
		
		types_ptr.get_array_of_pointer(0, num_elements).map { |ptr| Type.from_ptr(ptr) }
	end
end

#return_typeType

Returns The return type of this function type.

Returns:

  • (Type)

    The return type of this function type.



351
352
353
# File 'lib/rltk/cg/type.rb', line 351

def return_type
	@return_type ||= Type.from_ptr(Bindings.get_return_type(@ptr))
end