home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / cp_info.e < prev    next >
Text File  |  1999-06-05  |  8KB  |  356 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr
  4. --                       http://SmallEiffel.loria.fr
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License
  11. -- for  more  details.  You  should  have  received a copy of the GNU General
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class CP_INFO
  17.    --
  18.    -- Print a human readable version of a JVM *.class generated
  19.    -- by SmallEiffel.
  20.    --
  21.  
  22. inherit CP_INFO_TAGS;
  23.  
  24. creation clear
  25.  
  26. feature {PRINT_JVM_CLASS}
  27.  
  28.    tag: CHARACTER; -- Must be one defined in CP_INFO_TAGS.
  29.  
  30.    info: STRING; -- Contains the corresponding information.
  31.  
  32. feature {CONSTANT_POOL}
  33.  
  34.    clear is
  35.       do
  36.          tag := Constant_empty;
  37.          if info = Void then
  38.             !!info.make(4);
  39.          else
  40.             info.clear;
  41.          end;
  42.       end;
  43.  
  44.    is_tagged(tag_value: CHARACTER): BOOLEAN is
  45.       do
  46.          Result := tag = tag_value;
  47.       end;
  48.  
  49. feature
  50.  
  51.    set_class(i: STRING) is
  52.       require
  53.          i.count = 2
  54.       do
  55.          tag := Constant_class;
  56.          info.copy(i);
  57.       end;
  58.  
  59.    set_fieldref(i: STRING) is
  60.       require
  61.          i.count = 4
  62.       do
  63.          tag := Constant_fieldref;
  64.          info.copy(i);
  65.       end;
  66.  
  67.    set_methodref(i: STRING) is
  68.       require
  69.          i.count = 4
  70.       do
  71.          tag := Constant_methodref;
  72.          info.copy(i);
  73.       end;
  74.  
  75.    set_interface_methodref(i: STRING) is
  76.       require
  77.          i.count = 4
  78.       do
  79.          tag := Constant_interfacemethodref;
  80.          info.copy(i);
  81.       end;
  82.  
  83.    set_string(str: STRING) is
  84.       require
  85.          str.count >= 2
  86.       local
  87.          i: INTEGER;
  88.          c: CHARACTER;
  89.       do
  90.          tag := Constant_string;
  91.          info.clear;
  92.          info.extend(str.item(1));
  93.          info.extend(str.item(2));
  94.          from
  95.             i := 3;
  96.          until
  97.             i > str.count
  98.          loop
  99.             c := str.item(i);
  100.             if c = '%U' then
  101.                info.extend('%/192/');
  102.                info.extend('%/128/');
  103.             else
  104.                info.extend(c);
  105.             end;
  106.             i := i + 1;
  107.          end;
  108.       end;
  109.  
  110.    set_integer(i: STRING) is
  111.       require
  112.          i.count = 4
  113.       do
  114.          tag := Constant_integer;
  115.          info.copy(i);
  116.       end;
  117.  
  118.    set_float(i: STRING) is
  119.       require
  120.          i.count = 4
  121.       do
  122.          tag := Constant_float;
  123.          info.copy(i);
  124.       end;
  125.  
  126.    set_long(i: STRING) is
  127.       require
  128.          i.count = 8
  129.       do
  130.          tag := Constant_long;
  131.          info.copy(i);
  132.       end;
  133.  
  134.    set_double(i: STRING) is
  135.       require
  136.          i.count = 8
  137.       do
  138.          tag := Constant_double;
  139.          info.copy(i);
  140.       end;
  141.  
  142.    set_name_and_type(i: STRING) is
  143.       require
  144.          i.count = 4
  145.       do
  146.          tag := Constant_name_and_type;
  147.          info.copy(i);
  148.       end;
  149.  
  150.    set_utf8(i: STRING) is
  151.       require
  152.          i.count >= 2
  153.       do
  154.          tag := Constant_utf8;
  155.          info.copy(i);
  156.       ensure
  157.          info.count = u2_to_integer(1) + 2
  158.       end;
  159.  
  160. feature -- Testing :
  161.  
  162.    is_class: BOOLEAN is
  163.       do
  164.          Result := tag = Constant_class;
  165.       end;
  166.  
  167.    is_fieldref: BOOLEAN is
  168.       do
  169.          Result := tag = Constant_fieldref;
  170.       end;
  171.  
  172.    is_methodref: BOOLEAN is
  173.       do
  174.          Result := tag = Constant_methodref;
  175.       end;
  176.  
  177.    is_interface_methodref: BOOLEAN is
  178.       do
  179.          Result := tag = Constant_interfacemethodref;
  180.       end;
  181.  
  182.    is_string: BOOLEAN is
  183.       do
  184.          Result := tag = Constant_string;
  185.       end;
  186.  
  187.    is_integer: BOOLEAN is
  188.       do
  189.          Result := tag = Constant_integer;
  190.       end;
  191.  
  192.    is_float: BOOLEAN is
  193.       do
  194.          Result := tag = Constant_float;
  195.       end;
  196.  
  197.    is_long: BOOLEAN is
  198.       do
  199.          Result := tag = Constant_long;
  200.       end;
  201.  
  202.    is_double: BOOLEAN is
  203.       do
  204.          Result := tag = Constant_double;
  205.       end;
  206.  
  207.    is_name_and_type: BOOLEAN is
  208.       do
  209.          Result := tag = Constant_name_and_type;
  210.       end;
  211.  
  212.    is_utf8: BOOLEAN is
  213.       do
  214.          Result := tag = Constant_utf8;
  215.       end;
  216.  
  217. feature
  218.  
  219.    view_in(str: STRING) is
  220.          -- Append in `str' a human readable version.
  221.          -- Note: assume `constant_pool' is checked.
  222.       local
  223.          idx, length, i: INTEGER;
  224.       do
  225.          inspect
  226.             tag
  227.          when Constant_class then
  228.             idx := u2_to_integer(1);
  229.             constant_pool.view_in(str,idx);
  230.          when Constant_fieldref then
  231.             idx := u2_to_integer(1);
  232.             constant_pool.view_in(str,idx);
  233.             str.extend('.');
  234.             idx := u2_to_integer(3);
  235.             constant_pool.view_in(str,idx);
  236.          when Constant_methodref then
  237.             idx := u2_to_integer(1);
  238.             constant_pool.view_in(str,idx);
  239.             str.extend('.');
  240.             idx := u2_to_integer(3);
  241.             constant_pool.view_in(str,idx);
  242.          when Constant_interfacemethodref then
  243.          when Constant_string then
  244.             idx := u2_to_integer(1);
  245.             constant_pool.view_in(str,idx);
  246.          when Constant_integer then
  247.          when Constant_float then
  248.          when Constant_long then
  249.          when Constant_double then
  250.          when Constant_name_and_type then
  251.             idx := u2_to_integer(1);
  252.             constant_pool.view_in(str,idx);
  253.             str.extend(':');
  254.             idx := u2_to_integer(3);
  255.             constant_pool.view_in(str,idx);
  256.          when Constant_utf8 then
  257.             from
  258.                length := u2_to_integer(1);
  259.                i := 3;
  260.             until
  261.                length = 0
  262.             loop
  263.                str.extend(info.item(i));
  264.                i := i + 1;
  265.                length := length - 1;
  266.             end;
  267.          end;
  268.       end;
  269.  
  270. feature {CONSTANT_POOL}
  271.  
  272.    b_put is
  273.       do
  274.          jvm.b_put_u1(tag)
  275.          jvm.b_put_byte_string(info);
  276.       end;
  277.  
  278. feature {CONSTANT_POOL} -- Update and search :
  279.    -- *** ACOMPLETER AU FUR ET A MESURE ***
  280.  
  281.    is_class_idx(utf8: INTEGER): BOOLEAN is
  282.       do
  283.          if Constant_class = tag then
  284.             Result := u2_to_integer(1) = utf8;
  285.          end;
  286.       end;
  287.  
  288.    is_fieldref_idx(c, nt: INTEGER): BOOLEAN is
  289.       do
  290.          if Constant_fieldref = tag then
  291.             if u2_to_integer(1) = c then
  292.                Result := u2_to_integer(3) = nt;
  293.             end;
  294.          end;
  295.       end;
  296.  
  297.    is_methodref_idx(c, nt: INTEGER): BOOLEAN is
  298.       do
  299.          if Constant_methodref = tag then
  300.             if u2_to_integer(1) = c then
  301.                Result := u2_to_integer(3) = nt;
  302.             end;
  303.          end;
  304.       end;
  305.  
  306.    is_name_and_type_idx(n, d: INTEGER): BOOLEAN is
  307.       do
  308.          if Constant_name_and_type = tag then
  309.             if u2_to_integer(1) = n then
  310.                Result := u2_to_integer(3) = d;
  311.             end;
  312.          end;
  313.       end;
  314.  
  315.    is_string_idx(utf8: INTEGER): BOOLEAN is
  316.       do
  317.          if Constant_string = tag then
  318.             Result := u2_to_integer(1) = utf8;
  319.          end;
  320.       end;
  321.  
  322.    is_utf8_idx(contents: STRING): BOOLEAN is
  323.       local
  324.          i1, i2: INTEGER;
  325.       do
  326.          if Constant_utf8 = tag then
  327.             if u2_to_integer(1) = contents.count then
  328.                from
  329.                   i1 := contents.count + 1;
  330.                   i2 := info.count + 1;
  331.                   check
  332.                      i1 + 2 = i2
  333.                   end;
  334.                   Result := true;
  335.                until
  336.                   not Result or else i1 = 1
  337.                loop
  338.                   i1 := i1 - 1;
  339.                   i2 := i2 - 1;
  340.                   Result := contents.item(i1) = info.item(i2);
  341.                end;
  342.             end;
  343.          end;
  344.       end;
  345.  
  346. feature {NONE}
  347.  
  348.    u2_to_integer(i: INTEGER): INTEGER is
  349.       do
  350.          Result := info.item(i).code * 256;
  351.          Result := Result + info.item(i + 1).code;
  352.       end;
  353.  
  354. end -- CP_INFO
  355.  
  356.