1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Dbug; use Exp_Dbug;
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 Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Cat; use Sem_Cat;
59 with Sem_Ch3; use Sem_Ch3;
60 with Sem_Ch4; use Sem_Ch4;
61 with Sem_Ch5; use Sem_Ch5;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch9; use Sem_Ch9;
64 with Sem_Ch10; use Sem_Ch10;
65 with Sem_Ch12; use Sem_Ch12;
66 with Sem_Ch13; use Sem_Ch13;
67 with Sem_Dim; use Sem_Dim;
68 with Sem_Disp; use Sem_Disp;
69 with Sem_Dist; use Sem_Dist;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Mech; use Sem_Mech;
73 with Sem_Prag; use Sem_Prag;
74 with Sem_Res; use Sem_Res;
75 with Sem_Util; use Sem_Util;
76 with Sem_Type; use Sem_Type;
77 with Sem_Warn; use Sem_Warn;
78 with Sinput; use Sinput;
79 with Stand; use Stand;
80 with Sinfo; use Sinfo;
81 with Sinfo.CN; use Sinfo.CN;
82 with Snames; use Snames;
83 with Stringt; use Stringt;
85 with Stylesw; use Stylesw;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Urealp; use Urealp;
89 with Validsw; use Validsw;
91 package body Sem_Ch6 is
93 May_Hide_Profile : Boolean := False;
94 -- This flag is used to indicate that two formals in two subprograms being
95 -- checked for conformance differ only in that one is an access parameter
96 -- while the other is of a general access type with the same designated
97 -- type. In this case, if the rest of the signatures match, a call to
98 -- either subprogram may be ambiguous, which is worth a warning. The flag
99 -- is set in Compatible_Types, and the warning emitted in
100 -- New_Overloaded_Entity.
102 -----------------------
103 -- Local Subprograms --
104 -----------------------
106 procedure Analyze_Function_Return (N : Node_Id);
107 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
108 -- applies to a [generic] function.
110 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
111 -- Analyze a generic subprogram body. N is the body to be analyzed, and
112 -- Gen_Id is the defining entity Id for the corresponding spec.
114 procedure Analyze_Null_Procedure
116 Is_Completion : out Boolean);
117 -- A null procedure can be a declaration or (Ada 2012) a completion
119 procedure Analyze_Return_Statement (N : Node_Id);
120 -- Common processing for simple and extended return statements
122 procedure Analyze_Return_Type (N : Node_Id);
123 -- Subsidiary to Process_Formals: analyze subtype mark in function
124 -- specification in a context where the formals are visible and hide
127 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
128 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
129 -- that we can use RETURN but not skip the debug output at the end.
131 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
132 -- Returns true if Subp can override a predefined operator.
134 procedure Check_Conformance
137 Ctype : Conformance_Type;
139 Conforms : out Boolean;
140 Err_Loc : Node_Id := Empty;
141 Get_Inst : Boolean := False;
142 Skip_Controlling_Formals : Boolean := False);
143 -- Given two entities, this procedure checks that the profiles associated
144 -- with these entities meet the conformance criterion given by the third
145 -- parameter. If they conform, Conforms is set True and control returns
146 -- to the caller. If they do not conform, Conforms is set to False, and
147 -- in addition, if Errmsg is True on the call, proper messages are output
148 -- to complain about the conformance failure. If Err_Loc is non_Empty
149 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
150 -- error messages are placed on the appropriate part of the construct
151 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
152 -- against a formal access-to-subprogram type so Get_Instance_Of must
155 procedure Check_Formal_Subprogram_Conformance
160 Conforms : out Boolean);
161 -- Core implementation of Check_Formal_Subprogram_Conformance from spec.
162 -- Errmsg can be set to False to not emit error messages.
163 -- Conforms is set to True if there is conformance, False otherwise.
165 procedure Check_Limited_Return
169 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
170 -- types. Used only for simple return statements. Expr is the expression
173 procedure Check_Subprogram_Order (N : Node_Id);
174 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
175 -- the alpha ordering rule for N if this ordering requirement applicable.
177 procedure Check_Returns
181 Proc : Entity_Id := Empty);
182 -- Called to check for missing return statements in a function body, or for
183 -- returns present in a procedure body which has No_Return set. HSS is the
184 -- handled statement sequence for the subprogram body. This procedure
185 -- checks all flow paths to make sure they either have return (Mode = 'F',
186 -- used for functions) or do not have a return (Mode = 'P', used for
187 -- No_Return procedures). The flag Err is set if there are any control
188 -- paths not explicitly terminated by a return in the function case, and is
189 -- True otherwise. Proc is the entity for the procedure case and is used
190 -- in posting the warning message.
192 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
193 -- In Ada 2012, a primitive equality operator on an untagged record type
194 -- must appear before the type is frozen, and have the same visibility as
195 -- that of the type. This procedure checks that this rule is met, and
196 -- otherwise emits an error on the subprogram declaration and a warning
197 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
198 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
199 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
200 -- is set, otherwise the call has no effect.
202 procedure Enter_Overloaded_Entity (S : Entity_Id);
203 -- This procedure makes S, a new overloaded entity, into the first visible
204 -- entity with that name.
206 function Is_Non_Overriding_Operation
208 New_E : Entity_Id) return Boolean;
209 -- Enforce the rule given in 12.3(18): a private operation in an instance
210 -- overrides an inherited operation only if the corresponding operation
211 -- was overriding in the generic. This needs to be checked for primitive
212 -- operations of types derived (in the generic unit) from formal private
213 -- or formal derived types.
215 procedure Make_Inequality_Operator (S : Entity_Id);
216 -- Create the declaration for an inequality operator that is implicitly
217 -- created by a user-defined equality operator that yields a boolean.
219 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
220 -- Preanalysis of default expressions of subprogram formals. N is the
221 -- expression to be analyzed and T is the expected type.
223 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
224 -- Formal_Id is an formal parameter entity. This procedure deals with
225 -- setting the proper validity status for this entity, which depends on
226 -- the kind of parameter and the validity checking mode.
228 ---------------------------------------------
229 -- Analyze_Abstract_Subprogram_Declaration --
230 ---------------------------------------------
232 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
233 Scop : constant Entity_Id := Current_Scope;
234 Subp_Id : constant Entity_Id :=
235 Analyze_Subprogram_Specification (Specification (N));
238 Generate_Definition (Subp_Id);
240 -- Set the SPARK mode from the current context (may be overwritten later
241 -- with explicit pragma).
243 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
244 Set_SPARK_Pragma_Inherited (Subp_Id);
246 -- Preserve relevant elaboration-related attributes of the context which
247 -- are no longer available or very expensive to recompute once analysis,
248 -- resolution, and expansion are over.
250 Mark_Elaboration_Attributes
255 Set_Is_Abstract_Subprogram (Subp_Id);
256 New_Overloaded_Entity (Subp_Id);
257 Check_Delayed_Subprogram (Subp_Id);
259 Set_Categorization_From_Scope (Subp_Id, Scop);
261 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
262 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
264 -- Issue a warning if the abstract subprogram is neither a dispatching
265 -- operation nor an operation that overrides an inherited subprogram or
266 -- predefined operator, since this most likely indicates a mistake.
268 elsif Warn_On_Redundant_Constructs
269 and then not Is_Dispatching_Operation (Subp_Id)
270 and then not Present (Overridden_Operation (Subp_Id))
271 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
272 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
275 ("abstract subprogram is not dispatching or overriding?r?", N);
278 Generate_Reference_To_Formals (Subp_Id);
279 Check_Eliminated (Subp_Id);
281 if Has_Aspects (N) then
282 Analyze_Aspect_Specifications (N, Subp_Id);
284 end Analyze_Abstract_Subprogram_Declaration;
286 ---------------------------------
287 -- Analyze_Expression_Function --
288 ---------------------------------
290 procedure Analyze_Expression_Function (N : Node_Id) is
291 Expr : constant Node_Id := Expression (N);
292 Loc : constant Source_Ptr := Sloc (N);
293 LocX : constant Source_Ptr := Sloc (Expr);
294 Spec : constant Node_Id := Specification (N);
304 Def_Id : Entity_Id := Empty;
306 -- If the expression is a completion, Prev is the entity whose
307 -- declaration is completed. Def_Id is needed to analyze the spec.
310 -- This is one of the occasions on which we transform the tree during
311 -- semantic analysis. If this is a completion, transform the expression
312 -- function into an equivalent subprogram body, and analyze it.
314 -- Expression functions are inlined unconditionally. The back-end will
315 -- determine whether this is possible.
317 Inline_Processing_Required := True;
319 -- Create a specification for the generated body. This must be done
320 -- prior to the analysis of the initial declaration.
322 New_Spec := Copy_Subprogram_Spec (Spec);
323 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
325 -- If there are previous overloadable entities with the same name,
326 -- check whether any of them is completed by the expression function.
327 -- In a generic context a formal subprogram has no completion.
330 and then Is_Overloadable (Prev)
331 and then not Is_Formal_Subprogram (Prev)
333 Def_Id := Analyze_Subprogram_Specification (Spec);
334 Prev := Find_Corresponding_Spec (N);
336 -- The previous entity may be an expression function as well, in
337 -- which case the redeclaration is illegal.
340 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
341 N_Expression_Function
343 Error_Msg_Sloc := Sloc (Prev);
344 Error_Msg_N ("& conflicts with declaration#", Def_Id);
349 Ret := Make_Simple_Return_Statement (LocX, Expr);
352 Make_Subprogram_Body (Loc,
353 Specification => New_Spec,
354 Declarations => Empty_List,
355 Handled_Statement_Sequence =>
356 Make_Handled_Sequence_Of_Statements (LocX,
357 Statements => New_List (Ret)));
358 Set_Was_Expression_Function (New_Body);
360 -- If the expression completes a generic subprogram, we must create a
361 -- separate node for the body, because at instantiation the original
362 -- node of the generic copy must be a generic subprogram body, and
363 -- cannot be a expression function. Otherwise we just rewrite the
364 -- expression with the non-generic body.
366 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
367 Insert_After (N, New_Body);
369 -- Propagate any aspects or pragmas that apply to the expression
370 -- function to the proper body when the expression function acts
373 if Has_Aspects (N) then
374 Move_Aspects (N, To => New_Body);
377 Relocate_Pragmas_To_Body (New_Body);
379 Rewrite (N, Make_Null_Statement (Loc));
380 Set_Has_Completion (Prev, False);
383 Set_Is_Inlined (Prev);
385 -- If the expression function is a completion, the previous declaration
386 -- must come from source. We know already that it appears in the current
387 -- scope. The entity itself may be internally created if within a body
391 and then Is_Overloadable (Prev)
392 and then not Is_Formal_Subprogram (Prev)
393 and then Comes_From_Source (Parent (Prev))
395 Set_Has_Completion (Prev, False);
396 Set_Is_Inlined (Prev);
398 -- AI12-0103: Expression functions that are a completion freeze their
399 -- expression but don't freeze anything else (unlike regular bodies).
401 -- Note that we cannot defer this freezing to the analysis of the
402 -- expression itself, because a freeze node might appear in a nested
403 -- scope, leading to an elaboration order issue in gigi.
404 -- As elsewhere, we do not emit freeze nodes within a generic unit.
406 if not Inside_A_Generic then
409 Typ => Etype (Def_Id),
414 -- For navigation purposes, indicate that the function is a body
416 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
417 Rewrite (N, New_Body);
419 -- Remove any existing aspects from the original node because the act
420 -- of rewriting causes the list to be shared between the two nodes.
422 Orig_N := Original_Node (N);
423 Remove_Aspects (Orig_N);
425 -- Propagate any pragmas that apply to expression function to the
426 -- proper body when the expression function acts as a completion.
427 -- Aspects are automatically transfered because of node rewriting.
429 Relocate_Pragmas_To_Body (N);
432 -- Prev is the previous entity with the same name, but it is can
433 -- be an unrelated spec that is not completed by the expression
434 -- function. In that case the relevant entity is the one in the body.
435 -- Not clear that the backend can inline it in this case ???
437 if Has_Completion (Prev) then
439 -- The formals of the expression function are body formals,
440 -- and do not appear in the ali file, which will only contain
441 -- references to the formals of the original subprogram spec.
448 F1 := First_Formal (Def_Id);
449 F2 := First_Formal (Prev);
451 while Present (F1) loop
452 Set_Spec_Entity (F1, F2);
459 Set_Is_Inlined (Defining_Entity (New_Body));
462 -- If this is not a completion, create both a declaration and a body, so
463 -- that the expression can be inlined whenever possible.
466 -- An expression function that is not a completion is not a
467 -- subprogram declaration, and thus cannot appear in a protected
470 if Nkind (Parent (N)) = N_Protected_Definition then
472 ("an expression function is not a legal protected operation", N);
475 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
477 -- Remove any existing aspects from the original node because the act
478 -- of rewriting causes the list to be shared between the two nodes.
480 Orig_N := Original_Node (N);
481 Remove_Aspects (Orig_N);
485 -- If aspect SPARK_Mode was specified on the body, it needs to be
486 -- repeated both on the generated spec and the body.
488 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
490 if Present (Asp) then
491 Asp := New_Copy_Tree (Asp);
492 Set_Analyzed (Asp, False);
493 Set_Aspect_Specifications (New_Body, New_List (Asp));
496 Def_Id := Defining_Entity (N);
497 Set_Is_Inlined (Def_Id);
499 -- Establish the linkages between the spec and the body. These are
500 -- used when the expression function acts as the prefix of attribute
501 -- 'Access in order to freeze the original expression which has been
502 -- moved to the generated body.
504 Set_Corresponding_Body (N, Defining_Entity (New_Body));
505 Set_Corresponding_Spec (New_Body, Def_Id);
507 -- Within a generic preanalyze the original expression for name
508 -- capture. The body is also generated but plays no role in
509 -- this because it is not part of the original source.
510 -- If this is an ignored Ghost entity, analysis of the generated
511 -- body is needed to hide external references (as is done in
512 -- Analyze_Subprogram_Body) after which the the subprogram profile
513 -- can be frozen, which is needed to expand calls to such an ignored
516 if Inside_A_Generic then
517 Set_Has_Completion (Def_Id, not Is_Ignored_Ghost_Entity (Def_Id));
519 Install_Formals (Def_Id);
520 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
524 -- To prevent premature freeze action, insert the new body at the end
525 -- of the current declarations, or at the end of the package spec.
526 -- However, resolve usage names now, to prevent spurious visibility
527 -- on later entities. Note that the function can now be called in
528 -- the current declarative part, which will appear to be prior to
529 -- the presence of the body in the code. There are nevertheless no
530 -- order of elaboration issues because all name resolution has taken
531 -- place at the point of declaration.
534 Decls : List_Id := List_Containing (N);
535 Expr : constant Node_Id := Expression (Ret);
536 Par : constant Node_Id := Parent (Decls);
537 Typ : constant Entity_Id := Etype (Def_Id);
540 -- If this is a wrapper created for in an instance for a formal
541 -- subprogram, insert body after declaration, to be analyzed when
542 -- the enclosing instance is analyzed.
545 and then Is_Generic_Actual_Subprogram (Def_Id)
547 Insert_After (N, New_Body);
550 if Nkind (Par) = N_Package_Specification
551 and then Decls = Visible_Declarations (Par)
552 and then Present (Private_Declarations (Par))
553 and then not Is_Empty_List (Private_Declarations (Par))
555 Decls := Private_Declarations (Par);
558 Insert_After (Last (Decls), New_Body);
560 -- Preanalyze the expression if not already done above
562 if not Inside_A_Generic then
564 Install_Formals (Def_Id);
565 Preanalyze_Formal_Expression (Expr, Typ);
566 Check_Limited_Return (Original_Node (N), Expr, Typ);
570 -- In the case of an expression function marked with the
571 -- aspect Static, we need to check the requirement that the
572 -- function's expression is a potentially static expression.
573 -- This is done by making a full copy of the expression tree
574 -- and performing a special preanalysis on that tree with
575 -- the global flag Checking_Potentially_Static_Expression
576 -- enabled. If the resulting expression is static, then it's
577 -- OK, but if not, that means the expression violates the
578 -- requirements of the Ada 202x RM in 4.9(3.2/5-3.4/5) and
581 if Is_Static_Function (Def_Id) then
582 if not Is_Static_Expression (Expr) then
584 Exp_Copy : constant Node_Id := New_Copy_Tree (Expr);
586 Set_Checking_Potentially_Static_Expression (True);
588 Preanalyze_Formal_Expression (Exp_Copy, Typ);
590 if not Is_Static_Expression (Exp_Copy) then
592 ("static expression function requires "
593 & "potentially static expression", Expr);
596 Set_Checking_Potentially_Static_Expression (False);
600 -- We also make an additional copy of the expression and
601 -- replace the expression of the expression function with
602 -- this copy, because the currently present expression is
603 -- now associated with the body created for the static
604 -- expression function, which will later be analyzed and
605 -- possibly rewritten, and we need to have the separate
606 -- unanalyzed copy available for use with later static
610 (Original_Node (Subprogram_Spec (Def_Id)),
611 New_Copy_Tree (Expr));
613 -- Mark static expression functions as inlined, to ensure
614 -- that even calls with nonstatic actuals will be inlined.
616 Set_Has_Pragma_Inline (Def_Id);
617 Set_Is_Inlined (Def_Id);
623 -- Check incorrect use of dynamically tagged expression. This doesn't
624 -- fall out automatically when analyzing the generated function body,
625 -- because Check_Dynamically_Tagged_Expression deliberately ignores
626 -- nodes that don't come from source.
629 and then Nkind (Def_Id) in N_Has_Etype
630 and then Is_Tagged_Type (Etype (Def_Id))
632 Check_Dynamically_Tagged_Expression
634 Typ => Etype (Def_Id),
635 Related_Nod => Original_Node (N));
638 -- We must enforce checks for unreferenced formals in our newly
639 -- generated function, so we propagate the referenced flag from the
640 -- original spec to the new spec as well as setting Comes_From_Source.
642 if Present (Parameter_Specifications (New_Spec)) then
644 Form_New_Def : Entity_Id;
645 Form_New_Spec : Entity_Id;
646 Form_Old_Def : Entity_Id;
647 Form_Old_Spec : Entity_Id;
650 Form_New_Spec := First (Parameter_Specifications (New_Spec));
651 Form_Old_Spec := First (Parameter_Specifications (Spec));
653 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
654 Form_New_Def := Defining_Identifier (Form_New_Spec);
655 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
657 Set_Comes_From_Source (Form_New_Def, True);
659 -- Because of the usefulness of unreferenced controlling
660 -- formals we exempt them from unreferenced warnings by marking
661 -- them as always referenced.
663 Set_Referenced (Form_Old_Def,
664 (Is_Formal (Form_Old_Def)
665 and then Is_Controlling_Formal (Form_Old_Def))
666 or else Referenced (Form_Old_Def));
668 Next (Form_New_Spec);
669 Next (Form_Old_Spec);
673 end Analyze_Expression_Function;
675 ---------------------------------------
676 -- Analyze_Extended_Return_Statement --
677 ---------------------------------------
679 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
681 Check_Compiler_Unit ("extended return statement", N);
682 Analyze_Return_Statement (N);
683 end Analyze_Extended_Return_Statement;
685 ----------------------------
686 -- Analyze_Function_Call --
687 ----------------------------
689 procedure Analyze_Function_Call (N : Node_Id) is
690 Actuals : constant List_Id := Parameter_Associations (N);
691 Func_Nam : constant Node_Id := Name (N);
697 -- A call of the form A.B (X) may be an Ada 2005 call, which is
698 -- rewritten as B (A, X). If the rewriting is successful, the call
699 -- has been analyzed and we just return.
701 if Nkind (Func_Nam) = N_Selected_Component
702 and then Name (N) /= Func_Nam
703 and then Is_Rewrite_Substitution (N)
704 and then Present (Etype (N))
709 -- If error analyzing name, then set Any_Type as result type and return
711 if Etype (Func_Nam) = Any_Type then
712 Set_Etype (N, Any_Type);
716 -- Otherwise analyze the parameters
718 if Present (Actuals) then
719 Actual := First (Actuals);
720 while Present (Actual) loop
722 Check_Parameterless_Call (Actual);
728 end Analyze_Function_Call;
730 -----------------------------
731 -- Analyze_Function_Return --
732 -----------------------------
734 procedure Analyze_Function_Return (N : Node_Id) is
735 Loc : constant Source_Ptr := Sloc (N);
736 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
737 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
739 R_Type : constant Entity_Id := Etype (Scope_Id);
740 -- Function result subtype
742 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
743 -- Ada 2020: Check that the return expression in a No_Return function
744 -- meets the conditions specified by RM 6.5.1(5.1/5).
746 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id);
747 -- Apply legality rule of 6.5 (5.9) to the access discriminants of an
748 -- aggregate in a return statement.
750 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
751 -- Check that the return_subtype_indication properly matches the result
752 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
754 --------------------------------
755 -- Check_No_Return_Expression --
756 --------------------------------
758 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
759 Kind : constant Node_Kind := Nkind (Return_Expr);
762 if Kind = N_Raise_Expression then
765 elsif Kind = N_Function_Call
766 and then Is_Entity_Name (Name (Return_Expr))
767 and then Ekind (Entity (Name (Return_Expr))) in
768 E_Function | E_Generic_Function
769 and then No_Return (Entity (Name (Return_Expr)))
775 ("illegal expression in RETURN statement of No_Return function",
778 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
780 end Check_No_Return_Expression;
782 ------------------------------------------
783 -- Check_Return_Construct_Accessibility --
784 ------------------------------------------
786 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id) is
787 Return_Con : Node_Id;
788 Assoc : Node_Id := Empty;
789 Assoc_Expr : Node_Id;
795 -- Only perform checks on record types with access discriminants and
796 -- non-internally generated functions.
798 if not Is_Record_Type (R_Type)
799 or else not Has_Anonymous_Access_Discriminant (R_Type)
800 or else not Comes_From_Source (Return_Stmt)
805 -- We are only interested in return statements
807 if Nkind (Return_Stmt) not in
808 N_Extended_Return_Statement | N_Simple_Return_Statement
813 -- Fetch the object from the return statement, in the case of a
814 -- simple return statement the expression is part of the node.
816 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
817 -- Obtain the object definition from the expanded extended return
819 Return_Con := First (Return_Object_Declarations (Return_Stmt));
820 while Present (Return_Con) loop
821 -- Inspect the original node to avoid object declarations
822 -- expanded into renamings.
824 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
825 and then Comes_From_Source (Original_Node (Return_Con))
830 Nlists.Next (Return_Con);
833 pragma Assert (Present (Return_Con));
835 -- Could be dealing with a renaming
837 Return_Con := Original_Node (Return_Con);
839 Return_Con := Expression (Return_Stmt);
842 -- Obtain the accessibility levels of the expressions associated
843 -- with all anonymous access discriminants, then generate a
844 -- dynamic check or static error when relevant.
846 Unqual := Unqualify (Original_Node (Return_Con));
848 -- Obtain the corresponding declaration based on the return object's
851 if Nkind (Unqual) = N_Identifier
852 and then Nkind (Parent (Entity (Unqual)))
853 in N_Object_Declaration
854 | N_Object_Renaming_Declaration
856 Obj_Decl := Original_Node (Parent (Entity (Unqual)));
858 -- We were passed the object declaration directly, so use it
860 elsif Nkind (Unqual) in N_Object_Declaration
861 | N_Object_Renaming_Declaration
865 -- Otherwise, we are looking at something else
872 -- Hop up object renamings when present
874 if Present (Obj_Decl)
875 and then Nkind (Obj_Decl) = N_Object_Renaming_Declaration
877 while Nkind (Obj_Decl) = N_Object_Renaming_Declaration loop
879 if Nkind (Name (Obj_Decl)) not in N_Entity then
880 -- We may be looking at the expansion of iterators or
881 -- some other internally generated construct, so it is safe
882 -- to ignore checks ???
884 if not Comes_From_Source (Obj_Decl) then
888 Obj_Decl := Original_Node
890 (Ultimate_Prefix (Name (Obj_Decl))));
892 -- Move up to the next declaration based on the object's name
895 Obj_Decl := Original_Node
896 (Declaration_Node (Name (Obj_Decl)));
901 -- Obtain the discriminant values from the return aggregate
903 -- Do we cover extension aggregates correctly ???
905 if Nkind (Unqual) = N_Aggregate then
906 if Present (Expressions (Unqual)) then
907 Assoc := First (Expressions (Unqual));
909 Assoc := First (Component_Associations (Unqual));
912 -- There is an object declaration for the return object
914 elsif Present (Obj_Decl) then
915 -- When a subtype indication is present in an object declaration
916 -- it must contain the object's discriminants.
918 if Nkind (Object_Definition (Obj_Decl)) = N_Subtype_Indication then
922 (Object_Definition (Obj_Decl))));
924 -- The object declaration contains an aggregate
926 elsif Present (Expression (Obj_Decl)) then
928 if Nkind (Unqualify (Expression (Obj_Decl))) = N_Aggregate then
929 -- Grab the first associated discriminant expresion
932 (Expressions (Unqualify (Expression (Obj_Decl))))
936 (Unqualify (Expression (Obj_Decl))));
939 (Component_Associations
940 (Unqualify (Expression (Obj_Decl))));
943 -- Otherwise, this is something else
949 -- There are no supplied discriminants in the object declaration,
950 -- so get them from the type definition since they must be default
953 -- Do we handle constrained subtypes correctly ???
955 elsif Nkind (Unqual) = N_Object_Declaration then
956 Assoc := First_Discriminant
957 (Etype (Object_Definition (Obj_Decl)));
960 Assoc := First_Discriminant (Etype (Unqual));
963 -- When we are not looking at an aggregate or an identifier, return
964 -- since any other construct (like a function call) is not
965 -- applicable since checks will be performed on the side of the
972 -- Obtain the discriminants so we know the actual type in case the
973 -- value of their associated expression gets implicitly converted.
975 if No (Obj_Decl) then
976 pragma Assert (Nkind (Unqual) = N_Aggregate);
978 Disc := First_Discriminant (Etype (Unqual));
981 Disc := First_Discriminant
982 (Etype (Defining_Identifier (Obj_Decl)));
985 -- Loop through each of the discriminants and check each expression
986 -- associated with an anonymous access discriminant.
988 while Present (Assoc) and then Present (Disc) loop
989 -- Unwrap the associated expression
992 in N_Component_Association | N_Discriminant_Association
994 Assoc_Expr := Expression (Assoc);
996 elsif Nkind (Assoc) in N_Entity
997 and then Ekind (Assoc) = E_Discriminant
999 Assoc_Expr := Discriminant_Default_Value (Assoc);
1002 Assoc_Expr := Assoc;
1005 -- Check the accessibility level of the expression when the
1006 -- discriminant is of an anonymous access type.
1008 if Present (Assoc_Expr)
1009 and then Ekind (Etype (Disc)) = E_Anonymous_Access_Type
1011 -- Perform a static check first, if possible
1013 if Static_Accessibility_Level
1014 (Expr => Assoc_Expr,
1015 Level => Zero_On_Dynamic_Level,
1016 In_Return_Context => True)
1017 > Scope_Depth (Scope (Scope_Id))
1020 ("access discriminant in return object would be a dangling"
1021 & " reference", Return_Stmt);
1026 -- Otherwise, generate a dynamic check based on the extra
1027 -- accessibility of the result.
1029 if Present (Extra_Accessibility_Of_Result (Scope_Id)) then
1030 Insert_Before_And_Analyze (Return_Stmt,
1031 Make_Raise_Program_Error (Loc,
1034 Left_Opnd => Accessibility_Level
1035 (Expr => Assoc_Expr,
1036 Level => Dynamic_Level,
1037 In_Return_Context => True),
1038 Right_Opnd => Extra_Accessibility_Of_Result
1040 Reason => PE_Accessibility_Check_Failed));
1044 -- Iterate over the discriminants
1046 Disc := Next_Discriminant (Disc);
1047 if not Is_List_Member (Assoc) then
1050 Nlists.Next (Assoc);
1053 end Check_Return_Construct_Accessibility;
1055 -------------------------------------
1056 -- Check_Return_Subtype_Indication --
1057 -------------------------------------
1059 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
1060 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
1062 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
1063 -- Subtype given in the extended return statement (must match R_Type)
1065 Subtype_Ind : constant Node_Id :=
1066 Object_Definition (Original_Node (Obj_Decl));
1068 procedure Error_No_Match (N : Node_Id);
1069 -- Output error messages for case where types do not statically
1070 -- match. N is the location for the messages.
1072 --------------------
1073 -- Error_No_Match --
1074 --------------------
1076 procedure Error_No_Match (N : Node_Id) is
1079 ("subtype must statically match function result subtype", N);
1081 if not Predicates_Match (R_Stm_Type, R_Type) then
1082 Error_Msg_Node_2 := R_Type;
1084 ("\predicate of& does not match predicate of&",
1089 -- Start of processing for Check_Return_Subtype_Indication
1092 -- First, avoid cascaded errors
1094 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
1098 -- "return access T" case; check that the return statement also has
1099 -- "access T", and that the subtypes statically match:
1100 -- if this is an access to subprogram the signatures must match.
1102 if Is_Anonymous_Access_Type (R_Type) then
1103 if Is_Anonymous_Access_Type (R_Stm_Type) then
1104 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
1106 if Base_Type (Designated_Type (R_Stm_Type)) /=
1107 Base_Type (Designated_Type (R_Type))
1108 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
1110 Error_No_Match (Subtype_Mark (Subtype_Ind));
1114 -- For two anonymous access to subprogram types, the types
1115 -- themselves must be type conformant.
1117 if not Conforming_Types
1118 (R_Stm_Type, R_Type, Fully_Conformant)
1120 Error_No_Match (Subtype_Ind);
1125 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
1128 -- If the return object is of an anonymous access type, then report
1129 -- an error if the function's result type is not also anonymous.
1131 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1132 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1134 ("anonymous access not allowed for function with named access "
1135 & "result", Subtype_Ind);
1137 -- Subtype indication case: check that the return object's type is
1138 -- covered by the result type, and that the subtypes statically match
1139 -- when the result subtype is constrained. Also handle record types
1140 -- with unknown discriminants for which we have built the underlying
1141 -- record view. Coverage is needed to allow specific-type return
1142 -- objects when the result type is class-wide (see AI05-32).
1144 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1145 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1148 (Base_Type (R_Type),
1149 Underlying_Record_View (Base_Type (R_Stm_Type))))
1151 -- A null exclusion may be present on the return type, on the
1152 -- function specification, on the object declaration or on the
1155 if Is_Access_Type (R_Type)
1157 (Can_Never_Be_Null (R_Type)
1158 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1159 Can_Never_Be_Null (R_Stm_Type)
1161 Error_No_Match (Subtype_Ind);
1164 -- AI05-103: for elementary types, subtypes must statically match
1166 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1167 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1168 Error_No_Match (Subtype_Ind);
1172 -- All remaining cases are illegal
1174 -- Note: previous versions of this subprogram allowed the return
1175 -- value to be the ancestor of the return type if the return type
1176 -- was a null extension. This was plainly incorrect.
1180 ("wrong type for return_subtype_indication", Subtype_Ind);
1182 end Check_Return_Subtype_Indication;
1184 ---------------------
1185 -- Local Variables --
1186 ---------------------
1189 Obj_Decl : Node_Id := Empty;
1191 -- Start of processing for Analyze_Function_Return
1194 Set_Return_Present (Scope_Id);
1196 if Nkind (N) = N_Simple_Return_Statement then
1197 Expr := Expression (N);
1199 -- Guard against a malformed expression. The parser may have tried to
1200 -- recover but the node is not analyzable.
1202 if Nkind (Expr) = N_Error then
1203 Set_Etype (Expr, Any_Type);
1204 Expander_Mode_Save_And_Set (False);
1208 -- The resolution of a controlled [extension] aggregate associated
1209 -- with a return statement creates a temporary which needs to be
1210 -- finalized on function exit. Wrap the return statement inside a
1211 -- block so that the finalization machinery can detect this case.
1212 -- This early expansion is done only when the return statement is
1213 -- not part of a handled sequence of statements.
1215 if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
1216 and then Needs_Finalization (R_Type)
1217 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1220 Make_Block_Statement (Loc,
1221 Handled_Statement_Sequence =>
1222 Make_Handled_Sequence_Of_Statements (Loc,
1223 Statements => New_List (Relocate_Node (N)))));
1231 -- Ada 2005 (AI-251): If the type of the returned object is
1232 -- an access to an interface type then we add an implicit type
1233 -- conversion to force the displacement of the "this" pointer to
1234 -- reference the secondary dispatch table. We cannot delay the
1235 -- generation of this implicit conversion until the expansion
1236 -- because in this case the type resolution changes the decoration
1237 -- of the expression node to match R_Type; by contrast, if the
1238 -- returned object is a class-wide interface type then it is too
1239 -- early to generate here the implicit conversion since the return
1240 -- statement may be rewritten by the expander into an extended
1241 -- return statement whose expansion takes care of adding the
1242 -- implicit type conversion to displace the pointer to the object.
1245 and then Serious_Errors_Detected = 0
1246 and then Is_Access_Type (R_Type)
1247 and then Nkind (Expr) not in N_Null | N_Raise_Expression
1248 and then Is_Interface (Designated_Type (R_Type))
1249 and then Is_Progenitor (Designated_Type (R_Type),
1250 Designated_Type (Etype (Expr)))
1252 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1256 Resolve (Expr, R_Type);
1257 Check_Limited_Return (N, Expr, R_Type);
1259 Check_Return_Construct_Accessibility (N);
1261 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1262 -- nonreturning function shall be a simple_return_statement with
1263 -- an expression that is a raise_expression, or else a call on a
1264 -- nonreturning function, or else a parenthesized expression of
1267 if Ada_Version >= Ada_2020
1268 and then No_Return (Scope_Id)
1269 and then Comes_From_Source (N)
1271 Check_No_Return_Expression (Original_Node (Expr));
1275 Obj_Decl := Last (Return_Object_Declarations (N));
1277 -- Analyze parts specific to extended_return_statement:
1280 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1281 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1284 Expr := Expression (Obj_Decl);
1286 -- Note: The check for OK_For_Limited_Init will happen in
1287 -- Analyze_Object_Declaration; we treat it as a normal
1288 -- object declaration.
1290 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1292 -- Returning a build-in-place unconstrained array type we defer
1293 -- the full analysis of the returned object to avoid generating
1294 -- the corresponding constrained subtype; otherwise the bounds
1295 -- would be created in the stack and a dangling reference would
1296 -- be returned pointing to the bounds. We perform its preanalysis
1297 -- to report errors on the initializing aggregate now (if any);
1298 -- we also ensure its activation chain and Master variable are
1299 -- defined (if tasks are being declared) since they are generated
1300 -- as part of the analysis and expansion of the object declaration
1303 if Is_Array_Type (R_Type)
1304 and then not Is_Constrained (R_Type)
1305 and then Is_Build_In_Place_Function (Scope_Id)
1306 and then Needs_BIP_Alloc_Form (Scope_Id)
1307 and then Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
1309 Preanalyze (Obj_Decl);
1311 if Expander_Active then
1312 Ensure_Activation_Chain_And_Master (Obj_Decl);
1319 Check_Return_Subtype_Indication (Obj_Decl);
1321 if Present (HSS) then
1324 if Present (Exception_Handlers (HSS)) then
1326 -- ???Has_Nested_Block_With_Handler needs to be set.
1327 -- Probably by creating an actual N_Block_Statement.
1328 -- Probably in Expand.
1334 -- Mark the return object as referenced, since the return is an
1335 -- implicit reference of the object.
1337 Set_Referenced (Defining_Identifier (Obj_Decl));
1339 Check_References (Stm_Entity);
1341 Check_Return_Construct_Accessibility (N);
1343 -- Check RM 6.5 (5.9/3)
1346 if Ada_Version < Ada_2012 then
1348 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1349 -- Can it really happen (extended return???)
1352 ("aliased only allowed for limited return objects "
1353 & "in Ada 2012??", N);
1355 elsif not Is_Limited_View (R_Type) then
1357 ("aliased only allowed for limited return objects", N);
1361 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1362 -- nonreturning function shall be a simple_return_statement.
1364 if Ada_Version >= Ada_2020
1365 and then No_Return (Scope_Id)
1366 and then Comes_From_Source (N)
1369 ("extended RETURN statement not allowed in No_Return "
1375 -- Case of Expr present
1377 if Present (Expr) then
1379 -- Defend against previous errors
1381 if Nkind (Expr) = N_Empty
1382 or else No (Etype (Expr))
1387 -- Apply constraint check. Note that this is done before the implicit
1388 -- conversion of the expression done for anonymous access types to
1389 -- ensure correct generation of the null-excluding check associated
1390 -- with null-excluding expressions found in return statements. We
1391 -- don't need a check if the subtype of the return object is the
1392 -- same as the result subtype of the function.
1394 if Nkind (N) /= N_Extended_Return_Statement
1395 or else Nkind (Obj_Decl) /= N_Object_Declaration
1396 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
1397 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
1399 Apply_Constraint_Check (Expr, R_Type);
1402 -- The return value is converted to the return type of the function,
1403 -- which implies a predicate check if the return type is predicated.
1404 -- We do not apply the check for an extended return statement because
1405 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
1406 -- We do not apply the check to a case expression because it will
1407 -- be expanded into a series of return statements, each of which
1408 -- will receive a predicate check.
1410 if Nkind (N) /= N_Extended_Return_Statement
1411 and then Nkind (Expr) /= N_Case_Expression
1413 Apply_Predicate_Check (Expr, R_Type);
1416 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1417 -- type, apply an implicit conversion of the expression to that type
1418 -- to force appropriate static and run-time accessibility checks.
1419 -- But we want to apply the checks to an extended return statement
1420 -- only once, i.e. not to the simple return statement generated at
1421 -- the end of its expansion because, prior to leaving the function,
1422 -- the accessibility level of the return object changes to be a level
1423 -- determined by the point of call (RM 3.10.2(10.8/3)).
1425 if Ada_Version >= Ada_2005
1426 and then Ekind (R_Type) = E_Anonymous_Access_Type
1427 and then (Nkind (N) = N_Extended_Return_Statement
1428 or else not Comes_From_Extended_Return_Statement (N))
1430 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1431 Analyze_And_Resolve (Expr, R_Type);
1433 -- If this is a local anonymous access to subprogram, the
1434 -- accessibility check can be applied statically. The return is
1435 -- illegal if the access type of the return expression is declared
1436 -- inside of the subprogram (except if it is the subtype indication
1437 -- of an extended return statement).
1439 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1440 if not Comes_From_Source (Current_Scope)
1441 or else Ekind (Current_Scope) = E_Return_Statement
1446 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1448 Error_Msg_N ("cannot return local access to subprogram", N);
1451 -- The expression cannot be of a formal incomplete type
1453 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1454 and then Is_Generic_Type (Etype (Expr))
1457 ("cannot return expression of a formal incomplete type", N);
1460 -- If the result type is class-wide, then check that the return
1461 -- expression's type is not declared at a deeper level than the
1462 -- function (RM05-6.5(5.6/2)).
1464 if Ada_Version >= Ada_2005
1465 and then Is_Class_Wide_Type (R_Type)
1467 if Type_Access_Level (Etype (Expr)) >
1468 Subprogram_Access_Level (Scope_Id)
1471 ("level of return expression type is deeper than "
1472 & "class-wide function!", Expr);
1476 -- Check incorrect use of dynamically tagged expression
1478 if Is_Tagged_Type (R_Type) then
1479 Check_Dynamically_Tagged_Expression
1485 -- ??? A real run-time accessibility check is needed in cases
1486 -- involving dereferences of access parameters. For now we just
1487 -- check the static cases.
1489 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1490 and then Is_Limited_View (Etype (Scope_Id))
1491 and then Static_Accessibility_Level (Expr, Zero_On_Dynamic_Level)
1492 > Subprogram_Access_Level (Scope_Id)
1494 -- Suppress the message in a generic, where the rewriting
1497 if Inside_A_Generic then
1502 Make_Raise_Program_Error (Loc,
1503 Reason => PE_Accessibility_Check_Failed));
1506 Error_Msg_Warn := SPARK_Mode /= On;
1507 Error_Msg_N ("cannot return a local value by reference<<", N);
1508 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1512 if Known_Null (Expr)
1513 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1514 and then Null_Exclusion_Present (Parent (Scope_Id))
1516 Apply_Compile_Time_Constraint_Error
1518 Msg => "(Ada 2005) null not allowed for "
1519 & "null-excluding return??",
1520 Reason => CE_Null_Not_Allowed);
1523 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1524 -- has no initializing expression.
1526 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1527 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1528 Subprogram_Access_Level (Scope_Id)
1531 ("level of return expression type is deeper than "
1532 & "class-wide function!", Obj_Decl);
1535 end Analyze_Function_Return;
1537 -------------------------------------
1538 -- Analyze_Generic_Subprogram_Body --
1539 -------------------------------------
1541 procedure Analyze_Generic_Subprogram_Body
1545 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1546 Kind : constant Entity_Kind := Ekind (Gen_Id);
1547 Body_Id : Entity_Id;
1552 -- Copy body and disable expansion while analyzing the generic For a
1553 -- stub, do not copy the stub (which would load the proper body), this
1554 -- will be done when the proper body is analyzed.
1556 if Nkind (N) /= N_Subprogram_Body_Stub then
1557 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1560 -- Once the contents of the generic copy and the template are
1561 -- swapped, do the same for their respective aspect specifications.
1563 Exchange_Aspects (N, New_N);
1565 -- Collect all contract-related source pragmas found within the
1566 -- template and attach them to the contract of the subprogram body.
1567 -- This contract is used in the capture of global references within
1570 Create_Generic_Contract (N);
1575 Spec := Specification (N);
1577 -- Within the body of the generic, the subprogram is callable, and
1578 -- behaves like the corresponding non-generic unit.
1580 Body_Id := Defining_Entity (Spec);
1582 if Kind = E_Generic_Procedure
1583 and then Nkind (Spec) /= N_Procedure_Specification
1585 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1588 elsif Kind = E_Generic_Function
1589 and then Nkind (Spec) /= N_Function_Specification
1591 Error_Msg_N ("invalid body for generic function ", Body_Id);
1595 Set_Corresponding_Body (Gen_Decl, Body_Id);
1597 if Has_Completion (Gen_Id)
1598 and then Nkind (Parent (N)) /= N_Subunit
1600 Error_Msg_N ("duplicate generic body", N);
1603 Set_Has_Completion (Gen_Id);
1606 if Nkind (N) = N_Subprogram_Body_Stub then
1607 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1609 Set_Corresponding_Spec (N, Gen_Id);
1612 if Nkind (Parent (N)) = N_Compilation_Unit then
1613 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1616 -- Make generic parameters immediately visible in the body. They are
1617 -- needed to process the formals declarations. Then make the formals
1618 -- visible in a separate step.
1620 Push_Scope (Gen_Id);
1624 First_Ent : Entity_Id;
1627 First_Ent := First_Entity (Gen_Id);
1630 while Present (E) and then not Is_Formal (E) loop
1635 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1637 -- Now generic formals are visible, and the specification can be
1638 -- analyzed, for subsequent conformance check.
1640 Body_Id := Analyze_Subprogram_Specification (Spec);
1642 -- Make formal parameters visible
1646 -- E is the first formal parameter, we loop through the formals
1647 -- installing them so that they will be visible.
1649 Set_First_Entity (Gen_Id, E);
1650 while Present (E) loop
1656 -- Visible generic entity is callable within its own body
1658 Set_Ekind (Gen_Id, Ekind (Body_Id));
1659 Set_Ekind (Body_Id, E_Subprogram_Body);
1660 Set_Convention (Body_Id, Convention (Gen_Id));
1661 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1662 Set_Scope (Body_Id, Scope (Gen_Id));
1664 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1666 if Nkind (N) = N_Subprogram_Body_Stub then
1668 -- No body to analyze, so restore state of generic unit
1670 Set_Ekind (Gen_Id, Kind);
1671 Set_Ekind (Body_Id, Kind);
1673 if Present (First_Ent) then
1674 Set_First_Entity (Gen_Id, First_Ent);
1681 -- If this is a compilation unit, it must be made visible explicitly,
1682 -- because the compilation of the declaration, unlike other library
1683 -- unit declarations, does not. If it is not a unit, the following
1684 -- is redundant but harmless.
1686 Set_Is_Immediately_Visible (Gen_Id);
1687 Reference_Body_Formals (Gen_Id, Body_Id);
1689 if Is_Child_Unit (Gen_Id) then
1690 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1693 Set_Actual_Subtypes (N, Current_Scope);
1695 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1696 Set_SPARK_Pragma_Inherited (Body_Id);
1698 -- Analyze any aspect specifications that appear on the generic
1701 if Has_Aspects (N) then
1702 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1705 Analyze_Declarations (Declarations (N));
1708 -- Process the contract of the subprogram body after all declarations
1709 -- have been analyzed. This ensures that any contract-related pragmas
1710 -- are available through the N_Contract node of the body.
1712 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1714 Analyze (Handled_Statement_Sequence (N));
1715 Save_Global_References (Original_Node (N));
1717 -- Prior to exiting the scope, include generic formals again (if any
1718 -- are present) in the set of local entities.
1720 if Present (First_Ent) then
1721 Set_First_Entity (Gen_Id, First_Ent);
1724 Check_References (Gen_Id);
1727 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1728 Update_Use_Clause_Chain;
1729 Validate_Categorization_Dependency (N, Gen_Id);
1731 Check_Subprogram_Order (N);
1733 -- Outside of its body, unit is generic again
1735 Set_Ekind (Gen_Id, Kind);
1736 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1739 Style.Check_Identifier (Body_Id, Gen_Id);
1743 end Analyze_Generic_Subprogram_Body;
1745 ----------------------------
1746 -- Analyze_Null_Procedure --
1747 ----------------------------
1749 -- WARNING: This routine manages Ghost regions. Return statements must be
1750 -- replaced by gotos that jump to the end of the routine and restore the
1753 procedure Analyze_Null_Procedure
1755 Is_Completion : out Boolean)
1757 Loc : constant Source_Ptr := Sloc (N);
1758 Spec : constant Node_Id := Specification (N);
1760 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1761 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1762 Saved_ISMP : constant Boolean :=
1763 Ignore_SPARK_Mode_Pragmas_In_Instance;
1764 -- Save the Ghost and SPARK mode-related data to restore on exit
1766 Designator : Entity_Id;
1768 Null_Body : Node_Id := Empty;
1769 Null_Stmt : Node_Id := Null_Statement (Spec);
1773 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1775 -- A null procedure is Ghost when it is stand-alone and is subject to
1776 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1777 -- now, to ensure that any nodes generated during analysis and expansion
1778 -- are properly marked as Ghost.
1780 if Present (Prev) then
1781 Mark_And_Set_Ghost_Body (N, Prev);
1784 -- Capture the profile of the null procedure before analysis, for
1785 -- expansion at the freeze point and at each point of call. The body is
1786 -- used if the procedure has preconditions, or if it is a completion. In
1787 -- the first case the body is analyzed at the freeze point, in the other
1788 -- it replaces the null procedure declaration.
1790 -- For a null procedure that comes from source, a NULL statement is
1791 -- provided by the parser, which carries the source location of the
1792 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1793 -- from expansion, create one now.
1795 if No (Null_Stmt) then
1796 Null_Stmt := Make_Null_Statement (Loc);
1800 Make_Subprogram_Body (Loc,
1801 Specification => New_Copy_Tree (Spec),
1802 Declarations => New_List,
1803 Handled_Statement_Sequence =>
1804 Make_Handled_Sequence_Of_Statements (Loc,
1805 Statements => New_List (Null_Stmt)));
1807 -- Create new entities for body and formals
1809 Set_Defining_Unit_Name (Specification (Null_Body),
1810 Make_Defining_Identifier
1811 (Sloc (Defining_Entity (N)),
1812 Chars (Defining_Entity (N))));
1814 Form := First (Parameter_Specifications (Specification (Null_Body)));
1815 while Present (Form) loop
1816 Set_Defining_Identifier (Form,
1817 Make_Defining_Identifier
1818 (Sloc (Defining_Identifier (Form)),
1819 Chars (Defining_Identifier (Form))));
1823 -- Determine whether the null procedure may be a completion of a generic
1824 -- suprogram, in which case we use the new null body as the completion
1825 -- and set minimal semantic information on the original declaration,
1826 -- which is rewritten as a null statement.
1828 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1829 Insert_Before (N, Null_Body);
1830 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1832 Rewrite (N, Make_Null_Statement (Loc));
1833 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1834 Is_Completion := True;
1839 -- Resolve the types of the formals now, because the freeze point may
1840 -- appear in a different context, e.g. an instantiation.
1842 Form := First (Parameter_Specifications (Specification (Null_Body)));
1843 while Present (Form) loop
1844 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1845 Find_Type (Parameter_Type (Form));
1847 elsif No (Access_To_Subprogram_Definition
1848 (Parameter_Type (Form)))
1850 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1852 -- The case of a null procedure with a formal that is an
1853 -- access-to-subprogram type, and that is used as an actual
1854 -- in an instantiation is left to the enthusiastic reader.
1864 -- If there are previous overloadable entities with the same name, check
1865 -- whether any of them is completed by the null procedure.
1867 if Present (Prev) and then Is_Overloadable (Prev) then
1868 Designator := Analyze_Subprogram_Specification (Spec);
1869 Prev := Find_Corresponding_Spec (N);
1872 if No (Prev) or else not Comes_From_Source (Prev) then
1873 Designator := Analyze_Subprogram_Specification (Spec);
1874 Set_Has_Completion (Designator);
1876 -- Signal to caller that this is a procedure declaration
1878 Is_Completion := False;
1880 -- Null procedures are always inlined, but generic formal subprograms
1881 -- which appear as such in the internal instance of formal packages,
1882 -- need no completion and are not marked Inline.
1885 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1887 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1888 Set_Body_To_Inline (N, Null_Body);
1889 Set_Is_Inlined (Designator);
1893 -- The null procedure is a completion. We unconditionally rewrite
1894 -- this as a null body (even if expansion is not active), because
1895 -- there are various error checks that are applied on this body
1896 -- when it is analyzed (e.g. correct aspect placement).
1898 if Has_Completion (Prev) then
1899 Error_Msg_Sloc := Sloc (Prev);
1900 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1903 Check_Previous_Null_Procedure (N, Prev);
1905 Is_Completion := True;
1906 Rewrite (N, Null_Body);
1911 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1912 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1913 end Analyze_Null_Procedure;
1915 -----------------------------
1916 -- Analyze_Operator_Symbol --
1917 -----------------------------
1919 -- An operator symbol such as "+" or "and" may appear in context where the
1920 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1921 -- is just a string, as in (conjunction = "or"). In these cases the parser
1922 -- generates this node, and the semantics does the disambiguation. Other
1923 -- such case are actuals in an instantiation, the generic unit in an
1924 -- instantiation, and pragma arguments.
1926 procedure Analyze_Operator_Symbol (N : Node_Id) is
1927 Par : constant Node_Id := Parent (N);
1930 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1931 or else Nkind (Par) = N_Function_Instantiation
1932 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1933 or else (Nkind (Par) = N_Pragma_Argument_Association
1934 and then not Is_Pragma_String_Literal (Par))
1935 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1936 or else (Nkind (Par) = N_Attribute_Reference
1937 and then Attribute_Name (Par) /= Name_Value)
1939 Find_Direct_Name (N);
1942 Change_Operator_Symbol_To_String_Literal (N);
1945 end Analyze_Operator_Symbol;
1947 -----------------------------------
1948 -- Analyze_Parameter_Association --
1949 -----------------------------------
1951 procedure Analyze_Parameter_Association (N : Node_Id) is
1953 Analyze (Explicit_Actual_Parameter (N));
1954 end Analyze_Parameter_Association;
1956 ----------------------------
1957 -- Analyze_Procedure_Call --
1958 ----------------------------
1960 -- WARNING: This routine manages Ghost regions. Return statements must be
1961 -- replaced by gotos that jump to the end of the routine and restore the
1964 procedure Analyze_Procedure_Call (N : Node_Id) is
1965 procedure Analyze_Call_And_Resolve;
1966 -- Do Analyze and Resolve calls for procedure call. At the end, check
1967 -- for illegal order dependence.
1968 -- ??? where is the check for illegal order dependencies?
1970 ------------------------------
1971 -- Analyze_Call_And_Resolve --
1972 ------------------------------
1974 procedure Analyze_Call_And_Resolve is
1976 if Nkind (N) = N_Procedure_Call_Statement then
1978 Resolve (N, Standard_Void_Type);
1982 end Analyze_Call_And_Resolve;
1986 Actuals : constant List_Id := Parameter_Associations (N);
1987 Loc : constant Source_Ptr := Sloc (N);
1988 P : constant Node_Id := Name (N);
1990 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1991 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1992 -- Save the Ghost-related attributes to restore on exit
1997 -- Start of processing for Analyze_Procedure_Call
2000 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
2001 -- a procedure call or an entry call. The prefix may denote an access
2002 -- to subprogram type, in which case an implicit dereference applies.
2003 -- If the prefix is an indexed component (without implicit dereference)
2004 -- then the construct denotes a call to a member of an entire family.
2005 -- If the prefix is a simple name, it may still denote a call to a
2006 -- parameterless member of an entry family. Resolution of these various
2007 -- interpretations is delicate.
2009 -- Do not analyze machine code statements to avoid rejecting them in
2012 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
2013 Set_Etype (P, Standard_Void_Type);
2018 -- If this is a call of the form Obj.Op, the call may have been analyzed
2019 -- and possibly rewritten into a block, in which case we are done.
2021 if Analyzed (N) then
2024 -- If there is an error analyzing the name (which may have been
2025 -- rewritten if the original call was in prefix notation) then error
2026 -- has been emitted already, mark node and return.
2028 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
2029 Set_Etype (N, Any_Type);
2033 -- A procedure call is Ghost when its name denotes a Ghost procedure.
2034 -- Set the mode now to ensure that any nodes generated during analysis
2035 -- and expansion are properly marked as Ghost.
2037 Mark_And_Set_Ghost_Procedure_Call (N);
2039 -- Otherwise analyze the parameters
2041 if Present (Actuals) then
2042 Actual := First (Actuals);
2044 while Present (Actual) loop
2046 Check_Parameterless_Call (Actual);
2051 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
2053 if Nkind (P) = N_Attribute_Reference
2054 and then Attribute_Name (P) in Name_Elab_Spec
2056 | Name_Elab_Subp_Body
2058 if Present (Actuals) then
2060 ("no parameters allowed for this call", First (Actuals));
2064 Set_Etype (N, Standard_Void_Type);
2067 elsif Is_Entity_Name (P)
2068 and then Is_Record_Type (Etype (Entity (P)))
2069 and then Remote_AST_I_Dereference (P)
2073 elsif Is_Entity_Name (P)
2074 and then Ekind (Entity (P)) /= E_Entry_Family
2076 if Is_Access_Type (Etype (P))
2077 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
2078 and then No (Actuals)
2079 and then Comes_From_Source (N)
2081 Error_Msg_N ("missing explicit dereference in call", N);
2083 elsif Ekind (Entity (P)) = E_Operator then
2084 Error_Msg_Name_1 := Chars (P);
2085 Error_Msg_N ("operator % cannot be used as a procedure", N);
2088 Analyze_Call_And_Resolve;
2090 -- If the prefix is the simple name of an entry family, this is a
2091 -- parameterless call from within the task body itself.
2093 elsif Is_Entity_Name (P)
2094 and then Nkind (P) = N_Identifier
2095 and then Ekind (Entity (P)) = E_Entry_Family
2096 and then Present (Actuals)
2097 and then No (Next (First (Actuals)))
2099 -- Can be call to parameterless entry family. What appears to be the
2100 -- sole argument is in fact the entry index. Rewrite prefix of node
2101 -- accordingly. Source representation is unchanged by this
2105 Make_Indexed_Component (Loc,
2107 Make_Selected_Component (Loc,
2108 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
2109 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
2110 Expressions => Actuals);
2111 Set_Name (N, New_N);
2112 Set_Etype (New_N, Standard_Void_Type);
2113 Set_Parameter_Associations (N, No_List);
2114 Analyze_Call_And_Resolve;
2116 elsif Nkind (P) = N_Explicit_Dereference then
2117 if Ekind (Etype (P)) = E_Subprogram_Type then
2118 Analyze_Call_And_Resolve;
2120 Error_Msg_N ("expect access to procedure in call", P);
2123 -- The name can be a selected component or an indexed component that
2124 -- yields an access to subprogram. Such a prefix is legal if the call
2125 -- has parameter associations.
2127 elsif Is_Access_Type (Etype (P))
2128 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
2130 if Present (Actuals) then
2131 Analyze_Call_And_Resolve;
2133 Error_Msg_N ("missing explicit dereference in call ", N);
2136 -- If not an access to subprogram, then the prefix must resolve to the
2137 -- name of an entry, entry family, or protected operation.
2139 -- For the case of a simple entry call, P is a selected component where
2140 -- the prefix is the task and the selector name is the entry. A call to
2141 -- a protected procedure will have the same syntax. If the protected
2142 -- object contains overloaded operations, the entity may appear as a
2143 -- function, the context will select the operation whose type is Void.
2145 elsif Nkind (P) = N_Selected_Component
2146 and then Ekind (Entity (Selector_Name (P)))
2147 in E_Entry | E_Function | E_Procedure
2149 -- When front-end inlining is enabled, as with SPARK_Mode, a call
2150 -- in prefix notation may still be missing its controlling argument,
2151 -- so perform the transformation now.
2153 if SPARK_Mode = On and then In_Inlined_Body then
2155 Subp : constant Entity_Id := Entity (Selector_Name (P));
2156 Typ : constant Entity_Id := Etype (Prefix (P));
2159 if Is_Tagged_Type (Typ)
2160 and then Present (First_Formal (Subp))
2161 and then (Etype (First_Formal (Subp)) = Typ
2163 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
2164 and then Try_Object_Operation (P)
2169 Analyze_Call_And_Resolve;
2174 Analyze_Call_And_Resolve;
2177 elsif Nkind (P) = N_Selected_Component
2178 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
2179 and then Present (Actuals)
2180 and then No (Next (First (Actuals)))
2182 -- Can be call to parameterless entry family. What appears to be the
2183 -- sole argument is in fact the entry index. Rewrite prefix of node
2184 -- accordingly. Source representation is unchanged by this
2188 Make_Indexed_Component (Loc,
2189 Prefix => New_Copy (P),
2190 Expressions => Actuals);
2191 Set_Name (N, New_N);
2192 Set_Etype (New_N, Standard_Void_Type);
2193 Set_Parameter_Associations (N, No_List);
2194 Analyze_Call_And_Resolve;
2196 -- For the case of a reference to an element of an entry family, P is
2197 -- an indexed component whose prefix is a selected component (task and
2198 -- entry family), and whose index is the entry family index.
2200 elsif Nkind (P) = N_Indexed_Component
2201 and then Nkind (Prefix (P)) = N_Selected_Component
2202 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
2204 Analyze_Call_And_Resolve;
2206 -- If the prefix is the name of an entry family, it is a call from
2207 -- within the task body itself.
2209 elsif Nkind (P) = N_Indexed_Component
2210 and then Nkind (Prefix (P)) = N_Identifier
2211 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
2214 Make_Selected_Component (Loc,
2216 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
2217 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
2218 Rewrite (Prefix (P), New_N);
2220 Analyze_Call_And_Resolve;
2222 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2223 -- procedure name, so the construct can only be a qualified expression.
2225 elsif Nkind (P) = N_Qualified_Expression
2226 and then Ada_Version >= Ada_2012
2228 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2231 -- Anything else is an error
2234 Error_Msg_N ("invalid procedure or entry call", N);
2238 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2239 end Analyze_Procedure_Call;
2241 ------------------------------
2242 -- Analyze_Return_Statement --
2243 ------------------------------
2245 procedure Analyze_Return_Statement (N : Node_Id) is
2247 (Nkind (N) in N_Extended_Return_Statement | N_Simple_Return_Statement);
2249 Returns_Object : constant Boolean :=
2250 Nkind (N) = N_Extended_Return_Statement
2252 (Nkind (N) = N_Simple_Return_Statement
2253 and then Present (Expression (N)));
2254 -- True if we're returning something; that is, "return <expression>;"
2255 -- or "return Result : T [:= ...]". False for "return;". Used for error
2256 -- checking: If Returns_Object is True, N should apply to a function
2257 -- body; otherwise N should apply to a procedure body, entry body,
2258 -- accept statement, or extended return statement.
2260 function Find_What_It_Applies_To return Entity_Id;
2261 -- Find the entity representing the innermost enclosing body, accept
2262 -- statement, or extended return statement. If the result is a callable
2263 -- construct or extended return statement, then this will be the value
2264 -- of the Return_Applies_To attribute. Otherwise, the program is
2265 -- illegal. See RM-6.5(4/2).
2267 -----------------------------
2268 -- Find_What_It_Applies_To --
2269 -----------------------------
2271 function Find_What_It_Applies_To return Entity_Id is
2272 Result : Entity_Id := Empty;
2275 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2276 -- and postconditions.
2278 for J in reverse 0 .. Scope_Stack.Last loop
2279 Result := Scope_Stack.Table (J).Entity;
2280 exit when Ekind (Result) not in E_Block | E_Loop
2281 and then Chars (Result) /= Name_uPostconditions;
2284 pragma Assert (Present (Result));
2286 end Find_What_It_Applies_To;
2288 -- Local declarations
2290 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2291 Kind : constant Entity_Kind := Ekind (Scope_Id);
2292 Loc : constant Source_Ptr := Sloc (N);
2293 Stm_Entity : constant Entity_Id :=
2295 (E_Return_Statement, Current_Scope, Loc, 'R');
2297 -- Start of processing for Analyze_Return_Statement
2300 Set_Return_Statement_Entity (N, Stm_Entity);
2302 Set_Etype (Stm_Entity, Standard_Void_Type);
2303 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2305 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2306 -- (4/2): an inner return statement will apply to this extended return.
2308 if Nkind (N) = N_Extended_Return_Statement then
2309 Push_Scope (Stm_Entity);
2312 -- Check that pragma No_Return is obeyed. Don't complain about the
2313 -- implicitly-generated return that is placed at the end.
2315 if No_Return (Scope_Id)
2316 and then Kind in E_Procedure | E_Generic_Procedure
2317 and then Comes_From_Source (N)
2320 ("RETURN statement not allowed in No_Return procedure", N);
2323 -- Warn on any unassigned OUT parameters if in procedure
2325 if Ekind (Scope_Id) = E_Procedure then
2326 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2329 -- Check that functions return objects, and other things do not
2331 if Kind in E_Function | E_Generic_Function then
2332 if not Returns_Object then
2333 Error_Msg_N ("missing expression in return from function", N);
2336 elsif Kind in E_Procedure | E_Generic_Procedure then
2337 if Returns_Object then
2338 Error_Msg_N ("procedure cannot return value (use function)", N);
2341 elsif Kind in E_Entry | E_Entry_Family then
2342 if Returns_Object then
2343 if Is_Protected_Type (Scope (Scope_Id)) then
2344 Error_Msg_N ("entry body cannot return value", N);
2346 Error_Msg_N ("accept statement cannot return value", N);
2350 elsif Kind = E_Return_Statement then
2352 -- We are nested within another return statement, which must be an
2353 -- extended_return_statement.
2355 if Returns_Object then
2356 if Nkind (N) = N_Extended_Return_Statement then
2358 ("extended return statement cannot be nested (use `RETURN;`)",
2361 -- Case of a simple return statement with a value inside extended
2362 -- return statement.
2366 ("return nested in extended return statement cannot return "
2367 & "value (use `RETURN;`)", N);
2372 Error_Msg_N ("illegal context for return statement", N);
2375 if Kind in E_Function | E_Generic_Function then
2376 Analyze_Function_Return (N);
2378 elsif Kind in E_Procedure | E_Generic_Procedure then
2379 Set_Return_Present (Scope_Id);
2382 if Nkind (N) = N_Extended_Return_Statement then
2386 Kill_Current_Values (Last_Assignment_Only => True);
2387 Check_Unreachable_Code (N);
2389 Analyze_Dimension (N);
2390 end Analyze_Return_Statement;
2392 -------------------------------------
2393 -- Analyze_Simple_Return_Statement --
2394 -------------------------------------
2396 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2398 if Present (Expression (N)) then
2399 Mark_Coextensions (N, Expression (N));
2402 Analyze_Return_Statement (N);
2403 end Analyze_Simple_Return_Statement;
2405 -------------------------
2406 -- Analyze_Return_Type --
2407 -------------------------
2409 procedure Analyze_Return_Type (N : Node_Id) is
2410 Designator : constant Entity_Id := Defining_Entity (N);
2411 Typ : Entity_Id := Empty;
2414 -- Normal case where result definition does not indicate an error
2416 if Result_Definition (N) /= Error then
2417 if Nkind (Result_Definition (N)) = N_Access_Definition then
2419 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2422 AD : constant Node_Id :=
2423 Access_To_Subprogram_Definition (Result_Definition (N));
2425 if Present (AD) and then Protected_Present (AD) then
2426 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2428 Typ := Access_Definition (N, Result_Definition (N));
2432 Set_Parent (Typ, Result_Definition (N));
2433 Set_Is_Local_Anonymous_Access (Typ);
2434 Set_Etype (Designator, Typ);
2436 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2438 Null_Exclusion_Static_Checks (N);
2440 -- Subtype_Mark case
2443 Find_Type (Result_Definition (N));
2444 Typ := Entity (Result_Definition (N));
2445 Set_Etype (Designator, Typ);
2447 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2449 Null_Exclusion_Static_Checks (N);
2451 -- If a null exclusion is imposed on the result type, then create
2452 -- a null-excluding itype (an access subtype) and use it as the
2453 -- function's Etype. Note that the null exclusion checks are done
2454 -- right before this, because they don't get applied to types that
2455 -- do not come from source.
2457 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2458 Set_Etype (Designator,
2459 Create_Null_Excluding_Itype
2462 Scope_Id => Scope (Current_Scope)));
2464 -- The new subtype must be elaborated before use because
2465 -- it is visible outside of the function. However its base
2466 -- type may not be frozen yet, so the reference that will
2467 -- force elaboration must be attached to the freezing of
2470 -- If the return specification appears on a proper body,
2471 -- the subtype will have been created already on the spec.
2473 if Is_Frozen (Typ) then
2474 if Nkind (Parent (N)) = N_Subprogram_Body
2475 and then Nkind (Parent (Parent (N))) = N_Subunit
2479 Build_Itype_Reference (Etype (Designator), Parent (N));
2483 Ensure_Freeze_Node (Typ);
2486 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2488 Set_Itype (IR, Etype (Designator));
2489 Append_Freeze_Actions (Typ, New_List (IR));
2494 Set_Etype (Designator, Typ);
2497 if Ekind (Typ) = E_Incomplete_Type
2498 or else (Is_Class_Wide_Type (Typ)
2499 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2501 -- AI05-0151: Tagged incomplete types are allowed in all formal
2502 -- parts. Untagged incomplete types are not allowed in bodies.
2503 -- As a consequence, limited views cannot appear in a basic
2504 -- declaration that is itself within a body, because there is
2505 -- no point at which the non-limited view will become visible.
2507 if Ada_Version >= Ada_2012 then
2508 if From_Limited_With (Typ) and then In_Package_Body then
2510 ("invalid use of incomplete type&",
2511 Result_Definition (N), Typ);
2513 -- The return type of a subprogram body cannot be of a
2514 -- formal incomplete type.
2516 elsif Is_Generic_Type (Typ)
2517 and then Nkind (Parent (N)) = N_Subprogram_Body
2520 ("return type cannot be a formal incomplete type",
2521 Result_Definition (N));
2523 elsif Is_Class_Wide_Type (Typ)
2524 and then Is_Generic_Type (Root_Type (Typ))
2525 and then Nkind (Parent (N)) = N_Subprogram_Body
2528 ("return type cannot be a formal incomplete type",
2529 Result_Definition (N));
2531 elsif Is_Tagged_Type (Typ) then
2534 -- Use is legal in a thunk generated for an operation
2535 -- inherited from a progenitor.
2537 elsif Is_Thunk (Designator)
2538 and then Present (Non_Limited_View (Typ))
2542 elsif Nkind (Parent (N)) = N_Subprogram_Body
2543 or else Nkind (Parent (Parent (N))) in
2544 N_Accept_Statement | N_Entry_Body
2547 ("invalid use of untagged incomplete type&",
2551 -- The type must be completed in the current package. This
2552 -- is checked at the end of the package declaration when
2553 -- Taft-amendment types are identified. If the return type
2554 -- is class-wide, there is no required check, the type can
2555 -- be a bona fide TAT.
2557 if Ekind (Scope (Current_Scope)) = E_Package
2558 and then In_Private_Part (Scope (Current_Scope))
2559 and then not Is_Class_Wide_Type (Typ)
2561 Append_Elmt (Designator, Private_Dependents (Typ));
2566 ("invalid use of incomplete type&", Designator, Typ);
2571 -- Case where result definition does indicate an error
2574 Set_Etype (Designator, Any_Type);
2576 end Analyze_Return_Type;
2578 -----------------------------
2579 -- Analyze_Subprogram_Body --
2580 -----------------------------
2582 procedure Analyze_Subprogram_Body (N : Node_Id) is
2583 Loc : constant Source_Ptr := Sloc (N);
2584 Body_Spec : constant Node_Id := Specification (N);
2585 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2588 if Debug_Flag_C then
2589 Write_Str ("==> subprogram body ");
2590 Write_Name (Chars (Body_Id));
2591 Write_Str (" from ");
2592 Write_Location (Loc);
2597 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2599 -- The real work is split out into the helper, so it can do "return;"
2600 -- without skipping the debug output:
2602 Analyze_Subprogram_Body_Helper (N);
2604 if Debug_Flag_C then
2606 Write_Str ("<== subprogram body ");
2607 Write_Name (Chars (Body_Id));
2608 Write_Str (" from ");
2609 Write_Location (Loc);
2612 end Analyze_Subprogram_Body;
2614 ------------------------------------
2615 -- Analyze_Subprogram_Body_Helper --
2616 ------------------------------------
2618 -- This procedure is called for regular subprogram bodies, generic bodies,
2619 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2620 -- specification matters, and is used to create a proper declaration for
2621 -- the subprogram, or to perform conformance checks.
2623 -- WARNING: This routine manages Ghost regions. Return statements must be
2624 -- replaced by gotos that jump to the end of the routine and restore the
2627 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2628 Body_Spec : Node_Id := Specification (N);
2629 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2630 Loc : constant Source_Ptr := Sloc (N);
2631 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2633 Body_Nod : Node_Id := Empty;
2634 Minimum_Acc_Objs : List_Id := No_List;
2636 Conformant : Boolean;
2637 Desig_View : Entity_Id := Empty;
2638 Exch_Views : Elist_Id := No_Elist;
2640 Mask_Types : Elist_Id := No_Elist;
2641 Prot_Typ : Entity_Id := Empty;
2642 Spec_Decl : Node_Id := Empty;
2643 Spec_Id : Entity_Id;
2645 Last_Real_Spec_Entity : Entity_Id := Empty;
2646 -- When we analyze a separate spec, the entity chain ends up containing
2647 -- the formals, as well as any itypes generated during analysis of the
2648 -- default expressions for parameters, or the arguments of associated
2649 -- precondition/postcondition pragmas (which are analyzed in the context
2650 -- of the spec since they have visibility on formals).
2652 -- These entities belong with the spec and not the body. However we do
2653 -- the analysis of the body in the context of the spec (again to obtain
2654 -- visibility to the formals), and all the entities generated during
2655 -- this analysis end up also chained to the entity chain of the spec.
2656 -- But they really belong to the body, and there is circuitry to move
2657 -- them from the spec to the body.
2659 -- However, when we do this move, we don't want to move the real spec
2660 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2661 -- variable points to the last real spec entity, so we only move those
2662 -- chained beyond that point. It is initialized to Empty to deal with
2663 -- the case where there is no separate spec.
2665 function Body_Has_Contract return Boolean;
2666 -- Check whether unanalyzed body has an aspect or pragma that may
2667 -- generate a SPARK contract.
2669 function Body_Has_SPARK_Mode_On return Boolean;
2670 -- Check whether SPARK_Mode On applies to the subprogram body, either
2671 -- because it is specified directly on the body, or because it is
2672 -- inherited from the enclosing subprogram or package.
2674 function Build_Internal_Protected_Declaration
2675 (N : Node_Id) return Entity_Id;
2676 -- A subprogram body without a previous spec that appears in a protected
2677 -- body must be expanded separately to create a subprogram declaration
2678 -- for it, in order to resolve internal calls to it from other protected
2681 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2683 procedure Build_Subprogram_Declaration;
2684 -- Create a matching subprogram declaration for subprogram body N
2686 procedure Check_Anonymous_Return;
2687 -- Ada 2005: if a function returns an access type that denotes a task,
2688 -- or a type that contains tasks, we must create a master entity for
2689 -- the anonymous type, which typically will be used in an allocator
2690 -- in the body of the function.
2692 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2693 -- Look ahead to recognize a pragma that may appear after the body.
2694 -- If there is a previous spec, check that it appears in the same
2695 -- declarative part. If the pragma is Inline_Always, perform inlining
2696 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2697 -- If the body acts as a spec, and inlining is required, we create a
2698 -- subprogram declaration for it, in order to attach the body to inline.
2699 -- If pragma does not appear after the body, check whether there is
2700 -- an inline pragma before any local declarations.
2702 procedure Check_Missing_Return;
2703 -- Checks for a function with a no return statements, and also performs
2704 -- the warning checks implemented by Check_Returns. In formal mode, also
2705 -- verify that a function ends with a RETURN and that a procedure does
2706 -- not contain any RETURN.
2708 function Disambiguate_Spec return Entity_Id;
2709 -- When a primitive is declared between the private view and the full
2710 -- view of a concurrent type which implements an interface, a special
2711 -- mechanism is used to find the corresponding spec of the primitive
2714 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2715 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2716 -- incomplete types coming from a limited context and replace their
2717 -- limited views with the non-limited ones. Return the list of changes
2718 -- to be used to undo the transformation.
2720 procedure Generate_Minimum_Accessibility
2721 (Extra_Access : Entity_Id;
2722 Related_Form : Entity_Id := Empty);
2723 -- Generate a minimum accessibility object for a given extra
2724 -- accessibility formal (Extra_Access) and its related formal if it
2727 function Is_Private_Concurrent_Primitive
2728 (Subp_Id : Entity_Id) return Boolean;
2729 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2730 -- type that implements an interface and has a private view.
2732 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2733 -- N is the body generated for an expression function that is not a
2734 -- completion and Spec_Id the defining entity of its spec. Mark all
2735 -- the not-yet-frozen types referenced by the simple return statement
2736 -- of the function as formally frozen.
2738 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2739 -- Find all suitable source pragmas at the top of subprogram body
2740 -- From's declarations and move them after arbitrary node To.
2741 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2742 -- as it applies to the body too.
2744 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2745 -- Undo the transformation done by Exchange_Limited_Views.
2747 procedure Set_Trivial_Subprogram (N : Node_Id);
2748 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2749 -- subprogram whose body is being analyzed. N is the statement node
2750 -- causing the flag to be set, if the following statement is a return
2751 -- of an entity, we mark the entity as set in source to suppress any
2752 -- warning on the stylized use of function stubs with a dummy return.
2754 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2755 -- Undo the transformation done by Mask_Unfrozen_Types
2757 procedure Verify_Overriding_Indicator;
2758 -- If there was a previous spec, the entity has been entered in the
2759 -- current scope previously. If the body itself carries an overriding
2760 -- indicator, check that it is consistent with the known status of the
2763 -----------------------
2764 -- Body_Has_Contract --
2765 -----------------------
2767 function Body_Has_Contract return Boolean is
2768 Decls : constant List_Id := Declarations (N);
2772 -- Check for aspects that may generate a contract
2774 if Present (Aspect_Specifications (N)) then
2775 Item := First (Aspect_Specifications (N));
2776 while Present (Item) loop
2777 if Is_Subprogram_Contract_Annotation (Item) then
2785 -- Check for pragmas that may generate a contract
2787 if Present (Decls) then
2788 Item := First (Decls);
2789 while Present (Item) loop
2790 if Nkind (Item) = N_Pragma
2791 and then Is_Subprogram_Contract_Annotation (Item)
2801 end Body_Has_Contract;
2803 ----------------------------
2804 -- Body_Has_SPARK_Mode_On --
2805 ----------------------------
2807 function Body_Has_SPARK_Mode_On return Boolean is
2808 Decls : constant List_Id := Declarations (N);
2812 -- Check for SPARK_Mode aspect
2814 if Present (Aspect_Specifications (N)) then
2815 Item := First (Aspect_Specifications (N));
2816 while Present (Item) loop
2817 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2818 return Get_SPARK_Mode_From_Annotation (Item) = On;
2825 -- Check for SPARK_Mode pragma
2827 if Present (Decls) then
2828 Item := First (Decls);
2829 while Present (Item) loop
2831 -- Pragmas that apply to a subprogram body are usually grouped
2832 -- together. Look for a potential pragma SPARK_Mode among them.
2834 if Nkind (Item) = N_Pragma then
2835 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2836 return Get_SPARK_Mode_From_Annotation (Item) = On;
2839 -- Otherwise the first non-pragma declarative item terminates
2840 -- the region where pragma SPARK_Mode may appear.
2850 -- Otherwise, the applicable SPARK_Mode is inherited from the
2851 -- enclosing subprogram or package.
2853 return SPARK_Mode = On;
2854 end Body_Has_SPARK_Mode_On;
2856 ------------------------------------------
2857 -- Build_Internal_Protected_Declaration --
2858 ------------------------------------------
2860 function Build_Internal_Protected_Declaration
2861 (N : Node_Id) return Entity_Id
2863 procedure Analyze_Pragmas (From : Node_Id);
2864 -- Analyze all pragmas which follow arbitrary node From
2866 ---------------------
2867 -- Analyze_Pragmas --
2868 ---------------------
2870 procedure Analyze_Pragmas (From : Node_Id) is
2874 Decl := Next (From);
2875 while Present (Decl) loop
2876 if Nkind (Decl) = N_Pragma then
2877 Analyze_Pragma (Decl);
2879 -- No candidate pragmas are available for analysis
2887 end Analyze_Pragmas;
2891 Body_Id : constant Entity_Id := Defining_Entity (N);
2892 Loc : constant Source_Ptr := Sloc (N);
2897 Spec_Id : Entity_Id;
2899 -- Start of processing for Build_Internal_Protected_Declaration
2902 Formal := First_Formal (Body_Id);
2904 -- The protected operation always has at least one formal, namely the
2905 -- object itself, but it is only placed in the parameter list if
2906 -- expansion is enabled.
2908 if Present (Formal) or else Expander_Active then
2909 Formals := Copy_Parameter_List (Body_Id);
2915 Make_Defining_Identifier (Sloc (Body_Id),
2916 Chars => Chars (Body_Id));
2918 -- Indicate that the entity comes from source, to ensure that cross-
2919 -- reference information is properly generated. The body itself is
2920 -- rewritten during expansion, and the body entity will not appear in
2921 -- calls to the operation.
2923 Set_Comes_From_Source (Spec_Id, True);
2925 if Nkind (Specification (N)) = N_Procedure_Specification then
2927 Make_Procedure_Specification (Loc,
2928 Defining_Unit_Name => Spec_Id,
2929 Parameter_Specifications => Formals);
2932 Make_Function_Specification (Loc,
2933 Defining_Unit_Name => Spec_Id,
2934 Parameter_Specifications => Formals,
2935 Result_Definition =>
2936 New_Occurrence_Of (Etype (Body_Id), Loc));
2939 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2940 Set_Corresponding_Body (Decl, Body_Id);
2941 Set_Corresponding_Spec (N, Spec_Id);
2943 Insert_Before (N, Decl);
2945 -- Associate all aspects and pragmas of the body with the spec. This
2946 -- ensures that these annotations apply to the initial declaration of
2947 -- the subprogram body.
2949 Move_Aspects (From => N, To => Decl);
2950 Move_Pragmas (From => N, To => Decl);
2954 -- The analysis of the spec may generate pragmas which require manual
2955 -- analysis. Since the generation of the spec and the relocation of
2956 -- the annotations is driven by the expansion of the stand-alone
2957 -- body, the pragmas will not be analyzed in a timely manner. Do this
2960 Analyze_Pragmas (Decl);
2962 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2963 -- ensuring in particular that 'Access is illegal.
2965 Set_Convention (Spec_Id, Convention_Intrinsic);
2966 Set_Has_Completion (Spec_Id);
2969 end Build_Internal_Protected_Declaration;
2971 ----------------------------------
2972 -- Build_Subprogram_Declaration --
2973 ----------------------------------
2975 procedure Build_Subprogram_Declaration is
2977 Subp_Decl : Node_Id;
2980 -- Create a matching subprogram spec using the profile of the body.
2981 -- The structure of the tree is identical, but has new entities for
2982 -- the defining unit name and formal parameters.
2985 Make_Subprogram_Declaration (Loc,
2986 Specification => Copy_Subprogram_Spec (Body_Spec));
2987 Set_Comes_From_Source (Subp_Decl, True);
2989 -- Also mark parameters as coming from source
2991 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2996 First (Parameter_Specifications (Specification (Subp_Decl)));
2998 while Present (Form) loop
2999 Set_Comes_From_Source (Defining_Identifier (Form), True);
3005 -- Relocate the aspects and relevant pragmas from the subprogram body
3006 -- to the generated spec because it acts as the initial declaration.
3008 Insert_Before (N, Subp_Decl);
3009 Move_Aspects (N, To => Subp_Decl);
3010 Move_Pragmas (N, To => Subp_Decl);
3012 -- Ensure that the generated corresponding spec and original body
3013 -- share the same SPARK_Mode pragma or aspect. As a result, both have
3014 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
3015 -- correctly set for local subprograms.
3017 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
3019 Analyze (Subp_Decl);
3021 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
3022 -- the body since the expander may generate calls using that entity.
3023 -- Required to ensure that Expand_Call rewrites calls to this
3024 -- function by calls to the built procedure.
3026 if Transform_Function_Array
3027 and then Nkind (Body_Spec) = N_Function_Specification
3029 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
3031 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
3032 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
3033 Corresponding_Procedure
3034 (Defining_Entity (Specification (Subp_Decl))));
3037 -- Analyze any relocated source pragmas or pragmas created for aspect
3040 Decl := Next (Subp_Decl);
3041 while Present (Decl) loop
3043 -- Stop the search for pragmas once the body has been reached as
3044 -- this terminates the region where pragmas may appear.
3049 elsif Nkind (Decl) = N_Pragma then
3056 Spec_Id := Defining_Entity (Subp_Decl);
3057 Set_Corresponding_Spec (N, Spec_Id);
3059 -- Mark the generated spec as a source construct to ensure that all
3060 -- calls to it are properly registered in ALI files for GNATprove.
3062 Set_Comes_From_Source (Spec_Id, True);
3064 -- Ensure that the specs of the subprogram declaration and its body
3065 -- are identical, otherwise they will appear non-conformant due to
3066 -- rewritings in the default values of formal parameters.
3068 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
3069 Set_Specification (N, Body_Spec);
3070 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3071 end Build_Subprogram_Declaration;
3073 ----------------------------
3074 -- Check_Anonymous_Return --
3075 ----------------------------
3077 procedure Check_Anonymous_Return is
3083 if Present (Spec_Id) then
3089 if Ekind (Scop) = E_Function
3090 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
3091 and then not Is_Thunk (Scop)
3093 -- Skip internally built functions which handle the case of
3094 -- a null access (see Expand_Interface_Conversion)
3096 and then not (Is_Interface (Designated_Type (Etype (Scop)))
3097 and then not Comes_From_Source (Parent (Scop)))
3099 and then (Has_Task (Designated_Type (Etype (Scop)))
3101 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
3103 Is_Limited_Record (Designated_Type (Etype (Scop)))))
3104 and then Expander_Active
3106 Decl := Build_Master_Declaration (Loc);
3108 if Present (Declarations (N)) then
3109 Prepend (Decl, Declarations (N));
3111 Set_Declarations (N, New_List (Decl));
3114 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
3115 Set_Has_Master_Entity (Scop);
3117 -- Now mark the containing scope as a task master
3120 while Nkind (Par) /= N_Compilation_Unit loop
3121 Par := Parent (Par);
3122 pragma Assert (Present (Par));
3124 -- If we fall off the top, we are at the outer level, and
3125 -- the environment task is our effective master, so nothing
3129 in N_Task_Body | N_Block_Statement | N_Subprogram_Body
3131 Set_Is_Task_Master (Par, True);
3136 end Check_Anonymous_Return;
3138 -------------------------
3139 -- Check_Inline_Pragma --
3140 -------------------------
3142 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
3146 function Is_Inline_Pragma (N : Node_Id) return Boolean;
3147 -- True when N is a pragma Inline or Inline_Always that applies
3148 -- to this subprogram.
3150 -----------------------
3151 -- Is_Inline_Pragma --
3152 -----------------------
3154 function Is_Inline_Pragma (N : Node_Id) return Boolean is
3156 if Nkind (N) = N_Pragma
3158 (Pragma_Name_Unmapped (N) = Name_Inline_Always
3159 or else (Pragma_Name_Unmapped (N) = Name_Inline
3161 (Front_End_Inlining or else Optimization_Level > 0)))
3162 and then Present (Pragma_Argument_Associations (N))
3165 Pragma_Arg : Node_Id :=
3166 Expression (First (Pragma_Argument_Associations (N)));
3168 if Nkind (Pragma_Arg) = N_Selected_Component then
3169 Pragma_Arg := Selector_Name (Pragma_Arg);
3172 return Chars (Pragma_Arg) = Chars (Body_Id);
3178 end Is_Inline_Pragma;
3180 -- Start of processing for Check_Inline_Pragma
3183 if not Expander_Active then
3187 if Is_List_Member (N)
3188 and then Present (Next (N))
3189 and then Is_Inline_Pragma (Next (N))
3193 elsif Nkind (N) /= N_Subprogram_Body_Stub
3194 and then Present (Declarations (N))
3195 and then Is_Inline_Pragma (First (Declarations (N)))
3197 Prag := First (Declarations (N));
3203 if Present (Prag) then
3204 if Present (Spec_Id) then
3205 if Is_List_Member (N)
3206 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
3207 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
3213 -- Create a subprogram declaration, to make treatment uniform.
3214 -- Make the sloc of the subprogram name that of the entity in
3215 -- the body, so that style checks find identical strings.
3218 Subp : constant Entity_Id :=
3219 Make_Defining_Identifier
3220 (Sloc (Body_Id), Chars (Body_Id));
3221 Decl : constant Node_Id :=
3222 Make_Subprogram_Declaration (Loc,
3224 New_Copy_Tree (Specification (N)));
3227 -- Link the body and the generated spec
3229 Set_Corresponding_Body (Decl, Body_Id);
3230 Set_Corresponding_Spec (N, Subp);
3232 Set_Defining_Unit_Name (Specification (Decl), Subp);
3234 -- To ensure proper coverage when body is inlined, indicate
3235 -- whether the subprogram comes from source.
3237 Preserve_Comes_From_Source (Subp, N);
3239 if Present (First_Formal (Body_Id)) then
3240 Plist := Copy_Parameter_List (Body_Id);
3241 Set_Parameter_Specifications
3242 (Specification (Decl), Plist);
3245 -- Move aspects to the new spec
3247 if Has_Aspects (N) then
3248 Move_Aspects (N, To => Decl);
3251 Insert_Before (N, Decl);
3254 Set_Has_Pragma_Inline (Subp);
3256 if Pragma_Name (Prag) = Name_Inline_Always then
3257 Set_Is_Inlined (Subp);
3258 Set_Has_Pragma_Inline_Always (Subp);
3261 -- Prior to copying the subprogram body to create a template
3262 -- for it for subsequent inlining, remove the pragma from
3263 -- the current body so that the copy that will produce the
3264 -- new body will start from a completely unanalyzed tree.
3266 if Nkind (Parent (Prag)) = N_Subprogram_Body then
3267 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
3274 end Check_Inline_Pragma;
3276 --------------------------
3277 -- Check_Missing_Return --
3278 --------------------------
3280 procedure Check_Missing_Return is
3282 Missing_Ret : Boolean;
3285 if Nkind (Body_Spec) = N_Function_Specification then
3286 if Present (Spec_Id) then
3292 if Return_Present (Id) then
3293 Check_Returns (HSS, 'F', Missing_Ret);
3296 Set_Has_Missing_Return (Id);
3299 -- Within a premature instantiation of a package with no body, we
3300 -- build completions of the functions therein, with a Raise
3301 -- statement. No point in complaining about a missing return in
3304 elsif Ekind (Id) = E_Function
3305 and then In_Instance
3306 and then Present (Statements (HSS))
3307 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3311 elsif Is_Generic_Subprogram (Id)
3312 or else not Is_Machine_Code_Subprogram (Id)
3314 Error_Msg_N ("missing RETURN statement in function body", N);
3317 -- If procedure with No_Return, check returns
3319 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3320 if Present (Spec_Id) then
3326 if No_Return (Id) then
3327 Check_Returns (HSS, 'P', Missing_Ret, Id);
3330 end Check_Missing_Return;
3332 -----------------------
3333 -- Disambiguate_Spec --
3334 -----------------------
3336 function Disambiguate_Spec return Entity_Id is
3337 Priv_Spec : Entity_Id;
3340 procedure Replace_Types (To_Corresponding : Boolean);
3341 -- Depending on the flag, replace the type of formal parameters of
3342 -- Body_Id if it is a concurrent type implementing interfaces with
3343 -- the corresponding record type or the other way around.
3345 procedure Replace_Types (To_Corresponding : Boolean) is
3347 Formal_Typ : Entity_Id;
3350 Formal := First_Formal (Body_Id);
3351 while Present (Formal) loop
3352 Formal_Typ := Etype (Formal);
3354 if Is_Class_Wide_Type (Formal_Typ) then
3355 Formal_Typ := Root_Type (Formal_Typ);
3358 -- From concurrent type to corresponding record
3360 if To_Corresponding then
3361 if Is_Concurrent_Type (Formal_Typ)
3362 and then Present (Corresponding_Record_Type (Formal_Typ))
3365 (Corresponding_Record_Type (Formal_Typ)))
3368 Corresponding_Record_Type (Formal_Typ));
3371 -- From corresponding record to concurrent type
3374 if Is_Concurrent_Record_Type (Formal_Typ)
3375 and then Present (Interfaces (Formal_Typ))
3378 Corresponding_Concurrent_Type (Formal_Typ));
3382 Next_Formal (Formal);
3386 -- Start of processing for Disambiguate_Spec
3389 -- Try to retrieve the specification of the body as is. All error
3390 -- messages are suppressed because the body may not have a spec in
3391 -- its current state.
3393 Spec_N := Find_Corresponding_Spec (N, False);
3395 -- It is possible that this is the body of a primitive declared
3396 -- between a private and a full view of a concurrent type. The
3397 -- controlling parameter of the spec carries the concurrent type,
3398 -- not the corresponding record type as transformed by Analyze_
3399 -- Subprogram_Specification. In such cases, we undo the change
3400 -- made by the analysis of the specification and try to find the
3403 -- Note that wrappers already have their corresponding specs and
3404 -- bodies set during their creation, so if the candidate spec is
3405 -- a wrapper, then we definitely need to swap all types to their
3406 -- original concurrent status.
3409 or else Is_Primitive_Wrapper (Spec_N)
3411 -- Restore all references of corresponding record types to the
3412 -- original concurrent types.
3414 Replace_Types (To_Corresponding => False);
3415 Priv_Spec := Find_Corresponding_Spec (N, False);
3417 -- The current body truly belongs to a primitive declared between
3418 -- a private and a full view. We leave the modified body as is,
3419 -- and return the true spec.
3421 if Present (Priv_Spec)
3422 and then Is_Private_Primitive (Priv_Spec)
3427 -- In case that this is some sort of error, restore the original
3428 -- state of the body.
3430 Replace_Types (To_Corresponding => True);
3434 end Disambiguate_Spec;
3436 ----------------------------
3437 -- Exchange_Limited_Views --
3438 ----------------------------
3440 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3441 Result : Elist_Id := No_Elist;
3443 procedure Detect_And_Exchange (Id : Entity_Id);
3444 -- Determine whether Id's type denotes an incomplete type associated
3445 -- with a limited with clause and exchange the limited view with the
3446 -- non-limited one when available. Note that the non-limited view
3447 -- may exist because of a with_clause in another unit in the context,
3448 -- but cannot be used because the current view of the enclosing unit
3449 -- is still a limited view.
3451 -------------------------
3452 -- Detect_And_Exchange --
3453 -------------------------
3455 procedure Detect_And_Exchange (Id : Entity_Id) is
3456 Typ : constant Entity_Id := Etype (Id);
3458 if From_Limited_With (Typ)
3459 and then Has_Non_Limited_View (Typ)
3460 and then not From_Limited_With (Scope (Typ))
3463 Result := New_Elmt_List;
3466 Prepend_Elmt (Typ, Result);
3467 Prepend_Elmt (Id, Result);
3468 Set_Etype (Id, Non_Limited_View (Typ));
3470 end Detect_And_Exchange;
3476 -- Start of processing for Exchange_Limited_Views
3479 -- Do not process subprogram bodies as they already use the non-
3480 -- limited view of types.
3482 if Ekind (Subp_Id) not in E_Function | E_Procedure then
3486 -- Examine all formals and swap views when applicable
3488 Formal := First_Formal (Subp_Id);
3489 while Present (Formal) loop
3490 Detect_And_Exchange (Formal);
3492 Next_Formal (Formal);
3495 -- Process the return type of a function
3497 if Ekind (Subp_Id) = E_Function then
3498 Detect_And_Exchange (Subp_Id);
3502 end Exchange_Limited_Views;
3504 ------------------------------------
3505 -- Generate_Minimum_Accessibility --
3506 ------------------------------------
3508 procedure Generate_Minimum_Accessibility
3509 (Extra_Access : Entity_Id;
3510 Related_Form : Entity_Id := Empty)
3512 Loc : constant Source_Ptr := Sloc (Body_Nod);
3516 -- When no related formal exists then we are dealing with an
3517 -- extra accessibility formal for a function result.
3519 if No (Related_Form) then
3520 Form := Extra_Access;
3522 Form := Related_Form;
3525 -- Create the minimum accessibility object
3528 Make_Object_Declaration (Loc,
3529 Defining_Identifier =>
3531 (Loc, 'A', Extra_Access),
3532 Object_Definition => New_Occurrence_Of
3533 (Standard_Natural, Loc),
3535 Make_Attribute_Reference (Loc,
3536 Prefix => New_Occurrence_Of
3537 (Standard_Natural, Loc),
3538 Attribute_Name => Name_Min,
3539 Expressions => New_List (
3540 Make_Integer_Literal (Loc,
3541 Scope_Depth (Body_Id)),
3543 (Extra_Access, Loc))));
3545 -- Add the new local object to the Minimum_Acc_Obj to
3546 -- be later prepended to the subprogram's list of
3547 -- declarations after we are sure all expansion is
3550 if Present (Minimum_Acc_Objs) then
3551 Prepend (Obj_Node, Minimum_Acc_Objs);
3553 Minimum_Acc_Objs := New_List (Obj_Node);
3556 -- Register the object and analyze it
3558 Set_Minimum_Accessibility
3559 (Form, Defining_Identifier (Obj_Node));
3562 end Generate_Minimum_Accessibility;
3564 -------------------------------------
3565 -- Is_Private_Concurrent_Primitive --
3566 -------------------------------------
3568 function Is_Private_Concurrent_Primitive
3569 (Subp_Id : Entity_Id) return Boolean
3571 Formal_Typ : Entity_Id;
3574 if Present (First_Formal (Subp_Id)) then
3575 Formal_Typ := Etype (First_Formal (Subp_Id));
3577 if Is_Concurrent_Record_Type (Formal_Typ) then
3578 if Is_Class_Wide_Type (Formal_Typ) then
3579 Formal_Typ := Root_Type (Formal_Typ);
3582 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3585 -- The type of the first formal is a concurrent tagged type with
3589 Is_Concurrent_Type (Formal_Typ)
3590 and then Is_Tagged_Type (Formal_Typ)
3591 and then Has_Private_Declaration (Formal_Typ);
3595 end Is_Private_Concurrent_Primitive;
3597 -------------------------
3598 -- Mask_Unfrozen_Types --
3599 -------------------------
3601 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3602 Result : Elist_Id := No_Elist;
3604 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3605 -- Mask all types referenced in the subtree rooted at Node
3607 --------------------
3608 -- Mask_Type_Refs --
3609 --------------------
3611 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3612 procedure Mask_Type (Typ : Entity_Id);
3613 -- ??? what does this do?
3619 procedure Mask_Type (Typ : Entity_Id) is
3621 -- Skip Itypes created by the preanalysis
3624 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3629 if not Is_Frozen (Typ) then
3630 if Scope (Typ) /= Current_Scope then
3631 Set_Is_Frozen (Typ);
3632 Append_New_Elmt (Typ, Result);
3634 Freeze_Before (N, Typ);
3639 -- Start of processing for Mask_Type_Refs
3642 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3643 Mask_Type (Etype (Entity (Node)));
3645 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
3646 Mask_Type (Scope (Entity (Node)));
3649 elsif Nkind (Node) in N_Aggregate | N_Null | N_Type_Conversion
3650 and then Present (Etype (Node))
3652 Mask_Type (Etype (Node));
3658 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3662 Return_Stmt : constant Node_Id :=
3663 First (Statements (Handled_Statement_Sequence (N)));
3665 -- Start of processing for Mask_Unfrozen_Types
3668 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3670 Mask_References (Expression (Return_Stmt));
3673 end Mask_Unfrozen_Types;
3679 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3681 Insert_Nod : Node_Id;
3682 Next_Decl : Node_Id;
3685 pragma Assert (Nkind (From) = N_Subprogram_Body);
3687 -- The pragmas are moved in an order-preserving fashion
3691 -- Inspect the declarations of the subprogram body and relocate all
3692 -- candidate pragmas.
3694 Decl := First (Declarations (From));
3695 while Present (Decl) loop
3697 -- Preserve the following declaration for iteration purposes, due
3698 -- to possible relocation of a pragma.
3700 Next_Decl := Next (Decl);
3702 if Nkind (Decl) = N_Pragma then
3703 -- Copy pragma SPARK_Mode if present in the declarative list
3704 -- of subprogram body From and insert it after node To. This
3705 -- pragma should not be moved, as it applies to the body too.
3707 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3708 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3710 -- Move relevant pragmas to the spec
3712 elsif Pragma_Name_Unmapped (Decl) in Name_Depends
3718 | Name_Refined_Depends
3719 | Name_Refined_Global
3722 | Name_Pure_Function
3723 | Name_Volatile_Function
3726 Insert_After (Insert_Nod, Decl);
3730 -- Skip internally generated code
3732 elsif not Comes_From_Source (Decl) then
3735 -- No candidate pragmas are available for relocation
3745 ---------------------------
3746 -- Restore_Limited_Views --
3747 ---------------------------
3749 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3750 Elmt : Elmt_Id := First_Elmt (Restore_List);
3754 while Present (Elmt) loop
3757 Set_Etype (Id, Node (Elmt));
3760 end Restore_Limited_Views;
3762 ----------------------------
3763 -- Set_Trivial_Subprogram --
3764 ----------------------------
3766 procedure Set_Trivial_Subprogram (N : Node_Id) is
3767 Nxt : constant Node_Id := Next (N);
3770 Set_Is_Trivial_Subprogram (Body_Id);
3772 if Present (Spec_Id) then
3773 Set_Is_Trivial_Subprogram (Spec_Id);
3777 and then Nkind (Nxt) = N_Simple_Return_Statement
3778 and then No (Next (Nxt))
3779 and then Present (Expression (Nxt))
3780 and then Is_Entity_Name (Expression (Nxt))
3782 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3784 end Set_Trivial_Subprogram;
3786 ---------------------------
3787 -- Unmask_Unfrozen_Types --
3788 ---------------------------
3790 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3791 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3794 while Present (Elmt) loop
3795 Set_Is_Frozen (Node (Elmt), False);
3798 end Unmask_Unfrozen_Types;
3800 ---------------------------------
3801 -- Verify_Overriding_Indicator --
3802 ---------------------------------
3804 procedure Verify_Overriding_Indicator is
3806 if Must_Override (Body_Spec) then
3807 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3808 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3812 -- Overridden controlled primitives may have had their
3813 -- Overridden_Operation field cleared according to the setting of
3814 -- the Is_Hidden flag. An issue arises, however, when analyzing
3815 -- an instance that may have manipulated the flag during
3816 -- expansion. As a result, we add an exception for this case.
3818 elsif not Present (Overridden_Operation (Spec_Id))
3819 and then not (Chars (Spec_Id) in Name_Adjust
3822 and then In_Instance)
3825 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3827 -- Overriding indicators aren't allowed for protected subprogram
3828 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3829 -- this to a warning if -gnatd.E is enabled.
3831 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3832 Error_Msg_Warn := Error_To_Warning;
3834 ("<<overriding indicator not allowed for protected "
3835 & "subprogram body", Body_Spec);
3838 elsif Must_Not_Override (Body_Spec) then
3839 if Present (Overridden_Operation (Spec_Id)) then
3841 ("subprogram& overrides inherited operation",
3842 Body_Spec, Spec_Id);
3844 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3845 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3848 ("subprogram& overrides predefined operator ",
3849 Body_Spec, Spec_Id);
3851 -- Overriding indicators aren't allowed for protected subprogram
3852 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3853 -- this to a warning if -gnatd.E is enabled.
3855 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3856 Error_Msg_Warn := Error_To_Warning;
3859 ("<<overriding indicator not allowed "
3860 & "for protected subprogram body", Body_Spec);
3862 -- If this is not a primitive operation, then the overriding
3863 -- indicator is altogether illegal.
3865 elsif not Is_Primitive (Spec_Id) then
3867 ("overriding indicator only allowed "
3868 & "if subprogram is primitive", Body_Spec);
3871 -- If checking the style rule and the operation overrides, then
3872 -- issue a warning about a missing overriding_indicator. Protected
3873 -- subprogram bodies are excluded from this style checking, since
3874 -- they aren't primitives (even though their declarations can
3875 -- override) and aren't allowed to have an overriding_indicator.
3878 and then Present (Overridden_Operation (Spec_Id))
3879 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3881 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3882 Style.Missing_Overriding (N, Body_Id);
3885 and then Can_Override_Operator (Spec_Id)
3886 and then not In_Predefined_Unit (Spec_Id)
3888 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3889 Style.Missing_Overriding (N, Body_Id);
3891 end Verify_Overriding_Indicator;
3895 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3896 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3897 Saved_EA : constant Boolean := Expander_Active;
3898 Saved_ISMP : constant Boolean :=
3899 Ignore_SPARK_Mode_Pragmas_In_Instance;
3900 -- Save the Ghost and SPARK mode-related data to restore on exit
3902 -- Start of processing for Analyze_Subprogram_Body_Helper
3905 -- A [generic] subprogram body freezes the contract of the nearest
3906 -- enclosing package body and all other contracts encountered in the
3907 -- same declarative part up to and excluding the subprogram body:
3909 -- package body Nearest_Enclosing_Package
3910 -- with Refined_State => (State => Constit)
3914 -- procedure Freezes_Enclosing_Package_Body
3915 -- with Refined_Depends => (Input => Constit) ...
3917 -- This ensures that any annotations referenced by the contract of the
3918 -- [generic] subprogram body are available. This form of freezing is
3919 -- decoupled from the usual Freeze_xxx mechanism because it must also
3920 -- work in the context of generics where normal freezing is disabled.
3922 -- Only bodies coming from source should cause this type of freezing.
3923 -- Expression functions that act as bodies and complete an initial
3924 -- declaration must be included in this category, hence the use of
3927 if Comes_From_Source (Original_Node (N)) then
3928 Freeze_Previous_Contracts (N);
3931 -- Generic subprograms are handled separately. They always have a
3932 -- generic specification. Determine whether current scope has a
3933 -- previous declaration.
3935 -- If the subprogram body is defined within an instance of the same
3936 -- name, the instance appears as a package renaming, and will be hidden
3937 -- within the subprogram.
3939 if Present (Prev_Id)
3940 and then not Is_Overloadable (Prev_Id)
3941 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3942 or else Comes_From_Source (Prev_Id))
3944 if Is_Generic_Subprogram (Prev_Id) then
3947 -- A subprogram body is Ghost when it is stand-alone and subject
3948 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3949 -- the mode now to ensure that any nodes generated during analysis
3950 -- and expansion are properly marked as Ghost.
3952 Mark_And_Set_Ghost_Body (N, Spec_Id);
3954 -- If the body completes the initial declaration of a compilation
3955 -- unit which is subject to pragma Elaboration_Checks, set the
3956 -- model specified by the pragma because it applies to all parts
3959 Install_Elaboration_Model (Spec_Id);
3961 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3962 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3964 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3966 if Nkind (N) = N_Subprogram_Body then
3967 HSS := Handled_Statement_Sequence (N);
3968 Check_Missing_Return;
3973 -- Otherwise a previous entity conflicts with the subprogram name.
3974 -- Attempting to enter name will post error.
3977 Enter_Name (Body_Id);
3981 -- Non-generic case, find the subprogram declaration, if one was seen,
3982 -- or enter new overloaded entity in the current scope. If the
3983 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3984 -- part of the context of one of its subunits. No need to redo the
3987 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3991 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3993 if Nkind (N) = N_Subprogram_Body_Stub
3994 or else No (Corresponding_Spec (N))
3996 if Is_Private_Concurrent_Primitive (Body_Id) then
3997 Spec_Id := Disambiguate_Spec;
3999 -- A subprogram body is Ghost when it is stand-alone and
4000 -- subject to pragma Ghost or when the corresponding spec is
4001 -- Ghost. Set the mode now to ensure that any nodes generated
4002 -- during analysis and expansion are properly marked as Ghost.
4004 Mark_And_Set_Ghost_Body (N, Spec_Id);
4006 -- If the body completes a compilation unit which is subject
4007 -- to pragma Elaboration_Checks, set the model specified by
4008 -- the pragma because it applies to all parts of the unit.
4010 Install_Elaboration_Model (Spec_Id);
4013 Spec_Id := Find_Corresponding_Spec (N);
4015 -- A subprogram body is Ghost when it is stand-alone and
4016 -- subject to pragma Ghost or when the corresponding spec is
4017 -- Ghost. Set the mode now to ensure that any nodes generated
4018 -- during analysis and expansion are properly marked as Ghost.
4020 Mark_And_Set_Ghost_Body (N, Spec_Id);
4022 -- If the body completes a compilation unit which is subject
4023 -- to pragma Elaboration_Checks, set the model specified by
4024 -- the pragma because it applies to all parts of the unit.
4026 Install_Elaboration_Model (Spec_Id);
4028 -- In GNATprove mode, if the body has no previous spec, create
4029 -- one so that the inlining machinery can operate properly.
4030 -- Transfer aspects, if any, to the new spec, so that they
4031 -- are legal and can be processed ahead of the body.
4032 -- We make two copies of the given spec, one for the new
4033 -- declaration, and one for the body.
4034 -- ??? This should be conditioned on front-end inlining rather
4035 -- than GNATprove_Mode.
4037 if No (Spec_Id) and then GNATprove_Mode
4039 -- Inlining does not apply during preanalysis of code
4041 and then Full_Analysis
4043 -- Inlining only applies to full bodies, not stubs
4045 and then Nkind (N) /= N_Subprogram_Body_Stub
4047 -- Inlining only applies to bodies in the source code, not to
4048 -- those generated by the compiler. In particular, expression
4049 -- functions, whose body is generated by the compiler, are
4050 -- treated specially by GNATprove.
4052 and then Comes_From_Source (Body_Id)
4054 -- This cannot be done for a compilation unit, which is not
4055 -- in a context where we can insert a new spec.
4057 and then Is_List_Member (N)
4059 -- Inlining only applies to subprograms without contracts,
4060 -- as a contract is a sign that GNATprove should perform a
4061 -- modular analysis of the subprogram instead of a contextual
4062 -- analysis at each call site. The same test is performed in
4063 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
4064 -- here in another form (because the contract has not been
4065 -- attached to the body) to avoid front-end errors in case
4066 -- pragmas are used instead of aspects, because the
4067 -- corresponding pragmas in the body would not be transferred
4068 -- to the spec, leading to legality errors.
4070 and then not Body_Has_Contract
4071 and then not Inside_A_Generic
4073 Build_Subprogram_Declaration;
4075 -- If this is a function that returns a constrained array, and
4076 -- Transform_Function_Array is set, create subprogram
4077 -- declaration to simplify e.g. subsequent C generation.
4080 and then Transform_Function_Array
4081 and then Nkind (Body_Spec) = N_Function_Specification
4082 and then Is_Array_Type (Etype (Body_Id))
4083 and then Is_Constrained (Etype (Body_Id))
4085 Build_Subprogram_Declaration;
4089 -- If this is a duplicate body, no point in analyzing it
4091 if Error_Posted (N) then
4095 -- A subprogram body should cause freezing of its own declaration,
4096 -- but if there was no previous explicit declaration, then the
4097 -- subprogram will get frozen too late (there may be code within
4098 -- the body that depends on the subprogram having been frozen,
4099 -- such as uses of extra formals), so we force it to be frozen
4100 -- here. Same holds if the body and spec are compilation units.
4101 -- Finally, if the return type is an anonymous access to protected
4102 -- subprogram, it must be frozen before the body because its
4103 -- expansion has generated an equivalent type that is used when
4104 -- elaborating the body.
4106 -- An exception in the case of Ada 2012, AI05-177: The bodies
4107 -- created for expression functions do not freeze.
4110 and then Nkind (Original_Node (N)) /= N_Expression_Function
4112 Freeze_Before (N, Body_Id);
4114 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4115 Freeze_Before (N, Spec_Id);
4117 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
4118 Freeze_Before (N, Etype (Body_Id));
4122 Spec_Id := Corresponding_Spec (N);
4124 -- A subprogram body is Ghost when it is stand-alone and subject
4125 -- to pragma Ghost or when the corresponding spec is Ghost. Set
4126 -- the mode now to ensure that any nodes generated during analysis
4127 -- and expansion are properly marked as Ghost.
4129 Mark_And_Set_Ghost_Body (N, Spec_Id);
4131 -- If the body completes the initial declaration of a compilation
4132 -- unit which is subject to pragma Elaboration_Checks, set the
4133 -- model specified by the pragma because it applies to all parts
4136 Install_Elaboration_Model (Spec_Id);
4140 -- Deactivate expansion inside the body of ignored Ghost entities,
4141 -- as this code will ultimately be ignored. This avoids requiring the
4142 -- presence of run-time units which are not needed. Only do this for
4143 -- user entities, as internally generated entitities might still need
4144 -- to be expanded (e.g. those generated for types).
4146 if Present (Ignored_Ghost_Region)
4147 and then Comes_From_Source (Body_Id)
4149 Expander_Active := False;
4152 -- Previously we scanned the body to look for nested subprograms, and
4153 -- rejected an inline directive if nested subprograms were present,
4154 -- because the back-end would generate conflicting symbols for the
4155 -- nested bodies. This is now unnecessary.
4157 -- Look ahead to recognize a pragma Inline that appears after the body
4159 Check_Inline_Pragma (Spec_Id);
4161 -- Deal with special case of a fully private operation in the body of
4162 -- the protected type. We must create a declaration for the subprogram,
4163 -- in order to attach the subprogram that will be used in internal
4164 -- calls. We exclude compiler generated bodies from the expander since
4165 -- the issue does not arise for those cases.
4168 and then Comes_From_Source (N)
4169 and then Is_Protected_Type (Current_Scope)
4171 Spec_Id := Build_Internal_Protected_Declaration (N);
4174 -- If Transform_Function_Array is set and this is a function returning a
4175 -- constrained array type for which we must create a procedure with an
4176 -- extra out parameter, build and analyze the body now. The procedure
4177 -- declaration has already been created. We reuse the source body of the
4178 -- function, because in an instance it may contain global references
4179 -- that cannot be reanalyzed. The source function itself is not used any
4180 -- further, so we mark it as having a completion. If the subprogram is a
4181 -- stub the transformation is done later, when the proper body is
4185 and then Transform_Function_Array
4186 and then Present (Spec_Id)
4187 and then Ekind (Spec_Id) = E_Function
4188 and then Nkind (N) /= N_Subprogram_Body_Stub
4189 and then Rewritten_For_C (Spec_Id)
4191 Set_Has_Completion (Spec_Id);
4193 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
4196 -- The entity for the created procedure must remain invisible, so it
4197 -- does not participate in resolution of subsequent references to the
4200 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
4204 -- If a separate spec is present, then deal with freezing issues
4206 if Present (Spec_Id) then
4207 Spec_Decl := Unit_Declaration_Node (Spec_Id);
4208 Verify_Overriding_Indicator;
4210 -- In general, the spec will be frozen when we start analyzing the
4211 -- body. However, for internally generated operations, such as
4212 -- wrapper functions for inherited operations with controlling
4213 -- results, the spec may not have been frozen by the time we expand
4214 -- the freeze actions that include the bodies. In particular, extra
4215 -- formals for accessibility or for return-in-place may need to be
4216 -- generated. Freeze nodes, if any, are inserted before the current
4217 -- body. These freeze actions are also needed in Compile_Only mode to
4218 -- enable the proper back-end type annotations.
4219 -- They are necessary in any case to ensure proper elaboration order
4222 if Nkind (N) = N_Subprogram_Body
4223 and then Was_Expression_Function (N)
4224 and then not Has_Completion (Spec_Id)
4225 and then Serious_Errors_Detected = 0
4226 and then (Expander_Active
4227 or else Operating_Mode = Check_Semantics
4228 or else Is_Ignored_Ghost_Entity (Spec_Id))
4230 -- The body generated for an expression function that is not a
4231 -- completion is a freeze point neither for the profile nor for
4232 -- anything else. That's why, in order to prevent any freezing
4233 -- during analysis, we need to mask types declared outside the
4234 -- expression (and in an outer scope) that are not yet frozen.
4235 -- This also needs to be done in the case of an ignored Ghost
4236 -- expression function, where the expander isn't active.
4238 Set_Is_Frozen (Spec_Id);
4239 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4241 elsif not Is_Frozen (Spec_Id)
4242 and then Serious_Errors_Detected = 0
4244 Set_Has_Delayed_Freeze (Spec_Id);
4245 Freeze_Before (N, Spec_Id);
4249 -- If the subprogram has a class-wide clone, build its body as a copy
4250 -- of the original body, and rewrite body of original subprogram as a
4251 -- wrapper that calls the clone. If N is a stub, this construction will
4252 -- take place when the proper body is analyzed. No action needed if this
4253 -- subprogram has been eliminated.
4255 if Present (Spec_Id)
4256 and then Present (Class_Wide_Clone (Spec_Id))
4257 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4258 and then Nkind (N) /= N_Subprogram_Body_Stub
4259 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4261 Build_Class_Wide_Clone_Body (Spec_Id, N);
4263 -- This is the new body for the existing primitive operation
4265 Rewrite (N, Build_Class_Wide_Clone_Call
4266 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4267 Set_Has_Completion (Spec_Id, False);
4272 -- Place subprogram on scope stack, and make formals visible. If there
4273 -- is a spec, the visible entity remains that of the spec.
4275 if Present (Spec_Id) then
4276 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4278 if Is_Child_Unit (Spec_Id) then
4279 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4283 Style.Check_Identifier (Body_Id, Spec_Id);
4286 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4287 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4289 if Is_Abstract_Subprogram (Spec_Id) then
4290 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4294 Set_Convention (Body_Id, Convention (Spec_Id));
4295 Set_Has_Completion (Spec_Id);
4297 if Is_Protected_Type (Scope (Spec_Id)) then
4298 Prot_Typ := Scope (Spec_Id);
4301 -- If this is a body generated for a renaming, do not check for
4302 -- full conformance. The check is redundant, because the spec of
4303 -- the body is a copy of the spec in the renaming declaration,
4304 -- and the test can lead to spurious errors on nested defaults.
4306 if Present (Spec_Decl)
4307 and then not Comes_From_Source (N)
4309 (Nkind (Original_Node (Spec_Decl)) =
4310 N_Subprogram_Renaming_Declaration
4311 or else (Present (Corresponding_Body (Spec_Decl))
4313 Nkind (Unit_Declaration_Node
4314 (Corresponding_Body (Spec_Decl))) =
4315 N_Subprogram_Renaming_Declaration))
4319 -- Conversely, the spec may have been generated for specless body
4320 -- with an inline pragma. The entity comes from source, which is
4321 -- both semantically correct and necessary for proper inlining.
4322 -- The subprogram declaration itself is not in the source.
4324 elsif Comes_From_Source (N)
4325 and then Present (Spec_Decl)
4326 and then not Comes_From_Source (Spec_Decl)
4327 and then Has_Pragma_Inline (Spec_Id)
4334 Fully_Conformant, True, Conformant, Body_Id);
4337 -- If the body is not fully conformant, we have to decide if we
4338 -- should analyze it or not. If it has a really messed up profile
4339 -- then we probably should not analyze it, since we will get too
4340 -- many bogus messages.
4342 -- Our decision is to go ahead in the non-fully conformant case
4343 -- only if it is at least mode conformant with the spec. Note
4344 -- that the call to Check_Fully_Conformant has issued the proper
4345 -- error messages to complain about the lack of conformance.
4348 and then not Mode_Conformant (Body_Id, Spec_Id)
4354 -- In the case we are dealing with an expression function we check
4355 -- the formals attached to the spec instead of the body - so we don't
4356 -- reference body formals.
4358 if Spec_Id /= Body_Id
4359 and then not Is_Expression_Function (Spec_Id)
4361 Reference_Body_Formals (Spec_Id, Body_Id);
4364 Set_Ekind (Body_Id, E_Subprogram_Body);
4366 if Nkind (N) = N_Subprogram_Body_Stub then
4367 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4372 Set_Corresponding_Spec (N, Spec_Id);
4374 -- Ada 2005 (AI-345): If the operation is a primitive operation
4375 -- of a concurrent type, the type of the first parameter has been
4376 -- replaced with the corresponding record, which is the proper
4377 -- run-time structure to use. However, within the body there may
4378 -- be uses of the formals that depend on primitive operations
4379 -- of the type (in particular calls in prefixed form) for which
4380 -- we need the original concurrent type. The operation may have
4381 -- several controlling formals, so the replacement must be done
4384 if Comes_From_Source (Spec_Id)
4385 and then Present (First_Entity (Spec_Id))
4386 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4387 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4388 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4389 and then Present (Corresponding_Concurrent_Type
4390 (Etype (First_Entity (Spec_Id))))
4393 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4397 Form := First_Formal (Spec_Id);
4398 while Present (Form) loop
4399 if Etype (Form) = Typ then
4400 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4408 -- Make the formals visible, and place subprogram on scope stack.
4409 -- This is also the point at which we set Last_Real_Spec_Entity
4410 -- to mark the entities which will not be moved to the body.
4412 Install_Formals (Spec_Id);
4413 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4415 -- Within an instance, add local renaming declarations so that
4416 -- gdb can retrieve the values of actuals more easily. This is
4417 -- only relevant if generating code.
4419 if Is_Generic_Instance (Spec_Id)
4420 and then Is_Wrapper_Package (Current_Scope)
4421 and then Expander_Active
4423 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4426 Push_Scope (Spec_Id);
4428 -- Make sure that the subprogram is immediately visible. For
4429 -- child units that have no separate spec this is indispensable.
4430 -- Otherwise it is safe albeit redundant.
4432 Set_Is_Immediately_Visible (Spec_Id);
4435 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4436 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4437 Set_Scope (Body_Id, Scope (Spec_Id));
4439 -- Case of subprogram body with no previous spec
4442 -- Check for style warning required
4446 -- Only apply check for source level subprograms for which checks
4447 -- have not been suppressed.
4449 and then Comes_From_Source (Body_Id)
4450 and then not Suppress_Style_Checks (Body_Id)
4452 -- No warnings within an instance
4454 and then not In_Instance
4456 -- No warnings for expression functions
4458 and then Nkind (Original_Node (N)) /= N_Expression_Function
4460 Style.Body_With_No_Spec (N);
4463 New_Overloaded_Entity (Body_Id);
4465 if Nkind (N) /= N_Subprogram_Body_Stub then
4466 Set_Acts_As_Spec (N);
4467 Generate_Definition (Body_Id);
4469 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4471 -- If the body is an entry wrapper created for an entry with
4472 -- preconditions, it must be compiled in the context of the
4473 -- enclosing synchronized object, because it may mention other
4474 -- operations of the type.
4476 if Is_Entry_Wrapper (Body_Id) then
4478 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4481 Install_Declarations (Prot);
4485 Install_Formals (Body_Id);
4487 Push_Scope (Body_Id);
4490 -- For stubs and bodies with no previous spec, generate references to
4493 Generate_Reference_To_Formals (Body_Id);
4496 -- Entry barrier functions are generated outside the protected type and
4497 -- should not carry the SPARK_Mode of the enclosing context.
4499 if Nkind (N) = N_Subprogram_Body
4500 and then Is_Entry_Barrier_Function (N)
4504 -- The body is generated as part of expression function expansion. When
4505 -- the expression function appears in the visible declarations of a
4506 -- package, the body is added to the private declarations. Since both
4507 -- declarative lists may be subject to a different SPARK_Mode, inherit
4508 -- the mode of the spec.
4510 -- package P with SPARK_Mode is
4511 -- function Expr_Func ... is (...); -- original
4512 -- [function Expr_Func ...;] -- generated spec
4515 -- pragma SPARK_Mode (Off);
4516 -- [function Expr_Func ... is return ...;] -- generated body
4517 -- end P; -- mode is ON
4519 elsif not Comes_From_Source (N)
4520 and then Present (Spec_Id)
4521 and then Is_Expression_Function (Spec_Id)
4523 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4524 Set_SPARK_Pragma_Inherited
4525 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4527 -- Set the SPARK_Mode from the current context (may be overwritten later
4528 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4529 -- initially on a stand-alone subprogram body, but is then relocated to
4530 -- a generated corresponding spec. In this scenario the mode is shared
4531 -- between the spec and body.
4533 elsif No (SPARK_Pragma (Body_Id)) then
4534 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4535 Set_SPARK_Pragma_Inherited (Body_Id);
4538 -- A subprogram body may be instantiated or inlined at a later pass.
4539 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4540 -- applied to the initial declaration of the body.
4542 if Present (Spec_Id) then
4543 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4544 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4548 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4549 -- case the body is instantiated or inlined later and out of context.
4550 -- The body uses this attribute to restore the value of the global
4553 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4554 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4556 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4557 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4561 -- Preserve relevant elaboration-related attributes of the context which
4562 -- are no longer available or very expensive to recompute once analysis,
4563 -- resolution, and expansion are over.
4565 if No (Spec_Id) then
4566 Mark_Elaboration_Attributes
4572 -- If this is the proper body of a stub, we must verify that the stub
4573 -- conforms to the body, and to the previous spec if one was present.
4574 -- We know already that the body conforms to that spec. This test is
4575 -- only required for subprograms that come from source.
4577 if Nkind (Parent (N)) = N_Subunit
4578 and then Comes_From_Source (N)
4579 and then not Error_Posted (Body_Id)
4580 and then Nkind (Corresponding_Stub (Parent (N))) =
4581 N_Subprogram_Body_Stub
4584 Old_Id : constant Entity_Id :=
4586 (Specification (Corresponding_Stub (Parent (N))));
4588 Conformant : Boolean := False;
4591 if No (Spec_Id) then
4592 Check_Fully_Conformant (Body_Id, Old_Id);
4596 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4598 if not Conformant then
4600 -- The stub was taken to be a new declaration. Indicate that
4603 Set_Has_Completion (Old_Id, False);
4609 Set_Has_Completion (Body_Id);
4610 Check_Eliminated (Body_Id);
4612 -- Analyze any aspect specifications that appear on the subprogram body
4613 -- stub. Stop the analysis now as the stub does not have a declarative
4614 -- or a statement part, and it cannot be inlined.
4616 if Nkind (N) = N_Subprogram_Body_Stub then
4617 if Has_Aspects (N) then
4618 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4627 and then Serious_Errors_Detected = 0
4628 and then Present (Spec_Id)
4629 and then Has_Pragma_Inline (Spec_Id)
4631 -- Legacy implementation (relying on front-end inlining)
4633 if not Back_End_Inlining then
4634 if Has_Pragma_Inline_Always (Spec_Id)
4635 or else (Front_End_Inlining
4636 and then not Opt.Disable_FE_Inline)
4638 Build_Body_To_Inline (N, Spec_Id);
4641 -- New implementation (relying on back-end inlining)
4644 if Has_Pragma_Inline_Always (Spec_Id)
4645 or else Optimization_Level > 0
4647 -- Handle function returning an unconstrained type
4649 if Comes_From_Source (Body_Id)
4650 and then Ekind (Spec_Id) = E_Function
4651 and then Returns_Unconstrained_Type (Spec_Id)
4653 -- If function builds in place, i.e. returns a limited type,
4654 -- inlining cannot be done.
4656 and then not Is_Limited_Type (Etype (Spec_Id))
4658 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4662 Subp_Body : constant Node_Id :=
4663 Unit_Declaration_Node (Body_Id);
4664 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4667 -- Do not pass inlining to the backend if the subprogram
4668 -- has declarations or statements which cannot be inlined
4669 -- by the backend. This check is done here to emit an
4670 -- error instead of the generic warning message reported
4671 -- by the GCC backend (ie. "function might not be
4674 if Present (Subp_Decl)
4675 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4679 elsif Has_Excluded_Statement
4682 (Handled_Statement_Sequence (Subp_Body)))
4686 -- If the backend inlining is available then at this
4687 -- stage we only have to mark the subprogram as inlined.
4688 -- The expander will take care of registering it in the
4689 -- table of subprograms inlined by the backend a part of
4690 -- processing calls to it (cf. Expand_Call)
4693 Set_Is_Inlined (Spec_Id);
4700 -- In GNATprove mode, inline only when there is a separate subprogram
4701 -- declaration for now, as inlining of subprogram bodies acting as
4702 -- declarations, or subprogram stubs, are not supported by front-end
4703 -- inlining. This inlining should occur after analysis of the body, so
4704 -- that it is known whether the value of SPARK_Mode, which can be
4705 -- defined by a pragma inside the body, is applicable to the body.
4706 -- Inlining can be disabled with switch -gnatdm
4708 elsif GNATprove_Mode
4709 and then Full_Analysis
4710 and then not Inside_A_Generic
4711 and then Present (Spec_Id)
4713 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4714 and then Body_Has_SPARK_Mode_On
4715 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4716 and then not Body_Has_Contract
4717 and then not Debug_Flag_M
4719 Build_Body_To_Inline (N, Spec_Id);
4722 -- When generating code, inherited pre/postconditions are handled when
4723 -- expanding the corresponding contract.
4725 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4726 -- of the specification we have to install the private withed units.
4727 -- This holds for child units as well.
4729 if Is_Compilation_Unit (Body_Id)
4730 or else Nkind (Parent (N)) = N_Compilation_Unit
4732 Install_Private_With_Clauses (Body_Id);
4735 Check_Anonymous_Return;
4737 -- Set the Protected_Formal field of each extra formal of the protected
4738 -- subprogram to reference the corresponding extra formal of the
4739 -- subprogram that implements it. For regular formals this occurs when
4740 -- the protected subprogram's declaration is expanded, but the extra
4741 -- formals don't get created until the subprogram is frozen. We need to
4742 -- do this before analyzing the protected subprogram's body so that any
4743 -- references to the original subprogram's extra formals will be changed
4744 -- refer to the implementing subprogram's formals (see Expand_Formal).
4746 if Present (Spec_Id)
4747 and then Is_Protected_Type (Scope (Spec_Id))
4748 and then Present (Protected_Body_Subprogram (Spec_Id))
4751 Impl_Subp : constant Entity_Id :=
4752 Protected_Body_Subprogram (Spec_Id);
4753 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4754 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4757 while Present (Prot_Ext_Formal) loop
4758 pragma Assert (Present (Impl_Ext_Formal));
4759 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4760 Next_Formal_With_Extras (Prot_Ext_Formal);
4761 Next_Formal_With_Extras (Impl_Ext_Formal);
4766 -- Generate minimum accessibility local objects to correspond with
4767 -- any extra formal added for anonymous access types. This new local
4768 -- object can then be used instead of the formal in case it is used
4769 -- in an actual to a call to a nested subprogram.
4771 -- This method is used to supplement our "small integer model" for
4772 -- accessibility-check generation (for more information see
4773 -- Accessibility_Level).
4775 -- Because we allow accessibility values greater than our expected value
4776 -- passing along the same extra accessibility formal as an actual
4777 -- to a nested subprogram becomes a problem because high values mean
4778 -- different things to the callee even though they are the same to the
4779 -- caller. So, as described in the first section, we create a local
4780 -- object representing the minimum of the accessibility level value that
4781 -- is passed in and the accessibility level of the callee's parameter
4782 -- and locals and use it in the case of a call to a nested subprogram.
4783 -- This generated object is refered to as a "minimum accessiblity
4786 if Present (Spec_Id) or else Present (Body_Id) then
4787 Body_Nod := Unit_Declaration_Node (Body_Id);
4792 -- Grab the appropriate formal depending on whether there exists
4793 -- an actual spec for the subprogram or whether we are dealing
4794 -- with a protected subprogram.
4796 if Present (Spec_Id) then
4797 if Present (Protected_Body_Subprogram (Spec_Id)) then
4798 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4800 Form := First_Formal (Spec_Id);
4803 Form := First_Formal (Body_Id);
4806 -- Loop through formals if the subprogram is capable of accepting
4807 -- a generated local object. If it is not then it is also not
4808 -- capable of having local subprograms meaning it would not need
4809 -- a minimum accessibility level object anyway.
4811 if Present (Body_Nod)
4812 and then Has_Declarations (Body_Nod)
4813 and then Nkind (Body_Nod) /= N_Package_Specification
4815 while Present (Form) loop
4817 if Present (Extra_Accessibility (Form))
4818 and then No (Minimum_Accessibility (Form))
4820 -- Generate the minimum accessibility level object
4822 -- A60b : constant natural := natural'min(1, paramL);
4824 Generate_Minimum_Accessibility
4825 (Extra_Accessibility (Form), Form);
4831 -- Generate the minimum accessibility level object for the
4832 -- function's Extra_Accessibility_Of_Result.
4834 -- A31b : constant natural := natural'min (2, funcL);
4836 if Ekind (Body_Id) = E_Function
4837 and then Present (Extra_Accessibility_Of_Result (Body_Id))
4839 Generate_Minimum_Accessibility
4840 (Extra_Accessibility_Of_Result (Body_Id));
4842 -- Replace the Extra_Accessibility_Of_Result with the new
4843 -- minimum accessibility object.
4845 Set_Extra_Accessibility_Of_Result
4846 (Body_Id, Minimum_Accessibility
4847 (Extra_Accessibility_Of_Result (Body_Id)));
4853 -- Now we can go on to analyze the body
4855 HSS := Handled_Statement_Sequence (N);
4856 Set_Actual_Subtypes (N, Current_Scope);
4858 -- Add a declaration for the Protection object, renaming declarations
4859 -- for discriminals and privals and finally a declaration for the entry
4860 -- family index (if applicable). This form of early expansion is done
4861 -- when the Expander is active because Install_Private_Data_Declarations
4862 -- references entities which were created during regular expansion. The
4863 -- subprogram entity must come from source, and not be an internally
4864 -- generated subprogram.
4867 and then Present (Prot_Typ)
4868 and then Present (Spec_Id)
4869 and then Comes_From_Source (Spec_Id)
4870 and then not Is_Eliminated (Spec_Id)
4872 Install_Private_Data_Declarations
4873 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4876 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4877 -- may now appear in parameter and result profiles. Since the analysis
4878 -- of a subprogram body may use the parameter and result profile of the
4879 -- spec, swap any limited views with their non-limited counterpart.
4881 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4882 Exch_Views := Exchange_Limited_Views (Spec_Id);
4885 -- If the return type is an anonymous access type whose designated type
4886 -- is the limited view of a class-wide type and the non-limited view is
4887 -- available, update the return type accordingly.
4889 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4895 Rtyp := Etype (Spec_Id);
4897 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4898 Etyp := Directly_Designated_Type (Rtyp);
4900 if Is_Class_Wide_Type (Etyp)
4901 and then From_Limited_With (Etyp)
4904 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4910 -- Analyze any aspect specifications that appear on the subprogram body
4912 if Has_Aspects (N) then
4913 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4916 Analyze_Declarations (Declarations (N));
4918 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4920 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4921 if Present (SPARK_Pragma (Spec_Id)) then
4922 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4924 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4926 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4927 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4928 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4930 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4933 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4936 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4937 -- as specified in source code, or because SPARK_Mode On is ignored
4938 -- in an instance where the context is SPARK_Mode Off/Auto.
4940 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4941 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4946 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4947 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4948 Error_Msg_Sloc := Sloc (Spec_Id);
4950 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4954 -- A subprogram body freezes its own contract. Analyze the contract
4955 -- after the declarations of the body have been processed as pragmas
4956 -- are now chained on the contract of the subprogram body.
4958 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4960 -- Check completion, and analyze the statements
4963 Inspect_Deferred_Constant_Completion (Declarations (N));
4966 -- Add the generated minimum accessibility objects to the subprogram
4967 -- body's list of declarations after analysis of the statements and
4970 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4971 if Present (Declarations (Body_Nod)) then
4972 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4975 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4979 -- Deal with end of scope processing for the body
4981 Process_End_Label (HSS, 't', Current_Scope);
4982 Update_Use_Clause_Chain;
4985 -- If we are compiling an entry wrapper, remove the enclosing
4986 -- synchronized object from the stack.
4988 if Is_Entry_Wrapper (Body_Id) then
4992 Check_Subprogram_Order (N);
4993 Set_Analyzed (Body_Id);
4995 -- If we have a separate spec, then the analysis of the declarations
4996 -- caused the entities in the body to be chained to the spec id, but
4997 -- we want them chained to the body id. Only the formal parameters
4998 -- end up chained to the spec id in this case.
5000 if Present (Spec_Id) then
5002 -- We must conform to the categorization of our spec
5004 Validate_Categorization_Dependency (N, Spec_Id);
5006 -- And if this is a child unit, the parent units must conform
5008 if Is_Child_Unit (Spec_Id) then
5009 Validate_Categorization_Dependency
5010 (Unit_Declaration_Node (Spec_Id), Spec_Id);
5013 -- Here is where we move entities from the spec to the body
5015 -- Case where there are entities that stay with the spec
5017 if Present (Last_Real_Spec_Entity) then
5019 -- No body entities (happens when the only real spec entities come
5020 -- from precondition and postcondition pragmas).
5022 if No (Last_Entity (Body_Id)) then
5023 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
5025 -- Body entities present (formals), so chain stuff past them
5029 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
5032 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
5033 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5034 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
5036 -- Case where there are no spec entities, in this case there can be
5037 -- no body entities either, so just move everything.
5039 -- If the body is generated for an expression function, it may have
5040 -- been preanalyzed already, if 'access was applied to it.
5043 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
5044 N_Expression_Function
5046 pragma Assert (No (Last_Entity (Body_Id)));
5050 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
5051 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5052 Set_First_Entity (Spec_Id, Empty);
5053 Set_Last_Entity (Spec_Id, Empty);
5056 -- Otherwise the body does not complete a previous declaration. Check
5057 -- the categorization of the body against the units it withs.
5060 Validate_Categorization_Dependency (N, Body_Id);
5063 Check_Missing_Return;
5065 -- Now we are going to check for variables that are never modified in
5066 -- the body of the procedure. But first we deal with a special case
5067 -- where we want to modify this check. If the body of the subprogram
5068 -- starts with a raise statement or its equivalent, or if the body
5069 -- consists entirely of a null statement, then it is pretty obvious that
5070 -- it is OK to not reference the parameters. For example, this might be
5071 -- the following common idiom for a stubbed function: statement of the
5072 -- procedure raises an exception. In particular this deals with the
5073 -- common idiom of a stubbed function, which appears something like:
5075 -- function F (A : Integer) return Some_Type;
5078 -- raise Program_Error;
5082 -- Here the purpose of X is simply to satisfy the annoying requirement
5083 -- in Ada that there be at least one return, and we certainly do not
5084 -- want to go posting warnings on X that it is not initialized. On
5085 -- the other hand, if X is entirely unreferenced that should still
5088 -- What we do is to detect these cases, and if we find them, flag the
5089 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
5090 -- suppress unwanted warnings. For the case of the function stub above
5091 -- we have a special test to set X as apparently assigned to suppress
5098 -- Skip call markers installed by the ABE mechanism, labels, and
5099 -- Push_xxx_Error_Label to find the first real statement.
5101 Stm := First (Statements (HSS));
5102 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
5106 -- Do the test on the original statement before expansion
5109 Ostm : constant Node_Id := Original_Node (Stm);
5112 -- If explicit raise statement, turn on flag
5114 if Nkind (Ostm) = N_Raise_Statement then
5115 Set_Trivial_Subprogram (Stm);
5117 -- If null statement, and no following statements, turn on flag
5119 elsif Nkind (Stm) = N_Null_Statement
5120 and then Comes_From_Source (Stm)
5121 and then No (Next (Stm))
5123 Set_Trivial_Subprogram (Stm);
5125 -- Check for explicit call cases which likely raise an exception
5127 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
5128 if Is_Entity_Name (Name (Ostm)) then
5130 Ent : constant Entity_Id := Entity (Name (Ostm));
5133 -- If the procedure is marked No_Return, then likely it
5134 -- raises an exception, but in any case it is not coming
5135 -- back here, so turn on the flag.
5138 and then Ekind (Ent) = E_Procedure
5139 and then No_Return (Ent)
5141 Set_Trivial_Subprogram (Stm);
5149 -- Check for variables that are never modified
5156 -- If there is a separate spec, then transfer Never_Set_In_Source
5157 -- flags from out parameters to the corresponding entities in the
5158 -- body. The reason we do that is we want to post error flags on
5159 -- the body entities, not the spec entities.
5161 if Present (Spec_Id) then
5162 E1 := First_Entity (Spec_Id);
5163 while Present (E1) loop
5164 if Ekind (E1) = E_Out_Parameter then
5165 E2 := First_Entity (Body_Id);
5166 while Present (E2) loop
5167 exit when Chars (E1) = Chars (E2);
5171 if Present (E2) then
5172 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
5180 -- Check references of the subprogram spec when we are dealing with
5181 -- an expression function due to it having a generated body.
5182 -- Otherwise, we simply check the formals of the subprogram body.
5184 if Present (Spec_Id)
5185 and then Is_Expression_Function (Spec_Id)
5187 Check_References (Spec_Id);
5189 Check_References (Body_Id);
5193 -- Check for nested subprogram, and mark outer level subprogram if so
5199 if Present (Spec_Id) then
5206 Ent := Enclosing_Subprogram (Ent);
5207 exit when No (Ent) or else Is_Subprogram (Ent);
5210 if Present (Ent) then
5211 Set_Has_Nested_Subprogram (Ent);
5215 -- Restore the limited views in the spec, if any, to let the back end
5216 -- process it without running into circularities.
5218 if Exch_Views /= No_Elist then
5219 Restore_Limited_Views (Exch_Views);
5222 if Mask_Types /= No_Elist then
5223 Unmask_Unfrozen_Types (Mask_Types);
5226 if Present (Desig_View) then
5227 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5231 if Present (Ignored_Ghost_Region) then
5232 Expander_Active := Saved_EA;
5235 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5236 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5237 end Analyze_Subprogram_Body_Helper;
5239 ------------------------------------
5240 -- Analyze_Subprogram_Declaration --
5241 ------------------------------------
5243 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5244 Scop : constant Entity_Id := Current_Scope;
5245 Designator : Entity_Id;
5247 Is_Completion : Boolean;
5248 -- Indicates whether a null procedure declaration is a completion
5251 -- Null procedures are not allowed in SPARK
5253 if Nkind (Specification (N)) = N_Procedure_Specification
5254 and then Null_Present (Specification (N))
5256 -- Null procedures are allowed in protected types, following the
5257 -- recent AI12-0147.
5259 if Is_Protected_Type (Current_Scope)
5260 and then Ada_Version < Ada_2012
5262 Error_Msg_N ("protected operation cannot be a null procedure", N);
5265 Analyze_Null_Procedure (N, Is_Completion);
5267 -- The null procedure acts as a body, nothing further is needed
5269 if Is_Completion then
5274 Designator := Analyze_Subprogram_Specification (Specification (N));
5276 -- A reference may already have been generated for the unit name, in
5277 -- which case the following call is redundant. However it is needed for
5278 -- declarations that are the rewriting of an expression function.
5280 Generate_Definition (Designator);
5282 -- Set the SPARK mode from the current context (may be overwritten later
5283 -- with explicit pragma). This is not done for entry barrier functions
5284 -- because they are generated outside the protected type and should not
5285 -- carry the mode of the enclosing context.
5287 if Nkind (N) = N_Subprogram_Declaration
5288 and then Is_Entry_Barrier_Function (N)
5293 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5294 Set_SPARK_Pragma_Inherited (Designator);
5297 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5298 -- the body of this subprogram is instantiated or inlined later and out
5299 -- of context. The body uses this attribute to restore the value of the
5302 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5303 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5306 -- Preserve relevant elaboration-related attributes of the context which
5307 -- are no longer available or very expensive to recompute once analysis,
5308 -- resolution, and expansion are over.
5310 Mark_Elaboration_Attributes
5311 (N_Id => Designator,
5315 if Debug_Flag_C then
5316 Write_Str ("==> subprogram spec ");
5317 Write_Name (Chars (Designator));
5318 Write_Str (" from ");
5319 Write_Location (Sloc (N));
5324 Validate_RCI_Subprogram_Declaration (N);
5325 New_Overloaded_Entity (Designator);
5326 Check_Delayed_Subprogram (Designator);
5328 -- If the type of the first formal of the current subprogram is a non-
5329 -- generic tagged private type, mark the subprogram as being a private
5330 -- primitive. Ditto if this is a function with controlling result, and
5331 -- the return type is currently private. In both cases, the type of the
5332 -- controlling argument or result must be in the current scope for the
5333 -- operation to be primitive.
5335 if Has_Controlling_Result (Designator)
5336 and then Is_Private_Type (Etype (Designator))
5337 and then Scope (Etype (Designator)) = Current_Scope
5338 and then not Is_Generic_Actual_Type (Etype (Designator))
5340 Set_Is_Private_Primitive (Designator);
5342 elsif Present (First_Formal (Designator)) then
5344 Formal_Typ : constant Entity_Id :=
5345 Etype (First_Formal (Designator));
5347 Set_Is_Private_Primitive (Designator,
5348 Is_Tagged_Type (Formal_Typ)
5349 and then Scope (Formal_Typ) = Current_Scope
5350 and then Is_Private_Type (Formal_Typ)
5351 and then not Is_Generic_Actual_Type (Formal_Typ));
5355 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5358 if Ada_Version >= Ada_2005
5359 and then Comes_From_Source (N)
5360 and then Is_Dispatching_Operation (Designator)
5367 if Has_Controlling_Result (Designator) then
5368 Etyp := Etype (Designator);
5371 E := First_Entity (Designator);
5373 and then Is_Formal (E)
5374 and then not Is_Controlling_Formal (E)
5382 if Is_Access_Type (Etyp) then
5383 Etyp := Directly_Designated_Type (Etyp);
5386 if Is_Interface (Etyp)
5387 and then not Is_Abstract_Subprogram (Designator)
5388 and then not (Ekind (Designator) = E_Procedure
5389 and then Null_Present (Specification (N)))
5391 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5393 -- Specialize error message based on procedures vs. functions,
5394 -- since functions can't be null subprograms.
5396 if Ekind (Designator) = E_Procedure then
5398 ("interface procedure % must be abstract or null", N);
5401 ("interface function % must be abstract", N);
5407 -- What is the following code for, it used to be
5409 -- ??? Set_Suppress_Elaboration_Checks
5410 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5412 -- The following seems equivalent, but a bit dubious
5414 if Elaboration_Checks_Suppressed (Designator) then
5415 Set_Kill_Elaboration_Checks (Designator);
5418 -- For a compilation unit, set body required. This flag will only be
5419 -- reset if a valid Import or Interface pragma is processed later on.
5421 if Nkind (Parent (N)) = N_Compilation_Unit then
5422 Set_Body_Required (Parent (N), True);
5424 if Ada_Version >= Ada_2005
5425 and then Nkind (Specification (N)) = N_Procedure_Specification
5426 and then Null_Present (Specification (N))
5429 ("null procedure cannot be declared at library level", N);
5433 Generate_Reference_To_Formals (Designator);
5434 Check_Eliminated (Designator);
5436 if Debug_Flag_C then
5438 Write_Str ("<== subprogram spec ");
5439 Write_Name (Chars (Designator));
5440 Write_Str (" from ");
5441 Write_Location (Sloc (N));
5445 -- Indicate that this is a protected operation, because it may be used
5446 -- in subsequent declarations within the protected type.
5448 if Is_Protected_Type (Current_Scope) then
5449 Set_Convention (Designator, Convention_Protected);
5452 List_Inherited_Pre_Post_Aspects (Designator);
5454 -- Process the aspects before establishing the proper categorization in
5455 -- case the subprogram is a compilation unit and one of its aspects is
5456 -- converted into a categorization pragma.
5458 if Has_Aspects (N) then
5459 Analyze_Aspect_Specifications (N, Designator);
5462 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5463 Set_Categorization_From_Scope (Designator, Scop);
5465 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5466 -- proper categorization of the unit based on its pragmas.
5469 Push_Scope (Designator);
5470 Set_Categorization_From_Pragmas (N);
5471 Validate_Categorization_Dependency (N, Designator);
5474 end Analyze_Subprogram_Declaration;
5476 --------------------------------------
5477 -- Analyze_Subprogram_Specification --
5478 --------------------------------------
5480 -- Reminder: N here really is a subprogram specification (not a subprogram
5481 -- declaration). This procedure is called to analyze the specification in
5482 -- both subprogram bodies and subprogram declarations (specs).
5484 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5485 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5486 -- Determine whether entity E denotes the spec or body of an invariant
5489 ------------------------------------
5490 -- Is_Invariant_Procedure_Or_Body --
5491 ------------------------------------
5493 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5494 Decl : constant Node_Id := Unit_Declaration_Node (E);
5498 if Nkind (Decl) = N_Subprogram_Body then
5499 Spec := Corresponding_Spec (Decl);
5506 and then Ekind (Spec) = E_Procedure
5507 and then (Is_Partial_Invariant_Procedure (Spec)
5508 or else Is_Invariant_Procedure (Spec));
5509 end Is_Invariant_Procedure_Or_Body;
5513 Designator : constant Entity_Id := Defining_Entity (N);
5514 Formals : constant List_Id := Parameter_Specifications (N);
5516 -- Start of processing for Analyze_Subprogram_Specification
5519 -- Proceed with analysis. Do not emit a cross-reference entry if the
5520 -- specification comes from an expression function, because it may be
5521 -- the completion of a previous declaration. If it is not, the cross-
5522 -- reference entry will be emitted for the new subprogram declaration.
5524 if Nkind (Parent (N)) /= N_Expression_Function then
5525 Generate_Definition (Designator);
5528 if Nkind (N) = N_Function_Specification then
5529 Set_Ekind (Designator, E_Function);
5530 Set_Mechanism (Designator, Default_Mechanism);
5532 Set_Ekind (Designator, E_Procedure);
5533 Set_Etype (Designator, Standard_Void_Type);
5536 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5537 -- those subprograms which could be inlined in GNATprove mode (because
5538 -- Body_To_Inline is non-Empty) but should not be inlined.
5540 if GNATprove_Mode then
5541 Set_Is_Inlined_Always (Designator);
5544 -- Introduce new scope for analysis of the formals and the return type
5546 Set_Scope (Designator, Current_Scope);
5548 if Present (Formals) then
5549 Push_Scope (Designator);
5550 Process_Formals (Formals, N);
5552 -- Check dimensions in N for formals with default expression
5554 Analyze_Dimension_Formals (N, Formals);
5556 -- Ada 2005 (AI-345): If this is an overriding operation of an
5557 -- inherited interface operation, and the controlling type is
5558 -- a synchronized type, replace the type with its corresponding
5559 -- record, to match the proper signature of an overriding operation.
5560 -- Same processing for an access parameter whose designated type is
5561 -- derived from a synchronized interface.
5563 -- This modification is not done for invariant procedures because
5564 -- the corresponding record may not necessarely be visible when the
5565 -- concurrent type acts as the full view of a private type.
5568 -- type Prot is private with Type_Invariant => ...;
5569 -- procedure ConcInvariant (Obj : Prot);
5571 -- protected type Prot is ...;
5572 -- type Concurrent_Record_Prot is record ...;
5573 -- procedure ConcInvariant (Obj : Prot) is
5575 -- end ConcInvariant;
5578 -- In the example above, both the spec and body of the invariant
5579 -- procedure must utilize the private type as the controlling type.
5581 if Ada_Version >= Ada_2005
5582 and then not Is_Invariant_Procedure_Or_Body (Designator)
5586 Formal_Typ : Entity_Id;
5587 Rec_Typ : Entity_Id;
5588 Desig_Typ : Entity_Id;
5591 Formal := First_Formal (Designator);
5592 while Present (Formal) loop
5593 Formal_Typ := Etype (Formal);
5595 if Is_Concurrent_Type (Formal_Typ)
5596 and then Present (Corresponding_Record_Type (Formal_Typ))
5598 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5600 if Present (Interfaces (Rec_Typ)) then
5601 Set_Etype (Formal, Rec_Typ);
5604 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5605 Desig_Typ := Designated_Type (Formal_Typ);
5607 if Is_Concurrent_Type (Desig_Typ)
5608 and then Present (Corresponding_Record_Type (Desig_Typ))
5610 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5612 if Present (Interfaces (Rec_Typ)) then
5613 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5618 Next_Formal (Formal);
5625 -- The subprogram scope is pushed and popped around the processing of
5626 -- the return type for consistency with call above to Process_Formals
5627 -- (which itself can call Analyze_Return_Type), and to ensure that any
5628 -- itype created for the return type will be associated with the proper
5631 elsif Nkind (N) = N_Function_Specification then
5632 Push_Scope (Designator);
5633 Analyze_Return_Type (N);
5639 if Nkind (N) = N_Function_Specification then
5641 -- Deal with operator symbol case
5643 if Nkind (Designator) = N_Defining_Operator_Symbol then
5644 Valid_Operator_Definition (Designator);
5647 May_Need_Actuals (Designator);
5649 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5650 -- the subprogram is abstract also. This does not apply to renaming
5651 -- declarations, where abstractness is inherited, and to subprogram
5652 -- bodies generated for stream operations, which become renamings as
5655 -- In case of primitives associated with abstract interface types
5656 -- the check is applied later (see Analyze_Subprogram_Declaration).
5658 if Nkind (Original_Node (Parent (N))) not in
5659 N_Abstract_Subprogram_Declaration |
5660 N_Formal_Abstract_Subprogram_Declaration |
5661 N_Subprogram_Renaming_Declaration
5663 if Is_Abstract_Type (Etype (Designator)) then
5665 ("function that returns abstract type must be abstract", N);
5667 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5668 -- access result whose designated type is abstract.
5670 elsif Ada_Version >= Ada_2012
5671 and then Nkind (Result_Definition (N)) = N_Access_Definition
5673 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5674 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5677 ("function whose access result designates abstract type "
5678 & "must be abstract", N);
5684 end Analyze_Subprogram_Specification;
5686 -----------------------
5687 -- Check_Conformance --
5688 -----------------------
5690 procedure Check_Conformance
5691 (New_Id : Entity_Id;
5693 Ctype : Conformance_Type;
5695 Conforms : out Boolean;
5696 Err_Loc : Node_Id := Empty;
5697 Get_Inst : Boolean := False;
5698 Skip_Controlling_Formals : Boolean := False)
5700 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5701 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5702 -- If Errmsg is True, then processing continues to post an error message
5703 -- for conformance error on given node. Two messages are output. The
5704 -- first message points to the previous declaration with a general "no
5705 -- conformance" message. The second is the detailed reason, supplied as
5706 -- Msg. The parameter N provide information for a possible & insertion
5707 -- in the message, and also provides the location for posting the
5708 -- message in the absence of a specified Err_Loc location.
5710 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5711 -- True if the conventions of entities Id1 and Id2 match.
5713 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5714 -- True if the null exclusions of two formals of anonymous access type
5717 -----------------------
5718 -- Conformance_Error --
5719 -----------------------
5721 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5728 if No (Err_Loc) then
5734 Error_Msg_Sloc := Sloc (Old_Id);
5737 when Type_Conformant =>
5738 Error_Msg_N -- CODEFIX
5739 ("not type conformant with declaration#!", Enode);
5741 when Mode_Conformant =>
5742 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5744 ("not mode conformant with operation inherited#!",
5748 ("not mode conformant with declaration#!", Enode);
5751 when Subtype_Conformant =>
5752 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5754 ("not subtype conformant with operation inherited#!",
5758 ("not subtype conformant with declaration#!", Enode);
5761 when Fully_Conformant =>
5762 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5763 Error_Msg_N -- CODEFIX
5764 ("not fully conformant with operation inherited#!",
5767 Error_Msg_N -- CODEFIX
5768 ("not fully conformant with declaration#!", Enode);
5772 Error_Msg_NE (Msg, Enode, N);
5774 end Conformance_Error;
5776 -----------------------
5777 -- Conventions_Match --
5778 -----------------------
5780 function Conventions_Match
5782 Id2 : Entity_Id) return Boolean
5785 -- Ignore the conventions of anonymous access-to-subprogram types
5786 -- and subprogram types because these are internally generated and
5787 -- the only way these may receive a convention is if they inherit
5788 -- the convention of a related subprogram.
5790 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
5793 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
5798 -- Otherwise compare the conventions directly
5801 return Convention (Id1) = Convention (Id2);
5803 end Conventions_Match;
5805 ---------------------------
5806 -- Null_Exclusions_Match --
5807 ---------------------------
5809 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
5811 if not Is_Anonymous_Access_Type (Etype (F1))
5812 or else not Is_Anonymous_Access_Type (Etype (F2))
5817 -- AI12-0289-1: Case of controlling access parameter; False if the
5818 -- partial view is untagged, the full view is tagged, and no explicit
5819 -- "not null". Note that at this point, we're processing the package
5820 -- body, so private/full types have been swapped. The Sloc test below
5821 -- is to detect the (legal) case where F1 comes after the full type
5822 -- declaration. This part is disabled pre-2005, because "not null" is
5823 -- not allowed on those language versions.
5825 if Ada_Version >= Ada_2005
5826 and then Is_Controlling_Formal (F1)
5827 and then not Null_Exclusion_Present (Parent (F1))
5828 and then not Null_Exclusion_Present (Parent (F2))
5831 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
5832 Partial_View_Of_Desig : constant Entity_Id :=
5833 Incomplete_Or_Partial_View (D);
5835 return No (Partial_View_Of_Desig)
5836 or else Is_Tagged_Type (Partial_View_Of_Desig)
5837 or else Sloc (D) < Sloc (F1);
5840 -- Not a controlling parameter, or one or both views have an explicit
5844 return Null_Exclusion_Present (Parent (F1)) =
5845 Null_Exclusion_Present (Parent (F2));
5847 end Null_Exclusions_Match;
5851 Old_Type : constant Entity_Id := Etype (Old_Id);
5852 New_Type : constant Entity_Id := Etype (New_Id);
5853 Old_Formal : Entity_Id;
5854 New_Formal : Entity_Id;
5855 Old_Formal_Base : Entity_Id;
5856 New_Formal_Base : Entity_Id;
5858 -- Start of processing for Check_Conformance
5863 -- We need a special case for operators, since they don't appear
5866 if Ctype = Type_Conformant then
5867 if Ekind (New_Id) = E_Operator
5868 and then Operator_Matches_Spec (New_Id, Old_Id)
5874 -- If both are functions/operators, check return types conform
5876 if Old_Type /= Standard_Void_Type
5878 New_Type /= Standard_Void_Type
5880 -- If we are checking interface conformance we omit controlling
5881 -- arguments and result, because we are only checking the conformance
5882 -- of the remaining parameters.
5884 if Has_Controlling_Result (Old_Id)
5885 and then Has_Controlling_Result (New_Id)
5886 and then Skip_Controlling_Formals
5890 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5891 if Ctype >= Subtype_Conformant
5892 and then not Predicates_Match (Old_Type, New_Type)
5895 ("\predicate of return type does not match!", New_Id);
5898 ("\return type does not match!", New_Id);
5904 -- Ada 2005 (AI-231): In case of anonymous access types check the
5905 -- null-exclusion and access-to-constant attributes match.
5907 if Ada_Version >= Ada_2005
5908 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5910 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5911 or else Is_Access_Constant (Etype (Old_Type)) /=
5912 Is_Access_Constant (Etype (New_Type)))
5914 Conformance_Error ("\return type does not match!", New_Id);
5918 -- If either is a function/operator and the other isn't, error
5920 elsif Old_Type /= Standard_Void_Type
5921 or else New_Type /= Standard_Void_Type
5923 Conformance_Error ("\functions can only match functions!", New_Id);
5927 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5928 -- If this is a renaming as body, refine error message to indicate that
5929 -- the conflict is with the original declaration. If the entity is not
5930 -- frozen, the conventions don't have to match, the one of the renamed
5931 -- entity is inherited.
5933 if Ctype >= Subtype_Conformant then
5934 if not Conventions_Match (Old_Id, New_Id) then
5935 if not Is_Frozen (New_Id) then
5938 elsif Present (Err_Loc)
5939 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5940 and then Present (Corresponding_Spec (Err_Loc))
5942 Error_Msg_Name_1 := Chars (New_Id);
5944 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5945 Conformance_Error ("\prior declaration for% has convention %!");
5949 Conformance_Error ("\calling conventions do not match!");
5953 Check_Formal_Subprogram_Conformance
5954 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
5956 if not Conforms then
5962 -- Deal with parameters
5964 -- Note: we use the entity information, rather than going directly
5965 -- to the specification in the tree. This is not only simpler, but
5966 -- absolutely necessary for some cases of conformance tests between
5967 -- operators, where the declaration tree simply does not exist.
5969 Old_Formal := First_Formal (Old_Id);
5970 New_Formal := First_Formal (New_Id);
5971 while Present (Old_Formal) and then Present (New_Formal) loop
5972 if Is_Controlling_Formal (Old_Formal)
5973 and then Is_Controlling_Formal (New_Formal)
5974 and then Skip_Controlling_Formals
5976 -- The controlling formals will have different types when
5977 -- comparing an interface operation with its match, but both
5978 -- or neither must be access parameters.
5980 if Is_Access_Type (Etype (Old_Formal))
5982 Is_Access_Type (Etype (New_Formal))
5984 goto Skip_Controlling_Formal;
5987 ("\access parameter does not match!", New_Formal);
5991 -- Ada 2012: Mode conformance also requires that formal parameters
5992 -- be both aliased, or neither.
5994 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5995 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5997 ("\aliased parameter mismatch!", New_Formal);
6001 if Ctype = Fully_Conformant then
6003 -- Names must match. Error message is more accurate if we do
6004 -- this before checking that the types of the formals match.
6006 if Chars (Old_Formal) /= Chars (New_Formal) then
6007 Conformance_Error ("\name& does not match!", New_Formal);
6009 -- Set error posted flag on new formal as well to stop
6010 -- junk cascaded messages in some cases.
6012 Set_Error_Posted (New_Formal);
6016 -- Null exclusion must match
6018 if not Null_Exclusions_Match (Old_Formal, New_Formal) then
6020 ("\null exclusion for& does not match", New_Formal);
6022 -- Mark error posted on the new formal to avoid duplicated
6023 -- complaint about types not matching.
6025 Set_Error_Posted (New_Formal);
6029 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6030 -- case occurs whenever a subprogram is being renamed and one of its
6031 -- parameters imposes a null exclusion. For example:
6033 -- type T is null record;
6034 -- type Acc_T is access T;
6035 -- subtype Acc_T_Sub is Acc_T;
6037 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6038 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6041 Old_Formal_Base := Etype (Old_Formal);
6042 New_Formal_Base := Etype (New_Formal);
6045 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6046 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6049 -- Types must always match. In the visible part of an instance,
6050 -- usual overloading rules for dispatching operations apply, and
6051 -- we check base types (not the actual subtypes).
6053 if In_Instance_Visible_Part
6054 and then Is_Dispatching_Operation (New_Id)
6056 if not Conforming_Types
6057 (T1 => Base_Type (Etype (Old_Formal)),
6058 T2 => Base_Type (Etype (New_Formal)),
6060 Get_Inst => Get_Inst)
6062 Conformance_Error ("\type of & does not match!", New_Formal);
6066 elsif not Conforming_Types
6067 (T1 => Old_Formal_Base,
6068 T2 => New_Formal_Base,
6070 Get_Inst => Get_Inst)
6072 -- Don't give error message if old type is Any_Type. This test
6073 -- avoids some cascaded errors, e.g. in case of a bad spec.
6075 if Errmsg and then Old_Formal_Base = Any_Type then
6078 if Ctype >= Subtype_Conformant
6080 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6083 ("\predicate of & does not match!", New_Formal);
6086 ("\type of & does not match!", New_Formal);
6088 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
6090 Error_Msg_N ("\dimensions mismatch!", New_Formal);
6098 -- For mode conformance, mode must match
6100 if Ctype >= Mode_Conformant then
6101 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6102 if Ekind (New_Id) not in E_Function | E_Procedure
6103 or else not Is_Primitive_Wrapper (New_Id)
6105 Conformance_Error ("\mode of & does not match!", New_Formal);
6109 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6111 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
6116 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
6120 ("\mode of & does not match!", New_Formal);
6127 elsif Is_Access_Type (Old_Formal_Base)
6128 and then Is_Access_Type (New_Formal_Base)
6129 and then Is_Access_Constant (Old_Formal_Base) /=
6130 Is_Access_Constant (New_Formal_Base)
6133 ("\constant modifier does not match!", New_Formal);
6138 if Ctype >= Subtype_Conformant then
6140 -- Ada 2005 (AI-231): In case of anonymous access types check
6141 -- the null-exclusion and access-to-constant attributes must
6142 -- match. For null exclusion, we test the types rather than the
6143 -- formals themselves, since the attribute is only set reliably
6144 -- on the formals in the Ada 95 case, and we exclude the case
6145 -- where Old_Formal is marked as controlling, to avoid errors
6146 -- when matching completing bodies with dispatching declarations
6147 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6149 if Ada_Version >= Ada_2005
6150 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
6151 and then Is_Anonymous_Access_Type (Etype (New_Formal))
6153 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6154 Can_Never_Be_Null (Etype (New_Formal))
6156 not Is_Controlling_Formal (Old_Formal))
6158 Is_Access_Constant (Etype (Old_Formal)) /=
6159 Is_Access_Constant (Etype (New_Formal)))
6161 -- Do not complain if error already posted on New_Formal. This
6162 -- avoids some redundant error messages.
6164 and then not Error_Posted (New_Formal)
6166 -- It is allowed to omit the null-exclusion in case of stream
6167 -- attribute subprograms. We recognize stream subprograms
6168 -- through their TSS-generated suffix.
6171 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6174 if TSS_Name /= TSS_Stream_Read
6175 and then TSS_Name /= TSS_Stream_Write
6176 and then TSS_Name /= TSS_Stream_Input
6177 and then TSS_Name /= TSS_Stream_Output
6179 -- Here we have a definite conformance error. It is worth
6180 -- special casing the error message for the case of a
6181 -- controlling formal (which excludes null).
6183 if Is_Controlling_Formal (New_Formal) then
6184 Error_Msg_Node_2 := Scope (New_Formal);
6186 ("\controlling formal & of & excludes null, "
6187 & "declaration must exclude null as well",
6190 -- Normal case (couldn't we give more detail here???)
6194 ("\type of & does not match!", New_Formal);
6203 -- Full conformance checks
6205 if Ctype = Fully_Conformant then
6207 -- We have checked already that names match
6209 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6211 -- Check default expressions for in parameters
6214 NewD : constant Boolean :=
6215 Present (Default_Value (New_Formal));
6216 OldD : constant Boolean :=
6217 Present (Default_Value (Old_Formal));
6219 if NewD or OldD then
6221 -- The old default value has been analyzed because the
6222 -- current full declaration will have frozen everything
6223 -- before. The new default value has not been analyzed,
6224 -- so analyze it now before we check for conformance.
6227 Push_Scope (New_Id);
6228 Preanalyze_Spec_Expression
6229 (Default_Value (New_Formal), Etype (New_Formal));
6233 if not (NewD and OldD)
6234 or else not Fully_Conformant_Expressions
6235 (Default_Value (Old_Formal),
6236 Default_Value (New_Formal))
6239 ("\default expression for & does not match!",
6248 -- A couple of special checks for Ada 83 mode. These checks are
6249 -- skipped if either entity is an operator in package Standard,
6250 -- or if either old or new instance is not from the source program.
6252 if Ada_Version = Ada_83
6253 and then Sloc (Old_Id) > Standard_Location
6254 and then Sloc (New_Id) > Standard_Location
6255 and then Comes_From_Source (Old_Id)
6256 and then Comes_From_Source (New_Id)
6259 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6260 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6263 -- Explicit IN must be present or absent in both cases. This
6264 -- test is required only in the full conformance case.
6266 if In_Present (Old_Param) /= In_Present (New_Param)
6267 and then Ctype = Fully_Conformant
6270 ("\(Ada 83) IN must appear in both declarations",
6275 -- Grouping (use of comma in param lists) must be the same
6276 -- This is where we catch a misconformance like:
6279 -- A : Integer; B : Integer
6281 -- which are represented identically in the tree except
6282 -- for the setting of the flags More_Ids and Prev_Ids.
6284 if More_Ids (Old_Param) /= More_Ids (New_Param)
6285 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6288 ("\grouping of & does not match!", New_Formal);
6294 -- This label is required when skipping controlling formals
6296 <<Skip_Controlling_Formal>>
6298 Next_Formal (Old_Formal);
6299 Next_Formal (New_Formal);
6302 if Present (Old_Formal) then
6303 Conformance_Error ("\too few parameters!");
6306 elsif Present (New_Formal) then
6307 Conformance_Error ("\too many parameters!", New_Formal);
6310 end Check_Conformance;
6312 -----------------------
6313 -- Check_Conventions --
6314 -----------------------
6316 procedure Check_Conventions (Typ : Entity_Id) is
6317 Ifaces_List : Elist_Id;
6319 procedure Check_Convention (Op : Entity_Id);
6320 -- Verify that the convention of inherited dispatching operation Op is
6321 -- consistent among all subprograms it overrides. In order to minimize
6322 -- the search, Search_From is utilized to designate a specific point in
6323 -- the list rather than iterating over the whole list once more.
6325 ----------------------
6326 -- Check_Convention --
6327 ----------------------
6329 procedure Check_Convention (Op : Entity_Id) is
6330 Op_Conv : constant Convention_Id := Convention (Op);
6331 Iface_Conv : Convention_Id;
6332 Iface_Elmt : Elmt_Id;
6333 Iface_Prim_Elmt : Elmt_Id;
6334 Iface_Prim : Entity_Id;
6337 Iface_Elmt := First_Elmt (Ifaces_List);
6338 while Present (Iface_Elmt) loop
6340 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6341 while Present (Iface_Prim_Elmt) loop
6342 Iface_Prim := Node (Iface_Prim_Elmt);
6343 Iface_Conv := Convention (Iface_Prim);
6345 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6346 and then Iface_Conv /= Op_Conv
6349 ("inconsistent conventions in primitive operations", Typ);
6351 Error_Msg_Name_1 := Chars (Op);
6352 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6353 Error_Msg_Sloc := Sloc (Op);
6355 if Comes_From_Source (Op) or else No (Alias (Op)) then
6356 if not Present (Overridden_Operation (Op)) then
6357 Error_Msg_N ("\\primitive % defined #", Typ);
6360 ("\\overriding operation % with "
6361 & "convention % defined #", Typ);
6364 else pragma Assert (Present (Alias (Op)));
6365 Error_Msg_Sloc := Sloc (Alias (Op));
6366 Error_Msg_N ("\\inherited operation % with "
6367 & "convention % defined #", Typ);
6370 Error_Msg_Name_1 := Chars (Op);
6371 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6372 Error_Msg_Sloc := Sloc (Iface_Prim);
6373 Error_Msg_N ("\\overridden operation % with "
6374 & "convention % defined #", Typ);
6376 -- Avoid cascading errors
6381 Next_Elmt (Iface_Prim_Elmt);
6384 Next_Elmt (Iface_Elmt);
6386 end Check_Convention;
6390 Prim_Op : Entity_Id;
6391 Prim_Op_Elmt : Elmt_Id;
6393 -- Start of processing for Check_Conventions
6396 if not Has_Interfaces (Typ) then
6400 Collect_Interfaces (Typ, Ifaces_List);
6402 -- The algorithm checks every overriding dispatching operation against
6403 -- all the corresponding overridden dispatching operations, detecting
6404 -- differences in conventions.
6406 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6407 while Present (Prim_Op_Elmt) loop
6408 Prim_Op := Node (Prim_Op_Elmt);
6410 -- A small optimization: skip the predefined dispatching operations
6411 -- since they always have the same convention.
6413 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6414 Check_Convention (Prim_Op);
6417 Next_Elmt (Prim_Op_Elmt);
6419 end Check_Conventions;
6421 ------------------------------
6422 -- Check_Delayed_Subprogram --
6423 ------------------------------
6425 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6426 procedure Possible_Freeze (T : Entity_Id);
6427 -- T is the type of either a formal parameter or of the return type. If
6428 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6429 -- itself must be delayed.
6431 ---------------------
6432 -- Possible_Freeze --
6433 ---------------------
6435 procedure Possible_Freeze (T : Entity_Id) is
6436 Scop : constant Entity_Id := Scope (Designator);
6439 -- If the subprogram appears within a package instance (which may be
6440 -- the wrapper package of a subprogram instance) the freeze node for
6441 -- that package will freeze the subprogram at the proper place, so
6442 -- do not emit a freeze node for the subprogram, given that it may
6443 -- appear in the wrong scope.
6445 if Ekind (Scop) = E_Package
6446 and then not Comes_From_Source (Scop)
6447 and then Is_Generic_Instance (Scop)
6451 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6452 Set_Has_Delayed_Freeze (Designator);
6454 elsif Is_Access_Type (T)
6455 and then Has_Delayed_Freeze (Designated_Type (T))
6456 and then not Is_Frozen (Designated_Type (T))
6458 Set_Has_Delayed_Freeze (Designator);
6460 end Possible_Freeze;
6466 -- Start of processing for Check_Delayed_Subprogram
6469 -- All subprograms, including abstract subprograms, may need a freeze
6470 -- node if some formal type or the return type needs one.
6472 Possible_Freeze (Etype (Designator));
6473 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6475 -- Need delayed freeze if any of the formal types themselves need a
6476 -- delayed freeze and are not yet frozen.
6478 F := First_Formal (Designator);
6479 while Present (F) loop
6480 Possible_Freeze (Etype (F));
6481 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6485 -- Mark functions that return by reference. Note that it cannot be done
6486 -- for delayed_freeze subprograms because the underlying returned type
6487 -- may not be known yet (for private types).
6489 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6491 Typ : constant Entity_Id := Etype (Designator);
6492 Utyp : constant Entity_Id := Underlying_Type (Typ);
6495 if Is_Limited_View (Typ) then
6496 Set_Returns_By_Ref (Designator);
6498 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6499 Set_Returns_By_Ref (Designator);
6503 end Check_Delayed_Subprogram;
6505 ------------------------------------
6506 -- Check_Discriminant_Conformance --
6507 ------------------------------------
6509 procedure Check_Discriminant_Conformance
6514 Old_Discr : Entity_Id := First_Discriminant (Prev);
6515 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6516 New_Discr_Id : Entity_Id;
6517 New_Discr_Type : Entity_Id;
6519 procedure Conformance_Error (Msg : String; N : Node_Id);
6520 -- Post error message for conformance error on given node. Two messages
6521 -- are output. The first points to the previous declaration with a
6522 -- general "no conformance" message. The second is the detailed reason,
6523 -- supplied as Msg. The parameter N provide information for a possible
6524 -- & insertion in the message.
6526 -----------------------
6527 -- Conformance_Error --
6528 -----------------------
6530 procedure Conformance_Error (Msg : String; N : Node_Id) is
6532 Error_Msg_Sloc := Sloc (Prev_Loc);
6533 Error_Msg_N -- CODEFIX
6534 ("not fully conformant with declaration#!", N);
6535 Error_Msg_NE (Msg, N, N);
6536 end Conformance_Error;
6538 -- Start of processing for Check_Discriminant_Conformance
6541 while Present (Old_Discr) and then Present (New_Discr) loop
6542 New_Discr_Id := Defining_Identifier (New_Discr);
6544 -- The subtype mark of the discriminant on the full type has not
6545 -- been analyzed so we do it here. For an access discriminant a new
6548 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6550 Access_Definition (N, Discriminant_Type (New_Discr));
6553 Find_Type (Discriminant_Type (New_Discr));
6554 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6556 -- Ada 2005: if the discriminant definition carries a null
6557 -- exclusion, create an itype to check properly for consistency
6558 -- with partial declaration.
6560 if Is_Access_Type (New_Discr_Type)
6561 and then Null_Exclusion_Present (New_Discr)
6564 Create_Null_Excluding_Itype
6565 (T => New_Discr_Type,
6566 Related_Nod => New_Discr,
6567 Scope_Id => Current_Scope);
6571 if not Conforming_Types
6572 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6574 Conformance_Error ("type of & does not match!", New_Discr_Id);
6577 -- Treat the new discriminant as an occurrence of the old one,
6578 -- for navigation purposes, and fill in some semantic
6579 -- information, for completeness.
6581 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6582 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6583 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6588 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6589 Conformance_Error ("name & does not match!", New_Discr_Id);
6593 -- Default expressions must match
6596 NewD : constant Boolean :=
6597 Present (Expression (New_Discr));
6598 OldD : constant Boolean :=
6599 Present (Expression (Parent (Old_Discr)));
6602 if NewD or OldD then
6604 -- The old default value has been analyzed and expanded,
6605 -- because the current full declaration will have frozen
6606 -- everything before. The new default values have not been
6607 -- expanded, so expand now to check conformance.
6610 Preanalyze_Spec_Expression
6611 (Expression (New_Discr), New_Discr_Type);
6614 if not (NewD and OldD)
6615 or else not Fully_Conformant_Expressions
6616 (Expression (Parent (Old_Discr)),
6617 Expression (New_Discr))
6621 ("default expression for & does not match!",
6628 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6630 if Ada_Version = Ada_83 then
6632 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6635 -- Grouping (use of comma in param lists) must be the same
6636 -- This is where we catch a misconformance like:
6639 -- A : Integer; B : Integer
6641 -- which are represented identically in the tree except
6642 -- for the setting of the flags More_Ids and Prev_Ids.
6644 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6645 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6648 ("grouping of & does not match!", New_Discr_Id);
6654 Next_Discriminant (Old_Discr);
6658 if Present (Old_Discr) then
6659 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6662 elsif Present (New_Discr) then
6664 ("too many discriminants!", Defining_Identifier (New_Discr));
6667 end Check_Discriminant_Conformance;
6669 -----------------------------------------
6670 -- Check_Formal_Subprogram_Conformance --
6671 -----------------------------------------
6673 procedure Check_Formal_Subprogram_Conformance
6674 (New_Id : Entity_Id;
6678 Conforms : out Boolean)
6684 if Is_Formal_Subprogram (Old_Id)
6685 or else Is_Formal_Subprogram (New_Id)
6686 or else (Is_Subprogram (New_Id)
6687 and then Present (Alias (New_Id))
6688 and then Is_Formal_Subprogram (Alias (New_Id)))
6690 if Present (Err_Loc) then
6699 Error_Msg_Sloc := Sloc (Old_Id);
6700 Error_Msg_N ("not subtype conformant with declaration#!", N);
6702 ("\formal subprograms are not subtype conformant "
6703 & "(RM 6.3.1 (17/3))", N, New_Id);
6706 end Check_Formal_Subprogram_Conformance;
6708 procedure Check_Formal_Subprogram_Conformance
6709 (New_Id : Entity_Id;
6711 Err_Loc : Node_Id := Empty)
6715 Check_Formal_Subprogram_Conformance
6716 (New_Id, Old_Id, Err_Loc, True, Ignore);
6717 end Check_Formal_Subprogram_Conformance;
6719 ----------------------------
6720 -- Check_Fully_Conformant --
6721 ----------------------------
6723 procedure Check_Fully_Conformant
6724 (New_Id : Entity_Id;
6726 Err_Loc : Node_Id := Empty)
6729 pragma Warnings (Off, Result);
6732 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6733 end Check_Fully_Conformant;
6735 --------------------------
6736 -- Check_Limited_Return --
6737 --------------------------
6739 procedure Check_Limited_Return
6745 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6746 -- replaced by anonymous access results. This is an incompatibility with
6747 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6748 -- controllable with special switch. ???
6750 -- A limited interface that is not immutably limited is OK
6752 if Is_Limited_Interface (R_Type)
6754 not (Is_Task_Interface (R_Type)
6755 or else Is_Protected_Interface (R_Type)
6756 or else Is_Synchronized_Interface (R_Type))
6760 elsif Is_Limited_Type (R_Type)
6761 and then not Is_Interface (R_Type)
6762 and then Comes_From_Source (N)
6763 and then not In_Instance_Body
6764 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6766 -- Error in Ada 2005
6768 if Ada_Version >= Ada_2005
6769 and then not Debug_Flag_Dot_L
6770 and then not GNAT_Mode
6773 ("(Ada 2005) cannot copy object of a limited type "
6774 & "(RM-2005 6.5(5.5/2))", Expr);
6776 if Is_Limited_View (R_Type) then
6778 ("\return by reference not permitted in Ada 2005", Expr);
6781 -- Warn in Ada 95 mode, to give folks a heads up about this
6784 -- In GNAT mode, this is just a warning, to allow it to be evilly
6785 -- turned off. Otherwise it is a real error.
6787 -- In a generic context, simplify the warning because it makes no
6788 -- sense to discuss pass-by-reference or copy.
6790 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6791 if Inside_A_Generic then
6793 ("return of limited object not permitted in Ada 2005 "
6794 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6796 elsif Is_Limited_View (R_Type) then
6798 ("return by reference not permitted in Ada 2005 "
6799 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6802 ("cannot copy object of a limited type in Ada 2005 "
6803 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6806 -- Ada 95 mode, and compatibility warnings disabled
6809 pragma Assert (Ada_Version <= Ada_95);
6810 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6811 return; -- skip continuation messages below
6814 if not Inside_A_Generic then
6816 ("\consider switching to return of access type", Expr);
6817 Explain_Limited_Type (R_Type, Expr);
6820 end Check_Limited_Return;
6822 ---------------------------
6823 -- Check_Mode_Conformant --
6824 ---------------------------
6826 procedure Check_Mode_Conformant
6827 (New_Id : Entity_Id;
6829 Err_Loc : Node_Id := Empty;
6830 Get_Inst : Boolean := False)
6833 pragma Warnings (Off, Result);
6836 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6837 end Check_Mode_Conformant;
6839 --------------------------------
6840 -- Check_Overriding_Indicator --
6841 --------------------------------
6843 procedure Check_Overriding_Indicator
6845 Overridden_Subp : Entity_Id;
6846 Is_Primitive : Boolean)
6852 -- No overriding indicator for literals
6854 if Ekind (Subp) = E_Enumeration_Literal then
6857 elsif Ekind (Subp) = E_Entry then
6858 Decl := Parent (Subp);
6860 -- No point in analyzing a malformed operator
6862 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6863 and then Error_Posted (Subp)
6868 Decl := Unit_Declaration_Node (Subp);
6871 if Nkind (Decl) in N_Subprogram_Body
6872 | N_Subprogram_Body_Stub
6873 | N_Subprogram_Declaration
6874 | N_Abstract_Subprogram_Declaration
6875 | N_Subprogram_Renaming_Declaration
6877 Spec := Specification (Decl);
6879 elsif Nkind (Decl) = N_Entry_Declaration then
6886 -- An overriding indication is illegal on a subprogram declared
6887 -- in a protected body, where there is no operation to override.
6889 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6890 and then Is_List_Member (Decl)
6891 and then Present (Parent (List_Containing (Decl)))
6892 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6895 ("illegal overriding indication in protected body", Decl);
6899 -- The overriding operation is type conformant with the overridden one,
6900 -- but the names of the formals are not required to match. If the names
6901 -- appear permuted in the overriding operation, this is a possible
6902 -- source of confusion that is worth diagnosing. Controlling formals
6903 -- often carry names that reflect the type, and it is not worthwhile
6904 -- requiring that their names match.
6906 if Present (Overridden_Subp)
6907 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6914 Form1 := First_Formal (Subp);
6915 Form2 := First_Formal (Overridden_Subp);
6917 -- If the overriding operation is a synchronized operation, skip
6918 -- the first parameter of the overridden operation, which is
6919 -- implicit in the new one. If the operation is declared in the
6920 -- body it is not primitive and all formals must match.
6922 if Is_Concurrent_Type (Scope (Subp))
6923 and then Is_Tagged_Type (Scope (Subp))
6924 and then not Has_Completion (Scope (Subp))
6926 Form2 := Next_Formal (Form2);
6929 if Present (Form1) then
6930 Form1 := Next_Formal (Form1);
6931 Form2 := Next_Formal (Form2);
6934 while Present (Form1) loop
6935 if not Is_Controlling_Formal (Form1)
6936 and then Present (Next_Formal (Form2))
6937 and then Chars (Form1) = Chars (Next_Formal (Form2))
6939 Error_Msg_Node_2 := Alias (Overridden_Subp);
6940 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6942 ("& does not match corresponding formal of&#",
6947 Next_Formal (Form1);
6948 Next_Formal (Form2);
6953 -- If there is an overridden subprogram, then check that there is no
6954 -- "not overriding" indicator, and mark the subprogram as overriding.
6956 -- This is not done if the overridden subprogram is marked as hidden,
6957 -- which can occur for the case of inherited controlled operations
6958 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6959 -- subprogram is not itself hidden or we are within a generic instance,
6960 -- in which case the hidden flag may have been modified for the
6961 -- expansion of the instance.
6963 -- (Note: This condition could probably be simplified, leaving out the
6964 -- testing for the specific controlled cases, but it seems safer and
6965 -- clearer this way, and echoes similar special-case tests of this
6966 -- kind in other places.)
6968 if Present (Overridden_Subp)
6969 and then (not Is_Hidden (Overridden_Subp)
6971 (Chars (Overridden_Subp) in Name_Initialize
6974 and then Present (Alias (Overridden_Subp))
6975 and then (not Is_Hidden (Alias (Overridden_Subp))
6976 or else In_Instance)))
6978 if Must_Not_Override (Spec) then
6979 Error_Msg_Sloc := Sloc (Overridden_Subp);
6981 if Ekind (Subp) = E_Entry then
6983 ("entry & overrides inherited operation #", Spec, Subp);
6986 ("subprogram & overrides inherited operation #", Spec, Subp);
6989 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6990 -- as an extension of Root_Controlled, and thus has a useless Adjust
6991 -- operation. This operation should not be inherited by other limited
6992 -- controlled types. An explicit Adjust for them is not overriding.
6994 elsif Must_Override (Spec)
6995 and then Chars (Overridden_Subp) = Name_Adjust
6996 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6997 and then Present (Alias (Overridden_Subp))
6998 and then In_Predefined_Unit (Alias (Overridden_Subp))
7001 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
7002 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7004 elsif Is_Subprogram (Subp) then
7005 if Is_Init_Proc (Subp) then
7008 elsif No (Overridden_Operation (Subp)) then
7010 -- For entities generated by Derive_Subprograms the overridden
7011 -- operation is the inherited primitive (which is available
7012 -- through the attribute alias)
7014 if (Is_Dispatching_Operation (Subp)
7015 or else Is_Dispatching_Operation (Overridden_Subp))
7016 and then not Comes_From_Source (Overridden_Subp)
7017 and then Find_Dispatching_Type (Overridden_Subp) =
7018 Find_Dispatching_Type (Subp)
7019 and then Present (Alias (Overridden_Subp))
7020 and then Comes_From_Source (Alias (Overridden_Subp))
7022 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7023 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
7026 Set_Overridden_Operation (Subp, Overridden_Subp);
7027 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
7032 -- If primitive flag is set or this is a protected operation, then
7033 -- the operation is overriding at the point of its declaration, so
7034 -- warn if necessary. Otherwise it may have been declared before the
7035 -- operation it overrides and no check is required.
7038 and then not Must_Override (Spec)
7039 and then (Is_Primitive
7040 or else Ekind (Scope (Subp)) = E_Protected_Type)
7042 Style.Missing_Overriding (Decl, Subp);
7045 -- If Subp is an operator, it may override a predefined operation, if
7046 -- it is defined in the same scope as the type to which it applies.
7047 -- In that case Overridden_Subp is empty because of our implicit
7048 -- representation for predefined operators. We have to check whether the
7049 -- signature of Subp matches that of a predefined operator. Note that
7050 -- first argument provides the name of the operator, and the second
7051 -- argument the signature that may match that of a standard operation.
7052 -- If the indicator is overriding, then the operator must match a
7053 -- predefined signature, because we know already that there is no
7054 -- explicit overridden operation.
7056 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7057 if Must_Not_Override (Spec) then
7059 -- If this is not a primitive or a protected subprogram, then
7060 -- "not overriding" is illegal.
7063 and then Ekind (Scope (Subp)) /= E_Protected_Type
7065 Error_Msg_N ("overriding indicator only allowed "
7066 & "if subprogram is primitive", Subp);
7068 elsif Can_Override_Operator (Subp) then
7070 ("subprogram& overrides predefined operator ", Spec, Subp);
7073 elsif Must_Override (Spec) then
7074 if No (Overridden_Operation (Subp))
7075 and then not Can_Override_Operator (Subp)
7077 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7080 elsif not Error_Posted (Subp)
7081 and then Style_Check
7082 and then Can_Override_Operator (Subp)
7083 and then not In_Predefined_Unit (Subp)
7085 -- If style checks are enabled, indicate that the indicator is
7086 -- missing. However, at the point of declaration, the type of
7087 -- which this is a primitive operation may be private, in which
7088 -- case the indicator would be premature.
7090 if Has_Private_Declaration (Etype (Subp))
7091 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7095 Style.Missing_Overriding (Decl, Subp);
7099 elsif Must_Override (Spec) then
7100 if Ekind (Subp) = E_Entry then
7101 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7103 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7106 -- If the operation is marked "not overriding" and it's not primitive
7107 -- then an error is issued, unless this is an operation of a task or
7108 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7109 -- has been specified have already been checked above.
7111 elsif Must_Not_Override (Spec)
7112 and then not Is_Primitive
7113 and then Ekind (Subp) /= E_Entry
7114 and then Ekind (Scope (Subp)) /= E_Protected_Type
7117 ("overriding indicator only allowed if subprogram is primitive",
7121 end Check_Overriding_Indicator;
7127 -- Note: this procedure needs to know far too much about how the expander
7128 -- messes with exceptions. The use of the flag Exception_Junk and the
7129 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7130 -- works, but is not very clean. It would be better if the expansion
7131 -- routines would leave Original_Node working nicely, and we could use
7132 -- Original_Node here to ignore all the peculiar expander messing ???
7134 procedure Check_Returns
7138 Proc : Entity_Id := Empty)
7142 procedure Check_Statement_Sequence (L : List_Id);
7143 -- Internal recursive procedure to check a list of statements for proper
7144 -- termination by a return statement (or a transfer of control or a
7145 -- compound statement that is itself internally properly terminated).
7147 ------------------------------
7148 -- Check_Statement_Sequence --
7149 ------------------------------
7151 procedure Check_Statement_Sequence (L : List_Id) is
7156 function Assert_False return Boolean;
7157 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7158 -- rewritten as a null statement when assertions are off. The assert
7159 -- is not active, but it is still enough to kill the warning.
7165 function Assert_False return Boolean is
7166 Orig : constant Node_Id := Original_Node (Last_Stm);
7169 if Nkind (Orig) = N_Pragma
7170 and then Pragma_Name (Orig) = Name_Assert
7171 and then not Error_Posted (Orig)
7174 Arg : constant Node_Id :=
7175 First (Pragma_Argument_Associations (Orig));
7176 Exp : constant Node_Id := Expression (Arg);
7178 return Nkind (Exp) = N_Identifier
7179 and then Chars (Exp) = Name_False;
7189 Raise_Exception_Call : Boolean;
7190 -- Set True if statement sequence terminated by Raise_Exception call
7191 -- or a Reraise_Occurrence call.
7193 -- Start of processing for Check_Statement_Sequence
7196 Raise_Exception_Call := False;
7198 -- Get last real statement
7200 Last_Stm := Last (L);
7202 -- Deal with digging out exception handler statement sequences that
7203 -- have been transformed by the local raise to goto optimization.
7204 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7205 -- optimization has occurred, we are looking at something like:
7208 -- original stmts in block
7212 -- goto L1; | omitted if No_Exception_Propagation
7217 -- goto L3; -- skip handler when exception not raised
7219 -- <<L1>> -- target label for local exception
7233 -- and what we have to do is to dig out the estmts1 and estmts2
7234 -- sequences (which were the original sequences of statements in
7235 -- the exception handlers) and check them.
7237 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7242 exit when Nkind (Stm) /= N_Block_Statement;
7243 exit when not Exception_Junk (Stm);
7246 exit when Nkind (Stm) /= N_Label;
7247 exit when not Exception_Junk (Stm);
7248 Check_Statement_Sequence
7249 (Statements (Handled_Statement_Sequence (Next (Stm))));
7254 exit when Nkind (Stm) /= N_Goto_Statement;
7255 exit when not Exception_Junk (Stm);
7259 -- Don't count pragmas
7261 while Nkind (Last_Stm) = N_Pragma
7263 -- Don't count call to SS_Release (can happen after Raise_Exception)
7266 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7268 Nkind (Name (Last_Stm)) = N_Identifier
7270 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7272 -- Don't count exception junk
7275 (Nkind (Last_Stm) in
7276 N_Goto_Statement | N_Label | N_Object_Declaration
7277 and then Exception_Junk (Last_Stm))
7278 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7280 -- Inserted code, such as finalization calls, is irrelevant: we only
7281 -- need to check original source.
7283 or else Is_Rewrite_Insertion (Last_Stm)
7288 -- Here we have the "real" last statement
7290 Kind := Nkind (Last_Stm);
7292 -- Transfer of control, OK. Note that in the No_Return procedure
7293 -- case, we already diagnosed any explicit return statements, so
7294 -- we can treat them as OK in this context.
7296 if Is_Transfer (Last_Stm) then
7299 -- Check cases of explicit non-indirect procedure calls
7301 elsif Kind = N_Procedure_Call_Statement
7302 and then Is_Entity_Name (Name (Last_Stm))
7304 -- Check call to Raise_Exception procedure which is treated
7305 -- specially, as is a call to Reraise_Occurrence.
7307 -- We suppress the warning in these cases since it is likely that
7308 -- the programmer really does not expect to deal with the case
7309 -- of Null_Occurrence, and thus would find a warning about a
7310 -- missing return curious, and raising Program_Error does not
7311 -- seem such a bad behavior if this does occur.
7313 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7314 -- behavior will be to raise Constraint_Error (see AI-329).
7316 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7318 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7320 Raise_Exception_Call := True;
7322 -- For Raise_Exception call, test first argument, if it is
7323 -- an attribute reference for a 'Identity call, then we know
7324 -- that the call cannot possibly return.
7327 Arg : constant Node_Id :=
7328 Original_Node (First_Actual (Last_Stm));
7330 if Nkind (Arg) = N_Attribute_Reference
7331 and then Attribute_Name (Arg) = Name_Identity
7338 -- If statement, need to look inside if there is an else and check
7339 -- each constituent statement sequence for proper termination.
7341 elsif Kind = N_If_Statement
7342 and then Present (Else_Statements (Last_Stm))
7344 Check_Statement_Sequence (Then_Statements (Last_Stm));
7345 Check_Statement_Sequence (Else_Statements (Last_Stm));
7347 if Present (Elsif_Parts (Last_Stm)) then
7349 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7352 while Present (Elsif_Part) loop
7353 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7361 -- Case statement, check each case for proper termination
7363 elsif Kind = N_Case_Statement then
7367 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7368 while Present (Case_Alt) loop
7369 Check_Statement_Sequence (Statements (Case_Alt));
7370 Next_Non_Pragma (Case_Alt);
7376 -- Block statement, check its handled sequence of statements
7378 elsif Kind = N_Block_Statement then
7384 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7393 -- Loop statement. If there is an iteration scheme, we can definitely
7394 -- fall out of the loop. Similarly if there is an exit statement, we
7395 -- can fall out. In either case we need a following return.
7397 elsif Kind = N_Loop_Statement then
7398 if Present (Iteration_Scheme (Last_Stm))
7399 or else Has_Exit (Entity (Identifier (Last_Stm)))
7403 -- A loop with no exit statement or iteration scheme is either
7404 -- an infinite loop, or it has some other exit (raise/return).
7405 -- In either case, no warning is required.
7411 -- Timed entry call, check entry call and delay alternatives
7413 -- Note: in expanded code, the timed entry call has been converted
7414 -- to a set of expanded statements on which the check will work
7415 -- correctly in any case.
7417 elsif Kind = N_Timed_Entry_Call then
7419 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7420 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7423 -- If statement sequence of entry call alternative is missing,
7424 -- then we can definitely fall through, and we post the error
7425 -- message on the entry call alternative itself.
7427 if No (Statements (ECA)) then
7430 -- If statement sequence of delay alternative is missing, then
7431 -- we can definitely fall through, and we post the error
7432 -- message on the delay alternative itself.
7434 -- Note: if both ECA and DCA are missing the return, then we
7435 -- post only one message, should be enough to fix the bugs.
7436 -- If not we will get a message next time on the DCA when the
7439 elsif No (Statements (DCA)) then
7442 -- Else check both statement sequences
7445 Check_Statement_Sequence (Statements (ECA));
7446 Check_Statement_Sequence (Statements (DCA));
7451 -- Conditional entry call, check entry call and else part
7453 -- Note: in expanded code, the conditional entry call has been
7454 -- converted to a set of expanded statements on which the check
7455 -- will work correctly in any case.
7457 elsif Kind = N_Conditional_Entry_Call then
7459 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7462 -- If statement sequence of entry call alternative is missing,
7463 -- then we can definitely fall through, and we post the error
7464 -- message on the entry call alternative itself.
7466 if No (Statements (ECA)) then
7469 -- Else check statement sequence and else part
7472 Check_Statement_Sequence (Statements (ECA));
7473 Check_Statement_Sequence (Else_Statements (Last_Stm));
7479 -- If we fall through, issue appropriate message
7483 -- Kill warning if last statement is a raise exception call,
7484 -- or a pragma Assert (False). Note that with assertions enabled,
7485 -- such a pragma has been converted into a raise exception call
7486 -- already, so the Assert_False is for the assertions off case.
7488 if not Raise_Exception_Call and then not Assert_False then
7490 -- In GNATprove mode, it is an error to have a missing return
7492 Error_Msg_Warn := SPARK_Mode /= On;
7494 -- Issue error message or warning
7497 ("RETURN statement missing following this statement<<!",
7500 ("\Program_Error ]<<!", Last_Stm);
7503 -- Note: we set Err even though we have not issued a warning
7504 -- because we still have a case of a missing return. This is
7505 -- an extremely marginal case, probably will never be noticed
7506 -- but we might as well get it right.
7510 -- Otherwise we have the case of a procedure marked No_Return
7513 if not Raise_Exception_Call then
7514 if GNATprove_Mode then
7516 ("implied return after this statement would have raised "
7517 & "Program_Error", Last_Stm);
7519 -- In normal compilation mode, do not warn on a generated call
7520 -- (e.g. in the body of a renaming as completion).
7522 elsif Comes_From_Source (Last_Stm) then
7524 ("implied return after this statement will raise "
7525 & "Program_Error??", Last_Stm);
7528 Error_Msg_Warn := SPARK_Mode /= On;
7530 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7534 RE : constant Node_Id :=
7535 Make_Raise_Program_Error (Sloc (Last_Stm),
7536 Reason => PE_Implicit_Return);
7538 Insert_After (Last_Stm, RE);
7542 end Check_Statement_Sequence;
7544 -- Start of processing for Check_Returns
7548 Check_Statement_Sequence (Statements (HSS));
7550 if Present (Exception_Handlers (HSS)) then
7551 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7552 while Present (Handler) loop
7553 Check_Statement_Sequence (Statements (Handler));
7554 Next_Non_Pragma (Handler);
7559 ----------------------------
7560 -- Check_Subprogram_Order --
7561 ----------------------------
7563 procedure Check_Subprogram_Order (N : Node_Id) is
7565 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7566 -- This is used to check if S1 > S2 in the sense required by this test,
7567 -- for example nameab < namec, but name2 < name10.
7569 -----------------------------
7570 -- Subprogram_Name_Greater --
7571 -----------------------------
7573 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7578 -- Deal with special case where names are identical except for a
7579 -- numerical suffix. These are handled specially, taking the numeric
7580 -- ordering from the suffix into account.
7583 while S1 (L1) in '0' .. '9' loop
7588 while S2 (L2) in '0' .. '9' loop
7592 -- If non-numeric parts non-equal, do straight compare
7594 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7597 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7598 -- that a missing suffix is treated as numeric zero in this test.
7602 while L1 < S1'Last loop
7604 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7608 while L2 < S2'Last loop
7610 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7615 end Subprogram_Name_Greater;
7617 -- Start of processing for Check_Subprogram_Order
7620 -- Check body in alpha order if this is option
7623 and then Style_Check_Order_Subprograms
7624 and then Nkind (N) = N_Subprogram_Body
7625 and then Comes_From_Source (N)
7626 and then In_Extended_Main_Source_Unit (N)
7630 renames Scope_Stack.Table
7631 (Scope_Stack.Last).Last_Subprogram_Name;
7633 Body_Id : constant Entity_Id :=
7634 Defining_Entity (Specification (N));
7637 Get_Decoded_Name_String (Chars (Body_Id));
7640 if Subprogram_Name_Greater
7641 (LSN.all, Name_Buffer (1 .. Name_Len))
7643 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7649 LSN := new String'(Name_Buffer (1 .. Name_Len));
7652 end Check_Subprogram_Order;
7654 ------------------------------
7655 -- Check_Subtype_Conformant --
7656 ------------------------------
7658 procedure Check_Subtype_Conformant
7659 (New_Id : Entity_Id;
7661 Err_Loc : Node_Id := Empty;
7662 Skip_Controlling_Formals : Boolean := False;
7663 Get_Inst : Boolean := False)
7666 pragma Warnings (Off, Result);
7669 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7670 Skip_Controlling_Formals => Skip_Controlling_Formals,
7671 Get_Inst => Get_Inst);
7672 end Check_Subtype_Conformant;
7674 -----------------------------------
7675 -- Check_Synchronized_Overriding --
7676 -----------------------------------
7678 procedure Check_Synchronized_Overriding
7679 (Def_Id : Entity_Id;
7680 Overridden_Subp : out Entity_Id)
7682 Ifaces_List : Elist_Id;
7686 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7687 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7688 -- subprogram is implemented by a protected procedure or entry,
7689 -- its first parameter must be out, in out, or access-to-variable.
7691 function Matches_Prefixed_View_Profile
7692 (Prim_Params : List_Id;
7693 Iface_Params : List_Id) return Boolean;
7694 -- Determine whether a subprogram's parameter profile Prim_Params
7695 -- matches that of a potentially overridden interface subprogram
7696 -- Iface_Params. Also determine if the type of first parameter of
7697 -- Iface_Params is an implemented interface.
7699 ----------------------
7700 -- Is_Valid_Formal --
7701 ----------------------
7703 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7706 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7708 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7709 and then not Constant_Present (Parameter_Type (Parent (F))));
7710 end Is_Valid_Formal;
7712 -----------------------------------
7713 -- Matches_Prefixed_View_Profile --
7714 -----------------------------------
7716 function Matches_Prefixed_View_Profile
7717 (Prim_Params : List_Id;
7718 Iface_Params : List_Id) return Boolean
7720 function Is_Implemented
7721 (Ifaces_List : Elist_Id;
7722 Iface : Entity_Id) return Boolean;
7723 -- Determine if Iface is implemented by the current task or
7726 --------------------
7727 -- Is_Implemented --
7728 --------------------
7730 function Is_Implemented
7731 (Ifaces_List : Elist_Id;
7732 Iface : Entity_Id) return Boolean
7734 Iface_Elmt : Elmt_Id;
7737 Iface_Elmt := First_Elmt (Ifaces_List);
7738 while Present (Iface_Elmt) loop
7739 if Node (Iface_Elmt) = Iface then
7743 Next_Elmt (Iface_Elmt);
7751 Iface_Id : Entity_Id;
7752 Iface_Param : Node_Id;
7753 Iface_Typ : Entity_Id;
7754 Prim_Id : Entity_Id;
7755 Prim_Param : Node_Id;
7756 Prim_Typ : Entity_Id;
7758 -- Start of processing for Matches_Prefixed_View_Profile
7761 Iface_Param := First (Iface_Params);
7762 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7764 if Is_Access_Type (Iface_Typ) then
7765 Iface_Typ := Designated_Type (Iface_Typ);
7768 Prim_Param := First (Prim_Params);
7770 -- The first parameter of the potentially overridden subprogram must
7771 -- be an interface implemented by Prim.
7773 if not Is_Interface (Iface_Typ)
7774 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7779 -- The checks on the object parameters are done, so move on to the
7780 -- rest of the parameters.
7782 if not In_Scope then
7787 while Present (Iface_Param) and then Present (Prim_Param) loop
7788 Iface_Id := Defining_Identifier (Iface_Param);
7789 Iface_Typ := Find_Parameter_Type (Iface_Param);
7791 Prim_Id := Defining_Identifier (Prim_Param);
7792 Prim_Typ := Find_Parameter_Type (Prim_Param);
7794 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7795 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7796 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7798 Iface_Typ := Designated_Type (Iface_Typ);
7799 Prim_Typ := Designated_Type (Prim_Typ);
7802 -- Case of multiple interface types inside a parameter profile
7804 -- (Obj_Param : in out Iface; ...; Param : Iface)
7806 -- If the interface type is implemented, then the matching type in
7807 -- the primitive should be the implementing record type.
7809 if Ekind (Iface_Typ) = E_Record_Type
7810 and then Is_Interface (Iface_Typ)
7811 and then Is_Implemented (Ifaces_List, Iface_Typ)
7813 if Prim_Typ /= Typ then
7817 -- The two parameters must be both mode and subtype conformant
7819 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7821 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7830 -- One of the two lists contains more parameters than the other
7832 if Present (Iface_Param) or else Present (Prim_Param) then
7837 end Matches_Prefixed_View_Profile;
7839 -- Start of processing for Check_Synchronized_Overriding
7842 Overridden_Subp := Empty;
7844 -- Def_Id must be an entry or a subprogram. We should skip predefined
7845 -- primitives internally generated by the front end; however at this
7846 -- stage predefined primitives are still not fully decorated. As a
7847 -- minor optimization we skip here internally generated subprograms.
7849 if (Ekind (Def_Id) /= E_Entry
7850 and then Ekind (Def_Id) /= E_Function
7851 and then Ekind (Def_Id) /= E_Procedure)
7852 or else not Comes_From_Source (Def_Id)
7857 -- Search for the concurrent declaration since it contains the list of
7858 -- all implemented interfaces. In this case, the subprogram is declared
7859 -- within the scope of a protected or a task type.
7861 if Present (Scope (Def_Id))
7862 and then Is_Concurrent_Type (Scope (Def_Id))
7863 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7865 Typ := Scope (Def_Id);
7868 -- The enclosing scope is not a synchronized type and the subprogram
7871 elsif No (First_Formal (Def_Id)) then
7874 -- The subprogram has formals and hence it may be a primitive of a
7878 Typ := Etype (First_Formal (Def_Id));
7880 if Is_Access_Type (Typ) then
7881 Typ := Directly_Designated_Type (Typ);
7884 if Is_Concurrent_Type (Typ)
7885 and then not Is_Generic_Actual_Type (Typ)
7889 -- This case occurs when the concurrent type is declared within a
7890 -- generic unit. As a result the corresponding record has been built
7891 -- and used as the type of the first formal, we just have to retrieve
7892 -- the corresponding concurrent type.
7894 elsif Is_Concurrent_Record_Type (Typ)
7895 and then not Is_Class_Wide_Type (Typ)
7896 and then Present (Corresponding_Concurrent_Type (Typ))
7898 Typ := Corresponding_Concurrent_Type (Typ);
7906 -- There is no overriding to check if this is an inherited operation in
7907 -- a type derivation for a generic actual.
7909 Collect_Interfaces (Typ, Ifaces_List);
7911 if Is_Empty_Elmt_List (Ifaces_List) then
7915 -- Determine whether entry or subprogram Def_Id overrides a primitive
7916 -- operation that belongs to one of the interfaces in Ifaces_List.
7919 Candidate : Entity_Id := Empty;
7920 Hom : Entity_Id := Empty;
7921 Subp : Entity_Id := Empty;
7924 -- Traverse the homonym chain, looking for a potentially overridden
7925 -- subprogram that belongs to an implemented interface.
7927 Hom := Current_Entity_In_Scope (Def_Id);
7928 while Present (Hom) loop
7932 or else not Is_Overloadable (Subp)
7933 or else not Is_Primitive (Subp)
7934 or else not Is_Dispatching_Operation (Subp)
7935 or else not Present (Find_Dispatching_Type (Subp))
7936 or else not Is_Interface (Find_Dispatching_Type (Subp))
7940 -- Entries and procedures can override abstract or null interface
7943 elsif Ekind (Def_Id) in E_Entry | E_Procedure
7944 and then Ekind (Subp) = E_Procedure
7945 and then Matches_Prefixed_View_Profile
7946 (Parameter_Specifications (Parent (Def_Id)),
7947 Parameter_Specifications (Parent (Subp)))
7951 -- For an overridden subprogram Subp, check whether the mode
7952 -- of its first parameter is correct depending on the kind of
7953 -- synchronized type.
7956 Formal : constant Node_Id := First_Formal (Candidate);
7959 -- In order for an entry or a protected procedure to
7960 -- override, the first parameter of the overridden routine
7961 -- must be of mode "out", "in out", or access-to-variable.
7963 if Ekind (Candidate) in E_Entry | E_Procedure
7964 and then Is_Protected_Type (Typ)
7965 and then not Is_Valid_Formal (Formal)
7969 -- All other cases are OK since a task entry or routine does
7970 -- not have a restriction on the mode of the first parameter
7971 -- of the overridden interface routine.
7974 Overridden_Subp := Candidate;
7979 -- Functions can override abstract interface functions. Return
7980 -- types must be subtype conformant.
7982 elsif Ekind (Def_Id) = E_Function
7983 and then Ekind (Subp) = E_Function
7984 and then Matches_Prefixed_View_Profile
7985 (Parameter_Specifications (Parent (Def_Id)),
7986 Parameter_Specifications (Parent (Subp)))
7987 and then Conforming_Types
7988 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7992 -- If an inherited subprogram is implemented by a protected
7993 -- function, then the first parameter of the inherited
7994 -- subprogram shall be of mode in, but not an access-to-
7995 -- variable parameter (RM 9.4(11/9)).
7997 if Present (First_Formal (Subp))
7998 and then Ekind (First_Formal (Subp)) = E_In_Parameter
8000 (not Is_Access_Type (Etype (First_Formal (Subp)))
8002 Is_Access_Constant (Etype (First_Formal (Subp))))
8004 Overridden_Subp := Subp;
8009 Hom := Homonym (Hom);
8012 -- After examining all candidates for overriding, we are left with
8013 -- the best match, which is a mode-incompatible interface routine.
8015 if In_Scope and then Present (Candidate) then
8016 Error_Msg_PT (Def_Id, Candidate);
8019 Overridden_Subp := Candidate;
8022 end Check_Synchronized_Overriding;
8024 ---------------------------
8025 -- Check_Type_Conformant --
8026 ---------------------------
8028 procedure Check_Type_Conformant
8029 (New_Id : Entity_Id;
8031 Err_Loc : Node_Id := Empty)
8034 pragma Warnings (Off, Result);
8037 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
8038 end Check_Type_Conformant;
8040 ---------------------------
8041 -- Can_Override_Operator --
8042 ---------------------------
8044 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8048 if Nkind (Subp) /= N_Defining_Operator_Symbol then
8052 Typ := Base_Type (Etype (First_Formal (Subp)));
8054 -- Check explicitly that the operation is a primitive of the type
8056 return Operator_Matches_Spec (Subp, Subp)
8057 and then not Is_Generic_Type (Typ)
8058 and then Scope (Subp) = Scope (Typ)
8059 and then not Is_Class_Wide_Type (Typ);
8061 end Can_Override_Operator;
8063 ----------------------
8064 -- Conforming_Types --
8065 ----------------------
8067 function Conforming_Types
8070 Ctype : Conformance_Type;
8071 Get_Inst : Boolean := False) return Boolean
8073 function Base_Types_Match
8075 Typ_2 : Entity_Id) return Boolean;
8076 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8077 -- in different scopes (e.g. parent and child instances), then verify
8078 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8079 -- the same subtype chain. The whole purpose of this procedure is to
8080 -- prevent spurious ambiguities in an instantiation that may arise if
8081 -- two distinct generic types are instantiated with the same actual.
8083 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8084 -- An access parameter can designate an incomplete type. If the
8085 -- incomplete type is the limited view of a type from a limited_
8086 -- with_clause, check whether the non-limited view is available.
8087 -- If it is a (non-limited) incomplete type, get the full view.
8089 function Matches_Limited_With_View
8091 Typ_2 : Entity_Id) return Boolean;
8092 -- Returns True if and only if either Typ_1 denotes a limited view of
8093 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8094 -- the limited with view of a type is used in a subprogram declaration
8095 -- and the subprogram body is in the scope of a regular with clause for
8096 -- the same unit. In such a case, the two type entities are considered
8097 -- identical for purposes of conformance checking.
8099 ----------------------
8100 -- Base_Types_Match --
8101 ----------------------
8103 function Base_Types_Match
8105 Typ_2 : Entity_Id) return Boolean
8107 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8108 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8111 if Typ_1 = Typ_2 then
8114 elsif Base_1 = Base_2 then
8116 -- The following is too permissive. A more precise test should
8117 -- check that the generic actual is an ancestor subtype of the
8120 -- See code in Find_Corresponding_Spec that applies an additional
8121 -- filter to handle accidental amiguities in instances.
8124 not Is_Generic_Actual_Type (Typ_1)
8125 or else not Is_Generic_Actual_Type (Typ_2)
8126 or else Scope (Typ_1) /= Scope (Typ_2);
8128 -- If Typ_2 is a generic actual type it is declared as the subtype of
8129 -- the actual. If that actual is itself a subtype we need to use its
8130 -- own base type to check for compatibility.
8132 elsif Ekind (Base_2) = Ekind (Typ_2)
8133 and then Base_1 = Base_Type (Base_2)
8137 elsif Ekind (Base_1) = Ekind (Typ_1)
8138 and then Base_2 = Base_Type (Base_1)
8145 end Base_Types_Match;
8147 --------------------------
8148 -- Find_Designated_Type --
8149 --------------------------
8151 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8155 Desig := Directly_Designated_Type (Typ);
8157 if Ekind (Desig) = E_Incomplete_Type then
8159 -- If regular incomplete type, get full view if available
8161 if Present (Full_View (Desig)) then
8162 Desig := Full_View (Desig);
8164 -- If limited view of a type, get non-limited view if available,
8165 -- and check again for a regular incomplete type.
8167 elsif Present (Non_Limited_View (Desig)) then
8168 Desig := Get_Full_View (Non_Limited_View (Desig));
8173 end Find_Designated_Type;
8175 -------------------------------
8176 -- Matches_Limited_With_View --
8177 -------------------------------
8179 function Matches_Limited_With_View
8181 Typ_2 : Entity_Id) return Boolean
8183 function Is_Matching_Limited_View
8185 View : Entity_Id) return Boolean;
8186 -- Determine whether non-limited view View denotes type Typ in some
8187 -- conformant fashion.
8189 ------------------------------
8190 -- Is_Matching_Limited_View --
8191 ------------------------------
8193 function Is_Matching_Limited_View
8195 View : Entity_Id) return Boolean
8197 Root_Typ : Entity_Id;
8198 Root_View : Entity_Id;
8201 -- The non-limited view directly denotes the type
8206 -- The type is a subtype of the non-limited view
8208 elsif Is_Subtype_Of (Typ, View) then
8211 -- Both the non-limited view and the type denote class-wide types
8213 elsif Is_Class_Wide_Type (Typ)
8214 and then Is_Class_Wide_Type (View)
8216 Root_Typ := Root_Type (Typ);
8217 Root_View := Root_Type (View);
8219 if Root_Typ = Root_View then
8222 -- An incomplete tagged type and its full view may receive two
8223 -- distinct class-wide types when the related package has not
8224 -- been analyzed yet.
8227 -- type T is tagged; -- CW_1
8228 -- type T is tagged null record; -- CW_2
8231 -- This is because the package lacks any semantic information
8232 -- that may eventually link both views of T. As a consequence,
8233 -- a client of the limited view of Pack will see CW_2 while a
8234 -- client of the non-limited view of Pack will see CW_1.
8236 elsif Is_Incomplete_Type (Root_Typ)
8237 and then Present (Full_View (Root_Typ))
8238 and then Full_View (Root_Typ) = Root_View
8242 elsif Is_Incomplete_Type (Root_View)
8243 and then Present (Full_View (Root_View))
8244 and then Full_View (Root_View) = Root_Typ
8251 end Is_Matching_Limited_View;
8253 -- Start of processing for Matches_Limited_With_View
8256 -- In some cases a type imported through a limited_with clause, and
8257 -- its non-limited view are both visible, for example in an anonymous
8258 -- access-to-class-wide type in a formal, or when building the body
8259 -- for a subprogram renaming after the subprogram has been frozen.
8260 -- In these cases both entities designate the same type. In addition,
8261 -- if one of them is an actual in an instance, it may be a subtype of
8262 -- the non-limited view of the other.
8264 if From_Limited_With (Typ_1)
8265 and then From_Limited_With (Typ_2)
8266 and then Available_View (Typ_1) = Available_View (Typ_2)
8270 elsif From_Limited_With (Typ_1) then
8271 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8273 elsif From_Limited_With (Typ_2) then
8274 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8279 end Matches_Limited_With_View;
8283 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8285 Type_1 : Entity_Id := T1;
8286 Type_2 : Entity_Id := T2;
8288 -- Start of processing for Conforming_Types
8291 -- The context is an instance association for a formal access-to-
8292 -- subprogram type; the formal parameter types require mapping because
8293 -- they may denote other formal parameters of the generic unit.
8296 Type_1 := Get_Instance_Of (T1);
8297 Type_2 := Get_Instance_Of (T2);
8300 -- If one of the types is a view of the other introduced by a limited
8301 -- with clause, treat these as conforming for all purposes.
8303 if Matches_Limited_With_View (T1, T2) then
8306 elsif Base_Types_Match (Type_1, Type_2) then
8307 if Ctype <= Mode_Conformant then
8312 Subtypes_Statically_Match (Type_1, Type_2)
8313 and then Dimensions_Match (Type_1, Type_2);
8316 elsif Is_Incomplete_Or_Private_Type (Type_1)
8317 and then Present (Full_View (Type_1))
8318 and then Base_Types_Match (Full_View (Type_1), Type_2)
8321 Ctype <= Mode_Conformant
8322 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8324 elsif Ekind (Type_2) = E_Incomplete_Type
8325 and then Present (Full_View (Type_2))
8326 and then Base_Types_Match (Type_1, Full_View (Type_2))
8329 Ctype <= Mode_Conformant
8330 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8332 elsif Is_Private_Type (Type_2)
8333 and then In_Instance
8334 and then Present (Full_View (Type_2))
8335 and then Base_Types_Match (Type_1, Full_View (Type_2))
8338 Ctype <= Mode_Conformant
8339 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8341 -- Another confusion between views in a nested instance with an
8342 -- actual private type whose full view is not in scope.
8344 elsif Ekind (Type_2) = E_Private_Subtype
8345 and then In_Instance
8346 and then Etype (Type_2) = Type_1
8350 -- In Ada 2012, incomplete types (including limited views) can appear
8351 -- as actuals in instantiations, where they are conformant to the
8352 -- corresponding incomplete formal.
8354 elsif Is_Incomplete_Type (Type_1)
8355 and then Is_Incomplete_Type (Type_2)
8356 and then In_Instance
8357 and then (Used_As_Generic_Actual (Type_1)
8358 or else Used_As_Generic_Actual (Type_2))
8363 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8364 -- treated recursively because they carry a signature. As far as
8365 -- conformance is concerned, convention plays no role, and either
8366 -- or both could be access to protected subprograms.
8368 Are_Anonymous_Access_To_Subprogram_Types :=
8369 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8370 | E_Anonymous_Access_Protected_Subprogram_Type
8372 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8373 | E_Anonymous_Access_Protected_Subprogram_Type;
8375 -- Test anonymous access type case. For this case, static subtype
8376 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8377 -- the base types because we may have built internal subtype entities
8378 -- to handle null-excluding types (see Process_Formals).
8380 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8382 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8384 -- Ada 2005 (AI-254)
8386 or else Are_Anonymous_Access_To_Subprogram_Types
8389 Desig_1 : Entity_Id;
8390 Desig_2 : Entity_Id;
8393 -- In Ada 2005, access constant indicators must match for
8394 -- subtype conformance.
8396 if Ada_Version >= Ada_2005
8397 and then Ctype >= Subtype_Conformant
8399 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8404 Desig_1 := Find_Designated_Type (Type_1);
8405 Desig_2 := Find_Designated_Type (Type_2);
8407 -- If the context is an instance association for a formal
8408 -- access-to-subprogram type; formal access parameter designated
8409 -- types require mapping because they may denote other formal
8410 -- parameters of the generic unit.
8413 Desig_1 := Get_Instance_Of (Desig_1);
8414 Desig_2 := Get_Instance_Of (Desig_2);
8417 -- It is possible for a Class_Wide_Type to be introduced for an
8418 -- incomplete type, in which case there is a separate class_ wide
8419 -- type for the full view. The types conform if their Etypes
8420 -- conform, i.e. one may be the full view of the other. This can
8421 -- only happen in the context of an access parameter, other uses
8422 -- of an incomplete Class_Wide_Type are illegal.
8424 if Is_Class_Wide_Type (Desig_1)
8426 Is_Class_Wide_Type (Desig_2)
8430 (Etype (Base_Type (Desig_1)),
8431 Etype (Base_Type (Desig_2)), Ctype);
8433 elsif Are_Anonymous_Access_To_Subprogram_Types then
8434 if Ada_Version < Ada_2005 then
8436 Ctype = Type_Conformant
8437 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8439 -- We must check the conformance of the signatures themselves
8443 Conformant : Boolean;
8446 (Desig_1, Desig_2, Ctype, False, Conformant);
8451 -- A limited view of an actual matches the corresponding
8452 -- incomplete formal.
8454 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8455 and then From_Limited_With (Desig_2)
8456 and then Used_As_Generic_Actual (Etype (Desig_2))
8461 return Base_Type (Desig_1) = Base_Type (Desig_2)
8462 and then (Ctype = Type_Conformant
8464 Subtypes_Statically_Match (Desig_1, Desig_2));
8468 -- Otherwise definitely no match
8471 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8472 and then Is_Access_Type (Type_2))
8473 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8474 and then Is_Access_Type (Type_1)))
8477 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8479 May_Hide_Profile := True;
8484 end Conforming_Types;
8486 --------------------------
8487 -- Create_Extra_Formals --
8488 --------------------------
8490 procedure Create_Extra_Formals (E : Entity_Id) is
8491 First_Extra : Entity_Id := Empty;
8493 Last_Extra : Entity_Id := Empty;
8495 function Add_Extra_Formal
8496 (Assoc_Entity : Entity_Id;
8499 Suffix : String) return Entity_Id;
8500 -- Add an extra formal to the current list of formals and extra formals.
8501 -- The extra formal is added to the end of the list of extra formals,
8502 -- and also returned as the result. These formals are always of mode IN.
8503 -- The new formal has the type Typ, is declared in Scope, and its name
8504 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8505 -- The following suffixes are currently used. They should not be changed
8506 -- without coordinating with CodePeer, which makes use of these to
8507 -- provide better messages.
8509 -- O denotes the Constrained bit.
8510 -- L denotes the accessibility level.
8511 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8512 -- the full list in exp_ch6.BIP_Formal_Kind.
8514 ----------------------
8515 -- Add_Extra_Formal --
8516 ----------------------
8518 function Add_Extra_Formal
8519 (Assoc_Entity : Entity_Id;
8522 Suffix : String) return Entity_Id
8524 EF : constant Entity_Id :=
8525 Make_Defining_Identifier (Sloc (Assoc_Entity),
8526 Chars => New_External_Name (Chars (Assoc_Entity),
8530 -- A little optimization. Never generate an extra formal for the
8531 -- _init operand of an initialization procedure, since it could
8534 if Chars (Formal) = Name_uInit then
8538 Set_Ekind (EF, E_In_Parameter);
8539 Set_Actual_Subtype (EF, Typ);
8540 Set_Etype (EF, Typ);
8541 Set_Scope (EF, Scope);
8542 Set_Mechanism (EF, Default_Mechanism);
8543 Set_Formal_Validity (EF);
8545 if No (First_Extra) then
8547 Set_Extra_Formals (Scope, EF);
8550 if Present (Last_Extra) then
8551 Set_Extra_Formal (Last_Extra, EF);
8557 end Add_Extra_Formal;
8561 Formal_Type : Entity_Id;
8562 P_Formal : Entity_Id := Empty;
8564 -- Start of processing for Create_Extra_Formals
8567 -- We never generate extra formals if expansion is not active because we
8568 -- don't need them unless we are generating code.
8570 if not Expander_Active then
8574 -- No need to generate extra formals in interface thunks whose target
8575 -- primitive has no extra formals.
8577 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8581 -- If this is a derived subprogram then the subtypes of the parent
8582 -- subprogram's formal parameters will be used to determine the need
8583 -- for extra formals.
8585 if Is_Overloadable (E) and then Present (Alias (E)) then
8586 P_Formal := First_Formal (Alias (E));
8589 Formal := First_Formal (E);
8590 while Present (Formal) loop
8591 Last_Extra := Formal;
8592 Next_Formal (Formal);
8595 -- If Extra_Formals were already created, don't do it again. This
8596 -- situation may arise for subprogram types created as part of
8597 -- dispatching calls (see Expand_Dispatching_Call).
8599 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8603 -- If the subprogram is a predefined dispatching subprogram then don't
8604 -- generate any extra constrained or accessibility level formals. In
8605 -- general we suppress these for internal subprograms (by not calling
8606 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8607 -- generated stream attributes do get passed through because extra
8608 -- build-in-place formals are needed in some cases (limited 'Input).
8610 if Is_Predefined_Internal_Operation (E) then
8611 goto Test_For_Func_Result_Extras;
8614 Formal := First_Formal (E);
8615 while Present (Formal) loop
8617 -- Create extra formal for supporting the attribute 'Constrained.
8618 -- The case of a private type view without discriminants also
8619 -- requires the extra formal if the underlying type has defaulted
8622 if Ekind (Formal) /= E_In_Parameter then
8623 if Present (P_Formal) then
8624 Formal_Type := Etype (P_Formal);
8626 Formal_Type := Etype (Formal);
8629 -- Do not produce extra formals for Unchecked_Union parameters.
8630 -- Jump directly to the end of the loop.
8632 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8633 goto Skip_Extra_Formal_Generation;
8636 if not Has_Discriminants (Formal_Type)
8637 and then Ekind (Formal_Type) in Private_Kind
8638 and then Present (Underlying_Type (Formal_Type))
8640 Formal_Type := Underlying_Type (Formal_Type);
8643 -- Suppress the extra formal if formal's subtype is constrained or
8644 -- indefinite, or we're compiling for Ada 2012 and the underlying
8645 -- type is tagged and limited. In Ada 2012, a limited tagged type
8646 -- can have defaulted discriminants, but 'Constrained is required
8647 -- to return True, so the formal is never needed (see AI05-0214).
8648 -- Note that this ensures consistency of calling sequences for
8649 -- dispatching operations when some types in a class have defaults
8650 -- on discriminants and others do not (and requiring the extra
8651 -- formal would introduce distributed overhead).
8653 -- If the type does not have a completion yet, treat as prior to
8654 -- Ada 2012 for consistency.
8656 if Has_Discriminants (Formal_Type)
8657 and then not Is_Constrained (Formal_Type)
8658 and then Is_Definite_Subtype (Formal_Type)
8659 and then (Ada_Version < Ada_2012
8660 or else No (Underlying_Type (Formal_Type))
8662 (Is_Limited_Type (Formal_Type)
8665 (Underlying_Type (Formal_Type)))))
8667 Set_Extra_Constrained
8668 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8672 -- Create extra formal for supporting accessibility checking. This
8673 -- is done for both anonymous access formals and formals of named
8674 -- access types that are marked as controlling formals. The latter
8675 -- case can occur when Expand_Dispatching_Call creates a subprogram
8676 -- type and substitutes the types of access-to-class-wide actuals
8677 -- for the anonymous access-to-specific-type of controlling formals.
8678 -- Base_Type is applied because in cases where there is a null
8679 -- exclusion the formal may have an access subtype.
8681 -- This is suppressed if we specifically suppress accessibility
8682 -- checks at the package level for either the subprogram, or the
8683 -- package in which it resides. However, we do not suppress it
8684 -- simply if the scope has accessibility checks suppressed, since
8685 -- this could cause trouble when clients are compiled with a
8686 -- different suppression setting. The explicit checks at the
8687 -- package level are safe from this point of view.
8689 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8690 or else (Is_Controlling_Formal (Formal)
8691 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8693 (Explicit_Suppress (E, Accessibility_Check)
8695 Explicit_Suppress (Scope (E), Accessibility_Check))
8698 or else Present (Extra_Accessibility (P_Formal)))
8700 Set_Extra_Accessibility
8701 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8704 -- This label is required when skipping extra formal generation for
8705 -- Unchecked_Union parameters.
8707 <<Skip_Extra_Formal_Generation>>
8709 if Present (P_Formal) then
8710 Next_Formal (P_Formal);
8713 Next_Formal (Formal);
8716 <<Test_For_Func_Result_Extras>>
8718 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8719 -- function call is ... determined by the point of call ...".
8721 if Needs_Result_Accessibility_Level (E) then
8722 Set_Extra_Accessibility_Of_Result
8723 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8726 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8727 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8729 if Is_Build_In_Place_Function (E) then
8731 Result_Subt : constant Entity_Id := Etype (E);
8732 Formal_Typ : Entity_Id;
8733 Subp_Decl : Node_Id;
8734 Discard : Entity_Id;
8737 -- In the case of functions with unconstrained result subtypes,
8738 -- add a 4-state formal indicating whether the return object is
8739 -- allocated by the caller (1), or should be allocated by the
8740 -- callee on the secondary stack (2), in the global heap (3), or
8741 -- in a user-defined storage pool (4). For the moment we just use
8742 -- Natural for the type of this formal. Note that this formal
8743 -- isn't usually needed in the case where the result subtype is
8744 -- constrained, but it is needed when the function has a tagged
8745 -- result, because generally such functions can be called in a
8746 -- dispatching context and such calls must be handled like calls
8747 -- to a class-wide function.
8749 if Needs_BIP_Alloc_Form (E) then
8752 (E, Standard_Natural,
8753 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8755 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8756 -- use a user-defined pool. This formal is not added on
8757 -- ZFP as those targets do not support pools.
8759 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8762 (E, RTE (RE_Root_Storage_Pool_Ptr),
8763 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8767 -- In the case of functions whose result type needs finalization,
8768 -- add an extra formal which represents the finalization master.
8770 if Needs_BIP_Finalization_Master (E) then
8773 (E, RTE (RE_Finalization_Master_Ptr),
8774 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8777 -- When the result type contains tasks, add two extra formals: the
8778 -- master of the tasks to be created, and the caller's activation
8781 if Needs_BIP_Task_Actuals (E) then
8784 (E, RTE (RE_Master_Id),
8785 E, BIP_Formal_Suffix (BIP_Task_Master));
8787 Set_Has_Master_Entity (E);
8791 (E, RTE (RE_Activation_Chain_Access),
8792 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8795 -- All build-in-place functions get an extra formal that will be
8796 -- passed the address of the return object within the caller.
8799 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8801 -- Incomplete_View_From_Limited_With is needed here because
8802 -- gigi gets confused if the designated type is the full view
8803 -- coming from a limited-with'ed package. In the normal case,
8804 -- (no limited with) Incomplete_View_From_Limited_With
8805 -- returns Result_Subt.
8807 Set_Directly_Designated_Type
8808 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8809 Set_Etype (Formal_Typ, Formal_Typ);
8810 Set_Depends_On_Private
8811 (Formal_Typ, Has_Private_Component (Formal_Typ));
8812 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8813 Set_Is_Access_Constant (Formal_Typ, False);
8815 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8816 -- the designated type comes from the limited view (for back-end
8819 Set_From_Limited_With
8820 (Formal_Typ, From_Limited_With (Result_Subt));
8822 Layout_Type (Formal_Typ);
8824 -- Force the definition of the Itype in case of internal function
8825 -- calls within the same or nested scope.
8827 if Is_Subprogram_Or_Generic_Subprogram (E) then
8828 Subp_Decl := Parent (E);
8830 -- The insertion point for an Itype reference should be after
8831 -- the unit declaration node of the subprogram. An exception
8832 -- to this are inherited operations from a parent type in which
8833 -- case the derived type acts as their parent.
8835 if Nkind (Subp_Decl) in N_Function_Specification
8836 | N_Procedure_Specification
8838 Subp_Decl := Parent (Subp_Decl);
8841 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8846 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8850 -- If this is an instance of a generic, we need to have extra formals
8853 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8854 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8856 end Create_Extra_Formals;
8858 -----------------------------
8859 -- Enter_Overloaded_Entity --
8860 -----------------------------
8862 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8863 function Matches_Predefined_Op return Boolean;
8864 -- This returns an approximation of whether S matches a predefined
8865 -- operator, based on the operator symbol, and the parameter and result
8866 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8868 ---------------------------
8869 -- Matches_Predefined_Op --
8870 ---------------------------
8872 function Matches_Predefined_Op return Boolean is
8873 Formal_1 : constant Entity_Id := First_Formal (S);
8874 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8875 Op : constant Name_Id := Chars (S);
8876 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8877 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8882 if Present (Formal_2) then
8884 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8887 -- All but "&" and "**" have same-types parameters
8896 if Type_1 /= Type_2 then
8901 -- Check parameter and result types
8909 Is_Boolean_Type (Result_Type)
8910 and then Result_Type = Type_1;
8916 Is_Integer_Type (Result_Type)
8917 and then Result_Type = Type_1;
8925 Is_Numeric_Type (Result_Type)
8926 and then Result_Type = Type_1;
8932 Is_Boolean_Type (Result_Type)
8933 and then not Is_Limited_Type (Type_1);
8941 Is_Boolean_Type (Result_Type)
8942 and then (Is_Array_Type (Type_1)
8943 or else Is_Scalar_Type (Type_1));
8945 when Name_Op_Concat =>
8946 return Is_Array_Type (Result_Type);
8948 when Name_Op_Expon =>
8950 (Is_Integer_Type (Result_Type)
8951 or else Is_Floating_Point_Type (Result_Type))
8952 and then Result_Type = Type_1
8953 and then Type_2 = Standard_Integer;
8956 raise Program_Error;
8969 Is_Numeric_Type (Result_Type)
8970 and then Result_Type = Type_1;
8974 Is_Boolean_Type (Result_Type)
8975 and then Result_Type = Type_1;
8978 raise Program_Error;
8981 end Matches_Predefined_Op;
8985 E : Entity_Id := Current_Entity_In_Scope (S);
8986 C_E : Entity_Id := Current_Entity (S);
8988 -- Start of processing for Enter_Overloaded_Entity
8992 Set_Has_Homonym (E);
8993 Set_Has_Homonym (S);
8996 Set_Is_Immediately_Visible (S);
8997 Set_Scope (S, Current_Scope);
8999 -- Chain new entity if front of homonym in current scope, so that
9000 -- homonyms are contiguous.
9002 if Present (E) and then E /= C_E then
9003 while Homonym (C_E) /= E loop
9004 C_E := Homonym (C_E);
9007 Set_Homonym (C_E, S);
9011 Set_Current_Entity (S);
9016 if Is_Inherited_Operation (S) then
9017 Append_Inherited_Subprogram (S);
9019 Append_Entity (S, Current_Scope);
9022 Set_Public_Status (S);
9024 if Debug_Flag_E then
9025 Write_Str ("New overloaded entity chain: ");
9026 Write_Name (Chars (S));
9029 while Present (E) loop
9030 Write_Str (" "); Write_Int (Int (E));
9037 -- Generate warning for hiding
9040 and then Comes_From_Source (S)
9041 and then In_Extended_Main_Source_Unit (S)
9048 -- Warn unless genuine overloading. Do not emit warning on
9049 -- hiding predefined operators in Standard (these are either an
9050 -- artifact of our implicit declarations, or simple noise) but
9051 -- keep warning on a operator defined on a local subtype, because
9052 -- of the real danger that different operators may be applied in
9053 -- various parts of the program.
9055 -- Note that if E and S have the same scope, there is never any
9056 -- hiding. Either the two conflict, and the program is illegal,
9057 -- or S is overriding an implicit inherited subprogram.
9059 if Scope (E) /= Scope (S)
9060 and then (not Is_Overloadable (E)
9061 or else Subtype_Conformant (E, S))
9062 and then (Is_Immediately_Visible (E)
9063 or else Is_Potentially_Use_Visible (S))
9065 if Scope (E) = Standard_Standard then
9066 if Nkind (S) = N_Defining_Operator_Symbol
9067 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9069 and then Matches_Predefined_Op
9072 ("declaration of & hides predefined operator?h?", S);
9075 -- E not immediately within Standard
9078 Error_Msg_Sloc := Sloc (E);
9079 Error_Msg_N ("declaration of & hides one #?h?", S);
9084 end Enter_Overloaded_Entity;
9086 -----------------------------
9087 -- Check_Untagged_Equality --
9088 -----------------------------
9090 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
9091 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
9092 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
9096 -- This check applies only if we have a subprogram declaration with an
9097 -- untagged record type that is conformant to the predefined op.
9099 if Nkind (Decl) /= N_Subprogram_Declaration
9100 or else not Is_Record_Type (Typ)
9101 or else Is_Tagged_Type (Typ)
9102 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
9107 -- In Ada 2012 case, we will output errors or warnings depending on
9108 -- the setting of debug flag -gnatd.E.
9110 if Ada_Version >= Ada_2012 then
9111 Error_Msg_Warn := Debug_Flag_Dot_EE;
9113 -- In earlier versions of Ada, nothing to do unless we are warning on
9114 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
9117 if not Warn_On_Ada_2012_Compatibility then
9122 -- Cases where the type has already been frozen
9124 if Is_Frozen (Typ) then
9126 -- The check applies to a primitive operation, so check that type
9127 -- and equality operation are in the same scope.
9129 if Scope (Typ) /= Current_Scope then
9132 -- If the type is a generic actual (sub)type, the operation is not
9133 -- primitive either because the base type is declared elsewhere.
9135 elsif Is_Generic_Actual_Type (Typ) then
9138 -- Here we have a definite error of declaration after freezing
9141 if Ada_Version >= Ada_2012 then
9143 ("equality operator must be declared before type & is "
9144 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
9146 -- In Ada 2012 mode with error turned to warning, output one
9147 -- more warning to warn that the equality operation may not
9148 -- compose. This is the consequence of ignoring the error.
9150 if Error_Msg_Warn then
9151 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
9156 ("equality operator must be declared before type& is "
9157 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
9160 -- If we are in the package body, we could just move the
9161 -- declaration to the package spec, so add a message saying that.
9163 if In_Package_Body (Scope (Typ)) then
9164 if Ada_Version >= Ada_2012 then
9166 ("\move declaration to package spec<<", Eq_Op);
9169 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
9172 -- Otherwise try to find the freezing point for better message.
9175 Obj_Decl := Next (Parent (Typ));
9176 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9177 if Nkind (Obj_Decl) = N_Object_Declaration
9178 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9180 -- Freezing point, output warnings
9182 if Ada_Version >= Ada_2012 then
9184 ("type& is frozen by declaration??", Obj_Decl, Typ);
9186 ("\an equality operator cannot be declared after "
9191 ("type& is frozen by declaration (Ada 2012)?y?",
9194 ("\an equality operator cannot be declared after "
9195 & "this point (Ada 2012)?y?",
9201 -- If we reach generated code for subprogram declaration
9202 -- or body, it is the body that froze the type and the
9203 -- declaration is legal.
9205 elsif Sloc (Obj_Decl) = Sloc (Decl) then
9214 -- Here if type is not frozen yet. It is illegal to have a primitive
9215 -- equality declared in the private part if the type is visible.
9217 elsif not In_Same_List (Parent (Typ), Decl)
9218 and then not Is_Limited_Type (Typ)
9220 -- Shouldn't we give an RM reference here???
9222 if Ada_Version >= Ada_2012 then
9224 ("equality operator appears too late<<", Eq_Op);
9227 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9230 -- Finally check for AI12-0352: declaration of a user-defined primitive
9231 -- equality operation for a record type T is illegal if it occurs after
9232 -- a type has been derived from T.
9235 Obj_Decl := Next (Parent (Typ));
9237 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9238 if Nkind (Obj_Decl) = N_Full_Type_Declaration
9239 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9242 ("equality operator cannot appear after derivation", Eq_Op);
9244 ("an equality operator for& cannot be declared after "
9252 end Check_Untagged_Equality;
9254 -----------------------------
9255 -- Find_Corresponding_Spec --
9256 -----------------------------
9258 function Find_Corresponding_Spec
9260 Post_Error : Boolean := True) return Entity_Id
9262 Spec : constant Node_Id := Specification (N);
9263 Designator : constant Entity_Id := Defining_Entity (Spec);
9267 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9268 -- Even if fully conformant, a body may depend on a generic actual when
9269 -- the spec does not, or vice versa, in which case they were distinct
9270 -- entities in the generic.
9272 -------------------------------
9273 -- Different_Generic_Profile --
9274 -------------------------------
9276 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9279 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9280 -- Check that the types of corresponding formals have the same
9281 -- generic actual if any. We have to account for subtypes of a
9282 -- generic formal, declared between a spec and a body, which may
9283 -- appear distinct in an instance but matched in the generic, and
9284 -- the subtype may be used either in the spec or the body of the
9285 -- subprogram being checked.
9287 -------------------------
9288 -- Same_Generic_Actual --
9289 -------------------------
9291 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9293 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9294 -- Predicate to check whether S1 is a subtype of S2 in the source
9297 -------------------------
9298 -- Is_Declared_Subtype --
9299 -------------------------
9301 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9303 return Comes_From_Source (Parent (S1))
9304 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9305 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9306 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9307 end Is_Declared_Subtype;
9309 -- Start of processing for Same_Generic_Actual
9312 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9313 or else Is_Declared_Subtype (T1, T2)
9314 or else Is_Declared_Subtype (T2, T1);
9315 end Same_Generic_Actual;
9317 -- Start of processing for Different_Generic_Profile
9320 if not In_Instance then
9323 elsif Ekind (E) = E_Function
9324 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9329 F1 := First_Formal (Designator);
9330 F2 := First_Formal (E);
9331 while Present (F1) loop
9332 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9341 end Different_Generic_Profile;
9343 -- Start of processing for Find_Corresponding_Spec
9346 E := Current_Entity (Designator);
9347 while Present (E) loop
9349 -- We are looking for a matching spec. It must have the same scope,
9350 -- and the same name, and either be type conformant, or be the case
9351 -- of a library procedure spec and its body (which belong to one
9352 -- another regardless of whether they are type conformant or not).
9354 if Scope (E) = Current_Scope then
9355 if Current_Scope = Standard_Standard
9356 or else (Ekind (E) = Ekind (Designator)
9357 and then Type_Conformant (E, Designator))
9359 -- Within an instantiation, we know that spec and body are
9360 -- subtype conformant, because they were subtype conformant in
9361 -- the generic. We choose the subtype-conformant entity here as
9362 -- well, to resolve spurious ambiguities in the instance that
9363 -- were not present in the generic (i.e. when two different
9364 -- types are given the same actual). If we are looking for a
9365 -- spec to match a body, full conformance is expected.
9369 -- Inherit the convention and "ghostness" of the matching
9370 -- spec to ensure proper full and subtype conformance.
9372 Set_Convention (Designator, Convention (E));
9374 -- Skip past subprogram bodies and subprogram renamings that
9375 -- may appear to have a matching spec, but that aren't fully
9376 -- conformant with it. That can occur in cases where an
9377 -- actual type causes unrelated homographs in the instance.
9379 if Nkind (N) in N_Subprogram_Body
9380 | N_Subprogram_Renaming_Declaration
9381 and then Present (Homonym (E))
9382 and then not Fully_Conformant (Designator, E)
9386 elsif not Subtype_Conformant (Designator, E) then
9389 elsif Different_Generic_Profile (E) then
9394 -- Ada 2012 (AI05-0165): For internally generated bodies of
9395 -- null procedures locate the internally generated spec. We
9396 -- enforce mode conformance since a tagged type may inherit
9397 -- from interfaces several null primitives which differ only
9398 -- in the mode of the formals.
9400 if not (Comes_From_Source (E))
9401 and then Is_Null_Procedure (E)
9402 and then not Mode_Conformant (Designator, E)
9406 -- For null procedures coming from source that are completions,
9407 -- analysis of the generated body will establish the link.
9409 elsif Comes_From_Source (E)
9410 and then Nkind (Spec) = N_Procedure_Specification
9411 and then Null_Present (Spec)
9415 -- Expression functions can be completions, but cannot be
9416 -- completed by an explicit body.
9418 elsif Comes_From_Source (E)
9419 and then Comes_From_Source (N)
9420 and then Nkind (N) = N_Subprogram_Body
9421 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9422 N_Expression_Function
9424 Error_Msg_Sloc := Sloc (E);
9425 Error_Msg_N ("body conflicts with expression function#", N);
9428 elsif not Has_Completion (E) then
9429 if Nkind (N) /= N_Subprogram_Body_Stub then
9430 Set_Corresponding_Spec (N, E);
9433 Set_Has_Completion (E);
9436 elsif Nkind (Parent (N)) = N_Subunit then
9438 -- If this is the proper body of a subunit, the completion
9439 -- flag is set when analyzing the stub.
9443 -- If E is an internal function with a controlling result that
9444 -- was created for an operation inherited by a null extension,
9445 -- it may be overridden by a body without a previous spec (one
9446 -- more reason why these should be shunned). In that case we
9447 -- remove the generated body if present, because the current
9448 -- one is the explicit overriding.
9450 elsif Ekind (E) = E_Function
9451 and then Ada_Version >= Ada_2005
9452 and then not Comes_From_Source (E)
9453 and then Has_Controlling_Result (E)
9454 and then Is_Null_Extension (Etype (E))
9455 and then Comes_From_Source (Spec)
9457 Set_Has_Completion (E, False);
9460 and then Nkind (Parent (E)) = N_Function_Specification
9463 (Unit_Declaration_Node
9464 (Corresponding_Body (Unit_Declaration_Node (E))));
9468 -- If expansion is disabled, or if the wrapper function has
9469 -- not been generated yet, this a late body overriding an
9470 -- inherited operation, or it is an overriding by some other
9471 -- declaration before the controlling result is frozen. In
9472 -- either case this is a declaration of a new entity.
9478 -- If the body already exists, then this is an error unless
9479 -- the previous declaration is the implicit declaration of a
9480 -- derived subprogram. It is also legal for an instance to
9481 -- contain type conformant overloadable declarations (but the
9482 -- generic declaration may not), per 8.3(26/2).
9484 elsif No (Alias (E))
9485 and then not Is_Intrinsic_Subprogram (E)
9486 and then not In_Instance
9489 Error_Msg_Sloc := Sloc (E);
9491 if Is_Imported (E) then
9493 ("body not allowed for imported subprogram & declared#",
9496 Error_Msg_NE ("duplicate body for & declared#", N, E);
9500 -- Child units cannot be overloaded, so a conformance mismatch
9501 -- between body and a previous spec is an error.
9503 elsif Is_Child_Unit (E)
9505 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9507 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9512 ("body of child unit does not match previous declaration", N);
9520 -- On exit, we know that no previous declaration of subprogram exists
9523 end Find_Corresponding_Spec;
9525 ----------------------
9526 -- Fully_Conformant --
9527 ----------------------
9529 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9532 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9534 end Fully_Conformant;
9536 ----------------------------------
9537 -- Fully_Conformant_Expressions --
9538 ----------------------------------
9540 function Fully_Conformant_Expressions
9541 (Given_E1 : Node_Id;
9543 Report : Boolean := False) return Boolean
9545 E1 : constant Node_Id := Original_Node (Given_E1);
9546 E2 : constant Node_Id := Original_Node (Given_E2);
9547 -- We always test conformance on original nodes, since it is possible
9548 -- for analysis and/or expansion to make things look as though they
9549 -- conform when they do not, e.g. by converting 1+2 into 3.
9551 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9554 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9555 -- Compare elements of two lists for conformance. Elements have to be
9556 -- conformant, and actuals inserted as default parameters do not match
9557 -- explicit actuals with the same value.
9559 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9560 -- Compare an operator node with a function call
9566 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9568 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9575 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9580 if L1 = No_List then
9586 if L2 = No_List then
9592 -- Compare two lists, skipping rewrite insertions (we want to compare
9593 -- the original trees, not the expanded versions).
9596 if Is_Rewrite_Insertion (N1) then
9598 elsif Is_Rewrite_Insertion (N2) then
9604 elsif not FCE (N1, N2) then
9617 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9618 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9623 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9628 Act := First (Actuals);
9630 if Nkind (Op_Node) in N_Binary_Op then
9631 if not FCE (Left_Opnd (Op_Node), Act) then
9638 return Present (Act)
9639 and then FCE (Right_Opnd (Op_Node), Act)
9640 and then No (Next (Act));
9644 function User_Defined_Numeric_Literal_Mismatch return Boolean;
9645 -- Usually literals with the same value like 12345 and 12_345
9646 -- or 123.0 and 123.00 conform, but not if they are
9647 -- user-defined literals.
9649 -------------------------------------------
9650 -- User_Defined_Numeric_Literal_Mismatch --
9651 -------------------------------------------
9653 function User_Defined_Numeric_Literal_Mismatch return Boolean is
9654 E1_Is_User_Defined : constant Boolean :=
9655 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
9656 E2_Is_User_Defined : constant Boolean :=
9657 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
9660 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
9662 return E1_Is_User_Defined and then
9663 not String_Equal (String_From_Numeric_Literal (E1),
9664 String_From_Numeric_Literal (E2));
9665 end User_Defined_Numeric_Literal_Mismatch;
9671 -- Start of processing for Fully_Conformant_Expressions
9676 -- Nonconformant if paren count does not match. Note: if some idiot
9677 -- complains that we don't do this right for more than 3 levels of
9678 -- parentheses, they will be treated with the respect they deserve.
9680 if Paren_Count (E1) /= Paren_Count (E2) then
9683 -- If same entities are referenced, then they are conformant even if
9684 -- they have different forms (RM 8.3.1(19-20)).
9686 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9687 if Present (Entity (E1)) then
9688 Result := Entity (E1) = Entity (E2)
9690 -- One may be a discriminant that has been replaced by the
9691 -- corresponding discriminal.
9694 (Chars (Entity (E1)) = Chars (Entity (E2))
9695 and then Ekind (Entity (E1)) = E_Discriminant
9696 and then Ekind (Entity (E2)) = E_In_Parameter)
9698 -- The discriminant of a protected type is transformed into
9699 -- a local constant and then into a parameter of a protected
9703 (Ekind (Entity (E1)) = E_Constant
9704 and then Ekind (Entity (E2)) = E_In_Parameter
9705 and then Present (Discriminal_Link (Entity (E1)))
9706 and then Discriminal_Link (Entity (E1)) =
9707 Discriminal_Link (Entity (E2)))
9709 -- AI12-050: The loop variables of quantified expressions match
9710 -- if they have the same identifier, even though they may have
9711 -- different entities.
9714 (Chars (Entity (E1)) = Chars (Entity (E2))
9715 and then Ekind (Entity (E1)) = E_Loop_Parameter
9716 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9718 -- A call to an instantiation of Unchecked_Conversion is
9719 -- rewritten with the name of the generated function created for
9720 -- the instance, and this must be special-cased.
9723 (Ekind (Entity (E1)) = E_Function
9724 and then Is_Intrinsic_Subprogram (Entity (E1))
9725 and then Is_Generic_Instance (Entity (E1))
9726 and then Entity (E2) = Alias (Entity (E1)));
9727 if Report and not Result then
9729 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9731 ("Meaning of& differs because of declaration#", E1, E2);
9736 elsif Nkind (E1) = N_Expanded_Name
9737 and then Nkind (E2) = N_Expanded_Name
9738 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9739 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9741 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9744 -- Identifiers in component associations don't always have
9745 -- entities, but their names must conform.
9747 return Nkind (E1) = N_Identifier
9748 and then Nkind (E2) = N_Identifier
9749 and then Chars (E1) = Chars (E2);
9752 elsif Nkind (E1) = N_Character_Literal
9753 and then Nkind (E2) = N_Expanded_Name
9755 return Nkind (Selector_Name (E2)) = N_Character_Literal
9756 and then Chars (E1) = Chars (Selector_Name (E2));
9758 elsif Nkind (E2) = N_Character_Literal
9759 and then Nkind (E1) = N_Expanded_Name
9761 return Nkind (Selector_Name (E1)) = N_Character_Literal
9762 and then Chars (E2) = Chars (Selector_Name (E1));
9764 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9765 return FCO (E1, E2);
9767 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9768 return FCO (E2, E1);
9770 -- Otherwise we must have the same syntactic entity
9772 elsif Nkind (E1) /= Nkind (E2) then
9775 -- At this point, we specialize by node type
9781 FCL (Expressions (E1), Expressions (E2))
9783 FCL (Component_Associations (E1),
9784 Component_Associations (E2));
9787 if Nkind (Expression (E1)) = N_Qualified_Expression
9789 Nkind (Expression (E2)) = N_Qualified_Expression
9791 return FCE (Expression (E1), Expression (E2));
9793 -- Check that the subtype marks and any constraints
9798 Indic1 : constant Node_Id := Expression (E1);
9799 Indic2 : constant Node_Id := Expression (E2);
9804 if Nkind (Indic1) /= N_Subtype_Indication then
9806 Nkind (Indic2) /= N_Subtype_Indication
9807 and then Entity (Indic1) = Entity (Indic2);
9809 elsif Nkind (Indic2) /= N_Subtype_Indication then
9811 Nkind (Indic1) /= N_Subtype_Indication
9812 and then Entity (Indic1) = Entity (Indic2);
9815 if Entity (Subtype_Mark (Indic1)) /=
9816 Entity (Subtype_Mark (Indic2))
9821 Elt1 := First (Constraints (Constraint (Indic1)));
9822 Elt2 := First (Constraints (Constraint (Indic2)));
9823 while Present (Elt1) and then Present (Elt2) loop
9824 if not FCE (Elt1, Elt2) then
9837 when N_Attribute_Reference =>
9839 Attribute_Name (E1) = Attribute_Name (E2)
9840 and then FCL (Expressions (E1), Expressions (E2));
9844 Entity (E1) = Entity (E2)
9845 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9846 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9848 when N_Membership_Test
9852 FCE (Left_Opnd (E1), Left_Opnd (E2))
9854 FCE (Right_Opnd (E1), Right_Opnd (E2));
9856 when N_Case_Expression =>
9862 if not FCE (Expression (E1), Expression (E2)) then
9866 Alt1 := First (Alternatives (E1));
9867 Alt2 := First (Alternatives (E2));
9869 if Present (Alt1) /= Present (Alt2) then
9871 elsif No (Alt1) then
9875 if not FCE (Expression (Alt1), Expression (Alt2))
9876 or else not FCL (Discrete_Choices (Alt1),
9877 Discrete_Choices (Alt2))
9888 when N_Character_Literal =>
9890 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9892 when N_Component_Association =>
9894 FCL (Choices (E1), Choices (E2))
9896 FCE (Expression (E1), Expression (E2));
9898 when N_Explicit_Dereference =>
9900 FCE (Prefix (E1), Prefix (E2));
9902 when N_Extension_Aggregate =>
9904 FCL (Expressions (E1), Expressions (E2))
9905 and then Null_Record_Present (E1) =
9906 Null_Record_Present (E2)
9907 and then FCL (Component_Associations (E1),
9908 Component_Associations (E2));
9910 when N_Function_Call =>
9912 FCE (Name (E1), Name (E2))
9914 FCL (Parameter_Associations (E1),
9915 Parameter_Associations (E2));
9917 when N_If_Expression =>
9919 FCL (Expressions (E1), Expressions (E2));
9921 when N_Indexed_Component =>
9923 FCE (Prefix (E1), Prefix (E2))
9925 FCL (Expressions (E1), Expressions (E2));
9927 when N_Integer_Literal =>
9928 return (Intval (E1) = Intval (E2))
9929 and then not User_Defined_Numeric_Literal_Mismatch;
9934 when N_Operator_Symbol =>
9936 Chars (E1) = Chars (E2);
9938 when N_Others_Choice =>
9941 when N_Parameter_Association =>
9943 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9944 and then FCE (Explicit_Actual_Parameter (E1),
9945 Explicit_Actual_Parameter (E2));
9947 when N_Qualified_Expression
9949 | N_Unchecked_Type_Conversion
9952 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9954 FCE (Expression (E1), Expression (E2));
9956 when N_Quantified_Expression =>
9957 if not FCE (Condition (E1), Condition (E2)) then
9961 if Present (Loop_Parameter_Specification (E1))
9962 and then Present (Loop_Parameter_Specification (E2))
9965 L1 : constant Node_Id :=
9966 Loop_Parameter_Specification (E1);
9967 L2 : constant Node_Id :=
9968 Loop_Parameter_Specification (E2);
9972 Reverse_Present (L1) = Reverse_Present (L2)
9974 FCE (Defining_Identifier (L1),
9975 Defining_Identifier (L2))
9977 FCE (Discrete_Subtype_Definition (L1),
9978 Discrete_Subtype_Definition (L2));
9981 elsif Present (Iterator_Specification (E1))
9982 and then Present (Iterator_Specification (E2))
9985 I1 : constant Node_Id := Iterator_Specification (E1);
9986 I2 : constant Node_Id := Iterator_Specification (E2);
9990 FCE (Defining_Identifier (I1),
9991 Defining_Identifier (I2))
9993 Of_Present (I1) = Of_Present (I2)
9995 Reverse_Present (I1) = Reverse_Present (I2)
9996 and then FCE (Name (I1), Name (I2))
9997 and then FCE (Subtype_Indication (I1),
9998 Subtype_Indication (I2));
10001 -- The quantified expressions used different specifications to
10002 -- walk their respective ranges.
10010 FCE (Low_Bound (E1), Low_Bound (E2))
10012 FCE (High_Bound (E1), High_Bound (E2));
10014 when N_Real_Literal =>
10015 return (Realval (E1) = Realval (E2))
10016 and then not User_Defined_Numeric_Literal_Mismatch;
10018 when N_Selected_Component =>
10020 FCE (Prefix (E1), Prefix (E2))
10022 FCE (Selector_Name (E1), Selector_Name (E2));
10026 FCE (Prefix (E1), Prefix (E2))
10028 FCE (Discrete_Range (E1), Discrete_Range (E2));
10030 when N_String_Literal =>
10032 S1 : constant String_Id := Strval (E1);
10033 S2 : constant String_Id := Strval (E2);
10034 L1 : constant Nat := String_Length (S1);
10035 L2 : constant Nat := String_Length (S2);
10042 for J in 1 .. L1 loop
10043 if Get_String_Char (S1, J) /=
10044 Get_String_Char (S2, J)
10056 Entity (E1) = Entity (E2)
10058 FCE (Right_Opnd (E1), Right_Opnd (E2));
10060 -- All other node types cannot appear in this context. Strictly
10061 -- we should raise a fatal internal error. Instead we just ignore
10062 -- the nodes. This means that if anyone makes a mistake in the
10063 -- expander and mucks an expression tree irretrievably, the result
10064 -- will be a failure to detect a (probably very obscure) case
10065 -- of non-conformance, which is better than bombing on some
10066 -- case where two expressions do in fact conform.
10072 end Fully_Conformant_Expressions;
10074 ----------------------------------------
10075 -- Fully_Conformant_Discrete_Subtypes --
10076 ----------------------------------------
10078 function Fully_Conformant_Discrete_Subtypes
10079 (Given_S1 : Node_Id;
10080 Given_S2 : Node_Id) return Boolean
10082 S1 : constant Node_Id := Original_Node (Given_S1);
10083 S2 : constant Node_Id := Original_Node (Given_S2);
10085 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10086 -- Special-case for a bound given by a discriminant, which in the body
10087 -- is replaced with the discriminal of the enclosing type.
10089 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10090 -- Check both bounds
10092 -----------------------
10093 -- Conforming_Bounds --
10094 -----------------------
10096 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10098 if Is_Entity_Name (B1)
10099 and then Is_Entity_Name (B2)
10100 and then Ekind (Entity (B1)) = E_Discriminant
10102 return Chars (B1) = Chars (B2);
10105 return Fully_Conformant_Expressions (B1, B2);
10107 end Conforming_Bounds;
10109 -----------------------
10110 -- Conforming_Ranges --
10111 -----------------------
10113 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10116 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10118 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10119 end Conforming_Ranges;
10121 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10124 if Nkind (S1) /= Nkind (S2) then
10127 elsif Is_Entity_Name (S1) then
10128 return Entity (S1) = Entity (S2);
10130 elsif Nkind (S1) = N_Range then
10131 return Conforming_Ranges (S1, S2);
10133 elsif Nkind (S1) = N_Subtype_Indication then
10135 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10138 (Range_Expression (Constraint (S1)),
10139 Range_Expression (Constraint (S2)));
10143 end Fully_Conformant_Discrete_Subtypes;
10145 --------------------
10146 -- Install_Entity --
10147 --------------------
10149 procedure Install_Entity (E : Entity_Id) is
10150 Prev : constant Entity_Id := Current_Entity (E);
10152 Set_Is_Immediately_Visible (E);
10153 Set_Current_Entity (E);
10154 Set_Homonym (E, Prev);
10155 end Install_Entity;
10157 ---------------------
10158 -- Install_Formals --
10159 ---------------------
10161 procedure Install_Formals (Id : Entity_Id) is
10164 F := First_Formal (Id);
10165 while Present (F) loop
10166 Install_Entity (F);
10169 end Install_Formals;
10171 -----------------------------
10172 -- Is_Interface_Conformant --
10173 -----------------------------
10175 function Is_Interface_Conformant
10176 (Tagged_Type : Entity_Id;
10177 Iface_Prim : Entity_Id;
10178 Prim : Entity_Id) return Boolean
10180 -- The operation may in fact be an inherited (implicit) operation
10181 -- rather than the original interface primitive, so retrieve the
10182 -- ultimate ancestor.
10184 Iface : constant Entity_Id :=
10185 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10186 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10188 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10189 -- Return the controlling formal of Prim
10191 ------------------------
10192 -- Controlling_Formal --
10193 ------------------------
10195 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10199 E := First_Entity (Prim);
10200 while Present (E) loop
10201 if Is_Formal (E) and then Is_Controlling_Formal (E) then
10209 end Controlling_Formal;
10213 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10214 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10216 -- Start of processing for Is_Interface_Conformant
10219 pragma Assert (Is_Subprogram (Iface_Prim)
10220 and then Is_Subprogram (Prim)
10221 and then Is_Dispatching_Operation (Iface_Prim)
10222 and then Is_Dispatching_Operation (Prim));
10224 pragma Assert (Is_Interface (Iface)
10225 or else (Present (Alias (Iface_Prim))
10228 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10230 if Prim = Iface_Prim
10231 or else not Is_Subprogram (Prim)
10232 or else Ekind (Prim) /= Ekind (Iface_Prim)
10233 or else not Is_Dispatching_Operation (Prim)
10234 or else Scope (Prim) /= Scope (Tagged_Type)
10236 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10237 or else not Primitive_Names_Match (Iface_Prim, Prim)
10241 -- The mode of the controlling formals must match
10243 elsif Present (Iface_Ctrl_F)
10244 and then Present (Prim_Ctrl_F)
10245 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10249 -- Case of a procedure, or a function whose result type matches the
10250 -- result type of the interface primitive, or a function that has no
10251 -- controlling result (I or access I).
10253 elsif Ekind (Iface_Prim) = E_Procedure
10254 or else Etype (Prim) = Etype (Iface_Prim)
10255 or else not Has_Controlling_Result (Prim)
10257 return Type_Conformant
10258 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10260 -- Case of a function returning an interface, or an access to one. Check
10261 -- that the return types correspond.
10263 elsif Implements_Interface (Typ, Iface) then
10264 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10266 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10271 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10272 Skip_Controlling_Formals => True);
10278 end Is_Interface_Conformant;
10280 ---------------------------------
10281 -- Is_Non_Overriding_Operation --
10282 ---------------------------------
10284 function Is_Non_Overriding_Operation
10285 (Prev_E : Entity_Id;
10286 New_E : Entity_Id) return Boolean
10288 Formal : Entity_Id;
10290 G_Typ : Entity_Id := Empty;
10292 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10293 -- If F_Type is a derived type associated with a generic actual subtype,
10294 -- then return its Generic_Parent_Type attribute, else return Empty.
10296 function Types_Correspond
10297 (P_Type : Entity_Id;
10298 N_Type : Entity_Id) return Boolean;
10299 -- Returns true if and only if the types (or designated types in the
10300 -- case of anonymous access types) are the same or N_Type is derived
10301 -- directly or indirectly from P_Type.
10303 -----------------------------
10304 -- Get_Generic_Parent_Type --
10305 -----------------------------
10307 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10313 if Is_Derived_Type (F_Typ)
10314 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10316 -- The tree must be traversed to determine the parent subtype in
10317 -- the generic unit, which unfortunately isn't always available
10318 -- via semantic attributes. ??? (Note: The use of Original_Node
10319 -- is needed for cases where a full derived type has been
10322 -- If the parent type is a scalar type, the derivation creates
10323 -- an anonymous base type for it, and the source type is its
10326 if Is_Scalar_Type (F_Typ)
10327 and then not Comes_From_Source (F_Typ)
10331 (Original_Node (Parent (First_Subtype (F_Typ))));
10333 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10335 if Nkind (Defn) = N_Derived_Type_Definition then
10336 Indic := Subtype_Indication (Defn);
10338 if Nkind (Indic) = N_Subtype_Indication then
10339 G_Typ := Entity (Subtype_Mark (Indic));
10341 G_Typ := Entity (Indic);
10344 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10345 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10347 return Generic_Parent_Type (Parent (G_Typ));
10353 end Get_Generic_Parent_Type;
10355 ----------------------
10356 -- Types_Correspond --
10357 ----------------------
10359 function Types_Correspond
10360 (P_Type : Entity_Id;
10361 N_Type : Entity_Id) return Boolean
10363 Prev_Type : Entity_Id := Base_Type (P_Type);
10364 New_Type : Entity_Id := Base_Type (N_Type);
10367 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10368 Prev_Type := Designated_Type (Prev_Type);
10371 if Ekind (New_Type) = E_Anonymous_Access_Type then
10372 New_Type := Designated_Type (New_Type);
10375 if Prev_Type = New_Type then
10378 elsif not Is_Class_Wide_Type (New_Type) then
10379 while Etype (New_Type) /= New_Type loop
10380 New_Type := Etype (New_Type);
10382 if New_Type = Prev_Type then
10388 end Types_Correspond;
10390 -- Start of processing for Is_Non_Overriding_Operation
10393 -- In the case where both operations are implicit derived subprograms
10394 -- then neither overrides the other. This can only occur in certain
10395 -- obscure cases (e.g., derivation from homographs created in a generic
10398 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10401 elsif Ekind (Current_Scope) = E_Package
10402 and then Is_Generic_Instance (Current_Scope)
10403 and then In_Private_Part (Current_Scope)
10404 and then Comes_From_Source (New_E)
10406 -- We examine the formals and result type of the inherited operation,
10407 -- to determine whether their type is derived from (the instance of)
10408 -- a generic type. The first such formal or result type is the one
10411 Formal := First_Formal (Prev_E);
10413 while Present (Formal) loop
10414 F_Typ := Base_Type (Etype (Formal));
10416 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10417 F_Typ := Designated_Type (F_Typ);
10420 G_Typ := Get_Generic_Parent_Type (F_Typ);
10421 exit when Present (G_Typ);
10423 Next_Formal (Formal);
10426 -- If the function dispatches on result check the result type
10428 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10429 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10436 -- If the generic type is a private type, then the original operation
10437 -- was not overriding in the generic, because there was no primitive
10438 -- operation to override.
10440 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10441 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10442 N_Formal_Private_Type_Definition
10446 -- The generic parent type is the ancestor of a formal derived
10447 -- type declaration. We need to check whether it has a primitive
10448 -- operation that should be overridden by New_E in the generic.
10452 P_Formal : Entity_Id;
10453 N_Formal : Entity_Id;
10456 P_Prim : Entity_Id;
10457 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10460 while Present (Prim_Elt) loop
10461 P_Prim := Node (Prim_Elt);
10463 if Chars (P_Prim) = Chars (New_E)
10464 and then Ekind (P_Prim) = Ekind (New_E)
10466 P_Formal := First_Formal (P_Prim);
10467 N_Formal := First_Formal (New_E);
10468 while Present (P_Formal) and then Present (N_Formal) loop
10469 P_Typ := Etype (P_Formal);
10470 N_Typ := Etype (N_Formal);
10472 if not Types_Correspond (P_Typ, N_Typ) then
10476 Next_Entity (P_Formal);
10477 Next_Entity (N_Formal);
10480 -- Found a matching primitive operation belonging to the
10481 -- formal ancestor type, so the new subprogram is
10485 and then No (N_Formal)
10486 and then (Ekind (New_E) /= E_Function
10489 (Etype (P_Prim), Etype (New_E)))
10495 Next_Elmt (Prim_Elt);
10498 -- If no match found, then the new subprogram does not override
10499 -- in the generic (nor in the instance).
10501 -- If the type in question is not abstract, and the subprogram
10502 -- is, this will be an error if the new operation is in the
10503 -- private part of the instance. Emit a warning now, which will
10504 -- make the subsequent error message easier to understand.
10506 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10507 and then Is_Abstract_Subprogram (Prev_E)
10508 and then In_Private_Part (Current_Scope)
10510 Error_Msg_Node_2 := F_Typ;
10512 ("private operation& in generic unit does not override "
10513 & "any primitive operation of& (RM 12.3 (18))??",
10523 end Is_Non_Overriding_Operation;
10525 -------------------------------------
10526 -- List_Inherited_Pre_Post_Aspects --
10527 -------------------------------------
10529 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10531 if Opt.List_Inherited_Aspects
10532 and then Is_Subprogram_Or_Generic_Subprogram (E)
10535 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10540 for Index in Subps'Range loop
10541 Items := Contract (Subps (Index));
10543 if Present (Items) then
10544 Prag := Pre_Post_Conditions (Items);
10545 while Present (Prag) loop
10546 Error_Msg_Sloc := Sloc (Prag);
10548 if Class_Present (Prag)
10549 and then not Split_PPC (Prag)
10551 if Pragma_Name (Prag) = Name_Precondition then
10553 ("info: & inherits `Pre''Class` aspect from "
10557 ("info: & inherits `Post''Class` aspect from "
10562 Prag := Next_Pragma (Prag);
10568 end List_Inherited_Pre_Post_Aspects;
10570 ------------------------------
10571 -- Make_Inequality_Operator --
10572 ------------------------------
10574 -- S is the defining identifier of an equality operator. We build a
10575 -- subprogram declaration with the right signature. This operation is
10576 -- intrinsic, because it is always expanded as the negation of the
10577 -- call to the equality function.
10579 procedure Make_Inequality_Operator (S : Entity_Id) is
10580 Loc : constant Source_Ptr := Sloc (S);
10583 Op_Name : Entity_Id;
10585 FF : constant Entity_Id := First_Formal (S);
10586 NF : constant Entity_Id := Next_Formal (FF);
10589 -- Check that equality was properly defined, ignore call if not
10596 A : constant Entity_Id :=
10597 Make_Defining_Identifier (Sloc (FF),
10598 Chars => Chars (FF));
10600 B : constant Entity_Id :=
10601 Make_Defining_Identifier (Sloc (NF),
10602 Chars => Chars (NF));
10605 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10607 Formals := New_List (
10608 Make_Parameter_Specification (Loc,
10609 Defining_Identifier => A,
10611 New_Occurrence_Of (Etype (First_Formal (S)),
10612 Sloc (Etype (First_Formal (S))))),
10614 Make_Parameter_Specification (Loc,
10615 Defining_Identifier => B,
10617 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10618 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10621 Make_Subprogram_Declaration (Loc,
10623 Make_Function_Specification (Loc,
10624 Defining_Unit_Name => Op_Name,
10625 Parameter_Specifications => Formals,
10626 Result_Definition =>
10627 New_Occurrence_Of (Standard_Boolean, Loc)));
10629 -- Insert inequality right after equality if it is explicit or after
10630 -- the derived type when implicit. These entities are created only
10631 -- for visibility purposes, and eventually replaced in the course
10632 -- of expansion, so they do not need to be attached to the tree and
10633 -- seen by the back-end. Keeping them internal also avoids spurious
10634 -- freezing problems. The declaration is inserted in the tree for
10635 -- analysis, and removed afterwards. If the equality operator comes
10636 -- from an explicit declaration, attach the inequality immediately
10637 -- after. Else the equality is inherited from a derived type
10638 -- declaration, so insert inequality after that declaration.
10640 if No (Alias (S)) then
10641 Insert_After (Unit_Declaration_Node (S), Decl);
10642 elsif Is_List_Member (Parent (S)) then
10643 Insert_After (Parent (S), Decl);
10645 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10648 Mark_Rewrite_Insertion (Decl);
10649 Set_Is_Intrinsic_Subprogram (Op_Name);
10652 Set_Has_Completion (Op_Name);
10653 Set_Corresponding_Equality (Op_Name, S);
10654 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10656 end Make_Inequality_Operator;
10658 ----------------------
10659 -- May_Need_Actuals --
10660 ----------------------
10662 procedure May_Need_Actuals (Fun : Entity_Id) is
10667 F := First_Formal (Fun);
10669 while Present (F) loop
10670 if No (Default_Value (F)) then
10678 Set_Needs_No_Actuals (Fun, B);
10679 end May_Need_Actuals;
10681 ---------------------
10682 -- Mode_Conformant --
10683 ---------------------
10685 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10688 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10690 end Mode_Conformant;
10692 ---------------------------
10693 -- New_Overloaded_Entity --
10694 ---------------------------
10696 procedure New_Overloaded_Entity
10698 Derived_Type : Entity_Id := Empty)
10700 Overridden_Subp : Entity_Id := Empty;
10701 -- Set if the current scope has an operation that is type-conformant
10702 -- with S, and becomes hidden by S.
10704 Is_Primitive_Subp : Boolean;
10705 -- Set to True if the new subprogram is primitive
10708 -- Entity that S overrides
10710 procedure Check_For_Primitive_Subprogram
10711 (Is_Primitive : out Boolean;
10712 Is_Overriding : Boolean := False);
10713 -- If the subprogram being analyzed is a primitive operation of the type
10714 -- of a formal or result, set the Has_Primitive_Operations flag on the
10715 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10716 -- corresponding flag on the entity itself for later use.
10718 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10719 -- True if a) E is a subprogram whose first formal is a concurrent type
10720 -- defined in the scope of E that has some entry or subprogram whose
10721 -- profile matches E, or b) E is an internally built dispatching
10722 -- subprogram of a protected type and there is a matching subprogram
10723 -- defined in the enclosing scope of the protected type, or c) E is
10724 -- an entry of a synchronized type and a matching procedure has been
10725 -- previously defined in the enclosing scope of the synchronized type.
10727 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10728 -- Check that E is declared in the private part of the current package,
10729 -- or in the package body, where it may hide a previous declaration.
10730 -- We can't use In_Private_Part by itself because this flag is also
10731 -- set when freezing entities, so we must examine the place of the
10732 -- declaration in the tree, and recognize wrapper packages as well.
10734 function Is_Overriding_Alias
10735 (Old_E : Entity_Id;
10736 New_E : Entity_Id) return Boolean;
10737 -- Check whether new subprogram and old subprogram are both inherited
10738 -- from subprograms that have distinct dispatch table entries. This can
10739 -- occur with derivations from instances with accidental homonyms. The
10740 -- function is conservative given that the converse is only true within
10741 -- instances that contain accidental overloadings.
10743 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10744 -- Report conflict between entities S and E
10746 ------------------------------------
10747 -- Check_For_Primitive_Subprogram --
10748 ------------------------------------
10750 procedure Check_For_Primitive_Subprogram
10751 (Is_Primitive : out Boolean;
10752 Is_Overriding : Boolean := False)
10754 Formal : Entity_Id;
10758 function Visible_Part_Type (T : Entity_Id) return Boolean;
10759 -- Returns true if T is declared in the visible part of the current
10760 -- package scope; otherwise returns false. Assumes that T is declared
10763 procedure Check_Private_Overriding (T : Entity_Id);
10764 -- Checks that if a primitive abstract subprogram of a visible
10765 -- abstract type is declared in a private part, then it must override
10766 -- an abstract subprogram declared in the visible part. Also checks
10767 -- that if a primitive function with a controlling result is declared
10768 -- in a private part, then it must override a function declared in
10769 -- the visible part.
10771 ------------------------------
10772 -- Check_Private_Overriding --
10773 ------------------------------
10775 procedure Check_Private_Overriding (T : Entity_Id) is
10776 function Overrides_Private_Part_Op return Boolean;
10777 -- This detects the special case where the overriding subprogram
10778 -- is overriding a subprogram that was declared in the same
10779 -- private part. That case is illegal by 3.9.3(10).
10781 function Overrides_Visible_Function
10782 (Partial_View : Entity_Id) return Boolean;
10783 -- True if S overrides a function in the visible part. The
10784 -- overridden function could be explicitly or implicitly declared.
10786 -------------------------------
10787 -- Overrides_Private_Part_Op --
10788 -------------------------------
10790 function Overrides_Private_Part_Op return Boolean is
10791 Over_Decl : constant Node_Id :=
10792 Unit_Declaration_Node (Overridden_Operation (S));
10793 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10796 pragma Assert (Is_Overriding);
10798 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10800 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10802 return In_Same_List (Over_Decl, Subp_Decl);
10803 end Overrides_Private_Part_Op;
10805 --------------------------------
10806 -- Overrides_Visible_Function --
10807 --------------------------------
10809 function Overrides_Visible_Function
10810 (Partial_View : Entity_Id) return Boolean
10813 if not Is_Overriding or else not Has_Homonym (S) then
10817 if not Present (Partial_View) then
10821 -- Search through all the homonyms H of S in the current
10822 -- package spec, and return True if we find one that matches.
10823 -- Note that Parent (H) will be the declaration of the
10824 -- partial view of T for a match.
10827 H : Entity_Id := S;
10831 exit when not Present (H) or else Scope (H) /= Scope (S);
10833 if Nkind (Parent (H)) in
10834 N_Private_Extension_Declaration |
10835 N_Private_Type_Declaration
10836 and then Defining_Identifier (Parent (H)) = Partial_View
10844 end Overrides_Visible_Function;
10846 -- Start of processing for Check_Private_Overriding
10849 if Is_Package_Or_Generic_Package (Current_Scope)
10850 and then In_Private_Part (Current_Scope)
10851 and then Visible_Part_Type (T)
10852 and then not In_Instance
10854 if Is_Abstract_Type (T)
10855 and then Is_Abstract_Subprogram (S)
10856 and then (not Is_Overriding
10857 or else not Is_Abstract_Subprogram (E)
10858 or else Overrides_Private_Part_Op)
10861 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10864 elsif Ekind (S) = E_Function then
10866 Partial_View : constant Entity_Id :=
10867 Incomplete_Or_Partial_View (T);
10870 if not Overrides_Visible_Function (Partial_View) then
10872 -- Here, S is "function ... return T;" declared in
10873 -- the private part, not overriding some visible
10874 -- operation. That's illegal in the tagged case
10875 -- (but not if the private type is untagged).
10877 if ((Present (Partial_View)
10878 and then Is_Tagged_Type (Partial_View))
10879 or else (not Present (Partial_View)
10880 and then Is_Tagged_Type (T)))
10881 and then T = Base_Type (Etype (S))
10884 ("private function with tagged result must"
10885 & " override visible-part function", S);
10887 ("\move subprogram to the visible part"
10888 & " (RM 3.9.3(10))", S);
10890 -- Ada 2012 (AI05-0073): Extend this check to the case
10891 -- of a function whose result subtype is defined by an
10892 -- access_definition designating specific tagged type.
10894 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10895 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10897 not Is_Class_Wide_Type
10898 (Designated_Type (Etype (S)))
10899 and then Ada_Version >= Ada_2012
10902 ("private function with controlling access "
10903 & "result must override visible-part function",
10906 ("\move subprogram to the visible part"
10907 & " (RM 3.9.3(10))", S);
10913 end Check_Private_Overriding;
10915 -----------------------
10916 -- Visible_Part_Type --
10917 -----------------------
10919 function Visible_Part_Type (T : Entity_Id) return Boolean is
10920 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10923 -- If the entity is a private type, then it must be declared in a
10926 if Ekind (T) in Private_Kind then
10929 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10932 elsif Is_List_Member (Declaration_Node (T))
10933 and then List_Containing (Declaration_Node (T)) =
10934 Visible_Declarations (Specification (P))
10941 end Visible_Part_Type;
10943 -- Start of processing for Check_For_Primitive_Subprogram
10946 Is_Primitive := False;
10948 if not Comes_From_Source (S) then
10951 -- If subprogram is at library level, it is not primitive operation
10953 elsif Current_Scope = Standard_Standard then
10956 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10957 and then not In_Package_Body (Current_Scope))
10958 or else Is_Overriding
10960 -- For function, check return type
10962 if Ekind (S) = E_Function then
10963 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10964 F_Typ := Designated_Type (Etype (S));
10966 F_Typ := Etype (S);
10969 B_Typ := Base_Type (F_Typ);
10971 if Scope (B_Typ) = Current_Scope
10972 and then not Is_Class_Wide_Type (B_Typ)
10973 and then not Is_Generic_Type (B_Typ)
10975 Is_Primitive := True;
10976 Set_Has_Primitive_Operations (B_Typ);
10977 Set_Is_Primitive (S);
10978 Check_Private_Overriding (B_Typ);
10980 -- The Ghost policy in effect at the point of declaration
10981 -- or a tagged type and a primitive operation must match
10982 -- (SPARK RM 6.9(16)).
10984 Check_Ghost_Primitive (S, B_Typ);
10988 -- For all subprograms, check formals
10990 Formal := First_Formal (S);
10991 while Present (Formal) loop
10992 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10993 F_Typ := Designated_Type (Etype (Formal));
10995 F_Typ := Etype (Formal);
10998 B_Typ := Base_Type (F_Typ);
11000 if Ekind (B_Typ) = E_Access_Subtype then
11001 B_Typ := Base_Type (B_Typ);
11004 if Scope (B_Typ) = Current_Scope
11005 and then not Is_Class_Wide_Type (B_Typ)
11006 and then not Is_Generic_Type (B_Typ)
11008 Is_Primitive := True;
11009 Set_Is_Primitive (S);
11010 Set_Has_Primitive_Operations (B_Typ);
11011 Check_Private_Overriding (B_Typ);
11013 -- The Ghost policy in effect at the point of declaration
11014 -- of a tagged type and a primitive operation must match
11015 -- (SPARK RM 6.9(16)).
11017 Check_Ghost_Primitive (S, B_Typ);
11020 Next_Formal (Formal);
11023 -- Special case: An equality function can be redefined for a type
11024 -- occurring in a declarative part, and won't otherwise be treated as
11025 -- a primitive because it doesn't occur in a package spec and doesn't
11026 -- override an inherited subprogram. It's important that we mark it
11027 -- primitive so it can be returned by Collect_Primitive_Operations
11028 -- and be used in composing the equality operation of later types
11029 -- that have a component of the type.
11031 elsif Chars (S) = Name_Op_Eq
11032 and then Etype (S) = Standard_Boolean
11034 B_Typ := Base_Type (Etype (First_Formal (S)));
11036 if Scope (B_Typ) = Current_Scope
11038 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11039 and then not Is_Limited_Type (B_Typ)
11041 Is_Primitive := True;
11042 Set_Is_Primitive (S);
11043 Set_Has_Primitive_Operations (B_Typ);
11044 Check_Private_Overriding (B_Typ);
11046 -- The Ghost policy in effect at the point of declaration of a
11047 -- tagged type and a primitive operation must match
11048 -- (SPARK RM 6.9(16)).
11050 Check_Ghost_Primitive (S, B_Typ);
11053 end Check_For_Primitive_Subprogram;
11055 --------------------------------------
11056 -- Has_Matching_Entry_Or_Subprogram --
11057 --------------------------------------
11059 function Has_Matching_Entry_Or_Subprogram
11060 (E : Entity_Id) return Boolean
11062 function Check_Conforming_Parameters
11063 (E1_Param : Node_Id;
11064 E2_Param : Node_Id;
11065 Ctype : Conformance_Type) return Boolean;
11066 -- Starting from the given parameters, check that all the parameters
11067 -- of two entries or subprograms are conformant. Used to skip
11068 -- the check on the controlling argument.
11070 function Matching_Entry_Or_Subprogram
11071 (Conc_Typ : Entity_Id;
11072 Subp : Entity_Id) return Entity_Id;
11073 -- Return the first entry or subprogram of the given concurrent type
11074 -- whose name matches the name of Subp and has a profile conformant
11075 -- with Subp; return Empty if not found.
11077 function Matching_Dispatching_Subprogram
11078 (Conc_Typ : Entity_Id;
11079 Ent : Entity_Id) return Entity_Id;
11080 -- Return the first dispatching primitive of Conc_Type defined in the
11081 -- enclosing scope of Conc_Type (i.e. before the full definition of
11082 -- this concurrent type) whose name matches the entry Ent and has a
11083 -- profile conformant with the profile of the corresponding (not yet
11084 -- built) dispatching primitive of Ent; return Empty if not found.
11086 function Matching_Original_Protected_Subprogram
11087 (Prot_Typ : Entity_Id;
11088 Subp : Entity_Id) return Entity_Id;
11089 -- Return the first subprogram defined in the enclosing scope of
11090 -- Prot_Typ (before the full definition of this protected type)
11091 -- whose name matches the original name of Subp and has a profile
11092 -- conformant with the profile of Subp; return Empty if not found.
11094 function Normalized_First_Parameter_Type
11095 (E : Entity_Id) return Entity_Id;
11096 -- Return the type of the first parameter unless that type
11097 -- is an anonymous access type, in which case return the
11098 -- designated type. Used to treat anonymous-access-to-synchronized
11099 -- the same as synchronized for purposes of checking for
11100 -- prefixed view profile conflicts.
11102 ---------------------------------
11103 -- Check_Conforming_Parameters --
11104 ---------------------------------
11106 function Check_Conforming_Parameters
11107 (E1_Param : Node_Id;
11108 E2_Param : Node_Id;
11109 Ctype : Conformance_Type) return Boolean
11111 Param_E1 : Node_Id := E1_Param;
11112 Param_E2 : Node_Id := E2_Param;
11115 while Present (Param_E1) and then Present (Param_E2) loop
11116 if (Ctype >= Mode_Conformant) and then
11117 Ekind (Defining_Identifier (Param_E1)) /=
11118 Ekind (Defining_Identifier (Param_E2))
11123 (Find_Parameter_Type (Param_E1),
11124 Find_Parameter_Type (Param_E2),
11134 -- The candidate is not valid if one of the two lists contains
11135 -- more parameters than the other
11137 return No (Param_E1) and then No (Param_E2);
11138 end Check_Conforming_Parameters;
11140 ----------------------------------
11141 -- Matching_Entry_Or_Subprogram --
11142 ----------------------------------
11144 function Matching_Entry_Or_Subprogram
11145 (Conc_Typ : Entity_Id;
11146 Subp : Entity_Id) return Entity_Id
11151 E := First_Entity (Conc_Typ);
11152 while Present (E) loop
11153 if Chars (Subp) = Chars (E)
11154 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11156 Check_Conforming_Parameters
11157 (First (Parameter_Specifications (Parent (E))),
11158 Next (First (Parameter_Specifications (Parent (Subp)))),
11168 end Matching_Entry_Or_Subprogram;
11170 -------------------------------------
11171 -- Matching_Dispatching_Subprogram --
11172 -------------------------------------
11174 function Matching_Dispatching_Subprogram
11175 (Conc_Typ : Entity_Id;
11176 Ent : Entity_Id) return Entity_Id
11181 -- Search for entities in the enclosing scope of this synchonized
11184 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11185 Push_Scope (Scope (Conc_Typ));
11186 E := Current_Entity_In_Scope (Ent);
11189 while Present (E) loop
11190 if Scope (E) = Scope (Conc_Typ)
11191 and then Comes_From_Source (E)
11192 and then Ekind (E) = E_Procedure
11193 and then Present (First_Entity (E))
11194 and then Is_Controlling_Formal (First_Entity (E))
11195 and then Etype (First_Entity (E)) = Conc_Typ
11197 Check_Conforming_Parameters
11198 (First (Parameter_Specifications (Parent (Ent))),
11199 Next (First (Parameter_Specifications (Parent (E)))),
11200 Subtype_Conformant)
11209 end Matching_Dispatching_Subprogram;
11211 --------------------------------------------
11212 -- Matching_Original_Protected_Subprogram --
11213 --------------------------------------------
11215 function Matching_Original_Protected_Subprogram
11216 (Prot_Typ : Entity_Id;
11217 Subp : Entity_Id) return Entity_Id
11219 ICF : constant Boolean :=
11220 Is_Controlling_Formal (First_Entity (Subp));
11224 -- Temporarily decorate the first parameter of Subp as controlling
11225 -- formal, required to invoke Subtype_Conformant.
11227 Set_Is_Controlling_Formal (First_Entity (Subp));
11230 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11232 while Present (E) loop
11233 if Scope (E) = Scope (Prot_Typ)
11234 and then Comes_From_Source (E)
11235 and then Ekind (Subp) = Ekind (E)
11236 and then Present (First_Entity (E))
11237 and then Is_Controlling_Formal (First_Entity (E))
11238 and then Etype (First_Entity (E)) = Prot_Typ
11239 and then Subtype_Conformant (Subp, E,
11240 Skip_Controlling_Formals => True)
11242 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11249 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11252 end Matching_Original_Protected_Subprogram;
11254 -------------------------------------
11255 -- Normalized_First_Parameter_Type --
11256 -------------------------------------
11258 function Normalized_First_Parameter_Type
11259 (E : Entity_Id) return Entity_Id
11261 Result : Entity_Id := Etype (First_Entity (E));
11263 if Ekind (Result) = E_Anonymous_Access_Type then
11264 Result := Designated_Type (Result);
11267 end Normalized_First_Parameter_Type;
11269 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11272 -- Case 1: E is a subprogram whose first formal is a concurrent type
11273 -- defined in the scope of E that has an entry or subprogram whose
11274 -- profile matches E.
11276 if Comes_From_Source (E)
11277 and then Is_Subprogram (E)
11278 and then Present (First_Entity (E))
11279 and then Is_Concurrent_Record_Type
11280 (Normalized_First_Parameter_Type (E))
11283 Scope (Corresponding_Concurrent_Type
11284 (Normalized_First_Parameter_Type (E)))
11287 (Matching_Entry_Or_Subprogram
11288 (Corresponding_Concurrent_Type
11289 (Normalized_First_Parameter_Type (E)),
11292 Report_Conflict (E,
11293 Matching_Entry_Or_Subprogram
11294 (Corresponding_Concurrent_Type
11295 (Normalized_First_Parameter_Type (E)),
11300 -- Case 2: E is an internally built dispatching subprogram of a
11301 -- protected type and there is a subprogram defined in the enclosing
11302 -- scope of the protected type that has the original name of E and
11303 -- its profile is conformant with the profile of E. We check the
11304 -- name of the original protected subprogram associated with E since
11305 -- the expander builds dispatching primitives of protected functions
11306 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11308 elsif not Comes_From_Source (E)
11309 and then Is_Subprogram (E)
11310 and then Present (First_Entity (E))
11311 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11312 and then Present (Original_Protected_Subprogram (E))
11315 (Matching_Original_Protected_Subprogram
11316 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11319 Report_Conflict (E,
11320 Matching_Original_Protected_Subprogram
11321 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11325 -- Case 3: E is an entry of a synchronized type and a matching
11326 -- procedure has been previously defined in the enclosing scope
11327 -- of the synchronized type.
11329 elsif Comes_From_Source (E)
11330 and then Ekind (E) = E_Entry
11332 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11334 Report_Conflict (E,
11335 Matching_Dispatching_Subprogram (Current_Scope, E));
11340 end Has_Matching_Entry_Or_Subprogram;
11342 ----------------------------
11343 -- Is_Private_Declaration --
11344 ----------------------------
11346 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11347 Decl : constant Node_Id := Unit_Declaration_Node (E);
11348 Priv_Decls : List_Id;
11351 if Is_Package_Or_Generic_Package (Current_Scope)
11352 and then In_Private_Part (Current_Scope)
11355 Private_Declarations (Package_Specification (Current_Scope));
11357 return In_Package_Body (Current_Scope)
11359 (Is_List_Member (Decl)
11360 and then List_Containing (Decl) = Priv_Decls)
11361 or else (Nkind (Parent (Decl)) = N_Package_Specification
11363 Is_Compilation_Unit
11364 (Defining_Entity (Parent (Decl)))
11365 and then List_Containing (Parent (Parent (Decl))) =
11370 end Is_Private_Declaration;
11372 --------------------------
11373 -- Is_Overriding_Alias --
11374 --------------------------
11376 function Is_Overriding_Alias
11377 (Old_E : Entity_Id;
11378 New_E : Entity_Id) return Boolean
11380 AO : constant Entity_Id := Alias (Old_E);
11381 AN : constant Entity_Id := Alias (New_E);
11384 return Scope (AO) /= Scope (AN)
11385 or else No (DTC_Entity (AO))
11386 or else No (DTC_Entity (AN))
11387 or else DT_Position (AO) = DT_Position (AN);
11388 end Is_Overriding_Alias;
11390 ---------------------
11391 -- Report_Conflict --
11392 ---------------------
11394 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11396 Error_Msg_Sloc := Sloc (E);
11398 -- Generate message, with useful additional warning if in generic
11400 if Is_Generic_Unit (E) then
11401 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11402 Error_Msg_N ("\& conflicts with declaration#", S);
11404 Error_Msg_N ("& conflicts with declaration#", S);
11406 end Report_Conflict;
11408 -- Start of processing for New_Overloaded_Entity
11411 -- We need to look for an entity that S may override. This must be a
11412 -- homonym in the current scope, so we look for the first homonym of
11413 -- S in the current scope as the starting point for the search.
11415 E := Current_Entity_In_Scope (S);
11417 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11418 -- They are directly added to the list of primitive operations of
11419 -- Derived_Type, unless this is a rederivation in the private part
11420 -- of an operation that was already derived in the visible part of
11421 -- the current package.
11423 if Ada_Version >= Ada_2005
11424 and then Present (Derived_Type)
11425 and then Present (Alias (S))
11426 and then Is_Dispatching_Operation (Alias (S))
11427 and then Present (Find_Dispatching_Type (Alias (S)))
11428 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11430 -- For private types, when the full-view is processed we propagate to
11431 -- the full view the non-overridden entities whose attribute "alias"
11432 -- references an interface primitive. These entities were added by
11433 -- Derive_Subprograms to ensure that interface primitives are
11436 -- Inside_Freeze_Actions is non zero when S corresponds with an
11437 -- internal entity that links an interface primitive with its
11438 -- covering primitive through attribute Interface_Alias (see
11439 -- Add_Internal_Interface_Entities).
11441 if Inside_Freezing_Actions = 0
11442 and then Is_Package_Or_Generic_Package (Current_Scope)
11443 and then In_Private_Part (Current_Scope)
11444 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11445 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11446 and then Full_View (Defining_Identifier (Parent (E)))
11447 = Defining_Identifier (Parent (S))
11448 and then Alias (E) = Alias (S)
11450 Check_Operation_From_Private_View (S, E);
11451 Set_Is_Dispatching_Operation (S);
11456 Enter_Overloaded_Entity (S);
11457 Check_Dispatching_Operation (S, Empty);
11458 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11464 -- For synchronized types check conflicts of this entity with previously
11465 -- defined entities.
11467 if Ada_Version >= Ada_2005
11468 and then Has_Matching_Entry_Or_Subprogram (S)
11473 -- If there is no homonym then this is definitely not overriding
11476 Enter_Overloaded_Entity (S);
11477 Check_Dispatching_Operation (S, Empty);
11478 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11480 -- If subprogram has an explicit declaration, check whether it has an
11481 -- overriding indicator.
11483 if Comes_From_Source (S) then
11484 Check_Synchronized_Overriding (S, Overridden_Subp);
11486 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11487 -- it may have overridden some hidden inherited primitive. Update
11488 -- Overridden_Subp to avoid spurious errors when checking the
11489 -- overriding indicator.
11491 if Ada_Version >= Ada_2012
11492 and then No (Overridden_Subp)
11493 and then Is_Dispatching_Operation (S)
11494 and then Present (Overridden_Operation (S))
11496 Overridden_Subp := Overridden_Operation (S);
11499 Check_Overriding_Indicator
11500 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11502 -- The Ghost policy in effect at the point of declaration of a
11503 -- parent subprogram and an overriding subprogram must match
11504 -- (SPARK RM 6.9(17)).
11506 Check_Ghost_Overriding (S, Overridden_Subp);
11509 -- If there is a homonym that is not overloadable, then we have an
11510 -- error, except for the special cases checked explicitly below.
11512 elsif not Is_Overloadable (E) then
11514 -- Check for spurious conflict produced by a subprogram that has the
11515 -- same name as that of the enclosing generic package. The conflict
11516 -- occurs within an instance, between the subprogram and the renaming
11517 -- declaration for the package. After the subprogram, the package
11518 -- renaming declaration becomes hidden.
11520 if Ekind (E) = E_Package
11521 and then Present (Renamed_Object (E))
11522 and then Renamed_Object (E) = Current_Scope
11523 and then Nkind (Parent (Renamed_Object (E))) =
11524 N_Package_Specification
11525 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11528 Set_Is_Immediately_Visible (E, False);
11529 Enter_Overloaded_Entity (S);
11530 Set_Homonym (S, Homonym (E));
11531 Check_Dispatching_Operation (S, Empty);
11532 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11534 -- If the subprogram is implicit it is hidden by the previous
11535 -- declaration. However if it is dispatching, it must appear in the
11536 -- dispatch table anyway, because it can be dispatched to even if it
11537 -- cannot be called directly.
11539 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11540 Set_Scope (S, Current_Scope);
11542 if Is_Dispatching_Operation (Alias (S)) then
11543 Check_Dispatching_Operation (S, Empty);
11549 Report_Conflict (S, E);
11553 -- E exists and is overloadable
11556 Check_Synchronized_Overriding (S, Overridden_Subp);
11558 -- Loop through E and its homonyms to determine if any of them is
11559 -- the candidate for overriding by S.
11561 while Present (E) loop
11563 -- Definitely not interesting if not in the current scope
11565 if Scope (E) /= Current_Scope then
11568 -- A function can overload the name of an abstract state. The
11569 -- state can be viewed as a function with a profile that cannot
11570 -- be matched by anything.
11572 elsif Ekind (S) = E_Function
11573 and then Ekind (E) = E_Abstract_State
11575 Enter_Overloaded_Entity (S);
11578 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11579 -- procedures locate the internally generated spec. We enforce
11580 -- mode conformance since a tagged type may inherit from
11581 -- interfaces several null primitives which differ only in
11582 -- the mode of the formals.
11584 elsif not Comes_From_Source (S)
11585 and then Is_Null_Procedure (S)
11586 and then not Mode_Conformant (E, S)
11590 -- Check if we have type conformance
11592 elsif Type_Conformant (E, S) then
11594 -- If the old and new entities have the same profile and one
11595 -- is not the body of the other, then this is an error, unless
11596 -- one of them is implicitly declared.
11598 -- There are some cases when both can be implicit, for example
11599 -- when both a literal and a function that overrides it are
11600 -- inherited in a derivation, or when an inherited operation
11601 -- of a tagged full type overrides the inherited operation of
11602 -- a private extension. Ada 83 had a special rule for the
11603 -- literal case. In Ada 95, the later implicit operation hides
11604 -- the former, and the literal is always the former. In the
11605 -- odd case where both are derived operations declared at the
11606 -- same point, both operations should be declared, and in that
11607 -- case we bypass the following test and proceed to the next
11608 -- part. This can only occur for certain obscure cases in
11609 -- instances, when an operation on a type derived from a formal
11610 -- private type does not override a homograph inherited from
11611 -- the actual. In subsequent derivations of such a type, the
11612 -- DT positions of these operations remain distinct, if they
11615 if Present (Alias (S))
11616 and then (No (Alias (E))
11617 or else Comes_From_Source (E)
11618 or else Is_Abstract_Subprogram (S)
11620 (Is_Dispatching_Operation (E)
11621 and then Is_Overriding_Alias (E, S)))
11622 and then Ekind (E) /= E_Enumeration_Literal
11624 -- When an derived operation is overloaded it may be due to
11625 -- the fact that the full view of a private extension
11626 -- re-inherits. It has to be dealt with.
11628 if Is_Package_Or_Generic_Package (Current_Scope)
11629 and then In_Private_Part (Current_Scope)
11631 Check_Operation_From_Private_View (S, E);
11634 -- In any case the implicit operation remains hidden by the
11635 -- existing declaration, which is overriding. Indicate that
11636 -- E overrides the operation from which S is inherited.
11638 if Present (Alias (S)) then
11639 Set_Overridden_Operation (E, Alias (S));
11640 Inherit_Subprogram_Contract (E, Alias (S));
11643 Set_Overridden_Operation (E, S);
11644 Inherit_Subprogram_Contract (E, S);
11647 -- When a dispatching operation overrides an inherited
11648 -- subprogram, it shall be subtype conformant with the
11649 -- inherited subprogram (RM 3.9.2 (10.2)).
11651 if Comes_From_Source (E)
11652 and then Is_Dispatching_Operation (E)
11653 and then Find_Dispatching_Type (S)
11654 = Find_Dispatching_Type (E)
11656 Check_Subtype_Conformant (E, S);
11659 if Comes_From_Source (E) then
11660 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11662 -- The Ghost policy in effect at the point of declaration
11663 -- of a parent subprogram and an overriding subprogram
11664 -- must match (SPARK RM 6.9(17)).
11666 Check_Ghost_Overriding (E, S);
11671 -- Within an instance, the renaming declarations for actual
11672 -- subprograms may become ambiguous, but they do not hide each
11675 elsif Ekind (E) /= E_Entry
11676 and then not Comes_From_Source (E)
11677 and then not Is_Generic_Instance (E)
11678 and then (Present (Alias (E))
11679 or else Is_Intrinsic_Subprogram (E))
11680 and then (not In_Instance
11681 or else No (Parent (E))
11682 or else Nkind (Unit_Declaration_Node (E)) /=
11683 N_Subprogram_Renaming_Declaration)
11685 -- A subprogram child unit is not allowed to override an
11686 -- inherited subprogram (10.1.1(20)).
11688 if Is_Child_Unit (S) then
11690 ("child unit overrides inherited subprogram in parent",
11695 if Is_Non_Overriding_Operation (E, S) then
11696 Enter_Overloaded_Entity (S);
11698 if No (Derived_Type)
11699 or else Is_Tagged_Type (Derived_Type)
11701 Check_Dispatching_Operation (S, Empty);
11707 -- E is a derived operation or an internal operator which
11708 -- is being overridden. Remove E from further visibility.
11709 -- Furthermore, if E is a dispatching operation, it must be
11710 -- replaced in the list of primitive operations of its type
11711 -- (see Override_Dispatching_Operation).
11713 Overridden_Subp := E;
11715 -- It is possible for E to be in the current scope and
11716 -- yet not in the entity chain. This can only occur in a
11717 -- generic context where E is an implicit concatenation
11718 -- in the formal part, because in a generic body the
11719 -- entity chain starts with the formals.
11721 -- In GNATprove mode, a wrapper for an operation with
11722 -- axiomatization may be a homonym of another declaration
11723 -- for an actual subprogram (needs refinement ???).
11725 if No (Prev_Entity (E)) then
11727 and then GNATprove_Mode
11729 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11730 N_Subprogram_Renaming_Declaration
11734 pragma Assert (Chars (E) = Name_Op_Concat);
11739 -- E must be removed both from the entity_list of the
11740 -- current scope, and from the visibility chain.
11742 if Debug_Flag_E then
11743 Write_Str ("Override implicit operation ");
11744 Write_Int (Int (E));
11748 -- If E is a predefined concatenation, it stands for four
11749 -- different operations. As a result, a single explicit
11750 -- declaration does not hide it. In a possible ambiguous
11751 -- situation, Disambiguate chooses the user-defined op,
11752 -- so it is correct to retain the previous internal one.
11754 if Chars (E) /= Name_Op_Concat
11755 or else Ekind (E) /= E_Operator
11757 -- For nondispatching derived operations that are
11758 -- overridden by a subprogram declared in the private
11759 -- part of a package, we retain the derived subprogram
11760 -- but mark it as not immediately visible. If the
11761 -- derived operation was declared in the visible part
11762 -- then this ensures that it will still be visible
11763 -- outside the package with the proper signature
11764 -- (calls from outside must also be directed to this
11765 -- version rather than the overriding one, unlike the
11766 -- dispatching case). Calls from inside the package
11767 -- will still resolve to the overriding subprogram
11768 -- since the derived one is marked as not visible
11769 -- within the package.
11771 -- If the private operation is dispatching, we achieve
11772 -- the overriding by keeping the implicit operation
11773 -- but setting its alias to be the overriding one. In
11774 -- this fashion the proper body is executed in all
11775 -- cases, but the original signature is used outside
11778 -- If the overriding is not in the private part, we
11779 -- remove the implicit operation altogether.
11781 if Is_Private_Declaration (S) then
11782 if not Is_Dispatching_Operation (E) then
11783 Set_Is_Immediately_Visible (E, False);
11785 -- Work done in Override_Dispatching_Operation, so
11786 -- nothing else needs to be done here.
11792 Remove_Entity_And_Homonym (E);
11796 Enter_Overloaded_Entity (S);
11798 -- For entities generated by Derive_Subprograms the
11799 -- overridden operation is the inherited primitive
11800 -- (which is available through the attribute alias).
11802 if not (Comes_From_Source (E))
11803 and then Is_Dispatching_Operation (E)
11804 and then Find_Dispatching_Type (E) =
11805 Find_Dispatching_Type (S)
11806 and then Present (Alias (E))
11807 and then Comes_From_Source (Alias (E))
11809 Set_Overridden_Operation (S, Alias (E));
11810 Inherit_Subprogram_Contract (S, Alias (E));
11812 -- Normal case of setting entity as overridden
11814 -- Note: Static_Initialization and Overridden_Operation
11815 -- attributes use the same field in subprogram entities.
11816 -- Static_Initialization is only defined for internal
11817 -- initialization procedures, where Overridden_Operation
11818 -- is irrelevant. Therefore the setting of this attribute
11819 -- must check whether the target is an init_proc.
11821 elsif not Is_Init_Proc (S) then
11822 Set_Overridden_Operation (S, E);
11823 Inherit_Subprogram_Contract (S, E);
11826 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11828 -- The Ghost policy in effect at the point of declaration
11829 -- of a parent subprogram and an overriding subprogram
11830 -- must match (SPARK RM 6.9(17)).
11832 Check_Ghost_Overriding (S, E);
11834 -- If S is a user-defined subprogram or a null procedure
11835 -- expanded to override an inherited null procedure, or a
11836 -- predefined dispatching primitive then indicate that E
11837 -- overrides the operation from which S is inherited.
11839 if Comes_From_Source (S)
11841 (Present (Parent (S))
11842 and then Nkind (Parent (S)) = N_Procedure_Specification
11843 and then Null_Present (Parent (S)))
11845 (Present (Alias (E))
11847 Is_Predefined_Dispatching_Operation (Alias (E)))
11849 if Present (Alias (E)) then
11850 Set_Overridden_Operation (S, Alias (E));
11851 Inherit_Subprogram_Contract (S, Alias (E));
11855 if Is_Dispatching_Operation (E) then
11857 -- An overriding dispatching subprogram inherits the
11858 -- convention of the overridden subprogram (AI-117).
11860 Set_Convention (S, Convention (E));
11861 Check_Dispatching_Operation (S, E);
11864 Check_Dispatching_Operation (S, Empty);
11867 Check_For_Primitive_Subprogram
11868 (Is_Primitive_Subp, Is_Overriding => True);
11869 goto Check_Inequality;
11871 -- Apparent redeclarations in instances can occur when two
11872 -- formal types get the same actual type. The subprograms in
11873 -- in the instance are legal, even if not callable from the
11874 -- outside. Calls from within are disambiguated elsewhere.
11875 -- For dispatching operations in the visible part, the usual
11876 -- rules apply, and operations with the same profile are not
11877 -- legal (B830001).
11879 elsif (In_Instance_Visible_Part
11880 and then not Is_Dispatching_Operation (E))
11881 or else In_Instance_Not_Visible
11885 -- Here we have a real error (identical profile)
11888 Error_Msg_Sloc := Sloc (E);
11890 -- Avoid cascaded errors if the entity appears in
11891 -- subsequent calls.
11893 Set_Scope (S, Current_Scope);
11895 -- Generate error, with extra useful warning for the case
11896 -- of a generic instance with no completion.
11898 if Is_Generic_Instance (S)
11899 and then not Has_Completion (E)
11902 ("instantiation cannot provide body for&", S);
11903 Error_Msg_N ("\& conflicts with declaration#", S);
11905 Error_Msg_N ("& conflicts with declaration#", S);
11912 -- If one subprogram has an access parameter and the other
11913 -- a parameter of an access type, calls to either might be
11914 -- ambiguous. Verify that parameters match except for the
11915 -- access parameter.
11917 if May_Hide_Profile then
11923 F1 := First_Formal (S);
11924 F2 := First_Formal (E);
11925 while Present (F1) and then Present (F2) loop
11926 if Is_Access_Type (Etype (F1)) then
11927 if not Is_Access_Type (Etype (F2))
11928 or else not Conforming_Types
11929 (Designated_Type (Etype (F1)),
11930 Designated_Type (Etype (F2)),
11933 May_Hide_Profile := False;
11937 not Conforming_Types
11938 (Etype (F1), Etype (F2), Type_Conformant)
11940 May_Hide_Profile := False;
11947 if May_Hide_Profile
11951 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11960 -- On exit, we know that S is a new entity
11962 Enter_Overloaded_Entity (S);
11963 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11964 Check_Overriding_Indicator
11965 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11967 -- The Ghost policy in effect at the point of declaration of a parent
11968 -- subprogram and an overriding subprogram must match
11969 -- (SPARK RM 6.9(17)).
11971 Check_Ghost_Overriding (S, Overridden_Subp);
11973 -- If S is a derived operation for an untagged type then by
11974 -- definition it's not a dispatching operation (even if the parent
11975 -- operation was dispatching), so Check_Dispatching_Operation is not
11976 -- called in that case.
11978 if No (Derived_Type)
11979 or else Is_Tagged_Type (Derived_Type)
11981 Check_Dispatching_Operation (S, Empty);
11985 -- If this is a user-defined equality operator that is not a derived
11986 -- subprogram, create the corresponding inequality. If the operation is
11987 -- dispatching, the expansion is done elsewhere, and we do not create
11988 -- an explicit inequality operation.
11990 <<Check_Inequality>>
11991 if Chars (S) = Name_Op_Eq
11992 and then Etype (S) = Standard_Boolean
11993 and then Present (Parent (S))
11994 and then not Is_Dispatching_Operation (S)
11996 Make_Inequality_Operator (S);
11997 Check_Untagged_Equality (S);
11999 end New_Overloaded_Entity;
12001 ----------------------------------
12002 -- Preanalyze_Formal_Expression --
12003 ----------------------------------
12005 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12006 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12008 In_Spec_Expression := True;
12009 Preanalyze_With_Freezing_And_Resolve (N, T);
12010 In_Spec_Expression := Save_In_Spec_Expression;
12011 end Preanalyze_Formal_Expression;
12013 ---------------------
12014 -- Process_Formals --
12015 ---------------------
12017 procedure Process_Formals
12019 Related_Nod : Node_Id)
12021 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12022 -- Determine whether an access type designates a type coming from a
12025 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12026 -- Check whether the default has a class-wide type. After analysis the
12027 -- default has the type of the formal, so we must also check explicitly
12028 -- for an access attribute.
12030 ----------------------------------
12031 -- Designates_From_Limited_With --
12032 ----------------------------------
12034 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12035 Desig : Entity_Id := Typ;
12038 if Is_Access_Type (Desig) then
12039 Desig := Directly_Designated_Type (Desig);
12042 if Is_Class_Wide_Type (Desig) then
12043 Desig := Root_Type (Desig);
12047 Ekind (Desig) = E_Incomplete_Type
12048 and then From_Limited_With (Desig);
12049 end Designates_From_Limited_With;
12051 ---------------------------
12052 -- Is_Class_Wide_Default --
12053 ---------------------------
12055 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12057 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12058 or else (Nkind (D) = N_Attribute_Reference
12059 and then Attribute_Name (D) = Name_Access
12060 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12061 end Is_Class_Wide_Default;
12065 Context : constant Node_Id := Parent (Parent (T));
12067 Formal : Entity_Id;
12068 Formal_Type : Entity_Id;
12069 Param_Spec : Node_Id;
12072 Num_Out_Params : Nat := 0;
12073 First_Out_Param : Entity_Id := Empty;
12074 -- Used for setting Is_Only_Out_Parameter
12076 -- Start of processing for Process_Formals
12079 -- In order to prevent premature use of the formals in the same formal
12080 -- part, the Ekind is left undefined until all default expressions are
12081 -- analyzed. The Ekind is established in a separate loop at the end.
12083 Param_Spec := First (T);
12084 while Present (Param_Spec) loop
12085 Formal := Defining_Identifier (Param_Spec);
12086 Set_Never_Set_In_Source (Formal, True);
12087 Enter_Name (Formal);
12089 -- Case of ordinary parameters
12091 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12092 Find_Type (Parameter_Type (Param_Spec));
12093 Ptype := Parameter_Type (Param_Spec);
12095 if Ptype = Error then
12099 -- Protect against malformed parameter types
12101 if Nkind (Ptype) not in N_Has_Entity then
12102 Formal_Type := Any_Type;
12104 Formal_Type := Entity (Ptype);
12107 if Is_Incomplete_Type (Formal_Type)
12109 (Is_Class_Wide_Type (Formal_Type)
12110 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12112 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12113 -- primitive operations, as long as their completion is
12114 -- in the same declarative part. If in the private part
12115 -- this means that the type cannot be a Taft-amendment type.
12116 -- Check is done on package exit. For access to subprograms,
12117 -- the use is legal for Taft-amendment types.
12119 -- Ada 2012: tagged incomplete types are allowed as generic
12120 -- formal types. They do not introduce dependencies and the
12121 -- corresponding generic subprogram does not have a delayed
12122 -- freeze, because it does not need a freeze node. However,
12123 -- it is still the case that untagged incomplete types cannot
12124 -- be Taft-amendment types and must be completed in private
12125 -- part, so the subprogram must appear in the list of private
12126 -- dependents of the type.
12128 if Is_Tagged_Type (Formal_Type)
12129 or else (Ada_Version >= Ada_2012
12130 and then not From_Limited_With (Formal_Type)
12131 and then not Is_Generic_Type (Formal_Type))
12133 if Ekind (Scope (Current_Scope)) = E_Package
12134 and then not Is_Generic_Type (Formal_Type)
12135 and then not Is_Class_Wide_Type (Formal_Type)
12137 if Nkind (Parent (T)) not in
12138 N_Access_Function_Definition |
12139 N_Access_Procedure_Definition
12141 Append_Elmt (Current_Scope,
12142 Private_Dependents (Base_Type (Formal_Type)));
12144 -- Freezing is delayed to ensure that Register_Prim
12145 -- will get called for this operation, which is needed
12146 -- in cases where static dispatch tables aren't built.
12147 -- (Note that the same is done for controlling access
12148 -- parameter cases in function Access_Definition.)
12150 if not Is_Thunk (Current_Scope) then
12151 Set_Has_Delayed_Freeze (Current_Scope);
12156 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12157 | N_Access_Procedure_Definition
12159 -- AI05-0151: Tagged incomplete types are allowed in all
12160 -- formal parts. Untagged incomplete types are not allowed
12161 -- in bodies. Limited views of either kind are not allowed
12162 -- if there is no place at which the non-limited view can
12163 -- become available.
12165 -- Incomplete formal untagged types are not allowed in
12166 -- subprogram bodies (but are legal in their declarations).
12167 -- This excludes bodies created for null procedures, which
12168 -- are basic declarations.
12170 if Is_Generic_Type (Formal_Type)
12171 and then not Is_Tagged_Type (Formal_Type)
12172 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12175 ("invalid use of formal incomplete type", Param_Spec);
12177 elsif Ada_Version >= Ada_2012 then
12178 if Is_Tagged_Type (Formal_Type)
12179 and then (not From_Limited_With (Formal_Type)
12180 or else not In_Package_Body)
12184 elsif Nkind (Context) in N_Accept_Statement
12185 | N_Accept_Alternative
12187 or else (Nkind (Context) = N_Subprogram_Body
12188 and then Comes_From_Source (Context))
12191 ("invalid use of untagged incomplete type &",
12192 Ptype, Formal_Type);
12197 ("invalid use of incomplete type&",
12198 Param_Spec, Formal_Type);
12200 -- Further checks on the legality of incomplete types
12201 -- in formal parts are delayed until the freeze point
12202 -- of the enclosing subprogram or access to subprogram.
12206 elsif Ekind (Formal_Type) = E_Void then
12208 ("premature use of&",
12209 Parameter_Type (Param_Spec), Formal_Type);
12212 -- Ada 2012 (AI-142): Handle aliased parameters
12214 if Ada_Version >= Ada_2012
12215 and then Aliased_Present (Param_Spec)
12217 Set_Is_Aliased (Formal);
12219 -- AI12-001: All aliased objects are considered to be specified
12220 -- as independently addressable (RM C.6(8.1/4)).
12222 Set_Is_Independent (Formal);
12225 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12226 -- declaration corresponding to the null-excluding type of the
12227 -- formal in the enclosing scope. Finally, replace the parameter
12228 -- type of the formal with the internal subtype.
12230 if Ada_Version >= Ada_2005
12231 and then Null_Exclusion_Present (Param_Spec)
12233 if not Is_Access_Type (Formal_Type) then
12235 ("`NOT NULL` allowed only for an access type", Param_Spec);
12238 if Can_Never_Be_Null (Formal_Type)
12239 and then Comes_From_Source (Related_Nod)
12242 ("`NOT NULL` not allowed (& already excludes null)",
12243 Param_Spec, Formal_Type);
12247 Create_Null_Excluding_Itype
12249 Related_Nod => Related_Nod,
12250 Scope_Id => Scope (Current_Scope));
12252 -- If the designated type of the itype is an itype that is
12253 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12254 -- on the access subtype, to prevent order-of-elaboration
12255 -- issues in the backend.
12258 -- type T is access procedure;
12259 -- procedure Op (O : not null T);
12261 if Is_Itype (Directly_Designated_Type (Formal_Type))
12263 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12265 Set_Has_Delayed_Freeze (Formal_Type);
12270 -- An access formal type
12274 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12276 -- No need to continue if we already notified errors
12278 if not Present (Formal_Type) then
12282 -- Ada 2005 (AI-254)
12285 AD : constant Node_Id :=
12286 Access_To_Subprogram_Definition
12287 (Parameter_Type (Param_Spec));
12289 if Present (AD) and then Protected_Present (AD) then
12291 Replace_Anonymous_Access_To_Protected_Subprogram
12297 Set_Etype (Formal, Formal_Type);
12299 -- Deal with default expression if present
12301 Default := Expression (Param_Spec);
12303 if Present (Default) then
12304 if Out_Present (Param_Spec) then
12306 ("default initialization only allowed for IN parameters",
12310 -- Do the special preanalysis of the expression (see section on
12311 -- "Handling of Default Expressions" in the spec of package Sem).
12313 Preanalyze_Formal_Expression (Default, Formal_Type);
12315 -- An access to constant cannot be the default for
12316 -- an access parameter that is an access to variable.
12318 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12319 and then not Is_Access_Constant (Formal_Type)
12320 and then Is_Access_Type (Etype (Default))
12321 and then Is_Access_Constant (Etype (Default))
12324 ("formal that is access to variable cannot be initialized "
12325 & "with an access-to-constant expression", Default);
12328 -- Check that the designated type of an access parameter's default
12329 -- is not a class-wide type unless the parameter's designated type
12330 -- is also class-wide.
12332 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12333 and then not Designates_From_Limited_With (Formal_Type)
12334 and then Is_Class_Wide_Default (Default)
12335 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12338 ("access to class-wide expression not allowed here", Default);
12341 -- Check incorrect use of dynamically tagged expressions
12343 if Is_Tagged_Type (Formal_Type) then
12344 Check_Dynamically_Tagged_Expression
12346 Typ => Formal_Type,
12347 Related_Nod => Default);
12351 -- Ada 2005 (AI-231): Static checks
12353 if Ada_Version >= Ada_2005
12354 and then Is_Access_Type (Etype (Formal))
12355 and then Can_Never_Be_Null (Etype (Formal))
12357 Null_Exclusion_Static_Checks (Param_Spec);
12360 -- The following checks are relevant only when SPARK_Mode is on as
12361 -- these are not standard Ada legality rules.
12363 if SPARK_Mode = On then
12364 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
12366 -- A function cannot have a parameter of mode IN OUT or OUT
12369 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
12371 ("function cannot have parameter of mode `OUT` or "
12372 & "`IN OUT`", Formal);
12375 -- A procedure cannot have an effectively volatile formal
12376 -- parameter of mode IN because it behaves as a constant
12377 -- (SPARK RM 7.1.3(4)).
12379 elsif Ekind (Scope (Formal)) = E_Procedure
12380 and then Ekind (Formal) = E_In_Parameter
12381 and then Is_Effectively_Volatile (Formal)
12384 ("formal parameter of mode `IN` cannot be volatile", Formal);
12388 -- Deal with aspects on formal parameters. Only Unreferenced is
12389 -- supported for the time being.
12391 if Has_Aspects (Param_Spec) then
12393 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
12395 while Present (Aspect) loop
12396 if Chars (Identifier (Aspect)) = Name_Unreferenced then
12397 Set_Has_Pragma_Unreferenced (Formal);
12400 ("unsupported aspect& on parameter",
12401 Aspect, Identifier (Aspect));
12413 -- If this is the formal part of a function specification, analyze the
12414 -- subtype mark in the context where the formals are visible but not
12415 -- yet usable, and may hide outer homographs.
12417 if Nkind (Related_Nod) = N_Function_Specification then
12418 Analyze_Return_Type (Related_Nod);
12421 -- Now set the kind (mode) of each formal
12423 Param_Spec := First (T);
12424 while Present (Param_Spec) loop
12425 Formal := Defining_Identifier (Param_Spec);
12426 Set_Formal_Mode (Formal);
12428 if Ekind (Formal) = E_In_Parameter then
12429 Set_Default_Value (Formal, Expression (Param_Spec));
12431 if Present (Expression (Param_Spec)) then
12432 Default := Expression (Param_Spec);
12434 if Is_Scalar_Type (Etype (Default)) then
12435 if Nkind (Parameter_Type (Param_Spec)) /=
12436 N_Access_Definition
12438 Formal_Type := Entity (Parameter_Type (Param_Spec));
12442 (Related_Nod, Parameter_Type (Param_Spec));
12445 Apply_Scalar_Range_Check (Default, Formal_Type);
12449 elsif Ekind (Formal) = E_Out_Parameter then
12450 Num_Out_Params := Num_Out_Params + 1;
12452 if Num_Out_Params = 1 then
12453 First_Out_Param := Formal;
12456 elsif Ekind (Formal) = E_In_Out_Parameter then
12457 Num_Out_Params := Num_Out_Params + 1;
12460 -- Skip remaining processing if formal type was in error
12462 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12463 goto Next_Parameter;
12466 -- Force call by reference if aliased
12469 Conv : constant Convention_Id := Convention (Etype (Formal));
12471 if Is_Aliased (Formal) then
12472 Set_Mechanism (Formal, By_Reference);
12474 -- Warn if user asked this to be passed by copy
12476 if Conv = Convention_Ada_Pass_By_Copy then
12478 ("cannot pass aliased parameter & by copy??", Formal);
12481 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12483 elsif Conv = Convention_Ada_Pass_By_Copy then
12484 Set_Mechanism (Formal, By_Copy);
12486 elsif Conv = Convention_Ada_Pass_By_Reference then
12487 Set_Mechanism (Formal, By_Reference);
12495 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12496 Set_Is_Only_Out_Parameter (First_Out_Param);
12498 end Process_Formals;
12500 ----------------------------
12501 -- Reference_Body_Formals --
12502 ----------------------------
12504 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12509 if Error_Posted (Spec) then
12513 -- Iterate over both lists. They may be of different lengths if the two
12514 -- specs are not conformant.
12516 Fs := First_Formal (Spec);
12517 Fb := First_Formal (Bod);
12518 while Present (Fs) and then Present (Fb) loop
12519 Generate_Reference (Fs, Fb, 'b');
12521 if Style_Check then
12522 Style.Check_Identifier (Fb, Fs);
12525 Set_Spec_Entity (Fb, Fs);
12526 Set_Referenced (Fs, False);
12530 end Reference_Body_Formals;
12532 -------------------------
12533 -- Set_Actual_Subtypes --
12534 -------------------------
12536 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12538 Formal : Entity_Id;
12540 First_Stmt : Node_Id := Empty;
12541 AS_Needed : Boolean;
12544 -- If this is an empty initialization procedure, no need to create
12545 -- actual subtypes (small optimization).
12547 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12550 -- Within a predicate function we do not want to generate local
12551 -- subtypes that may generate nested predicate functions.
12553 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12557 -- The subtype declarations may freeze the formals. The body generated
12558 -- for an expression function is not a freeze point, so do not emit
12559 -- these declarations (small loss of efficiency in rare cases).
12561 if Nkind (N) = N_Subprogram_Body
12562 and then Was_Expression_Function (N)
12567 Formal := First_Formal (Subp);
12568 while Present (Formal) loop
12569 T := Etype (Formal);
12571 -- We never need an actual subtype for a constrained formal
12573 if Is_Constrained (T) then
12574 AS_Needed := False;
12576 -- If we have unknown discriminants, then we do not need an actual
12577 -- subtype, or more accurately we cannot figure it out. Note that
12578 -- all class-wide types have unknown discriminants.
12580 elsif Has_Unknown_Discriminants (T) then
12581 AS_Needed := False;
12583 -- At this stage we have an unconstrained type that may need an
12584 -- actual subtype. For sure the actual subtype is needed if we have
12585 -- an unconstrained array type. However, in an instance, the type
12586 -- may appear as a subtype of the full view, while the actual is
12587 -- in fact private (in which case no actual subtype is needed) so
12588 -- check the kind of the base type.
12590 elsif Is_Array_Type (Base_Type (T)) then
12593 -- The only other case needing an actual subtype is an unconstrained
12594 -- record type which is an IN parameter (we cannot generate actual
12595 -- subtypes for the OUT or IN OUT case, since an assignment can
12596 -- change the discriminant values. However we exclude the case of
12597 -- initialization procedures, since discriminants are handled very
12598 -- specially in this context, see the section entitled "Handling of
12599 -- Discriminants" in Einfo.
12601 -- We also exclude the case of Discrim_SO_Functions (functions used
12602 -- in front-end layout mode for size/offset values), since in such
12603 -- functions only discriminants are referenced, and not only are such
12604 -- subtypes not needed, but they cannot always be generated, because
12605 -- of order of elaboration issues.
12607 elsif Is_Record_Type (T)
12608 and then Ekind (Formal) = E_In_Parameter
12609 and then Chars (Formal) /= Name_uInit
12610 and then not Is_Unchecked_Union (T)
12611 and then not Is_Discrim_SO_Function (Subp)
12615 -- All other cases do not need an actual subtype
12618 AS_Needed := False;
12621 -- Generate actual subtypes for unconstrained arrays and
12622 -- unconstrained discriminated records.
12625 if Nkind (N) = N_Accept_Statement then
12627 -- If expansion is active, the formal is replaced by a local
12628 -- variable that renames the corresponding entry of the
12629 -- parameter block, and it is this local variable that may
12630 -- require an actual subtype.
12632 if Expander_Active then
12633 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12635 Decl := Build_Actual_Subtype (T, Formal);
12638 if Present (Handled_Statement_Sequence (N)) then
12640 First (Statements (Handled_Statement_Sequence (N)));
12641 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12642 Mark_Rewrite_Insertion (Decl);
12644 -- If the accept statement has no body, there will be no
12645 -- reference to the actuals, so no need to compute actual
12652 Decl := Build_Actual_Subtype (T, Formal);
12653 Prepend (Decl, Declarations (N));
12654 Mark_Rewrite_Insertion (Decl);
12657 -- The declaration uses the bounds of an existing object, and
12658 -- therefore needs no constraint checks.
12660 Analyze (Decl, Suppress => All_Checks);
12661 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12663 -- We need to freeze manually the generated type when it is
12664 -- inserted anywhere else than in a declarative part.
12666 if Present (First_Stmt) then
12667 Insert_List_Before_And_Analyze (First_Stmt,
12668 Freeze_Entity (Defining_Identifier (Decl), N));
12670 -- Ditto if the type has a dynamic predicate, because the
12671 -- generated function will mention the actual subtype. The
12672 -- predicate may come from an explicit aspect of be inherited.
12674 elsif Has_Predicates (T) then
12675 Insert_List_After_And_Analyze (Decl,
12676 Freeze_Entity (Defining_Identifier (Decl), N));
12679 if Nkind (N) = N_Accept_Statement
12680 and then Expander_Active
12682 Set_Actual_Subtype (Renamed_Object (Formal),
12683 Defining_Identifier (Decl));
12685 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12689 Next_Formal (Formal);
12691 end Set_Actual_Subtypes;
12693 ---------------------
12694 -- Set_Formal_Mode --
12695 ---------------------
12697 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12698 Spec : constant Node_Id := Parent (Formal_Id);
12699 Id : constant Entity_Id := Scope (Formal_Id);
12702 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12703 -- since we ensure that corresponding actuals are always valid at the
12704 -- point of the call.
12706 if Out_Present (Spec) then
12708 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12710 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12713 if Ekind (Id) in E_Function | E_Generic_Function then
12715 -- [IN] OUT parameters allowed for functions in Ada 2012
12717 if Ada_Version >= Ada_2012 then
12719 -- Even in Ada 2012 operators can only have IN parameters
12721 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12722 Error_Msg_N ("operators can only have IN parameters", Spec);
12725 if In_Present (Spec) then
12726 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12728 Set_Ekind (Formal_Id, E_Out_Parameter);
12731 -- But not in earlier versions of Ada
12734 Error_Msg_N ("functions can only have IN parameters", Spec);
12735 Set_Ekind (Formal_Id, E_In_Parameter);
12738 elsif In_Present (Spec) then
12739 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12742 Set_Ekind (Formal_Id, E_Out_Parameter);
12743 Set_Never_Set_In_Source (Formal_Id, True);
12744 Set_Is_True_Constant (Formal_Id, False);
12745 Set_Current_Value (Formal_Id, Empty);
12749 Set_Ekind (Formal_Id, E_In_Parameter);
12752 -- Set Is_Known_Non_Null for access parameters since the language
12753 -- guarantees that access parameters are always non-null. We also set
12754 -- Can_Never_Be_Null, since there is no way to change the value.
12756 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12758 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12759 -- null; In Ada 2005, only if then null_exclusion is explicit.
12761 if Ada_Version < Ada_2005
12762 or else Can_Never_Be_Null (Etype (Formal_Id))
12764 Set_Is_Known_Non_Null (Formal_Id);
12765 Set_Can_Never_Be_Null (Formal_Id);
12768 -- Ada 2005 (AI-231): Null-exclusion access subtype
12770 elsif Is_Access_Type (Etype (Formal_Id))
12771 and then Can_Never_Be_Null (Etype (Formal_Id))
12773 Set_Is_Known_Non_Null (Formal_Id);
12775 -- We can also set Can_Never_Be_Null (thus preventing some junk
12776 -- access checks) for the case of an IN parameter, which cannot
12777 -- be changed, or for an IN OUT parameter, which can be changed but
12778 -- not to a null value. But for an OUT parameter, the initial value
12779 -- passed in can be null, so we can't set this flag in that case.
12781 if Ekind (Formal_Id) /= E_Out_Parameter then
12782 Set_Can_Never_Be_Null (Formal_Id);
12786 Set_Mechanism (Formal_Id, Default_Mechanism);
12787 Set_Formal_Validity (Formal_Id);
12788 end Set_Formal_Mode;
12790 -------------------------
12791 -- Set_Formal_Validity --
12792 -------------------------
12794 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12796 -- If no validity checking, then we cannot assume anything about the
12797 -- validity of parameters, since we do not know there is any checking
12798 -- of the validity on the call side.
12800 if not Validity_Checks_On then
12803 -- If validity checking for parameters is enabled, this means we are
12804 -- not supposed to make any assumptions about argument values.
12806 elsif Validity_Check_Parameters then
12809 -- If we are checking in parameters, we will assume that the caller is
12810 -- also checking parameters, so we can assume the parameter is valid.
12812 elsif Ekind (Formal_Id) = E_In_Parameter
12813 and then Validity_Check_In_Params
12815 Set_Is_Known_Valid (Formal_Id, True);
12817 -- Similar treatment for IN OUT parameters
12819 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12820 and then Validity_Check_In_Out_Params
12822 Set_Is_Known_Valid (Formal_Id, True);
12824 end Set_Formal_Validity;
12826 ------------------------
12827 -- Subtype_Conformant --
12828 ------------------------
12830 function Subtype_Conformant
12831 (New_Id : Entity_Id;
12832 Old_Id : Entity_Id;
12833 Skip_Controlling_Formals : Boolean := False) return Boolean
12837 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12838 Skip_Controlling_Formals => Skip_Controlling_Formals);
12840 end Subtype_Conformant;
12842 ---------------------
12843 -- Type_Conformant --
12844 ---------------------
12846 function Type_Conformant
12847 (New_Id : Entity_Id;
12848 Old_Id : Entity_Id;
12849 Skip_Controlling_Formals : Boolean := False) return Boolean
12853 May_Hide_Profile := False;
12855 (New_Id, Old_Id, Type_Conformant, False, Result,
12856 Skip_Controlling_Formals => Skip_Controlling_Formals);
12858 end Type_Conformant;
12860 -------------------------------
12861 -- Valid_Operator_Definition --
12862 -------------------------------
12864 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12867 Id : constant Name_Id := Chars (Designator);
12871 F := First_Formal (Designator);
12872 while Present (F) loop
12875 if Present (Default_Value (F)) then
12877 ("default values not allowed for operator parameters",
12880 -- For function instantiations that are operators, we must check
12881 -- separately that the corresponding generic only has in-parameters.
12882 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12883 -- an error could not arise in earlier versions of the language.
12885 elsif Ekind (F) /= E_In_Parameter then
12886 Error_Msg_N ("operators can only have IN parameters", F);
12892 -- Verify that user-defined operators have proper number of arguments
12893 -- First case of operators which can only be unary
12895 if Id in Name_Op_Not | Name_Op_Abs then
12898 -- Case of operators which can be unary or binary
12900 elsif Id in Name_Op_Add | Name_Op_Subtract then
12901 N_OK := (N in 1 .. 2);
12903 -- All other operators can only be binary
12911 ("incorrect number of arguments for operator", Designator);
12915 and then Base_Type (Etype (Designator)) = Standard_Boolean
12916 and then not Is_Intrinsic_Subprogram (Designator)
12919 ("explicit definition of inequality not allowed", Designator);
12921 end Valid_Operator_Definition;