(********************************************************************)
(*                                                                  *)
(*  typ_act.s7i   Generate code for actions of the type type.       *)
(*  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: TYP_CMP        is action "TYP_CMP";
const ACTION: TYP_CPY        is action "TYP_CPY";
const ACTION: TYP_EQ         is action "TYP_EQ";
const ACTION: TYP_FUNC       is action "TYP_FUNC";
const ACTION: TYP_HASHCODE   is action "TYP_HASHCODE";
const ACTION: TYP_ISDERIVED  is action "TYP_ISDERIVED";
const ACTION: TYP_ISFUNC     is action "TYP_ISFUNC";
const ACTION: TYP_ISVARFUNC  is action "TYP_ISVARFUNC";
const ACTION: TYP_MATCHOBJ   is action "TYP_MATCHOBJ";
const ACTION: TYP_META       is action "TYP_META";
const ACTION: TYP_NE         is action "TYP_NE";
const ACTION: TYP_NUM        is action "TYP_NUM";
const ACTION: TYP_RESULT     is action "TYP_RESULT";
const ACTION: TYP_STR        is action "TYP_STR";
const ACTION: TYP_VALUE      is action "TYP_VALUE";
const ACTION: TYP_VARCONV    is action "TYP_VARCONV";
const ACTION: TYP_VARFUNC    is action "TYP_VARFUNC";


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

  begin
    declareExtern(c_prog, "intType     typCmp (const const_typeType, const const_typeType);");
    declareExtern(c_prog, "intType     typCmpGeneric (const genericType, const genericType);");
    declareExtern(c_prog, "typeType    typFunc (typeType);");
    declareExtern(c_prog, "boolType    typIsDerived (typeType);");
    declareExtern(c_prog, "boolType    typIsFunc (typeType);");
    declareExtern(c_prog, "boolType    typIsVarfunc (typeType);");
    declareExtern(c_prog, "objRefType  typMatchobj (typeType);");
    declareExtern(c_prog, "typeType    typMeta (typeType);");
    declareExtern(c_prog, "intType     typNum (typeType);");
    declareExtern(c_prog, "typeType    typResult (typeType);");
    declareExtern(c_prog, "striType    typStr (typeType);");
    declareExtern(c_prog, "typeType    typValue (const const_objRefType);");
    declareExtern(c_prog, "typeType    typVarfunc (typeType);");
  end func;


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

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


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

  local
    var expr_type: statement is expr_type.value;
  begin
    process_expr(params[1], statement);
    statement.expr &:= "=";
    process_expr(params[3], statement);
    statement.expr &:= ";\n";
    doLocalDeclsOfStatement(statement, c_expr);
  end func;


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

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


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typFunc(";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (TYP_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 (TYP_ISDERIVED, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typIsDerived(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typIsFunc(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typIsVarfunc(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typMatchobj(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typMeta(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

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


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typNum(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typResult(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


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

  begin
    if category(params[1]) = TYPEOBJECT and not isVar(params[1]) then
      incr(countOptimizations);
      c_expr.expr &:= stringLiteral(str(getValue(params[1], type)));
    else
      compDataLibraryUsed := TRUE;
      prepare_stri_result(c_expr);
      c_expr.result_expr := "typStr(";
      getStdParamToResultExpr(params[1], c_expr);
      c_expr.result_expr &:= ")";
    end if;
  end func;


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

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typValue(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (TYP_VARCONV, 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 (TYP_VARFUNC, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    compDataLibraryUsed := TRUE;
    c_expr.expr &:= "typVarfunc(";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;