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