]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/par-ch6.adb
[Ada] Fix assertion failure on functions with contracts
[thirdparty/gcc.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-2020, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 pragma Style_Checks (All_Checks);
27 -- Turn off subprogram body ordering check. Subprograms are in order
28 -- by RM section rather than alphabetical
29
30 with Sinfo.CN; use Sinfo.CN;
31
32 separate (Par)
33 package body Ch6 is
34
35 -- Local subprograms, used only in this chapter
36
37 function P_Defining_Designator return Node_Id;
38 function P_Defining_Operator_Symbol return Node_Id;
39 function P_Return_Object_Declaration return Node_Id;
40
41 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id);
42 -- Decl_Node is a N_Object_Declaration. Set the Null_Exclusion_Present and
43 -- Object_Definition fields of Decl_Node.
44
45 procedure Check_Junk_Semicolon_Before_Return;
46 -- Check for common error of junk semicolon before RETURN keyword of
47 -- function specification. If present, skip over it with appropriate error
48 -- message, leaving Scan_Ptr pointing to the RETURN after. This routine
49 -- also deals with a possibly misspelled version of Return.
50
51 procedure No_Constraint_Maybe_Expr_Func;
52 -- Called after scanning return subtype to check for missing constraint,
53 -- taking into account the possibility of an occurrence of an expression
54 -- function where the IS has been forgotten.
55
56 ----------------------------------------
57 -- Check_Junk_Semicolon_Before_Return --
58 ----------------------------------------
59
60 procedure Check_Junk_Semicolon_Before_Return is
61 Scan_State : Saved_Scan_State;
62
63 begin
64 if Token = Tok_Semicolon then
65 Save_Scan_State (Scan_State);
66 Scan; -- past the semicolon
67
68 if Token = Tok_Return then
69 Restore_Scan_State (Scan_State);
70 Error_Msg_SC -- CODEFIX
71 ("|extra "";"" ignored");
72 Scan; -- rescan past junk semicolon
73 else
74 Restore_Scan_State (Scan_State);
75 end if;
76 end if;
77 end Check_Junk_Semicolon_Before_Return;
78
79 -----------------------------------
80 -- No_Constraint_Maybe_Expr_Func --
81 -----------------------------------
82
83 procedure No_Constraint_Maybe_Expr_Func is
84 begin
85 -- If we have a left paren at the start of the line, then assume this is
86 -- the case of an expression function with missing IS. We do not have to
87 -- diagnose the missing IS, that is done elsewhere. We do this game in
88 -- Ada 2012 mode where expression functions are legal.
89
90 if Token = Tok_Left_Paren
91 and Ada_Version >= Ada_2012
92 and Token_Is_At_Start_Of_Line
93 then
94 -- One exception if we have "(token .." then this is a constraint
95
96 declare
97 Scan_State : Saved_Scan_State;
98
99 begin
100 Save_Scan_State (Scan_State);
101 Scan; -- past left paren
102 Scan; -- past following token
103
104 -- If we have "(token .." then restore scan state and treat as
105 -- unexpected constraint.
106
107 if Token = Tok_Dot_Dot then
108 Restore_Scan_State (Scan_State);
109 No_Constraint;
110
111 -- Otherwise we treat this as an expression function
112
113 else
114 Restore_Scan_State (Scan_State);
115 end if;
116 end;
117
118 -- Otherwise use standard routine to check for no constraint present
119
120 else
121 No_Constraint;
122 end if;
123 end No_Constraint_Maybe_Expr_Func;
124
125 -----------------------------------------------------
126 -- 6.1 Subprogram (Also 6.3, 8.5.4, 10.1.3, 12.3) --
127 -----------------------------------------------------
128
129 -- This routine scans out a subprogram declaration, subprogram body,
130 -- subprogram renaming declaration or subprogram generic instantiation.
131 -- It also handles the new Ada 2012 expression function form
132
133 -- SUBPROGRAM_DECLARATION ::=
134 -- SUBPROGRAM_SPECIFICATION
135 -- [ASPECT_SPECIFICATIONS];
136
137 -- ABSTRACT_SUBPROGRAM_DECLARATION ::=
138 -- SUBPROGRAM_SPECIFICATION is abstract
139 -- [ASPECT_SPECIFICATIONS];
140
141 -- SUBPROGRAM_SPECIFICATION ::=
142 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
143 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
144
145 -- PARAMETER_PROFILE ::= [FORMAL_PART]
146
147 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
148
149 -- SUBPROGRAM_BODY ::=
150 -- SUBPROGRAM_SPECIFICATION [ASPECT_SPECIFICATIONS] is
151 -- DECLARATIVE_PART
152 -- begin
153 -- HANDLED_SEQUENCE_OF_STATEMENTS
154 -- end [DESIGNATOR];
155
156 -- SUBPROGRAM_RENAMING_DECLARATION ::=
157 -- SUBPROGRAM_SPECIFICATION renames callable_entity_NAME
158 -- [ASPECT_SPECIFICATIONS];
159
160 -- SUBPROGRAM_BODY_STUB ::=
161 -- SUBPROGRAM_SPECIFICATION is separate
162 -- [ASPECT_SPECIFICATIONS];
163
164 -- GENERIC_INSTANTIATION ::=
165 -- procedure DEFINING_PROGRAM_UNIT_NAME is
166 -- new generic_procedure_NAME [GENERIC_ACTUAL_PART]
167 -- [ASPECT_SPECIFICATIONS];
168 -- | function DEFINING_DESIGNATOR is
169 -- new generic_function_NAME [GENERIC_ACTUAL_PART]
170 -- [ASPECT_SPECIFICATIONS];
171
172 -- NULL_PROCEDURE_DECLARATION ::=
173 -- SUBPROGRAM_SPECIFICATION is null;
174
175 -- Null procedures are an Ada 2005 feature. A null procedure declaration
176 -- is classified as a basic declarative item, but it is parsed here, with
177 -- other subprogram constructs.
178
179 -- EXPRESSION_FUNCTION ::=
180 -- FUNCTION SPECIFICATION IS (EXPRESSION)
181 -- [ASPECT_SPECIFICATIONS];
182
183 -- The value in Pf_Flags indicates which of these possible declarations
184 -- is acceptable to the caller:
185
186 -- Pf_Flags.Decl Set if declaration OK
187 -- Pf_Flags.Gins Set if generic instantiation OK
188 -- Pf_Flags.Pbod Set if proper body OK
189 -- Pf_Flags.Rnam Set if renaming declaration OK
190 -- Pf_Flags.Stub Set if body stub OK
191 -- Pf_Flags.Pexp Set if expression function OK
192
193 -- If an inappropriate form is encountered, it is scanned out but an
194 -- error message indicating that it is appearing in an inappropriate
195 -- context is issued. The only possible values for Pf_Flags are those
196 -- defined as constants in the Par package.
197
198 -- The caller has checked that the initial token is FUNCTION, PROCEDURE,
199 -- NOT or OVERRIDING.
200
201 -- Error recovery: cannot raise Error_Resync
202
203 function P_Subprogram (Pf_Flags : Pf_Rec) return Node_Id is
204 Specification_Node : Node_Id;
205 Name_Node : Node_Id;
206 Aspects : List_Id;
207 Fpart_List : List_Id;
208 Fpart_Sloc : Source_Ptr;
209 Result_Not_Null : Boolean := False;
210 Result_Node : Node_Id;
211 Inst_Node : Node_Id;
212 Body_Node : Node_Id;
213 Decl_Node : Node_Id;
214 Rename_Node : Node_Id;
215 Absdec_Node : Node_Id;
216 Stub_Node : Node_Id;
217 Fproc_Sloc : Source_Ptr;
218 Func : Boolean;
219 Scan_State : Saved_Scan_State;
220
221 -- Flags for optional overriding indication. Two flags are needed,
222 -- to distinguish positive and negative overriding indicators from
223 -- the absence of any indicator.
224
225 Is_Overriding : Boolean := False;
226 Not_Overriding : Boolean := False;
227
228 begin
229 -- Set up scope stack entry. Note that the Labl field will be set later
230
231 SIS_Entry_Active := False;
232 SIS_Aspect_Import_Seen := False;
233 SIS_Missing_Semicolon_Message := No_Error_Msg;
234 Push_Scope_Stack;
235 Scopes (Scope.Last).Sloc := Token_Ptr;
236 Scopes (Scope.Last).Etyp := E_Name;
237 Scopes (Scope.Last).Ecol := Start_Column;
238 Scopes (Scope.Last).Lreq := False;
239
240 Aspects := Empty_List;
241
242 -- Ada 2005: Scan leading NOT OVERRIDING indicator
243
244 if Token = Tok_Not then
245 Scan; -- past NOT
246
247 if Token = Tok_Overriding then
248 Scan; -- past OVERRIDING
249 Not_Overriding := True;
250
251 -- Overriding keyword used in non Ada 2005 mode
252
253 elsif Token = Tok_Identifier
254 and then Token_Name = Name_Overriding
255 then
256 Error_Msg_SC ("overriding indicator is an Ada 2005 extension");
257 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
258 Scan; -- past Overriding
259 Not_Overriding := True;
260
261 else
262 Error_Msg_SC -- CODEFIX
263 ("OVERRIDING expected!");
264 end if;
265
266 -- Ada 2005: scan leading OVERRIDING indicator
267
268 -- Note: in the case of OVERRIDING keyword used in Ada 95 mode, the
269 -- declaration circuit already gave an error message and changed the
270 -- token to Tok_Overriding.
271
272 elsif Token = Tok_Overriding then
273 Scan; -- past OVERRIDING
274 Is_Overriding := True;
275 end if;
276
277 if Is_Overriding or else Not_Overriding then
278
279 -- Note that if we are not in Ada_2005 mode, error messages have
280 -- already been given, so no need to give another message here.
281
282 -- An overriding indicator is allowed for subprogram declarations,
283 -- bodies (including subunits), renamings, stubs, and instantiations.
284 -- The test against Pf_Decl_Pbod is added to account for the case of
285 -- subprograms declared in a protected type, where only subprogram
286 -- declarations and bodies can occur. The Pf_Pbod case is for
287 -- subunits.
288
289 if Pf_Flags /= Pf_Decl_Gins_Pbod_Rnam_Stub_Pexp
290 and then
291 Pf_Flags /= Pf_Decl_Pbod_Pexp
292 and then
293 Pf_Flags /= Pf_Pbod_Pexp
294 then
295 Error_Msg_SC ("overriding indicator not allowed here!");
296
297 elsif Token /= Tok_Function and then Token /= Tok_Procedure then
298 Error_Msg_SC -- CODEFIX
299 ("FUNCTION or PROCEDURE expected!");
300 end if;
301 end if;
302
303 Func := (Token = Tok_Function);
304 Fproc_Sloc := Token_Ptr;
305 Scan; -- past FUNCTION or PROCEDURE
306 Ignore (Tok_Type);
307 Ignore (Tok_Body);
308
309 if Func then
310 Name_Node := P_Defining_Designator;
311
312 if Nkind (Name_Node) = N_Defining_Operator_Symbol
313 and then Scope.Last = 1
314 then
315 Error_Msg_SP ("operator symbol not allowed at library level");
316 Name_Node := New_Entity (N_Defining_Identifier, Sloc (Name_Node));
317
318 -- Set name from file name, we need some junk name, and that's
319 -- as good as anything. This is only approximate, since we do
320 -- not do anything with non-standard name translations.
321
322 Get_Name_String (File_Name (Current_Source_File));
323
324 for J in 1 .. Name_Len loop
325 if Name_Buffer (J) = '.' then
326 Name_Len := J - 1;
327 exit;
328 end if;
329 end loop;
330
331 Set_Chars (Name_Node, Name_Find);
332 Set_Error_Posted (Name_Node);
333 end if;
334
335 else
336 Name_Node := P_Defining_Program_Unit_Name;
337 end if;
338
339 Scopes (Scope.Last).Labl := Name_Node;
340 Current_Node := Name_Node;
341 Ignore (Tok_Colon);
342
343 -- Deal with generic instantiation, the one case in which we do not
344 -- have a subprogram specification as part of whatever we are parsing
345
346 if Token = Tok_Is then
347 Save_Scan_State (Scan_State); -- at the IS
348 T_Is; -- checks for redundant IS
349
350 if Token = Tok_New then
351 if not Pf_Flags.Gins then
352 Error_Msg_SC ("generic instantiation not allowed here!");
353 end if;
354
355 Scan; -- past NEW
356
357 if Func then
358 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
359 Set_Name (Inst_Node, P_Function_Name);
360 else
361 Inst_Node := New_Node (N_Procedure_Instantiation, Fproc_Sloc);
362 Set_Name (Inst_Node, P_Qualified_Simple_Name);
363 end if;
364
365 Set_Defining_Unit_Name (Inst_Node, Name_Node);
366 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
367 P_Aspect_Specifications (Inst_Node);
368 Pop_Scope_Stack; -- Don't need scope stack entry in this case
369
370 if Is_Overriding then
371 Set_Must_Override (Inst_Node);
372
373 elsif Not_Overriding then
374 Set_Must_Not_Override (Inst_Node);
375 end if;
376
377 return Inst_Node;
378
379 else
380 Restore_Scan_State (Scan_State); -- to the IS
381 end if;
382 end if;
383
384 -- If not a generic instantiation, then we definitely have a subprogram
385 -- specification (all possibilities at this stage include one here)
386
387 Fpart_Sloc := Token_Ptr;
388
389 Check_Misspelling_Of (Tok_Return);
390
391 -- Scan formal part. First a special error check. If we have an
392 -- identifier here, then we have a definite error. If this identifier
393 -- is on the same line as the designator, then we assume it is the
394 -- first formal after a missing left parenthesis
395
396 if Token = Tok_Identifier
397 and then not Token_Is_At_Start_Of_Line
398 then
399 T_Left_Paren; -- to generate message
400 Fpart_List := P_Formal_Part;
401
402 -- Otherwise scan out an optional formal part in the usual manner
403
404 else
405 Fpart_List := P_Parameter_Profile;
406 end if;
407
408 -- We treat what we have as a function specification if FUNCTION was
409 -- used, or if a RETURN is present. This gives better error recovery
410 -- since later RETURN statements will be valid in either case.
411
412 Check_Junk_Semicolon_Before_Return;
413 Result_Node := Error;
414
415 if Token = Tok_Return then
416 if not Func then
417 Error_Msg -- CODEFIX
418 ("PROCEDURE should be FUNCTION", Fproc_Sloc);
419 Func := True;
420 end if;
421
422 Scan; -- past RETURN
423
424 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
425
426 -- Ada 2005 (AI-318-02)
427
428 if Token = Tok_Access then
429 if Ada_Version < Ada_2005 then
430 Error_Msg_SC
431 ("anonymous access result type is an Ada 2005 extension");
432 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
433 end if;
434
435 Result_Node := P_Access_Definition (Result_Not_Null);
436
437 else
438 Result_Node := P_Subtype_Mark;
439 No_Constraint_Maybe_Expr_Func;
440 end if;
441
442 else
443 -- Skip extra parenthesis at end of formal part
444
445 Ignore (Tok_Right_Paren);
446
447 -- For function, scan result subtype
448
449 if Func then
450 TF_Return;
451
452 if Prev_Token = Tok_Return then
453 Result_Node := P_Subtype_Mark;
454 end if;
455 end if;
456 end if;
457
458 if Func then
459 Specification_Node :=
460 New_Node (N_Function_Specification, Fproc_Sloc);
461
462 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
463 Set_Result_Definition (Specification_Node, Result_Node);
464
465 else
466 Specification_Node :=
467 New_Node (N_Procedure_Specification, Fproc_Sloc);
468 end if;
469
470 Set_Defining_Unit_Name (Specification_Node, Name_Node);
471 Set_Parameter_Specifications (Specification_Node, Fpart_List);
472
473 if Is_Overriding then
474 Set_Must_Override (Specification_Node);
475
476 elsif Not_Overriding then
477 Set_Must_Not_Override (Specification_Node);
478 end if;
479
480 -- Error check: barriers not allowed on protected functions/procedures
481
482 if Token = Tok_When then
483 if Func then
484 Error_Msg_SC ("barrier not allowed on function, only on entry");
485 else
486 Error_Msg_SC ("barrier not allowed on procedure, only on entry");
487 end if;
488
489 Scan; -- past WHEN
490 Discard_Junk_Node (P_Expression);
491 end if;
492
493 -- Deal with semicolon followed by IS. We want to treat this as IS
494
495 if Token = Tok_Semicolon then
496 Save_Scan_State (Scan_State);
497 Scan; -- past semicolon
498
499 if Token = Tok_Is then
500 Error_Msg_SP -- CODEFIX
501 ("extra "";"" ignored");
502 else
503 Restore_Scan_State (Scan_State);
504 end if;
505 end if;
506
507 -- Subprogram declaration ended by aspect specifications
508
509 if Aspect_Specifications_Present then
510 goto Subprogram_Declaration;
511
512 -- Deal with case of semicolon ending a subprogram declaration
513
514 elsif Token = Tok_Semicolon then
515 if not Pf_Flags.Decl then
516 T_Is;
517 end if;
518
519 Save_Scan_State (Scan_State);
520 Scan; -- past semicolon
521
522 -- If semicolon is immediately followed by IS, then ignore the
523 -- semicolon, and go process the body.
524
525 if Token = Tok_Is then
526 Error_Msg_SP -- CODEFIX
527 ("|extra "";"" ignored");
528 T_Is; -- scan past IS
529 goto Subprogram_Body;
530
531 -- If BEGIN follows in an appropriate column, we immediately
532 -- commence the error action of assuming that the previous
533 -- subprogram declaration should have been a subprogram body,
534 -- i.e. that the terminating semicolon should have been IS.
535
536 elsif Token = Tok_Begin
537 and then Start_Column >= Scopes (Scope.Last).Ecol
538 then
539 Error_Msg_SP -- CODEFIX
540 ("|"";"" should be IS!");
541 goto Subprogram_Body;
542
543 else
544 Restore_Scan_State (Scan_State);
545 goto Subprogram_Declaration;
546 end if;
547
548 -- Case of not followed by semicolon
549
550 else
551 -- Subprogram renaming declaration case
552
553 Check_Misspelling_Of (Tok_Renames);
554
555 if Token = Tok_Renames then
556 if not Pf_Flags.Rnam then
557 Error_Msg_SC ("renaming declaration not allowed here!");
558 end if;
559
560 Rename_Node :=
561 New_Node (N_Subprogram_Renaming_Declaration, Token_Ptr);
562 Scan; -- past RENAMES
563 Set_Name (Rename_Node, P_Name);
564 Set_Specification (Rename_Node, Specification_Node);
565 P_Aspect_Specifications (Rename_Node);
566 TF_Semicolon;
567 Pop_Scope_Stack;
568 return Rename_Node;
569
570 -- Case of IS following subprogram specification
571
572 elsif Token = Tok_Is then
573 T_Is; -- ignore redundant Is's
574
575 if Token_Name = Name_Abstract then
576 Check_95_Keyword (Tok_Abstract, Tok_Semicolon);
577 end if;
578
579 -- Deal nicely with (now obsolete) use of <> in place of abstract
580
581 if Token = Tok_Box then
582 Error_Msg_SC -- CODEFIX
583 ("ABSTRACT expected");
584 Token := Tok_Abstract;
585 end if;
586
587 -- Abstract subprogram declaration case
588
589 if Token = Tok_Abstract then
590 Absdec_Node :=
591 New_Node (N_Abstract_Subprogram_Declaration, Token_Ptr);
592 Set_Specification (Absdec_Node, Specification_Node);
593 Pop_Scope_Stack; -- discard unneeded entry
594 Scan; -- past ABSTRACT
595 P_Aspect_Specifications (Absdec_Node);
596 return Absdec_Node;
597
598 -- Ada 2005 (AI-248): Parse a null procedure declaration
599
600 elsif Token = Tok_Null then
601 if Ada_Version < Ada_2005 then
602 Error_Msg_SP ("null procedures are an Ada 2005 extension");
603 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
604 end if;
605
606 Scan; -- past NULL
607
608 if Func then
609 Error_Msg_SP ("only procedures can be null");
610 else
611 Set_Null_Present (Specification_Node);
612 Set_Null_Statement (Specification_Node,
613 New_Node (N_Null_Statement, Prev_Token_Ptr));
614 end if;
615
616 goto Subprogram_Declaration;
617
618 -- Check for IS NEW with Formal_Part present and handle nicely
619
620 elsif Token = Tok_New then
621 Error_Msg
622 ("formal part not allowed in instantiation", Fpart_Sloc);
623 Scan; -- past NEW
624
625 if Func then
626 Inst_Node := New_Node (N_Function_Instantiation, Fproc_Sloc);
627 else
628 Inst_Node :=
629 New_Node (N_Procedure_Instantiation, Fproc_Sloc);
630 end if;
631
632 Set_Defining_Unit_Name (Inst_Node, Name_Node);
633 Set_Name (Inst_Node, P_Name);
634 Set_Generic_Associations (Inst_Node, P_Generic_Actual_Part_Opt);
635 TF_Semicolon;
636 Pop_Scope_Stack; -- Don't need scope stack entry in this case
637 return Inst_Node;
638
639 else
640 goto Subprogram_Body;
641 end if;
642
643 -- Aspect specifications present
644
645 elsif Aspect_Specifications_Present then
646 goto Subprogram_Declaration;
647
648 -- Here we have a missing IS or missing semicolon
649
650 else
651 -- If the next token is a left paren at the start of a line, then
652 -- this is almost certainly the start of the expression for an
653 -- expression function, so in this case guess a missing IS.
654
655 if Token = Tok_Left_Paren and then Token_Is_At_Start_Of_Line then
656 Error_Msg_AP -- CODEFIX
657 ("missing IS");
658
659 -- In all other cases, we guess a missing semicolon, since we are
660 -- good at fixing up a semicolon which should really be an IS.
661
662 else
663 Error_Msg_AP -- CODEFIX
664 ("|missing "";""");
665 SIS_Missing_Semicolon_Message := Get_Msg_Id;
666 goto Subprogram_Declaration;
667 end if;
668 end if;
669 end if;
670
671 -- Processing for stub or subprogram body or expression function
672
673 <<Subprogram_Body>>
674
675 -- Subprogram body stub case
676
677 if Separate_Present then
678 if not Pf_Flags.Stub then
679 Error_Msg_SC ("body stub not allowed here!");
680 end if;
681
682 if Nkind (Name_Node) = N_Defining_Operator_Symbol then
683 Error_Msg
684 ("operator symbol cannot be used as subunit name",
685 Sloc (Name_Node));
686 end if;
687
688 Scan; -- past SEPARATE
689
690 Stub_Node :=
691 New_Node (N_Subprogram_Body_Stub, Sloc (Specification_Node));
692 Set_Specification (Stub_Node, Specification_Node);
693
694 if Is_Non_Empty_List (Aspects) then
695 Error_Msg
696 ("aspect specifications must come after SEPARATE",
697 Sloc (First (Aspects)));
698 end if;
699
700 P_Aspect_Specifications (Stub_Node, Semicolon => False);
701 TF_Semicolon;
702 Pop_Scope_Stack;
703 return Stub_Node;
704
705 -- Subprogram body or expression function case
706
707 else
708 Scan_Body_Or_Expression_Function : declare
709
710 function Likely_Expression_Function return Boolean;
711 -- Returns True if we have a probable case of an expression
712 -- function omitting the parentheses, if so, returns True
713 -- and emits an appropriate error message, else returns False.
714
715 --------------------------------
716 -- Likely_Expression_Function --
717 --------------------------------
718
719 function Likely_Expression_Function return Boolean is
720 begin
721 -- If currently pointing to BEGIN or a declaration keyword
722 -- or a pragma, then we definitely have a subprogram body.
723 -- This is a common case, so worth testing first.
724
725 if Token = Tok_Begin
726 or else Token in Token_Class_Declk
727 or else Token = Tok_Pragma
728 then
729 return False;
730
731 -- Test for tokens which could only start an expression and
732 -- thus signal the case of a expression function.
733
734 elsif Token in Token_Class_Literal
735 or else Token in Token_Class_Unary_Addop
736 or else Token = Tok_Left_Paren
737 or else Token = Tok_Abs
738 or else Token = Tok_Null
739 or else Token = Tok_New
740 or else Token = Tok_Not
741 then
742 null;
743
744 -- Anything other than an identifier must be a body
745
746 elsif Token /= Tok_Identifier then
747 return False;
748
749 -- Here for an identifier
750
751 else
752 -- If the identifier is the first token on its line, then
753 -- let's assume that we have a missing begin and this is
754 -- intended as a subprogram body. However, if the context
755 -- is a function and the unit is a package declaration, a
756 -- body would be illegal, so try for an unparenthesized
757 -- expression function.
758
759 if Token_Is_At_Start_Of_Line then
760 declare
761 -- The enclosing scope entry is a subprogram spec
762
763 Spec_Node : constant Node_Id :=
764 Parent
765 (Scopes (Scope.Last).Labl);
766 Lib_Node : Node_Id := Spec_Node;
767
768 begin
769 -- Check whether there is an enclosing scope that
770 -- is a package declaration.
771
772 if Scope.Last > 1 then
773 Lib_Node :=
774 Parent (Scopes (Scope.Last - 1).Labl);
775 end if;
776
777 if Ada_Version >= Ada_2012
778 and then
779 Nkind (Lib_Node) = N_Package_Specification
780 and then
781 Nkind (Spec_Node) = N_Function_Specification
782 then
783 null;
784 else
785 return False;
786 end if;
787 end;
788
789 -- Otherwise we have to scan ahead. If the identifier is
790 -- followed by a colon or a comma, it is a declaration
791 -- and hence we have a subprogram body. Otherwise assume
792 -- a expression function.
793
794 else
795 declare
796 Scan_State : Saved_Scan_State;
797 Tok : Token_Type;
798
799 begin
800 Save_Scan_State (Scan_State);
801 Scan; -- past identifier
802 Tok := Token;
803 Restore_Scan_State (Scan_State);
804
805 if Tok = Tok_Colon or else Tok = Tok_Comma then
806 return False;
807 end if;
808 end;
809 end if;
810 end if;
811
812 -- Fall through if we have a likely expression function.
813 -- If the starting keyword is not "function" the error
814 -- will be reported elsewhere.
815
816 if Func then
817 Error_Msg_SC
818 ("expression function must be enclosed in parentheses");
819 end if;
820
821 return True;
822 end Likely_Expression_Function;
823
824 -- Start of processing for Scan_Body_Or_Expression_Function
825
826 begin
827 -- Expression_Function case
828
829 if Token = Tok_Left_Paren
830 or else Likely_Expression_Function
831 then
832 -- Check expression function allowed here
833
834 if not Pf_Flags.Pexp then
835 Error_Msg_SC ("expression function not allowed here!");
836 end if;
837
838 -- Check we are in Ada 2012 mode
839
840 Error_Msg_Ada_2012_Feature
841 ("!expression function", Token_Ptr);
842
843 -- Catch an illegal placement of the aspect specification
844 -- list:
845
846 -- function_specification
847 -- [aspect_specification] is (expression);
848
849 -- This case is correctly processed by the parser because
850 -- the expression function first appears as a subprogram
851 -- declaration to the parser. The starting keyword may
852 -- not have been "function" in which case the error is
853 -- on a malformed procedure.
854
855 if Is_Non_Empty_List (Aspects) then
856 if Func then
857 Error_Msg
858 ("aspect specifications must come after "
859 & "parenthesized expression",
860 Sloc (First (Aspects)));
861 else
862 Error_Msg
863 ("aspect specifications must come after subprogram "
864 & "specification", Sloc (First (Aspects)));
865 end if;
866 end if;
867
868 -- Parse out expression and build expression function
869
870 Body_Node :=
871 New_Node
872 (N_Expression_Function, Sloc (Specification_Node));
873 Set_Specification (Body_Node, Specification_Node);
874
875 declare
876 Expr : constant Node_Id := P_Expression;
877 begin
878 Set_Expression (Body_Node, Expr);
879
880 -- Check that the full expression is properly
881 -- parenthesized since we may have a left-operand that is
882 -- parenthesized but that is not one of the allowed cases
883 -- with syntactic parentheses.
884
885 if not (Paren_Count (Expr) /= 0
886 or else Nkind_In (Expr, N_Aggregate,
887 N_Extension_Aggregate,
888 N_Quantified_Expression))
889 then
890 Error_Msg
891 ("expression function must be enclosed in "
892 & "parentheses", Sloc (Expr));
893 end if;
894 end;
895
896 -- Expression functions can carry pre/postconditions
897
898 P_Aspect_Specifications (Body_Node);
899 Pop_Scope_Stack;
900
901 -- Subprogram body case
902
903 else
904 -- Check body allowed here
905
906 if not Pf_Flags.Pbod then
907 Error_Msg_SP ("subprogram body not allowed here!");
908 end if;
909
910 -- Here is the test for a suspicious IS (i.e. one that
911 -- looks like it might more properly be a semicolon).
912 -- See separate section describing use of IS instead
913 -- of semicolon in package Parse.
914
915 if (Token in Token_Class_Declk
916 or else
917 Token = Tok_Identifier)
918 and then Start_Column <= Scopes (Scope.Last).Ecol
919 and then Scope.Last /= 1
920 then
921 Scopes (Scope.Last).Etyp := E_Suspicious_Is;
922 Scopes (Scope.Last).S_Is := Prev_Token_Ptr;
923 end if;
924
925 -- Build and return subprogram body, parsing declarations
926 -- and statement sequence that belong to the body.
927
928 Body_Node :=
929 New_Node (N_Subprogram_Body, Sloc (Specification_Node));
930 Set_Specification (Body_Node, Specification_Node);
931
932 -- If aspects are present, the specification is parsed as
933 -- a subprogram declaration, and we jump here after seeing
934 -- the keyword IS. Attach asspects previously collected to
935 -- the body.
936
937 if Is_Non_Empty_List (Aspects) then
938 Set_Parent (Aspects, Body_Node);
939 Set_Aspect_Specifications (Body_Node, Aspects);
940 end if;
941
942 Parse_Decls_Begin_End (Body_Node);
943 end if;
944
945 return Body_Node;
946 end Scan_Body_Or_Expression_Function;
947 end if;
948
949 -- Processing for subprogram declaration
950
951 <<Subprogram_Declaration>>
952 Decl_Node :=
953 New_Node (N_Subprogram_Declaration, Sloc (Specification_Node));
954 Set_Specification (Decl_Node, Specification_Node);
955 Aspects := Get_Aspect_Specifications (Semicolon => False);
956
957 -- Aspects may be present on a subprogram body. The source parsed
958 -- so far is that of its specification. Go parse the body and attach
959 -- the collected aspects, if any, to the body.
960
961 if Token = Tok_Is then
962
963 -- If the subprogram is a procedure and already has a
964 -- specification, we can't define another.
965
966 if Nkind (Specification (Decl_Node)) = N_Procedure_Specification
967 and then Null_Present (Specification (Decl_Node))
968 then
969 Error_Msg_AP ("null procedure cannot have a body");
970 end if;
971
972 Scan;
973 goto Subprogram_Body;
974
975 else
976 if Is_Non_Empty_List (Aspects) then
977 Set_Parent (Aspects, Decl_Node);
978 Set_Aspect_Specifications (Decl_Node, Aspects);
979 end if;
980
981 TF_Semicolon;
982 end if;
983
984 -- If this is a context in which a subprogram body is permitted,
985 -- set active SIS entry in case (see section titled "Handling
986 -- Semicolon Used in Place of IS" in body of Parser package)
987 -- Note that SIS_Missing_Semicolon_Message is already set properly.
988
989 if Pf_Flags.Pbod
990
991 -- Disconnect this processing if we have scanned a null procedure
992 -- because in this case the spec is complete anyway with no body.
993
994 and then (Nkind (Specification_Node) /= N_Procedure_Specification
995 or else not Null_Present (Specification_Node))
996 then
997 SIS_Labl := Scopes (Scope.Last).Labl;
998 SIS_Sloc := Scopes (Scope.Last).Sloc;
999 SIS_Ecol := Scopes (Scope.Last).Ecol;
1000 SIS_Declaration_Node := Decl_Node;
1001 SIS_Semicolon_Sloc := Prev_Token_Ptr;
1002
1003 -- Do not activate the entry if we have "with Import"
1004
1005 if not SIS_Aspect_Import_Seen then
1006 SIS_Entry_Active := True;
1007 end if;
1008 end if;
1009
1010 Pop_Scope_Stack;
1011 return Decl_Node;
1012 end P_Subprogram;
1013
1014 ---------------------------------
1015 -- 6.1 Subprogram Declaration --
1016 ---------------------------------
1017
1018 -- Parsed by P_Subprogram (6.1)
1019
1020 ------------------------------------------
1021 -- 6.1 Abstract Subprogram Declaration --
1022 ------------------------------------------
1023
1024 -- Parsed by P_Subprogram (6.1)
1025
1026 -----------------------------------
1027 -- 6.1 Subprogram Specification --
1028 -----------------------------------
1029
1030 -- SUBPROGRAM_SPECIFICATION ::=
1031 -- procedure DEFINING_PROGRAM_UNIT_NAME PARAMETER_PROFILE
1032 -- | function DEFINING_DESIGNATOR PARAMETER_AND_RESULT_PROFILE
1033
1034 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1035
1036 -- PARAMETER_AND_RESULT_PROFILE ::= [FORMAL_PART] return SUBTYPE_MARK
1037
1038 -- Subprogram specifications that appear in subprogram declarations
1039 -- are parsed by P_Subprogram (6.1). This routine is used in other
1040 -- contexts where subprogram specifications occur.
1041
1042 -- Note: this routine does not affect the scope stack in any way
1043
1044 -- Error recovery: can raise Error_Resync
1045
1046 function P_Subprogram_Specification return Node_Id is
1047 Specification_Node : Node_Id;
1048 Result_Not_Null : Boolean;
1049 Result_Node : Node_Id;
1050
1051 begin
1052 if Token = Tok_Function then
1053 Specification_Node := New_Node (N_Function_Specification, Token_Ptr);
1054 Scan; -- past FUNCTION
1055 Ignore (Tok_Body);
1056 Set_Defining_Unit_Name (Specification_Node, P_Defining_Designator);
1057 Set_Parameter_Specifications
1058 (Specification_Node, P_Parameter_Profile);
1059 Check_Junk_Semicolon_Before_Return;
1060 TF_Return;
1061
1062 Result_Not_Null := P_Null_Exclusion; -- Ada 2005 (AI-231)
1063
1064 -- Ada 2005 (AI-318-02)
1065
1066 if Token = Tok_Access then
1067 if Ada_Version < Ada_2005 then
1068 Error_Msg_SC
1069 ("anonymous access result type is an Ada 2005 extension");
1070 Error_Msg_SC ("\unit must be compiled with -gnat05 switch");
1071 end if;
1072
1073 Result_Node := P_Access_Definition (Result_Not_Null);
1074
1075 else
1076 Result_Node := P_Subtype_Mark;
1077 No_Constraint_Maybe_Expr_Func;
1078 end if;
1079
1080 Set_Null_Exclusion_Present (Specification_Node, Result_Not_Null);
1081 Set_Result_Definition (Specification_Node, Result_Node);
1082 return Specification_Node;
1083
1084 elsif Token = Tok_Procedure then
1085 Specification_Node := New_Node (N_Procedure_Specification, Token_Ptr);
1086 Scan; -- past PROCEDURE
1087 Ignore (Tok_Body);
1088 Set_Defining_Unit_Name
1089 (Specification_Node, P_Defining_Program_Unit_Name);
1090 Set_Parameter_Specifications
1091 (Specification_Node, P_Parameter_Profile);
1092 return Specification_Node;
1093
1094 else
1095 Error_Msg_SC ("subprogram specification expected");
1096 raise Error_Resync;
1097 end if;
1098 end P_Subprogram_Specification;
1099
1100 ---------------------
1101 -- 6.1 Designator --
1102 ---------------------
1103
1104 -- DESIGNATOR ::=
1105 -- [PARENT_UNIT_NAME .] IDENTIFIER | OPERATOR_SYMBOL
1106
1107 -- The caller has checked that the initial token is an identifier,
1108 -- operator symbol, or string literal. Note that we don't bother to
1109 -- do much error diagnosis in this routine, since it is only used for
1110 -- the label on END lines, and the routines in package Par.Endh will
1111 -- check that the label is appropriate.
1112
1113 -- Error recovery: cannot raise Error_Resync
1114
1115 function P_Designator return Node_Id is
1116 Ident_Node : Node_Id;
1117 Name_Node : Node_Id;
1118 Prefix_Node : Node_Id;
1119
1120 function Real_Dot return Boolean;
1121 -- Tests if a current token is an interesting period, i.e. is followed
1122 -- by an identifier or operator symbol or string literal. If not, it is
1123 -- probably just incorrect punctuation to be caught by our caller. Note
1124 -- that the case of an operator symbol or string literal is also an
1125 -- error, but that is an error that we catch here. If the result is
1126 -- True, a real dot has been scanned and we are positioned past it,
1127 -- if the result is False, the scan position is unchanged.
1128
1129 --------------
1130 -- Real_Dot --
1131 --------------
1132
1133 function Real_Dot return Boolean is
1134 Scan_State : Saved_Scan_State;
1135
1136 begin
1137 if Token /= Tok_Dot then
1138 return False;
1139
1140 else
1141 Save_Scan_State (Scan_State);
1142 Scan; -- past dot
1143
1144 if Token = Tok_Identifier
1145 or else Token = Tok_Operator_Symbol
1146 or else Token = Tok_String_Literal
1147 then
1148 return True;
1149
1150 else
1151 Restore_Scan_State (Scan_State);
1152 return False;
1153 end if;
1154 end if;
1155 end Real_Dot;
1156
1157 -- Start of processing for P_Designator
1158
1159 begin
1160 Ident_Node := Token_Node;
1161 Scan; -- past initial token
1162
1163 if Prev_Token = Tok_Operator_Symbol
1164 or else Prev_Token = Tok_String_Literal
1165 or else not Real_Dot
1166 then
1167 return Ident_Node;
1168
1169 -- Child name case
1170
1171 else
1172 Prefix_Node := Ident_Node;
1173
1174 -- Loop through child names, on entry to this loop, Prefix contains
1175 -- the name scanned so far, and Ident_Node is the last identifier.
1176
1177 loop
1178 Name_Node := New_Node (N_Selected_Component, Prev_Token_Ptr);
1179 Set_Prefix (Name_Node, Prefix_Node);
1180 Ident_Node := P_Identifier;
1181 Set_Selector_Name (Name_Node, Ident_Node);
1182 Prefix_Node := Name_Node;
1183 exit when not Real_Dot;
1184 end loop;
1185
1186 -- On exit from the loop, Ident_Node is the last identifier scanned,
1187 -- i.e. the defining identifier, and Prefix_Node is a node for the
1188 -- entire name, structured (incorrectly) as a selected component.
1189
1190 Name_Node := Prefix (Prefix_Node);
1191 Change_Node (Prefix_Node, N_Designator);
1192 Set_Name (Prefix_Node, Name_Node);
1193 Set_Identifier (Prefix_Node, Ident_Node);
1194 return Prefix_Node;
1195 end if;
1196
1197 exception
1198 when Error_Resync =>
1199 while Token = Tok_Dot or else Token = Tok_Identifier loop
1200 Scan;
1201 end loop;
1202
1203 return Error;
1204 end P_Designator;
1205
1206 ------------------------------
1207 -- 6.1 Defining Designator --
1208 ------------------------------
1209
1210 -- DEFINING_DESIGNATOR ::=
1211 -- DEFINING_PROGRAM_UNIT_NAME | DEFINING_OPERATOR_SYMBOL
1212
1213 -- Error recovery: cannot raise Error_Resync
1214
1215 function P_Defining_Designator return Node_Id is
1216 begin
1217 if Token = Tok_Operator_Symbol then
1218 return P_Defining_Operator_Symbol;
1219
1220 elsif Token = Tok_String_Literal then
1221 Error_Msg_SC ("invalid operator name");
1222 Scan; -- past junk string
1223 return Error;
1224
1225 else
1226 return P_Defining_Program_Unit_Name;
1227 end if;
1228 end P_Defining_Designator;
1229
1230 -------------------------------------
1231 -- 6.1 Defining Program Unit Name --
1232 -------------------------------------
1233
1234 -- DEFINING_PROGRAM_UNIT_NAME ::=
1235 -- [PARENT_UNIT_NAME .] DEFINING_IDENTIFIER
1236
1237 -- Note: PARENT_UNIT_NAME may be present only in 95 mode at the outer level
1238
1239 -- Error recovery: cannot raise Error_Resync
1240
1241 function P_Defining_Program_Unit_Name return Node_Id is
1242 Ident_Node : Node_Id;
1243 Name_Node : Node_Id;
1244 Prefix_Node : Node_Id;
1245
1246 begin
1247 -- Set identifier casing if not already set and scan initial identifier
1248
1249 if Token = Tok_Identifier
1250 and then Identifier_Casing (Current_Source_File) = Unknown
1251 then
1252 Set_Identifier_Casing (Current_Source_File, Determine_Token_Casing);
1253 end if;
1254
1255 Ident_Node := P_Identifier (C_Dot);
1256 Merge_Identifier (Ident_Node, Tok_Return);
1257
1258 -- Normal case (not child library unit name)
1259
1260 if Token /= Tok_Dot then
1261 Change_Identifier_To_Defining_Identifier (Ident_Node);
1262 Warn_If_Standard_Redefinition (Ident_Node);
1263 return Ident_Node;
1264
1265 -- Child library unit name case
1266
1267 else
1268 if Scope.Last > 1 then
1269 Error_Msg_SP ("child unit allowed only at library level");
1270 raise Error_Resync;
1271
1272 elsif Ada_Version = Ada_83 then
1273 Error_Msg_SP ("(Ada 83) child unit not allowed!");
1274
1275 end if;
1276
1277 Prefix_Node := Ident_Node;
1278
1279 -- Loop through child names, on entry to this loop, Prefix contains
1280 -- the name scanned so far, and Ident_Node is the last identifier.
1281
1282 loop
1283 exit when Token /= Tok_Dot;
1284 Name_Node := New_Node (N_Selected_Component, Token_Ptr);
1285 Scan; -- past period
1286 Set_Prefix (Name_Node, Prefix_Node);
1287 Ident_Node := P_Identifier (C_Dot);
1288 Set_Selector_Name (Name_Node, Ident_Node);
1289 Prefix_Node := Name_Node;
1290 end loop;
1291
1292 -- On exit from the loop, Ident_Node is the last identifier scanned,
1293 -- i.e. the defining identifier, and Prefix_Node is a node for the
1294 -- entire name, structured (incorrectly) as a selected component.
1295
1296 Name_Node := Prefix (Prefix_Node);
1297 Change_Node (Prefix_Node, N_Defining_Program_Unit_Name);
1298 Set_Name (Prefix_Node, Name_Node);
1299 Change_Identifier_To_Defining_Identifier (Ident_Node);
1300 Warn_If_Standard_Redefinition (Ident_Node);
1301 Set_Defining_Identifier (Prefix_Node, Ident_Node);
1302
1303 -- All set with unit name parsed
1304
1305 return Prefix_Node;
1306 end if;
1307
1308 exception
1309 when Error_Resync =>
1310 while Token = Tok_Dot or else Token = Tok_Identifier loop
1311 Scan;
1312 end loop;
1313
1314 return Error;
1315 end P_Defining_Program_Unit_Name;
1316
1317 --------------------------
1318 -- 6.1 Operator Symbol --
1319 --------------------------
1320
1321 -- OPERATOR_SYMBOL ::= STRING_LITERAL
1322
1323 -- Operator symbol is returned by the scanner as Tok_Operator_Symbol
1324
1325 -----------------------------------
1326 -- 6.1 Defining Operator Symbol --
1327 -----------------------------------
1328
1329 -- DEFINING_OPERATOR_SYMBOL ::= OPERATOR_SYMBOL
1330
1331 -- The caller has checked that the initial symbol is an operator symbol
1332
1333 function P_Defining_Operator_Symbol return Node_Id is
1334 Op_Node : Node_Id;
1335
1336 begin
1337 Op_Node := Token_Node;
1338 Change_Operator_Symbol_To_Defining_Operator_Symbol (Op_Node);
1339 Scan; -- past operator symbol
1340 return Op_Node;
1341 end P_Defining_Operator_Symbol;
1342
1343 ----------------------------
1344 -- 6.1 Parameter_Profile --
1345 ----------------------------
1346
1347 -- PARAMETER_PROFILE ::= [FORMAL_PART]
1348
1349 -- Empty is returned if no formal part is present
1350
1351 -- Error recovery: cannot raise Error_Resync
1352
1353 function P_Parameter_Profile return List_Id is
1354 begin
1355 if Token = Tok_Left_Paren then
1356 Scan; -- part left paren
1357 return P_Formal_Part;
1358 else
1359 return No_List;
1360 end if;
1361 end P_Parameter_Profile;
1362
1363 ---------------------------------------
1364 -- 6.1 Parameter And Result Profile --
1365 ---------------------------------------
1366
1367 -- Parsed by its parent construct, which uses P_Parameter_Profile to
1368 -- parse the parameters, and P_Subtype_Mark to parse the return type.
1369
1370 ----------------------
1371 -- 6.1 Formal part --
1372 ----------------------
1373
1374 -- FORMAL_PART ::= (PARAMETER_SPECIFICATION {; PARAMETER_SPECIFICATION})
1375
1376 -- PARAMETER_SPECIFICATION ::=
1377 -- DEFINING_IDENTIFIER_LIST : [ALIASED] MODE [NULL_EXCLUSION]
1378 -- SUBTYPE_MARK [:= DEFAULT_EXPRESSION]
1379 -- | DEFINING_IDENTIFIER_LIST : ACCESS_DEFINITION
1380 -- [:= DEFAULT_EXPRESSION]
1381
1382 -- This scans the construct Formal_Part. The caller has already checked
1383 -- that the initial token is a left parenthesis, and skipped past it, so
1384 -- that on entry Token is the first token following the left parenthesis.
1385
1386 -- Note: The ALIASED keyword is allowed only in Ada 2012 mode (AI 142)
1387
1388 -- Error recovery: cannot raise Error_Resync
1389
1390 function P_Formal_Part return List_Id is
1391 Specification_List : List_Id;
1392 Specification_Node : Node_Id;
1393 Scan_State : Saved_Scan_State;
1394 Num_Idents : Nat;
1395 Ident : Nat;
1396 Ident_Sloc : Source_Ptr;
1397 Not_Null_Present : Boolean := False;
1398 Not_Null_Sloc : Source_Ptr;
1399
1400 Idents : array (Int range 1 .. 4096) of Entity_Id;
1401 -- This array holds the list of defining identifiers. The upper bound
1402 -- of 4096 is intended to be essentially infinite, and we do not even
1403 -- bother to check for it being exceeded.
1404
1405 begin
1406 Specification_List := New_List;
1407 Specification_Loop : loop
1408 begin
1409 if Token = Tok_Pragma then
1410 Error_Msg_SC ("pragma not allowed in formal part");
1411 Discard_Junk_Node (P_Pragma (Skipping => True));
1412 end if;
1413
1414 Ignore (Tok_Left_Paren);
1415 Ident_Sloc := Token_Ptr;
1416 Idents (1) := P_Defining_Identifier (C_Comma_Colon);
1417 Num_Idents := 1;
1418
1419 Ident_Loop : loop
1420 exit Ident_Loop when Token = Tok_Colon;
1421
1422 -- The only valid tokens are colon and comma, so if we have
1423 -- neither do a bit of investigation to see which is the
1424 -- better choice for insertion.
1425
1426 if Token /= Tok_Comma then
1427
1428 -- Assume colon if ALIASED, IN or OUT keyword found
1429
1430 exit Ident_Loop when Token = Tok_Aliased or else
1431 Token = Tok_In or else
1432 Token = Tok_Out;
1433
1434 -- Otherwise scan ahead
1435
1436 Save_Scan_State (Scan_State);
1437 Look_Ahead : loop
1438
1439 -- If we run into a semicolon, then assume that a
1440 -- colon was missing, e.g. Parms (X Y; ...). Also
1441 -- assume missing colon on EOF (a real disaster)
1442 -- and on a right paren, e.g. Parms (X Y), and also
1443 -- on an assignment symbol, e.g. Parms (X Y := ..)
1444
1445 if Token = Tok_Semicolon
1446 or else Token = Tok_Right_Paren
1447 or else Token = Tok_EOF
1448 or else Token = Tok_Colon_Equal
1449 then
1450 Restore_Scan_State (Scan_State);
1451 exit Ident_Loop;
1452
1453 -- If we run into a colon, assume that we had a missing
1454 -- comma, e.g. Parms (A B : ...). Also assume a missing
1455 -- comma if we hit another comma, e.g. Parms (A B, C ..)
1456
1457 elsif Token = Tok_Colon
1458 or else Token = Tok_Comma
1459 then
1460 Restore_Scan_State (Scan_State);
1461 exit Look_Ahead;
1462 end if;
1463
1464 Scan;
1465 end loop Look_Ahead;
1466 end if;
1467
1468 -- Here if a comma is present, or to be assumed
1469
1470 T_Comma;
1471 Num_Idents := Num_Idents + 1;
1472 Idents (Num_Idents) := P_Defining_Identifier (C_Comma_Colon);
1473 end loop Ident_Loop;
1474
1475 -- Fall through the loop on encountering a colon, or deciding
1476 -- that there is a missing colon.
1477
1478 T_Colon;
1479
1480 -- If there are multiple identifiers, we repeatedly scan the
1481 -- type and initialization expression information by resetting
1482 -- the scan pointer (so that we get completely separate trees
1483 -- for each occurrence).
1484
1485 if Num_Idents > 1 then
1486 Save_Scan_State (Scan_State);
1487 end if;
1488
1489 -- Loop through defining identifiers in list
1490
1491 Ident := 1;
1492
1493 Ident_List_Loop : loop
1494 Specification_Node :=
1495 New_Node (N_Parameter_Specification, Ident_Sloc);
1496 Set_Defining_Identifier (Specification_Node, Idents (Ident));
1497
1498 -- Scan possible ALIASED for Ada 2012 (AI-142)
1499
1500 if Token = Tok_Aliased then
1501 if Ada_Version < Ada_2012 then
1502 Error_Msg_Ada_2012_Feature
1503 ("ALIASED parameter", Token_Ptr);
1504 else
1505 Set_Aliased_Present (Specification_Node);
1506 end if;
1507
1508 Scan; -- past ALIASED
1509 end if;
1510
1511 -- Scan possible NOT NULL for Ada 2005 (AI-231, AI-447)
1512
1513 Not_Null_Sloc := Token_Ptr;
1514 Not_Null_Present :=
1515 P_Null_Exclusion (Allow_Anonymous_In_95 => True);
1516
1517 -- Case of ACCESS keyword present
1518
1519 if Token = Tok_Access then
1520 Set_Null_Exclusion_Present
1521 (Specification_Node, Not_Null_Present);
1522
1523 if Ada_Version = Ada_83 then
1524 Error_Msg_SC ("(Ada 83) access parameters not allowed");
1525 end if;
1526
1527 Set_Parameter_Type
1528 (Specification_Node,
1529 P_Access_Definition (Not_Null_Present));
1530
1531 -- Case of IN or OUT present
1532
1533 else
1534 if Token = Tok_In or else Token = Tok_Out then
1535 if Not_Null_Present then
1536 Error_Msg
1537 ("`NOT NULL` can only be used with `ACCESS`",
1538 Not_Null_Sloc);
1539
1540 if Token = Tok_In then
1541 Error_Msg
1542 ("\`IN` not allowed together with `ACCESS`",
1543 Not_Null_Sloc);
1544 else
1545 Error_Msg
1546 ("\`OUT` not allowed together with `ACCESS`",
1547 Not_Null_Sloc);
1548 end if;
1549 end if;
1550
1551 P_Mode (Specification_Node);
1552 Not_Null_Present := P_Null_Exclusion; -- Ada 2005 (AI-231)
1553 end if;
1554
1555 Set_Null_Exclusion_Present
1556 (Specification_Node, Not_Null_Present);
1557
1558 if Token = Tok_Procedure
1559 or else
1560 Token = Tok_Function
1561 then
1562 Error_Msg_SC ("formal subprogram parameter not allowed");
1563 Scan;
1564
1565 if Token = Tok_Left_Paren then
1566 Discard_Junk_List (P_Formal_Part);
1567 end if;
1568
1569 if Token = Tok_Return then
1570 Scan;
1571 Discard_Junk_Node (P_Subtype_Mark);
1572 end if;
1573
1574 Set_Parameter_Type (Specification_Node, Error);
1575
1576 else
1577 Set_Parameter_Type (Specification_Node, P_Subtype_Mark);
1578 No_Constraint;
1579 end if;
1580 end if;
1581
1582 Set_Expression (Specification_Node, Init_Expr_Opt (True));
1583
1584 if Ident > 1 then
1585 Set_Prev_Ids (Specification_Node, True);
1586 end if;
1587
1588 if Ident < Num_Idents then
1589 Set_More_Ids (Specification_Node, True);
1590 end if;
1591
1592 Append (Specification_Node, Specification_List);
1593 exit Ident_List_Loop when Ident = Num_Idents;
1594 Ident := Ident + 1;
1595 Restore_Scan_State (Scan_State);
1596 end loop Ident_List_Loop;
1597
1598 exception
1599 when Error_Resync =>
1600 Resync_Semicolon_List;
1601 end;
1602
1603 if Token = Tok_Semicolon then
1604 Save_Scan_State (Scan_State);
1605 Scan; -- past semicolon
1606
1607 -- If we have RETURN or IS after the semicolon, then assume
1608 -- that semicolon should have been a right parenthesis and exit
1609
1610 if Token = Tok_Is or else Token = Tok_Return then
1611 Error_Msg_SP -- CODEFIX
1612 ("|"";"" should be "")""");
1613 exit Specification_Loop;
1614 end if;
1615
1616 -- If we have a declaration keyword after the semicolon, then
1617 -- assume we had a missing right parenthesis and terminate list
1618
1619 if Token in Token_Class_Declk then
1620 Error_Msg_AP -- CODEFIX
1621 ("missing "")""");
1622 Restore_Scan_State (Scan_State);
1623 exit Specification_Loop;
1624 end if;
1625
1626 elsif Token = Tok_Right_Paren then
1627 Scan; -- past right paren
1628 exit Specification_Loop;
1629
1630 -- Special check for common error of using comma instead of semicolon
1631
1632 elsif Token = Tok_Comma then
1633 T_Semicolon;
1634 Scan; -- past comma
1635
1636 -- Special check for omitted separator
1637
1638 elsif Token = Tok_Identifier then
1639 T_Semicolon;
1640
1641 -- If nothing sensible, skip to next semicolon or right paren
1642
1643 else
1644 T_Semicolon;
1645 Resync_Semicolon_List;
1646
1647 if Token = Tok_Semicolon then
1648 Scan; -- past semicolon
1649 else
1650 T_Right_Paren;
1651 exit Specification_Loop;
1652 end if;
1653 end if;
1654 end loop Specification_Loop;
1655
1656 return Specification_List;
1657 end P_Formal_Part;
1658
1659 ----------------------------------
1660 -- 6.1 Parameter Specification --
1661 ----------------------------------
1662
1663 -- Parsed by P_Formal_Part (6.1)
1664
1665 ---------------
1666 -- 6.1 Mode --
1667 ---------------
1668
1669 -- MODE ::= [in] | in out | out
1670
1671 -- There is no explicit node in the tree for the Mode. Instead the
1672 -- In_Present and Out_Present flags are set in the parent node to
1673 -- record the presence of keywords specifying the mode.
1674
1675 -- Error_Recovery: cannot raise Error_Resync
1676
1677 procedure P_Mode (Node : Node_Id) is
1678 begin
1679 if Token = Tok_In then
1680 Scan; -- past IN
1681 Set_In_Present (Node, True);
1682
1683 if Style.Mode_In_Check and then Token /= Tok_Out then
1684 Error_Msg_SP -- CODEFIX
1685 ("(style) IN should be omitted");
1686 end if;
1687
1688 -- Since Ada 2005, formal objects can have an anonymous access type,
1689 -- and of course carry a mode indicator.
1690
1691 if Token = Tok_Access
1692 and then Nkind (Node) /= N_Formal_Object_Declaration
1693 then
1694 Error_Msg_SP ("IN not allowed together with ACCESS");
1695 Scan; -- past ACCESS
1696 end if;
1697 end if;
1698
1699 if Token = Tok_Out then
1700 Scan; -- past OUT
1701 Set_Out_Present (Node, True);
1702 end if;
1703
1704 if Token = Tok_In then
1705 Error_Msg_SC ("IN must precede OUT in parameter mode");
1706 Scan; -- past IN
1707 Set_In_Present (Node, True);
1708 end if;
1709 end P_Mode;
1710
1711 --------------------------
1712 -- 6.3 Subprogram Body --
1713 --------------------------
1714
1715 -- Parsed by P_Subprogram (6.1)
1716
1717 -----------------------------------
1718 -- 6.4 Procedure Call Statement --
1719 -----------------------------------
1720
1721 -- Parsed by P_Sequence_Of_Statements (5.1)
1722
1723 ------------------------
1724 -- 6.4 Function Call --
1725 ------------------------
1726
1727 -- Parsed by P_Name (4.1)
1728
1729 --------------------------------
1730 -- 6.4 Actual Parameter Part --
1731 --------------------------------
1732
1733 -- Parsed by P_Name (4.1)
1734
1735 --------------------------------
1736 -- 6.4 Parameter Association --
1737 --------------------------------
1738
1739 -- Parsed by P_Name (4.1)
1740
1741 ------------------------------------
1742 -- 6.4 Explicit Actual Parameter --
1743 ------------------------------------
1744
1745 -- Parsed by P_Name (4.1)
1746
1747 ---------------------------
1748 -- 6.5 Return Statement --
1749 ---------------------------
1750
1751 -- SIMPLE_RETURN_STATEMENT ::= return [EXPRESSION];
1752 --
1753 -- EXTENDED_RETURN_STATEMENT ::=
1754 -- return DEFINING_IDENTIFIER : [aliased] RETURN_SUBTYPE_INDICATION
1755 -- [:= EXPRESSION] [do
1756 -- HANDLED_SEQUENCE_OF_STATEMENTS
1757 -- end return];
1758 --
1759 -- RETURN_SUBTYPE_INDICATION ::= SUBTYPE_INDICATION | ACCESS_DEFINITION
1760
1761 -- RETURN_STATEMENT ::= return [EXPRESSION];
1762
1763 -- Error recovery: can raise Error_Resync
1764
1765 procedure P_Return_Subtype_Indication (Decl_Node : Node_Id) is
1766
1767 -- Note: We don't need to check Ada_Version here, because this is
1768 -- only called in >= Ada 2005 cases anyway.
1769
1770 Not_Null_Present : constant Boolean := P_Null_Exclusion;
1771
1772 begin
1773 Set_Null_Exclusion_Present (Decl_Node, Not_Null_Present);
1774
1775 if Token = Tok_Access then
1776 Set_Object_Definition
1777 (Decl_Node, P_Access_Definition (Not_Null_Present));
1778 else
1779 Set_Object_Definition
1780 (Decl_Node, P_Subtype_Indication (Not_Null_Present));
1781 end if;
1782 end P_Return_Subtype_Indication;
1783
1784 -- Error recovery: can raise Error_Resync
1785
1786 function P_Return_Object_Declaration return Node_Id is
1787 Return_Obj : Node_Id;
1788 Decl_Node : Node_Id;
1789
1790 begin
1791 Return_Obj := Token_Node;
1792 Change_Identifier_To_Defining_Identifier (Return_Obj);
1793 Warn_If_Standard_Redefinition (Return_Obj);
1794 Decl_Node := New_Node (N_Object_Declaration, Token_Ptr);
1795 Set_Defining_Identifier (Decl_Node, Return_Obj);
1796
1797 Scan; -- past identifier
1798 Scan; -- past :
1799
1800 -- First an error check, if we have two identifiers in a row, a likely
1801 -- possibility is that the first of the identifiers is an incorrectly
1802 -- spelled keyword. See similar check in P_Identifier_Declarations.
1803
1804 if Token = Tok_Identifier then
1805 declare
1806 SS : Saved_Scan_State;
1807 I2 : Boolean;
1808
1809 begin
1810 Save_Scan_State (SS);
1811 Scan; -- past initial identifier
1812 I2 := (Token = Tok_Identifier);
1813 Restore_Scan_State (SS);
1814
1815 if I2
1816 and then
1817 (Bad_Spelling_Of (Tok_Access) or else
1818 Bad_Spelling_Of (Tok_Aliased) or else
1819 Bad_Spelling_Of (Tok_Constant))
1820 then
1821 null;
1822 end if;
1823 end;
1824 end if;
1825
1826 -- We allow "constant" here (as in "return Result : constant
1827 -- T..."). This is not in the latest RM, but the ARG is considering an
1828 -- AI on the subject (see AI05-0015-1), which we expect to be approved.
1829
1830 if Token = Tok_Constant then
1831 Scan; -- past CONSTANT
1832 Set_Constant_Present (Decl_Node);
1833
1834 if Token = Tok_Aliased then
1835 Error_Msg_SC -- CODEFIX
1836 ("ALIASED should be before CONSTANT");
1837 Scan; -- past ALIASED
1838 Set_Aliased_Present (Decl_Node);
1839 end if;
1840
1841 elsif Token = Tok_Aliased then
1842 Scan; -- past ALIASED
1843 Set_Aliased_Present (Decl_Node);
1844
1845 -- The restrictions on the use of aliased in an extended return
1846 -- are semantic, not syntactic.
1847
1848 if Token = Tok_Constant then
1849 Scan; -- past CONSTANT
1850 Set_Constant_Present (Decl_Node);
1851 end if;
1852 end if;
1853
1854 P_Return_Subtype_Indication (Decl_Node);
1855
1856 if Token = Tok_Colon_Equal then
1857 Scan; -- past :=
1858 Set_Expression (Decl_Node, P_Expression_No_Right_Paren);
1859 end if;
1860
1861 return Decl_Node;
1862 end P_Return_Object_Declaration;
1863
1864 -- Error recovery: can raise Error_Resync
1865
1866 function P_Return_Statement return Node_Id is
1867 -- The caller has checked that the initial token is RETURN
1868
1869 function Is_Simple return Boolean;
1870 -- Scan state is just after RETURN (and is left that way). Determine
1871 -- whether this is a simple or extended return statement by looking
1872 -- ahead for "identifier :", which implies extended.
1873
1874 ---------------
1875 -- Is_Simple --
1876 ---------------
1877
1878 function Is_Simple return Boolean is
1879 Scan_State : Saved_Scan_State;
1880 Result : Boolean := True;
1881
1882 begin
1883 if Token = Tok_Identifier then
1884 Save_Scan_State (Scan_State); -- at identifier
1885 Scan; -- past identifier
1886
1887 if Token = Tok_Colon then
1888 Result := False; -- It's an extended_return_statement.
1889 end if;
1890
1891 Restore_Scan_State (Scan_State); -- to identifier
1892 end if;
1893
1894 return Result;
1895 end Is_Simple;
1896
1897 Ret_Sloc : constant Source_Ptr := Token_Ptr;
1898 Ret_Strt : constant Column_Number := Start_Column;
1899 Ret_Node : Node_Id;
1900
1901 -- Start of processing for P_Return_Statement
1902
1903 begin
1904 Scan; -- past RETURN
1905
1906 -- Simple_return_statement, no expression, return an
1907 -- N_Simple_Return_Statement node with the expression field left Empty.
1908
1909 if Token = Tok_Semicolon then
1910 Scan; -- past ;
1911 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1912
1913 -- Nontrivial case
1914
1915 else
1916 -- Simple_return_statement with expression
1917
1918 -- We avoid trying to scan an expression if we are at an
1919 -- expression terminator since in that case the best error
1920 -- message is probably that we have a missing semicolon.
1921
1922 if Is_Simple then
1923 Ret_Node := New_Node (N_Simple_Return_Statement, Ret_Sloc);
1924
1925 if Token not in Token_Class_Eterm then
1926 Set_Expression (Ret_Node, P_Expression_No_Right_Paren);
1927 end if;
1928
1929 -- Extended_return_statement (Ada 2005 only -- AI-318):
1930
1931 else
1932 if Ada_Version < Ada_2005 then
1933 Error_Msg_SP
1934 (" extended_return_statement is an Ada 2005 extension");
1935 Error_Msg_SP ("\unit must be compiled with -gnat05 switch");
1936 end if;
1937
1938 Ret_Node := New_Node (N_Extended_Return_Statement, Ret_Sloc);
1939 Set_Return_Object_Declarations
1940 (Ret_Node, New_List (P_Return_Object_Declaration));
1941
1942 if Token = Tok_Do then
1943 Push_Scope_Stack;
1944 Scopes (Scope.Last).Ecol := Ret_Strt;
1945 Scopes (Scope.Last).Etyp := E_Return;
1946 Scopes (Scope.Last).Labl := Error;
1947 Scopes (Scope.Last).Sloc := Ret_Sloc;
1948
1949 Scan; -- past DO
1950 Set_Handled_Statement_Sequence
1951 (Ret_Node, P_Handled_Sequence_Of_Statements);
1952 End_Statements;
1953
1954 -- Do we need to handle Error_Resync here???
1955 end if;
1956 end if;
1957
1958 TF_Semicolon;
1959 end if;
1960
1961 return Ret_Node;
1962 end P_Return_Statement;
1963
1964 end Ch6;