External Issue: Support for C unions by c2chapel

17278, "souris-dev", "Support for C unions by c2chapel", "2021-02-26T10:41:38Z"

Currently, the c2chapel tool does not recognize a C union while converting definitions in C headers to Chapel extern definitions.
On experimentation, it was found that C unions can indeed be treated in a similar way as C structs are treated while working with C interoperability.

Hence, this code, where the C union is treated as a struct (example given by @bradcray) runs as expected:

File: testit.h

typedef union _u {
  int x;
  unsigned y;
} u;

extern void setX(int x);
extern int getX(void);
extern unsigned getY(void);

File: testit.c

#include "testit.h"

u myU;

void setX(int x) {
  myU.x = x;
}

int getX() {
  return myU.x;
}

unsigned getY() {
  return myU.y;
}

File: testit.chpl

use SysCTypes; // get definitions of c_int, c_uint                              

require "testit.h", "testit.c"; // C code defining union                        

// reflect C code here                                                          
extern record u {
  var x: c_int;
  var y: c_uint;
}
extern proc setX(x: c_int);
extern proc getX(): c_int;
extern proc getY(): c_uint;

// create and modify union from Chapel:                                         
var myChplU: u;

myChplU.x = -3: c_int;
writeln(myChplU.x);
writeln(myChplU.y);

// create and modfiy C union through routines:                                   
setX(-3);
writeln(getX());
writeln(getY());

As the union shows the expected behavior of a C union (confirmed by experimentation), it should be good to be added as a recognized node in the c2chapel tool.