c-semantics.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* This file contains subroutine used by the C front-end to construct GENERIC.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Written by Benjamin Chelf (chelf@codesourcery.com).
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "hard-reg-set.h"
  32. #include "function.h"
  33. #include "splay-tree.h"
  34. #include "c-common.h"
  35. #include "flags.h"
  36. #include "tree-iterator.h"
  37. /* Create an empty statement tree rooted at T. */
  38. tree
  39. push_stmt_list (void)
  40. {
  41. tree t;
  42. t = alloc_stmt_list ();
  43. vec_safe_push (stmt_list_stack, t);
  44. return t;
  45. }
  46. /* Finish the statement tree rooted at T. */
  47. tree
  48. pop_stmt_list (tree t)
  49. {
  50. tree u = NULL_TREE;
  51. /* Pop statement lists until we reach the target level. The extra
  52. nestings will be due to outstanding cleanups. */
  53. while (1)
  54. {
  55. u = stmt_list_stack->pop ();
  56. if (!stmt_list_stack->is_empty ())
  57. {
  58. tree x = stmt_list_stack->last ();
  59. STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
  60. }
  61. if (t == u)
  62. break;
  63. }
  64. gcc_assert (u != NULL_TREE);
  65. /* If the statement list is completely empty, just return it. This is
  66. just as good small as build_empty_stmt, with the advantage that
  67. statement lists are merged when they appended to one another. So
  68. using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
  69. statements. */
  70. if (TREE_SIDE_EFFECTS (t))
  71. {
  72. tree_stmt_iterator i = tsi_start (t);
  73. /* If the statement list contained exactly one statement, then
  74. extract it immediately. */
  75. if (tsi_one_before_end_p (i))
  76. {
  77. u = tsi_stmt (i);
  78. tsi_delink (&i);
  79. free_stmt_list (t);
  80. t = u;
  81. }
  82. }
  83. return t;
  84. }
  85. /* Build a generic statement based on the given type of node and
  86. arguments. Similar to `build_nt', except that we set
  87. EXPR_LOCATION to LOC. */
  88. /* ??? This should be obsolete with the lineno_stmt productions
  89. in the grammar. */
  90. tree
  91. build_stmt (location_t loc, enum tree_code code, ...)
  92. {
  93. tree ret;
  94. int length, i;
  95. va_list p;
  96. bool side_effects;
  97. /* This function cannot be used to construct variably-sized nodes. */
  98. gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
  99. va_start (p, code);
  100. ret = make_node (code);
  101. TREE_TYPE (ret) = void_type_node;
  102. length = TREE_CODE_LENGTH (code);
  103. SET_EXPR_LOCATION (ret, loc);
  104. /* TREE_SIDE_EFFECTS will already be set for statements with
  105. implicit side effects. Here we make sure it is set for other
  106. expressions by checking whether the parameters have side
  107. effects. */
  108. side_effects = false;
  109. for (i = 0; i < length; i++)
  110. {
  111. tree t = va_arg (p, tree);
  112. if (t && !TYPE_P (t))
  113. side_effects |= TREE_SIDE_EFFECTS (t);
  114. TREE_OPERAND (ret, i) = t;
  115. }
  116. TREE_SIDE_EFFECTS (ret) |= side_effects;
  117. va_end (p);
  118. return ret;
  119. }
  120. /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
  121. tree
  122. build_real_imag_expr (location_t location, enum tree_code code, tree arg)
  123. {
  124. tree ret;
  125. tree arg_type = TREE_TYPE (arg);
  126. gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
  127. if (TREE_CODE (arg_type) == COMPLEX_TYPE)
  128. {
  129. ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
  130. SET_EXPR_LOCATION (ret, location);
  131. }
  132. else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
  133. {
  134. ret = (code == REALPART_EXPR
  135. ? arg
  136. : omit_one_operand_loc (location, arg_type,
  137. integer_zero_node, arg));
  138. }
  139. else
  140. {
  141. error_at (location, "wrong type argument to %s",
  142. code == REALPART_EXPR ? "__real" : "__imag");
  143. ret = error_mark_node;
  144. }
  145. return ret;
  146. }