]> git.ipfire.org Git - thirdparty/gcc.git/blame - libcc1/marshall.cc
Update GCC to autoconf 2.69, automake 1.15.1 (PR bootstrap/82856).
[thirdparty/gcc.git] / libcc1 / marshall.cc
CommitLineData
ab103e33 1/* Marshalling and unmarshalling.
8e8f6434 2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
ab103e33 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include <cc1plugin-config.h>
21#include <new>
22#include <string.h>
23#include "marshall.hh"
24#include "connection.hh"
25
26cc1_plugin::status
27cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
28{
29 unsigned long long r;
30
31 if (!unmarshall (conn, &r))
32 return FAIL;
33 return check == r ? OK : FAIL;
34}
35
36cc1_plugin::status
37cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
38{
39 if (!conn->send ('i'))
40 return FAIL;
41 return conn->send (&val, sizeof (val));
42}
43
44cc1_plugin::status
45cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
46{
47 if (!conn->require ('i'))
48 return FAIL;
49 return conn->get (result, sizeof (*result));
50}
51
ab103e33 52cc1_plugin::status
53cc1_plugin::marshall (connection *conn, const char *str)
54{
55 if (!conn->send ('s'))
56 return FAIL;
57
58 unsigned long long len = str == NULL ? -1ULL : strlen (str);
59 if (!conn->send (&len, sizeof (len)))
60 return FAIL;
61
62 if (str == NULL)
63 return OK;
64
65 return conn->send (str, len);
66}
67
68cc1_plugin::status
69cc1_plugin::unmarshall (connection *conn, char **result)
70{
71 unsigned long long len;
72
73 if (!conn->require ('s'))
74 return FAIL;
75 if (!conn->get (&len, sizeof (len)))
76 return FAIL;
77
78 if (len == -1ULL)
79 {
80 *result = NULL;
81 return OK;
82 }
83
84 char *str = new (std::nothrow) char[len + 1];
85 if (str == NULL)
86 return FAIL;
87
88 if (!conn->get (str, len))
89 {
90 delete[] str;
91 return FAIL;
92 }
93
94 str[len] = '\0';
95 *result = str;
96
97 return OK;
98}
99
100cc1_plugin::status
37af486a 101cc1_plugin::marshall_array_start (connection *conn, char id,
102 size_t n_elements)
ab103e33 103{
37af486a 104 if (!conn->send (id))
ab103e33 105 return FAIL;
106
37af486a 107 unsigned long long r = n_elements;
ab103e33 108 if (!conn->send (&r, sizeof (r)))
109 return FAIL;
110
37af486a 111 return OK;
ab103e33 112}
113
114cc1_plugin::status
37af486a 115cc1_plugin::marshall_array_elmts (connection *conn, size_t n_bytes,
116 void *elements)
117{
118 return conn->send (elements, n_bytes);
119}
120
121cc1_plugin::status
122cc1_plugin::unmarshall_array_start (connection *conn, char id,
123 size_t *n_elements)
ab103e33 124{
125 unsigned long long len;
126
37af486a 127 if (!conn->require (id))
ab103e33 128 return FAIL;
129 if (!conn->get (&len, sizeof (len)))
130 return FAIL;
131
37af486a 132 *n_elements = len;
133
134 return OK;
135}
136
137cc1_plugin::status
138cc1_plugin::unmarshall_array_elmts (connection *conn, size_t n_bytes,
139 void *elements)
140{
141 return conn->get (elements, n_bytes);
142}
143
144cc1_plugin::status
145cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
146{
147 size_t len;
148
149 if (a)
150 len = a->n_elements;
151 else
152 len = (size_t)-1;
153
154 if (!marshall_array_start (conn, 'a', len))
155 return FAIL;
156
157 if (!a)
158 return OK;
159
160 return marshall_array_elmts (conn, len * sizeof (a->elements[0]),
161 a->elements);
162}
163
164cc1_plugin::status
165cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
166{
167 size_t len;
168
169 if (!unmarshall_array_start (conn, 'a', &len))
170 return FAIL;
171
172 if (len == (size_t)-1)
173 {
174 *result = NULL;
175 return OK;
176 }
177
178 gcc_type_array *gta = new gcc_type_array;
ab103e33 179
37af486a 180 gta->n_elements = len;
181 gta->elements = new gcc_type[len];
ab103e33 182
37af486a 183 if (!unmarshall_array_elmts (conn,
184 len * sizeof (gta->elements[0]),
185 gta->elements))
ab103e33 186 {
37af486a 187 delete[] gta->elements;
ab103e33 188 delete *result;
189 return FAIL;
190 }
191
37af486a 192 *result = gta;
193
ab103e33 194 return OK;
195}