Debian Codegraph @

gcc/ada/sem.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 with Atree; use Atree;
27 with Debug; use Debug;
28 with Debug_A; use Debug_A;
29 with Einfo; use Einfo;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Exp_SPARK; use Exp_SPARK;
33 with Expander; use Expander;
34 with Ghost; use Ghost;
35 with Lib; use Lib;
36 with Lib.Load; use Lib.Load;
37 with Nlists; use Nlists;
38 with Output; use Output;
39 with Restrict; use Restrict;
40 with Sem_Attr; use Sem_Attr;
41 with Sem_Ch2; use Sem_Ch2;
42 with Sem_Ch3; use Sem_Ch3;
43 with Sem_Ch4; use Sem_Ch4;
44 with Sem_Ch5; use Sem_Ch5;
45 with Sem_Ch6; use Sem_Ch6;
46 with Sem_Ch7; use Sem_Ch7;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Ch9; use Sem_Ch9;
49 with Sem_Ch10; use Sem_Ch10;
50 with Sem_Ch11; use Sem_Ch11;
51 with Sem_Ch12; use Sem_Ch12;
52 with Sem_Ch13; use Sem_Ch13;
53 with Sem_Prag; use Sem_Prag;
54 with Sem_Util; use Sem_Util;
55 with Sinfo; use Sinfo;
56 with Sinfo.Nodes; use Sinfo.Nodes;
57 with Sinfo.Utils; use Sinfo.Utils;
58 with Stand; use Stand;
59 with Stylesw; use Stylesw;
60 with Uintp; use Uintp;
61 with Uname; use Uname;
62
63 with Ada.Unchecked_Deallocation;
64
65 pragma Warnings (Off, Sem_Util);
66 -- Suppress warnings of unused with for Sem_Util (used only in asserts)
67
68 package body Sem is
69
70 Debug_Unit_Walk : Boolean renames Debug_Flag_Dot_WW;
71 -- Controls debugging printouts for Walk_Library_Items
72
73 Outer_Generic_Scope : Entity_Id := Empty;
74 -- Global reference to the outer scope that is generic. In a non-generic
75 -- context, it is empty. At the moment, it is only used for avoiding
76 -- freezing of external references in generics.
77
78 Comp_Unit_List : Elist_Id := No_Elist;
79 -- Used by Walk_Library_Items. This is a list of N_Compilation_Unit nodes
80 -- processed by Semantics, in an appropriate order. Initialized to
81 -- No_Elist, because it's too early to call New_Elmt_List; we will set it
82 -- to New_Elmt_List on first use.
83
84 generic
85 with procedure Action (Withed_Unit : Node_Id);
86 procedure Walk_Withs_Immediate (CU : Node_Id; Include_Limited : Boolean);
87 -- Walk all the with clauses of CU, and call Action for the with'ed unit.
88 -- Ignore limited withs, unless Include_Limited is True. CU must be an
89 -- N_Compilation_Unit.
90
91 generic
92 with procedure Action (Withed_Unit : Node_Id);
93 procedure Walk_Withs (CU : Node_Id; Include_Limited : Boolean);
94 -- Same as Walk_Withs_Immediate, but also include with clauses on subunits
95 -- of this unit, since they count as dependences on their parent library
96 -- item. CU must be an N_Compilation_Unit whose Unit is not an N_Subunit.
97
98 -------------
99 -- Analyze --
100 -------------
101
102 -- WARNING: This routine manages Ghost regions. Return statements must be
103 -- replaced by gotos which jump to the end of the routine and restore the
104 -- Ghost mode.
105
106 procedure Analyze (N : Node_Id) is
107 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
108 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
109 -- Save the Ghost-related attributes to restore on exit
110
111 begin
112 Debug_A_Entry ("analyzing ", N);
113
114 -- Immediate return if already analyzed
115
116 if Analyzed (N) then
117 Debug_A_Exit ("analyzing ", N, " (done, analyzed already)");
118 return;
119 end if;
120
121 -- A declaration may be subject to pragma Ghost. Set the mode now to
122 -- ensure that any nodes generated during analysis and expansion are
123 -- marked as Ghost.
124
125 if Is_Declaration (N) then
126 Mark_And_Set_Ghost_Declaration (N);
127 end if;
128
129 -- Otherwise processing depends on the node kind
130
131 case Nkind (N) is
132 when N_Abort_Statement =>
133 Analyze_Abort_Statement (N);
134
135 when N_Abstract_Subprogram_Declaration =>
136 Analyze_Abstract_Subprogram_Declaration (N);
137
138 when N_Accept_Alternative =>
139 Analyze_Accept_Alternative (N);
140
141 when N_Accept_Statement =>
142 Analyze_Accept_Statement (N);
143
144 when N_Aggregate =>
145 Analyze_Aggregate (N);
146
147 when N_Allocator =>
148 Analyze_Allocator (N);
149
150 when N_And_Then =>
151 Analyze_Short_Circuit (N);
152
153 when N_Assignment_Statement =>
154 Analyze_Assignment (N);
155
156 when N_Asynchronous_Select =>
157 Analyze_Asynchronous_Select (N);
158
159 when N_At_Clause =>
160 Analyze_At_Clause (N);
161
162 when N_Attribute_Reference =>
163 Analyze_Attribute (N);
164
165 when N_Attribute_Definition_Clause =>
166 Analyze_Attribute_Definition_Clause (N);
167
168 when N_Block_Statement =>
169 Analyze_Block_Statement (N);
170
171 when N_Case_Expression =>
172 Analyze_Case_Expression (N);
173
174 when N_Case_Statement =>
175 Analyze_Case_Statement (N);
176
177 when N_Character_Literal =>
178 Analyze_Character_Literal (N);
179
180 when N_Code_Statement =>
181 Analyze_Code_Statement (N);
182
183 when N_Compilation_Unit =>
184 Analyze_Compilation_Unit (N);
185
186 when N_Component_Declaration =>
187 Analyze_Component_Declaration (N);
188
189 when N_Compound_Statement =>
190 Analyze_Compound_Statement (N);
191
192 when N_Conditional_Entry_Call =>
193 Analyze_Conditional_Entry_Call (N);
194
195 when N_Delay_Alternative =>
196 Analyze_Delay_Alternative (N);
197
198 when N_Delay_Relative_Statement =>
199 Analyze_Delay_Relative (N);
200
201 when N_Delay_Until_Statement =>
202 Analyze_Delay_Until (N);
203
204 when N_Delta_Aggregate =>
205 Analyze_Aggregate (N);
206
207 when N_Entry_Body =>
208 Analyze_Entry_Body (N);
209
210 when N_Entry_Body_Formal_Part =>
211 Analyze_Entry_Body_Formal_Part (N);
212
213 when N_Entry_Call_Alternative =>
214 Analyze_Entry_Call_Alternative (N);
215
216 when N_Entry_Declaration =>
217 Analyze_Entry_Declaration (N);
218
219 when N_Entry_Index_Specification =>
220 Analyze_Entry_Index_Specification (N);
221
222 when N_Enumeration_Representation_Clause =>
223 Analyze_Enumeration_Representation_Clause (N);
224
225 when N_Exception_Declaration =>
226 Analyze_Exception_Declaration (N);
227
228 when N_Exception_Renaming_Declaration =>
229 Analyze_Exception_Renaming (N);
230
231 when N_Exit_Statement =>
232 Analyze_Exit_Statement (N);
233
234 when N_Expanded_Name =>
235 Analyze_Expanded_Name (N);
236
237 when N_Explicit_Dereference =>
238 Analyze_Explicit_Dereference (N);
239
240 when N_Expression_Function =>
241 Analyze_Expression_Function (N);
242
243 when N_Expression_With_Actions =>
244 Analyze_Expression_With_Actions (N);
245
246 when N_Extended_Return_Statement =>
247 Analyze_Extended_Return_Statement (N);
248
249 when N_Extension_Aggregate =>
250 Analyze_Aggregate (N);
251
252 -- The expansion of the External_Initialization aspect creates fully
253 -- analyzed N_External_Initializer nodes.
254
255 when N_External_Initializer =>
256 null;
257
258 when N_Formal_Object_Declaration =>
259 Analyze_Formal_Object_Declaration (N);
260
261 when N_Formal_Package_Declaration =>
262 Analyze_Formal_Package_Declaration (N);
263
264 when N_Formal_Subprogram_Declaration =>
265 Analyze_Formal_Subprogram_Declaration (N);
266
267 when N_Formal_Type_Declaration =>
268 Analyze_Formal_Type_Declaration (N);
269
270 when N_Free_Statement =>
271 Analyze_Free_Statement (N);
272
273 when N_Freeze_Entity =>
274 Analyze_Freeze_Entity (N);
275
276 when N_Freeze_Generic_Entity =>
277 Analyze_Freeze_Generic_Entity (N);
278
279 when N_Full_Type_Declaration =>
280 Analyze_Full_Type_Declaration (N);
281
282 when N_Function_Call =>
283 Analyze_Function_Call (N);
284
285 when N_Function_Instantiation =>
286 Analyze_Function_Instantiation (N);
287
288 when N_Generic_Function_Renaming_Declaration =>
289 Analyze_Generic_Function_Renaming (N);
290
291 when N_Generic_Package_Declaration =>
292 Analyze_Generic_Package_Declaration (N);
293
294 when N_Generic_Package_Renaming_Declaration =>
295 Analyze_Generic_Package_Renaming (N);
296
297 when N_Generic_Procedure_Renaming_Declaration =>
298 Analyze_Generic_Procedure_Renaming (N);
299
300 when N_Generic_Subprogram_Declaration =>
301 Analyze_Generic_Subprogram_Declaration (N);
302
303 when N_Goto_Statement =>
304 Analyze_Goto_Statement (N);
305
306 when N_Goto_When_Statement =>
307 Analyze_Goto_When_Statement (N);
308
309 when N_Handled_Sequence_Of_Statements =>
310 Analyze_Handled_Statements (N);
311
312 when N_Identifier =>
313 Analyze_Identifier (N);
314
315 when N_If_Expression =>
316 Analyze_If_Expression (N);
317
318 when N_If_Statement =>
319 Analyze_If_Statement (N);
320
321 when N_Implicit_Label_Declaration =>
322 Analyze_Implicit_Label_Declaration (N);
323
324 when N_In =>
325 Analyze_Membership_Op (N);
326
327 when N_Incomplete_Type_Declaration =>
328 Analyze_Incomplete_Type_Decl (N);
329
330 when N_Indexed_Component =>
331 Analyze_Indexed_Component_Form (N);
332
333 when N_Integer_Literal =>
334 Analyze_Integer_Literal (N);
335
336 when N_Iterator_Specification =>
337 Analyze_Iterator_Specification (N);
338
339 when N_Itype_Reference =>
340 Analyze_Itype_Reference (N);
341
342 when N_Label =>
343 Analyze_Label (N);
344
345 when N_Loop_Parameter_Specification =>
346 Analyze_Loop_Parameter_Specification (N);
347
348 when N_Loop_Statement =>
349 Analyze_Loop_Statement (N);
350
351 when N_Not_In =>
352 Analyze_Membership_Op (N);
353
354 when N_Null =>
355 Analyze_Null (N);
356
357 when N_Null_Statement =>
358 Analyze_Null_Statement (N);
359
360 when N_Number_Declaration =>
361 Analyze_Number_Declaration (N);
362
363 when N_Object_Declaration =>
364 Analyze_Object_Declaration (N);
365
366 when N_Object_Renaming_Declaration =>
367 Analyze_Object_Renaming (N);
368
369 when N_Operator_Symbol =>
370 Analyze_Operator_Symbol (N);
371
372 when N_Op_Abs =>
373 Analyze_Unary_Op (N);
374
375 when N_Op_Add =>
376 Analyze_Arithmetic_Op (N);
377
378 when N_Op_And =>
379 Analyze_Logical_Op (N);
380
381 when N_Op_Concat =>
382 Analyze_Concatenation (N);
383
384 when N_Op_Divide =>
385 Analyze_Arithmetic_Op (N);
386
387 when N_Op_Eq =>
388 Analyze_Comparison_Equality_Op (N);
389
390 when N_Op_Expon =>
391 Analyze_Arithmetic_Op (N);
392
393 when N_Op_Ge =>
394 Analyze_Comparison_Equality_Op (N);
395
396 when N_Op_Gt =>
397 Analyze_Comparison_Equality_Op (N);
398
399 when N_Op_Le =>
400 Analyze_Comparison_Equality_Op (N);
401
402 when N_Op_Lt =>
403 Analyze_Comparison_Equality_Op (N);
404
405 when N_Op_Minus =>
406 Analyze_Unary_Op (N);
407
408 when N_Op_Mod =>
409 Analyze_Mod (N);
410
411 when N_Op_Multiply =>
412 Analyze_Arithmetic_Op (N);
413
414 when N_Op_Ne =>
415 Analyze_Comparison_Equality_Op (N);
416
417 when N_Op_Not =>
418 Analyze_Negation (N);
419
420 when N_Op_Or =>
421 Analyze_Logical_Op (N);
422
423 when N_Op_Plus =>
424 Analyze_Unary_Op (N);
425
426 when N_Op_Rem =>
427 Analyze_Arithmetic_Op (N);
428
429 when N_Op_Rotate_Left =>
430 Analyze_Arithmetic_Op (N);
431
432 when N_Op_Rotate_Right =>
433 Analyze_Arithmetic_Op (N);
434
435 when N_Op_Shift_Left =>
436 Analyze_Arithmetic_Op (N);
437
438 when N_Op_Shift_Right =>
439 Analyze_Arithmetic_Op (N);
440
441 when N_Op_Shift_Right_Arithmetic =>
442 Analyze_Arithmetic_Op (N);
443
444 when N_Op_Subtract =>
445 Analyze_Arithmetic_Op (N);
446
447 when N_Op_Xor =>
448 Analyze_Logical_Op (N);
449
450 when N_Or_Else =>
451 Analyze_Short_Circuit (N);
452
453 when N_Others_Choice =>
454 Analyze_Others_Choice (N);
455
456 when N_Package_Body =>
457 Analyze_Package_Body (N);
458
459 when N_Package_Body_Stub =>
460 Analyze_Package_Body_Stub (N);
461
462 when N_Package_Declaration =>
463 Analyze_Package_Declaration (N);
464
465 when N_Package_Instantiation =>
466 Analyze_Package_Instantiation (N);
467
468 when N_Package_Renaming_Declaration =>
469 Analyze_Package_Renaming (N);
470
471 when N_Package_Specification =>
472 Analyze_Package_Specification (N);
473
474 when N_Parameter_Association =>
475 Analyze_Parameter_Association (N);
476
477 when N_Pragma =>
478 Analyze_Pragma (N);
479
480 when N_Private_Extension_Declaration =>
481 Analyze_Private_Extension_Declaration (N);
482
483 when N_Private_Type_Declaration =>
484 Analyze_Private_Type_Declaration (N);
485
486 when N_Procedure_Call_Statement =>
487 Analyze_Procedure_Call (N);
488
489 when N_Procedure_Instantiation =>
490 Analyze_Procedure_Instantiation (N);
491
492 when N_Protected_Body =>
493 Analyze_Protected_Body (N);
494
495 when N_Protected_Body_Stub =>
496 Analyze_Protected_Body_Stub (N);
497
498 when N_Protected_Definition =>
499 Analyze_Protected_Definition (N);
500
501 when N_Protected_Type_Declaration =>
502 Analyze_Protected_Type_Declaration (N);
503
504 when N_Qualified_Expression =>
505 Analyze_Qualified_Expression (N);
506
507 when N_Quantified_Expression =>
508 Analyze_Quantified_Expression (N);
509
510 when N_Raise_Expression =>
511 Analyze_Raise_Expression (N);
512
513 when N_Raise_Statement =>
514 Analyze_Raise_Statement (N);
515
516 when N_Raise_When_Statement =>
517 Analyze_Raise_When_Statement (N);
518
519 when N_Raise_xxx_Error =>
520 Analyze_Raise_xxx_Error (N);
521
522 when N_Range =>
523 Analyze_Range (N);
524
525 when N_Range_Constraint =>
526 Analyze_Range (Range_Expression (N));
527
528 when N_Real_Literal =>
529 Analyze_Real_Literal (N);
530
531 when N_Record_Representation_Clause =>
532 Analyze_Record_Representation_Clause (N);
533
534 when N_Reference =>
535 Analyze_Reference (N);
536
537 when N_Requeue_Statement =>
538 Analyze_Requeue (N);
539
540 when N_Return_When_Statement =>
541 Analyze_Return_When_Statement (N);
542
543 when N_Simple_Return_Statement =>
544 Analyze_Simple_Return_Statement (N);
545
546 when N_Selected_Component =>
547 Find_Selected_Component (N);
548 -- ??? why not Analyze_Selected_Component, needs comments
549
550 when N_Selective_Accept =>
551 Analyze_Selective_Accept (N);
552
553 when N_Single_Protected_Declaration =>
554 Analyze_Single_Protected_Declaration (N);
555
556 when N_Single_Task_Declaration =>
557 Analyze_Single_Task_Declaration (N);
558
559 when N_Slice =>
560 Analyze_Slice (N);
561
562 when N_String_Literal =>
563 Analyze_String_Literal (N);
564
565 when N_Interpolated_String_Literal =>
566 Analyze_Interpolated_String_Literal (N);
567
568 when N_Subprogram_Body =>
569 Analyze_Subprogram_Body (N);
570
571 when N_Subprogram_Body_Stub =>
572 Analyze_Subprogram_Body_Stub (N);
573
574 when N_Subprogram_Declaration =>
575 Analyze_Subprogram_Declaration (N);
576
577 when N_Subprogram_Renaming_Declaration =>
578 Analyze_Subprogram_Renaming (N);
579
580 when N_Subtype_Declaration =>
581 Analyze_Subtype_Declaration (N);
582
583 when N_Subtype_Indication =>
584 Analyze_Subtype_Indication (N);
585
586 when N_Subunit =>
587 Analyze_Subunit (N);
588
589 when N_Target_Name =>
590 Analyze_Target_Name (N);
591
592 when N_Task_Body =>
593 Analyze_Task_Body (N);
594
595 when N_Task_Body_Stub =>
596 Analyze_Task_Body_Stub (N);
597
598 when N_Task_Definition =>
599 Analyze_Task_Definition (N);
600
601 when N_Task_Type_Declaration =>
602 Analyze_Task_Type_Declaration (N);
603
604 when N_Terminate_Alternative =>
605 Analyze_Terminate_Alternative (N);
606
607 when N_Timed_Entry_Call =>
608 Analyze_Timed_Entry_Call (N);
609
610 when N_Triggering_Alternative =>
611 Analyze_Triggering_Alternative (N);
612
613 when N_Type_Conversion =>
614 Analyze_Type_Conversion (N);
615
616 when N_Unchecked_Type_Conversion =>
617 Analyze_Unchecked_Type_Conversion (N);
618
619 when N_Use_Package_Clause =>
620 Analyze_Use_Package (N);
621
622 when N_Use_Type_Clause =>
623 Analyze_Use_Type (N);
624
625 when N_Validate_Unchecked_Conversion =>
626 null;
627
628 when N_Variant_Part =>
629 Analyze_Variant_Part (N);
630
631 when N_With_Clause =>
632 Analyze_With_Clause (N);
633
634 -- A call to analyze a marker is ignored because the node does not
635 -- have any static and run-time semantics.
636
637 when N_Call_Marker
638 | N_Variable_Reference_Marker
639 =>
640 null;
641
642 -- A call to analyze the Empty node is an error, but most likely it
643 -- is an error caused by an attempt to analyze a malformed piece of
644 -- tree caused by some other error, so if there have been any other
645 -- errors, we just ignore it, otherwise it is a real internal error
646 -- which we complain about.
647
648 -- We must also consider the case of call to a runtime function that
649 -- is not available in the configurable runtime.
650
651 when N_Empty =>
652 pragma Assert (Serious_Errors_Detected /= 0
653 or else Configurable_Run_Time_Violations /= 0);
654 null;
655
656 -- A call to analyze the error node is simply ignored, to avoid
657 -- causing cascaded errors (happens of course only in error cases)
658 -- Disable expansion in case it is still enabled, to prevent other
659 -- subsequent compiler glitches.
660
661 when N_Error =>
662 Expander_Mode_Save_And_Set (False);
663 null;
664
665 -- Push/Pop nodes normally don't come through an analyze call. An
666 -- exception is the dummy ones bracketing a subprogram body. In any
667 -- case there is nothing to be done to analyze such nodes.
668
669 when N_Push_Pop_xxx_Label =>
670 null;
671
672 -- SCIL nodes don't need analysis because they are decorated when
673 -- they are built. They are added to the tree by Insert_Actions and
674 -- the call to analyze them is generated when the full list is
675 -- analyzed.
676
677 when N_SCIL_Dispatch_Table_Tag_Init
678 | N_SCIL_Dispatching_Call
679 | N_SCIL_Membership_Test
680 =>
681 null;
682
683 when N_Iterated_Element_Association =>
684 null; -- May require a more precise error if misplaced.
685
686 -- For the remaining node types, we generate compiler abort, because
687 -- these nodes are always analyzed within the Sem_Chn routines and
688 -- there should never be a case of making a call to the main Analyze
689 -- routine for these node kinds. For example, an N_Access_Definition
690 -- node appears only in the context of a type declaration, and is
691 -- processed by the analyze routine for type declarations.
692
693 when N_Abortable_Part
694 | N_Access_Definition
695 | N_Access_Function_Definition
696 | N_Access_Procedure_Definition
697 | N_Access_To_Object_Definition
698 | N_Aspect_Specification
699 | N_Case_Expression_Alternative
700 | N_Case_Statement_Alternative
701 | N_Compilation_Unit_Aux
702 | N_Component_Association
703 | N_Component_Clause
704 | N_Component_Definition
705 | N_Component_List
706 | N_Constrained_Array_Definition
707 | N_Contract
708 | N_Decimal_Fixed_Point_Definition
709 | N_Defining_Character_Literal
710 | N_Defining_Identifier
711 | N_Defining_Operator_Symbol
712 | N_Defining_Program_Unit_Name
713 | N_Delta_Constraint
714 | N_Derived_Type_Definition
715 | N_Designator
716 | N_Digits_Constraint
717 | N_Discriminant_Association
718 | N_Discriminant_Specification
719 | N_Elsif_Part
720 | N_Entry_Call_Statement
721 | N_Enumeration_Type_Definition
722 | N_Exception_Handler
723 | N_Floating_Point_Definition
724 | N_Formal_Decimal_Fixed_Point_Definition
725 | N_Formal_Derived_Type_Definition
726 | N_Formal_Discrete_Type_Definition
727 | N_Formal_Floating_Point_Definition
728 | N_Formal_Modular_Type_Definition
729 | N_Formal_Ordinary_Fixed_Point_Definition
730 | N_Formal_Private_Type_Definition
731 | N_Formal_Incomplete_Type_Definition
732 | N_Formal_Signed_Integer_Type_Definition
733 | N_Function_Specification
734 | N_Generic_Association
735 | N_Index_Or_Discriminant_Constraint
736 | N_Iterated_Component_Association
737 | N_Iteration_Scheme
738 | N_Mod_Clause
739 | N_Modular_Type_Definition
740 | N_Ordinary_Fixed_Point_Definition
741 | N_Parameter_Specification
742 | N_Pragma_Argument_Association
743 | N_Procedure_Specification
744 | N_Real_Range_Specification
745 | N_Record_Definition
746 | N_Signed_Integer_Type_Definition
747 | N_Unconstrained_Array_Definition
748 | N_Unused_At_End
749 | N_Unused_At_Start
750 | N_Variant
751 =>
752 raise Program_Error;
753 end case;
754
755 Debug_A_Exit ("analyzing ", N, " (done)");
756
757 -- Set Is_Not_Self_Hidden flag. RM-8.3(16) says a declaration
758 -- is no longer hidden from all visibility after "the end of the
759 -- declaration", so we set the flag here (in addition to setting it
760 -- elsewhere to handle the "except..." cases of 8.3(16)). However,
761 -- we implement 3.8(10) using the same flag, so in that case we
762 -- need to defer the setting until the end of the record.
763
764 declare
765 E : constant Entity_Id := Defining_Entity_Or_Empty (N);
766 begin
767 if Present (E) then
768 if Ekind (E) = E_Void
769 and then Nkind (N) = N_Component_Declaration
770 and then Present (Scope (E))
771 and then Ekind (Scope (E)) = E_Record_Type
772 then
773 null; -- Set it later, in Analyze_Component_Declaration
774 elsif not Is_Not_Self_Hidden (E) then
775 Set_Is_Not_Self_Hidden (E);
776 end if;
777 end if;
778 end;
779
780 -- Mark relevant use-type and use-package clauses as effective
781 -- preferring the original node over the analyzed one in the case that
782 -- constant folding has occurred and removed references that need to be
783 -- examined. Also, if the node in question is overloaded then this is
784 -- deferred until resolution.
785
786 declare
787 Operat : Node_Id := Empty;
788 begin
789 -- Attempt to obtain a checkable operator node
790
791 if Nkind (Original_Node (N)) in N_Op then
792 Operat := Original_Node (N);
793 elsif Nkind (N) in N_Op then
794 Operat := N;
795 end if;
796
797 -- Mark the operator
798
799 if Present (Operat)
800 and then Present (Entity (Operat))
801 and then not Is_Overloaded (Operat)
802 then
803 Mark_Use_Clauses (Operat);
804 end if;
805 end;
806
807 -- Now that we have analyzed the node, we call the expander to perform
808 -- possible expansion. We skip this for subexpressions, because we don't
809 -- have the type yet, and the expander will need to know the type before
810 -- it can do its job. For subexpression nodes, the call to the expander
811 -- happens in Sem_Res.Resolve. A special exception is Raise_xxx_Error,
812 -- which can appear in a statement context, and needs expanding now in
813 -- the case (distinguished by Etype, as documented in Sinfo).
814
815 -- The Analyzed flag is also set at this point for non-subexpression
816 -- nodes (in the case of subexpression nodes, we can't set the flag yet,
817 -- since resolution and expansion have not yet been completed). Note
818 -- that for N_Raise_xxx_Error we have to distinguish the expression
819 -- case from the statement case.
820
821 if Nkind (N) not in N_Subexpr
822 or else (Nkind (N) in N_Raise_xxx_Error
823 and then Etype (N) = Standard_Void_Type)
824 then
825 Expand (N);
826
827 -- Replace a reference to a renaming with the renamed object for SPARK.
828 -- In general this modification is performed by Expand_SPARK, however
829 -- certain constructs may not reach the resolution or expansion phase
830 -- and thus remain unchanged. The replacement is not performed when the
831 -- construct is overloaded as resolution must first take place. This is
832 -- also not done when analyzing a generic to preserve the original tree
833 -- and because the reference may become overloaded in the instance.
834
835 elsif GNATprove_Mode
836 and then Nkind (N) in N_Expanded_Name | N_Identifier
837 and then not Is_Overloaded (N)
838 and then not Inside_A_Generic
839 then
840 Expand_SPARK_Potential_Renaming (N);
841 end if;
842
843 Restore_Ghost_Region (Saved_GM, Saved_IGR);
844 end Analyze;
845
846 -- Version with check(s) suppressed
847
848 procedure Analyze (N : Node_Id; Suppress : Check_Id) is
849 begin
850 if Suppress = All_Checks then
851 declare
852 Svs : constant Suppress_Array := Scope_Suppress.Suppress;
853 begin
854 Scope_Suppress.Suppress := (others => True);
855 Analyze (N);
856 Scope_Suppress.Suppress := Svs;
857 end;
858
859 else
860 declare
861 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
862 begin
863 Scope_Suppress.Suppress (Suppress) := True;
864 Analyze (N);
865 Scope_Suppress.Suppress (Suppress) := Svg;
866 end;
867 end if;
868 end Analyze;
869
870 ------------------
871 -- Analyze_List --
872 ------------------
873
874 procedure Analyze_List (L : List_Id) is
875 Node : Node_Id;
876
877 begin
878 Node := First (L);
879 while Present (Node) loop
880 Analyze (Node);
881 Next (Node);
882 end loop;
883 end Analyze_List;
884
885 -- Version with check(s) suppressed
886
887 procedure Analyze_List (L : List_Id; Suppress : Check_Id) is
888 begin
889 if Suppress = All_Checks then
890 declare
891 Svs : constant Suppress_Array := Scope_Suppress.Suppress;
892 begin
893 Scope_Suppress.Suppress := (others => True);
894 Analyze_List (L);
895 Scope_Suppress.Suppress := Svs;
896 end;
897
898 else
899 declare
900 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
901 begin
902 Scope_Suppress.Suppress (Suppress) := True;
903 Analyze_List (L);
904 Scope_Suppress.Suppress (Suppress) := Svg;
905 end;
906 end if;
907 end Analyze_List;
908
909 --------------------------
910 -- Copy_Suppress_Status --
911 --------------------------
912
913 procedure Copy_Suppress_Status
914 (C : Check_Id;
915 From : Entity_Id;
916 To : Entity_Id)
917 is
918 Found : Boolean;
919 pragma Warnings (Off, Found);
920
921 procedure Search_Stack
922 (Top : Suppress_Stack_Entry_Ptr;
923 Found : out Boolean);
924 -- Search given suppress stack for matching entry for entity. If found
925 -- then set Checks_May_Be_Suppressed on To, and push an appropriate
926 -- entry for To onto the local suppress stack.
927
928 ------------------
929 -- Search_Stack --
930 ------------------
931
932 procedure Search_Stack
933 (Top : Suppress_Stack_Entry_Ptr;
934 Found : out Boolean)
935 is
936 Ptr : Suppress_Stack_Entry_Ptr;
937
938 begin
939 Ptr := Top;
940 while Ptr /= null loop
941 if Ptr.Entity = From
942 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
943 then
944 if Ptr.Suppress then
945 Set_Checks_May_Be_Suppressed (To, True);
946 Push_Local_Suppress_Stack_Entry
947 (Entity => To,
948 Check => C,
949 Suppress => True);
950 Found := True;
951 return;
952 end if;
953 end if;
954
955 Ptr := Ptr.Prev;
956 end loop;
957
958 Found := False;
959 return;
960 end Search_Stack;
961
962 -- Start of processing for Copy_Suppress_Status
963
964 begin
965 if not Checks_May_Be_Suppressed (From) then
966 return;
967 end if;
968
969 -- First search the global entity suppress table for a matching entry.
970 -- We also search this in reverse order so that if there are multiple
971 -- pragmas for the same entity, the last one applies.
972
973 Search_Stack (Global_Suppress_Stack_Top, Found);
974
975 if Found then
976 return;
977 end if;
978
979 -- Now search the local entity suppress stack, we search this in
980 -- reverse order so that we get the innermost entry that applies to
981 -- this case if there are nested entries. Note that for the purpose
982 -- of this procedure we are ONLY looking for entries corresponding
983 -- to a two-argument Suppress, where the second argument matches From.
984
985 Search_Stack (Local_Suppress_Stack_Top, Found);
986 end Copy_Suppress_Status;
987
988 -------------------------
989 -- Enter_Generic_Scope --
990 -------------------------
991
992 procedure Enter_Generic_Scope (S : Entity_Id) is
993 begin
994 if No (Outer_Generic_Scope) then
995 Outer_Generic_Scope := S;
996 end if;
997 end Enter_Generic_Scope;
998
999 ------------------------
1000 -- Exit_Generic_Scope --
1001 ------------------------
1002
1003 procedure Exit_Generic_Scope (S : Entity_Id) is
1004 begin
1005 if S = Outer_Generic_Scope then
1006 Outer_Generic_Scope := Empty;
1007 end if;
1008 end Exit_Generic_Scope;
1009
1010 -----------------------
1011 -- Explicit_Suppress --
1012 -----------------------
1013
1014 function Explicit_Suppress (E : Entity_Id; C : Check_Id) return Boolean is
1015 Ptr : Suppress_Stack_Entry_Ptr;
1016
1017 begin
1018 if not Checks_May_Be_Suppressed (E) then
1019 return False;
1020
1021 else
1022 Ptr := Global_Suppress_Stack_Top;
1023 while Ptr /= null loop
1024 if Ptr.Entity = E
1025 and then (Ptr.Check = All_Checks or else Ptr.Check = C)
1026 then
1027 return Ptr.Suppress;
1028 end if;
1029
1030 Ptr := Ptr.Prev;
1031 end loop;
1032 end if;
1033
1034 return False;
1035 end Explicit_Suppress;
1036
1037 -----------------------------
1038 -- External_Ref_In_Generic --
1039 -----------------------------
1040
1041 function External_Ref_In_Generic (E : Entity_Id) return Boolean is
1042 Scop : Entity_Id;
1043
1044 begin
1045 -- Entity is global if defined outside of current Outer_Generic_Scope:
1046 -- Either the entity has a smaller depth than the outer generic, or it
1047 -- is in a different compilation unit, or it is defined within a unit
1048 -- in the same compilation, that is not within the outer generic.
1049
1050 if No (Outer_Generic_Scope) then
1051 return False;
1052
1053 -- It makes no sense to compare depths if not in same unit. Scope_Depth
1054 -- is not set for inherited operations.
1055
1056 elsif not In_Same_Source_Unit (E, Outer_Generic_Scope)
1057 or else not Scope_Depth_Set (Scope (E))
1058 or else Scope_Depth (Scope (E)) < Scope_Depth (Outer_Generic_Scope)
1059 then
1060 return True;
1061
1062 else
1063 Scop := Scope (E);
1064 while Present (Scop) loop
1065 if Scop = Outer_Generic_Scope then
1066 return False;
1067 elsif Scope_Depth (Scop) < Scope_Depth (Outer_Generic_Scope) then
1068 return True;
1069 else
1070 Scop := Scope (Scop);
1071 end if;
1072 end loop;
1073
1074 return True;
1075 end if;
1076 end External_Ref_In_Generic;
1077
1078 ----------------
1079 -- Initialize --
1080 ----------------
1081
1082 procedure Initialize is
1083 Next : Suppress_Stack_Entry_Ptr;
1084
1085 procedure Free is new Ada.Unchecked_Deallocation
1086 (Suppress_Stack_Entry, Suppress_Stack_Entry_Ptr);
1087
1088 begin
1089 -- Free any global suppress stack entries from a previous invocation
1090 -- of the compiler (in the normal case this loop does nothing).
1091
1092 while Suppress_Stack_Entries /= null loop
1093 Next := Suppress_Stack_Entries.Next;
1094 Free (Suppress_Stack_Entries);
1095 Suppress_Stack_Entries := Next;
1096 end loop;
1097
1098 Local_Suppress_Stack_Top := null;
1099 Global_Suppress_Stack_Top := null;
1100
1101 -- Clear scope stack, and reset global variables
1102
1103 Scope_Stack.Init;
1104 Unloaded_Subunits := False;
1105 end Initialize;
1106
1107 ------------------------------
1108 -- Insert_After_And_Analyze --
1109 ------------------------------
1110
1111 procedure Insert_After_And_Analyze (N : Node_Id; M : Node_Id) is
1112 Node : Node_Id;
1113
1114 begin
1115 if Present (M) then
1116
1117 -- If we are not at the end of the list, then the easiest
1118 -- coding is simply to insert before our successor.
1119
1120 if Present (Next (N)) then
1121 Insert_Before_And_Analyze (Next (N), M);
1122
1123 -- Case of inserting at the end of the list
1124
1125 else
1126 -- Capture the Node_Id of the node to be inserted. This Node_Id
1127 -- will still be the same after the insert operation.
1128
1129 Node := M;
1130 Insert_After (N, M);
1131
1132 -- Now just analyze from the inserted node to the end of
1133 -- the new list (note that this properly handles the case
1134 -- where any of the analyze calls result in the insertion of
1135 -- nodes after the analyzed node, expecting analysis).
1136
1137 while Present (Node) loop
1138 Analyze (Node);
1139 Mark_Rewrite_Insertion (Node);
1140 Next (Node);
1141 end loop;
1142 end if;
1143 end if;
1144 end Insert_After_And_Analyze;
1145
1146 -- Version with check(s) suppressed
1147
1148 procedure Insert_After_And_Analyze
1149 (N : Node_Id;
1150 M : Node_Id;
1151 Suppress : Check_Id)
1152 is
1153 begin
1154 if Suppress = All_Checks then
1155 declare
1156 Svs : constant Suppress_Array := Scope_Suppress.Suppress;
1157 begin
1158 Scope_Suppress.Suppress := (others => True);
1159 Insert_After_And_Analyze (N, M);
1160 Scope_Suppress.Suppress := Svs;
1161 end;
1162
1163 else
1164 declare
1165 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
1166 begin
1167 Scope_Suppress.Suppress (Suppress) := True;
1168 Insert_After_And_Analyze (N, M);
1169 Scope_Suppress.Suppress (Suppress) := Svg;
1170 end;
1171 end if;
1172 end Insert_After_And_Analyze;
1173
1174 -------------------------------
1175 -- Insert_Before_And_Analyze --
1176 -------------------------------
1177
1178 procedure Insert_Before_And_Analyze (N : Node_Id; M : Node_Id) is
1179 Node : Node_Id;
1180
1181 begin
1182 if Present (M) then
1183
1184 -- Capture the Node_Id of the first list node to be inserted.
1185 -- This will still be the first node after the insert operation,
1186 -- since Insert_List_After does not modify the Node_Id values.
1187
1188 Node := M;
1189 Insert_Before (N, M);
1190
1191 -- The insertion does not change the Id's of any of the nodes in
1192 -- the list, and they are still linked, so we can simply loop from
1193 -- the original first node until we meet the node before which the
1194 -- insertion is occurring. Note that this properly handles the case
1195 -- where any of the analyzed nodes insert nodes after themselves,
1196 -- expecting them to get analyzed.
1197
1198 while Node /= N loop
1199 Analyze (Node);
1200 Mark_Rewrite_Insertion (Node);
1201 Next (Node);
1202 end loop;
1203 end if;
1204 end Insert_Before_And_Analyze;
1205
1206 -- Version with check(s) suppressed
1207
1208 procedure Insert_Before_And_Analyze
1209 (N : Node_Id;
1210 M : Node_Id;
1211 Suppress : Check_Id)
1212 is
1213 begin
1214 if Suppress = All_Checks then
1215 declare
1216 Svs : constant Suppress_Array := Scope_Suppress.Suppress;
1217 begin
1218 Scope_Suppress.Suppress := (others => True);
1219 Insert_Before_And_Analyze (N, M);
1220 Scope_Suppress.Suppress := Svs;
1221 end;
1222
1223 else
1224 declare
1225 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
1226 begin
1227 Scope_Suppress.Suppress (Suppress) := True;
1228 Insert_Before_And_Analyze (N, M);
1229 Scope_Suppress.Suppress (Suppress) := Svg;
1230 end;
1231 end if;
1232 end Insert_Before_And_Analyze;
1233
1234 --------------------------------------------
1235 -- Insert_Before_First_Source_Declaration --
1236 --------------------------------------------
1237
1238 procedure Insert_Before_First_Source_Declaration
1239 (Stmt : Node_Id;
1240 Decls : List_Id)
1241 is
1242 Decl : Node_Id;
1243 begin
1244 -- Inspect the declarations of the related subprogram body looking for
1245 -- the first source declaration.
1246
1247 pragma Assert (Present (Decls));
1248
1249 Decl := First (Decls);
1250 while Present (Decl) loop
1251 if Comes_From_Source (Decl) then
1252 Insert_Before (Decl, Stmt);
1253 return;
1254 end if;
1255
1256 Next (Decl);
1257 end loop;
1258
1259 -- If we get there, then the subprogram body lacks any source
1260 -- declarations. The body of _Postconditions now acts as the
1261 -- last declaration.
1262
1263 Append (Stmt, Decls);
1264 end Insert_Before_First_Source_Declaration;
1265
1266 -----------------------------------
1267 -- Insert_List_After_And_Analyze --
1268 -----------------------------------
1269
1270 procedure Insert_List_After_And_Analyze (N : Node_Id; L : List_Id) is
1271 After : constant Node_Id := Next (N);
1272 Node : Node_Id;
1273
1274 begin
1275 if Is_Non_Empty_List (L) then
1276
1277 -- Capture the Node_Id of the first list node to be inserted.
1278 -- This will still be the first node after the insert operation,
1279 -- since Insert_List_After does not modify the Node_Id values.
1280
1281 Node := First (L);
1282 Insert_List_After (N, L);
1283
1284 -- Now just analyze from the original first node until we get to the
1285 -- successor of the original insertion point (which may be Empty if
1286 -- the insertion point was at the end of the list). Note that this
1287 -- properly handles the case where any of the analyze calls result in
1288 -- the insertion of nodes after the analyzed node (possibly calling
1289 -- this routine recursively).
1290
1291 while Node /= After loop
1292 Analyze (Node);
1293 Mark_Rewrite_Insertion (Node);
1294 Next (Node);
1295 end loop;
1296 end if;
1297 end Insert_List_After_And_Analyze;
1298
1299 ------------------------------------
1300 -- Insert_List_Before_And_Analyze --
1301 ------------------------------------
1302
1303 procedure Insert_List_Before_And_Analyze (N : Node_Id; L : List_Id) is
1304 Node : Node_Id;
1305
1306 begin
1307 if Is_Non_Empty_List (L) then
1308
1309 -- Capture the Node_Id of the first list node to be inserted. This
1310 -- will still be the first node after the insert operation, since
1311 -- Insert_List_After does not modify the Node_Id values.
1312
1313 Node := First (L);
1314 Insert_List_Before (N, L);
1315
1316 -- The insertion does not change the Id's of any of the nodes in
1317 -- the list, and they are still linked, so we can simply loop from
1318 -- the original first node until we meet the node before which the
1319 -- insertion is occurring. Note that this properly handles the case
1320 -- where any of the analyzed nodes insert nodes after themselves,
1321 -- expecting them to get analyzed.
1322
1323 while Node /= N loop
1324 Analyze (Node);
1325 Mark_Rewrite_Insertion (Node);
1326 Next (Node);
1327 end loop;
1328 end if;
1329 end Insert_List_Before_And_Analyze;
1330
1331 ----------
1332 -- Lock --
1333 ----------
1334
1335 procedure Lock is
1336 begin
1337 Scope_Stack.Release;
1338 Scope_Stack.Locked := True;
1339 end Lock;
1340
1341 ---------------------------
1342 -- In_Strict_Preanalysis --
1343 ---------------------------
1344
1345 function In_Strict_Preanalysis return Boolean is
1346 begin
1347 return Preanalysis_Active and then not In_Spec_Expression;
1348 end In_Strict_Preanalysis;
1349
1350 ------------------------
1351 -- Preanalysis_Active --
1352 ------------------------
1353
1354 function Preanalysis_Active return Boolean is
1355 begin
1356 return not Full_Analysis and then not Expander_Active;
1357 end Preanalysis_Active;
1358
1359 ----------------
1360 -- Preanalyze --
1361 ----------------
1362
1363 procedure Preanalyze (N : Node_Id) is
1364 Save_Full_Analysis : constant Boolean := Full_Analysis;
1365
1366 begin
1367 Full_Analysis := False;
1368 Expander_Mode_Save_And_Set (False);
1369
1370 -- See comment in sem_res.adb for Preanalyze_And_Resolve
1371
1372 if GNATprove_Mode
1373 or else Nkind (Parent (N)) = N_Simple_Return_Statement
1374 then
1375 Analyze (N);
1376 else
1377 Analyze (N, Suppress => All_Checks);
1378 end if;
1379
1380 Expander_Mode_Restore;
1381 Full_Analysis := Save_Full_Analysis;
1382 end Preanalyze;
1383
1384 --------------------------------------
1385 -- Push_Global_Suppress_Stack_Entry --
1386 --------------------------------------
1387
1388 procedure Push_Global_Suppress_Stack_Entry
1389 (Entity : Entity_Id;
1390 Check : Check_Id;
1391 Suppress : Boolean)
1392 is
1393 begin
1394 Global_Suppress_Stack_Top :=
1395 new Suppress_Stack_Entry'
1396 (Entity => Entity,
1397 Check => Check,
1398 Suppress => Suppress,
1399 Prev => Global_Suppress_Stack_Top,
1400 Next => Suppress_Stack_Entries);
1401 Suppress_Stack_Entries := Global_Suppress_Stack_Top;
1402 return;
1403 end Push_Global_Suppress_Stack_Entry;
1404
1405 -------------------------------------
1406 -- Push_Local_Suppress_Stack_Entry --
1407 -------------------------------------
1408
1409 procedure Push_Local_Suppress_Stack_Entry
1410 (Entity : Entity_Id;
1411 Check : Check_Id;
1412 Suppress : Boolean)
1413 is
1414 begin
1415 Local_Suppress_Stack_Top :=
1416 new Suppress_Stack_Entry'
1417 (Entity => Entity,
1418 Check => Check,
1419 Suppress => Suppress,
1420 Prev => Local_Suppress_Stack_Top,
1421 Next => Suppress_Stack_Entries);
1422 Suppress_Stack_Entries := Local_Suppress_Stack_Top;
1423
1424 return;
1425 end Push_Local_Suppress_Stack_Entry;
1426
1427 ---------------
1428 -- Semantics --
1429 ---------------
1430
1431 procedure Semantics (Comp_Unit : Node_Id) is
1432 procedure Do_Analyze;
1433 -- Perform the analysis of the compilation unit
1434
1435 ----------------
1436 -- Do_Analyze --
1437 ----------------
1438
1439 -- WARNING: This routine manages Ghost regions. Return statements must
1440 -- be replaced by gotos which jump to the end of the routine and restore
1441 -- the Ghost mode.
1442
1443 procedure Do_Analyze is
1444 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1445 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1446 Saved_ISMP : constant Boolean :=
1447 Ignore_SPARK_Mode_Pragmas_In_Instance;
1448 -- Save Ghost and SPARK mode-related data to restore on exit
1449
1450 -- Generally style checks are preserved across compilations, with
1451 -- one exception: s-oscons.ads, which allows arbitrary long lines
1452 -- unconditionally, and has no restore mechanism, because it is
1453 -- intended as a lowest-level Pure package.
1454
1455 Saved_ML : constant Int := Style_Max_Line_Length;
1456 Saved_CML : constant Boolean := Style_Check_Max_Line_Length;
1457
1458 List : Elist_Id;
1459
1460 begin
1461 List := Save_Scope_Stack;
1462 Push_Scope (Standard_Standard);
1463
1464 -- Set up a clean environment before analyzing
1465
1466 Install_Ghost_Region (None, Empty);
1467 Ignore_SPARK_Mode_Pragmas_In_Instance := False;
1468
1469 Outer_Generic_Scope := Empty;
1470 Scope_Suppress := Suppress_Options;
1471 Scope_Stack.Table
1472 (Scope_Stack.Last).Component_Alignment_Default :=
1473 Configuration_Component_Alignment;
1474 Scope_Stack.Table
1475 (Scope_Stack.Last).Is_Active_Stack_Base := True;
1476
1477 -- Now analyze the top level compilation unit node
1478
1479 Analyze (Comp_Unit);
1480
1481 -- Check for scope mismatch on exit from compilation
1482
1483 pragma Assert (Current_Scope = Standard_Standard
1484 or else Comp_Unit = Cunit (Main_Unit));
1485
1486 -- Then pop entry for Standard, and pop implicit types
1487
1488 Pop_Scope;
1489 Restore_Scope_Stack (List);
1490 Style_Max_Line_Length := Saved_ML;
1491 Style_Check_Max_Line_Length := Saved_CML;
1492
1493 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1494 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1495 end Do_Analyze;
1496
1497 -- Local variables
1498
1499 -- The following locations save the corresponding global flags and
1500 -- variables so that they can be restored on completion. This is needed
1501 -- so that calls to Rtsfind start with the proper default values for
1502 -- these variables, and also that such calls do not disturb the settings
1503 -- for units being analyzed at a higher level.
1504
1505 S_Current_Sem_Unit : constant Unit_Number_Type := Current_Sem_Unit;
1506 S_Full_Analysis : constant Boolean := Full_Analysis;
1507 S_GNAT_Mode : constant Boolean := GNAT_Mode;
1508 S_Global_Dis_Names : constant Boolean := Global_Discard_Names;
1509 S_In_Assertion_Expr : constant Nat := In_Assertion_Expr;
1510 S_In_Declare_Expr : constant Nat := In_Declare_Expr;
1511 S_In_Default_Expr : constant Boolean := In_Default_Expr;
1512 S_In_Spec_Expr : constant Boolean := In_Spec_Expression;
1513 S_Inside_A_Generic : constant Boolean := Inside_A_Generic;
1514 S_Outer_Gen_Scope : constant Entity_Id := Outer_Generic_Scope;
1515 S_Style_Check : constant Boolean := Style_Check;
1516
1517 Already_Analyzed : constant Boolean := Analyzed (Comp_Unit);
1518
1519 Curunit : constant Unit_Number_Type := Get_Cunit_Unit_Number (Comp_Unit);
1520 -- New value of Current_Sem_Unit
1521
1522 Generic_Main : constant Boolean :=
1523 Nkind (Unit (Cunit (Main_Unit))) in N_Generic_Declaration;
1524 -- If the main unit is generic, every compiled unit, including its
1525 -- context, is compiled with expansion disabled.
1526
1527 Is_Main_Unit_Or_Main_Unit_Spec : constant Boolean :=
1528 Curunit = Main_Unit
1529 or else
1530 (Nkind (Unit (Cunit (Main_Unit))) = N_Package_Body
1531 and then Spec_Lib_Unit (Cunit (Main_Unit)) = Cunit (Curunit));
1532 -- Configuration flags have special settings when compiling a predefined
1533 -- file as a main unit. This applies to its spec as well.
1534
1535 Ext_Main_Source_Unit : constant Boolean :=
1536 In_Extended_Main_Source_Unit (Comp_Unit);
1537 -- Determine if unit is in extended main source unit
1538
1539 Save_Config_Attrs : Config_Switches_Type;
1540 -- Variable used to save values of config switches while we analyze the
1541 -- new unit, to be restored on exit for proper recursive behavior.
1542
1543 Save_Cunit_Restrictions : Save_Cunit_Boolean_Restrictions;
1544 -- Used to save non-partition wide restrictions before processing new
1545 -- unit. All with'ed units are analyzed with config restrictions reset
1546 -- and we need to restore these saved values at the end.
1547
1548 -- Start of processing for Semantics
1549
1550 begin
1551 if Debug_Unit_Walk then
1552 if Already_Analyzed then
1553 Write_Str ("(done)");
1554 end if;
1555
1556 Write_Unit_Info
1557 (Get_Cunit_Unit_Number (Comp_Unit),
1558 Unit (Comp_Unit),
1559 Prefix => "--> ");
1560 Indent;
1561 end if;
1562
1563 Compiler_State := Analyzing;
1564 Current_Sem_Unit := Curunit;
1565
1566 -- Compile predefined units with GNAT_Mode set to True, to properly
1567 -- process the categorization stuff. However, do not set GNAT_Mode
1568 -- to True for the renaming units (Text_IO, IO_Exceptions, Direct_IO,
1569 -- Sequential_IO) as this would prevent pragma Extend_System from being
1570 -- taken into account, for example when Text_IO is renaming DEC.Text_IO.
1571
1572 if Is_Predefined_Unit (Current_Sem_Unit)
1573 and then not Is_Predefined_Renaming (Current_Sem_Unit)
1574 then
1575 GNAT_Mode := True;
1576 end if;
1577
1578 -- For generic main, never do expansion
1579
1580 if Generic_Main then
1581 Expander_Mode_Save_And_Set (False);
1582
1583 -- Non generic case
1584
1585 else
1586 Expander_Mode_Save_And_Set
1587
1588 -- Turn on expansion if generating code
1589
1590 (Operating_Mode = Generate_Code
1591
1592 -- Or if special debug flag -gnatdx is set
1593
1594 or else Debug_Flag_X
1595
1596 -- Or if in configuration run-time mode. We do this so we get
1597 -- error messages about missing entities in the run-time even
1598 -- if we are compiling in -gnatc (no code generation) mode.
1599 -- Similar processing applies to No_Run_Time_Mode. However,
1600 -- don't do this if debug flag -gnatd.Z is set or when we are
1601 -- compiling a separate unit (this is to handle a situation
1602 -- where this new processing causes trouble).
1603
1604 or else
1605 ((Configurable_Run_Time_Mode or No_Run_Time_Mode)
1606 and then not Debug_Flag_Dot_ZZ
1607 and then Nkind (Unit (Cunit (Main_Unit))) /= N_Subunit));
1608 end if;
1609
1610 Full_Analysis := True;
1611 Inside_A_Generic := False;
1612 In_Assertion_Expr := 0;
1613 In_Declare_Expr := 0;
1614 In_Default_Expr := False;
1615 In_Spec_Expression := False;
1616 Set_Comes_From_Source_Default (False);
1617
1618 -- Save current config switches and reset then appropriately
1619
1620 Save_Config_Attrs := Save_Config_Switches;
1621 Set_Config_Switches
1622 (Is_Internal_Unit (Current_Sem_Unit),
1623 Is_Main_Unit_Or_Main_Unit_Spec);
1624
1625 -- Save current non-partition-wide restrictions
1626
1627 Save_Cunit_Restrictions := Cunit_Boolean_Restrictions_Save;
1628
1629 -- For unit in main extended unit, we reset the configuration values
1630 -- for the non-partition-wide restrictions. For other units reset them.
1631
1632 if Ext_Main_Source_Unit then
1633 Restore_Config_Cunit_Boolean_Restrictions;
1634 else
1635 Reset_Cunit_Boolean_Restrictions;
1636 end if;
1637
1638 -- Turn off style checks for unit that is not in the extended main
1639 -- source unit. This improves processing efficiency for such units
1640 -- (for which we don't want style checks anyway, and where they will
1641 -- get suppressed), and is definitely needed to stop some style checks
1642 -- from invading the run-time units (e.g. overriding checks).
1643
1644 if not Ext_Main_Source_Unit then
1645 Style_Check := False;
1646
1647 -- If this is part of the extended main source unit, set style check
1648 -- mode to match the style check mode of the main source unit itself.
1649
1650 else
1651 Style_Check := Style_Check_Main;
1652 end if;
1653
1654 -- Only do analysis of unit that has not already been analyzed
1655
1656 if not Analyzed (Comp_Unit) then
1657 Initialize_Version (Current_Sem_Unit);
1658
1659 -- Do analysis, and then append the compilation unit onto the
1660 -- Comp_Unit_List, if appropriate. This is done after analysis,
1661 -- so if this unit depends on some others, they have already been
1662 -- appended. We ignore bodies, except for the main unit itself, and
1663 -- for subprogram bodies that act as specs. We have also to guard
1664 -- against ill-formed subunits that have an improper context.
1665
1666 Do_Analyze;
1667
1668 if Present (Comp_Unit)
1669 and then Nkind (Unit (Comp_Unit)) in N_Proper_Body
1670 and then (Nkind (Unit (Comp_Unit)) /= N_Subprogram_Body
1671 or else not Acts_As_Spec (Comp_Unit))
1672 and then not Ext_Main_Source_Unit
1673 then
1674 null;
1675
1676 else
1677 Append_New_Elmt (Comp_Unit, To => Comp_Unit_List);
1678
1679 if Debug_Unit_Walk then
1680 Write_Str ("Appending ");
1681 Write_Unit_Info
1682 (Get_Cunit_Unit_Number (Comp_Unit), Unit (Comp_Unit));
1683 end if;
1684 end if;
1685 end if;
1686
1687 -- Save indication of dynamic elaboration checks for ALI file
1688
1689 Set_Dynamic_Elab (Current_Sem_Unit, Dynamic_Elaboration_Checks);
1690
1691 -- Restore settings of saved switches to entry values
1692
1693 Current_Sem_Unit := S_Current_Sem_Unit;
1694 Full_Analysis := S_Full_Analysis;
1695 Global_Discard_Names := S_Global_Dis_Names;
1696 GNAT_Mode := S_GNAT_Mode;
1697 In_Assertion_Expr := S_In_Assertion_Expr;
1698 In_Declare_Expr := S_In_Declare_Expr;
1699 In_Default_Expr := S_In_Default_Expr;
1700 In_Spec_Expression := S_In_Spec_Expr;
1701 Inside_A_Generic := S_Inside_A_Generic;
1702 Outer_Generic_Scope := S_Outer_Gen_Scope;
1703 Style_Check := S_Style_Check;
1704
1705 Restore_Config_Switches (Save_Config_Attrs);
1706
1707 -- Deal with restore of restrictions
1708
1709 Cunit_Boolean_Restrictions_Restore (Save_Cunit_Restrictions);
1710
1711 Expander_Mode_Restore;
1712
1713 if Debug_Unit_Walk then
1714 Outdent;
1715
1716 if Already_Analyzed then
1717 Write_Str ("(done)");
1718 end if;
1719
1720 Write_Unit_Info
1721 (Get_Cunit_Unit_Number (Comp_Unit),
1722 Unit (Comp_Unit),
1723 Prefix => "<-- ");
1724 end if;
1725 end Semantics;
1726
1727 --------
1728 -- ss --
1729 --------
1730
1731 function ss (Index : Int) return Scope_Stack_Entry is
1732 begin
1733 return Scope_Stack.Table (Index);
1734 end ss;
1735
1736 ---------
1737 -- sst --
1738 ---------
1739
1740 function sst return Scope_Stack_Entry is
1741 begin
1742 return ss (Scope_Stack.Last);
1743 end sst;
1744
1745 ------------
1746 -- Unlock --
1747 ------------
1748
1749 procedure Unlock is
1750 begin
1751 Scope_Stack.Locked := False;
1752 end Unlock;
1753
1754 ------------------------
1755 -- Walk_Library_Items --
1756 ------------------------
1757
1758 procedure Walk_Library_Items is
1759 type Unit_Number_Set is array (Main_Unit .. Last_Unit) of Boolean;
1760 pragma Pack (Unit_Number_Set);
1761
1762 Main_CU : constant Node_Id := Cunit (Main_Unit);
1763 Spec_CU : Node_Id := Empty;
1764
1765 Seen, Done : Unit_Number_Set := (others => False);
1766 -- Seen (X) is True after we have seen unit X in the walk. This is used
1767 -- to prevent processing the same unit more than once. Done (X) is True
1768 -- after we have fully processed X, and is used only for debugging
1769 -- printouts and assertions.
1770
1771 Do_Main : Boolean := False;
1772 -- Flag to delay processing the main body until after all other units.
1773 -- This is needed because the spec of the main unit may appear in the
1774 -- context of some other unit. We do not want this to force processing
1775 -- of the main body before all other units have been processed.
1776 --
1777 -- Another circularity pattern occurs when the main unit is a child unit
1778 -- and the body of an ancestor has a with-clause of the main unit or on
1779 -- one of its children. In both cases the body in question has a with-
1780 -- clause on the main unit, and must be excluded from the traversal. In
1781 -- some convoluted cases this may lead to a CodePeer error because the
1782 -- spec of a subprogram declared in an instance within the parent will
1783 -- not be seen in the main unit.
1784
1785 function Depends_On_Main (CU : Node_Id) return Boolean;
1786 -- The body of a unit that is withed by the spec of the main unit may in
1787 -- turn have a with_clause on that spec. In that case do not traverse
1788 -- the body, to prevent loops. It can also happen that the main body has
1789 -- a with_clause on a child, which of course has an implicit with on its
1790 -- parent. It's OK to traverse the child body if the main spec has been
1791 -- processed, otherwise we also have a circularity to avoid.
1792
1793 procedure Do_Action (CU : Node_Id; Item : Node_Id);
1794 -- Calls Action, with some validity checks
1795
1796 procedure Do_Unit_And_Dependents (CU : Node_Id; Item : Node_Id);
1797 -- Calls Do_Action, first on the units with'ed by this one, then on
1798 -- this unit. If it's an instance body, do the spec first. If it is
1799 -- an instance spec, do the body last.
1800
1801 procedure Do_Withed_Unit (Withed_Unit : Node_Id);
1802 -- Apply Do_Unit_And_Dependents to a unit in a context clause
1803
1804 procedure Process_Bodies_In_Context (Comp : Node_Id);
1805 -- The main unit and its spec may depend on bodies that contain generics
1806 -- that are instantiated in them. Iterate through the corresponding
1807 -- contexts before processing main (spec/body) itself, to process bodies
1808 -- that may be present, together with their context. The spec of main
1809 -- is processed wherever it appears in the list of units, while the body
1810 -- is processed as the last unit in the list.
1811
1812 ---------------------
1813 -- Depends_On_Main --
1814 ---------------------
1815
1816 function Depends_On_Main (CU : Node_Id) return Boolean is
1817 CL : Node_Id;
1818 MCU : constant Node_Id := Unit (Main_CU);
1819
1820 begin
1821 -- Problem does not arise with main subprograms
1822
1823 if Nkind (MCU) not in N_Package_Body | N_Package_Declaration then
1824 return False;
1825 end if;
1826
1827 CL := First (Context_Items (CU));
1828
1829 while Present (CL) loop
1830 if Nkind (CL) = N_With_Clause
1831 and then Withed_Lib_Unit (CL) = Main_CU
1832 and then not Done (Get_Cunit_Unit_Number (Withed_Lib_Unit (CL)))
1833 then
1834 return True;
1835 end if;
1836
1837 Next (CL);
1838 end loop;
1839
1840 return False;
1841 end Depends_On_Main;
1842
1843 ---------------
1844 -- Do_Action --
1845 ---------------
1846
1847 procedure Do_Action (CU : Node_Id; Item : Node_Id) is
1848 begin
1849 -- This calls Action at the end. All the preceding code is just
1850 -- assertions and debugging output.
1851
1852 pragma Assert (No (CU) or else Nkind (CU) = N_Compilation_Unit);
1853
1854 case Nkind (Item) is
1855 when N_Generic_Function_Renaming_Declaration
1856 | N_Generic_Package_Declaration
1857 | N_Generic_Package_Renaming_Declaration
1858 | N_Generic_Procedure_Renaming_Declaration
1859 | N_Generic_Subprogram_Declaration
1860 | N_Package_Declaration
1861 | N_Package_Renaming_Declaration
1862 | N_Subprogram_Declaration
1863 | N_Subprogram_Renaming_Declaration
1864 =>
1865 -- Specs are OK
1866
1867 null;
1868
1869 when N_Package_Body =>
1870
1871 -- Package bodies are processed separately if the main unit
1872 -- depends on them.
1873
1874 null;
1875
1876 when N_Subprogram_Body =>
1877
1878 -- A subprogram body must be the main unit
1879
1880 pragma Assert (Acts_As_Spec (CU) or else CU = Main_CU);
1881 null;
1882
1883 when N_Function_Instantiation
1884 | N_Package_Instantiation
1885 | N_Procedure_Instantiation
1886 =>
1887 -- Can only happen if some generic body (needed for gnat2scil
1888 -- traversal, but not by GNAT) is not available, ignore.
1889
1890 null;
1891
1892 -- All other cases cannot happen
1893
1894 when N_Subunit =>
1895 pragma Assert (False, "subunit");
1896 null;
1897
1898 when N_Null_Statement =>
1899
1900 -- Do not call Action for an ignored ghost unit
1901
1902 pragma Assert (Is_Ignored_Ghost_Node (Original_Node (Item)));
1903 return;
1904
1905 when others =>
1906 pragma Assert (False);
1907 null;
1908 end case;
1909
1910 if Present (CU) then
1911 pragma Assert (Item /= Stand.Standard_Package_Node);
1912 pragma Assert (Item = Unit (CU));
1913
1914 declare
1915 Unit_Num : constant Unit_Number_Type :=
1916 Get_Cunit_Unit_Number (CU);
1917
1918 procedure Assert_Done (Withed_Unit : Node_Id);
1919 -- Assert Withed_Unit is already Done, unless it's a body. It
1920 -- might seem strange for a with_clause to refer to a body, but
1921 -- this happens in the case of a generic instantiation, which
1922 -- gets transformed into the instance body (and the instance
1923 -- spec is also created). With clauses pointing to the
1924 -- instantiation end up pointing to the instance body.
1925
1926 -----------------
1927 -- Assert_Done --
1928 -----------------
1929
1930 procedure Assert_Done (Withed_Unit : Node_Id) is
1931 begin
1932 if Withed_Unit /= Main_CU
1933 and then not Done (Get_Cunit_Unit_Number (Withed_Unit))
1934 then
1935 -- N_Null_Statement will happen in case of a ghost unit
1936 -- which gets rewritten.
1937
1938 if Nkind (Unit (Withed_Unit)) not in
1939 N_Generic_Package_Declaration |
1940 N_Package_Body |
1941 N_Package_Renaming_Declaration |
1942 N_Subprogram_Body |
1943 N_Null_Statement
1944 then
1945 Write_Unit_Name
1946 (Unit_Name (Get_Cunit_Unit_Number (Withed_Unit)));
1947 Write_Str (" not yet walked!");
1948
1949 if Get_Cunit_Unit_Number (Withed_Unit) = Unit_Num then
1950 Write_Str (" (self-ref)");
1951 end if;
1952
1953 Write_Eol;
1954
1955 pragma Assert (False);
1956 end if;
1957 end if;
1958 end Assert_Done;
1959
1960 procedure Assert_Withed_Units_Done is
1961 new Walk_Withs (Assert_Done);
1962
1963 begin
1964 if Debug_Unit_Walk then
1965 Write_Unit_Info (Unit_Num, Item, Withs => True);
1966 end if;
1967
1968 -- Main unit should come last, except in the case where we
1969 -- skipped System_Aux_Id, in which case we missed the things it
1970 -- depends on, and in the case of parent bodies if present.
1971
1972 pragma Assert
1973 (not Done (Main_Unit)
1974 or else Present (System_Aux_Id)
1975 or else Nkind (Item) = N_Package_Body);
1976
1977 -- We shouldn't do the same thing twice
1978
1979 pragma Assert (not Done (Unit_Num));
1980
1981 -- Everything we depend upon should already be done
1982
1983 pragma Debug
1984 (Assert_Withed_Units_Done (CU, Include_Limited => False));
1985 end;
1986
1987 else
1988 -- Must be Standard, which has no entry in the units table
1989
1990 pragma Assert (Item = Stand.Standard_Package_Node);
1991
1992 if Debug_Unit_Walk then
1993 Write_Line ("Standard");
1994 end if;
1995 end if;
1996
1997 Action (Item);
1998 end Do_Action;
1999
2000 --------------------
2001 -- Do_Withed_Unit --
2002 --------------------
2003
2004 procedure Do_Withed_Unit (Withed_Unit : Node_Id) is
2005 begin
2006 Do_Unit_And_Dependents (Withed_Unit, Unit (Withed_Unit));
2007
2008 -- If the unit in the with_clause is a generic instance, the clause
2009 -- now denotes the instance body. Traverse the corresponding spec
2010 -- because there may be no other dependence that will force the
2011 -- traversal of its own context.
2012
2013 if Nkind (Unit (Withed_Unit)) = N_Package_Body
2014 and then Is_Generic_Instance
2015 (Defining_Entity (Unit (Spec_Lib_Unit (Withed_Unit))))
2016 then
2017 Do_Withed_Unit (Spec_Lib_Unit (Withed_Unit));
2018 end if;
2019 end Do_Withed_Unit;
2020
2021 ----------------------------
2022 -- Do_Unit_And_Dependents --
2023 ----------------------------
2024
2025 procedure Do_Unit_And_Dependents (CU : Node_Id; Item : Node_Id) is
2026 Unit_Num : constant Unit_Number_Type := Get_Cunit_Unit_Number (CU);
2027 Child : Node_Id;
2028 Body_U : Unit_Number_Type;
2029 Parent_CU : Node_Id;
2030
2031 procedure Do_Withed_Units is new Walk_Withs (Do_Withed_Unit);
2032
2033 begin
2034 if not Seen (Unit_Num) then
2035
2036 -- Process the with clauses
2037
2038 Do_Withed_Units (CU, Include_Limited => False);
2039
2040 -- Process the unit if it is a spec or the main unit, if it
2041 -- has no previous spec or we have done all other units.
2042
2043 if Nkind (Item) not in N_Package_Body | N_Subprogram_Body
2044 or else Acts_As_Spec (CU)
2045 then
2046 if CU = Main_CU and then not Do_Main then
2047 Seen (Unit_Num) := False;
2048
2049 else
2050 Seen (Unit_Num) := True;
2051
2052 if CU = Spec_Or_Body_Lib_Unit (Main_CU) then
2053 Process_Bodies_In_Context (CU);
2054
2055 -- If main is a child unit, examine parent unit contexts
2056 -- to see if they include instantiated units. Also, if
2057 -- the parent itself is an instance, process its body
2058 -- because it may contain subprograms that are called
2059 -- in the main unit.
2060
2061 if Is_Child_Unit (Cunit_Entity (Main_Unit)) then
2062 Child := Cunit_Entity (Main_Unit);
2063 while Is_Child_Unit (Child) loop
2064 Parent_CU :=
2065 Cunit
2066 (Get_Cunit_Entity_Unit_Number (Scope (Child)));
2067 Process_Bodies_In_Context (Parent_CU);
2068
2069 if Nkind (Unit (Parent_CU)) = N_Package_Body
2070 and then
2071 Nkind (Original_Node (Unit (Parent_CU)))
2072 = N_Package_Instantiation
2073 and then
2074 not Seen (Get_Cunit_Unit_Number (Parent_CU))
2075 then
2076 Body_U := Get_Cunit_Unit_Number (Parent_CU);
2077 Seen (Body_U) := True;
2078 Do_Action (Parent_CU, Unit (Parent_CU));
2079 Done (Body_U) := True;
2080 end if;
2081
2082 Child := Scope (Child);
2083 end loop;
2084 end if;
2085 end if;
2086
2087 Do_Action (CU, Item);
2088 Done (Unit_Num) := True;
2089 end if;
2090 end if;
2091 end if;
2092 end Do_Unit_And_Dependents;
2093
2094 -------------------------------
2095 -- Process_Bodies_In_Context --
2096 -------------------------------
2097
2098 procedure Process_Bodies_In_Context (Comp : Node_Id) is
2099 Body_CU : Node_Id;
2100 Body_U : Unit_Number_Type;
2101 Clause : Node_Id;
2102 Spec : Node_Id;
2103
2104 procedure Do_Withed_Units is new Walk_Withs (Do_Withed_Unit);
2105
2106 -- Start of processing for Process_Bodies_In_Context
2107
2108 begin
2109 Clause := First (Context_Items (Comp));
2110 while Present (Clause) loop
2111 if Nkind (Clause) = N_With_Clause then
2112 Spec := Withed_Lib_Unit (Clause);
2113 if Unit (Spec) in N_Lib_Unit_Declaration_Id
2114 | N_Lib_Unit_Renaming_Declaration_Id
2115 then
2116 Body_CU := Body_Lib_Unit (Spec);
2117
2118 -- If we are processing the spec of the main unit, load
2119 -- bodies only if the with_clause indicates that it forced
2120 -- the loading of the body for a generic instantiation.
2121 -- Note that bodies of parents that are instances have been
2122 -- loaded already.
2123
2124 if Present (Body_CU)
2125 and then Body_CU /= Main_CU
2126 and then Nkind (Unit (Body_CU)) /= N_Subprogram_Body
2127 and then Nkind (Unit (Comp)) /= N_Package_Declaration
2128 then
2129 Body_U := Get_Cunit_Unit_Number (Body_CU);
2130
2131 if not Seen (Body_U)
2132 and then not Depends_On_Main (Body_CU)
2133 then
2134 Seen (Body_U) := True;
2135 Do_Withed_Units (Body_CU, Include_Limited => False);
2136 Do_Action (Body_CU, Unit (Body_CU));
2137 Done (Body_U) := True;
2138 end if;
2139 end if;
2140 end if;
2141 end if;
2142
2143 Next (Clause);
2144 end loop;
2145 end Process_Bodies_In_Context;
2146
2147 -- Local Declarations
2148
2149 Cur : Elmt_Id;
2150
2151 -- Start of processing for Walk_Library_Items
2152
2153 begin
2154 if Debug_Unit_Walk then
2155 Write_Line ("Walk_Library_Items:");
2156 Indent;
2157 end if;
2158
2159 -- Do Standard first, then walk the Comp_Unit_List
2160
2161 Do_Action (Empty, Standard_Package_Node);
2162
2163 -- First place the context of all instance bodies on the corresponding
2164 -- spec, because it may be needed to analyze the code at the place of
2165 -- the instantiation.
2166
2167 Cur := First_Elmt (Comp_Unit_List);
2168 while Present (Cur) loop
2169 declare
2170 CU : constant Node_Id := Node (Cur);
2171 N : constant Node_Id := Unit (CU);
2172
2173 begin
2174 if Nkind (N) = N_Package_Body
2175 and then Is_Generic_Instance (Defining_Entity (N))
2176 then
2177 Append_List
2178 (Context_Items (CU), Context_Items (Spec_Lib_Unit (CU)));
2179 end if;
2180
2181 Next_Elmt (Cur);
2182 end;
2183 end loop;
2184
2185 -- Now traverse compilation units (specs) in order
2186
2187 Cur := First_Elmt (Comp_Unit_List);
2188 while Present (Cur) loop
2189 declare
2190 CU : constant Node_Id := Node (Cur);
2191 N : constant Node_Id := Unit (CU);
2192 Par : Entity_Id;
2193
2194 begin
2195 pragma Assert (Nkind (CU) = N_Compilation_Unit);
2196
2197 case Nkind (N) is
2198
2199 -- If it is a subprogram body, process it if it has no
2200 -- separate spec.
2201
2202 -- If it's a package body, ignore it, unless it is a body
2203 -- created for an instance that is the main unit. In the case
2204 -- of subprograms, the body is the wrapper package. In case of
2205 -- a package, the original file carries the body, and the spec
2206 -- appears as a later entry in the units list.
2207
2208 -- Otherwise bodies appear in the list only because of inlining
2209 -- or instantiations, and they are processed only if relevant.
2210 -- The flag Withed_Body on a context clause indicates that a
2211 -- unit contains an instantiation that may be needed later,
2212 -- and therefore the body that contains the generic body (and
2213 -- its context) must be traversed immediately after the
2214 -- corresponding spec (see Do_Unit_And_Dependents).
2215
2216 -- The main unit itself is processed separately after all other
2217 -- specs, and relevant bodies are examined in Process_Main.
2218
2219 when N_Subprogram_Body =>
2220 if Acts_As_Spec (N) then
2221 Do_Unit_And_Dependents (CU, N);
2222 end if;
2223
2224 when N_Package_Body =>
2225 if CU = Main_CU
2226 and then Nkind (Original_Node (Unit (Main_CU))) in
2227 N_Generic_Instantiation
2228 and then Present (Spec_Lib_Unit (Main_CU))
2229 then
2230 Do_Unit_And_Dependents
2231 (Spec_Lib_Unit (Main_CU),
2232 Unit (Spec_Lib_Unit (Main_CU)));
2233 end if;
2234
2235 -- It is a spec, process it, and the units it depends on,
2236 -- unless it is a descendant of the main unit. This can happen
2237 -- when the body of a parent depends on some other descendant.
2238
2239 when N_Null_Statement =>
2240
2241 -- Ignore an ignored ghost unit
2242
2243 pragma Assert (Is_Ignored_Ghost_Node (Original_Node (N)));
2244 null;
2245
2246 when others =>
2247
2248 -- Skip spec of main unit for now, we want to process it
2249 -- after all other specs.
2250
2251 if Nkind (Unit (CU)) = N_Package_Declaration
2252 and then Body_Lib_Unit (CU) = Main_CU
2253 and then CU /= Main_CU
2254 then
2255 Spec_CU := CU;
2256 else
2257 Par := Scope (Defining_Entity (Unit (CU)));
2258
2259 if Is_Child_Unit (Defining_Entity (Unit (CU))) then
2260 while Present (Par)
2261 and then Par /= Standard_Standard
2262 and then Par /= Cunit_Entity (Main_Unit)
2263 loop
2264 Par := Scope (Par);
2265 end loop;
2266 end if;
2267
2268 if Par /= Cunit_Entity (Main_Unit) then
2269 Do_Unit_And_Dependents (CU, N);
2270 end if;
2271 end if;
2272 end case;
2273 end;
2274
2275 Next_Elmt (Cur);
2276 end loop;
2277
2278 -- Now process main package spec if skipped
2279
2280 if Present (Spec_CU) then
2281 Do_Unit_And_Dependents (Spec_CU, Unit (Spec_CU));
2282 end if;
2283
2284 -- Now process package bodies on which main depends, followed by bodies
2285 -- of parents, if present, and finally main itself.
2286
2287 if not Done (Main_Unit) then
2288 Do_Main := True;
2289
2290 Process_Main : declare
2291 Parent_CU : Node_Id;
2292 Body_CU : Node_Id;
2293 Body_U : Unit_Number_Type;
2294 Child : Entity_Id;
2295
2296 function Is_Subunit_Of_Main (U : Node_Id) return Boolean;
2297 -- If the main unit has subunits, their context may include
2298 -- bodies that are needed in the body of main. We must examine
2299 -- the context of the subunits, which are otherwise not made
2300 -- explicit in the main unit.
2301
2302 ------------------------
2303 -- Is_Subunit_Of_Main --
2304 ------------------------
2305
2306 function Is_Subunit_Of_Main (U : Node_Id) return Boolean is
2307 Lib : Node_Id;
2308
2309 begin
2310 if Present (U) and then Nkind (Unit (U)) = N_Subunit then
2311 Lib := Subunit_Parent (U);
2312 return Lib = Main_CU or else Is_Subunit_Of_Main (Lib);
2313 else
2314 return False;
2315 end if;
2316 end Is_Subunit_Of_Main;
2317
2318 -- Start of processing for Process_Main
2319
2320 begin
2321 Process_Bodies_In_Context (Main_CU);
2322
2323 for Unit_Num in Done'Range loop
2324 if Is_Subunit_Of_Main (Cunit (Unit_Num)) then
2325 Process_Bodies_In_Context (Cunit (Unit_Num));
2326 end if;
2327 end loop;
2328
2329 -- If the main unit is a child unit, parent bodies may be present
2330 -- because they export instances or inlined subprograms. Check for
2331 -- presence of these, which are not present in context clauses.
2332 -- Note that if the parents are instances, their bodies have been
2333 -- processed before the main spec, because they may be needed
2334 -- therein, so the following loop only affects non-instances.
2335
2336 if Is_Child_Unit (Cunit_Entity (Main_Unit)) then
2337 Child := Cunit_Entity (Main_Unit);
2338 while Is_Child_Unit (Child) loop
2339 Parent_CU :=
2340 Cunit (Get_Cunit_Entity_Unit_Number (Scope (Child)));
2341 Body_CU := Body_Lib_Unit (Parent_CU);
2342
2343 if Present (Body_CU)
2344 and then not Seen (Get_Cunit_Unit_Number (Body_CU))
2345 and then not Depends_On_Main (Body_CU)
2346 then
2347 Body_U := Get_Cunit_Unit_Number (Body_CU);
2348 Seen (Body_U) := True;
2349 Do_Action (Body_CU, Unit (Body_CU));
2350 Done (Body_U) := True;
2351 end if;
2352
2353 Child := Scope (Child);
2354 end loop;
2355 end if;
2356
2357 Do_Action (Main_CU, Unit (Main_CU));
2358 Done (Main_Unit) := True;
2359 end Process_Main;
2360 end if;
2361
2362 if Debug_Unit_Walk then
2363 if Done /= (Done'Range => True) then
2364 Write_Eol;
2365 Write_Line ("Ignored units:");
2366
2367 Indent;
2368
2369 for Unit_Num in Done'Range loop
2370 if not Done (Unit_Num) then
2371
2372 -- Units with configuration pragmas (.ads files) have empty
2373 -- compilation-unit nodes; skip printing info about them.
2374
2375 if Present (Cunit (Unit_Num)) then
2376 Write_Unit_Info
2377 (Unit_Num, Unit (Cunit (Unit_Num)), Withs => True);
2378 end if;
2379 end if;
2380 end loop;
2381
2382 Outdent;
2383 end if;
2384 end if;
2385
2386 pragma Assert (Done (Main_Unit));
2387
2388 if Debug_Unit_Walk then
2389 Outdent;
2390 Write_Line ("end Walk_Library_Items.");
2391 end if;
2392 end Walk_Library_Items;
2393
2394 ----------------
2395 -- Walk_Withs --
2396 ----------------
2397
2398 procedure Walk_Withs (CU : Node_Id; Include_Limited : Boolean) is
2399 pragma Assert (Nkind (CU) = N_Compilation_Unit);
2400 pragma Assert (Nkind (Unit (CU)) /= N_Subunit);
2401
2402 procedure Walk_Immediate is new Walk_Withs_Immediate (Action);
2403
2404 begin
2405 -- First walk the withs immediately on the library item
2406
2407 Walk_Immediate (CU, Include_Limited);
2408
2409 -- For a body, we must also check for any subunits which belong to it
2410 -- and which have context clauses of their own, since these with'ed
2411 -- units are part of its own dependencies.
2412
2413 if Nkind (Unit (CU)) in N_Lib_Unit_Body then
2414 for S in Main_Unit .. Last_Unit loop
2415
2416 -- We are only interested in subunits. For preproc. data and def.
2417 -- files, Cunit is Empty, so we need to test that first.
2418
2419 if Cunit (S) /= Empty
2420 and then Nkind (Unit (Cunit (S))) = N_Subunit
2421 then
2422 declare
2423 Pnode : Node_Id;
2424
2425 begin
2426 Pnode := Subunit_Parent (Cunit (S));
2427
2428 -- In -gnatc mode, the errors in the subunits will not have
2429 -- been recorded, but the analysis of the subunit may have
2430 -- failed, so just quit.
2431
2432 if No (Pnode) then
2433 exit;
2434 end if;
2435
2436 -- Find ultimate parent of the subunit
2437
2438 while Nkind (Unit (Pnode)) = N_Subunit loop
2439 Pnode := Subunit_Parent (Pnode);
2440 end loop;
2441
2442 -- See if it belongs to current unit, and if so, include its
2443 -- with_clauses. Do not process main unit prematurely.
2444
2445 if Pnode = CU and then CU /= Cunit (Main_Unit) then
2446 Walk_Immediate (Cunit (S), Include_Limited);
2447 end if;
2448 end;
2449 end if;
2450 end loop;
2451 end if;
2452 end Walk_Withs;
2453
2454 --------------------------
2455 -- Walk_Withs_Immediate --
2456 --------------------------
2457
2458 procedure Walk_Withs_Immediate (CU : Node_Id; Include_Limited : Boolean) is
2459 pragma Assert (Nkind (CU) = N_Compilation_Unit);
2460
2461 Context_Item : Node_Id;
2462 Lib_Unit : Node_Id;
2463
2464 begin
2465 Context_Item := First (Context_Items (CU));
2466 while Present (Context_Item) loop
2467 if Nkind (Context_Item) = N_With_Clause
2468 and then (Include_Limited
2469 or else not Limited_Present (Context_Item))
2470 then
2471 Lib_Unit := Withed_Lib_Unit (Context_Item);
2472 Action (Lib_Unit);
2473 end if;
2474
2475 Next (Context_Item);
2476 end loop;
2477 end Walk_Withs_Immediate;
2478
2479 end Sem;