Skip to content

Commit

Permalink
factor c++ code out of function spec
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Nov 4, 2024
1 parent 71a6da7 commit 6a684fb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
28 changes: 25 additions & 3 deletions lib/wrapture/cpp_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,12 @@ def factory_constructor_hash
'return' => { 'type' => "#{@spec.name} *" } }
end

# True if the return value of the function's wrapped call is saved.
def function_captures_return?(func_spec)
!func_spec.constructor? &&
(func_spec.wrapped.use_return? || function_returns_return_val?(func_spec))
end

# The parameter list for the function declaration.
def function_declaration_param_list(func_spec)
if func_spec.params.empty?
Expand Down Expand Up @@ -713,12 +719,28 @@ def function_definition_signature(func_spec)
def function_locals(spec)
yield 'va_list variadic_args;' if spec.variadic?

if spec.capture_return?
if function_captures_return?(spec)
wrapped_type = spec.resolve_type(spec.wrapped.return_val_type)
yield "#{type_variable(wrapped_type, 'return_val')};"
end
end

# True if the function returns the result of the wrapped function call
# directly without any after actions.
def function_returns_call_directly?(func_spec)
!func_spec.constructor? &&
!func_spec.destructor? &&
!%w[void self-reference].include?(func_spec.return_type.name) &&
!func_spec.wrapped.error_check?
end

# True if the function returns the return_val variable.
def function_returns_return_val?(func_spec)
!func_spec.return_type.self_reference? &&
!func_spec.void_return? &&
!function_returns_call_directly?(func_spec)
end

# The suffix to add to a function definition for initializers, if any exist.
def initializer_suffix
return '' if @spec.initializers.empty?
Expand Down Expand Up @@ -823,7 +845,7 @@ def return_expression(type_spec, func_spec, func_name: func_spec.name)
def return_statement
if @spec.return_type.self_reference?
'return *this;'
elsif @spec.return_type.name != 'void' && !@spec.returns_call_directly?
elsif @spec.return_type.name != 'void' && !function_returns_call_directly?(@spec)
'return return_val;'
else
''
Expand Down Expand Up @@ -882,7 +904,7 @@ def wrapped_call_expression
"this->equivalent = #{call}"
elsif @spec.wrapped.error_check?
"return_val = #{call}"
elsif @spec.returns_call_directly?
elsif function_returns_call_directly?(@spec)
"return #{return_cast(call)}"
else
call
Expand Down
34 changes: 0 additions & 34 deletions lib/wrapture/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,6 @@ def initialize(spec, owner = Scope.new, constructor: false,
# A WrappedFunctionSpec or WrappedCodeSpec this .
attr_reader :wrapped

# True if the return value of the wrapped call is saved.
def capture_return?
# TODO this should be factored into the C++ wrapper, as it's only relevant
# for languages where the C type is directly compatible
!@constructor && (@wrapped.use_return? || returns_return_val?)
end

# True if the function is a constructor, false otherwise.
def constructor?
@constructor
Expand Down Expand Up @@ -256,11 +249,6 @@ def optional_params
@params.select(&:default_value?)
end

# A string with the parameter list for this function.
# def param_list
# ParamSpec.signature(@params, self)
# end

# An array of the names of the function params.
def param_names
@params.map(&:name)
Expand Down Expand Up @@ -311,17 +299,6 @@ def return_overloaded?
@spec['return']['overloaded']
end

# True if the function returns the result of the wrapped function call
# directly without any after actions.
def returns_call_directly?
# TODO this should be factored into the C++ wrapper, as it's only relevant
# for languages where the C type is directly compatible
!@constructor &&
!@destructor &&
!%w[void self-reference].include?(@spec['return']['type']) &&
!@wrapped.error_check?
end

# True if the function is static.
def static?
@spec['static']
Expand All @@ -341,16 +318,5 @@ def virtual?
def void_return?
@return_type.name == 'void'
end

private

# True if the function returns the return_val variable.
def returns_return_val?
# TODO this should be factored into the C++ wrapper, as it's only relevant
# for languages where the C type is directly compatible
!@return_type.self_reference? &&
@spec['return']['type'] != 'void' &&
!returns_call_directly?
end
end
end
3 changes: 3 additions & 0 deletions sig/cpp_wrapper.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ module Wrapture
def equivalent_member_field: -> String
def error_check: (Wrapture::WrappedFunctionSpec, ?return_val: String) { (String) -> void } -> void
def factory_constructor_hash: -> String
def function_captures_return?: (Wrapture::FunctionSpec) -> bool
def function_declaration_param_list: (Wrapture::FunctionSpec) -> String
def function_declaration_signature: (Wrapture::FunctionSpec) { (String) -> void } -> void
def function_definition_param_list: (Wrapture::FunctionSpec) -> String
def function_locals: (Wrapture::FunctionSpec spec) { (String) -> void } -> void
def function_returns_call_directly?: (Wrapture::FunctionSpec) -> bool
def function_returns_return_val?: (Wrapture::FunctionSpec) -> bool
def initializer_suffix: -> String
def member_constructor_hash: -> spec_hash
def pointer_constructor_hash: -> spec_hash
Expand Down
2 changes: 0 additions & 2 deletions sig/function_spec.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Wrapture
attr_reader wrapped: Wrapture::WrappedFunctionSpec | Wrapture::WrappedCodeSpec | nil

def initialize: (spec_hash spec, ?(Wrapture::ClassSpec | Wrapture::Scope) owner, ?constructor: bool, ?destructor: bool) -> void
def capture_return?: -> bool
def constructor?: -> bool
def declaration_includes: -> Array[String]
def definable?: -> bool
Expand All @@ -40,7 +39,6 @@ module Wrapture
def static?: -> bool
def resolve_type: (Wrapture::TypeSpec type_spec) -> Wrapture::TypeSpec
def return_overloaded?: -> bool
def returns_call_directly?: -> bool
def variadic?: -> bool
def virtual?: -> bool
def void_return?: -> bool
Expand Down

0 comments on commit 6a684fb

Please sign in to comment.