OSDN Git Service

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