1#!/usr/bin/env ruby
2# Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15require 'ostruct'
16require 'set'
17require 'delegate'
18
19module Signatures
20  DEFINES = Hash.new()
21  SIGNATURES = Hash.new()
22  BUILTINS = Hash.new()
23  PRIMITIVES = Hash.new()
24  TYPEDESCRIPTORS = Hash.new()
25  DYNAMIC = Hash.new()
26
27  def wrap_data(data)
28    refs = Hash.new()
29
30    unless data
31      return
32    end
33
34    data.defines.each do |define|
35      refs[define.ref] = define.name
36      Signatures::DEFINES[define.ref] = define
37    end
38
39    data.packages.each do |package|
40        refs[package.ref] = package.name
41    end
42
43    data.primitives.each do |primitive|
44        refs[primitive.ref] = primitive.name
45        Signatures::PRIMITIVES[primitive.ref] = primitive.name
46    end
47
48    data.typedescriptors.each do |typedescriptor|
49      refs[typedescriptor.ref] = typedescriptor.name
50      Signatures::TYPEDESCRIPTORS[typedescriptor.ref] = typedescriptor.name
51    end
52
53    data.builtins.each do |builtin|
54        sig = "%s.%s" % [refs[builtin.package], builtin.name]
55        refs[builtin.ref] = sig
56        Signatures::BUILTINS[builtin.ref] = [sig, builtin.name]
57    end
58
59    data.signatures.each do |signature|
60        method_name = signature.method_name.start_with?('$') ? refs[signature.method_name[1..-1]] : signature.method_name
61        sig = "%s.%s:" % [refs[signature.callee], method_name]
62
63        signature.params.each do |param|
64            sig += "%s;" % [refs[param]]
65        end
66
67        sig += "%s;" % [refs[signature.return_type]]
68
69        Signatures::SIGNATURES[signature.ref] = sig
70    end
71
72    data.dynamiclangs.each do |lang|
73      Signatures::DYNAMIC[lang.name] = OpenStruct.new(lang)
74    end
75  end
76  module_function :wrap_data
77end
78
79def Gen.on_require(data)
80    Signatures.wrap_data(data)
81end
82
83