* gcc.dg/Wtrampolines.c: XFAIL AIX.
[official-gcc.git] / gcc / ada / ghost.adb
blob8621aea1514509d30bb499caf2aecbc2f6c13eac
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G H O S T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2014-2016, 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; use Alloc;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Lib; use Lib;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Opt; use Opt;
37 with Sem; use Sem;
38 with Sem_Aux; use Sem_Aux;
39 with Sem_Disp; use Sem_Disp;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Prag; use Sem_Prag;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Table;
48 package body Ghost is
50 -- The following table contains the N_Compilation_Unit node for a unit that
51 -- is either subject to pragma Ghost with policy Ignore or contains ignored
52 -- Ghost code. The table is used in the removal of ignored Ghost code from
53 -- units.
55 package Ignored_Ghost_Units is new Table.Table (
56 Table_Component_Type => Node_Id,
57 Table_Index_Type => Int,
58 Table_Low_Bound => 0,
59 Table_Initial => Alloc.Ignored_Ghost_Units_Initial,
60 Table_Increment => Alloc.Ignored_Ghost_Units_Increment,
61 Table_Name => "Ignored_Ghost_Units");
63 -----------------------
64 -- Local Subprograms --
65 -----------------------
67 function Ghost_Entity (N : Node_Id) return Entity_Id;
68 -- Subsidiary to Check_Ghost_Context and Set_Ghost_Mode. Find the entity of
69 -- a reference to a Ghost entity. Return Empty if there is no such entity.
71 function Is_Subject_To_Ghost (N : Node_Id) return Boolean;
72 -- Subsidiary to routines Is_OK_xxx and Set_Ghost_Mode. Determine whether
73 -- declaration or body N is subject to aspect or pragma Ghost. Use this
74 -- routine in cases where [source] pragma Ghost has not been analyzed yet,
75 -- but the context needs to establish the "ghostness" of N.
77 procedure Propagate_Ignored_Ghost_Code (N : Node_Id);
78 -- Subsidiary to routines Mark_xxx_As_Ghost and Set_Ghost_Mode_From_xxx.
79 -- Signal all enclosing scopes that they now contain ignored Ghost code.
80 -- Add the compilation unit containing N to table Ignored_Ghost_Units for
81 -- post processing.
83 ----------------------------
84 -- Add_Ignored_Ghost_Unit --
85 ----------------------------
87 procedure Add_Ignored_Ghost_Unit (Unit : Node_Id) is
88 begin
89 pragma Assert (Nkind (Unit) = N_Compilation_Unit);
91 -- Avoid duplicates in the table as pruning the same unit more than once
92 -- is wasteful. Since ignored Ghost code tends to be grouped up, check
93 -- the contents of the table in reverse.
95 for Index in reverse Ignored_Ghost_Units.First ..
96 Ignored_Ghost_Units.Last
97 loop
98 -- If the unit is already present in the table, do not add it again
100 if Unit = Ignored_Ghost_Units.Table (Index) then
101 return;
102 end if;
103 end loop;
105 -- If we get here, then this is the first time the unit is being added
107 Ignored_Ghost_Units.Append (Unit);
108 end Add_Ignored_Ghost_Unit;
110 ----------------------------
111 -- Check_Ghost_Completion --
112 ----------------------------
114 procedure Check_Ghost_Completion
115 (Partial_View : Entity_Id;
116 Full_View : Entity_Id)
118 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
120 begin
121 -- The Ghost policy in effect at the point of declaration and at the
122 -- point of completion must match (SPARK RM 6.9(14)).
124 if Is_Checked_Ghost_Entity (Partial_View)
125 and then Policy = Name_Ignore
126 then
127 Error_Msg_Sloc := Sloc (Full_View);
129 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
130 Error_Msg_N ("\& declared with ghost policy `Check`", Partial_View);
131 Error_Msg_N ("\& completed # with ghost policy `Ignore`",
132 Partial_View);
134 elsif Is_Ignored_Ghost_Entity (Partial_View)
135 and then Policy = Name_Check
136 then
137 Error_Msg_Sloc := Sloc (Full_View);
139 Error_Msg_N ("incompatible ghost policies in effect", Partial_View);
140 Error_Msg_N ("\& declared with ghost policy `Ignore`", Partial_View);
141 Error_Msg_N ("\& completed # with ghost policy `Check`",
142 Partial_View);
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; Err_N : 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. If this is not the case
154 -- emit an error at Err_N.
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 -------------------------
161 -- Is_OK_Ghost_Context --
162 -------------------------
164 function Is_OK_Ghost_Context (Context : Node_Id) return Boolean is
165 function Is_OK_Declaration (Decl : Node_Id) return Boolean;
166 -- Determine whether node Decl is a suitable context for a reference
167 -- to a Ghost entity. To qualify as such, Decl must either
168 -- 1) Be subject to pragma Ghost
169 -- 2) Rename a Ghost entity
171 function Is_OK_Pragma (Prag : Node_Id) return Boolean;
172 -- Determine whether node Prag is a suitable context for a reference
173 -- to a Ghost entity. To qualify as such, Prag must either
174 -- 1) Be an assertion expression pragma
175 -- 2) Denote pragma Global, Depends, Initializes, Refined_Global,
176 -- Refined_Depends or Refined_State
177 -- 3) Specify an aspect of a Ghost entity
178 -- 4) Contain a reference to a Ghost entity
180 function Is_OK_Statement (Stmt : Node_Id) return Boolean;
181 -- Determine whether node Stmt is a suitable context for a reference
182 -- to a Ghost entity. To qualify as such, Stmt must either
183 -- 1) Denote a call to a Ghost procedure
184 -- 2) Denote an assignment statement whose target is Ghost
186 -----------------------
187 -- Is_OK_Declaration --
188 -----------------------
190 function Is_OK_Declaration (Decl : Node_Id) return Boolean is
191 function In_Subprogram_Body_Profile (N : Node_Id) return Boolean;
192 -- Determine whether node N appears in the profile of a subprogram
193 -- body.
195 function Is_Ghost_Renaming (Ren_Decl : Node_Id) return Boolean;
196 -- Determine whether node Ren_Decl denotes a renaming declaration
197 -- with a Ghost name.
199 --------------------------------
200 -- In_Subprogram_Body_Profile --
201 --------------------------------
203 function In_Subprogram_Body_Profile (N : Node_Id) return Boolean is
204 Spec : constant Node_Id := Parent (N);
206 begin
207 -- The node appears in a parameter specification in which case
208 -- it is either the parameter type or the default expression or
209 -- the node appears as the result definition of a function.
211 return
212 (Nkind (N) = N_Parameter_Specification
213 or else
214 (Nkind (Spec) = N_Function_Specification
215 and then N = Result_Definition (Spec)))
216 and then Nkind (Parent (Spec)) = N_Subprogram_Body;
217 end In_Subprogram_Body_Profile;
219 -----------------------
220 -- Is_Ghost_Renaming --
221 -----------------------
223 function Is_Ghost_Renaming (Ren_Decl : Node_Id) return Boolean is
224 Nam_Id : Entity_Id;
226 begin
227 if Is_Renaming_Declaration (Ren_Decl) then
228 Nam_Id := Ghost_Entity (Name (Ren_Decl));
230 return Present (Nam_Id) and then Is_Ghost_Entity (Nam_Id);
231 end if;
233 return False;
234 end Is_Ghost_Renaming;
236 -- Local variables
238 Subp_Decl : Node_Id;
239 Subp_Id : Entity_Id;
241 -- Start of processing for Is_OK_Declaration
243 begin
244 if Is_Declaration (Decl) then
246 -- A renaming declaration is Ghost when it renames a Ghost
247 -- entity.
249 if Is_Ghost_Renaming (Decl) then
250 return True;
252 -- The declaration may not have been analyzed yet, determine
253 -- whether it is subject to pragma Ghost.
255 elsif Is_Subject_To_Ghost (Decl) then
256 return True;
257 end if;
259 -- Special cases
261 -- A reference to a Ghost entity may appear within the profile of
262 -- a subprogram body. This context is treated as suitable because
263 -- it duplicates the context of the corresponding spec. The real
264 -- check was already performed during the analysis of the spec.
266 elsif In_Subprogram_Body_Profile (Decl) then
267 return True;
269 -- A reference to a Ghost entity may appear within an expression
270 -- function which is still being analyzed. This context is treated
271 -- as suitable because it is not yet known whether the expression
272 -- function is an initial declaration or a completion. The real
273 -- check is performed when the expression function is expanded.
275 elsif Nkind (Decl) = N_Expression_Function
276 and then not Analyzed (Decl)
277 then
278 return True;
280 -- References to Ghost entities may be relocated in internally
281 -- generated bodies.
283 elsif Nkind (Decl) = N_Subprogram_Body
284 and then not Comes_From_Source (Decl)
285 then
286 Subp_Id := Corresponding_Spec (Decl);
288 if Present (Subp_Id) then
290 -- The context is the internally built _Postconditions
291 -- procedure, which is OK because the real check was done
292 -- before expansion activities.
294 if Chars (Subp_Id) = Name_uPostconditions then
295 return True;
297 else
298 Subp_Decl :=
299 Original_Node (Unit_Declaration_Node (Subp_Id));
301 -- The original context is an expression function that
302 -- has been split into a spec and a body. The context is
303 -- OK as long as the initial declaration is Ghost.
305 if Nkind (Subp_Decl) = N_Expression_Function then
306 return Is_Subject_To_Ghost (Subp_Decl);
307 end if;
308 end if;
310 -- Otherwise this is either an internal body or an internal
311 -- completion. Both are OK because the real check was done
312 -- before expansion activities.
314 else
315 return True;
316 end if;
317 end if;
319 return False;
320 end Is_OK_Declaration;
322 ------------------
323 -- Is_OK_Pragma --
324 ------------------
326 function Is_OK_Pragma (Prag : Node_Id) return Boolean is
327 procedure Check_Policies (Prag_Nam : Name_Id);
328 -- Verify that the Ghost policy in effect is the same as the
329 -- assertion policy for pragma name Prag_Nam. Emit an error if
330 -- this is not the case.
332 --------------------
333 -- Check_Policies --
334 --------------------
336 procedure Check_Policies (Prag_Nam : Name_Id) is
337 AP : constant Name_Id := Check_Kind (Prag_Nam);
338 GP : constant Name_Id := Policy_In_Effect (Name_Ghost);
340 begin
341 -- If the Ghost policy in effect at the point of a Ghost entity
342 -- reference is Ignore, then the assertion policy of the pragma
343 -- must be Ignore (SPARK RM 6.9(18)).
345 if GP = Name_Ignore and then AP /= Name_Ignore then
346 Error_Msg_N
347 ("incompatible ghost policies in effect",
348 Ghost_Ref);
349 Error_Msg_NE
350 ("\ghost entity & has policy `Ignore`",
351 Ghost_Ref, Ghost_Id);
353 Error_Msg_Name_1 := AP;
354 Error_Msg_N
355 ("\assertion expression has policy %", Ghost_Ref);
356 end if;
357 end Check_Policies;
359 -- Local variables
361 Arg : Node_Id;
362 Arg_Id : Entity_Id;
363 Prag_Id : Pragma_Id;
364 Prag_Nam : Name_Id;
366 -- Start of processing for Is_OK_Pragma
368 begin
369 if Nkind (Prag) = N_Pragma then
370 Prag_Id := Get_Pragma_Id (Prag);
371 Prag_Nam := Original_Aspect_Pragma_Name (Prag);
373 -- A pragma that applies to a Ghost construct or specifies an
374 -- aspect of a Ghost entity is a Ghost pragma (SPARK RM 6.9(3))
376 if Is_Ghost_Pragma (Prag) then
377 return True;
379 -- An assertion expression pragma is Ghost when it contains a
380 -- reference to a Ghost entity (SPARK RM 6.9(10)).
382 elsif Assertion_Expression_Pragma (Prag_Id) then
384 -- Ensure that the assertion policy and the Ghost policy are
385 -- compatible (SPARK RM 6.9(18)).
387 Check_Policies (Prag_Nam);
388 return True;
390 -- Several pragmas that may apply to a non-Ghost entity are
391 -- treated as Ghost when they contain a reference to a Ghost
392 -- entity (SPARK RM 6.9(11)).
394 elsif Nam_In (Prag_Nam, Name_Global,
395 Name_Depends,
396 Name_Initializes,
397 Name_Refined_Global,
398 Name_Refined_Depends,
399 Name_Refined_State)
400 then
401 return True;
403 -- Otherwise a normal pragma is Ghost when it encloses a Ghost
404 -- name (SPARK RM 6.9(3)).
406 else
407 Arg := First (Pragma_Argument_Associations (Prag));
408 while Present (Arg) loop
409 Arg_Id := Ghost_Entity (Get_Pragma_Arg (Arg));
411 if Present (Arg_Id) and then Is_Ghost_Entity (Arg_Id) then
412 return True;
413 end if;
415 Next (Arg);
416 end loop;
417 end if;
418 end if;
420 return False;
421 end Is_OK_Pragma;
423 ---------------------
424 -- Is_OK_Statement --
425 ---------------------
427 function Is_OK_Statement (Stmt : Node_Id) return Boolean is
428 Nam_Id : Entity_Id;
430 begin
431 -- An assignment statement or a procedure call is Ghost when the
432 -- name denotes a Ghost entity.
434 if Nkind_In (Stmt, N_Assignment_Statement,
435 N_Procedure_Call_Statement)
436 then
437 Nam_Id := Ghost_Entity (Name (Stmt));
439 return Present (Nam_Id) and then Is_Ghost_Entity (Nam_Id);
441 -- Special cases
443 -- An if statement is a suitable context for a Ghost entity if it
444 -- is the byproduct of assertion expression expansion. Note that
445 -- the assertion expression may not be related to a Ghost entity,
446 -- but it may still contain references to Ghost entities.
448 elsif Nkind (Stmt) = N_If_Statement
449 and then Nkind (Original_Node (Stmt)) = N_Pragma
450 and then Assertion_Expression_Pragma
451 (Get_Pragma_Id (Original_Node (Stmt)))
452 then
453 return True;
454 end if;
456 return False;
457 end Is_OK_Statement;
459 -- Local variables
461 Par : Node_Id;
463 -- Start of processing for Is_OK_Ghost_Context
465 begin
466 -- The context is Ghost when it appears within a Ghost package or
467 -- subprogram.
469 if Ghost_Mode > None then
470 return True;
472 -- A Ghost type may be referenced in a use_type clause
473 -- (SPARK RM 6.9.10).
475 elsif Present (Parent (Context))
476 and then Nkind (Parent (Context)) = N_Use_Type_Clause
477 then
478 return True;
480 -- Routine Expand_Record_Extension creates a parent subtype without
481 -- inserting it into the tree. There is no good way of recognizing
482 -- this special case as there is no parent. Try to approximate the
483 -- context.
485 elsif No (Parent (Context)) and then Is_Tagged_Type (Ghost_Id) then
486 return True;
488 -- Otherwise climb the parent chain looking for a suitable Ghost
489 -- context.
491 else
492 Par := Context;
493 while Present (Par) loop
494 if Is_Ignored_Ghost_Node (Par) then
495 return True;
497 -- A reference to a Ghost entity can appear within an aspect
498 -- specification (SPARK RM 6.9(10)).
500 elsif Nkind (Par) = N_Aspect_Specification then
501 return True;
503 elsif Is_OK_Declaration (Par) then
504 return True;
506 elsif Is_OK_Pragma (Par) then
507 return True;
509 elsif Is_OK_Statement (Par) then
510 return True;
512 -- Prevent the search from going too far
514 elsif Is_Body_Or_Package_Declaration (Par) then
515 exit;
516 end if;
518 Par := Parent (Par);
519 end loop;
521 -- The expansion of assertion expression pragmas and attribute Old
522 -- may cause a legal Ghost entity reference to become illegal due
523 -- to node relocation. Check the In_Assertion_Expr counter as last
524 -- resort to try and infer the original legal context.
526 if In_Assertion_Expr > 0 then
527 return True;
529 -- Otherwise the context is not suitable for a reference to a
530 -- Ghost entity.
532 else
533 return False;
534 end if;
535 end if;
536 end Is_OK_Ghost_Context;
538 ------------------------
539 -- Check_Ghost_Policy --
540 ------------------------
542 procedure Check_Ghost_Policy (Id : Entity_Id; Err_N : Node_Id) is
543 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
545 begin
546 -- The Ghost policy in effect a the point of declaration and at the
547 -- point of use must match (SPARK RM 6.9(13)).
549 if Is_Checked_Ghost_Entity (Id) and then Policy = Name_Ignore then
550 Error_Msg_Sloc := Sloc (Err_N);
552 Error_Msg_N ("incompatible ghost policies in effect", Err_N);
553 Error_Msg_NE ("\& declared with ghost policy `Check`", Err_N, Id);
554 Error_Msg_NE ("\& used # with ghost policy `Ignore`", Err_N, Id);
556 elsif Is_Ignored_Ghost_Entity (Id) and then Policy = Name_Check then
557 Error_Msg_Sloc := Sloc (Err_N);
559 Error_Msg_N ("incompatible ghost policies in effect", Err_N);
560 Error_Msg_NE ("\& declared with ghost policy `Ignore`", Err_N, Id);
561 Error_Msg_NE ("\& used # with ghost policy `Check`", Err_N, Id);
562 end if;
563 end Check_Ghost_Policy;
565 -- Start of processing for Check_Ghost_Context
567 begin
568 -- Once it has been established that the reference to the Ghost entity
569 -- is within a suitable context, ensure that the policy at the point of
570 -- declaration and at the point of use match.
572 if Is_OK_Ghost_Context (Ghost_Ref) then
573 Check_Ghost_Policy (Ghost_Id, Ghost_Ref);
575 -- Otherwise the Ghost entity appears in a non-Ghost context and affects
576 -- its behavior or value (SPARK RM 6.9(11,12)).
578 else
579 Error_Msg_N ("ghost entity cannot appear in this context", Ghost_Ref);
580 end if;
581 end Check_Ghost_Context;
583 ----------------------------
584 -- Check_Ghost_Overriding --
585 ----------------------------
587 procedure Check_Ghost_Overriding
588 (Subp : Entity_Id;
589 Overridden_Subp : Entity_Id)
591 Deriv_Typ : Entity_Id;
592 Over_Subp : Entity_Id;
594 begin
595 if Present (Subp) and then Present (Overridden_Subp) then
596 Over_Subp := Ultimate_Alias (Overridden_Subp);
597 Deriv_Typ := Find_Dispatching_Type (Subp);
599 -- A Ghost primitive of a non-Ghost type extension cannot override an
600 -- inherited non-Ghost primitive (SPARK RM 6.9(8)).
602 if Is_Ghost_Entity (Subp)
603 and then Present (Deriv_Typ)
604 and then not Is_Ghost_Entity (Deriv_Typ)
605 and then not Is_Ghost_Entity (Over_Subp)
606 and then not Is_Abstract_Subprogram (Over_Subp)
607 then
608 Error_Msg_N ("incompatible overriding in effect", Subp);
610 Error_Msg_Sloc := Sloc (Over_Subp);
611 Error_Msg_N ("\& declared # as non-ghost subprogram", Subp);
613 Error_Msg_Sloc := Sloc (Subp);
614 Error_Msg_N ("\overridden # with ghost subprogram", Subp);
615 end if;
617 -- A non-Ghost primitive of a type extension cannot override an
618 -- inherited Ghost primitive (SPARK RM 6.9(8)).
620 if Is_Ghost_Entity (Over_Subp)
621 and then not Is_Ghost_Entity (Subp)
622 and then not Is_Abstract_Subprogram (Subp)
623 then
624 Error_Msg_N ("incompatible overriding in effect", Subp);
626 Error_Msg_Sloc := Sloc (Over_Subp);
627 Error_Msg_N ("\& declared # as ghost subprogram", Subp);
629 Error_Msg_Sloc := Sloc (Subp);
630 Error_Msg_N ("\overridden # with non-ghost subprogram", Subp);
631 end if;
633 if Present (Deriv_Typ)
634 and then not Is_Ignored_Ghost_Entity (Deriv_Typ)
635 then
636 -- When a tagged type is either non-Ghost or checked Ghost and
637 -- one of its primitives overrides an inherited operation, the
638 -- overridden operation of the ancestor type must be ignored Ghost
639 -- if the primitive is ignored Ghost (SPARK RM 6.9(17)).
641 if Is_Ignored_Ghost_Entity (Subp) then
643 -- Both the parent subprogram and overriding subprogram are
644 -- ignored Ghost.
646 if Is_Ignored_Ghost_Entity (Over_Subp) then
647 null;
649 -- The parent subprogram carries policy Check
651 elsif Is_Checked_Ghost_Entity (Over_Subp) then
652 Error_Msg_N
653 ("incompatible ghost policies in effect", Subp);
655 Error_Msg_Sloc := Sloc (Over_Subp);
656 Error_Msg_N
657 ("\& declared # with ghost policy `Check`", Subp);
659 Error_Msg_Sloc := Sloc (Subp);
660 Error_Msg_N
661 ("\overridden # with ghost policy `Ignore`", Subp);
663 -- The parent subprogram is non-Ghost
665 else
666 Error_Msg_N
667 ("incompatible ghost policies in effect", Subp);
669 Error_Msg_Sloc := Sloc (Over_Subp);
670 Error_Msg_N ("\& declared # as non-ghost subprogram", Subp);
672 Error_Msg_Sloc := Sloc (Subp);
673 Error_Msg_N
674 ("\overridden # with ghost policy `Ignore`", Subp);
675 end if;
677 -- When a tagged type is either non-Ghost or checked Ghost and
678 -- one of its primitives overrides an inherited operation, the
679 -- the primitive of the tagged type must be ignored Ghost if the
680 -- overridden operation is ignored Ghost (SPARK RM 6.9(17)).
682 elsif Is_Ignored_Ghost_Entity (Over_Subp) then
684 -- Both the parent subprogram and the overriding subprogram are
685 -- ignored Ghost.
687 if Is_Ignored_Ghost_Entity (Subp) then
688 null;
690 -- The overriding subprogram carries policy Check
692 elsif Is_Checked_Ghost_Entity (Subp) then
693 Error_Msg_N
694 ("incompatible ghost policies in effect", Subp);
696 Error_Msg_Sloc := Sloc (Over_Subp);
697 Error_Msg_N
698 ("\& declared # with ghost policy `Ignore`", Subp);
700 Error_Msg_Sloc := Sloc (Subp);
701 Error_Msg_N
702 ("\overridden # with Ghost policy `Check`", Subp);
704 -- The overriding subprogram is non-Ghost
706 else
707 Error_Msg_N
708 ("incompatible ghost policies in effect", Subp);
710 Error_Msg_Sloc := Sloc (Over_Subp);
711 Error_Msg_N
712 ("\& declared # with ghost policy `Ignore`", Subp);
714 Error_Msg_Sloc := Sloc (Subp);
715 Error_Msg_N
716 ("\overridden # with non-ghost subprogram", Subp);
717 end if;
718 end if;
719 end if;
720 end if;
721 end Check_Ghost_Overriding;
723 ---------------------------
724 -- Check_Ghost_Primitive --
725 ---------------------------
727 procedure Check_Ghost_Primitive (Prim : Entity_Id; Typ : Entity_Id) is
728 begin
729 -- The Ghost policy in effect at the point of declaration of a primitive
730 -- operation and a tagged type must match (SPARK RM 6.9(16)).
732 if Is_Tagged_Type (Typ) then
733 if Is_Checked_Ghost_Entity (Prim)
734 and then Is_Ignored_Ghost_Entity (Typ)
735 then
736 Error_Msg_N ("incompatible ghost policies in effect", Prim);
738 Error_Msg_Sloc := Sloc (Typ);
739 Error_Msg_NE
740 ("\tagged type & declared # with ghost policy `Ignore`",
741 Prim, Typ);
743 Error_Msg_Sloc := Sloc (Prim);
744 Error_Msg_N
745 ("\primitive subprogram & declared # with ghost policy `Check`",
746 Prim);
748 elsif Is_Ignored_Ghost_Entity (Prim)
749 and then Is_Checked_Ghost_Entity (Typ)
750 then
751 Error_Msg_N ("incompatible ghost policies in effect", Prim);
753 Error_Msg_Sloc := Sloc (Typ);
754 Error_Msg_NE
755 ("\tagged type & declared # with ghost policy `Check`",
756 Prim, Typ);
758 Error_Msg_Sloc := Sloc (Prim);
759 Error_Msg_N
760 ("\primitive subprogram & declared # with ghost policy `Ignore`",
761 Prim);
762 end if;
763 end if;
764 end Check_Ghost_Primitive;
766 ----------------------------
767 -- Check_Ghost_Refinement --
768 ----------------------------
770 procedure Check_Ghost_Refinement
771 (State : Node_Id;
772 State_Id : Entity_Id;
773 Constit : Node_Id;
774 Constit_Id : Entity_Id)
776 begin
777 if Is_Ghost_Entity (State_Id) then
778 if Is_Ghost_Entity (Constit_Id) then
780 -- The Ghost policy in effect at the point of abstract state
781 -- declaration and constituent must match (SPARK RM 6.9(15)).
783 if Is_Checked_Ghost_Entity (State_Id)
784 and then Is_Ignored_Ghost_Entity (Constit_Id)
785 then
786 Error_Msg_Sloc := Sloc (Constit);
787 SPARK_Msg_N ("incompatible ghost policies in effect", State);
789 SPARK_Msg_NE
790 ("\abstract state & declared with ghost policy `Check`",
791 State, State_Id);
792 SPARK_Msg_NE
793 ("\constituent & declared # with ghost policy `Ignore`",
794 State, Constit_Id);
796 elsif Is_Ignored_Ghost_Entity (State_Id)
797 and then Is_Checked_Ghost_Entity (Constit_Id)
798 then
799 Error_Msg_Sloc := Sloc (Constit);
800 SPARK_Msg_N ("incompatible ghost policies in effect", State);
802 SPARK_Msg_NE
803 ("\abstract state & declared with ghost policy `Ignore`",
804 State, State_Id);
805 SPARK_Msg_NE
806 ("\constituent & declared # with ghost policy `Check`",
807 State, Constit_Id);
808 end if;
810 -- A constituent of a Ghost abstract state must be a Ghost entity
811 -- (SPARK RM 7.2.2(12)).
813 else
814 SPARK_Msg_NE
815 ("constituent of ghost state & must be ghost",
816 Constit, State_Id);
817 end if;
818 end if;
819 end Check_Ghost_Refinement;
821 ------------------
822 -- Ghost_Entity --
823 ------------------
825 function Ghost_Entity (N : Node_Id) return Entity_Id is
826 Ref : Node_Id;
828 begin
829 -- When the reference extracts a subcomponent, recover the related
830 -- object (SPARK RM 6.9(1)).
832 Ref := N;
833 while Nkind_In (Ref, N_Explicit_Dereference,
834 N_Indexed_Component,
835 N_Selected_Component,
836 N_Slice)
837 loop
838 Ref := Prefix (Ref);
839 end loop;
841 if Is_Entity_Name (Ref) then
842 return Entity (Ref);
843 else
844 return Empty;
845 end if;
846 end Ghost_Entity;
848 --------------------------------
849 -- Implements_Ghost_Interface --
850 --------------------------------
852 function Implements_Ghost_Interface (Typ : Entity_Id) return Boolean is
853 Iface_Elmt : Elmt_Id;
855 begin
856 -- Traverse the list of interfaces looking for a Ghost interface
858 if Is_Tagged_Type (Typ) and then Present (Interfaces (Typ)) then
859 Iface_Elmt := First_Elmt (Interfaces (Typ));
860 while Present (Iface_Elmt) loop
861 if Is_Ghost_Entity (Node (Iface_Elmt)) then
862 return True;
863 end if;
865 Next_Elmt (Iface_Elmt);
866 end loop;
867 end if;
869 return False;
870 end Implements_Ghost_Interface;
872 ----------------
873 -- Initialize --
874 ----------------
876 procedure Initialize is
877 begin
878 Ignored_Ghost_Units.Init;
879 end Initialize;
881 -------------------------
882 -- Is_Subject_To_Ghost --
883 -------------------------
885 function Is_Subject_To_Ghost (N : Node_Id) return Boolean is
886 function Enables_Ghostness (Arg : Node_Id) return Boolean;
887 -- Determine whether aspect or pragma argument Arg enables "ghostness"
889 -----------------------
890 -- Enables_Ghostness --
891 -----------------------
893 function Enables_Ghostness (Arg : Node_Id) return Boolean is
894 Expr : Node_Id;
896 begin
897 Expr := Arg;
899 if Nkind (Expr) = N_Pragma_Argument_Association then
900 Expr := Get_Pragma_Arg (Expr);
901 end if;
903 -- Determine whether the expression of the aspect or pragma is static
904 -- and denotes True.
906 if Present (Expr) then
907 Preanalyze_And_Resolve (Expr);
909 return
910 Is_OK_Static_Expression (Expr)
911 and then Is_True (Expr_Value (Expr));
913 -- Otherwise Ghost defaults to True
915 else
916 return True;
917 end if;
918 end Enables_Ghostness;
920 -- Local variables
922 Id : constant Entity_Id := Defining_Entity (N);
923 Asp : Node_Id;
924 Decl : Node_Id;
925 Prev_Id : Entity_Id;
927 -- Start of processing for Is_Subject_To_Ghost
929 begin
930 -- The related entity of the declaration has not been analyzed yet, do
931 -- not inspect its attributes.
933 if Ekind (Id) = E_Void then
934 null;
936 elsif Is_Ghost_Entity (Id) then
937 return True;
939 -- The completion of a type or a constant is not fully analyzed when the
940 -- reference to the Ghost entity is resolved. Because the completion is
941 -- not marked as Ghost yet, inspect the partial view.
943 elsif Is_Record_Type (Id)
944 or else Ekind (Id) = E_Constant
945 or else (Nkind (N) = N_Object_Declaration
946 and then Constant_Present (N))
947 then
948 Prev_Id := Incomplete_Or_Partial_View (Id);
950 if Present (Prev_Id) and then Is_Ghost_Entity (Prev_Id) then
951 return True;
952 end if;
953 end if;
955 -- Examine the aspect specifications (if any) looking for aspect Ghost
957 if Permits_Aspect_Specifications (N) then
958 Asp := First (Aspect_Specifications (N));
959 while Present (Asp) loop
960 if Chars (Identifier (Asp)) = Name_Ghost then
961 return Enables_Ghostness (Expression (Asp));
962 end if;
964 Next (Asp);
965 end loop;
966 end if;
968 Decl := Empty;
970 -- When the context is a [generic] package declaration, pragma Ghost
971 -- resides in the visible declarations.
973 if Nkind_In (N, N_Generic_Package_Declaration,
974 N_Package_Declaration)
975 then
976 Decl := First (Visible_Declarations (Specification (N)));
978 -- When the context is a package or a subprogram body, pragma Ghost
979 -- resides in the declarative part.
981 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
982 Decl := First (Declarations (N));
984 -- Otherwise pragma Ghost appears in the declarations following N
986 elsif Is_List_Member (N) then
987 Decl := Next (N);
988 end if;
990 while Present (Decl) loop
991 if Nkind (Decl) = N_Pragma
992 and then Pragma_Name (Decl) = Name_Ghost
993 then
994 return
995 Enables_Ghostness (First (Pragma_Argument_Associations (Decl)));
997 -- A source construct ends the region where pragma Ghost may appear,
998 -- stop the traversal. Check the original node as source constructs
999 -- may be rewritten into something else by expansion.
1001 elsif Comes_From_Source (Original_Node (Decl)) then
1002 exit;
1003 end if;
1005 Next (Decl);
1006 end loop;
1008 return False;
1009 end Is_Subject_To_Ghost;
1011 ----------
1012 -- Lock --
1013 ----------
1015 procedure Lock is
1016 begin
1017 Ignored_Ghost_Units.Locked := True;
1018 Ignored_Ghost_Units.Release;
1019 end Lock;
1021 -----------------------------
1022 -- Mark_Full_View_As_Ghost --
1023 -----------------------------
1025 procedure Mark_Full_View_As_Ghost
1026 (Priv_Typ : Entity_Id;
1027 Full_Typ : Entity_Id)
1029 Full_Decl : constant Node_Id := Declaration_Node (Full_Typ);
1031 begin
1032 if Is_Checked_Ghost_Entity (Priv_Typ) then
1033 Set_Is_Checked_Ghost_Entity (Full_Typ);
1035 elsif Is_Ignored_Ghost_Entity (Priv_Typ) then
1036 Set_Is_Ignored_Ghost_Entity (Full_Typ);
1037 Set_Is_Ignored_Ghost_Node (Full_Decl);
1038 Propagate_Ignored_Ghost_Code (Full_Decl);
1039 end if;
1040 end Mark_Full_View_As_Ghost;
1042 --------------------------
1043 -- Mark_Pragma_As_Ghost --
1044 --------------------------
1046 procedure Mark_Pragma_As_Ghost
1047 (Prag : Node_Id;
1048 Context_Id : Entity_Id)
1050 begin
1051 if Is_Checked_Ghost_Entity (Context_Id) then
1052 Set_Is_Ghost_Pragma (Prag);
1054 elsif Is_Ignored_Ghost_Entity (Context_Id) then
1055 Set_Is_Ghost_Pragma (Prag);
1056 Set_Is_Ignored_Ghost_Node (Prag);
1057 Propagate_Ignored_Ghost_Code (Prag);
1058 end if;
1059 end Mark_Pragma_As_Ghost;
1061 ----------------------------
1062 -- Mark_Renaming_As_Ghost --
1063 ----------------------------
1065 procedure Mark_Renaming_As_Ghost
1066 (Ren_Decl : Node_Id;
1067 Nam_Id : Entity_Id)
1069 Ren_Id : constant Entity_Id := Defining_Entity (Ren_Decl);
1071 begin
1072 if Is_Checked_Ghost_Entity (Nam_Id) then
1073 Set_Is_Checked_Ghost_Entity (Ren_Id);
1075 elsif Is_Ignored_Ghost_Entity (Nam_Id) then
1076 Set_Is_Ignored_Ghost_Entity (Ren_Id);
1077 Set_Is_Ignored_Ghost_Node (Ren_Decl);
1078 Propagate_Ignored_Ghost_Code (Ren_Decl);
1079 end if;
1080 end Mark_Renaming_As_Ghost;
1082 ----------------------------------
1083 -- Propagate_Ignored_Ghost_Code --
1084 ----------------------------------
1086 procedure Propagate_Ignored_Ghost_Code (N : Node_Id) is
1087 Nod : Node_Id;
1088 Scop : Entity_Id;
1090 begin
1091 -- Traverse the parent chain looking for blocks, packages and
1092 -- subprograms or their respective bodies.
1094 Nod := Parent (N);
1095 while Present (Nod) loop
1096 Scop := Empty;
1098 if Nkind (Nod) = N_Block_Statement then
1099 Scop := Entity (Identifier (Nod));
1101 elsif Nkind_In (Nod, N_Package_Body,
1102 N_Package_Declaration,
1103 N_Subprogram_Body,
1104 N_Subprogram_Declaration)
1105 then
1106 Scop := Defining_Entity (Nod);
1107 end if;
1109 -- The current node denotes a scoping construct
1111 if Present (Scop) then
1113 -- Stop the traversal when the scope already contains ignored
1114 -- Ghost code as all enclosing scopes have already been marked.
1116 if Contains_Ignored_Ghost_Code (Scop) then
1117 exit;
1119 -- Otherwise mark this scope and keep climbing
1121 else
1122 Set_Contains_Ignored_Ghost_Code (Scop);
1123 end if;
1124 end if;
1126 Nod := Parent (Nod);
1127 end loop;
1129 -- The unit containing the ignored Ghost code must be post processed
1130 -- before invoking the back end.
1132 Add_Ignored_Ghost_Unit (Cunit (Get_Code_Unit (N)));
1133 end Propagate_Ignored_Ghost_Code;
1135 -------------------------------
1136 -- Remove_Ignored_Ghost_Code --
1137 -------------------------------
1139 procedure Remove_Ignored_Ghost_Code is
1140 procedure Prune_Tree (Root : Node_Id);
1141 -- Remove all code marked as ignored Ghost from the tree of denoted by
1142 -- Root.
1144 ----------------
1145 -- Prune_Tree --
1146 ----------------
1148 procedure Prune_Tree (Root : Node_Id) is
1149 procedure Prune (N : Node_Id);
1150 -- Remove a given node from the tree by rewriting it into null
1152 function Prune_Node (N : Node_Id) return Traverse_Result;
1153 -- Determine whether node N denotes an ignored Ghost construct. If
1154 -- this is the case, rewrite N as a null statement. See the body for
1155 -- special cases.
1157 -----------
1158 -- Prune --
1159 -----------
1161 procedure Prune (N : Node_Id) is
1162 begin
1163 -- Destroy any aspects that may be associated with the node
1165 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
1166 Remove_Aspects (N);
1167 end if;
1169 Rewrite (N, Make_Null_Statement (Sloc (N)));
1170 end Prune;
1172 ----------------
1173 -- Prune_Node --
1174 ----------------
1176 function Prune_Node (N : Node_Id) return Traverse_Result is
1177 Id : Entity_Id;
1179 begin
1180 -- The node is either declared as ignored Ghost or is a byproduct
1181 -- of expansion. Destroy it and stop the traversal on this branch.
1183 if Is_Ignored_Ghost_Node (N) then
1184 Prune (N);
1185 return Skip;
1187 -- A freeze node for an ignored ghost entity must be pruned as
1188 -- well, to prevent meaningless references in the back end.
1190 -- ??? the freeze node itself should be ignored ghost
1192 elsif Nkind (N) = N_Freeze_Entity
1193 and then Is_Ignored_Ghost_Entity (Entity (N))
1194 then
1195 Prune (N);
1196 return Skip;
1198 -- Scoping constructs such as blocks, packages, subprograms and
1199 -- bodies offer some flexibility with respect to pruning.
1201 elsif Nkind_In (N, N_Block_Statement,
1202 N_Package_Body,
1203 N_Package_Declaration,
1204 N_Subprogram_Body,
1205 N_Subprogram_Declaration)
1206 then
1207 if Nkind (N) = N_Block_Statement then
1208 Id := Entity (Identifier (N));
1209 else
1210 Id := Defining_Entity (N);
1211 end if;
1213 -- The scoping construct contains both living and ignored Ghost
1214 -- code, let the traversal prune all relevant nodes.
1216 if Contains_Ignored_Ghost_Code (Id) then
1217 return OK;
1219 -- Otherwise the construct contains only living code and should
1220 -- not be pruned.
1222 else
1223 return Skip;
1224 end if;
1226 -- Otherwise keep searching for ignored Ghost nodes
1228 else
1229 return OK;
1230 end if;
1231 end Prune_Node;
1233 procedure Prune_Nodes is new Traverse_Proc (Prune_Node);
1235 -- Start of processing for Prune_Tree
1237 begin
1238 Prune_Nodes (Root);
1239 end Prune_Tree;
1241 -- Start of processing for Remove_Ignored_Ghost_Code
1243 begin
1244 for Index in Ignored_Ghost_Units.First .. Ignored_Ghost_Units.Last loop
1245 Prune_Tree (Unit (Ignored_Ghost_Units.Table (Index)));
1246 end loop;
1247 end Remove_Ignored_Ghost_Code;
1249 --------------------
1250 -- Set_Ghost_Mode --
1251 --------------------
1253 procedure Set_Ghost_Mode (N : Node_Id; Id : Entity_Id := Empty) is
1254 procedure Set_From_Entity (Ent_Id : Entity_Id);
1255 -- Set the value of global variable Ghost_Mode depending on the mode of
1256 -- entity Ent_Id.
1258 procedure Set_From_Policy;
1259 -- Set the value of global variable Ghost_Mode depending on the current
1260 -- Ghost policy in effect.
1262 ---------------------
1263 -- Set_From_Entity --
1264 ---------------------
1266 procedure Set_From_Entity (Ent_Id : Entity_Id) is
1267 begin
1268 Set_Ghost_Mode_From_Entity (Ent_Id);
1270 if Is_Ignored_Ghost_Entity (Ent_Id) then
1271 Set_Is_Ignored_Ghost_Node (N);
1272 Propagate_Ignored_Ghost_Code (N);
1273 end if;
1274 end Set_From_Entity;
1276 ---------------------
1277 -- Set_From_Policy --
1278 ---------------------
1280 procedure Set_From_Policy is
1281 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
1283 begin
1284 if Policy = Name_Check then
1285 Ghost_Mode := Check;
1287 elsif Policy = Name_Ignore then
1288 Ghost_Mode := Ignore;
1290 Set_Is_Ignored_Ghost_Node (N);
1291 Propagate_Ignored_Ghost_Code (N);
1292 end if;
1293 end Set_From_Policy;
1295 -- Local variables
1297 Nam_Id : Entity_Id;
1299 -- Start of processing for Set_Ghost_Mode
1301 begin
1302 -- The input node denotes one of the many declaration kinds that may be
1303 -- subject to pragma Ghost.
1305 if Is_Declaration (N) then
1306 if Is_Subject_To_Ghost (N) then
1307 Set_From_Policy;
1309 -- The declaration denotes the completion of a deferred constant,
1310 -- pragma Ghost appears on the partial declaration.
1312 elsif Nkind (N) = N_Object_Declaration
1313 and then Constant_Present (N)
1314 and then Present (Id)
1315 then
1316 Set_From_Entity (Id);
1318 -- The declaration denotes the full view of a private type, pragma
1319 -- Ghost appears on the partial declaration.
1321 elsif Nkind (N) = N_Full_Type_Declaration
1322 and then Is_Private_Type (Defining_Entity (N))
1323 and then Present (Id)
1324 then
1325 Set_From_Entity (Id);
1326 end if;
1328 -- The input denotes an assignment or a procedure call. In this case
1329 -- the Ghost mode is dictated by the name of the construct.
1331 elsif Nkind_In (N, N_Assignment_Statement,
1332 N_Procedure_Call_Statement)
1333 then
1334 Nam_Id := Ghost_Entity (Name (N));
1336 if Present (Nam_Id) then
1337 Set_From_Entity (Nam_Id);
1338 end if;
1340 -- The input denotes a package or subprogram body
1342 elsif Nkind_In (N, N_Package_Body, N_Subprogram_Body) then
1343 if (Present (Id) and then Is_Ghost_Entity (Id))
1344 or else Is_Subject_To_Ghost (N)
1345 then
1346 Set_From_Policy;
1347 end if;
1349 -- The input denotes a pragma
1351 elsif Nkind (N) = N_Pragma and then Is_Ghost_Pragma (N) then
1352 if Is_Ignored_Ghost_Node (N) then
1353 Ghost_Mode := Ignore;
1354 else
1355 Ghost_Mode := Check;
1356 end if;
1358 -- The input denotes a freeze node
1360 elsif Nkind (N) = N_Freeze_Entity and then Present (Id) then
1361 Set_From_Entity (Id);
1362 end if;
1363 end Set_Ghost_Mode;
1365 --------------------------------
1366 -- Set_Ghost_Mode_From_Entity --
1367 --------------------------------
1369 procedure Set_Ghost_Mode_From_Entity (Id : Entity_Id) is
1370 begin
1371 if Is_Checked_Ghost_Entity (Id) then
1372 Ghost_Mode := Check;
1373 elsif Is_Ignored_Ghost_Entity (Id) then
1374 Ghost_Mode := Ignore;
1375 end if;
1376 end Set_Ghost_Mode_From_Entity;
1378 -------------------------
1379 -- Set_Is_Ghost_Entity --
1380 -------------------------
1382 procedure Set_Is_Ghost_Entity (Id : Entity_Id) is
1383 Policy : constant Name_Id := Policy_In_Effect (Name_Ghost);
1384 begin
1385 if Policy = Name_Check then
1386 Set_Is_Checked_Ghost_Entity (Id);
1387 elsif Policy = Name_Ignore then
1388 Set_Is_Ignored_Ghost_Entity (Id);
1389 end if;
1390 end Set_Is_Ghost_Entity;
1392 end Ghost;