Module: MilkTea::ImportedBindings::Generator::GeneratorPolicy
- Included in:
- MilkTea::ImportedBindings::Generator
- Defined in:
- lib/milk_tea/bindings/imported_bindings/generator.rb
Instance Method Summary collapse
- #alias_public_name(raw_name, spec:, override:, binding_kind:) ⇒ Object
- #apply_native_type_mapping(type_str) ⇒ Object
- #apply_rename_rules(name, rules, context:) ⇒ Object
- #build_call_expression(name, type_params:, args:) ⇒ Object
- #build_foreign_signature(name, type_params:, params:, return_type:, mapping:, variadic: false, visibility: :public) ⇒ Object
- #build_method_signature(name, type_params:, params:, return_type:, kind:, visibility: :public) ⇒ Object
- #build_public_type_names(spec, declarations) ⇒ Object
- #default_include(value, context:) ⇒ Object
- #default_public_name(raw_name, spec:, context:, binding_kind:) ⇒ Object
- #duplicate_name(names) ⇒ Object
- #emit_const_aliases(spec, declarations) ⇒ Object
- #emit_foreign_functions(function_entries) ⇒ Object
- #emit_methods(method_specs, function_entries, declarations, method_sources:) ⇒ Object
- #emit_type_aliases(spec, declarations, referenced_lines:, method_sources:) ⇒ Object
- #foreign_function_name(raw_name, spec:) ⇒ Object
- #generated_binding_name_conflict?(name, binding_kind:) ⇒ Boolean
- #generated_foreign_param_name(name) ⇒ Object
- #generated_module_path ⇒ Object
- #heuristic_foreign_param_spec(param) ⇒ Object
- #index_alias_overrides(entries, declarations_by_name, context:) ⇒ Object
- #index_function_overrides(entries, declarations_by_name) ⇒ Object
- #index_raw_declarations(raw_ast) ⇒ Object
- #load_policy ⇒ Object
- #merge_import_specs(*groups) ⇒ Object
- #method_kind(function_entry, spec:, raw_name:) ⇒ Object
- #method_receiver_type_match?(param_type, receiver_types) ⇒ Boolean
- #normalize_alias_overrides(value, context:) ⇒ Object
- #normalize_alias_spec(value, context:) ⇒ Object
- #normalize_function_overrides(value) ⇒ Object
- #normalize_function_spec(value) ⇒ Object
- #normalize_generated_public_value_name(name, raw_name:, spec:) ⇒ Object
- #normalize_include(value, context:) ⇒ Object
- #normalize_method_module_import_alias(value, module_name:) ⇒ Object
- #normalize_method_module_name(value) ⇒ Object
- #normalize_method_receiver_types(value, type:) ⇒ Object
- #normalize_method_specs(value) ⇒ Object
- #normalize_method_type(value) ⇒ Object
- #normalize_name_list(value, context:, label:) ⇒ Object
- #normalize_prefix_list(value, context:) ⇒ Object
- #normalize_rename_rules(value, context:) ⇒ Object
- #normalize_strip_prefix(value, context:) ⇒ Object
- #override_param_specs(entry, raw_declaration) ⇒ Object
- #override_type_param_names(entry, raw_declaration) ⇒ Object
- #plan_foreign_functions(spec, declarations) ⇒ Object
- #plan_overridden_function(entry, raw_declaration, spec:) ⇒ Object
- #plan_pass_through_foreign_function(raw_declaration, spec:) ⇒ Object
- #policy_import_specs(policy) ⇒ Object
- #policy_label ⇒ Object
- #project_public_handle_type(type) ⇒ Object
- #public_type_kind(raw_name, override:, raw_declaration:) ⇒ Object
- #raw_import_specs(raw_ast) ⇒ Object
- #raw_type_param_names(raw_declaration) ⇒ Object
- #render_foreign_param(param) ⇒ Object
- #render_function_type_param(param) ⇒ Object
- #render_heuristic_foreign_param(param) ⇒ Object
- #render_method_param(param) ⇒ Object
- #render_method_wrapper(function_entry, spec:) ⇒ Object
- #render_overridden_foreign_function(entry, raw_declaration, spec:) ⇒ Object
- #render_overridden_function(entry, raw_declaration, spec:) ⇒ Object
- #render_pass_through_foreign_function(raw_declaration, spec:) ⇒ Object
- #render_public_foreign_type(type) ⇒ Object
- #render_raw_foreign_param(param) ⇒ Object
- #render_type(type) ⇒ Object
- #render_type_argument(argument) ⇒ Object
- #render_type_params(type_params) ⇒ Object
- #rendered_type_name(raw_name, seen = []) ⇒ Object
- #resolve_module_path(module_name) ⇒ Object
- #resolve_selected_names(spec, declarations_by_name, ordered_names, context:) ⇒ Object
- #sanitize_generated_binding_name(name, binding_kind:) ⇒ Object
- #selected_by_spec?(raw_name, spec) ⇒ Boolean
- #strip_prefix(name, prefix, context:) ⇒ Object
- #unqualified_type_name_referenced?(name, lines) ⇒ Boolean
- #validate_allowed_keys!(value, allowed_keys, context:) ⇒ Object
- #validate_import_specs!(import_specs) ⇒ Object
- #validate_known_raw_names!(names, declarations_by_name, context:) ⇒ Object
- #validate_policy!(policy) ⇒ Object
- #validate_raw_module!(raw_ast) ⇒ Object
Instance Method Details
#alias_public_name(raw_name, spec:, override:, binding_kind:) ⇒ Object
655 656 657 658 659 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 655 def alias_public_name(raw_name, spec:, override:, binding_kind:) return override["name"] if override && override.key?("name") default_public_name(raw_name, spec:, context: "generated name", binding_kind:) end |
#apply_native_type_mapping(type_str) ⇒ Object
791 792 793 794 795 796 797 798 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 791 def apply_native_type_mapping(type_str) return type_str unless type_str.is_a?(String) return type_str if @native_type_mapping.empty? type_str.gsub(/\b([A-Z][A-Za-z0-9_]*)\b/) do |match| @native_type_mapping[match] || match end end |
#apply_rename_rules(name, rules, context:) ⇒ Object
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 693 def apply_rename_rules(name, rules, context:) transformed = name rules.each do |rule| case rule[:kind] when :prefix next unless transformed.start_with?(rule[:match]) transformed = rule[:replace_with] + transformed.delete_prefix(rule[:match]) when :replace transformed = transformed.gsub(rule[:match], rule[:replace_with]) when :camelize transformed = camelize_binding_name(transformed) when :opengl transformed = openglize_binding_name(transformed) else raise Error, "unsupported #{context} rename rule #{rule[:kind]} in #{@policy_path}" end end raise Error, "#{context} in #{@policy_path} cannot be empty" if transformed.empty? transformed end |
#build_call_expression(name, type_params:, args:) ⇒ Object
928 929 930 931 932 933 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 928 def build_call_expression(name, type_params:, args:) expression = name.dup expression << render_type_params(type_params) expression << "(#{args.join(', ')})" expression end |
#build_foreign_signature(name, type_params:, params:, return_type:, mapping:, variadic: false, visibility: :public) ⇒ Object
899 900 901 902 903 904 905 906 907 908 909 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 899 def build_foreign_signature(name, type_params:, params:, return_type:, mapping:, variadic: false, visibility: :public) signature = +"" signature << "public " if visibility == :public signature << "foreign function #{name}" signature << render_type_params(type_params) rendered_params = params.dup rendered_params << "..." if variadic signature << "(#{rendered_params.join(', ')}) -> #{return_type}" signature << " = #{mapping}" signature end |
#build_method_signature(name, type_params:, params:, return_type:, kind:, visibility: :public) ⇒ Object
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 911 def build_method_signature(name, type_params:, params:, return_type:, kind:, visibility: :public) signature = +"" signature << "public " if visibility == :public signature << case kind when :static "static function " when :editable "editable function " else "function " end signature << name signature << render_type_params(type_params) signature << "(#{params.join(', ')}) -> #{return_type}:" signature end |
#build_public_type_names(spec, declarations) ⇒ Object
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 617 def build_public_type_names(spec, declarations) overrides = index_alias_overrides(spec[:overrides], declarations[:types], context: "type") public_names = {} seen_public_names = {} @public_type_kinds_by_raw_name = {} resolve_selected_names(spec, declarations[:types], declarations[:type_order], context: "type").each do |raw_name| raw_declaration = declarations[:types].fetch(raw_name) override = overrides[raw_name] public_name = alias_public_name(raw_name, spec:, override:, binding_kind: :type) raise Error, "duplicate generated type #{public_name} in #{@policy_path}" if seen_public_names.key?(public_name) seen_public_names[public_name] = true @public_type_kinds_by_raw_name[raw_name] = public_type_kind(raw_name, override:, raw_declaration:) public_names[raw_name] = public_name end public_names end |
#default_include(value, context:) ⇒ Object
524 525 526 527 528 529 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 524 def default_include(value, context:) return normalize_include(value["include"], context:) if value.key?("include") return [] if value.key?("include_prefixes") :all end |
#default_public_name(raw_name, spec:, context:, binding_kind:) ⇒ Object
679 680 681 682 683 684 685 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 679 def default_public_name(raw_name, spec:, context:, binding_kind:) transformed = apply_rename_rules(raw_name, spec[:rename_rules], context:) transformed = strip_prefix(transformed, spec[:strip_prefix], context:) if spec[:strip_prefix] raise Error, "#{context} in #{@policy_path} cannot be empty" if transformed.empty? sanitize_generated_binding_name(transformed, binding_kind:) end |
#duplicate_name(names) ⇒ Object
575 576 577 578 579 580 581 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 575 def duplicate_name(names) names.tally.each do |name, count| return name if count > 1 end nil end |
#emit_const_aliases(spec, declarations) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 176 def emit_const_aliases(spec, declarations) overrides = index_alias_overrides(spec[:overrides], declarations[:values], context: "constant") seen_public_names = {} resolve_selected_names(spec, declarations[:values], declarations[:value_order], context: "constant").map do |raw_name| raw_declaration = declarations[:values].fetch(raw_name) override = overrides[raw_name] public_name = alias_public_name(raw_name, spec:, override:, binding_kind: :value) raise Error, "duplicate generated constant #{public_name} in #{@policy_path}" if seen_public_names.key?(public_name) seen_public_names[public_name] = true const_type = override && override["type"] || render_type(raw_declaration.type) mapping = override && override["mapping"] || "#{@import_alias}.#{raw_name}" "public const #{public_name}: #{const_type} = #{mapping}" end end |
#emit_foreign_functions(function_entries) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 193 def emit_foreign_functions(function_entries) function_entries.map do |entry| build_foreign_signature( entry.fetch(:public_name), type_params: entry.fetch(:type_params), params: entry.fetch(:params).map { |param| render_foreign_param(param) }, return_type: entry.fetch(:return_type), mapping: entry.fetch(:mapping), variadic: entry.fetch(:variadic, false), ) end end |
#emit_methods(method_specs, function_entries, declarations, method_sources:) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 259 260 261 262 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 206 def emit_methods(method_specs, function_entries, declarations, method_sources:) return [] if method_specs.empty? entries_by_raw_name = function_entries.group_by { |entry| entry.fetch(:raw_name) } available_entries = entries_by_raw_name.transform_values(&:first) ordered_names = declarations[:function_order].select { |raw_name| entries_by_raw_name.key?(raw_name) } lines = [] method_specs.each do |spec| if spec[:module_name] source = method_sources.fetch(method_source_key(spec)) source_entries = source.functions selected_names = resolve_selected_names(spec, source_entries, source.function_order, context: "method function") next if selected_names.empty? seen_method_names = {} lines << "" unless lines.empty? lines << "extending #{spec.fetch(:type)}:" selected_names.each do |source_name| method_name, wrapper_lines = render_method_wrapper(plan_method_source_function(source_entries.fetch(source_name), source:), spec:) if seen_method_names.key?(method_name) raise Error, "duplicate generated method #{method_name} in #{@policy_path}" end seen_method_names[method_name] = true wrapper_lines.each { |line| lines << " #{line}" } end next end selected_names = resolve_selected_names(spec, available_entries, ordered_names, context: "method function") next if selected_names.empty? seen_method_names = {} lines << "" unless lines.empty? lines << "extending #{spec.fetch(:type)}:" selected_names.each do |raw_name| generated_entries = entries_by_raw_name.fetch(raw_name) if generated_entries.length != 1 raise Error, "method generation for #{raw_name} in #{@policy_path} does not support multiple generated signatures" end method_name, wrapper_lines = render_method_wrapper(generated_entries.first, spec:) if seen_method_names.key?(method_name) raise Error, "duplicate generated method #{method_name} in #{@policy_path}" end seen_method_names[method_name] = true wrapper_lines.each { |line| lines << " #{line}" } end end lines end |
#emit_type_aliases(spec, declarations, referenced_lines:, method_sources:) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 134 def emit_type_aliases(spec, declarations, referenced_lines:, method_sources:) overrides = index_alias_overrides(spec[:overrides], declarations[:types], context: "type") seen_public_names = {} selected_names = resolve_selected_names(spec, declarations[:types], declarations[:type_order], context: "type") if spec[:include] == :all shadowed_public_type_names = method_sources.values.flat_map(&:public_type_names).uniq selected_names = selected_names.reject do |raw_name| override = overrides[raw_name] next false if override public_name = alias_public_name(raw_name, spec:, override:, binding_kind: :type) shadowed_public_type_names.include?(public_name) && !unqualified_type_name_referenced?(public_name, referenced_lines) end end selected_names.map do |raw_name| raw_declaration = declarations[:types].fetch(raw_name) override = overrides[raw_name] public_name = alias_public_name(raw_name, spec:, override:, binding_kind: :type) raise Error, "duplicate generated type #{public_name} in #{@policy_path}" if seen_public_names.key?(public_name) seen_public_names[public_name] = true case public_type_kind(raw_name, override:, raw_declaration:) when :alias mapping = override && override["mapping"] || @native_type_mapping[raw_name] || "#{@import_alias}.#{raw_name}" "public type #{public_name} = #{mapping}" when :opaque opaque_c_name = raw_declaration.c_name || raw_name "public opaque #{public_name} = c#{opaque_c_name.inspect}" else raise Error, "unsupported generated public type kind for #{raw_name} in #{@policy_path}" end end end |
#foreign_function_name(raw_name, spec:) ⇒ Object
827 828 829 830 831 832 833 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 827 def foreign_function_name(raw_name, spec:) normalize_generated_public_value_name( default_public_name(raw_name, spec:, context: "generated function name", binding_kind: :value), raw_name:, spec:, ) end |
#generated_binding_name_conflict?(name, binding_kind:) ⇒ Boolean
976 977 978 979 980 981 982 983 984 985 986 987 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 976 def generated_binding_name_conflict?(name, binding_kind:) return true if Token::KEYWORDS.key?(name) case binding_kind when :type Types::RESERVED_TYPE_BINDING_NAMES.include?(name) when :value Types::RESERVED_VALUE_TYPE_NAMES.include?(name) else raise Error, "unsupported generated binding kind #{binding_kind.inspect} in #{@policy_path}" end end |
#generated_foreign_param_name(name) ⇒ Object
969 970 971 972 973 974 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 969 def generated_foreign_param_name(name) normalized = snake_case(name) return normalized unless generated_binding_name_conflict?(normalized, binding_kind: :value) "#{normalized}_" end |
#generated_module_path ⇒ Object
78 79 80 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 78 def generated_module_path "#{@module_name.tr('.', '/')}" + ".mt" end |
#heuristic_foreign_param_spec(param) ⇒ Object
958 959 960 961 962 963 964 965 966 967 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 958 def heuristic_foreign_param_spec(param) type = param.type if type.is_a?(AST::TypeRef) && type.name.to_s == "cstr" && type.arguments.empty? && !type.nullable name = generated_foreign_param_name(param.name) return { "name" => name, "type" => "str", "boundary_type" => "cstr" } end { "name" => generated_foreign_param_name(param.name), "type" => render_public_foreign_type(param.type) } end |
#index_alias_overrides(entries, declarations_by_name, context:) ⇒ Object
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 637 def index_alias_overrides(entries, declarations_by_name, context:) overrides = {} allowed_keys = context == "constant" ? %w[raw name type mapping] : %w[raw name mapping kind] entries.each do |entry| validate_allowed_keys!(entry, allowed_keys, context: "#{context} override") raw_name = entry.fetch("raw") raise Error, "#{context} override raw names in #{@policy_path} must be strings" unless raw_name.is_a?(String) raise Error, "unknown raw #{context} #{raw_name} in #{@raw_module_name}" unless declarations_by_name.key?(raw_name) raise Error, "duplicate #{context} override #{raw_name} in #{@policy_path}" if overrides.key?(raw_name) overrides[raw_name] = entry end overrides end |
#index_function_overrides(entries, declarations_by_name) ⇒ Object
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 725 def index_function_overrides(entries, declarations_by_name) overrides = {} entries.each do |entry| validate_allowed_keys!(entry, %w[raw name type_params params return_type mapping], context: "function override") raw_name = entry.fetch("raw") raise Error, "function override raw names in #{@policy_path} must be strings" unless raw_name.is_a?(String) raise Error, "unknown raw function #{raw_name} in #{@raw_module_name}" unless declarations_by_name.key?(raw_name) (overrides[raw_name] ||= []) << entry end overrides end |
#index_raw_declarations(raw_ast) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 102 def index_raw_declarations(raw_ast) types = {} type_order = [] values = {} value_order = [] functions = {} function_order = [] raw_ast.declarations.each do |declaration| case declaration when AST::TypeAliasDecl, AST::StructDecl, AST::UnionDecl, AST::EnumDecl, AST::FlagsDecl, AST::OpaqueDecl types[declaration.name] = declaration type_order << declaration.name when AST::ConstDecl values[declaration.name] = declaration value_order << declaration.name when AST::ExternFunctionDecl functions[declaration.name] = declaration function_order << declaration.name end end { types:, type_order:, values:, value_order:, functions:, function_order:, } end |
#load_policy ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 7 def load_policy JSON.parse(File.read(@policy_path)) rescue Errno::ENOENT raise Error, "imported binding policy not found: #{@policy_path}" rescue JSON::ParserError => e raise Error, "failed to parse imported binding policy #{@policy_path}: #{e.}" end |
#merge_import_specs(*groups) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 90 def merge_import_specs(*groups) seen = {} groups.flatten.compact.each_with_object([]) do |spec, merged| key = [spec[:module_name], spec[:alias]] next if seen[key] seen[key] = true merged << spec end end |
#method_kind(function_entry, spec:, raw_name:) ⇒ Object
878 879 880 881 882 883 884 885 886 887 888 889 890 891 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 878 def method_kind(function_entry, spec:, raw_name:) first_param = function_entry.fetch(:params).first return :static unless first_param return :static unless method_receiver_type_match?(first_param.fetch("type"), spec.fetch(:receiver_types)) case first_param["mode"] when nil :instance when "inout" :editable else raise Error, "method generation for #{raw_name} in #{@policy_path} cannot use receiver mode #{first_param["mode"].inspect}" end end |
#method_receiver_type_match?(param_type, receiver_types) ⇒ Boolean
893 894 895 896 897 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 893 def method_receiver_type_match?(param_type, receiver_types) return true if receiver_types.include?(param_type) receiver_types.any? { |rt| @native_type_mapping[rt] == param_type } end |
#normalize_alias_overrides(value, context:) ⇒ Object
462 463 464 465 466 467 468 469 470 471 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 462 def normalize_alias_overrides(value, context:) return [] if value.nil? raise Error, "#{context} overrides in #{@policy_path} must be an array" unless value.is_a?(Array) value.each do |entry| raise Error, "#{context} overrides in #{@policy_path} must be objects" unless entry.is_a?(Hash) end value end |
#normalize_alias_spec(value, context:) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 317 def normalize_alias_spec(value, context:) case value when nil { include: :all, include_prefixes: [], exclude: [], overrides: [], rename_rules: [], strip_prefix: nil, native_types: {}, } when Array { include: normalize_name_list(value, context:, label: "include"), include_prefixes: [], exclude: [], overrides: [], rename_rules: [], strip_prefix: nil, native_types: {}, } when Hash allowed_keys = %w[include include_prefixes exclude overrides rename_rules strip_prefix native_types] validate_allowed_keys!(value, allowed_keys, context: "#{context} section") native_types = value["native_types"] || {} raise Error, "native_types in #{@policy_path} must be an object" unless native_types.is_a?(Hash) native_types.each do |raw_type, native_type| raise Error, "native_types key '#{raw_type}' in #{@policy_path} must be a non-empty string" unless native_type.is_a?(String) && !native_type.empty? end { include: default_include(value, context:), include_prefixes: normalize_prefix_list(value["include_prefixes"], context:), exclude: normalize_name_list(value["exclude"], context:, label: "exclude"), overrides: normalize_alias_overrides(value["overrides"], context:), rename_rules: normalize_rename_rules(value["rename_rules"], context:), strip_prefix: normalize_strip_prefix(value["strip_prefix"], context:), native_types:, } else raise Error, "#{context} section in #{@policy_path} must be an array or object" end end |
#normalize_function_overrides(value) ⇒ Object
564 565 566 567 568 569 570 571 572 573 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 564 def normalize_function_overrides(value) return [] if value.nil? raise Error, "function overrides in #{@policy_path} must be an array" unless value.is_a?(Array) value.each do |entry| raise Error, "function overrides in #{@policy_path} must be objects" unless entry.is_a?(Hash) end value end |
#normalize_function_spec(value) ⇒ Object
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 361 def normalize_function_spec(value) case value when nil { include: [], include_prefixes: [], exclude: [], overrides: [], rename_rules: [], strip_prefix: nil, } when Array if value.all? { |entry| entry.is_a?(String) } { include: normalize_name_list(value, context: "function", label: "include"), include_prefixes: [], exclude: [], overrides: [], rename_rules: [], strip_prefix: nil, } else { include: [], include_prefixes: [], exclude: [], overrides: normalize_function_overrides(value), rename_rules: [], strip_prefix: nil, } end when Hash validate_allowed_keys!(value, %w[include include_prefixes exclude overrides rename_rules strip_prefix], context: "function section") { include: default_include(value, context: "function"), include_prefixes: normalize_prefix_list(value["include_prefixes"], context: "function"), exclude: normalize_name_list(value["exclude"], context: "function", label: "exclude"), overrides: normalize_function_overrides(value["overrides"]), rename_rules: normalize_rename_rules(value["rename_rules"], context: "function"), strip_prefix: normalize_strip_prefix(value["strip_prefix"], context: "function"), } else raise Error, "function section in #{@policy_path} must be an array or object" end end |
#normalize_generated_public_value_name(name, raw_name:, spec:) ⇒ Object
871 872 873 874 875 876 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 871 def normalize_generated_public_value_name(name, raw_name:, spec:) normalized = snake_case(name) return normalized unless spec[:rename_rules].any? { |rule| rule[:kind] == :opengl } normalize_opengl_terminal_suffix(normalize_opengl_snake_case(normalized), raw_name) end |
#normalize_include(value, context:) ⇒ Object
518 519 520 521 522 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 518 def normalize_include(value, context:) return :all if value.nil? || value == "all" normalize_name_list(value, context:, label: "include") end |
#normalize_method_module_import_alias(value, module_name:) ⇒ Object
439 440 441 442 443 444 445 446 447 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 439 def normalize_method_module_import_alias(value, module_name:) return nil if module_name.nil? && value.nil? raise Error, "methods entry module_import_alias in #{@policy_path} requires module_name" if module_name.nil? return module_name.split(".").last if value.nil? raise Error, "methods entry module_import_alias in #{@policy_path} must be a string" unless value.is_a?(String) raise Error, "methods entry module_import_alias in #{@policy_path} cannot be empty" if value.empty? value end |
#normalize_method_module_name(value) ⇒ Object
431 432 433 434 435 436 437 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 431 def normalize_method_module_name(value) return nil if value.nil? raise Error, "methods entry module_name in #{@policy_path} must be a string" unless value.is_a?(String) raise Error, "methods entry module_name in #{@policy_path} cannot be empty" if value.empty? value end |
#normalize_method_receiver_types(value, type:) ⇒ Object
456 457 458 459 460 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 456 def normalize_method_receiver_types(value, type:) return [type] if value.nil? normalize_name_list(value, context: "method receiver type", label: "method receiver type") end |
#normalize_method_specs(value) ⇒ Object
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 407 def normalize_method_specs(value) return [] if value.nil? raise Error, "methods section in #{@policy_path} must be an array" unless value.is_a?(Array) value.map do |entry| raise Error, "methods entries in #{@policy_path} must be objects" unless entry.is_a?(Hash) validate_allowed_keys!(entry, %w[type receiver_types include include_prefixes exclude rename_rules strip_prefix module_name module_import_alias], context: "methods entry") type = normalize_method_type(entry["type"]) { type:, receiver_types: normalize_method_receiver_types(entry["receiver_types"], type:), include: default_include(entry, context: "method"), include_prefixes: normalize_prefix_list(entry["include_prefixes"], context: "method"), exclude: normalize_name_list(entry["exclude"], context: "method", label: "exclude"), rename_rules: normalize_rename_rules(entry["rename_rules"], context: "method"), strip_prefix: normalize_strip_prefix(entry["strip_prefix"], context: "method"), module_name: normalize_method_module_name(entry["module_name"]), module_import_alias: normalize_method_module_import_alias(entry["module_import_alias"], module_name: entry["module_name"]), } end end |
#normalize_method_type(value) ⇒ Object
449 450 451 452 453 454 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 449 def normalize_method_type(value) raise Error, "methods entry type in #{@policy_path} must be a string" unless value.is_a?(String) raise Error, "methods entry type in #{@policy_path} cannot be empty" if value.empty? value end |
#normalize_name_list(value, context:, label:) ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 548 def normalize_name_list(value, context:, label:) return [] if value.nil? raise Error, "#{label} #{context}s in #{@policy_path} must be an array" unless value.is_a?(Array) names = value.map do |entry| raise Error, "#{label} #{context} entries in #{@policy_path} must be strings" unless entry.is_a?(String) entry end duplicate = duplicate_name(names) raise Error, "duplicate #{label} #{context} #{duplicate} in #{@policy_path}" if duplicate names end |
#normalize_prefix_list(value, context:) ⇒ Object
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 531 def normalize_prefix_list(value, context:) return [] if value.nil? raise Error, "include_prefixes #{context}s in #{@policy_path} must be an array" unless value.is_a?(Array) prefixes = value.map do |entry| raise Error, "include_prefixes #{context} entries in #{@policy_path} must be strings" unless entry.is_a?(String) raise Error, "include_prefixes #{context} entries in #{@policy_path} cannot be empty" if entry.empty? entry end duplicate = duplicate_name(prefixes) raise Error, "duplicate include_prefixes #{context} #{duplicate} in #{@policy_path}" if duplicate prefixes end |
#normalize_rename_rules(value, context:) ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 481 def normalize_rename_rules(value, context:) return [] if value.nil? raise Error, "#{context} rename_rules in #{@policy_path} must be an array" unless value.is_a?(Array) value.map do |entry| raise Error, "#{context} rename_rules in #{@policy_path} must be objects" unless entry.is_a?(Hash) validate_allowed_keys!(entry, %w[kind match replace_with], context: "#{context} rename rule") kind = entry.fetch("kind") unless %w[prefix replace camelize opengl].include?(kind) raise Error, "#{context} rename rule kind in #{@policy_path} must be prefix, replace, camelize, or opengl" end if %w[camelize opengl].include?(kind) next { kind: kind.to_sym, match: nil, replace_with: nil, } end match = entry.fetch("match") raise Error, "#{context} rename rule match in #{@policy_path} must be a string" unless match.is_a?(String) raise Error, "#{context} rename rule match in #{@policy_path} cannot be empty" if match.empty? replace_with = entry.fetch("replace_with", "") raise Error, "#{context} rename rule replace_with in #{@policy_path} must be a string" unless replace_with.is_a?(String) { kind: kind.to_sym, match:, replace_with:, } end end |
#normalize_strip_prefix(value, context:) ⇒ Object
473 474 475 476 477 478 479 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 473 def normalize_strip_prefix(value, context:) return nil if value.nil? raise Error, "#{context} strip_prefix in #{@policy_path} must be a string" unless value.is_a?(String) raise Error, "#{context} strip_prefix in #{@policy_path} cannot be empty" if value.empty? value end |
#override_param_specs(entry, raw_declaration) ⇒ Object
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 773 def override_param_specs(entry, raw_declaration) if entry.key?("params") Array(entry["params"]).map do |param| param = param.dup param["name"] = generated_foreign_param_name(param.fetch("name")) param["type"] = apply_native_type_mapping(param["type"]) param end else raw_declaration.params.map do |param| { "name" => generated_foreign_param_name(param.name), "type" => render_public_foreign_type(param.type), } end end end |
#override_type_param_names(entry, raw_declaration) ⇒ Object
765 766 767 768 769 770 771 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 765 def override_type_param_names(entry, raw_declaration) if entry.key?("type_params") normalize_name_list(entry["type_params"], context: "function type parameter", label: "function type parameter") else raw_type_param_names(raw_declaration) end end |
#plan_foreign_functions(spec, declarations) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 264 def plan_foreign_functions(spec, declarations) validate_known_raw_names!(spec[:exclude], declarations[:functions], context: "function") validate_known_raw_names!(spec[:include], declarations[:functions], context: "function") unless spec[:include] == :all overrides = index_function_overrides(spec[:overrides], declarations[:functions]) seen_public_names = {} declarations[:function_order].flat_map do |raw_name| selected = selected_by_spec?(raw_name, spec) || overrides.key?(raw_name) next [] unless selected next [] if spec[:exclude].include?(raw_name) raw_declaration = declarations[:functions].fetch(raw_name) if raw_declaration.variadic if overrides.key?(raw_name) generated_entries = overrides.fetch(raw_name).map do |entry| unless entry.key?("mapping") raise Error, "variadic raw function #{raw_name} in #{@raw_module_name} requires an explicit mapping override" end plan_overridden_function(entry, raw_declaration, spec:) end generated_entries.each do |entry| public_name = entry.fetch(:public_name) raise Error, "duplicate generated function #{public_name} in #{@policy_path}" if seen_public_names.key?(public_name) seen_public_names[public_name] = true end next generated_entries end end generated_entries = if overrides.key?(raw_name) overrides.fetch(raw_name).map do |entry| plan_overridden_function(entry, raw_declaration, spec:) end else [plan_pass_through_foreign_function(raw_declaration, spec:)] end generated_entries.each do |entry| public_name = entry.fetch(:public_name) raise Error, "duplicate generated function #{public_name} in #{@policy_path}" if seen_public_names.key?(public_name) seen_public_names[public_name] = true end generated_entries end end |
#plan_overridden_function(entry, raw_declaration, spec:) ⇒ Object
800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 800 def plan_overridden_function(entry, raw_declaration, spec:) raw_name = raw_declaration.name return_type = entry["return_type"] || render_public_foreign_type(raw_declaration.return_type) return_type = apply_native_type_mapping(return_type) if return_type { raw_name:, public_name: entry["name"] || foreign_function_name(raw_name, spec:), type_params: override_type_param_names(entry, raw_declaration), params: override_param_specs(entry, raw_declaration), return_type:, mapping: entry["mapping"] || "#{@import_alias}.#{raw_name}", variadic: false, } end |
#plan_pass_through_foreign_function(raw_declaration, spec:) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 815 def plan_pass_through_foreign_function(raw_declaration, spec:) { raw_name: raw_declaration.name, public_name: foreign_function_name(raw_declaration.name, spec:), type_params: raw_type_param_names(raw_declaration), params: raw_declaration.params.map { |param| heuristic_foreign_param_spec(param) }, return_type: render_public_foreign_type(raw_declaration.return_type), mapping: "#{@import_alias}.#{raw_declaration.name}", variadic: raw_declaration.variadic, } end |
#policy_import_specs(policy) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 57 def policy_import_specs(policy) entries = policy["imports"] || [] raise Error, "imports in #{@policy_path} must be an array" unless entries.is_a?(Array) entries.map.with_index do |entry, index| raise Error, "imports[#{index}] in #{@policy_path} must be an object" unless entry.is_a?(Hash) validate_allowed_keys!(entry, %w[module_name alias], context: "policy import") module_name = entry.fetch("module_name") alias_name = entry.fetch("alias") raise Error, "policy import module_name in #{@policy_path} must be a non-empty string" unless module_name.is_a?(String) && !module_name.empty? raise Error, "policy import alias in #{@policy_path} must be a non-empty string" unless alias_name.is_a?(String) && !alias_name.empty? { module_name:, alias: alias_name, } end end |
#policy_label ⇒ Object
1095 1096 1097 1098 1099 1100 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1095 def policy_label root_prefix = MilkTea.root.to_s + "/" return @policy_path.delete_prefix(root_prefix) if @policy_path.start_with?(root_prefix) File.basename(@policy_path) end |
#project_public_handle_type(type) ⇒ Object
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 996 def project_public_handle_type(type) return unless type.is_a?(AST::TypeRef) return unless %w[ptr const_ptr].include?(type.name.to_s) return unless type.arguments.length == 1 pointee = type.arguments.first.value return unless pointee.is_a?(AST::TypeRef) return unless pointee.arguments.empty? return unless @public_type_kinds_by_raw_name[pointee.name.to_s] == :opaque text = rendered_type_name(pointee.name.to_s).dup text << "?" if type.nullable text end |
#public_type_kind(raw_name, override:, raw_declaration:) ⇒ Object
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 661 def public_type_kind(raw_name, override:, raw_declaration:) kind = override && override["kind"] return :alias if kind.nil? raise Error, "type override kind for #{raw_name} in #{@policy_path} must be alias or opaque" unless %w[alias opaque].include?(kind) return :alias if kind == "alias" unless raw_declaration.is_a?(AST::OpaqueDecl) raise Error, "type override #{raw_name} in #{@policy_path} can only use kind opaque for raw opaque declarations" end if override.key?("mapping") raise Error, "type override #{raw_name} in #{@policy_path} cannot use mapping with kind opaque" end :opaque end |
#raw_import_specs(raw_ast) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 48 def raw_import_specs(raw_ast) raw_ast.imports.map do |import| { module_name: import.path.parts.join("."), alias: import.alias_name, } end end |
#raw_type_param_names(raw_declaration) ⇒ Object
935 936 937 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 935 def raw_type_param_names(raw_declaration) Array(raw_declaration.type_params).map(&:name) end |
#render_foreign_param(param) ⇒ Object
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1011 def render_foreign_param(param) unless param.is_a?(Hash) raise Error, "function parameters in #{@policy_path} must be objects" end text = +"" mode = param["mode"] text << "#{mode} " if mode text << "#{param.fetch("name")}: #{param.fetch("type")}" boundary_type = param["boundary_type"] text << " as #{boundary_type}" if boundary_type text end |
#render_function_type_param(param) ⇒ Object
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1049 def render_function_type_param(param) case param when AST::Param "#{param.name}: #{render_public_foreign_type(param.type)}" when AST::ForeignParam render_foreign_param({ "name" => param.name, "type" => render_public_foreign_type(param.type), "mode" => param.mode, "boundary_type" => param.boundary_type && render_public_foreign_type(param.boundary_type) }) else render_type(param) end end |
#render_heuristic_foreign_param(param) ⇒ Object
947 948 949 950 951 952 953 954 955 956 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 947 def render_heuristic_foreign_param(param) type = param.type if type.is_a?(AST::TypeRef) && type.name.to_s == "cstr" && type.arguments.empty? && !type.nullable name = generated_foreign_param_name(param.name) return render_foreign_param({ "name" => name, "type" => "str", "boundary_type" => "cstr" }) end render_raw_foreign_param(param) end |
#render_method_param(param) ⇒ Object
943 944 945 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 943 def render_method_param(param) "#{param.fetch("name")}: #{param.fetch("type")}" end |
#render_method_wrapper(function_entry, spec:) ⇒ Object
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 835 def render_method_wrapper(function_entry, spec:) raw_name = function_entry.fetch(:raw_name) method_name = normalize_generated_public_value_name( default_public_name(raw_name, spec:, context: "generated method name", binding_kind: :value), raw_name:, spec:, ) if function_entry.fetch(:variadic, false) raise Error, "method generation for #{raw_name} in #{@policy_path} cannot wrap variadic functions" end method_kind = method_kind(function_entry, spec:, raw_name:) method_params = method_kind == :static ? function_entry.fetch(:params) : function_entry.fetch(:params).drop(1) if method_params.any? { |param| param["mode"] } raise Error, "method generation for #{raw_name} in #{@policy_path} cannot wrap out/inout non-receiver parameters; exclude it from methods" end call_args = [] call_args << "this" unless method_kind == :static call_args.concat(method_params.map { |param| param.fetch("name") }) call_expression = build_call_expression(function_entry.fetch(:call_name, function_entry.fetch(:public_name)), type_params: function_entry.fetch(:type_params), args: call_args) body_line = function_entry.fetch(:return_type) == "void" ? call_expression : "return #{call_expression}" [ method_name, [ build_method_signature( method_name, type_params: function_entry.fetch(:type_params), params: method_params.map { |param| render_method_param(param) }, return_type: function_entry.fetch(:return_type), kind: method_kind, ), " #{body_line}", ], ] end |
#render_overridden_foreign_function(entry, raw_declaration, spec:) ⇒ Object
745 746 747 748 749 750 751 752 753 754 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 745 def render_overridden_foreign_function(entry, raw_declaration, spec:) raw_name = raw_declaration.name function_name = entry["name"] || foreign_function_name(raw_name, spec:) type_params = override_type_param_names(entry, raw_declaration) params = override_param_specs(entry, raw_declaration).map { |param| render_foreign_param(param) } return_type = entry["return_type"] || render_public_foreign_type(raw_declaration.return_type) mapping = entry["mapping"] || "#{@import_alias}.#{raw_name}" [function_name, [build_foreign_signature(function_name, type_params:, params:, return_type:, mapping:, variadic: false)]] end |
#render_overridden_function(entry, raw_declaration, spec:) ⇒ Object
741 742 743 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 741 def render_overridden_function(entry, raw_declaration, spec:) render_overridden_foreign_function(entry, raw_declaration, spec:) end |
#render_pass_through_foreign_function(raw_declaration, spec:) ⇒ Object
756 757 758 759 760 761 762 763 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 756 def render_pass_through_foreign_function(raw_declaration, spec:) function_name = foreign_function_name(raw_declaration.name, spec:) params = raw_declaration.params.map { |param| render_heuristic_foreign_param(param) } return_type = render_public_foreign_type(raw_declaration.return_type) mapping = "#{@import_alias}.#{raw_declaration.name}" [function_name, [build_foreign_signature(function_name, type_params: raw_type_param_names(raw_declaration), params:, return_type:, mapping:, variadic: raw_declaration.variadic)]] end |
#render_public_foreign_type(type) ⇒ Object
989 990 991 992 993 994 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 989 def render_public_foreign_type(type) projected_handle = project_public_handle_type(type) return projected_handle if projected_handle render_type(type) end |
#render_raw_foreign_param(param) ⇒ Object
939 940 941 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 939 def render_raw_foreign_param(param) "#{generated_foreign_param_name(param.name)}: #{render_public_foreign_type(param.type)}" end |
#render_type(type) ⇒ Object
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1031 def render_type(type) case type when AST::TypeRef text = rendered_type_name(type.name.to_s).dup unless type.arguments.empty? rendered_arguments = type.arguments.map { |argument| render_type_argument(argument.value) } text << "[#{rendered_arguments.join(', ')}]" end text << "?" if type.nullable text when AST::FunctionType params = type.params.map { |param| render_function_type_param(param) }.join(', ') "fn(#{params}) -> #{render_public_foreign_type(type.return_type)}" else raise Error, "unsupported raw type node #{type.class.name} in #{@raw_module_path}" end end |
#render_type_argument(argument) ⇒ Object
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1060 def render_type_argument(argument) case argument when AST::TypeRef, AST::FunctionType render_type(argument) when AST::IntegerLiteral argument.lexeme when AST::Identifier argument.name else raise Error, "unsupported raw type argument #{argument.class.name} in #{@raw_module_path}" end end |
#render_type_params(type_params) ⇒ Object
1025 1026 1027 1028 1029 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1025 def render_type_params(type_params) return "" if type_params.empty? "[#{type_params.join(', ')}]" end |
#rendered_type_name(raw_name, seen = []) ⇒ Object
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1073 def rendered_type_name(raw_name, seen = []) return @native_type_mapping[raw_name] if @native_type_mapping.key?(raw_name) = raw_name.split(".").last return @native_type_mapping[] if @native_type_mapping.key?() public_name = @public_type_names_by_raw_name[raw_name] return public_name if public_name raise Error, "cyclic raw type alias #{raw_name} in #{@raw_module_path}" if seen.include?(raw_name) raw_declaration = @raw_type_declarations[raw_name] if raw_declaration.is_a?(AST::TypeAliasDecl) && raw_declaration.target.is_a?(AST::TypeRef) && raw_declaration.target.arguments.empty? && !raw_declaration.target.nullable return rendered_type_name(raw_declaration.target.name.to_s, seen + [raw_name]) end raw_name end |
#resolve_module_path(module_name) ⇒ Object
1102 1103 1104 1105 1106 1107 1108 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 1102 def resolve_module_path(module_name) relative_path = File.join(*module_name.split(".")) + ".mt" candidate = @module_roots.lazy.map { |root| File.join(root, relative_path) }.find { |path| File.file?(path) } raise Error, "module not found: #{module_name}" unless candidate File.(candidate) end |
#resolve_selected_names(spec, declarations_by_name, ordered_names, context:) ⇒ Object
590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 590 def resolve_selected_names(spec, declarations_by_name, ordered_names, context:) validate_known_raw_names!(spec[:exclude], declarations_by_name, context:) selected_names = if spec[:include] == :all ordered_names.dup else validate_known_raw_names!(spec[:include], declarations_by_name, context:) ordered_names.select { |raw_name| selected_by_spec?(raw_name, spec) } end selected_names.reject { |raw_name| spec[:exclude].include?(raw_name) } end |
#sanitize_generated_binding_name(name, binding_kind:) ⇒ Object
687 688 689 690 691 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 687 def sanitize_generated_binding_name(name, binding_kind:) return name unless generated_binding_name_conflict?(name, binding_kind:) "#{name}_" end |
#selected_by_spec?(raw_name, spec) ⇒ Boolean
603 604 605 606 607 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 603 def selected_by_spec?(raw_name, spec) return true if spec[:include] == :all spec[:include].include?(raw_name) || spec[:include_prefixes].any? { |prefix| raw_name.start_with?(prefix) } end |
#strip_prefix(name, prefix, context:) ⇒ Object
718 719 720 721 722 723 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 718 def strip_prefix(name, prefix, context:) stripped = prefix && name.start_with?(prefix) ? name.delete_prefix(prefix) : name raise Error, "#{context} in #{@policy_path} cannot be empty" if stripped.empty? stripped end |
#unqualified_type_name_referenced?(name, lines) ⇒ Boolean
170 171 172 173 174 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 170 def unqualified_type_name_referenced?(name, lines) pattern = /(?<![A-Za-z0-9_.])#{Regexp.escape(name)}(?![A-Za-z0-9_])/ lines.any? { |line| line.match?(pattern) } end |
#validate_allowed_keys!(value, allowed_keys, context:) ⇒ Object
583 584 585 586 587 588 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 583 def validate_allowed_keys!(value, allowed_keys, context:) unknown_keys = value.keys - allowed_keys return if unknown_keys.empty? raise Error, "#{context} in #{@policy_path} has unknown keys: #{unknown_keys.join(', ')}" end |
#validate_import_specs!(import_specs) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 82 def validate_import_specs!(import_specs) duplicate_module = duplicate_name(import_specs.map { |spec| spec[:module_name] }) raise Error, "duplicate import #{duplicate_module} in #{@policy_path}" if duplicate_module duplicate_alias = duplicate_name(([@import_alias] + import_specs.map { |spec| spec[:alias] })) raise Error, "duplicate import alias #{duplicate_alias} in #{@policy_path}" if duplicate_alias end |
#validate_known_raw_names!(names, declarations_by_name, context:) ⇒ Object
609 610 611 612 613 614 615 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 609 def validate_known_raw_names!(names, declarations_by_name, context:) names.each do |name| next if declarations_by_name.key?(name) raise Error, "unknown raw #{context} #{name} in #{@raw_module_name}" end end |
#validate_policy!(policy) ⇒ Object
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 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 15 def validate_policy!(policy) unless policy.is_a?(Hash) raise Error, "imported binding policy #{@policy_path} must be a JSON object" end if policy.key?("extra_source") raise Error, "extra_source in #{@policy_path} is no longer supported; move helper code into a normal .mt module" end validate_allowed_keys!(policy, %w[module_name raw_module_name raw_import_alias imports types constants functions methods], context: "imported binding policy") policy_module_name = policy.fetch("module_name") if policy_module_name != @module_name raise Error, "imported binding policy #{@policy_path} targets #{policy_module_name}, expected #{@module_name}" end policy_raw_module_name = policy.fetch("raw_module_name") if policy_raw_module_name != @raw_module_name raise Error, "imported binding policy #{@policy_path} expects raw module #{policy_raw_module_name}, expected #{@raw_module_name}" end import_alias = policy["raw_import_alias"] return unless import_alias && import_alias != @import_alias raise Error, "imported binding policy #{@policy_path} expects import alias #{import_alias}, expected #{@import_alias}" end |
#validate_raw_module!(raw_ast) ⇒ Object
42 43 44 45 46 |
# File 'lib/milk_tea/bindings/imported_bindings/generator.rb', line 42 def validate_raw_module!(raw_ast) unless raw_ast.module_kind == :raw_module && raw_ast.module_name&.to_s == @raw_module_name raise Error, "expected #{@raw_module_path} to be external file #{@raw_module_name}" end end |