class Type
Represents a type in the C/C++ type system. This class wraps libclang's type representation and provides methods to query type properties.
Definitions
attr_reader :type, :translation_unit
Signature
-
attribute
r type
-
returns
FFI::Struct The underlying CXType structure.
-
returns
-
attribute
r translation_unit
-
returns
TranslationUnit The translation unit this type belongs to.
-
returns
def self.create(cxtype, translation_unit)
Create a type instance of the appropriate subclass based on the type kind.
Signature
-
parameter
cxtypeFFI::Struct The low-level CXType structure.
-
parameter
translation_unitTranslationUnit The translation unit this type belongs to.
-
returns
Type A Type instance of the appropriate subclass.
Implementation
def self.create(cxtype, translation_unit)
case cxtype[:kind]
when :type_pointer, :type_block_pointer, :type_obj_c_object_pointer, :type_member_pointer
Pointer.new(cxtype, translation_unit)
when :type_constant_array, :type_incomplete_array, :type_variable_array, :type_dependent_sized_array
Array.new(cxtype, translation_unit)
when :type_vector
Vector.new(cxtype, translation_unit)
when :type_function_no_proto, :type_function_proto
Function.new(cxtype, translation_unit)
when :type_elaborated
Elaborated.new(cxtype, translation_unit)
when :type_typedef
TypeDef.new(cxtype, translation_unit)
when :type_record
Record.new(cxtype, translation_unit)
else
Type.new(cxtype, translation_unit)
end
end
def initialize(type, translation_unit)
Create a new type instance.
Signature
-
parameter
typeFFI::Struct The low-level CXType structure.
-
parameter
translation_unitTranslationUnit The translation unit this type belongs to.
Implementation
def initialize(type, translation_unit)
@type = type
@translation_unit = translation_unit
end
def kind
Get the kind of this type.
Signature
-
returns
Symbol The type kind (e.g., :type_int, :type_pointer).
Implementation
def kind
@type[:kind]
end
def kind_spelling
Get the spelling of this type's kind.
Signature
-
returns
String A human-readable string describing the type kind.
Implementation
def kind_spelling
Lib.extract_string Lib.get_type_kind_spelling @type[:kind]
end
def spelling
Get the spelling of this type.
Signature
-
returns
String The type as it would appear in source code.
Implementation
def spelling
Lib.extract_string Lib.get_type_spelling(@type)
end
def canonical
Get the canonical type.
Signature
-
returns
Type The canonical (unqualified, unaliased) form of this type.
Implementation
def canonical
Type.create Lib.get_canonical_type(@type), @translation_unit
end
def pod?
Check if this is a Plain Old Data (POD) type.
Signature
-
returns
Boolean True if this is a POD type.
Implementation
def pod?
Lib.is_pod_type(@type) != 0
end
def const_qualified?
Check if this type is const-qualified.
Signature
-
returns
Boolean True if the type has a const qualifier.
Implementation
def const_qualified?
Lib.is_const_qualified_type(@type) != 0
end
def volatile_qualified?
Check if this type is volatile-qualified.
Signature
-
returns
Boolean True if the type has a volatile qualifier.
Implementation
def volatile_qualified?
Lib.is_volatile_qualified_type(@type) != 0
end
def restrict_qualified?
Check if this type is restrict-qualified.
Signature
-
returns
Boolean True if the type has a restrict qualifier.
Implementation
def restrict_qualified?
Lib.is_restrict_qualified_type(@type) != 0
end
def alignof
Get the alignment of this type in bytes.
Signature
-
returns
Integer The alignment requirement in bytes.
Implementation
def alignof
Lib.type_get_align_of(@type)
end
def sizeof
Get the size of this type in bytes.
Signature
-
returns
Integer The size in bytes, or -1 if the size cannot be determined.
Implementation
def sizeof
Lib.type_get_size_of(@type)
end
def ref_qualifier
Get the ref-qualifier for this type (C++ only).
Signature
-
returns
Symbol The ref-qualifier (:ref_qualifier_none, :ref_qualifier_lvalue, :ref_qualifier_rvalue).
Implementation
def ref_qualifier
Lib.type_get_cxx_ref_qualifier(@type)
end
def declaration
Get the cursor for the declaration of this type.
Signature
-
returns
Cursor The cursor representing the type declaration.
Implementation
def declaration
Cursor.new Lib.get_type_declaration(@type), @translation_unit
end
def unqualified_type
Get the type with all qualifiers (const, volatile, restrict) removed.
Signature
-
returns
Type The unqualified type.
Implementation
def unqualified_type
Type.create Lib.get_unqualified_type(@type), @translation_unit
end
def non_reference_type
Get the non-reference type. For reference types, returns the type that is being referenced.
Signature
-
returns
Type The non-reference type.
Implementation
def non_reference_type
Type.create Lib.get_non_reference_type(@type), @translation_unit
end
def template_argument_type(index)
Get the type of a template argument at the given index.
For template specializations (e.g., std::vector<int>), this returns the type of
the template argument at the specified position.
Signature
-
parameter
indexInteger The zero-based index of the template argument.
-
returns
Type The type of the template argument at the given index.
Implementation
def template_argument_type(index)
Type.create Lib.get_template_argument_as_type(@type, index), @translation_unit
end
def num_template_arguments
Get the number of template arguments for this type.
For template specializations (e.g., std::map<int, std::string>), this returns the
number of template arguments. Returns -1 if this is not a template specialization.
Signature
-
returns
Integer The number of template arguments, or -1 if not a template type.
Implementation
def num_template_arguments
Lib.get_num_template_arguments(@type)
end
def address_space
Get the address space of this type.
Signature
-
returns
Integer The address space number.
Implementation
def address_space
Lib.get_address_space(@type)
end
def typedef_name
Get the typedef name of this type.
Signature
-
returns
String The typedef name.
Implementation
def typedef_name
Lib.extract_string Lib.get_typedef_name(@type)
end
def transparent_tag_typedef?
Check if this typedef is transparent.
Signature
-
returns
Boolean True if this is a transparent tag typedef.
Implementation
def transparent_tag_typedef?
Lib.type_is_transparent_tag_typedef(@type) != 0
end
def nullability
Get the nullability kind of a pointer type.
Signature
-
returns
Integer The nullability kind.
Implementation
def nullability
Lib.type_get_nullability(@type)
end
def modified_type
Get the type modified by an attributed type.
Signature
-
returns
Type The modified type.
Implementation
def modified_type
Type.create Lib.type_get_modified_type(@type), @translation_unit
end
def value_type
Get the value type of an atomic type.
Signature
-
returns
Type The value type.
Implementation
def value_type
Type.create Lib.type_get_value_type(@type), @translation_unit
end
def pretty_printed(policy)
Pretty-print this type using a printing policy.
Signature
-
parameter
policyPrintingPolicy The printing policy to use.
-
returns
String The pretty-printed type string.
Implementation
def pretty_printed(policy)
Lib.extract_string Lib.get_type_pretty_printed(@type, policy)
end
def fully_qualified_name(policy, with_global_ns_prefix: false)
Get the fully qualified name of this type.
Signature
-
parameter
policyPrintingPolicy The printing policy to use.
-
parameter
with_global_ns_prefixBoolean Whether to prepend "::".
-
returns
String The fully qualified type name.
Implementation
def fully_qualified_name(policy, with_global_ns_prefix: false)
Lib.extract_string Lib.get_fully_qualified_name(@type, policy, with_global_ns_prefix ? 1 : 0)
end
def visit_base_classes(&block)
Visit all base classes of a C++ record type.
Signature
-
yields
{|cursor| ...} Each base class cursor.
-
parameter
cursorCursor The base class cursor.
-
parameter
-
returns
Enumerator If no block is given.
-
returns
self The receiver.
Implementation
def visit_base_classes(&block)
return to_enum(__method__) unless block_given?
visit_type(:visit_cxx_base_classes, &block)
end
def visit_methods(&block)
Visit all methods of a C++ record type.
Signature
-
yields
{|cursor| ...} Each method cursor.
-
parameter
cursorCursor The method cursor.
-
parameter
-
returns
Enumerator If no block is given.
-
returns
self The receiver.
Implementation
def visit_methods(&block)
return to_enum(__method__) unless block_given?
visit_type(:visit_cxx_methods, &block)
end
def visit_fields(&block)
Visit all fields of a record type.
Signature
-
yields
{|cursor| ...} Each field cursor.
-
parameter
cursorCursor The field cursor.
-
parameter
-
returns
Enumerator If no block is given.
-
returns
self The receiver.
Implementation
def visit_fields(&block)
return to_enum(__method__) unless block_given?
visit_type(:type_visit_fields, &block)
end
def visit_type(function_name, &block)
Visit a type using a libclang visitor function. The C API documents a non-zero return on early termination, but in practice (libclang 21.1.7) it returns 1 in both cases, so ffi-clang treats these as side-effect iterators and returns self.
Signature
-
parameter
function_nameSymbol The Lib function to invoke.
-
yields
{|cursor| ...} Each visited cursor.
-
returns
self The receiver.
Implementation
def visit_type(function_name, &block)
callback = Proc.new do |cursor, _data|
result = block.call(Cursor.new(cursor, @translation_unit))
result == :break ? 0 : 1
end
Lib.send(function_name, @type, callback, nil)
self
end
def ==(other)
Compare this type with another for equality.
Signature
-
parameter
otherType The other type to compare.
-
returns
Boolean True if the types are equal.
Implementation
def ==(other)
Lib.equal_types(@type, other.type) != 0
end
def to_s
Get a string representation of this type.
Signature
-
returns
String A string describing this type.
Implementation
def to_s
"#{self.class.name} <#{self.kind}: #{self.spelling}>"
end