]> git.ipfire.org Git - thirdparty/gcc.git/blob - libcc1/marshall.hh
libcc1: use templates to unmarshall enums
[thirdparty/gcc.git] / libcc1 / marshall.hh
1 /* Marshalling and unmarshalling.
2 Copyright (C) 2014-2021 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 // A template function that can handle marshalling various integer
56 // objects to the connection.
57 template<typename T>
58 status marshall (connection *conn, T scalar)
59 {
60 return marshall_intlike (conn, scalar);
61 }
62
63 // A template function that can handle unmarshalling various integer
64 // objects from the connection. Note that there's no way at the
65 // protocol level to distinguish different int types.
66 template<typename T>
67 status unmarshall (connection *conn, T *scalar,
68 typename std::enable_if<std::is_integral<T>::value, T>::type * = 0)
69 {
70 protocol_int result;
71
72 if (!unmarshall_intlike (conn, &result))
73 return FAIL;
74 *scalar = (T) result;
75 return OK;
76 }
77
78 // A template function that can handle unmarshalling various enum
79 // objects from the connection.
80 template<typename T>
81 status unmarshall (connection *conn, T *e_val,
82 typename std::enable_if<std::is_enum<T>::value, T>::type * = 0)
83 {
84 protocol_int result;
85
86 if (!unmarshall_intlike (conn, &result))
87 return FAIL;
88 *e_val = (T) result;
89 return OK;
90 }
91
92 // Send a string type marker followed by a string.
93 status marshall (connection *, const char *);
94
95 // Read a string type marker followed by a string. The caller is
96 // responsible for freeing the resulting string using 'delete[]'.
97 status unmarshall (connection *, char **);
98
99 // Send a gcc_type_array marker followed by the array.
100 status marshall (connection *, const gcc_type_array *);
101
102 // Read a gcc_type_array marker, followed by a gcc_type_array. The
103 // resulting array must be freed by the caller, using 'delete[]' on
104 // the elements, and 'delete' on the array object itself.
105 status unmarshall (connection *, struct gcc_type_array **);
106 };
107
108 #endif // CC1_PLUGIN_MARSHALL_HH