aboutsummaryrefslogtreecommitdiff
path: root/gcc/algol68/a68-moids-diagnostics.cc
blob: a984fbc868fd80152fce8aed1213471f41821eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* MOID diagnostics routines.
   Copyright (C) 2001-2023 J. Marcel van der Veer.
   Copyright (C) 2025 Jose E. Marchesi.

   Original implementation by J. Marcel van der Veer.
   Adapted for GCC by Jose E. Marchesi.

   GCC is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GCC is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
   License for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"

#include "a68.h"

/* Give accurate error message.  */

const char *
a68_mode_error_text (NODE_T *n, MOID_T *p, MOID_T *q, int context, int deflex, int depth)
{
#define TAIL(z) (&(z)[strlen (z)])
#define ACTUAL_SNPRINTF_SIZE ((SNPRINTF_SIZE - len))
  static BUFFER txt;
  size_t len;
  if (depth == 1)
    txt[0] = '\0';
  if (IS (p, SERIES_MODE))
    {
      len = strlen (txt);
      PACK_T *u = PACK (p);

      int N = 0;
      if (u == NO_PACK)
	{
	  if (snprintf (txt, ACTUAL_SNPRINTF_SIZE, "empty mode-list") < 0)
	    gcc_unreachable ();
	  N++;
	}
      else
	{
	  for (; u != NO_PACK; FORWARD (u))
	    {
	      if (MOID (u) != NO_MOID)
		{
		  if (IS (MOID (u), SERIES_MODE))
		    (void) a68_mode_error_text (n, MOID (u), q, context, deflex, depth + 1);
		  else if (!a68_is_coercible (MOID (u), q, context, deflex))
		    {
		      len = strlen (txt);
		      if (len > BUFFER_SIZE / 2)
			{
			  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " etcetera") < 0)
			    gcc_unreachable ();
			  N++;
			}
		      else
			{
			  if (len > 0)
			    {
			      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " and ") < 0)
				gcc_unreachable ();
			      N++;
			      len = strlen (txt);
			    }
			  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, "%%<%s%%>",
					a68_moid_to_string (MOID (u), MOID_ERROR_WIDTH, n)) < 0)
			    gcc_unreachable ();
			  N++;
			}
		    }
		}
	    }
	}
      if (depth == 1)
	{
	  len = strlen (txt);
	  if (N == 0)
	    {
	      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, "mode") < 0)
		gcc_unreachable ();
	      len = strlen (txt);
	    }
	  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " cannot be coerced to %%<%s%%>",
			a68_moid_to_string (q, MOID_ERROR_WIDTH, n)) < 0)
	    gcc_unreachable ();
	}
    }
  else if (IS (p, STOWED_MODE) && IS_FLEX (q))
    {
      PACK_T *u = PACK (p);
      len = strlen (txt);
      if (u == NO_PACK)
	{
	  if (snprintf (txt, ACTUAL_SNPRINTF_SIZE, "empty mode-list") < 0)
	    gcc_unreachable ();
	}
      else
	{
	  for (; u != NO_PACK; FORWARD (u))
	    {
	      if (!a68_is_coercible (MOID (u), SLICE (SUB (q)), context, deflex))
		{
		  len = strlen (txt);
		  if (len > BUFFER_SIZE / 2)
		    {
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " etcetera") < 0)
			gcc_unreachable ();
		    }
		  else
		    {
		      if (len > 0)
			{
			  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " and ") < 0)
			    gcc_unreachable ();
			  len = strlen (txt);
			}
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, "%s",
				    a68_moid_to_string (MOID (u), MOID_ERROR_WIDTH, n)) < 0)
			gcc_unreachable ();
		    }
		}
	    }
	  len = strlen (txt);
	  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " cannot be coerced to %%<%s%%>",
			a68_moid_to_string (SLICE (SUB (q)), MOID_ERROR_WIDTH, n)) < 0)
	    gcc_unreachable ();
	}
    }
  else if (IS (p, STOWED_MODE) && IS (q, ROW_SYMBOL))
    {
      PACK_T *u = PACK (p);
      len = strlen (txt);
      if (u == NO_PACK)
	{
	  if (snprintf (txt, ACTUAL_SNPRINTF_SIZE, "empty mode-list") < 0)
	    gcc_unreachable ();
	}
      else
	{
	  for (; u != NO_PACK; FORWARD (u))
	    {
	      if (!a68_is_coercible (MOID (u), SLICE (q), context, deflex))
		{
		  len = strlen (txt);
		  if (len > BUFFER_SIZE / 2)
		    {
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " etcetera") < 0)
			gcc_unreachable ();
		    }
		  else
		    {
		      if (len > 0)
			{
			  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " and ") < 0)
			    gcc_unreachable ();
			  len = strlen (txt);
			}
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, "%%<%s%%>",
				    a68_moid_to_string (MOID (u), MOID_ERROR_WIDTH, n)) < 0)
			gcc_unreachable ();
		    }
		}
	    }
	  len = strlen (txt);
	  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " cannot be coerced to %s",
			a68_moid_to_string (SLICE (q), MOID_ERROR_WIDTH, n)) < 0)
	    gcc_unreachable ();
	}
    }
  else if (IS (p, STOWED_MODE) && (IS (q, PROC_SYMBOL) || IS (q, STRUCT_SYMBOL)))
    {
      PACK_T *u = PACK (p), *v = PACK (q);
      len = strlen (txt);
      if (u == NO_PACK)
	{
	  if (snprintf (txt, ACTUAL_SNPRINTF_SIZE, "empty mode-list") < 0)
	    gcc_unreachable ();
	}
      else
	{
	  for (; u != NO_PACK && v != NO_PACK; FORWARD (u), FORWARD (v))
	    {
	      if (!a68_is_coercible (MOID (u), MOID (v), context, deflex))
		{
		  len = strlen (txt);
		  if (len > BUFFER_SIZE / 2)
		    {
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " etcetera") < 0)
			gcc_unreachable ();
		    }
		  else
		    {
		      if (len > 0)
			{
			  if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, " and ") < 0)
			    gcc_unreachable ();
			  len = strlen (txt);
			}
		      if (snprintf (TAIL (txt), ACTUAL_SNPRINTF_SIZE, "%%<%s%%> cannot be coerced to %%<%s%%>",
				    a68_moid_to_string (MOID (u), MOID_ERROR_WIDTH, n),
				    a68_moid_to_string (MOID (v), MOID_ERROR_WIDTH, n)) < 0)
			gcc_unreachable ();
		    }
		}
	    }
	}
    }
  return txt;
#undef TAIL
#undef ACTUAL_SNPRINTF_SIZE
}

/* Cannot coerce error.  */

void
a68_cannot_coerce (NODE_T *p, MOID_T *from, MOID_T *to, int context, int deflex, int att)
{
  const char *txt = a68_mode_error_text (p, from, to, context, deflex, 1);

  if (att == STOP)
    {
      if (strlen (txt) == 0)
	a68_error (p, "M cannot be coerced to M in C context", from, to, context);
      else
	a68_error (p, "Y in C context", txt, context);
    }
  else
    {
      if (strlen (txt) == 0)
	a68_error (p, "M cannot be coerced to M in C-A", from, to, context, att);
      else
	a68_error (p, "Y in C-A", txt, context, att);
    }
}

/* Give a warning when a value is silently discarded.  */

void
a68_warn_for_voiding (NODE_T *p, SOID_T *x, SOID_T *y, int c)
{
  (void) c;

  if (CAST (x) == false)
    {
      if (MOID (x) == M_VOID && MOID (y) != M_ERROR && !(MOID (y) == M_VOID || !a68_is_nonproc (MOID (y))))
	{
	  if (IS (p, FORMULA))
	    a68_warning (p, OPT_Wvoiding, "value of M @ will be voided", MOID (y));
	  else
	    a68_warning (p, OPT_Wvoiding, "value of M @ will be voided", MOID (y));
	}
    }
}

/* Warn for things that are likely unintended.  */

void
a68_semantic_pitfall (NODE_T *p, MOID_T *m, int c, int u)
{
  /* semantic_pitfall: warn for things that are likely unintended, for instance
                       REF INT i := LOC INT := 0, which should probably be
                       REF INT i = LOC INT := 0.  */
  if (IS (p, u))
    a68_warning (p, 0, "possibly unintended M A in M A",
		 MOID (p), u, m, c);
  else if (a68_is_one_of (p, UNIT, TERTIARY, SECONDARY, PRIMARY, STOP))
    a68_semantic_pitfall (SUB (p), m, c, u);
}