1/* st.c -- Implementation File (module.c template V1.0)
2   Copyright (C) 1995 Free Software Foundation, Inc.
3   Contributed by James Craig Burley.
4
5This file is part of GNU Fortran.
6
7GNU Fortran is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Fortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Fortran; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA.
21
22   Related Modules:
23      None
24
25   Description:
26      The high-level input level to statement handling for the rest of the
27      FFE.  ffest_first is the first state for the lexer to invoke to start
28      a statement.  A statement normally starts with a NUMBER token (to indicate
29      a label def) followed by a NAME token (to indicate what kind of statement
30      it is), though of course the NUMBER token may be omitted.	 ffest_first
31      gathers the first NAME token and returns a state of ffest_second_,
32      where the trailing underscore means "internal to ffest" and thus outside
33      users should not depend on this.	ffest_second_ then looks at the second
34      token in conjunction with the first, decides what possible statements are
35      meant, and tries each possible statement in turn, from most likely to
36      least likely.  A successful attempt currently is recorded, and further
37      successful attempts by other possibilities raise an assertion error in
38      ffest_confirmed (this is to detect ambiguities).	A failure in an
39      attempt is signaled by calling ffest_ffebad_start; this results in the
40      next token sent by ffest_save_ (the intermediary when more than one
41      possible statement exists) being EOS to shut down processing and the next
42      possibility tried.
43
44      When all possibilities have been tried, the successful one is retried with
45      inhibition turned off (FALSE) as reported by ffest_is_inhibited().  If
46      there is no successful one, the first one is retried so the user gets to
47      see the error messages.
48
49      In the future, after syntactic bugs have been reasonably shaken out and
50      ambiguities thus detected, the first successful possibility will be
51      enabled (inhibited goes FALSE) as soon as it confirms success by calling
52      ffest_confirmed, thus retrying the possibility will not be necessary.
53
54      The only complication in all this is that expression handling is
55      happening while possibilities are inhibited.  It is up to the expression
56      handler, conceptually, to not make any changes to its knowledge base for
57      variable names and so on when inhibited that cannot be undone if
58      the current possibility fails (shuts down via ffest_ffebad_start).  In
59      fact, this business is handled not be ffeexpr, but by lower levels.
60
61      ffesta functions serve only to provide information used in syntactic
62      processing of possible statements, and thus may not make changes to the
63      knowledge base for variables and such.
64
65      ffestb functions perform the syntactic analysis for possible statements,
66      and thus again may not make changes to the knowledge base except under the
67      auspices of ffeexpr and its subordinates, changes which can be undone when
68      necessary.
69
70      ffestc functions perform the semantic analysis for the chosen statement,
71      and thus may change the knowledge base as necessary since they are invoked
72      by ffestb functions only after a given statement is confirmed and
73      enabled.	Note, however, that a few ffestc functions (identified by
74      their statement names rather than grammar numbers) indicate valid forms
75      that are, outside of any context, ambiguous, such as ELSE WHERE and
76      PRIVATE; these functions should make a quick decision as to what is
77      intended and dispatch to the appropriate specific ffestc function.
78
79      ffestd functions actually implement statements.  When called, the
80      statement is considered valid and is either an executable statement or
81      a nonexecutable statement with direct-output results.  For example, CALL,
82      GOTO, and assignment statements pass through ffestd because they are
83      executable; DATA statements pass through because they map directly to the
84      output file (or at least might so map); ENTRY statements also pass through
85      because they essentially affect code generation in an immediate way;
86      whereas INTEGER, SAVE, and SUBROUTINE statements do not go through
87      ffestd functions because they merely update the knowledge base.
88
89   Modifications:
90*/
91
92/* Include files. */
93
94#include "proj.h"
95#include "st.h"
96#include "bad.h"
97#include "lex.h"
98#include "sta.h"
99#include "stb.h"
100#include "stc.h"
101#include "std.h"
102#include "ste.h"
103#include "stp.h"
104#include "str.h"
105#include "sts.h"
106#include "stt.h"
107#include "stu.h"
108#include "stv.h"
109#include "stw.h"
110
111/* Externals defined here. */
112
113
114/* Simple definitions and enumerations. */
115
116
117/* Internal typedefs. */
118
119
120/* Private include files. */
121
122
123/* Internal structure definitions. */
124
125
126/* Static objects accessed by functions in this module. */
127
128
129/* Static functions (internal). */
130
131
132/* Internal macros. */
133
134
135/* ffest_confirmed -- Confirm current possibility as only one
136
137   ffest_confirmed();
138
139   Sets the confirmation flag.	During debugging for ambiguous constructs,
140   asserts that the confirmation flag for a previous possibility has not
141   yet been set.  */
142
143void
144ffest_confirmed ()
145{
146  ffesta_confirmed ();
147}
148
149/* ffest_eof -- End of (non-INCLUDEd) source file
150
151   ffest_eof();
152
153   Call after piping tokens through ffest_first, where the most recent
154   token sent through must be EOS.
155
156   20-Feb-91  JCB  1.1
157      Put new EOF token in ffesta_tokens[0], not NULL, because too much
158      code expects something there for error reporting and the like.  Also,
159      do basically the same things ffest_second and ffesta_zero do for
160      processing a statement (make and destroy pools, et cetera).  */
161
162void
163ffest_eof ()
164{
165  ffesta_eof ();
166}
167
168/* ffest_ffebad_here_current_stmt -- ffebad_here with ptr to current stmt
169
170   ffest_ffebad_here_current_stmt(0);
171
172   Outsiders can call this fn if they have no more convenient place to
173   point to (via a token or pair of ffewhere objects) and they know a
174   current, useful statement is being evaluted by ffest (i.e. they are
175   being called from ffestb, ffestc, ffestd, ... functions).  */
176
177void
178ffest_ffebad_here_current_stmt (ffebadIndex i)
179{
180  ffesta_ffebad_here_current_stmt (i);
181}
182
183/* ffest_ffebad_here_doiter -- Calls ffebad_here with ptr to DO iter var
184
185   ffesymbol s;
186   // call ffebad_start first, of course.
187   ffest_ffebad_here_doiter(0,s);
188   // call ffebad_finish afterwards, naturally.
189
190   Searches the stack of blocks backwards for a DO loop that has s
191   as its iteration variable, then calls ffebad_here with pointers to
192   that particular reference to the variable.  Crashes if the DO loop
193   can't be found.  */
194
195void
196ffest_ffebad_here_doiter (ffebadIndex i, ffesymbol s)
197{
198  ffestc_ffebad_here_doiter (i, s);
199}
200
201/* ffest_ffebad_start -- Start a possibly inhibited error report
202
203   if (ffest_ffebad_start(FFEBAD_SOME_ERROR))
204       {
205       ffebad_here, ffebad_string ...;
206       ffebad_finish();
207       }
208
209   Call if the error might indicate that ffest is evaluating the wrong
210   statement form, instead of calling ffebad_start directly.  If ffest
211   is choosing between forms, it will return FALSE, send an EOS/SEMICOLON
212   token through as the next token (if the current one isn't already one
213   of those), and try another possible form.  Otherwise, ffebad_start is
214   called with the argument and TRUE returned.	*/
215
216bool
217ffest_ffebad_start (ffebad errnum)
218{
219  return ffesta_ffebad_start (errnum);
220}
221
222/* ffest_first -- Parse the first token in a statement
223
224   return ffest_first;	// to lexer.  */
225
226ffelexHandler
227ffest_first (ffelexToken t)
228{
229  return ffesta_first (t);
230}
231
232/* ffest_init_0 -- Initialize for entire image invocation
233
234   ffest_init_0();
235
236   Call just once per invocation of the compiler (not once per invocation
237   of the front end).
238
239   Gets memory for the list of possibles once and for all, since this
240   list never gets larger than a certain size (FFEST_maxPOSSIBLES_)
241   and is not particularly large.  Initializes the array of pointers to
242   this list.  Initializes the executable and nonexecutable lists.  */
243
244void
245ffest_init_0 ()
246{
247  ffesta_init_0 ();
248  ffestb_init_0 ();
249  ffestc_init_0 ();
250  ffestd_init_0 ();
251  ffeste_init_0 ();
252  ffestp_init_0 ();
253  ffestr_init_0 ();
254  ffests_init_0 ();
255  ffestt_init_0 ();
256  ffestu_init_0 ();
257  ffestv_init_0 ();
258  ffestw_init_0 ();
259}
260
261/* ffest_init_1 -- Initialize for entire image invocation
262
263   ffest_init_1();
264
265   Call just once per invocation of the compiler (not once per invocation
266   of the front end).
267
268   Gets memory for the list of possibles once and for all, since this
269   list never gets larger than a certain size (FFEST_maxPOSSIBLES_)
270   and is not particularly large.  Initializes the array of pointers to
271   this list.  Initializes the executable and nonexecutable lists.  */
272
273void
274ffest_init_1 ()
275{
276  ffesta_init_1 ();
277  ffestb_init_1 ();
278  ffestc_init_1 ();
279  ffestd_init_1 ();
280  ffeste_init_1 ();
281  ffestp_init_1 ();
282  ffestr_init_1 ();
283  ffests_init_1 ();
284  ffestt_init_1 ();
285  ffestu_init_1 ();
286  ffestv_init_1 ();
287  ffestw_init_1 ();
288}
289
290/* ffest_init_2 -- Initialize for entire image invocation
291
292   ffest_init_2();
293
294   Call just once per invocation of the compiler (not once per invocation
295   of the front end).
296
297   Gets memory for the list of possibles once and for all, since this
298   list never gets larger than a certain size (FFEST_maxPOSSIBLES_)
299   and is not particularly large.  Initializes the array of pointers to
300   this list.  Initializes the executable and nonexecutable lists.  */
301
302void
303ffest_init_2 ()
304{
305  ffesta_init_2 ();
306  ffestb_init_2 ();
307  ffestc_init_2 ();
308  ffestd_init_2 ();
309  ffeste_init_2 ();
310  ffestp_init_2 ();
311  ffestr_init_2 ();
312  ffests_init_2 ();
313  ffestt_init_2 ();
314  ffestu_init_2 ();
315  ffestv_init_2 ();
316  ffestw_init_2 ();
317}
318
319/* ffest_init_3 -- Initialize for any program unit
320
321   ffest_init_3();  */
322
323void
324ffest_init_3 ()
325{
326  ffesta_init_3 ();
327  ffestb_init_3 ();
328  ffestc_init_3 ();
329  ffestd_init_3 ();
330  ffeste_init_3 ();
331  ffestp_init_3 ();
332  ffestr_init_3 ();
333  ffests_init_3 ();
334  ffestt_init_3 ();
335  ffestu_init_3 ();
336  ffestv_init_3 ();
337  ffestw_init_3 ();
338
339  ffestw_display_state ();
340}
341
342/* ffest_init_4 -- Initialize for statement functions
343
344   ffest_init_4();  */
345
346void
347ffest_init_4 ()
348{
349  ffesta_init_4 ();
350  ffestb_init_4 ();
351  ffestc_init_4 ();
352  ffestd_init_4 ();
353  ffeste_init_4 ();
354  ffestp_init_4 ();
355  ffestr_init_4 ();
356  ffests_init_4 ();
357  ffestt_init_4 ();
358  ffestu_init_4 ();
359  ffestv_init_4 ();
360  ffestw_init_4 ();
361}
362
363/* Test whether ENTRY statement is valid.
364
365   Returns TRUE if current program unit is known to be FUNCTION or SUBROUTINE.
366   Else returns FALSE.  */
367
368bool
369ffest_is_entry_valid ()
370{
371  return ffesta_is_entry_valid;
372}
373
374/* ffest_is_inhibited -- Test whether the current possibility is inhibited
375
376   if (!ffest_is_inhibited())
377       // implement the statement.
378
379   Just make sure the current possibility has been confirmed.  If anyone
380   really needs to test whether the current possibility is inhibited prior
381   to confirming it, that indicates a need to begin statement processing
382   before it is certain that the given possibility is indeed the statement
383   to be processed.  As of this writing, there does not appear to be such
384   a need.  If there is, then when confirming a statement would normally
385   immediately disable the inhibition (whereas currently we leave the
386   confirmed statement disabled until we've tried the other possibilities,
387   to check for ambiguities), we must check to see if the possibility has
388   already tested for inhibition prior to confirmation and, if so, maintain
389   inhibition until the end of the statement (which may be forced right
390   away) and then rerun the entire statement from the beginning.  Otherwise,
391   initial calls to ffestb functions won't have been made, but subsequent
392   calls (after confirmation) will, which is wrong.  Of course, this all
393   applies only to those statements implemented via multiple calls to
394   ffestb, although if a statement requiring only a single ffestb call
395   tested for inhibition prior to confirmation, it would likely mean that
396   the ffestb call would be completely dropped without this mechanism.	*/
397
398bool
399ffest_is_inhibited ()
400{
401  return ffesta_is_inhibited ();
402}
403
404/* ffest_seen_first_exec -- Test whether first executable stmt has been seen
405
406   if (ffest_seen_first_exec())
407       // No more spec stmts can be seen.
408
409   In a case where, say, the first statement is PARAMETER(A)=B, FALSE
410   will be returned while the PARAMETER statement is being run, and TRUE
411   will be returned if it doesn't confirm and the assignment statement
412   is being run.  */
413
414bool
415ffest_seen_first_exec ()
416{
417  return ffesta_seen_first_exec;
418}
419
420/* Shut down current parsing possibility, but without bothering the
421   user with a diagnostic if we're not inhibited.  */
422
423void
424ffest_shutdown ()
425{
426  ffesta_shutdown ();
427}
428
429/* ffest_sym_end_transition -- Update symbol info just before end of unit
430
431   ffesymbol s;
432   ffest_sym_end_transition(s);	 */
433
434ffesymbol
435ffest_sym_end_transition (ffesymbol s)
436{
437  return ffestu_sym_end_transition (s);
438}
439
440/* ffest_sym_exec_transition -- Update symbol just before first exec stmt
441
442   ffesymbol s;
443   ffest_sym_exec_transition(s);  */
444
445ffesymbol
446ffest_sym_exec_transition (ffesymbol s)
447{
448  return ffestu_sym_exec_transition (s);
449}
450
451/* ffest_terminate_0 -- Terminate for entire image invocation
452
453   ffest_terminate_0();	 */
454
455void
456ffest_terminate_0 ()
457{
458  ffesta_terminate_0 ();
459  ffestb_terminate_0 ();
460  ffestc_terminate_0 ();
461  ffestd_terminate_0 ();
462  ffeste_terminate_0 ();
463  ffestp_terminate_0 ();
464  ffestr_terminate_0 ();
465  ffests_terminate_0 ();
466  ffestt_terminate_0 ();
467  ffestu_terminate_0 ();
468  ffestv_terminate_0 ();
469  ffestw_terminate_0 ();
470}
471
472/* ffest_terminate_1 -- Terminate for source file
473
474   ffest_terminate_1();	 */
475
476void
477ffest_terminate_1 ()
478{
479  ffesta_terminate_1 ();
480  ffestb_terminate_1 ();
481  ffestc_terminate_1 ();
482  ffestd_terminate_1 ();
483  ffeste_terminate_1 ();
484  ffestp_terminate_1 ();
485  ffestr_terminate_1 ();
486  ffests_terminate_1 ();
487  ffestt_terminate_1 ();
488  ffestu_terminate_1 ();
489  ffestv_terminate_1 ();
490  ffestw_terminate_1 ();
491}
492
493/* ffest_terminate_2 -- Terminate for outer program unit
494
495   ffest_terminate_2();	 */
496
497void
498ffest_terminate_2 ()
499{
500  ffesta_terminate_2 ();
501  ffestb_terminate_2 ();
502  ffestc_terminate_2 ();
503  ffestd_terminate_2 ();
504  ffeste_terminate_2 ();
505  ffestp_terminate_2 ();
506  ffestr_terminate_2 ();
507  ffests_terminate_2 ();
508  ffestt_terminate_2 ();
509  ffestu_terminate_2 ();
510  ffestv_terminate_2 ();
511  ffestw_terminate_2 ();
512}
513
514/* ffest_terminate_3 -- Terminate for any program unit
515
516   ffest_terminate_3();	 */
517
518void
519ffest_terminate_3 ()
520{
521  ffesta_terminate_3 ();
522  ffestb_terminate_3 ();
523  ffestc_terminate_3 ();
524  ffestd_terminate_3 ();
525  ffeste_terminate_3 ();
526  ffestp_terminate_3 ();
527  ffestr_terminate_3 ();
528  ffests_terminate_3 ();
529  ffestt_terminate_3 ();
530  ffestu_terminate_3 ();
531  ffestv_terminate_3 ();
532  ffestw_terminate_3 ();
533}
534
535/* ffest_terminate_4 -- Terminate for statement functions
536
537   ffest_terminate_4();	 */
538
539void
540ffest_terminate_4 ()
541{
542  ffesta_terminate_4 ();
543  ffestb_terminate_4 ();
544  ffestc_terminate_4 ();
545  ffestd_terminate_4 ();
546  ffeste_terminate_4 ();
547  ffestp_terminate_4 ();
548  ffestr_terminate_4 ();
549  ffests_terminate_4 ();
550  ffestt_terminate_4 ();
551  ffestu_terminate_4 ();
552  ffestv_terminate_4 ();
553  ffestw_terminate_4 ();
554}
555