+static void
+translate_common (gfc_common_head *common, gfc_symbol *var_list)
+{
+ gfc_symbol *sym;
+ segment_info *s;
+ segment_info *common_segment;
+ HOST_WIDE_INT offset;
+ HOST_WIDE_INT current_offset;
+ unsigned HOST_WIDE_INT align;
+ unsigned HOST_WIDE_INT max_align;
+ bool saw_equiv;
+
+ common_segment = NULL;
+ current_offset = 0;
+ max_align = 1;
+ saw_equiv = false;
+
+ /* Add symbols to the segment. */
+ for (sym = var_list; sym; sym = sym->common_next)
+ {
+ current_segment = common_segment;
+ s = find_segment_info (sym);
+
+ /* Symbol has already been added via an equivalence. Multiple
+ use associations of the same common block result in equiv_built
+ being set but no information about the symbol in the segment. */
+ if (s && sym->equiv_built)
+ {
+ /* Ensure the current location is properly aligned. */
+ align = TYPE_ALIGN_UNIT (s->field);
+ current_offset = (current_offset + align - 1) &~ (align - 1);
+
+ /* Verify that it ended up where we expect it. */
+ if (s->offset != current_offset)
+ {
+ gfc_error ("Equivalence for '%s' does not match ordering of "
+ "COMMON '%s' at %L", sym->name,
+ common->name, &common->where);
+ }
+ }
+ else
+ {
+ /* A symbol we haven't seen before. */
+ s = current_segment = get_segment_info (sym, current_offset);
+
+ /* Add all objects directly or indirectly equivalenced with this
+ symbol. */
+ add_equivalences (&saw_equiv);
+
+ if (current_segment->offset < 0)
+ gfc_error ("The equivalence set for '%s' cause an invalid "
+ "extension to COMMON '%s' at %L", sym->name,
+ common->name, &common->where);
+
+ offset = align_segment (&align);
+
+ if (offset & (max_align - 1))
+ {
+ /* The required offset conflicts with previous alignment
+ requirements. Insert padding immediately before this
+ segment. */
+ gfc_warning ("Padding of %d bytes required before '%s' in "
+ "COMMON '%s' at %L", (int)offset, s->sym->name,
+ common->name, &common->where);
+ }
+ else
+ {
+ /* Offset the whole common block. */
+ apply_segment_offset (common_segment, offset);
+ }
+
+ /* Apply the offset to the new segments. */
+ apply_segment_offset (current_segment, offset);
+ current_offset += offset;
+ if (max_align < align)
+ max_align = align;
+
+ /* Add the new segments to the common block. */
+ common_segment = add_segments (common_segment, current_segment);
+ }