1 /* Marshalling and unmarshalling.
2 Copyright (C) 2014-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #include <cc1plugin-config.h>
23 #include "marshall.hh"
24 #include "connection.hh"
28 cc1_plugin::unmarshall_check (connection
*conn
, unsigned long long check
)
32 if (!unmarshall (conn
, &r
))
34 return check
== r
? OK
: FAIL
;
38 cc1_plugin::marshall_intlike (connection
*conn
, unsigned long long val
)
40 if (!conn
->send ('i'))
42 return conn
->send (&val
, sizeof (val
));
46 cc1_plugin::unmarshall_intlike (connection
*conn
, unsigned long long *result
)
48 if (!conn
->require ('i'))
50 return conn
->get (result
, sizeof (*result
));
54 cc1_plugin::marshall (connection
*conn
, const char *str
)
56 if (!conn
->send ('s'))
59 unsigned long long len
= str
== NULL
? -1ULL : strlen (str
);
60 if (!conn
->send (&len
, sizeof (len
)))
66 return conn
->send (str
, len
);
70 cc1_plugin::unmarshall (connection
*conn
, char **result
)
72 unsigned long long len
;
74 if (!conn
->require ('s'))
76 if (!conn
->get (&len
, sizeof (len
)))
85 char *str
= new (std::nothrow
) char[len
+ 1];
89 if (!conn
->get (str
, len
))
102 cc1_plugin::marshall_array_start (connection
*conn
, char id
,
105 if (!conn
->send (id
))
108 unsigned long long r
= n_elements
;
109 if (!conn
->send (&r
, sizeof (r
)))
116 cc1_plugin::marshall_array_elmts (connection
*conn
, size_t n_bytes
,
119 return conn
->send (elements
, n_bytes
);
123 cc1_plugin::unmarshall_array_start (connection
*conn
, char id
,
126 unsigned long long len
;
128 if (!conn
->require (id
))
130 if (!conn
->get (&len
, sizeof (len
)))
139 cc1_plugin::unmarshall_array_elmts (connection
*conn
, size_t n_bytes
,
142 return conn
->get (elements
, n_bytes
);
146 cc1_plugin::marshall (connection
*conn
, const gcc_type_array
*a
)
155 if (!marshall_array_start (conn
, 'a', len
))
161 return marshall_array_elmts (conn
, len
* sizeof (a
->elements
[0]),
166 cc1_plugin::unmarshall (connection
*conn
, gcc_type_array
**result
)
170 if (!unmarshall_array_start (conn
, 'a', &len
))
173 if (len
== (size_t)-1)
179 cc1_plugin::unique_ptr
<gcc_type_array
> gta (new gcc_type_array
{});
181 gta
->n_elements
= len
;
182 gta
->elements
= new gcc_type
[len
];
184 if (!unmarshall_array_elmts (conn
,
185 len
* sizeof (gta
->elements
[0]),
189 *result
= gta
.release ();