| 1 |
/* Marshalling and unmarshalling. |
| 2 |
Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 3, or (at your option) any later |
| 9 |
version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
#ifndef CC1_PLUGIN_MARSHALL_HH |
| 21 |
#define CC1_PLUGIN_MARSHALL_HH |
| 22 |
|
| 23 |
#include <type_traits> |
| 24 |
|
| 25 |
#include "status.hh" |
| 26 |
#include "gcc-interface.h" |
| 27 |
|
| 28 |
namespace cc1_plugin |
| 29 |
{ |
| 30 |
class connection; |
| 31 |
|
| 32 |
// Only a single kind of integer is ever sent over the wire, and |
| 33 |
// this is it. |
| 34 |
typedef unsigned long long protocol_int; |
| 35 |
|
| 36 |
// Read an integer from the connection and verify that it has the |
| 37 |
// value V. |
| 38 |
status unmarshall_check (connection *, protocol_int v); |
| 39 |
|
| 40 |
// Write an integer, prefixed with the integer type marker, to the |
| 41 |
// connection. |
| 42 |
status marshall_intlike (connection *, protocol_int); |
| 43 |
|
| 44 |
// Read a type marker from the connection and verify that it is an |
| 45 |
// integer type marker. If not, return FAIL. If so, read an |
| 46 |
// integer store it in the out argument. |
| 47 |
status unmarshall_intlike (connection *, protocol_int *); |
| 48 |
|
| 49 |
status marshall_array_start (connection *, char, size_t); |
| 50 |
status marshall_array_elmts (connection *, size_t, void *); |
| 51 |
|
| 52 |
status unmarshall_array_start (connection *, char, size_t *); |
| 53 |
status unmarshall_array_elmts (connection *, size_t, void *); |
| 54 |
|
| 55 |
// An "empty" marshall call -- used to handle the base case for some |
| 56 |
// variadic templates. |
| 57 |
static inline |
| 58 |
status marshall (connection *) |
| 59 |
{ |
| 60 |
return OK; |
| 61 |
} |
| 62 |
|
| 63 |
// A template function that can handle marshalling various integer |
| 64 |
// objects to the connection. |
| 65 |
template<typename T> |
| 66 |
status marshall (connection *conn, T scalar) |
| 67 |
{ |
| 68 |
return marshall_intlike (conn, scalar); |
| 69 |
} |
| 70 |
|
| 71 |
// A template function that can handle unmarshalling various integer |
| 72 |
// objects from the connection. Note that there's no way at the |
| 73 |
// protocol level to distinguish different int types. |
| 74 |
template<typename T> |
| 75 |
status unmarshall (connection *conn, T *scalar, |
| 76 |
typename std::enable_if<std::is_integral<T>::value, T>::type * = 0) |
| 77 |
{ |
| 78 |
protocol_int result; |
| 79 |
|
| 80 |
if (!unmarshall_intlike (conn, &result)) |
| 81 |
return FAIL; |
| 82 |
*scalar = (T) result; |
| 83 |
return OK; |
| 84 |
} |
| 85 |
|
| 86 |
// A template function that can handle unmarshalling various enum |
| 87 |
// objects from the connection. |
| 88 |
template<typename T> |
| 89 |
status unmarshall (connection *conn, T *e_val, |
| 90 |
typename std::enable_if<std::is_enum<T>::value, T>::type * = 0) |
| 91 |
{ |
| 92 |
protocol_int result; |
| 93 |
|
| 94 |
if (!unmarshall_intlike (conn, &result)) |
| 95 |
return FAIL; |
| 96 |
*e_val = (T) result; |
| 97 |
return OK; |
| 98 |
} |
| 99 |
|
| 100 |
// Send a string type marker followed by a string. |
| 101 |
status marshall (connection *, const char *); |
| 102 |
|
| 103 |
// Read a string type marker followed by a string. The caller is |
| 104 |
// responsible for freeing the resulting string using 'delete[]'. |
| 105 |
status unmarshall (connection *, char **); |
| 106 |
|
| 107 |
// Send a gcc_type_array marker followed by the array. |
| 108 |
status marshall (connection *, const gcc_type_array *); |
| 109 |
|
| 110 |
// Read a gcc_type_array marker, followed by a gcc_type_array. The |
| 111 |
// resulting array must be freed by the caller, using 'delete[]' on |
| 112 |
// the elements, and 'delete' on the array object itself. |
| 113 |
status unmarshall (connection *, struct gcc_type_array **); |
| 114 |
|
| 115 |
template<typename T1, typename T2, typename... Arg> |
| 116 |
status marshall (connection *c, T1 arg1, T2 arg2, Arg... rest) |
| 117 |
{ |
| 118 |
if (!marshall (c, arg1)) |
| 119 |
return FAIL; |
| 120 |
return marshall (c, arg2, rest...); |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
#endif // CC1_PLUGIN_MARSHALL_HH |