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