C1x: DR314 proposed wording (WG14 N1237)

Joseph Myers

My aim with DR314 is that it should be possible for an implementation to combine the translation units of a program into a single internal representation and optimize that, unifying structure and union types across translation units where required to be compatible in the process.

Normally, it is possible to create a single translation unit equivalent to multiple translation units, after renaming static variables and functions and structure and union tags. DR314 question 3 is an example where such renaming is not possible. My proposal is to add a requirement that it is possible.

Proposed amendment for C1x:

6.2.7 after paragraph 2 insert: There shall exist a partition of all the structure and union types in the program into disjoint classes such that (a) if two types are in the same class, then they are compatible and (b) whenever two structure or union types are required to be compatible, including by (a), they are in the same class. If there does not exist such a partition, the behavior is undefined.

This is intended to be consistent with all the analysis in N1226.

The precise wording used affects the validity of other related cases. I take the view that unifiability should be possible even when incomplete types aren't involved, and it shouldn't be necessary to unify two incompatible types from the same translation unit (even if they'd be compatible in different translation units), nor to split the uses of a single type within a translation unit into two or more distinct and incompatible types. Thus, the following example would be invalid with my proposed wording.

// TU 1
void
f (void)
{
  struct s { int a; };
  extern struct s a, b;
}

void
g (void)
{
  struct s { int a; };
  extern struct s c, d;
}

// TU 2
void
h (void)
{
  struct s { int a; };
  extern struct s a, c;
}

void
i (void)
{
  struct s { int a; };
  extern struct s b, d;
}