<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141506>141506</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang][enhancement] operator overload attribute for C
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
olemayu
</td>
</tr>
</table>
<pre>
Simple and elegant. Just like C itself.
```c
typedef struct vec3 {
float x, y, z;
} vec3;
[[clang::operator(*)]]
inline vec3 v3v3_mul(vec3 lhs, vec3 rhs) {
return (vec3) { lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z };
}
[[clang::operator(*)]]
inline vec3 v3v1_mul(vec3 lhs, float rhs) {
return (vec3) { lhs.x * rhs, lhs.y * rhs, lhs.z * rhs };
}
// lhs/self expected to be a pointer for assignment operators
[[clang::operator(*=)]]
inline vec3 v3v3_smul(vec3* self, vec3 rhs) {
self->x *= rhs.x;
self->y *= rhs.y;
self->z *= rhs.z;
return *self;
}
[[clang::operator(*=)]]
inline vec3 v3v1_smul(vec3* self, float rhs) {
self->x *= rhs;
self->y *= rhs;
self->z *= rhs;
return *self;
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVE2PmzAQ_TXmMlpkbAjkwIEszaHX_oDKmCFxazCyTbTk11cGNll1d6N-SFYC8zzMe_PGFs6p04BYkuxAsjoSkz8bWxqNvZinqDHtXH5T_agRxNACajyJwcfwdXIetPqJ8AzKO9RdTGgV1o6uSxJa-XnEFjtw3k7SwwUlB5IfCK0AADpthIcXwp5hDj9XwgNE8nrZub3RaqF2kFoMJ8IrwiszohXeWMIKwirC9iSrw6KVGrQacC104Rf-vZ80YcXyrs8uVFmebXjev-Fi0U92gG3vhoWU-AUIq0JCvDANofkWml9D11voCiSvb0r-V0HygYK1b_8i4Z2Ad_Q_Is-OhB3X4sfgNODLiNJjC95AgyBgNGrwaKEzFtaJ6nHw8KrS_UELeP3QR3dvQ2AaaHxuZkCfCP-y6Ca83tzjv-PzW3x-j1_f4tt03ttcLST-zuiHKpPPVH5i-EcyH4t8LPGOPtD4eryjtuTtnu9FhGWSpwXLi5wn0bnkHUuzXYvdTmaUCyGLtmipLLjMOrFP80iVjLKMZmyX7LIiS2LcUylo0TZpywqZpSSl2AulY60vfWzsKVLOTVgmaZLRXaRFg9otFxZja6sZC3eXLUPCUzOdHEmpVs67-ye88nq55daMYMEBh7MYJIZZJVl9G1cwF7TaiBaE91Y1k8dlsp-jyery7P3ogrnLsTgpf56aWJqesGMotv09jdb8QOkJOy7kw9nZ-F9K9isAAP__YXCXVg">