1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Tss; use Exp_Tss;
41 with Exp_Util; use Exp_Util;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Inline; use Inline;
45 with Itypes; use Itypes;
46 with Lib.Xref; use Lib.Xref;
47 with Layout; use Layout;
48 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Output; use Output;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch9; use Sem_Ch9;
65 with Sem_Ch10; use Sem_Ch10;
66 with Sem_Ch12; use Sem_Ch12;
67 with Sem_Ch13; use Sem_Ch13;
68 with Sem_Dim; use Sem_Dim;
69 with Sem_Disp; use Sem_Disp;
70 with Sem_Dist; use Sem_Dist;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Prag; use Sem_Prag;
75 with Sem_Res; use Sem_Res;
76 with Sem_Util; use Sem_Util;
77 with Sem_Type; use Sem_Type;
78 with Sem_Warn; use Sem_Warn;
79 with Sinput; use Sinput;
80 with Stand; use Stand;
81 with Sinfo; use Sinfo;
82 with Sinfo.CN; use Sinfo.CN;
83 with Snames; use Snames;
84 with Stringt; use Stringt;
86 with Stylesw; use Stylesw;
87 with Tbuild; use Tbuild;
88 with Uintp; use Uintp;
89 with Urealp; use Urealp;
90 with Validsw; use Validsw;
92 package body Sem_Ch6 is
94 May_Hide_Profile : Boolean := False;
95 -- This flag is used to indicate that two formals in two subprograms being
96 -- checked for conformance differ only in that one is an access parameter
97 -- while the other is of a general access type with the same designated
98 -- type. In this case, if the rest of the signatures match, a call to
99 -- either subprogram may be ambiguous, which is worth a warning. The flag
100 -- is set in Compatible_Types, and the warning emitted in
101 -- New_Overloaded_Entity.
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
107 procedure Analyze_Function_Return (N : Node_Id);
108 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
109 -- applies to a [generic] function.
111 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
112 -- Analyze a generic subprogram body. N is the body to be analyzed, and
113 -- Gen_Id is the defining entity Id for the corresponding spec.
115 procedure Analyze_Null_Procedure
117 Is_Completion : out Boolean);
118 -- A null procedure can be a declaration or (Ada 2012) a completion
120 procedure Analyze_Return_Statement (N : Node_Id);
121 -- Common processing for simple and extended return statements
123 procedure Analyze_Return_Type (N : Node_Id);
124 -- Subsidiary to Process_Formals: analyze subtype mark in function
125 -- specification in a context where the formals are visible and hide
128 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
129 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
130 -- that we can use RETURN but not skip the debug output at the end.
132 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
133 -- Returns true if Subp can override a predefined operator.
135 procedure Check_Conformance
138 Ctype : Conformance_Type;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
156 procedure Check_Limited_Return
160 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
161 -- types. Used only for simple return statements. Expr is the expression
164 procedure Check_Subprogram_Order (N : Node_Id);
165 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
166 -- the alpha ordering rule for N if this ordering requirement applicable.
168 procedure Check_Returns
172 Proc : Entity_Id := Empty);
173 -- Called to check for missing return statements in a function body, or for
174 -- returns present in a procedure body which has No_Return set. HSS is the
175 -- handled statement sequence for the subprogram body. This procedure
176 -- checks all flow paths to make sure they either have return (Mode = 'F',
177 -- used for functions) or do not have a return (Mode = 'P', used for
178 -- No_Return procedures). The flag Err is set if there are any control
179 -- paths not explicitly terminated by a return in the function case, and is
180 -- True otherwise. Proc is the entity for the procedure case and is used
181 -- in posting the warning message.
183 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
184 -- In Ada 2012, a primitive equality operator on an untagged record type
185 -- must appear before the type is frozen, and have the same visibility as
186 -- that of the type. This procedure checks that this rule is met, and
187 -- otherwise emits an error on the subprogram declaration and a warning
188 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
189 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
190 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
191 -- is set, otherwise the call has no effect.
193 procedure Enter_Overloaded_Entity (S : Entity_Id);
194 -- This procedure makes S, a new overloaded entity, into the first visible
195 -- entity with that name.
197 function Is_Non_Overriding_Operation
199 New_E : Entity_Id) return Boolean;
200 -- Enforce the rule given in 12.3(18): a private operation in an instance
201 -- overrides an inherited operation only if the corresponding operation
202 -- was overriding in the generic. This needs to be checked for primitive
203 -- operations of types derived (in the generic unit) from formal private
204 -- or formal derived types.
206 procedure Make_Inequality_Operator (S : Entity_Id);
207 -- Create the declaration for an inequality operator that is implicitly
208 -- created by a user-defined equality operator that yields a boolean.
210 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
211 -- Formal_Id is an formal parameter entity. This procedure deals with
212 -- setting the proper validity status for this entity, which depends on
213 -- the kind of parameter and the validity checking mode.
215 ---------------------------------------------
216 -- Analyze_Abstract_Subprogram_Declaration --
217 ---------------------------------------------
219 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
220 Scop : constant Entity_Id := Current_Scope;
221 Subp_Id : constant Entity_Id :=
222 Analyze_Subprogram_Specification (Specification (N));
225 Check_SPARK_05_Restriction ("abstract subprogram is not allowed", N);
227 Generate_Definition (Subp_Id);
229 Set_Is_Abstract_Subprogram (Subp_Id);
230 New_Overloaded_Entity (Subp_Id);
231 Check_Delayed_Subprogram (Subp_Id);
233 Set_Categorization_From_Scope (Subp_Id, Scop);
235 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
236 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
238 -- Issue a warning if the abstract subprogram is neither a dispatching
239 -- operation nor an operation that overrides an inherited subprogram or
240 -- predefined operator, since this most likely indicates a mistake.
242 elsif Warn_On_Redundant_Constructs
243 and then not Is_Dispatching_Operation (Subp_Id)
244 and then not Present (Overridden_Operation (Subp_Id))
245 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
246 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
249 ("abstract subprogram is not dispatching or overriding?r?", N);
252 Generate_Reference_To_Formals (Subp_Id);
253 Check_Eliminated (Subp_Id);
255 if Has_Aspects (N) then
256 Analyze_Aspect_Specifications (N, Subp_Id);
258 end Analyze_Abstract_Subprogram_Declaration;
260 ---------------------------------
261 -- Analyze_Expression_Function --
262 ---------------------------------
264 procedure Analyze_Expression_Function (N : Node_Id) is
265 Expr : constant Node_Id := Expression (N);
266 Loc : constant Source_Ptr := Sloc (N);
267 LocX : constant Source_Ptr := Sloc (Expr);
268 Spec : constant Node_Id := Specification (N);
270 procedure Freeze_Expr_Types (Spec_Id : Entity_Id);
271 -- N is an expression function that is a completion and Spec_Id its
272 -- defining entity. Freeze before N all the types referenced by the
273 -- expression of the function.
275 -----------------------
276 -- Freeze_Expr_Types --
277 -----------------------
279 procedure Freeze_Expr_Types (Spec_Id : Entity_Id) is
280 function Cloned_Expression return Node_Id;
281 -- Build a duplicate of the expression of the return statement that
282 -- has no defining entities shared with the original expression.
284 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result;
285 -- Freeze all types referenced in the subtree rooted at Node
287 -----------------------
288 -- Cloned_Expression --
289 -----------------------
291 function Cloned_Expression return Node_Id is
292 function Clone_Id (Node : Node_Id) return Traverse_Result;
293 -- Tree traversal routine that clones the defining identifier of
294 -- iterator and loop parameter specification nodes.
300 function Clone_Id (Node : Node_Id) return Traverse_Result is
302 if Nkind_In (Node, N_Iterator_Specification,
303 N_Loop_Parameter_Specification)
305 Set_Defining_Identifier (Node,
306 New_Copy (Defining_Identifier (Node)));
312 procedure Clone_Def_Ids is new Traverse_Proc (Clone_Id);
316 Dup_Expr : constant Node_Id := New_Copy_Tree (Expr);
318 -- Start of processing for Cloned_Expression
321 -- We must duplicate the expression with semantic information to
322 -- inherit the decoration of global entities in generic instances.
323 -- Set the parent of the new node to be the parent of the original
324 -- to get the proper context, which is needed for complete error
325 -- reporting and for semantic analysis.
327 Set_Parent (Dup_Expr, Parent (Expr));
329 -- Replace the defining identifier of iterators and loop param
330 -- specifications by a clone to ensure that the cloned expression
331 -- and the original expression don't have shared identifiers;
332 -- otherwise, as part of the preanalysis of the expression, these
333 -- shared identifiers may be left decorated with itypes which
334 -- will not be available in the tree passed to the backend.
336 Clone_Def_Ids (Dup_Expr);
339 end Cloned_Expression;
341 ----------------------
342 -- Freeze_Type_Refs --
343 ----------------------
345 function Freeze_Type_Refs (Node : Node_Id) return Traverse_Result is
347 procedure Check_And_Freeze_Type (Typ : Entity_Id);
348 -- Check that Typ is fully declared and freeze it if so
350 ---------------------------
351 -- Check_And_Freeze_Type --
352 ---------------------------
354 procedure Check_And_Freeze_Type (Typ : Entity_Id) is
356 -- Skip Itypes created by the preanalysis
359 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
364 -- This provides a better error message than generating
365 -- primitives whose compilation fails much later. Refine
366 -- the error message if possible.
368 Check_Fully_Declared (Typ, Node);
370 if Error_Posted (Node) then
371 if Has_Private_Component (Typ)
372 and then not Is_Private_Type (Typ)
375 ("\type& has private component", Node, Typ);
379 Freeze_Before (N, Typ);
381 end Check_And_Freeze_Type;
383 -- Start of processing for Freeze_Type_Refs
386 -- Check that a type referenced by an entity can be frozen
388 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
389 Check_And_Freeze_Type (Etype (Entity (Node)));
391 -- Check that the enclosing record type can be frozen
393 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
394 Check_And_Freeze_Type (Scope (Entity (Node)));
397 -- Freezing an access type does not freeze the designated type,
398 -- but freezing conversions between access to interfaces requires
399 -- that the interface types themselves be frozen, so that dispatch
400 -- table entities are properly created.
402 -- Unclear whether a more general rule is needed ???
404 elsif Nkind (Node) = N_Type_Conversion
405 and then Is_Access_Type (Etype (Node))
406 and then Is_Interface (Designated_Type (Etype (Node)))
408 Check_And_Freeze_Type (Designated_Type (Etype (Node)));
411 -- No point in posting several errors on the same expression
413 if Serious_Errors_Detected > 0 then
418 end Freeze_Type_Refs;
420 procedure Freeze_References is new Traverse_Proc (Freeze_Type_Refs);
424 Saved_First_Entity : constant Entity_Id := First_Entity (Spec_Id);
425 Saved_Last_Entity : constant Entity_Id := Last_Entity (Spec_Id);
426 Dup_Expr : constant Node_Id := Cloned_Expression;
428 -- Start of processing for Freeze_Expr_Types
431 -- Preanalyze a duplicate of the expression to have available the
432 -- minimum decoration needed to locate referenced unfrozen types
433 -- without adding any decoration to the function expression. This
434 -- preanalysis is performed with errors disabled to avoid reporting
435 -- spurious errors on Ghost entities (since the expression is not
438 Push_Scope (Spec_Id);
439 Install_Formals (Spec_Id);
440 Ignore_Errors_Enable := Ignore_Errors_Enable + 1;
442 Preanalyze_Spec_Expression (Dup_Expr, Etype (Spec_Id));
444 Ignore_Errors_Enable := Ignore_Errors_Enable - 1;
447 -- Restore certain attributes of Spec_Id since the preanalysis may
448 -- have introduced itypes to this scope, thus modifying attributes
449 -- First_Entity and Last_Entity.
451 Set_First_Entity (Spec_Id, Saved_First_Entity);
452 Set_Last_Entity (Spec_Id, Saved_Last_Entity);
454 if Present (Last_Entity (Spec_Id)) then
455 Set_Next_Entity (Last_Entity (Spec_Id), Empty);
458 -- Freeze all types referenced in the expression
460 Freeze_References (Dup_Expr);
461 end Freeze_Expr_Types;
473 -- If the expression is a completion, Prev is the entity whose
474 -- declaration is completed. Def_Id is needed to analyze the spec.
476 -- Start of processing for Analyze_Expression_Function
479 -- This is one of the occasions on which we transform the tree during
480 -- semantic analysis. If this is a completion, transform the expression
481 -- function into an equivalent subprogram body, and analyze it.
483 -- Expression functions are inlined unconditionally. The back-end will
484 -- determine whether this is possible.
486 Inline_Processing_Required := True;
488 -- Create a specification for the generated body. This must be done
489 -- prior to the analysis of the initial declaration.
491 New_Spec := Copy_Subprogram_Spec (Spec);
492 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
494 -- If there are previous overloadable entities with the same name,
495 -- check whether any of them is completed by the expression function.
496 -- In a generic context a formal subprogram has no completion.
499 and then Is_Overloadable (Prev)
500 and then not Is_Formal_Subprogram (Prev)
502 Def_Id := Analyze_Subprogram_Specification (Spec);
503 Prev := Find_Corresponding_Spec (N);
505 -- The previous entity may be an expression function as well, in
506 -- which case the redeclaration is illegal.
509 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
510 N_Expression_Function
512 Error_Msg_Sloc := Sloc (Prev);
513 Error_Msg_N ("& conflicts with declaration#", Def_Id);
518 Ret := Make_Simple_Return_Statement (LocX, Expr);
521 Make_Subprogram_Body (Loc,
522 Specification => New_Spec,
523 Declarations => Empty_List,
524 Handled_Statement_Sequence =>
525 Make_Handled_Sequence_Of_Statements (LocX,
526 Statements => New_List (Ret)));
527 Set_Was_Expression_Function (New_Body);
529 -- If the expression completes a generic subprogram, we must create a
530 -- separate node for the body, because at instantiation the original
531 -- node of the generic copy must be a generic subprogram body, and
532 -- cannot be a expression function. Otherwise we just rewrite the
533 -- expression with the non-generic body.
535 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
536 Insert_After (N, New_Body);
538 -- Propagate any aspects or pragmas that apply to the expression
539 -- function to the proper body when the expression function acts
542 if Has_Aspects (N) then
543 Move_Aspects (N, To => New_Body);
546 Relocate_Pragmas_To_Body (New_Body);
548 Rewrite (N, Make_Null_Statement (Loc));
549 Set_Has_Completion (Prev, False);
552 Set_Is_Inlined (Prev);
554 -- If the expression function is a completion, the previous declaration
555 -- must come from source. We know already that it appears in the current
556 -- scope. The entity itself may be internally created if within a body
560 and then Is_Overloadable (Prev)
561 and then not Is_Formal_Subprogram (Prev)
562 and then Comes_From_Source (Parent (Prev))
564 Set_Has_Completion (Prev, False);
565 Set_Is_Inlined (Prev);
567 -- AI12-0103: Expression functions that are a completion freeze their
568 -- expression but don't freeze anything else (unlike regular bodies).
570 -- Note that we cannot defer this freezing to the analysis of the
571 -- expression itself, because a freeze node might appear in a nested
572 -- scope, leading to an elaboration order issue in gigi.
574 Freeze_Expr_Types (Def_Id);
576 -- For navigation purposes, indicate that the function is a body
578 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
579 Rewrite (N, New_Body);
581 -- Remove any existing aspects from the original node because the act
582 -- of rewriting causes the list to be shared between the two nodes.
584 Orig_N := Original_Node (N);
585 Remove_Aspects (Orig_N);
587 -- Propagate any pragmas that apply to expression function to the
588 -- proper body when the expression function acts as a completion.
589 -- Aspects are automatically transfered because of node rewriting.
591 Relocate_Pragmas_To_Body (N);
594 -- Once the aspects of the generated body have been analyzed, create
595 -- a copy for ASIS purposes and associate it with the original node.
597 if Has_Aspects (N) then
598 Set_Aspect_Specifications (Orig_N,
599 New_Copy_List_Tree (Aspect_Specifications (N)));
602 -- Prev is the previous entity with the same name, but it is can
603 -- be an unrelated spec that is not completed by the expression
604 -- function. In that case the relevant entity is the one in the body.
605 -- Not clear that the backend can inline it in this case ???
607 if Has_Completion (Prev) then
609 -- The formals of the expression function are body formals,
610 -- and do not appear in the ali file, which will only contain
611 -- references to the formals of the original subprogram spec.
618 F1 := First_Formal (Def_Id);
619 F2 := First_Formal (Prev);
621 while Present (F1) loop
622 Set_Spec_Entity (F1, F2);
629 Set_Is_Inlined (Defining_Entity (New_Body));
632 -- If this is not a completion, create both a declaration and a body, so
633 -- that the expression can be inlined whenever possible.
636 -- An expression function that is not a completion is not a
637 -- subprogram declaration, and thus cannot appear in a protected
640 if Nkind (Parent (N)) = N_Protected_Definition then
642 ("an expression function is not a legal protected operation", N);
645 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
647 -- Remove any existing aspects from the original node because the act
648 -- of rewriting causes the list to be shared between the two nodes.
650 Orig_N := Original_Node (N);
651 Remove_Aspects (Orig_N);
655 -- Once the aspects of the generated spec have been analyzed, create
656 -- a copy for ASIS purposes and associate it with the original node.
658 if Has_Aspects (N) then
659 Set_Aspect_Specifications (Orig_N,
660 New_Copy_List_Tree (Aspect_Specifications (N)));
663 -- If aspect SPARK_Mode was specified on the body, it needs to be
664 -- repeated both on the generated spec and the body.
666 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
668 if Present (Asp) then
669 Asp := New_Copy_Tree (Asp);
670 Set_Analyzed (Asp, False);
671 Set_Aspect_Specifications (New_Body, New_List (Asp));
674 Def_Id := Defining_Entity (N);
675 Set_Is_Inlined (Def_Id);
677 -- Establish the linkages between the spec and the body. These are
678 -- used when the expression function acts as the prefix of attribute
679 -- 'Access in order to freeze the original expression which has been
680 -- moved to the generated body.
682 Set_Corresponding_Body (N, Defining_Entity (New_Body));
683 Set_Corresponding_Spec (New_Body, Def_Id);
685 -- Within a generic pre-analyze the original expression for name
686 -- capture. The body is also generated but plays no role in
687 -- this because it is not part of the original source.
689 if Inside_A_Generic then
690 Set_Has_Completion (Def_Id);
692 Install_Formals (Def_Id);
693 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
697 -- To prevent premature freeze action, insert the new body at the end
698 -- of the current declarations, or at the end of the package spec.
699 -- However, resolve usage names now, to prevent spurious visibility
700 -- on later entities. Note that the function can now be called in
701 -- the current declarative part, which will appear to be prior to
702 -- the presence of the body in the code. There are nevertheless no
703 -- order of elaboration issues because all name resolution has taken
704 -- place at the point of declaration.
707 Decls : List_Id := List_Containing (N);
708 Expr : constant Node_Id := Expression (Ret);
709 Par : constant Node_Id := Parent (Decls);
710 Typ : constant Entity_Id := Etype (Def_Id);
713 -- If this is a wrapper created for in an instance for a formal
714 -- subprogram, insert body after declaration, to be analyzed when
715 -- the enclosing instance is analyzed.
718 and then Is_Generic_Actual_Subprogram (Def_Id)
720 Insert_After (N, New_Body);
723 if Nkind (Par) = N_Package_Specification
724 and then Decls = Visible_Declarations (Par)
725 and then Present (Private_Declarations (Par))
726 and then not Is_Empty_List (Private_Declarations (Par))
728 Decls := Private_Declarations (Par);
731 Insert_After (Last (Decls), New_Body);
733 -- Preanalyze the expression for name capture, except in an
734 -- instance, where this has been done during generic analysis,
735 -- and will be redone when analyzing the body.
737 if not In_Instance then
739 Install_Formals (Def_Id);
740 Preanalyze_Spec_Expression (Expr, Typ);
741 Check_Limited_Return (Original_Node (N), Expr, Typ);
748 -- If the return expression is a static constant, we suppress warning
749 -- messages on unused formals, which in most cases will be noise.
751 Set_Is_Trivial_Subprogram
752 (Defining_Entity (New_Body), Is_OK_Static_Expression (Expr));
753 end Analyze_Expression_Function;
755 ----------------------------------------
756 -- Analyze_Extended_Return_Statement --
757 ----------------------------------------
759 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
761 Check_Compiler_Unit ("extended return statement", N);
762 Analyze_Return_Statement (N);
763 end Analyze_Extended_Return_Statement;
765 ----------------------------
766 -- Analyze_Function_Call --
767 ----------------------------
769 procedure Analyze_Function_Call (N : Node_Id) is
770 Actuals : constant List_Id := Parameter_Associations (N);
771 Func_Nam : constant Node_Id := Name (N);
777 -- A call of the form A.B (X) may be an Ada 2005 call, which is
778 -- rewritten as B (A, X). If the rewriting is successful, the call
779 -- has been analyzed and we just return.
781 if Nkind (Func_Nam) = N_Selected_Component
782 and then Name (N) /= Func_Nam
783 and then Is_Rewrite_Substitution (N)
784 and then Present (Etype (N))
789 -- If error analyzing name, then set Any_Type as result type and return
791 if Etype (Func_Nam) = Any_Type then
792 Set_Etype (N, Any_Type);
796 -- Otherwise analyze the parameters
798 if Present (Actuals) then
799 Actual := First (Actuals);
800 while Present (Actual) loop
802 Check_Parameterless_Call (Actual);
808 end Analyze_Function_Call;
810 -----------------------------
811 -- Analyze_Function_Return --
812 -----------------------------
814 procedure Analyze_Function_Return (N : Node_Id) is
815 Loc : constant Source_Ptr := Sloc (N);
816 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
817 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
819 R_Type : constant Entity_Id := Etype (Scope_Id);
820 -- Function result subtype
822 procedure Check_Aggregate_Accessibility (Aggr : Node_Id);
823 -- Apply legality rule of 6.5 (5.8) to the access discriminants of an
824 -- aggregate in a return statement.
826 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
827 -- Check that the return_subtype_indication properly matches the result
828 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
830 -----------------------------------
831 -- Check_Aggregate_Accessibility --
832 -----------------------------------
834 procedure Check_Aggregate_Accessibility (Aggr : Node_Id) is
835 Typ : constant Entity_Id := Etype (Aggr);
842 if Is_Record_Type (Typ) and then Has_Discriminants (Typ) then
843 Discr := First_Discriminant (Typ);
844 Assoc := First (Component_Associations (Aggr));
845 while Present (Discr) loop
846 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
847 Expr := Expression (Assoc);
849 if Nkind (Expr) = N_Attribute_Reference
850 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
852 Obj := Prefix (Expr);
853 while Nkind_In (Obj, N_Indexed_Component,
854 N_Selected_Component)
859 -- Do not check aliased formals or function calls. A
860 -- run-time check may still be needed ???
862 if Is_Entity_Name (Obj)
863 and then Comes_From_Source (Obj)
865 if Is_Formal (Entity (Obj))
866 and then Is_Aliased (Entity (Obj))
870 elsif Object_Access_Level (Obj) >
871 Scope_Depth (Scope (Scope_Id))
874 ("access discriminant in return aggregate would "
875 & "be a dangling reference", Obj);
881 Next_Discriminant (Discr);
884 end Check_Aggregate_Accessibility;
886 -------------------------------------
887 -- Check_Return_Subtype_Indication --
888 -------------------------------------
890 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
891 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
893 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
894 -- Subtype given in the extended return statement (must match R_Type)
896 Subtype_Ind : constant Node_Id :=
897 Object_Definition (Original_Node (Obj_Decl));
899 procedure Error_No_Match (N : Node_Id);
900 -- Output error messages for case where types do not statically
901 -- match. N is the location for the messages.
907 procedure Error_No_Match (N : Node_Id) is
910 ("subtype must statically match function result subtype", N);
912 if not Predicates_Match (R_Stm_Type, R_Type) then
913 Error_Msg_Node_2 := R_Type;
915 ("\predicate of& does not match predicate of&",
920 -- Start of processing for Check_Return_Subtype_Indication
923 -- First, avoid cascaded errors
925 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
929 -- "return access T" case; check that the return statement also has
930 -- "access T", and that the subtypes statically match:
931 -- if this is an access to subprogram the signatures must match.
933 if Is_Anonymous_Access_Type (R_Type) then
934 if Is_Anonymous_Access_Type (R_Stm_Type) then
935 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
937 if Base_Type (Designated_Type (R_Stm_Type)) /=
938 Base_Type (Designated_Type (R_Type))
939 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
941 Error_No_Match (Subtype_Mark (Subtype_Ind));
945 -- For two anonymous access to subprogram types, the types
946 -- themselves must be type conformant.
948 if not Conforming_Types
949 (R_Stm_Type, R_Type, Fully_Conformant)
951 Error_No_Match (Subtype_Ind);
956 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
959 -- If the return object is of an anonymous access type, then report
960 -- an error if the function's result type is not also anonymous.
962 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
963 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
965 ("anonymous access not allowed for function with named access "
966 & "result", Subtype_Ind);
968 -- Subtype indication case: check that the return object's type is
969 -- covered by the result type, and that the subtypes statically match
970 -- when the result subtype is constrained. Also handle record types
971 -- with unknown discriminants for which we have built the underlying
972 -- record view. Coverage is needed to allow specific-type return
973 -- objects when the result type is class-wide (see AI05-32).
975 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
976 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
980 Underlying_Record_View (Base_Type (R_Stm_Type))))
982 -- A null exclusion may be present on the return type, on the
983 -- function specification, on the object declaration or on the
986 if Is_Access_Type (R_Type)
988 (Can_Never_Be_Null (R_Type)
989 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
990 Can_Never_Be_Null (R_Stm_Type)
992 Error_No_Match (Subtype_Ind);
995 -- AI05-103: for elementary types, subtypes must statically match
997 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
998 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
999 Error_No_Match (Subtype_Ind);
1003 -- All remaining cases are illegal
1005 -- Note: previous versions of this subprogram allowed the return
1006 -- value to be the ancestor of the return type if the return type
1007 -- was a null extension. This was plainly incorrect.
1011 ("wrong type for return_subtype_indication", Subtype_Ind);
1013 end Check_Return_Subtype_Indication;
1015 ---------------------
1016 -- Local Variables --
1017 ---------------------
1022 -- Start of processing for Analyze_Function_Return
1025 Set_Return_Present (Scope_Id);
1027 if Nkind (N) = N_Simple_Return_Statement then
1028 Expr := Expression (N);
1030 -- Guard against a malformed expression. The parser may have tried to
1031 -- recover but the node is not analyzable.
1033 if Nkind (Expr) = N_Error then
1034 Set_Etype (Expr, Any_Type);
1035 Expander_Mode_Save_And_Set (False);
1039 -- The resolution of a controlled [extension] aggregate associated
1040 -- with a return statement creates a temporary which needs to be
1041 -- finalized on function exit. Wrap the return statement inside a
1042 -- block so that the finalization machinery can detect this case.
1043 -- This early expansion is done only when the return statement is
1044 -- not part of a handled sequence of statements.
1046 if Nkind_In (Expr, N_Aggregate,
1047 N_Extension_Aggregate)
1048 and then Needs_Finalization (R_Type)
1049 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1052 Make_Block_Statement (Loc,
1053 Handled_Statement_Sequence =>
1054 Make_Handled_Sequence_Of_Statements (Loc,
1055 Statements => New_List (Relocate_Node (N)))));
1063 -- Ada 2005 (AI-251): If the type of the returned object is
1064 -- an access to an interface type then we add an implicit type
1065 -- conversion to force the displacement of the "this" pointer to
1066 -- reference the secondary dispatch table. We cannot delay the
1067 -- generation of this implicit conversion until the expansion
1068 -- because in this case the type resolution changes the decoration
1069 -- of the expression node to match R_Type; by contrast, if the
1070 -- returned object is a class-wide interface type then it is too
1071 -- early to generate here the implicit conversion since the return
1072 -- statement may be rewritten by the expander into an extended
1073 -- return statement whose expansion takes care of adding the
1074 -- implicit type conversion to displace the pointer to the object.
1077 and then Serious_Errors_Detected = 0
1078 and then Is_Access_Type (R_Type)
1079 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1080 and then Is_Interface (Designated_Type (R_Type))
1081 and then Is_Progenitor (Designated_Type (R_Type),
1082 Designated_Type (Etype (Expr)))
1084 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1088 Resolve (Expr, R_Type);
1089 Check_Limited_Return (N, Expr, R_Type);
1091 if Present (Expr) and then Nkind (Expr) = N_Aggregate then
1092 Check_Aggregate_Accessibility (Expr);
1096 -- RETURN only allowed in SPARK as the last statement in function
1098 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1100 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
1101 or else Present (Next (N)))
1103 Check_SPARK_05_Restriction
1104 ("RETURN should be the last statement in function", N);
1108 Check_SPARK_05_Restriction ("extended RETURN is not allowed", N);
1109 Obj_Decl := Last (Return_Object_Declarations (N));
1111 -- Analyze parts specific to extended_return_statement:
1114 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1115 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1118 Expr := Expression (Obj_Decl);
1120 -- Note: The check for OK_For_Limited_Init will happen in
1121 -- Analyze_Object_Declaration; we treat it as a normal
1122 -- object declaration.
1124 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1127 Check_Return_Subtype_Indication (Obj_Decl);
1129 if Present (HSS) then
1132 if Present (Exception_Handlers (HSS)) then
1134 -- ???Has_Nested_Block_With_Handler needs to be set.
1135 -- Probably by creating an actual N_Block_Statement.
1136 -- Probably in Expand.
1142 -- Mark the return object as referenced, since the return is an
1143 -- implicit reference of the object.
1145 Set_Referenced (Defining_Identifier (Obj_Decl));
1147 Check_References (Stm_Entity);
1149 -- Check RM 6.5 (5.9/3)
1152 if Ada_Version < Ada_2012 then
1154 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1155 -- Can it really happen (extended return???)
1158 ("aliased only allowed for limited return objects "
1159 & "in Ada 2012??", N);
1161 elsif not Is_Limited_View (R_Type) then
1163 ("aliased only allowed for limited return objects", N);
1169 -- Case of Expr present
1173 -- Defend against previous errors
1175 and then Nkind (Expr) /= N_Empty
1176 and then Present (Etype (Expr))
1178 -- Apply constraint check. Note that this is done before the implicit
1179 -- conversion of the expression done for anonymous access types to
1180 -- ensure correct generation of the null-excluding check associated
1181 -- with null-excluding expressions found in return statements.
1183 Apply_Constraint_Check (Expr, R_Type);
1185 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1186 -- type, apply an implicit conversion of the expression to that type
1187 -- to force appropriate static and run-time accessibility checks.
1189 if Ada_Version >= Ada_2005
1190 and then Ekind (R_Type) = E_Anonymous_Access_Type
1192 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1193 Analyze_And_Resolve (Expr, R_Type);
1195 -- If this is a local anonymous access to subprogram, the
1196 -- accessibility check can be applied statically. The return is
1197 -- illegal if the access type of the return expression is declared
1198 -- inside of the subprogram (except if it is the subtype indication
1199 -- of an extended return statement).
1201 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1202 if not Comes_From_Source (Current_Scope)
1203 or else Ekind (Current_Scope) = E_Return_Statement
1208 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1210 Error_Msg_N ("cannot return local access to subprogram", N);
1213 -- The expression cannot be of a formal incomplete type
1215 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1216 and then Is_Generic_Type (Etype (Expr))
1219 ("cannot return expression of a formal incomplete type", N);
1222 -- If the result type is class-wide, then check that the return
1223 -- expression's type is not declared at a deeper level than the
1224 -- function (RM05-6.5(5.6/2)).
1226 if Ada_Version >= Ada_2005
1227 and then Is_Class_Wide_Type (R_Type)
1229 if Type_Access_Level (Etype (Expr)) >
1230 Subprogram_Access_Level (Scope_Id)
1233 ("level of return expression type is deeper than "
1234 & "class-wide function!", Expr);
1238 -- Check incorrect use of dynamically tagged expression
1240 if Is_Tagged_Type (R_Type) then
1241 Check_Dynamically_Tagged_Expression
1247 -- ??? A real run-time accessibility check is needed in cases
1248 -- involving dereferences of access parameters. For now we just
1249 -- check the static cases.
1251 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1252 and then Is_Limited_View (Etype (Scope_Id))
1253 and then Object_Access_Level (Expr) >
1254 Subprogram_Access_Level (Scope_Id)
1256 -- Suppress the message in a generic, where the rewriting
1259 if Inside_A_Generic then
1264 Make_Raise_Program_Error (Loc,
1265 Reason => PE_Accessibility_Check_Failed));
1268 Error_Msg_Warn := SPARK_Mode /= On;
1269 Error_Msg_N ("cannot return a local value by reference<<", N);
1270 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1274 if Known_Null (Expr)
1275 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1276 and then Null_Exclusion_Present (Parent (Scope_Id))
1278 Apply_Compile_Time_Constraint_Error
1280 Msg => "(Ada 2005) null not allowed for "
1281 & "null-excluding return??",
1282 Reason => CE_Null_Not_Allowed);
1285 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1286 -- has no initializing expression.
1288 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1289 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1290 Subprogram_Access_Level (Scope_Id)
1293 ("level of return expression type is deeper than "
1294 & "class-wide function!", Obj_Decl);
1297 end Analyze_Function_Return;
1299 -------------------------------------
1300 -- Analyze_Generic_Subprogram_Body --
1301 -------------------------------------
1303 procedure Analyze_Generic_Subprogram_Body
1307 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1308 Kind : constant Entity_Kind := Ekind (Gen_Id);
1309 Body_Id : Entity_Id;
1314 -- Copy body and disable expansion while analyzing the generic For a
1315 -- stub, do not copy the stub (which would load the proper body), this
1316 -- will be done when the proper body is analyzed.
1318 if Nkind (N) /= N_Subprogram_Body_Stub then
1319 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1322 -- Once the contents of the generic copy and the template are
1323 -- swapped, do the same for their respective aspect specifications.
1325 Exchange_Aspects (N, New_N);
1327 -- Collect all contract-related source pragmas found within the
1328 -- template and attach them to the contract of the subprogram body.
1329 -- This contract is used in the capture of global references within
1332 Create_Generic_Contract (N);
1337 Spec := Specification (N);
1339 -- Within the body of the generic, the subprogram is callable, and
1340 -- behaves like the corresponding non-generic unit.
1342 Body_Id := Defining_Entity (Spec);
1344 if Kind = E_Generic_Procedure
1345 and then Nkind (Spec) /= N_Procedure_Specification
1347 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1350 elsif Kind = E_Generic_Function
1351 and then Nkind (Spec) /= N_Function_Specification
1353 Error_Msg_N ("invalid body for generic function ", Body_Id);
1357 Set_Corresponding_Body (Gen_Decl, Body_Id);
1359 if Has_Completion (Gen_Id)
1360 and then Nkind (Parent (N)) /= N_Subunit
1362 Error_Msg_N ("duplicate generic body", N);
1365 Set_Has_Completion (Gen_Id);
1368 if Nkind (N) = N_Subprogram_Body_Stub then
1369 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1371 Set_Corresponding_Spec (N, Gen_Id);
1374 if Nkind (Parent (N)) = N_Compilation_Unit then
1375 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1378 -- Make generic parameters immediately visible in the body. They are
1379 -- needed to process the formals declarations. Then make the formals
1380 -- visible in a separate step.
1382 Push_Scope (Gen_Id);
1386 First_Ent : Entity_Id;
1389 First_Ent := First_Entity (Gen_Id);
1392 while Present (E) and then not Is_Formal (E) loop
1397 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1399 -- Now generic formals are visible, and the specification can be
1400 -- analyzed, for subsequent conformance check.
1402 Body_Id := Analyze_Subprogram_Specification (Spec);
1404 -- Make formal parameters visible
1408 -- E is the first formal parameter, we loop through the formals
1409 -- installing them so that they will be visible.
1411 Set_First_Entity (Gen_Id, E);
1412 while Present (E) loop
1418 -- Visible generic entity is callable within its own body
1420 Set_Ekind (Gen_Id, Ekind (Body_Id));
1421 Set_Ekind (Body_Id, E_Subprogram_Body);
1422 Set_Convention (Body_Id, Convention (Gen_Id));
1423 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1424 Set_Scope (Body_Id, Scope (Gen_Id));
1426 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1428 if Nkind (N) = N_Subprogram_Body_Stub then
1430 -- No body to analyze, so restore state of generic unit
1432 Set_Ekind (Gen_Id, Kind);
1433 Set_Ekind (Body_Id, Kind);
1435 if Present (First_Ent) then
1436 Set_First_Entity (Gen_Id, First_Ent);
1443 -- If this is a compilation unit, it must be made visible explicitly,
1444 -- because the compilation of the declaration, unlike other library
1445 -- unit declarations, does not. If it is not a unit, the following
1446 -- is redundant but harmless.
1448 Set_Is_Immediately_Visible (Gen_Id);
1449 Reference_Body_Formals (Gen_Id, Body_Id);
1451 if Is_Child_Unit (Gen_Id) then
1452 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1455 Set_Actual_Subtypes (N, Current_Scope);
1457 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1458 Set_SPARK_Pragma_Inherited (Body_Id);
1460 -- Analyze any aspect specifications that appear on the generic
1463 if Has_Aspects (N) then
1464 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
1467 Analyze_Declarations (Declarations (N));
1470 -- Process the contract of the subprogram body after all declarations
1471 -- have been analyzed. This ensures that any contract-related pragmas
1472 -- are available through the N_Contract node of the body.
1474 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1476 Analyze (Handled_Statement_Sequence (N));
1477 Save_Global_References (Original_Node (N));
1479 -- Prior to exiting the scope, include generic formals again (if any
1480 -- are present) in the set of local entities.
1482 if Present (First_Ent) then
1483 Set_First_Entity (Gen_Id, First_Ent);
1486 Check_References (Gen_Id);
1489 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1491 Check_Subprogram_Order (N);
1493 -- Outside of its body, unit is generic again
1495 Set_Ekind (Gen_Id, Kind);
1496 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1499 Style.Check_Identifier (Body_Id, Gen_Id);
1503 end Analyze_Generic_Subprogram_Body;
1505 ----------------------------
1506 -- Analyze_Null_Procedure --
1507 ----------------------------
1509 procedure Analyze_Null_Procedure
1511 Is_Completion : out Boolean)
1513 Loc : constant Source_Ptr := Sloc (N);
1514 Spec : constant Node_Id := Specification (N);
1515 Designator : Entity_Id;
1517 Null_Body : Node_Id := Empty;
1518 Null_Stmt : Node_Id := Null_Statement (Spec);
1522 -- Capture the profile of the null procedure before analysis, for
1523 -- expansion at the freeze point and at each point of call. The body is
1524 -- used if the procedure has preconditions, or if it is a completion. In
1525 -- the first case the body is analyzed at the freeze point, in the other
1526 -- it replaces the null procedure declaration.
1528 -- For a null procedure that comes from source, a NULL statement is
1529 -- provided by the parser, which carries the source location of the
1530 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1531 -- from expansion, create one now.
1533 if No (Null_Stmt) then
1534 Null_Stmt := Make_Null_Statement (Loc);
1538 Make_Subprogram_Body (Loc,
1539 Specification => New_Copy_Tree (Spec),
1540 Declarations => New_List,
1541 Handled_Statement_Sequence =>
1542 Make_Handled_Sequence_Of_Statements (Loc,
1543 Statements => New_List (Null_Stmt)));
1545 -- Create new entities for body and formals
1547 Set_Defining_Unit_Name (Specification (Null_Body),
1548 Make_Defining_Identifier
1549 (Sloc (Defining_Entity (N)),
1550 Chars (Defining_Entity (N))));
1552 Form := First (Parameter_Specifications (Specification (Null_Body)));
1553 while Present (Form) loop
1554 Set_Defining_Identifier (Form,
1555 Make_Defining_Identifier
1556 (Sloc (Defining_Identifier (Form)),
1557 Chars (Defining_Identifier (Form))));
1561 -- Determine whether the null procedure may be a completion of a generic
1562 -- suprogram, in which case we use the new null body as the completion
1563 -- and set minimal semantic information on the original declaration,
1564 -- which is rewritten as a null statement.
1566 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1568 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1569 Insert_Before (N, Null_Body);
1570 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1572 Rewrite (N, Make_Null_Statement (Loc));
1573 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1574 Is_Completion := True;
1578 -- Resolve the types of the formals now, because the freeze point may
1579 -- appear in a different context, e.g. an instantiation.
1581 Form := First (Parameter_Specifications (Specification (Null_Body)));
1582 while Present (Form) loop
1583 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1584 Find_Type (Parameter_Type (Form));
1586 elsif No (Access_To_Subprogram_Definition
1587 (Parameter_Type (Form)))
1589 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1591 -- The case of a null procedure with a formal that is an
1592 -- access-to-subprogram type, and that is used as an actual
1593 -- in an instantiation is left to the enthusiastic reader.
1603 -- If there are previous overloadable entities with the same name, check
1604 -- whether any of them is completed by the null procedure.
1606 if Present (Prev) and then Is_Overloadable (Prev) then
1607 Designator := Analyze_Subprogram_Specification (Spec);
1608 Prev := Find_Corresponding_Spec (N);
1611 if No (Prev) or else not Comes_From_Source (Prev) then
1612 Designator := Analyze_Subprogram_Specification (Spec);
1613 Set_Has_Completion (Designator);
1615 -- Signal to caller that this is a procedure declaration
1617 Is_Completion := False;
1619 -- Null procedures are always inlined, but generic formal subprograms
1620 -- which appear as such in the internal instance of formal packages,
1621 -- need no completion and are not marked Inline.
1624 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1626 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1627 Set_Body_To_Inline (N, Null_Body);
1628 Set_Is_Inlined (Designator);
1632 -- The null procedure is a completion. We unconditionally rewrite
1633 -- this as a null body (even if expansion is not active), because
1634 -- there are various error checks that are applied on this body
1635 -- when it is analyzed (e.g. correct aspect placement).
1637 if Has_Completion (Prev) then
1638 Error_Msg_Sloc := Sloc (Prev);
1639 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1642 Check_Previous_Null_Procedure (N, Prev);
1644 Is_Completion := True;
1645 Rewrite (N, Null_Body);
1648 end Analyze_Null_Procedure;
1650 -----------------------------
1651 -- Analyze_Operator_Symbol --
1652 -----------------------------
1654 -- An operator symbol such as "+" or "and" may appear in context where the
1655 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1656 -- is just a string, as in (conjunction = "or"). In these cases the parser
1657 -- generates this node, and the semantics does the disambiguation. Other
1658 -- such case are actuals in an instantiation, the generic unit in an
1659 -- instantiation, and pragma arguments.
1661 procedure Analyze_Operator_Symbol (N : Node_Id) is
1662 Par : constant Node_Id := Parent (N);
1665 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1666 or else Nkind (Par) = N_Function_Instantiation
1667 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1668 or else (Nkind (Par) = N_Pragma_Argument_Association
1669 and then not Is_Pragma_String_Literal (Par))
1670 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1671 or else (Nkind (Par) = N_Attribute_Reference
1672 and then Attribute_Name (Par) /= Name_Value)
1674 Find_Direct_Name (N);
1677 Change_Operator_Symbol_To_String_Literal (N);
1680 end Analyze_Operator_Symbol;
1682 -----------------------------------
1683 -- Analyze_Parameter_Association --
1684 -----------------------------------
1686 procedure Analyze_Parameter_Association (N : Node_Id) is
1688 Analyze (Explicit_Actual_Parameter (N));
1689 end Analyze_Parameter_Association;
1691 ----------------------------
1692 -- Analyze_Procedure_Call --
1693 ----------------------------
1695 -- WARNING: This routine manages Ghost regions. Return statements must be
1696 -- replaced by gotos which jump to the end of the routine and restore the
1699 procedure Analyze_Procedure_Call (N : Node_Id) is
1700 procedure Analyze_Call_And_Resolve;
1701 -- Do Analyze and Resolve calls for procedure call. At the end, check
1702 -- for illegal order dependence.
1703 -- ??? where is the check for illegal order dependencies?
1705 ------------------------------
1706 -- Analyze_Call_And_Resolve --
1707 ------------------------------
1709 procedure Analyze_Call_And_Resolve is
1711 if Nkind (N) = N_Procedure_Call_Statement then
1713 Resolve (N, Standard_Void_Type);
1717 end Analyze_Call_And_Resolve;
1721 Actuals : constant List_Id := Parameter_Associations (N);
1722 Loc : constant Source_Ptr := Sloc (N);
1723 P : constant Node_Id := Name (N);
1725 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1726 -- Save the Ghost mode to restore on exit
1731 -- Start of processing for Analyze_Procedure_Call
1734 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1735 -- a procedure call or an entry call. The prefix may denote an access
1736 -- to subprogram type, in which case an implicit dereference applies.
1737 -- If the prefix is an indexed component (without implicit dereference)
1738 -- then the construct denotes a call to a member of an entire family.
1739 -- If the prefix is a simple name, it may still denote a call to a
1740 -- parameterless member of an entry family. Resolution of these various
1741 -- interpretations is delicate.
1743 -- Do not analyze machine code statements to avoid rejecting them in
1746 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1747 Set_Etype (P, Standard_Void_Type);
1752 -- If this is a call of the form Obj.Op, the call may have been analyzed
1753 -- and possibly rewritten into a block, in which case we are done.
1755 if Analyzed (N) then
1759 -- If there is an error analyzing the name (which may have been
1760 -- rewritten if the original call was in prefix notation) then error
1761 -- has been emitted already, mark node and return.
1763 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1764 Set_Etype (N, Any_Type);
1768 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1769 -- Set the mode now to ensure that any nodes generated during analysis
1770 -- and expansion are properly marked as Ghost.
1772 Mark_And_Set_Ghost_Procedure_Call (N);
1774 -- Otherwise analyze the parameters
1776 if Present (Actuals) then
1777 Actual := First (Actuals);
1779 while Present (Actual) loop
1781 Check_Parameterless_Call (Actual);
1786 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1788 if Nkind (P) = N_Attribute_Reference
1789 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1791 Name_Elab_Subp_Body)
1793 if Present (Actuals) then
1795 ("no parameters allowed for this call", First (Actuals));
1799 Set_Etype (N, Standard_Void_Type);
1802 elsif Is_Entity_Name (P)
1803 and then Is_Record_Type (Etype (Entity (P)))
1804 and then Remote_AST_I_Dereference (P)
1808 elsif Is_Entity_Name (P)
1809 and then Ekind (Entity (P)) /= E_Entry_Family
1811 if Is_Access_Type (Etype (P))
1812 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1813 and then No (Actuals)
1814 and then Comes_From_Source (N)
1816 Error_Msg_N ("missing explicit dereference in call", N);
1819 Analyze_Call_And_Resolve;
1821 -- If the prefix is the simple name of an entry family, this is a
1822 -- parameterless call from within the task body itself.
1824 elsif Is_Entity_Name (P)
1825 and then Nkind (P) = N_Identifier
1826 and then Ekind (Entity (P)) = E_Entry_Family
1827 and then Present (Actuals)
1828 and then No (Next (First (Actuals)))
1830 -- Can be call to parameterless entry family. What appears to be the
1831 -- sole argument is in fact the entry index. Rewrite prefix of node
1832 -- accordingly. Source representation is unchanged by this
1836 Make_Indexed_Component (Loc,
1838 Make_Selected_Component (Loc,
1839 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1840 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1841 Expressions => Actuals);
1842 Set_Name (N, New_N);
1843 Set_Etype (New_N, Standard_Void_Type);
1844 Set_Parameter_Associations (N, No_List);
1845 Analyze_Call_And_Resolve;
1847 elsif Nkind (P) = N_Explicit_Dereference then
1848 if Ekind (Etype (P)) = E_Subprogram_Type then
1849 Analyze_Call_And_Resolve;
1851 Error_Msg_N ("expect access to procedure in call", P);
1854 -- The name can be a selected component or an indexed component that
1855 -- yields an access to subprogram. Such a prefix is legal if the call
1856 -- has parameter associations.
1858 elsif Is_Access_Type (Etype (P))
1859 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1861 if Present (Actuals) then
1862 Analyze_Call_And_Resolve;
1864 Error_Msg_N ("missing explicit dereference in call ", N);
1867 -- If not an access to subprogram, then the prefix must resolve to the
1868 -- name of an entry, entry family, or protected operation.
1870 -- For the case of a simple entry call, P is a selected component where
1871 -- the prefix is the task and the selector name is the entry. A call to
1872 -- a protected procedure will have the same syntax. If the protected
1873 -- object contains overloaded operations, the entity may appear as a
1874 -- function, the context will select the operation whose type is Void.
1876 elsif Nkind (P) = N_Selected_Component
1877 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1881 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1882 -- in prefix notation may still be missing its controlling argument,
1883 -- so perform the transformation now.
1885 if SPARK_Mode = On and then In_Inlined_Body then
1887 Subp : constant Entity_Id := Entity (Selector_Name (P));
1888 Typ : constant Entity_Id := Etype (Prefix (P));
1891 if Is_Tagged_Type (Typ)
1892 and then Present (First_Formal (Subp))
1893 and then Etype (First_Formal (Subp)) = Typ
1894 and then Try_Object_Operation (P)
1899 Analyze_Call_And_Resolve;
1904 Analyze_Call_And_Resolve;
1907 elsif Nkind (P) = N_Selected_Component
1908 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1909 and then Present (Actuals)
1910 and then No (Next (First (Actuals)))
1912 -- Can be call to parameterless entry family. What appears to be the
1913 -- sole argument is in fact the entry index. Rewrite prefix of node
1914 -- accordingly. Source representation is unchanged by this
1918 Make_Indexed_Component (Loc,
1919 Prefix => New_Copy (P),
1920 Expressions => Actuals);
1921 Set_Name (N, New_N);
1922 Set_Etype (New_N, Standard_Void_Type);
1923 Set_Parameter_Associations (N, No_List);
1924 Analyze_Call_And_Resolve;
1926 -- For the case of a reference to an element of an entry family, P is
1927 -- an indexed component whose prefix is a selected component (task and
1928 -- entry family), and whose index is the entry family index.
1930 elsif Nkind (P) = N_Indexed_Component
1931 and then Nkind (Prefix (P)) = N_Selected_Component
1932 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1934 Analyze_Call_And_Resolve;
1936 -- If the prefix is the name of an entry family, it is a call from
1937 -- within the task body itself.
1939 elsif Nkind (P) = N_Indexed_Component
1940 and then Nkind (Prefix (P)) = N_Identifier
1941 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1944 Make_Selected_Component (Loc,
1945 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1946 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1947 Rewrite (Prefix (P), New_N);
1949 Analyze_Call_And_Resolve;
1951 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1952 -- procedure name, so the construct can only be a qualified expression.
1954 elsif Nkind (P) = N_Qualified_Expression
1955 and then Ada_Version >= Ada_2012
1957 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1960 -- Anything else is an error
1963 Error_Msg_N ("invalid procedure or entry call", N);
1967 Restore_Ghost_Mode (Saved_GM);
1968 end Analyze_Procedure_Call;
1970 ------------------------------
1971 -- Analyze_Return_Statement --
1972 ------------------------------
1974 procedure Analyze_Return_Statement (N : Node_Id) is
1975 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
1976 N_Simple_Return_Statement));
1978 Returns_Object : constant Boolean :=
1979 Nkind (N) = N_Extended_Return_Statement
1981 (Nkind (N) = N_Simple_Return_Statement
1982 and then Present (Expression (N)));
1983 -- True if we're returning something; that is, "return <expression>;"
1984 -- or "return Result : T [:= ...]". False for "return;". Used for error
1985 -- checking: If Returns_Object is True, N should apply to a function
1986 -- body; otherwise N should apply to a procedure body, entry body,
1987 -- accept statement, or extended return statement.
1989 function Find_What_It_Applies_To return Entity_Id;
1990 -- Find the entity representing the innermost enclosing body, accept
1991 -- statement, or extended return statement. If the result is a callable
1992 -- construct or extended return statement, then this will be the value
1993 -- of the Return_Applies_To attribute. Otherwise, the program is
1994 -- illegal. See RM-6.5(4/2).
1996 -----------------------------
1997 -- Find_What_It_Applies_To --
1998 -----------------------------
2000 function Find_What_It_Applies_To return Entity_Id is
2001 Result : Entity_Id := Empty;
2004 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2005 -- and postconditions.
2007 for J in reverse 0 .. Scope_Stack.Last loop
2008 Result := Scope_Stack.Table (J).Entity;
2009 exit when not Ekind_In (Result, E_Block, E_Loop)
2010 and then Chars (Result) /= Name_uPostconditions;
2013 pragma Assert (Present (Result));
2015 end Find_What_It_Applies_To;
2017 -- Local declarations
2019 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2020 Kind : constant Entity_Kind := Ekind (Scope_Id);
2021 Loc : constant Source_Ptr := Sloc (N);
2022 Stm_Entity : constant Entity_Id :=
2024 (E_Return_Statement, Current_Scope, Loc, 'R');
2026 -- Start of processing for Analyze_Return_Statement
2029 Set_Return_Statement_Entity (N, Stm_Entity);
2031 Set_Etype (Stm_Entity, Standard_Void_Type);
2032 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2034 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2035 -- (4/2): an inner return statement will apply to this extended return.
2037 if Nkind (N) = N_Extended_Return_Statement then
2038 Push_Scope (Stm_Entity);
2041 -- Check that pragma No_Return is obeyed. Don't complain about the
2042 -- implicitly-generated return that is placed at the end.
2044 if No_Return (Scope_Id) and then Comes_From_Source (N) then
2045 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
2048 -- Warn on any unassigned OUT parameters if in procedure
2050 if Ekind (Scope_Id) = E_Procedure then
2051 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2054 -- Check that functions return objects, and other things do not
2056 if Kind = E_Function or else Kind = E_Generic_Function then
2057 if not Returns_Object then
2058 Error_Msg_N ("missing expression in return from function", N);
2061 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
2062 if Returns_Object then
2063 Error_Msg_N ("procedure cannot return value (use function)", N);
2066 elsif Kind = E_Entry or else Kind = E_Entry_Family then
2067 if Returns_Object then
2068 if Is_Protected_Type (Scope (Scope_Id)) then
2069 Error_Msg_N ("entry body cannot return value", N);
2071 Error_Msg_N ("accept statement cannot return value", N);
2075 elsif Kind = E_Return_Statement then
2077 -- We are nested within another return statement, which must be an
2078 -- extended_return_statement.
2080 if Returns_Object then
2081 if Nkind (N) = N_Extended_Return_Statement then
2083 ("extended return statement cannot be nested (use `RETURN;`)",
2086 -- Case of a simple return statement with a value inside extended
2087 -- return statement.
2091 ("return nested in extended return statement cannot return "
2092 & "value (use `RETURN;`)", N);
2097 Error_Msg_N ("illegal context for return statement", N);
2100 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2101 Analyze_Function_Return (N);
2103 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2104 Set_Return_Present (Scope_Id);
2107 if Nkind (N) = N_Extended_Return_Statement then
2111 Kill_Current_Values (Last_Assignment_Only => True);
2112 Check_Unreachable_Code (N);
2114 Analyze_Dimension (N);
2115 end Analyze_Return_Statement;
2117 -------------------------------------
2118 -- Analyze_Simple_Return_Statement --
2119 -------------------------------------
2121 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2123 if Present (Expression (N)) then
2124 Mark_Coextensions (N, Expression (N));
2127 Analyze_Return_Statement (N);
2128 end Analyze_Simple_Return_Statement;
2130 -------------------------
2131 -- Analyze_Return_Type --
2132 -------------------------
2134 procedure Analyze_Return_Type (N : Node_Id) is
2135 Designator : constant Entity_Id := Defining_Entity (N);
2136 Typ : Entity_Id := Empty;
2139 -- Normal case where result definition does not indicate an error
2141 if Result_Definition (N) /= Error then
2142 if Nkind (Result_Definition (N)) = N_Access_Definition then
2143 Check_SPARK_05_Restriction
2144 ("access result is not allowed", Result_Definition (N));
2146 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2149 AD : constant Node_Id :=
2150 Access_To_Subprogram_Definition (Result_Definition (N));
2152 if Present (AD) and then Protected_Present (AD) then
2153 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2155 Typ := Access_Definition (N, Result_Definition (N));
2159 Set_Parent (Typ, Result_Definition (N));
2160 Set_Is_Local_Anonymous_Access (Typ);
2161 Set_Etype (Designator, Typ);
2163 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2165 Null_Exclusion_Static_Checks (N);
2167 -- Subtype_Mark case
2170 Find_Type (Result_Definition (N));
2171 Typ := Entity (Result_Definition (N));
2172 Set_Etype (Designator, Typ);
2174 -- Unconstrained array as result is not allowed in SPARK
2176 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
2177 Check_SPARK_05_Restriction
2178 ("returning an unconstrained array is not allowed",
2179 Result_Definition (N));
2182 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2184 Null_Exclusion_Static_Checks (N);
2186 -- If a null exclusion is imposed on the result type, then create
2187 -- a null-excluding itype (an access subtype) and use it as the
2188 -- function's Etype. Note that the null exclusion checks are done
2189 -- right before this, because they don't get applied to types that
2190 -- do not come from source.
2192 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2193 Set_Etype (Designator,
2194 Create_Null_Excluding_Itype
2197 Scope_Id => Scope (Current_Scope)));
2199 -- The new subtype must be elaborated before use because
2200 -- it is visible outside of the function. However its base
2201 -- type may not be frozen yet, so the reference that will
2202 -- force elaboration must be attached to the freezing of
2205 -- If the return specification appears on a proper body,
2206 -- the subtype will have been created already on the spec.
2208 if Is_Frozen (Typ) then
2209 if Nkind (Parent (N)) = N_Subprogram_Body
2210 and then Nkind (Parent (Parent (N))) = N_Subunit
2214 Build_Itype_Reference (Etype (Designator), Parent (N));
2218 Ensure_Freeze_Node (Typ);
2221 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2223 Set_Itype (IR, Etype (Designator));
2224 Append_Freeze_Actions (Typ, New_List (IR));
2229 Set_Etype (Designator, Typ);
2232 if Ekind (Typ) = E_Incomplete_Type
2233 or else (Is_Class_Wide_Type (Typ)
2234 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2236 -- AI05-0151: Tagged incomplete types are allowed in all formal
2237 -- parts. Untagged incomplete types are not allowed in bodies.
2238 -- As a consequence, limited views cannot appear in a basic
2239 -- declaration that is itself within a body, because there is
2240 -- no point at which the non-limited view will become visible.
2242 if Ada_Version >= Ada_2012 then
2243 if From_Limited_With (Typ) and then In_Package_Body then
2245 ("invalid use of incomplete type&",
2246 Result_Definition (N), Typ);
2248 -- The return type of a subprogram body cannot be of a
2249 -- formal incomplete type.
2251 elsif Is_Generic_Type (Typ)
2252 and then Nkind (Parent (N)) = N_Subprogram_Body
2255 ("return type cannot be a formal incomplete type",
2256 Result_Definition (N));
2258 elsif Is_Class_Wide_Type (Typ)
2259 and then Is_Generic_Type (Root_Type (Typ))
2260 and then Nkind (Parent (N)) = N_Subprogram_Body
2263 ("return type cannot be a formal incomplete type",
2264 Result_Definition (N));
2266 elsif Is_Tagged_Type (Typ) then
2269 -- Use is legal in a thunk generated for an operation
2270 -- inherited from a progenitor.
2272 elsif Is_Thunk (Designator)
2273 and then Present (Non_Limited_View (Typ))
2277 elsif Nkind (Parent (N)) = N_Subprogram_Body
2278 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2282 ("invalid use of untagged incomplete type&",
2286 -- The type must be completed in the current package. This
2287 -- is checked at the end of the package declaration when
2288 -- Taft-amendment types are identified. If the return type
2289 -- is class-wide, there is no required check, the type can
2290 -- be a bona fide TAT.
2292 if Ekind (Scope (Current_Scope)) = E_Package
2293 and then In_Private_Part (Scope (Current_Scope))
2294 and then not Is_Class_Wide_Type (Typ)
2296 Append_Elmt (Designator, Private_Dependents (Typ));
2301 ("invalid use of incomplete type&", Designator, Typ);
2306 -- Case where result definition does indicate an error
2309 Set_Etype (Designator, Any_Type);
2311 end Analyze_Return_Type;
2313 -----------------------------
2314 -- Analyze_Subprogram_Body --
2315 -----------------------------
2317 procedure Analyze_Subprogram_Body (N : Node_Id) is
2318 Loc : constant Source_Ptr := Sloc (N);
2319 Body_Spec : constant Node_Id := Specification (N);
2320 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2323 if Debug_Flag_C then
2324 Write_Str ("==> subprogram body ");
2325 Write_Name (Chars (Body_Id));
2326 Write_Str (" from ");
2327 Write_Location (Loc);
2332 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2334 -- The real work is split out into the helper, so it can do "return;"
2335 -- without skipping the debug output:
2337 Analyze_Subprogram_Body_Helper (N);
2339 if Debug_Flag_C then
2341 Write_Str ("<== subprogram body ");
2342 Write_Name (Chars (Body_Id));
2343 Write_Str (" from ");
2344 Write_Location (Loc);
2347 end Analyze_Subprogram_Body;
2349 ------------------------------------
2350 -- Analyze_Subprogram_Body_Helper --
2351 ------------------------------------
2353 -- This procedure is called for regular subprogram bodies, generic bodies,
2354 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2355 -- specification matters, and is used to create a proper declaration for
2356 -- the subprogram, or to perform conformance checks.
2358 -- WARNING: This routine manages Ghost regions. Return statements must be
2359 -- replaced by gotos which jump to the end of the routine and restore the
2362 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2363 Body_Spec : Node_Id := Specification (N);
2364 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2365 Loc : constant Source_Ptr := Sloc (N);
2366 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2368 Conformant : Boolean;
2369 Desig_View : Entity_Id := Empty;
2370 Exch_Views : Elist_Id := No_Elist;
2372 Prot_Typ : Entity_Id := Empty;
2373 Spec_Decl : Node_Id := Empty;
2374 Spec_Id : Entity_Id;
2376 Last_Real_Spec_Entity : Entity_Id := Empty;
2377 -- When we analyze a separate spec, the entity chain ends up containing
2378 -- the formals, as well as any itypes generated during analysis of the
2379 -- default expressions for parameters, or the arguments of associated
2380 -- precondition/postcondition pragmas (which are analyzed in the context
2381 -- of the spec since they have visibility on formals).
2383 -- These entities belong with the spec and not the body. However we do
2384 -- the analysis of the body in the context of the spec (again to obtain
2385 -- visibility to the formals), and all the entities generated during
2386 -- this analysis end up also chained to the entity chain of the spec.
2387 -- But they really belong to the body, and there is circuitry to move
2388 -- them from the spec to the body.
2390 -- However, when we do this move, we don't want to move the real spec
2391 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2392 -- variable points to the last real spec entity, so we only move those
2393 -- chained beyond that point. It is initialized to Empty to deal with
2394 -- the case where there is no separate spec.
2396 function Body_Has_Contract return Boolean;
2397 -- Check whether unanalyzed body has an aspect or pragma that may
2398 -- generate a SPARK contract.
2400 function Body_Has_SPARK_Mode_On return Boolean;
2401 -- Check whether SPARK_Mode On applies to the subprogram body, either
2402 -- because it is specified directly on the body, or because it is
2403 -- inherited from the enclosing subprogram or package.
2405 procedure Build_Subprogram_Declaration;
2406 -- Create a matching subprogram declaration for subprogram body N
2408 procedure Check_Anonymous_Return;
2409 -- Ada 2005: if a function returns an access type that denotes a task,
2410 -- or a type that contains tasks, we must create a master entity for
2411 -- the anonymous type, which typically will be used in an allocator
2412 -- in the body of the function.
2414 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2415 -- Look ahead to recognize a pragma that may appear after the body.
2416 -- If there is a previous spec, check that it appears in the same
2417 -- declarative part. If the pragma is Inline_Always, perform inlining
2418 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2419 -- If the body acts as a spec, and inlining is required, we create a
2420 -- subprogram declaration for it, in order to attach the body to inline.
2421 -- If pragma does not appear after the body, check whether there is
2422 -- an inline pragma before any local declarations.
2424 procedure Check_Missing_Return;
2425 -- Checks for a function with a no return statements, and also performs
2426 -- the warning checks implemented by Check_Returns. In formal mode, also
2427 -- verify that a function ends with a RETURN and that a procedure does
2428 -- not contain any RETURN.
2430 function Disambiguate_Spec return Entity_Id;
2431 -- When a primitive is declared between the private view and the full
2432 -- view of a concurrent type which implements an interface, a special
2433 -- mechanism is used to find the corresponding spec of the primitive
2436 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2437 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2438 -- incomplete types coming from a limited context and replace their
2439 -- limited views with the non-limited ones. Return the list of changes
2440 -- to be used to undo the transformation.
2442 function Is_Private_Concurrent_Primitive
2443 (Subp_Id : Entity_Id) return Boolean;
2444 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2445 -- type that implements an interface and has a private view.
2447 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2448 -- Undo the transformation done by Exchange_Limited_Views.
2450 procedure Set_Trivial_Subprogram (N : Node_Id);
2451 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2452 -- subprogram whose body is being analyzed. N is the statement node
2453 -- causing the flag to be set, if the following statement is a return
2454 -- of an entity, we mark the entity as set in source to suppress any
2455 -- warning on the stylized use of function stubs with a dummy return.
2457 procedure Verify_Overriding_Indicator;
2458 -- If there was a previous spec, the entity has been entered in the
2459 -- current scope previously. If the body itself carries an overriding
2460 -- indicator, check that it is consistent with the known status of the
2463 -----------------------
2464 -- Body_Has_Contract --
2465 -----------------------
2467 function Body_Has_Contract return Boolean is
2468 Decls : constant List_Id := Declarations (N);
2472 -- Check for aspects that may generate a contract
2474 if Present (Aspect_Specifications (N)) then
2475 Item := First (Aspect_Specifications (N));
2476 while Present (Item) loop
2477 if Is_Subprogram_Contract_Annotation (Item) then
2485 -- Check for pragmas that may generate a contract
2487 if Present (Decls) then
2488 Item := First (Decls);
2489 while Present (Item) loop
2490 if Nkind (Item) = N_Pragma
2491 and then Is_Subprogram_Contract_Annotation (Item)
2501 end Body_Has_Contract;
2503 ----------------------------
2504 -- Body_Has_SPARK_Mode_On --
2505 ----------------------------
2507 function Body_Has_SPARK_Mode_On return Boolean is
2508 Decls : constant List_Id := Declarations (N);
2512 -- Check for SPARK_Mode aspect
2514 if Present (Aspect_Specifications (N)) then
2515 Item := First (Aspect_Specifications (N));
2516 while Present (Item) loop
2517 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2518 return Get_SPARK_Mode_From_Annotation (Item) = On;
2525 -- Check for SPARK_Mode pragma
2527 if Present (Decls) then
2528 Item := First (Decls);
2529 while Present (Item) loop
2531 -- Pragmas that apply to a subprogram body are usually grouped
2532 -- together. Look for a potential pragma SPARK_Mode among them.
2534 if Nkind (Item) = N_Pragma then
2535 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2536 return Get_SPARK_Mode_From_Annotation (Item) = On;
2539 -- Otherwise the first non-pragma declarative item terminates
2540 -- the region where pragma SPARK_Mode may appear.
2550 -- Otherwise, the applicable SPARK_Mode is inherited from the
2551 -- enclosing subprogram or package.
2553 return SPARK_Mode = On;
2554 end Body_Has_SPARK_Mode_On;
2556 ----------------------------------
2557 -- Build_Subprogram_Declaration --
2558 ----------------------------------
2560 procedure Build_Subprogram_Declaration is
2561 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2562 -- Relocate certain categorization pragmas from the declarative list
2563 -- of subprogram body From and insert them after node To. The pragmas
2566 -- Volatile_Function
2567 -- Also copy pragma SPARK_Mode if present in the declarative list
2568 -- of subprogram body From and insert it after node To. This pragma
2569 -- should not be moved, as it applies to the body too.
2575 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
2577 Next_Decl : Node_Id;
2580 pragma Assert (Nkind (From) = N_Subprogram_Body);
2582 -- The destination node must be part of a list, as the pragmas are
2583 -- inserted after it.
2585 pragma Assert (Is_List_Member (To));
2587 -- Inspect the declarations of the subprogram body looking for
2588 -- specific pragmas.
2590 Decl := First (Declarations (N));
2591 while Present (Decl) loop
2592 Next_Decl := Next (Decl);
2594 if Nkind (Decl) = N_Pragma then
2595 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
2596 Insert_After (To, New_Copy_Tree (Decl));
2598 elsif Nam_In (Pragma_Name_Unmapped (Decl),
2600 Name_Volatile_Function)
2603 Insert_After (To, Decl);
2614 Subp_Decl : Node_Id;
2616 -- Start of processing for Build_Subprogram_Declaration
2619 -- Create a matching subprogram spec using the profile of the body.
2620 -- The structure of the tree is identical, but has new entities for
2621 -- the defining unit name and formal parameters.
2624 Make_Subprogram_Declaration (Loc,
2625 Specification => Copy_Subprogram_Spec (Body_Spec));
2626 Set_Comes_From_Source (Subp_Decl, True);
2628 -- Relocate the aspects and relevant pragmas from the subprogram body
2629 -- to the generated spec because it acts as the initial declaration.
2631 Insert_Before (N, Subp_Decl);
2632 Move_Aspects (N, To => Subp_Decl);
2633 Move_Pragmas (N, To => Subp_Decl);
2635 -- Ensure that the generated corresponding spec and original body
2636 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2637 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2638 -- correctly set for local subprograms.
2640 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2642 Analyze (Subp_Decl);
2644 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2645 -- the body since the expander may generate calls using that entity.
2646 -- Required to ensure that Expand_Call rewrites calls to this
2647 -- function by calls to the built procedure.
2649 if Modify_Tree_For_C
2650 and then Nkind (Body_Spec) = N_Function_Specification
2652 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2654 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2655 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2656 Corresponding_Procedure
2657 (Defining_Entity (Specification (Subp_Decl))));
2660 -- Analyze any relocated source pragmas or pragmas created for aspect
2663 Decl := Next (Subp_Decl);
2664 while Present (Decl) loop
2666 -- Stop the search for pragmas once the body has been reached as
2667 -- this terminates the region where pragmas may appear.
2672 elsif Nkind (Decl) = N_Pragma then
2679 Spec_Id := Defining_Entity (Subp_Decl);
2680 Set_Corresponding_Spec (N, Spec_Id);
2682 -- Mark the generated spec as a source construct to ensure that all
2683 -- calls to it are properly registered in ALI files for GNATprove.
2685 Set_Comes_From_Source (Spec_Id, True);
2687 -- Ensure that the specs of the subprogram declaration and its body
2688 -- are identical, otherwise they will appear non-conformant due to
2689 -- rewritings in the default values of formal parameters.
2691 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2692 Set_Specification (N, Body_Spec);
2693 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2694 end Build_Subprogram_Declaration;
2696 ----------------------------
2697 -- Check_Anonymous_Return --
2698 ----------------------------
2700 procedure Check_Anonymous_Return is
2706 if Present (Spec_Id) then
2712 if Ekind (Scop) = E_Function
2713 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2714 and then not Is_Thunk (Scop)
2716 -- Skip internally built functions which handle the case of
2717 -- a null access (see Expand_Interface_Conversion)
2719 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2720 and then not Comes_From_Source (Parent (Scop)))
2722 and then (Has_Task (Designated_Type (Etype (Scop)))
2724 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2726 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2727 and then Expander_Active
2729 -- Avoid cases with no tasking support
2731 and then RTE_Available (RE_Current_Master)
2732 and then not Restriction_Active (No_Task_Hierarchy)
2735 Make_Object_Declaration (Loc,
2736 Defining_Identifier =>
2737 Make_Defining_Identifier (Loc, Name_uMaster),
2738 Constant_Present => True,
2739 Object_Definition =>
2740 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2742 Make_Explicit_Dereference (Loc,
2743 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2745 if Present (Declarations (N)) then
2746 Prepend (Decl, Declarations (N));
2748 Set_Declarations (N, New_List (Decl));
2751 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2752 Set_Has_Master_Entity (Scop);
2754 -- Now mark the containing scope as a task master
2757 while Nkind (Par) /= N_Compilation_Unit loop
2758 Par := Parent (Par);
2759 pragma Assert (Present (Par));
2761 -- If we fall off the top, we are at the outer level, and
2762 -- the environment task is our effective master, so nothing
2766 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2768 Set_Is_Task_Master (Par, True);
2773 end Check_Anonymous_Return;
2775 -------------------------
2776 -- Check_Inline_Pragma --
2777 -------------------------
2779 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2783 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2784 -- True when N is a pragma Inline or Inline_Always that applies
2785 -- to this subprogram.
2787 -----------------------
2788 -- Is_Inline_Pragma --
2789 -----------------------
2791 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2793 if Nkind (N) = N_Pragma
2795 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2796 or else (Pragma_Name_Unmapped (N) = Name_Inline
2798 (Front_End_Inlining or else Optimization_Level > 0)))
2799 and then Present (Pragma_Argument_Associations (N))
2802 Pragma_Arg : Node_Id :=
2803 Expression (First (Pragma_Argument_Associations (N)));
2805 if Nkind (Pragma_Arg) = N_Selected_Component then
2806 Pragma_Arg := Selector_Name (Pragma_Arg);
2809 return Chars (Pragma_Arg) = Chars (Body_Id);
2815 end Is_Inline_Pragma;
2817 -- Start of processing for Check_Inline_Pragma
2820 if not Expander_Active then
2824 if Is_List_Member (N)
2825 and then Present (Next (N))
2826 and then Is_Inline_Pragma (Next (N))
2830 elsif Nkind (N) /= N_Subprogram_Body_Stub
2831 and then Present (Declarations (N))
2832 and then Is_Inline_Pragma (First (Declarations (N)))
2834 Prag := First (Declarations (N));
2840 if Present (Prag) then
2841 if Present (Spec_Id) then
2842 if Is_List_Member (N)
2843 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
2844 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2850 -- Create a subprogram declaration, to make treatment uniform.
2851 -- Make the sloc of the subprogram name that of the entity in
2852 -- the body, so that style checks find identical strings.
2855 Subp : constant Entity_Id :=
2856 Make_Defining_Identifier
2857 (Sloc (Body_Id), Chars (Body_Id));
2858 Decl : constant Node_Id :=
2859 Make_Subprogram_Declaration (Loc,
2861 New_Copy_Tree (Specification (N)));
2864 Set_Defining_Unit_Name (Specification (Decl), Subp);
2866 -- To ensure proper coverage when body is inlined, indicate
2867 -- whether the subprogram comes from source.
2869 Set_Comes_From_Source (Subp, Comes_From_Source (N));
2871 if Present (First_Formal (Body_Id)) then
2872 Plist := Copy_Parameter_List (Body_Id);
2873 Set_Parameter_Specifications
2874 (Specification (Decl), Plist);
2877 -- Move aspects to the new spec
2879 if Has_Aspects (N) then
2880 Move_Aspects (N, To => Decl);
2883 Insert_Before (N, Decl);
2886 Set_Has_Pragma_Inline (Subp);
2888 if Pragma_Name (Prag) = Name_Inline_Always then
2889 Set_Is_Inlined (Subp);
2890 Set_Has_Pragma_Inline_Always (Subp);
2893 -- Prior to copying the subprogram body to create a template
2894 -- for it for subsequent inlining, remove the pragma from
2895 -- the current body so that the copy that will produce the
2896 -- new body will start from a completely unanalyzed tree.
2898 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2899 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2906 end Check_Inline_Pragma;
2908 --------------------------
2909 -- Check_Missing_Return --
2910 --------------------------
2912 procedure Check_Missing_Return is
2914 Missing_Ret : Boolean;
2917 if Nkind (Body_Spec) = N_Function_Specification then
2918 if Present (Spec_Id) then
2924 if Return_Present (Id) then
2925 Check_Returns (HSS, 'F', Missing_Ret);
2928 Set_Has_Missing_Return (Id);
2931 -- Within a premature instantiation of a package with no body, we
2932 -- build completions of the functions therein, with a Raise
2933 -- statement. No point in complaining about a missing return in
2936 elsif Ekind (Id) = E_Function
2937 and then In_Instance
2938 and then Present (Statements (HSS))
2939 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2943 elsif Is_Generic_Subprogram (Id)
2944 or else not Is_Machine_Code_Subprogram (Id)
2946 Error_Msg_N ("missing RETURN statement in function body", N);
2949 -- If procedure with No_Return, check returns
2951 elsif Nkind (Body_Spec) = N_Procedure_Specification
2952 and then Present (Spec_Id)
2953 and then No_Return (Spec_Id)
2955 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2958 -- Special checks in SPARK mode
2960 if Nkind (Body_Spec) = N_Function_Specification then
2962 -- In SPARK mode, last statement of a function should be a return
2965 Stat : constant Node_Id := Last_Source_Statement (HSS);
2968 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2969 N_Extended_Return_Statement)
2971 Check_SPARK_05_Restriction
2972 ("last statement in function should be RETURN", Stat);
2976 -- In SPARK mode, verify that a procedure has no return
2978 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2979 if Present (Spec_Id) then
2985 -- Would be nice to point to return statement here, can we
2986 -- borrow the Check_Returns procedure here ???
2988 if Return_Present (Id) then
2989 Check_SPARK_05_Restriction
2990 ("procedure should not have RETURN", N);
2993 end Check_Missing_Return;
2995 -----------------------
2996 -- Disambiguate_Spec --
2997 -----------------------
2999 function Disambiguate_Spec return Entity_Id is
3000 Priv_Spec : Entity_Id;
3003 procedure Replace_Types (To_Corresponding : Boolean);
3004 -- Depending on the flag, replace the type of formal parameters of
3005 -- Body_Id if it is a concurrent type implementing interfaces with
3006 -- the corresponding record type or the other way around.
3008 procedure Replace_Types (To_Corresponding : Boolean) is
3010 Formal_Typ : Entity_Id;
3013 Formal := First_Formal (Body_Id);
3014 while Present (Formal) loop
3015 Formal_Typ := Etype (Formal);
3017 if Is_Class_Wide_Type (Formal_Typ) then
3018 Formal_Typ := Root_Type (Formal_Typ);
3021 -- From concurrent type to corresponding record
3023 if To_Corresponding then
3024 if Is_Concurrent_Type (Formal_Typ)
3025 and then Present (Corresponding_Record_Type (Formal_Typ))
3028 (Corresponding_Record_Type (Formal_Typ)))
3031 Corresponding_Record_Type (Formal_Typ));
3034 -- From corresponding record to concurrent type
3037 if Is_Concurrent_Record_Type (Formal_Typ)
3038 and then Present (Interfaces (Formal_Typ))
3041 Corresponding_Concurrent_Type (Formal_Typ));
3045 Next_Formal (Formal);
3049 -- Start of processing for Disambiguate_Spec
3052 -- Try to retrieve the specification of the body as is. All error
3053 -- messages are suppressed because the body may not have a spec in
3054 -- its current state.
3056 Spec_N := Find_Corresponding_Spec (N, False);
3058 -- It is possible that this is the body of a primitive declared
3059 -- between a private and a full view of a concurrent type. The
3060 -- controlling parameter of the spec carries the concurrent type,
3061 -- not the corresponding record type as transformed by Analyze_
3062 -- Subprogram_Specification. In such cases, we undo the change
3063 -- made by the analysis of the specification and try to find the
3066 -- Note that wrappers already have their corresponding specs and
3067 -- bodies set during their creation, so if the candidate spec is
3068 -- a wrapper, then we definitely need to swap all types to their
3069 -- original concurrent status.
3072 or else Is_Primitive_Wrapper (Spec_N)
3074 -- Restore all references of corresponding record types to the
3075 -- original concurrent types.
3077 Replace_Types (To_Corresponding => False);
3078 Priv_Spec := Find_Corresponding_Spec (N, False);
3080 -- The current body truly belongs to a primitive declared between
3081 -- a private and a full view. We leave the modified body as is,
3082 -- and return the true spec.
3084 if Present (Priv_Spec)
3085 and then Is_Private_Primitive (Priv_Spec)
3090 -- In case that this is some sort of error, restore the original
3091 -- state of the body.
3093 Replace_Types (To_Corresponding => True);
3097 end Disambiguate_Spec;
3099 ----------------------------
3100 -- Exchange_Limited_Views --
3101 ----------------------------
3103 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3104 Result : Elist_Id := No_Elist;
3106 procedure Detect_And_Exchange (Id : Entity_Id);
3107 -- Determine whether Id's type denotes an incomplete type associated
3108 -- with a limited with clause and exchange the limited view with the
3109 -- non-limited one when available. Note that the non-limited view
3110 -- may exist because of a with_clause in another unit in the context,
3111 -- but cannot be used because the current view of the enclosing unit
3112 -- is still a limited view.
3114 -------------------------
3115 -- Detect_And_Exchange --
3116 -------------------------
3118 procedure Detect_And_Exchange (Id : Entity_Id) is
3119 Typ : constant Entity_Id := Etype (Id);
3121 if From_Limited_With (Typ)
3122 and then Has_Non_Limited_View (Typ)
3123 and then not From_Limited_With (Scope (Typ))
3126 Result := New_Elmt_List;
3129 Prepend_Elmt (Typ, Result);
3130 Prepend_Elmt (Id, Result);
3131 Set_Etype (Id, Non_Limited_View (Typ));
3133 end Detect_And_Exchange;
3139 -- Start of processing for Exchange_Limited_Views
3142 -- Do not process subprogram bodies as they already use the non-
3143 -- limited view of types.
3145 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3149 -- Examine all formals and swap views when applicable
3151 Formal := First_Formal (Subp_Id);
3152 while Present (Formal) loop
3153 Detect_And_Exchange (Formal);
3155 Next_Formal (Formal);
3158 -- Process the return type of a function
3160 if Ekind (Subp_Id) = E_Function then
3161 Detect_And_Exchange (Subp_Id);
3165 end Exchange_Limited_Views;
3167 -------------------------------------
3168 -- Is_Private_Concurrent_Primitive --
3169 -------------------------------------
3171 function Is_Private_Concurrent_Primitive
3172 (Subp_Id : Entity_Id) return Boolean
3174 Formal_Typ : Entity_Id;
3177 if Present (First_Formal (Subp_Id)) then
3178 Formal_Typ := Etype (First_Formal (Subp_Id));
3180 if Is_Concurrent_Record_Type (Formal_Typ) then
3181 if Is_Class_Wide_Type (Formal_Typ) then
3182 Formal_Typ := Root_Type (Formal_Typ);
3185 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3188 -- The type of the first formal is a concurrent tagged type with
3192 Is_Concurrent_Type (Formal_Typ)
3193 and then Is_Tagged_Type (Formal_Typ)
3194 and then Has_Private_Declaration (Formal_Typ);
3198 end Is_Private_Concurrent_Primitive;
3200 ---------------------------
3201 -- Restore_Limited_Views --
3202 ---------------------------
3204 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3205 Elmt : Elmt_Id := First_Elmt (Restore_List);
3209 while Present (Elmt) loop
3212 Set_Etype (Id, Node (Elmt));
3215 end Restore_Limited_Views;
3217 ----------------------------
3218 -- Set_Trivial_Subprogram --
3219 ----------------------------
3221 procedure Set_Trivial_Subprogram (N : Node_Id) is
3222 Nxt : constant Node_Id := Next (N);
3225 Set_Is_Trivial_Subprogram (Body_Id);
3227 if Present (Spec_Id) then
3228 Set_Is_Trivial_Subprogram (Spec_Id);
3232 and then Nkind (Nxt) = N_Simple_Return_Statement
3233 and then No (Next (Nxt))
3234 and then Present (Expression (Nxt))
3235 and then Is_Entity_Name (Expression (Nxt))
3237 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3239 end Set_Trivial_Subprogram;
3241 ---------------------------------
3242 -- Verify_Overriding_Indicator --
3243 ---------------------------------
3245 procedure Verify_Overriding_Indicator is
3247 if Must_Override (Body_Spec) then
3248 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3249 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3253 elsif not Present (Overridden_Operation (Spec_Id)) then
3255 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3257 -- Overriding indicators aren't allowed for protected subprogram
3258 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3259 -- this to a warning if -gnatd.E is enabled.
3261 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3262 Error_Msg_Warn := Error_To_Warning;
3264 ("<<overriding indicator not allowed for protected "
3265 & "subprogram body", Body_Spec);
3268 elsif Must_Not_Override (Body_Spec) then
3269 if Present (Overridden_Operation (Spec_Id)) then
3271 ("subprogram& overrides inherited operation",
3272 Body_Spec, Spec_Id);
3274 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3275 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3278 ("subprogram& overrides predefined operator ",
3279 Body_Spec, Spec_Id);
3281 -- Overriding indicators aren't allowed for protected subprogram
3282 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3283 -- this to a warning if -gnatd.E is enabled.
3285 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3286 Error_Msg_Warn := Error_To_Warning;
3289 ("<<overriding indicator not allowed "
3290 & "for protected subprogram body", Body_Spec);
3292 -- If this is not a primitive operation, then the overriding
3293 -- indicator is altogether illegal.
3295 elsif not Is_Primitive (Spec_Id) then
3297 ("overriding indicator only allowed "
3298 & "if subprogram is primitive", Body_Spec);
3301 -- If checking the style rule and the operation overrides, then
3302 -- issue a warning about a missing overriding_indicator. Protected
3303 -- subprogram bodies are excluded from this style checking, since
3304 -- they aren't primitives (even though their declarations can
3305 -- override) and aren't allowed to have an overriding_indicator.
3308 and then Present (Overridden_Operation (Spec_Id))
3309 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3311 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3312 Style.Missing_Overriding (N, Body_Id);
3315 and then Can_Override_Operator (Spec_Id)
3316 and then not In_Predefined_Unit (Spec_Id)
3318 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3319 Style.Missing_Overriding (N, Body_Id);
3321 end Verify_Overriding_Indicator;
3325 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3326 Saved_ISMP : constant Boolean :=
3327 Ignore_SPARK_Mode_Pragmas_In_Instance;
3328 -- Save the Ghost and SPARK mode-related data to restore on exit
3330 -- Start of processing for Analyze_Subprogram_Body_Helper
3333 -- A [generic] subprogram body "freezes" the contract of the nearest
3334 -- enclosing package body and all other contracts encountered in the
3335 -- same declarative part up to and excluding the subprogram body:
3337 -- package body Nearest_Enclosing_Package
3338 -- with Refined_State => (State => Constit)
3342 -- procedure Freezes_Enclosing_Package_Body
3343 -- with Refined_Depends => (Input => Constit) ...
3345 -- This ensures that any annotations referenced by the contract of the
3346 -- [generic] subprogram body are available. This form of "freezing" is
3347 -- decoupled from the usual Freeze_xxx mechanism because it must also
3348 -- work in the context of generics where normal freezing is disabled.
3350 -- Only bodies coming from source should cause this type of "freezing".
3351 -- Expression functions that act as bodies and complete an initial
3352 -- declaration must be included in this category, hence the use of
3355 if Comes_From_Source (Original_Node (N)) then
3356 Analyze_Previous_Contracts (N);
3359 -- Generic subprograms are handled separately. They always have a
3360 -- generic specification. Determine whether current scope has a
3361 -- previous declaration.
3363 -- If the subprogram body is defined within an instance of the same
3364 -- name, the instance appears as a package renaming, and will be hidden
3365 -- within the subprogram.
3367 if Present (Prev_Id)
3368 and then not Is_Overloadable (Prev_Id)
3369 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3370 or else Comes_From_Source (Prev_Id))
3372 if Is_Generic_Subprogram (Prev_Id) then
3375 -- A subprogram body is Ghost when it is stand alone and subject
3376 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3377 -- the mode now to ensure that any nodes generated during analysis
3378 -- and expansion are properly marked as Ghost.
3380 Mark_And_Set_Ghost_Body (N, Spec_Id);
3382 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3383 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3385 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3387 if Nkind (N) = N_Subprogram_Body then
3388 HSS := Handled_Statement_Sequence (N);
3389 Check_Missing_Return;
3394 -- Otherwise a previous entity conflicts with the subprogram name.
3395 -- Attempting to enter name will post error.
3398 Enter_Name (Body_Id);
3402 -- Non-generic case, find the subprogram declaration, if one was seen,
3403 -- or enter new overloaded entity in the current scope. If the
3404 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3405 -- part of the context of one of its subunits. No need to redo the
3408 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3412 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3414 if Nkind (N) = N_Subprogram_Body_Stub
3415 or else No (Corresponding_Spec (N))
3417 if Is_Private_Concurrent_Primitive (Body_Id) then
3418 Spec_Id := Disambiguate_Spec;
3420 -- A subprogram body is Ghost when it is stand alone and
3421 -- subject to pragma Ghost or when the corresponding spec is
3422 -- Ghost. Set the mode now to ensure that any nodes generated
3423 -- during analysis and expansion are properly marked as Ghost.
3425 Mark_And_Set_Ghost_Body (N, Spec_Id);
3428 Spec_Id := Find_Corresponding_Spec (N);
3430 -- A subprogram body is Ghost when it is stand alone and
3431 -- subject to pragma Ghost or when the corresponding spec is
3432 -- Ghost. Set the mode now to ensure that any nodes generated
3433 -- during analysis and expansion are properly marked as Ghost.
3435 Mark_And_Set_Ghost_Body (N, Spec_Id);
3437 -- In GNATprove mode, if the body has no previous spec, create
3438 -- one so that the inlining machinery can operate properly.
3439 -- Transfer aspects, if any, to the new spec, so that they
3440 -- are legal and can be processed ahead of the body.
3441 -- We make two copies of the given spec, one for the new
3442 -- declaration, and one for the body.
3444 if No (Spec_Id) and then GNATprove_Mode
3446 -- Inlining does not apply during pre-analysis of code
3448 and then Full_Analysis
3450 -- Inlining only applies to full bodies, not stubs
3452 and then Nkind (N) /= N_Subprogram_Body_Stub
3454 -- Inlining only applies to bodies in the source code, not to
3455 -- those generated by the compiler. In particular, expression
3456 -- functions, whose body is generated by the compiler, are
3457 -- treated specially by GNATprove.
3459 and then Comes_From_Source (Body_Id)
3461 -- This cannot be done for a compilation unit, which is not
3462 -- in a context where we can insert a new spec.
3464 and then Is_List_Member (N)
3466 -- Inlining only applies to subprograms without contracts,
3467 -- as a contract is a sign that GNATprove should perform a
3468 -- modular analysis of the subprogram instead of a contextual
3469 -- analysis at each call site. The same test is performed in
3470 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3471 -- here in another form (because the contract has not been
3472 -- attached to the body) to avoid front-end errors in case
3473 -- pragmas are used instead of aspects, because the
3474 -- corresponding pragmas in the body would not be transferred
3475 -- to the spec, leading to legality errors.
3477 and then not Body_Has_Contract
3478 and then not Inside_A_Generic
3480 Build_Subprogram_Declaration;
3482 -- If this is a function that returns a constrained array, and
3483 -- we are generating SPARK_For_C, create subprogram declaration
3484 -- to simplify subsequent C generation.
3487 and then Modify_Tree_For_C
3488 and then Nkind (Body_Spec) = N_Function_Specification
3489 and then Is_Array_Type (Etype (Body_Id))
3490 and then Is_Constrained (Etype (Body_Id))
3492 Build_Subprogram_Declaration;
3496 -- If this is a duplicate body, no point in analyzing it
3498 if Error_Posted (N) then
3502 -- A subprogram body should cause freezing of its own declaration,
3503 -- but if there was no previous explicit declaration, then the
3504 -- subprogram will get frozen too late (there may be code within
3505 -- the body that depends on the subprogram having been frozen,
3506 -- such as uses of extra formals), so we force it to be frozen
3507 -- here. Same holds if the body and spec are compilation units.
3508 -- Finally, if the return type is an anonymous access to protected
3509 -- subprogram, it must be frozen before the body because its
3510 -- expansion has generated an equivalent type that is used when
3511 -- elaborating the body.
3513 -- An exception in the case of Ada 2012, AI05-177: The bodies
3514 -- created for expression functions do not freeze.
3517 and then Nkind (Original_Node (N)) /= N_Expression_Function
3519 Freeze_Before (N, Body_Id);
3521 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3522 Freeze_Before (N, Spec_Id);
3524 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3525 Freeze_Before (N, Etype (Body_Id));
3529 Spec_Id := Corresponding_Spec (N);
3531 -- A subprogram body is Ghost when it is stand alone and subject
3532 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3533 -- the mode now to ensure that any nodes generated during analysis
3534 -- and expansion are properly marked as Ghost.
3536 Mark_And_Set_Ghost_Body (N, Spec_Id);
3540 -- Previously we scanned the body to look for nested subprograms, and
3541 -- rejected an inline directive if nested subprograms were present,
3542 -- because the back-end would generate conflicting symbols for the
3543 -- nested bodies. This is now unnecessary.
3545 -- Look ahead to recognize a pragma Inline that appears after the body
3547 Check_Inline_Pragma (Spec_Id);
3549 -- Deal with special case of a fully private operation in the body of
3550 -- the protected type. We must create a declaration for the subprogram,
3551 -- in order to attach the protected subprogram that will be used in
3552 -- internal calls. We exclude compiler generated bodies from the
3553 -- expander since the issue does not arise for those cases.
3556 and then Comes_From_Source (N)
3557 and then Is_Protected_Type (Current_Scope)
3559 Spec_Id := Build_Private_Protected_Declaration (N);
3562 -- If we are generating C and this is a function returning a constrained
3563 -- array type for which we must create a procedure with an extra out
3564 -- parameter, build and analyze the body now. The procedure declaration
3565 -- has already been created. We reuse the source body of the function,
3566 -- because in an instance it may contain global references that cannot
3567 -- be reanalyzed. The source function itself is not used any further,
3568 -- so we mark it as having a completion. If the subprogram is a stub the
3569 -- transformation is done later, when the proper body is analyzed.
3572 and then Modify_Tree_For_C
3573 and then Present (Spec_Id)
3574 and then Ekind (Spec_Id) = E_Function
3575 and then Nkind (N) /= N_Subprogram_Body_Stub
3576 and then Rewritten_For_C (Spec_Id)
3578 Set_Has_Completion (Spec_Id);
3580 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3583 -- The entity for the created procedure must remain invisible, so it
3584 -- does not participate in resolution of subsequent references to the
3587 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3591 -- If a separate spec is present, then deal with freezing issues
3593 if Present (Spec_Id) then
3594 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3595 Verify_Overriding_Indicator;
3597 -- In general, the spec will be frozen when we start analyzing the
3598 -- body. However, for internally generated operations, such as
3599 -- wrapper functions for inherited operations with controlling
3600 -- results, the spec may not have been frozen by the time we expand
3601 -- the freeze actions that include the bodies. In particular, extra
3602 -- formals for accessibility or for return-in-place may need to be
3603 -- generated. Freeze nodes, if any, are inserted before the current
3604 -- body. These freeze actions are also needed in ASIS mode and in
3605 -- Compile_Only mode to enable the proper back-end type annotations.
3606 -- They are necessary in any case to insure order of elaboration
3609 if not Is_Frozen (Spec_Id)
3610 and then (Expander_Active
3612 or else (Operating_Mode = Check_Semantics
3613 and then Serious_Errors_Detected = 0))
3615 Set_Has_Delayed_Freeze (Spec_Id);
3616 Freeze_Before (N, Spec_Id);
3620 -- If the subprogram has a class-wide clone, build its body as a copy
3621 -- of the original body, and rewrite body of original subprogram as a
3622 -- wrapper that calls the clone.
3624 if Present (Spec_Id)
3625 and then Present (Class_Wide_Clone (Spec_Id))
3626 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
3628 Build_Class_Wide_Clone_Body (Spec_Id, N);
3630 -- This is the new body for the existing primitive operation
3632 Rewrite (N, Build_Class_Wide_Clone_Call
3633 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
3634 Set_Has_Completion (Spec_Id, False);
3639 -- Place subprogram on scope stack, and make formals visible. If there
3640 -- is a spec, the visible entity remains that of the spec.
3642 if Present (Spec_Id) then
3643 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3645 if Is_Child_Unit (Spec_Id) then
3646 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3650 Style.Check_Identifier (Body_Id, Spec_Id);
3653 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3654 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3656 if Is_Abstract_Subprogram (Spec_Id) then
3657 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3661 Set_Convention (Body_Id, Convention (Spec_Id));
3662 Set_Has_Completion (Spec_Id);
3664 if Is_Protected_Type (Scope (Spec_Id)) then
3665 Prot_Typ := Scope (Spec_Id);
3668 -- If this is a body generated for a renaming, do not check for
3669 -- full conformance. The check is redundant, because the spec of
3670 -- the body is a copy of the spec in the renaming declaration,
3671 -- and the test can lead to spurious errors on nested defaults.
3673 if Present (Spec_Decl)
3674 and then not Comes_From_Source (N)
3676 (Nkind (Original_Node (Spec_Decl)) =
3677 N_Subprogram_Renaming_Declaration
3678 or else (Present (Corresponding_Body (Spec_Decl))
3680 Nkind (Unit_Declaration_Node
3681 (Corresponding_Body (Spec_Decl))) =
3682 N_Subprogram_Renaming_Declaration))
3686 -- Conversely, the spec may have been generated for specless body
3687 -- with an inline pragma. The entity comes from source, which is
3688 -- both semantically correct and necessary for proper inlining.
3689 -- The subprogram declaration itself is not in the source.
3691 elsif Comes_From_Source (N)
3692 and then Present (Spec_Decl)
3693 and then not Comes_From_Source (Spec_Decl)
3694 and then Has_Pragma_Inline (Spec_Id)
3701 Fully_Conformant, True, Conformant, Body_Id);
3704 -- If the body is not fully conformant, we have to decide if we
3705 -- should analyze it or not. If it has a really messed up profile
3706 -- then we probably should not analyze it, since we will get too
3707 -- many bogus messages.
3709 -- Our decision is to go ahead in the non-fully conformant case
3710 -- only if it is at least mode conformant with the spec. Note
3711 -- that the call to Check_Fully_Conformant has issued the proper
3712 -- error messages to complain about the lack of conformance.
3715 and then not Mode_Conformant (Body_Id, Spec_Id)
3721 if Spec_Id /= Body_Id then
3722 Reference_Body_Formals (Spec_Id, Body_Id);
3725 Set_Ekind (Body_Id, E_Subprogram_Body);
3727 if Nkind (N) = N_Subprogram_Body_Stub then
3728 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3733 Set_Corresponding_Spec (N, Spec_Id);
3735 -- Ada 2005 (AI-345): If the operation is a primitive operation
3736 -- of a concurrent type, the type of the first parameter has been
3737 -- replaced with the corresponding record, which is the proper
3738 -- run-time structure to use. However, within the body there may
3739 -- be uses of the formals that depend on primitive operations
3740 -- of the type (in particular calls in prefixed form) for which
3741 -- we need the original concurrent type. The operation may have
3742 -- several controlling formals, so the replacement must be done
3745 if Comes_From_Source (Spec_Id)
3746 and then Present (First_Entity (Spec_Id))
3747 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3748 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3749 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3750 and then Present (Corresponding_Concurrent_Type
3751 (Etype (First_Entity (Spec_Id))))
3754 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3758 Form := First_Formal (Spec_Id);
3759 while Present (Form) loop
3760 if Etype (Form) = Typ then
3761 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3769 -- Make the formals visible, and place subprogram on scope stack.
3770 -- This is also the point at which we set Last_Real_Spec_Entity
3771 -- to mark the entities which will not be moved to the body.
3773 Install_Formals (Spec_Id);
3774 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3776 -- Within an instance, add local renaming declarations so that
3777 -- gdb can retrieve the values of actuals more easily. This is
3778 -- only relevant if generating code (and indeed we definitely
3779 -- do not want these definitions -gnatc mode, because that would
3782 if Is_Generic_Instance (Spec_Id)
3783 and then Is_Wrapper_Package (Current_Scope)
3784 and then Expander_Active
3786 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3789 Push_Scope (Spec_Id);
3791 -- Make sure that the subprogram is immediately visible. For
3792 -- child units that have no separate spec this is indispensable.
3793 -- Otherwise it is safe albeit redundant.
3795 Set_Is_Immediately_Visible (Spec_Id);
3798 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3799 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3800 Set_Scope (Body_Id, Scope (Spec_Id));
3802 -- Case of subprogram body with no previous spec
3805 -- Check for style warning required
3809 -- Only apply check for source level subprograms for which checks
3810 -- have not been suppressed.
3812 and then Comes_From_Source (Body_Id)
3813 and then not Suppress_Style_Checks (Body_Id)
3815 -- No warnings within an instance
3817 and then not In_Instance
3819 -- No warnings for expression functions
3821 and then Nkind (Original_Node (N)) /= N_Expression_Function
3823 Style.Body_With_No_Spec (N);
3826 New_Overloaded_Entity (Body_Id);
3828 if Nkind (N) /= N_Subprogram_Body_Stub then
3829 Set_Acts_As_Spec (N);
3830 Generate_Definition (Body_Id);
3832 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3834 -- If the body is an entry wrapper created for an entry with
3835 -- preconditions, it must be compiled in the context of the
3836 -- enclosing synchronized object, because it may mention other
3837 -- operations of the type.
3839 if Is_Entry_Wrapper (Body_Id) then
3841 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
3844 Install_Declarations (Prot);
3848 Install_Formals (Body_Id);
3850 Push_Scope (Body_Id);
3853 -- For stubs and bodies with no previous spec, generate references to
3856 Generate_Reference_To_Formals (Body_Id);
3859 -- Entry barrier functions are generated outside the protected type and
3860 -- should not carry the SPARK_Mode of the enclosing context.
3862 if Nkind (N) = N_Subprogram_Body
3863 and then Is_Entry_Barrier_Function (N)
3867 -- The body is generated as part of expression function expansion. When
3868 -- the expression function appears in the visible declarations of a
3869 -- package, the body is added to the private declarations. Since both
3870 -- declarative lists may be subject to a different SPARK_Mode, inherit
3871 -- the mode of the spec.
3873 -- package P with SPARK_Mode is
3874 -- function Expr_Func ... is (...); -- original
3875 -- [function Expr_Func ...;] -- generated spec
3878 -- pragma SPARK_Mode (Off);
3879 -- [function Expr_Func ... is return ...;] -- generated body
3880 -- end P; -- mode is ON
3882 elsif not Comes_From_Source (N)
3883 and then Present (Spec_Id)
3884 and then Is_Expression_Function (Spec_Id)
3886 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
3887 Set_SPARK_Pragma_Inherited
3888 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
3890 -- Set the SPARK_Mode from the current context (may be overwritten later
3891 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
3892 -- initially on a stand-alone subprogram body, but is then relocated to
3893 -- a generated corresponding spec. In this scenario the mode is shared
3894 -- between the spec and body.
3896 elsif No (SPARK_Pragma (Body_Id)) then
3897 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3898 Set_SPARK_Pragma_Inherited (Body_Id);
3901 -- A subprogram body may be instantiated or inlined at a later pass.
3902 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
3903 -- applied to the initial declaration of the body.
3905 if Present (Spec_Id) then
3906 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
3907 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3911 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
3912 -- case the body is instantiated or inlined later and out of context.
3913 -- The body uses this attribute to restore the value of the global
3916 if Ignore_SPARK_Mode_Pragmas_In_Instance then
3917 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
3919 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
3920 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3924 -- If this is the proper body of a stub, we must verify that the stub
3925 -- conforms to the body, and to the previous spec if one was present.
3926 -- We know already that the body conforms to that spec. This test is
3927 -- only required for subprograms that come from source.
3929 if Nkind (Parent (N)) = N_Subunit
3930 and then Comes_From_Source (N)
3931 and then not Error_Posted (Body_Id)
3932 and then Nkind (Corresponding_Stub (Parent (N))) =
3933 N_Subprogram_Body_Stub
3936 Old_Id : constant Entity_Id :=
3938 (Specification (Corresponding_Stub (Parent (N))));
3940 Conformant : Boolean := False;
3943 if No (Spec_Id) then
3944 Check_Fully_Conformant (Body_Id, Old_Id);
3948 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3950 if not Conformant then
3952 -- The stub was taken to be a new declaration. Indicate that
3955 Set_Has_Completion (Old_Id, False);
3961 Set_Has_Completion (Body_Id);
3962 Check_Eliminated (Body_Id);
3964 -- Analyze any aspect specifications that appear on the subprogram body
3965 -- stub. Stop the analysis now as the stub does not have a declarative
3966 -- or a statement part, and it cannot be inlined.
3968 if Nkind (N) = N_Subprogram_Body_Stub then
3969 if Has_Aspects (N) then
3970 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
3978 -- Note: Normally we don't do any inlining if expansion is off, since
3979 -- we won't generate code in any case. An exception arises in GNATprove
3980 -- mode where we want to expand some calls in place, even with expansion
3981 -- disabled, since the inlining eases formal verification.
3983 if not GNATprove_Mode
3984 and then Expander_Active
3985 and then Serious_Errors_Detected = 0
3986 and then Present (Spec_Id)
3987 and then Has_Pragma_Inline (Spec_Id)
3989 -- Legacy implementation (relying on front-end inlining)
3991 if not Back_End_Inlining then
3992 if (Has_Pragma_Inline_Always (Spec_Id)
3993 and then not Opt.Disable_FE_Inline_Always)
3994 or else (Front_End_Inlining
3995 and then not Opt.Disable_FE_Inline)
3997 Build_Body_To_Inline (N, Spec_Id);
4000 -- New implementation (relying on back-end inlining)
4003 if Has_Pragma_Inline_Always (Spec_Id)
4004 or else Optimization_Level > 0
4006 -- Handle function returning an unconstrained type
4008 if Comes_From_Source (Body_Id)
4009 and then Ekind (Spec_Id) = E_Function
4010 and then Returns_Unconstrained_Type (Spec_Id)
4012 -- If function builds in place, i.e. returns a limited type,
4013 -- inlining cannot be done.
4015 and then not Is_Limited_Type (Etype (Spec_Id))
4017 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4021 Subp_Body : constant Node_Id :=
4022 Unit_Declaration_Node (Body_Id);
4023 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4026 -- Do not pass inlining to the backend if the subprogram
4027 -- has declarations or statements which cannot be inlined
4028 -- by the backend. This check is done here to emit an
4029 -- error instead of the generic warning message reported
4030 -- by the GCC backend (ie. "function might not be
4033 if Present (Subp_Decl)
4034 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4038 elsif Has_Excluded_Statement
4041 (Handled_Statement_Sequence (Subp_Body)))
4045 -- If the backend inlining is available then at this
4046 -- stage we only have to mark the subprogram as inlined.
4047 -- The expander will take care of registering it in the
4048 -- table of subprograms inlined by the backend a part of
4049 -- processing calls to it (cf. Expand_Call)
4052 Set_Is_Inlined (Spec_Id);
4059 -- In GNATprove mode, inline only when there is a separate subprogram
4060 -- declaration for now, as inlining of subprogram bodies acting as
4061 -- declarations, or subprogram stubs, are not supported by front-end
4062 -- inlining. This inlining should occur after analysis of the body, so
4063 -- that it is known whether the value of SPARK_Mode, which can be
4064 -- defined by a pragma inside the body, is applicable to the body.
4065 -- Inlining can be disabled with switch -gnatdm
4067 elsif GNATprove_Mode
4068 and then Full_Analysis
4069 and then not Inside_A_Generic
4070 and then Present (Spec_Id)
4072 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4073 and then Body_Has_SPARK_Mode_On
4074 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4075 and then not Body_Has_Contract
4076 and then not Debug_Flag_M
4078 Build_Body_To_Inline (N, Spec_Id);
4081 -- When generating code, inherited pre/postconditions are handled when
4082 -- expanding the corresponding contract.
4084 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4085 -- of the specification we have to install the private withed units.
4086 -- This holds for child units as well.
4088 if Is_Compilation_Unit (Body_Id)
4089 or else Nkind (Parent (N)) = N_Compilation_Unit
4091 Install_Private_With_Clauses (Body_Id);
4094 Check_Anonymous_Return;
4096 -- Set the Protected_Formal field of each extra formal of the protected
4097 -- subprogram to reference the corresponding extra formal of the
4098 -- subprogram that implements it. For regular formals this occurs when
4099 -- the protected subprogram's declaration is expanded, but the extra
4100 -- formals don't get created until the subprogram is frozen. We need to
4101 -- do this before analyzing the protected subprogram's body so that any
4102 -- references to the original subprogram's extra formals will be changed
4103 -- refer to the implementing subprogram's formals (see Expand_Formal).
4105 if Present (Spec_Id)
4106 and then Is_Protected_Type (Scope (Spec_Id))
4107 and then Present (Protected_Body_Subprogram (Spec_Id))
4110 Impl_Subp : constant Entity_Id :=
4111 Protected_Body_Subprogram (Spec_Id);
4112 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4113 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4116 while Present (Prot_Ext_Formal) loop
4117 pragma Assert (Present (Impl_Ext_Formal));
4118 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4119 Next_Formal_With_Extras (Prot_Ext_Formal);
4120 Next_Formal_With_Extras (Impl_Ext_Formal);
4125 -- Now we can go on to analyze the body
4127 HSS := Handled_Statement_Sequence (N);
4128 Set_Actual_Subtypes (N, Current_Scope);
4130 -- Add a declaration for the Protection object, renaming declarations
4131 -- for discriminals and privals and finally a declaration for the entry
4132 -- family index (if applicable). This form of early expansion is done
4133 -- when the Expander is active because Install_Private_Data_Declarations
4134 -- references entities which were created during regular expansion. The
4135 -- subprogram entity must come from source, and not be an internally
4136 -- generated subprogram.
4139 and then Present (Prot_Typ)
4140 and then Present (Spec_Id)
4141 and then Comes_From_Source (Spec_Id)
4142 and then not Is_Eliminated (Spec_Id)
4144 Install_Private_Data_Declarations
4145 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4148 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4149 -- may now appear in parameter and result profiles. Since the analysis
4150 -- of a subprogram body may use the parameter and result profile of the
4151 -- spec, swap any limited views with their non-limited counterpart.
4153 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4154 Exch_Views := Exchange_Limited_Views (Spec_Id);
4157 -- If the return type is an anonymous access type whose designated type
4158 -- is the limited view of a class-wide type and the non-limited view is
4159 -- available, update the return type accordingly.
4161 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4167 Rtyp := Etype (Spec_Id);
4169 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4170 Etyp := Directly_Designated_Type (Rtyp);
4172 if Is_Class_Wide_Type (Etyp)
4173 and then From_Limited_With (Etyp)
4176 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4182 -- Analyze any aspect specifications that appear on the subprogram body
4184 if Has_Aspects (N) then
4185 Analyze_Aspect_Specifications_On_Body_Or_Stub (N);
4188 Analyze_Declarations (Declarations (N));
4190 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4192 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4193 if Present (SPARK_Pragma (Spec_Id)) then
4194 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4196 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4198 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4199 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4200 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4202 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4205 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4209 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4210 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4211 Error_Msg_Sloc := Sloc (Spec_Id);
4213 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4217 -- A subprogram body "freezes" its own contract. Analyze the contract
4218 -- after the declarations of the body have been processed as pragmas
4219 -- are now chained on the contract of the subprogram body.
4221 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4223 -- Check completion, and analyze the statements
4226 Inspect_Deferred_Constant_Completion (Declarations (N));
4229 -- Deal with end of scope processing for the body
4231 Process_End_Label (HSS, 't', Current_Scope);
4234 -- If we are compiling an entry wrapper, remove the enclosing
4235 -- synchronized object from the stack.
4237 if Is_Entry_Wrapper (Body_Id) then
4241 Check_Subprogram_Order (N);
4242 Set_Analyzed (Body_Id);
4244 -- If we have a separate spec, then the analysis of the declarations
4245 -- caused the entities in the body to be chained to the spec id, but
4246 -- we want them chained to the body id. Only the formal parameters
4247 -- end up chained to the spec id in this case.
4249 if Present (Spec_Id) then
4251 -- We must conform to the categorization of our spec
4253 Validate_Categorization_Dependency (N, Spec_Id);
4255 -- And if this is a child unit, the parent units must conform
4257 if Is_Child_Unit (Spec_Id) then
4258 Validate_Categorization_Dependency
4259 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4262 -- Here is where we move entities from the spec to the body
4264 -- Case where there are entities that stay with the spec
4266 if Present (Last_Real_Spec_Entity) then
4268 -- No body entities (happens when the only real spec entities come
4269 -- from precondition and postcondition pragmas).
4271 if No (Last_Entity (Body_Id)) then
4272 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4274 -- Body entities present (formals), so chain stuff past them
4278 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4281 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4282 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4283 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4285 -- Case where there are no spec entities, in this case there can be
4286 -- no body entities either, so just move everything.
4288 -- If the body is generated for an expression function, it may have
4289 -- been preanalyzed already, if 'access was applied to it.
4292 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4293 N_Expression_Function
4295 pragma Assert (No (Last_Entity (Body_Id)));
4299 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4300 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4301 Set_First_Entity (Spec_Id, Empty);
4302 Set_Last_Entity (Spec_Id, Empty);
4306 Check_Missing_Return;
4308 -- Now we are going to check for variables that are never modified in
4309 -- the body of the procedure. But first we deal with a special case
4310 -- where we want to modify this check. If the body of the subprogram
4311 -- starts with a raise statement or its equivalent, or if the body
4312 -- consists entirely of a null statement, then it is pretty obvious that
4313 -- it is OK to not reference the parameters. For example, this might be
4314 -- the following common idiom for a stubbed function: statement of the
4315 -- procedure raises an exception. In particular this deals with the
4316 -- common idiom of a stubbed function, which appears something like:
4318 -- function F (A : Integer) return Some_Type;
4321 -- raise Program_Error;
4325 -- Here the purpose of X is simply to satisfy the annoying requirement
4326 -- in Ada that there be at least one return, and we certainly do not
4327 -- want to go posting warnings on X that it is not initialized. On
4328 -- the other hand, if X is entirely unreferenced that should still
4331 -- What we do is to detect these cases, and if we find them, flag the
4332 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4333 -- suppress unwanted warnings. For the case of the function stub above
4334 -- we have a special test to set X as apparently assigned to suppress
4341 -- Skip initial labels (for one thing this occurs when we are in
4342 -- front-end ZCX mode, but in any case it is irrelevant), and also
4343 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
4345 Stm := First (Statements (HSS));
4346 while Nkind (Stm) = N_Label
4347 or else Nkind (Stm) in N_Push_xxx_Label
4352 -- Do the test on the original statement before expansion
4355 Ostm : constant Node_Id := Original_Node (Stm);
4358 -- If explicit raise statement, turn on flag
4360 if Nkind (Ostm) = N_Raise_Statement then
4361 Set_Trivial_Subprogram (Stm);
4363 -- If null statement, and no following statements, turn on flag
4365 elsif Nkind (Stm) = N_Null_Statement
4366 and then Comes_From_Source (Stm)
4367 and then No (Next (Stm))
4369 Set_Trivial_Subprogram (Stm);
4371 -- Check for explicit call cases which likely raise an exception
4373 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4374 if Is_Entity_Name (Name (Ostm)) then
4376 Ent : constant Entity_Id := Entity (Name (Ostm));
4379 -- If the procedure is marked No_Return, then likely it
4380 -- raises an exception, but in any case it is not coming
4381 -- back here, so turn on the flag.
4384 and then Ekind (Ent) = E_Procedure
4385 and then No_Return (Ent)
4387 Set_Trivial_Subprogram (Stm);
4395 -- Check for variables that are never modified
4402 -- If there is a separate spec, then transfer Never_Set_In_Source
4403 -- flags from out parameters to the corresponding entities in the
4404 -- body. The reason we do that is we want to post error flags on
4405 -- the body entities, not the spec entities.
4407 if Present (Spec_Id) then
4408 E1 := First_Entity (Spec_Id);
4409 while Present (E1) loop
4410 if Ekind (E1) = E_Out_Parameter then
4411 E2 := First_Entity (Body_Id);
4412 while Present (E2) loop
4413 exit when Chars (E1) = Chars (E2);
4417 if Present (E2) then
4418 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4426 -- Check references in body
4428 Check_References (Body_Id);
4431 -- Check for nested subprogram, and mark outer level subprogram if so
4437 if Present (Spec_Id) then
4444 Ent := Enclosing_Subprogram (Ent);
4445 exit when No (Ent) or else Is_Subprogram (Ent);
4448 if Present (Ent) then
4449 Set_Has_Nested_Subprogram (Ent);
4453 -- Restore the limited views in the spec, if any, to let the back end
4454 -- process it without running into circularities.
4456 if Exch_Views /= No_Elist then
4457 Restore_Limited_Views (Exch_Views);
4460 if Present (Desig_View) then
4461 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4465 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4466 Restore_Ghost_Mode (Saved_GM);
4467 end Analyze_Subprogram_Body_Helper;
4469 ------------------------------------
4470 -- Analyze_Subprogram_Declaration --
4471 ------------------------------------
4473 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4474 Scop : constant Entity_Id := Current_Scope;
4475 Designator : Entity_Id;
4477 Is_Completion : Boolean;
4478 -- Indicates whether a null procedure declaration is a completion
4481 -- Null procedures are not allowed in SPARK
4483 if Nkind (Specification (N)) = N_Procedure_Specification
4484 and then Null_Present (Specification (N))
4486 Check_SPARK_05_Restriction ("null procedure is not allowed", N);
4488 -- Null procedures are allowed in protected types, following the
4489 -- recent AI12-0147.
4491 if Is_Protected_Type (Current_Scope)
4492 and then Ada_Version < Ada_2012
4494 Error_Msg_N ("protected operation cannot be a null procedure", N);
4497 Analyze_Null_Procedure (N, Is_Completion);
4499 -- The null procedure acts as a body, nothing further is needed
4501 if Is_Completion then
4506 Designator := Analyze_Subprogram_Specification (Specification (N));
4508 -- A reference may already have been generated for the unit name, in
4509 -- which case the following call is redundant. However it is needed for
4510 -- declarations that are the rewriting of an expression function.
4512 Generate_Definition (Designator);
4514 -- Set the SPARK mode from the current context (may be overwritten later
4515 -- with explicit pragma). This is not done for entry barrier functions
4516 -- because they are generated outside the protected type and should not
4517 -- carry the mode of the enclosing context.
4519 if Nkind (N) = N_Subprogram_Declaration
4520 and then Is_Entry_Barrier_Function (N)
4524 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
4525 Set_SPARK_Pragma_Inherited (Designator);
4528 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
4529 -- the body of this subprogram is instantiated or inlined later and out
4530 -- of context. The body uses this attribute to restore the value of the
4533 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4534 Set_Ignore_SPARK_Mode_Pragmas (Designator);
4537 if Debug_Flag_C then
4538 Write_Str ("==> subprogram spec ");
4539 Write_Name (Chars (Designator));
4540 Write_Str (" from ");
4541 Write_Location (Sloc (N));
4546 Validate_RCI_Subprogram_Declaration (N);
4547 New_Overloaded_Entity (Designator);
4548 Check_Delayed_Subprogram (Designator);
4550 -- If the type of the first formal of the current subprogram is a non-
4551 -- generic tagged private type, mark the subprogram as being a private
4552 -- primitive. Ditto if this is a function with controlling result, and
4553 -- the return type is currently private. In both cases, the type of the
4554 -- controlling argument or result must be in the current scope for the
4555 -- operation to be primitive.
4557 if Has_Controlling_Result (Designator)
4558 and then Is_Private_Type (Etype (Designator))
4559 and then Scope (Etype (Designator)) = Current_Scope
4560 and then not Is_Generic_Actual_Type (Etype (Designator))
4562 Set_Is_Private_Primitive (Designator);
4564 elsif Present (First_Formal (Designator)) then
4566 Formal_Typ : constant Entity_Id :=
4567 Etype (First_Formal (Designator));
4569 Set_Is_Private_Primitive (Designator,
4570 Is_Tagged_Type (Formal_Typ)
4571 and then Scope (Formal_Typ) = Current_Scope
4572 and then Is_Private_Type (Formal_Typ)
4573 and then not Is_Generic_Actual_Type (Formal_Typ));
4577 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
4580 if Ada_Version >= Ada_2005
4581 and then Comes_From_Source (N)
4582 and then Is_Dispatching_Operation (Designator)
4589 if Has_Controlling_Result (Designator) then
4590 Etyp := Etype (Designator);
4593 E := First_Entity (Designator);
4595 and then Is_Formal (E)
4596 and then not Is_Controlling_Formal (E)
4604 if Is_Access_Type (Etyp) then
4605 Etyp := Directly_Designated_Type (Etyp);
4608 if Is_Interface (Etyp)
4609 and then not Is_Abstract_Subprogram (Designator)
4610 and then not (Ekind (Designator) = E_Procedure
4611 and then Null_Present (Specification (N)))
4613 Error_Msg_Name_1 := Chars (Defining_Entity (N));
4615 -- Specialize error message based on procedures vs. functions,
4616 -- since functions can't be null subprograms.
4618 if Ekind (Designator) = E_Procedure then
4620 ("interface procedure % must be abstract or null", N);
4623 ("interface function % must be abstract", N);
4629 -- What is the following code for, it used to be
4631 -- ??? Set_Suppress_Elaboration_Checks
4632 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
4634 -- The following seems equivalent, but a bit dubious
4636 if Elaboration_Checks_Suppressed (Designator) then
4637 Set_Kill_Elaboration_Checks (Designator);
4640 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
4641 Set_Categorization_From_Scope (Designator, Scop);
4644 -- For a compilation unit, check for library-unit pragmas
4646 Push_Scope (Designator);
4647 Set_Categorization_From_Pragmas (N);
4648 Validate_Categorization_Dependency (N, Designator);
4652 -- For a compilation unit, set body required. This flag will only be
4653 -- reset if a valid Import or Interface pragma is processed later on.
4655 if Nkind (Parent (N)) = N_Compilation_Unit then
4656 Set_Body_Required (Parent (N), True);
4658 if Ada_Version >= Ada_2005
4659 and then Nkind (Specification (N)) = N_Procedure_Specification
4660 and then Null_Present (Specification (N))
4663 ("null procedure cannot be declared at library level", N);
4667 Generate_Reference_To_Formals (Designator);
4668 Check_Eliminated (Designator);
4670 if Debug_Flag_C then
4672 Write_Str ("<== subprogram spec ");
4673 Write_Name (Chars (Designator));
4674 Write_Str (" from ");
4675 Write_Location (Sloc (N));
4679 if Is_Protected_Type (Current_Scope) then
4681 -- Indicate that this is a protected operation, because it may be
4682 -- used in subsequent declarations within the protected type.
4684 Set_Convention (Designator, Convention_Protected);
4687 List_Inherited_Pre_Post_Aspects (Designator);
4689 if Has_Aspects (N) then
4690 Analyze_Aspect_Specifications (N, Designator);
4692 end Analyze_Subprogram_Declaration;
4694 --------------------------------------
4695 -- Analyze_Subprogram_Specification --
4696 --------------------------------------
4698 -- Reminder: N here really is a subprogram specification (not a subprogram
4699 -- declaration). This procedure is called to analyze the specification in
4700 -- both subprogram bodies and subprogram declarations (specs).
4702 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4703 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
4704 -- Determine whether entity E denotes the spec or body of an invariant
4707 ------------------------------------
4708 -- Is_Invariant_Procedure_Or_Body --
4709 ------------------------------------
4711 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
4712 Decl : constant Node_Id := Unit_Declaration_Node (E);
4716 if Nkind (Decl) = N_Subprogram_Body then
4717 Spec := Corresponding_Spec (Decl);
4724 and then Ekind (Spec) = E_Procedure
4725 and then (Is_Partial_Invariant_Procedure (Spec)
4726 or else Is_Invariant_Procedure (Spec));
4727 end Is_Invariant_Procedure_Or_Body;
4731 Designator : constant Entity_Id := Defining_Entity (N);
4732 Formals : constant List_Id := Parameter_Specifications (N);
4734 -- Start of processing for Analyze_Subprogram_Specification
4737 -- User-defined operator is not allowed in SPARK, except as a renaming
4739 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4740 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4742 Check_SPARK_05_Restriction
4743 ("user-defined operator is not allowed", N);
4746 -- Proceed with analysis. Do not emit a cross-reference entry if the
4747 -- specification comes from an expression function, because it may be
4748 -- the completion of a previous declaration. It is not, the cross-
4749 -- reference entry will be emitted for the new subprogram declaration.
4751 if Nkind (Parent (N)) /= N_Expression_Function then
4752 Generate_Definition (Designator);
4755 if Nkind (N) = N_Function_Specification then
4756 Set_Ekind (Designator, E_Function);
4757 Set_Mechanism (Designator, Default_Mechanism);
4759 Set_Ekind (Designator, E_Procedure);
4760 Set_Etype (Designator, Standard_Void_Type);
4763 -- Flag Is_Inlined_Always is True by default, and reversed to False for
4764 -- those subprograms which could be inlined in GNATprove mode (because
4765 -- Body_To_Inline is non-Empty) but should not be inlined.
4767 if GNATprove_Mode then
4768 Set_Is_Inlined_Always (Designator);
4771 -- Introduce new scope for analysis of the formals and the return type
4773 Set_Scope (Designator, Current_Scope);
4775 if Present (Formals) then
4776 Push_Scope (Designator);
4777 Process_Formals (Formals, N);
4779 -- Check dimensions in N for formals with default expression
4781 Analyze_Dimension_Formals (N, Formals);
4783 -- Ada 2005 (AI-345): If this is an overriding operation of an
4784 -- inherited interface operation, and the controlling type is
4785 -- a synchronized type, replace the type with its corresponding
4786 -- record, to match the proper signature of an overriding operation.
4787 -- Same processing for an access parameter whose designated type is
4788 -- derived from a synchronized interface.
4790 -- This modification is not done for invariant procedures because
4791 -- the corresponding record may not necessarely be visible when the
4792 -- concurrent type acts as the full view of a private type.
4795 -- type Prot is private with Type_Invariant => ...;
4796 -- procedure ConcInvariant (Obj : Prot);
4798 -- protected type Prot is ...;
4799 -- type Concurrent_Record_Prot is record ...;
4800 -- procedure ConcInvariant (Obj : Prot) is
4802 -- end ConcInvariant;
4805 -- In the example above, both the spec and body of the invariant
4806 -- procedure must utilize the private type as the controlling type.
4808 if Ada_Version >= Ada_2005
4809 and then not Is_Invariant_Procedure_Or_Body (Designator)
4813 Formal_Typ : Entity_Id;
4814 Rec_Typ : Entity_Id;
4815 Desig_Typ : Entity_Id;
4818 Formal := First_Formal (Designator);
4819 while Present (Formal) loop
4820 Formal_Typ := Etype (Formal);
4822 if Is_Concurrent_Type (Formal_Typ)
4823 and then Present (Corresponding_Record_Type (Formal_Typ))
4825 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4827 if Present (Interfaces (Rec_Typ)) then
4828 Set_Etype (Formal, Rec_Typ);
4831 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4832 Desig_Typ := Designated_Type (Formal_Typ);
4834 if Is_Concurrent_Type (Desig_Typ)
4835 and then Present (Corresponding_Record_Type (Desig_Typ))
4837 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4839 if Present (Interfaces (Rec_Typ)) then
4840 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4845 Next_Formal (Formal);
4852 -- The subprogram scope is pushed and popped around the processing of
4853 -- the return type for consistency with call above to Process_Formals
4854 -- (which itself can call Analyze_Return_Type), and to ensure that any
4855 -- itype created for the return type will be associated with the proper
4858 elsif Nkind (N) = N_Function_Specification then
4859 Push_Scope (Designator);
4860 Analyze_Return_Type (N);
4866 if Nkind (N) = N_Function_Specification then
4868 -- Deal with operator symbol case
4870 if Nkind (Designator) = N_Defining_Operator_Symbol then
4871 Valid_Operator_Definition (Designator);
4874 May_Need_Actuals (Designator);
4876 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4877 -- the subprogram is abstract also. This does not apply to renaming
4878 -- declarations, where abstractness is inherited, and to subprogram
4879 -- bodies generated for stream operations, which become renamings as
4882 -- In case of primitives associated with abstract interface types
4883 -- the check is applied later (see Analyze_Subprogram_Declaration).
4885 if not Nkind_In (Original_Node (Parent (N)),
4886 N_Abstract_Subprogram_Declaration,
4887 N_Formal_Abstract_Subprogram_Declaration,
4888 N_Subprogram_Renaming_Declaration)
4890 if Is_Abstract_Type (Etype (Designator))
4891 and then not Is_Interface (Etype (Designator))
4894 ("function that returns abstract type must be abstract", N);
4896 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4897 -- access result whose designated type is abstract.
4899 elsif Ada_Version >= Ada_2012
4900 and then Nkind (Result_Definition (N)) = N_Access_Definition
4902 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4903 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4906 ("function whose access result designates abstract type "
4907 & "must be abstract", N);
4913 end Analyze_Subprogram_Specification;
4915 -----------------------
4916 -- Check_Conformance --
4917 -----------------------
4919 procedure Check_Conformance
4920 (New_Id : Entity_Id;
4922 Ctype : Conformance_Type;
4924 Conforms : out Boolean;
4925 Err_Loc : Node_Id := Empty;
4926 Get_Inst : Boolean := False;
4927 Skip_Controlling_Formals : Boolean := False)
4929 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4930 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4931 -- If Errmsg is True, then processing continues to post an error message
4932 -- for conformance error on given node. Two messages are output. The
4933 -- first message points to the previous declaration with a general "no
4934 -- conformance" message. The second is the detailed reason, supplied as
4935 -- Msg. The parameter N provide information for a possible & insertion
4936 -- in the message, and also provides the location for posting the
4937 -- message in the absence of a specified Err_Loc location.
4939 function Conventions_Match
4941 Id2 : Entity_Id) return Boolean;
4942 -- Determine whether the conventions of arbitrary entities Id1 and Id2
4945 -----------------------
4946 -- Conformance_Error --
4947 -----------------------
4949 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4956 if No (Err_Loc) then
4962 Error_Msg_Sloc := Sloc (Old_Id);
4965 when Type_Conformant =>
4966 Error_Msg_N -- CODEFIX
4967 ("not type conformant with declaration#!", Enode);
4969 when Mode_Conformant =>
4970 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4972 ("not mode conformant with operation inherited#!",
4976 ("not mode conformant with declaration#!", Enode);
4979 when Subtype_Conformant =>
4980 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4982 ("not subtype conformant with operation inherited#!",
4986 ("not subtype conformant with declaration#!", Enode);
4989 when Fully_Conformant =>
4990 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4991 Error_Msg_N -- CODEFIX
4992 ("not fully conformant with operation inherited#!",
4995 Error_Msg_N -- CODEFIX
4996 ("not fully conformant with declaration#!", Enode);
5000 Error_Msg_NE (Msg, Enode, N);
5002 end Conformance_Error;
5004 -----------------------
5005 -- Conventions_Match --
5006 -----------------------
5008 function Conventions_Match
5010 Id2 : Entity_Id) return Boolean
5013 -- Ignore the conventions of anonymous access-to-subprogram types
5014 -- and subprogram types because these are internally generated and
5015 -- the only way these may receive a convention is if they inherit
5016 -- the convention of a related subprogram.
5018 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5021 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5026 -- Otherwise compare the conventions directly
5029 return Convention (Id1) = Convention (Id2);
5031 end Conventions_Match;
5035 Old_Type : constant Entity_Id := Etype (Old_Id);
5036 New_Type : constant Entity_Id := Etype (New_Id);
5037 Old_Formal : Entity_Id;
5038 New_Formal : Entity_Id;
5039 Access_Types_Match : Boolean;
5040 Old_Formal_Base : Entity_Id;
5041 New_Formal_Base : Entity_Id;
5043 -- Start of processing for Check_Conformance
5048 -- We need a special case for operators, since they don't appear
5051 if Ctype = Type_Conformant then
5052 if Ekind (New_Id) = E_Operator
5053 and then Operator_Matches_Spec (New_Id, Old_Id)
5059 -- If both are functions/operators, check return types conform
5061 if Old_Type /= Standard_Void_Type
5063 New_Type /= Standard_Void_Type
5065 -- If we are checking interface conformance we omit controlling
5066 -- arguments and result, because we are only checking the conformance
5067 -- of the remaining parameters.
5069 if Has_Controlling_Result (Old_Id)
5070 and then Has_Controlling_Result (New_Id)
5071 and then Skip_Controlling_Formals
5075 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5076 if Ctype >= Subtype_Conformant
5077 and then not Predicates_Match (Old_Type, New_Type)
5080 ("\predicate of return type does not match!", New_Id);
5083 ("\return type does not match!", New_Id);
5089 -- Ada 2005 (AI-231): In case of anonymous access types check the
5090 -- null-exclusion and access-to-constant attributes match.
5092 if Ada_Version >= Ada_2005
5093 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5095 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5096 or else Is_Access_Constant (Etype (Old_Type)) /=
5097 Is_Access_Constant (Etype (New_Type)))
5099 Conformance_Error ("\return type does not match!", New_Id);
5103 -- If either is a function/operator and the other isn't, error
5105 elsif Old_Type /= Standard_Void_Type
5106 or else New_Type /= Standard_Void_Type
5108 Conformance_Error ("\functions can only match functions!", New_Id);
5112 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5113 -- If this is a renaming as body, refine error message to indicate that
5114 -- the conflict is with the original declaration. If the entity is not
5115 -- frozen, the conventions don't have to match, the one of the renamed
5116 -- entity is inherited.
5118 if Ctype >= Subtype_Conformant then
5119 if not Conventions_Match (Old_Id, New_Id) then
5120 if not Is_Frozen (New_Id) then
5123 elsif Present (Err_Loc)
5124 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5125 and then Present (Corresponding_Spec (Err_Loc))
5127 Error_Msg_Name_1 := Chars (New_Id);
5129 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5130 Conformance_Error ("\prior declaration for% has convention %!");
5133 Conformance_Error ("\calling conventions do not match!");
5138 elsif Is_Formal_Subprogram (Old_Id)
5139 or else Is_Formal_Subprogram (New_Id)
5141 Conformance_Error ("\formal subprograms not allowed!");
5146 -- Deal with parameters
5148 -- Note: we use the entity information, rather than going directly
5149 -- to the specification in the tree. This is not only simpler, but
5150 -- absolutely necessary for some cases of conformance tests between
5151 -- operators, where the declaration tree simply does not exist.
5153 Old_Formal := First_Formal (Old_Id);
5154 New_Formal := First_Formal (New_Id);
5155 while Present (Old_Formal) and then Present (New_Formal) loop
5156 if Is_Controlling_Formal (Old_Formal)
5157 and then Is_Controlling_Formal (New_Formal)
5158 and then Skip_Controlling_Formals
5160 -- The controlling formals will have different types when
5161 -- comparing an interface operation with its match, but both
5162 -- or neither must be access parameters.
5164 if Is_Access_Type (Etype (Old_Formal))
5166 Is_Access_Type (Etype (New_Formal))
5168 goto Skip_Controlling_Formal;
5171 ("\access parameter does not match!", New_Formal);
5175 -- Ada 2012: Mode conformance also requires that formal parameters
5176 -- be both aliased, or neither.
5178 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5179 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5181 ("\aliased parameter mismatch!", New_Formal);
5185 if Ctype = Fully_Conformant then
5187 -- Names must match. Error message is more accurate if we do
5188 -- this before checking that the types of the formals match.
5190 if Chars (Old_Formal) /= Chars (New_Formal) then
5191 Conformance_Error ("\name& does not match!", New_Formal);
5193 -- Set error posted flag on new formal as well to stop
5194 -- junk cascaded messages in some cases.
5196 Set_Error_Posted (New_Formal);
5200 -- Null exclusion must match
5202 if Null_Exclusion_Present (Parent (Old_Formal))
5204 Null_Exclusion_Present (Parent (New_Formal))
5206 -- Only give error if both come from source. This should be
5207 -- investigated some time, since it should not be needed ???
5209 if Comes_From_Source (Old_Formal)
5211 Comes_From_Source (New_Formal)
5214 ("\null exclusion for& does not match", New_Formal);
5216 -- Mark error posted on the new formal to avoid duplicated
5217 -- complaint about types not matching.
5219 Set_Error_Posted (New_Formal);
5224 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5225 -- case occurs whenever a subprogram is being renamed and one of its
5226 -- parameters imposes a null exclusion. For example:
5228 -- type T is null record;
5229 -- type Acc_T is access T;
5230 -- subtype Acc_T_Sub is Acc_T;
5232 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5233 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5236 Old_Formal_Base := Etype (Old_Formal);
5237 New_Formal_Base := Etype (New_Formal);
5240 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5241 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5244 Access_Types_Match := Ada_Version >= Ada_2005
5246 -- Ensure that this rule is only applied when New_Id is a
5247 -- renaming of Old_Id.
5249 and then Nkind (Parent (Parent (New_Id))) =
5250 N_Subprogram_Renaming_Declaration
5251 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5252 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5253 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5255 -- Now handle the allowed access-type case
5257 and then Is_Access_Type (Old_Formal_Base)
5258 and then Is_Access_Type (New_Formal_Base)
5260 -- The type kinds must match. The only exception occurs with
5261 -- multiple generics of the form:
5264 -- type F is private; type A is private;
5265 -- type F_Ptr is access F; type A_Ptr is access A;
5266 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5267 -- package F_Pack is ... package A_Pack is
5268 -- package F_Inst is
5269 -- new F_Pack (A, A_Ptr, A_P);
5271 -- When checking for conformance between the parameters of A_P
5272 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5273 -- because the compiler has transformed A_Ptr into a subtype of
5274 -- F_Ptr. We catch this case in the code below.
5276 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5278 (Is_Generic_Type (Old_Formal_Base)
5279 and then Is_Generic_Type (New_Formal_Base)
5280 and then Is_Internal (New_Formal_Base)
5281 and then Etype (Etype (New_Formal_Base)) =
5283 and then Directly_Designated_Type (Old_Formal_Base) =
5284 Directly_Designated_Type (New_Formal_Base)
5285 and then ((Is_Itype (Old_Formal_Base)
5286 and then Can_Never_Be_Null (Old_Formal_Base))
5288 (Is_Itype (New_Formal_Base)
5289 and then Can_Never_Be_Null (New_Formal_Base)));
5291 -- Types must always match. In the visible part of an instance,
5292 -- usual overloading rules for dispatching operations apply, and
5293 -- we check base types (not the actual subtypes).
5295 if In_Instance_Visible_Part
5296 and then Is_Dispatching_Operation (New_Id)
5298 if not Conforming_Types
5299 (T1 => Base_Type (Etype (Old_Formal)),
5300 T2 => Base_Type (Etype (New_Formal)),
5302 Get_Inst => Get_Inst)
5303 and then not Access_Types_Match
5305 Conformance_Error ("\type of & does not match!", New_Formal);
5309 elsif not Conforming_Types
5310 (T1 => Old_Formal_Base,
5311 T2 => New_Formal_Base,
5313 Get_Inst => Get_Inst)
5314 and then not Access_Types_Match
5316 -- Don't give error message if old type is Any_Type. This test
5317 -- avoids some cascaded errors, e.g. in case of a bad spec.
5319 if Errmsg and then Old_Formal_Base = Any_Type then
5322 if Ctype >= Subtype_Conformant
5324 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5327 ("\predicate of & does not match!", New_Formal);
5330 ("\type of & does not match!", New_Formal);
5332 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5334 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5342 -- For mode conformance, mode must match
5344 if Ctype >= Mode_Conformant then
5345 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5346 if not Ekind_In (New_Id, E_Function, E_Procedure)
5347 or else not Is_Primitive_Wrapper (New_Id)
5349 Conformance_Error ("\mode of & does not match!", New_Formal);
5353 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5355 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5357 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5360 ("\mode of & does not match!", New_Formal);
5367 -- Part of mode conformance for access types is having the same
5368 -- constant modifier.
5370 elsif Access_Types_Match
5371 and then Is_Access_Constant (Old_Formal_Base) /=
5372 Is_Access_Constant (New_Formal_Base)
5375 ("\constant modifier does not match!", New_Formal);
5380 if Ctype >= Subtype_Conformant then
5382 -- Ada 2005 (AI-231): In case of anonymous access types check
5383 -- the null-exclusion and access-to-constant attributes must
5384 -- match. For null exclusion, we test the types rather than the
5385 -- formals themselves, since the attribute is only set reliably
5386 -- on the formals in the Ada 95 case, and we exclude the case
5387 -- where Old_Formal is marked as controlling, to avoid errors
5388 -- when matching completing bodies with dispatching declarations
5389 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5391 if Ada_Version >= Ada_2005
5392 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5393 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5395 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5396 Can_Never_Be_Null (Etype (New_Formal))
5398 not Is_Controlling_Formal (Old_Formal))
5400 Is_Access_Constant (Etype (Old_Formal)) /=
5401 Is_Access_Constant (Etype (New_Formal)))
5403 -- Do not complain if error already posted on New_Formal. This
5404 -- avoids some redundant error messages.
5406 and then not Error_Posted (New_Formal)
5408 -- It is allowed to omit the null-exclusion in case of stream
5409 -- attribute subprograms. We recognize stream subprograms
5410 -- through their TSS-generated suffix.
5413 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5416 if TSS_Name /= TSS_Stream_Read
5417 and then TSS_Name /= TSS_Stream_Write
5418 and then TSS_Name /= TSS_Stream_Input
5419 and then TSS_Name /= TSS_Stream_Output
5421 -- Here we have a definite conformance error. It is worth
5422 -- special casing the error message for the case of a
5423 -- controlling formal (which excludes null).
5425 if Is_Controlling_Formal (New_Formal) then
5426 Error_Msg_Node_2 := Scope (New_Formal);
5428 ("\controlling formal & of & excludes null, "
5429 & "declaration must exclude null as well",
5432 -- Normal case (couldn't we give more detail here???)
5436 ("\type of & does not match!", New_Formal);
5445 -- Full conformance checks
5447 if Ctype = Fully_Conformant then
5449 -- We have checked already that names match
5451 if Parameter_Mode (Old_Formal) = E_In_Parameter then
5453 -- Check default expressions for in parameters
5456 NewD : constant Boolean :=
5457 Present (Default_Value (New_Formal));
5458 OldD : constant Boolean :=
5459 Present (Default_Value (Old_Formal));
5461 if NewD or OldD then
5463 -- The old default value has been analyzed because the
5464 -- current full declaration will have frozen everything
5465 -- before. The new default value has not been analyzed,
5466 -- so analyze it now before we check for conformance.
5469 Push_Scope (New_Id);
5470 Preanalyze_Spec_Expression
5471 (Default_Value (New_Formal), Etype (New_Formal));
5475 if not (NewD and OldD)
5476 or else not Fully_Conformant_Expressions
5477 (Default_Value (Old_Formal),
5478 Default_Value (New_Formal))
5481 ("\default expression for & does not match!",
5490 -- A couple of special checks for Ada 83 mode. These checks are
5491 -- skipped if either entity is an operator in package Standard,
5492 -- or if either old or new instance is not from the source program.
5494 if Ada_Version = Ada_83
5495 and then Sloc (Old_Id) > Standard_Location
5496 and then Sloc (New_Id) > Standard_Location
5497 and then Comes_From_Source (Old_Id)
5498 and then Comes_From_Source (New_Id)
5501 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5502 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5505 -- Explicit IN must be present or absent in both cases. This
5506 -- test is required only in the full conformance case.
5508 if In_Present (Old_Param) /= In_Present (New_Param)
5509 and then Ctype = Fully_Conformant
5512 ("\(Ada 83) IN must appear in both declarations",
5517 -- Grouping (use of comma in param lists) must be the same
5518 -- This is where we catch a misconformance like:
5521 -- A : Integer; B : Integer
5523 -- which are represented identically in the tree except
5524 -- for the setting of the flags More_Ids and Prev_Ids.
5526 if More_Ids (Old_Param) /= More_Ids (New_Param)
5527 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5530 ("\grouping of & does not match!", New_Formal);
5536 -- This label is required when skipping controlling formals
5538 <<Skip_Controlling_Formal>>
5540 Next_Formal (Old_Formal);
5541 Next_Formal (New_Formal);
5544 if Present (Old_Formal) then
5545 Conformance_Error ("\too few parameters!");
5548 elsif Present (New_Formal) then
5549 Conformance_Error ("\too many parameters!", New_Formal);
5552 end Check_Conformance;
5554 -----------------------
5555 -- Check_Conventions --
5556 -----------------------
5558 procedure Check_Conventions (Typ : Entity_Id) is
5559 Ifaces_List : Elist_Id;
5561 procedure Check_Convention (Op : Entity_Id);
5562 -- Verify that the convention of inherited dispatching operation Op is
5563 -- consistent among all subprograms it overrides. In order to minimize
5564 -- the search, Search_From is utilized to designate a specific point in
5565 -- the list rather than iterating over the whole list once more.
5567 ----------------------
5568 -- Check_Convention --
5569 ----------------------
5571 procedure Check_Convention (Op : Entity_Id) is
5572 Op_Conv : constant Convention_Id := Convention (Op);
5573 Iface_Conv : Convention_Id;
5574 Iface_Elmt : Elmt_Id;
5575 Iface_Prim_Elmt : Elmt_Id;
5576 Iface_Prim : Entity_Id;
5579 Iface_Elmt := First_Elmt (Ifaces_List);
5580 while Present (Iface_Elmt) loop
5582 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5583 while Present (Iface_Prim_Elmt) loop
5584 Iface_Prim := Node (Iface_Prim_Elmt);
5585 Iface_Conv := Convention (Iface_Prim);
5587 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5588 and then Iface_Conv /= Op_Conv
5591 ("inconsistent conventions in primitive operations", Typ);
5593 Error_Msg_Name_1 := Chars (Op);
5594 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
5595 Error_Msg_Sloc := Sloc (Op);
5597 if Comes_From_Source (Op) or else No (Alias (Op)) then
5598 if not Present (Overridden_Operation (Op)) then
5599 Error_Msg_N ("\\primitive % defined #", Typ);
5602 ("\\overriding operation % with "
5603 & "convention % defined #", Typ);
5606 else pragma Assert (Present (Alias (Op)));
5607 Error_Msg_Sloc := Sloc (Alias (Op));
5608 Error_Msg_N ("\\inherited operation % with "
5609 & "convention % defined #", Typ);
5612 Error_Msg_Name_1 := Chars (Op);
5613 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
5614 Error_Msg_Sloc := Sloc (Iface_Prim);
5615 Error_Msg_N ("\\overridden operation % with "
5616 & "convention % defined #", Typ);
5618 -- Avoid cascading errors
5623 Next_Elmt (Iface_Prim_Elmt);
5626 Next_Elmt (Iface_Elmt);
5628 end Check_Convention;
5632 Prim_Op : Entity_Id;
5633 Prim_Op_Elmt : Elmt_Id;
5635 -- Start of processing for Check_Conventions
5638 if not Has_Interfaces (Typ) then
5642 Collect_Interfaces (Typ, Ifaces_List);
5644 -- The algorithm checks every overriding dispatching operation against
5645 -- all the corresponding overridden dispatching operations, detecting
5646 -- differences in conventions.
5648 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5649 while Present (Prim_Op_Elmt) loop
5650 Prim_Op := Node (Prim_Op_Elmt);
5652 -- A small optimization: skip the predefined dispatching operations
5653 -- since they always have the same convention.
5655 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5656 Check_Convention (Prim_Op);
5659 Next_Elmt (Prim_Op_Elmt);
5661 end Check_Conventions;
5663 ------------------------------
5664 -- Check_Delayed_Subprogram --
5665 ------------------------------
5667 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5670 procedure Possible_Freeze (T : Entity_Id);
5671 -- T is the type of either a formal parameter or of the return type.
5672 -- If T is not yet frozen and needs a delayed freeze, then the
5673 -- subprogram itself must be delayed.
5675 ---------------------
5676 -- Possible_Freeze --
5677 ---------------------
5679 procedure Possible_Freeze (T : Entity_Id) is
5681 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
5682 Set_Has_Delayed_Freeze (Designator);
5684 elsif Is_Access_Type (T)
5685 and then Has_Delayed_Freeze (Designated_Type (T))
5686 and then not Is_Frozen (Designated_Type (T))
5688 Set_Has_Delayed_Freeze (Designator);
5691 end Possible_Freeze;
5693 -- Start of processing for Check_Delayed_Subprogram
5696 -- All subprograms, including abstract subprograms, may need a freeze
5697 -- node if some formal type or the return type needs one.
5699 Possible_Freeze (Etype (Designator));
5700 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
5702 -- Need delayed freeze if any of the formal types themselves need
5703 -- a delayed freeze and are not yet frozen.
5705 F := First_Formal (Designator);
5706 while Present (F) loop
5707 Possible_Freeze (Etype (F));
5708 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
5712 -- Mark functions that return by reference. Note that it cannot be
5713 -- done for delayed_freeze subprograms because the underlying
5714 -- returned type may not be known yet (for private types)
5716 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
5718 Typ : constant Entity_Id := Etype (Designator);
5719 Utyp : constant Entity_Id := Underlying_Type (Typ);
5721 if Is_Limited_View (Typ) then
5722 Set_Returns_By_Ref (Designator);
5723 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
5724 Set_Returns_By_Ref (Designator);
5728 end Check_Delayed_Subprogram;
5730 ------------------------------------
5731 -- Check_Discriminant_Conformance --
5732 ------------------------------------
5734 procedure Check_Discriminant_Conformance
5739 Old_Discr : Entity_Id := First_Discriminant (Prev);
5740 New_Discr : Node_Id := First (Discriminant_Specifications (N));
5741 New_Discr_Id : Entity_Id;
5742 New_Discr_Type : Entity_Id;
5744 procedure Conformance_Error (Msg : String; N : Node_Id);
5745 -- Post error message for conformance error on given node. Two messages
5746 -- are output. The first points to the previous declaration with a
5747 -- general "no conformance" message. The second is the detailed reason,
5748 -- supplied as Msg. The parameter N provide information for a possible
5749 -- & insertion in the message.
5751 -----------------------
5752 -- Conformance_Error --
5753 -----------------------
5755 procedure Conformance_Error (Msg : String; N : Node_Id) is
5757 Error_Msg_Sloc := Sloc (Prev_Loc);
5758 Error_Msg_N -- CODEFIX
5759 ("not fully conformant with declaration#!", N);
5760 Error_Msg_NE (Msg, N, N);
5761 end Conformance_Error;
5763 -- Start of processing for Check_Discriminant_Conformance
5766 while Present (Old_Discr) and then Present (New_Discr) loop
5767 New_Discr_Id := Defining_Identifier (New_Discr);
5769 -- The subtype mark of the discriminant on the full type has not
5770 -- been analyzed so we do it here. For an access discriminant a new
5773 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
5775 Access_Definition (N, Discriminant_Type (New_Discr));
5778 Analyze (Discriminant_Type (New_Discr));
5779 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
5781 -- Ada 2005: if the discriminant definition carries a null
5782 -- exclusion, create an itype to check properly for consistency
5783 -- with partial declaration.
5785 if Is_Access_Type (New_Discr_Type)
5786 and then Null_Exclusion_Present (New_Discr)
5789 Create_Null_Excluding_Itype
5790 (T => New_Discr_Type,
5791 Related_Nod => New_Discr,
5792 Scope_Id => Current_Scope);
5796 if not Conforming_Types
5797 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
5799 Conformance_Error ("type of & does not match!", New_Discr_Id);
5802 -- Treat the new discriminant as an occurrence of the old one,
5803 -- for navigation purposes, and fill in some semantic
5804 -- information, for completeness.
5806 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
5807 Set_Etype (New_Discr_Id, Etype (Old_Discr));
5808 Set_Scope (New_Discr_Id, Scope (Old_Discr));
5813 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
5814 Conformance_Error ("name & does not match!", New_Discr_Id);
5818 -- Default expressions must match
5821 NewD : constant Boolean :=
5822 Present (Expression (New_Discr));
5823 OldD : constant Boolean :=
5824 Present (Expression (Parent (Old_Discr)));
5827 if NewD or OldD then
5829 -- The old default value has been analyzed and expanded,
5830 -- because the current full declaration will have frozen
5831 -- everything before. The new default values have not been
5832 -- expanded, so expand now to check conformance.
5835 Preanalyze_Spec_Expression
5836 (Expression (New_Discr), New_Discr_Type);
5839 if not (NewD and OldD)
5840 or else not Fully_Conformant_Expressions
5841 (Expression (Parent (Old_Discr)),
5842 Expression (New_Discr))
5846 ("default expression for & does not match!",
5853 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
5855 if Ada_Version = Ada_83 then
5857 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
5860 -- Grouping (use of comma in param lists) must be the same
5861 -- This is where we catch a misconformance like:
5864 -- A : Integer; B : Integer
5866 -- which are represented identically in the tree except
5867 -- for the setting of the flags More_Ids and Prev_Ids.
5869 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
5870 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
5873 ("grouping of & does not match!", New_Discr_Id);
5879 Next_Discriminant (Old_Discr);
5883 if Present (Old_Discr) then
5884 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
5887 elsif Present (New_Discr) then
5889 ("too many discriminants!", Defining_Identifier (New_Discr));
5892 end Check_Discriminant_Conformance;
5894 ----------------------------
5895 -- Check_Fully_Conformant --
5896 ----------------------------
5898 procedure Check_Fully_Conformant
5899 (New_Id : Entity_Id;
5901 Err_Loc : Node_Id := Empty)
5904 pragma Warnings (Off, Result);
5907 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
5908 end Check_Fully_Conformant;
5910 --------------------------
5911 -- Check_Limited_Return --
5912 --------------------------
5914 procedure Check_Limited_Return
5920 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
5921 -- replaced by anonymous access results. This is an incompatibility with
5922 -- Ada 95. Not clear whether this should be enforced yet or perhaps
5923 -- controllable with special switch. ???
5925 -- A limited interface that is not immutably limited is OK
5927 if Is_Limited_Interface (R_Type)
5929 not (Is_Task_Interface (R_Type)
5930 or else Is_Protected_Interface (R_Type)
5931 or else Is_Synchronized_Interface (R_Type))
5935 elsif Is_Limited_Type (R_Type)
5936 and then not Is_Interface (R_Type)
5937 and then Comes_From_Source (N)
5938 and then not In_Instance_Body
5939 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
5941 -- Error in Ada 2005
5943 if Ada_Version >= Ada_2005
5944 and then not Debug_Flag_Dot_L
5945 and then not GNAT_Mode
5948 ("(Ada 2005) cannot copy object of a limited type "
5949 & "(RM-2005 6.5(5.5/2))", Expr);
5951 if Is_Limited_View (R_Type) then
5953 ("\return by reference not permitted in Ada 2005", Expr);
5956 -- Warn in Ada 95 mode, to give folks a heads up about this
5959 -- In GNAT mode, this is just a warning, to allow it to be evilly
5960 -- turned off. Otherwise it is a real error.
5962 -- In a generic context, simplify the warning because it makes no
5963 -- sense to discuss pass-by-reference or copy.
5965 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
5966 if Inside_A_Generic then
5968 ("return of limited object not permitted in Ada 2005 "
5969 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5971 elsif Is_Limited_View (R_Type) then
5973 ("return by reference not permitted in Ada 2005 "
5974 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5977 ("cannot copy object of a limited type in Ada 2005 "
5978 & "(RM-2005 6.5(5.5/2))?y?", Expr);
5981 -- Ada 95 mode, and compatibility warnings disabled
5984 pragma Assert (Ada_Version <= Ada_95);
5985 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
5986 return; -- skip continuation messages below
5989 if not Inside_A_Generic then
5991 ("\consider switching to return of access type", Expr);
5992 Explain_Limited_Type (R_Type, Expr);
5995 end Check_Limited_Return;
5997 ---------------------------
5998 -- Check_Mode_Conformant --
5999 ---------------------------
6001 procedure Check_Mode_Conformant
6002 (New_Id : Entity_Id;
6004 Err_Loc : Node_Id := Empty;
6005 Get_Inst : Boolean := False)
6008 pragma Warnings (Off, Result);
6011 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6012 end Check_Mode_Conformant;
6014 --------------------------------
6015 -- Check_Overriding_Indicator --
6016 --------------------------------
6018 procedure Check_Overriding_Indicator
6020 Overridden_Subp : Entity_Id;
6021 Is_Primitive : Boolean)
6027 -- No overriding indicator for literals
6029 if Ekind (Subp) = E_Enumeration_Literal then
6032 elsif Ekind (Subp) = E_Entry then
6033 Decl := Parent (Subp);
6035 -- No point in analyzing a malformed operator
6037 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6038 and then Error_Posted (Subp)
6043 Decl := Unit_Declaration_Node (Subp);
6046 if Nkind_In (Decl, N_Subprogram_Body,
6047 N_Subprogram_Body_Stub,
6048 N_Subprogram_Declaration,
6049 N_Abstract_Subprogram_Declaration,
6050 N_Subprogram_Renaming_Declaration)
6052 Spec := Specification (Decl);
6054 elsif Nkind (Decl) = N_Entry_Declaration then
6061 -- The overriding operation is type conformant with the overridden one,
6062 -- but the names of the formals are not required to match. If the names
6063 -- appear permuted in the overriding operation, this is a possible
6064 -- source of confusion that is worth diagnosing. Controlling formals
6065 -- often carry names that reflect the type, and it is not worthwhile
6066 -- requiring that their names match.
6068 if Present (Overridden_Subp)
6069 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6076 Form1 := First_Formal (Subp);
6077 Form2 := First_Formal (Overridden_Subp);
6079 -- If the overriding operation is a synchronized operation, skip
6080 -- the first parameter of the overridden operation, which is
6081 -- implicit in the new one. If the operation is declared in the
6082 -- body it is not primitive and all formals must match.
6084 if Is_Concurrent_Type (Scope (Subp))
6085 and then Is_Tagged_Type (Scope (Subp))
6086 and then not Has_Completion (Scope (Subp))
6088 Form2 := Next_Formal (Form2);
6091 if Present (Form1) then
6092 Form1 := Next_Formal (Form1);
6093 Form2 := Next_Formal (Form2);
6096 while Present (Form1) loop
6097 if not Is_Controlling_Formal (Form1)
6098 and then Present (Next_Formal (Form2))
6099 and then Chars (Form1) = Chars (Next_Formal (Form2))
6101 Error_Msg_Node_2 := Alias (Overridden_Subp);
6102 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6104 ("& does not match corresponding formal of&#",
6109 Next_Formal (Form1);
6110 Next_Formal (Form2);
6115 -- If there is an overridden subprogram, then check that there is no
6116 -- "not overriding" indicator, and mark the subprogram as overriding.
6117 -- This is not done if the overridden subprogram is marked as hidden,
6118 -- which can occur for the case of inherited controlled operations
6119 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6120 -- subprogram is not itself hidden. (Note: This condition could probably
6121 -- be simplified, leaving out the testing for the specific controlled
6122 -- cases, but it seems safer and clearer this way, and echoes similar
6123 -- special-case tests of this kind in other places.)
6125 if Present (Overridden_Subp)
6126 and then (not Is_Hidden (Overridden_Subp)
6128 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6131 and then Present (Alias (Overridden_Subp))
6132 and then not Is_Hidden (Alias (Overridden_Subp))))
6134 if Must_Not_Override (Spec) then
6135 Error_Msg_Sloc := Sloc (Overridden_Subp);
6137 if Ekind (Subp) = E_Entry then
6139 ("entry & overrides inherited operation #", Spec, Subp);
6142 ("subprogram & overrides inherited operation #", Spec, Subp);
6145 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6146 -- as an extension of Root_Controlled, and thus has a useless Adjust
6147 -- operation. This operation should not be inherited by other limited
6148 -- controlled types. An explicit Adjust for them is not overriding.
6150 elsif Must_Override (Spec)
6151 and then Chars (Overridden_Subp) = Name_Adjust
6152 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6153 and then Present (Alias (Overridden_Subp))
6154 and then In_Predefined_Unit (Alias (Overridden_Subp))
6157 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6158 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6160 elsif Is_Subprogram (Subp) then
6161 if Is_Init_Proc (Subp) then
6164 elsif No (Overridden_Operation (Subp)) then
6166 -- For entities generated by Derive_Subprograms the overridden
6167 -- operation is the inherited primitive (which is available
6168 -- through the attribute alias)
6170 if (Is_Dispatching_Operation (Subp)
6171 or else Is_Dispatching_Operation (Overridden_Subp))
6172 and then not Comes_From_Source (Overridden_Subp)
6173 and then Find_Dispatching_Type (Overridden_Subp) =
6174 Find_Dispatching_Type (Subp)
6175 and then Present (Alias (Overridden_Subp))
6176 and then Comes_From_Source (Alias (Overridden_Subp))
6178 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6179 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6182 Set_Overridden_Operation (Subp, Overridden_Subp);
6183 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6188 -- If primitive flag is set or this is a protected operation, then
6189 -- the operation is overriding at the point of its declaration, so
6190 -- warn if necessary. Otherwise it may have been declared before the
6191 -- operation it overrides and no check is required.
6194 and then not Must_Override (Spec)
6195 and then (Is_Primitive
6196 or else Ekind (Scope (Subp)) = E_Protected_Type)
6198 Style.Missing_Overriding (Decl, Subp);
6201 -- If Subp is an operator, it may override a predefined operation, if
6202 -- it is defined in the same scope as the type to which it applies.
6203 -- In that case Overridden_Subp is empty because of our implicit
6204 -- representation for predefined operators. We have to check whether the
6205 -- signature of Subp matches that of a predefined operator. Note that
6206 -- first argument provides the name of the operator, and the second
6207 -- argument the signature that may match that of a standard operation.
6208 -- If the indicator is overriding, then the operator must match a
6209 -- predefined signature, because we know already that there is no
6210 -- explicit overridden operation.
6212 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6213 if Must_Not_Override (Spec) then
6215 -- If this is not a primitive or a protected subprogram, then
6216 -- "not overriding" is illegal.
6219 and then Ekind (Scope (Subp)) /= E_Protected_Type
6221 Error_Msg_N ("overriding indicator only allowed "
6222 & "if subprogram is primitive", Subp);
6224 elsif Can_Override_Operator (Subp) then
6226 ("subprogram& overrides predefined operator ", Spec, Subp);
6229 elsif Must_Override (Spec) then
6230 if No (Overridden_Operation (Subp))
6231 and then not Can_Override_Operator (Subp)
6233 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6236 elsif not Error_Posted (Subp)
6237 and then Style_Check
6238 and then Can_Override_Operator (Subp)
6239 and then not In_Predefined_Unit (Subp)
6241 -- If style checks are enabled, indicate that the indicator is
6242 -- missing. However, at the point of declaration, the type of
6243 -- which this is a primitive operation may be private, in which
6244 -- case the indicator would be premature.
6246 if Has_Private_Declaration (Etype (Subp))
6247 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6251 Style.Missing_Overriding (Decl, Subp);
6255 elsif Must_Override (Spec) then
6256 if Ekind (Subp) = E_Entry then
6257 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6259 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6262 -- If the operation is marked "not overriding" and it's not primitive
6263 -- then an error is issued, unless this is an operation of a task or
6264 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6265 -- has been specified have already been checked above.
6267 elsif Must_Not_Override (Spec)
6268 and then not Is_Primitive
6269 and then Ekind (Subp) /= E_Entry
6270 and then Ekind (Scope (Subp)) /= E_Protected_Type
6273 ("overriding indicator only allowed if subprogram is primitive",
6277 end Check_Overriding_Indicator;
6283 -- Note: this procedure needs to know far too much about how the expander
6284 -- messes with exceptions. The use of the flag Exception_Junk and the
6285 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6286 -- works, but is not very clean. It would be better if the expansion
6287 -- routines would leave Original_Node working nicely, and we could use
6288 -- Original_Node here to ignore all the peculiar expander messing ???
6290 procedure Check_Returns
6294 Proc : Entity_Id := Empty)
6298 procedure Check_Statement_Sequence (L : List_Id);
6299 -- Internal recursive procedure to check a list of statements for proper
6300 -- termination by a return statement (or a transfer of control or a
6301 -- compound statement that is itself internally properly terminated).
6303 ------------------------------
6304 -- Check_Statement_Sequence --
6305 ------------------------------
6307 procedure Check_Statement_Sequence (L : List_Id) is
6312 function Assert_False return Boolean;
6313 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6314 -- rewritten as a null statement when assertions are off. The assert
6315 -- is not active, but it is still enough to kill the warning.
6321 function Assert_False return Boolean is
6322 Orig : constant Node_Id := Original_Node (Last_Stm);
6325 if Nkind (Orig) = N_Pragma
6326 and then Pragma_Name (Orig) = Name_Assert
6327 and then not Error_Posted (Orig)
6330 Arg : constant Node_Id :=
6331 First (Pragma_Argument_Associations (Orig));
6332 Exp : constant Node_Id := Expression (Arg);
6334 return Nkind (Exp) = N_Identifier
6335 and then Chars (Exp) = Name_False;
6345 Raise_Exception_Call : Boolean;
6346 -- Set True if statement sequence terminated by Raise_Exception call
6347 -- or a Reraise_Occurrence call.
6349 -- Start of processing for Check_Statement_Sequence
6352 Raise_Exception_Call := False;
6354 -- Get last real statement
6356 Last_Stm := Last (L);
6358 -- Deal with digging out exception handler statement sequences that
6359 -- have been transformed by the local raise to goto optimization.
6360 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6361 -- optimization has occurred, we are looking at something like:
6364 -- original stmts in block
6368 -- goto L1; | omitted if No_Exception_Propagation
6373 -- goto L3; -- skip handler when exception not raised
6375 -- <<L1>> -- target label for local exception
6389 -- and what we have to do is to dig out the estmts1 and estmts2
6390 -- sequences (which were the original sequences of statements in
6391 -- the exception handlers) and check them.
6393 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
6398 exit when Nkind (Stm) /= N_Block_Statement;
6399 exit when not Exception_Junk (Stm);
6402 exit when Nkind (Stm) /= N_Label;
6403 exit when not Exception_Junk (Stm);
6404 Check_Statement_Sequence
6405 (Statements (Handled_Statement_Sequence (Next (Stm))));
6410 exit when Nkind (Stm) /= N_Goto_Statement;
6411 exit when not Exception_Junk (Stm);
6415 -- Don't count pragmas
6417 while Nkind (Last_Stm) = N_Pragma
6419 -- Don't count call to SS_Release (can happen after Raise_Exception)
6422 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6424 Nkind (Name (Last_Stm)) = N_Identifier
6426 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6428 -- Don't count exception junk
6431 (Nkind_In (Last_Stm, N_Goto_Statement,
6433 N_Object_Declaration)
6434 and then Exception_Junk (Last_Stm))
6435 or else Nkind (Last_Stm) in N_Push_xxx_Label
6436 or else Nkind (Last_Stm) in N_Pop_xxx_Label
6438 -- Inserted code, such as finalization calls, is irrelevant: we only
6439 -- need to check original source.
6441 or else Is_Rewrite_Insertion (Last_Stm)
6446 -- Here we have the "real" last statement
6448 Kind := Nkind (Last_Stm);
6450 -- Transfer of control, OK. Note that in the No_Return procedure
6451 -- case, we already diagnosed any explicit return statements, so
6452 -- we can treat them as OK in this context.
6454 if Is_Transfer (Last_Stm) then
6457 -- Check cases of explicit non-indirect procedure calls
6459 elsif Kind = N_Procedure_Call_Statement
6460 and then Is_Entity_Name (Name (Last_Stm))
6462 -- Check call to Raise_Exception procedure which is treated
6463 -- specially, as is a call to Reraise_Occurrence.
6465 -- We suppress the warning in these cases since it is likely that
6466 -- the programmer really does not expect to deal with the case
6467 -- of Null_Occurrence, and thus would find a warning about a
6468 -- missing return curious, and raising Program_Error does not
6469 -- seem such a bad behavior if this does occur.
6471 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6472 -- behavior will be to raise Constraint_Error (see AI-329).
6474 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6476 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6478 Raise_Exception_Call := True;
6480 -- For Raise_Exception call, test first argument, if it is
6481 -- an attribute reference for a 'Identity call, then we know
6482 -- that the call cannot possibly return.
6485 Arg : constant Node_Id :=
6486 Original_Node (First_Actual (Last_Stm));
6488 if Nkind (Arg) = N_Attribute_Reference
6489 and then Attribute_Name (Arg) = Name_Identity
6496 -- If statement, need to look inside if there is an else and check
6497 -- each constituent statement sequence for proper termination.
6499 elsif Kind = N_If_Statement
6500 and then Present (Else_Statements (Last_Stm))
6502 Check_Statement_Sequence (Then_Statements (Last_Stm));
6503 Check_Statement_Sequence (Else_Statements (Last_Stm));
6505 if Present (Elsif_Parts (Last_Stm)) then
6507 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6510 while Present (Elsif_Part) loop
6511 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6519 -- Case statement, check each case for proper termination
6521 elsif Kind = N_Case_Statement then
6525 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6526 while Present (Case_Alt) loop
6527 Check_Statement_Sequence (Statements (Case_Alt));
6528 Next_Non_Pragma (Case_Alt);
6534 -- Block statement, check its handled sequence of statements
6536 elsif Kind = N_Block_Statement then
6542 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6551 -- Loop statement. If there is an iteration scheme, we can definitely
6552 -- fall out of the loop. Similarly if there is an exit statement, we
6553 -- can fall out. In either case we need a following return.
6555 elsif Kind = N_Loop_Statement then
6556 if Present (Iteration_Scheme (Last_Stm))
6557 or else Has_Exit (Entity (Identifier (Last_Stm)))
6561 -- A loop with no exit statement or iteration scheme is either
6562 -- an infinite loop, or it has some other exit (raise/return).
6563 -- In either case, no warning is required.
6569 -- Timed entry call, check entry call and delay alternatives
6571 -- Note: in expanded code, the timed entry call has been converted
6572 -- to a set of expanded statements on which the check will work
6573 -- correctly in any case.
6575 elsif Kind = N_Timed_Entry_Call then
6577 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6578 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6581 -- If statement sequence of entry call alternative is missing,
6582 -- then we can definitely fall through, and we post the error
6583 -- message on the entry call alternative itself.
6585 if No (Statements (ECA)) then
6588 -- If statement sequence of delay alternative is missing, then
6589 -- we can definitely fall through, and we post the error
6590 -- message on the delay alternative itself.
6592 -- Note: if both ECA and DCA are missing the return, then we
6593 -- post only one message, should be enough to fix the bugs.
6594 -- If not we will get a message next time on the DCA when the
6597 elsif No (Statements (DCA)) then
6600 -- Else check both statement sequences
6603 Check_Statement_Sequence (Statements (ECA));
6604 Check_Statement_Sequence (Statements (DCA));
6609 -- Conditional entry call, check entry call and else part
6611 -- Note: in expanded code, the conditional entry call has been
6612 -- converted to a set of expanded statements on which the check
6613 -- will work correctly in any case.
6615 elsif Kind = N_Conditional_Entry_Call then
6617 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6620 -- If statement sequence of entry call alternative is missing,
6621 -- then we can definitely fall through, and we post the error
6622 -- message on the entry call alternative itself.
6624 if No (Statements (ECA)) then
6627 -- Else check statement sequence and else part
6630 Check_Statement_Sequence (Statements (ECA));
6631 Check_Statement_Sequence (Else_Statements (Last_Stm));
6637 -- If we fall through, issue appropriate message
6641 -- Kill warning if last statement is a raise exception call,
6642 -- or a pragma Assert (False). Note that with assertions enabled,
6643 -- such a pragma has been converted into a raise exception call
6644 -- already, so the Assert_False is for the assertions off case.
6646 if not Raise_Exception_Call and then not Assert_False then
6648 -- In GNATprove mode, it is an error to have a missing return
6650 Error_Msg_Warn := SPARK_Mode /= On;
6652 -- Issue error message or warning
6655 ("RETURN statement missing following this statement<<!",
6658 ("\Program_Error ]<<!", Last_Stm);
6661 -- Note: we set Err even though we have not issued a warning
6662 -- because we still have a case of a missing return. This is
6663 -- an extremely marginal case, probably will never be noticed
6664 -- but we might as well get it right.
6668 -- Otherwise we have the case of a procedure marked No_Return
6671 if not Raise_Exception_Call then
6672 if GNATprove_Mode then
6674 ("implied return after this statement would have raised "
6675 & "Program_Error", Last_Stm);
6677 -- In normal compilation mode, do not warn on a generated call
6678 -- (e.g. in the body of a renaming as completion).
6680 elsif Comes_From_Source (Last_Stm) then
6682 ("implied return after this statement will raise "
6683 & "Program_Error??", Last_Stm);
6686 Error_Msg_Warn := SPARK_Mode /= On;
6688 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
6692 RE : constant Node_Id :=
6693 Make_Raise_Program_Error (Sloc (Last_Stm),
6694 Reason => PE_Implicit_Return);
6696 Insert_After (Last_Stm, RE);
6700 end Check_Statement_Sequence;
6702 -- Start of processing for Check_Returns
6706 Check_Statement_Sequence (Statements (HSS));
6708 if Present (Exception_Handlers (HSS)) then
6709 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6710 while Present (Handler) loop
6711 Check_Statement_Sequence (Statements (Handler));
6712 Next_Non_Pragma (Handler);
6717 ----------------------------
6718 -- Check_Subprogram_Order --
6719 ----------------------------
6721 procedure Check_Subprogram_Order (N : Node_Id) is
6723 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
6724 -- This is used to check if S1 > S2 in the sense required by this test,
6725 -- for example nameab < namec, but name2 < name10.
6727 -----------------------------
6728 -- Subprogram_Name_Greater --
6729 -----------------------------
6731 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
6736 -- Deal with special case where names are identical except for a
6737 -- numerical suffix. These are handled specially, taking the numeric
6738 -- ordering from the suffix into account.
6741 while S1 (L1) in '0' .. '9' loop
6746 while S2 (L2) in '0' .. '9' loop
6750 -- If non-numeric parts non-equal, do straight compare
6752 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
6755 -- If non-numeric parts equal, compare suffixed numeric parts. Note
6756 -- that a missing suffix is treated as numeric zero in this test.
6760 while L1 < S1'Last loop
6762 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
6766 while L2 < S2'Last loop
6768 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
6773 end Subprogram_Name_Greater;
6775 -- Start of processing for Check_Subprogram_Order
6778 -- Check body in alpha order if this is option
6781 and then Style_Check_Order_Subprograms
6782 and then Nkind (N) = N_Subprogram_Body
6783 and then Comes_From_Source (N)
6784 and then In_Extended_Main_Source_Unit (N)
6788 renames Scope_Stack.Table
6789 (Scope_Stack.Last).Last_Subprogram_Name;
6791 Body_Id : constant Entity_Id :=
6792 Defining_Entity (Specification (N));
6795 Get_Decoded_Name_String (Chars (Body_Id));
6798 if Subprogram_Name_Greater
6799 (LSN.all, Name_Buffer (1 .. Name_Len))
6801 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
6807 LSN := new String'(Name_Buffer (1 .. Name_Len));
6810 end Check_Subprogram_Order;
6812 ------------------------------
6813 -- Check_Subtype_Conformant --
6814 ------------------------------
6816 procedure Check_Subtype_Conformant
6817 (New_Id : Entity_Id;
6819 Err_Loc : Node_Id := Empty;
6820 Skip_Controlling_Formals : Boolean := False;
6821 Get_Inst : Boolean := False)
6824 pragma Warnings (Off, Result);
6827 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
6828 Skip_Controlling_Formals => Skip_Controlling_Formals,
6829 Get_Inst => Get_Inst);
6830 end Check_Subtype_Conformant;
6832 -----------------------------------
6833 -- Check_Synchronized_Overriding --
6834 -----------------------------------
6836 procedure Check_Synchronized_Overriding
6837 (Def_Id : Entity_Id;
6838 Overridden_Subp : out Entity_Id)
6840 Ifaces_List : Elist_Id;
6844 function Matches_Prefixed_View_Profile
6845 (Prim_Params : List_Id;
6846 Iface_Params : List_Id) return Boolean;
6847 -- Determine whether a subprogram's parameter profile Prim_Params
6848 -- matches that of a potentially overridden interface subprogram
6849 -- Iface_Params. Also determine if the type of first parameter of
6850 -- Iface_Params is an implemented interface.
6852 -----------------------------------
6853 -- Matches_Prefixed_View_Profile --
6854 -----------------------------------
6856 function Matches_Prefixed_View_Profile
6857 (Prim_Params : List_Id;
6858 Iface_Params : List_Id) return Boolean
6860 function Is_Implemented
6861 (Ifaces_List : Elist_Id;
6862 Iface : Entity_Id) return Boolean;
6863 -- Determine if Iface is implemented by the current task or
6866 --------------------
6867 -- Is_Implemented --
6868 --------------------
6870 function Is_Implemented
6871 (Ifaces_List : Elist_Id;
6872 Iface : Entity_Id) return Boolean
6874 Iface_Elmt : Elmt_Id;
6877 Iface_Elmt := First_Elmt (Ifaces_List);
6878 while Present (Iface_Elmt) loop
6879 if Node (Iface_Elmt) = Iface then
6883 Next_Elmt (Iface_Elmt);
6891 Iface_Id : Entity_Id;
6892 Iface_Param : Node_Id;
6893 Iface_Typ : Entity_Id;
6894 Prim_Id : Entity_Id;
6895 Prim_Param : Node_Id;
6896 Prim_Typ : Entity_Id;
6898 -- Start of processing for Matches_Prefixed_View_Profile
6901 Iface_Param := First (Iface_Params);
6902 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6904 if Is_Access_Type (Iface_Typ) then
6905 Iface_Typ := Designated_Type (Iface_Typ);
6908 Prim_Param := First (Prim_Params);
6910 -- The first parameter of the potentially overridden subprogram must
6911 -- be an interface implemented by Prim.
6913 if not Is_Interface (Iface_Typ)
6914 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6919 -- The checks on the object parameters are done, so move on to the
6920 -- rest of the parameters.
6922 if not In_Scope then
6923 Prim_Param := Next (Prim_Param);
6926 Iface_Param := Next (Iface_Param);
6927 while Present (Iface_Param) and then Present (Prim_Param) loop
6928 Iface_Id := Defining_Identifier (Iface_Param);
6929 Iface_Typ := Find_Parameter_Type (Iface_Param);
6931 Prim_Id := Defining_Identifier (Prim_Param);
6932 Prim_Typ := Find_Parameter_Type (Prim_Param);
6934 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6935 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6936 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6938 Iface_Typ := Designated_Type (Iface_Typ);
6939 Prim_Typ := Designated_Type (Prim_Typ);
6942 -- Case of multiple interface types inside a parameter profile
6944 -- (Obj_Param : in out Iface; ...; Param : Iface)
6946 -- If the interface type is implemented, then the matching type in
6947 -- the primitive should be the implementing record type.
6949 if Ekind (Iface_Typ) = E_Record_Type
6950 and then Is_Interface (Iface_Typ)
6951 and then Is_Implemented (Ifaces_List, Iface_Typ)
6953 if Prim_Typ /= Typ then
6957 -- The two parameters must be both mode and subtype conformant
6959 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6961 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6970 -- One of the two lists contains more parameters than the other
6972 if Present (Iface_Param) or else Present (Prim_Param) then
6977 end Matches_Prefixed_View_Profile;
6979 -- Start of processing for Check_Synchronized_Overriding
6982 Overridden_Subp := Empty;
6984 -- Def_Id must be an entry or a subprogram. We should skip predefined
6985 -- primitives internally generated by the front end; however at this
6986 -- stage predefined primitives are still not fully decorated. As a
6987 -- minor optimization we skip here internally generated subprograms.
6989 if (Ekind (Def_Id) /= E_Entry
6990 and then Ekind (Def_Id) /= E_Function
6991 and then Ekind (Def_Id) /= E_Procedure)
6992 or else not Comes_From_Source (Def_Id)
6997 -- Search for the concurrent declaration since it contains the list of
6998 -- all implemented interfaces. In this case, the subprogram is declared
6999 -- within the scope of a protected or a task type.
7001 if Present (Scope (Def_Id))
7002 and then Is_Concurrent_Type (Scope (Def_Id))
7003 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7005 Typ := Scope (Def_Id);
7008 -- The enclosing scope is not a synchronized type and the subprogram
7011 elsif No (First_Formal (Def_Id)) then
7014 -- The subprogram has formals and hence it may be a primitive of a
7018 Typ := Etype (First_Formal (Def_Id));
7020 if Is_Access_Type (Typ) then
7021 Typ := Directly_Designated_Type (Typ);
7024 if Is_Concurrent_Type (Typ)
7025 and then not Is_Generic_Actual_Type (Typ)
7029 -- This case occurs when the concurrent type is declared within a
7030 -- generic unit. As a result the corresponding record has been built
7031 -- and used as the type of the first formal, we just have to retrieve
7032 -- the corresponding concurrent type.
7034 elsif Is_Concurrent_Record_Type (Typ)
7035 and then not Is_Class_Wide_Type (Typ)
7036 and then Present (Corresponding_Concurrent_Type (Typ))
7038 Typ := Corresponding_Concurrent_Type (Typ);
7046 -- There is no overriding to check if this is an inherited operation in
7047 -- a type derivation for a generic actual.
7049 Collect_Interfaces (Typ, Ifaces_List);
7051 if Is_Empty_Elmt_List (Ifaces_List) then
7055 -- Determine whether entry or subprogram Def_Id overrides a primitive
7056 -- operation that belongs to one of the interfaces in Ifaces_List.
7059 Candidate : Entity_Id := Empty;
7060 Hom : Entity_Id := Empty;
7061 Subp : Entity_Id := Empty;
7064 -- Traverse the homonym chain, looking for a potentially overridden
7065 -- subprogram that belongs to an implemented interface.
7067 Hom := Current_Entity_In_Scope (Def_Id);
7068 while Present (Hom) loop
7072 or else not Is_Overloadable (Subp)
7073 or else not Is_Primitive (Subp)
7074 or else not Is_Dispatching_Operation (Subp)
7075 or else not Present (Find_Dispatching_Type (Subp))
7076 or else not Is_Interface (Find_Dispatching_Type (Subp))
7080 -- Entries and procedures can override abstract or null interface
7083 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7084 and then Ekind (Subp) = E_Procedure
7085 and then Matches_Prefixed_View_Profile
7086 (Parameter_Specifications (Parent (Def_Id)),
7087 Parameter_Specifications (Parent (Subp)))
7091 -- For an overridden subprogram Subp, check whether the mode
7092 -- of its first parameter is correct depending on the kind of
7093 -- synchronized type.
7096 Formal : constant Node_Id := First_Formal (Candidate);
7099 -- In order for an entry or a protected procedure to
7100 -- override, the first parameter of the overridden routine
7101 -- must be of mode "out", "in out", or access-to-variable.
7103 if Ekind_In (Candidate, E_Entry, E_Procedure)
7104 and then Is_Protected_Type (Typ)
7105 and then Ekind (Formal) /= E_In_Out_Parameter
7106 and then Ekind (Formal) /= E_Out_Parameter
7107 and then Nkind (Parameter_Type (Parent (Formal))) /=
7112 -- All other cases are OK since a task entry or routine does
7113 -- not have a restriction on the mode of the first parameter
7114 -- of the overridden interface routine.
7117 Overridden_Subp := Candidate;
7122 -- Functions can override abstract interface functions
7124 elsif Ekind (Def_Id) = E_Function
7125 and then Ekind (Subp) = E_Function
7126 and then Matches_Prefixed_View_Profile
7127 (Parameter_Specifications (Parent (Def_Id)),
7128 Parameter_Specifications (Parent (Subp)))
7129 and then Etype (Def_Id) = Etype (Subp)
7133 -- If an inherited subprogram is implemented by a protected
7134 -- function, then the first parameter of the inherited
7135 -- subprogram shall be of mode in, but not an access-to-
7136 -- variable parameter (RM 9.4(11/9)).
7138 if Present (First_Formal (Subp))
7139 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7141 (not Is_Access_Type (Etype (First_Formal (Subp)))
7143 Is_Access_Constant (Etype (First_Formal (Subp))))
7145 Overridden_Subp := Subp;
7150 Hom := Homonym (Hom);
7153 -- After examining all candidates for overriding, we are left with
7154 -- the best match, which is a mode-incompatible interface routine.
7156 if In_Scope and then Present (Candidate) then
7157 Error_Msg_PT (Def_Id, Candidate);
7160 Overridden_Subp := Candidate;
7163 end Check_Synchronized_Overriding;
7165 ---------------------------
7166 -- Check_Type_Conformant --
7167 ---------------------------
7169 procedure Check_Type_Conformant
7170 (New_Id : Entity_Id;
7172 Err_Loc : Node_Id := Empty)
7175 pragma Warnings (Off, Result);
7178 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7179 end Check_Type_Conformant;
7181 ---------------------------
7182 -- Can_Override_Operator --
7183 ---------------------------
7185 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7189 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7193 Typ := Base_Type (Etype (First_Formal (Subp)));
7195 -- Check explicitly that the operation is a primitive of the type
7197 return Operator_Matches_Spec (Subp, Subp)
7198 and then not Is_Generic_Type (Typ)
7199 and then Scope (Subp) = Scope (Typ)
7200 and then not Is_Class_Wide_Type (Typ);
7202 end Can_Override_Operator;
7204 ----------------------
7205 -- Conforming_Types --
7206 ----------------------
7208 function Conforming_Types
7211 Ctype : Conformance_Type;
7212 Get_Inst : Boolean := False) return Boolean
7214 function Base_Types_Match
7216 Typ_2 : Entity_Id) return Boolean;
7217 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7218 -- in different scopes (e.g. parent and child instances), then verify
7219 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7220 -- the same subtype chain. The whole purpose of this procedure is to
7221 -- prevent spurious ambiguities in an instantiation that may arise if
7222 -- two distinct generic types are instantiated with the same actual.
7224 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7225 -- An access parameter can designate an incomplete type. If the
7226 -- incomplete type is the limited view of a type from a limited_
7227 -- with_clause, check whether the non-limited view is available.
7228 -- If it is a (non-limited) incomplete type, get the full view.
7230 function Matches_Limited_With_View
7232 Typ_2 : Entity_Id) return Boolean;
7233 -- Returns True if and only if either Typ_1 denotes a limited view of
7234 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7235 -- the limited with view of a type is used in a subprogram declaration
7236 -- and the subprogram body is in the scope of a regular with clause for
7237 -- the same unit. In such a case, the two type entities are considered
7238 -- identical for purposes of conformance checking.
7240 ----------------------
7241 -- Base_Types_Match --
7242 ----------------------
7244 function Base_Types_Match
7246 Typ_2 : Entity_Id) return Boolean
7248 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7249 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7252 if Typ_1 = Typ_2 then
7255 elsif Base_1 = Base_2 then
7257 -- The following is too permissive. A more precise test should
7258 -- check that the generic actual is an ancestor subtype of the
7261 -- See code in Find_Corresponding_Spec that applies an additional
7262 -- filter to handle accidental amiguities in instances.
7265 not Is_Generic_Actual_Type (Typ_1)
7266 or else not Is_Generic_Actual_Type (Typ_2)
7267 or else Scope (Typ_1) /= Scope (Typ_2);
7269 -- If Typ_2 is a generic actual type it is declared as the subtype of
7270 -- the actual. If that actual is itself a subtype we need to use its
7271 -- own base type to check for compatibility.
7273 elsif Ekind (Base_2) = Ekind (Typ_2)
7274 and then Base_1 = Base_Type (Base_2)
7278 elsif Ekind (Base_1) = Ekind (Typ_1)
7279 and then Base_2 = Base_Type (Base_1)
7286 end Base_Types_Match;
7288 --------------------------
7289 -- Find_Designated_Type --
7290 --------------------------
7292 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7296 Desig := Directly_Designated_Type (Typ);
7298 if Ekind (Desig) = E_Incomplete_Type then
7300 -- If regular incomplete type, get full view if available
7302 if Present (Full_View (Desig)) then
7303 Desig := Full_View (Desig);
7305 -- If limited view of a type, get non-limited view if available,
7306 -- and check again for a regular incomplete type.
7308 elsif Present (Non_Limited_View (Desig)) then
7309 Desig := Get_Full_View (Non_Limited_View (Desig));
7314 end Find_Designated_Type;
7316 -------------------------------
7317 -- Matches_Limited_With_View --
7318 -------------------------------
7320 function Matches_Limited_With_View
7322 Typ_2 : Entity_Id) return Boolean
7324 function Is_Matching_Limited_View
7326 View : Entity_Id) return Boolean;
7327 -- Determine whether non-limited view View denotes type Typ in some
7328 -- conformant fashion.
7330 ------------------------------
7331 -- Is_Matching_Limited_View --
7332 ------------------------------
7334 function Is_Matching_Limited_View
7336 View : Entity_Id) return Boolean
7338 Root_Typ : Entity_Id;
7339 Root_View : Entity_Id;
7342 -- The non-limited view directly denotes the type
7347 -- The type is a subtype of the non-limited view
7349 elsif Is_Subtype_Of (Typ, View) then
7352 -- Both the non-limited view and the type denote class-wide types
7354 elsif Is_Class_Wide_Type (Typ)
7355 and then Is_Class_Wide_Type (View)
7357 Root_Typ := Root_Type (Typ);
7358 Root_View := Root_Type (View);
7360 if Root_Typ = Root_View then
7363 -- An incomplete tagged type and its full view may receive two
7364 -- distinct class-wide types when the related package has not
7365 -- been analyzed yet.
7368 -- type T is tagged; -- CW_1
7369 -- type T is tagged null record; -- CW_2
7372 -- This is because the package lacks any semantic information
7373 -- that may eventually link both views of T. As a consequence,
7374 -- a client of the limited view of Pack will see CW_2 while a
7375 -- client of the non-limited view of Pack will see CW_1.
7377 elsif Is_Incomplete_Type (Root_Typ)
7378 and then Present (Full_View (Root_Typ))
7379 and then Full_View (Root_Typ) = Root_View
7383 elsif Is_Incomplete_Type (Root_View)
7384 and then Present (Full_View (Root_View))
7385 and then Full_View (Root_View) = Root_Typ
7392 end Is_Matching_Limited_View;
7394 -- Start of processing for Matches_Limited_With_View
7397 -- In some cases a type imported through a limited_with clause, and
7398 -- its non-limited view are both visible, for example in an anonymous
7399 -- access-to-class-wide type in a formal, or when building the body
7400 -- for a subprogram renaming after the subprogram has been frozen.
7401 -- In these cases both entities designate the same type. In addition,
7402 -- if one of them is an actual in an instance, it may be a subtype of
7403 -- the non-limited view of the other.
7405 if From_Limited_With (Typ_1)
7406 and then From_Limited_With (Typ_2)
7407 and then Available_View (Typ_1) = Available_View (Typ_2)
7411 elsif From_Limited_With (Typ_1) then
7412 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
7414 elsif From_Limited_With (Typ_2) then
7415 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
7420 end Matches_Limited_With_View;
7424 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7426 Type_1 : Entity_Id := T1;
7427 Type_2 : Entity_Id := T2;
7429 -- Start of processing for Conforming_Types
7432 -- The context is an instance association for a formal access-to-
7433 -- subprogram type; the formal parameter types require mapping because
7434 -- they may denote other formal parameters of the generic unit.
7437 Type_1 := Get_Instance_Of (T1);
7438 Type_2 := Get_Instance_Of (T2);
7441 -- If one of the types is a view of the other introduced by a limited
7442 -- with clause, treat these as conforming for all purposes.
7444 if Matches_Limited_With_View (T1, T2) then
7447 elsif Base_Types_Match (Type_1, Type_2) then
7448 if Ctype <= Mode_Conformant then
7453 Subtypes_Statically_Match (Type_1, Type_2)
7454 and then Dimensions_Match (Type_1, Type_2);
7457 elsif Is_Incomplete_Or_Private_Type (Type_1)
7458 and then Present (Full_View (Type_1))
7459 and then Base_Types_Match (Full_View (Type_1), Type_2)
7462 Ctype <= Mode_Conformant
7463 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7465 elsif Ekind (Type_2) = E_Incomplete_Type
7466 and then Present (Full_View (Type_2))
7467 and then Base_Types_Match (Type_1, Full_View (Type_2))
7470 Ctype <= Mode_Conformant
7471 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7473 elsif Is_Private_Type (Type_2)
7474 and then In_Instance
7475 and then Present (Full_View (Type_2))
7476 and then Base_Types_Match (Type_1, Full_View (Type_2))
7479 Ctype <= Mode_Conformant
7480 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7482 -- Another confusion between views in a nested instance with an
7483 -- actual private type whose full view is not in scope.
7485 elsif Ekind (Type_2) = E_Private_Subtype
7486 and then In_Instance
7487 and then Etype (Type_2) = Type_1
7491 -- In Ada 2012, incomplete types (including limited views) can appear
7492 -- as actuals in instantiations.
7494 elsif Is_Incomplete_Type (Type_1)
7495 and then Is_Incomplete_Type (Type_2)
7496 and then (Used_As_Generic_Actual (Type_1)
7497 or else Used_As_Generic_Actual (Type_2))
7502 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7503 -- treated recursively because they carry a signature. As far as
7504 -- conformance is concerned, convention plays no role, and either
7505 -- or both could be access to protected subprograms.
7507 Are_Anonymous_Access_To_Subprogram_Types :=
7508 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7509 E_Anonymous_Access_Protected_Subprogram_Type)
7511 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7512 E_Anonymous_Access_Protected_Subprogram_Type);
7514 -- Test anonymous access type case. For this case, static subtype
7515 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7516 -- the base types because we may have built internal subtype entities
7517 -- to handle null-excluding types (see Process_Formals).
7519 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7521 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7523 -- Ada 2005 (AI-254)
7525 or else Are_Anonymous_Access_To_Subprogram_Types
7528 Desig_1 : Entity_Id;
7529 Desig_2 : Entity_Id;
7532 -- In Ada 2005, access constant indicators must match for
7533 -- subtype conformance.
7535 if Ada_Version >= Ada_2005
7536 and then Ctype >= Subtype_Conformant
7538 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7543 Desig_1 := Find_Designated_Type (Type_1);
7544 Desig_2 := Find_Designated_Type (Type_2);
7546 -- If the context is an instance association for a formal
7547 -- access-to-subprogram type; formal access parameter designated
7548 -- types require mapping because they may denote other formal
7549 -- parameters of the generic unit.
7552 Desig_1 := Get_Instance_Of (Desig_1);
7553 Desig_2 := Get_Instance_Of (Desig_2);
7556 -- It is possible for a Class_Wide_Type to be introduced for an
7557 -- incomplete type, in which case there is a separate class_ wide
7558 -- type for the full view. The types conform if their Etypes
7559 -- conform, i.e. one may be the full view of the other. This can
7560 -- only happen in the context of an access parameter, other uses
7561 -- of an incomplete Class_Wide_Type are illegal.
7563 if Is_Class_Wide_Type (Desig_1)
7565 Is_Class_Wide_Type (Desig_2)
7569 (Etype (Base_Type (Desig_1)),
7570 Etype (Base_Type (Desig_2)), Ctype);
7572 elsif Are_Anonymous_Access_To_Subprogram_Types then
7573 if Ada_Version < Ada_2005 then
7575 Ctype = Type_Conformant
7576 or else Subtypes_Statically_Match (Desig_1, Desig_2);
7578 -- We must check the conformance of the signatures themselves
7582 Conformant : Boolean;
7585 (Desig_1, Desig_2, Ctype, False, Conformant);
7590 -- A limited view of an actual matches the corresponding
7591 -- incomplete formal.
7593 elsif Ekind (Desig_2) = E_Incomplete_Subtype
7594 and then From_Limited_With (Desig_2)
7595 and then Used_As_Generic_Actual (Etype (Desig_2))
7600 return Base_Type (Desig_1) = Base_Type (Desig_2)
7601 and then (Ctype = Type_Conformant
7603 Subtypes_Statically_Match (Desig_1, Desig_2));
7607 -- Otherwise definitely no match
7610 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7611 and then Is_Access_Type (Type_2))
7612 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7613 and then Is_Access_Type (Type_1)))
7616 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7618 May_Hide_Profile := True;
7623 end Conforming_Types;
7625 --------------------------
7626 -- Create_Extra_Formals --
7627 --------------------------
7629 procedure Create_Extra_Formals (E : Entity_Id) is
7630 First_Extra : Entity_Id := Empty;
7632 Last_Extra : Entity_Id := Empty;
7634 function Add_Extra_Formal
7635 (Assoc_Entity : Entity_Id;
7638 Suffix : String) return Entity_Id;
7639 -- Add an extra formal to the current list of formals and extra formals.
7640 -- The extra formal is added to the end of the list of extra formals,
7641 -- and also returned as the result. These formals are always of mode IN.
7642 -- The new formal has the type Typ, is declared in Scope, and its name
7643 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7644 -- The following suffixes are currently used. They should not be changed
7645 -- without coordinating with CodePeer, which makes use of these to
7646 -- provide better messages.
7648 -- O denotes the Constrained bit.
7649 -- L denotes the accessibility level.
7650 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7651 -- the full list in exp_ch6.BIP_Formal_Kind.
7653 ----------------------
7654 -- Add_Extra_Formal --
7655 ----------------------
7657 function Add_Extra_Formal
7658 (Assoc_Entity : Entity_Id;
7661 Suffix : String) return Entity_Id
7663 EF : constant Entity_Id :=
7664 Make_Defining_Identifier (Sloc (Assoc_Entity),
7665 Chars => New_External_Name (Chars (Assoc_Entity),
7669 -- A little optimization. Never generate an extra formal for the
7670 -- _init operand of an initialization procedure, since it could
7673 if Chars (Formal) = Name_uInit then
7677 Set_Ekind (EF, E_In_Parameter);
7678 Set_Actual_Subtype (EF, Typ);
7679 Set_Etype (EF, Typ);
7680 Set_Scope (EF, Scope);
7681 Set_Mechanism (EF, Default_Mechanism);
7682 Set_Formal_Validity (EF);
7684 if No (First_Extra) then
7686 Set_Extra_Formals (Scope, First_Extra);
7689 if Present (Last_Extra) then
7690 Set_Extra_Formal (Last_Extra, EF);
7696 end Add_Extra_Formal;
7700 Formal_Type : Entity_Id;
7701 P_Formal : Entity_Id := Empty;
7703 -- Start of processing for Create_Extra_Formals
7706 -- We never generate extra formals if expansion is not active because we
7707 -- don't need them unless we are generating code.
7709 if not Expander_Active then
7713 -- No need to generate extra formals in interface thunks whose target
7714 -- primitive has no extra formals.
7716 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7720 -- If this is a derived subprogram then the subtypes of the parent
7721 -- subprogram's formal parameters will be used to determine the need
7722 -- for extra formals.
7724 if Is_Overloadable (E) and then Present (Alias (E)) then
7725 P_Formal := First_Formal (Alias (E));
7728 Formal := First_Formal (E);
7729 while Present (Formal) loop
7730 Last_Extra := Formal;
7731 Next_Formal (Formal);
7734 -- If Extra_Formals were already created, don't do it again. This
7735 -- situation may arise for subprogram types created as part of
7736 -- dispatching calls (see Expand_Dispatching_Call)
7738 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7742 -- If the subprogram is a predefined dispatching subprogram then don't
7743 -- generate any extra constrained or accessibility level formals. In
7744 -- general we suppress these for internal subprograms (by not calling
7745 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7746 -- generated stream attributes do get passed through because extra
7747 -- build-in-place formals are needed in some cases (limited 'Input).
7749 if Is_Predefined_Internal_Operation (E) then
7750 goto Test_For_Func_Result_Extras;
7753 Formal := First_Formal (E);
7754 while Present (Formal) loop
7756 -- Create extra formal for supporting the attribute 'Constrained.
7757 -- The case of a private type view without discriminants also
7758 -- requires the extra formal if the underlying type has defaulted
7761 if Ekind (Formal) /= E_In_Parameter then
7762 if Present (P_Formal) then
7763 Formal_Type := Etype (P_Formal);
7765 Formal_Type := Etype (Formal);
7768 -- Do not produce extra formals for Unchecked_Union parameters.
7769 -- Jump directly to the end of the loop.
7771 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7772 goto Skip_Extra_Formal_Generation;
7775 if not Has_Discriminants (Formal_Type)
7776 and then Ekind (Formal_Type) in Private_Kind
7777 and then Present (Underlying_Type (Formal_Type))
7779 Formal_Type := Underlying_Type (Formal_Type);
7782 -- Suppress the extra formal if formal's subtype is constrained or
7783 -- indefinite, or we're compiling for Ada 2012 and the underlying
7784 -- type is tagged and limited. In Ada 2012, a limited tagged type
7785 -- can have defaulted discriminants, but 'Constrained is required
7786 -- to return True, so the formal is never needed (see AI05-0214).
7787 -- Note that this ensures consistency of calling sequences for
7788 -- dispatching operations when some types in a class have defaults
7789 -- on discriminants and others do not (and requiring the extra
7790 -- formal would introduce distributed overhead).
7792 -- If the type does not have a completion yet, treat as prior to
7793 -- Ada 2012 for consistency.
7795 if Has_Discriminants (Formal_Type)
7796 and then not Is_Constrained (Formal_Type)
7797 and then Is_Definite_Subtype (Formal_Type)
7798 and then (Ada_Version < Ada_2012
7799 or else No (Underlying_Type (Formal_Type))
7801 (Is_Limited_Type (Formal_Type)
7804 (Underlying_Type (Formal_Type)))))
7806 Set_Extra_Constrained
7807 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7811 -- Create extra formal for supporting accessibility checking. This
7812 -- is done for both anonymous access formals and formals of named
7813 -- access types that are marked as controlling formals. The latter
7814 -- case can occur when Expand_Dispatching_Call creates a subprogram
7815 -- type and substitutes the types of access-to-class-wide actuals
7816 -- for the anonymous access-to-specific-type of controlling formals.
7817 -- Base_Type is applied because in cases where there is a null
7818 -- exclusion the formal may have an access subtype.
7820 -- This is suppressed if we specifically suppress accessibility
7821 -- checks at the package level for either the subprogram, or the
7822 -- package in which it resides. However, we do not suppress it
7823 -- simply if the scope has accessibility checks suppressed, since
7824 -- this could cause trouble when clients are compiled with a
7825 -- different suppression setting. The explicit checks at the
7826 -- package level are safe from this point of view.
7828 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
7829 or else (Is_Controlling_Formal (Formal)
7830 and then Is_Access_Type (Base_Type (Etype (Formal)))))
7832 (Explicit_Suppress (E, Accessibility_Check)
7834 Explicit_Suppress (Scope (E), Accessibility_Check))
7837 or else Present (Extra_Accessibility (P_Formal)))
7839 Set_Extra_Accessibility
7840 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
7843 -- This label is required when skipping extra formal generation for
7844 -- Unchecked_Union parameters.
7846 <<Skip_Extra_Formal_Generation>>
7848 if Present (P_Formal) then
7849 Next_Formal (P_Formal);
7852 Next_Formal (Formal);
7855 <<Test_For_Func_Result_Extras>>
7857 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7858 -- function call is ... determined by the point of call ...".
7860 if Needs_Result_Accessibility_Level (E) then
7861 Set_Extra_Accessibility_Of_Result
7862 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7865 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
7866 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7868 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
7870 Result_Subt : constant Entity_Id := Etype (E);
7871 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
7872 Formal_Typ : Entity_Id;
7873 Subp_Decl : Node_Id;
7875 Discard : Entity_Id;
7876 pragma Warnings (Off, Discard);
7879 -- In the case of functions with unconstrained result subtypes,
7880 -- add a 4-state formal indicating whether the return object is
7881 -- allocated by the caller (1), or should be allocated by the
7882 -- callee on the secondary stack (2), in the global heap (3), or
7883 -- in a user-defined storage pool (4). For the moment we just use
7884 -- Natural for the type of this formal. Note that this formal
7885 -- isn't usually needed in the case where the result subtype is
7886 -- constrained, but it is needed when the function has a tagged
7887 -- result, because generally such functions can be called in a
7888 -- dispatching context and such calls must be handled like calls
7889 -- to a class-wide function.
7891 if Needs_BIP_Alloc_Form (E) then
7894 (E, Standard_Natural,
7895 E, BIP_Formal_Suffix (BIP_Alloc_Form));
7897 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
7898 -- use a user-defined pool. This formal is not added on
7899 -- ZFP as those targets do not support pools.
7901 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
7904 (E, RTE (RE_Root_Storage_Pool_Ptr),
7905 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7909 -- In the case of functions whose result type needs finalization,
7910 -- add an extra formal which represents the finalization master.
7912 if Needs_BIP_Finalization_Master (E) then
7915 (E, RTE (RE_Finalization_Master_Ptr),
7916 E, BIP_Formal_Suffix (BIP_Finalization_Master));
7919 -- When the result type contains tasks, add two extra formals: the
7920 -- master of the tasks to be created, and the caller's activation
7923 if Has_Task (Full_Subt) then
7926 (E, RTE (RE_Master_Id),
7927 E, BIP_Formal_Suffix (BIP_Task_Master));
7930 (E, RTE (RE_Activation_Chain_Access),
7931 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7934 -- All build-in-place functions get an extra formal that will be
7935 -- passed the address of the return object within the caller.
7938 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
7940 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7941 Set_Etype (Formal_Typ, Formal_Typ);
7942 Set_Depends_On_Private
7943 (Formal_Typ, Has_Private_Component (Formal_Typ));
7944 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7945 Set_Is_Access_Constant (Formal_Typ, False);
7947 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7948 -- the designated type comes from the limited view (for back-end
7951 Set_From_Limited_With
7952 (Formal_Typ, From_Limited_With (Result_Subt));
7954 Layout_Type (Formal_Typ);
7956 -- Force the definition of the Itype in case of internal function
7957 -- calls within the same or nested scope.
7959 if Is_Subprogram_Or_Generic_Subprogram (E) then
7960 Subp_Decl := Parent (E);
7962 -- The insertion point for an Itype reference should be after
7963 -- the unit declaration node of the subprogram. An exception
7964 -- to this are inherited operations from a parent type in which
7965 -- case the derived type acts as their parent.
7967 if Nkind_In (Subp_Decl, N_Function_Specification,
7968 N_Procedure_Specification)
7970 Subp_Decl := Parent (Subp_Decl);
7973 Build_Itype_Reference (Formal_Typ, Subp_Decl);
7978 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
7981 end Create_Extra_Formals;
7983 -----------------------------
7984 -- Enter_Overloaded_Entity --
7985 -----------------------------
7987 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7988 function Matches_Predefined_Op return Boolean;
7989 -- This returns an approximation of whether S matches a predefined
7990 -- operator, based on the operator symbol, and the parameter and result
7991 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
7993 ---------------------------
7994 -- Matches_Predefined_Op --
7995 ---------------------------
7997 function Matches_Predefined_Op return Boolean is
7998 Formal_1 : constant Entity_Id := First_Formal (S);
7999 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8000 Op : constant Name_Id := Chars (S);
8001 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8002 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8007 if Present (Formal_2) then
8009 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8012 -- All but "&" and "**" have same-types parameters
8021 if Type_1 /= Type_2 then
8026 -- Check parameter and result types
8034 Is_Boolean_Type (Result_Type)
8035 and then Result_Type = Type_1;
8041 Is_Integer_Type (Result_Type)
8042 and then Result_Type = Type_1;
8050 Is_Numeric_Type (Result_Type)
8051 and then Result_Type = Type_1;
8057 Is_Boolean_Type (Result_Type)
8058 and then not Is_Limited_Type (Type_1);
8066 Is_Boolean_Type (Result_Type)
8067 and then (Is_Array_Type (Type_1)
8068 or else Is_Scalar_Type (Type_1));
8070 when Name_Op_Concat =>
8071 return Is_Array_Type (Result_Type);
8073 when Name_Op_Expon =>
8075 (Is_Integer_Type (Result_Type)
8076 or else Is_Floating_Point_Type (Result_Type))
8077 and then Result_Type = Type_1
8078 and then Type_2 = Standard_Integer;
8081 raise Program_Error;
8094 Is_Numeric_Type (Result_Type)
8095 and then Result_Type = Type_1;
8099 Is_Boolean_Type (Result_Type)
8100 and then Result_Type = Type_1;
8103 raise Program_Error;
8106 end Matches_Predefined_Op;
8110 E : Entity_Id := Current_Entity_In_Scope (S);
8111 C_E : Entity_Id := Current_Entity (S);
8113 -- Start of processing for Enter_Overloaded_Entity
8117 Set_Has_Homonym (E);
8118 Set_Has_Homonym (S);
8121 Set_Is_Immediately_Visible (S);
8122 Set_Scope (S, Current_Scope);
8124 -- Chain new entity if front of homonym in current scope, so that
8125 -- homonyms are contiguous.
8127 if Present (E) and then E /= C_E then
8128 while Homonym (C_E) /= E loop
8129 C_E := Homonym (C_E);
8132 Set_Homonym (C_E, S);
8136 Set_Current_Entity (S);
8141 if Is_Inherited_Operation (S) then
8142 Append_Inherited_Subprogram (S);
8144 Append_Entity (S, Current_Scope);
8147 Set_Public_Status (S);
8149 if Debug_Flag_E then
8150 Write_Str ("New overloaded entity chain: ");
8151 Write_Name (Chars (S));
8154 while Present (E) loop
8155 Write_Str (" "); Write_Int (Int (E));
8162 -- Generate warning for hiding
8165 and then Comes_From_Source (S)
8166 and then In_Extended_Main_Source_Unit (S)
8173 -- Warn unless genuine overloading. Do not emit warning on
8174 -- hiding predefined operators in Standard (these are either an
8175 -- (artifact of our implicit declarations, or simple noise) but
8176 -- keep warning on a operator defined on a local subtype, because
8177 -- of the real danger that different operators may be applied in
8178 -- various parts of the program.
8180 -- Note that if E and S have the same scope, there is never any
8181 -- hiding. Either the two conflict, and the program is illegal,
8182 -- or S is overriding an implicit inherited subprogram.
8184 if Scope (E) /= Scope (S)
8185 and then (not Is_Overloadable (E)
8186 or else Subtype_Conformant (E, S))
8187 and then (Is_Immediately_Visible (E)
8188 or else Is_Potentially_Use_Visible (S))
8190 if Scope (E) = Standard_Standard then
8191 if Nkind (S) = N_Defining_Operator_Symbol
8192 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8194 and then Matches_Predefined_Op
8197 ("declaration of & hides predefined operator?h?", S);
8200 -- E not immediately within Standard
8203 Error_Msg_Sloc := Sloc (E);
8204 Error_Msg_N ("declaration of & hides one #?h?", S);
8209 end Enter_Overloaded_Entity;
8211 -----------------------------
8212 -- Check_Untagged_Equality --
8213 -----------------------------
8215 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8216 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8217 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8221 -- This check applies only if we have a subprogram declaration with an
8222 -- untagged record type.
8224 if Nkind (Decl) /= N_Subprogram_Declaration
8225 or else not Is_Record_Type (Typ)
8226 or else Is_Tagged_Type (Typ)
8231 -- In Ada 2012 case, we will output errors or warnings depending on
8232 -- the setting of debug flag -gnatd.E.
8234 if Ada_Version >= Ada_2012 then
8235 Error_Msg_Warn := Debug_Flag_Dot_EE;
8237 -- In earlier versions of Ada, nothing to do unless we are warning on
8238 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8241 if not Warn_On_Ada_2012_Compatibility then
8246 -- Cases where the type has already been frozen
8248 if Is_Frozen (Typ) then
8250 -- If the type is not declared in a package, or if we are in the body
8251 -- of the package or in some other scope, the new operation is not
8252 -- primitive, and therefore legal, though suspicious. Should we
8253 -- generate a warning in this case ???
8255 if Ekind (Scope (Typ)) /= E_Package
8256 or else Scope (Typ) /= Current_Scope
8260 -- If the type is a generic actual (sub)type, the operation is not
8261 -- primitive either because the base type is declared elsewhere.
8263 elsif Is_Generic_Actual_Type (Typ) then
8266 -- Here we have a definite error of declaration after freezing
8269 if Ada_Version >= Ada_2012 then
8271 ("equality operator must be declared before type & is "
8272 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8274 -- In Ada 2012 mode with error turned to warning, output one
8275 -- more warning to warn that the equality operation may not
8276 -- compose. This is the consequence of ignoring the error.
8278 if Error_Msg_Warn then
8279 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8284 ("equality operator must be declared before type& is "
8285 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8288 -- If we are in the package body, we could just move the
8289 -- declaration to the package spec, so add a message saying that.
8291 if In_Package_Body (Scope (Typ)) then
8292 if Ada_Version >= Ada_2012 then
8294 ("\move declaration to package spec<<", Eq_Op);
8297 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8300 -- Otherwise try to find the freezing point
8303 Obj_Decl := Next (Parent (Typ));
8304 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8305 if Nkind (Obj_Decl) = N_Object_Declaration
8306 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8308 -- Freezing point, output warnings
8310 if Ada_Version >= Ada_2012 then
8312 ("type& is frozen by declaration??", Obj_Decl, Typ);
8314 ("\an equality operator cannot be declared after "
8319 ("type& is frozen by declaration (Ada 2012)?y?",
8322 ("\an equality operator cannot be declared after "
8323 & "this point (Ada 2012)?y?",
8335 -- Here if type is not frozen yet. It is illegal to have a primitive
8336 -- equality declared in the private part if the type is visible.
8338 elsif not In_Same_List (Parent (Typ), Decl)
8339 and then not Is_Limited_Type (Typ)
8341 -- Shouldn't we give an RM reference here???
8343 if Ada_Version >= Ada_2012 then
8345 ("equality operator appears too late<<", Eq_Op);
8348 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8351 -- No error detected
8356 end Check_Untagged_Equality;
8358 -----------------------------
8359 -- Find_Corresponding_Spec --
8360 -----------------------------
8362 function Find_Corresponding_Spec
8364 Post_Error : Boolean := True) return Entity_Id
8366 Spec : constant Node_Id := Specification (N);
8367 Designator : constant Entity_Id := Defining_Entity (Spec);
8371 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8372 -- Even if fully conformant, a body may depend on a generic actual when
8373 -- the spec does not, or vice versa, in which case they were distinct
8374 -- entities in the generic.
8376 -------------------------------
8377 -- Different_Generic_Profile --
8378 -------------------------------
8380 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8383 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8384 -- Check that the types of corresponding formals have the same
8385 -- generic actual if any. We have to account for subtypes of a
8386 -- generic formal, declared between a spec and a body, which may
8387 -- appear distinct in an instance but matched in the generic, and
8388 -- the subtype may be used either in the spec or the body of the
8389 -- subprogram being checked.
8391 -------------------------
8392 -- Same_Generic_Actual --
8393 -------------------------
8395 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8397 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
8398 -- Predicate to check whether S1 is a subtype of S2 in the source
8401 -------------------------
8402 -- Is_Declared_Subtype --
8403 -------------------------
8405 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
8407 return Comes_From_Source (Parent (S1))
8408 and then Nkind (Parent (S1)) = N_Subtype_Declaration
8409 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
8410 and then Entity (Subtype_Indication (Parent (S1))) = S2;
8411 end Is_Declared_Subtype;
8413 -- Start of processing for Same_Generic_Actual
8416 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8417 or else Is_Declared_Subtype (T1, T2)
8418 or else Is_Declared_Subtype (T2, T1);
8419 end Same_Generic_Actual;
8421 -- Start of processing for Different_Generic_Profile
8424 if not In_Instance then
8427 elsif Ekind (E) = E_Function
8428 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8433 F1 := First_Formal (Designator);
8434 F2 := First_Formal (E);
8435 while Present (F1) loop
8436 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8445 end Different_Generic_Profile;
8447 -- Start of processing for Find_Corresponding_Spec
8450 E := Current_Entity (Designator);
8451 while Present (E) loop
8453 -- We are looking for a matching spec. It must have the same scope,
8454 -- and the same name, and either be type conformant, or be the case
8455 -- of a library procedure spec and its body (which belong to one
8456 -- another regardless of whether they are type conformant or not).
8458 if Scope (E) = Current_Scope then
8459 if Current_Scope = Standard_Standard
8460 or else (Ekind (E) = Ekind (Designator)
8461 and then Type_Conformant (E, Designator))
8463 -- Within an instantiation, we know that spec and body are
8464 -- subtype conformant, because they were subtype conformant in
8465 -- the generic. We choose the subtype-conformant entity here as
8466 -- well, to resolve spurious ambiguities in the instance that
8467 -- were not present in the generic (i.e. when two different
8468 -- types are given the same actual). If we are looking for a
8469 -- spec to match a body, full conformance is expected.
8473 -- Inherit the convention and "ghostness" of the matching
8474 -- spec to ensure proper full and subtype conformance.
8476 Set_Convention (Designator, Convention (E));
8478 -- Skip past subprogram bodies and subprogram renamings that
8479 -- may appear to have a matching spec, but that aren't fully
8480 -- conformant with it. That can occur in cases where an
8481 -- actual type causes unrelated homographs in the instance.
8483 if Nkind_In (N, N_Subprogram_Body,
8484 N_Subprogram_Renaming_Declaration)
8485 and then Present (Homonym (E))
8486 and then not Fully_Conformant (Designator, E)
8490 elsif not Subtype_Conformant (Designator, E) then
8493 elsif Different_Generic_Profile (E) then
8498 -- Ada 2012 (AI05-0165): For internally generated bodies of
8499 -- null procedures locate the internally generated spec. We
8500 -- enforce mode conformance since a tagged type may inherit
8501 -- from interfaces several null primitives which differ only
8502 -- in the mode of the formals.
8504 if not (Comes_From_Source (E))
8505 and then Is_Null_Procedure (E)
8506 and then not Mode_Conformant (Designator, E)
8510 -- For null procedures coming from source that are completions,
8511 -- analysis of the generated body will establish the link.
8513 elsif Comes_From_Source (E)
8514 and then Nkind (Spec) = N_Procedure_Specification
8515 and then Null_Present (Spec)
8519 -- Expression functions can be completions, but cannot be
8520 -- completed by an explicit body.
8522 elsif Comes_From_Source (E)
8523 and then Comes_From_Source (N)
8524 and then Nkind (N) = N_Subprogram_Body
8525 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
8526 N_Expression_Function
8528 Error_Msg_Sloc := Sloc (E);
8529 Error_Msg_N ("body conflicts with expression function#", N);
8532 elsif not Has_Completion (E) then
8533 if Nkind (N) /= N_Subprogram_Body_Stub then
8534 Set_Corresponding_Spec (N, E);
8537 Set_Has_Completion (E);
8540 elsif Nkind (Parent (N)) = N_Subunit then
8542 -- If this is the proper body of a subunit, the completion
8543 -- flag is set when analyzing the stub.
8547 -- If E is an internal function with a controlling result that
8548 -- was created for an operation inherited by a null extension,
8549 -- it may be overridden by a body without a previous spec (one
8550 -- more reason why these should be shunned). In that case we
8551 -- remove the generated body if present, because the current
8552 -- one is the explicit overriding.
8554 elsif Ekind (E) = E_Function
8555 and then Ada_Version >= Ada_2005
8556 and then not Comes_From_Source (E)
8557 and then Has_Controlling_Result (E)
8558 and then Is_Null_Extension (Etype (E))
8559 and then Comes_From_Source (Spec)
8561 Set_Has_Completion (E, False);
8564 and then Nkind (Parent (E)) = N_Function_Specification
8567 (Unit_Declaration_Node
8568 (Corresponding_Body (Unit_Declaration_Node (E))));
8572 -- If expansion is disabled, or if the wrapper function has
8573 -- not been generated yet, this a late body overriding an
8574 -- inherited operation, or it is an overriding by some other
8575 -- declaration before the controlling result is frozen. In
8576 -- either case this is a declaration of a new entity.
8582 -- If the body already exists, then this is an error unless
8583 -- the previous declaration is the implicit declaration of a
8584 -- derived subprogram. It is also legal for an instance to
8585 -- contain type conformant overloadable declarations (but the
8586 -- generic declaration may not), per 8.3(26/2).
8588 elsif No (Alias (E))
8589 and then not Is_Intrinsic_Subprogram (E)
8590 and then not In_Instance
8593 Error_Msg_Sloc := Sloc (E);
8595 if Is_Imported (E) then
8597 ("body not allowed for imported subprogram & declared#",
8600 Error_Msg_NE ("duplicate body for & declared#", N, E);
8604 -- Child units cannot be overloaded, so a conformance mismatch
8605 -- between body and a previous spec is an error.
8607 elsif Is_Child_Unit (E)
8609 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8611 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8616 ("body of child unit does not match previous declaration", N);
8624 -- On exit, we know that no previous declaration of subprogram exists
8627 end Find_Corresponding_Spec;
8629 ----------------------
8630 -- Fully_Conformant --
8631 ----------------------
8633 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8636 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8638 end Fully_Conformant;
8640 ----------------------------------
8641 -- Fully_Conformant_Expressions --
8642 ----------------------------------
8644 function Fully_Conformant_Expressions
8645 (Given_E1 : Node_Id;
8646 Given_E2 : Node_Id) return Boolean
8648 E1 : constant Node_Id := Original_Node (Given_E1);
8649 E2 : constant Node_Id := Original_Node (Given_E2);
8650 -- We always test conformance on original nodes, since it is possible
8651 -- for analysis and/or expansion to make things look as though they
8652 -- conform when they do not, e.g. by converting 1+2 into 3.
8654 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8655 renames Fully_Conformant_Expressions;
8657 function FCL (L1, L2 : List_Id) return Boolean;
8658 -- Compare elements of two lists for conformance. Elements have to be
8659 -- conformant, and actuals inserted as default parameters do not match
8660 -- explicit actuals with the same value.
8662 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8663 -- Compare an operator node with a function call
8669 function FCL (L1, L2 : List_Id) return Boolean is
8673 if L1 = No_List then
8679 if L2 = No_List then
8685 -- Compare two lists, skipping rewrite insertions (we want to compare
8686 -- the original trees, not the expanded versions).
8689 if Is_Rewrite_Insertion (N1) then
8691 elsif Is_Rewrite_Insertion (N2) then
8697 elsif not FCE (N1, N2) then
8710 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8711 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8716 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8721 Act := First (Actuals);
8723 if Nkind (Op_Node) in N_Binary_Op then
8724 if not FCE (Left_Opnd (Op_Node), Act) then
8731 return Present (Act)
8732 and then FCE (Right_Opnd (Op_Node), Act)
8733 and then No (Next (Act));
8737 -- Start of processing for Fully_Conformant_Expressions
8740 -- Nonconformant if paren count does not match. Note: if some idiot
8741 -- complains that we don't do this right for more than 3 levels of
8742 -- parentheses, they will be treated with the respect they deserve.
8744 if Paren_Count (E1) /= Paren_Count (E2) then
8747 -- If same entities are referenced, then they are conformant even if
8748 -- they have different forms (RM 8.3.1(19-20)).
8750 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8751 if Present (Entity (E1)) then
8752 return Entity (E1) = Entity (E2)
8754 -- One may be a discriminant that has been replaced by the
8755 -- corresponding discriminal.
8758 (Chars (Entity (E1)) = Chars (Entity (E2))
8759 and then Ekind (Entity (E1)) = E_Discriminant
8760 and then Ekind (Entity (E2)) = E_In_Parameter)
8762 -- The discriminant of a protected type is transformed into
8763 -- a local constant and then into a parameter of a protected
8767 (Ekind (Entity (E1)) = E_Constant
8768 and then Ekind (Entity (E2)) = E_In_Parameter
8769 and then Present (Discriminal_Link (Entity (E1)))
8770 and then Discriminal_Link (Entity (E1)) =
8771 Discriminal_Link (Entity (E2)))
8773 -- AI12-050: The loop variables of quantified expressions
8774 -- match if they have the same identifier, even though they
8775 -- are different entities.
8778 (Chars (Entity (E1)) = Chars (Entity (E2))
8779 and then Ekind (Entity (E1)) = E_Loop_Parameter
8780 and then Ekind (Entity (E2)) = E_Loop_Parameter);
8782 elsif Nkind (E1) = N_Expanded_Name
8783 and then Nkind (E2) = N_Expanded_Name
8784 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8785 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8787 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8790 -- Identifiers in component associations don't always have
8791 -- entities, but their names must conform.
8793 return Nkind (E1) = N_Identifier
8794 and then Nkind (E2) = N_Identifier
8795 and then Chars (E1) = Chars (E2);
8798 elsif Nkind (E1) = N_Character_Literal
8799 and then Nkind (E2) = N_Expanded_Name
8801 return Nkind (Selector_Name (E2)) = N_Character_Literal
8802 and then Chars (E1) = Chars (Selector_Name (E2));
8804 elsif Nkind (E2) = N_Character_Literal
8805 and then Nkind (E1) = N_Expanded_Name
8807 return Nkind (Selector_Name (E1)) = N_Character_Literal
8808 and then Chars (E2) = Chars (Selector_Name (E1));
8810 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8811 return FCO (E1, E2);
8813 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8814 return FCO (E2, E1);
8816 -- Otherwise we must have the same syntactic entity
8818 elsif Nkind (E1) /= Nkind (E2) then
8821 -- At this point, we specialize by node type
8827 FCL (Expressions (E1), Expressions (E2))
8829 FCL (Component_Associations (E1),
8830 Component_Associations (E2));
8833 if Nkind (Expression (E1)) = N_Qualified_Expression
8835 Nkind (Expression (E2)) = N_Qualified_Expression
8837 return FCE (Expression (E1), Expression (E2));
8839 -- Check that the subtype marks and any constraints
8844 Indic1 : constant Node_Id := Expression (E1);
8845 Indic2 : constant Node_Id := Expression (E2);
8850 if Nkind (Indic1) /= N_Subtype_Indication then
8852 Nkind (Indic2) /= N_Subtype_Indication
8853 and then Entity (Indic1) = Entity (Indic2);
8855 elsif Nkind (Indic2) /= N_Subtype_Indication then
8857 Nkind (Indic1) /= N_Subtype_Indication
8858 and then Entity (Indic1) = Entity (Indic2);
8861 if Entity (Subtype_Mark (Indic1)) /=
8862 Entity (Subtype_Mark (Indic2))
8867 Elt1 := First (Constraints (Constraint (Indic1)));
8868 Elt2 := First (Constraints (Constraint (Indic2)));
8869 while Present (Elt1) and then Present (Elt2) loop
8870 if not FCE (Elt1, Elt2) then
8883 when N_Attribute_Reference =>
8885 Attribute_Name (E1) = Attribute_Name (E2)
8886 and then FCL (Expressions (E1), Expressions (E2));
8890 Entity (E1) = Entity (E2)
8891 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8892 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8894 when N_Membership_Test
8898 FCE (Left_Opnd (E1), Left_Opnd (E2))
8900 FCE (Right_Opnd (E1), Right_Opnd (E2));
8902 when N_Case_Expression =>
8908 if not FCE (Expression (E1), Expression (E2)) then
8912 Alt1 := First (Alternatives (E1));
8913 Alt2 := First (Alternatives (E2));
8915 if Present (Alt1) /= Present (Alt2) then
8917 elsif No (Alt1) then
8921 if not FCE (Expression (Alt1), Expression (Alt2))
8922 or else not FCL (Discrete_Choices (Alt1),
8923 Discrete_Choices (Alt2))
8934 when N_Character_Literal =>
8936 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8938 when N_Component_Association =>
8940 FCL (Choices (E1), Choices (E2))
8942 FCE (Expression (E1), Expression (E2));
8944 when N_Explicit_Dereference =>
8946 FCE (Prefix (E1), Prefix (E2));
8948 when N_Extension_Aggregate =>
8950 FCL (Expressions (E1), Expressions (E2))
8951 and then Null_Record_Present (E1) =
8952 Null_Record_Present (E2)
8953 and then FCL (Component_Associations (E1),
8954 Component_Associations (E2));
8956 when N_Function_Call =>
8958 FCE (Name (E1), Name (E2))
8960 FCL (Parameter_Associations (E1),
8961 Parameter_Associations (E2));
8963 when N_If_Expression =>
8965 FCL (Expressions (E1), Expressions (E2));
8967 when N_Indexed_Component =>
8969 FCE (Prefix (E1), Prefix (E2))
8971 FCL (Expressions (E1), Expressions (E2));
8973 when N_Integer_Literal =>
8974 return (Intval (E1) = Intval (E2));
8979 when N_Operator_Symbol =>
8981 Chars (E1) = Chars (E2);
8983 when N_Others_Choice =>
8986 when N_Parameter_Association =>
8988 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8989 and then FCE (Explicit_Actual_Parameter (E1),
8990 Explicit_Actual_Parameter (E2));
8992 when N_Qualified_Expression
8994 | N_Unchecked_Type_Conversion
8997 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8999 FCE (Expression (E1), Expression (E2));
9001 when N_Quantified_Expression =>
9002 if not FCE (Condition (E1), Condition (E2)) then
9006 if Present (Loop_Parameter_Specification (E1))
9007 and then Present (Loop_Parameter_Specification (E2))
9010 L1 : constant Node_Id :=
9011 Loop_Parameter_Specification (E1);
9012 L2 : constant Node_Id :=
9013 Loop_Parameter_Specification (E2);
9017 Reverse_Present (L1) = Reverse_Present (L2)
9019 FCE (Defining_Identifier (L1),
9020 Defining_Identifier (L2))
9022 FCE (Discrete_Subtype_Definition (L1),
9023 Discrete_Subtype_Definition (L2));
9026 elsif Present (Iterator_Specification (E1))
9027 and then Present (Iterator_Specification (E2))
9030 I1 : constant Node_Id := Iterator_Specification (E1);
9031 I2 : constant Node_Id := Iterator_Specification (E2);
9035 FCE (Defining_Identifier (I1),
9036 Defining_Identifier (I2))
9038 Of_Present (I1) = Of_Present (I2)
9040 Reverse_Present (I1) = Reverse_Present (I2)
9041 and then FCE (Name (I1), Name (I2))
9042 and then FCE (Subtype_Indication (I1),
9043 Subtype_Indication (I2));
9046 -- The quantified expressions used different specifications to
9047 -- walk their respective ranges.
9055 FCE (Low_Bound (E1), Low_Bound (E2))
9057 FCE (High_Bound (E1), High_Bound (E2));
9059 when N_Real_Literal =>
9060 return (Realval (E1) = Realval (E2));
9062 when N_Selected_Component =>
9064 FCE (Prefix (E1), Prefix (E2))
9066 FCE (Selector_Name (E1), Selector_Name (E2));
9070 FCE (Prefix (E1), Prefix (E2))
9072 FCE (Discrete_Range (E1), Discrete_Range (E2));
9074 when N_String_Literal =>
9076 S1 : constant String_Id := Strval (E1);
9077 S2 : constant String_Id := Strval (E2);
9078 L1 : constant Nat := String_Length (S1);
9079 L2 : constant Nat := String_Length (S2);
9086 for J in 1 .. L1 loop
9087 if Get_String_Char (S1, J) /=
9088 Get_String_Char (S2, J)
9100 Entity (E1) = Entity (E2)
9102 FCE (Right_Opnd (E1), Right_Opnd (E2));
9104 -- All other node types cannot appear in this context. Strictly
9105 -- we should raise a fatal internal error. Instead we just ignore
9106 -- the nodes. This means that if anyone makes a mistake in the
9107 -- expander and mucks an expression tree irretrievably, the result
9108 -- will be a failure to detect a (probably very obscure) case
9109 -- of non-conformance, which is better than bombing on some
9110 -- case where two expressions do in fact conform.
9116 end Fully_Conformant_Expressions;
9118 ----------------------------------------
9119 -- Fully_Conformant_Discrete_Subtypes --
9120 ----------------------------------------
9122 function Fully_Conformant_Discrete_Subtypes
9123 (Given_S1 : Node_Id;
9124 Given_S2 : Node_Id) return Boolean
9126 S1 : constant Node_Id := Original_Node (Given_S1);
9127 S2 : constant Node_Id := Original_Node (Given_S2);
9129 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9130 -- Special-case for a bound given by a discriminant, which in the body
9131 -- is replaced with the discriminal of the enclosing type.
9133 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9134 -- Check both bounds
9136 -----------------------
9137 -- Conforming_Bounds --
9138 -----------------------
9140 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9142 if Is_Entity_Name (B1)
9143 and then Is_Entity_Name (B2)
9144 and then Ekind (Entity (B1)) = E_Discriminant
9146 return Chars (B1) = Chars (B2);
9149 return Fully_Conformant_Expressions (B1, B2);
9151 end Conforming_Bounds;
9153 -----------------------
9154 -- Conforming_Ranges --
9155 -----------------------
9157 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9160 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9162 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9163 end Conforming_Ranges;
9165 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9168 if Nkind (S1) /= Nkind (S2) then
9171 elsif Is_Entity_Name (S1) then
9172 return Entity (S1) = Entity (S2);
9174 elsif Nkind (S1) = N_Range then
9175 return Conforming_Ranges (S1, S2);
9177 elsif Nkind (S1) = N_Subtype_Indication then
9179 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9182 (Range_Expression (Constraint (S1)),
9183 Range_Expression (Constraint (S2)));
9187 end Fully_Conformant_Discrete_Subtypes;
9189 --------------------
9190 -- Install_Entity --
9191 --------------------
9193 procedure Install_Entity (E : Entity_Id) is
9194 Prev : constant Entity_Id := Current_Entity (E);
9196 Set_Is_Immediately_Visible (E);
9197 Set_Current_Entity (E);
9198 Set_Homonym (E, Prev);
9201 ---------------------
9202 -- Install_Formals --
9203 ---------------------
9205 procedure Install_Formals (Id : Entity_Id) is
9208 F := First_Formal (Id);
9209 while Present (F) loop
9213 end Install_Formals;
9215 -----------------------------
9216 -- Is_Interface_Conformant --
9217 -----------------------------
9219 function Is_Interface_Conformant
9220 (Tagged_Type : Entity_Id;
9221 Iface_Prim : Entity_Id;
9222 Prim : Entity_Id) return Boolean
9224 -- The operation may in fact be an inherited (implicit) operation
9225 -- rather than the original interface primitive, so retrieve the
9226 -- ultimate ancestor.
9228 Iface : constant Entity_Id :=
9229 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9230 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9232 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9233 -- Return the controlling formal of Prim
9235 ------------------------
9236 -- Controlling_Formal --
9237 ------------------------
9239 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9243 E := First_Entity (Prim);
9244 while Present (E) loop
9245 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9253 end Controlling_Formal;
9257 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9258 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9260 -- Start of processing for Is_Interface_Conformant
9263 pragma Assert (Is_Subprogram (Iface_Prim)
9264 and then Is_Subprogram (Prim)
9265 and then Is_Dispatching_Operation (Iface_Prim)
9266 and then Is_Dispatching_Operation (Prim));
9268 pragma Assert (Is_Interface (Iface)
9269 or else (Present (Alias (Iface_Prim))
9272 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9274 if Prim = Iface_Prim
9275 or else not Is_Subprogram (Prim)
9276 or else Ekind (Prim) /= Ekind (Iface_Prim)
9277 or else not Is_Dispatching_Operation (Prim)
9278 or else Scope (Prim) /= Scope (Tagged_Type)
9280 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9281 or else not Primitive_Names_Match (Iface_Prim, Prim)
9285 -- The mode of the controlling formals must match
9287 elsif Present (Iface_Ctrl_F)
9288 and then Present (Prim_Ctrl_F)
9289 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9293 -- Case of a procedure, or a function whose result type matches the
9294 -- result type of the interface primitive, or a function that has no
9295 -- controlling result (I or access I).
9297 elsif Ekind (Iface_Prim) = E_Procedure
9298 or else Etype (Prim) = Etype (Iface_Prim)
9299 or else not Has_Controlling_Result (Prim)
9301 return Type_Conformant
9302 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9304 -- Case of a function returning an interface, or an access to one. Check
9305 -- that the return types correspond.
9307 elsif Implements_Interface (Typ, Iface) then
9308 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9310 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9315 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9316 Skip_Controlling_Formals => True);
9322 end Is_Interface_Conformant;
9324 ---------------------------------
9325 -- Is_Non_Overriding_Operation --
9326 ---------------------------------
9328 function Is_Non_Overriding_Operation
9329 (Prev_E : Entity_Id;
9330 New_E : Entity_Id) return Boolean
9334 G_Typ : Entity_Id := Empty;
9336 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9337 -- If F_Type is a derived type associated with a generic actual subtype,
9338 -- then return its Generic_Parent_Type attribute, else return Empty.
9340 function Types_Correspond
9341 (P_Type : Entity_Id;
9342 N_Type : Entity_Id) return Boolean;
9343 -- Returns true if and only if the types (or designated types in the
9344 -- case of anonymous access types) are the same or N_Type is derived
9345 -- directly or indirectly from P_Type.
9347 -----------------------------
9348 -- Get_Generic_Parent_Type --
9349 -----------------------------
9351 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9357 if Is_Derived_Type (F_Typ)
9358 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9360 -- The tree must be traversed to determine the parent subtype in
9361 -- the generic unit, which unfortunately isn't always available
9362 -- via semantic attributes. ??? (Note: The use of Original_Node
9363 -- is needed for cases where a full derived type has been
9366 -- If the parent type is a scalar type, the derivation creates
9367 -- an anonymous base type for it, and the source type is its
9370 if Is_Scalar_Type (F_Typ)
9371 and then not Comes_From_Source (F_Typ)
9375 (Original_Node (Parent (First_Subtype (F_Typ))));
9377 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9379 if Nkind (Defn) = N_Derived_Type_Definition then
9380 Indic := Subtype_Indication (Defn);
9382 if Nkind (Indic) = N_Subtype_Indication then
9383 G_Typ := Entity (Subtype_Mark (Indic));
9385 G_Typ := Entity (Indic);
9388 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9389 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9391 return Generic_Parent_Type (Parent (G_Typ));
9397 end Get_Generic_Parent_Type;
9399 ----------------------
9400 -- Types_Correspond --
9401 ----------------------
9403 function Types_Correspond
9404 (P_Type : Entity_Id;
9405 N_Type : Entity_Id) return Boolean
9407 Prev_Type : Entity_Id := Base_Type (P_Type);
9408 New_Type : Entity_Id := Base_Type (N_Type);
9411 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9412 Prev_Type := Designated_Type (Prev_Type);
9415 if Ekind (New_Type) = E_Anonymous_Access_Type then
9416 New_Type := Designated_Type (New_Type);
9419 if Prev_Type = New_Type then
9422 elsif not Is_Class_Wide_Type (New_Type) then
9423 while Etype (New_Type) /= New_Type loop
9424 New_Type := Etype (New_Type);
9426 if New_Type = Prev_Type then
9432 end Types_Correspond;
9434 -- Start of processing for Is_Non_Overriding_Operation
9437 -- In the case where both operations are implicit derived subprograms
9438 -- then neither overrides the other. This can only occur in certain
9439 -- obscure cases (e.g., derivation from homographs created in a generic
9442 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9445 elsif Ekind (Current_Scope) = E_Package
9446 and then Is_Generic_Instance (Current_Scope)
9447 and then In_Private_Part (Current_Scope)
9448 and then Comes_From_Source (New_E)
9450 -- We examine the formals and result type of the inherited operation,
9451 -- to determine whether their type is derived from (the instance of)
9452 -- a generic type. The first such formal or result type is the one
9455 Formal := First_Formal (Prev_E);
9457 while Present (Formal) loop
9458 F_Typ := Base_Type (Etype (Formal));
9460 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9461 F_Typ := Designated_Type (F_Typ);
9464 G_Typ := Get_Generic_Parent_Type (F_Typ);
9465 exit when Present (G_Typ);
9467 Next_Formal (Formal);
9470 -- If the function dispatches on result check the result type
9472 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9473 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9480 -- If the generic type is a private type, then the original operation
9481 -- was not overriding in the generic, because there was no primitive
9482 -- operation to override.
9484 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9485 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9486 N_Formal_Private_Type_Definition
9490 -- The generic parent type is the ancestor of a formal derived
9491 -- type declaration. We need to check whether it has a primitive
9492 -- operation that should be overridden by New_E in the generic.
9496 P_Formal : Entity_Id;
9497 N_Formal : Entity_Id;
9501 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9504 while Present (Prim_Elt) loop
9505 P_Prim := Node (Prim_Elt);
9507 if Chars (P_Prim) = Chars (New_E)
9508 and then Ekind (P_Prim) = Ekind (New_E)
9510 P_Formal := First_Formal (P_Prim);
9511 N_Formal := First_Formal (New_E);
9512 while Present (P_Formal) and then Present (N_Formal) loop
9513 P_Typ := Etype (P_Formal);
9514 N_Typ := Etype (N_Formal);
9516 if not Types_Correspond (P_Typ, N_Typ) then
9520 Next_Entity (P_Formal);
9521 Next_Entity (N_Formal);
9524 -- Found a matching primitive operation belonging to the
9525 -- formal ancestor type, so the new subprogram is
9529 and then No (N_Formal)
9530 and then (Ekind (New_E) /= E_Function
9533 (Etype (P_Prim), Etype (New_E)))
9539 Next_Elmt (Prim_Elt);
9542 -- If no match found, then the new subprogram does not override
9543 -- in the generic (nor in the instance).
9545 -- If the type in question is not abstract, and the subprogram
9546 -- is, this will be an error if the new operation is in the
9547 -- private part of the instance. Emit a warning now, which will
9548 -- make the subsequent error message easier to understand.
9550 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
9551 and then Is_Abstract_Subprogram (Prev_E)
9552 and then In_Private_Part (Current_Scope)
9554 Error_Msg_Node_2 := F_Typ;
9556 ("private operation& in generic unit does not override "
9557 & "any primitive operation of& (RM 12.3 (18))??",
9567 end Is_Non_Overriding_Operation;
9569 -------------------------------------
9570 -- List_Inherited_Pre_Post_Aspects --
9571 -------------------------------------
9573 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9575 if Opt.List_Inherited_Aspects
9576 and then Is_Subprogram_Or_Generic_Subprogram (E)
9579 Subps : constant Subprogram_List := Inherited_Subprograms (E);
9584 for Index in Subps'Range loop
9585 Items := Contract (Subps (Index));
9587 if Present (Items) then
9588 Prag := Pre_Post_Conditions (Items);
9589 while Present (Prag) loop
9590 Error_Msg_Sloc := Sloc (Prag);
9592 if Class_Present (Prag)
9593 and then not Split_PPC (Prag)
9595 if Pragma_Name (Prag) = Name_Precondition then
9597 ("info: & inherits `Pre''Class` aspect from "
9601 ("info: & inherits `Post''Class` aspect from "
9606 Prag := Next_Pragma (Prag);
9612 end List_Inherited_Pre_Post_Aspects;
9614 ------------------------------
9615 -- Make_Inequality_Operator --
9616 ------------------------------
9618 -- S is the defining identifier of an equality operator. We build a
9619 -- subprogram declaration with the right signature. This operation is
9620 -- intrinsic, because it is always expanded as the negation of the
9621 -- call to the equality function.
9623 procedure Make_Inequality_Operator (S : Entity_Id) is
9624 Loc : constant Source_Ptr := Sloc (S);
9627 Op_Name : Entity_Id;
9629 FF : constant Entity_Id := First_Formal (S);
9630 NF : constant Entity_Id := Next_Formal (FF);
9633 -- Check that equality was properly defined, ignore call if not
9640 A : constant Entity_Id :=
9641 Make_Defining_Identifier (Sloc (FF),
9642 Chars => Chars (FF));
9644 B : constant Entity_Id :=
9645 Make_Defining_Identifier (Sloc (NF),
9646 Chars => Chars (NF));
9649 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9651 Formals := New_List (
9652 Make_Parameter_Specification (Loc,
9653 Defining_Identifier => A,
9655 New_Occurrence_Of (Etype (First_Formal (S)),
9656 Sloc (Etype (First_Formal (S))))),
9658 Make_Parameter_Specification (Loc,
9659 Defining_Identifier => B,
9661 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9662 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9665 Make_Subprogram_Declaration (Loc,
9667 Make_Function_Specification (Loc,
9668 Defining_Unit_Name => Op_Name,
9669 Parameter_Specifications => Formals,
9670 Result_Definition =>
9671 New_Occurrence_Of (Standard_Boolean, Loc)));
9673 -- Insert inequality right after equality if it is explicit or after
9674 -- the derived type when implicit. These entities are created only
9675 -- for visibility purposes, and eventually replaced in the course
9676 -- of expansion, so they do not need to be attached to the tree and
9677 -- seen by the back-end. Keeping them internal also avoids spurious
9678 -- freezing problems. The declaration is inserted in the tree for
9679 -- analysis, and removed afterwards. If the equality operator comes
9680 -- from an explicit declaration, attach the inequality immediately
9681 -- after. Else the equality is inherited from a derived type
9682 -- declaration, so insert inequality after that declaration.
9684 if No (Alias (S)) then
9685 Insert_After (Unit_Declaration_Node (S), Decl);
9686 elsif Is_List_Member (Parent (S)) then
9687 Insert_After (Parent (S), Decl);
9689 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9692 Mark_Rewrite_Insertion (Decl);
9693 Set_Is_Intrinsic_Subprogram (Op_Name);
9696 Set_Has_Completion (Op_Name);
9697 Set_Corresponding_Equality (Op_Name, S);
9698 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9700 end Make_Inequality_Operator;
9702 ----------------------
9703 -- May_Need_Actuals --
9704 ----------------------
9706 procedure May_Need_Actuals (Fun : Entity_Id) is
9711 F := First_Formal (Fun);
9713 while Present (F) loop
9714 if No (Default_Value (F)) then
9722 Set_Needs_No_Actuals (Fun, B);
9723 end May_Need_Actuals;
9725 ---------------------
9726 -- Mode_Conformant --
9727 ---------------------
9729 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9732 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9734 end Mode_Conformant;
9736 ---------------------------
9737 -- New_Overloaded_Entity --
9738 ---------------------------
9740 procedure New_Overloaded_Entity
9742 Derived_Type : Entity_Id := Empty)
9744 Overridden_Subp : Entity_Id := Empty;
9745 -- Set if the current scope has an operation that is type-conformant
9746 -- with S, and becomes hidden by S.
9748 Is_Primitive_Subp : Boolean;
9749 -- Set to True if the new subprogram is primitive
9752 -- Entity that S overrides
9754 Prev_Vis : Entity_Id := Empty;
9755 -- Predecessor of E in Homonym chain
9757 procedure Check_For_Primitive_Subprogram
9758 (Is_Primitive : out Boolean;
9759 Is_Overriding : Boolean := False);
9760 -- If the subprogram being analyzed is a primitive operation of the type
9761 -- of a formal or result, set the Has_Primitive_Operations flag on the
9762 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9763 -- corresponding flag on the entity itself for later use.
9765 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
9766 -- True if a) E is a subprogram whose first formal is a concurrent type
9767 -- defined in the scope of E that has some entry or subprogram whose
9768 -- profile matches E, or b) E is an internally built dispatching
9769 -- subprogram of a protected type and there is a matching subprogram
9770 -- defined in the enclosing scope of the protected type, or c) E is
9771 -- an entry of a synchronized type and a matching procedure has been
9772 -- previously defined in the enclosing scope of the synchronized type.
9774 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9775 -- Check that E is declared in the private part of the current package,
9776 -- or in the package body, where it may hide a previous declaration.
9777 -- We can't use In_Private_Part by itself because this flag is also
9778 -- set when freezing entities, so we must examine the place of the
9779 -- declaration in the tree, and recognize wrapper packages as well.
9781 function Is_Overriding_Alias
9783 New_E : Entity_Id) return Boolean;
9784 -- Check whether new subprogram and old subprogram are both inherited
9785 -- from subprograms that have distinct dispatch table entries. This can
9786 -- occur with derivations from instances with accidental homonyms. The
9787 -- function is conservative given that the converse is only true within
9788 -- instances that contain accidental overloadings.
9790 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
9791 -- Report conflict between entities S and E
9793 ------------------------------------
9794 -- Check_For_Primitive_Subprogram --
9795 ------------------------------------
9797 procedure Check_For_Primitive_Subprogram
9798 (Is_Primitive : out Boolean;
9799 Is_Overriding : Boolean := False)
9805 function Visible_Part_Type (T : Entity_Id) return Boolean;
9806 -- Returns true if T is declared in the visible part of the current
9807 -- package scope; otherwise returns false. Assumes that T is declared
9810 procedure Check_Private_Overriding (T : Entity_Id);
9811 -- Checks that if a primitive abstract subprogram of a visible
9812 -- abstract type is declared in a private part, then it must override
9813 -- an abstract subprogram declared in the visible part. Also checks
9814 -- that if a primitive function with a controlling result is declared
9815 -- in a private part, then it must override a function declared in
9816 -- the visible part.
9818 ------------------------------
9819 -- Check_Private_Overriding --
9820 ------------------------------
9822 procedure Check_Private_Overriding (T : Entity_Id) is
9823 function Overrides_Private_Part_Op return Boolean;
9824 -- This detects the special case where the overriding subprogram
9825 -- is overriding a subprogram that was declared in the same
9826 -- private part. That case is illegal by 3.9.3(10).
9828 function Overrides_Visible_Function
9829 (Partial_View : Entity_Id) return Boolean;
9830 -- True if S overrides a function in the visible part. The
9831 -- overridden function could be explicitly or implicitly declared.
9833 -------------------------------
9834 -- Overrides_Private_Part_Op --
9835 -------------------------------
9837 function Overrides_Private_Part_Op return Boolean is
9838 Over_Decl : constant Node_Id :=
9839 Unit_Declaration_Node (Overridden_Operation (S));
9840 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
9843 pragma Assert (Is_Overriding);
9845 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
9847 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
9849 return In_Same_List (Over_Decl, Subp_Decl);
9850 end Overrides_Private_Part_Op;
9852 --------------------------------
9853 -- Overrides_Visible_Function --
9854 --------------------------------
9856 function Overrides_Visible_Function
9857 (Partial_View : Entity_Id) return Boolean
9860 if not Is_Overriding or else not Has_Homonym (S) then
9864 if not Present (Partial_View) then
9868 -- Search through all the homonyms H of S in the current
9869 -- package spec, and return True if we find one that matches.
9870 -- Note that Parent (H) will be the declaration of the
9871 -- partial view of T for a match.
9878 exit when not Present (H) or else Scope (H) /= Scope (S);
9882 N_Private_Extension_Declaration,
9883 N_Private_Type_Declaration)
9884 and then Defining_Identifier (Parent (H)) = Partial_View
9892 end Overrides_Visible_Function;
9894 -- Start of processing for Check_Private_Overriding
9897 if Is_Package_Or_Generic_Package (Current_Scope)
9898 and then In_Private_Part (Current_Scope)
9899 and then Visible_Part_Type (T)
9900 and then not In_Instance
9902 if Is_Abstract_Type (T)
9903 and then Is_Abstract_Subprogram (S)
9904 and then (not Is_Overriding
9905 or else not Is_Abstract_Subprogram (E)
9906 or else Overrides_Private_Part_Op)
9909 ("abstract subprograms must be visible (RM 3.9.3(10))!",
9912 elsif Ekind (S) = E_Function then
9914 Partial_View : constant Entity_Id :=
9915 Incomplete_Or_Partial_View (T);
9918 if not Overrides_Visible_Function (Partial_View) then
9920 -- Here, S is "function ... return T;" declared in
9921 -- the private part, not overriding some visible
9922 -- operation. That's illegal in the tagged case
9923 -- (but not if the private type is untagged).
9925 if ((Present (Partial_View)
9926 and then Is_Tagged_Type (Partial_View))
9927 or else (not Present (Partial_View)
9928 and then Is_Tagged_Type (T)))
9929 and then T = Base_Type (Etype (S))
9932 ("private function with tagged result must"
9933 & " override visible-part function", S);
9935 ("\move subprogram to the visible part"
9936 & " (RM 3.9.3(10))", S);
9938 -- AI05-0073: extend this test to the case of a
9939 -- function with a controlling access result.
9941 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9942 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9944 not Is_Class_Wide_Type
9945 (Designated_Type (Etype (S)))
9946 and then Ada_Version >= Ada_2012
9949 ("private function with controlling access "
9950 & "result must override visible-part function",
9953 ("\move subprogram to the visible part"
9954 & " (RM 3.9.3(10))", S);
9960 end Check_Private_Overriding;
9962 -----------------------
9963 -- Visible_Part_Type --
9964 -----------------------
9966 function Visible_Part_Type (T : Entity_Id) return Boolean is
9967 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9971 -- If the entity is a private type, then it must be declared in a
9974 if Ekind (T) in Private_Kind then
9978 -- Otherwise, we traverse the visible part looking for its
9979 -- corresponding declaration. We cannot use the declaration
9980 -- node directly because in the private part the entity of a
9981 -- private type is the one in the full view, which does not
9982 -- indicate that it is the completion of something visible.
9984 N := First (Visible_Declarations (Specification (P)));
9985 while Present (N) loop
9986 if Nkind (N) = N_Full_Type_Declaration
9987 and then Present (Defining_Identifier (N))
9988 and then T = Defining_Identifier (N)
9992 elsif Nkind_In (N, N_Private_Type_Declaration,
9993 N_Private_Extension_Declaration)
9994 and then Present (Defining_Identifier (N))
9995 and then T = Full_View (Defining_Identifier (N))
10004 end Visible_Part_Type;
10006 -- Start of processing for Check_For_Primitive_Subprogram
10009 Is_Primitive := False;
10011 if not Comes_From_Source (S) then
10014 -- If subprogram is at library level, it is not primitive operation
10016 elsif Current_Scope = Standard_Standard then
10019 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10020 and then not In_Package_Body (Current_Scope))
10021 or else Is_Overriding
10023 -- For function, check return type
10025 if Ekind (S) = E_Function then
10026 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10027 F_Typ := Designated_Type (Etype (S));
10029 F_Typ := Etype (S);
10032 B_Typ := Base_Type (F_Typ);
10034 if Scope (B_Typ) = Current_Scope
10035 and then not Is_Class_Wide_Type (B_Typ)
10036 and then not Is_Generic_Type (B_Typ)
10038 Is_Primitive := True;
10039 Set_Has_Primitive_Operations (B_Typ);
10040 Set_Is_Primitive (S);
10041 Check_Private_Overriding (B_Typ);
10043 -- The Ghost policy in effect at the point of declaration
10044 -- or a tagged type and a primitive operation must match
10045 -- (SPARK RM 6.9(16)).
10047 Check_Ghost_Primitive (S, B_Typ);
10051 -- For all subprograms, check formals
10053 Formal := First_Formal (S);
10054 while Present (Formal) loop
10055 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10056 F_Typ := Designated_Type (Etype (Formal));
10058 F_Typ := Etype (Formal);
10061 B_Typ := Base_Type (F_Typ);
10063 if Ekind (B_Typ) = E_Access_Subtype then
10064 B_Typ := Base_Type (B_Typ);
10067 if Scope (B_Typ) = Current_Scope
10068 and then not Is_Class_Wide_Type (B_Typ)
10069 and then not Is_Generic_Type (B_Typ)
10071 Is_Primitive := True;
10072 Set_Is_Primitive (S);
10073 Set_Has_Primitive_Operations (B_Typ);
10074 Check_Private_Overriding (B_Typ);
10076 -- The Ghost policy in effect at the point of declaration
10077 -- of a tagged type and a primitive operation must match
10078 -- (SPARK RM 6.9(16)).
10080 Check_Ghost_Primitive (S, B_Typ);
10083 Next_Formal (Formal);
10086 -- Special case: An equality function can be redefined for a type
10087 -- occurring in a declarative part, and won't otherwise be treated as
10088 -- a primitive because it doesn't occur in a package spec and doesn't
10089 -- override an inherited subprogram. It's important that we mark it
10090 -- primitive so it can be returned by Collect_Primitive_Operations
10091 -- and be used in composing the equality operation of later types
10092 -- that have a component of the type.
10094 elsif Chars (S) = Name_Op_Eq
10095 and then Etype (S) = Standard_Boolean
10097 B_Typ := Base_Type (Etype (First_Formal (S)));
10099 if Scope (B_Typ) = Current_Scope
10101 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10102 and then not Is_Limited_Type (B_Typ)
10104 Is_Primitive := True;
10105 Set_Is_Primitive (S);
10106 Set_Has_Primitive_Operations (B_Typ);
10107 Check_Private_Overriding (B_Typ);
10109 -- The Ghost policy in effect at the point of declaration of a
10110 -- tagged type and a primitive operation must match
10111 -- (SPARK RM 6.9(16)).
10113 Check_Ghost_Primitive (S, B_Typ);
10116 end Check_For_Primitive_Subprogram;
10118 --------------------------------------
10119 -- Has_Matching_Entry_Or_Subprogram --
10120 --------------------------------------
10122 function Has_Matching_Entry_Or_Subprogram
10123 (E : Entity_Id) return Boolean
10125 function Check_Conforming_Parameters
10126 (E1_Param : Node_Id;
10127 E2_Param : Node_Id) return Boolean;
10128 -- Starting from the given parameters, check that all the parameters
10129 -- of two entries or subprograms are subtype conformant. Used to skip
10130 -- the check on the controlling argument.
10132 function Matching_Entry_Or_Subprogram
10133 (Conc_Typ : Entity_Id;
10134 Subp : Entity_Id) return Entity_Id;
10135 -- Return the first entry or subprogram of the given concurrent type
10136 -- whose name matches the name of Subp and has a profile conformant
10137 -- with Subp; return Empty if not found.
10139 function Matching_Dispatching_Subprogram
10140 (Conc_Typ : Entity_Id;
10141 Ent : Entity_Id) return Entity_Id;
10142 -- Return the first dispatching primitive of Conc_Type defined in the
10143 -- enclosing scope of Conc_Type (i.e. before the full definition of
10144 -- this concurrent type) whose name matches the entry Ent and has a
10145 -- profile conformant with the profile of the corresponding (not yet
10146 -- built) dispatching primitive of Ent; return Empty if not found.
10148 function Matching_Original_Protected_Subprogram
10149 (Prot_Typ : Entity_Id;
10150 Subp : Entity_Id) return Entity_Id;
10151 -- Return the first subprogram defined in the enclosing scope of
10152 -- Prot_Typ (before the full definition of this protected type)
10153 -- whose name matches the original name of Subp and has a profile
10154 -- conformant with the profile of Subp; return Empty if not found.
10156 ---------------------------------
10157 -- Check_Confirming_Parameters --
10158 ---------------------------------
10160 function Check_Conforming_Parameters
10161 (E1_Param : Node_Id;
10162 E2_Param : Node_Id) return Boolean
10164 Param_E1 : Node_Id := E1_Param;
10165 Param_E2 : Node_Id := E2_Param;
10168 while Present (Param_E1) and then Present (Param_E2) loop
10169 if Ekind (Defining_Identifier (Param_E1)) /=
10170 Ekind (Defining_Identifier (Param_E2))
10173 (Find_Parameter_Type (Param_E1),
10174 Find_Parameter_Type (Param_E2),
10175 Subtype_Conformant)
10184 -- The candidate is not valid if one of the two lists contains
10185 -- more parameters than the other
10187 return No (Param_E1) and then No (Param_E2);
10188 end Check_Conforming_Parameters;
10190 ----------------------------------
10191 -- Matching_Entry_Or_Subprogram --
10192 ----------------------------------
10194 function Matching_Entry_Or_Subprogram
10195 (Conc_Typ : Entity_Id;
10196 Subp : Entity_Id) return Entity_Id
10201 E := First_Entity (Conc_Typ);
10202 while Present (E) loop
10203 if Chars (Subp) = Chars (E)
10204 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10206 Check_Conforming_Parameters
10207 (First (Parameter_Specifications (Parent (E))),
10208 Next (First (Parameter_Specifications (Parent (Subp)))))
10217 end Matching_Entry_Or_Subprogram;
10219 -------------------------------------
10220 -- Matching_Dispatching_Subprogram --
10221 -------------------------------------
10223 function Matching_Dispatching_Subprogram
10224 (Conc_Typ : Entity_Id;
10225 Ent : Entity_Id) return Entity_Id
10230 -- Search for entities in the enclosing scope of this synchonized
10233 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10234 Push_Scope (Scope (Conc_Typ));
10235 E := Current_Entity_In_Scope (Ent);
10238 while Present (E) loop
10239 if Scope (E) = Scope (Conc_Typ)
10240 and then Comes_From_Source (E)
10241 and then Ekind (E) = E_Procedure
10242 and then Present (First_Entity (E))
10243 and then Is_Controlling_Formal (First_Entity (E))
10244 and then Etype (First_Entity (E)) = Conc_Typ
10246 Check_Conforming_Parameters
10247 (First (Parameter_Specifications (Parent (Ent))),
10248 Next (First (Parameter_Specifications (Parent (E)))))
10257 end Matching_Dispatching_Subprogram;
10259 --------------------------------------------
10260 -- Matching_Original_Protected_Subprogram --
10261 --------------------------------------------
10263 function Matching_Original_Protected_Subprogram
10264 (Prot_Typ : Entity_Id;
10265 Subp : Entity_Id) return Entity_Id
10267 ICF : constant Boolean :=
10268 Is_Controlling_Formal (First_Entity (Subp));
10272 -- Temporarily decorate the first parameter of Subp as controlling
10273 -- formal, required to invoke Subtype_Conformant.
10275 Set_Is_Controlling_Formal (First_Entity (Subp));
10278 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10280 while Present (E) loop
10281 if Scope (E) = Scope (Prot_Typ)
10282 and then Comes_From_Source (E)
10283 and then Ekind (Subp) = Ekind (E)
10284 and then Present (First_Entity (E))
10285 and then Is_Controlling_Formal (First_Entity (E))
10286 and then Etype (First_Entity (E)) = Prot_Typ
10287 and then Subtype_Conformant (Subp, E,
10288 Skip_Controlling_Formals => True)
10290 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10297 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10300 end Matching_Original_Protected_Subprogram;
10302 -- Start of processing for Has_Matching_Entry_Or_Subprogram
10305 -- Case 1: E is a subprogram whose first formal is a concurrent type
10306 -- defined in the scope of E that has an entry or subprogram whose
10307 -- profile matches E.
10309 if Comes_From_Source (E)
10310 and then Is_Subprogram (E)
10311 and then Present (First_Entity (E))
10312 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10315 Scope (Corresponding_Concurrent_Type
10316 (Etype (First_Entity (E))))
10319 (Matching_Entry_Or_Subprogram
10320 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10323 Report_Conflict (E,
10324 Matching_Entry_Or_Subprogram
10325 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10330 -- Case 2: E is an internally built dispatching subprogram of a
10331 -- protected type and there is a subprogram defined in the enclosing
10332 -- scope of the protected type that has the original name of E and
10333 -- its profile is conformant with the profile of E. We check the
10334 -- name of the original protected subprogram associated with E since
10335 -- the expander builds dispatching primitives of protected functions
10336 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
10338 elsif not Comes_From_Source (E)
10339 and then Is_Subprogram (E)
10340 and then Present (First_Entity (E))
10341 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
10342 and then Present (Original_Protected_Subprogram (E))
10345 (Matching_Original_Protected_Subprogram
10346 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10349 Report_Conflict (E,
10350 Matching_Original_Protected_Subprogram
10351 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
10355 -- Case 3: E is an entry of a synchronized type and a matching
10356 -- procedure has been previously defined in the enclosing scope
10357 -- of the synchronized type.
10359 elsif Comes_From_Source (E)
10360 and then Ekind (E) = E_Entry
10362 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
10364 Report_Conflict (E,
10365 Matching_Dispatching_Subprogram (Current_Scope, E));
10370 end Has_Matching_Entry_Or_Subprogram;
10372 ----------------------------
10373 -- Is_Private_Declaration --
10374 ----------------------------
10376 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10377 Decl : constant Node_Id := Unit_Declaration_Node (E);
10378 Priv_Decls : List_Id;
10381 if Is_Package_Or_Generic_Package (Current_Scope)
10382 and then In_Private_Part (Current_Scope)
10385 Private_Declarations (Package_Specification (Current_Scope));
10387 return In_Package_Body (Current_Scope)
10389 (Is_List_Member (Decl)
10390 and then List_Containing (Decl) = Priv_Decls)
10391 or else (Nkind (Parent (Decl)) = N_Package_Specification
10393 Is_Compilation_Unit
10394 (Defining_Entity (Parent (Decl)))
10395 and then List_Containing (Parent (Parent (Decl))) =
10400 end Is_Private_Declaration;
10402 --------------------------
10403 -- Is_Overriding_Alias --
10404 --------------------------
10406 function Is_Overriding_Alias
10407 (Old_E : Entity_Id;
10408 New_E : Entity_Id) return Boolean
10410 AO : constant Entity_Id := Alias (Old_E);
10411 AN : constant Entity_Id := Alias (New_E);
10414 return Scope (AO) /= Scope (AN)
10415 or else No (DTC_Entity (AO))
10416 or else No (DTC_Entity (AN))
10417 or else DT_Position (AO) = DT_Position (AN);
10418 end Is_Overriding_Alias;
10420 ---------------------
10421 -- Report_Conflict --
10422 ---------------------
10424 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
10426 Error_Msg_Sloc := Sloc (E);
10428 -- Generate message, with useful additional warning if in generic
10430 if Is_Generic_Unit (E) then
10431 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10432 Error_Msg_N ("\& conflicts with declaration#", S);
10434 Error_Msg_N ("& conflicts with declaration#", S);
10436 end Report_Conflict;
10438 -- Start of processing for New_Overloaded_Entity
10441 -- We need to look for an entity that S may override. This must be a
10442 -- homonym in the current scope, so we look for the first homonym of
10443 -- S in the current scope as the starting point for the search.
10445 E := Current_Entity_In_Scope (S);
10447 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10448 -- They are directly added to the list of primitive operations of
10449 -- Derived_Type, unless this is a rederivation in the private part
10450 -- of an operation that was already derived in the visible part of
10451 -- the current package.
10453 if Ada_Version >= Ada_2005
10454 and then Present (Derived_Type)
10455 and then Present (Alias (S))
10456 and then Is_Dispatching_Operation (Alias (S))
10457 and then Present (Find_Dispatching_Type (Alias (S)))
10458 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10460 -- For private types, when the full-view is processed we propagate to
10461 -- the full view the non-overridden entities whose attribute "alias"
10462 -- references an interface primitive. These entities were added by
10463 -- Derive_Subprograms to ensure that interface primitives are
10466 -- Inside_Freeze_Actions is non zero when S corresponds with an
10467 -- internal entity that links an interface primitive with its
10468 -- covering primitive through attribute Interface_Alias (see
10469 -- Add_Internal_Interface_Entities).
10471 if Inside_Freezing_Actions = 0
10472 and then Is_Package_Or_Generic_Package (Current_Scope)
10473 and then In_Private_Part (Current_Scope)
10474 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10475 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10476 and then Full_View (Defining_Identifier (Parent (E)))
10477 = Defining_Identifier (Parent (S))
10478 and then Alias (E) = Alias (S)
10480 Check_Operation_From_Private_View (S, E);
10481 Set_Is_Dispatching_Operation (S);
10486 Enter_Overloaded_Entity (S);
10487 Check_Dispatching_Operation (S, Empty);
10488 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10494 -- For synchronized types check conflicts of this entity with previously
10495 -- defined entities.
10497 if Ada_Version >= Ada_2005
10498 and then Has_Matching_Entry_Or_Subprogram (S)
10503 -- If there is no homonym then this is definitely not overriding
10506 Enter_Overloaded_Entity (S);
10507 Check_Dispatching_Operation (S, Empty);
10508 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10510 -- If subprogram has an explicit declaration, check whether it has an
10511 -- overriding indicator.
10513 if Comes_From_Source (S) then
10514 Check_Synchronized_Overriding (S, Overridden_Subp);
10516 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10517 -- it may have overridden some hidden inherited primitive. Update
10518 -- Overridden_Subp to avoid spurious errors when checking the
10519 -- overriding indicator.
10521 if Ada_Version >= Ada_2012
10522 and then No (Overridden_Subp)
10523 and then Is_Dispatching_Operation (S)
10524 and then Present (Overridden_Operation (S))
10526 Overridden_Subp := Overridden_Operation (S);
10529 Check_Overriding_Indicator
10530 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10532 -- The Ghost policy in effect at the point of declaration of a
10533 -- parent subprogram and an overriding subprogram must match
10534 -- (SPARK RM 6.9(17)).
10536 Check_Ghost_Overriding (S, Overridden_Subp);
10539 -- If there is a homonym that is not overloadable, then we have an
10540 -- error, except for the special cases checked explicitly below.
10542 elsif not Is_Overloadable (E) then
10544 -- Check for spurious conflict produced by a subprogram that has the
10545 -- same name as that of the enclosing generic package. The conflict
10546 -- occurs within an instance, between the subprogram and the renaming
10547 -- declaration for the package. After the subprogram, the package
10548 -- renaming declaration becomes hidden.
10550 if Ekind (E) = E_Package
10551 and then Present (Renamed_Object (E))
10552 and then Renamed_Object (E) = Current_Scope
10553 and then Nkind (Parent (Renamed_Object (E))) =
10554 N_Package_Specification
10555 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10558 Set_Is_Immediately_Visible (E, False);
10559 Enter_Overloaded_Entity (S);
10560 Set_Homonym (S, Homonym (E));
10561 Check_Dispatching_Operation (S, Empty);
10562 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10564 -- If the subprogram is implicit it is hidden by the previous
10565 -- declaration. However if it is dispatching, it must appear in the
10566 -- dispatch table anyway, because it can be dispatched to even if it
10567 -- cannot be called directly.
10569 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10570 Set_Scope (S, Current_Scope);
10572 if Is_Dispatching_Operation (Alias (S)) then
10573 Check_Dispatching_Operation (S, Empty);
10579 Report_Conflict (S, E);
10583 -- E exists and is overloadable
10586 Check_Synchronized_Overriding (S, Overridden_Subp);
10588 -- Loop through E and its homonyms to determine if any of them is
10589 -- the candidate for overriding by S.
10591 while Present (E) loop
10593 -- Definitely not interesting if not in the current scope
10595 if Scope (E) /= Current_Scope then
10598 -- A function can overload the name of an abstract state. The
10599 -- state can be viewed as a function with a profile that cannot
10600 -- be matched by anything.
10602 elsif Ekind (S) = E_Function
10603 and then Ekind (E) = E_Abstract_State
10605 Enter_Overloaded_Entity (S);
10608 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10609 -- procedures locate the internally generated spec. We enforce
10610 -- mode conformance since a tagged type may inherit from
10611 -- interfaces several null primitives which differ only in
10612 -- the mode of the formals.
10614 elsif not Comes_From_Source (S)
10615 and then Is_Null_Procedure (S)
10616 and then not Mode_Conformant (E, S)
10620 -- Check if we have type conformance
10622 elsif Type_Conformant (E, S) then
10624 -- If the old and new entities have the same profile and one
10625 -- is not the body of the other, then this is an error, unless
10626 -- one of them is implicitly declared.
10628 -- There are some cases when both can be implicit, for example
10629 -- when both a literal and a function that overrides it are
10630 -- inherited in a derivation, or when an inherited operation
10631 -- of a tagged full type overrides the inherited operation of
10632 -- a private extension. Ada 83 had a special rule for the
10633 -- literal case. In Ada 95, the later implicit operation hides
10634 -- the former, and the literal is always the former. In the
10635 -- odd case where both are derived operations declared at the
10636 -- same point, both operations should be declared, and in that
10637 -- case we bypass the following test and proceed to the next
10638 -- part. This can only occur for certain obscure cases in
10639 -- instances, when an operation on a type derived from a formal
10640 -- private type does not override a homograph inherited from
10641 -- the actual. In subsequent derivations of such a type, the
10642 -- DT positions of these operations remain distinct, if they
10645 if Present (Alias (S))
10646 and then (No (Alias (E))
10647 or else Comes_From_Source (E)
10648 or else Is_Abstract_Subprogram (S)
10650 (Is_Dispatching_Operation (E)
10651 and then Is_Overriding_Alias (E, S)))
10652 and then Ekind (E) /= E_Enumeration_Literal
10654 -- When an derived operation is overloaded it may be due to
10655 -- the fact that the full view of a private extension
10656 -- re-inherits. It has to be dealt with.
10658 if Is_Package_Or_Generic_Package (Current_Scope)
10659 and then In_Private_Part (Current_Scope)
10661 Check_Operation_From_Private_View (S, E);
10664 -- In any case the implicit operation remains hidden by the
10665 -- existing declaration, which is overriding. Indicate that
10666 -- E overrides the operation from which S is inherited.
10668 if Present (Alias (S)) then
10669 Set_Overridden_Operation (E, Alias (S));
10670 Inherit_Subprogram_Contract (E, Alias (S));
10673 Set_Overridden_Operation (E, S);
10674 Inherit_Subprogram_Contract (E, S);
10677 if Comes_From_Source (E) then
10678 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10680 -- The Ghost policy in effect at the point of declaration
10681 -- of a parent subprogram and an overriding subprogram
10682 -- must match (SPARK RM 6.9(17)).
10684 Check_Ghost_Overriding (E, S);
10689 -- Within an instance, the renaming declarations for actual
10690 -- subprograms may become ambiguous, but they do not hide each
10693 elsif Ekind (E) /= E_Entry
10694 and then not Comes_From_Source (E)
10695 and then not Is_Generic_Instance (E)
10696 and then (Present (Alias (E))
10697 or else Is_Intrinsic_Subprogram (E))
10698 and then (not In_Instance
10699 or else No (Parent (E))
10700 or else Nkind (Unit_Declaration_Node (E)) /=
10701 N_Subprogram_Renaming_Declaration)
10703 -- A subprogram child unit is not allowed to override an
10704 -- inherited subprogram (10.1.1(20)).
10706 if Is_Child_Unit (S) then
10708 ("child unit overrides inherited subprogram in parent",
10713 if Is_Non_Overriding_Operation (E, S) then
10714 Enter_Overloaded_Entity (S);
10716 if No (Derived_Type)
10717 or else Is_Tagged_Type (Derived_Type)
10719 Check_Dispatching_Operation (S, Empty);
10725 -- E is a derived operation or an internal operator which
10726 -- is being overridden. Remove E from further visibility.
10727 -- Furthermore, if E is a dispatching operation, it must be
10728 -- replaced in the list of primitive operations of its type
10729 -- (see Override_Dispatching_Operation).
10731 Overridden_Subp := E;
10737 Prev := First_Entity (Current_Scope);
10738 while Present (Prev) and then Next_Entity (Prev) /= E loop
10739 Next_Entity (Prev);
10742 -- It is possible for E to be in the current scope and
10743 -- yet not in the entity chain. This can only occur in a
10744 -- generic context where E is an implicit concatenation
10745 -- in the formal part, because in a generic body the
10746 -- entity chain starts with the formals.
10748 -- In GNATprove mode, a wrapper for an operation with
10749 -- axiomatization may be a homonym of another declaration
10750 -- for an actual subprogram (needs refinement ???).
10754 and then GNATprove_Mode
10756 Nkind (Original_Node (Unit_Declaration_Node (S))) =
10757 N_Subprogram_Renaming_Declaration
10761 pragma Assert (Chars (E) = Name_Op_Concat);
10766 -- E must be removed both from the entity_list of the
10767 -- current scope, and from the visibility chain.
10769 if Debug_Flag_E then
10770 Write_Str ("Override implicit operation ");
10771 Write_Int (Int (E));
10775 -- If E is a predefined concatenation, it stands for four
10776 -- different operations. As a result, a single explicit
10777 -- declaration does not hide it. In a possible ambiguous
10778 -- situation, Disambiguate chooses the user-defined op,
10779 -- so it is correct to retain the previous internal one.
10781 if Chars (E) /= Name_Op_Concat
10782 or else Ekind (E) /= E_Operator
10784 -- For nondispatching derived operations that are
10785 -- overridden by a subprogram declared in the private
10786 -- part of a package, we retain the derived subprogram
10787 -- but mark it as not immediately visible. If the
10788 -- derived operation was declared in the visible part
10789 -- then this ensures that it will still be visible
10790 -- outside the package with the proper signature
10791 -- (calls from outside must also be directed to this
10792 -- version rather than the overriding one, unlike the
10793 -- dispatching case). Calls from inside the package
10794 -- will still resolve to the overriding subprogram
10795 -- since the derived one is marked as not visible
10796 -- within the package.
10798 -- If the private operation is dispatching, we achieve
10799 -- the overriding by keeping the implicit operation
10800 -- but setting its alias to be the overriding one. In
10801 -- this fashion the proper body is executed in all
10802 -- cases, but the original signature is used outside
10805 -- If the overriding is not in the private part, we
10806 -- remove the implicit operation altogether.
10808 if Is_Private_Declaration (S) then
10809 if not Is_Dispatching_Operation (E) then
10810 Set_Is_Immediately_Visible (E, False);
10812 -- Work done in Override_Dispatching_Operation,
10813 -- so nothing else needs to be done here.
10819 -- Find predecessor of E in Homonym chain
10821 if E = Current_Entity (E) then
10824 Prev_Vis := Current_Entity (E);
10825 while Homonym (Prev_Vis) /= E loop
10826 Prev_Vis := Homonym (Prev_Vis);
10830 if Prev_Vis /= Empty then
10832 -- Skip E in the visibility chain
10834 Set_Homonym (Prev_Vis, Homonym (E));
10837 Set_Name_Entity_Id (Chars (E), Homonym (E));
10840 Set_Next_Entity (Prev, Next_Entity (E));
10842 if No (Next_Entity (Prev)) then
10843 Set_Last_Entity (Current_Scope, Prev);
10848 Enter_Overloaded_Entity (S);
10850 -- For entities generated by Derive_Subprograms the
10851 -- overridden operation is the inherited primitive
10852 -- (which is available through the attribute alias).
10854 if not (Comes_From_Source (E))
10855 and then Is_Dispatching_Operation (E)
10856 and then Find_Dispatching_Type (E) =
10857 Find_Dispatching_Type (S)
10858 and then Present (Alias (E))
10859 and then Comes_From_Source (Alias (E))
10861 Set_Overridden_Operation (S, Alias (E));
10862 Inherit_Subprogram_Contract (S, Alias (E));
10864 -- Normal case of setting entity as overridden
10866 -- Note: Static_Initialization and Overridden_Operation
10867 -- attributes use the same field in subprogram entities.
10868 -- Static_Initialization is only defined for internal
10869 -- initialization procedures, where Overridden_Operation
10870 -- is irrelevant. Therefore the setting of this attribute
10871 -- must check whether the target is an init_proc.
10873 elsif not Is_Init_Proc (S) then
10874 Set_Overridden_Operation (S, E);
10875 Inherit_Subprogram_Contract (S, E);
10878 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10880 -- The Ghost policy in effect at the point of declaration
10881 -- of a parent subprogram and an overriding subprogram
10882 -- must match (SPARK RM 6.9(17)).
10884 Check_Ghost_Overriding (S, E);
10886 -- If S is a user-defined subprogram or a null procedure
10887 -- expanded to override an inherited null procedure, or a
10888 -- predefined dispatching primitive then indicate that E
10889 -- overrides the operation from which S is inherited.
10891 if Comes_From_Source (S)
10893 (Present (Parent (S))
10895 Nkind (Parent (S)) = N_Procedure_Specification
10897 Null_Present (Parent (S)))
10899 (Present (Alias (E))
10901 Is_Predefined_Dispatching_Operation (Alias (E)))
10903 if Present (Alias (E)) then
10904 Set_Overridden_Operation (S, Alias (E));
10905 Inherit_Subprogram_Contract (S, Alias (E));
10909 if Is_Dispatching_Operation (E) then
10911 -- An overriding dispatching subprogram inherits the
10912 -- convention of the overridden subprogram (AI-117).
10914 Set_Convention (S, Convention (E));
10915 Check_Dispatching_Operation (S, E);
10918 Check_Dispatching_Operation (S, Empty);
10921 Check_For_Primitive_Subprogram
10922 (Is_Primitive_Subp, Is_Overriding => True);
10923 goto Check_Inequality;
10926 -- Apparent redeclarations in instances can occur when two
10927 -- formal types get the same actual type. The subprograms in
10928 -- in the instance are legal, even if not callable from the
10929 -- outside. Calls from within are disambiguated elsewhere.
10930 -- For dispatching operations in the visible part, the usual
10931 -- rules apply, and operations with the same profile are not
10932 -- legal (B830001).
10934 elsif (In_Instance_Visible_Part
10935 and then not Is_Dispatching_Operation (E))
10936 or else In_Instance_Not_Visible
10940 -- Here we have a real error (identical profile)
10943 Error_Msg_Sloc := Sloc (E);
10945 -- Avoid cascaded errors if the entity appears in
10946 -- subsequent calls.
10948 Set_Scope (S, Current_Scope);
10950 -- Generate error, with extra useful warning for the case
10951 -- of a generic instance with no completion.
10953 if Is_Generic_Instance (S)
10954 and then not Has_Completion (E)
10957 ("instantiation cannot provide body for&", S);
10958 Error_Msg_N ("\& conflicts with declaration#", S);
10960 Error_Msg_N ("& conflicts with declaration#", S);
10967 -- If one subprogram has an access parameter and the other
10968 -- a parameter of an access type, calls to either might be
10969 -- ambiguous. Verify that parameters match except for the
10970 -- access parameter.
10972 if May_Hide_Profile then
10978 F1 := First_Formal (S);
10979 F2 := First_Formal (E);
10980 while Present (F1) and then Present (F2) loop
10981 if Is_Access_Type (Etype (F1)) then
10982 if not Is_Access_Type (Etype (F2))
10983 or else not Conforming_Types
10984 (Designated_Type (Etype (F1)),
10985 Designated_Type (Etype (F2)),
10988 May_Hide_Profile := False;
10992 not Conforming_Types
10993 (Etype (F1), Etype (F2), Type_Conformant)
10995 May_Hide_Profile := False;
11002 if May_Hide_Profile
11006 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11015 -- On exit, we know that S is a new entity
11017 Enter_Overloaded_Entity (S);
11018 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11019 Check_Overriding_Indicator
11020 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11022 -- The Ghost policy in effect at the point of declaration of a parent
11023 -- subprogram and an overriding subprogram must match
11024 -- (SPARK RM 6.9(17)).
11026 Check_Ghost_Overriding (S, Overridden_Subp);
11028 -- Overloading is not allowed in SPARK, except for operators
11030 if Nkind (S) /= N_Defining_Operator_Symbol then
11031 Error_Msg_Sloc := Sloc (Homonym (S));
11032 Check_SPARK_05_Restriction
11033 ("overloading not allowed with entity#", S);
11036 -- If S is a derived operation for an untagged type then by
11037 -- definition it's not a dispatching operation (even if the parent
11038 -- operation was dispatching), so Check_Dispatching_Operation is not
11039 -- called in that case.
11041 if No (Derived_Type)
11042 or else Is_Tagged_Type (Derived_Type)
11044 Check_Dispatching_Operation (S, Empty);
11048 -- If this is a user-defined equality operator that is not a derived
11049 -- subprogram, create the corresponding inequality. If the operation is
11050 -- dispatching, the expansion is done elsewhere, and we do not create
11051 -- an explicit inequality operation.
11053 <<Check_Inequality>>
11054 if Chars (S) = Name_Op_Eq
11055 and then Etype (S) = Standard_Boolean
11056 and then Present (Parent (S))
11057 and then not Is_Dispatching_Operation (S)
11059 Make_Inequality_Operator (S);
11060 Check_Untagged_Equality (S);
11062 end New_Overloaded_Entity;
11064 ---------------------
11065 -- Process_Formals --
11066 ---------------------
11068 procedure Process_Formals
11070 Related_Nod : Node_Id)
11072 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11073 -- Determine whether an access type designates a type coming from a
11076 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11077 -- Check whether the default has a class-wide type. After analysis the
11078 -- default has the type of the formal, so we must also check explicitly
11079 -- for an access attribute.
11081 ----------------------------------
11082 -- Designates_From_Limited_With --
11083 ----------------------------------
11085 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11086 Desig : Entity_Id := Typ;
11089 if Is_Access_Type (Desig) then
11090 Desig := Directly_Designated_Type (Desig);
11093 if Is_Class_Wide_Type (Desig) then
11094 Desig := Root_Type (Desig);
11098 Ekind (Desig) = E_Incomplete_Type
11099 and then From_Limited_With (Desig);
11100 end Designates_From_Limited_With;
11102 ---------------------------
11103 -- Is_Class_Wide_Default --
11104 ---------------------------
11106 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11108 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11109 or else (Nkind (D) = N_Attribute_Reference
11110 and then Attribute_Name (D) = Name_Access
11111 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11112 end Is_Class_Wide_Default;
11116 Context : constant Node_Id := Parent (Parent (T));
11118 Formal : Entity_Id;
11119 Formal_Type : Entity_Id;
11120 Param_Spec : Node_Id;
11123 Num_Out_Params : Nat := 0;
11124 First_Out_Param : Entity_Id := Empty;
11125 -- Used for setting Is_Only_Out_Parameter
11127 -- Start of processing for Process_Formals
11130 -- In order to prevent premature use of the formals in the same formal
11131 -- part, the Ekind is left undefined until all default expressions are
11132 -- analyzed. The Ekind is established in a separate loop at the end.
11134 Param_Spec := First (T);
11135 while Present (Param_Spec) loop
11136 Formal := Defining_Identifier (Param_Spec);
11137 Set_Never_Set_In_Source (Formal, True);
11138 Enter_Name (Formal);
11140 -- Case of ordinary parameters
11142 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11143 Find_Type (Parameter_Type (Param_Spec));
11144 Ptype := Parameter_Type (Param_Spec);
11146 if Ptype = Error then
11150 Formal_Type := Entity (Ptype);
11152 if Is_Incomplete_Type (Formal_Type)
11154 (Is_Class_Wide_Type (Formal_Type)
11155 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11157 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11158 -- primitive operations, as long as their completion is
11159 -- in the same declarative part. If in the private part
11160 -- this means that the type cannot be a Taft-amendment type.
11161 -- Check is done on package exit. For access to subprograms,
11162 -- the use is legal for Taft-amendment types.
11164 -- Ada 2012: tagged incomplete types are allowed as generic
11165 -- formal types. They do not introduce dependencies and the
11166 -- corresponding generic subprogram does not have a delayed
11167 -- freeze, because it does not need a freeze node. However,
11168 -- it is still the case that untagged incomplete types cannot
11169 -- be Taft-amendment types and must be completed in private
11170 -- part, so the subprogram must appear in the list of private
11171 -- dependents of the type.
11173 if Is_Tagged_Type (Formal_Type)
11174 or else (Ada_Version >= Ada_2012
11175 and then not From_Limited_With (Formal_Type)
11176 and then not Is_Generic_Type (Formal_Type))
11178 if Ekind (Scope (Current_Scope)) = E_Package
11179 and then not Is_Generic_Type (Formal_Type)
11180 and then not Is_Class_Wide_Type (Formal_Type)
11183 (Parent (T), N_Access_Function_Definition,
11184 N_Access_Procedure_Definition)
11186 Append_Elmt (Current_Scope,
11187 Private_Dependents (Base_Type (Formal_Type)));
11189 -- Freezing is delayed to ensure that Register_Prim
11190 -- will get called for this operation, which is needed
11191 -- in cases where static dispatch tables aren't built.
11192 -- (Note that the same is done for controlling access
11193 -- parameter cases in function Access_Definition.)
11195 if not Is_Thunk (Current_Scope) then
11196 Set_Has_Delayed_Freeze (Current_Scope);
11201 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11202 N_Access_Procedure_Definition)
11204 -- AI05-0151: Tagged incomplete types are allowed in all
11205 -- formal parts. Untagged incomplete types are not allowed
11206 -- in bodies. Limited views of either kind are not allowed
11207 -- if there is no place at which the non-limited view can
11208 -- become available.
11210 -- Incomplete formal untagged types are not allowed in
11211 -- subprogram bodies (but are legal in their declarations).
11212 -- This excludes bodies created for null procedures, which
11213 -- are basic declarations.
11215 if Is_Generic_Type (Formal_Type)
11216 and then not Is_Tagged_Type (Formal_Type)
11217 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11220 ("invalid use of formal incomplete type", Param_Spec);
11222 elsif Ada_Version >= Ada_2012 then
11223 if Is_Tagged_Type (Formal_Type)
11224 and then (not From_Limited_With (Formal_Type)
11225 or else not In_Package_Body)
11229 elsif Nkind_In (Context, N_Accept_Statement,
11230 N_Accept_Alternative,
11232 or else (Nkind (Context) = N_Subprogram_Body
11233 and then Comes_From_Source (Context))
11236 ("invalid use of untagged incomplete type &",
11237 Ptype, Formal_Type);
11242 ("invalid use of incomplete type&",
11243 Param_Spec, Formal_Type);
11245 -- Further checks on the legality of incomplete types
11246 -- in formal parts are delayed until the freeze point
11247 -- of the enclosing subprogram or access to subprogram.
11251 elsif Ekind (Formal_Type) = E_Void then
11253 ("premature use of&",
11254 Parameter_Type (Param_Spec), Formal_Type);
11257 -- Ada 2012 (AI-142): Handle aliased parameters
11259 if Ada_Version >= Ada_2012
11260 and then Aliased_Present (Param_Spec)
11262 Set_Is_Aliased (Formal);
11265 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11266 -- declaration corresponding to the null-excluding type of the
11267 -- formal in the enclosing scope. Finally, replace the parameter
11268 -- type of the formal with the internal subtype.
11270 if Ada_Version >= Ada_2005
11271 and then Null_Exclusion_Present (Param_Spec)
11273 if not Is_Access_Type (Formal_Type) then
11275 ("`NOT NULL` allowed only for an access type", Param_Spec);
11278 if Can_Never_Be_Null (Formal_Type)
11279 and then Comes_From_Source (Related_Nod)
11282 ("`NOT NULL` not allowed (& already excludes null)",
11283 Param_Spec, Formal_Type);
11287 Create_Null_Excluding_Itype
11289 Related_Nod => Related_Nod,
11290 Scope_Id => Scope (Current_Scope));
11292 -- If the designated type of the itype is an itype that is
11293 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11294 -- on the access subtype, to prevent order-of-elaboration
11295 -- issues in the backend.
11298 -- type T is access procedure;
11299 -- procedure Op (O : not null T);
11301 if Is_Itype (Directly_Designated_Type (Formal_Type))
11303 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11305 Set_Has_Delayed_Freeze (Formal_Type);
11310 -- An access formal type
11314 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11316 -- No need to continue if we already notified errors
11318 if not Present (Formal_Type) then
11322 -- Ada 2005 (AI-254)
11325 AD : constant Node_Id :=
11326 Access_To_Subprogram_Definition
11327 (Parameter_Type (Param_Spec));
11329 if Present (AD) and then Protected_Present (AD) then
11331 Replace_Anonymous_Access_To_Protected_Subprogram
11337 Set_Etype (Formal, Formal_Type);
11339 -- Deal with default expression if present
11341 Default := Expression (Param_Spec);
11343 if Present (Default) then
11344 Check_SPARK_05_Restriction
11345 ("default expression is not allowed", Default);
11347 if Out_Present (Param_Spec) then
11349 ("default initialization only allowed for IN parameters",
11353 -- Do the special preanalysis of the expression (see section on
11354 -- "Handling of Default Expressions" in the spec of package Sem).
11356 Preanalyze_Spec_Expression (Default, Formal_Type);
11358 -- An access to constant cannot be the default for
11359 -- an access parameter that is an access to variable.
11361 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11362 and then not Is_Access_Constant (Formal_Type)
11363 and then Is_Access_Type (Etype (Default))
11364 and then Is_Access_Constant (Etype (Default))
11367 ("formal that is access to variable cannot be initialized "
11368 & "with an access-to-constant expression", Default);
11371 -- Check that the designated type of an access parameter's default
11372 -- is not a class-wide type unless the parameter's designated type
11373 -- is also class-wide.
11375 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11376 and then not Designates_From_Limited_With (Formal_Type)
11377 and then Is_Class_Wide_Default (Default)
11378 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11381 ("access to class-wide expression not allowed here", Default);
11384 -- Check incorrect use of dynamically tagged expressions
11386 if Is_Tagged_Type (Formal_Type) then
11387 Check_Dynamically_Tagged_Expression
11389 Typ => Formal_Type,
11390 Related_Nod => Default);
11394 -- Ada 2005 (AI-231): Static checks
11396 if Ada_Version >= Ada_2005
11397 and then Is_Access_Type (Etype (Formal))
11398 and then Can_Never_Be_Null (Etype (Formal))
11400 Null_Exclusion_Static_Checks (Param_Spec);
11403 -- The following checks are relevant only when SPARK_Mode is on as
11404 -- these are not standard Ada legality rules.
11406 if SPARK_Mode = On then
11407 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11409 -- A function cannot have a parameter of mode IN OUT or OUT
11412 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11414 ("function cannot have parameter of mode `OUT` or "
11415 & "`IN OUT`", Formal);
11418 -- A procedure cannot have an effectively volatile formal
11419 -- parameter of mode IN because it behaves as a constant
11420 -- (SPARK RM 7.1.3(6)). -- ??? maybe 7.1.3(4)
11422 elsif Ekind (Scope (Formal)) = E_Procedure
11423 and then Ekind (Formal) = E_In_Parameter
11424 and then Is_Effectively_Volatile (Formal)
11427 ("formal parameter of mode `IN` cannot be volatile", Formal);
11435 -- If this is the formal part of a function specification, analyze the
11436 -- subtype mark in the context where the formals are visible but not
11437 -- yet usable, and may hide outer homographs.
11439 if Nkind (Related_Nod) = N_Function_Specification then
11440 Analyze_Return_Type (Related_Nod);
11443 -- Now set the kind (mode) of each formal
11445 Param_Spec := First (T);
11446 while Present (Param_Spec) loop
11447 Formal := Defining_Identifier (Param_Spec);
11448 Set_Formal_Mode (Formal);
11450 if Ekind (Formal) = E_In_Parameter then
11451 Set_Default_Value (Formal, Expression (Param_Spec));
11453 if Present (Expression (Param_Spec)) then
11454 Default := Expression (Param_Spec);
11456 if Is_Scalar_Type (Etype (Default)) then
11457 if Nkind (Parameter_Type (Param_Spec)) /=
11458 N_Access_Definition
11460 Formal_Type := Entity (Parameter_Type (Param_Spec));
11464 (Related_Nod, Parameter_Type (Param_Spec));
11467 Apply_Scalar_Range_Check (Default, Formal_Type);
11471 elsif Ekind (Formal) = E_Out_Parameter then
11472 Num_Out_Params := Num_Out_Params + 1;
11474 if Num_Out_Params = 1 then
11475 First_Out_Param := Formal;
11478 elsif Ekind (Formal) = E_In_Out_Parameter then
11479 Num_Out_Params := Num_Out_Params + 1;
11482 -- Skip remaining processing if formal type was in error
11484 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11485 goto Next_Parameter;
11488 -- Force call by reference if aliased
11491 Conv : constant Convention_Id := Convention (Etype (Formal));
11493 if Is_Aliased (Formal) then
11494 Set_Mechanism (Formal, By_Reference);
11496 -- Warn if user asked this to be passed by copy
11498 if Conv = Convention_Ada_Pass_By_Copy then
11500 ("cannot pass aliased parameter & by copy??", Formal);
11503 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11505 elsif Conv = Convention_Ada_Pass_By_Copy then
11506 Set_Mechanism (Formal, By_Copy);
11508 elsif Conv = Convention_Ada_Pass_By_Reference then
11509 Set_Mechanism (Formal, By_Reference);
11517 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11518 Set_Is_Only_Out_Parameter (First_Out_Param);
11520 end Process_Formals;
11522 ----------------------------
11523 -- Reference_Body_Formals --
11524 ----------------------------
11526 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11531 if Error_Posted (Spec) then
11535 -- Iterate over both lists. They may be of different lengths if the two
11536 -- specs are not conformant.
11538 Fs := First_Formal (Spec);
11539 Fb := First_Formal (Bod);
11540 while Present (Fs) and then Present (Fb) loop
11541 Generate_Reference (Fs, Fb, 'b');
11543 if Style_Check then
11544 Style.Check_Identifier (Fb, Fs);
11547 Set_Spec_Entity (Fb, Fs);
11548 Set_Referenced (Fs, False);
11552 end Reference_Body_Formals;
11554 -------------------------
11555 -- Set_Actual_Subtypes --
11556 -------------------------
11558 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11560 Formal : Entity_Id;
11562 First_Stmt : Node_Id := Empty;
11563 AS_Needed : Boolean;
11566 -- If this is an empty initialization procedure, no need to create
11567 -- actual subtypes (small optimization).
11569 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11572 -- Within a predicate function we do not want to generate local
11573 -- subtypes that may generate nested predicate functions.
11575 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
11579 -- The subtype declarations may freeze the formals. The body generated
11580 -- for an expression function is not a freeze point, so do not emit
11581 -- these declarations (small loss of efficiency in rare cases).
11583 if Nkind (N) = N_Subprogram_Body
11584 and then Was_Expression_Function (N)
11589 Formal := First_Formal (Subp);
11590 while Present (Formal) loop
11591 T := Etype (Formal);
11593 -- We never need an actual subtype for a constrained formal
11595 if Is_Constrained (T) then
11596 AS_Needed := False;
11598 -- If we have unknown discriminants, then we do not need an actual
11599 -- subtype, or more accurately we cannot figure it out. Note that
11600 -- all class-wide types have unknown discriminants.
11602 elsif Has_Unknown_Discriminants (T) then
11603 AS_Needed := False;
11605 -- At this stage we have an unconstrained type that may need an
11606 -- actual subtype. For sure the actual subtype is needed if we have
11607 -- an unconstrained array type. However, in an instance, the type
11608 -- may appear as a subtype of the full view, while the actual is
11609 -- in fact private (in which case no actual subtype is needed) so
11610 -- check the kind of the base type.
11612 elsif Is_Array_Type (Base_Type (T)) then
11615 -- The only other case needing an actual subtype is an unconstrained
11616 -- record type which is an IN parameter (we cannot generate actual
11617 -- subtypes for the OUT or IN OUT case, since an assignment can
11618 -- change the discriminant values. However we exclude the case of
11619 -- initialization procedures, since discriminants are handled very
11620 -- specially in this context, see the section entitled "Handling of
11621 -- Discriminants" in Einfo.
11623 -- We also exclude the case of Discrim_SO_Functions (functions used
11624 -- in front-end layout mode for size/offset values), since in such
11625 -- functions only discriminants are referenced, and not only are such
11626 -- subtypes not needed, but they cannot always be generated, because
11627 -- of order of elaboration issues.
11629 elsif Is_Record_Type (T)
11630 and then Ekind (Formal) = E_In_Parameter
11631 and then Chars (Formal) /= Name_uInit
11632 and then not Is_Unchecked_Union (T)
11633 and then not Is_Discrim_SO_Function (Subp)
11637 -- All other cases do not need an actual subtype
11640 AS_Needed := False;
11643 -- Generate actual subtypes for unconstrained arrays and
11644 -- unconstrained discriminated records.
11647 if Nkind (N) = N_Accept_Statement then
11649 -- If expansion is active, the formal is replaced by a local
11650 -- variable that renames the corresponding entry of the
11651 -- parameter block, and it is this local variable that may
11652 -- require an actual subtype.
11654 if Expander_Active then
11655 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11657 Decl := Build_Actual_Subtype (T, Formal);
11660 if Present (Handled_Statement_Sequence (N)) then
11662 First (Statements (Handled_Statement_Sequence (N)));
11663 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11664 Mark_Rewrite_Insertion (Decl);
11666 -- If the accept statement has no body, there will be no
11667 -- reference to the actuals, so no need to compute actual
11674 Decl := Build_Actual_Subtype (T, Formal);
11675 Prepend (Decl, Declarations (N));
11676 Mark_Rewrite_Insertion (Decl);
11679 -- The declaration uses the bounds of an existing object, and
11680 -- therefore needs no constraint checks.
11682 Analyze (Decl, Suppress => All_Checks);
11683 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
11685 -- We need to freeze manually the generated type when it is
11686 -- inserted anywhere else than in a declarative part.
11688 if Present (First_Stmt) then
11689 Insert_List_Before_And_Analyze (First_Stmt,
11690 Freeze_Entity (Defining_Identifier (Decl), N));
11692 -- Ditto if the type has a dynamic predicate, because the
11693 -- generated function will mention the actual subtype. The
11694 -- predicate may come from an explicit aspect of be inherited.
11696 elsif Has_Predicates (T) then
11697 Insert_List_Before_And_Analyze (Decl,
11698 Freeze_Entity (Defining_Identifier (Decl), N));
11701 if Nkind (N) = N_Accept_Statement
11702 and then Expander_Active
11704 Set_Actual_Subtype (Renamed_Object (Formal),
11705 Defining_Identifier (Decl));
11707 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11711 Next_Formal (Formal);
11713 end Set_Actual_Subtypes;
11715 ---------------------
11716 -- Set_Formal_Mode --
11717 ---------------------
11719 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11720 Spec : constant Node_Id := Parent (Formal_Id);
11721 Id : constant Entity_Id := Scope (Formal_Id);
11724 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11725 -- since we ensure that corresponding actuals are always valid at the
11726 -- point of the call.
11728 if Out_Present (Spec) then
11729 if Ekind_In (Id, E_Entry, E_Entry_Family)
11730 or else Is_Subprogram_Or_Generic_Subprogram (Id)
11732 Set_Has_Out_Or_In_Out_Parameter (Id, True);
11735 if Ekind_In (Id, E_Function, E_Generic_Function) then
11737 -- [IN] OUT parameters allowed for functions in Ada 2012
11739 if Ada_Version >= Ada_2012 then
11741 -- Even in Ada 2012 operators can only have IN parameters
11743 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11744 Error_Msg_N ("operators can only have IN parameters", Spec);
11747 if In_Present (Spec) then
11748 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11750 Set_Ekind (Formal_Id, E_Out_Parameter);
11753 -- But not in earlier versions of Ada
11756 Error_Msg_N ("functions can only have IN parameters", Spec);
11757 Set_Ekind (Formal_Id, E_In_Parameter);
11760 elsif In_Present (Spec) then
11761 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11764 Set_Ekind (Formal_Id, E_Out_Parameter);
11765 Set_Never_Set_In_Source (Formal_Id, True);
11766 Set_Is_True_Constant (Formal_Id, False);
11767 Set_Current_Value (Formal_Id, Empty);
11771 Set_Ekind (Formal_Id, E_In_Parameter);
11774 -- Set Is_Known_Non_Null for access parameters since the language
11775 -- guarantees that access parameters are always non-null. We also set
11776 -- Can_Never_Be_Null, since there is no way to change the value.
11778 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11780 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11781 -- null; In Ada 2005, only if then null_exclusion is explicit.
11783 if Ada_Version < Ada_2005
11784 or else Can_Never_Be_Null (Etype (Formal_Id))
11786 Set_Is_Known_Non_Null (Formal_Id);
11787 Set_Can_Never_Be_Null (Formal_Id);
11790 -- Ada 2005 (AI-231): Null-exclusion access subtype
11792 elsif Is_Access_Type (Etype (Formal_Id))
11793 and then Can_Never_Be_Null (Etype (Formal_Id))
11795 Set_Is_Known_Non_Null (Formal_Id);
11797 -- We can also set Can_Never_Be_Null (thus preventing some junk
11798 -- access checks) for the case of an IN parameter, which cannot
11799 -- be changed, or for an IN OUT parameter, which can be changed but
11800 -- not to a null value. But for an OUT parameter, the initial value
11801 -- passed in can be null, so we can't set this flag in that case.
11803 if Ekind (Formal_Id) /= E_Out_Parameter then
11804 Set_Can_Never_Be_Null (Formal_Id);
11808 Set_Mechanism (Formal_Id, Default_Mechanism);
11809 Set_Formal_Validity (Formal_Id);
11810 end Set_Formal_Mode;
11812 -------------------------
11813 -- Set_Formal_Validity --
11814 -------------------------
11816 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11818 -- If no validity checking, then we cannot assume anything about the
11819 -- validity of parameters, since we do not know there is any checking
11820 -- of the validity on the call side.
11822 if not Validity_Checks_On then
11825 -- If validity checking for parameters is enabled, this means we are
11826 -- not supposed to make any assumptions about argument values.
11828 elsif Validity_Check_Parameters then
11831 -- If we are checking in parameters, we will assume that the caller is
11832 -- also checking parameters, so we can assume the parameter is valid.
11834 elsif Ekind (Formal_Id) = E_In_Parameter
11835 and then Validity_Check_In_Params
11837 Set_Is_Known_Valid (Formal_Id, True);
11839 -- Similar treatment for IN OUT parameters
11841 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11842 and then Validity_Check_In_Out_Params
11844 Set_Is_Known_Valid (Formal_Id, True);
11846 end Set_Formal_Validity;
11848 ------------------------
11849 -- Subtype_Conformant --
11850 ------------------------
11852 function Subtype_Conformant
11853 (New_Id : Entity_Id;
11854 Old_Id : Entity_Id;
11855 Skip_Controlling_Formals : Boolean := False) return Boolean
11859 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11860 Skip_Controlling_Formals => Skip_Controlling_Formals);
11862 end Subtype_Conformant;
11864 ---------------------
11865 -- Type_Conformant --
11866 ---------------------
11868 function Type_Conformant
11869 (New_Id : Entity_Id;
11870 Old_Id : Entity_Id;
11871 Skip_Controlling_Formals : Boolean := False) return Boolean
11875 May_Hide_Profile := False;
11877 (New_Id, Old_Id, Type_Conformant, False, Result,
11878 Skip_Controlling_Formals => Skip_Controlling_Formals);
11880 end Type_Conformant;
11882 -------------------------------
11883 -- Valid_Operator_Definition --
11884 -------------------------------
11886 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11889 Id : constant Name_Id := Chars (Designator);
11893 F := First_Formal (Designator);
11894 while Present (F) loop
11897 if Present (Default_Value (F)) then
11899 ("default values not allowed for operator parameters",
11902 -- For function instantiations that are operators, we must check
11903 -- separately that the corresponding generic only has in-parameters.
11904 -- For subprogram declarations this is done in Set_Formal_Mode. Such
11905 -- an error could not arise in earlier versions of the language.
11907 elsif Ekind (F) /= E_In_Parameter then
11908 Error_Msg_N ("operators can only have IN parameters", F);
11914 -- Verify that user-defined operators have proper number of arguments
11915 -- First case of operators which can only be unary
11917 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
11920 -- Case of operators which can be unary or binary
11922 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
11923 N_OK := (N in 1 .. 2);
11925 -- All other operators can only be binary
11933 ("incorrect number of arguments for operator", Designator);
11937 and then Base_Type (Etype (Designator)) = Standard_Boolean
11938 and then not Is_Intrinsic_Subprogram (Designator)
11941 ("explicit definition of inequality not allowed", Designator);
11943 end Valid_Operator_Definition;