wrapture 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,156 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
3
+ # frozen_string_literal: true
4
+
5
+ #--
6
+ # Copyright 2020 Joel E. Anderson
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #++
20
+
21
+ module Wrapture
22
+ # A description of a type used in a specification.
23
+ class TypeSpec
24
+ # Returns a normalized copy of the hash specification of a type in +spec+.
25
+ # See normalize_spec_hash! for details.
26
+ def self.normalize_spec_hash(spec)
27
+ normalize_spec_hash!(Marshal.load(Marshal.dump(spec)))
28
+ end
29
+
30
+ # Normalizes the hash specification of a type in +spec+ in place. This will
31
+ # normalize the include list.
32
+ def self.normalize_spec_hash!(spec)
33
+ spec['includes'] = Wrapture.normalize_includes(spec['includes'])
34
+ spec
35
+ end
36
+
37
+ # Creates a parameter specification based on the provided hash +spec+.
38
+ # +spec+ can be a string instead of a hash, in which case it will be used
39
+ # as the name of the type.
40
+ #
41
+ # Type specs must have a 'name' key with either the type itself (for example
42
+ # 'const char *') or a keyword specifying some other type (for example
43
+ # 'equivalent-struct'). The only exception is for function pointers, which
44
+ # instead use a 'function' key that contains a FunctionSpec specification.
45
+ # This specification does not need to be definable, it only needs to have
46
+ # a parameter list and return type for the signature to be clear.
47
+ def initialize(spec = 'void')
48
+ actual_spec = if spec.is_a?(String)
49
+ { 'name' => spec }
50
+ else
51
+ spec
52
+ end
53
+
54
+ @spec = TypeSpec.normalize_spec_hash(actual_spec)
55
+ end
56
+
57
+ # The name of this type with all special characters removed.
58
+ def base
59
+ name.delete('*&').strip
60
+ end
61
+
62
+ # True if this type is an equivalent struct pointer reference.
63
+ def equivalent_pointer?
64
+ name == EQUIVALENT_POINTER_KEYWORD
65
+ end
66
+
67
+ # True if this type is an equivalent struct reference.
68
+ def equivalent_struct?
69
+ name == EQUIVALENT_STRUCT_KEYWORD
70
+ end
71
+
72
+ # True if this type is a function.
73
+ def function?
74
+ @spec.key?('function')
75
+ end
76
+
77
+ # A list of includes needed for this type.
78
+ def includes
79
+ includes = @spec['includes'].dup
80
+
81
+ if function?
82
+ func = FunctionSpec.new(@spec['function'])
83
+ includes.concat(func.declaration_includes)
84
+ end
85
+
86
+ includes.uniq
87
+ end
88
+
89
+ # The name of the type.
90
+ def name
91
+ @spec['name']
92
+ end
93
+
94
+ # True if this type is a pointer.
95
+ def pointer?
96
+ name.end_with?('*')
97
+ end
98
+
99
+ # Creates a new TypeSpec within the scope of +owner+ that will be directly
100
+ # usable. This will replace equivalent structs, pointers, and self
101
+ # references with a usable type name.
102
+ def resolve(owner)
103
+ owner.resolve_type(self)
104
+ end
105
+
106
+ # A string with a declaration of FunctionSpec +func+ with this type as the
107
+ # return value. +func_name+ can be provided to override the function name,
108
+ # for example if a class name needs to be included.
109
+ def return_expression(func, func_name: func.name)
110
+ name_part = String.new(func_name || '')
111
+ param_part = String.new
112
+ ret_part = name
113
+
114
+ current_spec = @spec
115
+ while current_spec.is_a?(Hash) && current_spec.key?('function')
116
+ name_part.prepend('( *')
117
+
118
+ current_func = FunctionSpec.new(current_spec['function'])
119
+ param_part.concat(" )( #{current_func.param_list} )")
120
+
121
+ current_spec = current_spec.dig('function', 'return', 'type')
122
+ ret_part = current_spec
123
+ end
124
+
125
+ ret_part << ' ' unless ret_part.end_with?('*')
126
+
127
+ "#{ret_part}#{name_part}( #{func.param_list} )#{param_part}"
128
+ end
129
+
130
+ # True if this type is a reference to a class instance.
131
+ def self?
132
+ name == SELF_REFERENCE_KEYWORD
133
+ end
134
+
135
+ # A string with a declaration of a variable named +var_name+ of this type.
136
+ # If +var_name+ is nil then this will simply be a type declaration.
137
+ def variable(var_name = nil)
138
+ if variadic?
139
+ '...'
140
+ elsif function?
141
+ func = FunctionSpec.new(@spec['function'])
142
+ func_name = "( *#{var_name} )" || '(*)'
143
+ func.return_expression(func_name: func_name)
144
+ elsif var_name.nil?
145
+ name
146
+ else
147
+ "#{name}#{' ' unless name.end_with?('*')}#{var_name}"
148
+ end
149
+ end
150
+
151
+ # True if this type is a variadic parameter type (name is equal to +...+).
152
+ def variadic?
153
+ name == '...'
154
+ end
155
+ end
156
+ end
@@ -1,8 +1,26 @@
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
1
3
  # frozen_string_literal: true
2
4
 
5
+ #--
6
+ # Copyright 2019-2020 Joel E. Anderson
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #++
20
+
3
21
  module Wrapture
4
22
  # the current version of Wrapture
5
- VERSION = '0.3.0'
23
+ VERSION = '0.4.0'
6
24
 
7
25
  # Returns true if the version of the spec is supported by this version of
8
26
  # Wrapture. Otherwise returns false.
@@ -2,6 +2,7 @@
2
2
 
3
3
  # frozen_string_literal: true
4
4
 
5
+ #--
5
6
  # Copyright 2019-2020 Joel E. Anderson
6
7
  #
7
8
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +16,7 @@
15
16
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
17
  # See the License for the specific language governing permissions and
17
18
  # limitations under the License.
19
+ #++
18
20
 
19
21
  module Wrapture
20
22
  # A description of a function to be wrapped by another language.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrapture
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-01 00:00:00.000000000 Z
11
+ date: 2020-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -41,14 +41,19 @@ files:
41
41
  - lib/wrapture.rb
42
42
  - lib/wrapture/action_spec.rb
43
43
  - lib/wrapture/class_spec.rb
44
+ - lib/wrapture/comment.rb
44
45
  - lib/wrapture/constant_spec.rb
45
46
  - lib/wrapture/constants.rb
47
+ - lib/wrapture/enum_spec.rb
46
48
  - lib/wrapture/errors.rb
47
49
  - lib/wrapture/function_spec.rb
48
50
  - lib/wrapture/normalize.rb
51
+ - lib/wrapture/param_spec.rb
49
52
  - lib/wrapture/rule_spec.rb
50
53
  - lib/wrapture/scope.rb
51
54
  - lib/wrapture/struct_spec.rb
55
+ - lib/wrapture/template_spec.rb
56
+ - lib/wrapture/type_spec.rb
52
57
  - lib/wrapture/version.rb
53
58
  - lib/wrapture/wrapped_function_spec.rb
54
59
  homepage: http://rubygems.org/gems/wrapture
@@ -57,6 +62,7 @@ licenses:
57
62
  metadata:
58
63
  bug_tracker_uri: https://github.com/goatshriek/wrapture/issues
59
64
  changelog_uri: https://github.com/goatshriek/wrapture/blob/master/ChangeLog.md
65
+ documentation_uri: https://goatshriek.github.io/wrapture/rdoc/
60
66
  source_code_uri: https://github.com/goatshriek/wrapture/
61
67
  post_install_message:
62
68
  rdoc_options: []
@@ -73,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
79
  - !ruby/object:Gem::Version
74
80
  version: '0'
75
81
  requirements: []
76
- rubygems_version: 3.0.3
82
+ rubygems_version: 3.1.2
77
83
  signing_key:
78
84
  specification_version: 4
79
85
  summary: wrap C in C++