1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 the SPARK mode from the current context (may be overwritten later
230 -- with explicit pragma).
232 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
233 Set_SPARK_Pragma_Inherited (Subp_Id);
235 -- Preserve relevant elaboration-related attributes of the context which
236 -- are no longer available or very expensive to recompute once analysis,
237 -- resolution, and expansion are over.
239 Mark_Elaboration_Attributes
243 Set_Is_Abstract_Subprogram (Subp_Id);
244 New_Overloaded_Entity (Subp_Id);
245 Check_Delayed_Subprogram (Subp_Id);
247 Set_Categorization_From_Scope (Subp_Id, Scop);
249 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
250 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
252 -- Issue a warning if the abstract subprogram is neither a dispatching
253 -- operation nor an operation that overrides an inherited subprogram or
254 -- predefined operator, since this most likely indicates a mistake.
256 elsif Warn_On_Redundant_Constructs
257 and then not Is_Dispatching_Operation (Subp_Id)
258 and then not Present (Overridden_Operation (Subp_Id))
259 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
260 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
263 ("abstract subprogram is not dispatching or overriding?r?", N);
266 Generate_Reference_To_Formals (Subp_Id);
267 Check_Eliminated (Subp_Id);
269 if Has_Aspects (N) then
270 Analyze_Aspect_Specifications (N, Subp_Id);
272 end Analyze_Abstract_Subprogram_Declaration;
274 ---------------------------------
275 -- Analyze_Expression_Function --
276 ---------------------------------
278 procedure Analyze_Expression_Function (N : Node_Id) is
279 Expr : constant Node_Id := Expression (N);
280 Loc : constant Source_Ptr := Sloc (N);
281 LocX : constant Source_Ptr := Sloc (Expr);
282 Spec : constant Node_Id := Specification (N);
284 procedure Freeze_Expr_Types (Def_Id : Entity_Id);
285 -- N is an expression function that is a completion and Def_Id its
286 -- defining entity. Freeze before N all the types referenced by the
287 -- expression of the function.
289 -----------------------
290 -- Freeze_Expr_Types --
291 -----------------------
293 procedure Freeze_Expr_Types (Def_Id : Entity_Id) is
294 function Cloned_Expression return Node_Id;
295 -- Build a duplicate of the expression of the return statement that
296 -- has no defining entities shared with the original expression.
298 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
299 -- Freeze all types referenced in the subtree rooted at Node
301 -----------------------
302 -- Cloned_Expression --
303 -----------------------
305 function Cloned_Expression return Node_Id is
306 function Clone_Id (Node : Node_Id) return Traverse_Result;
307 -- Tree traversal routine that clones the defining identifier of
308 -- iterator and loop parameter specification nodes.
314 function Clone_Id (Node : Node_Id) return Traverse_Result is
316 if Nkind_In (Node, N_Iterator_Specification,
317 N_Loop_Parameter_Specification)
319 Set_Defining_Identifier (Node,
320 New_Copy (Defining_Identifier (Node)));
326 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
330 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
332 -- Start of processing for Cloned_Expression
335 -- We must duplicate the expression with semantic information to
336 -- inherit the decoration of global entities in generic instances.
337 -- Set the parent of the new node to be the parent of the original
338 -- to get the proper context, which is needed for complete error
339 -- reporting and for semantic analysis.
341 Set_Parent (Dup_Expr, Parent (Expr));
343 -- Replace the defining identifier of iterators and loop param
344 -- specifications by a clone to ensure that the cloned expression
345 -- and the original expression don't have shared identifiers;
346 -- otherwise, as part of the preanalysis of the expression, these
347 -- shared identifiers may be left decorated with itypes which
348 -- will not be available in the tree passed to the backend.
350 Clone_Def_Ids (Dup_Expr);
353 end Cloned_Expression;
355 ----------------------
356 -- Freeze_Type_Refs --
357 ----------------------
359 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
360 procedure Check_And_Freeze_Type (Typ : Entity_Id);
361 -- Check that Typ is fully declared and freeze it if so
363 ---------------------------
364 -- Check_And_Freeze_Type --
365 ---------------------------
367 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
369 -- Skip Itypes created by the preanalysis, and itypes whose
370 -- scope is another type (i.e. component subtypes that depend
371 -- on a discriminant),
374 and then (Scope_Within_Or_Same (Scope (Typ), Def_Id)
375 or else Is_Type (Scope (Typ)))
380 -- This provides a better error message than generating
381 -- primitives whose compilation fails much later. Refine
382 -- the error message if possible.
384 Check_Fully_Declared (Typ, Node);
386 if Error_Posted (Node) then
387 if Has_Private_Component (Typ)
388 and then not Is_Private_Type (Typ)
390 Error_Msg_NE ("\type& has private component", Node, Typ);
394 Freeze_Before (N, Typ);
396 end Check_And_Freeze_Type;
398 -- Start of processing for Freeze_Type_Refs
401 -- Check that a type referenced by an entity can be frozen
403 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
404 Check_And_Freeze_Type (Etype (Entity (Node)));
406 -- Check that the enclosing record type can be frozen
408 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
409 Check_And_Freeze_Type (Scope (Entity (Node)));
412 -- Freezing an access type does not freeze the designated type,
413 -- but freezing conversions between access to interfaces requires
414 -- that the interface types themselves be frozen, so that dispatch
415 -- table entities are properly created.
417 -- Unclear whether a more general rule is needed ???
419 elsif Nkind (Node) = N_Type_Conversion
420 and then Is_Access_Type (Etype (Node))
421 and then Is_Interface (Designated_Type (Etype (Node)))
423 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
426 -- An implicit dereference freezes the designated type. In the
427 -- case of a dispatching call whose controlling argument is an
428 -- access type, the dereference is not made explicit, so we must
429 -- check for such a call and freeze the designated type.
431 if Nkind (Node) in N_Has_Etype
432 and then Present (Etype (Node))
433 and then Is_Access_Type (Etype (Node))
434 and then Nkind (Parent (Node)) = N_Function_Call
435 and then Node = Controlling_Argument (Parent (Node))
437 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
440 -- No point in posting several errors on the same expression
442 if Serious_Errors_Detected > 0 then
447 end Freeze_Type_Refs;
449 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
453 Saved_First_Entity : constant Entity_Id := First_Entity (Def_Id);
454 Saved_Last_Entity : constant Entity_Id := Last_Entity (Def_Id);
455 Dup_Expr : constant Node_Id := Cloned_Expression;
457 -- Start of processing for Freeze_Expr_Types
460 -- Preanalyze a duplicate of the expression to have available the
461 -- minimum decoration needed to locate referenced unfrozen types
462 -- without adding any decoration to the function expression.
465 Install_Formals (Def_Id);
467 Preanalyze_Spec_Expression (Dup_Expr, Etype (Def_Id));
470 -- Restore certain attributes of Def_Id since the preanalysis may
471 -- have introduced itypes to this scope, thus modifying attributes
472 -- First_Entity and Last_Entity.
474 Set_First_Entity (Def_Id, Saved_First_Entity);
475 Set_Last_Entity (Def_Id, Saved_Last_Entity);
477 if Present (Last_Entity (Def_Id)) then
478 Set_Next_Entity (Last_Entity (Def_Id), Empty);
481 -- Freeze all types referenced in the expression
483 Freeze_References (Dup_Expr);
484 end Freeze_Expr_Types;
494 Def_Id : Entity_Id := Empty;
496 -- If the expression is a completion, Prev is the entity whose
497 -- declaration is completed. Def_Id is needed to analyze the spec.
499 -- Start of processing for Analyze_Expression_Function
502 -- This is one of the occasions on which we transform the tree during
503 -- semantic analysis. If this is a completion, transform the expression
504 -- function into an equivalent subprogram body, and analyze it.
506 -- Expression functions are inlined unconditionally. The back-end will
507 -- determine whether this is possible.
509 Inline_Processing_Required := True;
511 -- Create a specification for the generated body. This must be done
512 -- prior to the analysis of the initial declaration.
514 New_Spec := Copy_Subprogram_Spec (Spec);
515 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
517 -- If there are previous overloadable entities with the same name,
518 -- check whether any of them is completed by the expression function.
519 -- In a generic context a formal subprogram has no completion.
522 and then Is_Overloadable (Prev)
523 and then not Is_Formal_Subprogram (Prev)
525 Def_Id := Analyze_Subprogram_Specification (Spec);
526 Prev := Find_Corresponding_Spec (N);
528 -- The previous entity may be an expression function as well, in
529 -- which case the redeclaration is illegal.
532 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
533 N_Expression_Function
535 Error_Msg_Sloc := Sloc (Prev);
536 Error_Msg_N ("& conflicts with declaration#", Def_Id);
541 Ret := Make_Simple_Return_Statement (LocX, Expr);
544 Make_Subprogram_Body (Loc,
545 Specification => New_Spec,
546 Declarations => Empty_List,
547 Handled_Statement_Sequence =>
548 Make_Handled_Sequence_Of_Statements (LocX,
549 Statements => New_List (Ret)));
550 Set_Was_Expression_Function (New_Body);
552 -- If the expression completes a generic subprogram, we must create a
553 -- separate node for the body, because at instantiation the original
554 -- node of the generic copy must be a generic subprogram body, and
555 -- cannot be a expression function. Otherwise we just rewrite the
556 -- expression with the non-generic body.
558 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
559 Insert_After (N, New_Body);
561 -- Propagate any aspects or pragmas that apply to the expression
562 -- function to the proper body when the expression function acts
565 if Has_Aspects (N) then
566 Move_Aspects (N, To => New_Body);
569 Relocate_Pragmas_To_Body (New_Body);
571 Rewrite (N, Make_Null_Statement (Loc));
572 Set_Has_Completion (Prev, False);
575 Set_Is_Inlined (Prev);
577 -- If the expression function is a completion, the previous declaration
578 -- must come from source. We know already that it appears in the current
579 -- scope. The entity itself may be internally created if within a body
583 and then Is_Overloadable (Prev)
584 and then not Is_Formal_Subprogram (Prev)
585 and then Comes_From_Source (Parent (Prev))
587 Set_Has_Completion (Prev, False);
588 Set_Is_Inlined (Prev);
590 -- AI12-0103: Expression functions that are a completion freeze their
591 -- expression but don't freeze anything else (unlike regular bodies).
593 -- Note that we cannot defer this freezing to the analysis of the
594 -- expression itself, because a freeze node might appear in a nested
595 -- scope, leading to an elaboration order issue in gigi.
596 -- As elsewhere, we do not emit freeze nodes within a generic unit.
598 if not Inside_A_Generic then
599 Freeze_Expr_Types (Def_Id);
602 -- For navigation purposes, indicate that the function is a body
604 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
605 Rewrite (N, New_Body);
607 -- Remove any existing aspects from the original node because the act
608 -- of rewriting causes the list to be shared between the two nodes.
610 Orig_N := Original_Node (N);
611 Remove_Aspects (Orig_N);
613 -- Propagate any pragmas that apply to expression function to the
614 -- proper body when the expression function acts as a completion.
615 -- Aspects are automatically transfered because of node rewriting.
617 Relocate_Pragmas_To_Body (N);
620 -- Once the aspects of the generated body have been analyzed, create
621 -- a copy for ASIS purposes and associate it with the original node.
623 if Has_Aspects (N) then
624 Set_Aspect_Specifications (Orig_N,
625 New_Copy_List_Tree (Aspect_Specifications (N)));
628 -- Prev is the previous entity with the same name, but it is can
629 -- be an unrelated spec that is not completed by the expression
630 -- function. In that case the relevant entity is the one in the body.
631 -- Not clear that the backend can inline it in this case ???
633 if Has_Completion (Prev) then
635 -- The formals of the expression function are body formals,
636 -- and do not appear in the ali file, which will only contain
637 -- references to the formals of the original subprogram spec.
644 F1 := First_Formal (Def_Id);
645 F2 := First_Formal (Prev);
647 while Present (F1) loop
648 Set_Spec_Entity (F1, F2);
655 Set_Is_Inlined (Defining_Entity (New_Body));
658 -- If this is not a completion, create both a declaration and a body, so
659 -- that the expression can be inlined whenever possible.
662 -- An expression function that is not a completion is not a
663 -- subprogram declaration, and thus cannot appear in a protected
666 if Nkind (Parent (N)) = N_Protected_Definition then
668 ("an expression function is not a legal protected operation", N);
671 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
673 -- Remove any existing aspects from the original node because the act
674 -- of rewriting causes the list to be shared between the two nodes.
676 Orig_N := Original_Node (N);
677 Remove_Aspects (Orig_N);
681 -- Once the aspects of the generated spec have been analyzed, create
682 -- a copy for ASIS purposes and associate it with the original node.
684 if Has_Aspects (N) then
685 Set_Aspect_Specifications (Orig_N,
686 New_Copy_List_Tree (Aspect_Specifications (N)));
689 -- If aspect SPARK_Mode was specified on the body, it needs to be
690 -- repeated both on the generated spec and the body.
692 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
694 if Present (Asp) then
695 Asp := New_Copy_Tree (Asp);
696 Set_Analyzed (Asp, False);
697 Set_Aspect_Specifications (New_Body, New_List (Asp));
700 Def_Id := Defining_Entity (N);
701 Set_Is_Inlined (Def_Id);
703 -- Establish the linkages between the spec and the body. These are
704 -- used when the expression function acts as the prefix of attribute
705 -- 'Access in order to freeze the original expression which has been
706 -- moved to the generated body.
708 Set_Corresponding_Body (N, Defining_Entity (New_Body));
709 Set_Corresponding_Spec (New_Body, Def_Id);
711 -- Within a generic pre-analyze the original expression for name
712 -- capture. The body is also generated but plays no role in
713 -- this because it is not part of the original source.
715 if Inside_A_Generic then
716 Set_Has_Completion (Def_Id);
718 Install_Formals (Def_Id);
719 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
723 -- To prevent premature freeze action, insert the new body at the end
724 -- of the current declarations, or at the end of the package spec.
725 -- However, resolve usage names now, to prevent spurious visibility
726 -- on later entities. Note that the function can now be called in
727 -- the current declarative part, which will appear to be prior to
728 -- the presence of the body in the code. There are nevertheless no
729 -- order of elaboration issues because all name resolution has taken
730 -- place at the point of declaration.
733 Decls : List_Id := List_Containing (N);
734 Expr : constant Node_Id := Expression (Ret);
735 Par : constant Node_Id := Parent (Decls);
736 Typ : constant Entity_Id := Etype (Def_Id);
739 -- If this is a wrapper created for in an instance for a formal
740 -- subprogram, insert body after declaration, to be analyzed when
741 -- the enclosing instance is analyzed.
744 and then Is_Generic_Actual_Subprogram (Def_Id)
746 Insert_After (N, New_Body);
749 if Nkind (Par) = N_Package_Specification
750 and then Decls = Visible_Declarations (Par)
751 and then Present (Private_Declarations (Par))
752 and then not Is_Empty_List (Private_Declarations (Par))
754 Decls := Private_Declarations (Par);
757 Insert_After (Last (Decls), New_Body);
759 -- Preanalyze the expression if not already done above
761 if not Inside_A_Generic then
763 Install_Formals (Def_Id);
764 Preanalyze_Spec_Expression (Expr, Typ);
765 Check_Limited_Return (Original_Node (N), Expr, Typ);
772 -- Check incorrect use of dynamically tagged expression. This doesn't
773 -- fall out automatically when analyzing the generated function body,
774 -- because Check_Dynamically_Tagged_Expression deliberately ignores
775 -- nodes that don't come from source.
778 and then Nkind (Def_Id) in N_Has_Etype
779 and then Is_Tagged_Type (Etype (Def_Id))
781 Check_Dynamically_Tagged_Expression
783 Typ => Etype (Def_Id),
784 Related_Nod => Original_Node (N));
787 -- If the return expression is a static constant, we suppress warning
788 -- messages on unused formals, which in most cases will be noise.
790 Set_Is_Trivial_Subprogram
791 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
792 end Analyze_Expression_Function;
794 ----------------------------------------
795 -- Analyze_Extended_Return_Statement --
796 ----------------------------------------
798 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
800 Check_Compiler_Unit ("extended return statement", N);
801 Analyze_Return_Statement (N);
802 end Analyze_Extended_Return_Statement;
804 ----------------------------
805 -- Analyze_Function_Call --
806 ----------------------------
808 procedure Analyze_Function_Call (N : Node_Id) is
809 Actuals : constant List_Id := Parameter_Associations (N);
810 Func_Nam : constant Node_Id := Name (N);
816 -- A call of the form A.B (X) may be an Ada 2005 call, which is
817 -- rewritten as B (A, X). If the rewriting is successful, the call
818 -- has been analyzed and we just return.
820 if Nkind (Func_Nam) = N_Selected_Component
821 and then Name (N) /= Func_Nam
822 and then Is_Rewrite_Substitution (N)
823 and then Present (Etype (N))
828 -- If error analyzing name, then set Any_Type as result type and return
830 if Etype (Func_Nam) = Any_Type then
831 Set_Etype (N, Any_Type);
835 -- Otherwise analyze the parameters
837 if Present (Actuals) then
838 Actual := First (Actuals);
839 while Present (Actual) loop
841 Check_Parameterless_Call (Actual);
847 end Analyze_Function_Call;
849 -----------------------------
850 -- Analyze_Function_Return --
851 -----------------------------
853 procedure Analyze_Function_Return (N : Node_Id) is
854 Loc : constant Source_Ptr := Sloc (N);
855 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
856 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
858 R_Type : constant Entity_Id := Etype (Scope_Id);
859 -- Function result subtype
861 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
862 -- Apply legality rule of 6.5 (5.8) to the access discriminants of an
863 -- aggregate in a return statement.
865 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
866 -- Check that the return_subtype_indication properly matches the result
867 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
869 -----------------------------------
870 -- Check_Aggregate_Accessibility --
871 -----------------------------------
873 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
874 Typ : constant Entity_Id := Etype (Aggr);
881 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
882 Discr := First_Discriminant (Typ);
883 Assoc := First (Component_Associations (Aggr));
884 while Present (Discr) loop
885 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
886 Expr := Expression (Assoc);
888 if Nkind (Expr) = N_Attribute_Reference
889 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
891 Obj := Prefix (Expr);
892 while Nkind_In (Obj, N_Indexed_Component,
893 N_Selected_Component)
898 -- Do not check aliased formals or function calls. A
899 -- run-time check may still be needed ???
901 if Is_Entity_Name (Obj)
902 and then Comes_From_Source (Obj)
904 if Is_Formal (Entity (Obj))
905 and then Is_Aliased (Entity (Obj))
909 elsif Object_Access_Level (Obj) >
910 Scope_Depth (Scope (Scope_Id))
913 ("access discriminant in return aggregate would "
914 & "be a dangling reference", Obj);
920 Next_Discriminant (Discr);
923 end Check_Aggregate_Accessibility;
925 -------------------------------------
926 -- Check_Return_Subtype_Indication --
927 -------------------------------------
929 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
930 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
932 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
933 -- Subtype given in the extended return statement (must match R_Type)
935 Subtype_Ind : constant Node_Id :=
936 Object_Definition (Original_Node (Obj_Decl));
938 procedure Error_No_Match (N : Node_Id);
939 -- Output error messages for case where types do not statically
940 -- match. N is the location for the messages.
946 procedure Error_No_Match (N : Node_Id) is
949 ("subtype must statically match function result subtype", N);
951 if not Predicates_Match (R_Stm_Type, R_Type) then
952 Error_Msg_Node_2 := R_Type;
954 ("\predicate of& does not match predicate of&",
959 -- Start of processing for Check_Return_Subtype_Indication
962 -- First, avoid cascaded errors
964 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
968 -- "return access T" case; check that the return statement also has
969 -- "access T", and that the subtypes statically match:
970 -- if this is an access to subprogram the signatures must match.
972 if Is_Anonymous_Access_Type (R_Type) then
973 if Is_Anonymous_Access_Type (R_Stm_Type) then
974 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
976 if Base_Type (Designated_Type (R_Stm_Type)) /=
977 Base_Type (Designated_Type (R_Type))
978 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
980 Error_No_Match (Subtype_Mark (Subtype_Ind));
984 -- For two anonymous access to subprogram types, the types
985 -- themselves must be type conformant.
987 if not Conforming_Types
988 (R_Stm_Type, R_Type, Fully_Conformant)
990 Error_No_Match (Subtype_Ind);
995 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
998 -- If the return object is of an anonymous access type, then report
999 -- an error if the function's result type is not also anonymous.
1001 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1002 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1004 ("anonymous access not allowed for function with named access "
1005 & "result", Subtype_Ind);
1007 -- Subtype indication case: check that the return object's type is
1008 -- covered by the result type, and that the subtypes statically match
1009 -- when the result subtype is constrained. Also handle record types
1010 -- with unknown discriminants for which we have built the underlying
1011 -- record view. Coverage is needed to allow specific-type return
1012 -- objects when the result type is class-wide (see AI05-32).
1014 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1015 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1018 (Base_Type (R_Type),
1019 Underlying_Record_View (Base_Type (R_Stm_Type))))
1021 -- A null exclusion may be present on the return type, on the
1022 -- function specification, on the object declaration or on the
1025 if Is_Access_Type (R_Type)
1027 (Can_Never_Be_Null (R_Type)
1028 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1029 Can_Never_Be_Null (R_Stm_Type)
1031 Error_No_Match (Subtype_Ind);
1034 -- AI05-103: for elementary types, subtypes must statically match
1036 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1037 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1038 Error_No_Match (Subtype_Ind);
1042 -- All remaining cases are illegal
1044 -- Note: previous versions of this subprogram allowed the return
1045 -- value to be the ancestor of the return type if the return type
1046 -- was a null extension. This was plainly incorrect.
1050 ("wrong type for return_subtype_indication", Subtype_Ind);
1052 end Check_Return_Subtype_Indication;
1054 ---------------------
1055 -- Local Variables --
1056 ---------------------
1059 Obj_Decl : Node_Id := Empty;
1061 -- Start of processing for Analyze_Function_Return
1064 Set_Return_Present (Scope_Id);
1066 if Nkind (N) = N_Simple_Return_Statement then
1067 Expr := Expression (N);
1069 -- Guard against a malformed expression. The parser may have tried to
1070 -- recover but the node is not analyzable.
1072 if Nkind (Expr) = N_Error then
1073 Set_Etype (Expr, Any_Type);
1074 Expander_Mode_Save_And_Set (False);
1078 -- The resolution of a controlled [extension] aggregate associated
1079 -- with a return statement creates a temporary which needs to be
1080 -- finalized on function exit. Wrap the return statement inside a
1081 -- block so that the finalization machinery can detect this case.
1082 -- This early expansion is done only when the return statement is
1083 -- not part of a handled sequence of statements.
1085 if Nkind_In (Expr, N_Aggregate,
1086 N_Extension_Aggregate)
1087 and then Needs_Finalization (R_Type)
1088 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1091 Make_Block_Statement (Loc,
1092 Handled_Statement_Sequence =>
1093 Make_Handled_Sequence_Of_Statements (Loc,
1094 Statements => New_List (Relocate_Node (N)))));
1102 -- Ada 2005 (AI-251): If the type of the returned object is
1103 -- an access to an interface type then we add an implicit type
1104 -- conversion to force the displacement of the "this" pointer to
1105 -- reference the secondary dispatch table. We cannot delay the
1106 -- generation of this implicit conversion until the expansion
1107 -- because in this case the type resolution changes the decoration
1108 -- of the expression node to match R_Type; by contrast, if the
1109 -- returned object is a class-wide interface type then it is too
1110 -- early to generate here the implicit conversion since the return
1111 -- statement may be rewritten by the expander into an extended
1112 -- return statement whose expansion takes care of adding the
1113 -- implicit type conversion to displace the pointer to the object.
1116 and then Serious_Errors_Detected = 0
1117 and then Is_Access_Type (R_Type)
1118 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1119 and then Is_Interface (Designated_Type (R_Type))
1120 and then Is_Progenitor (Designated_Type (R_Type),
1121 Designated_Type (Etype (Expr)))
1123 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1127 Resolve (Expr, R_Type);
1128 Check_Limited_Return (N, Expr, R_Type);
1130 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
1131 Check_Aggregate_Accessibility (Expr);
1135 -- RETURN only allowed in SPARK as the last statement in function
1137 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1139 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
1140 or else Present (Next (N)))
1142 Check_SPARK_05_Restriction
1143 ("RETURN should be the last statement in function", N);
1147 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
1148 Obj_Decl := Last (Return_Object_Declarations (N));
1150 -- Analyze parts specific to extended_return_statement:
1153 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1154 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1157 Expr := Expression (Obj_Decl);
1159 -- Note: The check for OK_For_Limited_Init will happen in
1160 -- Analyze_Object_Declaration; we treat it as a normal
1161 -- object declaration.
1163 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1166 Check_Return_Subtype_Indication (Obj_Decl);
1168 if Present (HSS) then
1171 if Present (Exception_Handlers (HSS)) then
1173 -- ???Has_Nested_Block_With_Handler needs to be set.
1174 -- Probably by creating an actual N_Block_Statement.
1175 -- Probably in Expand.
1181 -- Mark the return object as referenced, since the return is an
1182 -- implicit reference of the object.
1184 Set_Referenced (Defining_Identifier (Obj_Decl));
1186 Check_References (Stm_Entity);
1188 -- Check RM 6.5 (5.9/3)
1191 if Ada_Version < Ada_2012 then
1193 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1194 -- Can it really happen (extended return???)
1197 ("aliased only allowed for limited return objects "
1198 & "in Ada 2012??", N);
1200 elsif not Is_Limited_View (R_Type) then
1202 ("aliased only allowed for limited return objects", N);
1208 -- Case of Expr present
1210 if Present (Expr) then
1212 -- Defend against previous errors
1214 if Nkind (Expr) = N_Empty
1215 or else No (Etype (Expr))
1220 -- Apply constraint check. Note that this is done before the implicit
1221 -- conversion of the expression done for anonymous access types to
1222 -- ensure correct generation of the null-excluding check associated
1223 -- with null-excluding expressions found in return statements.
1225 Apply_Constraint_Check (Expr, R_Type);
1227 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1228 -- type, apply an implicit conversion of the expression to that type
1229 -- to force appropriate static and run-time accessibility checks.
1231 if Ada_Version >= Ada_2005
1232 and then Ekind (R_Type) = E_Anonymous_Access_Type
1234 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1235 Analyze_And_Resolve (Expr, R_Type);
1237 -- If this is a local anonymous access to subprogram, the
1238 -- accessibility check can be applied statically. The return is
1239 -- illegal if the access type of the return expression is declared
1240 -- inside of the subprogram (except if it is the subtype indication
1241 -- of an extended return statement).
1243 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1244 if not Comes_From_Source (Current_Scope)
1245 or else Ekind (Current_Scope) = E_Return_Statement
1250 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1252 Error_Msg_N ("cannot return local access to subprogram", N);
1255 -- The expression cannot be of a formal incomplete type
1257 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1258 and then Is_Generic_Type (Etype (Expr))
1261 ("cannot return expression of a formal incomplete type", N);
1264 -- If the result type is class-wide, then check that the return
1265 -- expression's type is not declared at a deeper level than the
1266 -- function (RM05-6.5(5.6/2)).
1268 if Ada_Version >= Ada_2005
1269 and then Is_Class_Wide_Type (R_Type)
1271 if Type_Access_Level (Etype (Expr)) >
1272 Subprogram_Access_Level (Scope_Id)
1275 ("level of return expression type is deeper than "
1276 & "class-wide function!", Expr);
1280 -- Check incorrect use of dynamically tagged expression
1282 if Is_Tagged_Type (R_Type) then
1283 Check_Dynamically_Tagged_Expression
1289 -- ??? A real run-time accessibility check is needed in cases
1290 -- involving dereferences of access parameters. For now we just
1291 -- check the static cases.
1293 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1294 and then Is_Limited_View (Etype (Scope_Id))
1295 and then Object_Access_Level (Expr) >
1296 Subprogram_Access_Level (Scope_Id)
1298 -- Suppress the message in a generic, where the rewriting
1301 if Inside_A_Generic then
1306 Make_Raise_Program_Error (Loc,
1307 Reason => PE_Accessibility_Check_Failed));
1310 Error_Msg_Warn := SPARK_Mode /= On;
1311 Error_Msg_N ("cannot return a local value by reference<<", N);
1312 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1316 if Known_Null (Expr)
1317 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1318 and then Null_Exclusion_Present (Parent (Scope_Id))
1320 Apply_Compile_Time_Constraint_Error
1322 Msg => "(Ada 2005) null not allowed for "
1323 & "null-excluding return??",
1324 Reason => CE_Null_Not_Allowed);
1327 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1328 -- has no initializing expression.
1330 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1331 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1332 Subprogram_Access_Level (Scope_Id)
1335 ("level of return expression type is deeper than "
1336 & "class-wide function!", Obj_Decl);
1339 end Analyze_Function_Return;
1341 -------------------------------------
1342 -- Analyze_Generic_Subprogram_Body --
1343 -------------------------------------
1345 procedure Analyze_Generic_Subprogram_Body
1349 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1350 Kind : constant Entity_Kind := Ekind (Gen_Id);
1351 Body_Id : Entity_Id;
1356 -- Copy body and disable expansion while analyzing the generic For a
1357 -- stub, do not copy the stub (which would load the proper body), this
1358 -- will be done when the proper body is analyzed.
1360 if Nkind (N) /= N_Subprogram_Body_Stub then
1361 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1364 -- Once the contents of the generic copy and the template are
1365 -- swapped, do the same for their respective aspect specifications.
1367 Exchange_Aspects (N, New_N);
1369 -- Collect all contract-related source pragmas found within the
1370 -- template and attach them to the contract of the subprogram body.
1371 -- This contract is used in the capture of global references within
1374 Create_Generic_Contract (N);
1379 Spec := Specification (N);
1381 -- Within the body of the generic, the subprogram is callable, and
1382 -- behaves like the corresponding non-generic unit.
1384 Body_Id := Defining_Entity (Spec);
1386 if Kind = E_Generic_Procedure
1387 and then Nkind (Spec) /= N_Procedure_Specification
1389 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1392 elsif Kind = E_Generic_Function
1393 and then Nkind (Spec) /= N_Function_Specification
1395 Error_Msg_N ("invalid body for generic function ", Body_Id);
1399 Set_Corresponding_Body (Gen_Decl, Body_Id);
1401 if Has_Completion (Gen_Id)
1402 and then Nkind (Parent (N)) /= N_Subunit
1404 Error_Msg_N ("duplicate generic body", N);
1407 Set_Has_Completion (Gen_Id);
1410 if Nkind (N) = N_Subprogram_Body_Stub then
1411 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1413 Set_Corresponding_Spec (N, Gen_Id);
1416 if Nkind (Parent (N)) = N_Compilation_Unit then
1417 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1420 -- Make generic parameters immediately visible in the body. They are
1421 -- needed to process the formals declarations. Then make the formals
1422 -- visible in a separate step.
1424 Push_Scope (Gen_Id);
1428 First_Ent : Entity_Id;
1431 First_Ent := First_Entity (Gen_Id);
1434 while Present (E) and then not Is_Formal (E) loop
1439 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1441 -- Now generic formals are visible, and the specification can be
1442 -- analyzed, for subsequent conformance check.
1444 Body_Id := Analyze_Subprogram_Specification (Spec);
1446 -- Make formal parameters visible
1450 -- E is the first formal parameter, we loop through the formals
1451 -- installing them so that they will be visible.
1453 Set_First_Entity (Gen_Id, E);
1454 while Present (E) loop
1460 -- Visible generic entity is callable within its own body
1462 Set_Ekind (Gen_Id, Ekind (Body_Id));
1463 Set_Ekind (Body_Id, E_Subprogram_Body);
1464 Set_Convention (Body_Id, Convention (Gen_Id));
1465 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1466 Set_Scope (Body_Id, Scope (Gen_Id));
1468 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1470 if Nkind (N) = N_Subprogram_Body_Stub then
1472 -- No body to analyze, so restore state of generic unit
1474 Set_Ekind (Gen_Id, Kind);
1475 Set_Ekind (Body_Id, Kind);
1477 if Present (First_Ent) then
1478 Set_First_Entity (Gen_Id, First_Ent);
1485 -- If this is a compilation unit, it must be made visible explicitly,
1486 -- because the compilation of the declaration, unlike other library
1487 -- unit declarations, does not. If it is not a unit, the following
1488 -- is redundant but harmless.
1490 Set_Is_Immediately_Visible (Gen_Id);
1491 Reference_Body_Formals (Gen_Id, Body_Id);
1493 if Is_Child_Unit (Gen_Id) then
1494 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1497 Set_Actual_Subtypes (N, Current_Scope);
1499 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1500 Set_SPARK_Pragma_Inherited (Body_Id);
1502 -- Analyze any aspect specifications that appear on the generic
1505 if Has_Aspects (N) then
1506 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1509 Analyze_Declarations (Declarations (N));
1512 -- Process the contract of the subprogram body after all declarations
1513 -- have been analyzed. This ensures that any contract-related pragmas
1514 -- are available through the N_Contract node of the body.
1516 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1518 Analyze (Handled_Statement_Sequence (N));
1519 Save_Global_References (Original_Node (N));
1521 -- Prior to exiting the scope, include generic formals again (if any
1522 -- are present) in the set of local entities.
1524 if Present (First_Ent) then
1525 Set_First_Entity (Gen_Id, First_Ent);
1528 Check_References (Gen_Id);
1531 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1532 Update_Use_Clause_Chain;
1533 Validate_Categorization_Dependency (N, Gen_Id);
1535 Check_Subprogram_Order (N);
1537 -- Outside of its body, unit is generic again
1539 Set_Ekind (Gen_Id, Kind);
1540 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1543 Style.Check_Identifier (Body_Id, Gen_Id);
1547 end Analyze_Generic_Subprogram_Body;
1549 ----------------------------
1550 -- Analyze_Null_Procedure --
1551 ----------------------------
1553 procedure Analyze_Null_Procedure
1555 Is_Completion : out Boolean)
1557 Loc : constant Source_Ptr := Sloc (N);
1558 Spec : constant Node_Id := Specification (N);
1559 Designator : Entity_Id;
1561 Null_Body : Node_Id := Empty;
1562 Null_Stmt : Node_Id := Null_Statement (Spec);
1566 -- Capture the profile of the null procedure before analysis, for
1567 -- expansion at the freeze point and at each point of call. The body is
1568 -- used if the procedure has preconditions, or if it is a completion. In
1569 -- the first case the body is analyzed at the freeze point, in the other
1570 -- it replaces the null procedure declaration.
1572 -- For a null procedure that comes from source, a NULL statement is
1573 -- provided by the parser, which carries the source location of the
1574 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1575 -- from expansion, create one now.
1577 if No (Null_Stmt) then
1578 Null_Stmt := Make_Null_Statement (Loc);
1582 Make_Subprogram_Body (Loc,
1583 Specification => New_Copy_Tree (Spec),
1584 Declarations => New_List,
1585 Handled_Statement_Sequence =>
1586 Make_Handled_Sequence_Of_Statements (Loc,
1587 Statements => New_List (Null_Stmt)));
1589 -- Create new entities for body and formals
1591 Set_Defining_Unit_Name (Specification (Null_Body),
1592 Make_Defining_Identifier
1593 (Sloc (Defining_Entity (N)),
1594 Chars (Defining_Entity (N))));
1596 Form := First (Parameter_Specifications (Specification (Null_Body)));
1597 while Present (Form) loop
1598 Set_Defining_Identifier (Form,
1599 Make_Defining_Identifier
1600 (Sloc (Defining_Identifier (Form)),
1601 Chars (Defining_Identifier (Form))));
1605 -- Determine whether the null procedure may be a completion of a generic
1606 -- suprogram, in which case we use the new null body as the completion
1607 -- and set minimal semantic information on the original declaration,
1608 -- which is rewritten as a null statement.
1610 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1612 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1613 Insert_Before (N, Null_Body);
1614 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1616 Rewrite (N, Make_Null_Statement (Loc));
1617 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1618 Is_Completion := True;
1622 -- Resolve the types of the formals now, because the freeze point may
1623 -- appear in a different context, e.g. an instantiation.
1625 Form := First (Parameter_Specifications (Specification (Null_Body)));
1626 while Present (Form) loop
1627 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1628 Find_Type (Parameter_Type (Form));
1630 elsif No (Access_To_Subprogram_Definition
1631 (Parameter_Type (Form)))
1633 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1635 -- The case of a null procedure with a formal that is an
1636 -- access-to-subprogram type, and that is used as an actual
1637 -- in an instantiation is left to the enthusiastic reader.
1647 -- If there are previous overloadable entities with the same name, check
1648 -- whether any of them is completed by the null procedure.
1650 if Present (Prev) and then Is_Overloadable (Prev) then
1651 Designator := Analyze_Subprogram_Specification (Spec);
1652 Prev := Find_Corresponding_Spec (N);
1655 if No (Prev) or else not Comes_From_Source (Prev) then
1656 Designator := Analyze_Subprogram_Specification (Spec);
1657 Set_Has_Completion (Designator);
1659 -- Signal to caller that this is a procedure declaration
1661 Is_Completion := False;
1663 -- Null procedures are always inlined, but generic formal subprograms
1664 -- which appear as such in the internal instance of formal packages,
1665 -- need no completion and are not marked Inline.
1668 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1670 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1671 Set_Body_To_Inline (N, Null_Body);
1672 Set_Is_Inlined (Designator);
1676 -- The null procedure is a completion. We unconditionally rewrite
1677 -- this as a null body (even if expansion is not active), because
1678 -- there are various error checks that are applied on this body
1679 -- when it is analyzed (e.g. correct aspect placement).
1681 if Has_Completion (Prev) then
1682 Error_Msg_Sloc := Sloc (Prev);
1683 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1686 Check_Previous_Null_Procedure (N, Prev);
1688 Is_Completion := True;
1689 Rewrite (N, Null_Body);
1692 end Analyze_Null_Procedure;
1694 -----------------------------
1695 -- Analyze_Operator_Symbol --
1696 -----------------------------
1698 -- An operator symbol such as "+" or "and" may appear in context where the
1699 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1700 -- is just a string, as in (conjunction = "or"). In these cases the parser
1701 -- generates this node, and the semantics does the disambiguation. Other
1702 -- such case are actuals in an instantiation, the generic unit in an
1703 -- instantiation, and pragma arguments.
1705 procedure Analyze_Operator_Symbol (N : Node_Id) is
1706 Par : constant Node_Id := Parent (N);
1709 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1710 or else Nkind (Par) = N_Function_Instantiation
1711 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1712 or else (Nkind (Par) = N_Pragma_Argument_Association
1713 and then not Is_Pragma_String_Literal (Par))
1714 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1715 or else (Nkind (Par) = N_Attribute_Reference
1716 and then Attribute_Name (Par) /= Name_Value)
1718 Find_Direct_Name (N);
1721 Change_Operator_Symbol_To_String_Literal (N);
1724 end Analyze_Operator_Symbol;
1726 -----------------------------------
1727 -- Analyze_Parameter_Association --
1728 -----------------------------------
1730 procedure Analyze_Parameter_Association (N : Node_Id) is
1732 Analyze (Explicit_Actual_Parameter (N));
1733 end Analyze_Parameter_Association;
1735 ----------------------------
1736 -- Analyze_Procedure_Call --
1737 ----------------------------
1739 -- WARNING: This routine manages Ghost regions. Return statements must be
1740 -- replaced by gotos which jump to the end of the routine and restore the
1743 procedure Analyze_Procedure_Call (N : Node_Id) is
1744 procedure Analyze_Call_And_Resolve;
1745 -- Do Analyze and Resolve calls for procedure call. At the end, check
1746 -- for illegal order dependence.
1747 -- ??? where is the check for illegal order dependencies?
1749 ------------------------------
1750 -- Analyze_Call_And_Resolve --
1751 ------------------------------
1753 procedure Analyze_Call_And_Resolve is
1755 if Nkind (N) = N_Procedure_Call_Statement then
1757 Resolve (N, Standard_Void_Type);
1761 end Analyze_Call_And_Resolve;
1765 Actuals : constant List_Id := Parameter_Associations (N);
1766 Loc : constant Source_Ptr := Sloc (N);
1767 P : constant Node_Id := Name (N);
1769 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1770 -- Save the Ghost mode to restore on exit
1775 -- Start of processing for Analyze_Procedure_Call
1778 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1779 -- a procedure call or an entry call. The prefix may denote an access
1780 -- to subprogram type, in which case an implicit dereference applies.
1781 -- If the prefix is an indexed component (without implicit dereference)
1782 -- then the construct denotes a call to a member of an entire family.
1783 -- If the prefix is a simple name, it may still denote a call to a
1784 -- parameterless member of an entry family. Resolution of these various
1785 -- interpretations is delicate.
1787 -- Do not analyze machine code statements to avoid rejecting them in
1790 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1791 Set_Etype (P, Standard_Void_Type);
1796 -- If this is a call of the form Obj.Op, the call may have been analyzed
1797 -- and possibly rewritten into a block, in which case we are done.
1799 if Analyzed (N) then
1802 -- If there is an error analyzing the name (which may have been
1803 -- rewritten if the original call was in prefix notation) then error
1804 -- has been emitted already, mark node and return.
1806 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1807 Set_Etype (N, Any_Type);
1811 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1812 -- Set the mode now to ensure that any nodes generated during analysis
1813 -- and expansion are properly marked as Ghost.
1815 Mark_And_Set_Ghost_Procedure_Call (N);
1817 -- Otherwise analyze the parameters
1819 if Present (Actuals) then
1820 Actual := First (Actuals);
1822 while Present (Actual) loop
1824 Check_Parameterless_Call (Actual);
1829 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1831 if Nkind (P) = N_Attribute_Reference
1832 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1834 Name_Elab_Subp_Body)
1836 if Present (Actuals) then
1838 ("no parameters allowed for this call", First (Actuals));
1842 Set_Etype (N, Standard_Void_Type);
1845 elsif Is_Entity_Name (P)
1846 and then Is_Record_Type (Etype (Entity (P)))
1847 and then Remote_AST_I_Dereference (P)
1851 elsif Is_Entity_Name (P)
1852 and then Ekind (Entity (P)) /= E_Entry_Family
1854 if Is_Access_Type (Etype (P))
1855 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1856 and then No (Actuals)
1857 and then Comes_From_Source (N)
1859 Error_Msg_N ("missing explicit dereference in call", N);
1862 Analyze_Call_And_Resolve;
1864 -- If the prefix is the simple name of an entry family, this is a
1865 -- parameterless call from within the task body itself.
1867 elsif Is_Entity_Name (P)
1868 and then Nkind (P) = N_Identifier
1869 and then Ekind (Entity (P)) = E_Entry_Family
1870 and then Present (Actuals)
1871 and then No (Next (First (Actuals)))
1873 -- Can be call to parameterless entry family. What appears to be the
1874 -- sole argument is in fact the entry index. Rewrite prefix of node
1875 -- accordingly. Source representation is unchanged by this
1879 Make_Indexed_Component (Loc,
1881 Make_Selected_Component (Loc,
1882 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1883 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1884 Expressions => Actuals);
1885 Set_Name (N, New_N);
1886 Set_Etype (New_N, Standard_Void_Type);
1887 Set_Parameter_Associations (N, No_List);
1888 Analyze_Call_And_Resolve;
1890 elsif Nkind (P) = N_Explicit_Dereference then
1891 if Ekind (Etype (P)) = E_Subprogram_Type then
1892 Analyze_Call_And_Resolve;
1894 Error_Msg_N ("expect access to procedure in call", P);
1897 -- The name can be a selected component or an indexed component that
1898 -- yields an access to subprogram. Such a prefix is legal if the call
1899 -- has parameter associations.
1901 elsif Is_Access_Type (Etype (P))
1902 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1904 if Present (Actuals) then
1905 Analyze_Call_And_Resolve;
1907 Error_Msg_N ("missing explicit dereference in call ", N);
1910 -- If not an access to subprogram, then the prefix must resolve to the
1911 -- name of an entry, entry family, or protected operation.
1913 -- For the case of a simple entry call, P is a selected component where
1914 -- the prefix is the task and the selector name is the entry. A call to
1915 -- a protected procedure will have the same syntax. If the protected
1916 -- object contains overloaded operations, the entity may appear as a
1917 -- function, the context will select the operation whose type is Void.
1919 elsif Nkind (P) = N_Selected_Component
1920 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1924 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1925 -- in prefix notation may still be missing its controlling argument,
1926 -- so perform the transformation now.
1928 if SPARK_Mode = On and then In_Inlined_Body then
1930 Subp : constant Entity_Id := Entity (Selector_Name (P));
1931 Typ : constant Entity_Id := Etype (Prefix (P));
1934 if Is_Tagged_Type (Typ)
1935 and then Present (First_Formal (Subp))
1936 and then (Etype (First_Formal (Subp)) = Typ
1938 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
1939 and then Try_Object_Operation (P)
1944 Analyze_Call_And_Resolve;
1949 Analyze_Call_And_Resolve;
1952 elsif Nkind (P) = N_Selected_Component
1953 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1954 and then Present (Actuals)
1955 and then No (Next (First (Actuals)))
1957 -- Can be call to parameterless entry family. What appears to be the
1958 -- sole argument is in fact the entry index. Rewrite prefix of node
1959 -- accordingly. Source representation is unchanged by this
1963 Make_Indexed_Component (Loc,
1964 Prefix => New_Copy (P),
1965 Expressions => Actuals);
1966 Set_Name (N, New_N);
1967 Set_Etype (New_N, Standard_Void_Type);
1968 Set_Parameter_Associations (N, No_List);
1969 Analyze_Call_And_Resolve;
1971 -- For the case of a reference to an element of an entry family, P is
1972 -- an indexed component whose prefix is a selected component (task and
1973 -- entry family), and whose index is the entry family index.
1975 elsif Nkind (P) = N_Indexed_Component
1976 and then Nkind (Prefix (P)) = N_Selected_Component
1977 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1979 Analyze_Call_And_Resolve;
1981 -- If the prefix is the name of an entry family, it is a call from
1982 -- within the task body itself.
1984 elsif Nkind (P) = N_Indexed_Component
1985 and then Nkind (Prefix (P)) = N_Identifier
1986 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1989 Make_Selected_Component (Loc,
1991 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1992 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1993 Rewrite (Prefix (P), New_N);
1995 Analyze_Call_And_Resolve;
1997 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1998 -- procedure name, so the construct can only be a qualified expression.
2000 elsif Nkind (P) = N_Qualified_Expression
2001 and then Ada_Version >= Ada_2012
2003 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2006 -- Anything else is an error
2009 Error_Msg_N ("invalid procedure or entry call", N);
2013 Restore_Ghost_Mode (Saved_GM);
2014 end Analyze_Procedure_Call;
2016 ------------------------------
2017 -- Analyze_Return_Statement --
2018 ------------------------------
2020 procedure Analyze_Return_Statement (N : Node_Id) is
2021 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
2022 N_Simple_Return_Statement));
2024 Returns_Object : constant Boolean :=
2025 Nkind (N) = N_Extended_Return_Statement
2027 (Nkind (N) = N_Simple_Return_Statement
2028 and then Present (Expression (N)));
2029 -- True if we're returning something; that is, "return <expression>;"
2030 -- or "return Result : T [:= ...]". False for "return;". Used for error
2031 -- checking: If Returns_Object is True, N should apply to a function
2032 -- body; otherwise N should apply to a procedure body, entry body,
2033 -- accept statement, or extended return statement.
2035 function Find_What_It_Applies_To return Entity_Id;
2036 -- Find the entity representing the innermost enclosing body, accept
2037 -- statement, or extended return statement. If the result is a callable
2038 -- construct or extended return statement, then this will be the value
2039 -- of the Return_Applies_To attribute. Otherwise, the program is
2040 -- illegal. See RM-6.5(4/2).
2042 -----------------------------
2043 -- Find_What_It_Applies_To --
2044 -----------------------------
2046 function Find_What_It_Applies_To return Entity_Id is
2047 Result : Entity_Id := Empty;
2050 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2051 -- and postconditions.
2053 for J in reverse 0 .. Scope_Stack.Last loop
2054 Result := Scope_Stack.Table (J).Entity;
2055 exit when not Ekind_In (Result, E_Block, E_Loop)
2056 and then Chars (Result) /= Name_uPostconditions;
2059 pragma Assert (Present (Result));
2061 end Find_What_It_Applies_To;
2063 -- Local declarations
2065 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2066 Kind : constant Entity_Kind := Ekind (Scope_Id);
2067 Loc : constant Source_Ptr := Sloc (N);
2068 Stm_Entity : constant Entity_Id :=
2070 (E_Return_Statement, Current_Scope, Loc, 'R');
2072 -- Start of processing for Analyze_Return_Statement
2075 Set_Return_Statement_Entity (N, Stm_Entity);
2077 Set_Etype (Stm_Entity, Standard_Void_Type);
2078 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2080 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2081 -- (4/2): an inner return statement will apply to this extended return.
2083 if Nkind (N) = N_Extended_Return_Statement then
2084 Push_Scope (Stm_Entity);
2087 -- Check that pragma No_Return is obeyed. Don't complain about the
2088 -- implicitly-generated return that is placed at the end.
2090 if No_Return (Scope_Id) and then Comes_From_Source (N) then
2091 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
2094 -- Warn on any unassigned OUT parameters if in procedure
2096 if Ekind (Scope_Id) = E_Procedure then
2097 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2100 -- Check that functions return objects, and other things do not
2102 if Kind = E_Function or else Kind = E_Generic_Function then
2103 if not Returns_Object then
2104 Error_Msg_N ("missing expression in return from function", N);
2107 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
2108 if Returns_Object then
2109 Error_Msg_N ("procedure cannot return value (use function)", N);
2112 elsif Kind = E_Entry or else Kind = E_Entry_Family then
2113 if Returns_Object then
2114 if Is_Protected_Type (Scope (Scope_Id)) then
2115 Error_Msg_N ("entry body cannot return value", N);
2117 Error_Msg_N ("accept statement cannot return value", N);
2121 elsif Kind = E_Return_Statement then
2123 -- We are nested within another return statement, which must be an
2124 -- extended_return_statement.
2126 if Returns_Object then
2127 if Nkind (N) = N_Extended_Return_Statement then
2129 ("extended return statement cannot be nested (use `RETURN;`)",
2132 -- Case of a simple return statement with a value inside extended
2133 -- return statement.
2137 ("return nested in extended return statement cannot return "
2138 & "value (use `RETURN;`)", N);
2143 Error_Msg_N ("illegal context for return statement", N);
2146 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2147 Analyze_Function_Return (N);
2149 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2150 Set_Return_Present (Scope_Id);
2153 if Nkind (N) = N_Extended_Return_Statement then
2157 Kill_Current_Values (Last_Assignment_Only => True);
2158 Check_Unreachable_Code (N);
2160 Analyze_Dimension (N);
2161 end Analyze_Return_Statement;
2163 -------------------------------------
2164 -- Analyze_Simple_Return_Statement --
2165 -------------------------------------
2167 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2169 if Present (Expression (N)) then
2170 Mark_Coextensions (N, Expression (N));
2173 Analyze_Return_Statement (N);
2174 end Analyze_Simple_Return_Statement;
2176 -------------------------
2177 -- Analyze_Return_Type --
2178 -------------------------
2180 procedure Analyze_Return_Type (N : Node_Id) is
2181 Designator : constant Entity_Id := Defining_Entity (N);
2182 Typ : Entity_Id := Empty;
2185 -- Normal case where result definition does not indicate an error
2187 if Result_Definition (N) /= Error then
2188 if Nkind (Result_Definition (N)) = N_Access_Definition then
2189 Check_SPARK_05_Restriction
2190 ("access result is not allowed", Result_Definition (N));
2192 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2195 AD : constant Node_Id :=
2196 Access_To_Subprogram_Definition (Result_Definition (N));
2198 if Present (AD) and then Protected_Present (AD) then
2199 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2201 Typ := Access_Definition (N, Result_Definition (N));
2205 Set_Parent (Typ, Result_Definition (N));
2206 Set_Is_Local_Anonymous_Access (Typ);
2207 Set_Etype (Designator, Typ);
2209 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2211 Null_Exclusion_Static_Checks (N);
2213 -- Subtype_Mark case
2216 Find_Type (Result_Definition (N));
2217 Typ := Entity (Result_Definition (N));
2218 Set_Etype (Designator, Typ);
2220 -- Unconstrained array as result is not allowed in SPARK
2222 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2223 Check_SPARK_05_Restriction
2224 ("returning an unconstrained array is not allowed",
2225 Result_Definition (N));
2228 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2230 Null_Exclusion_Static_Checks (N);
2232 -- If a null exclusion is imposed on the result type, then create
2233 -- a null-excluding itype (an access subtype) and use it as the
2234 -- function's Etype. Note that the null exclusion checks are done
2235 -- right before this, because they don't get applied to types that
2236 -- do not come from source.
2238 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2239 Set_Etype (Designator,
2240 Create_Null_Excluding_Itype
2243 Scope_Id => Scope (Current_Scope)));
2245 -- The new subtype must be elaborated before use because
2246 -- it is visible outside of the function. However its base
2247 -- type may not be frozen yet, so the reference that will
2248 -- force elaboration must be attached to the freezing of
2251 -- If the return specification appears on a proper body,
2252 -- the subtype will have been created already on the spec.
2254 if Is_Frozen (Typ) then
2255 if Nkind (Parent (N)) = N_Subprogram_Body
2256 and then Nkind (Parent (Parent (N))) = N_Subunit
2260 Build_Itype_Reference (Etype (Designator), Parent (N));
2264 Ensure_Freeze_Node (Typ);
2267 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2269 Set_Itype (IR, Etype (Designator));
2270 Append_Freeze_Actions (Typ, New_List (IR));
2275 Set_Etype (Designator, Typ);
2278 if Ekind (Typ) = E_Incomplete_Type
2279 or else (Is_Class_Wide_Type (Typ)
2280 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2282 -- AI05-0151: Tagged incomplete types are allowed in all formal
2283 -- parts. Untagged incomplete types are not allowed in bodies.
2284 -- As a consequence, limited views cannot appear in a basic
2285 -- declaration that is itself within a body, because there is
2286 -- no point at which the non-limited view will become visible.
2288 if Ada_Version >= Ada_2012 then
2289 if From_Limited_With (Typ) and then In_Package_Body then
2291 ("invalid use of incomplete type&",
2292 Result_Definition (N), Typ);
2294 -- The return type of a subprogram body cannot be of a
2295 -- formal incomplete type.
2297 elsif Is_Generic_Type (Typ)
2298 and then Nkind (Parent (N)) = N_Subprogram_Body
2301 ("return type cannot be a formal incomplete type",
2302 Result_Definition (N));
2304 elsif Is_Class_Wide_Type (Typ)
2305 and then Is_Generic_Type (Root_Type (Typ))
2306 and then Nkind (Parent (N)) = N_Subprogram_Body
2309 ("return type cannot be a formal incomplete type",
2310 Result_Definition (N));
2312 elsif Is_Tagged_Type (Typ) then
2315 -- Use is legal in a thunk generated for an operation
2316 -- inherited from a progenitor.
2318 elsif Is_Thunk (Designator)
2319 and then Present (Non_Limited_View (Typ))
2323 elsif Nkind (Parent (N)) = N_Subprogram_Body
2324 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2328 ("invalid use of untagged incomplete type&",
2332 -- The type must be completed in the current package. This
2333 -- is checked at the end of the package declaration when
2334 -- Taft-amendment types are identified. If the return type
2335 -- is class-wide, there is no required check, the type can
2336 -- be a bona fide TAT.
2338 if Ekind (Scope (Current_Scope)) = E_Package
2339 and then In_Private_Part (Scope (Current_Scope))
2340 and then not Is_Class_Wide_Type (Typ)
2342 Append_Elmt (Designator, Private_Dependents (Typ));
2347 ("invalid use of incomplete type&", Designator, Typ);
2352 -- Case where result definition does indicate an error
2355 Set_Etype (Designator, Any_Type);
2357 end Analyze_Return_Type;
2359 -----------------------------
2360 -- Analyze_Subprogram_Body --
2361 -----------------------------
2363 procedure Analyze_Subprogram_Body (N : Node_Id) is
2364 Loc : constant Source_Ptr := Sloc (N);
2365 Body_Spec : constant Node_Id := Specification (N);
2366 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2369 if Debug_Flag_C then
2370 Write_Str ("==> subprogram body ");
2371 Write_Name (Chars (Body_Id));
2372 Write_Str (" from ");
2373 Write_Location (Loc);
2378 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2380 -- The real work is split out into the helper, so it can do "return;"
2381 -- without skipping the debug output:
2383 Analyze_Subprogram_Body_Helper (N);
2385 if Debug_Flag_C then
2387 Write_Str ("<== subprogram body ");
2388 Write_Name (Chars (Body_Id));
2389 Write_Str (" from ");
2390 Write_Location (Loc);
2393 end Analyze_Subprogram_Body;
2395 ------------------------------------
2396 -- Analyze_Subprogram_Body_Helper --
2397 ------------------------------------
2399 -- This procedure is called for regular subprogram bodies, generic bodies,
2400 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2401 -- specification matters, and is used to create a proper declaration for
2402 -- the subprogram, or to perform conformance checks.
2404 -- WARNING: This routine manages Ghost regions. Return statements must be
2405 -- replaced by gotos which jump to the end of the routine and restore the
2408 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2409 Body_Spec : Node_Id := Specification (N);
2410 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2411 Loc : constant Source_Ptr := Sloc (N);
2412 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2414 Conformant : Boolean;
2415 Desig_View : Entity_Id := Empty;
2416 Exch_Views : Elist_Id := No_Elist;
2418 Mask_Types : Elist_Id := No_Elist;
2419 Prot_Typ : Entity_Id := Empty;
2420 Spec_Decl : Node_Id := Empty;
2421 Spec_Id : Entity_Id;
2423 Last_Real_Spec_Entity : Entity_Id := Empty;
2424 -- When we analyze a separate spec, the entity chain ends up containing
2425 -- the formals, as well as any itypes generated during analysis of the
2426 -- default expressions for parameters, or the arguments of associated
2427 -- precondition/postcondition pragmas (which are analyzed in the context
2428 -- of the spec since they have visibility on formals).
2430 -- These entities belong with the spec and not the body. However we do
2431 -- the analysis of the body in the context of the spec (again to obtain
2432 -- visibility to the formals), and all the entities generated during
2433 -- this analysis end up also chained to the entity chain of the spec.
2434 -- But they really belong to the body, and there is circuitry to move
2435 -- them from the spec to the body.
2437 -- However, when we do this move, we don't want to move the real spec
2438 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2439 -- variable points to the last real spec entity, so we only move those
2440 -- chained beyond that point. It is initialized to Empty to deal with
2441 -- the case where there is no separate spec.
2443 function Body_Has_Contract return Boolean;
2444 -- Check whether unanalyzed body has an aspect or pragma that may
2445 -- generate a SPARK contract.
2447 function Body_Has_SPARK_Mode_On return Boolean;
2448 -- Check whether SPARK_Mode On applies to the subprogram body, either
2449 -- because it is specified directly on the body, or because it is
2450 -- inherited from the enclosing subprogram or package.
2452 procedure Build_Subprogram_Declaration;
2453 -- Create a matching subprogram declaration for subprogram body N
2455 procedure Check_Anonymous_Return;
2456 -- Ada 2005: if a function returns an access type that denotes a task,
2457 -- or a type that contains tasks, we must create a master entity for
2458 -- the anonymous type, which typically will be used in an allocator
2459 -- in the body of the function.
2461 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2462 -- Look ahead to recognize a pragma that may appear after the body.
2463 -- If there is a previous spec, check that it appears in the same
2464 -- declarative part. If the pragma is Inline_Always, perform inlining
2465 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2466 -- If the body acts as a spec, and inlining is required, we create a
2467 -- subprogram declaration for it, in order to attach the body to inline.
2468 -- If pragma does not appear after the body, check whether there is
2469 -- an inline pragma before any local declarations.
2471 procedure Check_Missing_Return;
2472 -- Checks for a function with a no return statements, and also performs
2473 -- the warning checks implemented by Check_Returns. In formal mode, also
2474 -- verify that a function ends with a RETURN and that a procedure does
2475 -- not contain any RETURN.
2477 function Disambiguate_Spec return Entity_Id;
2478 -- When a primitive is declared between the private view and the full
2479 -- view of a concurrent type which implements an interface, a special
2480 -- mechanism is used to find the corresponding spec of the primitive
2483 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2484 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2485 -- incomplete types coming from a limited context and replace their
2486 -- limited views with the non-limited ones. Return the list of changes
2487 -- to be used to undo the transformation.
2489 function Is_Private_Concurrent_Primitive
2490 (Subp_Id : Entity_Id) return Boolean;
2491 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2492 -- type that implements an interface and has a private view.
2494 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2495 -- N is the body generated for an expression function that is not a
2496 -- completion and Spec_Id the defining entity of its spec. Mark all
2497 -- the not-yet-frozen types referenced by the simple return statement
2498 -- of the function as formally frozen.
2500 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2501 -- Undo the transformation done by Exchange_Limited_Views.
2503 procedure Set_Trivial_Subprogram (N : Node_Id);
2504 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2505 -- subprogram whose body is being analyzed. N is the statement node
2506 -- causing the flag to be set, if the following statement is a return
2507 -- of an entity, we mark the entity as set in source to suppress any
2508 -- warning on the stylized use of function stubs with a dummy return.
2510 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2511 -- Undo the transformation done by Mask_Unfrozen_Types
2513 procedure Verify_Overriding_Indicator;
2514 -- If there was a previous spec, the entity has been entered in the
2515 -- current scope previously. If the body itself carries an overriding
2516 -- indicator, check that it is consistent with the known status of the
2519 -----------------------
2520 -- Body_Has_Contract --
2521 -----------------------
2523 function Body_Has_Contract return Boolean is
2524 Decls : constant List_Id := Declarations (N);
2528 -- Check for aspects that may generate a contract
2530 if Present (Aspect_Specifications (N)) then
2531 Item := First (Aspect_Specifications (N));
2532 while Present (Item) loop
2533 if Is_Subprogram_Contract_Annotation (Item) then
2541 -- Check for pragmas that may generate a contract
2543 if Present (Decls) then
2544 Item := First (Decls);
2545 while Present (Item) loop
2546 if Nkind (Item) = N_Pragma
2547 and then Is_Subprogram_Contract_Annotation (Item)
2557 end Body_Has_Contract;
2559 ----------------------------
2560 -- Body_Has_SPARK_Mode_On --
2561 ----------------------------
2563 function Body_Has_SPARK_Mode_On return Boolean is
2564 Decls : constant List_Id := Declarations (N);
2568 -- Check for SPARK_Mode aspect
2570 if Present (Aspect_Specifications (N)) then
2571 Item := First (Aspect_Specifications (N));
2572 while Present (Item) loop
2573 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2574 return Get_SPARK_Mode_From_Annotation (Item) = On;
2581 -- Check for SPARK_Mode pragma
2583 if Present (Decls) then
2584 Item := First (Decls);
2585 while Present (Item) loop
2587 -- Pragmas that apply to a subprogram body are usually grouped
2588 -- together. Look for a potential pragma SPARK_Mode among them.
2590 if Nkind (Item) = N_Pragma then
2591 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2592 return Get_SPARK_Mode_From_Annotation (Item) = On;
2595 -- Otherwise the first non-pragma declarative item terminates
2596 -- the region where pragma SPARK_Mode may appear.
2606 -- Otherwise, the applicable SPARK_Mode is inherited from the
2607 -- enclosing subprogram or package.
2609 return SPARK_Mode = On;
2610 end Body_Has_SPARK_Mode_On;
2612 ----------------------------------
2613 -- Build_Subprogram_Declaration --
2614 ----------------------------------
2616 procedure Build_Subprogram_Declaration is
2617 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2618 -- Relocate certain categorization pragmas from the declarative list
2619 -- of subprogram body From and insert them after node To. The pragmas
2622 -- Volatile_Function
2623 -- Also copy pragma SPARK_Mode if present in the declarative list
2624 -- of subprogram body From and insert it after node To. This pragma
2625 -- should not be moved, as it applies to the body too.
2631 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2633 Next_Decl : Node_Id;
2636 pragma Assert (Nkind (From) = N_Subprogram_Body);
2638 -- The destination node must be part of a list, as the pragmas are
2639 -- inserted after it.
2641 pragma Assert (Is_List_Member (To));
2643 -- Inspect the declarations of the subprogram body looking for
2644 -- specific pragmas.
2646 Decl := First (Declarations (N));
2647 while Present (Decl) loop
2648 Next_Decl := Next (Decl);
2650 if Nkind (Decl) = N_Pragma then
2651 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
2652 Insert_After (To, New_Copy_Tree (Decl));
2654 elsif Nam_In (Pragma_Name_Unmapped (Decl),
2656 Name_Volatile_Function)
2659 Insert_After (To, Decl);
2670 Subp_Decl : Node_Id;
2672 -- Start of processing for Build_Subprogram_Declaration
2675 -- Create a matching subprogram spec using the profile of the body.
2676 -- The structure of the tree is identical, but has new entities for
2677 -- the defining unit name and formal parameters.
2680 Make_Subprogram_Declaration (Loc,
2681 Specification => Copy_Subprogram_Spec (Body_Spec));
2682 Set_Comes_From_Source (Subp_Decl, True);
2684 -- Relocate the aspects and relevant pragmas from the subprogram body
2685 -- to the generated spec because it acts as the initial declaration.
2687 Insert_Before (N, Subp_Decl);
2688 Move_Aspects (N, To => Subp_Decl);
2689 Move_Pragmas (N, To => Subp_Decl);
2691 -- Ensure that the generated corresponding spec and original body
2692 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2693 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2694 -- correctly set for local subprograms.
2696 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2698 Analyze (Subp_Decl);
2700 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2701 -- the body since the expander may generate calls using that entity.
2702 -- Required to ensure that Expand_Call rewrites calls to this
2703 -- function by calls to the built procedure.
2705 if Modify_Tree_For_C
2706 and then Nkind (Body_Spec) = N_Function_Specification
2708 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2710 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2711 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2712 Corresponding_Procedure
2713 (Defining_Entity (Specification (Subp_Decl))));
2716 -- Analyze any relocated source pragmas or pragmas created for aspect
2719 Decl := Next (Subp_Decl);
2720 while Present (Decl) loop
2722 -- Stop the search for pragmas once the body has been reached as
2723 -- this terminates the region where pragmas may appear.
2728 elsif Nkind (Decl) = N_Pragma then
2735 Spec_Id := Defining_Entity (Subp_Decl);
2736 Set_Corresponding_Spec (N, Spec_Id);
2738 -- Mark the generated spec as a source construct to ensure that all
2739 -- calls to it are properly registered in ALI files for GNATprove.
2741 Set_Comes_From_Source (Spec_Id, True);
2743 -- Ensure that the specs of the subprogram declaration and its body
2744 -- are identical, otherwise they will appear non-conformant due to
2745 -- rewritings in the default values of formal parameters.
2747 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2748 Set_Specification (N, Body_Spec);
2749 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2750 end Build_Subprogram_Declaration;
2752 ----------------------------
2753 -- Check_Anonymous_Return --
2754 ----------------------------
2756 procedure Check_Anonymous_Return is
2762 if Present (Spec_Id) then
2768 if Ekind (Scop) = E_Function
2769 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2770 and then not Is_Thunk (Scop)
2772 -- Skip internally built functions which handle the case of
2773 -- a null access (see Expand_Interface_Conversion)
2775 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2776 and then not Comes_From_Source (Parent (Scop)))
2778 and then (Has_Task (Designated_Type (Etype (Scop)))
2780 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2782 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2783 and then Expander_Active
2785 -- Avoid cases with no tasking support
2787 and then RTE_Available (RE_Current_Master)
2788 and then not Restriction_Active (No_Task_Hierarchy)
2791 Make_Object_Declaration (Loc,
2792 Defining_Identifier =>
2793 Make_Defining_Identifier (Loc, Name_uMaster),
2794 Constant_Present => True,
2795 Object_Definition =>
2796 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2798 Make_Explicit_Dereference (Loc,
2799 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2801 if Present (Declarations (N)) then
2802 Prepend (Decl, Declarations (N));
2804 Set_Declarations (N, New_List (Decl));
2807 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2808 Set_Has_Master_Entity (Scop);
2810 -- Now mark the containing scope as a task master
2813 while Nkind (Par) /= N_Compilation_Unit loop
2814 Par := Parent (Par);
2815 pragma Assert (Present (Par));
2817 -- If we fall off the top, we are at the outer level, and
2818 -- the environment task is our effective master, so nothing
2822 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2824 Set_Is_Task_Master (Par, True);
2829 end Check_Anonymous_Return;
2831 -------------------------
2832 -- Check_Inline_Pragma --
2833 -------------------------
2835 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2839 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2840 -- True when N is a pragma Inline or Inline_Always that applies
2841 -- to this subprogram.
2843 -----------------------
2844 -- Is_Inline_Pragma --
2845 -----------------------
2847 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2849 if Nkind (N) = N_Pragma
2851 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2852 or else (Pragma_Name_Unmapped (N) = Name_Inline
2854 (Front_End_Inlining or else Optimization_Level > 0)))
2855 and then Present (Pragma_Argument_Associations (N))
2858 Pragma_Arg : Node_Id :=
2859 Expression (First (Pragma_Argument_Associations (N)));
2861 if Nkind (Pragma_Arg) = N_Selected_Component then
2862 Pragma_Arg := Selector_Name (Pragma_Arg);
2865 return Chars (Pragma_Arg) = Chars (Body_Id);
2871 end Is_Inline_Pragma;
2873 -- Start of processing for Check_Inline_Pragma
2876 if not Expander_Active then
2880 if Is_List_Member (N)
2881 and then Present (Next (N))
2882 and then Is_Inline_Pragma (Next (N))
2886 elsif Nkind (N) /= N_Subprogram_Body_Stub
2887 and then Present (Declarations (N))
2888 and then Is_Inline_Pragma (First (Declarations (N)))
2890 Prag := First (Declarations (N));
2896 if Present (Prag) then
2897 if Present (Spec_Id) then
2898 if Is_List_Member (N)
2899 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2900 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2906 -- Create a subprogram declaration, to make treatment uniform.
2907 -- Make the sloc of the subprogram name that of the entity in
2908 -- the body, so that style checks find identical strings.
2911 Subp : constant Entity_Id :=
2912 Make_Defining_Identifier
2913 (Sloc (Body_Id), Chars (Body_Id));
2914 Decl : constant Node_Id :=
2915 Make_Subprogram_Declaration (Loc,
2917 New_Copy_Tree (Specification (N)));
2920 -- Link the body and the generated spec
2922 Set_Corresponding_Body (Decl, Body_Id);
2923 Set_Corresponding_Spec (N, Subp);
2925 Set_Defining_Unit_Name (Specification (Decl), Subp);
2927 -- To ensure proper coverage when body is inlined, indicate
2928 -- whether the subprogram comes from source.
2930 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2932 if Present (First_Formal (Body_Id)) then
2933 Plist := Copy_Parameter_List (Body_Id);
2934 Set_Parameter_Specifications
2935 (Specification (Decl), Plist);
2938 -- Move aspects to the new spec
2940 if Has_Aspects (N) then
2941 Move_Aspects (N, To => Decl);
2944 Insert_Before (N, Decl);
2947 Set_Has_Pragma_Inline (Subp);
2949 if Pragma_Name (Prag) = Name_Inline_Always then
2950 Set_Is_Inlined (Subp);
2951 Set_Has_Pragma_Inline_Always (Subp);
2954 -- Prior to copying the subprogram body to create a template
2955 -- for it for subsequent inlining, remove the pragma from
2956 -- the current body so that the copy that will produce the
2957 -- new body will start from a completely unanalyzed tree.
2959 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2960 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2967 end Check_Inline_Pragma;
2969 --------------------------
2970 -- Check_Missing_Return --
2971 --------------------------
2973 procedure Check_Missing_Return is
2975 Missing_Ret : Boolean;
2978 if Nkind (Body_Spec) = N_Function_Specification then
2979 if Present (Spec_Id) then
2985 if Return_Present (Id) then
2986 Check_Returns (HSS, 'F', Missing_Ret);
2989 Set_Has_Missing_Return (Id);
2992 -- Within a premature instantiation of a package with no body, we
2993 -- build completions of the functions therein, with a Raise
2994 -- statement. No point in complaining about a missing return in
2997 elsif Ekind (Id) = E_Function
2998 and then In_Instance
2999 and then Present (Statements (HSS))
3000 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3004 elsif Is_Generic_Subprogram (Id)
3005 or else not Is_Machine_Code_Subprogram (Id)
3007 Error_Msg_N ("missing RETURN statement in function body", N);
3010 -- If procedure with No_Return, check returns
3012 elsif Nkind (Body_Spec) = N_Procedure_Specification
3013 and then Present (Spec_Id)
3014 and then No_Return (Spec_Id)
3016 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
3019 -- Special checks in SPARK mode
3021 if Nkind (Body_Spec) = N_Function_Specification then
3023 -- In SPARK mode, last statement of a function should be a return
3026 Stat : constant Node_Id := Last_Source_Statement (HSS);
3029 and then not Nkind_In (Stat, N_Simple_Return_Statement,
3030 N_Extended_Return_Statement)
3032 Check_SPARK_05_Restriction
3033 ("last statement in function should be RETURN", Stat);
3037 -- In SPARK mode, verify that a procedure has no return
3039 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3040 if Present (Spec_Id) then
3046 -- Would be nice to point to return statement here, can we
3047 -- borrow the Check_Returns procedure here ???
3049 if Return_Present (Id) then
3050 Check_SPARK_05_Restriction
3051 ("procedure should not have RETURN", N);
3054 end Check_Missing_Return;
3056 -----------------------
3057 -- Disambiguate_Spec --
3058 -----------------------
3060 function Disambiguate_Spec return Entity_Id is
3061 Priv_Spec : Entity_Id;
3064 procedure Replace_Types (To_Corresponding : Boolean);
3065 -- Depending on the flag, replace the type of formal parameters of
3066 -- Body_Id if it is a concurrent type implementing interfaces with
3067 -- the corresponding record type or the other way around.
3069 procedure Replace_Types (To_Corresponding : Boolean) is
3071 Formal_Typ : Entity_Id;
3074 Formal := First_Formal (Body_Id);
3075 while Present (Formal) loop
3076 Formal_Typ := Etype (Formal);
3078 if Is_Class_Wide_Type (Formal_Typ) then
3079 Formal_Typ := Root_Type (Formal_Typ);
3082 -- From concurrent type to corresponding record
3084 if To_Corresponding then
3085 if Is_Concurrent_Type (Formal_Typ)
3086 and then Present (Corresponding_Record_Type (Formal_Typ))
3089 (Corresponding_Record_Type (Formal_Typ)))
3092 Corresponding_Record_Type (Formal_Typ));
3095 -- From corresponding record to concurrent type
3098 if Is_Concurrent_Record_Type (Formal_Typ)
3099 and then Present (Interfaces (Formal_Typ))
3102 Corresponding_Concurrent_Type (Formal_Typ));
3106 Next_Formal (Formal);
3110 -- Start of processing for Disambiguate_Spec
3113 -- Try to retrieve the specification of the body as is. All error
3114 -- messages are suppressed because the body may not have a spec in
3115 -- its current state.
3117 Spec_N := Find_Corresponding_Spec (N, False);
3119 -- It is possible that this is the body of a primitive declared
3120 -- between a private and a full view of a concurrent type. The
3121 -- controlling parameter of the spec carries the concurrent type,
3122 -- not the corresponding record type as transformed by Analyze_
3123 -- Subprogram_Specification. In such cases, we undo the change
3124 -- made by the analysis of the specification and try to find the
3127 -- Note that wrappers already have their corresponding specs and
3128 -- bodies set during their creation, so if the candidate spec is
3129 -- a wrapper, then we definitely need to swap all types to their
3130 -- original concurrent status.
3133 or else Is_Primitive_Wrapper (Spec_N)
3135 -- Restore all references of corresponding record types to the
3136 -- original concurrent types.
3138 Replace_Types (To_Corresponding => False);
3139 Priv_Spec := Find_Corresponding_Spec (N, False);
3141 -- The current body truly belongs to a primitive declared between
3142 -- a private and a full view. We leave the modified body as is,
3143 -- and return the true spec.
3145 if Present (Priv_Spec)
3146 and then Is_Private_Primitive (Priv_Spec)
3151 -- In case that this is some sort of error, restore the original
3152 -- state of the body.
3154 Replace_Types (To_Corresponding => True);
3158 end Disambiguate_Spec;
3160 ----------------------------
3161 -- Exchange_Limited_Views --
3162 ----------------------------
3164 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3165 Result : Elist_Id := No_Elist;
3167 procedure Detect_And_Exchange (Id : Entity_Id);
3168 -- Determine whether Id's type denotes an incomplete type associated
3169 -- with a limited with clause and exchange the limited view with the
3170 -- non-limited one when available. Note that the non-limited view
3171 -- may exist because of a with_clause in another unit in the context,
3172 -- but cannot be used because the current view of the enclosing unit
3173 -- is still a limited view.
3175 -------------------------
3176 -- Detect_And_Exchange --
3177 -------------------------
3179 procedure Detect_And_Exchange (Id : Entity_Id) is
3180 Typ : constant Entity_Id := Etype (Id);
3182 if From_Limited_With (Typ)
3183 and then Has_Non_Limited_View (Typ)
3184 and then not From_Limited_With (Scope (Typ))
3187 Result := New_Elmt_List;
3190 Prepend_Elmt (Typ, Result);
3191 Prepend_Elmt (Id, Result);
3192 Set_Etype (Id, Non_Limited_View (Typ));
3194 end Detect_And_Exchange;
3200 -- Start of processing for Exchange_Limited_Views
3203 -- Do not process subprogram bodies as they already use the non-
3204 -- limited view of types.
3206 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3210 -- Examine all formals and swap views when applicable
3212 Formal := First_Formal (Subp_Id);
3213 while Present (Formal) loop
3214 Detect_And_Exchange (Formal);
3216 Next_Formal (Formal);
3219 -- Process the return type of a function
3221 if Ekind (Subp_Id) = E_Function then
3222 Detect_And_Exchange (Subp_Id);
3226 end Exchange_Limited_Views;
3228 -------------------------------------
3229 -- Is_Private_Concurrent_Primitive --
3230 -------------------------------------
3232 function Is_Private_Concurrent_Primitive
3233 (Subp_Id : Entity_Id) return Boolean
3235 Formal_Typ : Entity_Id;
3238 if Present (First_Formal (Subp_Id)) then
3239 Formal_Typ := Etype (First_Formal (Subp_Id));
3241 if Is_Concurrent_Record_Type (Formal_Typ) then
3242 if Is_Class_Wide_Type (Formal_Typ) then
3243 Formal_Typ := Root_Type (Formal_Typ);
3246 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3249 -- The type of the first formal is a concurrent tagged type with
3253 Is_Concurrent_Type (Formal_Typ)
3254 and then Is_Tagged_Type (Formal_Typ)
3255 and then Has_Private_Declaration (Formal_Typ);
3259 end Is_Private_Concurrent_Primitive;
3261 -------------------------
3262 -- Mask_Unfrozen_Types --
3263 -------------------------
3265 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3266 Result : Elist_Id := No_Elist;
3268 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3269 -- Mask all types referenced in the subtree rooted at Node
3271 --------------------
3272 -- Mask_Type_Refs --
3273 --------------------
3275 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3276 procedure Mask_Type (Typ : Entity_Id);
3277 -- ??? what does this do?
3283 procedure Mask_Type (Typ : Entity_Id) is
3285 -- Skip Itypes created by the preanalysis
3288 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3293 if not Is_Frozen (Typ) then
3294 Set_Is_Frozen (Typ);
3295 Append_New_Elmt (Typ, Result);
3299 -- Start of processing for Mask_Type_Refs
3302 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3303 Mask_Type (Etype (Entity (Node)));
3305 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3306 Mask_Type (Scope (Entity (Node)));
3309 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3310 and then Present (Etype (Node))
3312 Mask_Type (Etype (Node));
3318 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3322 Return_Stmt : constant Node_Id :=
3323 First (Statements (Handled_Statement_Sequence (N)));
3325 -- Start of processing for Mask_Unfrozen_Types
3328 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3330 Mask_References (Expression (Return_Stmt));
3333 end Mask_Unfrozen_Types;
3335 ---------------------------
3336 -- Restore_Limited_Views --
3337 ---------------------------
3339 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3340 Elmt : Elmt_Id := First_Elmt (Restore_List);
3344 while Present (Elmt) loop
3347 Set_Etype (Id, Node (Elmt));
3350 end Restore_Limited_Views;
3352 ----------------------------
3353 -- Set_Trivial_Subprogram --
3354 ----------------------------
3356 procedure Set_Trivial_Subprogram (N : Node_Id) is
3357 Nxt : constant Node_Id := Next (N);
3360 Set_Is_Trivial_Subprogram (Body_Id);
3362 if Present (Spec_Id) then
3363 Set_Is_Trivial_Subprogram (Spec_Id);
3367 and then Nkind (Nxt) = N_Simple_Return_Statement
3368 and then No (Next (Nxt))
3369 and then Present (Expression (Nxt))
3370 and then Is_Entity_Name (Expression (Nxt))
3372 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3374 end Set_Trivial_Subprogram;
3376 ---------------------------
3377 -- Unmask_Unfrozen_Types --
3378 ---------------------------
3380 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3381 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3384 while Present (Elmt) loop
3385 Set_Is_Frozen (Node (Elmt), False);
3388 end Unmask_Unfrozen_Types;
3390 ---------------------------------
3391 -- Verify_Overriding_Indicator --
3392 ---------------------------------
3394 procedure Verify_Overriding_Indicator is
3396 if Must_Override (Body_Spec) then
3397 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3398 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3402 elsif not Present (Overridden_Operation (Spec_Id)) then
3404 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3406 -- Overriding indicators aren't allowed for protected subprogram
3407 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3408 -- this to a warning if -gnatd.E is enabled.
3410 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3411 Error_Msg_Warn := Error_To_Warning;
3413 ("<<overriding indicator not allowed for protected "
3414 & "subprogram body", Body_Spec);
3417 elsif Must_Not_Override (Body_Spec) then
3418 if Present (Overridden_Operation (Spec_Id)) then
3420 ("subprogram& overrides inherited operation",
3421 Body_Spec, Spec_Id);
3423 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3424 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3427 ("subprogram& overrides predefined operator ",
3428 Body_Spec, Spec_Id);
3430 -- Overriding indicators aren't allowed for protected subprogram
3431 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3432 -- this to a warning if -gnatd.E is enabled.
3434 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3435 Error_Msg_Warn := Error_To_Warning;
3438 ("<<overriding indicator not allowed "
3439 & "for protected subprogram body", Body_Spec);
3441 -- If this is not a primitive operation, then the overriding
3442 -- indicator is altogether illegal.
3444 elsif not Is_Primitive (Spec_Id) then
3446 ("overriding indicator only allowed "
3447 & "if subprogram is primitive", Body_Spec);
3450 -- If checking the style rule and the operation overrides, then
3451 -- issue a warning about a missing overriding_indicator. Protected
3452 -- subprogram bodies are excluded from this style checking, since
3453 -- they aren't primitives (even though their declarations can
3454 -- override) and aren't allowed to have an overriding_indicator.
3457 and then Present (Overridden_Operation (Spec_Id))
3458 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3460 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3461 Style.Missing_Overriding (N, Body_Id);
3464 and then Can_Override_Operator (Spec_Id)
3465 and then not In_Predefined_Unit (Spec_Id)
3467 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3468 Style.Missing_Overriding (N, Body_Id);
3470 end Verify_Overriding_Indicator;
3474 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3475 Saved_ISMP : constant Boolean :=
3476 Ignore_SPARK_Mode_Pragmas_In_Instance;
3477 -- Save the Ghost and SPARK mode-related data to restore on exit
3479 -- Start of processing for Analyze_Subprogram_Body_Helper
3482 -- A [generic] subprogram body freezes the contract of the nearest
3483 -- enclosing package body and all other contracts encountered in the
3484 -- same declarative part up to and excluding the subprogram body:
3486 -- package body Nearest_Enclosing_Package
3487 -- with Refined_State => (State => Constit)
3491 -- procedure Freezes_Enclosing_Package_Body
3492 -- with Refined_Depends => (Input => Constit) ...
3494 -- This ensures that any annotations referenced by the contract of the
3495 -- [generic] subprogram body are available. This form of freezing is
3496 -- decoupled from the usual Freeze_xxx mechanism because it must also
3497 -- work in the context of generics where normal freezing is disabled.
3499 -- Only bodies coming from source should cause this type of freezing.
3500 -- Expression functions that act as bodies and complete an initial
3501 -- declaration must be included in this category, hence the use of
3504 if Comes_From_Source (Original_Node (N)) then
3505 Freeze_Previous_Contracts (N);
3508 -- Generic subprograms are handled separately. They always have a
3509 -- generic specification. Determine whether current scope has a
3510 -- previous declaration.
3512 -- If the subprogram body is defined within an instance of the same
3513 -- name, the instance appears as a package renaming, and will be hidden
3514 -- within the subprogram.
3516 if Present (Prev_Id)
3517 and then not Is_Overloadable (Prev_Id)
3518 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3519 or else Comes_From_Source (Prev_Id))
3521 if Is_Generic_Subprogram (Prev_Id) then
3524 -- A subprogram body is Ghost when it is stand alone and subject
3525 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3526 -- the mode now to ensure that any nodes generated during analysis
3527 -- and expansion are properly marked as Ghost.
3529 Mark_And_Set_Ghost_Body (N, Spec_Id);
3531 -- If the body completes the initial declaration of a compilation
3532 -- unit which is subject to pragma Elaboration_Checks, set the
3533 -- model specified by the pragma because it applies to all parts
3536 Install_Elaboration_Model (Spec_Id);
3538 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3539 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3541 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3543 if Nkind (N) = N_Subprogram_Body then
3544 HSS := Handled_Statement_Sequence (N);
3545 Check_Missing_Return;
3550 -- Otherwise a previous entity conflicts with the subprogram name.
3551 -- Attempting to enter name will post error.
3554 Enter_Name (Body_Id);
3558 -- Non-generic case, find the subprogram declaration, if one was seen,
3559 -- or enter new overloaded entity in the current scope. If the
3560 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3561 -- part of the context of one of its subunits. No need to redo the
3564 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3568 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3570 if Nkind (N) = N_Subprogram_Body_Stub
3571 or else No (Corresponding_Spec (N))
3573 if Is_Private_Concurrent_Primitive (Body_Id) then
3574 Spec_Id := Disambiguate_Spec;
3576 -- A subprogram body is Ghost when it is stand alone and
3577 -- subject to pragma Ghost or when the corresponding spec is
3578 -- Ghost. Set the mode now to ensure that any nodes generated
3579 -- during analysis and expansion are properly marked as Ghost.
3581 Mark_And_Set_Ghost_Body (N, Spec_Id);
3583 -- If the body completes a compilation unit which is subject
3584 -- to pragma Elaboration_Checks, set the model specified by
3585 -- the pragma because it applies to all parts of the unit.
3587 Install_Elaboration_Model (Spec_Id);
3590 Spec_Id := Find_Corresponding_Spec (N);
3592 -- A subprogram body is Ghost when it is stand alone and
3593 -- subject to pragma Ghost or when the corresponding spec is
3594 -- Ghost. Set the mode now to ensure that any nodes generated
3595 -- during analysis and expansion are properly marked as Ghost.
3597 Mark_And_Set_Ghost_Body (N, Spec_Id);
3599 -- If the body completes a compilation unit which is subject
3600 -- to pragma Elaboration_Checks, set the model specified by
3601 -- the pragma because it applies to all parts of the unit.
3603 Install_Elaboration_Model (Spec_Id);
3605 -- In GNATprove mode, if the body has no previous spec, create
3606 -- one so that the inlining machinery can operate properly.
3607 -- Transfer aspects, if any, to the new spec, so that they
3608 -- are legal and can be processed ahead of the body.
3609 -- We make two copies of the given spec, one for the new
3610 -- declaration, and one for the body.
3612 if No (Spec_Id) and then GNATprove_Mode
3614 -- Inlining does not apply during pre-analysis of code
3616 and then Full_Analysis
3618 -- Inlining only applies to full bodies, not stubs
3620 and then Nkind (N) /= N_Subprogram_Body_Stub
3622 -- Inlining only applies to bodies in the source code, not to
3623 -- those generated by the compiler. In particular, expression
3624 -- functions, whose body is generated by the compiler, are
3625 -- treated specially by GNATprove.
3627 and then Comes_From_Source (Body_Id)
3629 -- This cannot be done for a compilation unit, which is not
3630 -- in a context where we can insert a new spec.
3632 and then Is_List_Member (N)
3634 -- Inlining only applies to subprograms without contracts,
3635 -- as a contract is a sign that GNATprove should perform a
3636 -- modular analysis of the subprogram instead of a contextual
3637 -- analysis at each call site. The same test is performed in
3638 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3639 -- here in another form (because the contract has not been
3640 -- attached to the body) to avoid front-end errors in case
3641 -- pragmas are used instead of aspects, because the
3642 -- corresponding pragmas in the body would not be transferred
3643 -- to the spec, leading to legality errors.
3645 and then not Body_Has_Contract
3646 and then not Inside_A_Generic
3648 Build_Subprogram_Declaration;
3650 -- If this is a function that returns a constrained array, and
3651 -- we are generating SPARK_For_C, create subprogram declaration
3652 -- to simplify subsequent C generation.
3655 and then Modify_Tree_For_C
3656 and then Nkind (Body_Spec) = N_Function_Specification
3657 and then Is_Array_Type (Etype (Body_Id))
3658 and then Is_Constrained (Etype (Body_Id))
3660 Build_Subprogram_Declaration;
3664 -- If this is a duplicate body, no point in analyzing it
3666 if Error_Posted (N) then
3670 -- A subprogram body should cause freezing of its own declaration,
3671 -- but if there was no previous explicit declaration, then the
3672 -- subprogram will get frozen too late (there may be code within
3673 -- the body that depends on the subprogram having been frozen,
3674 -- such as uses of extra formals), so we force it to be frozen
3675 -- here. Same holds if the body and spec are compilation units.
3676 -- Finally, if the return type is an anonymous access to protected
3677 -- subprogram, it must be frozen before the body because its
3678 -- expansion has generated an equivalent type that is used when
3679 -- elaborating the body.
3681 -- An exception in the case of Ada 2012, AI05-177: The bodies
3682 -- created for expression functions do not freeze.
3685 and then Nkind (Original_Node (N)) /= N_Expression_Function
3687 Freeze_Before (N, Body_Id);
3689 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3690 Freeze_Before (N, Spec_Id);
3692 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3693 Freeze_Before (N, Etype (Body_Id));
3697 Spec_Id := Corresponding_Spec (N);
3699 -- A subprogram body is Ghost when it is stand alone and subject
3700 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3701 -- the mode now to ensure that any nodes generated during analysis
3702 -- and expansion are properly marked as Ghost.
3704 Mark_And_Set_Ghost_Body (N, Spec_Id);
3706 -- If the body completes the initial declaration of a compilation
3707 -- unit which is subject to pragma Elaboration_Checks, set the
3708 -- model specified by the pragma because it applies to all parts
3711 Install_Elaboration_Model (Spec_Id);
3715 -- Previously we scanned the body to look for nested subprograms, and
3716 -- rejected an inline directive if nested subprograms were present,
3717 -- because the back-end would generate conflicting symbols for the
3718 -- nested bodies. This is now unnecessary.
3720 -- Look ahead to recognize a pragma Inline that appears after the body
3722 Check_Inline_Pragma (Spec_Id);
3724 -- Deal with special case of a fully private operation in the body of
3725 -- the protected type. We must create a declaration for the subprogram,
3726 -- in order to attach the protected subprogram that will be used in
3727 -- internal calls. We exclude compiler generated bodies from the
3728 -- expander since the issue does not arise for those cases.
3731 and then Comes_From_Source (N)
3732 and then Is_Protected_Type (Current_Scope)
3734 Spec_Id := Build_Private_Protected_Declaration (N);
3737 -- If we are generating C and this is a function returning a constrained
3738 -- array type for which we must create a procedure with an extra out
3739 -- parameter, build and analyze the body now. The procedure declaration
3740 -- has already been created. We reuse the source body of the function,
3741 -- because in an instance it may contain global references that cannot
3742 -- be reanalyzed. The source function itself is not used any further,
3743 -- so we mark it as having a completion. If the subprogram is a stub the
3744 -- transformation is done later, when the proper body is analyzed.
3747 and then Modify_Tree_For_C
3748 and then Present (Spec_Id)
3749 and then Ekind (Spec_Id) = E_Function
3750 and then Nkind (N) /= N_Subprogram_Body_Stub
3751 and then Rewritten_For_C (Spec_Id)
3753 Set_Has_Completion (Spec_Id);
3755 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3758 -- The entity for the created procedure must remain invisible, so it
3759 -- does not participate in resolution of subsequent references to the
3762 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3766 -- If a separate spec is present, then deal with freezing issues
3768 if Present (Spec_Id) then
3769 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3770 Verify_Overriding_Indicator;
3772 -- In general, the spec will be frozen when we start analyzing the
3773 -- body. However, for internally generated operations, such as
3774 -- wrapper functions for inherited operations with controlling
3775 -- results, the spec may not have been frozen by the time we expand
3776 -- the freeze actions that include the bodies. In particular, extra
3777 -- formals for accessibility or for return-in-place may need to be
3778 -- generated. Freeze nodes, if any, are inserted before the current
3779 -- body. These freeze actions are also needed in ASIS mode and in
3780 -- Compile_Only mode to enable the proper back-end type annotations.
3781 -- They are necessary in any case to insure order of elaboration
3784 if not Is_Frozen (Spec_Id)
3785 and then (Expander_Active
3787 or else (Operating_Mode = Check_Semantics
3788 and then Serious_Errors_Detected = 0))
3790 -- The body generated for an expression function that is not a
3791 -- completion is a freeze point neither for the profile nor for
3792 -- anything else. That's why, in order to prevent any freezing
3793 -- during analysis, we need to mask types declared outside the
3794 -- expression that are not yet frozen.
3796 if Nkind (N) = N_Subprogram_Body
3797 and then Was_Expression_Function (N)
3798 and then not Has_Completion (Spec_Id)
3800 Set_Is_Frozen (Spec_Id);
3801 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
3803 Set_Has_Delayed_Freeze (Spec_Id);
3804 Freeze_Before (N, Spec_Id);
3809 -- If the subprogram has a class-wide clone, build its body as a copy
3810 -- of the original body, and rewrite body of original subprogram as a
3811 -- wrapper that calls the clone.
3813 if Present (Spec_Id)
3814 and then Present (Class_Wide_Clone (Spec_Id))
3815 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
3817 Build_Class_Wide_Clone_Body (Spec_Id, N);
3819 -- This is the new body for the existing primitive operation
3821 Rewrite (N, Build_Class_Wide_Clone_Call
3822 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
3823 Set_Has_Completion (Spec_Id, False);
3828 -- Place subprogram on scope stack, and make formals visible. If there
3829 -- is a spec, the visible entity remains that of the spec.
3831 if Present (Spec_Id) then
3832 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3834 if Is_Child_Unit (Spec_Id) then
3835 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3839 Style.Check_Identifier (Body_Id, Spec_Id);
3842 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3843 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3845 if Is_Abstract_Subprogram (Spec_Id) then
3846 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3850 Set_Convention (Body_Id, Convention (Spec_Id));
3851 Set_Has_Completion (Spec_Id);
3853 if Is_Protected_Type (Scope (Spec_Id)) then
3854 Prot_Typ := Scope (Spec_Id);
3857 -- If this is a body generated for a renaming, do not check for
3858 -- full conformance. The check is redundant, because the spec of
3859 -- the body is a copy of the spec in the renaming declaration,
3860 -- and the test can lead to spurious errors on nested defaults.
3862 if Present (Spec_Decl)
3863 and then not Comes_From_Source (N)
3865 (Nkind (Original_Node (Spec_Decl)) =
3866 N_Subprogram_Renaming_Declaration
3867 or else (Present (Corresponding_Body (Spec_Decl))
3869 Nkind (Unit_Declaration_Node
3870 (Corresponding_Body (Spec_Decl))) =
3871 N_Subprogram_Renaming_Declaration))
3875 -- Conversely, the spec may have been generated for specless body
3876 -- with an inline pragma. The entity comes from source, which is
3877 -- both semantically correct and necessary for proper inlining.
3878 -- The subprogram declaration itself is not in the source.
3880 elsif Comes_From_Source (N)
3881 and then Present (Spec_Decl)
3882 and then not Comes_From_Source (Spec_Decl)
3883 and then Has_Pragma_Inline (Spec_Id)
3890 Fully_Conformant, True, Conformant, Body_Id);
3893 -- If the body is not fully conformant, we have to decide if we
3894 -- should analyze it or not. If it has a really messed up profile
3895 -- then we probably should not analyze it, since we will get too
3896 -- many bogus messages.
3898 -- Our decision is to go ahead in the non-fully conformant case
3899 -- only if it is at least mode conformant with the spec. Note
3900 -- that the call to Check_Fully_Conformant has issued the proper
3901 -- error messages to complain about the lack of conformance.
3904 and then not Mode_Conformant (Body_Id, Spec_Id)
3910 if Spec_Id /= Body_Id then
3911 Reference_Body_Formals (Spec_Id, Body_Id);
3914 Set_Ekind (Body_Id, E_Subprogram_Body);
3916 if Nkind (N) = N_Subprogram_Body_Stub then
3917 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3922 Set_Corresponding_Spec (N, Spec_Id);
3924 -- Ada 2005 (AI-345): If the operation is a primitive operation
3925 -- of a concurrent type, the type of the first parameter has been
3926 -- replaced with the corresponding record, which is the proper
3927 -- run-time structure to use. However, within the body there may
3928 -- be uses of the formals that depend on primitive operations
3929 -- of the type (in particular calls in prefixed form) for which
3930 -- we need the original concurrent type. The operation may have
3931 -- several controlling formals, so the replacement must be done
3934 if Comes_From_Source (Spec_Id)
3935 and then Present (First_Entity (Spec_Id))
3936 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3937 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3938 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3939 and then Present (Corresponding_Concurrent_Type
3940 (Etype (First_Entity (Spec_Id))))
3943 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3947 Form := First_Formal (Spec_Id);
3948 while Present (Form) loop
3949 if Etype (Form) = Typ then
3950 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3958 -- Make the formals visible, and place subprogram on scope stack.
3959 -- This is also the point at which we set Last_Real_Spec_Entity
3960 -- to mark the entities which will not be moved to the body.
3962 Install_Formals (Spec_Id);
3963 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3965 -- Within an instance, add local renaming declarations so that
3966 -- gdb can retrieve the values of actuals more easily. This is
3967 -- only relevant if generating code (and indeed we definitely
3968 -- do not want these definitions -gnatc mode, because that would
3971 if Is_Generic_Instance (Spec_Id)
3972 and then Is_Wrapper_Package (Current_Scope)
3973 and then Expander_Active
3975 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3978 Push_Scope (Spec_Id);
3980 -- Make sure that the subprogram is immediately visible. For
3981 -- child units that have no separate spec this is indispensable.
3982 -- Otherwise it is safe albeit redundant.
3984 Set_Is_Immediately_Visible (Spec_Id);
3987 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3988 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3989 Set_Scope (Body_Id, Scope (Spec_Id));
3991 -- Case of subprogram body with no previous spec
3994 -- Check for style warning required
3998 -- Only apply check for source level subprograms for which checks
3999 -- have not been suppressed.
4001 and then Comes_From_Source (Body_Id)
4002 and then not Suppress_Style_Checks (Body_Id)
4004 -- No warnings within an instance
4006 and then not In_Instance
4008 -- No warnings for expression functions
4010 and then Nkind (Original_Node (N)) /= N_Expression_Function
4012 Style.Body_With_No_Spec (N);
4015 New_Overloaded_Entity (Body_Id);
4017 if Nkind (N) /= N_Subprogram_Body_Stub then
4018 Set_Acts_As_Spec (N);
4019 Generate_Definition (Body_Id);
4021 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4023 -- If the body is an entry wrapper created for an entry with
4024 -- preconditions, it must be compiled in the context of the
4025 -- enclosing synchronized object, because it may mention other
4026 -- operations of the type.
4028 if Is_Entry_Wrapper (Body_Id) then
4030 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4033 Install_Declarations (Prot);
4037 Install_Formals (Body_Id);
4039 Push_Scope (Body_Id);
4042 -- For stubs and bodies with no previous spec, generate references to
4045 Generate_Reference_To_Formals (Body_Id);
4048 -- Entry barrier functions are generated outside the protected type and
4049 -- should not carry the SPARK_Mode of the enclosing context.
4051 if Nkind (N) = N_Subprogram_Body
4052 and then Is_Entry_Barrier_Function (N)
4056 -- The body is generated as part of expression function expansion. When
4057 -- the expression function appears in the visible declarations of a
4058 -- package, the body is added to the private declarations. Since both
4059 -- declarative lists may be subject to a different SPARK_Mode, inherit
4060 -- the mode of the spec.
4062 -- package P with SPARK_Mode is
4063 -- function Expr_Func ... is (...); -- original
4064 -- [function Expr_Func ...;] -- generated spec
4067 -- pragma SPARK_Mode (Off);
4068 -- [function Expr_Func ... is return ...;] -- generated body
4069 -- end P; -- mode is ON
4071 elsif not Comes_From_Source (N)
4072 and then Present (Spec_Id)
4073 and then Is_Expression_Function (Spec_Id)
4075 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4076 Set_SPARK_Pragma_Inherited
4077 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4079 -- Set the SPARK_Mode from the current context (may be overwritten later
4080 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4081 -- initially on a stand-alone subprogram body, but is then relocated to
4082 -- a generated corresponding spec. In this scenario the mode is shared
4083 -- between the spec and body.
4085 elsif No (SPARK_Pragma (Body_Id)) then
4086 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4087 Set_SPARK_Pragma_Inherited (Body_Id);
4090 -- A subprogram body may be instantiated or inlined at a later pass.
4091 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4092 -- applied to the initial declaration of the body.
4094 if Present (Spec_Id) then
4095 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4096 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4100 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4101 -- case the body is instantiated or inlined later and out of context.
4102 -- The body uses this attribute to restore the value of the global
4105 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4106 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4108 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4109 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4113 -- If this is the proper body of a stub, we must verify that the stub
4114 -- conforms to the body, and to the previous spec if one was present.
4115 -- We know already that the body conforms to that spec. This test is
4116 -- only required for subprograms that come from source.
4118 if Nkind (Parent (N)) = N_Subunit
4119 and then Comes_From_Source (N)
4120 and then not Error_Posted (Body_Id)
4121 and then Nkind (Corresponding_Stub (Parent (N))) =
4122 N_Subprogram_Body_Stub
4125 Old_Id : constant Entity_Id :=
4127 (Specification (Corresponding_Stub (Parent (N))));
4129 Conformant : Boolean := False;
4132 if No (Spec_Id) then
4133 Check_Fully_Conformant (Body_Id, Old_Id);
4137 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4139 if not Conformant then
4141 -- The stub was taken to be a new declaration. Indicate that
4144 Set_Has_Completion (Old_Id, False);
4150 Set_Has_Completion (Body_Id);
4151 Check_Eliminated (Body_Id);
4153 -- Analyze any aspect specifications that appear on the subprogram body
4154 -- stub. Stop the analysis now as the stub does not have a declarative
4155 -- or a statement part, and it cannot be inlined.
4157 if Nkind (N) = N_Subprogram_Body_Stub then
4158 if Has_Aspects (N) then
4159 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4167 -- Note: Normally we don't do any inlining if expansion is off, since
4168 -- we won't generate code in any case. An exception arises in GNATprove
4169 -- mode where we want to expand some calls in place, even with expansion
4170 -- disabled, since the inlining eases formal verification.
4172 if not GNATprove_Mode
4173 and then Expander_Active
4174 and then Serious_Errors_Detected = 0
4175 and then Present (Spec_Id)
4176 and then Has_Pragma_Inline (Spec_Id)
4178 -- Legacy implementation (relying on front-end inlining)
4180 if not Back_End_Inlining then
4181 if (Has_Pragma_Inline_Always (Spec_Id)
4182 and then not Opt.Disable_FE_Inline_Always)
4183 or else (Front_End_Inlining
4184 and then not Opt.Disable_FE_Inline)
4186 Build_Body_To_Inline (N, Spec_Id);
4189 -- New implementation (relying on back-end inlining)
4192 if Has_Pragma_Inline_Always (Spec_Id)
4193 or else Optimization_Level > 0
4195 -- Handle function returning an unconstrained type
4197 if Comes_From_Source (Body_Id)
4198 and then Ekind (Spec_Id) = E_Function
4199 and then Returns_Unconstrained_Type (Spec_Id)
4201 -- If function builds in place, i.e. returns a limited type,
4202 -- inlining cannot be done.
4204 and then not Is_Limited_Type (Etype (Spec_Id))
4206 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4210 Subp_Body : constant Node_Id :=
4211 Unit_Declaration_Node (Body_Id);
4212 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4215 -- Do not pass inlining to the backend if the subprogram
4216 -- has declarations or statements which cannot be inlined
4217 -- by the backend. This check is done here to emit an
4218 -- error instead of the generic warning message reported
4219 -- by the GCC backend (ie. "function might not be
4222 if Present (Subp_Decl)
4223 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4227 elsif Has_Excluded_Statement
4230 (Handled_Statement_Sequence (Subp_Body)))
4234 -- If the backend inlining is available then at this
4235 -- stage we only have to mark the subprogram as inlined.
4236 -- The expander will take care of registering it in the
4237 -- table of subprograms inlined by the backend a part of
4238 -- processing calls to it (cf. Expand_Call)
4241 Set_Is_Inlined (Spec_Id);
4248 -- In GNATprove mode, inline only when there is a separate subprogram
4249 -- declaration for now, as inlining of subprogram bodies acting as
4250 -- declarations, or subprogram stubs, are not supported by front-end
4251 -- inlining. This inlining should occur after analysis of the body, so
4252 -- that it is known whether the value of SPARK_Mode, which can be
4253 -- defined by a pragma inside the body, is applicable to the body.
4254 -- Inlining can be disabled with switch -gnatdm
4256 elsif GNATprove_Mode
4257 and then Full_Analysis
4258 and then not Inside_A_Generic
4259 and then Present (Spec_Id)
4261 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4262 and then Body_Has_SPARK_Mode_On
4263 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4264 and then not Body_Has_Contract
4265 and then not Debug_Flag_M
4267 Build_Body_To_Inline (N, Spec_Id);
4270 -- When generating code, inherited pre/postconditions are handled when
4271 -- expanding the corresponding contract.
4273 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4274 -- of the specification we have to install the private withed units.
4275 -- This holds for child units as well.
4277 if Is_Compilation_Unit (Body_Id)
4278 or else Nkind (Parent (N)) = N_Compilation_Unit
4280 Install_Private_With_Clauses (Body_Id);
4283 Check_Anonymous_Return;
4285 -- Set the Protected_Formal field of each extra formal of the protected
4286 -- subprogram to reference the corresponding extra formal of the
4287 -- subprogram that implements it. For regular formals this occurs when
4288 -- the protected subprogram's declaration is expanded, but the extra
4289 -- formals don't get created until the subprogram is frozen. We need to
4290 -- do this before analyzing the protected subprogram's body so that any
4291 -- references to the original subprogram's extra formals will be changed
4292 -- refer to the implementing subprogram's formals (see Expand_Formal).
4294 if Present (Spec_Id)
4295 and then Is_Protected_Type (Scope (Spec_Id))
4296 and then Present (Protected_Body_Subprogram (Spec_Id))
4299 Impl_Subp : constant Entity_Id :=
4300 Protected_Body_Subprogram (Spec_Id);
4301 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4302 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4305 while Present (Prot_Ext_Formal) loop
4306 pragma Assert (Present (Impl_Ext_Formal));
4307 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4308 Next_Formal_With_Extras (Prot_Ext_Formal);
4309 Next_Formal_With_Extras (Impl_Ext_Formal);
4314 -- Now we can go on to analyze the body
4316 HSS := Handled_Statement_Sequence (N);
4317 Set_Actual_Subtypes (N, Current_Scope);
4319 -- Add a declaration for the Protection object, renaming declarations
4320 -- for discriminals and privals and finally a declaration for the entry
4321 -- family index (if applicable). This form of early expansion is done
4322 -- when the Expander is active because Install_Private_Data_Declarations
4323 -- references entities which were created during regular expansion. The
4324 -- subprogram entity must come from source, and not be an internally
4325 -- generated subprogram.
4328 and then Present (Prot_Typ)
4329 and then Present (Spec_Id)
4330 and then Comes_From_Source (Spec_Id)
4331 and then not Is_Eliminated (Spec_Id)
4333 Install_Private_Data_Declarations
4334 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4337 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4338 -- may now appear in parameter and result profiles. Since the analysis
4339 -- of a subprogram body may use the parameter and result profile of the
4340 -- spec, swap any limited views with their non-limited counterpart.
4342 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4343 Exch_Views := Exchange_Limited_Views (Spec_Id);
4346 -- If the return type is an anonymous access type whose designated type
4347 -- is the limited view of a class-wide type and the non-limited view is
4348 -- available, update the return type accordingly.
4350 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4356 Rtyp := Etype (Spec_Id);
4358 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4359 Etyp := Directly_Designated_Type (Rtyp);
4361 if Is_Class_Wide_Type (Etyp)
4362 and then From_Limited_With (Etyp)
4365 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4371 -- Analyze any aspect specifications that appear on the subprogram body
4373 if Has_Aspects (N) then
4374 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4377 Analyze_Declarations (Declarations (N));
4379 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4381 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4382 if Present (SPARK_Pragma (Spec_Id)) then
4383 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4385 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4387 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4388 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4389 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4391 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4394 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4398 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4399 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4400 Error_Msg_Sloc := Sloc (Spec_Id);
4402 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4406 -- A subprogram body freezes its own contract. Analyze the contract
4407 -- after the declarations of the body have been processed as pragmas
4408 -- are now chained on the contract of the subprogram body.
4410 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4412 -- Check completion, and analyze the statements
4415 Inspect_Deferred_Constant_Completion (Declarations (N));
4418 -- Deal with end of scope processing for the body
4420 Process_End_Label (HSS, 't', Current_Scope);
4421 Update_Use_Clause_Chain;
4424 -- If we are compiling an entry wrapper, remove the enclosing
4425 -- synchronized object from the stack.
4427 if Is_Entry_Wrapper (Body_Id) then
4431 Check_Subprogram_Order (N);
4432 Set_Analyzed (Body_Id);
4434 -- If we have a separate spec, then the analysis of the declarations
4435 -- caused the entities in the body to be chained to the spec id, but
4436 -- we want them chained to the body id. Only the formal parameters
4437 -- end up chained to the spec id in this case.
4439 if Present (Spec_Id) then
4441 -- We must conform to the categorization of our spec
4443 Validate_Categorization_Dependency (N, Spec_Id);
4445 -- And if this is a child unit, the parent units must conform
4447 if Is_Child_Unit (Spec_Id) then
4448 Validate_Categorization_Dependency
4449 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4452 -- Here is where we move entities from the spec to the body
4454 -- Case where there are entities that stay with the spec
4456 if Present (Last_Real_Spec_Entity) then
4458 -- No body entities (happens when the only real spec entities come
4459 -- from precondition and postcondition pragmas).
4461 if No (Last_Entity (Body_Id)) then
4462 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4464 -- Body entities present (formals), so chain stuff past them
4468 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4471 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4472 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4473 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4475 -- Case where there are no spec entities, in this case there can be
4476 -- no body entities either, so just move everything.
4478 -- If the body is generated for an expression function, it may have
4479 -- been preanalyzed already, if 'access was applied to it.
4482 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4483 N_Expression_Function
4485 pragma Assert (No (Last_Entity (Body_Id)));
4489 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4490 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4491 Set_First_Entity (Spec_Id, Empty);
4492 Set_Last_Entity (Spec_Id, Empty);
4495 -- Otherwise the body does not complete a previous declaration. Check
4496 -- the categorization of the body against the units it withs.
4499 Validate_Categorization_Dependency (N, Body_Id);
4502 Check_Missing_Return;
4504 -- Now we are going to check for variables that are never modified in
4505 -- the body of the procedure. But first we deal with a special case
4506 -- where we want to modify this check. If the body of the subprogram
4507 -- starts with a raise statement or its equivalent, or if the body
4508 -- consists entirely of a null statement, then it is pretty obvious that
4509 -- it is OK to not reference the parameters. For example, this might be
4510 -- the following common idiom for a stubbed function: statement of the
4511 -- procedure raises an exception. In particular this deals with the
4512 -- common idiom of a stubbed function, which appears something like:
4514 -- function F (A : Integer) return Some_Type;
4517 -- raise Program_Error;
4521 -- Here the purpose of X is simply to satisfy the annoying requirement
4522 -- in Ada that there be at least one return, and we certainly do not
4523 -- want to go posting warnings on X that it is not initialized. On
4524 -- the other hand, if X is entirely unreferenced that should still
4527 -- What we do is to detect these cases, and if we find them, flag the
4528 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4529 -- suppress unwanted warnings. For the case of the function stub above
4530 -- we have a special test to set X as apparently assigned to suppress
4537 -- Skip call markers installed by the ABE mechanism, labels, and
4538 -- Push_xxx_Error_Label to find the first real statement.
4540 Stm := First (Statements (HSS));
4541 while Nkind_In (Stm, N_Call_Marker, N_Label)
4542 or else Nkind (Stm) in N_Push_xxx_Label
4547 -- Do the test on the original statement before expansion
4550 Ostm : constant Node_Id := Original_Node (Stm);
4553 -- If explicit raise statement, turn on flag
4555 if Nkind (Ostm) = N_Raise_Statement then
4556 Set_Trivial_Subprogram (Stm);
4558 -- If null statement, and no following statements, turn on flag
4560 elsif Nkind (Stm) = N_Null_Statement
4561 and then Comes_From_Source (Stm)
4562 and then No (Next (Stm))
4564 Set_Trivial_Subprogram (Stm);
4566 -- Check for explicit call cases which likely raise an exception
4568 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4569 if Is_Entity_Name (Name (Ostm)) then
4571 Ent : constant Entity_Id := Entity (Name (Ostm));
4574 -- If the procedure is marked No_Return, then likely it
4575 -- raises an exception, but in any case it is not coming
4576 -- back here, so turn on the flag.
4579 and then Ekind (Ent) = E_Procedure
4580 and then No_Return (Ent)
4582 Set_Trivial_Subprogram (Stm);
4590 -- Check for variables that are never modified
4597 -- If there is a separate spec, then transfer Never_Set_In_Source
4598 -- flags from out parameters to the corresponding entities in the
4599 -- body. The reason we do that is we want to post error flags on
4600 -- the body entities, not the spec entities.
4602 if Present (Spec_Id) then
4603 E1 := First_Entity (Spec_Id);
4604 while Present (E1) loop
4605 if Ekind (E1) = E_Out_Parameter then
4606 E2 := First_Entity (Body_Id);
4607 while Present (E2) loop
4608 exit when Chars (E1) = Chars (E2);
4612 if Present (E2) then
4613 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4621 -- Check references in body
4623 Check_References (Body_Id);
4626 -- Check for nested subprogram, and mark outer level subprogram if so
4632 if Present (Spec_Id) then
4639 Ent := Enclosing_Subprogram (Ent);
4640 exit when No (Ent) or else Is_Subprogram (Ent);
4643 if Present (Ent) then
4644 Set_Has_Nested_Subprogram (Ent);
4648 -- Restore the limited views in the spec, if any, to let the back end
4649 -- process it without running into circularities.
4651 if Exch_Views /= No_Elist then
4652 Restore_Limited_Views (Exch_Views);
4655 if Mask_Types /= No_Elist then
4656 Unmask_Unfrozen_Types (Mask_Types);
4659 if Present (Desig_View) then
4660 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4664 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4665 Restore_Ghost_Mode (Saved_GM);
4666 end Analyze_Subprogram_Body_Helper;
4668 ------------------------------------
4669 -- Analyze_Subprogram_Declaration --
4670 ------------------------------------
4672 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4673 Scop : constant Entity_Id := Current_Scope;
4674 Designator : Entity_Id;
4676 Is_Completion : Boolean;
4677 -- Indicates whether a null procedure declaration is a completion
4680 -- Null procedures are not allowed in SPARK
4682 if Nkind (Specification (N)) = N_Procedure_Specification
4683 and then Null_Present (Specification (N))
4685 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4687 -- Null procedures are allowed in protected types, following the
4688 -- recent AI12-0147.
4690 if Is_Protected_Type (Current_Scope)
4691 and then Ada_Version < Ada_2012
4693 Error_Msg_N ("protected operation cannot be a null procedure", N);
4696 Analyze_Null_Procedure (N, Is_Completion);
4698 -- The null procedure acts as a body, nothing further is needed
4700 if Is_Completion then
4705 Designator := Analyze_Subprogram_Specification (Specification (N));
4707 -- A reference may already have been generated for the unit name, in
4708 -- which case the following call is redundant. However it is needed for
4709 -- declarations that are the rewriting of an expression function.
4711 Generate_Definition (Designator);
4713 -- Set the SPARK mode from the current context (may be overwritten later
4714 -- with explicit pragma). This is not done for entry barrier functions
4715 -- because they are generated outside the protected type and should not
4716 -- carry the mode of the enclosing context.
4718 if Nkind (N) = N_Subprogram_Declaration
4719 and then Is_Entry_Barrier_Function (N)
4724 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4725 Set_SPARK_Pragma_Inherited (Designator);
4728 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
4729 -- the body of this subprogram is instantiated or inlined later and out
4730 -- of context. The body uses this attribute to restore the value of the
4733 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4734 Set_Ignore_SPARK_Mode_Pragmas (Designator);
4737 -- Preserve relevant elaboration-related attributes of the context which
4738 -- are no longer available or very expensive to recompute once analysis,
4739 -- resolution, and expansion are over.
4741 Mark_Elaboration_Attributes
4742 (N_Id => Designator,
4745 if Debug_Flag_C then
4746 Write_Str ("==> subprogram spec ");
4747 Write_Name (Chars (Designator));
4748 Write_Str (" from ");
4749 Write_Location (Sloc (N));
4754 Validate_RCI_Subprogram_Declaration (N);
4755 New_Overloaded_Entity (Designator);
4756 Check_Delayed_Subprogram (Designator);
4758 -- If the type of the first formal of the current subprogram is a non-
4759 -- generic tagged private type, mark the subprogram as being a private
4760 -- primitive. Ditto if this is a function with controlling result, and
4761 -- the return type is currently private. In both cases, the type of the
4762 -- controlling argument or result must be in the current scope for the
4763 -- operation to be primitive.
4765 if Has_Controlling_Result (Designator)
4766 and then Is_Private_Type (Etype (Designator))
4767 and then Scope (Etype (Designator)) = Current_Scope
4768 and then not Is_Generic_Actual_Type (Etype (Designator))
4770 Set_Is_Private_Primitive (Designator);
4772 elsif Present (First_Formal (Designator)) then
4774 Formal_Typ : constant Entity_Id :=
4775 Etype (First_Formal (Designator));
4777 Set_Is_Private_Primitive (Designator,
4778 Is_Tagged_Type (Formal_Typ)
4779 and then Scope (Formal_Typ) = Current_Scope
4780 and then Is_Private_Type (Formal_Typ)
4781 and then not Is_Generic_Actual_Type (Formal_Typ));
4785 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4788 if Ada_Version >= Ada_2005
4789 and then Comes_From_Source (N)
4790 and then Is_Dispatching_Operation (Designator)
4797 if Has_Controlling_Result (Designator) then
4798 Etyp := Etype (Designator);
4801 E := First_Entity (Designator);
4803 and then Is_Formal (E)
4804 and then not Is_Controlling_Formal (E)
4812 if Is_Access_Type (Etyp) then
4813 Etyp := Directly_Designated_Type (Etyp);
4816 if Is_Interface (Etyp)
4817 and then not Is_Abstract_Subprogram (Designator)
4818 and then not (Ekind (Designator) = E_Procedure
4819 and then Null_Present (Specification (N)))
4821 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4823 -- Specialize error message based on procedures vs. functions,
4824 -- since functions can't be null subprograms.
4826 if Ekind (Designator) = E_Procedure then
4828 ("interface procedure % must be abstract or null", N);
4831 ("interface function % must be abstract", N);
4837 -- What is the following code for, it used to be
4839 -- ??? Set_Suppress_Elaboration_Checks
4840 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4842 -- The following seems equivalent, but a bit dubious
4844 if Elaboration_Checks_Suppressed (Designator) then
4845 Set_Kill_Elaboration_Checks (Designator);
4848 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4849 Set_Categorization_From_Scope (Designator, Scop);
4852 -- For a compilation unit, check for library-unit pragmas
4854 Push_Scope (Designator);
4855 Set_Categorization_From_Pragmas (N);
4856 Validate_Categorization_Dependency (N, Designator);
4860 -- For a compilation unit, set body required. This flag will only be
4861 -- reset if a valid Import or Interface pragma is processed later on.
4863 if Nkind (Parent (N)) = N_Compilation_Unit then
4864 Set_Body_Required (Parent (N), True);
4866 if Ada_Version >= Ada_2005
4867 and then Nkind (Specification (N)) = N_Procedure_Specification
4868 and then Null_Present (Specification (N))
4871 ("null procedure cannot be declared at library level", N);
4875 Generate_Reference_To_Formals (Designator);
4876 Check_Eliminated (Designator);
4878 if Debug_Flag_C then
4880 Write_Str ("<== subprogram spec ");
4881 Write_Name (Chars (Designator));
4882 Write_Str (" from ");
4883 Write_Location (Sloc (N));
4887 if Is_Protected_Type (Current_Scope) then
4889 -- Indicate that this is a protected operation, because it may be
4890 -- used in subsequent declarations within the protected type.
4892 Set_Convention (Designator, Convention_Protected);
4895 List_Inherited_Pre_Post_Aspects (Designator);
4897 if Has_Aspects (N) then
4898 Analyze_Aspect_Specifications (N, Designator);
4900 end Analyze_Subprogram_Declaration;
4902 --------------------------------------
4903 -- Analyze_Subprogram_Specification --
4904 --------------------------------------
4906 -- Reminder: N here really is a subprogram specification (not a subprogram
4907 -- declaration). This procedure is called to analyze the specification in
4908 -- both subprogram bodies and subprogram declarations (specs).
4910 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4911 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
4912 -- Determine whether entity E denotes the spec or body of an invariant
4915 ------------------------------------
4916 -- Is_Invariant_Procedure_Or_Body --
4917 ------------------------------------
4919 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
4920 Decl : constant Node_Id := Unit_Declaration_Node (E);
4924 if Nkind (Decl) = N_Subprogram_Body then
4925 Spec := Corresponding_Spec (Decl);
4932 and then Ekind (Spec) = E_Procedure
4933 and then (Is_Partial_Invariant_Procedure (Spec)
4934 or else Is_Invariant_Procedure (Spec));
4935 end Is_Invariant_Procedure_Or_Body;
4939 Designator : constant Entity_Id := Defining_Entity (N);
4940 Formals : constant List_Id := Parameter_Specifications (N);
4942 -- Start of processing for Analyze_Subprogram_Specification
4945 -- User-defined operator is not allowed in SPARK, except as a renaming
4947 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4948 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4950 Check_SPARK_05_Restriction
4951 ("user-defined operator is not allowed", N);
4954 -- Proceed with analysis. Do not emit a cross-reference entry if the
4955 -- specification comes from an expression function, because it may be
4956 -- the completion of a previous declaration. If it is not, the cross-
4957 -- reference entry will be emitted for the new subprogram declaration.
4959 if Nkind (Parent (N)) /= N_Expression_Function then
4960 Generate_Definition (Designator);
4963 if Nkind (N) = N_Function_Specification then
4964 Set_Ekind (Designator, E_Function);
4965 Set_Mechanism (Designator, Default_Mechanism);
4967 Set_Ekind (Designator, E_Procedure);
4968 Set_Etype (Designator, Standard_Void_Type);
4971 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4972 -- those subprograms which could be inlined in GNATprove mode (because
4973 -- Body_To_Inline is non-Empty) but should not be inlined.
4975 if GNATprove_Mode then
4976 Set_Is_Inlined_Always (Designator);
4979 -- Introduce new scope for analysis of the formals and the return type
4981 Set_Scope (Designator, Current_Scope);
4983 if Present (Formals) then
4984 Push_Scope (Designator);
4985 Process_Formals (Formals, N);
4987 -- Check dimensions in N for formals with default expression
4989 Analyze_Dimension_Formals (N, Formals);
4991 -- Ada 2005 (AI-345): If this is an overriding operation of an
4992 -- inherited interface operation, and the controlling type is
4993 -- a synchronized type, replace the type with its corresponding
4994 -- record, to match the proper signature of an overriding operation.
4995 -- Same processing for an access parameter whose designated type is
4996 -- derived from a synchronized interface.
4998 -- This modification is not done for invariant procedures because
4999 -- the corresponding record may not necessarely be visible when the
5000 -- concurrent type acts as the full view of a private type.
5003 -- type Prot is private with Type_Invariant => ...;
5004 -- procedure ConcInvariant (Obj : Prot);
5006 -- protected type Prot is ...;
5007 -- type Concurrent_Record_Prot is record ...;
5008 -- procedure ConcInvariant (Obj : Prot) is
5010 -- end ConcInvariant;
5013 -- In the example above, both the spec and body of the invariant
5014 -- procedure must utilize the private type as the controlling type.
5016 if Ada_Version >= Ada_2005
5017 and then not Is_Invariant_Procedure_Or_Body (Designator)
5021 Formal_Typ : Entity_Id;
5022 Rec_Typ : Entity_Id;
5023 Desig_Typ : Entity_Id;
5026 Formal := First_Formal (Designator);
5027 while Present (Formal) loop
5028 Formal_Typ := Etype (Formal);
5030 if Is_Concurrent_Type (Formal_Typ)
5031 and then Present (Corresponding_Record_Type (Formal_Typ))
5033 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5035 if Present (Interfaces (Rec_Typ)) then
5036 Set_Etype (Formal, Rec_Typ);
5039 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5040 Desig_Typ := Designated_Type (Formal_Typ);
5042 if Is_Concurrent_Type (Desig_Typ)
5043 and then Present (Corresponding_Record_Type (Desig_Typ))
5045 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5047 if Present (Interfaces (Rec_Typ)) then
5048 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5053 Next_Formal (Formal);
5060 -- The subprogram scope is pushed and popped around the processing of
5061 -- the return type for consistency with call above to Process_Formals
5062 -- (which itself can call Analyze_Return_Type), and to ensure that any
5063 -- itype created for the return type will be associated with the proper
5066 elsif Nkind (N) = N_Function_Specification then
5067 Push_Scope (Designator);
5068 Analyze_Return_Type (N);
5074 if Nkind (N) = N_Function_Specification then
5076 -- Deal with operator symbol case
5078 if Nkind (Designator) = N_Defining_Operator_Symbol then
5079 Valid_Operator_Definition (Designator);
5082 May_Need_Actuals (Designator);
5084 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5085 -- the subprogram is abstract also. This does not apply to renaming
5086 -- declarations, where abstractness is inherited, and to subprogram
5087 -- bodies generated for stream operations, which become renamings as
5090 -- In case of primitives associated with abstract interface types
5091 -- the check is applied later (see Analyze_Subprogram_Declaration).
5093 if not Nkind_In (Original_Node (Parent (N)),
5094 N_Abstract_Subprogram_Declaration,
5095 N_Formal_Abstract_Subprogram_Declaration,
5096 N_Subprogram_Renaming_Declaration)
5098 if Is_Abstract_Type (Etype (Designator))
5099 and then not Is_Interface (Etype (Designator))
5102 ("function that returns abstract type must be abstract", N);
5104 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5105 -- access result whose designated type is abstract.
5107 elsif Ada_Version >= Ada_2012
5108 and then Nkind (Result_Definition (N)) = N_Access_Definition
5110 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5111 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5114 ("function whose access result designates abstract type "
5115 & "must be abstract", N);
5121 end Analyze_Subprogram_Specification;
5123 -----------------------
5124 -- Check_Conformance --
5125 -----------------------
5127 procedure Check_Conformance
5128 (New_Id : Entity_Id;
5130 Ctype : Conformance_Type;
5132 Conforms : out Boolean;
5133 Err_Loc : Node_Id := Empty;
5134 Get_Inst : Boolean := False;
5135 Skip_Controlling_Formals : Boolean := False)
5137 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5138 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5139 -- If Errmsg is True, then processing continues to post an error message
5140 -- for conformance error on given node. Two messages are output. The
5141 -- first message points to the previous declaration with a general "no
5142 -- conformance" message. The second is the detailed reason, supplied as
5143 -- Msg. The parameter N provide information for a possible & insertion
5144 -- in the message, and also provides the location for posting the
5145 -- message in the absence of a specified Err_Loc location.
5147 function Conventions_Match
5149 Id2 : Entity_Id) return Boolean;
5150 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5153 -----------------------
5154 -- Conformance_Error --
5155 -----------------------
5157 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5164 if No (Err_Loc) then
5170 Error_Msg_Sloc := Sloc (Old_Id);
5173 when Type_Conformant =>
5174 Error_Msg_N -- CODEFIX
5175 ("not type conformant with declaration#!", Enode);
5177 when Mode_Conformant =>
5178 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5180 ("not mode conformant with operation inherited#!",
5184 ("not mode conformant with declaration#!", Enode);
5187 when Subtype_Conformant =>
5188 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5190 ("not subtype conformant with operation inherited#!",
5194 ("not subtype conformant with declaration#!", Enode);
5197 when Fully_Conformant =>
5198 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5199 Error_Msg_N -- CODEFIX
5200 ("not fully conformant with operation inherited#!",
5203 Error_Msg_N -- CODEFIX
5204 ("not fully conformant with declaration#!", Enode);
5208 Error_Msg_NE (Msg, Enode, N);
5210 end Conformance_Error;
5212 -----------------------
5213 -- Conventions_Match --
5214 -----------------------
5216 function Conventions_Match
5218 Id2 : Entity_Id) return Boolean
5221 -- Ignore the conventions of anonymous access-to-subprogram types
5222 -- and subprogram types because these are internally generated and
5223 -- the only way these may receive a convention is if they inherit
5224 -- the convention of a related subprogram.
5226 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5229 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5234 -- Otherwise compare the conventions directly
5237 return Convention (Id1) = Convention (Id2);
5239 end Conventions_Match;
5243 Old_Type : constant Entity_Id := Etype (Old_Id);
5244 New_Type : constant Entity_Id := Etype (New_Id);
5245 Old_Formal : Entity_Id;
5246 New_Formal : Entity_Id;
5247 Access_Types_Match : Boolean;
5248 Old_Formal_Base : Entity_Id;
5249 New_Formal_Base : Entity_Id;
5251 -- Start of processing for Check_Conformance
5256 -- We need a special case for operators, since they don't appear
5259 if Ctype = Type_Conformant then
5260 if Ekind (New_Id) = E_Operator
5261 and then Operator_Matches_Spec (New_Id, Old_Id)
5267 -- If both are functions/operators, check return types conform
5269 if Old_Type /= Standard_Void_Type
5271 New_Type /= Standard_Void_Type
5273 -- If we are checking interface conformance we omit controlling
5274 -- arguments and result, because we are only checking the conformance
5275 -- of the remaining parameters.
5277 if Has_Controlling_Result (Old_Id)
5278 and then Has_Controlling_Result (New_Id)
5279 and then Skip_Controlling_Formals
5283 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5284 if Ctype >= Subtype_Conformant
5285 and then not Predicates_Match (Old_Type, New_Type)
5288 ("\predicate of return type does not match!", New_Id);
5291 ("\return type does not match!", New_Id);
5297 -- Ada 2005 (AI-231): In case of anonymous access types check the
5298 -- null-exclusion and access-to-constant attributes match.
5300 if Ada_Version >= Ada_2005
5301 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5303 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5304 or else Is_Access_Constant (Etype (Old_Type)) /=
5305 Is_Access_Constant (Etype (New_Type)))
5307 Conformance_Error ("\return type does not match!", New_Id);
5311 -- If either is a function/operator and the other isn't, error
5313 elsif Old_Type /= Standard_Void_Type
5314 or else New_Type /= Standard_Void_Type
5316 Conformance_Error ("\functions can only match functions!", New_Id);
5320 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5321 -- If this is a renaming as body, refine error message to indicate that
5322 -- the conflict is with the original declaration. If the entity is not
5323 -- frozen, the conventions don't have to match, the one of the renamed
5324 -- entity is inherited.
5326 if Ctype >= Subtype_Conformant then
5327 if not Conventions_Match (Old_Id, New_Id) then
5328 if not Is_Frozen (New_Id) then
5331 elsif Present (Err_Loc)
5332 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5333 and then Present (Corresponding_Spec (Err_Loc))
5335 Error_Msg_Name_1 := Chars (New_Id);
5337 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5338 Conformance_Error ("\prior declaration for% has convention %!");
5341 Conformance_Error ("\calling conventions do not match!");
5346 elsif Is_Formal_Subprogram (Old_Id)
5347 or else Is_Formal_Subprogram (New_Id)
5349 Conformance_Error ("\formal subprograms not allowed!");
5354 -- Deal with parameters
5356 -- Note: we use the entity information, rather than going directly
5357 -- to the specification in the tree. This is not only simpler, but
5358 -- absolutely necessary for some cases of conformance tests between
5359 -- operators, where the declaration tree simply does not exist.
5361 Old_Formal := First_Formal (Old_Id);
5362 New_Formal := First_Formal (New_Id);
5363 while Present (Old_Formal) and then Present (New_Formal) loop
5364 if Is_Controlling_Formal (Old_Formal)
5365 and then Is_Controlling_Formal (New_Formal)
5366 and then Skip_Controlling_Formals
5368 -- The controlling formals will have different types when
5369 -- comparing an interface operation with its match, but both
5370 -- or neither must be access parameters.
5372 if Is_Access_Type (Etype (Old_Formal))
5374 Is_Access_Type (Etype (New_Formal))
5376 goto Skip_Controlling_Formal;
5379 ("\access parameter does not match!", New_Formal);
5383 -- Ada 2012: Mode conformance also requires that formal parameters
5384 -- be both aliased, or neither.
5386 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5387 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5389 ("\aliased parameter mismatch!", New_Formal);
5393 if Ctype = Fully_Conformant then
5395 -- Names must match. Error message is more accurate if we do
5396 -- this before checking that the types of the formals match.
5398 if Chars (Old_Formal) /= Chars (New_Formal) then
5399 Conformance_Error ("\name& does not match!", New_Formal);
5401 -- Set error posted flag on new formal as well to stop
5402 -- junk cascaded messages in some cases.
5404 Set_Error_Posted (New_Formal);
5408 -- Null exclusion must match
5410 if Null_Exclusion_Present (Parent (Old_Formal))
5412 Null_Exclusion_Present (Parent (New_Formal))
5414 -- Only give error if both come from source. This should be
5415 -- investigated some time, since it should not be needed ???
5417 if Comes_From_Source (Old_Formal)
5419 Comes_From_Source (New_Formal)
5422 ("\null exclusion for& does not match", New_Formal);
5424 -- Mark error posted on the new formal to avoid duplicated
5425 -- complaint about types not matching.
5427 Set_Error_Posted (New_Formal);
5432 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5433 -- case occurs whenever a subprogram is being renamed and one of its
5434 -- parameters imposes a null exclusion. For example:
5436 -- type T is null record;
5437 -- type Acc_T is access T;
5438 -- subtype Acc_T_Sub is Acc_T;
5440 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5441 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5444 Old_Formal_Base := Etype (Old_Formal);
5445 New_Formal_Base := Etype (New_Formal);
5448 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5449 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5452 Access_Types_Match := Ada_Version >= Ada_2005
5454 -- Ensure that this rule is only applied when New_Id is a
5455 -- renaming of Old_Id.
5457 and then Nkind (Parent (Parent (New_Id))) =
5458 N_Subprogram_Renaming_Declaration
5459 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5460 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5461 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5463 -- Now handle the allowed access-type case
5465 and then Is_Access_Type (Old_Formal_Base)
5466 and then Is_Access_Type (New_Formal_Base)
5468 -- The type kinds must match. The only exception occurs with
5469 -- multiple generics of the form:
5472 -- type F is private; type A is private;
5473 -- type F_Ptr is access F; type A_Ptr is access A;
5474 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5475 -- package F_Pack is ... package A_Pack is
5476 -- package F_Inst is
5477 -- new F_Pack (A, A_Ptr, A_P);
5479 -- When checking for conformance between the parameters of A_P
5480 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5481 -- because the compiler has transformed A_Ptr into a subtype of
5482 -- F_Ptr. We catch this case in the code below.
5484 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5486 (Is_Generic_Type (Old_Formal_Base)
5487 and then Is_Generic_Type (New_Formal_Base)
5488 and then Is_Internal (New_Formal_Base)
5489 and then Etype (Etype (New_Formal_Base)) =
5491 and then Directly_Designated_Type (Old_Formal_Base) =
5492 Directly_Designated_Type (New_Formal_Base)
5493 and then ((Is_Itype (Old_Formal_Base)
5494 and then Can_Never_Be_Null (Old_Formal_Base))
5496 (Is_Itype (New_Formal_Base)
5497 and then Can_Never_Be_Null (New_Formal_Base)));
5499 -- Types must always match. In the visible part of an instance,
5500 -- usual overloading rules for dispatching operations apply, and
5501 -- we check base types (not the actual subtypes).
5503 if In_Instance_Visible_Part
5504 and then Is_Dispatching_Operation (New_Id)
5506 if not Conforming_Types
5507 (T1 => Base_Type (Etype (Old_Formal)),
5508 T2 => Base_Type (Etype (New_Formal)),
5510 Get_Inst => Get_Inst)
5511 and then not Access_Types_Match
5513 Conformance_Error ("\type of & does not match!", New_Formal);
5517 elsif not Conforming_Types
5518 (T1 => Old_Formal_Base,
5519 T2 => New_Formal_Base,
5521 Get_Inst => Get_Inst)
5522 and then not Access_Types_Match
5524 -- Don't give error message if old type is Any_Type. This test
5525 -- avoids some cascaded errors, e.g. in case of a bad spec.
5527 if Errmsg and then Old_Formal_Base = Any_Type then
5530 if Ctype >= Subtype_Conformant
5532 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5535 ("\predicate of & does not match!", New_Formal);
5538 ("\type of & does not match!", New_Formal);
5540 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5542 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5550 -- For mode conformance, mode must match
5552 if Ctype >= Mode_Conformant then
5553 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5554 if not Ekind_In (New_Id, E_Function, E_Procedure)
5555 or else not Is_Primitive_Wrapper (New_Id)
5557 Conformance_Error ("\mode of & does not match!", New_Formal);
5561 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5563 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5565 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5568 ("\mode of & does not match!", New_Formal);
5575 -- Part of mode conformance for access types is having the same
5576 -- constant modifier.
5578 elsif Access_Types_Match
5579 and then Is_Access_Constant (Old_Formal_Base) /=
5580 Is_Access_Constant (New_Formal_Base)
5583 ("\constant modifier does not match!", New_Formal);
5588 if Ctype >= Subtype_Conformant then
5590 -- Ada 2005 (AI-231): In case of anonymous access types check
5591 -- the null-exclusion and access-to-constant attributes must
5592 -- match. For null exclusion, we test the types rather than the
5593 -- formals themselves, since the attribute is only set reliably
5594 -- on the formals in the Ada 95 case, and we exclude the case
5595 -- where Old_Formal is marked as controlling, to avoid errors
5596 -- when matching completing bodies with dispatching declarations
5597 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5599 if Ada_Version >= Ada_2005
5600 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5601 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5603 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5604 Can_Never_Be_Null (Etype (New_Formal))
5606 not Is_Controlling_Formal (Old_Formal))
5608 Is_Access_Constant (Etype (Old_Formal)) /=
5609 Is_Access_Constant (Etype (New_Formal)))
5611 -- Do not complain if error already posted on New_Formal. This
5612 -- avoids some redundant error messages.
5614 and then not Error_Posted (New_Formal)
5616 -- It is allowed to omit the null-exclusion in case of stream
5617 -- attribute subprograms. We recognize stream subprograms
5618 -- through their TSS-generated suffix.
5621 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5624 if TSS_Name /= TSS_Stream_Read
5625 and then TSS_Name /= TSS_Stream_Write
5626 and then TSS_Name /= TSS_Stream_Input
5627 and then TSS_Name /= TSS_Stream_Output
5629 -- Here we have a definite conformance error. It is worth
5630 -- special casing the error message for the case of a
5631 -- controlling formal (which excludes null).
5633 if Is_Controlling_Formal (New_Formal) then
5634 Error_Msg_Node_2 := Scope (New_Formal);
5636 ("\controlling formal & of & excludes null, "
5637 & "declaration must exclude null as well",
5640 -- Normal case (couldn't we give more detail here???)
5644 ("\type of & does not match!", New_Formal);
5653 -- Full conformance checks
5655 if Ctype = Fully_Conformant then
5657 -- We have checked already that names match
5659 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5661 -- Check default expressions for in parameters
5664 NewD : constant Boolean :=
5665 Present (Default_Value (New_Formal));
5666 OldD : constant Boolean :=
5667 Present (Default_Value (Old_Formal));
5669 if NewD or OldD then
5671 -- The old default value has been analyzed because the
5672 -- current full declaration will have frozen everything
5673 -- before. The new default value has not been analyzed,
5674 -- so analyze it now before we check for conformance.
5677 Push_Scope (New_Id);
5678 Preanalyze_Spec_Expression
5679 (Default_Value (New_Formal), Etype (New_Formal));
5683 if not (NewD and OldD)
5684 or else not Fully_Conformant_Expressions
5685 (Default_Value (Old_Formal),
5686 Default_Value (New_Formal))
5689 ("\default expression for & does not match!",
5698 -- A couple of special checks for Ada 83 mode. These checks are
5699 -- skipped if either entity is an operator in package Standard,
5700 -- or if either old or new instance is not from the source program.
5702 if Ada_Version = Ada_83
5703 and then Sloc (Old_Id) > Standard_Location
5704 and then Sloc (New_Id) > Standard_Location
5705 and then Comes_From_Source (Old_Id)
5706 and then Comes_From_Source (New_Id)
5709 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5710 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5713 -- Explicit IN must be present or absent in both cases. This
5714 -- test is required only in the full conformance case.
5716 if In_Present (Old_Param) /= In_Present (New_Param)
5717 and then Ctype = Fully_Conformant
5720 ("\(Ada 83) IN must appear in both declarations",
5725 -- Grouping (use of comma in param lists) must be the same
5726 -- This is where we catch a misconformance like:
5729 -- A : Integer; B : Integer
5731 -- which are represented identically in the tree except
5732 -- for the setting of the flags More_Ids and Prev_Ids.
5734 if More_Ids (Old_Param) /= More_Ids (New_Param)
5735 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5738 ("\grouping of & does not match!", New_Formal);
5744 -- This label is required when skipping controlling formals
5746 <<Skip_Controlling_Formal>>
5748 Next_Formal (Old_Formal);
5749 Next_Formal (New_Formal);
5752 if Present (Old_Formal) then
5753 Conformance_Error ("\too few parameters!");
5756 elsif Present (New_Formal) then
5757 Conformance_Error ("\too many parameters!", New_Formal);
5760 end Check_Conformance;
5762 -----------------------
5763 -- Check_Conventions --
5764 -----------------------
5766 procedure Check_Conventions (Typ : Entity_Id) is
5767 Ifaces_List : Elist_Id;
5769 procedure Check_Convention (Op : Entity_Id);
5770 -- Verify that the convention of inherited dispatching operation Op is
5771 -- consistent among all subprograms it overrides. In order to minimize
5772 -- the search, Search_From is utilized to designate a specific point in
5773 -- the list rather than iterating over the whole list once more.
5775 ----------------------
5776 -- Check_Convention --
5777 ----------------------
5779 procedure Check_Convention (Op : Entity_Id) is
5780 Op_Conv : constant Convention_Id := Convention (Op);
5781 Iface_Conv : Convention_Id;
5782 Iface_Elmt : Elmt_Id;
5783 Iface_Prim_Elmt : Elmt_Id;
5784 Iface_Prim : Entity_Id;
5787 Iface_Elmt := First_Elmt (Ifaces_List);
5788 while Present (Iface_Elmt) loop
5790 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5791 while Present (Iface_Prim_Elmt) loop
5792 Iface_Prim := Node (Iface_Prim_Elmt);
5793 Iface_Conv := Convention (Iface_Prim);
5795 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5796 and then Iface_Conv /= Op_Conv
5799 ("inconsistent conventions in primitive operations", Typ);
5801 Error_Msg_Name_1 := Chars (Op);
5802 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5803 Error_Msg_Sloc := Sloc (Op);
5805 if Comes_From_Source (Op) or else No (Alias (Op)) then
5806 if not Present (Overridden_Operation (Op)) then
5807 Error_Msg_N ("\\primitive % defined #", Typ);
5810 ("\\overriding operation % with "
5811 & "convention % defined #", Typ);
5814 else pragma Assert (Present (Alias (Op)));
5815 Error_Msg_Sloc := Sloc (Alias (Op));
5816 Error_Msg_N ("\\inherited operation % with "
5817 & "convention % defined #", Typ);
5820 Error_Msg_Name_1 := Chars (Op);
5821 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5822 Error_Msg_Sloc := Sloc (Iface_Prim);
5823 Error_Msg_N ("\\overridden operation % with "
5824 & "convention % defined #", Typ);
5826 -- Avoid cascading errors
5831 Next_Elmt (Iface_Prim_Elmt);
5834 Next_Elmt (Iface_Elmt);
5836 end Check_Convention;
5840 Prim_Op : Entity_Id;
5841 Prim_Op_Elmt : Elmt_Id;
5843 -- Start of processing for Check_Conventions
5846 if not Has_Interfaces (Typ) then
5850 Collect_Interfaces (Typ, Ifaces_List);
5852 -- The algorithm checks every overriding dispatching operation against
5853 -- all the corresponding overridden dispatching operations, detecting
5854 -- differences in conventions.
5856 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5857 while Present (Prim_Op_Elmt) loop
5858 Prim_Op := Node (Prim_Op_Elmt);
5860 -- A small optimization: skip the predefined dispatching operations
5861 -- since they always have the same convention.
5863 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5864 Check_Convention (Prim_Op);
5867 Next_Elmt (Prim_Op_Elmt);
5869 end Check_Conventions;
5871 ------------------------------
5872 -- Check_Delayed_Subprogram --
5873 ------------------------------
5875 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5876 procedure Possible_Freeze (T : Entity_Id);
5877 -- T is the type of either a formal parameter or of the return type. If
5878 -- T is not yet frozen and needs a delayed freeze, then the subprogram
5879 -- itself must be delayed.
5881 ---------------------
5882 -- Possible_Freeze --
5883 ---------------------
5885 procedure Possible_Freeze (T : Entity_Id) is
5886 Scop : constant Entity_Id := Scope (Designator);
5889 -- If the subprogram appears within a package instance (which may be
5890 -- the wrapper package of a subprogram instance) the freeze node for
5891 -- that package will freeze the subprogram at the proper place, so
5892 -- do not emit a freeze node for the subprogram, given that it may
5893 -- appear in the wrong scope.
5895 if Ekind (Scop) = E_Package
5896 and then not Comes_From_Source (Scop)
5897 and then Is_Generic_Instance (Scop)
5901 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5902 Set_Has_Delayed_Freeze (Designator);
5904 elsif Is_Access_Type (T)
5905 and then Has_Delayed_Freeze (Designated_Type (T))
5906 and then not Is_Frozen (Designated_Type (T))
5908 Set_Has_Delayed_Freeze (Designator);
5910 end Possible_Freeze;
5916 -- Start of processing for Check_Delayed_Subprogram
5919 -- All subprograms, including abstract subprograms, may need a freeze
5920 -- node if some formal type or the return type needs one.
5922 Possible_Freeze (Etype (Designator));
5923 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5925 -- Need delayed freeze if any of the formal types themselves need a
5926 -- delayed freeze and are not yet frozen.
5928 F := First_Formal (Designator);
5929 while Present (F) loop
5930 Possible_Freeze (Etype (F));
5931 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5935 -- Mark functions that return by reference. Note that it cannot be done
5936 -- for delayed_freeze subprograms because the underlying returned type
5937 -- may not be known yet (for private types).
5939 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5941 Typ : constant Entity_Id := Etype (Designator);
5942 Utyp : constant Entity_Id := Underlying_Type (Typ);
5945 if Is_Limited_View (Typ) then
5946 Set_Returns_By_Ref (Designator);
5948 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5949 Set_Returns_By_Ref (Designator);
5953 end Check_Delayed_Subprogram;
5955 ------------------------------------
5956 -- Check_Discriminant_Conformance --
5957 ------------------------------------
5959 procedure Check_Discriminant_Conformance
5964 Old_Discr : Entity_Id := First_Discriminant (Prev);
5965 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5966 New_Discr_Id : Entity_Id;
5967 New_Discr_Type : Entity_Id;
5969 procedure Conformance_Error (Msg : String; N : Node_Id);
5970 -- Post error message for conformance error on given node. Two messages
5971 -- are output. The first points to the previous declaration with a
5972 -- general "no conformance" message. The second is the detailed reason,
5973 -- supplied as Msg. The parameter N provide information for a possible
5974 -- & insertion in the message.
5976 -----------------------
5977 -- Conformance_Error --
5978 -----------------------
5980 procedure Conformance_Error (Msg : String; N : Node_Id) is
5982 Error_Msg_Sloc := Sloc (Prev_Loc);
5983 Error_Msg_N -- CODEFIX
5984 ("not fully conformant with declaration#!", N);
5985 Error_Msg_NE (Msg, N, N);
5986 end Conformance_Error;
5988 -- Start of processing for Check_Discriminant_Conformance
5991 while Present (Old_Discr) and then Present (New_Discr) loop
5992 New_Discr_Id := Defining_Identifier (New_Discr);
5994 -- The subtype mark of the discriminant on the full type has not
5995 -- been analyzed so we do it here. For an access discriminant a new
5998 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6000 Access_Definition (N, Discriminant_Type (New_Discr));
6003 Analyze (Discriminant_Type (New_Discr));
6004 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6006 -- Ada 2005: if the discriminant definition carries a null
6007 -- exclusion, create an itype to check properly for consistency
6008 -- with partial declaration.
6010 if Is_Access_Type (New_Discr_Type)
6011 and then Null_Exclusion_Present (New_Discr)
6014 Create_Null_Excluding_Itype
6015 (T => New_Discr_Type,
6016 Related_Nod => New_Discr,
6017 Scope_Id => Current_Scope);
6021 if not Conforming_Types
6022 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6024 Conformance_Error ("type of & does not match!", New_Discr_Id);
6027 -- Treat the new discriminant as an occurrence of the old one,
6028 -- for navigation purposes, and fill in some semantic
6029 -- information, for completeness.
6031 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6032 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6033 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6038 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6039 Conformance_Error ("name & does not match!", New_Discr_Id);
6043 -- Default expressions must match
6046 NewD : constant Boolean :=
6047 Present (Expression (New_Discr));
6048 OldD : constant Boolean :=
6049 Present (Expression (Parent (Old_Discr)));
6052 if NewD or OldD then
6054 -- The old default value has been analyzed and expanded,
6055 -- because the current full declaration will have frozen
6056 -- everything before. The new default values have not been
6057 -- expanded, so expand now to check conformance.
6060 Preanalyze_Spec_Expression
6061 (Expression (New_Discr), New_Discr_Type);
6064 if not (NewD and OldD)
6065 or else not Fully_Conformant_Expressions
6066 (Expression (Parent (Old_Discr)),
6067 Expression (New_Discr))
6071 ("default expression for & does not match!",
6078 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6080 if Ada_Version = Ada_83 then
6082 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6085 -- Grouping (use of comma in param lists) must be the same
6086 -- This is where we catch a misconformance like:
6089 -- A : Integer; B : Integer
6091 -- which are represented identically in the tree except
6092 -- for the setting of the flags More_Ids and Prev_Ids.
6094 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6095 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6098 ("grouping of & does not match!", New_Discr_Id);
6104 Next_Discriminant (Old_Discr);
6108 if Present (Old_Discr) then
6109 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6112 elsif Present (New_Discr) then
6114 ("too many discriminants!", Defining_Identifier (New_Discr));
6117 end Check_Discriminant_Conformance;
6119 ----------------------------
6120 -- Check_Fully_Conformant --
6121 ----------------------------
6123 procedure Check_Fully_Conformant
6124 (New_Id : Entity_Id;
6126 Err_Loc : Node_Id := Empty)
6129 pragma Warnings (Off, Result);
6132 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6133 end Check_Fully_Conformant;
6135 --------------------------
6136 -- Check_Limited_Return --
6137 --------------------------
6139 procedure Check_Limited_Return
6145 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6146 -- replaced by anonymous access results. This is an incompatibility with
6147 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6148 -- controllable with special switch. ???
6150 -- A limited interface that is not immutably limited is OK
6152 if Is_Limited_Interface (R_Type)
6154 not (Is_Task_Interface (R_Type)
6155 or else Is_Protected_Interface (R_Type)
6156 or else Is_Synchronized_Interface (R_Type))
6160 elsif Is_Limited_Type (R_Type)
6161 and then not Is_Interface (R_Type)
6162 and then Comes_From_Source (N)
6163 and then not In_Instance_Body
6164 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6166 -- Error in Ada 2005
6168 if Ada_Version >= Ada_2005
6169 and then not Debug_Flag_Dot_L
6170 and then not GNAT_Mode
6173 ("(Ada 2005) cannot copy object of a limited type "
6174 & "(RM-2005 6.5(5.5/2))", Expr);
6176 if Is_Limited_View (R_Type) then
6178 ("\return by reference not permitted in Ada 2005", Expr);
6181 -- Warn in Ada 95 mode, to give folks a heads up about this
6184 -- In GNAT mode, this is just a warning, to allow it to be evilly
6185 -- turned off. Otherwise it is a real error.
6187 -- In a generic context, simplify the warning because it makes no
6188 -- sense to discuss pass-by-reference or copy.
6190 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6191 if Inside_A_Generic then
6193 ("return of limited object not permitted in Ada 2005 "
6194 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6196 elsif Is_Limited_View (R_Type) then
6198 ("return by reference not permitted in Ada 2005 "
6199 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6202 ("cannot copy object of a limited type in Ada 2005 "
6203 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6206 -- Ada 95 mode, and compatibility warnings disabled
6209 pragma Assert (Ada_Version <= Ada_95);
6210 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6211 return; -- skip continuation messages below
6214 if not Inside_A_Generic then
6216 ("\consider switching to return of access type", Expr);
6217 Explain_Limited_Type (R_Type, Expr);
6220 end Check_Limited_Return;
6222 ---------------------------
6223 -- Check_Mode_Conformant --
6224 ---------------------------
6226 procedure Check_Mode_Conformant
6227 (New_Id : Entity_Id;
6229 Err_Loc : Node_Id := Empty;
6230 Get_Inst : Boolean := False)
6233 pragma Warnings (Off, Result);
6236 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6237 end Check_Mode_Conformant;
6239 --------------------------------
6240 -- Check_Overriding_Indicator --
6241 --------------------------------
6243 procedure Check_Overriding_Indicator
6245 Overridden_Subp : Entity_Id;
6246 Is_Primitive : Boolean)
6252 -- No overriding indicator for literals
6254 if Ekind (Subp) = E_Enumeration_Literal then
6257 elsif Ekind (Subp) = E_Entry then
6258 Decl := Parent (Subp);
6260 -- No point in analyzing a malformed operator
6262 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6263 and then Error_Posted (Subp)
6268 Decl := Unit_Declaration_Node (Subp);
6271 if Nkind_In (Decl, N_Subprogram_Body,
6272 N_Subprogram_Body_Stub,
6273 N_Subprogram_Declaration,
6274 N_Abstract_Subprogram_Declaration,
6275 N_Subprogram_Renaming_Declaration)
6277 Spec := Specification (Decl);
6279 elsif Nkind (Decl) = N_Entry_Declaration then
6286 -- The overriding operation is type conformant with the overridden one,
6287 -- but the names of the formals are not required to match. If the names
6288 -- appear permuted in the overriding operation, this is a possible
6289 -- source of confusion that is worth diagnosing. Controlling formals
6290 -- often carry names that reflect the type, and it is not worthwhile
6291 -- requiring that their names match.
6293 if Present (Overridden_Subp)
6294 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6301 Form1 := First_Formal (Subp);
6302 Form2 := First_Formal (Overridden_Subp);
6304 -- If the overriding operation is a synchronized operation, skip
6305 -- the first parameter of the overridden operation, which is
6306 -- implicit in the new one. If the operation is declared in the
6307 -- body it is not primitive and all formals must match.
6309 if Is_Concurrent_Type (Scope (Subp))
6310 and then Is_Tagged_Type (Scope (Subp))
6311 and then not Has_Completion (Scope (Subp))
6313 Form2 := Next_Formal (Form2);
6316 if Present (Form1) then
6317 Form1 := Next_Formal (Form1);
6318 Form2 := Next_Formal (Form2);
6321 while Present (Form1) loop
6322 if not Is_Controlling_Formal (Form1)
6323 and then Present (Next_Formal (Form2))
6324 and then Chars (Form1) = Chars (Next_Formal (Form2))
6326 Error_Msg_Node_2 := Alias (Overridden_Subp);
6327 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6329 ("& does not match corresponding formal of&#",
6334 Next_Formal (Form1);
6335 Next_Formal (Form2);
6340 -- If there is an overridden subprogram, then check that there is no
6341 -- "not overriding" indicator, and mark the subprogram as overriding.
6342 -- This is not done if the overridden subprogram is marked as hidden,
6343 -- which can occur for the case of inherited controlled operations
6344 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6345 -- subprogram is not itself hidden. (Note: This condition could probably
6346 -- be simplified, leaving out the testing for the specific controlled
6347 -- cases, but it seems safer and clearer this way, and echoes similar
6348 -- special-case tests of this kind in other places.)
6350 if Present (Overridden_Subp)
6351 and then (not Is_Hidden (Overridden_Subp)
6353 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6356 and then Present (Alias (Overridden_Subp))
6357 and then not Is_Hidden (Alias (Overridden_Subp))))
6359 if Must_Not_Override (Spec) then
6360 Error_Msg_Sloc := Sloc (Overridden_Subp);
6362 if Ekind (Subp) = E_Entry then
6364 ("entry & overrides inherited operation #", Spec, Subp);
6367 ("subprogram & overrides inherited operation #", Spec, Subp);
6370 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6371 -- as an extension of Root_Controlled, and thus has a useless Adjust
6372 -- operation. This operation should not be inherited by other limited
6373 -- controlled types. An explicit Adjust for them is not overriding.
6375 elsif Must_Override (Spec)
6376 and then Chars (Overridden_Subp) = Name_Adjust
6377 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6378 and then Present (Alias (Overridden_Subp))
6379 and then In_Predefined_Unit (Alias (Overridden_Subp))
6382 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6383 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6385 elsif Is_Subprogram (Subp) then
6386 if Is_Init_Proc (Subp) then
6389 elsif No (Overridden_Operation (Subp)) then
6391 -- For entities generated by Derive_Subprograms the overridden
6392 -- operation is the inherited primitive (which is available
6393 -- through the attribute alias)
6395 if (Is_Dispatching_Operation (Subp)
6396 or else Is_Dispatching_Operation (Overridden_Subp))
6397 and then not Comes_From_Source (Overridden_Subp)
6398 and then Find_Dispatching_Type (Overridden_Subp) =
6399 Find_Dispatching_Type (Subp)
6400 and then Present (Alias (Overridden_Subp))
6401 and then Comes_From_Source (Alias (Overridden_Subp))
6403 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6404 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6407 Set_Overridden_Operation (Subp, Overridden_Subp);
6408 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6413 -- If primitive flag is set or this is a protected operation, then
6414 -- the operation is overriding at the point of its declaration, so
6415 -- warn if necessary. Otherwise it may have been declared before the
6416 -- operation it overrides and no check is required.
6419 and then not Must_Override (Spec)
6420 and then (Is_Primitive
6421 or else Ekind (Scope (Subp)) = E_Protected_Type)
6423 Style.Missing_Overriding (Decl, Subp);
6426 -- If Subp is an operator, it may override a predefined operation, if
6427 -- it is defined in the same scope as the type to which it applies.
6428 -- In that case Overridden_Subp is empty because of our implicit
6429 -- representation for predefined operators. We have to check whether the
6430 -- signature of Subp matches that of a predefined operator. Note that
6431 -- first argument provides the name of the operator, and the second
6432 -- argument the signature that may match that of a standard operation.
6433 -- If the indicator is overriding, then the operator must match a
6434 -- predefined signature, because we know already that there is no
6435 -- explicit overridden operation.
6437 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6438 if Must_Not_Override (Spec) then
6440 -- If this is not a primitive or a protected subprogram, then
6441 -- "not overriding" is illegal.
6444 and then Ekind (Scope (Subp)) /= E_Protected_Type
6446 Error_Msg_N ("overriding indicator only allowed "
6447 & "if subprogram is primitive", Subp);
6449 elsif Can_Override_Operator (Subp) then
6451 ("subprogram& overrides predefined operator ", Spec, Subp);
6454 elsif Must_Override (Spec) then
6455 if No (Overridden_Operation (Subp))
6456 and then not Can_Override_Operator (Subp)
6458 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6461 elsif not Error_Posted (Subp)
6462 and then Style_Check
6463 and then Can_Override_Operator (Subp)
6464 and then not In_Predefined_Unit (Subp)
6466 -- If style checks are enabled, indicate that the indicator is
6467 -- missing. However, at the point of declaration, the type of
6468 -- which this is a primitive operation may be private, in which
6469 -- case the indicator would be premature.
6471 if Has_Private_Declaration (Etype (Subp))
6472 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6476 Style.Missing_Overriding (Decl, Subp);
6480 elsif Must_Override (Spec) then
6481 if Ekind (Subp) = E_Entry then
6482 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6484 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6487 -- If the operation is marked "not overriding" and it's not primitive
6488 -- then an error is issued, unless this is an operation of a task or
6489 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6490 -- has been specified have already been checked above.
6492 elsif Must_Not_Override (Spec)
6493 and then not Is_Primitive
6494 and then Ekind (Subp) /= E_Entry
6495 and then Ekind (Scope (Subp)) /= E_Protected_Type
6498 ("overriding indicator only allowed if subprogram is primitive",
6502 end Check_Overriding_Indicator;
6508 -- Note: this procedure needs to know far too much about how the expander
6509 -- messes with exceptions. The use of the flag Exception_Junk and the
6510 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6511 -- works, but is not very clean. It would be better if the expansion
6512 -- routines would leave Original_Node working nicely, and we could use
6513 -- Original_Node here to ignore all the peculiar expander messing ???
6515 procedure Check_Returns
6519 Proc : Entity_Id := Empty)
6523 procedure Check_Statement_Sequence (L : List_Id);
6524 -- Internal recursive procedure to check a list of statements for proper
6525 -- termination by a return statement (or a transfer of control or a
6526 -- compound statement that is itself internally properly terminated).
6528 ------------------------------
6529 -- Check_Statement_Sequence --
6530 ------------------------------
6532 procedure Check_Statement_Sequence (L : List_Id) is
6537 function Assert_False return Boolean;
6538 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6539 -- rewritten as a null statement when assertions are off. The assert
6540 -- is not active, but it is still enough to kill the warning.
6546 function Assert_False return Boolean is
6547 Orig : constant Node_Id := Original_Node (Last_Stm);
6550 if Nkind (Orig) = N_Pragma
6551 and then Pragma_Name (Orig) = Name_Assert
6552 and then not Error_Posted (Orig)
6555 Arg : constant Node_Id :=
6556 First (Pragma_Argument_Associations (Orig));
6557 Exp : constant Node_Id := Expression (Arg);
6559 return Nkind (Exp) = N_Identifier
6560 and then Chars (Exp) = Name_False;
6570 Raise_Exception_Call : Boolean;
6571 -- Set True if statement sequence terminated by Raise_Exception call
6572 -- or a Reraise_Occurrence call.
6574 -- Start of processing for Check_Statement_Sequence
6577 Raise_Exception_Call := False;
6579 -- Get last real statement
6581 Last_Stm := Last (L);
6583 -- Deal with digging out exception handler statement sequences that
6584 -- have been transformed by the local raise to goto optimization.
6585 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6586 -- optimization has occurred, we are looking at something like:
6589 -- original stmts in block
6593 -- goto L1; | omitted if No_Exception_Propagation
6598 -- goto L3; -- skip handler when exception not raised
6600 -- <<L1>> -- target label for local exception
6614 -- and what we have to do is to dig out the estmts1 and estmts2
6615 -- sequences (which were the original sequences of statements in
6616 -- the exception handlers) and check them.
6618 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6623 exit when Nkind (Stm) /= N_Block_Statement;
6624 exit when not Exception_Junk (Stm);
6627 exit when Nkind (Stm) /= N_Label;
6628 exit when not Exception_Junk (Stm);
6629 Check_Statement_Sequence
6630 (Statements (Handled_Statement_Sequence (Next (Stm))));
6635 exit when Nkind (Stm) /= N_Goto_Statement;
6636 exit when not Exception_Junk (Stm);
6640 -- Don't count pragmas
6642 while Nkind (Last_Stm) = N_Pragma
6644 -- Don't count call to SS_Release (can happen after Raise_Exception)
6647 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6649 Nkind (Name (Last_Stm)) = N_Identifier
6651 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6653 -- Don't count exception junk
6656 (Nkind_In (Last_Stm, N_Goto_Statement,
6658 N_Object_Declaration)
6659 and then Exception_Junk (Last_Stm))
6660 or else Nkind (Last_Stm) in N_Push_xxx_Label
6661 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6663 -- Inserted code, such as finalization calls, is irrelevant: we only
6664 -- need to check original source.
6666 or else Is_Rewrite_Insertion (Last_Stm)
6671 -- Here we have the "real" last statement
6673 Kind := Nkind (Last_Stm);
6675 -- Transfer of control, OK. Note that in the No_Return procedure
6676 -- case, we already diagnosed any explicit return statements, so
6677 -- we can treat them as OK in this context.
6679 if Is_Transfer (Last_Stm) then
6682 -- Check cases of explicit non-indirect procedure calls
6684 elsif Kind = N_Procedure_Call_Statement
6685 and then Is_Entity_Name (Name (Last_Stm))
6687 -- Check call to Raise_Exception procedure which is treated
6688 -- specially, as is a call to Reraise_Occurrence.
6690 -- We suppress the warning in these cases since it is likely that
6691 -- the programmer really does not expect to deal with the case
6692 -- of Null_Occurrence, and thus would find a warning about a
6693 -- missing return curious, and raising Program_Error does not
6694 -- seem such a bad behavior if this does occur.
6696 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6697 -- behavior will be to raise Constraint_Error (see AI-329).
6699 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6701 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6703 Raise_Exception_Call := True;
6705 -- For Raise_Exception call, test first argument, if it is
6706 -- an attribute reference for a 'Identity call, then we know
6707 -- that the call cannot possibly return.
6710 Arg : constant Node_Id :=
6711 Original_Node (First_Actual (Last_Stm));
6713 if Nkind (Arg) = N_Attribute_Reference
6714 and then Attribute_Name (Arg) = Name_Identity
6721 -- If statement, need to look inside if there is an else and check
6722 -- each constituent statement sequence for proper termination.
6724 elsif Kind = N_If_Statement
6725 and then Present (Else_Statements (Last_Stm))
6727 Check_Statement_Sequence (Then_Statements (Last_Stm));
6728 Check_Statement_Sequence (Else_Statements (Last_Stm));
6730 if Present (Elsif_Parts (Last_Stm)) then
6732 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6735 while Present (Elsif_Part) loop
6736 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6744 -- Case statement, check each case for proper termination
6746 elsif Kind = N_Case_Statement then
6750 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6751 while Present (Case_Alt) loop
6752 Check_Statement_Sequence (Statements (Case_Alt));
6753 Next_Non_Pragma (Case_Alt);
6759 -- Block statement, check its handled sequence of statements
6761 elsif Kind = N_Block_Statement then
6767 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6776 -- Loop statement. If there is an iteration scheme, we can definitely
6777 -- fall out of the loop. Similarly if there is an exit statement, we
6778 -- can fall out. In either case we need a following return.
6780 elsif Kind = N_Loop_Statement then
6781 if Present (Iteration_Scheme (Last_Stm))
6782 or else Has_Exit (Entity (Identifier (Last_Stm)))
6786 -- A loop with no exit statement or iteration scheme is either
6787 -- an infinite loop, or it has some other exit (raise/return).
6788 -- In either case, no warning is required.
6794 -- Timed entry call, check entry call and delay alternatives
6796 -- Note: in expanded code, the timed entry call has been converted
6797 -- to a set of expanded statements on which the check will work
6798 -- correctly in any case.
6800 elsif Kind = N_Timed_Entry_Call then
6802 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6803 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6806 -- If statement sequence of entry call alternative is missing,
6807 -- then we can definitely fall through, and we post the error
6808 -- message on the entry call alternative itself.
6810 if No (Statements (ECA)) then
6813 -- If statement sequence of delay alternative is missing, then
6814 -- we can definitely fall through, and we post the error
6815 -- message on the delay alternative itself.
6817 -- Note: if both ECA and DCA are missing the return, then we
6818 -- post only one message, should be enough to fix the bugs.
6819 -- If not we will get a message next time on the DCA when the
6822 elsif No (Statements (DCA)) then
6825 -- Else check both statement sequences
6828 Check_Statement_Sequence (Statements (ECA));
6829 Check_Statement_Sequence (Statements (DCA));
6834 -- Conditional entry call, check entry call and else part
6836 -- Note: in expanded code, the conditional entry call has been
6837 -- converted to a set of expanded statements on which the check
6838 -- will work correctly in any case.
6840 elsif Kind = N_Conditional_Entry_Call then
6842 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6845 -- If statement sequence of entry call alternative is missing,
6846 -- then we can definitely fall through, and we post the error
6847 -- message on the entry call alternative itself.
6849 if No (Statements (ECA)) then
6852 -- Else check statement sequence and else part
6855 Check_Statement_Sequence (Statements (ECA));
6856 Check_Statement_Sequence (Else_Statements (Last_Stm));
6862 -- If we fall through, issue appropriate message
6866 -- Kill warning if last statement is a raise exception call,
6867 -- or a pragma Assert (False). Note that with assertions enabled,
6868 -- such a pragma has been converted into a raise exception call
6869 -- already, so the Assert_False is for the assertions off case.
6871 if not Raise_Exception_Call and then not Assert_False then
6873 -- In GNATprove mode, it is an error to have a missing return
6875 Error_Msg_Warn := SPARK_Mode /= On;
6877 -- Issue error message or warning
6880 ("RETURN statement missing following this statement<<!",
6883 ("\Program_Error ]<<!", Last_Stm);
6886 -- Note: we set Err even though we have not issued a warning
6887 -- because we still have a case of a missing return. This is
6888 -- an extremely marginal case, probably will never be noticed
6889 -- but we might as well get it right.
6893 -- Otherwise we have the case of a procedure marked No_Return
6896 if not Raise_Exception_Call then
6897 if GNATprove_Mode then
6899 ("implied return after this statement would have raised "
6900 & "Program_Error", Last_Stm);
6902 -- In normal compilation mode, do not warn on a generated call
6903 -- (e.g. in the body of a renaming as completion).
6905 elsif Comes_From_Source (Last_Stm) then
6907 ("implied return after this statement will raise "
6908 & "Program_Error??", Last_Stm);
6911 Error_Msg_Warn := SPARK_Mode /= On;
6913 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6917 RE : constant Node_Id :=
6918 Make_Raise_Program_Error (Sloc (Last_Stm),
6919 Reason => PE_Implicit_Return);
6921 Insert_After (Last_Stm, RE);
6925 end Check_Statement_Sequence;
6927 -- Start of processing for Check_Returns
6931 Check_Statement_Sequence (Statements (HSS));
6933 if Present (Exception_Handlers (HSS)) then
6934 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6935 while Present (Handler) loop
6936 Check_Statement_Sequence (Statements (Handler));
6937 Next_Non_Pragma (Handler);
6942 ----------------------------
6943 -- Check_Subprogram_Order --
6944 ----------------------------
6946 procedure Check_Subprogram_Order (N : Node_Id) is
6948 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6949 -- This is used to check if S1 > S2 in the sense required by this test,
6950 -- for example nameab < namec, but name2 < name10.
6952 -----------------------------
6953 -- Subprogram_Name_Greater --
6954 -----------------------------
6956 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6961 -- Deal with special case where names are identical except for a
6962 -- numerical suffix. These are handled specially, taking the numeric
6963 -- ordering from the suffix into account.
6966 while S1 (L1) in '0' .. '9' loop
6971 while S2 (L2) in '0' .. '9' loop
6975 -- If non-numeric parts non-equal, do straight compare
6977 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6980 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6981 -- that a missing suffix is treated as numeric zero in this test.
6985 while L1 < S1'Last loop
6987 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6991 while L2 < S2'Last loop
6993 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6998 end Subprogram_Name_Greater;
7000 -- Start of processing for Check_Subprogram_Order
7003 -- Check body in alpha order if this is option
7006 and then Style_Check_Order_Subprograms
7007 and then Nkind (N) = N_Subprogram_Body
7008 and then Comes_From_Source (N)
7009 and then In_Extended_Main_Source_Unit (N)
7013 renames Scope_Stack.Table
7014 (Scope_Stack.Last).Last_Subprogram_Name;
7016 Body_Id : constant Entity_Id :=
7017 Defining_Entity (Specification (N));
7020 Get_Decoded_Name_String (Chars (Body_Id));
7023 if Subprogram_Name_Greater
7024 (LSN.all, Name_Buffer (1 .. Name_Len))
7026 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7032 LSN := new String'(Name_Buffer (1 .. Name_Len));
7035 end Check_Subprogram_Order;
7037 ------------------------------
7038 -- Check_Subtype_Conformant --
7039 ------------------------------
7041 procedure Check_Subtype_Conformant
7042 (New_Id : Entity_Id;
7044 Err_Loc : Node_Id := Empty;
7045 Skip_Controlling_Formals : Boolean := False;
7046 Get_Inst : Boolean := False)
7049 pragma Warnings (Off, Result);
7052 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7053 Skip_Controlling_Formals => Skip_Controlling_Formals,
7054 Get_Inst => Get_Inst);
7055 end Check_Subtype_Conformant;
7057 -----------------------------------
7058 -- Check_Synchronized_Overriding --
7059 -----------------------------------
7061 procedure Check_Synchronized_Overriding
7062 (Def_Id : Entity_Id;
7063 Overridden_Subp : out Entity_Id)
7065 Ifaces_List : Elist_Id;
7069 function Matches_Prefixed_View_Profile
7070 (Prim_Params : List_Id;
7071 Iface_Params : List_Id) return Boolean;
7072 -- Determine whether a subprogram's parameter profile Prim_Params
7073 -- matches that of a potentially overridden interface subprogram
7074 -- Iface_Params. Also determine if the type of first parameter of
7075 -- Iface_Params is an implemented interface.
7077 -----------------------------------
7078 -- Matches_Prefixed_View_Profile --
7079 -----------------------------------
7081 function Matches_Prefixed_View_Profile
7082 (Prim_Params : List_Id;
7083 Iface_Params : List_Id) return Boolean
7085 function Is_Implemented
7086 (Ifaces_List : Elist_Id;
7087 Iface : Entity_Id) return Boolean;
7088 -- Determine if Iface is implemented by the current task or
7091 --------------------
7092 -- Is_Implemented --
7093 --------------------
7095 function Is_Implemented
7096 (Ifaces_List : Elist_Id;
7097 Iface : Entity_Id) return Boolean
7099 Iface_Elmt : Elmt_Id;
7102 Iface_Elmt := First_Elmt (Ifaces_List);
7103 while Present (Iface_Elmt) loop
7104 if Node (Iface_Elmt) = Iface then
7108 Next_Elmt (Iface_Elmt);
7116 Iface_Id : Entity_Id;
7117 Iface_Param : Node_Id;
7118 Iface_Typ : Entity_Id;
7119 Prim_Id : Entity_Id;
7120 Prim_Param : Node_Id;
7121 Prim_Typ : Entity_Id;
7123 -- Start of processing for Matches_Prefixed_View_Profile
7126 Iface_Param := First (Iface_Params);
7127 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7129 if Is_Access_Type (Iface_Typ) then
7130 Iface_Typ := Designated_Type (Iface_Typ);
7133 Prim_Param := First (Prim_Params);
7135 -- The first parameter of the potentially overridden subprogram must
7136 -- be an interface implemented by Prim.
7138 if not Is_Interface (Iface_Typ)
7139 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7144 -- The checks on the object parameters are done, so move on to the
7145 -- rest of the parameters.
7147 if not In_Scope then
7148 Prim_Param := Next (Prim_Param);
7151 Iface_Param := Next (Iface_Param);
7152 while Present (Iface_Param) and then Present (Prim_Param) loop
7153 Iface_Id := Defining_Identifier (Iface_Param);
7154 Iface_Typ := Find_Parameter_Type (Iface_Param);
7156 Prim_Id := Defining_Identifier (Prim_Param);
7157 Prim_Typ := Find_Parameter_Type (Prim_Param);
7159 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7160 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7161 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7163 Iface_Typ := Designated_Type (Iface_Typ);
7164 Prim_Typ := Designated_Type (Prim_Typ);
7167 -- Case of multiple interface types inside a parameter profile
7169 -- (Obj_Param : in out Iface; ...; Param : Iface)
7171 -- If the interface type is implemented, then the matching type in
7172 -- the primitive should be the implementing record type.
7174 if Ekind (Iface_Typ) = E_Record_Type
7175 and then Is_Interface (Iface_Typ)
7176 and then Is_Implemented (Ifaces_List, Iface_Typ)
7178 if Prim_Typ /= Typ then
7182 -- The two parameters must be both mode and subtype conformant
7184 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7186 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7195 -- One of the two lists contains more parameters than the other
7197 if Present (Iface_Param) or else Present (Prim_Param) then
7202 end Matches_Prefixed_View_Profile;
7204 -- Start of processing for Check_Synchronized_Overriding
7207 Overridden_Subp := Empty;
7209 -- Def_Id must be an entry or a subprogram. We should skip predefined
7210 -- primitives internally generated by the front end; however at this
7211 -- stage predefined primitives are still not fully decorated. As a
7212 -- minor optimization we skip here internally generated subprograms.
7214 if (Ekind (Def_Id) /= E_Entry
7215 and then Ekind (Def_Id) /= E_Function
7216 and then Ekind (Def_Id) /= E_Procedure)
7217 or else not Comes_From_Source (Def_Id)
7222 -- Search for the concurrent declaration since it contains the list of
7223 -- all implemented interfaces. In this case, the subprogram is declared
7224 -- within the scope of a protected or a task type.
7226 if Present (Scope (Def_Id))
7227 and then Is_Concurrent_Type (Scope (Def_Id))
7228 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7230 Typ := Scope (Def_Id);
7233 -- The enclosing scope is not a synchronized type and the subprogram
7236 elsif No (First_Formal (Def_Id)) then
7239 -- The subprogram has formals and hence it may be a primitive of a
7243 Typ := Etype (First_Formal (Def_Id));
7245 if Is_Access_Type (Typ) then
7246 Typ := Directly_Designated_Type (Typ);
7249 if Is_Concurrent_Type (Typ)
7250 and then not Is_Generic_Actual_Type (Typ)
7254 -- This case occurs when the concurrent type is declared within a
7255 -- generic unit. As a result the corresponding record has been built
7256 -- and used as the type of the first formal, we just have to retrieve
7257 -- the corresponding concurrent type.
7259 elsif Is_Concurrent_Record_Type (Typ)
7260 and then not Is_Class_Wide_Type (Typ)
7261 and then Present (Corresponding_Concurrent_Type (Typ))
7263 Typ := Corresponding_Concurrent_Type (Typ);
7271 -- There is no overriding to check if this is an inherited operation in
7272 -- a type derivation for a generic actual.
7274 Collect_Interfaces (Typ, Ifaces_List);
7276 if Is_Empty_Elmt_List (Ifaces_List) then
7280 -- Determine whether entry or subprogram Def_Id overrides a primitive
7281 -- operation that belongs to one of the interfaces in Ifaces_List.
7284 Candidate : Entity_Id := Empty;
7285 Hom : Entity_Id := Empty;
7286 Subp : Entity_Id := Empty;
7289 -- Traverse the homonym chain, looking for a potentially overridden
7290 -- subprogram that belongs to an implemented interface.
7292 Hom := Current_Entity_In_Scope (Def_Id);
7293 while Present (Hom) loop
7297 or else not Is_Overloadable (Subp)
7298 or else not Is_Primitive (Subp)
7299 or else not Is_Dispatching_Operation (Subp)
7300 or else not Present (Find_Dispatching_Type (Subp))
7301 or else not Is_Interface (Find_Dispatching_Type (Subp))
7305 -- Entries and procedures can override abstract or null interface
7308 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7309 and then Ekind (Subp) = E_Procedure
7310 and then Matches_Prefixed_View_Profile
7311 (Parameter_Specifications (Parent (Def_Id)),
7312 Parameter_Specifications (Parent (Subp)))
7316 -- For an overridden subprogram Subp, check whether the mode
7317 -- of its first parameter is correct depending on the kind of
7318 -- synchronized type.
7321 Formal : constant Node_Id := First_Formal (Candidate);
7324 -- In order for an entry or a protected procedure to
7325 -- override, the first parameter of the overridden routine
7326 -- must be of mode "out", "in out", or access-to-variable.
7328 if Ekind_In (Candidate, E_Entry, E_Procedure)
7329 and then Is_Protected_Type (Typ)
7330 and then Ekind (Formal) /= E_In_Out_Parameter
7331 and then Ekind (Formal) /= E_Out_Parameter
7332 and then Nkind (Parameter_Type (Parent (Formal))) /=
7337 -- All other cases are OK since a task entry or routine does
7338 -- not have a restriction on the mode of the first parameter
7339 -- of the overridden interface routine.
7342 Overridden_Subp := Candidate;
7347 -- Functions can override abstract interface functions
7349 elsif Ekind (Def_Id) = E_Function
7350 and then Ekind (Subp) = E_Function
7351 and then Matches_Prefixed_View_Profile
7352 (Parameter_Specifications (Parent (Def_Id)),
7353 Parameter_Specifications (Parent (Subp)))
7354 and then Etype (Def_Id) = Etype (Subp)
7358 -- If an inherited subprogram is implemented by a protected
7359 -- function, then the first parameter of the inherited
7360 -- subprogram shall be of mode in, but not an access-to-
7361 -- variable parameter (RM 9.4(11/9)).
7363 if Present (First_Formal (Subp))
7364 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7366 (not Is_Access_Type (Etype (First_Formal (Subp)))
7368 Is_Access_Constant (Etype (First_Formal (Subp))))
7370 Overridden_Subp := Subp;
7375 Hom := Homonym (Hom);
7378 -- After examining all candidates for overriding, we are left with
7379 -- the best match, which is a mode-incompatible interface routine.
7381 if In_Scope and then Present (Candidate) then
7382 Error_Msg_PT (Def_Id, Candidate);
7385 Overridden_Subp := Candidate;
7388 end Check_Synchronized_Overriding;
7390 ---------------------------
7391 -- Check_Type_Conformant --
7392 ---------------------------
7394 procedure Check_Type_Conformant
7395 (New_Id : Entity_Id;
7397 Err_Loc : Node_Id := Empty)
7400 pragma Warnings (Off, Result);
7403 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7404 end Check_Type_Conformant;
7406 ---------------------------
7407 -- Can_Override_Operator --
7408 ---------------------------
7410 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7414 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7418 Typ := Base_Type (Etype (First_Formal (Subp)));
7420 -- Check explicitly that the operation is a primitive of the type
7422 return Operator_Matches_Spec (Subp, Subp)
7423 and then not Is_Generic_Type (Typ)
7424 and then Scope (Subp) = Scope (Typ)
7425 and then not Is_Class_Wide_Type (Typ);
7427 end Can_Override_Operator;
7429 ----------------------
7430 -- Conforming_Types --
7431 ----------------------
7433 function Conforming_Types
7436 Ctype : Conformance_Type;
7437 Get_Inst : Boolean := False) return Boolean
7439 function Base_Types_Match
7441 Typ_2 : Entity_Id) return Boolean;
7442 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7443 -- in different scopes (e.g. parent and child instances), then verify
7444 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7445 -- the same subtype chain. The whole purpose of this procedure is to
7446 -- prevent spurious ambiguities in an instantiation that may arise if
7447 -- two distinct generic types are instantiated with the same actual.
7449 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7450 -- An access parameter can designate an incomplete type. If the
7451 -- incomplete type is the limited view of a type from a limited_
7452 -- with_clause, check whether the non-limited view is available.
7453 -- If it is a (non-limited) incomplete type, get the full view.
7455 function Matches_Limited_With_View
7457 Typ_2 : Entity_Id) return Boolean;
7458 -- Returns True if and only if either Typ_1 denotes a limited view of
7459 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7460 -- the limited with view of a type is used in a subprogram declaration
7461 -- and the subprogram body is in the scope of a regular with clause for
7462 -- the same unit. In such a case, the two type entities are considered
7463 -- identical for purposes of conformance checking.
7465 ----------------------
7466 -- Base_Types_Match --
7467 ----------------------
7469 function Base_Types_Match
7471 Typ_2 : Entity_Id) return Boolean
7473 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7474 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7477 if Typ_1 = Typ_2 then
7480 elsif Base_1 = Base_2 then
7482 -- The following is too permissive. A more precise test should
7483 -- check that the generic actual is an ancestor subtype of the
7486 -- See code in Find_Corresponding_Spec that applies an additional
7487 -- filter to handle accidental amiguities in instances.
7490 not Is_Generic_Actual_Type (Typ_1)
7491 or else not Is_Generic_Actual_Type (Typ_2)
7492 or else Scope (Typ_1) /= Scope (Typ_2);
7494 -- If Typ_2 is a generic actual type it is declared as the subtype of
7495 -- the actual. If that actual is itself a subtype we need to use its
7496 -- own base type to check for compatibility.
7498 elsif Ekind (Base_2) = Ekind (Typ_2)
7499 and then Base_1 = Base_Type (Base_2)
7503 elsif Ekind (Base_1) = Ekind (Typ_1)
7504 and then Base_2 = Base_Type (Base_1)
7511 end Base_Types_Match;
7513 --------------------------
7514 -- Find_Designated_Type --
7515 --------------------------
7517 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7521 Desig := Directly_Designated_Type (Typ);
7523 if Ekind (Desig) = E_Incomplete_Type then
7525 -- If regular incomplete type, get full view if available
7527 if Present (Full_View (Desig)) then
7528 Desig := Full_View (Desig);
7530 -- If limited view of a type, get non-limited view if available,
7531 -- and check again for a regular incomplete type.
7533 elsif Present (Non_Limited_View (Desig)) then
7534 Desig := Get_Full_View (Non_Limited_View (Desig));
7539 end Find_Designated_Type;
7541 -------------------------------
7542 -- Matches_Limited_With_View --
7543 -------------------------------
7545 function Matches_Limited_With_View
7547 Typ_2 : Entity_Id) return Boolean
7549 function Is_Matching_Limited_View
7551 View : Entity_Id) return Boolean;
7552 -- Determine whether non-limited view View denotes type Typ in some
7553 -- conformant fashion.
7555 ------------------------------
7556 -- Is_Matching_Limited_View --
7557 ------------------------------
7559 function Is_Matching_Limited_View
7561 View : Entity_Id) return Boolean
7563 Root_Typ : Entity_Id;
7564 Root_View : Entity_Id;
7567 -- The non-limited view directly denotes the type
7572 -- The type is a subtype of the non-limited view
7574 elsif Is_Subtype_Of (Typ, View) then
7577 -- Both the non-limited view and the type denote class-wide types
7579 elsif Is_Class_Wide_Type (Typ)
7580 and then Is_Class_Wide_Type (View)
7582 Root_Typ := Root_Type (Typ);
7583 Root_View := Root_Type (View);
7585 if Root_Typ = Root_View then
7588 -- An incomplete tagged type and its full view may receive two
7589 -- distinct class-wide types when the related package has not
7590 -- been analyzed yet.
7593 -- type T is tagged; -- CW_1
7594 -- type T is tagged null record; -- CW_2
7597 -- This is because the package lacks any semantic information
7598 -- that may eventually link both views of T. As a consequence,
7599 -- a client of the limited view of Pack will see CW_2 while a
7600 -- client of the non-limited view of Pack will see CW_1.
7602 elsif Is_Incomplete_Type (Root_Typ)
7603 and then Present (Full_View (Root_Typ))
7604 and then Full_View (Root_Typ) = Root_View
7608 elsif Is_Incomplete_Type (Root_View)
7609 and then Present (Full_View (Root_View))
7610 and then Full_View (Root_View) = Root_Typ
7617 end Is_Matching_Limited_View;
7619 -- Start of processing for Matches_Limited_With_View
7622 -- In some cases a type imported through a limited_with clause, and
7623 -- its non-limited view are both visible, for example in an anonymous
7624 -- access-to-class-wide type in a formal, or when building the body
7625 -- for a subprogram renaming after the subprogram has been frozen.
7626 -- In these cases both entities designate the same type. In addition,
7627 -- if one of them is an actual in an instance, it may be a subtype of
7628 -- the non-limited view of the other.
7630 if From_Limited_With (Typ_1)
7631 and then From_Limited_With (Typ_2)
7632 and then Available_View (Typ_1) = Available_View (Typ_2)
7636 elsif From_Limited_With (Typ_1) then
7637 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7639 elsif From_Limited_With (Typ_2) then
7640 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7645 end Matches_Limited_With_View;
7649 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7651 Type_1 : Entity_Id := T1;
7652 Type_2 : Entity_Id := T2;
7654 -- Start of processing for Conforming_Types
7657 -- The context is an instance association for a formal access-to-
7658 -- subprogram type; the formal parameter types require mapping because
7659 -- they may denote other formal parameters of the generic unit.
7662 Type_1 := Get_Instance_Of (T1);
7663 Type_2 := Get_Instance_Of (T2);
7666 -- If one of the types is a view of the other introduced by a limited
7667 -- with clause, treat these as conforming for all purposes.
7669 if Matches_Limited_With_View (T1, T2) then
7672 elsif Base_Types_Match (Type_1, Type_2) then
7673 if Ctype <= Mode_Conformant then
7678 Subtypes_Statically_Match (Type_1, Type_2)
7679 and then Dimensions_Match (Type_1, Type_2);
7682 elsif Is_Incomplete_Or_Private_Type (Type_1)
7683 and then Present (Full_View (Type_1))
7684 and then Base_Types_Match (Full_View (Type_1), Type_2)
7687 Ctype <= Mode_Conformant
7688 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7690 elsif Ekind (Type_2) = E_Incomplete_Type
7691 and then Present (Full_View (Type_2))
7692 and then Base_Types_Match (Type_1, Full_View (Type_2))
7695 Ctype <= Mode_Conformant
7696 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7698 elsif Is_Private_Type (Type_2)
7699 and then In_Instance
7700 and then Present (Full_View (Type_2))
7701 and then Base_Types_Match (Type_1, Full_View (Type_2))
7704 Ctype <= Mode_Conformant
7705 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7707 -- Another confusion between views in a nested instance with an
7708 -- actual private type whose full view is not in scope.
7710 elsif Ekind (Type_2) = E_Private_Subtype
7711 and then In_Instance
7712 and then Etype (Type_2) = Type_1
7716 -- In Ada 2012, incomplete types (including limited views) can appear
7717 -- as actuals in instantiations, where they are conformant to the
7718 -- corresponding incomplete formal.
7720 elsif Is_Incomplete_Type (Type_1)
7721 and then Is_Incomplete_Type (Type_2)
7722 and then In_Instance
7723 and then (Used_As_Generic_Actual (Type_1)
7724 or else Used_As_Generic_Actual (Type_2))
7729 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7730 -- treated recursively because they carry a signature. As far as
7731 -- conformance is concerned, convention plays no role, and either
7732 -- or both could be access to protected subprograms.
7734 Are_Anonymous_Access_To_Subprogram_Types :=
7735 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7736 E_Anonymous_Access_Protected_Subprogram_Type)
7738 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7739 E_Anonymous_Access_Protected_Subprogram_Type);
7741 -- Test anonymous access type case. For this case, static subtype
7742 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7743 -- the base types because we may have built internal subtype entities
7744 -- to handle null-excluding types (see Process_Formals).
7746 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7748 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7750 -- Ada 2005 (AI-254)
7752 or else Are_Anonymous_Access_To_Subprogram_Types
7755 Desig_1 : Entity_Id;
7756 Desig_2 : Entity_Id;
7759 -- In Ada 2005, access constant indicators must match for
7760 -- subtype conformance.
7762 if Ada_Version >= Ada_2005
7763 and then Ctype >= Subtype_Conformant
7765 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7770 Desig_1 := Find_Designated_Type (Type_1);
7771 Desig_2 := Find_Designated_Type (Type_2);
7773 -- If the context is an instance association for a formal
7774 -- access-to-subprogram type; formal access parameter designated
7775 -- types require mapping because they may denote other formal
7776 -- parameters of the generic unit.
7779 Desig_1 := Get_Instance_Of (Desig_1);
7780 Desig_2 := Get_Instance_Of (Desig_2);
7783 -- It is possible for a Class_Wide_Type to be introduced for an
7784 -- incomplete type, in which case there is a separate class_ wide
7785 -- type for the full view. The types conform if their Etypes
7786 -- conform, i.e. one may be the full view of the other. This can
7787 -- only happen in the context of an access parameter, other uses
7788 -- of an incomplete Class_Wide_Type are illegal.
7790 if Is_Class_Wide_Type (Desig_1)
7792 Is_Class_Wide_Type (Desig_2)
7796 (Etype (Base_Type (Desig_1)),
7797 Etype (Base_Type (Desig_2)), Ctype);
7799 elsif Are_Anonymous_Access_To_Subprogram_Types then
7800 if Ada_Version < Ada_2005 then
7802 Ctype = Type_Conformant
7803 or else Subtypes_Statically_Match (Desig_1, Desig_2);
7805 -- We must check the conformance of the signatures themselves
7809 Conformant : Boolean;
7812 (Desig_1, Desig_2, Ctype, False, Conformant);
7817 -- A limited view of an actual matches the corresponding
7818 -- incomplete formal.
7820 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7821 and then From_Limited_With (Desig_2)
7822 and then Used_As_Generic_Actual (Etype (Desig_2))
7827 return Base_Type (Desig_1) = Base_Type (Desig_2)
7828 and then (Ctype = Type_Conformant
7830 Subtypes_Statically_Match (Desig_1, Desig_2));
7834 -- Otherwise definitely no match
7837 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7838 and then Is_Access_Type (Type_2))
7839 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7840 and then Is_Access_Type (Type_1)))
7843 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7845 May_Hide_Profile := True;
7850 end Conforming_Types;
7852 --------------------------
7853 -- Create_Extra_Formals --
7854 --------------------------
7856 procedure Create_Extra_Formals (E : Entity_Id) is
7857 First_Extra : Entity_Id := Empty;
7859 Last_Extra : Entity_Id := Empty;
7861 function Add_Extra_Formal
7862 (Assoc_Entity : Entity_Id;
7865 Suffix : String) return Entity_Id;
7866 -- Add an extra formal to the current list of formals and extra formals.
7867 -- The extra formal is added to the end of the list of extra formals,
7868 -- and also returned as the result. These formals are always of mode IN.
7869 -- The new formal has the type Typ, is declared in Scope, and its name
7870 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7871 -- The following suffixes are currently used. They should not be changed
7872 -- without coordinating with CodePeer, which makes use of these to
7873 -- provide better messages.
7875 -- O denotes the Constrained bit.
7876 -- L denotes the accessibility level.
7877 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7878 -- the full list in exp_ch6.BIP_Formal_Kind.
7880 ----------------------
7881 -- Add_Extra_Formal --
7882 ----------------------
7884 function Add_Extra_Formal
7885 (Assoc_Entity : Entity_Id;
7888 Suffix : String) return Entity_Id
7890 EF : constant Entity_Id :=
7891 Make_Defining_Identifier (Sloc (Assoc_Entity),
7892 Chars => New_External_Name (Chars (Assoc_Entity),
7896 -- A little optimization. Never generate an extra formal for the
7897 -- _init operand of an initialization procedure, since it could
7900 if Chars (Formal) = Name_uInit then
7904 Set_Ekind (EF, E_In_Parameter);
7905 Set_Actual_Subtype (EF, Typ);
7906 Set_Etype (EF, Typ);
7907 Set_Scope (EF, Scope);
7908 Set_Mechanism (EF, Default_Mechanism);
7909 Set_Formal_Validity (EF);
7911 if No (First_Extra) then
7913 Set_Extra_Formals (Scope, EF);
7916 if Present (Last_Extra) then
7917 Set_Extra_Formal (Last_Extra, EF);
7923 end Add_Extra_Formal;
7927 Formal_Type : Entity_Id;
7928 P_Formal : Entity_Id := Empty;
7930 -- Start of processing for Create_Extra_Formals
7933 -- We never generate extra formals if expansion is not active because we
7934 -- don't need them unless we are generating code.
7936 if not Expander_Active then
7940 -- No need to generate extra formals in interface thunks whose target
7941 -- primitive has no extra formals.
7943 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7947 -- If this is a derived subprogram then the subtypes of the parent
7948 -- subprogram's formal parameters will be used to determine the need
7949 -- for extra formals.
7951 if Is_Overloadable (E) and then Present (Alias (E)) then
7952 P_Formal := First_Formal (Alias (E));
7955 Formal := First_Formal (E);
7956 while Present (Formal) loop
7957 Last_Extra := Formal;
7958 Next_Formal (Formal);
7961 -- If Extra_Formals were already created, don't do it again. This
7962 -- situation may arise for subprogram types created as part of
7963 -- dispatching calls (see Expand_Dispatching_Call).
7965 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7969 -- If the subprogram is a predefined dispatching subprogram then don't
7970 -- generate any extra constrained or accessibility level formals. In
7971 -- general we suppress these for internal subprograms (by not calling
7972 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7973 -- generated stream attributes do get passed through because extra
7974 -- build-in-place formals are needed in some cases (limited 'Input).
7976 if Is_Predefined_Internal_Operation (E) then
7977 goto Test_For_Func_Result_Extras;
7980 Formal := First_Formal (E);
7981 while Present (Formal) loop
7983 -- Create extra formal for supporting the attribute 'Constrained.
7984 -- The case of a private type view without discriminants also
7985 -- requires the extra formal if the underlying type has defaulted
7988 if Ekind (Formal) /= E_In_Parameter then
7989 if Present (P_Formal) then
7990 Formal_Type := Etype (P_Formal);
7992 Formal_Type := Etype (Formal);
7995 -- Do not produce extra formals for Unchecked_Union parameters.
7996 -- Jump directly to the end of the loop.
7998 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7999 goto Skip_Extra_Formal_Generation;
8002 if not Has_Discriminants (Formal_Type)
8003 and then Ekind (Formal_Type) in Private_Kind
8004 and then Present (Underlying_Type (Formal_Type))
8006 Formal_Type := Underlying_Type (Formal_Type);
8009 -- Suppress the extra formal if formal's subtype is constrained or
8010 -- indefinite, or we're compiling for Ada 2012 and the underlying
8011 -- type is tagged and limited. In Ada 2012, a limited tagged type
8012 -- can have defaulted discriminants, but 'Constrained is required
8013 -- to return True, so the formal is never needed (see AI05-0214).
8014 -- Note that this ensures consistency of calling sequences for
8015 -- dispatching operations when some types in a class have defaults
8016 -- on discriminants and others do not (and requiring the extra
8017 -- formal would introduce distributed overhead).
8019 -- If the type does not have a completion yet, treat as prior to
8020 -- Ada 2012 for consistency.
8022 if Has_Discriminants (Formal_Type)
8023 and then not Is_Constrained (Formal_Type)
8024 and then Is_Definite_Subtype (Formal_Type)
8025 and then (Ada_Version < Ada_2012
8026 or else No (Underlying_Type (Formal_Type))
8028 (Is_Limited_Type (Formal_Type)
8031 (Underlying_Type (Formal_Type)))))
8033 Set_Extra_Constrained
8034 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8038 -- Create extra formal for supporting accessibility checking. This
8039 -- is done for both anonymous access formals and formals of named
8040 -- access types that are marked as controlling formals. The latter
8041 -- case can occur when Expand_Dispatching_Call creates a subprogram
8042 -- type and substitutes the types of access-to-class-wide actuals
8043 -- for the anonymous access-to-specific-type of controlling formals.
8044 -- Base_Type is applied because in cases where there is a null
8045 -- exclusion the formal may have an access subtype.
8047 -- This is suppressed if we specifically suppress accessibility
8048 -- checks at the package level for either the subprogram, or the
8049 -- package in which it resides. However, we do not suppress it
8050 -- simply if the scope has accessibility checks suppressed, since
8051 -- this could cause trouble when clients are compiled with a
8052 -- different suppression setting. The explicit checks at the
8053 -- package level are safe from this point of view.
8055 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8056 or else (Is_Controlling_Formal (Formal)
8057 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8059 (Explicit_Suppress (E, Accessibility_Check)
8061 Explicit_Suppress (Scope (E), Accessibility_Check))
8064 or else Present (Extra_Accessibility (P_Formal)))
8066 Set_Extra_Accessibility
8067 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8070 -- This label is required when skipping extra formal generation for
8071 -- Unchecked_Union parameters.
8073 <<Skip_Extra_Formal_Generation>>
8075 if Present (P_Formal) then
8076 Next_Formal (P_Formal);
8079 Next_Formal (Formal);
8082 <<Test_For_Func_Result_Extras>>
8084 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8085 -- function call is ... determined by the point of call ...".
8087 if Needs_Result_Accessibility_Level (E) then
8088 Set_Extra_Accessibility_Of_Result
8089 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8092 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8093 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8095 if Is_Build_In_Place_Function (E) then
8097 Result_Subt : constant Entity_Id := Etype (E);
8098 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8099 Formal_Typ : Entity_Id;
8100 Subp_Decl : Node_Id;
8101 Discard : Entity_Id;
8104 -- In the case of functions with unconstrained result subtypes,
8105 -- add a 4-state formal indicating whether the return object is
8106 -- allocated by the caller (1), or should be allocated by the
8107 -- callee on the secondary stack (2), in the global heap (3), or
8108 -- in a user-defined storage pool (4). For the moment we just use
8109 -- Natural for the type of this formal. Note that this formal
8110 -- isn't usually needed in the case where the result subtype is
8111 -- constrained, but it is needed when the function has a tagged
8112 -- result, because generally such functions can be called in a
8113 -- dispatching context and such calls must be handled like calls
8114 -- to a class-wide function.
8116 if Needs_BIP_Alloc_Form (E) then
8119 (E, Standard_Natural,
8120 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8122 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8123 -- use a user-defined pool. This formal is not added on
8124 -- ZFP as those targets do not support pools.
8126 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8129 (E, RTE (RE_Root_Storage_Pool_Ptr),
8130 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8134 -- In the case of functions whose result type needs finalization,
8135 -- add an extra formal which represents the finalization master.
8137 if Needs_BIP_Finalization_Master (E) then
8140 (E, RTE (RE_Finalization_Master_Ptr),
8141 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8144 -- When the result type contains tasks, add two extra formals: the
8145 -- master of the tasks to be created, and the caller's activation
8148 if Has_Task (Full_Subt) then
8151 (E, RTE (RE_Master_Id),
8152 E, BIP_Formal_Suffix (BIP_Task_Master));
8155 (E, RTE (RE_Activation_Chain_Access),
8156 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8159 -- All build-in-place functions get an extra formal that will be
8160 -- passed the address of the return object within the caller.
8163 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8165 -- Incomplete_View_From_Limited_With is needed here because
8166 -- gigi gets confused if the designated type is the full view
8167 -- coming from a limited-with'ed package. In the normal case,
8168 -- (no limited with) Incomplete_View_From_Limited_With
8169 -- returns Result_Subt.
8171 Set_Directly_Designated_Type
8172 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8173 Set_Etype (Formal_Typ, Formal_Typ);
8174 Set_Depends_On_Private
8175 (Formal_Typ, Has_Private_Component (Formal_Typ));
8176 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8177 Set_Is_Access_Constant (Formal_Typ, False);
8179 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8180 -- the designated type comes from the limited view (for back-end
8183 Set_From_Limited_With
8184 (Formal_Typ, From_Limited_With (Result_Subt));
8186 Layout_Type (Formal_Typ);
8188 -- Force the definition of the Itype in case of internal function
8189 -- calls within the same or nested scope.
8191 if Is_Subprogram_Or_Generic_Subprogram (E) then
8192 Subp_Decl := Parent (E);
8194 -- The insertion point for an Itype reference should be after
8195 -- the unit declaration node of the subprogram. An exception
8196 -- to this are inherited operations from a parent type in which
8197 -- case the derived type acts as their parent.
8199 if Nkind_In (Subp_Decl, N_Function_Specification,
8200 N_Procedure_Specification)
8202 Subp_Decl := Parent (Subp_Decl);
8205 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8210 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8214 -- If this is an instance of a generic, we need to have extra formals
8217 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8218 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8220 end Create_Extra_Formals;
8222 -----------------------------
8223 -- Enter_Overloaded_Entity --
8224 -----------------------------
8226 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8227 function Matches_Predefined_Op return Boolean;
8228 -- This returns an approximation of whether S matches a predefined
8229 -- operator, based on the operator symbol, and the parameter and result
8230 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8232 ---------------------------
8233 -- Matches_Predefined_Op --
8234 ---------------------------
8236 function Matches_Predefined_Op return Boolean is
8237 Formal_1 : constant Entity_Id := First_Formal (S);
8238 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8239 Op : constant Name_Id := Chars (S);
8240 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8241 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8246 if Present (Formal_2) then
8248 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8251 -- All but "&" and "**" have same-types parameters
8260 if Type_1 /= Type_2 then
8265 -- Check parameter and result types
8273 Is_Boolean_Type (Result_Type)
8274 and then Result_Type = Type_1;
8280 Is_Integer_Type (Result_Type)
8281 and then Result_Type = Type_1;
8289 Is_Numeric_Type (Result_Type)
8290 and then Result_Type = Type_1;
8296 Is_Boolean_Type (Result_Type)
8297 and then not Is_Limited_Type (Type_1);
8305 Is_Boolean_Type (Result_Type)
8306 and then (Is_Array_Type (Type_1)
8307 or else Is_Scalar_Type (Type_1));
8309 when Name_Op_Concat =>
8310 return Is_Array_Type (Result_Type);
8312 when Name_Op_Expon =>
8314 (Is_Integer_Type (Result_Type)
8315 or else Is_Floating_Point_Type (Result_Type))
8316 and then Result_Type = Type_1
8317 and then Type_2 = Standard_Integer;
8320 raise Program_Error;
8333 Is_Numeric_Type (Result_Type)
8334 and then Result_Type = Type_1;
8338 Is_Boolean_Type (Result_Type)
8339 and then Result_Type = Type_1;
8342 raise Program_Error;
8345 end Matches_Predefined_Op;
8349 E : Entity_Id := Current_Entity_In_Scope (S);
8350 C_E : Entity_Id := Current_Entity (S);
8352 -- Start of processing for Enter_Overloaded_Entity
8356 Set_Has_Homonym (E);
8357 Set_Has_Homonym (S);
8360 Set_Is_Immediately_Visible (S);
8361 Set_Scope (S, Current_Scope);
8363 -- Chain new entity if front of homonym in current scope, so that
8364 -- homonyms are contiguous.
8366 if Present (E) and then E /= C_E then
8367 while Homonym (C_E) /= E loop
8368 C_E := Homonym (C_E);
8371 Set_Homonym (C_E, S);
8375 Set_Current_Entity (S);
8380 if Is_Inherited_Operation (S) then
8381 Append_Inherited_Subprogram (S);
8383 Append_Entity (S, Current_Scope);
8386 Set_Public_Status (S);
8388 if Debug_Flag_E then
8389 Write_Str ("New overloaded entity chain: ");
8390 Write_Name (Chars (S));
8393 while Present (E) loop
8394 Write_Str (" "); Write_Int (Int (E));
8401 -- Generate warning for hiding
8404 and then Comes_From_Source (S)
8405 and then In_Extended_Main_Source_Unit (S)
8412 -- Warn unless genuine overloading. Do not emit warning on
8413 -- hiding predefined operators in Standard (these are either an
8414 -- (artifact of our implicit declarations, or simple noise) but
8415 -- keep warning on a operator defined on a local subtype, because
8416 -- of the real danger that different operators may be applied in
8417 -- various parts of the program.
8419 -- Note that if E and S have the same scope, there is never any
8420 -- hiding. Either the two conflict, and the program is illegal,
8421 -- or S is overriding an implicit inherited subprogram.
8423 if Scope (E) /= Scope (S)
8424 and then (not Is_Overloadable (E)
8425 or else Subtype_Conformant (E, S))
8426 and then (Is_Immediately_Visible (E)
8427 or else Is_Potentially_Use_Visible (S))
8429 if Scope (E) = Standard_Standard then
8430 if Nkind (S) = N_Defining_Operator_Symbol
8431 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8433 and then Matches_Predefined_Op
8436 ("declaration of & hides predefined operator?h?", S);
8439 -- E not immediately within Standard
8442 Error_Msg_Sloc := Sloc (E);
8443 Error_Msg_N ("declaration of & hides one #?h?", S);
8448 end Enter_Overloaded_Entity;
8450 -----------------------------
8451 -- Check_Untagged_Equality --
8452 -----------------------------
8454 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8455 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8456 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8460 -- This check applies only if we have a subprogram declaration with an
8461 -- untagged record type.
8463 if Nkind (Decl) /= N_Subprogram_Declaration
8464 or else not Is_Record_Type (Typ)
8465 or else Is_Tagged_Type (Typ)
8470 -- In Ada 2012 case, we will output errors or warnings depending on
8471 -- the setting of debug flag -gnatd.E.
8473 if Ada_Version >= Ada_2012 then
8474 Error_Msg_Warn := Debug_Flag_Dot_EE;
8476 -- In earlier versions of Ada, nothing to do unless we are warning on
8477 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8480 if not Warn_On_Ada_2012_Compatibility then
8485 -- Cases where the type has already been frozen
8487 if Is_Frozen (Typ) then
8489 -- If the type is not declared in a package, or if we are in the body
8490 -- of the package or in some other scope, the new operation is not
8491 -- primitive, and therefore legal, though suspicious. Should we
8492 -- generate a warning in this case ???
8494 if Ekind (Scope (Typ)) /= E_Package
8495 or else Scope (Typ) /= Current_Scope
8499 -- If the type is a generic actual (sub)type, the operation is not
8500 -- primitive either because the base type is declared elsewhere.
8502 elsif Is_Generic_Actual_Type (Typ) then
8505 -- Here we have a definite error of declaration after freezing
8508 if Ada_Version >= Ada_2012 then
8510 ("equality operator must be declared before type & is "
8511 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8513 -- In Ada 2012 mode with error turned to warning, output one
8514 -- more warning to warn that the equality operation may not
8515 -- compose. This is the consequence of ignoring the error.
8517 if Error_Msg_Warn then
8518 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8523 ("equality operator must be declared before type& is "
8524 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8527 -- If we are in the package body, we could just move the
8528 -- declaration to the package spec, so add a message saying that.
8530 if In_Package_Body (Scope (Typ)) then
8531 if Ada_Version >= Ada_2012 then
8533 ("\move declaration to package spec<<", Eq_Op);
8536 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8539 -- Otherwise try to find the freezing point
8542 Obj_Decl := Next (Parent (Typ));
8543 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8544 if Nkind (Obj_Decl) = N_Object_Declaration
8545 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8547 -- Freezing point, output warnings
8549 if Ada_Version >= Ada_2012 then
8551 ("type& is frozen by declaration??", Obj_Decl, Typ);
8553 ("\an equality operator cannot be declared after "
8558 ("type& is frozen by declaration (Ada 2012)?y?",
8561 ("\an equality operator cannot be declared after "
8562 & "this point (Ada 2012)?y?",
8574 -- Here if type is not frozen yet. It is illegal to have a primitive
8575 -- equality declared in the private part if the type is visible.
8577 elsif not In_Same_List (Parent (Typ), Decl)
8578 and then not Is_Limited_Type (Typ)
8580 -- Shouldn't we give an RM reference here???
8582 if Ada_Version >= Ada_2012 then
8584 ("equality operator appears too late<<", Eq_Op);
8587 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8590 -- No error detected
8595 end Check_Untagged_Equality;
8597 -----------------------------
8598 -- Find_Corresponding_Spec --
8599 -----------------------------
8601 function Find_Corresponding_Spec
8603 Post_Error : Boolean := True) return Entity_Id
8605 Spec : constant Node_Id := Specification (N);
8606 Designator : constant Entity_Id := Defining_Entity (Spec);
8610 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8611 -- Even if fully conformant, a body may depend on a generic actual when
8612 -- the spec does not, or vice versa, in which case they were distinct
8613 -- entities in the generic.
8615 -------------------------------
8616 -- Different_Generic_Profile --
8617 -------------------------------
8619 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8622 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8623 -- Check that the types of corresponding formals have the same
8624 -- generic actual if any. We have to account for subtypes of a
8625 -- generic formal, declared between a spec and a body, which may
8626 -- appear distinct in an instance but matched in the generic, and
8627 -- the subtype may be used either in the spec or the body of the
8628 -- subprogram being checked.
8630 -------------------------
8631 -- Same_Generic_Actual --
8632 -------------------------
8634 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8636 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
8637 -- Predicate to check whether S1 is a subtype of S2 in the source
8640 -------------------------
8641 -- Is_Declared_Subtype --
8642 -------------------------
8644 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
8646 return Comes_From_Source (Parent (S1))
8647 and then Nkind (Parent (S1)) = N_Subtype_Declaration
8648 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
8649 and then Entity (Subtype_Indication (Parent (S1))) = S2;
8650 end Is_Declared_Subtype;
8652 -- Start of processing for Same_Generic_Actual
8655 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8656 or else Is_Declared_Subtype (T1, T2)
8657 or else Is_Declared_Subtype (T2, T1);
8658 end Same_Generic_Actual;
8660 -- Start of processing for Different_Generic_Profile
8663 if not In_Instance then
8666 elsif Ekind (E) = E_Function
8667 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8672 F1 := First_Formal (Designator);
8673 F2 := First_Formal (E);
8674 while Present (F1) loop
8675 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8684 end Different_Generic_Profile;
8686 -- Start of processing for Find_Corresponding_Spec
8689 E := Current_Entity (Designator);
8690 while Present (E) loop
8692 -- We are looking for a matching spec. It must have the same scope,
8693 -- and the same name, and either be type conformant, or be the case
8694 -- of a library procedure spec and its body (which belong to one
8695 -- another regardless of whether they are type conformant or not).
8697 if Scope (E) = Current_Scope then
8698 if Current_Scope = Standard_Standard
8699 or else (Ekind (E) = Ekind (Designator)
8700 and then Type_Conformant (E, Designator))
8702 -- Within an instantiation, we know that spec and body are
8703 -- subtype conformant, because they were subtype conformant in
8704 -- the generic. We choose the subtype-conformant entity here as
8705 -- well, to resolve spurious ambiguities in the instance that
8706 -- were not present in the generic (i.e. when two different
8707 -- types are given the same actual). If we are looking for a
8708 -- spec to match a body, full conformance is expected.
8712 -- Inherit the convention and "ghostness" of the matching
8713 -- spec to ensure proper full and subtype conformance.
8715 Set_Convention (Designator, Convention (E));
8717 -- Skip past subprogram bodies and subprogram renamings that
8718 -- may appear to have a matching spec, but that aren't fully
8719 -- conformant with it. That can occur in cases where an
8720 -- actual type causes unrelated homographs in the instance.
8722 if Nkind_In (N, N_Subprogram_Body,
8723 N_Subprogram_Renaming_Declaration)
8724 and then Present (Homonym (E))
8725 and then not Fully_Conformant (Designator, E)
8729 elsif not Subtype_Conformant (Designator, E) then
8732 elsif Different_Generic_Profile (E) then
8737 -- Ada 2012 (AI05-0165): For internally generated bodies of
8738 -- null procedures locate the internally generated spec. We
8739 -- enforce mode conformance since a tagged type may inherit
8740 -- from interfaces several null primitives which differ only
8741 -- in the mode of the formals.
8743 if not (Comes_From_Source (E))
8744 and then Is_Null_Procedure (E)
8745 and then not Mode_Conformant (Designator, E)
8749 -- For null procedures coming from source that are completions,
8750 -- analysis of the generated body will establish the link.
8752 elsif Comes_From_Source (E)
8753 and then Nkind (Spec) = N_Procedure_Specification
8754 and then Null_Present (Spec)
8758 -- Expression functions can be completions, but cannot be
8759 -- completed by an explicit body.
8761 elsif Comes_From_Source (E)
8762 and then Comes_From_Source (N)
8763 and then Nkind (N) = N_Subprogram_Body
8764 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8765 N_Expression_Function
8767 Error_Msg_Sloc := Sloc (E);
8768 Error_Msg_N ("body conflicts with expression function#", N);
8771 elsif not Has_Completion (E) then
8772 if Nkind (N) /= N_Subprogram_Body_Stub then
8773 Set_Corresponding_Spec (N, E);
8776 Set_Has_Completion (E);
8779 elsif Nkind (Parent (N)) = N_Subunit then
8781 -- If this is the proper body of a subunit, the completion
8782 -- flag is set when analyzing the stub.
8786 -- If E is an internal function with a controlling result that
8787 -- was created for an operation inherited by a null extension,
8788 -- it may be overridden by a body without a previous spec (one
8789 -- more reason why these should be shunned). In that case we
8790 -- remove the generated body if present, because the current
8791 -- one is the explicit overriding.
8793 elsif Ekind (E) = E_Function
8794 and then Ada_Version >= Ada_2005
8795 and then not Comes_From_Source (E)
8796 and then Has_Controlling_Result (E)
8797 and then Is_Null_Extension (Etype (E))
8798 and then Comes_From_Source (Spec)
8800 Set_Has_Completion (E, False);
8803 and then Nkind (Parent (E)) = N_Function_Specification
8806 (Unit_Declaration_Node
8807 (Corresponding_Body (Unit_Declaration_Node (E))));
8811 -- If expansion is disabled, or if the wrapper function has
8812 -- not been generated yet, this a late body overriding an
8813 -- inherited operation, or it is an overriding by some other
8814 -- declaration before the controlling result is frozen. In
8815 -- either case this is a declaration of a new entity.
8821 -- If the body already exists, then this is an error unless
8822 -- the previous declaration is the implicit declaration of a
8823 -- derived subprogram. It is also legal for an instance to
8824 -- contain type conformant overloadable declarations (but the
8825 -- generic declaration may not), per 8.3(26/2).
8827 elsif No (Alias (E))
8828 and then not Is_Intrinsic_Subprogram (E)
8829 and then not In_Instance
8832 Error_Msg_Sloc := Sloc (E);
8834 if Is_Imported (E) then
8836 ("body not allowed for imported subprogram & declared#",
8839 Error_Msg_NE ("duplicate body for & declared#", N, E);
8843 -- Child units cannot be overloaded, so a conformance mismatch
8844 -- between body and a previous spec is an error.
8846 elsif Is_Child_Unit (E)
8848 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8850 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8855 ("body of child unit does not match previous declaration", N);
8863 -- On exit, we know that no previous declaration of subprogram exists
8866 end Find_Corresponding_Spec;
8868 ----------------------
8869 -- Fully_Conformant --
8870 ----------------------
8872 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8875 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8877 end Fully_Conformant;
8879 ----------------------------------
8880 -- Fully_Conformant_Expressions --
8881 ----------------------------------
8883 function Fully_Conformant_Expressions
8884 (Given_E1 : Node_Id;
8885 Given_E2 : Node_Id) return Boolean
8887 E1 : constant Node_Id := Original_Node (Given_E1);
8888 E2 : constant Node_Id := Original_Node (Given_E2);
8889 -- We always test conformance on original nodes, since it is possible
8890 -- for analysis and/or expansion to make things look as though they
8891 -- conform when they do not, e.g. by converting 1+2 into 3.
8893 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8894 renames Fully_Conformant_Expressions;
8896 function FCL (L1, L2 : List_Id) return Boolean;
8897 -- Compare elements of two lists for conformance. Elements have to be
8898 -- conformant, and actuals inserted as default parameters do not match
8899 -- explicit actuals with the same value.
8901 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8902 -- Compare an operator node with a function call
8908 function FCL (L1, L2 : List_Id) return Boolean is
8912 if L1 = No_List then
8918 if L2 = No_List then
8924 -- Compare two lists, skipping rewrite insertions (we want to compare
8925 -- the original trees, not the expanded versions).
8928 if Is_Rewrite_Insertion (N1) then
8930 elsif Is_Rewrite_Insertion (N2) then
8936 elsif not FCE (N1, N2) then
8949 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8950 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8955 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8960 Act := First (Actuals);
8962 if Nkind (Op_Node) in N_Binary_Op then
8963 if not FCE (Left_Opnd (Op_Node), Act) then
8970 return Present (Act)
8971 and then FCE (Right_Opnd (Op_Node), Act)
8972 and then No (Next (Act));
8976 -- Start of processing for Fully_Conformant_Expressions
8979 -- Nonconformant if paren count does not match. Note: if some idiot
8980 -- complains that we don't do this right for more than 3 levels of
8981 -- parentheses, they will be treated with the respect they deserve.
8983 if Paren_Count (E1) /= Paren_Count (E2) then
8986 -- If same entities are referenced, then they are conformant even if
8987 -- they have different forms (RM 8.3.1(19-20)).
8989 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8990 if Present (Entity (E1)) then
8991 return Entity (E1) = Entity (E2)
8993 -- One may be a discriminant that has been replaced by the
8994 -- corresponding discriminal.
8997 (Chars (Entity (E1)) = Chars (Entity (E2))
8998 and then Ekind (Entity (E1)) = E_Discriminant
8999 and then Ekind (Entity (E2)) = E_In_Parameter)
9001 -- The discriminant of a protected type is transformed into
9002 -- a local constant and then into a parameter of a protected
9006 (Ekind (Entity (E1)) = E_Constant
9007 and then Ekind (Entity (E2)) = E_In_Parameter
9008 and then Present (Discriminal_Link (Entity (E1)))
9009 and then Discriminal_Link (Entity (E1)) =
9010 Discriminal_Link (Entity (E2)))
9012 -- AI12-050: The loop variables of quantified expressions
9013 -- match if they have the same identifier, even though they
9014 -- are different entities.
9017 (Chars (Entity (E1)) = Chars (Entity (E2))
9018 and then Ekind (Entity (E1)) = E_Loop_Parameter
9019 and then Ekind (Entity (E2)) = E_Loop_Parameter);
9021 elsif Nkind (E1) = N_Expanded_Name
9022 and then Nkind (E2) = N_Expanded_Name
9023 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9024 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9026 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9029 -- Identifiers in component associations don't always have
9030 -- entities, but their names must conform.
9032 return Nkind (E1) = N_Identifier
9033 and then Nkind (E2) = N_Identifier
9034 and then Chars (E1) = Chars (E2);
9037 elsif Nkind (E1) = N_Character_Literal
9038 and then Nkind (E2) = N_Expanded_Name
9040 return Nkind (Selector_Name (E2)) = N_Character_Literal
9041 and then Chars (E1) = Chars (Selector_Name (E2));
9043 elsif Nkind (E2) = N_Character_Literal
9044 and then Nkind (E1) = N_Expanded_Name
9046 return Nkind (Selector_Name (E1)) = N_Character_Literal
9047 and then Chars (E2) = Chars (Selector_Name (E1));
9049 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9050 return FCO (E1, E2);
9052 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9053 return FCO (E2, E1);
9055 -- Otherwise we must have the same syntactic entity
9057 elsif Nkind (E1) /= Nkind (E2) then
9060 -- At this point, we specialize by node type
9066 FCL (Expressions (E1), Expressions (E2))
9068 FCL (Component_Associations (E1),
9069 Component_Associations (E2));
9072 if Nkind (Expression (E1)) = N_Qualified_Expression
9074 Nkind (Expression (E2)) = N_Qualified_Expression
9076 return FCE (Expression (E1), Expression (E2));
9078 -- Check that the subtype marks and any constraints
9083 Indic1 : constant Node_Id := Expression (E1);
9084 Indic2 : constant Node_Id := Expression (E2);
9089 if Nkind (Indic1) /= N_Subtype_Indication then
9091 Nkind (Indic2) /= N_Subtype_Indication
9092 and then Entity (Indic1) = Entity (Indic2);
9094 elsif Nkind (Indic2) /= N_Subtype_Indication then
9096 Nkind (Indic1) /= N_Subtype_Indication
9097 and then Entity (Indic1) = Entity (Indic2);
9100 if Entity (Subtype_Mark (Indic1)) /=
9101 Entity (Subtype_Mark (Indic2))
9106 Elt1 := First (Constraints (Constraint (Indic1)));
9107 Elt2 := First (Constraints (Constraint (Indic2)));
9108 while Present (Elt1) and then Present (Elt2) loop
9109 if not FCE (Elt1, Elt2) then
9122 when N_Attribute_Reference =>
9124 Attribute_Name (E1) = Attribute_Name (E2)
9125 and then FCL (Expressions (E1), Expressions (E2));
9129 Entity (E1) = Entity (E2)
9130 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9131 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9133 when N_Membership_Test
9137 FCE (Left_Opnd (E1), Left_Opnd (E2))
9139 FCE (Right_Opnd (E1), Right_Opnd (E2));
9141 when N_Case_Expression =>
9147 if not FCE (Expression (E1), Expression (E2)) then
9151 Alt1 := First (Alternatives (E1));
9152 Alt2 := First (Alternatives (E2));
9154 if Present (Alt1) /= Present (Alt2) then
9156 elsif No (Alt1) then
9160 if not FCE (Expression (Alt1), Expression (Alt2))
9161 or else not FCL (Discrete_Choices (Alt1),
9162 Discrete_Choices (Alt2))
9173 when N_Character_Literal =>
9175 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9177 when N_Component_Association =>
9179 FCL (Choices (E1), Choices (E2))
9181 FCE (Expression (E1), Expression (E2));
9183 when N_Explicit_Dereference =>
9185 FCE (Prefix (E1), Prefix (E2));
9187 when N_Extension_Aggregate =>
9189 FCL (Expressions (E1), Expressions (E2))
9190 and then Null_Record_Present (E1) =
9191 Null_Record_Present (E2)
9192 and then FCL (Component_Associations (E1),
9193 Component_Associations (E2));
9195 when N_Function_Call =>
9197 FCE (Name (E1), Name (E2))
9199 FCL (Parameter_Associations (E1),
9200 Parameter_Associations (E2));
9202 when N_If_Expression =>
9204 FCL (Expressions (E1), Expressions (E2));
9206 when N_Indexed_Component =>
9208 FCE (Prefix (E1), Prefix (E2))
9210 FCL (Expressions (E1), Expressions (E2));
9212 when N_Integer_Literal =>
9213 return (Intval (E1) = Intval (E2));
9218 when N_Operator_Symbol =>
9220 Chars (E1) = Chars (E2);
9222 when N_Others_Choice =>
9225 when N_Parameter_Association =>
9227 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9228 and then FCE (Explicit_Actual_Parameter (E1),
9229 Explicit_Actual_Parameter (E2));
9231 when N_Qualified_Expression
9233 | N_Unchecked_Type_Conversion
9236 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9238 FCE (Expression (E1), Expression (E2));
9240 when N_Quantified_Expression =>
9241 if not FCE (Condition (E1), Condition (E2)) then
9245 if Present (Loop_Parameter_Specification (E1))
9246 and then Present (Loop_Parameter_Specification (E2))
9249 L1 : constant Node_Id :=
9250 Loop_Parameter_Specification (E1);
9251 L2 : constant Node_Id :=
9252 Loop_Parameter_Specification (E2);
9256 Reverse_Present (L1) = Reverse_Present (L2)
9258 FCE (Defining_Identifier (L1),
9259 Defining_Identifier (L2))
9261 FCE (Discrete_Subtype_Definition (L1),
9262 Discrete_Subtype_Definition (L2));
9265 elsif Present (Iterator_Specification (E1))
9266 and then Present (Iterator_Specification (E2))
9269 I1 : constant Node_Id := Iterator_Specification (E1);
9270 I2 : constant Node_Id := Iterator_Specification (E2);
9274 FCE (Defining_Identifier (I1),
9275 Defining_Identifier (I2))
9277 Of_Present (I1) = Of_Present (I2)
9279 Reverse_Present (I1) = Reverse_Present (I2)
9280 and then FCE (Name (I1), Name (I2))
9281 and then FCE (Subtype_Indication (I1),
9282 Subtype_Indication (I2));
9285 -- The quantified expressions used different specifications to
9286 -- walk their respective ranges.
9294 FCE (Low_Bound (E1), Low_Bound (E2))
9296 FCE (High_Bound (E1), High_Bound (E2));
9298 when N_Real_Literal =>
9299 return (Realval (E1) = Realval (E2));
9301 when N_Selected_Component =>
9303 FCE (Prefix (E1), Prefix (E2))
9305 FCE (Selector_Name (E1), Selector_Name (E2));
9309 FCE (Prefix (E1), Prefix (E2))
9311 FCE (Discrete_Range (E1), Discrete_Range (E2));
9313 when N_String_Literal =>
9315 S1 : constant String_Id := Strval (E1);
9316 S2 : constant String_Id := Strval (E2);
9317 L1 : constant Nat := String_Length (S1);
9318 L2 : constant Nat := String_Length (S2);
9325 for J in 1 .. L1 loop
9326 if Get_String_Char (S1, J) /=
9327 Get_String_Char (S2, J)
9339 Entity (E1) = Entity (E2)
9341 FCE (Right_Opnd (E1), Right_Opnd (E2));
9343 -- All other node types cannot appear in this context. Strictly
9344 -- we should raise a fatal internal error. Instead we just ignore
9345 -- the nodes. This means that if anyone makes a mistake in the
9346 -- expander and mucks an expression tree irretrievably, the result
9347 -- will be a failure to detect a (probably very obscure) case
9348 -- of non-conformance, which is better than bombing on some
9349 -- case where two expressions do in fact conform.
9355 end Fully_Conformant_Expressions;
9357 ----------------------------------------
9358 -- Fully_Conformant_Discrete_Subtypes --
9359 ----------------------------------------
9361 function Fully_Conformant_Discrete_Subtypes
9362 (Given_S1 : Node_Id;
9363 Given_S2 : Node_Id) return Boolean
9365 S1 : constant Node_Id := Original_Node (Given_S1);
9366 S2 : constant Node_Id := Original_Node (Given_S2);
9368 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9369 -- Special-case for a bound given by a discriminant, which in the body
9370 -- is replaced with the discriminal of the enclosing type.
9372 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9373 -- Check both bounds
9375 -----------------------
9376 -- Conforming_Bounds --
9377 -----------------------
9379 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9381 if Is_Entity_Name (B1)
9382 and then Is_Entity_Name (B2)
9383 and then Ekind (Entity (B1)) = E_Discriminant
9385 return Chars (B1) = Chars (B2);
9388 return Fully_Conformant_Expressions (B1, B2);
9390 end Conforming_Bounds;
9392 -----------------------
9393 -- Conforming_Ranges --
9394 -----------------------
9396 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9399 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9401 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9402 end Conforming_Ranges;
9404 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9407 if Nkind (S1) /= Nkind (S2) then
9410 elsif Is_Entity_Name (S1) then
9411 return Entity (S1) = Entity (S2);
9413 elsif Nkind (S1) = N_Range then
9414 return Conforming_Ranges (S1, S2);
9416 elsif Nkind (S1) = N_Subtype_Indication then
9418 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9421 (Range_Expression (Constraint (S1)),
9422 Range_Expression (Constraint (S2)));
9426 end Fully_Conformant_Discrete_Subtypes;
9428 --------------------
9429 -- Install_Entity --
9430 --------------------
9432 procedure Install_Entity (E : Entity_Id) is
9433 Prev : constant Entity_Id := Current_Entity (E);
9435 Set_Is_Immediately_Visible (E);
9436 Set_Current_Entity (E);
9437 Set_Homonym (E, Prev);
9440 ---------------------
9441 -- Install_Formals --
9442 ---------------------
9444 procedure Install_Formals (Id : Entity_Id) is
9447 F := First_Formal (Id);
9448 while Present (F) loop
9452 end Install_Formals;
9454 -----------------------------
9455 -- Is_Interface_Conformant --
9456 -----------------------------
9458 function Is_Interface_Conformant
9459 (Tagged_Type : Entity_Id;
9460 Iface_Prim : Entity_Id;
9461 Prim : Entity_Id) return Boolean
9463 -- The operation may in fact be an inherited (implicit) operation
9464 -- rather than the original interface primitive, so retrieve the
9465 -- ultimate ancestor.
9467 Iface : constant Entity_Id :=
9468 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9469 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9471 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9472 -- Return the controlling formal of Prim
9474 ------------------------
9475 -- Controlling_Formal --
9476 ------------------------
9478 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9482 E := First_Entity (Prim);
9483 while Present (E) loop
9484 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9492 end Controlling_Formal;
9496 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9497 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9499 -- Start of processing for Is_Interface_Conformant
9502 pragma Assert (Is_Subprogram (Iface_Prim)
9503 and then Is_Subprogram (Prim)
9504 and then Is_Dispatching_Operation (Iface_Prim)
9505 and then Is_Dispatching_Operation (Prim));
9507 pragma Assert (Is_Interface (Iface)
9508 or else (Present (Alias (Iface_Prim))
9511 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9513 if Prim = Iface_Prim
9514 or else not Is_Subprogram (Prim)
9515 or else Ekind (Prim) /= Ekind (Iface_Prim)
9516 or else not Is_Dispatching_Operation (Prim)
9517 or else Scope (Prim) /= Scope (Tagged_Type)
9519 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9520 or else not Primitive_Names_Match (Iface_Prim, Prim)
9524 -- The mode of the controlling formals must match
9526 elsif Present (Iface_Ctrl_F)
9527 and then Present (Prim_Ctrl_F)
9528 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9532 -- Case of a procedure, or a function whose result type matches the
9533 -- result type of the interface primitive, or a function that has no
9534 -- controlling result (I or access I).
9536 elsif Ekind (Iface_Prim) = E_Procedure
9537 or else Etype (Prim) = Etype (Iface_Prim)
9538 or else not Has_Controlling_Result (Prim)
9540 return Type_Conformant
9541 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9543 -- Case of a function returning an interface, or an access to one. Check
9544 -- that the return types correspond.
9546 elsif Implements_Interface (Typ, Iface) then
9547 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9549 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9554 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9555 Skip_Controlling_Formals => True);
9561 end Is_Interface_Conformant;
9563 ---------------------------------
9564 -- Is_Non_Overriding_Operation --
9565 ---------------------------------
9567 function Is_Non_Overriding_Operation
9568 (Prev_E : Entity_Id;
9569 New_E : Entity_Id) return Boolean
9573 G_Typ : Entity_Id := Empty;
9575 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9576 -- If F_Type is a derived type associated with a generic actual subtype,
9577 -- then return its Generic_Parent_Type attribute, else return Empty.
9579 function Types_Correspond
9580 (P_Type : Entity_Id;
9581 N_Type : Entity_Id) return Boolean;
9582 -- Returns true if and only if the types (or designated types in the
9583 -- case of anonymous access types) are the same or N_Type is derived
9584 -- directly or indirectly from P_Type.
9586 -----------------------------
9587 -- Get_Generic_Parent_Type --
9588 -----------------------------
9590 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9596 if Is_Derived_Type (F_Typ)
9597 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9599 -- The tree must be traversed to determine the parent subtype in
9600 -- the generic unit, which unfortunately isn't always available
9601 -- via semantic attributes. ??? (Note: The use of Original_Node
9602 -- is needed for cases where a full derived type has been
9605 -- If the parent type is a scalar type, the derivation creates
9606 -- an anonymous base type for it, and the source type is its
9609 if Is_Scalar_Type (F_Typ)
9610 and then not Comes_From_Source (F_Typ)
9614 (Original_Node (Parent (First_Subtype (F_Typ))));
9616 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9618 if Nkind (Defn) = N_Derived_Type_Definition then
9619 Indic := Subtype_Indication (Defn);
9621 if Nkind (Indic) = N_Subtype_Indication then
9622 G_Typ := Entity (Subtype_Mark (Indic));
9624 G_Typ := Entity (Indic);
9627 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9628 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9630 return Generic_Parent_Type (Parent (G_Typ));
9636 end Get_Generic_Parent_Type;
9638 ----------------------
9639 -- Types_Correspond --
9640 ----------------------
9642 function Types_Correspond
9643 (P_Type : Entity_Id;
9644 N_Type : Entity_Id) return Boolean
9646 Prev_Type : Entity_Id := Base_Type (P_Type);
9647 New_Type : Entity_Id := Base_Type (N_Type);
9650 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9651 Prev_Type := Designated_Type (Prev_Type);
9654 if Ekind (New_Type) = E_Anonymous_Access_Type then
9655 New_Type := Designated_Type (New_Type);
9658 if Prev_Type = New_Type then
9661 elsif not Is_Class_Wide_Type (New_Type) then
9662 while Etype (New_Type) /= New_Type loop
9663 New_Type := Etype (New_Type);
9665 if New_Type = Prev_Type then
9671 end Types_Correspond;
9673 -- Start of processing for Is_Non_Overriding_Operation
9676 -- In the case where both operations are implicit derived subprograms
9677 -- then neither overrides the other. This can only occur in certain
9678 -- obscure cases (e.g., derivation from homographs created in a generic
9681 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9684 elsif Ekind (Current_Scope) = E_Package
9685 and then Is_Generic_Instance (Current_Scope)
9686 and then In_Private_Part (Current_Scope)
9687 and then Comes_From_Source (New_E)
9689 -- We examine the formals and result type of the inherited operation,
9690 -- to determine whether their type is derived from (the instance of)
9691 -- a generic type. The first such formal or result type is the one
9694 Formal := First_Formal (Prev_E);
9696 while Present (Formal) loop
9697 F_Typ := Base_Type (Etype (Formal));
9699 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9700 F_Typ := Designated_Type (F_Typ);
9703 G_Typ := Get_Generic_Parent_Type (F_Typ);
9704 exit when Present (G_Typ);
9706 Next_Formal (Formal);
9709 -- If the function dispatches on result check the result type
9711 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9712 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9719 -- If the generic type is a private type, then the original operation
9720 -- was not overriding in the generic, because there was no primitive
9721 -- operation to override.
9723 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9724 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9725 N_Formal_Private_Type_Definition
9729 -- The generic parent type is the ancestor of a formal derived
9730 -- type declaration. We need to check whether it has a primitive
9731 -- operation that should be overridden by New_E in the generic.
9735 P_Formal : Entity_Id;
9736 N_Formal : Entity_Id;
9740 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9743 while Present (Prim_Elt) loop
9744 P_Prim := Node (Prim_Elt);
9746 if Chars (P_Prim) = Chars (New_E)
9747 and then Ekind (P_Prim) = Ekind (New_E)
9749 P_Formal := First_Formal (P_Prim);
9750 N_Formal := First_Formal (New_E);
9751 while Present (P_Formal) and then Present (N_Formal) loop
9752 P_Typ := Etype (P_Formal);
9753 N_Typ := Etype (N_Formal);
9755 if not Types_Correspond (P_Typ, N_Typ) then
9759 Next_Entity (P_Formal);
9760 Next_Entity (N_Formal);
9763 -- Found a matching primitive operation belonging to the
9764 -- formal ancestor type, so the new subprogram is
9768 and then No (N_Formal)
9769 and then (Ekind (New_E) /= E_Function
9772 (Etype (P_Prim), Etype (New_E)))
9778 Next_Elmt (Prim_Elt);
9781 -- If no match found, then the new subprogram does not override
9782 -- in the generic (nor in the instance).
9784 -- If the type in question is not abstract, and the subprogram
9785 -- is, this will be an error if the new operation is in the
9786 -- private part of the instance. Emit a warning now, which will
9787 -- make the subsequent error message easier to understand.
9789 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
9790 and then Is_Abstract_Subprogram (Prev_E)
9791 and then In_Private_Part (Current_Scope)
9793 Error_Msg_Node_2 := F_Typ;
9795 ("private operation& in generic unit does not override "
9796 & "any primitive operation of& (RM 12.3 (18))??",
9806 end Is_Non_Overriding_Operation;
9808 -------------------------------------
9809 -- List_Inherited_Pre_Post_Aspects --
9810 -------------------------------------
9812 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9814 if Opt.List_Inherited_Aspects
9815 and then Is_Subprogram_Or_Generic_Subprogram (E)
9818 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9823 for Index in Subps'Range loop
9824 Items := Contract (Subps (Index));
9826 if Present (Items) then
9827 Prag := Pre_Post_Conditions (Items);
9828 while Present (Prag) loop
9829 Error_Msg_Sloc := Sloc (Prag);
9831 if Class_Present (Prag)
9832 and then not Split_PPC (Prag)
9834 if Pragma_Name (Prag) = Name_Precondition then
9836 ("info: & inherits `Pre''Class` aspect from "
9840 ("info: & inherits `Post''Class` aspect from "
9845 Prag := Next_Pragma (Prag);
9851 end List_Inherited_Pre_Post_Aspects;
9853 ------------------------------
9854 -- Make_Inequality_Operator --
9855 ------------------------------
9857 -- S is the defining identifier of an equality operator. We build a
9858 -- subprogram declaration with the right signature. This operation is
9859 -- intrinsic, because it is always expanded as the negation of the
9860 -- call to the equality function.
9862 procedure Make_Inequality_Operator (S : Entity_Id) is
9863 Loc : constant Source_Ptr := Sloc (S);
9866 Op_Name : Entity_Id;
9868 FF : constant Entity_Id := First_Formal (S);
9869 NF : constant Entity_Id := Next_Formal (FF);
9872 -- Check that equality was properly defined, ignore call if not
9879 A : constant Entity_Id :=
9880 Make_Defining_Identifier (Sloc (FF),
9881 Chars => Chars (FF));
9883 B : constant Entity_Id :=
9884 Make_Defining_Identifier (Sloc (NF),
9885 Chars => Chars (NF));
9888 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9890 Formals := New_List (
9891 Make_Parameter_Specification (Loc,
9892 Defining_Identifier => A,
9894 New_Occurrence_Of (Etype (First_Formal (S)),
9895 Sloc (Etype (First_Formal (S))))),
9897 Make_Parameter_Specification (Loc,
9898 Defining_Identifier => B,
9900 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9901 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9904 Make_Subprogram_Declaration (Loc,
9906 Make_Function_Specification (Loc,
9907 Defining_Unit_Name => Op_Name,
9908 Parameter_Specifications => Formals,
9909 Result_Definition =>
9910 New_Occurrence_Of (Standard_Boolean, Loc)));
9912 -- Insert inequality right after equality if it is explicit or after
9913 -- the derived type when implicit. These entities are created only
9914 -- for visibility purposes, and eventually replaced in the course
9915 -- of expansion, so they do not need to be attached to the tree and
9916 -- seen by the back-end. Keeping them internal also avoids spurious
9917 -- freezing problems. The declaration is inserted in the tree for
9918 -- analysis, and removed afterwards. If the equality operator comes
9919 -- from an explicit declaration, attach the inequality immediately
9920 -- after. Else the equality is inherited from a derived type
9921 -- declaration, so insert inequality after that declaration.
9923 if No (Alias (S)) then
9924 Insert_After (Unit_Declaration_Node (S), Decl);
9925 elsif Is_List_Member (Parent (S)) then
9926 Insert_After (Parent (S), Decl);
9928 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9931 Mark_Rewrite_Insertion (Decl);
9932 Set_Is_Intrinsic_Subprogram (Op_Name);
9935 Set_Has_Completion (Op_Name);
9936 Set_Corresponding_Equality (Op_Name, S);
9937 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9939 end Make_Inequality_Operator;
9941 ----------------------
9942 -- May_Need_Actuals --
9943 ----------------------
9945 procedure May_Need_Actuals (Fun : Entity_Id) is
9950 F := First_Formal (Fun);
9952 while Present (F) loop
9953 if No (Default_Value (F)) then
9961 Set_Needs_No_Actuals (Fun, B);
9962 end May_Need_Actuals;
9964 ---------------------
9965 -- Mode_Conformant --
9966 ---------------------
9968 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9971 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9973 end Mode_Conformant;
9975 ---------------------------
9976 -- New_Overloaded_Entity --
9977 ---------------------------
9979 procedure New_Overloaded_Entity
9981 Derived_Type : Entity_Id := Empty)
9983 Overridden_Subp : Entity_Id := Empty;
9984 -- Set if the current scope has an operation that is type-conformant
9985 -- with S, and becomes hidden by S.
9987 Is_Primitive_Subp : Boolean;
9988 -- Set to True if the new subprogram is primitive
9991 -- Entity that S overrides
9993 Prev_Vis : Entity_Id := Empty;
9994 -- Predecessor of E in Homonym chain
9996 procedure Check_For_Primitive_Subprogram
9997 (Is_Primitive : out Boolean;
9998 Is_Overriding : Boolean := False);
9999 -- If the subprogram being analyzed is a primitive operation of the type
10000 -- of a formal or result, set the Has_Primitive_Operations flag on the
10001 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10002 -- corresponding flag on the entity itself for later use.
10004 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10005 -- True if a) E is a subprogram whose first formal is a concurrent type
10006 -- defined in the scope of E that has some entry or subprogram whose
10007 -- profile matches E, or b) E is an internally built dispatching
10008 -- subprogram of a protected type and there is a matching subprogram
10009 -- defined in the enclosing scope of the protected type, or c) E is
10010 -- an entry of a synchronized type and a matching procedure has been
10011 -- previously defined in the enclosing scope of the synchronized type.
10013 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10014 -- Check that E is declared in the private part of the current package,
10015 -- or in the package body, where it may hide a previous declaration.
10016 -- We can't use In_Private_Part by itself because this flag is also
10017 -- set when freezing entities, so we must examine the place of the
10018 -- declaration in the tree, and recognize wrapper packages as well.
10020 function Is_Overriding_Alias
10021 (Old_E : Entity_Id;
10022 New_E : Entity_Id) return Boolean;
10023 -- Check whether new subprogram and old subprogram are both inherited
10024 -- from subprograms that have distinct dispatch table entries. This can
10025 -- occur with derivations from instances with accidental homonyms. The
10026 -- function is conservative given that the converse is only true within
10027 -- instances that contain accidental overloadings.
10029 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10030 -- Report conflict between entities S and E
10032 ------------------------------------
10033 -- Check_For_Primitive_Subprogram --
10034 ------------------------------------
10036 procedure Check_For_Primitive_Subprogram
10037 (Is_Primitive : out Boolean;
10038 Is_Overriding : Boolean := False)
10040 Formal : Entity_Id;
10044 function Visible_Part_Type (T : Entity_Id) return Boolean;
10045 -- Returns true if T is declared in the visible part of the current
10046 -- package scope; otherwise returns false. Assumes that T is declared
10049 procedure Check_Private_Overriding (T : Entity_Id);
10050 -- Checks that if a primitive abstract subprogram of a visible
10051 -- abstract type is declared in a private part, then it must override
10052 -- an abstract subprogram declared in the visible part. Also checks
10053 -- that if a primitive function with a controlling result is declared
10054 -- in a private part, then it must override a function declared in
10055 -- the visible part.
10057 ------------------------------
10058 -- Check_Private_Overriding --
10059 ------------------------------
10061 procedure Check_Private_Overriding (T : Entity_Id) is
10062 function Overrides_Private_Part_Op return Boolean;
10063 -- This detects the special case where the overriding subprogram
10064 -- is overriding a subprogram that was declared in the same
10065 -- private part. That case is illegal by 3.9.3(10).
10067 function Overrides_Visible_Function
10068 (Partial_View : Entity_Id) return Boolean;
10069 -- True if S overrides a function in the visible part. The
10070 -- overridden function could be explicitly or implicitly declared.
10072 -------------------------------
10073 -- Overrides_Private_Part_Op --
10074 -------------------------------
10076 function Overrides_Private_Part_Op return Boolean is
10077 Over_Decl : constant Node_Id :=
10078 Unit_Declaration_Node (Overridden_Operation (S));
10079 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10082 pragma Assert (Is_Overriding);
10084 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10086 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10088 return In_Same_List (Over_Decl, Subp_Decl);
10089 end Overrides_Private_Part_Op;
10091 --------------------------------
10092 -- Overrides_Visible_Function --
10093 --------------------------------
10095 function Overrides_Visible_Function
10096 (Partial_View : Entity_Id) return Boolean
10099 if not Is_Overriding or else not Has_Homonym (S) then
10103 if not Present (Partial_View) then
10107 -- Search through all the homonyms H of S in the current
10108 -- package spec, and return True if we find one that matches.
10109 -- Note that Parent (H) will be the declaration of the
10110 -- partial view of T for a match.
10113 H : Entity_Id := S;
10117 exit when not Present (H) or else Scope (H) /= Scope (S);
10121 N_Private_Extension_Declaration,
10122 N_Private_Type_Declaration)
10123 and then Defining_Identifier (Parent (H)) = Partial_View
10131 end Overrides_Visible_Function;
10133 -- Start of processing for Check_Private_Overriding
10136 if Is_Package_Or_Generic_Package (Current_Scope)
10137 and then In_Private_Part (Current_Scope)
10138 and then Visible_Part_Type (T)
10139 and then not In_Instance
10141 if Is_Abstract_Type (T)
10142 and then Is_Abstract_Subprogram (S)
10143 and then (not Is_Overriding
10144 or else not Is_Abstract_Subprogram (E)
10145 or else Overrides_Private_Part_Op)
10148 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10151 elsif Ekind (S) = E_Function then
10153 Partial_View : constant Entity_Id :=
10154 Incomplete_Or_Partial_View (T);
10157 if not Overrides_Visible_Function (Partial_View) then
10159 -- Here, S is "function ... return T;" declared in
10160 -- the private part, not overriding some visible
10161 -- operation. That's illegal in the tagged case
10162 -- (but not if the private type is untagged).
10164 if ((Present (Partial_View)
10165 and then Is_Tagged_Type (Partial_View))
10166 or else (not Present (Partial_View)
10167 and then Is_Tagged_Type (T)))
10168 and then T = Base_Type (Etype (S))
10171 ("private function with tagged result must"
10172 & " override visible-part function", S);
10174 ("\move subprogram to the visible part"
10175 & " (RM 3.9.3(10))", S);
10177 -- AI05-0073: extend this test to the case of a
10178 -- function with a controlling access result.
10180 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10181 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10183 not Is_Class_Wide_Type
10184 (Designated_Type (Etype (S)))
10185 and then Ada_Version >= Ada_2012
10188 ("private function with controlling access "
10189 & "result must override visible-part function",
10192 ("\move subprogram to the visible part"
10193 & " (RM 3.9.3(10))", S);
10199 end Check_Private_Overriding;
10201 -----------------------
10202 -- Visible_Part_Type --
10203 -----------------------
10205 function Visible_Part_Type (T : Entity_Id) return Boolean is
10206 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10209 -- If the entity is a private type, then it must be declared in a
10212 if Ekind (T) in Private_Kind then
10215 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10218 elsif Is_List_Member (Declaration_Node (T))
10219 and then List_Containing (Declaration_Node (T)) =
10220 Visible_Declarations (Specification (P))
10227 end Visible_Part_Type;
10229 -- Start of processing for Check_For_Primitive_Subprogram
10232 Is_Primitive := False;
10234 if not Comes_From_Source (S) then
10237 -- If subprogram is at library level, it is not primitive operation
10239 elsif Current_Scope = Standard_Standard then
10242 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10243 and then not In_Package_Body (Current_Scope))
10244 or else Is_Overriding
10246 -- For function, check return type
10248 if Ekind (S) = E_Function then
10249 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10250 F_Typ := Designated_Type (Etype (S));
10252 F_Typ := Etype (S);
10255 B_Typ := Base_Type (F_Typ);
10257 if Scope (B_Typ) = Current_Scope
10258 and then not Is_Class_Wide_Type (B_Typ)
10259 and then not Is_Generic_Type (B_Typ)
10261 Is_Primitive := True;
10262 Set_Has_Primitive_Operations (B_Typ);
10263 Set_Is_Primitive (S);
10264 Check_Private_Overriding (B_Typ);
10266 -- The Ghost policy in effect at the point of declaration
10267 -- or a tagged type and a primitive operation must match
10268 -- (SPARK RM 6.9(16)).
10270 Check_Ghost_Primitive (S, B_Typ);
10274 -- For all subprograms, check formals
10276 Formal := First_Formal (S);
10277 while Present (Formal) loop
10278 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10279 F_Typ := Designated_Type (Etype (Formal));
10281 F_Typ := Etype (Formal);
10284 B_Typ := Base_Type (F_Typ);
10286 if Ekind (B_Typ) = E_Access_Subtype then
10287 B_Typ := Base_Type (B_Typ);
10290 if Scope (B_Typ) = Current_Scope
10291 and then not Is_Class_Wide_Type (B_Typ)
10292 and then not Is_Generic_Type (B_Typ)
10294 Is_Primitive := True;
10295 Set_Is_Primitive (S);
10296 Set_Has_Primitive_Operations (B_Typ);
10297 Check_Private_Overriding (B_Typ);
10299 -- The Ghost policy in effect at the point of declaration
10300 -- of a tagged type and a primitive operation must match
10301 -- (SPARK RM 6.9(16)).
10303 Check_Ghost_Primitive (S, B_Typ);
10306 Next_Formal (Formal);
10309 -- Special case: An equality function can be redefined for a type
10310 -- occurring in a declarative part, and won't otherwise be treated as
10311 -- a primitive because it doesn't occur in a package spec and doesn't
10312 -- override an inherited subprogram. It's important that we mark it
10313 -- primitive so it can be returned by Collect_Primitive_Operations
10314 -- and be used in composing the equality operation of later types
10315 -- that have a component of the type.
10317 elsif Chars (S) = Name_Op_Eq
10318 and then Etype (S) = Standard_Boolean
10320 B_Typ := Base_Type (Etype (First_Formal (S)));
10322 if Scope (B_Typ) = Current_Scope
10324 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10325 and then not Is_Limited_Type (B_Typ)
10327 Is_Primitive := True;
10328 Set_Is_Primitive (S);
10329 Set_Has_Primitive_Operations (B_Typ);
10330 Check_Private_Overriding (B_Typ);
10332 -- The Ghost policy in effect at the point of declaration of a
10333 -- tagged type and a primitive operation must match
10334 -- (SPARK RM 6.9(16)).
10336 Check_Ghost_Primitive (S, B_Typ);
10339 end Check_For_Primitive_Subprogram;
10341 --------------------------------------
10342 -- Has_Matching_Entry_Or_Subprogram --
10343 --------------------------------------
10345 function Has_Matching_Entry_Or_Subprogram
10346 (E : Entity_Id) return Boolean
10348 function Check_Conforming_Parameters
10349 (E1_Param : Node_Id;
10350 E2_Param : Node_Id) return Boolean;
10351 -- Starting from the given parameters, check that all the parameters
10352 -- of two entries or subprograms are subtype conformant. Used to skip
10353 -- the check on the controlling argument.
10355 function Matching_Entry_Or_Subprogram
10356 (Conc_Typ : Entity_Id;
10357 Subp : Entity_Id) return Entity_Id;
10358 -- Return the first entry or subprogram of the given concurrent type
10359 -- whose name matches the name of Subp and has a profile conformant
10360 -- with Subp; return Empty if not found.
10362 function Matching_Dispatching_Subprogram
10363 (Conc_Typ : Entity_Id;
10364 Ent : Entity_Id) return Entity_Id;
10365 -- Return the first dispatching primitive of Conc_Type defined in the
10366 -- enclosing scope of Conc_Type (i.e. before the full definition of
10367 -- this concurrent type) whose name matches the entry Ent and has a
10368 -- profile conformant with the profile of the corresponding (not yet
10369 -- built) dispatching primitive of Ent; return Empty if not found.
10371 function Matching_Original_Protected_Subprogram
10372 (Prot_Typ : Entity_Id;
10373 Subp : Entity_Id) return Entity_Id;
10374 -- Return the first subprogram defined in the enclosing scope of
10375 -- Prot_Typ (before the full definition of this protected type)
10376 -- whose name matches the original name of Subp and has a profile
10377 -- conformant with the profile of Subp; return Empty if not found.
10379 ---------------------------------
10380 -- Check_Conforming_Parameters --
10381 ---------------------------------
10383 function Check_Conforming_Parameters
10384 (E1_Param : Node_Id;
10385 E2_Param : Node_Id) return Boolean
10387 Param_E1 : Node_Id := E1_Param;
10388 Param_E2 : Node_Id := E2_Param;
10391 while Present (Param_E1) and then Present (Param_E2) loop
10392 if Ekind (Defining_Identifier (Param_E1)) /=
10393 Ekind (Defining_Identifier (Param_E2))
10396 (Find_Parameter_Type (Param_E1),
10397 Find_Parameter_Type (Param_E2),
10398 Subtype_Conformant)
10407 -- The candidate is not valid if one of the two lists contains
10408 -- more parameters than the other
10410 return No (Param_E1) and then No (Param_E2);
10411 end Check_Conforming_Parameters;
10413 ----------------------------------
10414 -- Matching_Entry_Or_Subprogram --
10415 ----------------------------------
10417 function Matching_Entry_Or_Subprogram
10418 (Conc_Typ : Entity_Id;
10419 Subp : Entity_Id) return Entity_Id
10424 E := First_Entity (Conc_Typ);
10425 while Present (E) loop
10426 if Chars (Subp) = Chars (E)
10427 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10429 Check_Conforming_Parameters
10430 (First (Parameter_Specifications (Parent (E))),
10431 Next (First (Parameter_Specifications (Parent (Subp)))))
10440 end Matching_Entry_Or_Subprogram;
10442 -------------------------------------
10443 -- Matching_Dispatching_Subprogram --
10444 -------------------------------------
10446 function Matching_Dispatching_Subprogram
10447 (Conc_Typ : Entity_Id;
10448 Ent : Entity_Id) return Entity_Id
10453 -- Search for entities in the enclosing scope of this synchonized
10456 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10457 Push_Scope (Scope (Conc_Typ));
10458 E := Current_Entity_In_Scope (Ent);
10461 while Present (E) loop
10462 if Scope (E) = Scope (Conc_Typ)
10463 and then Comes_From_Source (E)
10464 and then Ekind (E) = E_Procedure
10465 and then Present (First_Entity (E))
10466 and then Is_Controlling_Formal (First_Entity (E))
10467 and then Etype (First_Entity (E)) = Conc_Typ
10469 Check_Conforming_Parameters
10470 (First (Parameter_Specifications (Parent (Ent))),
10471 Next (First (Parameter_Specifications (Parent (E)))))
10480 end Matching_Dispatching_Subprogram;
10482 --------------------------------------------
10483 -- Matching_Original_Protected_Subprogram --
10484 --------------------------------------------
10486 function Matching_Original_Protected_Subprogram
10487 (Prot_Typ : Entity_Id;
10488 Subp : Entity_Id) return Entity_Id
10490 ICF : constant Boolean :=
10491 Is_Controlling_Formal (First_Entity (Subp));
10495 -- Temporarily decorate the first parameter of Subp as controlling
10496 -- formal, required to invoke Subtype_Conformant.
10498 Set_Is_Controlling_Formal (First_Entity (Subp));
10501 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10503 while Present (E) loop
10504 if Scope (E) = Scope (Prot_Typ)
10505 and then Comes_From_Source (E)
10506 and then Ekind (Subp) = Ekind (E)
10507 and then Present (First_Entity (E))
10508 and then Is_Controlling_Formal (First_Entity (E))
10509 and then Etype (First_Entity (E)) = Prot_Typ
10510 and then Subtype_Conformant (Subp, E,
10511 Skip_Controlling_Formals => True)
10513 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10520 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10523 end Matching_Original_Protected_Subprogram;
10525 -- Start of processing for Has_Matching_Entry_Or_Subprogram
10528 -- Case 1: E is a subprogram whose first formal is a concurrent type
10529 -- defined in the scope of E that has an entry or subprogram whose
10530 -- profile matches E.
10532 if Comes_From_Source (E)
10533 and then Is_Subprogram (E)
10534 and then Present (First_Entity (E))
10535 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10538 Scope (Corresponding_Concurrent_Type
10539 (Etype (First_Entity (E))))
10542 (Matching_Entry_Or_Subprogram
10543 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10546 Report_Conflict (E,
10547 Matching_Entry_Or_Subprogram
10548 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10553 -- Case 2: E is an internally built dispatching subprogram of a
10554 -- protected type and there is a subprogram defined in the enclosing
10555 -- scope of the protected type that has the original name of E and
10556 -- its profile is conformant with the profile of E. We check the
10557 -- name of the original protected subprogram associated with E since
10558 -- the expander builds dispatching primitives of protected functions
10559 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
10561 elsif not Comes_From_Source (E)
10562 and then Is_Subprogram (E)
10563 and then Present (First_Entity (E))
10564 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10565 and then Present (Original_Protected_Subprogram (E))
10568 (Matching_Original_Protected_Subprogram
10569 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10572 Report_Conflict (E,
10573 Matching_Original_Protected_Subprogram
10574 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10578 -- Case 3: E is an entry of a synchronized type and a matching
10579 -- procedure has been previously defined in the enclosing scope
10580 -- of the synchronized type.
10582 elsif Comes_From_Source (E)
10583 and then Ekind (E) = E_Entry
10585 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
10587 Report_Conflict (E,
10588 Matching_Dispatching_Subprogram (Current_Scope, E));
10593 end Has_Matching_Entry_Or_Subprogram;
10595 ----------------------------
10596 -- Is_Private_Declaration --
10597 ----------------------------
10599 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10600 Decl : constant Node_Id := Unit_Declaration_Node (E);
10601 Priv_Decls : List_Id;
10604 if Is_Package_Or_Generic_Package (Current_Scope)
10605 and then In_Private_Part (Current_Scope)
10608 Private_Declarations (Package_Specification (Current_Scope));
10610 return In_Package_Body (Current_Scope)
10612 (Is_List_Member (Decl)
10613 and then List_Containing (Decl) = Priv_Decls)
10614 or else (Nkind (Parent (Decl)) = N_Package_Specification
10616 Is_Compilation_Unit
10617 (Defining_Entity (Parent (Decl)))
10618 and then List_Containing (Parent (Parent (Decl))) =
10623 end Is_Private_Declaration;
10625 --------------------------
10626 -- Is_Overriding_Alias --
10627 --------------------------
10629 function Is_Overriding_Alias
10630 (Old_E : Entity_Id;
10631 New_E : Entity_Id) return Boolean
10633 AO : constant Entity_Id := Alias (Old_E);
10634 AN : constant Entity_Id := Alias (New_E);
10637 return Scope (AO) /= Scope (AN)
10638 or else No (DTC_Entity (AO))
10639 or else No (DTC_Entity (AN))
10640 or else DT_Position (AO) = DT_Position (AN);
10641 end Is_Overriding_Alias;
10643 ---------------------
10644 -- Report_Conflict --
10645 ---------------------
10647 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
10649 Error_Msg_Sloc := Sloc (E);
10651 -- Generate message, with useful additional warning if in generic
10653 if Is_Generic_Unit (E) then
10654 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10655 Error_Msg_N ("\& conflicts with declaration#", S);
10657 Error_Msg_N ("& conflicts with declaration#", S);
10659 end Report_Conflict;
10661 -- Start of processing for New_Overloaded_Entity
10664 -- We need to look for an entity that S may override. This must be a
10665 -- homonym in the current scope, so we look for the first homonym of
10666 -- S in the current scope as the starting point for the search.
10668 E := Current_Entity_In_Scope (S);
10670 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10671 -- They are directly added to the list of primitive operations of
10672 -- Derived_Type, unless this is a rederivation in the private part
10673 -- of an operation that was already derived in the visible part of
10674 -- the current package.
10676 if Ada_Version >= Ada_2005
10677 and then Present (Derived_Type)
10678 and then Present (Alias (S))
10679 and then Is_Dispatching_Operation (Alias (S))
10680 and then Present (Find_Dispatching_Type (Alias (S)))
10681 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10683 -- For private types, when the full-view is processed we propagate to
10684 -- the full view the non-overridden entities whose attribute "alias"
10685 -- references an interface primitive. These entities were added by
10686 -- Derive_Subprograms to ensure that interface primitives are
10689 -- Inside_Freeze_Actions is non zero when S corresponds with an
10690 -- internal entity that links an interface primitive with its
10691 -- covering primitive through attribute Interface_Alias (see
10692 -- Add_Internal_Interface_Entities).
10694 if Inside_Freezing_Actions = 0
10695 and then Is_Package_Or_Generic_Package (Current_Scope)
10696 and then In_Private_Part (Current_Scope)
10697 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10698 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10699 and then Full_View (Defining_Identifier (Parent (E)))
10700 = Defining_Identifier (Parent (S))
10701 and then Alias (E) = Alias (S)
10703 Check_Operation_From_Private_View (S, E);
10704 Set_Is_Dispatching_Operation (S);
10709 Enter_Overloaded_Entity (S);
10710 Check_Dispatching_Operation (S, Empty);
10711 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10717 -- For synchronized types check conflicts of this entity with previously
10718 -- defined entities.
10720 if Ada_Version >= Ada_2005
10721 and then Has_Matching_Entry_Or_Subprogram (S)
10726 -- If there is no homonym then this is definitely not overriding
10729 Enter_Overloaded_Entity (S);
10730 Check_Dispatching_Operation (S, Empty);
10731 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10733 -- If subprogram has an explicit declaration, check whether it has an
10734 -- overriding indicator.
10736 if Comes_From_Source (S) then
10737 Check_Synchronized_Overriding (S, Overridden_Subp);
10739 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10740 -- it may have overridden some hidden inherited primitive. Update
10741 -- Overridden_Subp to avoid spurious errors when checking the
10742 -- overriding indicator.
10744 if Ada_Version >= Ada_2012
10745 and then No (Overridden_Subp)
10746 and then Is_Dispatching_Operation (S)
10747 and then Present (Overridden_Operation (S))
10749 Overridden_Subp := Overridden_Operation (S);
10752 Check_Overriding_Indicator
10753 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10755 -- The Ghost policy in effect at the point of declaration of a
10756 -- parent subprogram and an overriding subprogram must match
10757 -- (SPARK RM 6.9(17)).
10759 Check_Ghost_Overriding (S, Overridden_Subp);
10762 -- If there is a homonym that is not overloadable, then we have an
10763 -- error, except for the special cases checked explicitly below.
10765 elsif not Is_Overloadable (E) then
10767 -- Check for spurious conflict produced by a subprogram that has the
10768 -- same name as that of the enclosing generic package. The conflict
10769 -- occurs within an instance, between the subprogram and the renaming
10770 -- declaration for the package. After the subprogram, the package
10771 -- renaming declaration becomes hidden.
10773 if Ekind (E) = E_Package
10774 and then Present (Renamed_Object (E))
10775 and then Renamed_Object (E) = Current_Scope
10776 and then Nkind (Parent (Renamed_Object (E))) =
10777 N_Package_Specification
10778 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10781 Set_Is_Immediately_Visible (E, False);
10782 Enter_Overloaded_Entity (S);
10783 Set_Homonym (S, Homonym (E));
10784 Check_Dispatching_Operation (S, Empty);
10785 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10787 -- If the subprogram is implicit it is hidden by the previous
10788 -- declaration. However if it is dispatching, it must appear in the
10789 -- dispatch table anyway, because it can be dispatched to even if it
10790 -- cannot be called directly.
10792 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10793 Set_Scope (S, Current_Scope);
10795 if Is_Dispatching_Operation (Alias (S)) then
10796 Check_Dispatching_Operation (S, Empty);
10802 Report_Conflict (S, E);
10806 -- E exists and is overloadable
10809 Check_Synchronized_Overriding (S, Overridden_Subp);
10811 -- Loop through E and its homonyms to determine if any of them is
10812 -- the candidate for overriding by S.
10814 while Present (E) loop
10816 -- Definitely not interesting if not in the current scope
10818 if Scope (E) /= Current_Scope then
10821 -- A function can overload the name of an abstract state. The
10822 -- state can be viewed as a function with a profile that cannot
10823 -- be matched by anything.
10825 elsif Ekind (S) = E_Function
10826 and then Ekind (E) = E_Abstract_State
10828 Enter_Overloaded_Entity (S);
10831 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10832 -- procedures locate the internally generated spec. We enforce
10833 -- mode conformance since a tagged type may inherit from
10834 -- interfaces several null primitives which differ only in
10835 -- the mode of the formals.
10837 elsif not Comes_From_Source (S)
10838 and then Is_Null_Procedure (S)
10839 and then not Mode_Conformant (E, S)
10843 -- Check if we have type conformance
10845 elsif Type_Conformant (E, S) then
10847 -- If the old and new entities have the same profile and one
10848 -- is not the body of the other, then this is an error, unless
10849 -- one of them is implicitly declared.
10851 -- There are some cases when both can be implicit, for example
10852 -- when both a literal and a function that overrides it are
10853 -- inherited in a derivation, or when an inherited operation
10854 -- of a tagged full type overrides the inherited operation of
10855 -- a private extension. Ada 83 had a special rule for the
10856 -- literal case. In Ada 95, the later implicit operation hides
10857 -- the former, and the literal is always the former. In the
10858 -- odd case where both are derived operations declared at the
10859 -- same point, both operations should be declared, and in that
10860 -- case we bypass the following test and proceed to the next
10861 -- part. This can only occur for certain obscure cases in
10862 -- instances, when an operation on a type derived from a formal
10863 -- private type does not override a homograph inherited from
10864 -- the actual. In subsequent derivations of such a type, the
10865 -- DT positions of these operations remain distinct, if they
10868 if Present (Alias (S))
10869 and then (No (Alias (E))
10870 or else Comes_From_Source (E)
10871 or else Is_Abstract_Subprogram (S)
10873 (Is_Dispatching_Operation (E)
10874 and then Is_Overriding_Alias (E, S)))
10875 and then Ekind (E) /= E_Enumeration_Literal
10877 -- When an derived operation is overloaded it may be due to
10878 -- the fact that the full view of a private extension
10879 -- re-inherits. It has to be dealt with.
10881 if Is_Package_Or_Generic_Package (Current_Scope)
10882 and then In_Private_Part (Current_Scope)
10884 Check_Operation_From_Private_View (S, E);
10887 -- In any case the implicit operation remains hidden by the
10888 -- existing declaration, which is overriding. Indicate that
10889 -- E overrides the operation from which S is inherited.
10891 if Present (Alias (S)) then
10892 Set_Overridden_Operation (E, Alias (S));
10893 Inherit_Subprogram_Contract (E, Alias (S));
10896 Set_Overridden_Operation (E, S);
10897 Inherit_Subprogram_Contract (E, S);
10900 if Comes_From_Source (E) then
10901 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10903 -- The Ghost policy in effect at the point of declaration
10904 -- of a parent subprogram and an overriding subprogram
10905 -- must match (SPARK RM 6.9(17)).
10907 Check_Ghost_Overriding (E, S);
10912 -- Within an instance, the renaming declarations for actual
10913 -- subprograms may become ambiguous, but they do not hide each
10916 elsif Ekind (E) /= E_Entry
10917 and then not Comes_From_Source (E)
10918 and then not Is_Generic_Instance (E)
10919 and then (Present (Alias (E))
10920 or else Is_Intrinsic_Subprogram (E))
10921 and then (not In_Instance
10922 or else No (Parent (E))
10923 or else Nkind (Unit_Declaration_Node (E)) /=
10924 N_Subprogram_Renaming_Declaration)
10926 -- A subprogram child unit is not allowed to override an
10927 -- inherited subprogram (10.1.1(20)).
10929 if Is_Child_Unit (S) then
10931 ("child unit overrides inherited subprogram in parent",
10936 if Is_Non_Overriding_Operation (E, S) then
10937 Enter_Overloaded_Entity (S);
10939 if No (Derived_Type)
10940 or else Is_Tagged_Type (Derived_Type)
10942 Check_Dispatching_Operation (S, Empty);
10948 -- E is a derived operation or an internal operator which
10949 -- is being overridden. Remove E from further visibility.
10950 -- Furthermore, if E is a dispatching operation, it must be
10951 -- replaced in the list of primitive operations of its type
10952 -- (see Override_Dispatching_Operation).
10954 Overridden_Subp := E;
10960 Prev := First_Entity (Current_Scope);
10961 while Present (Prev) and then Next_Entity (Prev) /= E loop
10962 Next_Entity (Prev);
10965 -- It is possible for E to be in the current scope and
10966 -- yet not in the entity chain. This can only occur in a
10967 -- generic context where E is an implicit concatenation
10968 -- in the formal part, because in a generic body the
10969 -- entity chain starts with the formals.
10971 -- In GNATprove mode, a wrapper for an operation with
10972 -- axiomatization may be a homonym of another declaration
10973 -- for an actual subprogram (needs refinement ???).
10977 and then GNATprove_Mode
10979 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10980 N_Subprogram_Renaming_Declaration
10984 pragma Assert (Chars (E) = Name_Op_Concat);
10989 -- E must be removed both from the entity_list of the
10990 -- current scope, and from the visibility chain.
10992 if Debug_Flag_E then
10993 Write_Str ("Override implicit operation ");
10994 Write_Int (Int (E));
10998 -- If E is a predefined concatenation, it stands for four
10999 -- different operations. As a result, a single explicit
11000 -- declaration does not hide it. In a possible ambiguous
11001 -- situation, Disambiguate chooses the user-defined op,
11002 -- so it is correct to retain the previous internal one.
11004 if Chars (E) /= Name_Op_Concat
11005 or else Ekind (E) /= E_Operator
11007 -- For nondispatching derived operations that are
11008 -- overridden by a subprogram declared in the private
11009 -- part of a package, we retain the derived subprogram
11010 -- but mark it as not immediately visible. If the
11011 -- derived operation was declared in the visible part
11012 -- then this ensures that it will still be visible
11013 -- outside the package with the proper signature
11014 -- (calls from outside must also be directed to this
11015 -- version rather than the overriding one, unlike the
11016 -- dispatching case). Calls from inside the package
11017 -- will still resolve to the overriding subprogram
11018 -- since the derived one is marked as not visible
11019 -- within the package.
11021 -- If the private operation is dispatching, we achieve
11022 -- the overriding by keeping the implicit operation
11023 -- but setting its alias to be the overriding one. In
11024 -- this fashion the proper body is executed in all
11025 -- cases, but the original signature is used outside
11028 -- If the overriding is not in the private part, we
11029 -- remove the implicit operation altogether.
11031 if Is_Private_Declaration (S) then
11032 if not Is_Dispatching_Operation (E) then
11033 Set_Is_Immediately_Visible (E, False);
11035 -- Work done in Override_Dispatching_Operation,
11036 -- so nothing else needs to be done here.
11042 -- Find predecessor of E in Homonym chain
11044 if E = Current_Entity (E) then
11047 Prev_Vis := Current_Entity (E);
11048 while Homonym (Prev_Vis) /= E loop
11049 Prev_Vis := Homonym (Prev_Vis);
11053 if Prev_Vis /= Empty then
11055 -- Skip E in the visibility chain
11057 Set_Homonym (Prev_Vis, Homonym (E));
11060 Set_Name_Entity_Id (Chars (E), Homonym (E));
11063 Set_Next_Entity (Prev, Next_Entity (E));
11065 if No (Next_Entity (Prev)) then
11066 Set_Last_Entity (Current_Scope, Prev);
11071 Enter_Overloaded_Entity (S);
11073 -- For entities generated by Derive_Subprograms the
11074 -- overridden operation is the inherited primitive
11075 -- (which is available through the attribute alias).
11077 if not (Comes_From_Source (E))
11078 and then Is_Dispatching_Operation (E)
11079 and then Find_Dispatching_Type (E) =
11080 Find_Dispatching_Type (S)
11081 and then Present (Alias (E))
11082 and then Comes_From_Source (Alias (E))
11084 Set_Overridden_Operation (S, Alias (E));
11085 Inherit_Subprogram_Contract (S, Alias (E));
11087 -- Normal case of setting entity as overridden
11089 -- Note: Static_Initialization and Overridden_Operation
11090 -- attributes use the same field in subprogram entities.
11091 -- Static_Initialization is only defined for internal
11092 -- initialization procedures, where Overridden_Operation
11093 -- is irrelevant. Therefore the setting of this attribute
11094 -- must check whether the target is an init_proc.
11096 elsif not Is_Init_Proc (S) then
11097 Set_Overridden_Operation (S, E);
11098 Inherit_Subprogram_Contract (S, E);
11101 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11103 -- The Ghost policy in effect at the point of declaration
11104 -- of a parent subprogram and an overriding subprogram
11105 -- must match (SPARK RM 6.9(17)).
11107 Check_Ghost_Overriding (S, E);
11109 -- If S is a user-defined subprogram or a null procedure
11110 -- expanded to override an inherited null procedure, or a
11111 -- predefined dispatching primitive then indicate that E
11112 -- overrides the operation from which S is inherited.
11114 if Comes_From_Source (S)
11116 (Present (Parent (S))
11118 Nkind (Parent (S)) = N_Procedure_Specification
11120 Null_Present (Parent (S)))
11122 (Present (Alias (E))
11124 Is_Predefined_Dispatching_Operation (Alias (E)))
11126 if Present (Alias (E)) then
11127 Set_Overridden_Operation (S, Alias (E));
11128 Inherit_Subprogram_Contract (S, Alias (E));
11132 if Is_Dispatching_Operation (E) then
11134 -- An overriding dispatching subprogram inherits the
11135 -- convention of the overridden subprogram (AI-117).
11137 Set_Convention (S, Convention (E));
11138 Check_Dispatching_Operation (S, E);
11141 Check_Dispatching_Operation (S, Empty);
11144 Check_For_Primitive_Subprogram
11145 (Is_Primitive_Subp, Is_Overriding => True);
11146 goto Check_Inequality;
11149 -- Apparent redeclarations in instances can occur when two
11150 -- formal types get the same actual type. The subprograms in
11151 -- in the instance are legal, even if not callable from the
11152 -- outside. Calls from within are disambiguated elsewhere.
11153 -- For dispatching operations in the visible part, the usual
11154 -- rules apply, and operations with the same profile are not
11155 -- legal (B830001).
11157 elsif (In_Instance_Visible_Part
11158 and then not Is_Dispatching_Operation (E))
11159 or else In_Instance_Not_Visible
11163 -- Here we have a real error (identical profile)
11166 Error_Msg_Sloc := Sloc (E);
11168 -- Avoid cascaded errors if the entity appears in
11169 -- subsequent calls.
11171 Set_Scope (S, Current_Scope);
11173 -- Generate error, with extra useful warning for the case
11174 -- of a generic instance with no completion.
11176 if Is_Generic_Instance (S)
11177 and then not Has_Completion (E)
11180 ("instantiation cannot provide body for&", S);
11181 Error_Msg_N ("\& conflicts with declaration#", S);
11183 Error_Msg_N ("& conflicts with declaration#", S);
11190 -- If one subprogram has an access parameter and the other
11191 -- a parameter of an access type, calls to either might be
11192 -- ambiguous. Verify that parameters match except for the
11193 -- access parameter.
11195 if May_Hide_Profile then
11201 F1 := First_Formal (S);
11202 F2 := First_Formal (E);
11203 while Present (F1) and then Present (F2) loop
11204 if Is_Access_Type (Etype (F1)) then
11205 if not Is_Access_Type (Etype (F2))
11206 or else not Conforming_Types
11207 (Designated_Type (Etype (F1)),
11208 Designated_Type (Etype (F2)),
11211 May_Hide_Profile := False;
11215 not Conforming_Types
11216 (Etype (F1), Etype (F2), Type_Conformant)
11218 May_Hide_Profile := False;
11225 if May_Hide_Profile
11229 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11238 -- On exit, we know that S is a new entity
11240 Enter_Overloaded_Entity (S);
11241 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11242 Check_Overriding_Indicator
11243 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11245 -- The Ghost policy in effect at the point of declaration of a parent
11246 -- subprogram and an overriding subprogram must match
11247 -- (SPARK RM 6.9(17)).
11249 Check_Ghost_Overriding (S, Overridden_Subp);
11251 -- Overloading is not allowed in SPARK, except for operators
11253 if Nkind (S) /= N_Defining_Operator_Symbol then
11254 Error_Msg_Sloc := Sloc (Homonym (S));
11255 Check_SPARK_05_Restriction
11256 ("overloading not allowed with entity#", S);
11259 -- If S is a derived operation for an untagged type then by
11260 -- definition it's not a dispatching operation (even if the parent
11261 -- operation was dispatching), so Check_Dispatching_Operation is not
11262 -- called in that case.
11264 if No (Derived_Type)
11265 or else Is_Tagged_Type (Derived_Type)
11267 Check_Dispatching_Operation (S, Empty);
11271 -- If this is a user-defined equality operator that is not a derived
11272 -- subprogram, create the corresponding inequality. If the operation is
11273 -- dispatching, the expansion is done elsewhere, and we do not create
11274 -- an explicit inequality operation.
11276 <<Check_Inequality>>
11277 if Chars (S) = Name_Op_Eq
11278 and then Etype (S) = Standard_Boolean
11279 and then Present (Parent (S))
11280 and then not Is_Dispatching_Operation (S)
11282 Make_Inequality_Operator (S);
11283 Check_Untagged_Equality (S);
11285 end New_Overloaded_Entity;
11287 ---------------------
11288 -- Process_Formals --
11289 ---------------------
11291 procedure Process_Formals
11293 Related_Nod : Node_Id)
11295 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11296 -- Determine whether an access type designates a type coming from a
11299 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11300 -- Check whether the default has a class-wide type. After analysis the
11301 -- default has the type of the formal, so we must also check explicitly
11302 -- for an access attribute.
11304 ----------------------------------
11305 -- Designates_From_Limited_With --
11306 ----------------------------------
11308 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11309 Desig : Entity_Id := Typ;
11312 if Is_Access_Type (Desig) then
11313 Desig := Directly_Designated_Type (Desig);
11316 if Is_Class_Wide_Type (Desig) then
11317 Desig := Root_Type (Desig);
11321 Ekind (Desig) = E_Incomplete_Type
11322 and then From_Limited_With (Desig);
11323 end Designates_From_Limited_With;
11325 ---------------------------
11326 -- Is_Class_Wide_Default --
11327 ---------------------------
11329 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11331 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11332 or else (Nkind (D) = N_Attribute_Reference
11333 and then Attribute_Name (D) = Name_Access
11334 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11335 end Is_Class_Wide_Default;
11339 Context : constant Node_Id := Parent (Parent (T));
11341 Formal : Entity_Id;
11342 Formal_Type : Entity_Id;
11343 Param_Spec : Node_Id;
11346 Num_Out_Params : Nat := 0;
11347 First_Out_Param : Entity_Id := Empty;
11348 -- Used for setting Is_Only_Out_Parameter
11350 -- Start of processing for Process_Formals
11353 -- In order to prevent premature use of the formals in the same formal
11354 -- part, the Ekind is left undefined until all default expressions are
11355 -- analyzed. The Ekind is established in a separate loop at the end.
11357 Param_Spec := First (T);
11358 while Present (Param_Spec) loop
11359 Formal := Defining_Identifier (Param_Spec);
11360 Set_Never_Set_In_Source (Formal, True);
11361 Enter_Name (Formal);
11363 -- Case of ordinary parameters
11365 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11366 Find_Type (Parameter_Type (Param_Spec));
11367 Ptype := Parameter_Type (Param_Spec);
11369 if Ptype = Error then
11373 Formal_Type := Entity (Ptype);
11375 if Is_Incomplete_Type (Formal_Type)
11377 (Is_Class_Wide_Type (Formal_Type)
11378 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11380 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11381 -- primitive operations, as long as their completion is
11382 -- in the same declarative part. If in the private part
11383 -- this means that the type cannot be a Taft-amendment type.
11384 -- Check is done on package exit. For access to subprograms,
11385 -- the use is legal for Taft-amendment types.
11387 -- Ada 2012: tagged incomplete types are allowed as generic
11388 -- formal types. They do not introduce dependencies and the
11389 -- corresponding generic subprogram does not have a delayed
11390 -- freeze, because it does not need a freeze node. However,
11391 -- it is still the case that untagged incomplete types cannot
11392 -- be Taft-amendment types and must be completed in private
11393 -- part, so the subprogram must appear in the list of private
11394 -- dependents of the type.
11396 if Is_Tagged_Type (Formal_Type)
11397 or else (Ada_Version >= Ada_2012
11398 and then not From_Limited_With (Formal_Type)
11399 and then not Is_Generic_Type (Formal_Type))
11401 if Ekind (Scope (Current_Scope)) = E_Package
11402 and then not Is_Generic_Type (Formal_Type)
11403 and then not Is_Class_Wide_Type (Formal_Type)
11406 (Parent (T), N_Access_Function_Definition,
11407 N_Access_Procedure_Definition)
11409 Append_Elmt (Current_Scope,
11410 Private_Dependents (Base_Type (Formal_Type)));
11412 -- Freezing is delayed to ensure that Register_Prim
11413 -- will get called for this operation, which is needed
11414 -- in cases where static dispatch tables aren't built.
11415 -- (Note that the same is done for controlling access
11416 -- parameter cases in function Access_Definition.)
11418 if not Is_Thunk (Current_Scope) then
11419 Set_Has_Delayed_Freeze (Current_Scope);
11424 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11425 N_Access_Procedure_Definition)
11427 -- AI05-0151: Tagged incomplete types are allowed in all
11428 -- formal parts. Untagged incomplete types are not allowed
11429 -- in bodies. Limited views of either kind are not allowed
11430 -- if there is no place at which the non-limited view can
11431 -- become available.
11433 -- Incomplete formal untagged types are not allowed in
11434 -- subprogram bodies (but are legal in their declarations).
11435 -- This excludes bodies created for null procedures, which
11436 -- are basic declarations.
11438 if Is_Generic_Type (Formal_Type)
11439 and then not Is_Tagged_Type (Formal_Type)
11440 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11443 ("invalid use of formal incomplete type", Param_Spec);
11445 elsif Ada_Version >= Ada_2012 then
11446 if Is_Tagged_Type (Formal_Type)
11447 and then (not From_Limited_With (Formal_Type)
11448 or else not In_Package_Body)
11452 elsif Nkind_In (Context, N_Accept_Statement,
11453 N_Accept_Alternative,
11455 or else (Nkind (Context) = N_Subprogram_Body
11456 and then Comes_From_Source (Context))
11459 ("invalid use of untagged incomplete type &",
11460 Ptype, Formal_Type);
11465 ("invalid use of incomplete type&",
11466 Param_Spec, Formal_Type);
11468 -- Further checks on the legality of incomplete types
11469 -- in formal parts are delayed until the freeze point
11470 -- of the enclosing subprogram or access to subprogram.
11474 elsif Ekind (Formal_Type) = E_Void then
11476 ("premature use of&",
11477 Parameter_Type (Param_Spec), Formal_Type);
11480 -- Ada 2012 (AI-142): Handle aliased parameters
11482 if Ada_Version >= Ada_2012
11483 and then Aliased_Present (Param_Spec)
11485 Set_Is_Aliased (Formal);
11488 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11489 -- declaration corresponding to the null-excluding type of the
11490 -- formal in the enclosing scope. Finally, replace the parameter
11491 -- type of the formal with the internal subtype.
11493 if Ada_Version >= Ada_2005
11494 and then Null_Exclusion_Present (Param_Spec)
11496 if not Is_Access_Type (Formal_Type) then
11498 ("`NOT NULL` allowed only for an access type", Param_Spec);
11501 if Can_Never_Be_Null (Formal_Type)
11502 and then Comes_From_Source (Related_Nod)
11505 ("`NOT NULL` not allowed (& already excludes null)",
11506 Param_Spec, Formal_Type);
11510 Create_Null_Excluding_Itype
11512 Related_Nod => Related_Nod,
11513 Scope_Id => Scope (Current_Scope));
11515 -- If the designated type of the itype is an itype that is
11516 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11517 -- on the access subtype, to prevent order-of-elaboration
11518 -- issues in the backend.
11521 -- type T is access procedure;
11522 -- procedure Op (O : not null T);
11524 if Is_Itype (Directly_Designated_Type (Formal_Type))
11526 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11528 Set_Has_Delayed_Freeze (Formal_Type);
11533 -- An access formal type
11537 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11539 -- No need to continue if we already notified errors
11541 if not Present (Formal_Type) then
11545 -- Ada 2005 (AI-254)
11548 AD : constant Node_Id :=
11549 Access_To_Subprogram_Definition
11550 (Parameter_Type (Param_Spec));
11552 if Present (AD) and then Protected_Present (AD) then
11554 Replace_Anonymous_Access_To_Protected_Subprogram
11560 Set_Etype (Formal, Formal_Type);
11562 -- Deal with default expression if present
11564 Default := Expression (Param_Spec);
11566 if Present (Default) then
11567 Check_SPARK_05_Restriction
11568 ("default expression is not allowed", Default);
11570 if Out_Present (Param_Spec) then
11572 ("default initialization only allowed for IN parameters",
11576 -- Do the special preanalysis of the expression (see section on
11577 -- "Handling of Default Expressions" in the spec of package Sem).
11579 Preanalyze_Spec_Expression (Default, Formal_Type);
11581 -- An access to constant cannot be the default for
11582 -- an access parameter that is an access to variable.
11584 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11585 and then not Is_Access_Constant (Formal_Type)
11586 and then Is_Access_Type (Etype (Default))
11587 and then Is_Access_Constant (Etype (Default))
11590 ("formal that is access to variable cannot be initialized "
11591 & "with an access-to-constant expression", Default);
11594 -- Check that the designated type of an access parameter's default
11595 -- is not a class-wide type unless the parameter's designated type
11596 -- is also class-wide.
11598 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11599 and then not Designates_From_Limited_With (Formal_Type)
11600 and then Is_Class_Wide_Default (Default)
11601 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11604 ("access to class-wide expression not allowed here", Default);
11607 -- Check incorrect use of dynamically tagged expressions
11609 if Is_Tagged_Type (Formal_Type) then
11610 Check_Dynamically_Tagged_Expression
11612 Typ => Formal_Type,
11613 Related_Nod => Default);
11617 -- Ada 2005 (AI-231): Static checks
11619 if Ada_Version >= Ada_2005
11620 and then Is_Access_Type (Etype (Formal))
11621 and then Can_Never_Be_Null (Etype (Formal))
11623 Null_Exclusion_Static_Checks (Param_Spec);
11626 -- The following checks are relevant only when SPARK_Mode is on as
11627 -- these are not standard Ada legality rules.
11629 if SPARK_Mode = On then
11630 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11632 -- A function cannot have a parameter of mode IN OUT or OUT
11635 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11637 ("function cannot have parameter of mode `OUT` or "
11638 & "`IN OUT`", Formal);
11641 -- A procedure cannot have an effectively volatile formal
11642 -- parameter of mode IN because it behaves as a constant
11643 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
11645 elsif Ekind (Scope (Formal)) = E_Procedure
11646 and then Ekind (Formal) = E_In_Parameter
11647 and then Is_Effectively_Volatile (Formal)
11650 ("formal parameter of mode `IN` cannot be volatile", Formal);
11658 -- If this is the formal part of a function specification, analyze the
11659 -- subtype mark in the context where the formals are visible but not
11660 -- yet usable, and may hide outer homographs.
11662 if Nkind (Related_Nod) = N_Function_Specification then
11663 Analyze_Return_Type (Related_Nod);
11666 -- Now set the kind (mode) of each formal
11668 Param_Spec := First (T);
11669 while Present (Param_Spec) loop
11670 Formal := Defining_Identifier (Param_Spec);
11671 Set_Formal_Mode (Formal);
11673 if Ekind (Formal) = E_In_Parameter then
11674 Set_Default_Value (Formal, Expression (Param_Spec));
11676 if Present (Expression (Param_Spec)) then
11677 Default := Expression (Param_Spec);
11679 if Is_Scalar_Type (Etype (Default)) then
11680 if Nkind (Parameter_Type (Param_Spec)) /=
11681 N_Access_Definition
11683 Formal_Type := Entity (Parameter_Type (Param_Spec));
11687 (Related_Nod, Parameter_Type (Param_Spec));
11690 Apply_Scalar_Range_Check (Default, Formal_Type);
11694 elsif Ekind (Formal) = E_Out_Parameter then
11695 Num_Out_Params := Num_Out_Params + 1;
11697 if Num_Out_Params = 1 then
11698 First_Out_Param := Formal;
11701 elsif Ekind (Formal) = E_In_Out_Parameter then
11702 Num_Out_Params := Num_Out_Params + 1;
11705 -- Skip remaining processing if formal type was in error
11707 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11708 goto Next_Parameter;
11711 -- Force call by reference if aliased
11714 Conv : constant Convention_Id := Convention (Etype (Formal));
11716 if Is_Aliased (Formal) then
11717 Set_Mechanism (Formal, By_Reference);
11719 -- Warn if user asked this to be passed by copy
11721 if Conv = Convention_Ada_Pass_By_Copy then
11723 ("cannot pass aliased parameter & by copy??", Formal);
11726 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11728 elsif Conv = Convention_Ada_Pass_By_Copy then
11729 Set_Mechanism (Formal, By_Copy);
11731 elsif Conv = Convention_Ada_Pass_By_Reference then
11732 Set_Mechanism (Formal, By_Reference);
11740 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11741 Set_Is_Only_Out_Parameter (First_Out_Param);
11743 end Process_Formals;
11745 ----------------------------
11746 -- Reference_Body_Formals --
11747 ----------------------------
11749 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11754 if Error_Posted (Spec) then
11758 -- Iterate over both lists. They may be of different lengths if the two
11759 -- specs are not conformant.
11761 Fs := First_Formal (Spec);
11762 Fb := First_Formal (Bod);
11763 while Present (Fs) and then Present (Fb) loop
11764 Generate_Reference (Fs, Fb, 'b');
11766 if Style_Check then
11767 Style.Check_Identifier (Fb, Fs);
11770 Set_Spec_Entity (Fb, Fs);
11771 Set_Referenced (Fs, False);
11775 end Reference_Body_Formals;
11777 -------------------------
11778 -- Set_Actual_Subtypes --
11779 -------------------------
11781 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11783 Formal : Entity_Id;
11785 First_Stmt : Node_Id := Empty;
11786 AS_Needed : Boolean;
11789 -- If this is an empty initialization procedure, no need to create
11790 -- actual subtypes (small optimization).
11792 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11795 -- Within a predicate function we do not want to generate local
11796 -- subtypes that may generate nested predicate functions.
11798 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
11802 -- The subtype declarations may freeze the formals. The body generated
11803 -- for an expression function is not a freeze point, so do not emit
11804 -- these declarations (small loss of efficiency in rare cases).
11806 if Nkind (N) = N_Subprogram_Body
11807 and then Was_Expression_Function (N)
11812 Formal := First_Formal (Subp);
11813 while Present (Formal) loop
11814 T := Etype (Formal);
11816 -- We never need an actual subtype for a constrained formal
11818 if Is_Constrained (T) then
11819 AS_Needed := False;
11821 -- If we have unknown discriminants, then we do not need an actual
11822 -- subtype, or more accurately we cannot figure it out. Note that
11823 -- all class-wide types have unknown discriminants.
11825 elsif Has_Unknown_Discriminants (T) then
11826 AS_Needed := False;
11828 -- At this stage we have an unconstrained type that may need an
11829 -- actual subtype. For sure the actual subtype is needed if we have
11830 -- an unconstrained array type. However, in an instance, the type
11831 -- may appear as a subtype of the full view, while the actual is
11832 -- in fact private (in which case no actual subtype is needed) so
11833 -- check the kind of the base type.
11835 elsif Is_Array_Type (Base_Type (T)) then
11838 -- The only other case needing an actual subtype is an unconstrained
11839 -- record type which is an IN parameter (we cannot generate actual
11840 -- subtypes for the OUT or IN OUT case, since an assignment can
11841 -- change the discriminant values. However we exclude the case of
11842 -- initialization procedures, since discriminants are handled very
11843 -- specially in this context, see the section entitled "Handling of
11844 -- Discriminants" in Einfo.
11846 -- We also exclude the case of Discrim_SO_Functions (functions used
11847 -- in front-end layout mode for size/offset values), since in such
11848 -- functions only discriminants are referenced, and not only are such
11849 -- subtypes not needed, but they cannot always be generated, because
11850 -- of order of elaboration issues.
11852 elsif Is_Record_Type (T)
11853 and then Ekind (Formal) = E_In_Parameter
11854 and then Chars (Formal) /= Name_uInit
11855 and then not Is_Unchecked_Union (T)
11856 and then not Is_Discrim_SO_Function (Subp)
11860 -- All other cases do not need an actual subtype
11863 AS_Needed := False;
11866 -- Generate actual subtypes for unconstrained arrays and
11867 -- unconstrained discriminated records.
11870 if Nkind (N) = N_Accept_Statement then
11872 -- If expansion is active, the formal is replaced by a local
11873 -- variable that renames the corresponding entry of the
11874 -- parameter block, and it is this local variable that may
11875 -- require an actual subtype.
11877 if Expander_Active then
11878 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11880 Decl := Build_Actual_Subtype (T, Formal);
11883 if Present (Handled_Statement_Sequence (N)) then
11885 First (Statements (Handled_Statement_Sequence (N)));
11886 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11887 Mark_Rewrite_Insertion (Decl);
11889 -- If the accept statement has no body, there will be no
11890 -- reference to the actuals, so no need to compute actual
11897 Decl := Build_Actual_Subtype (T, Formal);
11898 Prepend (Decl, Declarations (N));
11899 Mark_Rewrite_Insertion (Decl);
11902 -- The declaration uses the bounds of an existing object, and
11903 -- therefore needs no constraint checks.
11905 Analyze (Decl, Suppress => All_Checks);
11906 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
11908 -- We need to freeze manually the generated type when it is
11909 -- inserted anywhere else than in a declarative part.
11911 if Present (First_Stmt) then
11912 Insert_List_Before_And_Analyze (First_Stmt,
11913 Freeze_Entity (Defining_Identifier (Decl), N));
11915 -- Ditto if the type has a dynamic predicate, because the
11916 -- generated function will mention the actual subtype. The
11917 -- predicate may come from an explicit aspect of be inherited.
11919 elsif Has_Predicates (T) then
11920 Insert_List_Before_And_Analyze (Decl,
11921 Freeze_Entity (Defining_Identifier (Decl), N));
11924 if Nkind (N) = N_Accept_Statement
11925 and then Expander_Active
11927 Set_Actual_Subtype (Renamed_Object (Formal),
11928 Defining_Identifier (Decl));
11930 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11934 Next_Formal (Formal);
11936 end Set_Actual_Subtypes;
11938 ---------------------
11939 -- Set_Formal_Mode --
11940 ---------------------
11942 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11943 Spec : constant Node_Id := Parent (Formal_Id);
11944 Id : constant Entity_Id := Scope (Formal_Id);
11947 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11948 -- since we ensure that corresponding actuals are always valid at the
11949 -- point of the call.
11951 if Out_Present (Spec) then
11952 if Ekind_In (Id, E_Entry, E_Entry_Family)
11953 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11955 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11958 if Ekind_In (Id, E_Function, E_Generic_Function) then
11960 -- [IN] OUT parameters allowed for functions in Ada 2012
11962 if Ada_Version >= Ada_2012 then
11964 -- Even in Ada 2012 operators can only have IN parameters
11966 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11967 Error_Msg_N ("operators can only have IN parameters", Spec);
11970 if In_Present (Spec) then
11971 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11973 Set_Ekind (Formal_Id, E_Out_Parameter);
11976 -- But not in earlier versions of Ada
11979 Error_Msg_N ("functions can only have IN parameters", Spec);
11980 Set_Ekind (Formal_Id, E_In_Parameter);
11983 elsif In_Present (Spec) then
11984 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11987 Set_Ekind (Formal_Id, E_Out_Parameter);
11988 Set_Never_Set_In_Source (Formal_Id, True);
11989 Set_Is_True_Constant (Formal_Id, False);
11990 Set_Current_Value (Formal_Id, Empty);
11994 Set_Ekind (Formal_Id, E_In_Parameter);
11997 -- Set Is_Known_Non_Null for access parameters since the language
11998 -- guarantees that access parameters are always non-null. We also set
11999 -- Can_Never_Be_Null, since there is no way to change the value.
12001 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12003 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12004 -- null; In Ada 2005, only if then null_exclusion is explicit.
12006 if Ada_Version < Ada_2005
12007 or else Can_Never_Be_Null (Etype (Formal_Id))
12009 Set_Is_Known_Non_Null (Formal_Id);
12010 Set_Can_Never_Be_Null (Formal_Id);
12013 -- Ada 2005 (AI-231): Null-exclusion access subtype
12015 elsif Is_Access_Type (Etype (Formal_Id))
12016 and then Can_Never_Be_Null (Etype (Formal_Id))
12018 Set_Is_Known_Non_Null (Formal_Id);
12020 -- We can also set Can_Never_Be_Null (thus preventing some junk
12021 -- access checks) for the case of an IN parameter, which cannot
12022 -- be changed, or for an IN OUT parameter, which can be changed but
12023 -- not to a null value. But for an OUT parameter, the initial value
12024 -- passed in can be null, so we can't set this flag in that case.
12026 if Ekind (Formal_Id) /= E_Out_Parameter then
12027 Set_Can_Never_Be_Null (Formal_Id);
12031 Set_Mechanism (Formal_Id, Default_Mechanism);
12032 Set_Formal_Validity (Formal_Id);
12033 end Set_Formal_Mode;
12035 -------------------------
12036 -- Set_Formal_Validity --
12037 -------------------------
12039 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12041 -- If no validity checking, then we cannot assume anything about the
12042 -- validity of parameters, since we do not know there is any checking
12043 -- of the validity on the call side.
12045 if not Validity_Checks_On then
12048 -- If validity checking for parameters is enabled, this means we are
12049 -- not supposed to make any assumptions about argument values.
12051 elsif Validity_Check_Parameters then
12054 -- If we are checking in parameters, we will assume that the caller is
12055 -- also checking parameters, so we can assume the parameter is valid.
12057 elsif Ekind (Formal_Id) = E_In_Parameter
12058 and then Validity_Check_In_Params
12060 Set_Is_Known_Valid (Formal_Id, True);
12062 -- Similar treatment for IN OUT parameters
12064 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12065 and then Validity_Check_In_Out_Params
12067 Set_Is_Known_Valid (Formal_Id, True);
12069 end Set_Formal_Validity;
12071 ------------------------
12072 -- Subtype_Conformant --
12073 ------------------------
12075 function Subtype_Conformant
12076 (New_Id : Entity_Id;
12077 Old_Id : Entity_Id;
12078 Skip_Controlling_Formals : Boolean := False) return Boolean
12082 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12083 Skip_Controlling_Formals => Skip_Controlling_Formals);
12085 end Subtype_Conformant;
12087 ---------------------
12088 -- Type_Conformant --
12089 ---------------------
12091 function Type_Conformant
12092 (New_Id : Entity_Id;
12093 Old_Id : Entity_Id;
12094 Skip_Controlling_Formals : Boolean := False) return Boolean
12098 May_Hide_Profile := False;
12100 (New_Id, Old_Id, Type_Conformant, False, Result,
12101 Skip_Controlling_Formals => Skip_Controlling_Formals);
12103 end Type_Conformant;
12105 -------------------------------
12106 -- Valid_Operator_Definition --
12107 -------------------------------
12109 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12112 Id : constant Name_Id := Chars (Designator);
12116 F := First_Formal (Designator);
12117 while Present (F) loop
12120 if Present (Default_Value (F)) then
12122 ("default values not allowed for operator parameters",
12125 -- For function instantiations that are operators, we must check
12126 -- separately that the corresponding generic only has in-parameters.
12127 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12128 -- an error could not arise in earlier versions of the language.
12130 elsif Ekind (F) /= E_In_Parameter then
12131 Error_Msg_N ("operators can only have IN parameters", F);
12137 -- Verify that user-defined operators have proper number of arguments
12138 -- First case of operators which can only be unary
12140 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12143 -- Case of operators which can be unary or binary
12145 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12146 N_OK := (N in 1 .. 2);
12148 -- All other operators can only be binary
12156 ("incorrect number of arguments for operator", Designator);
12160 and then Base_Type (Etype (Designator)) = Standard_Boolean
12161 and then not Is_Intrinsic_Subprogram (Designator)
12164 ("explicit definition of inequality not allowed", Designator);
12166 end Valid_Operator_Definition;