1 /* Marshalling and unmarshalling.
2 Copyright (C) 2014-2020 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"
27 cc1_plugin::unmarshall_check (connection
*conn
, unsigned long long check
)
31 if (!unmarshall (conn
, &r
))
33 return check
== r
? OK
: FAIL
;
37 cc1_plugin::marshall_intlike (connection
*conn
, unsigned long long val
)
39 if (!conn
->send ('i'))
41 return conn
->send (&val
, sizeof (val
));
45 cc1_plugin::unmarshall_intlike (connection
*conn
, unsigned long long *result
)
47 if (!conn
->require ('i'))
49 return conn
->get (result
, sizeof (*result
));
53 cc1_plugin::marshall (connection
*conn
, const char *str
)
55 if (!conn
->send ('s'))
58 unsigned long long len
= str
== NULL
? -1ULL : strlen (str
);
59 if (!conn
->send (&len
, sizeof (len
)))
65 return conn
->send (str
, len
);
69 cc1_plugin::unmarshall (connection
*conn
, char **result
)
71 unsigned long long len
;
73 if (!conn
->require ('s'))
75 if (!conn
->get (&len
, sizeof (len
)))
84 char *str
= new (std::nothrow
) char[len
+ 1];
88 if (!conn
->get (str
, len
))
101 cc1_plugin::marshall_array_start (connection
*conn
, char id
,
104 if (!conn
->send (id
))
107 unsigned long long r
= n_elements
;
108 if (!conn
->send (&r
, sizeof (r
)))
115 cc1_plugin::marshall_array_elmts (connection
*conn
, size_t n_bytes
,
118 return conn
->send (elements
, n_bytes
);
122 cc1_plugin::unmarshall_array_start (connection
*conn
, char id
,
125 unsigned long long len
;
127 if (!conn
->require (id
))
129 if (!conn
->get (&len
, sizeof (len
)))
138 cc1_plugin::unmarshall_array_elmts (connection
*conn
, size_t n_bytes
,
141 return conn
->get (elements
, n_bytes
);
145 cc1_plugin::marshall (connection
*conn
, const gcc_type_array
*a
)
154 if (!marshall_array_start (conn
, 'a', len
))
160 return marshall_array_elmts (conn
, len
* sizeof (a
->elements
[0]),
165 cc1_plugin::unmarshall (connection
*conn
, gcc_type_array
**result
)
169 if (!unmarshall_array_start (conn
, 'a', &len
))
172 if (len
== (size_t)-1)
178 gcc_type_array
*gta
= new gcc_type_array
;
180 gta
->n_elements
= len
;
181 gta
->elements
= new gcc_type
[len
];
183 if (!unmarshall_array_elmts (conn
,
184 len
* sizeof (gta
->elements
[0]),
187 delete[] gta
->elements
;