Class: Idl::FunctionType

Inherits:
Type
  • Object
show all
Defined in:
lib/idlc/type.rb

Constant Summary

Constants inherited from Type

Type::KINDS, Type::QUALIFIERS, Type::TYPE_FROM_KIND

Instance Attribute Summary collapse

Attributes inherited from Type

#kind, #max_width, #qualifiers, #width_ast

Instance Method Summary collapse

Methods inherited from Type

#==, #ary?, #ary_type, #comparable_to?, #const?, #convertable_to?, #default, #enum_class, #equal_to?, from_json_schema, from_typename, #global?, #integral?, #known?, #make_const, #make_const!, #make_global, #make_known, #make_signed, #mutable?, #name, #qualify, #runtime?, #signed?, #sub_type, #to_idl, #to_s, #tuple_types, #width

Constructor Details

#initialize(func_name, func_def_ast, symtab) ⇒ FunctionType

Returns a new instance of FunctionType.



788
789
790
791
792
793
794
# File 'lib/idlc/type.rb', line 788

def initialize(func_name, func_def_ast, symtab)
  super(:function, name: func_name)
  @func_def_ast = func_def_ast
  @symtab = symtab

  raise "symtab should be at level 1" unless symtab.levels == 1
end

Instance Attribute Details

#func_def_astObject (readonly)

Returns the value of attribute func_def_ast.



778
779
780
# File 'lib/idlc/type.rb', line 778

def func_def_ast
  @func_def_ast
end

Instance Method Details

#apply_arguments(symtab, argument_nodes, call_site_symtab, func_call_ast) ⇒ Object



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
# File 'lib/idlc/type.rb', line 827

def apply_arguments(symtab, argument_nodes, call_site_symtab, func_call_ast)
  idx = 0
  values = []
  @func_def_ast.arguments(symtab).each do |atype, aname|
    func_call_ast.type_error "Missing argument #{idx}" if idx >= argument_nodes.size
    value_result = Idl::AstNode.value_try do
      value = argument_nodes.fetch(idx).value(call_site_symtab)
      symtab.add(aname, Var.new(aname, atype, value))
      values << value
    end
    Idl::AstNode.value_else(value_result) do
      symtab.add(aname, Var.new(aname, atype))
      values << :unknown
    end
    idx += 1
  end
  values
end

#argument_name(index, func_call_ast) ⇒ Object



945
946
947
948
949
950
951
952
953
954
955
956
957
958
# File 'lib/idlc/type.rb', line 945

def argument_name(index, func_call_ast)
  return nil if index >= @func_def_ast.num_args

  symtab = @symtab.global_clone
  symtab.push(func_call_ast)

  begin
    arguments = @func_def_ast.arguments(symtab)
  ensure
    symtab.pop
    symtab.release
  end
  arguments[index][1]
end

#argument_nodesObject



796
# File 'lib/idlc/type.rb', line 796

def argument_nodes = @func_def_ast.argument_nodes

#argument_type(index, argument_nodes, call_site_symtab, func_call_ast) ⇒ Object



924
925
926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/idlc/type.rb', line 924

def argument_type(index, argument_nodes, call_site_symtab, func_call_ast)
  return nil if index >= @func_def_ast.num_args

  symtab = @symtab.global_clone
  symtab.push(func_call_ast)

  begin
    arguments = @func_def_ast.arguments(symtab)
  ensure
    symtab.pop
    symtab.release
  end
  arguments[index][0]
end

#argument_values(symtab, argument_nodes, call_site_symtab, func_call_ast) ⇒ Object



856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/idlc/type.rb', line 856

def argument_values(symtab, argument_nodes, call_site_symtab, func_call_ast)
  idx = 0
  values = []
  @func_def_ast.arguments(symtab).each do |atype, aname|
    func_call_ast.type_error "Missing argument #{idx}" if idx >= argument_nodes.size
    value_result = Idl::AstNode.value_try do
      values << argument_nodes.fetch(idx).value(call_site_symtab)
    end
    Idl::AstNode.value_else(value_result) do
      return nil
    end
    idx += 1
  end
  values
end

#bodyObject



963
# File 'lib/idlc/type.rb', line 963

def body = @func_def_ast.body

#builtin?Boolean

Returns:

  • (Boolean)


806
# File 'lib/idlc/type.rb', line 806

def builtin? = @func_def_ast.builtin?

#cloneObject



801
802
803
# File 'lib/idlc/type.rb', line 801

def clone
  FunctionType.new(name, @func_def_ast, @symtab)
end

#external?Boolean

Returns:

  • (Boolean)


812
# File 'lib/idlc/type.rb', line 812

def external? = @func_def_ast.external?

#generated?Boolean

Returns:

  • (Boolean)


809
# File 'lib/idlc/type.rb', line 809

def generated? = @func_def_ast.generated?

#num_argsObject



815
# File 'lib/idlc/type.rb', line 815

def num_args = @func_def_ast.num_args

#return_type(argument_nodes, func_call_ast) ⇒ Object



880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/idlc/type.rb', line 880

def return_type(argument_nodes, func_call_ast)
  rtype =
    begin
      symtab = @symtab.global_clone
      symtab.push(func_call_ast)
      @func_def_ast.return_type(symtab)
    ensure
      symtab.pop
      symtab.release
    end

  T.must(rtype)
end

#return_value(argument_nodes, call_site_symtab, func_call_ast) ⇒ Object



901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/idlc/type.rb', line 901

def return_value(argument_nodes, call_site_symtab, func_call_ast)
  symtab = @symtab.global_clone
  symtab.push(func_call_ast)
  apply_arguments(symtab, argument_nodes, call_site_symtab, func_call_ast)

  begin
    value = @func_def_ast.body.return_value(symtab)
  ensure
    symtab.pop
    symtab.release
  end
  raise "?" if value.is_a?(SymbolTable)
  value
end