You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`getsockname`, and the rest of the socket functions that receive a
`struct sockaddr*` (`__SOCKADDR_ARG`) parameter, are defined differently
in C and C++:
```c
int getsockname (int fd, __SOCKADDR_ARG addr, socklen_t *len)
#if defined __cplusplus
#define __SOCKADDR_ARG struct sockaddr *__restrict
#else
typedef union {
struct sockaddr *__restrict __sockaddr__;
struct sockaddr_in *__restrict __sockaddr_in__;
} __SOCKADDR_ARG __attribute__ ((__transparent_union__));
#endif
```
`__transparent_union__` is an attribute that allows a function whose
parameter has that union type can be called with any of the union's arm
types directly, without a cast:
```c
struct sockaddr_in in;
// this would normally generate an error because &in has type
// sockaddr_in* but the argument is declared sockaddr*
getsockname(fd, &in, len);
struct sockaddr_in6 in6;
getsockname(fd, &in6, len); // same tension between sockaddr_in6* and sockaddr_in*
```
`__transparent_union` is only available in C. In C++, an explicit cast
is required.
In the Clang AST, the transparent union object is created through a
`CompoundLiteralExpression(InitListExpr(struct sockaddr_in *))`. We
convert the argument of CompoundLiteralExpression in order to get the
correct rust code.
0 commit comments