convert.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Data type conversion
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This file contains the functions for converting expressions to
  16. different data types for the translation of the gfortran internal
  17. representation to GIMPLE. The only entry point is `convert'. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "options.h"
  29. #include "wide-int.h"
  30. #include "inchash.h"
  31. #include "tree.h"
  32. #include "fold-const.h"
  33. #include "convert.h"
  34. /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
  35. or validate its data type for a GIMPLE `if' or `while' statement.
  36. The resulting type should always be `boolean_type_node'. */
  37. static tree
  38. truthvalue_conversion (tree expr)
  39. {
  40. switch (TREE_CODE (TREE_TYPE (expr)))
  41. {
  42. case BOOLEAN_TYPE:
  43. if (TREE_TYPE (expr) == boolean_type_node)
  44. return expr;
  45. else if (COMPARISON_CLASS_P (expr))
  46. {
  47. TREE_TYPE (expr) = boolean_type_node;
  48. return expr;
  49. }
  50. else if (TREE_CODE (expr) == NOP_EXPR)
  51. return fold_build1_loc (input_location, NOP_EXPR,
  52. boolean_type_node, TREE_OPERAND (expr, 0));
  53. else
  54. return fold_build1_loc (input_location, NOP_EXPR, boolean_type_node,
  55. expr);
  56. case INTEGER_TYPE:
  57. if (TREE_CODE (expr) == INTEGER_CST)
  58. return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
  59. else
  60. return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
  61. expr, build_int_cst (TREE_TYPE (expr), 0));
  62. default:
  63. gcc_unreachable ();
  64. }
  65. }
  66. /* Create an expression whose value is that of EXPR,
  67. converted to type TYPE. The TREE_TYPE of the value
  68. is always TYPE. This function implements all reasonable
  69. conversions; callers should filter out those that are
  70. not permitted by the language being compiled. */
  71. tree
  72. convert (tree type, tree expr)
  73. {
  74. tree e = expr;
  75. enum tree_code code;
  76. if (type == TREE_TYPE (expr))
  77. return expr;
  78. if (TREE_CODE (type) == ERROR_MARK
  79. || TREE_CODE (expr) == ERROR_MARK
  80. || TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
  81. return expr;
  82. gcc_checking_assert (TREE_CODE (TREE_TYPE (expr)) != VOID_TYPE);
  83. if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (expr)))
  84. return fold_build1_loc (input_location, NOP_EXPR, type, expr);
  85. code = TREE_CODE (type);
  86. if (code == VOID_TYPE)
  87. return fold_build1_loc (input_location, CONVERT_EXPR, type, e);
  88. if (code == BOOLEAN_TYPE)
  89. return fold_build1_loc (input_location, NOP_EXPR, type,
  90. truthvalue_conversion (e));
  91. if (code == INTEGER_TYPE)
  92. return fold (convert_to_integer (type, e));
  93. if (code == POINTER_TYPE || code == REFERENCE_TYPE)
  94. return fold (convert_to_pointer (type, e));
  95. if (code == REAL_TYPE)
  96. return fold (convert_to_real (type, e));
  97. if (code == COMPLEX_TYPE)
  98. return fold (convert_to_complex (type, e));
  99. if (code == VECTOR_TYPE)
  100. return fold (convert_to_vector (type, e));
  101. gcc_unreachable ();
  102. }