(********************************************************************)
(*                                                                  *)
(*  itf_act.s7i   Generate code for actions of interface types.     *)
(*  Copyright (C) 1990 - 1994, 2004 - 2014  Thomas Mertes           *)
(*                                                                  *)
(*  This file is part of the Seed7 compiler.                        *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const ACTION: ITF_CMP           is action "ITF_CMP";
const ACTION: ITF_CONV2         is action "ITF_CONV2";
const ACTION: ITF_CPY           is action "ITF_CPY";
const ACTION: ITF_CPY2          is action "ITF_CPY2";
const ACTION: ITF_EQ            is action "ITF_EQ";
const ACTION: ITF_HASHCODE      is action "ITF_HASHCODE";
const ACTION: ITF_NE            is action "ITF_NE";
const ACTION: ITF_TO_INTERFACE  is action "ITF_TO_INTERFACE";


const proc: itf_prototypes (inout file: c_prog) is func

  begin
    declareExtern(c_prog, "interfaceType itfCreate (const interfaceType);");
    declareExtern(c_prog, "genericType itfCreateGeneric (const genericType);");
  end func;


const proc: process (ITF_CMP, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "ptrCmp(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ITF_CONV2, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ITF_CPY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: statement is expr_type.value;
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    statement.temp_num := c_expr.temp_num;
    prepareAnyParamTemporarys(params[1], c_param1, statement);
    c_param3.demand := ASSIGN_RESULT;
    prepareAnyParamTemporarys(params[3], c_param3, statement);
    statement.temp_decls &:= "interfaceType new_interface;\n";
    statement.expr &:= "new_interface=";
    if c_param3.result_expr <> "" then
      statement.expr &:= c_param3.result_expr;
      statement.expr &:= ";\n";
    else
      statement.expr &:= c_param3.expr;
      statement.expr &:= ";\n";
      statement.expr &:= "if (new_interface->usage_count != 0) {new_interface->usage_count++; }\n";
    end if;
    process_destr_declaration(getExprResultType(params[1]), global_c_expr);
    if isNormalVariable(params[1]) then
      process_destr_call(getExprResultType(params[1]),
          c_param1.expr, statement.expr);
      statement.expr &:= c_param1.expr;
      statement.expr &:= "=new_interface;\n";
    else
      statement.temp_decls &:= "interfaceType *interface_ptr=&(";
      statement.temp_decls &:= c_param1.expr;
      statement.temp_decls &:= ");\n";
      process_destr_call(getExprResultType(params[1]),
          "*interface_ptr", statement.expr);
      statement.expr &:= "*interface_ptr=new_interface;\n";
    end if;
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


const proc: process (ITF_CPY2, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    process(ITF_CPY, function, params, c_expr);
  end func;


const proc: process (ITF_EQ, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ") == (";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ITF_HASHCODE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(intType)(((memSizeType)(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")) >> 6)";
  end func;


const proc: process (ITF_NE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    getAnyParamToExpr(params[1], c_expr);
    c_expr.expr &:= ") != (";
    getAnyParamToExpr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ITF_TO_INTERFACE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var string: result_name is "";
  begin
    prepare_typed_result(resultType(getType(function)), c_expr);
    incr(c_expr.temp_num);
    result_name := "result_" & str(c_expr.temp_num);
    c_expr.temp_decls &:= "interfaceType ";
    c_expr.temp_decls &:= result_name;
    c_expr.temp_decls &:= ";\n";
    c_expr.result_expr &:= "/* ITF_TO_INTERFACE */(";
    c_expr.result_expr &:= result_name;
    c_expr.result_expr &:= "=";
    getAnyParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ",";
    c_expr.result_expr &:= result_name;
    c_expr.result_expr &:= "->usage_count!=0 ? ";
    c_expr.result_expr &:= result_name;
    c_expr.result_expr &:= "->usage_count++ : 0,";
    c_expr.result_expr &:= result_name;
    c_expr.result_expr &:= ")";
  end func;