Module: MilkTea::Bindgen::Generator::GeneratorAstParser

Included in:
MilkTea::Bindgen::Generator
Defined in:
lib/milk_tea/bindings/bindgen/ast_parser.rb

Instance Method Summary collapse

Instance Method Details

#active_macro_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 277

def active_macro_name?(name)
  @active_macro_names.include?(name)
end

#allowed_declaration_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 126

def allowed_declaration_name?(name)
  return false unless name
  return false if @excluded_declaration_names.include?(name)
  return true if @declaration_name_prefixes.empty?

  @declaration_name_prefixes.any? { |prefix| name.start_with?(prefix) }
end

#build_alias_maps(nodes) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 159

def build_alias_maps(nodes)
  nodes.each do |node|
    next unless node["kind"] == "TypedefDecl"

    target = typedef_target(node)
    next unless target

    case target[:kind]
    when "RecordDecl"
      @record_aliases[target[:id]] = node["name"]
      if target[:name] && !target[:name].empty?
        @record_aliases_by_tag_name[target[:name]] ||= node["name"]
      end
    when "EnumDecl"
      @enum_aliases[target[:id]] = node["name"]
    end
  end
end

#constant_name_for(node) ⇒ Object



355
356
357
358
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 355

def constant_name_for(node)
  name = node["name"].to_s
  macro_probe_declaration?(node) ? name.delete_prefix(MACRO_CONST_PREFIX) : name
end

#contains_disallowed_macro_call?(source) ⇒ Boolean

Returns:

  • (Boolean)


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 304

def contains_disallowed_macro_call?(source)
  index = 0

  while index < source.length
    match = source.match(/\b[A-Za-z_][A-Za-z0-9_]*\s*\(/, index)
    return false unless match

    callee = match[0][/\A[A-Za-z_][A-Za-z0-9_]*/]
    open_index = match[0].rindex("(") + match.begin(0)
    close_index = matching_paren_index(source, open_index)
    return true unless close_index

    return true unless callee&.match?(/\A[A-Z][A-Z0-9_]*\z/)

    index = close_index + 1
  end

  false
end

#dump_astObject



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
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 7

def dump_ast
  Tempfile.create(["milk-tea-bindgen", ".c"]) do |translation_unit|
    @translation_unit_path = translation_unit.path
    write_translation_unit_prelude(translation_unit)
    translation_unit.flush
    @active_macro_names = preprocessed_macro_names(translation_unit.path)
    macro_probe_declarations.each do |declaration|
      translation_unit.write(declaration)
      translation_unit.write("\n")
    end
    translation_unit.flush
    translation_unit.close

    command = [
      @clang,
      "-x",
      "c",
      "-fno-builtin",
      *@clang_args,
      "-Xclang",
      "-ast-dump=json",
      "-fsyntax-only",
      translation_unit.path,
    ]
    stdout, stderr, status = Open3.capture3(*command)
    begin
      return JSON.parse(stdout)
    rescue JSON::ParserError
      details = [stdout, stderr].reject(&:empty?).join
      raise BindgenError, details.empty? ? "clang bindgen failed" : "clang bindgen failed:\n#{details}" unless status.success?

      raise
    end
  end
rescue Errno::ENOENT
  raise BindgenError, "clang not found: #{@clang}"
rescue JSON::ParserError => e
  raise BindgenError, "failed to parse clang AST JSON: #{e.message}"
end

#each_macro_definitionObject



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
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 225

def each_macro_definition
  logical_line = +""
  current_file = nil

  preprocessed_macro_source.each_line do |line|
    current = line.delete_suffix("\n")
    if logical_line.empty?
      logical_line = current
    else
      logical_line << current.lstrip
    end

    if logical_line.end_with?("\\")
      logical_line = logical_line.delete_suffix("\\") + " "
      next
    end

    line_marker = logical_line.match(/\A#\s+\d+\s+"([^"]+)"/)
    if line_marker
      current_file = normalize_preprocessor_path(line_marker[1])
      logical_line = +""
      next
    end

    match = logical_line.match(/\A\s*#define\s+([A-Za-z_][A-Za-z0-9_]*)(.*)\z/)
    if match && current_file && tracked_header_path?(current_file)
      name = match[1]
      suffix = match[2]
      yield(name, suffix.strip) unless suffix.start_with?("(")
    end

    logical_line = +""
  end
end

#extract_top_level_header_nodes(ast) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 63

def extract_top_level_header_nodes(ast)
  seen_ids = {}

  Array(ast["inner"]).flat_map do |node|
    next [] unless node.is_a?(Hash)

    nodes = []
    nodes << node if interesting_top_level_kind?(node["kind"]) && node_from_header?(node)
    nodes.concat(typedef_owned_tag_nodes(node)) if node["kind"] == "TypedefDecl"
    nodes
  end.filter do |node|
    node_id = node["id"] || node.object_id
    next false if seen_ids.key?(node_id)

    seen_ids[node_id] = true
  end
end

#index_referenceable_record_declarations(ast) ⇒ Object



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
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 178

def index_referenceable_record_declarations(ast)
  indexed = {}
  indexed_by_id = {}
  queue = Array(ast["inner"]).dup

  until queue.empty?
    node = queue.shift
    next unless node.is_a?(Hash)

    if node["kind"] == "RecordDecl" && %w[struct union].include?(node["tagUsed"])
      if node["name"] && !node["name"].empty?
        existing = indexed[node["name"]]
        if existing.nil? || (!record_complete_definition?(existing) && record_complete_definition?(node))
          indexed[node["name"]] = node
        end
      end
      indexed_by_id[node["id"]] = node if node["id"]
    end

    queue.concat(Array(node["inner"]))
  end

  @referenceable_record_declarations = indexed
  @referenceable_record_declarations_by_id = indexed_by_id
end

#interesting_top_level_kind?(kind) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 101

def interesting_top_level_kind?(kind)
  %w[TypedefDecl RecordDecl EnumDecl VarDecl FunctionDecl].include?(kind)
end

#macro_constant_candidate?(name, body) ⇒ Boolean

Returns:

  • (Boolean)


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 260

def macro_constant_candidate?(name, body)
  return false unless name.match?(/\A[A-Z][A-Z0-9_]*\z/)
  return false unless allowed_declaration_name?(name)
  return false unless active_macro_name?(name)

  normalized = normalize_macro_body(body)
  return false if normalized.empty?
  return false if normalized.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)
  return false if normalized.include?('"') || normalized.include?("'")
  return false if contains_disallowed_macro_call?(normalized)

  first_token = normalized[/\A[A-Za-z_][A-Za-z0-9_]*/]
  return false if first_token && NON_VALUE_MACRO_TOKENS.include?(first_token)

  normalized.match?(/\A[A-Za-z0-9_()+\-*\/%<>&|~^.,{}\[\]:?\s]+\z/)
end

#macro_constant_candidatesObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 210

def macro_constant_candidates
  return @macro_constant_candidates if defined?(@macro_constant_candidates)

  definitions = {}
  each_macro_definition do |name, body|
    definitions[name] = body
  end

  @macro_constant_candidates = definitions.filter_map do |name, body|
    next unless macro_constant_candidate?(name, body)

    name
  end
end

#macro_probe_declaration?(node) ⇒ Boolean

Returns:

  • (Boolean)


351
352
353
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 351

def macro_probe_declaration?(node)
  node["name"].to_s.start_with?(MACRO_CONST_PREFIX)
end

#macro_probe_declarationsObject



204
205
206
207
208
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 204

def macro_probe_declarations
  macro_constant_candidates.map do |name|
    %(static const __typeof__(#{name}) #{macro_probe_name(name)} = #{name};)
  end
end

#macro_probe_name(name) ⇒ Object



347
348
349
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 347

def macro_probe_name(name)
  "#{MACRO_CONST_PREFIX}#{name}"
end

#matching_paren_index(source, open_index) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 324

def matching_paren_index(source, open_index)
  depth = 0
  index = open_index

  while index < source.length
    case source[index]
    when "("
      depth += 1
    when ")"
      depth -= 1
      return index if depth.zero?
    end

    index += 1
  end

  nil
end

#node_from_header?(node) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 105

def node_from_header?(node)
  source_files = node_source_files(node).map { |path| File.expand_path(path) }
  return true if source_files.include?(@translation_unit_path)
  return true if source_files.any? { |path| tracked_header_path?(path) }

  return false unless source_files.empty?

  include_files = node_include_files(node).map { |path| File.expand_path(path) }
  return true if include_files.include?(@translation_unit_path)

  include_files.any? { |path| tracked_header_path?(path) }
end

#node_include_files(node) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 148

def node_include_files(node)
  [
    node.dig("loc", "includedFrom", "file"),
    node.dig("loc", "spellingLoc", "includedFrom", "file"),
    node.dig("range", "begin", "includedFrom", "file"),
    node.dig("range", "end", "includedFrom", "file"),
    node.dig("range", "begin", "spellingLoc", "includedFrom", "file"),
    node.dig("range", "end", "spellingLoc", "includedFrom", "file"),
  ].compact.uniq
end

#node_source_files(node) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 134

def node_source_files(node)
  [
    node.dig("loc", "file"),
    node.dig("loc", "spellingLoc", "file"),
    node.dig("loc", "expansionLoc", "file"),
    node.dig("range", "begin", "file"),
    node.dig("range", "end", "file"),
    node.dig("range", "begin", "spellingLoc", "file"),
    node.dig("range", "begin", "expansionLoc", "file"),
    node.dig("range", "end", "spellingLoc", "file"),
    node.dig("range", "end", "expansionLoc", "file"),
  ].compact.uniq
end

#normalize_macro_body(body) ⇒ Object



343
344
345
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 343

def normalize_macro_body(body)
  body.gsub(%r{/\*.*?\*/}, " ").sub(%r{//.*\z}, "").strip
end

#normalize_preprocessor_path(path) ⇒ Object



388
389
390
391
392
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 388

def normalize_preprocessor_path(path)
  return nil if path.start_with?("<") && path.end_with?(">")

  File.expand_path(path)
end

#preprocessed_macro_names(translation_unit_path) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 281

def preprocessed_macro_names(translation_unit_path)
  command = [
    @clang,
    "-x",
    "c",
    "-fno-builtin",
    *@clang_args,
    "-dM",
    "-E",
    translation_unit_path,
  ]
  stdout, stderr, status = Open3.capture3(*command)
  unless status.success?
    details = [stdout, stderr].reject(&:empty?).join
    raise BindgenError, details.empty? ? "clang bindgen macro probe failed" : "clang bindgen macro probe failed:\n#{details}"
  end

  stdout.each_line.filter_map do |line|
    match = line.match(/\A#define\s+([A-Za-z_][A-Za-z0-9_]*)\b/)
    match[1] if match
  end.to_set
end

#preprocessed_macro_sourceObject



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 368

def preprocessed_macro_source
  command = [
    @clang,
    "-x",
    "c",
    "-fno-builtin",
    *@clang_args,
    "-E",
    "-dD",
    @translation_unit_path,
  ]
  stdout, stderr, status = Open3.capture3(*command)
  unless status.success?
    details = [stdout, stderr].reject(&:empty?).join
    raise BindgenError, details.empty? ? "clang bindgen macro dump failed" : "clang bindgen macro dump failed:\n#{details}"
  end

  stdout
end

#tracked_header_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
124
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 118

def tracked_header_path?(path)
  return true if @tracked_header_paths.include?(path)

  @tracked_header_prefixes.any? do |prefix|
    path == prefix || path.start_with?(prefix + File::SEPARATOR)
  end
end

#typedef_owned_tag_nodes(node) ⇒ Object



81
82
83
84
85
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 81

def typedef_owned_tag_nodes(node)
  Array(node["inner"]).flat_map do |child|
    typedef_owned_tag_nodes_from(child)
  end
end

#typedef_owned_tag_nodes_from(node) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 87

def typedef_owned_tag_nodes_from(node)
  return [] unless node.is_a?(Hash)

  nodes = []
  if %w[RecordDecl EnumDecl].include?(node["kind"]) && node_from_header?(node)
    nodes << node
  end

  Array(node["inner"]).each do |child|
    nodes.concat(typedef_owned_tag_nodes_from(child))
  end
  nodes
end

#typedef_target(node) ⇒ Object



360
361
362
363
364
365
366
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 360

def typedef_target(node)
  inner = Array(node["inner"]).find { |child| %w[RecordType EnumType].include?(child["kind"]) }
  decl = inner&.dig("decl")
  return unless decl

  { id: decl["id"], kind: decl["kind"], name: decl["name"] }
end

#write_translation_unit_prelude(translation_unit) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/milk_tea/bindings/bindgen/ast_parser.rb', line 47

def write_translation_unit_prelude(translation_unit)
  @bindgen_defines.each do |define|
    name, value = define.split("=", 2)
    if value.nil?
      translation_unit.write("#define #{name}\n")
    else
      translation_unit.write("#define #{name} #{value}\n")
    end
  end

  translation_unit.write(%(#include #{@header_path.dump}\n))
  @bindgen_include_directives.each do |directive|
    translation_unit.write(%(#include #{directive.dump}\n))
  end
end