home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / assertion.e < prev    next >
Text File  |  1999-06-05  |  7KB  |  268 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 ASSERTION
  17.    --
  18.    -- To store one assertion.
  19.    --
  20.  
  21. inherit GLOBALS;
  22.  
  23. creation make
  24.  
  25. feature
  26.  
  27.    tag: TAG_NAME;
  28.  
  29.    expression: EXPRESSION;
  30.  
  31.    comment: COMMENT;
  32.  
  33.    current_type: TYPE;
  34.  
  35. feature {NONE}
  36.  
  37.    make(t: like tag; exp: like expression; c: like comment) is
  38.       require
  39.          t /= void or exp /= Void or c /= Void;
  40.       do
  41.          tag := t;
  42.          expression := exp;
  43.          comment := c;
  44.       ensure
  45.          tag = t;
  46.          expression = exp;
  47.          comment = c;
  48.       end;
  49.  
  50. feature
  51.  
  52.    start_position: POSITION is
  53.       do
  54.          if tag /= Void then
  55.             Result := tag.start_position;
  56.          elseif expression /= Void then
  57.             Result := expression.start_position;
  58.          else
  59.             Result := comment.start_position;
  60.          end;
  61.       end;
  62.  
  63.    pretty_print is
  64.       require
  65.          fmt.indent_level >= 1;
  66.       do
  67.          if tag /= Void then
  68.             fmt.put_string(tag.to_string);
  69.             fmt.put_string(": ");
  70.          end;
  71.          if expression /= Void then
  72.             expression.pretty_print;
  73.             if fmt.semi_colon_flag then
  74.                fmt.put_string("; ");
  75.             end;
  76.          end;
  77.          if comment /= Void then
  78.             comment.pretty_print;
  79.          end;
  80.       end;
  81.  
  82.    short(h01,r01,h02,r02,h03,r03,h04,r04,h05,r05,h06,r06,h07,r07,
  83.          h08,r08,h09,r09,h10,r10,h11,r11,h12,r12,h13,r13: STRING) is
  84.       do
  85.          short_print.hook_or(h01,r01);
  86.          if tag = Void then
  87.             short_print.hook_or(h02,r02);
  88.          else
  89.             short_print.hook_or(h03,r03);
  90.             tag.short;
  91.             short_print.hook_or(h04,r04);
  92.          end;
  93.          if expression = Void then
  94.             short_print.hook_or(h05,r05);
  95.          else
  96.             short_print.hook_or(h06,r06);
  97.             expression.short;
  98.             short_print.hook_or(h07,r07);
  99.          end;
  100.          if comment = Void then
  101.             short_print.hook_or(h08,r08);
  102.          else
  103.             short_print.hook_or(h09,r09);
  104.             comment.short(h10,r10,h11,r11);
  105.             short_print.hook_or(h12,r12);
  106.          end;
  107.          short_print.hook_or(h13,r13);
  108.       end;
  109.  
  110.    to_runnable(ct: TYPE; assertion_check_tag: CHARACTER): like Current is
  111.       require
  112.          ct.is_run_type;
  113.       local
  114.          e: like expression;
  115.       do
  116.          if current_type = Void then
  117.             current_type := ct;
  118.             Result := Current;
  119.             if expression /= Void then
  120.                e := expression.to_runnable(ct);
  121.                if e = Void then
  122.                   error(start_position,fz_bad_assertion);
  123.                else
  124.                   expression := e;
  125.                   if not expression.result_type.is_boolean then
  126.                      eh.add_type(expression.result_type,fz_is_not_boolean);
  127.                      error(start_position,fz_bad_assertion);
  128.                   end;
  129.                end;
  130.             end;
  131.             if expression /= Void and then nb_errors = 0 then
  132.                if run_control.require_check then
  133.                   expression.assertion_check(assertion_check_tag);
  134.                end;
  135.             end;
  136.          else
  137.             !!Result.make(tag,expression,comment);
  138.             Result := Result.to_runnable(ct,assertion_check_tag);
  139.          end;
  140.       ensure
  141.          nb_errors = 0 implies Result.is_checked;
  142.       end;
  143.  
  144.    is_checked: BOOLEAN is
  145.       do
  146.          Result := current_type /= Void;
  147.       end;
  148.  
  149.    use_current: BOOLEAN is
  150.       do
  151.          if expression /= Void then
  152.             Result := expression.use_current;
  153.          end;
  154.       end;
  155.  
  156.    afd_check is
  157.       require
  158.          is_checked
  159.       do
  160.          if expression /= Void then
  161.             expression.afd_check;
  162.          end;
  163.       end;
  164.  
  165.    compile_to_c is
  166.       require
  167.          is_checked
  168.       do
  169.          if expression /= Void then
  170.             cpp.check_assertion(expression,tag);
  171.          end;
  172.       end;
  173.  
  174.    is_pre_computable: BOOLEAN is
  175.       do
  176.          if expression = Void then
  177.             Result := true;
  178.          else
  179.             Result := expression.is_pre_computable;
  180.          end;
  181.       end;
  182.  
  183.    is_always_true: BOOLEAN is
  184.       do
  185.          if expression = Void then
  186.             Result := true;
  187.          elseif expression.is_static then
  188.             Result := expression.static_value = 1
  189.          end;
  190.       end;
  191.  
  192.    c_declare_for_old is
  193.       require
  194.          is_checked
  195.       do
  196.          if expression /= Void then
  197.             expression.c_declare_for_old;
  198.          end;
  199.       end;
  200.  
  201.    compile_to_c_old is
  202.       require
  203.          is_checked
  204.       do
  205.          if expression /= Void then
  206.             expression.compile_to_c_old;
  207.          end;
  208.       end;
  209.  
  210.    compile_to_jvm_old is
  211.       require
  212.          is_checked
  213.       do
  214.          if expression /= Void then
  215.             expression.compile_to_jvm_old;
  216.          end;
  217.       end;
  218.  
  219. feature {ASSERTION_LIST}
  220.  
  221.    compile_to_jvm(last_chance: BOOLEAN) is
  222.          -- The boolean result of the expression is pushed.
  223.          -- When `last_chance' is true, the generated code includes an
  224.          -- error message to be printed when assertion is false.
  225.          --
  226.       require
  227.          is_checked
  228.       local
  229.          point1: INTEGER;
  230.          ca: like code_attribute;
  231.       do
  232.          ca := code_attribute;
  233.          if expression = Void then
  234.             ca.opcode_iconst_1;
  235.          else
  236.             expression.compile_to_jvm
  237.             if last_chance then
  238.                point1 := code_attribute.opcode_ifne;
  239.                ca.runtime_error(expression.start_position,Void,fz_50);
  240.                ca.resolve_u2_branch(point1);
  241.                ca.opcode_iconst_1;
  242.             end;
  243.          end;
  244.       end;
  245.  
  246. feature {ASSERTION_LIST}
  247.  
  248.    collect_c_tmp is
  249.       do
  250.          if expression /= Void then
  251.             expression.collect_c_tmp;
  252.          end;
  253.       end;
  254.  
  255. feature {NONE}
  256.  
  257.    tmp_string: STRING is
  258.       once
  259.          !!Result.make(128);
  260.       end;
  261.  
  262. invariant
  263.  
  264.    tag /= Void or expression /= Void or comment /= Void;
  265.  
  266. end -- ASSERTION
  267.  
  268.