libstdc++: Update tzdata to 2023d
[official-gcc.git] / libcc1 / marshall.cc
blob672b66f2cfe42709a7a3abbcd08bd5d711bc252b
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
9 version.
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.
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>
21 #include <new>
22 #include <string.h>
23 #include "marshall.hh"
24 #include "connection.hh"
25 #include "rpc.hh"
27 cc1_plugin::status
28 cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
30 unsigned long long r;
32 if (!unmarshall (conn, &r))
33 return FAIL;
34 return check == r ? OK : FAIL;
37 cc1_plugin::status
38 cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
40 if (!conn->send ('i'))
41 return FAIL;
42 return conn->send (&val, sizeof (val));
45 cc1_plugin::status
46 cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
48 if (!conn->require ('i'))
49 return FAIL;
50 return conn->get (result, sizeof (*result));
53 cc1_plugin::status
54 cc1_plugin::marshall (connection *conn, const char *str)
56 if (!conn->send ('s'))
57 return FAIL;
59 unsigned long long len = str == NULL ? -1ULL : strlen (str);
60 if (!conn->send (&len, sizeof (len)))
61 return FAIL;
63 if (str == NULL)
64 return OK;
66 return conn->send (str, len);
69 cc1_plugin::status
70 cc1_plugin::unmarshall (connection *conn, char **result)
72 unsigned long long len;
74 if (!conn->require ('s'))
75 return FAIL;
76 if (!conn->get (&len, sizeof (len)))
77 return FAIL;
79 if (len == -1ULL)
81 *result = NULL;
82 return OK;
85 char *str = new (std::nothrow) char[len + 1];
86 if (str == NULL)
87 return FAIL;
89 if (!conn->get (str, len))
91 delete[] str;
92 return FAIL;
95 str[len] = '\0';
96 *result = str;
98 return OK;
101 cc1_plugin::status
102 cc1_plugin::marshall_array_start (connection *conn, char id,
103 size_t n_elements)
105 if (!conn->send (id))
106 return FAIL;
108 unsigned long long r = n_elements;
109 if (!conn->send (&r, sizeof (r)))
110 return FAIL;
112 return OK;
115 cc1_plugin::status
116 cc1_plugin::marshall_array_elmts (connection *conn, size_t n_bytes,
117 void *elements)
119 return conn->send (elements, n_bytes);
122 cc1_plugin::status
123 cc1_plugin::unmarshall_array_start (connection *conn, char id,
124 size_t *n_elements)
126 unsigned long long len;
128 if (!conn->require (id))
129 return FAIL;
130 if (!conn->get (&len, sizeof (len)))
131 return FAIL;
133 *n_elements = len;
135 return OK;
138 cc1_plugin::status
139 cc1_plugin::unmarshall_array_elmts (connection *conn, size_t n_bytes,
140 void *elements)
142 return conn->get (elements, n_bytes);
145 cc1_plugin::status
146 cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
148 size_t len;
150 if (a)
151 len = a->n_elements;
152 else
153 len = (size_t)-1;
155 if (!marshall_array_start (conn, 'a', len))
156 return FAIL;
158 if (!a)
159 return OK;
161 return marshall_array_elmts (conn, len * sizeof (a->elements[0]),
162 a->elements);
165 cc1_plugin::status
166 cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
168 size_t len;
170 if (!unmarshall_array_start (conn, 'a', &len))
171 return FAIL;
173 if (len == (size_t)-1)
175 *result = NULL;
176 return OK;
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]),
186 gta->elements))
187 return FAIL;
189 *result = gta.release ();
191 return OK;