+
+static void init_index(type *arr) {
+ int init;
+ int dim = 0;
+
+ assert(get_array_n_dimensions(arr) == 1);
+
+ if (has_array_lower_bound(arr, dim))
+ init = get_array_lower_bound_int(arr, 0) -1;
+ else
+ init = get_array_upper_bound_int(arr, 0) +1;
+
+ set_entity_link(get_array_element_entity(arr), (void *)init);
+}
+
+
+static int get_next_index(entity *elem_ent) {
+ type *arr = get_entity_owner(elem_ent);
+ int next;
+ int dim = 0;
+
+ assert(get_array_n_dimensions(arr) == 1);
+
+ if (has_array_lower_bound(arr, dim)) {
+ next = (int)get_entity_link(elem_ent) +1;
+ if (has_array_upper_bound(arr, dim)) {
+ int upper = get_array_upper_bound_int(arr, dim);
+ if (next == upper) next = get_array_lower_bound_int(arr, dim);
+ }
+ } else {
+ next = (int)get_entity_link(elem_ent) -1;
+ if (has_array_lower_bound(arr, dim)) {
+ int upper = get_array_upper_bound_int(arr, dim);
+ if (next == upper) next = get_array_upper_bound_int(arr, dim);
+ }
+ }
+
+ set_entity_link(elem_ent, (void *)next);
+ return next;
+}
+
+/* Compute the array indicees in compound graph paths of initialized entities.
+ *
+ * All arrays must have fixed lower and upper bounds. One array can
+ * have an open bound. If there are several open bounds, we do
+ * nothing. There must be initializer elements for all array
+ * elements. Uses the link field in the array element entities. The
+ * array bounds must be representable as ints.
+ *
+ * (If the bounds are not representable as ints we have to represent
+ * the indicees as firm nodes. But the still we must be able to
+ * evaluate the index against the upper bound.)
+ */
+void compute_compound_ent_array_indicees(entity *ent) {
+ type *tp = get_entity_type(ent);
+ int i, n_vals;
+ entity *unknown_bound_entity = NULL;
+
+ if (!is_compound_type(tp) ||
+ (ent->variability == variability_uninitialized)) return ;
+
+ n_vals = get_compound_ent_n_values(ent);
+ if (n_vals == 0) return;
+
+ /* We can not compute the indicees if there is more than one array
+ with an unknown bound. For this remember the first entity that
+ represents such an array. It could be ent. */
+ if (is_array_type(tp)) {
+ assert(get_array_n_dimensions(tp) == 1 && "other not implemented");
+ int dim = 0;
+ if (!has_array_lower_bound(tp, dim) || !has_array_upper_bound(tp, dim))
+ unknown_bound_entity = ent;
+ }
+
+ /* Initialize the entity links to lower bound -1 and test all path elements
+ for known bounds. */
+ for (i = 0; i < n_vals; ++i) {
+ compound_graph_path *path = get_compound_ent_value_path(ent, i);
+ int j, path_len = get_compound_graph_path_length(path);
+ for (j = 0; j < path_len; ++j) {
+ entity *node = get_compound_graph_path_node(path, j);
+ type *elem_tp = get_entity_type(node);
+
+ if (is_array_type(elem_tp)) {
+ assert(get_array_n_dimensions(elem_tp) == 1 && "other not implemented");
+ int dim = 0;
+ if (!has_array_lower_bound(elem_tp, dim) || !has_array_upper_bound(elem_tp, dim)) {
+ if (!unknown_bound_entity) unknown_bound_entity = node;
+ if (node != unknown_bound_entity) return;
+ }
+
+ init_index(elem_tp);
+ }
+ }
+ }
+
+ /* Finally compute the indicees ... */
+ for (i = 0; i < n_vals; ++i) {
+ compound_graph_path *path = get_compound_ent_value_path(ent, i);
+ int j, path_len = get_compound_graph_path_length(path);
+ for (j = 0; j < path_len; ++j) {
+ entity *node = get_compound_graph_path_node(path, j);
+ type *owner_tp = get_entity_owner(node);
+ if (is_array_type(owner_tp))
+ set_compound_graph_path_array_index (path, j, get_next_index(node));
+ }
+ }
+
+}
+
+