1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Inline; use Inline;
45 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Layout; use Layout;
48 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Output; use Output;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch9; use Sem_Ch9;
65 with Sem_Ch10; use Sem_Ch10;
66 with Sem_Ch12; use Sem_Ch12;
67 with Sem_Ch13; use Sem_Ch13;
68 with Sem_Dim; use Sem_Dim;
69 with Sem_Disp; use Sem_Disp;
70 with Sem_Dist; use Sem_Dist;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Prag; use Sem_Prag;
75 with Sem_Res; use Sem_Res;
76 with Sem_Util; use Sem_Util;
77 with Sem_Type; use Sem_Type;
78 with Sem_Warn; use Sem_Warn;
79 with Sinput; use Sinput;
80 with Stand; use Stand;
81 with Sinfo; use Sinfo;
82 with Sinfo.CN; use Sinfo.CN;
83 with Snames; use Snames;
84 with Stringt; use Stringt;
86 with Stylesw; use Stylesw;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
138 Ctype : Conformance_Type;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
156 procedure Check_Limited_Return
160 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
161 -- types. Used only for simple return statements. Expr is the expression
164 procedure Check_Subprogram_Order (N : Node_Id);
165 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
166 -- the alpha ordering rule for N if this ordering requirement applicable.
168 procedure Check_Returns
172 Proc : Entity_Id := Empty);
173 -- Called to check for missing return statements in a function body, or for
174 -- returns present in a procedure body which has No_Return set. HSS is the
175 -- handled statement sequence for the subprogram body. This procedure
176 -- checks all flow paths to make sure they either have return (Mode = 'F',
177 -- used for functions) or do not have a return (Mode = 'P', used for
178 -- No_Return procedures). The flag Err is set if there are any control
179 -- paths not explicitly terminated by a return in the function case, and is
180 -- True otherwise. Proc is the entity for the procedure case and is used
181 -- in posting the warning message.
183 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
184 -- In Ada 2012, a primitive equality operator on an untagged record type
185 -- must appear before the type is frozen, and have the same visibility as
186 -- that of the type. This procedure checks that this rule is met, and
187 -- otherwise emits an error on the subprogram declaration and a warning
188 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
189 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
190 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
191 -- is set, otherwise the call has no effect.
193 procedure Enter_Overloaded_Entity (S : Entity_Id);
194 -- This procedure makes S, a new overloaded entity, into the first visible
195 -- entity with that name.
197 function Is_Non_Overriding_Operation
199 New_E : Entity_Id) return Boolean;
200 -- Enforce the rule given in 12.3(18): a private operation in an instance
201 -- overrides an inherited operation only if the corresponding operation
202 -- was overriding in the generic. This needs to be checked for primitive
203 -- operations of types derived (in the generic unit) from formal private
204 -- or formal derived types.
206 procedure Make_Inequality_Operator (S : Entity_Id);
207 -- Create the declaration for an inequality operator that is implicitly
208 -- created by a user-defined equality operator that yields a boolean.
210 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
211 -- Formal_Id is an formal parameter entity. This procedure deals with
212 -- setting the proper validity status for this entity, which depends on
213 -- the kind of parameter and the validity checking mode.
215 ---------------------------------------------
216 -- Analyze_Abstract_Subprogram_Declaration --
217 ---------------------------------------------
219 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
220 Scop : constant Entity_Id := Current_Scope;
221 Subp_Id : constant Entity_Id :=
222 Analyze_Subprogram_Specification (Specification (N));
225 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
227 Generate_Definition (Subp_Id);
229 Set_Is_Abstract_Subprogram (Subp_Id);
230 New_Overloaded_Entity (Subp_Id);
231 Check_Delayed_Subprogram (Subp_Id);
233 Set_Categorization_From_Scope (Subp_Id, Scop);
235 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
236 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
238 -- Issue a warning if the abstract subprogram is neither a dispatching
239 -- operation nor an operation that overrides an inherited subprogram or
240 -- predefined operator, since this most likely indicates a mistake.
242 elsif Warn_On_Redundant_Constructs
243 and then not Is_Dispatching_Operation (Subp_Id)
244 and then not Present (Overridden_Operation (Subp_Id))
245 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
246 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
249 ("abstract subprogram is not dispatching or overriding?r?", N);
252 Generate_Reference_To_Formals (Subp_Id);
253 Check_Eliminated (Subp_Id);
255 if Has_Aspects (N) then
256 Analyze_Aspect_Specifications (N, Subp_Id);
258 end Analyze_Abstract_Subprogram_Declaration;
260 ---------------------------------
261 -- Analyze_Expression_Function --
262 ---------------------------------
264 procedure Analyze_Expression_Function (N : Node_Id) is
265 Expr : constant Node_Id := Expression (N);
266 Loc : constant Source_Ptr := Sloc (N);
267 LocX : constant Source_Ptr := Sloc (Expr);
268 Spec : constant Node_Id := Specification (N);
270 procedure Freeze_Expr_Types (Def_Id : Entity_Id);
271 -- N is an expression function that is a completion and Def_Id its
272 -- defining entity. Freeze before N all the types referenced by the
273 -- expression of the function.
275 -----------------------
276 -- Freeze_Expr_Types --
277 -----------------------
279 procedure Freeze_Expr_Types (Def_Id : Entity_Id) is
280 function Cloned_Expression return Node_Id;
281 -- Build a duplicate of the expression of the return statement that
282 -- has no defining entities shared with the original expression.
284 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
285 -- Freeze all types referenced in the subtree rooted at Node
287 -----------------------
288 -- Cloned_Expression --
289 -----------------------
291 function Cloned_Expression return Node_Id is
292 function Clone_Id (Node : Node_Id) return Traverse_Result;
293 -- Tree traversal routine that clones the defining identifier of
294 -- iterator and loop parameter specification nodes.
300 function Clone_Id (Node : Node_Id) return Traverse_Result is
302 if Nkind_In (Node, N_Iterator_Specification,
303 N_Loop_Parameter_Specification)
305 Set_Defining_Identifier (Node,
306 New_Copy (Defining_Identifier (Node)));
312 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
316 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
318 -- Start of processing for Cloned_Expression
321 -- We must duplicate the expression with semantic information to
322 -- inherit the decoration of global entities in generic instances.
323 -- Set the parent of the new node to be the parent of the original
324 -- to get the proper context, which is needed for complete error
325 -- reporting and for semantic analysis.
327 Set_Parent (Dup_Expr, Parent (Expr));
329 -- Replace the defining identifier of iterators and loop param
330 -- specifications by a clone to ensure that the cloned expression
331 -- and the original expression don't have shared identifiers;
332 -- otherwise, as part of the preanalysis of the expression, these
333 -- shared identifiers may be left decorated with itypes which
334 -- will not be available in the tree passed to the backend.
336 Clone_Def_Ids (Dup_Expr);
339 end Cloned_Expression;
341 ----------------------
342 -- Freeze_Type_Refs --
343 ----------------------
345 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
346 procedure Check_And_Freeze_Type (Typ : Entity_Id);
347 -- Check that Typ is fully declared and freeze it if so
349 ---------------------------
350 -- Check_And_Freeze_Type --
351 ---------------------------
353 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
355 -- Skip Itypes created by the preanalysis
358 and then Scope_Within_Or_Same (Scope (Typ), Def_Id)
363 -- This provides a better error message than generating
364 -- primitives whose compilation fails much later. Refine
365 -- the error message if possible.
367 Check_Fully_Declared (Typ, Node);
369 if Error_Posted (Node) then
370 if Has_Private_Component (Typ)
371 and then not Is_Private_Type (Typ)
373 Error_Msg_NE ("\type& has private component", Node, Typ);
377 Freeze_Before (N, Typ);
379 end Check_And_Freeze_Type;
381 -- Start of processing for Freeze_Type_Refs
384 -- Check that a type referenced by an entity can be frozen
386 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
387 Check_And_Freeze_Type (Etype (Entity (Node)));
389 -- Check that the enclosing record type can be frozen
391 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
392 Check_And_Freeze_Type (Scope (Entity (Node)));
395 -- Freezing an access type does not freeze the designated type,
396 -- but freezing conversions between access to interfaces requires
397 -- that the interface types themselves be frozen, so that dispatch
398 -- table entities are properly created.
400 -- Unclear whether a more general rule is needed ???
402 elsif Nkind (Node) = N_Type_Conversion
403 and then Is_Access_Type (Etype (Node))
404 and then Is_Interface (Designated_Type (Etype (Node)))
406 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
409 -- No point in posting several errors on the same expression
411 if Serious_Errors_Detected > 0 then
416 end Freeze_Type_Refs;
418 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
422 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
423 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
424 Dup_Expr : constant Node_Id := Cloned_Expression;
426 -- Start of processing for Freeze_Expr_Types
429 -- Preanalyze a duplicate of the expression to have available the
430 -- minimum decoration needed to locate referenced unfrozen types
431 -- without adding any decoration to the function expression. This
432 -- preanalysis is performed with errors disabled to avoid reporting
433 -- spurious errors on Ghost entities (since the expression is not
437 Install_Formals (Def_Id);
438 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
440 Preanalyze_Spec_Expression (Dup_Expr, Etype (Def_Id));
442 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
445 -- Restore certain attributes of Def_Id since the preanalysis may
446 -- have introduced itypes to this scope, thus modifying attributes
447 -- First_Entity and Last_Entity.
449 Set_First_Entity (Def_Id, Saved_First_Entity);
450 Set_Last_Entity (Def_Id, Saved_Last_Entity);
452 if Present (Last_Entity (Def_Id)) then
453 Set_Next_Entity (Last_Entity (Def_Id), Empty);
456 -- Freeze all types referenced in the expression
458 Freeze_References (Dup_Expr);
459 end Freeze_Expr_Types;
469 Def_Id : Entity_Id := Empty;
471 -- If the expression is a completion, Prev is the entity whose
472 -- declaration is completed. Def_Id is needed to analyze the spec.
474 -- Start of processing for Analyze_Expression_Function
477 -- This is one of the occasions on which we transform the tree during
478 -- semantic analysis. If this is a completion, transform the expression
479 -- function into an equivalent subprogram body, and analyze it.
481 -- Expression functions are inlined unconditionally. The back-end will
482 -- determine whether this is possible.
484 Inline_Processing_Required := True;
486 -- Create a specification for the generated body. This must be done
487 -- prior to the analysis of the initial declaration.
489 New_Spec := Copy_Subprogram_Spec (Spec);
490 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
492 -- If there are previous overloadable entities with the same name,
493 -- check whether any of them is completed by the expression function.
494 -- In a generic context a formal subprogram has no completion.
497 and then Is_Overloadable (Prev)
498 and then not Is_Formal_Subprogram (Prev)
500 Def_Id := Analyze_Subprogram_Specification (Spec);
501 Prev := Find_Corresponding_Spec (N);
503 -- The previous entity may be an expression function as well, in
504 -- which case the redeclaration is illegal.
507 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
508 N_Expression_Function
510 Error_Msg_Sloc := Sloc (Prev);
511 Error_Msg_N ("& conflicts with declaration#", Def_Id);
516 Ret := Make_Simple_Return_Statement (LocX, Expr);
519 Make_Subprogram_Body (Loc,
520 Specification => New_Spec,
521 Declarations => Empty_List,
522 Handled_Statement_Sequence =>
523 Make_Handled_Sequence_Of_Statements (LocX,
524 Statements => New_List (Ret)));
525 Set_Was_Expression_Function (New_Body);
527 -- If the expression completes a generic subprogram, we must create a
528 -- separate node for the body, because at instantiation the original
529 -- node of the generic copy must be a generic subprogram body, and
530 -- cannot be a expression function. Otherwise we just rewrite the
531 -- expression with the non-generic body.
533 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
534 Insert_After (N, New_Body);
536 -- Propagate any aspects or pragmas that apply to the expression
537 -- function to the proper body when the expression function acts
540 if Has_Aspects (N) then
541 Move_Aspects (N, To => New_Body);
544 Relocate_Pragmas_To_Body (New_Body);
546 Rewrite (N, Make_Null_Statement (Loc));
547 Set_Has_Completion (Prev, False);
550 Set_Is_Inlined (Prev);
552 -- If the expression function is a completion, the previous declaration
553 -- must come from source. We know already that it appears in the current
554 -- scope. The entity itself may be internally created if within a body
558 and then Is_Overloadable (Prev)
559 and then not Is_Formal_Subprogram (Prev)
560 and then Comes_From_Source (Parent (Prev))
562 Set_Has_Completion (Prev, False);
563 Set_Is_Inlined (Prev);
565 -- AI12-0103: Expression functions that are a completion freeze their
566 -- expression but don't freeze anything else (unlike regular bodies).
568 -- Note that we cannot defer this freezing to the analysis of the
569 -- expression itself, because a freeze node might appear in a nested
570 -- scope, leading to an elaboration order issue in gigi.
572 Freeze_Expr_Types (Def_Id);
574 -- For navigation purposes, indicate that the function is a body
576 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
577 Rewrite (N, New_Body);
579 -- Remove any existing aspects from the original node because the act
580 -- of rewriting causes the list to be shared between the two nodes.
582 Orig_N := Original_Node (N);
583 Remove_Aspects (Orig_N);
585 -- Propagate any pragmas that apply to expression function to the
586 -- proper body when the expression function acts as a completion.
587 -- Aspects are automatically transfered because of node rewriting.
589 Relocate_Pragmas_To_Body (N);
592 -- Once the aspects of the generated body have been analyzed, create
593 -- a copy for ASIS purposes and associate it with the original node.
595 if Has_Aspects (N) then
596 Set_Aspect_Specifications (Orig_N,
597 New_Copy_List_Tree (Aspect_Specifications (N)));
600 -- Prev is the previous entity with the same name, but it is can
601 -- be an unrelated spec that is not completed by the expression
602 -- function. In that case the relevant entity is the one in the body.
603 -- Not clear that the backend can inline it in this case ???
605 if Has_Completion (Prev) then
607 -- The formals of the expression function are body formals,
608 -- and do not appear in the ali file, which will only contain
609 -- references to the formals of the original subprogram spec.
616 F1 := First_Formal (Def_Id);
617 F2 := First_Formal (Prev);
619 while Present (F1) loop
620 Set_Spec_Entity (F1, F2);
627 Set_Is_Inlined (Defining_Entity (New_Body));
630 -- If this is not a completion, create both a declaration and a body, so
631 -- that the expression can be inlined whenever possible.
634 -- An expression function that is not a completion is not a
635 -- subprogram declaration, and thus cannot appear in a protected
638 if Nkind (Parent (N)) = N_Protected_Definition then
640 ("an expression function is not a legal protected operation", N);
643 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
645 -- Remove any existing aspects from the original node because the act
646 -- of rewriting causes the list to be shared between the two nodes.
648 Orig_N := Original_Node (N);
649 Remove_Aspects (Orig_N);
653 -- Once the aspects of the generated spec have been analyzed, create
654 -- a copy for ASIS purposes and associate it with the original node.
656 if Has_Aspects (N) then
657 Set_Aspect_Specifications (Orig_N,
658 New_Copy_List_Tree (Aspect_Specifications (N)));
661 -- If aspect SPARK_Mode was specified on the body, it needs to be
662 -- repeated both on the generated spec and the body.
664 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
666 if Present (Asp) then
667 Asp := New_Copy_Tree (Asp);
668 Set_Analyzed (Asp, False);
669 Set_Aspect_Specifications (New_Body, New_List (Asp));
672 Def_Id := Defining_Entity (N);
673 Set_Is_Inlined (Def_Id);
675 -- Establish the linkages between the spec and the body. These are
676 -- used when the expression function acts as the prefix of attribute
677 -- 'Access in order to freeze the original expression which has been
678 -- moved to the generated body.
680 Set_Corresponding_Body (N, Defining_Entity (New_Body));
681 Set_Corresponding_Spec (New_Body, Def_Id);
683 -- Within a generic pre-analyze the original expression for name
684 -- capture. The body is also generated but plays no role in
685 -- this because it is not part of the original source.
687 if Inside_A_Generic then
688 Set_Has_Completion (Def_Id);
690 Install_Formals (Def_Id);
691 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
695 -- To prevent premature freeze action, insert the new body at the end
696 -- of the current declarations, or at the end of the package spec.
697 -- However, resolve usage names now, to prevent spurious visibility
698 -- on later entities. Note that the function can now be called in
699 -- the current declarative part, which will appear to be prior to
700 -- the presence of the body in the code. There are nevertheless no
701 -- order of elaboration issues because all name resolution has taken
702 -- place at the point of declaration.
705 Decls : List_Id := List_Containing (N);
706 Expr : constant Node_Id := Expression (Ret);
707 Par : constant Node_Id := Parent (Decls);
708 Typ : constant Entity_Id := Etype (Def_Id);
711 -- If this is a wrapper created for in an instance for a formal
712 -- subprogram, insert body after declaration, to be analyzed when
713 -- the enclosing instance is analyzed.
716 and then Is_Generic_Actual_Subprogram (Def_Id)
718 Insert_After (N, New_Body);
721 if Nkind (Par) = N_Package_Specification
722 and then Decls = Visible_Declarations (Par)
723 and then Present (Private_Declarations (Par))
724 and then not Is_Empty_List (Private_Declarations (Par))
726 Decls := Private_Declarations (Par);
729 Insert_After (Last (Decls), New_Body);
731 -- Preanalyze the expression if not already done above
733 if not Inside_A_Generic then
735 Install_Formals (Def_Id);
736 Preanalyze_Spec_Expression (Expr, Typ);
737 Check_Limited_Return (Original_Node (N), Expr, Typ);
744 -- Check incorrect use of dynamically tagged expression. This doesn't
745 -- fall out automatically when analyzing the generated function body,
746 -- because Check_Dynamically_Tagged_Expression deliberately ignores
747 -- nodes that don't come from source.
750 and then Nkind (Def_Id) in N_Has_Etype
751 and then Is_Tagged_Type (Etype (Def_Id))
753 Check_Dynamically_Tagged_Expression
755 Typ => Etype (Def_Id),
756 Related_Nod => Original_Node (N));
759 -- If the return expression is a static constant, we suppress warning
760 -- messages on unused formals, which in most cases will be noise.
762 Set_Is_Trivial_Subprogram
763 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
764 end Analyze_Expression_Function;
766 ----------------------------------------
767 -- Analyze_Extended_Return_Statement --
768 ----------------------------------------
770 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
772 Check_Compiler_Unit ("extended return statement", N);
773 Analyze_Return_Statement (N);
774 end Analyze_Extended_Return_Statement;
776 ----------------------------
777 -- Analyze_Function_Call --
778 ----------------------------
780 procedure Analyze_Function_Call (N : Node_Id) is
781 Actuals : constant List_Id := Parameter_Associations (N);
782 Func_Nam : constant Node_Id := Name (N);
788 -- A call of the form A.B (X) may be an Ada 2005 call, which is
789 -- rewritten as B (A, X). If the rewriting is successful, the call
790 -- has been analyzed and we just return.
792 if Nkind (Func_Nam) = N_Selected_Component
793 and then Name (N) /= Func_Nam
794 and then Is_Rewrite_Substitution (N)
795 and then Present (Etype (N))
800 -- If error analyzing name, then set Any_Type as result type and return
802 if Etype (Func_Nam) = Any_Type then
803 Set_Etype (N, Any_Type);
807 -- Otherwise analyze the parameters
809 if Present (Actuals) then
810 Actual := First (Actuals);
811 while Present (Actual) loop
813 Check_Parameterless_Call (Actual);
819 end Analyze_Function_Call;
821 -----------------------------
822 -- Analyze_Function_Return --
823 -----------------------------
825 procedure Analyze_Function_Return (N : Node_Id) is
826 Loc : constant Source_Ptr := Sloc (N);
827 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
828 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
830 R_Type : constant Entity_Id := Etype (Scope_Id);
831 -- Function result subtype
833 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
834 -- Apply legality rule of 6.5 (5.8) to the access discriminants of an
835 -- aggregate in a return statement.
837 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
838 -- Check that the return_subtype_indication properly matches the result
839 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
841 -----------------------------------
842 -- Check_Aggregate_Accessibility --
843 -----------------------------------
845 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
846 Typ : constant Entity_Id := Etype (Aggr);
853 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
854 Discr := First_Discriminant (Typ);
855 Assoc := First (Component_Associations (Aggr));
856 while Present (Discr) loop
857 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
858 Expr := Expression (Assoc);
860 if Nkind (Expr) = N_Attribute_Reference
861 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
863 Obj := Prefix (Expr);
864 while Nkind_In (Obj, N_Indexed_Component,
865 N_Selected_Component)
870 -- Do not check aliased formals or function calls. A
871 -- run-time check may still be needed ???
873 if Is_Entity_Name (Obj)
874 and then Comes_From_Source (Obj)
876 if Is_Formal (Entity (Obj))
877 and then Is_Aliased (Entity (Obj))
881 elsif Object_Access_Level (Obj) >
882 Scope_Depth (Scope (Scope_Id))
885 ("access discriminant in return aggregate would "
886 & "be a dangling reference", Obj);
892 Next_Discriminant (Discr);
895 end Check_Aggregate_Accessibility;
897 -------------------------------------
898 -- Check_Return_Subtype_Indication --
899 -------------------------------------
901 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
902 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
904 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
905 -- Subtype given in the extended return statement (must match R_Type)
907 Subtype_Ind : constant Node_Id :=
908 Object_Definition (Original_Node (Obj_Decl));
910 procedure Error_No_Match (N : Node_Id);
911 -- Output error messages for case where types do not statically
912 -- match. N is the location for the messages.
918 procedure Error_No_Match (N : Node_Id) is
921 ("subtype must statically match function result subtype", N);
923 if not Predicates_Match (R_Stm_Type, R_Type) then
924 Error_Msg_Node_2 := R_Type;
926 ("\predicate of& does not match predicate of&",
931 -- Start of processing for Check_Return_Subtype_Indication
934 -- First, avoid cascaded errors
936 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
940 -- "return access T" case; check that the return statement also has
941 -- "access T", and that the subtypes statically match:
942 -- if this is an access to subprogram the signatures must match.
944 if Is_Anonymous_Access_Type (R_Type) then
945 if Is_Anonymous_Access_Type (R_Stm_Type) then
946 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
948 if Base_Type (Designated_Type (R_Stm_Type)) /=
949 Base_Type (Designated_Type (R_Type))
950 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
952 Error_No_Match (Subtype_Mark (Subtype_Ind));
956 -- For two anonymous access to subprogram types, the types
957 -- themselves must be type conformant.
959 if not Conforming_Types
960 (R_Stm_Type, R_Type, Fully_Conformant)
962 Error_No_Match (Subtype_Ind);
967 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
970 -- If the return object is of an anonymous access type, then report
971 -- an error if the function's result type is not also anonymous.
973 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
974 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
976 ("anonymous access not allowed for function with named access "
977 & "result", Subtype_Ind);
979 -- Subtype indication case: check that the return object's type is
980 -- covered by the result type, and that the subtypes statically match
981 -- when the result subtype is constrained. Also handle record types
982 -- with unknown discriminants for which we have built the underlying
983 -- record view. Coverage is needed to allow specific-type return
984 -- objects when the result type is class-wide (see AI05-32).
986 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
987 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
991 Underlying_Record_View (Base_Type (R_Stm_Type))))
993 -- A null exclusion may be present on the return type, on the
994 -- function specification, on the object declaration or on the
997 if Is_Access_Type (R_Type)
999 (Can_Never_Be_Null (R_Type)
1000 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1001 Can_Never_Be_Null (R_Stm_Type)
1003 Error_No_Match (Subtype_Ind);
1006 -- AI05-103: for elementary types, subtypes must statically match
1008 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1009 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1010 Error_No_Match (Subtype_Ind);
1014 -- All remaining cases are illegal
1016 -- Note: previous versions of this subprogram allowed the return
1017 -- value to be the ancestor of the return type if the return type
1018 -- was a null extension. This was plainly incorrect.
1022 ("wrong type for return_subtype_indication", Subtype_Ind);
1024 end Check_Return_Subtype_Indication;
1026 ---------------------
1027 -- Local Variables --
1028 ---------------------
1033 -- Start of processing for Analyze_Function_Return
1036 Set_Return_Present (Scope_Id);
1038 if Nkind (N) = N_Simple_Return_Statement then
1039 Expr := Expression (N);
1041 -- Guard against a malformed expression. The parser may have tried to
1042 -- recover but the node is not analyzable.
1044 if Nkind (Expr) = N_Error then
1045 Set_Etype (Expr, Any_Type);
1046 Expander_Mode_Save_And_Set (False);
1050 -- The resolution of a controlled [extension] aggregate associated
1051 -- with a return statement creates a temporary which needs to be
1052 -- finalized on function exit. Wrap the return statement inside a
1053 -- block so that the finalization machinery can detect this case.
1054 -- This early expansion is done only when the return statement is
1055 -- not part of a handled sequence of statements.
1057 if Nkind_In (Expr, N_Aggregate,
1058 N_Extension_Aggregate)
1059 and then Needs_Finalization (R_Type)
1060 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1063 Make_Block_Statement (Loc,
1064 Handled_Statement_Sequence =>
1065 Make_Handled_Sequence_Of_Statements (Loc,
1066 Statements => New_List (Relocate_Node (N)))));
1074 -- Ada 2005 (AI-251): If the type of the returned object is
1075 -- an access to an interface type then we add an implicit type
1076 -- conversion to force the displacement of the "this" pointer to
1077 -- reference the secondary dispatch table. We cannot delay the
1078 -- generation of this implicit conversion until the expansion
1079 -- because in this case the type resolution changes the decoration
1080 -- of the expression node to match R_Type; by contrast, if the
1081 -- returned object is a class-wide interface type then it is too
1082 -- early to generate here the implicit conversion since the return
1083 -- statement may be rewritten by the expander into an extended
1084 -- return statement whose expansion takes care of adding the
1085 -- implicit type conversion to displace the pointer to the object.
1088 and then Serious_Errors_Detected = 0
1089 and then Is_Access_Type (R_Type)
1090 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1091 and then Is_Interface (Designated_Type (R_Type))
1092 and then Is_Progenitor (Designated_Type (R_Type),
1093 Designated_Type (Etype (Expr)))
1095 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1099 Resolve (Expr, R_Type);
1100 Check_Limited_Return (N, Expr, R_Type);
1102 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
1103 Check_Aggregate_Accessibility (Expr);
1107 -- RETURN only allowed in SPARK as the last statement in function
1109 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1111 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
1112 or else Present (Next (N)))
1114 Check_SPARK_05_Restriction
1115 ("RETURN should be the last statement in function", N);
1119 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
1120 Obj_Decl := Last (Return_Object_Declarations (N));
1122 -- Analyze parts specific to extended_return_statement:
1125 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1126 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1129 Expr := Expression (Obj_Decl);
1131 -- Note: The check for OK_For_Limited_Init will happen in
1132 -- Analyze_Object_Declaration; we treat it as a normal
1133 -- object declaration.
1135 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1138 Check_Return_Subtype_Indication (Obj_Decl);
1140 if Present (HSS) then
1143 if Present (Exception_Handlers (HSS)) then
1145 -- ???Has_Nested_Block_With_Handler needs to be set.
1146 -- Probably by creating an actual N_Block_Statement.
1147 -- Probably in Expand.
1153 -- Mark the return object as referenced, since the return is an
1154 -- implicit reference of the object.
1156 Set_Referenced (Defining_Identifier (Obj_Decl));
1158 Check_References (Stm_Entity);
1160 -- Check RM 6.5 (5.9/3)
1163 if Ada_Version < Ada_2012 then
1165 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1166 -- Can it really happen (extended return???)
1169 ("aliased only allowed for limited return objects "
1170 & "in Ada 2012??", N);
1172 elsif not Is_Limited_View (R_Type) then
1174 ("aliased only allowed for limited return objects", N);
1180 -- Case of Expr present
1184 -- Defend against previous errors
1186 and then Nkind (Expr) /= N_Empty
1187 and then Present (Etype (Expr))
1189 -- Apply constraint check. Note that this is done before the implicit
1190 -- conversion of the expression done for anonymous access types to
1191 -- ensure correct generation of the null-excluding check associated
1192 -- with null-excluding expressions found in return statements.
1194 Apply_Constraint_Check (Expr, R_Type);
1196 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1197 -- type, apply an implicit conversion of the expression to that type
1198 -- to force appropriate static and run-time accessibility checks.
1200 if Ada_Version >= Ada_2005
1201 and then Ekind (R_Type) = E_Anonymous_Access_Type
1203 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1204 Analyze_And_Resolve (Expr, R_Type);
1206 -- If this is a local anonymous access to subprogram, the
1207 -- accessibility check can be applied statically. The return is
1208 -- illegal if the access type of the return expression is declared
1209 -- inside of the subprogram (except if it is the subtype indication
1210 -- of an extended return statement).
1212 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1213 if not Comes_From_Source (Current_Scope)
1214 or else Ekind (Current_Scope) = E_Return_Statement
1219 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1221 Error_Msg_N ("cannot return local access to subprogram", N);
1224 -- The expression cannot be of a formal incomplete type
1226 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1227 and then Is_Generic_Type (Etype (Expr))
1230 ("cannot return expression of a formal incomplete type", N);
1233 -- If the result type is class-wide, then check that the return
1234 -- expression's type is not declared at a deeper level than the
1235 -- function (RM05-6.5(5.6/2)).
1237 if Ada_Version >= Ada_2005
1238 and then Is_Class_Wide_Type (R_Type)
1240 if Type_Access_Level (Etype (Expr)) >
1241 Subprogram_Access_Level (Scope_Id)
1244 ("level of return expression type is deeper than "
1245 & "class-wide function!", Expr);
1249 -- Check incorrect use of dynamically tagged expression
1251 if Is_Tagged_Type (R_Type) then
1252 Check_Dynamically_Tagged_Expression
1258 -- ??? A real run-time accessibility check is needed in cases
1259 -- involving dereferences of access parameters. For now we just
1260 -- check the static cases.
1262 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1263 and then Is_Limited_View (Etype (Scope_Id))
1264 and then Object_Access_Level (Expr) >
1265 Subprogram_Access_Level (Scope_Id)
1267 -- Suppress the message in a generic, where the rewriting
1270 if Inside_A_Generic then
1275 Make_Raise_Program_Error (Loc,
1276 Reason => PE_Accessibility_Check_Failed));
1279 Error_Msg_Warn := SPARK_Mode /= On;
1280 Error_Msg_N ("cannot return a local value by reference<<", N);
1281 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1285 if Known_Null (Expr)
1286 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1287 and then Null_Exclusion_Present (Parent (Scope_Id))
1289 Apply_Compile_Time_Constraint_Error
1291 Msg => "(Ada 2005) null not allowed for "
1292 & "null-excluding return??",
1293 Reason => CE_Null_Not_Allowed);
1296 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1297 -- has no initializing expression.
1299 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1300 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1301 Subprogram_Access_Level (Scope_Id)
1304 ("level of return expression type is deeper than "
1305 & "class-wide function!", Obj_Decl);
1308 end Analyze_Function_Return;
1310 -------------------------------------
1311 -- Analyze_Generic_Subprogram_Body --
1312 -------------------------------------
1314 procedure Analyze_Generic_Subprogram_Body
1318 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1319 Kind : constant Entity_Kind := Ekind (Gen_Id);
1320 Body_Id : Entity_Id;
1325 -- Copy body and disable expansion while analyzing the generic For a
1326 -- stub, do not copy the stub (which would load the proper body), this
1327 -- will be done when the proper body is analyzed.
1329 if Nkind (N) /= N_Subprogram_Body_Stub then
1330 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1333 -- Once the contents of the generic copy and the template are
1334 -- swapped, do the same for their respective aspect specifications.
1336 Exchange_Aspects (N, New_N);
1338 -- Collect all contract-related source pragmas found within the
1339 -- template and attach them to the contract of the subprogram body.
1340 -- This contract is used in the capture of global references within
1343 Create_Generic_Contract (N);
1348 Spec := Specification (N);
1350 -- Within the body of the generic, the subprogram is callable, and
1351 -- behaves like the corresponding non-generic unit.
1353 Body_Id := Defining_Entity (Spec);
1355 if Kind = E_Generic_Procedure
1356 and then Nkind (Spec) /= N_Procedure_Specification
1358 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1361 elsif Kind = E_Generic_Function
1362 and then Nkind (Spec) /= N_Function_Specification
1364 Error_Msg_N ("invalid body for generic function ", Body_Id);
1368 Set_Corresponding_Body (Gen_Decl, Body_Id);
1370 if Has_Completion (Gen_Id)
1371 and then Nkind (Parent (N)) /= N_Subunit
1373 Error_Msg_N ("duplicate generic body", N);
1376 Set_Has_Completion (Gen_Id);
1379 if Nkind (N) = N_Subprogram_Body_Stub then
1380 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1382 Set_Corresponding_Spec (N, Gen_Id);
1385 if Nkind (Parent (N)) = N_Compilation_Unit then
1386 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1389 -- Make generic parameters immediately visible in the body. They are
1390 -- needed to process the formals declarations. Then make the formals
1391 -- visible in a separate step.
1393 Push_Scope (Gen_Id);
1397 First_Ent : Entity_Id;
1400 First_Ent := First_Entity (Gen_Id);
1403 while Present (E) and then not Is_Formal (E) loop
1408 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1410 -- Now generic formals are visible, and the specification can be
1411 -- analyzed, for subsequent conformance check.
1413 Body_Id := Analyze_Subprogram_Specification (Spec);
1415 -- Make formal parameters visible
1419 -- E is the first formal parameter, we loop through the formals
1420 -- installing them so that they will be visible.
1422 Set_First_Entity (Gen_Id, E);
1423 while Present (E) loop
1429 -- Visible generic entity is callable within its own body
1431 Set_Ekind (Gen_Id, Ekind (Body_Id));
1432 Set_Ekind (Body_Id, E_Subprogram_Body);
1433 Set_Convention (Body_Id, Convention (Gen_Id));
1434 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1435 Set_Scope (Body_Id, Scope (Gen_Id));
1437 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1439 if Nkind (N) = N_Subprogram_Body_Stub then
1441 -- No body to analyze, so restore state of generic unit
1443 Set_Ekind (Gen_Id, Kind);
1444 Set_Ekind (Body_Id, Kind);
1446 if Present (First_Ent) then
1447 Set_First_Entity (Gen_Id, First_Ent);
1454 -- If this is a compilation unit, it must be made visible explicitly,
1455 -- because the compilation of the declaration, unlike other library
1456 -- unit declarations, does not. If it is not a unit, the following
1457 -- is redundant but harmless.
1459 Set_Is_Immediately_Visible (Gen_Id);
1460 Reference_Body_Formals (Gen_Id, Body_Id);
1462 if Is_Child_Unit (Gen_Id) then
1463 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1466 Set_Actual_Subtypes (N, Current_Scope);
1468 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1469 Set_SPARK_Pragma_Inherited (Body_Id);
1471 -- Analyze any aspect specifications that appear on the generic
1474 if Has_Aspects (N) then
1475 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1478 Analyze_Declarations (Declarations (N));
1481 -- Process the contract of the subprogram body after all declarations
1482 -- have been analyzed. This ensures that any contract-related pragmas
1483 -- are available through the N_Contract node of the body.
1485 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1487 Analyze (Handled_Statement_Sequence (N));
1488 Save_Global_References (Original_Node (N));
1490 -- Prior to exiting the scope, include generic formals again (if any
1491 -- are present) in the set of local entities.
1493 if Present (First_Ent) then
1494 Set_First_Entity (Gen_Id, First_Ent);
1497 Check_References (Gen_Id);
1500 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1501 Update_Use_Clause_Chain;
1503 Check_Subprogram_Order (N);
1505 -- Outside of its body, unit is generic again
1507 Set_Ekind (Gen_Id, Kind);
1508 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1511 Style.Check_Identifier (Body_Id, Gen_Id);
1515 end Analyze_Generic_Subprogram_Body;
1517 ----------------------------
1518 -- Analyze_Null_Procedure --
1519 ----------------------------
1521 procedure Analyze_Null_Procedure
1523 Is_Completion : out Boolean)
1525 Loc : constant Source_Ptr := Sloc (N);
1526 Spec : constant Node_Id := Specification (N);
1527 Designator : Entity_Id;
1529 Null_Body : Node_Id := Empty;
1530 Null_Stmt : Node_Id := Null_Statement (Spec);
1534 -- Capture the profile of the null procedure before analysis, for
1535 -- expansion at the freeze point and at each point of call. The body is
1536 -- used if the procedure has preconditions, or if it is a completion. In
1537 -- the first case the body is analyzed at the freeze point, in the other
1538 -- it replaces the null procedure declaration.
1540 -- For a null procedure that comes from source, a NULL statement is
1541 -- provided by the parser, which carries the source location of the
1542 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1543 -- from expansion, create one now.
1545 if No (Null_Stmt) then
1546 Null_Stmt := Make_Null_Statement (Loc);
1550 Make_Subprogram_Body (Loc,
1551 Specification => New_Copy_Tree (Spec),
1552 Declarations => New_List,
1553 Handled_Statement_Sequence =>
1554 Make_Handled_Sequence_Of_Statements (Loc,
1555 Statements => New_List (Null_Stmt)));
1557 -- Create new entities for body and formals
1559 Set_Defining_Unit_Name (Specification (Null_Body),
1560 Make_Defining_Identifier
1561 (Sloc (Defining_Entity (N)),
1562 Chars (Defining_Entity (N))));
1564 Form := First (Parameter_Specifications (Specification (Null_Body)));
1565 while Present (Form) loop
1566 Set_Defining_Identifier (Form,
1567 Make_Defining_Identifier
1568 (Sloc (Defining_Identifier (Form)),
1569 Chars (Defining_Identifier (Form))));
1573 -- Determine whether the null procedure may be a completion of a generic
1574 -- suprogram, in which case we use the new null body as the completion
1575 -- and set minimal semantic information on the original declaration,
1576 -- which is rewritten as a null statement.
1578 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1580 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1581 Insert_Before (N, Null_Body);
1582 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1584 Rewrite (N, Make_Null_Statement (Loc));
1585 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1586 Is_Completion := True;
1590 -- Resolve the types of the formals now, because the freeze point may
1591 -- appear in a different context, e.g. an instantiation.
1593 Form := First (Parameter_Specifications (Specification (Null_Body)));
1594 while Present (Form) loop
1595 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1596 Find_Type (Parameter_Type (Form));
1598 elsif No (Access_To_Subprogram_Definition
1599 (Parameter_Type (Form)))
1601 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1603 -- The case of a null procedure with a formal that is an
1604 -- access-to-subprogram type, and that is used as an actual
1605 -- in an instantiation is left to the enthusiastic reader.
1615 -- If there are previous overloadable entities with the same name, check
1616 -- whether any of them is completed by the null procedure.
1618 if Present (Prev) and then Is_Overloadable (Prev) then
1619 Designator := Analyze_Subprogram_Specification (Spec);
1620 Prev := Find_Corresponding_Spec (N);
1623 if No (Prev) or else not Comes_From_Source (Prev) then
1624 Designator := Analyze_Subprogram_Specification (Spec);
1625 Set_Has_Completion (Designator);
1627 -- Signal to caller that this is a procedure declaration
1629 Is_Completion := False;
1631 -- Null procedures are always inlined, but generic formal subprograms
1632 -- which appear as such in the internal instance of formal packages,
1633 -- need no completion and are not marked Inline.
1636 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1638 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1639 Set_Body_To_Inline (N, Null_Body);
1640 Set_Is_Inlined (Designator);
1644 -- The null procedure is a completion. We unconditionally rewrite
1645 -- this as a null body (even if expansion is not active), because
1646 -- there are various error checks that are applied on this body
1647 -- when it is analyzed (e.g. correct aspect placement).
1649 if Has_Completion (Prev) then
1650 Error_Msg_Sloc := Sloc (Prev);
1651 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1654 Check_Previous_Null_Procedure (N, Prev);
1656 Is_Completion := True;
1657 Rewrite (N, Null_Body);
1660 end Analyze_Null_Procedure;
1662 -----------------------------
1663 -- Analyze_Operator_Symbol --
1664 -----------------------------
1666 -- An operator symbol such as "+" or "and" may appear in context where the
1667 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1668 -- is just a string, as in (conjunction = "or"). In these cases the parser
1669 -- generates this node, and the semantics does the disambiguation. Other
1670 -- such case are actuals in an instantiation, the generic unit in an
1671 -- instantiation, and pragma arguments.
1673 procedure Analyze_Operator_Symbol (N : Node_Id) is
1674 Par : constant Node_Id := Parent (N);
1677 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1678 or else Nkind (Par) = N_Function_Instantiation
1679 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1680 or else (Nkind (Par) = N_Pragma_Argument_Association
1681 and then not Is_Pragma_String_Literal (Par))
1682 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1683 or else (Nkind (Par) = N_Attribute_Reference
1684 and then Attribute_Name (Par) /= Name_Value)
1686 Find_Direct_Name (N);
1689 Change_Operator_Symbol_To_String_Literal (N);
1692 end Analyze_Operator_Symbol;
1694 -----------------------------------
1695 -- Analyze_Parameter_Association --
1696 -----------------------------------
1698 procedure Analyze_Parameter_Association (N : Node_Id) is
1700 Analyze (Explicit_Actual_Parameter (N));
1701 end Analyze_Parameter_Association;
1703 ----------------------------
1704 -- Analyze_Procedure_Call --
1705 ----------------------------
1707 -- WARNING: This routine manages Ghost regions. Return statements must be
1708 -- replaced by gotos which jump to the end of the routine and restore the
1711 procedure Analyze_Procedure_Call (N : Node_Id) is
1712 procedure Analyze_Call_And_Resolve;
1713 -- Do Analyze and Resolve calls for procedure call. At the end, check
1714 -- for illegal order dependence.
1715 -- ??? where is the check for illegal order dependencies?
1717 ------------------------------
1718 -- Analyze_Call_And_Resolve --
1719 ------------------------------
1721 procedure Analyze_Call_And_Resolve is
1723 if Nkind (N) = N_Procedure_Call_Statement then
1725 Resolve (N, Standard_Void_Type);
1729 end Analyze_Call_And_Resolve;
1733 Actuals : constant List_Id := Parameter_Associations (N);
1734 Loc : constant Source_Ptr := Sloc (N);
1735 P : constant Node_Id := Name (N);
1737 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1738 -- Save the Ghost mode to restore on exit
1743 -- Start of processing for Analyze_Procedure_Call
1746 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1747 -- a procedure call or an entry call. The prefix may denote an access
1748 -- to subprogram type, in which case an implicit dereference applies.
1749 -- If the prefix is an indexed component (without implicit dereference)
1750 -- then the construct denotes a call to a member of an entire family.
1751 -- If the prefix is a simple name, it may still denote a call to a
1752 -- parameterless member of an entry family. Resolution of these various
1753 -- interpretations is delicate.
1755 -- Do not analyze machine code statements to avoid rejecting them in
1758 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1759 Set_Etype (P, Standard_Void_Type);
1764 -- If this is a call of the form Obj.Op, the call may have been analyzed
1765 -- and possibly rewritten into a block, in which case we are done.
1767 if Analyzed (N) then
1771 -- If there is an error analyzing the name (which may have been
1772 -- rewritten if the original call was in prefix notation) then error
1773 -- has been emitted already, mark node and return.
1775 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1776 Set_Etype (N, Any_Type);
1780 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1781 -- Set the mode now to ensure that any nodes generated during analysis
1782 -- and expansion are properly marked as Ghost.
1784 Mark_And_Set_Ghost_Procedure_Call (N);
1786 -- Otherwise analyze the parameters
1788 if Present (Actuals) then
1789 Actual := First (Actuals);
1791 while Present (Actual) loop
1793 Check_Parameterless_Call (Actual);
1798 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1800 if Nkind (P) = N_Attribute_Reference
1801 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1803 Name_Elab_Subp_Body)
1805 if Present (Actuals) then
1807 ("no parameters allowed for this call", First (Actuals));
1811 Set_Etype (N, Standard_Void_Type);
1814 elsif Is_Entity_Name (P)
1815 and then Is_Record_Type (Etype (Entity (P)))
1816 and then Remote_AST_I_Dereference (P)
1820 elsif Is_Entity_Name (P)
1821 and then Ekind (Entity (P)) /= E_Entry_Family
1823 if Is_Access_Type (Etype (P))
1824 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1825 and then No (Actuals)
1826 and then Comes_From_Source (N)
1828 Error_Msg_N ("missing explicit dereference in call", N);
1831 Analyze_Call_And_Resolve;
1833 -- If the prefix is the simple name of an entry family, this is a
1834 -- parameterless call from within the task body itself.
1836 elsif Is_Entity_Name (P)
1837 and then Nkind (P) = N_Identifier
1838 and then Ekind (Entity (P)) = E_Entry_Family
1839 and then Present (Actuals)
1840 and then No (Next (First (Actuals)))
1842 -- Can be call to parameterless entry family. What appears to be the
1843 -- sole argument is in fact the entry index. Rewrite prefix of node
1844 -- accordingly. Source representation is unchanged by this
1848 Make_Indexed_Component (Loc,
1850 Make_Selected_Component (Loc,
1851 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1852 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1853 Expressions => Actuals);
1854 Set_Name (N, New_N);
1855 Set_Etype (New_N, Standard_Void_Type);
1856 Set_Parameter_Associations (N, No_List);
1857 Analyze_Call_And_Resolve;
1859 elsif Nkind (P) = N_Explicit_Dereference then
1860 if Ekind (Etype (P)) = E_Subprogram_Type then
1861 Analyze_Call_And_Resolve;
1863 Error_Msg_N ("expect access to procedure in call", P);
1866 -- The name can be a selected component or an indexed component that
1867 -- yields an access to subprogram. Such a prefix is legal if the call
1868 -- has parameter associations.
1870 elsif Is_Access_Type (Etype (P))
1871 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1873 if Present (Actuals) then
1874 Analyze_Call_And_Resolve;
1876 Error_Msg_N ("missing explicit dereference in call ", N);
1879 -- If not an access to subprogram, then the prefix must resolve to the
1880 -- name of an entry, entry family, or protected operation.
1882 -- For the case of a simple entry call, P is a selected component where
1883 -- the prefix is the task and the selector name is the entry. A call to
1884 -- a protected procedure will have the same syntax. If the protected
1885 -- object contains overloaded operations, the entity may appear as a
1886 -- function, the context will select the operation whose type is Void.
1888 elsif Nkind (P) = N_Selected_Component
1889 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1893 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1894 -- in prefix notation may still be missing its controlling argument,
1895 -- so perform the transformation now.
1897 if SPARK_Mode = On and then In_Inlined_Body then
1899 Subp : constant Entity_Id := Entity (Selector_Name (P));
1900 Typ : constant Entity_Id := Etype (Prefix (P));
1903 if Is_Tagged_Type (Typ)
1904 and then Present (First_Formal (Subp))
1905 and then Etype (First_Formal (Subp)) = Typ
1906 and then Try_Object_Operation (P)
1911 Analyze_Call_And_Resolve;
1916 Analyze_Call_And_Resolve;
1919 elsif Nkind (P) = N_Selected_Component
1920 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1921 and then Present (Actuals)
1922 and then No (Next (First (Actuals)))
1924 -- Can be call to parameterless entry family. What appears to be the
1925 -- sole argument is in fact the entry index. Rewrite prefix of node
1926 -- accordingly. Source representation is unchanged by this
1930 Make_Indexed_Component (Loc,
1931 Prefix => New_Copy (P),
1932 Expressions => Actuals);
1933 Set_Name (N, New_N);
1934 Set_Etype (New_N, Standard_Void_Type);
1935 Set_Parameter_Associations (N, No_List);
1936 Analyze_Call_And_Resolve;
1938 -- For the case of a reference to an element of an entry family, P is
1939 -- an indexed component whose prefix is a selected component (task and
1940 -- entry family), and whose index is the entry family index.
1942 elsif Nkind (P) = N_Indexed_Component
1943 and then Nkind (Prefix (P)) = N_Selected_Component
1944 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1946 Analyze_Call_And_Resolve;
1948 -- If the prefix is the name of an entry family, it is a call from
1949 -- within the task body itself.
1951 elsif Nkind (P) = N_Indexed_Component
1952 and then Nkind (Prefix (P)) = N_Identifier
1953 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1956 Make_Selected_Component (Loc,
1957 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1958 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1959 Rewrite (Prefix (P), New_N);
1961 Analyze_Call_And_Resolve;
1963 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1964 -- procedure name, so the construct can only be a qualified expression.
1966 elsif Nkind (P) = N_Qualified_Expression
1967 and then Ada_Version >= Ada_2012
1969 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1972 -- Anything else is an error
1975 Error_Msg_N ("invalid procedure or entry call", N);
1979 Restore_Ghost_Mode (Saved_GM);
1980 end Analyze_Procedure_Call;
1982 ------------------------------
1983 -- Analyze_Return_Statement --
1984 ------------------------------
1986 procedure Analyze_Return_Statement (N : Node_Id) is
1987 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
1988 N_Simple_Return_Statement));
1990 Returns_Object : constant Boolean :=
1991 Nkind (N) = N_Extended_Return_Statement
1993 (Nkind (N) = N_Simple_Return_Statement
1994 and then Present (Expression (N)));
1995 -- True if we're returning something; that is, "return <expression>;"
1996 -- or "return Result : T [:= ...]". False for "return;". Used for error
1997 -- checking: If Returns_Object is True, N should apply to a function
1998 -- body; otherwise N should apply to a procedure body, entry body,
1999 -- accept statement, or extended return statement.
2001 function Find_What_It_Applies_To return Entity_Id;
2002 -- Find the entity representing the innermost enclosing body, accept
2003 -- statement, or extended return statement. If the result is a callable
2004 -- construct or extended return statement, then this will be the value
2005 -- of the Return_Applies_To attribute. Otherwise, the program is
2006 -- illegal. See RM-6.5(4/2).
2008 -----------------------------
2009 -- Find_What_It_Applies_To --
2010 -----------------------------
2012 function Find_What_It_Applies_To return Entity_Id is
2013 Result : Entity_Id := Empty;
2016 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2017 -- and postconditions.
2019 for J in reverse 0 .. Scope_Stack.Last loop
2020 Result := Scope_Stack.Table (J).Entity;
2021 exit when not Ekind_In (Result, E_Block, E_Loop)
2022 and then Chars (Result) /= Name_uPostconditions;
2025 pragma Assert (Present (Result));
2027 end Find_What_It_Applies_To;
2029 -- Local declarations
2031 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2032 Kind : constant Entity_Kind := Ekind (Scope_Id);
2033 Loc : constant Source_Ptr := Sloc (N);
2034 Stm_Entity : constant Entity_Id :=
2036 (E_Return_Statement, Current_Scope, Loc, 'R');
2038 -- Start of processing for Analyze_Return_Statement
2041 Set_Return_Statement_Entity (N, Stm_Entity);
2043 Set_Etype (Stm_Entity, Standard_Void_Type);
2044 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2046 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2047 -- (4/2): an inner return statement will apply to this extended return.
2049 if Nkind (N) = N_Extended_Return_Statement then
2050 Push_Scope (Stm_Entity);
2053 -- Check that pragma No_Return is obeyed. Don't complain about the
2054 -- implicitly-generated return that is placed at the end.
2056 if No_Return (Scope_Id) and then Comes_From_Source (N) then
2057 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
2060 -- Warn on any unassigned OUT parameters if in procedure
2062 if Ekind (Scope_Id) = E_Procedure then
2063 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2066 -- Check that functions return objects, and other things do not
2068 if Kind = E_Function or else Kind = E_Generic_Function then
2069 if not Returns_Object then
2070 Error_Msg_N ("missing expression in return from function", N);
2073 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
2074 if Returns_Object then
2075 Error_Msg_N ("procedure cannot return value (use function)", N);
2078 elsif Kind = E_Entry or else Kind = E_Entry_Family then
2079 if Returns_Object then
2080 if Is_Protected_Type (Scope (Scope_Id)) then
2081 Error_Msg_N ("entry body cannot return value", N);
2083 Error_Msg_N ("accept statement cannot return value", N);
2087 elsif Kind = E_Return_Statement then
2089 -- We are nested within another return statement, which must be an
2090 -- extended_return_statement.
2092 if Returns_Object then
2093 if Nkind (N) = N_Extended_Return_Statement then
2095 ("extended return statement cannot be nested (use `RETURN;`)",
2098 -- Case of a simple return statement with a value inside extended
2099 -- return statement.
2103 ("return nested in extended return statement cannot return "
2104 & "value (use `RETURN;`)", N);
2109 Error_Msg_N ("illegal context for return statement", N);
2112 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2113 Analyze_Function_Return (N);
2115 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2116 Set_Return_Present (Scope_Id);
2119 if Nkind (N) = N_Extended_Return_Statement then
2123 Kill_Current_Values (Last_Assignment_Only => True);
2124 Check_Unreachable_Code (N);
2126 Analyze_Dimension (N);
2127 end Analyze_Return_Statement;
2129 -------------------------------------
2130 -- Analyze_Simple_Return_Statement --
2131 -------------------------------------
2133 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2135 if Present (Expression (N)) then
2136 Mark_Coextensions (N, Expression (N));
2139 Analyze_Return_Statement (N);
2140 end Analyze_Simple_Return_Statement;
2142 -------------------------
2143 -- Analyze_Return_Type --
2144 -------------------------
2146 procedure Analyze_Return_Type (N : Node_Id) is
2147 Designator : constant Entity_Id := Defining_Entity (N);
2148 Typ : Entity_Id := Empty;
2151 -- Normal case where result definition does not indicate an error
2153 if Result_Definition (N) /= Error then
2154 if Nkind (Result_Definition (N)) = N_Access_Definition then
2155 Check_SPARK_05_Restriction
2156 ("access result is not allowed", Result_Definition (N));
2158 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2161 AD : constant Node_Id :=
2162 Access_To_Subprogram_Definition (Result_Definition (N));
2164 if Present (AD) and then Protected_Present (AD) then
2165 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2167 Typ := Access_Definition (N, Result_Definition (N));
2171 Set_Parent (Typ, Result_Definition (N));
2172 Set_Is_Local_Anonymous_Access (Typ);
2173 Set_Etype (Designator, Typ);
2175 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2177 Null_Exclusion_Static_Checks (N);
2179 -- Subtype_Mark case
2182 Find_Type (Result_Definition (N));
2183 Typ := Entity (Result_Definition (N));
2184 Set_Etype (Designator, Typ);
2186 -- Unconstrained array as result is not allowed in SPARK
2188 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2189 Check_SPARK_05_Restriction
2190 ("returning an unconstrained array is not allowed",
2191 Result_Definition (N));
2194 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2196 Null_Exclusion_Static_Checks (N);
2198 -- If a null exclusion is imposed on the result type, then create
2199 -- a null-excluding itype (an access subtype) and use it as the
2200 -- function's Etype. Note that the null exclusion checks are done
2201 -- right before this, because they don't get applied to types that
2202 -- do not come from source.
2204 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2205 Set_Etype (Designator,
2206 Create_Null_Excluding_Itype
2209 Scope_Id => Scope (Current_Scope)));
2211 -- The new subtype must be elaborated before use because
2212 -- it is visible outside of the function. However its base
2213 -- type may not be frozen yet, so the reference that will
2214 -- force elaboration must be attached to the freezing of
2217 -- If the return specification appears on a proper body,
2218 -- the subtype will have been created already on the spec.
2220 if Is_Frozen (Typ) then
2221 if Nkind (Parent (N)) = N_Subprogram_Body
2222 and then Nkind (Parent (Parent (N))) = N_Subunit
2226 Build_Itype_Reference (Etype (Designator), Parent (N));
2230 Ensure_Freeze_Node (Typ);
2233 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2235 Set_Itype (IR, Etype (Designator));
2236 Append_Freeze_Actions (Typ, New_List (IR));
2241 Set_Etype (Designator, Typ);
2244 if Ekind (Typ) = E_Incomplete_Type
2245 or else (Is_Class_Wide_Type (Typ)
2246 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2248 -- AI05-0151: Tagged incomplete types are allowed in all formal
2249 -- parts. Untagged incomplete types are not allowed in bodies.
2250 -- As a consequence, limited views cannot appear in a basic
2251 -- declaration that is itself within a body, because there is
2252 -- no point at which the non-limited view will become visible.
2254 if Ada_Version >= Ada_2012 then
2255 if From_Limited_With (Typ) and then In_Package_Body then
2257 ("invalid use of incomplete type&",
2258 Result_Definition (N), Typ);
2260 -- The return type of a subprogram body cannot be of a
2261 -- formal incomplete type.
2263 elsif Is_Generic_Type (Typ)
2264 and then Nkind (Parent (N)) = N_Subprogram_Body
2267 ("return type cannot be a formal incomplete type",
2268 Result_Definition (N));
2270 elsif Is_Class_Wide_Type (Typ)
2271 and then Is_Generic_Type (Root_Type (Typ))
2272 and then Nkind (Parent (N)) = N_Subprogram_Body
2275 ("return type cannot be a formal incomplete type",
2276 Result_Definition (N));
2278 elsif Is_Tagged_Type (Typ) then
2281 -- Use is legal in a thunk generated for an operation
2282 -- inherited from a progenitor.
2284 elsif Is_Thunk (Designator)
2285 and then Present (Non_Limited_View (Typ))
2289 elsif Nkind (Parent (N)) = N_Subprogram_Body
2290 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2294 ("invalid use of untagged incomplete type&",
2298 -- The type must be completed in the current package. This
2299 -- is checked at the end of the package declaration when
2300 -- Taft-amendment types are identified. If the return type
2301 -- is class-wide, there is no required check, the type can
2302 -- be a bona fide TAT.
2304 if Ekind (Scope (Current_Scope)) = E_Package
2305 and then In_Private_Part (Scope (Current_Scope))
2306 and then not Is_Class_Wide_Type (Typ)
2308 Append_Elmt (Designator, Private_Dependents (Typ));
2313 ("invalid use of incomplete type&", Designator, Typ);
2318 -- Case where result definition does indicate an error
2321 Set_Etype (Designator, Any_Type);
2323 end Analyze_Return_Type;
2325 -----------------------------
2326 -- Analyze_Subprogram_Body --
2327 -----------------------------
2329 procedure Analyze_Subprogram_Body (N : Node_Id) is
2330 Loc : constant Source_Ptr := Sloc (N);
2331 Body_Spec : constant Node_Id := Specification (N);
2332 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2335 if Debug_Flag_C then
2336 Write_Str ("==> subprogram body ");
2337 Write_Name (Chars (Body_Id));
2338 Write_Str (" from ");
2339 Write_Location (Loc);
2344 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2346 -- The real work is split out into the helper, so it can do "return;"
2347 -- without skipping the debug output:
2349 Analyze_Subprogram_Body_Helper (N);
2351 if Debug_Flag_C then
2353 Write_Str ("<== subprogram body ");
2354 Write_Name (Chars (Body_Id));
2355 Write_Str (" from ");
2356 Write_Location (Loc);
2359 end Analyze_Subprogram_Body;
2361 ------------------------------------
2362 -- Analyze_Subprogram_Body_Helper --
2363 ------------------------------------
2365 -- This procedure is called for regular subprogram bodies, generic bodies,
2366 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2367 -- specification matters, and is used to create a proper declaration for
2368 -- the subprogram, or to perform conformance checks.
2370 -- WARNING: This routine manages Ghost regions. Return statements must be
2371 -- replaced by gotos which jump to the end of the routine and restore the
2374 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2375 Body_Spec : Node_Id := Specification (N);
2376 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2377 Loc : constant Source_Ptr := Sloc (N);
2378 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2380 Conformant : Boolean;
2381 Desig_View : Entity_Id := Empty;
2382 Exch_Views : Elist_Id := No_Elist;
2384 Mask_Types : Elist_Id := No_Elist;
2385 Prot_Typ : Entity_Id := Empty;
2386 Spec_Decl : Node_Id := Empty;
2387 Spec_Id : Entity_Id;
2389 Last_Real_Spec_Entity : Entity_Id := Empty;
2390 -- When we analyze a separate spec, the entity chain ends up containing
2391 -- the formals, as well as any itypes generated during analysis of the
2392 -- default expressions for parameters, or the arguments of associated
2393 -- precondition/postcondition pragmas (which are analyzed in the context
2394 -- of the spec since they have visibility on formals).
2396 -- These entities belong with the spec and not the body. However we do
2397 -- the analysis of the body in the context of the spec (again to obtain
2398 -- visibility to the formals), and all the entities generated during
2399 -- this analysis end up also chained to the entity chain of the spec.
2400 -- But they really belong to the body, and there is circuitry to move
2401 -- them from the spec to the body.
2403 -- However, when we do this move, we don't want to move the real spec
2404 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2405 -- variable points to the last real spec entity, so we only move those
2406 -- chained beyond that point. It is initialized to Empty to deal with
2407 -- the case where there is no separate spec.
2409 function Body_Has_Contract return Boolean;
2410 -- Check whether unanalyzed body has an aspect or pragma that may
2411 -- generate a SPARK contract.
2413 function Body_Has_SPARK_Mode_On return Boolean;
2414 -- Check whether SPARK_Mode On applies to the subprogram body, either
2415 -- because it is specified directly on the body, or because it is
2416 -- inherited from the enclosing subprogram or package.
2418 procedure Build_Subprogram_Declaration;
2419 -- Create a matching subprogram declaration for subprogram body N
2421 procedure Check_Anonymous_Return;
2422 -- Ada 2005: if a function returns an access type that denotes a task,
2423 -- or a type that contains tasks, we must create a master entity for
2424 -- the anonymous type, which typically will be used in an allocator
2425 -- in the body of the function.
2427 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2428 -- Look ahead to recognize a pragma that may appear after the body.
2429 -- If there is a previous spec, check that it appears in the same
2430 -- declarative part. If the pragma is Inline_Always, perform inlining
2431 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2432 -- If the body acts as a spec, and inlining is required, we create a
2433 -- subprogram declaration for it, in order to attach the body to inline.
2434 -- If pragma does not appear after the body, check whether there is
2435 -- an inline pragma before any local declarations.
2437 procedure Check_Missing_Return;
2438 -- Checks for a function with a no return statements, and also performs
2439 -- the warning checks implemented by Check_Returns. In formal mode, also
2440 -- verify that a function ends with a RETURN and that a procedure does
2441 -- not contain any RETURN.
2443 function Disambiguate_Spec return Entity_Id;
2444 -- When a primitive is declared between the private view and the full
2445 -- view of a concurrent type which implements an interface, a special
2446 -- mechanism is used to find the corresponding spec of the primitive
2449 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2450 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2451 -- incomplete types coming from a limited context and replace their
2452 -- limited views with the non-limited ones. Return the list of changes
2453 -- to be used to undo the transformation.
2455 function Is_Private_Concurrent_Primitive
2456 (Subp_Id : Entity_Id) return Boolean;
2457 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2458 -- type that implements an interface and has a private view.
2460 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2461 -- N is the body generated for an expression function that is not a
2462 -- completion and Spec_Id the defining entity of its spec. Mark all
2463 -- the not-yet-frozen types referenced by the simple return statement
2464 -- of the function as formally frozen.
2466 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2467 -- Undo the transformation done by Exchange_Limited_Views.
2469 procedure Set_Trivial_Subprogram (N : Node_Id);
2470 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2471 -- subprogram whose body is being analyzed. N is the statement node
2472 -- causing the flag to be set, if the following statement is a return
2473 -- of an entity, we mark the entity as set in source to suppress any
2474 -- warning on the stylized use of function stubs with a dummy return.
2476 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2477 -- Undo the transformation done by Mask_Unfrozen_Types
2479 procedure Verify_Overriding_Indicator;
2480 -- If there was a previous spec, the entity has been entered in the
2481 -- current scope previously. If the body itself carries an overriding
2482 -- indicator, check that it is consistent with the known status of the
2485 -----------------------
2486 -- Body_Has_Contract --
2487 -----------------------
2489 function Body_Has_Contract return Boolean is
2490 Decls : constant List_Id := Declarations (N);
2494 -- Check for aspects that may generate a contract
2496 if Present (Aspect_Specifications (N)) then
2497 Item := First (Aspect_Specifications (N));
2498 while Present (Item) loop
2499 if Is_Subprogram_Contract_Annotation (Item) then
2507 -- Check for pragmas that may generate a contract
2509 if Present (Decls) then
2510 Item := First (Decls);
2511 while Present (Item) loop
2512 if Nkind (Item) = N_Pragma
2513 and then Is_Subprogram_Contract_Annotation (Item)
2523 end Body_Has_Contract;
2525 ----------------------------
2526 -- Body_Has_SPARK_Mode_On --
2527 ----------------------------
2529 function Body_Has_SPARK_Mode_On return Boolean is
2530 Decls : constant List_Id := Declarations (N);
2534 -- Check for SPARK_Mode aspect
2536 if Present (Aspect_Specifications (N)) then
2537 Item := First (Aspect_Specifications (N));
2538 while Present (Item) loop
2539 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2540 return Get_SPARK_Mode_From_Annotation (Item) = On;
2547 -- Check for SPARK_Mode pragma
2549 if Present (Decls) then
2550 Item := First (Decls);
2551 while Present (Item) loop
2553 -- Pragmas that apply to a subprogram body are usually grouped
2554 -- together. Look for a potential pragma SPARK_Mode among them.
2556 if Nkind (Item) = N_Pragma then
2557 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2558 return Get_SPARK_Mode_From_Annotation (Item) = On;
2561 -- Otherwise the first non-pragma declarative item terminates
2562 -- the region where pragma SPARK_Mode may appear.
2572 -- Otherwise, the applicable SPARK_Mode is inherited from the
2573 -- enclosing subprogram or package.
2575 return SPARK_Mode = On;
2576 end Body_Has_SPARK_Mode_On;
2578 ----------------------------------
2579 -- Build_Subprogram_Declaration --
2580 ----------------------------------
2582 procedure Build_Subprogram_Declaration is
2583 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2584 -- Relocate certain categorization pragmas from the declarative list
2585 -- of subprogram body From and insert them after node To. The pragmas
2588 -- Volatile_Function
2589 -- Also copy pragma SPARK_Mode if present in the declarative list
2590 -- of subprogram body From and insert it after node To. This pragma
2591 -- should not be moved, as it applies to the body too.
2597 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2599 Next_Decl : Node_Id;
2602 pragma Assert (Nkind (From) = N_Subprogram_Body);
2604 -- The destination node must be part of a list, as the pragmas are
2605 -- inserted after it.
2607 pragma Assert (Is_List_Member (To));
2609 -- Inspect the declarations of the subprogram body looking for
2610 -- specific pragmas.
2612 Decl := First (Declarations (N));
2613 while Present (Decl) loop
2614 Next_Decl := Next (Decl);
2616 if Nkind (Decl) = N_Pragma then
2617 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
2618 Insert_After (To, New_Copy_Tree (Decl));
2620 elsif Nam_In (Pragma_Name_Unmapped (Decl),
2622 Name_Volatile_Function)
2625 Insert_After (To, Decl);
2636 Subp_Decl : Node_Id;
2638 -- Start of processing for Build_Subprogram_Declaration
2641 -- Create a matching subprogram spec using the profile of the body.
2642 -- The structure of the tree is identical, but has new entities for
2643 -- the defining unit name and formal parameters.
2646 Make_Subprogram_Declaration (Loc,
2647 Specification => Copy_Subprogram_Spec (Body_Spec));
2648 Set_Comes_From_Source (Subp_Decl, True);
2650 -- Relocate the aspects and relevant pragmas from the subprogram body
2651 -- to the generated spec because it acts as the initial declaration.
2653 Insert_Before (N, Subp_Decl);
2654 Move_Aspects (N, To => Subp_Decl);
2655 Move_Pragmas (N, To => Subp_Decl);
2657 -- Ensure that the generated corresponding spec and original body
2658 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2659 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2660 -- correctly set for local subprograms.
2662 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2664 Analyze (Subp_Decl);
2666 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2667 -- the body since the expander may generate calls using that entity.
2668 -- Required to ensure that Expand_Call rewrites calls to this
2669 -- function by calls to the built procedure.
2671 if Modify_Tree_For_C
2672 and then Nkind (Body_Spec) = N_Function_Specification
2674 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2676 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2677 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2678 Corresponding_Procedure
2679 (Defining_Entity (Specification (Subp_Decl))));
2682 -- Analyze any relocated source pragmas or pragmas created for aspect
2685 Decl := Next (Subp_Decl);
2686 while Present (Decl) loop
2688 -- Stop the search for pragmas once the body has been reached as
2689 -- this terminates the region where pragmas may appear.
2694 elsif Nkind (Decl) = N_Pragma then
2701 Spec_Id := Defining_Entity (Subp_Decl);
2702 Set_Corresponding_Spec (N, Spec_Id);
2704 -- Mark the generated spec as a source construct to ensure that all
2705 -- calls to it are properly registered in ALI files for GNATprove.
2707 Set_Comes_From_Source (Spec_Id, True);
2709 -- Ensure that the specs of the subprogram declaration and its body
2710 -- are identical, otherwise they will appear non-conformant due to
2711 -- rewritings in the default values of formal parameters.
2713 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2714 Set_Specification (N, Body_Spec);
2715 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2716 end Build_Subprogram_Declaration;
2718 ----------------------------
2719 -- Check_Anonymous_Return --
2720 ----------------------------
2722 procedure Check_Anonymous_Return is
2728 if Present (Spec_Id) then
2734 if Ekind (Scop) = E_Function
2735 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2736 and then not Is_Thunk (Scop)
2738 -- Skip internally built functions which handle the case of
2739 -- a null access (see Expand_Interface_Conversion)
2741 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2742 and then not Comes_From_Source (Parent (Scop)))
2744 and then (Has_Task (Designated_Type (Etype (Scop)))
2746 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2748 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2749 and then Expander_Active
2751 -- Avoid cases with no tasking support
2753 and then RTE_Available (RE_Current_Master)
2754 and then not Restriction_Active (No_Task_Hierarchy)
2757 Make_Object_Declaration (Loc,
2758 Defining_Identifier =>
2759 Make_Defining_Identifier (Loc, Name_uMaster),
2760 Constant_Present => True,
2761 Object_Definition =>
2762 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2764 Make_Explicit_Dereference (Loc,
2765 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2767 if Present (Declarations (N)) then
2768 Prepend (Decl, Declarations (N));
2770 Set_Declarations (N, New_List (Decl));
2773 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2774 Set_Has_Master_Entity (Scop);
2776 -- Now mark the containing scope as a task master
2779 while Nkind (Par) /= N_Compilation_Unit loop
2780 Par := Parent (Par);
2781 pragma Assert (Present (Par));
2783 -- If we fall off the top, we are at the outer level, and
2784 -- the environment task is our effective master, so nothing
2788 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2790 Set_Is_Task_Master (Par, True);
2795 end Check_Anonymous_Return;
2797 -------------------------
2798 -- Check_Inline_Pragma --
2799 -------------------------
2801 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2805 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2806 -- True when N is a pragma Inline or Inline_Always that applies
2807 -- to this subprogram.
2809 -----------------------
2810 -- Is_Inline_Pragma --
2811 -----------------------
2813 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2815 if Nkind (N) = N_Pragma
2817 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2818 or else (Pragma_Name_Unmapped (N) = Name_Inline
2820 (Front_End_Inlining or else Optimization_Level > 0)))
2821 and then Present (Pragma_Argument_Associations (N))
2824 Pragma_Arg : Node_Id :=
2825 Expression (First (Pragma_Argument_Associations (N)));
2827 if Nkind (Pragma_Arg) = N_Selected_Component then
2828 Pragma_Arg := Selector_Name (Pragma_Arg);
2831 return Chars (Pragma_Arg) = Chars (Body_Id);
2837 end Is_Inline_Pragma;
2839 -- Start of processing for Check_Inline_Pragma
2842 if not Expander_Active then
2846 if Is_List_Member (N)
2847 and then Present (Next (N))
2848 and then Is_Inline_Pragma (Next (N))
2852 elsif Nkind (N) /= N_Subprogram_Body_Stub
2853 and then Present (Declarations (N))
2854 and then Is_Inline_Pragma (First (Declarations (N)))
2856 Prag := First (Declarations (N));
2862 if Present (Prag) then
2863 if Present (Spec_Id) then
2864 if Is_List_Member (N)
2865 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2866 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2872 -- Create a subprogram declaration, to make treatment uniform.
2873 -- Make the sloc of the subprogram name that of the entity in
2874 -- the body, so that style checks find identical strings.
2877 Subp : constant Entity_Id :=
2878 Make_Defining_Identifier
2879 (Sloc (Body_Id), Chars (Body_Id));
2880 Decl : constant Node_Id :=
2881 Make_Subprogram_Declaration (Loc,
2883 New_Copy_Tree (Specification (N)));
2886 -- Link the body and the generated spec
2888 Set_Corresponding_Body (Decl, Body_Id);
2889 Set_Corresponding_Spec (N, Subp);
2891 Set_Defining_Unit_Name (Specification (Decl), Subp);
2893 -- To ensure proper coverage when body is inlined, indicate
2894 -- whether the subprogram comes from source.
2896 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2898 if Present (First_Formal (Body_Id)) then
2899 Plist := Copy_Parameter_List (Body_Id);
2900 Set_Parameter_Specifications
2901 (Specification (Decl), Plist);
2904 -- Move aspects to the new spec
2906 if Has_Aspects (N) then
2907 Move_Aspects (N, To => Decl);
2910 Insert_Before (N, Decl);
2913 Set_Has_Pragma_Inline (Subp);
2915 if Pragma_Name (Prag) = Name_Inline_Always then
2916 Set_Is_Inlined (Subp);
2917 Set_Has_Pragma_Inline_Always (Subp);
2920 -- Prior to copying the subprogram body to create a template
2921 -- for it for subsequent inlining, remove the pragma from
2922 -- the current body so that the copy that will produce the
2923 -- new body will start from a completely unanalyzed tree.
2925 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2926 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2933 end Check_Inline_Pragma;
2935 --------------------------
2936 -- Check_Missing_Return --
2937 --------------------------
2939 procedure Check_Missing_Return is
2941 Missing_Ret : Boolean;
2944 if Nkind (Body_Spec) = N_Function_Specification then
2945 if Present (Spec_Id) then
2951 if Return_Present (Id) then
2952 Check_Returns (HSS, 'F', Missing_Ret);
2955 Set_Has_Missing_Return (Id);
2958 -- Within a premature instantiation of a package with no body, we
2959 -- build completions of the functions therein, with a Raise
2960 -- statement. No point in complaining about a missing return in
2963 elsif Ekind (Id) = E_Function
2964 and then In_Instance
2965 and then Present (Statements (HSS))
2966 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2970 elsif Is_Generic_Subprogram (Id)
2971 or else not Is_Machine_Code_Subprogram (Id)
2973 Error_Msg_N ("missing RETURN statement in function body", N);
2976 -- If procedure with No_Return, check returns
2978 elsif Nkind (Body_Spec) = N_Procedure_Specification
2979 and then Present (Spec_Id)
2980 and then No_Return (Spec_Id)
2982 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2985 -- Special checks in SPARK mode
2987 if Nkind (Body_Spec) = N_Function_Specification then
2989 -- In SPARK mode, last statement of a function should be a return
2992 Stat : constant Node_Id := Last_Source_Statement (HSS);
2995 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2996 N_Extended_Return_Statement)
2998 Check_SPARK_05_Restriction
2999 ("last statement in function should be RETURN", Stat);
3003 -- In SPARK mode, verify that a procedure has no return
3005 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3006 if Present (Spec_Id) then
3012 -- Would be nice to point to return statement here, can we
3013 -- borrow the Check_Returns procedure here ???
3015 if Return_Present (Id) then
3016 Check_SPARK_05_Restriction
3017 ("procedure should not have RETURN", N);
3020 end Check_Missing_Return;
3022 -----------------------
3023 -- Disambiguate_Spec --
3024 -----------------------
3026 function Disambiguate_Spec return Entity_Id is
3027 Priv_Spec : Entity_Id;
3030 procedure Replace_Types (To_Corresponding : Boolean);
3031 -- Depending on the flag, replace the type of formal parameters of
3032 -- Body_Id if it is a concurrent type implementing interfaces with
3033 -- the corresponding record type or the other way around.
3035 procedure Replace_Types (To_Corresponding : Boolean) is
3037 Formal_Typ : Entity_Id;
3040 Formal := First_Formal (Body_Id);
3041 while Present (Formal) loop
3042 Formal_Typ := Etype (Formal);
3044 if Is_Class_Wide_Type (Formal_Typ) then
3045 Formal_Typ := Root_Type (Formal_Typ);
3048 -- From concurrent type to corresponding record
3050 if To_Corresponding then
3051 if Is_Concurrent_Type (Formal_Typ)
3052 and then Present (Corresponding_Record_Type (Formal_Typ))
3055 (Corresponding_Record_Type (Formal_Typ)))
3058 Corresponding_Record_Type (Formal_Typ));
3061 -- From corresponding record to concurrent type
3064 if Is_Concurrent_Record_Type (Formal_Typ)
3065 and then Present (Interfaces (Formal_Typ))
3068 Corresponding_Concurrent_Type (Formal_Typ));
3072 Next_Formal (Formal);
3076 -- Start of processing for Disambiguate_Spec
3079 -- Try to retrieve the specification of the body as is. All error
3080 -- messages are suppressed because the body may not have a spec in
3081 -- its current state.
3083 Spec_N := Find_Corresponding_Spec (N, False);
3085 -- It is possible that this is the body of a primitive declared
3086 -- between a private and a full view of a concurrent type. The
3087 -- controlling parameter of the spec carries the concurrent type,
3088 -- not the corresponding record type as transformed by Analyze_
3089 -- Subprogram_Specification. In such cases, we undo the change
3090 -- made by the analysis of the specification and try to find the
3093 -- Note that wrappers already have their corresponding specs and
3094 -- bodies set during their creation, so if the candidate spec is
3095 -- a wrapper, then we definitely need to swap all types to their
3096 -- original concurrent status.
3099 or else Is_Primitive_Wrapper (Spec_N)
3101 -- Restore all references of corresponding record types to the
3102 -- original concurrent types.
3104 Replace_Types (To_Corresponding => False);
3105 Priv_Spec := Find_Corresponding_Spec (N, False);
3107 -- The current body truly belongs to a primitive declared between
3108 -- a private and a full view. We leave the modified body as is,
3109 -- and return the true spec.
3111 if Present (Priv_Spec)
3112 and then Is_Private_Primitive (Priv_Spec)
3117 -- In case that this is some sort of error, restore the original
3118 -- state of the body.
3120 Replace_Types (To_Corresponding => True);
3124 end Disambiguate_Spec;
3126 ----------------------------
3127 -- Exchange_Limited_Views --
3128 ----------------------------
3130 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3131 Result : Elist_Id := No_Elist;
3133 procedure Detect_And_Exchange (Id : Entity_Id);
3134 -- Determine whether Id's type denotes an incomplete type associated
3135 -- with a limited with clause and exchange the limited view with the
3136 -- non-limited one when available. Note that the non-limited view
3137 -- may exist because of a with_clause in another unit in the context,
3138 -- but cannot be used because the current view of the enclosing unit
3139 -- is still a limited view.
3141 -------------------------
3142 -- Detect_And_Exchange --
3143 -------------------------
3145 procedure Detect_And_Exchange (Id : Entity_Id) is
3146 Typ : constant Entity_Id := Etype (Id);
3148 if From_Limited_With (Typ)
3149 and then Has_Non_Limited_View (Typ)
3150 and then not From_Limited_With (Scope (Typ))
3153 Result := New_Elmt_List;
3156 Prepend_Elmt (Typ, Result);
3157 Prepend_Elmt (Id, Result);
3158 Set_Etype (Id, Non_Limited_View (Typ));
3160 end Detect_And_Exchange;
3166 -- Start of processing for Exchange_Limited_Views
3169 -- Do not process subprogram bodies as they already use the non-
3170 -- limited view of types.
3172 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3176 -- Examine all formals and swap views when applicable
3178 Formal := First_Formal (Subp_Id);
3179 while Present (Formal) loop
3180 Detect_And_Exchange (Formal);
3182 Next_Formal (Formal);
3185 -- Process the return type of a function
3187 if Ekind (Subp_Id) = E_Function then
3188 Detect_And_Exchange (Subp_Id);
3192 end Exchange_Limited_Views;
3194 -------------------------------------
3195 -- Is_Private_Concurrent_Primitive --
3196 -------------------------------------
3198 function Is_Private_Concurrent_Primitive
3199 (Subp_Id : Entity_Id) return Boolean
3201 Formal_Typ : Entity_Id;
3204 if Present (First_Formal (Subp_Id)) then
3205 Formal_Typ := Etype (First_Formal (Subp_Id));
3207 if Is_Concurrent_Record_Type (Formal_Typ) then
3208 if Is_Class_Wide_Type (Formal_Typ) then
3209 Formal_Typ := Root_Type (Formal_Typ);
3212 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3215 -- The type of the first formal is a concurrent tagged type with
3219 Is_Concurrent_Type (Formal_Typ)
3220 and then Is_Tagged_Type (Formal_Typ)
3221 and then Has_Private_Declaration (Formal_Typ);
3225 end Is_Private_Concurrent_Primitive;
3227 -------------------------
3228 -- Mask_Unfrozen_Types --
3229 -------------------------
3231 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3232 Result : Elist_Id := No_Elist;
3234 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3235 -- Mask all types referenced in the subtree rooted at Node
3237 --------------------
3238 -- Mask_Type_Refs --
3239 --------------------
3241 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3242 procedure Mask_Type (Typ : Entity_Id);
3243 -- ??? what does this do?
3249 procedure Mask_Type (Typ : Entity_Id) is
3251 -- Skip Itypes created by the preanalysis
3254 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3259 if not Is_Frozen (Typ) then
3260 Set_Is_Frozen (Typ);
3261 Append_New_Elmt (Typ, Result);
3265 -- Start of processing for Mask_Type_Refs
3268 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3269 Mask_Type (Etype (Entity (Node)));
3271 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3272 Mask_Type (Scope (Entity (Node)));
3275 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3276 and then Present (Etype (Node))
3278 Mask_Type (Etype (Node));
3284 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3288 Return_Stmt : constant Node_Id :=
3289 First (Statements (Handled_Statement_Sequence (N)));
3291 -- Start of processing for Mask_Unfrozen_Types
3294 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3296 Mask_References (Expression (Return_Stmt));
3299 end Mask_Unfrozen_Types;
3301 ---------------------------
3302 -- Restore_Limited_Views --
3303 ---------------------------
3305 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3306 Elmt : Elmt_Id := First_Elmt (Restore_List);
3310 while Present (Elmt) loop
3313 Set_Etype (Id, Node (Elmt));
3316 end Restore_Limited_Views;
3318 ----------------------------
3319 -- Set_Trivial_Subprogram --
3320 ----------------------------
3322 procedure Set_Trivial_Subprogram (N : Node_Id) is
3323 Nxt : constant Node_Id := Next (N);
3326 Set_Is_Trivial_Subprogram (Body_Id);
3328 if Present (Spec_Id) then
3329 Set_Is_Trivial_Subprogram (Spec_Id);
3333 and then Nkind (Nxt) = N_Simple_Return_Statement
3334 and then No (Next (Nxt))
3335 and then Present (Expression (Nxt))
3336 and then Is_Entity_Name (Expression (Nxt))
3338 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3340 end Set_Trivial_Subprogram;
3342 ---------------------------
3343 -- Unmask_Unfrozen_Types --
3344 ---------------------------
3346 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3347 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3350 while Present (Elmt) loop
3351 Set_Is_Frozen (Node (Elmt), False);
3354 end Unmask_Unfrozen_Types;
3356 ---------------------------------
3357 -- Verify_Overriding_Indicator --
3358 ---------------------------------
3360 procedure Verify_Overriding_Indicator is
3362 if Must_Override (Body_Spec) then
3363 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3364 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3368 elsif not Present (Overridden_Operation (Spec_Id)) then
3370 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3372 -- Overriding indicators aren't allowed for protected subprogram
3373 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3374 -- this to a warning if -gnatd.E is enabled.
3376 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3377 Error_Msg_Warn := Error_To_Warning;
3379 ("<<overriding indicator not allowed for protected "
3380 & "subprogram body", Body_Spec);
3383 elsif Must_Not_Override (Body_Spec) then
3384 if Present (Overridden_Operation (Spec_Id)) then
3386 ("subprogram& overrides inherited operation",
3387 Body_Spec, Spec_Id);
3389 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3390 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3393 ("subprogram& overrides predefined operator ",
3394 Body_Spec, Spec_Id);
3396 -- Overriding indicators aren't allowed for protected subprogram
3397 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3398 -- this to a warning if -gnatd.E is enabled.
3400 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3401 Error_Msg_Warn := Error_To_Warning;
3404 ("<<overriding indicator not allowed "
3405 & "for protected subprogram body", Body_Spec);
3407 -- If this is not a primitive operation, then the overriding
3408 -- indicator is altogether illegal.
3410 elsif not Is_Primitive (Spec_Id) then
3412 ("overriding indicator only allowed "
3413 & "if subprogram is primitive", Body_Spec);
3416 -- If checking the style rule and the operation overrides, then
3417 -- issue a warning about a missing overriding_indicator. Protected
3418 -- subprogram bodies are excluded from this style checking, since
3419 -- they aren't primitives (even though their declarations can
3420 -- override) and aren't allowed to have an overriding_indicator.
3423 and then Present (Overridden_Operation (Spec_Id))
3424 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3426 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3427 Style.Missing_Overriding (N, Body_Id);
3430 and then Can_Override_Operator (Spec_Id)
3431 and then not In_Predefined_Unit (Spec_Id)
3433 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3434 Style.Missing_Overriding (N, Body_Id);
3436 end Verify_Overriding_Indicator;
3440 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3441 Saved_ISMP : constant Boolean :=
3442 Ignore_SPARK_Mode_Pragmas_In_Instance;
3443 -- Save the Ghost and SPARK mode-related data to restore on exit
3445 -- Start of processing for Analyze_Subprogram_Body_Helper
3448 -- A [generic] subprogram body "freezes" the contract of the nearest
3449 -- enclosing package body and all other contracts encountered in the
3450 -- same declarative part up to and excluding the subprogram body:
3452 -- package body Nearest_Enclosing_Package
3453 -- with Refined_State => (State => Constit)
3457 -- procedure Freezes_Enclosing_Package_Body
3458 -- with Refined_Depends => (Input => Constit) ...
3460 -- This ensures that any annotations referenced by the contract of the
3461 -- [generic] subprogram body are available. This form of "freezing" is
3462 -- decoupled from the usual Freeze_xxx mechanism because it must also
3463 -- work in the context of generics where normal freezing is disabled.
3465 -- Only bodies coming from source should cause this type of "freezing".
3466 -- Expression functions that act as bodies and complete an initial
3467 -- declaration must be included in this category, hence the use of
3470 if Comes_From_Source (Original_Node (N)) then
3471 Analyze_Previous_Contracts (N);
3474 -- Generic subprograms are handled separately. They always have a
3475 -- generic specification. Determine whether current scope has a
3476 -- previous declaration.
3478 -- If the subprogram body is defined within an instance of the same
3479 -- name, the instance appears as a package renaming, and will be hidden
3480 -- within the subprogram.
3482 if Present (Prev_Id)
3483 and then not Is_Overloadable (Prev_Id)
3484 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3485 or else Comes_From_Source (Prev_Id))
3487 if Is_Generic_Subprogram (Prev_Id) then
3490 -- A subprogram body is Ghost when it is stand alone and subject
3491 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3492 -- the mode now to ensure that any nodes generated during analysis
3493 -- and expansion are properly marked as Ghost.
3495 Mark_And_Set_Ghost_Body (N, Spec_Id);
3497 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3498 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3500 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3502 if Nkind (N) = N_Subprogram_Body then
3503 HSS := Handled_Statement_Sequence (N);
3504 Check_Missing_Return;
3509 -- Otherwise a previous entity conflicts with the subprogram name.
3510 -- Attempting to enter name will post error.
3513 Enter_Name (Body_Id);
3517 -- Non-generic case, find the subprogram declaration, if one was seen,
3518 -- or enter new overloaded entity in the current scope. If the
3519 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3520 -- part of the context of one of its subunits. No need to redo the
3523 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3527 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3529 if Nkind (N) = N_Subprogram_Body_Stub
3530 or else No (Corresponding_Spec (N))
3532 if Is_Private_Concurrent_Primitive (Body_Id) then
3533 Spec_Id := Disambiguate_Spec;
3535 -- A subprogram body is Ghost when it is stand alone and
3536 -- subject to pragma Ghost or when the corresponding spec is
3537 -- Ghost. Set the mode now to ensure that any nodes generated
3538 -- during analysis and expansion are properly marked as Ghost.
3540 Mark_And_Set_Ghost_Body (N, Spec_Id);
3543 Spec_Id := Find_Corresponding_Spec (N);
3545 -- A subprogram body is Ghost when it is stand alone and
3546 -- subject to pragma Ghost or when the corresponding spec is
3547 -- Ghost. Set the mode now to ensure that any nodes generated
3548 -- during analysis and expansion are properly marked as Ghost.
3550 Mark_And_Set_Ghost_Body (N, Spec_Id);
3552 -- In GNATprove mode, if the body has no previous spec, create
3553 -- one so that the inlining machinery can operate properly.
3554 -- Transfer aspects, if any, to the new spec, so that they
3555 -- are legal and can be processed ahead of the body.
3556 -- We make two copies of the given spec, one for the new
3557 -- declaration, and one for the body.
3559 if No (Spec_Id) and then GNATprove_Mode
3561 -- Inlining does not apply during pre-analysis of code
3563 and then Full_Analysis
3565 -- Inlining only applies to full bodies, not stubs
3567 and then Nkind (N) /= N_Subprogram_Body_Stub
3569 -- Inlining only applies to bodies in the source code, not to
3570 -- those generated by the compiler. In particular, expression
3571 -- functions, whose body is generated by the compiler, are
3572 -- treated specially by GNATprove.
3574 and then Comes_From_Source (Body_Id)
3576 -- This cannot be done for a compilation unit, which is not
3577 -- in a context where we can insert a new spec.
3579 and then Is_List_Member (N)
3581 -- Inlining only applies to subprograms without contracts,
3582 -- as a contract is a sign that GNATprove should perform a
3583 -- modular analysis of the subprogram instead of a contextual
3584 -- analysis at each call site. The same test is performed in
3585 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3586 -- here in another form (because the contract has not been
3587 -- attached to the body) to avoid front-end errors in case
3588 -- pragmas are used instead of aspects, because the
3589 -- corresponding pragmas in the body would not be transferred
3590 -- to the spec, leading to legality errors.
3592 and then not Body_Has_Contract
3593 and then not Inside_A_Generic
3595 Build_Subprogram_Declaration;
3597 -- If this is a function that returns a constrained array, and
3598 -- we are generating SPARK_For_C, create subprogram declaration
3599 -- to simplify subsequent C generation.
3602 and then Modify_Tree_For_C
3603 and then Nkind (Body_Spec) = N_Function_Specification
3604 and then Is_Array_Type (Etype (Body_Id))
3605 and then Is_Constrained (Etype (Body_Id))
3607 Build_Subprogram_Declaration;
3611 -- If this is a duplicate body, no point in analyzing it
3613 if Error_Posted (N) then
3617 -- A subprogram body should cause freezing of its own declaration,
3618 -- but if there was no previous explicit declaration, then the
3619 -- subprogram will get frozen too late (there may be code within
3620 -- the body that depends on the subprogram having been frozen,
3621 -- such as uses of extra formals), so we force it to be frozen
3622 -- here. Same holds if the body and spec are compilation units.
3623 -- Finally, if the return type is an anonymous access to protected
3624 -- subprogram, it must be frozen before the body because its
3625 -- expansion has generated an equivalent type that is used when
3626 -- elaborating the body.
3628 -- An exception in the case of Ada 2012, AI05-177: The bodies
3629 -- created for expression functions do not freeze.
3632 and then Nkind (Original_Node (N)) /= N_Expression_Function
3634 Freeze_Before (N, Body_Id);
3636 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3637 Freeze_Before (N, Spec_Id);
3639 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3640 Freeze_Before (N, Etype (Body_Id));
3644 Spec_Id := Corresponding_Spec (N);
3646 -- A subprogram body is Ghost when it is stand alone and subject
3647 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3648 -- the mode now to ensure that any nodes generated during analysis
3649 -- and expansion are properly marked as Ghost.
3651 Mark_And_Set_Ghost_Body (N, Spec_Id);
3655 -- Previously we scanned the body to look for nested subprograms, and
3656 -- rejected an inline directive if nested subprograms were present,
3657 -- because the back-end would generate conflicting symbols for the
3658 -- nested bodies. This is now unnecessary.
3660 -- Look ahead to recognize a pragma Inline that appears after the body
3662 Check_Inline_Pragma (Spec_Id);
3664 -- Deal with special case of a fully private operation in the body of
3665 -- the protected type. We must create a declaration for the subprogram,
3666 -- in order to attach the protected subprogram that will be used in
3667 -- internal calls. We exclude compiler generated bodies from the
3668 -- expander since the issue does not arise for those cases.
3671 and then Comes_From_Source (N)
3672 and then Is_Protected_Type (Current_Scope)
3674 Spec_Id := Build_Private_Protected_Declaration (N);
3677 -- If we are generating C and this is a function returning a constrained
3678 -- array type for which we must create a procedure with an extra out
3679 -- parameter, build and analyze the body now. The procedure declaration
3680 -- has already been created. We reuse the source body of the function,
3681 -- because in an instance it may contain global references that cannot
3682 -- be reanalyzed. The source function itself is not used any further,
3683 -- so we mark it as having a completion. If the subprogram is a stub the
3684 -- transformation is done later, when the proper body is analyzed.
3687 and then Modify_Tree_For_C
3688 and then Present (Spec_Id)
3689 and then Ekind (Spec_Id) = E_Function
3690 and then Nkind (N) /= N_Subprogram_Body_Stub
3691 and then Rewritten_For_C (Spec_Id)
3693 Set_Has_Completion (Spec_Id);
3695 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3698 -- The entity for the created procedure must remain invisible, so it
3699 -- does not participate in resolution of subsequent references to the
3702 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3706 -- If a separate spec is present, then deal with freezing issues
3708 if Present (Spec_Id) then
3709 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3710 Verify_Overriding_Indicator;
3712 -- In general, the spec will be frozen when we start analyzing the
3713 -- body. However, for internally generated operations, such as
3714 -- wrapper functions for inherited operations with controlling
3715 -- results, the spec may not have been frozen by the time we expand
3716 -- the freeze actions that include the bodies. In particular, extra
3717 -- formals for accessibility or for return-in-place may need to be
3718 -- generated. Freeze nodes, if any, are inserted before the current
3719 -- body. These freeze actions are also needed in ASIS mode and in
3720 -- Compile_Only mode to enable the proper back-end type annotations.
3721 -- They are necessary in any case to insure order of elaboration
3724 if not Is_Frozen (Spec_Id)
3725 and then (Expander_Active
3727 or else (Operating_Mode = Check_Semantics
3728 and then Serious_Errors_Detected = 0))
3730 -- The body generated for an expression function that is not a
3731 -- completion is a freeze point neither for the profile nor for
3732 -- anything else. That's why, in order to prevent any freezing
3733 -- during analysis, we need to mask types declared outside the
3734 -- expression that are not yet frozen.
3736 if Nkind (N) = N_Subprogram_Body
3737 and then Was_Expression_Function (N)
3738 and then not Has_Completion (Spec_Id)
3740 Set_Is_Frozen (Spec_Id);
3741 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
3743 Set_Has_Delayed_Freeze (Spec_Id);
3744 Freeze_Before (N, Spec_Id);
3749 -- If the subprogram has a class-wide clone, build its body as a copy
3750 -- of the original body, and rewrite body of original subprogram as a
3751 -- wrapper that calls the clone.
3753 if Present (Spec_Id)
3754 and then Present (Class_Wide_Clone (Spec_Id))
3755 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
3757 Build_Class_Wide_Clone_Body (Spec_Id, N);
3759 -- This is the new body for the existing primitive operation
3761 Rewrite (N, Build_Class_Wide_Clone_Call
3762 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
3763 Set_Has_Completion (Spec_Id, False);
3768 -- Place subprogram on scope stack, and make formals visible. If there
3769 -- is a spec, the visible entity remains that of the spec.
3771 if Present (Spec_Id) then
3772 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3774 if Is_Child_Unit (Spec_Id) then
3775 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3779 Style.Check_Identifier (Body_Id, Spec_Id);
3782 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3783 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3785 if Is_Abstract_Subprogram (Spec_Id) then
3786 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3790 Set_Convention (Body_Id, Convention (Spec_Id));
3791 Set_Has_Completion (Spec_Id);
3793 if Is_Protected_Type (Scope (Spec_Id)) then
3794 Prot_Typ := Scope (Spec_Id);
3797 -- If this is a body generated for a renaming, do not check for
3798 -- full conformance. The check is redundant, because the spec of
3799 -- the body is a copy of the spec in the renaming declaration,
3800 -- and the test can lead to spurious errors on nested defaults.
3802 if Present (Spec_Decl)
3803 and then not Comes_From_Source (N)
3805 (Nkind (Original_Node (Spec_Decl)) =
3806 N_Subprogram_Renaming_Declaration
3807 or else (Present (Corresponding_Body (Spec_Decl))
3809 Nkind (Unit_Declaration_Node
3810 (Corresponding_Body (Spec_Decl))) =
3811 N_Subprogram_Renaming_Declaration))
3815 -- Conversely, the spec may have been generated for specless body
3816 -- with an inline pragma. The entity comes from source, which is
3817 -- both semantically correct and necessary for proper inlining.
3818 -- The subprogram declaration itself is not in the source.
3820 elsif Comes_From_Source (N)
3821 and then Present (Spec_Decl)
3822 and then not Comes_From_Source (Spec_Decl)
3823 and then Has_Pragma_Inline (Spec_Id)
3830 Fully_Conformant, True, Conformant, Body_Id);
3833 -- If the body is not fully conformant, we have to decide if we
3834 -- should analyze it or not. If it has a really messed up profile
3835 -- then we probably should not analyze it, since we will get too
3836 -- many bogus messages.
3838 -- Our decision is to go ahead in the non-fully conformant case
3839 -- only if it is at least mode conformant with the spec. Note
3840 -- that the call to Check_Fully_Conformant has issued the proper
3841 -- error messages to complain about the lack of conformance.
3844 and then not Mode_Conformant (Body_Id, Spec_Id)
3850 if Spec_Id /= Body_Id then
3851 Reference_Body_Formals (Spec_Id, Body_Id);
3854 Set_Ekind (Body_Id, E_Subprogram_Body);
3856 if Nkind (N) = N_Subprogram_Body_Stub then
3857 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3862 Set_Corresponding_Spec (N, Spec_Id);
3864 -- Ada 2005 (AI-345): If the operation is a primitive operation
3865 -- of a concurrent type, the type of the first parameter has been
3866 -- replaced with the corresponding record, which is the proper
3867 -- run-time structure to use. However, within the body there may
3868 -- be uses of the formals that depend on primitive operations
3869 -- of the type (in particular calls in prefixed form) for which
3870 -- we need the original concurrent type. The operation may have
3871 -- several controlling formals, so the replacement must be done
3874 if Comes_From_Source (Spec_Id)
3875 and then Present (First_Entity (Spec_Id))
3876 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3877 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3878 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3879 and then Present (Corresponding_Concurrent_Type
3880 (Etype (First_Entity (Spec_Id))))
3883 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3887 Form := First_Formal (Spec_Id);
3888 while Present (Form) loop
3889 if Etype (Form) = Typ then
3890 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3898 -- Make the formals visible, and place subprogram on scope stack.
3899 -- This is also the point at which we set Last_Real_Spec_Entity
3900 -- to mark the entities which will not be moved to the body.
3902 Install_Formals (Spec_Id);
3903 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3905 -- Within an instance, add local renaming declarations so that
3906 -- gdb can retrieve the values of actuals more easily. This is
3907 -- only relevant if generating code (and indeed we definitely
3908 -- do not want these definitions -gnatc mode, because that would
3911 if Is_Generic_Instance (Spec_Id)
3912 and then Is_Wrapper_Package (Current_Scope)
3913 and then Expander_Active
3915 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3918 Push_Scope (Spec_Id);
3920 -- Make sure that the subprogram is immediately visible. For
3921 -- child units that have no separate spec this is indispensable.
3922 -- Otherwise it is safe albeit redundant.
3924 Set_Is_Immediately_Visible (Spec_Id);
3927 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3928 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3929 Set_Scope (Body_Id, Scope (Spec_Id));
3931 -- Case of subprogram body with no previous spec
3934 -- Check for style warning required
3938 -- Only apply check for source level subprograms for which checks
3939 -- have not been suppressed.
3941 and then Comes_From_Source (Body_Id)
3942 and then not Suppress_Style_Checks (Body_Id)
3944 -- No warnings within an instance
3946 and then not In_Instance
3948 -- No warnings for expression functions
3950 and then Nkind (Original_Node (N)) /= N_Expression_Function
3952 Style.Body_With_No_Spec (N);
3955 New_Overloaded_Entity (Body_Id);
3957 if Nkind (N) /= N_Subprogram_Body_Stub then
3958 Set_Acts_As_Spec (N);
3959 Generate_Definition (Body_Id);
3961 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3963 -- If the body is an entry wrapper created for an entry with
3964 -- preconditions, it must be compiled in the context of the
3965 -- enclosing synchronized object, because it may mention other
3966 -- operations of the type.
3968 if Is_Entry_Wrapper (Body_Id) then
3970 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
3973 Install_Declarations (Prot);
3977 Install_Formals (Body_Id);
3979 Push_Scope (Body_Id);
3982 -- For stubs and bodies with no previous spec, generate references to
3985 Generate_Reference_To_Formals (Body_Id);
3988 -- Entry barrier functions are generated outside the protected type and
3989 -- should not carry the SPARK_Mode of the enclosing context.
3991 if Nkind (N) = N_Subprogram_Body
3992 and then Is_Entry_Barrier_Function (N)
3996 -- The body is generated as part of expression function expansion. When
3997 -- the expression function appears in the visible declarations of a
3998 -- package, the body is added to the private declarations. Since both
3999 -- declarative lists may be subject to a different SPARK_Mode, inherit
4000 -- the mode of the spec.
4002 -- package P with SPARK_Mode is
4003 -- function Expr_Func ... is (...); -- original
4004 -- [function Expr_Func ...;] -- generated spec
4007 -- pragma SPARK_Mode (Off);
4008 -- [function Expr_Func ... is return ...;] -- generated body
4009 -- end P; -- mode is ON
4011 elsif not Comes_From_Source (N)
4012 and then Present (Spec_Id)
4013 and then Is_Expression_Function (Spec_Id)
4015 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4016 Set_SPARK_Pragma_Inherited
4017 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4019 -- Set the SPARK_Mode from the current context (may be overwritten later
4020 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4021 -- initially on a stand-alone subprogram body, but is then relocated to
4022 -- a generated corresponding spec. In this scenario the mode is shared
4023 -- between the spec and body.
4025 elsif No (SPARK_Pragma (Body_Id)) then
4026 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4027 Set_SPARK_Pragma_Inherited (Body_Id);
4030 -- A subprogram body may be instantiated or inlined at a later pass.
4031 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4032 -- applied to the initial declaration of the body.
4034 if Present (Spec_Id) then
4035 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4036 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4040 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4041 -- case the body is instantiated or inlined later and out of context.
4042 -- The body uses this attribute to restore the value of the global
4045 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4046 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4048 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4049 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4053 -- If this is the proper body of a stub, we must verify that the stub
4054 -- conforms to the body, and to the previous spec if one was present.
4055 -- We know already that the body conforms to that spec. This test is
4056 -- only required for subprograms that come from source.
4058 if Nkind (Parent (N)) = N_Subunit
4059 and then Comes_From_Source (N)
4060 and then not Error_Posted (Body_Id)
4061 and then Nkind (Corresponding_Stub (Parent (N))) =
4062 N_Subprogram_Body_Stub
4065 Old_Id : constant Entity_Id :=
4067 (Specification (Corresponding_Stub (Parent (N))));
4069 Conformant : Boolean := False;
4072 if No (Spec_Id) then
4073 Check_Fully_Conformant (Body_Id, Old_Id);
4077 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4079 if not Conformant then
4081 -- The stub was taken to be a new declaration. Indicate that
4084 Set_Has_Completion (Old_Id, False);
4090 Set_Has_Completion (Body_Id);
4091 Check_Eliminated (Body_Id);
4093 -- Analyze any aspect specifications that appear on the subprogram body
4094 -- stub. Stop the analysis now as the stub does not have a declarative
4095 -- or a statement part, and it cannot be inlined.
4097 if Nkind (N) = N_Subprogram_Body_Stub then
4098 if Has_Aspects (N) then
4099 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4107 -- Note: Normally we don't do any inlining if expansion is off, since
4108 -- we won't generate code in any case. An exception arises in GNATprove
4109 -- mode where we want to expand some calls in place, even with expansion
4110 -- disabled, since the inlining eases formal verification.
4112 if not GNATprove_Mode
4113 and then Expander_Active
4114 and then Serious_Errors_Detected = 0
4115 and then Present (Spec_Id)
4116 and then Has_Pragma_Inline (Spec_Id)
4118 -- Legacy implementation (relying on front-end inlining)
4120 if not Back_End_Inlining then
4121 if (Has_Pragma_Inline_Always (Spec_Id)
4122 and then not Opt.Disable_FE_Inline_Always)
4123 or else (Front_End_Inlining
4124 and then not Opt.Disable_FE_Inline)
4126 Build_Body_To_Inline (N, Spec_Id);
4129 -- New implementation (relying on back-end inlining)
4132 if Has_Pragma_Inline_Always (Spec_Id)
4133 or else Optimization_Level > 0
4135 -- Handle function returning an unconstrained type
4137 if Comes_From_Source (Body_Id)
4138 and then Ekind (Spec_Id) = E_Function
4139 and then Returns_Unconstrained_Type (Spec_Id)
4141 -- If function builds in place, i.e. returns a limited type,
4142 -- inlining cannot be done.
4144 and then not Is_Limited_Type (Etype (Spec_Id))
4146 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4150 Subp_Body : constant Node_Id :=
4151 Unit_Declaration_Node (Body_Id);
4152 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4155 -- Do not pass inlining to the backend if the subprogram
4156 -- has declarations or statements which cannot be inlined
4157 -- by the backend. This check is done here to emit an
4158 -- error instead of the generic warning message reported
4159 -- by the GCC backend (ie. "function might not be
4162 if Present (Subp_Decl)
4163 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4167 elsif Has_Excluded_Statement
4170 (Handled_Statement_Sequence (Subp_Body)))
4174 -- If the backend inlining is available then at this
4175 -- stage we only have to mark the subprogram as inlined.
4176 -- The expander will take care of registering it in the
4177 -- table of subprograms inlined by the backend a part of
4178 -- processing calls to it (cf. Expand_Call)
4181 Set_Is_Inlined (Spec_Id);
4188 -- In GNATprove mode, inline only when there is a separate subprogram
4189 -- declaration for now, as inlining of subprogram bodies acting as
4190 -- declarations, or subprogram stubs, are not supported by front-end
4191 -- inlining. This inlining should occur after analysis of the body, so
4192 -- that it is known whether the value of SPARK_Mode, which can be
4193 -- defined by a pragma inside the body, is applicable to the body.
4194 -- Inlining can be disabled with switch -gnatdm
4196 elsif GNATprove_Mode
4197 and then Full_Analysis
4198 and then not Inside_A_Generic
4199 and then Present (Spec_Id)
4201 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4202 and then Body_Has_SPARK_Mode_On
4203 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4204 and then not Body_Has_Contract
4205 and then not Debug_Flag_M
4207 Build_Body_To_Inline (N, Spec_Id);
4210 -- When generating code, inherited pre/postconditions are handled when
4211 -- expanding the corresponding contract.
4213 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4214 -- of the specification we have to install the private withed units.
4215 -- This holds for child units as well.
4217 if Is_Compilation_Unit (Body_Id)
4218 or else Nkind (Parent (N)) = N_Compilation_Unit
4220 Install_Private_With_Clauses (Body_Id);
4223 Check_Anonymous_Return;
4225 -- Set the Protected_Formal field of each extra formal of the protected
4226 -- subprogram to reference the corresponding extra formal of the
4227 -- subprogram that implements it. For regular formals this occurs when
4228 -- the protected subprogram's declaration is expanded, but the extra
4229 -- formals don't get created until the subprogram is frozen. We need to
4230 -- do this before analyzing the protected subprogram's body so that any
4231 -- references to the original subprogram's extra formals will be changed
4232 -- refer to the implementing subprogram's formals (see Expand_Formal).
4234 if Present (Spec_Id)
4235 and then Is_Protected_Type (Scope (Spec_Id))
4236 and then Present (Protected_Body_Subprogram (Spec_Id))
4239 Impl_Subp : constant Entity_Id :=
4240 Protected_Body_Subprogram (Spec_Id);
4241 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4242 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4245 while Present (Prot_Ext_Formal) loop
4246 pragma Assert (Present (Impl_Ext_Formal));
4247 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4248 Next_Formal_With_Extras (Prot_Ext_Formal);
4249 Next_Formal_With_Extras (Impl_Ext_Formal);
4254 -- Now we can go on to analyze the body
4256 HSS := Handled_Statement_Sequence (N);
4257 Set_Actual_Subtypes (N, Current_Scope);
4259 -- Add a declaration for the Protection object, renaming declarations
4260 -- for discriminals and privals and finally a declaration for the entry
4261 -- family index (if applicable). This form of early expansion is done
4262 -- when the Expander is active because Install_Private_Data_Declarations
4263 -- references entities which were created during regular expansion. The
4264 -- subprogram entity must come from source, and not be an internally
4265 -- generated subprogram.
4268 and then Present (Prot_Typ)
4269 and then Present (Spec_Id)
4270 and then Comes_From_Source (Spec_Id)
4271 and then not Is_Eliminated (Spec_Id)
4273 Install_Private_Data_Declarations
4274 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4277 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4278 -- may now appear in parameter and result profiles. Since the analysis
4279 -- of a subprogram body may use the parameter and result profile of the
4280 -- spec, swap any limited views with their non-limited counterpart.
4282 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4283 Exch_Views := Exchange_Limited_Views (Spec_Id);
4286 -- If the return type is an anonymous access type whose designated type
4287 -- is the limited view of a class-wide type and the non-limited view is
4288 -- available, update the return type accordingly.
4290 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4296 Rtyp := Etype (Spec_Id);
4298 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4299 Etyp := Directly_Designated_Type (Rtyp);
4301 if Is_Class_Wide_Type (Etyp)
4302 and then From_Limited_With (Etyp)
4305 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4311 -- Analyze any aspect specifications that appear on the subprogram body
4313 if Has_Aspects (N) then
4314 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4317 Analyze_Declarations (Declarations (N));
4319 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4321 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4322 if Present (SPARK_Pragma (Spec_Id)) then
4323 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4325 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4327 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4328 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4329 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4331 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4334 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4338 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4339 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4340 Error_Msg_Sloc := Sloc (Spec_Id);
4342 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4346 -- A subprogram body "freezes" its own contract. Analyze the contract
4347 -- after the declarations of the body have been processed as pragmas
4348 -- are now chained on the contract of the subprogram body.
4350 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4352 -- Check completion, and analyze the statements
4355 Inspect_Deferred_Constant_Completion (Declarations (N));
4358 -- Deal with end of scope processing for the body
4360 Process_End_Label (HSS, 't', Current_Scope);
4361 Update_Use_Clause_Chain;
4364 -- If we are compiling an entry wrapper, remove the enclosing
4365 -- synchronized object from the stack.
4367 if Is_Entry_Wrapper (Body_Id) then
4371 Check_Subprogram_Order (N);
4372 Set_Analyzed (Body_Id);
4374 -- If we have a separate spec, then the analysis of the declarations
4375 -- caused the entities in the body to be chained to the spec id, but
4376 -- we want them chained to the body id. Only the formal parameters
4377 -- end up chained to the spec id in this case.
4379 if Present (Spec_Id) then
4381 -- We must conform to the categorization of our spec
4383 Validate_Categorization_Dependency (N, Spec_Id);
4385 -- And if this is a child unit, the parent units must conform
4387 if Is_Child_Unit (Spec_Id) then
4388 Validate_Categorization_Dependency
4389 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4392 -- Here is where we move entities from the spec to the body
4394 -- Case where there are entities that stay with the spec
4396 if Present (Last_Real_Spec_Entity) then
4398 -- No body entities (happens when the only real spec entities come
4399 -- from precondition and postcondition pragmas).
4401 if No (Last_Entity (Body_Id)) then
4402 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4404 -- Body entities present (formals), so chain stuff past them
4408 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4411 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4412 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4413 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4415 -- Case where there are no spec entities, in this case there can be
4416 -- no body entities either, so just move everything.
4418 -- If the body is generated for an expression function, it may have
4419 -- been preanalyzed already, if 'access was applied to it.
4422 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4423 N_Expression_Function
4425 pragma Assert (No (Last_Entity (Body_Id)));
4429 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4430 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4431 Set_First_Entity (Spec_Id, Empty);
4432 Set_Last_Entity (Spec_Id, Empty);
4436 Check_Missing_Return;
4438 -- Now we are going to check for variables that are never modified in
4439 -- the body of the procedure. But first we deal with a special case
4440 -- where we want to modify this check. If the body of the subprogram
4441 -- starts with a raise statement or its equivalent, or if the body
4442 -- consists entirely of a null statement, then it is pretty obvious that
4443 -- it is OK to not reference the parameters. For example, this might be
4444 -- the following common idiom for a stubbed function: statement of the
4445 -- procedure raises an exception. In particular this deals with the
4446 -- common idiom of a stubbed function, which appears something like:
4448 -- function F (A : Integer) return Some_Type;
4451 -- raise Program_Error;
4455 -- Here the purpose of X is simply to satisfy the annoying requirement
4456 -- in Ada that there be at least one return, and we certainly do not
4457 -- want to go posting warnings on X that it is not initialized. On
4458 -- the other hand, if X is entirely unreferenced that should still
4461 -- What we do is to detect these cases, and if we find them, flag the
4462 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4463 -- suppress unwanted warnings. For the case of the function stub above
4464 -- we have a special test to set X as apparently assigned to suppress
4471 -- Skip initial labels (for one thing this occurs when we are in
4472 -- front-end ZCX mode, but in any case it is irrelevant), and also
4473 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4475 Stm := First (Statements (HSS));
4476 while Nkind (Stm) = N_Label
4477 or else Nkind (Stm) in N_Push_xxx_Label
4482 -- Do the test on the original statement before expansion
4485 Ostm : constant Node_Id := Original_Node (Stm);
4488 -- If explicit raise statement, turn on flag
4490 if Nkind (Ostm) = N_Raise_Statement then
4491 Set_Trivial_Subprogram (Stm);
4493 -- If null statement, and no following statements, turn on flag
4495 elsif Nkind (Stm) = N_Null_Statement
4496 and then Comes_From_Source (Stm)
4497 and then No (Next (Stm))
4499 Set_Trivial_Subprogram (Stm);
4501 -- Check for explicit call cases which likely raise an exception
4503 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4504 if Is_Entity_Name (Name (Ostm)) then
4506 Ent : constant Entity_Id := Entity (Name (Ostm));
4509 -- If the procedure is marked No_Return, then likely it
4510 -- raises an exception, but in any case it is not coming
4511 -- back here, so turn on the flag.
4514 and then Ekind (Ent) = E_Procedure
4515 and then No_Return (Ent)
4517 Set_Trivial_Subprogram (Stm);
4525 -- Check for variables that are never modified
4532 -- If there is a separate spec, then transfer Never_Set_In_Source
4533 -- flags from out parameters to the corresponding entities in the
4534 -- body. The reason we do that is we want to post error flags on
4535 -- the body entities, not the spec entities.
4537 if Present (Spec_Id) then
4538 E1 := First_Entity (Spec_Id);
4539 while Present (E1) loop
4540 if Ekind (E1) = E_Out_Parameter then
4541 E2 := First_Entity (Body_Id);
4542 while Present (E2) loop
4543 exit when Chars (E1) = Chars (E2);
4547 if Present (E2) then
4548 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4556 -- Check references in body
4558 Check_References (Body_Id);
4561 -- Check for nested subprogram, and mark outer level subprogram if so
4567 if Present (Spec_Id) then
4574 Ent := Enclosing_Subprogram (Ent);
4575 exit when No (Ent) or else Is_Subprogram (Ent);
4578 if Present (Ent) then
4579 Set_Has_Nested_Subprogram (Ent);
4583 -- Restore the limited views in the spec, if any, to let the back end
4584 -- process it without running into circularities.
4586 if Exch_Views /= No_Elist then
4587 Restore_Limited_Views (Exch_Views);
4590 if Mask_Types /= No_Elist then
4591 Unmask_Unfrozen_Types (Mask_Types);
4594 if Present (Desig_View) then
4595 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4599 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4600 Restore_Ghost_Mode (Saved_GM);
4601 end Analyze_Subprogram_Body_Helper;
4603 ------------------------------------
4604 -- Analyze_Subprogram_Declaration --
4605 ------------------------------------
4607 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4608 Scop : constant Entity_Id := Current_Scope;
4609 Designator : Entity_Id;
4611 Is_Completion : Boolean;
4612 -- Indicates whether a null procedure declaration is a completion
4615 -- Null procedures are not allowed in SPARK
4617 if Nkind (Specification (N)) = N_Procedure_Specification
4618 and then Null_Present (Specification (N))
4620 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4622 -- Null procedures are allowed in protected types, following the
4623 -- recent AI12-0147.
4625 if Is_Protected_Type (Current_Scope)
4626 and then Ada_Version < Ada_2012
4628 Error_Msg_N ("protected operation cannot be a null procedure", N);
4631 Analyze_Null_Procedure (N, Is_Completion);
4633 -- The null procedure acts as a body, nothing further is needed
4635 if Is_Completion then
4640 Designator := Analyze_Subprogram_Specification (Specification (N));
4642 -- A reference may already have been generated for the unit name, in
4643 -- which case the following call is redundant. However it is needed for
4644 -- declarations that are the rewriting of an expression function.
4646 Generate_Definition (Designator);
4648 -- Set the SPARK mode from the current context (may be overwritten later
4649 -- with explicit pragma). This is not done for entry barrier functions
4650 -- because they are generated outside the protected type and should not
4651 -- carry the mode of the enclosing context.
4653 if Nkind (N) = N_Subprogram_Declaration
4654 and then Is_Entry_Barrier_Function (N)
4658 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4659 Set_SPARK_Pragma_Inherited (Designator);
4662 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
4663 -- the body of this subprogram is instantiated or inlined later and out
4664 -- of context. The body uses this attribute to restore the value of the
4667 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4668 Set_Ignore_SPARK_Mode_Pragmas (Designator);
4671 if Debug_Flag_C then
4672 Write_Str ("==> subprogram spec ");
4673 Write_Name (Chars (Designator));
4674 Write_Str (" from ");
4675 Write_Location (Sloc (N));
4680 Validate_RCI_Subprogram_Declaration (N);
4681 New_Overloaded_Entity (Designator);
4682 Check_Delayed_Subprogram (Designator);
4684 -- If the type of the first formal of the current subprogram is a non-
4685 -- generic tagged private type, mark the subprogram as being a private
4686 -- primitive. Ditto if this is a function with controlling result, and
4687 -- the return type is currently private. In both cases, the type of the
4688 -- controlling argument or result must be in the current scope for the
4689 -- operation to be primitive.
4691 if Has_Controlling_Result (Designator)
4692 and then Is_Private_Type (Etype (Designator))
4693 and then Scope (Etype (Designator)) = Current_Scope
4694 and then not Is_Generic_Actual_Type (Etype (Designator))
4696 Set_Is_Private_Primitive (Designator);
4698 elsif Present (First_Formal (Designator)) then
4700 Formal_Typ : constant Entity_Id :=
4701 Etype (First_Formal (Designator));
4703 Set_Is_Private_Primitive (Designator,
4704 Is_Tagged_Type (Formal_Typ)
4705 and then Scope (Formal_Typ) = Current_Scope
4706 and then Is_Private_Type (Formal_Typ)
4707 and then not Is_Generic_Actual_Type (Formal_Typ));
4711 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4714 if Ada_Version >= Ada_2005
4715 and then Comes_From_Source (N)
4716 and then Is_Dispatching_Operation (Designator)
4723 if Has_Controlling_Result (Designator) then
4724 Etyp := Etype (Designator);
4727 E := First_Entity (Designator);
4729 and then Is_Formal (E)
4730 and then not Is_Controlling_Formal (E)
4738 if Is_Access_Type (Etyp) then
4739 Etyp := Directly_Designated_Type (Etyp);
4742 if Is_Interface (Etyp)
4743 and then not Is_Abstract_Subprogram (Designator)
4744 and then not (Ekind (Designator) = E_Procedure
4745 and then Null_Present (Specification (N)))
4747 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4749 -- Specialize error message based on procedures vs. functions,
4750 -- since functions can't be null subprograms.
4752 if Ekind (Designator) = E_Procedure then
4754 ("interface procedure % must be abstract or null", N);
4757 ("interface function % must be abstract", N);
4763 -- What is the following code for, it used to be
4765 -- ??? Set_Suppress_Elaboration_Checks
4766 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4768 -- The following seems equivalent, but a bit dubious
4770 if Elaboration_Checks_Suppressed (Designator) then
4771 Set_Kill_Elaboration_Checks (Designator);
4774 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4775 Set_Categorization_From_Scope (Designator, Scop);
4778 -- For a compilation unit, check for library-unit pragmas
4780 Push_Scope (Designator);
4781 Set_Categorization_From_Pragmas (N);
4782 Validate_Categorization_Dependency (N, Designator);
4786 -- For a compilation unit, set body required. This flag will only be
4787 -- reset if a valid Import or Interface pragma is processed later on.
4789 if Nkind (Parent (N)) = N_Compilation_Unit then
4790 Set_Body_Required (Parent (N), True);
4792 if Ada_Version >= Ada_2005
4793 and then Nkind (Specification (N)) = N_Procedure_Specification
4794 and then Null_Present (Specification (N))
4797 ("null procedure cannot be declared at library level", N);
4801 Generate_Reference_To_Formals (Designator);
4802 Check_Eliminated (Designator);
4804 if Debug_Flag_C then
4806 Write_Str ("<== subprogram spec ");
4807 Write_Name (Chars (Designator));
4808 Write_Str (" from ");
4809 Write_Location (Sloc (N));
4813 if Is_Protected_Type (Current_Scope) then
4815 -- Indicate that this is a protected operation, because it may be
4816 -- used in subsequent declarations within the protected type.
4818 Set_Convention (Designator, Convention_Protected);
4821 List_Inherited_Pre_Post_Aspects (Designator);
4823 if Has_Aspects (N) then
4824 Analyze_Aspect_Specifications (N, Designator);
4826 end Analyze_Subprogram_Declaration;
4828 --------------------------------------
4829 -- Analyze_Subprogram_Specification --
4830 --------------------------------------
4832 -- Reminder: N here really is a subprogram specification (not a subprogram
4833 -- declaration). This procedure is called to analyze the specification in
4834 -- both subprogram bodies and subprogram declarations (specs).
4836 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4837 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
4838 -- Determine whether entity E denotes the spec or body of an invariant
4841 ------------------------------------
4842 -- Is_Invariant_Procedure_Or_Body --
4843 ------------------------------------
4845 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
4846 Decl : constant Node_Id := Unit_Declaration_Node (E);
4850 if Nkind (Decl) = N_Subprogram_Body then
4851 Spec := Corresponding_Spec (Decl);
4858 and then Ekind (Spec) = E_Procedure
4859 and then (Is_Partial_Invariant_Procedure (Spec)
4860 or else Is_Invariant_Procedure (Spec));
4861 end Is_Invariant_Procedure_Or_Body;
4865 Designator : constant Entity_Id := Defining_Entity (N);
4866 Formals : constant List_Id := Parameter_Specifications (N);
4868 -- Start of processing for Analyze_Subprogram_Specification
4871 -- User-defined operator is not allowed in SPARK, except as a renaming
4873 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4874 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4876 Check_SPARK_05_Restriction
4877 ("user-defined operator is not allowed", N);
4880 -- Proceed with analysis. Do not emit a cross-reference entry if the
4881 -- specification comes from an expression function, because it may be
4882 -- the completion of a previous declaration. It is not, the cross-
4883 -- reference entry will be emitted for the new subprogram declaration.
4885 if Nkind (Parent (N)) /= N_Expression_Function then
4886 Generate_Definition (Designator);
4889 if Nkind (N) = N_Function_Specification then
4890 Set_Ekind (Designator, E_Function);
4891 Set_Mechanism (Designator, Default_Mechanism);
4893 Set_Ekind (Designator, E_Procedure);
4894 Set_Etype (Designator, Standard_Void_Type);
4897 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4898 -- those subprograms which could be inlined in GNATprove mode (because
4899 -- Body_To_Inline is non-Empty) but should not be inlined.
4901 if GNATprove_Mode then
4902 Set_Is_Inlined_Always (Designator);
4905 -- Introduce new scope for analysis of the formals and the return type
4907 Set_Scope (Designator, Current_Scope);
4909 if Present (Formals) then
4910 Push_Scope (Designator);
4911 Process_Formals (Formals, N);
4913 -- Check dimensions in N for formals with default expression
4915 Analyze_Dimension_Formals (N, Formals);
4917 -- Ada 2005 (AI-345): If this is an overriding operation of an
4918 -- inherited interface operation, and the controlling type is
4919 -- a synchronized type, replace the type with its corresponding
4920 -- record, to match the proper signature of an overriding operation.
4921 -- Same processing for an access parameter whose designated type is
4922 -- derived from a synchronized interface.
4924 -- This modification is not done for invariant procedures because
4925 -- the corresponding record may not necessarely be visible when the
4926 -- concurrent type acts as the full view of a private type.
4929 -- type Prot is private with Type_Invariant => ...;
4930 -- procedure ConcInvariant (Obj : Prot);
4932 -- protected type Prot is ...;
4933 -- type Concurrent_Record_Prot is record ...;
4934 -- procedure ConcInvariant (Obj : Prot) is
4936 -- end ConcInvariant;
4939 -- In the example above, both the spec and body of the invariant
4940 -- procedure must utilize the private type as the controlling type.
4942 if Ada_Version >= Ada_2005
4943 and then not Is_Invariant_Procedure_Or_Body (Designator)
4947 Formal_Typ : Entity_Id;
4948 Rec_Typ : Entity_Id;
4949 Desig_Typ : Entity_Id;
4952 Formal := First_Formal (Designator);
4953 while Present (Formal) loop
4954 Formal_Typ := Etype (Formal);
4956 if Is_Concurrent_Type (Formal_Typ)
4957 and then Present (Corresponding_Record_Type (Formal_Typ))
4959 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4961 if Present (Interfaces (Rec_Typ)) then
4962 Set_Etype (Formal, Rec_Typ);
4965 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4966 Desig_Typ := Designated_Type (Formal_Typ);
4968 if Is_Concurrent_Type (Desig_Typ)
4969 and then Present (Corresponding_Record_Type (Desig_Typ))
4971 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4973 if Present (Interfaces (Rec_Typ)) then
4974 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4979 Next_Formal (Formal);
4986 -- The subprogram scope is pushed and popped around the processing of
4987 -- the return type for consistency with call above to Process_Formals
4988 -- (which itself can call Analyze_Return_Type), and to ensure that any
4989 -- itype created for the return type will be associated with the proper
4992 elsif Nkind (N) = N_Function_Specification then
4993 Push_Scope (Designator);
4994 Analyze_Return_Type (N);
5000 if Nkind (N) = N_Function_Specification then
5002 -- Deal with operator symbol case
5004 if Nkind (Designator) = N_Defining_Operator_Symbol then
5005 Valid_Operator_Definition (Designator);
5008 May_Need_Actuals (Designator);
5010 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5011 -- the subprogram is abstract also. This does not apply to renaming
5012 -- declarations, where abstractness is inherited, and to subprogram
5013 -- bodies generated for stream operations, which become renamings as
5016 -- In case of primitives associated with abstract interface types
5017 -- the check is applied later (see Analyze_Subprogram_Declaration).
5019 if not Nkind_In (Original_Node (Parent (N)),
5020 N_Abstract_Subprogram_Declaration,
5021 N_Formal_Abstract_Subprogram_Declaration,
5022 N_Subprogram_Renaming_Declaration)
5024 if Is_Abstract_Type (Etype (Designator))
5025 and then not Is_Interface (Etype (Designator))
5028 ("function that returns abstract type must be abstract", N);
5030 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5031 -- access result whose designated type is abstract.
5033 elsif Ada_Version >= Ada_2012
5034 and then Nkind (Result_Definition (N)) = N_Access_Definition
5036 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5037 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5040 ("function whose access result designates abstract type "
5041 & "must be abstract", N);
5047 end Analyze_Subprogram_Specification;
5049 -----------------------
5050 -- Check_Conformance --
5051 -----------------------
5053 procedure Check_Conformance
5054 (New_Id : Entity_Id;
5056 Ctype : Conformance_Type;
5058 Conforms : out Boolean;
5059 Err_Loc : Node_Id := Empty;
5060 Get_Inst : Boolean := False;
5061 Skip_Controlling_Formals : Boolean := False)
5063 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5064 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5065 -- If Errmsg is True, then processing continues to post an error message
5066 -- for conformance error on given node. Two messages are output. The
5067 -- first message points to the previous declaration with a general "no
5068 -- conformance" message. The second is the detailed reason, supplied as
5069 -- Msg. The parameter N provide information for a possible & insertion
5070 -- in the message, and also provides the location for posting the
5071 -- message in the absence of a specified Err_Loc location.
5073 function Conventions_Match
5075 Id2 : Entity_Id) return Boolean;
5076 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5079 -----------------------
5080 -- Conformance_Error --
5081 -----------------------
5083 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5090 if No (Err_Loc) then
5096 Error_Msg_Sloc := Sloc (Old_Id);
5099 when Type_Conformant =>
5100 Error_Msg_N -- CODEFIX
5101 ("not type conformant with declaration#!", Enode);
5103 when Mode_Conformant =>
5104 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5106 ("not mode conformant with operation inherited#!",
5110 ("not mode conformant with declaration#!", Enode);
5113 when Subtype_Conformant =>
5114 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5116 ("not subtype conformant with operation inherited#!",
5120 ("not subtype conformant with declaration#!", Enode);
5123 when Fully_Conformant =>
5124 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5125 Error_Msg_N -- CODEFIX
5126 ("not fully conformant with operation inherited#!",
5129 Error_Msg_N -- CODEFIX
5130 ("not fully conformant with declaration#!", Enode);
5134 Error_Msg_NE (Msg, Enode, N);
5136 end Conformance_Error;
5138 -----------------------
5139 -- Conventions_Match --
5140 -----------------------
5142 function Conventions_Match
5144 Id2 : Entity_Id) return Boolean
5147 -- Ignore the conventions of anonymous access-to-subprogram types
5148 -- and subprogram types because these are internally generated and
5149 -- the only way these may receive a convention is if they inherit
5150 -- the convention of a related subprogram.
5152 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5155 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5160 -- Otherwise compare the conventions directly
5163 return Convention (Id1) = Convention (Id2);
5165 end Conventions_Match;
5169 Old_Type : constant Entity_Id := Etype (Old_Id);
5170 New_Type : constant Entity_Id := Etype (New_Id);
5171 Old_Formal : Entity_Id;
5172 New_Formal : Entity_Id;
5173 Access_Types_Match : Boolean;
5174 Old_Formal_Base : Entity_Id;
5175 New_Formal_Base : Entity_Id;
5177 -- Start of processing for Check_Conformance
5182 -- We need a special case for operators, since they don't appear
5185 if Ctype = Type_Conformant then
5186 if Ekind (New_Id) = E_Operator
5187 and then Operator_Matches_Spec (New_Id, Old_Id)
5193 -- If both are functions/operators, check return types conform
5195 if Old_Type /= Standard_Void_Type
5197 New_Type /= Standard_Void_Type
5199 -- If we are checking interface conformance we omit controlling
5200 -- arguments and result, because we are only checking the conformance
5201 -- of the remaining parameters.
5203 if Has_Controlling_Result (Old_Id)
5204 and then Has_Controlling_Result (New_Id)
5205 and then Skip_Controlling_Formals
5209 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5210 if Ctype >= Subtype_Conformant
5211 and then not Predicates_Match (Old_Type, New_Type)
5214 ("\predicate of return type does not match!", New_Id);
5217 ("\return type does not match!", New_Id);
5223 -- Ada 2005 (AI-231): In case of anonymous access types check the
5224 -- null-exclusion and access-to-constant attributes match.
5226 if Ada_Version >= Ada_2005
5227 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5229 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5230 or else Is_Access_Constant (Etype (Old_Type)) /=
5231 Is_Access_Constant (Etype (New_Type)))
5233 Conformance_Error ("\return type does not match!", New_Id);
5237 -- If either is a function/operator and the other isn't, error
5239 elsif Old_Type /= Standard_Void_Type
5240 or else New_Type /= Standard_Void_Type
5242 Conformance_Error ("\functions can only match functions!", New_Id);
5246 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5247 -- If this is a renaming as body, refine error message to indicate that
5248 -- the conflict is with the original declaration. If the entity is not
5249 -- frozen, the conventions don't have to match, the one of the renamed
5250 -- entity is inherited.
5252 if Ctype >= Subtype_Conformant then
5253 if not Conventions_Match (Old_Id, New_Id) then
5254 if not Is_Frozen (New_Id) then
5257 elsif Present (Err_Loc)
5258 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5259 and then Present (Corresponding_Spec (Err_Loc))
5261 Error_Msg_Name_1 := Chars (New_Id);
5263 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5264 Conformance_Error ("\prior declaration for% has convention %!");
5267 Conformance_Error ("\calling conventions do not match!");
5272 elsif Is_Formal_Subprogram (Old_Id)
5273 or else Is_Formal_Subprogram (New_Id)
5275 Conformance_Error ("\formal subprograms not allowed!");
5280 -- Deal with parameters
5282 -- Note: we use the entity information, rather than going directly
5283 -- to the specification in the tree. This is not only simpler, but
5284 -- absolutely necessary for some cases of conformance tests between
5285 -- operators, where the declaration tree simply does not exist.
5287 Old_Formal := First_Formal (Old_Id);
5288 New_Formal := First_Formal (New_Id);
5289 while Present (Old_Formal) and then Present (New_Formal) loop
5290 if Is_Controlling_Formal (Old_Formal)
5291 and then Is_Controlling_Formal (New_Formal)
5292 and then Skip_Controlling_Formals
5294 -- The controlling formals will have different types when
5295 -- comparing an interface operation with its match, but both
5296 -- or neither must be access parameters.
5298 if Is_Access_Type (Etype (Old_Formal))
5300 Is_Access_Type (Etype (New_Formal))
5302 goto Skip_Controlling_Formal;
5305 ("\access parameter does not match!", New_Formal);
5309 -- Ada 2012: Mode conformance also requires that formal parameters
5310 -- be both aliased, or neither.
5312 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5313 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5315 ("\aliased parameter mismatch!", New_Formal);
5319 if Ctype = Fully_Conformant then
5321 -- Names must match. Error message is more accurate if we do
5322 -- this before checking that the types of the formals match.
5324 if Chars (Old_Formal) /= Chars (New_Formal) then
5325 Conformance_Error ("\name& does not match!", New_Formal);
5327 -- Set error posted flag on new formal as well to stop
5328 -- junk cascaded messages in some cases.
5330 Set_Error_Posted (New_Formal);
5334 -- Null exclusion must match
5336 if Null_Exclusion_Present (Parent (Old_Formal))
5338 Null_Exclusion_Present (Parent (New_Formal))
5340 -- Only give error if both come from source. This should be
5341 -- investigated some time, since it should not be needed ???
5343 if Comes_From_Source (Old_Formal)
5345 Comes_From_Source (New_Formal)
5348 ("\null exclusion for& does not match", New_Formal);
5350 -- Mark error posted on the new formal to avoid duplicated
5351 -- complaint about types not matching.
5353 Set_Error_Posted (New_Formal);
5358 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5359 -- case occurs whenever a subprogram is being renamed and one of its
5360 -- parameters imposes a null exclusion. For example:
5362 -- type T is null record;
5363 -- type Acc_T is access T;
5364 -- subtype Acc_T_Sub is Acc_T;
5366 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5367 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5370 Old_Formal_Base := Etype (Old_Formal);
5371 New_Formal_Base := Etype (New_Formal);
5374 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5375 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5378 Access_Types_Match := Ada_Version >= Ada_2005
5380 -- Ensure that this rule is only applied when New_Id is a
5381 -- renaming of Old_Id.
5383 and then Nkind (Parent (Parent (New_Id))) =
5384 N_Subprogram_Renaming_Declaration
5385 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5386 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5387 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5389 -- Now handle the allowed access-type case
5391 and then Is_Access_Type (Old_Formal_Base)
5392 and then Is_Access_Type (New_Formal_Base)
5394 -- The type kinds must match. The only exception occurs with
5395 -- multiple generics of the form:
5398 -- type F is private; type A is private;
5399 -- type F_Ptr is access F; type A_Ptr is access A;
5400 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5401 -- package F_Pack is ... package A_Pack is
5402 -- package F_Inst is
5403 -- new F_Pack (A, A_Ptr, A_P);
5405 -- When checking for conformance between the parameters of A_P
5406 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5407 -- because the compiler has transformed A_Ptr into a subtype of
5408 -- F_Ptr. We catch this case in the code below.
5410 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5412 (Is_Generic_Type (Old_Formal_Base)
5413 and then Is_Generic_Type (New_Formal_Base)
5414 and then Is_Internal (New_Formal_Base)
5415 and then Etype (Etype (New_Formal_Base)) =
5417 and then Directly_Designated_Type (Old_Formal_Base) =
5418 Directly_Designated_Type (New_Formal_Base)
5419 and then ((Is_Itype (Old_Formal_Base)
5420 and then Can_Never_Be_Null (Old_Formal_Base))
5422 (Is_Itype (New_Formal_Base)
5423 and then Can_Never_Be_Null (New_Formal_Base)));
5425 -- Types must always match. In the visible part of an instance,
5426 -- usual overloading rules for dispatching operations apply, and
5427 -- we check base types (not the actual subtypes).
5429 if In_Instance_Visible_Part
5430 and then Is_Dispatching_Operation (New_Id)
5432 if not Conforming_Types
5433 (T1 => Base_Type (Etype (Old_Formal)),
5434 T2 => Base_Type (Etype (New_Formal)),
5436 Get_Inst => Get_Inst)
5437 and then not Access_Types_Match
5439 Conformance_Error ("\type of & does not match!", New_Formal);
5443 elsif not Conforming_Types
5444 (T1 => Old_Formal_Base,
5445 T2 => New_Formal_Base,
5447 Get_Inst => Get_Inst)
5448 and then not Access_Types_Match
5450 -- Don't give error message if old type is Any_Type. This test
5451 -- avoids some cascaded errors, e.g. in case of a bad spec.
5453 if Errmsg and then Old_Formal_Base = Any_Type then
5456 if Ctype >= Subtype_Conformant
5458 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5461 ("\predicate of & does not match!", New_Formal);
5464 ("\type of & does not match!", New_Formal);
5466 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5468 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5476 -- For mode conformance, mode must match
5478 if Ctype >= Mode_Conformant then
5479 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5480 if not Ekind_In (New_Id, E_Function, E_Procedure)
5481 or else not Is_Primitive_Wrapper (New_Id)
5483 Conformance_Error ("\mode of & does not match!", New_Formal);
5487 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5489 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5491 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5494 ("\mode of & does not match!", New_Formal);
5501 -- Part of mode conformance for access types is having the same
5502 -- constant modifier.
5504 elsif Access_Types_Match
5505 and then Is_Access_Constant (Old_Formal_Base) /=
5506 Is_Access_Constant (New_Formal_Base)
5509 ("\constant modifier does not match!", New_Formal);
5514 if Ctype >= Subtype_Conformant then
5516 -- Ada 2005 (AI-231): In case of anonymous access types check
5517 -- the null-exclusion and access-to-constant attributes must
5518 -- match. For null exclusion, we test the types rather than the
5519 -- formals themselves, since the attribute is only set reliably
5520 -- on the formals in the Ada 95 case, and we exclude the case
5521 -- where Old_Formal is marked as controlling, to avoid errors
5522 -- when matching completing bodies with dispatching declarations
5523 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5525 if Ada_Version >= Ada_2005
5526 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5527 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5529 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5530 Can_Never_Be_Null (Etype (New_Formal))
5532 not Is_Controlling_Formal (Old_Formal))
5534 Is_Access_Constant (Etype (Old_Formal)) /=
5535 Is_Access_Constant (Etype (New_Formal)))
5537 -- Do not complain if error already posted on New_Formal. This
5538 -- avoids some redundant error messages.
5540 and then not Error_Posted (New_Formal)
5542 -- It is allowed to omit the null-exclusion in case of stream
5543 -- attribute subprograms. We recognize stream subprograms
5544 -- through their TSS-generated suffix.
5547 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5550 if TSS_Name /= TSS_Stream_Read
5551 and then TSS_Name /= TSS_Stream_Write
5552 and then TSS_Name /= TSS_Stream_Input
5553 and then TSS_Name /= TSS_Stream_Output
5555 -- Here we have a definite conformance error. It is worth
5556 -- special casing the error message for the case of a
5557 -- controlling formal (which excludes null).
5559 if Is_Controlling_Formal (New_Formal) then
5560 Error_Msg_Node_2 := Scope (New_Formal);
5562 ("\controlling formal & of & excludes null, "
5563 & "declaration must exclude null as well",
5566 -- Normal case (couldn't we give more detail here???)
5570 ("\type of & does not match!", New_Formal);
5579 -- Full conformance checks
5581 if Ctype = Fully_Conformant then
5583 -- We have checked already that names match
5585 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5587 -- Check default expressions for in parameters
5590 NewD : constant Boolean :=
5591 Present (Default_Value (New_Formal));
5592 OldD : constant Boolean :=
5593 Present (Default_Value (Old_Formal));
5595 if NewD or OldD then
5597 -- The old default value has been analyzed because the
5598 -- current full declaration will have frozen everything
5599 -- before. The new default value has not been analyzed,
5600 -- so analyze it now before we check for conformance.
5603 Push_Scope (New_Id);
5604 Preanalyze_Spec_Expression
5605 (Default_Value (New_Formal), Etype (New_Formal));
5609 if not (NewD and OldD)
5610 or else not Fully_Conformant_Expressions
5611 (Default_Value (Old_Formal),
5612 Default_Value (New_Formal))
5615 ("\default expression for & does not match!",
5624 -- A couple of special checks for Ada 83 mode. These checks are
5625 -- skipped if either entity is an operator in package Standard,
5626 -- or if either old or new instance is not from the source program.
5628 if Ada_Version = Ada_83
5629 and then Sloc (Old_Id) > Standard_Location
5630 and then Sloc (New_Id) > Standard_Location
5631 and then Comes_From_Source (Old_Id)
5632 and then Comes_From_Source (New_Id)
5635 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5636 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5639 -- Explicit IN must be present or absent in both cases. This
5640 -- test is required only in the full conformance case.
5642 if In_Present (Old_Param) /= In_Present (New_Param)
5643 and then Ctype = Fully_Conformant
5646 ("\(Ada 83) IN must appear in both declarations",
5651 -- Grouping (use of comma in param lists) must be the same
5652 -- This is where we catch a misconformance like:
5655 -- A : Integer; B : Integer
5657 -- which are represented identically in the tree except
5658 -- for the setting of the flags More_Ids and Prev_Ids.
5660 if More_Ids (Old_Param) /= More_Ids (New_Param)
5661 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5664 ("\grouping of & does not match!", New_Formal);
5670 -- This label is required when skipping controlling formals
5672 <<Skip_Controlling_Formal>>
5674 Next_Formal (Old_Formal);
5675 Next_Formal (New_Formal);
5678 if Present (Old_Formal) then
5679 Conformance_Error ("\too few parameters!");
5682 elsif Present (New_Formal) then
5683 Conformance_Error ("\too many parameters!", New_Formal);
5686 end Check_Conformance;
5688 -----------------------
5689 -- Check_Conventions --
5690 -----------------------
5692 procedure Check_Conventions (Typ : Entity_Id) is
5693 Ifaces_List : Elist_Id;
5695 procedure Check_Convention (Op : Entity_Id);
5696 -- Verify that the convention of inherited dispatching operation Op is
5697 -- consistent among all subprograms it overrides. In order to minimize
5698 -- the search, Search_From is utilized to designate a specific point in
5699 -- the list rather than iterating over the whole list once more.
5701 ----------------------
5702 -- Check_Convention --
5703 ----------------------
5705 procedure Check_Convention (Op : Entity_Id) is
5706 Op_Conv : constant Convention_Id := Convention (Op);
5707 Iface_Conv : Convention_Id;
5708 Iface_Elmt : Elmt_Id;
5709 Iface_Prim_Elmt : Elmt_Id;
5710 Iface_Prim : Entity_Id;
5713 Iface_Elmt := First_Elmt (Ifaces_List);
5714 while Present (Iface_Elmt) loop
5716 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5717 while Present (Iface_Prim_Elmt) loop
5718 Iface_Prim := Node (Iface_Prim_Elmt);
5719 Iface_Conv := Convention (Iface_Prim);
5721 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5722 and then Iface_Conv /= Op_Conv
5725 ("inconsistent conventions in primitive operations", Typ);
5727 Error_Msg_Name_1 := Chars (Op);
5728 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5729 Error_Msg_Sloc := Sloc (Op);
5731 if Comes_From_Source (Op) or else No (Alias (Op)) then
5732 if not Present (Overridden_Operation (Op)) then
5733 Error_Msg_N ("\\primitive % defined #", Typ);
5736 ("\\overriding operation % with "
5737 & "convention % defined #", Typ);
5740 else pragma Assert (Present (Alias (Op)));
5741 Error_Msg_Sloc := Sloc (Alias (Op));
5742 Error_Msg_N ("\\inherited operation % with "
5743 & "convention % defined #", Typ);
5746 Error_Msg_Name_1 := Chars (Op);
5747 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5748 Error_Msg_Sloc := Sloc (Iface_Prim);
5749 Error_Msg_N ("\\overridden operation % with "
5750 & "convention % defined #", Typ);
5752 -- Avoid cascading errors
5757 Next_Elmt (Iface_Prim_Elmt);
5760 Next_Elmt (Iface_Elmt);
5762 end Check_Convention;
5766 Prim_Op : Entity_Id;
5767 Prim_Op_Elmt : Elmt_Id;
5769 -- Start of processing for Check_Conventions
5772 if not Has_Interfaces (Typ) then
5776 Collect_Interfaces (Typ, Ifaces_List);
5778 -- The algorithm checks every overriding dispatching operation against
5779 -- all the corresponding overridden dispatching operations, detecting
5780 -- differences in conventions.
5782 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5783 while Present (Prim_Op_Elmt) loop
5784 Prim_Op := Node (Prim_Op_Elmt);
5786 -- A small optimization: skip the predefined dispatching operations
5787 -- since they always have the same convention.
5789 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5790 Check_Convention (Prim_Op);
5793 Next_Elmt (Prim_Op_Elmt);
5795 end Check_Conventions;
5797 ------------------------------
5798 -- Check_Delayed_Subprogram --
5799 ------------------------------
5801 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5804 procedure Possible_Freeze (T : Entity_Id);
5805 -- T is the type of either a formal parameter or of the return type.
5806 -- If T is not yet frozen and needs a delayed freeze, then the
5807 -- subprogram itself must be delayed.
5809 ---------------------
5810 -- Possible_Freeze --
5811 ---------------------
5813 procedure Possible_Freeze (T : Entity_Id) is
5815 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5816 Set_Has_Delayed_Freeze (Designator);
5818 elsif Is_Access_Type (T)
5819 and then Has_Delayed_Freeze (Designated_Type (T))
5820 and then not Is_Frozen (Designated_Type (T))
5822 Set_Has_Delayed_Freeze (Designator);
5825 end Possible_Freeze;
5827 -- Start of processing for Check_Delayed_Subprogram
5830 -- All subprograms, including abstract subprograms, may need a freeze
5831 -- node if some formal type or the return type needs one.
5833 Possible_Freeze (Etype (Designator));
5834 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5836 -- Need delayed freeze if any of the formal types themselves need
5837 -- a delayed freeze and are not yet frozen.
5839 F := First_Formal (Designator);
5840 while Present (F) loop
5841 Possible_Freeze (Etype (F));
5842 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5846 -- Mark functions that return by reference. Note that it cannot be
5847 -- done for delayed_freeze subprograms because the underlying
5848 -- returned type may not be known yet (for private types)
5850 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5852 Typ : constant Entity_Id := Etype (Designator);
5853 Utyp : constant Entity_Id := Underlying_Type (Typ);
5855 if Is_Limited_View (Typ) then
5856 Set_Returns_By_Ref (Designator);
5857 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5858 Set_Returns_By_Ref (Designator);
5862 end Check_Delayed_Subprogram;
5864 ------------------------------------
5865 -- Check_Discriminant_Conformance --
5866 ------------------------------------
5868 procedure Check_Discriminant_Conformance
5873 Old_Discr : Entity_Id := First_Discriminant (Prev);
5874 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5875 New_Discr_Id : Entity_Id;
5876 New_Discr_Type : Entity_Id;
5878 procedure Conformance_Error (Msg : String; N : Node_Id);
5879 -- Post error message for conformance error on given node. Two messages
5880 -- are output. The first points to the previous declaration with a
5881 -- general "no conformance" message. The second is the detailed reason,
5882 -- supplied as Msg. The parameter N provide information for a possible
5883 -- & insertion in the message.
5885 -----------------------
5886 -- Conformance_Error --
5887 -----------------------
5889 procedure Conformance_Error (Msg : String; N : Node_Id) is
5891 Error_Msg_Sloc := Sloc (Prev_Loc);
5892 Error_Msg_N -- CODEFIX
5893 ("not fully conformant with declaration#!", N);
5894 Error_Msg_NE (Msg, N, N);
5895 end Conformance_Error;
5897 -- Start of processing for Check_Discriminant_Conformance
5900 while Present (Old_Discr) and then Present (New_Discr) loop
5901 New_Discr_Id := Defining_Identifier (New_Discr);
5903 -- The subtype mark of the discriminant on the full type has not
5904 -- been analyzed so we do it here. For an access discriminant a new
5907 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5909 Access_Definition (N, Discriminant_Type (New_Discr));
5912 Analyze (Discriminant_Type (New_Discr));
5913 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5915 -- Ada 2005: if the discriminant definition carries a null
5916 -- exclusion, create an itype to check properly for consistency
5917 -- with partial declaration.
5919 if Is_Access_Type (New_Discr_Type)
5920 and then Null_Exclusion_Present (New_Discr)
5923 Create_Null_Excluding_Itype
5924 (T => New_Discr_Type,
5925 Related_Nod => New_Discr,
5926 Scope_Id => Current_Scope);
5930 if not Conforming_Types
5931 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5933 Conformance_Error ("type of & does not match!", New_Discr_Id);
5936 -- Treat the new discriminant as an occurrence of the old one,
5937 -- for navigation purposes, and fill in some semantic
5938 -- information, for completeness.
5940 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5941 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5942 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5947 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5948 Conformance_Error ("name & does not match!", New_Discr_Id);
5952 -- Default expressions must match
5955 NewD : constant Boolean :=
5956 Present (Expression (New_Discr));
5957 OldD : constant Boolean :=
5958 Present (Expression (Parent (Old_Discr)));
5961 if NewD or OldD then
5963 -- The old default value has been analyzed and expanded,
5964 -- because the current full declaration will have frozen
5965 -- everything before. The new default values have not been
5966 -- expanded, so expand now to check conformance.
5969 Preanalyze_Spec_Expression
5970 (Expression (New_Discr), New_Discr_Type);
5973 if not (NewD and OldD)
5974 or else not Fully_Conformant_Expressions
5975 (Expression (Parent (Old_Discr)),
5976 Expression (New_Discr))
5980 ("default expression for & does not match!",
5987 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5989 if Ada_Version = Ada_83 then
5991 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5994 -- Grouping (use of comma in param lists) must be the same
5995 -- This is where we catch a misconformance like:
5998 -- A : Integer; B : Integer
6000 -- which are represented identically in the tree except
6001 -- for the setting of the flags More_Ids and Prev_Ids.
6003 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6004 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6007 ("grouping of & does not match!", New_Discr_Id);
6013 Next_Discriminant (Old_Discr);
6017 if Present (Old_Discr) then
6018 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6021 elsif Present (New_Discr) then
6023 ("too many discriminants!", Defining_Identifier (New_Discr));
6026 end Check_Discriminant_Conformance;
6028 ----------------------------
6029 -- Check_Fully_Conformant --
6030 ----------------------------
6032 procedure Check_Fully_Conformant
6033 (New_Id : Entity_Id;
6035 Err_Loc : Node_Id := Empty)
6038 pragma Warnings (Off, Result);
6041 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6042 end Check_Fully_Conformant;
6044 --------------------------
6045 -- Check_Limited_Return --
6046 --------------------------
6048 procedure Check_Limited_Return
6054 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6055 -- replaced by anonymous access results. This is an incompatibility with
6056 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6057 -- controllable with special switch. ???
6059 -- A limited interface that is not immutably limited is OK
6061 if Is_Limited_Interface (R_Type)
6063 not (Is_Task_Interface (R_Type)
6064 or else Is_Protected_Interface (R_Type)
6065 or else Is_Synchronized_Interface (R_Type))
6069 elsif Is_Limited_Type (R_Type)
6070 and then not Is_Interface (R_Type)
6071 and then Comes_From_Source (N)
6072 and then not In_Instance_Body
6073 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6075 -- Error in Ada 2005
6077 if Ada_Version >= Ada_2005
6078 and then not Debug_Flag_Dot_L
6079 and then not GNAT_Mode
6082 ("(Ada 2005) cannot copy object of a limited type "
6083 & "(RM-2005 6.5(5.5/2))", Expr);
6085 if Is_Limited_View (R_Type) then
6087 ("\return by reference not permitted in Ada 2005", Expr);
6090 -- Warn in Ada 95 mode, to give folks a heads up about this
6093 -- In GNAT mode, this is just a warning, to allow it to be evilly
6094 -- turned off. Otherwise it is a real error.
6096 -- In a generic context, simplify the warning because it makes no
6097 -- sense to discuss pass-by-reference or copy.
6099 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6100 if Inside_A_Generic then
6102 ("return of limited object not permitted in Ada 2005 "
6103 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6105 elsif Is_Limited_View (R_Type) then
6107 ("return by reference not permitted in Ada 2005 "
6108 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6111 ("cannot copy object of a limited type in Ada 2005 "
6112 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6115 -- Ada 95 mode, and compatibility warnings disabled
6118 pragma Assert (Ada_Version <= Ada_95);
6119 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6120 return; -- skip continuation messages below
6123 if not Inside_A_Generic then
6125 ("\consider switching to return of access type", Expr);
6126 Explain_Limited_Type (R_Type, Expr);
6129 end Check_Limited_Return;
6131 ---------------------------
6132 -- Check_Mode_Conformant --
6133 ---------------------------
6135 procedure Check_Mode_Conformant
6136 (New_Id : Entity_Id;
6138 Err_Loc : Node_Id := Empty;
6139 Get_Inst : Boolean := False)
6142 pragma Warnings (Off, Result);
6145 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6146 end Check_Mode_Conformant;
6148 --------------------------------
6149 -- Check_Overriding_Indicator --
6150 --------------------------------
6152 procedure Check_Overriding_Indicator
6154 Overridden_Subp : Entity_Id;
6155 Is_Primitive : Boolean)
6161 -- No overriding indicator for literals
6163 if Ekind (Subp) = E_Enumeration_Literal then
6166 elsif Ekind (Subp) = E_Entry then
6167 Decl := Parent (Subp);
6169 -- No point in analyzing a malformed operator
6171 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6172 and then Error_Posted (Subp)
6177 Decl := Unit_Declaration_Node (Subp);
6180 if Nkind_In (Decl, N_Subprogram_Body,
6181 N_Subprogram_Body_Stub,
6182 N_Subprogram_Declaration,
6183 N_Abstract_Subprogram_Declaration,
6184 N_Subprogram_Renaming_Declaration)
6186 Spec := Specification (Decl);
6188 elsif Nkind (Decl) = N_Entry_Declaration then
6195 -- The overriding operation is type conformant with the overridden one,
6196 -- but the names of the formals are not required to match. If the names
6197 -- appear permuted in the overriding operation, this is a possible
6198 -- source of confusion that is worth diagnosing. Controlling formals
6199 -- often carry names that reflect the type, and it is not worthwhile
6200 -- requiring that their names match.
6202 if Present (Overridden_Subp)
6203 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6210 Form1 := First_Formal (Subp);
6211 Form2 := First_Formal (Overridden_Subp);
6213 -- If the overriding operation is a synchronized operation, skip
6214 -- the first parameter of the overridden operation, which is
6215 -- implicit in the new one. If the operation is declared in the
6216 -- body it is not primitive and all formals must match.
6218 if Is_Concurrent_Type (Scope (Subp))
6219 and then Is_Tagged_Type (Scope (Subp))
6220 and then not Has_Completion (Scope (Subp))
6222 Form2 := Next_Formal (Form2);
6225 if Present (Form1) then
6226 Form1 := Next_Formal (Form1);
6227 Form2 := Next_Formal (Form2);
6230 while Present (Form1) loop
6231 if not Is_Controlling_Formal (Form1)
6232 and then Present (Next_Formal (Form2))
6233 and then Chars (Form1) = Chars (Next_Formal (Form2))
6235 Error_Msg_Node_2 := Alias (Overridden_Subp);
6236 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6238 ("& does not match corresponding formal of&#",
6243 Next_Formal (Form1);
6244 Next_Formal (Form2);
6249 -- If there is an overridden subprogram, then check that there is no
6250 -- "not overriding" indicator, and mark the subprogram as overriding.
6251 -- This is not done if the overridden subprogram is marked as hidden,
6252 -- which can occur for the case of inherited controlled operations
6253 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6254 -- subprogram is not itself hidden. (Note: This condition could probably
6255 -- be simplified, leaving out the testing for the specific controlled
6256 -- cases, but it seems safer and clearer this way, and echoes similar
6257 -- special-case tests of this kind in other places.)
6259 if Present (Overridden_Subp)
6260 and then (not Is_Hidden (Overridden_Subp)
6262 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6265 and then Present (Alias (Overridden_Subp))
6266 and then not Is_Hidden (Alias (Overridden_Subp))))
6268 if Must_Not_Override (Spec) then
6269 Error_Msg_Sloc := Sloc (Overridden_Subp);
6271 if Ekind (Subp) = E_Entry then
6273 ("entry & overrides inherited operation #", Spec, Subp);
6276 ("subprogram & overrides inherited operation #", Spec, Subp);
6279 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6280 -- as an extension of Root_Controlled, and thus has a useless Adjust
6281 -- operation. This operation should not be inherited by other limited
6282 -- controlled types. An explicit Adjust for them is not overriding.
6284 elsif Must_Override (Spec)
6285 and then Chars (Overridden_Subp) = Name_Adjust
6286 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6287 and then Present (Alias (Overridden_Subp))
6288 and then In_Predefined_Unit (Alias (Overridden_Subp))
6291 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6292 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6294 elsif Is_Subprogram (Subp) then
6295 if Is_Init_Proc (Subp) then
6298 elsif No (Overridden_Operation (Subp)) then
6300 -- For entities generated by Derive_Subprograms the overridden
6301 -- operation is the inherited primitive (which is available
6302 -- through the attribute alias)
6304 if (Is_Dispatching_Operation (Subp)
6305 or else Is_Dispatching_Operation (Overridden_Subp))
6306 and then not Comes_From_Source (Overridden_Subp)
6307 and then Find_Dispatching_Type (Overridden_Subp) =
6308 Find_Dispatching_Type (Subp)
6309 and then Present (Alias (Overridden_Subp))
6310 and then Comes_From_Source (Alias (Overridden_Subp))
6312 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6313 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6316 Set_Overridden_Operation (Subp, Overridden_Subp);
6317 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6322 -- If primitive flag is set or this is a protected operation, then
6323 -- the operation is overriding at the point of its declaration, so
6324 -- warn if necessary. Otherwise it may have been declared before the
6325 -- operation it overrides and no check is required.
6328 and then not Must_Override (Spec)
6329 and then (Is_Primitive
6330 or else Ekind (Scope (Subp)) = E_Protected_Type)
6332 Style.Missing_Overriding (Decl, Subp);
6335 -- If Subp is an operator, it may override a predefined operation, if
6336 -- it is defined in the same scope as the type to which it applies.
6337 -- In that case Overridden_Subp is empty because of our implicit
6338 -- representation for predefined operators. We have to check whether the
6339 -- signature of Subp matches that of a predefined operator. Note that
6340 -- first argument provides the name of the operator, and the second
6341 -- argument the signature that may match that of a standard operation.
6342 -- If the indicator is overriding, then the operator must match a
6343 -- predefined signature, because we know already that there is no
6344 -- explicit overridden operation.
6346 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6347 if Must_Not_Override (Spec) then
6349 -- If this is not a primitive or a protected subprogram, then
6350 -- "not overriding" is illegal.
6353 and then Ekind (Scope (Subp)) /= E_Protected_Type
6355 Error_Msg_N ("overriding indicator only allowed "
6356 & "if subprogram is primitive", Subp);
6358 elsif Can_Override_Operator (Subp) then
6360 ("subprogram& overrides predefined operator ", Spec, Subp);
6363 elsif Must_Override (Spec) then
6364 if No (Overridden_Operation (Subp))
6365 and then not Can_Override_Operator (Subp)
6367 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6370 elsif not Error_Posted (Subp)
6371 and then Style_Check
6372 and then Can_Override_Operator (Subp)
6373 and then not In_Predefined_Unit (Subp)
6375 -- If style checks are enabled, indicate that the indicator is
6376 -- missing. However, at the point of declaration, the type of
6377 -- which this is a primitive operation may be private, in which
6378 -- case the indicator would be premature.
6380 if Has_Private_Declaration (Etype (Subp))
6381 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6385 Style.Missing_Overriding (Decl, Subp);
6389 elsif Must_Override (Spec) then
6390 if Ekind (Subp) = E_Entry then
6391 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6393 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6396 -- If the operation is marked "not overriding" and it's not primitive
6397 -- then an error is issued, unless this is an operation of a task or
6398 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6399 -- has been specified have already been checked above.
6401 elsif Must_Not_Override (Spec)
6402 and then not Is_Primitive
6403 and then Ekind (Subp) /= E_Entry
6404 and then Ekind (Scope (Subp)) /= E_Protected_Type
6407 ("overriding indicator only allowed if subprogram is primitive",
6411 end Check_Overriding_Indicator;
6417 -- Note: this procedure needs to know far too much about how the expander
6418 -- messes with exceptions. The use of the flag Exception_Junk and the
6419 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6420 -- works, but is not very clean. It would be better if the expansion
6421 -- routines would leave Original_Node working nicely, and we could use
6422 -- Original_Node here to ignore all the peculiar expander messing ???
6424 procedure Check_Returns
6428 Proc : Entity_Id := Empty)
6432 procedure Check_Statement_Sequence (L : List_Id);
6433 -- Internal recursive procedure to check a list of statements for proper
6434 -- termination by a return statement (or a transfer of control or a
6435 -- compound statement that is itself internally properly terminated).
6437 ------------------------------
6438 -- Check_Statement_Sequence --
6439 ------------------------------
6441 procedure Check_Statement_Sequence (L : List_Id) is
6446 function Assert_False return Boolean;
6447 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6448 -- rewritten as a null statement when assertions are off. The assert
6449 -- is not active, but it is still enough to kill the warning.
6455 function Assert_False return Boolean is
6456 Orig : constant Node_Id := Original_Node (Last_Stm);
6459 if Nkind (Orig) = N_Pragma
6460 and then Pragma_Name (Orig) = Name_Assert
6461 and then not Error_Posted (Orig)
6464 Arg : constant Node_Id :=
6465 First (Pragma_Argument_Associations (Orig));
6466 Exp : constant Node_Id := Expression (Arg);
6468 return Nkind (Exp) = N_Identifier
6469 and then Chars (Exp) = Name_False;
6479 Raise_Exception_Call : Boolean;
6480 -- Set True if statement sequence terminated by Raise_Exception call
6481 -- or a Reraise_Occurrence call.
6483 -- Start of processing for Check_Statement_Sequence
6486 Raise_Exception_Call := False;
6488 -- Get last real statement
6490 Last_Stm := Last (L);
6492 -- Deal with digging out exception handler statement sequences that
6493 -- have been transformed by the local raise to goto optimization.
6494 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6495 -- optimization has occurred, we are looking at something like:
6498 -- original stmts in block
6502 -- goto L1; | omitted if No_Exception_Propagation
6507 -- goto L3; -- skip handler when exception not raised
6509 -- <<L1>> -- target label for local exception
6523 -- and what we have to do is to dig out the estmts1 and estmts2
6524 -- sequences (which were the original sequences of statements in
6525 -- the exception handlers) and check them.
6527 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6532 exit when Nkind (Stm) /= N_Block_Statement;
6533 exit when not Exception_Junk (Stm);
6536 exit when Nkind (Stm) /= N_Label;
6537 exit when not Exception_Junk (Stm);
6538 Check_Statement_Sequence
6539 (Statements (Handled_Statement_Sequence (Next (Stm))));
6544 exit when Nkind (Stm) /= N_Goto_Statement;
6545 exit when not Exception_Junk (Stm);
6549 -- Don't count pragmas
6551 while Nkind (Last_Stm) = N_Pragma
6553 -- Don't count call to SS_Release (can happen after Raise_Exception)
6556 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6558 Nkind (Name (Last_Stm)) = N_Identifier
6560 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6562 -- Don't count exception junk
6565 (Nkind_In (Last_Stm, N_Goto_Statement,
6567 N_Object_Declaration)
6568 and then Exception_Junk (Last_Stm))
6569 or else Nkind (Last_Stm) in N_Push_xxx_Label
6570 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6572 -- Inserted code, such as finalization calls, is irrelevant: we only
6573 -- need to check original source.
6575 or else Is_Rewrite_Insertion (Last_Stm)
6580 -- Here we have the "real" last statement
6582 Kind := Nkind (Last_Stm);
6584 -- Transfer of control, OK. Note that in the No_Return procedure
6585 -- case, we already diagnosed any explicit return statements, so
6586 -- we can treat them as OK in this context.
6588 if Is_Transfer (Last_Stm) then
6591 -- Check cases of explicit non-indirect procedure calls
6593 elsif Kind = N_Procedure_Call_Statement
6594 and then Is_Entity_Name (Name (Last_Stm))
6596 -- Check call to Raise_Exception procedure which is treated
6597 -- specially, as is a call to Reraise_Occurrence.
6599 -- We suppress the warning in these cases since it is likely that
6600 -- the programmer really does not expect to deal with the case
6601 -- of Null_Occurrence, and thus would find a warning about a
6602 -- missing return curious, and raising Program_Error does not
6603 -- seem such a bad behavior if this does occur.
6605 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6606 -- behavior will be to raise Constraint_Error (see AI-329).
6608 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6610 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6612 Raise_Exception_Call := True;
6614 -- For Raise_Exception call, test first argument, if it is
6615 -- an attribute reference for a 'Identity call, then we know
6616 -- that the call cannot possibly return.
6619 Arg : constant Node_Id :=
6620 Original_Node (First_Actual (Last_Stm));
6622 if Nkind (Arg) = N_Attribute_Reference
6623 and then Attribute_Name (Arg) = Name_Identity
6630 -- If statement, need to look inside if there is an else and check
6631 -- each constituent statement sequence for proper termination.
6633 elsif Kind = N_If_Statement
6634 and then Present (Else_Statements (Last_Stm))
6636 Check_Statement_Sequence (Then_Statements (Last_Stm));
6637 Check_Statement_Sequence (Else_Statements (Last_Stm));
6639 if Present (Elsif_Parts (Last_Stm)) then
6641 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6644 while Present (Elsif_Part) loop
6645 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6653 -- Case statement, check each case for proper termination
6655 elsif Kind = N_Case_Statement then
6659 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6660 while Present (Case_Alt) loop
6661 Check_Statement_Sequence (Statements (Case_Alt));
6662 Next_Non_Pragma (Case_Alt);
6668 -- Block statement, check its handled sequence of statements
6670 elsif Kind = N_Block_Statement then
6676 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6685 -- Loop statement. If there is an iteration scheme, we can definitely
6686 -- fall out of the loop. Similarly if there is an exit statement, we
6687 -- can fall out. In either case we need a following return.
6689 elsif Kind = N_Loop_Statement then
6690 if Present (Iteration_Scheme (Last_Stm))
6691 or else Has_Exit (Entity (Identifier (Last_Stm)))
6695 -- A loop with no exit statement or iteration scheme is either
6696 -- an infinite loop, or it has some other exit (raise/return).
6697 -- In either case, no warning is required.
6703 -- Timed entry call, check entry call and delay alternatives
6705 -- Note: in expanded code, the timed entry call has been converted
6706 -- to a set of expanded statements on which the check will work
6707 -- correctly in any case.
6709 elsif Kind = N_Timed_Entry_Call then
6711 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6712 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6715 -- If statement sequence of entry call alternative is missing,
6716 -- then we can definitely fall through, and we post the error
6717 -- message on the entry call alternative itself.
6719 if No (Statements (ECA)) then
6722 -- If statement sequence of delay alternative is missing, then
6723 -- we can definitely fall through, and we post the error
6724 -- message on the delay alternative itself.
6726 -- Note: if both ECA and DCA are missing the return, then we
6727 -- post only one message, should be enough to fix the bugs.
6728 -- If not we will get a message next time on the DCA when the
6731 elsif No (Statements (DCA)) then
6734 -- Else check both statement sequences
6737 Check_Statement_Sequence (Statements (ECA));
6738 Check_Statement_Sequence (Statements (DCA));
6743 -- Conditional entry call, check entry call and else part
6745 -- Note: in expanded code, the conditional entry call has been
6746 -- converted to a set of expanded statements on which the check
6747 -- will work correctly in any case.
6749 elsif Kind = N_Conditional_Entry_Call then
6751 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6754 -- If statement sequence of entry call alternative is missing,
6755 -- then we can definitely fall through, and we post the error
6756 -- message on the entry call alternative itself.
6758 if No (Statements (ECA)) then
6761 -- Else check statement sequence and else part
6764 Check_Statement_Sequence (Statements (ECA));
6765 Check_Statement_Sequence (Else_Statements (Last_Stm));
6771 -- If we fall through, issue appropriate message
6775 -- Kill warning if last statement is a raise exception call,
6776 -- or a pragma Assert (False). Note that with assertions enabled,
6777 -- such a pragma has been converted into a raise exception call
6778 -- already, so the Assert_False is for the assertions off case.
6780 if not Raise_Exception_Call and then not Assert_False then
6782 -- In GNATprove mode, it is an error to have a missing return
6784 Error_Msg_Warn := SPARK_Mode /= On;
6786 -- Issue error message or warning
6789 ("RETURN statement missing following this statement<<!",
6792 ("\Program_Error ]<<!", Last_Stm);
6795 -- Note: we set Err even though we have not issued a warning
6796 -- because we still have a case of a missing return. This is
6797 -- an extremely marginal case, probably will never be noticed
6798 -- but we might as well get it right.
6802 -- Otherwise we have the case of a procedure marked No_Return
6805 if not Raise_Exception_Call then
6806 if GNATprove_Mode then
6808 ("implied return after this statement would have raised "
6809 & "Program_Error", Last_Stm);
6811 -- In normal compilation mode, do not warn on a generated call
6812 -- (e.g. in the body of a renaming as completion).
6814 elsif Comes_From_Source (Last_Stm) then
6816 ("implied return after this statement will raise "
6817 & "Program_Error??", Last_Stm);
6820 Error_Msg_Warn := SPARK_Mode /= On;
6822 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6826 RE : constant Node_Id :=
6827 Make_Raise_Program_Error (Sloc (Last_Stm),
6828 Reason => PE_Implicit_Return);
6830 Insert_After (Last_Stm, RE);
6834 end Check_Statement_Sequence;
6836 -- Start of processing for Check_Returns
6840 Check_Statement_Sequence (Statements (HSS));
6842 if Present (Exception_Handlers (HSS)) then
6843 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6844 while Present (Handler) loop
6845 Check_Statement_Sequence (Statements (Handler));
6846 Next_Non_Pragma (Handler);
6851 ----------------------------
6852 -- Check_Subprogram_Order --
6853 ----------------------------
6855 procedure Check_Subprogram_Order (N : Node_Id) is
6857 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6858 -- This is used to check if S1 > S2 in the sense required by this test,
6859 -- for example nameab < namec, but name2 < name10.
6861 -----------------------------
6862 -- Subprogram_Name_Greater --
6863 -----------------------------
6865 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6870 -- Deal with special case where names are identical except for a
6871 -- numerical suffix. These are handled specially, taking the numeric
6872 -- ordering from the suffix into account.
6875 while S1 (L1) in '0' .. '9' loop
6880 while S2 (L2) in '0' .. '9' loop
6884 -- If non-numeric parts non-equal, do straight compare
6886 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6889 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6890 -- that a missing suffix is treated as numeric zero in this test.
6894 while L1 < S1'Last loop
6896 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6900 while L2 < S2'Last loop
6902 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6907 end Subprogram_Name_Greater;
6909 -- Start of processing for Check_Subprogram_Order
6912 -- Check body in alpha order if this is option
6915 and then Style_Check_Order_Subprograms
6916 and then Nkind (N) = N_Subprogram_Body
6917 and then Comes_From_Source (N)
6918 and then In_Extended_Main_Source_Unit (N)
6922 renames Scope_Stack.Table
6923 (Scope_Stack.Last).Last_Subprogram_Name;
6925 Body_Id : constant Entity_Id :=
6926 Defining_Entity (Specification (N));
6929 Get_Decoded_Name_String (Chars (Body_Id));
6932 if Subprogram_Name_Greater
6933 (LSN.all, Name_Buffer (1 .. Name_Len))
6935 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6941 LSN := new String'(Name_Buffer (1 .. Name_Len));
6944 end Check_Subprogram_Order;
6946 ------------------------------
6947 -- Check_Subtype_Conformant --
6948 ------------------------------
6950 procedure Check_Subtype_Conformant
6951 (New_Id : Entity_Id;
6953 Err_Loc : Node_Id := Empty;
6954 Skip_Controlling_Formals : Boolean := False;
6955 Get_Inst : Boolean := False)
6958 pragma Warnings (Off, Result);
6961 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6962 Skip_Controlling_Formals => Skip_Controlling_Formals,
6963 Get_Inst => Get_Inst);
6964 end Check_Subtype_Conformant;
6966 -----------------------------------
6967 -- Check_Synchronized_Overriding --
6968 -----------------------------------
6970 procedure Check_Synchronized_Overriding
6971 (Def_Id : Entity_Id;
6972 Overridden_Subp : out Entity_Id)
6974 Ifaces_List : Elist_Id;
6978 function Matches_Prefixed_View_Profile
6979 (Prim_Params : List_Id;
6980 Iface_Params : List_Id) return Boolean;
6981 -- Determine whether a subprogram's parameter profile Prim_Params
6982 -- matches that of a potentially overridden interface subprogram
6983 -- Iface_Params. Also determine if the type of first parameter of
6984 -- Iface_Params is an implemented interface.
6986 -----------------------------------
6987 -- Matches_Prefixed_View_Profile --
6988 -----------------------------------
6990 function Matches_Prefixed_View_Profile
6991 (Prim_Params : List_Id;
6992 Iface_Params : List_Id) return Boolean
6994 function Is_Implemented
6995 (Ifaces_List : Elist_Id;
6996 Iface : Entity_Id) return Boolean;
6997 -- Determine if Iface is implemented by the current task or
7000 --------------------
7001 -- Is_Implemented --
7002 --------------------
7004 function Is_Implemented
7005 (Ifaces_List : Elist_Id;
7006 Iface : Entity_Id) return Boolean
7008 Iface_Elmt : Elmt_Id;
7011 Iface_Elmt := First_Elmt (Ifaces_List);
7012 while Present (Iface_Elmt) loop
7013 if Node (Iface_Elmt) = Iface then
7017 Next_Elmt (Iface_Elmt);
7025 Iface_Id : Entity_Id;
7026 Iface_Param : Node_Id;
7027 Iface_Typ : Entity_Id;
7028 Prim_Id : Entity_Id;
7029 Prim_Param : Node_Id;
7030 Prim_Typ : Entity_Id;
7032 -- Start of processing for Matches_Prefixed_View_Profile
7035 Iface_Param := First (Iface_Params);
7036 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7038 if Is_Access_Type (Iface_Typ) then
7039 Iface_Typ := Designated_Type (Iface_Typ);
7042 Prim_Param := First (Prim_Params);
7044 -- The first parameter of the potentially overridden subprogram must
7045 -- be an interface implemented by Prim.
7047 if not Is_Interface (Iface_Typ)
7048 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7053 -- The checks on the object parameters are done, so move on to the
7054 -- rest of the parameters.
7056 if not In_Scope then
7057 Prim_Param := Next (Prim_Param);
7060 Iface_Param := Next (Iface_Param);
7061 while Present (Iface_Param) and then Present (Prim_Param) loop
7062 Iface_Id := Defining_Identifier (Iface_Param);
7063 Iface_Typ := Find_Parameter_Type (Iface_Param);
7065 Prim_Id := Defining_Identifier (Prim_Param);
7066 Prim_Typ := Find_Parameter_Type (Prim_Param);
7068 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7069 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7070 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7072 Iface_Typ := Designated_Type (Iface_Typ);
7073 Prim_Typ := Designated_Type (Prim_Typ);
7076 -- Case of multiple interface types inside a parameter profile
7078 -- (Obj_Param : in out Iface; ...; Param : Iface)
7080 -- If the interface type is implemented, then the matching type in
7081 -- the primitive should be the implementing record type.
7083 if Ekind (Iface_Typ) = E_Record_Type
7084 and then Is_Interface (Iface_Typ)
7085 and then Is_Implemented (Ifaces_List, Iface_Typ)
7087 if Prim_Typ /= Typ then
7091 -- The two parameters must be both mode and subtype conformant
7093 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7095 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7104 -- One of the two lists contains more parameters than the other
7106 if Present (Iface_Param) or else Present (Prim_Param) then
7111 end Matches_Prefixed_View_Profile;
7113 -- Start of processing for Check_Synchronized_Overriding
7116 Overridden_Subp := Empty;
7118 -- Def_Id must be an entry or a subprogram. We should skip predefined
7119 -- primitives internally generated by the front end; however at this
7120 -- stage predefined primitives are still not fully decorated. As a
7121 -- minor optimization we skip here internally generated subprograms.
7123 if (Ekind (Def_Id) /= E_Entry
7124 and then Ekind (Def_Id) /= E_Function
7125 and then Ekind (Def_Id) /= E_Procedure)
7126 or else not Comes_From_Source (Def_Id)
7131 -- Search for the concurrent declaration since it contains the list of
7132 -- all implemented interfaces. In this case, the subprogram is declared
7133 -- within the scope of a protected or a task type.
7135 if Present (Scope (Def_Id))
7136 and then Is_Concurrent_Type (Scope (Def_Id))
7137 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7139 Typ := Scope (Def_Id);
7142 -- The enclosing scope is not a synchronized type and the subprogram
7145 elsif No (First_Formal (Def_Id)) then
7148 -- The subprogram has formals and hence it may be a primitive of a
7152 Typ := Etype (First_Formal (Def_Id));
7154 if Is_Access_Type (Typ) then
7155 Typ := Directly_Designated_Type (Typ);
7158 if Is_Concurrent_Type (Typ)
7159 and then not Is_Generic_Actual_Type (Typ)
7163 -- This case occurs when the concurrent type is declared within a
7164 -- generic unit. As a result the corresponding record has been built
7165 -- and used as the type of the first formal, we just have to retrieve
7166 -- the corresponding concurrent type.
7168 elsif Is_Concurrent_Record_Type (Typ)
7169 and then not Is_Class_Wide_Type (Typ)
7170 and then Present (Corresponding_Concurrent_Type (Typ))
7172 Typ := Corresponding_Concurrent_Type (Typ);
7180 -- There is no overriding to check if this is an inherited operation in
7181 -- a type derivation for a generic actual.
7183 Collect_Interfaces (Typ, Ifaces_List);
7185 if Is_Empty_Elmt_List (Ifaces_List) then
7189 -- Determine whether entry or subprogram Def_Id overrides a primitive
7190 -- operation that belongs to one of the interfaces in Ifaces_List.
7193 Candidate : Entity_Id := Empty;
7194 Hom : Entity_Id := Empty;
7195 Subp : Entity_Id := Empty;
7198 -- Traverse the homonym chain, looking for a potentially overridden
7199 -- subprogram that belongs to an implemented interface.
7201 Hom := Current_Entity_In_Scope (Def_Id);
7202 while Present (Hom) loop
7206 or else not Is_Overloadable (Subp)
7207 or else not Is_Primitive (Subp)
7208 or else not Is_Dispatching_Operation (Subp)
7209 or else not Present (Find_Dispatching_Type (Subp))
7210 or else not Is_Interface (Find_Dispatching_Type (Subp))
7214 -- Entries and procedures can override abstract or null interface
7217 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7218 and then Ekind (Subp) = E_Procedure
7219 and then Matches_Prefixed_View_Profile
7220 (Parameter_Specifications (Parent (Def_Id)),
7221 Parameter_Specifications (Parent (Subp)))
7225 -- For an overridden subprogram Subp, check whether the mode
7226 -- of its first parameter is correct depending on the kind of
7227 -- synchronized type.
7230 Formal : constant Node_Id := First_Formal (Candidate);
7233 -- In order for an entry or a protected procedure to
7234 -- override, the first parameter of the overridden routine
7235 -- must be of mode "out", "in out", or access-to-variable.
7237 if Ekind_In (Candidate, E_Entry, E_Procedure)
7238 and then Is_Protected_Type (Typ)
7239 and then Ekind (Formal) /= E_In_Out_Parameter
7240 and then Ekind (Formal) /= E_Out_Parameter
7241 and then Nkind (Parameter_Type (Parent (Formal))) /=
7246 -- All other cases are OK since a task entry or routine does
7247 -- not have a restriction on the mode of the first parameter
7248 -- of the overridden interface routine.
7251 Overridden_Subp := Candidate;
7256 -- Functions can override abstract interface functions
7258 elsif Ekind (Def_Id) = E_Function
7259 and then Ekind (Subp) = E_Function
7260 and then Matches_Prefixed_View_Profile
7261 (Parameter_Specifications (Parent (Def_Id)),
7262 Parameter_Specifications (Parent (Subp)))
7263 and then Etype (Def_Id) = Etype (Subp)
7267 -- If an inherited subprogram is implemented by a protected
7268 -- function, then the first parameter of the inherited
7269 -- subprogram shall be of mode in, but not an access-to-
7270 -- variable parameter (RM 9.4(11/9)).
7272 if Present (First_Formal (Subp))
7273 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7275 (not Is_Access_Type (Etype (First_Formal (Subp)))
7277 Is_Access_Constant (Etype (First_Formal (Subp))))
7279 Overridden_Subp := Subp;
7284 Hom := Homonym (Hom);
7287 -- After examining all candidates for overriding, we are left with
7288 -- the best match, which is a mode-incompatible interface routine.
7290 if In_Scope and then Present (Candidate) then
7291 Error_Msg_PT (Def_Id, Candidate);
7294 Overridden_Subp := Candidate;
7297 end Check_Synchronized_Overriding;
7299 ---------------------------
7300 -- Check_Type_Conformant --
7301 ---------------------------
7303 procedure Check_Type_Conformant
7304 (New_Id : Entity_Id;
7306 Err_Loc : Node_Id := Empty)
7309 pragma Warnings (Off, Result);
7312 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7313 end Check_Type_Conformant;
7315 ---------------------------
7316 -- Can_Override_Operator --
7317 ---------------------------
7319 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7323 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7327 Typ := Base_Type (Etype (First_Formal (Subp)));
7329 -- Check explicitly that the operation is a primitive of the type
7331 return Operator_Matches_Spec (Subp, Subp)
7332 and then not Is_Generic_Type (Typ)
7333 and then Scope (Subp) = Scope (Typ)
7334 and then not Is_Class_Wide_Type (Typ);
7336 end Can_Override_Operator;
7338 ----------------------
7339 -- Conforming_Types --
7340 ----------------------
7342 function Conforming_Types
7345 Ctype : Conformance_Type;
7346 Get_Inst : Boolean := False) return Boolean
7348 function Base_Types_Match
7350 Typ_2 : Entity_Id) return Boolean;
7351 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7352 -- in different scopes (e.g. parent and child instances), then verify
7353 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7354 -- the same subtype chain. The whole purpose of this procedure is to
7355 -- prevent spurious ambiguities in an instantiation that may arise if
7356 -- two distinct generic types are instantiated with the same actual.
7358 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7359 -- An access parameter can designate an incomplete type. If the
7360 -- incomplete type is the limited view of a type from a limited_
7361 -- with_clause, check whether the non-limited view is available.
7362 -- If it is a (non-limited) incomplete type, get the full view.
7364 function Matches_Limited_With_View
7366 Typ_2 : Entity_Id) return Boolean;
7367 -- Returns True if and only if either Typ_1 denotes a limited view of
7368 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7369 -- the limited with view of a type is used in a subprogram declaration
7370 -- and the subprogram body is in the scope of a regular with clause for
7371 -- the same unit. In such a case, the two type entities are considered
7372 -- identical for purposes of conformance checking.
7374 ----------------------
7375 -- Base_Types_Match --
7376 ----------------------
7378 function Base_Types_Match
7380 Typ_2 : Entity_Id) return Boolean
7382 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7383 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7386 if Typ_1 = Typ_2 then
7389 elsif Base_1 = Base_2 then
7391 -- The following is too permissive. A more precise test should
7392 -- check that the generic actual is an ancestor subtype of the
7395 -- See code in Find_Corresponding_Spec that applies an additional
7396 -- filter to handle accidental amiguities in instances.
7399 not Is_Generic_Actual_Type (Typ_1)
7400 or else not Is_Generic_Actual_Type (Typ_2)
7401 or else Scope (Typ_1) /= Scope (Typ_2);
7403 -- If Typ_2 is a generic actual type it is declared as the subtype of
7404 -- the actual. If that actual is itself a subtype we need to use its
7405 -- own base type to check for compatibility.
7407 elsif Ekind (Base_2) = Ekind (Typ_2)
7408 and then Base_1 = Base_Type (Base_2)
7412 elsif Ekind (Base_1) = Ekind (Typ_1)
7413 and then Base_2 = Base_Type (Base_1)
7420 end Base_Types_Match;
7422 --------------------------
7423 -- Find_Designated_Type --
7424 --------------------------
7426 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7430 Desig := Directly_Designated_Type (Typ);
7432 if Ekind (Desig) = E_Incomplete_Type then
7434 -- If regular incomplete type, get full view if available
7436 if Present (Full_View (Desig)) then
7437 Desig := Full_View (Desig);
7439 -- If limited view of a type, get non-limited view if available,
7440 -- and check again for a regular incomplete type.
7442 elsif Present (Non_Limited_View (Desig)) then
7443 Desig := Get_Full_View (Non_Limited_View (Desig));
7448 end Find_Designated_Type;
7450 -------------------------------
7451 -- Matches_Limited_With_View --
7452 -------------------------------
7454 function Matches_Limited_With_View
7456 Typ_2 : Entity_Id) return Boolean
7458 function Is_Matching_Limited_View
7460 View : Entity_Id) return Boolean;
7461 -- Determine whether non-limited view View denotes type Typ in some
7462 -- conformant fashion.
7464 ------------------------------
7465 -- Is_Matching_Limited_View --
7466 ------------------------------
7468 function Is_Matching_Limited_View
7470 View : Entity_Id) return Boolean
7472 Root_Typ : Entity_Id;
7473 Root_View : Entity_Id;
7476 -- The non-limited view directly denotes the type
7481 -- The type is a subtype of the non-limited view
7483 elsif Is_Subtype_Of (Typ, View) then
7486 -- Both the non-limited view and the type denote class-wide types
7488 elsif Is_Class_Wide_Type (Typ)
7489 and then Is_Class_Wide_Type (View)
7491 Root_Typ := Root_Type (Typ);
7492 Root_View := Root_Type (View);
7494 if Root_Typ = Root_View then
7497 -- An incomplete tagged type and its full view may receive two
7498 -- distinct class-wide types when the related package has not
7499 -- been analyzed yet.
7502 -- type T is tagged; -- CW_1
7503 -- type T is tagged null record; -- CW_2
7506 -- This is because the package lacks any semantic information
7507 -- that may eventually link both views of T. As a consequence,
7508 -- a client of the limited view of Pack will see CW_2 while a
7509 -- client of the non-limited view of Pack will see CW_1.
7511 elsif Is_Incomplete_Type (Root_Typ)
7512 and then Present (Full_View (Root_Typ))
7513 and then Full_View (Root_Typ) = Root_View
7517 elsif Is_Incomplete_Type (Root_View)
7518 and then Present (Full_View (Root_View))
7519 and then Full_View (Root_View) = Root_Typ
7526 end Is_Matching_Limited_View;
7528 -- Start of processing for Matches_Limited_With_View
7531 -- In some cases a type imported through a limited_with clause, and
7532 -- its non-limited view are both visible, for example in an anonymous
7533 -- access-to-class-wide type in a formal, or when building the body
7534 -- for a subprogram renaming after the subprogram has been frozen.
7535 -- In these cases both entities designate the same type. In addition,
7536 -- if one of them is an actual in an instance, it may be a subtype of
7537 -- the non-limited view of the other.
7539 if From_Limited_With (Typ_1)
7540 and then From_Limited_With (Typ_2)
7541 and then Available_View (Typ_1) = Available_View (Typ_2)
7545 elsif From_Limited_With (Typ_1) then
7546 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7548 elsif From_Limited_With (Typ_2) then
7549 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7554 end Matches_Limited_With_View;
7558 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7560 Type_1 : Entity_Id := T1;
7561 Type_2 : Entity_Id := T2;
7563 -- Start of processing for Conforming_Types
7566 -- The context is an instance association for a formal access-to-
7567 -- subprogram type; the formal parameter types require mapping because
7568 -- they may denote other formal parameters of the generic unit.
7571 Type_1 := Get_Instance_Of (T1);
7572 Type_2 := Get_Instance_Of (T2);
7575 -- If one of the types is a view of the other introduced by a limited
7576 -- with clause, treat these as conforming for all purposes.
7578 if Matches_Limited_With_View (T1, T2) then
7581 elsif Base_Types_Match (Type_1, Type_2) then
7582 if Ctype <= Mode_Conformant then
7587 Subtypes_Statically_Match (Type_1, Type_2)
7588 and then Dimensions_Match (Type_1, Type_2);
7591 elsif Is_Incomplete_Or_Private_Type (Type_1)
7592 and then Present (Full_View (Type_1))
7593 and then Base_Types_Match (Full_View (Type_1), Type_2)
7596 Ctype <= Mode_Conformant
7597 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7599 elsif Ekind (Type_2) = E_Incomplete_Type
7600 and then Present (Full_View (Type_2))
7601 and then Base_Types_Match (Type_1, Full_View (Type_2))
7604 Ctype <= Mode_Conformant
7605 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7607 elsif Is_Private_Type (Type_2)
7608 and then In_Instance
7609 and then Present (Full_View (Type_2))
7610 and then Base_Types_Match (Type_1, Full_View (Type_2))
7613 Ctype <= Mode_Conformant
7614 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7616 -- Another confusion between views in a nested instance with an
7617 -- actual private type whose full view is not in scope.
7619 elsif Ekind (Type_2) = E_Private_Subtype
7620 and then In_Instance
7621 and then Etype (Type_2) = Type_1
7625 -- In Ada 2012, incomplete types (including limited views) can appear
7626 -- as actuals in instantiations.
7628 elsif Is_Incomplete_Type (Type_1)
7629 and then Is_Incomplete_Type (Type_2)
7630 and then (Used_As_Generic_Actual (Type_1)
7631 or else Used_As_Generic_Actual (Type_2))
7636 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7637 -- treated recursively because they carry a signature. As far as
7638 -- conformance is concerned, convention plays no role, and either
7639 -- or both could be access to protected subprograms.
7641 Are_Anonymous_Access_To_Subprogram_Types :=
7642 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7643 E_Anonymous_Access_Protected_Subprogram_Type)
7645 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7646 E_Anonymous_Access_Protected_Subprogram_Type);
7648 -- Test anonymous access type case. For this case, static subtype
7649 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7650 -- the base types because we may have built internal subtype entities
7651 -- to handle null-excluding types (see Process_Formals).
7653 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7655 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7657 -- Ada 2005 (AI-254)
7659 or else Are_Anonymous_Access_To_Subprogram_Types
7662 Desig_1 : Entity_Id;
7663 Desig_2 : Entity_Id;
7666 -- In Ada 2005, access constant indicators must match for
7667 -- subtype conformance.
7669 if Ada_Version >= Ada_2005
7670 and then Ctype >= Subtype_Conformant
7672 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7677 Desig_1 := Find_Designated_Type (Type_1);
7678 Desig_2 := Find_Designated_Type (Type_2);
7680 -- If the context is an instance association for a formal
7681 -- access-to-subprogram type; formal access parameter designated
7682 -- types require mapping because they may denote other formal
7683 -- parameters of the generic unit.
7686 Desig_1 := Get_Instance_Of (Desig_1);
7687 Desig_2 := Get_Instance_Of (Desig_2);
7690 -- It is possible for a Class_Wide_Type to be introduced for an
7691 -- incomplete type, in which case there is a separate class_ wide
7692 -- type for the full view. The types conform if their Etypes
7693 -- conform, i.e. one may be the full view of the other. This can
7694 -- only happen in the context of an access parameter, other uses
7695 -- of an incomplete Class_Wide_Type are illegal.
7697 if Is_Class_Wide_Type (Desig_1)
7699 Is_Class_Wide_Type (Desig_2)
7703 (Etype (Base_Type (Desig_1)),
7704 Etype (Base_Type (Desig_2)), Ctype);
7706 elsif Are_Anonymous_Access_To_Subprogram_Types then
7707 if Ada_Version < Ada_2005 then
7709 Ctype = Type_Conformant
7710 or else Subtypes_Statically_Match (Desig_1, Desig_2);
7712 -- We must check the conformance of the signatures themselves
7716 Conformant : Boolean;
7719 (Desig_1, Desig_2, Ctype, False, Conformant);
7724 -- A limited view of an actual matches the corresponding
7725 -- incomplete formal.
7727 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7728 and then From_Limited_With (Desig_2)
7729 and then Used_As_Generic_Actual (Etype (Desig_2))
7734 return Base_Type (Desig_1) = Base_Type (Desig_2)
7735 and then (Ctype = Type_Conformant
7737 Subtypes_Statically_Match (Desig_1, Desig_2));
7741 -- Otherwise definitely no match
7744 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7745 and then Is_Access_Type (Type_2))
7746 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7747 and then Is_Access_Type (Type_1)))
7750 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7752 May_Hide_Profile := True;
7757 end Conforming_Types;
7759 --------------------------
7760 -- Create_Extra_Formals --
7761 --------------------------
7763 procedure Create_Extra_Formals (E : Entity_Id) is
7764 First_Extra : Entity_Id := Empty;
7766 Last_Extra : Entity_Id := Empty;
7768 function Add_Extra_Formal
7769 (Assoc_Entity : Entity_Id;
7772 Suffix : String) return Entity_Id;
7773 -- Add an extra formal to the current list of formals and extra formals.
7774 -- The extra formal is added to the end of the list of extra formals,
7775 -- and also returned as the result. These formals are always of mode IN.
7776 -- The new formal has the type Typ, is declared in Scope, and its name
7777 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7778 -- The following suffixes are currently used. They should not be changed
7779 -- without coordinating with CodePeer, which makes use of these to
7780 -- provide better messages.
7782 -- O denotes the Constrained bit.
7783 -- L denotes the accessibility level.
7784 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7785 -- the full list in exp_ch6.BIP_Formal_Kind.
7787 ----------------------
7788 -- Add_Extra_Formal --
7789 ----------------------
7791 function Add_Extra_Formal
7792 (Assoc_Entity : Entity_Id;
7795 Suffix : String) return Entity_Id
7797 EF : constant Entity_Id :=
7798 Make_Defining_Identifier (Sloc (Assoc_Entity),
7799 Chars => New_External_Name (Chars (Assoc_Entity),
7803 -- A little optimization. Never generate an extra formal for the
7804 -- _init operand of an initialization procedure, since it could
7807 if Chars (Formal) = Name_uInit then
7811 Set_Ekind (EF, E_In_Parameter);
7812 Set_Actual_Subtype (EF, Typ);
7813 Set_Etype (EF, Typ);
7814 Set_Scope (EF, Scope);
7815 Set_Mechanism (EF, Default_Mechanism);
7816 Set_Formal_Validity (EF);
7818 if No (First_Extra) then
7820 Set_Extra_Formals (Scope, First_Extra);
7823 if Present (Last_Extra) then
7824 Set_Extra_Formal (Last_Extra, EF);
7830 end Add_Extra_Formal;
7834 Formal_Type : Entity_Id;
7835 P_Formal : Entity_Id := Empty;
7837 -- Start of processing for Create_Extra_Formals
7840 -- We never generate extra formals if expansion is not active because we
7841 -- don't need them unless we are generating code.
7843 if not Expander_Active then
7847 -- No need to generate extra formals in interface thunks whose target
7848 -- primitive has no extra formals.
7850 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7854 -- If this is a derived subprogram then the subtypes of the parent
7855 -- subprogram's formal parameters will be used to determine the need
7856 -- for extra formals.
7858 if Is_Overloadable (E) and then Present (Alias (E)) then
7859 P_Formal := First_Formal (Alias (E));
7862 Formal := First_Formal (E);
7863 while Present (Formal) loop
7864 Last_Extra := Formal;
7865 Next_Formal (Formal);
7868 -- If Extra_Formals were already created, don't do it again. This
7869 -- situation may arise for subprogram types created as part of
7870 -- dispatching calls (see Expand_Dispatching_Call)
7872 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7876 -- If the subprogram is a predefined dispatching subprogram then don't
7877 -- generate any extra constrained or accessibility level formals. In
7878 -- general we suppress these for internal subprograms (by not calling
7879 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7880 -- generated stream attributes do get passed through because extra
7881 -- build-in-place formals are needed in some cases (limited 'Input).
7883 if Is_Predefined_Internal_Operation (E) then
7884 goto Test_For_Func_Result_Extras;
7887 Formal := First_Formal (E);
7888 while Present (Formal) loop
7890 -- Create extra formal for supporting the attribute 'Constrained.
7891 -- The case of a private type view without discriminants also
7892 -- requires the extra formal if the underlying type has defaulted
7895 if Ekind (Formal) /= E_In_Parameter then
7896 if Present (P_Formal) then
7897 Formal_Type := Etype (P_Formal);
7899 Formal_Type := Etype (Formal);
7902 -- Do not produce extra formals for Unchecked_Union parameters.
7903 -- Jump directly to the end of the loop.
7905 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7906 goto Skip_Extra_Formal_Generation;
7909 if not Has_Discriminants (Formal_Type)
7910 and then Ekind (Formal_Type) in Private_Kind
7911 and then Present (Underlying_Type (Formal_Type))
7913 Formal_Type := Underlying_Type (Formal_Type);
7916 -- Suppress the extra formal if formal's subtype is constrained or
7917 -- indefinite, or we're compiling for Ada 2012 and the underlying
7918 -- type is tagged and limited. In Ada 2012, a limited tagged type
7919 -- can have defaulted discriminants, but 'Constrained is required
7920 -- to return True, so the formal is never needed (see AI05-0214).
7921 -- Note that this ensures consistency of calling sequences for
7922 -- dispatching operations when some types in a class have defaults
7923 -- on discriminants and others do not (and requiring the extra
7924 -- formal would introduce distributed overhead).
7926 -- If the type does not have a completion yet, treat as prior to
7927 -- Ada 2012 for consistency.
7929 if Has_Discriminants (Formal_Type)
7930 and then not Is_Constrained (Formal_Type)
7931 and then Is_Definite_Subtype (Formal_Type)
7932 and then (Ada_Version < Ada_2012
7933 or else No (Underlying_Type (Formal_Type))
7935 (Is_Limited_Type (Formal_Type)
7938 (Underlying_Type (Formal_Type)))))
7940 Set_Extra_Constrained
7941 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7945 -- Create extra formal for supporting accessibility checking. This
7946 -- is done for both anonymous access formals and formals of named
7947 -- access types that are marked as controlling formals. The latter
7948 -- case can occur when Expand_Dispatching_Call creates a subprogram
7949 -- type and substitutes the types of access-to-class-wide actuals
7950 -- for the anonymous access-to-specific-type of controlling formals.
7951 -- Base_Type is applied because in cases where there is a null
7952 -- exclusion the formal may have an access subtype.
7954 -- This is suppressed if we specifically suppress accessibility
7955 -- checks at the package level for either the subprogram, or the
7956 -- package in which it resides. However, we do not suppress it
7957 -- simply if the scope has accessibility checks suppressed, since
7958 -- this could cause trouble when clients are compiled with a
7959 -- different suppression setting. The explicit checks at the
7960 -- package level are safe from this point of view.
7962 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7963 or else (Is_Controlling_Formal (Formal)
7964 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7966 (Explicit_Suppress (E, Accessibility_Check)
7968 Explicit_Suppress (Scope (E), Accessibility_Check))
7971 or else Present (Extra_Accessibility (P_Formal)))
7973 Set_Extra_Accessibility
7974 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7977 -- This label is required when skipping extra formal generation for
7978 -- Unchecked_Union parameters.
7980 <<Skip_Extra_Formal_Generation>>
7982 if Present (P_Formal) then
7983 Next_Formal (P_Formal);
7986 Next_Formal (Formal);
7989 <<Test_For_Func_Result_Extras>>
7991 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7992 -- function call is ... determined by the point of call ...".
7994 if Needs_Result_Accessibility_Level (E) then
7995 Set_Extra_Accessibility_Of_Result
7996 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7999 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8000 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8002 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8004 Result_Subt : constant Entity_Id := Etype (E);
8005 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8006 Formal_Typ : Entity_Id;
8007 Subp_Decl : Node_Id;
8009 Discard : Entity_Id;
8010 pragma Warnings (Off, Discard);
8013 -- In the case of functions with unconstrained result subtypes,
8014 -- add a 4-state formal indicating whether the return object is
8015 -- allocated by the caller (1), or should be allocated by the
8016 -- callee on the secondary stack (2), in the global heap (3), or
8017 -- in a user-defined storage pool (4). For the moment we just use
8018 -- Natural for the type of this formal. Note that this formal
8019 -- isn't usually needed in the case where the result subtype is
8020 -- constrained, but it is needed when the function has a tagged
8021 -- result, because generally such functions can be called in a
8022 -- dispatching context and such calls must be handled like calls
8023 -- to a class-wide function.
8025 if Needs_BIP_Alloc_Form (E) then
8028 (E, Standard_Natural,
8029 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8031 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8032 -- use a user-defined pool. This formal is not added on
8033 -- ZFP as those targets do not support pools.
8035 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8038 (E, RTE (RE_Root_Storage_Pool_Ptr),
8039 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8043 -- In the case of functions whose result type needs finalization,
8044 -- add an extra formal which represents the finalization master.
8046 if Needs_BIP_Finalization_Master (E) then
8049 (E, RTE (RE_Finalization_Master_Ptr),
8050 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8053 -- When the result type contains tasks, add two extra formals: the
8054 -- master of the tasks to be created, and the caller's activation
8057 if Has_Task (Full_Subt) then
8060 (E, RTE (RE_Master_Id),
8061 E, BIP_Formal_Suffix (BIP_Task_Master));
8064 (E, RTE (RE_Activation_Chain_Access),
8065 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8068 -- All build-in-place functions get an extra formal that will be
8069 -- passed the address of the return object within the caller.
8072 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8074 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8075 Set_Etype (Formal_Typ, Formal_Typ);
8076 Set_Depends_On_Private
8077 (Formal_Typ, Has_Private_Component (Formal_Typ));
8078 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8079 Set_Is_Access_Constant (Formal_Typ, False);
8081 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8082 -- the designated type comes from the limited view (for back-end
8085 Set_From_Limited_With
8086 (Formal_Typ, From_Limited_With (Result_Subt));
8088 Layout_Type (Formal_Typ);
8090 -- Force the definition of the Itype in case of internal function
8091 -- calls within the same or nested scope.
8093 if Is_Subprogram_Or_Generic_Subprogram (E) then
8094 Subp_Decl := Parent (E);
8096 -- The insertion point for an Itype reference should be after
8097 -- the unit declaration node of the subprogram. An exception
8098 -- to this are inherited operations from a parent type in which
8099 -- case the derived type acts as their parent.
8101 if Nkind_In (Subp_Decl, N_Function_Specification,
8102 N_Procedure_Specification)
8104 Subp_Decl := Parent (Subp_Decl);
8107 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8112 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8115 end Create_Extra_Formals;
8117 -----------------------------
8118 -- Enter_Overloaded_Entity --
8119 -----------------------------
8121 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8122 function Matches_Predefined_Op return Boolean;
8123 -- This returns an approximation of whether S matches a predefined
8124 -- operator, based on the operator symbol, and the parameter and result
8125 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8127 ---------------------------
8128 -- Matches_Predefined_Op --
8129 ---------------------------
8131 function Matches_Predefined_Op return Boolean is
8132 Formal_1 : constant Entity_Id := First_Formal (S);
8133 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8134 Op : constant Name_Id := Chars (S);
8135 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8136 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8141 if Present (Formal_2) then
8143 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8146 -- All but "&" and "**" have same-types parameters
8155 if Type_1 /= Type_2 then
8160 -- Check parameter and result types
8168 Is_Boolean_Type (Result_Type)
8169 and then Result_Type = Type_1;
8175 Is_Integer_Type (Result_Type)
8176 and then Result_Type = Type_1;
8184 Is_Numeric_Type (Result_Type)
8185 and then Result_Type = Type_1;
8191 Is_Boolean_Type (Result_Type)
8192 and then not Is_Limited_Type (Type_1);
8200 Is_Boolean_Type (Result_Type)
8201 and then (Is_Array_Type (Type_1)
8202 or else Is_Scalar_Type (Type_1));
8204 when Name_Op_Concat =>
8205 return Is_Array_Type (Result_Type);
8207 when Name_Op_Expon =>
8209 (Is_Integer_Type (Result_Type)
8210 or else Is_Floating_Point_Type (Result_Type))
8211 and then Result_Type = Type_1
8212 and then Type_2 = Standard_Integer;
8215 raise Program_Error;
8228 Is_Numeric_Type (Result_Type)
8229 and then Result_Type = Type_1;
8233 Is_Boolean_Type (Result_Type)
8234 and then Result_Type = Type_1;
8237 raise Program_Error;
8240 end Matches_Predefined_Op;
8244 E : Entity_Id := Current_Entity_In_Scope (S);
8245 C_E : Entity_Id := Current_Entity (S);
8247 -- Start of processing for Enter_Overloaded_Entity
8251 Set_Has_Homonym (E);
8252 Set_Has_Homonym (S);
8255 Set_Is_Immediately_Visible (S);
8256 Set_Scope (S, Current_Scope);
8258 -- Chain new entity if front of homonym in current scope, so that
8259 -- homonyms are contiguous.
8261 if Present (E) and then E /= C_E then
8262 while Homonym (C_E) /= E loop
8263 C_E := Homonym (C_E);
8266 Set_Homonym (C_E, S);
8270 Set_Current_Entity (S);
8275 if Is_Inherited_Operation (S) then
8276 Append_Inherited_Subprogram (S);
8278 Append_Entity (S, Current_Scope);
8281 Set_Public_Status (S);
8283 if Debug_Flag_E then
8284 Write_Str ("New overloaded entity chain: ");
8285 Write_Name (Chars (S));
8288 while Present (E) loop
8289 Write_Str (" "); Write_Int (Int (E));
8296 -- Generate warning for hiding
8299 and then Comes_From_Source (S)
8300 and then In_Extended_Main_Source_Unit (S)
8307 -- Warn unless genuine overloading. Do not emit warning on
8308 -- hiding predefined operators in Standard (these are either an
8309 -- (artifact of our implicit declarations, or simple noise) but
8310 -- keep warning on a operator defined on a local subtype, because
8311 -- of the real danger that different operators may be applied in
8312 -- various parts of the program.
8314 -- Note that if E and S have the same scope, there is never any
8315 -- hiding. Either the two conflict, and the program is illegal,
8316 -- or S is overriding an implicit inherited subprogram.
8318 if Scope (E) /= Scope (S)
8319 and then (not Is_Overloadable (E)
8320 or else Subtype_Conformant (E, S))
8321 and then (Is_Immediately_Visible (E)
8322 or else Is_Potentially_Use_Visible (S))
8324 if Scope (E) = Standard_Standard then
8325 if Nkind (S) = N_Defining_Operator_Symbol
8326 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8328 and then Matches_Predefined_Op
8331 ("declaration of & hides predefined operator?h?", S);
8334 -- E not immediately within Standard
8337 Error_Msg_Sloc := Sloc (E);
8338 Error_Msg_N ("declaration of & hides one #?h?", S);
8343 end Enter_Overloaded_Entity;
8345 -----------------------------
8346 -- Check_Untagged_Equality --
8347 -----------------------------
8349 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8350 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8351 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8355 -- This check applies only if we have a subprogram declaration with an
8356 -- untagged record type.
8358 if Nkind (Decl) /= N_Subprogram_Declaration
8359 or else not Is_Record_Type (Typ)
8360 or else Is_Tagged_Type (Typ)
8365 -- In Ada 2012 case, we will output errors or warnings depending on
8366 -- the setting of debug flag -gnatd.E.
8368 if Ada_Version >= Ada_2012 then
8369 Error_Msg_Warn := Debug_Flag_Dot_EE;
8371 -- In earlier versions of Ada, nothing to do unless we are warning on
8372 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8375 if not Warn_On_Ada_2012_Compatibility then
8380 -- Cases where the type has already been frozen
8382 if Is_Frozen (Typ) then
8384 -- If the type is not declared in a package, or if we are in the body
8385 -- of the package or in some other scope, the new operation is not
8386 -- primitive, and therefore legal, though suspicious. Should we
8387 -- generate a warning in this case ???
8389 if Ekind (Scope (Typ)) /= E_Package
8390 or else Scope (Typ) /= Current_Scope
8394 -- If the type is a generic actual (sub)type, the operation is not
8395 -- primitive either because the base type is declared elsewhere.
8397 elsif Is_Generic_Actual_Type (Typ) then
8400 -- Here we have a definite error of declaration after freezing
8403 if Ada_Version >= Ada_2012 then
8405 ("equality operator must be declared before type & is "
8406 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8408 -- In Ada 2012 mode with error turned to warning, output one
8409 -- more warning to warn that the equality operation may not
8410 -- compose. This is the consequence of ignoring the error.
8412 if Error_Msg_Warn then
8413 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8418 ("equality operator must be declared before type& is "
8419 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8422 -- If we are in the package body, we could just move the
8423 -- declaration to the package spec, so add a message saying that.
8425 if In_Package_Body (Scope (Typ)) then
8426 if Ada_Version >= Ada_2012 then
8428 ("\move declaration to package spec<<", Eq_Op);
8431 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8434 -- Otherwise try to find the freezing point
8437 Obj_Decl := Next (Parent (Typ));
8438 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8439 if Nkind (Obj_Decl) = N_Object_Declaration
8440 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8442 -- Freezing point, output warnings
8444 if Ada_Version >= Ada_2012 then
8446 ("type& is frozen by declaration??", Obj_Decl, Typ);
8448 ("\an equality operator cannot be declared after "
8453 ("type& is frozen by declaration (Ada 2012)?y?",
8456 ("\an equality operator cannot be declared after "
8457 & "this point (Ada 2012)?y?",
8469 -- Here if type is not frozen yet. It is illegal to have a primitive
8470 -- equality declared in the private part if the type is visible.
8472 elsif not In_Same_List (Parent (Typ), Decl)
8473 and then not Is_Limited_Type (Typ)
8475 -- Shouldn't we give an RM reference here???
8477 if Ada_Version >= Ada_2012 then
8479 ("equality operator appears too late<<", Eq_Op);
8482 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8485 -- No error detected
8490 end Check_Untagged_Equality;
8492 -----------------------------
8493 -- Find_Corresponding_Spec --
8494 -----------------------------
8496 function Find_Corresponding_Spec
8498 Post_Error : Boolean := True) return Entity_Id
8500 Spec : constant Node_Id := Specification (N);
8501 Designator : constant Entity_Id := Defining_Entity (Spec);
8505 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8506 -- Even if fully conformant, a body may depend on a generic actual when
8507 -- the spec does not, or vice versa, in which case they were distinct
8508 -- entities in the generic.
8510 -------------------------------
8511 -- Different_Generic_Profile --
8512 -------------------------------
8514 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8517 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8518 -- Check that the types of corresponding formals have the same
8519 -- generic actual if any. We have to account for subtypes of a
8520 -- generic formal, declared between a spec and a body, which may
8521 -- appear distinct in an instance but matched in the generic, and
8522 -- the subtype may be used either in the spec or the body of the
8523 -- subprogram being checked.
8525 -------------------------
8526 -- Same_Generic_Actual --
8527 -------------------------
8529 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8531 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
8532 -- Predicate to check whether S1 is a subtype of S2 in the source
8535 -------------------------
8536 -- Is_Declared_Subtype --
8537 -------------------------
8539 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
8541 return Comes_From_Source (Parent (S1))
8542 and then Nkind (Parent (S1)) = N_Subtype_Declaration
8543 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
8544 and then Entity (Subtype_Indication (Parent (S1))) = S2;
8545 end Is_Declared_Subtype;
8547 -- Start of processing for Same_Generic_Actual
8550 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8551 or else Is_Declared_Subtype (T1, T2)
8552 or else Is_Declared_Subtype (T2, T1);
8553 end Same_Generic_Actual;
8555 -- Start of processing for Different_Generic_Profile
8558 if not In_Instance then
8561 elsif Ekind (E) = E_Function
8562 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8567 F1 := First_Formal (Designator);
8568 F2 := First_Formal (E);
8569 while Present (F1) loop
8570 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8579 end Different_Generic_Profile;
8581 -- Start of processing for Find_Corresponding_Spec
8584 E := Current_Entity (Designator);
8585 while Present (E) loop
8587 -- We are looking for a matching spec. It must have the same scope,
8588 -- and the same name, and either be type conformant, or be the case
8589 -- of a library procedure spec and its body (which belong to one
8590 -- another regardless of whether they are type conformant or not).
8592 if Scope (E) = Current_Scope then
8593 if Current_Scope = Standard_Standard
8594 or else (Ekind (E) = Ekind (Designator)
8595 and then Type_Conformant (E, Designator))
8597 -- Within an instantiation, we know that spec and body are
8598 -- subtype conformant, because they were subtype conformant in
8599 -- the generic. We choose the subtype-conformant entity here as
8600 -- well, to resolve spurious ambiguities in the instance that
8601 -- were not present in the generic (i.e. when two different
8602 -- types are given the same actual). If we are looking for a
8603 -- spec to match a body, full conformance is expected.
8607 -- Inherit the convention and "ghostness" of the matching
8608 -- spec to ensure proper full and subtype conformance.
8610 Set_Convention (Designator, Convention (E));
8612 -- Skip past subprogram bodies and subprogram renamings that
8613 -- may appear to have a matching spec, but that aren't fully
8614 -- conformant with it. That can occur in cases where an
8615 -- actual type causes unrelated homographs in the instance.
8617 if Nkind_In (N, N_Subprogram_Body,
8618 N_Subprogram_Renaming_Declaration)
8619 and then Present (Homonym (E))
8620 and then not Fully_Conformant (Designator, E)
8624 elsif not Subtype_Conformant (Designator, E) then
8627 elsif Different_Generic_Profile (E) then
8632 -- Ada 2012 (AI05-0165): For internally generated bodies of
8633 -- null procedures locate the internally generated spec. We
8634 -- enforce mode conformance since a tagged type may inherit
8635 -- from interfaces several null primitives which differ only
8636 -- in the mode of the formals.
8638 if not (Comes_From_Source (E))
8639 and then Is_Null_Procedure (E)
8640 and then not Mode_Conformant (Designator, E)
8644 -- For null procedures coming from source that are completions,
8645 -- analysis of the generated body will establish the link.
8647 elsif Comes_From_Source (E)
8648 and then Nkind (Spec) = N_Procedure_Specification
8649 and then Null_Present (Spec)
8653 -- Expression functions can be completions, but cannot be
8654 -- completed by an explicit body.
8656 elsif Comes_From_Source (E)
8657 and then Comes_From_Source (N)
8658 and then Nkind (N) = N_Subprogram_Body
8659 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8660 N_Expression_Function
8662 Error_Msg_Sloc := Sloc (E);
8663 Error_Msg_N ("body conflicts with expression function#", N);
8666 elsif not Has_Completion (E) then
8667 if Nkind (N) /= N_Subprogram_Body_Stub then
8668 Set_Corresponding_Spec (N, E);
8671 Set_Has_Completion (E);
8674 elsif Nkind (Parent (N)) = N_Subunit then
8676 -- If this is the proper body of a subunit, the completion
8677 -- flag is set when analyzing the stub.
8681 -- If E is an internal function with a controlling result that
8682 -- was created for an operation inherited by a null extension,
8683 -- it may be overridden by a body without a previous spec (one
8684 -- more reason why these should be shunned). In that case we
8685 -- remove the generated body if present, because the current
8686 -- one is the explicit overriding.
8688 elsif Ekind (E) = E_Function
8689 and then Ada_Version >= Ada_2005
8690 and then not Comes_From_Source (E)
8691 and then Has_Controlling_Result (E)
8692 and then Is_Null_Extension (Etype (E))
8693 and then Comes_From_Source (Spec)
8695 Set_Has_Completion (E, False);
8698 and then Nkind (Parent (E)) = N_Function_Specification
8701 (Unit_Declaration_Node
8702 (Corresponding_Body (Unit_Declaration_Node (E))));
8706 -- If expansion is disabled, or if the wrapper function has
8707 -- not been generated yet, this a late body overriding an
8708 -- inherited operation, or it is an overriding by some other
8709 -- declaration before the controlling result is frozen. In
8710 -- either case this is a declaration of a new entity.
8716 -- If the body already exists, then this is an error unless
8717 -- the previous declaration is the implicit declaration of a
8718 -- derived subprogram. It is also legal for an instance to
8719 -- contain type conformant overloadable declarations (but the
8720 -- generic declaration may not), per 8.3(26/2).
8722 elsif No (Alias (E))
8723 and then not Is_Intrinsic_Subprogram (E)
8724 and then not In_Instance
8727 Error_Msg_Sloc := Sloc (E);
8729 if Is_Imported (E) then
8731 ("body not allowed for imported subprogram & declared#",
8734 Error_Msg_NE ("duplicate body for & declared#", N, E);
8738 -- Child units cannot be overloaded, so a conformance mismatch
8739 -- between body and a previous spec is an error.
8741 elsif Is_Child_Unit (E)
8743 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8745 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8750 ("body of child unit does not match previous declaration", N);
8758 -- On exit, we know that no previous declaration of subprogram exists
8761 end Find_Corresponding_Spec;
8763 ----------------------
8764 -- Fully_Conformant --
8765 ----------------------
8767 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8770 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8772 end Fully_Conformant;
8774 ----------------------------------
8775 -- Fully_Conformant_Expressions --
8776 ----------------------------------
8778 function Fully_Conformant_Expressions
8779 (Given_E1 : Node_Id;
8780 Given_E2 : Node_Id) return Boolean
8782 E1 : constant Node_Id := Original_Node (Given_E1);
8783 E2 : constant Node_Id := Original_Node (Given_E2);
8784 -- We always test conformance on original nodes, since it is possible
8785 -- for analysis and/or expansion to make things look as though they
8786 -- conform when they do not, e.g. by converting 1+2 into 3.
8788 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8789 renames Fully_Conformant_Expressions;
8791 function FCL (L1, L2 : List_Id) return Boolean;
8792 -- Compare elements of two lists for conformance. Elements have to be
8793 -- conformant, and actuals inserted as default parameters do not match
8794 -- explicit actuals with the same value.
8796 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8797 -- Compare an operator node with a function call
8803 function FCL (L1, L2 : List_Id) return Boolean is
8807 if L1 = No_List then
8813 if L2 = No_List then
8819 -- Compare two lists, skipping rewrite insertions (we want to compare
8820 -- the original trees, not the expanded versions).
8823 if Is_Rewrite_Insertion (N1) then
8825 elsif Is_Rewrite_Insertion (N2) then
8831 elsif not FCE (N1, N2) then
8844 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8845 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8850 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8855 Act := First (Actuals);
8857 if Nkind (Op_Node) in N_Binary_Op then
8858 if not FCE (Left_Opnd (Op_Node), Act) then
8865 return Present (Act)
8866 and then FCE (Right_Opnd (Op_Node), Act)
8867 and then No (Next (Act));
8871 -- Start of processing for Fully_Conformant_Expressions
8874 -- Nonconformant if paren count does not match. Note: if some idiot
8875 -- complains that we don't do this right for more than 3 levels of
8876 -- parentheses, they will be treated with the respect they deserve.
8878 if Paren_Count (E1) /= Paren_Count (E2) then
8881 -- If same entities are referenced, then they are conformant even if
8882 -- they have different forms (RM 8.3.1(19-20)).
8884 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8885 if Present (Entity (E1)) then
8886 return Entity (E1) = Entity (E2)
8888 -- One may be a discriminant that has been replaced by the
8889 -- corresponding discriminal.
8892 (Chars (Entity (E1)) = Chars (Entity (E2))
8893 and then Ekind (Entity (E1)) = E_Discriminant
8894 and then Ekind (Entity (E2)) = E_In_Parameter)
8896 -- The discriminant of a protected type is transformed into
8897 -- a local constant and then into a parameter of a protected
8901 (Ekind (Entity (E1)) = E_Constant
8902 and then Ekind (Entity (E2)) = E_In_Parameter
8903 and then Present (Discriminal_Link (Entity (E1)))
8904 and then Discriminal_Link (Entity (E1)) =
8905 Discriminal_Link (Entity (E2)))
8907 -- AI12-050: The loop variables of quantified expressions
8908 -- match if they have the same identifier, even though they
8909 -- are different entities.
8912 (Chars (Entity (E1)) = Chars (Entity (E2))
8913 and then Ekind (Entity (E1)) = E_Loop_Parameter
8914 and then Ekind (Entity (E2)) = E_Loop_Parameter);
8916 elsif Nkind (E1) = N_Expanded_Name
8917 and then Nkind (E2) = N_Expanded_Name
8918 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8919 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8921 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8924 -- Identifiers in component associations don't always have
8925 -- entities, but their names must conform.
8927 return Nkind (E1) = N_Identifier
8928 and then Nkind (E2) = N_Identifier
8929 and then Chars (E1) = Chars (E2);
8932 elsif Nkind (E1) = N_Character_Literal
8933 and then Nkind (E2) = N_Expanded_Name
8935 return Nkind (Selector_Name (E2)) = N_Character_Literal
8936 and then Chars (E1) = Chars (Selector_Name (E2));
8938 elsif Nkind (E2) = N_Character_Literal
8939 and then Nkind (E1) = N_Expanded_Name
8941 return Nkind (Selector_Name (E1)) = N_Character_Literal
8942 and then Chars (E2) = Chars (Selector_Name (E1));
8944 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8945 return FCO (E1, E2);
8947 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8948 return FCO (E2, E1);
8950 -- Otherwise we must have the same syntactic entity
8952 elsif Nkind (E1) /= Nkind (E2) then
8955 -- At this point, we specialize by node type
8961 FCL (Expressions (E1), Expressions (E2))
8963 FCL (Component_Associations (E1),
8964 Component_Associations (E2));
8967 if Nkind (Expression (E1)) = N_Qualified_Expression
8969 Nkind (Expression (E2)) = N_Qualified_Expression
8971 return FCE (Expression (E1), Expression (E2));
8973 -- Check that the subtype marks and any constraints
8978 Indic1 : constant Node_Id := Expression (E1);
8979 Indic2 : constant Node_Id := Expression (E2);
8984 if Nkind (Indic1) /= N_Subtype_Indication then
8986 Nkind (Indic2) /= N_Subtype_Indication
8987 and then Entity (Indic1) = Entity (Indic2);
8989 elsif Nkind (Indic2) /= N_Subtype_Indication then
8991 Nkind (Indic1) /= N_Subtype_Indication
8992 and then Entity (Indic1) = Entity (Indic2);
8995 if Entity (Subtype_Mark (Indic1)) /=
8996 Entity (Subtype_Mark (Indic2))
9001 Elt1 := First (Constraints (Constraint (Indic1)));
9002 Elt2 := First (Constraints (Constraint (Indic2)));
9003 while Present (Elt1) and then Present (Elt2) loop
9004 if not FCE (Elt1, Elt2) then
9017 when N_Attribute_Reference =>
9019 Attribute_Name (E1) = Attribute_Name (E2)
9020 and then FCL (Expressions (E1), Expressions (E2));
9024 Entity (E1) = Entity (E2)
9025 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9026 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9028 when N_Membership_Test
9032 FCE (Left_Opnd (E1), Left_Opnd (E2))
9034 FCE (Right_Opnd (E1), Right_Opnd (E2));
9036 when N_Case_Expression =>
9042 if not FCE (Expression (E1), Expression (E2)) then
9046 Alt1 := First (Alternatives (E1));
9047 Alt2 := First (Alternatives (E2));
9049 if Present (Alt1) /= Present (Alt2) then
9051 elsif No (Alt1) then
9055 if not FCE (Expression (Alt1), Expression (Alt2))
9056 or else not FCL (Discrete_Choices (Alt1),
9057 Discrete_Choices (Alt2))
9068 when N_Character_Literal =>
9070 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9072 when N_Component_Association =>
9074 FCL (Choices (E1), Choices (E2))
9076 FCE (Expression (E1), Expression (E2));
9078 when N_Explicit_Dereference =>
9080 FCE (Prefix (E1), Prefix (E2));
9082 when N_Extension_Aggregate =>
9084 FCL (Expressions (E1), Expressions (E2))
9085 and then Null_Record_Present (E1) =
9086 Null_Record_Present (E2)
9087 and then FCL (Component_Associations (E1),
9088 Component_Associations (E2));
9090 when N_Function_Call =>
9092 FCE (Name (E1), Name (E2))
9094 FCL (Parameter_Associations (E1),
9095 Parameter_Associations (E2));
9097 when N_If_Expression =>
9099 FCL (Expressions (E1), Expressions (E2));
9101 when N_Indexed_Component =>
9103 FCE (Prefix (E1), Prefix (E2))
9105 FCL (Expressions (E1), Expressions (E2));
9107 when N_Integer_Literal =>
9108 return (Intval (E1) = Intval (E2));
9113 when N_Operator_Symbol =>
9115 Chars (E1) = Chars (E2);
9117 when N_Others_Choice =>
9120 when N_Parameter_Association =>
9122 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9123 and then FCE (Explicit_Actual_Parameter (E1),
9124 Explicit_Actual_Parameter (E2));
9126 when N_Qualified_Expression
9128 | N_Unchecked_Type_Conversion
9131 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9133 FCE (Expression (E1), Expression (E2));
9135 when N_Quantified_Expression =>
9136 if not FCE (Condition (E1), Condition (E2)) then
9140 if Present (Loop_Parameter_Specification (E1))
9141 and then Present (Loop_Parameter_Specification (E2))
9144 L1 : constant Node_Id :=
9145 Loop_Parameter_Specification (E1);
9146 L2 : constant Node_Id :=
9147 Loop_Parameter_Specification (E2);
9151 Reverse_Present (L1) = Reverse_Present (L2)
9153 FCE (Defining_Identifier (L1),
9154 Defining_Identifier (L2))
9156 FCE (Discrete_Subtype_Definition (L1),
9157 Discrete_Subtype_Definition (L2));
9160 elsif Present (Iterator_Specification (E1))
9161 and then Present (Iterator_Specification (E2))
9164 I1 : constant Node_Id := Iterator_Specification (E1);
9165 I2 : constant Node_Id := Iterator_Specification (E2);
9169 FCE (Defining_Identifier (I1),
9170 Defining_Identifier (I2))
9172 Of_Present (I1) = Of_Present (I2)
9174 Reverse_Present (I1) = Reverse_Present (I2)
9175 and then FCE (Name (I1), Name (I2))
9176 and then FCE (Subtype_Indication (I1),
9177 Subtype_Indication (I2));
9180 -- The quantified expressions used different specifications to
9181 -- walk their respective ranges.
9189 FCE (Low_Bound (E1), Low_Bound (E2))
9191 FCE (High_Bound (E1), High_Bound (E2));
9193 when N_Real_Literal =>
9194 return (Realval (E1) = Realval (E2));
9196 when N_Selected_Component =>
9198 FCE (Prefix (E1), Prefix (E2))
9200 FCE (Selector_Name (E1), Selector_Name (E2));
9204 FCE (Prefix (E1), Prefix (E2))
9206 FCE (Discrete_Range (E1), Discrete_Range (E2));
9208 when N_String_Literal =>
9210 S1 : constant String_Id := Strval (E1);
9211 S2 : constant String_Id := Strval (E2);
9212 L1 : constant Nat := String_Length (S1);
9213 L2 : constant Nat := String_Length (S2);
9220 for J in 1 .. L1 loop
9221 if Get_String_Char (S1, J) /=
9222 Get_String_Char (S2, J)
9234 Entity (E1) = Entity (E2)
9236 FCE (Right_Opnd (E1), Right_Opnd (E2));
9238 -- All other node types cannot appear in this context. Strictly
9239 -- we should raise a fatal internal error. Instead we just ignore
9240 -- the nodes. This means that if anyone makes a mistake in the
9241 -- expander and mucks an expression tree irretrievably, the result
9242 -- will be a failure to detect a (probably very obscure) case
9243 -- of non-conformance, which is better than bombing on some
9244 -- case where two expressions do in fact conform.
9250 end Fully_Conformant_Expressions;
9252 ----------------------------------------
9253 -- Fully_Conformant_Discrete_Subtypes --
9254 ----------------------------------------
9256 function Fully_Conformant_Discrete_Subtypes
9257 (Given_S1 : Node_Id;
9258 Given_S2 : Node_Id) return Boolean
9260 S1 : constant Node_Id := Original_Node (Given_S1);
9261 S2 : constant Node_Id := Original_Node (Given_S2);
9263 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9264 -- Special-case for a bound given by a discriminant, which in the body
9265 -- is replaced with the discriminal of the enclosing type.
9267 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9268 -- Check both bounds
9270 -----------------------
9271 -- Conforming_Bounds --
9272 -----------------------
9274 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9276 if Is_Entity_Name (B1)
9277 and then Is_Entity_Name (B2)
9278 and then Ekind (Entity (B1)) = E_Discriminant
9280 return Chars (B1) = Chars (B2);
9283 return Fully_Conformant_Expressions (B1, B2);
9285 end Conforming_Bounds;
9287 -----------------------
9288 -- Conforming_Ranges --
9289 -----------------------
9291 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9294 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9296 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9297 end Conforming_Ranges;
9299 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9302 if Nkind (S1) /= Nkind (S2) then
9305 elsif Is_Entity_Name (S1) then
9306 return Entity (S1) = Entity (S2);
9308 elsif Nkind (S1) = N_Range then
9309 return Conforming_Ranges (S1, S2);
9311 elsif Nkind (S1) = N_Subtype_Indication then
9313 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9316 (Range_Expression (Constraint (S1)),
9317 Range_Expression (Constraint (S2)));
9321 end Fully_Conformant_Discrete_Subtypes;
9323 --------------------
9324 -- Install_Entity --
9325 --------------------
9327 procedure Install_Entity (E : Entity_Id) is
9328 Prev : constant Entity_Id := Current_Entity (E);
9330 Set_Is_Immediately_Visible (E);
9331 Set_Current_Entity (E);
9332 Set_Homonym (E, Prev);
9335 ---------------------
9336 -- Install_Formals --
9337 ---------------------
9339 procedure Install_Formals (Id : Entity_Id) is
9342 F := First_Formal (Id);
9343 while Present (F) loop
9347 end Install_Formals;
9349 -----------------------------
9350 -- Is_Interface_Conformant --
9351 -----------------------------
9353 function Is_Interface_Conformant
9354 (Tagged_Type : Entity_Id;
9355 Iface_Prim : Entity_Id;
9356 Prim : Entity_Id) return Boolean
9358 -- The operation may in fact be an inherited (implicit) operation
9359 -- rather than the original interface primitive, so retrieve the
9360 -- ultimate ancestor.
9362 Iface : constant Entity_Id :=
9363 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9364 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9366 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9367 -- Return the controlling formal of Prim
9369 ------------------------
9370 -- Controlling_Formal --
9371 ------------------------
9373 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9377 E := First_Entity (Prim);
9378 while Present (E) loop
9379 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9387 end Controlling_Formal;
9391 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9392 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9394 -- Start of processing for Is_Interface_Conformant
9397 pragma Assert (Is_Subprogram (Iface_Prim)
9398 and then Is_Subprogram (Prim)
9399 and then Is_Dispatching_Operation (Iface_Prim)
9400 and then Is_Dispatching_Operation (Prim));
9402 pragma Assert (Is_Interface (Iface)
9403 or else (Present (Alias (Iface_Prim))
9406 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9408 if Prim = Iface_Prim
9409 or else not Is_Subprogram (Prim)
9410 or else Ekind (Prim) /= Ekind (Iface_Prim)
9411 or else not Is_Dispatching_Operation (Prim)
9412 or else Scope (Prim) /= Scope (Tagged_Type)
9414 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9415 or else not Primitive_Names_Match (Iface_Prim, Prim)
9419 -- The mode of the controlling formals must match
9421 elsif Present (Iface_Ctrl_F)
9422 and then Present (Prim_Ctrl_F)
9423 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9427 -- Case of a procedure, or a function whose result type matches the
9428 -- result type of the interface primitive, or a function that has no
9429 -- controlling result (I or access I).
9431 elsif Ekind (Iface_Prim) = E_Procedure
9432 or else Etype (Prim) = Etype (Iface_Prim)
9433 or else not Has_Controlling_Result (Prim)
9435 return Type_Conformant
9436 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9438 -- Case of a function returning an interface, or an access to one. Check
9439 -- that the return types correspond.
9441 elsif Implements_Interface (Typ, Iface) then
9442 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9444 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9449 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9450 Skip_Controlling_Formals => True);
9456 end Is_Interface_Conformant;
9458 ---------------------------------
9459 -- Is_Non_Overriding_Operation --
9460 ---------------------------------
9462 function Is_Non_Overriding_Operation
9463 (Prev_E : Entity_Id;
9464 New_E : Entity_Id) return Boolean
9468 G_Typ : Entity_Id := Empty;
9470 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9471 -- If F_Type is a derived type associated with a generic actual subtype,
9472 -- then return its Generic_Parent_Type attribute, else return Empty.
9474 function Types_Correspond
9475 (P_Type : Entity_Id;
9476 N_Type : Entity_Id) return Boolean;
9477 -- Returns true if and only if the types (or designated types in the
9478 -- case of anonymous access types) are the same or N_Type is derived
9479 -- directly or indirectly from P_Type.
9481 -----------------------------
9482 -- Get_Generic_Parent_Type --
9483 -----------------------------
9485 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9491 if Is_Derived_Type (F_Typ)
9492 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9494 -- The tree must be traversed to determine the parent subtype in
9495 -- the generic unit, which unfortunately isn't always available
9496 -- via semantic attributes. ??? (Note: The use of Original_Node
9497 -- is needed for cases where a full derived type has been
9500 -- If the parent type is a scalar type, the derivation creates
9501 -- an anonymous base type for it, and the source type is its
9504 if Is_Scalar_Type (F_Typ)
9505 and then not Comes_From_Source (F_Typ)
9509 (Original_Node (Parent (First_Subtype (F_Typ))));
9511 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9513 if Nkind (Defn) = N_Derived_Type_Definition then
9514 Indic := Subtype_Indication (Defn);
9516 if Nkind (Indic) = N_Subtype_Indication then
9517 G_Typ := Entity (Subtype_Mark (Indic));
9519 G_Typ := Entity (Indic);
9522 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9523 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9525 return Generic_Parent_Type (Parent (G_Typ));
9531 end Get_Generic_Parent_Type;
9533 ----------------------
9534 -- Types_Correspond --
9535 ----------------------
9537 function Types_Correspond
9538 (P_Type : Entity_Id;
9539 N_Type : Entity_Id) return Boolean
9541 Prev_Type : Entity_Id := Base_Type (P_Type);
9542 New_Type : Entity_Id := Base_Type (N_Type);
9545 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9546 Prev_Type := Designated_Type (Prev_Type);
9549 if Ekind (New_Type) = E_Anonymous_Access_Type then
9550 New_Type := Designated_Type (New_Type);
9553 if Prev_Type = New_Type then
9556 elsif not Is_Class_Wide_Type (New_Type) then
9557 while Etype (New_Type) /= New_Type loop
9558 New_Type := Etype (New_Type);
9560 if New_Type = Prev_Type then
9566 end Types_Correspond;
9568 -- Start of processing for Is_Non_Overriding_Operation
9571 -- In the case where both operations are implicit derived subprograms
9572 -- then neither overrides the other. This can only occur in certain
9573 -- obscure cases (e.g., derivation from homographs created in a generic
9576 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9579 elsif Ekind (Current_Scope) = E_Package
9580 and then Is_Generic_Instance (Current_Scope)
9581 and then In_Private_Part (Current_Scope)
9582 and then Comes_From_Source (New_E)
9584 -- We examine the formals and result type of the inherited operation,
9585 -- to determine whether their type is derived from (the instance of)
9586 -- a generic type. The first such formal or result type is the one
9589 Formal := First_Formal (Prev_E);
9591 while Present (Formal) loop
9592 F_Typ := Base_Type (Etype (Formal));
9594 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9595 F_Typ := Designated_Type (F_Typ);
9598 G_Typ := Get_Generic_Parent_Type (F_Typ);
9599 exit when Present (G_Typ);
9601 Next_Formal (Formal);
9604 -- If the function dispatches on result check the result type
9606 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9607 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9614 -- If the generic type is a private type, then the original operation
9615 -- was not overriding in the generic, because there was no primitive
9616 -- operation to override.
9618 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9619 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9620 N_Formal_Private_Type_Definition
9624 -- The generic parent type is the ancestor of a formal derived
9625 -- type declaration. We need to check whether it has a primitive
9626 -- operation that should be overridden by New_E in the generic.
9630 P_Formal : Entity_Id;
9631 N_Formal : Entity_Id;
9635 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9638 while Present (Prim_Elt) loop
9639 P_Prim := Node (Prim_Elt);
9641 if Chars (P_Prim) = Chars (New_E)
9642 and then Ekind (P_Prim) = Ekind (New_E)
9644 P_Formal := First_Formal (P_Prim);
9645 N_Formal := First_Formal (New_E);
9646 while Present (P_Formal) and then Present (N_Formal) loop
9647 P_Typ := Etype (P_Formal);
9648 N_Typ := Etype (N_Formal);
9650 if not Types_Correspond (P_Typ, N_Typ) then
9654 Next_Entity (P_Formal);
9655 Next_Entity (N_Formal);
9658 -- Found a matching primitive operation belonging to the
9659 -- formal ancestor type, so the new subprogram is
9663 and then No (N_Formal)
9664 and then (Ekind (New_E) /= E_Function
9667 (Etype (P_Prim), Etype (New_E)))
9673 Next_Elmt (Prim_Elt);
9676 -- If no match found, then the new subprogram does not override
9677 -- in the generic (nor in the instance).
9679 -- If the type in question is not abstract, and the subprogram
9680 -- is, this will be an error if the new operation is in the
9681 -- private part of the instance. Emit a warning now, which will
9682 -- make the subsequent error message easier to understand.
9684 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
9685 and then Is_Abstract_Subprogram (Prev_E)
9686 and then In_Private_Part (Current_Scope)
9688 Error_Msg_Node_2 := F_Typ;
9690 ("private operation& in generic unit does not override "
9691 & "any primitive operation of& (RM 12.3 (18))??",
9701 end Is_Non_Overriding_Operation;
9703 -------------------------------------
9704 -- List_Inherited_Pre_Post_Aspects --
9705 -------------------------------------
9707 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9709 if Opt.List_Inherited_Aspects
9710 and then Is_Subprogram_Or_Generic_Subprogram (E)
9713 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9718 for Index in Subps'Range loop
9719 Items := Contract (Subps (Index));
9721 if Present (Items) then
9722 Prag := Pre_Post_Conditions (Items);
9723 while Present (Prag) loop
9724 Error_Msg_Sloc := Sloc (Prag);
9726 if Class_Present (Prag)
9727 and then not Split_PPC (Prag)
9729 if Pragma_Name (Prag) = Name_Precondition then
9731 ("info: & inherits `Pre''Class` aspect from "
9735 ("info: & inherits `Post''Class` aspect from "
9740 Prag := Next_Pragma (Prag);
9746 end List_Inherited_Pre_Post_Aspects;
9748 ------------------------------
9749 -- Make_Inequality_Operator --
9750 ------------------------------
9752 -- S is the defining identifier of an equality operator. We build a
9753 -- subprogram declaration with the right signature. This operation is
9754 -- intrinsic, because it is always expanded as the negation of the
9755 -- call to the equality function.
9757 procedure Make_Inequality_Operator (S : Entity_Id) is
9758 Loc : constant Source_Ptr := Sloc (S);
9761 Op_Name : Entity_Id;
9763 FF : constant Entity_Id := First_Formal (S);
9764 NF : constant Entity_Id := Next_Formal (FF);
9767 -- Check that equality was properly defined, ignore call if not
9774 A : constant Entity_Id :=
9775 Make_Defining_Identifier (Sloc (FF),
9776 Chars => Chars (FF));
9778 B : constant Entity_Id :=
9779 Make_Defining_Identifier (Sloc (NF),
9780 Chars => Chars (NF));
9783 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9785 Formals := New_List (
9786 Make_Parameter_Specification (Loc,
9787 Defining_Identifier => A,
9789 New_Occurrence_Of (Etype (First_Formal (S)),
9790 Sloc (Etype (First_Formal (S))))),
9792 Make_Parameter_Specification (Loc,
9793 Defining_Identifier => B,
9795 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9796 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9799 Make_Subprogram_Declaration (Loc,
9801 Make_Function_Specification (Loc,
9802 Defining_Unit_Name => Op_Name,
9803 Parameter_Specifications => Formals,
9804 Result_Definition =>
9805 New_Occurrence_Of (Standard_Boolean, Loc)));
9807 -- Insert inequality right after equality if it is explicit or after
9808 -- the derived type when implicit. These entities are created only
9809 -- for visibility purposes, and eventually replaced in the course
9810 -- of expansion, so they do not need to be attached to the tree and
9811 -- seen by the back-end. Keeping them internal also avoids spurious
9812 -- freezing problems. The declaration is inserted in the tree for
9813 -- analysis, and removed afterwards. If the equality operator comes
9814 -- from an explicit declaration, attach the inequality immediately
9815 -- after. Else the equality is inherited from a derived type
9816 -- declaration, so insert inequality after that declaration.
9818 if No (Alias (S)) then
9819 Insert_After (Unit_Declaration_Node (S), Decl);
9820 elsif Is_List_Member (Parent (S)) then
9821 Insert_After (Parent (S), Decl);
9823 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9826 Mark_Rewrite_Insertion (Decl);
9827 Set_Is_Intrinsic_Subprogram (Op_Name);
9830 Set_Has_Completion (Op_Name);
9831 Set_Corresponding_Equality (Op_Name, S);
9832 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9834 end Make_Inequality_Operator;
9836 ----------------------
9837 -- May_Need_Actuals --
9838 ----------------------
9840 procedure May_Need_Actuals (Fun : Entity_Id) is
9845 F := First_Formal (Fun);
9847 while Present (F) loop
9848 if No (Default_Value (F)) then
9856 Set_Needs_No_Actuals (Fun, B);
9857 end May_Need_Actuals;
9859 ---------------------
9860 -- Mode_Conformant --
9861 ---------------------
9863 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9866 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9868 end Mode_Conformant;
9870 ---------------------------
9871 -- New_Overloaded_Entity --
9872 ---------------------------
9874 procedure New_Overloaded_Entity
9876 Derived_Type : Entity_Id := Empty)
9878 Overridden_Subp : Entity_Id := Empty;
9879 -- Set if the current scope has an operation that is type-conformant
9880 -- with S, and becomes hidden by S.
9882 Is_Primitive_Subp : Boolean;
9883 -- Set to True if the new subprogram is primitive
9886 -- Entity that S overrides
9888 Prev_Vis : Entity_Id := Empty;
9889 -- Predecessor of E in Homonym chain
9891 procedure Check_For_Primitive_Subprogram
9892 (Is_Primitive : out Boolean;
9893 Is_Overriding : Boolean := False);
9894 -- If the subprogram being analyzed is a primitive operation of the type
9895 -- of a formal or result, set the Has_Primitive_Operations flag on the
9896 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9897 -- corresponding flag on the entity itself for later use.
9899 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
9900 -- True if a) E is a subprogram whose first formal is a concurrent type
9901 -- defined in the scope of E that has some entry or subprogram whose
9902 -- profile matches E, or b) E is an internally built dispatching
9903 -- subprogram of a protected type and there is a matching subprogram
9904 -- defined in the enclosing scope of the protected type, or c) E is
9905 -- an entry of a synchronized type and a matching procedure has been
9906 -- previously defined in the enclosing scope of the synchronized type.
9908 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9909 -- Check that E is declared in the private part of the current package,
9910 -- or in the package body, where it may hide a previous declaration.
9911 -- We can't use In_Private_Part by itself because this flag is also
9912 -- set when freezing entities, so we must examine the place of the
9913 -- declaration in the tree, and recognize wrapper packages as well.
9915 function Is_Overriding_Alias
9917 New_E : Entity_Id) return Boolean;
9918 -- Check whether new subprogram and old subprogram are both inherited
9919 -- from subprograms that have distinct dispatch table entries. This can
9920 -- occur with derivations from instances with accidental homonyms. The
9921 -- function is conservative given that the converse is only true within
9922 -- instances that contain accidental overloadings.
9924 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
9925 -- Report conflict between entities S and E
9927 ------------------------------------
9928 -- Check_For_Primitive_Subprogram --
9929 ------------------------------------
9931 procedure Check_For_Primitive_Subprogram
9932 (Is_Primitive : out Boolean;
9933 Is_Overriding : Boolean := False)
9939 function Visible_Part_Type (T : Entity_Id) return Boolean;
9940 -- Returns true if T is declared in the visible part of the current
9941 -- package scope; otherwise returns false. Assumes that T is declared
9944 procedure Check_Private_Overriding (T : Entity_Id);
9945 -- Checks that if a primitive abstract subprogram of a visible
9946 -- abstract type is declared in a private part, then it must override
9947 -- an abstract subprogram declared in the visible part. Also checks
9948 -- that if a primitive function with a controlling result is declared
9949 -- in a private part, then it must override a function declared in
9950 -- the visible part.
9952 ------------------------------
9953 -- Check_Private_Overriding --
9954 ------------------------------
9956 procedure Check_Private_Overriding (T : Entity_Id) is
9957 function Overrides_Private_Part_Op return Boolean;
9958 -- This detects the special case where the overriding subprogram
9959 -- is overriding a subprogram that was declared in the same
9960 -- private part. That case is illegal by 3.9.3(10).
9962 function Overrides_Visible_Function
9963 (Partial_View : Entity_Id) return Boolean;
9964 -- True if S overrides a function in the visible part. The
9965 -- overridden function could be explicitly or implicitly declared.
9967 -------------------------------
9968 -- Overrides_Private_Part_Op --
9969 -------------------------------
9971 function Overrides_Private_Part_Op return Boolean is
9972 Over_Decl : constant Node_Id :=
9973 Unit_Declaration_Node (Overridden_Operation (S));
9974 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
9977 pragma Assert (Is_Overriding);
9979 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
9981 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
9983 return In_Same_List (Over_Decl, Subp_Decl);
9984 end Overrides_Private_Part_Op;
9986 --------------------------------
9987 -- Overrides_Visible_Function --
9988 --------------------------------
9990 function Overrides_Visible_Function
9991 (Partial_View : Entity_Id) return Boolean
9994 if not Is_Overriding or else not Has_Homonym (S) then
9998 if not Present (Partial_View) then
10002 -- Search through all the homonyms H of S in the current
10003 -- package spec, and return True if we find one that matches.
10004 -- Note that Parent (H) will be the declaration of the
10005 -- partial view of T for a match.
10008 H : Entity_Id := S;
10012 exit when not Present (H) or else Scope (H) /= Scope (S);
10016 N_Private_Extension_Declaration,
10017 N_Private_Type_Declaration)
10018 and then Defining_Identifier (Parent (H)) = Partial_View
10026 end Overrides_Visible_Function;
10028 -- Start of processing for Check_Private_Overriding
10031 if Is_Package_Or_Generic_Package (Current_Scope)
10032 and then In_Private_Part (Current_Scope)
10033 and then Visible_Part_Type (T)
10034 and then not In_Instance
10036 if Is_Abstract_Type (T)
10037 and then Is_Abstract_Subprogram (S)
10038 and then (not Is_Overriding
10039 or else not Is_Abstract_Subprogram (E)
10040 or else Overrides_Private_Part_Op)
10043 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10046 elsif Ekind (S) = E_Function then
10048 Partial_View : constant Entity_Id :=
10049 Incomplete_Or_Partial_View (T);
10052 if not Overrides_Visible_Function (Partial_View) then
10054 -- Here, S is "function ... return T;" declared in
10055 -- the private part, not overriding some visible
10056 -- operation. That's illegal in the tagged case
10057 -- (but not if the private type is untagged).
10059 if ((Present (Partial_View)
10060 and then Is_Tagged_Type (Partial_View))
10061 or else (not Present (Partial_View)
10062 and then Is_Tagged_Type (T)))
10063 and then T = Base_Type (Etype (S))
10066 ("private function with tagged result must"
10067 & " override visible-part function", S);
10069 ("\move subprogram to the visible part"
10070 & " (RM 3.9.3(10))", S);
10072 -- AI05-0073: extend this test to the case of a
10073 -- function with a controlling access result.
10075 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10076 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10078 not Is_Class_Wide_Type
10079 (Designated_Type (Etype (S)))
10080 and then Ada_Version >= Ada_2012
10083 ("private function with controlling access "
10084 & "result must override visible-part function",
10087 ("\move subprogram to the visible part"
10088 & " (RM 3.9.3(10))", S);
10094 end Check_Private_Overriding;
10096 -----------------------
10097 -- Visible_Part_Type --
10098 -----------------------
10100 function Visible_Part_Type (T : Entity_Id) return Boolean is
10101 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10105 -- If the entity is a private type, then it must be declared in a
10108 if Ekind (T) in Private_Kind then
10112 -- Otherwise, we traverse the visible part looking for its
10113 -- corresponding declaration. We cannot use the declaration
10114 -- node directly because in the private part the entity of a
10115 -- private type is the one in the full view, which does not
10116 -- indicate that it is the completion of something visible.
10118 N := First (Visible_Declarations (Specification (P)));
10119 while Present (N) loop
10120 if Nkind (N) = N_Full_Type_Declaration
10121 and then Present (Defining_Identifier (N))
10122 and then T = Defining_Identifier (N)
10126 elsif Nkind_In (N, N_Private_Type_Declaration,
10127 N_Private_Extension_Declaration)
10128 and then Present (Defining_Identifier (N))
10129 and then T = Full_View (Defining_Identifier (N))
10138 end Visible_Part_Type;
10140 -- Start of processing for Check_For_Primitive_Subprogram
10143 Is_Primitive := False;
10145 if not Comes_From_Source (S) then
10148 -- If subprogram is at library level, it is not primitive operation
10150 elsif Current_Scope = Standard_Standard then
10153 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10154 and then not In_Package_Body (Current_Scope))
10155 or else Is_Overriding
10157 -- For function, check return type
10159 if Ekind (S) = E_Function then
10160 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10161 F_Typ := Designated_Type (Etype (S));
10163 F_Typ := Etype (S);
10166 B_Typ := Base_Type (F_Typ);
10168 if Scope (B_Typ) = Current_Scope
10169 and then not Is_Class_Wide_Type (B_Typ)
10170 and then not Is_Generic_Type (B_Typ)
10172 Is_Primitive := True;
10173 Set_Has_Primitive_Operations (B_Typ);
10174 Set_Is_Primitive (S);
10175 Check_Private_Overriding (B_Typ);
10177 -- The Ghost policy in effect at the point of declaration
10178 -- or a tagged type and a primitive operation must match
10179 -- (SPARK RM 6.9(16)).
10181 Check_Ghost_Primitive (S, B_Typ);
10185 -- For all subprograms, check formals
10187 Formal := First_Formal (S);
10188 while Present (Formal) loop
10189 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10190 F_Typ := Designated_Type (Etype (Formal));
10192 F_Typ := Etype (Formal);
10195 B_Typ := Base_Type (F_Typ);
10197 if Ekind (B_Typ) = E_Access_Subtype then
10198 B_Typ := Base_Type (B_Typ);
10201 if Scope (B_Typ) = Current_Scope
10202 and then not Is_Class_Wide_Type (B_Typ)
10203 and then not Is_Generic_Type (B_Typ)
10205 Is_Primitive := True;
10206 Set_Is_Primitive (S);
10207 Set_Has_Primitive_Operations (B_Typ);
10208 Check_Private_Overriding (B_Typ);
10210 -- The Ghost policy in effect at the point of declaration
10211 -- of a tagged type and a primitive operation must match
10212 -- (SPARK RM 6.9(16)).
10214 Check_Ghost_Primitive (S, B_Typ);
10217 Next_Formal (Formal);
10220 -- Special case: An equality function can be redefined for a type
10221 -- occurring in a declarative part, and won't otherwise be treated as
10222 -- a primitive because it doesn't occur in a package spec and doesn't
10223 -- override an inherited subprogram. It's important that we mark it
10224 -- primitive so it can be returned by Collect_Primitive_Operations
10225 -- and be used in composing the equality operation of later types
10226 -- that have a component of the type.
10228 elsif Chars (S) = Name_Op_Eq
10229 and then Etype (S) = Standard_Boolean
10231 B_Typ := Base_Type (Etype (First_Formal (S)));
10233 if Scope (B_Typ) = Current_Scope
10235 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10236 and then not Is_Limited_Type (B_Typ)
10238 Is_Primitive := True;
10239 Set_Is_Primitive (S);
10240 Set_Has_Primitive_Operations (B_Typ);
10241 Check_Private_Overriding (B_Typ);
10243 -- The Ghost policy in effect at the point of declaration of a
10244 -- tagged type and a primitive operation must match
10245 -- (SPARK RM 6.9(16)).
10247 Check_Ghost_Primitive (S, B_Typ);
10250 end Check_For_Primitive_Subprogram;
10252 --------------------------------------
10253 -- Has_Matching_Entry_Or_Subprogram --
10254 --------------------------------------
10256 function Has_Matching_Entry_Or_Subprogram
10257 (E : Entity_Id) return Boolean
10259 function Check_Conforming_Parameters
10260 (E1_Param : Node_Id;
10261 E2_Param : Node_Id) return Boolean;
10262 -- Starting from the given parameters, check that all the parameters
10263 -- of two entries or subprograms are subtype conformant. Used to skip
10264 -- the check on the controlling argument.
10266 function Matching_Entry_Or_Subprogram
10267 (Conc_Typ : Entity_Id;
10268 Subp : Entity_Id) return Entity_Id;
10269 -- Return the first entry or subprogram of the given concurrent type
10270 -- whose name matches the name of Subp and has a profile conformant
10271 -- with Subp; return Empty if not found.
10273 function Matching_Dispatching_Subprogram
10274 (Conc_Typ : Entity_Id;
10275 Ent : Entity_Id) return Entity_Id;
10276 -- Return the first dispatching primitive of Conc_Type defined in the
10277 -- enclosing scope of Conc_Type (i.e. before the full definition of
10278 -- this concurrent type) whose name matches the entry Ent and has a
10279 -- profile conformant with the profile of the corresponding (not yet
10280 -- built) dispatching primitive of Ent; return Empty if not found.
10282 function Matching_Original_Protected_Subprogram
10283 (Prot_Typ : Entity_Id;
10284 Subp : Entity_Id) return Entity_Id;
10285 -- Return the first subprogram defined in the enclosing scope of
10286 -- Prot_Typ (before the full definition of this protected type)
10287 -- whose name matches the original name of Subp and has a profile
10288 -- conformant with the profile of Subp; return Empty if not found.
10290 ---------------------------------
10291 -- Check_Confirming_Parameters --
10292 ---------------------------------
10294 function Check_Conforming_Parameters
10295 (E1_Param : Node_Id;
10296 E2_Param : Node_Id) return Boolean
10298 Param_E1 : Node_Id := E1_Param;
10299 Param_E2 : Node_Id := E2_Param;
10302 while Present (Param_E1) and then Present (Param_E2) loop
10303 if Ekind (Defining_Identifier (Param_E1)) /=
10304 Ekind (Defining_Identifier (Param_E2))
10307 (Find_Parameter_Type (Param_E1),
10308 Find_Parameter_Type (Param_E2),
10309 Subtype_Conformant)
10318 -- The candidate is not valid if one of the two lists contains
10319 -- more parameters than the other
10321 return No (Param_E1) and then No (Param_E2);
10322 end Check_Conforming_Parameters;
10324 ----------------------------------
10325 -- Matching_Entry_Or_Subprogram --
10326 ----------------------------------
10328 function Matching_Entry_Or_Subprogram
10329 (Conc_Typ : Entity_Id;
10330 Subp : Entity_Id) return Entity_Id
10335 E := First_Entity (Conc_Typ);
10336 while Present (E) loop
10337 if Chars (Subp) = Chars (E)
10338 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10340 Check_Conforming_Parameters
10341 (First (Parameter_Specifications (Parent (E))),
10342 Next (First (Parameter_Specifications (Parent (Subp)))))
10351 end Matching_Entry_Or_Subprogram;
10353 -------------------------------------
10354 -- Matching_Dispatching_Subprogram --
10355 -------------------------------------
10357 function Matching_Dispatching_Subprogram
10358 (Conc_Typ : Entity_Id;
10359 Ent : Entity_Id) return Entity_Id
10364 -- Search for entities in the enclosing scope of this synchonized
10367 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10368 Push_Scope (Scope (Conc_Typ));
10369 E := Current_Entity_In_Scope (Ent);
10372 while Present (E) loop
10373 if Scope (E) = Scope (Conc_Typ)
10374 and then Comes_From_Source (E)
10375 and then Ekind (E) = E_Procedure
10376 and then Present (First_Entity (E))
10377 and then Is_Controlling_Formal (First_Entity (E))
10378 and then Etype (First_Entity (E)) = Conc_Typ
10380 Check_Conforming_Parameters
10381 (First (Parameter_Specifications (Parent (Ent))),
10382 Next (First (Parameter_Specifications (Parent (E)))))
10391 end Matching_Dispatching_Subprogram;
10393 --------------------------------------------
10394 -- Matching_Original_Protected_Subprogram --
10395 --------------------------------------------
10397 function Matching_Original_Protected_Subprogram
10398 (Prot_Typ : Entity_Id;
10399 Subp : Entity_Id) return Entity_Id
10401 ICF : constant Boolean :=
10402 Is_Controlling_Formal (First_Entity (Subp));
10406 -- Temporarily decorate the first parameter of Subp as controlling
10407 -- formal, required to invoke Subtype_Conformant.
10409 Set_Is_Controlling_Formal (First_Entity (Subp));
10412 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10414 while Present (E) loop
10415 if Scope (E) = Scope (Prot_Typ)
10416 and then Comes_From_Source (E)
10417 and then Ekind (Subp) = Ekind (E)
10418 and then Present (First_Entity (E))
10419 and then Is_Controlling_Formal (First_Entity (E))
10420 and then Etype (First_Entity (E)) = Prot_Typ
10421 and then Subtype_Conformant (Subp, E,
10422 Skip_Controlling_Formals => True)
10424 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10431 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10434 end Matching_Original_Protected_Subprogram;
10436 -- Start of processing for Has_Matching_Entry_Or_Subprogram
10439 -- Case 1: E is a subprogram whose first formal is a concurrent type
10440 -- defined in the scope of E that has an entry or subprogram whose
10441 -- profile matches E.
10443 if Comes_From_Source (E)
10444 and then Is_Subprogram (E)
10445 and then Present (First_Entity (E))
10446 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10449 Scope (Corresponding_Concurrent_Type
10450 (Etype (First_Entity (E))))
10453 (Matching_Entry_Or_Subprogram
10454 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10457 Report_Conflict (E,
10458 Matching_Entry_Or_Subprogram
10459 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10464 -- Case 2: E is an internally built dispatching subprogram of a
10465 -- protected type and there is a subprogram defined in the enclosing
10466 -- scope of the protected type that has the original name of E and
10467 -- its profile is conformant with the profile of E. We check the
10468 -- name of the original protected subprogram associated with E since
10469 -- the expander builds dispatching primitives of protected functions
10470 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
10472 elsif not Comes_From_Source (E)
10473 and then Is_Subprogram (E)
10474 and then Present (First_Entity (E))
10475 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10476 and then Present (Original_Protected_Subprogram (E))
10479 (Matching_Original_Protected_Subprogram
10480 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10483 Report_Conflict (E,
10484 Matching_Original_Protected_Subprogram
10485 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10489 -- Case 3: E is an entry of a synchronized type and a matching
10490 -- procedure has been previously defined in the enclosing scope
10491 -- of the synchronized type.
10493 elsif Comes_From_Source (E)
10494 and then Ekind (E) = E_Entry
10496 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
10498 Report_Conflict (E,
10499 Matching_Dispatching_Subprogram (Current_Scope, E));
10504 end Has_Matching_Entry_Or_Subprogram;
10506 ----------------------------
10507 -- Is_Private_Declaration --
10508 ----------------------------
10510 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10511 Decl : constant Node_Id := Unit_Declaration_Node (E);
10512 Priv_Decls : List_Id;
10515 if Is_Package_Or_Generic_Package (Current_Scope)
10516 and then In_Private_Part (Current_Scope)
10519 Private_Declarations (Package_Specification (Current_Scope));
10521 return In_Package_Body (Current_Scope)
10523 (Is_List_Member (Decl)
10524 and then List_Containing (Decl) = Priv_Decls)
10525 or else (Nkind (Parent (Decl)) = N_Package_Specification
10527 Is_Compilation_Unit
10528 (Defining_Entity (Parent (Decl)))
10529 and then List_Containing (Parent (Parent (Decl))) =
10534 end Is_Private_Declaration;
10536 --------------------------
10537 -- Is_Overriding_Alias --
10538 --------------------------
10540 function Is_Overriding_Alias
10541 (Old_E : Entity_Id;
10542 New_E : Entity_Id) return Boolean
10544 AO : constant Entity_Id := Alias (Old_E);
10545 AN : constant Entity_Id := Alias (New_E);
10548 return Scope (AO) /= Scope (AN)
10549 or else No (DTC_Entity (AO))
10550 or else No (DTC_Entity (AN))
10551 or else DT_Position (AO) = DT_Position (AN);
10552 end Is_Overriding_Alias;
10554 ---------------------
10555 -- Report_Conflict --
10556 ---------------------
10558 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
10560 Error_Msg_Sloc := Sloc (E);
10562 -- Generate message, with useful additional warning if in generic
10564 if Is_Generic_Unit (E) then
10565 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10566 Error_Msg_N ("\& conflicts with declaration#", S);
10568 Error_Msg_N ("& conflicts with declaration#", S);
10570 end Report_Conflict;
10572 -- Start of processing for New_Overloaded_Entity
10575 -- We need to look for an entity that S may override. This must be a
10576 -- homonym in the current scope, so we look for the first homonym of
10577 -- S in the current scope as the starting point for the search.
10579 E := Current_Entity_In_Scope (S);
10581 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10582 -- They are directly added to the list of primitive operations of
10583 -- Derived_Type, unless this is a rederivation in the private part
10584 -- of an operation that was already derived in the visible part of
10585 -- the current package.
10587 if Ada_Version >= Ada_2005
10588 and then Present (Derived_Type)
10589 and then Present (Alias (S))
10590 and then Is_Dispatching_Operation (Alias (S))
10591 and then Present (Find_Dispatching_Type (Alias (S)))
10592 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10594 -- For private types, when the full-view is processed we propagate to
10595 -- the full view the non-overridden entities whose attribute "alias"
10596 -- references an interface primitive. These entities were added by
10597 -- Derive_Subprograms to ensure that interface primitives are
10600 -- Inside_Freeze_Actions is non zero when S corresponds with an
10601 -- internal entity that links an interface primitive with its
10602 -- covering primitive through attribute Interface_Alias (see
10603 -- Add_Internal_Interface_Entities).
10605 if Inside_Freezing_Actions = 0
10606 and then Is_Package_Or_Generic_Package (Current_Scope)
10607 and then In_Private_Part (Current_Scope)
10608 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10609 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10610 and then Full_View (Defining_Identifier (Parent (E)))
10611 = Defining_Identifier (Parent (S))
10612 and then Alias (E) = Alias (S)
10614 Check_Operation_From_Private_View (S, E);
10615 Set_Is_Dispatching_Operation (S);
10620 Enter_Overloaded_Entity (S);
10621 Check_Dispatching_Operation (S, Empty);
10622 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10628 -- For synchronized types check conflicts of this entity with previously
10629 -- defined entities.
10631 if Ada_Version >= Ada_2005
10632 and then Has_Matching_Entry_Or_Subprogram (S)
10637 -- If there is no homonym then this is definitely not overriding
10640 Enter_Overloaded_Entity (S);
10641 Check_Dispatching_Operation (S, Empty);
10642 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10644 -- If subprogram has an explicit declaration, check whether it has an
10645 -- overriding indicator.
10647 if Comes_From_Source (S) then
10648 Check_Synchronized_Overriding (S, Overridden_Subp);
10650 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10651 -- it may have overridden some hidden inherited primitive. Update
10652 -- Overridden_Subp to avoid spurious errors when checking the
10653 -- overriding indicator.
10655 if Ada_Version >= Ada_2012
10656 and then No (Overridden_Subp)
10657 and then Is_Dispatching_Operation (S)
10658 and then Present (Overridden_Operation (S))
10660 Overridden_Subp := Overridden_Operation (S);
10663 Check_Overriding_Indicator
10664 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10666 -- The Ghost policy in effect at the point of declaration of a
10667 -- parent subprogram and an overriding subprogram must match
10668 -- (SPARK RM 6.9(17)).
10670 Check_Ghost_Overriding (S, Overridden_Subp);
10673 -- If there is a homonym that is not overloadable, then we have an
10674 -- error, except for the special cases checked explicitly below.
10676 elsif not Is_Overloadable (E) then
10678 -- Check for spurious conflict produced by a subprogram that has the
10679 -- same name as that of the enclosing generic package. The conflict
10680 -- occurs within an instance, between the subprogram and the renaming
10681 -- declaration for the package. After the subprogram, the package
10682 -- renaming declaration becomes hidden.
10684 if Ekind (E) = E_Package
10685 and then Present (Renamed_Object (E))
10686 and then Renamed_Object (E) = Current_Scope
10687 and then Nkind (Parent (Renamed_Object (E))) =
10688 N_Package_Specification
10689 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10692 Set_Is_Immediately_Visible (E, False);
10693 Enter_Overloaded_Entity (S);
10694 Set_Homonym (S, Homonym (E));
10695 Check_Dispatching_Operation (S, Empty);
10696 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10698 -- If the subprogram is implicit it is hidden by the previous
10699 -- declaration. However if it is dispatching, it must appear in the
10700 -- dispatch table anyway, because it can be dispatched to even if it
10701 -- cannot be called directly.
10703 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10704 Set_Scope (S, Current_Scope);
10706 if Is_Dispatching_Operation (Alias (S)) then
10707 Check_Dispatching_Operation (S, Empty);
10713 Report_Conflict (S, E);
10717 -- E exists and is overloadable
10720 Check_Synchronized_Overriding (S, Overridden_Subp);
10722 -- Loop through E and its homonyms to determine if any of them is
10723 -- the candidate for overriding by S.
10725 while Present (E) loop
10727 -- Definitely not interesting if not in the current scope
10729 if Scope (E) /= Current_Scope then
10732 -- A function can overload the name of an abstract state. The
10733 -- state can be viewed as a function with a profile that cannot
10734 -- be matched by anything.
10736 elsif Ekind (S) = E_Function
10737 and then Ekind (E) = E_Abstract_State
10739 Enter_Overloaded_Entity (S);
10742 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10743 -- procedures locate the internally generated spec. We enforce
10744 -- mode conformance since a tagged type may inherit from
10745 -- interfaces several null primitives which differ only in
10746 -- the mode of the formals.
10748 elsif not Comes_From_Source (S)
10749 and then Is_Null_Procedure (S)
10750 and then not Mode_Conformant (E, S)
10754 -- Check if we have type conformance
10756 elsif Type_Conformant (E, S) then
10758 -- If the old and new entities have the same profile and one
10759 -- is not the body of the other, then this is an error, unless
10760 -- one of them is implicitly declared.
10762 -- There are some cases when both can be implicit, for example
10763 -- when both a literal and a function that overrides it are
10764 -- inherited in a derivation, or when an inherited operation
10765 -- of a tagged full type overrides the inherited operation of
10766 -- a private extension. Ada 83 had a special rule for the
10767 -- literal case. In Ada 95, the later implicit operation hides
10768 -- the former, and the literal is always the former. In the
10769 -- odd case where both are derived operations declared at the
10770 -- same point, both operations should be declared, and in that
10771 -- case we bypass the following test and proceed to the next
10772 -- part. This can only occur for certain obscure cases in
10773 -- instances, when an operation on a type derived from a formal
10774 -- private type does not override a homograph inherited from
10775 -- the actual. In subsequent derivations of such a type, the
10776 -- DT positions of these operations remain distinct, if they
10779 if Present (Alias (S))
10780 and then (No (Alias (E))
10781 or else Comes_From_Source (E)
10782 or else Is_Abstract_Subprogram (S)
10784 (Is_Dispatching_Operation (E)
10785 and then Is_Overriding_Alias (E, S)))
10786 and then Ekind (E) /= E_Enumeration_Literal
10788 -- When an derived operation is overloaded it may be due to
10789 -- the fact that the full view of a private extension
10790 -- re-inherits. It has to be dealt with.
10792 if Is_Package_Or_Generic_Package (Current_Scope)
10793 and then In_Private_Part (Current_Scope)
10795 Check_Operation_From_Private_View (S, E);
10798 -- In any case the implicit operation remains hidden by the
10799 -- existing declaration, which is overriding. Indicate that
10800 -- E overrides the operation from which S is inherited.
10802 if Present (Alias (S)) then
10803 Set_Overridden_Operation (E, Alias (S));
10804 Inherit_Subprogram_Contract (E, Alias (S));
10807 Set_Overridden_Operation (E, S);
10808 Inherit_Subprogram_Contract (E, S);
10811 if Comes_From_Source (E) then
10812 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10814 -- The Ghost policy in effect at the point of declaration
10815 -- of a parent subprogram and an overriding subprogram
10816 -- must match (SPARK RM 6.9(17)).
10818 Check_Ghost_Overriding (E, S);
10823 -- Within an instance, the renaming declarations for actual
10824 -- subprograms may become ambiguous, but they do not hide each
10827 elsif Ekind (E) /= E_Entry
10828 and then not Comes_From_Source (E)
10829 and then not Is_Generic_Instance (E)
10830 and then (Present (Alias (E))
10831 or else Is_Intrinsic_Subprogram (E))
10832 and then (not In_Instance
10833 or else No (Parent (E))
10834 or else Nkind (Unit_Declaration_Node (E)) /=
10835 N_Subprogram_Renaming_Declaration)
10837 -- A subprogram child unit is not allowed to override an
10838 -- inherited subprogram (10.1.1(20)).
10840 if Is_Child_Unit (S) then
10842 ("child unit overrides inherited subprogram in parent",
10847 if Is_Non_Overriding_Operation (E, S) then
10848 Enter_Overloaded_Entity (S);
10850 if No (Derived_Type)
10851 or else Is_Tagged_Type (Derived_Type)
10853 Check_Dispatching_Operation (S, Empty);
10859 -- E is a derived operation or an internal operator which
10860 -- is being overridden. Remove E from further visibility.
10861 -- Furthermore, if E is a dispatching operation, it must be
10862 -- replaced in the list of primitive operations of its type
10863 -- (see Override_Dispatching_Operation).
10865 Overridden_Subp := E;
10871 Prev := First_Entity (Current_Scope);
10872 while Present (Prev) and then Next_Entity (Prev) /= E loop
10873 Next_Entity (Prev);
10876 -- It is possible for E to be in the current scope and
10877 -- yet not in the entity chain. This can only occur in a
10878 -- generic context where E is an implicit concatenation
10879 -- in the formal part, because in a generic body the
10880 -- entity chain starts with the formals.
10882 -- In GNATprove mode, a wrapper for an operation with
10883 -- axiomatization may be a homonym of another declaration
10884 -- for an actual subprogram (needs refinement ???).
10888 and then GNATprove_Mode
10890 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10891 N_Subprogram_Renaming_Declaration
10895 pragma Assert (Chars (E) = Name_Op_Concat);
10900 -- E must be removed both from the entity_list of the
10901 -- current scope, and from the visibility chain.
10903 if Debug_Flag_E then
10904 Write_Str ("Override implicit operation ");
10905 Write_Int (Int (E));
10909 -- If E is a predefined concatenation, it stands for four
10910 -- different operations. As a result, a single explicit
10911 -- declaration does not hide it. In a possible ambiguous
10912 -- situation, Disambiguate chooses the user-defined op,
10913 -- so it is correct to retain the previous internal one.
10915 if Chars (E) /= Name_Op_Concat
10916 or else Ekind (E) /= E_Operator
10918 -- For nondispatching derived operations that are
10919 -- overridden by a subprogram declared in the private
10920 -- part of a package, we retain the derived subprogram
10921 -- but mark it as not immediately visible. If the
10922 -- derived operation was declared in the visible part
10923 -- then this ensures that it will still be visible
10924 -- outside the package with the proper signature
10925 -- (calls from outside must also be directed to this
10926 -- version rather than the overriding one, unlike the
10927 -- dispatching case). Calls from inside the package
10928 -- will still resolve to the overriding subprogram
10929 -- since the derived one is marked as not visible
10930 -- within the package.
10932 -- If the private operation is dispatching, we achieve
10933 -- the overriding by keeping the implicit operation
10934 -- but setting its alias to be the overriding one. In
10935 -- this fashion the proper body is executed in all
10936 -- cases, but the original signature is used outside
10939 -- If the overriding is not in the private part, we
10940 -- remove the implicit operation altogether.
10942 if Is_Private_Declaration (S) then
10943 if not Is_Dispatching_Operation (E) then
10944 Set_Is_Immediately_Visible (E, False);
10946 -- Work done in Override_Dispatching_Operation,
10947 -- so nothing else needs to be done here.
10953 -- Find predecessor of E in Homonym chain
10955 if E = Current_Entity (E) then
10958 Prev_Vis := Current_Entity (E);
10959 while Homonym (Prev_Vis) /= E loop
10960 Prev_Vis := Homonym (Prev_Vis);
10964 if Prev_Vis /= Empty then
10966 -- Skip E in the visibility chain
10968 Set_Homonym (Prev_Vis, Homonym (E));
10971 Set_Name_Entity_Id (Chars (E), Homonym (E));
10974 Set_Next_Entity (Prev, Next_Entity (E));
10976 if No (Next_Entity (Prev)) then
10977 Set_Last_Entity (Current_Scope, Prev);
10982 Enter_Overloaded_Entity (S);
10984 -- For entities generated by Derive_Subprograms the
10985 -- overridden operation is the inherited primitive
10986 -- (which is available through the attribute alias).
10988 if not (Comes_From_Source (E))
10989 and then Is_Dispatching_Operation (E)
10990 and then Find_Dispatching_Type (E) =
10991 Find_Dispatching_Type (S)
10992 and then Present (Alias (E))
10993 and then Comes_From_Source (Alias (E))
10995 Set_Overridden_Operation (S, Alias (E));
10996 Inherit_Subprogram_Contract (S, Alias (E));
10998 -- Normal case of setting entity as overridden
11000 -- Note: Static_Initialization and Overridden_Operation
11001 -- attributes use the same field in subprogram entities.
11002 -- Static_Initialization is only defined for internal
11003 -- initialization procedures, where Overridden_Operation
11004 -- is irrelevant. Therefore the setting of this attribute
11005 -- must check whether the target is an init_proc.
11007 elsif not Is_Init_Proc (S) then
11008 Set_Overridden_Operation (S, E);
11009 Inherit_Subprogram_Contract (S, E);
11012 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11014 -- The Ghost policy in effect at the point of declaration
11015 -- of a parent subprogram and an overriding subprogram
11016 -- must match (SPARK RM 6.9(17)).
11018 Check_Ghost_Overriding (S, E);
11020 -- If S is a user-defined subprogram or a null procedure
11021 -- expanded to override an inherited null procedure, or a
11022 -- predefined dispatching primitive then indicate that E
11023 -- overrides the operation from which S is inherited.
11025 if Comes_From_Source (S)
11027 (Present (Parent (S))
11029 Nkind (Parent (S)) = N_Procedure_Specification
11031 Null_Present (Parent (S)))
11033 (Present (Alias (E))
11035 Is_Predefined_Dispatching_Operation (Alias (E)))
11037 if Present (Alias (E)) then
11038 Set_Overridden_Operation (S, Alias (E));
11039 Inherit_Subprogram_Contract (S, Alias (E));
11043 if Is_Dispatching_Operation (E) then
11045 -- An overriding dispatching subprogram inherits the
11046 -- convention of the overridden subprogram (AI-117).
11048 Set_Convention (S, Convention (E));
11049 Check_Dispatching_Operation (S, E);
11052 Check_Dispatching_Operation (S, Empty);
11055 Check_For_Primitive_Subprogram
11056 (Is_Primitive_Subp, Is_Overriding => True);
11057 goto Check_Inequality;
11060 -- Apparent redeclarations in instances can occur when two
11061 -- formal types get the same actual type. The subprograms in
11062 -- in the instance are legal, even if not callable from the
11063 -- outside. Calls from within are disambiguated elsewhere.
11064 -- For dispatching operations in the visible part, the usual
11065 -- rules apply, and operations with the same profile are not
11066 -- legal (B830001).
11068 elsif (In_Instance_Visible_Part
11069 and then not Is_Dispatching_Operation (E))
11070 or else In_Instance_Not_Visible
11074 -- Here we have a real error (identical profile)
11077 Error_Msg_Sloc := Sloc (E);
11079 -- Avoid cascaded errors if the entity appears in
11080 -- subsequent calls.
11082 Set_Scope (S, Current_Scope);
11084 -- Generate error, with extra useful warning for the case
11085 -- of a generic instance with no completion.
11087 if Is_Generic_Instance (S)
11088 and then not Has_Completion (E)
11091 ("instantiation cannot provide body for&", S);
11092 Error_Msg_N ("\& conflicts with declaration#", S);
11094 Error_Msg_N ("& conflicts with declaration#", S);
11101 -- If one subprogram has an access parameter and the other
11102 -- a parameter of an access type, calls to either might be
11103 -- ambiguous. Verify that parameters match except for the
11104 -- access parameter.
11106 if May_Hide_Profile then
11112 F1 := First_Formal (S);
11113 F2 := First_Formal (E);
11114 while Present (F1) and then Present (F2) loop
11115 if Is_Access_Type (Etype (F1)) then
11116 if not Is_Access_Type (Etype (F2))
11117 or else not Conforming_Types
11118 (Designated_Type (Etype (F1)),
11119 Designated_Type (Etype (F2)),
11122 May_Hide_Profile := False;
11126 not Conforming_Types
11127 (Etype (F1), Etype (F2), Type_Conformant)
11129 May_Hide_Profile := False;
11136 if May_Hide_Profile
11140 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11149 -- On exit, we know that S is a new entity
11151 Enter_Overloaded_Entity (S);
11152 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11153 Check_Overriding_Indicator
11154 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11156 -- The Ghost policy in effect at the point of declaration of a parent
11157 -- subprogram and an overriding subprogram must match
11158 -- (SPARK RM 6.9(17)).
11160 Check_Ghost_Overriding (S, Overridden_Subp);
11162 -- Overloading is not allowed in SPARK, except for operators
11164 if Nkind (S) /= N_Defining_Operator_Symbol then
11165 Error_Msg_Sloc := Sloc (Homonym (S));
11166 Check_SPARK_05_Restriction
11167 ("overloading not allowed with entity#", S);
11170 -- If S is a derived operation for an untagged type then by
11171 -- definition it's not a dispatching operation (even if the parent
11172 -- operation was dispatching), so Check_Dispatching_Operation is not
11173 -- called in that case.
11175 if No (Derived_Type)
11176 or else Is_Tagged_Type (Derived_Type)
11178 Check_Dispatching_Operation (S, Empty);
11182 -- If this is a user-defined equality operator that is not a derived
11183 -- subprogram, create the corresponding inequality. If the operation is
11184 -- dispatching, the expansion is done elsewhere, and we do not create
11185 -- an explicit inequality operation.
11187 <<Check_Inequality>>
11188 if Chars (S) = Name_Op_Eq
11189 and then Etype (S) = Standard_Boolean
11190 and then Present (Parent (S))
11191 and then not Is_Dispatching_Operation (S)
11193 Make_Inequality_Operator (S);
11194 Check_Untagged_Equality (S);
11196 end New_Overloaded_Entity;
11198 ---------------------
11199 -- Process_Formals --
11200 ---------------------
11202 procedure Process_Formals
11204 Related_Nod : Node_Id)
11206 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11207 -- Determine whether an access type designates a type coming from a
11210 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11211 -- Check whether the default has a class-wide type. After analysis the
11212 -- default has the type of the formal, so we must also check explicitly
11213 -- for an access attribute.
11215 ----------------------------------
11216 -- Designates_From_Limited_With --
11217 ----------------------------------
11219 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11220 Desig : Entity_Id := Typ;
11223 if Is_Access_Type (Desig) then
11224 Desig := Directly_Designated_Type (Desig);
11227 if Is_Class_Wide_Type (Desig) then
11228 Desig := Root_Type (Desig);
11232 Ekind (Desig) = E_Incomplete_Type
11233 and then From_Limited_With (Desig);
11234 end Designates_From_Limited_With;
11236 ---------------------------
11237 -- Is_Class_Wide_Default --
11238 ---------------------------
11240 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11242 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11243 or else (Nkind (D) = N_Attribute_Reference
11244 and then Attribute_Name (D) = Name_Access
11245 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11246 end Is_Class_Wide_Default;
11250 Context : constant Node_Id := Parent (Parent (T));
11252 Formal : Entity_Id;
11253 Formal_Type : Entity_Id;
11254 Param_Spec : Node_Id;
11257 Num_Out_Params : Nat := 0;
11258 First_Out_Param : Entity_Id := Empty;
11259 -- Used for setting Is_Only_Out_Parameter
11261 -- Start of processing for Process_Formals
11264 -- In order to prevent premature use of the formals in the same formal
11265 -- part, the Ekind is left undefined until all default expressions are
11266 -- analyzed. The Ekind is established in a separate loop at the end.
11268 Param_Spec := First (T);
11269 while Present (Param_Spec) loop
11270 Formal := Defining_Identifier (Param_Spec);
11271 Set_Never_Set_In_Source (Formal, True);
11272 Enter_Name (Formal);
11274 -- Case of ordinary parameters
11276 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11277 Find_Type (Parameter_Type (Param_Spec));
11278 Ptype := Parameter_Type (Param_Spec);
11280 if Ptype = Error then
11284 Formal_Type := Entity (Ptype);
11286 if Is_Incomplete_Type (Formal_Type)
11288 (Is_Class_Wide_Type (Formal_Type)
11289 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11291 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11292 -- primitive operations, as long as their completion is
11293 -- in the same declarative part. If in the private part
11294 -- this means that the type cannot be a Taft-amendment type.
11295 -- Check is done on package exit. For access to subprograms,
11296 -- the use is legal for Taft-amendment types.
11298 -- Ada 2012: tagged incomplete types are allowed as generic
11299 -- formal types. They do not introduce dependencies and the
11300 -- corresponding generic subprogram does not have a delayed
11301 -- freeze, because it does not need a freeze node. However,
11302 -- it is still the case that untagged incomplete types cannot
11303 -- be Taft-amendment types and must be completed in private
11304 -- part, so the subprogram must appear in the list of private
11305 -- dependents of the type.
11307 if Is_Tagged_Type (Formal_Type)
11308 or else (Ada_Version >= Ada_2012
11309 and then not From_Limited_With (Formal_Type)
11310 and then not Is_Generic_Type (Formal_Type))
11312 if Ekind (Scope (Current_Scope)) = E_Package
11313 and then not Is_Generic_Type (Formal_Type)
11314 and then not Is_Class_Wide_Type (Formal_Type)
11317 (Parent (T), N_Access_Function_Definition,
11318 N_Access_Procedure_Definition)
11320 Append_Elmt (Current_Scope,
11321 Private_Dependents (Base_Type (Formal_Type)));
11323 -- Freezing is delayed to ensure that Register_Prim
11324 -- will get called for this operation, which is needed
11325 -- in cases where static dispatch tables aren't built.
11326 -- (Note that the same is done for controlling access
11327 -- parameter cases in function Access_Definition.)
11329 if not Is_Thunk (Current_Scope) then
11330 Set_Has_Delayed_Freeze (Current_Scope);
11335 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11336 N_Access_Procedure_Definition)
11338 -- AI05-0151: Tagged incomplete types are allowed in all
11339 -- formal parts. Untagged incomplete types are not allowed
11340 -- in bodies. Limited views of either kind are not allowed
11341 -- if there is no place at which the non-limited view can
11342 -- become available.
11344 -- Incomplete formal untagged types are not allowed in
11345 -- subprogram bodies (but are legal in their declarations).
11346 -- This excludes bodies created for null procedures, which
11347 -- are basic declarations.
11349 if Is_Generic_Type (Formal_Type)
11350 and then not Is_Tagged_Type (Formal_Type)
11351 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11354 ("invalid use of formal incomplete type", Param_Spec);
11356 elsif Ada_Version >= Ada_2012 then
11357 if Is_Tagged_Type (Formal_Type)
11358 and then (not From_Limited_With (Formal_Type)
11359 or else not In_Package_Body)
11363 elsif Nkind_In (Context, N_Accept_Statement,
11364 N_Accept_Alternative,
11366 or else (Nkind (Context) = N_Subprogram_Body
11367 and then Comes_From_Source (Context))
11370 ("invalid use of untagged incomplete type &",
11371 Ptype, Formal_Type);
11376 ("invalid use of incomplete type&",
11377 Param_Spec, Formal_Type);
11379 -- Further checks on the legality of incomplete types
11380 -- in formal parts are delayed until the freeze point
11381 -- of the enclosing subprogram or access to subprogram.
11385 elsif Ekind (Formal_Type) = E_Void then
11387 ("premature use of&",
11388 Parameter_Type (Param_Spec), Formal_Type);
11391 -- Ada 2012 (AI-142): Handle aliased parameters
11393 if Ada_Version >= Ada_2012
11394 and then Aliased_Present (Param_Spec)
11396 Set_Is_Aliased (Formal);
11399 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11400 -- declaration corresponding to the null-excluding type of the
11401 -- formal in the enclosing scope. Finally, replace the parameter
11402 -- type of the formal with the internal subtype.
11404 if Ada_Version >= Ada_2005
11405 and then Null_Exclusion_Present (Param_Spec)
11407 if not Is_Access_Type (Formal_Type) then
11409 ("`NOT NULL` allowed only for an access type", Param_Spec);
11412 if Can_Never_Be_Null (Formal_Type)
11413 and then Comes_From_Source (Related_Nod)
11416 ("`NOT NULL` not allowed (& already excludes null)",
11417 Param_Spec, Formal_Type);
11421 Create_Null_Excluding_Itype
11423 Related_Nod => Related_Nod,
11424 Scope_Id => Scope (Current_Scope));
11426 -- If the designated type of the itype is an itype that is
11427 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11428 -- on the access subtype, to prevent order-of-elaboration
11429 -- issues in the backend.
11432 -- type T is access procedure;
11433 -- procedure Op (O : not null T);
11435 if Is_Itype (Directly_Designated_Type (Formal_Type))
11437 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11439 Set_Has_Delayed_Freeze (Formal_Type);
11444 -- An access formal type
11448 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11450 -- No need to continue if we already notified errors
11452 if not Present (Formal_Type) then
11456 -- Ada 2005 (AI-254)
11459 AD : constant Node_Id :=
11460 Access_To_Subprogram_Definition
11461 (Parameter_Type (Param_Spec));
11463 if Present (AD) and then Protected_Present (AD) then
11465 Replace_Anonymous_Access_To_Protected_Subprogram
11471 Set_Etype (Formal, Formal_Type);
11473 -- Deal with default expression if present
11475 Default := Expression (Param_Spec);
11477 if Present (Default) then
11478 Check_SPARK_05_Restriction
11479 ("default expression is not allowed", Default);
11481 if Out_Present (Param_Spec) then
11483 ("default initialization only allowed for IN parameters",
11487 -- Do the special preanalysis of the expression (see section on
11488 -- "Handling of Default Expressions" in the spec of package Sem).
11490 Preanalyze_Spec_Expression (Default, Formal_Type);
11492 -- An access to constant cannot be the default for
11493 -- an access parameter that is an access to variable.
11495 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11496 and then not Is_Access_Constant (Formal_Type)
11497 and then Is_Access_Type (Etype (Default))
11498 and then Is_Access_Constant (Etype (Default))
11501 ("formal that is access to variable cannot be initialized "
11502 & "with an access-to-constant expression", Default);
11505 -- Check that the designated type of an access parameter's default
11506 -- is not a class-wide type unless the parameter's designated type
11507 -- is also class-wide.
11509 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11510 and then not Designates_From_Limited_With (Formal_Type)
11511 and then Is_Class_Wide_Default (Default)
11512 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11515 ("access to class-wide expression not allowed here", Default);
11518 -- Check incorrect use of dynamically tagged expressions
11520 if Is_Tagged_Type (Formal_Type) then
11521 Check_Dynamically_Tagged_Expression
11523 Typ => Formal_Type,
11524 Related_Nod => Default);
11528 -- Ada 2005 (AI-231): Static checks
11530 if Ada_Version >= Ada_2005
11531 and then Is_Access_Type (Etype (Formal))
11532 and then Can_Never_Be_Null (Etype (Formal))
11534 Null_Exclusion_Static_Checks (Param_Spec);
11537 -- The following checks are relevant only when SPARK_Mode is on as
11538 -- these are not standard Ada legality rules.
11540 if SPARK_Mode = On then
11541 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11543 -- A function cannot have a parameter of mode IN OUT or OUT
11546 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11548 ("function cannot have parameter of mode `OUT` or "
11549 & "`IN OUT`", Formal);
11552 -- A procedure cannot have an effectively volatile formal
11553 -- parameter of mode IN because it behaves as a constant
11554 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
11556 elsif Ekind (Scope (Formal)) = E_Procedure
11557 and then Ekind (Formal) = E_In_Parameter
11558 and then Is_Effectively_Volatile (Formal)
11561 ("formal parameter of mode `IN` cannot be volatile", Formal);
11569 -- If this is the formal part of a function specification, analyze the
11570 -- subtype mark in the context where the formals are visible but not
11571 -- yet usable, and may hide outer homographs.
11573 if Nkind (Related_Nod) = N_Function_Specification then
11574 Analyze_Return_Type (Related_Nod);
11577 -- Now set the kind (mode) of each formal
11579 Param_Spec := First (T);
11580 while Present (Param_Spec) loop
11581 Formal := Defining_Identifier (Param_Spec);
11582 Set_Formal_Mode (Formal);
11584 if Ekind (Formal) = E_In_Parameter then
11585 Set_Default_Value (Formal, Expression (Param_Spec));
11587 if Present (Expression (Param_Spec)) then
11588 Default := Expression (Param_Spec);
11590 if Is_Scalar_Type (Etype (Default)) then
11591 if Nkind (Parameter_Type (Param_Spec)) /=
11592 N_Access_Definition
11594 Formal_Type := Entity (Parameter_Type (Param_Spec));
11598 (Related_Nod, Parameter_Type (Param_Spec));
11601 Apply_Scalar_Range_Check (Default, Formal_Type);
11605 elsif Ekind (Formal) = E_Out_Parameter then
11606 Num_Out_Params := Num_Out_Params + 1;
11608 if Num_Out_Params = 1 then
11609 First_Out_Param := Formal;
11612 elsif Ekind (Formal) = E_In_Out_Parameter then
11613 Num_Out_Params := Num_Out_Params + 1;
11616 -- Skip remaining processing if formal type was in error
11618 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11619 goto Next_Parameter;
11622 -- Force call by reference if aliased
11625 Conv : constant Convention_Id := Convention (Etype (Formal));
11627 if Is_Aliased (Formal) then
11628 Set_Mechanism (Formal, By_Reference);
11630 -- Warn if user asked this to be passed by copy
11632 if Conv = Convention_Ada_Pass_By_Copy then
11634 ("cannot pass aliased parameter & by copy??", Formal);
11637 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11639 elsif Conv = Convention_Ada_Pass_By_Copy then
11640 Set_Mechanism (Formal, By_Copy);
11642 elsif Conv = Convention_Ada_Pass_By_Reference then
11643 Set_Mechanism (Formal, By_Reference);
11651 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11652 Set_Is_Only_Out_Parameter (First_Out_Param);
11654 end Process_Formals;
11656 ----------------------------
11657 -- Reference_Body_Formals --
11658 ----------------------------
11660 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11665 if Error_Posted (Spec) then
11669 -- Iterate over both lists. They may be of different lengths if the two
11670 -- specs are not conformant.
11672 Fs := First_Formal (Spec);
11673 Fb := First_Formal (Bod);
11674 while Present (Fs) and then Present (Fb) loop
11675 Generate_Reference (Fs, Fb, 'b');
11677 if Style_Check then
11678 Style.Check_Identifier (Fb, Fs);
11681 Set_Spec_Entity (Fb, Fs);
11682 Set_Referenced (Fs, False);
11686 end Reference_Body_Formals;
11688 -------------------------
11689 -- Set_Actual_Subtypes --
11690 -------------------------
11692 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11694 Formal : Entity_Id;
11696 First_Stmt : Node_Id := Empty;
11697 AS_Needed : Boolean;
11700 -- If this is an empty initialization procedure, no need to create
11701 -- actual subtypes (small optimization).
11703 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11706 -- Within a predicate function we do not want to generate local
11707 -- subtypes that may generate nested predicate functions.
11709 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
11713 -- The subtype declarations may freeze the formals. The body generated
11714 -- for an expression function is not a freeze point, so do not emit
11715 -- these declarations (small loss of efficiency in rare cases).
11717 if Nkind (N) = N_Subprogram_Body
11718 and then Was_Expression_Function (N)
11723 Formal := First_Formal (Subp);
11724 while Present (Formal) loop
11725 T := Etype (Formal);
11727 -- We never need an actual subtype for a constrained formal
11729 if Is_Constrained (T) then
11730 AS_Needed := False;
11732 -- If we have unknown discriminants, then we do not need an actual
11733 -- subtype, or more accurately we cannot figure it out. Note that
11734 -- all class-wide types have unknown discriminants.
11736 elsif Has_Unknown_Discriminants (T) then
11737 AS_Needed := False;
11739 -- At this stage we have an unconstrained type that may need an
11740 -- actual subtype. For sure the actual subtype is needed if we have
11741 -- an unconstrained array type. However, in an instance, the type
11742 -- may appear as a subtype of the full view, while the actual is
11743 -- in fact private (in which case no actual subtype is needed) so
11744 -- check the kind of the base type.
11746 elsif Is_Array_Type (Base_Type (T)) then
11749 -- The only other case needing an actual subtype is an unconstrained
11750 -- record type which is an IN parameter (we cannot generate actual
11751 -- subtypes for the OUT or IN OUT case, since an assignment can
11752 -- change the discriminant values. However we exclude the case of
11753 -- initialization procedures, since discriminants are handled very
11754 -- specially in this context, see the section entitled "Handling of
11755 -- Discriminants" in Einfo.
11757 -- We also exclude the case of Discrim_SO_Functions (functions used
11758 -- in front-end layout mode for size/offset values), since in such
11759 -- functions only discriminants are referenced, and not only are such
11760 -- subtypes not needed, but they cannot always be generated, because
11761 -- of order of elaboration issues.
11763 elsif Is_Record_Type (T)
11764 and then Ekind (Formal) = E_In_Parameter
11765 and then Chars (Formal) /= Name_uInit
11766 and then not Is_Unchecked_Union (T)
11767 and then not Is_Discrim_SO_Function (Subp)
11771 -- All other cases do not need an actual subtype
11774 AS_Needed := False;
11777 -- Generate actual subtypes for unconstrained arrays and
11778 -- unconstrained discriminated records.
11781 if Nkind (N) = N_Accept_Statement then
11783 -- If expansion is active, the formal is replaced by a local
11784 -- variable that renames the corresponding entry of the
11785 -- parameter block, and it is this local variable that may
11786 -- require an actual subtype.
11788 if Expander_Active then
11789 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11791 Decl := Build_Actual_Subtype (T, Formal);
11794 if Present (Handled_Statement_Sequence (N)) then
11796 First (Statements (Handled_Statement_Sequence (N)));
11797 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11798 Mark_Rewrite_Insertion (Decl);
11800 -- If the accept statement has no body, there will be no
11801 -- reference to the actuals, so no need to compute actual
11808 Decl := Build_Actual_Subtype (T, Formal);
11809 Prepend (Decl, Declarations (N));
11810 Mark_Rewrite_Insertion (Decl);
11813 -- The declaration uses the bounds of an existing object, and
11814 -- therefore needs no constraint checks.
11816 Analyze (Decl, Suppress => All_Checks);
11817 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
11819 -- We need to freeze manually the generated type when it is
11820 -- inserted anywhere else than in a declarative part.
11822 if Present (First_Stmt) then
11823 Insert_List_Before_And_Analyze (First_Stmt,
11824 Freeze_Entity (Defining_Identifier (Decl), N));
11826 -- Ditto if the type has a dynamic predicate, because the
11827 -- generated function will mention the actual subtype. The
11828 -- predicate may come from an explicit aspect of be inherited.
11830 elsif Has_Predicates (T) then
11831 Insert_List_Before_And_Analyze (Decl,
11832 Freeze_Entity (Defining_Identifier (Decl), N));
11835 if Nkind (N) = N_Accept_Statement
11836 and then Expander_Active
11838 Set_Actual_Subtype (Renamed_Object (Formal),
11839 Defining_Identifier (Decl));
11841 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11845 Next_Formal (Formal);
11847 end Set_Actual_Subtypes;
11849 ---------------------
11850 -- Set_Formal_Mode --
11851 ---------------------
11853 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11854 Spec : constant Node_Id := Parent (Formal_Id);
11855 Id : constant Entity_Id := Scope (Formal_Id);
11858 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11859 -- since we ensure that corresponding actuals are always valid at the
11860 -- point of the call.
11862 if Out_Present (Spec) then
11863 if Ekind_In (Id, E_Entry, E_Entry_Family)
11864 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11866 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11869 if Ekind_In (Id, E_Function, E_Generic_Function) then
11871 -- [IN] OUT parameters allowed for functions in Ada 2012
11873 if Ada_Version >= Ada_2012 then
11875 -- Even in Ada 2012 operators can only have IN parameters
11877 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11878 Error_Msg_N ("operators can only have IN parameters", Spec);
11881 if In_Present (Spec) then
11882 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11884 Set_Ekind (Formal_Id, E_Out_Parameter);
11887 -- But not in earlier versions of Ada
11890 Error_Msg_N ("functions can only have IN parameters", Spec);
11891 Set_Ekind (Formal_Id, E_In_Parameter);
11894 elsif In_Present (Spec) then
11895 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11898 Set_Ekind (Formal_Id, E_Out_Parameter);
11899 Set_Never_Set_In_Source (Formal_Id, True);
11900 Set_Is_True_Constant (Formal_Id, False);
11901 Set_Current_Value (Formal_Id, Empty);
11905 Set_Ekind (Formal_Id, E_In_Parameter);
11908 -- Set Is_Known_Non_Null for access parameters since the language
11909 -- guarantees that access parameters are always non-null. We also set
11910 -- Can_Never_Be_Null, since there is no way to change the value.
11912 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11914 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11915 -- null; In Ada 2005, only if then null_exclusion is explicit.
11917 if Ada_Version < Ada_2005
11918 or else Can_Never_Be_Null (Etype (Formal_Id))
11920 Set_Is_Known_Non_Null (Formal_Id);
11921 Set_Can_Never_Be_Null (Formal_Id);
11924 -- Ada 2005 (AI-231): Null-exclusion access subtype
11926 elsif Is_Access_Type (Etype (Formal_Id))
11927 and then Can_Never_Be_Null (Etype (Formal_Id))
11929 Set_Is_Known_Non_Null (Formal_Id);
11931 -- We can also set Can_Never_Be_Null (thus preventing some junk
11932 -- access checks) for the case of an IN parameter, which cannot
11933 -- be changed, or for an IN OUT parameter, which can be changed but
11934 -- not to a null value. But for an OUT parameter, the initial value
11935 -- passed in can be null, so we can't set this flag in that case.
11937 if Ekind (Formal_Id) /= E_Out_Parameter then
11938 Set_Can_Never_Be_Null (Formal_Id);
11942 Set_Mechanism (Formal_Id, Default_Mechanism);
11943 Set_Formal_Validity (Formal_Id);
11944 end Set_Formal_Mode;
11946 -------------------------
11947 -- Set_Formal_Validity --
11948 -------------------------
11950 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11952 -- If no validity checking, then we cannot assume anything about the
11953 -- validity of parameters, since we do not know there is any checking
11954 -- of the validity on the call side.
11956 if not Validity_Checks_On then
11959 -- If validity checking for parameters is enabled, this means we are
11960 -- not supposed to make any assumptions about argument values.
11962 elsif Validity_Check_Parameters then
11965 -- If we are checking in parameters, we will assume that the caller is
11966 -- also checking parameters, so we can assume the parameter is valid.
11968 elsif Ekind (Formal_Id) = E_In_Parameter
11969 and then Validity_Check_In_Params
11971 Set_Is_Known_Valid (Formal_Id, True);
11973 -- Similar treatment for IN OUT parameters
11975 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11976 and then Validity_Check_In_Out_Params
11978 Set_Is_Known_Valid (Formal_Id, True);
11980 end Set_Formal_Validity;
11982 ------------------------
11983 -- Subtype_Conformant --
11984 ------------------------
11986 function Subtype_Conformant
11987 (New_Id : Entity_Id;
11988 Old_Id : Entity_Id;
11989 Skip_Controlling_Formals : Boolean := False) return Boolean
11993 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11994 Skip_Controlling_Formals => Skip_Controlling_Formals);
11996 end Subtype_Conformant;
11998 ---------------------
11999 -- Type_Conformant --
12000 ---------------------
12002 function Type_Conformant
12003 (New_Id : Entity_Id;
12004 Old_Id : Entity_Id;
12005 Skip_Controlling_Formals : Boolean := False) return Boolean
12009 May_Hide_Profile := False;
12011 (New_Id, Old_Id, Type_Conformant, False, Result,
12012 Skip_Controlling_Formals => Skip_Controlling_Formals);
12014 end Type_Conformant;
12016 -------------------------------
12017 -- Valid_Operator_Definition --
12018 -------------------------------
12020 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12023 Id : constant Name_Id := Chars (Designator);
12027 F := First_Formal (Designator);
12028 while Present (F) loop
12031 if Present (Default_Value (F)) then
12033 ("default values not allowed for operator parameters",
12036 -- For function instantiations that are operators, we must check
12037 -- separately that the corresponding generic only has in-parameters.
12038 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12039 -- an error could not arise in earlier versions of the language.
12041 elsif Ekind (F) /= E_In_Parameter then
12042 Error_Msg_N ("operators can only have IN parameters", F);
12048 -- Verify that user-defined operators have proper number of arguments
12049 -- First case of operators which can only be unary
12051 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12054 -- Case of operators which can be unary or binary
12056 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12057 N_OK := (N in 1 .. 2);
12059 -- All other operators can only be binary
12067 ("incorrect number of arguments for operator", Designator);
12071 and then Base_Type (Etype (Designator)) = Standard_Boolean
12072 and then not Is_Intrinsic_Subprogram (Designator)
12075 ("explicit definition of inequality not allowed", Designator);
12077 end Valid_Operator_Definition;