ada: Simplify "not Present" with "No"
[official-gcc.git] / gcc / ada / ghost.adb
blob6cf87ce29b158f39704309a185a23fe4f9137932
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G H O S T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2014-2023, 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 ------------------------------------------------------------------------------
26 with Alloc;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Sem; use Sem;
37 with Sem_Aux; use Sem_Aux;
38 with Sem_Disp; use Sem_Disp;
39 with Sem_Eval; use Sem_Eval;
40 with Sem_Prag; use Sem_Prag;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Sinfo.Nodes; use Sinfo.Nodes;
45 with Sinfo.Utils; use Sinfo.Utils;
46 with Snames; use Snames;
47 with Table;
49 package body Ghost is
51 ---------------------
52 -- Data strictures --
53 ---------------------
55 -- The following table contains all ignored Ghost nodes that must be
56 -- eliminated from the tree by routine Remove_Ignored_Ghost_Code.
58 package Ignored_Ghost_Nodes is new Table.Table (
59 Table_Component_Type => Node_Id,
60 Table_Index_Type => Int,
61 Table_Low_Bound => 0,
62 Table_Initial => Alloc.Ignored_Ghost_Nodes_Initial,
63 Table_Increment => Alloc.Ignored_Ghost_Nodes_Increment,
64 Table_Name => "Ignored_Ghost_Nodes");
66 -----------------------
67 -- Local subprograms --
68 -----------------------
70 function Whole_Object_Ref (Ref : Node_Id) return Node_Id;
71 -- For a name that denotes an object, returns a name that denotes the whole
72 -- object, declared by an object declaration, formal parameter declaration,
73 -- etc. For example, for P.X.Comp (J), if P is a package X is a record
74 -- object, this returns P.X.
76 function Ghost_Entity (Ref : Node_Id) return Entity_Id;
77 pragma Inline (Ghost_Entity);
78 -- Obtain the entity of a Ghost entity from reference Ref. Return Empty if
79 -- no such entity exists.
81 procedure Install_Ghost_Mode (Mode : Ghost_Mode_Type);
82 pragma Inline (Install_Ghost_Mode);
83 -- Install Ghost mode Mode as the Ghost mode in effect
85 procedure Install_Ghost_Region (Mode : Name_Id; N : Node_Id);
86 pragma Inline (Install_Ghost_Region);
87 -- Install a Ghost region comprised of mode Mode and ignored region start
88 -- node N.
90 function Is_Subject_To_Ghost (N : Node_Id) return Boolean;
91 -- Determine whether declaration or body N is subject to aspect or pragma
92 -- Ghost. This routine must be used in cases where pragma Ghost has not
93 -- been analyzed yet, but the context needs to establish the "ghostness"
94 -- of N.
96 procedure Mark_Ghost_Declaration_Or_Body
97 (N : Node_Id;
98 Mode : Name_Id);
99 -- Mark the defining entity of declaration or body N as Ghost depending on
100 -- mode Mode. Mark all formals parameters when N denotes a subprogram or a
101 -- body.
103 procedure Record_Ignored_Ghost_Node (N : Node_Or_Entity_Id);
104 -- Save ignored Ghost node or entity N in table Ignored_Ghost_Nodes for
105 -- later elimination.
107 ----------------------------
108 -- Check_Ghost_Completion --
109 ----------------------------
111 procedure Check_Ghost_Completion
112 (Prev_Id : Entity_Id;
113 Compl_Id : Entity_Id)
115 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
117 begin
118 -- Nothing to do if one of the views is missing
120 if No (Prev_Id) or else No (Compl_Id) then
121 null;
123 -- The Ghost policy in effect at the point of declaration and at the
124 -- point of completion must match (SPARK RM 6.9(14)).
126 elsif Is_Checked_Ghost_Entity (Prev_Id)
127 and then Policy = Name_Ignore
128 then
129 Error_Msg_Sloc := Sloc (Compl_Id);
131 Error_Msg_N ("incompatible ghost policies in effect", Prev_Id);
132 Error_Msg_N ("\& declared with ghost policy `Check`", Prev_Id);
133 Error_Msg_N ("\& completed # with ghost policy `Ignore`", Prev_Id);
135 elsif Is_Ignored_Ghost_Entity (Prev_Id)
136 and then Policy = Name_Check
137 then
138 Error_Msg_Sloc := Sloc (Compl_Id);
140 Error_Msg_N ("incompatible ghost policies in effect", Prev_Id);
141 Error_Msg_N ("\& declared with ghost policy `Ignore`", Prev_Id);
142 Error_Msg_N ("\& completed # with ghost policy `Check`", Prev_Id);
143 end if;
144 end Check_Ghost_Completion;
146 -------------------------
147 -- Check_Ghost_Context --
148 -------------------------
150 procedure Check_Ghost_Context (Ghost_Id : Entity_Id; Ghost_Ref : Node_Id) is
151 procedure Check_Ghost_Policy (Id : Entity_Id; Ref : Node_Id);
152 -- Verify that the Ghost policy at the point of declaration of entity Id
153 -- matches the policy at the point of reference Ref. If this is not the
154 -- case emit an error at Ref.
156 function Is_OK_Ghost_Context (Context : Node_Id) return Boolean;
157 -- Determine whether node Context denotes a Ghost-friendly context where
158 -- a Ghost entity can safely reside (SPARK RM 6.9(10)).
160 function In_Aspect_Or_Pragma_Predicate (N : Node_Id) return Boolean;
161 -- Return True iff N is enclosed in an aspect or pragma Predicate
163 -------------------------
164 -- Is_OK_Ghost_Context --
165 -------------------------
167 function Is_OK_Ghost_Context (Context : Node_Id) return Boolean is
168 function Is_OK_Declaration (Decl : Node_Id) return Boolean;
169 -- Determine whether node Decl is a suitable context for a reference
170 -- to a Ghost entity. To qualify as such, Decl must either
172 -- * Define a Ghost entity
174 -- * Be subject to pragma Ghost
176 function Is_OK_Pragma (Prag : Node_Id) return Boolean;
177 -- Determine whether node Prag is a suitable context for a reference
178 -- to a Ghost entity. To qualify as such, Prag must either
180 -- * Be an assertion expression pragma
182 -- * Denote pragma Global, Depends, Initializes, Refined_Global,
183 -- Refined_Depends or Refined_State.
185 -- * Specify an aspect of a Ghost entity
187 -- * Contain a reference to a Ghost entity
189 function Is_OK_Statement (Stmt : Node_Id) return Boolean;
190 -- Determine whether node Stmt is a suitable context for a reference
191 -- to a Ghost entity. To qualify as such, Stmt must either
193 -- * Denote a procedure call to a Ghost procedure
195 -- * Denote an assignment statement whose target is Ghost
197 -----------------------
198 -- Is_OK_Declaration --
199 -----------------------
201 function Is_OK_Declaration (Decl : Node_Id) return Boolean is
202 function In_Subprogram_Body_Profile (N : Node_Id) return Boolean;
203 -- Determine whether node N appears in the profile of a subprogram
204 -- body.
206 --------------------------------
207 -- In_Subprogram_Body_Profile --
208 --------------------------------
210 function In_Subprogram_Body_Profile (N : Node_Id) return Boolean is
211 Spec : constant Node_Id := Parent (N);
213 begin
214 -- The node appears in a parameter specification in which case
215 -- it is either the parameter type or the default expression or
216 -- the node appears as the result definition of a function.
218 return
219 (Nkind (N) = N_Parameter_Specification
220 or else
221 (Nkind (Spec) = N_Function_Specification
222 and then N = Result_Definition (Spec)))
223 and then Nkind (Parent (Spec)) = N_Subprogram_Body;
224 end In_Subprogram_Body_Profile;
226 -- Local variables
228 Subp_Decl : Node_Id;
229 Subp_Id : Entity_Id;
231 -- Start of processing for Is_OK_Declaration
233 begin
234 if Is_Ghost_Declaration (Decl) then
235 return True;
237 -- Special cases
239 -- A reference to a Ghost entity may appear within the profile of
240 -- a subprogram body. This context is treated as suitable because
241 -- it duplicates the context of the corresponding spec. The real
242 -- check was already performed during the analysis of the spec.
244 elsif In_Subprogram_Body_Profile (Decl) then
245 return True;
247 -- A reference to a Ghost entity may appear within an expression
248 -- function which is still being analyzed. This context is treated
249 -- as suitable because it is not yet known whether the expression
250 -- function is an initial declaration or a completion. The real
251 -- check is performed when the expression function is expanded.
253 elsif Nkind (Decl) = N_Expression_Function
254 and then not Analyzed (Decl)
255 then
256 return True;
258 -- A reference to a Ghost entity may appear within the class-wide
259 -- precondition of a helper subprogram. This context is treated
260 -- as suitable because it was already verified when we were
261 -- analyzing the original class-wide precondition.
263 elsif Is_Subprogram (Current_Scope)
264 and then Present (Class_Preconditions_Subprogram (Current_Scope))
265 then
266 return True;
268 -- References to Ghost entities may be relocated in internally
269 -- generated bodies.
271 elsif Nkind (Decl) = N_Subprogram_Body
272 and then not Comes_From_Source (Decl)
273 then
274 Subp_Id := Corresponding_Spec (Decl);
276 if Present (Subp_Id) then
278 -- The context is the internally built _Wrapped_Statements
279 -- procedure, which is OK because the real check was done
280 -- before contract expansion activities.
282 if Chars (Subp_Id) = Name_uWrapped_Statements then
283 return True;
285 -- The context is the internally built predicate function,
286 -- which is OK because the real check was done before the
287 -- predicate function was generated.
289 elsif Is_Predicate_Function (Subp_Id) then
290 return True;
292 else
293 Subp_Decl :=
294 Original_Node (Unit_Declaration_Node (Subp_Id));
296 -- The original context is an expression function that
297 -- has been split into a spec and a body. The context is
298 -- OK as long as the initial declaration is Ghost.
300 if Nkind (Subp_Decl) = N_Expression_Function then
301 return Is_Ghost_Declaration (Subp_Decl);
302 end if;
303 end if;
305 -- Otherwise this is either an internal body or an internal
306 -- completion. Both are OK because the real check was done
307 -- before expansion activities.
309 else
310 return True;
311 end if;
312 end if;
314 return False;
315 end Is_OK_Declaration;
317 ------------------
318 -- Is_OK_Pragma --
319 ------------------
321 function Is_OK_Pragma (Prag : Node_Id) return Boolean is
322 procedure Check_Policies (Prag_Nam : Name_Id);
323 -- Verify that the Ghost policy in effect is the same as the
324 -- assertion policy for pragma name Prag_Nam. Emit an error if
325 -- this is not the case.
327 --------------------
328 -- Check_Policies --
329 --------------------
331 procedure Check_Policies (Prag_Nam : Name_Id) is
332 AP : constant Name_Id := Check_Kind (Prag_Nam);
333 GP : constant Name_Id := Policy_In_Effect (Name_Ghost);
335 begin
336 -- If the Ghost policy in effect at the point of a Ghost entity
337 -- reference is Ignore, then the assertion policy of the pragma
338 -- must be Ignore (SPARK RM 6.9(18)).
340 if GP = Name_Ignore and then AP /= Name_Ignore then
341 Error_Msg_N
342 ("incompatible ghost policies in effect",
343 Ghost_Ref);
344 Error_Msg_NE
345 ("\ghost entity & has policy `Ignore`",
346 Ghost_Ref, Ghost_Id);
348 Error_Msg_Name_1 := AP;
349 Error_Msg_N
350 ("\assertion expression has policy %", Ghost_Ref);
351 end if;
352 end Check_Policies;
354 -- Local variables
356 Prag_Id : Pragma_Id;
357 Prag_Nam : Name_Id;
359 -- Start of processing for Is_OK_Pragma
361 begin
362 if Nkind (Prag) = N_Pragma then
363 Prag_Id := Get_Pragma_Id (Prag);
364 Prag_Nam := Original_Aspect_Pragma_Name (Prag);
366 -- A pragma that applies to a Ghost construct or specifies an
367 -- aspect of a Ghost entity is a Ghost pragma (SPARK RM 6.9(3))
369 if Is_Ghost_Pragma (Prag) then
370 return True;
372 -- A pragma may not be analyzed, so that its Ghost status is
373 -- not determined yet, but it is guaranteed to be Ghost when
374 -- referencing a Ghost entity.
376 elsif Prag_Nam in Name_Annotate
377 | Name_Compile_Time_Error
378 | Name_Compile_Time_Warning
379 | Name_Unreferenced
380 then
381 return True;
383 -- An assertion expression pragma is Ghost when it contains a
384 -- reference to a Ghost entity (SPARK RM 6.9(10)), except for
385 -- predicate pragmas (SPARK RM 6.9(11)).
387 elsif Assertion_Expression_Pragma (Prag_Id)
388 and then Prag_Id /= Pragma_Predicate
389 then
390 -- Ensure that the assertion policy and the Ghost policy are
391 -- compatible (SPARK RM 6.9(18)).
393 Check_Policies (Prag_Nam);
394 return True;
396 -- Several pragmas that may apply to a non-Ghost entity are
397 -- treated as Ghost when they contain a reference to a Ghost
398 -- entity (SPARK RM 6.9(11)).
400 elsif Prag_Nam in Name_Global
401 | Name_Depends
402 | Name_Initializes
403 | Name_Refined_Global
404 | Name_Refined_Depends
405 | Name_Refined_State
406 then
407 return True;
408 end if;
409 end if;
411 return False;
412 end Is_OK_Pragma;
414 ---------------------
415 -- Is_OK_Statement --
416 ---------------------
418 function Is_OK_Statement (Stmt : Node_Id) return Boolean is
419 begin
420 -- An assignment statement is Ghost when the target is a Ghost
421 -- entity.
423 if Nkind (Stmt) = N_Assignment_Statement then
424 return Is_Ghost_Assignment (Stmt);
426 -- A procedure call is Ghost when it calls a Ghost procedure
428 elsif Nkind (Stmt) = N_Procedure_Call_Statement then
429 return Is_Ghost_Procedure_Call (Stmt);
431 -- Special cases
433 -- An if statement is a suitable context for a Ghost entity if it
434 -- is the byproduct of assertion expression expansion. Note that
435 -- the assertion expression may not be related to a Ghost entity,
436 -- but it may still contain references to Ghost entities.
438 elsif Nkind (Stmt) = N_If_Statement
439 and then Comes_From_Check_Or_Contract (Stmt)
440 then
441 return True;
442 end if;
444 return False;
445 end Is_OK_Statement;
447 -- Local variables
449 Par : Node_Id;
451 -- Start of processing for Is_OK_Ghost_Context
453 begin
454 -- The context is Ghost when it appears within a Ghost package or
455 -- subprogram.
457 if Ghost_Mode > None then
458 return True;
460 -- Routine Expand_Record_Extension creates a parent subtype without
461 -- inserting it into the tree. There is no good way of recognizing
462 -- this special case as there is no parent. Try to approximate the
463 -- context.
465 elsif No (Parent (Context)) and then Is_Tagged_Type (Ghost_Id) then
466 return True;
468 -- Otherwise climb the parent chain looking for a suitable Ghost
469 -- context.
471 else
472 Par := Context;
473 while Present (Par) loop
474 if Is_Ignored_Ghost_Node (Par) then
475 return True;
477 -- It is not possible to check correct use of Ghost entities
478 -- in generic instantiations until after the generic has been
479 -- resolved. Postpone that verification to after resolution.
481 elsif Nkind (Par) = N_Generic_Association then
482 return True;
484 -- A reference to a Ghost entity can appear within an aspect
485 -- specification (SPARK RM 6.9(10)). The precise checking will
486 -- occur when analyzing the corresponding pragma. We make an
487 -- exception for predicate aspects other than Ghost_Predicate
488 -- that only allow referencing a Ghost entity when the
489 -- corresponding type declaration is Ghost (SPARK RM 6.9(11)).
491 elsif Nkind (Par) = N_Aspect_Specification
492 and then
493 (Get_Aspect_Id (Par) = Aspect_Ghost_Predicate
494 or else not Same_Aspect
495 (Get_Aspect_Id (Par), Aspect_Predicate))
496 then
497 return True;
499 -- A Ghost type may be referenced in a use or use_type clause
500 -- (SPARK RM 6.9(10)).
502 elsif Present (Parent (Par))
503 and then Nkind (Parent (Par)) in N_Use_Package_Clause
504 | N_Use_Type_Clause
505 then
506 return True;
508 -- The context is an attribute definition clause for a Ghost
509 -- entity.
511 elsif Nkind (Parent (Par)) = N_Attribute_Definition_Clause
512 and then Par = Name (Parent (Par))
513 then
514 return True;
516 -- The context is the instantiation or renaming of a Ghost
517 -- entity.
519 elsif Nkind (Parent (Par)) in N_Generic_Instantiation
520 | N_Renaming_Declaration
521 | N_Generic_Renaming_Declaration
522 and then Par = Name (Parent (Par))
523 then
524 return True;
526 -- In the case of the renaming of a ghost object, the type
527 -- itself may be ghost.
529 elsif Nkind (Parent (Par)) = N_Object_Renaming_Declaration
530 and then (Par = Subtype_Mark (Parent (Par))
531 or else Par = Access_Definition (Parent (Par)))
532 then
533 return True;
535 elsif Is_OK_Declaration (Par) then
536 return True;
538 elsif Is_OK_Pragma (Par) then
539 return True;
541 elsif Is_OK_Statement (Par) then
542 return True;
544 -- Prevent the search from going too far
546 elsif Is_Body_Or_Package_Declaration (Par) then
547 exit;
548 end if;
550 Par := Parent (Par);
551 end loop;
553 -- The expansion of assertion expression pragmas and attribute Old
554 -- may cause a legal Ghost entity reference to become illegal due
555 -- to node relocation. Check the In_Assertion_Expr counter as last
556 -- resort to try and infer the original legal context.
558 if In_Assertion_Expr > 0 then
559 return True;
561 -- Otherwise the context is not suitable for a reference to a
562 -- Ghost entity.
564 else
565 return False;
566 end if;
567 end if;
568 end Is_OK_Ghost_Context;
570 ------------------------
571 -- Check_Ghost_Policy --
572 ------------------------
574 procedure Check_Ghost_Policy (Id : Entity_Id; Ref : Node_Id) is
575 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
577 begin
578 -- The Ghost policy in effect a the point of declaration and at the
579 -- point of use must match (SPARK RM 6.9(13)).
581 if Is_Checked_Ghost_Entity (Id)
582 and then Policy = Name_Ignore
583 and then Known_To_Be_Assigned (Ref)
584 then
585 Error_Msg_Sloc := Sloc (Ref);
587 Error_Msg_N ("incompatible ghost policies in effect", Ref);
588 Error_Msg_NE ("\& declared with ghost policy `Check`", Ref, Id);
589 Error_Msg_NE ("\& used # with ghost policy `Ignore`", Ref, Id);
591 elsif Is_Ignored_Ghost_Entity (Id) and then Policy = Name_Check then
592 Error_Msg_Sloc := Sloc (Ref);
594 Error_Msg_N ("incompatible ghost policies in effect", Ref);
595 Error_Msg_NE ("\& declared with ghost policy `Ignore`", Ref, Id);
596 Error_Msg_NE ("\& used # with ghost policy `Check`", Ref, Id);
597 end if;
598 end Check_Ghost_Policy;
600 -----------------------------------
601 -- In_Aspect_Or_Pragma_Predicate --
602 -----------------------------------
604 function In_Aspect_Or_Pragma_Predicate (N : Node_Id) return Boolean is
605 Par : Node_Id := N;
606 begin
607 while Present (Par) loop
608 if Nkind (Par) = N_Pragma
609 and then Get_Pragma_Id (Par) = Pragma_Predicate
610 then
611 return True;
613 elsif Nkind (Par) = N_Aspect_Specification
614 and then Same_Aspect (Get_Aspect_Id (Par), Aspect_Predicate)
615 then
616 return True;
618 -- Stop the search when it's clear it cannot be inside an aspect
619 -- or pragma.
621 elsif Is_Declaration (Par)
622 or else Is_Statement (Par)
623 or else Is_Body (Par)
624 then
625 return False;
626 end if;
628 Par := Parent (Par);
629 end loop;
631 return False;
632 end In_Aspect_Or_Pragma_Predicate;
634 -- Start of processing for Check_Ghost_Context
636 begin
637 -- Class-wide pre/postconditions of ignored pragmas are preanalyzed
638 -- to report errors on wrong conditions; however, ignored pragmas may
639 -- also have references to ghost entities and we must disable checking
640 -- their context to avoid reporting spurious errors.
642 if Inside_Class_Condition_Preanalysis then
643 return;
644 end if;
646 -- When assertions are enabled, compiler generates code for ghost
647 -- entities, that is not subject to Ghost policy.
649 if not Comes_From_Source (Ghost_Ref) then
650 return;
651 end if;
653 -- Once it has been established that the reference to the Ghost entity
654 -- is within a suitable context, ensure that the policy at the point of
655 -- declaration and at the point of use match.
657 if Is_OK_Ghost_Context (Ghost_Ref) then
658 if Present (Ghost_Id) then
659 Check_Ghost_Policy (Ghost_Id, Ghost_Ref);
660 end if;
662 -- Otherwise the Ghost entity appears in a non-Ghost context and affects
663 -- its behavior or value (SPARK RM 6.9(10,11)).
665 else
666 Error_Msg_N ("ghost entity cannot appear in this context", Ghost_Ref);
668 -- When the Ghost entity appears in a pragma Predicate, explain the
669 -- reason for this being illegal, and suggest a fix instead.
671 if In_Aspect_Or_Pragma_Predicate (Ghost_Ref) then
672 Error_Msg_N
673 ("\as predicates are checked in membership tests, "
674 & "the type and its predicate must be both ghost",
675 Ghost_Ref);
676 Error_Msg_N
677 ("\either make the type ghost "
678 & "or use a Ghost_Predicate "
679 & "or use a type invariant on a private type", Ghost_Ref);
680 end if;
681 end if;
682 end Check_Ghost_Context;
684 ------------------------------------------------
685 -- Check_Ghost_Context_In_Generic_Association --
686 ------------------------------------------------
688 procedure Check_Ghost_Context_In_Generic_Association
689 (Actual : Node_Id;
690 Formal : Entity_Id)
692 function Emit_Error_On_Ghost_Reference
693 (N : Node_Id)
694 return Traverse_Result;
695 -- Determine wether N denotes a reference to a ghost entity, and if so
696 -- issue an error.
698 -----------------------------------
699 -- Emit_Error_On_Ghost_Reference --
700 -----------------------------------
702 function Emit_Error_On_Ghost_Reference
703 (N : Node_Id)
704 return Traverse_Result
706 begin
707 if Is_Entity_Name (N)
708 and then Present (Entity (N))
709 and then Is_Ghost_Entity (Entity (N))
710 then
711 Error_Msg_N ("ghost entity cannot appear in this context", N);
712 Error_Msg_Sloc := Sloc (Formal);
713 Error_Msg_NE ("\formal & was not declared as ghost #", N, Formal);
714 return Abandon;
715 end if;
717 return OK;
718 end Emit_Error_On_Ghost_Reference;
720 procedure Check_Ghost_References is
721 new Traverse_Proc (Emit_Error_On_Ghost_Reference);
723 -- Start of processing for Check_Ghost_Context_In_Generic_Association
725 begin
726 -- The context is ghost when it appears within a Ghost package or
727 -- subprogram.
729 if Ghost_Mode > None then
730 return;
732 -- The context is ghost if Formal is explicitly marked as ghost
734 elsif Is_Ghost_Entity (Formal) then
735 return;
737 else
738 Check_Ghost_References (Actual);
739 end if;
740 end Check_Ghost_Context_In_Generic_Association;
742 ---------------------------------------------
743 -- Check_Ghost_Formal_Procedure_Or_Package --
744 ---------------------------------------------
746 procedure Check_Ghost_Formal_Procedure_Or_Package
747 (N : Node_Id;
748 Actual : Entity_Id;
749 Formal : Entity_Id;
750 Is_Default : Boolean := False)
752 begin
753 if not Is_Ghost_Entity (Formal) then
754 return;
755 end if;
757 if Present (Actual) and then Is_Ghost_Entity (Actual) then
758 return;
759 end if;
761 if Is_Default then
762 Error_Msg_N ("ghost procedure expected as default", N);
763 Error_Msg_NE ("\formal & is declared as ghost", N, Formal);
765 else
766 if Ekind (Formal) = E_Procedure then
767 Error_Msg_N ("ghost procedure expected for actual", N);
768 else
769 Error_Msg_N ("ghost package expected for actual", N);
770 end if;
772 Error_Msg_Sloc := Sloc (Formal);
773 Error_Msg_NE ("\formal & was declared as ghost #", N, Formal);
774 end if;
775 end Check_Ghost_Formal_Procedure_Or_Package;
777 ---------------------------------
778 -- Check_Ghost_Formal_Variable --
779 ---------------------------------
781 procedure Check_Ghost_Formal_Variable
782 (Actual : Node_Id;
783 Formal : Entity_Id;
784 Is_Default : Boolean := False)
786 Actual_Obj : constant Entity_Id := Get_Enclosing_Deep_Object (Actual);
787 begin
788 if not Is_Ghost_Entity (Formal) then
789 return;
790 end if;
792 if No (Actual_Obj)
793 or else not Is_Ghost_Entity (Actual_Obj)
794 then
795 if Is_Default then
796 Error_Msg_N ("ghost object expected as default", Actual);
797 Error_Msg_NE ("\formal & is declared as ghost", Actual, Formal);
798 else
799 Error_Msg_N ("ghost object expected for mutable actual", Actual);
800 Error_Msg_Sloc := Sloc (Formal);
801 Error_Msg_NE ("\formal & was declared as ghost #", Actual, Formal);
802 end if;
803 end if;
804 end Check_Ghost_Formal_Variable;
806 ----------------------------
807 -- Check_Ghost_Overriding --
808 ----------------------------
810 procedure Check_Ghost_Overriding
811 (Subp : Entity_Id;
812 Overridden_Subp : Entity_Id)
814 Deriv_Typ : Entity_Id;
815 Over_Subp : Entity_Id;
817 begin
818 if Present (Subp) and then Present (Overridden_Subp) then
819 Over_Subp := Ultimate_Alias (Overridden_Subp);
820 Deriv_Typ := Find_Dispatching_Type (Subp);
822 -- A Ghost primitive of a non-Ghost type extension cannot override an
823 -- inherited non-Ghost primitive (SPARK RM 6.9(8)).
825 if Is_Ghost_Entity (Subp)
826 and then Present (Deriv_Typ)
827 and then not Is_Ghost_Entity (Deriv_Typ)
828 and then not Is_Ghost_Entity (Over_Subp)
829 and then not Is_Abstract_Subprogram (Over_Subp)
830 then
831 Error_Msg_N ("incompatible overriding in effect", Subp);
833 Error_Msg_Sloc := Sloc (Over_Subp);
834 Error_Msg_N ("\& declared # as non-ghost subprogram", Subp);
836 Error_Msg_Sloc := Sloc (Subp);
837 Error_Msg_N ("\overridden # with ghost subprogram", Subp);
838 end if;
840 -- A non-Ghost primitive of a type extension cannot override an
841 -- inherited Ghost primitive (SPARK RM 6.9(8)).
843 if Is_Ghost_Entity (Over_Subp)
844 and then not Is_Ghost_Entity (Subp)
845 and then not Is_Abstract_Subprogram (Subp)
846 then
847 Error_Msg_N ("incompatible overriding in effect", Subp);
849 Error_Msg_Sloc := Sloc (Over_Subp);
850 Error_Msg_N ("\& declared # as ghost subprogram", Subp);
852 Error_Msg_Sloc := Sloc (Subp);
853 Error_Msg_N ("\overridden # with non-ghost subprogram", Subp);
854 end if;
856 if Present (Deriv_Typ)
857 and then not Is_Ignored_Ghost_Entity (Deriv_Typ)
858 then
859 -- When a tagged type is either non-Ghost or checked Ghost and
860 -- one of its primitives overrides an inherited operation, the
861 -- overridden operation of the ancestor type must be ignored Ghost
862 -- if the primitive is ignored Ghost (SPARK RM 6.9(17)).
864 if Is_Ignored_Ghost_Entity (Subp) then
866 -- Both the parent subprogram and overriding subprogram are
867 -- ignored Ghost.
869 if Is_Ignored_Ghost_Entity (Over_Subp) then
870 null;
872 -- The parent subprogram carries policy Check
874 elsif Is_Checked_Ghost_Entity (Over_Subp) then
875 Error_Msg_N
876 ("incompatible ghost policies in effect", Subp);
878 Error_Msg_Sloc := Sloc (Over_Subp);
879 Error_Msg_N
880 ("\& declared # with ghost policy `Check`", Subp);
882 Error_Msg_Sloc := Sloc (Subp);
883 Error_Msg_N
884 ("\overridden # with ghost policy `Ignore`", Subp);
886 -- The parent subprogram is non-Ghost
888 else
889 Error_Msg_N
890 ("incompatible ghost policies in effect", Subp);
892 Error_Msg_Sloc := Sloc (Over_Subp);
893 Error_Msg_N ("\& declared # as non-ghost subprogram", Subp);
895 Error_Msg_Sloc := Sloc (Subp);
896 Error_Msg_N
897 ("\overridden # with ghost policy `Ignore`", Subp);
898 end if;
900 -- When a tagged type is either non-Ghost or checked Ghost and
901 -- one of its primitives overrides an inherited operation, the
902 -- the primitive of the tagged type must be ignored Ghost if the
903 -- overridden operation is ignored Ghost (SPARK RM 6.9(17)).
905 elsif Is_Ignored_Ghost_Entity (Over_Subp) then
907 -- Both the parent subprogram and the overriding subprogram are
908 -- ignored Ghost.
910 if Is_Ignored_Ghost_Entity (Subp) then
911 null;
913 -- The overriding subprogram carries policy Check
915 elsif Is_Checked_Ghost_Entity (Subp) then
916 Error_Msg_N
917 ("incompatible ghost policies in effect", Subp);
919 Error_Msg_Sloc := Sloc (Over_Subp);
920 Error_Msg_N
921 ("\& declared # with ghost policy `Ignore`", Subp);
923 Error_Msg_Sloc := Sloc (Subp);
924 Error_Msg_N
925 ("\overridden # with Ghost policy `Check`", Subp);
927 -- The overriding subprogram is non-Ghost
929 else
930 Error_Msg_N
931 ("incompatible ghost policies in effect", Subp);
933 Error_Msg_Sloc := Sloc (Over_Subp);
934 Error_Msg_N
935 ("\& declared # with ghost policy `Ignore`", Subp);
937 Error_Msg_Sloc := Sloc (Subp);
938 Error_Msg_N
939 ("\overridden # with non-ghost subprogram", Subp);
940 end if;
941 end if;
942 end if;
943 end if;
944 end Check_Ghost_Overriding;
946 ---------------------------
947 -- Check_Ghost_Primitive --
948 ---------------------------
950 procedure Check_Ghost_Primitive (Prim : Entity_Id; Typ : Entity_Id) is
951 begin
952 -- The Ghost policy in effect at the point of declaration of a primitive
953 -- operation and a tagged type must match (SPARK RM 6.9(16)).
955 if Is_Tagged_Type (Typ) then
956 if Is_Checked_Ghost_Entity (Prim)
957 and then Is_Ignored_Ghost_Entity (Typ)
958 then
959 Error_Msg_N ("incompatible ghost policies in effect", Prim);
961 Error_Msg_Sloc := Sloc (Typ);
962 Error_Msg_NE
963 ("\tagged type & declared # with ghost policy `Ignore`",
964 Prim, Typ);
966 Error_Msg_Sloc := Sloc (Prim);
967 Error_Msg_N
968 ("\primitive subprogram & declared # with ghost policy `Check`",
969 Prim);
971 elsif Is_Ignored_Ghost_Entity (Prim)
972 and then Is_Checked_Ghost_Entity (Typ)
973 then
974 Error_Msg_N ("incompatible ghost policies in effect", Prim);
976 Error_Msg_Sloc := Sloc (Typ);
977 Error_Msg_NE
978 ("\tagged type & declared # with ghost policy `Check`",
979 Prim, Typ);
981 Error_Msg_Sloc := Sloc (Prim);
982 Error_Msg_N
983 ("\primitive subprogram & declared # with ghost policy `Ignore`",
984 Prim);
985 end if;
986 end if;
987 end Check_Ghost_Primitive;
989 ----------------------------
990 -- Check_Ghost_Refinement --
991 ----------------------------
993 procedure Check_Ghost_Refinement
994 (State : Node_Id;
995 State_Id : Entity_Id;
996 Constit : Node_Id;
997 Constit_Id : Entity_Id)
999 begin
1000 if Is_Ghost_Entity (State_Id) then
1001 if Is_Ghost_Entity (Constit_Id) then
1003 -- The Ghost policy in effect at the point of abstract state
1004 -- declaration and constituent must match (SPARK RM 6.9(15)).
1006 if Is_Checked_Ghost_Entity (State_Id)
1007 and then Is_Ignored_Ghost_Entity (Constit_Id)
1008 then
1009 Error_Msg_Sloc := Sloc (Constit);
1010 SPARK_Msg_N ("incompatible ghost policies in effect", State);
1012 SPARK_Msg_NE
1013 ("\abstract state & declared with ghost policy `Check`",
1014 State, State_Id);
1015 SPARK_Msg_NE
1016 ("\constituent & declared # with ghost policy `Ignore`",
1017 State, Constit_Id);
1019 elsif Is_Ignored_Ghost_Entity (State_Id)
1020 and then Is_Checked_Ghost_Entity (Constit_Id)
1021 then
1022 Error_Msg_Sloc := Sloc (Constit);
1023 SPARK_Msg_N ("incompatible ghost policies in effect", State);
1025 SPARK_Msg_NE
1026 ("\abstract state & declared with ghost policy `Ignore`",
1027 State, State_Id);
1028 SPARK_Msg_NE
1029 ("\constituent & declared # with ghost policy `Check`",
1030 State, Constit_Id);
1031 end if;
1033 -- A constituent of a Ghost abstract state must be a Ghost entity
1034 -- (SPARK RM 7.2.2(12)).
1036 else
1037 SPARK_Msg_NE
1038 ("constituent of ghost state & must be ghost",
1039 Constit, State_Id);
1040 end if;
1041 end if;
1042 end Check_Ghost_Refinement;
1044 ----------------------
1045 -- Check_Ghost_Type --
1046 ----------------------
1048 procedure Check_Ghost_Type (Typ : Entity_Id) is
1049 Conc_Typ : Entity_Id;
1050 Full_Typ : Entity_Id;
1052 begin
1053 if Is_Ghost_Entity (Typ) then
1054 Conc_Typ := Empty;
1055 Full_Typ := Typ;
1057 if Is_Single_Concurrent_Type (Typ) then
1058 Conc_Typ := Anonymous_Object (Typ);
1059 Full_Typ := Conc_Typ;
1061 elsif Is_Concurrent_Type (Typ) then
1062 Conc_Typ := Typ;
1063 end if;
1065 -- A Ghost type cannot be concurrent (SPARK RM 6.9(19)). Verify this
1066 -- legality rule first to give a finer-grained diagnostic.
1068 if Present (Conc_Typ) then
1069 Error_Msg_N ("ghost type & cannot be concurrent", Conc_Typ);
1070 end if;
1072 -- A Ghost type cannot be effectively volatile (SPARK RM 6.9(7))
1074 if Is_Effectively_Volatile (Full_Typ) then
1075 Error_Msg_N ("ghost type & cannot be volatile", Full_Typ);
1076 end if;
1077 end if;
1078 end Check_Ghost_Type;
1080 ------------------
1081 -- Ghost_Entity --
1082 ------------------
1084 function Ghost_Entity (Ref : Node_Id) return Entity_Id is
1085 Obj_Ref : constant Node_Id := Ultimate_Prefix (Ref);
1087 begin
1088 -- When the reference denotes a subcomponent, recover the related whole
1089 -- object (SPARK RM 6.9(1)).
1091 if Is_Entity_Name (Obj_Ref) then
1092 return Entity (Obj_Ref);
1094 -- Otherwise the reference cannot possibly denote a Ghost entity
1096 else
1097 return Empty;
1098 end if;
1099 end Ghost_Entity;
1101 --------------------------------
1102 -- Implements_Ghost_Interface --
1103 --------------------------------
1105 function Implements_Ghost_Interface (Typ : Entity_Id) return Boolean is
1106 Iface_Elmt : Elmt_Id;
1108 begin
1109 -- Traverse the list of interfaces looking for a Ghost interface
1111 if Is_Tagged_Type (Typ) and then Present (Interfaces (Typ)) then
1112 Iface_Elmt := First_Elmt (Interfaces (Typ));
1113 while Present (Iface_Elmt) loop
1114 if Is_Ghost_Entity (Node (Iface_Elmt)) then
1115 return True;
1116 end if;
1118 Next_Elmt (Iface_Elmt);
1119 end loop;
1120 end if;
1122 return False;
1123 end Implements_Ghost_Interface;
1125 ----------------
1126 -- Initialize --
1127 ----------------
1129 procedure Initialize is
1130 begin
1131 Ignored_Ghost_Nodes.Init;
1133 -- Set the soft link which enables Atree.Mark_New_Ghost_Node to record
1134 -- an ignored Ghost node or entity.
1136 Set_Ignored_Ghost_Recording_Proc (Record_Ignored_Ghost_Node'Access);
1137 end Initialize;
1139 ------------------------
1140 -- Install_Ghost_Mode --
1141 ------------------------
1143 procedure Install_Ghost_Mode (Mode : Ghost_Mode_Type) is
1144 begin
1145 Install_Ghost_Region (Mode, Empty);
1146 end Install_Ghost_Mode;
1148 --------------------------
1149 -- Install_Ghost_Region --
1150 --------------------------
1152 procedure Install_Ghost_Region (Mode : Ghost_Mode_Type; N : Node_Id) is
1153 begin
1154 -- The context is already within an ignored Ghost region. Maintain the
1155 -- start of the outermost ignored Ghost region.
1157 if Present (Ignored_Ghost_Region) then
1158 null;
1160 -- The current region is the outermost ignored Ghost region. Save its
1161 -- starting node.
1163 elsif Present (N) and then Mode = Ignore then
1164 Ignored_Ghost_Region := N;
1166 -- Otherwise the current region is not ignored, nothing to save
1168 else
1169 Ignored_Ghost_Region := Empty;
1170 end if;
1172 Ghost_Mode := Mode;
1173 end Install_Ghost_Region;
1175 procedure Install_Ghost_Region (Mode : Name_Id; N : Node_Id) is
1176 begin
1177 Install_Ghost_Region (Name_To_Ghost_Mode (Mode), N);
1178 end Install_Ghost_Region;
1180 -------------------------
1181 -- Is_Ghost_Assignment --
1182 -------------------------
1184 function Is_Ghost_Assignment (N : Node_Id) return Boolean is
1185 Id : Entity_Id;
1187 begin
1188 -- An assignment statement is Ghost when its target denotes a Ghost
1189 -- entity.
1191 if Nkind (N) = N_Assignment_Statement then
1192 Id := Ghost_Entity (Name (N));
1194 return Present (Id) and then Is_Ghost_Entity (Id);
1195 end if;
1197 return False;
1198 end Is_Ghost_Assignment;
1200 ----------------------------------
1201 -- Is_Ghost_Attribute_Reference --
1202 ----------------------------------
1204 function Is_Ghost_Attribute_Reference (N : Node_Id) return Boolean is
1205 begin
1206 return Nkind (N) = N_Attribute_Reference
1207 and then Attribute_Name (N) = Name_Initialized;
1208 end Is_Ghost_Attribute_Reference;
1210 --------------------------
1211 -- Is_Ghost_Declaration --
1212 --------------------------
1214 function Is_Ghost_Declaration (N : Node_Id) return Boolean is
1215 Id : Entity_Id;
1217 begin
1218 -- A declaration is Ghost when it elaborates a Ghost entity or is
1219 -- subject to pragma Ghost.
1221 if Is_Declaration (N) then
1222 Id := Defining_Entity (N);
1224 return Is_Ghost_Entity (Id) or else Is_Subject_To_Ghost (N);
1225 end if;
1227 return False;
1228 end Is_Ghost_Declaration;
1230 ---------------------
1231 -- Is_Ghost_Pragma --
1232 ---------------------
1234 function Is_Ghost_Pragma (N : Node_Id) return Boolean is
1235 begin
1236 return Is_Checked_Ghost_Pragma (N) or else Is_Ignored_Ghost_Pragma (N);
1237 end Is_Ghost_Pragma;
1239 -----------------------------
1240 -- Is_Ghost_Procedure_Call --
1241 -----------------------------
1243 function Is_Ghost_Procedure_Call (N : Node_Id) return Boolean is
1244 Id : Entity_Id;
1246 begin
1247 -- A procedure call is Ghost when it invokes a Ghost procedure
1249 if Nkind (N) = N_Procedure_Call_Statement then
1250 Id := Ghost_Entity (Name (N));
1252 return Present (Id) and then Is_Ghost_Entity (Id);
1253 end if;
1255 return False;
1256 end Is_Ghost_Procedure_Call;
1258 ---------------------------
1259 -- Is_Ignored_Ghost_Unit --
1260 ---------------------------
1262 function Is_Ignored_Ghost_Unit (N : Node_Id) return Boolean is
1263 function Ultimate_Original_Node (Nod : Node_Id) return Node_Id;
1264 -- Obtain the original node of arbitrary node Nod following a potential
1265 -- chain of rewritings.
1267 ----------------------------
1268 -- Ultimate_Original_Node --
1269 ----------------------------
1271 function Ultimate_Original_Node (Nod : Node_Id) return Node_Id is
1272 Res : Node_Id := Nod;
1273 begin
1274 while Is_Rewrite_Substitution (Res) loop
1275 Res := Original_Node (Res);
1276 end loop;
1278 return Res;
1279 end Ultimate_Original_Node;
1281 -- Start of processing for Is_Ignored_Ghost_Unit
1283 begin
1284 -- Inspect the original node of the unit in case removal of ignored
1285 -- Ghost code has already taken place.
1287 return
1288 Nkind (N) = N_Compilation_Unit
1289 and then Is_Ignored_Ghost_Entity
1290 (Defining_Entity (Ultimate_Original_Node (Unit (N))));
1291 end Is_Ignored_Ghost_Unit;
1293 -------------------------
1294 -- Is_Subject_To_Ghost --
1295 -------------------------
1297 function Is_Subject_To_Ghost (N : Node_Id) return Boolean is
1298 function Enables_Ghostness (Arg : Node_Id) return Boolean;
1299 -- Determine whether aspect or pragma argument Arg enables "ghostness"
1301 -----------------------
1302 -- Enables_Ghostness --
1303 -----------------------
1305 function Enables_Ghostness (Arg : Node_Id) return Boolean is
1306 Expr : Node_Id;
1308 begin
1309 Expr := Arg;
1311 if Nkind (Expr) = N_Pragma_Argument_Association then
1312 Expr := Get_Pragma_Arg (Expr);
1313 end if;
1315 -- Determine whether the expression of the aspect or pragma is static
1316 -- and denotes True.
1318 if Present (Expr) then
1319 Preanalyze_And_Resolve (Expr);
1321 return
1322 Is_OK_Static_Expression (Expr)
1323 and then Is_True (Expr_Value (Expr));
1325 -- Otherwise Ghost defaults to True
1327 else
1328 return True;
1329 end if;
1330 end Enables_Ghostness;
1332 -- Local variables
1334 Id : constant Entity_Id := Defining_Entity (N);
1335 Asp : Node_Id;
1336 Decl : Node_Id;
1337 Prev_Id : Entity_Id;
1339 -- Start of processing for Is_Subject_To_Ghost
1341 begin
1342 -- The related entity of the declaration has not been analyzed yet, do
1343 -- not inspect its attributes.
1345 if Ekind (Id) = E_Void then
1346 null;
1348 elsif Is_Ghost_Entity (Id) then
1349 return True;
1351 -- The completion of a type or a constant is not fully analyzed when the
1352 -- reference to the Ghost entity is resolved. Because the completion is
1353 -- not marked as Ghost yet, inspect the partial view.
1355 elsif Is_Record_Type (Id)
1356 or else Ekind (Id) = E_Constant
1357 or else (Nkind (N) = N_Object_Declaration
1358 and then Constant_Present (N))
1359 then
1360 Prev_Id := Incomplete_Or_Partial_View (Id);
1362 if Present (Prev_Id) and then Is_Ghost_Entity (Prev_Id) then
1363 return True;
1364 end if;
1365 end if;
1367 -- Examine the aspect specifications (if any) looking for aspect Ghost
1369 if Permits_Aspect_Specifications (N) then
1370 Asp := First (Aspect_Specifications (N));
1371 while Present (Asp) loop
1372 if Chars (Identifier (Asp)) = Name_Ghost then
1373 return Enables_Ghostness (Expression (Asp));
1374 end if;
1376 Next (Asp);
1377 end loop;
1378 end if;
1380 Decl := Empty;
1382 -- When the context is a [generic] package declaration, pragma Ghost
1383 -- resides in the visible declarations.
1385 if Nkind (N) in N_Generic_Package_Declaration | N_Package_Declaration
1386 then
1387 Decl := First (Visible_Declarations (Specification (N)));
1389 -- When the context is a package or a subprogram body, pragma Ghost
1390 -- resides in the declarative part.
1392 elsif Nkind (N) in N_Package_Body | N_Subprogram_Body then
1393 Decl := First (Declarations (N));
1395 -- Otherwise pragma Ghost appears in the declarations following N
1397 elsif Is_List_Member (N) then
1398 Decl := Next (N);
1399 end if;
1401 while Present (Decl) loop
1402 if Nkind (Decl) = N_Pragma
1403 and then Pragma_Name (Decl) = Name_Ghost
1404 then
1405 return
1406 Enables_Ghostness (First (Pragma_Argument_Associations (Decl)));
1408 -- A source construct ends the region where pragma Ghost may appear,
1409 -- stop the traversal. Check the original node as source constructs
1410 -- may be rewritten into something else by expansion.
1412 elsif Comes_From_Source (Original_Node (Decl)) then
1413 exit;
1414 end if;
1416 Next (Decl);
1417 end loop;
1419 return False;
1420 end Is_Subject_To_Ghost;
1422 ----------
1423 -- Lock --
1424 ----------
1426 procedure Lock is
1427 begin
1428 Ignored_Ghost_Nodes.Release;
1429 Ignored_Ghost_Nodes.Locked := True;
1430 end Lock;
1432 -----------------------------------
1433 -- Mark_And_Set_Ghost_Assignment --
1434 -----------------------------------
1436 procedure Mark_And_Set_Ghost_Assignment (N : Node_Id) is
1437 -- A ghost assignment is an assignment whose left-hand side denotes a
1438 -- ghost object. Subcomponents are not marked "ghost", so we need to
1439 -- find the containing "whole" object. So, for "P.X.Comp (J) := ...",
1440 -- where P is a package, X is a record, and Comp is an array, we need
1441 -- to check the ghost flags of X.
1443 Orig_Lhs : constant Node_Id := Name (N);
1444 begin
1445 -- Ghost assignments are irrelevant when the expander is inactive, and
1446 -- processing them in that mode can lead to spurious errors.
1448 if Expander_Active then
1449 -- Cases where full analysis is needed, involving array indexing
1450 -- which would otherwise be missing array-bounds checks:
1452 if not Analyzed (Orig_Lhs)
1453 and then
1454 ((Nkind (Orig_Lhs) = N_Indexed_Component
1455 and then Nkind (Prefix (Orig_Lhs)) = N_Selected_Component
1456 and then Nkind (Prefix (Prefix (Orig_Lhs))) =
1457 N_Indexed_Component)
1458 or else
1459 (Nkind (Orig_Lhs) = N_Selected_Component
1460 and then Nkind (Prefix (Orig_Lhs)) = N_Indexed_Component
1461 and then Nkind (Prefix (Prefix (Orig_Lhs))) =
1462 N_Selected_Component
1463 and then Nkind (Parent (N)) /= N_Loop_Statement))
1464 then
1465 Analyze (Orig_Lhs);
1466 end if;
1468 -- Make sure Lhs is at least preanalyzed, so we can tell whether
1469 -- it denotes a ghost variable. In some cases we need to do a full
1470 -- analysis, or else the back end gets confused. Note that in the
1471 -- preanalysis case, we are preanalyzing a copy of the left-hand
1472 -- side name, temporarily attached to the tree.
1474 declare
1475 Lhs : constant Node_Id :=
1476 (if Analyzed (Orig_Lhs) then Orig_Lhs
1477 else New_Copy_Tree (Orig_Lhs));
1478 begin
1479 if not Analyzed (Lhs) then
1480 Set_Name (N, Lhs);
1481 Set_Parent (Lhs, N);
1482 Preanalyze_Without_Errors (Lhs);
1483 Set_Name (N, Orig_Lhs);
1484 end if;
1486 declare
1487 Whole : constant Node_Id := Whole_Object_Ref (Lhs);
1488 Id : Entity_Id;
1489 begin
1490 if Is_Entity_Name (Whole) then
1491 Id := Entity (Whole);
1493 if Present (Id) then
1494 -- Left-hand side denotes a Checked ghost entity, so
1495 -- install the region.
1497 if Is_Checked_Ghost_Entity (Id) then
1498 Install_Ghost_Region (Check, N);
1500 -- Left-hand side denotes an Ignored ghost entity, so
1501 -- install the region, and mark the assignment statement
1502 -- as an ignored ghost assignment, so it will be removed
1503 -- later.
1505 elsif Is_Ignored_Ghost_Entity (Id) then
1506 Install_Ghost_Region (Ignore, N);
1507 Set_Is_Ignored_Ghost_Node (N);
1508 Record_Ignored_Ghost_Node (N);
1509 end if;
1510 end if;
1511 end if;
1512 end;
1513 end;
1514 end if;
1515 end Mark_And_Set_Ghost_Assignment;
1517 -----------------------------
1518 -- Mark_And_Set_Ghost_Body --
1519 -----------------------------
1521 procedure Mark_And_Set_Ghost_Body
1522 (N : Node_Id;
1523 Spec_Id : Entity_Id)
1525 Body_Id : constant Entity_Id := Defining_Entity (N);
1526 Policy : Name_Id := No_Name;
1528 begin
1529 -- A body becomes Ghost when it is subject to aspect or pragma Ghost
1531 if Is_Subject_To_Ghost (N) then
1532 Policy := Policy_In_Effect (Name_Ghost);
1534 -- A body declared within a Ghost region is automatically Ghost
1535 -- (SPARK RM 6.9(2)).
1537 elsif Ghost_Mode = Check then
1538 Policy := Name_Check;
1540 elsif Ghost_Mode = Ignore then
1541 Policy := Name_Ignore;
1543 -- Inherit the "ghostness" of the previous declaration when the body
1544 -- acts as a completion.
1546 elsif Present (Spec_Id) then
1547 if Is_Checked_Ghost_Entity (Spec_Id) then
1548 Policy := Name_Check;
1550 elsif Is_Ignored_Ghost_Entity (Spec_Id) then
1551 Policy := Name_Ignore;
1552 end if;
1553 end if;
1555 -- The Ghost policy in effect at the point of declaration and at the
1556 -- point of completion must match (SPARK RM 6.9(14)).
1558 Check_Ghost_Completion
1559 (Prev_Id => Spec_Id,
1560 Compl_Id => Body_Id);
1562 -- Mark the body as its formals as Ghost
1564 Mark_Ghost_Declaration_Or_Body (N, Policy);
1566 -- Install the appropriate Ghost region
1568 Install_Ghost_Region (Policy, N);
1569 end Mark_And_Set_Ghost_Body;
1571 -----------------------------------
1572 -- Mark_And_Set_Ghost_Completion --
1573 -----------------------------------
1575 procedure Mark_And_Set_Ghost_Completion
1576 (N : Node_Id;
1577 Prev_Id : Entity_Id)
1579 Compl_Id : constant Entity_Id := Defining_Entity (N);
1580 Policy : Name_Id := No_Name;
1582 begin
1583 -- A completion elaborated in a Ghost region is automatically Ghost
1584 -- (SPARK RM 6.9(2)).
1586 if Ghost_Mode = Check then
1587 Policy := Name_Check;
1589 elsif Ghost_Mode = Ignore then
1590 Policy := Name_Ignore;
1592 -- The completion becomes Ghost when its initial declaration is also
1593 -- Ghost.
1595 elsif Is_Checked_Ghost_Entity (Prev_Id) then
1596 Policy := Name_Check;
1598 elsif Is_Ignored_Ghost_Entity (Prev_Id) then
1599 Policy := Name_Ignore;
1600 end if;
1602 -- The Ghost policy in effect at the point of declaration and at the
1603 -- point of completion must match (SPARK RM 6.9(14)).
1605 Check_Ghost_Completion
1606 (Prev_Id => Prev_Id,
1607 Compl_Id => Compl_Id);
1609 -- Mark the completion as Ghost
1611 Mark_Ghost_Declaration_Or_Body (N, Policy);
1613 -- Install the appropriate Ghost region
1615 Install_Ghost_Region (Policy, N);
1616 end Mark_And_Set_Ghost_Completion;
1618 ------------------------------------
1619 -- Mark_And_Set_Ghost_Declaration --
1620 ------------------------------------
1622 procedure Mark_And_Set_Ghost_Declaration (N : Node_Id) is
1623 Par_Id : Entity_Id;
1624 Policy : Name_Id := No_Name;
1626 begin
1627 -- A declaration becomes Ghost when it is subject to aspect or pragma
1628 -- Ghost.
1630 if Is_Subject_To_Ghost (N) then
1631 Policy := Policy_In_Effect (Name_Ghost);
1633 -- A declaration elaborated in a Ghost region is automatically Ghost
1634 -- (SPARK RM 6.9(2)).
1636 elsif Ghost_Mode = Check then
1637 Policy := Name_Check;
1639 elsif Ghost_Mode = Ignore then
1640 Policy := Name_Ignore;
1642 -- A child package or subprogram declaration becomes Ghost when its
1643 -- parent is Ghost (SPARK RM 6.9(2)).
1645 elsif Nkind (N) in N_Generic_Function_Renaming_Declaration
1646 | N_Generic_Package_Declaration
1647 | N_Generic_Package_Renaming_Declaration
1648 | N_Generic_Procedure_Renaming_Declaration
1649 | N_Generic_Subprogram_Declaration
1650 | N_Package_Declaration
1651 | N_Package_Renaming_Declaration
1652 | N_Subprogram_Declaration
1653 | N_Subprogram_Renaming_Declaration
1654 and then Present (Parent_Spec (N))
1655 then
1656 Par_Id := Defining_Entity (Unit (Parent_Spec (N)));
1658 if Is_Checked_Ghost_Entity (Par_Id) then
1659 Policy := Name_Check;
1661 elsif Is_Ignored_Ghost_Entity (Par_Id) then
1662 Policy := Name_Ignore;
1663 end if;
1664 end if;
1666 -- Mark the declaration and its formals as Ghost
1668 Mark_Ghost_Declaration_Or_Body (N, Policy);
1670 -- Install the appropriate Ghost region
1672 Install_Ghost_Region (Policy, N);
1673 end Mark_And_Set_Ghost_Declaration;
1675 --------------------------------------
1676 -- Mark_And_Set_Ghost_Instantiation --
1677 --------------------------------------
1679 procedure Mark_And_Set_Ghost_Instantiation
1680 (N : Node_Id;
1681 Gen_Id : Entity_Id)
1683 procedure Check_Ghost_Actuals;
1684 -- Check the context of ghost actuals
1686 -------------------------
1687 -- Check_Ghost_Actuals --
1688 -------------------------
1690 procedure Check_Ghost_Actuals is
1691 Assoc : Node_Id := First (Generic_Associations (N));
1692 Act : Node_Id;
1694 begin
1695 while Present (Assoc) loop
1696 if Nkind (Assoc) /= N_Others_Choice then
1697 Act := Explicit_Generic_Actual_Parameter (Assoc);
1699 -- Within a nested instantiation, a defaulted actual is an
1700 -- empty association, so nothing to check.
1702 if No (Act) then
1703 null;
1705 elsif Comes_From_Source (Act)
1706 and then Nkind (Act) in N_Has_Etype
1707 and then Present (Etype (Act))
1708 and then Is_Ghost_Entity (Etype (Act))
1709 then
1710 Check_Ghost_Context (Etype (Act), Act);
1711 end if;
1712 end if;
1714 Next (Assoc);
1715 end loop;
1716 end Check_Ghost_Actuals;
1718 -- Local variables
1720 Policy : Name_Id := No_Name;
1722 begin
1723 -- An instantiation becomes Ghost when it is subject to pragma Ghost
1725 if Is_Subject_To_Ghost (N) then
1726 Policy := Policy_In_Effect (Name_Ghost);
1728 -- An instantiation declaration within a Ghost region is automatically
1729 -- Ghost (SPARK RM 6.9(2)).
1731 elsif Ghost_Mode = Check then
1732 Policy := Name_Check;
1734 elsif Ghost_Mode = Ignore then
1735 Policy := Name_Ignore;
1737 -- Inherit the "ghostness" of the generic unit
1739 elsif Is_Checked_Ghost_Entity (Gen_Id) then
1740 Policy := Name_Check;
1742 elsif Is_Ignored_Ghost_Entity (Gen_Id) then
1743 Policy := Name_Ignore;
1744 end if;
1746 -- Mark the instantiation as Ghost
1748 Mark_Ghost_Declaration_Or_Body (N, Policy);
1750 -- Install the appropriate Ghost region
1752 Install_Ghost_Region (Policy, N);
1754 -- Check Ghost actuals. Given that this routine is unconditionally
1755 -- invoked with subprogram and package instantiations, this check
1756 -- verifies the context of all the ghost entities passed in generic
1757 -- instantiations.
1759 Check_Ghost_Actuals;
1760 end Mark_And_Set_Ghost_Instantiation;
1762 ---------------------------------------
1763 -- Mark_And_Set_Ghost_Procedure_Call --
1764 ---------------------------------------
1766 procedure Mark_And_Set_Ghost_Procedure_Call (N : Node_Id) is
1767 Id : Entity_Id;
1769 begin
1770 -- A procedure call becomes Ghost when the procedure being invoked is
1771 -- Ghost. Install the Ghost mode of the procedure.
1773 Id := Ghost_Entity (Name (N));
1775 if Present (Id) then
1776 if Is_Checked_Ghost_Entity (Id) then
1777 Install_Ghost_Region (Check, N);
1779 elsif Is_Ignored_Ghost_Entity (Id) then
1780 Install_Ghost_Region (Ignore, N);
1782 Set_Is_Ignored_Ghost_Node (N);
1783 Record_Ignored_Ghost_Node (N);
1784 end if;
1785 end if;
1786 end Mark_And_Set_Ghost_Procedure_Call;
1788 -----------------------
1789 -- Mark_Ghost_Clause --
1790 -----------------------
1792 procedure Mark_Ghost_Clause (N : Node_Id) is
1793 Nam : Node_Id := Empty;
1795 begin
1796 if Nkind (N) = N_Use_Package_Clause then
1797 Nam := Name (N);
1799 elsif Nkind (N) = N_Use_Type_Clause then
1800 Nam := Subtype_Mark (N);
1802 elsif Nkind (N) = N_With_Clause then
1803 Nam := Name (N);
1804 end if;
1806 if Present (Nam)
1807 and then Is_Entity_Name (Nam)
1808 and then Present (Entity (Nam))
1809 and then Is_Ignored_Ghost_Entity (Entity (Nam))
1810 then
1811 Set_Is_Ignored_Ghost_Node (N);
1812 Record_Ignored_Ghost_Node (N);
1813 end if;
1814 end Mark_Ghost_Clause;
1816 ------------------------------------
1817 -- Mark_Ghost_Declaration_Or_Body --
1818 ------------------------------------
1820 procedure Mark_Ghost_Declaration_Or_Body
1821 (N : Node_Id;
1822 Mode : Name_Id)
1824 Id : constant Entity_Id := Defining_Entity (N);
1826 Mark_Formals : Boolean := False;
1827 Param : Node_Id;
1828 Param_Id : Entity_Id;
1830 begin
1831 -- Mark the related node and its entity
1833 if Mode = Name_Check then
1834 Mark_Formals := True;
1835 Set_Is_Checked_Ghost_Entity (Id);
1837 elsif Mode = Name_Ignore then
1838 Mark_Formals := True;
1839 Set_Is_Ignored_Ghost_Entity (Id);
1840 Set_Is_Ignored_Ghost_Node (N);
1841 Record_Ignored_Ghost_Node (N);
1842 end if;
1844 -- Mark all formal parameters when the related node denotes a subprogram
1845 -- or a body. The traversal is performed via the specification because
1846 -- the related subprogram or body may be unanalyzed.
1848 -- ??? could extra formal parameters cause a Ghost leak?
1850 if Mark_Formals
1851 and then Nkind (N) in N_Abstract_Subprogram_Declaration
1852 | N_Formal_Abstract_Subprogram_Declaration
1853 | N_Formal_Concrete_Subprogram_Declaration
1854 | N_Generic_Subprogram_Declaration
1855 | N_Subprogram_Body
1856 | N_Subprogram_Body_Stub
1857 | N_Subprogram_Declaration
1858 | N_Subprogram_Renaming_Declaration
1859 then
1860 Param := First (Parameter_Specifications (Specification (N)));
1861 while Present (Param) loop
1862 Param_Id := Defining_Entity (Param);
1864 if Mode = Name_Check then
1865 Set_Is_Checked_Ghost_Entity (Param_Id);
1867 elsif Mode = Name_Ignore then
1868 Set_Is_Ignored_Ghost_Entity (Param_Id);
1869 end if;
1871 Next (Param);
1872 end loop;
1873 end if;
1874 end Mark_Ghost_Declaration_Or_Body;
1876 -----------------------
1877 -- Mark_Ghost_Pragma --
1878 -----------------------
1880 procedure Mark_Ghost_Pragma
1881 (N : Node_Id;
1882 Id : Entity_Id)
1884 begin
1885 -- A pragma becomes Ghost when it encloses a Ghost entity or relates to
1886 -- a Ghost entity.
1888 if Is_Checked_Ghost_Entity (Id) then
1889 Mark_Ghost_Pragma (N, Check);
1891 elsif Is_Ignored_Ghost_Entity (Id) then
1892 Mark_Ghost_Pragma (N, Ignore);
1893 end if;
1894 end Mark_Ghost_Pragma;
1896 procedure Mark_Ghost_Pragma
1897 (N : Node_Id;
1898 Mode : Ghost_Mode_Type)
1900 begin
1901 if Mode = Check then
1902 Set_Is_Checked_Ghost_Pragma (N);
1904 else
1905 Set_Is_Ignored_Ghost_Pragma (N);
1906 Set_Is_Ignored_Ghost_Node (N);
1907 Record_Ignored_Ghost_Node (N);
1908 end if;
1909 end Mark_Ghost_Pragma;
1911 -------------------------
1912 -- Mark_Ghost_Renaming --
1913 -------------------------
1915 procedure Mark_Ghost_Renaming
1916 (N : Node_Id;
1917 Id : Entity_Id)
1919 Policy : Name_Id := No_Name;
1921 begin
1922 -- A renaming becomes Ghost when it renames a Ghost entity
1924 if Is_Checked_Ghost_Entity (Id) then
1925 Policy := Name_Check;
1927 elsif Is_Ignored_Ghost_Entity (Id) then
1928 Policy := Name_Ignore;
1929 end if;
1931 Mark_Ghost_Declaration_Or_Body (N, Policy);
1932 end Mark_Ghost_Renaming;
1934 ------------------------
1935 -- Name_To_Ghost_Mode --
1936 ------------------------
1938 function Name_To_Ghost_Mode (Mode : Name_Id) return Ghost_Mode_Type is
1939 begin
1940 if Mode = Name_Check then
1941 return Check;
1943 elsif Mode = Name_Ignore then
1944 return Ignore;
1946 -- Otherwise the mode must denote one of the following:
1948 -- * Disable indicates that the Ghost policy in effect is Disable
1950 -- * None or No_Name indicates that the associated construct is not
1951 -- subject to any Ghost annotation.
1953 else
1954 pragma Assert (Mode in Name_Disable | Name_None | No_Name);
1955 return None;
1956 end if;
1957 end Name_To_Ghost_Mode;
1959 -------------------------------
1960 -- Record_Ignored_Ghost_Node --
1961 -------------------------------
1963 procedure Record_Ignored_Ghost_Node (N : Node_Or_Entity_Id) is
1964 begin
1965 -- Save all "top level" ignored Ghost nodes which can be safely replaced
1966 -- with a null statement. Note that there is need to save other kinds of
1967 -- nodes because those will always be enclosed by some top level ignored
1968 -- Ghost node.
1970 if Is_Body (N)
1971 or else Is_Declaration (N)
1972 or else Nkind (N) in N_Generic_Instantiation
1973 | N_Push_Pop_xxx_Label
1974 | N_Raise_xxx_Error
1975 | N_Representation_Clause
1976 | N_Statement_Other_Than_Procedure_Call
1977 | N_Call_Marker
1978 | N_Freeze_Entity
1979 | N_Freeze_Generic_Entity
1980 | N_Itype_Reference
1981 | N_Pragma
1982 | N_Procedure_Call_Statement
1983 | N_Use_Package_Clause
1984 | N_Use_Type_Clause
1985 | N_Variable_Reference_Marker
1986 | N_With_Clause
1987 then
1988 -- Only ignored Ghost nodes must be recorded in the table
1990 pragma Assert (Is_Ignored_Ghost_Node (N));
1991 Ignored_Ghost_Nodes.Append (N);
1992 end if;
1993 end Record_Ignored_Ghost_Node;
1995 -------------------------------
1996 -- Remove_Ignored_Ghost_Code --
1997 -------------------------------
1999 procedure Remove_Ignored_Ghost_Code is
2000 procedure Remove_Ignored_Ghost_Node (N : Node_Id);
2001 -- Eliminate ignored Ghost node N from the tree
2003 -------------------------------
2004 -- Remove_Ignored_Ghost_Node --
2005 -------------------------------
2007 procedure Remove_Ignored_Ghost_Node (N : Node_Id) is
2008 begin
2009 -- The generation and processing of ignored Ghost nodes may cause the
2010 -- same node to be saved multiple times. Reducing the number of saves
2011 -- to one involves costly solutions such as a hash table or the use
2012 -- of a flag shared by all nodes. To solve this problem, the removal
2013 -- machinery allows for multiple saves, but does not eliminate a node
2014 -- which has already been eliminated.
2016 if Nkind (N) = N_Null_Statement then
2017 null;
2019 -- Otherwise the ignored Ghost node must be eliminated
2021 else
2022 -- Only ignored Ghost nodes must be eliminated from the tree
2024 pragma Assert (Is_Ignored_Ghost_Node (N));
2026 -- Eliminate the node by rewriting it into null. Another option
2027 -- is to remove it from the tree, however multiple corner cases
2028 -- emerge which have be dealt individually.
2030 Rewrite (N, Make_Null_Statement (Sloc (N)));
2032 -- Eliminate any aspects hanging off the ignored Ghost node
2034 Remove_Aspects (N);
2035 end if;
2036 end Remove_Ignored_Ghost_Node;
2038 -- Start of processing for Remove_Ignored_Ghost_Code
2040 begin
2041 for Index in Ignored_Ghost_Nodes.First .. Ignored_Ghost_Nodes.Last loop
2042 Remove_Ignored_Ghost_Node (Ignored_Ghost_Nodes.Table (Index));
2043 end loop;
2044 end Remove_Ignored_Ghost_Code;
2046 --------------------------
2047 -- Restore_Ghost_Region --
2048 --------------------------
2050 procedure Restore_Ghost_Region (Mode : Ghost_Mode_Type; N : Node_Id) is
2051 begin
2052 Ghost_Mode := Mode;
2053 Ignored_Ghost_Region := N;
2054 end Restore_Ghost_Region;
2056 --------------------
2057 -- Set_Ghost_Mode --
2058 --------------------
2060 procedure Set_Ghost_Mode (N : Node_Or_Entity_Id) is
2061 procedure Set_Ghost_Mode_From_Entity (Id : Entity_Id);
2062 -- Install the Ghost mode of entity Id
2064 --------------------------------
2065 -- Set_Ghost_Mode_From_Entity --
2066 --------------------------------
2068 procedure Set_Ghost_Mode_From_Entity (Id : Entity_Id) is
2069 begin
2070 if Is_Checked_Ghost_Entity (Id) then
2071 Install_Ghost_Mode (Check);
2072 elsif Is_Ignored_Ghost_Entity (Id) then
2073 Install_Ghost_Mode (Ignore);
2074 else
2075 Install_Ghost_Mode (None);
2076 end if;
2077 end Set_Ghost_Mode_From_Entity;
2079 -- Local variables
2081 Id : Entity_Id;
2083 -- Start of processing for Set_Ghost_Mode
2085 begin
2086 -- The Ghost mode of an assignment statement depends on the Ghost mode
2087 -- of the target.
2089 if Nkind (N) = N_Assignment_Statement then
2090 Id := Ghost_Entity (Name (N));
2092 if Present (Id) then
2093 Set_Ghost_Mode_From_Entity (Id);
2094 end if;
2096 -- The Ghost mode of a body or a declaration depends on the Ghost mode
2097 -- of its defining entity.
2099 elsif Is_Body (N) or else Is_Declaration (N) then
2100 Set_Ghost_Mode_From_Entity (Defining_Entity (N));
2102 -- The Ghost mode of an entity depends on the entity itself
2104 elsif Nkind (N) in N_Entity then
2105 Set_Ghost_Mode_From_Entity (N);
2107 -- The Ghost mode of a [generic] freeze node depends on the Ghost mode
2108 -- of the entity being frozen.
2110 elsif Nkind (N) in N_Freeze_Entity | N_Freeze_Generic_Entity then
2111 Set_Ghost_Mode_From_Entity (Entity (N));
2113 -- The Ghost mode of a pragma depends on the associated entity. The
2114 -- property is encoded in the pragma itself.
2116 elsif Nkind (N) = N_Pragma then
2117 if Is_Checked_Ghost_Pragma (N) then
2118 Install_Ghost_Mode (Check);
2119 elsif Is_Ignored_Ghost_Pragma (N) then
2120 Install_Ghost_Mode (Ignore);
2121 else
2122 Install_Ghost_Mode (None);
2123 end if;
2125 -- The Ghost mode of a procedure call depends on the Ghost mode of the
2126 -- procedure being invoked.
2128 elsif Nkind (N) = N_Procedure_Call_Statement then
2129 Id := Ghost_Entity (Name (N));
2131 if Present (Id) then
2132 Set_Ghost_Mode_From_Entity (Id);
2133 end if;
2134 end if;
2135 end Set_Ghost_Mode;
2137 -------------------------
2138 -- Set_Is_Ghost_Entity --
2139 -------------------------
2141 procedure Set_Is_Ghost_Entity (Id : Entity_Id) is
2142 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
2143 begin
2144 if Policy = Name_Check then
2145 Set_Is_Checked_Ghost_Entity (Id);
2146 elsif Policy = Name_Ignore then
2147 Set_Is_Ignored_Ghost_Entity (Id);
2148 end if;
2149 end Set_Is_Ghost_Entity;
2151 ----------------------
2152 -- Whole_Object_Ref --
2153 ----------------------
2155 function Whole_Object_Ref (Ref : Node_Id) return Node_Id is
2156 begin
2157 if Nkind (Ref) in N_Indexed_Component | N_Slice
2158 or else (Nkind (Ref) = N_Selected_Component
2159 and then Is_Object_Reference (Prefix (Ref)))
2160 then
2161 if Is_Access_Type (Etype (Prefix (Ref))) then
2162 return Ref;
2163 else
2164 return Whole_Object_Ref (Prefix (Ref));
2165 end if;
2166 else
2167 return Ref;
2168 end if;
2169 end Whole_Object_Ref;
2171 end Ghost;