OSDN Git Service

2009-04-17 Gary Dismukes <dismukes@adacore.com>
[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-2008, 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 ("protected 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          end if;
500
501          Set_Protected_Definition (Protected_Node, P_Protected_Definition);
502          return Protected_Node;
503       end if;
504    end P_Protected;
505
506    -------------------------------------
507    -- 9.4  Protected Type Declaration --
508    -------------------------------------
509
510    --  Parsed by P_Protected (9.4)
511
512    ---------------------------------------
513    -- 9.4  Single Protected Declaration --
514    ---------------------------------------
515
516    --  Parsed by P_Protected (9.4)
517
518    -------------------------------
519    -- 9.4  Protected Definition --
520    -------------------------------
521
522    --  PROTECTED_DEFINITION ::=
523    --      {PROTECTED_OPERATION_DECLARATION}
524    --    [private
525    --      {PROTECTED_ELEMENT_DECLARATION}]
526    --    end [protected_IDENTIFIER]
527
528    --  PROTECTED_ELEMENT_DECLARATION ::=
529    --    PROTECTED_OPERATION_DECLARATION
530    --  | COMPONENT_DECLARATION
531
532    --  The caller has already established the scope stack entry
533
534    --  Error recovery: cannot raise Error_Resync
535
536    function P_Protected_Definition return Node_Id is
537       Def_Node  : Node_Id;
538       Item_Node : Node_Id;
539
540    begin
541       Def_Node := New_Node (N_Protected_Definition, Token_Ptr);
542
543       --  Get rid of active SIS entry from outer scope. This means we will
544       --  miss some nested cases, but it doesn't seem worth the effort. See
545       --  discussion in Par for further details
546
547       SIS_Entry_Active := False;
548
549       --  Loop to scan visible declarations (protected operation declarations)
550
551       Set_Visible_Declarations (Def_Node, New_List);
552
553       loop
554          Item_Node := P_Protected_Operation_Declaration_Opt;
555          exit when No (Item_Node);
556          Append (Item_Node, Visible_Declarations (Def_Node));
557       end loop;
558
559       --  Deal with PRIVATE part (including graceful handling of multiple
560       --  PRIVATE parts).
561
562       Private_Loop : while Token = Tok_Private loop
563          if No (Private_Declarations (Def_Node)) then
564             Set_Private_Declarations (Def_Node, New_List);
565          else
566             Error_Msg_SC ("duplicate private part");
567          end if;
568
569          Scan; -- past PRIVATE
570
571          Declaration_Loop : loop
572             if Token = Tok_Identifier then
573                P_Component_Items (Private_Declarations (Def_Node));
574             else
575                Item_Node := P_Protected_Operation_Declaration_Opt;
576                exit Declaration_Loop when No (Item_Node);
577                Append (Item_Node, Private_Declarations (Def_Node));
578             end if;
579          end loop Declaration_Loop;
580       end loop Private_Loop;
581
582       End_Statements (Def_Node);
583       return Def_Node;
584    end P_Protected_Definition;
585
586    ------------------------------------------
587    -- 9.4  Protected Operation Declaration --
588    ------------------------------------------
589
590    --  PROTECTED_OPERATION_DECLARATION ::=
591    --    SUBPROGRAM_DECLARATION
592    --  | ENTRY_DECLARATION
593    --  | REPRESENTATION_CLAUSE
594
595    --  Error recovery: cannot raise Error_Resync
596
597    --  Note: a pragma can also be returned in this position
598
599    --  We are not currently permitting representation clauses to appear as
600    --  protected operation declarations, do we have to rethink this???
601
602    function P_Protected_Operation_Declaration_Opt return Node_Id is
603       L : List_Id;
604       P : Source_Ptr;
605
606       function P_Entry_Or_Subprogram_With_Indicator return Node_Id;
607       --  Ada 2005 (AI-397): Parse an entry or a subprogram with an overriding
608       --  indicator. The caller has checked that the initial token is NOT or
609       --  OVERRIDING.
610
611       ------------------------------------------
612       -- P_Entry_Or_Subprogram_With_Indicator --
613       ------------------------------------------
614
615       function P_Entry_Or_Subprogram_With_Indicator return Node_Id is
616          Decl           : Node_Id := Error;
617          Is_Overriding  : Boolean := False;
618          Not_Overriding : Boolean := False;
619
620       begin
621          if Token = Tok_Not then
622             Scan;  -- past NOT
623
624             if Token = Tok_Overriding then
625                Scan;  -- past OVERRIDING
626                Not_Overriding := True;
627             else
628                Error_Msg_SC ("OVERRIDING expected!");
629             end if;
630
631          else
632             Scan;  -- past OVERRIDING
633             Is_Overriding := True;
634          end if;
635
636          if (Is_Overriding or else Not_Overriding) then
637             if Ada_Version < Ada_05 then
638                Error_Msg_SP ("overriding indicator is an Ada 2005 extension");
639                Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
640
641             elsif Token = Tok_Entry then
642                Decl := P_Entry_Declaration;
643
644                Set_Must_Override     (Decl, Is_Overriding);
645                Set_Must_Not_Override (Decl, Not_Overriding);
646
647             elsif Token = Tok_Function or else Token = Tok_Procedure then
648                Decl := P_Subprogram (Pf_Decl);
649
650                Set_Must_Override     (Specification (Decl), Is_Overriding);
651                Set_Must_Not_Override (Specification (Decl), Not_Overriding);
652
653             else
654                Error_Msg_SC ("ENTRY, FUNCTION or PROCEDURE expected!");
655             end if;
656          end if;
657
658          return Decl;
659       end P_Entry_Or_Subprogram_With_Indicator;
660
661    --  Start of processing for P_Protected_Operation_Declaration_Opt
662
663    begin
664       --  This loop runs more than once only when a junk declaration
665       --  is skipped.
666
667       loop
668          if Token = Tok_Pragma then
669             return P_Pragma;
670
671          elsif Token = Tok_Not or else Token = Tok_Overriding then
672             return P_Entry_Or_Subprogram_With_Indicator;
673
674          elsif Token = Tok_Entry then
675             return P_Entry_Declaration;
676
677          elsif Token = Tok_Function or else Token = Tok_Procedure then
678             return P_Subprogram (Pf_Decl);
679
680          elsif Token = Tok_Identifier then
681             L := New_List;
682             P := Token_Ptr;
683             Skip_Declaration (L);
684
685             if Nkind (First (L)) = N_Object_Declaration then
686                Error_Msg
687                  ("component must be declared in private part of " &
688                   "protected type", P);
689             else
690                Error_Msg
691                  ("illegal declaration in protected definition", P);
692             end if;
693
694          elsif Token in Token_Class_Declk then
695             Error_Msg_SC ("illegal declaration in protected definition");
696             Resync_Past_Semicolon;
697
698             --  Return now to avoid cascaded messages if next declaration
699             --  is a valid component declaration.
700
701             return Error;
702
703          elsif Token = Tok_For then
704             Error_Msg_SC
705               ("representation clause not allowed in protected definition");
706             Resync_Past_Semicolon;
707
708          else
709             return Empty;
710          end if;
711       end loop;
712    end P_Protected_Operation_Declaration_Opt;
713
714    -----------------------------------
715    -- 9.4  Protected Operation Item --
716    -----------------------------------
717
718    --  PROTECTED_OPERATION_ITEM ::=
719    --    SUBPROGRAM_DECLARATION
720    --  | SUBPROGRAM_BODY
721    --  | ENTRY_BODY
722    --  | REPRESENTATION_CLAUSE
723
724    --  This procedure parses and returns a list of protected operation items
725
726    --  We are not currently permitting representation clauses to appear
727    --  as protected operation items, do we have to rethink this???
728
729    function P_Protected_Operation_Items return List_Id is
730       Item_List : List_Id;
731
732    begin
733       Item_List := New_List;
734
735       loop
736          if Token = Tok_Entry or else Bad_Spelling_Of (Tok_Entry) then
737             Append (P_Entry_Body, Item_List);
738
739          --  If the operation starts with procedure, function, or an overriding
740          --  indicator ("overriding" or "not overriding"), parse a subprogram.
741
742          elsif Token = Tok_Function or else Bad_Spelling_Of (Tok_Function)
743                  or else
744                Token = Tok_Procedure or else Bad_Spelling_Of (Tok_Procedure)
745                  or else
746                Token = Tok_Overriding or else Bad_Spelling_Of (Tok_Overriding)
747                  or else
748                Token = Tok_Not or else Bad_Spelling_Of (Tok_Not)
749          then
750             Append (P_Subprogram (Pf_Decl_Pbod), Item_List);
751
752          elsif Token = Tok_Pragma or else Bad_Spelling_Of (Tok_Pragma) then
753             P_Pragmas_Opt (Item_List);
754
755          elsif Token = Tok_Private or else Bad_Spelling_Of (Tok_Private) then
756             Error_Msg_SC ("PRIVATE not allowed in protected body");
757             Scan; -- past PRIVATE
758
759          elsif Token = Tok_Identifier then
760             Error_Msg_SC
761               ("all components must be declared in spec!");
762             Resync_Past_Semicolon;
763
764          elsif Token in Token_Class_Declk then
765             Error_Msg_SC ("this declaration not allowed in protected body");
766             Resync_Past_Semicolon;
767
768          else
769             exit;
770          end if;
771       end loop;
772
773       return Item_List;
774    end P_Protected_Operation_Items;
775
776    ------------------------------
777    -- 9.5.2  Entry Declaration --
778    ------------------------------
779
780    --  ENTRY_DECLARATION ::=
781    --    [OVERRIDING_INDICATOR]
782    --    entry DEFINING_IDENTIFIER [(DISCRETE_SUBTYPE_DEFINITION)]
783    --      PARAMETER_PROFILE;
784
785    --  The caller has checked that the initial token is ENTRY, NOT or
786    --  OVERRIDING.
787
788    --  Error recovery: cannot raise Error_Resync
789
790    function P_Entry_Declaration return Node_Id is
791       Decl_Node  : Node_Id;
792       Scan_State : Saved_Scan_State;
793
794       --  Flags for optional overriding indication. Two flags are needed,
795       --  to distinguish positive and negative overriding indicators from
796       --  the absence of any indicator.
797
798       Is_Overriding  : Boolean := False;
799       Not_Overriding : Boolean := False;
800
801    begin
802       --  Ada 2005 (AI-397): Scan leading overriding indicator
803
804       if Token = Tok_Not then
805          Scan;  -- past NOT
806
807          if Token = Tok_Overriding then
808             Scan;  -- part OVERRIDING
809             Not_Overriding := True;
810          else
811             Error_Msg_SC ("OVERRIDING expected!");
812          end if;
813
814       elsif Token = Tok_Overriding then
815          Scan;  -- part OVERRIDING
816          Is_Overriding := True;
817       end if;
818
819       if (Is_Overriding or else Not_Overriding) then
820          if Ada_Version < Ada_05 then
821             Error_Msg_SP ("overriding indicator is an Ada 2005 extension");
822             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
823
824          elsif Token /= Tok_Entry then
825             Error_Msg_SC ("ENTRY expected!");
826          end if;
827       end if;
828
829       Decl_Node := New_Node (N_Entry_Declaration, Token_Ptr);
830       Scan; -- past ENTRY
831
832       Set_Defining_Identifier
833         (Decl_Node, P_Defining_Identifier (C_Left_Paren_Semicolon));
834
835       --  If left paren, could be (Discrete_Subtype_Definition) or Formal_Part
836
837       if Token = Tok_Left_Paren then
838          Scan; -- past (
839
840          --  If identifier after left paren, could still be either
841
842          if Token = Tok_Identifier then
843             Save_Scan_State (Scan_State); -- at Id
844             Scan; -- past Id
845
846             --  If comma or colon after Id, must be Formal_Part
847
848             if Token = Tok_Comma or else Token = Tok_Colon then
849                Restore_Scan_State (Scan_State); -- to Id
850                Set_Parameter_Specifications (Decl_Node, P_Formal_Part);
851
852             --  Else if Id without comma or colon, must be discrete subtype
853             --  defn
854
855             else
856                Restore_Scan_State (Scan_State); -- to Id
857                Set_Discrete_Subtype_Definition
858                  (Decl_Node, P_Discrete_Subtype_Definition);
859                T_Right_Paren;
860                Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
861             end if;
862
863          --  If no Id, must be discrete subtype definition
864
865          else
866             Set_Discrete_Subtype_Definition
867               (Decl_Node, P_Discrete_Subtype_Definition);
868             T_Right_Paren;
869             Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
870          end if;
871       end if;
872
873       if Is_Overriding then
874          Set_Must_Override (Decl_Node);
875       elsif Not_Overriding then
876          Set_Must_Not_Override (Decl_Node);
877       end if;
878
879       --  Error recovery check for illegal return
880
881       if Token = Tok_Return then
882          Error_Msg_SC ("entry cannot have return value!");
883          Scan;
884          Discard_Junk_Node (P_Subtype_Indication);
885       end if;
886
887       --  Error recovery check for improper use of entry barrier in spec
888
889       if Token = Tok_When then
890          Error_Msg_SC ("barrier not allowed here (belongs in body)");
891          Scan; -- past WHEN;
892          Discard_Junk_Node (P_Expression_No_Right_Paren);
893       end if;
894
895       TF_Semicolon;
896       return Decl_Node;
897
898    exception
899       when Error_Resync =>
900          Resync_Past_Semicolon;
901          return Error;
902    end P_Entry_Declaration;
903
904    -----------------------------
905    -- 9.5.2  Accept Statement --
906    -----------------------------
907
908    --  ACCEPT_STATEMENT ::=
909    --    accept entry_DIRECT_NAME
910    --      [(ENTRY_INDEX)] PARAMETER_PROFILE [do
911    --        HANDLED_SEQUENCE_OF_STATEMENTS
912    --    end [entry_IDENTIFIER]];
913
914    --  The caller has checked that the initial token is ACCEPT
915
916    --  Error recovery: cannot raise Error_Resync. If an error occurs, the
917    --  scan is resynchronized past the next semicolon and control returns.
918
919    function P_Accept_Statement return Node_Id is
920       Scan_State  : Saved_Scan_State;
921       Accept_Node : Node_Id;
922       Hand_Seq    : Node_Id;
923
924    begin
925       Push_Scope_Stack;
926       Scope.Table (Scope.Last).Sloc := Token_Ptr;
927       Scope.Table (Scope.Last).Ecol := Start_Column;
928
929       Accept_Node := New_Node (N_Accept_Statement, Token_Ptr);
930       Scan; -- past ACCEPT
931       Scope.Table (Scope.Last).Labl := Token_Node;
932
933       Set_Entry_Direct_Name (Accept_Node, P_Identifier (C_Do));
934
935       --  Left paren could be (Entry_Index) or Formal_Part, determine which
936
937       if Token = Tok_Left_Paren then
938          Save_Scan_State (Scan_State); -- at left paren
939          Scan; -- past left paren
940
941          --  If first token after left paren not identifier, then Entry_Index
942
943          if Token /= Tok_Identifier then
944             Set_Entry_Index (Accept_Node, P_Expression);
945             T_Right_Paren;
946             Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
947
948          --  First token after left paren is identifier, could be either case
949
950          else -- Token = Tok_Identifier
951             Scan; -- past identifier
952
953             --  If identifier followed by comma or colon, must be Formal_Part
954
955             if Token = Tok_Comma or else Token = Tok_Colon then
956                Restore_Scan_State (Scan_State); -- to left paren
957                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
958
959             --  If identifier not followed by comma/colon, must be entry index
960
961             else
962                Restore_Scan_State (Scan_State); -- to left paren
963                Scan; -- past left paren (again!)
964                Set_Entry_Index (Accept_Node, P_Expression);
965                T_Right_Paren;
966                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
967             end if;
968          end if;
969       end if;
970
971       --  Scan out DO if present
972
973       if Token = Tok_Do then
974          Scope.Table (Scope.Last).Etyp := E_Name;
975          Scope.Table (Scope.Last).Lreq := False;
976          Scan; -- past DO
977          Hand_Seq := P_Handled_Sequence_Of_Statements;
978          Set_Handled_Statement_Sequence (Accept_Node, Hand_Seq);
979          End_Statements (Handled_Statement_Sequence (Accept_Node));
980
981          --  Exception handlers not allowed in Ada 95 node
982
983          if Present (Exception_Handlers (Hand_Seq)) then
984             if Ada_Version = Ada_83 then
985                Error_Msg_N
986                  ("(Ada 83) exception handlers in accept not allowed",
987                   First_Non_Pragma (Exception_Handlers (Hand_Seq)));
988             end if;
989          end if;
990
991       else
992          Pop_Scope_Stack; -- discard unused entry
993          TF_Semicolon;
994       end if;
995
996       return Accept_Node;
997
998    --  If error, resynchronize past semicolon
999
1000    exception
1001       when Error_Resync =>
1002          Resync_Past_Semicolon;
1003          Pop_Scope_Stack; -- discard unused entry
1004          return Error;
1005
1006    end P_Accept_Statement;
1007
1008    ------------------------
1009    -- 9.5.2  Entry Index --
1010    ------------------------
1011
1012    --  Parsed by P_Expression (4.4)
1013
1014    -----------------------
1015    -- 9.5.2  Entry Body --
1016    -----------------------
1017
1018    --  ENTRY_BODY ::=
1019    --    entry DEFINING_IDENTIFIER ENTRY_BODY_FORMAL_PART ENTRY_BARRIER is
1020    --      DECLARATIVE_PART
1021    --    begin
1022    --      HANDLED_SEQUENCE_OF_STATEMENTS
1023    --    end [entry_IDENTIFIER];
1024
1025    --  The caller has checked that the initial token is ENTRY
1026
1027    --  Error_Recovery: cannot raise Error_Resync
1028
1029    function P_Entry_Body return Node_Id is
1030       Entry_Node       : Node_Id;
1031       Formal_Part_Node : Node_Id;
1032       Name_Node        : Node_Id;
1033
1034    begin
1035       Push_Scope_Stack;
1036       Entry_Node := New_Node (N_Entry_Body, Token_Ptr);
1037       Scan; -- past ENTRY
1038
1039       Scope.Table (Scope.Last).Ecol := Start_Column;
1040       Scope.Table (Scope.Last).Lreq := False;
1041       Scope.Table (Scope.Last).Etyp := E_Name;
1042
1043       Name_Node := P_Defining_Identifier;
1044       Set_Defining_Identifier (Entry_Node, Name_Node);
1045       Scope.Table (Scope.Last).Labl := Name_Node;
1046
1047       Formal_Part_Node := P_Entry_Body_Formal_Part;
1048       Set_Entry_Body_Formal_Part (Entry_Node, Formal_Part_Node);
1049
1050       Set_Condition (Formal_Part_Node, P_Entry_Barrier);
1051       Parse_Decls_Begin_End (Entry_Node);
1052       return Entry_Node;
1053    end P_Entry_Body;
1054
1055    -----------------------------------
1056    -- 9.5.2  Entry Body Formal Part --
1057    -----------------------------------
1058
1059    --  ENTRY_BODY_FORMAL_PART ::=
1060    --    [(ENTRY_INDEX_SPECIFICATION)] [PARAMETER_PART]
1061
1062    --  Error_Recovery: cannot raise Error_Resync
1063
1064    function P_Entry_Body_Formal_Part return Node_Id is
1065       Fpart_Node : Node_Id;
1066       Scan_State : Saved_Scan_State;
1067
1068    begin
1069       Fpart_Node := New_Node (N_Entry_Body_Formal_Part, Token_Ptr);
1070
1071       --  See if entry index specification present, and if so parse it
1072
1073       if Token = Tok_Left_Paren then
1074          Save_Scan_State (Scan_State); -- at left paren
1075          Scan; -- past left paren
1076
1077          if Token = Tok_For then
1078             Set_Entry_Index_Specification
1079               (Fpart_Node, P_Entry_Index_Specification);
1080             T_Right_Paren;
1081          else
1082             Restore_Scan_State (Scan_State); -- to left paren
1083          end if;
1084
1085       --  Check for (common?) case of left paren omitted before FOR. This
1086       --  is a tricky case, because the corresponding missing left paren
1087       --  can cause real havoc if a formal part is present which gets
1088       --  treated as part of the discrete subtype definition of the
1089       --  entry index specification, so just give error and resynchronize
1090
1091       elsif Token = Tok_For then
1092          T_Left_Paren; -- to give error message
1093          Resync_To_When;
1094       end if;
1095
1096       Set_Parameter_Specifications (Fpart_Node, P_Parameter_Profile);
1097       return Fpart_Node;
1098    end P_Entry_Body_Formal_Part;
1099
1100    --------------------------
1101    -- 9.5.2  Entry Barrier --
1102    --------------------------
1103
1104    --  ENTRY_BARRIER ::= when CONDITION
1105
1106    --  Error_Recovery: cannot raise Error_Resync
1107
1108    function P_Entry_Barrier return Node_Id is
1109       Bnode : Node_Id;
1110
1111    begin
1112       if Token = Tok_When then
1113          Scan; -- past WHEN;
1114          Bnode := P_Expression_No_Right_Paren;
1115
1116          if Token = Tok_Colon_Equal then
1117             Error_Msg_SC ("|"":="" should be ""=""");
1118             Scan;
1119             Bnode := P_Expression_No_Right_Paren;
1120          end if;
1121
1122       else
1123          T_When; -- to give error message
1124          Bnode := Error;
1125       end if;
1126
1127       TF_Is;
1128       return Bnode;
1129    end P_Entry_Barrier;
1130
1131    --------------------------------------
1132    -- 9.5.2  Entry Index Specification --
1133    --------------------------------------
1134
1135    --  ENTRY_INDEX_SPECIFICATION ::=
1136    --    for DEFINING_IDENTIFIER in DISCRETE_SUBTYPE_DEFINITION
1137
1138    --  Error recovery: can raise Error_Resync
1139
1140    function P_Entry_Index_Specification return Node_Id is
1141       Iterator_Node : Node_Id;
1142
1143    begin
1144       Iterator_Node := New_Node (N_Entry_Index_Specification, Token_Ptr);
1145       T_For; -- past FOR
1146       Set_Defining_Identifier (Iterator_Node, P_Defining_Identifier (C_In));
1147       T_In;
1148       Set_Discrete_Subtype_Definition
1149         (Iterator_Node, P_Discrete_Subtype_Definition);
1150       return Iterator_Node;
1151    end P_Entry_Index_Specification;
1152
1153    ---------------------------------
1154    -- 9.5.3  Entry Call Statement --
1155    ---------------------------------
1156
1157    --  Parsed by P_Name (4.1). Within a select, an entry call is parsed
1158    --  by P_Select_Statement (9.7)
1159
1160    ------------------------------
1161    -- 9.5.4  Requeue Statement --
1162    ------------------------------
1163
1164    --  REQUEUE_STATEMENT ::= requeue entry_NAME [with abort];
1165
1166    --  The caller has checked that the initial token is requeue
1167
1168    --  Error recovery: can raise Error_Resync
1169
1170    function P_Requeue_Statement return Node_Id is
1171       Requeue_Node : Node_Id;
1172
1173    begin
1174       Requeue_Node := New_Node (N_Requeue_Statement, Token_Ptr);
1175       Scan; -- past REQUEUE
1176       Set_Name (Requeue_Node, P_Name);
1177
1178       if Token = Tok_With then
1179          Scan; -- past WITH
1180          T_Abort;
1181          Set_Abort_Present (Requeue_Node, True);
1182       end if;
1183
1184       TF_Semicolon;
1185       return Requeue_Node;
1186    end P_Requeue_Statement;
1187
1188    --------------------------
1189    -- 9.6  Delay Statement --
1190    --------------------------
1191
1192    --  DELAY_STATEMENT ::=
1193    --    DELAY_UNTIL_STATEMENT
1194    --  | DELAY_RELATIVE_STATEMENT
1195
1196    --  The caller has checked that the initial token is DELAY
1197
1198    --  Error recovery: cannot raise Error_Resync
1199
1200    function P_Delay_Statement return Node_Id is
1201    begin
1202       Scan; -- past DELAY
1203
1204       --  The following check for delay until misused in Ada 83 doesn't catch
1205       --  all cases, but it's good enough to catch most of them!
1206
1207       if Token_Name = Name_Until then
1208          Check_95_Keyword (Tok_Until, Tok_Left_Paren);
1209          Check_95_Keyword (Tok_Until, Tok_Identifier);
1210       end if;
1211
1212       if Token = Tok_Until then
1213          return P_Delay_Until_Statement;
1214       else
1215          return P_Delay_Relative_Statement;
1216       end if;
1217    end P_Delay_Statement;
1218
1219    --------------------------------
1220    -- 9.6  Delay Until Statement --
1221    --------------------------------
1222
1223    --  DELAY_UNTIL_STATEMENT ::= delay until delay_EXPRESSION;
1224
1225    --  The caller has checked that the initial token is DELAY, scanned it
1226    --  out and checked that the current token is UNTIL
1227
1228    --  Error recovery: cannot raise Error_Resync
1229
1230    function P_Delay_Until_Statement return Node_Id is
1231       Delay_Node : Node_Id;
1232
1233    begin
1234       Delay_Node := New_Node (N_Delay_Until_Statement, Prev_Token_Ptr);
1235       Scan; -- past UNTIL
1236       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1237       TF_Semicolon;
1238       return Delay_Node;
1239    end P_Delay_Until_Statement;
1240
1241    -----------------------------------
1242    -- 9.6  Delay Relative Statement --
1243    -----------------------------------
1244
1245    --  DELAY_RELATIVE_STATEMENT ::= delay delay_EXPRESSION;
1246
1247    --  The caller has checked that the initial token is DELAY, scanned it
1248    --  out and determined that the current token is not UNTIL
1249
1250    --  Error recovery: cannot raise Error_Resync
1251
1252    function P_Delay_Relative_Statement return Node_Id is
1253       Delay_Node : Node_Id;
1254
1255    begin
1256       Delay_Node := New_Node (N_Delay_Relative_Statement, Prev_Token_Ptr);
1257       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1258       Check_Simple_Expression_In_Ada_83 (Expression (Delay_Node));
1259       TF_Semicolon;
1260       return Delay_Node;
1261    end P_Delay_Relative_Statement;
1262
1263    ---------------------------
1264    -- 9.7  Select Statement --
1265    ---------------------------
1266
1267    --  SELECT_STATEMENT ::=
1268    --    SELECTIVE_ACCEPT
1269    --  | TIMED_ENTRY_CALL
1270    --  | CONDITIONAL_ENTRY_CALL
1271    --  | ASYNCHRONOUS_SELECT
1272
1273    --  SELECTIVE_ACCEPT ::=
1274    --    select
1275    --      [GUARD]
1276    --        SELECT_ALTERNATIVE
1277    --    {or
1278    --      [GUARD]
1279    --        SELECT_ALTERNATIVE
1280    --    [else
1281    --      SEQUENCE_OF_STATEMENTS]
1282    --    end select;
1283
1284    --  GUARD ::= when CONDITION =>
1285
1286    --  Note: the guard preceding a select alternative is included as part
1287    --  of the node generated for a selective accept alternative.
1288
1289    --  SELECT_ALTERNATIVE ::=
1290    --    ACCEPT_ALTERNATIVE
1291    --  | DELAY_ALTERNATIVE
1292    --  | TERMINATE_ALTERNATIVE
1293
1294    --  TIMED_ENTRY_CALL ::=
1295    --    select
1296    --      ENTRY_CALL_ALTERNATIVE
1297    --    or
1298    --      DELAY_ALTERNATIVE
1299    --    end select;
1300
1301    --  CONDITIONAL_ENTRY_CALL ::=
1302    --    select
1303    --      ENTRY_CALL_ALTERNATIVE
1304    --    else
1305    --      SEQUENCE_OF_STATEMENTS
1306    --    end select;
1307
1308    --  ENTRY_CALL_ALTERNATIVE ::=
1309    --    ENTRY_CALL_STATEMENT [SEQUENCE_OF_STATEMENTS]
1310
1311    --  ASYNCHRONOUS_SELECT ::=
1312    --    select
1313    --      TRIGGERING_ALTERNATIVE
1314    --    then abort
1315    --      ABORTABLE_PART
1316    --    end select;
1317
1318    --  TRIGGERING_ALTERNATIVE ::=
1319    --    TRIGGERING_STATEMENT [SEQUENCE_OF_STATEMENTS]
1320
1321    --  TRIGGERING_STATEMENT ::= ENTRY_CALL_STATEMENT | DELAY_STATEMENT
1322
1323    --  The caller has checked that the initial token is SELECT
1324
1325    --  Error recovery: can raise Error_Resync
1326
1327    function P_Select_Statement return Node_Id is
1328       Select_Node    : Node_Id;
1329       Select_Sloc    : Source_Ptr;
1330       Stmnt_Sloc     : Source_Ptr;
1331       Ecall_Node     : Node_Id;
1332       Alternative    : Node_Id;
1333       Select_Pragmas : List_Id;
1334       Alt_Pragmas    : List_Id;
1335       Statement_List : List_Id;
1336       Alt_List       : List_Id;
1337       Cond_Expr      : Node_Id;
1338       Delay_Stmnt    : Node_Id;
1339
1340    begin
1341       Push_Scope_Stack;
1342       Scope.Table (Scope.Last).Etyp := E_Select;
1343       Scope.Table (Scope.Last).Ecol := Start_Column;
1344       Scope.Table (Scope.Last).Sloc := Token_Ptr;
1345       Scope.Table (Scope.Last).Labl := Error;
1346
1347       Select_Sloc := Token_Ptr;
1348       Scan; -- past SELECT
1349       Stmnt_Sloc := Token_Ptr;
1350       Select_Pragmas := P_Pragmas_Opt;
1351
1352       --  If first token after select is designator, then we have an entry
1353       --  call, which must be the start of a conditional entry call, timed
1354       --  entry call or asynchronous select
1355
1356       if Token in Token_Class_Desig then
1357
1358          --  Scan entry call statement
1359
1360          begin
1361             Ecall_Node := P_Name;
1362
1363             --  ??  The following two clauses exactly parallel code in ch5
1364             --      and should be combined sometime
1365
1366             if Nkind (Ecall_Node) = N_Indexed_Component then
1367                declare
1368                   Prefix_Node : constant Node_Id := Prefix (Ecall_Node);
1369                   Exprs_Node  : constant List_Id := Expressions (Ecall_Node);
1370
1371                begin
1372                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1373                   Set_Name (Ecall_Node, Prefix_Node);
1374                   Set_Parameter_Associations (Ecall_Node, Exprs_Node);
1375                end;
1376
1377             elsif Nkind (Ecall_Node) = N_Function_Call then
1378                declare
1379                   Fname_Node  : constant Node_Id := Name (Ecall_Node);
1380                   Params_List : constant List_Id :=
1381                                   Parameter_Associations (Ecall_Node);
1382
1383                begin
1384                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1385                   Set_Name (Ecall_Node, Fname_Node);
1386                   Set_Parameter_Associations (Ecall_Node, Params_List);
1387                end;
1388
1389             elsif Nkind (Ecall_Node) = N_Identifier
1390               or else Nkind (Ecall_Node) = N_Selected_Component
1391             then
1392                --  Case of a call to a parameterless entry
1393
1394                declare
1395                   C_Node : constant Node_Id :=
1396                          New_Node (N_Procedure_Call_Statement, Stmnt_Sloc);
1397                begin
1398                   Set_Name (C_Node, Ecall_Node);
1399                   Set_Parameter_Associations (C_Node, No_List);
1400                   Ecall_Node := C_Node;
1401                end;
1402             end if;
1403
1404             TF_Semicolon;
1405
1406          exception
1407             when Error_Resync =>
1408                Resync_Past_Semicolon;
1409                return Error;
1410          end;
1411
1412          Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1413
1414          --  OR follows, we have a timed entry call
1415
1416          if Token = Tok_Or then
1417             Scan; -- past OR
1418             Alt_Pragmas := P_Pragmas_Opt;
1419
1420             Select_Node := New_Node (N_Timed_Entry_Call, Select_Sloc);
1421             Set_Entry_Call_Alternative (Select_Node,
1422               Make_Entry_Call_Alternative (Stmnt_Sloc,
1423                 Entry_Call_Statement => Ecall_Node,
1424                 Pragmas_Before       => Select_Pragmas,
1425                 Statements           => Statement_List));
1426
1427             --  Only possibility is delay alternative. If we have anything
1428             --  else, give message, and treat as conditional entry call.
1429
1430             if Token /= Tok_Delay then
1431                Error_Msg_SC
1432                  ("only allowed alternative in timed entry call is delay!");
1433                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1434                Set_Delay_Alternative (Select_Node, Error);
1435
1436             else
1437                Set_Delay_Alternative (Select_Node, P_Delay_Alternative);
1438                Set_Pragmas_Before
1439                  (Delay_Alternative (Select_Node), Alt_Pragmas);
1440             end if;
1441
1442          --  ELSE follows, we have a conditional entry call
1443
1444          elsif Token = Tok_Else then
1445             Scan; -- past ELSE
1446             Select_Node := New_Node (N_Conditional_Entry_Call, Select_Sloc);
1447
1448             Set_Entry_Call_Alternative (Select_Node,
1449               Make_Entry_Call_Alternative (Stmnt_Sloc,
1450                 Entry_Call_Statement => Ecall_Node,
1451                 Pragmas_Before       => Select_Pragmas,
1452                 Statements           => Statement_List));
1453
1454             Set_Else_Statements
1455               (Select_Node, P_Sequence_Of_Statements (SS_Sreq));
1456
1457          --  Only remaining case is THEN ABORT (asynchronous select)
1458
1459          elsif Token = Tok_Abort then
1460             Select_Node :=
1461               Make_Asynchronous_Select (Select_Sloc,
1462                 Triggering_Alternative =>
1463                   Make_Triggering_Alternative (Stmnt_Sloc,
1464                     Triggering_Statement => Ecall_Node,
1465                     Pragmas_Before       => Select_Pragmas,
1466                     Statements           => Statement_List),
1467                 Abortable_Part => P_Abortable_Part);
1468
1469          --  Else error
1470
1471          else
1472             if Ada_Version = Ada_83 then
1473                Error_Msg_BC ("OR or ELSE expected");
1474             else
1475                Error_Msg_BC ("OR or ELSE or THEN ABORT expected");
1476             end if;
1477
1478             Select_Node := Error;
1479          end if;
1480
1481          End_Statements;
1482
1483       --  Here we have a selective accept or an asynchronous select (first
1484       --  token after SELECT is other than a designator token).
1485
1486       else
1487          --  If we have delay with no guard, could be asynchronous select
1488
1489          if Token = Tok_Delay then
1490             Delay_Stmnt := P_Delay_Statement;
1491             Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1492
1493             --  Asynchronous select
1494
1495             if Token = Tok_Abort then
1496                Select_Node :=
1497                  Make_Asynchronous_Select (Select_Sloc,
1498                    Triggering_Alternative =>
1499                      Make_Triggering_Alternative (Stmnt_Sloc,
1500                        Triggering_Statement => Delay_Stmnt,
1501                        Pragmas_Before       => Select_Pragmas,
1502                        Statements           => Statement_List),
1503                      Abortable_Part => P_Abortable_Part);
1504
1505                End_Statements;
1506                return Select_Node;
1507
1508             --  Delay which was not an asynchronous select. Must be a selective
1509             --  accept, and since at least one accept statement is required,
1510             --  we must have at least one OR phrase present.
1511
1512             else
1513                Alt_List := New_List (
1514                  Make_Delay_Alternative (Stmnt_Sloc,
1515                    Delay_Statement => Delay_Stmnt,
1516                    Pragmas_Before  => Select_Pragmas,
1517                    Statements      => Statement_List));
1518                T_Or;
1519                Alt_Pragmas := P_Pragmas_Opt;
1520             end if;
1521
1522          --  If not a delay statement, then must be another possibility for
1523          --  a selective accept alternative, or perhaps a guard is present
1524
1525          else
1526             Alt_List := New_List;
1527             Alt_Pragmas := Select_Pragmas;
1528          end if;
1529
1530          Select_Node := New_Node (N_Selective_Accept, Select_Sloc);
1531          Set_Select_Alternatives (Select_Node, Alt_List);
1532
1533          --  Scan out selective accept alternatives. On entry to this loop,
1534          --  we are just past a SELECT or OR token, and any pragmas that
1535          --  immediately follow the SELECT or OR are in Alt_Pragmas.
1536
1537          loop
1538             if Token = Tok_When then
1539
1540                if Present (Alt_Pragmas) then
1541                   Error_Msg_SC ("pragmas may not precede guard");
1542                end if;
1543
1544                Scan; --  past WHEN
1545                Cond_Expr := P_Expression_No_Right_Paren;
1546                T_Arrow;
1547                Alt_Pragmas := P_Pragmas_Opt;
1548
1549             else
1550                Cond_Expr := Empty;
1551             end if;
1552
1553             if Token = Tok_Accept then
1554                Alternative := P_Accept_Alternative;
1555
1556                --  Check for junk attempt at asynchronous select using
1557                --  an Accept alternative as the triggering statement
1558
1559                if Token = Tok_Abort
1560                  and then Is_Empty_List (Alt_List)
1561                  and then No (Cond_Expr)
1562                then
1563                   Error_Msg
1564                     ("triggering statement must be entry call or delay",
1565                      Sloc (Alternative));
1566                   Scan; -- past junk ABORT
1567                   Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1568                   End_Statements;
1569                   return Error;
1570                end if;
1571
1572             elsif Token = Tok_Delay then
1573                Alternative := P_Delay_Alternative;
1574
1575             elsif Token = Tok_Terminate then
1576                Alternative := P_Terminate_Alternative;
1577
1578             else
1579                Error_Msg_SC
1580                  ("select alternative (ACCEPT, ABORT, DELAY) expected");
1581                Alternative := Error;
1582
1583                if Token = Tok_Semicolon then
1584                   Scan; -- past junk semicolon
1585                end if;
1586             end if;
1587
1588             --  THEN ABORT at this stage is just junk
1589
1590             if Token = Tok_Abort then
1591                Error_Msg_SP ("misplaced `THEN ABORT`");
1592                Scan; -- past junk ABORT
1593                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1594                End_Statements;
1595                return Error;
1596
1597             else
1598                if Alternative /= Error then
1599                   Set_Condition (Alternative, Cond_Expr);
1600                   Set_Pragmas_Before (Alternative, Alt_Pragmas);
1601                   Append (Alternative, Alt_List);
1602                end if;
1603
1604                exit when Token /= Tok_Or;
1605             end if;
1606
1607             T_Or;
1608             Alt_Pragmas := P_Pragmas_Opt;
1609          end loop;
1610
1611          if Token = Tok_Else then
1612             Scan; -- past ELSE
1613             Set_Else_Statements
1614               (Select_Node, P_Sequence_Of_Statements (SS_Ortm_Sreq));
1615
1616             if Token = Tok_Or then
1617                Error_Msg_SC ("select alternative cannot follow else part!");
1618             end if;
1619          end if;
1620
1621          End_Statements;
1622       end if;
1623
1624       return Select_Node;
1625    end P_Select_Statement;
1626
1627    -----------------------------
1628    -- 9.7.1  Selective Accept --
1629    -----------------------------
1630
1631    --  Parsed by P_Select_Statement (9.7)
1632
1633    ------------------
1634    -- 9.7.1  Guard --
1635    ------------------
1636
1637    --  Parsed by P_Select_Statement (9.7)
1638
1639    -------------------------------
1640    -- 9.7.1  Select Alternative --
1641    -------------------------------
1642
1643    --  SELECT_ALTERNATIVE ::=
1644    --    ACCEPT_ALTERNATIVE
1645    --  | DELAY_ALTERNATIVE
1646    --  | TERMINATE_ALTERNATIVE
1647
1648    --  Note: the guard preceding a select alternative is included as part
1649    --  of the node generated for a selective accept alternative.
1650
1651    --  Error recovery: cannot raise Error_Resync
1652
1653    -------------------------------
1654    -- 9.7.1  Accept Alternative --
1655    -------------------------------
1656
1657    --  ACCEPT_ALTERNATIVE ::=
1658    --    ACCEPT_STATEMENT [SEQUENCE_OF_STATEMENTS]
1659
1660    --  Error_Recovery: Cannot raise Error_Resync
1661
1662    --  Note: the caller is responsible for setting the Pragmas_Before
1663    --  field of the returned N_Terminate_Alternative node.
1664
1665    function P_Accept_Alternative return Node_Id is
1666       Accept_Alt_Node : Node_Id;
1667
1668    begin
1669       Accept_Alt_Node := New_Node (N_Accept_Alternative, Token_Ptr);
1670       Set_Accept_Statement (Accept_Alt_Node, P_Accept_Statement);
1671
1672       --  Note: the reason that we accept THEN ABORT as a terminator for
1673       --  the sequence of statements is for error recovery which allows
1674       --  for misuse of an accept statement as a triggering statement.
1675
1676       Set_Statements
1677         (Accept_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1678       return Accept_Alt_Node;
1679    end P_Accept_Alternative;
1680
1681    ------------------------------
1682    -- 9.7.1  Delay Alternative --
1683    ------------------------------
1684
1685    --  DELAY_ALTERNATIVE ::=
1686    --    DELAY_STATEMENT [SEQUENCE_OF_STATEMENTS]
1687
1688    --  Error_Recovery: Cannot raise Error_Resync
1689
1690    --  Note: the caller is responsible for setting the Pragmas_Before
1691    --  field of the returned N_Terminate_Alternative node.
1692
1693    function P_Delay_Alternative return Node_Id is
1694       Delay_Alt_Node : Node_Id;
1695
1696    begin
1697       Delay_Alt_Node := New_Node (N_Delay_Alternative, Token_Ptr);
1698       Set_Delay_Statement (Delay_Alt_Node, P_Delay_Statement);
1699
1700       --  Note: the reason that we accept THEN ABORT as a terminator for
1701       --  the sequence of statements is for error recovery which allows
1702       --  for misuse of an accept statement as a triggering statement.
1703
1704       Set_Statements
1705         (Delay_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1706       return Delay_Alt_Node;
1707    end P_Delay_Alternative;
1708
1709    ----------------------------------
1710    -- 9.7.1  Terminate Alternative --
1711    ----------------------------------
1712
1713    --  TERMINATE_ALTERNATIVE ::= terminate;
1714
1715    --  Error_Recovery: Cannot raise Error_Resync
1716
1717    --  Note: the caller is responsible for setting the Pragmas_Before
1718    --  field of the returned N_Terminate_Alternative node.
1719
1720    function P_Terminate_Alternative return Node_Id is
1721       Terminate_Alt_Node : Node_Id;
1722
1723    begin
1724       Terminate_Alt_Node := New_Node (N_Terminate_Alternative, Token_Ptr);
1725       Scan; -- past TERMINATE
1726       TF_Semicolon;
1727
1728       --  For all other select alternatives, the sequence of statements
1729       --  after the alternative statement will swallow up any pragmas
1730       --  coming in this position. But the terminate alternative has no
1731       --  sequence of statements, so the pragmas here must be treated
1732       --  specially.
1733
1734       Set_Pragmas_After (Terminate_Alt_Node, P_Pragmas_Opt);
1735       return Terminate_Alt_Node;
1736    end P_Terminate_Alternative;
1737
1738    -----------------------------
1739    -- 9.7.2  Timed Entry Call --
1740    -----------------------------
1741
1742    --  Parsed by P_Select_Statement (9.7)
1743
1744    -----------------------------------
1745    -- 9.7.2  Entry Call Alternative --
1746    -----------------------------------
1747
1748    --  Parsed by P_Select_Statement (9.7)
1749
1750    -----------------------------------
1751    -- 9.7.3  Conditional Entry Call --
1752    -----------------------------------
1753
1754    --  Parsed by P_Select_Statement (9.7)
1755
1756    --------------------------------
1757    -- 9.7.4  Asynchronous Select --
1758    --------------------------------
1759
1760    --  Parsed by P_Select_Statement (9.7)
1761
1762    -----------------------------------
1763    -- 9.7.4  Triggering Alternative --
1764    -----------------------------------
1765
1766    --  Parsed by P_Select_Statement (9.7)
1767
1768    ---------------------------------
1769    -- 9.7.4  Triggering Statement --
1770    ---------------------------------
1771
1772    --  Parsed by P_Select_Statement (9.7)
1773
1774    ---------------------------
1775    -- 9.7.4  Abortable Part --
1776    ---------------------------
1777
1778    --  ABORTABLE_PART ::= SEQUENCE_OF_STATEMENTS
1779
1780    --  The caller has verified that THEN ABORT is present, and Token is
1781    --  pointing to the ABORT on entry (or if not, then we have an error)
1782
1783    --  Error recovery: cannot raise Error_Resync
1784
1785    function P_Abortable_Part return Node_Id is
1786       Abortable_Part_Node : Node_Id;
1787
1788    begin
1789       Abortable_Part_Node := New_Node (N_Abortable_Part, Token_Ptr);
1790       T_Abort; -- scan past ABORT
1791
1792       if Ada_Version = Ada_83 then
1793          Error_Msg_SP ("(Ada 83) asynchronous select not allowed!");
1794       end if;
1795
1796       Set_Statements (Abortable_Part_Node, P_Sequence_Of_Statements (SS_Sreq));
1797       return Abortable_Part_Node;
1798    end P_Abortable_Part;
1799
1800    --------------------------
1801    -- 9.8  Abort Statement --
1802    --------------------------
1803
1804    --  ABORT_STATEMENT ::= abort task_NAME {, task_NAME};
1805
1806    --  The caller has checked that the initial token is ABORT
1807
1808    --  Error recovery: cannot raise Error_Resync
1809
1810    function P_Abort_Statement return Node_Id is
1811       Abort_Node : Node_Id;
1812
1813    begin
1814       Abort_Node := New_Node (N_Abort_Statement, Token_Ptr);
1815       Scan; -- past ABORT
1816       Set_Names (Abort_Node, New_List);
1817
1818       loop
1819          Append (P_Name, Names (Abort_Node));
1820          exit when Token /= Tok_Comma;
1821          Scan; -- past comma
1822       end loop;
1823
1824       TF_Semicolon;
1825       return Abort_Node;
1826    end P_Abort_Statement;
1827
1828 end Ch9;