constructor.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Array and structure constructors
  2. Copyright (C) 2009-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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "gfortran.h"
  19. #include "constructor.h"
  20. static void
  21. node_free (splay_tree_value value)
  22. {
  23. gfc_constructor *c = (gfc_constructor*)value;
  24. if (c->expr)
  25. gfc_free_expr (c->expr);
  26. if (c->iterator)
  27. gfc_free_iterator (c->iterator, 1);
  28. mpz_clear (c->offset);
  29. mpz_clear (c->repeat);
  30. free (c);
  31. }
  32. static gfc_constructor *
  33. node_copy (splay_tree_node node, void *base)
  34. {
  35. gfc_constructor *c, *src = (gfc_constructor*)node->value;
  36. c = XCNEW (gfc_constructor);
  37. c->base = (gfc_constructor_base)base;
  38. c->expr = gfc_copy_expr (src->expr);
  39. c->iterator = gfc_copy_iterator (src->iterator);
  40. c->where = src->where;
  41. c->n.component = src->n.component;
  42. mpz_init_set (c->offset, src->offset);
  43. mpz_init_set (c->repeat, src->repeat);
  44. return c;
  45. }
  46. static int
  47. node_copy_and_insert (splay_tree_node node, void *base)
  48. {
  49. int n = mpz_get_si (((gfc_constructor*)node->value)->offset);
  50. gfc_constructor_insert ((gfc_constructor_base*)base,
  51. node_copy (node, base), n);
  52. return 0;
  53. }
  54. gfc_constructor *
  55. gfc_constructor_get (void)
  56. {
  57. gfc_constructor *c = XCNEW (gfc_constructor);
  58. c->base = NULL;
  59. c->expr = NULL;
  60. c->iterator = NULL;
  61. mpz_init_set_si (c->offset, 0);
  62. mpz_init_set_si (c->repeat, 1);
  63. return c;
  64. }
  65. gfc_constructor_base gfc_constructor_get_base (void)
  66. {
  67. return splay_tree_new (splay_tree_compare_ints, NULL, node_free);
  68. }
  69. gfc_constructor_base
  70. gfc_constructor_copy (gfc_constructor_base base)
  71. {
  72. gfc_constructor_base new_base;
  73. if (!base)
  74. return NULL;
  75. new_base = gfc_constructor_get_base ();
  76. splay_tree_foreach (base, node_copy_and_insert, &new_base);
  77. return new_base;
  78. }
  79. void
  80. gfc_constructor_free (gfc_constructor_base base)
  81. {
  82. if (base)
  83. splay_tree_delete (base);
  84. }
  85. gfc_constructor *
  86. gfc_constructor_append (gfc_constructor_base *base, gfc_constructor *c)
  87. {
  88. int offset = 0;
  89. if (*base)
  90. offset = (int)(splay_tree_max (*base)->key) + 1;
  91. return gfc_constructor_insert (base, c, offset);
  92. }
  93. gfc_constructor *
  94. gfc_constructor_append_expr (gfc_constructor_base *base,
  95. gfc_expr *e, locus *where)
  96. {
  97. gfc_constructor *c = gfc_constructor_get ();
  98. c->expr = e;
  99. if (where)
  100. c->where = *where;
  101. return gfc_constructor_append (base, c);
  102. }
  103. gfc_constructor *
  104. gfc_constructor_insert (gfc_constructor_base *base, gfc_constructor *c, int n)
  105. {
  106. splay_tree_node node;
  107. if (*base == NULL)
  108. *base = splay_tree_new (splay_tree_compare_ints, NULL, node_free);
  109. c->base = *base;
  110. mpz_set_si (c->offset, n);
  111. node = splay_tree_insert (*base, (splay_tree_key) n, (splay_tree_value) c);
  112. gcc_assert (node);
  113. return (gfc_constructor*)node->value;
  114. }
  115. gfc_constructor *
  116. gfc_constructor_insert_expr (gfc_constructor_base *base,
  117. gfc_expr *e, locus *where, int n)
  118. {
  119. gfc_constructor *c = gfc_constructor_get ();
  120. c->expr = e;
  121. if (where)
  122. c->where = *where;
  123. return gfc_constructor_insert (base, c, n);
  124. }
  125. gfc_constructor *
  126. gfc_constructor_lookup (gfc_constructor_base base, int offset)
  127. {
  128. gfc_constructor *c;
  129. splay_tree_node node;
  130. if (!base)
  131. return NULL;
  132. node = splay_tree_lookup (base, (splay_tree_key) offset);
  133. if (node)
  134. return (gfc_constructor *) node->value;
  135. /* Check if the previous node has a repeat count big enough to
  136. cover the offset looked for. */
  137. node = splay_tree_predecessor (base, (splay_tree_key) offset);
  138. if (!node)
  139. return NULL;
  140. c = (gfc_constructor *) node->value;
  141. if (mpz_cmp_si (c->repeat, 1) > 0)
  142. {
  143. if (mpz_get_si (c->offset) + mpz_get_si (c->repeat) <= offset)
  144. c = NULL;
  145. }
  146. else
  147. c = NULL;
  148. return c;
  149. }
  150. gfc_expr *
  151. gfc_constructor_lookup_expr (gfc_constructor_base base, int offset)
  152. {
  153. gfc_constructor *c = gfc_constructor_lookup (base, offset);
  154. return c ? c->expr : NULL;
  155. }
  156. int
  157. gfc_constructor_expr_foreach (gfc_constructor *ctor ATTRIBUTE_UNUSED,
  158. int(*f)(gfc_expr *) ATTRIBUTE_UNUSED)
  159. {
  160. gcc_assert (0);
  161. return 0;
  162. }
  163. void
  164. gfc_constructor_swap (gfc_constructor *ctor ATTRIBUTE_UNUSED,
  165. int n ATTRIBUTE_UNUSED, int m ATTRIBUTE_UNUSED)
  166. {
  167. gcc_assert (0);
  168. }
  169. gfc_constructor *
  170. gfc_constructor_first (gfc_constructor_base base)
  171. {
  172. if (base)
  173. {
  174. splay_tree_node node = splay_tree_min (base);
  175. return node ? (gfc_constructor*) node->value : NULL;
  176. }
  177. else
  178. return NULL;
  179. }
  180. gfc_constructor *
  181. gfc_constructor_next (gfc_constructor *ctor)
  182. {
  183. if (ctor)
  184. {
  185. splay_tree_node node = splay_tree_successor (ctor->base,
  186. mpz_get_si (ctor->offset));
  187. return node ? (gfc_constructor*) node->value : NULL;
  188. }
  189. else
  190. return NULL;
  191. }
  192. void
  193. gfc_constructor_remove (gfc_constructor *ctor)
  194. {
  195. if (ctor)
  196. splay_tree_remove (ctor->base, mpz_get_si (ctor->offset));
  197. }
  198. gfc_constructor *
  199. gfc_constructor_lookup_next (gfc_constructor_base base, int offset)
  200. {
  201. splay_tree_node node;
  202. if (!base)
  203. return NULL;
  204. node = splay_tree_successor (base, (splay_tree_key) offset);
  205. if (!node)
  206. return NULL;
  207. return (gfc_constructor *) node->value;
  208. }