cvt.c

来自「GCC编译器源代码」· C语言 代码 · 共 1,673 行 · 第 1/4 页

C
1,673
字号
/* Language-level data type conversion for GNU C++.   Copyright (C) 1987, 88, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.   Hacked by Michael Tiemann (tiemann@cygnus.com)This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING.  If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA.  *//* This file contains the functions for converting C expressions   to different data types.  The only entry point is `convert'.   Every language front end must have a `convert' function   but what kind of conversions it does will depend on the language.  */#include "config.h"#include <stdio.h>#include "tree.h"#include "flags.h"#include "cp-tree.h"#include "class.h"#include "convert.h"#ifdef HAVE_STDLIB_H#include <stdlib.h>#endifextern tree static_aggregates;static tree build_thunk PROTO((tree, tree));static tree convert_fn_ptr PROTO((tree, tree));static tree cp_convert_to_pointer PROTO((tree, tree));static tree convert_to_pointer_force PROTO((tree, tree));static tree build_up_reference PROTO((tree, tree, int, int));static tree build_type_conversion_1 PROTO((tree, tree, tree, tree,					   int));/* Change of width--truncation and extension of integers or reals--   is represented with NOP_EXPR.  Proper functioning of many things   assumes that no other conversions can be NOP_EXPRs.   Conversion between integer and pointer is represented with CONVERT_EXPR.   Converting integer to real uses FLOAT_EXPR   and real to integer uses FIX_TRUNC_EXPR.   Here is a list of all the functions that assume that widening and   narrowing is always done with a NOP_EXPR:     In convert.c, convert_to_integer.     In c-typeck.c, build_binary_op_nodefault (boolean ops),        and truthvalue_conversion.     In expr.c: expand_expr, for operands of a MULT_EXPR.     In fold-const.c: fold.     In tree.c: get_narrower and get_unwidened.   C++: in multiple-inheritance, converting between pointers may involve   adjusting them by a delta stored within the class definition.  *//* Subroutines of `convert'.  *//* Build a thunk.  What it is, is an entry point that when called will   adjust the this pointer (the first argument) by offset, and then   goto the real address of the function given by REAL_ADDR that we   would like called.  What we return is the address of the thunk.  */static treebuild_thunk (offset, real_addr)     tree offset, real_addr;{  if (TREE_CODE (real_addr) != ADDR_EXPR      || TREE_CODE (TREE_OPERAND (real_addr, 0)) != FUNCTION_DECL)    {      sorry ("MI pointer to member conversion too complex");      return error_mark_node;    }  sorry ("MI pointer to member conversion too complex");  return error_mark_node;}/* Convert a `pointer to member' (POINTER_TYPE to METHOD_TYPE) into   another `pointer to method'.  This may involved the creation of   a thunk to handle the this offset calculation.  */static treeconvert_fn_ptr (type, expr)     tree type, expr;{#if 0				/* We don't use thunks for pmfs.  */  if (flag_vtable_thunks)    {      tree intype = TREE_TYPE (expr);      tree binfo = get_binfo (TYPE_METHOD_BASETYPE (TREE_TYPE (intype)),			      TYPE_METHOD_BASETYPE (TREE_TYPE (type)), 1);      if (binfo == error_mark_node)	{	  error ("  in pointer to member conversion");	  return error_mark_node;	}      if (binfo == NULL_TREE)	{	  /* ARM 4.8 restriction.  */	  error ("invalid pointer to member conversion");	  return error_mark_node;	}      if (BINFO_OFFSET_ZEROP (binfo))	return build1 (NOP_EXPR, type, expr);      return build1 (NOP_EXPR, type, build_thunk (BINFO_OFFSET (binfo), expr));    }  else#endif    return build_ptrmemfunc (type, expr, 1);}/* if converting pointer to pointer     if dealing with classes, check for derived->base or vice versa     else if dealing with method pointers, delegate     else convert blindly   else if converting class, pass off to build_type_conversion   else try C-style pointer conversion  */static treecp_convert_to_pointer (type, expr)     tree type, expr;{  register tree intype = TREE_TYPE (expr);  register enum tree_code form;  if (IS_AGGR_TYPE (intype))    {      tree rval;      intype = complete_type (intype);      if (TYPE_SIZE (intype) == NULL_TREE)	{	  cp_error ("can't convert from incomplete type `%T' to `%T'",		    intype, type);	  return error_mark_node;	}      rval = build_type_conversion (CONVERT_EXPR, type, expr, 1);      if (rval)	{	  if (rval == error_mark_node)	    cp_error ("conversion of `%E' from `%T' to `%T' is ambiguous",		      expr, intype, type);	  return rval;	}    }  if (TYPE_PTRMEMFUNC_P (type))    type = TYPE_PTRMEMFUNC_FN_TYPE (type);  /* Handle anachronistic conversions from (::*)() to cv void* or (*)().  */  if (TREE_CODE (type) == POINTER_TYPE      && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE	  || TYPE_MAIN_VARIANT (TREE_TYPE (type)) == void_type_node))    {      /* Allow an implicit this pointer for pointer to member	 functions.  */      if (TYPE_PTRMEMFUNC_P (intype))	{	  tree decl, basebinfo;	  tree fntype = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (intype));	  tree t = TYPE_METHOD_BASETYPE (fntype);	  if (current_class_type == 0	      || get_base_distance (t, current_class_type, 0, &basebinfo)	      == -1)	    {	      decl = build1 (NOP_EXPR, t, error_mark_node);	    }	  else if (current_class_ptr == 0)	    decl = build1 (NOP_EXPR, t, error_mark_node);	  else	    decl = current_class_ref;	  expr = build (OFFSET_REF, fntype, decl, expr);	}      if (TREE_CODE (expr) == OFFSET_REF	  && TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)	expr = resolve_offset_ref (expr);      if (TREE_CODE (TREE_TYPE (expr)) == METHOD_TYPE)	expr = build_addr_func (expr);      if (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)	{	  if (TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == METHOD_TYPE)	    if (pedantic || warn_pmf2ptr)	      cp_pedwarn ("converting from `%T' to `%T'", TREE_TYPE (expr),			  type);	  return build1 (NOP_EXPR, type, expr);	}      intype = TREE_TYPE (expr);    }  if (TYPE_PTRMEMFUNC_P (intype))    intype = TYPE_PTRMEMFUNC_FN_TYPE (intype);  form = TREE_CODE (intype);  if (form == POINTER_TYPE || form == REFERENCE_TYPE)    {      intype = TYPE_MAIN_VARIANT (intype);      if (TYPE_MAIN_VARIANT (type) != intype	  && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE	  && IS_AGGR_TYPE (TREE_TYPE (type))	  && IS_AGGR_TYPE (TREE_TYPE (intype))	  && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)	{	  enum tree_code code = PLUS_EXPR;	  tree binfo = get_binfo (TREE_TYPE (type), TREE_TYPE (intype), 1);	  if (binfo == error_mark_node)	    return error_mark_node;	  if (binfo == NULL_TREE)	    {	      binfo = get_binfo (TREE_TYPE (intype), TREE_TYPE (type), 1);	      if (binfo == error_mark_node)		return error_mark_node;	      code = MINUS_EXPR;	    }	  if (binfo)	    {	      if (TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (type))		  || TYPE_USES_VIRTUAL_BASECLASSES (TREE_TYPE (intype))		  || ! BINFO_OFFSET_ZEROP (binfo))		{		  /* Need to get the path we took.  */		  tree path;		  if (code == PLUS_EXPR)		    get_base_distance (TREE_TYPE (type), TREE_TYPE (intype), 0, &path);		  else		    get_base_distance (TREE_TYPE (intype), TREE_TYPE (type), 0, &path);		  return build_vbase_path (code, type, expr, path, 0);		}	    }	}      if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE	  && TREE_CODE (type) == POINTER_TYPE	  && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)	return convert_fn_ptr (type, expr);      if (TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE	  && TREE_CODE (TREE_TYPE (intype)) == OFFSET_TYPE)	{	  tree b1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (type));	  tree b2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (intype));	  tree binfo = get_binfo (b1, b2, 1);	  if (binfo == NULL_TREE)	    binfo = get_binfo (b2, b1, 1);	  if (binfo == error_mark_node)	    return error_mark_node;	}      if (TREE_CODE (TREE_TYPE (intype)) == METHOD_TYPE	  || (TREE_CODE (type) == POINTER_TYPE	      && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE))	{	  cp_error ("cannot convert `%E' from type `%T' to type `%T'",		    expr, intype, type);	  return error_mark_node;	}      return build1 (NOP_EXPR, type, expr);    }  my_friendly_assert (form != OFFSET_TYPE, 186);  if (TYPE_LANG_SPECIFIC (intype)      && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))    return convert_to_pointer (type, build_optr_ref (expr));  if (integer_zerop (expr))    {      if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)	return build_ptrmemfunc (type, expr, 0);      expr = build_int_2 (0, 0);      TREE_TYPE (expr) = type;      return expr;    }  if (INTEGRAL_CODE_P (form))    {      if (type_precision (intype) == POINTER_SIZE)	return build1 (CONVERT_EXPR, type, expr);      expr = cp_convert (type_for_size (POINTER_SIZE, 0), expr);      /* Modes may be different but sizes should be the same.  */      if (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (expr)))	  != GET_MODE_SIZE (TYPE_MODE (type)))	/* There is supposed to be some integral type	   that is the same width as a pointer.  */	abort ();      return convert_to_pointer (type, expr);    }  cp_error ("cannot convert `%E' from type `%T' to type `%T'",	    expr, intype, type);  return error_mark_node;}/* Like convert, except permit conversions to take place which   are not normally allowed due to access restrictions   (such as conversion from sub-type to private super-type).  */static treeconvert_to_pointer_force (type, expr)     tree type, expr;{  register tree intype = TREE_TYPE (expr);  register enum tree_code form = TREE_CODE (intype);    if (integer_zerop (expr))    {      expr = build_int_2 (0, 0);      TREE_TYPE (expr) = type;      return expr;    }  /* Convert signature pointer/reference to `void *' first.  */  if (form == RECORD_TYPE      && (IS_SIGNATURE_POINTER (intype) || IS_SIGNATURE_REFERENCE (intype)))    {      expr = build_optr_ref (expr);      intype = TREE_TYPE (expr);      form = TREE_CODE (intype);    }  if (form == POINTER_TYPE)    {      intype = TYPE_MAIN_VARIANT (intype);      if (TYPE_MAIN_VARIANT (type) != intype	  && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE	  && IS_AGGR_TYPE (TREE_TYPE (type))	  && IS_AGGR_TYPE (TREE_TYPE (intype))	  && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)	{	  enum tree_code code = PLUS_EXPR;	  tree path;	  int distance = get_base_distance (TREE_TYPE (type),					    TREE_TYPE (intype), 0, &path);	  if (distance == -2)	    {	    ambig:	      cp_error ("type `%T' is ambiguous baseclass of `%s'",			TREE_TYPE (type),			TYPE_NAME_STRING (TREE_TYPE (intype)));	      return error_mark_node;	    }	  if (distance == -1)	    {	      distance = get_base_distance (TREE_TYPE (intype),					    TREE_TYPE (type), 0, &path);	      if (distance == -2)		goto ambig;	      if (distance < 0)		/* Doesn't need any special help from us.  */		return build1 (NOP_EXPR, type, expr);	      code = MINUS_EXPR;	    }	  return build_vbase_path (code, type, expr, path, 0);	}    }  return cp_convert_to_pointer (type, expr);}/* We are passing something to a function which requires a reference.   The type we are interested in is in TYPE. The initial   value we have to begin with is in ARG.   FLAGS controls how we manage access checking.   DIRECT_BIND in FLAGS controls how any temporaries are generated.  */static treebuild_up_reference (type, arg, flags, checkconst)     tree type, arg;     int flags, checkconst;{  tree rval;  tree argtype = TREE_TYPE (arg);  tree target_type = TREE_TYPE (type);  my_friendly_assert (TREE_CODE (type) == REFERENCE_TYPE, 187);  if ((flags & DIRECT_BIND) && ! real_lvalue_p (arg))    {      tree targ = arg;      if (toplevel_bindings_p ())	arg = get_temp_name (argtype, 1);      else	{	  arg = pushdecl (build_decl (VAR_DECL, NULL_TREE, argtype));	  DECL_ARTIFICIAL (arg) = 1;	}      DECL_INITIAL (arg) = targ;      cp_finish_decl (arg, targ, NULL_TREE, 0, LOOKUP_ONLYCONVERTING);    }  else if (!(flags & DIRECT_BIND) && ! lvalue_p (arg))    {      tree slot = build_decl (VAR_DECL, NULL_TREE, argtype);      arg = build (TARGET_EXPR, argtype, slot, arg, NULL_TREE, NULL_TREE);    }  /* If we had a way to wrap this up, and say, if we ever needed it's

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?