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 Modify_Tree_For_C
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 -- we are generating C code, create subprogram declaration
4077 -- to simplify subsequent C generation.
4080 and then Modify_Tree_For_C
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 we are generating C and this is a function returning a constrained
4175 -- array type for which we must create a procedure with an extra out
4176 -- parameter, build and analyze the body now. The procedure declaration
4177 -- has already been created. We reuse the source body of the function,
4178 -- because in an instance it may contain global references that cannot
4179 -- be reanalyzed. The source function itself is not used any further,
4180 -- so we mark it as having a completion. If the subprogram is a stub the
4181 -- transformation is done later, when the proper body is analyzed.
4184 and then Modify_Tree_For_C
4185 and then Present (Spec_Id)
4186 and then Ekind (Spec_Id) = E_Function
4187 and then Nkind (N) /= N_Subprogram_Body_Stub
4188 and then Rewritten_For_C (Spec_Id)
4190 Set_Has_Completion (Spec_Id);
4192 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
4195 -- The entity for the created procedure must remain invisible, so it
4196 -- does not participate in resolution of subsequent references to the
4199 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
4203 -- If a separate spec is present, then deal with freezing issues
4205 if Present (Spec_Id) then
4206 Spec_Decl := Unit_Declaration_Node (Spec_Id);
4207 Verify_Overriding_Indicator;
4209 -- In general, the spec will be frozen when we start analyzing the
4210 -- body. However, for internally generated operations, such as
4211 -- wrapper functions for inherited operations with controlling
4212 -- results, the spec may not have been frozen by the time we expand
4213 -- the freeze actions that include the bodies. In particular, extra
4214 -- formals for accessibility or for return-in-place may need to be
4215 -- generated. Freeze nodes, if any, are inserted before the current
4216 -- body. These freeze actions are also needed in Compile_Only mode to
4217 -- enable the proper back-end type annotations.
4218 -- They are necessary in any case to ensure proper elaboration order
4221 if Nkind (N) = N_Subprogram_Body
4222 and then Was_Expression_Function (N)
4223 and then not Has_Completion (Spec_Id)
4224 and then Serious_Errors_Detected = 0
4225 and then (Expander_Active
4226 or else Operating_Mode = Check_Semantics
4227 or else Is_Ignored_Ghost_Entity (Spec_Id))
4229 -- The body generated for an expression function that is not a
4230 -- completion is a freeze point neither for the profile nor for
4231 -- anything else. That's why, in order to prevent any freezing
4232 -- during analysis, we need to mask types declared outside the
4233 -- expression (and in an outer scope) that are not yet frozen.
4234 -- This also needs to be done in the case of an ignored Ghost
4235 -- expression function, where the expander isn't active.
4237 Set_Is_Frozen (Spec_Id);
4238 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4240 elsif not Is_Frozen (Spec_Id)
4241 and then Serious_Errors_Detected = 0
4243 Set_Has_Delayed_Freeze (Spec_Id);
4244 Freeze_Before (N, Spec_Id);
4248 -- If the subprogram has a class-wide clone, build its body as a copy
4249 -- of the original body, and rewrite body of original subprogram as a
4250 -- wrapper that calls the clone. If N is a stub, this construction will
4251 -- take place when the proper body is analyzed. No action needed if this
4252 -- subprogram has been eliminated.
4254 if Present (Spec_Id)
4255 and then Present (Class_Wide_Clone (Spec_Id))
4256 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4257 and then Nkind (N) /= N_Subprogram_Body_Stub
4258 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4260 Build_Class_Wide_Clone_Body (Spec_Id, N);
4262 -- This is the new body for the existing primitive operation
4264 Rewrite (N, Build_Class_Wide_Clone_Call
4265 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4266 Set_Has_Completion (Spec_Id, False);
4271 -- Place subprogram on scope stack, and make formals visible. If there
4272 -- is a spec, the visible entity remains that of the spec.
4274 if Present (Spec_Id) then
4275 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4277 if Is_Child_Unit (Spec_Id) then
4278 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4282 Style.Check_Identifier (Body_Id, Spec_Id);
4285 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4286 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4288 if Is_Abstract_Subprogram (Spec_Id) then
4289 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4293 Set_Convention (Body_Id, Convention (Spec_Id));
4294 Set_Has_Completion (Spec_Id);
4296 if Is_Protected_Type (Scope (Spec_Id)) then
4297 Prot_Typ := Scope (Spec_Id);
4300 -- If this is a body generated for a renaming, do not check for
4301 -- full conformance. The check is redundant, because the spec of
4302 -- the body is a copy of the spec in the renaming declaration,
4303 -- and the test can lead to spurious errors on nested defaults.
4305 if Present (Spec_Decl)
4306 and then not Comes_From_Source (N)
4308 (Nkind (Original_Node (Spec_Decl)) =
4309 N_Subprogram_Renaming_Declaration
4310 or else (Present (Corresponding_Body (Spec_Decl))
4312 Nkind (Unit_Declaration_Node
4313 (Corresponding_Body (Spec_Decl))) =
4314 N_Subprogram_Renaming_Declaration))
4318 -- Conversely, the spec may have been generated for specless body
4319 -- with an inline pragma. The entity comes from source, which is
4320 -- both semantically correct and necessary for proper inlining.
4321 -- The subprogram declaration itself is not in the source.
4323 elsif Comes_From_Source (N)
4324 and then Present (Spec_Decl)
4325 and then not Comes_From_Source (Spec_Decl)
4326 and then Has_Pragma_Inline (Spec_Id)
4333 Fully_Conformant, True, Conformant, Body_Id);
4336 -- If the body is not fully conformant, we have to decide if we
4337 -- should analyze it or not. If it has a really messed up profile
4338 -- then we probably should not analyze it, since we will get too
4339 -- many bogus messages.
4341 -- Our decision is to go ahead in the non-fully conformant case
4342 -- only if it is at least mode conformant with the spec. Note
4343 -- that the call to Check_Fully_Conformant has issued the proper
4344 -- error messages to complain about the lack of conformance.
4347 and then not Mode_Conformant (Body_Id, Spec_Id)
4353 -- In the case we are dealing with an expression function we check
4354 -- the formals attached to the spec instead of the body - so we don't
4355 -- reference body formals.
4357 if Spec_Id /= Body_Id
4358 and then not Is_Expression_Function (Spec_Id)
4360 Reference_Body_Formals (Spec_Id, Body_Id);
4363 Set_Ekind (Body_Id, E_Subprogram_Body);
4365 if Nkind (N) = N_Subprogram_Body_Stub then
4366 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4371 Set_Corresponding_Spec (N, Spec_Id);
4373 -- Ada 2005 (AI-345): If the operation is a primitive operation
4374 -- of a concurrent type, the type of the first parameter has been
4375 -- replaced with the corresponding record, which is the proper
4376 -- run-time structure to use. However, within the body there may
4377 -- be uses of the formals that depend on primitive operations
4378 -- of the type (in particular calls in prefixed form) for which
4379 -- we need the original concurrent type. The operation may have
4380 -- several controlling formals, so the replacement must be done
4383 if Comes_From_Source (Spec_Id)
4384 and then Present (First_Entity (Spec_Id))
4385 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4386 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4387 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4388 and then Present (Corresponding_Concurrent_Type
4389 (Etype (First_Entity (Spec_Id))))
4392 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4396 Form := First_Formal (Spec_Id);
4397 while Present (Form) loop
4398 if Etype (Form) = Typ then
4399 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4407 -- Make the formals visible, and place subprogram on scope stack.
4408 -- This is also the point at which we set Last_Real_Spec_Entity
4409 -- to mark the entities which will not be moved to the body.
4411 Install_Formals (Spec_Id);
4412 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4414 -- Within an instance, add local renaming declarations so that
4415 -- gdb can retrieve the values of actuals more easily. This is
4416 -- only relevant if generating code.
4418 if Is_Generic_Instance (Spec_Id)
4419 and then Is_Wrapper_Package (Current_Scope)
4420 and then Expander_Active
4422 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4425 Push_Scope (Spec_Id);
4427 -- Make sure that the subprogram is immediately visible. For
4428 -- child units that have no separate spec this is indispensable.
4429 -- Otherwise it is safe albeit redundant.
4431 Set_Is_Immediately_Visible (Spec_Id);
4434 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4435 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4436 Set_Scope (Body_Id, Scope (Spec_Id));
4438 -- Case of subprogram body with no previous spec
4441 -- Check for style warning required
4445 -- Only apply check for source level subprograms for which checks
4446 -- have not been suppressed.
4448 and then Comes_From_Source (Body_Id)
4449 and then not Suppress_Style_Checks (Body_Id)
4451 -- No warnings within an instance
4453 and then not In_Instance
4455 -- No warnings for expression functions
4457 and then Nkind (Original_Node (N)) /= N_Expression_Function
4459 Style.Body_With_No_Spec (N);
4462 New_Overloaded_Entity (Body_Id);
4464 if Nkind (N) /= N_Subprogram_Body_Stub then
4465 Set_Acts_As_Spec (N);
4466 Generate_Definition (Body_Id);
4468 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4470 -- If the body is an entry wrapper created for an entry with
4471 -- preconditions, it must be compiled in the context of the
4472 -- enclosing synchronized object, because it may mention other
4473 -- operations of the type.
4475 if Is_Entry_Wrapper (Body_Id) then
4477 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4480 Install_Declarations (Prot);
4484 Install_Formals (Body_Id);
4486 Push_Scope (Body_Id);
4489 -- For stubs and bodies with no previous spec, generate references to
4492 Generate_Reference_To_Formals (Body_Id);
4495 -- Entry barrier functions are generated outside the protected type and
4496 -- should not carry the SPARK_Mode of the enclosing context.
4498 if Nkind (N) = N_Subprogram_Body
4499 and then Is_Entry_Barrier_Function (N)
4503 -- The body is generated as part of expression function expansion. When
4504 -- the expression function appears in the visible declarations of a
4505 -- package, the body is added to the private declarations. Since both
4506 -- declarative lists may be subject to a different SPARK_Mode, inherit
4507 -- the mode of the spec.
4509 -- package P with SPARK_Mode is
4510 -- function Expr_Func ... is (...); -- original
4511 -- [function Expr_Func ...;] -- generated spec
4514 -- pragma SPARK_Mode (Off);
4515 -- [function Expr_Func ... is return ...;] -- generated body
4516 -- end P; -- mode is ON
4518 elsif not Comes_From_Source (N)
4519 and then Present (Spec_Id)
4520 and then Is_Expression_Function (Spec_Id)
4522 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4523 Set_SPARK_Pragma_Inherited
4524 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4526 -- Set the SPARK_Mode from the current context (may be overwritten later
4527 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4528 -- initially on a stand-alone subprogram body, but is then relocated to
4529 -- a generated corresponding spec. In this scenario the mode is shared
4530 -- between the spec and body.
4532 elsif No (SPARK_Pragma (Body_Id)) then
4533 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4534 Set_SPARK_Pragma_Inherited (Body_Id);
4537 -- A subprogram body may be instantiated or inlined at a later pass.
4538 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4539 -- applied to the initial declaration of the body.
4541 if Present (Spec_Id) then
4542 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4543 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4547 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4548 -- case the body is instantiated or inlined later and out of context.
4549 -- The body uses this attribute to restore the value of the global
4552 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4553 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4555 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4556 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4560 -- Preserve relevant elaboration-related attributes of the context which
4561 -- are no longer available or very expensive to recompute once analysis,
4562 -- resolution, and expansion are over.
4564 if No (Spec_Id) then
4565 Mark_Elaboration_Attributes
4571 -- If this is the proper body of a stub, we must verify that the stub
4572 -- conforms to the body, and to the previous spec if one was present.
4573 -- We know already that the body conforms to that spec. This test is
4574 -- only required for subprograms that come from source.
4576 if Nkind (Parent (N)) = N_Subunit
4577 and then Comes_From_Source (N)
4578 and then not Error_Posted (Body_Id)
4579 and then Nkind (Corresponding_Stub (Parent (N))) =
4580 N_Subprogram_Body_Stub
4583 Old_Id : constant Entity_Id :=
4585 (Specification (Corresponding_Stub (Parent (N))));
4587 Conformant : Boolean := False;
4590 if No (Spec_Id) then
4591 Check_Fully_Conformant (Body_Id, Old_Id);
4595 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4597 if not Conformant then
4599 -- The stub was taken to be a new declaration. Indicate that
4602 Set_Has_Completion (Old_Id, False);
4608 Set_Has_Completion (Body_Id);
4609 Check_Eliminated (Body_Id);
4611 -- Analyze any aspect specifications that appear on the subprogram body
4612 -- stub. Stop the analysis now as the stub does not have a declarative
4613 -- or a statement part, and it cannot be inlined.
4615 if Nkind (N) = N_Subprogram_Body_Stub then
4616 if Has_Aspects (N) then
4617 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4626 and then Serious_Errors_Detected = 0
4627 and then Present (Spec_Id)
4628 and then Has_Pragma_Inline (Spec_Id)
4630 -- Legacy implementation (relying on front-end inlining)
4632 if not Back_End_Inlining then
4633 if Has_Pragma_Inline_Always (Spec_Id)
4634 or else (Front_End_Inlining
4635 and then not Opt.Disable_FE_Inline)
4637 Build_Body_To_Inline (N, Spec_Id);
4640 -- New implementation (relying on back-end inlining)
4643 if Has_Pragma_Inline_Always (Spec_Id)
4644 or else Optimization_Level > 0
4646 -- Handle function returning an unconstrained type
4648 if Comes_From_Source (Body_Id)
4649 and then Ekind (Spec_Id) = E_Function
4650 and then Returns_Unconstrained_Type (Spec_Id)
4652 -- If function builds in place, i.e. returns a limited type,
4653 -- inlining cannot be done.
4655 and then not Is_Limited_Type (Etype (Spec_Id))
4657 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4661 Subp_Body : constant Node_Id :=
4662 Unit_Declaration_Node (Body_Id);
4663 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4666 -- Do not pass inlining to the backend if the subprogram
4667 -- has declarations or statements which cannot be inlined
4668 -- by the backend. This check is done here to emit an
4669 -- error instead of the generic warning message reported
4670 -- by the GCC backend (ie. "function might not be
4673 if Present (Subp_Decl)
4674 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4678 elsif Has_Excluded_Statement
4681 (Handled_Statement_Sequence (Subp_Body)))
4685 -- If the backend inlining is available then at this
4686 -- stage we only have to mark the subprogram as inlined.
4687 -- The expander will take care of registering it in the
4688 -- table of subprograms inlined by the backend a part of
4689 -- processing calls to it (cf. Expand_Call)
4692 Set_Is_Inlined (Spec_Id);
4699 -- In GNATprove mode, inline only when there is a separate subprogram
4700 -- declaration for now, as inlining of subprogram bodies acting as
4701 -- declarations, or subprogram stubs, are not supported by front-end
4702 -- inlining. This inlining should occur after analysis of the body, so
4703 -- that it is known whether the value of SPARK_Mode, which can be
4704 -- defined by a pragma inside the body, is applicable to the body.
4705 -- Inlining can be disabled with switch -gnatdm
4707 elsif GNATprove_Mode
4708 and then Full_Analysis
4709 and then not Inside_A_Generic
4710 and then Present (Spec_Id)
4712 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4713 and then Body_Has_SPARK_Mode_On
4714 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4715 and then not Body_Has_Contract
4716 and then not Debug_Flag_M
4718 Build_Body_To_Inline (N, Spec_Id);
4721 -- When generating code, inherited pre/postconditions are handled when
4722 -- expanding the corresponding contract.
4724 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4725 -- of the specification we have to install the private withed units.
4726 -- This holds for child units as well.
4728 if Is_Compilation_Unit (Body_Id)
4729 or else Nkind (Parent (N)) = N_Compilation_Unit
4731 Install_Private_With_Clauses (Body_Id);
4734 Check_Anonymous_Return;
4736 -- Set the Protected_Formal field of each extra formal of the protected
4737 -- subprogram to reference the corresponding extra formal of the
4738 -- subprogram that implements it. For regular formals this occurs when
4739 -- the protected subprogram's declaration is expanded, but the extra
4740 -- formals don't get created until the subprogram is frozen. We need to
4741 -- do this before analyzing the protected subprogram's body so that any
4742 -- references to the original subprogram's extra formals will be changed
4743 -- refer to the implementing subprogram's formals (see Expand_Formal).
4745 if Present (Spec_Id)
4746 and then Is_Protected_Type (Scope (Spec_Id))
4747 and then Present (Protected_Body_Subprogram (Spec_Id))
4750 Impl_Subp : constant Entity_Id :=
4751 Protected_Body_Subprogram (Spec_Id);
4752 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4753 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4756 while Present (Prot_Ext_Formal) loop
4757 pragma Assert (Present (Impl_Ext_Formal));
4758 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4759 Next_Formal_With_Extras (Prot_Ext_Formal);
4760 Next_Formal_With_Extras (Impl_Ext_Formal);
4765 -- Generate minimum accessibility local objects to correspond with
4766 -- any extra formal added for anonymous access types. This new local
4767 -- object can then be used instead of the formal in case it is used
4768 -- in an actual to a call to a nested subprogram.
4770 -- This method is used to supplement our "small integer model" for
4771 -- accessibility-check generation (for more information see
4772 -- Accessibility_Level).
4774 -- Because we allow accessibility values greater than our expected value
4775 -- passing along the same extra accessibility formal as an actual
4776 -- to a nested subprogram becomes a problem because high values mean
4777 -- different things to the callee even though they are the same to the
4778 -- caller. So, as described in the first section, we create a local
4779 -- object representing the minimum of the accessibility level value that
4780 -- is passed in and the accessibility level of the callee's parameter
4781 -- and locals and use it in the case of a call to a nested subprogram.
4782 -- This generated object is refered to as a "minimum accessiblity
4785 if Present (Spec_Id) or else Present (Body_Id) then
4786 Body_Nod := Unit_Declaration_Node (Body_Id);
4791 -- Grab the appropriate formal depending on whether there exists
4792 -- an actual spec for the subprogram or whether we are dealing
4793 -- with a protected subprogram.
4795 if Present (Spec_Id) then
4796 if Present (Protected_Body_Subprogram (Spec_Id)) then
4797 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4799 Form := First_Formal (Spec_Id);
4802 Form := First_Formal (Body_Id);
4805 -- Loop through formals if the subprogram is capable of accepting
4806 -- a generated local object. If it is not then it is also not
4807 -- capable of having local subprograms meaning it would not need
4808 -- a minimum accessibility level object anyway.
4810 if Present (Body_Nod)
4811 and then Has_Declarations (Body_Nod)
4812 and then Nkind (Body_Nod) /= N_Package_Specification
4814 while Present (Form) loop
4816 if Present (Extra_Accessibility (Form))
4817 and then No (Minimum_Accessibility (Form))
4819 -- Generate the minimum accessibility level object
4821 -- A60b : constant natural := natural'min(1, paramL);
4823 Generate_Minimum_Accessibility
4824 (Extra_Accessibility (Form), Form);
4830 -- Generate the minimum accessibility level object for the
4831 -- function's Extra_Accessibility_Of_Result.
4833 -- A31b : constant natural := natural'min (2, funcL);
4835 if Ekind (Body_Id) = E_Function
4836 and then Present (Extra_Accessibility_Of_Result (Body_Id))
4838 Generate_Minimum_Accessibility
4839 (Extra_Accessibility_Of_Result (Body_Id));
4841 -- Replace the Extra_Accessibility_Of_Result with the new
4842 -- minimum accessibility object.
4844 Set_Extra_Accessibility_Of_Result
4845 (Body_Id, Minimum_Accessibility
4846 (Extra_Accessibility_Of_Result (Body_Id)));
4852 -- Now we can go on to analyze the body
4854 HSS := Handled_Statement_Sequence (N);
4855 Set_Actual_Subtypes (N, Current_Scope);
4857 -- Add a declaration for the Protection object, renaming declarations
4858 -- for discriminals and privals and finally a declaration for the entry
4859 -- family index (if applicable). This form of early expansion is done
4860 -- when the Expander is active because Install_Private_Data_Declarations
4861 -- references entities which were created during regular expansion. The
4862 -- subprogram entity must come from source, and not be an internally
4863 -- generated subprogram.
4866 and then Present (Prot_Typ)
4867 and then Present (Spec_Id)
4868 and then Comes_From_Source (Spec_Id)
4869 and then not Is_Eliminated (Spec_Id)
4871 Install_Private_Data_Declarations
4872 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4875 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4876 -- may now appear in parameter and result profiles. Since the analysis
4877 -- of a subprogram body may use the parameter and result profile of the
4878 -- spec, swap any limited views with their non-limited counterpart.
4880 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4881 Exch_Views := Exchange_Limited_Views (Spec_Id);
4884 -- If the return type is an anonymous access type whose designated type
4885 -- is the limited view of a class-wide type and the non-limited view is
4886 -- available, update the return type accordingly.
4888 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4894 Rtyp := Etype (Spec_Id);
4896 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4897 Etyp := Directly_Designated_Type (Rtyp);
4899 if Is_Class_Wide_Type (Etyp)
4900 and then From_Limited_With (Etyp)
4903 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4909 -- Analyze any aspect specifications that appear on the subprogram body
4911 if Has_Aspects (N) then
4912 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4915 Analyze_Declarations (Declarations (N));
4917 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4919 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4920 if Present (SPARK_Pragma (Spec_Id)) then
4921 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4923 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4925 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4926 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4927 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4929 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4932 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4935 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4936 -- as specified in source code, or because SPARK_Mode On is ignored
4937 -- in an instance where the context is SPARK_Mode Off/Auto.
4939 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4940 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4945 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4946 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4947 Error_Msg_Sloc := Sloc (Spec_Id);
4949 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4953 -- A subprogram body freezes its own contract. Analyze the contract
4954 -- after the declarations of the body have been processed as pragmas
4955 -- are now chained on the contract of the subprogram body.
4957 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4959 -- Check completion, and analyze the statements
4962 Inspect_Deferred_Constant_Completion (Declarations (N));
4965 -- Add the generated minimum accessibility objects to the subprogram
4966 -- body's list of declarations after analysis of the statements and
4969 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4970 if Present (Declarations (Body_Nod)) then
4971 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4974 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4978 -- Deal with end of scope processing for the body
4980 Process_End_Label (HSS, 't', Current_Scope);
4981 Update_Use_Clause_Chain;
4984 -- If we are compiling an entry wrapper, remove the enclosing
4985 -- synchronized object from the stack.
4987 if Is_Entry_Wrapper (Body_Id) then
4991 Check_Subprogram_Order (N);
4992 Set_Analyzed (Body_Id);
4994 -- If we have a separate spec, then the analysis of the declarations
4995 -- caused the entities in the body to be chained to the spec id, but
4996 -- we want them chained to the body id. Only the formal parameters
4997 -- end up chained to the spec id in this case.
4999 if Present (Spec_Id) then
5001 -- We must conform to the categorization of our spec
5003 Validate_Categorization_Dependency (N, Spec_Id);
5005 -- And if this is a child unit, the parent units must conform
5007 if Is_Child_Unit (Spec_Id) then
5008 Validate_Categorization_Dependency
5009 (Unit_Declaration_Node (Spec_Id), Spec_Id);
5012 -- Here is where we move entities from the spec to the body
5014 -- Case where there are entities that stay with the spec
5016 if Present (Last_Real_Spec_Entity) then
5018 -- No body entities (happens when the only real spec entities come
5019 -- from precondition and postcondition pragmas).
5021 if No (Last_Entity (Body_Id)) then
5022 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
5024 -- Body entities present (formals), so chain stuff past them
5028 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
5031 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
5032 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5033 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
5035 -- Case where there are no spec entities, in this case there can be
5036 -- no body entities either, so just move everything.
5038 -- If the body is generated for an expression function, it may have
5039 -- been preanalyzed already, if 'access was applied to it.
5042 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
5043 N_Expression_Function
5045 pragma Assert (No (Last_Entity (Body_Id)));
5049 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
5050 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5051 Set_First_Entity (Spec_Id, Empty);
5052 Set_Last_Entity (Spec_Id, Empty);
5055 -- Otherwise the body does not complete a previous declaration. Check
5056 -- the categorization of the body against the units it withs.
5059 Validate_Categorization_Dependency (N, Body_Id);
5062 Check_Missing_Return;
5064 -- Now we are going to check for variables that are never modified in
5065 -- the body of the procedure. But first we deal with a special case
5066 -- where we want to modify this check. If the body of the subprogram
5067 -- starts with a raise statement or its equivalent, or if the body
5068 -- consists entirely of a null statement, then it is pretty obvious that
5069 -- it is OK to not reference the parameters. For example, this might be
5070 -- the following common idiom for a stubbed function: statement of the
5071 -- procedure raises an exception. In particular this deals with the
5072 -- common idiom of a stubbed function, which appears something like:
5074 -- function F (A : Integer) return Some_Type;
5077 -- raise Program_Error;
5081 -- Here the purpose of X is simply to satisfy the annoying requirement
5082 -- in Ada that there be at least one return, and we certainly do not
5083 -- want to go posting warnings on X that it is not initialized. On
5084 -- the other hand, if X is entirely unreferenced that should still
5087 -- What we do is to detect these cases, and if we find them, flag the
5088 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
5089 -- suppress unwanted warnings. For the case of the function stub above
5090 -- we have a special test to set X as apparently assigned to suppress
5097 -- Skip call markers installed by the ABE mechanism, labels, and
5098 -- Push_xxx_Error_Label to find the first real statement.
5100 Stm := First (Statements (HSS));
5101 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
5105 -- Do the test on the original statement before expansion
5108 Ostm : constant Node_Id := Original_Node (Stm);
5111 -- If explicit raise statement, turn on flag
5113 if Nkind (Ostm) = N_Raise_Statement then
5114 Set_Trivial_Subprogram (Stm);
5116 -- If null statement, and no following statements, turn on flag
5118 elsif Nkind (Stm) = N_Null_Statement
5119 and then Comes_From_Source (Stm)
5120 and then No (Next (Stm))
5122 Set_Trivial_Subprogram (Stm);
5124 -- Check for explicit call cases which likely raise an exception
5126 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
5127 if Is_Entity_Name (Name (Ostm)) then
5129 Ent : constant Entity_Id := Entity (Name (Ostm));
5132 -- If the procedure is marked No_Return, then likely it
5133 -- raises an exception, but in any case it is not coming
5134 -- back here, so turn on the flag.
5137 and then Ekind (Ent) = E_Procedure
5138 and then No_Return (Ent)
5140 Set_Trivial_Subprogram (Stm);
5148 -- Check for variables that are never modified
5155 -- If there is a separate spec, then transfer Never_Set_In_Source
5156 -- flags from out parameters to the corresponding entities in the
5157 -- body. The reason we do that is we want to post error flags on
5158 -- the body entities, not the spec entities.
5160 if Present (Spec_Id) then
5161 E1 := First_Entity (Spec_Id);
5162 while Present (E1) loop
5163 if Ekind (E1) = E_Out_Parameter then
5164 E2 := First_Entity (Body_Id);
5165 while Present (E2) loop
5166 exit when Chars (E1) = Chars (E2);
5170 if Present (E2) then
5171 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
5179 -- Check references of the subprogram spec when we are dealing with
5180 -- an expression function due to it having a generated body.
5181 -- Otherwise, we simply check the formals of the subprogram body.
5183 if Present (Spec_Id)
5184 and then Is_Expression_Function (Spec_Id)
5186 Check_References (Spec_Id);
5188 Check_References (Body_Id);
5192 -- Check for nested subprogram, and mark outer level subprogram if so
5198 if Present (Spec_Id) then
5205 Ent := Enclosing_Subprogram (Ent);
5206 exit when No (Ent) or else Is_Subprogram (Ent);
5209 if Present (Ent) then
5210 Set_Has_Nested_Subprogram (Ent);
5214 -- Restore the limited views in the spec, if any, to let the back end
5215 -- process it without running into circularities.
5217 if Exch_Views /= No_Elist then
5218 Restore_Limited_Views (Exch_Views);
5221 if Mask_Types /= No_Elist then
5222 Unmask_Unfrozen_Types (Mask_Types);
5225 if Present (Desig_View) then
5226 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5230 if Present (Ignored_Ghost_Region) then
5231 Expander_Active := Saved_EA;
5234 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5235 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5236 end Analyze_Subprogram_Body_Helper;
5238 ------------------------------------
5239 -- Analyze_Subprogram_Declaration --
5240 ------------------------------------
5242 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5243 Scop : constant Entity_Id := Current_Scope;
5244 Designator : Entity_Id;
5246 Is_Completion : Boolean;
5247 -- Indicates whether a null procedure declaration is a completion
5250 -- Null procedures are not allowed in SPARK
5252 if Nkind (Specification (N)) = N_Procedure_Specification
5253 and then Null_Present (Specification (N))
5255 -- Null procedures are allowed in protected types, following the
5256 -- recent AI12-0147.
5258 if Is_Protected_Type (Current_Scope)
5259 and then Ada_Version < Ada_2012
5261 Error_Msg_N ("protected operation cannot be a null procedure", N);
5264 Analyze_Null_Procedure (N, Is_Completion);
5266 -- The null procedure acts as a body, nothing further is needed
5268 if Is_Completion then
5273 Designator := Analyze_Subprogram_Specification (Specification (N));
5275 -- A reference may already have been generated for the unit name, in
5276 -- which case the following call is redundant. However it is needed for
5277 -- declarations that are the rewriting of an expression function.
5279 Generate_Definition (Designator);
5281 -- Set the SPARK mode from the current context (may be overwritten later
5282 -- with explicit pragma). This is not done for entry barrier functions
5283 -- because they are generated outside the protected type and should not
5284 -- carry the mode of the enclosing context.
5286 if Nkind (N) = N_Subprogram_Declaration
5287 and then Is_Entry_Barrier_Function (N)
5292 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5293 Set_SPARK_Pragma_Inherited (Designator);
5296 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5297 -- the body of this subprogram is instantiated or inlined later and out
5298 -- of context. The body uses this attribute to restore the value of the
5301 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5302 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5305 -- Preserve relevant elaboration-related attributes of the context which
5306 -- are no longer available or very expensive to recompute once analysis,
5307 -- resolution, and expansion are over.
5309 Mark_Elaboration_Attributes
5310 (N_Id => Designator,
5314 if Debug_Flag_C then
5315 Write_Str ("==> subprogram spec ");
5316 Write_Name (Chars (Designator));
5317 Write_Str (" from ");
5318 Write_Location (Sloc (N));
5323 Validate_RCI_Subprogram_Declaration (N);
5324 New_Overloaded_Entity (Designator);
5325 Check_Delayed_Subprogram (Designator);
5327 -- If the type of the first formal of the current subprogram is a non-
5328 -- generic tagged private type, mark the subprogram as being a private
5329 -- primitive. Ditto if this is a function with controlling result, and
5330 -- the return type is currently private. In both cases, the type of the
5331 -- controlling argument or result must be in the current scope for the
5332 -- operation to be primitive.
5334 if Has_Controlling_Result (Designator)
5335 and then Is_Private_Type (Etype (Designator))
5336 and then Scope (Etype (Designator)) = Current_Scope
5337 and then not Is_Generic_Actual_Type (Etype (Designator))
5339 Set_Is_Private_Primitive (Designator);
5341 elsif Present (First_Formal (Designator)) then
5343 Formal_Typ : constant Entity_Id :=
5344 Etype (First_Formal (Designator));
5346 Set_Is_Private_Primitive (Designator,
5347 Is_Tagged_Type (Formal_Typ)
5348 and then Scope (Formal_Typ) = Current_Scope
5349 and then Is_Private_Type (Formal_Typ)
5350 and then not Is_Generic_Actual_Type (Formal_Typ));
5354 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5357 if Ada_Version >= Ada_2005
5358 and then Comes_From_Source (N)
5359 and then Is_Dispatching_Operation (Designator)
5366 if Has_Controlling_Result (Designator) then
5367 Etyp := Etype (Designator);
5370 E := First_Entity (Designator);
5372 and then Is_Formal (E)
5373 and then not Is_Controlling_Formal (E)
5381 if Is_Access_Type (Etyp) then
5382 Etyp := Directly_Designated_Type (Etyp);
5385 if Is_Interface (Etyp)
5386 and then not Is_Abstract_Subprogram (Designator)
5387 and then not (Ekind (Designator) = E_Procedure
5388 and then Null_Present (Specification (N)))
5390 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5392 -- Specialize error message based on procedures vs. functions,
5393 -- since functions can't be null subprograms.
5395 if Ekind (Designator) = E_Procedure then
5397 ("interface procedure % must be abstract or null", N);
5400 ("interface function % must be abstract", N);
5406 -- What is the following code for, it used to be
5408 -- ??? Set_Suppress_Elaboration_Checks
5409 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5411 -- The following seems equivalent, but a bit dubious
5413 if Elaboration_Checks_Suppressed (Designator) then
5414 Set_Kill_Elaboration_Checks (Designator);
5417 -- For a compilation unit, set body required. This flag will only be
5418 -- reset if a valid Import or Interface pragma is processed later on.
5420 if Nkind (Parent (N)) = N_Compilation_Unit then
5421 Set_Body_Required (Parent (N), True);
5423 if Ada_Version >= Ada_2005
5424 and then Nkind (Specification (N)) = N_Procedure_Specification
5425 and then Null_Present (Specification (N))
5428 ("null procedure cannot be declared at library level", N);
5432 Generate_Reference_To_Formals (Designator);
5433 Check_Eliminated (Designator);
5435 if Debug_Flag_C then
5437 Write_Str ("<== subprogram spec ");
5438 Write_Name (Chars (Designator));
5439 Write_Str (" from ");
5440 Write_Location (Sloc (N));
5444 -- Indicate that this is a protected operation, because it may be used
5445 -- in subsequent declarations within the protected type.
5447 if Is_Protected_Type (Current_Scope) then
5448 Set_Convention (Designator, Convention_Protected);
5451 List_Inherited_Pre_Post_Aspects (Designator);
5453 -- Process the aspects before establishing the proper categorization in
5454 -- case the subprogram is a compilation unit and one of its aspects is
5455 -- converted into a categorization pragma.
5457 if Has_Aspects (N) then
5458 Analyze_Aspect_Specifications (N, Designator);
5461 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5462 Set_Categorization_From_Scope (Designator, Scop);
5464 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5465 -- proper categorization of the unit based on its pragmas.
5468 Push_Scope (Designator);
5469 Set_Categorization_From_Pragmas (N);
5470 Validate_Categorization_Dependency (N, Designator);
5473 end Analyze_Subprogram_Declaration;
5475 --------------------------------------
5476 -- Analyze_Subprogram_Specification --
5477 --------------------------------------
5479 -- Reminder: N here really is a subprogram specification (not a subprogram
5480 -- declaration). This procedure is called to analyze the specification in
5481 -- both subprogram bodies and subprogram declarations (specs).
5483 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5484 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5485 -- Determine whether entity E denotes the spec or body of an invariant
5488 ------------------------------------
5489 -- Is_Invariant_Procedure_Or_Body --
5490 ------------------------------------
5492 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5493 Decl : constant Node_Id := Unit_Declaration_Node (E);
5497 if Nkind (Decl) = N_Subprogram_Body then
5498 Spec := Corresponding_Spec (Decl);
5505 and then Ekind (Spec) = E_Procedure
5506 and then (Is_Partial_Invariant_Procedure (Spec)
5507 or else Is_Invariant_Procedure (Spec));
5508 end Is_Invariant_Procedure_Or_Body;
5512 Designator : constant Entity_Id := Defining_Entity (N);
5513 Formals : constant List_Id := Parameter_Specifications (N);
5515 -- Start of processing for Analyze_Subprogram_Specification
5518 -- Proceed with analysis. Do not emit a cross-reference entry if the
5519 -- specification comes from an expression function, because it may be
5520 -- the completion of a previous declaration. If it is not, the cross-
5521 -- reference entry will be emitted for the new subprogram declaration.
5523 if Nkind (Parent (N)) /= N_Expression_Function then
5524 Generate_Definition (Designator);
5527 if Nkind (N) = N_Function_Specification then
5528 Set_Ekind (Designator, E_Function);
5529 Set_Mechanism (Designator, Default_Mechanism);
5531 Set_Ekind (Designator, E_Procedure);
5532 Set_Etype (Designator, Standard_Void_Type);
5535 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5536 -- those subprograms which could be inlined in GNATprove mode (because
5537 -- Body_To_Inline is non-Empty) but should not be inlined.
5539 if GNATprove_Mode then
5540 Set_Is_Inlined_Always (Designator);
5543 -- Introduce new scope for analysis of the formals and the return type
5545 Set_Scope (Designator, Current_Scope);
5547 if Present (Formals) then
5548 Push_Scope (Designator);
5549 Process_Formals (Formals, N);
5551 -- Check dimensions in N for formals with default expression
5553 Analyze_Dimension_Formals (N, Formals);
5555 -- Ada 2005 (AI-345): If this is an overriding operation of an
5556 -- inherited interface operation, and the controlling type is
5557 -- a synchronized type, replace the type with its corresponding
5558 -- record, to match the proper signature of an overriding operation.
5559 -- Same processing for an access parameter whose designated type is
5560 -- derived from a synchronized interface.
5562 -- This modification is not done for invariant procedures because
5563 -- the corresponding record may not necessarely be visible when the
5564 -- concurrent type acts as the full view of a private type.
5567 -- type Prot is private with Type_Invariant => ...;
5568 -- procedure ConcInvariant (Obj : Prot);
5570 -- protected type Prot is ...;
5571 -- type Concurrent_Record_Prot is record ...;
5572 -- procedure ConcInvariant (Obj : Prot) is
5574 -- end ConcInvariant;
5577 -- In the example above, both the spec and body of the invariant
5578 -- procedure must utilize the private type as the controlling type.
5580 if Ada_Version >= Ada_2005
5581 and then not Is_Invariant_Procedure_Or_Body (Designator)
5585 Formal_Typ : Entity_Id;
5586 Rec_Typ : Entity_Id;
5587 Desig_Typ : Entity_Id;
5590 Formal := First_Formal (Designator);
5591 while Present (Formal) loop
5592 Formal_Typ := Etype (Formal);
5594 if Is_Concurrent_Type (Formal_Typ)
5595 and then Present (Corresponding_Record_Type (Formal_Typ))
5597 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5599 if Present (Interfaces (Rec_Typ)) then
5600 Set_Etype (Formal, Rec_Typ);
5603 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5604 Desig_Typ := Designated_Type (Formal_Typ);
5606 if Is_Concurrent_Type (Desig_Typ)
5607 and then Present (Corresponding_Record_Type (Desig_Typ))
5609 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5611 if Present (Interfaces (Rec_Typ)) then
5612 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5617 Next_Formal (Formal);
5624 -- The subprogram scope is pushed and popped around the processing of
5625 -- the return type for consistency with call above to Process_Formals
5626 -- (which itself can call Analyze_Return_Type), and to ensure that any
5627 -- itype created for the return type will be associated with the proper
5630 elsif Nkind (N) = N_Function_Specification then
5631 Push_Scope (Designator);
5632 Analyze_Return_Type (N);
5638 if Nkind (N) = N_Function_Specification then
5640 -- Deal with operator symbol case
5642 if Nkind (Designator) = N_Defining_Operator_Symbol then
5643 Valid_Operator_Definition (Designator);
5646 May_Need_Actuals (Designator);
5648 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5649 -- the subprogram is abstract also. This does not apply to renaming
5650 -- declarations, where abstractness is inherited, and to subprogram
5651 -- bodies generated for stream operations, which become renamings as
5654 -- In case of primitives associated with abstract interface types
5655 -- the check is applied later (see Analyze_Subprogram_Declaration).
5657 if Nkind (Original_Node (Parent (N))) not in
5658 N_Abstract_Subprogram_Declaration |
5659 N_Formal_Abstract_Subprogram_Declaration |
5660 N_Subprogram_Renaming_Declaration
5662 if Is_Abstract_Type (Etype (Designator)) then
5664 ("function that returns abstract type must be abstract", N);
5666 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5667 -- access result whose designated type is abstract.
5669 elsif Ada_Version >= Ada_2012
5670 and then Nkind (Result_Definition (N)) = N_Access_Definition
5672 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5673 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5676 ("function whose access result designates abstract type "
5677 & "must be abstract", N);
5683 end Analyze_Subprogram_Specification;
5685 -----------------------
5686 -- Check_Conformance --
5687 -----------------------
5689 procedure Check_Conformance
5690 (New_Id : Entity_Id;
5692 Ctype : Conformance_Type;
5694 Conforms : out Boolean;
5695 Err_Loc : Node_Id := Empty;
5696 Get_Inst : Boolean := False;
5697 Skip_Controlling_Formals : Boolean := False)
5699 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5700 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5701 -- If Errmsg is True, then processing continues to post an error message
5702 -- for conformance error on given node. Two messages are output. The
5703 -- first message points to the previous declaration with a general "no
5704 -- conformance" message. The second is the detailed reason, supplied as
5705 -- Msg. The parameter N provide information for a possible & insertion
5706 -- in the message, and also provides the location for posting the
5707 -- message in the absence of a specified Err_Loc location.
5709 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5710 -- True if the conventions of entities Id1 and Id2 match.
5712 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5713 -- True if the null exclusions of two formals of anonymous access type
5716 -----------------------
5717 -- Conformance_Error --
5718 -----------------------
5720 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5727 if No (Err_Loc) then
5733 Error_Msg_Sloc := Sloc (Old_Id);
5736 when Type_Conformant =>
5737 Error_Msg_N -- CODEFIX
5738 ("not type conformant with declaration#!", Enode);
5740 when Mode_Conformant =>
5741 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5743 ("not mode conformant with operation inherited#!",
5747 ("not mode conformant with declaration#!", Enode);
5750 when Subtype_Conformant =>
5751 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5753 ("not subtype conformant with operation inherited#!",
5757 ("not subtype conformant with declaration#!", Enode);
5760 when Fully_Conformant =>
5761 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5762 Error_Msg_N -- CODEFIX
5763 ("not fully conformant with operation inherited#!",
5766 Error_Msg_N -- CODEFIX
5767 ("not fully conformant with declaration#!", Enode);
5771 Error_Msg_NE (Msg, Enode, N);
5773 end Conformance_Error;
5775 -----------------------
5776 -- Conventions_Match --
5777 -----------------------
5779 function Conventions_Match
5781 Id2 : Entity_Id) return Boolean
5784 -- Ignore the conventions of anonymous access-to-subprogram types
5785 -- and subprogram types because these are internally generated and
5786 -- the only way these may receive a convention is if they inherit
5787 -- the convention of a related subprogram.
5789 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
5792 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
5797 -- Otherwise compare the conventions directly
5800 return Convention (Id1) = Convention (Id2);
5802 end Conventions_Match;
5804 ---------------------------
5805 -- Null_Exclusions_Match --
5806 ---------------------------
5808 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
5810 if not Is_Anonymous_Access_Type (Etype (F1))
5811 or else not Is_Anonymous_Access_Type (Etype (F2))
5816 -- AI12-0289-1: Case of controlling access parameter; False if the
5817 -- partial view is untagged, the full view is tagged, and no explicit
5818 -- "not null". Note that at this point, we're processing the package
5819 -- body, so private/full types have been swapped. The Sloc test below
5820 -- is to detect the (legal) case where F1 comes after the full type
5821 -- declaration. This part is disabled pre-2005, because "not null" is
5822 -- not allowed on those language versions.
5824 if Ada_Version >= Ada_2005
5825 and then Is_Controlling_Formal (F1)
5826 and then not Null_Exclusion_Present (Parent (F1))
5827 and then not Null_Exclusion_Present (Parent (F2))
5830 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
5831 Partial_View_Of_Desig : constant Entity_Id :=
5832 Incomplete_Or_Partial_View (D);
5834 return No (Partial_View_Of_Desig)
5835 or else Is_Tagged_Type (Partial_View_Of_Desig)
5836 or else Sloc (D) < Sloc (F1);
5839 -- Not a controlling parameter, or one or both views have an explicit
5843 return Null_Exclusion_Present (Parent (F1)) =
5844 Null_Exclusion_Present (Parent (F2));
5846 end Null_Exclusions_Match;
5850 Old_Type : constant Entity_Id := Etype (Old_Id);
5851 New_Type : constant Entity_Id := Etype (New_Id);
5852 Old_Formal : Entity_Id;
5853 New_Formal : Entity_Id;
5854 Old_Formal_Base : Entity_Id;
5855 New_Formal_Base : Entity_Id;
5857 -- Start of processing for Check_Conformance
5862 -- We need a special case for operators, since they don't appear
5865 if Ctype = Type_Conformant then
5866 if Ekind (New_Id) = E_Operator
5867 and then Operator_Matches_Spec (New_Id, Old_Id)
5873 -- If both are functions/operators, check return types conform
5875 if Old_Type /= Standard_Void_Type
5877 New_Type /= Standard_Void_Type
5879 -- If we are checking interface conformance we omit controlling
5880 -- arguments and result, because we are only checking the conformance
5881 -- of the remaining parameters.
5883 if Has_Controlling_Result (Old_Id)
5884 and then Has_Controlling_Result (New_Id)
5885 and then Skip_Controlling_Formals
5889 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5890 if Ctype >= Subtype_Conformant
5891 and then not Predicates_Match (Old_Type, New_Type)
5894 ("\predicate of return type does not match!", New_Id);
5897 ("\return type does not match!", New_Id);
5903 -- Ada 2005 (AI-231): In case of anonymous access types check the
5904 -- null-exclusion and access-to-constant attributes match.
5906 if Ada_Version >= Ada_2005
5907 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5909 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5910 or else Is_Access_Constant (Etype (Old_Type)) /=
5911 Is_Access_Constant (Etype (New_Type)))
5913 Conformance_Error ("\return type does not match!", New_Id);
5917 -- If either is a function/operator and the other isn't, error
5919 elsif Old_Type /= Standard_Void_Type
5920 or else New_Type /= Standard_Void_Type
5922 Conformance_Error ("\functions can only match functions!", New_Id);
5926 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5927 -- If this is a renaming as body, refine error message to indicate that
5928 -- the conflict is with the original declaration. If the entity is not
5929 -- frozen, the conventions don't have to match, the one of the renamed
5930 -- entity is inherited.
5932 if Ctype >= Subtype_Conformant then
5933 if not Conventions_Match (Old_Id, New_Id) then
5934 if not Is_Frozen (New_Id) then
5937 elsif Present (Err_Loc)
5938 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5939 and then Present (Corresponding_Spec (Err_Loc))
5941 Error_Msg_Name_1 := Chars (New_Id);
5943 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5944 Conformance_Error ("\prior declaration for% has convention %!");
5948 Conformance_Error ("\calling conventions do not match!");
5952 Check_Formal_Subprogram_Conformance
5953 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
5955 if not Conforms then
5961 -- Deal with parameters
5963 -- Note: we use the entity information, rather than going directly
5964 -- to the specification in the tree. This is not only simpler, but
5965 -- absolutely necessary for some cases of conformance tests between
5966 -- operators, where the declaration tree simply does not exist.
5968 Old_Formal := First_Formal (Old_Id);
5969 New_Formal := First_Formal (New_Id);
5970 while Present (Old_Formal) and then Present (New_Formal) loop
5971 if Is_Controlling_Formal (Old_Formal)
5972 and then Is_Controlling_Formal (New_Formal)
5973 and then Skip_Controlling_Formals
5975 -- The controlling formals will have different types when
5976 -- comparing an interface operation with its match, but both
5977 -- or neither must be access parameters.
5979 if Is_Access_Type (Etype (Old_Formal))
5981 Is_Access_Type (Etype (New_Formal))
5983 goto Skip_Controlling_Formal;
5986 ("\access parameter does not match!", New_Formal);
5990 -- Ada 2012: Mode conformance also requires that formal parameters
5991 -- be both aliased, or neither.
5993 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5994 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5996 ("\aliased parameter mismatch!", New_Formal);
6000 if Ctype = Fully_Conformant then
6002 -- Names must match. Error message is more accurate if we do
6003 -- this before checking that the types of the formals match.
6005 if Chars (Old_Formal) /= Chars (New_Formal) then
6006 Conformance_Error ("\name& does not match!", New_Formal);
6008 -- Set error posted flag on new formal as well to stop
6009 -- junk cascaded messages in some cases.
6011 Set_Error_Posted (New_Formal);
6015 -- Null exclusion must match
6017 if not Null_Exclusions_Match (Old_Formal, New_Formal) then
6019 ("\null exclusion for& does not match", New_Formal);
6021 -- Mark error posted on the new formal to avoid duplicated
6022 -- complaint about types not matching.
6024 Set_Error_Posted (New_Formal);
6028 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6029 -- case occurs whenever a subprogram is being renamed and one of its
6030 -- parameters imposes a null exclusion. For example:
6032 -- type T is null record;
6033 -- type Acc_T is access T;
6034 -- subtype Acc_T_Sub is Acc_T;
6036 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6037 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6040 Old_Formal_Base := Etype (Old_Formal);
6041 New_Formal_Base := Etype (New_Formal);
6044 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6045 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6048 -- Types must always match. In the visible part of an instance,
6049 -- usual overloading rules for dispatching operations apply, and
6050 -- we check base types (not the actual subtypes).
6052 if In_Instance_Visible_Part
6053 and then Is_Dispatching_Operation (New_Id)
6055 if not Conforming_Types
6056 (T1 => Base_Type (Etype (Old_Formal)),
6057 T2 => Base_Type (Etype (New_Formal)),
6059 Get_Inst => Get_Inst)
6061 Conformance_Error ("\type of & does not match!", New_Formal);
6065 elsif not Conforming_Types
6066 (T1 => Old_Formal_Base,
6067 T2 => New_Formal_Base,
6069 Get_Inst => Get_Inst)
6071 -- Don't give error message if old type is Any_Type. This test
6072 -- avoids some cascaded errors, e.g. in case of a bad spec.
6074 if Errmsg and then Old_Formal_Base = Any_Type then
6077 if Ctype >= Subtype_Conformant
6079 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6082 ("\predicate of & does not match!", New_Formal);
6085 ("\type of & does not match!", New_Formal);
6087 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
6089 Error_Msg_N ("\dimensions mismatch!", New_Formal);
6097 -- For mode conformance, mode must match
6099 if Ctype >= Mode_Conformant then
6100 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6101 if Ekind (New_Id) not in E_Function | E_Procedure
6102 or else not Is_Primitive_Wrapper (New_Id)
6104 Conformance_Error ("\mode of & does not match!", New_Formal);
6108 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6110 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
6115 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
6119 ("\mode of & does not match!", New_Formal);
6126 elsif Is_Access_Type (Old_Formal_Base)
6127 and then Is_Access_Type (New_Formal_Base)
6128 and then Is_Access_Constant (Old_Formal_Base) /=
6129 Is_Access_Constant (New_Formal_Base)
6132 ("\constant modifier does not match!", New_Formal);
6137 if Ctype >= Subtype_Conformant then
6139 -- Ada 2005 (AI-231): In case of anonymous access types check
6140 -- the null-exclusion and access-to-constant attributes must
6141 -- match. For null exclusion, we test the types rather than the
6142 -- formals themselves, since the attribute is only set reliably
6143 -- on the formals in the Ada 95 case, and we exclude the case
6144 -- where Old_Formal is marked as controlling, to avoid errors
6145 -- when matching completing bodies with dispatching declarations
6146 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6148 if Ada_Version >= Ada_2005
6149 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
6150 and then Is_Anonymous_Access_Type (Etype (New_Formal))
6152 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6153 Can_Never_Be_Null (Etype (New_Formal))
6155 not Is_Controlling_Formal (Old_Formal))
6157 Is_Access_Constant (Etype (Old_Formal)) /=
6158 Is_Access_Constant (Etype (New_Formal)))
6160 -- Do not complain if error already posted on New_Formal. This
6161 -- avoids some redundant error messages.
6163 and then not Error_Posted (New_Formal)
6165 -- It is allowed to omit the null-exclusion in case of stream
6166 -- attribute subprograms. We recognize stream subprograms
6167 -- through their TSS-generated suffix.
6170 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6173 if TSS_Name /= TSS_Stream_Read
6174 and then TSS_Name /= TSS_Stream_Write
6175 and then TSS_Name /= TSS_Stream_Input
6176 and then TSS_Name /= TSS_Stream_Output
6178 -- Here we have a definite conformance error. It is worth
6179 -- special casing the error message for the case of a
6180 -- controlling formal (which excludes null).
6182 if Is_Controlling_Formal (New_Formal) then
6183 Error_Msg_Node_2 := Scope (New_Formal);
6185 ("\controlling formal & of & excludes null, "
6186 & "declaration must exclude null as well",
6189 -- Normal case (couldn't we give more detail here???)
6193 ("\type of & does not match!", New_Formal);
6202 -- Full conformance checks
6204 if Ctype = Fully_Conformant then
6206 -- We have checked already that names match
6208 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6210 -- Check default expressions for in parameters
6213 NewD : constant Boolean :=
6214 Present (Default_Value (New_Formal));
6215 OldD : constant Boolean :=
6216 Present (Default_Value (Old_Formal));
6218 if NewD or OldD then
6220 -- The old default value has been analyzed because the
6221 -- current full declaration will have frozen everything
6222 -- before. The new default value has not been analyzed,
6223 -- so analyze it now before we check for conformance.
6226 Push_Scope (New_Id);
6227 Preanalyze_Spec_Expression
6228 (Default_Value (New_Formal), Etype (New_Formal));
6232 if not (NewD and OldD)
6233 or else not Fully_Conformant_Expressions
6234 (Default_Value (Old_Formal),
6235 Default_Value (New_Formal))
6238 ("\default expression for & does not match!",
6247 -- A couple of special checks for Ada 83 mode. These checks are
6248 -- skipped if either entity is an operator in package Standard,
6249 -- or if either old or new instance is not from the source program.
6251 if Ada_Version = Ada_83
6252 and then Sloc (Old_Id) > Standard_Location
6253 and then Sloc (New_Id) > Standard_Location
6254 and then Comes_From_Source (Old_Id)
6255 and then Comes_From_Source (New_Id)
6258 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6259 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6262 -- Explicit IN must be present or absent in both cases. This
6263 -- test is required only in the full conformance case.
6265 if In_Present (Old_Param) /= In_Present (New_Param)
6266 and then Ctype = Fully_Conformant
6269 ("\(Ada 83) IN must appear in both declarations",
6274 -- Grouping (use of comma in param lists) must be the same
6275 -- This is where we catch a misconformance like:
6278 -- A : Integer; B : Integer
6280 -- which are represented identically in the tree except
6281 -- for the setting of the flags More_Ids and Prev_Ids.
6283 if More_Ids (Old_Param) /= More_Ids (New_Param)
6284 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6287 ("\grouping of & does not match!", New_Formal);
6293 -- This label is required when skipping controlling formals
6295 <<Skip_Controlling_Formal>>
6297 Next_Formal (Old_Formal);
6298 Next_Formal (New_Formal);
6301 if Present (Old_Formal) then
6302 Conformance_Error ("\too few parameters!");
6305 elsif Present (New_Formal) then
6306 Conformance_Error ("\too many parameters!", New_Formal);
6309 end Check_Conformance;
6311 -----------------------
6312 -- Check_Conventions --
6313 -----------------------
6315 procedure Check_Conventions (Typ : Entity_Id) is
6316 Ifaces_List : Elist_Id;
6318 procedure Check_Convention (Op : Entity_Id);
6319 -- Verify that the convention of inherited dispatching operation Op is
6320 -- consistent among all subprograms it overrides. In order to minimize
6321 -- the search, Search_From is utilized to designate a specific point in
6322 -- the list rather than iterating over the whole list once more.
6324 ----------------------
6325 -- Check_Convention --
6326 ----------------------
6328 procedure Check_Convention (Op : Entity_Id) is
6329 Op_Conv : constant Convention_Id := Convention (Op);
6330 Iface_Conv : Convention_Id;
6331 Iface_Elmt : Elmt_Id;
6332 Iface_Prim_Elmt : Elmt_Id;
6333 Iface_Prim : Entity_Id;
6336 Iface_Elmt := First_Elmt (Ifaces_List);
6337 while Present (Iface_Elmt) loop
6339 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6340 while Present (Iface_Prim_Elmt) loop
6341 Iface_Prim := Node (Iface_Prim_Elmt);
6342 Iface_Conv := Convention (Iface_Prim);
6344 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6345 and then Iface_Conv /= Op_Conv
6348 ("inconsistent conventions in primitive operations", Typ);
6350 Error_Msg_Name_1 := Chars (Op);
6351 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6352 Error_Msg_Sloc := Sloc (Op);
6354 if Comes_From_Source (Op) or else No (Alias (Op)) then
6355 if not Present (Overridden_Operation (Op)) then
6356 Error_Msg_N ("\\primitive % defined #", Typ);
6359 ("\\overriding operation % with "
6360 & "convention % defined #", Typ);
6363 else pragma Assert (Present (Alias (Op)));
6364 Error_Msg_Sloc := Sloc (Alias (Op));
6365 Error_Msg_N ("\\inherited operation % with "
6366 & "convention % defined #", Typ);
6369 Error_Msg_Name_1 := Chars (Op);
6370 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6371 Error_Msg_Sloc := Sloc (Iface_Prim);
6372 Error_Msg_N ("\\overridden operation % with "
6373 & "convention % defined #", Typ);
6375 -- Avoid cascading errors
6380 Next_Elmt (Iface_Prim_Elmt);
6383 Next_Elmt (Iface_Elmt);
6385 end Check_Convention;
6389 Prim_Op : Entity_Id;
6390 Prim_Op_Elmt : Elmt_Id;
6392 -- Start of processing for Check_Conventions
6395 if not Has_Interfaces (Typ) then
6399 Collect_Interfaces (Typ, Ifaces_List);
6401 -- The algorithm checks every overriding dispatching operation against
6402 -- all the corresponding overridden dispatching operations, detecting
6403 -- differences in conventions.
6405 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6406 while Present (Prim_Op_Elmt) loop
6407 Prim_Op := Node (Prim_Op_Elmt);
6409 -- A small optimization: skip the predefined dispatching operations
6410 -- since they always have the same convention.
6412 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6413 Check_Convention (Prim_Op);
6416 Next_Elmt (Prim_Op_Elmt);
6418 end Check_Conventions;
6420 ------------------------------
6421 -- Check_Delayed_Subprogram --
6422 ------------------------------
6424 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6425 procedure Possible_Freeze (T : Entity_Id);
6426 -- T is the type of either a formal parameter or of the return type. If
6427 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6428 -- itself must be delayed.
6430 ---------------------
6431 -- Possible_Freeze --
6432 ---------------------
6434 procedure Possible_Freeze (T : Entity_Id) is
6435 Scop : constant Entity_Id := Scope (Designator);
6438 -- If the subprogram appears within a package instance (which may be
6439 -- the wrapper package of a subprogram instance) the freeze node for
6440 -- that package will freeze the subprogram at the proper place, so
6441 -- do not emit a freeze node for the subprogram, given that it may
6442 -- appear in the wrong scope.
6444 if Ekind (Scop) = E_Package
6445 and then not Comes_From_Source (Scop)
6446 and then Is_Generic_Instance (Scop)
6450 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6451 Set_Has_Delayed_Freeze (Designator);
6453 elsif Is_Access_Type (T)
6454 and then Has_Delayed_Freeze (Designated_Type (T))
6455 and then not Is_Frozen (Designated_Type (T))
6457 Set_Has_Delayed_Freeze (Designator);
6459 end Possible_Freeze;
6465 -- Start of processing for Check_Delayed_Subprogram
6468 -- All subprograms, including abstract subprograms, may need a freeze
6469 -- node if some formal type or the return type needs one.
6471 Possible_Freeze (Etype (Designator));
6472 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6474 -- Need delayed freeze if any of the formal types themselves need a
6475 -- delayed freeze and are not yet frozen.
6477 F := First_Formal (Designator);
6478 while Present (F) loop
6479 Possible_Freeze (Etype (F));
6480 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6484 -- Mark functions that return by reference. Note that it cannot be done
6485 -- for delayed_freeze subprograms because the underlying returned type
6486 -- may not be known yet (for private types).
6488 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6490 Typ : constant Entity_Id := Etype (Designator);
6491 Utyp : constant Entity_Id := Underlying_Type (Typ);
6494 if Is_Limited_View (Typ) then
6495 Set_Returns_By_Ref (Designator);
6497 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6498 Set_Returns_By_Ref (Designator);
6502 end Check_Delayed_Subprogram;
6504 ------------------------------------
6505 -- Check_Discriminant_Conformance --
6506 ------------------------------------
6508 procedure Check_Discriminant_Conformance
6513 Old_Discr : Entity_Id := First_Discriminant (Prev);
6514 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6515 New_Discr_Id : Entity_Id;
6516 New_Discr_Type : Entity_Id;
6518 procedure Conformance_Error (Msg : String; N : Node_Id);
6519 -- Post error message for conformance error on given node. Two messages
6520 -- are output. The first points to the previous declaration with a
6521 -- general "no conformance" message. The second is the detailed reason,
6522 -- supplied as Msg. The parameter N provide information for a possible
6523 -- & insertion in the message.
6525 -----------------------
6526 -- Conformance_Error --
6527 -----------------------
6529 procedure Conformance_Error (Msg : String; N : Node_Id) is
6531 Error_Msg_Sloc := Sloc (Prev_Loc);
6532 Error_Msg_N -- CODEFIX
6533 ("not fully conformant with declaration#!", N);
6534 Error_Msg_NE (Msg, N, N);
6535 end Conformance_Error;
6537 -- Start of processing for Check_Discriminant_Conformance
6540 while Present (Old_Discr) and then Present (New_Discr) loop
6541 New_Discr_Id := Defining_Identifier (New_Discr);
6543 -- The subtype mark of the discriminant on the full type has not
6544 -- been analyzed so we do it here. For an access discriminant a new
6547 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6549 Access_Definition (N, Discriminant_Type (New_Discr));
6552 Find_Type (Discriminant_Type (New_Discr));
6553 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6555 -- Ada 2005: if the discriminant definition carries a null
6556 -- exclusion, create an itype to check properly for consistency
6557 -- with partial declaration.
6559 if Is_Access_Type (New_Discr_Type)
6560 and then Null_Exclusion_Present (New_Discr)
6563 Create_Null_Excluding_Itype
6564 (T => New_Discr_Type,
6565 Related_Nod => New_Discr,
6566 Scope_Id => Current_Scope);
6570 if not Conforming_Types
6571 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6573 Conformance_Error ("type of & does not match!", New_Discr_Id);
6576 -- Treat the new discriminant as an occurrence of the old one,
6577 -- for navigation purposes, and fill in some semantic
6578 -- information, for completeness.
6580 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6581 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6582 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6587 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6588 Conformance_Error ("name & does not match!", New_Discr_Id);
6592 -- Default expressions must match
6595 NewD : constant Boolean :=
6596 Present (Expression (New_Discr));
6597 OldD : constant Boolean :=
6598 Present (Expression (Parent (Old_Discr)));
6601 if NewD or OldD then
6603 -- The old default value has been analyzed and expanded,
6604 -- because the current full declaration will have frozen
6605 -- everything before. The new default values have not been
6606 -- expanded, so expand now to check conformance.
6609 Preanalyze_Spec_Expression
6610 (Expression (New_Discr), New_Discr_Type);
6613 if not (NewD and OldD)
6614 or else not Fully_Conformant_Expressions
6615 (Expression (Parent (Old_Discr)),
6616 Expression (New_Discr))
6620 ("default expression for & does not match!",
6627 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6629 if Ada_Version = Ada_83 then
6631 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6634 -- Grouping (use of comma in param lists) must be the same
6635 -- This is where we catch a misconformance like:
6638 -- A : Integer; B : Integer
6640 -- which are represented identically in the tree except
6641 -- for the setting of the flags More_Ids and Prev_Ids.
6643 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6644 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6647 ("grouping of & does not match!", New_Discr_Id);
6653 Next_Discriminant (Old_Discr);
6657 if Present (Old_Discr) then
6658 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6661 elsif Present (New_Discr) then
6663 ("too many discriminants!", Defining_Identifier (New_Discr));
6666 end Check_Discriminant_Conformance;
6668 -----------------------------------------
6669 -- Check_Formal_Subprogram_Conformance --
6670 -----------------------------------------
6672 procedure Check_Formal_Subprogram_Conformance
6673 (New_Id : Entity_Id;
6677 Conforms : out Boolean)
6683 if Is_Formal_Subprogram (Old_Id)
6684 or else Is_Formal_Subprogram (New_Id)
6685 or else (Is_Subprogram (New_Id)
6686 and then Present (Alias (New_Id))
6687 and then Is_Formal_Subprogram (Alias (New_Id)))
6689 if Present (Err_Loc) then
6698 Error_Msg_Sloc := Sloc (Old_Id);
6699 Error_Msg_N ("not subtype conformant with declaration#!", N);
6701 ("\formal subprograms are not subtype conformant "
6702 & "(RM 6.3.1 (17/3))", N, New_Id);
6705 end Check_Formal_Subprogram_Conformance;
6707 procedure Check_Formal_Subprogram_Conformance
6708 (New_Id : Entity_Id;
6710 Err_Loc : Node_Id := Empty)
6714 Check_Formal_Subprogram_Conformance
6715 (New_Id, Old_Id, Err_Loc, True, Ignore);
6716 end Check_Formal_Subprogram_Conformance;
6718 ----------------------------
6719 -- Check_Fully_Conformant --
6720 ----------------------------
6722 procedure Check_Fully_Conformant
6723 (New_Id : Entity_Id;
6725 Err_Loc : Node_Id := Empty)
6728 pragma Warnings (Off, Result);
6731 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6732 end Check_Fully_Conformant;
6734 --------------------------
6735 -- Check_Limited_Return --
6736 --------------------------
6738 procedure Check_Limited_Return
6744 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6745 -- replaced by anonymous access results. This is an incompatibility with
6746 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6747 -- controllable with special switch. ???
6749 -- A limited interface that is not immutably limited is OK
6751 if Is_Limited_Interface (R_Type)
6753 not (Is_Task_Interface (R_Type)
6754 or else Is_Protected_Interface (R_Type)
6755 or else Is_Synchronized_Interface (R_Type))
6759 elsif Is_Limited_Type (R_Type)
6760 and then not Is_Interface (R_Type)
6761 and then Comes_From_Source (N)
6762 and then not In_Instance_Body
6763 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6765 -- Error in Ada 2005
6767 if Ada_Version >= Ada_2005
6768 and then not Debug_Flag_Dot_L
6769 and then not GNAT_Mode
6772 ("(Ada 2005) cannot copy object of a limited type "
6773 & "(RM-2005 6.5(5.5/2))", Expr);
6775 if Is_Limited_View (R_Type) then
6777 ("\return by reference not permitted in Ada 2005", Expr);
6780 -- Warn in Ada 95 mode, to give folks a heads up about this
6783 -- In GNAT mode, this is just a warning, to allow it to be evilly
6784 -- turned off. Otherwise it is a real error.
6786 -- In a generic context, simplify the warning because it makes no
6787 -- sense to discuss pass-by-reference or copy.
6789 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6790 if Inside_A_Generic then
6792 ("return of limited object not permitted in Ada 2005 "
6793 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6795 elsif Is_Limited_View (R_Type) then
6797 ("return by reference not permitted in Ada 2005 "
6798 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6801 ("cannot copy object of a limited type in Ada 2005 "
6802 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6805 -- Ada 95 mode, and compatibility warnings disabled
6808 pragma Assert (Ada_Version <= Ada_95);
6809 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6810 return; -- skip continuation messages below
6813 if not Inside_A_Generic then
6815 ("\consider switching to return of access type", Expr);
6816 Explain_Limited_Type (R_Type, Expr);
6819 end Check_Limited_Return;
6821 ---------------------------
6822 -- Check_Mode_Conformant --
6823 ---------------------------
6825 procedure Check_Mode_Conformant
6826 (New_Id : Entity_Id;
6828 Err_Loc : Node_Id := Empty;
6829 Get_Inst : Boolean := False)
6832 pragma Warnings (Off, Result);
6835 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6836 end Check_Mode_Conformant;
6838 --------------------------------
6839 -- Check_Overriding_Indicator --
6840 --------------------------------
6842 procedure Check_Overriding_Indicator
6844 Overridden_Subp : Entity_Id;
6845 Is_Primitive : Boolean)
6851 -- No overriding indicator for literals
6853 if Ekind (Subp) = E_Enumeration_Literal then
6856 elsif Ekind (Subp) = E_Entry then
6857 Decl := Parent (Subp);
6859 -- No point in analyzing a malformed operator
6861 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6862 and then Error_Posted (Subp)
6867 Decl := Unit_Declaration_Node (Subp);
6870 if Nkind (Decl) in N_Subprogram_Body
6871 | N_Subprogram_Body_Stub
6872 | N_Subprogram_Declaration
6873 | N_Abstract_Subprogram_Declaration
6874 | N_Subprogram_Renaming_Declaration
6876 Spec := Specification (Decl);
6878 elsif Nkind (Decl) = N_Entry_Declaration then
6885 -- An overriding indication is illegal on a subprogram declared
6886 -- in a protected body, where there is no operation to override.
6888 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6889 and then Is_List_Member (Decl)
6890 and then Present (Parent (List_Containing (Decl)))
6891 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6894 ("illegal overriding indication in protected body", Decl);
6898 -- The overriding operation is type conformant with the overridden one,
6899 -- but the names of the formals are not required to match. If the names
6900 -- appear permuted in the overriding operation, this is a possible
6901 -- source of confusion that is worth diagnosing. Controlling formals
6902 -- often carry names that reflect the type, and it is not worthwhile
6903 -- requiring that their names match.
6905 if Present (Overridden_Subp)
6906 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6913 Form1 := First_Formal (Subp);
6914 Form2 := First_Formal (Overridden_Subp);
6916 -- If the overriding operation is a synchronized operation, skip
6917 -- the first parameter of the overridden operation, which is
6918 -- implicit in the new one. If the operation is declared in the
6919 -- body it is not primitive and all formals must match.
6921 if Is_Concurrent_Type (Scope (Subp))
6922 and then Is_Tagged_Type (Scope (Subp))
6923 and then not Has_Completion (Scope (Subp))
6925 Form2 := Next_Formal (Form2);
6928 if Present (Form1) then
6929 Form1 := Next_Formal (Form1);
6930 Form2 := Next_Formal (Form2);
6933 while Present (Form1) loop
6934 if not Is_Controlling_Formal (Form1)
6935 and then Present (Next_Formal (Form2))
6936 and then Chars (Form1) = Chars (Next_Formal (Form2))
6938 Error_Msg_Node_2 := Alias (Overridden_Subp);
6939 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6941 ("& does not match corresponding formal of&#",
6946 Next_Formal (Form1);
6947 Next_Formal (Form2);
6952 -- If there is an overridden subprogram, then check that there is no
6953 -- "not overriding" indicator, and mark the subprogram as overriding.
6955 -- This is not done if the overridden subprogram is marked as hidden,
6956 -- which can occur for the case of inherited controlled operations
6957 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6958 -- subprogram is not itself hidden or we are within a generic instance,
6959 -- in which case the hidden flag may have been modified for the
6960 -- expansion of the instance.
6962 -- (Note: This condition could probably be simplified, leaving out the
6963 -- testing for the specific controlled cases, but it seems safer and
6964 -- clearer this way, and echoes similar special-case tests of this
6965 -- kind in other places.)
6967 if Present (Overridden_Subp)
6968 and then (not Is_Hidden (Overridden_Subp)
6970 (Chars (Overridden_Subp) in Name_Initialize
6973 and then Present (Alias (Overridden_Subp))
6974 and then (not Is_Hidden (Alias (Overridden_Subp))
6975 or else In_Instance)))
6977 if Must_Not_Override (Spec) then
6978 Error_Msg_Sloc := Sloc (Overridden_Subp);
6980 if Ekind (Subp) = E_Entry then
6982 ("entry & overrides inherited operation #", Spec, Subp);
6985 ("subprogram & overrides inherited operation #", Spec, Subp);
6988 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6989 -- as an extension of Root_Controlled, and thus has a useless Adjust
6990 -- operation. This operation should not be inherited by other limited
6991 -- controlled types. An explicit Adjust for them is not overriding.
6993 elsif Must_Override (Spec)
6994 and then Chars (Overridden_Subp) = Name_Adjust
6995 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6996 and then Present (Alias (Overridden_Subp))
6997 and then In_Predefined_Unit (Alias (Overridden_Subp))
7000 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
7001 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7003 elsif Is_Subprogram (Subp) then
7004 if Is_Init_Proc (Subp) then
7007 elsif No (Overridden_Operation (Subp)) then
7009 -- For entities generated by Derive_Subprograms the overridden
7010 -- operation is the inherited primitive (which is available
7011 -- through the attribute alias)
7013 if (Is_Dispatching_Operation (Subp)
7014 or else Is_Dispatching_Operation (Overridden_Subp))
7015 and then not Comes_From_Source (Overridden_Subp)
7016 and then Find_Dispatching_Type (Overridden_Subp) =
7017 Find_Dispatching_Type (Subp)
7018 and then Present (Alias (Overridden_Subp))
7019 and then Comes_From_Source (Alias (Overridden_Subp))
7021 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7022 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
7025 Set_Overridden_Operation (Subp, Overridden_Subp);
7026 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
7031 -- If primitive flag is set or this is a protected operation, then
7032 -- the operation is overriding at the point of its declaration, so
7033 -- warn if necessary. Otherwise it may have been declared before the
7034 -- operation it overrides and no check is required.
7037 and then not Must_Override (Spec)
7038 and then (Is_Primitive
7039 or else Ekind (Scope (Subp)) = E_Protected_Type)
7041 Style.Missing_Overriding (Decl, Subp);
7044 -- If Subp is an operator, it may override a predefined operation, if
7045 -- it is defined in the same scope as the type to which it applies.
7046 -- In that case Overridden_Subp is empty because of our implicit
7047 -- representation for predefined operators. We have to check whether the
7048 -- signature of Subp matches that of a predefined operator. Note that
7049 -- first argument provides the name of the operator, and the second
7050 -- argument the signature that may match that of a standard operation.
7051 -- If the indicator is overriding, then the operator must match a
7052 -- predefined signature, because we know already that there is no
7053 -- explicit overridden operation.
7055 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7056 if Must_Not_Override (Spec) then
7058 -- If this is not a primitive or a protected subprogram, then
7059 -- "not overriding" is illegal.
7062 and then Ekind (Scope (Subp)) /= E_Protected_Type
7064 Error_Msg_N ("overriding indicator only allowed "
7065 & "if subprogram is primitive", Subp);
7067 elsif Can_Override_Operator (Subp) then
7069 ("subprogram& overrides predefined operator ", Spec, Subp);
7072 elsif Must_Override (Spec) then
7073 if No (Overridden_Operation (Subp))
7074 and then not Can_Override_Operator (Subp)
7076 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7079 elsif not Error_Posted (Subp)
7080 and then Style_Check
7081 and then Can_Override_Operator (Subp)
7082 and then not In_Predefined_Unit (Subp)
7084 -- If style checks are enabled, indicate that the indicator is
7085 -- missing. However, at the point of declaration, the type of
7086 -- which this is a primitive operation may be private, in which
7087 -- case the indicator would be premature.
7089 if Has_Private_Declaration (Etype (Subp))
7090 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7094 Style.Missing_Overriding (Decl, Subp);
7098 elsif Must_Override (Spec) then
7099 if Ekind (Subp) = E_Entry then
7100 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7102 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7105 -- If the operation is marked "not overriding" and it's not primitive
7106 -- then an error is issued, unless this is an operation of a task or
7107 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7108 -- has been specified have already been checked above.
7110 elsif Must_Not_Override (Spec)
7111 and then not Is_Primitive
7112 and then Ekind (Subp) /= E_Entry
7113 and then Ekind (Scope (Subp)) /= E_Protected_Type
7116 ("overriding indicator only allowed if subprogram is primitive",
7120 end Check_Overriding_Indicator;
7126 -- Note: this procedure needs to know far too much about how the expander
7127 -- messes with exceptions. The use of the flag Exception_Junk and the
7128 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7129 -- works, but is not very clean. It would be better if the expansion
7130 -- routines would leave Original_Node working nicely, and we could use
7131 -- Original_Node here to ignore all the peculiar expander messing ???
7133 procedure Check_Returns
7137 Proc : Entity_Id := Empty)
7141 procedure Check_Statement_Sequence (L : List_Id);
7142 -- Internal recursive procedure to check a list of statements for proper
7143 -- termination by a return statement (or a transfer of control or a
7144 -- compound statement that is itself internally properly terminated).
7146 ------------------------------
7147 -- Check_Statement_Sequence --
7148 ------------------------------
7150 procedure Check_Statement_Sequence (L : List_Id) is
7155 function Assert_False return Boolean;
7156 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7157 -- rewritten as a null statement when assertions are off. The assert
7158 -- is not active, but it is still enough to kill the warning.
7164 function Assert_False return Boolean is
7165 Orig : constant Node_Id := Original_Node (Last_Stm);
7168 if Nkind (Orig) = N_Pragma
7169 and then Pragma_Name (Orig) = Name_Assert
7170 and then not Error_Posted (Orig)
7173 Arg : constant Node_Id :=
7174 First (Pragma_Argument_Associations (Orig));
7175 Exp : constant Node_Id := Expression (Arg);
7177 return Nkind (Exp) = N_Identifier
7178 and then Chars (Exp) = Name_False;
7188 Raise_Exception_Call : Boolean;
7189 -- Set True if statement sequence terminated by Raise_Exception call
7190 -- or a Reraise_Occurrence call.
7192 -- Start of processing for Check_Statement_Sequence
7195 Raise_Exception_Call := False;
7197 -- Get last real statement
7199 Last_Stm := Last (L);
7201 -- Deal with digging out exception handler statement sequences that
7202 -- have been transformed by the local raise to goto optimization.
7203 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7204 -- optimization has occurred, we are looking at something like:
7207 -- original stmts in block
7211 -- goto L1; | omitted if No_Exception_Propagation
7216 -- goto L3; -- skip handler when exception not raised
7218 -- <<L1>> -- target label for local exception
7232 -- and what we have to do is to dig out the estmts1 and estmts2
7233 -- sequences (which were the original sequences of statements in
7234 -- the exception handlers) and check them.
7236 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7241 exit when Nkind (Stm) /= N_Block_Statement;
7242 exit when not Exception_Junk (Stm);
7245 exit when Nkind (Stm) /= N_Label;
7246 exit when not Exception_Junk (Stm);
7247 Check_Statement_Sequence
7248 (Statements (Handled_Statement_Sequence (Next (Stm))));
7253 exit when Nkind (Stm) /= N_Goto_Statement;
7254 exit when not Exception_Junk (Stm);
7258 -- Don't count pragmas
7260 while Nkind (Last_Stm) = N_Pragma
7262 -- Don't count call to SS_Release (can happen after Raise_Exception)
7265 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7267 Nkind (Name (Last_Stm)) = N_Identifier
7269 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7271 -- Don't count exception junk
7274 (Nkind (Last_Stm) in
7275 N_Goto_Statement | N_Label | N_Object_Declaration
7276 and then Exception_Junk (Last_Stm))
7277 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7279 -- Inserted code, such as finalization calls, is irrelevant: we only
7280 -- need to check original source.
7282 or else Is_Rewrite_Insertion (Last_Stm)
7287 -- Here we have the "real" last statement
7289 Kind := Nkind (Last_Stm);
7291 -- Transfer of control, OK. Note that in the No_Return procedure
7292 -- case, we already diagnosed any explicit return statements, so
7293 -- we can treat them as OK in this context.
7295 if Is_Transfer (Last_Stm) then
7298 -- Check cases of explicit non-indirect procedure calls
7300 elsif Kind = N_Procedure_Call_Statement
7301 and then Is_Entity_Name (Name (Last_Stm))
7303 -- Check call to Raise_Exception procedure which is treated
7304 -- specially, as is a call to Reraise_Occurrence.
7306 -- We suppress the warning in these cases since it is likely that
7307 -- the programmer really does not expect to deal with the case
7308 -- of Null_Occurrence, and thus would find a warning about a
7309 -- missing return curious, and raising Program_Error does not
7310 -- seem such a bad behavior if this does occur.
7312 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7313 -- behavior will be to raise Constraint_Error (see AI-329).
7315 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7317 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7319 Raise_Exception_Call := True;
7321 -- For Raise_Exception call, test first argument, if it is
7322 -- an attribute reference for a 'Identity call, then we know
7323 -- that the call cannot possibly return.
7326 Arg : constant Node_Id :=
7327 Original_Node (First_Actual (Last_Stm));
7329 if Nkind (Arg) = N_Attribute_Reference
7330 and then Attribute_Name (Arg) = Name_Identity
7337 -- If statement, need to look inside if there is an else and check
7338 -- each constituent statement sequence for proper termination.
7340 elsif Kind = N_If_Statement
7341 and then Present (Else_Statements (Last_Stm))
7343 Check_Statement_Sequence (Then_Statements (Last_Stm));
7344 Check_Statement_Sequence (Else_Statements (Last_Stm));
7346 if Present (Elsif_Parts (Last_Stm)) then
7348 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7351 while Present (Elsif_Part) loop
7352 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7360 -- Case statement, check each case for proper termination
7362 elsif Kind = N_Case_Statement then
7366 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7367 while Present (Case_Alt) loop
7368 Check_Statement_Sequence (Statements (Case_Alt));
7369 Next_Non_Pragma (Case_Alt);
7375 -- Block statement, check its handled sequence of statements
7377 elsif Kind = N_Block_Statement then
7383 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7392 -- Loop statement. If there is an iteration scheme, we can definitely
7393 -- fall out of the loop. Similarly if there is an exit statement, we
7394 -- can fall out. In either case we need a following return.
7396 elsif Kind = N_Loop_Statement then
7397 if Present (Iteration_Scheme (Last_Stm))
7398 or else Has_Exit (Entity (Identifier (Last_Stm)))
7402 -- A loop with no exit statement or iteration scheme is either
7403 -- an infinite loop, or it has some other exit (raise/return).
7404 -- In either case, no warning is required.
7410 -- Timed entry call, check entry call and delay alternatives
7412 -- Note: in expanded code, the timed entry call has been converted
7413 -- to a set of expanded statements on which the check will work
7414 -- correctly in any case.
7416 elsif Kind = N_Timed_Entry_Call then
7418 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7419 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7422 -- If statement sequence of entry call alternative is missing,
7423 -- then we can definitely fall through, and we post the error
7424 -- message on the entry call alternative itself.
7426 if No (Statements (ECA)) then
7429 -- If statement sequence of delay alternative is missing, then
7430 -- we can definitely fall through, and we post the error
7431 -- message on the delay alternative itself.
7433 -- Note: if both ECA and DCA are missing the return, then we
7434 -- post only one message, should be enough to fix the bugs.
7435 -- If not we will get a message next time on the DCA when the
7438 elsif No (Statements (DCA)) then
7441 -- Else check both statement sequences
7444 Check_Statement_Sequence (Statements (ECA));
7445 Check_Statement_Sequence (Statements (DCA));
7450 -- Conditional entry call, check entry call and else part
7452 -- Note: in expanded code, the conditional entry call has been
7453 -- converted to a set of expanded statements on which the check
7454 -- will work correctly in any case.
7456 elsif Kind = N_Conditional_Entry_Call then
7458 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7461 -- If statement sequence of entry call alternative is missing,
7462 -- then we can definitely fall through, and we post the error
7463 -- message on the entry call alternative itself.
7465 if No (Statements (ECA)) then
7468 -- Else check statement sequence and else part
7471 Check_Statement_Sequence (Statements (ECA));
7472 Check_Statement_Sequence (Else_Statements (Last_Stm));
7478 -- If we fall through, issue appropriate message
7482 -- Kill warning if last statement is a raise exception call,
7483 -- or a pragma Assert (False). Note that with assertions enabled,
7484 -- such a pragma has been converted into a raise exception call
7485 -- already, so the Assert_False is for the assertions off case.
7487 if not Raise_Exception_Call and then not Assert_False then
7489 -- In GNATprove mode, it is an error to have a missing return
7491 Error_Msg_Warn := SPARK_Mode /= On;
7493 -- Issue error message or warning
7496 ("RETURN statement missing following this statement<<!",
7499 ("\Program_Error ]<<!", Last_Stm);
7502 -- Note: we set Err even though we have not issued a warning
7503 -- because we still have a case of a missing return. This is
7504 -- an extremely marginal case, probably will never be noticed
7505 -- but we might as well get it right.
7509 -- Otherwise we have the case of a procedure marked No_Return
7512 if not Raise_Exception_Call then
7513 if GNATprove_Mode then
7515 ("implied return after this statement would have raised "
7516 & "Program_Error", Last_Stm);
7518 -- In normal compilation mode, do not warn on a generated call
7519 -- (e.g. in the body of a renaming as completion).
7521 elsif Comes_From_Source (Last_Stm) then
7523 ("implied return after this statement will raise "
7524 & "Program_Error??", Last_Stm);
7527 Error_Msg_Warn := SPARK_Mode /= On;
7529 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7533 RE : constant Node_Id :=
7534 Make_Raise_Program_Error (Sloc (Last_Stm),
7535 Reason => PE_Implicit_Return);
7537 Insert_After (Last_Stm, RE);
7541 end Check_Statement_Sequence;
7543 -- Start of processing for Check_Returns
7547 Check_Statement_Sequence (Statements (HSS));
7549 if Present (Exception_Handlers (HSS)) then
7550 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7551 while Present (Handler) loop
7552 Check_Statement_Sequence (Statements (Handler));
7553 Next_Non_Pragma (Handler);
7558 ----------------------------
7559 -- Check_Subprogram_Order --
7560 ----------------------------
7562 procedure Check_Subprogram_Order (N : Node_Id) is
7564 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7565 -- This is used to check if S1 > S2 in the sense required by this test,
7566 -- for example nameab < namec, but name2 < name10.
7568 -----------------------------
7569 -- Subprogram_Name_Greater --
7570 -----------------------------
7572 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7577 -- Deal with special case where names are identical except for a
7578 -- numerical suffix. These are handled specially, taking the numeric
7579 -- ordering from the suffix into account.
7582 while S1 (L1) in '0' .. '9' loop
7587 while S2 (L2) in '0' .. '9' loop
7591 -- If non-numeric parts non-equal, do straight compare
7593 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7596 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7597 -- that a missing suffix is treated as numeric zero in this test.
7601 while L1 < S1'Last loop
7603 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7607 while L2 < S2'Last loop
7609 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7614 end Subprogram_Name_Greater;
7616 -- Start of processing for Check_Subprogram_Order
7619 -- Check body in alpha order if this is option
7622 and then Style_Check_Order_Subprograms
7623 and then Nkind (N) = N_Subprogram_Body
7624 and then Comes_From_Source (N)
7625 and then In_Extended_Main_Source_Unit (N)
7629 renames Scope_Stack.Table
7630 (Scope_Stack.Last).Last_Subprogram_Name;
7632 Body_Id : constant Entity_Id :=
7633 Defining_Entity (Specification (N));
7636 Get_Decoded_Name_String (Chars (Body_Id));
7639 if Subprogram_Name_Greater
7640 (LSN.all, Name_Buffer (1 .. Name_Len))
7642 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7648 LSN := new String'(Name_Buffer (1 .. Name_Len));
7651 end Check_Subprogram_Order;
7653 ------------------------------
7654 -- Check_Subtype_Conformant --
7655 ------------------------------
7657 procedure Check_Subtype_Conformant
7658 (New_Id : Entity_Id;
7660 Err_Loc : Node_Id := Empty;
7661 Skip_Controlling_Formals : Boolean := False;
7662 Get_Inst : Boolean := False)
7665 pragma Warnings (Off, Result);
7668 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7669 Skip_Controlling_Formals => Skip_Controlling_Formals,
7670 Get_Inst => Get_Inst);
7671 end Check_Subtype_Conformant;
7673 -----------------------------------
7674 -- Check_Synchronized_Overriding --
7675 -----------------------------------
7677 procedure Check_Synchronized_Overriding
7678 (Def_Id : Entity_Id;
7679 Overridden_Subp : out Entity_Id)
7681 Ifaces_List : Elist_Id;
7685 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7686 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7687 -- subprogram is implemented by a protected procedure or entry,
7688 -- its first parameter must be out, in out, or access-to-variable.
7690 function Matches_Prefixed_View_Profile
7691 (Prim_Params : List_Id;
7692 Iface_Params : List_Id) return Boolean;
7693 -- Determine whether a subprogram's parameter profile Prim_Params
7694 -- matches that of a potentially overridden interface subprogram
7695 -- Iface_Params. Also determine if the type of first parameter of
7696 -- Iface_Params is an implemented interface.
7698 ----------------------
7699 -- Is_Valid_Formal --
7700 ----------------------
7702 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7705 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7707 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7708 and then not Constant_Present (Parameter_Type (Parent (F))));
7709 end Is_Valid_Formal;
7711 -----------------------------------
7712 -- Matches_Prefixed_View_Profile --
7713 -----------------------------------
7715 function Matches_Prefixed_View_Profile
7716 (Prim_Params : List_Id;
7717 Iface_Params : List_Id) return Boolean
7719 function Is_Implemented
7720 (Ifaces_List : Elist_Id;
7721 Iface : Entity_Id) return Boolean;
7722 -- Determine if Iface is implemented by the current task or
7725 --------------------
7726 -- Is_Implemented --
7727 --------------------
7729 function Is_Implemented
7730 (Ifaces_List : Elist_Id;
7731 Iface : Entity_Id) return Boolean
7733 Iface_Elmt : Elmt_Id;
7736 Iface_Elmt := First_Elmt (Ifaces_List);
7737 while Present (Iface_Elmt) loop
7738 if Node (Iface_Elmt) = Iface then
7742 Next_Elmt (Iface_Elmt);
7750 Iface_Id : Entity_Id;
7751 Iface_Param : Node_Id;
7752 Iface_Typ : Entity_Id;
7753 Prim_Id : Entity_Id;
7754 Prim_Param : Node_Id;
7755 Prim_Typ : Entity_Id;
7757 -- Start of processing for Matches_Prefixed_View_Profile
7760 Iface_Param := First (Iface_Params);
7761 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7763 if Is_Access_Type (Iface_Typ) then
7764 Iface_Typ := Designated_Type (Iface_Typ);
7767 Prim_Param := First (Prim_Params);
7769 -- The first parameter of the potentially overridden subprogram must
7770 -- be an interface implemented by Prim.
7772 if not Is_Interface (Iface_Typ)
7773 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7778 -- The checks on the object parameters are done, so move on to the
7779 -- rest of the parameters.
7781 if not In_Scope then
7786 while Present (Iface_Param) and then Present (Prim_Param) loop
7787 Iface_Id := Defining_Identifier (Iface_Param);
7788 Iface_Typ := Find_Parameter_Type (Iface_Param);
7790 Prim_Id := Defining_Identifier (Prim_Param);
7791 Prim_Typ := Find_Parameter_Type (Prim_Param);
7793 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7794 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7795 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7797 Iface_Typ := Designated_Type (Iface_Typ);
7798 Prim_Typ := Designated_Type (Prim_Typ);
7801 -- Case of multiple interface types inside a parameter profile
7803 -- (Obj_Param : in out Iface; ...; Param : Iface)
7805 -- If the interface type is implemented, then the matching type in
7806 -- the primitive should be the implementing record type.
7808 if Ekind (Iface_Typ) = E_Record_Type
7809 and then Is_Interface (Iface_Typ)
7810 and then Is_Implemented (Ifaces_List, Iface_Typ)
7812 if Prim_Typ /= Typ then
7816 -- The two parameters must be both mode and subtype conformant
7818 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7820 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7829 -- One of the two lists contains more parameters than the other
7831 if Present (Iface_Param) or else Present (Prim_Param) then
7836 end Matches_Prefixed_View_Profile;
7838 -- Start of processing for Check_Synchronized_Overriding
7841 Overridden_Subp := Empty;
7843 -- Def_Id must be an entry or a subprogram. We should skip predefined
7844 -- primitives internally generated by the front end; however at this
7845 -- stage predefined primitives are still not fully decorated. As a
7846 -- minor optimization we skip here internally generated subprograms.
7848 if (Ekind (Def_Id) /= E_Entry
7849 and then Ekind (Def_Id) /= E_Function
7850 and then Ekind (Def_Id) /= E_Procedure)
7851 or else not Comes_From_Source (Def_Id)
7856 -- Search for the concurrent declaration since it contains the list of
7857 -- all implemented interfaces. In this case, the subprogram is declared
7858 -- within the scope of a protected or a task type.
7860 if Present (Scope (Def_Id))
7861 and then Is_Concurrent_Type (Scope (Def_Id))
7862 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7864 Typ := Scope (Def_Id);
7867 -- The enclosing scope is not a synchronized type and the subprogram
7870 elsif No (First_Formal (Def_Id)) then
7873 -- The subprogram has formals and hence it may be a primitive of a
7877 Typ := Etype (First_Formal (Def_Id));
7879 if Is_Access_Type (Typ) then
7880 Typ := Directly_Designated_Type (Typ);
7883 if Is_Concurrent_Type (Typ)
7884 and then not Is_Generic_Actual_Type (Typ)
7888 -- This case occurs when the concurrent type is declared within a
7889 -- generic unit. As a result the corresponding record has been built
7890 -- and used as the type of the first formal, we just have to retrieve
7891 -- the corresponding concurrent type.
7893 elsif Is_Concurrent_Record_Type (Typ)
7894 and then not Is_Class_Wide_Type (Typ)
7895 and then Present (Corresponding_Concurrent_Type (Typ))
7897 Typ := Corresponding_Concurrent_Type (Typ);
7905 -- There is no overriding to check if this is an inherited operation in
7906 -- a type derivation for a generic actual.
7908 Collect_Interfaces (Typ, Ifaces_List);
7910 if Is_Empty_Elmt_List (Ifaces_List) then
7914 -- Determine whether entry or subprogram Def_Id overrides a primitive
7915 -- operation that belongs to one of the interfaces in Ifaces_List.
7918 Candidate : Entity_Id := Empty;
7919 Hom : Entity_Id := Empty;
7920 Subp : Entity_Id := Empty;
7923 -- Traverse the homonym chain, looking for a potentially overridden
7924 -- subprogram that belongs to an implemented interface.
7926 Hom := Current_Entity_In_Scope (Def_Id);
7927 while Present (Hom) loop
7931 or else not Is_Overloadable (Subp)
7932 or else not Is_Primitive (Subp)
7933 or else not Is_Dispatching_Operation (Subp)
7934 or else not Present (Find_Dispatching_Type (Subp))
7935 or else not Is_Interface (Find_Dispatching_Type (Subp))
7939 -- Entries and procedures can override abstract or null interface
7942 elsif Ekind (Def_Id) in E_Entry | E_Procedure
7943 and then Ekind (Subp) = E_Procedure
7944 and then Matches_Prefixed_View_Profile
7945 (Parameter_Specifications (Parent (Def_Id)),
7946 Parameter_Specifications (Parent (Subp)))
7950 -- For an overridden subprogram Subp, check whether the mode
7951 -- of its first parameter is correct depending on the kind of
7952 -- synchronized type.
7955 Formal : constant Node_Id := First_Formal (Candidate);
7958 -- In order for an entry or a protected procedure to
7959 -- override, the first parameter of the overridden routine
7960 -- must be of mode "out", "in out", or access-to-variable.
7962 if Ekind (Candidate) in E_Entry | E_Procedure
7963 and then Is_Protected_Type (Typ)
7964 and then not Is_Valid_Formal (Formal)
7968 -- All other cases are OK since a task entry or routine does
7969 -- not have a restriction on the mode of the first parameter
7970 -- of the overridden interface routine.
7973 Overridden_Subp := Candidate;
7978 -- Functions can override abstract interface functions. Return
7979 -- types must be subtype conformant.
7981 elsif Ekind (Def_Id) = E_Function
7982 and then Ekind (Subp) = E_Function
7983 and then Matches_Prefixed_View_Profile
7984 (Parameter_Specifications (Parent (Def_Id)),
7985 Parameter_Specifications (Parent (Subp)))
7986 and then Conforming_Types
7987 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7991 -- If an inherited subprogram is implemented by a protected
7992 -- function, then the first parameter of the inherited
7993 -- subprogram shall be of mode in, but not an access-to-
7994 -- variable parameter (RM 9.4(11/9)).
7996 if Present (First_Formal (Subp))
7997 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7999 (not Is_Access_Type (Etype (First_Formal (Subp)))
8001 Is_Access_Constant (Etype (First_Formal (Subp))))
8003 Overridden_Subp := Subp;
8008 Hom := Homonym (Hom);
8011 -- After examining all candidates for overriding, we are left with
8012 -- the best match, which is a mode-incompatible interface routine.
8014 if In_Scope and then Present (Candidate) then
8015 Error_Msg_PT (Def_Id, Candidate);
8018 Overridden_Subp := Candidate;
8021 end Check_Synchronized_Overriding;
8023 ---------------------------
8024 -- Check_Type_Conformant --
8025 ---------------------------
8027 procedure Check_Type_Conformant
8028 (New_Id : Entity_Id;
8030 Err_Loc : Node_Id := Empty)
8033 pragma Warnings (Off, Result);
8036 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
8037 end Check_Type_Conformant;
8039 ---------------------------
8040 -- Can_Override_Operator --
8041 ---------------------------
8043 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8047 if Nkind (Subp) /= N_Defining_Operator_Symbol then
8051 Typ := Base_Type (Etype (First_Formal (Subp)));
8053 -- Check explicitly that the operation is a primitive of the type
8055 return Operator_Matches_Spec (Subp, Subp)
8056 and then not Is_Generic_Type (Typ)
8057 and then Scope (Subp) = Scope (Typ)
8058 and then not Is_Class_Wide_Type (Typ);
8060 end Can_Override_Operator;
8062 ----------------------
8063 -- Conforming_Types --
8064 ----------------------
8066 function Conforming_Types
8069 Ctype : Conformance_Type;
8070 Get_Inst : Boolean := False) return Boolean
8072 function Base_Types_Match
8074 Typ_2 : Entity_Id) return Boolean;
8075 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8076 -- in different scopes (e.g. parent and child instances), then verify
8077 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8078 -- the same subtype chain. The whole purpose of this procedure is to
8079 -- prevent spurious ambiguities in an instantiation that may arise if
8080 -- two distinct generic types are instantiated with the same actual.
8082 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8083 -- An access parameter can designate an incomplete type. If the
8084 -- incomplete type is the limited view of a type from a limited_
8085 -- with_clause, check whether the non-limited view is available.
8086 -- If it is a (non-limited) incomplete type, get the full view.
8088 function Matches_Limited_With_View
8090 Typ_2 : Entity_Id) return Boolean;
8091 -- Returns True if and only if either Typ_1 denotes a limited view of
8092 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8093 -- the limited with view of a type is used in a subprogram declaration
8094 -- and the subprogram body is in the scope of a regular with clause for
8095 -- the same unit. In such a case, the two type entities are considered
8096 -- identical for purposes of conformance checking.
8098 ----------------------
8099 -- Base_Types_Match --
8100 ----------------------
8102 function Base_Types_Match
8104 Typ_2 : Entity_Id) return Boolean
8106 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8107 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8110 if Typ_1 = Typ_2 then
8113 elsif Base_1 = Base_2 then
8115 -- The following is too permissive. A more precise test should
8116 -- check that the generic actual is an ancestor subtype of the
8119 -- See code in Find_Corresponding_Spec that applies an additional
8120 -- filter to handle accidental amiguities in instances.
8123 not Is_Generic_Actual_Type (Typ_1)
8124 or else not Is_Generic_Actual_Type (Typ_2)
8125 or else Scope (Typ_1) /= Scope (Typ_2);
8127 -- If Typ_2 is a generic actual type it is declared as the subtype of
8128 -- the actual. If that actual is itself a subtype we need to use its
8129 -- own base type to check for compatibility.
8131 elsif Ekind (Base_2) = Ekind (Typ_2)
8132 and then Base_1 = Base_Type (Base_2)
8136 elsif Ekind (Base_1) = Ekind (Typ_1)
8137 and then Base_2 = Base_Type (Base_1)
8144 end Base_Types_Match;
8146 --------------------------
8147 -- Find_Designated_Type --
8148 --------------------------
8150 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8154 Desig := Directly_Designated_Type (Typ);
8156 if Ekind (Desig) = E_Incomplete_Type then
8158 -- If regular incomplete type, get full view if available
8160 if Present (Full_View (Desig)) then
8161 Desig := Full_View (Desig);
8163 -- If limited view of a type, get non-limited view if available,
8164 -- and check again for a regular incomplete type.
8166 elsif Present (Non_Limited_View (Desig)) then
8167 Desig := Get_Full_View (Non_Limited_View (Desig));
8172 end Find_Designated_Type;
8174 -------------------------------
8175 -- Matches_Limited_With_View --
8176 -------------------------------
8178 function Matches_Limited_With_View
8180 Typ_2 : Entity_Id) return Boolean
8182 function Is_Matching_Limited_View
8184 View : Entity_Id) return Boolean;
8185 -- Determine whether non-limited view View denotes type Typ in some
8186 -- conformant fashion.
8188 ------------------------------
8189 -- Is_Matching_Limited_View --
8190 ------------------------------
8192 function Is_Matching_Limited_View
8194 View : Entity_Id) return Boolean
8196 Root_Typ : Entity_Id;
8197 Root_View : Entity_Id;
8200 -- The non-limited view directly denotes the type
8205 -- The type is a subtype of the non-limited view
8207 elsif Is_Subtype_Of (Typ, View) then
8210 -- Both the non-limited view and the type denote class-wide types
8212 elsif Is_Class_Wide_Type (Typ)
8213 and then Is_Class_Wide_Type (View)
8215 Root_Typ := Root_Type (Typ);
8216 Root_View := Root_Type (View);
8218 if Root_Typ = Root_View then
8221 -- An incomplete tagged type and its full view may receive two
8222 -- distinct class-wide types when the related package has not
8223 -- been analyzed yet.
8226 -- type T is tagged; -- CW_1
8227 -- type T is tagged null record; -- CW_2
8230 -- This is because the package lacks any semantic information
8231 -- that may eventually link both views of T. As a consequence,
8232 -- a client of the limited view of Pack will see CW_2 while a
8233 -- client of the non-limited view of Pack will see CW_1.
8235 elsif Is_Incomplete_Type (Root_Typ)
8236 and then Present (Full_View (Root_Typ))
8237 and then Full_View (Root_Typ) = Root_View
8241 elsif Is_Incomplete_Type (Root_View)
8242 and then Present (Full_View (Root_View))
8243 and then Full_View (Root_View) = Root_Typ
8250 end Is_Matching_Limited_View;
8252 -- Start of processing for Matches_Limited_With_View
8255 -- In some cases a type imported through a limited_with clause, and
8256 -- its non-limited view are both visible, for example in an anonymous
8257 -- access-to-class-wide type in a formal, or when building the body
8258 -- for a subprogram renaming after the subprogram has been frozen.
8259 -- In these cases both entities designate the same type. In addition,
8260 -- if one of them is an actual in an instance, it may be a subtype of
8261 -- the non-limited view of the other.
8263 if From_Limited_With (Typ_1)
8264 and then From_Limited_With (Typ_2)
8265 and then Available_View (Typ_1) = Available_View (Typ_2)
8269 elsif From_Limited_With (Typ_1) then
8270 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8272 elsif From_Limited_With (Typ_2) then
8273 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8278 end Matches_Limited_With_View;
8282 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8284 Type_1 : Entity_Id := T1;
8285 Type_2 : Entity_Id := T2;
8287 -- Start of processing for Conforming_Types
8290 -- The context is an instance association for a formal access-to-
8291 -- subprogram type; the formal parameter types require mapping because
8292 -- they may denote other formal parameters of the generic unit.
8295 Type_1 := Get_Instance_Of (T1);
8296 Type_2 := Get_Instance_Of (T2);
8299 -- If one of the types is a view of the other introduced by a limited
8300 -- with clause, treat these as conforming for all purposes.
8302 if Matches_Limited_With_View (T1, T2) then
8305 elsif Base_Types_Match (Type_1, Type_2) then
8306 if Ctype <= Mode_Conformant then
8311 Subtypes_Statically_Match (Type_1, Type_2)
8312 and then Dimensions_Match (Type_1, Type_2);
8315 elsif Is_Incomplete_Or_Private_Type (Type_1)
8316 and then Present (Full_View (Type_1))
8317 and then Base_Types_Match (Full_View (Type_1), Type_2)
8320 Ctype <= Mode_Conformant
8321 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8323 elsif Ekind (Type_2) = E_Incomplete_Type
8324 and then Present (Full_View (Type_2))
8325 and then Base_Types_Match (Type_1, Full_View (Type_2))
8328 Ctype <= Mode_Conformant
8329 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8331 elsif Is_Private_Type (Type_2)
8332 and then In_Instance
8333 and then Present (Full_View (Type_2))
8334 and then Base_Types_Match (Type_1, Full_View (Type_2))
8337 Ctype <= Mode_Conformant
8338 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8340 -- Another confusion between views in a nested instance with an
8341 -- actual private type whose full view is not in scope.
8343 elsif Ekind (Type_2) = E_Private_Subtype
8344 and then In_Instance
8345 and then Etype (Type_2) = Type_1
8349 -- In Ada 2012, incomplete types (including limited views) can appear
8350 -- as actuals in instantiations, where they are conformant to the
8351 -- corresponding incomplete formal.
8353 elsif Is_Incomplete_Type (Type_1)
8354 and then Is_Incomplete_Type (Type_2)
8355 and then In_Instance
8356 and then (Used_As_Generic_Actual (Type_1)
8357 or else Used_As_Generic_Actual (Type_2))
8362 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8363 -- treated recursively because they carry a signature. As far as
8364 -- conformance is concerned, convention plays no role, and either
8365 -- or both could be access to protected subprograms.
8367 Are_Anonymous_Access_To_Subprogram_Types :=
8368 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8369 | E_Anonymous_Access_Protected_Subprogram_Type
8371 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8372 | E_Anonymous_Access_Protected_Subprogram_Type;
8374 -- Test anonymous access type case. For this case, static subtype
8375 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8376 -- the base types because we may have built internal subtype entities
8377 -- to handle null-excluding types (see Process_Formals).
8379 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8381 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8383 -- Ada 2005 (AI-254)
8385 or else Are_Anonymous_Access_To_Subprogram_Types
8388 Desig_1 : Entity_Id;
8389 Desig_2 : Entity_Id;
8392 -- In Ada 2005, access constant indicators must match for
8393 -- subtype conformance.
8395 if Ada_Version >= Ada_2005
8396 and then Ctype >= Subtype_Conformant
8398 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8403 Desig_1 := Find_Designated_Type (Type_1);
8404 Desig_2 := Find_Designated_Type (Type_2);
8406 -- If the context is an instance association for a formal
8407 -- access-to-subprogram type; formal access parameter designated
8408 -- types require mapping because they may denote other formal
8409 -- parameters of the generic unit.
8412 Desig_1 := Get_Instance_Of (Desig_1);
8413 Desig_2 := Get_Instance_Of (Desig_2);
8416 -- It is possible for a Class_Wide_Type to be introduced for an
8417 -- incomplete type, in which case there is a separate class_ wide
8418 -- type for the full view. The types conform if their Etypes
8419 -- conform, i.e. one may be the full view of the other. This can
8420 -- only happen in the context of an access parameter, other uses
8421 -- of an incomplete Class_Wide_Type are illegal.
8423 if Is_Class_Wide_Type (Desig_1)
8425 Is_Class_Wide_Type (Desig_2)
8429 (Etype (Base_Type (Desig_1)),
8430 Etype (Base_Type (Desig_2)), Ctype);
8432 elsif Are_Anonymous_Access_To_Subprogram_Types then
8433 if Ada_Version < Ada_2005 then
8435 Ctype = Type_Conformant
8436 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8438 -- We must check the conformance of the signatures themselves
8442 Conformant : Boolean;
8445 (Desig_1, Desig_2, Ctype, False, Conformant);
8450 -- A limited view of an actual matches the corresponding
8451 -- incomplete formal.
8453 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8454 and then From_Limited_With (Desig_2)
8455 and then Used_As_Generic_Actual (Etype (Desig_2))
8460 return Base_Type (Desig_1) = Base_Type (Desig_2)
8461 and then (Ctype = Type_Conformant
8463 Subtypes_Statically_Match (Desig_1, Desig_2));
8467 -- Otherwise definitely no match
8470 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8471 and then Is_Access_Type (Type_2))
8472 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8473 and then Is_Access_Type (Type_1)))
8476 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8478 May_Hide_Profile := True;
8483 end Conforming_Types;
8485 --------------------------
8486 -- Create_Extra_Formals --
8487 --------------------------
8489 procedure Create_Extra_Formals (E : Entity_Id) is
8490 First_Extra : Entity_Id := Empty;
8492 Last_Extra : Entity_Id := Empty;
8494 function Add_Extra_Formal
8495 (Assoc_Entity : Entity_Id;
8498 Suffix : String) return Entity_Id;
8499 -- Add an extra formal to the current list of formals and extra formals.
8500 -- The extra formal is added to the end of the list of extra formals,
8501 -- and also returned as the result. These formals are always of mode IN.
8502 -- The new formal has the type Typ, is declared in Scope, and its name
8503 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8504 -- The following suffixes are currently used. They should not be changed
8505 -- without coordinating with CodePeer, which makes use of these to
8506 -- provide better messages.
8508 -- O denotes the Constrained bit.
8509 -- L denotes the accessibility level.
8510 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8511 -- the full list in exp_ch6.BIP_Formal_Kind.
8513 ----------------------
8514 -- Add_Extra_Formal --
8515 ----------------------
8517 function Add_Extra_Formal
8518 (Assoc_Entity : Entity_Id;
8521 Suffix : String) return Entity_Id
8523 EF : constant Entity_Id :=
8524 Make_Defining_Identifier (Sloc (Assoc_Entity),
8525 Chars => New_External_Name (Chars (Assoc_Entity),
8529 -- A little optimization. Never generate an extra formal for the
8530 -- _init operand of an initialization procedure, since it could
8533 if Chars (Formal) = Name_uInit then
8537 Set_Ekind (EF, E_In_Parameter);
8538 Set_Actual_Subtype (EF, Typ);
8539 Set_Etype (EF, Typ);
8540 Set_Scope (EF, Scope);
8541 Set_Mechanism (EF, Default_Mechanism);
8542 Set_Formal_Validity (EF);
8544 if No (First_Extra) then
8546 Set_Extra_Formals (Scope, EF);
8549 if Present (Last_Extra) then
8550 Set_Extra_Formal (Last_Extra, EF);
8556 end Add_Extra_Formal;
8560 Formal_Type : Entity_Id;
8561 P_Formal : Entity_Id := Empty;
8563 -- Start of processing for Create_Extra_Formals
8566 -- We never generate extra formals if expansion is not active because we
8567 -- don't need them unless we are generating code.
8569 if not Expander_Active then
8573 -- No need to generate extra formals in interface thunks whose target
8574 -- primitive has no extra formals.
8576 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8580 -- If this is a derived subprogram then the subtypes of the parent
8581 -- subprogram's formal parameters will be used to determine the need
8582 -- for extra formals.
8584 if Is_Overloadable (E) and then Present (Alias (E)) then
8585 P_Formal := First_Formal (Alias (E));
8588 Formal := First_Formal (E);
8589 while Present (Formal) loop
8590 Last_Extra := Formal;
8591 Next_Formal (Formal);
8594 -- If Extra_Formals were already created, don't do it again. This
8595 -- situation may arise for subprogram types created as part of
8596 -- dispatching calls (see Expand_Dispatching_Call).
8598 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8602 -- If the subprogram is a predefined dispatching subprogram then don't
8603 -- generate any extra constrained or accessibility level formals. In
8604 -- general we suppress these for internal subprograms (by not calling
8605 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8606 -- generated stream attributes do get passed through because extra
8607 -- build-in-place formals are needed in some cases (limited 'Input).
8609 if Is_Predefined_Internal_Operation (E) then
8610 goto Test_For_Func_Result_Extras;
8613 Formal := First_Formal (E);
8614 while Present (Formal) loop
8616 -- Create extra formal for supporting the attribute 'Constrained.
8617 -- The case of a private type view without discriminants also
8618 -- requires the extra formal if the underlying type has defaulted
8621 if Ekind (Formal) /= E_In_Parameter then
8622 if Present (P_Formal) then
8623 Formal_Type := Etype (P_Formal);
8625 Formal_Type := Etype (Formal);
8628 -- Do not produce extra formals for Unchecked_Union parameters.
8629 -- Jump directly to the end of the loop.
8631 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8632 goto Skip_Extra_Formal_Generation;
8635 if not Has_Discriminants (Formal_Type)
8636 and then Ekind (Formal_Type) in Private_Kind
8637 and then Present (Underlying_Type (Formal_Type))
8639 Formal_Type := Underlying_Type (Formal_Type);
8642 -- Suppress the extra formal if formal's subtype is constrained or
8643 -- indefinite, or we're compiling for Ada 2012 and the underlying
8644 -- type is tagged and limited. In Ada 2012, a limited tagged type
8645 -- can have defaulted discriminants, but 'Constrained is required
8646 -- to return True, so the formal is never needed (see AI05-0214).
8647 -- Note that this ensures consistency of calling sequences for
8648 -- dispatching operations when some types in a class have defaults
8649 -- on discriminants and others do not (and requiring the extra
8650 -- formal would introduce distributed overhead).
8652 -- If the type does not have a completion yet, treat as prior to
8653 -- Ada 2012 for consistency.
8655 if Has_Discriminants (Formal_Type)
8656 and then not Is_Constrained (Formal_Type)
8657 and then Is_Definite_Subtype (Formal_Type)
8658 and then (Ada_Version < Ada_2012
8659 or else No (Underlying_Type (Formal_Type))
8661 (Is_Limited_Type (Formal_Type)
8664 (Underlying_Type (Formal_Type)))))
8666 Set_Extra_Constrained
8667 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8671 -- Create extra formal for supporting accessibility checking. This
8672 -- is done for both anonymous access formals and formals of named
8673 -- access types that are marked as controlling formals. The latter
8674 -- case can occur when Expand_Dispatching_Call creates a subprogram
8675 -- type and substitutes the types of access-to-class-wide actuals
8676 -- for the anonymous access-to-specific-type of controlling formals.
8677 -- Base_Type is applied because in cases where there is a null
8678 -- exclusion the formal may have an access subtype.
8680 -- This is suppressed if we specifically suppress accessibility
8681 -- checks at the package level for either the subprogram, or the
8682 -- package in which it resides. However, we do not suppress it
8683 -- simply if the scope has accessibility checks suppressed, since
8684 -- this could cause trouble when clients are compiled with a
8685 -- different suppression setting. The explicit checks at the
8686 -- package level are safe from this point of view.
8688 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8689 or else (Is_Controlling_Formal (Formal)
8690 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8692 (Explicit_Suppress (E, Accessibility_Check)
8694 Explicit_Suppress (Scope (E), Accessibility_Check))
8697 or else Present (Extra_Accessibility (P_Formal)))
8699 Set_Extra_Accessibility
8700 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8703 -- This label is required when skipping extra formal generation for
8704 -- Unchecked_Union parameters.
8706 <<Skip_Extra_Formal_Generation>>
8708 if Present (P_Formal) then
8709 Next_Formal (P_Formal);
8712 Next_Formal (Formal);
8715 <<Test_For_Func_Result_Extras>>
8717 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8718 -- function call is ... determined by the point of call ...".
8720 if Needs_Result_Accessibility_Level (E) then
8721 Set_Extra_Accessibility_Of_Result
8722 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8725 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8726 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8728 if Is_Build_In_Place_Function (E) then
8730 Result_Subt : constant Entity_Id := Etype (E);
8731 Formal_Typ : Entity_Id;
8732 Subp_Decl : Node_Id;
8733 Discard : Entity_Id;
8736 -- In the case of functions with unconstrained result subtypes,
8737 -- add a 4-state formal indicating whether the return object is
8738 -- allocated by the caller (1), or should be allocated by the
8739 -- callee on the secondary stack (2), in the global heap (3), or
8740 -- in a user-defined storage pool (4). For the moment we just use
8741 -- Natural for the type of this formal. Note that this formal
8742 -- isn't usually needed in the case where the result subtype is
8743 -- constrained, but it is needed when the function has a tagged
8744 -- result, because generally such functions can be called in a
8745 -- dispatching context and such calls must be handled like calls
8746 -- to a class-wide function.
8748 if Needs_BIP_Alloc_Form (E) then
8751 (E, Standard_Natural,
8752 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8754 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8755 -- use a user-defined pool. This formal is not added on
8756 -- ZFP as those targets do not support pools.
8758 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8761 (E, RTE (RE_Root_Storage_Pool_Ptr),
8762 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8766 -- In the case of functions whose result type needs finalization,
8767 -- add an extra formal which represents the finalization master.
8769 if Needs_BIP_Finalization_Master (E) then
8772 (E, RTE (RE_Finalization_Master_Ptr),
8773 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8776 -- When the result type contains tasks, add two extra formals: the
8777 -- master of the tasks to be created, and the caller's activation
8780 if Needs_BIP_Task_Actuals (E) then
8783 (E, RTE (RE_Master_Id),
8784 E, BIP_Formal_Suffix (BIP_Task_Master));
8786 Set_Has_Master_Entity (E);
8790 (E, RTE (RE_Activation_Chain_Access),
8791 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8794 -- All build-in-place functions get an extra formal that will be
8795 -- passed the address of the return object within the caller.
8798 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8800 -- Incomplete_View_From_Limited_With is needed here because
8801 -- gigi gets confused if the designated type is the full view
8802 -- coming from a limited-with'ed package. In the normal case,
8803 -- (no limited with) Incomplete_View_From_Limited_With
8804 -- returns Result_Subt.
8806 Set_Directly_Designated_Type
8807 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8808 Set_Etype (Formal_Typ, Formal_Typ);
8809 Set_Depends_On_Private
8810 (Formal_Typ, Has_Private_Component (Formal_Typ));
8811 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8812 Set_Is_Access_Constant (Formal_Typ, False);
8814 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8815 -- the designated type comes from the limited view (for back-end
8818 Set_From_Limited_With
8819 (Formal_Typ, From_Limited_With (Result_Subt));
8821 Layout_Type (Formal_Typ);
8823 -- Force the definition of the Itype in case of internal function
8824 -- calls within the same or nested scope.
8826 if Is_Subprogram_Or_Generic_Subprogram (E) then
8827 Subp_Decl := Parent (E);
8829 -- The insertion point for an Itype reference should be after
8830 -- the unit declaration node of the subprogram. An exception
8831 -- to this are inherited operations from a parent type in which
8832 -- case the derived type acts as their parent.
8834 if Nkind (Subp_Decl) in N_Function_Specification
8835 | N_Procedure_Specification
8837 Subp_Decl := Parent (Subp_Decl);
8840 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8845 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8849 -- If this is an instance of a generic, we need to have extra formals
8852 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8853 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8855 end Create_Extra_Formals;
8857 -----------------------------
8858 -- Enter_Overloaded_Entity --
8859 -----------------------------
8861 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8862 function Matches_Predefined_Op return Boolean;
8863 -- This returns an approximation of whether S matches a predefined
8864 -- operator, based on the operator symbol, and the parameter and result
8865 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8867 ---------------------------
8868 -- Matches_Predefined_Op --
8869 ---------------------------
8871 function Matches_Predefined_Op return Boolean is
8872 Formal_1 : constant Entity_Id := First_Formal (S);
8873 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8874 Op : constant Name_Id := Chars (S);
8875 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8876 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8881 if Present (Formal_2) then
8883 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8886 -- All but "&" and "**" have same-types parameters
8895 if Type_1 /= Type_2 then
8900 -- Check parameter and result types
8908 Is_Boolean_Type (Result_Type)
8909 and then Result_Type = Type_1;
8915 Is_Integer_Type (Result_Type)
8916 and then Result_Type = Type_1;
8924 Is_Numeric_Type (Result_Type)
8925 and then Result_Type = Type_1;
8931 Is_Boolean_Type (Result_Type)
8932 and then not Is_Limited_Type (Type_1);
8940 Is_Boolean_Type (Result_Type)
8941 and then (Is_Array_Type (Type_1)
8942 or else Is_Scalar_Type (Type_1));
8944 when Name_Op_Concat =>
8945 return Is_Array_Type (Result_Type);
8947 when Name_Op_Expon =>
8949 (Is_Integer_Type (Result_Type)
8950 or else Is_Floating_Point_Type (Result_Type))
8951 and then Result_Type = Type_1
8952 and then Type_2 = Standard_Integer;
8955 raise Program_Error;
8968 Is_Numeric_Type (Result_Type)
8969 and then Result_Type = Type_1;
8973 Is_Boolean_Type (Result_Type)
8974 and then Result_Type = Type_1;
8977 raise Program_Error;
8980 end Matches_Predefined_Op;
8984 E : Entity_Id := Current_Entity_In_Scope (S);
8985 C_E : Entity_Id := Current_Entity (S);
8987 -- Start of processing for Enter_Overloaded_Entity
8991 Set_Has_Homonym (E);
8992 Set_Has_Homonym (S);
8995 Set_Is_Immediately_Visible (S);
8996 Set_Scope (S, Current_Scope);
8998 -- Chain new entity if front of homonym in current scope, so that
8999 -- homonyms are contiguous.
9001 if Present (E) and then E /= C_E then
9002 while Homonym (C_E) /= E loop
9003 C_E := Homonym (C_E);
9006 Set_Homonym (C_E, S);
9010 Set_Current_Entity (S);
9015 if Is_Inherited_Operation (S) then
9016 Append_Inherited_Subprogram (S);
9018 Append_Entity (S, Current_Scope);
9021 Set_Public_Status (S);
9023 if Debug_Flag_E then
9024 Write_Str ("New overloaded entity chain: ");
9025 Write_Name (Chars (S));
9028 while Present (E) loop
9029 Write_Str (" "); Write_Int (Int (E));
9036 -- Generate warning for hiding
9039 and then Comes_From_Source (S)
9040 and then In_Extended_Main_Source_Unit (S)
9047 -- Warn unless genuine overloading. Do not emit warning on
9048 -- hiding predefined operators in Standard (these are either an
9049 -- artifact of our implicit declarations, or simple noise) but
9050 -- keep warning on a operator defined on a local subtype, because
9051 -- of the real danger that different operators may be applied in
9052 -- various parts of the program.
9054 -- Note that if E and S have the same scope, there is never any
9055 -- hiding. Either the two conflict, and the program is illegal,
9056 -- or S is overriding an implicit inherited subprogram.
9058 if Scope (E) /= Scope (S)
9059 and then (not Is_Overloadable (E)
9060 or else Subtype_Conformant (E, S))
9061 and then (Is_Immediately_Visible (E)
9062 or else Is_Potentially_Use_Visible (S))
9064 if Scope (E) = Standard_Standard then
9065 if Nkind (S) = N_Defining_Operator_Symbol
9066 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9068 and then Matches_Predefined_Op
9071 ("declaration of & hides predefined operator?h?", S);
9074 -- E not immediately within Standard
9077 Error_Msg_Sloc := Sloc (E);
9078 Error_Msg_N ("declaration of & hides one #?h?", S);
9083 end Enter_Overloaded_Entity;
9085 -----------------------------
9086 -- Check_Untagged_Equality --
9087 -----------------------------
9089 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
9090 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
9091 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
9095 -- This check applies only if we have a subprogram declaration with an
9096 -- untagged record type that is conformant to the predefined op.
9098 if Nkind (Decl) /= N_Subprogram_Declaration
9099 or else not Is_Record_Type (Typ)
9100 or else Is_Tagged_Type (Typ)
9101 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
9106 -- In Ada 2012 case, we will output errors or warnings depending on
9107 -- the setting of debug flag -gnatd.E.
9109 if Ada_Version >= Ada_2012 then
9110 Error_Msg_Warn := Debug_Flag_Dot_EE;
9112 -- In earlier versions of Ada, nothing to do unless we are warning on
9113 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
9116 if not Warn_On_Ada_2012_Compatibility then
9121 -- Cases where the type has already been frozen
9123 if Is_Frozen (Typ) then
9125 -- The check applies to a primitive operation, so check that type
9126 -- and equality operation are in the same scope.
9128 if Scope (Typ) /= Current_Scope then
9131 -- If the type is a generic actual (sub)type, the operation is not
9132 -- primitive either because the base type is declared elsewhere.
9134 elsif Is_Generic_Actual_Type (Typ) then
9137 -- Here we have a definite error of declaration after freezing
9140 if Ada_Version >= Ada_2012 then
9142 ("equality operator must be declared before type & is "
9143 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
9145 -- In Ada 2012 mode with error turned to warning, output one
9146 -- more warning to warn that the equality operation may not
9147 -- compose. This is the consequence of ignoring the error.
9149 if Error_Msg_Warn then
9150 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
9155 ("equality operator must be declared before type& is "
9156 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
9159 -- If we are in the package body, we could just move the
9160 -- declaration to the package spec, so add a message saying that.
9162 if In_Package_Body (Scope (Typ)) then
9163 if Ada_Version >= Ada_2012 then
9165 ("\move declaration to package spec<<", Eq_Op);
9168 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
9171 -- Otherwise try to find the freezing point for better message.
9174 Obj_Decl := Next (Parent (Typ));
9175 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9176 if Nkind (Obj_Decl) = N_Object_Declaration
9177 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9179 -- Freezing point, output warnings
9181 if Ada_Version >= Ada_2012 then
9183 ("type& is frozen by declaration??", Obj_Decl, Typ);
9185 ("\an equality operator cannot be declared after "
9190 ("type& is frozen by declaration (Ada 2012)?y?",
9193 ("\an equality operator cannot be declared after "
9194 & "this point (Ada 2012)?y?",
9200 -- If we reach generated code for subprogram declaration
9201 -- or body, it is the body that froze the type and the
9202 -- declaration is legal.
9204 elsif Sloc (Obj_Decl) = Sloc (Decl) then
9213 -- Here if type is not frozen yet. It is illegal to have a primitive
9214 -- equality declared in the private part if the type is visible.
9216 elsif not In_Same_List (Parent (Typ), Decl)
9217 and then not Is_Limited_Type (Typ)
9219 -- Shouldn't we give an RM reference here???
9221 if Ada_Version >= Ada_2012 then
9223 ("equality operator appears too late<<", Eq_Op);
9226 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9229 -- Finally check for AI12-0352: declaration of a user-defined primitive
9230 -- equality operation for a record type T is illegal if it occurs after
9231 -- a type has been derived from T.
9234 Obj_Decl := Next (Parent (Typ));
9236 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9237 if Nkind (Obj_Decl) = N_Full_Type_Declaration
9238 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9241 ("equality operator cannot appear after derivation", Eq_Op);
9243 ("an equality operator for& cannot be declared after "
9251 end Check_Untagged_Equality;
9253 -----------------------------
9254 -- Find_Corresponding_Spec --
9255 -----------------------------
9257 function Find_Corresponding_Spec
9259 Post_Error : Boolean := True) return Entity_Id
9261 Spec : constant Node_Id := Specification (N);
9262 Designator : constant Entity_Id := Defining_Entity (Spec);
9266 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9267 -- Even if fully conformant, a body may depend on a generic actual when
9268 -- the spec does not, or vice versa, in which case they were distinct
9269 -- entities in the generic.
9271 -------------------------------
9272 -- Different_Generic_Profile --
9273 -------------------------------
9275 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9278 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9279 -- Check that the types of corresponding formals have the same
9280 -- generic actual if any. We have to account for subtypes of a
9281 -- generic formal, declared between a spec and a body, which may
9282 -- appear distinct in an instance but matched in the generic, and
9283 -- the subtype may be used either in the spec or the body of the
9284 -- subprogram being checked.
9286 -------------------------
9287 -- Same_Generic_Actual --
9288 -------------------------
9290 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9292 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9293 -- Predicate to check whether S1 is a subtype of S2 in the source
9296 -------------------------
9297 -- Is_Declared_Subtype --
9298 -------------------------
9300 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9302 return Comes_From_Source (Parent (S1))
9303 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9304 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9305 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9306 end Is_Declared_Subtype;
9308 -- Start of processing for Same_Generic_Actual
9311 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9312 or else Is_Declared_Subtype (T1, T2)
9313 or else Is_Declared_Subtype (T2, T1);
9314 end Same_Generic_Actual;
9316 -- Start of processing for Different_Generic_Profile
9319 if not In_Instance then
9322 elsif Ekind (E) = E_Function
9323 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9328 F1 := First_Formal (Designator);
9329 F2 := First_Formal (E);
9330 while Present (F1) loop
9331 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9340 end Different_Generic_Profile;
9342 -- Start of processing for Find_Corresponding_Spec
9345 E := Current_Entity (Designator);
9346 while Present (E) loop
9348 -- We are looking for a matching spec. It must have the same scope,
9349 -- and the same name, and either be type conformant, or be the case
9350 -- of a library procedure spec and its body (which belong to one
9351 -- another regardless of whether they are type conformant or not).
9353 if Scope (E) = Current_Scope then
9354 if Current_Scope = Standard_Standard
9355 or else (Ekind (E) = Ekind (Designator)
9356 and then Type_Conformant (E, Designator))
9358 -- Within an instantiation, we know that spec and body are
9359 -- subtype conformant, because they were subtype conformant in
9360 -- the generic. We choose the subtype-conformant entity here as
9361 -- well, to resolve spurious ambiguities in the instance that
9362 -- were not present in the generic (i.e. when two different
9363 -- types are given the same actual). If we are looking for a
9364 -- spec to match a body, full conformance is expected.
9368 -- Inherit the convention and "ghostness" of the matching
9369 -- spec to ensure proper full and subtype conformance.
9371 Set_Convention (Designator, Convention (E));
9373 -- Skip past subprogram bodies and subprogram renamings that
9374 -- may appear to have a matching spec, but that aren't fully
9375 -- conformant with it. That can occur in cases where an
9376 -- actual type causes unrelated homographs in the instance.
9378 if Nkind (N) in N_Subprogram_Body
9379 | N_Subprogram_Renaming_Declaration
9380 and then Present (Homonym (E))
9381 and then not Fully_Conformant (Designator, E)
9385 elsif not Subtype_Conformant (Designator, E) then
9388 elsif Different_Generic_Profile (E) then
9393 -- Ada 2012 (AI05-0165): For internally generated bodies of
9394 -- null procedures locate the internally generated spec. We
9395 -- enforce mode conformance since a tagged type may inherit
9396 -- from interfaces several null primitives which differ only
9397 -- in the mode of the formals.
9399 if not (Comes_From_Source (E))
9400 and then Is_Null_Procedure (E)
9401 and then not Mode_Conformant (Designator, E)
9405 -- For null procedures coming from source that are completions,
9406 -- analysis of the generated body will establish the link.
9408 elsif Comes_From_Source (E)
9409 and then Nkind (Spec) = N_Procedure_Specification
9410 and then Null_Present (Spec)
9414 -- Expression functions can be completions, but cannot be
9415 -- completed by an explicit body.
9417 elsif Comes_From_Source (E)
9418 and then Comes_From_Source (N)
9419 and then Nkind (N) = N_Subprogram_Body
9420 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9421 N_Expression_Function
9423 Error_Msg_Sloc := Sloc (E);
9424 Error_Msg_N ("body conflicts with expression function#", N);
9427 elsif not Has_Completion (E) then
9428 if Nkind (N) /= N_Subprogram_Body_Stub then
9429 Set_Corresponding_Spec (N, E);
9432 Set_Has_Completion (E);
9435 elsif Nkind (Parent (N)) = N_Subunit then
9437 -- If this is the proper body of a subunit, the completion
9438 -- flag is set when analyzing the stub.
9442 -- If E is an internal function with a controlling result that
9443 -- was created for an operation inherited by a null extension,
9444 -- it may be overridden by a body without a previous spec (one
9445 -- more reason why these should be shunned). In that case we
9446 -- remove the generated body if present, because the current
9447 -- one is the explicit overriding.
9449 elsif Ekind (E) = E_Function
9450 and then Ada_Version >= Ada_2005
9451 and then not Comes_From_Source (E)
9452 and then Has_Controlling_Result (E)
9453 and then Is_Null_Extension (Etype (E))
9454 and then Comes_From_Source (Spec)
9456 Set_Has_Completion (E, False);
9459 and then Nkind (Parent (E)) = N_Function_Specification
9462 (Unit_Declaration_Node
9463 (Corresponding_Body (Unit_Declaration_Node (E))));
9467 -- If expansion is disabled, or if the wrapper function has
9468 -- not been generated yet, this a late body overriding an
9469 -- inherited operation, or it is an overriding by some other
9470 -- declaration before the controlling result is frozen. In
9471 -- either case this is a declaration of a new entity.
9477 -- If the body already exists, then this is an error unless
9478 -- the previous declaration is the implicit declaration of a
9479 -- derived subprogram. It is also legal for an instance to
9480 -- contain type conformant overloadable declarations (but the
9481 -- generic declaration may not), per 8.3(26/2).
9483 elsif No (Alias (E))
9484 and then not Is_Intrinsic_Subprogram (E)
9485 and then not In_Instance
9488 Error_Msg_Sloc := Sloc (E);
9490 if Is_Imported (E) then
9492 ("body not allowed for imported subprogram & declared#",
9495 Error_Msg_NE ("duplicate body for & declared#", N, E);
9499 -- Child units cannot be overloaded, so a conformance mismatch
9500 -- between body and a previous spec is an error.
9502 elsif Is_Child_Unit (E)
9504 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9506 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9511 ("body of child unit does not match previous declaration", N);
9519 -- On exit, we know that no previous declaration of subprogram exists
9522 end Find_Corresponding_Spec;
9524 ----------------------
9525 -- Fully_Conformant --
9526 ----------------------
9528 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9531 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9533 end Fully_Conformant;
9535 ----------------------------------
9536 -- Fully_Conformant_Expressions --
9537 ----------------------------------
9539 function Fully_Conformant_Expressions
9540 (Given_E1 : Node_Id;
9542 Report : Boolean := False) return Boolean
9544 E1 : constant Node_Id := Original_Node (Given_E1);
9545 E2 : constant Node_Id := Original_Node (Given_E2);
9546 -- We always test conformance on original nodes, since it is possible
9547 -- for analysis and/or expansion to make things look as though they
9548 -- conform when they do not, e.g. by converting 1+2 into 3.
9550 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9553 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9554 -- Compare elements of two lists for conformance. Elements have to be
9555 -- conformant, and actuals inserted as default parameters do not match
9556 -- explicit actuals with the same value.
9558 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9559 -- Compare an operator node with a function call
9565 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9567 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9574 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9579 if L1 = No_List then
9585 if L2 = No_List then
9591 -- Compare two lists, skipping rewrite insertions (we want to compare
9592 -- the original trees, not the expanded versions).
9595 if Is_Rewrite_Insertion (N1) then
9597 elsif Is_Rewrite_Insertion (N2) then
9603 elsif not FCE (N1, N2) then
9616 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9617 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9622 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9627 Act := First (Actuals);
9629 if Nkind (Op_Node) in N_Binary_Op then
9630 if not FCE (Left_Opnd (Op_Node), Act) then
9637 return Present (Act)
9638 and then FCE (Right_Opnd (Op_Node), Act)
9639 and then No (Next (Act));
9643 function User_Defined_Numeric_Literal_Mismatch return Boolean;
9644 -- Usually literals with the same value like 12345 and 12_345
9645 -- or 123.0 and 123.00 conform, but not if they are
9646 -- user-defined literals.
9648 -------------------------------------------
9649 -- User_Defined_Numeric_Literal_Mismatch --
9650 -------------------------------------------
9652 function User_Defined_Numeric_Literal_Mismatch return Boolean is
9653 E1_Is_User_Defined : constant Boolean :=
9654 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
9655 E2_Is_User_Defined : constant Boolean :=
9656 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
9659 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
9661 return E1_Is_User_Defined and then
9662 not String_Equal (String_From_Numeric_Literal (E1),
9663 String_From_Numeric_Literal (E2));
9664 end User_Defined_Numeric_Literal_Mismatch;
9670 -- Start of processing for Fully_Conformant_Expressions
9675 -- Nonconformant if paren count does not match. Note: if some idiot
9676 -- complains that we don't do this right for more than 3 levels of
9677 -- parentheses, they will be treated with the respect they deserve.
9679 if Paren_Count (E1) /= Paren_Count (E2) then
9682 -- If same entities are referenced, then they are conformant even if
9683 -- they have different forms (RM 8.3.1(19-20)).
9685 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9686 if Present (Entity (E1)) then
9687 Result := Entity (E1) = Entity (E2)
9689 -- One may be a discriminant that has been replaced by the
9690 -- corresponding discriminal.
9693 (Chars (Entity (E1)) = Chars (Entity (E2))
9694 and then Ekind (Entity (E1)) = E_Discriminant
9695 and then Ekind (Entity (E2)) = E_In_Parameter)
9697 -- The discriminant of a protected type is transformed into
9698 -- a local constant and then into a parameter of a protected
9702 (Ekind (Entity (E1)) = E_Constant
9703 and then Ekind (Entity (E2)) = E_In_Parameter
9704 and then Present (Discriminal_Link (Entity (E1)))
9705 and then Discriminal_Link (Entity (E1)) =
9706 Discriminal_Link (Entity (E2)))
9708 -- AI12-050: The loop variables of quantified expressions match
9709 -- if they have the same identifier, even though they may have
9710 -- different entities.
9713 (Chars (Entity (E1)) = Chars (Entity (E2))
9714 and then Ekind (Entity (E1)) = E_Loop_Parameter
9715 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9717 -- A call to an instantiation of Unchecked_Conversion is
9718 -- rewritten with the name of the generated function created for
9719 -- the instance, and this must be special-cased.
9722 (Ekind (Entity (E1)) = E_Function
9723 and then Is_Intrinsic_Subprogram (Entity (E1))
9724 and then Is_Generic_Instance (Entity (E1))
9725 and then Entity (E2) = Alias (Entity (E1)));
9726 if Report and not Result then
9728 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9730 ("Meaning of& differs because of declaration#", E1, E2);
9735 elsif Nkind (E1) = N_Expanded_Name
9736 and then Nkind (E2) = N_Expanded_Name
9737 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9738 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9740 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9743 -- Identifiers in component associations don't always have
9744 -- entities, but their names must conform.
9746 return Nkind (E1) = N_Identifier
9747 and then Nkind (E2) = N_Identifier
9748 and then Chars (E1) = Chars (E2);
9751 elsif Nkind (E1) = N_Character_Literal
9752 and then Nkind (E2) = N_Expanded_Name
9754 return Nkind (Selector_Name (E2)) = N_Character_Literal
9755 and then Chars (E1) = Chars (Selector_Name (E2));
9757 elsif Nkind (E2) = N_Character_Literal
9758 and then Nkind (E1) = N_Expanded_Name
9760 return Nkind (Selector_Name (E1)) = N_Character_Literal
9761 and then Chars (E2) = Chars (Selector_Name (E1));
9763 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9764 return FCO (E1, E2);
9766 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9767 return FCO (E2, E1);
9769 -- Otherwise we must have the same syntactic entity
9771 elsif Nkind (E1) /= Nkind (E2) then
9774 -- At this point, we specialize by node type
9780 FCL (Expressions (E1), Expressions (E2))
9782 FCL (Component_Associations (E1),
9783 Component_Associations (E2));
9786 if Nkind (Expression (E1)) = N_Qualified_Expression
9788 Nkind (Expression (E2)) = N_Qualified_Expression
9790 return FCE (Expression (E1), Expression (E2));
9792 -- Check that the subtype marks and any constraints
9797 Indic1 : constant Node_Id := Expression (E1);
9798 Indic2 : constant Node_Id := Expression (E2);
9803 if Nkind (Indic1) /= N_Subtype_Indication then
9805 Nkind (Indic2) /= N_Subtype_Indication
9806 and then Entity (Indic1) = Entity (Indic2);
9808 elsif Nkind (Indic2) /= N_Subtype_Indication then
9810 Nkind (Indic1) /= N_Subtype_Indication
9811 and then Entity (Indic1) = Entity (Indic2);
9814 if Entity (Subtype_Mark (Indic1)) /=
9815 Entity (Subtype_Mark (Indic2))
9820 Elt1 := First (Constraints (Constraint (Indic1)));
9821 Elt2 := First (Constraints (Constraint (Indic2)));
9822 while Present (Elt1) and then Present (Elt2) loop
9823 if not FCE (Elt1, Elt2) then
9836 when N_Attribute_Reference =>
9838 Attribute_Name (E1) = Attribute_Name (E2)
9839 and then FCL (Expressions (E1), Expressions (E2));
9843 Entity (E1) = Entity (E2)
9844 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9845 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9847 when N_Membership_Test
9851 FCE (Left_Opnd (E1), Left_Opnd (E2))
9853 FCE (Right_Opnd (E1), Right_Opnd (E2));
9855 when N_Case_Expression =>
9861 if not FCE (Expression (E1), Expression (E2)) then
9865 Alt1 := First (Alternatives (E1));
9866 Alt2 := First (Alternatives (E2));
9868 if Present (Alt1) /= Present (Alt2) then
9870 elsif No (Alt1) then
9874 if not FCE (Expression (Alt1), Expression (Alt2))
9875 or else not FCL (Discrete_Choices (Alt1),
9876 Discrete_Choices (Alt2))
9887 when N_Character_Literal =>
9889 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9891 when N_Component_Association =>
9893 FCL (Choices (E1), Choices (E2))
9895 FCE (Expression (E1), Expression (E2));
9897 when N_Explicit_Dereference =>
9899 FCE (Prefix (E1), Prefix (E2));
9901 when N_Extension_Aggregate =>
9903 FCL (Expressions (E1), Expressions (E2))
9904 and then Null_Record_Present (E1) =
9905 Null_Record_Present (E2)
9906 and then FCL (Component_Associations (E1),
9907 Component_Associations (E2));
9909 when N_Function_Call =>
9911 FCE (Name (E1), Name (E2))
9913 FCL (Parameter_Associations (E1),
9914 Parameter_Associations (E2));
9916 when N_If_Expression =>
9918 FCL (Expressions (E1), Expressions (E2));
9920 when N_Indexed_Component =>
9922 FCE (Prefix (E1), Prefix (E2))
9924 FCL (Expressions (E1), Expressions (E2));
9926 when N_Integer_Literal =>
9927 return (Intval (E1) = Intval (E2))
9928 and then not User_Defined_Numeric_Literal_Mismatch;
9933 when N_Operator_Symbol =>
9935 Chars (E1) = Chars (E2);
9937 when N_Others_Choice =>
9940 when N_Parameter_Association =>
9942 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9943 and then FCE (Explicit_Actual_Parameter (E1),
9944 Explicit_Actual_Parameter (E2));
9946 when N_Qualified_Expression
9948 | N_Unchecked_Type_Conversion
9951 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9953 FCE (Expression (E1), Expression (E2));
9955 when N_Quantified_Expression =>
9956 if not FCE (Condition (E1), Condition (E2)) then
9960 if Present (Loop_Parameter_Specification (E1))
9961 and then Present (Loop_Parameter_Specification (E2))
9964 L1 : constant Node_Id :=
9965 Loop_Parameter_Specification (E1);
9966 L2 : constant Node_Id :=
9967 Loop_Parameter_Specification (E2);
9971 Reverse_Present (L1) = Reverse_Present (L2)
9973 FCE (Defining_Identifier (L1),
9974 Defining_Identifier (L2))
9976 FCE (Discrete_Subtype_Definition (L1),
9977 Discrete_Subtype_Definition (L2));
9980 elsif Present (Iterator_Specification (E1))
9981 and then Present (Iterator_Specification (E2))
9984 I1 : constant Node_Id := Iterator_Specification (E1);
9985 I2 : constant Node_Id := Iterator_Specification (E2);
9989 FCE (Defining_Identifier (I1),
9990 Defining_Identifier (I2))
9992 Of_Present (I1) = Of_Present (I2)
9994 Reverse_Present (I1) = Reverse_Present (I2)
9995 and then FCE (Name (I1), Name (I2))
9996 and then FCE (Subtype_Indication (I1),
9997 Subtype_Indication (I2));
10000 -- The quantified expressions used different specifications to
10001 -- walk their respective ranges.
10009 FCE (Low_Bound (E1), Low_Bound (E2))
10011 FCE (High_Bound (E1), High_Bound (E2));
10013 when N_Real_Literal =>
10014 return (Realval (E1) = Realval (E2))
10015 and then not User_Defined_Numeric_Literal_Mismatch;
10017 when N_Selected_Component =>
10019 FCE (Prefix (E1), Prefix (E2))
10021 FCE (Selector_Name (E1), Selector_Name (E2));
10025 FCE (Prefix (E1), Prefix (E2))
10027 FCE (Discrete_Range (E1), Discrete_Range (E2));
10029 when N_String_Literal =>
10031 S1 : constant String_Id := Strval (E1);
10032 S2 : constant String_Id := Strval (E2);
10033 L1 : constant Nat := String_Length (S1);
10034 L2 : constant Nat := String_Length (S2);
10041 for J in 1 .. L1 loop
10042 if Get_String_Char (S1, J) /=
10043 Get_String_Char (S2, J)
10055 Entity (E1) = Entity (E2)
10057 FCE (Right_Opnd (E1), Right_Opnd (E2));
10059 -- All other node types cannot appear in this context. Strictly
10060 -- we should raise a fatal internal error. Instead we just ignore
10061 -- the nodes. This means that if anyone makes a mistake in the
10062 -- expander and mucks an expression tree irretrievably, the result
10063 -- will be a failure to detect a (probably very obscure) case
10064 -- of non-conformance, which is better than bombing on some
10065 -- case where two expressions do in fact conform.
10071 end Fully_Conformant_Expressions;
10073 ----------------------------------------
10074 -- Fully_Conformant_Discrete_Subtypes --
10075 ----------------------------------------
10077 function Fully_Conformant_Discrete_Subtypes
10078 (Given_S1 : Node_Id;
10079 Given_S2 : Node_Id) return Boolean
10081 S1 : constant Node_Id := Original_Node (Given_S1);
10082 S2 : constant Node_Id := Original_Node (Given_S2);
10084 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10085 -- Special-case for a bound given by a discriminant, which in the body
10086 -- is replaced with the discriminal of the enclosing type.
10088 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10089 -- Check both bounds
10091 -----------------------
10092 -- Conforming_Bounds --
10093 -----------------------
10095 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10097 if Is_Entity_Name (B1)
10098 and then Is_Entity_Name (B2)
10099 and then Ekind (Entity (B1)) = E_Discriminant
10101 return Chars (B1) = Chars (B2);
10104 return Fully_Conformant_Expressions (B1, B2);
10106 end Conforming_Bounds;
10108 -----------------------
10109 -- Conforming_Ranges --
10110 -----------------------
10112 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10115 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10117 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10118 end Conforming_Ranges;
10120 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10123 if Nkind (S1) /= Nkind (S2) then
10126 elsif Is_Entity_Name (S1) then
10127 return Entity (S1) = Entity (S2);
10129 elsif Nkind (S1) = N_Range then
10130 return Conforming_Ranges (S1, S2);
10132 elsif Nkind (S1) = N_Subtype_Indication then
10134 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10137 (Range_Expression (Constraint (S1)),
10138 Range_Expression (Constraint (S2)));
10142 end Fully_Conformant_Discrete_Subtypes;
10144 --------------------
10145 -- Install_Entity --
10146 --------------------
10148 procedure Install_Entity (E : Entity_Id) is
10149 Prev : constant Entity_Id := Current_Entity (E);
10151 Set_Is_Immediately_Visible (E);
10152 Set_Current_Entity (E);
10153 Set_Homonym (E, Prev);
10154 end Install_Entity;
10156 ---------------------
10157 -- Install_Formals --
10158 ---------------------
10160 procedure Install_Formals (Id : Entity_Id) is
10163 F := First_Formal (Id);
10164 while Present (F) loop
10165 Install_Entity (F);
10168 end Install_Formals;
10170 -----------------------------
10171 -- Is_Interface_Conformant --
10172 -----------------------------
10174 function Is_Interface_Conformant
10175 (Tagged_Type : Entity_Id;
10176 Iface_Prim : Entity_Id;
10177 Prim : Entity_Id) return Boolean
10179 -- The operation may in fact be an inherited (implicit) operation
10180 -- rather than the original interface primitive, so retrieve the
10181 -- ultimate ancestor.
10183 Iface : constant Entity_Id :=
10184 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10185 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10187 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10188 -- Return the controlling formal of Prim
10190 ------------------------
10191 -- Controlling_Formal --
10192 ------------------------
10194 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10198 E := First_Entity (Prim);
10199 while Present (E) loop
10200 if Is_Formal (E) and then Is_Controlling_Formal (E) then
10208 end Controlling_Formal;
10212 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10213 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10215 -- Start of processing for Is_Interface_Conformant
10218 pragma Assert (Is_Subprogram (Iface_Prim)
10219 and then Is_Subprogram (Prim)
10220 and then Is_Dispatching_Operation (Iface_Prim)
10221 and then Is_Dispatching_Operation (Prim));
10223 pragma Assert (Is_Interface (Iface)
10224 or else (Present (Alias (Iface_Prim))
10227 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10229 if Prim = Iface_Prim
10230 or else not Is_Subprogram (Prim)
10231 or else Ekind (Prim) /= Ekind (Iface_Prim)
10232 or else not Is_Dispatching_Operation (Prim)
10233 or else Scope (Prim) /= Scope (Tagged_Type)
10235 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10236 or else not Primitive_Names_Match (Iface_Prim, Prim)
10240 -- The mode of the controlling formals must match
10242 elsif Present (Iface_Ctrl_F)
10243 and then Present (Prim_Ctrl_F)
10244 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10248 -- Case of a procedure, or a function whose result type matches the
10249 -- result type of the interface primitive, or a function that has no
10250 -- controlling result (I or access I).
10252 elsif Ekind (Iface_Prim) = E_Procedure
10253 or else Etype (Prim) = Etype (Iface_Prim)
10254 or else not Has_Controlling_Result (Prim)
10256 return Type_Conformant
10257 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10259 -- Case of a function returning an interface, or an access to one. Check
10260 -- that the return types correspond.
10262 elsif Implements_Interface (Typ, Iface) then
10263 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10265 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10270 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10271 Skip_Controlling_Formals => True);
10277 end Is_Interface_Conformant;
10279 ---------------------------------
10280 -- Is_Non_Overriding_Operation --
10281 ---------------------------------
10283 function Is_Non_Overriding_Operation
10284 (Prev_E : Entity_Id;
10285 New_E : Entity_Id) return Boolean
10287 Formal : Entity_Id;
10289 G_Typ : Entity_Id := Empty;
10291 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10292 -- If F_Type is a derived type associated with a generic actual subtype,
10293 -- then return its Generic_Parent_Type attribute, else return Empty.
10295 function Types_Correspond
10296 (P_Type : Entity_Id;
10297 N_Type : Entity_Id) return Boolean;
10298 -- Returns true if and only if the types (or designated types in the
10299 -- case of anonymous access types) are the same or N_Type is derived
10300 -- directly or indirectly from P_Type.
10302 -----------------------------
10303 -- Get_Generic_Parent_Type --
10304 -----------------------------
10306 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10312 if Is_Derived_Type (F_Typ)
10313 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10315 -- The tree must be traversed to determine the parent subtype in
10316 -- the generic unit, which unfortunately isn't always available
10317 -- via semantic attributes. ??? (Note: The use of Original_Node
10318 -- is needed for cases where a full derived type has been
10321 -- If the parent type is a scalar type, the derivation creates
10322 -- an anonymous base type for it, and the source type is its
10325 if Is_Scalar_Type (F_Typ)
10326 and then not Comes_From_Source (F_Typ)
10330 (Original_Node (Parent (First_Subtype (F_Typ))));
10332 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10334 if Nkind (Defn) = N_Derived_Type_Definition then
10335 Indic := Subtype_Indication (Defn);
10337 if Nkind (Indic) = N_Subtype_Indication then
10338 G_Typ := Entity (Subtype_Mark (Indic));
10340 G_Typ := Entity (Indic);
10343 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10344 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10346 return Generic_Parent_Type (Parent (G_Typ));
10352 end Get_Generic_Parent_Type;
10354 ----------------------
10355 -- Types_Correspond --
10356 ----------------------
10358 function Types_Correspond
10359 (P_Type : Entity_Id;
10360 N_Type : Entity_Id) return Boolean
10362 Prev_Type : Entity_Id := Base_Type (P_Type);
10363 New_Type : Entity_Id := Base_Type (N_Type);
10366 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10367 Prev_Type := Designated_Type (Prev_Type);
10370 if Ekind (New_Type) = E_Anonymous_Access_Type then
10371 New_Type := Designated_Type (New_Type);
10374 if Prev_Type = New_Type then
10377 elsif not Is_Class_Wide_Type (New_Type) then
10378 while Etype (New_Type) /= New_Type loop
10379 New_Type := Etype (New_Type);
10381 if New_Type = Prev_Type then
10387 end Types_Correspond;
10389 -- Start of processing for Is_Non_Overriding_Operation
10392 -- In the case where both operations are implicit derived subprograms
10393 -- then neither overrides the other. This can only occur in certain
10394 -- obscure cases (e.g., derivation from homographs created in a generic
10397 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10400 elsif Ekind (Current_Scope) = E_Package
10401 and then Is_Generic_Instance (Current_Scope)
10402 and then In_Private_Part (Current_Scope)
10403 and then Comes_From_Source (New_E)
10405 -- We examine the formals and result type of the inherited operation,
10406 -- to determine whether their type is derived from (the instance of)
10407 -- a generic type. The first such formal or result type is the one
10410 Formal := First_Formal (Prev_E);
10412 while Present (Formal) loop
10413 F_Typ := Base_Type (Etype (Formal));
10415 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10416 F_Typ := Designated_Type (F_Typ);
10419 G_Typ := Get_Generic_Parent_Type (F_Typ);
10420 exit when Present (G_Typ);
10422 Next_Formal (Formal);
10425 -- If the function dispatches on result check the result type
10427 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10428 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10435 -- If the generic type is a private type, then the original operation
10436 -- was not overriding in the generic, because there was no primitive
10437 -- operation to override.
10439 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10440 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10441 N_Formal_Private_Type_Definition
10445 -- The generic parent type is the ancestor of a formal derived
10446 -- type declaration. We need to check whether it has a primitive
10447 -- operation that should be overridden by New_E in the generic.
10451 P_Formal : Entity_Id;
10452 N_Formal : Entity_Id;
10455 P_Prim : Entity_Id;
10456 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10459 while Present (Prim_Elt) loop
10460 P_Prim := Node (Prim_Elt);
10462 if Chars (P_Prim) = Chars (New_E)
10463 and then Ekind (P_Prim) = Ekind (New_E)
10465 P_Formal := First_Formal (P_Prim);
10466 N_Formal := First_Formal (New_E);
10467 while Present (P_Formal) and then Present (N_Formal) loop
10468 P_Typ := Etype (P_Formal);
10469 N_Typ := Etype (N_Formal);
10471 if not Types_Correspond (P_Typ, N_Typ) then
10475 Next_Entity (P_Formal);
10476 Next_Entity (N_Formal);
10479 -- Found a matching primitive operation belonging to the
10480 -- formal ancestor type, so the new subprogram is
10484 and then No (N_Formal)
10485 and then (Ekind (New_E) /= E_Function
10488 (Etype (P_Prim), Etype (New_E)))
10494 Next_Elmt (Prim_Elt);
10497 -- If no match found, then the new subprogram does not override
10498 -- in the generic (nor in the instance).
10500 -- If the type in question is not abstract, and the subprogram
10501 -- is, this will be an error if the new operation is in the
10502 -- private part of the instance. Emit a warning now, which will
10503 -- make the subsequent error message easier to understand.
10505 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10506 and then Is_Abstract_Subprogram (Prev_E)
10507 and then In_Private_Part (Current_Scope)
10509 Error_Msg_Node_2 := F_Typ;
10511 ("private operation& in generic unit does not override "
10512 & "any primitive operation of& (RM 12.3 (18))??",
10522 end Is_Non_Overriding_Operation;
10524 -------------------------------------
10525 -- List_Inherited_Pre_Post_Aspects --
10526 -------------------------------------
10528 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10530 if Opt.List_Inherited_Aspects
10531 and then Is_Subprogram_Or_Generic_Subprogram (E)
10534 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10539 for Index in Subps'Range loop
10540 Items := Contract (Subps (Index));
10542 if Present (Items) then
10543 Prag := Pre_Post_Conditions (Items);
10544 while Present (Prag) loop
10545 Error_Msg_Sloc := Sloc (Prag);
10547 if Class_Present (Prag)
10548 and then not Split_PPC (Prag)
10550 if Pragma_Name (Prag) = Name_Precondition then
10552 ("info: & inherits `Pre''Class` aspect from "
10556 ("info: & inherits `Post''Class` aspect from "
10561 Prag := Next_Pragma (Prag);
10567 end List_Inherited_Pre_Post_Aspects;
10569 ------------------------------
10570 -- Make_Inequality_Operator --
10571 ------------------------------
10573 -- S is the defining identifier of an equality operator. We build a
10574 -- subprogram declaration with the right signature. This operation is
10575 -- intrinsic, because it is always expanded as the negation of the
10576 -- call to the equality function.
10578 procedure Make_Inequality_Operator (S : Entity_Id) is
10579 Loc : constant Source_Ptr := Sloc (S);
10582 Op_Name : Entity_Id;
10584 FF : constant Entity_Id := First_Formal (S);
10585 NF : constant Entity_Id := Next_Formal (FF);
10588 -- Check that equality was properly defined, ignore call if not
10595 A : constant Entity_Id :=
10596 Make_Defining_Identifier (Sloc (FF),
10597 Chars => Chars (FF));
10599 B : constant Entity_Id :=
10600 Make_Defining_Identifier (Sloc (NF),
10601 Chars => Chars (NF));
10604 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10606 Formals := New_List (
10607 Make_Parameter_Specification (Loc,
10608 Defining_Identifier => A,
10610 New_Occurrence_Of (Etype (First_Formal (S)),
10611 Sloc (Etype (First_Formal (S))))),
10613 Make_Parameter_Specification (Loc,
10614 Defining_Identifier => B,
10616 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10617 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10620 Make_Subprogram_Declaration (Loc,
10622 Make_Function_Specification (Loc,
10623 Defining_Unit_Name => Op_Name,
10624 Parameter_Specifications => Formals,
10625 Result_Definition =>
10626 New_Occurrence_Of (Standard_Boolean, Loc)));
10628 -- Insert inequality right after equality if it is explicit or after
10629 -- the derived type when implicit. These entities are created only
10630 -- for visibility purposes, and eventually replaced in the course
10631 -- of expansion, so they do not need to be attached to the tree and
10632 -- seen by the back-end. Keeping them internal also avoids spurious
10633 -- freezing problems. The declaration is inserted in the tree for
10634 -- analysis, and removed afterwards. If the equality operator comes
10635 -- from an explicit declaration, attach the inequality immediately
10636 -- after. Else the equality is inherited from a derived type
10637 -- declaration, so insert inequality after that declaration.
10639 if No (Alias (S)) then
10640 Insert_After (Unit_Declaration_Node (S), Decl);
10641 elsif Is_List_Member (Parent (S)) then
10642 Insert_After (Parent (S), Decl);
10644 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10647 Mark_Rewrite_Insertion (Decl);
10648 Set_Is_Intrinsic_Subprogram (Op_Name);
10651 Set_Has_Completion (Op_Name);
10652 Set_Corresponding_Equality (Op_Name, S);
10653 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10655 end Make_Inequality_Operator;
10657 ----------------------
10658 -- May_Need_Actuals --
10659 ----------------------
10661 procedure May_Need_Actuals (Fun : Entity_Id) is
10666 F := First_Formal (Fun);
10668 while Present (F) loop
10669 if No (Default_Value (F)) then
10677 Set_Needs_No_Actuals (Fun, B);
10678 end May_Need_Actuals;
10680 ---------------------
10681 -- Mode_Conformant --
10682 ---------------------
10684 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10687 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10689 end Mode_Conformant;
10691 ---------------------------
10692 -- New_Overloaded_Entity --
10693 ---------------------------
10695 procedure New_Overloaded_Entity
10697 Derived_Type : Entity_Id := Empty)
10699 Overridden_Subp : Entity_Id := Empty;
10700 -- Set if the current scope has an operation that is type-conformant
10701 -- with S, and becomes hidden by S.
10703 Is_Primitive_Subp : Boolean;
10704 -- Set to True if the new subprogram is primitive
10707 -- Entity that S overrides
10709 procedure Check_For_Primitive_Subprogram
10710 (Is_Primitive : out Boolean;
10711 Is_Overriding : Boolean := False);
10712 -- If the subprogram being analyzed is a primitive operation of the type
10713 -- of a formal or result, set the Has_Primitive_Operations flag on the
10714 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10715 -- corresponding flag on the entity itself for later use.
10717 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10718 -- True if a) E is a subprogram whose first formal is a concurrent type
10719 -- defined in the scope of E that has some entry or subprogram whose
10720 -- profile matches E, or b) E is an internally built dispatching
10721 -- subprogram of a protected type and there is a matching subprogram
10722 -- defined in the enclosing scope of the protected type, or c) E is
10723 -- an entry of a synchronized type and a matching procedure has been
10724 -- previously defined in the enclosing scope of the synchronized type.
10726 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10727 -- Check that E is declared in the private part of the current package,
10728 -- or in the package body, where it may hide a previous declaration.
10729 -- We can't use In_Private_Part by itself because this flag is also
10730 -- set when freezing entities, so we must examine the place of the
10731 -- declaration in the tree, and recognize wrapper packages as well.
10733 function Is_Overriding_Alias
10734 (Old_E : Entity_Id;
10735 New_E : Entity_Id) return Boolean;
10736 -- Check whether new subprogram and old subprogram are both inherited
10737 -- from subprograms that have distinct dispatch table entries. This can
10738 -- occur with derivations from instances with accidental homonyms. The
10739 -- function is conservative given that the converse is only true within
10740 -- instances that contain accidental overloadings.
10742 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10743 -- Report conflict between entities S and E
10745 ------------------------------------
10746 -- Check_For_Primitive_Subprogram --
10747 ------------------------------------
10749 procedure Check_For_Primitive_Subprogram
10750 (Is_Primitive : out Boolean;
10751 Is_Overriding : Boolean := False)
10753 Formal : Entity_Id;
10757 function Visible_Part_Type (T : Entity_Id) return Boolean;
10758 -- Returns true if T is declared in the visible part of the current
10759 -- package scope; otherwise returns false. Assumes that T is declared
10762 procedure Check_Private_Overriding (T : Entity_Id);
10763 -- Checks that if a primitive abstract subprogram of a visible
10764 -- abstract type is declared in a private part, then it must override
10765 -- an abstract subprogram declared in the visible part. Also checks
10766 -- that if a primitive function with a controlling result is declared
10767 -- in a private part, then it must override a function declared in
10768 -- the visible part.
10770 ------------------------------
10771 -- Check_Private_Overriding --
10772 ------------------------------
10774 procedure Check_Private_Overriding (T : Entity_Id) is
10775 function Overrides_Private_Part_Op return Boolean;
10776 -- This detects the special case where the overriding subprogram
10777 -- is overriding a subprogram that was declared in the same
10778 -- private part. That case is illegal by 3.9.3(10).
10780 function Overrides_Visible_Function
10781 (Partial_View : Entity_Id) return Boolean;
10782 -- True if S overrides a function in the visible part. The
10783 -- overridden function could be explicitly or implicitly declared.
10785 -------------------------------
10786 -- Overrides_Private_Part_Op --
10787 -------------------------------
10789 function Overrides_Private_Part_Op return Boolean is
10790 Over_Decl : constant Node_Id :=
10791 Unit_Declaration_Node (Overridden_Operation (S));
10792 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10795 pragma Assert (Is_Overriding);
10797 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10799 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10801 return In_Same_List (Over_Decl, Subp_Decl);
10802 end Overrides_Private_Part_Op;
10804 --------------------------------
10805 -- Overrides_Visible_Function --
10806 --------------------------------
10808 function Overrides_Visible_Function
10809 (Partial_View : Entity_Id) return Boolean
10812 if not Is_Overriding or else not Has_Homonym (S) then
10816 if not Present (Partial_View) then
10820 -- Search through all the homonyms H of S in the current
10821 -- package spec, and return True if we find one that matches.
10822 -- Note that Parent (H) will be the declaration of the
10823 -- partial view of T for a match.
10826 H : Entity_Id := S;
10830 exit when not Present (H) or else Scope (H) /= Scope (S);
10832 if Nkind (Parent (H)) in
10833 N_Private_Extension_Declaration |
10834 N_Private_Type_Declaration
10835 and then Defining_Identifier (Parent (H)) = Partial_View
10843 end Overrides_Visible_Function;
10845 -- Start of processing for Check_Private_Overriding
10848 if Is_Package_Or_Generic_Package (Current_Scope)
10849 and then In_Private_Part (Current_Scope)
10850 and then Visible_Part_Type (T)
10851 and then not In_Instance
10853 if Is_Abstract_Type (T)
10854 and then Is_Abstract_Subprogram (S)
10855 and then (not Is_Overriding
10856 or else not Is_Abstract_Subprogram (E)
10857 or else Overrides_Private_Part_Op)
10860 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10863 elsif Ekind (S) = E_Function then
10865 Partial_View : constant Entity_Id :=
10866 Incomplete_Or_Partial_View (T);
10869 if not Overrides_Visible_Function (Partial_View) then
10871 -- Here, S is "function ... return T;" declared in
10872 -- the private part, not overriding some visible
10873 -- operation. That's illegal in the tagged case
10874 -- (but not if the private type is untagged).
10876 if ((Present (Partial_View)
10877 and then Is_Tagged_Type (Partial_View))
10878 or else (not Present (Partial_View)
10879 and then Is_Tagged_Type (T)))
10880 and then T = Base_Type (Etype (S))
10883 ("private function with tagged result must"
10884 & " override visible-part function", S);
10886 ("\move subprogram to the visible part"
10887 & " (RM 3.9.3(10))", S);
10889 -- Ada 2012 (AI05-0073): Extend this check to the case
10890 -- of a function whose result subtype is defined by an
10891 -- access_definition designating specific tagged type.
10893 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10894 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10896 not Is_Class_Wide_Type
10897 (Designated_Type (Etype (S)))
10898 and then Ada_Version >= Ada_2012
10901 ("private function with controlling access "
10902 & "result must override visible-part function",
10905 ("\move subprogram to the visible part"
10906 & " (RM 3.9.3(10))", S);
10912 end Check_Private_Overriding;
10914 -----------------------
10915 -- Visible_Part_Type --
10916 -----------------------
10918 function Visible_Part_Type (T : Entity_Id) return Boolean is
10919 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10922 -- If the entity is a private type, then it must be declared in a
10925 if Ekind (T) in Private_Kind then
10928 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10931 elsif Is_List_Member (Declaration_Node (T))
10932 and then List_Containing (Declaration_Node (T)) =
10933 Visible_Declarations (Specification (P))
10940 end Visible_Part_Type;
10942 -- Start of processing for Check_For_Primitive_Subprogram
10945 Is_Primitive := False;
10947 if not Comes_From_Source (S) then
10950 -- If subprogram is at library level, it is not primitive operation
10952 elsif Current_Scope = Standard_Standard then
10955 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10956 and then not In_Package_Body (Current_Scope))
10957 or else Is_Overriding
10959 -- For function, check return type
10961 if Ekind (S) = E_Function then
10962 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10963 F_Typ := Designated_Type (Etype (S));
10965 F_Typ := Etype (S);
10968 B_Typ := Base_Type (F_Typ);
10970 if Scope (B_Typ) = Current_Scope
10971 and then not Is_Class_Wide_Type (B_Typ)
10972 and then not Is_Generic_Type (B_Typ)
10974 Is_Primitive := True;
10975 Set_Has_Primitive_Operations (B_Typ);
10976 Set_Is_Primitive (S);
10977 Check_Private_Overriding (B_Typ);
10979 -- The Ghost policy in effect at the point of declaration
10980 -- or a tagged type and a primitive operation must match
10981 -- (SPARK RM 6.9(16)).
10983 Check_Ghost_Primitive (S, B_Typ);
10987 -- For all subprograms, check formals
10989 Formal := First_Formal (S);
10990 while Present (Formal) loop
10991 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10992 F_Typ := Designated_Type (Etype (Formal));
10994 F_Typ := Etype (Formal);
10997 B_Typ := Base_Type (F_Typ);
10999 if Ekind (B_Typ) = E_Access_Subtype then
11000 B_Typ := Base_Type (B_Typ);
11003 if Scope (B_Typ) = Current_Scope
11004 and then not Is_Class_Wide_Type (B_Typ)
11005 and then not Is_Generic_Type (B_Typ)
11007 Is_Primitive := True;
11008 Set_Is_Primitive (S);
11009 Set_Has_Primitive_Operations (B_Typ);
11010 Check_Private_Overriding (B_Typ);
11012 -- The Ghost policy in effect at the point of declaration
11013 -- of a tagged type and a primitive operation must match
11014 -- (SPARK RM 6.9(16)).
11016 Check_Ghost_Primitive (S, B_Typ);
11019 Next_Formal (Formal);
11022 -- Special case: An equality function can be redefined for a type
11023 -- occurring in a declarative part, and won't otherwise be treated as
11024 -- a primitive because it doesn't occur in a package spec and doesn't
11025 -- override an inherited subprogram. It's important that we mark it
11026 -- primitive so it can be returned by Collect_Primitive_Operations
11027 -- and be used in composing the equality operation of later types
11028 -- that have a component of the type.
11030 elsif Chars (S) = Name_Op_Eq
11031 and then Etype (S) = Standard_Boolean
11033 B_Typ := Base_Type (Etype (First_Formal (S)));
11035 if Scope (B_Typ) = Current_Scope
11037 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11038 and then not Is_Limited_Type (B_Typ)
11040 Is_Primitive := True;
11041 Set_Is_Primitive (S);
11042 Set_Has_Primitive_Operations (B_Typ);
11043 Check_Private_Overriding (B_Typ);
11045 -- The Ghost policy in effect at the point of declaration of a
11046 -- tagged type and a primitive operation must match
11047 -- (SPARK RM 6.9(16)).
11049 Check_Ghost_Primitive (S, B_Typ);
11052 end Check_For_Primitive_Subprogram;
11054 --------------------------------------
11055 -- Has_Matching_Entry_Or_Subprogram --
11056 --------------------------------------
11058 function Has_Matching_Entry_Or_Subprogram
11059 (E : Entity_Id) return Boolean
11061 function Check_Conforming_Parameters
11062 (E1_Param : Node_Id;
11063 E2_Param : Node_Id;
11064 Ctype : Conformance_Type) return Boolean;
11065 -- Starting from the given parameters, check that all the parameters
11066 -- of two entries or subprograms are conformant. Used to skip
11067 -- the check on the controlling argument.
11069 function Matching_Entry_Or_Subprogram
11070 (Conc_Typ : Entity_Id;
11071 Subp : Entity_Id) return Entity_Id;
11072 -- Return the first entry or subprogram of the given concurrent type
11073 -- whose name matches the name of Subp and has a profile conformant
11074 -- with Subp; return Empty if not found.
11076 function Matching_Dispatching_Subprogram
11077 (Conc_Typ : Entity_Id;
11078 Ent : Entity_Id) return Entity_Id;
11079 -- Return the first dispatching primitive of Conc_Type defined in the
11080 -- enclosing scope of Conc_Type (i.e. before the full definition of
11081 -- this concurrent type) whose name matches the entry Ent and has a
11082 -- profile conformant with the profile of the corresponding (not yet
11083 -- built) dispatching primitive of Ent; return Empty if not found.
11085 function Matching_Original_Protected_Subprogram
11086 (Prot_Typ : Entity_Id;
11087 Subp : Entity_Id) return Entity_Id;
11088 -- Return the first subprogram defined in the enclosing scope of
11089 -- Prot_Typ (before the full definition of this protected type)
11090 -- whose name matches the original name of Subp and has a profile
11091 -- conformant with the profile of Subp; return Empty if not found.
11093 function Normalized_First_Parameter_Type
11094 (E : Entity_Id) return Entity_Id;
11095 -- Return the type of the first parameter unless that type
11096 -- is an anonymous access type, in which case return the
11097 -- designated type. Used to treat anonymous-access-to-synchronized
11098 -- the same as synchronized for purposes of checking for
11099 -- prefixed view profile conflicts.
11101 ---------------------------------
11102 -- Check_Conforming_Parameters --
11103 ---------------------------------
11105 function Check_Conforming_Parameters
11106 (E1_Param : Node_Id;
11107 E2_Param : Node_Id;
11108 Ctype : Conformance_Type) return Boolean
11110 Param_E1 : Node_Id := E1_Param;
11111 Param_E2 : Node_Id := E2_Param;
11114 while Present (Param_E1) and then Present (Param_E2) loop
11115 if (Ctype >= Mode_Conformant) and then
11116 Ekind (Defining_Identifier (Param_E1)) /=
11117 Ekind (Defining_Identifier (Param_E2))
11122 (Find_Parameter_Type (Param_E1),
11123 Find_Parameter_Type (Param_E2),
11133 -- The candidate is not valid if one of the two lists contains
11134 -- more parameters than the other
11136 return No (Param_E1) and then No (Param_E2);
11137 end Check_Conforming_Parameters;
11139 ----------------------------------
11140 -- Matching_Entry_Or_Subprogram --
11141 ----------------------------------
11143 function Matching_Entry_Or_Subprogram
11144 (Conc_Typ : Entity_Id;
11145 Subp : Entity_Id) return Entity_Id
11150 E := First_Entity (Conc_Typ);
11151 while Present (E) loop
11152 if Chars (Subp) = Chars (E)
11153 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11155 Check_Conforming_Parameters
11156 (First (Parameter_Specifications (Parent (E))),
11157 Next (First (Parameter_Specifications (Parent (Subp)))),
11167 end Matching_Entry_Or_Subprogram;
11169 -------------------------------------
11170 -- Matching_Dispatching_Subprogram --
11171 -------------------------------------
11173 function Matching_Dispatching_Subprogram
11174 (Conc_Typ : Entity_Id;
11175 Ent : Entity_Id) return Entity_Id
11180 -- Search for entities in the enclosing scope of this synchonized
11183 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11184 Push_Scope (Scope (Conc_Typ));
11185 E := Current_Entity_In_Scope (Ent);
11188 while Present (E) loop
11189 if Scope (E) = Scope (Conc_Typ)
11190 and then Comes_From_Source (E)
11191 and then Ekind (E) = E_Procedure
11192 and then Present (First_Entity (E))
11193 and then Is_Controlling_Formal (First_Entity (E))
11194 and then Etype (First_Entity (E)) = Conc_Typ
11196 Check_Conforming_Parameters
11197 (First (Parameter_Specifications (Parent (Ent))),
11198 Next (First (Parameter_Specifications (Parent (E)))),
11199 Subtype_Conformant)
11208 end Matching_Dispatching_Subprogram;
11210 --------------------------------------------
11211 -- Matching_Original_Protected_Subprogram --
11212 --------------------------------------------
11214 function Matching_Original_Protected_Subprogram
11215 (Prot_Typ : Entity_Id;
11216 Subp : Entity_Id) return Entity_Id
11218 ICF : constant Boolean :=
11219 Is_Controlling_Formal (First_Entity (Subp));
11223 -- Temporarily decorate the first parameter of Subp as controlling
11224 -- formal, required to invoke Subtype_Conformant.
11226 Set_Is_Controlling_Formal (First_Entity (Subp));
11229 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11231 while Present (E) loop
11232 if Scope (E) = Scope (Prot_Typ)
11233 and then Comes_From_Source (E)
11234 and then Ekind (Subp) = Ekind (E)
11235 and then Present (First_Entity (E))
11236 and then Is_Controlling_Formal (First_Entity (E))
11237 and then Etype (First_Entity (E)) = Prot_Typ
11238 and then Subtype_Conformant (Subp, E,
11239 Skip_Controlling_Formals => True)
11241 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11248 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11251 end Matching_Original_Protected_Subprogram;
11253 -------------------------------------
11254 -- Normalized_First_Parameter_Type --
11255 -------------------------------------
11257 function Normalized_First_Parameter_Type
11258 (E : Entity_Id) return Entity_Id
11260 Result : Entity_Id := Etype (First_Entity (E));
11262 if Ekind (Result) = E_Anonymous_Access_Type then
11263 Result := Designated_Type (Result);
11266 end Normalized_First_Parameter_Type;
11268 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11271 -- Case 1: E is a subprogram whose first formal is a concurrent type
11272 -- defined in the scope of E that has an entry or subprogram whose
11273 -- profile matches E.
11275 if Comes_From_Source (E)
11276 and then Is_Subprogram (E)
11277 and then Present (First_Entity (E))
11278 and then Is_Concurrent_Record_Type
11279 (Normalized_First_Parameter_Type (E))
11282 Scope (Corresponding_Concurrent_Type
11283 (Normalized_First_Parameter_Type (E)))
11286 (Matching_Entry_Or_Subprogram
11287 (Corresponding_Concurrent_Type
11288 (Normalized_First_Parameter_Type (E)),
11291 Report_Conflict (E,
11292 Matching_Entry_Or_Subprogram
11293 (Corresponding_Concurrent_Type
11294 (Normalized_First_Parameter_Type (E)),
11299 -- Case 2: E is an internally built dispatching subprogram of a
11300 -- protected type and there is a subprogram defined in the enclosing
11301 -- scope of the protected type that has the original name of E and
11302 -- its profile is conformant with the profile of E. We check the
11303 -- name of the original protected subprogram associated with E since
11304 -- the expander builds dispatching primitives of protected functions
11305 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11307 elsif not Comes_From_Source (E)
11308 and then Is_Subprogram (E)
11309 and then Present (First_Entity (E))
11310 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11311 and then Present (Original_Protected_Subprogram (E))
11314 (Matching_Original_Protected_Subprogram
11315 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11318 Report_Conflict (E,
11319 Matching_Original_Protected_Subprogram
11320 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11324 -- Case 3: E is an entry of a synchronized type and a matching
11325 -- procedure has been previously defined in the enclosing scope
11326 -- of the synchronized type.
11328 elsif Comes_From_Source (E)
11329 and then Ekind (E) = E_Entry
11331 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11333 Report_Conflict (E,
11334 Matching_Dispatching_Subprogram (Current_Scope, E));
11339 end Has_Matching_Entry_Or_Subprogram;
11341 ----------------------------
11342 -- Is_Private_Declaration --
11343 ----------------------------
11345 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11346 Decl : constant Node_Id := Unit_Declaration_Node (E);
11347 Priv_Decls : List_Id;
11350 if Is_Package_Or_Generic_Package (Current_Scope)
11351 and then In_Private_Part (Current_Scope)
11354 Private_Declarations (Package_Specification (Current_Scope));
11356 return In_Package_Body (Current_Scope)
11358 (Is_List_Member (Decl)
11359 and then List_Containing (Decl) = Priv_Decls)
11360 or else (Nkind (Parent (Decl)) = N_Package_Specification
11362 Is_Compilation_Unit
11363 (Defining_Entity (Parent (Decl)))
11364 and then List_Containing (Parent (Parent (Decl))) =
11369 end Is_Private_Declaration;
11371 --------------------------
11372 -- Is_Overriding_Alias --
11373 --------------------------
11375 function Is_Overriding_Alias
11376 (Old_E : Entity_Id;
11377 New_E : Entity_Id) return Boolean
11379 AO : constant Entity_Id := Alias (Old_E);
11380 AN : constant Entity_Id := Alias (New_E);
11383 return Scope (AO) /= Scope (AN)
11384 or else No (DTC_Entity (AO))
11385 or else No (DTC_Entity (AN))
11386 or else DT_Position (AO) = DT_Position (AN);
11387 end Is_Overriding_Alias;
11389 ---------------------
11390 -- Report_Conflict --
11391 ---------------------
11393 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11395 Error_Msg_Sloc := Sloc (E);
11397 -- Generate message, with useful additional warning if in generic
11399 if Is_Generic_Unit (E) then
11400 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11401 Error_Msg_N ("\& conflicts with declaration#", S);
11403 Error_Msg_N ("& conflicts with declaration#", S);
11405 end Report_Conflict;
11407 -- Start of processing for New_Overloaded_Entity
11410 -- We need to look for an entity that S may override. This must be a
11411 -- homonym in the current scope, so we look for the first homonym of
11412 -- S in the current scope as the starting point for the search.
11414 E := Current_Entity_In_Scope (S);
11416 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11417 -- They are directly added to the list of primitive operations of
11418 -- Derived_Type, unless this is a rederivation in the private part
11419 -- of an operation that was already derived in the visible part of
11420 -- the current package.
11422 if Ada_Version >= Ada_2005
11423 and then Present (Derived_Type)
11424 and then Present (Alias (S))
11425 and then Is_Dispatching_Operation (Alias (S))
11426 and then Present (Find_Dispatching_Type (Alias (S)))
11427 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11429 -- For private types, when the full-view is processed we propagate to
11430 -- the full view the non-overridden entities whose attribute "alias"
11431 -- references an interface primitive. These entities were added by
11432 -- Derive_Subprograms to ensure that interface primitives are
11435 -- Inside_Freeze_Actions is non zero when S corresponds with an
11436 -- internal entity that links an interface primitive with its
11437 -- covering primitive through attribute Interface_Alias (see
11438 -- Add_Internal_Interface_Entities).
11440 if Inside_Freezing_Actions = 0
11441 and then Is_Package_Or_Generic_Package (Current_Scope)
11442 and then In_Private_Part (Current_Scope)
11443 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11444 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11445 and then Full_View (Defining_Identifier (Parent (E)))
11446 = Defining_Identifier (Parent (S))
11447 and then Alias (E) = Alias (S)
11449 Check_Operation_From_Private_View (S, E);
11450 Set_Is_Dispatching_Operation (S);
11455 Enter_Overloaded_Entity (S);
11456 Check_Dispatching_Operation (S, Empty);
11457 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11463 -- For synchronized types check conflicts of this entity with previously
11464 -- defined entities.
11466 if Ada_Version >= Ada_2005
11467 and then Has_Matching_Entry_Or_Subprogram (S)
11472 -- If there is no homonym then this is definitely not overriding
11475 Enter_Overloaded_Entity (S);
11476 Check_Dispatching_Operation (S, Empty);
11477 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11479 -- If subprogram has an explicit declaration, check whether it has an
11480 -- overriding indicator.
11482 if Comes_From_Source (S) then
11483 Check_Synchronized_Overriding (S, Overridden_Subp);
11485 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11486 -- it may have overridden some hidden inherited primitive. Update
11487 -- Overridden_Subp to avoid spurious errors when checking the
11488 -- overriding indicator.
11490 if Ada_Version >= Ada_2012
11491 and then No (Overridden_Subp)
11492 and then Is_Dispatching_Operation (S)
11493 and then Present (Overridden_Operation (S))
11495 Overridden_Subp := Overridden_Operation (S);
11498 Check_Overriding_Indicator
11499 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11501 -- The Ghost policy in effect at the point of declaration of a
11502 -- parent subprogram and an overriding subprogram must match
11503 -- (SPARK RM 6.9(17)).
11505 Check_Ghost_Overriding (S, Overridden_Subp);
11508 -- If there is a homonym that is not overloadable, then we have an
11509 -- error, except for the special cases checked explicitly below.
11511 elsif not Is_Overloadable (E) then
11513 -- Check for spurious conflict produced by a subprogram that has the
11514 -- same name as that of the enclosing generic package. The conflict
11515 -- occurs within an instance, between the subprogram and the renaming
11516 -- declaration for the package. After the subprogram, the package
11517 -- renaming declaration becomes hidden.
11519 if Ekind (E) = E_Package
11520 and then Present (Renamed_Object (E))
11521 and then Renamed_Object (E) = Current_Scope
11522 and then Nkind (Parent (Renamed_Object (E))) =
11523 N_Package_Specification
11524 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11527 Set_Is_Immediately_Visible (E, False);
11528 Enter_Overloaded_Entity (S);
11529 Set_Homonym (S, Homonym (E));
11530 Check_Dispatching_Operation (S, Empty);
11531 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11533 -- If the subprogram is implicit it is hidden by the previous
11534 -- declaration. However if it is dispatching, it must appear in the
11535 -- dispatch table anyway, because it can be dispatched to even if it
11536 -- cannot be called directly.
11538 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11539 Set_Scope (S, Current_Scope);
11541 if Is_Dispatching_Operation (Alias (S)) then
11542 Check_Dispatching_Operation (S, Empty);
11548 Report_Conflict (S, E);
11552 -- E exists and is overloadable
11555 Check_Synchronized_Overriding (S, Overridden_Subp);
11557 -- Loop through E and its homonyms to determine if any of them is
11558 -- the candidate for overriding by S.
11560 while Present (E) loop
11562 -- Definitely not interesting if not in the current scope
11564 if Scope (E) /= Current_Scope then
11567 -- A function can overload the name of an abstract state. The
11568 -- state can be viewed as a function with a profile that cannot
11569 -- be matched by anything.
11571 elsif Ekind (S) = E_Function
11572 and then Ekind (E) = E_Abstract_State
11574 Enter_Overloaded_Entity (S);
11577 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11578 -- procedures locate the internally generated spec. We enforce
11579 -- mode conformance since a tagged type may inherit from
11580 -- interfaces several null primitives which differ only in
11581 -- the mode of the formals.
11583 elsif not Comes_From_Source (S)
11584 and then Is_Null_Procedure (S)
11585 and then not Mode_Conformant (E, S)
11589 -- Check if we have type conformance
11591 elsif Type_Conformant (E, S) then
11593 -- If the old and new entities have the same profile and one
11594 -- is not the body of the other, then this is an error, unless
11595 -- one of them is implicitly declared.
11597 -- There are some cases when both can be implicit, for example
11598 -- when both a literal and a function that overrides it are
11599 -- inherited in a derivation, or when an inherited operation
11600 -- of a tagged full type overrides the inherited operation of
11601 -- a private extension. Ada 83 had a special rule for the
11602 -- literal case. In Ada 95, the later implicit operation hides
11603 -- the former, and the literal is always the former. In the
11604 -- odd case where both are derived operations declared at the
11605 -- same point, both operations should be declared, and in that
11606 -- case we bypass the following test and proceed to the next
11607 -- part. This can only occur for certain obscure cases in
11608 -- instances, when an operation on a type derived from a formal
11609 -- private type does not override a homograph inherited from
11610 -- the actual. In subsequent derivations of such a type, the
11611 -- DT positions of these operations remain distinct, if they
11614 if Present (Alias (S))
11615 and then (No (Alias (E))
11616 or else Comes_From_Source (E)
11617 or else Is_Abstract_Subprogram (S)
11619 (Is_Dispatching_Operation (E)
11620 and then Is_Overriding_Alias (E, S)))
11621 and then Ekind (E) /= E_Enumeration_Literal
11623 -- When an derived operation is overloaded it may be due to
11624 -- the fact that the full view of a private extension
11625 -- re-inherits. It has to be dealt with.
11627 if Is_Package_Or_Generic_Package (Current_Scope)
11628 and then In_Private_Part (Current_Scope)
11630 Check_Operation_From_Private_View (S, E);
11633 -- In any case the implicit operation remains hidden by the
11634 -- existing declaration, which is overriding. Indicate that
11635 -- E overrides the operation from which S is inherited.
11637 if Present (Alias (S)) then
11638 Set_Overridden_Operation (E, Alias (S));
11639 Inherit_Subprogram_Contract (E, Alias (S));
11642 Set_Overridden_Operation (E, S);
11643 Inherit_Subprogram_Contract (E, S);
11646 -- When a dispatching operation overrides an inherited
11647 -- subprogram, it shall be subtype conformant with the
11648 -- inherited subprogram (RM 3.9.2 (10.2)).
11650 if Comes_From_Source (E)
11651 and then Is_Dispatching_Operation (E)
11652 and then Find_Dispatching_Type (S)
11653 = Find_Dispatching_Type (E)
11655 Check_Subtype_Conformant (E, S);
11658 if Comes_From_Source (E) then
11659 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11661 -- The Ghost policy in effect at the point of declaration
11662 -- of a parent subprogram and an overriding subprogram
11663 -- must match (SPARK RM 6.9(17)).
11665 Check_Ghost_Overriding (E, S);
11670 -- Within an instance, the renaming declarations for actual
11671 -- subprograms may become ambiguous, but they do not hide each
11674 elsif Ekind (E) /= E_Entry
11675 and then not Comes_From_Source (E)
11676 and then not Is_Generic_Instance (E)
11677 and then (Present (Alias (E))
11678 or else Is_Intrinsic_Subprogram (E))
11679 and then (not In_Instance
11680 or else No (Parent (E))
11681 or else Nkind (Unit_Declaration_Node (E)) /=
11682 N_Subprogram_Renaming_Declaration)
11684 -- A subprogram child unit is not allowed to override an
11685 -- inherited subprogram (10.1.1(20)).
11687 if Is_Child_Unit (S) then
11689 ("child unit overrides inherited subprogram in parent",
11694 if Is_Non_Overriding_Operation (E, S) then
11695 Enter_Overloaded_Entity (S);
11697 if No (Derived_Type)
11698 or else Is_Tagged_Type (Derived_Type)
11700 Check_Dispatching_Operation (S, Empty);
11706 -- E is a derived operation or an internal operator which
11707 -- is being overridden. Remove E from further visibility.
11708 -- Furthermore, if E is a dispatching operation, it must be
11709 -- replaced in the list of primitive operations of its type
11710 -- (see Override_Dispatching_Operation).
11712 Overridden_Subp := E;
11714 -- It is possible for E to be in the current scope and
11715 -- yet not in the entity chain. This can only occur in a
11716 -- generic context where E is an implicit concatenation
11717 -- in the formal part, because in a generic body the
11718 -- entity chain starts with the formals.
11720 -- In GNATprove mode, a wrapper for an operation with
11721 -- axiomatization may be a homonym of another declaration
11722 -- for an actual subprogram (needs refinement ???).
11724 if No (Prev_Entity (E)) then
11726 and then GNATprove_Mode
11728 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11729 N_Subprogram_Renaming_Declaration
11733 pragma Assert (Chars (E) = Name_Op_Concat);
11738 -- E must be removed both from the entity_list of the
11739 -- current scope, and from the visibility chain.
11741 if Debug_Flag_E then
11742 Write_Str ("Override implicit operation ");
11743 Write_Int (Int (E));
11747 -- If E is a predefined concatenation, it stands for four
11748 -- different operations. As a result, a single explicit
11749 -- declaration does not hide it. In a possible ambiguous
11750 -- situation, Disambiguate chooses the user-defined op,
11751 -- so it is correct to retain the previous internal one.
11753 if Chars (E) /= Name_Op_Concat
11754 or else Ekind (E) /= E_Operator
11756 -- For nondispatching derived operations that are
11757 -- overridden by a subprogram declared in the private
11758 -- part of a package, we retain the derived subprogram
11759 -- but mark it as not immediately visible. If the
11760 -- derived operation was declared in the visible part
11761 -- then this ensures that it will still be visible
11762 -- outside the package with the proper signature
11763 -- (calls from outside must also be directed to this
11764 -- version rather than the overriding one, unlike the
11765 -- dispatching case). Calls from inside the package
11766 -- will still resolve to the overriding subprogram
11767 -- since the derived one is marked as not visible
11768 -- within the package.
11770 -- If the private operation is dispatching, we achieve
11771 -- the overriding by keeping the implicit operation
11772 -- but setting its alias to be the overriding one. In
11773 -- this fashion the proper body is executed in all
11774 -- cases, but the original signature is used outside
11777 -- If the overriding is not in the private part, we
11778 -- remove the implicit operation altogether.
11780 if Is_Private_Declaration (S) then
11781 if not Is_Dispatching_Operation (E) then
11782 Set_Is_Immediately_Visible (E, False);
11784 -- Work done in Override_Dispatching_Operation, so
11785 -- nothing else needs to be done here.
11791 Remove_Entity_And_Homonym (E);
11795 Enter_Overloaded_Entity (S);
11797 -- For entities generated by Derive_Subprograms the
11798 -- overridden operation is the inherited primitive
11799 -- (which is available through the attribute alias).
11801 if not (Comes_From_Source (E))
11802 and then Is_Dispatching_Operation (E)
11803 and then Find_Dispatching_Type (E) =
11804 Find_Dispatching_Type (S)
11805 and then Present (Alias (E))
11806 and then Comes_From_Source (Alias (E))
11808 Set_Overridden_Operation (S, Alias (E));
11809 Inherit_Subprogram_Contract (S, Alias (E));
11811 -- Normal case of setting entity as overridden
11813 -- Note: Static_Initialization and Overridden_Operation
11814 -- attributes use the same field in subprogram entities.
11815 -- Static_Initialization is only defined for internal
11816 -- initialization procedures, where Overridden_Operation
11817 -- is irrelevant. Therefore the setting of this attribute
11818 -- must check whether the target is an init_proc.
11820 elsif not Is_Init_Proc (S) then
11821 Set_Overridden_Operation (S, E);
11822 Inherit_Subprogram_Contract (S, E);
11825 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11827 -- The Ghost policy in effect at the point of declaration
11828 -- of a parent subprogram and an overriding subprogram
11829 -- must match (SPARK RM 6.9(17)).
11831 Check_Ghost_Overriding (S, E);
11833 -- If S is a user-defined subprogram or a null procedure
11834 -- expanded to override an inherited null procedure, or a
11835 -- predefined dispatching primitive then indicate that E
11836 -- overrides the operation from which S is inherited.
11838 if Comes_From_Source (S)
11840 (Present (Parent (S))
11841 and then Nkind (Parent (S)) = N_Procedure_Specification
11842 and then Null_Present (Parent (S)))
11844 (Present (Alias (E))
11846 Is_Predefined_Dispatching_Operation (Alias (E)))
11848 if Present (Alias (E)) then
11849 Set_Overridden_Operation (S, Alias (E));
11850 Inherit_Subprogram_Contract (S, Alias (E));
11854 if Is_Dispatching_Operation (E) then
11856 -- An overriding dispatching subprogram inherits the
11857 -- convention of the overridden subprogram (AI-117).
11859 Set_Convention (S, Convention (E));
11860 Check_Dispatching_Operation (S, E);
11863 Check_Dispatching_Operation (S, Empty);
11866 Check_For_Primitive_Subprogram
11867 (Is_Primitive_Subp, Is_Overriding => True);
11868 goto Check_Inequality;
11870 -- Apparent redeclarations in instances can occur when two
11871 -- formal types get the same actual type. The subprograms in
11872 -- in the instance are legal, even if not callable from the
11873 -- outside. Calls from within are disambiguated elsewhere.
11874 -- For dispatching operations in the visible part, the usual
11875 -- rules apply, and operations with the same profile are not
11876 -- legal (B830001).
11878 elsif (In_Instance_Visible_Part
11879 and then not Is_Dispatching_Operation (E))
11880 or else In_Instance_Not_Visible
11884 -- Here we have a real error (identical profile)
11887 Error_Msg_Sloc := Sloc (E);
11889 -- Avoid cascaded errors if the entity appears in
11890 -- subsequent calls.
11892 Set_Scope (S, Current_Scope);
11894 -- Generate error, with extra useful warning for the case
11895 -- of a generic instance with no completion.
11897 if Is_Generic_Instance (S)
11898 and then not Has_Completion (E)
11901 ("instantiation cannot provide body for&", S);
11902 Error_Msg_N ("\& conflicts with declaration#", S);
11904 Error_Msg_N ("& conflicts with declaration#", S);
11911 -- If one subprogram has an access parameter and the other
11912 -- a parameter of an access type, calls to either might be
11913 -- ambiguous. Verify that parameters match except for the
11914 -- access parameter.
11916 if May_Hide_Profile then
11922 F1 := First_Formal (S);
11923 F2 := First_Formal (E);
11924 while Present (F1) and then Present (F2) loop
11925 if Is_Access_Type (Etype (F1)) then
11926 if not Is_Access_Type (Etype (F2))
11927 or else not Conforming_Types
11928 (Designated_Type (Etype (F1)),
11929 Designated_Type (Etype (F2)),
11932 May_Hide_Profile := False;
11936 not Conforming_Types
11937 (Etype (F1), Etype (F2), Type_Conformant)
11939 May_Hide_Profile := False;
11946 if May_Hide_Profile
11950 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11959 -- On exit, we know that S is a new entity
11961 Enter_Overloaded_Entity (S);
11962 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11963 Check_Overriding_Indicator
11964 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11966 -- The Ghost policy in effect at the point of declaration of a parent
11967 -- subprogram and an overriding subprogram must match
11968 -- (SPARK RM 6.9(17)).
11970 Check_Ghost_Overriding (S, Overridden_Subp);
11972 -- If S is a derived operation for an untagged type then by
11973 -- definition it's not a dispatching operation (even if the parent
11974 -- operation was dispatching), so Check_Dispatching_Operation is not
11975 -- called in that case.
11977 if No (Derived_Type)
11978 or else Is_Tagged_Type (Derived_Type)
11980 Check_Dispatching_Operation (S, Empty);
11984 -- If this is a user-defined equality operator that is not a derived
11985 -- subprogram, create the corresponding inequality. If the operation is
11986 -- dispatching, the expansion is done elsewhere, and we do not create
11987 -- an explicit inequality operation.
11989 <<Check_Inequality>>
11990 if Chars (S) = Name_Op_Eq
11991 and then Etype (S) = Standard_Boolean
11992 and then Present (Parent (S))
11993 and then not Is_Dispatching_Operation (S)
11995 Make_Inequality_Operator (S);
11996 Check_Untagged_Equality (S);
11998 end New_Overloaded_Entity;
12000 ----------------------------------
12001 -- Preanalyze_Formal_Expression --
12002 ----------------------------------
12004 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12005 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12007 In_Spec_Expression := True;
12008 Preanalyze_With_Freezing_And_Resolve (N, T);
12009 In_Spec_Expression := Save_In_Spec_Expression;
12010 end Preanalyze_Formal_Expression;
12012 ---------------------
12013 -- Process_Formals --
12014 ---------------------
12016 procedure Process_Formals
12018 Related_Nod : Node_Id)
12020 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12021 -- Determine whether an access type designates a type coming from a
12024 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12025 -- Check whether the default has a class-wide type. After analysis the
12026 -- default has the type of the formal, so we must also check explicitly
12027 -- for an access attribute.
12029 ----------------------------------
12030 -- Designates_From_Limited_With --
12031 ----------------------------------
12033 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12034 Desig : Entity_Id := Typ;
12037 if Is_Access_Type (Desig) then
12038 Desig := Directly_Designated_Type (Desig);
12041 if Is_Class_Wide_Type (Desig) then
12042 Desig := Root_Type (Desig);
12046 Ekind (Desig) = E_Incomplete_Type
12047 and then From_Limited_With (Desig);
12048 end Designates_From_Limited_With;
12050 ---------------------------
12051 -- Is_Class_Wide_Default --
12052 ---------------------------
12054 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12056 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12057 or else (Nkind (D) = N_Attribute_Reference
12058 and then Attribute_Name (D) = Name_Access
12059 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12060 end Is_Class_Wide_Default;
12064 Context : constant Node_Id := Parent (Parent (T));
12066 Formal : Entity_Id;
12067 Formal_Type : Entity_Id;
12068 Param_Spec : Node_Id;
12071 Num_Out_Params : Nat := 0;
12072 First_Out_Param : Entity_Id := Empty;
12073 -- Used for setting Is_Only_Out_Parameter
12075 -- Start of processing for Process_Formals
12078 -- In order to prevent premature use of the formals in the same formal
12079 -- part, the Ekind is left undefined until all default expressions are
12080 -- analyzed. The Ekind is established in a separate loop at the end.
12082 Param_Spec := First (T);
12083 while Present (Param_Spec) loop
12084 Formal := Defining_Identifier (Param_Spec);
12085 Set_Never_Set_In_Source (Formal, True);
12086 Enter_Name (Formal);
12088 -- Case of ordinary parameters
12090 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12091 Find_Type (Parameter_Type (Param_Spec));
12092 Ptype := Parameter_Type (Param_Spec);
12094 if Ptype = Error then
12098 -- Protect against malformed parameter types
12100 if Nkind (Ptype) not in N_Has_Entity then
12101 Formal_Type := Any_Type;
12103 Formal_Type := Entity (Ptype);
12106 if Is_Incomplete_Type (Formal_Type)
12108 (Is_Class_Wide_Type (Formal_Type)
12109 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12111 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12112 -- primitive operations, as long as their completion is
12113 -- in the same declarative part. If in the private part
12114 -- this means that the type cannot be a Taft-amendment type.
12115 -- Check is done on package exit. For access to subprograms,
12116 -- the use is legal for Taft-amendment types.
12118 -- Ada 2012: tagged incomplete types are allowed as generic
12119 -- formal types. They do not introduce dependencies and the
12120 -- corresponding generic subprogram does not have a delayed
12121 -- freeze, because it does not need a freeze node. However,
12122 -- it is still the case that untagged incomplete types cannot
12123 -- be Taft-amendment types and must be completed in private
12124 -- part, so the subprogram must appear in the list of private
12125 -- dependents of the type.
12127 if Is_Tagged_Type (Formal_Type)
12128 or else (Ada_Version >= Ada_2012
12129 and then not From_Limited_With (Formal_Type)
12130 and then not Is_Generic_Type (Formal_Type))
12132 if Ekind (Scope (Current_Scope)) = E_Package
12133 and then not Is_Generic_Type (Formal_Type)
12134 and then not Is_Class_Wide_Type (Formal_Type)
12136 if Nkind (Parent (T)) not in
12137 N_Access_Function_Definition |
12138 N_Access_Procedure_Definition
12140 Append_Elmt (Current_Scope,
12141 Private_Dependents (Base_Type (Formal_Type)));
12143 -- Freezing is delayed to ensure that Register_Prim
12144 -- will get called for this operation, which is needed
12145 -- in cases where static dispatch tables aren't built.
12146 -- (Note that the same is done for controlling access
12147 -- parameter cases in function Access_Definition.)
12149 if not Is_Thunk (Current_Scope) then
12150 Set_Has_Delayed_Freeze (Current_Scope);
12155 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12156 | N_Access_Procedure_Definition
12158 -- AI05-0151: Tagged incomplete types are allowed in all
12159 -- formal parts. Untagged incomplete types are not allowed
12160 -- in bodies. Limited views of either kind are not allowed
12161 -- if there is no place at which the non-limited view can
12162 -- become available.
12164 -- Incomplete formal untagged types are not allowed in
12165 -- subprogram bodies (but are legal in their declarations).
12166 -- This excludes bodies created for null procedures, which
12167 -- are basic declarations.
12169 if Is_Generic_Type (Formal_Type)
12170 and then not Is_Tagged_Type (Formal_Type)
12171 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12174 ("invalid use of formal incomplete type", Param_Spec);
12176 elsif Ada_Version >= Ada_2012 then
12177 if Is_Tagged_Type (Formal_Type)
12178 and then (not From_Limited_With (Formal_Type)
12179 or else not In_Package_Body)
12183 elsif Nkind (Context) in N_Accept_Statement
12184 | N_Accept_Alternative
12186 or else (Nkind (Context) = N_Subprogram_Body
12187 and then Comes_From_Source (Context))
12190 ("invalid use of untagged incomplete type &",
12191 Ptype, Formal_Type);
12196 ("invalid use of incomplete type&",
12197 Param_Spec, Formal_Type);
12199 -- Further checks on the legality of incomplete types
12200 -- in formal parts are delayed until the freeze point
12201 -- of the enclosing subprogram or access to subprogram.
12205 elsif Ekind (Formal_Type) = E_Void then
12207 ("premature use of&",
12208 Parameter_Type (Param_Spec), Formal_Type);
12211 -- Ada 2012 (AI-142): Handle aliased parameters
12213 if Ada_Version >= Ada_2012
12214 and then Aliased_Present (Param_Spec)
12216 Set_Is_Aliased (Formal);
12218 -- AI12-001: All aliased objects are considered to be specified
12219 -- as independently addressable (RM C.6(8.1/4)).
12221 Set_Is_Independent (Formal);
12224 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12225 -- declaration corresponding to the null-excluding type of the
12226 -- formal in the enclosing scope. Finally, replace the parameter
12227 -- type of the formal with the internal subtype.
12229 if Ada_Version >= Ada_2005
12230 and then Null_Exclusion_Present (Param_Spec)
12232 if not Is_Access_Type (Formal_Type) then
12234 ("`NOT NULL` allowed only for an access type", Param_Spec);
12237 if Can_Never_Be_Null (Formal_Type)
12238 and then Comes_From_Source (Related_Nod)
12241 ("`NOT NULL` not allowed (& already excludes null)",
12242 Param_Spec, Formal_Type);
12246 Create_Null_Excluding_Itype
12248 Related_Nod => Related_Nod,
12249 Scope_Id => Scope (Current_Scope));
12251 -- If the designated type of the itype is an itype that is
12252 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12253 -- on the access subtype, to prevent order-of-elaboration
12254 -- issues in the backend.
12257 -- type T is access procedure;
12258 -- procedure Op (O : not null T);
12260 if Is_Itype (Directly_Designated_Type (Formal_Type))
12262 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12264 Set_Has_Delayed_Freeze (Formal_Type);
12269 -- An access formal type
12273 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12275 -- No need to continue if we already notified errors
12277 if not Present (Formal_Type) then
12281 -- Ada 2005 (AI-254)
12284 AD : constant Node_Id :=
12285 Access_To_Subprogram_Definition
12286 (Parameter_Type (Param_Spec));
12288 if Present (AD) and then Protected_Present (AD) then
12290 Replace_Anonymous_Access_To_Protected_Subprogram
12296 Set_Etype (Formal, Formal_Type);
12298 -- Deal with default expression if present
12300 Default := Expression (Param_Spec);
12302 if Present (Default) then
12303 if Out_Present (Param_Spec) then
12305 ("default initialization only allowed for IN parameters",
12309 -- Do the special preanalysis of the expression (see section on
12310 -- "Handling of Default Expressions" in the spec of package Sem).
12312 Preanalyze_Formal_Expression (Default, Formal_Type);
12314 -- An access to constant cannot be the default for
12315 -- an access parameter that is an access to variable.
12317 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12318 and then not Is_Access_Constant (Formal_Type)
12319 and then Is_Access_Type (Etype (Default))
12320 and then Is_Access_Constant (Etype (Default))
12323 ("formal that is access to variable cannot be initialized "
12324 & "with an access-to-constant expression", Default);
12327 -- Check that the designated type of an access parameter's default
12328 -- is not a class-wide type unless the parameter's designated type
12329 -- is also class-wide.
12331 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12332 and then not Designates_From_Limited_With (Formal_Type)
12333 and then Is_Class_Wide_Default (Default)
12334 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12337 ("access to class-wide expression not allowed here", Default);
12340 -- Check incorrect use of dynamically tagged expressions
12342 if Is_Tagged_Type (Formal_Type) then
12343 Check_Dynamically_Tagged_Expression
12345 Typ => Formal_Type,
12346 Related_Nod => Default);
12350 -- Ada 2005 (AI-231): Static checks
12352 if Ada_Version >= Ada_2005
12353 and then Is_Access_Type (Etype (Formal))
12354 and then Can_Never_Be_Null (Etype (Formal))
12356 Null_Exclusion_Static_Checks (Param_Spec);
12359 -- The following checks are relevant only when SPARK_Mode is on as
12360 -- these are not standard Ada legality rules.
12362 if SPARK_Mode = On then
12363 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
12365 -- A function cannot have a parameter of mode IN OUT or OUT
12368 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
12370 ("function cannot have parameter of mode `OUT` or "
12371 & "`IN OUT`", Formal);
12374 -- A procedure cannot have an effectively volatile formal
12375 -- parameter of mode IN because it behaves as a constant
12376 -- (SPARK RM 7.1.3(4)).
12378 elsif Ekind (Scope (Formal)) = E_Procedure
12379 and then Ekind (Formal) = E_In_Parameter
12380 and then Is_Effectively_Volatile (Formal)
12383 ("formal parameter of mode `IN` cannot be volatile", Formal);
12387 -- Deal with aspects on formal parameters. Only Unreferenced is
12388 -- supported for the time being.
12390 if Has_Aspects (Param_Spec) then
12392 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
12394 while Present (Aspect) loop
12395 if Chars (Identifier (Aspect)) = Name_Unreferenced then
12396 Set_Has_Pragma_Unreferenced (Formal);
12399 ("unsupported aspect& on parameter",
12400 Aspect, Identifier (Aspect));
12412 -- If this is the formal part of a function specification, analyze the
12413 -- subtype mark in the context where the formals are visible but not
12414 -- yet usable, and may hide outer homographs.
12416 if Nkind (Related_Nod) = N_Function_Specification then
12417 Analyze_Return_Type (Related_Nod);
12420 -- Now set the kind (mode) of each formal
12422 Param_Spec := First (T);
12423 while Present (Param_Spec) loop
12424 Formal := Defining_Identifier (Param_Spec);
12425 Set_Formal_Mode (Formal);
12427 if Ekind (Formal) = E_In_Parameter then
12428 Set_Default_Value (Formal, Expression (Param_Spec));
12430 if Present (Expression (Param_Spec)) then
12431 Default := Expression (Param_Spec);
12433 if Is_Scalar_Type (Etype (Default)) then
12434 if Nkind (Parameter_Type (Param_Spec)) /=
12435 N_Access_Definition
12437 Formal_Type := Entity (Parameter_Type (Param_Spec));
12441 (Related_Nod, Parameter_Type (Param_Spec));
12444 Apply_Scalar_Range_Check (Default, Formal_Type);
12448 elsif Ekind (Formal) = E_Out_Parameter then
12449 Num_Out_Params := Num_Out_Params + 1;
12451 if Num_Out_Params = 1 then
12452 First_Out_Param := Formal;
12455 elsif Ekind (Formal) = E_In_Out_Parameter then
12456 Num_Out_Params := Num_Out_Params + 1;
12459 -- Skip remaining processing if formal type was in error
12461 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12462 goto Next_Parameter;
12465 -- Force call by reference if aliased
12468 Conv : constant Convention_Id := Convention (Etype (Formal));
12470 if Is_Aliased (Formal) then
12471 Set_Mechanism (Formal, By_Reference);
12473 -- Warn if user asked this to be passed by copy
12475 if Conv = Convention_Ada_Pass_By_Copy then
12477 ("cannot pass aliased parameter & by copy??", Formal);
12480 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12482 elsif Conv = Convention_Ada_Pass_By_Copy then
12483 Set_Mechanism (Formal, By_Copy);
12485 elsif Conv = Convention_Ada_Pass_By_Reference then
12486 Set_Mechanism (Formal, By_Reference);
12494 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12495 Set_Is_Only_Out_Parameter (First_Out_Param);
12497 end Process_Formals;
12499 ----------------------------
12500 -- Reference_Body_Formals --
12501 ----------------------------
12503 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12508 if Error_Posted (Spec) then
12512 -- Iterate over both lists. They may be of different lengths if the two
12513 -- specs are not conformant.
12515 Fs := First_Formal (Spec);
12516 Fb := First_Formal (Bod);
12517 while Present (Fs) and then Present (Fb) loop
12518 Generate_Reference (Fs, Fb, 'b');
12520 if Style_Check then
12521 Style.Check_Identifier (Fb, Fs);
12524 Set_Spec_Entity (Fb, Fs);
12525 Set_Referenced (Fs, False);
12529 end Reference_Body_Formals;
12531 -------------------------
12532 -- Set_Actual_Subtypes --
12533 -------------------------
12535 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12537 Formal : Entity_Id;
12539 First_Stmt : Node_Id := Empty;
12540 AS_Needed : Boolean;
12543 -- If this is an empty initialization procedure, no need to create
12544 -- actual subtypes (small optimization).
12546 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12549 -- Within a predicate function we do not want to generate local
12550 -- subtypes that may generate nested predicate functions.
12552 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12556 -- The subtype declarations may freeze the formals. The body generated
12557 -- for an expression function is not a freeze point, so do not emit
12558 -- these declarations (small loss of efficiency in rare cases).
12560 if Nkind (N) = N_Subprogram_Body
12561 and then Was_Expression_Function (N)
12566 Formal := First_Formal (Subp);
12567 while Present (Formal) loop
12568 T := Etype (Formal);
12570 -- We never need an actual subtype for a constrained formal
12572 if Is_Constrained (T) then
12573 AS_Needed := False;
12575 -- If we have unknown discriminants, then we do not need an actual
12576 -- subtype, or more accurately we cannot figure it out. Note that
12577 -- all class-wide types have unknown discriminants.
12579 elsif Has_Unknown_Discriminants (T) then
12580 AS_Needed := False;
12582 -- At this stage we have an unconstrained type that may need an
12583 -- actual subtype. For sure the actual subtype is needed if we have
12584 -- an unconstrained array type. However, in an instance, the type
12585 -- may appear as a subtype of the full view, while the actual is
12586 -- in fact private (in which case no actual subtype is needed) so
12587 -- check the kind of the base type.
12589 elsif Is_Array_Type (Base_Type (T)) then
12592 -- The only other case needing an actual subtype is an unconstrained
12593 -- record type which is an IN parameter (we cannot generate actual
12594 -- subtypes for the OUT or IN OUT case, since an assignment can
12595 -- change the discriminant values. However we exclude the case of
12596 -- initialization procedures, since discriminants are handled very
12597 -- specially in this context, see the section entitled "Handling of
12598 -- Discriminants" in Einfo.
12600 -- We also exclude the case of Discrim_SO_Functions (functions used
12601 -- in front-end layout mode for size/offset values), since in such
12602 -- functions only discriminants are referenced, and not only are such
12603 -- subtypes not needed, but they cannot always be generated, because
12604 -- of order of elaboration issues.
12606 elsif Is_Record_Type (T)
12607 and then Ekind (Formal) = E_In_Parameter
12608 and then Chars (Formal) /= Name_uInit
12609 and then not Is_Unchecked_Union (T)
12610 and then not Is_Discrim_SO_Function (Subp)
12614 -- All other cases do not need an actual subtype
12617 AS_Needed := False;
12620 -- Generate actual subtypes for unconstrained arrays and
12621 -- unconstrained discriminated records.
12624 if Nkind (N) = N_Accept_Statement then
12626 -- If expansion is active, the formal is replaced by a local
12627 -- variable that renames the corresponding entry of the
12628 -- parameter block, and it is this local variable that may
12629 -- require an actual subtype.
12631 if Expander_Active then
12632 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12634 Decl := Build_Actual_Subtype (T, Formal);
12637 if Present (Handled_Statement_Sequence (N)) then
12639 First (Statements (Handled_Statement_Sequence (N)));
12640 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12641 Mark_Rewrite_Insertion (Decl);
12643 -- If the accept statement has no body, there will be no
12644 -- reference to the actuals, so no need to compute actual
12651 Decl := Build_Actual_Subtype (T, Formal);
12652 Prepend (Decl, Declarations (N));
12653 Mark_Rewrite_Insertion (Decl);
12656 -- The declaration uses the bounds of an existing object, and
12657 -- therefore needs no constraint checks.
12659 Analyze (Decl, Suppress => All_Checks);
12660 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12662 -- We need to freeze manually the generated type when it is
12663 -- inserted anywhere else than in a declarative part.
12665 if Present (First_Stmt) then
12666 Insert_List_Before_And_Analyze (First_Stmt,
12667 Freeze_Entity (Defining_Identifier (Decl), N));
12669 -- Ditto if the type has a dynamic predicate, because the
12670 -- generated function will mention the actual subtype. The
12671 -- predicate may come from an explicit aspect of be inherited.
12673 elsif Has_Predicates (T) then
12674 Insert_List_After_And_Analyze (Decl,
12675 Freeze_Entity (Defining_Identifier (Decl), N));
12678 if Nkind (N) = N_Accept_Statement
12679 and then Expander_Active
12681 Set_Actual_Subtype (Renamed_Object (Formal),
12682 Defining_Identifier (Decl));
12684 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12688 Next_Formal (Formal);
12690 end Set_Actual_Subtypes;
12692 ---------------------
12693 -- Set_Formal_Mode --
12694 ---------------------
12696 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12697 Spec : constant Node_Id := Parent (Formal_Id);
12698 Id : constant Entity_Id := Scope (Formal_Id);
12701 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12702 -- since we ensure that corresponding actuals are always valid at the
12703 -- point of the call.
12705 if Out_Present (Spec) then
12707 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12709 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12712 if Ekind (Id) in E_Function | E_Generic_Function then
12714 -- [IN] OUT parameters allowed for functions in Ada 2012
12716 if Ada_Version >= Ada_2012 then
12718 -- Even in Ada 2012 operators can only have IN parameters
12720 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12721 Error_Msg_N ("operators can only have IN parameters", Spec);
12724 if In_Present (Spec) then
12725 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12727 Set_Ekind (Formal_Id, E_Out_Parameter);
12730 -- But not in earlier versions of Ada
12733 Error_Msg_N ("functions can only have IN parameters", Spec);
12734 Set_Ekind (Formal_Id, E_In_Parameter);
12737 elsif In_Present (Spec) then
12738 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12741 Set_Ekind (Formal_Id, E_Out_Parameter);
12742 Set_Never_Set_In_Source (Formal_Id, True);
12743 Set_Is_True_Constant (Formal_Id, False);
12744 Set_Current_Value (Formal_Id, Empty);
12748 Set_Ekind (Formal_Id, E_In_Parameter);
12751 -- Set Is_Known_Non_Null for access parameters since the language
12752 -- guarantees that access parameters are always non-null. We also set
12753 -- Can_Never_Be_Null, since there is no way to change the value.
12755 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12757 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12758 -- null; In Ada 2005, only if then null_exclusion is explicit.
12760 if Ada_Version < Ada_2005
12761 or else Can_Never_Be_Null (Etype (Formal_Id))
12763 Set_Is_Known_Non_Null (Formal_Id);
12764 Set_Can_Never_Be_Null (Formal_Id);
12767 -- Ada 2005 (AI-231): Null-exclusion access subtype
12769 elsif Is_Access_Type (Etype (Formal_Id))
12770 and then Can_Never_Be_Null (Etype (Formal_Id))
12772 Set_Is_Known_Non_Null (Formal_Id);
12774 -- We can also set Can_Never_Be_Null (thus preventing some junk
12775 -- access checks) for the case of an IN parameter, which cannot
12776 -- be changed, or for an IN OUT parameter, which can be changed but
12777 -- not to a null value. But for an OUT parameter, the initial value
12778 -- passed in can be null, so we can't set this flag in that case.
12780 if Ekind (Formal_Id) /= E_Out_Parameter then
12781 Set_Can_Never_Be_Null (Formal_Id);
12785 Set_Mechanism (Formal_Id, Default_Mechanism);
12786 Set_Formal_Validity (Formal_Id);
12787 end Set_Formal_Mode;
12789 -------------------------
12790 -- Set_Formal_Validity --
12791 -------------------------
12793 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12795 -- If no validity checking, then we cannot assume anything about the
12796 -- validity of parameters, since we do not know there is any checking
12797 -- of the validity on the call side.
12799 if not Validity_Checks_On then
12802 -- If validity checking for parameters is enabled, this means we are
12803 -- not supposed to make any assumptions about argument values.
12805 elsif Validity_Check_Parameters then
12808 -- If we are checking in parameters, we will assume that the caller is
12809 -- also checking parameters, so we can assume the parameter is valid.
12811 elsif Ekind (Formal_Id) = E_In_Parameter
12812 and then Validity_Check_In_Params
12814 Set_Is_Known_Valid (Formal_Id, True);
12816 -- Similar treatment for IN OUT parameters
12818 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12819 and then Validity_Check_In_Out_Params
12821 Set_Is_Known_Valid (Formal_Id, True);
12823 end Set_Formal_Validity;
12825 ------------------------
12826 -- Subtype_Conformant --
12827 ------------------------
12829 function Subtype_Conformant
12830 (New_Id : Entity_Id;
12831 Old_Id : Entity_Id;
12832 Skip_Controlling_Formals : Boolean := False) return Boolean
12836 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12837 Skip_Controlling_Formals => Skip_Controlling_Formals);
12839 end Subtype_Conformant;
12841 ---------------------
12842 -- Type_Conformant --
12843 ---------------------
12845 function Type_Conformant
12846 (New_Id : Entity_Id;
12847 Old_Id : Entity_Id;
12848 Skip_Controlling_Formals : Boolean := False) return Boolean
12852 May_Hide_Profile := False;
12854 (New_Id, Old_Id, Type_Conformant, False, Result,
12855 Skip_Controlling_Formals => Skip_Controlling_Formals);
12857 end Type_Conformant;
12859 -------------------------------
12860 -- Valid_Operator_Definition --
12861 -------------------------------
12863 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12866 Id : constant Name_Id := Chars (Designator);
12870 F := First_Formal (Designator);
12871 while Present (F) loop
12874 if Present (Default_Value (F)) then
12876 ("default values not allowed for operator parameters",
12879 -- For function instantiations that are operators, we must check
12880 -- separately that the corresponding generic only has in-parameters.
12881 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12882 -- an error could not arise in earlier versions of the language.
12884 elsif Ekind (F) /= E_In_Parameter then
12885 Error_Msg_N ("operators can only have IN parameters", F);
12891 -- Verify that user-defined operators have proper number of arguments
12892 -- First case of operators which can only be unary
12894 if Id in Name_Op_Not | Name_Op_Abs then
12897 -- Case of operators which can be unary or binary
12899 elsif Id in Name_Op_Add | Name_Op_Subtract then
12900 N_OK := (N in 1 .. 2);
12902 -- All other operators can only be binary
12910 ("incorrect number of arguments for operator", Designator);
12914 and then Base_Type (Etype (Designator)) = Standard_Boolean
12915 and then not Is_Intrinsic_Subprogram (Designator)
12918 ("explicit definition of inequality not allowed", Designator);
12920 end Valid_Operator_Definition;