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