OSDN Git Service

gcc/ada/
[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 without comma or colon, must be discrete subtype
851             --  defn
852
853             else
854                Restore_Scan_State (Scan_State); -- to Id
855                Set_Discrete_Subtype_Definition
856                  (Decl_Node, P_Discrete_Subtype_Definition);
857                T_Right_Paren;
858                Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
859             end if;
860
861          --  If no Id, must be discrete subtype definition
862
863          else
864             Set_Discrete_Subtype_Definition
865               (Decl_Node, P_Discrete_Subtype_Definition);
866             T_Right_Paren;
867             Set_Parameter_Specifications (Decl_Node, P_Parameter_Profile);
868          end if;
869       end if;
870
871       if Is_Overriding then
872          Set_Must_Override (Decl_Node);
873       elsif Not_Overriding then
874          Set_Must_Not_Override (Decl_Node);
875       end if;
876
877       --  Error recovery check for illegal return
878
879       if Token = Tok_Return then
880          Error_Msg_SC ("entry cannot have return value!");
881          Scan;
882          Discard_Junk_Node (P_Subtype_Indication);
883       end if;
884
885       --  Error recovery check for improper use of entry barrier in spec
886
887       if Token = Tok_When then
888          Error_Msg_SC ("barrier not allowed here (belongs in body)");
889          Scan; -- past WHEN;
890          Discard_Junk_Node (P_Expression_No_Right_Paren);
891       end if;
892
893       TF_Semicolon;
894       return Decl_Node;
895
896    exception
897       when Error_Resync =>
898          Resync_Past_Semicolon;
899          return Error;
900    end P_Entry_Declaration;
901
902    -----------------------------
903    -- 9.5.2  Accept Statement --
904    -----------------------------
905
906    --  ACCEPT_STATEMENT ::=
907    --    accept entry_DIRECT_NAME
908    --      [(ENTRY_INDEX)] PARAMETER_PROFILE [do
909    --        HANDLED_SEQUENCE_OF_STATEMENTS
910    --    end [entry_IDENTIFIER]];
911
912    --  The caller has checked that the initial token is ACCEPT
913
914    --  Error recovery: cannot raise Error_Resync. If an error occurs, the
915    --  scan is resynchronized past the next semicolon and control returns.
916
917    function P_Accept_Statement return Node_Id is
918       Scan_State  : Saved_Scan_State;
919       Accept_Node : Node_Id;
920       Hand_Seq    : Node_Id;
921
922    begin
923       Push_Scope_Stack;
924       Scope.Table (Scope.Last).Sloc := Token_Ptr;
925       Scope.Table (Scope.Last).Ecol := Start_Column;
926
927       Accept_Node := New_Node (N_Accept_Statement, Token_Ptr);
928       Scan; -- past ACCEPT
929       Scope.Table (Scope.Last).Labl := Token_Node;
930
931       Set_Entry_Direct_Name (Accept_Node, P_Identifier (C_Do));
932
933       --  Left paren could be (Entry_Index) or Formal_Part, determine which
934
935       if Token = Tok_Left_Paren then
936          Save_Scan_State (Scan_State); -- at left paren
937          Scan; -- past left paren
938
939          --  If first token after left paren not identifier, then Entry_Index
940
941          if Token /= Tok_Identifier then
942             Set_Entry_Index (Accept_Node, P_Expression);
943             T_Right_Paren;
944             Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
945
946          --  First token after left paren is identifier, could be either case
947
948          else -- Token = Tok_Identifier
949             Scan; -- past identifier
950
951             --  If identifier followed by comma or colon, must be Formal_Part
952
953             if Token = Tok_Comma or else Token = Tok_Colon then
954                Restore_Scan_State (Scan_State); -- to left paren
955                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
956
957             --  If identifier not followed by comma/colon, must be entry index
958
959             else
960                Restore_Scan_State (Scan_State); -- to left paren
961                Scan; -- past left paren (again!)
962                Set_Entry_Index (Accept_Node, P_Expression);
963                T_Right_Paren;
964                Set_Parameter_Specifications (Accept_Node, P_Parameter_Profile);
965             end if;
966          end if;
967       end if;
968
969       --  Scan out DO if present
970
971       if Token = Tok_Do then
972          Scope.Table (Scope.Last).Etyp := E_Name;
973          Scope.Table (Scope.Last).Lreq := False;
974          Scan; -- past DO
975          Hand_Seq := P_Handled_Sequence_Of_Statements;
976          Set_Handled_Statement_Sequence (Accept_Node, Hand_Seq);
977          End_Statements (Handled_Statement_Sequence (Accept_Node));
978
979          --  Exception handlers not allowed in Ada 95 node
980
981          if Present (Exception_Handlers (Hand_Seq)) then
982             if Ada_Version = Ada_83 then
983                Error_Msg_N
984                  ("(Ada 83) exception handlers in accept not allowed",
985                   First_Non_Pragma (Exception_Handlers (Hand_Seq)));
986             end if;
987          end if;
988
989       else
990          Pop_Scope_Stack; -- discard unused entry
991          TF_Semicolon;
992       end if;
993
994       return Accept_Node;
995
996    --  If error, resynchronize past semicolon
997
998    exception
999       when Error_Resync =>
1000          Resync_Past_Semicolon;
1001          Pop_Scope_Stack; -- discard unused entry
1002          return Error;
1003
1004    end P_Accept_Statement;
1005
1006    ------------------------
1007    -- 9.5.2  Entry Index --
1008    ------------------------
1009
1010    --  Parsed by P_Expression (4.4)
1011
1012    -----------------------
1013    -- 9.5.2  Entry Body --
1014    -----------------------
1015
1016    --  ENTRY_BODY ::=
1017    --    entry DEFINING_IDENTIFIER ENTRY_BODY_FORMAL_PART ENTRY_BARRIER is
1018    --      DECLARATIVE_PART
1019    --    begin
1020    --      HANDLED_SEQUENCE_OF_STATEMENTS
1021    --    end [entry_IDENTIFIER];
1022
1023    --  The caller has checked that the initial token is ENTRY
1024
1025    --  Error_Recovery: cannot raise Error_Resync
1026
1027    function P_Entry_Body return Node_Id is
1028       Entry_Node       : Node_Id;
1029       Formal_Part_Node : Node_Id;
1030       Name_Node        : Node_Id;
1031
1032    begin
1033       Push_Scope_Stack;
1034       Entry_Node := New_Node (N_Entry_Body, Token_Ptr);
1035       Scan; -- past ENTRY
1036
1037       Scope.Table (Scope.Last).Ecol := Start_Column;
1038       Scope.Table (Scope.Last).Lreq := False;
1039       Scope.Table (Scope.Last).Etyp := E_Name;
1040
1041       Name_Node := P_Defining_Identifier;
1042       Set_Defining_Identifier (Entry_Node, Name_Node);
1043       Scope.Table (Scope.Last).Labl := Name_Node;
1044
1045       Formal_Part_Node := P_Entry_Body_Formal_Part;
1046       Set_Entry_Body_Formal_Part (Entry_Node, Formal_Part_Node);
1047
1048       Set_Condition (Formal_Part_Node, P_Entry_Barrier);
1049       Parse_Decls_Begin_End (Entry_Node);
1050       return Entry_Node;
1051    end P_Entry_Body;
1052
1053    -----------------------------------
1054    -- 9.5.2  Entry Body Formal Part --
1055    -----------------------------------
1056
1057    --  ENTRY_BODY_FORMAL_PART ::=
1058    --    [(ENTRY_INDEX_SPECIFICATION)] [PARAMETER_PART]
1059
1060    --  Error_Recovery: cannot raise Error_Resync
1061
1062    function P_Entry_Body_Formal_Part return Node_Id is
1063       Fpart_Node : Node_Id;
1064       Scan_State : Saved_Scan_State;
1065
1066    begin
1067       Fpart_Node := New_Node (N_Entry_Body_Formal_Part, Token_Ptr);
1068
1069       --  See if entry index specification present, and if so parse it
1070
1071       if Token = Tok_Left_Paren then
1072          Save_Scan_State (Scan_State); -- at left paren
1073          Scan; -- past left paren
1074
1075          if Token = Tok_For then
1076             Set_Entry_Index_Specification
1077               (Fpart_Node, P_Entry_Index_Specification);
1078             T_Right_Paren;
1079          else
1080             Restore_Scan_State (Scan_State); -- to left paren
1081          end if;
1082
1083       --  Check for (common?) case of left paren omitted before FOR. This
1084       --  is a tricky case, because the corresponding missing left paren
1085       --  can cause real havoc if a formal part is present which gets
1086       --  treated as part of the discrete subtype definition of the
1087       --  entry index specification, so just give error and resynchronize
1088
1089       elsif Token = Tok_For then
1090          T_Left_Paren; -- to give error message
1091          Resync_To_When;
1092       end if;
1093
1094       Set_Parameter_Specifications (Fpart_Node, P_Parameter_Profile);
1095       return Fpart_Node;
1096    end P_Entry_Body_Formal_Part;
1097
1098    --------------------------
1099    -- 9.5.2  Entry Barrier --
1100    --------------------------
1101
1102    --  ENTRY_BARRIER ::= when CONDITION
1103
1104    --  Error_Recovery: cannot raise Error_Resync
1105
1106    function P_Entry_Barrier return Node_Id is
1107       Bnode : Node_Id;
1108
1109    begin
1110       if Token = Tok_When then
1111          Scan; -- past WHEN;
1112          Bnode := P_Expression_No_Right_Paren;
1113
1114          if Token = Tok_Colon_Equal then
1115             Error_Msg_SC ("|"":="" should be ""=""");
1116             Scan;
1117             Bnode := P_Expression_No_Right_Paren;
1118          end if;
1119
1120       else
1121          T_When; -- to give error message
1122          Bnode := Error;
1123       end if;
1124
1125       TF_Is;
1126       return Bnode;
1127    end P_Entry_Barrier;
1128
1129    --------------------------------------
1130    -- 9.5.2  Entry Index Specification --
1131    --------------------------------------
1132
1133    --  ENTRY_INDEX_SPECIFICATION ::=
1134    --    for DEFINING_IDENTIFIER in DISCRETE_SUBTYPE_DEFINITION
1135
1136    --  Error recovery: can raise Error_Resync
1137
1138    function P_Entry_Index_Specification return Node_Id is
1139       Iterator_Node : Node_Id;
1140
1141    begin
1142       Iterator_Node := New_Node (N_Entry_Index_Specification, Token_Ptr);
1143       T_For; -- past FOR
1144       Set_Defining_Identifier (Iterator_Node, P_Defining_Identifier (C_In));
1145       T_In;
1146       Set_Discrete_Subtype_Definition
1147         (Iterator_Node, P_Discrete_Subtype_Definition);
1148       return Iterator_Node;
1149    end P_Entry_Index_Specification;
1150
1151    ---------------------------------
1152    -- 9.5.3  Entry Call Statement --
1153    ---------------------------------
1154
1155    --  Parsed by P_Name (4.1). Within a select, an entry call is parsed
1156    --  by P_Select_Statement (9.7)
1157
1158    ------------------------------
1159    -- 9.5.4  Requeue Statement --
1160    ------------------------------
1161
1162    --  REQUEUE_STATEMENT ::= requeue entry_NAME [with abort];
1163
1164    --  The caller has checked that the initial token is requeue
1165
1166    --  Error recovery: can raise Error_Resync
1167
1168    function P_Requeue_Statement return Node_Id is
1169       Requeue_Node : Node_Id;
1170
1171    begin
1172       Requeue_Node := New_Node (N_Requeue_Statement, Token_Ptr);
1173       Scan; -- past REQUEUE
1174       Set_Name (Requeue_Node, P_Name);
1175
1176       if Token = Tok_With then
1177          Scan; -- past WITH
1178          T_Abort;
1179          Set_Abort_Present (Requeue_Node, True);
1180       end if;
1181
1182       TF_Semicolon;
1183       return Requeue_Node;
1184    end P_Requeue_Statement;
1185
1186    --------------------------
1187    -- 9.6  Delay Statement --
1188    --------------------------
1189
1190    --  DELAY_STATEMENT ::=
1191    --    DELAY_UNTIL_STATEMENT
1192    --  | DELAY_RELATIVE_STATEMENT
1193
1194    --  The caller has checked that the initial token is DELAY
1195
1196    --  Error recovery: cannot raise Error_Resync
1197
1198    function P_Delay_Statement return Node_Id is
1199    begin
1200       Scan; -- past DELAY
1201
1202       --  The following check for delay until misused in Ada 83 doesn't catch
1203       --  all cases, but it's good enough to catch most of them!
1204
1205       if Token_Name = Name_Until then
1206          Check_95_Keyword (Tok_Until, Tok_Left_Paren);
1207          Check_95_Keyword (Tok_Until, Tok_Identifier);
1208       end if;
1209
1210       if Token = Tok_Until then
1211          return P_Delay_Until_Statement;
1212       else
1213          return P_Delay_Relative_Statement;
1214       end if;
1215    end P_Delay_Statement;
1216
1217    --------------------------------
1218    -- 9.6  Delay Until Statement --
1219    --------------------------------
1220
1221    --  DELAY_UNTIL_STATEMENT ::= delay until delay_EXPRESSION;
1222
1223    --  The caller has checked that the initial token is DELAY, scanned it
1224    --  out and checked that the current token is UNTIL
1225
1226    --  Error recovery: cannot raise Error_Resync
1227
1228    function P_Delay_Until_Statement return Node_Id is
1229       Delay_Node : Node_Id;
1230
1231    begin
1232       Delay_Node := New_Node (N_Delay_Until_Statement, Prev_Token_Ptr);
1233       Scan; -- past UNTIL
1234       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1235       TF_Semicolon;
1236       return Delay_Node;
1237    end P_Delay_Until_Statement;
1238
1239    -----------------------------------
1240    -- 9.6  Delay Relative Statement --
1241    -----------------------------------
1242
1243    --  DELAY_RELATIVE_STATEMENT ::= delay delay_EXPRESSION;
1244
1245    --  The caller has checked that the initial token is DELAY, scanned it
1246    --  out and determined that the current token is not UNTIL
1247
1248    --  Error recovery: cannot raise Error_Resync
1249
1250    function P_Delay_Relative_Statement return Node_Id is
1251       Delay_Node : Node_Id;
1252
1253    begin
1254       Delay_Node := New_Node (N_Delay_Relative_Statement, Prev_Token_Ptr);
1255       Set_Expression (Delay_Node, P_Expression_No_Right_Paren);
1256       Check_Simple_Expression_In_Ada_83 (Expression (Delay_Node));
1257       TF_Semicolon;
1258       return Delay_Node;
1259    end P_Delay_Relative_Statement;
1260
1261    ---------------------------
1262    -- 9.7  Select Statement --
1263    ---------------------------
1264
1265    --  SELECT_STATEMENT ::=
1266    --    SELECTIVE_ACCEPT
1267    --  | TIMED_ENTRY_CALL
1268    --  | CONDITIONAL_ENTRY_CALL
1269    --  | ASYNCHRONOUS_SELECT
1270
1271    --  SELECTIVE_ACCEPT ::=
1272    --    select
1273    --      [GUARD]
1274    --        SELECT_ALTERNATIVE
1275    --    {or
1276    --      [GUARD]
1277    --        SELECT_ALTERNATIVE
1278    --    [else
1279    --      SEQUENCE_OF_STATEMENTS]
1280    --    end select;
1281
1282    --  GUARD ::= when CONDITION =>
1283
1284    --  Note: the guard preceding a select alternative is included as part
1285    --  of the node generated for a selective accept alternative.
1286
1287    --  SELECT_ALTERNATIVE ::=
1288    --    ACCEPT_ALTERNATIVE
1289    --  | DELAY_ALTERNATIVE
1290    --  | TERMINATE_ALTERNATIVE
1291
1292    --  TIMED_ENTRY_CALL ::=
1293    --    select
1294    --      ENTRY_CALL_ALTERNATIVE
1295    --    or
1296    --      DELAY_ALTERNATIVE
1297    --    end select;
1298
1299    --  CONDITIONAL_ENTRY_CALL ::=
1300    --    select
1301    --      ENTRY_CALL_ALTERNATIVE
1302    --    else
1303    --      SEQUENCE_OF_STATEMENTS
1304    --    end select;
1305
1306    --  ENTRY_CALL_ALTERNATIVE ::=
1307    --    ENTRY_CALL_STATEMENT [SEQUENCE_OF_STATEMENTS]
1308
1309    --  ASYNCHRONOUS_SELECT ::=
1310    --    select
1311    --      TRIGGERING_ALTERNATIVE
1312    --    then abort
1313    --      ABORTABLE_PART
1314    --    end select;
1315
1316    --  TRIGGERING_ALTERNATIVE ::=
1317    --    TRIGGERING_STATEMENT [SEQUENCE_OF_STATEMENTS]
1318
1319    --  TRIGGERING_STATEMENT ::= ENTRY_CALL_STATEMENT | DELAY_STATEMENT
1320
1321    --  The caller has checked that the initial token is SELECT
1322
1323    --  Error recovery: can raise Error_Resync
1324
1325    function P_Select_Statement return Node_Id is
1326       Select_Node    : Node_Id;
1327       Select_Sloc    : Source_Ptr;
1328       Stmnt_Sloc     : Source_Ptr;
1329       Ecall_Node     : Node_Id;
1330       Alternative    : Node_Id;
1331       Select_Pragmas : List_Id;
1332       Alt_Pragmas    : List_Id;
1333       Statement_List : List_Id;
1334       Alt_List       : List_Id;
1335       Cond_Expr      : Node_Id;
1336       Delay_Stmnt    : Node_Id;
1337
1338    begin
1339       Push_Scope_Stack;
1340       Scope.Table (Scope.Last).Etyp := E_Select;
1341       Scope.Table (Scope.Last).Ecol := Start_Column;
1342       Scope.Table (Scope.Last).Sloc := Token_Ptr;
1343       Scope.Table (Scope.Last).Labl := Error;
1344
1345       Select_Sloc := Token_Ptr;
1346       Scan; -- past SELECT
1347       Stmnt_Sloc := Token_Ptr;
1348       Select_Pragmas := P_Pragmas_Opt;
1349
1350       --  If first token after select is designator, then we have an entry
1351       --  call, which must be the start of a conditional entry call, timed
1352       --  entry call or asynchronous select
1353
1354       if Token in Token_Class_Desig then
1355
1356          --  Scan entry call statement
1357
1358          begin
1359             Ecall_Node := P_Name;
1360
1361             --  ??  The following two clauses exactly parallel code in ch5
1362             --      and should be combined sometime
1363
1364             if Nkind (Ecall_Node) = N_Indexed_Component then
1365                declare
1366                   Prefix_Node : constant Node_Id := Prefix (Ecall_Node);
1367                   Exprs_Node  : constant List_Id := Expressions (Ecall_Node);
1368
1369                begin
1370                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1371                   Set_Name (Ecall_Node, Prefix_Node);
1372                   Set_Parameter_Associations (Ecall_Node, Exprs_Node);
1373                end;
1374
1375             elsif Nkind (Ecall_Node) = N_Function_Call then
1376                declare
1377                   Fname_Node  : constant Node_Id := Name (Ecall_Node);
1378                   Params_List : constant List_Id :=
1379                                   Parameter_Associations (Ecall_Node);
1380
1381                begin
1382                   Change_Node (Ecall_Node, N_Procedure_Call_Statement);
1383                   Set_Name (Ecall_Node, Fname_Node);
1384                   Set_Parameter_Associations (Ecall_Node, Params_List);
1385                end;
1386
1387             elsif Nkind (Ecall_Node) = N_Identifier
1388               or else Nkind (Ecall_Node) = N_Selected_Component
1389             then
1390                --  Case of a call to a parameterless entry
1391
1392                declare
1393                   C_Node : constant Node_Id :=
1394                          New_Node (N_Procedure_Call_Statement, Stmnt_Sloc);
1395                begin
1396                   Set_Name (C_Node, Ecall_Node);
1397                   Set_Parameter_Associations (C_Node, No_List);
1398                   Ecall_Node := C_Node;
1399                end;
1400             end if;
1401
1402             TF_Semicolon;
1403
1404          exception
1405             when Error_Resync =>
1406                Resync_Past_Semicolon;
1407                return Error;
1408          end;
1409
1410          Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1411
1412          --  OR follows, we have a timed entry call
1413
1414          if Token = Tok_Or then
1415             Scan; -- past OR
1416             Alt_Pragmas := P_Pragmas_Opt;
1417
1418             Select_Node := New_Node (N_Timed_Entry_Call, Select_Sloc);
1419             Set_Entry_Call_Alternative (Select_Node,
1420               Make_Entry_Call_Alternative (Stmnt_Sloc,
1421                 Entry_Call_Statement => Ecall_Node,
1422                 Pragmas_Before       => Select_Pragmas,
1423                 Statements           => Statement_List));
1424
1425             --  Only possibility is delay alternative. If we have anything
1426             --  else, give message, and treat as conditional entry call.
1427
1428             if Token /= Tok_Delay then
1429                Error_Msg_SC
1430                  ("only allowed alternative in timed entry call is delay!");
1431                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1432                Set_Delay_Alternative (Select_Node, Error);
1433
1434             else
1435                Set_Delay_Alternative (Select_Node, P_Delay_Alternative);
1436                Set_Pragmas_Before
1437                  (Delay_Alternative (Select_Node), Alt_Pragmas);
1438             end if;
1439
1440          --  ELSE follows, we have a conditional entry call
1441
1442          elsif Token = Tok_Else then
1443             Scan; -- past ELSE
1444             Select_Node := New_Node (N_Conditional_Entry_Call, Select_Sloc);
1445
1446             Set_Entry_Call_Alternative (Select_Node,
1447               Make_Entry_Call_Alternative (Stmnt_Sloc,
1448                 Entry_Call_Statement => Ecall_Node,
1449                 Pragmas_Before       => Select_Pragmas,
1450                 Statements           => Statement_List));
1451
1452             Set_Else_Statements
1453               (Select_Node, P_Sequence_Of_Statements (SS_Sreq));
1454
1455          --  Only remaining case is THEN ABORT (asynchronous select)
1456
1457          elsif Token = Tok_Abort then
1458             Select_Node :=
1459               Make_Asynchronous_Select (Select_Sloc,
1460                 Triggering_Alternative =>
1461                   Make_Triggering_Alternative (Stmnt_Sloc,
1462                     Triggering_Statement => Ecall_Node,
1463                     Pragmas_Before       => Select_Pragmas,
1464                     Statements           => Statement_List),
1465                 Abortable_Part => P_Abortable_Part);
1466
1467          --  Else error
1468
1469          else
1470             if Ada_Version = Ada_83 then
1471                Error_Msg_BC ("OR or ELSE expected");
1472             else
1473                Error_Msg_BC ("OR or ELSE or THEN ABORT expected");
1474             end if;
1475
1476             Select_Node := Error;
1477          end if;
1478
1479          End_Statements;
1480
1481       --  Here we have a selective accept or an asynchronous select (first
1482       --  token after SELECT is other than a designator token).
1483
1484       else
1485          --  If we have delay with no guard, could be asynchronous select
1486
1487          if Token = Tok_Delay then
1488             Delay_Stmnt := P_Delay_Statement;
1489             Statement_List := P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm);
1490
1491             --  Asynchronous select
1492
1493             if Token = Tok_Abort then
1494                Select_Node :=
1495                  Make_Asynchronous_Select (Select_Sloc,
1496                    Triggering_Alternative =>
1497                      Make_Triggering_Alternative (Stmnt_Sloc,
1498                        Triggering_Statement => Delay_Stmnt,
1499                        Pragmas_Before       => Select_Pragmas,
1500                        Statements           => Statement_List),
1501                      Abortable_Part => P_Abortable_Part);
1502
1503                End_Statements;
1504                return Select_Node;
1505
1506             --  Delay which was not an asynchronous select. Must be a selective
1507             --  accept, and since at least one accept statement is required,
1508             --  we must have at least one OR phrase present.
1509
1510             else
1511                Alt_List := New_List (
1512                  Make_Delay_Alternative (Stmnt_Sloc,
1513                    Delay_Statement => Delay_Stmnt,
1514                    Pragmas_Before  => Select_Pragmas,
1515                    Statements      => Statement_List));
1516                T_Or;
1517                Alt_Pragmas := P_Pragmas_Opt;
1518             end if;
1519
1520          --  If not a delay statement, then must be another possibility for
1521          --  a selective accept alternative, or perhaps a guard is present
1522
1523          else
1524             Alt_List := New_List;
1525             Alt_Pragmas := Select_Pragmas;
1526          end if;
1527
1528          Select_Node := New_Node (N_Selective_Accept, Select_Sloc);
1529          Set_Select_Alternatives (Select_Node, Alt_List);
1530
1531          --  Scan out selective accept alternatives. On entry to this loop,
1532          --  we are just past a SELECT or OR token, and any pragmas that
1533          --  immediately follow the SELECT or OR are in Alt_Pragmas.
1534
1535          loop
1536             if Token = Tok_When then
1537
1538                if Present (Alt_Pragmas) then
1539                   Error_Msg_SC ("pragmas may not precede guard");
1540                end if;
1541
1542                Scan; --  past WHEN
1543                Cond_Expr := P_Expression_No_Right_Paren;
1544                T_Arrow;
1545                Alt_Pragmas := P_Pragmas_Opt;
1546
1547             else
1548                Cond_Expr := Empty;
1549             end if;
1550
1551             if Token = Tok_Accept then
1552                Alternative := P_Accept_Alternative;
1553
1554                --  Check for junk attempt at asynchronous select using
1555                --  an Accept alternative as the triggering statement
1556
1557                if Token = Tok_Abort
1558                  and then Is_Empty_List (Alt_List)
1559                  and then No (Cond_Expr)
1560                then
1561                   Error_Msg
1562                     ("triggering statement must be entry call or delay",
1563                      Sloc (Alternative));
1564                   Scan; -- past junk ABORT
1565                   Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1566                   End_Statements;
1567                   return Error;
1568                end if;
1569
1570             elsif Token = Tok_Delay then
1571                Alternative := P_Delay_Alternative;
1572
1573             elsif Token = Tok_Terminate then
1574                Alternative := P_Terminate_Alternative;
1575
1576             else
1577                Error_Msg_SC
1578                  ("select alternative (ACCEPT, ABORT, DELAY) expected");
1579                Alternative := Error;
1580
1581                if Token = Tok_Semicolon then
1582                   Scan; -- past junk semicolon
1583                end if;
1584             end if;
1585
1586             --  THEN ABORT at this stage is just junk
1587
1588             if Token = Tok_Abort then
1589                Error_Msg_SP ("misplaced `THEN ABORT`");
1590                Scan; -- past junk ABORT
1591                Discard_Junk_List (P_Sequence_Of_Statements (SS_Sreq));
1592                End_Statements;
1593                return Error;
1594
1595             else
1596                if Alternative /= Error then
1597                   Set_Condition (Alternative, Cond_Expr);
1598                   Set_Pragmas_Before (Alternative, Alt_Pragmas);
1599                   Append (Alternative, Alt_List);
1600                end if;
1601
1602                exit when Token /= Tok_Or;
1603             end if;
1604
1605             T_Or;
1606             Alt_Pragmas := P_Pragmas_Opt;
1607          end loop;
1608
1609          if Token = Tok_Else then
1610             Scan; -- past ELSE
1611             Set_Else_Statements
1612               (Select_Node, P_Sequence_Of_Statements (SS_Ortm_Sreq));
1613
1614             if Token = Tok_Or then
1615                Error_Msg_SC ("select alternative cannot follow else part!");
1616             end if;
1617          end if;
1618
1619          End_Statements;
1620       end if;
1621
1622       return Select_Node;
1623    end P_Select_Statement;
1624
1625    -----------------------------
1626    -- 9.7.1  Selective Accept --
1627    -----------------------------
1628
1629    --  Parsed by P_Select_Statement (9.7)
1630
1631    ------------------
1632    -- 9.7.1  Guard --
1633    ------------------
1634
1635    --  Parsed by P_Select_Statement (9.7)
1636
1637    -------------------------------
1638    -- 9.7.1  Select Alternative --
1639    -------------------------------
1640
1641    --  SELECT_ALTERNATIVE ::=
1642    --    ACCEPT_ALTERNATIVE
1643    --  | DELAY_ALTERNATIVE
1644    --  | TERMINATE_ALTERNATIVE
1645
1646    --  Note: the guard preceding a select alternative is included as part
1647    --  of the node generated for a selective accept alternative.
1648
1649    --  Error recovery: cannot raise Error_Resync
1650
1651    -------------------------------
1652    -- 9.7.1  Accept Alternative --
1653    -------------------------------
1654
1655    --  ACCEPT_ALTERNATIVE ::=
1656    --    ACCEPT_STATEMENT [SEQUENCE_OF_STATEMENTS]
1657
1658    --  Error_Recovery: Cannot raise Error_Resync
1659
1660    --  Note: the caller is responsible for setting the Pragmas_Before
1661    --  field of the returned N_Terminate_Alternative node.
1662
1663    function P_Accept_Alternative return Node_Id is
1664       Accept_Alt_Node : Node_Id;
1665
1666    begin
1667       Accept_Alt_Node := New_Node (N_Accept_Alternative, Token_Ptr);
1668       Set_Accept_Statement (Accept_Alt_Node, P_Accept_Statement);
1669
1670       --  Note: the reason that we accept THEN ABORT as a terminator for
1671       --  the sequence of statements is for error recovery which allows
1672       --  for misuse of an accept statement as a triggering statement.
1673
1674       Set_Statements
1675         (Accept_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1676       return Accept_Alt_Node;
1677    end P_Accept_Alternative;
1678
1679    ------------------------------
1680    -- 9.7.1  Delay Alternative --
1681    ------------------------------
1682
1683    --  DELAY_ALTERNATIVE ::=
1684    --    DELAY_STATEMENT [SEQUENCE_OF_STATEMENTS]
1685
1686    --  Error_Recovery: Cannot raise Error_Resync
1687
1688    --  Note: the caller is responsible for setting the Pragmas_Before
1689    --  field of the returned N_Terminate_Alternative node.
1690
1691    function P_Delay_Alternative return Node_Id is
1692       Delay_Alt_Node : Node_Id;
1693
1694    begin
1695       Delay_Alt_Node := New_Node (N_Delay_Alternative, Token_Ptr);
1696       Set_Delay_Statement (Delay_Alt_Node, P_Delay_Statement);
1697
1698       --  Note: the reason that we accept THEN ABORT as a terminator for
1699       --  the sequence of statements is for error recovery which allows
1700       --  for misuse of an accept statement as a triggering statement.
1701
1702       Set_Statements
1703         (Delay_Alt_Node, P_Sequence_Of_Statements (SS_Eltm_Ortm_Tatm));
1704       return Delay_Alt_Node;
1705    end P_Delay_Alternative;
1706
1707    ----------------------------------
1708    -- 9.7.1  Terminate Alternative --
1709    ----------------------------------
1710
1711    --  TERMINATE_ALTERNATIVE ::= terminate;
1712
1713    --  Error_Recovery: Cannot raise Error_Resync
1714
1715    --  Note: the caller is responsible for setting the Pragmas_Before
1716    --  field of the returned N_Terminate_Alternative node.
1717
1718    function P_Terminate_Alternative return Node_Id is
1719       Terminate_Alt_Node : Node_Id;
1720
1721    begin
1722       Terminate_Alt_Node := New_Node (N_Terminate_Alternative, Token_Ptr);
1723       Scan; -- past TERMINATE
1724       TF_Semicolon;
1725
1726       --  For all other select alternatives, the sequence of statements
1727       --  after the alternative statement will swallow up any pragmas
1728       --  coming in this position. But the terminate alternative has no
1729       --  sequence of statements, so the pragmas here must be treated
1730       --  specially.
1731
1732       Set_Pragmas_After (Terminate_Alt_Node, P_Pragmas_Opt);
1733       return Terminate_Alt_Node;
1734    end P_Terminate_Alternative;
1735
1736    -----------------------------
1737    -- 9.7.2  Timed Entry Call --
1738    -----------------------------
1739
1740    --  Parsed by P_Select_Statement (9.7)
1741
1742    -----------------------------------
1743    -- 9.7.2  Entry Call Alternative --
1744    -----------------------------------
1745
1746    --  Parsed by P_Select_Statement (9.7)
1747
1748    -----------------------------------
1749    -- 9.7.3  Conditional Entry Call --
1750    -----------------------------------
1751
1752    --  Parsed by P_Select_Statement (9.7)
1753
1754    --------------------------------
1755    -- 9.7.4  Asynchronous Select --
1756    --------------------------------
1757
1758    --  Parsed by P_Select_Statement (9.7)
1759
1760    -----------------------------------
1761    -- 9.7.4  Triggering Alternative --
1762    -----------------------------------
1763
1764    --  Parsed by P_Select_Statement (9.7)
1765
1766    ---------------------------------
1767    -- 9.7.4  Triggering Statement --
1768    ---------------------------------
1769
1770    --  Parsed by P_Select_Statement (9.7)
1771
1772    ---------------------------
1773    -- 9.7.4  Abortable Part --
1774    ---------------------------
1775
1776    --  ABORTABLE_PART ::= SEQUENCE_OF_STATEMENTS
1777
1778    --  The caller has verified that THEN ABORT is present, and Token is
1779    --  pointing to the ABORT on entry (or if not, then we have an error)
1780
1781    --  Error recovery: cannot raise Error_Resync
1782
1783    function P_Abortable_Part return Node_Id is
1784       Abortable_Part_Node : Node_Id;
1785
1786    begin
1787       Abortable_Part_Node := New_Node (N_Abortable_Part, Token_Ptr);
1788       T_Abort; -- scan past ABORT
1789
1790       if Ada_Version = Ada_83 then
1791          Error_Msg_SP ("(Ada 83) asynchronous select not allowed!");
1792       end if;
1793
1794       Set_Statements (Abortable_Part_Node, P_Sequence_Of_Statements (SS_Sreq));
1795       return Abortable_Part_Node;
1796    end P_Abortable_Part;
1797
1798    --------------------------
1799    -- 9.8  Abort Statement --
1800    --------------------------
1801
1802    --  ABORT_STATEMENT ::= abort task_NAME {, task_NAME};
1803
1804    --  The caller has checked that the initial token is ABORT
1805
1806    --  Error recovery: cannot raise Error_Resync
1807
1808    function P_Abort_Statement return Node_Id is
1809       Abort_Node : Node_Id;
1810
1811    begin
1812       Abort_Node := New_Node (N_Abort_Statement, Token_Ptr);
1813       Scan; -- past ABORT
1814       Set_Names (Abort_Node, New_List);
1815
1816       loop
1817          Append (P_Name, Names (Abort_Node));
1818          exit when Token /= Tok_Comma;
1819          Scan; -- past comma
1820       end loop;
1821
1822       TF_Semicolon;
1823       return Abort_Node;
1824    end P_Abort_Statement;
1825
1826 end Ch9;