OSDN Git Service

241b2db54ef4b7b4e1aea402778703dee9299c45
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch9.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 9                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 --  Turn off subprogram body ordering check. Subprograms are in order by RM
28 --  section rather than alphabetical.
29
30 separate (Par)
31 package body Ch9 is
32
33    --  Local subprograms, used only in this chapter
34
35    function P_Accept_Alternative                   return Node_Id;
36    function P_Delay_Alternative                    return Node_Id;
37    function P_Delay_Relative_Statement             return Node_Id;
38    function P_Delay_Until_Statement                return Node_Id;
39    function P_Entry_Barrier                        return Node_Id;
40    function P_Entry_Body_Formal_Part               return Node_Id;
41    function P_Entry_Declaration                    return Node_Id;
42    function P_Entry_Index_Specification            return Node_Id;
43    function P_Protected_Definition                 return Node_Id;
44    function P_Protected_Operation_Declaration_Opt  return Node_Id;
45    function P_Protected_Operation_Items            return List_Id;
46    function P_Task_Definition                      return Node_Id;
47    function P_Task_Items                           return List_Id;
48
49    -----------------------------
50    -- 9.1  Task (also 10.1.3) --
51    -----------------------------
52
53    --  TASK_TYPE_DECLARATION ::=
54    --    task type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART]
55    --      [is [new INTERFACE_LIST with] TASK_DEFINITION];
56
57    --  SINGLE_TASK_DECLARATION ::=
58    --    task DEFINING_IDENTIFIER
59    --      [is [new INTERFACE_LIST with] TASK_DEFINITION];
60
61    --  TASK_BODY ::=
62    --    task body DEFINING_IDENTIFIER is
63    --      DECLARATIVE_PART
64    --    begin
65    --      HANDLED_SEQUENCE_OF_STATEMENTS
66    --    end [task_IDENTIFIER]
67
68    --  TASK_BODY_STUB ::=
69    --    task body DEFINING_IDENTIFIER is separate;
70
71    --  This routine scans out a task declaration, task body, or task stub
72
73    --  The caller has checked that the initial token is TASK and scanned
74    --  past it, so that Token is set to the token after TASK
75
76    --  Error recovery: cannot raise Error_Resync
77
78    function P_Task return Node_Id is
79       Name_Node  : Node_Id;
80       Task_Node  : Node_Id;
81       Task_Sloc  : Source_Ptr;
82
83    begin
84       Push_Scope_Stack;
85       Scope.Table (Scope.Last).Etyp := E_Name;
86       Scope.Table (Scope.Last).Ecol := Start_Column;
87       Scope.Table (Scope.Last).Sloc := Token_Ptr;
88       Scope.Table (Scope.Last).Lreq := False;
89       Task_Sloc := Prev_Token_Ptr;
90
91       if Token = Tok_Body then
92          Scan; -- past BODY
93          Name_Node := P_Defining_Identifier (C_Is);
94          Scope.Table (Scope.Last).Labl := Name_Node;
95
96          if Token = Tok_Left_Paren then
97             Error_Msg_SC ("discriminant part not allowed in task body");
98             Discard_Junk_List (P_Known_Discriminant_Part_Opt);
99          end if;
100
101          TF_Is;
102
103          --  Task stub
104
105          if Token = Tok_Separate then
106             Scan; -- past SEPARATE
107             Task_Node := New_Node (N_Task_Body_Stub, Task_Sloc);
108             Set_Defining_Identifier (Task_Node, Name_Node);
109             TF_Semicolon;
110             Pop_Scope_Stack; -- remove unused entry
111
112          --  Task body
113
114          else
115             Task_Node := New_Node (N_Task_Body, Task_Sloc);
116             Set_Defining_Identifier (Task_Node, Name_Node);
117             Parse_Decls_Begin_End (Task_Node);
118          end if;
119
120          return Task_Node;
121
122       --  Otherwise we must have a task declaration
123
124       else
125          if Token = Tok_Type then
126             Scan; -- past TYPE
127             Task_Node := New_Node (N_Task_Type_Declaration, Task_Sloc);
128             Name_Node := P_Defining_Identifier;
129             Set_Defining_Identifier (Task_Node, Name_Node);
130             Scope.Table (Scope.Last).Labl := Name_Node;
131             Set_Discriminant_Specifications
132               (Task_Node, P_Known_Discriminant_Part_Opt);
133
134          else
135             Task_Node := New_Node (N_Single_Task_Declaration, Task_Sloc);
136             Name_Node := P_Defining_Identifier (C_Is);
137             Set_Defining_Identifier (Task_Node, Name_Node);
138             Scope.Table (Scope.Last).Labl := Name_Node;
139
140             if Token = Tok_Left_Paren then
141                Error_Msg_SC ("discriminant part not allowed for single task");
142                Discard_Junk_List (P_Known_Discriminant_Part_Opt);
143             end if;
144          end if;
145
146          --  Parse optional task definition. Note that P_Task_Definition scans
147          --  out the semicolon as well as the task definition itself.
148
149          if Token = Tok_Semicolon then
150
151             --  A little check, if the next token after semicolon is
152             --  Entry, then surely the semicolon should really be IS
153
154             Scan; -- past semicolon
155
156             if Token = Tok_Entry then
157                Error_Msg_SP ("|"";"" should be IS");
158                Set_Task_Definition (Task_Node, P_Task_Definition);
159             else
160                Pop_Scope_Stack; -- Remove unused entry
161             end if;
162          else
163             TF_Is; -- must have IS if no semicolon
164
165             --  Ada 2005 (AI-345)
166
167             if Token = Tok_New then
168                Scan; --  past NEW
169
170                if Ada_Version < Ada_05 then
171                   Error_Msg_SP ("task interface is an Ada 2005 extension");
172                   Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
173                end if;
174
175                Set_Interface_List (Task_Node, New_List);
176
177                loop
178                   Append (P_Qualified_Simple_Name, Interface_List (Task_Node));
179                   exit when Token /= Tok_And;
180                   Scan; --  past AND
181                end loop;
182
183                if Token /= Tok_With then
184                   Error_Msg_SC ("WITH expected");
185                end if;
186
187                Scan; -- past WITH
188
189                if Token = Tok_Private then
190                   Error_Msg_SP
191                     ("PRIVATE not allowed in task type declaration");
192                end if;
193             end if;
194
195             Set_Task_Definition (Task_Node, P_Task_Definition);
196          end if;
197
198          return Task_Node;
199       end if;
200    end P_Task;
201
202    --------------------------------
203    -- 9.1  Task Type Declaration --
204    --------------------------------
205
206    --  Parsed by P_Task (9.1)
207
208    ----------------------------------
209    -- 9.1  Single Task Declaration --
210    ----------------------------------
211
212    --  Parsed by P_Task (9.1)
213
214    --------------------------
215    -- 9.1  Task Definition --
216    --------------------------
217
218    --  TASK_DEFINITION ::=
219    --      {TASK_ITEM}
220    --    [private
221    --      {TASK_ITEM}]
222    --    end [task_IDENTIFIER];
223
224    --  The caller has already made the scope stack entry
225
226    --  Note: there is a small deviation from official syntax here in that we
227    --  regard the semicolon after end as part of the Task_Definition, and in
228    --  the official syntax, it's part of the enclosing declaration. The reason
229    --  for this deviation is that otherwise the end processing would have to
230    --  be special cased, which would be a nuisance!
231
232    --  Error recovery:  cannot raise Error_Resync
233
234    function P_Task_Definition return Node_Id is
235       Def_Node  : Node_Id;
236
237    begin
238       Def_Node := New_Node (N_Task_Definition, Token_Ptr);
239       Set_Visible_Declarations (Def_Node, P_Task_Items);
240
241       if Token = Tok_Private then
242          Scan; -- past PRIVATE
243          Set_Private_Declarations (Def_Node, P_Task_Items);
244
245          --  Deal gracefully with multiple PRIVATE parts
246
247          while Token = Tok_Private loop
248             Error_Msg_SC ("only one private part allowed per task");
249             Scan; -- past PRIVATE
250             Append_List (P_Task_Items, Private_Declarations (Def_Node));
251          end loop;
252       end if;
253
254       End_Statements (Def_Node);
255       return Def_Node;
256    end P_Task_Definition;
257
258    --------------------
259    -- 9.1  Task Item --
260    --------------------
261
262    --  TASK_ITEM ::= ENTRY_DECLARATION | REPRESENTATION_CLAUSE
263
264    --  This subprogram scans a (possibly empty) list of task items and pragmas
265
266    --  Error recovery:  cannot raise Error_Resync
267
268    --  Note: a pragma can also be returned in this position
269
270    function P_Task_Items return List_Id is
271       Items      : List_Id;
272       Item_Node  : Node_Id;
273       Decl_Sloc  : Source_Ptr;
274
275    begin
276       --  Get rid of active SIS entry from outer scope. This means we will
277       --  miss some nested cases, but it doesn't seem worth the effort. See
278       --  discussion in Par for further details
279
280       SIS_Entry_Active := False;
281
282       --  Loop to scan out task items
283
284       Items := New_List;
285
286       Decl_Loop : loop
287          Decl_Sloc := Token_Ptr;
288
289          if Token = Tok_Pragma then
290             Append (P_Pragma, Items);
291
292          --  Ada 2005 (AI-397): Reserved words NOT and OVERRIDING
293          --  may begin an entry declaration.
294
295          elsif Token = Tok_Entry
296            or else Token = Tok_Not
297            or else Token = Tok_Overriding
298          then
299             Append (P_Entry_Declaration, Items);
300
301          elsif Token = Tok_For then
302             --  Representation clause in task declaration. The only rep
303             --  clause which is legal in a protected is an address clause,
304             --  so that is what we try to scan out.
305
306             Item_Node := P_Representation_Clause;
307
308             if Nkind (Item_Node) = N_At_Clause then
309                Append (Item_Node, Items);
310
311             elsif Nkind (Item_Node) = N_Attribute_Definition_Clause
312               and then Chars (Item_Node) = Name_Address
313             then
314                Append (Item_Node, Items);
315
316             else
317                Error_Msg
318                  ("the only representation clause " &
319                   "allowed here is an address clause!", Decl_Sloc);
320             end if;
321
322          elsif Token = Tok_Identifier
323            or else Token in Token_Class_Declk
324          then
325             Error_Msg_SC ("illegal declaration in task definition");
326             Resync_Past_Semicolon;
327
328          else
329             exit Decl_Loop;
330          end if;
331       end loop Decl_Loop;
332
333       return Items;
334    end P_Task_Items;
335
336    --------------------
337    -- 9.1  Task Body --
338    --------------------
339
340    --  Parsed by P_Task (9.1)
341
342    ----------------------------------
343    -- 9.4  Protected (also 10.1.3) --
344    ----------------------------------
345
346    --  PROTECTED_TYPE_DECLARATION ::=
347    --    protected type DEFINING_IDENTIFIER [KNOWN_DISCRIMINANT_PART]
348    --      is [new INTERFACE_LIST with] PROTECTED_DEFINITION;
349
350    --  SINGLE_PROTECTED_DECLARATION ::=
351    --    protected DEFINING_IDENTIFIER
352    --    is [new INTERFACE_LIST with] PROTECTED_DEFINITION;
353
354    --  PROTECTED_BODY ::=
355    --    protected body DEFINING_IDENTIFIER is
356    --      {PROTECTED_OPERATION_ITEM}
357    --    end [protected_IDENTIFIER];
358
359    --  PROTECTED_BODY_STUB ::=
360    --    protected body DEFINING_IDENTIFIER is separate;
361
362    --  This routine scans out a protected declaration, protected body
363    --  or a protected stub.
364
365    --  The caller has checked that the initial token is PROTECTED and
366    --  scanned past it, so Token is set to the following token.
367
368    --  Error recovery: cannot raise Error_Resync
369
370    function P_Protected return Node_Id is
371       Name_Node      : Node_Id;
372       Protected_Node : Node_Id;
373       Protected_Sloc : Source_Ptr;
374       Scan_State     : Saved_Scan_State;
375
376    begin
377       Push_Scope_Stack;
378       Scope.Table (Scope.Last).Etyp := E_Name;
379       Scope.Table (Scope.Last).Ecol := Start_Column;
380       Scope.Table (Scope.Last).Lreq := False;
381       Protected_Sloc := Prev_Token_Ptr;
382
383       if Token = Tok_Body then
384          Scan; -- past BODY
385          Name_Node := P_Defining_Identifier (C_Is);
386          Scope.Table (Scope.Last).Labl := Name_Node;
387
388          if Token = Tok_Left_Paren then
389             Error_Msg_SC ("discriminant part not allowed in protected body");
390             Discard_Junk_List (P_Known_Discriminant_Part_Opt);
391          end if;
392
393          TF_Is;
394
395          --  Protected stub
396
397          if Token = Tok_Separate then
398             Scan; -- past SEPARATE
399             Protected_Node := New_Node (N_Protected_Body_Stub, Protected_Sloc);
400             Set_Defining_Identifier (Protected_Node, Name_Node);
401             TF_Semicolon;
402             Pop_Scope_Stack; -- remove unused entry
403
404          --  Protected body
405
406          else
407             Protected_Node := New_Node (N_Protected_Body, Protected_Sloc);
408             Set_Defining_Identifier (Protected_Node, Name_Node);
409             Set_Declarations (Protected_Node, P_Protected_Operation_Items);
410             End_Statements (Protected_Node);
411          end if;
412
413          return Protected_Node;
414
415       --  Otherwise we must have a protected declaration
416
417       else
418          if Token = Tok_Type then
419             Scan; -- past TYPE
420             Protected_Node :=
421               New_Node (N_Protected_Type_Declaration, Protected_Sloc);
422             Name_Node := P_Defining_Identifier (C_Is);
423             Set_Defining_Identifier (Protected_Node, Name_Node);
424             Scope.Table (Scope.Last).Labl := Name_Node;
425             Set_Discriminant_Specifications
426               (Protected_Node, P_Known_Discriminant_Part_Opt);
427
428          else
429             Protected_Node :=
430               New_Node (N_Single_Protected_Declaration, Protected_Sloc);
431             Name_Node := P_Defining_Identifier (C_Is);
432             Set_Defining_Identifier (Protected_Node, Name_Node);
433
434             if Token = Tok_Left_Paren then
435                Error_Msg_SC
436                  ("discriminant part not allowed for single protected");
437                Discard_Junk_List (P_Known_Discriminant_Part_Opt);
438             end if;
439
440             Scope.Table (Scope.Last).Labl := Name_Node;
441          end if;
442
443          --  Check for semicolon not followed by IS, this is something like
444
445          --    protected type r;
446
447          --  where we want
448
449          --    protected type r IS END;
450
451          if Token = Tok_Semicolon then
452             Save_Scan_State (Scan_State); -- at semicolon
453             Scan; -- past semicolon
454
455             if Token /= Tok_Is then
456                Restore_Scan_State (Scan_State);
457                Error_Msg_SC ("missing IS");
458                Set_Protected_Definition (Protected_Node,
459                  Make_Protected_Definition (Token_Ptr,
460                    Visible_Declarations => Empty_List,
461                    End_Label           => Empty));
462
463                SIS_Entry_Active := False;
464                End_Statements (Protected_Definition (Protected_Node));
465                Scan; -- past semicolon
466                return Protected_Node;
467             end if;
468
469             Error_Msg_SP ("|extra ""("" ignored");
470          end if;
471
472          T_Is;
473
474          --  Ada 2005 (AI-345)
475
476          if Token = Tok_New then
477             Scan; --  past NEW
478
479             if Ada_Version < Ada_05 then
480                Error_Msg_SP ("task interface is an Ada 2005 extension");
481                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
482             end if;
483
484             Set_Interface_List (Protected_Node, New_List);
485
486             loop
487                Append (P_Qualified_Simple_Name,
488                  Interface_List (Protected_Node));
489
490                exit when Token /= Tok_And;
491                Scan; --  past AND
492             end loop;
493
494             if Token /= Tok_With then
495                Error_Msg_SC ("WITH expected");
496             end if;
497
498             Scan; -- past WITH
499
500             if Token = Tok_Private then
501                Error_Msg_SP
502                  ("PRIVATE not allowed in protected type declaration");
503             end if;
504          end if;
505
506          Set_Protected_Definition (Protected_Node, P_Protected_Definition);
507          return Protected_Node;
508       end if;
509    end P_Protected;
510
511    -------------------------------------
512    -- 9.4  Protected Type Declaration --
513    -------------------------------------
514
515    --  Parsed by P_Protected (9.4)
516
517    ---------------------------------------
518    -- 9.4  Single Protected Declaration --
519    ---------------------------------------
520
521    --  Parsed by P_Protected (9.4)
522
523    -------------------------------
524    -- 9.4  Protected Definition --
525    -------------------------------
526
527    --  PROTECTED_DEFINITION ::=
528    --      {PROTECTED_OPERATION_DECLARATION}
529    --    [private
530    --      {PROTECTED_ELEMENT_DECLARATION}]
531    --    end [protected_IDENTIFIER]
532
533    --  PROTECTED_ELEMENT_DECLARATION ::=
534    --    PROTECTED_OPERATION_DECLARATION
535    --  | COMPONENT_DECLARATION
536
537    --  The caller has already established the scope stack entry
538
539    --  Error recovery: cannot raise Error_Resync
540
541    function P_Protected_Definition return Node_Id is
542       Def_Node  : Node_Id;
543       Item_Node : Node_Id;
544
545    begin
546       Def_Node := New_Node (N_Protected_Definition, Token_Ptr);
547
548       --  Get rid of active SIS entry from outer scope. This means we will
549       --  miss some nested cases, but it doesn't seem worth the effort. See
550       --  discussion in Par for further details
551
552       SIS_Entry_Active := False;
553
554       --  Loop to scan visible declarations (protected operation declarations)
555
556       Set_Visible_Declarations (Def_Node, New_List);
557
558       loop
559          Item_Node := P_Protected_Operation_Declaration_Opt;
560          exit when No (Item_Node);
561          Append (Item_Node, Visible_Declarations (Def_Node));
562       end loop;
563
564       --  Deal with PRIVATE part (including graceful handling
565       --  of multiple PRIVATE parts).
566
567       Private_Loop : while Token = Tok_Private loop
568          if No (Private_Declarations (Def_Node)) then
569             Set_Private_Declarations (Def_Node, New_List);
570          else
571             Error_Msg_SC ("duplicate private part");
572          end if;
573
574          Scan; -- past PRIVATE
575
576          Declaration_Loop : loop
577             if Token = Tok_Identifier then
578                P_Component_Items (Private_Declarations (Def_Node));
579             else
580                Item_Node := P_Protected_Operation_Declaration_Opt;
581                exit Declaration_Loop when No (Item_Node);
582                Append (Item_Node, Private_Declarations (Def_Node));
583             end if;
584          end loop Declaration_Loop;
585       end loop Private_Loop;
586
587       End_Statements (Def_Node);
588       return Def_Node;
589    end P_Protected_Definition;
590
591    ------------------------------------------
592    -- 9.4  Protected Operation Declaration --
593    ------------------------------------------
594
595    --  PROTECTED_OPERATION_DECLARATION ::=
596    --    SUBPROGRAM_DECLARATION
597    --  | ENTRY_DECLARATION
598    --  | REPRESENTATION_CLAUSE
599
600    --  Error recovery: cannot raise Error_Resync
601
602    --  Note: a pragma can also be returned in this position
603
604    --  We are not currently permitting representation clauses to appear as
605    --  protected operation declarations, do we have to rethink this???
606
607    function P_Protected_Operation_Declaration_Opt return Node_Id is
608       L : List_Id;
609       P : Source_Ptr;
610
611       function P_Entry_Or_Subprogram_With_Indicator return Node_Id;
612       --  Ada 2005 (AI-397): Parse an entry or a subprogram with an overriding
613       --  indicator. The caller has checked that the initial token is NOT or
614       --  OVERRIDING.
615
616       ------------------------------------------
617       -- P_Entry_Or_Subprogram_With_Indicator --
618       ------------------------------------------
619
620       function P_Entry_Or_Subprogram_With_Indicator return Node_Id is
621          Decl           : Node_Id := Error;
622          Is_Overriding  : Boolean := False;
623          Not_Overriding : Boolean := False;
624
625       begin
626          if Token = Tok_Not then
627             Scan;  -- past NOT
628
629             if Token = Tok_Overriding then
630                Scan;  -- past OVERRIDING
631                Not_Overriding := True;
632             else
633                Error_Msg_SC ("OVERRIDING expected!");
634             end if;
635
636          else
637             Scan;  -- past OVERRIDING
638             Is_Overriding := True;
639          end if;
640
641          if (Is_Overriding or else Not_Overriding) then
642             if Ada_Version < Ada_05 then
643                Error_Msg_SP ("overriding indicator is an Ada 2005 extension");
644                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
645
646             elsif Token = Tok_Entry then
647                Decl := P_Entry_Declaration;
648
649                Set_Must_Override     (Decl, Is_Overriding);
650                Set_Must_Not_Override (Decl, Not_Overriding);
651
652             elsif Token = Tok_Function or else Token = Tok_Procedure then
653                Decl := P_Subprogram (Pf_Decl);
654
655                Set_Must_Override     (Specification (Decl), Is_Overriding);
656                Set_Must_Not_Override (Specification (Decl), Not_Overriding);
657
658             else
659                Error_Msg_SC ("ENTRY, FUNCTION or PROCEDURE expected!");
660             end if;
661          end if;
662
663          return Decl;
664       end P_Entry_Or_Subprogram_With_Indicator;
665
666    --  Start of processing for P_Protected_Operation_Declaration_Opt
667
668    begin
669       --  This loop runs more than once only when a junk declaration
670       --  is skipped.
671
672       loop
673          if Token = Tok_Pragma then
674             return P_Pragma;
675
676          elsif Token = Tok_Not or else Token = Tok_Overriding then
677             return P_Entry_Or_Subprogram_With_Indicator;
678
679          elsif Token = Tok_Entry then
680             return P_Entry_Declaration;
681
682          elsif Token = Tok_Function or else Token = Tok_Procedure then
683             return P_Subprogram (Pf_Decl);
684
685          elsif Token = Tok_Identifier then
686             L := New_List;
687             P := Token_Ptr;
688             Skip_Declaration (L);
689
690             if Nkind (First (L)) = N_Object_Declaration then
691                Error_Msg
692                  ("component must be declared in private part of " &
693                   "protected type", P);
694             else
695                Error_Msg
696                  ("illegal declaration in protected definition", P);
697             end if;
698
699          elsif Token in Token_Class_Declk then
700             Error_Msg_SC ("illegal declaration in protected definition");
701             Resync_Past_Semicolon;
702
703             --  Return now to avoid cascaded messages if next declaration
704             --  is a valid component declaration.
705
706             return Error;
707
708          elsif Token = Tok_For then
709             Error_Msg_SC
710               ("representation clause not allowed in protected definition");
711             Resync_Past_Semicolon;
712
713          else
714             return Empty;
715          end if;
716       end loop;
717    end P_Protected_Operation_Declaration_Opt;
718
719    -----------------------------------
720    -- 9.4  Protected Operation Item --
721    -----------------------------------
722
723    --  PROTECTED_OPERATION_ITEM ::=
724    --    SUBPROGRAM_DECLARATION
725    --  | SUBPROGRAM_BODY
726    --  | ENTRY_BODY
727    --  | REPRESENTATION_CLAUSE
728
729    --  This procedure parses and returns a list of protected operation items
730
731    --  We are not currently permitting representation clauses to appear
732    --  as protected operation items, do we have to rethink this???
733
734    function P_Protected_Operation_Items return List_Id is
735       Item_List : List_Id;
736
737    begin
738       Item_List := New_List;
739
740       loop
741          if Token = Tok_Entry or else Bad_Spelling_Of (Tok_Entry) then
742             Append (P_Entry_Body, Item_List);
743
744          elsif Token = Tok_Function or else Bad_Spelling_Of (Tok_Function)
745                  or else
746                Token = Tok_Procedure or else Bad_Spelling_Of (Tok_Procedure)
747          then
748             Append (P_Subprogram (Pf_Decl_Pbod), Item_List);
749
750          elsif Token = Tok_Pragma or else Bad_Spelling_Of (Tok_Pragma) then
751             P_Pragmas_Opt (Item_List);
752
753          elsif Token = Tok_Private or else Bad_Spelling_Of (Tok_Private) then
754             Error_Msg_SC ("PRIVATE not allowed in protected body");
755             Scan; -- past PRIVATE
756
757          elsif Token = Tok_Identifier then
758             Error_Msg_SC
759               ("all components must be declared in spec!");
760             Resync_Past_Semicolon;
761
762          elsif Token in Token_Class_Declk then
763             Error_Msg_SC ("this declaration not allowed in protected body");
764             Resync_Past_Semicolon;
765
766          else
767             exit;
768          end if;
769       end loop;
770
771       return Item_List;
772    end P_Protected_Operation_Items;
773
774    ------------------------------
775    -- 9.5.2  Entry Declaration --
776    ------------------------------
777
778    --  ENTRY_DECLARATION ::=
779    --    [OVERRIDING_INDICATOR]
780    --    entry DEFINING_IDENTIFIER [(DISCRETE_SUBTYPE_DEFINITION)]
781    --      PARAMETER_PROFILE;
782
783    --  The caller has checked that the initial token is ENTRY, NOT or
784    --  OVERRIDING.
785
786    --  Error recovery: cannot raise Error_Resync
787
788    function P_Entry_Declaration return Node_Id is
789       Decl_Node  : Node_Id;
790       Scan_State : Saved_Scan_State;
791
792       --  Flags for optional overriding indication. Two flags are needed,
793       --  to distinguish positive and negative overriding indicators from
794       --  the absence of any indicator.
795
796       Is_Overriding  : Boolean := False;
797       Not_Overriding : Boolean := False;
798
799    begin
800       --  Ada 2005 (AI-397): Scan leading overriding indicator
801
802       if Token = Tok_Not then
803          Scan;  -- past NOT
804
805          if Token = Tok_Overriding then
806             Scan;  -- part OVERRIDING
807             Not_Overriding := True;
808          else
809             Error_Msg_SC ("OVERRIDING expected!");
810          end if;
811
812       elsif Token = Tok_Overriding then
813          Scan;  -- part OVERRIDING
814          Is_Overriding := True;
815       end if;
816
817       if (Is_Overriding or else Not_Overriding) then
818          if Ada_Version < Ada_05 then
819             Error_Msg_SP ("overriding indicator is an Ada 2005 extension");
820             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
821
822          elsif Token /= Tok_Entry then
823             Error_Msg_SC ("ENTRY expected!");
824          end if;
825       end if;
826
827       Decl_Node := New_Node (N_Entry_Declaration, Token_Ptr);
828       Scan; -- past ENTRY
829
830       Set_Defining_Identifier
831         (Decl_Node, P_Defining_Identifier (C_Left_Paren_Semicolon));
832
833       --  If left paren, could be (Discrete_Subtype_Definition) or Formal_Part
834
835       if Token = Tok_Left_Paren then
836          Scan; -- past (
837
838          --  If identifier after left paren, could still be either
839
840          if Token = Tok_Identifier then
841             Save_Scan_State (Scan_State); -- at Id
842             Scan; -- past Id
843
844             --  If comma or colon after Id, must be Formal_Part
845
846             if Token = Tok_Comma or else Token = Tok_Colon then
847                Restore_Scan_State (Scan_State); -- to Id
848                Set_Parameter_Specifications (Decl_Node, P_Formal_Part);
849
850             --  Else if Id wi no comma or colon, must be discrete subtype defn
851
852             else
853                Restore_Scan_State (Scan_State); -- to Id
854                Set_Discrete_Subtype_Definition
855                  (Decl_Node, P_Discrete_Subtype_Definition);
856                T_Right_Paren;
857                Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
858             end if;
859
860          --  If no Id, must be discrete subtype definition
861
862          else
863             Set_Discrete_Subtype_Definition
864               (Decl_Node, P_Discrete_Subtype_Definition);
865             T_Right_Paren;
866             Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
867          end if;
868       end if;
869
870       if Is_Overriding then
871          Set_Must_Override (Decl_Node);
872       elsif Not_Overriding then
873          Set_Must_Not_Override (Decl_Node);
874       end if;
875
876       --  Error recovery check for illegal return
877
878       if Token = Tok_Return then
879          Error_Msg_SC ("entry cannot have return value!");
880          Scan;
881          Discard_Junk_Node (P_Subtype_Indication);
882       end if;
883
884       --  Error recovery check for improper use of entry barrier in spec
885
886       if Token = Tok_When then
887          Error_Msg_SC ("barrier not allowed here (belongs in body)");
888          Scan; -- past WHEN;
889          Discard_Junk_Node (P_Expression_No_Right_Paren);
890       end if;
891
892       TF_Semicolon;
893       return Decl_Node;
894
895    exception
896       when Error_Resync =>
897          Resync_Past_Semicolon;
898          return Error;
899    end P_Entry_Declaration;
900
901    -----------------------------
902    -- 9.5.2  Accept Statement --
903    -----------------------------
904
905    --  ACCEPT_STATEMENT ::=
906    --    accept entry_DIRECT_NAME
907    --      [(ENTRY_INDEX)] PARAMETER_PROFILE [do
908    --        HANDLED_SEQUENCE_OF_STATEMENTS
909    --    end [entry_IDENTIFIER]];
910
911    --  The caller has checked that the initial token is ACCEPT
912
913    --  Error recovery: cannot raise Error_Resync. If an error occurs, the
914    --  scan is resynchronized past the next semicolon and control returns.
915
916    function P_Accept_Statement return Node_Id is
917       Scan_State  : Saved_Scan_State;
918       Accept_Node : Node_Id;
919       Hand_Seq    : Node_Id;
920
921    begin
922       Push_Scope_Stack;
923       Scope.Table (Scope.Last).Sloc := Token_Ptr;
924       Scope.Table (Scope.Last).Ecol := Start_Column;
925
926       Accept_Node := New_Node (N_Accept_Statement, Token_Ptr);
927       Scan; -- past ACCEPT
928       Scope.Table (Scope.Last).Labl := Token_Node;
929
930       Set_Entry_Direct_Name (Accept_Node, P_Identifier (C_Do));
931
932       --  Left paren could be (Entry_Index) or Formal_Part, determine which
933
934       if Token = Tok_Left_Paren then
935          Save_Scan_State (Scan_State); -- at left paren
936          Scan; -- past left paren
937
938          --  If first token after left paren not identifier, then Entry_Index
939
940          if Token /= Tok_Identifier then
941             Set_Entry_Index (Accept_Node, P_Expression);
942             T_Right_Paren;
943             Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
944
945          --  First token after left paren is identifier, could be either case
946
947          else -- Token = Tok_Identifier
948             Scan; -- past identifier
949
950             --  If identifier followed by comma or colon, must be Formal_Part
951
952             if Token = Tok_Comma or else Token = Tok_Colon then
953                Restore_Scan_State (Scan_State); -- to left paren
954                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
955
956             --  If identifier not followed by comma/colon, must be entry index
957
958             else
959                Restore_Scan_State (Scan_State); -- to left paren
960                Scan; -- past left paren (again!)
961                Set_Entry_Index (Accept_Node, P_Expression);
962                T_Right_Paren;
963                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
964             end if;
965          end if;
966       end if;
967
968       --  Scan out DO if present
969
970       if Token = Tok_Do then
971          Scope.Table (Scope.Last).Etyp := E_Name;
972          Scope.Table (Scope.Last).Lreq := False;
973          Scan; -- past DO
974          Hand_Seq := P_Handled_Sequence_Of_Statements;
975          Set_Handled_Statement_Sequence (Accept_Node, Hand_Seq);
976          End_Statements (Handled_Statement_Sequence (Accept_Node));
977
978          --  Exception handlers not allowed in Ada 95 node
979
980          if Present (Exception_Handlers (Hand_Seq)) then
981             if Ada_Version = Ada_83 then
982                Error_Msg_N
983                  ("(Ada 83) exception handlers in accept not allowed",
984                   First_Non_Pragma (Exception_Handlers (Hand_Seq)));
985             end if;
986          end if;
987
988       else
989          Pop_Scope_Stack; -- discard unused entry
990          TF_Semicolon;
991       end if;
992
993       return Accept_Node;
994
995    --  If error, resynchronize past semicolon
996
997    exception
998       when Error_Resync =>
999          Resync_Past_Semicolon;
1000          Pop_Scope_Stack; -- discard unused entry
1001          return Error;
1002
1003    end P_Accept_Statement;
1004
1005    ------------------------
1006    -- 9.5.2  Entry Index --
1007    ------------------------
1008
1009    --  Parsed by P_Expression (4.4)
1010
1011    -----------------------
1012    -- 9.5.2  Entry Body --
1013    -----------------------
1014
1015    --  ENTRY_BODY ::=
1016    --    entry DEFINING_IDENTIFIER ENTRY_BODY_FORMAL_PART ENTRY_BARRIER is
1017    --      DECLARATIVE_PART
1018    --    begin
1019    --      HANDLED_SEQUENCE_OF_STATEMENTS
1020    --    end [entry_IDENTIFIER];
1021
1022    --  The caller has checked that the initial token is ENTRY
1023
1024    --  Error_Recovery: cannot raise Error_Resync
1025
1026    function P_Entry_Body return Node_Id is
1027       Entry_Node       : Node_Id;
1028       Formal_Part_Node : Node_Id;
1029       Name_Node        : Node_Id;
1030
1031    begin
1032       Push_Scope_Stack;
1033       Entry_Node := New_Node (N_Entry_Body, Token_Ptr);
1034       Scan; -- past ENTRY
1035
1036       Scope.Table (Scope.Last).Ecol := Start_Column;
1037       Scope.Table (Scope.Last).Lreq := False;
1038       Scope.Table (Scope.Last).Etyp := E_Name;
1039
1040       Name_Node := P_Defining_Identifier;
1041       Set_Defining_Identifier (Entry_Node, Name_Node);
1042       Scope.Table (Scope.Last).Labl := Name_Node;
1043
1044       Formal_Part_Node := P_Entry_Body_Formal_Part;
1045       Set_Entry_Body_Formal_Part (Entry_Node, Formal_Part_Node);
1046
1047       Set_Condition (Formal_Part_Node, P_Entry_Barrier);
1048       Parse_Decls_Begin_End (Entry_Node);
1049       return Entry_Node;
1050    end P_Entry_Body;
1051
1052    -----------------------------------
1053    -- 9.5.2  Entry Body Formal Part --
1054    -----------------------------------
1055
1056    --  ENTRY_BODY_FORMAL_PART ::=
1057    --    [(ENTRY_INDEX_SPECIFICATION)] [PARAMETER_PART]
1058
1059    --  Error_Recovery: cannot raise Error_Resync
1060
1061    function P_Entry_Body_Formal_Part return Node_Id is
1062       Fpart_Node : Node_Id;
1063       Scan_State : Saved_Scan_State;
1064
1065    begin
1066       Fpart_Node := New_Node (N_Entry_Body_Formal_Part, Token_Ptr);
1067
1068       --  See if entry index specification present, and if so parse it
1069
1070       if Token = Tok_Left_Paren then
1071          Save_Scan_State (Scan_State); -- at left paren
1072          Scan; -- past left paren
1073
1074          if Token = Tok_For then
1075             Set_Entry_Index_Specification
1076               (Fpart_Node, P_Entry_Index_Specification);
1077             T_Right_Paren;
1078          else
1079             Restore_Scan_State (Scan_State); -- to left paren
1080          end if;
1081
1082       --  Check for (common?) case of left paren omitted before FOR. This
1083       --  is a tricky case, because the corresponding missing left paren
1084       --  can cause real havoc if a formal part is present which gets
1085       --  treated as part of the discrete subtype definition of the
1086       --  entry index specification, so just give error and resynchronize
1087
1088       elsif Token = Tok_For then
1089          T_Left_Paren; -- to give error message
1090          Resync_To_When;
1091       end if;
1092
1093       Set_Parameter_Specifications (Fpart_Node, P_Parameter_Profile);
1094       return Fpart_Node;
1095    end P_Entry_Body_Formal_Part;
1096
1097    --------------------------
1098    -- 9.5.2  Entry Barrier --
1099    --------------------------
1100
1101    --  ENTRY_BARRIER ::= when CONDITION
1102
1103    --  Error_Recovery: cannot raise Error_Resync
1104
1105    function P_Entry_Barrier return Node_Id is
1106       Bnode : Node_Id;
1107
1108    begin
1109       if Token = Tok_When then
1110          Scan; -- past WHEN;
1111          Bnode := P_Expression_No_Right_Paren;
1112
1113          if Token = Tok_Colon_Equal then
1114             Error_Msg_SC ("|"":="" should be ""=""");
1115             Scan;
1116             Bnode := P_Expression_No_Right_Paren;
1117          end if;
1118
1119       else
1120          T_When; -- to give error message
1121          Bnode := Error;
1122       end if;
1123
1124       TF_Is;
1125       return Bnode;
1126    end P_Entry_Barrier;
1127
1128    --------------------------------------
1129    -- 9.5.2  Entry Index Specification --
1130    --------------------------------------
1131
1132    --  ENTRY_INDEX_SPECIFICATION ::=
1133    --    for DEFINING_IDENTIFIER in DISCRETE_SUBTYPE_DEFINITION
1134
1135    --  Error recovery: can raise Error_Resync
1136
1137    function P_Entry_Index_Specification return Node_Id is
1138       Iterator_Node : Node_Id;
1139
1140    begin
1141       Iterator_Node := New_Node (N_Entry_Index_Specification, Token_Ptr);
1142       T_For; -- past FOR
1143       Set_Defining_Identifier (Iterator_Node, P_Defining_Identifier (C_In));
1144       T_In;
1145       Set_Discrete_Subtype_Definition
1146         (Iterator_Node, P_Discrete_Subtype_Definition);
1147       return Iterator_Node;
1148    end P_Entry_Index_Specification;
1149
1150    ---------------------------------
1151    -- 9.5.3  Entry Call Statement --
1152    ---------------------------------
1153
1154    --  Parsed by P_Name (4.1). Within a select, an entry call is parsed
1155    --  by P_Select_Statement (9.7)
1156
1157    ------------------------------
1158    -- 9.5.4  Requeue Statement --
1159    ------------------------------
1160
1161    --  REQUEUE_STATEMENT ::= requeue entry_NAME [with abort];
1162
1163    --  The caller has checked that the initial token is requeue
1164
1165    --  Error recovery: can raise Error_Resync
1166
1167    function P_Requeue_Statement return Node_Id is
1168       Requeue_Node : Node_Id;
1169
1170    begin
1171       Requeue_Node := New_Node (N_Requeue_Statement, Token_Ptr);
1172       Scan; -- past REQUEUE
1173       Set_Name (Requeue_Node, P_Name);
1174
1175       if Token = Tok_With then
1176          Scan; -- past WITH
1177          T_Abort;
1178          Set_Abort_Present (Requeue_Node, True);
1179       end if;
1180
1181       TF_Semicolon;
1182       return Requeue_Node;
1183    end P_Requeue_Statement;
1184
1185    --------------------------
1186    -- 9.6  Delay Statement --
1187    --------------------------
1188
1189    --  DELAY_STATEMENT ::=
1190    --    DELAY_UNTIL_STATEMENT
1191    --  | DELAY_RELATIVE_STATEMENT
1192
1193    --  The caller has checked that the initial token is DELAY
1194
1195    --  Error recovery: cannot raise Error_Resync
1196
1197    function P_Delay_Statement return Node_Id is
1198    begin
1199       Scan; -- past DELAY
1200
1201       --  The following check for delay until misused in Ada 83 doesn't catch
1202       --  all cases, but it's good enough to catch most of them!
1203
1204       if Token_Name = Name_Until then
1205          Check_95_Keyword (Tok_Until, Tok_Left_Paren);
1206          Check_95_Keyword (Tok_Until, Tok_Identifier);
1207       end if;
1208
1209       if Token = Tok_Until then
1210          return P_Delay_Until_Statement;
1211       else
1212          return P_Delay_Relative_Statement;
1213       end if;
1214    end P_Delay_Statement;
1215
1216    --------------------------------
1217    -- 9.6  Delay Until Statement --
1218    --------------------------------
1219
1220    --  DELAY_UNTIL_STATEMENT ::= delay until delay_EXPRESSION;
1221
1222    --  The caller has checked that the initial token is DELAY, scanned it
1223    --  out and checked that the current token is UNTIL
1224
1225    --  Error recovery: cannot raise Error_Resync
1226
1227    function P_Delay_Until_Statement return Node_Id is
1228       Delay_Node : Node_Id;
1229
1230    begin
1231       Delay_Node := New_Node (N_Delay_Until_Statement, Prev_Token_Ptr);
1232       Scan; -- past UNTIL
1233       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1234       TF_Semicolon;
1235       return Delay_Node;
1236    end P_Delay_Until_Statement;
1237
1238    -----------------------------------
1239    -- 9.6  Delay Relative Statement --
1240    -----------------------------------
1241
1242    --  DELAY_RELATIVE_STATEMENT ::= delay delay_EXPRESSION;
1243
1244    --  The caller has checked that the initial token is DELAY, scanned it
1245    --  out and determined that the current token is not UNTIL
1246
1247    --  Error recovery: cannot raise Error_Resync
1248
1249    function P_Delay_Relative_Statement return Node_Id is
1250       Delay_Node : Node_Id;
1251
1252    begin
1253       Delay_Node := New_Node (N_Delay_Relative_Statement, Prev_Token_Ptr);
1254       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1255       Check_Simple_Expression_In_Ada_83 (Expression (Delay_Node));
1256       TF_Semicolon;
1257       return Delay_Node;
1258    end P_Delay_Relative_Statement;
1259
1260    ---------------------------
1261    -- 9.7  Select Statement --
1262    ---------------------------
1263
1264    --  SELECT_STATEMENT ::=
1265    --    SELECTIVE_ACCEPT
1266    --  | TIMED_ENTRY_CALL
1267    --  | CONDITIONAL_ENTRY_CALL
1268    --  | ASYNCHRONOUS_SELECT
1269
1270    --  SELECTIVE_ACCEPT ::=
1271    --    select
1272    --      [GUARD]
1273    --        SELECT_ALTERNATIVE
1274    --    {or
1275    --      [GUARD]
1276    --        SELECT_ALTERNATIVE
1277    --    [else
1278    --      SEQUENCE_OF_STATEMENTS]
1279    --    end select;
1280
1281    --  GUARD ::= when CONDITION =>
1282
1283    --  Note: the guard preceding a select alternative is included as part
1284    --  of the node generated for a selective accept alternative.
1285
1286    --  SELECT_ALTERNATIVE ::=
1287    --    ACCEPT_ALTERNATIVE
1288    --  | DELAY_ALTERNATIVE
1289    --  | TERMINATE_ALTERNATIVE
1290
1291    --  TIMED_ENTRY_CALL ::=
1292    --    select
1293    --      ENTRY_CALL_ALTERNATIVE
1294    --    or
1295    --      DELAY_ALTERNATIVE
1296    --    end select;
1297
1298    --  CONDITIONAL_ENTRY_CALL ::=
1299    --    select
1300    --      ENTRY_CALL_ALTERNATIVE
1301    --    else
1302    --      SEQUENCE_OF_STATEMENTS
1303    --    end select;
1304
1305    --  ENTRY_CALL_ALTERNATIVE ::=
1306    --    ENTRY_CALL_STATEMENT [SEQUENCE_OF_STATEMENTS]
1307
1308    --  ASYNCHRONOUS_SELECT ::=
1309    --    select
1310    --      TRIGGERING_ALTERNATIVE
1311    --    then abort
1312    --      ABORTABLE_PART
1313    --    end select;
1314
1315    --  TRIGGERING_ALTERNATIVE ::=
1316    --    TRIGGERING_STATEMENT [SEQUENCE_OF_STATEMENTS]
1317
1318    --  TRIGGERING_STATEMENT ::= ENTRY_CALL_STATEMENT | DELAY_STATEMENT
1319
1320    --  The caller has checked that the initial token is SELECT
1321
1322    --  Error recovery: can raise Error_Resync
1323
1324    function P_Select_Statement return Node_Id is
1325       Select_Node    : Node_Id;
1326       Select_Sloc    : Source_Ptr;
1327       Stmnt_Sloc     : Source_Ptr;
1328       Ecall_Node     : Node_Id;
1329       Alternative    : Node_Id;
1330       Select_Pragmas : List_Id;
1331       Alt_Pragmas    : List_Id;
1332       Statement_List : List_Id;
1333       Alt_List       : List_Id;
1334       Cond_Expr      : Node_Id;
1335       Delay_Stmnt    : Node_Id;
1336
1337    begin
1338       Push_Scope_Stack;
1339       Scope.Table (Scope.Last).Etyp := E_Select;
1340       Scope.Table (Scope.Last).Ecol := Start_Column;
1341       Scope.Table (Scope.Last).Sloc := Token_Ptr;
1342       Scope.Table (Scope.Last).Labl := Error;
1343
1344       Select_Sloc := Token_Ptr;
1345       Scan; -- past SELECT
1346       Stmnt_Sloc := Token_Ptr;
1347       Select_Pragmas := P_Pragmas_Opt;
1348
1349       --  If first token after select is designator, then we have an entry
1350       --  call, which must be the start of a conditional entry call, timed
1351       --  entry call or asynchronous select
1352
1353       if Token in Token_Class_Desig then
1354
1355          --  Scan entry call statement
1356
1357          begin
1358             Ecall_Node := P_Name;
1359
1360             --  ??  The following two clauses exactly parallel code in ch5
1361             --      and should be commoned sometime
1362
1363             if Nkind (Ecall_Node) = N_Indexed_Component then
1364                declare
1365                   Prefix_Node : constant Node_Id := Prefix (Ecall_Node);
1366                   Exprs_Node  : constant List_Id := Expressions (Ecall_Node);
1367
1368                begin
1369                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1370                   Set_Name (Ecall_Node, Prefix_Node);
1371                   Set_Parameter_Associations (Ecall_Node, Exprs_Node);
1372                end;
1373
1374             elsif Nkind (Ecall_Node) = N_Function_Call then
1375                declare
1376                   Fname_Node  : constant Node_Id := Name (Ecall_Node);
1377                   Params_List : constant List_Id :=
1378                                   Parameter_Associations (Ecall_Node);
1379
1380                begin
1381                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1382                   Set_Name (Ecall_Node, Fname_Node);
1383                   Set_Parameter_Associations (Ecall_Node, Params_List);
1384                end;
1385
1386             elsif Nkind (Ecall_Node) = N_Identifier
1387               or else Nkind (Ecall_Node) = N_Selected_Component
1388             then
1389                --  Case of a call to a parameterless entry
1390
1391                declare
1392                   C_Node : constant Node_Id :=
1393                          New_Node (N_Procedure_Call_Statement, Stmnt_Sloc);
1394                begin
1395                   Set_Name (C_Node, Ecall_Node);
1396                   Set_Parameter_Associations (C_Node, No_List);
1397                   Ecall_Node := C_Node;
1398                end;
1399             end if;
1400
1401             TF_Semicolon;
1402
1403          exception
1404             when Error_Resync =>
1405                Resync_Past_Semicolon;
1406                return Error;
1407          end;
1408
1409          Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1410
1411          --  OR follows, we have a timed entry call
1412
1413          if Token = Tok_Or then
1414             Scan; -- past OR
1415             Alt_Pragmas := P_Pragmas_Opt;
1416
1417             Select_Node := New_Node (N_Timed_Entry_Call, Select_Sloc);
1418             Set_Entry_Call_Alternative (Select_Node,
1419               Make_Entry_Call_Alternative (Stmnt_Sloc,
1420                 Entry_Call_Statement => Ecall_Node,
1421                 Pragmas_Before       => Select_Pragmas,
1422                 Statements           => Statement_List));
1423
1424             --  Only possibility is delay alternative. If we have anything
1425             --  else, give message, and treat as conditional entry call.
1426
1427             if Token /= Tok_Delay then
1428                Error_Msg_SC
1429                  ("only allowed alternative in timed entry call is delay!");
1430                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1431                Set_Delay_Alternative (Select_Node, Error);
1432
1433             else
1434                Set_Delay_Alternative (Select_Node, P_Delay_Alternative);
1435                Set_Pragmas_Before
1436                  (Delay_Alternative (Select_Node), Alt_Pragmas);
1437             end if;
1438
1439          --  ELSE follows, we have a conditional entry call
1440
1441          elsif Token = Tok_Else then
1442             Scan; -- past ELSE
1443             Select_Node := New_Node (N_Conditional_Entry_Call, Select_Sloc);
1444
1445             Set_Entry_Call_Alternative (Select_Node,
1446               Make_Entry_Call_Alternative (Stmnt_Sloc,
1447                 Entry_Call_Statement => Ecall_Node,
1448                 Pragmas_Before       => Select_Pragmas,
1449                 Statements           => Statement_List));
1450
1451             Set_Else_Statements
1452               (Select_Node, P_Sequence_Of_Statements (SS_Sreq));
1453
1454          --  Only remaining case is THEN ABORT (asynchronous select)
1455
1456          elsif Token = Tok_Abort then
1457             Select_Node :=
1458               Make_Asynchronous_Select (Select_Sloc,
1459                 Triggering_Alternative =>
1460                   Make_Triggering_Alternative (Stmnt_Sloc,
1461                     Triggering_Statement => Ecall_Node,
1462                     Pragmas_Before       => Select_Pragmas,
1463                     Statements           => Statement_List),
1464                 Abortable_Part => P_Abortable_Part);
1465
1466          --  Else error
1467
1468          else
1469             if Ada_Version = Ada_83 then
1470                Error_Msg_BC ("OR or ELSE expected");
1471             else
1472                Error_Msg_BC ("OR or ELSE or THEN ABORT expected");
1473             end if;
1474
1475             Select_Node := Error;
1476          end if;
1477
1478          End_Statements;
1479
1480       --  Here we have a selective accept or an an asynchronous select (first
1481       --  token after SELECT is other than a designator token).
1482
1483       else
1484          --  If we have delay with no guard, could be asynchronous select
1485
1486          if Token = Tok_Delay then
1487             Delay_Stmnt := P_Delay_Statement;
1488             Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1489
1490             --  Asynchronous select
1491
1492             if Token = Tok_Abort then
1493                Select_Node :=
1494                  Make_Asynchronous_Select (Select_Sloc,
1495                    Triggering_Alternative =>
1496                      Make_Triggering_Alternative (Stmnt_Sloc,
1497                        Triggering_Statement => Delay_Stmnt,
1498                        Pragmas_Before       => Select_Pragmas,
1499                        Statements           => Statement_List),
1500                      Abortable_Part => P_Abortable_Part);
1501
1502                End_Statements;
1503                return Select_Node;
1504
1505             --  Delay which was not an asynchronous select. Must be a selective
1506             --  accept, and since at least one accept statement is required,
1507             --  we must have at least one OR phrase present.
1508
1509             else
1510                Alt_List := New_List (
1511                  Make_Delay_Alternative (Stmnt_Sloc,
1512                    Delay_Statement => Delay_Stmnt,
1513                    Pragmas_Before  => Select_Pragmas,
1514                    Statements      => Statement_List));
1515                T_Or;
1516                Alt_Pragmas := P_Pragmas_Opt;
1517             end if;
1518
1519          --  If not a delay statement, then must be another possibility for
1520          --  a selective accept alternative, or perhaps a guard is present
1521
1522          else
1523             Alt_List := New_List;
1524             Alt_Pragmas := Select_Pragmas;
1525          end if;
1526
1527          Select_Node := New_Node (N_Selective_Accept, Select_Sloc);
1528          Set_Select_Alternatives (Select_Node, Alt_List);
1529
1530          --  Scan out selective accept alternatives. On entry to this loop,
1531          --  we are just past a SELECT or OR token, and any pragmas that
1532          --  immediately follow the SELECT or OR are in Alt_Pragmas.
1533
1534          loop
1535             if Token = Tok_When then
1536
1537                if Present (Alt_Pragmas) then
1538                   Error_Msg_SC ("pragmas may not precede guard");
1539                end if;
1540
1541                Scan; --  past WHEN
1542                Cond_Expr := P_Expression_No_Right_Paren;
1543                T_Arrow;
1544                Alt_Pragmas := P_Pragmas_Opt;
1545
1546             else
1547                Cond_Expr := Empty;
1548             end if;
1549
1550             if Token = Tok_Accept then
1551                Alternative := P_Accept_Alternative;
1552
1553                --  Check for junk attempt at asynchronous select using
1554                --  an Accept alternative as the triggering statement
1555
1556                if Token = Tok_Abort
1557                  and then Is_Empty_List (Alt_List)
1558                  and then No (Cond_Expr)
1559                then
1560                   Error_Msg
1561                     ("triggering statement must be entry call or delay",
1562                      Sloc (Alternative));
1563                   Scan; -- past junk ABORT
1564                   Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1565                   End_Statements;
1566                   return Error;
1567                end if;
1568
1569             elsif Token = Tok_Delay then
1570                Alternative := P_Delay_Alternative;
1571
1572             elsif Token = Tok_Terminate then
1573                Alternative := P_Terminate_Alternative;
1574
1575             else
1576                Error_Msg_SC
1577                  ("select alternative (ACCEPT, ABORT, DELAY) expected");
1578                Alternative := Error;
1579
1580                if Token = Tok_Semicolon then
1581                   Scan; -- past junk semicolon
1582                end if;
1583             end if;
1584
1585             --  THEN ABORT at this stage is just junk
1586
1587             if Token = Tok_Abort then
1588                Error_Msg_SP ("misplaced `THEN ABORT`");
1589                Scan; -- past junk ABORT
1590                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1591                End_Statements;
1592                return Error;
1593
1594             else
1595                if Alternative /= Error then
1596                   Set_Condition (Alternative, Cond_Expr);
1597                   Set_Pragmas_Before (Alternative, Alt_Pragmas);
1598                   Append (Alternative, Alt_List);
1599                end if;
1600
1601                exit when Token /= Tok_Or;
1602             end if;
1603
1604             T_Or;
1605             Alt_Pragmas := P_Pragmas_Opt;
1606          end loop;
1607
1608          if Token = Tok_Else then
1609             Scan; -- past ELSE
1610             Set_Else_Statements
1611               (Select_Node, P_Sequence_Of_Statements (SS_Ortm_Sreq));
1612
1613             if Token = Tok_Or then
1614                Error_Msg_SC ("select alternative cannot follow else part!");
1615             end if;
1616          end if;
1617
1618          End_Statements;
1619       end if;
1620
1621       return Select_Node;
1622    end P_Select_Statement;
1623
1624    -----------------------------
1625    -- 9.7.1  Selective Accept --
1626    -----------------------------
1627
1628    --  Parsed by P_Select_Statement (9.7)
1629
1630    ------------------
1631    -- 9.7.1  Guard --
1632    ------------------
1633
1634    --  Parsed by P_Select_Statement (9.7)
1635
1636    -------------------------------
1637    -- 9.7.1  Select Alternative --
1638    -------------------------------
1639
1640    --  SELECT_ALTERNATIVE ::=
1641    --    ACCEPT_ALTERNATIVE
1642    --  | DELAY_ALTERNATIVE
1643    --  | TERMINATE_ALTERNATIVE
1644
1645    --  Note: the guard preceding a select alternative is included as part
1646    --  of the node generated for a selective accept alternative.
1647
1648    --  Error recovery: cannot raise Error_Resync
1649
1650    -------------------------------
1651    -- 9.7.1  Accept Alternative --
1652    -------------------------------
1653
1654    --  ACCEPT_ALTERNATIVE ::=
1655    --    ACCEPT_STATEMENT [SEQUENCE_OF_STATEMENTS]
1656
1657    --  Error_Recovery: Cannot raise Error_Resync
1658
1659    --  Note: the caller is responsible for setting the Pragmas_Before
1660    --  field of the returned N_Terminate_Alternative node.
1661
1662    function P_Accept_Alternative return Node_Id is
1663       Accept_Alt_Node : Node_Id;
1664
1665    begin
1666       Accept_Alt_Node := New_Node (N_Accept_Alternative, Token_Ptr);
1667       Set_Accept_Statement (Accept_Alt_Node, P_Accept_Statement);
1668
1669       --  Note: the reason that we accept THEN ABORT as a terminator for
1670       --  the sequence of statements is for error recovery which allows
1671       --  for misuse of an accept statement as a triggering statememt.
1672
1673       Set_Statements
1674         (Accept_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1675       return Accept_Alt_Node;
1676    end P_Accept_Alternative;
1677
1678    ------------------------------
1679    -- 9.7.1  Delay Alternative --
1680    ------------------------------
1681
1682    --  DELAY_ALTERNATIVE ::=
1683    --    DELAY_STATEMENT [SEQUENCE_OF_STATEMENTS]
1684
1685    --  Error_Recovery: Cannot raise Error_Resync
1686
1687    --  Note: the caller is responsible for setting the Pragmas_Before
1688    --  field of the returned N_Terminate_Alternative node.
1689
1690    function P_Delay_Alternative return Node_Id is
1691       Delay_Alt_Node : Node_Id;
1692
1693    begin
1694       Delay_Alt_Node := New_Node (N_Delay_Alternative, Token_Ptr);
1695       Set_Delay_Statement (Delay_Alt_Node, P_Delay_Statement);
1696
1697       --  Note: the reason that we accept THEN ABORT as a terminator for
1698       --  the sequence of statements is for error recovery which allows
1699       --  for misuse of an accept statement as a triggering statememt.
1700
1701       Set_Statements
1702         (Delay_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1703       return Delay_Alt_Node;
1704    end P_Delay_Alternative;
1705
1706    ----------------------------------
1707    -- 9.7.1  Terminate Alternative --
1708    ----------------------------------
1709
1710    --  TERMINATE_ALTERNATIVE ::= terminate;
1711
1712    --  Error_Recovery: Cannot raise Error_Resync
1713
1714    --  Note: the caller is responsible for setting the Pragmas_Before
1715    --  field of the returned N_Terminate_Alternative node.
1716
1717    function P_Terminate_Alternative return Node_Id is
1718       Terminate_Alt_Node : Node_Id;
1719
1720    begin
1721       Terminate_Alt_Node := New_Node (N_Terminate_Alternative, Token_Ptr);
1722       Scan; -- past TERMINATE
1723       TF_Semicolon;
1724
1725       --  For all other select alternatives, the sequence of statements
1726       --  after the alternative statement will swallow up any pragmas
1727       --  coming in this position. But the terminate alternative has no
1728       --  sequence of statements, so the pragmas here must be treated
1729       --  specially.
1730
1731       Set_Pragmas_After (Terminate_Alt_Node, P_Pragmas_Opt);
1732       return Terminate_Alt_Node;
1733    end P_Terminate_Alternative;
1734
1735    -----------------------------
1736    -- 9.7.2  Timed Entry Call --
1737    -----------------------------
1738
1739    --  Parsed by P_Select_Statement (9.7)
1740
1741    -----------------------------------
1742    -- 9.7.2  Entry Call Alternative --
1743    -----------------------------------
1744
1745    --  Parsed by P_Select_Statement (9.7)
1746
1747    -----------------------------------
1748    -- 9.7.3  Conditional Entry Call --
1749    -----------------------------------
1750
1751    --  Parsed by P_Select_Statement (9.7)
1752
1753    --------------------------------
1754    -- 9.7.4  Asynchronous Select --
1755    --------------------------------
1756
1757    --  Parsed by P_Select_Statement (9.7)
1758
1759    -----------------------------------
1760    -- 9.7.4  Triggering Alternative --
1761    -----------------------------------
1762
1763    --  Parsed by P_Select_Statement (9.7)
1764
1765    ---------------------------------
1766    -- 9.7.4  Triggering Statement --
1767    ---------------------------------
1768
1769    --  Parsed by P_Select_Statement (9.7)
1770
1771    ---------------------------
1772    -- 9.7.4  Abortable Part --
1773    ---------------------------
1774
1775    --  ABORTABLE_PART ::= SEQUENCE_OF_STATEMENTS
1776
1777    --  The caller has verified that THEN ABORT is present, and Token is
1778    --  pointing to the ABORT on entry (or if not, then we have an error)
1779
1780    --  Error recovery: cannot raise Error_Resync
1781
1782    function P_Abortable_Part return Node_Id is
1783       Abortable_Part_Node : Node_Id;
1784
1785    begin
1786       Abortable_Part_Node := New_Node (N_Abortable_Part, Token_Ptr);
1787       T_Abort; -- scan past ABORT
1788
1789       if Ada_Version = Ada_83 then
1790          Error_Msg_SP ("(Ada 83) asynchronous select not allowed!");
1791       end if;
1792
1793       Set_Statements (Abortable_Part_Node, P_Sequence_Of_Statements (SS_Sreq));
1794       return Abortable_Part_Node;
1795    end P_Abortable_Part;
1796
1797    --------------------------
1798    -- 9.8  Abort Statement --
1799    --------------------------
1800
1801    --  ABORT_STATEMENT ::= abort task_NAME {, task_NAME};
1802
1803    --  The caller has checked that the initial token is ABORT
1804
1805    --  Error recovery: cannot raise Error_Resync
1806
1807    function P_Abort_Statement return Node_Id is
1808       Abort_Node : Node_Id;
1809
1810    begin
1811       Abort_Node := New_Node (N_Abort_Statement, Token_Ptr);
1812       Scan; -- past ABORT
1813       Set_Names (Abort_Node, New_List);
1814
1815       loop
1816          Append (P_Name, Names (Abort_Node));
1817          exit when Token /= Tok_Comma;
1818          Scan; -- past comma
1819       end loop;
1820
1821       TF_Semicolon;
1822       return Abort_Node;
1823    end P_Abort_Statement;
1824
1825 end Ch9;