cast-to-yaml 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/cast-to-yaml.gemspec +13 -0
- data/lib/cast-to-yaml.rb +2 -0
- data/lib/cast-to-yaml/to_yaml.rb +268 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2e8524dd3e090d0b7183a64d0211cb576e323335ef7f74c796bf9b782d6878d0
|
4
|
+
data.tar.gz: 6d477343c3f14724045a5630f755a6f00b67a0b30c2fcd5056ecda1b3fa6db26
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c285dfe327f870bf4d5d9da514f9211784dbc3d613fb6e3fe5fbc3443bfcf02426925df7fceb670ea2e40adc7447fba126e8a72daad88d2e1b524467628cc758
|
7
|
+
data.tar.gz: e9fb38a6ea74eb661e681f3d5282898cbba31b244db2b8530d1069ff09e657ed69b29f6fe910ceee38e474b9067c48518b99d1c35378f0296d5d6a69bb2accfc
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 alcf-perfengr
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'cast-to-yaml'
|
3
|
+
s.version = "0.1.0"
|
4
|
+
s.author = "Brice Videau"
|
5
|
+
s.email = "bvideau@anl.gov"
|
6
|
+
s.homepage = "https://github.com/alcf-perfengr/cast-to-yaml"
|
7
|
+
s.summary = "Extract information fom a c ast"
|
8
|
+
s.files = Dir[ 'cast-to-yaml.gemspec', 'LICENSE', 'lib/**/*.rb' ]
|
9
|
+
s.has_rdoc = false
|
10
|
+
s.license = 'MIT'
|
11
|
+
s.required_ruby_version = '>= 2.3.0'
|
12
|
+
s.add_dependency 'cast', '~> 0.3', '>=0.3.0'
|
13
|
+
end
|
data/lib/cast-to-yaml.rb
ADDED
@@ -0,0 +1,268 @@
|
|
1
|
+
module C
|
2
|
+
|
3
|
+
class Declarator
|
4
|
+
def to_h(declaration)
|
5
|
+
res = {}
|
6
|
+
res["name"] = name.dup
|
7
|
+
res["type"] = if indirect_type
|
8
|
+
indirect_type.to_h(declaration)
|
9
|
+
else
|
10
|
+
declaration.type.to_h
|
11
|
+
end
|
12
|
+
if init
|
13
|
+
res["init"] = init.to_s
|
14
|
+
end
|
15
|
+
if num_bits
|
16
|
+
res["num_bits"] = num_bits.to_s
|
17
|
+
end
|
18
|
+
res
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Declaration
|
23
|
+
|
24
|
+
def to_a
|
25
|
+
declarators.collect { |d|
|
26
|
+
d.to_h(self)
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def extract(res = Hash::new { |h, k| h[k] = [] })
|
31
|
+
if typedef?
|
32
|
+
declarators.each { |d|
|
33
|
+
res["typedefs"].push d.to_h(self)
|
34
|
+
}
|
35
|
+
else
|
36
|
+
declarators.each { |d|
|
37
|
+
if d.indirect_type && d.indirect_type.kind_of?(Function)
|
38
|
+
f = {}
|
39
|
+
f["name"] = d.name
|
40
|
+
if d.indirect_type.type
|
41
|
+
f["type"] = d.indirect_type.type.to_h(self)
|
42
|
+
else
|
43
|
+
f["type"] = type.to_h(self)
|
44
|
+
end
|
45
|
+
if d.indirect_type.params
|
46
|
+
f["params"] = d.indirect_type.params.collect { |p| p.to_h }
|
47
|
+
end
|
48
|
+
|
49
|
+
res["functions"].push f
|
50
|
+
else
|
51
|
+
res["declarations"].push d.to_h(self)
|
52
|
+
end
|
53
|
+
}
|
54
|
+
end
|
55
|
+
if type.kind_of?(Struct) && type.members && type.name
|
56
|
+
s = {}
|
57
|
+
s["name"] = type.name
|
58
|
+
m = []
|
59
|
+
type.members.each { |mem|
|
60
|
+
m += mem.to_a
|
61
|
+
}
|
62
|
+
s["members"] = m
|
63
|
+
res["structs"].push s
|
64
|
+
elsif type.kind_of?(Enum) && type.members && type.name
|
65
|
+
s = {}
|
66
|
+
s["name"] = type.name
|
67
|
+
m = []
|
68
|
+
type.members.each { |mem|
|
69
|
+
m.push mem.to_h
|
70
|
+
}
|
71
|
+
s["members"] = m
|
72
|
+
res["enums"].push s
|
73
|
+
elsif type.kind_of?(Union) && type.members && type.name
|
74
|
+
s = {}
|
75
|
+
s["name"] = type.name
|
76
|
+
m = []
|
77
|
+
type.members.each { |mem|
|
78
|
+
m += mem.to_a
|
79
|
+
}
|
80
|
+
s["members"] = m
|
81
|
+
res["unions"].push s
|
82
|
+
end
|
83
|
+
res
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class TranslationUnit
|
88
|
+
def extract_declarations(res = Hash::new { |h, k| h[k] = [] })
|
89
|
+
entities.select { |e|
|
90
|
+
e.kind_of? Declaration
|
91
|
+
}.each { |e|
|
92
|
+
e.extract(res)
|
93
|
+
}
|
94
|
+
res
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
int_longnesses = ['short ', '', 'long ', 'long long ']
|
99
|
+
float_longnesses = ['float', 'double', 'long double']
|
100
|
+
## DirectTypes
|
101
|
+
class Struct
|
102
|
+
def to_h
|
103
|
+
res = {}
|
104
|
+
res["kind"] = "struct"
|
105
|
+
if name
|
106
|
+
res["name"] = name
|
107
|
+
else
|
108
|
+
m = []
|
109
|
+
members.each { |mem|
|
110
|
+
m += mem.to_a
|
111
|
+
}
|
112
|
+
res["members"] = m
|
113
|
+
end
|
114
|
+
res["const"] = true if const?
|
115
|
+
res["restrict"] = true if restrict?
|
116
|
+
res["volatile"] = true if volatile?
|
117
|
+
res
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class Union
|
122
|
+
def to_h
|
123
|
+
res = {}
|
124
|
+
res["kind"] = "union"
|
125
|
+
if name
|
126
|
+
res["name"] = name
|
127
|
+
else
|
128
|
+
m = []
|
129
|
+
members.each { |mem|
|
130
|
+
m += mem.to_a
|
131
|
+
}
|
132
|
+
res["members"] = m
|
133
|
+
end
|
134
|
+
res["const"] = true if const?
|
135
|
+
res["restrict"] = true if restrict?
|
136
|
+
res["volatile"] = true if volatile?
|
137
|
+
res
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class Enum
|
142
|
+
def to_h
|
143
|
+
res = {}
|
144
|
+
res["kind"] = "enum"
|
145
|
+
if name
|
146
|
+
res["name"] = name
|
147
|
+
else
|
148
|
+
m = []
|
149
|
+
members.each { |mem|
|
150
|
+
m.push mem.to_h
|
151
|
+
}
|
152
|
+
res["members"] = m
|
153
|
+
end
|
154
|
+
res["const"] = true if const?
|
155
|
+
res["restrict"] = true if restrict?
|
156
|
+
res["volatile"] = true if volatile?
|
157
|
+
res
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
class Enumerator
|
162
|
+
def to_h
|
163
|
+
res = {}
|
164
|
+
res["name"] = name
|
165
|
+
if val
|
166
|
+
res["val"] = val.to_s
|
167
|
+
end
|
168
|
+
res
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
[
|
173
|
+
[CustomType, proc{name.dup }],
|
174
|
+
[Void , proc{'void' }],
|
175
|
+
[Int , proc do
|
176
|
+
longness_str = int_longnesses[longness+1].dup
|
177
|
+
"#{unsigned? ? 'unsigned ' : ''}#{longness_str}int"
|
178
|
+
end],
|
179
|
+
[Float , proc{float_longnesses[longness].dup}],
|
180
|
+
[Char , proc{"#{unsigned? ? 'unsigned ' : signed? ? 'signed ' : ''}char"}],
|
181
|
+
[Bool , proc{"_Bool" }],
|
182
|
+
[Complex , proc{"_Complex #{float_longnesses[longness]}"}],
|
183
|
+
[Imaginary , proc{"_Imaginary #{float_longnesses[longness]}"}]
|
184
|
+
].each do |c, x|
|
185
|
+
c.send(:define_method, :to_h) do |_ = nil|
|
186
|
+
res = {}
|
187
|
+
if self.kind_of? CustomType
|
188
|
+
res["kind"] = "custom_type"
|
189
|
+
else
|
190
|
+
res["kind"] = "#{self.class.name.split('::').last}".downcase
|
191
|
+
end
|
192
|
+
res["name"] = instance_eval(&x)
|
193
|
+
res["const"] = true if const?
|
194
|
+
res["restrict"] = true if restrict?
|
195
|
+
res["volatile"] = true if volatile?
|
196
|
+
res
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
## IndirectTypes
|
201
|
+
class Pointer
|
202
|
+
def to_h(declaration = nil)
|
203
|
+
res = {}
|
204
|
+
res["kind"] = "pointer"
|
205
|
+
res["const"] = true if const?
|
206
|
+
res["restrict"] = true if restrict?
|
207
|
+
res["volatile"] = true if volatile?
|
208
|
+
if type
|
209
|
+
if declaration
|
210
|
+
res["type"] = type.to_h(declaration)
|
211
|
+
else
|
212
|
+
res["type"] = type.to_h
|
213
|
+
end
|
214
|
+
else
|
215
|
+
res["type"] = declaration.type.to_h
|
216
|
+
end
|
217
|
+
res
|
218
|
+
end
|
219
|
+
end
|
220
|
+
class Array
|
221
|
+
def to_h(declaration = nil)
|
222
|
+
res = {}
|
223
|
+
res["kind"] = "array"
|
224
|
+
if type
|
225
|
+
if declaration
|
226
|
+
res["type"] = type.to_h(declaration)
|
227
|
+
else
|
228
|
+
res["type"] = type.to_h
|
229
|
+
end
|
230
|
+
else
|
231
|
+
res["type"] = declaration.type.to_h
|
232
|
+
end
|
233
|
+
if length
|
234
|
+
res["length"] = length.to_s
|
235
|
+
end
|
236
|
+
res
|
237
|
+
end
|
238
|
+
end
|
239
|
+
class Function
|
240
|
+
def to_h(declaration, no_types=false)
|
241
|
+
res = {}
|
242
|
+
res["kind"] = "function"
|
243
|
+
if type
|
244
|
+
res["type"] = type.to_h(declaration)
|
245
|
+
else
|
246
|
+
res["type"] = declaration.type.to_h
|
247
|
+
end
|
248
|
+
if !params.nil?
|
249
|
+
res["params"] = if no_types
|
250
|
+
params.collect{|p| p.name }
|
251
|
+
else
|
252
|
+
params.collect{|p| p.to_h }
|
253
|
+
end
|
254
|
+
end
|
255
|
+
res
|
256
|
+
end
|
257
|
+
end
|
258
|
+
class Parameter
|
259
|
+
def to_h
|
260
|
+
res = {}
|
261
|
+
res["name"] = name.to_s if name.to_s != ''
|
262
|
+
res["type"] = type.to_h
|
263
|
+
res["register"] = true if register?
|
264
|
+
res
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cast-to-yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brice Videau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: cast
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.3'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.3.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.3'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.3.0
|
33
|
+
description:
|
34
|
+
email: bvideau@anl.gov
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- LICENSE
|
40
|
+
- cast-to-yaml.gemspec
|
41
|
+
- lib/cast-to-yaml.rb
|
42
|
+
- lib/cast-to-yaml/to_yaml.rb
|
43
|
+
homepage: https://github.com/alcf-perfengr/cast-to-yaml
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.3.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.7.6.2
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Extract information fom a c ast
|
67
|
+
test_files: []
|