OSDN Git Service

2005-11-14 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / par-ch6.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              P A R . C H 6                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005, 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 2,  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 COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 pragma Style_Checks (All_Checks);
28 --  Turn off subprogram body ordering check. Subprograms are in order
29 --  by RM section rather than alphabetical
30
31 with Sinfo.CN; use Sinfo.CN;
32
33 separate (Par)
34 package body Ch6 is
35
36    --  Local subprograms, used only in this chapter
37
38    function P_Defining_Designator        return Node_Id;
39    function P_Defining_Operator_Symbol   return Node_Id;
40
41    procedure Check_Junk_Semicolon_Before_Return;
42
43    --  Check for common error of junk semicolon before RETURN keyword of
44    --  function specification. If present, skip over it with appropriate
45    --  error message, leaving Scan_Ptr pointing to the RETURN after. This
46    --  routine also deals with a possibly misspelled version of Return.
47
48    ----------------------------------------
49    -- Check_Junk_Semicolon_Before_Return --
50    ----------------------------------------
51
52    procedure Check_Junk_Semicolon_Before_Return is
53       Scan_State : Saved_Scan_State;
54
55    begin
56       if Token = Tok_Semicolon then
57          Save_Scan_State (Scan_State);
58          Scan; -- past the semicolon
59
60          if Token = Tok_Return then
61             Restore_Scan_State (Scan_State);
62             Error_Msg_SC ("unexpected semicolon ignored");
63             Scan; -- rescan past junk semicolon
64
65          else
66             Restore_Scan_State (Scan_State);
67          end if;
68
69       elsif Bad_Spelling_Of (Tok_Return) then
70          null;
71       end if;
72    end Check_Junk_Semicolon_Before_Return;
73
74    -----------------------------------------------------
75    -- 6.1  Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
76    -----------------------------------------------------
77
78    --  This routine scans out a subprogram declaration, subprogram body,
79    --  subprogram renaming declaration or subprogram generic instantiation.
80
81    --  SUBPROGRAM_DECLARATION ::= SUBPROGRAM_SPECIFICATION;
82
83    --  ABSTRACT_SUBPROGRAM_DECLARATION ::=
84    --    SUBPROGRAM_SPECIFICATION is abstract;
85
86    --  SUBPROGRAM_SPECIFICATION ::=
87    --      procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
88    --    | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
89
90    --  PARAMETER_PROFILE ::= [FORMAL_PART]
91
92    --  PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
93
94    --  SUBPROGRAM_BODY ::=
95    --    SUBPROGRAM_SPECIFICATION is
96    --      DECLARATIVE_PART
97    --    begin
98    --      HANDLED_SEQUENCE_OF_STATEMENTS
99    --    end [DESIGNATOR];
100
101    --  SUBPROGRAM_RENAMING_DECLARATION ::=
102    --    SUBPROGRAM_SPECIFICATION renames callable_entity_NAME;
103
104    --  SUBPROGRAM_BODY_STUB ::=
105    --    SUBPROGRAM_SPECIFICATION is separate;
106
107    --  GENERIC_INSTANTIATION ::=
108    --    procedure DEFINING_PROGRAM_UNIT_NAME is
109    --      new generic_procedure_NAME [GENERIC_ACTUAL_PART];
110    --  | function DEFINING_DESIGNATOR is
111    --      new generic_function_NAME [GENERIC_ACTUAL_PART];
112
113    --  NULL_PROCEDURE_DECLARATION ::=
114    --    SUBPROGRAM_SPECIFICATION is null;
115
116    --  Null procedures are an Ada 2005 feature. A null procedure declaration
117    --  is classified as a basic declarative item, but it is parsed here, with
118    --  other subprogram constructs.
119
120    --  The value in Pf_Flags indicates which of these possible declarations
121    --  is acceptable to the caller:
122
123    --    Pf_Flags.Decl                 Set if declaration OK
124    --    Pf_Flags.Gins                 Set if generic instantiation OK
125    --    Pf_Flags.Pbod                 Set if proper body OK
126    --    Pf_Flags.Rnam                 Set if renaming declaration OK
127    --    Pf_Flags.Stub                 Set if body stub OK
128
129    --  If an inappropriate form is encountered, it is scanned out but an
130    --  error message indicating that it is appearing in an inappropriate
131    --  context is issued. The only possible values for Pf_Flags are those
132    --  defined as constants in the Par package.
133
134    --  The caller has checked that the initial token is FUNCTION, PROCEDURE,
135    --  NOT or OVERRIDING.
136
137    --  Error recovery: cannot raise Error_Resync
138
139    function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
140       Specification_Node : Node_Id;
141       Name_Node          : Node_Id;
142       Fpart_List         : List_Id;
143       Fpart_Sloc         : Source_Ptr;
144       Result_Not_Null    : Boolean := False;
145       Result_Node        : Node_Id;
146       Inst_Node          : Node_Id;
147       Body_Node          : Node_Id;
148       Decl_Node          : Node_Id;
149       Rename_Node        : Node_Id;
150       Absdec_Node        : Node_Id;
151       Stub_Node          : Node_Id;
152       Fproc_Sloc         : Source_Ptr;
153       Func               : Boolean;
154       Scan_State         : Saved_Scan_State;
155
156       --  Flags for optional overriding indication. Two flags are needed,
157       --  to distinguish positive and negative overriding indicators from
158       --  the absence of any indicator.
159
160       Is_Overriding  : Boolean := False;
161       Not_Overriding : Boolean := False;
162
163    begin
164       --  Set up scope stack entry. Note that the Labl field will be set later
165
166       SIS_Entry_Active := False;
167       SIS_Missing_Semicolon_Message := No_Error_Msg;
168       Push_Scope_Stack;
169       Scope.Table (Scope.Last).Sloc := Token_Ptr;
170       Scope.Table (Scope.Last).Etyp := E_Name;
171       Scope.Table (Scope.Last).Ecol := Start_Column;
172       Scope.Table (Scope.Last).Lreq := False;
173
174       --  Ada2005: scan leading overriding indicator
175
176       if Token = Tok_Not then
177          Scan;  -- past NOT
178
179          if Token = Tok_Overriding then
180             Scan;  --  past OVERRIDING
181             Not_Overriding := True;
182          else
183             Error_Msg_SC ("OVERRIDING expected!");
184          end if;
185
186       elsif Token = Tok_Overriding then
187          Scan;  --  past OVERRIDING
188          Is_Overriding := True;
189       end if;
190
191       if (Is_Overriding or else Not_Overriding) then
192          if Ada_Version < Ada_05 then
193             Error_Msg_SP (" overriding indicator is an Ada 2005 extension");
194             Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
195
196          --  An overriding indicator is allowed for subprogram declarations,
197          --  bodies, renamings, stubs, and instantiations.
198
199          elsif Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub then
200             Error_Msg_SC ("overriding indicator not allowed here!");
201
202          elsif Token /= Tok_Function
203            and then Token /= Tok_Procedure
204          then
205             Error_Msg_SC ("FUNCTION or PROCEDURE expected!");
206          end if;
207       end if;
208
209       Func := (Token = Tok_Function);
210       Fproc_Sloc := Token_Ptr;
211       Scan; -- past FUNCTION or PROCEDURE
212       Ignore (Tok_Type);
213       Ignore (Tok_Body);
214
215       if Func then
216          Name_Node := P_Defining_Designator;
217
218          if Nkind (Name_Node) = N_Defining_Operator_Symbol
219            and then Scope.Last = 1
220          then
221             Error_Msg_SP ("operator symbol not allowed at library level");
222             Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
223
224             --  Set name from file name, we need some junk name, and that's
225             --  as good as anything. This is only approximate, since we do
226             --  not do anything with non-standard name translations.
227
228             Get_Name_String (File_Name (Current_Source_File));
229
230             for J in 1 .. Name_Len loop
231                if Name_Buffer (J) = '.' then
232                   Name_Len := J - 1;
233                   exit;
234                end if;
235             end loop;
236
237             Set_Chars (Name_Node, Name_Find);
238             Set_Error_Posted (Name_Node);
239          end if;
240
241       else
242          Name_Node := P_Defining_Program_Unit_Name;
243       end if;
244
245       Scope.Table (Scope.Last).Labl := Name_Node;
246
247       if Token = Tok_Colon then
248          Error_Msg_SC ("redundant colon ignored");
249          Scan; -- past colon
250       end if;
251
252       --  Deal with generic instantiation, the one case in which we do not
253       --  have a subprogram specification as part of whatever we are parsing
254
255       if Token = Tok_Is then
256          Save_Scan_State (Scan_State); -- at the IS
257          T_Is; -- checks for redundant IS
258
259          if Token = Tok_New then
260             if not Pf_Flags.Gins then
261                Error_Msg_SC ("generic instantation not allowed here!");
262             end if;
263
264             Scan; -- past NEW
265
266             if Func then
267                Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
268                Set_Name (Inst_Node, P_Function_Name);
269             else
270                Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
271                Set_Name (Inst_Node, P_Qualified_Simple_Name);
272             end if;
273
274             Set_Defining_Unit_Name (Inst_Node, Name_Node);
275             Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
276             TF_Semicolon;
277             Pop_Scope_Stack; -- Don't need scope stack entry in this case
278
279             if Is_Overriding then
280                Set_Must_Override (Inst_Node);
281
282             elsif Not_Overriding then
283                Set_Must_Not_Override (Inst_Node);
284             end if;
285
286             return Inst_Node;
287
288          else
289             Restore_Scan_State (Scan_State); -- to the IS
290          end if;
291       end if;
292
293       --  If not a generic instantiation, then we definitely have a subprogram
294       --  specification (all possibilities at this stage include one here)
295
296       Fpart_Sloc := Token_Ptr;
297
298       Check_Misspelling_Of (Tok_Return);
299
300       --  Scan formal part. First a special error check. If we have an
301       --  identifier here, then we have a definite error. If this identifier
302       --  is on the same line as the designator, then we assume it is the
303       --  first formal after a missing left parenthesis
304
305       if Token = Tok_Identifier
306         and then not Token_Is_At_Start_Of_Line
307       then
308             T_Left_Paren; -- to generate message
309             Fpart_List := P_Formal_Part;
310
311       --  Otherwise scan out an optional formal part in the usual manner
312
313       else
314          Fpart_List := P_Parameter_Profile;
315       end if;
316
317       --  We treat what we have as a function specification if FUNCTION was
318       --  used, or if a RETURN is present. This gives better error recovery
319       --  since later RETURN statements will be valid in either case.
320
321       Check_Junk_Semicolon_Before_Return;
322       Result_Node := Error;
323
324       if Token = Tok_Return then
325          if not Func then
326             Error_Msg ("PROCEDURE should be FUNCTION", Fproc_Sloc);
327             Func := True;
328          end if;
329
330          Scan; -- past RETURN
331
332          Result_Not_Null := P_Null_Exclusion;     --  Ada 2005 (AI-231)
333
334          --  Ada 2005 (AI-318-02)
335
336          if Token = Tok_Access then
337             if Ada_Version < Ada_05 then
338                Error_Msg_SC
339                  ("anonymous access result type is an Ada 2005 extension");
340                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
341             end if;
342
343             Result_Node := P_Access_Definition (Result_Not_Null);
344
345          else
346             Result_Node := P_Subtype_Mark;
347             No_Constraint;
348          end if;
349
350       else
351          if Func then
352             Ignore (Tok_Right_Paren);
353             TF_Return;
354          end if;
355       end if;
356
357       if Func then
358          Specification_Node :=
359            New_Node (N_Function_Specification, Fproc_Sloc);
360
361          Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
362          Set_Result_Definition (Specification_Node, Result_Node);
363
364       else
365          Specification_Node :=
366            New_Node (N_Procedure_Specification, Fproc_Sloc);
367       end if;
368
369       Set_Defining_Unit_Name (Specification_Node, Name_Node);
370       Set_Parameter_Specifications (Specification_Node, Fpart_List);
371
372       if Is_Overriding then
373          Set_Must_Override (Specification_Node);
374
375       elsif Not_Overriding then
376          Set_Must_Not_Override (Specification_Node);
377       end if;
378
379       --  Error check: barriers not allowed on protected functions/procedures
380
381       if Token = Tok_When then
382          if Func then
383             Error_Msg_SC ("barrier not allowed on function, only on entry");
384          else
385             Error_Msg_SC ("barrier not allowed on procedure, only on entry");
386          end if;
387
388          Scan; -- past WHEN
389          Discard_Junk_Node (P_Expression);
390       end if;
391
392       --  Deal with case of semicolon ending a subprogram declaration
393
394       if Token = Tok_Semicolon then
395          if not Pf_Flags.Decl then
396             T_Is;
397          end if;
398
399          Scan; -- past semicolon
400
401          --  If semicolon is immediately followed by IS, then ignore the
402          --  semicolon, and go process the body.
403
404          if Token = Tok_Is then
405             Error_Msg_SP ("unexpected semicolon ignored");
406             T_Is; -- ignroe redundant IS's
407             goto Subprogram_Body;
408
409          --  If BEGIN follows in an appropriate column, we immediately
410          --  commence the error action of assuming that the previous
411          --  subprogram declaration should have been a subprogram body,
412          --  i.e. that the terminating semicolon should have been IS.
413
414          elsif Token = Tok_Begin
415             and then Start_Column >= Scope.Table (Scope.Last).Ecol
416          then
417             Error_Msg_SP (""";"" should be IS!");
418             goto Subprogram_Body;
419
420          else
421             goto Subprogram_Declaration;
422          end if;
423
424       --  Case of not followed by semicolon
425
426       else
427          --  Subprogram renaming declaration case
428
429          Check_Misspelling_Of (Tok_Renames);
430
431          if Token = Tok_Renames then
432             if not Pf_Flags.Rnam then
433                Error_Msg_SC ("renaming declaration not allowed here!");
434             end if;
435
436             Rename_Node :=
437               New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
438             Scan; -- past RENAMES
439             Set_Name (Rename_Node, P_Name);
440             Set_Specification (Rename_Node, Specification_Node);
441             TF_Semicolon;
442             Pop_Scope_Stack;
443             return Rename_Node;
444
445          --  Case of IS following subprogram specification
446
447          elsif Token = Tok_Is then
448             T_Is; -- ignore redundant Is's
449
450             if Token_Name = Name_Abstract then
451                Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
452             end if;
453
454             --  Deal nicely with (now obsolete) use of <> in place of abstract
455
456             if Token = Tok_Box then
457                Error_Msg_SC ("ABSTRACT expected");
458                Token := Tok_Abstract;
459             end if;
460
461             --  Abstract subprogram declaration case
462
463             if Token = Tok_Abstract then
464                Absdec_Node :=
465                  New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
466                Set_Specification (Absdec_Node, Specification_Node);
467                Pop_Scope_Stack; -- discard unneeded entry
468                Scan; -- past ABSTRACT
469                TF_Semicolon;
470                return Absdec_Node;
471
472             --  Ada 2005 (AI-248): Parse a null procedure declaration
473
474             elsif Token = Tok_Null then
475                if Ada_Version < Ada_05 then
476                   Error_Msg_SP ("null procedures are an Ada 2005 extension");
477                   Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
478                end if;
479
480                Scan; -- past NULL
481
482                if Func then
483                   Error_Msg_SP ("only procedures can be null");
484                else
485                   Set_Null_Present (Specification_Node);
486                end if;
487
488                TF_Semicolon;
489                goto Subprogram_Declaration;
490
491             --  Check for IS NEW with Formal_Part present and handle nicely
492
493             elsif Token = Tok_New then
494                Error_Msg
495                  ("formal part not allowed in instantiation", Fpart_Sloc);
496                Scan; -- past NEW
497
498                if Func then
499                   Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
500                else
501                   Inst_Node :=
502                     New_Node (N_Procedure_Instantiation, Fproc_Sloc);
503                end if;
504
505                Set_Defining_Unit_Name (Inst_Node, Name_Node);
506                Set_Name (Inst_Node, P_Name);
507                Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
508                TF_Semicolon;
509                Pop_Scope_Stack; -- Don't need scope stack entry in this case
510                return Inst_Node;
511
512             else
513                goto Subprogram_Body;
514             end if;
515
516          --  Here we have a missing IS or missing semicolon, we always guess
517          --  a missing semicolon, since we are pretty good at fixing up a
518          --  semicolon which should really be an IS
519
520          else
521             Error_Msg_AP ("missing "";""");
522             SIS_Missing_Semicolon_Message := Get_Msg_Id;
523             goto Subprogram_Declaration;
524          end if;
525       end if;
526
527       --  Processing for subprogram body
528
529       <<Subprogram_Body>>
530          if not Pf_Flags.Pbod then
531             Error_Msg_SP ("subprogram body not allowed here!");
532          end if;
533
534          --  Subprogram body stub case
535
536          if Separate_Present then
537             if not Pf_Flags.Stub then
538                Error_Msg_SC ("body stub not allowed here!");
539             end if;
540
541             if Nkind (Name_Node) = N_Defining_Operator_Symbol then
542                Error_Msg
543                  ("operator symbol cannot be used as subunit name",
544                   Sloc (Name_Node));
545             end if;
546
547             Stub_Node :=
548               New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
549             Set_Specification (Stub_Node, Specification_Node);
550             Scan; -- past SEPARATE
551             Pop_Scope_Stack;
552             TF_Semicolon;
553             return Stub_Node;
554
555          --  Subprogram body case
556
557          else
558             --  Here is the test for a suspicious IS (i.e. one that looks
559             --  like it might more properly be a semicolon). See separate
560             --  section discussing use of IS instead of semicolon in
561             --  package Parse.
562
563             if (Token in Token_Class_Declk
564                   or else
565                 Token = Tok_Identifier)
566               and then Start_Column <= Scope.Table (Scope.Last).Ecol
567               and then Scope.Last /= 1
568             then
569                Scope.Table (Scope.Last).Etyp := E_Suspicious_Is;
570                Scope.Table (Scope.Last).S_Is := Prev_Token_Ptr;
571             end if;
572
573             Body_Node :=
574               New_Node (N_Subprogram_Body, Sloc (Specification_Node));
575             Set_Specification (Body_Node, Specification_Node);
576             Parse_Decls_Begin_End (Body_Node);
577             return Body_Node;
578          end if;
579
580       --  Processing for subprogram declaration
581
582       <<Subprogram_Declaration>>
583          Decl_Node :=
584            New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
585          Set_Specification (Decl_Node, Specification_Node);
586
587          --  If this is a context in which a subprogram body is permitted,
588          --  set active SIS entry in case (see section titled "Handling
589          --  Semicolon Used in Place of IS" in body of Parser package)
590          --  Note that SIS_Missing_Semicolon_Message is already set properly.
591
592          if Pf_Flags.Pbod then
593             SIS_Labl := Scope.Table (Scope.Last).Labl;
594             SIS_Sloc := Scope.Table (Scope.Last).Sloc;
595             SIS_Ecol := Scope.Table (Scope.Last).Ecol;
596             SIS_Declaration_Node := Decl_Node;
597             SIS_Semicolon_Sloc := Prev_Token_Ptr;
598             SIS_Entry_Active := True;
599          end if;
600
601          Pop_Scope_Stack;
602          return Decl_Node;
603
604    end P_Subprogram;
605
606    ---------------------------------
607    -- 6.1  Subprogram Declaration --
608    ---------------------------------
609
610    --  Parsed by P_Subprogram (6.1)
611
612    ------------------------------------------
613    -- 6.1  Abstract Subprogram Declaration --
614    ------------------------------------------
615
616    --  Parsed by P_Subprogram (6.1)
617
618    -----------------------------------
619    -- 6.1  Subprogram Specification --
620    -----------------------------------
621
622    --  SUBPROGRAM_SPECIFICATION ::=
623    --      procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
624    --    | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
625
626    --  PARAMETER_PROFILE ::= [FORMAL_PART]
627
628    --  PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
629
630    --  Subprogram specifications that appear in subprogram declarations
631    --  are parsed by P_Subprogram (6.1). This routine is used in other
632    --  contexts where subprogram specifications occur.
633
634    --  Note: this routine does not affect the scope stack in any way
635
636    --  Error recovery: can raise Error_Resync
637
638    function P_Subprogram_Specification return Node_Id is
639       Specification_Node : Node_Id;
640       Result_Not_Null    : Boolean;
641       Result_Node        : Node_Id;
642
643    begin
644       if Token = Tok_Function then
645          Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
646          Scan; -- past FUNCTION
647          Ignore (Tok_Body);
648          Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
649          Set_Parameter_Specifications
650            (Specification_Node, P_Parameter_Profile);
651          Check_Junk_Semicolon_Before_Return;
652          TF_Return;
653
654          Result_Not_Null := P_Null_Exclusion;     --  Ada 2005 (AI-231)
655
656          --  Ada 2005 (AI-318-02)
657
658          if Token = Tok_Access then
659             if Ada_Version < Ada_05 then
660                Error_Msg_SC
661                  ("anonymous access result type is an Ada 2005 extension");
662                Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
663             end if;
664
665             Result_Node := P_Access_Definition (Result_Not_Null);
666
667          else
668             Result_Node := P_Subtype_Mark;
669             No_Constraint;
670          end if;
671
672          Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
673          Set_Result_Definition (Specification_Node, Result_Node);
674          return Specification_Node;
675
676       elsif Token = Tok_Procedure then
677          Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
678          Scan; -- past PROCEDURE
679          Ignore (Tok_Body);
680          Set_Defining_Unit_Name
681            (Specification_Node, P_Defining_Program_Unit_Name);
682          Set_Parameter_Specifications
683            (Specification_Node, P_Parameter_Profile);
684          return Specification_Node;
685
686       else
687          Error_Msg_SC ("subprogram specification expected");
688          raise Error_Resync;
689       end if;
690    end P_Subprogram_Specification;
691
692    ---------------------
693    -- 6.1  Designator --
694    ---------------------
695
696    --  DESIGNATOR ::=
697    --    [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
698
699    --  The caller has checked that the initial token is an identifier,
700    --  operator symbol, or string literal. Note that we don't bother to
701    --  do much error diagnosis in this routine, since it is only used for
702    --  the label on END lines, and the routines in package Par.Endh will
703    --  check that the label is appropriate.
704
705    --  Error recovery: cannot raise Error_Resync
706
707    function P_Designator return Node_Id is
708       Ident_Node  : Node_Id;
709       Name_Node   : Node_Id;
710       Prefix_Node : Node_Id;
711
712       function Real_Dot return Boolean;
713       --  Tests if a current token is an interesting period, i.e. is followed
714       --  by an identifier or operator symbol or string literal. If not, it is
715       --  probably just incorrect punctuation to be caught by our caller. Note
716       --  that the case of an operator symbol or string literal is also an
717       --  error, but that is an error that we catch here. If the result is
718       --  True, a real dot has been scanned and we are positioned past it,
719       --  if the result is False, the scan position is unchanged.
720
721       --------------
722       -- Real_Dot --
723       --------------
724
725       function Real_Dot return Boolean is
726          Scan_State  : Saved_Scan_State;
727
728       begin
729          if Token /= Tok_Dot then
730             return False;
731
732          else
733             Save_Scan_State (Scan_State);
734             Scan; -- past dot
735
736             if Token = Tok_Identifier
737               or else Token = Tok_Operator_Symbol
738               or else Token = Tok_String_Literal
739             then
740                return True;
741
742             else
743                Restore_Scan_State (Scan_State);
744                return False;
745             end if;
746          end if;
747       end Real_Dot;
748
749    --  Start of processing for P_Designator
750
751    begin
752       Ident_Node := Token_Node;
753       Scan; -- past initial token
754
755       if Prev_Token = Tok_Operator_Symbol
756         or else Prev_Token = Tok_String_Literal
757         or else not Real_Dot
758       then
759          return Ident_Node;
760
761       --  Child name case
762
763       else
764          Prefix_Node := Ident_Node;
765
766          --  Loop through child names, on entry to this loop, Prefix contains
767          --  the name scanned so far, and Ident_Node is the last identifier.
768
769          loop
770             Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
771             Set_Prefix (Name_Node, Prefix_Node);
772             Ident_Node := P_Identifier;
773             Set_Selector_Name (Name_Node, Ident_Node);
774             Prefix_Node := Name_Node;
775             exit when not Real_Dot;
776          end loop;
777
778          --  On exit from the loop, Ident_Node is the last identifier scanned,
779          --  i.e. the defining identifier, and Prefix_Node is a node for the
780          --  entire name, structured (incorrectly!) as a selected component.
781
782          Name_Node := Prefix (Prefix_Node);
783          Change_Node (Prefix_Node, N_Designator);
784          Set_Name (Prefix_Node, Name_Node);
785          Set_Identifier (Prefix_Node, Ident_Node);
786          return Prefix_Node;
787       end if;
788
789    exception
790       when Error_Resync =>
791          while Token = Tok_Dot or else Token = Tok_Identifier loop
792             Scan;
793          end loop;
794
795          return Error;
796    end P_Designator;
797
798    ------------------------------
799    -- 6.1  Defining Designator --
800    ------------------------------
801
802    --  DEFINING_DESIGNATOR ::=
803    --    DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
804
805    --  Error recovery: cannot raise Error_Resync
806
807    function P_Defining_Designator return Node_Id is
808    begin
809       if Token = Tok_Operator_Symbol then
810          return P_Defining_Operator_Symbol;
811
812       elsif Token = Tok_String_Literal then
813          Error_Msg_SC ("invalid operator name");
814          Scan; -- past junk string
815          return Error;
816
817       else
818          return P_Defining_Program_Unit_Name;
819       end if;
820    end P_Defining_Designator;
821
822    -------------------------------------
823    -- 6.1  Defining Program Unit Name --
824    -------------------------------------
825
826    --  DEFINING_PROGRAM_UNIT_NAME ::=
827    --    [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
828
829    --  Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
830
831    --  Error recovery: cannot raise Error_Resync
832
833    function P_Defining_Program_Unit_Name return Node_Id is
834       Ident_Node  : Node_Id;
835       Name_Node   : Node_Id;
836       Prefix_Node : Node_Id;
837
838    begin
839       --  Set identifier casing if not already set and scan initial identifier
840
841       if Token = Tok_Identifier
842         and then Identifier_Casing (Current_Source_File) = Unknown
843       then
844          Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
845       end if;
846
847       Ident_Node := P_Identifier (C_Dot);
848       Merge_Identifier (Ident_Node, Tok_Return);
849
850       --  Normal case (not child library unit name)
851
852       if Token /= Tok_Dot then
853          Change_Identifier_To_Defining_Identifier (Ident_Node);
854          return Ident_Node;
855
856       --  Child library unit name case
857
858       else
859          if Scope.Last > 1 then
860             Error_Msg_SP ("child unit allowed only at library level");
861             raise Error_Resync;
862
863          elsif Ada_Version = Ada_83 then
864             Error_Msg_SP ("(Ada 83) child unit not allowed!");
865
866          end if;
867
868          Prefix_Node := Ident_Node;
869
870          --  Loop through child names, on entry to this loop, Prefix contains
871          --  the name scanned so far, and Ident_Node is the last identifier.
872
873          loop
874             exit when Token /= Tok_Dot;
875             Name_Node := New_Node (N_Selected_Component, Token_Ptr);
876             Scan; -- past period
877             Set_Prefix (Name_Node, Prefix_Node);
878             Ident_Node := P_Identifier (C_Dot);
879             Set_Selector_Name (Name_Node, Ident_Node);
880             Prefix_Node := Name_Node;
881          end loop;
882
883          --  On exit from the loop, Ident_Node is the last identifier scanned,
884          --  i.e. the defining identifier, and Prefix_Node is a node for the
885          --  entire name, structured (incorrectly!) as a selected component.
886
887          Name_Node := Prefix (Prefix_Node);
888          Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
889          Set_Name (Prefix_Node, Name_Node);
890          Change_Identifier_To_Defining_Identifier (Ident_Node);
891          Set_Defining_Identifier (Prefix_Node, Ident_Node);
892
893          --  All set with unit name parsed
894
895          return Prefix_Node;
896       end if;
897
898    exception
899       when Error_Resync =>
900          while Token = Tok_Dot or else Token = Tok_Identifier loop
901             Scan;
902          end loop;
903
904          return Error;
905    end P_Defining_Program_Unit_Name;
906
907    --------------------------
908    -- 6.1  Operator Symbol --
909    --------------------------
910
911    --  OPERATOR_SYMBOL ::= STRING_LITERAL
912
913    --  Operator symbol is returned by the scanner as Tok_Operator_Symbol
914
915    -----------------------------------
916    -- 6.1  Defining Operator Symbol --
917    -----------------------------------
918
919    --  DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
920
921    --  The caller has checked that the initial symbol is an operator symbol
922
923    function P_Defining_Operator_Symbol return Node_Id is
924       Op_Node : Node_Id;
925
926    begin
927       Op_Node := Token_Node;
928       Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
929       Scan; -- past operator symbol
930       return Op_Node;
931    end P_Defining_Operator_Symbol;
932
933    ----------------------------
934    -- 6.1  Parameter_Profile --
935    ----------------------------
936
937    --  PARAMETER_PROFILE ::= [FORMAL_PART]
938
939    --  Empty is returned if no formal part is present
940
941    --  Error recovery: cannot raise Error_Resync
942
943    function P_Parameter_Profile return List_Id is
944    begin
945       if Token = Tok_Left_Paren then
946          Scan; -- part left paren
947          return P_Formal_Part;
948       else
949          return No_List;
950       end if;
951    end P_Parameter_Profile;
952
953    ---------------------------------------
954    -- 6.1  Parameter And Result Profile --
955    ---------------------------------------
956
957    --  Parsed by its parent construct, which uses P_Parameter_Profile to
958    --  parse the parameters, and P_Subtype_Mark to parse the return type.
959
960    ----------------------
961    -- 6.1  Formal part --
962    ----------------------
963
964    --  FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
965
966    --  PARAMETER_SPECIFICATION ::=
967    --    DEFINING_IDENTIFIER_LIST : MODE [NULL_EXCLUSION] SUBTYPE_MARK
968    --      [:= DEFAULT_EXPRESSION]
969    --  | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
970    --      [:= DEFAULT_EXPRESSION]
971
972    --  This scans the construct Formal_Part. The caller has already checked
973    --  that the initial token is a left parenthesis, and skipped past it, so
974    --  that on entry Token is the first token following the left parenthesis.
975
976    --  Error recovery: cannot raise Error_Resync
977
978    function P_Formal_Part return List_Id is
979       Specification_List : List_Id;
980       Specification_Node : Node_Id;
981       Scan_State         : Saved_Scan_State;
982       Num_Idents         : Nat;
983       Ident              : Nat;
984       Ident_Sloc         : Source_Ptr;
985       Not_Null_Present   : Boolean := False;
986
987       Idents : array (Int range 1 .. 4096) of Entity_Id;
988       --  This array holds the list of defining identifiers. The upper bound
989       --  of 4096 is intended to be essentially infinite, and we do not even
990       --  bother to check for it being exceeded.
991
992    begin
993       Specification_List := New_List;
994       Specification_Loop : loop
995          begin
996             if Token = Tok_Pragma then
997                P_Pragmas_Misplaced;
998             end if;
999
1000             Ignore (Tok_Left_Paren);
1001             Ident_Sloc := Token_Ptr;
1002             Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1003             Num_Idents := 1;
1004
1005             Ident_Loop : loop
1006                exit Ident_Loop when Token = Tok_Colon;
1007
1008                --  The only valid tokens are colon and comma, so if we have
1009                --  neither do a bit of investigation to see which is the
1010                --  better choice for insertion.
1011
1012                if Token /= Tok_Comma then
1013
1014                   --  Assume colon if IN or OUT keyword found
1015
1016                   exit Ident_Loop when Token = Tok_In or else Token = Tok_Out;
1017
1018                   --  Otherwise scan ahead
1019
1020                   Save_Scan_State (Scan_State);
1021                   Look_Ahead : loop
1022
1023                      --  If we run into a semicolon, then assume that a
1024                      --  colon was missing, e.g.  Parms (X Y; ...). Also
1025                      --  assume missing colon on EOF (a real disaster!)
1026                      --  and on a right paren, e.g. Parms (X Y), and also
1027                      --  on an assignment symbol, e.g. Parms (X Y := ..)
1028
1029                      if Token = Tok_Semicolon
1030                        or else Token = Tok_Right_Paren
1031                        or else Token = Tok_EOF
1032                        or else Token = Tok_Colon_Equal
1033                      then
1034                         Restore_Scan_State (Scan_State);
1035                         exit Ident_Loop;
1036
1037                      --  If we run into a colon, assume that we had a missing
1038                      --  comma, e.g. Parms (A B : ...). Also assume a missing
1039                      --  comma if we hit another comma, e.g. Parms (A B, C ..)
1040
1041                      elsif Token = Tok_Colon
1042                        or else Token = Tok_Comma
1043                      then
1044                         Restore_Scan_State (Scan_State);
1045                         exit Look_Ahead;
1046                      end if;
1047
1048                      Scan;
1049                   end loop Look_Ahead;
1050                end if;
1051
1052                --  Here if a comma is present, or to be assumed
1053
1054                T_Comma;
1055                Num_Idents := Num_Idents + 1;
1056                Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1057             end loop Ident_Loop;
1058
1059             --  Fall through the loop on encountering a colon, or deciding
1060             --  that there is a missing colon.
1061
1062             T_Colon;
1063
1064             --  If there are multiple identifiers, we repeatedly scan the
1065             --  type and initialization expression information by resetting
1066             --  the scan pointer (so that we get completely separate trees
1067             --  for each occurrence).
1068
1069             if Num_Idents > 1 then
1070                Save_Scan_State (Scan_State);
1071             end if;
1072
1073             --  Loop through defining identifiers in list
1074
1075             Ident := 1;
1076
1077             Ident_List_Loop : loop
1078                Specification_Node :=
1079                  New_Node (N_Parameter_Specification, Ident_Sloc);
1080                Set_Defining_Identifier (Specification_Node, Idents (Ident));
1081                Not_Null_Present := P_Null_Exclusion;     --  Ada 2005 (AI-231)
1082
1083                if Token = Tok_Access then
1084                   Set_Null_Exclusion_Present
1085                     (Specification_Node, Not_Null_Present);
1086
1087                   if Ada_Version = Ada_83 then
1088                      Error_Msg_SC ("(Ada 83) access parameters not allowed");
1089                   end if;
1090
1091                   Set_Parameter_Type (Specification_Node,
1092                     P_Access_Definition (Not_Null_Present));
1093
1094                else
1095                   if Token = Tok_In or else Token = Tok_Out then
1096                      if Not_Null_Present then
1097                         Error_Msg_SC
1098                           ("ACCESS must be placed after the parameter mode");
1099                      end if;
1100
1101                      P_Mode (Specification_Node);
1102                      Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1103                   end if;
1104
1105                   Set_Null_Exclusion_Present
1106                     (Specification_Node, Not_Null_Present);
1107
1108                   if Token = Tok_Procedure
1109                        or else
1110                      Token = Tok_Function
1111                   then
1112                      Error_Msg_SC ("formal subprogram parameter not allowed");
1113                      Scan;
1114
1115                      if Token = Tok_Left_Paren then
1116                         Discard_Junk_List (P_Formal_Part);
1117                      end if;
1118
1119                      if Token = Tok_Return then
1120                         Scan;
1121                         Discard_Junk_Node (P_Subtype_Mark);
1122                      end if;
1123
1124                      Set_Parameter_Type (Specification_Node, Error);
1125
1126                   else
1127                      Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1128                      No_Constraint;
1129                   end if;
1130                end if;
1131
1132                Set_Expression (Specification_Node, Init_Expr_Opt (True));
1133
1134                if Ident > 1 then
1135                   Set_Prev_Ids (Specification_Node, True);
1136                end if;
1137
1138                if Ident < Num_Idents then
1139                   Set_More_Ids (Specification_Node, True);
1140                end if;
1141
1142                Append (Specification_Node, Specification_List);
1143                exit Ident_List_Loop when Ident = Num_Idents;
1144                Ident := Ident + 1;
1145                Restore_Scan_State (Scan_State);
1146             end loop Ident_List_Loop;
1147
1148          exception
1149             when Error_Resync =>
1150                Resync_Semicolon_List;
1151          end;
1152
1153          if Token = Tok_Semicolon then
1154             Save_Scan_State (Scan_State);
1155             Scan; -- past semicolon
1156
1157             --  If we have RETURN or IS after the semicolon, then assume
1158             --  that semicolon should have been a right parenthesis and exit
1159
1160             if Token = Tok_Is or else Token = Tok_Return then
1161                Error_Msg_SP ("expected "")"" in place of "";""");
1162                exit Specification_Loop;
1163             end if;
1164
1165             --  If we have a declaration keyword after the semicolon, then
1166             --  assume we had a missing right parenthesis and terminate list
1167
1168             if Token in Token_Class_Declk then
1169                Error_Msg_AP ("missing "")""");
1170                Restore_Scan_State (Scan_State);
1171                exit Specification_Loop;
1172             end if;
1173
1174          elsif Token = Tok_Right_Paren then
1175             Scan; -- past right paren
1176             exit Specification_Loop;
1177
1178          --  Special check for common error of using comma instead of semicolon
1179
1180          elsif Token = Tok_Comma then
1181             T_Semicolon;
1182             Scan; -- past comma
1183
1184          --  Special check for omitted separator
1185
1186          elsif Token = Tok_Identifier then
1187             T_Semicolon;
1188
1189          --  If nothing sensible, skip to next semicolon or right paren
1190
1191          else
1192             T_Semicolon;
1193             Resync_Semicolon_List;
1194
1195             if Token = Tok_Semicolon then
1196                Scan; -- past semicolon
1197             else
1198                T_Right_Paren;
1199                exit Specification_Loop;
1200             end if;
1201          end if;
1202       end loop Specification_Loop;
1203
1204       return Specification_List;
1205    end P_Formal_Part;
1206
1207    ----------------------------------
1208    -- 6.1  Parameter Specification --
1209    ----------------------------------
1210
1211    --  Parsed by P_Formal_Part (6.1)
1212
1213    ---------------
1214    -- 6.1  Mode --
1215    ---------------
1216
1217    --  MODE ::= [in] | in out | out
1218
1219    --  There is no explicit node in the tree for the Mode. Instead the
1220    --  In_Present and Out_Present flags are set in the parent node to
1221    --  record the presence of keywords specifying the mode.
1222
1223    --  Error_Recovery: cannot raise Error_Resync
1224
1225    procedure P_Mode (Node : Node_Id) is
1226    begin
1227       if Token = Tok_In then
1228          Scan; -- past IN
1229          Set_In_Present (Node, True);
1230       end if;
1231
1232       if Token = Tok_Out then
1233          Scan; -- past OUT
1234          Set_Out_Present (Node, True);
1235       end if;
1236
1237       if Token = Tok_In then
1238          Error_Msg_SC ("IN must preceed OUT in parameter mode");
1239          Scan; -- past IN
1240          Set_In_Present (Node, True);
1241       end if;
1242    end P_Mode;
1243
1244    --------------------------
1245    -- 6.3  Subprogram Body --
1246    --------------------------
1247
1248    --  Parsed by P_Subprogram (6.1)
1249
1250    -----------------------------------
1251    -- 6.4  Procedure Call Statement --
1252    -----------------------------------
1253
1254    --  Parsed by P_Sequence_Of_Statements (5.1)
1255
1256    ------------------------
1257    -- 6.4  Function Call --
1258    ------------------------
1259
1260    --  Parsed by P_Call_Or_Name (4.1)
1261
1262    --------------------------------
1263    -- 6.4  Actual Parameter Part --
1264    --------------------------------
1265
1266    --  Parsed by P_Call_Or_Name (4.1)
1267
1268    --------------------------------
1269    -- 6.4  Parameter Association --
1270    --------------------------------
1271
1272    --  Parsed by P_Call_Or_Name (4.1)
1273
1274    ------------------------------------
1275    -- 6.4  Explicit Actual Parameter --
1276    ------------------------------------
1277
1278    --  Parsed by P_Call_Or_Name (4.1)
1279
1280    ---------------------------
1281    -- 6.5  Return Statement --
1282    ---------------------------
1283
1284    --  RETURN_STATEMENT ::= return [EXPRESSION];
1285
1286    --  The caller has checked that the initial token is RETURN
1287
1288    --  Error recovery: can raise Error_Resync
1289
1290    function P_Return_Statement return Node_Id is
1291       Return_Node : Node_Id;
1292
1293    begin
1294       Return_Node := New_Node (N_Return_Statement, Token_Ptr);
1295
1296       --  Sloc points to RETURN
1297       --  Expression (Op3)
1298
1299       Scan; -- past RETURN
1300
1301       if Token /= Tok_Semicolon then
1302
1303          --  If no semicolon, then scan an expression, except that
1304          --  we avoid trying to scan an expression if we are at an
1305          --  expression terminator since in that case the best error
1306          --  message is probably that we have a missing semicolon.
1307
1308          if Token not in Token_Class_Eterm then
1309             Set_Expression (Return_Node, P_Expression_No_Right_Paren);
1310          end if;
1311       end if;
1312
1313       TF_Semicolon;
1314       return Return_Node;
1315    end P_Return_Statement;
1316
1317 end Ch6;