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