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.
571 -- As elsewhere, we do not emit freeze nodes within a generic unit.
573 if not Inside_A_Generic then
574 Freeze_Expr_Types (Def_Id);
577 -- For navigation purposes, indicate that the function is a body
579 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
580 Rewrite (N, New_Body);
582 -- Remove any existing aspects from the original node because the act
583 -- of rewriting causes the list to be shared between the two nodes.
585 Orig_N := Original_Node (N);
586 Remove_Aspects (Orig_N);
588 -- Propagate any pragmas that apply to expression function to the
589 -- proper body when the expression function acts as a completion.
590 -- Aspects are automatically transfered because of node rewriting.
592 Relocate_Pragmas_To_Body (N);
595 -- Once the aspects of the generated body have been analyzed, create
596 -- a copy for ASIS purposes and associate it with the original node.
598 if Has_Aspects (N) then
599 Set_Aspect_Specifications (Orig_N,
600 New_Copy_List_Tree (Aspect_Specifications (N)));
603 -- Prev is the previous entity with the same name, but it is can
604 -- be an unrelated spec that is not completed by the expression
605 -- function. In that case the relevant entity is the one in the body.
606 -- Not clear that the backend can inline it in this case ???
608 if Has_Completion (Prev) then
610 -- The formals of the expression function are body formals,
611 -- and do not appear in the ali file, which will only contain
612 -- references to the formals of the original subprogram spec.
619 F1 := First_Formal (Def_Id);
620 F2 := First_Formal (Prev);
622 while Present (F1) loop
623 Set_Spec_Entity (F1, F2);
630 Set_Is_Inlined (Defining_Entity (New_Body));
633 -- If this is not a completion, create both a declaration and a body, so
634 -- that the expression can be inlined whenever possible.
637 -- An expression function that is not a completion is not a
638 -- subprogram declaration, and thus cannot appear in a protected
641 if Nkind (Parent (N)) = N_Protected_Definition then
643 ("an expression function is not a legal protected operation", N);
646 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
648 -- Remove any existing aspects from the original node because the act
649 -- of rewriting causes the list to be shared between the two nodes.
651 Orig_N := Original_Node (N);
652 Remove_Aspects (Orig_N);
656 -- Once the aspects of the generated spec have been analyzed, create
657 -- a copy for ASIS purposes and associate it with the original node.
659 if Has_Aspects (N) then
660 Set_Aspect_Specifications (Orig_N,
661 New_Copy_List_Tree (Aspect_Specifications (N)));
664 -- If aspect SPARK_Mode was specified on the body, it needs to be
665 -- repeated both on the generated spec and the body.
667 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
669 if Present (Asp) then
670 Asp := New_Copy_Tree (Asp);
671 Set_Analyzed (Asp, False);
672 Set_Aspect_Specifications (New_Body, New_List (Asp));
675 Def_Id := Defining_Entity (N);
676 Set_Is_Inlined (Def_Id);
678 -- Establish the linkages between the spec and the body. These are
679 -- used when the expression function acts as the prefix of attribute
680 -- 'Access in order to freeze the original expression which has been
681 -- moved to the generated body.
683 Set_Corresponding_Body (N, Defining_Entity (New_Body));
684 Set_Corresponding_Spec (New_Body, Def_Id);
686 -- Within a generic pre-analyze the original expression for name
687 -- capture. The body is also generated but plays no role in
688 -- this because it is not part of the original source.
690 if Inside_A_Generic then
691 Set_Has_Completion (Def_Id);
693 Install_Formals (Def_Id);
694 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
698 -- To prevent premature freeze action, insert the new body at the end
699 -- of the current declarations, or at the end of the package spec.
700 -- However, resolve usage names now, to prevent spurious visibility
701 -- on later entities. Note that the function can now be called in
702 -- the current declarative part, which will appear to be prior to
703 -- the presence of the body in the code. There are nevertheless no
704 -- order of elaboration issues because all name resolution has taken
705 -- place at the point of declaration.
708 Decls : List_Id := List_Containing (N);
709 Expr : constant Node_Id := Expression (Ret);
710 Par : constant Node_Id := Parent (Decls);
711 Typ : constant Entity_Id := Etype (Def_Id);
714 -- If this is a wrapper created for in an instance for a formal
715 -- subprogram, insert body after declaration, to be analyzed when
716 -- the enclosing instance is analyzed.
719 and then Is_Generic_Actual_Subprogram (Def_Id)
721 Insert_After (N, New_Body);
724 if Nkind (Par) = N_Package_Specification
725 and then Decls = Visible_Declarations (Par)
726 and then Present (Private_Declarations (Par))
727 and then not Is_Empty_List (Private_Declarations (Par))
729 Decls := Private_Declarations (Par);
732 Insert_After (Last (Decls), New_Body);
734 -- Preanalyze the expression if not already done above
736 if not Inside_A_Generic then
738 Install_Formals (Def_Id);
739 Preanalyze_Spec_Expression (Expr, Typ);
740 Check_Limited_Return (Original_Node (N), Expr, Typ);
747 -- Check incorrect use of dynamically tagged expression. This doesn't
748 -- fall out automatically when analyzing the generated function body,
749 -- because Check_Dynamically_Tagged_Expression deliberately ignores
750 -- nodes that don't come from source.
753 and then Nkind (Def_Id) in N_Has_Etype
754 and then Is_Tagged_Type (Etype (Def_Id))
756 Check_Dynamically_Tagged_Expression
758 Typ => Etype (Def_Id),
759 Related_Nod => Original_Node (N));
762 -- If the return expression is a static constant, we suppress warning
763 -- messages on unused formals, which in most cases will be noise.
765 Set_Is_Trivial_Subprogram
766 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
767 end Analyze_Expression_Function;
769 ----------------------------------------
770 -- Analyze_Extended_Return_Statement --
771 ----------------------------------------
773 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
775 Check_Compiler_Unit ("extended return statement", N);
776 Analyze_Return_Statement (N);
777 end Analyze_Extended_Return_Statement;
779 ----------------------------
780 -- Analyze_Function_Call --
781 ----------------------------
783 procedure Analyze_Function_Call (N : Node_Id) is
784 Actuals : constant List_Id := Parameter_Associations (N);
785 Func_Nam : constant Node_Id := Name (N);
791 -- A call of the form A.B (X) may be an Ada 2005 call, which is
792 -- rewritten as B (A, X). If the rewriting is successful, the call
793 -- has been analyzed and we just return.
795 if Nkind (Func_Nam) = N_Selected_Component
796 and then Name (N) /= Func_Nam
797 and then Is_Rewrite_Substitution (N)
798 and then Present (Etype (N))
803 -- If error analyzing name, then set Any_Type as result type and return
805 if Etype (Func_Nam) = Any_Type then
806 Set_Etype (N, Any_Type);
810 -- Otherwise analyze the parameters
812 if Present (Actuals) then
813 Actual := First (Actuals);
814 while Present (Actual) loop
816 Check_Parameterless_Call (Actual);
822 end Analyze_Function_Call;
824 -----------------------------
825 -- Analyze_Function_Return --
826 -----------------------------
828 procedure Analyze_Function_Return (N : Node_Id) is
829 Loc : constant Source_Ptr := Sloc (N);
830 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
831 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
833 R_Type : constant Entity_Id := Etype (Scope_Id);
834 -- Function result subtype
836 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
837 -- Apply legality rule of 6.5 (5.8) to the access discriminants of an
838 -- aggregate in a return statement.
840 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
841 -- Check that the return_subtype_indication properly matches the result
842 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
844 -----------------------------------
845 -- Check_Aggregate_Accessibility --
846 -----------------------------------
848 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
849 Typ : constant Entity_Id := Etype (Aggr);
856 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
857 Discr := First_Discriminant (Typ);
858 Assoc := First (Component_Associations (Aggr));
859 while Present (Discr) loop
860 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
861 Expr := Expression (Assoc);
863 if Nkind (Expr) = N_Attribute_Reference
864 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
866 Obj := Prefix (Expr);
867 while Nkind_In (Obj, N_Indexed_Component,
868 N_Selected_Component)
873 -- Do not check aliased formals or function calls. A
874 -- run-time check may still be needed ???
876 if Is_Entity_Name (Obj)
877 and then Comes_From_Source (Obj)
879 if Is_Formal (Entity (Obj))
880 and then Is_Aliased (Entity (Obj))
884 elsif Object_Access_Level (Obj) >
885 Scope_Depth (Scope (Scope_Id))
888 ("access discriminant in return aggregate would "
889 & "be a dangling reference", Obj);
895 Next_Discriminant (Discr);
898 end Check_Aggregate_Accessibility;
900 -------------------------------------
901 -- Check_Return_Subtype_Indication --
902 -------------------------------------
904 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
905 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
907 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
908 -- Subtype given in the extended return statement (must match R_Type)
910 Subtype_Ind : constant Node_Id :=
911 Object_Definition (Original_Node (Obj_Decl));
913 procedure Error_No_Match (N : Node_Id);
914 -- Output error messages for case where types do not statically
915 -- match. N is the location for the messages.
921 procedure Error_No_Match (N : Node_Id) is
924 ("subtype must statically match function result subtype", N);
926 if not Predicates_Match (R_Stm_Type, R_Type) then
927 Error_Msg_Node_2 := R_Type;
929 ("\predicate of& does not match predicate of&",
934 -- Start of processing for Check_Return_Subtype_Indication
937 -- First, avoid cascaded errors
939 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
943 -- "return access T" case; check that the return statement also has
944 -- "access T", and that the subtypes statically match:
945 -- if this is an access to subprogram the signatures must match.
947 if Is_Anonymous_Access_Type (R_Type) then
948 if Is_Anonymous_Access_Type (R_Stm_Type) then
949 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
951 if Base_Type (Designated_Type (R_Stm_Type)) /=
952 Base_Type (Designated_Type (R_Type))
953 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
955 Error_No_Match (Subtype_Mark (Subtype_Ind));
959 -- For two anonymous access to subprogram types, the types
960 -- themselves must be type conformant.
962 if not Conforming_Types
963 (R_Stm_Type, R_Type, Fully_Conformant)
965 Error_No_Match (Subtype_Ind);
970 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
973 -- If the return object is of an anonymous access type, then report
974 -- an error if the function's result type is not also anonymous.
976 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
977 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
979 ("anonymous access not allowed for function with named access "
980 & "result", Subtype_Ind);
982 -- Subtype indication case: check that the return object's type is
983 -- covered by the result type, and that the subtypes statically match
984 -- when the result subtype is constrained. Also handle record types
985 -- with unknown discriminants for which we have built the underlying
986 -- record view. Coverage is needed to allow specific-type return
987 -- objects when the result type is class-wide (see AI05-32).
989 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
990 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
994 Underlying_Record_View (Base_Type (R_Stm_Type))))
996 -- A null exclusion may be present on the return type, on the
997 -- function specification, on the object declaration or on the
1000 if Is_Access_Type (R_Type)
1002 (Can_Never_Be_Null (R_Type)
1003 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1004 Can_Never_Be_Null (R_Stm_Type)
1006 Error_No_Match (Subtype_Ind);
1009 -- AI05-103: for elementary types, subtypes must statically match
1011 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1012 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1013 Error_No_Match (Subtype_Ind);
1017 -- All remaining cases are illegal
1019 -- Note: previous versions of this subprogram allowed the return
1020 -- value to be the ancestor of the return type if the return type
1021 -- was a null extension. This was plainly incorrect.
1025 ("wrong type for return_subtype_indication", Subtype_Ind);
1027 end Check_Return_Subtype_Indication;
1029 ---------------------
1030 -- Local Variables --
1031 ---------------------
1036 -- Start of processing for Analyze_Function_Return
1039 Set_Return_Present (Scope_Id);
1041 if Nkind (N) = N_Simple_Return_Statement then
1042 Expr := Expression (N);
1044 -- Guard against a malformed expression. The parser may have tried to
1045 -- recover but the node is not analyzable.
1047 if Nkind (Expr) = N_Error then
1048 Set_Etype (Expr, Any_Type);
1049 Expander_Mode_Save_And_Set (False);
1053 -- The resolution of a controlled [extension] aggregate associated
1054 -- with a return statement creates a temporary which needs to be
1055 -- finalized on function exit. Wrap the return statement inside a
1056 -- block so that the finalization machinery can detect this case.
1057 -- This early expansion is done only when the return statement is
1058 -- not part of a handled sequence of statements.
1060 if Nkind_In (Expr, N_Aggregate,
1061 N_Extension_Aggregate)
1062 and then Needs_Finalization (R_Type)
1063 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1066 Make_Block_Statement (Loc,
1067 Handled_Statement_Sequence =>
1068 Make_Handled_Sequence_Of_Statements (Loc,
1069 Statements => New_List (Relocate_Node (N)))));
1077 -- Ada 2005 (AI-251): If the type of the returned object is
1078 -- an access to an interface type then we add an implicit type
1079 -- conversion to force the displacement of the "this" pointer to
1080 -- reference the secondary dispatch table. We cannot delay the
1081 -- generation of this implicit conversion until the expansion
1082 -- because in this case the type resolution changes the decoration
1083 -- of the expression node to match R_Type; by contrast, if the
1084 -- returned object is a class-wide interface type then it is too
1085 -- early to generate here the implicit conversion since the return
1086 -- statement may be rewritten by the expander into an extended
1087 -- return statement whose expansion takes care of adding the
1088 -- implicit type conversion to displace the pointer to the object.
1091 and then Serious_Errors_Detected = 0
1092 and then Is_Access_Type (R_Type)
1093 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1094 and then Is_Interface (Designated_Type (R_Type))
1095 and then Is_Progenitor (Designated_Type (R_Type),
1096 Designated_Type (Etype (Expr)))
1098 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1102 Resolve (Expr, R_Type);
1103 Check_Limited_Return (N, Expr, R_Type);
1105 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
1106 Check_Aggregate_Accessibility (Expr);
1110 -- RETURN only allowed in SPARK as the last statement in function
1112 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1114 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
1115 or else Present (Next (N)))
1117 Check_SPARK_05_Restriction
1118 ("RETURN should be the last statement in function", N);
1122 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
1123 Obj_Decl := Last (Return_Object_Declarations (N));
1125 -- Analyze parts specific to extended_return_statement:
1128 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1129 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1132 Expr := Expression (Obj_Decl);
1134 -- Note: The check for OK_For_Limited_Init will happen in
1135 -- Analyze_Object_Declaration; we treat it as a normal
1136 -- object declaration.
1138 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1141 Check_Return_Subtype_Indication (Obj_Decl);
1143 if Present (HSS) then
1146 if Present (Exception_Handlers (HSS)) then
1148 -- ???Has_Nested_Block_With_Handler needs to be set.
1149 -- Probably by creating an actual N_Block_Statement.
1150 -- Probably in Expand.
1156 -- Mark the return object as referenced, since the return is an
1157 -- implicit reference of the object.
1159 Set_Referenced (Defining_Identifier (Obj_Decl));
1161 Check_References (Stm_Entity);
1163 -- Check RM 6.5 (5.9/3)
1166 if Ada_Version < Ada_2012 then
1168 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1169 -- Can it really happen (extended return???)
1172 ("aliased only allowed for limited return objects "
1173 & "in Ada 2012??", N);
1175 elsif not Is_Limited_View (R_Type) then
1177 ("aliased only allowed for limited return objects", N);
1183 -- Case of Expr present
1187 -- Defend against previous errors
1189 and then Nkind (Expr) /= N_Empty
1190 and then Present (Etype (Expr))
1192 -- Apply constraint check. Note that this is done before the implicit
1193 -- conversion of the expression done for anonymous access types to
1194 -- ensure correct generation of the null-excluding check associated
1195 -- with null-excluding expressions found in return statements.
1197 Apply_Constraint_Check (Expr, R_Type);
1199 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1200 -- type, apply an implicit conversion of the expression to that type
1201 -- to force appropriate static and run-time accessibility checks.
1203 if Ada_Version >= Ada_2005
1204 and then Ekind (R_Type) = E_Anonymous_Access_Type
1206 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1207 Analyze_And_Resolve (Expr, R_Type);
1209 -- If this is a local anonymous access to subprogram, the
1210 -- accessibility check can be applied statically. The return is
1211 -- illegal if the access type of the return expression is declared
1212 -- inside of the subprogram (except if it is the subtype indication
1213 -- of an extended return statement).
1215 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1216 if not Comes_From_Source (Current_Scope)
1217 or else Ekind (Current_Scope) = E_Return_Statement
1222 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1224 Error_Msg_N ("cannot return local access to subprogram", N);
1227 -- The expression cannot be of a formal incomplete type
1229 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1230 and then Is_Generic_Type (Etype (Expr))
1233 ("cannot return expression of a formal incomplete type", N);
1236 -- If the result type is class-wide, then check that the return
1237 -- expression's type is not declared at a deeper level than the
1238 -- function (RM05-6.5(5.6/2)).
1240 if Ada_Version >= Ada_2005
1241 and then Is_Class_Wide_Type (R_Type)
1243 if Type_Access_Level (Etype (Expr)) >
1244 Subprogram_Access_Level (Scope_Id)
1247 ("level of return expression type is deeper than "
1248 & "class-wide function!", Expr);
1252 -- Check incorrect use of dynamically tagged expression
1254 if Is_Tagged_Type (R_Type) then
1255 Check_Dynamically_Tagged_Expression
1261 -- ??? A real run-time accessibility check is needed in cases
1262 -- involving dereferences of access parameters. For now we just
1263 -- check the static cases.
1265 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1266 and then Is_Limited_View (Etype (Scope_Id))
1267 and then Object_Access_Level (Expr) >
1268 Subprogram_Access_Level (Scope_Id)
1270 -- Suppress the message in a generic, where the rewriting
1273 if Inside_A_Generic then
1278 Make_Raise_Program_Error (Loc,
1279 Reason => PE_Accessibility_Check_Failed));
1282 Error_Msg_Warn := SPARK_Mode /= On;
1283 Error_Msg_N ("cannot return a local value by reference<<", N);
1284 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1288 if Known_Null (Expr)
1289 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1290 and then Null_Exclusion_Present (Parent (Scope_Id))
1292 Apply_Compile_Time_Constraint_Error
1294 Msg => "(Ada 2005) null not allowed for "
1295 & "null-excluding return??",
1296 Reason => CE_Null_Not_Allowed);
1299 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1300 -- has no initializing expression.
1302 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1303 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1304 Subprogram_Access_Level (Scope_Id)
1307 ("level of return expression type is deeper than "
1308 & "class-wide function!", Obj_Decl);
1311 end Analyze_Function_Return;
1313 -------------------------------------
1314 -- Analyze_Generic_Subprogram_Body --
1315 -------------------------------------
1317 procedure Analyze_Generic_Subprogram_Body
1321 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1322 Kind : constant Entity_Kind := Ekind (Gen_Id);
1323 Body_Id : Entity_Id;
1328 -- Copy body and disable expansion while analyzing the generic For a
1329 -- stub, do not copy the stub (which would load the proper body), this
1330 -- will be done when the proper body is analyzed.
1332 if Nkind (N) /= N_Subprogram_Body_Stub then
1333 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1336 -- Once the contents of the generic copy and the template are
1337 -- swapped, do the same for their respective aspect specifications.
1339 Exchange_Aspects (N, New_N);
1341 -- Collect all contract-related source pragmas found within the
1342 -- template and attach them to the contract of the subprogram body.
1343 -- This contract is used in the capture of global references within
1346 Create_Generic_Contract (N);
1351 Spec := Specification (N);
1353 -- Within the body of the generic, the subprogram is callable, and
1354 -- behaves like the corresponding non-generic unit.
1356 Body_Id := Defining_Entity (Spec);
1358 if Kind = E_Generic_Procedure
1359 and then Nkind (Spec) /= N_Procedure_Specification
1361 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1364 elsif Kind = E_Generic_Function
1365 and then Nkind (Spec) /= N_Function_Specification
1367 Error_Msg_N ("invalid body for generic function ", Body_Id);
1371 Set_Corresponding_Body (Gen_Decl, Body_Id);
1373 if Has_Completion (Gen_Id)
1374 and then Nkind (Parent (N)) /= N_Subunit
1376 Error_Msg_N ("duplicate generic body", N);
1379 Set_Has_Completion (Gen_Id);
1382 if Nkind (N) = N_Subprogram_Body_Stub then
1383 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1385 Set_Corresponding_Spec (N, Gen_Id);
1388 if Nkind (Parent (N)) = N_Compilation_Unit then
1389 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1392 -- Make generic parameters immediately visible in the body. They are
1393 -- needed to process the formals declarations. Then make the formals
1394 -- visible in a separate step.
1396 Push_Scope (Gen_Id);
1400 First_Ent : Entity_Id;
1403 First_Ent := First_Entity (Gen_Id);
1406 while Present (E) and then not Is_Formal (E) loop
1411 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1413 -- Now generic formals are visible, and the specification can be
1414 -- analyzed, for subsequent conformance check.
1416 Body_Id := Analyze_Subprogram_Specification (Spec);
1418 -- Make formal parameters visible
1422 -- E is the first formal parameter, we loop through the formals
1423 -- installing them so that they will be visible.
1425 Set_First_Entity (Gen_Id, E);
1426 while Present (E) loop
1432 -- Visible generic entity is callable within its own body
1434 Set_Ekind (Gen_Id, Ekind (Body_Id));
1435 Set_Ekind (Body_Id, E_Subprogram_Body);
1436 Set_Convention (Body_Id, Convention (Gen_Id));
1437 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1438 Set_Scope (Body_Id, Scope (Gen_Id));
1440 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1442 if Nkind (N) = N_Subprogram_Body_Stub then
1444 -- No body to analyze, so restore state of generic unit
1446 Set_Ekind (Gen_Id, Kind);
1447 Set_Ekind (Body_Id, Kind);
1449 if Present (First_Ent) then
1450 Set_First_Entity (Gen_Id, First_Ent);
1457 -- If this is a compilation unit, it must be made visible explicitly,
1458 -- because the compilation of the declaration, unlike other library
1459 -- unit declarations, does not. If it is not a unit, the following
1460 -- is redundant but harmless.
1462 Set_Is_Immediately_Visible (Gen_Id);
1463 Reference_Body_Formals (Gen_Id, Body_Id);
1465 if Is_Child_Unit (Gen_Id) then
1466 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1469 Set_Actual_Subtypes (N, Current_Scope);
1471 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1472 Set_SPARK_Pragma_Inherited (Body_Id);
1474 -- Analyze any aspect specifications that appear on the generic
1477 if Has_Aspects (N) then
1478 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1481 Analyze_Declarations (Declarations (N));
1484 -- Process the contract of the subprogram body after all declarations
1485 -- have been analyzed. This ensures that any contract-related pragmas
1486 -- are available through the N_Contract node of the body.
1488 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1490 Analyze (Handled_Statement_Sequence (N));
1491 Save_Global_References (Original_Node (N));
1493 -- Prior to exiting the scope, include generic formals again (if any
1494 -- are present) in the set of local entities.
1496 if Present (First_Ent) then
1497 Set_First_Entity (Gen_Id, First_Ent);
1500 Check_References (Gen_Id);
1503 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1504 Update_Use_Clause_Chain;
1506 Check_Subprogram_Order (N);
1508 -- Outside of its body, unit is generic again
1510 Set_Ekind (Gen_Id, Kind);
1511 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1514 Style.Check_Identifier (Body_Id, Gen_Id);
1518 end Analyze_Generic_Subprogram_Body;
1520 ----------------------------
1521 -- Analyze_Null_Procedure --
1522 ----------------------------
1524 procedure Analyze_Null_Procedure
1526 Is_Completion : out Boolean)
1528 Loc : constant Source_Ptr := Sloc (N);
1529 Spec : constant Node_Id := Specification (N);
1530 Designator : Entity_Id;
1532 Null_Body : Node_Id := Empty;
1533 Null_Stmt : Node_Id := Null_Statement (Spec);
1537 -- Capture the profile of the null procedure before analysis, for
1538 -- expansion at the freeze point and at each point of call. The body is
1539 -- used if the procedure has preconditions, or if it is a completion. In
1540 -- the first case the body is analyzed at the freeze point, in the other
1541 -- it replaces the null procedure declaration.
1543 -- For a null procedure that comes from source, a NULL statement is
1544 -- provided by the parser, which carries the source location of the
1545 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1546 -- from expansion, create one now.
1548 if No (Null_Stmt) then
1549 Null_Stmt := Make_Null_Statement (Loc);
1553 Make_Subprogram_Body (Loc,
1554 Specification => New_Copy_Tree (Spec),
1555 Declarations => New_List,
1556 Handled_Statement_Sequence =>
1557 Make_Handled_Sequence_Of_Statements (Loc,
1558 Statements => New_List (Null_Stmt)));
1560 -- Create new entities for body and formals
1562 Set_Defining_Unit_Name (Specification (Null_Body),
1563 Make_Defining_Identifier
1564 (Sloc (Defining_Entity (N)),
1565 Chars (Defining_Entity (N))));
1567 Form := First (Parameter_Specifications (Specification (Null_Body)));
1568 while Present (Form) loop
1569 Set_Defining_Identifier (Form,
1570 Make_Defining_Identifier
1571 (Sloc (Defining_Identifier (Form)),
1572 Chars (Defining_Identifier (Form))));
1576 -- Determine whether the null procedure may be a completion of a generic
1577 -- suprogram, in which case we use the new null body as the completion
1578 -- and set minimal semantic information on the original declaration,
1579 -- which is rewritten as a null statement.
1581 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1583 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1584 Insert_Before (N, Null_Body);
1585 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1587 Rewrite (N, Make_Null_Statement (Loc));
1588 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1589 Is_Completion := True;
1593 -- Resolve the types of the formals now, because the freeze point may
1594 -- appear in a different context, e.g. an instantiation.
1596 Form := First (Parameter_Specifications (Specification (Null_Body)));
1597 while Present (Form) loop
1598 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1599 Find_Type (Parameter_Type (Form));
1601 elsif No (Access_To_Subprogram_Definition
1602 (Parameter_Type (Form)))
1604 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1606 -- The case of a null procedure with a formal that is an
1607 -- access-to-subprogram type, and that is used as an actual
1608 -- in an instantiation is left to the enthusiastic reader.
1618 -- If there are previous overloadable entities with the same name, check
1619 -- whether any of them is completed by the null procedure.
1621 if Present (Prev) and then Is_Overloadable (Prev) then
1622 Designator := Analyze_Subprogram_Specification (Spec);
1623 Prev := Find_Corresponding_Spec (N);
1626 if No (Prev) or else not Comes_From_Source (Prev) then
1627 Designator := Analyze_Subprogram_Specification (Spec);
1628 Set_Has_Completion (Designator);
1630 -- Signal to caller that this is a procedure declaration
1632 Is_Completion := False;
1634 -- Null procedures are always inlined, but generic formal subprograms
1635 -- which appear as such in the internal instance of formal packages,
1636 -- need no completion and are not marked Inline.
1639 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1641 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1642 Set_Body_To_Inline (N, Null_Body);
1643 Set_Is_Inlined (Designator);
1647 -- The null procedure is a completion. We unconditionally rewrite
1648 -- this as a null body (even if expansion is not active), because
1649 -- there are various error checks that are applied on this body
1650 -- when it is analyzed (e.g. correct aspect placement).
1652 if Has_Completion (Prev) then
1653 Error_Msg_Sloc := Sloc (Prev);
1654 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1657 Check_Previous_Null_Procedure (N, Prev);
1659 Is_Completion := True;
1660 Rewrite (N, Null_Body);
1663 end Analyze_Null_Procedure;
1665 -----------------------------
1666 -- Analyze_Operator_Symbol --
1667 -----------------------------
1669 -- An operator symbol such as "+" or "and" may appear in context where the
1670 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1671 -- is just a string, as in (conjunction = "or"). In these cases the parser
1672 -- generates this node, and the semantics does the disambiguation. Other
1673 -- such case are actuals in an instantiation, the generic unit in an
1674 -- instantiation, and pragma arguments.
1676 procedure Analyze_Operator_Symbol (N : Node_Id) is
1677 Par : constant Node_Id := Parent (N);
1680 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1681 or else Nkind (Par) = N_Function_Instantiation
1682 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1683 or else (Nkind (Par) = N_Pragma_Argument_Association
1684 and then not Is_Pragma_String_Literal (Par))
1685 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1686 or else (Nkind (Par) = N_Attribute_Reference
1687 and then Attribute_Name (Par) /= Name_Value)
1689 Find_Direct_Name (N);
1692 Change_Operator_Symbol_To_String_Literal (N);
1695 end Analyze_Operator_Symbol;
1697 -----------------------------------
1698 -- Analyze_Parameter_Association --
1699 -----------------------------------
1701 procedure Analyze_Parameter_Association (N : Node_Id) is
1703 Analyze (Explicit_Actual_Parameter (N));
1704 end Analyze_Parameter_Association;
1706 ----------------------------
1707 -- Analyze_Procedure_Call --
1708 ----------------------------
1710 -- WARNING: This routine manages Ghost regions. Return statements must be
1711 -- replaced by gotos which jump to the end of the routine and restore the
1714 procedure Analyze_Procedure_Call (N : Node_Id) is
1715 procedure Analyze_Call_And_Resolve;
1716 -- Do Analyze and Resolve calls for procedure call. At the end, check
1717 -- for illegal order dependence.
1718 -- ??? where is the check for illegal order dependencies?
1720 ------------------------------
1721 -- Analyze_Call_And_Resolve --
1722 ------------------------------
1724 procedure Analyze_Call_And_Resolve is
1726 if Nkind (N) = N_Procedure_Call_Statement then
1728 Resolve (N, Standard_Void_Type);
1732 end Analyze_Call_And_Resolve;
1736 Actuals : constant List_Id := Parameter_Associations (N);
1737 Loc : constant Source_Ptr := Sloc (N);
1738 P : constant Node_Id := Name (N);
1740 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1741 -- Save the Ghost mode to restore on exit
1746 -- Start of processing for Analyze_Procedure_Call
1749 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1750 -- a procedure call or an entry call. The prefix may denote an access
1751 -- to subprogram type, in which case an implicit dereference applies.
1752 -- If the prefix is an indexed component (without implicit dereference)
1753 -- then the construct denotes a call to a member of an entire family.
1754 -- If the prefix is a simple name, it may still denote a call to a
1755 -- parameterless member of an entry family. Resolution of these various
1756 -- interpretations is delicate.
1758 -- Do not analyze machine code statements to avoid rejecting them in
1761 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1762 Set_Etype (P, Standard_Void_Type);
1767 -- If this is a call of the form Obj.Op, the call may have been analyzed
1768 -- and possibly rewritten into a block, in which case we are done.
1770 if Analyzed (N) then
1774 -- If there is an error analyzing the name (which may have been
1775 -- rewritten if the original call was in prefix notation) then error
1776 -- has been emitted already, mark node and return.
1778 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1779 Set_Etype (N, Any_Type);
1783 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1784 -- Set the mode now to ensure that any nodes generated during analysis
1785 -- and expansion are properly marked as Ghost.
1787 Mark_And_Set_Ghost_Procedure_Call (N);
1789 -- Otherwise analyze the parameters
1791 if Present (Actuals) then
1792 Actual := First (Actuals);
1794 while Present (Actual) loop
1796 Check_Parameterless_Call (Actual);
1801 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1803 if Nkind (P) = N_Attribute_Reference
1804 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1806 Name_Elab_Subp_Body)
1808 if Present (Actuals) then
1810 ("no parameters allowed for this call", First (Actuals));
1814 Set_Etype (N, Standard_Void_Type);
1817 elsif Is_Entity_Name (P)
1818 and then Is_Record_Type (Etype (Entity (P)))
1819 and then Remote_AST_I_Dereference (P)
1823 elsif Is_Entity_Name (P)
1824 and then Ekind (Entity (P)) /= E_Entry_Family
1826 if Is_Access_Type (Etype (P))
1827 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1828 and then No (Actuals)
1829 and then Comes_From_Source (N)
1831 Error_Msg_N ("missing explicit dereference in call", N);
1834 Analyze_Call_And_Resolve;
1836 -- If the prefix is the simple name of an entry family, this is a
1837 -- parameterless call from within the task body itself.
1839 elsif Is_Entity_Name (P)
1840 and then Nkind (P) = N_Identifier
1841 and then Ekind (Entity (P)) = E_Entry_Family
1842 and then Present (Actuals)
1843 and then No (Next (First (Actuals)))
1845 -- Can be call to parameterless entry family. What appears to be the
1846 -- sole argument is in fact the entry index. Rewrite prefix of node
1847 -- accordingly. Source representation is unchanged by this
1851 Make_Indexed_Component (Loc,
1853 Make_Selected_Component (Loc,
1854 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1855 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1856 Expressions => Actuals);
1857 Set_Name (N, New_N);
1858 Set_Etype (New_N, Standard_Void_Type);
1859 Set_Parameter_Associations (N, No_List);
1860 Analyze_Call_And_Resolve;
1862 elsif Nkind (P) = N_Explicit_Dereference then
1863 if Ekind (Etype (P)) = E_Subprogram_Type then
1864 Analyze_Call_And_Resolve;
1866 Error_Msg_N ("expect access to procedure in call", P);
1869 -- The name can be a selected component or an indexed component that
1870 -- yields an access to subprogram. Such a prefix is legal if the call
1871 -- has parameter associations.
1873 elsif Is_Access_Type (Etype (P))
1874 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1876 if Present (Actuals) then
1877 Analyze_Call_And_Resolve;
1879 Error_Msg_N ("missing explicit dereference in call ", N);
1882 -- If not an access to subprogram, then the prefix must resolve to the
1883 -- name of an entry, entry family, or protected operation.
1885 -- For the case of a simple entry call, P is a selected component where
1886 -- the prefix is the task and the selector name is the entry. A call to
1887 -- a protected procedure will have the same syntax. If the protected
1888 -- object contains overloaded operations, the entity may appear as a
1889 -- function, the context will select the operation whose type is Void.
1891 elsif Nkind (P) = N_Selected_Component
1892 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1896 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1897 -- in prefix notation may still be missing its controlling argument,
1898 -- so perform the transformation now.
1900 if SPARK_Mode = On and then In_Inlined_Body then
1902 Subp : constant Entity_Id := Entity (Selector_Name (P));
1903 Typ : constant Entity_Id := Etype (Prefix (P));
1906 if Is_Tagged_Type (Typ)
1907 and then Present (First_Formal (Subp))
1908 and then Etype (First_Formal (Subp)) = Typ
1909 and then Try_Object_Operation (P)
1914 Analyze_Call_And_Resolve;
1919 Analyze_Call_And_Resolve;
1922 elsif Nkind (P) = N_Selected_Component
1923 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1924 and then Present (Actuals)
1925 and then No (Next (First (Actuals)))
1927 -- Can be call to parameterless entry family. What appears to be the
1928 -- sole argument is in fact the entry index. Rewrite prefix of node
1929 -- accordingly. Source representation is unchanged by this
1933 Make_Indexed_Component (Loc,
1934 Prefix => New_Copy (P),
1935 Expressions => Actuals);
1936 Set_Name (N, New_N);
1937 Set_Etype (New_N, Standard_Void_Type);
1938 Set_Parameter_Associations (N, No_List);
1939 Analyze_Call_And_Resolve;
1941 -- For the case of a reference to an element of an entry family, P is
1942 -- an indexed component whose prefix is a selected component (task and
1943 -- entry family), and whose index is the entry family index.
1945 elsif Nkind (P) = N_Indexed_Component
1946 and then Nkind (Prefix (P)) = N_Selected_Component
1947 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1949 Analyze_Call_And_Resolve;
1951 -- If the prefix is the name of an entry family, it is a call from
1952 -- within the task body itself.
1954 elsif Nkind (P) = N_Indexed_Component
1955 and then Nkind (Prefix (P)) = N_Identifier
1956 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1959 Make_Selected_Component (Loc,
1960 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1961 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1962 Rewrite (Prefix (P), New_N);
1964 Analyze_Call_And_Resolve;
1966 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1967 -- procedure name, so the construct can only be a qualified expression.
1969 elsif Nkind (P) = N_Qualified_Expression
1970 and then Ada_Version >= Ada_2012
1972 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1975 -- Anything else is an error
1978 Error_Msg_N ("invalid procedure or entry call", N);
1982 Restore_Ghost_Mode (Saved_GM);
1983 end Analyze_Procedure_Call;
1985 ------------------------------
1986 -- Analyze_Return_Statement --
1987 ------------------------------
1989 procedure Analyze_Return_Statement (N : Node_Id) is
1990 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
1991 N_Simple_Return_Statement));
1993 Returns_Object : constant Boolean :=
1994 Nkind (N) = N_Extended_Return_Statement
1996 (Nkind (N) = N_Simple_Return_Statement
1997 and then Present (Expression (N)));
1998 -- True if we're returning something; that is, "return <expression>;"
1999 -- or "return Result : T [:= ...]". False for "return;". Used for error
2000 -- checking: If Returns_Object is True, N should apply to a function
2001 -- body; otherwise N should apply to a procedure body, entry body,
2002 -- accept statement, or extended return statement.
2004 function Find_What_It_Applies_To return Entity_Id;
2005 -- Find the entity representing the innermost enclosing body, accept
2006 -- statement, or extended return statement. If the result is a callable
2007 -- construct or extended return statement, then this will be the value
2008 -- of the Return_Applies_To attribute. Otherwise, the program is
2009 -- illegal. See RM-6.5(4/2).
2011 -----------------------------
2012 -- Find_What_It_Applies_To --
2013 -----------------------------
2015 function Find_What_It_Applies_To return Entity_Id is
2016 Result : Entity_Id := Empty;
2019 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2020 -- and postconditions.
2022 for J in reverse 0 .. Scope_Stack.Last loop
2023 Result := Scope_Stack.Table (J).Entity;
2024 exit when not Ekind_In (Result, E_Block, E_Loop)
2025 and then Chars (Result) /= Name_uPostconditions;
2028 pragma Assert (Present (Result));
2030 end Find_What_It_Applies_To;
2032 -- Local declarations
2034 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2035 Kind : constant Entity_Kind := Ekind (Scope_Id);
2036 Loc : constant Source_Ptr := Sloc (N);
2037 Stm_Entity : constant Entity_Id :=
2039 (E_Return_Statement, Current_Scope, Loc, 'R');
2041 -- Start of processing for Analyze_Return_Statement
2044 Set_Return_Statement_Entity (N, Stm_Entity);
2046 Set_Etype (Stm_Entity, Standard_Void_Type);
2047 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2049 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2050 -- (4/2): an inner return statement will apply to this extended return.
2052 if Nkind (N) = N_Extended_Return_Statement then
2053 Push_Scope (Stm_Entity);
2056 -- Check that pragma No_Return is obeyed. Don't complain about the
2057 -- implicitly-generated return that is placed at the end.
2059 if No_Return (Scope_Id) and then Comes_From_Source (N) then
2060 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
2063 -- Warn on any unassigned OUT parameters if in procedure
2065 if Ekind (Scope_Id) = E_Procedure then
2066 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2069 -- Check that functions return objects, and other things do not
2071 if Kind = E_Function or else Kind = E_Generic_Function then
2072 if not Returns_Object then
2073 Error_Msg_N ("missing expression in return from function", N);
2076 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
2077 if Returns_Object then
2078 Error_Msg_N ("procedure cannot return value (use function)", N);
2081 elsif Kind = E_Entry or else Kind = E_Entry_Family then
2082 if Returns_Object then
2083 if Is_Protected_Type (Scope (Scope_Id)) then
2084 Error_Msg_N ("entry body cannot return value", N);
2086 Error_Msg_N ("accept statement cannot return value", N);
2090 elsif Kind = E_Return_Statement then
2092 -- We are nested within another return statement, which must be an
2093 -- extended_return_statement.
2095 if Returns_Object then
2096 if Nkind (N) = N_Extended_Return_Statement then
2098 ("extended return statement cannot be nested (use `RETURN;`)",
2101 -- Case of a simple return statement with a value inside extended
2102 -- return statement.
2106 ("return nested in extended return statement cannot return "
2107 & "value (use `RETURN;`)", N);
2112 Error_Msg_N ("illegal context for return statement", N);
2115 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2116 Analyze_Function_Return (N);
2118 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2119 Set_Return_Present (Scope_Id);
2122 if Nkind (N) = N_Extended_Return_Statement then
2126 Kill_Current_Values (Last_Assignment_Only => True);
2127 Check_Unreachable_Code (N);
2129 Analyze_Dimension (N);
2130 end Analyze_Return_Statement;
2132 -------------------------------------
2133 -- Analyze_Simple_Return_Statement --
2134 -------------------------------------
2136 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2138 if Present (Expression (N)) then
2139 Mark_Coextensions (N, Expression (N));
2142 Analyze_Return_Statement (N);
2143 end Analyze_Simple_Return_Statement;
2145 -------------------------
2146 -- Analyze_Return_Type --
2147 -------------------------
2149 procedure Analyze_Return_Type (N : Node_Id) is
2150 Designator : constant Entity_Id := Defining_Entity (N);
2151 Typ : Entity_Id := Empty;
2154 -- Normal case where result definition does not indicate an error
2156 if Result_Definition (N) /= Error then
2157 if Nkind (Result_Definition (N)) = N_Access_Definition then
2158 Check_SPARK_05_Restriction
2159 ("access result is not allowed", Result_Definition (N));
2161 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2164 AD : constant Node_Id :=
2165 Access_To_Subprogram_Definition (Result_Definition (N));
2167 if Present (AD) and then Protected_Present (AD) then
2168 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2170 Typ := Access_Definition (N, Result_Definition (N));
2174 Set_Parent (Typ, Result_Definition (N));
2175 Set_Is_Local_Anonymous_Access (Typ);
2176 Set_Etype (Designator, Typ);
2178 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2180 Null_Exclusion_Static_Checks (N);
2182 -- Subtype_Mark case
2185 Find_Type (Result_Definition (N));
2186 Typ := Entity (Result_Definition (N));
2187 Set_Etype (Designator, Typ);
2189 -- Unconstrained array as result is not allowed in SPARK
2191 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2192 Check_SPARK_05_Restriction
2193 ("returning an unconstrained array is not allowed",
2194 Result_Definition (N));
2197 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2199 Null_Exclusion_Static_Checks (N);
2201 -- If a null exclusion is imposed on the result type, then create
2202 -- a null-excluding itype (an access subtype) and use it as the
2203 -- function's Etype. Note that the null exclusion checks are done
2204 -- right before this, because they don't get applied to types that
2205 -- do not come from source.
2207 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2208 Set_Etype (Designator,
2209 Create_Null_Excluding_Itype
2212 Scope_Id => Scope (Current_Scope)));
2214 -- The new subtype must be elaborated before use because
2215 -- it is visible outside of the function. However its base
2216 -- type may not be frozen yet, so the reference that will
2217 -- force elaboration must be attached to the freezing of
2220 -- If the return specification appears on a proper body,
2221 -- the subtype will have been created already on the spec.
2223 if Is_Frozen (Typ) then
2224 if Nkind (Parent (N)) = N_Subprogram_Body
2225 and then Nkind (Parent (Parent (N))) = N_Subunit
2229 Build_Itype_Reference (Etype (Designator), Parent (N));
2233 Ensure_Freeze_Node (Typ);
2236 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2238 Set_Itype (IR, Etype (Designator));
2239 Append_Freeze_Actions (Typ, New_List (IR));
2244 Set_Etype (Designator, Typ);
2247 if Ekind (Typ) = E_Incomplete_Type
2248 or else (Is_Class_Wide_Type (Typ)
2249 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2251 -- AI05-0151: Tagged incomplete types are allowed in all formal
2252 -- parts. Untagged incomplete types are not allowed in bodies.
2253 -- As a consequence, limited views cannot appear in a basic
2254 -- declaration that is itself within a body, because there is
2255 -- no point at which the non-limited view will become visible.
2257 if Ada_Version >= Ada_2012 then
2258 if From_Limited_With (Typ) and then In_Package_Body then
2260 ("invalid use of incomplete type&",
2261 Result_Definition (N), Typ);
2263 -- The return type of a subprogram body cannot be of a
2264 -- formal incomplete type.
2266 elsif Is_Generic_Type (Typ)
2267 and then Nkind (Parent (N)) = N_Subprogram_Body
2270 ("return type cannot be a formal incomplete type",
2271 Result_Definition (N));
2273 elsif Is_Class_Wide_Type (Typ)
2274 and then Is_Generic_Type (Root_Type (Typ))
2275 and then Nkind (Parent (N)) = N_Subprogram_Body
2278 ("return type cannot be a formal incomplete type",
2279 Result_Definition (N));
2281 elsif Is_Tagged_Type (Typ) then
2284 -- Use is legal in a thunk generated for an operation
2285 -- inherited from a progenitor.
2287 elsif Is_Thunk (Designator)
2288 and then Present (Non_Limited_View (Typ))
2292 elsif Nkind (Parent (N)) = N_Subprogram_Body
2293 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2297 ("invalid use of untagged incomplete type&",
2301 -- The type must be completed in the current package. This
2302 -- is checked at the end of the package declaration when
2303 -- Taft-amendment types are identified. If the return type
2304 -- is class-wide, there is no required check, the type can
2305 -- be a bona fide TAT.
2307 if Ekind (Scope (Current_Scope)) = E_Package
2308 and then In_Private_Part (Scope (Current_Scope))
2309 and then not Is_Class_Wide_Type (Typ)
2311 Append_Elmt (Designator, Private_Dependents (Typ));
2316 ("invalid use of incomplete type&", Designator, Typ);
2321 -- Case where result definition does indicate an error
2324 Set_Etype (Designator, Any_Type);
2326 end Analyze_Return_Type;
2328 -----------------------------
2329 -- Analyze_Subprogram_Body --
2330 -----------------------------
2332 procedure Analyze_Subprogram_Body (N : Node_Id) is
2333 Loc : constant Source_Ptr := Sloc (N);
2334 Body_Spec : constant Node_Id := Specification (N);
2335 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2338 if Debug_Flag_C then
2339 Write_Str ("==> subprogram body ");
2340 Write_Name (Chars (Body_Id));
2341 Write_Str (" from ");
2342 Write_Location (Loc);
2347 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2349 -- The real work is split out into the helper, so it can do "return;"
2350 -- without skipping the debug output:
2352 Analyze_Subprogram_Body_Helper (N);
2354 if Debug_Flag_C then
2356 Write_Str ("<== subprogram body ");
2357 Write_Name (Chars (Body_Id));
2358 Write_Str (" from ");
2359 Write_Location (Loc);
2362 end Analyze_Subprogram_Body;
2364 ------------------------------------
2365 -- Analyze_Subprogram_Body_Helper --
2366 ------------------------------------
2368 -- This procedure is called for regular subprogram bodies, generic bodies,
2369 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2370 -- specification matters, and is used to create a proper declaration for
2371 -- the subprogram, or to perform conformance checks.
2373 -- WARNING: This routine manages Ghost regions. Return statements must be
2374 -- replaced by gotos which jump to the end of the routine and restore the
2377 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2378 Body_Spec : Node_Id := Specification (N);
2379 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2380 Loc : constant Source_Ptr := Sloc (N);
2381 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2383 Conformant : Boolean;
2384 Desig_View : Entity_Id := Empty;
2385 Exch_Views : Elist_Id := No_Elist;
2387 Mask_Types : Elist_Id := No_Elist;
2388 Prot_Typ : Entity_Id := Empty;
2389 Spec_Decl : Node_Id := Empty;
2390 Spec_Id : Entity_Id;
2392 Last_Real_Spec_Entity : Entity_Id := Empty;
2393 -- When we analyze a separate spec, the entity chain ends up containing
2394 -- the formals, as well as any itypes generated during analysis of the
2395 -- default expressions for parameters, or the arguments of associated
2396 -- precondition/postcondition pragmas (which are analyzed in the context
2397 -- of the spec since they have visibility on formals).
2399 -- These entities belong with the spec and not the body. However we do
2400 -- the analysis of the body in the context of the spec (again to obtain
2401 -- visibility to the formals), and all the entities generated during
2402 -- this analysis end up also chained to the entity chain of the spec.
2403 -- But they really belong to the body, and there is circuitry to move
2404 -- them from the spec to the body.
2406 -- However, when we do this move, we don't want to move the real spec
2407 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2408 -- variable points to the last real spec entity, so we only move those
2409 -- chained beyond that point. It is initialized to Empty to deal with
2410 -- the case where there is no separate spec.
2412 function Body_Has_Contract return Boolean;
2413 -- Check whether unanalyzed body has an aspect or pragma that may
2414 -- generate a SPARK contract.
2416 function Body_Has_SPARK_Mode_On return Boolean;
2417 -- Check whether SPARK_Mode On applies to the subprogram body, either
2418 -- because it is specified directly on the body, or because it is
2419 -- inherited from the enclosing subprogram or package.
2421 procedure Build_Subprogram_Declaration;
2422 -- Create a matching subprogram declaration for subprogram body N
2424 procedure Check_Anonymous_Return;
2425 -- Ada 2005: if a function returns an access type that denotes a task,
2426 -- or a type that contains tasks, we must create a master entity for
2427 -- the anonymous type, which typically will be used in an allocator
2428 -- in the body of the function.
2430 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2431 -- Look ahead to recognize a pragma that may appear after the body.
2432 -- If there is a previous spec, check that it appears in the same
2433 -- declarative part. If the pragma is Inline_Always, perform inlining
2434 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2435 -- If the body acts as a spec, and inlining is required, we create a
2436 -- subprogram declaration for it, in order to attach the body to inline.
2437 -- If pragma does not appear after the body, check whether there is
2438 -- an inline pragma before any local declarations.
2440 procedure Check_Missing_Return;
2441 -- Checks for a function with a no return statements, and also performs
2442 -- the warning checks implemented by Check_Returns. In formal mode, also
2443 -- verify that a function ends with a RETURN and that a procedure does
2444 -- not contain any RETURN.
2446 function Disambiguate_Spec return Entity_Id;
2447 -- When a primitive is declared between the private view and the full
2448 -- view of a concurrent type which implements an interface, a special
2449 -- mechanism is used to find the corresponding spec of the primitive
2452 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2453 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2454 -- incomplete types coming from a limited context and replace their
2455 -- limited views with the non-limited ones. Return the list of changes
2456 -- to be used to undo the transformation.
2458 function Is_Private_Concurrent_Primitive
2459 (Subp_Id : Entity_Id) return Boolean;
2460 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2461 -- type that implements an interface and has a private view.
2463 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2464 -- N is the body generated for an expression function that is not a
2465 -- completion and Spec_Id the defining entity of its spec. Mark all
2466 -- the not-yet-frozen types referenced by the simple return statement
2467 -- of the function as formally frozen.
2469 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2470 -- Undo the transformation done by Exchange_Limited_Views.
2472 procedure Set_Trivial_Subprogram (N : Node_Id);
2473 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2474 -- subprogram whose body is being analyzed. N is the statement node
2475 -- causing the flag to be set, if the following statement is a return
2476 -- of an entity, we mark the entity as set in source to suppress any
2477 -- warning on the stylized use of function stubs with a dummy return.
2479 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2480 -- Undo the transformation done by Mask_Unfrozen_Types
2482 procedure Verify_Overriding_Indicator;
2483 -- If there was a previous spec, the entity has been entered in the
2484 -- current scope previously. If the body itself carries an overriding
2485 -- indicator, check that it is consistent with the known status of the
2488 -----------------------
2489 -- Body_Has_Contract --
2490 -----------------------
2492 function Body_Has_Contract return Boolean is
2493 Decls : constant List_Id := Declarations (N);
2497 -- Check for aspects that may generate a contract
2499 if Present (Aspect_Specifications (N)) then
2500 Item := First (Aspect_Specifications (N));
2501 while Present (Item) loop
2502 if Is_Subprogram_Contract_Annotation (Item) then
2510 -- Check for pragmas that may generate a contract
2512 if Present (Decls) then
2513 Item := First (Decls);
2514 while Present (Item) loop
2515 if Nkind (Item) = N_Pragma
2516 and then Is_Subprogram_Contract_Annotation (Item)
2526 end Body_Has_Contract;
2528 ----------------------------
2529 -- Body_Has_SPARK_Mode_On --
2530 ----------------------------
2532 function Body_Has_SPARK_Mode_On return Boolean is
2533 Decls : constant List_Id := Declarations (N);
2537 -- Check for SPARK_Mode aspect
2539 if Present (Aspect_Specifications (N)) then
2540 Item := First (Aspect_Specifications (N));
2541 while Present (Item) loop
2542 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2543 return Get_SPARK_Mode_From_Annotation (Item) = On;
2550 -- Check for SPARK_Mode pragma
2552 if Present (Decls) then
2553 Item := First (Decls);
2554 while Present (Item) loop
2556 -- Pragmas that apply to a subprogram body are usually grouped
2557 -- together. Look for a potential pragma SPARK_Mode among them.
2559 if Nkind (Item) = N_Pragma then
2560 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2561 return Get_SPARK_Mode_From_Annotation (Item) = On;
2564 -- Otherwise the first non-pragma declarative item terminates
2565 -- the region where pragma SPARK_Mode may appear.
2575 -- Otherwise, the applicable SPARK_Mode is inherited from the
2576 -- enclosing subprogram or package.
2578 return SPARK_Mode = On;
2579 end Body_Has_SPARK_Mode_On;
2581 ----------------------------------
2582 -- Build_Subprogram_Declaration --
2583 ----------------------------------
2585 procedure Build_Subprogram_Declaration is
2586 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2587 -- Relocate certain categorization pragmas from the declarative list
2588 -- of subprogram body From and insert them after node To. The pragmas
2591 -- Volatile_Function
2592 -- Also copy pragma SPARK_Mode if present in the declarative list
2593 -- of subprogram body From and insert it after node To. This pragma
2594 -- should not be moved, as it applies to the body too.
2600 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2602 Next_Decl : Node_Id;
2605 pragma Assert (Nkind (From) = N_Subprogram_Body);
2607 -- The destination node must be part of a list, as the pragmas are
2608 -- inserted after it.
2610 pragma Assert (Is_List_Member (To));
2612 -- Inspect the declarations of the subprogram body looking for
2613 -- specific pragmas.
2615 Decl := First (Declarations (N));
2616 while Present (Decl) loop
2617 Next_Decl := Next (Decl);
2619 if Nkind (Decl) = N_Pragma then
2620 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
2621 Insert_After (To, New_Copy_Tree (Decl));
2623 elsif Nam_In (Pragma_Name_Unmapped (Decl),
2625 Name_Volatile_Function)
2628 Insert_After (To, Decl);
2639 Subp_Decl : Node_Id;
2641 -- Start of processing for Build_Subprogram_Declaration
2644 -- Create a matching subprogram spec using the profile of the body.
2645 -- The structure of the tree is identical, but has new entities for
2646 -- the defining unit name and formal parameters.
2649 Make_Subprogram_Declaration (Loc,
2650 Specification => Copy_Subprogram_Spec (Body_Spec));
2651 Set_Comes_From_Source (Subp_Decl, True);
2653 -- Relocate the aspects and relevant pragmas from the subprogram body
2654 -- to the generated spec because it acts as the initial declaration.
2656 Insert_Before (N, Subp_Decl);
2657 Move_Aspects (N, To => Subp_Decl);
2658 Move_Pragmas (N, To => Subp_Decl);
2660 -- Ensure that the generated corresponding spec and original body
2661 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2662 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2663 -- correctly set for local subprograms.
2665 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2667 Analyze (Subp_Decl);
2669 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2670 -- the body since the expander may generate calls using that entity.
2671 -- Required to ensure that Expand_Call rewrites calls to this
2672 -- function by calls to the built procedure.
2674 if Modify_Tree_For_C
2675 and then Nkind (Body_Spec) = N_Function_Specification
2677 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2679 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2680 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2681 Corresponding_Procedure
2682 (Defining_Entity (Specification (Subp_Decl))));
2685 -- Analyze any relocated source pragmas or pragmas created for aspect
2688 Decl := Next (Subp_Decl);
2689 while Present (Decl) loop
2691 -- Stop the search for pragmas once the body has been reached as
2692 -- this terminates the region where pragmas may appear.
2697 elsif Nkind (Decl) = N_Pragma then
2704 Spec_Id := Defining_Entity (Subp_Decl);
2705 Set_Corresponding_Spec (N, Spec_Id);
2707 -- Mark the generated spec as a source construct to ensure that all
2708 -- calls to it are properly registered in ALI files for GNATprove.
2710 Set_Comes_From_Source (Spec_Id, True);
2712 -- Ensure that the specs of the subprogram declaration and its body
2713 -- are identical, otherwise they will appear non-conformant due to
2714 -- rewritings in the default values of formal parameters.
2716 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2717 Set_Specification (N, Body_Spec);
2718 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2719 end Build_Subprogram_Declaration;
2721 ----------------------------
2722 -- Check_Anonymous_Return --
2723 ----------------------------
2725 procedure Check_Anonymous_Return is
2731 if Present (Spec_Id) then
2737 if Ekind (Scop) = E_Function
2738 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2739 and then not Is_Thunk (Scop)
2741 -- Skip internally built functions which handle the case of
2742 -- a null access (see Expand_Interface_Conversion)
2744 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2745 and then not Comes_From_Source (Parent (Scop)))
2747 and then (Has_Task (Designated_Type (Etype (Scop)))
2749 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2751 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2752 and then Expander_Active
2754 -- Avoid cases with no tasking support
2756 and then RTE_Available (RE_Current_Master)
2757 and then not Restriction_Active (No_Task_Hierarchy)
2760 Make_Object_Declaration (Loc,
2761 Defining_Identifier =>
2762 Make_Defining_Identifier (Loc, Name_uMaster),
2763 Constant_Present => True,
2764 Object_Definition =>
2765 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2767 Make_Explicit_Dereference (Loc,
2768 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2770 if Present (Declarations (N)) then
2771 Prepend (Decl, Declarations (N));
2773 Set_Declarations (N, New_List (Decl));
2776 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2777 Set_Has_Master_Entity (Scop);
2779 -- Now mark the containing scope as a task master
2782 while Nkind (Par) /= N_Compilation_Unit loop
2783 Par := Parent (Par);
2784 pragma Assert (Present (Par));
2786 -- If we fall off the top, we are at the outer level, and
2787 -- the environment task is our effective master, so nothing
2791 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2793 Set_Is_Task_Master (Par, True);
2798 end Check_Anonymous_Return;
2800 -------------------------
2801 -- Check_Inline_Pragma --
2802 -------------------------
2804 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2808 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2809 -- True when N is a pragma Inline or Inline_Always that applies
2810 -- to this subprogram.
2812 -----------------------
2813 -- Is_Inline_Pragma --
2814 -----------------------
2816 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2818 if Nkind (N) = N_Pragma
2820 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2821 or else (Pragma_Name_Unmapped (N) = Name_Inline
2823 (Front_End_Inlining or else Optimization_Level > 0)))
2824 and then Present (Pragma_Argument_Associations (N))
2827 Pragma_Arg : Node_Id :=
2828 Expression (First (Pragma_Argument_Associations (N)));
2830 if Nkind (Pragma_Arg) = N_Selected_Component then
2831 Pragma_Arg := Selector_Name (Pragma_Arg);
2834 return Chars (Pragma_Arg) = Chars (Body_Id);
2840 end Is_Inline_Pragma;
2842 -- Start of processing for Check_Inline_Pragma
2845 if not Expander_Active then
2849 if Is_List_Member (N)
2850 and then Present (Next (N))
2851 and then Is_Inline_Pragma (Next (N))
2855 elsif Nkind (N) /= N_Subprogram_Body_Stub
2856 and then Present (Declarations (N))
2857 and then Is_Inline_Pragma (First (Declarations (N)))
2859 Prag := First (Declarations (N));
2865 if Present (Prag) then
2866 if Present (Spec_Id) then
2867 if Is_List_Member (N)
2868 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2869 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2875 -- Create a subprogram declaration, to make treatment uniform.
2876 -- Make the sloc of the subprogram name that of the entity in
2877 -- the body, so that style checks find identical strings.
2880 Subp : constant Entity_Id :=
2881 Make_Defining_Identifier
2882 (Sloc (Body_Id), Chars (Body_Id));
2883 Decl : constant Node_Id :=
2884 Make_Subprogram_Declaration (Loc,
2886 New_Copy_Tree (Specification (N)));
2889 -- Link the body and the generated spec
2891 Set_Corresponding_Body (Decl, Body_Id);
2892 Set_Corresponding_Spec (N, Subp);
2894 Set_Defining_Unit_Name (Specification (Decl), Subp);
2896 -- To ensure proper coverage when body is inlined, indicate
2897 -- whether the subprogram comes from source.
2899 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2901 if Present (First_Formal (Body_Id)) then
2902 Plist := Copy_Parameter_List (Body_Id);
2903 Set_Parameter_Specifications
2904 (Specification (Decl), Plist);
2907 -- Move aspects to the new spec
2909 if Has_Aspects (N) then
2910 Move_Aspects (N, To => Decl);
2913 Insert_Before (N, Decl);
2916 Set_Has_Pragma_Inline (Subp);
2918 if Pragma_Name (Prag) = Name_Inline_Always then
2919 Set_Is_Inlined (Subp);
2920 Set_Has_Pragma_Inline_Always (Subp);
2923 -- Prior to copying the subprogram body to create a template
2924 -- for it for subsequent inlining, remove the pragma from
2925 -- the current body so that the copy that will produce the
2926 -- new body will start from a completely unanalyzed tree.
2928 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2929 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2936 end Check_Inline_Pragma;
2938 --------------------------
2939 -- Check_Missing_Return --
2940 --------------------------
2942 procedure Check_Missing_Return is
2944 Missing_Ret : Boolean;
2947 if Nkind (Body_Spec) = N_Function_Specification then
2948 if Present (Spec_Id) then
2954 if Return_Present (Id) then
2955 Check_Returns (HSS, 'F', Missing_Ret);
2958 Set_Has_Missing_Return (Id);
2961 -- Within a premature instantiation of a package with no body, we
2962 -- build completions of the functions therein, with a Raise
2963 -- statement. No point in complaining about a missing return in
2966 elsif Ekind (Id) = E_Function
2967 and then In_Instance
2968 and then Present (Statements (HSS))
2969 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2973 elsif Is_Generic_Subprogram (Id)
2974 or else not Is_Machine_Code_Subprogram (Id)
2976 Error_Msg_N ("missing RETURN statement in function body", N);
2979 -- If procedure with No_Return, check returns
2981 elsif Nkind (Body_Spec) = N_Procedure_Specification
2982 and then Present (Spec_Id)
2983 and then No_Return (Spec_Id)
2985 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2988 -- Special checks in SPARK mode
2990 if Nkind (Body_Spec) = N_Function_Specification then
2992 -- In SPARK mode, last statement of a function should be a return
2995 Stat : constant Node_Id := Last_Source_Statement (HSS);
2998 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2999 N_Extended_Return_Statement)
3001 Check_SPARK_05_Restriction
3002 ("last statement in function should be RETURN", Stat);
3006 -- In SPARK mode, verify that a procedure has no return
3008 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3009 if Present (Spec_Id) then
3015 -- Would be nice to point to return statement here, can we
3016 -- borrow the Check_Returns procedure here ???
3018 if Return_Present (Id) then
3019 Check_SPARK_05_Restriction
3020 ("procedure should not have RETURN", N);
3023 end Check_Missing_Return;
3025 -----------------------
3026 -- Disambiguate_Spec --
3027 -----------------------
3029 function Disambiguate_Spec return Entity_Id is
3030 Priv_Spec : Entity_Id;
3033 procedure Replace_Types (To_Corresponding : Boolean);
3034 -- Depending on the flag, replace the type of formal parameters of
3035 -- Body_Id if it is a concurrent type implementing interfaces with
3036 -- the corresponding record type or the other way around.
3038 procedure Replace_Types (To_Corresponding : Boolean) is
3040 Formal_Typ : Entity_Id;
3043 Formal := First_Formal (Body_Id);
3044 while Present (Formal) loop
3045 Formal_Typ := Etype (Formal);
3047 if Is_Class_Wide_Type (Formal_Typ) then
3048 Formal_Typ := Root_Type (Formal_Typ);
3051 -- From concurrent type to corresponding record
3053 if To_Corresponding then
3054 if Is_Concurrent_Type (Formal_Typ)
3055 and then Present (Corresponding_Record_Type (Formal_Typ))
3058 (Corresponding_Record_Type (Formal_Typ)))
3061 Corresponding_Record_Type (Formal_Typ));
3064 -- From corresponding record to concurrent type
3067 if Is_Concurrent_Record_Type (Formal_Typ)
3068 and then Present (Interfaces (Formal_Typ))
3071 Corresponding_Concurrent_Type (Formal_Typ));
3075 Next_Formal (Formal);
3079 -- Start of processing for Disambiguate_Spec
3082 -- Try to retrieve the specification of the body as is. All error
3083 -- messages are suppressed because the body may not have a spec in
3084 -- its current state.
3086 Spec_N := Find_Corresponding_Spec (N, False);
3088 -- It is possible that this is the body of a primitive declared
3089 -- between a private and a full view of a concurrent type. The
3090 -- controlling parameter of the spec carries the concurrent type,
3091 -- not the corresponding record type as transformed by Analyze_
3092 -- Subprogram_Specification. In such cases, we undo the change
3093 -- made by the analysis of the specification and try to find the
3096 -- Note that wrappers already have their corresponding specs and
3097 -- bodies set during their creation, so if the candidate spec is
3098 -- a wrapper, then we definitely need to swap all types to their
3099 -- original concurrent status.
3102 or else Is_Primitive_Wrapper (Spec_N)
3104 -- Restore all references of corresponding record types to the
3105 -- original concurrent types.
3107 Replace_Types (To_Corresponding => False);
3108 Priv_Spec := Find_Corresponding_Spec (N, False);
3110 -- The current body truly belongs to a primitive declared between
3111 -- a private and a full view. We leave the modified body as is,
3112 -- and return the true spec.
3114 if Present (Priv_Spec)
3115 and then Is_Private_Primitive (Priv_Spec)
3120 -- In case that this is some sort of error, restore the original
3121 -- state of the body.
3123 Replace_Types (To_Corresponding => True);
3127 end Disambiguate_Spec;
3129 ----------------------------
3130 -- Exchange_Limited_Views --
3131 ----------------------------
3133 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3134 Result : Elist_Id := No_Elist;
3136 procedure Detect_And_Exchange (Id : Entity_Id);
3137 -- Determine whether Id's type denotes an incomplete type associated
3138 -- with a limited with clause and exchange the limited view with the
3139 -- non-limited one when available. Note that the non-limited view
3140 -- may exist because of a with_clause in another unit in the context,
3141 -- but cannot be used because the current view of the enclosing unit
3142 -- is still a limited view.
3144 -------------------------
3145 -- Detect_And_Exchange --
3146 -------------------------
3148 procedure Detect_And_Exchange (Id : Entity_Id) is
3149 Typ : constant Entity_Id := Etype (Id);
3151 if From_Limited_With (Typ)
3152 and then Has_Non_Limited_View (Typ)
3153 and then not From_Limited_With (Scope (Typ))
3156 Result := New_Elmt_List;
3159 Prepend_Elmt (Typ, Result);
3160 Prepend_Elmt (Id, Result);
3161 Set_Etype (Id, Non_Limited_View (Typ));
3163 end Detect_And_Exchange;
3169 -- Start of processing for Exchange_Limited_Views
3172 -- Do not process subprogram bodies as they already use the non-
3173 -- limited view of types.
3175 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3179 -- Examine all formals and swap views when applicable
3181 Formal := First_Formal (Subp_Id);
3182 while Present (Formal) loop
3183 Detect_And_Exchange (Formal);
3185 Next_Formal (Formal);
3188 -- Process the return type of a function
3190 if Ekind (Subp_Id) = E_Function then
3191 Detect_And_Exchange (Subp_Id);
3195 end Exchange_Limited_Views;
3197 -------------------------------------
3198 -- Is_Private_Concurrent_Primitive --
3199 -------------------------------------
3201 function Is_Private_Concurrent_Primitive
3202 (Subp_Id : Entity_Id) return Boolean
3204 Formal_Typ : Entity_Id;
3207 if Present (First_Formal (Subp_Id)) then
3208 Formal_Typ := Etype (First_Formal (Subp_Id));
3210 if Is_Concurrent_Record_Type (Formal_Typ) then
3211 if Is_Class_Wide_Type (Formal_Typ) then
3212 Formal_Typ := Root_Type (Formal_Typ);
3215 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3218 -- The type of the first formal is a concurrent tagged type with
3222 Is_Concurrent_Type (Formal_Typ)
3223 and then Is_Tagged_Type (Formal_Typ)
3224 and then Has_Private_Declaration (Formal_Typ);
3228 end Is_Private_Concurrent_Primitive;
3230 -------------------------
3231 -- Mask_Unfrozen_Types --
3232 -------------------------
3234 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3235 Result : Elist_Id := No_Elist;
3237 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3238 -- Mask all types referenced in the subtree rooted at Node
3240 --------------------
3241 -- Mask_Type_Refs --
3242 --------------------
3244 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3245 procedure Mask_Type (Typ : Entity_Id);
3246 -- ??? what does this do?
3252 procedure Mask_Type (Typ : Entity_Id) is
3254 -- Skip Itypes created by the preanalysis
3257 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3262 if not Is_Frozen (Typ) then
3263 Set_Is_Frozen (Typ);
3264 Append_New_Elmt (Typ, Result);
3268 -- Start of processing for Mask_Type_Refs
3271 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3272 Mask_Type (Etype (Entity (Node)));
3274 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3275 Mask_Type (Scope (Entity (Node)));
3278 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3279 and then Present (Etype (Node))
3281 Mask_Type (Etype (Node));
3287 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3291 Return_Stmt : constant Node_Id :=
3292 First (Statements (Handled_Statement_Sequence (N)));
3294 -- Start of processing for Mask_Unfrozen_Types
3297 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3299 Mask_References (Expression (Return_Stmt));
3302 end Mask_Unfrozen_Types;
3304 ---------------------------
3305 -- Restore_Limited_Views --
3306 ---------------------------
3308 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3309 Elmt : Elmt_Id := First_Elmt (Restore_List);
3313 while Present (Elmt) loop
3316 Set_Etype (Id, Node (Elmt));
3319 end Restore_Limited_Views;
3321 ----------------------------
3322 -- Set_Trivial_Subprogram --
3323 ----------------------------
3325 procedure Set_Trivial_Subprogram (N : Node_Id) is
3326 Nxt : constant Node_Id := Next (N);
3329 Set_Is_Trivial_Subprogram (Body_Id);
3331 if Present (Spec_Id) then
3332 Set_Is_Trivial_Subprogram (Spec_Id);
3336 and then Nkind (Nxt) = N_Simple_Return_Statement
3337 and then No (Next (Nxt))
3338 and then Present (Expression (Nxt))
3339 and then Is_Entity_Name (Expression (Nxt))
3341 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3343 end Set_Trivial_Subprogram;
3345 ---------------------------
3346 -- Unmask_Unfrozen_Types --
3347 ---------------------------
3349 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3350 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3353 while Present (Elmt) loop
3354 Set_Is_Frozen (Node (Elmt), False);
3357 end Unmask_Unfrozen_Types;
3359 ---------------------------------
3360 -- Verify_Overriding_Indicator --
3361 ---------------------------------
3363 procedure Verify_Overriding_Indicator is
3365 if Must_Override (Body_Spec) then
3366 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3367 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3371 elsif not Present (Overridden_Operation (Spec_Id)) then
3373 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3375 -- Overriding indicators aren't allowed for protected subprogram
3376 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3377 -- this to a warning if -gnatd.E is enabled.
3379 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3380 Error_Msg_Warn := Error_To_Warning;
3382 ("<<overriding indicator not allowed for protected "
3383 & "subprogram body", Body_Spec);
3386 elsif Must_Not_Override (Body_Spec) then
3387 if Present (Overridden_Operation (Spec_Id)) then
3389 ("subprogram& overrides inherited operation",
3390 Body_Spec, Spec_Id);
3392 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3393 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3396 ("subprogram& overrides predefined operator ",
3397 Body_Spec, Spec_Id);
3399 -- Overriding indicators aren't allowed for protected subprogram
3400 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3401 -- this to a warning if -gnatd.E is enabled.
3403 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3404 Error_Msg_Warn := Error_To_Warning;
3407 ("<<overriding indicator not allowed "
3408 & "for protected subprogram body", Body_Spec);
3410 -- If this is not a primitive operation, then the overriding
3411 -- indicator is altogether illegal.
3413 elsif not Is_Primitive (Spec_Id) then
3415 ("overriding indicator only allowed "
3416 & "if subprogram is primitive", Body_Spec);
3419 -- If checking the style rule and the operation overrides, then
3420 -- issue a warning about a missing overriding_indicator. Protected
3421 -- subprogram bodies are excluded from this style checking, since
3422 -- they aren't primitives (even though their declarations can
3423 -- override) and aren't allowed to have an overriding_indicator.
3426 and then Present (Overridden_Operation (Spec_Id))
3427 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3429 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3430 Style.Missing_Overriding (N, Body_Id);
3433 and then Can_Override_Operator (Spec_Id)
3434 and then not In_Predefined_Unit (Spec_Id)
3436 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3437 Style.Missing_Overriding (N, Body_Id);
3439 end Verify_Overriding_Indicator;
3443 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3444 Saved_ISMP : constant Boolean :=
3445 Ignore_SPARK_Mode_Pragmas_In_Instance;
3446 -- Save the Ghost and SPARK mode-related data to restore on exit
3448 -- Start of processing for Analyze_Subprogram_Body_Helper
3451 -- A [generic] subprogram body "freezes" the contract of the nearest
3452 -- enclosing package body and all other contracts encountered in the
3453 -- same declarative part up to and excluding the subprogram body:
3455 -- package body Nearest_Enclosing_Package
3456 -- with Refined_State => (State => Constit)
3460 -- procedure Freezes_Enclosing_Package_Body
3461 -- with Refined_Depends => (Input => Constit) ...
3463 -- This ensures that any annotations referenced by the contract of the
3464 -- [generic] subprogram body are available. This form of "freezing" is
3465 -- decoupled from the usual Freeze_xxx mechanism because it must also
3466 -- work in the context of generics where normal freezing is disabled.
3468 -- Only bodies coming from source should cause this type of "freezing".
3469 -- Expression functions that act as bodies and complete an initial
3470 -- declaration must be included in this category, hence the use of
3473 if Comes_From_Source (Original_Node (N)) then
3474 Analyze_Previous_Contracts (N);
3477 -- Generic subprograms are handled separately. They always have a
3478 -- generic specification. Determine whether current scope has a
3479 -- previous declaration.
3481 -- If the subprogram body is defined within an instance of the same
3482 -- name, the instance appears as a package renaming, and will be hidden
3483 -- within the subprogram.
3485 if Present (Prev_Id)
3486 and then not Is_Overloadable (Prev_Id)
3487 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3488 or else Comes_From_Source (Prev_Id))
3490 if Is_Generic_Subprogram (Prev_Id) then
3493 -- A subprogram body is Ghost when it is stand alone and subject
3494 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3495 -- the mode now to ensure that any nodes generated during analysis
3496 -- and expansion are properly marked as Ghost.
3498 Mark_And_Set_Ghost_Body (N, Spec_Id);
3500 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3501 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3503 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3505 if Nkind (N) = N_Subprogram_Body then
3506 HSS := Handled_Statement_Sequence (N);
3507 Check_Missing_Return;
3512 -- Otherwise a previous entity conflicts with the subprogram name.
3513 -- Attempting to enter name will post error.
3516 Enter_Name (Body_Id);
3520 -- Non-generic case, find the subprogram declaration, if one was seen,
3521 -- or enter new overloaded entity in the current scope. If the
3522 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3523 -- part of the context of one of its subunits. No need to redo the
3526 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3530 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3532 if Nkind (N) = N_Subprogram_Body_Stub
3533 or else No (Corresponding_Spec (N))
3535 if Is_Private_Concurrent_Primitive (Body_Id) then
3536 Spec_Id := Disambiguate_Spec;
3538 -- A subprogram body is Ghost when it is stand alone and
3539 -- subject to pragma Ghost or when the corresponding spec is
3540 -- Ghost. Set the mode now to ensure that any nodes generated
3541 -- during analysis and expansion are properly marked as Ghost.
3543 Mark_And_Set_Ghost_Body (N, Spec_Id);
3546 Spec_Id := Find_Corresponding_Spec (N);
3548 -- A subprogram body is Ghost when it is stand alone and
3549 -- subject to pragma Ghost or when the corresponding spec is
3550 -- Ghost. Set the mode now to ensure that any nodes generated
3551 -- during analysis and expansion are properly marked as Ghost.
3553 Mark_And_Set_Ghost_Body (N, Spec_Id);
3555 -- In GNATprove mode, if the body has no previous spec, create
3556 -- one so that the inlining machinery can operate properly.
3557 -- Transfer aspects, if any, to the new spec, so that they
3558 -- are legal and can be processed ahead of the body.
3559 -- We make two copies of the given spec, one for the new
3560 -- declaration, and one for the body.
3562 if No (Spec_Id) and then GNATprove_Mode
3564 -- Inlining does not apply during pre-analysis of code
3566 and then Full_Analysis
3568 -- Inlining only applies to full bodies, not stubs
3570 and then Nkind (N) /= N_Subprogram_Body_Stub
3572 -- Inlining only applies to bodies in the source code, not to
3573 -- those generated by the compiler. In particular, expression
3574 -- functions, whose body is generated by the compiler, are
3575 -- treated specially by GNATprove.
3577 and then Comes_From_Source (Body_Id)
3579 -- This cannot be done for a compilation unit, which is not
3580 -- in a context where we can insert a new spec.
3582 and then Is_List_Member (N)
3584 -- Inlining only applies to subprograms without contracts,
3585 -- as a contract is a sign that GNATprove should perform a
3586 -- modular analysis of the subprogram instead of a contextual
3587 -- analysis at each call site. The same test is performed in
3588 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3589 -- here in another form (because the contract has not been
3590 -- attached to the body) to avoid front-end errors in case
3591 -- pragmas are used instead of aspects, because the
3592 -- corresponding pragmas in the body would not be transferred
3593 -- to the spec, leading to legality errors.
3595 and then not Body_Has_Contract
3596 and then not Inside_A_Generic
3598 Build_Subprogram_Declaration;
3600 -- If this is a function that returns a constrained array, and
3601 -- we are generating SPARK_For_C, create subprogram declaration
3602 -- to simplify subsequent C generation.
3605 and then Modify_Tree_For_C
3606 and then Nkind (Body_Spec) = N_Function_Specification
3607 and then Is_Array_Type (Etype (Body_Id))
3608 and then Is_Constrained (Etype (Body_Id))
3610 Build_Subprogram_Declaration;
3614 -- If this is a duplicate body, no point in analyzing it
3616 if Error_Posted (N) then
3620 -- A subprogram body should cause freezing of its own declaration,
3621 -- but if there was no previous explicit declaration, then the
3622 -- subprogram will get frozen too late (there may be code within
3623 -- the body that depends on the subprogram having been frozen,
3624 -- such as uses of extra formals), so we force it to be frozen
3625 -- here. Same holds if the body and spec are compilation units.
3626 -- Finally, if the return type is an anonymous access to protected
3627 -- subprogram, it must be frozen before the body because its
3628 -- expansion has generated an equivalent type that is used when
3629 -- elaborating the body.
3631 -- An exception in the case of Ada 2012, AI05-177: The bodies
3632 -- created for expression functions do not freeze.
3635 and then Nkind (Original_Node (N)) /= N_Expression_Function
3637 Freeze_Before (N, Body_Id);
3639 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3640 Freeze_Before (N, Spec_Id);
3642 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3643 Freeze_Before (N, Etype (Body_Id));
3647 Spec_Id := Corresponding_Spec (N);
3649 -- A subprogram body is Ghost when it is stand alone and subject
3650 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3651 -- the mode now to ensure that any nodes generated during analysis
3652 -- and expansion are properly marked as Ghost.
3654 Mark_And_Set_Ghost_Body (N, Spec_Id);
3658 -- Previously we scanned the body to look for nested subprograms, and
3659 -- rejected an inline directive if nested subprograms were present,
3660 -- because the back-end would generate conflicting symbols for the
3661 -- nested bodies. This is now unnecessary.
3663 -- Look ahead to recognize a pragma Inline that appears after the body
3665 Check_Inline_Pragma (Spec_Id);
3667 -- Deal with special case of a fully private operation in the body of
3668 -- the protected type. We must create a declaration for the subprogram,
3669 -- in order to attach the protected subprogram that will be used in
3670 -- internal calls. We exclude compiler generated bodies from the
3671 -- expander since the issue does not arise for those cases.
3674 and then Comes_From_Source (N)
3675 and then Is_Protected_Type (Current_Scope)
3677 Spec_Id := Build_Private_Protected_Declaration (N);
3680 -- If we are generating C and this is a function returning a constrained
3681 -- array type for which we must create a procedure with an extra out
3682 -- parameter, build and analyze the body now. The procedure declaration
3683 -- has already been created. We reuse the source body of the function,
3684 -- because in an instance it may contain global references that cannot
3685 -- be reanalyzed. The source function itself is not used any further,
3686 -- so we mark it as having a completion. If the subprogram is a stub the
3687 -- transformation is done later, when the proper body is analyzed.
3690 and then Modify_Tree_For_C
3691 and then Present (Spec_Id)
3692 and then Ekind (Spec_Id) = E_Function
3693 and then Nkind (N) /= N_Subprogram_Body_Stub
3694 and then Rewritten_For_C (Spec_Id)
3696 Set_Has_Completion (Spec_Id);
3698 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3701 -- The entity for the created procedure must remain invisible, so it
3702 -- does not participate in resolution of subsequent references to the
3705 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3709 -- If a separate spec is present, then deal with freezing issues
3711 if Present (Spec_Id) then
3712 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3713 Verify_Overriding_Indicator;
3715 -- In general, the spec will be frozen when we start analyzing the
3716 -- body. However, for internally generated operations, such as
3717 -- wrapper functions for inherited operations with controlling
3718 -- results, the spec may not have been frozen by the time we expand
3719 -- the freeze actions that include the bodies. In particular, extra
3720 -- formals for accessibility or for return-in-place may need to be
3721 -- generated. Freeze nodes, if any, are inserted before the current
3722 -- body. These freeze actions are also needed in ASIS mode and in
3723 -- Compile_Only mode to enable the proper back-end type annotations.
3724 -- They are necessary in any case to insure order of elaboration
3727 if not Is_Frozen (Spec_Id)
3728 and then (Expander_Active
3730 or else (Operating_Mode = Check_Semantics
3731 and then Serious_Errors_Detected = 0))
3733 -- The body generated for an expression function that is not a
3734 -- completion is a freeze point neither for the profile nor for
3735 -- anything else. That's why, in order to prevent any freezing
3736 -- during analysis, we need to mask types declared outside the
3737 -- expression that are not yet frozen.
3739 if Nkind (N) = N_Subprogram_Body
3740 and then Was_Expression_Function (N)
3741 and then not Has_Completion (Spec_Id)
3743 Set_Is_Frozen (Spec_Id);
3744 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
3746 Set_Has_Delayed_Freeze (Spec_Id);
3747 Freeze_Before (N, Spec_Id);
3752 -- If the subprogram has a class-wide clone, build its body as a copy
3753 -- of the original body, and rewrite body of original subprogram as a
3754 -- wrapper that calls the clone.
3756 if Present (Spec_Id)
3757 and then Present (Class_Wide_Clone (Spec_Id))
3758 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
3760 Build_Class_Wide_Clone_Body (Spec_Id, N);
3762 -- This is the new body for the existing primitive operation
3764 Rewrite (N, Build_Class_Wide_Clone_Call
3765 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
3766 Set_Has_Completion (Spec_Id, False);
3771 -- Place subprogram on scope stack, and make formals visible. If there
3772 -- is a spec, the visible entity remains that of the spec.
3774 if Present (Spec_Id) then
3775 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3777 if Is_Child_Unit (Spec_Id) then
3778 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3782 Style.Check_Identifier (Body_Id, Spec_Id);
3785 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3786 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3788 if Is_Abstract_Subprogram (Spec_Id) then
3789 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3793 Set_Convention (Body_Id, Convention (Spec_Id));
3794 Set_Has_Completion (Spec_Id);
3796 if Is_Protected_Type (Scope (Spec_Id)) then
3797 Prot_Typ := Scope (Spec_Id);
3800 -- If this is a body generated for a renaming, do not check for
3801 -- full conformance. The check is redundant, because the spec of
3802 -- the body is a copy of the spec in the renaming declaration,
3803 -- and the test can lead to spurious errors on nested defaults.
3805 if Present (Spec_Decl)
3806 and then not Comes_From_Source (N)
3808 (Nkind (Original_Node (Spec_Decl)) =
3809 N_Subprogram_Renaming_Declaration
3810 or else (Present (Corresponding_Body (Spec_Decl))
3812 Nkind (Unit_Declaration_Node
3813 (Corresponding_Body (Spec_Decl))) =
3814 N_Subprogram_Renaming_Declaration))
3818 -- Conversely, the spec may have been generated for specless body
3819 -- with an inline pragma. The entity comes from source, which is
3820 -- both semantically correct and necessary for proper inlining.
3821 -- The subprogram declaration itself is not in the source.
3823 elsif Comes_From_Source (N)
3824 and then Present (Spec_Decl)
3825 and then not Comes_From_Source (Spec_Decl)
3826 and then Has_Pragma_Inline (Spec_Id)
3833 Fully_Conformant, True, Conformant, Body_Id);
3836 -- If the body is not fully conformant, we have to decide if we
3837 -- should analyze it or not. If it has a really messed up profile
3838 -- then we probably should not analyze it, since we will get too
3839 -- many bogus messages.
3841 -- Our decision is to go ahead in the non-fully conformant case
3842 -- only if it is at least mode conformant with the spec. Note
3843 -- that the call to Check_Fully_Conformant has issued the proper
3844 -- error messages to complain about the lack of conformance.
3847 and then not Mode_Conformant (Body_Id, Spec_Id)
3853 if Spec_Id /= Body_Id then
3854 Reference_Body_Formals (Spec_Id, Body_Id);
3857 Set_Ekind (Body_Id, E_Subprogram_Body);
3859 if Nkind (N) = N_Subprogram_Body_Stub then
3860 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3865 Set_Corresponding_Spec (N, Spec_Id);
3867 -- Ada 2005 (AI-345): If the operation is a primitive operation
3868 -- of a concurrent type, the type of the first parameter has been
3869 -- replaced with the corresponding record, which is the proper
3870 -- run-time structure to use. However, within the body there may
3871 -- be uses of the formals that depend on primitive operations
3872 -- of the type (in particular calls in prefixed form) for which
3873 -- we need the original concurrent type. The operation may have
3874 -- several controlling formals, so the replacement must be done
3877 if Comes_From_Source (Spec_Id)
3878 and then Present (First_Entity (Spec_Id))
3879 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3880 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3881 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3882 and then Present (Corresponding_Concurrent_Type
3883 (Etype (First_Entity (Spec_Id))))
3886 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3890 Form := First_Formal (Spec_Id);
3891 while Present (Form) loop
3892 if Etype (Form) = Typ then
3893 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3901 -- Make the formals visible, and place subprogram on scope stack.
3902 -- This is also the point at which we set Last_Real_Spec_Entity
3903 -- to mark the entities which will not be moved to the body.
3905 Install_Formals (Spec_Id);
3906 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3908 -- Within an instance, add local renaming declarations so that
3909 -- gdb can retrieve the values of actuals more easily. This is
3910 -- only relevant if generating code (and indeed we definitely
3911 -- do not want these definitions -gnatc mode, because that would
3914 if Is_Generic_Instance (Spec_Id)
3915 and then Is_Wrapper_Package (Current_Scope)
3916 and then Expander_Active
3918 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3921 Push_Scope (Spec_Id);
3923 -- Make sure that the subprogram is immediately visible. For
3924 -- child units that have no separate spec this is indispensable.
3925 -- Otherwise it is safe albeit redundant.
3927 Set_Is_Immediately_Visible (Spec_Id);
3930 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3931 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3932 Set_Scope (Body_Id, Scope (Spec_Id));
3934 -- Case of subprogram body with no previous spec
3937 -- Check for style warning required
3941 -- Only apply check for source level subprograms for which checks
3942 -- have not been suppressed.
3944 and then Comes_From_Source (Body_Id)
3945 and then not Suppress_Style_Checks (Body_Id)
3947 -- No warnings within an instance
3949 and then not In_Instance
3951 -- No warnings for expression functions
3953 and then Nkind (Original_Node (N)) /= N_Expression_Function
3955 Style.Body_With_No_Spec (N);
3958 New_Overloaded_Entity (Body_Id);
3960 if Nkind (N) /= N_Subprogram_Body_Stub then
3961 Set_Acts_As_Spec (N);
3962 Generate_Definition (Body_Id);
3964 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3966 -- If the body is an entry wrapper created for an entry with
3967 -- preconditions, it must be compiled in the context of the
3968 -- enclosing synchronized object, because it may mention other
3969 -- operations of the type.
3971 if Is_Entry_Wrapper (Body_Id) then
3973 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
3976 Install_Declarations (Prot);
3980 Install_Formals (Body_Id);
3982 Push_Scope (Body_Id);
3985 -- For stubs and bodies with no previous spec, generate references to
3988 Generate_Reference_To_Formals (Body_Id);
3991 -- Entry barrier functions are generated outside the protected type and
3992 -- should not carry the SPARK_Mode of the enclosing context.
3994 if Nkind (N) = N_Subprogram_Body
3995 and then Is_Entry_Barrier_Function (N)
3999 -- The body is generated as part of expression function expansion. When
4000 -- the expression function appears in the visible declarations of a
4001 -- package, the body is added to the private declarations. Since both
4002 -- declarative lists may be subject to a different SPARK_Mode, inherit
4003 -- the mode of the spec.
4005 -- package P with SPARK_Mode is
4006 -- function Expr_Func ... is (...); -- original
4007 -- [function Expr_Func ...;] -- generated spec
4010 -- pragma SPARK_Mode (Off);
4011 -- [function Expr_Func ... is return ...;] -- generated body
4012 -- end P; -- mode is ON
4014 elsif not Comes_From_Source (N)
4015 and then Present (Spec_Id)
4016 and then Is_Expression_Function (Spec_Id)
4018 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4019 Set_SPARK_Pragma_Inherited
4020 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4022 -- Set the SPARK_Mode from the current context (may be overwritten later
4023 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4024 -- initially on a stand-alone subprogram body, but is then relocated to
4025 -- a generated corresponding spec. In this scenario the mode is shared
4026 -- between the spec and body.
4028 elsif No (SPARK_Pragma (Body_Id)) then
4029 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4030 Set_SPARK_Pragma_Inherited (Body_Id);
4033 -- A subprogram body may be instantiated or inlined at a later pass.
4034 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4035 -- applied to the initial declaration of the body.
4037 if Present (Spec_Id) then
4038 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4039 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4043 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4044 -- case the body is instantiated or inlined later and out of context.
4045 -- The body uses this attribute to restore the value of the global
4048 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4049 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4051 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4052 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4056 -- If this is the proper body of a stub, we must verify that the stub
4057 -- conforms to the body, and to the previous spec if one was present.
4058 -- We know already that the body conforms to that spec. This test is
4059 -- only required for subprograms that come from source.
4061 if Nkind (Parent (N)) = N_Subunit
4062 and then Comes_From_Source (N)
4063 and then not Error_Posted (Body_Id)
4064 and then Nkind (Corresponding_Stub (Parent (N))) =
4065 N_Subprogram_Body_Stub
4068 Old_Id : constant Entity_Id :=
4070 (Specification (Corresponding_Stub (Parent (N))));
4072 Conformant : Boolean := False;
4075 if No (Spec_Id) then
4076 Check_Fully_Conformant (Body_Id, Old_Id);
4080 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4082 if not Conformant then
4084 -- The stub was taken to be a new declaration. Indicate that
4087 Set_Has_Completion (Old_Id, False);
4093 Set_Has_Completion (Body_Id);
4094 Check_Eliminated (Body_Id);
4096 -- Analyze any aspect specifications that appear on the subprogram body
4097 -- stub. Stop the analysis now as the stub does not have a declarative
4098 -- or a statement part, and it cannot be inlined.
4100 if Nkind (N) = N_Subprogram_Body_Stub then
4101 if Has_Aspects (N) then
4102 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4110 -- Note: Normally we don't do any inlining if expansion is off, since
4111 -- we won't generate code in any case. An exception arises in GNATprove
4112 -- mode where we want to expand some calls in place, even with expansion
4113 -- disabled, since the inlining eases formal verification.
4115 if not GNATprove_Mode
4116 and then Expander_Active
4117 and then Serious_Errors_Detected = 0
4118 and then Present (Spec_Id)
4119 and then Has_Pragma_Inline (Spec_Id)
4121 -- Legacy implementation (relying on front-end inlining)
4123 if not Back_End_Inlining then
4124 if (Has_Pragma_Inline_Always (Spec_Id)
4125 and then not Opt.Disable_FE_Inline_Always)
4126 or else (Front_End_Inlining
4127 and then not Opt.Disable_FE_Inline)
4129 Build_Body_To_Inline (N, Spec_Id);
4132 -- New implementation (relying on back-end inlining)
4135 if Has_Pragma_Inline_Always (Spec_Id)
4136 or else Optimization_Level > 0
4138 -- Handle function returning an unconstrained type
4140 if Comes_From_Source (Body_Id)
4141 and then Ekind (Spec_Id) = E_Function
4142 and then Returns_Unconstrained_Type (Spec_Id)
4144 -- If function builds in place, i.e. returns a limited type,
4145 -- inlining cannot be done.
4147 and then not Is_Limited_Type (Etype (Spec_Id))
4149 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4153 Subp_Body : constant Node_Id :=
4154 Unit_Declaration_Node (Body_Id);
4155 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4158 -- Do not pass inlining to the backend if the subprogram
4159 -- has declarations or statements which cannot be inlined
4160 -- by the backend. This check is done here to emit an
4161 -- error instead of the generic warning message reported
4162 -- by the GCC backend (ie. "function might not be
4165 if Present (Subp_Decl)
4166 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4170 elsif Has_Excluded_Statement
4173 (Handled_Statement_Sequence (Subp_Body)))
4177 -- If the backend inlining is available then at this
4178 -- stage we only have to mark the subprogram as inlined.
4179 -- The expander will take care of registering it in the
4180 -- table of subprograms inlined by the backend a part of
4181 -- processing calls to it (cf. Expand_Call)
4184 Set_Is_Inlined (Spec_Id);
4191 -- In GNATprove mode, inline only when there is a separate subprogram
4192 -- declaration for now, as inlining of subprogram bodies acting as
4193 -- declarations, or subprogram stubs, are not supported by front-end
4194 -- inlining. This inlining should occur after analysis of the body, so
4195 -- that it is known whether the value of SPARK_Mode, which can be
4196 -- defined by a pragma inside the body, is applicable to the body.
4197 -- Inlining can be disabled with switch -gnatdm
4199 elsif GNATprove_Mode
4200 and then Full_Analysis
4201 and then not Inside_A_Generic
4202 and then Present (Spec_Id)
4204 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4205 and then Body_Has_SPARK_Mode_On
4206 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4207 and then not Body_Has_Contract
4208 and then not Debug_Flag_M
4210 Build_Body_To_Inline (N, Spec_Id);
4213 -- When generating code, inherited pre/postconditions are handled when
4214 -- expanding the corresponding contract.
4216 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4217 -- of the specification we have to install the private withed units.
4218 -- This holds for child units as well.
4220 if Is_Compilation_Unit (Body_Id)
4221 or else Nkind (Parent (N)) = N_Compilation_Unit
4223 Install_Private_With_Clauses (Body_Id);
4226 Check_Anonymous_Return;
4228 -- Set the Protected_Formal field of each extra formal of the protected
4229 -- subprogram to reference the corresponding extra formal of the
4230 -- subprogram that implements it. For regular formals this occurs when
4231 -- the protected subprogram's declaration is expanded, but the extra
4232 -- formals don't get created until the subprogram is frozen. We need to
4233 -- do this before analyzing the protected subprogram's body so that any
4234 -- references to the original subprogram's extra formals will be changed
4235 -- refer to the implementing subprogram's formals (see Expand_Formal).
4237 if Present (Spec_Id)
4238 and then Is_Protected_Type (Scope (Spec_Id))
4239 and then Present (Protected_Body_Subprogram (Spec_Id))
4242 Impl_Subp : constant Entity_Id :=
4243 Protected_Body_Subprogram (Spec_Id);
4244 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4245 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4248 while Present (Prot_Ext_Formal) loop
4249 pragma Assert (Present (Impl_Ext_Formal));
4250 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4251 Next_Formal_With_Extras (Prot_Ext_Formal);
4252 Next_Formal_With_Extras (Impl_Ext_Formal);
4257 -- Now we can go on to analyze the body
4259 HSS := Handled_Statement_Sequence (N);
4260 Set_Actual_Subtypes (N, Current_Scope);
4262 -- Add a declaration for the Protection object, renaming declarations
4263 -- for discriminals and privals and finally a declaration for the entry
4264 -- family index (if applicable). This form of early expansion is done
4265 -- when the Expander is active because Install_Private_Data_Declarations
4266 -- references entities which were created during regular expansion. The
4267 -- subprogram entity must come from source, and not be an internally
4268 -- generated subprogram.
4271 and then Present (Prot_Typ)
4272 and then Present (Spec_Id)
4273 and then Comes_From_Source (Spec_Id)
4274 and then not Is_Eliminated (Spec_Id)
4276 Install_Private_Data_Declarations
4277 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4280 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4281 -- may now appear in parameter and result profiles. Since the analysis
4282 -- of a subprogram body may use the parameter and result profile of the
4283 -- spec, swap any limited views with their non-limited counterpart.
4285 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4286 Exch_Views := Exchange_Limited_Views (Spec_Id);
4289 -- If the return type is an anonymous access type whose designated type
4290 -- is the limited view of a class-wide type and the non-limited view is
4291 -- available, update the return type accordingly.
4293 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4299 Rtyp := Etype (Spec_Id);
4301 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4302 Etyp := Directly_Designated_Type (Rtyp);
4304 if Is_Class_Wide_Type (Etyp)
4305 and then From_Limited_With (Etyp)
4308 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4314 -- Analyze any aspect specifications that appear on the subprogram body
4316 if Has_Aspects (N) then
4317 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4320 Analyze_Declarations (Declarations (N));
4322 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4324 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4325 if Present (SPARK_Pragma (Spec_Id)) then
4326 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4328 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4330 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4331 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4332 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4334 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4337 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4341 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4342 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4343 Error_Msg_Sloc := Sloc (Spec_Id);
4345 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4349 -- A subprogram body "freezes" its own contract. Analyze the contract
4350 -- after the declarations of the body have been processed as pragmas
4351 -- are now chained on the contract of the subprogram body.
4353 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4355 -- Check completion, and analyze the statements
4358 Inspect_Deferred_Constant_Completion (Declarations (N));
4361 -- Deal with end of scope processing for the body
4363 Process_End_Label (HSS, 't', Current_Scope);
4364 Update_Use_Clause_Chain;
4367 -- If we are compiling an entry wrapper, remove the enclosing
4368 -- synchronized object from the stack.
4370 if Is_Entry_Wrapper (Body_Id) then
4374 Check_Subprogram_Order (N);
4375 Set_Analyzed (Body_Id);
4377 -- If we have a separate spec, then the analysis of the declarations
4378 -- caused the entities in the body to be chained to the spec id, but
4379 -- we want them chained to the body id. Only the formal parameters
4380 -- end up chained to the spec id in this case.
4382 if Present (Spec_Id) then
4384 -- We must conform to the categorization of our spec
4386 Validate_Categorization_Dependency (N, Spec_Id);
4388 -- And if this is a child unit, the parent units must conform
4390 if Is_Child_Unit (Spec_Id) then
4391 Validate_Categorization_Dependency
4392 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4395 -- Here is where we move entities from the spec to the body
4397 -- Case where there are entities that stay with the spec
4399 if Present (Last_Real_Spec_Entity) then
4401 -- No body entities (happens when the only real spec entities come
4402 -- from precondition and postcondition pragmas).
4404 if No (Last_Entity (Body_Id)) then
4405 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4407 -- Body entities present (formals), so chain stuff past them
4411 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4414 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4415 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4416 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4418 -- Case where there are no spec entities, in this case there can be
4419 -- no body entities either, so just move everything.
4421 -- If the body is generated for an expression function, it may have
4422 -- been preanalyzed already, if 'access was applied to it.
4425 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4426 N_Expression_Function
4428 pragma Assert (No (Last_Entity (Body_Id)));
4432 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4433 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4434 Set_First_Entity (Spec_Id, Empty);
4435 Set_Last_Entity (Spec_Id, Empty);
4439 Check_Missing_Return;
4441 -- Now we are going to check for variables that are never modified in
4442 -- the body of the procedure. But first we deal with a special case
4443 -- where we want to modify this check. If the body of the subprogram
4444 -- starts with a raise statement or its equivalent, or if the body
4445 -- consists entirely of a null statement, then it is pretty obvious that
4446 -- it is OK to not reference the parameters. For example, this might be
4447 -- the following common idiom for a stubbed function: statement of the
4448 -- procedure raises an exception. In particular this deals with the
4449 -- common idiom of a stubbed function, which appears something like:
4451 -- function F (A : Integer) return Some_Type;
4454 -- raise Program_Error;
4458 -- Here the purpose of X is simply to satisfy the annoying requirement
4459 -- in Ada that there be at least one return, and we certainly do not
4460 -- want to go posting warnings on X that it is not initialized. On
4461 -- the other hand, if X is entirely unreferenced that should still
4464 -- What we do is to detect these cases, and if we find them, flag the
4465 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4466 -- suppress unwanted warnings. For the case of the function stub above
4467 -- we have a special test to set X as apparently assigned to suppress
4474 -- Skip initial labels (for one thing this occurs when we are in
4475 -- front-end ZCX mode, but in any case it is irrelevant), and also
4476 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4478 Stm := First (Statements (HSS));
4479 while Nkind (Stm) = N_Label
4480 or else Nkind (Stm) in N_Push_xxx_Label
4485 -- Do the test on the original statement before expansion
4488 Ostm : constant Node_Id := Original_Node (Stm);
4491 -- If explicit raise statement, turn on flag
4493 if Nkind (Ostm) = N_Raise_Statement then
4494 Set_Trivial_Subprogram (Stm);
4496 -- If null statement, and no following statements, turn on flag
4498 elsif Nkind (Stm) = N_Null_Statement
4499 and then Comes_From_Source (Stm)
4500 and then No (Next (Stm))
4502 Set_Trivial_Subprogram (Stm);
4504 -- Check for explicit call cases which likely raise an exception
4506 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4507 if Is_Entity_Name (Name (Ostm)) then
4509 Ent : constant Entity_Id := Entity (Name (Ostm));
4512 -- If the procedure is marked No_Return, then likely it
4513 -- raises an exception, but in any case it is not coming
4514 -- back here, so turn on the flag.
4517 and then Ekind (Ent) = E_Procedure
4518 and then No_Return (Ent)
4520 Set_Trivial_Subprogram (Stm);
4528 -- Check for variables that are never modified
4535 -- If there is a separate spec, then transfer Never_Set_In_Source
4536 -- flags from out parameters to the corresponding entities in the
4537 -- body. The reason we do that is we want to post error flags on
4538 -- the body entities, not the spec entities.
4540 if Present (Spec_Id) then
4541 E1 := First_Entity (Spec_Id);
4542 while Present (E1) loop
4543 if Ekind (E1) = E_Out_Parameter then
4544 E2 := First_Entity (Body_Id);
4545 while Present (E2) loop
4546 exit when Chars (E1) = Chars (E2);
4550 if Present (E2) then
4551 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4559 -- Check references in body
4561 Check_References (Body_Id);
4564 -- Check for nested subprogram, and mark outer level subprogram if so
4570 if Present (Spec_Id) then
4577 Ent := Enclosing_Subprogram (Ent);
4578 exit when No (Ent) or else Is_Subprogram (Ent);
4581 if Present (Ent) then
4582 Set_Has_Nested_Subprogram (Ent);
4586 -- Restore the limited views in the spec, if any, to let the back end
4587 -- process it without running into circularities.
4589 if Exch_Views /= No_Elist then
4590 Restore_Limited_Views (Exch_Views);
4593 if Mask_Types /= No_Elist then
4594 Unmask_Unfrozen_Types (Mask_Types);
4597 if Present (Desig_View) then
4598 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4602 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4603 Restore_Ghost_Mode (Saved_GM);
4604 end Analyze_Subprogram_Body_Helper;
4606 ------------------------------------
4607 -- Analyze_Subprogram_Declaration --
4608 ------------------------------------
4610 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4611 Scop : constant Entity_Id := Current_Scope;
4612 Designator : Entity_Id;
4614 Is_Completion : Boolean;
4615 -- Indicates whether a null procedure declaration is a completion
4618 -- Null procedures are not allowed in SPARK
4620 if Nkind (Specification (N)) = N_Procedure_Specification
4621 and then Null_Present (Specification (N))
4623 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4625 -- Null procedures are allowed in protected types, following the
4626 -- recent AI12-0147.
4628 if Is_Protected_Type (Current_Scope)
4629 and then Ada_Version < Ada_2012
4631 Error_Msg_N ("protected operation cannot be a null procedure", N);
4634 Analyze_Null_Procedure (N, Is_Completion);
4636 -- The null procedure acts as a body, nothing further is needed
4638 if Is_Completion then
4643 Designator := Analyze_Subprogram_Specification (Specification (N));
4645 -- A reference may already have been generated for the unit name, in
4646 -- which case the following call is redundant. However it is needed for
4647 -- declarations that are the rewriting of an expression function.
4649 Generate_Definition (Designator);
4651 -- Set the SPARK mode from the current context (may be overwritten later
4652 -- with explicit pragma). This is not done for entry barrier functions
4653 -- because they are generated outside the protected type and should not
4654 -- carry the mode of the enclosing context.
4656 if Nkind (N) = N_Subprogram_Declaration
4657 and then Is_Entry_Barrier_Function (N)
4661 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4662 Set_SPARK_Pragma_Inherited (Designator);
4665 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
4666 -- the body of this subprogram is instantiated or inlined later and out
4667 -- of context. The body uses this attribute to restore the value of the
4670 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4671 Set_Ignore_SPARK_Mode_Pragmas (Designator);
4674 if Debug_Flag_C then
4675 Write_Str ("==> subprogram spec ");
4676 Write_Name (Chars (Designator));
4677 Write_Str (" from ");
4678 Write_Location (Sloc (N));
4683 Validate_RCI_Subprogram_Declaration (N);
4684 New_Overloaded_Entity (Designator);
4685 Check_Delayed_Subprogram (Designator);
4687 -- If the type of the first formal of the current subprogram is a non-
4688 -- generic tagged private type, mark the subprogram as being a private
4689 -- primitive. Ditto if this is a function with controlling result, and
4690 -- the return type is currently private. In both cases, the type of the
4691 -- controlling argument or result must be in the current scope for the
4692 -- operation to be primitive.
4694 if Has_Controlling_Result (Designator)
4695 and then Is_Private_Type (Etype (Designator))
4696 and then Scope (Etype (Designator)) = Current_Scope
4697 and then not Is_Generic_Actual_Type (Etype (Designator))
4699 Set_Is_Private_Primitive (Designator);
4701 elsif Present (First_Formal (Designator)) then
4703 Formal_Typ : constant Entity_Id :=
4704 Etype (First_Formal (Designator));
4706 Set_Is_Private_Primitive (Designator,
4707 Is_Tagged_Type (Formal_Typ)
4708 and then Scope (Formal_Typ) = Current_Scope
4709 and then Is_Private_Type (Formal_Typ)
4710 and then not Is_Generic_Actual_Type (Formal_Typ));
4714 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4717 if Ada_Version >= Ada_2005
4718 and then Comes_From_Source (N)
4719 and then Is_Dispatching_Operation (Designator)
4726 if Has_Controlling_Result (Designator) then
4727 Etyp := Etype (Designator);
4730 E := First_Entity (Designator);
4732 and then Is_Formal (E)
4733 and then not Is_Controlling_Formal (E)
4741 if Is_Access_Type (Etyp) then
4742 Etyp := Directly_Designated_Type (Etyp);
4745 if Is_Interface (Etyp)
4746 and then not Is_Abstract_Subprogram (Designator)
4747 and then not (Ekind (Designator) = E_Procedure
4748 and then Null_Present (Specification (N)))
4750 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4752 -- Specialize error message based on procedures vs. functions,
4753 -- since functions can't be null subprograms.
4755 if Ekind (Designator) = E_Procedure then
4757 ("interface procedure % must be abstract or null", N);
4760 ("interface function % must be abstract", N);
4766 -- What is the following code for, it used to be
4768 -- ??? Set_Suppress_Elaboration_Checks
4769 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4771 -- The following seems equivalent, but a bit dubious
4773 if Elaboration_Checks_Suppressed (Designator) then
4774 Set_Kill_Elaboration_Checks (Designator);
4777 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4778 Set_Categorization_From_Scope (Designator, Scop);
4781 -- For a compilation unit, check for library-unit pragmas
4783 Push_Scope (Designator);
4784 Set_Categorization_From_Pragmas (N);
4785 Validate_Categorization_Dependency (N, Designator);
4789 -- For a compilation unit, set body required. This flag will only be
4790 -- reset if a valid Import or Interface pragma is processed later on.
4792 if Nkind (Parent (N)) = N_Compilation_Unit then
4793 Set_Body_Required (Parent (N), True);
4795 if Ada_Version >= Ada_2005
4796 and then Nkind (Specification (N)) = N_Procedure_Specification
4797 and then Null_Present (Specification (N))
4800 ("null procedure cannot be declared at library level", N);
4804 Generate_Reference_To_Formals (Designator);
4805 Check_Eliminated (Designator);
4807 if Debug_Flag_C then
4809 Write_Str ("<== subprogram spec ");
4810 Write_Name (Chars (Designator));
4811 Write_Str (" from ");
4812 Write_Location (Sloc (N));
4816 if Is_Protected_Type (Current_Scope) then
4818 -- Indicate that this is a protected operation, because it may be
4819 -- used in subsequent declarations within the protected type.
4821 Set_Convention (Designator, Convention_Protected);
4824 List_Inherited_Pre_Post_Aspects (Designator);
4826 if Has_Aspects (N) then
4827 Analyze_Aspect_Specifications (N, Designator);
4829 end Analyze_Subprogram_Declaration;
4831 --------------------------------------
4832 -- Analyze_Subprogram_Specification --
4833 --------------------------------------
4835 -- Reminder: N here really is a subprogram specification (not a subprogram
4836 -- declaration). This procedure is called to analyze the specification in
4837 -- both subprogram bodies and subprogram declarations (specs).
4839 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4840 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
4841 -- Determine whether entity E denotes the spec or body of an invariant
4844 ------------------------------------
4845 -- Is_Invariant_Procedure_Or_Body --
4846 ------------------------------------
4848 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
4849 Decl : constant Node_Id := Unit_Declaration_Node (E);
4853 if Nkind (Decl) = N_Subprogram_Body then
4854 Spec := Corresponding_Spec (Decl);
4861 and then Ekind (Spec) = E_Procedure
4862 and then (Is_Partial_Invariant_Procedure (Spec)
4863 or else Is_Invariant_Procedure (Spec));
4864 end Is_Invariant_Procedure_Or_Body;
4868 Designator : constant Entity_Id := Defining_Entity (N);
4869 Formals : constant List_Id := Parameter_Specifications (N);
4871 -- Start of processing for Analyze_Subprogram_Specification
4874 -- User-defined operator is not allowed in SPARK, except as a renaming
4876 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4877 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4879 Check_SPARK_05_Restriction
4880 ("user-defined operator is not allowed", N);
4883 -- Proceed with analysis. Do not emit a cross-reference entry if the
4884 -- specification comes from an expression function, because it may be
4885 -- the completion of a previous declaration. It is not, the cross-
4886 -- reference entry will be emitted for the new subprogram declaration.
4888 if Nkind (Parent (N)) /= N_Expression_Function then
4889 Generate_Definition (Designator);
4892 if Nkind (N) = N_Function_Specification then
4893 Set_Ekind (Designator, E_Function);
4894 Set_Mechanism (Designator, Default_Mechanism);
4896 Set_Ekind (Designator, E_Procedure);
4897 Set_Etype (Designator, Standard_Void_Type);
4900 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4901 -- those subprograms which could be inlined in GNATprove mode (because
4902 -- Body_To_Inline is non-Empty) but should not be inlined.
4904 if GNATprove_Mode then
4905 Set_Is_Inlined_Always (Designator);
4908 -- Introduce new scope for analysis of the formals and the return type
4910 Set_Scope (Designator, Current_Scope);
4912 if Present (Formals) then
4913 Push_Scope (Designator);
4914 Process_Formals (Formals, N);
4916 -- Check dimensions in N for formals with default expression
4918 Analyze_Dimension_Formals (N, Formals);
4920 -- Ada 2005 (AI-345): If this is an overriding operation of an
4921 -- inherited interface operation, and the controlling type is
4922 -- a synchronized type, replace the type with its corresponding
4923 -- record, to match the proper signature of an overriding operation.
4924 -- Same processing for an access parameter whose designated type is
4925 -- derived from a synchronized interface.
4927 -- This modification is not done for invariant procedures because
4928 -- the corresponding record may not necessarely be visible when the
4929 -- concurrent type acts as the full view of a private type.
4932 -- type Prot is private with Type_Invariant => ...;
4933 -- procedure ConcInvariant (Obj : Prot);
4935 -- protected type Prot is ...;
4936 -- type Concurrent_Record_Prot is record ...;
4937 -- procedure ConcInvariant (Obj : Prot) is
4939 -- end ConcInvariant;
4942 -- In the example above, both the spec and body of the invariant
4943 -- procedure must utilize the private type as the controlling type.
4945 if Ada_Version >= Ada_2005
4946 and then not Is_Invariant_Procedure_Or_Body (Designator)
4950 Formal_Typ : Entity_Id;
4951 Rec_Typ : Entity_Id;
4952 Desig_Typ : Entity_Id;
4955 Formal := First_Formal (Designator);
4956 while Present (Formal) loop
4957 Formal_Typ := Etype (Formal);
4959 if Is_Concurrent_Type (Formal_Typ)
4960 and then Present (Corresponding_Record_Type (Formal_Typ))
4962 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4964 if Present (Interfaces (Rec_Typ)) then
4965 Set_Etype (Formal, Rec_Typ);
4968 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4969 Desig_Typ := Designated_Type (Formal_Typ);
4971 if Is_Concurrent_Type (Desig_Typ)
4972 and then Present (Corresponding_Record_Type (Desig_Typ))
4974 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4976 if Present (Interfaces (Rec_Typ)) then
4977 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4982 Next_Formal (Formal);
4989 -- The subprogram scope is pushed and popped around the processing of
4990 -- the return type for consistency with call above to Process_Formals
4991 -- (which itself can call Analyze_Return_Type), and to ensure that any
4992 -- itype created for the return type will be associated with the proper
4995 elsif Nkind (N) = N_Function_Specification then
4996 Push_Scope (Designator);
4997 Analyze_Return_Type (N);
5003 if Nkind (N) = N_Function_Specification then
5005 -- Deal with operator symbol case
5007 if Nkind (Designator) = N_Defining_Operator_Symbol then
5008 Valid_Operator_Definition (Designator);
5011 May_Need_Actuals (Designator);
5013 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5014 -- the subprogram is abstract also. This does not apply to renaming
5015 -- declarations, where abstractness is inherited, and to subprogram
5016 -- bodies generated for stream operations, which become renamings as
5019 -- In case of primitives associated with abstract interface types
5020 -- the check is applied later (see Analyze_Subprogram_Declaration).
5022 if not Nkind_In (Original_Node (Parent (N)),
5023 N_Abstract_Subprogram_Declaration,
5024 N_Formal_Abstract_Subprogram_Declaration,
5025 N_Subprogram_Renaming_Declaration)
5027 if Is_Abstract_Type (Etype (Designator))
5028 and then not Is_Interface (Etype (Designator))
5031 ("function that returns abstract type must be abstract", N);
5033 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5034 -- access result whose designated type is abstract.
5036 elsif Ada_Version >= Ada_2012
5037 and then Nkind (Result_Definition (N)) = N_Access_Definition
5039 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5040 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5043 ("function whose access result designates abstract type "
5044 & "must be abstract", N);
5050 end Analyze_Subprogram_Specification;
5052 -----------------------
5053 -- Check_Conformance --
5054 -----------------------
5056 procedure Check_Conformance
5057 (New_Id : Entity_Id;
5059 Ctype : Conformance_Type;
5061 Conforms : out Boolean;
5062 Err_Loc : Node_Id := Empty;
5063 Get_Inst : Boolean := False;
5064 Skip_Controlling_Formals : Boolean := False)
5066 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5067 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5068 -- If Errmsg is True, then processing continues to post an error message
5069 -- for conformance error on given node. Two messages are output. The
5070 -- first message points to the previous declaration with a general "no
5071 -- conformance" message. The second is the detailed reason, supplied as
5072 -- Msg. The parameter N provide information for a possible & insertion
5073 -- in the message, and also provides the location for posting the
5074 -- message in the absence of a specified Err_Loc location.
5076 function Conventions_Match
5078 Id2 : Entity_Id) return Boolean;
5079 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5082 -----------------------
5083 -- Conformance_Error --
5084 -----------------------
5086 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5093 if No (Err_Loc) then
5099 Error_Msg_Sloc := Sloc (Old_Id);
5102 when Type_Conformant =>
5103 Error_Msg_N -- CODEFIX
5104 ("not type conformant with declaration#!", Enode);
5106 when Mode_Conformant =>
5107 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5109 ("not mode conformant with operation inherited#!",
5113 ("not mode conformant with declaration#!", Enode);
5116 when Subtype_Conformant =>
5117 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5119 ("not subtype conformant with operation inherited#!",
5123 ("not subtype conformant with declaration#!", Enode);
5126 when Fully_Conformant =>
5127 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5128 Error_Msg_N -- CODEFIX
5129 ("not fully conformant with operation inherited#!",
5132 Error_Msg_N -- CODEFIX
5133 ("not fully conformant with declaration#!", Enode);
5137 Error_Msg_NE (Msg, Enode, N);
5139 end Conformance_Error;
5141 -----------------------
5142 -- Conventions_Match --
5143 -----------------------
5145 function Conventions_Match
5147 Id2 : Entity_Id) return Boolean
5150 -- Ignore the conventions of anonymous access-to-subprogram types
5151 -- and subprogram types because these are internally generated and
5152 -- the only way these may receive a convention is if they inherit
5153 -- the convention of a related subprogram.
5155 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5158 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5163 -- Otherwise compare the conventions directly
5166 return Convention (Id1) = Convention (Id2);
5168 end Conventions_Match;
5172 Old_Type : constant Entity_Id := Etype (Old_Id);
5173 New_Type : constant Entity_Id := Etype (New_Id);
5174 Old_Formal : Entity_Id;
5175 New_Formal : Entity_Id;
5176 Access_Types_Match : Boolean;
5177 Old_Formal_Base : Entity_Id;
5178 New_Formal_Base : Entity_Id;
5180 -- Start of processing for Check_Conformance
5185 -- We need a special case for operators, since they don't appear
5188 if Ctype = Type_Conformant then
5189 if Ekind (New_Id) = E_Operator
5190 and then Operator_Matches_Spec (New_Id, Old_Id)
5196 -- If both are functions/operators, check return types conform
5198 if Old_Type /= Standard_Void_Type
5200 New_Type /= Standard_Void_Type
5202 -- If we are checking interface conformance we omit controlling
5203 -- arguments and result, because we are only checking the conformance
5204 -- of the remaining parameters.
5206 if Has_Controlling_Result (Old_Id)
5207 and then Has_Controlling_Result (New_Id)
5208 and then Skip_Controlling_Formals
5212 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5213 if Ctype >= Subtype_Conformant
5214 and then not Predicates_Match (Old_Type, New_Type)
5217 ("\predicate of return type does not match!", New_Id);
5220 ("\return type does not match!", New_Id);
5226 -- Ada 2005 (AI-231): In case of anonymous access types check the
5227 -- null-exclusion and access-to-constant attributes match.
5229 if Ada_Version >= Ada_2005
5230 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5232 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5233 or else Is_Access_Constant (Etype (Old_Type)) /=
5234 Is_Access_Constant (Etype (New_Type)))
5236 Conformance_Error ("\return type does not match!", New_Id);
5240 -- If either is a function/operator and the other isn't, error
5242 elsif Old_Type /= Standard_Void_Type
5243 or else New_Type /= Standard_Void_Type
5245 Conformance_Error ("\functions can only match functions!", New_Id);
5249 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5250 -- If this is a renaming as body, refine error message to indicate that
5251 -- the conflict is with the original declaration. If the entity is not
5252 -- frozen, the conventions don't have to match, the one of the renamed
5253 -- entity is inherited.
5255 if Ctype >= Subtype_Conformant then
5256 if not Conventions_Match (Old_Id, New_Id) then
5257 if not Is_Frozen (New_Id) then
5260 elsif Present (Err_Loc)
5261 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5262 and then Present (Corresponding_Spec (Err_Loc))
5264 Error_Msg_Name_1 := Chars (New_Id);
5266 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5267 Conformance_Error ("\prior declaration for% has convention %!");
5270 Conformance_Error ("\calling conventions do not match!");
5275 elsif Is_Formal_Subprogram (Old_Id)
5276 or else Is_Formal_Subprogram (New_Id)
5278 Conformance_Error ("\formal subprograms not allowed!");
5283 -- Deal with parameters
5285 -- Note: we use the entity information, rather than going directly
5286 -- to the specification in the tree. This is not only simpler, but
5287 -- absolutely necessary for some cases of conformance tests between
5288 -- operators, where the declaration tree simply does not exist.
5290 Old_Formal := First_Formal (Old_Id);
5291 New_Formal := First_Formal (New_Id);
5292 while Present (Old_Formal) and then Present (New_Formal) loop
5293 if Is_Controlling_Formal (Old_Formal)
5294 and then Is_Controlling_Formal (New_Formal)
5295 and then Skip_Controlling_Formals
5297 -- The controlling formals will have different types when
5298 -- comparing an interface operation with its match, but both
5299 -- or neither must be access parameters.
5301 if Is_Access_Type (Etype (Old_Formal))
5303 Is_Access_Type (Etype (New_Formal))
5305 goto Skip_Controlling_Formal;
5308 ("\access parameter does not match!", New_Formal);
5312 -- Ada 2012: Mode conformance also requires that formal parameters
5313 -- be both aliased, or neither.
5315 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5316 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5318 ("\aliased parameter mismatch!", New_Formal);
5322 if Ctype = Fully_Conformant then
5324 -- Names must match. Error message is more accurate if we do
5325 -- this before checking that the types of the formals match.
5327 if Chars (Old_Formal) /= Chars (New_Formal) then
5328 Conformance_Error ("\name& does not match!", New_Formal);
5330 -- Set error posted flag on new formal as well to stop
5331 -- junk cascaded messages in some cases.
5333 Set_Error_Posted (New_Formal);
5337 -- Null exclusion must match
5339 if Null_Exclusion_Present (Parent (Old_Formal))
5341 Null_Exclusion_Present (Parent (New_Formal))
5343 -- Only give error if both come from source. This should be
5344 -- investigated some time, since it should not be needed ???
5346 if Comes_From_Source (Old_Formal)
5348 Comes_From_Source (New_Formal)
5351 ("\null exclusion for& does not match", New_Formal);
5353 -- Mark error posted on the new formal to avoid duplicated
5354 -- complaint about types not matching.
5356 Set_Error_Posted (New_Formal);
5361 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5362 -- case occurs whenever a subprogram is being renamed and one of its
5363 -- parameters imposes a null exclusion. For example:
5365 -- type T is null record;
5366 -- type Acc_T is access T;
5367 -- subtype Acc_T_Sub is Acc_T;
5369 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5370 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5373 Old_Formal_Base := Etype (Old_Formal);
5374 New_Formal_Base := Etype (New_Formal);
5377 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5378 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5381 Access_Types_Match := Ada_Version >= Ada_2005
5383 -- Ensure that this rule is only applied when New_Id is a
5384 -- renaming of Old_Id.
5386 and then Nkind (Parent (Parent (New_Id))) =
5387 N_Subprogram_Renaming_Declaration
5388 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5389 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5390 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5392 -- Now handle the allowed access-type case
5394 and then Is_Access_Type (Old_Formal_Base)
5395 and then Is_Access_Type (New_Formal_Base)
5397 -- The type kinds must match. The only exception occurs with
5398 -- multiple generics of the form:
5401 -- type F is private; type A is private;
5402 -- type F_Ptr is access F; type A_Ptr is access A;
5403 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5404 -- package F_Pack is ... package A_Pack is
5405 -- package F_Inst is
5406 -- new F_Pack (A, A_Ptr, A_P);
5408 -- When checking for conformance between the parameters of A_P
5409 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5410 -- because the compiler has transformed A_Ptr into a subtype of
5411 -- F_Ptr. We catch this case in the code below.
5413 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5415 (Is_Generic_Type (Old_Formal_Base)
5416 and then Is_Generic_Type (New_Formal_Base)
5417 and then Is_Internal (New_Formal_Base)
5418 and then Etype (Etype (New_Formal_Base)) =
5420 and then Directly_Designated_Type (Old_Formal_Base) =
5421 Directly_Designated_Type (New_Formal_Base)
5422 and then ((Is_Itype (Old_Formal_Base)
5423 and then Can_Never_Be_Null (Old_Formal_Base))
5425 (Is_Itype (New_Formal_Base)
5426 and then Can_Never_Be_Null (New_Formal_Base)));
5428 -- Types must always match. In the visible part of an instance,
5429 -- usual overloading rules for dispatching operations apply, and
5430 -- we check base types (not the actual subtypes).
5432 if In_Instance_Visible_Part
5433 and then Is_Dispatching_Operation (New_Id)
5435 if not Conforming_Types
5436 (T1 => Base_Type (Etype (Old_Formal)),
5437 T2 => Base_Type (Etype (New_Formal)),
5439 Get_Inst => Get_Inst)
5440 and then not Access_Types_Match
5442 Conformance_Error ("\type of & does not match!", New_Formal);
5446 elsif not Conforming_Types
5447 (T1 => Old_Formal_Base,
5448 T2 => New_Formal_Base,
5450 Get_Inst => Get_Inst)
5451 and then not Access_Types_Match
5453 -- Don't give error message if old type is Any_Type. This test
5454 -- avoids some cascaded errors, e.g. in case of a bad spec.
5456 if Errmsg and then Old_Formal_Base = Any_Type then
5459 if Ctype >= Subtype_Conformant
5461 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5464 ("\predicate of & does not match!", New_Formal);
5467 ("\type of & does not match!", New_Formal);
5469 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5471 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5479 -- For mode conformance, mode must match
5481 if Ctype >= Mode_Conformant then
5482 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5483 if not Ekind_In (New_Id, E_Function, E_Procedure)
5484 or else not Is_Primitive_Wrapper (New_Id)
5486 Conformance_Error ("\mode of & does not match!", New_Formal);
5490 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5492 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5494 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5497 ("\mode of & does not match!", New_Formal);
5504 -- Part of mode conformance for access types is having the same
5505 -- constant modifier.
5507 elsif Access_Types_Match
5508 and then Is_Access_Constant (Old_Formal_Base) /=
5509 Is_Access_Constant (New_Formal_Base)
5512 ("\constant modifier does not match!", New_Formal);
5517 if Ctype >= Subtype_Conformant then
5519 -- Ada 2005 (AI-231): In case of anonymous access types check
5520 -- the null-exclusion and access-to-constant attributes must
5521 -- match. For null exclusion, we test the types rather than the
5522 -- formals themselves, since the attribute is only set reliably
5523 -- on the formals in the Ada 95 case, and we exclude the case
5524 -- where Old_Formal is marked as controlling, to avoid errors
5525 -- when matching completing bodies with dispatching declarations
5526 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5528 if Ada_Version >= Ada_2005
5529 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5530 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5532 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5533 Can_Never_Be_Null (Etype (New_Formal))
5535 not Is_Controlling_Formal (Old_Formal))
5537 Is_Access_Constant (Etype (Old_Formal)) /=
5538 Is_Access_Constant (Etype (New_Formal)))
5540 -- Do not complain if error already posted on New_Formal. This
5541 -- avoids some redundant error messages.
5543 and then not Error_Posted (New_Formal)
5545 -- It is allowed to omit the null-exclusion in case of stream
5546 -- attribute subprograms. We recognize stream subprograms
5547 -- through their TSS-generated suffix.
5550 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5553 if TSS_Name /= TSS_Stream_Read
5554 and then TSS_Name /= TSS_Stream_Write
5555 and then TSS_Name /= TSS_Stream_Input
5556 and then TSS_Name /= TSS_Stream_Output
5558 -- Here we have a definite conformance error. It is worth
5559 -- special casing the error message for the case of a
5560 -- controlling formal (which excludes null).
5562 if Is_Controlling_Formal (New_Formal) then
5563 Error_Msg_Node_2 := Scope (New_Formal);
5565 ("\controlling formal & of & excludes null, "
5566 & "declaration must exclude null as well",
5569 -- Normal case (couldn't we give more detail here???)
5573 ("\type of & does not match!", New_Formal);
5582 -- Full conformance checks
5584 if Ctype = Fully_Conformant then
5586 -- We have checked already that names match
5588 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5590 -- Check default expressions for in parameters
5593 NewD : constant Boolean :=
5594 Present (Default_Value (New_Formal));
5595 OldD : constant Boolean :=
5596 Present (Default_Value (Old_Formal));
5598 if NewD or OldD then
5600 -- The old default value has been analyzed because the
5601 -- current full declaration will have frozen everything
5602 -- before. The new default value has not been analyzed,
5603 -- so analyze it now before we check for conformance.
5606 Push_Scope (New_Id);
5607 Preanalyze_Spec_Expression
5608 (Default_Value (New_Formal), Etype (New_Formal));
5612 if not (NewD and OldD)
5613 or else not Fully_Conformant_Expressions
5614 (Default_Value (Old_Formal),
5615 Default_Value (New_Formal))
5618 ("\default expression for & does not match!",
5627 -- A couple of special checks for Ada 83 mode. These checks are
5628 -- skipped if either entity is an operator in package Standard,
5629 -- or if either old or new instance is not from the source program.
5631 if Ada_Version = Ada_83
5632 and then Sloc (Old_Id) > Standard_Location
5633 and then Sloc (New_Id) > Standard_Location
5634 and then Comes_From_Source (Old_Id)
5635 and then Comes_From_Source (New_Id)
5638 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5639 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5642 -- Explicit IN must be present or absent in both cases. This
5643 -- test is required only in the full conformance case.
5645 if In_Present (Old_Param) /= In_Present (New_Param)
5646 and then Ctype = Fully_Conformant
5649 ("\(Ada 83) IN must appear in both declarations",
5654 -- Grouping (use of comma in param lists) must be the same
5655 -- This is where we catch a misconformance like:
5658 -- A : Integer; B : Integer
5660 -- which are represented identically in the tree except
5661 -- for the setting of the flags More_Ids and Prev_Ids.
5663 if More_Ids (Old_Param) /= More_Ids (New_Param)
5664 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5667 ("\grouping of & does not match!", New_Formal);
5673 -- This label is required when skipping controlling formals
5675 <<Skip_Controlling_Formal>>
5677 Next_Formal (Old_Formal);
5678 Next_Formal (New_Formal);
5681 if Present (Old_Formal) then
5682 Conformance_Error ("\too few parameters!");
5685 elsif Present (New_Formal) then
5686 Conformance_Error ("\too many parameters!", New_Formal);
5689 end Check_Conformance;
5691 -----------------------
5692 -- Check_Conventions --
5693 -----------------------
5695 procedure Check_Conventions (Typ : Entity_Id) is
5696 Ifaces_List : Elist_Id;
5698 procedure Check_Convention (Op : Entity_Id);
5699 -- Verify that the convention of inherited dispatching operation Op is
5700 -- consistent among all subprograms it overrides. In order to minimize
5701 -- the search, Search_From is utilized to designate a specific point in
5702 -- the list rather than iterating over the whole list once more.
5704 ----------------------
5705 -- Check_Convention --
5706 ----------------------
5708 procedure Check_Convention (Op : Entity_Id) is
5709 Op_Conv : constant Convention_Id := Convention (Op);
5710 Iface_Conv : Convention_Id;
5711 Iface_Elmt : Elmt_Id;
5712 Iface_Prim_Elmt : Elmt_Id;
5713 Iface_Prim : Entity_Id;
5716 Iface_Elmt := First_Elmt (Ifaces_List);
5717 while Present (Iface_Elmt) loop
5719 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5720 while Present (Iface_Prim_Elmt) loop
5721 Iface_Prim := Node (Iface_Prim_Elmt);
5722 Iface_Conv := Convention (Iface_Prim);
5724 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5725 and then Iface_Conv /= Op_Conv
5728 ("inconsistent conventions in primitive operations", Typ);
5730 Error_Msg_Name_1 := Chars (Op);
5731 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5732 Error_Msg_Sloc := Sloc (Op);
5734 if Comes_From_Source (Op) or else No (Alias (Op)) then
5735 if not Present (Overridden_Operation (Op)) then
5736 Error_Msg_N ("\\primitive % defined #", Typ);
5739 ("\\overriding operation % with "
5740 & "convention % defined #", Typ);
5743 else pragma Assert (Present (Alias (Op)));
5744 Error_Msg_Sloc := Sloc (Alias (Op));
5745 Error_Msg_N ("\\inherited operation % with "
5746 & "convention % defined #", Typ);
5749 Error_Msg_Name_1 := Chars (Op);
5750 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5751 Error_Msg_Sloc := Sloc (Iface_Prim);
5752 Error_Msg_N ("\\overridden operation % with "
5753 & "convention % defined #", Typ);
5755 -- Avoid cascading errors
5760 Next_Elmt (Iface_Prim_Elmt);
5763 Next_Elmt (Iface_Elmt);
5765 end Check_Convention;
5769 Prim_Op : Entity_Id;
5770 Prim_Op_Elmt : Elmt_Id;
5772 -- Start of processing for Check_Conventions
5775 if not Has_Interfaces (Typ) then
5779 Collect_Interfaces (Typ, Ifaces_List);
5781 -- The algorithm checks every overriding dispatching operation against
5782 -- all the corresponding overridden dispatching operations, detecting
5783 -- differences in conventions.
5785 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5786 while Present (Prim_Op_Elmt) loop
5787 Prim_Op := Node (Prim_Op_Elmt);
5789 -- A small optimization: skip the predefined dispatching operations
5790 -- since they always have the same convention.
5792 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5793 Check_Convention (Prim_Op);
5796 Next_Elmt (Prim_Op_Elmt);
5798 end Check_Conventions;
5800 ------------------------------
5801 -- Check_Delayed_Subprogram --
5802 ------------------------------
5804 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5807 procedure Possible_Freeze (T : Entity_Id);
5808 -- T is the type of either a formal parameter or of the return type.
5809 -- If T is not yet frozen and needs a delayed freeze, then the
5810 -- subprogram itself must be delayed.
5812 ---------------------
5813 -- Possible_Freeze --
5814 ---------------------
5816 procedure Possible_Freeze (T : Entity_Id) is
5818 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5819 Set_Has_Delayed_Freeze (Designator);
5821 elsif Is_Access_Type (T)
5822 and then Has_Delayed_Freeze (Designated_Type (T))
5823 and then not Is_Frozen (Designated_Type (T))
5825 Set_Has_Delayed_Freeze (Designator);
5828 end Possible_Freeze;
5830 -- Start of processing for Check_Delayed_Subprogram
5833 -- All subprograms, including abstract subprograms, may need a freeze
5834 -- node if some formal type or the return type needs one.
5836 Possible_Freeze (Etype (Designator));
5837 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5839 -- Need delayed freeze if any of the formal types themselves need
5840 -- a delayed freeze and are not yet frozen.
5842 F := First_Formal (Designator);
5843 while Present (F) loop
5844 Possible_Freeze (Etype (F));
5845 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5849 -- Mark functions that return by reference. Note that it cannot be
5850 -- done for delayed_freeze subprograms because the underlying
5851 -- returned type may not be known yet (for private types)
5853 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5855 Typ : constant Entity_Id := Etype (Designator);
5856 Utyp : constant Entity_Id := Underlying_Type (Typ);
5858 if Is_Limited_View (Typ) then
5859 Set_Returns_By_Ref (Designator);
5860 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5861 Set_Returns_By_Ref (Designator);
5865 end Check_Delayed_Subprogram;
5867 ------------------------------------
5868 -- Check_Discriminant_Conformance --
5869 ------------------------------------
5871 procedure Check_Discriminant_Conformance
5876 Old_Discr : Entity_Id := First_Discriminant (Prev);
5877 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5878 New_Discr_Id : Entity_Id;
5879 New_Discr_Type : Entity_Id;
5881 procedure Conformance_Error (Msg : String; N : Node_Id);
5882 -- Post error message for conformance error on given node. Two messages
5883 -- are output. The first points to the previous declaration with a
5884 -- general "no conformance" message. The second is the detailed reason,
5885 -- supplied as Msg. The parameter N provide information for a possible
5886 -- & insertion in the message.
5888 -----------------------
5889 -- Conformance_Error --
5890 -----------------------
5892 procedure Conformance_Error (Msg : String; N : Node_Id) is
5894 Error_Msg_Sloc := Sloc (Prev_Loc);
5895 Error_Msg_N -- CODEFIX
5896 ("not fully conformant with declaration#!", N);
5897 Error_Msg_NE (Msg, N, N);
5898 end Conformance_Error;
5900 -- Start of processing for Check_Discriminant_Conformance
5903 while Present (Old_Discr) and then Present (New_Discr) loop
5904 New_Discr_Id := Defining_Identifier (New_Discr);
5906 -- The subtype mark of the discriminant on the full type has not
5907 -- been analyzed so we do it here. For an access discriminant a new
5910 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5912 Access_Definition (N, Discriminant_Type (New_Discr));
5915 Analyze (Discriminant_Type (New_Discr));
5916 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5918 -- Ada 2005: if the discriminant definition carries a null
5919 -- exclusion, create an itype to check properly for consistency
5920 -- with partial declaration.
5922 if Is_Access_Type (New_Discr_Type)
5923 and then Null_Exclusion_Present (New_Discr)
5926 Create_Null_Excluding_Itype
5927 (T => New_Discr_Type,
5928 Related_Nod => New_Discr,
5929 Scope_Id => Current_Scope);
5933 if not Conforming_Types
5934 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5936 Conformance_Error ("type of & does not match!", New_Discr_Id);
5939 -- Treat the new discriminant as an occurrence of the old one,
5940 -- for navigation purposes, and fill in some semantic
5941 -- information, for completeness.
5943 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5944 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5945 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5950 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5951 Conformance_Error ("name & does not match!", New_Discr_Id);
5955 -- Default expressions must match
5958 NewD : constant Boolean :=
5959 Present (Expression (New_Discr));
5960 OldD : constant Boolean :=
5961 Present (Expression (Parent (Old_Discr)));
5964 if NewD or OldD then
5966 -- The old default value has been analyzed and expanded,
5967 -- because the current full declaration will have frozen
5968 -- everything before. The new default values have not been
5969 -- expanded, so expand now to check conformance.
5972 Preanalyze_Spec_Expression
5973 (Expression (New_Discr), New_Discr_Type);
5976 if not (NewD and OldD)
5977 or else not Fully_Conformant_Expressions
5978 (Expression (Parent (Old_Discr)),
5979 Expression (New_Discr))
5983 ("default expression for & does not match!",
5990 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5992 if Ada_Version = Ada_83 then
5994 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5997 -- Grouping (use of comma in param lists) must be the same
5998 -- This is where we catch a misconformance like:
6001 -- A : Integer; B : Integer
6003 -- which are represented identically in the tree except
6004 -- for the setting of the flags More_Ids and Prev_Ids.
6006 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6007 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6010 ("grouping of & does not match!", New_Discr_Id);
6016 Next_Discriminant (Old_Discr);
6020 if Present (Old_Discr) then
6021 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6024 elsif Present (New_Discr) then
6026 ("too many discriminants!", Defining_Identifier (New_Discr));
6029 end Check_Discriminant_Conformance;
6031 ----------------------------
6032 -- Check_Fully_Conformant --
6033 ----------------------------
6035 procedure Check_Fully_Conformant
6036 (New_Id : Entity_Id;
6038 Err_Loc : Node_Id := Empty)
6041 pragma Warnings (Off, Result);
6044 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6045 end Check_Fully_Conformant;
6047 --------------------------
6048 -- Check_Limited_Return --
6049 --------------------------
6051 procedure Check_Limited_Return
6057 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6058 -- replaced by anonymous access results. This is an incompatibility with
6059 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6060 -- controllable with special switch. ???
6062 -- A limited interface that is not immutably limited is OK
6064 if Is_Limited_Interface (R_Type)
6066 not (Is_Task_Interface (R_Type)
6067 or else Is_Protected_Interface (R_Type)
6068 or else Is_Synchronized_Interface (R_Type))
6072 elsif Is_Limited_Type (R_Type)
6073 and then not Is_Interface (R_Type)
6074 and then Comes_From_Source (N)
6075 and then not In_Instance_Body
6076 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6078 -- Error in Ada 2005
6080 if Ada_Version >= Ada_2005
6081 and then not Debug_Flag_Dot_L
6082 and then not GNAT_Mode
6085 ("(Ada 2005) cannot copy object of a limited type "
6086 & "(RM-2005 6.5(5.5/2))", Expr);
6088 if Is_Limited_View (R_Type) then
6090 ("\return by reference not permitted in Ada 2005", Expr);
6093 -- Warn in Ada 95 mode, to give folks a heads up about this
6096 -- In GNAT mode, this is just a warning, to allow it to be evilly
6097 -- turned off. Otherwise it is a real error.
6099 -- In a generic context, simplify the warning because it makes no
6100 -- sense to discuss pass-by-reference or copy.
6102 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6103 if Inside_A_Generic then
6105 ("return of limited object not permitted in Ada 2005 "
6106 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6108 elsif Is_Limited_View (R_Type) then
6110 ("return by reference not permitted in Ada 2005 "
6111 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6114 ("cannot copy object of a limited type in Ada 2005 "
6115 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6118 -- Ada 95 mode, and compatibility warnings disabled
6121 pragma Assert (Ada_Version <= Ada_95);
6122 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6123 return; -- skip continuation messages below
6126 if not Inside_A_Generic then
6128 ("\consider switching to return of access type", Expr);
6129 Explain_Limited_Type (R_Type, Expr);
6132 end Check_Limited_Return;
6134 ---------------------------
6135 -- Check_Mode_Conformant --
6136 ---------------------------
6138 procedure Check_Mode_Conformant
6139 (New_Id : Entity_Id;
6141 Err_Loc : Node_Id := Empty;
6142 Get_Inst : Boolean := False)
6145 pragma Warnings (Off, Result);
6148 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6149 end Check_Mode_Conformant;
6151 --------------------------------
6152 -- Check_Overriding_Indicator --
6153 --------------------------------
6155 procedure Check_Overriding_Indicator
6157 Overridden_Subp : Entity_Id;
6158 Is_Primitive : Boolean)
6164 -- No overriding indicator for literals
6166 if Ekind (Subp) = E_Enumeration_Literal then
6169 elsif Ekind (Subp) = E_Entry then
6170 Decl := Parent (Subp);
6172 -- No point in analyzing a malformed operator
6174 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6175 and then Error_Posted (Subp)
6180 Decl := Unit_Declaration_Node (Subp);
6183 if Nkind_In (Decl, N_Subprogram_Body,
6184 N_Subprogram_Body_Stub,
6185 N_Subprogram_Declaration,
6186 N_Abstract_Subprogram_Declaration,
6187 N_Subprogram_Renaming_Declaration)
6189 Spec := Specification (Decl);
6191 elsif Nkind (Decl) = N_Entry_Declaration then
6198 -- The overriding operation is type conformant with the overridden one,
6199 -- but the names of the formals are not required to match. If the names
6200 -- appear permuted in the overriding operation, this is a possible
6201 -- source of confusion that is worth diagnosing. Controlling formals
6202 -- often carry names that reflect the type, and it is not worthwhile
6203 -- requiring that their names match.
6205 if Present (Overridden_Subp)
6206 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6213 Form1 := First_Formal (Subp);
6214 Form2 := First_Formal (Overridden_Subp);
6216 -- If the overriding operation is a synchronized operation, skip
6217 -- the first parameter of the overridden operation, which is
6218 -- implicit in the new one. If the operation is declared in the
6219 -- body it is not primitive and all formals must match.
6221 if Is_Concurrent_Type (Scope (Subp))
6222 and then Is_Tagged_Type (Scope (Subp))
6223 and then not Has_Completion (Scope (Subp))
6225 Form2 := Next_Formal (Form2);
6228 if Present (Form1) then
6229 Form1 := Next_Formal (Form1);
6230 Form2 := Next_Formal (Form2);
6233 while Present (Form1) loop
6234 if not Is_Controlling_Formal (Form1)
6235 and then Present (Next_Formal (Form2))
6236 and then Chars (Form1) = Chars (Next_Formal (Form2))
6238 Error_Msg_Node_2 := Alias (Overridden_Subp);
6239 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6241 ("& does not match corresponding formal of&#",
6246 Next_Formal (Form1);
6247 Next_Formal (Form2);
6252 -- If there is an overridden subprogram, then check that there is no
6253 -- "not overriding" indicator, and mark the subprogram as overriding.
6254 -- This is not done if the overridden subprogram is marked as hidden,
6255 -- which can occur for the case of inherited controlled operations
6256 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6257 -- subprogram is not itself hidden. (Note: This condition could probably
6258 -- be simplified, leaving out the testing for the specific controlled
6259 -- cases, but it seems safer and clearer this way, and echoes similar
6260 -- special-case tests of this kind in other places.)
6262 if Present (Overridden_Subp)
6263 and then (not Is_Hidden (Overridden_Subp)
6265 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6268 and then Present (Alias (Overridden_Subp))
6269 and then not Is_Hidden (Alias (Overridden_Subp))))
6271 if Must_Not_Override (Spec) then
6272 Error_Msg_Sloc := Sloc (Overridden_Subp);
6274 if Ekind (Subp) = E_Entry then
6276 ("entry & overrides inherited operation #", Spec, Subp);
6279 ("subprogram & overrides inherited operation #", Spec, Subp);
6282 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6283 -- as an extension of Root_Controlled, and thus has a useless Adjust
6284 -- operation. This operation should not be inherited by other limited
6285 -- controlled types. An explicit Adjust for them is not overriding.
6287 elsif Must_Override (Spec)
6288 and then Chars (Overridden_Subp) = Name_Adjust
6289 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6290 and then Present (Alias (Overridden_Subp))
6291 and then In_Predefined_Unit (Alias (Overridden_Subp))
6294 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6295 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6297 elsif Is_Subprogram (Subp) then
6298 if Is_Init_Proc (Subp) then
6301 elsif No (Overridden_Operation (Subp)) then
6303 -- For entities generated by Derive_Subprograms the overridden
6304 -- operation is the inherited primitive (which is available
6305 -- through the attribute alias)
6307 if (Is_Dispatching_Operation (Subp)
6308 or else Is_Dispatching_Operation (Overridden_Subp))
6309 and then not Comes_From_Source (Overridden_Subp)
6310 and then Find_Dispatching_Type (Overridden_Subp) =
6311 Find_Dispatching_Type (Subp)
6312 and then Present (Alias (Overridden_Subp))
6313 and then Comes_From_Source (Alias (Overridden_Subp))
6315 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6316 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6319 Set_Overridden_Operation (Subp, Overridden_Subp);
6320 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6325 -- If primitive flag is set or this is a protected operation, then
6326 -- the operation is overriding at the point of its declaration, so
6327 -- warn if necessary. Otherwise it may have been declared before the
6328 -- operation it overrides and no check is required.
6331 and then not Must_Override (Spec)
6332 and then (Is_Primitive
6333 or else Ekind (Scope (Subp)) = E_Protected_Type)
6335 Style.Missing_Overriding (Decl, Subp);
6338 -- If Subp is an operator, it may override a predefined operation, if
6339 -- it is defined in the same scope as the type to which it applies.
6340 -- In that case Overridden_Subp is empty because of our implicit
6341 -- representation for predefined operators. We have to check whether the
6342 -- signature of Subp matches that of a predefined operator. Note that
6343 -- first argument provides the name of the operator, and the second
6344 -- argument the signature that may match that of a standard operation.
6345 -- If the indicator is overriding, then the operator must match a
6346 -- predefined signature, because we know already that there is no
6347 -- explicit overridden operation.
6349 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6350 if Must_Not_Override (Spec) then
6352 -- If this is not a primitive or a protected subprogram, then
6353 -- "not overriding" is illegal.
6356 and then Ekind (Scope (Subp)) /= E_Protected_Type
6358 Error_Msg_N ("overriding indicator only allowed "
6359 & "if subprogram is primitive", Subp);
6361 elsif Can_Override_Operator (Subp) then
6363 ("subprogram& overrides predefined operator ", Spec, Subp);
6366 elsif Must_Override (Spec) then
6367 if No (Overridden_Operation (Subp))
6368 and then not Can_Override_Operator (Subp)
6370 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6373 elsif not Error_Posted (Subp)
6374 and then Style_Check
6375 and then Can_Override_Operator (Subp)
6376 and then not In_Predefined_Unit (Subp)
6378 -- If style checks are enabled, indicate that the indicator is
6379 -- missing. However, at the point of declaration, the type of
6380 -- which this is a primitive operation may be private, in which
6381 -- case the indicator would be premature.
6383 if Has_Private_Declaration (Etype (Subp))
6384 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6388 Style.Missing_Overriding (Decl, Subp);
6392 elsif Must_Override (Spec) then
6393 if Ekind (Subp) = E_Entry then
6394 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6396 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6399 -- If the operation is marked "not overriding" and it's not primitive
6400 -- then an error is issued, unless this is an operation of a task or
6401 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6402 -- has been specified have already been checked above.
6404 elsif Must_Not_Override (Spec)
6405 and then not Is_Primitive
6406 and then Ekind (Subp) /= E_Entry
6407 and then Ekind (Scope (Subp)) /= E_Protected_Type
6410 ("overriding indicator only allowed if subprogram is primitive",
6414 end Check_Overriding_Indicator;
6420 -- Note: this procedure needs to know far too much about how the expander
6421 -- messes with exceptions. The use of the flag Exception_Junk and the
6422 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6423 -- works, but is not very clean. It would be better if the expansion
6424 -- routines would leave Original_Node working nicely, and we could use
6425 -- Original_Node here to ignore all the peculiar expander messing ???
6427 procedure Check_Returns
6431 Proc : Entity_Id := Empty)
6435 procedure Check_Statement_Sequence (L : List_Id);
6436 -- Internal recursive procedure to check a list of statements for proper
6437 -- termination by a return statement (or a transfer of control or a
6438 -- compound statement that is itself internally properly terminated).
6440 ------------------------------
6441 -- Check_Statement_Sequence --
6442 ------------------------------
6444 procedure Check_Statement_Sequence (L : List_Id) is
6449 function Assert_False return Boolean;
6450 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6451 -- rewritten as a null statement when assertions are off. The assert
6452 -- is not active, but it is still enough to kill the warning.
6458 function Assert_False return Boolean is
6459 Orig : constant Node_Id := Original_Node (Last_Stm);
6462 if Nkind (Orig) = N_Pragma
6463 and then Pragma_Name (Orig) = Name_Assert
6464 and then not Error_Posted (Orig)
6467 Arg : constant Node_Id :=
6468 First (Pragma_Argument_Associations (Orig));
6469 Exp : constant Node_Id := Expression (Arg);
6471 return Nkind (Exp) = N_Identifier
6472 and then Chars (Exp) = Name_False;
6482 Raise_Exception_Call : Boolean;
6483 -- Set True if statement sequence terminated by Raise_Exception call
6484 -- or a Reraise_Occurrence call.
6486 -- Start of processing for Check_Statement_Sequence
6489 Raise_Exception_Call := False;
6491 -- Get last real statement
6493 Last_Stm := Last (L);
6495 -- Deal with digging out exception handler statement sequences that
6496 -- have been transformed by the local raise to goto optimization.
6497 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6498 -- optimization has occurred, we are looking at something like:
6501 -- original stmts in block
6505 -- goto L1; | omitted if No_Exception_Propagation
6510 -- goto L3; -- skip handler when exception not raised
6512 -- <<L1>> -- target label for local exception
6526 -- and what we have to do is to dig out the estmts1 and estmts2
6527 -- sequences (which were the original sequences of statements in
6528 -- the exception handlers) and check them.
6530 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6535 exit when Nkind (Stm) /= N_Block_Statement;
6536 exit when not Exception_Junk (Stm);
6539 exit when Nkind (Stm) /= N_Label;
6540 exit when not Exception_Junk (Stm);
6541 Check_Statement_Sequence
6542 (Statements (Handled_Statement_Sequence (Next (Stm))));
6547 exit when Nkind (Stm) /= N_Goto_Statement;
6548 exit when not Exception_Junk (Stm);
6552 -- Don't count pragmas
6554 while Nkind (Last_Stm) = N_Pragma
6556 -- Don't count call to SS_Release (can happen after Raise_Exception)
6559 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6561 Nkind (Name (Last_Stm)) = N_Identifier
6563 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6565 -- Don't count exception junk
6568 (Nkind_In (Last_Stm, N_Goto_Statement,
6570 N_Object_Declaration)
6571 and then Exception_Junk (Last_Stm))
6572 or else Nkind (Last_Stm) in N_Push_xxx_Label
6573 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6575 -- Inserted code, such as finalization calls, is irrelevant: we only
6576 -- need to check original source.
6578 or else Is_Rewrite_Insertion (Last_Stm)
6583 -- Here we have the "real" last statement
6585 Kind := Nkind (Last_Stm);
6587 -- Transfer of control, OK. Note that in the No_Return procedure
6588 -- case, we already diagnosed any explicit return statements, so
6589 -- we can treat them as OK in this context.
6591 if Is_Transfer (Last_Stm) then
6594 -- Check cases of explicit non-indirect procedure calls
6596 elsif Kind = N_Procedure_Call_Statement
6597 and then Is_Entity_Name (Name (Last_Stm))
6599 -- Check call to Raise_Exception procedure which is treated
6600 -- specially, as is a call to Reraise_Occurrence.
6602 -- We suppress the warning in these cases since it is likely that
6603 -- the programmer really does not expect to deal with the case
6604 -- of Null_Occurrence, and thus would find a warning about a
6605 -- missing return curious, and raising Program_Error does not
6606 -- seem such a bad behavior if this does occur.
6608 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6609 -- behavior will be to raise Constraint_Error (see AI-329).
6611 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6613 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6615 Raise_Exception_Call := True;
6617 -- For Raise_Exception call, test first argument, if it is
6618 -- an attribute reference for a 'Identity call, then we know
6619 -- that the call cannot possibly return.
6622 Arg : constant Node_Id :=
6623 Original_Node (First_Actual (Last_Stm));
6625 if Nkind (Arg) = N_Attribute_Reference
6626 and then Attribute_Name (Arg) = Name_Identity
6633 -- If statement, need to look inside if there is an else and check
6634 -- each constituent statement sequence for proper termination.
6636 elsif Kind = N_If_Statement
6637 and then Present (Else_Statements (Last_Stm))
6639 Check_Statement_Sequence (Then_Statements (Last_Stm));
6640 Check_Statement_Sequence (Else_Statements (Last_Stm));
6642 if Present (Elsif_Parts (Last_Stm)) then
6644 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6647 while Present (Elsif_Part) loop
6648 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6656 -- Case statement, check each case for proper termination
6658 elsif Kind = N_Case_Statement then
6662 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6663 while Present (Case_Alt) loop
6664 Check_Statement_Sequence (Statements (Case_Alt));
6665 Next_Non_Pragma (Case_Alt);
6671 -- Block statement, check its handled sequence of statements
6673 elsif Kind = N_Block_Statement then
6679 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6688 -- Loop statement. If there is an iteration scheme, we can definitely
6689 -- fall out of the loop. Similarly if there is an exit statement, we
6690 -- can fall out. In either case we need a following return.
6692 elsif Kind = N_Loop_Statement then
6693 if Present (Iteration_Scheme (Last_Stm))
6694 or else Has_Exit (Entity (Identifier (Last_Stm)))
6698 -- A loop with no exit statement or iteration scheme is either
6699 -- an infinite loop, or it has some other exit (raise/return).
6700 -- In either case, no warning is required.
6706 -- Timed entry call, check entry call and delay alternatives
6708 -- Note: in expanded code, the timed entry call has been converted
6709 -- to a set of expanded statements on which the check will work
6710 -- correctly in any case.
6712 elsif Kind = N_Timed_Entry_Call then
6714 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6715 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6718 -- If statement sequence of entry call alternative is missing,
6719 -- then we can definitely fall through, and we post the error
6720 -- message on the entry call alternative itself.
6722 if No (Statements (ECA)) then
6725 -- If statement sequence of delay alternative is missing, then
6726 -- we can definitely fall through, and we post the error
6727 -- message on the delay alternative itself.
6729 -- Note: if both ECA and DCA are missing the return, then we
6730 -- post only one message, should be enough to fix the bugs.
6731 -- If not we will get a message next time on the DCA when the
6734 elsif No (Statements (DCA)) then
6737 -- Else check both statement sequences
6740 Check_Statement_Sequence (Statements (ECA));
6741 Check_Statement_Sequence (Statements (DCA));
6746 -- Conditional entry call, check entry call and else part
6748 -- Note: in expanded code, the conditional entry call has been
6749 -- converted to a set of expanded statements on which the check
6750 -- will work correctly in any case.
6752 elsif Kind = N_Conditional_Entry_Call then
6754 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6757 -- If statement sequence of entry call alternative is missing,
6758 -- then we can definitely fall through, and we post the error
6759 -- message on the entry call alternative itself.
6761 if No (Statements (ECA)) then
6764 -- Else check statement sequence and else part
6767 Check_Statement_Sequence (Statements (ECA));
6768 Check_Statement_Sequence (Else_Statements (Last_Stm));
6774 -- If we fall through, issue appropriate message
6778 -- Kill warning if last statement is a raise exception call,
6779 -- or a pragma Assert (False). Note that with assertions enabled,
6780 -- such a pragma has been converted into a raise exception call
6781 -- already, so the Assert_False is for the assertions off case.
6783 if not Raise_Exception_Call and then not Assert_False then
6785 -- In GNATprove mode, it is an error to have a missing return
6787 Error_Msg_Warn := SPARK_Mode /= On;
6789 -- Issue error message or warning
6792 ("RETURN statement missing following this statement<<!",
6795 ("\Program_Error ]<<!", Last_Stm);
6798 -- Note: we set Err even though we have not issued a warning
6799 -- because we still have a case of a missing return. This is
6800 -- an extremely marginal case, probably will never be noticed
6801 -- but we might as well get it right.
6805 -- Otherwise we have the case of a procedure marked No_Return
6808 if not Raise_Exception_Call then
6809 if GNATprove_Mode then
6811 ("implied return after this statement would have raised "
6812 & "Program_Error", Last_Stm);
6814 -- In normal compilation mode, do not warn on a generated call
6815 -- (e.g. in the body of a renaming as completion).
6817 elsif Comes_From_Source (Last_Stm) then
6819 ("implied return after this statement will raise "
6820 & "Program_Error??", Last_Stm);
6823 Error_Msg_Warn := SPARK_Mode /= On;
6825 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6829 RE : constant Node_Id :=
6830 Make_Raise_Program_Error (Sloc (Last_Stm),
6831 Reason => PE_Implicit_Return);
6833 Insert_After (Last_Stm, RE);
6837 end Check_Statement_Sequence;
6839 -- Start of processing for Check_Returns
6843 Check_Statement_Sequence (Statements (HSS));
6845 if Present (Exception_Handlers (HSS)) then
6846 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6847 while Present (Handler) loop
6848 Check_Statement_Sequence (Statements (Handler));
6849 Next_Non_Pragma (Handler);
6854 ----------------------------
6855 -- Check_Subprogram_Order --
6856 ----------------------------
6858 procedure Check_Subprogram_Order (N : Node_Id) is
6860 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6861 -- This is used to check if S1 > S2 in the sense required by this test,
6862 -- for example nameab < namec, but name2 < name10.
6864 -----------------------------
6865 -- Subprogram_Name_Greater --
6866 -----------------------------
6868 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6873 -- Deal with special case where names are identical except for a
6874 -- numerical suffix. These are handled specially, taking the numeric
6875 -- ordering from the suffix into account.
6878 while S1 (L1) in '0' .. '9' loop
6883 while S2 (L2) in '0' .. '9' loop
6887 -- If non-numeric parts non-equal, do straight compare
6889 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6892 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6893 -- that a missing suffix is treated as numeric zero in this test.
6897 while L1 < S1'Last loop
6899 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6903 while L2 < S2'Last loop
6905 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6910 end Subprogram_Name_Greater;
6912 -- Start of processing for Check_Subprogram_Order
6915 -- Check body in alpha order if this is option
6918 and then Style_Check_Order_Subprograms
6919 and then Nkind (N) = N_Subprogram_Body
6920 and then Comes_From_Source (N)
6921 and then In_Extended_Main_Source_Unit (N)
6925 renames Scope_Stack.Table
6926 (Scope_Stack.Last).Last_Subprogram_Name;
6928 Body_Id : constant Entity_Id :=
6929 Defining_Entity (Specification (N));
6932 Get_Decoded_Name_String (Chars (Body_Id));
6935 if Subprogram_Name_Greater
6936 (LSN.all, Name_Buffer (1 .. Name_Len))
6938 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6944 LSN := new String'(Name_Buffer (1 .. Name_Len));
6947 end Check_Subprogram_Order;
6949 ------------------------------
6950 -- Check_Subtype_Conformant --
6951 ------------------------------
6953 procedure Check_Subtype_Conformant
6954 (New_Id : Entity_Id;
6956 Err_Loc : Node_Id := Empty;
6957 Skip_Controlling_Formals : Boolean := False;
6958 Get_Inst : Boolean := False)
6961 pragma Warnings (Off, Result);
6964 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6965 Skip_Controlling_Formals => Skip_Controlling_Formals,
6966 Get_Inst => Get_Inst);
6967 end Check_Subtype_Conformant;
6969 -----------------------------------
6970 -- Check_Synchronized_Overriding --
6971 -----------------------------------
6973 procedure Check_Synchronized_Overriding
6974 (Def_Id : Entity_Id;
6975 Overridden_Subp : out Entity_Id)
6977 Ifaces_List : Elist_Id;
6981 function Matches_Prefixed_View_Profile
6982 (Prim_Params : List_Id;
6983 Iface_Params : List_Id) return Boolean;
6984 -- Determine whether a subprogram's parameter profile Prim_Params
6985 -- matches that of a potentially overridden interface subprogram
6986 -- Iface_Params. Also determine if the type of first parameter of
6987 -- Iface_Params is an implemented interface.
6989 -----------------------------------
6990 -- Matches_Prefixed_View_Profile --
6991 -----------------------------------
6993 function Matches_Prefixed_View_Profile
6994 (Prim_Params : List_Id;
6995 Iface_Params : List_Id) return Boolean
6997 function Is_Implemented
6998 (Ifaces_List : Elist_Id;
6999 Iface : Entity_Id) return Boolean;
7000 -- Determine if Iface is implemented by the current task or
7003 --------------------
7004 -- Is_Implemented --
7005 --------------------
7007 function Is_Implemented
7008 (Ifaces_List : Elist_Id;
7009 Iface : Entity_Id) return Boolean
7011 Iface_Elmt : Elmt_Id;
7014 Iface_Elmt := First_Elmt (Ifaces_List);
7015 while Present (Iface_Elmt) loop
7016 if Node (Iface_Elmt) = Iface then
7020 Next_Elmt (Iface_Elmt);
7028 Iface_Id : Entity_Id;
7029 Iface_Param : Node_Id;
7030 Iface_Typ : Entity_Id;
7031 Prim_Id : Entity_Id;
7032 Prim_Param : Node_Id;
7033 Prim_Typ : Entity_Id;
7035 -- Start of processing for Matches_Prefixed_View_Profile
7038 Iface_Param := First (Iface_Params);
7039 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7041 if Is_Access_Type (Iface_Typ) then
7042 Iface_Typ := Designated_Type (Iface_Typ);
7045 Prim_Param := First (Prim_Params);
7047 -- The first parameter of the potentially overridden subprogram must
7048 -- be an interface implemented by Prim.
7050 if not Is_Interface (Iface_Typ)
7051 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7056 -- The checks on the object parameters are done, so move on to the
7057 -- rest of the parameters.
7059 if not In_Scope then
7060 Prim_Param := Next (Prim_Param);
7063 Iface_Param := Next (Iface_Param);
7064 while Present (Iface_Param) and then Present (Prim_Param) loop
7065 Iface_Id := Defining_Identifier (Iface_Param);
7066 Iface_Typ := Find_Parameter_Type (Iface_Param);
7068 Prim_Id := Defining_Identifier (Prim_Param);
7069 Prim_Typ := Find_Parameter_Type (Prim_Param);
7071 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7072 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7073 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7075 Iface_Typ := Designated_Type (Iface_Typ);
7076 Prim_Typ := Designated_Type (Prim_Typ);
7079 -- Case of multiple interface types inside a parameter profile
7081 -- (Obj_Param : in out Iface; ...; Param : Iface)
7083 -- If the interface type is implemented, then the matching type in
7084 -- the primitive should be the implementing record type.
7086 if Ekind (Iface_Typ) = E_Record_Type
7087 and then Is_Interface (Iface_Typ)
7088 and then Is_Implemented (Ifaces_List, Iface_Typ)
7090 if Prim_Typ /= Typ then
7094 -- The two parameters must be both mode and subtype conformant
7096 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7098 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7107 -- One of the two lists contains more parameters than the other
7109 if Present (Iface_Param) or else Present (Prim_Param) then
7114 end Matches_Prefixed_View_Profile;
7116 -- Start of processing for Check_Synchronized_Overriding
7119 Overridden_Subp := Empty;
7121 -- Def_Id must be an entry or a subprogram. We should skip predefined
7122 -- primitives internally generated by the front end; however at this
7123 -- stage predefined primitives are still not fully decorated. As a
7124 -- minor optimization we skip here internally generated subprograms.
7126 if (Ekind (Def_Id) /= E_Entry
7127 and then Ekind (Def_Id) /= E_Function
7128 and then Ekind (Def_Id) /= E_Procedure)
7129 or else not Comes_From_Source (Def_Id)
7134 -- Search for the concurrent declaration since it contains the list of
7135 -- all implemented interfaces. In this case, the subprogram is declared
7136 -- within the scope of a protected or a task type.
7138 if Present (Scope (Def_Id))
7139 and then Is_Concurrent_Type (Scope (Def_Id))
7140 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7142 Typ := Scope (Def_Id);
7145 -- The enclosing scope is not a synchronized type and the subprogram
7148 elsif No (First_Formal (Def_Id)) then
7151 -- The subprogram has formals and hence it may be a primitive of a
7155 Typ := Etype (First_Formal (Def_Id));
7157 if Is_Access_Type (Typ) then
7158 Typ := Directly_Designated_Type (Typ);
7161 if Is_Concurrent_Type (Typ)
7162 and then not Is_Generic_Actual_Type (Typ)
7166 -- This case occurs when the concurrent type is declared within a
7167 -- generic unit. As a result the corresponding record has been built
7168 -- and used as the type of the first formal, we just have to retrieve
7169 -- the corresponding concurrent type.
7171 elsif Is_Concurrent_Record_Type (Typ)
7172 and then not Is_Class_Wide_Type (Typ)
7173 and then Present (Corresponding_Concurrent_Type (Typ))
7175 Typ := Corresponding_Concurrent_Type (Typ);
7183 -- There is no overriding to check if this is an inherited operation in
7184 -- a type derivation for a generic actual.
7186 Collect_Interfaces (Typ, Ifaces_List);
7188 if Is_Empty_Elmt_List (Ifaces_List) then
7192 -- Determine whether entry or subprogram Def_Id overrides a primitive
7193 -- operation that belongs to one of the interfaces in Ifaces_List.
7196 Candidate : Entity_Id := Empty;
7197 Hom : Entity_Id := Empty;
7198 Subp : Entity_Id := Empty;
7201 -- Traverse the homonym chain, looking for a potentially overridden
7202 -- subprogram that belongs to an implemented interface.
7204 Hom := Current_Entity_In_Scope (Def_Id);
7205 while Present (Hom) loop
7209 or else not Is_Overloadable (Subp)
7210 or else not Is_Primitive (Subp)
7211 or else not Is_Dispatching_Operation (Subp)
7212 or else not Present (Find_Dispatching_Type (Subp))
7213 or else not Is_Interface (Find_Dispatching_Type (Subp))
7217 -- Entries and procedures can override abstract or null interface
7220 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7221 and then Ekind (Subp) = E_Procedure
7222 and then Matches_Prefixed_View_Profile
7223 (Parameter_Specifications (Parent (Def_Id)),
7224 Parameter_Specifications (Parent (Subp)))
7228 -- For an overridden subprogram Subp, check whether the mode
7229 -- of its first parameter is correct depending on the kind of
7230 -- synchronized type.
7233 Formal : constant Node_Id := First_Formal (Candidate);
7236 -- In order for an entry or a protected procedure to
7237 -- override, the first parameter of the overridden routine
7238 -- must be of mode "out", "in out", or access-to-variable.
7240 if Ekind_In (Candidate, E_Entry, E_Procedure)
7241 and then Is_Protected_Type (Typ)
7242 and then Ekind (Formal) /= E_In_Out_Parameter
7243 and then Ekind (Formal) /= E_Out_Parameter
7244 and then Nkind (Parameter_Type (Parent (Formal))) /=
7249 -- All other cases are OK since a task entry or routine does
7250 -- not have a restriction on the mode of the first parameter
7251 -- of the overridden interface routine.
7254 Overridden_Subp := Candidate;
7259 -- Functions can override abstract interface functions
7261 elsif Ekind (Def_Id) = E_Function
7262 and then Ekind (Subp) = E_Function
7263 and then Matches_Prefixed_View_Profile
7264 (Parameter_Specifications (Parent (Def_Id)),
7265 Parameter_Specifications (Parent (Subp)))
7266 and then Etype (Def_Id) = Etype (Subp)
7270 -- If an inherited subprogram is implemented by a protected
7271 -- function, then the first parameter of the inherited
7272 -- subprogram shall be of mode in, but not an access-to-
7273 -- variable parameter (RM 9.4(11/9)).
7275 if Present (First_Formal (Subp))
7276 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7278 (not Is_Access_Type (Etype (First_Formal (Subp)))
7280 Is_Access_Constant (Etype (First_Formal (Subp))))
7282 Overridden_Subp := Subp;
7287 Hom := Homonym (Hom);
7290 -- After examining all candidates for overriding, we are left with
7291 -- the best match, which is a mode-incompatible interface routine.
7293 if In_Scope and then Present (Candidate) then
7294 Error_Msg_PT (Def_Id, Candidate);
7297 Overridden_Subp := Candidate;
7300 end Check_Synchronized_Overriding;
7302 ---------------------------
7303 -- Check_Type_Conformant --
7304 ---------------------------
7306 procedure Check_Type_Conformant
7307 (New_Id : Entity_Id;
7309 Err_Loc : Node_Id := Empty)
7312 pragma Warnings (Off, Result);
7315 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7316 end Check_Type_Conformant;
7318 ---------------------------
7319 -- Can_Override_Operator --
7320 ---------------------------
7322 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7326 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7330 Typ := Base_Type (Etype (First_Formal (Subp)));
7332 -- Check explicitly that the operation is a primitive of the type
7334 return Operator_Matches_Spec (Subp, Subp)
7335 and then not Is_Generic_Type (Typ)
7336 and then Scope (Subp) = Scope (Typ)
7337 and then not Is_Class_Wide_Type (Typ);
7339 end Can_Override_Operator;
7341 ----------------------
7342 -- Conforming_Types --
7343 ----------------------
7345 function Conforming_Types
7348 Ctype : Conformance_Type;
7349 Get_Inst : Boolean := False) return Boolean
7351 function Base_Types_Match
7353 Typ_2 : Entity_Id) return Boolean;
7354 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7355 -- in different scopes (e.g. parent and child instances), then verify
7356 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7357 -- the same subtype chain. The whole purpose of this procedure is to
7358 -- prevent spurious ambiguities in an instantiation that may arise if
7359 -- two distinct generic types are instantiated with the same actual.
7361 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7362 -- An access parameter can designate an incomplete type. If the
7363 -- incomplete type is the limited view of a type from a limited_
7364 -- with_clause, check whether the non-limited view is available.
7365 -- If it is a (non-limited) incomplete type, get the full view.
7367 function Matches_Limited_With_View
7369 Typ_2 : Entity_Id) return Boolean;
7370 -- Returns True if and only if either Typ_1 denotes a limited view of
7371 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7372 -- the limited with view of a type is used in a subprogram declaration
7373 -- and the subprogram body is in the scope of a regular with clause for
7374 -- the same unit. In such a case, the two type entities are considered
7375 -- identical for purposes of conformance checking.
7377 ----------------------
7378 -- Base_Types_Match --
7379 ----------------------
7381 function Base_Types_Match
7383 Typ_2 : Entity_Id) return Boolean
7385 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7386 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7389 if Typ_1 = Typ_2 then
7392 elsif Base_1 = Base_2 then
7394 -- The following is too permissive. A more precise test should
7395 -- check that the generic actual is an ancestor subtype of the
7398 -- See code in Find_Corresponding_Spec that applies an additional
7399 -- filter to handle accidental amiguities in instances.
7402 not Is_Generic_Actual_Type (Typ_1)
7403 or else not Is_Generic_Actual_Type (Typ_2)
7404 or else Scope (Typ_1) /= Scope (Typ_2);
7406 -- If Typ_2 is a generic actual type it is declared as the subtype of
7407 -- the actual. If that actual is itself a subtype we need to use its
7408 -- own base type to check for compatibility.
7410 elsif Ekind (Base_2) = Ekind (Typ_2)
7411 and then Base_1 = Base_Type (Base_2)
7415 elsif Ekind (Base_1) = Ekind (Typ_1)
7416 and then Base_2 = Base_Type (Base_1)
7423 end Base_Types_Match;
7425 --------------------------
7426 -- Find_Designated_Type --
7427 --------------------------
7429 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7433 Desig := Directly_Designated_Type (Typ);
7435 if Ekind (Desig) = E_Incomplete_Type then
7437 -- If regular incomplete type, get full view if available
7439 if Present (Full_View (Desig)) then
7440 Desig := Full_View (Desig);
7442 -- If limited view of a type, get non-limited view if available,
7443 -- and check again for a regular incomplete type.
7445 elsif Present (Non_Limited_View (Desig)) then
7446 Desig := Get_Full_View (Non_Limited_View (Desig));
7451 end Find_Designated_Type;
7453 -------------------------------
7454 -- Matches_Limited_With_View --
7455 -------------------------------
7457 function Matches_Limited_With_View
7459 Typ_2 : Entity_Id) return Boolean
7461 function Is_Matching_Limited_View
7463 View : Entity_Id) return Boolean;
7464 -- Determine whether non-limited view View denotes type Typ in some
7465 -- conformant fashion.
7467 ------------------------------
7468 -- Is_Matching_Limited_View --
7469 ------------------------------
7471 function Is_Matching_Limited_View
7473 View : Entity_Id) return Boolean
7475 Root_Typ : Entity_Id;
7476 Root_View : Entity_Id;
7479 -- The non-limited view directly denotes the type
7484 -- The type is a subtype of the non-limited view
7486 elsif Is_Subtype_Of (Typ, View) then
7489 -- Both the non-limited view and the type denote class-wide types
7491 elsif Is_Class_Wide_Type (Typ)
7492 and then Is_Class_Wide_Type (View)
7494 Root_Typ := Root_Type (Typ);
7495 Root_View := Root_Type (View);
7497 if Root_Typ = Root_View then
7500 -- An incomplete tagged type and its full view may receive two
7501 -- distinct class-wide types when the related package has not
7502 -- been analyzed yet.
7505 -- type T is tagged; -- CW_1
7506 -- type T is tagged null record; -- CW_2
7509 -- This is because the package lacks any semantic information
7510 -- that may eventually link both views of T. As a consequence,
7511 -- a client of the limited view of Pack will see CW_2 while a
7512 -- client of the non-limited view of Pack will see CW_1.
7514 elsif Is_Incomplete_Type (Root_Typ)
7515 and then Present (Full_View (Root_Typ))
7516 and then Full_View (Root_Typ) = Root_View
7520 elsif Is_Incomplete_Type (Root_View)
7521 and then Present (Full_View (Root_View))
7522 and then Full_View (Root_View) = Root_Typ
7529 end Is_Matching_Limited_View;
7531 -- Start of processing for Matches_Limited_With_View
7534 -- In some cases a type imported through a limited_with clause, and
7535 -- its non-limited view are both visible, for example in an anonymous
7536 -- access-to-class-wide type in a formal, or when building the body
7537 -- for a subprogram renaming after the subprogram has been frozen.
7538 -- In these cases both entities designate the same type. In addition,
7539 -- if one of them is an actual in an instance, it may be a subtype of
7540 -- the non-limited view of the other.
7542 if From_Limited_With (Typ_1)
7543 and then From_Limited_With (Typ_2)
7544 and then Available_View (Typ_1) = Available_View (Typ_2)
7548 elsif From_Limited_With (Typ_1) then
7549 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7551 elsif From_Limited_With (Typ_2) then
7552 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7557 end Matches_Limited_With_View;
7561 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7563 Type_1 : Entity_Id := T1;
7564 Type_2 : Entity_Id := T2;
7566 -- Start of processing for Conforming_Types
7569 -- The context is an instance association for a formal access-to-
7570 -- subprogram type; the formal parameter types require mapping because
7571 -- they may denote other formal parameters of the generic unit.
7574 Type_1 := Get_Instance_Of (T1);
7575 Type_2 := Get_Instance_Of (T2);
7578 -- If one of the types is a view of the other introduced by a limited
7579 -- with clause, treat these as conforming for all purposes.
7581 if Matches_Limited_With_View (T1, T2) then
7584 elsif Base_Types_Match (Type_1, Type_2) then
7585 if Ctype <= Mode_Conformant then
7590 Subtypes_Statically_Match (Type_1, Type_2)
7591 and then Dimensions_Match (Type_1, Type_2);
7594 elsif Is_Incomplete_Or_Private_Type (Type_1)
7595 and then Present (Full_View (Type_1))
7596 and then Base_Types_Match (Full_View (Type_1), Type_2)
7599 Ctype <= Mode_Conformant
7600 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7602 elsif Ekind (Type_2) = E_Incomplete_Type
7603 and then Present (Full_View (Type_2))
7604 and then Base_Types_Match (Type_1, Full_View (Type_2))
7607 Ctype <= Mode_Conformant
7608 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7610 elsif Is_Private_Type (Type_2)
7611 and then In_Instance
7612 and then Present (Full_View (Type_2))
7613 and then Base_Types_Match (Type_1, Full_View (Type_2))
7616 Ctype <= Mode_Conformant
7617 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7619 -- Another confusion between views in a nested instance with an
7620 -- actual private type whose full view is not in scope.
7622 elsif Ekind (Type_2) = E_Private_Subtype
7623 and then In_Instance
7624 and then Etype (Type_2) = Type_1
7628 -- In Ada 2012, incomplete types (including limited views) can appear
7629 -- as actuals in instantiations.
7631 elsif Is_Incomplete_Type (Type_1)
7632 and then Is_Incomplete_Type (Type_2)
7633 and then (Used_As_Generic_Actual (Type_1)
7634 or else Used_As_Generic_Actual (Type_2))
7639 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7640 -- treated recursively because they carry a signature. As far as
7641 -- conformance is concerned, convention plays no role, and either
7642 -- or both could be access to protected subprograms.
7644 Are_Anonymous_Access_To_Subprogram_Types :=
7645 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7646 E_Anonymous_Access_Protected_Subprogram_Type)
7648 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7649 E_Anonymous_Access_Protected_Subprogram_Type);
7651 -- Test anonymous access type case. For this case, static subtype
7652 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7653 -- the base types because we may have built internal subtype entities
7654 -- to handle null-excluding types (see Process_Formals).
7656 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7658 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7660 -- Ada 2005 (AI-254)
7662 or else Are_Anonymous_Access_To_Subprogram_Types
7665 Desig_1 : Entity_Id;
7666 Desig_2 : Entity_Id;
7669 -- In Ada 2005, access constant indicators must match for
7670 -- subtype conformance.
7672 if Ada_Version >= Ada_2005
7673 and then Ctype >= Subtype_Conformant
7675 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7680 Desig_1 := Find_Designated_Type (Type_1);
7681 Desig_2 := Find_Designated_Type (Type_2);
7683 -- If the context is an instance association for a formal
7684 -- access-to-subprogram type; formal access parameter designated
7685 -- types require mapping because they may denote other formal
7686 -- parameters of the generic unit.
7689 Desig_1 := Get_Instance_Of (Desig_1);
7690 Desig_2 := Get_Instance_Of (Desig_2);
7693 -- It is possible for a Class_Wide_Type to be introduced for an
7694 -- incomplete type, in which case there is a separate class_ wide
7695 -- type for the full view. The types conform if their Etypes
7696 -- conform, i.e. one may be the full view of the other. This can
7697 -- only happen in the context of an access parameter, other uses
7698 -- of an incomplete Class_Wide_Type are illegal.
7700 if Is_Class_Wide_Type (Desig_1)
7702 Is_Class_Wide_Type (Desig_2)
7706 (Etype (Base_Type (Desig_1)),
7707 Etype (Base_Type (Desig_2)), Ctype);
7709 elsif Are_Anonymous_Access_To_Subprogram_Types then
7710 if Ada_Version < Ada_2005 then
7712 Ctype = Type_Conformant
7713 or else Subtypes_Statically_Match (Desig_1, Desig_2);
7715 -- We must check the conformance of the signatures themselves
7719 Conformant : Boolean;
7722 (Desig_1, Desig_2, Ctype, False, Conformant);
7727 -- A limited view of an actual matches the corresponding
7728 -- incomplete formal.
7730 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7731 and then From_Limited_With (Desig_2)
7732 and then Used_As_Generic_Actual (Etype (Desig_2))
7737 return Base_Type (Desig_1) = Base_Type (Desig_2)
7738 and then (Ctype = Type_Conformant
7740 Subtypes_Statically_Match (Desig_1, Desig_2));
7744 -- Otherwise definitely no match
7747 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7748 and then Is_Access_Type (Type_2))
7749 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7750 and then Is_Access_Type (Type_1)))
7753 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7755 May_Hide_Profile := True;
7760 end Conforming_Types;
7762 --------------------------
7763 -- Create_Extra_Formals --
7764 --------------------------
7766 procedure Create_Extra_Formals (E : Entity_Id) is
7767 First_Extra : Entity_Id := Empty;
7769 Last_Extra : Entity_Id := Empty;
7771 function Add_Extra_Formal
7772 (Assoc_Entity : Entity_Id;
7775 Suffix : String) return Entity_Id;
7776 -- Add an extra formal to the current list of formals and extra formals.
7777 -- The extra formal is added to the end of the list of extra formals,
7778 -- and also returned as the result. These formals are always of mode IN.
7779 -- The new formal has the type Typ, is declared in Scope, and its name
7780 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7781 -- The following suffixes are currently used. They should not be changed
7782 -- without coordinating with CodePeer, which makes use of these to
7783 -- provide better messages.
7785 -- O denotes the Constrained bit.
7786 -- L denotes the accessibility level.
7787 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7788 -- the full list in exp_ch6.BIP_Formal_Kind.
7790 ----------------------
7791 -- Add_Extra_Formal --
7792 ----------------------
7794 function Add_Extra_Formal
7795 (Assoc_Entity : Entity_Id;
7798 Suffix : String) return Entity_Id
7800 EF : constant Entity_Id :=
7801 Make_Defining_Identifier (Sloc (Assoc_Entity),
7802 Chars => New_External_Name (Chars (Assoc_Entity),
7806 -- A little optimization. Never generate an extra formal for the
7807 -- _init operand of an initialization procedure, since it could
7810 if Chars (Formal) = Name_uInit then
7814 Set_Ekind (EF, E_In_Parameter);
7815 Set_Actual_Subtype (EF, Typ);
7816 Set_Etype (EF, Typ);
7817 Set_Scope (EF, Scope);
7818 Set_Mechanism (EF, Default_Mechanism);
7819 Set_Formal_Validity (EF);
7821 if No (First_Extra) then
7823 Set_Extra_Formals (Scope, First_Extra);
7826 if Present (Last_Extra) then
7827 Set_Extra_Formal (Last_Extra, EF);
7833 end Add_Extra_Formal;
7837 Formal_Type : Entity_Id;
7838 P_Formal : Entity_Id := Empty;
7840 -- Start of processing for Create_Extra_Formals
7843 -- We never generate extra formals if expansion is not active because we
7844 -- don't need them unless we are generating code.
7846 if not Expander_Active then
7850 -- No need to generate extra formals in interface thunks whose target
7851 -- primitive has no extra formals.
7853 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7857 -- If this is a derived subprogram then the subtypes of the parent
7858 -- subprogram's formal parameters will be used to determine the need
7859 -- for extra formals.
7861 if Is_Overloadable (E) and then Present (Alias (E)) then
7862 P_Formal := First_Formal (Alias (E));
7865 Formal := First_Formal (E);
7866 while Present (Formal) loop
7867 Last_Extra := Formal;
7868 Next_Formal (Formal);
7871 -- If Extra_Formals were already created, don't do it again. This
7872 -- situation may arise for subprogram types created as part of
7873 -- dispatching calls (see Expand_Dispatching_Call)
7875 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7879 -- If the subprogram is a predefined dispatching subprogram then don't
7880 -- generate any extra constrained or accessibility level formals. In
7881 -- general we suppress these for internal subprograms (by not calling
7882 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7883 -- generated stream attributes do get passed through because extra
7884 -- build-in-place formals are needed in some cases (limited 'Input).
7886 if Is_Predefined_Internal_Operation (E) then
7887 goto Test_For_Func_Result_Extras;
7890 Formal := First_Formal (E);
7891 while Present (Formal) loop
7893 -- Create extra formal for supporting the attribute 'Constrained.
7894 -- The case of a private type view without discriminants also
7895 -- requires the extra formal if the underlying type has defaulted
7898 if Ekind (Formal) /= E_In_Parameter then
7899 if Present (P_Formal) then
7900 Formal_Type := Etype (P_Formal);
7902 Formal_Type := Etype (Formal);
7905 -- Do not produce extra formals for Unchecked_Union parameters.
7906 -- Jump directly to the end of the loop.
7908 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7909 goto Skip_Extra_Formal_Generation;
7912 if not Has_Discriminants (Formal_Type)
7913 and then Ekind (Formal_Type) in Private_Kind
7914 and then Present (Underlying_Type (Formal_Type))
7916 Formal_Type := Underlying_Type (Formal_Type);
7919 -- Suppress the extra formal if formal's subtype is constrained or
7920 -- indefinite, or we're compiling for Ada 2012 and the underlying
7921 -- type is tagged and limited. In Ada 2012, a limited tagged type
7922 -- can have defaulted discriminants, but 'Constrained is required
7923 -- to return True, so the formal is never needed (see AI05-0214).
7924 -- Note that this ensures consistency of calling sequences for
7925 -- dispatching operations when some types in a class have defaults
7926 -- on discriminants and others do not (and requiring the extra
7927 -- formal would introduce distributed overhead).
7929 -- If the type does not have a completion yet, treat as prior to
7930 -- Ada 2012 for consistency.
7932 if Has_Discriminants (Formal_Type)
7933 and then not Is_Constrained (Formal_Type)
7934 and then Is_Definite_Subtype (Formal_Type)
7935 and then (Ada_Version < Ada_2012
7936 or else No (Underlying_Type (Formal_Type))
7938 (Is_Limited_Type (Formal_Type)
7941 (Underlying_Type (Formal_Type)))))
7943 Set_Extra_Constrained
7944 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7948 -- Create extra formal for supporting accessibility checking. This
7949 -- is done for both anonymous access formals and formals of named
7950 -- access types that are marked as controlling formals. The latter
7951 -- case can occur when Expand_Dispatching_Call creates a subprogram
7952 -- type and substitutes the types of access-to-class-wide actuals
7953 -- for the anonymous access-to-specific-type of controlling formals.
7954 -- Base_Type is applied because in cases where there is a null
7955 -- exclusion the formal may have an access subtype.
7957 -- This is suppressed if we specifically suppress accessibility
7958 -- checks at the package level for either the subprogram, or the
7959 -- package in which it resides. However, we do not suppress it
7960 -- simply if the scope has accessibility checks suppressed, since
7961 -- this could cause trouble when clients are compiled with a
7962 -- different suppression setting. The explicit checks at the
7963 -- package level are safe from this point of view.
7965 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7966 or else (Is_Controlling_Formal (Formal)
7967 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7969 (Explicit_Suppress (E, Accessibility_Check)
7971 Explicit_Suppress (Scope (E), Accessibility_Check))
7974 or else Present (Extra_Accessibility (P_Formal)))
7976 Set_Extra_Accessibility
7977 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7980 -- This label is required when skipping extra formal generation for
7981 -- Unchecked_Union parameters.
7983 <<Skip_Extra_Formal_Generation>>
7985 if Present (P_Formal) then
7986 Next_Formal (P_Formal);
7989 Next_Formal (Formal);
7992 <<Test_For_Func_Result_Extras>>
7994 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7995 -- function call is ... determined by the point of call ...".
7997 if Needs_Result_Accessibility_Level (E) then
7998 Set_Extra_Accessibility_Of_Result
7999 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8002 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8003 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8005 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8007 Result_Subt : constant Entity_Id := Etype (E);
8008 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8009 Formal_Typ : Entity_Id;
8010 Subp_Decl : Node_Id;
8012 Discard : Entity_Id;
8013 pragma Warnings (Off, Discard);
8016 -- In the case of functions with unconstrained result subtypes,
8017 -- add a 4-state formal indicating whether the return object is
8018 -- allocated by the caller (1), or should be allocated by the
8019 -- callee on the secondary stack (2), in the global heap (3), or
8020 -- in a user-defined storage pool (4). For the moment we just use
8021 -- Natural for the type of this formal. Note that this formal
8022 -- isn't usually needed in the case where the result subtype is
8023 -- constrained, but it is needed when the function has a tagged
8024 -- result, because generally such functions can be called in a
8025 -- dispatching context and such calls must be handled like calls
8026 -- to a class-wide function.
8028 if Needs_BIP_Alloc_Form (E) then
8031 (E, Standard_Natural,
8032 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8034 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8035 -- use a user-defined pool. This formal is not added on
8036 -- ZFP as those targets do not support pools.
8038 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8041 (E, RTE (RE_Root_Storage_Pool_Ptr),
8042 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8046 -- In the case of functions whose result type needs finalization,
8047 -- add an extra formal which represents the finalization master.
8049 if Needs_BIP_Finalization_Master (E) then
8052 (E, RTE (RE_Finalization_Master_Ptr),
8053 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8056 -- When the result type contains tasks, add two extra formals: the
8057 -- master of the tasks to be created, and the caller's activation
8060 if Has_Task (Full_Subt) then
8063 (E, RTE (RE_Master_Id),
8064 E, BIP_Formal_Suffix (BIP_Task_Master));
8067 (E, RTE (RE_Activation_Chain_Access),
8068 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8071 -- All build-in-place functions get an extra formal that will be
8072 -- passed the address of the return object within the caller.
8075 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8077 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8078 Set_Etype (Formal_Typ, Formal_Typ);
8079 Set_Depends_On_Private
8080 (Formal_Typ, Has_Private_Component (Formal_Typ));
8081 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8082 Set_Is_Access_Constant (Formal_Typ, False);
8084 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8085 -- the designated type comes from the limited view (for back-end
8088 Set_From_Limited_With
8089 (Formal_Typ, From_Limited_With (Result_Subt));
8091 Layout_Type (Formal_Typ);
8093 -- Force the definition of the Itype in case of internal function
8094 -- calls within the same or nested scope.
8096 if Is_Subprogram_Or_Generic_Subprogram (E) then
8097 Subp_Decl := Parent (E);
8099 -- The insertion point for an Itype reference should be after
8100 -- the unit declaration node of the subprogram. An exception
8101 -- to this are inherited operations from a parent type in which
8102 -- case the derived type acts as their parent.
8104 if Nkind_In (Subp_Decl, N_Function_Specification,
8105 N_Procedure_Specification)
8107 Subp_Decl := Parent (Subp_Decl);
8110 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8115 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8118 end Create_Extra_Formals;
8120 -----------------------------
8121 -- Enter_Overloaded_Entity --
8122 -----------------------------
8124 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8125 function Matches_Predefined_Op return Boolean;
8126 -- This returns an approximation of whether S matches a predefined
8127 -- operator, based on the operator symbol, and the parameter and result
8128 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8130 ---------------------------
8131 -- Matches_Predefined_Op --
8132 ---------------------------
8134 function Matches_Predefined_Op return Boolean is
8135 Formal_1 : constant Entity_Id := First_Formal (S);
8136 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8137 Op : constant Name_Id := Chars (S);
8138 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8139 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8144 if Present (Formal_2) then
8146 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8149 -- All but "&" and "**" have same-types parameters
8158 if Type_1 /= Type_2 then
8163 -- Check parameter and result types
8171 Is_Boolean_Type (Result_Type)
8172 and then Result_Type = Type_1;
8178 Is_Integer_Type (Result_Type)
8179 and then Result_Type = Type_1;
8187 Is_Numeric_Type (Result_Type)
8188 and then Result_Type = Type_1;
8194 Is_Boolean_Type (Result_Type)
8195 and then not Is_Limited_Type (Type_1);
8203 Is_Boolean_Type (Result_Type)
8204 and then (Is_Array_Type (Type_1)
8205 or else Is_Scalar_Type (Type_1));
8207 when Name_Op_Concat =>
8208 return Is_Array_Type (Result_Type);
8210 when Name_Op_Expon =>
8212 (Is_Integer_Type (Result_Type)
8213 or else Is_Floating_Point_Type (Result_Type))
8214 and then Result_Type = Type_1
8215 and then Type_2 = Standard_Integer;
8218 raise Program_Error;
8231 Is_Numeric_Type (Result_Type)
8232 and then Result_Type = Type_1;
8236 Is_Boolean_Type (Result_Type)
8237 and then Result_Type = Type_1;
8240 raise Program_Error;
8243 end Matches_Predefined_Op;
8247 E : Entity_Id := Current_Entity_In_Scope (S);
8248 C_E : Entity_Id := Current_Entity (S);
8250 -- Start of processing for Enter_Overloaded_Entity
8254 Set_Has_Homonym (E);
8255 Set_Has_Homonym (S);
8258 Set_Is_Immediately_Visible (S);
8259 Set_Scope (S, Current_Scope);
8261 -- Chain new entity if front of homonym in current scope, so that
8262 -- homonyms are contiguous.
8264 if Present (E) and then E /= C_E then
8265 while Homonym (C_E) /= E loop
8266 C_E := Homonym (C_E);
8269 Set_Homonym (C_E, S);
8273 Set_Current_Entity (S);
8278 if Is_Inherited_Operation (S) then
8279 Append_Inherited_Subprogram (S);
8281 Append_Entity (S, Current_Scope);
8284 Set_Public_Status (S);
8286 if Debug_Flag_E then
8287 Write_Str ("New overloaded entity chain: ");
8288 Write_Name (Chars (S));
8291 while Present (E) loop
8292 Write_Str (" "); Write_Int (Int (E));
8299 -- Generate warning for hiding
8302 and then Comes_From_Source (S)
8303 and then In_Extended_Main_Source_Unit (S)
8310 -- Warn unless genuine overloading. Do not emit warning on
8311 -- hiding predefined operators in Standard (these are either an
8312 -- (artifact of our implicit declarations, or simple noise) but
8313 -- keep warning on a operator defined on a local subtype, because
8314 -- of the real danger that different operators may be applied in
8315 -- various parts of the program.
8317 -- Note that if E and S have the same scope, there is never any
8318 -- hiding. Either the two conflict, and the program is illegal,
8319 -- or S is overriding an implicit inherited subprogram.
8321 if Scope (E) /= Scope (S)
8322 and then (not Is_Overloadable (E)
8323 or else Subtype_Conformant (E, S))
8324 and then (Is_Immediately_Visible (E)
8325 or else Is_Potentially_Use_Visible (S))
8327 if Scope (E) = Standard_Standard then
8328 if Nkind (S) = N_Defining_Operator_Symbol
8329 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8331 and then Matches_Predefined_Op
8334 ("declaration of & hides predefined operator?h?", S);
8337 -- E not immediately within Standard
8340 Error_Msg_Sloc := Sloc (E);
8341 Error_Msg_N ("declaration of & hides one #?h?", S);
8346 end Enter_Overloaded_Entity;
8348 -----------------------------
8349 -- Check_Untagged_Equality --
8350 -----------------------------
8352 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8353 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8354 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8358 -- This check applies only if we have a subprogram declaration with an
8359 -- untagged record type.
8361 if Nkind (Decl) /= N_Subprogram_Declaration
8362 or else not Is_Record_Type (Typ)
8363 or else Is_Tagged_Type (Typ)
8368 -- In Ada 2012 case, we will output errors or warnings depending on
8369 -- the setting of debug flag -gnatd.E.
8371 if Ada_Version >= Ada_2012 then
8372 Error_Msg_Warn := Debug_Flag_Dot_EE;
8374 -- In earlier versions of Ada, nothing to do unless we are warning on
8375 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8378 if not Warn_On_Ada_2012_Compatibility then
8383 -- Cases where the type has already been frozen
8385 if Is_Frozen (Typ) then
8387 -- If the type is not declared in a package, or if we are in the body
8388 -- of the package or in some other scope, the new operation is not
8389 -- primitive, and therefore legal, though suspicious. Should we
8390 -- generate a warning in this case ???
8392 if Ekind (Scope (Typ)) /= E_Package
8393 or else Scope (Typ) /= Current_Scope
8397 -- If the type is a generic actual (sub)type, the operation is not
8398 -- primitive either because the base type is declared elsewhere.
8400 elsif Is_Generic_Actual_Type (Typ) then
8403 -- Here we have a definite error of declaration after freezing
8406 if Ada_Version >= Ada_2012 then
8408 ("equality operator must be declared before type & is "
8409 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8411 -- In Ada 2012 mode with error turned to warning, output one
8412 -- more warning to warn that the equality operation may not
8413 -- compose. This is the consequence of ignoring the error.
8415 if Error_Msg_Warn then
8416 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8421 ("equality operator must be declared before type& is "
8422 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8425 -- If we are in the package body, we could just move the
8426 -- declaration to the package spec, so add a message saying that.
8428 if In_Package_Body (Scope (Typ)) then
8429 if Ada_Version >= Ada_2012 then
8431 ("\move declaration to package spec<<", Eq_Op);
8434 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8437 -- Otherwise try to find the freezing point
8440 Obj_Decl := Next (Parent (Typ));
8441 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8442 if Nkind (Obj_Decl) = N_Object_Declaration
8443 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8445 -- Freezing point, output warnings
8447 if Ada_Version >= Ada_2012 then
8449 ("type& is frozen by declaration??", Obj_Decl, Typ);
8451 ("\an equality operator cannot be declared after "
8456 ("type& is frozen by declaration (Ada 2012)?y?",
8459 ("\an equality operator cannot be declared after "
8460 & "this point (Ada 2012)?y?",
8472 -- Here if type is not frozen yet. It is illegal to have a primitive
8473 -- equality declared in the private part if the type is visible.
8475 elsif not In_Same_List (Parent (Typ), Decl)
8476 and then not Is_Limited_Type (Typ)
8478 -- Shouldn't we give an RM reference here???
8480 if Ada_Version >= Ada_2012 then
8482 ("equality operator appears too late<<", Eq_Op);
8485 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8488 -- No error detected
8493 end Check_Untagged_Equality;
8495 -----------------------------
8496 -- Find_Corresponding_Spec --
8497 -----------------------------
8499 function Find_Corresponding_Spec
8501 Post_Error : Boolean := True) return Entity_Id
8503 Spec : constant Node_Id := Specification (N);
8504 Designator : constant Entity_Id := Defining_Entity (Spec);
8508 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8509 -- Even if fully conformant, a body may depend on a generic actual when
8510 -- the spec does not, or vice versa, in which case they were distinct
8511 -- entities in the generic.
8513 -------------------------------
8514 -- Different_Generic_Profile --
8515 -------------------------------
8517 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8520 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8521 -- Check that the types of corresponding formals have the same
8522 -- generic actual if any. We have to account for subtypes of a
8523 -- generic formal, declared between a spec and a body, which may
8524 -- appear distinct in an instance but matched in the generic, and
8525 -- the subtype may be used either in the spec or the body of the
8526 -- subprogram being checked.
8528 -------------------------
8529 -- Same_Generic_Actual --
8530 -------------------------
8532 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8534 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
8535 -- Predicate to check whether S1 is a subtype of S2 in the source
8538 -------------------------
8539 -- Is_Declared_Subtype --
8540 -------------------------
8542 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
8544 return Comes_From_Source (Parent (S1))
8545 and then Nkind (Parent (S1)) = N_Subtype_Declaration
8546 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
8547 and then Entity (Subtype_Indication (Parent (S1))) = S2;
8548 end Is_Declared_Subtype;
8550 -- Start of processing for Same_Generic_Actual
8553 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8554 or else Is_Declared_Subtype (T1, T2)
8555 or else Is_Declared_Subtype (T2, T1);
8556 end Same_Generic_Actual;
8558 -- Start of processing for Different_Generic_Profile
8561 if not In_Instance then
8564 elsif Ekind (E) = E_Function
8565 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8570 F1 := First_Formal (Designator);
8571 F2 := First_Formal (E);
8572 while Present (F1) loop
8573 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8582 end Different_Generic_Profile;
8584 -- Start of processing for Find_Corresponding_Spec
8587 E := Current_Entity (Designator);
8588 while Present (E) loop
8590 -- We are looking for a matching spec. It must have the same scope,
8591 -- and the same name, and either be type conformant, or be the case
8592 -- of a library procedure spec and its body (which belong to one
8593 -- another regardless of whether they are type conformant or not).
8595 if Scope (E) = Current_Scope then
8596 if Current_Scope = Standard_Standard
8597 or else (Ekind (E) = Ekind (Designator)
8598 and then Type_Conformant (E, Designator))
8600 -- Within an instantiation, we know that spec and body are
8601 -- subtype conformant, because they were subtype conformant in
8602 -- the generic. We choose the subtype-conformant entity here as
8603 -- well, to resolve spurious ambiguities in the instance that
8604 -- were not present in the generic (i.e. when two different
8605 -- types are given the same actual). If we are looking for a
8606 -- spec to match a body, full conformance is expected.
8610 -- Inherit the convention and "ghostness" of the matching
8611 -- spec to ensure proper full and subtype conformance.
8613 Set_Convention (Designator, Convention (E));
8615 -- Skip past subprogram bodies and subprogram renamings that
8616 -- may appear to have a matching spec, but that aren't fully
8617 -- conformant with it. That can occur in cases where an
8618 -- actual type causes unrelated homographs in the instance.
8620 if Nkind_In (N, N_Subprogram_Body,
8621 N_Subprogram_Renaming_Declaration)
8622 and then Present (Homonym (E))
8623 and then not Fully_Conformant (Designator, E)
8627 elsif not Subtype_Conformant (Designator, E) then
8630 elsif Different_Generic_Profile (E) then
8635 -- Ada 2012 (AI05-0165): For internally generated bodies of
8636 -- null procedures locate the internally generated spec. We
8637 -- enforce mode conformance since a tagged type may inherit
8638 -- from interfaces several null primitives which differ only
8639 -- in the mode of the formals.
8641 if not (Comes_From_Source (E))
8642 and then Is_Null_Procedure (E)
8643 and then not Mode_Conformant (Designator, E)
8647 -- For null procedures coming from source that are completions,
8648 -- analysis of the generated body will establish the link.
8650 elsif Comes_From_Source (E)
8651 and then Nkind (Spec) = N_Procedure_Specification
8652 and then Null_Present (Spec)
8656 -- Expression functions can be completions, but cannot be
8657 -- completed by an explicit body.
8659 elsif Comes_From_Source (E)
8660 and then Comes_From_Source (N)
8661 and then Nkind (N) = N_Subprogram_Body
8662 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8663 N_Expression_Function
8665 Error_Msg_Sloc := Sloc (E);
8666 Error_Msg_N ("body conflicts with expression function#", N);
8669 elsif not Has_Completion (E) then
8670 if Nkind (N) /= N_Subprogram_Body_Stub then
8671 Set_Corresponding_Spec (N, E);
8674 Set_Has_Completion (E);
8677 elsif Nkind (Parent (N)) = N_Subunit then
8679 -- If this is the proper body of a subunit, the completion
8680 -- flag is set when analyzing the stub.
8684 -- If E is an internal function with a controlling result that
8685 -- was created for an operation inherited by a null extension,
8686 -- it may be overridden by a body without a previous spec (one
8687 -- more reason why these should be shunned). In that case we
8688 -- remove the generated body if present, because the current
8689 -- one is the explicit overriding.
8691 elsif Ekind (E) = E_Function
8692 and then Ada_Version >= Ada_2005
8693 and then not Comes_From_Source (E)
8694 and then Has_Controlling_Result (E)
8695 and then Is_Null_Extension (Etype (E))
8696 and then Comes_From_Source (Spec)
8698 Set_Has_Completion (E, False);
8701 and then Nkind (Parent (E)) = N_Function_Specification
8704 (Unit_Declaration_Node
8705 (Corresponding_Body (Unit_Declaration_Node (E))));
8709 -- If expansion is disabled, or if the wrapper function has
8710 -- not been generated yet, this a late body overriding an
8711 -- inherited operation, or it is an overriding by some other
8712 -- declaration before the controlling result is frozen. In
8713 -- either case this is a declaration of a new entity.
8719 -- If the body already exists, then this is an error unless
8720 -- the previous declaration is the implicit declaration of a
8721 -- derived subprogram. It is also legal for an instance to
8722 -- contain type conformant overloadable declarations (but the
8723 -- generic declaration may not), per 8.3(26/2).
8725 elsif No (Alias (E))
8726 and then not Is_Intrinsic_Subprogram (E)
8727 and then not In_Instance
8730 Error_Msg_Sloc := Sloc (E);
8732 if Is_Imported (E) then
8734 ("body not allowed for imported subprogram & declared#",
8737 Error_Msg_NE ("duplicate body for & declared#", N, E);
8741 -- Child units cannot be overloaded, so a conformance mismatch
8742 -- between body and a previous spec is an error.
8744 elsif Is_Child_Unit (E)
8746 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8748 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8753 ("body of child unit does not match previous declaration", N);
8761 -- On exit, we know that no previous declaration of subprogram exists
8764 end Find_Corresponding_Spec;
8766 ----------------------
8767 -- Fully_Conformant --
8768 ----------------------
8770 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8773 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8775 end Fully_Conformant;
8777 ----------------------------------
8778 -- Fully_Conformant_Expressions --
8779 ----------------------------------
8781 function Fully_Conformant_Expressions
8782 (Given_E1 : Node_Id;
8783 Given_E2 : Node_Id) return Boolean
8785 E1 : constant Node_Id := Original_Node (Given_E1);
8786 E2 : constant Node_Id := Original_Node (Given_E2);
8787 -- We always test conformance on original nodes, since it is possible
8788 -- for analysis and/or expansion to make things look as though they
8789 -- conform when they do not, e.g. by converting 1+2 into 3.
8791 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8792 renames Fully_Conformant_Expressions;
8794 function FCL (L1, L2 : List_Id) return Boolean;
8795 -- Compare elements of two lists for conformance. Elements have to be
8796 -- conformant, and actuals inserted as default parameters do not match
8797 -- explicit actuals with the same value.
8799 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8800 -- Compare an operator node with a function call
8806 function FCL (L1, L2 : List_Id) return Boolean is
8810 if L1 = No_List then
8816 if L2 = No_List then
8822 -- Compare two lists, skipping rewrite insertions (we want to compare
8823 -- the original trees, not the expanded versions).
8826 if Is_Rewrite_Insertion (N1) then
8828 elsif Is_Rewrite_Insertion (N2) then
8834 elsif not FCE (N1, N2) then
8847 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8848 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8853 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8858 Act := First (Actuals);
8860 if Nkind (Op_Node) in N_Binary_Op then
8861 if not FCE (Left_Opnd (Op_Node), Act) then
8868 return Present (Act)
8869 and then FCE (Right_Opnd (Op_Node), Act)
8870 and then No (Next (Act));
8874 -- Start of processing for Fully_Conformant_Expressions
8877 -- Nonconformant if paren count does not match. Note: if some idiot
8878 -- complains that we don't do this right for more than 3 levels of
8879 -- parentheses, they will be treated with the respect they deserve.
8881 if Paren_Count (E1) /= Paren_Count (E2) then
8884 -- If same entities are referenced, then they are conformant even if
8885 -- they have different forms (RM 8.3.1(19-20)).
8887 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8888 if Present (Entity (E1)) then
8889 return Entity (E1) = Entity (E2)
8891 -- One may be a discriminant that has been replaced by the
8892 -- corresponding discriminal.
8895 (Chars (Entity (E1)) = Chars (Entity (E2))
8896 and then Ekind (Entity (E1)) = E_Discriminant
8897 and then Ekind (Entity (E2)) = E_In_Parameter)
8899 -- The discriminant of a protected type is transformed into
8900 -- a local constant and then into a parameter of a protected
8904 (Ekind (Entity (E1)) = E_Constant
8905 and then Ekind (Entity (E2)) = E_In_Parameter
8906 and then Present (Discriminal_Link (Entity (E1)))
8907 and then Discriminal_Link (Entity (E1)) =
8908 Discriminal_Link (Entity (E2)))
8910 -- AI12-050: The loop variables of quantified expressions
8911 -- match if they have the same identifier, even though they
8912 -- are different entities.
8915 (Chars (Entity (E1)) = Chars (Entity (E2))
8916 and then Ekind (Entity (E1)) = E_Loop_Parameter
8917 and then Ekind (Entity (E2)) = E_Loop_Parameter);
8919 elsif Nkind (E1) = N_Expanded_Name
8920 and then Nkind (E2) = N_Expanded_Name
8921 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8922 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8924 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8927 -- Identifiers in component associations don't always have
8928 -- entities, but their names must conform.
8930 return Nkind (E1) = N_Identifier
8931 and then Nkind (E2) = N_Identifier
8932 and then Chars (E1) = Chars (E2);
8935 elsif Nkind (E1) = N_Character_Literal
8936 and then Nkind (E2) = N_Expanded_Name
8938 return Nkind (Selector_Name (E2)) = N_Character_Literal
8939 and then Chars (E1) = Chars (Selector_Name (E2));
8941 elsif Nkind (E2) = N_Character_Literal
8942 and then Nkind (E1) = N_Expanded_Name
8944 return Nkind (Selector_Name (E1)) = N_Character_Literal
8945 and then Chars (E2) = Chars (Selector_Name (E1));
8947 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8948 return FCO (E1, E2);
8950 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8951 return FCO (E2, E1);
8953 -- Otherwise we must have the same syntactic entity
8955 elsif Nkind (E1) /= Nkind (E2) then
8958 -- At this point, we specialize by node type
8964 FCL (Expressions (E1), Expressions (E2))
8966 FCL (Component_Associations (E1),
8967 Component_Associations (E2));
8970 if Nkind (Expression (E1)) = N_Qualified_Expression
8972 Nkind (Expression (E2)) = N_Qualified_Expression
8974 return FCE (Expression (E1), Expression (E2));
8976 -- Check that the subtype marks and any constraints
8981 Indic1 : constant Node_Id := Expression (E1);
8982 Indic2 : constant Node_Id := Expression (E2);
8987 if Nkind (Indic1) /= N_Subtype_Indication then
8989 Nkind (Indic2) /= N_Subtype_Indication
8990 and then Entity (Indic1) = Entity (Indic2);
8992 elsif Nkind (Indic2) /= N_Subtype_Indication then
8994 Nkind (Indic1) /= N_Subtype_Indication
8995 and then Entity (Indic1) = Entity (Indic2);
8998 if Entity (Subtype_Mark (Indic1)) /=
8999 Entity (Subtype_Mark (Indic2))
9004 Elt1 := First (Constraints (Constraint (Indic1)));
9005 Elt2 := First (Constraints (Constraint (Indic2)));
9006 while Present (Elt1) and then Present (Elt2) loop
9007 if not FCE (Elt1, Elt2) then
9020 when N_Attribute_Reference =>
9022 Attribute_Name (E1) = Attribute_Name (E2)
9023 and then FCL (Expressions (E1), Expressions (E2));
9027 Entity (E1) = Entity (E2)
9028 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9029 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9031 when N_Membership_Test
9035 FCE (Left_Opnd (E1), Left_Opnd (E2))
9037 FCE (Right_Opnd (E1), Right_Opnd (E2));
9039 when N_Case_Expression =>
9045 if not FCE (Expression (E1), Expression (E2)) then
9049 Alt1 := First (Alternatives (E1));
9050 Alt2 := First (Alternatives (E2));
9052 if Present (Alt1) /= Present (Alt2) then
9054 elsif No (Alt1) then
9058 if not FCE (Expression (Alt1), Expression (Alt2))
9059 or else not FCL (Discrete_Choices (Alt1),
9060 Discrete_Choices (Alt2))
9071 when N_Character_Literal =>
9073 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9075 when N_Component_Association =>
9077 FCL (Choices (E1), Choices (E2))
9079 FCE (Expression (E1), Expression (E2));
9081 when N_Explicit_Dereference =>
9083 FCE (Prefix (E1), Prefix (E2));
9085 when N_Extension_Aggregate =>
9087 FCL (Expressions (E1), Expressions (E2))
9088 and then Null_Record_Present (E1) =
9089 Null_Record_Present (E2)
9090 and then FCL (Component_Associations (E1),
9091 Component_Associations (E2));
9093 when N_Function_Call =>
9095 FCE (Name (E1), Name (E2))
9097 FCL (Parameter_Associations (E1),
9098 Parameter_Associations (E2));
9100 when N_If_Expression =>
9102 FCL (Expressions (E1), Expressions (E2));
9104 when N_Indexed_Component =>
9106 FCE (Prefix (E1), Prefix (E2))
9108 FCL (Expressions (E1), Expressions (E2));
9110 when N_Integer_Literal =>
9111 return (Intval (E1) = Intval (E2));
9116 when N_Operator_Symbol =>
9118 Chars (E1) = Chars (E2);
9120 when N_Others_Choice =>
9123 when N_Parameter_Association =>
9125 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9126 and then FCE (Explicit_Actual_Parameter (E1),
9127 Explicit_Actual_Parameter (E2));
9129 when N_Qualified_Expression
9131 | N_Unchecked_Type_Conversion
9134 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9136 FCE (Expression (E1), Expression (E2));
9138 when N_Quantified_Expression =>
9139 if not FCE (Condition (E1), Condition (E2)) then
9143 if Present (Loop_Parameter_Specification (E1))
9144 and then Present (Loop_Parameter_Specification (E2))
9147 L1 : constant Node_Id :=
9148 Loop_Parameter_Specification (E1);
9149 L2 : constant Node_Id :=
9150 Loop_Parameter_Specification (E2);
9154 Reverse_Present (L1) = Reverse_Present (L2)
9156 FCE (Defining_Identifier (L1),
9157 Defining_Identifier (L2))
9159 FCE (Discrete_Subtype_Definition (L1),
9160 Discrete_Subtype_Definition (L2));
9163 elsif Present (Iterator_Specification (E1))
9164 and then Present (Iterator_Specification (E2))
9167 I1 : constant Node_Id := Iterator_Specification (E1);
9168 I2 : constant Node_Id := Iterator_Specification (E2);
9172 FCE (Defining_Identifier (I1),
9173 Defining_Identifier (I2))
9175 Of_Present (I1) = Of_Present (I2)
9177 Reverse_Present (I1) = Reverse_Present (I2)
9178 and then FCE (Name (I1), Name (I2))
9179 and then FCE (Subtype_Indication (I1),
9180 Subtype_Indication (I2));
9183 -- The quantified expressions used different specifications to
9184 -- walk their respective ranges.
9192 FCE (Low_Bound (E1), Low_Bound (E2))
9194 FCE (High_Bound (E1), High_Bound (E2));
9196 when N_Real_Literal =>
9197 return (Realval (E1) = Realval (E2));
9199 when N_Selected_Component =>
9201 FCE (Prefix (E1), Prefix (E2))
9203 FCE (Selector_Name (E1), Selector_Name (E2));
9207 FCE (Prefix (E1), Prefix (E2))
9209 FCE (Discrete_Range (E1), Discrete_Range (E2));
9211 when N_String_Literal =>
9213 S1 : constant String_Id := Strval (E1);
9214 S2 : constant String_Id := Strval (E2);
9215 L1 : constant Nat := String_Length (S1);
9216 L2 : constant Nat := String_Length (S2);
9223 for J in 1 .. L1 loop
9224 if Get_String_Char (S1, J) /=
9225 Get_String_Char (S2, J)
9237 Entity (E1) = Entity (E2)
9239 FCE (Right_Opnd (E1), Right_Opnd (E2));
9241 -- All other node types cannot appear in this context. Strictly
9242 -- we should raise a fatal internal error. Instead we just ignore
9243 -- the nodes. This means that if anyone makes a mistake in the
9244 -- expander and mucks an expression tree irretrievably, the result
9245 -- will be a failure to detect a (probably very obscure) case
9246 -- of non-conformance, which is better than bombing on some
9247 -- case where two expressions do in fact conform.
9253 end Fully_Conformant_Expressions;
9255 ----------------------------------------
9256 -- Fully_Conformant_Discrete_Subtypes --
9257 ----------------------------------------
9259 function Fully_Conformant_Discrete_Subtypes
9260 (Given_S1 : Node_Id;
9261 Given_S2 : Node_Id) return Boolean
9263 S1 : constant Node_Id := Original_Node (Given_S1);
9264 S2 : constant Node_Id := Original_Node (Given_S2);
9266 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9267 -- Special-case for a bound given by a discriminant, which in the body
9268 -- is replaced with the discriminal of the enclosing type.
9270 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9271 -- Check both bounds
9273 -----------------------
9274 -- Conforming_Bounds --
9275 -----------------------
9277 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9279 if Is_Entity_Name (B1)
9280 and then Is_Entity_Name (B2)
9281 and then Ekind (Entity (B1)) = E_Discriminant
9283 return Chars (B1) = Chars (B2);
9286 return Fully_Conformant_Expressions (B1, B2);
9288 end Conforming_Bounds;
9290 -----------------------
9291 -- Conforming_Ranges --
9292 -----------------------
9294 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9297 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9299 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9300 end Conforming_Ranges;
9302 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9305 if Nkind (S1) /= Nkind (S2) then
9308 elsif Is_Entity_Name (S1) then
9309 return Entity (S1) = Entity (S2);
9311 elsif Nkind (S1) = N_Range then
9312 return Conforming_Ranges (S1, S2);
9314 elsif Nkind (S1) = N_Subtype_Indication then
9316 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9319 (Range_Expression (Constraint (S1)),
9320 Range_Expression (Constraint (S2)));
9324 end Fully_Conformant_Discrete_Subtypes;
9326 --------------------
9327 -- Install_Entity --
9328 --------------------
9330 procedure Install_Entity (E : Entity_Id) is
9331 Prev : constant Entity_Id := Current_Entity (E);
9333 Set_Is_Immediately_Visible (E);
9334 Set_Current_Entity (E);
9335 Set_Homonym (E, Prev);
9338 ---------------------
9339 -- Install_Formals --
9340 ---------------------
9342 procedure Install_Formals (Id : Entity_Id) is
9345 F := First_Formal (Id);
9346 while Present (F) loop
9350 end Install_Formals;
9352 -----------------------------
9353 -- Is_Interface_Conformant --
9354 -----------------------------
9356 function Is_Interface_Conformant
9357 (Tagged_Type : Entity_Id;
9358 Iface_Prim : Entity_Id;
9359 Prim : Entity_Id) return Boolean
9361 -- The operation may in fact be an inherited (implicit) operation
9362 -- rather than the original interface primitive, so retrieve the
9363 -- ultimate ancestor.
9365 Iface : constant Entity_Id :=
9366 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9367 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9369 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9370 -- Return the controlling formal of Prim
9372 ------------------------
9373 -- Controlling_Formal --
9374 ------------------------
9376 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9380 E := First_Entity (Prim);
9381 while Present (E) loop
9382 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9390 end Controlling_Formal;
9394 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9395 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9397 -- Start of processing for Is_Interface_Conformant
9400 pragma Assert (Is_Subprogram (Iface_Prim)
9401 and then Is_Subprogram (Prim)
9402 and then Is_Dispatching_Operation (Iface_Prim)
9403 and then Is_Dispatching_Operation (Prim));
9405 pragma Assert (Is_Interface (Iface)
9406 or else (Present (Alias (Iface_Prim))
9409 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9411 if Prim = Iface_Prim
9412 or else not Is_Subprogram (Prim)
9413 or else Ekind (Prim) /= Ekind (Iface_Prim)
9414 or else not Is_Dispatching_Operation (Prim)
9415 or else Scope (Prim) /= Scope (Tagged_Type)
9417 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9418 or else not Primitive_Names_Match (Iface_Prim, Prim)
9422 -- The mode of the controlling formals must match
9424 elsif Present (Iface_Ctrl_F)
9425 and then Present (Prim_Ctrl_F)
9426 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9430 -- Case of a procedure, or a function whose result type matches the
9431 -- result type of the interface primitive, or a function that has no
9432 -- controlling result (I or access I).
9434 elsif Ekind (Iface_Prim) = E_Procedure
9435 or else Etype (Prim) = Etype (Iface_Prim)
9436 or else not Has_Controlling_Result (Prim)
9438 return Type_Conformant
9439 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9441 -- Case of a function returning an interface, or an access to one. Check
9442 -- that the return types correspond.
9444 elsif Implements_Interface (Typ, Iface) then
9445 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9447 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9452 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9453 Skip_Controlling_Formals => True);
9459 end Is_Interface_Conformant;
9461 ---------------------------------
9462 -- Is_Non_Overriding_Operation --
9463 ---------------------------------
9465 function Is_Non_Overriding_Operation
9466 (Prev_E : Entity_Id;
9467 New_E : Entity_Id) return Boolean
9471 G_Typ : Entity_Id := Empty;
9473 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9474 -- If F_Type is a derived type associated with a generic actual subtype,
9475 -- then return its Generic_Parent_Type attribute, else return Empty.
9477 function Types_Correspond
9478 (P_Type : Entity_Id;
9479 N_Type : Entity_Id) return Boolean;
9480 -- Returns true if and only if the types (or designated types in the
9481 -- case of anonymous access types) are the same or N_Type is derived
9482 -- directly or indirectly from P_Type.
9484 -----------------------------
9485 -- Get_Generic_Parent_Type --
9486 -----------------------------
9488 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9494 if Is_Derived_Type (F_Typ)
9495 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9497 -- The tree must be traversed to determine the parent subtype in
9498 -- the generic unit, which unfortunately isn't always available
9499 -- via semantic attributes. ??? (Note: The use of Original_Node
9500 -- is needed for cases where a full derived type has been
9503 -- If the parent type is a scalar type, the derivation creates
9504 -- an anonymous base type for it, and the source type is its
9507 if Is_Scalar_Type (F_Typ)
9508 and then not Comes_From_Source (F_Typ)
9512 (Original_Node (Parent (First_Subtype (F_Typ))));
9514 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9516 if Nkind (Defn) = N_Derived_Type_Definition then
9517 Indic := Subtype_Indication (Defn);
9519 if Nkind (Indic) = N_Subtype_Indication then
9520 G_Typ := Entity (Subtype_Mark (Indic));
9522 G_Typ := Entity (Indic);
9525 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9526 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9528 return Generic_Parent_Type (Parent (G_Typ));
9534 end Get_Generic_Parent_Type;
9536 ----------------------
9537 -- Types_Correspond --
9538 ----------------------
9540 function Types_Correspond
9541 (P_Type : Entity_Id;
9542 N_Type : Entity_Id) return Boolean
9544 Prev_Type : Entity_Id := Base_Type (P_Type);
9545 New_Type : Entity_Id := Base_Type (N_Type);
9548 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9549 Prev_Type := Designated_Type (Prev_Type);
9552 if Ekind (New_Type) = E_Anonymous_Access_Type then
9553 New_Type := Designated_Type (New_Type);
9556 if Prev_Type = New_Type then
9559 elsif not Is_Class_Wide_Type (New_Type) then
9560 while Etype (New_Type) /= New_Type loop
9561 New_Type := Etype (New_Type);
9563 if New_Type = Prev_Type then
9569 end Types_Correspond;
9571 -- Start of processing for Is_Non_Overriding_Operation
9574 -- In the case where both operations are implicit derived subprograms
9575 -- then neither overrides the other. This can only occur in certain
9576 -- obscure cases (e.g., derivation from homographs created in a generic
9579 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9582 elsif Ekind (Current_Scope) = E_Package
9583 and then Is_Generic_Instance (Current_Scope)
9584 and then In_Private_Part (Current_Scope)
9585 and then Comes_From_Source (New_E)
9587 -- We examine the formals and result type of the inherited operation,
9588 -- to determine whether their type is derived from (the instance of)
9589 -- a generic type. The first such formal or result type is the one
9592 Formal := First_Formal (Prev_E);
9594 while Present (Formal) loop
9595 F_Typ := Base_Type (Etype (Formal));
9597 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9598 F_Typ := Designated_Type (F_Typ);
9601 G_Typ := Get_Generic_Parent_Type (F_Typ);
9602 exit when Present (G_Typ);
9604 Next_Formal (Formal);
9607 -- If the function dispatches on result check the result type
9609 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9610 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9617 -- If the generic type is a private type, then the original operation
9618 -- was not overriding in the generic, because there was no primitive
9619 -- operation to override.
9621 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9622 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9623 N_Formal_Private_Type_Definition
9627 -- The generic parent type is the ancestor of a formal derived
9628 -- type declaration. We need to check whether it has a primitive
9629 -- operation that should be overridden by New_E in the generic.
9633 P_Formal : Entity_Id;
9634 N_Formal : Entity_Id;
9638 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9641 while Present (Prim_Elt) loop
9642 P_Prim := Node (Prim_Elt);
9644 if Chars (P_Prim) = Chars (New_E)
9645 and then Ekind (P_Prim) = Ekind (New_E)
9647 P_Formal := First_Formal (P_Prim);
9648 N_Formal := First_Formal (New_E);
9649 while Present (P_Formal) and then Present (N_Formal) loop
9650 P_Typ := Etype (P_Formal);
9651 N_Typ := Etype (N_Formal);
9653 if not Types_Correspond (P_Typ, N_Typ) then
9657 Next_Entity (P_Formal);
9658 Next_Entity (N_Formal);
9661 -- Found a matching primitive operation belonging to the
9662 -- formal ancestor type, so the new subprogram is
9666 and then No (N_Formal)
9667 and then (Ekind (New_E) /= E_Function
9670 (Etype (P_Prim), Etype (New_E)))
9676 Next_Elmt (Prim_Elt);
9679 -- If no match found, then the new subprogram does not override
9680 -- in the generic (nor in the instance).
9682 -- If the type in question is not abstract, and the subprogram
9683 -- is, this will be an error if the new operation is in the
9684 -- private part of the instance. Emit a warning now, which will
9685 -- make the subsequent error message easier to understand.
9687 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
9688 and then Is_Abstract_Subprogram (Prev_E)
9689 and then In_Private_Part (Current_Scope)
9691 Error_Msg_Node_2 := F_Typ;
9693 ("private operation& in generic unit does not override "
9694 & "any primitive operation of& (RM 12.3 (18))??",
9704 end Is_Non_Overriding_Operation;
9706 -------------------------------------
9707 -- List_Inherited_Pre_Post_Aspects --
9708 -------------------------------------
9710 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9712 if Opt.List_Inherited_Aspects
9713 and then Is_Subprogram_Or_Generic_Subprogram (E)
9716 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9721 for Index in Subps'Range loop
9722 Items := Contract (Subps (Index));
9724 if Present (Items) then
9725 Prag := Pre_Post_Conditions (Items);
9726 while Present (Prag) loop
9727 Error_Msg_Sloc := Sloc (Prag);
9729 if Class_Present (Prag)
9730 and then not Split_PPC (Prag)
9732 if Pragma_Name (Prag) = Name_Precondition then
9734 ("info: & inherits `Pre''Class` aspect from "
9738 ("info: & inherits `Post''Class` aspect from "
9743 Prag := Next_Pragma (Prag);
9749 end List_Inherited_Pre_Post_Aspects;
9751 ------------------------------
9752 -- Make_Inequality_Operator --
9753 ------------------------------
9755 -- S is the defining identifier of an equality operator. We build a
9756 -- subprogram declaration with the right signature. This operation is
9757 -- intrinsic, because it is always expanded as the negation of the
9758 -- call to the equality function.
9760 procedure Make_Inequality_Operator (S : Entity_Id) is
9761 Loc : constant Source_Ptr := Sloc (S);
9764 Op_Name : Entity_Id;
9766 FF : constant Entity_Id := First_Formal (S);
9767 NF : constant Entity_Id := Next_Formal (FF);
9770 -- Check that equality was properly defined, ignore call if not
9777 A : constant Entity_Id :=
9778 Make_Defining_Identifier (Sloc (FF),
9779 Chars => Chars (FF));
9781 B : constant Entity_Id :=
9782 Make_Defining_Identifier (Sloc (NF),
9783 Chars => Chars (NF));
9786 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9788 Formals := New_List (
9789 Make_Parameter_Specification (Loc,
9790 Defining_Identifier => A,
9792 New_Occurrence_Of (Etype (First_Formal (S)),
9793 Sloc (Etype (First_Formal (S))))),
9795 Make_Parameter_Specification (Loc,
9796 Defining_Identifier => B,
9798 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9799 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9802 Make_Subprogram_Declaration (Loc,
9804 Make_Function_Specification (Loc,
9805 Defining_Unit_Name => Op_Name,
9806 Parameter_Specifications => Formals,
9807 Result_Definition =>
9808 New_Occurrence_Of (Standard_Boolean, Loc)));
9810 -- Insert inequality right after equality if it is explicit or after
9811 -- the derived type when implicit. These entities are created only
9812 -- for visibility purposes, and eventually replaced in the course
9813 -- of expansion, so they do not need to be attached to the tree and
9814 -- seen by the back-end. Keeping them internal also avoids spurious
9815 -- freezing problems. The declaration is inserted in the tree for
9816 -- analysis, and removed afterwards. If the equality operator comes
9817 -- from an explicit declaration, attach the inequality immediately
9818 -- after. Else the equality is inherited from a derived type
9819 -- declaration, so insert inequality after that declaration.
9821 if No (Alias (S)) then
9822 Insert_After (Unit_Declaration_Node (S), Decl);
9823 elsif Is_List_Member (Parent (S)) then
9824 Insert_After (Parent (S), Decl);
9826 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9829 Mark_Rewrite_Insertion (Decl);
9830 Set_Is_Intrinsic_Subprogram (Op_Name);
9833 Set_Has_Completion (Op_Name);
9834 Set_Corresponding_Equality (Op_Name, S);
9835 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9837 end Make_Inequality_Operator;
9839 ----------------------
9840 -- May_Need_Actuals --
9841 ----------------------
9843 procedure May_Need_Actuals (Fun : Entity_Id) is
9848 F := First_Formal (Fun);
9850 while Present (F) loop
9851 if No (Default_Value (F)) then
9859 Set_Needs_No_Actuals (Fun, B);
9860 end May_Need_Actuals;
9862 ---------------------
9863 -- Mode_Conformant --
9864 ---------------------
9866 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9869 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9871 end Mode_Conformant;
9873 ---------------------------
9874 -- New_Overloaded_Entity --
9875 ---------------------------
9877 procedure New_Overloaded_Entity
9879 Derived_Type : Entity_Id := Empty)
9881 Overridden_Subp : Entity_Id := Empty;
9882 -- Set if the current scope has an operation that is type-conformant
9883 -- with S, and becomes hidden by S.
9885 Is_Primitive_Subp : Boolean;
9886 -- Set to True if the new subprogram is primitive
9889 -- Entity that S overrides
9891 Prev_Vis : Entity_Id := Empty;
9892 -- Predecessor of E in Homonym chain
9894 procedure Check_For_Primitive_Subprogram
9895 (Is_Primitive : out Boolean;
9896 Is_Overriding : Boolean := False);
9897 -- If the subprogram being analyzed is a primitive operation of the type
9898 -- of a formal or result, set the Has_Primitive_Operations flag on the
9899 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9900 -- corresponding flag on the entity itself for later use.
9902 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
9903 -- True if a) E is a subprogram whose first formal is a concurrent type
9904 -- defined in the scope of E that has some entry or subprogram whose
9905 -- profile matches E, or b) E is an internally built dispatching
9906 -- subprogram of a protected type and there is a matching subprogram
9907 -- defined in the enclosing scope of the protected type, or c) E is
9908 -- an entry of a synchronized type and a matching procedure has been
9909 -- previously defined in the enclosing scope of the synchronized type.
9911 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9912 -- Check that E is declared in the private part of the current package,
9913 -- or in the package body, where it may hide a previous declaration.
9914 -- We can't use In_Private_Part by itself because this flag is also
9915 -- set when freezing entities, so we must examine the place of the
9916 -- declaration in the tree, and recognize wrapper packages as well.
9918 function Is_Overriding_Alias
9920 New_E : Entity_Id) return Boolean;
9921 -- Check whether new subprogram and old subprogram are both inherited
9922 -- from subprograms that have distinct dispatch table entries. This can
9923 -- occur with derivations from instances with accidental homonyms. The
9924 -- function is conservative given that the converse is only true within
9925 -- instances that contain accidental overloadings.
9927 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
9928 -- Report conflict between entities S and E
9930 ------------------------------------
9931 -- Check_For_Primitive_Subprogram --
9932 ------------------------------------
9934 procedure Check_For_Primitive_Subprogram
9935 (Is_Primitive : out Boolean;
9936 Is_Overriding : Boolean := False)
9942 function Visible_Part_Type (T : Entity_Id) return Boolean;
9943 -- Returns true if T is declared in the visible part of the current
9944 -- package scope; otherwise returns false. Assumes that T is declared
9947 procedure Check_Private_Overriding (T : Entity_Id);
9948 -- Checks that if a primitive abstract subprogram of a visible
9949 -- abstract type is declared in a private part, then it must override
9950 -- an abstract subprogram declared in the visible part. Also checks
9951 -- that if a primitive function with a controlling result is declared
9952 -- in a private part, then it must override a function declared in
9953 -- the visible part.
9955 ------------------------------
9956 -- Check_Private_Overriding --
9957 ------------------------------
9959 procedure Check_Private_Overriding (T : Entity_Id) is
9960 function Overrides_Private_Part_Op return Boolean;
9961 -- This detects the special case where the overriding subprogram
9962 -- is overriding a subprogram that was declared in the same
9963 -- private part. That case is illegal by 3.9.3(10).
9965 function Overrides_Visible_Function
9966 (Partial_View : Entity_Id) return Boolean;
9967 -- True if S overrides a function in the visible part. The
9968 -- overridden function could be explicitly or implicitly declared.
9970 -------------------------------
9971 -- Overrides_Private_Part_Op --
9972 -------------------------------
9974 function Overrides_Private_Part_Op return Boolean is
9975 Over_Decl : constant Node_Id :=
9976 Unit_Declaration_Node (Overridden_Operation (S));
9977 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
9980 pragma Assert (Is_Overriding);
9982 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
9984 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
9986 return In_Same_List (Over_Decl, Subp_Decl);
9987 end Overrides_Private_Part_Op;
9989 --------------------------------
9990 -- Overrides_Visible_Function --
9991 --------------------------------
9993 function Overrides_Visible_Function
9994 (Partial_View : Entity_Id) return Boolean
9997 if not Is_Overriding or else not Has_Homonym (S) then
10001 if not Present (Partial_View) then
10005 -- Search through all the homonyms H of S in the current
10006 -- package spec, and return True if we find one that matches.
10007 -- Note that Parent (H) will be the declaration of the
10008 -- partial view of T for a match.
10011 H : Entity_Id := S;
10015 exit when not Present (H) or else Scope (H) /= Scope (S);
10019 N_Private_Extension_Declaration,
10020 N_Private_Type_Declaration)
10021 and then Defining_Identifier (Parent (H)) = Partial_View
10029 end Overrides_Visible_Function;
10031 -- Start of processing for Check_Private_Overriding
10034 if Is_Package_Or_Generic_Package (Current_Scope)
10035 and then In_Private_Part (Current_Scope)
10036 and then Visible_Part_Type (T)
10037 and then not In_Instance
10039 if Is_Abstract_Type (T)
10040 and then Is_Abstract_Subprogram (S)
10041 and then (not Is_Overriding
10042 or else not Is_Abstract_Subprogram (E)
10043 or else Overrides_Private_Part_Op)
10046 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10049 elsif Ekind (S) = E_Function then
10051 Partial_View : constant Entity_Id :=
10052 Incomplete_Or_Partial_View (T);
10055 if not Overrides_Visible_Function (Partial_View) then
10057 -- Here, S is "function ... return T;" declared in
10058 -- the private part, not overriding some visible
10059 -- operation. That's illegal in the tagged case
10060 -- (but not if the private type is untagged).
10062 if ((Present (Partial_View)
10063 and then Is_Tagged_Type (Partial_View))
10064 or else (not Present (Partial_View)
10065 and then Is_Tagged_Type (T)))
10066 and then T = Base_Type (Etype (S))
10069 ("private function with tagged result must"
10070 & " override visible-part function", S);
10072 ("\move subprogram to the visible part"
10073 & " (RM 3.9.3(10))", S);
10075 -- AI05-0073: extend this test to the case of a
10076 -- function with a controlling access result.
10078 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10079 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10081 not Is_Class_Wide_Type
10082 (Designated_Type (Etype (S)))
10083 and then Ada_Version >= Ada_2012
10086 ("private function with controlling access "
10087 & "result must override visible-part function",
10090 ("\move subprogram to the visible part"
10091 & " (RM 3.9.3(10))", S);
10097 end Check_Private_Overriding;
10099 -----------------------
10100 -- Visible_Part_Type --
10101 -----------------------
10103 function Visible_Part_Type (T : Entity_Id) return Boolean is
10104 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10108 -- If the entity is a private type, then it must be declared in a
10111 if Ekind (T) in Private_Kind then
10115 -- Otherwise, we traverse the visible part looking for its
10116 -- corresponding declaration. We cannot use the declaration
10117 -- node directly because in the private part the entity of a
10118 -- private type is the one in the full view, which does not
10119 -- indicate that it is the completion of something visible.
10121 N := First (Visible_Declarations (Specification (P)));
10122 while Present (N) loop
10123 if Nkind (N) = N_Full_Type_Declaration
10124 and then Present (Defining_Identifier (N))
10125 and then T = Defining_Identifier (N)
10129 elsif Nkind_In (N, N_Private_Type_Declaration,
10130 N_Private_Extension_Declaration)
10131 and then Present (Defining_Identifier (N))
10132 and then T = Full_View (Defining_Identifier (N))
10141 end Visible_Part_Type;
10143 -- Start of processing for Check_For_Primitive_Subprogram
10146 Is_Primitive := False;
10148 if not Comes_From_Source (S) then
10151 -- If subprogram is at library level, it is not primitive operation
10153 elsif Current_Scope = Standard_Standard then
10156 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10157 and then not In_Package_Body (Current_Scope))
10158 or else Is_Overriding
10160 -- For function, check return type
10162 if Ekind (S) = E_Function then
10163 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10164 F_Typ := Designated_Type (Etype (S));
10166 F_Typ := Etype (S);
10169 B_Typ := Base_Type (F_Typ);
10171 if Scope (B_Typ) = Current_Scope
10172 and then not Is_Class_Wide_Type (B_Typ)
10173 and then not Is_Generic_Type (B_Typ)
10175 Is_Primitive := True;
10176 Set_Has_Primitive_Operations (B_Typ);
10177 Set_Is_Primitive (S);
10178 Check_Private_Overriding (B_Typ);
10180 -- The Ghost policy in effect at the point of declaration
10181 -- or a tagged type and a primitive operation must match
10182 -- (SPARK RM 6.9(16)).
10184 Check_Ghost_Primitive (S, B_Typ);
10188 -- For all subprograms, check formals
10190 Formal := First_Formal (S);
10191 while Present (Formal) loop
10192 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10193 F_Typ := Designated_Type (Etype (Formal));
10195 F_Typ := Etype (Formal);
10198 B_Typ := Base_Type (F_Typ);
10200 if Ekind (B_Typ) = E_Access_Subtype then
10201 B_Typ := Base_Type (B_Typ);
10204 if Scope (B_Typ) = Current_Scope
10205 and then not Is_Class_Wide_Type (B_Typ)
10206 and then not Is_Generic_Type (B_Typ)
10208 Is_Primitive := True;
10209 Set_Is_Primitive (S);
10210 Set_Has_Primitive_Operations (B_Typ);
10211 Check_Private_Overriding (B_Typ);
10213 -- The Ghost policy in effect at the point of declaration
10214 -- of a tagged type and a primitive operation must match
10215 -- (SPARK RM 6.9(16)).
10217 Check_Ghost_Primitive (S, B_Typ);
10220 Next_Formal (Formal);
10223 -- Special case: An equality function can be redefined for a type
10224 -- occurring in a declarative part, and won't otherwise be treated as
10225 -- a primitive because it doesn't occur in a package spec and doesn't
10226 -- override an inherited subprogram. It's important that we mark it
10227 -- primitive so it can be returned by Collect_Primitive_Operations
10228 -- and be used in composing the equality operation of later types
10229 -- that have a component of the type.
10231 elsif Chars (S) = Name_Op_Eq
10232 and then Etype (S) = Standard_Boolean
10234 B_Typ := Base_Type (Etype (First_Formal (S)));
10236 if Scope (B_Typ) = Current_Scope
10238 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10239 and then not Is_Limited_Type (B_Typ)
10241 Is_Primitive := True;
10242 Set_Is_Primitive (S);
10243 Set_Has_Primitive_Operations (B_Typ);
10244 Check_Private_Overriding (B_Typ);
10246 -- The Ghost policy in effect at the point of declaration of a
10247 -- tagged type and a primitive operation must match
10248 -- (SPARK RM 6.9(16)).
10250 Check_Ghost_Primitive (S, B_Typ);
10253 end Check_For_Primitive_Subprogram;
10255 --------------------------------------
10256 -- Has_Matching_Entry_Or_Subprogram --
10257 --------------------------------------
10259 function Has_Matching_Entry_Or_Subprogram
10260 (E : Entity_Id) return Boolean
10262 function Check_Conforming_Parameters
10263 (E1_Param : Node_Id;
10264 E2_Param : Node_Id) return Boolean;
10265 -- Starting from the given parameters, check that all the parameters
10266 -- of two entries or subprograms are subtype conformant. Used to skip
10267 -- the check on the controlling argument.
10269 function Matching_Entry_Or_Subprogram
10270 (Conc_Typ : Entity_Id;
10271 Subp : Entity_Id) return Entity_Id;
10272 -- Return the first entry or subprogram of the given concurrent type
10273 -- whose name matches the name of Subp and has a profile conformant
10274 -- with Subp; return Empty if not found.
10276 function Matching_Dispatching_Subprogram
10277 (Conc_Typ : Entity_Id;
10278 Ent : Entity_Id) return Entity_Id;
10279 -- Return the first dispatching primitive of Conc_Type defined in the
10280 -- enclosing scope of Conc_Type (i.e. before the full definition of
10281 -- this concurrent type) whose name matches the entry Ent and has a
10282 -- profile conformant with the profile of the corresponding (not yet
10283 -- built) dispatching primitive of Ent; return Empty if not found.
10285 function Matching_Original_Protected_Subprogram
10286 (Prot_Typ : Entity_Id;
10287 Subp : Entity_Id) return Entity_Id;
10288 -- Return the first subprogram defined in the enclosing scope of
10289 -- Prot_Typ (before the full definition of this protected type)
10290 -- whose name matches the original name of Subp and has a profile
10291 -- conformant with the profile of Subp; return Empty if not found.
10293 ---------------------------------
10294 -- Check_Confirming_Parameters --
10295 ---------------------------------
10297 function Check_Conforming_Parameters
10298 (E1_Param : Node_Id;
10299 E2_Param : Node_Id) return Boolean
10301 Param_E1 : Node_Id := E1_Param;
10302 Param_E2 : Node_Id := E2_Param;
10305 while Present (Param_E1) and then Present (Param_E2) loop
10306 if Ekind (Defining_Identifier (Param_E1)) /=
10307 Ekind (Defining_Identifier (Param_E2))
10310 (Find_Parameter_Type (Param_E1),
10311 Find_Parameter_Type (Param_E2),
10312 Subtype_Conformant)
10321 -- The candidate is not valid if one of the two lists contains
10322 -- more parameters than the other
10324 return No (Param_E1) and then No (Param_E2);
10325 end Check_Conforming_Parameters;
10327 ----------------------------------
10328 -- Matching_Entry_Or_Subprogram --
10329 ----------------------------------
10331 function Matching_Entry_Or_Subprogram
10332 (Conc_Typ : Entity_Id;
10333 Subp : Entity_Id) return Entity_Id
10338 E := First_Entity (Conc_Typ);
10339 while Present (E) loop
10340 if Chars (Subp) = Chars (E)
10341 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10343 Check_Conforming_Parameters
10344 (First (Parameter_Specifications (Parent (E))),
10345 Next (First (Parameter_Specifications (Parent (Subp)))))
10354 end Matching_Entry_Or_Subprogram;
10356 -------------------------------------
10357 -- Matching_Dispatching_Subprogram --
10358 -------------------------------------
10360 function Matching_Dispatching_Subprogram
10361 (Conc_Typ : Entity_Id;
10362 Ent : Entity_Id) return Entity_Id
10367 -- Search for entities in the enclosing scope of this synchonized
10370 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10371 Push_Scope (Scope (Conc_Typ));
10372 E := Current_Entity_In_Scope (Ent);
10375 while Present (E) loop
10376 if Scope (E) = Scope (Conc_Typ)
10377 and then Comes_From_Source (E)
10378 and then Ekind (E) = E_Procedure
10379 and then Present (First_Entity (E))
10380 and then Is_Controlling_Formal (First_Entity (E))
10381 and then Etype (First_Entity (E)) = Conc_Typ
10383 Check_Conforming_Parameters
10384 (First (Parameter_Specifications (Parent (Ent))),
10385 Next (First (Parameter_Specifications (Parent (E)))))
10394 end Matching_Dispatching_Subprogram;
10396 --------------------------------------------
10397 -- Matching_Original_Protected_Subprogram --
10398 --------------------------------------------
10400 function Matching_Original_Protected_Subprogram
10401 (Prot_Typ : Entity_Id;
10402 Subp : Entity_Id) return Entity_Id
10404 ICF : constant Boolean :=
10405 Is_Controlling_Formal (First_Entity (Subp));
10409 -- Temporarily decorate the first parameter of Subp as controlling
10410 -- formal, required to invoke Subtype_Conformant.
10412 Set_Is_Controlling_Formal (First_Entity (Subp));
10415 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10417 while Present (E) loop
10418 if Scope (E) = Scope (Prot_Typ)
10419 and then Comes_From_Source (E)
10420 and then Ekind (Subp) = Ekind (E)
10421 and then Present (First_Entity (E))
10422 and then Is_Controlling_Formal (First_Entity (E))
10423 and then Etype (First_Entity (E)) = Prot_Typ
10424 and then Subtype_Conformant (Subp, E,
10425 Skip_Controlling_Formals => True)
10427 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10434 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10437 end Matching_Original_Protected_Subprogram;
10439 -- Start of processing for Has_Matching_Entry_Or_Subprogram
10442 -- Case 1: E is a subprogram whose first formal is a concurrent type
10443 -- defined in the scope of E that has an entry or subprogram whose
10444 -- profile matches E.
10446 if Comes_From_Source (E)
10447 and then Is_Subprogram (E)
10448 and then Present (First_Entity (E))
10449 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10452 Scope (Corresponding_Concurrent_Type
10453 (Etype (First_Entity (E))))
10456 (Matching_Entry_Or_Subprogram
10457 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10460 Report_Conflict (E,
10461 Matching_Entry_Or_Subprogram
10462 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10467 -- Case 2: E is an internally built dispatching subprogram of a
10468 -- protected type and there is a subprogram defined in the enclosing
10469 -- scope of the protected type that has the original name of E and
10470 -- its profile is conformant with the profile of E. We check the
10471 -- name of the original protected subprogram associated with E since
10472 -- the expander builds dispatching primitives of protected functions
10473 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
10475 elsif not Comes_From_Source (E)
10476 and then Is_Subprogram (E)
10477 and then Present (First_Entity (E))
10478 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10479 and then Present (Original_Protected_Subprogram (E))
10482 (Matching_Original_Protected_Subprogram
10483 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10486 Report_Conflict (E,
10487 Matching_Original_Protected_Subprogram
10488 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10492 -- Case 3: E is an entry of a synchronized type and a matching
10493 -- procedure has been previously defined in the enclosing scope
10494 -- of the synchronized type.
10496 elsif Comes_From_Source (E)
10497 and then Ekind (E) = E_Entry
10499 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
10501 Report_Conflict (E,
10502 Matching_Dispatching_Subprogram (Current_Scope, E));
10507 end Has_Matching_Entry_Or_Subprogram;
10509 ----------------------------
10510 -- Is_Private_Declaration --
10511 ----------------------------
10513 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10514 Decl : constant Node_Id := Unit_Declaration_Node (E);
10515 Priv_Decls : List_Id;
10518 if Is_Package_Or_Generic_Package (Current_Scope)
10519 and then In_Private_Part (Current_Scope)
10522 Private_Declarations (Package_Specification (Current_Scope));
10524 return In_Package_Body (Current_Scope)
10526 (Is_List_Member (Decl)
10527 and then List_Containing (Decl) = Priv_Decls)
10528 or else (Nkind (Parent (Decl)) = N_Package_Specification
10530 Is_Compilation_Unit
10531 (Defining_Entity (Parent (Decl)))
10532 and then List_Containing (Parent (Parent (Decl))) =
10537 end Is_Private_Declaration;
10539 --------------------------
10540 -- Is_Overriding_Alias --
10541 --------------------------
10543 function Is_Overriding_Alias
10544 (Old_E : Entity_Id;
10545 New_E : Entity_Id) return Boolean
10547 AO : constant Entity_Id := Alias (Old_E);
10548 AN : constant Entity_Id := Alias (New_E);
10551 return Scope (AO) /= Scope (AN)
10552 or else No (DTC_Entity (AO))
10553 or else No (DTC_Entity (AN))
10554 or else DT_Position (AO) = DT_Position (AN);
10555 end Is_Overriding_Alias;
10557 ---------------------
10558 -- Report_Conflict --
10559 ---------------------
10561 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
10563 Error_Msg_Sloc := Sloc (E);
10565 -- Generate message, with useful additional warning if in generic
10567 if Is_Generic_Unit (E) then
10568 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10569 Error_Msg_N ("\& conflicts with declaration#", S);
10571 Error_Msg_N ("& conflicts with declaration#", S);
10573 end Report_Conflict;
10575 -- Start of processing for New_Overloaded_Entity
10578 -- We need to look for an entity that S may override. This must be a
10579 -- homonym in the current scope, so we look for the first homonym of
10580 -- S in the current scope as the starting point for the search.
10582 E := Current_Entity_In_Scope (S);
10584 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10585 -- They are directly added to the list of primitive operations of
10586 -- Derived_Type, unless this is a rederivation in the private part
10587 -- of an operation that was already derived in the visible part of
10588 -- the current package.
10590 if Ada_Version >= Ada_2005
10591 and then Present (Derived_Type)
10592 and then Present (Alias (S))
10593 and then Is_Dispatching_Operation (Alias (S))
10594 and then Present (Find_Dispatching_Type (Alias (S)))
10595 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10597 -- For private types, when the full-view is processed we propagate to
10598 -- the full view the non-overridden entities whose attribute "alias"
10599 -- references an interface primitive. These entities were added by
10600 -- Derive_Subprograms to ensure that interface primitives are
10603 -- Inside_Freeze_Actions is non zero when S corresponds with an
10604 -- internal entity that links an interface primitive with its
10605 -- covering primitive through attribute Interface_Alias (see
10606 -- Add_Internal_Interface_Entities).
10608 if Inside_Freezing_Actions = 0
10609 and then Is_Package_Or_Generic_Package (Current_Scope)
10610 and then In_Private_Part (Current_Scope)
10611 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10612 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10613 and then Full_View (Defining_Identifier (Parent (E)))
10614 = Defining_Identifier (Parent (S))
10615 and then Alias (E) = Alias (S)
10617 Check_Operation_From_Private_View (S, E);
10618 Set_Is_Dispatching_Operation (S);
10623 Enter_Overloaded_Entity (S);
10624 Check_Dispatching_Operation (S, Empty);
10625 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10631 -- For synchronized types check conflicts of this entity with previously
10632 -- defined entities.
10634 if Ada_Version >= Ada_2005
10635 and then Has_Matching_Entry_Or_Subprogram (S)
10640 -- If there is no homonym then this is definitely not overriding
10643 Enter_Overloaded_Entity (S);
10644 Check_Dispatching_Operation (S, Empty);
10645 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10647 -- If subprogram has an explicit declaration, check whether it has an
10648 -- overriding indicator.
10650 if Comes_From_Source (S) then
10651 Check_Synchronized_Overriding (S, Overridden_Subp);
10653 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10654 -- it may have overridden some hidden inherited primitive. Update
10655 -- Overridden_Subp to avoid spurious errors when checking the
10656 -- overriding indicator.
10658 if Ada_Version >= Ada_2012
10659 and then No (Overridden_Subp)
10660 and then Is_Dispatching_Operation (S)
10661 and then Present (Overridden_Operation (S))
10663 Overridden_Subp := Overridden_Operation (S);
10666 Check_Overriding_Indicator
10667 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10669 -- The Ghost policy in effect at the point of declaration of a
10670 -- parent subprogram and an overriding subprogram must match
10671 -- (SPARK RM 6.9(17)).
10673 Check_Ghost_Overriding (S, Overridden_Subp);
10676 -- If there is a homonym that is not overloadable, then we have an
10677 -- error, except for the special cases checked explicitly below.
10679 elsif not Is_Overloadable (E) then
10681 -- Check for spurious conflict produced by a subprogram that has the
10682 -- same name as that of the enclosing generic package. The conflict
10683 -- occurs within an instance, between the subprogram and the renaming
10684 -- declaration for the package. After the subprogram, the package
10685 -- renaming declaration becomes hidden.
10687 if Ekind (E) = E_Package
10688 and then Present (Renamed_Object (E))
10689 and then Renamed_Object (E) = Current_Scope
10690 and then Nkind (Parent (Renamed_Object (E))) =
10691 N_Package_Specification
10692 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10695 Set_Is_Immediately_Visible (E, False);
10696 Enter_Overloaded_Entity (S);
10697 Set_Homonym (S, Homonym (E));
10698 Check_Dispatching_Operation (S, Empty);
10699 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10701 -- If the subprogram is implicit it is hidden by the previous
10702 -- declaration. However if it is dispatching, it must appear in the
10703 -- dispatch table anyway, because it can be dispatched to even if it
10704 -- cannot be called directly.
10706 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10707 Set_Scope (S, Current_Scope);
10709 if Is_Dispatching_Operation (Alias (S)) then
10710 Check_Dispatching_Operation (S, Empty);
10716 Report_Conflict (S, E);
10720 -- E exists and is overloadable
10723 Check_Synchronized_Overriding (S, Overridden_Subp);
10725 -- Loop through E and its homonyms to determine if any of them is
10726 -- the candidate for overriding by S.
10728 while Present (E) loop
10730 -- Definitely not interesting if not in the current scope
10732 if Scope (E) /= Current_Scope then
10735 -- A function can overload the name of an abstract state. The
10736 -- state can be viewed as a function with a profile that cannot
10737 -- be matched by anything.
10739 elsif Ekind (S) = E_Function
10740 and then Ekind (E) = E_Abstract_State
10742 Enter_Overloaded_Entity (S);
10745 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10746 -- procedures locate the internally generated spec. We enforce
10747 -- mode conformance since a tagged type may inherit from
10748 -- interfaces several null primitives which differ only in
10749 -- the mode of the formals.
10751 elsif not Comes_From_Source (S)
10752 and then Is_Null_Procedure (S)
10753 and then not Mode_Conformant (E, S)
10757 -- Check if we have type conformance
10759 elsif Type_Conformant (E, S) then
10761 -- If the old and new entities have the same profile and one
10762 -- is not the body of the other, then this is an error, unless
10763 -- one of them is implicitly declared.
10765 -- There are some cases when both can be implicit, for example
10766 -- when both a literal and a function that overrides it are
10767 -- inherited in a derivation, or when an inherited operation
10768 -- of a tagged full type overrides the inherited operation of
10769 -- a private extension. Ada 83 had a special rule for the
10770 -- literal case. In Ada 95, the later implicit operation hides
10771 -- the former, and the literal is always the former. In the
10772 -- odd case where both are derived operations declared at the
10773 -- same point, both operations should be declared, and in that
10774 -- case we bypass the following test and proceed to the next
10775 -- part. This can only occur for certain obscure cases in
10776 -- instances, when an operation on a type derived from a formal
10777 -- private type does not override a homograph inherited from
10778 -- the actual. In subsequent derivations of such a type, the
10779 -- DT positions of these operations remain distinct, if they
10782 if Present (Alias (S))
10783 and then (No (Alias (E))
10784 or else Comes_From_Source (E)
10785 or else Is_Abstract_Subprogram (S)
10787 (Is_Dispatching_Operation (E)
10788 and then Is_Overriding_Alias (E, S)))
10789 and then Ekind (E) /= E_Enumeration_Literal
10791 -- When an derived operation is overloaded it may be due to
10792 -- the fact that the full view of a private extension
10793 -- re-inherits. It has to be dealt with.
10795 if Is_Package_Or_Generic_Package (Current_Scope)
10796 and then In_Private_Part (Current_Scope)
10798 Check_Operation_From_Private_View (S, E);
10801 -- In any case the implicit operation remains hidden by the
10802 -- existing declaration, which is overriding. Indicate that
10803 -- E overrides the operation from which S is inherited.
10805 if Present (Alias (S)) then
10806 Set_Overridden_Operation (E, Alias (S));
10807 Inherit_Subprogram_Contract (E, Alias (S));
10810 Set_Overridden_Operation (E, S);
10811 Inherit_Subprogram_Contract (E, S);
10814 if Comes_From_Source (E) then
10815 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10817 -- The Ghost policy in effect at the point of declaration
10818 -- of a parent subprogram and an overriding subprogram
10819 -- must match (SPARK RM 6.9(17)).
10821 Check_Ghost_Overriding (E, S);
10826 -- Within an instance, the renaming declarations for actual
10827 -- subprograms may become ambiguous, but they do not hide each
10830 elsif Ekind (E) /= E_Entry
10831 and then not Comes_From_Source (E)
10832 and then not Is_Generic_Instance (E)
10833 and then (Present (Alias (E))
10834 or else Is_Intrinsic_Subprogram (E))
10835 and then (not In_Instance
10836 or else No (Parent (E))
10837 or else Nkind (Unit_Declaration_Node (E)) /=
10838 N_Subprogram_Renaming_Declaration)
10840 -- A subprogram child unit is not allowed to override an
10841 -- inherited subprogram (10.1.1(20)).
10843 if Is_Child_Unit (S) then
10845 ("child unit overrides inherited subprogram in parent",
10850 if Is_Non_Overriding_Operation (E, S) then
10851 Enter_Overloaded_Entity (S);
10853 if No (Derived_Type)
10854 or else Is_Tagged_Type (Derived_Type)
10856 Check_Dispatching_Operation (S, Empty);
10862 -- E is a derived operation or an internal operator which
10863 -- is being overridden. Remove E from further visibility.
10864 -- Furthermore, if E is a dispatching operation, it must be
10865 -- replaced in the list of primitive operations of its type
10866 -- (see Override_Dispatching_Operation).
10868 Overridden_Subp := E;
10874 Prev := First_Entity (Current_Scope);
10875 while Present (Prev) and then Next_Entity (Prev) /= E loop
10876 Next_Entity (Prev);
10879 -- It is possible for E to be in the current scope and
10880 -- yet not in the entity chain. This can only occur in a
10881 -- generic context where E is an implicit concatenation
10882 -- in the formal part, because in a generic body the
10883 -- entity chain starts with the formals.
10885 -- In GNATprove mode, a wrapper for an operation with
10886 -- axiomatization may be a homonym of another declaration
10887 -- for an actual subprogram (needs refinement ???).
10891 and then GNATprove_Mode
10893 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10894 N_Subprogram_Renaming_Declaration
10898 pragma Assert (Chars (E) = Name_Op_Concat);
10903 -- E must be removed both from the entity_list of the
10904 -- current scope, and from the visibility chain.
10906 if Debug_Flag_E then
10907 Write_Str ("Override implicit operation ");
10908 Write_Int (Int (E));
10912 -- If E is a predefined concatenation, it stands for four
10913 -- different operations. As a result, a single explicit
10914 -- declaration does not hide it. In a possible ambiguous
10915 -- situation, Disambiguate chooses the user-defined op,
10916 -- so it is correct to retain the previous internal one.
10918 if Chars (E) /= Name_Op_Concat
10919 or else Ekind (E) /= E_Operator
10921 -- For nondispatching derived operations that are
10922 -- overridden by a subprogram declared in the private
10923 -- part of a package, we retain the derived subprogram
10924 -- but mark it as not immediately visible. If the
10925 -- derived operation was declared in the visible part
10926 -- then this ensures that it will still be visible
10927 -- outside the package with the proper signature
10928 -- (calls from outside must also be directed to this
10929 -- version rather than the overriding one, unlike the
10930 -- dispatching case). Calls from inside the package
10931 -- will still resolve to the overriding subprogram
10932 -- since the derived one is marked as not visible
10933 -- within the package.
10935 -- If the private operation is dispatching, we achieve
10936 -- the overriding by keeping the implicit operation
10937 -- but setting its alias to be the overriding one. In
10938 -- this fashion the proper body is executed in all
10939 -- cases, but the original signature is used outside
10942 -- If the overriding is not in the private part, we
10943 -- remove the implicit operation altogether.
10945 if Is_Private_Declaration (S) then
10946 if not Is_Dispatching_Operation (E) then
10947 Set_Is_Immediately_Visible (E, False);
10949 -- Work done in Override_Dispatching_Operation,
10950 -- so nothing else needs to be done here.
10956 -- Find predecessor of E in Homonym chain
10958 if E = Current_Entity (E) then
10961 Prev_Vis := Current_Entity (E);
10962 while Homonym (Prev_Vis) /= E loop
10963 Prev_Vis := Homonym (Prev_Vis);
10967 if Prev_Vis /= Empty then
10969 -- Skip E in the visibility chain
10971 Set_Homonym (Prev_Vis, Homonym (E));
10974 Set_Name_Entity_Id (Chars (E), Homonym (E));
10977 Set_Next_Entity (Prev, Next_Entity (E));
10979 if No (Next_Entity (Prev)) then
10980 Set_Last_Entity (Current_Scope, Prev);
10985 Enter_Overloaded_Entity (S);
10987 -- For entities generated by Derive_Subprograms the
10988 -- overridden operation is the inherited primitive
10989 -- (which is available through the attribute alias).
10991 if not (Comes_From_Source (E))
10992 and then Is_Dispatching_Operation (E)
10993 and then Find_Dispatching_Type (E) =
10994 Find_Dispatching_Type (S)
10995 and then Present (Alias (E))
10996 and then Comes_From_Source (Alias (E))
10998 Set_Overridden_Operation (S, Alias (E));
10999 Inherit_Subprogram_Contract (S, Alias (E));
11001 -- Normal case of setting entity as overridden
11003 -- Note: Static_Initialization and Overridden_Operation
11004 -- attributes use the same field in subprogram entities.
11005 -- Static_Initialization is only defined for internal
11006 -- initialization procedures, where Overridden_Operation
11007 -- is irrelevant. Therefore the setting of this attribute
11008 -- must check whether the target is an init_proc.
11010 elsif not Is_Init_Proc (S) then
11011 Set_Overridden_Operation (S, E);
11012 Inherit_Subprogram_Contract (S, E);
11015 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11017 -- The Ghost policy in effect at the point of declaration
11018 -- of a parent subprogram and an overriding subprogram
11019 -- must match (SPARK RM 6.9(17)).
11021 Check_Ghost_Overriding (S, E);
11023 -- If S is a user-defined subprogram or a null procedure
11024 -- expanded to override an inherited null procedure, or a
11025 -- predefined dispatching primitive then indicate that E
11026 -- overrides the operation from which S is inherited.
11028 if Comes_From_Source (S)
11030 (Present (Parent (S))
11032 Nkind (Parent (S)) = N_Procedure_Specification
11034 Null_Present (Parent (S)))
11036 (Present (Alias (E))
11038 Is_Predefined_Dispatching_Operation (Alias (E)))
11040 if Present (Alias (E)) then
11041 Set_Overridden_Operation (S, Alias (E));
11042 Inherit_Subprogram_Contract (S, Alias (E));
11046 if Is_Dispatching_Operation (E) then
11048 -- An overriding dispatching subprogram inherits the
11049 -- convention of the overridden subprogram (AI-117).
11051 Set_Convention (S, Convention (E));
11052 Check_Dispatching_Operation (S, E);
11055 Check_Dispatching_Operation (S, Empty);
11058 Check_For_Primitive_Subprogram
11059 (Is_Primitive_Subp, Is_Overriding => True);
11060 goto Check_Inequality;
11063 -- Apparent redeclarations in instances can occur when two
11064 -- formal types get the same actual type. The subprograms in
11065 -- in the instance are legal, even if not callable from the
11066 -- outside. Calls from within are disambiguated elsewhere.
11067 -- For dispatching operations in the visible part, the usual
11068 -- rules apply, and operations with the same profile are not
11069 -- legal (B830001).
11071 elsif (In_Instance_Visible_Part
11072 and then not Is_Dispatching_Operation (E))
11073 or else In_Instance_Not_Visible
11077 -- Here we have a real error (identical profile)
11080 Error_Msg_Sloc := Sloc (E);
11082 -- Avoid cascaded errors if the entity appears in
11083 -- subsequent calls.
11085 Set_Scope (S, Current_Scope);
11087 -- Generate error, with extra useful warning for the case
11088 -- of a generic instance with no completion.
11090 if Is_Generic_Instance (S)
11091 and then not Has_Completion (E)
11094 ("instantiation cannot provide body for&", S);
11095 Error_Msg_N ("\& conflicts with declaration#", S);
11097 Error_Msg_N ("& conflicts with declaration#", S);
11104 -- If one subprogram has an access parameter and the other
11105 -- a parameter of an access type, calls to either might be
11106 -- ambiguous. Verify that parameters match except for the
11107 -- access parameter.
11109 if May_Hide_Profile then
11115 F1 := First_Formal (S);
11116 F2 := First_Formal (E);
11117 while Present (F1) and then Present (F2) loop
11118 if Is_Access_Type (Etype (F1)) then
11119 if not Is_Access_Type (Etype (F2))
11120 or else not Conforming_Types
11121 (Designated_Type (Etype (F1)),
11122 Designated_Type (Etype (F2)),
11125 May_Hide_Profile := False;
11129 not Conforming_Types
11130 (Etype (F1), Etype (F2), Type_Conformant)
11132 May_Hide_Profile := False;
11139 if May_Hide_Profile
11143 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11152 -- On exit, we know that S is a new entity
11154 Enter_Overloaded_Entity (S);
11155 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11156 Check_Overriding_Indicator
11157 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11159 -- The Ghost policy in effect at the point of declaration of a parent
11160 -- subprogram and an overriding subprogram must match
11161 -- (SPARK RM 6.9(17)).
11163 Check_Ghost_Overriding (S, Overridden_Subp);
11165 -- Overloading is not allowed in SPARK, except for operators
11167 if Nkind (S) /= N_Defining_Operator_Symbol then
11168 Error_Msg_Sloc := Sloc (Homonym (S));
11169 Check_SPARK_05_Restriction
11170 ("overloading not allowed with entity#", S);
11173 -- If S is a derived operation for an untagged type then by
11174 -- definition it's not a dispatching operation (even if the parent
11175 -- operation was dispatching), so Check_Dispatching_Operation is not
11176 -- called in that case.
11178 if No (Derived_Type)
11179 or else Is_Tagged_Type (Derived_Type)
11181 Check_Dispatching_Operation (S, Empty);
11185 -- If this is a user-defined equality operator that is not a derived
11186 -- subprogram, create the corresponding inequality. If the operation is
11187 -- dispatching, the expansion is done elsewhere, and we do not create
11188 -- an explicit inequality operation.
11190 <<Check_Inequality>>
11191 if Chars (S) = Name_Op_Eq
11192 and then Etype (S) = Standard_Boolean
11193 and then Present (Parent (S))
11194 and then not Is_Dispatching_Operation (S)
11196 Make_Inequality_Operator (S);
11197 Check_Untagged_Equality (S);
11199 end New_Overloaded_Entity;
11201 ---------------------
11202 -- Process_Formals --
11203 ---------------------
11205 procedure Process_Formals
11207 Related_Nod : Node_Id)
11209 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11210 -- Determine whether an access type designates a type coming from a
11213 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11214 -- Check whether the default has a class-wide type. After analysis the
11215 -- default has the type of the formal, so we must also check explicitly
11216 -- for an access attribute.
11218 ----------------------------------
11219 -- Designates_From_Limited_With --
11220 ----------------------------------
11222 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11223 Desig : Entity_Id := Typ;
11226 if Is_Access_Type (Desig) then
11227 Desig := Directly_Designated_Type (Desig);
11230 if Is_Class_Wide_Type (Desig) then
11231 Desig := Root_Type (Desig);
11235 Ekind (Desig) = E_Incomplete_Type
11236 and then From_Limited_With (Desig);
11237 end Designates_From_Limited_With;
11239 ---------------------------
11240 -- Is_Class_Wide_Default --
11241 ---------------------------
11243 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11245 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11246 or else (Nkind (D) = N_Attribute_Reference
11247 and then Attribute_Name (D) = Name_Access
11248 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11249 end Is_Class_Wide_Default;
11253 Context : constant Node_Id := Parent (Parent (T));
11255 Formal : Entity_Id;
11256 Formal_Type : Entity_Id;
11257 Param_Spec : Node_Id;
11260 Num_Out_Params : Nat := 0;
11261 First_Out_Param : Entity_Id := Empty;
11262 -- Used for setting Is_Only_Out_Parameter
11264 -- Start of processing for Process_Formals
11267 -- In order to prevent premature use of the formals in the same formal
11268 -- part, the Ekind is left undefined until all default expressions are
11269 -- analyzed. The Ekind is established in a separate loop at the end.
11271 Param_Spec := First (T);
11272 while Present (Param_Spec) loop
11273 Formal := Defining_Identifier (Param_Spec);
11274 Set_Never_Set_In_Source (Formal, True);
11275 Enter_Name (Formal);
11277 -- Case of ordinary parameters
11279 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11280 Find_Type (Parameter_Type (Param_Spec));
11281 Ptype := Parameter_Type (Param_Spec);
11283 if Ptype = Error then
11287 Formal_Type := Entity (Ptype);
11289 if Is_Incomplete_Type (Formal_Type)
11291 (Is_Class_Wide_Type (Formal_Type)
11292 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11294 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11295 -- primitive operations, as long as their completion is
11296 -- in the same declarative part. If in the private part
11297 -- this means that the type cannot be a Taft-amendment type.
11298 -- Check is done on package exit. For access to subprograms,
11299 -- the use is legal for Taft-amendment types.
11301 -- Ada 2012: tagged incomplete types are allowed as generic
11302 -- formal types. They do not introduce dependencies and the
11303 -- corresponding generic subprogram does not have a delayed
11304 -- freeze, because it does not need a freeze node. However,
11305 -- it is still the case that untagged incomplete types cannot
11306 -- be Taft-amendment types and must be completed in private
11307 -- part, so the subprogram must appear in the list of private
11308 -- dependents of the type.
11310 if Is_Tagged_Type (Formal_Type)
11311 or else (Ada_Version >= Ada_2012
11312 and then not From_Limited_With (Formal_Type)
11313 and then not Is_Generic_Type (Formal_Type))
11315 if Ekind (Scope (Current_Scope)) = E_Package
11316 and then not Is_Generic_Type (Formal_Type)
11317 and then not Is_Class_Wide_Type (Formal_Type)
11320 (Parent (T), N_Access_Function_Definition,
11321 N_Access_Procedure_Definition)
11323 Append_Elmt (Current_Scope,
11324 Private_Dependents (Base_Type (Formal_Type)));
11326 -- Freezing is delayed to ensure that Register_Prim
11327 -- will get called for this operation, which is needed
11328 -- in cases where static dispatch tables aren't built.
11329 -- (Note that the same is done for controlling access
11330 -- parameter cases in function Access_Definition.)
11332 if not Is_Thunk (Current_Scope) then
11333 Set_Has_Delayed_Freeze (Current_Scope);
11338 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11339 N_Access_Procedure_Definition)
11341 -- AI05-0151: Tagged incomplete types are allowed in all
11342 -- formal parts. Untagged incomplete types are not allowed
11343 -- in bodies. Limited views of either kind are not allowed
11344 -- if there is no place at which the non-limited view can
11345 -- become available.
11347 -- Incomplete formal untagged types are not allowed in
11348 -- subprogram bodies (but are legal in their declarations).
11349 -- This excludes bodies created for null procedures, which
11350 -- are basic declarations.
11352 if Is_Generic_Type (Formal_Type)
11353 and then not Is_Tagged_Type (Formal_Type)
11354 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11357 ("invalid use of formal incomplete type", Param_Spec);
11359 elsif Ada_Version >= Ada_2012 then
11360 if Is_Tagged_Type (Formal_Type)
11361 and then (not From_Limited_With (Formal_Type)
11362 or else not In_Package_Body)
11366 elsif Nkind_In (Context, N_Accept_Statement,
11367 N_Accept_Alternative,
11369 or else (Nkind (Context) = N_Subprogram_Body
11370 and then Comes_From_Source (Context))
11373 ("invalid use of untagged incomplete type &",
11374 Ptype, Formal_Type);
11379 ("invalid use of incomplete type&",
11380 Param_Spec, Formal_Type);
11382 -- Further checks on the legality of incomplete types
11383 -- in formal parts are delayed until the freeze point
11384 -- of the enclosing subprogram or access to subprogram.
11388 elsif Ekind (Formal_Type) = E_Void then
11390 ("premature use of&",
11391 Parameter_Type (Param_Spec), Formal_Type);
11394 -- Ada 2012 (AI-142): Handle aliased parameters
11396 if Ada_Version >= Ada_2012
11397 and then Aliased_Present (Param_Spec)
11399 Set_Is_Aliased (Formal);
11402 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11403 -- declaration corresponding to the null-excluding type of the
11404 -- formal in the enclosing scope. Finally, replace the parameter
11405 -- type of the formal with the internal subtype.
11407 if Ada_Version >= Ada_2005
11408 and then Null_Exclusion_Present (Param_Spec)
11410 if not Is_Access_Type (Formal_Type) then
11412 ("`NOT NULL` allowed only for an access type", Param_Spec);
11415 if Can_Never_Be_Null (Formal_Type)
11416 and then Comes_From_Source (Related_Nod)
11419 ("`NOT NULL` not allowed (& already excludes null)",
11420 Param_Spec, Formal_Type);
11424 Create_Null_Excluding_Itype
11426 Related_Nod => Related_Nod,
11427 Scope_Id => Scope (Current_Scope));
11429 -- If the designated type of the itype is an itype that is
11430 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11431 -- on the access subtype, to prevent order-of-elaboration
11432 -- issues in the backend.
11435 -- type T is access procedure;
11436 -- procedure Op (O : not null T);
11438 if Is_Itype (Directly_Designated_Type (Formal_Type))
11440 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11442 Set_Has_Delayed_Freeze (Formal_Type);
11447 -- An access formal type
11451 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11453 -- No need to continue if we already notified errors
11455 if not Present (Formal_Type) then
11459 -- Ada 2005 (AI-254)
11462 AD : constant Node_Id :=
11463 Access_To_Subprogram_Definition
11464 (Parameter_Type (Param_Spec));
11466 if Present (AD) and then Protected_Present (AD) then
11468 Replace_Anonymous_Access_To_Protected_Subprogram
11474 Set_Etype (Formal, Formal_Type);
11476 -- Deal with default expression if present
11478 Default := Expression (Param_Spec);
11480 if Present (Default) then
11481 Check_SPARK_05_Restriction
11482 ("default expression is not allowed", Default);
11484 if Out_Present (Param_Spec) then
11486 ("default initialization only allowed for IN parameters",
11490 -- Do the special preanalysis of the expression (see section on
11491 -- "Handling of Default Expressions" in the spec of package Sem).
11493 Preanalyze_Spec_Expression (Default, Formal_Type);
11495 -- An access to constant cannot be the default for
11496 -- an access parameter that is an access to variable.
11498 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11499 and then not Is_Access_Constant (Formal_Type)
11500 and then Is_Access_Type (Etype (Default))
11501 and then Is_Access_Constant (Etype (Default))
11504 ("formal that is access to variable cannot be initialized "
11505 & "with an access-to-constant expression", Default);
11508 -- Check that the designated type of an access parameter's default
11509 -- is not a class-wide type unless the parameter's designated type
11510 -- is also class-wide.
11512 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11513 and then not Designates_From_Limited_With (Formal_Type)
11514 and then Is_Class_Wide_Default (Default)
11515 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11518 ("access to class-wide expression not allowed here", Default);
11521 -- Check incorrect use of dynamically tagged expressions
11523 if Is_Tagged_Type (Formal_Type) then
11524 Check_Dynamically_Tagged_Expression
11526 Typ => Formal_Type,
11527 Related_Nod => Default);
11531 -- Ada 2005 (AI-231): Static checks
11533 if Ada_Version >= Ada_2005
11534 and then Is_Access_Type (Etype (Formal))
11535 and then Can_Never_Be_Null (Etype (Formal))
11537 Null_Exclusion_Static_Checks (Param_Spec);
11540 -- The following checks are relevant only when SPARK_Mode is on as
11541 -- these are not standard Ada legality rules.
11543 if SPARK_Mode = On then
11544 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11546 -- A function cannot have a parameter of mode IN OUT or OUT
11549 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11551 ("function cannot have parameter of mode `OUT` or "
11552 & "`IN OUT`", Formal);
11555 -- A procedure cannot have an effectively volatile formal
11556 -- parameter of mode IN because it behaves as a constant
11557 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
11559 elsif Ekind (Scope (Formal)) = E_Procedure
11560 and then Ekind (Formal) = E_In_Parameter
11561 and then Is_Effectively_Volatile (Formal)
11564 ("formal parameter of mode `IN` cannot be volatile", Formal);
11572 -- If this is the formal part of a function specification, analyze the
11573 -- subtype mark in the context where the formals are visible but not
11574 -- yet usable, and may hide outer homographs.
11576 if Nkind (Related_Nod) = N_Function_Specification then
11577 Analyze_Return_Type (Related_Nod);
11580 -- Now set the kind (mode) of each formal
11582 Param_Spec := First (T);
11583 while Present (Param_Spec) loop
11584 Formal := Defining_Identifier (Param_Spec);
11585 Set_Formal_Mode (Formal);
11587 if Ekind (Formal) = E_In_Parameter then
11588 Set_Default_Value (Formal, Expression (Param_Spec));
11590 if Present (Expression (Param_Spec)) then
11591 Default := Expression (Param_Spec);
11593 if Is_Scalar_Type (Etype (Default)) then
11594 if Nkind (Parameter_Type (Param_Spec)) /=
11595 N_Access_Definition
11597 Formal_Type := Entity (Parameter_Type (Param_Spec));
11601 (Related_Nod, Parameter_Type (Param_Spec));
11604 Apply_Scalar_Range_Check (Default, Formal_Type);
11608 elsif Ekind (Formal) = E_Out_Parameter then
11609 Num_Out_Params := Num_Out_Params + 1;
11611 if Num_Out_Params = 1 then
11612 First_Out_Param := Formal;
11615 elsif Ekind (Formal) = E_In_Out_Parameter then
11616 Num_Out_Params := Num_Out_Params + 1;
11619 -- Skip remaining processing if formal type was in error
11621 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11622 goto Next_Parameter;
11625 -- Force call by reference if aliased
11628 Conv : constant Convention_Id := Convention (Etype (Formal));
11630 if Is_Aliased (Formal) then
11631 Set_Mechanism (Formal, By_Reference);
11633 -- Warn if user asked this to be passed by copy
11635 if Conv = Convention_Ada_Pass_By_Copy then
11637 ("cannot pass aliased parameter & by copy??", Formal);
11640 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11642 elsif Conv = Convention_Ada_Pass_By_Copy then
11643 Set_Mechanism (Formal, By_Copy);
11645 elsif Conv = Convention_Ada_Pass_By_Reference then
11646 Set_Mechanism (Formal, By_Reference);
11654 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11655 Set_Is_Only_Out_Parameter (First_Out_Param);
11657 end Process_Formals;
11659 ----------------------------
11660 -- Reference_Body_Formals --
11661 ----------------------------
11663 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11668 if Error_Posted (Spec) then
11672 -- Iterate over both lists. They may be of different lengths if the two
11673 -- specs are not conformant.
11675 Fs := First_Formal (Spec);
11676 Fb := First_Formal (Bod);
11677 while Present (Fs) and then Present (Fb) loop
11678 Generate_Reference (Fs, Fb, 'b');
11680 if Style_Check then
11681 Style.Check_Identifier (Fb, Fs);
11684 Set_Spec_Entity (Fb, Fs);
11685 Set_Referenced (Fs, False);
11689 end Reference_Body_Formals;
11691 -------------------------
11692 -- Set_Actual_Subtypes --
11693 -------------------------
11695 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11697 Formal : Entity_Id;
11699 First_Stmt : Node_Id := Empty;
11700 AS_Needed : Boolean;
11703 -- If this is an empty initialization procedure, no need to create
11704 -- actual subtypes (small optimization).
11706 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11709 -- Within a predicate function we do not want to generate local
11710 -- subtypes that may generate nested predicate functions.
11712 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
11716 -- The subtype declarations may freeze the formals. The body generated
11717 -- for an expression function is not a freeze point, so do not emit
11718 -- these declarations (small loss of efficiency in rare cases).
11720 if Nkind (N) = N_Subprogram_Body
11721 and then Was_Expression_Function (N)
11726 Formal := First_Formal (Subp);
11727 while Present (Formal) loop
11728 T := Etype (Formal);
11730 -- We never need an actual subtype for a constrained formal
11732 if Is_Constrained (T) then
11733 AS_Needed := False;
11735 -- If we have unknown discriminants, then we do not need an actual
11736 -- subtype, or more accurately we cannot figure it out. Note that
11737 -- all class-wide types have unknown discriminants.
11739 elsif Has_Unknown_Discriminants (T) then
11740 AS_Needed := False;
11742 -- At this stage we have an unconstrained type that may need an
11743 -- actual subtype. For sure the actual subtype is needed if we have
11744 -- an unconstrained array type. However, in an instance, the type
11745 -- may appear as a subtype of the full view, while the actual is
11746 -- in fact private (in which case no actual subtype is needed) so
11747 -- check the kind of the base type.
11749 elsif Is_Array_Type (Base_Type (T)) then
11752 -- The only other case needing an actual subtype is an unconstrained
11753 -- record type which is an IN parameter (we cannot generate actual
11754 -- subtypes for the OUT or IN OUT case, since an assignment can
11755 -- change the discriminant values. However we exclude the case of
11756 -- initialization procedures, since discriminants are handled very
11757 -- specially in this context, see the section entitled "Handling of
11758 -- Discriminants" in Einfo.
11760 -- We also exclude the case of Discrim_SO_Functions (functions used
11761 -- in front-end layout mode for size/offset values), since in such
11762 -- functions only discriminants are referenced, and not only are such
11763 -- subtypes not needed, but they cannot always be generated, because
11764 -- of order of elaboration issues.
11766 elsif Is_Record_Type (T)
11767 and then Ekind (Formal) = E_In_Parameter
11768 and then Chars (Formal) /= Name_uInit
11769 and then not Is_Unchecked_Union (T)
11770 and then not Is_Discrim_SO_Function (Subp)
11774 -- All other cases do not need an actual subtype
11777 AS_Needed := False;
11780 -- Generate actual subtypes for unconstrained arrays and
11781 -- unconstrained discriminated records.
11784 if Nkind (N) = N_Accept_Statement then
11786 -- If expansion is active, the formal is replaced by a local
11787 -- variable that renames the corresponding entry of the
11788 -- parameter block, and it is this local variable that may
11789 -- require an actual subtype.
11791 if Expander_Active then
11792 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11794 Decl := Build_Actual_Subtype (T, Formal);
11797 if Present (Handled_Statement_Sequence (N)) then
11799 First (Statements (Handled_Statement_Sequence (N)));
11800 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11801 Mark_Rewrite_Insertion (Decl);
11803 -- If the accept statement has no body, there will be no
11804 -- reference to the actuals, so no need to compute actual
11811 Decl := Build_Actual_Subtype (T, Formal);
11812 Prepend (Decl, Declarations (N));
11813 Mark_Rewrite_Insertion (Decl);
11816 -- The declaration uses the bounds of an existing object, and
11817 -- therefore needs no constraint checks.
11819 Analyze (Decl, Suppress => All_Checks);
11820 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
11822 -- We need to freeze manually the generated type when it is
11823 -- inserted anywhere else than in a declarative part.
11825 if Present (First_Stmt) then
11826 Insert_List_Before_And_Analyze (First_Stmt,
11827 Freeze_Entity (Defining_Identifier (Decl), N));
11829 -- Ditto if the type has a dynamic predicate, because the
11830 -- generated function will mention the actual subtype. The
11831 -- predicate may come from an explicit aspect of be inherited.
11833 elsif Has_Predicates (T) then
11834 Insert_List_Before_And_Analyze (Decl,
11835 Freeze_Entity (Defining_Identifier (Decl), N));
11838 if Nkind (N) = N_Accept_Statement
11839 and then Expander_Active
11841 Set_Actual_Subtype (Renamed_Object (Formal),
11842 Defining_Identifier (Decl));
11844 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11848 Next_Formal (Formal);
11850 end Set_Actual_Subtypes;
11852 ---------------------
11853 -- Set_Formal_Mode --
11854 ---------------------
11856 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11857 Spec : constant Node_Id := Parent (Formal_Id);
11858 Id : constant Entity_Id := Scope (Formal_Id);
11861 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11862 -- since we ensure that corresponding actuals are always valid at the
11863 -- point of the call.
11865 if Out_Present (Spec) then
11866 if Ekind_In (Id, E_Entry, E_Entry_Family)
11867 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11869 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11872 if Ekind_In (Id, E_Function, E_Generic_Function) then
11874 -- [IN] OUT parameters allowed for functions in Ada 2012
11876 if Ada_Version >= Ada_2012 then
11878 -- Even in Ada 2012 operators can only have IN parameters
11880 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11881 Error_Msg_N ("operators can only have IN parameters", Spec);
11884 if In_Present (Spec) then
11885 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11887 Set_Ekind (Formal_Id, E_Out_Parameter);
11890 -- But not in earlier versions of Ada
11893 Error_Msg_N ("functions can only have IN parameters", Spec);
11894 Set_Ekind (Formal_Id, E_In_Parameter);
11897 elsif In_Present (Spec) then
11898 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11901 Set_Ekind (Formal_Id, E_Out_Parameter);
11902 Set_Never_Set_In_Source (Formal_Id, True);
11903 Set_Is_True_Constant (Formal_Id, False);
11904 Set_Current_Value (Formal_Id, Empty);
11908 Set_Ekind (Formal_Id, E_In_Parameter);
11911 -- Set Is_Known_Non_Null for access parameters since the language
11912 -- guarantees that access parameters are always non-null. We also set
11913 -- Can_Never_Be_Null, since there is no way to change the value.
11915 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11917 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11918 -- null; In Ada 2005, only if then null_exclusion is explicit.
11920 if Ada_Version < Ada_2005
11921 or else Can_Never_Be_Null (Etype (Formal_Id))
11923 Set_Is_Known_Non_Null (Formal_Id);
11924 Set_Can_Never_Be_Null (Formal_Id);
11927 -- Ada 2005 (AI-231): Null-exclusion access subtype
11929 elsif Is_Access_Type (Etype (Formal_Id))
11930 and then Can_Never_Be_Null (Etype (Formal_Id))
11932 Set_Is_Known_Non_Null (Formal_Id);
11934 -- We can also set Can_Never_Be_Null (thus preventing some junk
11935 -- access checks) for the case of an IN parameter, which cannot
11936 -- be changed, or for an IN OUT parameter, which can be changed but
11937 -- not to a null value. But for an OUT parameter, the initial value
11938 -- passed in can be null, so we can't set this flag in that case.
11940 if Ekind (Formal_Id) /= E_Out_Parameter then
11941 Set_Can_Never_Be_Null (Formal_Id);
11945 Set_Mechanism (Formal_Id, Default_Mechanism);
11946 Set_Formal_Validity (Formal_Id);
11947 end Set_Formal_Mode;
11949 -------------------------
11950 -- Set_Formal_Validity --
11951 -------------------------
11953 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11955 -- If no validity checking, then we cannot assume anything about the
11956 -- validity of parameters, since we do not know there is any checking
11957 -- of the validity on the call side.
11959 if not Validity_Checks_On then
11962 -- If validity checking for parameters is enabled, this means we are
11963 -- not supposed to make any assumptions about argument values.
11965 elsif Validity_Check_Parameters then
11968 -- If we are checking in parameters, we will assume that the caller is
11969 -- also checking parameters, so we can assume the parameter is valid.
11971 elsif Ekind (Formal_Id) = E_In_Parameter
11972 and then Validity_Check_In_Params
11974 Set_Is_Known_Valid (Formal_Id, True);
11976 -- Similar treatment for IN OUT parameters
11978 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11979 and then Validity_Check_In_Out_Params
11981 Set_Is_Known_Valid (Formal_Id, True);
11983 end Set_Formal_Validity;
11985 ------------------------
11986 -- Subtype_Conformant --
11987 ------------------------
11989 function Subtype_Conformant
11990 (New_Id : Entity_Id;
11991 Old_Id : Entity_Id;
11992 Skip_Controlling_Formals : Boolean := False) return Boolean
11996 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11997 Skip_Controlling_Formals => Skip_Controlling_Formals);
11999 end Subtype_Conformant;
12001 ---------------------
12002 -- Type_Conformant --
12003 ---------------------
12005 function Type_Conformant
12006 (New_Id : Entity_Id;
12007 Old_Id : Entity_Id;
12008 Skip_Controlling_Formals : Boolean := False) return Boolean
12012 May_Hide_Profile := False;
12014 (New_Id, Old_Id, Type_Conformant, False, Result,
12015 Skip_Controlling_Formals => Skip_Controlling_Formals);
12017 end Type_Conformant;
12019 -------------------------------
12020 -- Valid_Operator_Definition --
12021 -------------------------------
12023 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12026 Id : constant Name_Id := Chars (Designator);
12030 F := First_Formal (Designator);
12031 while Present (F) loop
12034 if Present (Default_Value (F)) then
12036 ("default values not allowed for operator parameters",
12039 -- For function instantiations that are operators, we must check
12040 -- separately that the corresponding generic only has in-parameters.
12041 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12042 -- an error could not arise in earlier versions of the language.
12044 elsif Ekind (F) /= E_In_Parameter then
12045 Error_Msg_N ("operators can only have IN parameters", F);
12051 -- Verify that user-defined operators have proper number of arguments
12052 -- First case of operators which can only be unary
12054 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12057 -- Case of operators which can be unary or binary
12059 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12060 N_OK := (N in 1 .. 2);
12062 -- All other operators can only be binary
12070 ("incorrect number of arguments for operator", Designator);
12074 and then Base_Type (Etype (Designator)) = Standard_Boolean
12075 and then not Is_Intrinsic_Subprogram (Designator)
12078 ("explicit definition of inequality not allowed", Designator);
12080 end Valid_Operator_Definition;