/usr/lib/ruby/vendor_ruby/gir_ffi/builders/field_builder.rb is in ruby-gir-ffi 0.9.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 295 296 297 298 299 300 301 | require 'gir_ffi/builders/argument_builder'
require 'gir_ffi/variable_name_generator'
require 'gir_ffi/field_argument_info'
module GirFFI
module Builders
# Creates field getter and setter code for a given IFieldInfo.
class FieldBuilder
# Convertor for fields for field getters. Used when building getter
# methods.
class GetterArgumentBuilder < BaseArgumentBuilder
def initialize(var_gen, field_argument_info, field_info,
array_length_idx: -1)
super(var_gen, field_argument_info)
@field_info = field_info
@length_arg = NullArgumentBuilder.new
@array_length_idx = array_length_idx
end
def pre_conversion
[
"#{field_ptr} = @struct.to_ptr + #{field_offset}",
"#{typed_ptr} = GirFFI::InOutPointer.new(#{field_type_tag}, #{field_ptr})",
"#{bare_value} = #{typed_ptr}.to_value"
]
end
def capture_variable_name
nil
end
def post_converted_name
@post_converted_name ||= if has_post_conversion?
new_variable
else
bare_value
end
end
def return_value_name
post_converted_name
end
def post_conversion
if has_post_conversion?
["#{post_converted_name} = #{post_convertor.conversion}"]
else
[]
end
end
def array_length_idx
@array_length_idx
end
private
def field_offset
@field_info.offset
end
def field_ptr
@field_ptr ||= @var_gen.new_var
end
def typed_ptr
@typed_ptr ||= @var_gen.new_var
end
def bare_value
@bare_value ||= @var_gen.new_var
end
def field_type_tag
@field_type_tag ||= @field_info.field_type.tag_or_class.inspect
end
def field_type
@field_type ||= @field_info.field_type
end
def has_post_conversion?
type_info.needs_c_to_ruby_conversion_for_functions?
end
def post_convertor
@post_convertor ||= CToRubyConvertor.new(type_info,
bare_value,
length_arg.post_converted_name)
end
end
# Class to represent argument info for the argument of a getter method.
# Implements the necessary parts of IArgumentInfo's interface.
class GetterArgumentInfo
attr_reader :name, :argument_type
def initialize(name, type)
@name = name
@argument_type = type
end
def closure
-1
end
def destroy
-1
end
def direction
:out
end
def ownership_transfer
:everything
end
def caller_allocates?
false
end
def skip?
false
end
end
# Builder for field getters
class GetterBuilder
def initialize(info)
@info = info
end
def method_definition
template.method_definition
end
def singleton_method?
false
end
def method_name
@info.name
end
def method_arguments
[]
end
def preparation
[]
end
def invocation
nil
end
def result
[getter_argument_builder.return_value_name]
end
private
def var_gen
@var_gen ||= VariableNameGenerator.new
end
def template
@template ||= MethodTemplate.new(self, argument_builders)
end
def argument_builders
@argument_builders ||=
ArgumentBuilderCollection.new(
NullReturnValueBuilder.new,
base_argument_builders)
end
def base_argument_builders
if array_length_field
[getter_argument_builder, length_argument_builder]
else
[getter_argument_builder]
end
end
def getter_argument_builder
@getter_argument_builder ||=
GetterArgumentBuilder.new(var_gen, field_argument_info, @info,
array_length_idx: array_length_field ? 1 : -1)
end
def length_argument_builder
@length_argument_builder ||=
GetterArgumentBuilder.new(var_gen, length_argument_info, array_length_field)
end
def array_length_field
@info.related_array_length_field
end
def length_argument_info
@length_argument_info ||=
GetterArgumentInfo.new 'length', array_length_field.field_type
end
def field_offset
@info.offset
end
def field_type_tag
@field_type_tag ||= @info.field_type.tag_or_class.inspect
end
def field_type
@field_type ||= @info.field_type
end
def field_argument_info
@field_argument_info ||= GetterArgumentInfo.new 'value', field_type
end
end
attr_reader :info
def initialize(field_info)
@info = field_info
end
def build
setup_getter
setup_setter
end
def setup_getter
container_class.class_eval getter_def unless container_defines_getter_method?
end
def container_defines_getter_method?
container_info.find_instance_method info.name
end
def setup_setter
container_class.class_eval setter_def if info.writable?
end
def getter_def
getter_builder = GetterBuilder.new(info)
getter_builder.method_definition
end
# TODO: Use MethodTemplate
def setter_def
builder = setter_builder
field_ptr = builder.new_variable
typed_ptr = builder.new_variable
<<-CODE.reset_indentation
def #{info.name}= #{builder.method_argument_name}
#{field_ptr} = @struct.to_ptr + #{info.offset}
#{typed_ptr} = GirFFI::InOutPointer.new(#{field_type_tag}, #{field_ptr})
#{builder.pre_conversion.join("\n ")}
#{typed_ptr}.set_value #{builder.call_argument_name}
end
CODE
end
private
def field_type_tag
@field_type_tag ||= info.field_type.tag_or_class.inspect
end
def container_class
@container_class ||= container_module.const_get(container_info.safe_name)
end
def container_module
@container_module ||= Object.const_get(container_info.safe_namespace)
end
def container_info
@container_info ||= info.container
end
def field_type
@field_type ||= @info.field_type
end
def field_argument_info
@field_argument_info ||= FieldArgumentInfo.new 'value', field_type
end
def setter_builder
@setter_builder ||= ArgumentBuilder.new(VariableNameGenerator.new,
field_argument_info)
end
end
end
end
|